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