1 /* drivers/net/pppolac.c
3 * Driver for PPP on L2TP Access Concentrator / PPPoLAC Socket (RFC 2661)
5 * Copyright (C) 2009 Google, Inc.
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.
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.
17 /* This driver handles L2TP data packets between a UDP socket and a PPP channel.
18 * The socket must keep connected, and only one session per socket is permitted.
19 * Sequencing of outgoing packets is controlled by LNS. Incoming packets with
20 * sequences are reordered within a sliding window of one second. Currently
21 * reordering only happens when a packet is received. It is done for simplicity
22 * since no additional locks or threads are required. This driver only works on
23 * IPv4 due to the lack of UDP encapsulation support in IPv6. */
25 #include <linux/module.h>
26 #include <linux/jiffies.h>
27 #include <linux/workqueue.h>
28 #include <linux/skbuff.h>
29 #include <linux/file.h>
30 #include <linux/netdevice.h>
31 #include <linux/net.h>
32 #include <linux/udp.h>
33 #include <linux/ppp_defs.h>
34 #include <linux/if_ppp.h>
35 #include <linux/if_pppox.h>
36 #include <linux/ppp_channel.h>
37 #include <net/tcp_states.h>
38 #include <asm/uaccess.h>
40 #define L2TP_CONTROL_BIT 0x80
41 #define L2TP_LENGTH_BIT 0x40
42 #define L2TP_SEQUENCE_BIT 0x08
43 #define L2TP_OFFSET_BIT 0x02
44 #define L2TP_VERSION 0x02
45 #define L2TP_VERSION_MASK 0x0F
52 } __attribute__((packed));
54 static inline union unaligned *unaligned(void *ptr)
56 return (union unaligned *)ptr;
64 static inline struct meta *skb_meta(struct sk_buff *skb)
66 return (struct meta *)skb->cb;
69 /******************************************************************************/
71 static int pppolac_recv_core(struct sock *sk_udp, struct sk_buff *skb)
73 struct sock *sk = (struct sock *)sk_udp->sk_user_data;
74 struct pppolac_opt *opt = &pppox_sk(sk)->proto.lac;
75 struct meta *meta = skb_meta(skb);
80 /* Drop the packet if L2TP header is missing. */
81 if (skb->len < sizeof(struct udphdr) + 6)
84 /* Put it back if it is a control packet. */
85 if (skb->data[sizeof(struct udphdr)] & L2TP_CONTROL_BIT)
86 return opt->backlog_rcv(sk_udp, skb);
88 /* Skip UDP header. */
89 skb_pull(skb, sizeof(struct udphdr));
91 /* Check the version. */
92 if ((skb->data[1] & L2TP_VERSION_MASK) != L2TP_VERSION)
97 /* Check the length if it is present. */
98 if (bits & L2TP_LENGTH_BIT) {
99 if ((ptr[0] << 8 | ptr[1]) != skb->len)
104 /* Skip all fields including optional ones. */
105 if (!skb_pull(skb, 6 + (bits & L2TP_SEQUENCE_BIT ? 4 : 0) +
106 (bits & L2TP_LENGTH_BIT ? 2 : 0) +
107 (bits & L2TP_OFFSET_BIT ? 2 : 0)))
110 /* Skip the offset padding if it is present. */
111 if (bits & L2TP_OFFSET_BIT &&
112 !skb_pull(skb, skb->data[-2] << 8 | skb->data[-1]))
115 /* Check the tunnel and the session. */
116 if (unaligned(ptr)->u32 != opt->local)
119 /* Check the sequence if it is present. */
120 if (bits & L2TP_SEQUENCE_BIT) {
121 meta->sequence = ptr[4] << 8 | ptr[5];
122 if ((__s16)(meta->sequence - opt->recv_sequence) < 0)
126 /* Skip PPP address and control if they are present. */
127 if (skb->len >= 2 && skb->data[0] == PPP_ADDR &&
128 skb->data[1] == PPP_CTRL)
131 /* Fix PPP protocol if it is compressed. */
132 if (skb->len >= 1 && skb->data[0] & 1)
133 skb_push(skb, 1)[0] = 0;
135 /* Drop the packet if PPP protocol is missing. */
139 /* Perform reordering if sequencing is enabled. */
140 atomic_set(&opt->sequencing, bits & L2TP_SEQUENCE_BIT);
141 if (bits & L2TP_SEQUENCE_BIT) {
142 struct sk_buff *skb1;
144 /* Insert the packet into receive queue in order. */
145 skb_set_owner_r(skb, sk);
146 skb_queue_walk(&sk->sk_receive_queue, skb1) {
147 struct meta *meta1 = skb_meta(skb1);
148 __s16 order = meta->sequence - meta1->sequence;
152 meta->timestamp = meta1->timestamp;
153 skb_insert(skb1, skb, &sk->sk_receive_queue);
159 meta->timestamp = now;
160 skb_queue_tail(&sk->sk_receive_queue, skb);
163 /* Remove packets from receive queue as long as
164 * 1. the receive buffer is full,
165 * 2. they are queued longer than one second, or
166 * 3. there are no missing packets before them. */
167 skb_queue_walk_safe(&sk->sk_receive_queue, skb, skb1) {
168 meta = skb_meta(skb);
169 if (atomic_read(&sk->sk_rmem_alloc) < sk->sk_rcvbuf &&
170 now - meta->timestamp < HZ &&
171 meta->sequence != opt->recv_sequence)
173 skb_unlink(skb, &sk->sk_receive_queue);
174 opt->recv_sequence = (__u16)(meta->sequence + 1);
176 ppp_input(&pppox_sk(sk)->chan, skb);
178 return NET_RX_SUCCESS;
181 /* Flush receive queue if sequencing is disabled. */
182 skb_queue_purge(&sk->sk_receive_queue);
184 ppp_input(&pppox_sk(sk)->chan, skb);
185 return NET_RX_SUCCESS;
191 static int pppolac_recv(struct sock *sk_udp, struct sk_buff *skb)
194 sk_receive_skb(sk_udp, skb, 0);
198 static struct sk_buff_head delivery_queue;
200 static void pppolac_xmit_core(struct work_struct *delivery_work)
202 mm_segment_t old_fs = get_fs();
206 while ((skb = skb_dequeue(&delivery_queue))) {
207 struct sock *sk_udp = skb->sk;
208 struct kvec iov = {.iov_base = skb->data, .iov_len = skb->len};
209 struct msghdr msg = { 0 };
211 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, &iov, 1,
213 sk_udp->sk_prot->sendmsg(sk_udp, &msg, skb->len);
219 static DECLARE_WORK(delivery_work, pppolac_xmit_core);
221 static int pppolac_xmit(struct ppp_channel *chan, struct sk_buff *skb)
223 struct sock *sk_udp = (struct sock *)chan->private;
224 struct pppolac_opt *opt = &pppox_sk(sk_udp->sk_user_data)->proto.lac;
226 /* Install PPP address and control. */
228 skb->data[0] = PPP_ADDR;
229 skb->data[1] = PPP_CTRL;
231 /* Install L2TP header. */
232 if (atomic_read(&opt->sequencing)) {
234 skb->data[0] = L2TP_SEQUENCE_BIT;
235 skb->data[6] = opt->xmit_sequence >> 8;
236 skb->data[7] = opt->xmit_sequence;
239 opt->xmit_sequence++;
244 skb->data[1] = L2TP_VERSION;
245 unaligned(&skb->data[2])->u32 = opt->remote;
247 /* Now send the packet via the delivery queue. */
248 skb_set_owner_w(skb, sk_udp);
249 skb_queue_tail(&delivery_queue, skb);
250 schedule_work(&delivery_work);
254 /******************************************************************************/
256 static struct ppp_channel_ops pppolac_channel_ops = {
257 .start_xmit = pppolac_xmit,
260 static int pppolac_connect(struct socket *sock, struct sockaddr *useraddr,
261 int addrlen, int flags)
263 struct sock *sk = sock->sk;
264 struct pppox_sock *po = pppox_sk(sk);
265 struct sockaddr_pppolac *addr = (struct sockaddr_pppolac *)useraddr;
266 struct socket *sock_udp = NULL;
270 if (addrlen != sizeof(struct sockaddr_pppolac) ||
271 !addr->local.tunnel || !addr->local.session ||
272 !addr->remote.tunnel || !addr->remote.session) {
278 if (sk->sk_state != PPPOX_NONE)
281 sock_udp = sockfd_lookup(addr->udp_socket, &error);
284 sk_udp = sock_udp->sk;
287 /* Remove this check when IPv6 supports UDP encapsulation. */
288 error = -EAFNOSUPPORT;
289 if (sk_udp->sk_family != AF_INET)
291 error = -EPROTONOSUPPORT;
292 if (sk_udp->sk_protocol != IPPROTO_UDP)
294 error = -EDESTADDRREQ;
295 if (sk_udp->sk_state != TCP_ESTABLISHED)
298 if (udp_sk(sk_udp)->encap_type || sk_udp->sk_user_data)
300 if (!sk_udp->sk_bound_dev_if) {
301 struct dst_entry *dst = sk_dst_get(sk_udp);
305 sk_udp->sk_bound_dev_if = dst->dev->ifindex;
309 po->chan.hdrlen = 12;
310 po->chan.private = sk_udp;
311 po->chan.ops = &pppolac_channel_ops;
312 po->chan.mtu = PPP_MRU - 80;
313 po->proto.lac.local = unaligned(&addr->local)->u32;
314 po->proto.lac.remote = unaligned(&addr->remote)->u32;
315 atomic_set(&po->proto.lac.sequencing, 1);
316 po->proto.lac.backlog_rcv = sk_udp->sk_backlog_rcv;
318 error = ppp_register_channel(&po->chan);
322 sk->sk_state = PPPOX_CONNECTED;
323 udp_sk(sk_udp)->encap_type = UDP_ENCAP_L2TPINUDP;
324 udp_sk(sk_udp)->encap_rcv = pppolac_recv;
325 sk_udp->sk_backlog_rcv = pppolac_recv_core;
326 sk_udp->sk_user_data = sk;
329 release_sock(sk_udp);
331 sockfd_put(sock_udp);
337 static int pppolac_release(struct socket *sock)
339 struct sock *sk = sock->sk;
345 if (sock_flag(sk, SOCK_DEAD)) {
350 if (sk->sk_state != PPPOX_NONE) {
351 struct sock *sk_udp = (struct sock *)pppox_sk(sk)->chan.private;
353 skb_queue_purge(&sk->sk_receive_queue);
354 pppox_unbind_sock(sk);
355 udp_sk(sk_udp)->encap_type = 0;
356 udp_sk(sk_udp)->encap_rcv = NULL;
357 sk_udp->sk_backlog_rcv = pppox_sk(sk)->proto.lac.backlog_rcv;
358 sk_udp->sk_user_data = NULL;
359 release_sock(sk_udp);
360 sockfd_put(sk_udp->sk_socket);
370 /******************************************************************************/
372 static struct proto pppolac_proto = {
374 .owner = THIS_MODULE,
375 .obj_size = sizeof(struct pppox_sock),
378 static struct proto_ops pppolac_proto_ops = {
380 .owner = THIS_MODULE,
381 .release = pppolac_release,
382 .bind = sock_no_bind,
383 .connect = pppolac_connect,
384 .socketpair = sock_no_socketpair,
385 .accept = sock_no_accept,
386 .getname = sock_no_getname,
387 .poll = sock_no_poll,
388 .ioctl = pppox_ioctl,
389 .listen = sock_no_listen,
390 .shutdown = sock_no_shutdown,
391 .setsockopt = sock_no_setsockopt,
392 .getsockopt = sock_no_getsockopt,
393 .sendmsg = sock_no_sendmsg,
394 .recvmsg = sock_no_recvmsg,
395 .mmap = sock_no_mmap,
398 static int pppolac_create(struct net *net, struct socket *sock, int kern)
402 sk = sk_alloc(net, PF_PPPOX, GFP_KERNEL, &pppolac_proto, kern);
406 sock_init_data(sock, sk);
407 sock->state = SS_UNCONNECTED;
408 sock->ops = &pppolac_proto_ops;
409 sk->sk_protocol = PX_PROTO_OLAC;
410 sk->sk_state = PPPOX_NONE;
414 /******************************************************************************/
416 static struct pppox_proto pppolac_pppox_proto = {
417 .create = pppolac_create,
418 .owner = THIS_MODULE,
421 static int __init pppolac_init(void)
425 error = proto_register(&pppolac_proto, 0);
429 error = register_pppox_proto(PX_PROTO_OLAC, &pppolac_pppox_proto);
431 proto_unregister(&pppolac_proto);
433 skb_queue_head_init(&delivery_queue);
437 static void __exit pppolac_exit(void)
439 unregister_pppox_proto(PX_PROTO_OLAC);
440 proto_unregister(&pppolac_proto);
443 module_init(pppolac_init);
444 module_exit(pppolac_exit);
446 MODULE_DESCRIPTION("PPP on L2TP Access Concentrator (PPPoLAC)");
447 MODULE_AUTHOR("Chia-chi Yeh <chiachi@android.com>");
448 MODULE_LICENSE("GPL");