blob: af3202a920a005b1f2d379def77a73f8cf3f40af [file] [log] [blame]
Chia-chi Yeh36d00742009-05-08 04:02:40 +08001/* drivers/net/pppolac.c
2 *
3 * Driver for PPP on L2TP Access Concentrator / PPPoLAC Socket (RFC 2661)
4 *
5 * Copyright (C) 2009 Google, Inc.
Chia-chi Yeh36d00742009-05-08 04:02:40 +08006 *
7 * This software is licensed under the terms of the GNU General Public
8 * License version 2, as published by the Free Software Foundation, and
9 * may be copied, distributed, and modified under those terms.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 */
16
17/* This driver handles L2TP data packets between a UDP socket and a PPP channel.
18 * To keep things simple, only one session per socket is permitted. Packets are
19 * sent via the socket, so it must keep connected to the same address. One must
20 * not set sequencing in ICCN but let LNS controll it. Currently this driver
21 * only works on IPv4 due to the lack of UDP encapsulation support in IPv6. */
22
23#include <linux/module.h>
Chia-chi Yeh764fc0e2009-06-13 02:29:04 +080024#include <linux/workqueue.h>
Chia-chi Yeh36d00742009-05-08 04:02:40 +080025#include <linux/skbuff.h>
26#include <linux/file.h>
Chia-chi Yeh764fc0e2009-06-13 02:29:04 +080027#include <linux/netdevice.h>
Chia-chi Yeh36d00742009-05-08 04:02:40 +080028#include <linux/net.h>
29#include <linux/udp.h>
30#include <linux/ppp_defs.h>
31#include <linux/if_ppp.h>
32#include <linux/if_pppox.h>
33#include <linux/ppp_channel.h>
34#include <net/tcp_states.h>
Chia-chi Yeh764fc0e2009-06-13 02:29:04 +080035#include <asm/uaccess.h>
Chia-chi Yeh36d00742009-05-08 04:02:40 +080036
Chia-chi Yeh764fc0e2009-06-13 02:29:04 +080037#define L2TP_CONTROL_BIT 0x80
38#define L2TP_LENGTH_BIT 0x40
39#define L2TP_SEQUENCE_BIT 0x08
40#define L2TP_OFFSET_BIT 0x02
Chia-chi Yeh36d00742009-05-08 04:02:40 +080041#define L2TP_VERSION 0x02
Chia-chi Yeh764fc0e2009-06-13 02:29:04 +080042#define L2TP_VERSION_MASK 0x0F
Chia-chi Yeh36d00742009-05-08 04:02:40 +080043
44#define PPP_ADDR 0xFF
45#define PPP_CTRL 0x03
46
47union unaligned {
48 __u32 u32;
49} __attribute__((packed));
50
51static inline union unaligned *unaligned(void *ptr)
52{
53 return (union unaligned *)ptr;
54}
55
Chia-chi Yeh764fc0e2009-06-13 02:29:04 +080056static int pppolac_recv_core(struct sock *sk_udp, struct sk_buff *skb)
Chia-chi Yeh36d00742009-05-08 04:02:40 +080057{
Chia-chi Yeh764fc0e2009-06-13 02:29:04 +080058 struct sock *sk = (struct sock *)sk_udp->sk_user_data;
59 struct pppolac_opt *opt = &pppox_sk(sk)->proto.lac;
Chia-chi Yeh36d00742009-05-08 04:02:40 +080060 __u8 bits;
61 __u8 *ptr;
62
63 /* Drop the packet if it is too short. */
64 if (skb->len < sizeof(struct udphdr) + 6)
65 goto drop;
66
67 /* Put it back if it is a control packet. */
Chia-chi Yeh764fc0e2009-06-13 02:29:04 +080068 if (skb->data[sizeof(struct udphdr)] & L2TP_CONTROL_BIT)
69 return opt->backlog_rcv(sk_udp, skb);
Chia-chi Yeh36d00742009-05-08 04:02:40 +080070
Chia-chi Yeh764fc0e2009-06-13 02:29:04 +080071 /* Skip UDP header. */
Chia-chi Yeh36d00742009-05-08 04:02:40 +080072 skb_pull(skb, sizeof(struct udphdr));
73
74 /* Check the version. */
75 if ((skb->data[1] & L2TP_VERSION_MASK) != L2TP_VERSION)
76 goto drop;
77 bits = skb->data[0];
78 ptr = &skb->data[2];
79
80 /* Check the length if it is present. */
Chia-chi Yeh764fc0e2009-06-13 02:29:04 +080081 if (bits & L2TP_LENGTH_BIT) {
Chia-chi Yeh36d00742009-05-08 04:02:40 +080082 if ((ptr[0] << 8 | ptr[1]) != skb->len)
83 goto drop;
84 ptr += 2;
85 }
86
87 /* Skip all fields including optional ones. */
Chia-chi Yeh764fc0e2009-06-13 02:29:04 +080088 if (!skb_pull(skb, 6 + (bits & L2TP_SEQUENCE_BIT ? 4 : 0) +
89 (bits & L2TP_LENGTH_BIT ? 2 : 0) +
90 (bits & L2TP_OFFSET_BIT ? 2 : 0)))
Chia-chi Yeh36d00742009-05-08 04:02:40 +080091 goto drop;
92
93 /* Skip the offset padding if it is present. */
Chia-chi Yeh764fc0e2009-06-13 02:29:04 +080094 if (bits & L2TP_OFFSET_BIT &&
Chia-chi Yeh36d00742009-05-08 04:02:40 +080095 !skb_pull(skb, skb->data[-2] << 8 | skb->data[-1]))
96 goto drop;
97
Chia-chi Yeh36d00742009-05-08 04:02:40 +080098 /* Check the tunnel and the session. */
Chia-chi Yeh764fc0e2009-06-13 02:29:04 +080099 if (unaligned(ptr)->u32 != opt->local)
Chia-chi Yeh36d00742009-05-08 04:02:40 +0800100 goto drop;
Chia-chi Yeh36d00742009-05-08 04:02:40 +0800101
Chia-chi Yeh764fc0e2009-06-13 02:29:04 +0800102 /* Check the sequence if it is present. According to RFC 2661 section
103 * 5.4, the only thing to do is to update opt->sequencing. */
104 opt->sequencing = bits & L2TP_SEQUENCE_BIT;
Chia-chi Yeh36d00742009-05-08 04:02:40 +0800105
106 /* Skip PPP address and control if they are present. */
107 if (skb->len >= 2 && skb->data[0] == PPP_ADDR &&
108 skb->data[1] == PPP_CTRL)
109 skb_pull(skb, 2);
110
111 /* Fix PPP protocol if it is compressed. */
112 if (skb->len >= 1 && skb->data[0] & 1)
113 skb_push(skb, 1)[0] = 0;
114
Chia-chi Yeh764fc0e2009-06-13 02:29:04 +0800115 /* Finally, deliver the packet to PPP channel. */
Chia-chi Yeh36d00742009-05-08 04:02:40 +0800116 skb_orphan(skb);
Chia-chi Yeh36d00742009-05-08 04:02:40 +0800117 ppp_input(&pppox_sk(sk)->chan, skb);
Chia-chi Yeh764fc0e2009-06-13 02:29:04 +0800118 return NET_RX_SUCCESS;
Chia-chi Yeh36d00742009-05-08 04:02:40 +0800119drop:
120 kfree_skb(skb);
Chia-chi Yeh764fc0e2009-06-13 02:29:04 +0800121 return NET_RX_DROP;
122}
123
124static int pppolac_recv(struct sock *sk_udp, struct sk_buff *skb)
125{
126 sock_hold(sk_udp);
127 sk_receive_skb(sk_udp, skb, 0);
Chia-chi Yeh36d00742009-05-08 04:02:40 +0800128 return 0;
129}
130
Chia-chi Yeh764fc0e2009-06-13 02:29:04 +0800131static struct sk_buff_head delivery_queue;
132
133static void pppolac_xmit_core(struct work_struct *delivery_work)
134{
135 mm_segment_t old_fs = get_fs();
136 struct sk_buff *skb;
137
138 set_fs(KERNEL_DS);
139 while ((skb = skb_dequeue(&delivery_queue))) {
140 struct sock *sk_udp = skb->sk;
141 struct kvec iov = {.iov_base = skb->data, .iov_len = skb->len};
142 struct msghdr msg = {
143 .msg_iov = (struct iovec *)&iov,
144 .msg_iovlen = 1,
145 .msg_flags = MSG_NOSIGNAL | MSG_DONTWAIT,
146 };
147 sk_udp->sk_prot->sendmsg(NULL, sk_udp, &msg, skb->len);
148 kfree_skb(skb);
149 }
150 set_fs(old_fs);
151}
152
153static DECLARE_WORK(delivery_work, pppolac_xmit_core);
154
Chia-chi Yeh36d00742009-05-08 04:02:40 +0800155static int pppolac_xmit(struct ppp_channel *chan, struct sk_buff *skb)
156{
157 struct sock *sk_udp = (struct sock *)chan->private;
158 struct pppolac_opt *opt = &pppox_sk(sk_udp->sk_user_data)->proto.lac;
Chia-chi Yeh36d00742009-05-08 04:02:40 +0800159
160 /* Install PPP address and control. */
161 skb_push(skb, 2);
162 skb->data[0] = PPP_ADDR;
163 skb->data[1] = PPP_CTRL;
164
165 /* Install L2TP header. */
166 if (opt->sequencing) {
167 skb_push(skb, 10);
Chia-chi Yeh764fc0e2009-06-13 02:29:04 +0800168 skb->data[0] = L2TP_SEQUENCE_BIT;
Chia-chi Yeh36d00742009-05-08 04:02:40 +0800169 skb->data[6] = opt->sequence >> 8;
170 skb->data[7] = opt->sequence;
171 skb->data[8] = 0;
172 skb->data[9] = 0;
173 opt->sequence++;
174 } else {
175 skb_push(skb, 6);
176 skb->data[0] = 0;
177 }
178 skb->data[1] = L2TP_VERSION;
179 unaligned(&skb->data[2])->u32 = opt->remote;
180
Chia-chi Yeh764fc0e2009-06-13 02:29:04 +0800181 /* Now send the packet via the delivery queue. */
182 skb_set_owner_w(skb, sk_udp);
183 skb_queue_tail(&delivery_queue, skb);
184 schedule_work(&delivery_work);
Chia-chi Yeh36d00742009-05-08 04:02:40 +0800185 return 1;
186}
187
188/******************************************************************************/
189
190static struct ppp_channel_ops pppolac_channel_ops = {
191 .start_xmit = pppolac_xmit,
192};
193
194static int pppolac_connect(struct socket *sock, struct sockaddr *useraddr,
195 int addrlen, int flags)
196{
197 struct sock *sk = sock->sk;
198 struct pppox_sock *po = pppox_sk(sk);
199 struct sockaddr_pppolac *addr = (struct sockaddr_pppolac *)useraddr;
200 struct socket *sock_udp = NULL;
201 struct sock *sk_udp;
202 int error;
203
204 if (addrlen != sizeof(struct sockaddr_pppolac) ||
205 !addr->local.tunnel || !addr->local.session ||
206 !addr->remote.tunnel || !addr->remote.session) {
207 return -EINVAL;
208 }
209
210 lock_sock(sk);
211 error = -EALREADY;
212 if (sk->sk_state != PPPOX_NONE)
213 goto out;
214
215 sock_udp = sockfd_lookup(addr->udp_socket, &error);
216 if (!sock_udp)
217 goto out;
218 sk_udp = sock_udp->sk;
219 lock_sock(sk_udp);
220
221 /* Remove this check when IPv6 supports UDP encapsulation. */
222 error = -EAFNOSUPPORT;
223 if (sk_udp->sk_family != AF_INET)
224 goto out;
225 error = -EPROTONOSUPPORT;
226 if (sk_udp->sk_protocol != IPPROTO_UDP)
227 goto out;
228 error = -EDESTADDRREQ;
229 if (sk_udp->sk_state != TCP_ESTABLISHED)
230 goto out;
231 error = -EBUSY;
232 if (udp_sk(sk_udp)->encap_type || sk_udp->sk_user_data)
233 goto out;
Chia-chi Yeh764fc0e2009-06-13 02:29:04 +0800234 if (!sk_udp->sk_bound_dev_if) {
235 struct dst_entry *dst = sk_dst_get(sk_udp);
236 error = -ENODEV;
237 if (!dst)
238 goto out;
239 sk_udp->sk_bound_dev_if = dst->dev->ifindex;
240 dst_release(dst);
241 }
Chia-chi Yeh36d00742009-05-08 04:02:40 +0800242
243 po->chan.hdrlen = 12;
244 po->chan.private = sk_udp;
245 po->chan.ops = &pppolac_channel_ops;
246 po->chan.mtu = PPP_MTU - 80;
247 po->proto.lac.local = unaligned(&addr->local)->u32;
248 po->proto.lac.remote = unaligned(&addr->remote)->u32;
Chia-chi Yeh764fc0e2009-06-13 02:29:04 +0800249 po->proto.lac.backlog_rcv = sk_udp->sk_backlog_rcv;
Chia-chi Yeh36d00742009-05-08 04:02:40 +0800250
251 error = ppp_register_channel(&po->chan);
252 if (error)
253 goto out;
254
255 sk->sk_state = PPPOX_CONNECTED;
256 udp_sk(sk_udp)->encap_type = UDP_ENCAP_L2TPINUDP;
257 udp_sk(sk_udp)->encap_rcv = pppolac_recv;
Chia-chi Yeh764fc0e2009-06-13 02:29:04 +0800258 sk_udp->sk_backlog_rcv = pppolac_recv_core;
Chia-chi Yeh36d00742009-05-08 04:02:40 +0800259 sk_udp->sk_user_data = sk;
Chia-chi Yeh36d00742009-05-08 04:02:40 +0800260out:
261 if (sock_udp) {
262 release_sock(sk_udp);
263 if (error)
264 sockfd_put(sock_udp);
265 }
266 release_sock(sk);
267 return error;
268}
269
270static int pppolac_release(struct socket *sock)
271{
272 struct sock *sk = sock->sk;
273
274 if (!sk)
275 return 0;
276
277 lock_sock(sk);
278 if (sock_flag(sk, SOCK_DEAD)) {
279 release_sock(sk);
280 return -EBADF;
281 }
282
283 if (sk->sk_state != PPPOX_NONE) {
284 struct sock *sk_udp = (struct sock *)pppox_sk(sk)->chan.private;
285 lock_sock(sk_udp);
Chia-chi Yeh36d00742009-05-08 04:02:40 +0800286 pppox_unbind_sock(sk);
Chia-chi Yeh36d00742009-05-08 04:02:40 +0800287 udp_sk(sk_udp)->encap_type = 0;
288 udp_sk(sk_udp)->encap_rcv = NULL;
Chia-chi Yeh764fc0e2009-06-13 02:29:04 +0800289 sk_udp->sk_backlog_rcv = pppox_sk(sk)->proto.lac.backlog_rcv;
290 sk_udp->sk_user_data = NULL;
Chia-chi Yeh36d00742009-05-08 04:02:40 +0800291 release_sock(sk_udp);
292 sockfd_put(sk_udp->sk_socket);
293 }
294
295 sock_orphan(sk);
296 sock->sk = NULL;
297 release_sock(sk);
298 sock_put(sk);
299 return 0;
300}
301
302/******************************************************************************/
303
304static struct proto pppolac_proto = {
305 .name = "PPPOLAC",
306 .owner = THIS_MODULE,
307 .obj_size = sizeof(struct pppox_sock),
308};
309
310static struct proto_ops pppolac_proto_ops = {
311 .family = PF_PPPOX,
312 .owner = THIS_MODULE,
313 .release = pppolac_release,
314 .bind = sock_no_bind,
315 .connect = pppolac_connect,
316 .socketpair = sock_no_socketpair,
317 .accept = sock_no_accept,
318 .getname = sock_no_getname,
319 .poll = sock_no_poll,
320 .ioctl = pppox_ioctl,
321 .listen = sock_no_listen,
322 .shutdown = sock_no_shutdown,
323 .setsockopt = sock_no_setsockopt,
324 .getsockopt = sock_no_getsockopt,
325 .sendmsg = sock_no_sendmsg,
326 .recvmsg = sock_no_recvmsg,
327 .mmap = sock_no_mmap,
328};
329
330static int pppolac_create(struct net *net, struct socket *sock)
331{
332 struct sock *sk;
333
334 sk = sk_alloc(net, PF_PPPOX, GFP_KERNEL, &pppolac_proto);
335 if (!sk)
336 return -ENOMEM;
337
338 sock_init_data(sock, sk);
339 sock->state = SS_UNCONNECTED;
340 sock->ops = &pppolac_proto_ops;
341 sk->sk_protocol = PX_PROTO_OLAC;
342 sk->sk_state = PPPOX_NONE;
343 return 0;
344}
345
346/******************************************************************************/
347
348static struct pppox_proto pppolac_pppox_proto = {
349 .create = pppolac_create,
350 .owner = THIS_MODULE,
351};
352
353static int __init pppolac_init(void)
354{
355 int error;
356
357 error = proto_register(&pppolac_proto, 0);
358 if (error)
359 return error;
360
361 error = register_pppox_proto(PX_PROTO_OLAC, &pppolac_pppox_proto);
362 if (error)
363 proto_unregister(&pppolac_proto);
Chia-chi Yeh764fc0e2009-06-13 02:29:04 +0800364 else
365 skb_queue_head_init(&delivery_queue);
Chia-chi Yeh36d00742009-05-08 04:02:40 +0800366 return error;
367}
368
369static void __exit pppolac_exit(void)
370{
371 unregister_pppox_proto(PX_PROTO_OLAC);
372 proto_unregister(&pppolac_proto);
373}
374
375module_init(pppolac_init);
376module_exit(pppolac_exit);
377
378MODULE_DESCRIPTION("PPP on L2TP Access Concentrator (PPPoLAC)");
379MODULE_AUTHOR("Chia-chi Yeh <chiachi@android.com>");
380MODULE_LICENSE("GPL");