]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /** -*- linux-c -*- *********************************************************** |
2 | * Linux PPP over Ethernet (PPPoX/PPPoE) Sockets | |
3 | * | |
4 | * PPPoX --- Generic PPP encapsulation socket family | |
5 | * PPPoE --- PPP over Ethernet (RFC 2516) | |
6 | * | |
7 | * | |
8 | * Version: 0.7.0 | |
9 | * | |
10 | * 220102 : Fix module use count on failure in pppoe_create, pppox_sk -acme | |
11 | * 030700 : Fixed connect logic to allow for disconnect. | |
12 | * 270700 : Fixed potential SMP problems; we must protect against | |
13 | * simultaneous invocation of ppp_input | |
14 | * and ppp_unregister_channel. | |
15 | * 040800 : Respect reference count mechanisms on net-devices. | |
16 | * 200800 : fix kfree(skb) in pppoe_rcv (acme) | |
17 | * Module reference count is decremented in the right spot now, | |
18 | * guards against sock_put not actually freeing the sk | |
19 | * in pppoe_release. | |
20 | * 051000 : Initialization cleanup. | |
21 | * 111100 : Fix recvmsg. | |
22 | * 050101 : Fix PADT procesing. | |
23 | * 140501 : Use pppoe_rcv_core to handle all backlog. (Alexey) | |
24 | * 170701 : Do not lock_sock with rwlock held. (DaveM) | |
25 | * Ignore discovery frames if user has socket | |
26 | * locked. (DaveM) | |
27 | * Ignore return value of dev_queue_xmit in __pppoe_xmit | |
28 | * or else we may kfree an SKB twice. (DaveM) | |
29 | * 190701 : When doing copies of skb's in __pppoe_xmit, always delete | |
30 | * the original skb that was passed in on success, never on | |
31 | * failure. Delete the copy of the skb on failure to avoid | |
32 | * a memory leak. | |
33 | * 081001 : Misc. cleanup (licence string, non-blocking, prevent | |
34 | * reference of device on close). | |
35 | * 121301 : New ppp channels interface; cannot unregister a channel | |
36 | * from interrupts. Thus, we mark the socket as a ZOMBIE | |
37 | * and do the unregistration later. | |
38 | * 081002 : seq_file support for proc stuff -acme | |
39 | * 111602 : Merge all 2.4 fixes into 2.5/2.6 tree. Label 2.5/2.6 | |
40 | * as version 0.7. Spacing cleanup. | |
41 | * Author: Michal Ostrowski <mostrows@speakeasy.net> | |
42 | * Contributors: | |
43 | * Arnaldo Carvalho de Melo <acme@conectiva.com.br> | |
44 | * David S. Miller (davem@redhat.com) | |
45 | * | |
46 | * License: | |
47 | * This program is free software; you can redistribute it and/or | |
48 | * modify it under the terms of the GNU General Public License | |
49 | * as published by the Free Software Foundation; either version | |
50 | * 2 of the License, or (at your option) any later version. | |
51 | * | |
52 | */ | |
53 | ||
54 | #include <linux/string.h> | |
55 | #include <linux/module.h> | |
56 | #include <linux/kernel.h> | |
57 | #include <linux/slab.h> | |
58 | #include <linux/errno.h> | |
59 | #include <linux/netdevice.h> | |
60 | #include <linux/net.h> | |
61 | #include <linux/inetdevice.h> | |
62 | #include <linux/etherdevice.h> | |
63 | #include <linux/skbuff.h> | |
64 | #include <linux/init.h> | |
65 | #include <linux/if_ether.h> | |
66 | #include <linux/if_pppox.h> | |
67 | #include <linux/ppp_channel.h> | |
68 | #include <linux/ppp_defs.h> | |
69 | #include <linux/if_ppp.h> | |
70 | #include <linux/notifier.h> | |
71 | #include <linux/file.h> | |
72 | #include <linux/proc_fs.h> | |
73 | #include <linux/seq_file.h> | |
74 | ||
75 | #include <net/sock.h> | |
76 | ||
77 | #include <asm/uaccess.h> | |
78 | ||
79 | #define PPPOE_HASH_BITS 4 | |
80 | #define PPPOE_HASH_SIZE (1<<PPPOE_HASH_BITS) | |
81 | ||
82 | static struct ppp_channel_ops pppoe_chan_ops; | |
83 | ||
84 | static int pppoe_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg); | |
85 | static int pppoe_xmit(struct ppp_channel *chan, struct sk_buff *skb); | |
86 | static int __pppoe_xmit(struct sock *sk, struct sk_buff *skb); | |
87 | ||
88 | static struct proto_ops pppoe_ops; | |
89 | static DEFINE_RWLOCK(pppoe_hash_lock); | |
90 | ||
91 | static struct ppp_channel_ops pppoe_chan_ops; | |
92 | ||
93 | static inline int cmp_2_addr(struct pppoe_addr *a, struct pppoe_addr *b) | |
94 | { | |
95 | return (a->sid == b->sid && | |
96 | (memcmp(a->remote, b->remote, ETH_ALEN) == 0)); | |
97 | } | |
98 | ||
99 | static inline int cmp_addr(struct pppoe_addr *a, unsigned long sid, char *addr) | |
100 | { | |
101 | return (a->sid == sid && | |
102 | (memcmp(a->remote,addr,ETH_ALEN) == 0)); | |
103 | } | |
104 | ||
105 | static int hash_item(unsigned long sid, unsigned char *addr) | |
106 | { | |
107 | char hash = 0; | |
108 | int i, j; | |
109 | ||
110 | for (i = 0; i < ETH_ALEN ; ++i) { | |
111 | for (j = 0; j < 8/PPPOE_HASH_BITS ; ++j) { | |
112 | hash ^= addr[i] >> ( j * PPPOE_HASH_BITS ); | |
113 | } | |
114 | } | |
115 | ||
116 | for (i = 0; i < (sizeof(unsigned long)*8) / PPPOE_HASH_BITS ; ++i) | |
117 | hash ^= sid >> (i*PPPOE_HASH_BITS); | |
118 | ||
119 | return hash & ( PPPOE_HASH_SIZE - 1 ); | |
120 | } | |
121 | ||
122 | /* zeroed because its in .bss */ | |
123 | static struct pppox_sock *item_hash_table[PPPOE_HASH_SIZE]; | |
124 | ||
125 | /********************************************************************** | |
126 | * | |
127 | * Set/get/delete/rehash items (internal versions) | |
128 | * | |
129 | **********************************************************************/ | |
130 | static struct pppox_sock *__get_item(unsigned long sid, unsigned char *addr) | |
131 | { | |
132 | int hash = hash_item(sid, addr); | |
133 | struct pppox_sock *ret; | |
134 | ||
135 | ret = item_hash_table[hash]; | |
136 | ||
137 | while (ret && !cmp_addr(&ret->pppoe_pa, sid, addr)) | |
138 | ret = ret->next; | |
139 | ||
140 | return ret; | |
141 | } | |
142 | ||
143 | static int __set_item(struct pppox_sock *po) | |
144 | { | |
145 | int hash = hash_item(po->pppoe_pa.sid, po->pppoe_pa.remote); | |
146 | struct pppox_sock *ret; | |
147 | ||
148 | ret = item_hash_table[hash]; | |
149 | while (ret) { | |
150 | if (cmp_2_addr(&ret->pppoe_pa, &po->pppoe_pa)) | |
151 | return -EALREADY; | |
152 | ||
153 | ret = ret->next; | |
154 | } | |
155 | ||
156 | if (!ret) { | |
157 | po->next = item_hash_table[hash]; | |
158 | item_hash_table[hash] = po; | |
159 | } | |
160 | ||
161 | return 0; | |
162 | } | |
163 | ||
164 | static struct pppox_sock *__delete_item(unsigned long sid, char *addr) | |
165 | { | |
166 | int hash = hash_item(sid, addr); | |
167 | struct pppox_sock *ret, **src; | |
168 | ||
169 | ret = item_hash_table[hash]; | |
170 | src = &item_hash_table[hash]; | |
171 | ||
172 | while (ret) { | |
173 | if (cmp_addr(&ret->pppoe_pa, sid, addr)) { | |
174 | *src = ret->next; | |
175 | break; | |
176 | } | |
177 | ||
178 | src = &ret->next; | |
179 | ret = ret->next; | |
180 | } | |
181 | ||
182 | return ret; | |
183 | } | |
184 | ||
185 | /********************************************************************** | |
186 | * | |
187 | * Set/get/delete/rehash items | |
188 | * | |
189 | **********************************************************************/ | |
190 | static inline struct pppox_sock *get_item(unsigned long sid, | |
191 | unsigned char *addr) | |
192 | { | |
193 | struct pppox_sock *po; | |
194 | ||
195 | read_lock_bh(&pppoe_hash_lock); | |
196 | po = __get_item(sid, addr); | |
197 | if (po) | |
198 | sock_hold(sk_pppox(po)); | |
199 | read_unlock_bh(&pppoe_hash_lock); | |
200 | ||
201 | return po; | |
202 | } | |
203 | ||
204 | static inline struct pppox_sock *get_item_by_addr(struct sockaddr_pppox *sp) | |
205 | { | |
206 | return get_item(sp->sa_addr.pppoe.sid, sp->sa_addr.pppoe.remote); | |
207 | } | |
208 | ||
209 | static inline int set_item(struct pppox_sock *po) | |
210 | { | |
211 | int i; | |
212 | ||
213 | if (!po) | |
214 | return -EINVAL; | |
215 | ||
216 | write_lock_bh(&pppoe_hash_lock); | |
217 | i = __set_item(po); | |
218 | write_unlock_bh(&pppoe_hash_lock); | |
219 | ||
220 | return i; | |
221 | } | |
222 | ||
223 | static inline struct pppox_sock *delete_item(unsigned long sid, char *addr) | |
224 | { | |
225 | struct pppox_sock *ret; | |
226 | ||
227 | write_lock_bh(&pppoe_hash_lock); | |
228 | ret = __delete_item(sid, addr); | |
229 | write_unlock_bh(&pppoe_hash_lock); | |
230 | ||
231 | return ret; | |
232 | } | |
233 | ||
234 | ||
235 | ||
236 | /*************************************************************************** | |
237 | * | |
238 | * Handler for device events. | |
239 | * Certain device events require that sockets be unconnected. | |
240 | * | |
241 | **************************************************************************/ | |
242 | ||
243 | static void pppoe_flush_dev(struct net_device *dev) | |
244 | { | |
245 | int hash; | |
246 | ||
247 | BUG_ON(dev == NULL); | |
248 | ||
249 | read_lock_bh(&pppoe_hash_lock); | |
250 | for (hash = 0; hash < PPPOE_HASH_SIZE; hash++) { | |
251 | struct pppox_sock *po = item_hash_table[hash]; | |
252 | ||
253 | while (po != NULL) { | |
254 | if (po->pppoe_dev == dev) { | |
255 | struct sock *sk = sk_pppox(po); | |
256 | ||
257 | sock_hold(sk); | |
258 | po->pppoe_dev = NULL; | |
259 | ||
260 | /* We hold a reference to SK, now drop the | |
261 | * hash table lock so that we may attempt | |
262 | * to lock the socket (which can sleep). | |
263 | */ | |
264 | read_unlock_bh(&pppoe_hash_lock); | |
265 | ||
266 | lock_sock(sk); | |
267 | ||
268 | if (sk->sk_state & | |
269 | (PPPOX_CONNECTED | PPPOX_BOUND)) { | |
270 | pppox_unbind_sock(sk); | |
271 | dev_put(dev); | |
272 | sk->sk_state = PPPOX_ZOMBIE; | |
273 | sk->sk_state_change(sk); | |
274 | } | |
275 | ||
276 | release_sock(sk); | |
277 | ||
278 | sock_put(sk); | |
279 | ||
280 | read_lock_bh(&pppoe_hash_lock); | |
281 | ||
282 | /* Now restart from the beginning of this | |
283 | * hash chain. We always NULL out pppoe_dev | |
284 | * so we are guaranteed to make forward | |
285 | * progress. | |
286 | */ | |
287 | po = item_hash_table[hash]; | |
288 | continue; | |
289 | } | |
290 | po = po->next; | |
291 | } | |
292 | } | |
293 | read_unlock_bh(&pppoe_hash_lock); | |
294 | } | |
295 | ||
296 | static int pppoe_device_event(struct notifier_block *this, | |
297 | unsigned long event, void *ptr) | |
298 | { | |
299 | struct net_device *dev = (struct net_device *) ptr; | |
300 | ||
301 | /* Only look at sockets that are using this specific device. */ | |
302 | switch (event) { | |
303 | case NETDEV_CHANGEMTU: | |
304 | /* A change in mtu is a bad thing, requiring | |
305 | * LCP re-negotiation. | |
306 | */ | |
307 | ||
308 | case NETDEV_GOING_DOWN: | |
309 | case NETDEV_DOWN: | |
310 | /* Find every socket on this device and kill it. */ | |
311 | pppoe_flush_dev(dev); | |
312 | break; | |
313 | ||
314 | default: | |
315 | break; | |
316 | }; | |
317 | ||
318 | return NOTIFY_DONE; | |
319 | } | |
320 | ||
321 | ||
322 | static struct notifier_block pppoe_notifier = { | |
323 | .notifier_call = pppoe_device_event, | |
324 | }; | |
325 | ||
326 | ||
327 | /************************************************************************ | |
328 | * | |
329 | * Do the real work of receiving a PPPoE Session frame. | |
330 | * | |
331 | ***********************************************************************/ | |
332 | static int pppoe_rcv_core(struct sock *sk, struct sk_buff *skb) | |
333 | { | |
334 | struct pppox_sock *po = pppox_sk(sk); | |
335 | struct pppox_sock *relay_po = NULL; | |
336 | ||
337 | if (sk->sk_state & PPPOX_BOUND) { | |
338 | struct pppoe_hdr *ph = (struct pppoe_hdr *) skb->nh.raw; | |
339 | int len = ntohs(ph->length); | |
340 | skb_pull(skb, sizeof(struct pppoe_hdr)); | |
341 | skb_postpull_rcsum(skb, ph, sizeof(*ph)); | |
342 | if (pskb_trim_rcsum(skb, len)) | |
343 | goto abort_kfree; | |
344 | ||
345 | ppp_input(&po->chan, skb); | |
346 | } else if (sk->sk_state & PPPOX_RELAY) { | |
347 | relay_po = get_item_by_addr(&po->pppoe_relay); | |
348 | ||
349 | if (relay_po == NULL) | |
350 | goto abort_kfree; | |
351 | ||
352 | if ((sk_pppox(relay_po)->sk_state & PPPOX_CONNECTED) == 0) | |
353 | goto abort_put; | |
354 | ||
355 | skb_pull(skb, sizeof(struct pppoe_hdr)); | |
356 | if (!__pppoe_xmit(sk_pppox(relay_po), skb)) | |
357 | goto abort_put; | |
358 | } else { | |
359 | if (sock_queue_rcv_skb(sk, skb)) | |
360 | goto abort_kfree; | |
361 | } | |
362 | ||
363 | return NET_RX_SUCCESS; | |
364 | ||
365 | abort_put: | |
366 | sock_put(sk_pppox(relay_po)); | |
367 | ||
368 | abort_kfree: | |
369 | kfree_skb(skb); | |
370 | return NET_RX_DROP; | |
371 | } | |
372 | ||
373 | /************************************************************************ | |
374 | * | |
375 | * Receive wrapper called in BH context. | |
376 | * | |
377 | ***********************************************************************/ | |
378 | static int pppoe_rcv(struct sk_buff *skb, | |
379 | struct net_device *dev, | |
f2ccd8fa DM |
380 | struct packet_type *pt, |
381 | struct net_device *orig_dev) | |
1da177e4 LT |
382 | |
383 | { | |
384 | struct pppoe_hdr *ph; | |
385 | struct pppox_sock *po; | |
386 | struct sock *sk; | |
387 | int ret; | |
388 | ||
389 | if (!pskb_may_pull(skb, sizeof(struct pppoe_hdr))) | |
390 | goto drop; | |
391 | ||
392 | if (!(skb = skb_share_check(skb, GFP_ATOMIC))) | |
393 | goto out; | |
394 | ||
395 | ph = (struct pppoe_hdr *) skb->nh.raw; | |
396 | ||
397 | po = get_item((unsigned long) ph->sid, eth_hdr(skb)->h_source); | |
398 | if (!po) | |
399 | goto drop; | |
400 | ||
401 | sk = sk_pppox(po); | |
402 | bh_lock_sock(sk); | |
403 | ||
404 | /* Socket state is unknown, must put skb into backlog. */ | |
405 | if (sock_owned_by_user(sk) != 0) { | |
406 | sk_add_backlog(sk, skb); | |
407 | ret = NET_RX_SUCCESS; | |
408 | } else { | |
409 | ret = pppoe_rcv_core(sk, skb); | |
410 | } | |
411 | ||
412 | bh_unlock_sock(sk); | |
413 | sock_put(sk); | |
414 | ||
415 | return ret; | |
416 | drop: | |
417 | kfree_skb(skb); | |
418 | out: | |
419 | return NET_RX_DROP; | |
420 | } | |
421 | ||
422 | /************************************************************************ | |
423 | * | |
424 | * Receive a PPPoE Discovery frame. | |
425 | * This is solely for detection of PADT frames | |
426 | * | |
427 | ***********************************************************************/ | |
428 | static int pppoe_disc_rcv(struct sk_buff *skb, | |
429 | struct net_device *dev, | |
f2ccd8fa DM |
430 | struct packet_type *pt, |
431 | struct net_device *orig_dev) | |
1da177e4 LT |
432 | |
433 | { | |
434 | struct pppoe_hdr *ph; | |
435 | struct pppox_sock *po; | |
436 | ||
437 | if (!pskb_may_pull(skb, sizeof(struct pppoe_hdr))) | |
438 | goto abort; | |
439 | ||
440 | if (!(skb = skb_share_check(skb, GFP_ATOMIC))) | |
441 | goto out; | |
442 | ||
443 | ph = (struct pppoe_hdr *) skb->nh.raw; | |
444 | if (ph->code != PADT_CODE) | |
445 | goto abort; | |
446 | ||
447 | po = get_item((unsigned long) ph->sid, eth_hdr(skb)->h_source); | |
448 | if (po) { | |
449 | struct sock *sk = sk_pppox(po); | |
450 | ||
451 | bh_lock_sock(sk); | |
452 | ||
453 | /* If the user has locked the socket, just ignore | |
454 | * the packet. With the way two rcv protocols hook into | |
455 | * one socket family type, we cannot (easily) distinguish | |
456 | * what kind of SKB it is during backlog rcv. | |
457 | */ | |
458 | if (sock_owned_by_user(sk) == 0) { | |
459 | /* We're no longer connect at the PPPOE layer, | |
460 | * and must wait for ppp channel to disconnect us. | |
461 | */ | |
462 | sk->sk_state = PPPOX_ZOMBIE; | |
463 | } | |
464 | ||
465 | bh_unlock_sock(sk); | |
466 | sock_put(sk); | |
467 | } | |
468 | ||
469 | abort: | |
470 | kfree_skb(skb); | |
471 | out: | |
472 | return NET_RX_SUCCESS; /* Lies... :-) */ | |
473 | } | |
474 | ||
475 | static struct packet_type pppoes_ptype = { | |
476 | .type = __constant_htons(ETH_P_PPP_SES), | |
477 | .func = pppoe_rcv, | |
478 | }; | |
479 | ||
480 | static struct packet_type pppoed_ptype = { | |
481 | .type = __constant_htons(ETH_P_PPP_DISC), | |
482 | .func = pppoe_disc_rcv, | |
483 | }; | |
484 | ||
485 | static struct proto pppoe_sk_proto = { | |
486 | .name = "PPPOE", | |
487 | .owner = THIS_MODULE, | |
488 | .obj_size = sizeof(struct pppox_sock), | |
489 | }; | |
490 | ||
491 | /*********************************************************************** | |
492 | * | |
493 | * Initialize a new struct sock. | |
494 | * | |
495 | **********************************************************************/ | |
496 | static int pppoe_create(struct socket *sock) | |
497 | { | |
498 | int error = -ENOMEM; | |
499 | struct sock *sk; | |
500 | ||
501 | sk = sk_alloc(PF_PPPOX, GFP_KERNEL, &pppoe_sk_proto, 1); | |
502 | if (!sk) | |
503 | goto out; | |
504 | ||
505 | sock_init_data(sock, sk); | |
506 | ||
507 | sock->state = SS_UNCONNECTED; | |
508 | sock->ops = &pppoe_ops; | |
509 | ||
510 | sk->sk_backlog_rcv = pppoe_rcv_core; | |
511 | sk->sk_state = PPPOX_NONE; | |
512 | sk->sk_type = SOCK_STREAM; | |
513 | sk->sk_family = PF_PPPOX; | |
514 | sk->sk_protocol = PX_PROTO_OE; | |
515 | ||
516 | error = 0; | |
517 | out: return error; | |
518 | } | |
519 | ||
520 | static int pppoe_release(struct socket *sock) | |
521 | { | |
522 | struct sock *sk = sock->sk; | |
523 | struct pppox_sock *po; | |
524 | int error = 0; | |
525 | ||
526 | if (!sk) | |
527 | return 0; | |
528 | ||
529 | if (sock_flag(sk, SOCK_DEAD)) | |
530 | return -EBADF; | |
531 | ||
532 | pppox_unbind_sock(sk); | |
533 | ||
534 | /* Signal the death of the socket. */ | |
535 | sk->sk_state = PPPOX_DEAD; | |
536 | ||
537 | po = pppox_sk(sk); | |
538 | if (po->pppoe_pa.sid) { | |
539 | delete_item(po->pppoe_pa.sid, po->pppoe_pa.remote); | |
540 | } | |
541 | ||
542 | if (po->pppoe_dev) | |
543 | dev_put(po->pppoe_dev); | |
544 | ||
545 | po->pppoe_dev = NULL; | |
546 | ||
547 | sock_orphan(sk); | |
548 | sock->sk = NULL; | |
549 | ||
550 | skb_queue_purge(&sk->sk_receive_queue); | |
551 | sock_put(sk); | |
552 | ||
553 | return error; | |
554 | } | |
555 | ||
556 | ||
557 | static int pppoe_connect(struct socket *sock, struct sockaddr *uservaddr, | |
558 | int sockaddr_len, int flags) | |
559 | { | |
560 | struct sock *sk = sock->sk; | |
561 | struct net_device *dev = NULL; | |
562 | struct sockaddr_pppox *sp = (struct sockaddr_pppox *) uservaddr; | |
563 | struct pppox_sock *po = pppox_sk(sk); | |
564 | int error; | |
565 | ||
566 | lock_sock(sk); | |
567 | ||
568 | error = -EINVAL; | |
569 | if (sp->sa_protocol != PX_PROTO_OE) | |
570 | goto end; | |
571 | ||
572 | /* Check for already bound sockets */ | |
573 | error = -EBUSY; | |
574 | if ((sk->sk_state & PPPOX_CONNECTED) && sp->sa_addr.pppoe.sid) | |
575 | goto end; | |
576 | ||
577 | /* Check for already disconnected sockets, on attempts to disconnect */ | |
578 | error = -EALREADY; | |
579 | if ((sk->sk_state & PPPOX_DEAD) && !sp->sa_addr.pppoe.sid ) | |
580 | goto end; | |
581 | ||
582 | error = 0; | |
583 | if (po->pppoe_pa.sid) { | |
584 | pppox_unbind_sock(sk); | |
585 | ||
586 | /* Delete the old binding */ | |
587 | delete_item(po->pppoe_pa.sid,po->pppoe_pa.remote); | |
588 | ||
589 | if(po->pppoe_dev) | |
590 | dev_put(po->pppoe_dev); | |
591 | ||
592 | memset(sk_pppox(po) + 1, 0, | |
593 | sizeof(struct pppox_sock) - sizeof(struct sock)); | |
594 | ||
595 | sk->sk_state = PPPOX_NONE; | |
596 | } | |
597 | ||
598 | /* Don't re-bind if sid==0 */ | |
599 | if (sp->sa_addr.pppoe.sid != 0) { | |
600 | dev = dev_get_by_name(sp->sa_addr.pppoe.dev); | |
601 | ||
602 | error = -ENODEV; | |
603 | if (!dev) | |
604 | goto end; | |
605 | ||
606 | po->pppoe_dev = dev; | |
607 | ||
608 | if (!(dev->flags & IFF_UP)) | |
609 | goto err_put; | |
610 | ||
611 | memcpy(&po->pppoe_pa, | |
612 | &sp->sa_addr.pppoe, | |
613 | sizeof(struct pppoe_addr)); | |
614 | ||
615 | error = set_item(po); | |
616 | if (error < 0) | |
617 | goto err_put; | |
618 | ||
619 | po->chan.hdrlen = (sizeof(struct pppoe_hdr) + | |
620 | dev->hard_header_len); | |
621 | ||
622 | po->chan.private = sk; | |
623 | po->chan.ops = &pppoe_chan_ops; | |
624 | ||
625 | error = ppp_register_channel(&po->chan); | |
626 | if (error) | |
627 | goto err_put; | |
628 | ||
629 | sk->sk_state = PPPOX_CONNECTED; | |
630 | } | |
631 | ||
632 | po->num = sp->sa_addr.pppoe.sid; | |
633 | ||
634 | end: | |
635 | release_sock(sk); | |
636 | return error; | |
637 | err_put: | |
638 | if (po->pppoe_dev) { | |
639 | dev_put(po->pppoe_dev); | |
640 | po->pppoe_dev = NULL; | |
641 | } | |
642 | goto end; | |
643 | } | |
644 | ||
645 | ||
646 | static int pppoe_getname(struct socket *sock, struct sockaddr *uaddr, | |
647 | int *usockaddr_len, int peer) | |
648 | { | |
649 | int len = sizeof(struct sockaddr_pppox); | |
650 | struct sockaddr_pppox sp; | |
651 | ||
652 | sp.sa_family = AF_PPPOX; | |
653 | sp.sa_protocol = PX_PROTO_OE; | |
654 | memcpy(&sp.sa_addr.pppoe, &pppox_sk(sock->sk)->pppoe_pa, | |
655 | sizeof(struct pppoe_addr)); | |
656 | ||
657 | memcpy(uaddr, &sp, len); | |
658 | ||
659 | *usockaddr_len = len; | |
660 | ||
661 | return 0; | |
662 | } | |
663 | ||
664 | ||
665 | static int pppoe_ioctl(struct socket *sock, unsigned int cmd, | |
666 | unsigned long arg) | |
667 | { | |
668 | struct sock *sk = sock->sk; | |
669 | struct pppox_sock *po = pppox_sk(sk); | |
670 | int val = 0; | |
671 | int err = 0; | |
672 | ||
673 | switch (cmd) { | |
674 | case PPPIOCGMRU: | |
675 | err = -ENXIO; | |
676 | ||
677 | if (!(sk->sk_state & PPPOX_CONNECTED)) | |
678 | break; | |
679 | ||
680 | err = -EFAULT; | |
681 | if (put_user(po->pppoe_dev->mtu - | |
682 | sizeof(struct pppoe_hdr) - | |
683 | PPP_HDRLEN, | |
684 | (int __user *) arg)) | |
685 | break; | |
686 | err = 0; | |
687 | break; | |
688 | ||
689 | case PPPIOCSMRU: | |
690 | err = -ENXIO; | |
691 | if (!(sk->sk_state & PPPOX_CONNECTED)) | |
692 | break; | |
693 | ||
694 | err = -EFAULT; | |
695 | if (get_user(val,(int __user *) arg)) | |
696 | break; | |
697 | ||
698 | if (val < (po->pppoe_dev->mtu | |
699 | - sizeof(struct pppoe_hdr) | |
700 | - PPP_HDRLEN)) | |
701 | err = 0; | |
702 | else | |
703 | err = -EINVAL; | |
704 | break; | |
705 | ||
706 | case PPPIOCSFLAGS: | |
707 | err = -EFAULT; | |
708 | if (get_user(val, (int __user *) arg)) | |
709 | break; | |
710 | err = 0; | |
711 | break; | |
712 | ||
713 | case PPPOEIOCSFWD: | |
714 | { | |
715 | struct pppox_sock *relay_po; | |
716 | ||
717 | err = -EBUSY; | |
718 | if (sk->sk_state & (PPPOX_BOUND | PPPOX_ZOMBIE | PPPOX_DEAD)) | |
719 | break; | |
720 | ||
721 | err = -ENOTCONN; | |
722 | if (!(sk->sk_state & PPPOX_CONNECTED)) | |
723 | break; | |
724 | ||
725 | /* PPPoE address from the user specifies an outbound | |
726 | PPPoE address to which frames are forwarded to */ | |
727 | err = -EFAULT; | |
728 | if (copy_from_user(&po->pppoe_relay, | |
729 | (void __user *)arg, | |
730 | sizeof(struct sockaddr_pppox))) | |
731 | break; | |
732 | ||
733 | err = -EINVAL; | |
734 | if (po->pppoe_relay.sa_family != AF_PPPOX || | |
735 | po->pppoe_relay.sa_protocol!= PX_PROTO_OE) | |
736 | break; | |
737 | ||
738 | /* Check that the socket referenced by the address | |
739 | actually exists. */ | |
740 | relay_po = get_item_by_addr(&po->pppoe_relay); | |
741 | ||
742 | if (!relay_po) | |
743 | break; | |
744 | ||
745 | sock_put(sk_pppox(relay_po)); | |
746 | sk->sk_state |= PPPOX_RELAY; | |
747 | err = 0; | |
748 | break; | |
749 | } | |
750 | ||
751 | case PPPOEIOCDFWD: | |
752 | err = -EALREADY; | |
753 | if (!(sk->sk_state & PPPOX_RELAY)) | |
754 | break; | |
755 | ||
756 | sk->sk_state &= ~PPPOX_RELAY; | |
757 | err = 0; | |
758 | break; | |
759 | ||
760 | default:; | |
761 | }; | |
762 | ||
763 | return err; | |
764 | } | |
765 | ||
766 | ||
767 | static int pppoe_sendmsg(struct kiocb *iocb, struct socket *sock, | |
768 | struct msghdr *m, size_t total_len) | |
769 | { | |
770 | struct sk_buff *skb = NULL; | |
771 | struct sock *sk = sock->sk; | |
772 | struct pppox_sock *po = pppox_sk(sk); | |
773 | int error = 0; | |
774 | struct pppoe_hdr hdr; | |
775 | struct pppoe_hdr *ph; | |
776 | struct net_device *dev; | |
777 | char *start; | |
778 | ||
779 | if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED)) { | |
780 | error = -ENOTCONN; | |
781 | goto end; | |
782 | } | |
783 | ||
784 | hdr.ver = 1; | |
785 | hdr.type = 1; | |
786 | hdr.code = 0; | |
787 | hdr.sid = po->num; | |
788 | ||
789 | lock_sock(sk); | |
790 | ||
791 | dev = po->pppoe_dev; | |
792 | ||
793 | error = -EMSGSIZE; | |
794 | if (total_len > (dev->mtu + dev->hard_header_len)) | |
795 | goto end; | |
796 | ||
797 | ||
798 | skb = sock_wmalloc(sk, total_len + dev->hard_header_len + 32, | |
799 | 0, GFP_KERNEL); | |
800 | if (!skb) { | |
801 | error = -ENOMEM; | |
802 | goto end; | |
803 | } | |
804 | ||
805 | /* Reserve space for headers. */ | |
806 | skb_reserve(skb, dev->hard_header_len); | |
807 | skb->nh.raw = skb->data; | |
808 | ||
809 | skb->dev = dev; | |
810 | ||
811 | skb->priority = sk->sk_priority; | |
812 | skb->protocol = __constant_htons(ETH_P_PPP_SES); | |
813 | ||
814 | ph = (struct pppoe_hdr *) skb_put(skb, total_len + sizeof(struct pppoe_hdr)); | |
815 | start = (char *) &ph->tag[0]; | |
816 | ||
817 | error = memcpy_fromiovec(start, m->msg_iov, total_len); | |
818 | ||
819 | if (error < 0) { | |
820 | kfree_skb(skb); | |
821 | goto end; | |
822 | } | |
823 | ||
824 | error = total_len; | |
825 | dev->hard_header(skb, dev, ETH_P_PPP_SES, | |
826 | po->pppoe_pa.remote, NULL, total_len); | |
827 | ||
828 | memcpy(ph, &hdr, sizeof(struct pppoe_hdr)); | |
829 | ||
830 | ph->length = htons(total_len); | |
831 | ||
832 | dev_queue_xmit(skb); | |
833 | ||
834 | end: | |
835 | release_sock(sk); | |
836 | return error; | |
837 | } | |
838 | ||
839 | ||
840 | /************************************************************************ | |
841 | * | |
842 | * xmit function for internal use. | |
843 | * | |
844 | ***********************************************************************/ | |
845 | static int __pppoe_xmit(struct sock *sk, struct sk_buff *skb) | |
846 | { | |
847 | struct pppox_sock *po = pppox_sk(sk); | |
848 | struct net_device *dev = po->pppoe_dev; | |
849 | struct pppoe_hdr hdr; | |
850 | struct pppoe_hdr *ph; | |
851 | int headroom = skb_headroom(skb); | |
852 | int data_len = skb->len; | |
853 | struct sk_buff *skb2; | |
854 | ||
855 | if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED)) | |
856 | goto abort; | |
857 | ||
858 | hdr.ver = 1; | |
859 | hdr.type = 1; | |
860 | hdr.code = 0; | |
861 | hdr.sid = po->num; | |
862 | hdr.length = htons(skb->len); | |
863 | ||
864 | if (!dev) | |
865 | goto abort; | |
866 | ||
867 | /* Copy the skb if there is no space for the header. */ | |
868 | if (headroom < (sizeof(struct pppoe_hdr) + dev->hard_header_len)) { | |
869 | skb2 = dev_alloc_skb(32+skb->len + | |
870 | sizeof(struct pppoe_hdr) + | |
871 | dev->hard_header_len); | |
872 | ||
873 | if (skb2 == NULL) | |
874 | goto abort; | |
875 | ||
876 | skb_reserve(skb2, dev->hard_header_len + sizeof(struct pppoe_hdr)); | |
877 | memcpy(skb_put(skb2, skb->len), skb->data, skb->len); | |
878 | } else { | |
879 | /* Make a clone so as to not disturb the original skb, | |
880 | * give dev_queue_xmit something it can free. | |
881 | */ | |
882 | skb2 = skb_clone(skb, GFP_ATOMIC); | |
883 | } | |
884 | ||
885 | ph = (struct pppoe_hdr *) skb_push(skb2, sizeof(struct pppoe_hdr)); | |
886 | memcpy(ph, &hdr, sizeof(struct pppoe_hdr)); | |
887 | skb2->protocol = __constant_htons(ETH_P_PPP_SES); | |
888 | ||
889 | skb2->nh.raw = skb2->data; | |
890 | ||
891 | skb2->dev = dev; | |
892 | ||
893 | dev->hard_header(skb2, dev, ETH_P_PPP_SES, | |
894 | po->pppoe_pa.remote, NULL, data_len); | |
895 | ||
896 | /* We're transmitting skb2, and assuming that dev_queue_xmit | |
897 | * will free it. The generic ppp layer however, is expecting | |
898 | * that we give back 'skb' (not 'skb2') in case of failure, | |
899 | * but free it in case of success. | |
900 | */ | |
901 | ||
902 | if (dev_queue_xmit(skb2) < 0) | |
903 | goto abort; | |
904 | ||
905 | kfree_skb(skb); | |
906 | return 1; | |
907 | ||
908 | abort: | |
909 | return 0; | |
910 | } | |
911 | ||
912 | ||
913 | /************************************************************************ | |
914 | * | |
915 | * xmit function called by generic PPP driver | |
916 | * sends PPP frame over PPPoE socket | |
917 | * | |
918 | ***********************************************************************/ | |
919 | static int pppoe_xmit(struct ppp_channel *chan, struct sk_buff *skb) | |
920 | { | |
921 | struct sock *sk = (struct sock *) chan->private; | |
922 | return __pppoe_xmit(sk, skb); | |
923 | } | |
924 | ||
925 | ||
926 | static struct ppp_channel_ops pppoe_chan_ops = { | |
927 | .start_xmit = pppoe_xmit, | |
928 | }; | |
929 | ||
930 | static int pppoe_recvmsg(struct kiocb *iocb, struct socket *sock, | |
931 | struct msghdr *m, size_t total_len, int flags) | |
932 | { | |
933 | struct sock *sk = sock->sk; | |
934 | struct sk_buff *skb = NULL; | |
935 | int error = 0; | |
936 | int len; | |
937 | struct pppoe_hdr *ph = NULL; | |
938 | ||
939 | if (sk->sk_state & PPPOX_BOUND) { | |
940 | error = -EIO; | |
941 | goto end; | |
942 | } | |
943 | ||
944 | skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT, | |
945 | flags & MSG_DONTWAIT, &error); | |
946 | ||
947 | if (error < 0) { | |
948 | goto end; | |
949 | } | |
950 | ||
951 | m->msg_namelen = 0; | |
952 | ||
953 | if (skb) { | |
954 | error = 0; | |
955 | ph = (struct pppoe_hdr *) skb->nh.raw; | |
956 | len = ntohs(ph->length); | |
957 | ||
958 | error = memcpy_toiovec(m->msg_iov, (unsigned char *) &ph->tag[0], len); | |
959 | if (error < 0) | |
960 | goto do_skb_free; | |
961 | error = len; | |
962 | } | |
963 | ||
964 | do_skb_free: | |
965 | if (skb) | |
966 | kfree_skb(skb); | |
967 | end: | |
968 | return error; | |
969 | } | |
970 | ||
971 | #ifdef CONFIG_PROC_FS | |
972 | static int pppoe_seq_show(struct seq_file *seq, void *v) | |
973 | { | |
974 | struct pppox_sock *po; | |
975 | char *dev_name; | |
976 | ||
977 | if (v == SEQ_START_TOKEN) { | |
978 | seq_puts(seq, "Id Address Device\n"); | |
979 | goto out; | |
980 | } | |
981 | ||
982 | po = v; | |
983 | dev_name = po->pppoe_pa.dev; | |
984 | ||
985 | seq_printf(seq, "%08X %02X:%02X:%02X:%02X:%02X:%02X %8s\n", | |
986 | po->pppoe_pa.sid, | |
987 | po->pppoe_pa.remote[0], po->pppoe_pa.remote[1], | |
988 | po->pppoe_pa.remote[2], po->pppoe_pa.remote[3], | |
989 | po->pppoe_pa.remote[4], po->pppoe_pa.remote[5], dev_name); | |
990 | out: | |
991 | return 0; | |
992 | } | |
993 | ||
994 | static __inline__ struct pppox_sock *pppoe_get_idx(loff_t pos) | |
995 | { | |
996 | struct pppox_sock *po = NULL; | |
997 | int i = 0; | |
998 | ||
999 | for (; i < PPPOE_HASH_SIZE; i++) { | |
1000 | po = item_hash_table[i]; | |
1001 | while (po) { | |
1002 | if (!pos--) | |
1003 | goto out; | |
1004 | po = po->next; | |
1005 | } | |
1006 | } | |
1007 | out: | |
1008 | return po; | |
1009 | } | |
1010 | ||
1011 | static void *pppoe_seq_start(struct seq_file *seq, loff_t *pos) | |
1012 | { | |
1013 | loff_t l = *pos; | |
1014 | ||
1015 | read_lock_bh(&pppoe_hash_lock); | |
1016 | return l ? pppoe_get_idx(--l) : SEQ_START_TOKEN; | |
1017 | } | |
1018 | ||
1019 | static void *pppoe_seq_next(struct seq_file *seq, void *v, loff_t *pos) | |
1020 | { | |
1021 | struct pppox_sock *po; | |
1022 | ||
1023 | ++*pos; | |
1024 | if (v == SEQ_START_TOKEN) { | |
1025 | po = pppoe_get_idx(0); | |
1026 | goto out; | |
1027 | } | |
1028 | po = v; | |
1029 | if (po->next) | |
1030 | po = po->next; | |
1031 | else { | |
1032 | int hash = hash_item(po->pppoe_pa.sid, po->pppoe_pa.remote); | |
1033 | ||
1034 | while (++hash < PPPOE_HASH_SIZE) { | |
1035 | po = item_hash_table[hash]; | |
1036 | if (po) | |
1037 | break; | |
1038 | } | |
1039 | } | |
1040 | out: | |
1041 | return po; | |
1042 | } | |
1043 | ||
1044 | static void pppoe_seq_stop(struct seq_file *seq, void *v) | |
1045 | { | |
1046 | read_unlock_bh(&pppoe_hash_lock); | |
1047 | } | |
1048 | ||
1049 | static struct seq_operations pppoe_seq_ops = { | |
1050 | .start = pppoe_seq_start, | |
1051 | .next = pppoe_seq_next, | |
1052 | .stop = pppoe_seq_stop, | |
1053 | .show = pppoe_seq_show, | |
1054 | }; | |
1055 | ||
1056 | static int pppoe_seq_open(struct inode *inode, struct file *file) | |
1057 | { | |
1058 | return seq_open(file, &pppoe_seq_ops); | |
1059 | } | |
1060 | ||
1061 | static struct file_operations pppoe_seq_fops = { | |
1062 | .owner = THIS_MODULE, | |
1063 | .open = pppoe_seq_open, | |
1064 | .read = seq_read, | |
1065 | .llseek = seq_lseek, | |
1066 | .release = seq_release, | |
1067 | }; | |
1068 | ||
1069 | static int __init pppoe_proc_init(void) | |
1070 | { | |
1071 | struct proc_dir_entry *p; | |
1072 | ||
1073 | p = create_proc_entry("pppoe", S_IRUGO, proc_net); | |
1074 | if (!p) | |
1075 | return -ENOMEM; | |
1076 | ||
1077 | p->proc_fops = &pppoe_seq_fops; | |
1078 | return 0; | |
1079 | } | |
1080 | #else /* CONFIG_PROC_FS */ | |
1081 | static inline int pppoe_proc_init(void) { return 0; } | |
1082 | #endif /* CONFIG_PROC_FS */ | |
1083 | ||
1084 | /* ->ioctl are set at pppox_create */ | |
1085 | ||
1086 | static struct proto_ops pppoe_ops = { | |
1087 | .family = AF_PPPOX, | |
1088 | .owner = THIS_MODULE, | |
1089 | .release = pppoe_release, | |
1090 | .bind = sock_no_bind, | |
1091 | .connect = pppoe_connect, | |
1092 | .socketpair = sock_no_socketpair, | |
1093 | .accept = sock_no_accept, | |
1094 | .getname = pppoe_getname, | |
1095 | .poll = datagram_poll, | |
1096 | .listen = sock_no_listen, | |
1097 | .shutdown = sock_no_shutdown, | |
1098 | .setsockopt = sock_no_setsockopt, | |
1099 | .getsockopt = sock_no_getsockopt, | |
1100 | .sendmsg = pppoe_sendmsg, | |
1101 | .recvmsg = pppoe_recvmsg, | |
1102 | .mmap = sock_no_mmap | |
1103 | }; | |
1104 | ||
1105 | static struct pppox_proto pppoe_proto = { | |
1106 | .create = pppoe_create, | |
1107 | .ioctl = pppoe_ioctl, | |
1108 | .owner = THIS_MODULE, | |
1109 | }; | |
1110 | ||
1111 | ||
1112 | static int __init pppoe_init(void) | |
1113 | { | |
1114 | int err = proto_register(&pppoe_sk_proto, 0); | |
1115 | ||
1116 | if (err) | |
1117 | goto out; | |
1118 | ||
1119 | err = register_pppox_proto(PX_PROTO_OE, &pppoe_proto); | |
1120 | if (err) | |
1121 | goto out_unregister_pppoe_proto; | |
1122 | ||
1123 | err = pppoe_proc_init(); | |
1124 | if (err) | |
1125 | goto out_unregister_pppox_proto; | |
1126 | ||
1127 | dev_add_pack(&pppoes_ptype); | |
1128 | dev_add_pack(&pppoed_ptype); | |
1129 | register_netdevice_notifier(&pppoe_notifier); | |
1130 | out: | |
1131 | return err; | |
1132 | out_unregister_pppox_proto: | |
1133 | unregister_pppox_proto(PX_PROTO_OE); | |
1134 | out_unregister_pppoe_proto: | |
1135 | proto_unregister(&pppoe_sk_proto); | |
1136 | goto out; | |
1137 | } | |
1138 | ||
1139 | static void __exit pppoe_exit(void) | |
1140 | { | |
1141 | unregister_pppox_proto(PX_PROTO_OE); | |
1142 | dev_remove_pack(&pppoes_ptype); | |
1143 | dev_remove_pack(&pppoed_ptype); | |
1144 | unregister_netdevice_notifier(&pppoe_notifier); | |
1145 | remove_proc_entry("pppoe", proc_net); | |
1146 | proto_unregister(&pppoe_sk_proto); | |
1147 | } | |
1148 | ||
1149 | module_init(pppoe_init); | |
1150 | module_exit(pppoe_exit); | |
1151 | ||
1152 | MODULE_AUTHOR("Michal Ostrowski <mostrows@speakeasy.net>"); | |
1153 | MODULE_DESCRIPTION("PPP over Ethernet driver"); | |
1154 | MODULE_LICENSE("GPL"); | |
1155 | MODULE_ALIAS_NETPROTO(PF_PPPOX); |