1 /* drivers/net/pppopns.c
3 * Driver for PPP on PPTP Network Server / PPPoPNS Socket (RFC 2637)
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 PPTP data packets between a RAW socket and a PPP channel.
19 * The socket is created in the kernel space and connected to the same address
20 * of the control socket. To keep things simple, packets are always sent with
21 * sequence but without acknowledgement. This driver should work on both IPv4
24 #include <linux/module.h>
25 #include <linux/skbuff.h>
26 #include <linux/file.h>
27 #include <linux/net.h>
28 #include <linux/ppp_defs.h>
30 #include <linux/if_ppp.h>
31 #include <linux/if_pppox.h>
32 #include <linux/ppp_channel.h>
34 #define GRE_HEADER_SIZE 8
36 #define PPTP_GRE_MASK htons(0x2001)
37 #define PPTP_GRE_SEQ_MASK htons(0x1000)
38 #define PPTP_GRE_ACK_MASK htons(0x0080)
39 #define PPTP_GRE_TYPE htons(0x880B)
50 } __attribute__((packed));
52 static void pppopns_recv(struct sock *sk_raw, int length)
55 struct pppopns_opt *opt;
59 /* Lock sk_raw to prevent sk from being closed. */
61 sk = (struct sock *)sk_raw->sk_user_data;
68 opt = &pppox_sk(sk)->proto.pns;
70 /* Process packets from the receive queue. */
71 while ((skb = skb_dequeue(&sk_raw->sk_receive_queue))) {
72 skb_pull(skb, skb_transport_header(skb) - skb->data);
74 /* Drop the packet if it is too short. */
75 if (skb->len < GRE_HEADER_SIZE)
78 /* Check the header. */
79 hdr = (struct header *)skb->data;
80 if (hdr->type != PPTP_GRE_TYPE || hdr->call != opt->local ||
81 (hdr->bits & PPTP_GRE_MASK) != PPTP_GRE_MASK)
84 /* Skip all fields including optional ones. */
85 if (!skb_pull(skb, GRE_HEADER_SIZE +
86 (hdr->bits & PPTP_GRE_SEQ_MASK ? 4 : 0) +
87 (hdr->bits & PPTP_GRE_ACK_MASK ? 4 : 0)))
90 /* Check the length. */
91 if (skb->len != ntohs(hdr->length))
94 /* Skip PPP address and control if they are present. */
95 if (skb->len >= 2 && skb->data[0] == PPP_ADDR &&
96 skb->data[1] == PPP_CTRL)
99 /* Fix PPP protocol if it is compressed. */
100 if (skb->len >= 1 && skb->data[0] & 1)
101 skb_push(skb, 1)[0] = 0;
103 /* Deliver the packet to PPP channel. We have to lock sk to
104 * prevent another thread from calling pppox_unbind_sock(). */
107 ppp_input(&pppox_sk(sk)->chan, skb);
116 static int pppopns_xmit(struct ppp_channel *chan, struct sk_buff *skb)
118 struct sock *sk_raw = (struct sock *)chan->private;
119 struct pppopns_opt *opt = &pppox_sk(sk_raw->sk_user_data)->proto.pns;
120 struct msghdr msg = {.msg_flags = MSG_NOSIGNAL | MSG_DONTWAIT};
125 /* Install PPP address and control. */
127 skb->data[0] = PPP_ADDR;
128 skb->data[1] = PPP_CTRL;
131 /* Install PPTP GRE header. */
132 hdr = (struct header *)skb_push(skb, 12);
133 hdr->bits = PPTP_GRE_MASK | PPTP_GRE_SEQ_MASK;
134 hdr->type = PPTP_GRE_TYPE;
135 hdr->length = htons(length);
136 hdr->call = opt->remote;
137 hdr->sequence = htonl(opt->sequence);
140 /* Now send the packet via RAW socket. */
141 iov.iov_base = skb->data;
142 iov.iov_len = skb->len;
143 kernel_sendmsg(sk_raw->sk_socket, &msg, &iov, 1, skb->len);
148 /******************************************************************************/
150 static struct ppp_channel_ops pppopns_channel_ops = {
151 .start_xmit = pppopns_xmit,
154 static int pppopns_connect(struct socket *sock, struct sockaddr *useraddr,
155 int addrlen, int flags)
157 struct sock *sk = sock->sk;
158 struct pppox_sock *po = pppox_sk(sk);
159 struct sockaddr_pppopns *addr = (struct sockaddr_pppopns *)useraddr;
160 struct sockaddr_storage ss;
161 struct socket *sock_tcp = NULL;
162 struct socket *sock_raw = NULL;
166 if (addrlen != sizeof(struct sockaddr_pppopns))
171 if (sk->sk_state != PPPOX_NONE)
174 sock_tcp = sockfd_lookup(addr->tcp_socket, &error);
177 error = -EPROTONOSUPPORT;
178 if (sock_tcp->sk->sk_protocol != IPPROTO_TCP)
180 addrlen = sizeof(struct sockaddr_storage);
181 error = kernel_getpeername(sock_tcp, (struct sockaddr *)&ss, &addrlen);
185 error = sock_create(ss.ss_family, SOCK_RAW, IPPROTO_GRE, &sock_raw);
188 error = kernel_connect(sock_raw, (struct sockaddr *)&ss, addrlen, 0);
191 sk_raw = sock_raw->sk;
193 po->chan.hdrlen = 14;
194 po->chan.private = sk_raw;
195 po->chan.ops = &pppopns_channel_ops;
196 po->chan.mtu = PPP_MTU - 80;
197 po->proto.pns.local = addr->local;
198 po->proto.pns.remote = addr->remote;
200 error = ppp_register_channel(&po->chan);
204 sk->sk_state = PPPOX_CONNECTED;
205 sk_raw->sk_user_data = sk;
206 sk_raw->sk_data_ready = pppopns_recv;
210 sockfd_put(sock_tcp);
211 if (error && sock_raw)
212 sock_release(sock_raw);
217 static int pppopns_release(struct socket *sock)
219 struct sock *sk = sock->sk;
225 if (sock_flag(sk, SOCK_DEAD)) {
230 if (sk->sk_state != PPPOX_NONE) {
231 struct sock *sk_raw = (struct sock *)pppox_sk(sk)->chan.private;
233 pppox_unbind_sock(sk);
234 sk_raw->sk_user_data = NULL;
235 release_sock(sk_raw);
236 sock_release(sk_raw->sk_socket);
246 /******************************************************************************/
248 static struct proto pppopns_proto = {
250 .owner = THIS_MODULE,
251 .obj_size = sizeof(struct pppox_sock),
254 static struct proto_ops pppopns_proto_ops = {
256 .owner = THIS_MODULE,
257 .release = pppopns_release,
258 .bind = sock_no_bind,
259 .connect = pppopns_connect,
260 .socketpair = sock_no_socketpair,
261 .accept = sock_no_accept,
262 .getname = sock_no_getname,
263 .poll = sock_no_poll,
264 .ioctl = pppox_ioctl,
265 .listen = sock_no_listen,
266 .shutdown = sock_no_shutdown,
267 .setsockopt = sock_no_setsockopt,
268 .getsockopt = sock_no_getsockopt,
269 .sendmsg = sock_no_sendmsg,
270 .recvmsg = sock_no_recvmsg,
271 .mmap = sock_no_mmap,
274 static int pppopns_create(struct net *net, struct socket *sock)
278 sk = sk_alloc(net, PF_PPPOX, GFP_KERNEL, &pppopns_proto);
282 sock_init_data(sock, sk);
283 sock->state = SS_UNCONNECTED;
284 sock->ops = &pppopns_proto_ops;
285 sk->sk_protocol = PX_PROTO_OPNS;
286 sk->sk_state = PPPOX_NONE;
290 /******************************************************************************/
292 static struct pppox_proto pppopns_pppox_proto = {
293 .create = pppopns_create,
294 .owner = THIS_MODULE,
297 static int __init pppopns_init(void)
301 error = proto_register(&pppopns_proto, 0);
305 error = register_pppox_proto(PX_PROTO_OPNS, &pppopns_pppox_proto);
307 proto_unregister(&pppopns_proto);
311 static void __exit pppopns_exit(void)
313 unregister_pppox_proto(PX_PROTO_OPNS);
314 proto_unregister(&pppopns_proto);
317 module_init(pppopns_init);
318 module_exit(pppopns_exit);
320 MODULE_DESCRIPTION("PPP on PPTP Network Server (PPPoPNS)");
321 MODULE_AUTHOR("Chia-chi Yeh <chiachi@android.com>");
322 MODULE_LICENSE("GPL");