Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /** -*- linux-c -*- *********************************************************** |
| 2 | * Linux PPP over X/Ethernet (PPPoX/PPPoE) Sockets |
| 3 | * |
| 4 | * PPPoX --- Generic PPP encapsulation socket family |
| 5 | * PPPoE --- PPP over Ethernet (RFC 2516) |
| 6 | * |
| 7 | * |
| 8 | * Version: 0.5.2 |
| 9 | * |
| 10 | * Author: Michal Ostrowski <mostrows@speakeasy.net> |
| 11 | * |
| 12 | * 051000 : Initialization cleanup |
| 13 | * |
| 14 | * License: |
| 15 | * This program is free software; you can redistribute it and/or |
| 16 | * modify it under the terms of the GNU General Public License |
| 17 | * as published by the Free Software Foundation; either version |
| 18 | * 2 of the License, or (at your option) any later version. |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | #include <linux/string.h> |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/kernel.h> |
| 25 | #include <linux/slab.h> |
| 26 | #include <linux/errno.h> |
| 27 | #include <linux/netdevice.h> |
| 28 | #include <linux/net.h> |
| 29 | #include <linux/init.h> |
| 30 | #include <linux/if_pppox.h> |
| 31 | #include <linux/ppp_defs.h> |
| 32 | #include <linux/if_ppp.h> |
| 33 | #include <linux/ppp_channel.h> |
James Chapman | 65def81 | 2007-04-30 00:21:02 -0700 | [diff] [blame] | 34 | #include <linux/kmod.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
| 36 | #include <net/sock.h> |
| 37 | |
| 38 | #include <asm/uaccess.h> |
| 39 | |
| 40 | static struct pppox_proto *pppox_protos[PX_MAX_PROTO + 1]; |
| 41 | |
| 42 | int register_pppox_proto(int proto_num, struct pppox_proto *pp) |
| 43 | { |
| 44 | if (proto_num < 0 || proto_num > PX_MAX_PROTO) |
| 45 | return -EINVAL; |
| 46 | if (pppox_protos[proto_num]) |
| 47 | return -EALREADY; |
| 48 | pppox_protos[proto_num] = pp; |
| 49 | return 0; |
| 50 | } |
| 51 | |
| 52 | void unregister_pppox_proto(int proto_num) |
| 53 | { |
| 54 | if (proto_num >= 0 && proto_num <= PX_MAX_PROTO) |
| 55 | pppox_protos[proto_num] = NULL; |
| 56 | } |
| 57 | |
| 58 | void pppox_unbind_sock(struct sock *sk) |
| 59 | { |
| 60 | /* Clear connection to ppp device, if attached. */ |
| 61 | |
Florian Zumbiehl | 202a03a | 2007-04-20 16:58:14 -0700 | [diff] [blame] | 62 | if (sk->sk_state & (PPPOX_BOUND | PPPOX_CONNECTED | PPPOX_ZOMBIE)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | ppp_unregister_channel(&pppox_sk(sk)->chan); |
| 64 | sk->sk_state = PPPOX_DEAD; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | EXPORT_SYMBOL(register_pppox_proto); |
| 69 | EXPORT_SYMBOL(unregister_pppox_proto); |
| 70 | EXPORT_SYMBOL(pppox_unbind_sock); |
| 71 | |
David S. Miller | 17ba15f | 2005-12-27 20:57:40 -0800 | [diff] [blame] | 72 | int pppox_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | { |
| 74 | struct sock *sk = sock->sk; |
| 75 | struct pppox_sock *po = pppox_sk(sk); |
Florian Zumbiehl | 86c1dcf | 2007-07-30 17:48:23 -0700 | [diff] [blame] | 76 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | |
| 78 | lock_sock(sk); |
| 79 | |
| 80 | switch (cmd) { |
| 81 | case PPPIOCGCHAN: { |
| 82 | int index; |
| 83 | rc = -ENOTCONN; |
| 84 | if (!(sk->sk_state & PPPOX_CONNECTED)) |
| 85 | break; |
| 86 | |
| 87 | rc = -EINVAL; |
| 88 | index = ppp_channel_index(&po->chan); |
| 89 | if (put_user(index , (int __user *) arg)) |
| 90 | break; |
| 91 | |
| 92 | rc = 0; |
| 93 | sk->sk_state |= PPPOX_BOUND; |
| 94 | break; |
| 95 | } |
| 96 | default: |
Florian Zumbiehl | 86c1dcf | 2007-07-30 17:48:23 -0700 | [diff] [blame] | 97 | rc = pppox_protos[sk->sk_protocol]->ioctl ? |
| 98 | pppox_protos[sk->sk_protocol]->ioctl(sock, cmd, arg) : -ENOTTY; |
| 99 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | |
| 101 | release_sock(sk); |
| 102 | return rc; |
| 103 | } |
| 104 | |
David S. Miller | 17ba15f | 2005-12-27 20:57:40 -0800 | [diff] [blame] | 105 | EXPORT_SYMBOL(pppox_ioctl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | |
Eric W. Biederman | 1b8d7ae | 2007-10-08 23:24:22 -0700 | [diff] [blame] | 107 | static int pppox_create(struct net *net, struct socket *sock, int protocol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | { |
| 109 | int rc = -EPROTOTYPE; |
| 110 | |
Eric W. Biederman | 1b8d7ae | 2007-10-08 23:24:22 -0700 | [diff] [blame] | 111 | if (net != &init_net) |
| 112 | return -EAFNOSUPPORT; |
| 113 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | if (protocol < 0 || protocol > PX_MAX_PROTO) |
| 115 | goto out; |
| 116 | |
| 117 | rc = -EPROTONOSUPPORT; |
James Chapman | 65def81 | 2007-04-30 00:21:02 -0700 | [diff] [blame] | 118 | #ifdef CONFIG_KMOD |
| 119 | if (!pppox_protos[protocol]) { |
| 120 | char buffer[32]; |
| 121 | sprintf(buffer, "pppox-proto-%d", protocol); |
| 122 | request_module(buffer); |
| 123 | } |
| 124 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | if (!pppox_protos[protocol] || |
| 126 | !try_module_get(pppox_protos[protocol]->owner)) |
| 127 | goto out; |
| 128 | |
Eric W. Biederman | 1b8d7ae | 2007-10-08 23:24:22 -0700 | [diff] [blame] | 129 | rc = pppox_protos[protocol]->create(net, sock); |
David S. Miller | 17ba15f | 2005-12-27 20:57:40 -0800 | [diff] [blame] | 130 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | module_put(pppox_protos[protocol]->owner); |
| 132 | out: |
| 133 | return rc; |
| 134 | } |
| 135 | |
| 136 | static struct net_proto_family pppox_proto_family = { |
| 137 | .family = PF_PPPOX, |
| 138 | .create = pppox_create, |
| 139 | .owner = THIS_MODULE, |
| 140 | }; |
| 141 | |
| 142 | static int __init pppox_init(void) |
| 143 | { |
| 144 | return sock_register(&pppox_proto_family); |
| 145 | } |
| 146 | |
| 147 | static void __exit pppox_exit(void) |
| 148 | { |
| 149 | sock_unregister(PF_PPPOX); |
| 150 | } |
| 151 | |
| 152 | module_init(pppox_init); |
| 153 | module_exit(pppox_exit); |
| 154 | |
| 155 | MODULE_AUTHOR("Michal Ostrowski <mostrows@speakeasy.net>"); |
| 156 | MODULE_DESCRIPTION("PPP over Ethernet driver (generic socket layer)"); |
| 157 | MODULE_LICENSE("GPL"); |