l2tp_ip.c 17 KB

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