Dmitry Kozlov | 00959ad | 2010-08-21 23:05:39 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Point-to-Point Tunneling Protocol for Linux |
| 3 | * |
| 4 | * Authors: Dmitry Kozlov <xeb@mail.ru> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <linux/string.h> |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/kernel.h> |
| 16 | #include <linux/slab.h> |
| 17 | #include <linux/errno.h> |
| 18 | #include <linux/netdevice.h> |
| 19 | #include <linux/net.h> |
| 20 | #include <linux/skbuff.h> |
| 21 | #include <linux/vmalloc.h> |
| 22 | #include <linux/init.h> |
| 23 | #include <linux/ppp_channel.h> |
| 24 | #include <linux/ppp_defs.h> |
| 25 | #include <linux/if_pppox.h> |
| 26 | #include <linux/if_ppp.h> |
| 27 | #include <linux/notifier.h> |
| 28 | #include <linux/file.h> |
| 29 | #include <linux/in.h> |
| 30 | #include <linux/ip.h> |
| 31 | #include <linux/netfilter.h> |
| 32 | #include <linux/netfilter_ipv4.h> |
| 33 | #include <linux/version.h> |
| 34 | #include <linux/rcupdate.h> |
| 35 | #include <linux/spinlock.h> |
| 36 | |
| 37 | #include <net/sock.h> |
| 38 | #include <net/protocol.h> |
| 39 | #include <net/ip.h> |
| 40 | #include <net/icmp.h> |
| 41 | #include <net/route.h> |
| 42 | #include <net/gre.h> |
| 43 | |
| 44 | #include <linux/uaccess.h> |
| 45 | |
| 46 | #define PPTP_DRIVER_VERSION "0.8.5" |
| 47 | |
| 48 | #define MAX_CALLID 65535 |
| 49 | |
| 50 | static DECLARE_BITMAP(callid_bitmap, MAX_CALLID + 1); |
| 51 | static struct pppox_sock **callid_sock; |
| 52 | |
| 53 | static DEFINE_SPINLOCK(chan_lock); |
| 54 | |
| 55 | static struct proto pptp_sk_proto __read_mostly; |
Eric Dumazet | 756e64a | 2010-09-21 06:43:54 +0000 | [diff] [blame] | 56 | static const struct ppp_channel_ops pptp_chan_ops; |
Dmitry Kozlov | 00959ad | 2010-08-21 23:05:39 -0700 | [diff] [blame] | 57 | static const struct proto_ops pptp_ops; |
| 58 | |
| 59 | #define PPP_LCP_ECHOREQ 0x09 |
| 60 | #define PPP_LCP_ECHOREP 0x0A |
| 61 | #define SC_RCV_BITS (SC_RCV_B7_1|SC_RCV_B7_0|SC_RCV_ODDP|SC_RCV_EVNP) |
| 62 | |
| 63 | #define MISSING_WINDOW 20 |
| 64 | #define WRAPPED(curseq, lastseq)\ |
| 65 | ((((curseq) & 0xffffff00) == 0) &&\ |
| 66 | (((lastseq) & 0xffffff00) == 0xffffff00)) |
| 67 | |
| 68 | #define PPTP_GRE_PROTO 0x880B |
| 69 | #define PPTP_GRE_VER 0x1 |
| 70 | |
| 71 | #define PPTP_GRE_FLAG_C 0x80 |
| 72 | #define PPTP_GRE_FLAG_R 0x40 |
| 73 | #define PPTP_GRE_FLAG_K 0x20 |
| 74 | #define PPTP_GRE_FLAG_S 0x10 |
| 75 | #define PPTP_GRE_FLAG_A 0x80 |
| 76 | |
| 77 | #define PPTP_GRE_IS_C(f) ((f)&PPTP_GRE_FLAG_C) |
| 78 | #define PPTP_GRE_IS_R(f) ((f)&PPTP_GRE_FLAG_R) |
| 79 | #define PPTP_GRE_IS_K(f) ((f)&PPTP_GRE_FLAG_K) |
| 80 | #define PPTP_GRE_IS_S(f) ((f)&PPTP_GRE_FLAG_S) |
| 81 | #define PPTP_GRE_IS_A(f) ((f)&PPTP_GRE_FLAG_A) |
| 82 | |
| 83 | #define PPTP_HEADER_OVERHEAD (2+sizeof(struct pptp_gre_header)) |
| 84 | struct pptp_gre_header { |
| 85 | u8 flags; |
| 86 | u8 ver; |
| 87 | u16 protocol; |
| 88 | u16 payload_len; |
| 89 | u16 call_id; |
| 90 | u32 seq; |
| 91 | u32 ack; |
| 92 | } __packed; |
| 93 | |
| 94 | static struct pppox_sock *lookup_chan(u16 call_id, __be32 s_addr) |
| 95 | { |
| 96 | struct pppox_sock *sock; |
| 97 | struct pptp_opt *opt; |
| 98 | |
| 99 | rcu_read_lock(); |
| 100 | sock = rcu_dereference(callid_sock[call_id]); |
| 101 | if (sock) { |
| 102 | opt = &sock->proto.pptp; |
| 103 | if (opt->dst_addr.sin_addr.s_addr != s_addr) |
| 104 | sock = NULL; |
| 105 | else |
| 106 | sock_hold(sk_pppox(sock)); |
| 107 | } |
| 108 | rcu_read_unlock(); |
| 109 | |
| 110 | return sock; |
| 111 | } |
| 112 | |
| 113 | static int lookup_chan_dst(u16 call_id, __be32 d_addr) |
| 114 | { |
| 115 | struct pppox_sock *sock; |
| 116 | struct pptp_opt *opt; |
| 117 | int i; |
| 118 | |
| 119 | rcu_read_lock(); |
| 120 | for (i = find_next_bit(callid_bitmap, MAX_CALLID, 1); i < MAX_CALLID; |
| 121 | i = find_next_bit(callid_bitmap, MAX_CALLID, i + 1)) { |
| 122 | sock = rcu_dereference(callid_sock[i]); |
| 123 | if (!sock) |
| 124 | continue; |
| 125 | opt = &sock->proto.pptp; |
| 126 | if (opt->dst_addr.call_id == call_id && |
| 127 | opt->dst_addr.sin_addr.s_addr == d_addr) |
| 128 | break; |
| 129 | } |
| 130 | rcu_read_unlock(); |
| 131 | |
| 132 | return i < MAX_CALLID; |
| 133 | } |
| 134 | |
| 135 | static int add_chan(struct pppox_sock *sock) |
| 136 | { |
| 137 | static int call_id; |
| 138 | |
| 139 | spin_lock(&chan_lock); |
| 140 | if (!sock->proto.pptp.src_addr.call_id) { |
| 141 | call_id = find_next_zero_bit(callid_bitmap, MAX_CALLID, call_id + 1); |
| 142 | if (call_id == MAX_CALLID) { |
| 143 | call_id = find_next_zero_bit(callid_bitmap, MAX_CALLID, 1); |
| 144 | if (call_id == MAX_CALLID) |
| 145 | goto out_err; |
| 146 | } |
| 147 | sock->proto.pptp.src_addr.call_id = call_id; |
| 148 | } else if (test_bit(sock->proto.pptp.src_addr.call_id, callid_bitmap)) |
| 149 | goto out_err; |
| 150 | |
| 151 | set_bit(sock->proto.pptp.src_addr.call_id, callid_bitmap); |
| 152 | rcu_assign_pointer(callid_sock[sock->proto.pptp.src_addr.call_id], sock); |
| 153 | spin_unlock(&chan_lock); |
| 154 | |
| 155 | return 0; |
| 156 | |
| 157 | out_err: |
| 158 | spin_unlock(&chan_lock); |
| 159 | return -1; |
| 160 | } |
| 161 | |
| 162 | static void del_chan(struct pppox_sock *sock) |
| 163 | { |
| 164 | spin_lock(&chan_lock); |
| 165 | clear_bit(sock->proto.pptp.src_addr.call_id, callid_bitmap); |
| 166 | rcu_assign_pointer(callid_sock[sock->proto.pptp.src_addr.call_id], NULL); |
| 167 | spin_unlock(&chan_lock); |
| 168 | synchronize_rcu(); |
| 169 | } |
| 170 | |
| 171 | static int pptp_xmit(struct ppp_channel *chan, struct sk_buff *skb) |
| 172 | { |
| 173 | struct sock *sk = (struct sock *) chan->private; |
| 174 | struct pppox_sock *po = pppox_sk(sk); |
| 175 | struct pptp_opt *opt = &po->proto.pptp; |
| 176 | struct pptp_gre_header *hdr; |
| 177 | unsigned int header_len = sizeof(*hdr); |
David S. Miller | 31e4543d | 2011-05-03 20:25:42 -0700 | [diff] [blame] | 178 | struct flowi4 fl4; |
Dmitry Kozlov | 00959ad | 2010-08-21 23:05:39 -0700 | [diff] [blame] | 179 | int islcp; |
| 180 | int len; |
| 181 | unsigned char *data; |
| 182 | __u32 seq_recv; |
| 183 | |
| 184 | |
| 185 | struct rtable *rt; |
| 186 | struct net_device *tdev; |
| 187 | struct iphdr *iph; |
| 188 | int max_headroom; |
| 189 | |
| 190 | if (sk_pppox(po)->sk_state & PPPOX_DEAD) |
| 191 | goto tx_error; |
| 192 | |
David S. Miller | 31e4543d | 2011-05-03 20:25:42 -0700 | [diff] [blame] | 193 | rt = ip_route_output_ports(&init_net, &fl4, NULL, |
David S. Miller | 78fbfd8 | 2011-03-12 00:00:52 -0500 | [diff] [blame] | 194 | opt->dst_addr.sin_addr.s_addr, |
| 195 | opt->src_addr.sin_addr.s_addr, |
| 196 | 0, 0, IPPROTO_GRE, |
| 197 | RT_TOS(0), 0); |
| 198 | if (IS_ERR(rt)) |
| 199 | goto tx_error; |
| 200 | |
Dmitry Kozlov | 00959ad | 2010-08-21 23:05:39 -0700 | [diff] [blame] | 201 | tdev = rt->dst.dev; |
| 202 | |
| 203 | max_headroom = LL_RESERVED_SPACE(tdev) + sizeof(*iph) + sizeof(*hdr) + 2; |
| 204 | |
| 205 | if (skb_headroom(skb) < max_headroom || skb_cloned(skb) || skb_shared(skb)) { |
| 206 | struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom); |
| 207 | if (!new_skb) { |
| 208 | ip_rt_put(rt); |
| 209 | goto tx_error; |
| 210 | } |
| 211 | if (skb->sk) |
| 212 | skb_set_owner_w(new_skb, skb->sk); |
| 213 | kfree_skb(skb); |
| 214 | skb = new_skb; |
| 215 | } |
| 216 | |
| 217 | data = skb->data; |
| 218 | islcp = ((data[0] << 8) + data[1]) == PPP_LCP && 1 <= data[2] && data[2] <= 7; |
| 219 | |
| 220 | /* compress protocol field */ |
| 221 | if ((opt->ppp_flags & SC_COMP_PROT) && data[0] == 0 && !islcp) |
| 222 | skb_pull(skb, 1); |
| 223 | |
| 224 | /* Put in the address/control bytes if necessary */ |
| 225 | if ((opt->ppp_flags & SC_COMP_AC) == 0 || islcp) { |
| 226 | data = skb_push(skb, 2); |
| 227 | data[0] = PPP_ALLSTATIONS; |
| 228 | data[1] = PPP_UI; |
| 229 | } |
| 230 | |
| 231 | len = skb->len; |
| 232 | |
| 233 | seq_recv = opt->seq_recv; |
| 234 | |
| 235 | if (opt->ack_sent == seq_recv) |
| 236 | header_len -= sizeof(hdr->ack); |
| 237 | |
| 238 | /* Push down and install GRE header */ |
| 239 | skb_push(skb, header_len); |
| 240 | hdr = (struct pptp_gre_header *)(skb->data); |
| 241 | |
| 242 | hdr->flags = PPTP_GRE_FLAG_K; |
| 243 | hdr->ver = PPTP_GRE_VER; |
| 244 | hdr->protocol = htons(PPTP_GRE_PROTO); |
| 245 | hdr->call_id = htons(opt->dst_addr.call_id); |
| 246 | |
| 247 | hdr->flags |= PPTP_GRE_FLAG_S; |
| 248 | hdr->seq = htonl(++opt->seq_sent); |
| 249 | if (opt->ack_sent != seq_recv) { |
| 250 | /* send ack with this message */ |
| 251 | hdr->ver |= PPTP_GRE_FLAG_A; |
| 252 | hdr->ack = htonl(seq_recv); |
| 253 | opt->ack_sent = seq_recv; |
| 254 | } |
| 255 | hdr->payload_len = htons(len); |
| 256 | |
| 257 | /* Push down and install the IP header. */ |
| 258 | |
| 259 | skb_reset_transport_header(skb); |
| 260 | skb_push(skb, sizeof(*iph)); |
| 261 | skb_reset_network_header(skb); |
| 262 | memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt)); |
| 263 | IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED | IPSKB_REROUTED); |
| 264 | |
| 265 | iph = ip_hdr(skb); |
| 266 | iph->version = 4; |
| 267 | iph->ihl = sizeof(struct iphdr) >> 2; |
| 268 | if (ip_dont_fragment(sk, &rt->dst)) |
| 269 | iph->frag_off = htons(IP_DF); |
| 270 | else |
| 271 | iph->frag_off = 0; |
| 272 | iph->protocol = IPPROTO_GRE; |
| 273 | iph->tos = 0; |
David S. Miller | 28c90da | 2011-05-03 20:41:42 -0700 | [diff] [blame] | 274 | iph->daddr = fl4.daddr; |
| 275 | iph->saddr = fl4.saddr; |
David S. Miller | 323e126 | 2010-12-12 21:55:08 -0800 | [diff] [blame] | 276 | iph->ttl = ip4_dst_hoplimit(&rt->dst); |
Dmitry Kozlov | 00959ad | 2010-08-21 23:05:39 -0700 | [diff] [blame] | 277 | iph->tot_len = htons(skb->len); |
| 278 | |
| 279 | skb_dst_drop(skb); |
| 280 | skb_dst_set(skb, &rt->dst); |
| 281 | |
| 282 | nf_reset(skb); |
| 283 | |
| 284 | skb->ip_summed = CHECKSUM_NONE; |
| 285 | ip_select_ident(iph, &rt->dst, NULL); |
| 286 | ip_send_check(iph); |
| 287 | |
| 288 | ip_local_out(skb); |
| 289 | |
| 290 | tx_error: |
| 291 | return 1; |
| 292 | } |
| 293 | |
| 294 | static int pptp_rcv_core(struct sock *sk, struct sk_buff *skb) |
| 295 | { |
| 296 | struct pppox_sock *po = pppox_sk(sk); |
| 297 | struct pptp_opt *opt = &po->proto.pptp; |
| 298 | int headersize, payload_len, seq; |
| 299 | __u8 *payload; |
| 300 | struct pptp_gre_header *header; |
| 301 | |
| 302 | if (!(sk->sk_state & PPPOX_CONNECTED)) { |
| 303 | if (sock_queue_rcv_skb(sk, skb)) |
| 304 | goto drop; |
| 305 | return NET_RX_SUCCESS; |
| 306 | } |
| 307 | |
| 308 | header = (struct pptp_gre_header *)(skb->data); |
| 309 | |
| 310 | /* test if acknowledgement present */ |
| 311 | if (PPTP_GRE_IS_A(header->ver)) { |
| 312 | __u32 ack = (PPTP_GRE_IS_S(header->flags)) ? |
| 313 | header->ack : header->seq; /* ack in different place if S = 0 */ |
| 314 | |
| 315 | ack = ntohl(ack); |
| 316 | |
| 317 | if (ack > opt->ack_recv) |
| 318 | opt->ack_recv = ack; |
| 319 | /* also handle sequence number wrap-around */ |
| 320 | if (WRAPPED(ack, opt->ack_recv)) |
| 321 | opt->ack_recv = ack; |
| 322 | } |
| 323 | |
| 324 | /* test if payload present */ |
| 325 | if (!PPTP_GRE_IS_S(header->flags)) |
| 326 | goto drop; |
| 327 | |
| 328 | headersize = sizeof(*header); |
| 329 | payload_len = ntohs(header->payload_len); |
| 330 | seq = ntohl(header->seq); |
| 331 | |
| 332 | /* no ack present? */ |
| 333 | if (!PPTP_GRE_IS_A(header->ver)) |
| 334 | headersize -= sizeof(header->ack); |
| 335 | /* check for incomplete packet (length smaller than expected) */ |
| 336 | if (skb->len - headersize < payload_len) |
| 337 | goto drop; |
| 338 | |
| 339 | payload = skb->data + headersize; |
| 340 | /* check for expected sequence number */ |
| 341 | if (seq < opt->seq_recv + 1 || WRAPPED(opt->seq_recv, seq)) { |
| 342 | if ((payload[0] == PPP_ALLSTATIONS) && (payload[1] == PPP_UI) && |
| 343 | (PPP_PROTOCOL(payload) == PPP_LCP) && |
| 344 | ((payload[4] == PPP_LCP_ECHOREQ) || (payload[4] == PPP_LCP_ECHOREP))) |
| 345 | goto allow_packet; |
| 346 | } else { |
| 347 | opt->seq_recv = seq; |
| 348 | allow_packet: |
| 349 | skb_pull(skb, headersize); |
| 350 | |
| 351 | if (payload[0] == PPP_ALLSTATIONS && payload[1] == PPP_UI) { |
| 352 | /* chop off address/control */ |
| 353 | if (skb->len < 3) |
| 354 | goto drop; |
| 355 | skb_pull(skb, 2); |
| 356 | } |
| 357 | |
| 358 | if ((*skb->data) & 1) { |
| 359 | /* protocol is compressed */ |
| 360 | skb_push(skb, 1)[0] = 0; |
| 361 | } |
| 362 | |
| 363 | skb->ip_summed = CHECKSUM_NONE; |
| 364 | skb_set_network_header(skb, skb->head-skb->data); |
| 365 | ppp_input(&po->chan, skb); |
| 366 | |
| 367 | return NET_RX_SUCCESS; |
| 368 | } |
| 369 | drop: |
| 370 | kfree_skb(skb); |
| 371 | return NET_RX_DROP; |
| 372 | } |
| 373 | |
| 374 | static int pptp_rcv(struct sk_buff *skb) |
| 375 | { |
| 376 | struct pppox_sock *po; |
| 377 | struct pptp_gre_header *header; |
| 378 | struct iphdr *iph; |
| 379 | |
| 380 | if (skb->pkt_type != PACKET_HOST) |
| 381 | goto drop; |
| 382 | |
| 383 | if (!pskb_may_pull(skb, 12)) |
| 384 | goto drop; |
| 385 | |
| 386 | iph = ip_hdr(skb); |
| 387 | |
| 388 | header = (struct pptp_gre_header *)skb->data; |
| 389 | |
| 390 | if (ntohs(header->protocol) != PPTP_GRE_PROTO || /* PPTP-GRE protocol for PPTP */ |
| 391 | PPTP_GRE_IS_C(header->flags) || /* flag C should be clear */ |
| 392 | PPTP_GRE_IS_R(header->flags) || /* flag R should be clear */ |
| 393 | !PPTP_GRE_IS_K(header->flags) || /* flag K should be set */ |
| 394 | (header->flags&0xF) != 0) /* routing and recursion ctrl = 0 */ |
| 395 | /* if invalid, discard this packet */ |
| 396 | goto drop; |
| 397 | |
| 398 | po = lookup_chan(htons(header->call_id), iph->saddr); |
| 399 | if (po) { |
| 400 | skb_dst_drop(skb); |
| 401 | nf_reset(skb); |
| 402 | return sk_receive_skb(sk_pppox(po), skb, 0); |
| 403 | } |
| 404 | drop: |
| 405 | kfree_skb(skb); |
| 406 | return NET_RX_DROP; |
| 407 | } |
| 408 | |
| 409 | static int pptp_bind(struct socket *sock, struct sockaddr *uservaddr, |
| 410 | int sockaddr_len) |
| 411 | { |
| 412 | struct sock *sk = sock->sk; |
| 413 | struct sockaddr_pppox *sp = (struct sockaddr_pppox *) uservaddr; |
| 414 | struct pppox_sock *po = pppox_sk(sk); |
| 415 | struct pptp_opt *opt = &po->proto.pptp; |
| 416 | int error = 0; |
| 417 | |
| 418 | lock_sock(sk); |
| 419 | |
| 420 | opt->src_addr = sp->sa_addr.pptp; |
| 421 | if (add_chan(po)) { |
| 422 | release_sock(sk); |
| 423 | error = -EBUSY; |
| 424 | } |
| 425 | |
| 426 | release_sock(sk); |
| 427 | return error; |
| 428 | } |
| 429 | |
| 430 | static int pptp_connect(struct socket *sock, struct sockaddr *uservaddr, |
| 431 | int sockaddr_len, int flags) |
| 432 | { |
| 433 | struct sock *sk = sock->sk; |
| 434 | struct sockaddr_pppox *sp = (struct sockaddr_pppox *) uservaddr; |
| 435 | struct pppox_sock *po = pppox_sk(sk); |
| 436 | struct pptp_opt *opt = &po->proto.pptp; |
| 437 | struct rtable *rt; |
David S. Miller | 31e4543d | 2011-05-03 20:25:42 -0700 | [diff] [blame] | 438 | struct flowi4 fl4; |
Dmitry Kozlov | 00959ad | 2010-08-21 23:05:39 -0700 | [diff] [blame] | 439 | int error = 0; |
| 440 | |
| 441 | if (sp->sa_protocol != PX_PROTO_PPTP) |
| 442 | return -EINVAL; |
| 443 | |
| 444 | if (lookup_chan_dst(sp->sa_addr.pptp.call_id, sp->sa_addr.pptp.sin_addr.s_addr)) |
| 445 | return -EALREADY; |
| 446 | |
| 447 | lock_sock(sk); |
| 448 | /* Check for already bound sockets */ |
| 449 | if (sk->sk_state & PPPOX_CONNECTED) { |
| 450 | error = -EBUSY; |
| 451 | goto end; |
| 452 | } |
| 453 | |
| 454 | /* Check for already disconnected sockets, on attempts to disconnect */ |
| 455 | if (sk->sk_state & PPPOX_DEAD) { |
| 456 | error = -EALREADY; |
| 457 | goto end; |
| 458 | } |
| 459 | |
| 460 | if (!opt->src_addr.sin_addr.s_addr || !sp->sa_addr.pptp.sin_addr.s_addr) { |
| 461 | error = -EINVAL; |
| 462 | goto end; |
| 463 | } |
| 464 | |
| 465 | po->chan.private = sk; |
| 466 | po->chan.ops = &pptp_chan_ops; |
| 467 | |
David S. Miller | 31e4543d | 2011-05-03 20:25:42 -0700 | [diff] [blame] | 468 | rt = ip_route_output_ports(&init_net, &fl4, sk, |
David S. Miller | 78fbfd8 | 2011-03-12 00:00:52 -0500 | [diff] [blame] | 469 | opt->dst_addr.sin_addr.s_addr, |
| 470 | opt->src_addr.sin_addr.s_addr, |
| 471 | 0, 0, |
| 472 | IPPROTO_GRE, RT_CONN_FLAGS(sk), 0); |
| 473 | if (IS_ERR(rt)) { |
| 474 | error = -EHOSTUNREACH; |
| 475 | goto end; |
Dmitry Kozlov | 00959ad | 2010-08-21 23:05:39 -0700 | [diff] [blame] | 476 | } |
David S. Miller | 78fbfd8 | 2011-03-12 00:00:52 -0500 | [diff] [blame] | 477 | sk_setup_caps(sk, &rt->dst); |
| 478 | |
Dmitry Kozlov | 00959ad | 2010-08-21 23:05:39 -0700 | [diff] [blame] | 479 | po->chan.mtu = dst_mtu(&rt->dst); |
| 480 | if (!po->chan.mtu) |
| 481 | po->chan.mtu = PPP_MTU; |
| 482 | ip_rt_put(rt); |
| 483 | po->chan.mtu -= PPTP_HEADER_OVERHEAD; |
| 484 | |
| 485 | po->chan.hdrlen = 2 + sizeof(struct pptp_gre_header); |
| 486 | error = ppp_register_channel(&po->chan); |
| 487 | if (error) { |
| 488 | pr_err("PPTP: failed to register PPP channel (%d)\n", error); |
| 489 | goto end; |
| 490 | } |
| 491 | |
| 492 | opt->dst_addr = sp->sa_addr.pptp; |
| 493 | sk->sk_state = PPPOX_CONNECTED; |
| 494 | |
| 495 | end: |
| 496 | release_sock(sk); |
| 497 | return error; |
| 498 | } |
| 499 | |
| 500 | static int pptp_getname(struct socket *sock, struct sockaddr *uaddr, |
| 501 | int *usockaddr_len, int peer) |
| 502 | { |
| 503 | int len = sizeof(struct sockaddr_pppox); |
| 504 | struct sockaddr_pppox sp; |
| 505 | |
| 506 | sp.sa_family = AF_PPPOX; |
| 507 | sp.sa_protocol = PX_PROTO_PPTP; |
| 508 | sp.sa_addr.pptp = pppox_sk(sock->sk)->proto.pptp.src_addr; |
| 509 | |
| 510 | memcpy(uaddr, &sp, len); |
| 511 | |
| 512 | *usockaddr_len = len; |
| 513 | |
| 514 | return 0; |
| 515 | } |
| 516 | |
| 517 | static int pptp_release(struct socket *sock) |
| 518 | { |
| 519 | struct sock *sk = sock->sk; |
| 520 | struct pppox_sock *po; |
| 521 | struct pptp_opt *opt; |
| 522 | int error = 0; |
| 523 | |
| 524 | if (!sk) |
| 525 | return 0; |
| 526 | |
| 527 | lock_sock(sk); |
| 528 | |
| 529 | if (sock_flag(sk, SOCK_DEAD)) { |
| 530 | release_sock(sk); |
| 531 | return -EBADF; |
| 532 | } |
| 533 | |
| 534 | po = pppox_sk(sk); |
| 535 | opt = &po->proto.pptp; |
| 536 | del_chan(po); |
| 537 | |
| 538 | pppox_unbind_sock(sk); |
| 539 | sk->sk_state = PPPOX_DEAD; |
| 540 | |
| 541 | sock_orphan(sk); |
| 542 | sock->sk = NULL; |
| 543 | |
| 544 | release_sock(sk); |
| 545 | sock_put(sk); |
| 546 | |
| 547 | return error; |
| 548 | } |
| 549 | |
| 550 | static void pptp_sock_destruct(struct sock *sk) |
| 551 | { |
| 552 | if (!(sk->sk_state & PPPOX_DEAD)) { |
| 553 | del_chan(pppox_sk(sk)); |
| 554 | pppox_unbind_sock(sk); |
| 555 | } |
| 556 | skb_queue_purge(&sk->sk_receive_queue); |
| 557 | } |
| 558 | |
| 559 | static int pptp_create(struct net *net, struct socket *sock) |
| 560 | { |
| 561 | int error = -ENOMEM; |
| 562 | struct sock *sk; |
| 563 | struct pppox_sock *po; |
| 564 | struct pptp_opt *opt; |
| 565 | |
| 566 | sk = sk_alloc(net, PF_PPPOX, GFP_KERNEL, &pptp_sk_proto); |
| 567 | if (!sk) |
| 568 | goto out; |
| 569 | |
| 570 | sock_init_data(sock, sk); |
| 571 | |
| 572 | sock->state = SS_UNCONNECTED; |
| 573 | sock->ops = &pptp_ops; |
| 574 | |
| 575 | sk->sk_backlog_rcv = pptp_rcv_core; |
| 576 | sk->sk_state = PPPOX_NONE; |
| 577 | sk->sk_type = SOCK_STREAM; |
| 578 | sk->sk_family = PF_PPPOX; |
| 579 | sk->sk_protocol = PX_PROTO_PPTP; |
| 580 | sk->sk_destruct = pptp_sock_destruct; |
| 581 | |
| 582 | po = pppox_sk(sk); |
| 583 | opt = &po->proto.pptp; |
| 584 | |
| 585 | opt->seq_sent = 0; opt->seq_recv = 0; |
| 586 | opt->ack_recv = 0; opt->ack_sent = 0; |
| 587 | |
| 588 | error = 0; |
| 589 | out: |
| 590 | return error; |
| 591 | } |
| 592 | |
| 593 | static int pptp_ppp_ioctl(struct ppp_channel *chan, unsigned int cmd, |
| 594 | unsigned long arg) |
| 595 | { |
| 596 | struct sock *sk = (struct sock *) chan->private; |
| 597 | struct pppox_sock *po = pppox_sk(sk); |
| 598 | struct pptp_opt *opt = &po->proto.pptp; |
| 599 | void __user *argp = (void __user *)arg; |
| 600 | int __user *p = argp; |
| 601 | int err, val; |
| 602 | |
| 603 | err = -EFAULT; |
| 604 | switch (cmd) { |
| 605 | case PPPIOCGFLAGS: |
| 606 | val = opt->ppp_flags; |
| 607 | if (put_user(val, p)) |
| 608 | break; |
| 609 | err = 0; |
| 610 | break; |
| 611 | case PPPIOCSFLAGS: |
| 612 | if (get_user(val, p)) |
| 613 | break; |
| 614 | opt->ppp_flags = val & ~SC_RCV_BITS; |
| 615 | err = 0; |
| 616 | break; |
| 617 | default: |
| 618 | err = -ENOTTY; |
| 619 | } |
| 620 | |
| 621 | return err; |
| 622 | } |
| 623 | |
Eric Dumazet | 756e64a | 2010-09-21 06:43:54 +0000 | [diff] [blame] | 624 | static const struct ppp_channel_ops pptp_chan_ops = { |
Dmitry Kozlov | 00959ad | 2010-08-21 23:05:39 -0700 | [diff] [blame] | 625 | .start_xmit = pptp_xmit, |
| 626 | .ioctl = pptp_ppp_ioctl, |
| 627 | }; |
| 628 | |
| 629 | static struct proto pptp_sk_proto __read_mostly = { |
| 630 | .name = "PPTP", |
| 631 | .owner = THIS_MODULE, |
| 632 | .obj_size = sizeof(struct pppox_sock), |
| 633 | }; |
| 634 | |
| 635 | static const struct proto_ops pptp_ops = { |
| 636 | .family = AF_PPPOX, |
| 637 | .owner = THIS_MODULE, |
| 638 | .release = pptp_release, |
| 639 | .bind = pptp_bind, |
| 640 | .connect = pptp_connect, |
| 641 | .socketpair = sock_no_socketpair, |
| 642 | .accept = sock_no_accept, |
| 643 | .getname = pptp_getname, |
| 644 | .poll = sock_no_poll, |
| 645 | .listen = sock_no_listen, |
| 646 | .shutdown = sock_no_shutdown, |
| 647 | .setsockopt = sock_no_setsockopt, |
| 648 | .getsockopt = sock_no_getsockopt, |
| 649 | .sendmsg = sock_no_sendmsg, |
| 650 | .recvmsg = sock_no_recvmsg, |
| 651 | .mmap = sock_no_mmap, |
| 652 | .ioctl = pppox_ioctl, |
| 653 | }; |
| 654 | |
Eric Dumazet | 756e64a | 2010-09-21 06:43:54 +0000 | [diff] [blame] | 655 | static const struct pppox_proto pppox_pptp_proto = { |
Dmitry Kozlov | 00959ad | 2010-08-21 23:05:39 -0700 | [diff] [blame] | 656 | .create = pptp_create, |
| 657 | .owner = THIS_MODULE, |
| 658 | }; |
| 659 | |
Eric Dumazet | 756e64a | 2010-09-21 06:43:54 +0000 | [diff] [blame] | 660 | static const struct gre_protocol gre_pptp_protocol = { |
Dmitry Kozlov | 00959ad | 2010-08-21 23:05:39 -0700 | [diff] [blame] | 661 | .handler = pptp_rcv, |
| 662 | }; |
| 663 | |
| 664 | static int __init pptp_init_module(void) |
| 665 | { |
| 666 | int err = 0; |
| 667 | pr_info("PPTP driver version " PPTP_DRIVER_VERSION "\n"); |
| 668 | |
Eric Dumazet | 89bf67f | 2010-11-22 00:15:06 +0000 | [diff] [blame] | 669 | callid_sock = vzalloc((MAX_CALLID + 1) * sizeof(void *)); |
Dmitry Kozlov | 00959ad | 2010-08-21 23:05:39 -0700 | [diff] [blame] | 670 | if (!callid_sock) { |
| 671 | pr_err("PPTP: cann't allocate memory\n"); |
| 672 | return -ENOMEM; |
| 673 | } |
| 674 | |
| 675 | err = gre_add_protocol(&gre_pptp_protocol, GREPROTO_PPTP); |
| 676 | if (err) { |
| 677 | pr_err("PPTP: can't add gre protocol\n"); |
| 678 | goto out_mem_free; |
| 679 | } |
| 680 | |
| 681 | err = proto_register(&pptp_sk_proto, 0); |
| 682 | if (err) { |
| 683 | pr_err("PPTP: can't register sk_proto\n"); |
| 684 | goto out_gre_del_protocol; |
| 685 | } |
| 686 | |
| 687 | err = register_pppox_proto(PX_PROTO_PPTP, &pppox_pptp_proto); |
| 688 | if (err) { |
| 689 | pr_err("PPTP: can't register pppox_proto\n"); |
| 690 | goto out_unregister_sk_proto; |
| 691 | } |
| 692 | |
| 693 | return 0; |
| 694 | |
| 695 | out_unregister_sk_proto: |
| 696 | proto_unregister(&pptp_sk_proto); |
| 697 | out_gre_del_protocol: |
| 698 | gre_del_protocol(&gre_pptp_protocol, GREPROTO_PPTP); |
| 699 | out_mem_free: |
| 700 | vfree(callid_sock); |
| 701 | |
| 702 | return err; |
| 703 | } |
| 704 | |
| 705 | static void __exit pptp_exit_module(void) |
| 706 | { |
| 707 | unregister_pppox_proto(PX_PROTO_PPTP); |
| 708 | proto_unregister(&pptp_sk_proto); |
| 709 | gre_del_protocol(&gre_pptp_protocol, GREPROTO_PPTP); |
| 710 | vfree(callid_sock); |
| 711 | } |
| 712 | |
| 713 | module_init(pptp_init_module); |
| 714 | module_exit(pptp_exit_module); |
| 715 | |
| 716 | MODULE_DESCRIPTION("Point-to-Point Tunneling Protocol"); |
| 717 | MODULE_AUTHOR("D. Kozlov (xeb@mail.ru)"); |
| 718 | MODULE_LICENSE("GPL"); |