Branch data Line data Source code
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 : :
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>
22 : :
23 : 0 : 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 flowi4 *fl4;
28 : : struct rtable *rt;
29 : : __be32 saddr;
30 : : int oif;
31 : : int err;
32 : :
33 : :
34 [ + - ]: 37 : if (addr_len < sizeof(*usin))
35 : : return -EINVAL;
36 : :
37 [ + - ]: 37 : if (usin->sin_family != AF_INET)
38 : : return -EAFNOSUPPORT;
39 : :
40 : : sk_dst_reset(sk);
41 : :
42 : : lock_sock(sk);
43 : :
44 : 37 : oif = sk->sk_bound_dev_if;
45 : 37 : saddr = inet->inet_saddr;
46 [ - + ]: 37 : if (ipv4_is_multicast(usin->sin_addr.s_addr)) {
47 [ # # ]: 0 : if (!oif)
48 : 0 : oif = inet->mc_index;
49 [ # # ]: 0 : if (!saddr)
50 : 0 : saddr = inet->mc_addr;
51 : : }
52 : 37 : fl4 = &inet->cork.fl.u.ip4;
53 : 37 : rt = ip_route_connect(fl4, usin->sin_addr.s_addr, saddr,
54 : 74 : RT_CONN_FLAGS(sk), oif,
55 : : sk->sk_protocol,
56 : : inet->inet_sport, usin->sin_port, sk);
57 [ - + ]: 37 : if (IS_ERR(rt)) {
58 : : err = PTR_ERR(rt);
59 [ # # ]: 0 : if (err == -ENETUNREACH)
60 : 0 : IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTNOROUTES);
61 : : goto out;
62 : : }
63 : :
64 [ - + ][ # # ]: 37 : if ((rt->rt_flags & RTCF_BROADCAST) && !sock_flag(sk, SOCK_BROADCAST)) {
65 : : ip_rt_put(rt);
66 : : err = -EACCES;
67 : 0 : goto out;
68 : : }
69 [ + - ]: 37 : if (!inet->inet_saddr)
70 : 37 : inet->inet_saddr = fl4->saddr; /* Update source address */
71 [ + - ]: 37 : if (!inet->inet_rcv_saddr) {
72 : 37 : inet->inet_rcv_saddr = fl4->saddr;
73 [ + - ]: 37 : if (sk->sk_prot->rehash)
74 : 37 : sk->sk_prot->rehash(sk);
75 : : }
76 : 37 : inet->inet_daddr = fl4->daddr;
77 : 37 : inet->inet_dport = usin->sin_port;
78 : 37 : sk->sk_state = TCP_ESTABLISHED;
79 : 37 : inet->inet_id = jiffies;
80 : :
81 : 37 : sk_dst_set(sk, &rt->dst);
82 : : err = 0;
83 : : out:
84 : 37 : release_sock(sk);
85 : 37 : return err;
86 : : }
87 : : EXPORT_SYMBOL(ip4_datagram_connect);
88 : :
89 : 0 : void ip4_datagram_release_cb(struct sock *sk)
90 : : {
91 : : const struct inet_sock *inet = inet_sk(sk);
92 : : const struct ip_options_rcu *inet_opt;
93 : 178 : __be32 daddr = inet->inet_daddr;
94 : : struct flowi4 fl4;
95 : : struct rtable *rt;
96 : :
97 [ + + ][ - + ]: 178 : if (! __sk_dst_get(sk) || __sk_dst_check(sk, 0))
98 : 178 : return;
99 : :
100 : : rcu_read_lock();
101 : 0 : inet_opt = rcu_dereference(inet->inet_opt);
102 [ # # ][ # # ]: 0 : if (inet_opt && inet_opt->opt.srr)
103 : 0 : daddr = inet_opt->opt.faddr;
104 : 0 : rt = ip_route_output_ports(sock_net(sk), &fl4, sk, daddr,
105 : : inet->inet_saddr, inet->inet_dport,
106 : : inet->inet_sport, sk->sk_protocol,
107 : 0 : RT_CONN_FLAGS(sk), sk->sk_bound_dev_if);
108 [ # # ]: 0 : if (!IS_ERR(rt))
109 : 0 : __sk_dst_set(sk, &rt->dst);
110 : : rcu_read_unlock();
111 : : }
112 : : EXPORT_SYMBOL_GPL(ip4_datagram_release_cb);
|