sh: convert /proc/cpu/aligmnent, /proc/cpu/kernel_alignment to seq_file
[safe/jmp/linux-2.6] / net / atm / pppoatm.c
1 /* net/atm/pppoatm.c - RFC2364 PPP over ATM/AAL5 */
2
3 /* Copyright 1999-2000 by Mitchell Blank Jr */
4 /* Based on clip.c; 1995-1999 by Werner Almesberger, EPFL LRC/ICA */
5 /* And on ppp_async.c; Copyright 1999 Paul Mackerras */
6 /* And help from Jens Axboe */
7
8 /*
9  *  This program is free software; you can redistribute it and/or
10  *  modify it under the terms of the GNU General Public License
11  *  as published by the Free Software Foundation; either version
12  *  2 of the License, or (at your option) any later version.
13  *
14  * This driver provides the encapsulation and framing for sending
15  * and receiving PPP frames in ATM AAL5 PDUs.
16  */
17
18 /*
19  * One shortcoming of this driver is that it does not comply with
20  * section 8 of RFC2364 - we are supposed to detect a change
21  * in encapsulation and immediately abort the connection (in order
22  * to avoid a black-hole being created if our peer loses state
23  * and changes encapsulation unilaterally.  However, since the
24  * ppp_generic layer actually does the decapsulation, we need
25  * a way of notifying it when we _think_ there might be a problem)
26  * There's two cases:
27  *   1. LLC-encapsulation was missing when it was enabled.  In
28  *      this case, we should tell the upper layer "tear down
29  *      this session if this skb looks ok to you"
30  *   2. LLC-encapsulation was present when it was disabled.  Then
31  *      we need to tell the upper layer "this packet may be
32  *      ok, but if its in error tear down the session"
33  * These hooks are not yet available in ppp_generic
34  */
35
36 #include <linux/module.h>
37 #include <linux/init.h>
38 #include <linux/skbuff.h>
39 #include <linux/atm.h>
40 #include <linux/atmdev.h>
41 #include <linux/capability.h>
42 #include <linux/ppp_defs.h>
43 #include <linux/if_ppp.h>
44 #include <linux/ppp_channel.h>
45 #include <linux/atmppp.h>
46
47 #include "common.h"
48
49 enum pppoatm_encaps {
50         e_autodetect = PPPOATM_ENCAPS_AUTODETECT,
51         e_vc = PPPOATM_ENCAPS_VC,
52         e_llc = PPPOATM_ENCAPS_LLC,
53 };
54
55 struct pppoatm_vcc {
56         struct atm_vcc  *atmvcc;        /* VCC descriptor */
57         void (*old_push)(struct atm_vcc *, struct sk_buff *);
58         void (*old_pop)(struct atm_vcc *, struct sk_buff *);
59                                         /* keep old push/pop for detaching */
60         enum pppoatm_encaps encaps;
61         int flags;                      /* SC_COMP_PROT - compress protocol */
62         struct ppp_channel chan;        /* interface to generic ppp layer */
63         struct tasklet_struct wakeup_tasklet;
64 };
65
66 /*
67  * Header used for LLC Encapsulated PPP (4 bytes) followed by the LCP protocol
68  * ID (0xC021) used in autodetection
69  */
70 static const unsigned char pppllc[6] = { 0xFE, 0xFE, 0x03, 0xCF, 0xC0, 0x21 };
71 #define LLC_LEN         (4)
72
73 static inline struct pppoatm_vcc *atmvcc_to_pvcc(const struct atm_vcc *atmvcc)
74 {
75         return (struct pppoatm_vcc *) (atmvcc->user_back);
76 }
77
78 static inline struct pppoatm_vcc *chan_to_pvcc(const struct ppp_channel *chan)
79 {
80         return (struct pppoatm_vcc *) (chan->private);
81 }
82
83 /*
84  * We can't do this directly from our _pop handler, since the ppp code
85  * doesn't want to be called in interrupt context, so we do it from
86  * a tasklet
87  */
88 static void pppoatm_wakeup_sender(unsigned long arg)
89 {
90         ppp_output_wakeup((struct ppp_channel *) arg);
91 }
92
93 /*
94  * This gets called every time the ATM card has finished sending our
95  * skb.  The ->old_pop will take care up normal atm flow control,
96  * but we also need to wake up the device if we blocked it
97  */
98 static void pppoatm_pop(struct atm_vcc *atmvcc, struct sk_buff *skb)
99 {
100         struct pppoatm_vcc *pvcc = atmvcc_to_pvcc(atmvcc);
101         pvcc->old_pop(atmvcc, skb);
102         /*
103          * We don't really always want to do this since it's
104          * really inefficient - it would be much better if we could
105          * test if we had actually throttled the generic layer.
106          * Unfortunately then there would be a nasty SMP race where
107          * we could clear that flag just as we refuse another packet.
108          * For now we do the safe thing.
109          */
110         tasklet_schedule(&pvcc->wakeup_tasklet);
111 }
112
113 /*
114  * Unbind from PPP - currently we only do this when closing the socket,
115  * but we could put this into an ioctl if need be
116  */
117 static void pppoatm_unassign_vcc(struct atm_vcc *atmvcc)
118 {
119         struct pppoatm_vcc *pvcc;
120         pvcc = atmvcc_to_pvcc(atmvcc);
121         atmvcc->push = pvcc->old_push;
122         atmvcc->pop = pvcc->old_pop;
123         tasklet_kill(&pvcc->wakeup_tasklet);
124         ppp_unregister_channel(&pvcc->chan);
125         atmvcc->user_back = NULL;
126         kfree(pvcc);
127         /* Gee, I hope we have the big kernel lock here... */
128         module_put(THIS_MODULE);
129 }
130
131 /* Called when an AAL5 PDU comes in */
132 static void pppoatm_push(struct atm_vcc *atmvcc, struct sk_buff *skb)
133 {
134         struct pppoatm_vcc *pvcc = atmvcc_to_pvcc(atmvcc);
135         pr_debug("pppoatm push\n");
136         if (skb == NULL) {                      /* VCC was closed */
137                 pr_debug("removing ATMPPP VCC %p\n", pvcc);
138                 pppoatm_unassign_vcc(atmvcc);
139                 atmvcc->push(atmvcc, NULL);     /* Pass along bad news */
140                 return;
141         }
142         atm_return(atmvcc, skb->truesize);
143         switch (pvcc->encaps) {
144         case e_llc:
145                 if (skb->len < LLC_LEN ||
146                     memcmp(skb->data, pppllc, LLC_LEN))
147                         goto error;
148                 skb_pull(skb, LLC_LEN);
149                 break;
150         case e_autodetect:
151                 if (pvcc->chan.ppp == NULL) {   /* Not bound yet! */
152                         kfree_skb(skb);
153                         return;
154                 }
155                 if (skb->len >= sizeof(pppllc) &&
156                     !memcmp(skb->data, pppllc, sizeof(pppllc))) {
157                         pvcc->encaps = e_llc;
158                         skb_pull(skb, LLC_LEN);
159                         break;
160                 }
161                 if (skb->len >= (sizeof(pppllc) - LLC_LEN) &&
162                     !memcmp(skb->data, &pppllc[LLC_LEN],
163                     sizeof(pppllc) - LLC_LEN)) {
164                         pvcc->encaps = e_vc;
165                         pvcc->chan.mtu += LLC_LEN;
166                         break;
167                 }
168                 pr_debug("Couldn't autodetect yet "
169                     "(skb: %02X %02X %02X %02X %02X %02X)\n",
170                     skb->data[0], skb->data[1], skb->data[2],
171                     skb->data[3], skb->data[4], skb->data[5]);
172                 goto error;
173         case e_vc:
174                 break;
175         }
176         ppp_input(&pvcc->chan, skb);
177         return;
178     error:
179         kfree_skb(skb);
180         ppp_input_error(&pvcc->chan, 0);
181 }
182
183 /*
184  * Called by the ppp_generic.c to send a packet - returns true if packet
185  * was accepted.  If we return false, then it's our job to call
186  * ppp_output_wakeup(chan) when we're feeling more up to it.
187  * Note that in the ENOMEM case (as opposed to the !atm_may_send case)
188  * we should really drop the packet, but the generic layer doesn't
189  * support this yet.  We just return 'DROP_PACKET' which we actually define
190  * as success, just to be clear what we're really doing.
191  */
192 #define DROP_PACKET 1
193 static int pppoatm_send(struct ppp_channel *chan, struct sk_buff *skb)
194 {
195         struct pppoatm_vcc *pvcc = chan_to_pvcc(chan);
196         ATM_SKB(skb)->vcc = pvcc->atmvcc;
197         pr_debug("pppoatm_send (skb=0x%p, vcc=0x%p)\n", skb, pvcc->atmvcc);
198         if (skb->data[0] == '\0' && (pvcc->flags & SC_COMP_PROT))
199                 (void) skb_pull(skb, 1);
200         switch (pvcc->encaps) {         /* LLC encapsulation needed */
201         case e_llc:
202                 if (skb_headroom(skb) < LLC_LEN) {
203                         struct sk_buff *n;
204                         n = skb_realloc_headroom(skb, LLC_LEN);
205                         if (n != NULL &&
206                             !atm_may_send(pvcc->atmvcc, n->truesize)) {
207                                 kfree_skb(n);
208                                 goto nospace;
209                         }
210                         kfree_skb(skb);
211                         if ((skb = n) == NULL)
212                                 return DROP_PACKET;
213                 } else if (!atm_may_send(pvcc->atmvcc, skb->truesize))
214                         goto nospace;
215                 memcpy(skb_push(skb, LLC_LEN), pppllc, LLC_LEN);
216                 break;
217         case e_vc:
218                 if (!atm_may_send(pvcc->atmvcc, skb->truesize))
219                         goto nospace;
220                 break;
221         case e_autodetect:
222                 pr_debug("Trying to send without setting encaps!\n");
223                 kfree_skb(skb);
224                 return 1;
225         }
226
227         atomic_add(skb->truesize, &sk_atm(ATM_SKB(skb)->vcc)->sk_wmem_alloc);
228         ATM_SKB(skb)->atm_options = ATM_SKB(skb)->vcc->atm_options;
229         pr_debug("atm_skb(%p)->vcc(%p)->dev(%p)\n", skb, ATM_SKB(skb)->vcc,
230             ATM_SKB(skb)->vcc->dev);
231         return ATM_SKB(skb)->vcc->send(ATM_SKB(skb)->vcc, skb)
232             ? DROP_PACKET : 1;
233     nospace:
234         /*
235          * We don't have space to send this SKB now, but we might have
236          * already applied SC_COMP_PROT compression, so may need to undo
237          */
238         if ((pvcc->flags & SC_COMP_PROT) && skb_headroom(skb) > 0 &&
239             skb->data[-1] == '\0')
240                 (void) skb_push(skb, 1);
241         return 0;
242 }
243
244 /* This handles ioctls sent to the /dev/ppp interface */
245 static int pppoatm_devppp_ioctl(struct ppp_channel *chan, unsigned int cmd,
246         unsigned long arg)
247 {
248         switch (cmd) {
249         case PPPIOCGFLAGS:
250                 return put_user(chan_to_pvcc(chan)->flags, (int __user *) arg)
251                     ? -EFAULT : 0;
252         case PPPIOCSFLAGS:
253                 return get_user(chan_to_pvcc(chan)->flags, (int __user *) arg)
254                     ? -EFAULT : 0;
255         }
256         return -ENOTTY;
257 }
258
259 static /*const*/ struct ppp_channel_ops pppoatm_ops = {
260         .start_xmit = pppoatm_send,
261         .ioctl = pppoatm_devppp_ioctl,
262 };
263
264 static int pppoatm_assign_vcc(struct atm_vcc *atmvcc, void __user *arg)
265 {
266         struct atm_backend_ppp be;
267         struct pppoatm_vcc *pvcc;
268         int err;
269         /*
270          * Each PPPoATM instance has its own tasklet - this is just a
271          * prototypical one used to initialize them
272          */
273         static const DECLARE_TASKLET(tasklet_proto, pppoatm_wakeup_sender, 0);
274         if (copy_from_user(&be, arg, sizeof be))
275                 return -EFAULT;
276         if (be.encaps != PPPOATM_ENCAPS_AUTODETECT &&
277             be.encaps != PPPOATM_ENCAPS_VC && be.encaps != PPPOATM_ENCAPS_LLC)
278                 return -EINVAL;
279         pvcc = kzalloc(sizeof(*pvcc), GFP_KERNEL);
280         if (pvcc == NULL)
281                 return -ENOMEM;
282         pvcc->atmvcc = atmvcc;
283         pvcc->old_push = atmvcc->push;
284         pvcc->old_pop = atmvcc->pop;
285         pvcc->encaps = (enum pppoatm_encaps) be.encaps;
286         pvcc->chan.private = pvcc;
287         pvcc->chan.ops = &pppoatm_ops;
288         pvcc->chan.mtu = atmvcc->qos.txtp.max_sdu - PPP_HDRLEN -
289             (be.encaps == e_vc ? 0 : LLC_LEN);
290         pvcc->wakeup_tasklet = tasklet_proto;
291         pvcc->wakeup_tasklet.data = (unsigned long) &pvcc->chan;
292         if ((err = ppp_register_channel(&pvcc->chan)) != 0) {
293                 kfree(pvcc);
294                 return err;
295         }
296         atmvcc->user_back = pvcc;
297         atmvcc->push = pppoatm_push;
298         atmvcc->pop = pppoatm_pop;
299         __module_get(THIS_MODULE);
300         return 0;
301 }
302
303 /*
304  * This handles ioctls actually performed on our vcc - we must return
305  * -ENOIOCTLCMD for any unrecognized ioctl
306  */
307 static int pppoatm_ioctl(struct socket *sock, unsigned int cmd,
308         unsigned long arg)
309 {
310         struct atm_vcc *atmvcc = ATM_SD(sock);
311         void __user *argp = (void __user *)arg;
312
313         if (cmd != ATM_SETBACKEND && atmvcc->push != pppoatm_push)
314                 return -ENOIOCTLCMD;
315         switch (cmd) {
316         case ATM_SETBACKEND: {
317                 atm_backend_t b;
318                 if (get_user(b, (atm_backend_t __user *) argp))
319                         return -EFAULT;
320                 if (b != ATM_BACKEND_PPP)
321                         return -ENOIOCTLCMD;
322                 if (!capable(CAP_NET_ADMIN))
323                         return -EPERM;
324                 return pppoatm_assign_vcc(atmvcc, argp);
325                 }
326         case PPPIOCGCHAN:
327                 return put_user(ppp_channel_index(&atmvcc_to_pvcc(atmvcc)->
328                     chan), (int __user *) argp) ? -EFAULT : 0;
329         case PPPIOCGUNIT:
330                 return put_user(ppp_unit_number(&atmvcc_to_pvcc(atmvcc)->
331                     chan), (int __user *) argp) ? -EFAULT : 0;
332         }
333         return -ENOIOCTLCMD;
334 }
335
336 static struct atm_ioctl pppoatm_ioctl_ops = {
337         .owner  = THIS_MODULE,
338         .ioctl  = pppoatm_ioctl,
339 };
340
341 static int __init pppoatm_init(void)
342 {
343         register_atm_ioctl(&pppoatm_ioctl_ops);
344         return 0;
345 }
346
347 static void __exit pppoatm_exit(void)
348 {
349         deregister_atm_ioctl(&pppoatm_ioctl_ops);
350 }
351
352 module_init(pppoatm_init);
353 module_exit(pppoatm_exit);
354
355 MODULE_AUTHOR("Mitchell Blank Jr <mitch@sfgoth.com>");
356 MODULE_DESCRIPTION("RFC2364 PPP over ATM/AAL5");
357 MODULE_LICENSE("GPL");