blob: 955662a6dee754478da0f8ac95d41a787339242b [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
James Chapman0d767512010-04-02 06:19:00 +00002/*
3 * L2TPv3 IP encapsulation support
4 *
5 * Copyright (c) 2008,2009,2010 Katalix Systems Ltd
James Chapman0d767512010-04-02 06:19:00 +00006 */
7
Joe Perchesa4ca44f2012-05-16 09:55:56 +00008#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
Eric Dumazet72fb96e72017-02-09 16:15:52 -080010#include <asm/ioctls.h>
James Chapman0d767512010-04-02 06:19:00 +000011#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>
James Chapman0d767512010-04-02 06:19:00 +000023#include <net/tcp_states.h>
24#include <net/protocol.h>
25#include <net/xfrm.h>
26
27#include "l2tp_core.h"
28
29struct l2tp_ip_sock {
30 /* inet_sock has to be the first member of l2tp_ip_sock */
31 struct inet_sock inet;
32
James Chapmanc8657fd2012-04-29 21:48:48 +000033 u32 conn_id;
34 u32 peer_conn_id;
James Chapman0d767512010-04-02 06:19:00 +000035};
36
37static DEFINE_RWLOCK(l2tp_ip_lock);
38static struct hlist_head l2tp_ip_table;
39static struct hlist_head l2tp_ip_bind_table;
40
41static inline struct l2tp_ip_sock *l2tp_ip_sk(const struct sock *sk)
42{
43 return (struct l2tp_ip_sock *)sk;
44}
45
Guillaume Naulta9b2dff2016-12-30 19:48:20 +010046static struct sock *__l2tp_ip_bind_lookup(const struct net *net, __be32 laddr,
47 __be32 raddr, int dif, u32 tunnel_id)
James Chapman0d767512010-04-02 06:19:00 +000048{
James Chapman0d767512010-04-02 06:19:00 +000049 struct sock *sk;
50
Sasha Levinb67bfe02013-02-27 17:06:00 -080051 sk_for_each_bound(sk, &l2tp_ip_bind_table) {
Guillaume Naultbb39b0b2017-01-06 20:03:55 +010052 const struct l2tp_ip_sock *l2tp = l2tp_ip_sk(sk);
53 const struct inet_sock *inet = inet_sk(sk);
James Chapman0d767512010-04-02 06:19:00 +000054
Guillaume Naultc5fdae02017-01-06 20:03:57 +010055 if (!net_eq(sock_net(sk), net))
56 continue;
57
58 if (sk->sk_bound_dev_if && dif && sk->sk_bound_dev_if != dif)
59 continue;
60
61 if (inet->inet_rcv_saddr && laddr &&
62 inet->inet_rcv_saddr != laddr)
63 continue;
64
65 if (inet->inet_daddr && raddr && inet->inet_daddr != raddr)
66 continue;
67
68 if (l2tp->conn_id != tunnel_id)
69 continue;
70
71 goto found;
James Chapman0d767512010-04-02 06:19:00 +000072 }
73
74 sk = NULL;
75found:
76 return sk;
77}
78
James Chapman0d767512010-04-02 06:19:00 +000079/* When processing receive frames, there are two cases to
80 * consider. Data frames consist of a non-zero session-id and an
81 * optional cookie. Control frames consist of a regular L2TP header
82 * preceded by 32-bits of zeros.
83 *
84 * L2TPv3 Session 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 * | Session ID |
90 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
91 * | Cookie (optional, maximum 64 bits)...
92 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
93 * |
94 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
95 *
96 * L2TPv3 Control Message Header Over IP
97 *
98 * 0 1 2 3
99 * 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
100 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
101 * | (32 bits of zeros) |
102 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
103 * |T|L|x|x|S|x|x|x|x|x|x|x| Ver | Length |
104 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
105 * | Control Connection ID |
106 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
107 * | Ns | Nr |
108 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
109 *
110 * All control frames are passed to userspace.
111 */
112static int l2tp_ip_recv(struct sk_buff *skb)
113{
David S. Miller9d6ddb12013-02-05 14:36:02 -0500114 struct net *net = dev_net(skb->dev);
James Chapman0d767512010-04-02 06:19:00 +0000115 struct sock *sk;
116 u32 session_id;
117 u32 tunnel_id;
118 unsigned char *ptr, *optr;
119 struct l2tp_session *session;
120 struct l2tp_tunnel *tunnel = NULL;
Guillaume Nault8f7dc9a2017-11-03 16:49:00 +0100121 struct iphdr *iph;
James Chapman0d767512010-04-02 06:19:00 +0000122 int length;
James Chapman0d767512010-04-02 06:19:00 +0000123
James Chapman0d767512010-04-02 06:19:00 +0000124 if (!pskb_may_pull(skb, 4))
125 goto discard;
126
Haishuang Yan5745b8232016-04-03 22:09:23 +0800127 /* Point to L2TP header */
128 optr = ptr = skb->data;
James Chapman0d767512010-04-02 06:19:00 +0000129 session_id = ntohl(*((__be32 *) ptr));
130 ptr += 4;
131
132 /* RFC3931: L2TP/IP packets have the first 4 bytes containing
133 * the session_id. If it is 0, the packet is a L2TP control
134 * frame and the session_id value can be discarded.
135 */
136 if (session_id == 0) {
137 __skb_pull(skb, 4);
138 goto pass_up;
139 }
140
141 /* Ok, this is a data packet. Lookup the session. */
Guillaume Nault01e28b92018-08-10 13:21:57 +0200142 session = l2tp_session_get(net, session_id);
Guillaume Nault61b9a042017-03-31 13:02:25 +0200143 if (!session)
James Chapman0d767512010-04-02 06:19:00 +0000144 goto discard;
145
146 tunnel = session->tunnel;
Guillaume Nault61b9a042017-03-31 13:02:25 +0200147 if (!tunnel)
148 goto discard_sess;
James Chapman0d767512010-04-02 06:19:00 +0000149
150 /* Trace packet contents, if enabled */
151 if (tunnel->debug & L2TP_MSG_DATA) {
152 length = min(32u, skb->len);
153 if (!pskb_may_pull(skb, length))
Guillaume Nault61b9a042017-03-31 13:02:25 +0200154 goto discard_sess;
James Chapman0d767512010-04-02 06:19:00 +0000155
Haishuang Yan5745b8232016-04-03 22:09:23 +0800156 /* Point to L2TP header */
157 optr = ptr = skb->data;
158 ptr += 4;
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000159 pr_debug("%s: ip recv\n", tunnel->name);
160 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, ptr, length);
James Chapman0d767512010-04-02 06:19:00 +0000161 }
162
Jacob Wen4522a702019-01-30 14:55:14 +0800163 if (l2tp_v3_ensure_opt_in_linear(session, skb, &ptr, &optr))
164 goto discard_sess;
165
Guillaume Nault2b139e62018-07-25 14:53:33 +0200166 l2tp_recv_common(session, skb, ptr, optr, 0, skb->len);
Guillaume Nault61b9a042017-03-31 13:02:25 +0200167 l2tp_session_dec_refcount(session);
James Chapman0d767512010-04-02 06:19:00 +0000168
169 return 0;
170
171pass_up:
172 /* Get the tunnel_id from the L2TP header */
173 if (!pskb_may_pull(skb, 12))
174 goto discard;
175
176 if ((skb->data[0] & 0xc0) != 0xc0)
177 goto discard;
178
179 tunnel_id = ntohl(*(__be32 *) &skb->data[4]);
Guillaume Nault8f7dc9a2017-11-03 16:49:00 +0100180 iph = (struct iphdr *)skb_network_header(skb);
James Chapman0d767512010-04-02 06:19:00 +0000181
Guillaume Nault8f7dc9a2017-11-03 16:49:00 +0100182 read_lock_bh(&l2tp_ip_lock);
183 sk = __l2tp_ip_bind_lookup(net, iph->daddr, iph->saddr, inet_iif(skb),
184 tunnel_id);
185 if (!sk) {
James Chapman0d767512010-04-02 06:19:00 +0000186 read_unlock_bh(&l2tp_ip_lock);
Guillaume Nault8f7dc9a2017-11-03 16:49:00 +0100187 goto discard;
James Chapman0d767512010-04-02 06:19:00 +0000188 }
Guillaume Nault8f7dc9a2017-11-03 16:49:00 +0100189 sock_hold(sk);
190 read_unlock_bh(&l2tp_ip_lock);
James Chapman0d767512010-04-02 06:19:00 +0000191
James Chapman0d767512010-04-02 06:19:00 +0000192 if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb))
193 goto discard_put;
194
Florian Westphal895b5c92019-09-29 20:54:03 +0200195 nf_reset_ct(skb);
James Chapman0d767512010-04-02 06:19:00 +0000196
197 return sk_receive_skb(sk, skb, 1);
198
Guillaume Nault61b9a042017-03-31 13:02:25 +0200199discard_sess:
Guillaume Nault61b9a042017-03-31 13:02:25 +0200200 l2tp_session_dec_refcount(session);
201 goto discard;
202
James Chapman0d767512010-04-02 06:19:00 +0000203discard_put:
204 sock_put(sk);
205
206discard:
207 kfree_skb(skb);
208 return 0;
209}
210
Eric Dumazet02c71b12020-05-29 11:20:53 -0700211static int l2tp_ip_hash(struct sock *sk)
212{
213 if (sk_unhashed(sk)) {
214 write_lock_bh(&l2tp_ip_lock);
215 sk_add_node(sk, &l2tp_ip_table);
216 write_unlock_bh(&l2tp_ip_lock);
217 }
218 return 0;
219}
220
221static void l2tp_ip_unhash(struct sock *sk)
222{
223 if (sk_unhashed(sk))
224 return;
225 write_lock_bh(&l2tp_ip_lock);
226 sk_del_node_init(sk);
227 write_unlock_bh(&l2tp_ip_lock);
228}
229
James Chapman0d767512010-04-02 06:19:00 +0000230static int l2tp_ip_open(struct sock *sk)
231{
232 /* Prevent autobind. We don't have ports. */
233 inet_sk(sk)->inet_num = IPPROTO_L2TP;
234
Eric Dumazet02c71b12020-05-29 11:20:53 -0700235 l2tp_ip_hash(sk);
James Chapman0d767512010-04-02 06:19:00 +0000236 return 0;
237}
238
239static void l2tp_ip_close(struct sock *sk, long timeout)
240{
241 write_lock_bh(&l2tp_ip_lock);
242 hlist_del_init(&sk->sk_bind_node);
James Chapmand1f224a2012-04-10 00:10:42 +0000243 sk_del_node_init(sk);
James Chapman0d767512010-04-02 06:19:00 +0000244 write_unlock_bh(&l2tp_ip_lock);
245 sk_common_release(sk);
246}
247
248static void l2tp_ip_destroy_sock(struct sock *sk)
249{
250 struct sk_buff *skb;
James Chapmand00fa9a2018-02-23 17:45:45 +0000251 struct l2tp_tunnel *tunnel = sk->sk_user_data;
James Chapman0d767512010-04-02 06:19:00 +0000252
253 while ((skb = __skb_dequeue_tail(&sk->sk_write_queue)) != NULL)
254 kfree_skb(skb);
255
James Chapmand00fa9a2018-02-23 17:45:45 +0000256 if (tunnel)
257 l2tp_tunnel_delete(tunnel);
James Chapman0d767512010-04-02 06:19:00 +0000258}
259
260static int l2tp_ip_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len)
261{
262 struct inet_sock *inet = inet_sk(sk);
263 struct sockaddr_l2tpip *addr = (struct sockaddr_l2tpip *) uaddr;
David S. Miller9d6ddb12013-02-05 14:36:02 -0500264 struct net *net = sock_net(sk);
James Chapmanc51ce492012-05-29 03:30:42 +0000265 int ret;
James Chapman0d767512010-04-02 06:19:00 +0000266 int chk_addr_ret;
267
James Chapmanc51ce492012-05-29 03:30:42 +0000268 if (addr_len < sizeof(struct sockaddr_l2tpip))
269 return -EINVAL;
270 if (addr->l2tp_family != AF_INET)
271 return -EINVAL;
272
James Chapman0d767512010-04-02 06:19:00 +0000273 lock_sock(sk);
Guillaume Naultd5e3a192016-11-29 13:09:46 +0100274
275 ret = -EINVAL;
Guillaume Nault32c23112016-11-18 22:13:00 +0100276 if (!sock_flag(sk, SOCK_ZAPPED))
277 goto out;
278
Guillaume Nault8cf2f702017-01-06 20:03:54 +0100279 if (sk->sk_state != TCP_CLOSE)
James Chapman0d767512010-04-02 06:19:00 +0000280 goto out;
281
David S. Miller9d6ddb12013-02-05 14:36:02 -0500282 chk_addr_ret = inet_addr_type(net, addr->l2tp_addr.s_addr);
James Chapman0d767512010-04-02 06:19:00 +0000283 ret = -EADDRNOTAVAIL;
284 if (addr->l2tp_addr.s_addr && chk_addr_ret != RTN_LOCAL &&
285 chk_addr_ret != RTN_MULTICAST && chk_addr_ret != RTN_BROADCAST)
286 goto out;
287
James Chapmanc9be48d2012-04-10 00:10:43 +0000288 if (addr->l2tp_addr.s_addr)
289 inet->inet_rcv_saddr = inet->inet_saddr = addr->l2tp_addr.s_addr;
James Chapman0d767512010-04-02 06:19:00 +0000290 if (chk_addr_ret == RTN_MULTICAST || chk_addr_ret == RTN_BROADCAST)
291 inet->inet_saddr = 0; /* Use device */
James Chapman0d767512010-04-02 06:19:00 +0000292
293 write_lock_bh(&l2tp_ip_lock);
Guillaume Naulta9b2dff2016-12-30 19:48:20 +0100294 if (__l2tp_ip_bind_lookup(net, addr->l2tp_addr.s_addr, 0,
Guillaume Naultd5e3a192016-11-29 13:09:46 +0100295 sk->sk_bound_dev_if, addr->l2tp_conn_id)) {
296 write_unlock_bh(&l2tp_ip_lock);
297 ret = -EADDRINUSE;
298 goto out;
299 }
300
301 sk_dst_reset(sk);
302 l2tp_ip_sk(sk)->conn_id = addr->l2tp_conn_id;
303
James Chapman0d767512010-04-02 06:19:00 +0000304 sk_add_bind_node(sk, &l2tp_ip_bind_table);
305 sk_del_node_init(sk);
306 write_unlock_bh(&l2tp_ip_lock);
Guillaume Naultd5e3a192016-11-29 13:09:46 +0100307
James Chapman0d767512010-04-02 06:19:00 +0000308 ret = 0;
James Chapmanc51ce492012-05-29 03:30:42 +0000309 sock_reset_flag(sk, SOCK_ZAPPED);
310
James Chapman0d767512010-04-02 06:19:00 +0000311out:
312 release_sock(sk);
313
314 return ret;
James Chapman0d767512010-04-02 06:19:00 +0000315}
316
317static int l2tp_ip_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
318{
James Chapman0d767512010-04-02 06:19:00 +0000319 struct sockaddr_l2tpip *lsa = (struct sockaddr_l2tpip *) uaddr;
James Chapmande3c7a12012-04-29 21:48:47 +0000320 int rc;
James Chapman0d767512010-04-02 06:19:00 +0000321
James Chapman0d767512010-04-02 06:19:00 +0000322 if (addr_len < sizeof(*lsa))
James Chapmande3c7a12012-04-29 21:48:47 +0000323 return -EINVAL;
James Chapman0d767512010-04-02 06:19:00 +0000324
James Chapmande3c7a12012-04-29 21:48:47 +0000325 if (ipv4_is_multicast(lsa->l2tp_addr.s_addr))
326 return -EINVAL;
327
David S. Miller2f162702011-05-08 13:39:01 -0700328 lock_sock(sk);
329
Guillaume Nault0382a252016-11-29 13:09:44 +0100330 /* Must bind first - autobinding does not work */
331 if (sock_flag(sk, SOCK_ZAPPED)) {
332 rc = -EINVAL;
333 goto out_sk;
334 }
335
336 rc = __ip4_datagram_connect(sk, uaddr, addr_len);
337 if (rc < 0)
338 goto out_sk;
339
James Chapman0d767512010-04-02 06:19:00 +0000340 l2tp_ip_sk(sk)->peer_conn_id = lsa->l2tp_conn_id;
341
James Chapman0d767512010-04-02 06:19:00 +0000342 write_lock_bh(&l2tp_ip_lock);
343 hlist_del_init(&sk->sk_bind_node);
344 sk_add_bind_node(sk, &l2tp_ip_bind_table);
345 write_unlock_bh(&l2tp_ip_lock);
346
Guillaume Nault0382a252016-11-29 13:09:44 +0100347out_sk:
David S. Miller2f162702011-05-08 13:39:01 -0700348 release_sock(sk);
Guillaume Nault0382a252016-11-29 13:09:44 +0100349
James Chapman0d767512010-04-02 06:19:00 +0000350 return rc;
351}
352
James Chapmanc51ce492012-05-29 03:30:42 +0000353static int l2tp_ip_disconnect(struct sock *sk, int flags)
354{
355 if (sock_flag(sk, SOCK_ZAPPED))
356 return 0;
357
Eric Dumazet286c72d2016-10-20 09:39:40 -0700358 return __udp_disconnect(sk, flags);
James Chapmanc51ce492012-05-29 03:30:42 +0000359}
360
James Chapman0d767512010-04-02 06:19:00 +0000361static int l2tp_ip_getname(struct socket *sock, struct sockaddr *uaddr,
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +0100362 int peer)
James Chapman0d767512010-04-02 06:19:00 +0000363{
364 struct sock *sk = sock->sk;
365 struct inet_sock *inet = inet_sk(sk);
366 struct l2tp_ip_sock *lsk = l2tp_ip_sk(sk);
367 struct sockaddr_l2tpip *lsa = (struct sockaddr_l2tpip *)uaddr;
368
369 memset(lsa, 0, sizeof(*lsa));
370 lsa->l2tp_family = AF_INET;
371 if (peer) {
372 if (!inet->inet_dport)
373 return -ENOTCONN;
374 lsa->l2tp_conn_id = lsk->peer_conn_id;
375 lsa->l2tp_addr.s_addr = inet->inet_daddr;
376 } else {
377 __be32 addr = inet->inet_rcv_saddr;
378 if (!addr)
379 addr = inet->inet_saddr;
380 lsa->l2tp_conn_id = lsk->conn_id;
381 lsa->l2tp_addr.s_addr = addr;
382 }
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +0100383 return sizeof(*lsa);
James Chapman0d767512010-04-02 06:19:00 +0000384}
385
386static int l2tp_ip_backlog_recv(struct sock *sk, struct sk_buff *skb)
387{
388 int rc;
389
James Chapman0d767512010-04-02 06:19:00 +0000390 /* Charge it to the socket, dropping if the queue is full. */
391 rc = sock_queue_rcv_skb(sk, skb);
392 if (rc < 0)
393 goto drop;
394
395 return 0;
396
397drop:
David S. Miller9d6ddb12013-02-05 14:36:02 -0500398 IP_INC_STATS(sock_net(sk), IPSTATS_MIB_INDISCARDS);
James Chapman0d767512010-04-02 06:19:00 +0000399 kfree_skb(skb);
Paul Hüber51fb60e2017-02-26 17:58:19 +0100400 return 0;
James Chapman0d767512010-04-02 06:19:00 +0000401}
402
403/* Userspace will call sendmsg() on the tunnel socket to send L2TP
404 * control frames.
405 */
Ying Xue1b784142015-03-02 15:37:48 +0800406static int l2tp_ip_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
James Chapman0d767512010-04-02 06:19:00 +0000407{
408 struct sk_buff *skb;
409 int rc;
James Chapman0d767512010-04-02 06:19:00 +0000410 struct inet_sock *inet = inet_sk(sk);
James Chapman0d767512010-04-02 06:19:00 +0000411 struct rtable *rt = NULL;
David S. Millerfdbb0f02011-05-08 13:48:37 -0700412 struct flowi4 *fl4;
James Chapman0d767512010-04-02 06:19:00 +0000413 int connected = 0;
414 __be32 daddr;
415
David S. Miller2f162702011-05-08 13:39:01 -0700416 lock_sock(sk);
417
418 rc = -ENOTCONN;
James Chapman0d767512010-04-02 06:19:00 +0000419 if (sock_flag(sk, SOCK_DEAD))
David S. Miller2f162702011-05-08 13:39:01 -0700420 goto out;
James Chapman0d767512010-04-02 06:19:00 +0000421
422 /* Get and verify the address. */
423 if (msg->msg_name) {
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100424 DECLARE_SOCKADDR(struct sockaddr_l2tpip *, lip, msg->msg_name);
David S. Miller2f162702011-05-08 13:39:01 -0700425 rc = -EINVAL;
James Chapman0d767512010-04-02 06:19:00 +0000426 if (msg->msg_namelen < sizeof(*lip))
David S. Miller2f162702011-05-08 13:39:01 -0700427 goto out;
James Chapman0d767512010-04-02 06:19:00 +0000428
429 if (lip->l2tp_family != AF_INET) {
David S. Miller2f162702011-05-08 13:39:01 -0700430 rc = -EAFNOSUPPORT;
James Chapman0d767512010-04-02 06:19:00 +0000431 if (lip->l2tp_family != AF_UNSPEC)
David S. Miller2f162702011-05-08 13:39:01 -0700432 goto out;
James Chapman0d767512010-04-02 06:19:00 +0000433 }
434
435 daddr = lip->l2tp_addr.s_addr;
436 } else {
Sasha Levin84768ed2012-05-02 03:58:43 +0000437 rc = -EDESTADDRREQ;
James Chapman0d767512010-04-02 06:19:00 +0000438 if (sk->sk_state != TCP_ESTABLISHED)
Sasha Levin84768ed2012-05-02 03:58:43 +0000439 goto out;
James Chapman0d767512010-04-02 06:19:00 +0000440
441 daddr = inet->inet_daddr;
442 connected = 1;
443 }
444
445 /* Allocate a socket buffer */
446 rc = -ENOMEM;
447 skb = sock_wmalloc(sk, 2 + NET_SKB_PAD + sizeof(struct iphdr) +
448 4 + len, 0, GFP_KERNEL);
449 if (!skb)
450 goto error;
451
452 /* Reserve space for headers, putting IP header on 4-byte boundary. */
453 skb_reserve(skb, 2 + NET_SKB_PAD);
454 skb_reset_network_header(skb);
455 skb_reserve(skb, sizeof(struct iphdr));
456 skb_reset_transport_header(skb);
457
458 /* Insert 0 session_id */
459 *((__be32 *) skb_put(skb, 4)) = 0;
460
461 /* Copy user data into skb */
Al Viro6ce8e9c2014-04-06 21:25:44 -0400462 rc = memcpy_from_msg(skb_put(skb, len), msg, len);
James Chapman0d767512010-04-02 06:19:00 +0000463 if (rc < 0) {
464 kfree_skb(skb);
465 goto error;
466 }
467
David S. Millerfdbb0f02011-05-08 13:48:37 -0700468 fl4 = &inet->cork.fl.u.ip4;
James Chapman0d767512010-04-02 06:19:00 +0000469 if (connected)
470 rt = (struct rtable *) __sk_dst_check(sk, 0);
471
Eric Dumazet081b1b12011-06-11 22:27:09 +0000472 rcu_read_lock();
James Chapman0d767512010-04-02 06:19:00 +0000473 if (rt == NULL) {
Eric Dumazet081b1b12011-06-11 22:27:09 +0000474 const struct ip_options_rcu *inet_opt;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000475
David S. Miller778865a2011-04-28 13:54:06 -0700476 inet_opt = rcu_dereference(inet->inet_opt);
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000477
James Chapman0d767512010-04-02 06:19:00 +0000478 /* Use correct destination address if we have options. */
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000479 if (inet_opt && inet_opt->opt.srr)
480 daddr = inet_opt->opt.faddr;
James Chapman0d767512010-04-02 06:19:00 +0000481
David S. Miller78fbfd82011-03-12 00:00:52 -0500482 /* If this fails, retransmit mechanism of transport layer will
483 * keep trying until route appears or the connection times
484 * itself out.
485 */
David S. Millerfdbb0f02011-05-08 13:48:37 -0700486 rt = ip_route_output_ports(sock_net(sk), fl4, sk,
David S. Miller78fbfd82011-03-12 00:00:52 -0500487 daddr, inet->inet_saddr,
488 inet->inet_dport, inet->inet_sport,
489 sk->sk_protocol, RT_CONN_FLAGS(sk),
490 sk->sk_bound_dev_if);
491 if (IS_ERR(rt))
492 goto no_route;
Eric Dumazet4399a4d2012-06-08 06:25:00 +0000493 if (connected) {
Eric Dumazet081b1b12011-06-11 22:27:09 +0000494 sk_setup_caps(sk, &rt->dst);
Eric Dumazet4399a4d2012-06-08 06:25:00 +0000495 } else {
496 skb_dst_set(skb, &rt->dst);
497 goto xmit;
498 }
James Chapman0d767512010-04-02 06:19:00 +0000499 }
Eric Dumazet081b1b12011-06-11 22:27:09 +0000500
501 /* We dont need to clone dst here, it is guaranteed to not disappear.
502 * __dev_xmit_skb() might force a refcount if needed.
503 */
504 skb_dst_set_noref(skb, &rt->dst);
James Chapman0d767512010-04-02 06:19:00 +0000505
Eric Dumazet4399a4d2012-06-08 06:25:00 +0000506xmit:
James Chapman0d767512010-04-02 06:19:00 +0000507 /* Queue the packet to IP for output */
Eric Dumazetb0270e92014-04-15 12:58:34 -0400508 rc = ip_queue_xmit(sk, skb, &inet->cork.fl);
Eric Dumazet081b1b12011-06-11 22:27:09 +0000509 rcu_read_unlock();
James Chapman0d767512010-04-02 06:19:00 +0000510
511error:
James Chapmanc8657fd2012-04-29 21:48:48 +0000512 if (rc >= 0)
James Chapman0d767512010-04-02 06:19:00 +0000513 rc = len;
James Chapman0d767512010-04-02 06:19:00 +0000514
David S. Miller2f162702011-05-08 13:39:01 -0700515out:
516 release_sock(sk);
James Chapman0d767512010-04-02 06:19:00 +0000517 return rc;
518
519no_route:
Eric Dumazet081b1b12011-06-11 22:27:09 +0000520 rcu_read_unlock();
James Chapman0d767512010-04-02 06:19:00 +0000521 IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTNOROUTES);
522 kfree_skb(skb);
David S. Miller2f162702011-05-08 13:39:01 -0700523 rc = -EHOSTUNREACH;
524 goto out;
James Chapman0d767512010-04-02 06:19:00 +0000525}
526
Ying Xue1b784142015-03-02 15:37:48 +0800527static int l2tp_ip_recvmsg(struct sock *sk, struct msghdr *msg,
James Chapman0d767512010-04-02 06:19:00 +0000528 size_t len, int noblock, int flags, int *addr_len)
529{
530 struct inet_sock *inet = inet_sk(sk);
James Chapman0d767512010-04-02 06:19:00 +0000531 size_t copied = 0;
532 int err = -EOPNOTSUPP;
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100533 DECLARE_SOCKADDR(struct sockaddr_in *, sin, msg->msg_name);
James Chapman0d767512010-04-02 06:19:00 +0000534 struct sk_buff *skb;
535
536 if (flags & MSG_OOB)
537 goto out;
538
James Chapman0d767512010-04-02 06:19:00 +0000539 skb = skb_recv_datagram(sk, flags, noblock, &err);
540 if (!skb)
541 goto out;
542
543 copied = skb->len;
544 if (len < copied) {
545 msg->msg_flags |= MSG_TRUNC;
546 copied = len;
547 }
548
David S. Miller51f3d022014-11-05 16:46:40 -0500549 err = skb_copy_datagram_msg(skb, 0, msg, copied);
James Chapman0d767512010-04-02 06:19:00 +0000550 if (err)
551 goto done;
552
553 sock_recv_timestamp(msg, sk, skb);
554
555 /* Copy the address. */
556 if (sin) {
557 sin->sin_family = AF_INET;
558 sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
559 sin->sin_port = 0;
560 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
Hannes Frederic Sowabceaa902013-11-18 04:20:45 +0100561 *addr_len = sizeof(*sin);
James Chapman0d767512010-04-02 06:19:00 +0000562 }
563 if (inet->cmsg_flags)
564 ip_cmsg_recv(msg, skb);
565 if (flags & MSG_TRUNC)
566 copied = skb->len;
567done:
568 skb_free_datagram(sk, skb);
569out:
James Chapmanc8657fd2012-04-29 21:48:48 +0000570 return err ? err : copied;
James Chapman0d767512010-04-02 06:19:00 +0000571}
572
Eric Dumazet72fb96e72017-02-09 16:15:52 -0800573int l2tp_ioctl(struct sock *sk, int cmd, unsigned long arg)
574{
575 struct sk_buff *skb;
576 int amount;
577
578 switch (cmd) {
579 case SIOCOUTQ:
580 amount = sk_wmem_alloc_get(sk);
581 break;
582 case SIOCINQ:
583 spin_lock_bh(&sk->sk_receive_queue.lock);
584 skb = skb_peek(&sk->sk_receive_queue);
585 amount = skb ? skb->len : 0;
586 spin_unlock_bh(&sk->sk_receive_queue.lock);
587 break;
588
589 default:
590 return -ENOIOCTLCMD;
591 }
592
593 return put_user(amount, (int __user *)arg);
594}
595EXPORT_SYMBOL(l2tp_ioctl);
596
stephen hemmingerfc130842010-10-21 07:50:46 +0000597static struct proto l2tp_ip_prot = {
James Chapman0d767512010-04-02 06:19:00 +0000598 .name = "L2TP/IP",
599 .owner = THIS_MODULE,
600 .init = l2tp_ip_open,
601 .close = l2tp_ip_close,
602 .bind = l2tp_ip_bind,
603 .connect = l2tp_ip_connect,
James Chapmanc51ce492012-05-29 03:30:42 +0000604 .disconnect = l2tp_ip_disconnect,
Eric Dumazet72fb96e72017-02-09 16:15:52 -0800605 .ioctl = l2tp_ioctl,
James Chapman0d767512010-04-02 06:19:00 +0000606 .destroy = l2tp_ip_destroy_sock,
607 .setsockopt = ip_setsockopt,
608 .getsockopt = ip_getsockopt,
609 .sendmsg = l2tp_ip_sendmsg,
610 .recvmsg = l2tp_ip_recvmsg,
611 .backlog_rcv = l2tp_ip_backlog_recv,
Eric Dumazet02c71b12020-05-29 11:20:53 -0700612 .hash = l2tp_ip_hash,
613 .unhash = l2tp_ip_unhash,
James Chapman0d767512010-04-02 06:19:00 +0000614 .obj_size = sizeof(struct l2tp_ip_sock),
615#ifdef CONFIG_COMPAT
616 .compat_setsockopt = compat_ip_setsockopt,
617 .compat_getsockopt = compat_ip_getsockopt,
618#endif
619};
620
621static const struct proto_ops l2tp_ip_ops = {
622 .family = PF_INET,
623 .owner = THIS_MODULE,
624 .release = inet_release,
625 .bind = inet_bind,
626 .connect = inet_dgram_connect,
627 .socketpair = sock_no_socketpair,
628 .accept = sock_no_accept,
629 .getname = l2tp_ip_getname,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700630 .poll = datagram_poll,
James Chapman0d767512010-04-02 06:19:00 +0000631 .ioctl = inet_ioctl,
Arnd Bergmannc7cbdbf2019-04-17 22:51:48 +0200632 .gettstamp = sock_gettstamp,
James Chapman0d767512010-04-02 06:19:00 +0000633 .listen = sock_no_listen,
634 .shutdown = inet_shutdown,
635 .setsockopt = sock_common_setsockopt,
636 .getsockopt = sock_common_getsockopt,
637 .sendmsg = inet_sendmsg,
638 .recvmsg = sock_common_recvmsg,
639 .mmap = sock_no_mmap,
640 .sendpage = sock_no_sendpage,
641#ifdef CONFIG_COMPAT
642 .compat_setsockopt = compat_sock_common_setsockopt,
643 .compat_getsockopt = compat_sock_common_getsockopt,
644#endif
645};
646
647static struct inet_protosw l2tp_ip_protosw = {
648 .type = SOCK_DGRAM,
649 .protocol = IPPROTO_L2TP,
650 .prot = &l2tp_ip_prot,
651 .ops = &l2tp_ip_ops,
James Chapman0d767512010-04-02 06:19:00 +0000652};
653
654static struct net_protocol l2tp_ip_protocol __read_mostly = {
655 .handler = l2tp_ip_recv,
David S. Miller9d6ddb12013-02-05 14:36:02 -0500656 .netns_ok = 1,
James Chapman0d767512010-04-02 06:19:00 +0000657};
658
659static int __init l2tp_ip_init(void)
660{
661 int err;
662
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000663 pr_info("L2TP IP encapsulation support (L2TPv3)\n");
James Chapman0d767512010-04-02 06:19:00 +0000664
665 err = proto_register(&l2tp_ip_prot, 1);
666 if (err != 0)
667 goto out;
668
669 err = inet_add_protocol(&l2tp_ip_protocol, IPPROTO_L2TP);
670 if (err)
671 goto out1;
672
673 inet_register_protosw(&l2tp_ip_protosw);
674 return 0;
675
676out1:
677 proto_unregister(&l2tp_ip_prot);
678out:
679 return err;
680}
681
682static void __exit l2tp_ip_exit(void)
683{
684 inet_unregister_protosw(&l2tp_ip_protosw);
685 inet_del_protocol(&l2tp_ip_protocol, IPPROTO_L2TP);
686 proto_unregister(&l2tp_ip_prot);
687}
688
689module_init(l2tp_ip_init);
690module_exit(l2tp_ip_exit);
691
692MODULE_LICENSE("GPL");
693MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
694MODULE_DESCRIPTION("L2TP over IP");
695MODULE_VERSION("1.0");
Michal Mareke8d34a882010-12-06 02:39:12 +0000696
Lucas De Marchie9c54992011-04-26 23:28:26 -0700697/* Use the value of SOCK_DGRAM (2) directory, because __stringify doesn't like
Michal Mareke8d34a882010-12-06 02:39:12 +0000698 * enums
699 */
700MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_INET, 2, IPPROTO_L2TP);
stephen hemminger163c2e22015-09-23 21:33:35 -0700701MODULE_ALIAS_NET_PF_PROTO(PF_INET, IPPROTO_L2TP);