blob: 16a59cafb8d346bde82f8a416fb91c737a764b0b [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
25#include <linux/config.h>
26#include <linux/module.h>
27#include <linux/kernel.h>
28#include <linux/kmod.h>
29#include <linux/init.h>
30#include <linux/list.h>
31#include <linux/devfs_fs_kernel.h>
32#include <linux/netdevice.h>
33#include <linux/poll.h>
34#include <linux/ppp_defs.h>
35#include <linux/filter.h>
36#include <linux/if_ppp.h>
37#include <linux/ppp_channel.h>
38#include <linux/ppp-comp.h>
39#include <linux/skbuff.h>
40#include <linux/rtnetlink.h>
41#include <linux/if_arp.h>
42#include <linux/ip.h>
43#include <linux/tcp.h>
44#include <linux/spinlock.h>
45#include <linux/smp_lock.h>
46#include <linux/rwsem.h>
47#include <linux/stddef.h>
48#include <linux/device.h>
Arjan van de Ven8ed965d2006-03-23 03:00:21 -080049#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <net/slhc_vj.h>
51#include <asm/atomic.h>
52
53#define PPP_VERSION "2.4.2"
54
55/*
56 * Network protocols we support.
57 */
58#define NP_IP 0 /* Internet Protocol V4 */
59#define NP_IPV6 1 /* Internet Protocol V6 */
60#define NP_IPX 2 /* IPX protocol */
61#define NP_AT 3 /* Appletalk protocol */
62#define NP_MPLS_UC 4 /* MPLS unicast */
63#define NP_MPLS_MC 5 /* MPLS multicast */
64#define NUM_NP 6 /* Number of NPs. */
65
66#define MPHDRLEN 6 /* multilink protocol header length */
67#define MPHDRLEN_SSN 4 /* ditto with short sequence numbers */
68#define MIN_FRAG_SIZE 64
69
70/*
71 * An instance of /dev/ppp can be associated with either a ppp
72 * interface unit or a ppp channel. In both cases, file->private_data
73 * points to one of these.
74 */
75struct ppp_file {
76 enum {
77 INTERFACE=1, CHANNEL
78 } kind;
79 struct sk_buff_head xq; /* pppd transmit queue */
80 struct sk_buff_head rq; /* receive queue for pppd */
81 wait_queue_head_t rwait; /* for poll on reading /dev/ppp */
82 atomic_t refcnt; /* # refs (incl /dev/ppp attached) */
83 int hdrlen; /* space to leave for headers */
84 int index; /* interface unit / channel number */
85 int dead; /* unit/channel has been shut down */
86};
87
88#define PF_TO_X(pf, X) ((X *)((char *)(pf) - offsetof(X, file)))
89
90#define PF_TO_PPP(pf) PF_TO_X(pf, struct ppp)
91#define PF_TO_CHANNEL(pf) PF_TO_X(pf, struct channel)
92
93#define ROUNDUP(n, x) (((n) + (x) - 1) / (x))
94
95/*
96 * Data structure describing one ppp unit.
97 * A ppp unit corresponds to a ppp network interface device
98 * and represents a multilink bundle.
99 * It can have 0 or more ppp channels connected to it.
100 */
101struct ppp {
102 struct ppp_file file; /* stuff for read/write/poll 0 */
103 struct file *owner; /* file that owns this unit 48 */
104 struct list_head channels; /* list of attached channels 4c */
105 int n_channels; /* how many channels are attached 54 */
106 spinlock_t rlock; /* lock for receive side 58 */
107 spinlock_t wlock; /* lock for transmit side 5c */
108 int mru; /* max receive unit 60 */
109 unsigned int flags; /* control bits 64 */
110 unsigned int xstate; /* transmit state bits 68 */
111 unsigned int rstate; /* receive state bits 6c */
112 int debug; /* debug flags 70 */
113 struct slcompress *vj; /* state for VJ header compression */
114 enum NPmode npmode[NUM_NP]; /* what to do with each net proto 78 */
115 struct sk_buff *xmit_pending; /* a packet ready to go out 88 */
116 struct compressor *xcomp; /* transmit packet compressor 8c */
117 void *xc_state; /* its internal state 90 */
118 struct compressor *rcomp; /* receive decompressor 94 */
119 void *rc_state; /* its internal state 98 */
120 unsigned long last_xmit; /* jiffies when last pkt sent 9c */
121 unsigned long last_recv; /* jiffies when last pkt rcvd a0 */
122 struct net_device *dev; /* network interface device a4 */
123#ifdef CONFIG_PPP_MULTILINK
124 int nxchan; /* next channel to send something on */
125 u32 nxseq; /* next sequence number to send */
126 int mrru; /* MP: max reconst. receive unit */
127 u32 nextseq; /* MP: seq no of next packet */
128 u32 minseq; /* MP: min of most recent seqnos */
129 struct sk_buff_head mrq; /* MP: receive reconstruction queue */
130#endif /* CONFIG_PPP_MULTILINK */
131 struct net_device_stats stats; /* statistics */
132#ifdef CONFIG_PPP_FILTER
133 struct sock_filter *pass_filter; /* filter for packets to pass */
134 struct sock_filter *active_filter;/* filter for pkts to reset idle */
135 unsigned pass_len, active_len;
136#endif /* CONFIG_PPP_FILTER */
137};
138
139/*
140 * Bits in flags: SC_NO_TCP_CCID, SC_CCP_OPEN, SC_CCP_UP, SC_LOOP_TRAFFIC,
Matt Domschb3f9b922005-11-08 09:40:47 -0800141 * SC_MULTILINK, SC_MP_SHORTSEQ, SC_MP_XSHORTSEQ, SC_COMP_TCP, SC_REJ_COMP_TCP,
142 * SC_MUST_COMP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 * Bits in rstate: SC_DECOMP_RUN, SC_DC_ERROR, SC_DC_FERROR.
144 * Bits in xstate: SC_COMP_RUN
145 */
146#define SC_FLAG_BITS (SC_NO_TCP_CCID|SC_CCP_OPEN|SC_CCP_UP|SC_LOOP_TRAFFIC \
147 |SC_MULTILINK|SC_MP_SHORTSEQ|SC_MP_XSHORTSEQ \
Matt Domschb3f9b922005-11-08 09:40:47 -0800148 |SC_COMP_TCP|SC_REJ_COMP_TCP|SC_MUST_COMP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150/*
151 * Private data structure for each channel.
152 * This includes the data structure used for multilink.
153 */
154struct channel {
155 struct ppp_file file; /* stuff for read/write/poll */
156 struct list_head list; /* link in all/new_channels list */
157 struct ppp_channel *chan; /* public channel data structure */
158 struct rw_semaphore chan_sem; /* protects `chan' during chan ioctl */
159 spinlock_t downl; /* protects `chan', file.xq dequeue */
160 struct ppp *ppp; /* ppp unit we're connected to */
161 struct list_head clist; /* link in list of channels per unit */
162 rwlock_t upl; /* protects `ppp' */
163#ifdef CONFIG_PPP_MULTILINK
164 u8 avail; /* flag used in multilink stuff */
165 u8 had_frag; /* >= 1 fragments have been sent */
166 u32 lastseq; /* MP: last sequence # received */
167#endif /* CONFIG_PPP_MULTILINK */
168};
169
170/*
171 * SMP locking issues:
172 * Both the ppp.rlock and ppp.wlock locks protect the ppp.channels
173 * list and the ppp.n_channels field, you need to take both locks
174 * before you modify them.
175 * The lock ordering is: channel.upl -> ppp.wlock -> ppp.rlock ->
176 * channel.downl.
177 */
178
179/*
180 * A cardmap represents a mapping from unsigned integers to pointers,
181 * and provides a fast "find lowest unused number" operation.
182 * It uses a broad (32-way) tree with a bitmap at each level.
183 * It is designed to be space-efficient for small numbers of entries
184 * and time-efficient for large numbers of entries.
185 */
186#define CARDMAP_ORDER 5
187#define CARDMAP_WIDTH (1U << CARDMAP_ORDER)
188#define CARDMAP_MASK (CARDMAP_WIDTH - 1)
189
190struct cardmap {
191 int shift;
192 unsigned long inuse;
193 struct cardmap *parent;
194 void *ptr[CARDMAP_WIDTH];
195};
196static void *cardmap_get(struct cardmap *map, unsigned int nr);
197static void cardmap_set(struct cardmap **map, unsigned int nr, void *ptr);
198static unsigned int cardmap_find_first_free(struct cardmap *map);
199static void cardmap_destroy(struct cardmap **map);
200
201/*
Arjan van de Ven8ed965d2006-03-23 03:00:21 -0800202 * all_ppp_mutex protects the all_ppp_units mapping.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 * It also ensures that finding a ppp unit in the all_ppp_units map
204 * and updating its file.refcnt field is atomic.
205 */
Arjan van de Ven8ed965d2006-03-23 03:00:21 -0800206static DEFINE_MUTEX(all_ppp_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207static struct cardmap *all_ppp_units;
208static atomic_t ppp_unit_count = ATOMIC_INIT(0);
209
210/*
211 * all_channels_lock protects all_channels and last_channel_index,
212 * and the atomicity of find a channel and updating its file.refcnt
213 * field.
214 */
215static DEFINE_SPINLOCK(all_channels_lock);
216static LIST_HEAD(all_channels);
217static LIST_HEAD(new_channels);
218static int last_channel_index;
219static atomic_t channel_count = ATOMIC_INIT(0);
220
221/* Get the PPP protocol number from a skb */
222#define PPP_PROTO(skb) (((skb)->data[0] << 8) + (skb)->data[1])
223
224/* We limit the length of ppp->file.rq to this (arbitrary) value */
225#define PPP_MAX_RQLEN 32
226
227/*
228 * Maximum number of multilink fragments queued up.
229 * This has to be large enough to cope with the maximum latency of
230 * the slowest channel relative to the others. Strictly it should
231 * depend on the number of channels and their characteristics.
232 */
233#define PPP_MP_MAX_QLEN 128
234
235/* Multilink header bits. */
236#define B 0x80 /* this fragment begins a packet */
237#define E 0x40 /* this fragment ends a packet */
238
239/* Compare multilink sequence numbers (assumed to be 32 bits wide) */
240#define seq_before(a, b) ((s32)((a) - (b)) < 0)
241#define seq_after(a, b) ((s32)((a) - (b)) > 0)
242
243/* Prototypes. */
244static int ppp_unattached_ioctl(struct ppp_file *pf, struct file *file,
245 unsigned int cmd, unsigned long arg);
246static void ppp_xmit_process(struct ppp *ppp);
247static void ppp_send_frame(struct ppp *ppp, struct sk_buff *skb);
248static void ppp_push(struct ppp *ppp);
249static void ppp_channel_push(struct channel *pch);
250static void ppp_receive_frame(struct ppp *ppp, struct sk_buff *skb,
251 struct channel *pch);
252static void ppp_receive_error(struct ppp *ppp);
253static void ppp_receive_nonmp_frame(struct ppp *ppp, struct sk_buff *skb);
254static struct sk_buff *ppp_decompress_frame(struct ppp *ppp,
255 struct sk_buff *skb);
256#ifdef CONFIG_PPP_MULTILINK
257static void ppp_receive_mp_frame(struct ppp *ppp, struct sk_buff *skb,
258 struct channel *pch);
259static void ppp_mp_insert(struct ppp *ppp, struct sk_buff *skb);
260static struct sk_buff *ppp_mp_reconstruct(struct ppp *ppp);
261static int ppp_mp_explode(struct ppp *ppp, struct sk_buff *skb);
262#endif /* CONFIG_PPP_MULTILINK */
263static int ppp_set_compress(struct ppp *ppp, unsigned long arg);
264static void ppp_ccp_peek(struct ppp *ppp, struct sk_buff *skb, int inbound);
265static void ppp_ccp_closed(struct ppp *ppp);
266static struct compressor *find_compressor(int type);
267static void ppp_get_stats(struct ppp *ppp, struct ppp_stats *st);
268static struct ppp *ppp_create_interface(int unit, int *retp);
269static void init_ppp_file(struct ppp_file *pf, int kind);
270static void ppp_shutdown_interface(struct ppp *ppp);
271static void ppp_destroy_interface(struct ppp *ppp);
272static struct ppp *ppp_find_unit(int unit);
273static struct channel *ppp_find_channel(int unit);
274static int ppp_connect_channel(struct channel *pch, int unit);
275static int ppp_disconnect_channel(struct channel *pch);
276static void ppp_destroy_channel(struct channel *pch);
277
gregkh@suse.de56b22932005-03-23 10:01:41 -0800278static struct class *ppp_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280/* Translates a PPP protocol number to a NP index (NP == network protocol) */
281static inline int proto_to_npindex(int proto)
282{
283 switch (proto) {
284 case PPP_IP:
285 return NP_IP;
286 case PPP_IPV6:
287 return NP_IPV6;
288 case PPP_IPX:
289 return NP_IPX;
290 case PPP_AT:
291 return NP_AT;
292 case PPP_MPLS_UC:
293 return NP_MPLS_UC;
294 case PPP_MPLS_MC:
295 return NP_MPLS_MC;
296 }
297 return -EINVAL;
298}
299
300/* Translates an NP index into a PPP protocol number */
301static const int npindex_to_proto[NUM_NP] = {
302 PPP_IP,
303 PPP_IPV6,
304 PPP_IPX,
305 PPP_AT,
306 PPP_MPLS_UC,
307 PPP_MPLS_MC,
308};
309
310/* Translates an ethertype into an NP index */
311static inline int ethertype_to_npindex(int ethertype)
312{
313 switch (ethertype) {
314 case ETH_P_IP:
315 return NP_IP;
316 case ETH_P_IPV6:
317 return NP_IPV6;
318 case ETH_P_IPX:
319 return NP_IPX;
320 case ETH_P_PPPTALK:
321 case ETH_P_ATALK:
322 return NP_AT;
323 case ETH_P_MPLS_UC:
324 return NP_MPLS_UC;
325 case ETH_P_MPLS_MC:
326 return NP_MPLS_MC;
327 }
328 return -1;
329}
330
331/* Translates an NP index into an ethertype */
332static const int npindex_to_ethertype[NUM_NP] = {
333 ETH_P_IP,
334 ETH_P_IPV6,
335 ETH_P_IPX,
336 ETH_P_PPPTALK,
337 ETH_P_MPLS_UC,
338 ETH_P_MPLS_MC,
339};
340
341/*
342 * Locking shorthand.
343 */
344#define ppp_xmit_lock(ppp) spin_lock_bh(&(ppp)->wlock)
345#define ppp_xmit_unlock(ppp) spin_unlock_bh(&(ppp)->wlock)
346#define ppp_recv_lock(ppp) spin_lock_bh(&(ppp)->rlock)
347#define ppp_recv_unlock(ppp) spin_unlock_bh(&(ppp)->rlock)
348#define ppp_lock(ppp) do { ppp_xmit_lock(ppp); \
349 ppp_recv_lock(ppp); } while (0)
350#define ppp_unlock(ppp) do { ppp_recv_unlock(ppp); \
351 ppp_xmit_unlock(ppp); } while (0)
352
353/*
354 * /dev/ppp device routines.
355 * The /dev/ppp device is used by pppd to control the ppp unit.
356 * It supports the read, write, ioctl and poll functions.
357 * Open instances of /dev/ppp can be in one of three states:
358 * unattached, attached to a ppp unit, or attached to a ppp channel.
359 */
360static int ppp_open(struct inode *inode, struct file *file)
361{
362 /*
363 * This could (should?) be enforced by the permissions on /dev/ppp.
364 */
365 if (!capable(CAP_NET_ADMIN))
366 return -EPERM;
367 return 0;
368}
369
370static int ppp_release(struct inode *inode, struct file *file)
371{
372 struct ppp_file *pf = file->private_data;
373 struct ppp *ppp;
374
375 if (pf != 0) {
376 file->private_data = NULL;
377 if (pf->kind == INTERFACE) {
378 ppp = PF_TO_PPP(pf);
379 if (file == ppp->owner)
380 ppp_shutdown_interface(ppp);
381 }
382 if (atomic_dec_and_test(&pf->refcnt)) {
383 switch (pf->kind) {
384 case INTERFACE:
385 ppp_destroy_interface(PF_TO_PPP(pf));
386 break;
387 case CHANNEL:
388 ppp_destroy_channel(PF_TO_CHANNEL(pf));
389 break;
390 }
391 }
392 }
393 return 0;
394}
395
396static ssize_t ppp_read(struct file *file, char __user *buf,
397 size_t count, loff_t *ppos)
398{
399 struct ppp_file *pf = file->private_data;
400 DECLARE_WAITQUEUE(wait, current);
401 ssize_t ret;
402 struct sk_buff *skb = NULL;
403
404 ret = count;
405
406 if (pf == 0)
407 return -ENXIO;
408 add_wait_queue(&pf->rwait, &wait);
409 for (;;) {
410 set_current_state(TASK_INTERRUPTIBLE);
411 skb = skb_dequeue(&pf->rq);
412 if (skb)
413 break;
414 ret = 0;
415 if (pf->dead)
416 break;
417 if (pf->kind == INTERFACE) {
418 /*
419 * Return 0 (EOF) on an interface that has no
420 * channels connected, unless it is looping
421 * network traffic (demand mode).
422 */
423 struct ppp *ppp = PF_TO_PPP(pf);
424 if (ppp->n_channels == 0
425 && (ppp->flags & SC_LOOP_TRAFFIC) == 0)
426 break;
427 }
428 ret = -EAGAIN;
429 if (file->f_flags & O_NONBLOCK)
430 break;
431 ret = -ERESTARTSYS;
432 if (signal_pending(current))
433 break;
434 schedule();
435 }
436 set_current_state(TASK_RUNNING);
437 remove_wait_queue(&pf->rwait, &wait);
438
439 if (skb == 0)
440 goto out;
441
442 ret = -EOVERFLOW;
443 if (skb->len > count)
444 goto outf;
445 ret = -EFAULT;
446 if (copy_to_user(buf, skb->data, skb->len))
447 goto outf;
448 ret = skb->len;
449
450 outf:
451 kfree_skb(skb);
452 out:
453 return ret;
454}
455
456static ssize_t ppp_write(struct file *file, const char __user *buf,
457 size_t count, loff_t *ppos)
458{
459 struct ppp_file *pf = file->private_data;
460 struct sk_buff *skb;
461 ssize_t ret;
462
463 if (pf == 0)
464 return -ENXIO;
465 ret = -ENOMEM;
466 skb = alloc_skb(count + pf->hdrlen, GFP_KERNEL);
467 if (skb == 0)
468 goto out;
469 skb_reserve(skb, pf->hdrlen);
470 ret = -EFAULT;
471 if (copy_from_user(skb_put(skb, count), buf, count)) {
472 kfree_skb(skb);
473 goto out;
474 }
475
476 skb_queue_tail(&pf->xq, skb);
477
478 switch (pf->kind) {
479 case INTERFACE:
480 ppp_xmit_process(PF_TO_PPP(pf));
481 break;
482 case CHANNEL:
483 ppp_channel_push(PF_TO_CHANNEL(pf));
484 break;
485 }
486
487 ret = count;
488
489 out:
490 return ret;
491}
492
493/* No kernel lock - fine */
494static unsigned int ppp_poll(struct file *file, poll_table *wait)
495{
496 struct ppp_file *pf = file->private_data;
497 unsigned int mask;
498
499 if (pf == 0)
500 return 0;
501 poll_wait(file, &pf->rwait, wait);
502 mask = POLLOUT | POLLWRNORM;
503 if (skb_peek(&pf->rq) != 0)
504 mask |= POLLIN | POLLRDNORM;
505 if (pf->dead)
506 mask |= POLLHUP;
507 else if (pf->kind == INTERFACE) {
508 /* see comment in ppp_read */
509 struct ppp *ppp = PF_TO_PPP(pf);
510 if (ppp->n_channels == 0
511 && (ppp->flags & SC_LOOP_TRAFFIC) == 0)
512 mask |= POLLIN | POLLRDNORM;
513 }
514
515 return mask;
516}
517
518#ifdef CONFIG_PPP_FILTER
519static int get_filter(void __user *arg, struct sock_filter **p)
520{
521 struct sock_fprog uprog;
522 struct sock_filter *code = NULL;
523 int len, err;
524
525 if (copy_from_user(&uprog, arg, sizeof(uprog)))
526 return -EFAULT;
527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 if (!uprog.len) {
529 *p = NULL;
530 return 0;
531 }
532
533 len = uprog.len * sizeof(struct sock_filter);
534 code = kmalloc(len, GFP_KERNEL);
535 if (code == NULL)
536 return -ENOMEM;
537
538 if (copy_from_user(code, uprog.filter, len)) {
539 kfree(code);
540 return -EFAULT;
541 }
542
543 err = sk_chk_filter(code, uprog.len);
544 if (err) {
545 kfree(code);
546 return err;
547 }
548
549 *p = code;
550 return uprog.len;
551}
552#endif /* CONFIG_PPP_FILTER */
553
554static int ppp_ioctl(struct inode *inode, struct file *file,
555 unsigned int cmd, unsigned long arg)
556{
557 struct ppp_file *pf = file->private_data;
558 struct ppp *ppp;
559 int err = -EFAULT, val, val2, i;
560 struct ppp_idle idle;
561 struct npioctl npi;
562 int unit, cflags;
563 struct slcompress *vj;
564 void __user *argp = (void __user *)arg;
565 int __user *p = argp;
566
567 if (pf == 0)
568 return ppp_unattached_ioctl(pf, file, cmd, arg);
569
570 if (cmd == PPPIOCDETACH) {
571 /*
572 * We have to be careful here... if the file descriptor
573 * has been dup'd, we could have another process in the
574 * middle of a poll using the same file *, so we had
575 * better not free the interface data structures -
576 * instead we fail the ioctl. Even in this case, we
577 * shut down the interface if we are the owner of it.
578 * Actually, we should get rid of PPPIOCDETACH, userland
579 * (i.e. pppd) could achieve the same effect by closing
580 * this fd and reopening /dev/ppp.
581 */
582 err = -EINVAL;
583 if (pf->kind == INTERFACE) {
584 ppp = PF_TO_PPP(pf);
585 if (file == ppp->owner)
586 ppp_shutdown_interface(ppp);
587 }
588 if (atomic_read(&file->f_count) <= 2) {
589 ppp_release(inode, file);
590 err = 0;
591 } else
592 printk(KERN_DEBUG "PPPIOCDETACH file->f_count=%d\n",
593 atomic_read(&file->f_count));
594 return err;
595 }
596
597 if (pf->kind == CHANNEL) {
598 struct channel *pch = PF_TO_CHANNEL(pf);
599 struct ppp_channel *chan;
600
601 switch (cmd) {
602 case PPPIOCCONNECT:
603 if (get_user(unit, p))
604 break;
605 err = ppp_connect_channel(pch, unit);
606 break;
607
608 case PPPIOCDISCONN:
609 err = ppp_disconnect_channel(pch);
610 break;
611
612 default:
613 down_read(&pch->chan_sem);
614 chan = pch->chan;
615 err = -ENOTTY;
616 if (chan && chan->ops->ioctl)
617 err = chan->ops->ioctl(chan, cmd, arg);
618 up_read(&pch->chan_sem);
619 }
620 return err;
621 }
622
623 if (pf->kind != INTERFACE) {
624 /* can't happen */
625 printk(KERN_ERR "PPP: not interface or channel??\n");
626 return -EINVAL;
627 }
628
629 ppp = PF_TO_PPP(pf);
630 switch (cmd) {
631 case PPPIOCSMRU:
632 if (get_user(val, p))
633 break;
634 ppp->mru = val;
635 err = 0;
636 break;
637
638 case PPPIOCSFLAGS:
639 if (get_user(val, p))
640 break;
641 ppp_lock(ppp);
642 cflags = ppp->flags & ~val;
643 ppp->flags = val & SC_FLAG_BITS;
644 ppp_unlock(ppp);
645 if (cflags & SC_CCP_OPEN)
646 ppp_ccp_closed(ppp);
647 err = 0;
648 break;
649
650 case PPPIOCGFLAGS:
651 val = ppp->flags | ppp->xstate | ppp->rstate;
652 if (put_user(val, p))
653 break;
654 err = 0;
655 break;
656
657 case PPPIOCSCOMPRESS:
658 err = ppp_set_compress(ppp, arg);
659 break;
660
661 case PPPIOCGUNIT:
662 if (put_user(ppp->file.index, p))
663 break;
664 err = 0;
665 break;
666
667 case PPPIOCSDEBUG:
668 if (get_user(val, p))
669 break;
670 ppp->debug = val;
671 err = 0;
672 break;
673
674 case PPPIOCGDEBUG:
675 if (put_user(ppp->debug, p))
676 break;
677 err = 0;
678 break;
679
680 case PPPIOCGIDLE:
681 idle.xmit_idle = (jiffies - ppp->last_xmit) / HZ;
682 idle.recv_idle = (jiffies - ppp->last_recv) / HZ;
683 if (copy_to_user(argp, &idle, sizeof(idle)))
684 break;
685 err = 0;
686 break;
687
688 case PPPIOCSMAXCID:
689 if (get_user(val, p))
690 break;
691 val2 = 15;
692 if ((val >> 16) != 0) {
693 val2 = val >> 16;
694 val &= 0xffff;
695 }
696 vj = slhc_init(val2+1, val+1);
697 if (vj == 0) {
698 printk(KERN_ERR "PPP: no memory (VJ compressor)\n");
699 err = -ENOMEM;
700 break;
701 }
702 ppp_lock(ppp);
703 if (ppp->vj != 0)
704 slhc_free(ppp->vj);
705 ppp->vj = vj;
706 ppp_unlock(ppp);
707 err = 0;
708 break;
709
710 case PPPIOCGNPMODE:
711 case PPPIOCSNPMODE:
712 if (copy_from_user(&npi, argp, sizeof(npi)))
713 break;
714 err = proto_to_npindex(npi.protocol);
715 if (err < 0)
716 break;
717 i = err;
718 if (cmd == PPPIOCGNPMODE) {
719 err = -EFAULT;
720 npi.mode = ppp->npmode[i];
721 if (copy_to_user(argp, &npi, sizeof(npi)))
722 break;
723 } else {
724 ppp->npmode[i] = npi.mode;
725 /* we may be able to transmit more packets now (??) */
726 netif_wake_queue(ppp->dev);
727 }
728 err = 0;
729 break;
730
731#ifdef CONFIG_PPP_FILTER
732 case PPPIOCSPASS:
733 {
734 struct sock_filter *code;
735 err = get_filter(argp, &code);
736 if (err >= 0) {
737 ppp_lock(ppp);
738 kfree(ppp->pass_filter);
739 ppp->pass_filter = code;
740 ppp->pass_len = err;
741 ppp_unlock(ppp);
742 err = 0;
743 }
744 break;
745 }
746 case PPPIOCSACTIVE:
747 {
748 struct sock_filter *code;
749 err = get_filter(argp, &code);
750 if (err >= 0) {
751 ppp_lock(ppp);
752 kfree(ppp->active_filter);
753 ppp->active_filter = code;
754 ppp->active_len = err;
755 ppp_unlock(ppp);
756 err = 0;
757 }
758 break;
759 }
760#endif /* CONFIG_PPP_FILTER */
761
762#ifdef CONFIG_PPP_MULTILINK
763 case PPPIOCSMRRU:
764 if (get_user(val, p))
765 break;
766 ppp_recv_lock(ppp);
767 ppp->mrru = val;
768 ppp_recv_unlock(ppp);
769 err = 0;
770 break;
771#endif /* CONFIG_PPP_MULTILINK */
772
773 default:
774 err = -ENOTTY;
775 }
776
777 return err;
778}
779
780static int ppp_unattached_ioctl(struct ppp_file *pf, struct file *file,
781 unsigned int cmd, unsigned long arg)
782{
783 int unit, err = -EFAULT;
784 struct ppp *ppp;
785 struct channel *chan;
786 int __user *p = (int __user *)arg;
787
788 switch (cmd) {
789 case PPPIOCNEWUNIT:
790 /* Create a new ppp unit */
791 if (get_user(unit, p))
792 break;
793 ppp = ppp_create_interface(unit, &err);
794 if (ppp == 0)
795 break;
796 file->private_data = &ppp->file;
797 ppp->owner = file;
798 err = -EFAULT;
799 if (put_user(ppp->file.index, p))
800 break;
801 err = 0;
802 break;
803
804 case PPPIOCATTACH:
805 /* Attach to an existing ppp unit */
806 if (get_user(unit, p))
807 break;
Arjan van de Ven8ed965d2006-03-23 03:00:21 -0800808 mutex_lock(&all_ppp_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 err = -ENXIO;
810 ppp = ppp_find_unit(unit);
811 if (ppp != 0) {
812 atomic_inc(&ppp->file.refcnt);
813 file->private_data = &ppp->file;
814 err = 0;
815 }
Arjan van de Ven8ed965d2006-03-23 03:00:21 -0800816 mutex_unlock(&all_ppp_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 break;
818
819 case PPPIOCATTCHAN:
820 if (get_user(unit, p))
821 break;
822 spin_lock_bh(&all_channels_lock);
823 err = -ENXIO;
824 chan = ppp_find_channel(unit);
825 if (chan != 0) {
826 atomic_inc(&chan->file.refcnt);
827 file->private_data = &chan->file;
828 err = 0;
829 }
830 spin_unlock_bh(&all_channels_lock);
831 break;
832
833 default:
834 err = -ENOTTY;
835 }
836 return err;
837}
838
839static struct file_operations ppp_device_fops = {
840 .owner = THIS_MODULE,
841 .read = ppp_read,
842 .write = ppp_write,
843 .poll = ppp_poll,
844 .ioctl = ppp_ioctl,
845 .open = ppp_open,
846 .release = ppp_release
847};
848
849#define PPP_MAJOR 108
850
851/* Called at boot time if ppp is compiled into the kernel,
852 or at module load time (from init_module) if compiled as a module. */
853static int __init ppp_init(void)
854{
855 int err;
856
857 printk(KERN_INFO "PPP generic driver version " PPP_VERSION "\n");
858 err = register_chrdev(PPP_MAJOR, "ppp", &ppp_device_fops);
859 if (!err) {
gregkh@suse.de56b22932005-03-23 10:01:41 -0800860 ppp_class = class_create(THIS_MODULE, "ppp");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 if (IS_ERR(ppp_class)) {
862 err = PTR_ERR(ppp_class);
863 goto out_chrdev;
864 }
Greg Kroah-Hartman53f46542005-10-27 22:25:43 -0700865 class_device_create(ppp_class, NULL, MKDEV(PPP_MAJOR, 0), NULL, "ppp");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 }
867
868out:
869 if (err)
870 printk(KERN_ERR "failed to register PPP device (%d)\n", err);
871 return err;
872
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873out_chrdev:
874 unregister_chrdev(PPP_MAJOR, "ppp");
875 goto out;
876}
877
878/*
879 * Network interface unit routines.
880 */
881static int
882ppp_start_xmit(struct sk_buff *skb, struct net_device *dev)
883{
884 struct ppp *ppp = (struct ppp *) dev->priv;
885 int npi, proto;
886 unsigned char *pp;
887
888 npi = ethertype_to_npindex(ntohs(skb->protocol));
889 if (npi < 0)
890 goto outf;
891
892 /* Drop, accept or reject the packet */
893 switch (ppp->npmode[npi]) {
894 case NPMODE_PASS:
895 break;
896 case NPMODE_QUEUE:
897 /* it would be nice to have a way to tell the network
898 system to queue this one up for later. */
899 goto outf;
900 case NPMODE_DROP:
901 case NPMODE_ERROR:
902 goto outf;
903 }
904
905 /* Put the 2-byte PPP protocol number on the front,
906 making sure there is room for the address and control fields. */
907 if (skb_headroom(skb) < PPP_HDRLEN) {
908 struct sk_buff *ns;
909
910 ns = alloc_skb(skb->len + dev->hard_header_len, GFP_ATOMIC);
911 if (ns == 0)
912 goto outf;
913 skb_reserve(ns, dev->hard_header_len);
914 skb_copy_bits(skb, 0, skb_put(ns, skb->len), skb->len);
915 kfree_skb(skb);
916 skb = ns;
917 }
918 pp = skb_push(skb, 2);
919 proto = npindex_to_proto[npi];
920 pp[0] = proto >> 8;
921 pp[1] = proto;
922
923 netif_stop_queue(dev);
924 skb_queue_tail(&ppp->file.xq, skb);
925 ppp_xmit_process(ppp);
926 return 0;
927
928 outf:
929 kfree_skb(skb);
930 ++ppp->stats.tx_dropped;
931 return 0;
932}
933
934static struct net_device_stats *
935ppp_net_stats(struct net_device *dev)
936{
937 struct ppp *ppp = (struct ppp *) dev->priv;
938
939 return &ppp->stats;
940}
941
942static int
943ppp_net_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
944{
945 struct ppp *ppp = dev->priv;
946 int err = -EFAULT;
947 void __user *addr = (void __user *) ifr->ifr_ifru.ifru_data;
948 struct ppp_stats stats;
949 struct ppp_comp_stats cstats;
950 char *vers;
951
952 switch (cmd) {
953 case SIOCGPPPSTATS:
954 ppp_get_stats(ppp, &stats);
955 if (copy_to_user(addr, &stats, sizeof(stats)))
956 break;
957 err = 0;
958 break;
959
960 case SIOCGPPPCSTATS:
961 memset(&cstats, 0, sizeof(cstats));
962 if (ppp->xc_state != 0)
963 ppp->xcomp->comp_stat(ppp->xc_state, &cstats.c);
964 if (ppp->rc_state != 0)
965 ppp->rcomp->decomp_stat(ppp->rc_state, &cstats.d);
966 if (copy_to_user(addr, &cstats, sizeof(cstats)))
967 break;
968 err = 0;
969 break;
970
971 case SIOCGPPPVER:
972 vers = PPP_VERSION;
973 if (copy_to_user(addr, vers, strlen(vers) + 1))
974 break;
975 err = 0;
976 break;
977
978 default:
979 err = -EINVAL;
980 }
981
982 return err;
983}
984
985static void ppp_setup(struct net_device *dev)
986{
987 dev->hard_header_len = PPP_HDRLEN;
988 dev->mtu = PPP_MTU;
989 dev->addr_len = 0;
990 dev->tx_queue_len = 3;
991 dev->type = ARPHRD_PPP;
992 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
993}
994
995/*
996 * Transmit-side routines.
997 */
998
999/*
1000 * Called to do any work queued up on the transmit side
1001 * that can now be done.
1002 */
1003static void
1004ppp_xmit_process(struct ppp *ppp)
1005{
1006 struct sk_buff *skb;
1007
1008 ppp_xmit_lock(ppp);
1009 if (ppp->dev != 0) {
1010 ppp_push(ppp);
1011 while (ppp->xmit_pending == 0
1012 && (skb = skb_dequeue(&ppp->file.xq)) != 0)
1013 ppp_send_frame(ppp, skb);
1014 /* If there's no work left to do, tell the core net
1015 code that we can accept some more. */
1016 if (ppp->xmit_pending == 0 && skb_peek(&ppp->file.xq) == 0)
1017 netif_wake_queue(ppp->dev);
1018 }
1019 ppp_xmit_unlock(ppp);
1020}
1021
Matt Domschb3f9b922005-11-08 09:40:47 -08001022static inline struct sk_buff *
1023pad_compress_skb(struct ppp *ppp, struct sk_buff *skb)
1024{
1025 struct sk_buff *new_skb;
1026 int len;
1027 int new_skb_size = ppp->dev->mtu +
1028 ppp->xcomp->comp_extra + ppp->dev->hard_header_len;
1029 int compressor_skb_size = ppp->dev->mtu +
1030 ppp->xcomp->comp_extra + PPP_HDRLEN;
1031 new_skb = alloc_skb(new_skb_size, GFP_ATOMIC);
1032 if (!new_skb) {
1033 if (net_ratelimit())
1034 printk(KERN_ERR "PPP: no memory (comp pkt)\n");
1035 return NULL;
1036 }
1037 if (ppp->dev->hard_header_len > PPP_HDRLEN)
1038 skb_reserve(new_skb,
1039 ppp->dev->hard_header_len - PPP_HDRLEN);
1040
1041 /* compressor still expects A/C bytes in hdr */
1042 len = ppp->xcomp->compress(ppp->xc_state, skb->data - 2,
1043 new_skb->data, skb->len + 2,
1044 compressor_skb_size);
1045 if (len > 0 && (ppp->flags & SC_CCP_UP)) {
1046 kfree_skb(skb);
1047 skb = new_skb;
1048 skb_put(skb, len);
1049 skb_pull(skb, 2); /* pull off A/C bytes */
1050 } else if (len == 0) {
1051 /* didn't compress, or CCP not up yet */
1052 kfree_skb(new_skb);
1053 new_skb = skb;
1054 } else {
1055 /*
1056 * (len < 0)
1057 * MPPE requires that we do not send unencrypted
1058 * frames. The compressor will return -1 if we
1059 * should drop the frame. We cannot simply test
1060 * the compress_proto because MPPE and MPPC share
1061 * the same number.
1062 */
1063 if (net_ratelimit())
1064 printk(KERN_ERR "ppp: compressor dropped pkt\n");
1065 kfree_skb(skb);
1066 kfree_skb(new_skb);
1067 new_skb = NULL;
1068 }
1069 return new_skb;
1070}
1071
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072/*
1073 * Compress and send a frame.
1074 * The caller should have locked the xmit path,
1075 * and xmit_pending should be 0.
1076 */
1077static void
1078ppp_send_frame(struct ppp *ppp, struct sk_buff *skb)
1079{
1080 int proto = PPP_PROTO(skb);
1081 struct sk_buff *new_skb;
1082 int len;
1083 unsigned char *cp;
1084
1085 if (proto < 0x8000) {
1086#ifdef CONFIG_PPP_FILTER
1087 /* check if we should pass this packet */
1088 /* the filter instructions are constructed assuming
1089 a four-byte PPP header on each packet */
1090 *skb_push(skb, 2) = 1;
1091 if (ppp->pass_filter
1092 && sk_run_filter(skb, ppp->pass_filter,
1093 ppp->pass_len) == 0) {
1094 if (ppp->debug & 1)
1095 printk(KERN_DEBUG "PPP: outbound frame not passed\n");
1096 kfree_skb(skb);
1097 return;
1098 }
1099 /* if this packet passes the active filter, record the time */
1100 if (!(ppp->active_filter
1101 && sk_run_filter(skb, ppp->active_filter,
1102 ppp->active_len) == 0))
1103 ppp->last_xmit = jiffies;
1104 skb_pull(skb, 2);
1105#else
1106 /* for data packets, record the time */
1107 ppp->last_xmit = jiffies;
1108#endif /* CONFIG_PPP_FILTER */
1109 }
1110
1111 ++ppp->stats.tx_packets;
1112 ppp->stats.tx_bytes += skb->len - 2;
1113
1114 switch (proto) {
1115 case PPP_IP:
1116 if (ppp->vj == 0 || (ppp->flags & SC_COMP_TCP) == 0)
1117 break;
1118 /* try to do VJ TCP header compression */
1119 new_skb = alloc_skb(skb->len + ppp->dev->hard_header_len - 2,
1120 GFP_ATOMIC);
1121 if (new_skb == 0) {
1122 printk(KERN_ERR "PPP: no memory (VJ comp pkt)\n");
1123 goto drop;
1124 }
1125 skb_reserve(new_skb, ppp->dev->hard_header_len - 2);
1126 cp = skb->data + 2;
1127 len = slhc_compress(ppp->vj, cp, skb->len - 2,
1128 new_skb->data + 2, &cp,
1129 !(ppp->flags & SC_NO_TCP_CCID));
1130 if (cp == skb->data + 2) {
1131 /* didn't compress */
1132 kfree_skb(new_skb);
1133 } else {
1134 if (cp[0] & SL_TYPE_COMPRESSED_TCP) {
1135 proto = PPP_VJC_COMP;
1136 cp[0] &= ~SL_TYPE_COMPRESSED_TCP;
1137 } else {
1138 proto = PPP_VJC_UNCOMP;
1139 cp[0] = skb->data[2];
1140 }
1141 kfree_skb(skb);
1142 skb = new_skb;
1143 cp = skb_put(skb, len + 2);
1144 cp[0] = 0;
1145 cp[1] = proto;
1146 }
1147 break;
1148
1149 case PPP_CCP:
1150 /* peek at outbound CCP frames */
1151 ppp_ccp_peek(ppp, skb, 0);
1152 break;
1153 }
1154
1155 /* try to do packet compression */
1156 if ((ppp->xstate & SC_COMP_RUN) && ppp->xc_state != 0
1157 && proto != PPP_LCP && proto != PPP_CCP) {
Matt Domschb3f9b922005-11-08 09:40:47 -08001158 if (!(ppp->flags & SC_CCP_UP) && (ppp->flags & SC_MUST_COMP)) {
1159 if (net_ratelimit())
1160 printk(KERN_ERR "ppp: compression required but down - pkt dropped.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 goto drop;
1162 }
Matt Domschb3f9b922005-11-08 09:40:47 -08001163 skb = pad_compress_skb(ppp, skb);
1164 if (!skb)
1165 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 }
1167
1168 /*
1169 * If we are waiting for traffic (demand dialling),
1170 * queue it up for pppd to receive.
1171 */
1172 if (ppp->flags & SC_LOOP_TRAFFIC) {
1173 if (ppp->file.rq.qlen > PPP_MAX_RQLEN)
1174 goto drop;
1175 skb_queue_tail(&ppp->file.rq, skb);
1176 wake_up_interruptible(&ppp->file.rwait);
1177 return;
1178 }
1179
1180 ppp->xmit_pending = skb;
1181 ppp_push(ppp);
1182 return;
1183
1184 drop:
Matt Domschb3f9b922005-11-08 09:40:47 -08001185 if (skb)
1186 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 ++ppp->stats.tx_errors;
1188}
1189
1190/*
1191 * Try to send the frame in xmit_pending.
1192 * The caller should have the xmit path locked.
1193 */
1194static void
1195ppp_push(struct ppp *ppp)
1196{
1197 struct list_head *list;
1198 struct channel *pch;
1199 struct sk_buff *skb = ppp->xmit_pending;
1200
1201 if (skb == 0)
1202 return;
1203
1204 list = &ppp->channels;
1205 if (list_empty(list)) {
1206 /* nowhere to send the packet, just drop it */
1207 ppp->xmit_pending = NULL;
1208 kfree_skb(skb);
1209 return;
1210 }
1211
1212 if ((ppp->flags & SC_MULTILINK) == 0) {
1213 /* not doing multilink: send it down the first channel */
1214 list = list->next;
1215 pch = list_entry(list, struct channel, clist);
1216
1217 spin_lock_bh(&pch->downl);
1218 if (pch->chan) {
1219 if (pch->chan->ops->start_xmit(pch->chan, skb))
1220 ppp->xmit_pending = NULL;
1221 } else {
1222 /* channel got unregistered */
1223 kfree_skb(skb);
1224 ppp->xmit_pending = NULL;
1225 }
1226 spin_unlock_bh(&pch->downl);
1227 return;
1228 }
1229
1230#ifdef CONFIG_PPP_MULTILINK
1231 /* Multilink: fragment the packet over as many links
1232 as can take the packet at the moment. */
1233 if (!ppp_mp_explode(ppp, skb))
1234 return;
1235#endif /* CONFIG_PPP_MULTILINK */
1236
1237 ppp->xmit_pending = NULL;
1238 kfree_skb(skb);
1239}
1240
1241#ifdef CONFIG_PPP_MULTILINK
1242/*
1243 * Divide a packet to be transmitted into fragments and
1244 * send them out the individual links.
1245 */
1246static int ppp_mp_explode(struct ppp *ppp, struct sk_buff *skb)
1247{
Paul Mackerras516cd152005-05-12 19:47:12 -04001248 int len, fragsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 int i, bits, hdrlen, mtu;
Paul Mackerras516cd152005-05-12 19:47:12 -04001250 int flen;
1251 int navail, nfree;
1252 int nbigger;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 unsigned char *p, *q;
1254 struct list_head *list;
1255 struct channel *pch;
1256 struct sk_buff *frag;
1257 struct ppp_channel *chan;
1258
Paul Mackerras516cd152005-05-12 19:47:12 -04001259 nfree = 0; /* # channels which have no packet already queued */
1260 navail = 0; /* total # of usable channels (not deregistered) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 hdrlen = (ppp->flags & SC_MP_XSHORTSEQ)? MPHDRLEN_SSN: MPHDRLEN;
Paul Mackerras516cd152005-05-12 19:47:12 -04001262 i = 0;
Domen Puncer81616c52005-09-10 00:27:04 -07001263 list_for_each_entry(pch, &ppp->channels, clist) {
Paul Mackerras516cd152005-05-12 19:47:12 -04001264 navail += pch->avail = (pch->chan != NULL);
1265 if (pch->avail) {
David S. Millerb03efcf2005-07-08 14:57:23 -07001266 if (skb_queue_empty(&pch->file.xq) ||
1267 !pch->had_frag) {
Paul Mackerras516cd152005-05-12 19:47:12 -04001268 pch->avail = 2;
1269 ++nfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 }
Paul Mackerras516cd152005-05-12 19:47:12 -04001271 if (!pch->had_frag && i < ppp->nxchan)
1272 ppp->nxchan = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 }
Paul Mackerras516cd152005-05-12 19:47:12 -04001274 ++i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 }
Paul Mackerras516cd152005-05-12 19:47:12 -04001276
1277 /*
1278 * Don't start sending this packet unless at least half of
1279 * the channels are free. This gives much better TCP
1280 * performance if we have a lot of channels.
1281 */
1282 if (nfree == 0 || nfree < navail / 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 return 0; /* can't take now, leave it in xmit_pending */
1284
1285 /* Do protocol field compression (XXX this should be optional) */
1286 p = skb->data;
1287 len = skb->len;
1288 if (*p == 0) {
1289 ++p;
1290 --len;
1291 }
1292
Paul Mackerras516cd152005-05-12 19:47:12 -04001293 /*
1294 * Decide on fragment size.
1295 * We create a fragment for each free channel regardless of
1296 * how small they are (i.e. even 0 length) in order to minimize
1297 * the time that it will take to detect when a channel drops
1298 * a fragment.
1299 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 fragsize = len;
Paul Mackerras516cd152005-05-12 19:47:12 -04001301 if (nfree > 1)
1302 fragsize = ROUNDUP(fragsize, nfree);
1303 /* nbigger channels get fragsize bytes, the rest get fragsize-1,
1304 except if nbigger==0, then they all get fragsize. */
1305 nbigger = len % nfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
1307 /* skip to the channel after the one we last used
1308 and start at that one */
Domen Puncer81616c52005-09-10 00:27:04 -07001309 list = &ppp->channels;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 for (i = 0; i < ppp->nxchan; ++i) {
1311 list = list->next;
1312 if (list == &ppp->channels) {
1313 i = 0;
1314 break;
1315 }
1316 }
1317
1318 /* create a fragment for each channel */
1319 bits = B;
Paul Mackerras516cd152005-05-12 19:47:12 -04001320 while (nfree > 0 || len > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 list = list->next;
1322 if (list == &ppp->channels) {
1323 i = 0;
1324 continue;
1325 }
1326 pch = list_entry(list, struct channel, clist);
1327 ++i;
1328 if (!pch->avail)
1329 continue;
1330
Paul Mackerras516cd152005-05-12 19:47:12 -04001331 /*
1332 * Skip this channel if it has a fragment pending already and
1333 * we haven't given a fragment to all of the free channels.
1334 */
1335 if (pch->avail == 1) {
1336 if (nfree > 0)
1337 continue;
1338 } else {
1339 --nfree;
1340 pch->avail = 1;
1341 }
1342
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 /* check the channel's mtu and whether it is still attached. */
1344 spin_lock_bh(&pch->downl);
Paul Mackerras516cd152005-05-12 19:47:12 -04001345 if (pch->chan == NULL) {
1346 /* can't use this channel, it's being deregistered */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 spin_unlock_bh(&pch->downl);
1348 pch->avail = 0;
Paul Mackerras516cd152005-05-12 19:47:12 -04001349 if (--navail == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 break;
1351 continue;
1352 }
1353
1354 /*
Paul Mackerras516cd152005-05-12 19:47:12 -04001355 * Create a fragment for this channel of
1356 * min(max(mtu+2-hdrlen, 4), fragsize, len) bytes.
1357 * If mtu+2-hdrlen < 4, that is a ridiculously small
1358 * MTU, so we use mtu = 2 + hdrlen.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 */
1360 if (fragsize > len)
1361 fragsize = len;
Paul Mackerras516cd152005-05-12 19:47:12 -04001362 flen = fragsize;
1363 mtu = pch->chan->mtu + 2 - hdrlen;
1364 if (mtu < 4)
1365 mtu = 4;
1366 if (flen > mtu)
1367 flen = mtu;
1368 if (flen == len && nfree == 0)
1369 bits |= E;
1370 frag = alloc_skb(flen + hdrlen + (flen == 0), GFP_ATOMIC);
1371 if (frag == 0)
1372 goto noskb;
1373 q = skb_put(frag, flen + hdrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
Paul Mackerras516cd152005-05-12 19:47:12 -04001375 /* make the MP header */
1376 q[0] = PPP_MP >> 8;
1377 q[1] = PPP_MP;
1378 if (ppp->flags & SC_MP_XSHORTSEQ) {
1379 q[2] = bits + ((ppp->nxseq >> 8) & 0xf);
1380 q[3] = ppp->nxseq;
1381 } else {
1382 q[2] = bits;
1383 q[3] = ppp->nxseq >> 16;
1384 q[4] = ppp->nxseq >> 8;
1385 q[5] = ppp->nxseq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 }
Paul Mackerras516cd152005-05-12 19:47:12 -04001387
1388 /*
1389 * Copy the data in.
1390 * Unfortunately there is a bug in older versions of
1391 * the Linux PPP multilink reconstruction code where it
1392 * drops 0-length fragments. Therefore we make sure the
1393 * fragment has at least one byte of data. Any bytes
1394 * we add in this situation will end up as padding on the
1395 * end of the reconstructed packet.
1396 */
1397 if (flen == 0)
1398 *skb_put(frag, 1) = 0;
1399 else
1400 memcpy(q + hdrlen, p, flen);
1401
1402 /* try to send it down the channel */
1403 chan = pch->chan;
David S. Millerb03efcf2005-07-08 14:57:23 -07001404 if (!skb_queue_empty(&pch->file.xq) ||
1405 !chan->ops->start_xmit(chan, frag))
Paul Mackerras516cd152005-05-12 19:47:12 -04001406 skb_queue_tail(&pch->file.xq, frag);
1407 pch->had_frag = 1;
1408 p += flen;
1409 len -= flen;
1410 ++ppp->nxseq;
1411 bits = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 spin_unlock_bh(&pch->downl);
Paul Mackerras516cd152005-05-12 19:47:12 -04001413
1414 if (--nbigger == 0 && fragsize > 0)
1415 --fragsize;
1416 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 ppp->nxchan = i;
1418
1419 return 1;
1420
1421 noskb:
1422 spin_unlock_bh(&pch->downl);
1423 if (ppp->debug & 1)
1424 printk(KERN_ERR "PPP: no memory (fragment)\n");
1425 ++ppp->stats.tx_errors;
1426 ++ppp->nxseq;
1427 return 1; /* abandon the frame */
1428}
1429#endif /* CONFIG_PPP_MULTILINK */
1430
1431/*
1432 * Try to send data out on a channel.
1433 */
1434static void
1435ppp_channel_push(struct channel *pch)
1436{
1437 struct sk_buff *skb;
1438 struct ppp *ppp;
1439
1440 spin_lock_bh(&pch->downl);
1441 if (pch->chan != 0) {
David S. Millerb03efcf2005-07-08 14:57:23 -07001442 while (!skb_queue_empty(&pch->file.xq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 skb = skb_dequeue(&pch->file.xq);
1444 if (!pch->chan->ops->start_xmit(pch->chan, skb)) {
1445 /* put the packet back and try again later */
1446 skb_queue_head(&pch->file.xq, skb);
1447 break;
1448 }
1449 }
1450 } else {
1451 /* channel got deregistered */
1452 skb_queue_purge(&pch->file.xq);
1453 }
1454 spin_unlock_bh(&pch->downl);
1455 /* see if there is anything from the attached unit to be sent */
David S. Millerb03efcf2005-07-08 14:57:23 -07001456 if (skb_queue_empty(&pch->file.xq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 read_lock_bh(&pch->upl);
1458 ppp = pch->ppp;
1459 if (ppp != 0)
1460 ppp_xmit_process(ppp);
1461 read_unlock_bh(&pch->upl);
1462 }
1463}
1464
1465/*
1466 * Receive-side routines.
1467 */
1468
1469/* misuse a few fields of the skb for MP reconstruction */
1470#define sequence priority
1471#define BEbits cb[0]
1472
1473static inline void
1474ppp_do_recv(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
1475{
1476 ppp_recv_lock(ppp);
1477 /* ppp->dev == 0 means interface is closing down */
1478 if (ppp->dev != 0)
1479 ppp_receive_frame(ppp, skb, pch);
1480 else
1481 kfree_skb(skb);
1482 ppp_recv_unlock(ppp);
1483}
1484
1485void
1486ppp_input(struct ppp_channel *chan, struct sk_buff *skb)
1487{
1488 struct channel *pch = chan->ppp;
1489 int proto;
1490
1491 if (pch == 0 || skb->len == 0) {
1492 kfree_skb(skb);
1493 return;
1494 }
Paul Mackerras516cd152005-05-12 19:47:12 -04001495
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 proto = PPP_PROTO(skb);
1497 read_lock_bh(&pch->upl);
1498 if (pch->ppp == 0 || proto >= 0xc000 || proto == PPP_CCPFRAG) {
1499 /* put it on the channel queue */
1500 skb_queue_tail(&pch->file.rq, skb);
1501 /* drop old frames if queue too long */
1502 while (pch->file.rq.qlen > PPP_MAX_RQLEN
1503 && (skb = skb_dequeue(&pch->file.rq)) != 0)
1504 kfree_skb(skb);
1505 wake_up_interruptible(&pch->file.rwait);
1506 } else {
1507 ppp_do_recv(pch->ppp, skb, pch);
1508 }
1509 read_unlock_bh(&pch->upl);
1510}
1511
1512/* Put a 0-length skb in the receive queue as an error indication */
1513void
1514ppp_input_error(struct ppp_channel *chan, int code)
1515{
1516 struct channel *pch = chan->ppp;
1517 struct sk_buff *skb;
1518
1519 if (pch == 0)
1520 return;
1521
1522 read_lock_bh(&pch->upl);
1523 if (pch->ppp != 0) {
1524 skb = alloc_skb(0, GFP_ATOMIC);
1525 if (skb != 0) {
1526 skb->len = 0; /* probably unnecessary */
1527 skb->cb[0] = code;
1528 ppp_do_recv(pch->ppp, skb, pch);
1529 }
1530 }
1531 read_unlock_bh(&pch->upl);
1532}
1533
1534/*
1535 * We come in here to process a received frame.
1536 * The receive side of the ppp unit is locked.
1537 */
1538static void
1539ppp_receive_frame(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
1540{
1541 if (skb->len >= 2) {
1542#ifdef CONFIG_PPP_MULTILINK
1543 /* XXX do channel-level decompression here */
1544 if (PPP_PROTO(skb) == PPP_MP)
1545 ppp_receive_mp_frame(ppp, skb, pch);
1546 else
1547#endif /* CONFIG_PPP_MULTILINK */
1548 ppp_receive_nonmp_frame(ppp, skb);
1549 return;
1550 }
1551
1552 if (skb->len > 0)
1553 /* note: a 0-length skb is used as an error indication */
1554 ++ppp->stats.rx_length_errors;
1555
1556 kfree_skb(skb);
1557 ppp_receive_error(ppp);
1558}
1559
1560static void
1561ppp_receive_error(struct ppp *ppp)
1562{
1563 ++ppp->stats.rx_errors;
1564 if (ppp->vj != 0)
1565 slhc_toss(ppp->vj);
1566}
1567
1568static void
1569ppp_receive_nonmp_frame(struct ppp *ppp, struct sk_buff *skb)
1570{
1571 struct sk_buff *ns;
1572 int proto, len, npi;
1573
1574 /*
1575 * Decompress the frame, if compressed.
1576 * Note that some decompressors need to see uncompressed frames
1577 * that come in as well as compressed frames.
1578 */
1579 if (ppp->rc_state != 0 && (ppp->rstate & SC_DECOMP_RUN)
1580 && (ppp->rstate & (SC_DC_FERROR | SC_DC_ERROR)) == 0)
1581 skb = ppp_decompress_frame(ppp, skb);
1582
Matt Domschb3f9b922005-11-08 09:40:47 -08001583 if (ppp->flags & SC_MUST_COMP && ppp->rstate & SC_DC_FERROR)
1584 goto err;
1585
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 proto = PPP_PROTO(skb);
1587 switch (proto) {
1588 case PPP_VJC_COMP:
1589 /* decompress VJ compressed packets */
1590 if (ppp->vj == 0 || (ppp->flags & SC_REJ_COMP_TCP))
1591 goto err;
1592
1593 if (skb_tailroom(skb) < 124) {
1594 /* copy to a new sk_buff with more tailroom */
1595 ns = dev_alloc_skb(skb->len + 128);
1596 if (ns == 0) {
1597 printk(KERN_ERR"PPP: no memory (VJ decomp)\n");
1598 goto err;
1599 }
1600 skb_reserve(ns, 2);
1601 skb_copy_bits(skb, 0, skb_put(ns, skb->len), skb->len);
1602 kfree_skb(skb);
1603 skb = ns;
1604 }
Herbert Xue3f749c2006-02-05 20:23:33 -08001605 else
1606 skb->ip_summed = CHECKSUM_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607
1608 len = slhc_uncompress(ppp->vj, skb->data + 2, skb->len - 2);
1609 if (len <= 0) {
1610 printk(KERN_DEBUG "PPP: VJ decompression error\n");
1611 goto err;
1612 }
1613 len += 2;
1614 if (len > skb->len)
1615 skb_put(skb, len - skb->len);
1616 else if (len < skb->len)
1617 skb_trim(skb, len);
1618 proto = PPP_IP;
1619 break;
1620
1621 case PPP_VJC_UNCOMP:
1622 if (ppp->vj == 0 || (ppp->flags & SC_REJ_COMP_TCP))
1623 goto err;
1624
1625 /* Until we fix the decompressor need to make sure
1626 * data portion is linear.
1627 */
1628 if (!pskb_may_pull(skb, skb->len))
1629 goto err;
1630
1631 if (slhc_remember(ppp->vj, skb->data + 2, skb->len - 2) <= 0) {
1632 printk(KERN_ERR "PPP: VJ uncompressed error\n");
1633 goto err;
1634 }
1635 proto = PPP_IP;
1636 break;
1637
1638 case PPP_CCP:
1639 ppp_ccp_peek(ppp, skb, 1);
1640 break;
1641 }
1642
1643 ++ppp->stats.rx_packets;
1644 ppp->stats.rx_bytes += skb->len - 2;
1645
1646 npi = proto_to_npindex(proto);
1647 if (npi < 0) {
1648 /* control or unknown frame - pass it to pppd */
1649 skb_queue_tail(&ppp->file.rq, skb);
1650 /* limit queue length by dropping old frames */
1651 while (ppp->file.rq.qlen > PPP_MAX_RQLEN
1652 && (skb = skb_dequeue(&ppp->file.rq)) != 0)
1653 kfree_skb(skb);
1654 /* wake up any process polling or blocking on read */
1655 wake_up_interruptible(&ppp->file.rwait);
1656
1657 } else {
1658 /* network protocol frame - give it to the kernel */
1659
1660#ifdef CONFIG_PPP_FILTER
1661 /* check if the packet passes the pass and active filters */
1662 /* the filter instructions are constructed assuming
1663 a four-byte PPP header on each packet */
1664 *skb_push(skb, 2) = 0;
1665 if (ppp->pass_filter
1666 && sk_run_filter(skb, ppp->pass_filter,
1667 ppp->pass_len) == 0) {
1668 if (ppp->debug & 1)
1669 printk(KERN_DEBUG "PPP: inbound frame not passed\n");
1670 kfree_skb(skb);
1671 return;
1672 }
1673 if (!(ppp->active_filter
1674 && sk_run_filter(skb, ppp->active_filter,
1675 ppp->active_len) == 0))
1676 ppp->last_recv = jiffies;
1677 skb_pull(skb, 2);
1678#else
1679 ppp->last_recv = jiffies;
1680#endif /* CONFIG_PPP_FILTER */
1681
1682 if ((ppp->dev->flags & IFF_UP) == 0
1683 || ppp->npmode[npi] != NPMODE_PASS) {
1684 kfree_skb(skb);
1685 } else {
Herbert Xucbb042f2006-03-20 22:43:56 -08001686 /* chop off protocol */
1687 skb_pull_rcsum(skb, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 skb->dev = ppp->dev;
1689 skb->protocol = htons(npindex_to_ethertype[npi]);
1690 skb->mac.raw = skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 netif_rx(skb);
1692 ppp->dev->last_rx = jiffies;
1693 }
1694 }
1695 return;
1696
1697 err:
1698 kfree_skb(skb);
1699 ppp_receive_error(ppp);
1700}
1701
1702static struct sk_buff *
1703ppp_decompress_frame(struct ppp *ppp, struct sk_buff *skb)
1704{
1705 int proto = PPP_PROTO(skb);
1706 struct sk_buff *ns;
1707 int len;
1708
1709 /* Until we fix all the decompressor's need to make sure
1710 * data portion is linear.
1711 */
1712 if (!pskb_may_pull(skb, skb->len))
1713 goto err;
1714
1715 if (proto == PPP_COMP) {
1716 ns = dev_alloc_skb(ppp->mru + PPP_HDRLEN);
1717 if (ns == 0) {
1718 printk(KERN_ERR "ppp_decompress_frame: no memory\n");
1719 goto err;
1720 }
1721 /* the decompressor still expects the A/C bytes in the hdr */
1722 len = ppp->rcomp->decompress(ppp->rc_state, skb->data - 2,
1723 skb->len + 2, ns->data, ppp->mru + PPP_HDRLEN);
1724 if (len < 0) {
1725 /* Pass the compressed frame to pppd as an
1726 error indication. */
1727 if (len == DECOMP_FATALERROR)
1728 ppp->rstate |= SC_DC_FERROR;
1729 kfree_skb(ns);
1730 goto err;
1731 }
1732
1733 kfree_skb(skb);
1734 skb = ns;
1735 skb_put(skb, len);
1736 skb_pull(skb, 2); /* pull off the A/C bytes */
1737
1738 } else {
1739 /* Uncompressed frame - pass to decompressor so it
1740 can update its dictionary if necessary. */
1741 if (ppp->rcomp->incomp)
1742 ppp->rcomp->incomp(ppp->rc_state, skb->data - 2,
1743 skb->len + 2);
1744 }
1745
1746 return skb;
1747
1748 err:
1749 ppp->rstate |= SC_DC_ERROR;
1750 ppp_receive_error(ppp);
1751 return skb;
1752}
1753
1754#ifdef CONFIG_PPP_MULTILINK
1755/*
1756 * Receive a multilink frame.
1757 * We put it on the reconstruction queue and then pull off
1758 * as many completed frames as we can.
1759 */
1760static void
1761ppp_receive_mp_frame(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
1762{
1763 u32 mask, seq;
Domen Puncer81616c52005-09-10 00:27:04 -07001764 struct channel *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 int mphdrlen = (ppp->flags & SC_MP_SHORTSEQ)? MPHDRLEN_SSN: MPHDRLEN;
1766
Paul Mackerras516cd152005-05-12 19:47:12 -04001767 if (!pskb_may_pull(skb, mphdrlen) || ppp->mrru == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 goto err; /* no good, throw it away */
1769
1770 /* Decode sequence number and begin/end bits */
1771 if (ppp->flags & SC_MP_SHORTSEQ) {
1772 seq = ((skb->data[2] & 0x0f) << 8) | skb->data[3];
1773 mask = 0xfff;
1774 } else {
1775 seq = (skb->data[3] << 16) | (skb->data[4] << 8)| skb->data[5];
1776 mask = 0xffffff;
1777 }
1778 skb->BEbits = skb->data[2];
1779 skb_pull(skb, mphdrlen); /* pull off PPP and MP headers */
1780
1781 /*
1782 * Do protocol ID decompression on the first fragment of each packet.
1783 */
1784 if ((skb->BEbits & B) && (skb->data[0] & 1))
1785 *skb_push(skb, 1) = 0;
1786
1787 /*
1788 * Expand sequence number to 32 bits, making it as close
1789 * as possible to ppp->minseq.
1790 */
1791 seq |= ppp->minseq & ~mask;
1792 if ((int)(ppp->minseq - seq) > (int)(mask >> 1))
1793 seq += mask + 1;
1794 else if ((int)(seq - ppp->minseq) > (int)(mask >> 1))
1795 seq -= mask + 1; /* should never happen */
1796 skb->sequence = seq;
1797 pch->lastseq = seq;
1798
1799 /*
1800 * If this packet comes before the next one we were expecting,
1801 * drop it.
1802 */
1803 if (seq_before(seq, ppp->nextseq)) {
1804 kfree_skb(skb);
1805 ++ppp->stats.rx_dropped;
1806 ppp_receive_error(ppp);
1807 return;
1808 }
1809
1810 /*
1811 * Reevaluate minseq, the minimum over all channels of the
1812 * last sequence number received on each channel. Because of
1813 * the increasing sequence number rule, we know that any fragment
1814 * before `minseq' which hasn't arrived is never going to arrive.
1815 * The list of channels can't change because we have the receive
1816 * side of the ppp unit locked.
1817 */
Domen Puncer81616c52005-09-10 00:27:04 -07001818 list_for_each_entry(ch, &ppp->channels, clist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 if (seq_before(ch->lastseq, seq))
1820 seq = ch->lastseq;
1821 }
1822 if (seq_before(ppp->minseq, seq))
1823 ppp->minseq = seq;
1824
1825 /* Put the fragment on the reconstruction queue */
1826 ppp_mp_insert(ppp, skb);
1827
1828 /* If the queue is getting long, don't wait any longer for packets
1829 before the start of the queue. */
1830 if (skb_queue_len(&ppp->mrq) >= PPP_MP_MAX_QLEN
1831 && seq_before(ppp->minseq, ppp->mrq.next->sequence))
1832 ppp->minseq = ppp->mrq.next->sequence;
1833
1834 /* Pull completed packets off the queue and receive them. */
1835 while ((skb = ppp_mp_reconstruct(ppp)) != 0)
1836 ppp_receive_nonmp_frame(ppp, skb);
1837
1838 return;
1839
1840 err:
1841 kfree_skb(skb);
1842 ppp_receive_error(ppp);
1843}
1844
1845/*
1846 * Insert a fragment on the MP reconstruction queue.
1847 * The queue is ordered by increasing sequence number.
1848 */
1849static void
1850ppp_mp_insert(struct ppp *ppp, struct sk_buff *skb)
1851{
1852 struct sk_buff *p;
1853 struct sk_buff_head *list = &ppp->mrq;
1854 u32 seq = skb->sequence;
1855
1856 /* N.B. we don't need to lock the list lock because we have the
1857 ppp unit receive-side lock. */
1858 for (p = list->next; p != (struct sk_buff *)list; p = p->next)
1859 if (seq_before(seq, p->sequence))
1860 break;
1861 __skb_insert(skb, p->prev, p, list);
1862}
1863
1864/*
1865 * Reconstruct a packet from the MP fragment queue.
1866 * We go through increasing sequence numbers until we find a
1867 * complete packet, or we get to the sequence number for a fragment
1868 * which hasn't arrived but might still do so.
1869 */
1870struct sk_buff *
1871ppp_mp_reconstruct(struct ppp *ppp)
1872{
1873 u32 seq = ppp->nextseq;
1874 u32 minseq = ppp->minseq;
1875 struct sk_buff_head *list = &ppp->mrq;
1876 struct sk_buff *p, *next;
1877 struct sk_buff *head, *tail;
1878 struct sk_buff *skb = NULL;
1879 int lost = 0, len = 0;
1880
1881 if (ppp->mrru == 0) /* do nothing until mrru is set */
1882 return NULL;
1883 head = list->next;
1884 tail = NULL;
1885 for (p = head; p != (struct sk_buff *) list; p = next) {
1886 next = p->next;
1887 if (seq_before(p->sequence, seq)) {
1888 /* this can't happen, anyway ignore the skb */
1889 printk(KERN_ERR "ppp_mp_reconstruct bad seq %u < %u\n",
1890 p->sequence, seq);
1891 head = next;
1892 continue;
1893 }
1894 if (p->sequence != seq) {
1895 /* Fragment `seq' is missing. If it is after
1896 minseq, it might arrive later, so stop here. */
1897 if (seq_after(seq, minseq))
1898 break;
1899 /* Fragment `seq' is lost, keep going. */
1900 lost = 1;
1901 seq = seq_before(minseq, p->sequence)?
1902 minseq + 1: p->sequence;
1903 next = p;
1904 continue;
1905 }
1906
1907 /*
1908 * At this point we know that all the fragments from
1909 * ppp->nextseq to seq are either present or lost.
1910 * Also, there are no complete packets in the queue
1911 * that have no missing fragments and end before this
1912 * fragment.
1913 */
1914
1915 /* B bit set indicates this fragment starts a packet */
1916 if (p->BEbits & B) {
1917 head = p;
1918 lost = 0;
1919 len = 0;
1920 }
1921
1922 len += p->len;
1923
1924 /* Got a complete packet yet? */
1925 if (lost == 0 && (p->BEbits & E) && (head->BEbits & B)) {
1926 if (len > ppp->mrru + 2) {
1927 ++ppp->stats.rx_length_errors;
1928 printk(KERN_DEBUG "PPP: reconstructed packet"
1929 " is too long (%d)\n", len);
1930 } else if (p == head) {
1931 /* fragment is complete packet - reuse skb */
1932 tail = p;
1933 skb = skb_get(p);
1934 break;
1935 } else if ((skb = dev_alloc_skb(len)) == NULL) {
1936 ++ppp->stats.rx_missed_errors;
1937 printk(KERN_DEBUG "PPP: no memory for "
1938 "reconstructed packet");
1939 } else {
1940 tail = p;
1941 break;
1942 }
1943 ppp->nextseq = seq + 1;
1944 }
1945
1946 /*
1947 * If this is the ending fragment of a packet,
1948 * and we haven't found a complete valid packet yet,
1949 * we can discard up to and including this fragment.
1950 */
1951 if (p->BEbits & E)
1952 head = next;
1953
1954 ++seq;
1955 }
1956
1957 /* If we have a complete packet, copy it all into one skb. */
1958 if (tail != NULL) {
1959 /* If we have discarded any fragments,
1960 signal a receive error. */
1961 if (head->sequence != ppp->nextseq) {
1962 if (ppp->debug & 1)
1963 printk(KERN_DEBUG " missed pkts %u..%u\n",
1964 ppp->nextseq, head->sequence-1);
1965 ++ppp->stats.rx_dropped;
1966 ppp_receive_error(ppp);
1967 }
1968
1969 if (head != tail)
1970 /* copy to a single skb */
1971 for (p = head; p != tail->next; p = p->next)
1972 skb_copy_bits(p, 0, skb_put(skb, p->len), p->len);
1973 ppp->nextseq = tail->sequence + 1;
1974 head = tail->next;
1975 }
1976
1977 /* Discard all the skbuffs that we have copied the data out of
1978 or that we can't use. */
1979 while ((p = list->next) != head) {
1980 __skb_unlink(p, list);
1981 kfree_skb(p);
1982 }
1983
1984 return skb;
1985}
1986#endif /* CONFIG_PPP_MULTILINK */
1987
1988/*
1989 * Channel interface.
1990 */
1991
1992/*
1993 * Create a new, unattached ppp channel.
1994 */
1995int
1996ppp_register_channel(struct ppp_channel *chan)
1997{
1998 struct channel *pch;
1999
2000 pch = kmalloc(sizeof(struct channel), GFP_KERNEL);
2001 if (pch == 0)
2002 return -ENOMEM;
2003 memset(pch, 0, sizeof(struct channel));
2004 pch->ppp = NULL;
2005 pch->chan = chan;
2006 chan->ppp = pch;
2007 init_ppp_file(&pch->file, CHANNEL);
2008 pch->file.hdrlen = chan->hdrlen;
2009#ifdef CONFIG_PPP_MULTILINK
2010 pch->lastseq = -1;
2011#endif /* CONFIG_PPP_MULTILINK */
2012 init_rwsem(&pch->chan_sem);
2013 spin_lock_init(&pch->downl);
2014 rwlock_init(&pch->upl);
2015 spin_lock_bh(&all_channels_lock);
2016 pch->file.index = ++last_channel_index;
2017 list_add(&pch->list, &new_channels);
2018 atomic_inc(&channel_count);
2019 spin_unlock_bh(&all_channels_lock);
2020 return 0;
2021}
2022
2023/*
2024 * Return the index of a channel.
2025 */
2026int ppp_channel_index(struct ppp_channel *chan)
2027{
2028 struct channel *pch = chan->ppp;
2029
2030 if (pch != 0)
2031 return pch->file.index;
2032 return -1;
2033}
2034
2035/*
2036 * Return the PPP unit number to which a channel is connected.
2037 */
2038int ppp_unit_number(struct ppp_channel *chan)
2039{
2040 struct channel *pch = chan->ppp;
2041 int unit = -1;
2042
2043 if (pch != 0) {
2044 read_lock_bh(&pch->upl);
2045 if (pch->ppp != 0)
2046 unit = pch->ppp->file.index;
2047 read_unlock_bh(&pch->upl);
2048 }
2049 return unit;
2050}
2051
2052/*
2053 * Disconnect a channel from the generic layer.
2054 * This must be called in process context.
2055 */
2056void
2057ppp_unregister_channel(struct ppp_channel *chan)
2058{
2059 struct channel *pch = chan->ppp;
2060
2061 if (pch == 0)
2062 return; /* should never happen */
2063 chan->ppp = NULL;
2064
2065 /*
2066 * This ensures that we have returned from any calls into the
2067 * the channel's start_xmit or ioctl routine before we proceed.
2068 */
2069 down_write(&pch->chan_sem);
2070 spin_lock_bh(&pch->downl);
2071 pch->chan = NULL;
2072 spin_unlock_bh(&pch->downl);
2073 up_write(&pch->chan_sem);
2074 ppp_disconnect_channel(pch);
2075 spin_lock_bh(&all_channels_lock);
2076 list_del(&pch->list);
2077 spin_unlock_bh(&all_channels_lock);
2078 pch->file.dead = 1;
2079 wake_up_interruptible(&pch->file.rwait);
2080 if (atomic_dec_and_test(&pch->file.refcnt))
2081 ppp_destroy_channel(pch);
2082}
2083
2084/*
2085 * Callback from a channel when it can accept more to transmit.
2086 * This should be called at BH/softirq level, not interrupt level.
2087 */
2088void
2089ppp_output_wakeup(struct ppp_channel *chan)
2090{
2091 struct channel *pch = chan->ppp;
2092
2093 if (pch == 0)
2094 return;
2095 ppp_channel_push(pch);
2096}
2097
2098/*
2099 * Compression control.
2100 */
2101
2102/* Process the PPPIOCSCOMPRESS ioctl. */
2103static int
2104ppp_set_compress(struct ppp *ppp, unsigned long arg)
2105{
2106 int err;
2107 struct compressor *cp, *ocomp;
2108 struct ppp_option_data data;
2109 void *state, *ostate;
2110 unsigned char ccp_option[CCP_MAX_OPTION_LENGTH];
2111
2112 err = -EFAULT;
2113 if (copy_from_user(&data, (void __user *) arg, sizeof(data))
2114 || (data.length <= CCP_MAX_OPTION_LENGTH
2115 && copy_from_user(ccp_option, (void __user *) data.ptr, data.length)))
2116 goto out;
2117 err = -EINVAL;
2118 if (data.length > CCP_MAX_OPTION_LENGTH
2119 || ccp_option[1] < 2 || ccp_option[1] > data.length)
2120 goto out;
2121
2122 cp = find_compressor(ccp_option[0]);
2123#ifdef CONFIG_KMOD
2124 if (cp == 0) {
2125 request_module("ppp-compress-%d", ccp_option[0]);
2126 cp = find_compressor(ccp_option[0]);
2127 }
2128#endif /* CONFIG_KMOD */
2129 if (cp == 0)
2130 goto out;
2131
2132 err = -ENOBUFS;
2133 if (data.transmit) {
2134 state = cp->comp_alloc(ccp_option, data.length);
2135 if (state != 0) {
2136 ppp_xmit_lock(ppp);
2137 ppp->xstate &= ~SC_COMP_RUN;
2138 ocomp = ppp->xcomp;
2139 ostate = ppp->xc_state;
2140 ppp->xcomp = cp;
2141 ppp->xc_state = state;
2142 ppp_xmit_unlock(ppp);
2143 if (ostate != 0) {
2144 ocomp->comp_free(ostate);
2145 module_put(ocomp->owner);
2146 }
2147 err = 0;
2148 } else
2149 module_put(cp->owner);
2150
2151 } else {
2152 state = cp->decomp_alloc(ccp_option, data.length);
2153 if (state != 0) {
2154 ppp_recv_lock(ppp);
2155 ppp->rstate &= ~SC_DECOMP_RUN;
2156 ocomp = ppp->rcomp;
2157 ostate = ppp->rc_state;
2158 ppp->rcomp = cp;
2159 ppp->rc_state = state;
2160 ppp_recv_unlock(ppp);
2161 if (ostate != 0) {
2162 ocomp->decomp_free(ostate);
2163 module_put(ocomp->owner);
2164 }
2165 err = 0;
2166 } else
2167 module_put(cp->owner);
2168 }
2169
2170 out:
2171 return err;
2172}
2173
2174/*
2175 * Look at a CCP packet and update our state accordingly.
2176 * We assume the caller has the xmit or recv path locked.
2177 */
2178static void
2179ppp_ccp_peek(struct ppp *ppp, struct sk_buff *skb, int inbound)
2180{
2181 unsigned char *dp;
2182 int len;
2183
2184 if (!pskb_may_pull(skb, CCP_HDRLEN + 2))
2185 return; /* no header */
2186 dp = skb->data + 2;
2187
2188 switch (CCP_CODE(dp)) {
2189 case CCP_CONFREQ:
2190
2191 /* A ConfReq starts negotiation of compression
2192 * in one direction of transmission,
2193 * and hence brings it down...but which way?
2194 *
2195 * Remember:
2196 * A ConfReq indicates what the sender would like to receive
2197 */
2198 if(inbound)
2199 /* He is proposing what I should send */
2200 ppp->xstate &= ~SC_COMP_RUN;
2201 else
2202 /* I am proposing to what he should send */
2203 ppp->rstate &= ~SC_DECOMP_RUN;
2204
2205 break;
2206
2207 case CCP_TERMREQ:
2208 case CCP_TERMACK:
2209 /*
2210 * CCP is going down, both directions of transmission
2211 */
2212 ppp->rstate &= ~SC_DECOMP_RUN;
2213 ppp->xstate &= ~SC_COMP_RUN;
2214 break;
2215
2216 case CCP_CONFACK:
2217 if ((ppp->flags & (SC_CCP_OPEN | SC_CCP_UP)) != SC_CCP_OPEN)
2218 break;
2219 len = CCP_LENGTH(dp);
2220 if (!pskb_may_pull(skb, len + 2))
2221 return; /* too short */
2222 dp += CCP_HDRLEN;
2223 len -= CCP_HDRLEN;
2224 if (len < CCP_OPT_MINLEN || len < CCP_OPT_LENGTH(dp))
2225 break;
2226 if (inbound) {
2227 /* we will start receiving compressed packets */
2228 if (ppp->rc_state == 0)
2229 break;
2230 if (ppp->rcomp->decomp_init(ppp->rc_state, dp, len,
2231 ppp->file.index, 0, ppp->mru, ppp->debug)) {
2232 ppp->rstate |= SC_DECOMP_RUN;
2233 ppp->rstate &= ~(SC_DC_ERROR | SC_DC_FERROR);
2234 }
2235 } else {
2236 /* we will soon start sending compressed packets */
2237 if (ppp->xc_state == 0)
2238 break;
2239 if (ppp->xcomp->comp_init(ppp->xc_state, dp, len,
2240 ppp->file.index, 0, ppp->debug))
2241 ppp->xstate |= SC_COMP_RUN;
2242 }
2243 break;
2244
2245 case CCP_RESETACK:
2246 /* reset the [de]compressor */
2247 if ((ppp->flags & SC_CCP_UP) == 0)
2248 break;
2249 if (inbound) {
2250 if (ppp->rc_state && (ppp->rstate & SC_DECOMP_RUN)) {
2251 ppp->rcomp->decomp_reset(ppp->rc_state);
2252 ppp->rstate &= ~SC_DC_ERROR;
2253 }
2254 } else {
2255 if (ppp->xc_state && (ppp->xstate & SC_COMP_RUN))
2256 ppp->xcomp->comp_reset(ppp->xc_state);
2257 }
2258 break;
2259 }
2260}
2261
2262/* Free up compression resources. */
2263static void
2264ppp_ccp_closed(struct ppp *ppp)
2265{
2266 void *xstate, *rstate;
2267 struct compressor *xcomp, *rcomp;
2268
2269 ppp_lock(ppp);
2270 ppp->flags &= ~(SC_CCP_OPEN | SC_CCP_UP);
2271 ppp->xstate = 0;
2272 xcomp = ppp->xcomp;
2273 xstate = ppp->xc_state;
2274 ppp->xc_state = NULL;
2275 ppp->rstate = 0;
2276 rcomp = ppp->rcomp;
2277 rstate = ppp->rc_state;
2278 ppp->rc_state = NULL;
2279 ppp_unlock(ppp);
2280
2281 if (xstate) {
2282 xcomp->comp_free(xstate);
2283 module_put(xcomp->owner);
2284 }
2285 if (rstate) {
2286 rcomp->decomp_free(rstate);
2287 module_put(rcomp->owner);
2288 }
2289}
2290
2291/* List of compressors. */
2292static LIST_HEAD(compressor_list);
2293static DEFINE_SPINLOCK(compressor_list_lock);
2294
2295struct compressor_entry {
2296 struct list_head list;
2297 struct compressor *comp;
2298};
2299
2300static struct compressor_entry *
2301find_comp_entry(int proto)
2302{
2303 struct compressor_entry *ce;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304
Domen Puncer81616c52005-09-10 00:27:04 -07002305 list_for_each_entry(ce, &compressor_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 if (ce->comp->compress_proto == proto)
2307 return ce;
2308 }
2309 return NULL;
2310}
2311
2312/* Register a compressor */
2313int
2314ppp_register_compressor(struct compressor *cp)
2315{
2316 struct compressor_entry *ce;
2317 int ret;
2318 spin_lock(&compressor_list_lock);
2319 ret = -EEXIST;
2320 if (find_comp_entry(cp->compress_proto) != 0)
2321 goto out;
2322 ret = -ENOMEM;
2323 ce = kmalloc(sizeof(struct compressor_entry), GFP_ATOMIC);
2324 if (ce == 0)
2325 goto out;
2326 ret = 0;
2327 ce->comp = cp;
2328 list_add(&ce->list, &compressor_list);
2329 out:
2330 spin_unlock(&compressor_list_lock);
2331 return ret;
2332}
2333
2334/* Unregister a compressor */
2335void
2336ppp_unregister_compressor(struct compressor *cp)
2337{
2338 struct compressor_entry *ce;
2339
2340 spin_lock(&compressor_list_lock);
2341 ce = find_comp_entry(cp->compress_proto);
2342 if (ce != 0 && ce->comp == cp) {
2343 list_del(&ce->list);
2344 kfree(ce);
2345 }
2346 spin_unlock(&compressor_list_lock);
2347}
2348
2349/* Find a compressor. */
2350static struct compressor *
2351find_compressor(int type)
2352{
2353 struct compressor_entry *ce;
2354 struct compressor *cp = NULL;
2355
2356 spin_lock(&compressor_list_lock);
2357 ce = find_comp_entry(type);
2358 if (ce != 0) {
2359 cp = ce->comp;
2360 if (!try_module_get(cp->owner))
2361 cp = NULL;
2362 }
2363 spin_unlock(&compressor_list_lock);
2364 return cp;
2365}
2366
2367/*
2368 * Miscelleneous stuff.
2369 */
2370
2371static void
2372ppp_get_stats(struct ppp *ppp, struct ppp_stats *st)
2373{
2374 struct slcompress *vj = ppp->vj;
2375
2376 memset(st, 0, sizeof(*st));
2377 st->p.ppp_ipackets = ppp->stats.rx_packets;
2378 st->p.ppp_ierrors = ppp->stats.rx_errors;
2379 st->p.ppp_ibytes = ppp->stats.rx_bytes;
2380 st->p.ppp_opackets = ppp->stats.tx_packets;
2381 st->p.ppp_oerrors = ppp->stats.tx_errors;
2382 st->p.ppp_obytes = ppp->stats.tx_bytes;
2383 if (vj == 0)
2384 return;
2385 st->vj.vjs_packets = vj->sls_o_compressed + vj->sls_o_uncompressed;
2386 st->vj.vjs_compressed = vj->sls_o_compressed;
2387 st->vj.vjs_searches = vj->sls_o_searches;
2388 st->vj.vjs_misses = vj->sls_o_misses;
2389 st->vj.vjs_errorin = vj->sls_i_error;
2390 st->vj.vjs_tossed = vj->sls_i_tossed;
2391 st->vj.vjs_uncompressedin = vj->sls_i_uncompressed;
2392 st->vj.vjs_compressedin = vj->sls_i_compressed;
2393}
2394
2395/*
2396 * Stuff for handling the lists of ppp units and channels
2397 * and for initialization.
2398 */
2399
2400/*
2401 * Create a new ppp interface unit. Fails if it can't allocate memory
2402 * or if there is already a unit with the requested number.
2403 * unit == -1 means allocate a new number.
2404 */
2405static struct ppp *
2406ppp_create_interface(int unit, int *retp)
2407{
2408 struct ppp *ppp;
2409 struct net_device *dev = NULL;
2410 int ret = -ENOMEM;
2411 int i;
2412
2413 ppp = kmalloc(sizeof(struct ppp), GFP_KERNEL);
2414 if (!ppp)
2415 goto out;
2416 dev = alloc_netdev(0, "", ppp_setup);
2417 if (!dev)
2418 goto out1;
2419 memset(ppp, 0, sizeof(struct ppp));
2420
2421 ppp->mru = PPP_MRU;
2422 init_ppp_file(&ppp->file, INTERFACE);
2423 ppp->file.hdrlen = PPP_HDRLEN - 2; /* don't count proto bytes */
2424 for (i = 0; i < NUM_NP; ++i)
2425 ppp->npmode[i] = NPMODE_PASS;
2426 INIT_LIST_HEAD(&ppp->channels);
2427 spin_lock_init(&ppp->rlock);
2428 spin_lock_init(&ppp->wlock);
2429#ifdef CONFIG_PPP_MULTILINK
2430 ppp->minseq = -1;
2431 skb_queue_head_init(&ppp->mrq);
2432#endif /* CONFIG_PPP_MULTILINK */
2433 ppp->dev = dev;
2434 dev->priv = ppp;
2435
2436 dev->hard_start_xmit = ppp_start_xmit;
2437 dev->get_stats = ppp_net_stats;
2438 dev->do_ioctl = ppp_net_ioctl;
2439
2440 ret = -EEXIST;
Arjan van de Ven8ed965d2006-03-23 03:00:21 -08002441 mutex_lock(&all_ppp_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 if (unit < 0)
2443 unit = cardmap_find_first_free(all_ppp_units);
2444 else if (cardmap_get(all_ppp_units, unit) != NULL)
2445 goto out2; /* unit already exists */
2446
2447 /* Initialize the new ppp unit */
2448 ppp->file.index = unit;
2449 sprintf(dev->name, "ppp%d", unit);
2450
2451 ret = register_netdev(dev);
2452 if (ret != 0) {
2453 printk(KERN_ERR "PPP: couldn't register device %s (%d)\n",
2454 dev->name, ret);
2455 goto out2;
2456 }
2457
2458 atomic_inc(&ppp_unit_count);
2459 cardmap_set(&all_ppp_units, unit, ppp);
Arjan van de Ven8ed965d2006-03-23 03:00:21 -08002460 mutex_unlock(&all_ppp_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 *retp = 0;
2462 return ppp;
2463
2464out2:
Arjan van de Ven8ed965d2006-03-23 03:00:21 -08002465 mutex_unlock(&all_ppp_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 free_netdev(dev);
2467out1:
2468 kfree(ppp);
2469out:
2470 *retp = ret;
2471 return NULL;
2472}
2473
2474/*
2475 * Initialize a ppp_file structure.
2476 */
2477static void
2478init_ppp_file(struct ppp_file *pf, int kind)
2479{
2480 pf->kind = kind;
2481 skb_queue_head_init(&pf->xq);
2482 skb_queue_head_init(&pf->rq);
2483 atomic_set(&pf->refcnt, 1);
2484 init_waitqueue_head(&pf->rwait);
2485}
2486
2487/*
2488 * Take down a ppp interface unit - called when the owning file
2489 * (the one that created the unit) is closed or detached.
2490 */
2491static void ppp_shutdown_interface(struct ppp *ppp)
2492{
2493 struct net_device *dev;
2494
Arjan van de Ven8ed965d2006-03-23 03:00:21 -08002495 mutex_lock(&all_ppp_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 ppp_lock(ppp);
2497 dev = ppp->dev;
2498 ppp->dev = NULL;
2499 ppp_unlock(ppp);
2500 /* This will call dev_close() for us. */
2501 if (dev) {
2502 unregister_netdev(dev);
2503 free_netdev(dev);
2504 }
2505 cardmap_set(&all_ppp_units, ppp->file.index, NULL);
2506 ppp->file.dead = 1;
2507 ppp->owner = NULL;
2508 wake_up_interruptible(&ppp->file.rwait);
Arjan van de Ven8ed965d2006-03-23 03:00:21 -08002509 mutex_unlock(&all_ppp_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510}
2511
2512/*
2513 * Free the memory used by a ppp unit. This is only called once
2514 * there are no channels connected to the unit and no file structs
2515 * that reference the unit.
2516 */
2517static void ppp_destroy_interface(struct ppp *ppp)
2518{
2519 atomic_dec(&ppp_unit_count);
2520
2521 if (!ppp->file.dead || ppp->n_channels) {
2522 /* "can't happen" */
2523 printk(KERN_ERR "ppp: destroying ppp struct %p but dead=%d "
2524 "n_channels=%d !\n", ppp, ppp->file.dead,
2525 ppp->n_channels);
2526 return;
2527 }
2528
2529 ppp_ccp_closed(ppp);
2530 if (ppp->vj) {
2531 slhc_free(ppp->vj);
2532 ppp->vj = NULL;
2533 }
2534 skb_queue_purge(&ppp->file.xq);
2535 skb_queue_purge(&ppp->file.rq);
2536#ifdef CONFIG_PPP_MULTILINK
2537 skb_queue_purge(&ppp->mrq);
2538#endif /* CONFIG_PPP_MULTILINK */
2539#ifdef CONFIG_PPP_FILTER
Jesper Juhl96edf832005-05-03 14:38:09 -07002540 kfree(ppp->pass_filter);
2541 ppp->pass_filter = NULL;
2542 kfree(ppp->active_filter);
2543 ppp->active_filter = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544#endif /* CONFIG_PPP_FILTER */
2545
2546 kfree(ppp);
2547}
2548
2549/*
2550 * Locate an existing ppp unit.
Arjan van de Ven8ed965d2006-03-23 03:00:21 -08002551 * The caller should have locked the all_ppp_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 */
2553static struct ppp *
2554ppp_find_unit(int unit)
2555{
2556 return cardmap_get(all_ppp_units, unit);
2557}
2558
2559/*
2560 * Locate an existing ppp channel.
2561 * The caller should have locked the all_channels_lock.
2562 * First we look in the new_channels list, then in the
2563 * all_channels list. If found in the new_channels list,
2564 * we move it to the all_channels list. This is for speed
2565 * when we have a lot of channels in use.
2566 */
2567static struct channel *
2568ppp_find_channel(int unit)
2569{
2570 struct channel *pch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571
Domen Puncer81616c52005-09-10 00:27:04 -07002572 list_for_each_entry(pch, &new_channels, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 if (pch->file.index == unit) {
Akinobu Mita179e0912006-06-26 00:24:41 -07002574 list_move(&pch->list, &all_channels);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 return pch;
2576 }
2577 }
Domen Puncer81616c52005-09-10 00:27:04 -07002578 list_for_each_entry(pch, &all_channels, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 if (pch->file.index == unit)
2580 return pch;
2581 }
2582 return NULL;
2583}
2584
2585/*
2586 * Connect a PPP channel to a PPP interface unit.
2587 */
2588static int
2589ppp_connect_channel(struct channel *pch, int unit)
2590{
2591 struct ppp *ppp;
2592 int ret = -ENXIO;
2593 int hdrlen;
2594
Arjan van de Ven8ed965d2006-03-23 03:00:21 -08002595 mutex_lock(&all_ppp_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 ppp = ppp_find_unit(unit);
2597 if (ppp == 0)
2598 goto out;
2599 write_lock_bh(&pch->upl);
2600 ret = -EINVAL;
2601 if (pch->ppp != 0)
2602 goto outl;
2603
2604 ppp_lock(ppp);
2605 if (pch->file.hdrlen > ppp->file.hdrlen)
2606 ppp->file.hdrlen = pch->file.hdrlen;
2607 hdrlen = pch->file.hdrlen + 2; /* for protocol bytes */
2608 if (ppp->dev && hdrlen > ppp->dev->hard_header_len)
2609 ppp->dev->hard_header_len = hdrlen;
2610 list_add_tail(&pch->clist, &ppp->channels);
2611 ++ppp->n_channels;
2612 pch->ppp = ppp;
2613 atomic_inc(&ppp->file.refcnt);
2614 ppp_unlock(ppp);
2615 ret = 0;
2616
2617 outl:
2618 write_unlock_bh(&pch->upl);
2619 out:
Arjan van de Ven8ed965d2006-03-23 03:00:21 -08002620 mutex_unlock(&all_ppp_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 return ret;
2622}
2623
2624/*
2625 * Disconnect a channel from its ppp unit.
2626 */
2627static int
2628ppp_disconnect_channel(struct channel *pch)
2629{
2630 struct ppp *ppp;
2631 int err = -EINVAL;
2632
2633 write_lock_bh(&pch->upl);
2634 ppp = pch->ppp;
2635 pch->ppp = NULL;
2636 write_unlock_bh(&pch->upl);
2637 if (ppp != 0) {
2638 /* remove it from the ppp unit's list */
2639 ppp_lock(ppp);
2640 list_del(&pch->clist);
2641 if (--ppp->n_channels == 0)
2642 wake_up_interruptible(&ppp->file.rwait);
2643 ppp_unlock(ppp);
2644 if (atomic_dec_and_test(&ppp->file.refcnt))
2645 ppp_destroy_interface(ppp);
2646 err = 0;
2647 }
2648 return err;
2649}
2650
2651/*
2652 * Free up the resources used by a ppp channel.
2653 */
2654static void ppp_destroy_channel(struct channel *pch)
2655{
2656 atomic_dec(&channel_count);
2657
2658 if (!pch->file.dead) {
2659 /* "can't happen" */
2660 printk(KERN_ERR "ppp: destroying undead channel %p !\n",
2661 pch);
2662 return;
2663 }
2664 skb_queue_purge(&pch->file.xq);
2665 skb_queue_purge(&pch->file.rq);
2666 kfree(pch);
2667}
2668
2669static void __exit ppp_cleanup(void)
2670{
2671 /* should never happen */
2672 if (atomic_read(&ppp_unit_count) || atomic_read(&channel_count))
2673 printk(KERN_ERR "PPP: removing module but units remain!\n");
2674 cardmap_destroy(&all_ppp_units);
2675 if (unregister_chrdev(PPP_MAJOR, "ppp") != 0)
2676 printk(KERN_ERR "PPP: failed to unregister PPP device\n");
gregkh@suse.de56b22932005-03-23 10:01:41 -08002677 class_device_destroy(ppp_class, MKDEV(PPP_MAJOR, 0));
2678 class_destroy(ppp_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679}
2680
2681/*
2682 * Cardmap implementation.
2683 */
2684static void *cardmap_get(struct cardmap *map, unsigned int nr)
2685{
2686 struct cardmap *p;
2687 int i;
2688
2689 for (p = map; p != NULL; ) {
2690 if ((i = nr >> p->shift) >= CARDMAP_WIDTH)
2691 return NULL;
2692 if (p->shift == 0)
2693 return p->ptr[i];
2694 nr &= ~(CARDMAP_MASK << p->shift);
2695 p = p->ptr[i];
2696 }
2697 return NULL;
2698}
2699
2700static void cardmap_set(struct cardmap **pmap, unsigned int nr, void *ptr)
2701{
2702 struct cardmap *p;
2703 int i;
2704
2705 p = *pmap;
2706 if (p == NULL || (nr >> p->shift) >= CARDMAP_WIDTH) {
2707 do {
2708 /* need a new top level */
2709 struct cardmap *np = kmalloc(sizeof(*np), GFP_KERNEL);
2710 memset(np, 0, sizeof(*np));
2711 np->ptr[0] = p;
2712 if (p != NULL) {
2713 np->shift = p->shift + CARDMAP_ORDER;
2714 p->parent = np;
2715 } else
2716 np->shift = 0;
2717 p = np;
2718 } while ((nr >> p->shift) >= CARDMAP_WIDTH);
2719 *pmap = p;
2720 }
2721 while (p->shift > 0) {
2722 i = (nr >> p->shift) & CARDMAP_MASK;
2723 if (p->ptr[i] == NULL) {
2724 struct cardmap *np = kmalloc(sizeof(*np), GFP_KERNEL);
2725 memset(np, 0, sizeof(*np));
2726 np->shift = p->shift - CARDMAP_ORDER;
2727 np->parent = p;
2728 p->ptr[i] = np;
2729 }
2730 if (ptr == NULL)
2731 clear_bit(i, &p->inuse);
2732 p = p->ptr[i];
2733 }
2734 i = nr & CARDMAP_MASK;
2735 p->ptr[i] = ptr;
2736 if (ptr != NULL)
2737 set_bit(i, &p->inuse);
2738 else
2739 clear_bit(i, &p->inuse);
2740}
2741
2742static unsigned int cardmap_find_first_free(struct cardmap *map)
2743{
2744 struct cardmap *p;
2745 unsigned int nr = 0;
2746 int i;
2747
2748 if ((p = map) == NULL)
2749 return 0;
2750 for (;;) {
2751 i = find_first_zero_bit(&p->inuse, CARDMAP_WIDTH);
2752 if (i >= CARDMAP_WIDTH) {
2753 if (p->parent == NULL)
2754 return CARDMAP_WIDTH << p->shift;
2755 p = p->parent;
2756 i = (nr >> p->shift) & CARDMAP_MASK;
2757 set_bit(i, &p->inuse);
2758 continue;
2759 }
2760 nr = (nr & (~CARDMAP_MASK << p->shift)) | (i << p->shift);
2761 if (p->shift == 0 || p->ptr[i] == NULL)
2762 return nr;
2763 p = p->ptr[i];
2764 }
2765}
2766
2767static void cardmap_destroy(struct cardmap **pmap)
2768{
2769 struct cardmap *p, *np;
2770 int i;
2771
2772 for (p = *pmap; p != NULL; p = np) {
2773 if (p->shift != 0) {
2774 for (i = 0; i < CARDMAP_WIDTH; ++i)
2775 if (p->ptr[i] != NULL)
2776 break;
2777 if (i < CARDMAP_WIDTH) {
2778 np = p->ptr[i];
2779 p->ptr[i] = NULL;
2780 continue;
2781 }
2782 }
2783 np = p->parent;
2784 kfree(p);
2785 }
2786 *pmap = NULL;
2787}
2788
2789/* Module/initialization stuff */
2790
2791module_init(ppp_init);
2792module_exit(ppp_cleanup);
2793
2794EXPORT_SYMBOL(ppp_register_channel);
2795EXPORT_SYMBOL(ppp_unregister_channel);
2796EXPORT_SYMBOL(ppp_channel_index);
2797EXPORT_SYMBOL(ppp_unit_number);
2798EXPORT_SYMBOL(ppp_input);
2799EXPORT_SYMBOL(ppp_input_error);
2800EXPORT_SYMBOL(ppp_output_wakeup);
2801EXPORT_SYMBOL(ppp_register_compressor);
2802EXPORT_SYMBOL(ppp_unregister_compressor);
2803MODULE_LICENSE("GPL");
2804MODULE_ALIAS_CHARDEV_MAJOR(PPP_MAJOR);
2805MODULE_ALIAS("/dev/ppp");