l2tp_ip6.c 19 KB

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