l2tp_ip6.c 20 KB

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