2019-05-27 09:55:01 +03:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2005-04-17 02:20:36 +04:00
|
|
|
/*
|
|
|
|
* common UDP/RAW code
|
|
|
|
* Linux INET implementation
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Hideaki YOSHIFUJI <yoshfuji@linux-ipv6.org>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/types.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/ip.h>
|
|
|
|
#include <linux/in.h>
|
2005-08-16 09:18:02 +04:00
|
|
|
#include <net/ip.h>
|
2005-04-17 02:20:36 +04:00
|
|
|
#include <net/sock.h>
|
|
|
|
#include <net/route.h>
|
2005-08-10 07:08:28 +04:00
|
|
|
#include <net/tcp_states.h>
|
2019-09-13 04:16:39 +03:00
|
|
|
#include <net/sock_reuseport.h>
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2015-07-14 09:10:22 +03:00
|
|
|
int __ip4_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
|
|
|
struct inet_sock *inet = inet_sk(sk);
|
|
|
|
struct sockaddr_in *usin = (struct sockaddr_in *) uaddr;
|
2011-05-07 09:27:25 +04:00
|
|
|
struct flowi4 *fl4;
|
2005-04-17 02:20:36 +04:00
|
|
|
struct rtable *rt;
|
2006-09-27 08:27:15 +04:00
|
|
|
__be32 saddr;
|
2005-04-17 02:20:36 +04:00
|
|
|
int oif;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
|
2007-02-09 17:24:47 +03:00
|
|
|
if (addr_len < sizeof(*usin))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (usin->sin_family != AF_INET)
|
|
|
|
return -EAFNOSUPPORT;
|
2005-04-17 02:20:36 +04:00
|
|
|
|
|
|
|
sk_dst_reset(sk);
|
|
|
|
|
|
|
|
oif = sk->sk_bound_dev_if;
|
2009-10-15 10:30:45 +04:00
|
|
|
saddr = inet->inet_saddr;
|
2007-12-17 00:45:43 +03:00
|
|
|
if (ipv4_is_multicast(usin->sin_addr.s_addr)) {
|
2018-10-01 11:40:23 +03:00
|
|
|
if (!oif || netif_index_is_l3_master(sock_net(sk), oif))
|
2005-04-17 02:20:36 +04:00
|
|
|
oif = inet->mc_index;
|
|
|
|
if (!saddr)
|
|
|
|
saddr = inet->mc_addr;
|
|
|
|
}
|
2011-05-07 09:27:25 +04:00
|
|
|
fl4 = &inet->cork.fl.u.ip4;
|
|
|
|
rt = ip_route_connect(fl4, usin->sin_addr.s_addr, saddr,
|
2011-03-03 01:31:35 +03:00
|
|
|
RT_CONN_FLAGS(sk), oif,
|
|
|
|
sk->sk_protocol,
|
2013-08-28 10:04:14 +04:00
|
|
|
inet->inet_sport, usin->sin_port, sk);
|
2011-03-03 01:31:35 +03:00
|
|
|
if (IS_ERR(rt)) {
|
|
|
|
err = PTR_ERR(rt);
|
2007-06-01 09:49:28 +04:00
|
|
|
if (err == -ENETUNREACH)
|
2013-11-15 01:37:54 +04:00
|
|
|
IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTNOROUTES);
|
2011-05-07 09:27:25 +04:00
|
|
|
goto out;
|
2007-06-01 09:49:28 +04:00
|
|
|
}
|
|
|
|
|
2005-04-17 02:20:36 +04:00
|
|
|
if ((rt->rt_flags & RTCF_BROADCAST) && !sock_flag(sk, SOCK_BROADCAST)) {
|
|
|
|
ip_rt_put(rt);
|
2011-05-07 09:27:25 +04:00
|
|
|
err = -EACCES;
|
|
|
|
goto out;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
2009-10-15 10:30:45 +04:00
|
|
|
if (!inet->inet_saddr)
|
2011-05-07 09:27:25 +04:00
|
|
|
inet->inet_saddr = fl4->saddr; /* Update source address */
|
udp: add rehash on connect()
commit 30fff923 introduced in linux-2.6.33 (udp: bind() optimisation)
added a secondary hash on UDP, hashed on (local addr, local port).
Problem is that following sequence :
fd = socket(...)
connect(fd, &remote, ...)
not only selects remote end point (address and port), but also sets
local address, while UDP stack stored in secondary hash table the socket
while its local address was INADDR_ANY (or ipv6 equivalent)
Sequence is :
- autobind() : choose a random local port, insert socket in hash tables
[while local address is INADDR_ANY]
- connect() : set remote address and port, change local address to IP
given by a route lookup.
When an incoming UDP frame comes, if more than 10 sockets are found in
primary hash table, we switch to secondary table, and fail to find
socket because its local address changed.
One solution to this problem is to rehash datagram socket if needed.
We add a new rehash(struct socket *) method in "struct proto", and
implement this method for UDP v4 & v6, using a common helper.
This rehashing only takes care of secondary hash table, since primary
hash (based on local port only) is not changed.
Reported-by: Krzysztof Piotr Oledzki <ole@ans.pl>
Signed-off-by: Eric Dumazet <eric.dumazet@gmail.com>
Tested-by: Krzysztof Piotr Oledzki <ole@ans.pl>
Signed-off-by: David S. Miller <davem@davemloft.net>
2010-09-08 09:08:44 +04:00
|
|
|
if (!inet->inet_rcv_saddr) {
|
2011-05-07 09:27:25 +04:00
|
|
|
inet->inet_rcv_saddr = fl4->saddr;
|
udp: add rehash on connect()
commit 30fff923 introduced in linux-2.6.33 (udp: bind() optimisation)
added a secondary hash on UDP, hashed on (local addr, local port).
Problem is that following sequence :
fd = socket(...)
connect(fd, &remote, ...)
not only selects remote end point (address and port), but also sets
local address, while UDP stack stored in secondary hash table the socket
while its local address was INADDR_ANY (or ipv6 equivalent)
Sequence is :
- autobind() : choose a random local port, insert socket in hash tables
[while local address is INADDR_ANY]
- connect() : set remote address and port, change local address to IP
given by a route lookup.
When an incoming UDP frame comes, if more than 10 sockets are found in
primary hash table, we switch to secondary table, and fail to find
socket because its local address changed.
One solution to this problem is to rehash datagram socket if needed.
We add a new rehash(struct socket *) method in "struct proto", and
implement this method for UDP v4 & v6, using a common helper.
This rehashing only takes care of secondary hash table, since primary
hash (based on local port only) is not changed.
Reported-by: Krzysztof Piotr Oledzki <ole@ans.pl>
Signed-off-by: Eric Dumazet <eric.dumazet@gmail.com>
Tested-by: Krzysztof Piotr Oledzki <ole@ans.pl>
Signed-off-by: David S. Miller <davem@davemloft.net>
2010-09-08 09:08:44 +04:00
|
|
|
if (sk->sk_prot->rehash)
|
|
|
|
sk->sk_prot->rehash(sk);
|
|
|
|
}
|
2011-05-07 09:27:25 +04:00
|
|
|
inet->inet_daddr = fl4->daddr;
|
2009-10-15 10:30:45 +04:00
|
|
|
inet->inet_dport = usin->sin_port;
|
2019-09-13 04:16:39 +03:00
|
|
|
reuseport_has_conns(sk, true);
|
2005-04-17 02:20:36 +04:00
|
|
|
sk->sk_state = TCP_ESTABLISHED;
|
2015-07-29 02:02:05 +03:00
|
|
|
sk_set_txhash(sk);
|
2019-11-01 20:32:19 +03:00
|
|
|
inet->inet_id = prandom_u32();
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2010-06-11 10:31:35 +04:00
|
|
|
sk_dst_set(sk, &rt->dst);
|
2011-05-07 09:27:25 +04:00
|
|
|
err = 0;
|
|
|
|
out:
|
|
|
|
return err;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
2015-07-14 09:10:22 +03:00
|
|
|
EXPORT_SYMBOL(__ip4_datagram_connect);
|
|
|
|
|
|
|
|
int ip4_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
|
|
|
|
{
|
|
|
|
int res;
|
|
|
|
|
|
|
|
lock_sock(sk);
|
|
|
|
res = __ip4_datagram_connect(sk, uaddr, addr_len);
|
|
|
|
release_sock(sk);
|
|
|
|
return res;
|
|
|
|
}
|
2005-04-17 02:20:36 +04:00
|
|
|
EXPORT_SYMBOL(ip4_datagram_connect);
|
2013-01-21 06:00:03 +04:00
|
|
|
|
2014-06-10 17:43:01 +04:00
|
|
|
/* Because UDP xmit path can manipulate sk_dst_cache without holding
|
|
|
|
* socket lock, we need to use sk_dst_set() here,
|
|
|
|
* even if we own the socket lock.
|
|
|
|
*/
|
2013-01-21 06:00:03 +04:00
|
|
|
void ip4_datagram_release_cb(struct sock *sk)
|
|
|
|
{
|
|
|
|
const struct inet_sock *inet = inet_sk(sk);
|
|
|
|
const struct ip_options_rcu *inet_opt;
|
|
|
|
__be32 daddr = inet->inet_daddr;
|
2014-06-10 17:43:01 +04:00
|
|
|
struct dst_entry *dst;
|
2013-01-21 06:00:03 +04:00
|
|
|
struct flowi4 fl4;
|
|
|
|
struct rtable *rt;
|
|
|
|
|
|
|
|
rcu_read_lock();
|
2014-06-10 17:43:01 +04:00
|
|
|
|
|
|
|
dst = __sk_dst_get(sk);
|
|
|
|
if (!dst || !dst->obsolete || dst->ops->check(dst, 0)) {
|
|
|
|
rcu_read_unlock();
|
|
|
|
return;
|
|
|
|
}
|
2013-01-21 06:00:03 +04:00
|
|
|
inet_opt = rcu_dereference(inet->inet_opt);
|
|
|
|
if (inet_opt && inet_opt->opt.srr)
|
|
|
|
daddr = inet_opt->opt.faddr;
|
|
|
|
rt = ip_route_output_ports(sock_net(sk), &fl4, sk, daddr,
|
|
|
|
inet->inet_saddr, inet->inet_dport,
|
|
|
|
inet->inet_sport, sk->sk_protocol,
|
|
|
|
RT_CONN_FLAGS(sk), sk->sk_bound_dev_if);
|
2014-06-10 17:43:01 +04:00
|
|
|
|
|
|
|
dst = !IS_ERR(rt) ? &rt->dst : NULL;
|
|
|
|
sk_dst_set(sk, dst);
|
|
|
|
|
2013-01-21 06:00:03 +04:00
|
|
|
rcu_read_unlock();
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(ip4_datagram_release_cb);
|