1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33#define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
34
35#include <linux/module.h>
36#include <linux/init.h>
37#include <linux/interrupt.h>
38#include <linux/skbuff.h>
39#include <linux/slab.h>
40#include <linux/atm.h>
41#include <linux/atmdev.h>
42#include <linux/capability.h>
43#include <linux/ppp_defs.h>
44#include <linux/ppp-ioctl.h>
45#include <linux/ppp_channel.h>
46#include <linux/atmppp.h>
47
48#include "common.h"
49
50enum pppoatm_encaps {
51 e_autodetect = PPPOATM_ENCAPS_AUTODETECT,
52 e_vc = PPPOATM_ENCAPS_VC,
53 e_llc = PPPOATM_ENCAPS_LLC,
54};
55
56struct pppoatm_vcc {
57 struct atm_vcc *atmvcc;
58 void (*old_push)(struct atm_vcc *, struct sk_buff *);
59 void (*old_pop)(struct atm_vcc *, struct sk_buff *);
60 void (*old_release_cb)(struct atm_vcc *);
61 struct module *old_owner;
62
63 enum pppoatm_encaps encaps;
64 atomic_t inflight;
65 unsigned long blocked;
66 int flags;
67 struct ppp_channel chan;
68 struct tasklet_struct wakeup_tasklet;
69};
70
71
72
73
74
75
76
77
78#define NONE_INFLIGHT -2
79
80#define BLOCKED 0
81
82
83
84
85
86static const unsigned char pppllc[6] = { 0xFE, 0xFE, 0x03, 0xCF, 0xC0, 0x21 };
87#define LLC_LEN (4)
88
89static inline struct pppoatm_vcc *atmvcc_to_pvcc(const struct atm_vcc *atmvcc)
90{
91 return (struct pppoatm_vcc *) (atmvcc->user_back);
92}
93
94static inline struct pppoatm_vcc *chan_to_pvcc(const struct ppp_channel *chan)
95{
96 return (struct pppoatm_vcc *) (chan->private);
97}
98
99
100
101
102
103
104static void pppoatm_wakeup_sender(unsigned long arg)
105{
106 ppp_output_wakeup((struct ppp_channel *) arg);
107}
108
109static void pppoatm_release_cb(struct atm_vcc *atmvcc)
110{
111 struct pppoatm_vcc *pvcc = atmvcc_to_pvcc(atmvcc);
112
113
114
115
116
117
118
119
120
121
122 if (test_and_clear_bit(BLOCKED, &pvcc->blocked))
123 tasklet_schedule(&pvcc->wakeup_tasklet);
124 if (pvcc->old_release_cb)
125 pvcc->old_release_cb(atmvcc);
126}
127
128
129
130
131
132static void pppoatm_pop(struct atm_vcc *atmvcc, struct sk_buff *skb)
133{
134 struct pppoatm_vcc *pvcc = atmvcc_to_pvcc(atmvcc);
135
136 pvcc->old_pop(atmvcc, skb);
137 atomic_dec(&pvcc->inflight);
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157 if (test_and_clear_bit(BLOCKED, &pvcc->blocked))
158 tasklet_schedule(&pvcc->wakeup_tasklet);
159}
160
161
162
163
164
165static void pppoatm_unassign_vcc(struct atm_vcc *atmvcc)
166{
167 struct pppoatm_vcc *pvcc;
168 pvcc = atmvcc_to_pvcc(atmvcc);
169 atmvcc->push = pvcc->old_push;
170 atmvcc->pop = pvcc->old_pop;
171 atmvcc->release_cb = pvcc->old_release_cb;
172 tasklet_kill(&pvcc->wakeup_tasklet);
173 ppp_unregister_channel(&pvcc->chan);
174 atmvcc->user_back = NULL;
175 kfree(pvcc);
176}
177
178
179static void pppoatm_push(struct atm_vcc *atmvcc, struct sk_buff *skb)
180{
181 struct pppoatm_vcc *pvcc = atmvcc_to_pvcc(atmvcc);
182 pr_debug("\n");
183 if (skb == NULL) {
184 struct module *module;
185
186 pr_debug("removing ATMPPP VCC %p\n", pvcc);
187 module = pvcc->old_owner;
188 pppoatm_unassign_vcc(atmvcc);
189 atmvcc->push(atmvcc, NULL);
190 module_put(module);
191 return;
192 }
193 atm_return(atmvcc, skb->truesize);
194 switch (pvcc->encaps) {
195 case e_llc:
196 if (skb->len < LLC_LEN ||
197 memcmp(skb->data, pppllc, LLC_LEN))
198 goto error;
199 skb_pull(skb, LLC_LEN);
200 break;
201 case e_autodetect:
202 if (pvcc->chan.ppp == NULL) {
203 kfree_skb(skb);
204 return;
205 }
206 if (skb->len >= sizeof(pppllc) &&
207 !memcmp(skb->data, pppllc, sizeof(pppllc))) {
208 pvcc->encaps = e_llc;
209 skb_pull(skb, LLC_LEN);
210 break;
211 }
212 if (skb->len >= (sizeof(pppllc) - LLC_LEN) &&
213 !memcmp(skb->data, &pppllc[LLC_LEN],
214 sizeof(pppllc) - LLC_LEN)) {
215 pvcc->encaps = e_vc;
216 pvcc->chan.mtu += LLC_LEN;
217 break;
218 }
219 pr_debug("Couldn't autodetect yet (skb: %02X %02X %02X %02X %02X %02X)\n",
220 skb->data[0], skb->data[1], skb->data[2],
221 skb->data[3], skb->data[4], skb->data[5]);
222 goto error;
223 case e_vc:
224 break;
225 }
226 ppp_input(&pvcc->chan, skb);
227 return;
228
229error:
230 kfree_skb(skb);
231 ppp_input_error(&pvcc->chan, 0);
232}
233
234static int pppoatm_may_send(struct pppoatm_vcc *pvcc, int size)
235{
236
237
238
239
240
241
242
243 if (atm_may_send(pvcc->atmvcc, size) &&
244 atomic_inc_not_zero(&pvcc->inflight))
245 return 1;
246
247
248
249
250
251
252
253
254 test_and_set_bit(BLOCKED, &pvcc->blocked);
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273 if (atm_may_send(pvcc->atmvcc, size) &&
274 atomic_inc_not_zero(&pvcc->inflight))
275 return 1;
276
277 return 0;
278}
279
280
281
282
283
284
285
286
287
288#define DROP_PACKET 1
289static int pppoatm_send(struct ppp_channel *chan, struct sk_buff *skb)
290{
291 struct pppoatm_vcc *pvcc = chan_to_pvcc(chan);
292 struct atm_vcc *vcc;
293 int ret;
294
295 ATM_SKB(skb)->vcc = pvcc->atmvcc;
296 pr_debug("(skb=0x%p, vcc=0x%p)\n", skb, pvcc->atmvcc);
297 if (skb->data[0] == '\0' && (pvcc->flags & SC_COMP_PROT))
298 (void) skb_pull(skb, 1);
299
300 vcc = ATM_SKB(skb)->vcc;
301 bh_lock_sock(sk_atm(vcc));
302 if (sock_owned_by_user(sk_atm(vcc))) {
303
304
305
306
307
308 test_and_set_bit(BLOCKED, &pvcc->blocked);
309 goto nospace;
310 }
311 if (test_bit(ATM_VF_RELEASED, &vcc->flags) ||
312 test_bit(ATM_VF_CLOSE, &vcc->flags) ||
313 !test_bit(ATM_VF_READY, &vcc->flags)) {
314 bh_unlock_sock(sk_atm(vcc));
315 kfree_skb(skb);
316 return DROP_PACKET;
317 }
318
319 switch (pvcc->encaps) {
320 case e_llc:
321 if (skb_headroom(skb) < LLC_LEN) {
322 struct sk_buff *n;
323 n = skb_realloc_headroom(skb, LLC_LEN);
324 if (n != NULL &&
325 !pppoatm_may_send(pvcc, n->truesize)) {
326 kfree_skb(n);
327 goto nospace;
328 }
329 consume_skb(skb);
330 skb = n;
331 if (skb == NULL) {
332 bh_unlock_sock(sk_atm(vcc));
333 return DROP_PACKET;
334 }
335 } else if (!pppoatm_may_send(pvcc, skb->truesize))
336 goto nospace;
337 memcpy(skb_push(skb, LLC_LEN), pppllc, LLC_LEN);
338 break;
339 case e_vc:
340 if (!pppoatm_may_send(pvcc, skb->truesize))
341 goto nospace;
342 break;
343 case e_autodetect:
344 bh_unlock_sock(sk_atm(vcc));
345 pr_debug("Trying to send without setting encaps!\n");
346 kfree_skb(skb);
347 return 1;
348 }
349
350 atm_account_tx(vcc, skb);
351 pr_debug("atm_skb(%p)->vcc(%p)->dev(%p)\n",
352 skb, ATM_SKB(skb)->vcc, ATM_SKB(skb)->vcc->dev);
353 ret = ATM_SKB(skb)->vcc->send(ATM_SKB(skb)->vcc, skb)
354 ? DROP_PACKET : 1;
355 bh_unlock_sock(sk_atm(vcc));
356 return ret;
357nospace:
358 bh_unlock_sock(sk_atm(vcc));
359
360
361
362
363 if ((pvcc->flags & SC_COMP_PROT) && skb_headroom(skb) > 0 &&
364 skb->data[-1] == '\0')
365 (void) skb_push(skb, 1);
366 return 0;
367}
368
369
370static int pppoatm_devppp_ioctl(struct ppp_channel *chan, unsigned int cmd,
371 unsigned long arg)
372{
373 switch (cmd) {
374 case PPPIOCGFLAGS:
375 return put_user(chan_to_pvcc(chan)->flags, (int __user *) arg)
376 ? -EFAULT : 0;
377 case PPPIOCSFLAGS:
378 return get_user(chan_to_pvcc(chan)->flags, (int __user *) arg)
379 ? -EFAULT : 0;
380 }
381 return -ENOTTY;
382}
383
384static const struct ppp_channel_ops pppoatm_ops = {
385 .start_xmit = pppoatm_send,
386 .ioctl = pppoatm_devppp_ioctl,
387};
388
389static int pppoatm_assign_vcc(struct atm_vcc *atmvcc, void __user *arg)
390{
391 struct atm_backend_ppp be;
392 struct pppoatm_vcc *pvcc;
393 int err;
394
395
396
397
398 static const DECLARE_TASKLET(tasklet_proto, pppoatm_wakeup_sender, 0);
399 if (copy_from_user(&be, arg, sizeof be))
400 return -EFAULT;
401 if (be.encaps != PPPOATM_ENCAPS_AUTODETECT &&
402 be.encaps != PPPOATM_ENCAPS_VC && be.encaps != PPPOATM_ENCAPS_LLC)
403 return -EINVAL;
404 pvcc = kzalloc(sizeof(*pvcc), GFP_KERNEL);
405 if (pvcc == NULL)
406 return -ENOMEM;
407 pvcc->atmvcc = atmvcc;
408
409
410 atomic_set(&pvcc->inflight, NONE_INFLIGHT);
411 pvcc->old_push = atmvcc->push;
412 pvcc->old_pop = atmvcc->pop;
413 pvcc->old_owner = atmvcc->owner;
414 pvcc->old_release_cb = atmvcc->release_cb;
415 pvcc->encaps = (enum pppoatm_encaps) be.encaps;
416 pvcc->chan.private = pvcc;
417 pvcc->chan.ops = &pppoatm_ops;
418 pvcc->chan.mtu = atmvcc->qos.txtp.max_sdu - PPP_HDRLEN -
419 (be.encaps == e_vc ? 0 : LLC_LEN);
420 pvcc->wakeup_tasklet = tasklet_proto;
421 pvcc->wakeup_tasklet.data = (unsigned long) &pvcc->chan;
422 err = ppp_register_channel(&pvcc->chan);
423 if (err != 0) {
424 kfree(pvcc);
425 return err;
426 }
427 atmvcc->user_back = pvcc;
428 atmvcc->push = pppoatm_push;
429 atmvcc->pop = pppoatm_pop;
430 atmvcc->release_cb = pppoatm_release_cb;
431 __module_get(THIS_MODULE);
432 atmvcc->owner = THIS_MODULE;
433
434
435
436 vcc_process_recv_queue(atmvcc);
437 return 0;
438}
439
440
441
442
443
444static int pppoatm_ioctl(struct socket *sock, unsigned int cmd,
445 unsigned long arg)
446{
447 struct atm_vcc *atmvcc = ATM_SD(sock);
448 void __user *argp = (void __user *)arg;
449
450 if (cmd != ATM_SETBACKEND && atmvcc->push != pppoatm_push)
451 return -ENOIOCTLCMD;
452 switch (cmd) {
453 case ATM_SETBACKEND: {
454 atm_backend_t b;
455 if (get_user(b, (atm_backend_t __user *) argp))
456 return -EFAULT;
457 if (b != ATM_BACKEND_PPP)
458 return -ENOIOCTLCMD;
459 if (!capable(CAP_NET_ADMIN))
460 return -EPERM;
461 if (sock->state != SS_CONNECTED)
462 return -EINVAL;
463 return pppoatm_assign_vcc(atmvcc, argp);
464 }
465 case PPPIOCGCHAN:
466 return put_user(ppp_channel_index(&atmvcc_to_pvcc(atmvcc)->
467 chan), (int __user *) argp) ? -EFAULT : 0;
468 case PPPIOCGUNIT:
469 return put_user(ppp_unit_number(&atmvcc_to_pvcc(atmvcc)->
470 chan), (int __user *) argp) ? -EFAULT : 0;
471 }
472 return -ENOIOCTLCMD;
473}
474
475static struct atm_ioctl pppoatm_ioctl_ops = {
476 .owner = THIS_MODULE,
477 .ioctl = pppoatm_ioctl,
478};
479
480static int __init pppoatm_init(void)
481{
482 register_atm_ioctl(&pppoatm_ioctl_ops);
483 return 0;
484}
485
486static void __exit pppoatm_exit(void)
487{
488 deregister_atm_ioctl(&pppoatm_ioctl_ops);
489}
490
491module_init(pppoatm_init);
492module_exit(pppoatm_exit);
493
494MODULE_AUTHOR("Mitchell Blank Jr <mitch@sfgoth.com>");
495MODULE_DESCRIPTION("RFC2364 PPP over ATM/AAL5");
496MODULE_LICENSE("GPL");
497