pppolac.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /* drivers/net/pppolac.c
  2. *
  3. * Driver for PPP on L2TP Access Concentrator / PPPoLAC Socket (RFC 2661)
  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 L2TP data packets between a UDP socket and a PPP channel.
  17. * The socket must keep connected, and only one session per socket is permitted.
  18. * Sequencing of outgoing packets is controlled by LNS. Incoming packets with
  19. * sequences are reordered within a sliding window of one second. Currently
  20. * reordering only happens when a packet is received. It is done for simplicity
  21. * since no additional locks or threads are required. This driver only works on
  22. * IPv4 due to the lack of UDP encapsulation support in 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/udp.h>
  31. #include <linux/ppp_defs.h>
  32. #include <linux/if_ppp.h>
  33. #include <linux/if_pppox.h>
  34. #include <linux/ppp_channel.h>
  35. #include <net/tcp_states.h>
  36. #include <asm/uaccess.h>
  37. #define L2TP_CONTROL_BIT 0x80
  38. #define L2TP_LENGTH_BIT 0x40
  39. #define L2TP_SEQUENCE_BIT 0x08
  40. #define L2TP_OFFSET_BIT 0x02
  41. #define L2TP_VERSION 0x02
  42. #define L2TP_VERSION_MASK 0x0F
  43. #define PPP_ADDR 0xFF
  44. #define PPP_CTRL 0x03
  45. union unaligned {
  46. __u32 u32;
  47. } __attribute__((packed));
  48. static inline union unaligned *unaligned(void *ptr)
  49. {
  50. return (union unaligned *)ptr;
  51. }
  52. struct meta {
  53. __u32 sequence;
  54. __u32 timestamp;
  55. };
  56. static inline struct meta *skb_meta(struct sk_buff *skb)
  57. {
  58. return (struct meta *)skb->cb;
  59. }
  60. /******************************************************************************/
  61. static int pppolac_recv_core(struct sock *sk_udp, struct sk_buff *skb)
  62. {
  63. struct sock *sk = (struct sock *)sk_udp->sk_user_data;
  64. struct pppolac_opt *opt = &pppox_sk(sk)->proto.lac;
  65. struct meta *meta = skb_meta(skb);
  66. __u32 now = jiffies;
  67. __u8 bits;
  68. __u8 *ptr;
  69. /* Drop the packet if L2TP header is missing. */
  70. if (skb->len < sizeof(struct udphdr) + 6)
  71. goto drop;
  72. /* Put it back if it is a control packet. */
  73. if (skb->data[sizeof(struct udphdr)] & L2TP_CONTROL_BIT)
  74. return opt->backlog_rcv(sk_udp, skb);
  75. /* Skip UDP header. */
  76. skb_pull(skb, sizeof(struct udphdr));
  77. /* Check the version. */
  78. if ((skb->data[1] & L2TP_VERSION_MASK) != L2TP_VERSION)
  79. goto drop;
  80. bits = skb->data[0];
  81. ptr = &skb->data[2];
  82. /* Check the length if it is present. */
  83. if (bits & L2TP_LENGTH_BIT) {
  84. if ((ptr[0] << 8 | ptr[1]) != skb->len)
  85. goto drop;
  86. ptr += 2;
  87. }
  88. /* Skip all fields including optional ones. */
  89. if (!skb_pull(skb, 6 + (bits & L2TP_SEQUENCE_BIT ? 4 : 0) +
  90. (bits & L2TP_LENGTH_BIT ? 2 : 0) +
  91. (bits & L2TP_OFFSET_BIT ? 2 : 0)))
  92. goto drop;
  93. /* Skip the offset padding if it is present. */
  94. if (bits & L2TP_OFFSET_BIT &&
  95. !skb_pull(skb, skb->data[-2] << 8 | skb->data[-1]))
  96. goto drop;
  97. /* Check the tunnel and the session. */
  98. if (unaligned(ptr)->u32 != opt->local)
  99. goto drop;
  100. /* Check the sequence if it is present. */
  101. if (bits & L2TP_SEQUENCE_BIT) {
  102. meta->sequence = ptr[4] << 8 | ptr[5];
  103. if ((__s16)(meta->sequence - opt->recv_sequence) < 0)
  104. goto drop;
  105. }
  106. /* Skip PPP address and control if they are present. */
  107. if (skb->len >= 2 && skb->data[0] == PPP_ADDR &&
  108. skb->data[1] == PPP_CTRL)
  109. skb_pull(skb, 2);
  110. /* Fix PPP protocol if it is compressed. */
  111. if (skb->len >= 1 && skb->data[0] & 1)
  112. skb_push(skb, 1)[0] = 0;
  113. /* Drop the packet if PPP protocol is missing. */
  114. if (skb->len < 2)
  115. goto drop;
  116. /* Perform reordering if sequencing is enabled. */
  117. atomic_set(&opt->sequencing, bits & L2TP_SEQUENCE_BIT);
  118. if (bits & L2TP_SEQUENCE_BIT) {
  119. struct sk_buff *skb1;
  120. /* Insert the packet into receive queue in order. */
  121. skb_set_owner_r(skb, sk);
  122. skb_queue_walk(&sk->sk_receive_queue, skb1) {
  123. struct meta *meta1 = skb_meta(skb1);
  124. __s16 order = meta->sequence - meta1->sequence;
  125. if (order == 0)
  126. goto drop;
  127. if (order < 0) {
  128. meta->timestamp = meta1->timestamp;
  129. skb_insert(skb1, skb, &sk->sk_receive_queue);
  130. skb = NULL;
  131. break;
  132. }
  133. }
  134. if (skb) {
  135. meta->timestamp = now;
  136. skb_queue_tail(&sk->sk_receive_queue, skb);
  137. }
  138. /* Remove packets from receive queue as long as
  139. * 1. the receive buffer is full,
  140. * 2. they are queued longer than one second, or
  141. * 3. there are no missing packets before them. */
  142. skb_queue_walk_safe(&sk->sk_receive_queue, skb, skb1) {
  143. meta = skb_meta(skb);
  144. if (atomic_read(&sk->sk_rmem_alloc) < sk->sk_rcvbuf &&
  145. now - meta->timestamp < HZ &&
  146. meta->sequence != opt->recv_sequence)
  147. break;
  148. skb_unlink(skb, &sk->sk_receive_queue);
  149. opt->recv_sequence = (__u16)(meta->sequence + 1);
  150. skb_orphan(skb);
  151. ppp_input(&pppox_sk(sk)->chan, skb);
  152. }
  153. return NET_RX_SUCCESS;
  154. }
  155. /* Flush receive queue if sequencing is disabled. */
  156. skb_queue_purge(&sk->sk_receive_queue);
  157. skb_orphan(skb);
  158. ppp_input(&pppox_sk(sk)->chan, skb);
  159. return NET_RX_SUCCESS;
  160. drop:
  161. kfree_skb(skb);
  162. return NET_RX_DROP;
  163. }
  164. static int pppolac_recv(struct sock *sk_udp, struct sk_buff *skb)
  165. {
  166. sock_hold(sk_udp);
  167. sk_receive_skb(sk_udp, skb, 0);
  168. return 0;
  169. }
  170. static struct sk_buff_head delivery_queue;
  171. static void pppolac_xmit_core(struct work_struct *delivery_work)
  172. {
  173. mm_segment_t old_fs = get_fs();
  174. struct sk_buff *skb;
  175. set_fs(KERNEL_DS);
  176. while ((skb = skb_dequeue(&delivery_queue))) {
  177. struct sock *sk_udp = skb->sk;
  178. struct kvec iov = {.iov_base = skb->data, .iov_len = skb->len};
  179. struct msghdr msg = {
  180. .msg_iov = (struct iovec *)&iov,
  181. .msg_iovlen = 1,
  182. .msg_flags = MSG_NOSIGNAL | MSG_DONTWAIT,
  183. };
  184. sk_udp->sk_prot->sendmsg(NULL, sk_udp, &msg, skb->len);
  185. kfree_skb(skb);
  186. }
  187. set_fs(old_fs);
  188. }
  189. static DECLARE_WORK(delivery_work, pppolac_xmit_core);
  190. static int pppolac_xmit(struct ppp_channel *chan, struct sk_buff *skb)
  191. {
  192. struct sock *sk_udp = (struct sock *)chan->private;
  193. struct pppolac_opt *opt = &pppox_sk(sk_udp->sk_user_data)->proto.lac;
  194. /* Install PPP address and control. */
  195. skb_push(skb, 2);
  196. skb->data[0] = PPP_ADDR;
  197. skb->data[1] = PPP_CTRL;
  198. /* Install L2TP header. */
  199. if (atomic_read(&opt->sequencing)) {
  200. skb_push(skb, 10);
  201. skb->data[0] = L2TP_SEQUENCE_BIT;
  202. skb->data[6] = opt->xmit_sequence >> 8;
  203. skb->data[7] = opt->xmit_sequence;
  204. skb->data[8] = 0;
  205. skb->data[9] = 0;
  206. opt->xmit_sequence++;
  207. } else {
  208. skb_push(skb, 6);
  209. skb->data[0] = 0;
  210. }
  211. skb->data[1] = L2TP_VERSION;
  212. unaligned(&skb->data[2])->u32 = opt->remote;
  213. /* Now send the packet via the delivery queue. */
  214. skb_set_owner_w(skb, sk_udp);
  215. skb_queue_tail(&delivery_queue, skb);
  216. schedule_work(&delivery_work);
  217. return 1;
  218. }
  219. /******************************************************************************/
  220. static struct ppp_channel_ops pppolac_channel_ops = {
  221. .start_xmit = pppolac_xmit,
  222. };
  223. static int pppolac_connect(struct socket *sock, struct sockaddr *useraddr,
  224. int addrlen, int flags)
  225. {
  226. struct sock *sk = sock->sk;
  227. struct pppox_sock *po = pppox_sk(sk);
  228. struct sockaddr_pppolac *addr = (struct sockaddr_pppolac *)useraddr;
  229. struct socket *sock_udp = NULL;
  230. struct sock *sk_udp;
  231. int error;
  232. if (addrlen != sizeof(struct sockaddr_pppolac) ||
  233. !addr->local.tunnel || !addr->local.session ||
  234. !addr->remote.tunnel || !addr->remote.session) {
  235. return -EINVAL;
  236. }
  237. lock_sock(sk);
  238. error = -EALREADY;
  239. if (sk->sk_state != PPPOX_NONE)
  240. goto out;
  241. sock_udp = sockfd_lookup(addr->udp_socket, &error);
  242. if (!sock_udp)
  243. goto out;
  244. sk_udp = sock_udp->sk;
  245. lock_sock(sk_udp);
  246. /* Remove this check when IPv6 supports UDP encapsulation. */
  247. error = -EAFNOSUPPORT;
  248. if (sk_udp->sk_family != AF_INET)
  249. goto out;
  250. error = -EPROTONOSUPPORT;
  251. if (sk_udp->sk_protocol != IPPROTO_UDP)
  252. goto out;
  253. error = -EDESTADDRREQ;
  254. if (sk_udp->sk_state != TCP_ESTABLISHED)
  255. goto out;
  256. error = -EBUSY;
  257. if (udp_sk(sk_udp)->encap_type || sk_udp->sk_user_data)
  258. goto out;
  259. if (!sk_udp->sk_bound_dev_if) {
  260. struct dst_entry *dst = sk_dst_get(sk_udp);
  261. error = -ENODEV;
  262. if (!dst)
  263. goto out;
  264. sk_udp->sk_bound_dev_if = dst->dev->ifindex;
  265. dst_release(dst);
  266. }
  267. po->chan.hdrlen = 12;
  268. po->chan.private = sk_udp;
  269. po->chan.ops = &pppolac_channel_ops;
  270. po->chan.mtu = PPP_MRU - 80;
  271. po->proto.lac.local = unaligned(&addr->local)->u32;
  272. po->proto.lac.remote = unaligned(&addr->remote)->u32;
  273. atomic_set(&po->proto.lac.sequencing, 1);
  274. po->proto.lac.backlog_rcv = sk_udp->sk_backlog_rcv;
  275. error = ppp_register_channel(&po->chan);
  276. if (error)
  277. goto out;
  278. sk->sk_state = PPPOX_CONNECTED;
  279. udp_sk(sk_udp)->encap_type = UDP_ENCAP_L2TPINUDP;
  280. udp_sk(sk_udp)->encap_rcv = pppolac_recv;
  281. sk_udp->sk_backlog_rcv = pppolac_recv_core;
  282. sk_udp->sk_user_data = sk;
  283. out:
  284. if (sock_udp) {
  285. release_sock(sk_udp);
  286. if (error)
  287. sockfd_put(sock_udp);
  288. }
  289. release_sock(sk);
  290. return error;
  291. }
  292. static int pppolac_release(struct socket *sock)
  293. {
  294. struct sock *sk = sock->sk;
  295. if (!sk)
  296. return 0;
  297. lock_sock(sk);
  298. if (sock_flag(sk, SOCK_DEAD)) {
  299. release_sock(sk);
  300. return -EBADF;
  301. }
  302. if (sk->sk_state != PPPOX_NONE) {
  303. struct sock *sk_udp = (struct sock *)pppox_sk(sk)->chan.private;
  304. lock_sock(sk_udp);
  305. skb_queue_purge(&sk->sk_receive_queue);
  306. pppox_unbind_sock(sk);
  307. udp_sk(sk_udp)->encap_type = 0;
  308. udp_sk(sk_udp)->encap_rcv = NULL;
  309. sk_udp->sk_backlog_rcv = pppox_sk(sk)->proto.lac.backlog_rcv;
  310. sk_udp->sk_user_data = NULL;
  311. release_sock(sk_udp);
  312. sockfd_put(sk_udp->sk_socket);
  313. }
  314. sock_orphan(sk);
  315. sock->sk = NULL;
  316. release_sock(sk);
  317. sock_put(sk);
  318. return 0;
  319. }
  320. /******************************************************************************/
  321. static struct proto pppolac_proto = {
  322. .name = "PPPOLAC",
  323. .owner = THIS_MODULE,
  324. .obj_size = sizeof(struct pppox_sock),
  325. };
  326. static struct proto_ops pppolac_proto_ops = {
  327. .family = PF_PPPOX,
  328. .owner = THIS_MODULE,
  329. .release = pppolac_release,
  330. .bind = sock_no_bind,
  331. .connect = pppolac_connect,
  332. .socketpair = sock_no_socketpair,
  333. .accept = sock_no_accept,
  334. .getname = sock_no_getname,
  335. .poll = sock_no_poll,
  336. .ioctl = pppox_ioctl,
  337. .listen = sock_no_listen,
  338. .shutdown = sock_no_shutdown,
  339. .setsockopt = sock_no_setsockopt,
  340. .getsockopt = sock_no_getsockopt,
  341. .sendmsg = sock_no_sendmsg,
  342. .recvmsg = sock_no_recvmsg,
  343. .mmap = sock_no_mmap,
  344. };
  345. static int pppolac_create(struct net *net, struct socket *sock)
  346. {
  347. struct sock *sk;
  348. sk = sk_alloc(net, PF_PPPOX, GFP_KERNEL, &pppolac_proto);
  349. if (!sk)
  350. return -ENOMEM;
  351. sock_init_data(sock, sk);
  352. sock->state = SS_UNCONNECTED;
  353. sock->ops = &pppolac_proto_ops;
  354. sk->sk_protocol = PX_PROTO_OLAC;
  355. sk->sk_state = PPPOX_NONE;
  356. return 0;
  357. }
  358. /******************************************************************************/
  359. static struct pppox_proto pppolac_pppox_proto = {
  360. .create = pppolac_create,
  361. .owner = THIS_MODULE,
  362. };
  363. static int __init pppolac_init(void)
  364. {
  365. int error;
  366. error = proto_register(&pppolac_proto, 0);
  367. if (error)
  368. return error;
  369. error = register_pppox_proto(PX_PROTO_OLAC, &pppolac_pppox_proto);
  370. if (error)
  371. proto_unregister(&pppolac_proto);
  372. else
  373. skb_queue_head_init(&delivery_queue);
  374. return error;
  375. }
  376. static void __exit pppolac_exit(void)
  377. {
  378. unregister_pppox_proto(PX_PROTO_OLAC);
  379. proto_unregister(&pppolac_proto);
  380. }
  381. module_init(pppolac_init);
  382. module_exit(pppolac_exit);
  383. MODULE_DESCRIPTION("PPP on L2TP Access Concentrator (PPPoLAC)");
  384. MODULE_AUTHOR("Chia-chi Yeh <chiachi@android.com>");
  385. MODULE_LICENSE("GPL");