Commit | Line | Data |
---|---|---|
1da177e4 LT |
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 | ||
1da177e4 LT |
14 | #include <linux/types.h> |
15 | #include <linux/module.h> | |
16 | #include <linux/ip.h> | |
17 | #include <linux/in.h> | |
20380731 | 18 | #include <net/ip.h> |
1da177e4 | 19 | #include <net/sock.h> |
1da177e4 | 20 | #include <net/route.h> |
c752f073 | 21 | #include <net/tcp_states.h> |
1da177e4 | 22 | |
4b633bbe | 23 | int __ip4_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len) |
1da177e4 LT |
24 | { |
25 | struct inet_sock *inet = inet_sk(sk); | |
26 | struct sockaddr_in *usin = (struct sockaddr_in *) uaddr; | |
3038eeac | 27 | struct flowi4 *fl4; |
1da177e4 | 28 | struct rtable *rt; |
bada8adc | 29 | __be32 saddr; |
1da177e4 LT |
30 | int oif; |
31 | int err; | |
32 | ||
1da177e4 | 33 | |
e905a9ed YH |
34 | if (addr_len < sizeof(*usin)) |
35 | return -EINVAL; | |
36 | ||
37 | if (usin->sin_family != AF_INET) | |
38 | return -EAFNOSUPPORT; | |
1da177e4 LT |
39 | |
40 | sk_dst_reset(sk); | |
41 | ||
42 | oif = sk->sk_bound_dev_if; | |
c720c7e8 | 43 | saddr = inet->inet_saddr; |
f97c1e0c | 44 | if (ipv4_is_multicast(usin->sin_addr.s_addr)) { |
1da177e4 LT |
45 | if (!oif) |
46 | oif = inet->mc_index; | |
47 | if (!saddr) | |
48 | saddr = inet->mc_addr; | |
49 | } | |
3038eeac DM |
50 | fl4 = &inet->cork.fl.u.ip4; |
51 | rt = ip_route_connect(fl4, usin->sin_addr.s_addr, saddr, | |
b23dd4fe DM |
52 | RT_CONN_FLAGS(sk), oif, |
53 | sk->sk_protocol, | |
54 | inet->inet_sport, usin->sin_port, sk, true); | |
55 | if (IS_ERR(rt)) { | |
56 | err = PTR_ERR(rt); | |
584bdf8c | 57 | if (err == -ENETUNREACH) |
ac6a5b92 | 58 | IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTNOROUTES); |
3038eeac | 59 | goto out; |
584bdf8c WD |
60 | } |
61 | ||
1da177e4 LT |
62 | if ((rt->rt_flags & RTCF_BROADCAST) && !sock_flag(sk, SOCK_BROADCAST)) { |
63 | ip_rt_put(rt); | |
3038eeac DM |
64 | err = -EACCES; |
65 | goto out; | |
1da177e4 | 66 | } |
c720c7e8 | 67 | if (!inet->inet_saddr) |
3038eeac | 68 | inet->inet_saddr = fl4->saddr; /* Update source address */ |
719f8358 | 69 | if (!inet->inet_rcv_saddr) { |
3038eeac | 70 | inet->inet_rcv_saddr = fl4->saddr; |
719f8358 ED |
71 | if (sk->sk_prot->rehash) |
72 | sk->sk_prot->rehash(sk); | |
73 | } | |
3038eeac | 74 | inet->inet_daddr = fl4->daddr; |
c720c7e8 | 75 | inet->inet_dport = usin->sin_port; |
1da177e4 | 76 | sk->sk_state = TCP_ESTABLISHED; |
c720c7e8 | 77 | inet->inet_id = jiffies; |
1da177e4 | 78 | |
d8d1f30b | 79 | sk_dst_set(sk, &rt->dst); |
3038eeac DM |
80 | err = 0; |
81 | out: | |
3038eeac | 82 | return err; |
1da177e4 | 83 | } |
4b633bbe ED |
84 | EXPORT_SYMBOL(__ip4_datagram_connect); |
85 | ||
86 | int ip4_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len) | |
87 | { | |
88 | int res; | |
89 | ||
90 | lock_sock(sk); | |
91 | res = __ip4_datagram_connect(sk, uaddr, addr_len); | |
92 | release_sock(sk); | |
93 | return res; | |
94 | } | |
1da177e4 | 95 | EXPORT_SYMBOL(ip4_datagram_connect); |
8141ed9f | 96 | |
9138f241 ED |
97 | /* Because UDP xmit path can manipulate sk_dst_cache without holding |
98 | * socket lock, we need to use sk_dst_set() here, | |
99 | * even if we own the socket lock. | |
100 | */ | |
8141ed9f SK |
101 | void ip4_datagram_release_cb(struct sock *sk) |
102 | { | |
103 | const struct inet_sock *inet = inet_sk(sk); | |
104 | const struct ip_options_rcu *inet_opt; | |
105 | __be32 daddr = inet->inet_daddr; | |
9138f241 | 106 | struct dst_entry *dst; |
8141ed9f SK |
107 | struct flowi4 fl4; |
108 | struct rtable *rt; | |
109 | ||
8141ed9f | 110 | rcu_read_lock(); |
9138f241 ED |
111 | |
112 | dst = __sk_dst_get(sk); | |
113 | if (!dst || !dst->obsolete || dst->ops->check(dst, 0)) { | |
114 | rcu_read_unlock(); | |
115 | return; | |
116 | } | |
8141ed9f SK |
117 | inet_opt = rcu_dereference(inet->inet_opt); |
118 | if (inet_opt && inet_opt->opt.srr) | |
119 | daddr = inet_opt->opt.faddr; | |
120 | rt = ip_route_output_ports(sock_net(sk), &fl4, sk, daddr, | |
121 | inet->inet_saddr, inet->inet_dport, | |
122 | inet->inet_sport, sk->sk_protocol, | |
123 | RT_CONN_FLAGS(sk), sk->sk_bound_dev_if); | |
9138f241 ED |
124 | |
125 | dst = !IS_ERR(rt) ? &rt->dst : NULL; | |
126 | sk_dst_set(sk, dst); | |
127 | ||
8141ed9f SK |
128 | rcu_read_unlock(); |
129 | } | |
130 | EXPORT_SYMBOL_GPL(ip4_datagram_release_cb); |