2005-04-16 15:20:36 -07:00
|
|
|
/*
|
|
|
|
* common UDP/RAW code
|
|
|
|
* Linux INET implementation
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Hideaki YOSHIFUJI <yoshfuji@linux-ipv6.org>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version
|
|
|
|
* 2 of the License, or (at your option) any later version.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/types.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/ip.h>
|
|
|
|
#include <linux/in.h>
|
2005-08-15 22:18:02 -07:00
|
|
|
#include <net/ip.h>
|
2005-04-16 15:20:36 -07:00
|
|
|
#include <net/sock.h>
|
|
|
|
#include <net/route.h>
|
2005-08-09 20:08:28 -07:00
|
|
|
#include <net/tcp_states.h>
|
2005-04-16 15:20:36 -07:00
|
|
|
|
|
|
|
int ip4_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
|
|
|
|
{
|
|
|
|
struct inet_sock *inet = inet_sk(sk);
|
|
|
|
struct sockaddr_in *usin = (struct sockaddr_in *) uaddr;
|
2011-04-26 13:28:44 -07:00
|
|
|
struct flowi4 fl4;
|
2005-04-16 15:20:36 -07:00
|
|
|
struct rtable *rt;
|
2006-09-26 21:27:15 -07:00
|
|
|
__be32 saddr;
|
2005-04-16 15:20:36 -07:00
|
|
|
int oif;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
|
2007-02-09 07:24:47 -07:00
|
|
|
if (addr_len < sizeof(*usin))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (usin->sin_family != AF_INET)
|
|
|
|
return -EAFNOSUPPORT;
|
2005-04-16 15:20:36 -07:00
|
|
|
|
|
|
|
sk_dst_reset(sk);
|
|
|
|
|
|
|
|
oif = sk->sk_bound_dev_if;
|
2009-10-14 23:30:45 -07:00
|
|
|
saddr = inet->inet_saddr;
|
2007-12-16 14:45:43 -07:00
|
|
|
if (ipv4_is_multicast(usin->sin_addr.s_addr)) {
|
2005-04-16 15:20:36 -07:00
|
|
|
if (!oif)
|
|
|
|
oif = inet->mc_index;
|
|
|
|
if (!saddr)
|
|
|
|
saddr = inet->mc_addr;
|
|
|
|
}
|
2011-04-26 13:28:44 -07:00
|
|
|
rt = ip_route_connect(&fl4, usin->sin_addr.s_addr, saddr,
|
2011-03-02 15:31:35 -07:00
|
|
|
RT_CONN_FLAGS(sk), oif,
|
|
|
|
sk->sk_protocol,
|
|
|
|
inet->inet_sport, usin->sin_port, sk, true);
|
|
|
|
if (IS_ERR(rt)) {
|
|
|
|
err = PTR_ERR(rt);
|
2007-05-31 22:49:28 -07:00
|
|
|
if (err == -ENETUNREACH)
|
2008-07-16 20:20:11 -07:00
|
|
|
IP_INC_STATS_BH(sock_net(sk), IPSTATS_MIB_OUTNOROUTES);
|
2005-04-16 15:20:36 -07:00
|
|
|
return err;
|
2007-05-31 22:49:28 -07:00
|
|
|
}
|
|
|
|
|
2005-04-16 15:20:36 -07:00
|
|
|
if ((rt->rt_flags & RTCF_BROADCAST) && !sock_flag(sk, SOCK_BROADCAST)) {
|
|
|
|
ip_rt_put(rt);
|
|
|
|
return -EACCES;
|
|
|
|
}
|
2009-10-14 23:30:45 -07:00
|
|
|
if (!inet->inet_saddr)
|
2011-04-28 23:17:16 -07: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-07 22:08:44 -07:00
|
|
|
if (!inet->inet_rcv_saddr) {
|
2011-04-28 23:17:16 -07: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-07 22:08:44 -07:00
|
|
|
if (sk->sk_prot->rehash)
|
|
|
|
sk->sk_prot->rehash(sk);
|
|
|
|
}
|
2011-04-28 23:49:45 -07:00
|
|
|
inet->inet_daddr = fl4.daddr;
|
2009-10-14 23:30:45 -07:00
|
|
|
inet->inet_dport = usin->sin_port;
|
2005-04-16 15:20:36 -07:00
|
|
|
sk->sk_state = TCP_ESTABLISHED;
|
2009-10-14 23:30:45 -07:00
|
|
|
inet->inet_id = jiffies;
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2010-06-10 23:31:35 -07:00
|
|
|
sk_dst_set(sk, &rt->dst);
|
2010-09-22 13:43:57 -07:00
|
|
|
return 0;
|
2005-04-16 15:20:36 -07:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(ip4_datagram_connect);
|