l2tp_ip.c 16 KB

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