Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * common UDP/RAW code |
| 3 | * Linux INET implementation |
| 4 | * |
| 5 | * Authors: |
| 6 | * Hideaki YOSHIFUJI <yoshfuji@linux-ipv6.org> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License |
| 10 | * as published by the Free Software Foundation; either version |
| 11 | * 2 of the License, or (at your option) any later version. |
| 12 | */ |
| 13 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/types.h> |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/ip.h> |
| 17 | #include <linux/in.h> |
Arnaldo Carvalho de Melo | 2038073 | 2005-08-16 02:18:02 -0300 | [diff] [blame] | 18 | #include <net/ip.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <net/sock.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <net/route.h> |
Arnaldo Carvalho de Melo | c752f07 | 2005-08-09 20:08:28 -0700 | [diff] [blame] | 21 | #include <net/tcp_states.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
| 23 | int ip4_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len) |
| 24 | { |
| 25 | struct inet_sock *inet = inet_sk(sk); |
| 26 | struct sockaddr_in *usin = (struct sockaddr_in *) uaddr; |
| 27 | struct rtable *rt; |
Al Viro | bada8ad | 2006-09-26 21:27:15 -0700 | [diff] [blame] | 28 | __be32 saddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | int oif; |
| 30 | int err; |
| 31 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 33 | if (addr_len < sizeof(*usin)) |
| 34 | return -EINVAL; |
| 35 | |
| 36 | if (usin->sin_family != AF_INET) |
| 37 | return -EAFNOSUPPORT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
| 39 | sk_dst_reset(sk); |
| 40 | |
| 41 | oif = sk->sk_bound_dev_if; |
Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 42 | saddr = inet->inet_saddr; |
Joe Perches | f97c1e0 | 2007-12-16 13:45:43 -0800 | [diff] [blame] | 43 | if (ipv4_is_multicast(usin->sin_addr.s_addr)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | if (!oif) |
| 45 | oif = inet->mc_index; |
| 46 | if (!saddr) |
| 47 | saddr = inet->mc_addr; |
| 48 | } |
| 49 | err = ip_route_connect(&rt, usin->sin_addr.s_addr, saddr, |
| 50 | RT_CONN_FLAGS(sk), oif, |
| 51 | sk->sk_protocol, |
Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 52 | inet->inet_sport, usin->sin_port, sk, 1); |
Wei Dong | 584bdf8 | 2007-05-31 22:49:28 -0700 | [diff] [blame] | 53 | if (err) { |
| 54 | if (err == -ENETUNREACH) |
Pavel Emelyanov | 7c73a6f | 2008-07-16 20:20:11 -0700 | [diff] [blame] | 55 | IP_INC_STATS_BH(sock_net(sk), IPSTATS_MIB_OUTNOROUTES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | return err; |
Wei Dong | 584bdf8 | 2007-05-31 22:49:28 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | if ((rt->rt_flags & RTCF_BROADCAST) && !sock_flag(sk, SOCK_BROADCAST)) { |
| 60 | ip_rt_put(rt); |
| 61 | return -EACCES; |
| 62 | } |
Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 63 | if (!inet->inet_saddr) |
| 64 | inet->inet_saddr = rt->rt_src; /* Update source address */ |
Eric Dumazet | 719f835 | 2010-09-08 05:08:44 +0000 | [diff] [blame] | 65 | if (!inet->inet_rcv_saddr) { |
Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 66 | inet->inet_rcv_saddr = rt->rt_src; |
Eric Dumazet | 719f835 | 2010-09-08 05:08:44 +0000 | [diff] [blame] | 67 | if (sk->sk_prot->rehash) |
| 68 | sk->sk_prot->rehash(sk); |
| 69 | } |
Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 70 | inet->inet_daddr = rt->rt_dst; |
| 71 | inet->inet_dport = usin->sin_port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | sk->sk_state = TCP_ESTABLISHED; |
Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 73 | inet->inet_id = jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 75 | sk_dst_set(sk, &rt->dst); |
Eric Dumazet | a02cec2 | 2010-09-22 20:43:57 +0000 | [diff] [blame] | 76 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | EXPORT_SYMBOL(ip4_datagram_connect); |