pppopns.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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. static void recv_queue_timer_callback(unsigned long data);
  60. static void traverse_receive_queue(struct sock *sk)
  61. {
  62. struct pppox_sock *po = pppox_sk(sk);
  63. struct pppopns_opt *opt = &pppox_sk(sk)->proto.pns;
  64. struct sk_buff *skb;
  65. struct sk_buff *skb1;
  66. struct meta *meta;
  67. __u32 now = jiffies;
  68. /* Remove packets from receive queue as long as
  69. * 1. the receive buffer is full,
  70. * 2. they are queued longer than one second, or
  71. * 3. there are no missing packets before them. */
  72. skb_queue_walk_safe(&sk->sk_receive_queue, skb, skb1) {
  73. meta = skb_meta(skb);
  74. if (atomic_read(&sk->sk_rmem_alloc) < sk->sk_rcvbuf &&
  75. now - meta->timestamp < (HZ - 5) &&
  76. meta->sequence != opt->recv_sequence)
  77. break;
  78. skb_unlink(skb, &sk->sk_receive_queue);
  79. opt->recv_sequence = meta->sequence + 1;
  80. skb_orphan(skb);
  81. ppp_input(&po->chan, skb);
  82. }
  83. if (skb_queue_len(&sk->sk_receive_queue) > 0) {
  84. /* Start the timer. The timer will
  85. expire after one second. When the
  86. timer expires, the receive_queue is
  87. checked and all packets older than
  88. one second are removed from the queue and
  89. passed forward. */
  90. if (timer_pending(&po->recv_queue_timer)) {
  91. /* Something is wrong. Recv timer is already active. However, Ignoring...*/
  92. } else {
  93. init_timer(&po->recv_queue_timer);
  94. po->recv_queue_timer.data = (unsigned long)sk;
  95. po->recv_queue_timer.function = recv_queue_timer_callback;
  96. po->recv_queue_timer.expires = now + HZ;
  97. add_timer(&po->recv_queue_timer);
  98. }
  99. }
  100. }
  101. static void recv_queue_timer_callback(unsigned long data)
  102. {
  103. struct sock *sk = (struct sock *)data;
  104. unsigned long flags;
  105. spin_lock_irqsave(&pppox_sk(sk)->recv_queue_lock, flags);
  106. traverse_receive_queue(sk);
  107. spin_unlock_irqrestore(&pppox_sk(sk)->recv_queue_lock, flags);
  108. }
  109. /******************************************************************************/
  110. static int pppopns_recv_core(struct sock *sk_raw, struct sk_buff *skb)
  111. {
  112. struct sock *sk = (struct sock *)sk_raw->sk_user_data;
  113. struct pppopns_opt *opt = &pppox_sk(sk)->proto.pns;
  114. struct meta *meta = skb_meta(skb);
  115. __u32 now = jiffies;
  116. struct header *hdr;
  117. unsigned long flags;
  118. /* Skip transport header */
  119. skb_pull(skb, skb_transport_header(skb) - skb->data);
  120. /* Drop the packet if GRE header is missing. */
  121. if (skb->len < GRE_HEADER_SIZE)
  122. goto drop;
  123. hdr = (struct header *)skb->data;
  124. /* Check the header. */
  125. if (hdr->type != PPTP_GRE_TYPE || hdr->call != opt->local ||
  126. (hdr->bits & PPTP_GRE_BITS_MASK) != PPTP_GRE_BITS)
  127. goto drop;
  128. /* Skip all fields including optional ones. */
  129. if (!skb_pull(skb, GRE_HEADER_SIZE +
  130. (hdr->bits & PPTP_GRE_SEQ_BIT ? 4 : 0) +
  131. (hdr->bits & PPTP_GRE_ACK_BIT ? 4 : 0)))
  132. goto drop;
  133. /* Check the length. */
  134. if (skb->len != ntohs(hdr->length))
  135. goto drop;
  136. /* Check the sequence if it is present. */
  137. if (hdr->bits & PPTP_GRE_SEQ_BIT) {
  138. meta->sequence = ntohl(hdr->sequence);
  139. if ((__s32)(meta->sequence - opt->recv_sequence) < 0)
  140. goto drop;
  141. }
  142. /* Skip PPP address and control if they are present. */
  143. if (skb->len >= 2 && skb->data[0] == PPP_ADDR &&
  144. skb->data[1] == PPP_CTRL)
  145. skb_pull(skb, 2);
  146. /* Fix PPP protocol if it is compressed. */
  147. if (skb->len >= 1 && skb->data[0] & 1)
  148. skb_push(skb, 1)[0] = 0;
  149. /* Drop the packet if PPP protocol is missing. */
  150. if (skb->len < 2)
  151. goto drop;
  152. /* Perform reordering if sequencing is enabled. */
  153. if (hdr->bits & PPTP_GRE_SEQ_BIT) {
  154. struct sk_buff *skb1;
  155. if (timer_pending(&pppox_sk(sk)->recv_queue_timer)) {
  156. del_timer_sync(&pppox_sk(sk)->recv_queue_timer);
  157. }
  158. spin_lock_irqsave(&pppox_sk(sk)->recv_queue_lock, flags);
  159. /* Insert the packet into receive queue in order. */
  160. skb_set_owner_r(skb, sk);
  161. skb_queue_walk(&sk->sk_receive_queue, skb1) {
  162. struct meta *meta1 = skb_meta(skb1);
  163. __s32 order = meta->sequence - meta1->sequence;
  164. if (order == 0) {
  165. spin_unlock_irqrestore(&pppox_sk(sk)->recv_queue_lock, flags);
  166. goto drop;
  167. }
  168. if (order < 0) {
  169. meta->timestamp = meta1->timestamp;
  170. skb_insert(skb1, skb, &sk->sk_receive_queue);
  171. skb = NULL;
  172. break;
  173. }
  174. }
  175. if (skb) {
  176. meta->timestamp = now;
  177. skb_queue_tail(&sk->sk_receive_queue, skb);
  178. }
  179. traverse_receive_queue(sk);
  180. spin_unlock_irqrestore(&pppox_sk(sk)->recv_queue_lock, flags);
  181. return NET_RX_SUCCESS;
  182. }
  183. /* Flush receive queue if sequencing is disabled. */
  184. skb_queue_purge(&sk->sk_receive_queue);
  185. skb_orphan(skb);
  186. ppp_input(&pppox_sk(sk)->chan, skb);
  187. return NET_RX_SUCCESS;
  188. drop:
  189. kfree_skb(skb);
  190. return NET_RX_DROP;
  191. }
  192. static void pppopns_recv(struct sock *sk_raw, int length)
  193. {
  194. struct sk_buff *skb;
  195. while ((skb = skb_dequeue(&sk_raw->sk_receive_queue))) {
  196. sock_hold(sk_raw);
  197. sk_receive_skb(sk_raw, skb, 0);
  198. }
  199. }
  200. static struct sk_buff_head delivery_queue;
  201. static void pppopns_xmit_core(struct work_struct *delivery_work)
  202. {
  203. mm_segment_t old_fs = get_fs();
  204. struct sk_buff *skb;
  205. set_fs(KERNEL_DS);
  206. while ((skb = skb_dequeue(&delivery_queue))) {
  207. struct sock *sk_raw = skb->sk;
  208. struct kvec iov = {.iov_base = skb->data, .iov_len = skb->len};
  209. struct msghdr msg = {
  210. .msg_iov = (struct iovec *)&iov,
  211. .msg_iovlen = 1,
  212. .msg_flags = MSG_NOSIGNAL | MSG_DONTWAIT,
  213. };
  214. sk_raw->sk_prot->sendmsg(NULL, sk_raw, &msg, skb->len);
  215. kfree_skb(skb);
  216. }
  217. set_fs(old_fs);
  218. }
  219. static DECLARE_WORK(delivery_work, pppopns_xmit_core);
  220. static int pppopns_xmit(struct ppp_channel *chan, struct sk_buff *skb)
  221. {
  222. struct sock *sk_raw = (struct sock *)chan->private;
  223. struct pppopns_opt *opt = &pppox_sk(sk_raw->sk_user_data)->proto.pns;
  224. struct header *hdr;
  225. __u16 length;
  226. /* Install PPP address and control. */
  227. skb_push(skb, 2);
  228. skb->data[0] = PPP_ADDR;
  229. skb->data[1] = PPP_CTRL;
  230. length = skb->len;
  231. /* Install PPTP GRE header. */
  232. hdr = (struct header *)skb_push(skb, 12);
  233. hdr->bits = PPTP_GRE_BITS | PPTP_GRE_SEQ_BIT;
  234. hdr->type = PPTP_GRE_TYPE;
  235. hdr->length = htons(length);
  236. hdr->call = opt->remote;
  237. hdr->sequence = htonl(opt->xmit_sequence);
  238. opt->xmit_sequence++;
  239. /* Now send the packet via the delivery queue. */
  240. skb_set_owner_w(skb, sk_raw);
  241. skb_queue_tail(&delivery_queue, skb);
  242. schedule_work(&delivery_work);
  243. return 1;
  244. }
  245. /******************************************************************************/
  246. static struct ppp_channel_ops pppopns_channel_ops = {
  247. .start_xmit = pppopns_xmit,
  248. };
  249. static int pppopns_connect(struct socket *sock, struct sockaddr *useraddr,
  250. int addrlen, int flags)
  251. {
  252. struct sock *sk = sock->sk;
  253. struct pppox_sock *po = pppox_sk(sk);
  254. struct sockaddr_pppopns *addr = (struct sockaddr_pppopns *)useraddr;
  255. struct sockaddr_storage ss;
  256. struct socket *sock_tcp = NULL;
  257. struct socket *sock_raw = NULL;
  258. struct sock *sk_tcp;
  259. struct sock *sk_raw;
  260. int error;
  261. if (addrlen != sizeof(struct sockaddr_pppopns))
  262. return -EINVAL;
  263. lock_sock(sk);
  264. error = -EALREADY;
  265. if (sk->sk_state != PPPOX_NONE)
  266. goto out;
  267. sock_tcp = sockfd_lookup(addr->tcp_socket, &error);
  268. if (!sock_tcp)
  269. goto out;
  270. sk_tcp = sock_tcp->sk;
  271. error = -EPROTONOSUPPORT;
  272. if (sk_tcp->sk_protocol != IPPROTO_TCP)
  273. goto out;
  274. addrlen = sizeof(struct sockaddr_storage);
  275. error = kernel_getpeername(sock_tcp, (struct sockaddr *)&ss, &addrlen);
  276. if (error)
  277. goto out;
  278. if (!sk_tcp->sk_bound_dev_if) {
  279. struct dst_entry *dst = sk_dst_get(sk_tcp);
  280. error = -ENODEV;
  281. if (!dst)
  282. goto out;
  283. sk_tcp->sk_bound_dev_if = dst->dev->ifindex;
  284. dst_release(dst);
  285. }
  286. error = sock_create(ss.ss_family, SOCK_RAW, IPPROTO_GRE, &sock_raw);
  287. if (error)
  288. goto out;
  289. sk_raw = sock_raw->sk;
  290. sk_raw->sk_bound_dev_if = sk_tcp->sk_bound_dev_if;
  291. error = kernel_connect(sock_raw, (struct sockaddr *)&ss, addrlen, 0);
  292. if (error)
  293. goto out;
  294. po->chan.hdrlen = 14;
  295. po->chan.private = sk_raw;
  296. po->chan.ops = &pppopns_channel_ops;
  297. po->chan.mtu = PPP_MRU - 80;
  298. po->proto.pns.local = addr->local;
  299. po->proto.pns.remote = addr->remote;
  300. po->proto.pns.data_ready = sk_raw->sk_data_ready;
  301. po->proto.pns.backlog_rcv = sk_raw->sk_backlog_rcv;
  302. error = ppp_register_channel(&po->chan);
  303. if (error)
  304. goto out;
  305. sk->sk_state = PPPOX_CONNECTED;
  306. lock_sock(sk_raw);
  307. sk_raw->sk_data_ready = pppopns_recv;
  308. sk_raw->sk_backlog_rcv = pppopns_recv_core;
  309. sk_raw->sk_user_data = sk;
  310. release_sock(sk_raw);
  311. out:
  312. if (sock_tcp)
  313. sockfd_put(sock_tcp);
  314. if (error && sock_raw)
  315. sock_release(sock_raw);
  316. release_sock(sk);
  317. return error;
  318. }
  319. static int pppopns_release(struct socket *sock)
  320. {
  321. struct sock *sk = sock->sk;
  322. struct pppox_sock *po = pppox_sk(sk);
  323. unsigned long flags;
  324. if (!sk)
  325. return 0;
  326. lock_sock(sk);
  327. if (sock_flag(sk, SOCK_DEAD)) {
  328. release_sock(sk);
  329. return -EBADF;
  330. }
  331. if (po) {
  332. spin_lock_irqsave(&po->recv_queue_lock, flags);
  333. if (po && timer_pending( &po->recv_queue_timer )) {
  334. del_timer_sync( &po->recv_queue_timer );
  335. }
  336. spin_unlock_irqrestore(&po->recv_queue_lock, flags);
  337. }
  338. if (sk->sk_state != PPPOX_NONE) {
  339. struct sock *sk_raw = (struct sock *)pppox_sk(sk)->chan.private;
  340. lock_sock(sk_raw);
  341. skb_queue_purge(&sk->sk_receive_queue);
  342. pppox_unbind_sock(sk);
  343. sk_raw->sk_data_ready = pppox_sk(sk)->proto.pns.data_ready;
  344. sk_raw->sk_backlog_rcv = pppox_sk(sk)->proto.pns.backlog_rcv;
  345. sk_raw->sk_user_data = NULL;
  346. release_sock(sk_raw);
  347. sock_release(sk_raw->sk_socket);
  348. }
  349. sock_orphan(sk);
  350. sock->sk = NULL;
  351. release_sock(sk);
  352. sock_put(sk);
  353. return 0;
  354. }
  355. /******************************************************************************/
  356. static struct proto pppopns_proto = {
  357. .name = "PPPOPNS",
  358. .owner = THIS_MODULE,
  359. .obj_size = sizeof(struct pppox_sock),
  360. };
  361. static struct proto_ops pppopns_proto_ops = {
  362. .family = PF_PPPOX,
  363. .owner = THIS_MODULE,
  364. .release = pppopns_release,
  365. .bind = sock_no_bind,
  366. .connect = pppopns_connect,
  367. .socketpair = sock_no_socketpair,
  368. .accept = sock_no_accept,
  369. .getname = sock_no_getname,
  370. .poll = sock_no_poll,
  371. .ioctl = pppox_ioctl,
  372. .listen = sock_no_listen,
  373. .shutdown = sock_no_shutdown,
  374. .setsockopt = sock_no_setsockopt,
  375. .getsockopt = sock_no_getsockopt,
  376. .sendmsg = sock_no_sendmsg,
  377. .recvmsg = sock_no_recvmsg,
  378. .mmap = sock_no_mmap,
  379. };
  380. static int pppopns_create(struct net *net, struct socket *sock)
  381. {
  382. struct sock *sk;
  383. struct pppox_sock *po;
  384. struct pppopns_opt *opt;
  385. sk = sk_alloc(net, PF_PPPOX, GFP_KERNEL, &pppopns_proto);
  386. if (!sk)
  387. return -ENOMEM;
  388. sock_init_data(sock, sk);
  389. sock->state = SS_UNCONNECTED;
  390. sock->ops = &pppopns_proto_ops;
  391. sk->sk_protocol = PX_PROTO_OPNS;
  392. sk->sk_state = PPPOX_NONE;
  393. po = pppox_sk(sk);
  394. opt = &po->proto.pns;
  395. opt->ppp_flags = SC_GRE_SEQ_CHK;
  396. init_timer(&po->recv_queue_timer);
  397. spin_lock_init(&po->recv_queue_lock);
  398. return 0;
  399. }
  400. static int pppopns_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  401. {
  402. struct sock *sk = sock->sk;
  403. struct pppox_sock *po = pppox_sk(sk);
  404. struct pppopns_opt *opt = &po->proto.pns;
  405. void __user *argp = (void __user *)arg;
  406. int __user *p = argp;
  407. int err = -ENOTTY, val;
  408. switch (cmd) {
  409. case PPPIOCGFLAGS:
  410. printk("Getting pppopns socket flags.\n");
  411. val = opt->ppp_flags;
  412. if (put_user(val, p))
  413. break;
  414. err = 0;
  415. break;
  416. case PPPIOCSFLAGS:
  417. printk("Setting pppopns socket flags.\n");
  418. if (get_user(val, p))
  419. break;
  420. opt->ppp_flags = val;
  421. err = 0;
  422. break;
  423. }
  424. return err;
  425. }
  426. /******************************************************************************/
  427. static struct pppox_proto pppopns_pppox_proto = {
  428. .create = pppopns_create,
  429. .ioctl = pppopns_ioctl,
  430. .owner = THIS_MODULE,
  431. };
  432. static int __init pppopns_init(void)
  433. {
  434. int error;
  435. error = proto_register(&pppopns_proto, 0);
  436. if (error)
  437. return error;
  438. error = register_pppox_proto(PX_PROTO_OPNS, &pppopns_pppox_proto);
  439. if (error)
  440. proto_unregister(&pppopns_proto);
  441. else
  442. skb_queue_head_init(&delivery_queue);
  443. return error;
  444. }
  445. static void __exit pppopns_exit(void)
  446. {
  447. unregister_pppox_proto(PX_PROTO_OPNS);
  448. proto_unregister(&pppopns_proto);
  449. }
  450. module_init(pppopns_init);
  451. module_exit(pppopns_exit);
  452. MODULE_DESCRIPTION("PPP on PPTP Network Server (PPPoPNS)");
  453. MODULE_AUTHOR("Chia-chi Yeh <chiachi@android.com>");
  454. MODULE_LICENSE("GPL");