l2tp_ip6.c 20 KB

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