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