]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * common UDP/RAW code | |
3 | * Linux INET implementation | |
4 | * | |
5 | * Authors: | |
6 | * Hideaki YOSHIFUJI <yoshfuji@linux-ipv6.org> | |
7 | * | |
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. | |
12 | */ | |
13 | ||
1da177e4 LT |
14 | #include <linux/types.h> |
15 | #include <linux/module.h> | |
16 | #include <linux/ip.h> | |
17 | #include <linux/in.h> | |
20380731 | 18 | #include <net/ip.h> |
1da177e4 | 19 | #include <net/sock.h> |
1da177e4 | 20 | #include <net/route.h> |
c752f073 | 21 | #include <net/tcp_states.h> |
1da177e4 LT |
22 | |
23 | int ip4_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len) | |
24 | { | |
25 | struct inet_sock *inet = inet_sk(sk); | |
26 | struct sockaddr_in *usin = (struct sockaddr_in *) uaddr; | |
27 | struct rtable *rt; | |
28 | u32 saddr; | |
29 | int oif; | |
30 | int err; | |
31 | ||
32 | ||
33 | if (addr_len < sizeof(*usin)) | |
34 | return -EINVAL; | |
35 | ||
36 | if (usin->sin_family != AF_INET) | |
37 | return -EAFNOSUPPORT; | |
38 | ||
39 | sk_dst_reset(sk); | |
40 | ||
41 | oif = sk->sk_bound_dev_if; | |
42 | saddr = inet->saddr; | |
43 | if (MULTICAST(usin->sin_addr.s_addr)) { | |
44 | if (!oif) | |
45 | oif = inet->mc_index; | |
46 | if (!saddr) | |
47 | saddr = inet->mc_addr; | |
48 | } | |
49 | err = ip_route_connect(&rt, usin->sin_addr.s_addr, saddr, | |
50 | RT_CONN_FLAGS(sk), oif, | |
51 | sk->sk_protocol, | |
52 | inet->sport, usin->sin_port, sk); | |
53 | if (err) | |
54 | return err; | |
55 | if ((rt->rt_flags & RTCF_BROADCAST) && !sock_flag(sk, SOCK_BROADCAST)) { | |
56 | ip_rt_put(rt); | |
57 | return -EACCES; | |
58 | } | |
59 | if (!inet->saddr) | |
60 | inet->saddr = rt->rt_src; /* Update source address */ | |
61 | if (!inet->rcv_saddr) | |
62 | inet->rcv_saddr = rt->rt_src; | |
63 | inet->daddr = rt->rt_dst; | |
64 | inet->dport = usin->sin_port; | |
65 | sk->sk_state = TCP_ESTABLISHED; | |
66 | inet->id = jiffies; | |
67 | ||
68 | sk_dst_set(sk, &rt->u.dst); | |
69 | return(0); | |
70 | } | |
71 | ||
72 | EXPORT_SYMBOL(ip4_datagram_connect); | |
73 |