l2tp_ip6.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  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. #include <linux/icmp.h>
  12. #include <linux/module.h>
  13. #include <linux/skbuff.h>
  14. #include <linux/random.h>
  15. #include <linux/socket.h>
  16. #include <linux/l2tp.h>
  17. #include <linux/in.h>
  18. #include <linux/in6.h>
  19. #include <net/sock.h>
  20. #include <net/ip.h>
  21. #include <net/icmp.h>
  22. #include <net/udp.h>
  23. #include <net/inet_common.h>
  24. #include <net/inet_hashtables.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(struct net *net,
  49. struct in6_addr *laddr,
  50. int dif, u32 tunnel_id)
  51. {
  52. struct hlist_node *node;
  53. struct sock *sk;
  54. sk_for_each_bound(sk, node, &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. int offset;
  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. printk(KERN_DEBUG "%s: ip recv: ", tunnel->name);
  148. offset = 0;
  149. do {
  150. printk(" %02X", ptr[offset]);
  151. } while (++offset < length);
  152. printk("\n");
  153. }
  154. l2tp_recv_common(session, skb, ptr, optr, 0, skb->len,
  155. tunnel->recv_payload_hook);
  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. tunnel = l2tp_tunnel_find(&init_net, tunnel_id);
  165. if (tunnel != NULL)
  166. sk = tunnel->sock;
  167. else {
  168. struct ipv6hdr *iph = ipv6_hdr(skb);
  169. read_lock_bh(&l2tp_ip6_lock);
  170. sk = __l2tp_ip6_bind_lookup(&init_net, &iph->daddr,
  171. 0, tunnel_id);
  172. read_unlock_bh(&l2tp_ip6_lock);
  173. }
  174. if (sk == NULL)
  175. goto discard;
  176. sock_hold(sk);
  177. if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb))
  178. goto discard_put;
  179. nf_reset(skb);
  180. return sk_receive_skb(sk, skb, 1);
  181. discard_put:
  182. sock_put(sk);
  183. discard:
  184. kfree_skb(skb);
  185. return 0;
  186. }
  187. static int l2tp_ip6_open(struct sock *sk)
  188. {
  189. /* Prevent autobind. We don't have ports. */
  190. inet_sk(sk)->inet_num = IPPROTO_L2TP;
  191. write_lock_bh(&l2tp_ip6_lock);
  192. sk_add_node(sk, &l2tp_ip6_table);
  193. write_unlock_bh(&l2tp_ip6_lock);
  194. return 0;
  195. }
  196. static void l2tp_ip6_close(struct sock *sk, long timeout)
  197. {
  198. write_lock_bh(&l2tp_ip6_lock);
  199. hlist_del_init(&sk->sk_bind_node);
  200. sk_del_node_init(sk);
  201. write_unlock_bh(&l2tp_ip6_lock);
  202. sk_common_release(sk);
  203. }
  204. static void l2tp_ip6_destroy_sock(struct sock *sk)
  205. {
  206. lock_sock(sk);
  207. ip6_flush_pending_frames(sk);
  208. release_sock(sk);
  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 (addr_len < sizeof(*addr))
  220. return -EINVAL;
  221. addr_type = ipv6_addr_type(&addr->l2tp_addr);
  222. /* l2tp_ip6 sockets are IPv6 only */
  223. if (addr_type == IPV6_ADDR_MAPPED)
  224. return -EADDRNOTAVAIL;
  225. /* L2TP is point-point, not multicast */
  226. if (addr_type & IPV6_ADDR_MULTICAST)
  227. return -EADDRNOTAVAIL;
  228. err = -EADDRINUSE;
  229. read_lock_bh(&l2tp_ip6_lock);
  230. if (__l2tp_ip6_bind_lookup(&init_net, &addr->l2tp_addr,
  231. sk->sk_bound_dev_if, addr->l2tp_conn_id))
  232. goto out_in_use;
  233. read_unlock_bh(&l2tp_ip6_lock);
  234. lock_sock(sk);
  235. err = -EINVAL;
  236. if (sk->sk_state != TCP_CLOSE)
  237. goto out_unlock;
  238. /* Check if the address belongs to the host. */
  239. rcu_read_lock();
  240. if (addr_type != IPV6_ADDR_ANY) {
  241. struct net_device *dev = NULL;
  242. if (addr_type & IPV6_ADDR_LINKLOCAL) {
  243. if (addr_len >= sizeof(struct sockaddr_in6) &&
  244. addr->l2tp_scope_id) {
  245. /* Override any existing binding, if another
  246. * one is supplied by user.
  247. */
  248. sk->sk_bound_dev_if = addr->l2tp_scope_id;
  249. }
  250. /* Binding to link-local address requires an
  251. interface */
  252. if (!sk->sk_bound_dev_if)
  253. goto out_unlock_rcu;
  254. err = -ENODEV;
  255. dev = dev_get_by_index_rcu(sock_net(sk),
  256. sk->sk_bound_dev_if);
  257. if (!dev)
  258. goto out_unlock_rcu;
  259. }
  260. /* ipv4 addr of the socket is invalid. Only the
  261. * unspecified and mapped address have a v4 equivalent.
  262. */
  263. v4addr = LOOPBACK4_IPV6;
  264. err = -EADDRNOTAVAIL;
  265. if (!ipv6_chk_addr(sock_net(sk), &addr->l2tp_addr, dev, 0))
  266. goto out_unlock_rcu;
  267. }
  268. rcu_read_unlock();
  269. inet->inet_rcv_saddr = inet->inet_saddr = v4addr;
  270. np->rcv_saddr = addr->l2tp_addr;
  271. np->saddr = addr->l2tp_addr;
  272. l2tp_ip6_sk(sk)->conn_id = addr->l2tp_conn_id;
  273. write_lock_bh(&l2tp_ip6_lock);
  274. sk_add_bind_node(sk, &l2tp_ip6_bind_table);
  275. sk_del_node_init(sk);
  276. write_unlock_bh(&l2tp_ip6_lock);
  277. release_sock(sk);
  278. return 0;
  279. out_unlock_rcu:
  280. rcu_read_unlock();
  281. out_unlock:
  282. release_sock(sk);
  283. return err;
  284. out_in_use:
  285. read_unlock_bh(&l2tp_ip6_lock);
  286. return err;
  287. }
  288. static int l2tp_ip6_connect(struct sock *sk, struct sockaddr *uaddr,
  289. int addr_len)
  290. {
  291. struct sockaddr_l2tpip6 *lsa = (struct sockaddr_l2tpip6 *) uaddr;
  292. struct sockaddr_in6 *usin = (struct sockaddr_in6 *) uaddr;
  293. struct in6_addr *daddr;
  294. int addr_type;
  295. int rc;
  296. if (addr_len < sizeof(*lsa))
  297. return -EINVAL;
  298. addr_type = ipv6_addr_type(&usin->sin6_addr);
  299. if (addr_type & IPV6_ADDR_MULTICAST)
  300. return -EINVAL;
  301. if (addr_type & IPV6_ADDR_MAPPED) {
  302. daddr = &usin->sin6_addr;
  303. if (ipv4_is_multicast(daddr->s6_addr32[3]))
  304. return -EINVAL;
  305. }
  306. rc = ip6_datagram_connect(sk, uaddr, addr_len);
  307. lock_sock(sk);
  308. l2tp_ip6_sk(sk)->peer_conn_id = lsa->l2tp_conn_id;
  309. write_lock_bh(&l2tp_ip6_lock);
  310. hlist_del_init(&sk->sk_bind_node);
  311. sk_add_bind_node(sk, &l2tp_ip6_bind_table);
  312. write_unlock_bh(&l2tp_ip6_lock);
  313. release_sock(sk);
  314. return rc;
  315. }
  316. static int l2tp_ip6_getname(struct socket *sock, struct sockaddr *uaddr,
  317. int *uaddr_len, int peer)
  318. {
  319. struct sockaddr_l2tpip6 *lsa = (struct sockaddr_l2tpip6 *)uaddr;
  320. struct sock *sk = sock->sk;
  321. struct ipv6_pinfo *np = inet6_sk(sk);
  322. struct l2tp_ip6_sock *lsk = l2tp_ip6_sk(sk);
  323. lsa->l2tp_family = AF_INET6;
  324. lsa->l2tp_flowinfo = 0;
  325. lsa->l2tp_scope_id = 0;
  326. if (peer) {
  327. if (!lsk->peer_conn_id)
  328. return -ENOTCONN;
  329. lsa->l2tp_conn_id = lsk->peer_conn_id;
  330. lsa->l2tp_addr = np->daddr;
  331. if (np->sndflow)
  332. lsa->l2tp_flowinfo = np->flow_label;
  333. } else {
  334. if (ipv6_addr_any(&np->rcv_saddr))
  335. lsa->l2tp_addr = np->saddr;
  336. else
  337. lsa->l2tp_addr = np->rcv_saddr;
  338. lsa->l2tp_conn_id = lsk->conn_id;
  339. }
  340. if (ipv6_addr_type(&lsa->l2tp_addr) & IPV6_ADDR_LINKLOCAL)
  341. lsa->l2tp_scope_id = sk->sk_bound_dev_if;
  342. *uaddr_len = sizeof(*lsa);
  343. return 0;
  344. }
  345. static int l2tp_ip6_backlog_recv(struct sock *sk, struct sk_buff *skb)
  346. {
  347. int rc;
  348. /* Charge it to the socket, dropping if the queue is full. */
  349. rc = sock_queue_rcv_skb(sk, skb);
  350. if (rc < 0)
  351. goto drop;
  352. return 0;
  353. drop:
  354. IP_INC_STATS(&init_net, IPSTATS_MIB_INDISCARDS);
  355. kfree_skb(skb);
  356. return -1;
  357. }
  358. static int l2tp_ip6_push_pending_frames(struct sock *sk)
  359. {
  360. struct sk_buff *skb;
  361. __be32 *transhdr = NULL;
  362. int err = 0;
  363. skb = skb_peek(&sk->sk_write_queue);
  364. if (skb == NULL)
  365. goto out;
  366. transhdr = (__be32 *)skb_transport_header(skb);
  367. *transhdr = 0;
  368. err = ip6_push_pending_frames(sk);
  369. out:
  370. return err;
  371. }
  372. /* Userspace will call sendmsg() on the tunnel socket to send L2TP
  373. * control frames.
  374. */
  375. static int l2tp_ip6_sendmsg(struct kiocb *iocb, struct sock *sk,
  376. struct msghdr *msg, size_t len)
  377. {
  378. struct ipv6_txoptions opt_space;
  379. struct sockaddr_l2tpip6 *lsa =
  380. (struct sockaddr_l2tpip6 *) msg->msg_name;
  381. struct in6_addr *daddr, *final_p, final;
  382. struct ipv6_pinfo *np = inet6_sk(sk);
  383. struct ipv6_txoptions *opt = NULL;
  384. struct ip6_flowlabel *flowlabel = NULL;
  385. struct dst_entry *dst = NULL;
  386. struct flowi6 fl6;
  387. int addr_len = msg->msg_namelen;
  388. int hlimit = -1;
  389. int tclass = -1;
  390. int dontfrag = -1;
  391. int transhdrlen = 4; /* zero session-id */
  392. int ulen = len + transhdrlen;
  393. int err;
  394. /* Rough check on arithmetic overflow,
  395. better check is made in ip6_append_data().
  396. */
  397. if (len > INT_MAX)
  398. return -EMSGSIZE;
  399. /* Mirror BSD error message compatibility */
  400. if (msg->msg_flags & MSG_OOB)
  401. return -EOPNOTSUPP;
  402. /*
  403. * Get and verify the address.
  404. */
  405. memset(&fl6, 0, sizeof(fl6));
  406. fl6.flowi6_mark = sk->sk_mark;
  407. if (lsa) {
  408. if (addr_len < SIN6_LEN_RFC2133)
  409. return -EINVAL;
  410. if (lsa->l2tp_family && lsa->l2tp_family != AF_INET6)
  411. return -EAFNOSUPPORT;
  412. daddr = &lsa->l2tp_addr;
  413. if (np->sndflow) {
  414. fl6.flowlabel = lsa->l2tp_flowinfo & IPV6_FLOWINFO_MASK;
  415. if (fl6.flowlabel&IPV6_FLOWLABEL_MASK) {
  416. flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
  417. if (flowlabel == NULL)
  418. return -EINVAL;
  419. daddr = &flowlabel->dst;
  420. }
  421. }
  422. /*
  423. * Otherwise it will be difficult to maintain
  424. * sk->sk_dst_cache.
  425. */
  426. if (sk->sk_state == TCP_ESTABLISHED &&
  427. ipv6_addr_equal(daddr, &np->daddr))
  428. daddr = &np->daddr;
  429. if (addr_len >= sizeof(struct sockaddr_in6) &&
  430. lsa->l2tp_scope_id &&
  431. ipv6_addr_type(daddr) & IPV6_ADDR_LINKLOCAL)
  432. fl6.flowi6_oif = lsa->l2tp_scope_id;
  433. } else {
  434. if (sk->sk_state != TCP_ESTABLISHED)
  435. return -EDESTADDRREQ;
  436. daddr = &np->daddr;
  437. fl6.flowlabel = np->flow_label;
  438. }
  439. if (fl6.flowi6_oif == 0)
  440. fl6.flowi6_oif = sk->sk_bound_dev_if;
  441. if (msg->msg_controllen) {
  442. opt = &opt_space;
  443. memset(opt, 0, sizeof(struct ipv6_txoptions));
  444. opt->tot_len = sizeof(struct ipv6_txoptions);
  445. err = datagram_send_ctl(sock_net(sk), sk, msg, &fl6, opt,
  446. &hlimit, &tclass, &dontfrag);
  447. if (err < 0) {
  448. fl6_sock_release(flowlabel);
  449. return err;
  450. }
  451. if ((fl6.flowlabel & IPV6_FLOWLABEL_MASK) && !flowlabel) {
  452. flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
  453. if (flowlabel == NULL)
  454. return -EINVAL;
  455. }
  456. if (!(opt->opt_nflen|opt->opt_flen))
  457. opt = NULL;
  458. }
  459. if (opt == NULL)
  460. opt = np->opt;
  461. if (flowlabel)
  462. opt = fl6_merge_options(&opt_space, flowlabel, opt);
  463. opt = ipv6_fixup_options(&opt_space, opt);
  464. fl6.flowi6_proto = sk->sk_protocol;
  465. if (!ipv6_addr_any(daddr))
  466. fl6.daddr = *daddr;
  467. else
  468. fl6.daddr.s6_addr[15] = 0x1; /* :: means loopback (BSD'ism) */
  469. if (ipv6_addr_any(&fl6.saddr) && !ipv6_addr_any(&np->saddr))
  470. fl6.saddr = np->saddr;
  471. final_p = fl6_update_dst(&fl6, opt, &final);
  472. if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr))
  473. fl6.flowi6_oif = np->mcast_oif;
  474. else if (!fl6.flowi6_oif)
  475. fl6.flowi6_oif = np->ucast_oif;
  476. security_sk_classify_flow(sk, flowi6_to_flowi(&fl6));
  477. dst = ip6_dst_lookup_flow(sk, &fl6, final_p, true);
  478. if (IS_ERR(dst)) {
  479. err = PTR_ERR(dst);
  480. goto out;
  481. }
  482. if (hlimit < 0) {
  483. if (ipv6_addr_is_multicast(&fl6.daddr))
  484. hlimit = np->mcast_hops;
  485. else
  486. hlimit = np->hop_limit;
  487. if (hlimit < 0)
  488. hlimit = ip6_dst_hoplimit(dst);
  489. }
  490. if (tclass < 0)
  491. tclass = np->tclass;
  492. if (dontfrag < 0)
  493. dontfrag = np->dontfrag;
  494. if (msg->msg_flags & MSG_CONFIRM)
  495. goto do_confirm;
  496. back_from_confirm:
  497. lock_sock(sk);
  498. err = ip6_append_data(sk, ip_generic_getfrag, msg->msg_iov,
  499. ulen, transhdrlen, hlimit, tclass, opt,
  500. &fl6, (struct rt6_info *)dst,
  501. msg->msg_flags, dontfrag);
  502. if (err)
  503. ip6_flush_pending_frames(sk);
  504. else if (!(msg->msg_flags & MSG_MORE))
  505. err = l2tp_ip6_push_pending_frames(sk);
  506. release_sock(sk);
  507. done:
  508. dst_release(dst);
  509. out:
  510. fl6_sock_release(flowlabel);
  511. return err < 0 ? err : len;
  512. do_confirm:
  513. dst_confirm(dst);
  514. if (!(msg->msg_flags & MSG_PROBE) || len)
  515. goto back_from_confirm;
  516. err = 0;
  517. goto done;
  518. }
  519. static int l2tp_ip6_recvmsg(struct kiocb *iocb, struct sock *sk,
  520. struct msghdr *msg, size_t len, int noblock,
  521. int flags, int *addr_len)
  522. {
  523. struct inet_sock *inet = inet_sk(sk);
  524. struct sockaddr_l2tpip6 *lsa = (struct sockaddr_l2tpip6 *)msg->msg_name;
  525. size_t copied = 0;
  526. int err = -EOPNOTSUPP;
  527. struct sk_buff *skb;
  528. if (flags & MSG_OOB)
  529. goto out;
  530. if (addr_len)
  531. *addr_len = sizeof(*lsa);
  532. if (flags & MSG_ERRQUEUE)
  533. return ipv6_recv_error(sk, msg, len);
  534. skb = skb_recv_datagram(sk, flags, noblock, &err);
  535. if (!skb)
  536. goto out;
  537. copied = skb->len;
  538. if (len < copied) {
  539. msg->msg_flags |= MSG_TRUNC;
  540. copied = len;
  541. }
  542. err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
  543. if (err)
  544. goto done;
  545. sock_recv_timestamp(msg, sk, skb);
  546. /* Copy the address. */
  547. if (lsa) {
  548. lsa->l2tp_family = AF_INET6;
  549. lsa->l2tp_unused = 0;
  550. lsa->l2tp_addr = ipv6_hdr(skb)->saddr;
  551. lsa->l2tp_flowinfo = 0;
  552. lsa->l2tp_scope_id = 0;
  553. if (ipv6_addr_type(&lsa->l2tp_addr) & IPV6_ADDR_LINKLOCAL)
  554. lsa->l2tp_scope_id = IP6CB(skb)->iif;
  555. }
  556. if (inet->cmsg_flags)
  557. ip_cmsg_recv(msg, skb);
  558. if (flags & MSG_TRUNC)
  559. copied = skb->len;
  560. done:
  561. skb_free_datagram(sk, skb);
  562. out:
  563. return err ? err : copied;
  564. }
  565. static struct proto l2tp_ip6_prot = {
  566. .name = "L2TP/IPv6",
  567. .owner = THIS_MODULE,
  568. .init = l2tp_ip6_open,
  569. .close = l2tp_ip6_close,
  570. .bind = l2tp_ip6_bind,
  571. .connect = l2tp_ip6_connect,
  572. .disconnect = udp_disconnect,
  573. .ioctl = udp_ioctl,
  574. .destroy = l2tp_ip6_destroy_sock,
  575. .setsockopt = ipv6_setsockopt,
  576. .getsockopt = ipv6_getsockopt,
  577. .sendmsg = l2tp_ip6_sendmsg,
  578. .recvmsg = l2tp_ip6_recvmsg,
  579. .backlog_rcv = l2tp_ip6_backlog_recv,
  580. .hash = inet_hash,
  581. .unhash = inet_unhash,
  582. .obj_size = sizeof(struct l2tp_ip6_sock),
  583. #ifdef CONFIG_COMPAT
  584. .compat_setsockopt = compat_ipv6_setsockopt,
  585. .compat_getsockopt = compat_ipv6_getsockopt,
  586. #endif
  587. };
  588. static const struct proto_ops l2tp_ip6_ops = {
  589. .family = PF_INET6,
  590. .owner = THIS_MODULE,
  591. .release = inet6_release,
  592. .bind = inet6_bind,
  593. .connect = inet_dgram_connect,
  594. .socketpair = sock_no_socketpair,
  595. .accept = sock_no_accept,
  596. .getname = l2tp_ip6_getname,
  597. .poll = datagram_poll,
  598. .ioctl = inet6_ioctl,
  599. .listen = sock_no_listen,
  600. .shutdown = inet_shutdown,
  601. .setsockopt = sock_common_setsockopt,
  602. .getsockopt = sock_common_getsockopt,
  603. .sendmsg = inet_sendmsg,
  604. .recvmsg = sock_common_recvmsg,
  605. .mmap = sock_no_mmap,
  606. .sendpage = sock_no_sendpage,
  607. #ifdef CONFIG_COMPAT
  608. .compat_setsockopt = compat_sock_common_setsockopt,
  609. .compat_getsockopt = compat_sock_common_getsockopt,
  610. #endif
  611. };
  612. static struct inet_protosw l2tp_ip6_protosw = {
  613. .type = SOCK_DGRAM,
  614. .protocol = IPPROTO_L2TP,
  615. .prot = &l2tp_ip6_prot,
  616. .ops = &l2tp_ip6_ops,
  617. .no_check = 0,
  618. };
  619. static struct inet6_protocol l2tp_ip6_protocol __read_mostly = {
  620. .handler = l2tp_ip6_recv,
  621. };
  622. static int __init l2tp_ip6_init(void)
  623. {
  624. int err;
  625. printk(KERN_INFO "L2TP IP encapsulation support for IPv6 (L2TPv3)\n");
  626. err = proto_register(&l2tp_ip6_prot, 1);
  627. if (err != 0)
  628. goto out;
  629. err = inet6_add_protocol(&l2tp_ip6_protocol, IPPROTO_L2TP);
  630. if (err)
  631. goto out1;
  632. inet6_register_protosw(&l2tp_ip6_protosw);
  633. return 0;
  634. out1:
  635. proto_unregister(&l2tp_ip6_prot);
  636. out:
  637. return err;
  638. }
  639. static void __exit l2tp_ip6_exit(void)
  640. {
  641. inet6_unregister_protosw(&l2tp_ip6_protosw);
  642. inet6_del_protocol(&l2tp_ip6_protocol, IPPROTO_L2TP);
  643. proto_unregister(&l2tp_ip6_prot);
  644. }
  645. module_init(l2tp_ip6_init);
  646. module_exit(l2tp_ip6_exit);
  647. MODULE_LICENSE("GPL");
  648. MODULE_AUTHOR("Chris Elston <celston@katalix.com>");
  649. MODULE_DESCRIPTION("L2TP IP encapsulation for IPv6");
  650. MODULE_VERSION("1.0");
  651. /* Use the value of SOCK_DGRAM (2) directory, because __stringify doesn't like
  652. * enums
  653. */
  654. MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_INET6, 2, IPPROTO_L2TP);