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