Blame view

net/ipv4/datagram.c 3.17 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
6
7
8
9
10
11
12
  /*
   *	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.
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
13
14
15
16
  #include <linux/types.h>
  #include <linux/module.h>
  #include <linux/ip.h>
  #include <linux/in.h>
20380731b   Arnaldo Carvalho de Melo   [NET]: Fix sparse...
17
  #include <net/ip.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
18
  #include <net/sock.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
19
  #include <net/route.h>
c752f0739   Arnaldo Carvalho de Melo   [TCP]: Move the t...
20
  #include <net/tcp_states.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
21

03645a11a   Eric Dumazet   ipv6: lock socket...
22
  int __ip4_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
23
24
25
  {
  	struct inet_sock *inet = inet_sk(sk);
  	struct sockaddr_in *usin = (struct sockaddr_in *) uaddr;
3038eeac0   David S. Miller   ipv4: Lock socket...
26
  	struct flowi4 *fl4;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
27
  	struct rtable *rt;
bada8adc4   Al Viro   [IPV4]: ip_route_...
28
  	__be32 saddr;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
29
30
  	int oif;
  	int err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
31

e905a9eda   YOSHIFUJI Hideaki   [NET] IPV4: Fix w...
32
33
34
35
36
  	if (addr_len < sizeof(*usin))
  		return -EINVAL;
  
  	if (usin->sin_family != AF_INET)
  		return -EAFNOSUPPORT;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
37
38
39
40
  
  	sk_dst_reset(sk);
  
  	oif = sk->sk_bound_dev_if;
c720c7e83   Eric Dumazet   inet: rename some...
41
  	saddr = inet->inet_saddr;
f97c1e0c6   Joe Perches   [IPV4] net/ipv4: ...
42
  	if (ipv4_is_multicast(usin->sin_addr.s_addr)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
43
44
45
46
47
  		if (!oif)
  			oif = inet->mc_index;
  		if (!saddr)
  			saddr = inet->mc_addr;
  	}
3038eeac0   David S. Miller   ipv4: Lock socket...
48
49
  	fl4 = &inet->cork.fl.u.ip4;
  	rt = ip_route_connect(fl4, usin->sin_addr.s_addr, saddr,
b23dd4fe4   David S. Miller   ipv4: Make output...
50
51
  			      RT_CONN_FLAGS(sk), oif,
  			      sk->sk_protocol,
0e0d44ab4   Steffen Klassert   net: Remove FLOWI...
52
  			      inet->inet_sport, usin->sin_port, sk);
b23dd4fe4   David S. Miller   ipv4: Make output...
53
54
  	if (IS_ERR(rt)) {
  		err = PTR_ERR(rt);
584bdf8cb   Wei Dong   [IPV4]: Fix "ipOu...
55
  		if (err == -ENETUNREACH)
c9e904299   Eric Dumazet   ipv4: fix possibl...
56
  			IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTNOROUTES);
3038eeac0   David S. Miller   ipv4: Lock socket...
57
  		goto out;
584bdf8cb   Wei Dong   [IPV4]: Fix "ipOu...
58
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
59
60
  	if ((rt->rt_flags & RTCF_BROADCAST) && !sock_flag(sk, SOCK_BROADCAST)) {
  		ip_rt_put(rt);
3038eeac0   David S. Miller   ipv4: Lock socket...
61
62
  		err = -EACCES;
  		goto out;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
63
  	}
c720c7e83   Eric Dumazet   inet: rename some...
64
  	if (!inet->inet_saddr)
3038eeac0   David S. Miller   ipv4: Lock socket...
65
  		inet->inet_saddr = fl4->saddr;	/* Update source address */
719f83585   Eric Dumazet   udp: add rehash o...
66
  	if (!inet->inet_rcv_saddr) {
3038eeac0   David S. Miller   ipv4: Lock socket...
67
  		inet->inet_rcv_saddr = fl4->saddr;
719f83585   Eric Dumazet   udp: add rehash o...
68
69
70
  		if (sk->sk_prot->rehash)
  			sk->sk_prot->rehash(sk);
  	}
3038eeac0   David S. Miller   ipv4: Lock socket...
71
  	inet->inet_daddr = fl4->daddr;
c720c7e83   Eric Dumazet   inet: rename some...
72
  	inet->inet_dport = usin->sin_port;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
73
  	sk->sk_state = TCP_ESTABLISHED;
877d1f629   Tom Herbert   net: Set sk_txhas...
74
  	sk_set_txhash(sk);
c720c7e83   Eric Dumazet   inet: rename some...
75
  	inet->inet_id = jiffies;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
76

d8d1f30b9   Changli Gao   net-next: remove ...
77
  	sk_dst_set(sk, &rt->dst);
3038eeac0   David S. Miller   ipv4: Lock socket...
78
79
  	err = 0;
  out:
3038eeac0   David S. Miller   ipv4: Lock socket...
80
  	return err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
81
  }
03645a11a   Eric Dumazet   ipv6: lock socket...
82
83
84
85
86
87
88
89
90
91
92
  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;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
93
  EXPORT_SYMBOL(ip4_datagram_connect);
8141ed9fc   Steffen Klassert   ipv4: Add a socke...
94

9709674e6   Eric Dumazet   ipv4: fix a race ...
95
96
97
98
  /* 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.
   */
8141ed9fc   Steffen Klassert   ipv4: Add a socke...
99
100
101
102
103
  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;
9709674e6   Eric Dumazet   ipv4: fix a race ...
104
  	struct dst_entry *dst;
8141ed9fc   Steffen Klassert   ipv4: Add a socke...
105
106
  	struct flowi4 fl4;
  	struct rtable *rt;
8141ed9fc   Steffen Klassert   ipv4: Add a socke...
107
  	rcu_read_lock();
9709674e6   Eric Dumazet   ipv4: fix a race ...
108
109
110
111
112
113
  
  	dst = __sk_dst_get(sk);
  	if (!dst || !dst->obsolete || dst->ops->check(dst, 0)) {
  		rcu_read_unlock();
  		return;
  	}
8141ed9fc   Steffen Klassert   ipv4: Add a socke...
114
115
116
117
118
119
120
  	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);
9709674e6   Eric Dumazet   ipv4: fix a race ...
121
122
123
  
  	dst = !IS_ERR(rt) ? &rt->dst : NULL;
  	sk_dst_set(sk, dst);
8141ed9fc   Steffen Klassert   ipv4: Add a socke...
124
125
126
  	rcu_read_unlock();
  }
  EXPORT_SYMBOL_GPL(ip4_datagram_release_cb);