l2tp_ip.c 16 KB

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