xen: tidy up whitespace in drivers/xen/Makefile
[linux-2.6.git] / net / ipv4 / datagram.c
blob85bd24ca4f6dda83e51170dabdfb7584e932a52c
1 /*
2 * common UDP/RAW code
3 * Linux INET implementation
5 * Authors:
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>
16 #include <linux/ip.h>
17 #include <linux/in.h>
18 #include <net/ip.h>
19 #include <net/sock.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;
27 struct rtable *rt;
28 __be32 saddr;
29 int oif;
30 int err;
33 if (addr_len < sizeof(*usin))
34 return -EINVAL;
36 if (usin->sin_family != AF_INET)
37 return -EAFNOSUPPORT;
39 sk_dst_reset(sk);
41 oif = sk->sk_bound_dev_if;
42 saddr = inet->inet_saddr;
43 if (ipv4_is_multicast(usin->sin_addr.s_addr)) {
44 if (!oif)
45 oif = inet->mc_index;
46 if (!saddr)
47 saddr = inet->mc_addr;
49 rt = ip_route_connect(usin->sin_addr.s_addr, saddr,
50 RT_CONN_FLAGS(sk), oif,
51 sk->sk_protocol,
52 inet->inet_sport, usin->sin_port, sk, true);
53 if (IS_ERR(rt)) {
54 err = PTR_ERR(rt);
55 if (err == -ENETUNREACH)
56 IP_INC_STATS_BH(sock_net(sk), IPSTATS_MIB_OUTNOROUTES);
57 return err;
60 if ((rt->rt_flags & RTCF_BROADCAST) && !sock_flag(sk, SOCK_BROADCAST)) {
61 ip_rt_put(rt);
62 return -EACCES;
64 if (!inet->inet_saddr)
65 inet->inet_saddr = rt->rt_src; /* Update source address */
66 if (!inet->inet_rcv_saddr) {
67 inet->inet_rcv_saddr = rt->rt_src;
68 if (sk->sk_prot->rehash)
69 sk->sk_prot->rehash(sk);
71 inet->inet_daddr = rt->rt_dst;
72 inet->inet_dport = usin->sin_port;
73 sk->sk_state = TCP_ESTABLISHED;
74 inet->inet_id = jiffies;
76 sk_dst_set(sk, &rt->dst);
77 return 0;
79 EXPORT_SYMBOL(ip4_datagram_connect);