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 * 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. */
23 #include <linux/module.h>
24 #include <linux/workqueue.h>
25 #include <linux/skbuff.h>
26 #include <linux/file.h>
27 #include <linux/netdevice.h>
28 #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>
35 #include <asm/uaccess.h>
37 #define L2TP_CONTROL_BIT 0x80
38 #define L2TP_LENGTH_BIT 0x40
39 #define L2TP_SEQUENCE_BIT 0x08
40 #define L2TP_OFFSET_BIT 0x02
41 #define L2TP_VERSION 0x02
42 #define L2TP_VERSION_MASK 0x0F
49 } __attribute__((packed
));
51 static inline union unaligned
*unaligned(void *ptr
)
53 return (union unaligned
*)ptr
;
56 static int pppolac_recv_core(struct sock
*sk_udp
, struct sk_buff
*skb
)
58 struct sock
*sk
= (struct sock
*)sk_udp
->sk_user_data
;
59 struct pppolac_opt
*opt
= &pppox_sk(sk
)->proto
.lac
;
63 /* Drop the packet if it is too short. */
64 if (skb
->len
< sizeof(struct udphdr
) + 6)
67 /* Put it back if it is a control packet. */
68 if (skb
->data
[sizeof(struct udphdr
)] & L2TP_CONTROL_BIT
)
69 return opt
->backlog_rcv(sk_udp
, skb
);
71 /* Skip UDP header. */
72 skb_pull(skb
, sizeof(struct udphdr
));
74 /* Check the version. */
75 if ((skb
->data
[1] & L2TP_VERSION_MASK
) != L2TP_VERSION
)
80 /* Check the length if it is present. */
81 if (bits
& L2TP_LENGTH_BIT
) {
82 if ((ptr
[0] << 8 | ptr
[1]) != skb
->len
)
87 /* Skip all fields including optional ones. */
88 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)))
93 /* Skip the offset padding if it is present. */
94 if (bits
& L2TP_OFFSET_BIT
&&
95 !skb_pull(skb
, skb
->data
[-2] << 8 | skb
->data
[-1]))
98 /* Check the tunnel and the session. */
99 if (unaligned(ptr
)->u32
!= opt
->local
)
102 /* 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
;
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
)
111 /* Fix PPP protocol if it is compressed. */
112 if (skb
->len
>= 1 && skb
->data
[0] & 1)
113 skb_push(skb
, 1)[0] = 0;
115 /* Finally, deliver the packet to PPP channel. */
117 ppp_input(&pppox_sk(sk
)->chan
, skb
);
118 return NET_RX_SUCCESS
;
124 static int pppolac_recv(struct sock
*sk_udp
, struct sk_buff
*skb
)
127 sk_receive_skb(sk_udp
, skb
, 0);
131 static struct sk_buff_head delivery_queue
;
133 static void pppolac_xmit_core(struct work_struct
*delivery_work
)
135 mm_segment_t old_fs
= get_fs();
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
,
145 .msg_flags
= MSG_NOSIGNAL
| MSG_DONTWAIT
,
147 sk_udp
->sk_prot
->sendmsg(NULL
, sk_udp
, &msg
, skb
->len
);
153 static DECLARE_WORK(delivery_work
, pppolac_xmit_core
);
155 static int pppolac_xmit(struct ppp_channel
*chan
, struct sk_buff
*skb
)
157 struct sock
*sk_udp
= (struct sock
*)chan
->private;
158 struct pppolac_opt
*opt
= &pppox_sk(sk_udp
->sk_user_data
)->proto
.lac
;
160 /* Install PPP address and control. */
162 skb
->data
[0] = PPP_ADDR
;
163 skb
->data
[1] = PPP_CTRL
;
165 /* Install L2TP header. */
166 if (opt
->sequencing
) {
168 skb
->data
[0] = L2TP_SEQUENCE_BIT
;
169 skb
->data
[6] = opt
->sequence
>> 8;
170 skb
->data
[7] = opt
->sequence
;
178 skb
->data
[1] = L2TP_VERSION
;
179 unaligned(&skb
->data
[2])->u32
= opt
->remote
;
181 /* 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
);
188 /******************************************************************************/
190 static struct ppp_channel_ops pppolac_channel_ops
= {
191 .start_xmit
= pppolac_xmit
,
194 static int pppolac_connect(struct socket
*sock
, struct sockaddr
*useraddr
,
195 int addrlen
, int flags
)
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
;
204 if (addrlen
!= sizeof(struct sockaddr_pppolac
) ||
205 !addr
->local
.tunnel
|| !addr
->local
.session
||
206 !addr
->remote
.tunnel
|| !addr
->remote
.session
) {
212 if (sk
->sk_state
!= PPPOX_NONE
)
215 sock_udp
= sockfd_lookup(addr
->udp_socket
, &error
);
218 sk_udp
= sock_udp
->sk
;
221 /* Remove this check when IPv6 supports UDP encapsulation. */
222 error
= -EAFNOSUPPORT
;
223 if (sk_udp
->sk_family
!= AF_INET
)
225 error
= -EPROTONOSUPPORT
;
226 if (sk_udp
->sk_protocol
!= IPPROTO_UDP
)
228 error
= -EDESTADDRREQ
;
229 if (sk_udp
->sk_state
!= TCP_ESTABLISHED
)
232 if (udp_sk(sk_udp
)->encap_type
|| sk_udp
->sk_user_data
)
234 if (!sk_udp
->sk_bound_dev_if
) {
235 struct dst_entry
*dst
= sk_dst_get(sk_udp
);
239 sk_udp
->sk_bound_dev_if
= dst
->dev
->ifindex
;
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
;
249 po
->proto
.lac
.backlog_rcv
= sk_udp
->sk_backlog_rcv
;
251 error
= ppp_register_channel(&po
->chan
);
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
;
258 sk_udp
->sk_backlog_rcv
= pppolac_recv_core
;
259 sk_udp
->sk_user_data
= sk
;
262 release_sock(sk_udp
);
264 sockfd_put(sock_udp
);
270 static int pppolac_release(struct socket
*sock
)
272 struct sock
*sk
= sock
->sk
;
278 if (sock_flag(sk
, SOCK_DEAD
)) {
283 if (sk
->sk_state
!= PPPOX_NONE
) {
284 struct sock
*sk_udp
= (struct sock
*)pppox_sk(sk
)->chan
.private;
286 pppox_unbind_sock(sk
);
287 udp_sk(sk_udp
)->encap_type
= 0;
288 udp_sk(sk_udp
)->encap_rcv
= NULL
;
289 sk_udp
->sk_backlog_rcv
= pppox_sk(sk
)->proto
.lac
.backlog_rcv
;
290 sk_udp
->sk_user_data
= NULL
;
291 release_sock(sk_udp
);
292 sockfd_put(sk_udp
->sk_socket
);
302 /******************************************************************************/
304 static struct proto pppolac_proto
= {
306 .owner
= THIS_MODULE
,
307 .obj_size
= sizeof(struct pppox_sock
),
310 static struct proto_ops pppolac_proto_ops
= {
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
,
330 static int pppolac_create(struct net
*net
, struct socket
*sock
)
334 sk
= sk_alloc(net
, PF_PPPOX
, GFP_KERNEL
, &pppolac_proto
);
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
;
346 /******************************************************************************/
348 static struct pppox_proto pppolac_pppox_proto
= {
349 .create
= pppolac_create
,
350 .owner
= THIS_MODULE
,
353 static int __init
pppolac_init(void)
357 error
= proto_register(&pppolac_proto
, 0);
361 error
= register_pppox_proto(PX_PROTO_OLAC
, &pppolac_pppox_proto
);
363 proto_unregister(&pppolac_proto
);
365 skb_queue_head_init(&delivery_queue
);
369 static void __exit
pppolac_exit(void)
371 unregister_pppox_proto(PX_PROTO_OLAC
);
372 proto_unregister(&pppolac_proto
);
375 module_init(pppolac_init
);
376 module_exit(pppolac_exit
);
378 MODULE_DESCRIPTION("PPP on L2TP Access Concentrator (PPPoLAC)");
379 MODULE_AUTHOR("Chia-chi Yeh <chiachi@android.com>");
380 MODULE_LICENSE("GPL");