l2tp_ip6.c 19 KB

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