PageRenderTime 33ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/net/pppopns.c

http://github.com/CyanogenMod/cm-kernel
C | 357 lines | 270 code | 53 blank | 34 comment | 40 complexity | 7fd74658040c8a5c1ed2bc1667305600 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.0
  1. /* drivers/net/pppopns.c
  2. *
  3. * Driver for PPP on PPTP Network Server / PPPoPNS Socket (RFC 2637)
  4. *
  5. * Copyright (C) 2009 Google, Inc.
  6. *
  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.
  10. *
  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.
  15. */
  16. /* This driver handles PPTP data packets between a RAW socket and a PPP channel.
  17. * The socket is created in the kernel space and connected to the same address
  18. * of the control socket. To keep things simple, packets are always sent with
  19. * sequence but without acknowledgement. This driver should work on both IPv4
  20. * and IPv6. */
  21. #include <linux/module.h>
  22. #include <linux/workqueue.h>
  23. #include <linux/skbuff.h>
  24. #include <linux/file.h>
  25. #include <linux/netdevice.h>
  26. #include <linux/net.h>
  27. #include <linux/ppp_defs.h>
  28. #include <linux/if.h>
  29. #include <linux/if_ppp.h>
  30. #include <linux/if_pppox.h>
  31. #include <linux/ppp_channel.h>
  32. #include <asm/uaccess.h>
  33. #define GRE_HEADER_SIZE 8
  34. #define PPTP_GRE_BITS htons(0x2001)
  35. #define PPTP_GRE_BITS_MASK htons(0xEF7F)
  36. #define PPTP_GRE_SEQ_BIT htons(0x1000)
  37. #define PPTP_GRE_ACK_BIT htons(0x0080)
  38. #define PPTP_GRE_TYPE htons(0x880B)
  39. #define PPP_ADDR 0xFF
  40. #define PPP_CTRL 0x03
  41. struct header {
  42. __u16 bits;
  43. __u16 type;
  44. __u16 length;
  45. __u16 call;
  46. __u32 sequence;
  47. } __attribute__((packed));
  48. static int pppopns_recv_core(struct sock *sk_raw, struct sk_buff *skb)
  49. {
  50. struct sock *sk = (struct sock *)sk_raw->sk_user_data;
  51. struct pppopns_opt *opt = &pppox_sk(sk)->proto.pns;
  52. struct header *hdr;
  53. /* Skip transport header */
  54. skb_pull(skb, skb_transport_header(skb) - skb->data);
  55. /* Drop the packet if it is too short. */
  56. if (skb->len < GRE_HEADER_SIZE)
  57. goto drop;
  58. /* Check the header. */
  59. hdr = (struct header *)skb->data;
  60. if (hdr->type != PPTP_GRE_TYPE || hdr->call != opt->local ||
  61. (hdr->bits & PPTP_GRE_BITS_MASK) != PPTP_GRE_BITS)
  62. goto drop;
  63. /* Skip all fields including optional ones. */
  64. if (!skb_pull(skb, GRE_HEADER_SIZE +
  65. (hdr->bits & PPTP_GRE_SEQ_BIT ? 4 : 0) +
  66. (hdr->bits & PPTP_GRE_ACK_BIT ? 4 : 0)))
  67. goto drop;
  68. /* Check the length. */
  69. if (skb->len != ntohs(hdr->length))
  70. goto drop;
  71. /* Skip PPP address and control if they are present. */
  72. if (skb->len >= 2 && skb->data[0] == PPP_ADDR &&
  73. skb->data[1] == PPP_CTRL)
  74. skb_pull(skb, 2);
  75. /* Fix PPP protocol if it is compressed. */
  76. if (skb->len >= 1 && skb->data[0] & 1)
  77. skb_push(skb, 1)[0] = 0;
  78. /* Finally, deliver the packet to PPP channel. */
  79. skb_orphan(skb);
  80. ppp_input(&pppox_sk(sk)->chan, skb);
  81. return NET_RX_SUCCESS;
  82. drop:
  83. kfree_skb(skb);
  84. return NET_RX_DROP;
  85. }
  86. static void pppopns_recv(struct sock *sk_raw, int length)
  87. {
  88. struct sk_buff *skb;
  89. while ((skb = skb_dequeue(&sk_raw->sk_receive_queue))) {
  90. sock_hold(sk_raw);
  91. sk_receive_skb(sk_raw, skb, 0);
  92. }
  93. }
  94. static struct sk_buff_head delivery_queue;
  95. static void pppopns_xmit_core(struct work_struct *delivery_work)
  96. {
  97. mm_segment_t old_fs = get_fs();
  98. struct sk_buff *skb;
  99. set_fs(KERNEL_DS);
  100. while ((skb = skb_dequeue(&delivery_queue))) {
  101. struct sock *sk_raw = skb->sk;
  102. struct kvec iov = {.iov_base = skb->data, .iov_len = skb->len};
  103. struct msghdr msg = {
  104. .msg_iov = (struct iovec *)&iov,
  105. .msg_iovlen = 1,
  106. .msg_flags = MSG_NOSIGNAL | MSG_DONTWAIT,
  107. };
  108. sk_raw->sk_prot->sendmsg(NULL, sk_raw, &msg, skb->len);
  109. kfree_skb(skb);
  110. }
  111. set_fs(old_fs);
  112. }
  113. static DECLARE_WORK(delivery_work, pppopns_xmit_core);
  114. static int pppopns_xmit(struct ppp_channel *chan, struct sk_buff *skb)
  115. {
  116. struct sock *sk_raw = (struct sock *)chan->private;
  117. struct pppopns_opt *opt = &pppox_sk(sk_raw->sk_user_data)->proto.pns;
  118. struct header *hdr;
  119. __u16 length;
  120. /* Install PPP address and control. */
  121. skb_push(skb, 2);
  122. skb->data[0] = PPP_ADDR;
  123. skb->data[1] = PPP_CTRL;
  124. length = skb->len;
  125. /* Install PPTP GRE header. */
  126. hdr = (struct header *)skb_push(skb, 12);
  127. hdr->bits = PPTP_GRE_BITS | PPTP_GRE_SEQ_BIT;
  128. hdr->type = PPTP_GRE_TYPE;
  129. hdr->length = htons(length);
  130. hdr->call = opt->remote;
  131. hdr->sequence = htonl(opt->sequence);
  132. opt->sequence++;
  133. /* Now send the packet via the delivery queue. */
  134. skb_set_owner_w(skb, sk_raw);
  135. skb_queue_tail(&delivery_queue, skb);
  136. schedule_work(&delivery_work);
  137. return 1;
  138. }
  139. /******************************************************************************/
  140. static struct ppp_channel_ops pppopns_channel_ops = {
  141. .start_xmit = pppopns_xmit,
  142. };
  143. static int pppopns_connect(struct socket *sock, struct sockaddr *useraddr,
  144. int addrlen, int flags)
  145. {
  146. struct sock *sk = sock->sk;
  147. struct pppox_sock *po = pppox_sk(sk);
  148. struct sockaddr_pppopns *addr = (struct sockaddr_pppopns *)useraddr;
  149. struct sockaddr_storage ss;
  150. struct socket *sock_tcp = NULL;
  151. struct socket *sock_raw = NULL;
  152. struct sock *sk_tcp;
  153. struct sock *sk_raw;
  154. int error;
  155. if (addrlen != sizeof(struct sockaddr_pppopns))
  156. return -EINVAL;
  157. lock_sock(sk);
  158. error = -EALREADY;
  159. if (sk->sk_state != PPPOX_NONE)
  160. goto out;
  161. sock_tcp = sockfd_lookup(addr->tcp_socket, &error);
  162. if (!sock_tcp)
  163. goto out;
  164. sk_tcp = sock_tcp->sk;
  165. error = -EPROTONOSUPPORT;
  166. if (sk_tcp->sk_protocol != IPPROTO_TCP)
  167. goto out;
  168. addrlen = sizeof(struct sockaddr_storage);
  169. error = kernel_getpeername(sock_tcp, (struct sockaddr *)&ss, &addrlen);
  170. if (error)
  171. goto out;
  172. if (!sk_tcp->sk_bound_dev_if) {
  173. struct dst_entry *dst = sk_dst_get(sk_tcp);
  174. error = -ENODEV;
  175. if (!dst)
  176. goto out;
  177. sk_tcp->sk_bound_dev_if = dst->dev->ifindex;
  178. dst_release(dst);
  179. }
  180. error = sock_create(ss.ss_family, SOCK_RAW, IPPROTO_GRE, &sock_raw);
  181. if (error)
  182. goto out;
  183. sk_raw = sock_raw->sk;
  184. sk_raw->sk_bound_dev_if = sk_tcp->sk_bound_dev_if;
  185. error = kernel_connect(sock_raw, (struct sockaddr *)&ss, addrlen, 0);
  186. if (error)
  187. goto out;
  188. po->chan.hdrlen = 14;
  189. po->chan.private = sk_raw;
  190. po->chan.ops = &pppopns_channel_ops;
  191. po->chan.mtu = PPP_MTU - 80;
  192. po->proto.pns.local = addr->local;
  193. po->proto.pns.remote = addr->remote;
  194. po->proto.pns.data_ready = sk_raw->sk_data_ready;
  195. po->proto.pns.backlog_rcv = sk_raw->sk_backlog_rcv;
  196. error = ppp_register_channel(&po->chan);
  197. if (error)
  198. goto out;
  199. sk->sk_state = PPPOX_CONNECTED;
  200. lock_sock(sk_raw);
  201. sk_raw->sk_data_ready = pppopns_recv;
  202. sk_raw->sk_backlog_rcv = pppopns_recv_core;
  203. sk_raw->sk_user_data = sk;
  204. release_sock(sk_raw);
  205. out:
  206. if (sock_tcp)
  207. sockfd_put(sock_tcp);
  208. if (error && sock_raw)
  209. sock_release(sock_raw);
  210. release_sock(sk);
  211. return error;
  212. }
  213. static int pppopns_release(struct socket *sock)
  214. {
  215. struct sock *sk = sock->sk;
  216. if (!sk)
  217. return 0;
  218. lock_sock(sk);
  219. if (sock_flag(sk, SOCK_DEAD)) {
  220. release_sock(sk);
  221. return -EBADF;
  222. }
  223. if (sk->sk_state != PPPOX_NONE) {
  224. struct sock *sk_raw = (struct sock *)pppox_sk(sk)->chan.private;
  225. lock_sock(sk_raw);
  226. pppox_unbind_sock(sk);
  227. sk_raw->sk_data_ready = pppox_sk(sk)->proto.pns.data_ready;
  228. sk_raw->sk_backlog_rcv = pppox_sk(sk)->proto.pns.backlog_rcv;
  229. sk_raw->sk_user_data = NULL;
  230. release_sock(sk_raw);
  231. sock_release(sk_raw->sk_socket);
  232. }
  233. sock_orphan(sk);
  234. sock->sk = NULL;
  235. release_sock(sk);
  236. sock_put(sk);
  237. return 0;
  238. }
  239. /******************************************************************************/
  240. static struct proto pppopns_proto = {
  241. .name = "PPPOPNS",
  242. .owner = THIS_MODULE,
  243. .obj_size = sizeof(struct pppox_sock),
  244. };
  245. static struct proto_ops pppopns_proto_ops = {
  246. .family = PF_PPPOX,
  247. .owner = THIS_MODULE,
  248. .release = pppopns_release,
  249. .bind = sock_no_bind,
  250. .connect = pppopns_connect,
  251. .socketpair = sock_no_socketpair,
  252. .accept = sock_no_accept,
  253. .getname = sock_no_getname,
  254. .poll = sock_no_poll,
  255. .ioctl = pppox_ioctl,
  256. .listen = sock_no_listen,
  257. .shutdown = sock_no_shutdown,
  258. .setsockopt = sock_no_setsockopt,
  259. .getsockopt = sock_no_getsockopt,
  260. .sendmsg = sock_no_sendmsg,
  261. .recvmsg = sock_no_recvmsg,
  262. .mmap = sock_no_mmap,
  263. };
  264. static int pppopns_create(struct net *net, struct socket *sock)
  265. {
  266. struct sock *sk;
  267. sk = sk_alloc(net, PF_PPPOX, GFP_KERNEL, &pppopns_proto);
  268. if (!sk)
  269. return -ENOMEM;
  270. sock_init_data(sock, sk);
  271. sock->state = SS_UNCONNECTED;
  272. sock->ops = &pppopns_proto_ops;
  273. sk->sk_protocol = PX_PROTO_OPNS;
  274. sk->sk_state = PPPOX_NONE;
  275. return 0;
  276. }
  277. /******************************************************************************/
  278. static struct pppox_proto pppopns_pppox_proto = {
  279. .create = pppopns_create,
  280. .owner = THIS_MODULE,
  281. };
  282. static int __init pppopns_init(void)
  283. {
  284. int error;
  285. error = proto_register(&pppopns_proto, 0);
  286. if (error)
  287. return error;
  288. error = register_pppox_proto(PX_PROTO_OPNS, &pppopns_pppox_proto);
  289. if (error)
  290. proto_unregister(&pppopns_proto);
  291. else
  292. skb_queue_head_init(&delivery_queue);
  293. return error;
  294. }
  295. static void __exit pppopns_exit(void)
  296. {
  297. unregister_pppox_proto(PX_PROTO_OPNS);
  298. proto_unregister(&pppopns_proto);
  299. }
  300. module_init(pppopns_init);
  301. module_exit(pppopns_exit);
  302. MODULE_DESCRIPTION("PPP on PPTP Network Server (PPPoPNS)");
  303. MODULE_AUTHOR("Chia-chi Yeh <chiachi@android.com>");
  304. MODULE_LICENSE("GPL");