l2tp_ip.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  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 <linux/icmp.h>
  13. #include <linux/module.h>
  14. #include <linux/skbuff.h>
  15. #include <linux/random.h>
  16. #include <linux/socket.h>
  17. #include <linux/l2tp.h>
  18. #include <linux/in.h>
  19. #include <net/sock.h>
  20. #include <net/ip.h>
  21. #include <net/icmp.h>
  22. #include <net/udp.h>
  23. #include <net/inet_common.h>
  24. #include <net/inet_hashtables.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(struct net *net, __be32 laddr, int dif, u32 tunnel_id)
  43. {
  44. struct sock *sk;
  45. sk_for_each_bound(sk, &l2tp_ip_bind_table) {
  46. struct inet_sock *inet = inet_sk(sk);
  47. struct l2tp_ip_sock *l2tp = l2tp_ip_sk(sk);
  48. if (l2tp == NULL)
  49. continue;
  50. if ((l2tp->conn_id == tunnel_id) &&
  51. net_eq(sock_net(sk), net) &&
  52. !(inet->inet_rcv_saddr && inet->inet_rcv_saddr != laddr) &&
  53. !(sk->sk_bound_dev_if && sk->sk_bound_dev_if != dif))
  54. goto found;
  55. }
  56. sk = NULL;
  57. found:
  58. return sk;
  59. }
  60. static inline struct sock *l2tp_ip_bind_lookup(struct net *net, __be32 laddr, int dif, u32 tunnel_id)
  61. {
  62. struct sock *sk = __l2tp_ip_bind_lookup(net, laddr, dif, tunnel_id);
  63. if (sk)
  64. sock_hold(sk);
  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. int length;
  110. /* Point to L2TP header */
  111. optr = ptr = skb->data;
  112. if (!pskb_may_pull(skb, 4))
  113. goto discard;
  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_find(net, NULL, session_id);
  126. if (session == NULL)
  127. goto discard;
  128. tunnel = session->tunnel;
  129. if (tunnel == NULL)
  130. goto discard;
  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;
  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. return 0;
  141. pass_up:
  142. /* Get the tunnel_id from the L2TP header */
  143. if (!pskb_may_pull(skb, 12))
  144. goto discard;
  145. if ((skb->data[0] & 0xc0) != 0xc0)
  146. goto discard;
  147. tunnel_id = ntohl(*(__be32 *) &skb->data[4]);
  148. tunnel = l2tp_tunnel_find(net, tunnel_id);
  149. if (tunnel != NULL)
  150. sk = tunnel->sock;
  151. else {
  152. struct iphdr *iph = (struct iphdr *) skb_network_header(skb);
  153. read_lock_bh(&l2tp_ip_lock);
  154. sk = __l2tp_ip_bind_lookup(net, iph->daddr, 0, tunnel_id);
  155. read_unlock_bh(&l2tp_ip_lock);
  156. }
  157. if (sk == NULL)
  158. goto discard;
  159. sock_hold(sk);
  160. if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb))
  161. goto discard_put;
  162. nf_reset(skb);
  163. return sk_receive_skb(sk, skb, 1);
  164. discard_put:
  165. sock_put(sk);
  166. discard:
  167. kfree_skb(skb);
  168. return 0;
  169. }
  170. static int l2tp_ip_open(struct sock *sk)
  171. {
  172. /* Prevent autobind. We don't have ports. */
  173. inet_sk(sk)->inet_num = IPPROTO_L2TP;
  174. write_lock_bh(&l2tp_ip_lock);
  175. sk_add_node(sk, &l2tp_ip_table);
  176. write_unlock_bh(&l2tp_ip_lock);
  177. return 0;
  178. }
  179. static void l2tp_ip_close(struct sock *sk, long timeout)
  180. {
  181. write_lock_bh(&l2tp_ip_lock);
  182. hlist_del_init(&sk->sk_bind_node);
  183. sk_del_node_init(sk);
  184. write_unlock_bh(&l2tp_ip_lock);
  185. sk_common_release(sk);
  186. }
  187. static void l2tp_ip_destroy_sock(struct sock *sk)
  188. {
  189. struct sk_buff *skb;
  190. while ((skb = __skb_dequeue_tail(&sk->sk_write_queue)) != NULL)
  191. kfree_skb(skb);
  192. sk_refcnt_debug_dec(sk);
  193. }
  194. static int l2tp_ip_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len)
  195. {
  196. struct inet_sock *inet = inet_sk(sk);
  197. struct sockaddr_l2tpip *addr = (struct sockaddr_l2tpip *) uaddr;
  198. struct net *net = sock_net(sk);
  199. int ret;
  200. int chk_addr_ret;
  201. if (!sock_flag(sk, SOCK_ZAPPED))
  202. return -EINVAL;
  203. if (addr_len < sizeof(struct sockaddr_l2tpip))
  204. return -EINVAL;
  205. if (addr->l2tp_family != AF_INET)
  206. return -EINVAL;
  207. ret = -EADDRINUSE;
  208. read_lock_bh(&l2tp_ip_lock);
  209. if (__l2tp_ip_bind_lookup(net, addr->l2tp_addr.s_addr,
  210. sk->sk_bound_dev_if, addr->l2tp_conn_id))
  211. goto out_in_use;
  212. read_unlock_bh(&l2tp_ip_lock);
  213. lock_sock(sk);
  214. if (sk->sk_state != TCP_CLOSE || addr_len < sizeof(struct sockaddr_l2tpip))
  215. goto out;
  216. chk_addr_ret = inet_addr_type(net, addr->l2tp_addr.s_addr);
  217. ret = -EADDRNOTAVAIL;
  218. if (addr->l2tp_addr.s_addr && chk_addr_ret != RTN_LOCAL &&
  219. chk_addr_ret != RTN_MULTICAST && chk_addr_ret != RTN_BROADCAST)
  220. goto out;
  221. if (addr->l2tp_addr.s_addr)
  222. inet->inet_rcv_saddr = inet->inet_saddr = addr->l2tp_addr.s_addr;
  223. if (chk_addr_ret == RTN_MULTICAST || chk_addr_ret == RTN_BROADCAST)
  224. inet->inet_saddr = 0; /* Use device */
  225. sk_dst_reset(sk);
  226. l2tp_ip_sk(sk)->conn_id = addr->l2tp_conn_id;
  227. write_lock_bh(&l2tp_ip_lock);
  228. sk_add_bind_node(sk, &l2tp_ip_bind_table);
  229. sk_del_node_init(sk);
  230. write_unlock_bh(&l2tp_ip_lock);
  231. ret = 0;
  232. sock_reset_flag(sk, SOCK_ZAPPED);
  233. out:
  234. release_sock(sk);
  235. return ret;
  236. out_in_use:
  237. read_unlock_bh(&l2tp_ip_lock);
  238. return ret;
  239. }
  240. static int l2tp_ip_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
  241. {
  242. struct sockaddr_l2tpip *lsa = (struct sockaddr_l2tpip *) uaddr;
  243. int rc;
  244. if (sock_flag(sk, SOCK_ZAPPED)) /* Must bind first - autobinding does not work */
  245. return -EINVAL;
  246. if (addr_len < sizeof(*lsa))
  247. return -EINVAL;
  248. if (ipv4_is_multicast(lsa->l2tp_addr.s_addr))
  249. return -EINVAL;
  250. rc = ip4_datagram_connect(sk, uaddr, addr_len);
  251. if (rc < 0)
  252. return rc;
  253. lock_sock(sk);
  254. l2tp_ip_sk(sk)->peer_conn_id = lsa->l2tp_conn_id;
  255. write_lock_bh(&l2tp_ip_lock);
  256. hlist_del_init(&sk->sk_bind_node);
  257. sk_add_bind_node(sk, &l2tp_ip_bind_table);
  258. write_unlock_bh(&l2tp_ip_lock);
  259. release_sock(sk);
  260. return rc;
  261. }
  262. static int l2tp_ip_disconnect(struct sock *sk, int flags)
  263. {
  264. if (sock_flag(sk, SOCK_ZAPPED))
  265. return 0;
  266. return udp_disconnect(sk, flags);
  267. }
  268. static int l2tp_ip_getname(struct socket *sock, struct sockaddr *uaddr,
  269. int *uaddr_len, int peer)
  270. {
  271. struct sock *sk = sock->sk;
  272. struct inet_sock *inet = inet_sk(sk);
  273. struct l2tp_ip_sock *lsk = l2tp_ip_sk(sk);
  274. struct sockaddr_l2tpip *lsa = (struct sockaddr_l2tpip *)uaddr;
  275. memset(lsa, 0, sizeof(*lsa));
  276. lsa->l2tp_family = AF_INET;
  277. if (peer) {
  278. if (!inet->inet_dport)
  279. return -ENOTCONN;
  280. lsa->l2tp_conn_id = lsk->peer_conn_id;
  281. lsa->l2tp_addr.s_addr = inet->inet_daddr;
  282. } else {
  283. __be32 addr = inet->inet_rcv_saddr;
  284. if (!addr)
  285. addr = inet->inet_saddr;
  286. lsa->l2tp_conn_id = lsk->conn_id;
  287. lsa->l2tp_addr.s_addr = addr;
  288. }
  289. *uaddr_len = sizeof(*lsa);
  290. return 0;
  291. }
  292. static int l2tp_ip_backlog_recv(struct sock *sk, struct sk_buff *skb)
  293. {
  294. int rc;
  295. /* Charge it to the socket, dropping if the queue is full. */
  296. rc = sock_queue_rcv_skb(sk, skb);
  297. if (rc < 0)
  298. goto drop;
  299. return 0;
  300. drop:
  301. IP_INC_STATS(sock_net(sk), IPSTATS_MIB_INDISCARDS);
  302. kfree_skb(skb);
  303. return -1;
  304. }
  305. /* Userspace will call sendmsg() on the tunnel socket to send L2TP
  306. * control frames.
  307. */
  308. static int l2tp_ip_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, size_t len)
  309. {
  310. struct sk_buff *skb;
  311. int rc;
  312. struct inet_sock *inet = inet_sk(sk);
  313. struct rtable *rt = NULL;
  314. struct flowi4 *fl4;
  315. int connected = 0;
  316. __be32 daddr;
  317. lock_sock(sk);
  318. rc = -ENOTCONN;
  319. if (sock_flag(sk, SOCK_DEAD))
  320. goto out;
  321. /* Get and verify the address. */
  322. if (msg->msg_name) {
  323. struct sockaddr_l2tpip *lip = (struct sockaddr_l2tpip *) msg->msg_name;
  324. rc = -EINVAL;
  325. if (msg->msg_namelen < sizeof(*lip))
  326. goto out;
  327. if (lip->l2tp_family != AF_INET) {
  328. rc = -EAFNOSUPPORT;
  329. if (lip->l2tp_family != AF_UNSPEC)
  330. goto out;
  331. }
  332. daddr = lip->l2tp_addr.s_addr;
  333. } else {
  334. rc = -EDESTADDRREQ;
  335. if (sk->sk_state != TCP_ESTABLISHED)
  336. goto out;
  337. daddr = inet->inet_daddr;
  338. connected = 1;
  339. }
  340. /* Allocate a socket buffer */
  341. rc = -ENOMEM;
  342. skb = sock_wmalloc(sk, 2 + NET_SKB_PAD + sizeof(struct iphdr) +
  343. 4 + len, 0, GFP_KERNEL);
  344. if (!skb)
  345. goto error;
  346. /* Reserve space for headers, putting IP header on 4-byte boundary. */
  347. skb_reserve(skb, 2 + NET_SKB_PAD);
  348. skb_reset_network_header(skb);
  349. skb_reserve(skb, sizeof(struct iphdr));
  350. skb_reset_transport_header(skb);
  351. /* Insert 0 session_id */
  352. *((__be32 *) skb_put(skb, 4)) = 0;
  353. /* Copy user data into skb */
  354. rc = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len);
  355. if (rc < 0) {
  356. kfree_skb(skb);
  357. goto error;
  358. }
  359. fl4 = &inet->cork.fl.u.ip4;
  360. if (connected)
  361. rt = (struct rtable *) __sk_dst_check(sk, 0);
  362. rcu_read_lock();
  363. if (rt == NULL) {
  364. const struct ip_options_rcu *inet_opt;
  365. inet_opt = rcu_dereference(inet->inet_opt);
  366. /* Use correct destination address if we have options. */
  367. if (inet_opt && inet_opt->opt.srr)
  368. daddr = inet_opt->opt.faddr;
  369. /* If this fails, retransmit mechanism of transport layer will
  370. * keep trying until route appears or the connection times
  371. * itself out.
  372. */
  373. rt = ip_route_output_ports(sock_net(sk), fl4, sk,
  374. daddr, inet->inet_saddr,
  375. inet->inet_dport, inet->inet_sport,
  376. sk->sk_protocol, RT_CONN_FLAGS(sk),
  377. sk->sk_bound_dev_if);
  378. if (IS_ERR(rt))
  379. goto no_route;
  380. if (connected) {
  381. sk_setup_caps(sk, &rt->dst);
  382. } else {
  383. skb_dst_set(skb, &rt->dst);
  384. goto xmit;
  385. }
  386. }
  387. /* We dont need to clone dst here, it is guaranteed to not disappear.
  388. * __dev_xmit_skb() might force a refcount if needed.
  389. */
  390. skb_dst_set_noref(skb, &rt->dst);
  391. xmit:
  392. /* Queue the packet to IP for output */
  393. rc = ip_queue_xmit(skb, &inet->cork.fl);
  394. rcu_read_unlock();
  395. error:
  396. if (rc >= 0)
  397. rc = len;
  398. out:
  399. release_sock(sk);
  400. return rc;
  401. no_route:
  402. rcu_read_unlock();
  403. IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTNOROUTES);
  404. kfree_skb(skb);
  405. rc = -EHOSTUNREACH;
  406. goto out;
  407. }
  408. static int l2tp_ip_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
  409. size_t len, int noblock, int flags, int *addr_len)
  410. {
  411. struct inet_sock *inet = inet_sk(sk);
  412. size_t copied = 0;
  413. int err = -EOPNOTSUPP;
  414. struct sockaddr_in *sin = (struct sockaddr_in *)msg->msg_name;
  415. struct sk_buff *skb;
  416. if (flags & MSG_OOB)
  417. goto out;
  418. if (addr_len)
  419. *addr_len = sizeof(*sin);
  420. skb = skb_recv_datagram(sk, flags, noblock, &err);
  421. if (!skb)
  422. goto out;
  423. copied = skb->len;
  424. if (len < copied) {
  425. msg->msg_flags |= MSG_TRUNC;
  426. copied = len;
  427. }
  428. err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
  429. if (err)
  430. goto done;
  431. sock_recv_timestamp(msg, sk, skb);
  432. /* Copy the address. */
  433. if (sin) {
  434. sin->sin_family = AF_INET;
  435. sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
  436. sin->sin_port = 0;
  437. memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
  438. }
  439. if (inet->cmsg_flags)
  440. ip_cmsg_recv(msg, skb);
  441. if (flags & MSG_TRUNC)
  442. copied = skb->len;
  443. done:
  444. skb_free_datagram(sk, skb);
  445. out:
  446. return err ? err : copied;
  447. }
  448. static struct proto l2tp_ip_prot = {
  449. .name = "L2TP/IP",
  450. .owner = THIS_MODULE,
  451. .init = l2tp_ip_open,
  452. .close = l2tp_ip_close,
  453. .bind = l2tp_ip_bind,
  454. .connect = l2tp_ip_connect,
  455. .disconnect = l2tp_ip_disconnect,
  456. .ioctl = udp_ioctl,
  457. .destroy = l2tp_ip_destroy_sock,
  458. .setsockopt = ip_setsockopt,
  459. .getsockopt = ip_getsockopt,
  460. .sendmsg = l2tp_ip_sendmsg,
  461. .recvmsg = l2tp_ip_recvmsg,
  462. .backlog_rcv = l2tp_ip_backlog_recv,
  463. .hash = inet_hash,
  464. .unhash = inet_unhash,
  465. .obj_size = sizeof(struct l2tp_ip_sock),
  466. #ifdef CONFIG_COMPAT
  467. .compat_setsockopt = compat_ip_setsockopt,
  468. .compat_getsockopt = compat_ip_getsockopt,
  469. #endif
  470. };
  471. static const struct proto_ops l2tp_ip_ops = {
  472. .family = PF_INET,
  473. .owner = THIS_MODULE,
  474. .release = inet_release,
  475. .bind = inet_bind,
  476. .connect = inet_dgram_connect,
  477. .socketpair = sock_no_socketpair,
  478. .accept = sock_no_accept,
  479. .getname = l2tp_ip_getname,
  480. .poll = datagram_poll,
  481. .ioctl = inet_ioctl,
  482. .listen = sock_no_listen,
  483. .shutdown = inet_shutdown,
  484. .setsockopt = sock_common_setsockopt,
  485. .getsockopt = sock_common_getsockopt,
  486. .sendmsg = inet_sendmsg,
  487. .recvmsg = sock_common_recvmsg,
  488. .mmap = sock_no_mmap,
  489. .sendpage = sock_no_sendpage,
  490. #ifdef CONFIG_COMPAT
  491. .compat_setsockopt = compat_sock_common_setsockopt,
  492. .compat_getsockopt = compat_sock_common_getsockopt,
  493. #endif
  494. };
  495. static struct inet_protosw l2tp_ip_protosw = {
  496. .type = SOCK_DGRAM,
  497. .protocol = IPPROTO_L2TP,
  498. .prot = &l2tp_ip_prot,
  499. .ops = &l2tp_ip_ops,
  500. .no_check = 0,
  501. };
  502. static struct net_protocol l2tp_ip_protocol __read_mostly = {
  503. .handler = l2tp_ip_recv,
  504. .netns_ok = 1,
  505. };
  506. static int __init l2tp_ip_init(void)
  507. {
  508. int err;
  509. pr_info("L2TP IP encapsulation support (L2TPv3)\n");
  510. err = proto_register(&l2tp_ip_prot, 1);
  511. if (err != 0)
  512. goto out;
  513. err = inet_add_protocol(&l2tp_ip_protocol, IPPROTO_L2TP);
  514. if (err)
  515. goto out1;
  516. inet_register_protosw(&l2tp_ip_protosw);
  517. return 0;
  518. out1:
  519. proto_unregister(&l2tp_ip_prot);
  520. out:
  521. return err;
  522. }
  523. static void __exit l2tp_ip_exit(void)
  524. {
  525. inet_unregister_protosw(&l2tp_ip_protosw);
  526. inet_del_protocol(&l2tp_ip_protocol, IPPROTO_L2TP);
  527. proto_unregister(&l2tp_ip_prot);
  528. }
  529. module_init(l2tp_ip_init);
  530. module_exit(l2tp_ip_exit);
  531. MODULE_LICENSE("GPL");
  532. MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
  533. MODULE_DESCRIPTION("L2TP over IP");
  534. MODULE_VERSION("1.0");
  535. /* Use the value of SOCK_DGRAM (2) directory, because __stringify doesn't like
  536. * enums
  537. */
  538. MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_INET, 2, IPPROTO_L2TP);