This source file includes following definitions.
- atmvcc_to_pvcc
- chan_to_pvcc
- pppoatm_wakeup_sender
- pppoatm_release_cb
- pppoatm_pop
- pppoatm_unassign_vcc
- pppoatm_push
- pppoatm_may_send
- pppoatm_send
- pppoatm_devppp_ioctl
- pppoatm_assign_vcc
- pppoatm_ioctl
- pppoatm_init
- pppoatm_exit
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
50 enum pppoatm_encaps {
51 e_autodetect = PPPOATM_ENCAPS_AUTODETECT,
52 e_vc = PPPOATM_ENCAPS_VC,
53 e_llc = PPPOATM_ENCAPS_LLC,
54 };
55
56 struct 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
86 static const unsigned char pppllc[6] = { 0xFE, 0xFE, 0x03, 0xCF, 0xC0, 0x21 };
87 #define LLC_LEN (4)
88
89 static inline struct pppoatm_vcc *atmvcc_to_pvcc(const struct atm_vcc *atmvcc)
90 {
91 return (struct pppoatm_vcc *) (atmvcc->user_back);
92 }
93
94 static 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
104 static void pppoatm_wakeup_sender(unsigned long arg)
105 {
106 ppp_output_wakeup((struct ppp_channel *) arg);
107 }
108
109 static 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
132 static 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
165 static 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
179 static 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: %6ph)\n", skb->data);
220 goto error;
221 case e_vc:
222 break;
223 }
224 ppp_input(&pvcc->chan, skb);
225 return;
226
227 error:
228 kfree_skb(skb);
229 ppp_input_error(&pvcc->chan, 0);
230 }
231
232 static int pppoatm_may_send(struct pppoatm_vcc *pvcc, int size)
233 {
234
235
236
237
238
239
240
241 if (atm_may_send(pvcc->atmvcc, size) &&
242 atomic_inc_not_zero(&pvcc->inflight))
243 return 1;
244
245
246
247
248
249
250
251
252 test_and_set_bit(BLOCKED, &pvcc->blocked);
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271 if (atm_may_send(pvcc->atmvcc, size) &&
272 atomic_inc_not_zero(&pvcc->inflight))
273 return 1;
274
275 return 0;
276 }
277
278
279
280
281
282
283
284
285
286 #define DROP_PACKET 1
287 static int pppoatm_send(struct ppp_channel *chan, struct sk_buff *skb)
288 {
289 struct pppoatm_vcc *pvcc = chan_to_pvcc(chan);
290 struct atm_vcc *vcc;
291 int ret;
292
293 ATM_SKB(skb)->vcc = pvcc->atmvcc;
294 pr_debug("(skb=0x%p, vcc=0x%p)\n", skb, pvcc->atmvcc);
295 if (skb->data[0] == '\0' && (pvcc->flags & SC_COMP_PROT))
296 (void) skb_pull(skb, 1);
297
298 vcc = ATM_SKB(skb)->vcc;
299 bh_lock_sock(sk_atm(vcc));
300 if (sock_owned_by_user(sk_atm(vcc))) {
301
302
303
304
305
306 test_and_set_bit(BLOCKED, &pvcc->blocked);
307 goto nospace;
308 }
309 if (test_bit(ATM_VF_RELEASED, &vcc->flags) ||
310 test_bit(ATM_VF_CLOSE, &vcc->flags) ||
311 !test_bit(ATM_VF_READY, &vcc->flags)) {
312 bh_unlock_sock(sk_atm(vcc));
313 kfree_skb(skb);
314 return DROP_PACKET;
315 }
316
317 switch (pvcc->encaps) {
318 case e_llc:
319 if (skb_headroom(skb) < LLC_LEN) {
320 struct sk_buff *n;
321 n = skb_realloc_headroom(skb, LLC_LEN);
322 if (n != NULL &&
323 !pppoatm_may_send(pvcc, n->truesize)) {
324 kfree_skb(n);
325 goto nospace;
326 }
327 consume_skb(skb);
328 skb = n;
329 if (skb == NULL) {
330 bh_unlock_sock(sk_atm(vcc));
331 return DROP_PACKET;
332 }
333 } else if (!pppoatm_may_send(pvcc, skb->truesize))
334 goto nospace;
335 memcpy(skb_push(skb, LLC_LEN), pppllc, LLC_LEN);
336 break;
337 case e_vc:
338 if (!pppoatm_may_send(pvcc, skb->truesize))
339 goto nospace;
340 break;
341 case e_autodetect:
342 bh_unlock_sock(sk_atm(vcc));
343 pr_debug("Trying to send without setting encaps!\n");
344 kfree_skb(skb);
345 return 1;
346 }
347
348 atm_account_tx(vcc, skb);
349 pr_debug("atm_skb(%p)->vcc(%p)->dev(%p)\n",
350 skb, ATM_SKB(skb)->vcc, ATM_SKB(skb)->vcc->dev);
351 ret = ATM_SKB(skb)->vcc->send(ATM_SKB(skb)->vcc, skb)
352 ? DROP_PACKET : 1;
353 bh_unlock_sock(sk_atm(vcc));
354 return ret;
355 nospace:
356 bh_unlock_sock(sk_atm(vcc));
357
358
359
360
361 if ((pvcc->flags & SC_COMP_PROT) && skb_headroom(skb) > 0 &&
362 skb->data[-1] == '\0')
363 (void) skb_push(skb, 1);
364 return 0;
365 }
366
367
368 static int pppoatm_devppp_ioctl(struct ppp_channel *chan, unsigned int cmd,
369 unsigned long arg)
370 {
371 switch (cmd) {
372 case PPPIOCGFLAGS:
373 return put_user(chan_to_pvcc(chan)->flags, (int __user *) arg)
374 ? -EFAULT : 0;
375 case PPPIOCSFLAGS:
376 return get_user(chan_to_pvcc(chan)->flags, (int __user *) arg)
377 ? -EFAULT : 0;
378 }
379 return -ENOTTY;
380 }
381
382 static const struct ppp_channel_ops pppoatm_ops = {
383 .start_xmit = pppoatm_send,
384 .ioctl = pppoatm_devppp_ioctl,
385 };
386
387 static int pppoatm_assign_vcc(struct atm_vcc *atmvcc, void __user *arg)
388 {
389 struct atm_backend_ppp be;
390 struct pppoatm_vcc *pvcc;
391 int err;
392
393
394
395
396 static const DECLARE_TASKLET(tasklet_proto, pppoatm_wakeup_sender, 0);
397 if (copy_from_user(&be, arg, sizeof be))
398 return -EFAULT;
399 if (be.encaps != PPPOATM_ENCAPS_AUTODETECT &&
400 be.encaps != PPPOATM_ENCAPS_VC && be.encaps != PPPOATM_ENCAPS_LLC)
401 return -EINVAL;
402 pvcc = kzalloc(sizeof(*pvcc), GFP_KERNEL);
403 if (pvcc == NULL)
404 return -ENOMEM;
405 pvcc->atmvcc = atmvcc;
406
407
408 atomic_set(&pvcc->inflight, NONE_INFLIGHT);
409 pvcc->old_push = atmvcc->push;
410 pvcc->old_pop = atmvcc->pop;
411 pvcc->old_owner = atmvcc->owner;
412 pvcc->old_release_cb = atmvcc->release_cb;
413 pvcc->encaps = (enum pppoatm_encaps) be.encaps;
414 pvcc->chan.private = pvcc;
415 pvcc->chan.ops = &pppoatm_ops;
416 pvcc->chan.mtu = atmvcc->qos.txtp.max_sdu - PPP_HDRLEN -
417 (be.encaps == e_vc ? 0 : LLC_LEN);
418 pvcc->wakeup_tasklet = tasklet_proto;
419 pvcc->wakeup_tasklet.data = (unsigned long) &pvcc->chan;
420 err = ppp_register_channel(&pvcc->chan);
421 if (err != 0) {
422 kfree(pvcc);
423 return err;
424 }
425 atmvcc->user_back = pvcc;
426 atmvcc->push = pppoatm_push;
427 atmvcc->pop = pppoatm_pop;
428 atmvcc->release_cb = pppoatm_release_cb;
429 __module_get(THIS_MODULE);
430 atmvcc->owner = THIS_MODULE;
431
432
433
434 vcc_process_recv_queue(atmvcc);
435 return 0;
436 }
437
438
439
440
441
442 static int pppoatm_ioctl(struct socket *sock, unsigned int cmd,
443 unsigned long arg)
444 {
445 struct atm_vcc *atmvcc = ATM_SD(sock);
446 void __user *argp = (void __user *)arg;
447
448 if (cmd != ATM_SETBACKEND && atmvcc->push != pppoatm_push)
449 return -ENOIOCTLCMD;
450 switch (cmd) {
451 case ATM_SETBACKEND: {
452 atm_backend_t b;
453 if (get_user(b, (atm_backend_t __user *) argp))
454 return -EFAULT;
455 if (b != ATM_BACKEND_PPP)
456 return -ENOIOCTLCMD;
457 if (!capable(CAP_NET_ADMIN))
458 return -EPERM;
459 if (sock->state != SS_CONNECTED)
460 return -EINVAL;
461 return pppoatm_assign_vcc(atmvcc, argp);
462 }
463 case PPPIOCGCHAN:
464 return put_user(ppp_channel_index(&atmvcc_to_pvcc(atmvcc)->
465 chan), (int __user *) argp) ? -EFAULT : 0;
466 case PPPIOCGUNIT:
467 return put_user(ppp_unit_number(&atmvcc_to_pvcc(atmvcc)->
468 chan), (int __user *) argp) ? -EFAULT : 0;
469 }
470 return -ENOIOCTLCMD;
471 }
472
473 static struct atm_ioctl pppoatm_ioctl_ops = {
474 .owner = THIS_MODULE,
475 .ioctl = pppoatm_ioctl,
476 };
477
478 static int __init pppoatm_init(void)
479 {
480 register_atm_ioctl(&pppoatm_ioctl_ops);
481 return 0;
482 }
483
484 static void __exit pppoatm_exit(void)
485 {
486 deregister_atm_ioctl(&pppoatm_ioctl_ops);
487 }
488
489 module_init(pppoatm_init);
490 module_exit(pppoatm_exit);
491
492 MODULE_AUTHOR("Mitchell Blank Jr <mitch@sfgoth.com>");
493 MODULE_DESCRIPTION("RFC2364 PPP over ATM/AAL5");
494 MODULE_LICENSE("GPL");