pppopns.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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. Outgoing packets are always sent with sequences but
  19. * without acknowledgements. Incoming packets with sequences are reordered
  20. * within a sliding window of one second. Currently reordering only happens when
  21. * a packet is received. It is done for simplicity since no additional locks or
  22. * threads are required. This driver should work on both IPv4 and IPv6. */
  23. #include <linux/module.h>
  24. #include <linux/jiffies.h>
  25. #include <linux/workqueue.h>
  26. #include <linux/skbuff.h>
  27. #include <linux/file.h>
  28. #include <linux/netdevice.h>
  29. #include <linux/net.h>
  30. #include <linux/ppp_defs.h>
  31. #include <linux/if.h>
  32. #include <linux/if_ppp.h>
  33. #include <linux/if_pppox.h>
  34. #include <linux/ppp_channel.h>
  35. #include <asm/uaccess.h>
  36. #define GRE_HEADER_SIZE 8
  37. #define PPTP_GRE_BITS htons(0x2001)
  38. #define PPTP_GRE_BITS_MASK htons(0xEF7F)
  39. #define PPTP_GRE_SEQ_BIT htons(0x1000)
  40. #define PPTP_GRE_ACK_BIT htons(0x0080)
  41. #define PPTP_GRE_TYPE htons(0x880B)
  42. #define PPP_ADDR 0xFF
  43. #define PPP_CTRL 0x03
  44. struct header {
  45. __u16 bits;
  46. __u16 type;
  47. __u16 length;
  48. __u16 call;
  49. __u32 sequence;
  50. } __attribute__((packed));
  51. struct meta {
  52. __u32 sequence;
  53. __u32 timestamp;
  54. };
  55. static inline struct meta *skb_meta(struct sk_buff *skb)
  56. {
  57. return (struct meta *)skb->cb;
  58. }
  59. /******************************************************************************/
  60. static int pppopns_recv_core(struct sock *sk_raw, struct sk_buff *skb)
  61. {
  62. struct sock *sk = (struct sock *)sk_raw->sk_user_data;
  63. struct pppopns_opt *opt = &pppox_sk(sk)->proto.pns;
  64. struct meta *meta = skb_meta(skb);
  65. __u32 now = jiffies;
  66. struct header *hdr;
  67. /* Skip transport header */
  68. skb_pull(skb, skb_transport_header(skb) - skb->data);
  69. /* Drop the packet if GRE header is missing. */
  70. if (skb->len < GRE_HEADER_SIZE)
  71. goto drop;
  72. hdr = (struct header *)skb->data;
  73. /* Check the header. */
  74. if (hdr->type != PPTP_GRE_TYPE || hdr->call != opt->local ||
  75. (hdr->bits & PPTP_GRE_BITS_MASK) != PPTP_GRE_BITS)
  76. goto drop;
  77. /* Skip all fields including optional ones. */
  78. if (!skb_pull(skb, GRE_HEADER_SIZE +
  79. (hdr->bits & PPTP_GRE_SEQ_BIT ? 4 : 0) +
  80. (hdr->bits & PPTP_GRE_ACK_BIT ? 4 : 0)))
  81. goto drop;
  82. /* Check the length. */
  83. if (skb->len != ntohs(hdr->length))
  84. goto drop;
  85. /* Check the sequence if it is present. */
  86. if (hdr->bits & PPTP_GRE_SEQ_BIT) {
  87. meta->sequence = ntohl(hdr->sequence);
  88. if ((__s32)(meta->sequence - opt->recv_sequence) < 0)
  89. goto drop;
  90. }
  91. /* Skip PPP address and control if they are present. */
  92. if (skb->len >= 2 && skb->data[0] == PPP_ADDR &&
  93. skb->data[1] == PPP_CTRL)
  94. skb_pull(skb, 2);
  95. /* Fix PPP protocol if it is compressed. */
  96. if (skb->len >= 1 && skb->data[0] & 1)
  97. skb_push(skb, 1)[0] = 0;
  98. /* Drop the packet if PPP protocol is missing. */
  99. if (skb->len < 2)
  100. goto drop;
  101. /* Perform reordering if sequencing is enabled. */
  102. if (hdr->bits & PPTP_GRE_SEQ_BIT) {
  103. struct sk_buff *skb1;
  104. /* Insert the packet into receive queue in order. */
  105. skb_set_owner_r(skb, sk);
  106. skb_queue_walk(&sk->sk_receive_queue, skb1) {
  107. struct meta *meta1 = skb_meta(skb1);
  108. __s32 order = meta->sequence - meta1->sequence;
  109. if (order == 0)
  110. goto drop;
  111. if (order < 0) {
  112. meta->timestamp = meta1->timestamp;
  113. skb_insert(skb1, skb, &sk->sk_receive_queue);
  114. skb = NULL;
  115. break;
  116. }
  117. }
  118. if (skb) {
  119. meta->timestamp = now;
  120. skb_queue_tail(&sk->sk_receive_queue, skb);
  121. }
  122. /* Remove packets from receive queue as long as
  123. * 1. the receive buffer is full,
  124. * 2. they are queued longer than one second, or
  125. * 3. there are no missing packets before them. */
  126. skb_queue_walk_safe(&sk->sk_receive_queue, skb, skb1) {
  127. meta = skb_meta(skb);
  128. if (atomic_read(&sk->sk_rmem_alloc) < sk->sk_rcvbuf &&
  129. now - meta->timestamp < HZ &&
  130. meta->sequence != opt->recv_sequence)
  131. break;
  132. skb_unlink(skb, &sk->sk_receive_queue);
  133. opt->recv_sequence = meta->sequence + 1;
  134. skb_orphan(skb);
  135. ppp_input(&pppox_sk(sk)->chan, skb);
  136. }
  137. return NET_RX_SUCCESS;
  138. }
  139. /* Flush receive queue if sequencing is disabled. */
  140. skb_queue_purge(&sk->sk_receive_queue);
  141. skb_orphan(skb);
  142. ppp_input(&pppox_sk(sk)->chan, skb);
  143. return NET_RX_SUCCESS;
  144. drop:
  145. kfree_skb(skb);
  146. return NET_RX_DROP;
  147. }
  148. static void pppopns_recv(struct sock *sk_raw, int length)
  149. {
  150. struct sk_buff *skb;
  151. while ((skb = skb_dequeue(&sk_raw->sk_receive_queue))) {
  152. sock_hold(sk_raw);
  153. sk_receive_skb(sk_raw, skb, 0);
  154. }
  155. }
  156. static struct sk_buff_head delivery_queue;
  157. static void pppopns_xmit_core(struct work_struct *delivery_work)
  158. {
  159. mm_segment_t old_fs = get_fs();
  160. struct sk_buff *skb;
  161. set_fs(KERNEL_DS);
  162. while ((skb = skb_dequeue(&delivery_queue))) {
  163. struct sock *sk_raw = skb->sk;
  164. struct kvec iov = {.iov_base = skb->data, .iov_len = skb->len};
  165. struct msghdr msg = {
  166. .msg_iov = (struct iovec *)&iov,
  167. .msg_iovlen = 1,
  168. .msg_flags = MSG_NOSIGNAL | MSG_DONTWAIT,
  169. };
  170. sk_raw->sk_prot->sendmsg(NULL, sk_raw, &msg, skb->len);
  171. kfree_skb(skb);
  172. }
  173. set_fs(old_fs);
  174. }
  175. static DECLARE_WORK(delivery_work, pppopns_xmit_core);
  176. static int pppopns_xmit(struct ppp_channel *chan, struct sk_buff *skb)
  177. {
  178. struct sock *sk_raw = (struct sock *)chan->private;
  179. struct pppopns_opt *opt = &pppox_sk(sk_raw->sk_user_data)->proto.pns;
  180. struct header *hdr;
  181. __u16 length;
  182. /* Install PPP address and control. */
  183. skb_push(skb, 2);
  184. skb->data[0] = PPP_ADDR;
  185. skb->data[1] = PPP_CTRL;
  186. length = skb->len;
  187. /* Install PPTP GRE header. */
  188. hdr = (struct header *)skb_push(skb, 12);
  189. hdr->bits = PPTP_GRE_BITS | PPTP_GRE_SEQ_BIT;
  190. hdr->type = PPTP_GRE_TYPE;
  191. hdr->length = htons(length);
  192. hdr->call = opt->remote;
  193. hdr->sequence = htonl(opt->xmit_sequence);
  194. opt->xmit_sequence++;
  195. /* Now send the packet via the delivery queue. */
  196. skb_set_owner_w(skb, sk_raw);
  197. skb_queue_tail(&delivery_queue, skb);
  198. schedule_work(&delivery_work);
  199. return 1;
  200. }
  201. /******************************************************************************/
  202. static struct ppp_channel_ops pppopns_channel_ops = {
  203. .start_xmit = pppopns_xmit,
  204. };
  205. static int pppopns_connect(struct socket *sock, struct sockaddr *useraddr,
  206. int addrlen, int flags)
  207. {
  208. struct sock *sk = sock->sk;
  209. struct pppox_sock *po = pppox_sk(sk);
  210. struct sockaddr_pppopns *addr = (struct sockaddr_pppopns *)useraddr;
  211. struct sockaddr_storage ss;
  212. struct socket *sock_tcp = NULL;
  213. struct socket *sock_raw = NULL;
  214. struct sock *sk_tcp;
  215. struct sock *sk_raw;
  216. int error;
  217. if (addrlen != sizeof(struct sockaddr_pppopns))
  218. return -EINVAL;
  219. lock_sock(sk);
  220. error = -EALREADY;
  221. if (sk->sk_state != PPPOX_NONE)
  222. goto out;
  223. sock_tcp = sockfd_lookup(addr->tcp_socket, &error);
  224. if (!sock_tcp)
  225. goto out;
  226. sk_tcp = sock_tcp->sk;
  227. error = -EPROTONOSUPPORT;
  228. if (sk_tcp->sk_protocol != IPPROTO_TCP)
  229. goto out;
  230. addrlen = sizeof(struct sockaddr_storage);
  231. error = kernel_getpeername(sock_tcp, (struct sockaddr *)&ss, &addrlen);
  232. if (error)
  233. goto out;
  234. if (!sk_tcp->sk_bound_dev_if) {
  235. struct dst_entry *dst = sk_dst_get(sk_tcp);
  236. error = -ENODEV;
  237. if (!dst)
  238. goto out;
  239. sk_tcp->sk_bound_dev_if = dst->dev->ifindex;
  240. dst_release(dst);
  241. }
  242. error = sock_create(ss.ss_family, SOCK_RAW, IPPROTO_GRE, &sock_raw);
  243. if (error)
  244. goto out;
  245. sk_raw = sock_raw->sk;
  246. sk_raw->sk_bound_dev_if = sk_tcp->sk_bound_dev_if;
  247. error = kernel_connect(sock_raw, (struct sockaddr *)&ss, addrlen, 0);
  248. if (error)
  249. goto out;
  250. po->chan.hdrlen = 14;
  251. po->chan.private = sk_raw;
  252. po->chan.ops = &pppopns_channel_ops;
  253. po->chan.mtu = PPP_MTU - 80;
  254. po->proto.pns.local = addr->local;
  255. po->proto.pns.remote = addr->remote;
  256. po->proto.pns.data_ready = sk_raw->sk_data_ready;
  257. po->proto.pns.backlog_rcv = sk_raw->sk_backlog_rcv;
  258. error = ppp_register_channel(&po->chan);
  259. if (error)
  260. goto out;
  261. sk->sk_state = PPPOX_CONNECTED;
  262. lock_sock(sk_raw);
  263. sk_raw->sk_data_ready = pppopns_recv;
  264. sk_raw->sk_backlog_rcv = pppopns_recv_core;
  265. sk_raw->sk_user_data = sk;
  266. release_sock(sk_raw);
  267. out:
  268. if (sock_tcp)
  269. sockfd_put(sock_tcp);
  270. if (error && sock_raw)
  271. sock_release(sock_raw);
  272. release_sock(sk);
  273. return error;
  274. }
  275. static int pppopns_release(struct socket *sock)
  276. {
  277. struct sock *sk = sock->sk;
  278. if (!sk)
  279. return 0;
  280. lock_sock(sk);
  281. if (sock_flag(sk, SOCK_DEAD)) {
  282. release_sock(sk);
  283. return -EBADF;
  284. }
  285. if (sk->sk_state != PPPOX_NONE) {
  286. struct sock *sk_raw = (struct sock *)pppox_sk(sk)->chan.private;
  287. lock_sock(sk_raw);
  288. skb_queue_purge(&sk->sk_receive_queue);
  289. pppox_unbind_sock(sk);
  290. sk_raw->sk_data_ready = pppox_sk(sk)->proto.pns.data_ready;
  291. sk_raw->sk_backlog_rcv = pppox_sk(sk)->proto.pns.backlog_rcv;
  292. sk_raw->sk_user_data = NULL;
  293. release_sock(sk_raw);
  294. sock_release(sk_raw->sk_socket);
  295. }
  296. sock_orphan(sk);
  297. sock->sk = NULL;
  298. release_sock(sk);
  299. sock_put(sk);
  300. return 0;
  301. }
  302. /******************************************************************************/
  303. static struct proto pppopns_proto = {
  304. .name = "PPPOPNS",
  305. .owner = THIS_MODULE,
  306. .obj_size = sizeof(struct pppox_sock),
  307. };
  308. static struct proto_ops pppopns_proto_ops = {
  309. .family = PF_PPPOX,
  310. .owner = THIS_MODULE,
  311. .release = pppopns_release,
  312. .bind = sock_no_bind,
  313. .connect = pppopns_connect,
  314. .socketpair = sock_no_socketpair,
  315. .accept = sock_no_accept,
  316. .getname = sock_no_getname,
  317. .poll = sock_no_poll,
  318. .ioctl = pppox_ioctl,
  319. .listen = sock_no_listen,
  320. .shutdown = sock_no_shutdown,
  321. .setsockopt = sock_no_setsockopt,
  322. .getsockopt = sock_no_getsockopt,
  323. .sendmsg = sock_no_sendmsg,
  324. .recvmsg = sock_no_recvmsg,
  325. .mmap = sock_no_mmap,
  326. };
  327. static int pppopns_create(struct net *net, struct socket *sock)
  328. {
  329. struct sock *sk;
  330. sk = sk_alloc(net, PF_PPPOX, GFP_KERNEL, &pppopns_proto);
  331. if (!sk)
  332. return -ENOMEM;
  333. sock_init_data(sock, sk);
  334. sock->state = SS_UNCONNECTED;
  335. sock->ops = &pppopns_proto_ops;
  336. sk->sk_protocol = PX_PROTO_OPNS;
  337. sk->sk_state = PPPOX_NONE;
  338. return 0;
  339. }
  340. /******************************************************************************/
  341. static struct pppox_proto pppopns_pppox_proto = {
  342. .create = pppopns_create,
  343. .owner = THIS_MODULE,
  344. };
  345. static int __init pppopns_init(void)
  346. {
  347. int error;
  348. error = proto_register(&pppopns_proto, 0);
  349. if (error)
  350. return error;
  351. error = register_pppox_proto(PX_PROTO_OPNS, &pppopns_pppox_proto);
  352. if (error)
  353. proto_unregister(&pppopns_proto);
  354. else
  355. skb_queue_head_init(&delivery_queue);
  356. return error;
  357. }
  358. static void __exit pppopns_exit(void)
  359. {
  360. unregister_pppox_proto(PX_PROTO_OPNS);
  361. proto_unregister(&pppopns_proto);
  362. }
  363. module_init(pppopns_init);
  364. module_exit(pppopns_exit);
  365. MODULE_DESCRIPTION("PPP on PPTP Network Server (PPPoPNS)");
  366. MODULE_AUTHOR("Chia-chi Yeh <chiachi@android.com>");
  367. MODULE_LICENSE("GPL");