Blame view

net/ipv4/datagram.c 1.77 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
22
23
24
25
26
  
  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;
  	struct rtable *rt;
bada8adc4   Al Viro   [IPV4]: ip_route_...
27
  	__be32 saddr;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
28
29
  	int oif;
  	int err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
30

e905a9eda   YOSHIFUJI Hideaki   [NET] IPV4: Fix w...
31
32
33
34
35
  	if (addr_len < sizeof(*usin))
  		return -EINVAL;
  
  	if (usin->sin_family != AF_INET)
  		return -EAFNOSUPPORT;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
36
37
38
39
40
41
42
43
44
45
46
47
48
49
  
  	sk_dst_reset(sk);
  
  	oif = sk->sk_bound_dev_if;
  	saddr = inet->saddr;
  	if (MULTICAST(usin->sin_addr.s_addr)) {
  		if (!oif)
  			oif = inet->mc_index;
  		if (!saddr)
  			saddr = inet->mc_addr;
  	}
  	err = ip_route_connect(&rt, usin->sin_addr.s_addr, saddr,
  			       RT_CONN_FLAGS(sk), oif,
  			       sk->sk_protocol,
8eb9086f2   David S. Miller   [IPV4/IPV6]: Alwa...
50
  			       inet->sport, usin->sin_port, sk, 1);
584bdf8cb   Wei Dong   [IPV4]: Fix "ipOu...
51
52
53
  	if (err) {
  		if (err == -ENETUNREACH)
  			IP_INC_STATS_BH(IPSTATS_MIB_OUTNOROUTES);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
54
  		return err;
584bdf8cb   Wei Dong   [IPV4]: Fix "ipOu...
55
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
56
57
58
59
  	if ((rt->rt_flags & RTCF_BROADCAST) && !sock_flag(sk, SOCK_BROADCAST)) {
  		ip_rt_put(rt);
  		return -EACCES;
  	}
e905a9eda   YOSHIFUJI Hideaki   [NET] IPV4: Fix w...
60
61
  	if (!inet->saddr)
  		inet->saddr = rt->rt_src;	/* Update source address */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
62
63
64
65
66
67
68
69
70
71
72
73
  	if (!inet->rcv_saddr)
  		inet->rcv_saddr = rt->rt_src;
  	inet->daddr = rt->rt_dst;
  	inet->dport = usin->sin_port;
  	sk->sk_state = TCP_ESTABLISHED;
  	inet->id = jiffies;
  
  	sk_dst_set(sk, &rt->u.dst);
  	return(0);
  }
  
  EXPORT_SYMBOL(ip4_datagram_connect);