l2tp_ip6.c 20 KB

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