Linux 2.6.17.7
[linux/fpc-iii.git] / net / ipv4 / datagram.c
blobc1b42b5257f8455d54c0388ac1ac25e76595b659
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/config.h>
15 #include <linux/types.h>
16 #include <linux/module.h>
17 #include <linux/ip.h>
18 #include <linux/in.h>
19 #include <net/ip.h>
20 #include <net/sock.h>
21 #include <net/route.h>
22 #include <net/tcp_states.h>
24 int ip4_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
26 struct inet_sock *inet = inet_sk(sk);
27 struct sockaddr_in *usin = (struct sockaddr_in *) uaddr;
28 struct rtable *rt;
29 u32 saddr;
30 int oif;
31 int err;
34 if (addr_len < sizeof(*usin))
35 return -EINVAL;
37 if (usin->sin_family != AF_INET)
38 return -EAFNOSUPPORT;
40 sk_dst_reset(sk);
42 oif = sk->sk_bound_dev_if;
43 saddr = inet->saddr;
44 if (MULTICAST(usin->sin_addr.s_addr)) {
45 if (!oif)
46 oif = inet->mc_index;
47 if (!saddr)
48 saddr = inet->mc_addr;
50 err = ip_route_connect(&rt, usin->sin_addr.s_addr, saddr,
51 RT_CONN_FLAGS(sk), oif,
52 sk->sk_protocol,
53 inet->sport, usin->sin_port, sk);
54 if (err)
55 return err;
56 if ((rt->rt_flags & RTCF_BROADCAST) && !sock_flag(sk, SOCK_BROADCAST)) {
57 ip_rt_put(rt);
58 return -EACCES;
60 if (!inet->saddr)
61 inet->saddr = rt->rt_src; /* Update source address */
62 if (!inet->rcv_saddr)
63 inet->rcv_saddr = rt->rt_src;
64 inet->daddr = rt->rt_dst;
65 inet->dport = usin->sin_port;
66 sk->sk_state = TCP_ESTABLISHED;
67 inet->id = jiffies;
69 sk_dst_set(sk, &rt->u.dst);
70 return(0);
73 EXPORT_SYMBOL(ip4_datagram_connect);