| 1 |
// SPDX-License-Identifier: GPL-2.0-or-later |
| 2 |
/* L2TPv3 IP encapsulation support |
| 3 |
* |
| 4 |
* Copyright (c) 2008,2009,2010 Katalix Systems Ltd |
| 5 |
*/ |
| 6 |
|
| 7 |
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 8 |
|
| 9 |
#include <asm/ioctls.h> |
| 10 |
#include <linux/icmp.h> |
| 11 |
#include <linux/module.h> |
| 12 |
#include <linux/skbuff.h> |
| 13 |
#include <linux/random.h> |
| 14 |
#include <linux/socket.h> |
| 15 |
#include <linux/l2tp.h> |
| 16 |
#include <linux/in.h> |
| 17 |
#include <net/sock.h> |
| 18 |
#include <net/ip.h> |
| 19 |
#include <net/icmp.h> |
| 20 |
#include <net/udp.h> |
| 21 |
#include <net/inet_common.h> |
| 22 |
#include <net/tcp_states.h> |
| 23 |
#include <net/protocol.h> |
| 24 |
#include <net/xfrm.h> |
| 25 |
#include <net/net_namespace.h> |
| 26 |
#include <net/netns/generic.h> |
| 27 |
|
| 28 |
#include "l2tp_core.h" |
| 29 |
|
| 30 |
/* per-net private data for this module */ |
| 31 |
static unsigned int l2tp_ip_net_id; |
| 32 |
struct l2tp_ip_net { |
| 33 |
rwlock_t l2tp_ip_lock; |
| 34 |
struct hlist_head l2tp_ip_table; |
| 35 |
struct hlist_head l2tp_ip_bind_table; |
| 36 |
}; |
| 37 |
|
| 38 |
struct l2tp_ip_sock { |
| 39 |
/* inet_sock has to be the first member of l2tp_ip_sock */ |
| 40 |
struct inet_sock inet; |
| 41 |
|
| 42 |
u32 conn_id; |
| 43 |
u32 peer_conn_id; |
| 44 |
}; |
| 45 |
|
| 46 |
static struct l2tp_ip_sock *l2tp_ip_sk(const struct sock *sk) |
| 47 |
{ |
| 48 |
return (struct l2tp_ip_sock *)sk; |
| 49 |
} |
| 50 |
|
| 51 |
static struct l2tp_ip_net *l2tp_ip_pernet(const struct net *net) |
| 52 |
{ |
| 53 |
return net_generic(net, l2tp_ip_net_id); |
| 54 |
} |
| 55 |
|
| 56 |
static struct sock *__l2tp_ip_bind_lookup(const struct net *net, __be32 laddr, |
| 57 |
__be32 raddr, int dif, u32 tunnel_id) |
| 58 |
{ |
| 59 |
struct l2tp_ip_net *pn = l2tp_ip_pernet(net); |
| 60 |
struct sock *sk; |
| 61 |
|
| 62 |
sk_for_each_bound(sk, &pn->l2tp_ip_bind_table) { |
| 63 |
const struct l2tp_ip_sock *l2tp = l2tp_ip_sk(sk); |
| 64 |
const struct inet_sock *inet = inet_sk(sk); |
| 65 |
int bound_dev_if; |
| 66 |
|
| 67 |
if (!net_eq(sock_net(sk), net)) |
| 68 |
continue; |
| 69 |
|
| 70 |
bound_dev_if = READ_ONCE(sk->sk_bound_dev_if); |
| 71 |
if (bound_dev_if && dif && bound_dev_if != dif) |
| 72 |
continue; |
| 73 |
|
| 74 |
if (inet->inet_rcv_saddr && laddr && |
| 75 |
inet->inet_rcv_saddr != laddr) |
| 76 |
continue; |
| 77 |
|
| 78 |
if (inet->inet_daddr && raddr && inet->inet_daddr != raddr) |
| 79 |
continue; |
| 80 |
|
| 81 |
if (l2tp->conn_id != tunnel_id) |
| 82 |
continue; |
| 83 |
|
| 84 |
goto found; |
| 85 |
} |
| 86 |
|
| 87 |
sk = NULL; |
| 88 |
found: |
| 89 |
return sk; |
| 90 |
} |
| 91 |
|
| 92 |
/* When processing receive frames, there are two cases to |
| 93 |
* consider. Data frames consist of a non-zero session-id and an |
| 94 |
* optional cookie. Control frames consist of a regular L2TP header |
| 95 |
* preceded by 32-bits of zeros. |
| 96 |
* |
| 97 |
* L2TPv3 Session Header Over IP |
| 98 |
* |
| 99 |
* 0 1 2 3 |
| 100 |
* 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 |
| 101 |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 102 |
* | Session ID | |
| 103 |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 104 |
* | Cookie (optional, maximum 64 bits)... |
| 105 |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 106 |
* | |
| 107 |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 108 |
* |
| 109 |
* L2TPv3 Control Message Header Over IP |
| 110 |
* |
| 111 |
* 0 1 2 3 |
| 112 |
* 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 |
| 113 |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 114 |
* | (32 bits of zeros) | |
| 115 |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 116 |
* |T|L|x|x|S|x|x|x|x|x|x|x| Ver | Length | |
| 117 |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 118 |
* | Control Connection ID | |
| 119 |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 120 |
* | Ns | Nr | |
| 121 |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 122 |
* |
| 123 |
* All control frames are passed to userspace. |
| 124 |
*/ |
| 125 |
static int l2tp_ip_recv(struct sk_buff *skb) |
| 126 |
{ |
| 127 |
struct net *net = dev_net(skb->dev); |
| 128 |
struct l2tp_ip_net *pn; |
| 129 |
struct sock *sk; |
| 130 |
u32 session_id; |
| 131 |
u32 tunnel_id; |
| 132 |
unsigned char *ptr, *optr; |
| 133 |
struct l2tp_session *session; |
| 134 |
struct l2tp_tunnel *tunnel = NULL; |
| 135 |
struct iphdr *iph; |
| 136 |
|
| 137 |
pn = l2tp_ip_pernet(net); |
| 138 |
|
| 139 |
if (!pskb_may_pull(skb, 4)) |
| 140 |
goto discard; |
| 141 |
|
| 142 |
/* Point to L2TP header */ |
| 143 |
optr = skb->data; |
| 144 |
ptr = skb->data; |
| 145 |
session_id = ntohl(*((__be32 *)ptr)); |
| 146 |
ptr += 4; |
| 147 |
|
| 148 |
/* RFC3931: L2TP/IP packets have the first 4 bytes containing |
| 149 |
* the session_id. If it is 0, the packet is a L2TP control |
| 150 |
* frame and the session_id value can be discarded. |
| 151 |
*/ |
| 152 |
if (session_id == 0) { |
| 153 |
__skb_pull(skb, 4); |
| 154 |
goto pass_up; |
| 155 |
} |
| 156 |
|
| 157 |
/* Ok, this is a data packet. Lookup the session. */ |
| 158 |
session = l2tp_v3_session_get(net, NULL, session_id); |
| 159 |
if (!session) |
| 160 |
goto discard; |
| 161 |
|
| 162 |
tunnel = session->tunnel; |
| 163 |
if (!tunnel) |
| 164 |
goto discard_sess; |
| 165 |
|
| 166 |
if (l2tp_v3_ensure_opt_in_linear(session, skb, &ptr, &optr)) |
| 167 |
goto discard_sess; |
| 168 |
|
| 169 |
l2tp_recv_common(session, skb, ptr, optr, 0, skb->len); |
| 170 |
l2tp_session_put(session); |
| 171 |
|
| 172 |
return 0; |
| 173 |
|
| 174 |
pass_up: |
| 175 |
/* Get the tunnel_id from the L2TP header */ |
| 176 |
if (!pskb_may_pull(skb, 12)) |
| 177 |
goto discard; |
| 178 |
|
| 179 |
if ((skb->data[0] & 0xc0) != 0xc0) |
| 180 |
goto discard; |
| 181 |
|
| 182 |
tunnel_id = ntohl(*(__be32 *)&skb->data[4]); |
| 183 |
iph = (struct iphdr *)skb_network_header(skb); |
| 184 |
|
| 185 |
read_lock_bh(&pn->l2tp_ip_lock); |
| 186 |
sk = __l2tp_ip_bind_lookup(net, iph->daddr, iph->saddr, inet_iif(skb), |
| 187 |
tunnel_id); |
| 188 |
if (!sk) { |
| 189 |
read_unlock_bh(&pn->l2tp_ip_lock); |
| 190 |
goto discard; |
| 191 |
} |
| 192 |
sock_hold(sk); |
| 193 |
read_unlock_bh(&pn->l2tp_ip_lock); |
| 194 |
|
| 195 |
if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb)) |
| 196 |
goto discard_put; |
| 197 |
|
| 198 |
nf_reset_ct(skb); |
| 199 |
|
| 200 |
return sk_receive_skb(sk, skb, 1); |
| 201 |
|
| 202 |
discard_sess: |
| 203 |
l2tp_session_put(session); |
| 204 |
goto discard; |
| 205 |
|
| 206 |
discard_put: |
| 207 |
sock_put(sk); |
| 208 |
|
| 209 |
discard: |
| 210 |
kfree_skb(skb); |
| 211 |
return 0; |
| 212 |
} |
| 213 |
|
| 214 |
static int l2tp_ip_hash(struct sock *sk) |
| 215 |
{ |
| 216 |
struct l2tp_ip_net *pn = l2tp_ip_pernet(sock_net(sk)); |
| 217 |
|
| 218 |
if (sk_unhashed(sk)) { |
| 219 |
write_lock_bh(&pn->l2tp_ip_lock); |
| 220 |
sk_add_node(sk, &pn->l2tp_ip_table); |
| 221 |
write_unlock_bh(&pn->l2tp_ip_lock); |
| 222 |
} |
| 223 |
return 0; |
| 224 |
} |
| 225 |
|
| 226 |
static void l2tp_ip_unhash(struct sock *sk) |
| 227 |
{ |
| 228 |
struct l2tp_ip_net *pn = l2tp_ip_pernet(sock_net(sk)); |
| 229 |
|
| 230 |
if (sk_unhashed(sk)) |
| 231 |
return; |
| 232 |
write_lock_bh(&pn->l2tp_ip_lock); |
| 233 |
sk_del_node_init(sk); |
| 234 |
write_unlock_bh(&pn->l2tp_ip_lock); |
| 235 |
} |
| 236 |
|
| 237 |
static int l2tp_ip_open(struct sock *sk) |
| 238 |
{ |
| 239 |
/* Prevent autobind. We don't have ports. */ |
| 240 |
inet_sk(sk)->inet_num = IPPROTO_L2TP; |
| 241 |
|
| 242 |
l2tp_ip_hash(sk); |
| 243 |
return 0; |
| 244 |
} |
| 245 |
|
| 246 |
static void l2tp_ip_close(struct sock *sk, long timeout) |
| 247 |
{ |
| 248 |
struct l2tp_ip_net *pn = l2tp_ip_pernet(sock_net(sk)); |
| 249 |
|
| 250 |
write_lock_bh(&pn->l2tp_ip_lock); |
| 251 |
hlist_del_init(&sk->sk_bind_node); |
| 252 |
sk_del_node_init(sk); |
| 253 |
write_unlock_bh(&pn->l2tp_ip_lock); |
| 254 |
sk_common_release(sk); |
| 255 |
} |
| 256 |
|
| 257 |
static void l2tp_ip_destroy_sock(struct sock *sk) |
| 258 |
{ |
| 259 |
struct l2tp_tunnel *tunnel; |
| 260 |
|
| 261 |
__skb_queue_purge(&sk->sk_write_queue); |
| 262 |
|
| 263 |
tunnel = l2tp_sk_to_tunnel(sk); |
| 264 |
if (tunnel) { |
| 265 |
l2tp_tunnel_delete(tunnel); |
| 266 |
l2tp_tunnel_put(tunnel); |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
static int l2tp_ip_bind(struct sock *sk, struct sockaddr_unsized *uaddr, |
| 271 |
int addr_len) |
| 272 |
{ |
| 273 |
struct inet_sock *inet = inet_sk(sk); |
| 274 |
struct sockaddr_l2tpip *addr = (struct sockaddr_l2tpip *)uaddr; |
| 275 |
struct net *net = sock_net(sk); |
| 276 |
struct l2tp_ip_net *pn; |
| 277 |
int ret; |
| 278 |
int chk_addr_ret; |
| 279 |
|
| 280 |
if (addr_len < sizeof(struct sockaddr_l2tpip)) |
| 281 |
return -EINVAL; |
| 282 |
if (addr->l2tp_family != AF_INET) |
| 283 |
return -EINVAL; |
| 284 |
|
| 285 |
lock_sock(sk); |
| 286 |
|
| 287 |
ret = -EINVAL; |
| 288 |
if (!sock_flag(sk, SOCK_ZAPPED)) |
| 289 |
goto out; |
| 290 |
|
| 291 |
if (sk->sk_state != TCP_CLOSE) |
| 292 |
goto out; |
| 293 |
|
| 294 |
chk_addr_ret = inet_addr_type(net, addr->l2tp_addr.s_addr); |
| 295 |
ret = -EADDRNOTAVAIL; |
| 296 |
if (addr->l2tp_addr.s_addr && chk_addr_ret != RTN_LOCAL && |
| 297 |
chk_addr_ret != RTN_MULTICAST && chk_addr_ret != RTN_BROADCAST) |
| 298 |
goto out; |
| 299 |
|
| 300 |
if (addr->l2tp_addr.s_addr) { |
| 301 |
inet->inet_rcv_saddr = addr->l2tp_addr.s_addr; |
| 302 |
inet->inet_saddr = addr->l2tp_addr.s_addr; |
| 303 |
} |
| 304 |
if (chk_addr_ret == RTN_MULTICAST || chk_addr_ret == RTN_BROADCAST) |
| 305 |
inet->inet_saddr = 0; /* Use device */ |
| 306 |
|
| 307 |
pn = l2tp_ip_pernet(net); |
| 308 |
write_lock_bh(&pn->l2tp_ip_lock); |
| 309 |
if (__l2tp_ip_bind_lookup(net, addr->l2tp_addr.s_addr, 0, |
| 310 |
sk->sk_bound_dev_if, addr->l2tp_conn_id)) { |
| 311 |
write_unlock_bh(&pn->l2tp_ip_lock); |
| 312 |
ret = -EADDRINUSE; |
| 313 |
goto out; |
| 314 |
} |
| 315 |
|
| 316 |
sk_dst_reset(sk); |
| 317 |
l2tp_ip_sk(sk)->conn_id = addr->l2tp_conn_id; |
| 318 |
|
| 319 |
sk_add_bind_node(sk, &pn->l2tp_ip_bind_table); |
| 320 |
sk_del_node_init(sk); |
| 321 |
write_unlock_bh(&pn->l2tp_ip_lock); |
| 322 |
|
| 323 |
ret = 0; |
| 324 |
sock_reset_flag(sk, SOCK_ZAPPED); |
| 325 |
|
| 326 |
out: |
| 327 |
release_sock(sk); |
| 328 |
|
| 329 |
return ret; |
| 330 |
} |
| 331 |
|
| 332 |
static int l2tp_ip_connect(struct sock *sk, struct sockaddr_unsized *uaddr, |
| 333 |
int addr_len) |
| 334 |
{ |
| 335 |
struct sockaddr_l2tpip *lsa = (struct sockaddr_l2tpip *)uaddr; |
| 336 |
struct l2tp_ip_net *pn = l2tp_ip_pernet(sock_net(sk)); |
| 337 |
int rc; |
| 338 |
|
| 339 |
if (addr_len < sizeof(*lsa)) |
| 340 |
return -EINVAL; |
| 341 |
|
| 342 |
if (ipv4_is_multicast(lsa->l2tp_addr.s_addr)) |
| 343 |
return -EINVAL; |
| 344 |
|
| 345 |
lock_sock(sk); |
| 346 |
|
| 347 |
/* Must bind first - autobinding does not work */ |
| 348 |
if (sock_flag(sk, SOCK_ZAPPED)) { |
| 349 |
rc = -EINVAL; |
| 350 |
goto out_sk; |
| 351 |
} |
| 352 |
|
| 353 |
rc = __ip4_datagram_connect(sk, uaddr, addr_len); |
| 354 |
if (rc < 0) |
| 355 |
goto out_sk; |
| 356 |
|
| 357 |
l2tp_ip_sk(sk)->peer_conn_id = lsa->l2tp_conn_id; |
| 358 |
|
| 359 |
write_lock_bh(&pn->l2tp_ip_lock); |
| 360 |
hlist_del_init(&sk->sk_bind_node); |
| 361 |
sk_add_bind_node(sk, &pn->l2tp_ip_bind_table); |
| 362 |
write_unlock_bh(&pn->l2tp_ip_lock); |
| 363 |
|
| 364 |
out_sk: |
| 365 |
release_sock(sk); |
| 366 |
|
| 367 |
return rc; |
| 368 |
} |
| 369 |
|
| 370 |
static int l2tp_ip_disconnect(struct sock *sk, int flags) |
| 371 |
{ |
| 372 |
if (sock_flag(sk, SOCK_ZAPPED)) |
| 373 |
return 0; |
| 374 |
|
| 375 |
return __udp_disconnect(sk, flags); |
| 376 |
} |
| 377 |
|
| 378 |
static int l2tp_ip_getname(struct socket *sock, struct sockaddr *uaddr, |
| 379 |
int peer) |
| 380 |
{ |
| 381 |
struct sock *sk = sock->sk; |
| 382 |
struct inet_sock *inet = inet_sk(sk); |
| 383 |
struct l2tp_ip_sock *lsk = l2tp_ip_sk(sk); |
| 384 |
struct sockaddr_l2tpip *lsa = (struct sockaddr_l2tpip *)uaddr; |
| 385 |
|
| 386 |
memset(lsa, 0, sizeof(*lsa)); |
| 387 |
lsa->l2tp_family = AF_INET; |
| 388 |
if (peer) { |
| 389 |
if (!inet->inet_dport) |
| 390 |
return -ENOTCONN; |
| 391 |
lsa->l2tp_conn_id = lsk->peer_conn_id; |
| 392 |
lsa->l2tp_addr.s_addr = inet->inet_daddr; |
| 393 |
} else { |
| 394 |
__be32 addr = inet->inet_rcv_saddr; |
| 395 |
|
| 396 |
if (!addr) |
| 397 |
addr = inet->inet_saddr; |
| 398 |
lsa->l2tp_conn_id = lsk->conn_id; |
| 399 |
lsa->l2tp_addr.s_addr = addr; |
| 400 |
} |
| 401 |
return sizeof(*lsa); |
| 402 |
} |
| 403 |
|
| 404 |
static int l2tp_ip_backlog_recv(struct sock *sk, struct sk_buff *skb) |
| 405 |
{ |
| 406 |
int rc; |
| 407 |
|
| 408 |
/* Charge it to the socket, dropping if the queue is full. */ |
| 409 |
rc = sock_queue_rcv_skb(sk, skb); |
| 410 |
if (rc < 0) |
| 411 |
goto drop; |
| 412 |
|
| 413 |
return 0; |
| 414 |
|
| 415 |
drop: |
| 416 |
IP_INC_STATS(sock_net(sk), IPSTATS_MIB_INDISCARDS); |
| 417 |
kfree_skb(skb); |
| 418 |
return 0; |
| 419 |
} |
| 420 |
|
| 421 |
/* Userspace will call sendmsg() on the tunnel socket to send L2TP |
| 422 |
* control frames. |
| 423 |
*/ |
| 424 |
static int l2tp_ip_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) |
| 425 |
{ |
| 426 |
struct sk_buff *skb; |
| 427 |
int rc; |
| 428 |
struct inet_sock *inet = inet_sk(sk); |
| 429 |
struct rtable *rt = NULL; |
| 430 |
int connected = 0; |
| 431 |
__be32 daddr; |
| 432 |
|
| 433 |
lock_sock(sk); |
| 434 |
|
| 435 |
rc = -ENOTCONN; |
| 436 |
if (sock_flag(sk, SOCK_DEAD)) |
| 437 |
goto out; |
| 438 |
|
| 439 |
/* Get and verify the address. */ |
| 440 |
if (msg->msg_name) { |
| 441 |
DECLARE_SOCKADDR(struct sockaddr_l2tpip *, lip, msg->msg_name); |
| 442 |
|
| 443 |
rc = -EINVAL; |
| 444 |
if (msg->msg_namelen < sizeof(*lip)) |
| 445 |
goto out; |
| 446 |
|
| 447 |
if (lip->l2tp_family != AF_INET) { |
| 448 |
rc = -EAFNOSUPPORT; |
| 449 |
if (lip->l2tp_family != AF_UNSPEC) |
| 450 |
goto out; |
| 451 |
} |
| 452 |
|
| 453 |
daddr = lip->l2tp_addr.s_addr; |
| 454 |
} else { |
| 455 |
rc = -EDESTADDRREQ; |
| 456 |
if (sk->sk_state != TCP_ESTABLISHED) |
| 457 |
goto out; |
| 458 |
|
| 459 |
connected = 1; |
| 460 |
} |
| 461 |
|
| 462 |
/* Allocate a socket buffer */ |
| 463 |
rc = -ENOMEM; |
| 464 |
skb = sock_wmalloc(sk, 2 + NET_SKB_PAD + sizeof(struct iphdr) + |
| 465 |
4 + len, 0, GFP_KERNEL); |
| 466 |
if (!skb) |
| 467 |
goto error; |
| 468 |
|
| 469 |
/* Reserve space for headers, putting IP header on 4-byte boundary. */ |
| 470 |
skb_reserve(skb, 2 + NET_SKB_PAD); |
| 471 |
skb_reset_network_header(skb); |
| 472 |
skb_reserve(skb, sizeof(struct iphdr)); |
| 473 |
skb_reset_transport_header(skb); |
| 474 |
|
| 475 |
/* Insert 0 session_id */ |
| 476 |
*((__be32 *)skb_put(skb, 4)) = 0; |
| 477 |
|
| 478 |
/* Copy user data into skb */ |
| 479 |
rc = memcpy_from_msg(skb_put(skb, len), msg, len); |
| 480 |
if (rc < 0) { |
| 481 |
kfree_skb(skb); |
| 482 |
goto error; |
| 483 |
} |
| 484 |
|
| 485 |
if (connected) |
| 486 |
rt = dst_rtable(__sk_dst_check(sk, 0)); |
| 487 |
|
| 488 |
rcu_read_lock(); |
| 489 |
if (!rt) { |
| 490 |
struct flowi4 *fl4 = &inet->cork.fl.u.ip4; |
| 491 |
|
| 492 |
inet_sk_init_flowi4(inet, fl4); |
| 493 |
|
| 494 |
/* Overwrite ->daddr if msg->msg_name was provided */ |
| 495 |
if (!connected) |
| 496 |
fl4->daddr = daddr; |
| 497 |
|
| 498 |
/* If this fails, retransmit mechanism of transport layer will |
| 499 |
* keep trying until route appears or the connection times |
| 500 |
* itself out. |
| 501 |
*/ |
| 502 |
rt = ip_route_output_flow(sock_net(sk), fl4, sk); |
| 503 |
if (IS_ERR(rt)) |
| 504 |
goto no_route; |
| 505 |
if (connected) { |
| 506 |
sk_setup_caps(sk, &rt->dst); |
| 507 |
} else { |
| 508 |
skb_dst_set(skb, &rt->dst); |
| 509 |
goto xmit; |
| 510 |
} |
| 511 |
} |
| 512 |
|
| 513 |
/* We don't need to clone dst here, it is guaranteed to not disappear. |
| 514 |
* __dev_xmit_skb() might force a refcount if needed. |
| 515 |
*/ |
| 516 |
skb_dst_set_noref(skb, &rt->dst); |
| 517 |
|
| 518 |
xmit: |
| 519 |
/* Queue the packet to IP for output */ |
| 520 |
rc = ip_queue_xmit(sk, skb, &inet->cork.fl); |
| 521 |
rcu_read_unlock(); |
| 522 |
|
| 523 |
error: |
| 524 |
if (rc >= 0) |
| 525 |
rc = len; |
| 526 |
|
| 527 |
out: |
| 528 |
release_sock(sk); |
| 529 |
return rc; |
| 530 |
|
| 531 |
no_route: |
| 532 |
rcu_read_unlock(); |
| 533 |
IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTNOROUTES); |
| 534 |
kfree_skb(skb); |
| 535 |
rc = -EHOSTUNREACH; |
| 536 |
goto out; |
| 537 |
} |
| 538 |
|
| 539 |
static int l2tp_ip_recvmsg(struct sock *sk, struct msghdr *msg, |
| 540 |
size_t len, int flags, int *addr_len) |
| 541 |
{ |
| 542 |
struct inet_sock *inet = inet_sk(sk); |
| 543 |
size_t copied = 0; |
| 544 |
int err = -EOPNOTSUPP; |
| 545 |
DECLARE_SOCKADDR(struct sockaddr_in *, sin, msg->msg_name); |
| 546 |
struct sk_buff *skb; |
| 547 |
|
| 548 |
if (flags & MSG_OOB) |
| 549 |
goto out; |
| 550 |
|
| 551 |
skb = skb_recv_datagram(sk, flags, &err); |
| 552 |
if (!skb) |
| 553 |
goto out; |
| 554 |
|
| 555 |
copied = skb->len; |
| 556 |
if (len < copied) { |
| 557 |
msg->msg_flags |= MSG_TRUNC; |
| 558 |
copied = len; |
| 559 |
} |
| 560 |
|
| 561 |
err = skb_copy_datagram_msg(skb, 0, msg, copied); |
| 562 |
if (err) |
| 563 |
goto done; |
| 564 |
|
| 565 |
sock_recv_timestamp(msg, sk, skb); |
| 566 |
|
| 567 |
/* Copy the address. */ |
| 568 |
if (sin) { |
| 569 |
sin->sin_family = AF_INET; |
| 570 |
sin->sin_addr.s_addr = ip_hdr(skb)->saddr; |
| 571 |
sin->sin_port = 0; |
| 572 |
memset(&sin->sin_zero, 0, sizeof(sin->sin_zero)); |
| 573 |
*addr_len = sizeof(*sin); |
| 574 |
} |
| 575 |
if (inet_cmsg_flags(inet)) |
| 576 |
ip_cmsg_recv(msg, skb); |
| 577 |
if (flags & MSG_TRUNC) |
| 578 |
copied = skb->len; |
| 579 |
done: |
| 580 |
skb_free_datagram(sk, skb); |
| 581 |
out: |
| 582 |
return err ? err : copied; |
| 583 |
} |
| 584 |
|
| 585 |
int l2tp_ioctl(struct sock *sk, int cmd, int *karg) |
| 586 |
{ |
| 587 |
struct sk_buff *skb; |
| 588 |
|
| 589 |
switch (cmd) { |
| 590 |
case SIOCOUTQ: |
| 591 |
*karg = sk_wmem_alloc_get(sk); |
| 592 |
break; |
| 593 |
case SIOCINQ: |
| 594 |
spin_lock_bh(&sk->sk_receive_queue.lock); |
| 595 |
skb = skb_peek(&sk->sk_receive_queue); |
| 596 |
*karg = skb ? skb->len : 0; |
| 597 |
spin_unlock_bh(&sk->sk_receive_queue.lock); |
| 598 |
break; |
| 599 |
|
| 600 |
default: |
| 601 |
return -ENOIOCTLCMD; |
| 602 |
} |
| 603 |
|
| 604 |
return 0; |
| 605 |
} |
| 606 |
EXPORT_SYMBOL_GPL(l2tp_ioctl); |
| 607 |
|
| 608 |
static struct proto l2tp_ip_prot = { |
| 609 |
.name = "L2TP/IP", |
| 610 |
.owner = THIS_MODULE, |
| 611 |
.init = l2tp_ip_open, |
| 612 |
.close = l2tp_ip_close, |
| 613 |
.bind = l2tp_ip_bind, |
| 614 |
.connect = l2tp_ip_connect, |
| 615 |
.disconnect = l2tp_ip_disconnect, |
| 616 |
.ioctl = l2tp_ioctl, |
| 617 |
.destroy = l2tp_ip_destroy_sock, |
| 618 |
.setsockopt = ip_setsockopt, |
| 619 |
.getsockopt = ip_getsockopt, |
| 620 |
.sendmsg = l2tp_ip_sendmsg, |
| 621 |
.recvmsg = l2tp_ip_recvmsg, |
| 622 |
.backlog_rcv = l2tp_ip_backlog_recv, |
| 623 |
.hash = l2tp_ip_hash, |
| 624 |
.unhash = l2tp_ip_unhash, |
| 625 |
.obj_size = sizeof(struct l2tp_ip_sock), |
| 626 |
}; |
| 627 |
|
| 628 |
static const struct proto_ops l2tp_ip_ops = { |
| 629 |
.family = PF_INET, |
| 630 |
.owner = THIS_MODULE, |
| 631 |
.release = inet_release, |
| 632 |
.bind = inet_bind, |
| 633 |
.connect = inet_dgram_connect, |
| 634 |
.socketpair = sock_no_socketpair, |
| 635 |
.accept = sock_no_accept, |
| 636 |
.getname = l2tp_ip_getname, |
| 637 |
.poll = datagram_poll, |
| 638 |
.ioctl = inet_ioctl, |
| 639 |
.gettstamp = sock_gettstamp, |
| 640 |
.listen = sock_no_listen, |
| 641 |
.shutdown = inet_shutdown, |
| 642 |
.setsockopt = sock_common_setsockopt, |
| 643 |
.getsockopt = sock_common_getsockopt, |
| 644 |
.sendmsg = inet_sendmsg, |
| 645 |
.recvmsg = sock_common_recvmsg, |
| 646 |
.mmap = sock_no_mmap, |
| 647 |
}; |
| 648 |
|
| 649 |
static struct inet_protosw l2tp_ip_protosw = { |
| 650 |
.type = SOCK_DGRAM, |
| 651 |
.protocol = IPPROTO_L2TP, |
| 652 |
.prot = &l2tp_ip_prot, |
| 653 |
.ops = &l2tp_ip_ops, |
| 654 |
}; |
| 655 |
|
| 656 |
static struct net_protocol l2tp_ip_protocol __read_mostly = { |
| 657 |
.handler = l2tp_ip_recv, |
| 658 |
}; |
| 659 |
|
| 660 |
static __net_init int l2tp_ip_init_net(struct net *net) |
| 661 |
{ |
| 662 |
struct l2tp_ip_net *pn = net_generic(net, l2tp_ip_net_id); |
| 663 |
|
| 664 |
rwlock_init(&pn->l2tp_ip_lock); |
| 665 |
INIT_HLIST_HEAD(&pn->l2tp_ip_table); |
| 666 |
INIT_HLIST_HEAD(&pn->l2tp_ip_bind_table); |
| 667 |
return 0; |
| 668 |
} |
| 669 |
|
| 670 |
static __net_exit void l2tp_ip_exit_net(struct net *net) |
| 671 |
{ |
| 672 |
struct l2tp_ip_net *pn = l2tp_ip_pernet(net); |
| 673 |
|
| 674 |
write_lock_bh(&pn->l2tp_ip_lock); |
| 675 |
WARN_ON_ONCE(hlist_count_nodes(&pn->l2tp_ip_table) != 0); |
| 676 |
WARN_ON_ONCE(hlist_count_nodes(&pn->l2tp_ip_bind_table) != 0); |
| 677 |
write_unlock_bh(&pn->l2tp_ip_lock); |
| 678 |
} |
| 679 |
|
| 680 |
static struct pernet_operations l2tp_ip_net_ops = { |
| 681 |
.init = l2tp_ip_init_net, |
| 682 |
.exit = l2tp_ip_exit_net, |
| 683 |
.id = &l2tp_ip_net_id, |
| 684 |
.size = sizeof(struct l2tp_ip_net), |
| 685 |
}; |
| 686 |
|
| 687 |
static int __init l2tp_ip_init(void) |
| 688 |
{ |
| 689 |
int err; |
| 690 |
|
| 691 |
pr_info("L2TP IP encapsulation support (L2TPv3)\n"); |
| 692 |
|
| 693 |
err = register_pernet_device(&l2tp_ip_net_ops); |
| 694 |
if (err) |
| 695 |
goto out; |
| 696 |
|
| 697 |
err = proto_register(&l2tp_ip_prot, 1); |
| 698 |
if (err != 0) |
| 699 |
goto out1; |
| 700 |
|
| 701 |
err = inet_add_protocol(&l2tp_ip_protocol, IPPROTO_L2TP); |
| 702 |
if (err) |
| 703 |
goto out2; |
| 704 |
|
| 705 |
inet_register_protosw(&l2tp_ip_protosw); |
| 706 |
return 0; |
| 707 |
|
| 708 |
out2: |
| 709 |
proto_unregister(&l2tp_ip_prot); |
| 710 |
out1: |
| 711 |
unregister_pernet_device(&l2tp_ip_net_ops); |
| 712 |
out: |
| 713 |
return err; |
| 714 |
} |
| 715 |
|
| 716 |
static void __exit l2tp_ip_exit(void) |
| 717 |
{ |
| 718 |
inet_unregister_protosw(&l2tp_ip_protosw); |
| 719 |
inet_del_protocol(&l2tp_ip_protocol, IPPROTO_L2TP); |
| 720 |
proto_unregister(&l2tp_ip_prot); |
| 721 |
unregister_pernet_device(&l2tp_ip_net_ops); |
| 722 |
} |
| 723 |
|
| 724 |
module_init(l2tp_ip_init); |
| 725 |
module_exit(l2tp_ip_exit); |
| 726 |
|
| 727 |
MODULE_LICENSE("GPL"); |
| 728 |
MODULE_AUTHOR("James Chapman <[email protected]>"); |
| 729 |
MODULE_DESCRIPTION("L2TP over IP"); |
| 730 |
MODULE_VERSION("1.0"); |
| 731 |
|
| 732 |
/* Use the values of SOCK_DGRAM (2) as type and IPPROTO_L2TP (115) as protocol, |
| 733 |
* because __stringify doesn't like enums |
| 734 |
*/ |
| 735 |
MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_INET, 115, 2); |
| 736 |
MODULE_ALIAS_NET_PF_PROTO(PF_INET, 115); |