Chia-chi Yeh | 36d0074 | 2009-05-08 04:02:40 +0800 | [diff] [blame^] | 1 | /* drivers/net/pppolac.c |
| 2 | * |
| 3 | * Driver for PPP on L2TP Access Concentrator / PPPoLAC Socket (RFC 2661) |
| 4 | * |
| 5 | * Copyright (C) 2009 Google, Inc. |
| 6 | * Author: Chia-chi Yeh <chiachi@android.com> |
| 7 | * |
| 8 | * This software is licensed under the terms of the GNU General Public |
| 9 | * License version 2, as published by the Free Software Foundation, and |
| 10 | * may be copied, distributed, and modified under those terms. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | */ |
| 17 | |
| 18 | /* This driver handles L2TP data packets between a UDP socket and a PPP channel. |
| 19 | * To keep things simple, only one session per socket is permitted. Packets are |
| 20 | * sent via the socket, so it must keep connected to the same address. One must |
| 21 | * not set sequencing in ICCN but let LNS controll it. Currently this driver |
| 22 | * only works on IPv4 due to the lack of UDP encapsulation support in IPv6. */ |
| 23 | |
| 24 | #include <linux/module.h> |
| 25 | #include <linux/skbuff.h> |
| 26 | #include <linux/file.h> |
| 27 | #include <linux/net.h> |
| 28 | #include <linux/udp.h> |
| 29 | #include <linux/ppp_defs.h> |
| 30 | #include <linux/if_ppp.h> |
| 31 | #include <linux/if_pppox.h> |
| 32 | #include <linux/ppp_channel.h> |
| 33 | #include <net/tcp_states.h> |
| 34 | |
| 35 | #define L2TP_CONTROL_MASK 0x80 |
| 36 | #define L2TP_VERSION_MASK 0x0F |
| 37 | #define L2TP_VERSION 0x02 |
| 38 | #define L2TP_LENGTH_MASK 0x40 |
| 39 | #define L2TP_OFFSET_MASK 0x02 |
| 40 | #define L2TP_SEQUENCE_MASK 0x08 |
| 41 | |
| 42 | #define PPP_ADDR 0xFF |
| 43 | #define PPP_CTRL 0x03 |
| 44 | |
| 45 | union unaligned { |
| 46 | __u32 u32; |
| 47 | } __attribute__((packed)); |
| 48 | |
| 49 | static inline union unaligned *unaligned(void *ptr) |
| 50 | { |
| 51 | return (union unaligned *)ptr; |
| 52 | } |
| 53 | |
| 54 | static int pppolac_recv(struct sock *sk_udp, struct sk_buff *skb) |
| 55 | { |
| 56 | struct sock *sk; |
| 57 | struct pppolac_opt *opt; |
| 58 | __u8 bits; |
| 59 | __u8 *ptr; |
| 60 | |
| 61 | /* Drop the packet if it is too short. */ |
| 62 | if (skb->len < sizeof(struct udphdr) + 6) |
| 63 | goto drop; |
| 64 | |
| 65 | /* Put it back if it is a control packet. */ |
| 66 | if (skb->data[sizeof(struct udphdr)] & L2TP_CONTROL_MASK) |
| 67 | return 1; |
| 68 | |
| 69 | /* Now the packet is ours. Skip UDP header. */ |
| 70 | skb_pull(skb, sizeof(struct udphdr)); |
| 71 | |
| 72 | /* Check the version. */ |
| 73 | if ((skb->data[1] & L2TP_VERSION_MASK) != L2TP_VERSION) |
| 74 | goto drop; |
| 75 | bits = skb->data[0]; |
| 76 | ptr = &skb->data[2]; |
| 77 | |
| 78 | /* Check the length if it is present. */ |
| 79 | if (bits & L2TP_LENGTH_MASK) { |
| 80 | if ((ptr[0] << 8 | ptr[1]) != skb->len) |
| 81 | goto drop; |
| 82 | ptr += 2; |
| 83 | } |
| 84 | |
| 85 | /* Skip all fields including optional ones. */ |
| 86 | if (!skb_pull(skb, 6 + (bits & L2TP_SEQUENCE_MASK ? 4 : 0) + |
| 87 | (bits & L2TP_LENGTH_MASK ? 2 : 0) + |
| 88 | (bits & L2TP_OFFSET_MASK ? 2 : 0))) |
| 89 | goto drop; |
| 90 | |
| 91 | /* Skip the offset padding if it is present. */ |
| 92 | if (bits & L2TP_OFFSET_MASK && |
| 93 | !skb_pull(skb, skb->data[-2] << 8 | skb->data[-1])) |
| 94 | goto drop; |
| 95 | |
| 96 | /* Now ptr is pointing to the tunnel and skb is pointing to the payload. |
| 97 | * We have to lock sk_udp to prevent sk from being closed. */ |
| 98 | lock_sock(sk_udp); |
| 99 | sk = sk_udp->sk_user_data; |
| 100 | if (!sk) { |
| 101 | release_sock(sk_udp); |
| 102 | goto drop; |
| 103 | } |
| 104 | sock_hold(sk); |
| 105 | release_sock(sk_udp); |
| 106 | opt = &pppox_sk(sk)->proto.lac; |
| 107 | |
| 108 | /* Check the tunnel and the session. */ |
| 109 | if (unaligned(ptr)->u32 != opt->local) { |
| 110 | sock_put(sk); |
| 111 | goto drop; |
| 112 | } |
| 113 | |
| 114 | /* Check the sequence if it is present. According to RFC 2661 page 10 |
| 115 | * and 43, the only thing to do is updating opt->sequencing. */ |
| 116 | opt->sequencing = bits & L2TP_SEQUENCE_MASK; |
| 117 | |
| 118 | /* Skip PPP address and control if they are present. */ |
| 119 | if (skb->len >= 2 && skb->data[0] == PPP_ADDR && |
| 120 | skb->data[1] == PPP_CTRL) |
| 121 | skb_pull(skb, 2); |
| 122 | |
| 123 | /* Fix PPP protocol if it is compressed. */ |
| 124 | if (skb->len >= 1 && skb->data[0] & 1) |
| 125 | skb_push(skb, 1)[0] = 0; |
| 126 | |
| 127 | /* Finally, deliver the packet to PPP channel. We have to lock sk to |
| 128 | * prevent another thread from calling pppox_unbind_sock(). */ |
| 129 | skb_orphan(skb); |
| 130 | lock_sock(sk); |
| 131 | ppp_input(&pppox_sk(sk)->chan, skb); |
| 132 | release_sock(sk); |
| 133 | sock_put(sk); |
| 134 | return 0; |
| 135 | |
| 136 | drop: |
| 137 | kfree_skb(skb); |
| 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | static int pppolac_xmit(struct ppp_channel *chan, struct sk_buff *skb) |
| 142 | { |
| 143 | struct sock *sk_udp = (struct sock *)chan->private; |
| 144 | struct pppolac_opt *opt = &pppox_sk(sk_udp->sk_user_data)->proto.lac; |
| 145 | struct msghdr msg = {.msg_flags = MSG_NOSIGNAL | MSG_DONTWAIT}; |
| 146 | struct kvec iov; |
| 147 | |
| 148 | /* Install PPP address and control. */ |
| 149 | skb_push(skb, 2); |
| 150 | skb->data[0] = PPP_ADDR; |
| 151 | skb->data[1] = PPP_CTRL; |
| 152 | |
| 153 | /* Install L2TP header. */ |
| 154 | if (opt->sequencing) { |
| 155 | skb_push(skb, 10); |
| 156 | skb->data[0] = L2TP_SEQUENCE_MASK; |
| 157 | skb->data[6] = opt->sequence >> 8; |
| 158 | skb->data[7] = opt->sequence; |
| 159 | skb->data[8] = 0; |
| 160 | skb->data[9] = 0; |
| 161 | opt->sequence++; |
| 162 | } else { |
| 163 | skb_push(skb, 6); |
| 164 | skb->data[0] = 0; |
| 165 | } |
| 166 | skb->data[1] = L2TP_VERSION; |
| 167 | unaligned(&skb->data[2])->u32 = opt->remote; |
| 168 | |
| 169 | /* Now send the packet via UDP socket. */ |
| 170 | iov.iov_base = skb->data; |
| 171 | iov.iov_len = skb->len; |
| 172 | kernel_sendmsg(sk_udp->sk_socket, &msg, &iov, 1, skb->len); |
| 173 | kfree_skb(skb); |
| 174 | return 1; |
| 175 | } |
| 176 | |
| 177 | /******************************************************************************/ |
| 178 | |
| 179 | static struct ppp_channel_ops pppolac_channel_ops = { |
| 180 | .start_xmit = pppolac_xmit, |
| 181 | }; |
| 182 | |
| 183 | static int pppolac_connect(struct socket *sock, struct sockaddr *useraddr, |
| 184 | int addrlen, int flags) |
| 185 | { |
| 186 | struct sock *sk = sock->sk; |
| 187 | struct pppox_sock *po = pppox_sk(sk); |
| 188 | struct sockaddr_pppolac *addr = (struct sockaddr_pppolac *)useraddr; |
| 189 | struct socket *sock_udp = NULL; |
| 190 | struct sock *sk_udp; |
| 191 | int error; |
| 192 | |
| 193 | if (addrlen != sizeof(struct sockaddr_pppolac) || |
| 194 | !addr->local.tunnel || !addr->local.session || |
| 195 | !addr->remote.tunnel || !addr->remote.session) { |
| 196 | return -EINVAL; |
| 197 | } |
| 198 | |
| 199 | lock_sock(sk); |
| 200 | error = -EALREADY; |
| 201 | if (sk->sk_state != PPPOX_NONE) |
| 202 | goto out; |
| 203 | |
| 204 | sock_udp = sockfd_lookup(addr->udp_socket, &error); |
| 205 | if (!sock_udp) |
| 206 | goto out; |
| 207 | sk_udp = sock_udp->sk; |
| 208 | lock_sock(sk_udp); |
| 209 | |
| 210 | /* Remove this check when IPv6 supports UDP encapsulation. */ |
| 211 | error = -EAFNOSUPPORT; |
| 212 | if (sk_udp->sk_family != AF_INET) |
| 213 | goto out; |
| 214 | error = -EPROTONOSUPPORT; |
| 215 | if (sk_udp->sk_protocol != IPPROTO_UDP) |
| 216 | goto out; |
| 217 | error = -EDESTADDRREQ; |
| 218 | if (sk_udp->sk_state != TCP_ESTABLISHED) |
| 219 | goto out; |
| 220 | error = -EBUSY; |
| 221 | if (udp_sk(sk_udp)->encap_type || sk_udp->sk_user_data) |
| 222 | goto out; |
| 223 | |
| 224 | po->chan.hdrlen = 12; |
| 225 | po->chan.private = sk_udp; |
| 226 | po->chan.ops = &pppolac_channel_ops; |
| 227 | po->chan.mtu = PPP_MTU - 80; |
| 228 | po->proto.lac.local = unaligned(&addr->local)->u32; |
| 229 | po->proto.lac.remote = unaligned(&addr->remote)->u32; |
| 230 | |
| 231 | error = ppp_register_channel(&po->chan); |
| 232 | if (error) |
| 233 | goto out; |
| 234 | |
| 235 | sk->sk_state = PPPOX_CONNECTED; |
| 236 | udp_sk(sk_udp)->encap_type = UDP_ENCAP_L2TPINUDP; |
| 237 | udp_sk(sk_udp)->encap_rcv = pppolac_recv; |
| 238 | sk_udp->sk_user_data = sk; |
| 239 | |
| 240 | out: |
| 241 | if (sock_udp) { |
| 242 | release_sock(sk_udp); |
| 243 | if (error) |
| 244 | sockfd_put(sock_udp); |
| 245 | } |
| 246 | release_sock(sk); |
| 247 | return error; |
| 248 | } |
| 249 | |
| 250 | static int pppolac_release(struct socket *sock) |
| 251 | { |
| 252 | struct sock *sk = sock->sk; |
| 253 | |
| 254 | if (!sk) |
| 255 | return 0; |
| 256 | |
| 257 | lock_sock(sk); |
| 258 | if (sock_flag(sk, SOCK_DEAD)) { |
| 259 | release_sock(sk); |
| 260 | return -EBADF; |
| 261 | } |
| 262 | |
| 263 | if (sk->sk_state != PPPOX_NONE) { |
| 264 | struct sock *sk_udp = (struct sock *)pppox_sk(sk)->chan.private; |
| 265 | lock_sock(sk_udp); |
| 266 | |
| 267 | pppox_unbind_sock(sk); |
| 268 | sk_udp->sk_user_data = NULL; |
| 269 | udp_sk(sk_udp)->encap_type = 0; |
| 270 | udp_sk(sk_udp)->encap_rcv = NULL; |
| 271 | |
| 272 | release_sock(sk_udp); |
| 273 | sockfd_put(sk_udp->sk_socket); |
| 274 | } |
| 275 | |
| 276 | sock_orphan(sk); |
| 277 | sock->sk = NULL; |
| 278 | release_sock(sk); |
| 279 | sock_put(sk); |
| 280 | return 0; |
| 281 | } |
| 282 | |
| 283 | /******************************************************************************/ |
| 284 | |
| 285 | static struct proto pppolac_proto = { |
| 286 | .name = "PPPOLAC", |
| 287 | .owner = THIS_MODULE, |
| 288 | .obj_size = sizeof(struct pppox_sock), |
| 289 | }; |
| 290 | |
| 291 | static struct proto_ops pppolac_proto_ops = { |
| 292 | .family = PF_PPPOX, |
| 293 | .owner = THIS_MODULE, |
| 294 | .release = pppolac_release, |
| 295 | .bind = sock_no_bind, |
| 296 | .connect = pppolac_connect, |
| 297 | .socketpair = sock_no_socketpair, |
| 298 | .accept = sock_no_accept, |
| 299 | .getname = sock_no_getname, |
| 300 | .poll = sock_no_poll, |
| 301 | .ioctl = pppox_ioctl, |
| 302 | .listen = sock_no_listen, |
| 303 | .shutdown = sock_no_shutdown, |
| 304 | .setsockopt = sock_no_setsockopt, |
| 305 | .getsockopt = sock_no_getsockopt, |
| 306 | .sendmsg = sock_no_sendmsg, |
| 307 | .recvmsg = sock_no_recvmsg, |
| 308 | .mmap = sock_no_mmap, |
| 309 | }; |
| 310 | |
| 311 | static int pppolac_create(struct net *net, struct socket *sock) |
| 312 | { |
| 313 | struct sock *sk; |
| 314 | |
| 315 | sk = sk_alloc(net, PF_PPPOX, GFP_KERNEL, &pppolac_proto); |
| 316 | if (!sk) |
| 317 | return -ENOMEM; |
| 318 | |
| 319 | sock_init_data(sock, sk); |
| 320 | sock->state = SS_UNCONNECTED; |
| 321 | sock->ops = &pppolac_proto_ops; |
| 322 | sk->sk_protocol = PX_PROTO_OLAC; |
| 323 | sk->sk_state = PPPOX_NONE; |
| 324 | return 0; |
| 325 | } |
| 326 | |
| 327 | /******************************************************************************/ |
| 328 | |
| 329 | static struct pppox_proto pppolac_pppox_proto = { |
| 330 | .create = pppolac_create, |
| 331 | .owner = THIS_MODULE, |
| 332 | }; |
| 333 | |
| 334 | static int __init pppolac_init(void) |
| 335 | { |
| 336 | int error; |
| 337 | |
| 338 | error = proto_register(&pppolac_proto, 0); |
| 339 | if (error) |
| 340 | return error; |
| 341 | |
| 342 | error = register_pppox_proto(PX_PROTO_OLAC, &pppolac_pppox_proto); |
| 343 | if (error) |
| 344 | proto_unregister(&pppolac_proto); |
| 345 | return error; |
| 346 | } |
| 347 | |
| 348 | static void __exit pppolac_exit(void) |
| 349 | { |
| 350 | unregister_pppox_proto(PX_PROTO_OLAC); |
| 351 | proto_unregister(&pppolac_proto); |
| 352 | } |
| 353 | |
| 354 | module_init(pppolac_init); |
| 355 | module_exit(pppolac_exit); |
| 356 | |
| 357 | MODULE_DESCRIPTION("PPP on L2TP Access Concentrator (PPPoLAC)"); |
| 358 | MODULE_AUTHOR("Chia-chi Yeh <chiachi@android.com>"); |
| 359 | MODULE_LICENSE("GPL"); |