l2tp_ip6.c 19 KB

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