1 /* drivers/net/pppolac.c
3 * Driver for PPP on L2TP Access Concentrator / PPPoLAC Socket (RFC 2661)
5 * Copyright (C) 2009 Google, Inc.
6 * Author: Chia-chi Yeh <chiachi@android.com>
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.
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.
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. */
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>
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
47 } __attribute__((packed));
49 static inline union unaligned *unaligned(void *ptr)
51 return (union unaligned *)ptr;
54 static int pppolac_recv(struct sock *sk_udp, struct sk_buff *skb)
57 struct pppolac_opt *opt;
61 /* Drop the packet if it is too short. */
62 if (skb->len < sizeof(struct udphdr) + 6)
65 /* Put it back if it is a control packet. */
66 if (skb->data[sizeof(struct udphdr)] & L2TP_CONTROL_MASK)
69 /* Now the packet is ours. Skip UDP header. */
70 skb_pull(skb, sizeof(struct udphdr));
72 /* Check the version. */
73 if ((skb->data[1] & L2TP_VERSION_MASK) != L2TP_VERSION)
78 /* Check the length if it is present. */
79 if (bits & L2TP_LENGTH_MASK) {
80 if ((ptr[0] << 8 | ptr[1]) != skb->len)
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)))
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]))
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. */
99 sk = sk_udp->sk_user_data;
101 release_sock(sk_udp);
105 release_sock(sk_udp);
106 opt = &pppox_sk(sk)->proto.lac;
108 /* Check the tunnel and the session. */
109 if (unaligned(ptr)->u32 != opt->local) {
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;
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)
123 /* Fix PPP protocol if it is compressed. */
124 if (skb->len >= 1 && skb->data[0] & 1)
125 skb_push(skb, 1)[0] = 0;
127 /* Finally, deliver the packet to PPP channel. We have to lock sk to
128 * prevent another thread from calling pppox_unbind_sock(). */
131 ppp_input(&pppox_sk(sk)->chan, skb);
141 static int pppolac_xmit(struct ppp_channel *chan, struct sk_buff *skb)
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};
148 /* Install PPP address and control. */
150 skb->data[0] = PPP_ADDR;
151 skb->data[1] = PPP_CTRL;
153 /* Install L2TP header. */
154 if (opt->sequencing) {
156 skb->data[0] = L2TP_SEQUENCE_MASK;
157 skb->data[6] = opt->sequence >> 8;
158 skb->data[7] = opt->sequence;
166 skb->data[1] = L2TP_VERSION;
167 unaligned(&skb->data[2])->u32 = opt->remote;
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);
177 /******************************************************************************/
179 static struct ppp_channel_ops pppolac_channel_ops = {
180 .start_xmit = pppolac_xmit,
183 static int pppolac_connect(struct socket *sock, struct sockaddr *useraddr,
184 int addrlen, int flags)
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;
193 if (addrlen != sizeof(struct sockaddr_pppolac) ||
194 !addr->local.tunnel || !addr->local.session ||
195 !addr->remote.tunnel || !addr->remote.session) {
201 if (sk->sk_state != PPPOX_NONE)
204 sock_udp = sockfd_lookup(addr->udp_socket, &error);
207 sk_udp = sock_udp->sk;
210 /* Remove this check when IPv6 supports UDP encapsulation. */
211 error = -EAFNOSUPPORT;
212 if (sk_udp->sk_family != AF_INET)
214 error = -EPROTONOSUPPORT;
215 if (sk_udp->sk_protocol != IPPROTO_UDP)
217 error = -EDESTADDRREQ;
218 if (sk_udp->sk_state != TCP_ESTABLISHED)
221 if (udp_sk(sk_udp)->encap_type || sk_udp->sk_user_data)
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;
231 error = ppp_register_channel(&po->chan);
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;
242 release_sock(sk_udp);
244 sockfd_put(sock_udp);
250 static int pppolac_release(struct socket *sock)
252 struct sock *sk = sock->sk;
258 if (sock_flag(sk, SOCK_DEAD)) {
263 if (sk->sk_state != PPPOX_NONE) {
264 struct sock *sk_udp = (struct sock *)pppox_sk(sk)->chan.private;
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;
272 release_sock(sk_udp);
273 sockfd_put(sk_udp->sk_socket);
283 /******************************************************************************/
285 static struct proto pppolac_proto = {
287 .owner = THIS_MODULE,
288 .obj_size = sizeof(struct pppox_sock),
291 static struct proto_ops pppolac_proto_ops = {
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,
311 static int pppolac_create(struct net *net, struct socket *sock)
315 sk = sk_alloc(net, PF_PPPOX, GFP_KERNEL, &pppolac_proto);
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;
327 /******************************************************************************/
329 static struct pppox_proto pppolac_pppox_proto = {
330 .create = pppolac_create,
331 .owner = THIS_MODULE,
334 static int __init pppolac_init(void)
338 error = proto_register(&pppolac_proto, 0);
342 error = register_pppox_proto(PX_PROTO_OLAC, &pppolac_pppox_proto);
344 proto_unregister(&pppolac_proto);
348 static void __exit pppolac_exit(void)
350 unregister_pppox_proto(PX_PROTO_OLAC);
351 proto_unregister(&pppolac_proto);
354 module_init(pppolac_init);
355 module_exit(pppolac_exit);
357 MODULE_DESCRIPTION("PPP on L2TP Access Concentrator (PPPoLAC)");
358 MODULE_AUTHOR("Chia-chi Yeh <chiachi@android.com>");
359 MODULE_LICENSE("GPL");