12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 #include <linux/icmp.h>
15 #include <linux/module.h>
17 #include <linux/random.h>
18 #include <linux/socket.h>
19 #include <linux/l2tp.h>
62 if ((l2tp->
conn_id == tunnel_id) &&
63 net_eq(sock_net(sk), net) &&
64 !(inet->inet_rcv_saddr && inet->inet_rcv_saddr != laddr) &&
65 !(sk->sk_bound_dev_if && sk->sk_bound_dev_if != dif))
74 static inline struct sock *l2tp_ip_bind_lookup(
struct net *net,
__be32 laddr,
int dif,
u32 tunnel_id)
76 struct sock *sk = __l2tp_ip_bind_lookup(net, laddr, dif, tunnel_id);
121 unsigned char *
ptr, *optr;
127 optr = ptr = skb->
data;
129 if (!pskb_may_pull(skb, 4))
139 if (session_id == 0) {
156 if (!pskb_may_pull(skb, length))
169 if (!pskb_may_pull(skb, 12))
172 if ((skb->
data[0] & 0xc0) != 0xc0)
180 struct iphdr *iph = (
struct iphdr *) skb_network_header(skb);
183 sk = __l2tp_ip_bind_lookup(&
init_net, iph->
daddr, 0, tunnel_id);
207 static int l2tp_ip_open(
struct sock *sk)
213 sk_add_node(sk, &l2tp_ip_table);
219 static void l2tp_ip_close(
struct sock *sk,
long timeout)
222 hlist_del_init(&sk->sk_bind_node);
223 sk_del_node_init(sk);
228 static void l2tp_ip_destroy_sock(
struct sock *sk)
278 sk_add_bind_node(sk, &l2tp_ip_bind_table);
279 sk_del_node_init(sk);
295 static int l2tp_ip_connect(
struct sock *sk,
struct sockaddr *uaddr,
int addr_len)
303 if (addr_len <
sizeof(*lsa))
306 if (ipv4_is_multicast(lsa->
l2tp_addr.s_addr))
318 hlist_del_init(&sk->sk_bind_node);
319 sk_add_bind_node(sk, &l2tp_ip_bind_table);
326 static int l2tp_ip_disconnect(
struct sock *sk,
int flags)
335 int *uaddr_len,
int peer)
337 struct sock *sk = sock->
sk;
342 memset(lsa, 0,
sizeof(*lsa));
348 lsa->
l2tp_addr.s_addr = inet->inet_daddr;
350 __be32 addr = inet->inet_rcv_saddr;
356 *uaddr_len =
sizeof(*lsa);
360 static int l2tp_ip_backlog_recv(
struct sock *sk,
struct sk_buff *skb)
380 static int l2tp_ip_sendmsg(
struct kiocb *
iocb,
struct sock *sk,
struct msghdr *
msg,
size_t len)
415 daddr = inet->inet_daddr;
428 skb_reset_network_header(skb);
429 skb_reserve(skb,
sizeof(
struct iphdr));
430 skb_reset_transport_header(skb);
442 fl4 = &inet->
cork.fl.u.ip4;
453 if (inet_opt && inet_opt->
opt.srr)
454 daddr = inet_opt->
opt.faddr;
460 rt = ip_route_output_ports(sock_net(sk), fl4, sk,
464 sk->sk_bound_dev_if);
470 skb_dst_set(skb, &rt->
dst);
501 static int l2tp_ip_recvmsg(
struct kiocb *iocb,
struct sock *sk,
struct msghdr *msg,
502 size_t len,
int noblock,
int flags,
int *addr_len)
514 *addr_len =
sizeof(*sin);
530 sock_recv_timestamp(msg, sk, skb);
535 sin->
sin_addr.s_addr = ip_hdr(skb)->saddr;
537 memset(&sin->sin_zero, 0,
sizeof(sin->sin_zero));
546 return err ? err : copied;
549 static struct proto l2tp_ip_prot = {
552 .init = l2tp_ip_open,
553 .close = l2tp_ip_close,
554 .bind = l2tp_ip_bind,
555 .connect = l2tp_ip_connect,
556 .disconnect = l2tp_ip_disconnect,
558 .destroy = l2tp_ip_destroy_sock,
561 .sendmsg = l2tp_ip_sendmsg,
562 .recvmsg = l2tp_ip_recvmsg,
563 .backlog_rcv = l2tp_ip_backlog_recv,
573 static const struct proto_ops l2tp_ip_ops = {
581 .getname = l2tp_ip_getname,
601 .prot = &l2tp_ip_prot,
607 .handler = l2tp_ip_recv,
610 static int __init l2tp_ip_init(
void)
614 pr_info(
"L2TP IP encapsulation support (L2TPv3)\n");
633 static void __exit l2tp_ip_exit(
void)