l2tp_ip.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* L2TPv3 IP encapsulation support
  3. *
  4. * Copyright (c) 2008,2009,2010 Katalix Systems Ltd
  5. */
  6. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  7. #include <asm/ioctls.h>
  8. #include <linux/icmp.h>
  9. #include <linux/module.h>
  10. #include <linux/skbuff.h>
  11. #include <linux/random.h>
  12. #include <linux/socket.h>
  13. #include <linux/l2tp.h>
  14. #include <linux/in.h>
  15. #include <net/sock.h>
  16. #include <net/ip.h>
  17. #include <net/icmp.h>
  18. #include <net/udp.h>
  19. #include <net/inet_common.h>
  20. #include <net/tcp_states.h>
  21. #include <net/protocol.h>
  22. #include <net/xfrm.h>
  23. #include <net/net_namespace.h>
  24. #include <net/netns/generic.h>
  25. #include "l2tp_core.h"
  26. /* per-net private data for this module */
  27. static unsigned int l2tp_ip_net_id;
  28. struct l2tp_ip_net {
  29. rwlock_t l2tp_ip_lock;
  30. struct hlist_head l2tp_ip_table;
  31. struct hlist_head l2tp_ip_bind_table;
  32. };
  33. struct l2tp_ip_sock {
  34. /* inet_sock has to be the first member of l2tp_ip_sock */
  35. struct inet_sock inet;
  36. u32 conn_id;
  37. u32 peer_conn_id;
  38. };
  39. static struct l2tp_ip_sock *l2tp_ip_sk(const struct sock *sk)
  40. {
  41. return (struct l2tp_ip_sock *)sk;
  42. }
  43. static struct l2tp_ip_net *l2tp_ip_pernet(const struct net *net)
  44. {
  45. return net_generic(net, l2tp_ip_net_id);
  46. }
  47. static struct sock *__l2tp_ip_bind_lookup(const struct net *net, __be32 laddr,
  48. __be32 raddr, int dif, u32 tunnel_id)
  49. {
  50. struct l2tp_ip_net *pn = l2tp_ip_pernet(net);
  51. struct sock *sk;
  52. sk_for_each_bound(sk, &pn->l2tp_ip_bind_table) {
  53. const struct l2tp_ip_sock *l2tp = l2tp_ip_sk(sk);
  54. const struct inet_sock *inet = inet_sk(sk);
  55. int bound_dev_if;
  56. if (!net_eq(sock_net(sk), net))
  57. continue;
  58. bound_dev_if = READ_ONCE(sk->sk_bound_dev_if);
  59. if (bound_dev_if && dif && bound_dev_if != dif)
  60. continue;
  61. if (inet->inet_rcv_saddr && laddr &&
  62. inet->inet_rcv_saddr != laddr)
  63. continue;
  64. if (inet->inet_daddr && raddr && inet->inet_daddr != raddr)
  65. continue;
  66. if (l2tp->conn_id != tunnel_id)
  67. continue;
  68. goto found;
  69. }
  70. sk = NULL;
  71. found:
  72. return sk;
  73. }
  74. /* When processing receive frames, there are two cases to
  75. * consider. Data frames consist of a non-zero session-id and an
  76. * optional cookie. Control frames consist of a regular L2TP header
  77. * preceded by 32-bits of zeros.
  78. *
  79. * L2TPv3 Session Header Over IP
  80. *
  81. * 0 1 2 3
  82. * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  83. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  84. * | Session ID |
  85. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  86. * | Cookie (optional, maximum 64 bits)...
  87. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  88. * |
  89. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  90. *
  91. * L2TPv3 Control Message Header Over IP
  92. *
  93. * 0 1 2 3
  94. * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  95. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  96. * | (32 bits of zeros) |
  97. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  98. * |T|L|x|x|S|x|x|x|x|x|x|x| Ver | Length |
  99. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  100. * | Control Connection ID |
  101. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  102. * | Ns | Nr |
  103. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  104. *
  105. * All control frames are passed to userspace.
  106. */
  107. static int l2tp_ip_recv(struct sk_buff *skb)
  108. {
  109. struct net *net = dev_net(skb->dev);
  110. struct l2tp_ip_net *pn;
  111. struct sock *sk;
  112. u32 session_id;
  113. u32 tunnel_id;
  114. unsigned char *ptr, *optr;
  115. struct l2tp_session *session;
  116. struct l2tp_tunnel *tunnel = NULL;
  117. struct iphdr *iph;
  118. pn = l2tp_ip_pernet(net);
  119. if (!pskb_may_pull(skb, 4))
  120. goto discard;
  121. /* Point to L2TP header */
  122. optr = skb->data;
  123. 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_v3_session_get(net, NULL, session_id);
  136. if (!session)
  137. goto discard;
  138. tunnel = session->tunnel;
  139. if (!tunnel)
  140. goto discard_sess;
  141. if (l2tp_v3_ensure_opt_in_linear(session, skb, &ptr, &optr))
  142. goto discard_sess;
  143. l2tp_recv_common(session, skb, ptr, optr, 0, skb->len);
  144. l2tp_session_put(session);
  145. return 0;
  146. pass_up:
  147. /* Get the tunnel_id from the L2TP header */
  148. if (!pskb_may_pull(skb, 12))
  149. goto discard;
  150. if ((skb->data[0] & 0xc0) != 0xc0)
  151. goto discard;
  152. tunnel_id = ntohl(*(__be32 *)&skb->data[4]);
  153. iph = (struct iphdr *)skb_network_header(skb);
  154. read_lock_bh(&pn->l2tp_ip_lock);
  155. sk = __l2tp_ip_bind_lookup(net, iph->daddr, iph->saddr, inet_iif(skb),
  156. tunnel_id);
  157. if (!sk) {
  158. read_unlock_bh(&pn->l2tp_ip_lock);
  159. goto discard;
  160. }
  161. sock_hold(sk);
  162. read_unlock_bh(&pn->l2tp_ip_lock);
  163. if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb))
  164. goto discard_put;
  165. nf_reset_ct(skb);
  166. return sk_receive_skb(sk, skb, 1);
  167. discard_sess:
  168. l2tp_session_put(session);
  169. goto discard;
  170. discard_put:
  171. sock_put(sk);
  172. discard:
  173. kfree_skb(skb);
  174. return 0;
  175. }
  176. static int l2tp_ip_hash(struct sock *sk)
  177. {
  178. struct l2tp_ip_net *pn = l2tp_ip_pernet(sock_net(sk));
  179. if (sk_unhashed(sk)) {
  180. write_lock_bh(&pn->l2tp_ip_lock);
  181. sk_add_node(sk, &pn->l2tp_ip_table);
  182. write_unlock_bh(&pn->l2tp_ip_lock);
  183. }
  184. return 0;
  185. }
  186. static void l2tp_ip_unhash(struct sock *sk)
  187. {
  188. struct l2tp_ip_net *pn = l2tp_ip_pernet(sock_net(sk));
  189. if (sk_unhashed(sk))
  190. return;
  191. write_lock_bh(&pn->l2tp_ip_lock);
  192. sk_del_node_init(sk);
  193. write_unlock_bh(&pn->l2tp_ip_lock);
  194. }
  195. static int l2tp_ip_open(struct sock *sk)
  196. {
  197. /* Prevent autobind. We don't have ports. */
  198. inet_sk(sk)->inet_num = IPPROTO_L2TP;
  199. l2tp_ip_hash(sk);
  200. return 0;
  201. }
  202. static void l2tp_ip_close(struct sock *sk, long timeout)
  203. {
  204. struct l2tp_ip_net *pn = l2tp_ip_pernet(sock_net(sk));
  205. write_lock_bh(&pn->l2tp_ip_lock);
  206. hlist_del_init(&sk->sk_bind_node);
  207. sk_del_node_init(sk);
  208. write_unlock_bh(&pn->l2tp_ip_lock);
  209. sk_common_release(sk);
  210. }
  211. static void l2tp_ip_destroy_sock(struct sock *sk)
  212. {
  213. struct l2tp_tunnel *tunnel;
  214. __skb_queue_purge(&sk->sk_write_queue);
  215. tunnel = l2tp_sk_to_tunnel(sk);
  216. if (tunnel) {
  217. l2tp_tunnel_delete(tunnel);
  218. l2tp_tunnel_put(tunnel);
  219. }
  220. }
  221. static int l2tp_ip_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len)
  222. {
  223. struct inet_sock *inet = inet_sk(sk);
  224. struct sockaddr_l2tpip *addr = (struct sockaddr_l2tpip *)uaddr;
  225. struct net *net = sock_net(sk);
  226. struct l2tp_ip_net *pn;
  227. int ret;
  228. int chk_addr_ret;
  229. if (addr_len < sizeof(struct sockaddr_l2tpip))
  230. return -EINVAL;
  231. if (addr->l2tp_family != AF_INET)
  232. return -EINVAL;
  233. lock_sock(sk);
  234. ret = -EINVAL;
  235. if (!sock_flag(sk, SOCK_ZAPPED))
  236. goto out;
  237. if (sk->sk_state != TCP_CLOSE)
  238. goto out;
  239. chk_addr_ret = inet_addr_type(net, addr->l2tp_addr.s_addr);
  240. ret = -EADDRNOTAVAIL;
  241. if (addr->l2tp_addr.s_addr && chk_addr_ret != RTN_LOCAL &&
  242. chk_addr_ret != RTN_MULTICAST && chk_addr_ret != RTN_BROADCAST)
  243. goto out;
  244. if (addr->l2tp_addr.s_addr) {
  245. inet->inet_rcv_saddr = addr->l2tp_addr.s_addr;
  246. inet->inet_saddr = addr->l2tp_addr.s_addr;
  247. }
  248. if (chk_addr_ret == RTN_MULTICAST || chk_addr_ret == RTN_BROADCAST)
  249. inet->inet_saddr = 0; /* Use device */
  250. pn = l2tp_ip_pernet(net);
  251. write_lock_bh(&pn->l2tp_ip_lock);
  252. if (__l2tp_ip_bind_lookup(net, addr->l2tp_addr.s_addr, 0,
  253. sk->sk_bound_dev_if, addr->l2tp_conn_id)) {
  254. write_unlock_bh(&pn->l2tp_ip_lock);
  255. ret = -EADDRINUSE;
  256. goto out;
  257. }
  258. sk_dst_reset(sk);
  259. l2tp_ip_sk(sk)->conn_id = addr->l2tp_conn_id;
  260. sk_add_bind_node(sk, &pn->l2tp_ip_bind_table);
  261. sk_del_node_init(sk);
  262. write_unlock_bh(&pn->l2tp_ip_lock);
  263. ret = 0;
  264. sock_reset_flag(sk, SOCK_ZAPPED);
  265. out:
  266. release_sock(sk);
  267. return ret;
  268. }
  269. static int l2tp_ip_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
  270. {
  271. struct sockaddr_l2tpip *lsa = (struct sockaddr_l2tpip *)uaddr;
  272. struct l2tp_ip_net *pn = l2tp_ip_pernet(sock_net(sk));
  273. int rc;
  274. if (addr_len < sizeof(*lsa))
  275. return -EINVAL;
  276. if (ipv4_is_multicast(lsa->l2tp_addr.s_addr))
  277. return -EINVAL;
  278. lock_sock(sk);
  279. /* Must bind first - autobinding does not work */
  280. if (sock_flag(sk, SOCK_ZAPPED)) {
  281. rc = -EINVAL;
  282. goto out_sk;
  283. }
  284. rc = __ip4_datagram_connect(sk, uaddr, addr_len);
  285. if (rc < 0)
  286. goto out_sk;
  287. l2tp_ip_sk(sk)->peer_conn_id = lsa->l2tp_conn_id;
  288. write_lock_bh(&pn->l2tp_ip_lock);
  289. hlist_del_init(&sk->sk_bind_node);
  290. sk_add_bind_node(sk, &pn->l2tp_ip_bind_table);
  291. write_unlock_bh(&pn->l2tp_ip_lock);
  292. out_sk:
  293. release_sock(sk);
  294. return rc;
  295. }
  296. static int l2tp_ip_disconnect(struct sock *sk, int flags)
  297. {
  298. if (sock_flag(sk, SOCK_ZAPPED))
  299. return 0;
  300. return __udp_disconnect(sk, flags);
  301. }
  302. static int l2tp_ip_getname(struct socket *sock, struct sockaddr *uaddr,
  303. int peer)
  304. {
  305. struct sock *sk = sock->sk;
  306. struct inet_sock *inet = inet_sk(sk);
  307. struct l2tp_ip_sock *lsk = l2tp_ip_sk(sk);
  308. struct sockaddr_l2tpip *lsa = (struct sockaddr_l2tpip *)uaddr;
  309. memset(lsa, 0, sizeof(*lsa));
  310. lsa->l2tp_family = AF_INET;
  311. if (peer) {
  312. if (!inet->inet_dport)
  313. return -ENOTCONN;
  314. lsa->l2tp_conn_id = lsk->peer_conn_id;
  315. lsa->l2tp_addr.s_addr = inet->inet_daddr;
  316. } else {
  317. __be32 addr = inet->inet_rcv_saddr;
  318. if (!addr)
  319. addr = inet->inet_saddr;
  320. lsa->l2tp_conn_id = lsk->conn_id;
  321. lsa->l2tp_addr.s_addr = addr;
  322. }
  323. return sizeof(*lsa);
  324. }
  325. static int l2tp_ip_backlog_recv(struct sock *sk, struct sk_buff *skb)
  326. {
  327. int rc;
  328. /* Charge it to the socket, dropping if the queue is full. */
  329. rc = sock_queue_rcv_skb(sk, skb);
  330. if (rc < 0)
  331. goto drop;
  332. return 0;
  333. drop:
  334. IP_INC_STATS(sock_net(sk), IPSTATS_MIB_INDISCARDS);
  335. kfree_skb(skb);
  336. return 0;
  337. }
  338. /* Userspace will call sendmsg() on the tunnel socket to send L2TP
  339. * control frames.
  340. */
  341. static int l2tp_ip_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
  342. {
  343. struct sk_buff *skb;
  344. int rc;
  345. struct inet_sock *inet = inet_sk(sk);
  346. struct rtable *rt = NULL;
  347. struct flowi4 *fl4;
  348. int connected = 0;
  349. __be32 daddr;
  350. lock_sock(sk);
  351. rc = -ENOTCONN;
  352. if (sock_flag(sk, SOCK_DEAD))
  353. goto out;
  354. /* Get and verify the address. */
  355. if (msg->msg_name) {
  356. DECLARE_SOCKADDR(struct sockaddr_l2tpip *, lip, msg->msg_name);
  357. rc = -EINVAL;
  358. if (msg->msg_namelen < sizeof(*lip))
  359. goto out;
  360. if (lip->l2tp_family != AF_INET) {
  361. rc = -EAFNOSUPPORT;
  362. if (lip->l2tp_family != AF_UNSPEC)
  363. goto out;
  364. }
  365. daddr = lip->l2tp_addr.s_addr;
  366. } else {
  367. rc = -EDESTADDRREQ;
  368. if (sk->sk_state != TCP_ESTABLISHED)
  369. goto out;
  370. daddr = inet->inet_daddr;
  371. connected = 1;
  372. }
  373. /* Allocate a socket buffer */
  374. rc = -ENOMEM;
  375. skb = sock_wmalloc(sk, 2 + NET_SKB_PAD + sizeof(struct iphdr) +
  376. 4 + len, 0, GFP_KERNEL);
  377. if (!skb)
  378. goto error;
  379. /* Reserve space for headers, putting IP header on 4-byte boundary. */
  380. skb_reserve(skb, 2 + NET_SKB_PAD);
  381. skb_reset_network_header(skb);
  382. skb_reserve(skb, sizeof(struct iphdr));
  383. skb_reset_transport_header(skb);
  384. /* Insert 0 session_id */
  385. *((__be32 *)skb_put(skb, 4)) = 0;
  386. /* Copy user data into skb */
  387. rc = memcpy_from_msg(skb_put(skb, len), msg, len);
  388. if (rc < 0) {
  389. kfree_skb(skb);
  390. goto error;
  391. }
  392. fl4 = &inet->cork.fl.u.ip4;
  393. if (connected)
  394. rt = dst_rtable(__sk_dst_check(sk, 0));
  395. rcu_read_lock();
  396. if (!rt) {
  397. const struct ip_options_rcu *inet_opt;
  398. inet_opt = rcu_dereference(inet->inet_opt);
  399. /* Use correct destination address if we have options. */
  400. if (inet_opt && inet_opt->opt.srr)
  401. daddr = inet_opt->opt.faddr;
  402. /* If this fails, retransmit mechanism of transport layer will
  403. * keep trying until route appears or the connection times
  404. * itself out.
  405. */
  406. rt = ip_route_output_ports(sock_net(sk), fl4, sk,
  407. daddr, inet->inet_saddr,
  408. inet->inet_dport, inet->inet_sport,
  409. sk->sk_protocol, ip_sock_rt_tos(sk),
  410. sk->sk_bound_dev_if);
  411. if (IS_ERR(rt))
  412. goto no_route;
  413. if (connected) {
  414. sk_setup_caps(sk, &rt->dst);
  415. } else {
  416. skb_dst_set(skb, &rt->dst);
  417. goto xmit;
  418. }
  419. }
  420. /* We don't need to clone dst here, it is guaranteed to not disappear.
  421. * __dev_xmit_skb() might force a refcount if needed.
  422. */
  423. skb_dst_set_noref(skb, &rt->dst);
  424. xmit:
  425. /* Queue the packet to IP for output */
  426. rc = ip_queue_xmit(sk, skb, &inet->cork.fl);
  427. rcu_read_unlock();
  428. error:
  429. if (rc >= 0)
  430. rc = len;
  431. out:
  432. release_sock(sk);
  433. return rc;
  434. no_route:
  435. rcu_read_unlock();
  436. IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTNOROUTES);
  437. kfree_skb(skb);
  438. rc = -EHOSTUNREACH;
  439. goto out;
  440. }
  441. static int l2tp_ip_recvmsg(struct sock *sk, struct msghdr *msg,
  442. size_t len, int flags, int *addr_len)
  443. {
  444. struct inet_sock *inet = inet_sk(sk);
  445. size_t copied = 0;
  446. int err = -EOPNOTSUPP;
  447. DECLARE_SOCKADDR(struct sockaddr_in *, sin, msg->msg_name);
  448. struct sk_buff *skb;
  449. if (flags & MSG_OOB)
  450. goto out;
  451. skb = skb_recv_datagram(sk, flags, &err);
  452. if (!skb)
  453. goto out;
  454. copied = skb->len;
  455. if (len < copied) {
  456. msg->msg_flags |= MSG_TRUNC;
  457. copied = len;
  458. }
  459. err = skb_copy_datagram_msg(skb, 0, msg, copied);
  460. if (err)
  461. goto done;
  462. sock_recv_timestamp(msg, sk, skb);
  463. /* Copy the address. */
  464. if (sin) {
  465. sin->sin_family = AF_INET;
  466. sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
  467. sin->sin_port = 0;
  468. memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
  469. *addr_len = sizeof(*sin);
  470. }
  471. if (inet_cmsg_flags(inet))
  472. ip_cmsg_recv(msg, skb);
  473. if (flags & MSG_TRUNC)
  474. copied = skb->len;
  475. done:
  476. skb_free_datagram(sk, skb);
  477. out:
  478. return err ? err : copied;
  479. }
  480. int l2tp_ioctl(struct sock *sk, int cmd, int *karg)
  481. {
  482. struct sk_buff *skb;
  483. switch (cmd) {
  484. case SIOCOUTQ:
  485. *karg = sk_wmem_alloc_get(sk);
  486. break;
  487. case SIOCINQ:
  488. spin_lock_bh(&sk->sk_receive_queue.lock);
  489. skb = skb_peek(&sk->sk_receive_queue);
  490. *karg = skb ? skb->len : 0;
  491. spin_unlock_bh(&sk->sk_receive_queue.lock);
  492. break;
  493. default:
  494. return -ENOIOCTLCMD;
  495. }
  496. return 0;
  497. }
  498. EXPORT_SYMBOL_GPL(l2tp_ioctl);
  499. static struct proto l2tp_ip_prot = {
  500. .name = "L2TP/IP",
  501. .owner = THIS_MODULE,
  502. .init = l2tp_ip_open,
  503. .close = l2tp_ip_close,
  504. .bind = l2tp_ip_bind,
  505. .connect = l2tp_ip_connect,
  506. .disconnect = l2tp_ip_disconnect,
  507. .ioctl = l2tp_ioctl,
  508. .destroy = l2tp_ip_destroy_sock,
  509. .setsockopt = ip_setsockopt,
  510. .getsockopt = ip_getsockopt,
  511. .sendmsg = l2tp_ip_sendmsg,
  512. .recvmsg = l2tp_ip_recvmsg,
  513. .backlog_rcv = l2tp_ip_backlog_recv,
  514. .hash = l2tp_ip_hash,
  515. .unhash = l2tp_ip_unhash,
  516. .obj_size = sizeof(struct l2tp_ip_sock),
  517. };
  518. static const struct proto_ops l2tp_ip_ops = {
  519. .family = PF_INET,
  520. .owner = THIS_MODULE,
  521. .release = inet_release,
  522. .bind = inet_bind,
  523. .connect = inet_dgram_connect,
  524. .socketpair = sock_no_socketpair,
  525. .accept = sock_no_accept,
  526. .getname = l2tp_ip_getname,
  527. .poll = datagram_poll,
  528. .ioctl = inet_ioctl,
  529. .gettstamp = sock_gettstamp,
  530. .listen = sock_no_listen,
  531. .shutdown = inet_shutdown,
  532. .setsockopt = sock_common_setsockopt,
  533. .getsockopt = sock_common_getsockopt,
  534. .sendmsg = inet_sendmsg,
  535. .recvmsg = sock_common_recvmsg,
  536. .mmap = sock_no_mmap,
  537. };
  538. static struct inet_protosw l2tp_ip_protosw = {
  539. .type = SOCK_DGRAM,
  540. .protocol = IPPROTO_L2TP,
  541. .prot = &l2tp_ip_prot,
  542. .ops = &l2tp_ip_ops,
  543. };
  544. static struct net_protocol l2tp_ip_protocol __read_mostly = {
  545. .handler = l2tp_ip_recv,
  546. };
  547. static __net_init int l2tp_ip_init_net(struct net *net)
  548. {
  549. struct l2tp_ip_net *pn = net_generic(net, l2tp_ip_net_id);
  550. rwlock_init(&pn->l2tp_ip_lock);
  551. INIT_HLIST_HEAD(&pn->l2tp_ip_table);
  552. INIT_HLIST_HEAD(&pn->l2tp_ip_bind_table);
  553. return 0;
  554. }
  555. static __net_exit void l2tp_ip_exit_net(struct net *net)
  556. {
  557. struct l2tp_ip_net *pn = l2tp_ip_pernet(net);
  558. write_lock_bh(&pn->l2tp_ip_lock);
  559. WARN_ON_ONCE(hlist_count_nodes(&pn->l2tp_ip_table) != 0);
  560. WARN_ON_ONCE(hlist_count_nodes(&pn->l2tp_ip_bind_table) != 0);
  561. write_unlock_bh(&pn->l2tp_ip_lock);
  562. }
  563. static struct pernet_operations l2tp_ip_net_ops = {
  564. .init = l2tp_ip_init_net,
  565. .exit = l2tp_ip_exit_net,
  566. .id = &l2tp_ip_net_id,
  567. .size = sizeof(struct l2tp_ip_net),
  568. };
  569. static int __init l2tp_ip_init(void)
  570. {
  571. int err;
  572. pr_info("L2TP IP encapsulation support (L2TPv3)\n");
  573. err = register_pernet_device(&l2tp_ip_net_ops);
  574. if (err)
  575. goto out;
  576. err = proto_register(&l2tp_ip_prot, 1);
  577. if (err != 0)
  578. goto out1;
  579. err = inet_add_protocol(&l2tp_ip_protocol, IPPROTO_L2TP);
  580. if (err)
  581. goto out2;
  582. inet_register_protosw(&l2tp_ip_protosw);
  583. return 0;
  584. out2:
  585. proto_unregister(&l2tp_ip_prot);
  586. out1:
  587. unregister_pernet_device(&l2tp_ip_net_ops);
  588. out:
  589. return err;
  590. }
  591. static void __exit l2tp_ip_exit(void)
  592. {
  593. inet_unregister_protosw(&l2tp_ip_protosw);
  594. inet_del_protocol(&l2tp_ip_protocol, IPPROTO_L2TP);
  595. proto_unregister(&l2tp_ip_prot);
  596. unregister_pernet_device(&l2tp_ip_net_ops);
  597. }
  598. module_init(l2tp_ip_init);
  599. module_exit(l2tp_ip_exit);
  600. MODULE_LICENSE("GPL");
  601. MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
  602. MODULE_DESCRIPTION("L2TP over IP");
  603. MODULE_VERSION("1.0");
  604. /* Use the values of SOCK_DGRAM (2) as type and IPPROTO_L2TP (115) as protocol,
  605. * because __stringify doesn't like enums
  606. */
  607. MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_INET, 115, 2);
  608. MODULE_ALIAS_NET_PF_PROTO(PF_INET, 115);