l2tp_ip.c 17 KB

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