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