3 * Linux INET implementation
6 * Hideaki YOSHIFUJI <yoshfuji@linux-ipv6.org>
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.
14 #include <linux/types.h>
15 #include <linux/module.h>
20 #include <net/route.h>
21 #include <net/tcp_states.h>
23 int ip4_datagram_connect(struct sock
*sk
, struct sockaddr
*uaddr
, int addr_len
)
25 struct inet_sock
*inet
= inet_sk(sk
);
26 struct sockaddr_in
*usin
= (struct sockaddr_in
*) uaddr
;
34 if (addr_len
< sizeof(*usin
))
37 if (usin
->sin_family
!= AF_INET
)
44 oif
= sk
->sk_bound_dev_if
;
45 saddr
= inet
->inet_saddr
;
46 if (ipv4_is_multicast(usin
->sin_addr
.s_addr
)) {
50 saddr
= inet
->mc_addr
;
52 fl4
= &inet
->cork
.fl
.u
.ip4
;
53 rt
= ip_route_connect(fl4
, usin
->sin_addr
.s_addr
, saddr
,
54 RT_CONN_FLAGS(sk
), oif
,
56 inet
->inet_sport
, usin
->sin_port
, sk
);
59 if (err
== -ENETUNREACH
)
60 IP_INC_STATS(sock_net(sk
), IPSTATS_MIB_OUTNOROUTES
);
64 if ((rt
->rt_flags
& RTCF_BROADCAST
) && !sock_flag(sk
, SOCK_BROADCAST
)) {
69 if (!inet
->inet_saddr
)
70 inet
->inet_saddr
= fl4
->saddr
; /* Update source address */
71 if (!inet
->inet_rcv_saddr
) {
72 inet
->inet_rcv_saddr
= fl4
->saddr
;
73 if (sk
->sk_prot
->rehash
)
74 sk
->sk_prot
->rehash(sk
);
76 inet
->inet_daddr
= fl4
->daddr
;
77 inet
->inet_dport
= usin
->sin_port
;
78 sk
->sk_state
= TCP_ESTABLISHED
;
80 inet
->inet_id
= jiffies
;
82 sk_dst_set(sk
, &rt
->dst
);
88 EXPORT_SYMBOL(ip4_datagram_connect
);
90 /* Because UDP xmit path can manipulate sk_dst_cache without holding
91 * socket lock, we need to use sk_dst_set() here,
92 * even if we own the socket lock.
94 void ip4_datagram_release_cb(struct sock
*sk
)
96 const struct inet_sock
*inet
= inet_sk(sk
);
97 const struct ip_options_rcu
*inet_opt
;
98 __be32 daddr
= inet
->inet_daddr
;
99 struct dst_entry
*dst
;
105 dst
= __sk_dst_get(sk
);
106 if (!dst
|| !dst
->obsolete
|| dst
->ops
->check(dst
, 0)) {
110 inet_opt
= rcu_dereference(inet
->inet_opt
);
111 if (inet_opt
&& inet_opt
->opt
.srr
)
112 daddr
= inet_opt
->opt
.faddr
;
113 rt
= ip_route_output_ports(sock_net(sk
), &fl4
, sk
, daddr
,
114 inet
->inet_saddr
, inet
->inet_dport
,
115 inet
->inet_sport
, sk
->sk_protocol
,
116 RT_CONN_FLAGS(sk
), sk
->sk_bound_dev_if
);
118 dst
= !IS_ERR(rt
) ? &rt
->dst
: NULL
;
123 EXPORT_SYMBOL_GPL(ip4_datagram_release_cb
);