blob: f8f3a37a14948f4d291ba85cac267b33ebdf3079 [file] [log] [blame]
Arnaldo Carvalho de Melo81297652005-12-13 23:15:24 -08001/*
2 * INET An implementation of the TCP/IP protocol suite for the LINUX
3 * operating system. INET is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
5 *
6 * Support for INET6 connection oriented protocols.
7 *
8 * Authors: See the TCPv6 sources
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or(at your option) any later version.
14 */
15
16#include <linux/config.h>
17#include <linux/module.h>
18#include <linux/in6.h>
19#include <linux/ipv6.h>
20#include <linux/jhash.h>
21
22#include <net/addrconf.h>
23#include <net/inet_connection_sock.h>
Arnaldo Carvalho de Melob9750ce2005-12-13 23:22:54 -080024#include <net/inet_ecn.h>
25#include <net/inet_hashtables.h>
26#include <net/ip6_route.h>
Arnaldo Carvalho de Melo81297652005-12-13 23:15:24 -080027#include <net/sock.h>
Adrian Bunk9f5336e2006-01-07 13:24:25 -080028#include <net/inet6_connection_sock.h>
Arnaldo Carvalho de Melo81297652005-12-13 23:15:24 -080029
Arnaldo Carvalho de Melob9750ce2005-12-13 23:22:54 -080030int inet6_csk_bind_conflict(const struct sock *sk,
31 const struct inet_bind_bucket *tb)
32{
33 const struct sock *sk2;
34 const struct hlist_node *node;
35
36 /* We must walk the whole port owner list in this case. -DaveM */
37 sk_for_each_bound(sk2, node, &tb->owners) {
38 if (sk != sk2 &&
39 (!sk->sk_bound_dev_if ||
40 !sk2->sk_bound_dev_if ||
41 sk->sk_bound_dev_if == sk2->sk_bound_dev_if) &&
42 (!sk->sk_reuse || !sk2->sk_reuse ||
43 sk2->sk_state == TCP_LISTEN) &&
44 ipv6_rcv_saddr_equal(sk, sk2))
45 break;
46 }
47
48 return node != NULL;
49}
50
51EXPORT_SYMBOL_GPL(inet6_csk_bind_conflict);
52
Arnaldo Carvalho de Melo81297652005-12-13 23:15:24 -080053/*
54 * request_sock (formerly open request) hash tables.
55 */
56static u32 inet6_synq_hash(const struct in6_addr *raddr, const u16 rport,
57 const u32 rnd, const u16 synq_hsize)
58{
59 u32 a = raddr->s6_addr32[0];
60 u32 b = raddr->s6_addr32[1];
61 u32 c = raddr->s6_addr32[2];
62
63 a += JHASH_GOLDEN_RATIO;
64 b += JHASH_GOLDEN_RATIO;
65 c += rnd;
66 __jhash_mix(a, b, c);
67
68 a += raddr->s6_addr32[3];
69 b += (u32)rport;
70 __jhash_mix(a, b, c);
71
72 return c & (synq_hsize - 1);
73}
74
75struct request_sock *inet6_csk_search_req(const struct sock *sk,
76 struct request_sock ***prevp,
77 const __u16 rport,
78 const struct in6_addr *raddr,
79 const struct in6_addr *laddr,
80 const int iif)
81{
82 const struct inet_connection_sock *icsk = inet_csk(sk);
83 struct listen_sock *lopt = icsk->icsk_accept_queue.listen_opt;
84 struct request_sock *req, **prev;
85
86 for (prev = &lopt->syn_table[inet6_synq_hash(raddr, rport,
87 lopt->hash_rnd,
88 lopt->nr_table_entries)];
89 (req = *prev) != NULL;
90 prev = &req->dl_next) {
Arnaldo Carvalho de Meloca304b62005-12-13 23:15:40 -080091 const struct inet6_request_sock *treq = inet6_rsk(req);
Arnaldo Carvalho de Melo81297652005-12-13 23:15:24 -080092
93 if (inet_rsk(req)->rmt_port == rport &&
94 req->rsk_ops->family == AF_INET6 &&
95 ipv6_addr_equal(&treq->rmt_addr, raddr) &&
96 ipv6_addr_equal(&treq->loc_addr, laddr) &&
97 (!treq->iif || treq->iif == iif)) {
98 BUG_TRAP(req->sk == NULL);
99 *prevp = prev;
100 return req;
101 }
102 }
103
104 return NULL;
105}
106
107EXPORT_SYMBOL_GPL(inet6_csk_search_req);
108
109void inet6_csk_reqsk_queue_hash_add(struct sock *sk,
110 struct request_sock *req,
111 const unsigned long timeout)
112{
113 struct inet_connection_sock *icsk = inet_csk(sk);
114 struct listen_sock *lopt = icsk->icsk_accept_queue.listen_opt;
Arnaldo Carvalho de Meloca304b62005-12-13 23:15:40 -0800115 const u32 h = inet6_synq_hash(&inet6_rsk(req)->rmt_addr,
Arnaldo Carvalho de Melo81297652005-12-13 23:15:24 -0800116 inet_rsk(req)->rmt_port,
117 lopt->hash_rnd, lopt->nr_table_entries);
118
119 reqsk_queue_hash_req(&icsk->icsk_accept_queue, h, req, timeout);
120 inet_csk_reqsk_queue_added(sk, timeout);
121}
122
123EXPORT_SYMBOL_GPL(inet6_csk_reqsk_queue_hash_add);
Arnaldo Carvalho de Melob9750ce2005-12-13 23:22:54 -0800124
125void inet6_csk_addr2sockaddr(struct sock *sk, struct sockaddr * uaddr)
126{
127 struct ipv6_pinfo *np = inet6_sk(sk);
128 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) uaddr;
129
130 sin6->sin6_family = AF_INET6;
131 ipv6_addr_copy(&sin6->sin6_addr, &np->daddr);
132 sin6->sin6_port = inet_sk(sk)->dport;
133 /* We do not store received flowlabel for TCP */
134 sin6->sin6_flowinfo = 0;
135 sin6->sin6_scope_id = 0;
136 if (sk->sk_bound_dev_if &&
137 ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL)
138 sin6->sin6_scope_id = sk->sk_bound_dev_if;
139}
140
141EXPORT_SYMBOL_GPL(inet6_csk_addr2sockaddr);
142
143int inet6_csk_xmit(struct sk_buff *skb, int ipfragok)
144{
145 struct sock *sk = skb->sk;
146 struct inet_sock *inet = inet_sk(sk);
147 struct ipv6_pinfo *np = inet6_sk(sk);
148 struct flowi fl;
149 struct dst_entry *dst;
150 struct in6_addr *final_p = NULL, final;
151
152 memset(&fl, 0, sizeof(fl));
153 fl.proto = sk->sk_protocol;
154 ipv6_addr_copy(&fl.fl6_dst, &np->daddr);
155 ipv6_addr_copy(&fl.fl6_src, &np->saddr);
156 fl.fl6_flowlabel = np->flow_label;
157 IP6_ECN_flow_xmit(sk, fl.fl6_flowlabel);
158 fl.oif = sk->sk_bound_dev_if;
159 fl.fl_ip_sport = inet->sport;
160 fl.fl_ip_dport = inet->dport;
161
162 if (np->opt && np->opt->srcrt) {
163 struct rt0_hdr *rt0 = (struct rt0_hdr *)np->opt->srcrt;
164 ipv6_addr_copy(&final, &fl.fl6_dst);
165 ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
166 final_p = &final;
167 }
168
169 dst = __sk_dst_check(sk, np->dst_cookie);
170
171 if (dst == NULL) {
172 int err = ip6_dst_lookup(sk, &dst, &fl);
173
174 if (err) {
175 sk->sk_err_soft = -err;
176 return err;
177 }
178
179 if (final_p)
180 ipv6_addr_copy(&fl.fl6_dst, final_p);
181
182 if ((err = xfrm_lookup(&dst, &fl, sk, 0)) < 0) {
183 sk->sk_route_caps = 0;
184 return err;
185 }
186
187 ip6_dst_store(sk, dst, NULL);
188 sk->sk_route_caps = dst->dev->features &
189 ~(NETIF_F_IP_CSUM | NETIF_F_TSO);
190 }
191
192 skb->dst = dst_clone(dst);
193
194 /* Restore final destination back after routing done */
195 ipv6_addr_copy(&fl.fl6_dst, &np->daddr);
196
197 return ip6_xmit(sk, skb, &fl, np->opt, 0);
198}
199
200EXPORT_SYMBOL_GPL(inet6_csk_xmit);