l2tp_ip.c 17 KB

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