l2tp_ip6.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  1. /*
  2. * L2TPv3 IP encapsulation support for IPv6
  3. *
  4. * Copyright (c) 2012 Katalix Systems Ltd
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  12. #include <linux/icmp.h>
  13. #include <linux/module.h>
  14. #include <linux/skbuff.h>
  15. #include <linux/random.h>
  16. #include <linux/socket.h>
  17. #include <linux/l2tp.h>
  18. #include <linux/in.h>
  19. #include <linux/in6.h>
  20. #include <net/sock.h>
  21. #include <net/ip.h>
  22. #include <net/icmp.h>
  23. #include <net/udp.h>
  24. #include <net/inet_common.h>
  25. #include <net/inet_hashtables.h>
  26. #include <net/inet6_hashtables.h>
  27. #include <net/tcp_states.h>
  28. #include <net/protocol.h>
  29. #include <net/xfrm.h>
  30. #include <net/transp_v6.h>
  31. #include <net/addrconf.h>
  32. #include <net/ip6_route.h>
  33. #include "l2tp_core.h"
  34. struct l2tp_ip6_sock {
  35. /* inet_sock has to be the first member of l2tp_ip6_sock */
  36. struct inet_sock inet;
  37. u32 conn_id;
  38. u32 peer_conn_id;
  39. /* ipv6_pinfo has to be the last member of l2tp_ip6_sock, see
  40. inet6_sk_generic */
  41. struct ipv6_pinfo inet6;
  42. };
  43. static DEFINE_RWLOCK(l2tp_ip6_lock);
  44. static struct hlist_head l2tp_ip6_table;
  45. static struct hlist_head l2tp_ip6_bind_table;
  46. static inline struct l2tp_ip6_sock *l2tp_ip6_sk(const struct sock *sk)
  47. {
  48. return (struct l2tp_ip6_sock *)sk;
  49. }
  50. static struct sock *__l2tp_ip6_bind_lookup(const struct net *net,
  51. const struct in6_addr *laddr,
  52. const struct in6_addr *raddr,
  53. int dif, u32 tunnel_id)
  54. {
  55. struct sock *sk;
  56. sk_for_each_bound(sk, &l2tp_ip6_bind_table) {
  57. const struct in6_addr *sk_laddr = inet6_rcv_saddr(sk);
  58. const struct in6_addr *sk_raddr = &sk->sk_v6_daddr;
  59. const struct l2tp_ip6_sock *l2tp = l2tp_ip6_sk(sk);
  60. if (!net_eq(sock_net(sk), net))
  61. continue;
  62. if (sk->sk_bound_dev_if && dif && sk->sk_bound_dev_if != dif)
  63. continue;
  64. if (sk_laddr && !ipv6_addr_any(sk_laddr) &&
  65. !ipv6_addr_any(laddr) && !ipv6_addr_equal(sk_laddr, laddr))
  66. continue;
  67. if (!ipv6_addr_any(sk_raddr) && raddr &&
  68. !ipv6_addr_any(raddr) && !ipv6_addr_equal(sk_raddr, raddr))
  69. continue;
  70. if (l2tp->conn_id != tunnel_id)
  71. continue;
  72. goto found;
  73. }
  74. sk = NULL;
  75. found:
  76. return sk;
  77. }
  78. /* When processing receive frames, there are two cases to
  79. * consider. Data frames consist of a non-zero session-id and an
  80. * optional cookie. Control frames consist of a regular L2TP header
  81. * preceded by 32-bits of zeros.
  82. *
  83. * L2TPv3 Session Header Over IP
  84. *
  85. * 0 1 2 3
  86. * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  87. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  88. * | Session ID |
  89. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  90. * | Cookie (optional, maximum 64 bits)...
  91. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  92. * |
  93. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  94. *
  95. * L2TPv3 Control Message Header Over IP
  96. *
  97. * 0 1 2 3
  98. * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  99. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  100. * | (32 bits of zeros) |
  101. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  102. * |T|L|x|x|S|x|x|x|x|x|x|x| Ver | Length |
  103. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  104. * | Control Connection ID |
  105. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  106. * | Ns | Nr |
  107. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  108. *
  109. * All control frames are passed to userspace.
  110. */
  111. static int l2tp_ip6_recv(struct sk_buff *skb)
  112. {
  113. struct net *net = dev_net(skb->dev);
  114. struct sock *sk;
  115. u32 session_id;
  116. u32 tunnel_id;
  117. unsigned char *ptr, *optr;
  118. struct l2tp_session *session;
  119. struct l2tp_tunnel *tunnel = NULL;
  120. struct ipv6hdr *iph;
  121. int length;
  122. if (!pskb_may_pull(skb, 4))
  123. goto discard;
  124. /* Point to L2TP header */
  125. optr = ptr = skb->data;
  126. session_id = ntohl(*((__be32 *) ptr));
  127. ptr += 4;
  128. /* RFC3931: L2TP/IP packets have the first 4 bytes containing
  129. * the session_id. If it is 0, the packet is a L2TP control
  130. * frame and the session_id value can be discarded.
  131. */
  132. if (session_id == 0) {
  133. __skb_pull(skb, 4);
  134. goto pass_up;
  135. }
  136. /* Ok, this is a data packet. Lookup the session. */
  137. session = l2tp_session_get(net, session_id);
  138. if (!session)
  139. goto discard;
  140. tunnel = session->tunnel;
  141. if (!tunnel)
  142. goto discard_sess;
  143. /* Trace packet contents, if enabled */
  144. if (tunnel->debug & L2TP_MSG_DATA) {
  145. length = min(32u, skb->len);
  146. if (!pskb_may_pull(skb, length))
  147. goto discard_sess;
  148. /* Point to L2TP header */
  149. optr = ptr = skb->data;
  150. ptr += 4;
  151. pr_debug("%s: ip recv\n", tunnel->name);
  152. print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, ptr, length);
  153. }
  154. l2tp_recv_common(session, skb, ptr, optr, 0, skb->len);
  155. l2tp_session_dec_refcount(session);
  156. return 0;
  157. pass_up:
  158. /* Get the tunnel_id from the L2TP header */
  159. if (!pskb_may_pull(skb, 12))
  160. goto discard;
  161. if ((skb->data[0] & 0xc0) != 0xc0)
  162. goto discard;
  163. tunnel_id = ntohl(*(__be32 *) &skb->data[4]);
  164. iph = ipv6_hdr(skb);
  165. read_lock_bh(&l2tp_ip6_lock);
  166. sk = __l2tp_ip6_bind_lookup(net, &iph->daddr, &iph->saddr,
  167. inet6_iif(skb), tunnel_id);
  168. if (!sk) {
  169. read_unlock_bh(&l2tp_ip6_lock);
  170. goto discard;
  171. }
  172. sock_hold(sk);
  173. read_unlock_bh(&l2tp_ip6_lock);
  174. if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb))
  175. goto discard_put;
  176. nf_reset(skb);
  177. return sk_receive_skb(sk, skb, 1);
  178. discard_sess:
  179. l2tp_session_dec_refcount(session);
  180. goto discard;
  181. discard_put:
  182. sock_put(sk);
  183. discard:
  184. kfree_skb(skb);
  185. return 0;
  186. }
  187. static int l2tp_ip6_open(struct sock *sk)
  188. {
  189. /* Prevent autobind. We don't have ports. */
  190. inet_sk(sk)->inet_num = IPPROTO_L2TP;
  191. write_lock_bh(&l2tp_ip6_lock);
  192. sk_add_node(sk, &l2tp_ip6_table);
  193. write_unlock_bh(&l2tp_ip6_lock);
  194. return 0;
  195. }
  196. static void l2tp_ip6_close(struct sock *sk, long timeout)
  197. {
  198. write_lock_bh(&l2tp_ip6_lock);
  199. hlist_del_init(&sk->sk_bind_node);
  200. sk_del_node_init(sk);
  201. write_unlock_bh(&l2tp_ip6_lock);
  202. sk_common_release(sk);
  203. }
  204. static void l2tp_ip6_destroy_sock(struct sock *sk)
  205. {
  206. struct l2tp_tunnel *tunnel = sk->sk_user_data;
  207. lock_sock(sk);
  208. ip6_flush_pending_frames(sk);
  209. release_sock(sk);
  210. if (tunnel)
  211. l2tp_tunnel_delete(tunnel);
  212. inet6_destroy_sock(sk);
  213. }
  214. static int l2tp_ip6_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len)
  215. {
  216. struct inet_sock *inet = inet_sk(sk);
  217. struct ipv6_pinfo *np = inet6_sk(sk);
  218. struct sockaddr_l2tpip6 *addr = (struct sockaddr_l2tpip6 *) uaddr;
  219. struct net *net = sock_net(sk);
  220. __be32 v4addr = 0;
  221. int bound_dev_if;
  222. int addr_type;
  223. int err;
  224. if (addr->l2tp_family != AF_INET6)
  225. return -EINVAL;
  226. if (addr_len < sizeof(*addr))
  227. return -EINVAL;
  228. addr_type = ipv6_addr_type(&addr->l2tp_addr);
  229. /* l2tp_ip6 sockets are IPv6 only */
  230. if (addr_type == IPV6_ADDR_MAPPED)
  231. return -EADDRNOTAVAIL;
  232. /* L2TP is point-point, not multicast */
  233. if (addr_type & IPV6_ADDR_MULTICAST)
  234. return -EADDRNOTAVAIL;
  235. lock_sock(sk);
  236. err = -EINVAL;
  237. if (!sock_flag(sk, SOCK_ZAPPED))
  238. goto out_unlock;
  239. if (sk->sk_state != TCP_CLOSE)
  240. goto out_unlock;
  241. bound_dev_if = sk->sk_bound_dev_if;
  242. /* Check if the address belongs to the host. */
  243. rcu_read_lock();
  244. if (addr_type != IPV6_ADDR_ANY) {
  245. struct net_device *dev = NULL;
  246. if (addr_type & IPV6_ADDR_LINKLOCAL) {
  247. if (addr->l2tp_scope_id)
  248. bound_dev_if = addr->l2tp_scope_id;
  249. /* Binding to link-local address requires an
  250. * interface.
  251. */
  252. if (!bound_dev_if)
  253. goto out_unlock_rcu;
  254. err = -ENODEV;
  255. dev = dev_get_by_index_rcu(sock_net(sk), bound_dev_if);
  256. if (!dev)
  257. goto out_unlock_rcu;
  258. }
  259. /* ipv4 addr of the socket is invalid. Only the
  260. * unspecified and mapped address have a v4 equivalent.
  261. */
  262. v4addr = LOOPBACK4_IPV6;
  263. err = -EADDRNOTAVAIL;
  264. if (!ipv6_chk_addr(sock_net(sk), &addr->l2tp_addr, dev, 0))
  265. goto out_unlock_rcu;
  266. }
  267. rcu_read_unlock();
  268. write_lock_bh(&l2tp_ip6_lock);
  269. if (__l2tp_ip6_bind_lookup(net, &addr->l2tp_addr, NULL, bound_dev_if,
  270. addr->l2tp_conn_id)) {
  271. write_unlock_bh(&l2tp_ip6_lock);
  272. err = -EADDRINUSE;
  273. goto out_unlock;
  274. }
  275. inet->inet_saddr = v4addr;
  276. inet->inet_rcv_saddr = v4addr;
  277. sk->sk_bound_dev_if = bound_dev_if;
  278. sk->sk_v6_rcv_saddr = addr->l2tp_addr;
  279. np->saddr = addr->l2tp_addr;
  280. l2tp_ip6_sk(sk)->conn_id = addr->l2tp_conn_id;
  281. sk_add_bind_node(sk, &l2tp_ip6_bind_table);
  282. sk_del_node_init(sk);
  283. write_unlock_bh(&l2tp_ip6_lock);
  284. sock_reset_flag(sk, SOCK_ZAPPED);
  285. release_sock(sk);
  286. return 0;
  287. out_unlock_rcu:
  288. rcu_read_unlock();
  289. out_unlock:
  290. release_sock(sk);
  291. return err;
  292. }
  293. static int l2tp_ip6_connect(struct sock *sk, struct sockaddr *uaddr,
  294. int addr_len)
  295. {
  296. struct sockaddr_l2tpip6 *lsa = (struct sockaddr_l2tpip6 *) uaddr;
  297. struct sockaddr_in6 *usin = (struct sockaddr_in6 *) uaddr;
  298. struct in6_addr *daddr;
  299. int addr_type;
  300. int rc;
  301. if (addr_len < sizeof(*lsa))
  302. return -EINVAL;
  303. if (usin->sin6_family != AF_INET6)
  304. return -EINVAL;
  305. addr_type = ipv6_addr_type(&usin->sin6_addr);
  306. if (addr_type & IPV6_ADDR_MULTICAST)
  307. return -EINVAL;
  308. if (addr_type & IPV6_ADDR_MAPPED) {
  309. daddr = &usin->sin6_addr;
  310. if (ipv4_is_multicast(daddr->s6_addr32[3]))
  311. return -EINVAL;
  312. }
  313. lock_sock(sk);
  314. /* Must bind first - autobinding does not work */
  315. if (sock_flag(sk, SOCK_ZAPPED)) {
  316. rc = -EINVAL;
  317. goto out_sk;
  318. }
  319. rc = __ip6_datagram_connect(sk, uaddr, addr_len);
  320. if (rc < 0)
  321. goto out_sk;
  322. l2tp_ip6_sk(sk)->peer_conn_id = lsa->l2tp_conn_id;
  323. write_lock_bh(&l2tp_ip6_lock);
  324. hlist_del_init(&sk->sk_bind_node);
  325. sk_add_bind_node(sk, &l2tp_ip6_bind_table);
  326. write_unlock_bh(&l2tp_ip6_lock);
  327. out_sk:
  328. release_sock(sk);
  329. return rc;
  330. }
  331. static int l2tp_ip6_disconnect(struct sock *sk, int flags)
  332. {
  333. if (sock_flag(sk, SOCK_ZAPPED))
  334. return 0;
  335. return __udp_disconnect(sk, flags);
  336. }
  337. static int l2tp_ip6_getname(struct socket *sock, struct sockaddr *uaddr,
  338. int peer)
  339. {
  340. struct sockaddr_l2tpip6 *lsa = (struct sockaddr_l2tpip6 *)uaddr;
  341. struct sock *sk = sock->sk;
  342. struct ipv6_pinfo *np = inet6_sk(sk);
  343. struct l2tp_ip6_sock *lsk = l2tp_ip6_sk(sk);
  344. lsa->l2tp_family = AF_INET6;
  345. lsa->l2tp_flowinfo = 0;
  346. lsa->l2tp_scope_id = 0;
  347. lsa->l2tp_unused = 0;
  348. if (peer) {
  349. if (!lsk->peer_conn_id)
  350. return -ENOTCONN;
  351. lsa->l2tp_conn_id = lsk->peer_conn_id;
  352. lsa->l2tp_addr = sk->sk_v6_daddr;
  353. if (np->sndflow)
  354. lsa->l2tp_flowinfo = np->flow_label;
  355. } else {
  356. if (ipv6_addr_any(&sk->sk_v6_rcv_saddr))
  357. lsa->l2tp_addr = np->saddr;
  358. else
  359. lsa->l2tp_addr = sk->sk_v6_rcv_saddr;
  360. lsa->l2tp_conn_id = lsk->conn_id;
  361. }
  362. if (ipv6_addr_type(&lsa->l2tp_addr) & IPV6_ADDR_LINKLOCAL)
  363. lsa->l2tp_scope_id = sk->sk_bound_dev_if;
  364. return sizeof(*lsa);
  365. }
  366. static int l2tp_ip6_backlog_recv(struct sock *sk, struct sk_buff *skb)
  367. {
  368. int rc;
  369. /* Charge it to the socket, dropping if the queue is full. */
  370. rc = sock_queue_rcv_skb(sk, skb);
  371. if (rc < 0)
  372. goto drop;
  373. return 0;
  374. drop:
  375. IP_INC_STATS(sock_net(sk), IPSTATS_MIB_INDISCARDS);
  376. kfree_skb(skb);
  377. return -1;
  378. }
  379. static int l2tp_ip6_push_pending_frames(struct sock *sk)
  380. {
  381. struct sk_buff *skb;
  382. __be32 *transhdr = NULL;
  383. int err = 0;
  384. skb = skb_peek(&sk->sk_write_queue);
  385. if (skb == NULL)
  386. goto out;
  387. transhdr = (__be32 *)skb_transport_header(skb);
  388. *transhdr = 0;
  389. err = ip6_push_pending_frames(sk);
  390. out:
  391. return err;
  392. }
  393. /* Userspace will call sendmsg() on the tunnel socket to send L2TP
  394. * control frames.
  395. */
  396. static int l2tp_ip6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
  397. {
  398. struct ipv6_txoptions opt_space;
  399. DECLARE_SOCKADDR(struct sockaddr_l2tpip6 *, lsa, msg->msg_name);
  400. struct in6_addr *daddr, *final_p, final;
  401. struct ipv6_pinfo *np = inet6_sk(sk);
  402. struct ipv6_txoptions *opt_to_free = NULL;
  403. struct ipv6_txoptions *opt = NULL;
  404. struct ip6_flowlabel *flowlabel = NULL;
  405. struct dst_entry *dst = NULL;
  406. struct flowi6 fl6;
  407. struct ipcm6_cookie ipc6;
  408. int addr_len = msg->msg_namelen;
  409. int transhdrlen = 4; /* zero session-id */
  410. int ulen = len + transhdrlen;
  411. int err;
  412. /* Rough check on arithmetic overflow,
  413. better check is made in ip6_append_data().
  414. */
  415. if (len > INT_MAX)
  416. return -EMSGSIZE;
  417. /* Mirror BSD error message compatibility */
  418. if (msg->msg_flags & MSG_OOB)
  419. return -EOPNOTSUPP;
  420. /*
  421. * Get and verify the address.
  422. */
  423. memset(&fl6, 0, sizeof(fl6));
  424. fl6.flowi6_mark = sk->sk_mark;
  425. fl6.flowi6_uid = sk->sk_uid;
  426. ipcm6_init(&ipc6);
  427. if (lsa) {
  428. if (addr_len < SIN6_LEN_RFC2133)
  429. return -EINVAL;
  430. if (lsa->l2tp_family && lsa->l2tp_family != AF_INET6)
  431. return -EAFNOSUPPORT;
  432. daddr = &lsa->l2tp_addr;
  433. if (np->sndflow) {
  434. fl6.flowlabel = lsa->l2tp_flowinfo & IPV6_FLOWINFO_MASK;
  435. if (fl6.flowlabel&IPV6_FLOWLABEL_MASK) {
  436. flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
  437. if (flowlabel == NULL)
  438. return -EINVAL;
  439. }
  440. }
  441. /*
  442. * Otherwise it will be difficult to maintain
  443. * sk->sk_dst_cache.
  444. */
  445. if (sk->sk_state == TCP_ESTABLISHED &&
  446. ipv6_addr_equal(daddr, &sk->sk_v6_daddr))
  447. daddr = &sk->sk_v6_daddr;
  448. if (addr_len >= sizeof(struct sockaddr_in6) &&
  449. lsa->l2tp_scope_id &&
  450. ipv6_addr_type(daddr) & IPV6_ADDR_LINKLOCAL)
  451. fl6.flowi6_oif = lsa->l2tp_scope_id;
  452. } else {
  453. if (sk->sk_state != TCP_ESTABLISHED)
  454. return -EDESTADDRREQ;
  455. daddr = &sk->sk_v6_daddr;
  456. fl6.flowlabel = np->flow_label;
  457. }
  458. if (fl6.flowi6_oif == 0)
  459. fl6.flowi6_oif = sk->sk_bound_dev_if;
  460. if (msg->msg_controllen) {
  461. opt = &opt_space;
  462. memset(opt, 0, sizeof(struct ipv6_txoptions));
  463. opt->tot_len = sizeof(struct ipv6_txoptions);
  464. ipc6.opt = opt;
  465. err = ip6_datagram_send_ctl(sock_net(sk), sk, msg, &fl6, &ipc6);
  466. if (err < 0) {
  467. fl6_sock_release(flowlabel);
  468. return err;
  469. }
  470. if ((fl6.flowlabel & IPV6_FLOWLABEL_MASK) && !flowlabel) {
  471. flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
  472. if (flowlabel == NULL)
  473. return -EINVAL;
  474. }
  475. if (!(opt->opt_nflen|opt->opt_flen))
  476. opt = NULL;
  477. }
  478. if (!opt) {
  479. opt = txopt_get(np);
  480. opt_to_free = opt;
  481. }
  482. if (flowlabel)
  483. opt = fl6_merge_options(&opt_space, flowlabel, opt);
  484. opt = ipv6_fixup_options(&opt_space, opt);
  485. ipc6.opt = opt;
  486. fl6.flowi6_proto = sk->sk_protocol;
  487. if (!ipv6_addr_any(daddr))
  488. fl6.daddr = *daddr;
  489. else
  490. fl6.daddr.s6_addr[15] = 0x1; /* :: means loopback (BSD'ism) */
  491. if (ipv6_addr_any(&fl6.saddr) && !ipv6_addr_any(&np->saddr))
  492. fl6.saddr = np->saddr;
  493. final_p = fl6_update_dst(&fl6, opt, &final);
  494. if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr))
  495. fl6.flowi6_oif = np->mcast_oif;
  496. else if (!fl6.flowi6_oif)
  497. fl6.flowi6_oif = np->ucast_oif;
  498. security_sk_classify_flow(sk, flowi6_to_flowi(&fl6));
  499. if (ipc6.tclass < 0)
  500. ipc6.tclass = np->tclass;
  501. fl6.flowlabel = ip6_make_flowinfo(ipc6.tclass, fl6.flowlabel);
  502. dst = ip6_dst_lookup_flow(sk, &fl6, final_p);
  503. if (IS_ERR(dst)) {
  504. err = PTR_ERR(dst);
  505. goto out;
  506. }
  507. if (ipc6.hlimit < 0)
  508. ipc6.hlimit = ip6_sk_dst_hoplimit(np, &fl6, dst);
  509. if (ipc6.dontfrag < 0)
  510. ipc6.dontfrag = np->dontfrag;
  511. if (msg->msg_flags & MSG_CONFIRM)
  512. goto do_confirm;
  513. back_from_confirm:
  514. lock_sock(sk);
  515. err = ip6_append_data(sk, ip_generic_getfrag, msg,
  516. ulen, transhdrlen, &ipc6,
  517. &fl6, (struct rt6_info *)dst,
  518. msg->msg_flags);
  519. if (err)
  520. ip6_flush_pending_frames(sk);
  521. else if (!(msg->msg_flags & MSG_MORE))
  522. err = l2tp_ip6_push_pending_frames(sk);
  523. release_sock(sk);
  524. done:
  525. dst_release(dst);
  526. out:
  527. fl6_sock_release(flowlabel);
  528. txopt_put(opt_to_free);
  529. return err < 0 ? err : len;
  530. do_confirm:
  531. if (msg->msg_flags & MSG_PROBE)
  532. dst_confirm_neigh(dst, &fl6.daddr);
  533. if (!(msg->msg_flags & MSG_PROBE) || len)
  534. goto back_from_confirm;
  535. err = 0;
  536. goto done;
  537. }
  538. static int l2tp_ip6_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
  539. int noblock, int flags, int *addr_len)
  540. {
  541. struct ipv6_pinfo *np = inet6_sk(sk);
  542. DECLARE_SOCKADDR(struct sockaddr_l2tpip6 *, lsa, msg->msg_name);
  543. size_t copied = 0;
  544. int err = -EOPNOTSUPP;
  545. struct sk_buff *skb;
  546. if (flags & MSG_OOB)
  547. goto out;
  548. if (addr_len)
  549. *addr_len = sizeof(*lsa);
  550. if (flags & MSG_ERRQUEUE)
  551. return ipv6_recv_error(sk, msg, len, addr_len);
  552. skb = skb_recv_datagram(sk, flags, noblock, &err);
  553. if (!skb)
  554. goto out;
  555. copied = skb->len;
  556. if (len < copied) {
  557. msg->msg_flags |= MSG_TRUNC;
  558. copied = len;
  559. }
  560. err = skb_copy_datagram_msg(skb, 0, msg, copied);
  561. if (err)
  562. goto done;
  563. sock_recv_timestamp(msg, sk, skb);
  564. /* Copy the address. */
  565. if (lsa) {
  566. lsa->l2tp_family = AF_INET6;
  567. lsa->l2tp_unused = 0;
  568. lsa->l2tp_addr = ipv6_hdr(skb)->saddr;
  569. lsa->l2tp_flowinfo = 0;
  570. lsa->l2tp_scope_id = 0;
  571. lsa->l2tp_conn_id = 0;
  572. if (ipv6_addr_type(&lsa->l2tp_addr) & IPV6_ADDR_LINKLOCAL)
  573. lsa->l2tp_scope_id = inet6_iif(skb);
  574. }
  575. if (np->rxopt.all)
  576. ip6_datagram_recv_ctl(sk, msg, skb);
  577. if (flags & MSG_TRUNC)
  578. copied = skb->len;
  579. done:
  580. skb_free_datagram(sk, skb);
  581. out:
  582. return err ? err : copied;
  583. }
  584. static struct proto l2tp_ip6_prot = {
  585. .name = "L2TP/IPv6",
  586. .owner = THIS_MODULE,
  587. .init = l2tp_ip6_open,
  588. .close = l2tp_ip6_close,
  589. .bind = l2tp_ip6_bind,
  590. .connect = l2tp_ip6_connect,
  591. .disconnect = l2tp_ip6_disconnect,
  592. .ioctl = l2tp_ioctl,
  593. .destroy = l2tp_ip6_destroy_sock,
  594. .setsockopt = ipv6_setsockopt,
  595. .getsockopt = ipv6_getsockopt,
  596. .sendmsg = l2tp_ip6_sendmsg,
  597. .recvmsg = l2tp_ip6_recvmsg,
  598. .backlog_rcv = l2tp_ip6_backlog_recv,
  599. .hash = inet6_hash,
  600. .unhash = inet_unhash,
  601. .obj_size = sizeof(struct l2tp_ip6_sock),
  602. #ifdef CONFIG_COMPAT
  603. .compat_setsockopt = compat_ipv6_setsockopt,
  604. .compat_getsockopt = compat_ipv6_getsockopt,
  605. #endif
  606. };
  607. static const struct proto_ops l2tp_ip6_ops = {
  608. .family = PF_INET6,
  609. .owner = THIS_MODULE,
  610. .release = inet6_release,
  611. .bind = inet6_bind,
  612. .connect = inet_dgram_connect,
  613. .socketpair = sock_no_socketpair,
  614. .accept = sock_no_accept,
  615. .getname = l2tp_ip6_getname,
  616. .poll = datagram_poll,
  617. .ioctl = inet6_ioctl,
  618. .listen = sock_no_listen,
  619. .shutdown = inet_shutdown,
  620. .setsockopt = sock_common_setsockopt,
  621. .getsockopt = sock_common_getsockopt,
  622. .sendmsg = inet_sendmsg,
  623. .recvmsg = sock_common_recvmsg,
  624. .mmap = sock_no_mmap,
  625. .sendpage = sock_no_sendpage,
  626. #ifdef CONFIG_COMPAT
  627. .compat_setsockopt = compat_sock_common_setsockopt,
  628. .compat_getsockopt = compat_sock_common_getsockopt,
  629. #endif
  630. };
  631. static struct inet_protosw l2tp_ip6_protosw = {
  632. .type = SOCK_DGRAM,
  633. .protocol = IPPROTO_L2TP,
  634. .prot = &l2tp_ip6_prot,
  635. .ops = &l2tp_ip6_ops,
  636. };
  637. static struct inet6_protocol l2tp_ip6_protocol __read_mostly = {
  638. .handler = l2tp_ip6_recv,
  639. };
  640. static int __init l2tp_ip6_init(void)
  641. {
  642. int err;
  643. pr_info("L2TP IP encapsulation support for IPv6 (L2TPv3)\n");
  644. err = proto_register(&l2tp_ip6_prot, 1);
  645. if (err != 0)
  646. goto out;
  647. err = inet6_add_protocol(&l2tp_ip6_protocol, IPPROTO_L2TP);
  648. if (err)
  649. goto out1;
  650. inet6_register_protosw(&l2tp_ip6_protosw);
  651. return 0;
  652. out1:
  653. proto_unregister(&l2tp_ip6_prot);
  654. out:
  655. return err;
  656. }
  657. static void __exit l2tp_ip6_exit(void)
  658. {
  659. inet6_unregister_protosw(&l2tp_ip6_protosw);
  660. inet6_del_protocol(&l2tp_ip6_protocol, IPPROTO_L2TP);
  661. proto_unregister(&l2tp_ip6_prot);
  662. }
  663. module_init(l2tp_ip6_init);
  664. module_exit(l2tp_ip6_exit);
  665. MODULE_LICENSE("GPL");
  666. MODULE_AUTHOR("Chris Elston <celston@katalix.com>");
  667. MODULE_DESCRIPTION("L2TP IP encapsulation for IPv6");
  668. MODULE_VERSION("1.0");
  669. /* Use the value of SOCK_DGRAM (2) directory, because __stringify doesn't like
  670. * enums
  671. */
  672. MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_INET6, 2, IPPROTO_L2TP);
  673. MODULE_ALIAS_NET_PF_PROTO(PF_INET6, IPPROTO_L2TP);