blob: 7a47399cf31fd81817c83b41894d4184a88481e4 [file] [log] [blame]
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -08001/*
2 * DCCP over IPv6
Arnaldo Carvalho de Melo45329e72006-03-20 22:01:29 -08003 * Linux INET6 implementation
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -08004 *
5 * Based on net/dccp6/ipv6.c
6 *
7 * Arnaldo Carvalho de Melo <acme@ghostprotocols.net>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
13 */
14
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -080015#include <linux/module.h>
16#include <linux/random.h>
17#include <linux/xfrm.h>
18
19#include <net/addrconf.h>
20#include <net/inet_common.h>
21#include <net/inet_hashtables.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020022#include <net/inet_sock.h>
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -080023#include <net/inet6_connection_sock.h>
24#include <net/inet6_hashtables.h>
25#include <net/ip6_route.h>
26#include <net/ipv6.h>
27#include <net/protocol.h>
28#include <net/transp_v6.h>
David S. Milleraa0e4e42006-01-06 22:55:39 -080029#include <net/ip6_checksum.h>
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -080030#include <net/xfrm.h>
31
32#include "dccp.h"
33#include "ipv6.h"
Ian McDonald4b79f0a2006-07-23 23:33:28 -070034#include "feat.h"
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -080035
Arnaldo Carvalho de Melo72478872006-03-20 22:00:37 -080036/* Socket used for sending RSTs and ACKs */
37static struct socket *dccp_v6_ctl_socket;
38
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -080039static void dccp_v6_ctl_send_reset(struct sk_buff *skb);
40static void dccp_v6_reqsk_send_ack(struct sk_buff *skb,
41 struct request_sock *req);
42static void dccp_v6_send_check(struct sock *sk, int len, struct sk_buff *skb);
43
44static int dccp_v6_do_rcv(struct sock *sk, struct sk_buff *skb);
45
46static struct inet_connection_sock_af_ops dccp_ipv6_mapped;
47static struct inet_connection_sock_af_ops dccp_ipv6_af_ops;
48
49static int dccp_v6_get_port(struct sock *sk, unsigned short snum)
50{
51 return inet_csk_get_port(&dccp_hashinfo, sk, snum,
52 inet6_csk_bind_conflict);
53}
54
55static void dccp_v6_hash(struct sock *sk)
56{
57 if (sk->sk_state != DCCP_CLOSED) {
58 if (inet_csk(sk)->icsk_af_ops == &dccp_ipv6_mapped) {
Arnaldo Carvalho de Meloc985ed72006-03-20 21:23:39 -080059 dccp_hash(sk);
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -080060 return;
61 }
62 local_bh_disable();
63 __inet6_hash(&dccp_hashinfo, sk);
64 local_bh_enable();
65 }
66}
67
68static inline u16 dccp_v6_check(struct dccp_hdr *dh, int len,
Arnaldo Carvalho de Melo45329e72006-03-20 22:01:29 -080069 struct in6_addr *saddr,
70 struct in6_addr *daddr,
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -080071 unsigned long base)
72{
73 return csum_ipv6_magic(saddr, daddr, len, IPPROTO_DCCP, base);
74}
75
76static __u32 dccp_v6_init_sequence(struct sock *sk, struct sk_buff *skb)
77{
78 const struct dccp_hdr *dh = dccp_hdr(skb);
79
80 if (skb->protocol == htons(ETH_P_IPV6))
81 return secure_tcpv6_sequence_number(skb->nh.ipv6h->daddr.s6_addr32,
82 skb->nh.ipv6h->saddr.s6_addr32,
83 dh->dccph_dport,
84 dh->dccph_sport);
Arnaldo Carvalho de Melo45329e72006-03-20 22:01:29 -080085
86 return secure_dccp_sequence_number(skb->nh.iph->daddr,
87 skb->nh.iph->saddr,
88 dh->dccph_dport,
89 dh->dccph_sport);
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -080090}
91
Arnaldo Carvalho de Melo45329e72006-03-20 22:01:29 -080092static int dccp_v6_connect(struct sock *sk, struct sockaddr *uaddr,
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -080093 int addr_len)
94{
Arnaldo Carvalho de Melo45329e72006-03-20 22:01:29 -080095 struct sockaddr_in6 *usin = (struct sockaddr_in6 *)uaddr;
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -080096 struct inet_connection_sock *icsk = inet_csk(sk);
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -080097 struct inet_sock *inet = inet_sk(sk);
98 struct ipv6_pinfo *np = inet6_sk(sk);
99 struct dccp_sock *dp = dccp_sk(sk);
100 struct in6_addr *saddr = NULL, *final_p = NULL, final;
101 struct flowi fl;
102 struct dst_entry *dst;
103 int addr_type;
104 int err;
105
106 dp->dccps_role = DCCP_ROLE_CLIENT;
107
Arnaldo Carvalho de Melo45329e72006-03-20 22:01:29 -0800108 if (addr_len < SIN6_LEN_RFC2133)
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -0800109 return -EINVAL;
110
Arnaldo Carvalho de Melo45329e72006-03-20 22:01:29 -0800111 if (usin->sin6_family != AF_INET6)
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -0800112 return -EAFNOSUPPORT;
113
114 memset(&fl, 0, sizeof(fl));
115
116 if (np->sndflow) {
117 fl.fl6_flowlabel = usin->sin6_flowinfo & IPV6_FLOWINFO_MASK;
118 IP6_ECN_flow_init(fl.fl6_flowlabel);
119 if (fl.fl6_flowlabel & IPV6_FLOWLABEL_MASK) {
120 struct ip6_flowlabel *flowlabel;
121 flowlabel = fl6_sock_lookup(sk, fl.fl6_flowlabel);
122 if (flowlabel == NULL)
123 return -EINVAL;
124 ipv6_addr_copy(&usin->sin6_addr, &flowlabel->dst);
125 fl6_sock_release(flowlabel);
126 }
127 }
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -0800128 /*
Arnaldo Carvalho de Melo45329e72006-03-20 22:01:29 -0800129 * connect() to INADDR_ANY means loopback (BSD'ism).
130 */
131 if (ipv6_addr_any(&usin->sin6_addr))
132 usin->sin6_addr.s6_addr[15] = 1;
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -0800133
134 addr_type = ipv6_addr_type(&usin->sin6_addr);
135
Arnaldo Carvalho de Melo45329e72006-03-20 22:01:29 -0800136 if (addr_type & IPV6_ADDR_MULTICAST)
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -0800137 return -ENETUNREACH;
138
139 if (addr_type & IPV6_ADDR_LINKLOCAL) {
140 if (addr_len >= sizeof(struct sockaddr_in6) &&
141 usin->sin6_scope_id) {
142 /* If interface is set while binding, indices
143 * must coincide.
144 */
145 if (sk->sk_bound_dev_if &&
146 sk->sk_bound_dev_if != usin->sin6_scope_id)
147 return -EINVAL;
148
149 sk->sk_bound_dev_if = usin->sin6_scope_id;
150 }
151
152 /* Connect to link-local address requires an interface */
153 if (!sk->sk_bound_dev_if)
154 return -EINVAL;
155 }
156
157 ipv6_addr_copy(&np->daddr, &usin->sin6_addr);
158 np->flow_label = fl.fl6_flowlabel;
159
160 /*
Arnaldo Carvalho de Melo45329e72006-03-20 22:01:29 -0800161 * DCCP over IPv4
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -0800162 */
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -0800163 if (addr_type == IPV6_ADDR_MAPPED) {
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800164 u32 exthdrlen = icsk->icsk_ext_hdr_len;
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -0800165 struct sockaddr_in sin;
166
167 SOCK_DEBUG(sk, "connect: ipv4 mapped\n");
168
169 if (__ipv6_only_sock(sk))
170 return -ENETUNREACH;
171
172 sin.sin_family = AF_INET;
173 sin.sin_port = usin->sin6_port;
174 sin.sin_addr.s_addr = usin->sin6_addr.s6_addr32[3];
175
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800176 icsk->icsk_af_ops = &dccp_ipv6_mapped;
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -0800177 sk->sk_backlog_rcv = dccp_v4_do_rcv;
178
179 err = dccp_v4_connect(sk, (struct sockaddr *)&sin, sizeof(sin));
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -0800180 if (err) {
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800181 icsk->icsk_ext_hdr_len = exthdrlen;
182 icsk->icsk_af_ops = &dccp_ipv6_af_ops;
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -0800183 sk->sk_backlog_rcv = dccp_v6_do_rcv;
184 goto failure;
185 } else {
186 ipv6_addr_set(&np->saddr, 0, 0, htonl(0x0000FFFF),
187 inet->saddr);
188 ipv6_addr_set(&np->rcv_saddr, 0, 0, htonl(0x0000FFFF),
189 inet->rcv_saddr);
190 }
191
192 return err;
193 }
194
195 if (!ipv6_addr_any(&np->rcv_saddr))
196 saddr = &np->rcv_saddr;
197
198 fl.proto = IPPROTO_DCCP;
199 ipv6_addr_copy(&fl.fl6_dst, &np->daddr);
200 ipv6_addr_copy(&fl.fl6_src, saddr ? saddr : &np->saddr);
201 fl.oif = sk->sk_bound_dev_if;
202 fl.fl_ip_dport = usin->sin6_port;
203 fl.fl_ip_sport = inet->sport;
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -0700204 security_sk_classify_flow(sk, &fl);
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -0800205
Arnaldo Carvalho de Melo45329e72006-03-20 22:01:29 -0800206 if (np->opt != NULL && np->opt->srcrt != NULL) {
207 const struct rt0_hdr *rt0 = (struct rt0_hdr *)np->opt->srcrt;
208
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -0800209 ipv6_addr_copy(&final, &fl.fl6_dst);
210 ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
211 final_p = &final;
212 }
213
214 err = ip6_dst_lookup(sk, &dst, &fl);
215 if (err)
216 goto failure;
Arnaldo Carvalho de Melo45329e72006-03-20 22:01:29 -0800217
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -0800218 if (final_p)
219 ipv6_addr_copy(&fl.fl6_dst, final_p);
220
Arnaldo Carvalho de Melo45329e72006-03-20 22:01:29 -0800221 err = xfrm_lookup(&dst, &fl, sk, 0);
222 if (err < 0)
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -0800223 goto failure;
224
225 if (saddr == NULL) {
226 saddr = &fl.fl6_src;
227 ipv6_addr_copy(&np->rcv_saddr, saddr);
228 }
229
230 /* set the source address */
231 ipv6_addr_copy(&np->saddr, saddr);
232 inet->rcv_saddr = LOOPBACK4_IPV6;
233
YOSHIFUJI Hideaki8e1ef0a2006-08-29 17:15:09 -0700234 __ip6_dst_store(sk, dst, NULL, NULL);
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -0800235
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800236 icsk->icsk_ext_hdr_len = 0;
Arnaldo Carvalho de Melo45329e72006-03-20 22:01:29 -0800237 if (np->opt != NULL)
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800238 icsk->icsk_ext_hdr_len = (np->opt->opt_flen +
239 np->opt->opt_nflen);
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -0800240
241 inet->dport = usin->sin6_port;
242
243 dccp_set_state(sk, DCCP_REQUESTING);
Arnaldo Carvalho de Melod8313f52005-12-13 23:25:44 -0800244 err = inet6_hash_connect(&dccp_death_row, sk);
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -0800245 if (err)
246 goto late_failure;
247 /* FIXME */
248#if 0
249 dp->dccps_gar = secure_dccp_v6_sequence_number(np->saddr.s6_addr32,
250 np->daddr.s6_addr32,
251 inet->sport,
252 inet->dport);
253#endif
254 err = dccp_connect(sk);
255 if (err)
256 goto late_failure;
257
258 return 0;
259
260late_failure:
261 dccp_set_state(sk, DCCP_CLOSED);
262 __sk_dst_reset(sk);
263failure:
264 inet->dport = 0;
265 sk->sk_route_caps = 0;
266 return err;
267}
268
269static void dccp_v6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
Andrea Bittau60fe62e2006-03-20 19:23:32 -0800270 int type, int code, int offset, __be32 info)
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -0800271{
272 struct ipv6hdr *hdr = (struct ipv6hdr *)skb->data;
273 const struct dccp_hdr *dh = (struct dccp_hdr *)(skb->data + offset);
274 struct ipv6_pinfo *np;
275 struct sock *sk;
276 int err;
277 __u64 seq;
278
279 sk = inet6_lookup(&dccp_hashinfo, &hdr->daddr, dh->dccph_dport,
280 &hdr->saddr, dh->dccph_sport, skb->dev->ifindex);
281
282 if (sk == NULL) {
283 ICMP6_INC_STATS_BH(__in6_dev_get(skb->dev), ICMP6_MIB_INERRORS);
284 return;
285 }
286
287 if (sk->sk_state == DCCP_TIME_WAIT) {
288 inet_twsk_put((struct inet_timewait_sock *)sk);
289 return;
290 }
291
292 bh_lock_sock(sk);
293 if (sock_owned_by_user(sk))
294 NET_INC_STATS_BH(LINUX_MIB_LOCKDROPPEDICMPS);
295
296 if (sk->sk_state == DCCP_CLOSED)
297 goto out;
298
299 np = inet6_sk(sk);
300
301 if (type == ICMPV6_PKT_TOOBIG) {
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -0800302 struct dst_entry *dst = NULL;
303
304 if (sock_owned_by_user(sk))
305 goto out;
306 if ((1 << sk->sk_state) & (DCCPF_LISTEN | DCCPF_CLOSED))
307 goto out;
308
309 /* icmp should have updated the destination cache entry */
310 dst = __sk_dst_check(sk, np->dst_cookie);
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -0800311 if (dst == NULL) {
312 struct inet_sock *inet = inet_sk(sk);
313 struct flowi fl;
314
315 /* BUGGG_FUTURE: Again, it is not clear how
316 to handle rthdr case. Ignore this complexity
317 for now.
318 */
319 memset(&fl, 0, sizeof(fl));
320 fl.proto = IPPROTO_DCCP;
321 ipv6_addr_copy(&fl.fl6_dst, &np->daddr);
322 ipv6_addr_copy(&fl.fl6_src, &np->saddr);
323 fl.oif = sk->sk_bound_dev_if;
324 fl.fl_ip_dport = inet->dport;
325 fl.fl_ip_sport = inet->sport;
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -0700326 security_sk_classify_flow(sk, &fl);
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -0800327
Arnaldo Carvalho de Melo45329e72006-03-20 22:01:29 -0800328 err = ip6_dst_lookup(sk, &dst, &fl);
329 if (err) {
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -0800330 sk->sk_err_soft = -err;
331 goto out;
332 }
333
Arnaldo Carvalho de Melo45329e72006-03-20 22:01:29 -0800334 err = xfrm_lookup(&dst, &fl, sk, 0);
335 if (err < 0) {
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -0800336 sk->sk_err_soft = -err;
337 goto out;
338 }
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -0800339 } else
340 dst_hold(dst);
341
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800342 if (inet_csk(sk)->icsk_pmtu_cookie > dst_mtu(dst)) {
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -0800343 dccp_sync_mss(sk, dst_mtu(dst));
344 } /* else let the usual retransmit timer handle it */
345 dst_release(dst);
346 goto out;
347 }
348
349 icmpv6_err_convert(type, code, &err);
350
351 seq = DCCP_SKB_CB(skb)->dccpd_seq;
352 /* Might be for an request_sock */
353 switch (sk->sk_state) {
354 struct request_sock *req, **prev;
355 case DCCP_LISTEN:
356 if (sock_owned_by_user(sk))
357 goto out;
358
359 req = inet6_csk_search_req(sk, &prev, dh->dccph_dport,
360 &hdr->daddr, &hdr->saddr,
361 inet6_iif(skb));
Arnaldo Carvalho de Melo45329e72006-03-20 22:01:29 -0800362 if (req == NULL)
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -0800363 goto out;
364
Arnaldo Carvalho de Melo45329e72006-03-20 22:01:29 -0800365 /*
366 * ICMPs are not backlogged, hence we cannot get an established
367 * socket here.
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -0800368 */
369 BUG_TRAP(req->sk == NULL);
370
371 if (seq != dccp_rsk(req)->dreq_iss) {
372 NET_INC_STATS_BH(LINUX_MIB_OUTOFWINDOWICMPS);
373 goto out;
374 }
375
376 inet_csk_reqsk_queue_drop(sk, req, prev);
377 goto out;
378
379 case DCCP_REQUESTING:
380 case DCCP_RESPOND: /* Cannot happen.
Arnaldo Carvalho de Melo45329e72006-03-20 22:01:29 -0800381 It can, it SYNs are crossed. --ANK */
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -0800382 if (!sock_owned_by_user(sk)) {
383 DCCP_INC_STATS_BH(DCCP_MIB_ATTEMPTFAILS);
384 sk->sk_err = err;
385 /*
386 * Wake people up to see the error
387 * (see connect in sock.c)
388 */
389 sk->sk_error_report(sk);
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -0800390 dccp_done(sk);
391 } else
392 sk->sk_err_soft = err;
393 goto out;
394 }
395
396 if (!sock_owned_by_user(sk) && np->recverr) {
397 sk->sk_err = err;
398 sk->sk_error_report(sk);
399 } else
400 sk->sk_err_soft = err;
401
402out:
403 bh_unlock_sock(sk);
404 sock_put(sk);
405}
406
407
408static int dccp_v6_send_response(struct sock *sk, struct request_sock *req,
409 struct dst_entry *dst)
410{
411 struct inet6_request_sock *ireq6 = inet6_rsk(req);
412 struct ipv6_pinfo *np = inet6_sk(sk);
413 struct sk_buff *skb;
414 struct ipv6_txoptions *opt = NULL;
415 struct in6_addr *final_p = NULL, final;
416 struct flowi fl;
417 int err = -1;
418
419 memset(&fl, 0, sizeof(fl));
420 fl.proto = IPPROTO_DCCP;
421 ipv6_addr_copy(&fl.fl6_dst, &ireq6->rmt_addr);
422 ipv6_addr_copy(&fl.fl6_src, &ireq6->loc_addr);
423 fl.fl6_flowlabel = 0;
424 fl.oif = ireq6->iif;
425 fl.fl_ip_dport = inet_rsk(req)->rmt_port;
426 fl.fl_ip_sport = inet_sk(sk)->sport;
Venkat Yekkirala4237c752006-07-24 23:32:50 -0700427 security_req_classify_flow(req, &fl);
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -0800428
429 if (dst == NULL) {
430 opt = np->opt;
431 if (opt == NULL &&
432 np->rxopt.bits.osrcrt == 2 &&
433 ireq6->pktopts) {
434 struct sk_buff *pktopts = ireq6->pktopts;
435 struct inet6_skb_parm *rxopt = IP6CB(pktopts);
Arnaldo Carvalho de Melo45329e72006-03-20 22:01:29 -0800436
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -0800437 if (rxopt->srcrt)
438 opt = ipv6_invert_rthdr(sk,
439 (struct ipv6_rt_hdr *)(pktopts->nh.raw +
440 rxopt->srcrt));
441 }
442
Arnaldo Carvalho de Melo45329e72006-03-20 22:01:29 -0800443 if (opt != NULL && opt->srcrt != NULL) {
444 const struct rt0_hdr *rt0 = (struct rt0_hdr *)opt->srcrt;
445
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -0800446 ipv6_addr_copy(&final, &fl.fl6_dst);
447 ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
448 final_p = &final;
449 }
450
451 err = ip6_dst_lookup(sk, &dst, &fl);
452 if (err)
453 goto done;
Arnaldo Carvalho de Melo45329e72006-03-20 22:01:29 -0800454
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -0800455 if (final_p)
456 ipv6_addr_copy(&fl.fl6_dst, final_p);
Arnaldo Carvalho de Melo45329e72006-03-20 22:01:29 -0800457
458 err = xfrm_lookup(&dst, &fl, sk, 0);
459 if (err < 0)
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -0800460 goto done;
461 }
462
463 skb = dccp_make_response(sk, dst, req);
464 if (skb != NULL) {
465 struct dccp_hdr *dh = dccp_hdr(skb);
Arnaldo Carvalho de Melo45329e72006-03-20 22:01:29 -0800466
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -0800467 dh->dccph_checksum = dccp_v6_check(dh, skb->len,
468 &ireq6->loc_addr,
469 &ireq6->rmt_addr,
470 csum_partial((char *)dh,
471 skb->len,
472 skb->csum));
473 ipv6_addr_copy(&fl.fl6_dst, &ireq6->rmt_addr);
474 err = ip6_xmit(sk, skb, &fl, opt, 0);
475 if (err == NET_XMIT_CN)
476 err = 0;
477 }
478
479done:
Arnaldo Carvalho de Melo45329e72006-03-20 22:01:29 -0800480 if (opt != NULL && opt != np->opt)
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -0800481 sock_kfree_s(sk, opt, opt->tot_len);
David S. Miller0cbd7822006-01-31 17:53:37 -0800482 dst_release(dst);
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -0800483 return err;
484}
485
486static void dccp_v6_reqsk_destructor(struct request_sock *req)
487{
488 if (inet6_rsk(req)->pktopts != NULL)
489 kfree_skb(inet6_rsk(req)->pktopts);
490}
491
492static struct request_sock_ops dccp6_request_sock_ops = {
493 .family = AF_INET6,
494 .obj_size = sizeof(struct dccp6_request_sock),
495 .rtx_syn_ack = dccp_v6_send_response,
496 .send_ack = dccp_v6_reqsk_send_ack,
497 .destructor = dccp_v6_reqsk_destructor,
498 .send_reset = dccp_v6_ctl_send_reset,
499};
500
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -0800501static struct timewait_sock_ops dccp6_timewait_sock_ops = {
502 .twsk_obj_size = sizeof(struct dccp6_timewait_sock),
503};
504
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -0800505static void dccp_v6_send_check(struct sock *sk, int len, struct sk_buff *skb)
506{
507 struct ipv6_pinfo *np = inet6_sk(sk);
508 struct dccp_hdr *dh = dccp_hdr(skb);
509
510 dh->dccph_checksum = csum_ipv6_magic(&np->saddr, &np->daddr,
Arnaldo Carvalho de Melo45329e72006-03-20 22:01:29 -0800511 len, IPPROTO_DCCP,
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -0800512 csum_partial((char *)dh,
513 dh->dccph_doff << 2,
514 skb->csum));
515}
516
517static void dccp_v6_ctl_send_reset(struct sk_buff *rxskb)
518{
Arnaldo Carvalho de Melo45329e72006-03-20 22:01:29 -0800519 struct dccp_hdr *rxdh = dccp_hdr(rxskb), *dh;
Arnaldo Carvalho de Melo118b2c92006-03-20 22:31:09 -0800520 const u32 dccp_hdr_reset_len = sizeof(struct dccp_hdr) +
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -0800521 sizeof(struct dccp_hdr_ext) +
522 sizeof(struct dccp_hdr_reset);
523 struct sk_buff *skb;
524 struct flowi fl;
525 u64 seqno;
526
527 if (rxdh->dccph_type == DCCP_PKT_RESET)
528 return;
529
530 if (!ipv6_unicast_destination(rxskb))
Arnaldo Carvalho de Melo45329e72006-03-20 22:01:29 -0800531 return;
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -0800532
Arnaldo Carvalho de Melo118b2c92006-03-20 22:31:09 -0800533 skb = alloc_skb(dccp_v6_ctl_socket->sk->sk_prot->max_header,
534 GFP_ATOMIC);
Arnaldo Carvalho de Melo45329e72006-03-20 22:01:29 -0800535 if (skb == NULL)
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -0800536 return;
537
Arnaldo Carvalho de Melo118b2c92006-03-20 22:31:09 -0800538 skb_reserve(skb, dccp_v6_ctl_socket->sk->sk_prot->max_header);
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -0800539
540 skb->h.raw = skb_push(skb, dccp_hdr_reset_len);
541 dh = dccp_hdr(skb);
542 memset(dh, 0, dccp_hdr_reset_len);
543
544 /* Swap the send and the receive. */
545 dh->dccph_type = DCCP_PKT_RESET;
546 dh->dccph_sport = rxdh->dccph_dport;
547 dh->dccph_dport = rxdh->dccph_sport;
548 dh->dccph_doff = dccp_hdr_reset_len / 4;
549 dh->dccph_x = 1;
550 dccp_hdr_reset(skb)->dccph_reset_code =
551 DCCP_SKB_CB(rxskb)->dccpd_reset_code;
552
553 /* See "8.3.1. Abnormal Termination" in draft-ietf-dccp-spec-11 */
554 seqno = 0;
555 if (DCCP_SKB_CB(rxskb)->dccpd_ack_seq != DCCP_PKT_WITHOUT_ACK_SEQ)
556 dccp_set_seqno(&seqno, DCCP_SKB_CB(rxskb)->dccpd_ack_seq + 1);
557
558 dccp_hdr_set_seq(dh, seqno);
559 dccp_hdr_set_ack(dccp_hdr_ack_bits(skb),
560 DCCP_SKB_CB(rxskb)->dccpd_seq);
561
562 memset(&fl, 0, sizeof(fl));
563 ipv6_addr_copy(&fl.fl6_dst, &rxskb->nh.ipv6h->saddr);
564 ipv6_addr_copy(&fl.fl6_src, &rxskb->nh.ipv6h->daddr);
565 dh->dccph_checksum = csum_ipv6_magic(&fl.fl6_src, &fl.fl6_dst,
566 sizeof(*dh), IPPROTO_DCCP,
567 skb->csum);
568 fl.proto = IPPROTO_DCCP;
569 fl.oif = inet6_iif(rxskb);
570 fl.fl_ip_dport = dh->dccph_dport;
571 fl.fl_ip_sport = dh->dccph_sport;
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -0700572 security_skb_classify_flow(rxskb, &fl);
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -0800573
574 /* sk = NULL, but it is safe for now. RST socket required. */
575 if (!ip6_dst_lookup(NULL, &skb->dst, &fl)) {
576 if (xfrm_lookup(&skb->dst, &fl, NULL, 0) >= 0) {
Arnaldo Carvalho de Melo72478872006-03-20 22:00:37 -0800577 ip6_xmit(dccp_v6_ctl_socket->sk, skb, &fl, NULL, 0);
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -0800578 DCCP_INC_STATS_BH(DCCP_MIB_OUTSEGS);
579 DCCP_INC_STATS_BH(DCCP_MIB_OUTRSTS);
580 return;
581 }
582 }
583
584 kfree_skb(skb);
585}
586
Arnaldo Carvalho de Meloc5fed152006-03-20 22:31:26 -0800587static void dccp_v6_reqsk_send_ack(struct sk_buff *rxskb,
588 struct request_sock *req)
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -0800589{
590 struct flowi fl;
591 struct dccp_hdr *rxdh = dccp_hdr(rxskb), *dh;
Arnaldo Carvalho de Melo118b2c92006-03-20 22:31:09 -0800592 const u32 dccp_hdr_ack_len = sizeof(struct dccp_hdr) +
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -0800593 sizeof(struct dccp_hdr_ext) +
594 sizeof(struct dccp_hdr_ack_bits);
595 struct sk_buff *skb;
596
Arnaldo Carvalho de Melo118b2c92006-03-20 22:31:09 -0800597 skb = alloc_skb(dccp_v6_ctl_socket->sk->sk_prot->max_header,
598 GFP_ATOMIC);
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -0800599 if (skb == NULL)
600 return;
601
Arnaldo Carvalho de Melo118b2c92006-03-20 22:31:09 -0800602 skb_reserve(skb, dccp_v6_ctl_socket->sk->sk_prot->max_header);
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -0800603
604 skb->h.raw = skb_push(skb, dccp_hdr_ack_len);
605 dh = dccp_hdr(skb);
606 memset(dh, 0, dccp_hdr_ack_len);
607
608 /* Build DCCP header and checksum it. */
609 dh->dccph_type = DCCP_PKT_ACK;
610 dh->dccph_sport = rxdh->dccph_dport;
611 dh->dccph_dport = rxdh->dccph_sport;
612 dh->dccph_doff = dccp_hdr_ack_len / 4;
613 dh->dccph_x = 1;
Arnaldo Carvalho de Melo45329e72006-03-20 22:01:29 -0800614
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -0800615 dccp_hdr_set_seq(dh, DCCP_SKB_CB(rxskb)->dccpd_ack_seq);
616 dccp_hdr_set_ack(dccp_hdr_ack_bits(skb),
617 DCCP_SKB_CB(rxskb)->dccpd_seq);
618
619 memset(&fl, 0, sizeof(fl));
620 ipv6_addr_copy(&fl.fl6_dst, &rxskb->nh.ipv6h->saddr);
621 ipv6_addr_copy(&fl.fl6_src, &rxskb->nh.ipv6h->daddr);
622
623 /* FIXME: calculate checksum, IPv4 also should... */
624
625 fl.proto = IPPROTO_DCCP;
626 fl.oif = inet6_iif(rxskb);
627 fl.fl_ip_dport = dh->dccph_dport;
628 fl.fl_ip_sport = dh->dccph_sport;
Venkat Yekkirala4237c752006-07-24 23:32:50 -0700629 security_req_classify_flow(req, &fl);
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -0800630
631 if (!ip6_dst_lookup(NULL, &skb->dst, &fl)) {
632 if (xfrm_lookup(&skb->dst, &fl, NULL, 0) >= 0) {
Arnaldo Carvalho de Melo72478872006-03-20 22:00:37 -0800633 ip6_xmit(dccp_v6_ctl_socket->sk, skb, &fl, NULL, 0);
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -0800634 DCCP_INC_STATS_BH(DCCP_MIB_OUTSEGS);
635 return;
636 }
637 }
638
639 kfree_skb(skb);
640}
641
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -0800642static struct sock *dccp_v6_hnd_req(struct sock *sk,struct sk_buff *skb)
643{
644 const struct dccp_hdr *dh = dccp_hdr(skb);
645 const struct ipv6hdr *iph = skb->nh.ipv6h;
646 struct sock *nsk;
647 struct request_sock **prev;
648 /* Find possible connection requests. */
649 struct request_sock *req = inet6_csk_search_req(sk, &prev,
650 dh->dccph_sport,
651 &iph->saddr,
652 &iph->daddr,
653 inet6_iif(skb));
654 if (req != NULL)
655 return dccp_check_req(sk, skb, req, prev);
656
657 nsk = __inet6_lookup_established(&dccp_hashinfo,
658 &iph->saddr, dh->dccph_sport,
659 &iph->daddr, ntohs(dh->dccph_dport),
660 inet6_iif(skb));
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -0800661 if (nsk != NULL) {
662 if (nsk->sk_state != DCCP_TIME_WAIT) {
663 bh_lock_sock(nsk);
664 return nsk;
665 }
666 inet_twsk_put((struct inet_timewait_sock *)nsk);
667 return NULL;
668 }
669
670 return sk;
671}
672
673static int dccp_v6_conn_request(struct sock *sk, struct sk_buff *skb)
674{
675 struct inet_request_sock *ireq;
676 struct dccp_sock dp;
677 struct request_sock *req;
678 struct dccp_request_sock *dreq;
679 struct inet6_request_sock *ireq6;
680 struct ipv6_pinfo *np = inet6_sk(sk);
Andrea Bittau60fe62e2006-03-20 19:23:32 -0800681 const __be32 service = dccp_hdr_request(skb)->dccph_req_service;
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -0800682 struct dccp_skb_cb *dcb = DCCP_SKB_CB(skb);
683 __u8 reset_code = DCCP_RESET_CODE_TOO_BUSY;
684
685 if (skb->protocol == htons(ETH_P_IP))
686 return dccp_v4_conn_request(sk, skb);
687
688 if (!ipv6_unicast_destination(skb))
Arnaldo Carvalho de Melo45329e72006-03-20 22:01:29 -0800689 goto drop;
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -0800690
691 if (dccp_bad_service_code(sk, service)) {
692 reset_code = DCCP_RESET_CODE_BAD_SERVICE_CODE;
693 goto drop;
694 }
695 /*
Arnaldo Carvalho de Melo45329e72006-03-20 22:01:29 -0800696 * There are no SYN attacks on IPv6, yet...
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -0800697 */
698 if (inet_csk_reqsk_queue_is_full(sk))
Arnaldo Carvalho de Melo45329e72006-03-20 22:01:29 -0800699 goto drop;
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -0800700
701 if (sk_acceptq_is_full(sk) && inet_csk_reqsk_queue_young(sk) > 1)
702 goto drop;
703
704 req = inet6_reqsk_alloc(sk->sk_prot->rsk_prot);
705 if (req == NULL)
706 goto drop;
707
708 /* FIXME: process options */
709
710 dccp_openreq_init(req, &dp, skb);
711
Venkat Yekkirala4237c752006-07-24 23:32:50 -0700712 if (security_inet_conn_request(sk, skb, req))
713 goto drop_and_free;
714
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -0800715 ireq6 = inet6_rsk(req);
716 ireq = inet_rsk(req);
717 ipv6_addr_copy(&ireq6->rmt_addr, &skb->nh.ipv6h->saddr);
718 ipv6_addr_copy(&ireq6->loc_addr, &skb->nh.ipv6h->daddr);
Ian McDonald4b79f0a2006-07-23 23:33:28 -0700719 req->rcv_wnd = dccp_feat_default_sequence_window;
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -0800720 ireq6->pktopts = NULL;
721
722 if (ipv6_opt_accepted(sk, skb) ||
723 np->rxopt.bits.rxinfo || np->rxopt.bits.rxoinfo ||
724 np->rxopt.bits.rxhlim || np->rxopt.bits.rxohlim) {
725 atomic_inc(&skb->users);
726 ireq6->pktopts = skb;
727 }
728 ireq6->iif = sk->sk_bound_dev_if;
729
730 /* So that link locals have meaning */
731 if (!sk->sk_bound_dev_if &&
732 ipv6_addr_type(&ireq6->rmt_addr) & IPV6_ADDR_LINKLOCAL)
733 ireq6->iif = inet6_iif(skb);
734
Arnaldo Carvalho de Melo45329e72006-03-20 22:01:29 -0800735 /*
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -0800736 * Step 3: Process LISTEN state
737 *
738 * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init Cookie
739 *
740 * In fact we defer setting S.GSR, S.SWL, S.SWH to
741 * dccp_create_openreq_child.
742 */
743 dreq = dccp_rsk(req);
744 dreq->dreq_isr = dcb->dccpd_seq;
745 dreq->dreq_iss = dccp_v6_init_sequence(sk, skb);
746 dreq->dreq_service = service;
747
748 if (dccp_v6_send_response(sk, req, NULL))
749 goto drop_and_free;
750
751 inet6_csk_reqsk_queue_hash_add(sk, req, DCCP_TIMEOUT_INIT);
752 return 0;
753
754drop_and_free:
755 reqsk_free(req);
756drop:
757 DCCP_INC_STATS_BH(DCCP_MIB_ATTEMPTFAILS);
758 dcb->dccpd_reset_code = reset_code;
759 return -1;
760}
761
762static struct sock *dccp_v6_request_recv_sock(struct sock *sk,
763 struct sk_buff *skb,
764 struct request_sock *req,
765 struct dst_entry *dst)
766{
767 struct inet6_request_sock *ireq6 = inet6_rsk(req);
768 struct ipv6_pinfo *newnp, *np = inet6_sk(sk);
769 struct inet_sock *newinet;
770 struct dccp_sock *newdp;
771 struct dccp6_sock *newdp6;
772 struct sock *newsk;
773 struct ipv6_txoptions *opt;
774
775 if (skb->protocol == htons(ETH_P_IP)) {
776 /*
777 * v6 mapped
778 */
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -0800779 newsk = dccp_v4_request_recv_sock(sk, skb, req, dst);
Arnaldo Carvalho de Melo45329e72006-03-20 22:01:29 -0800780 if (newsk == NULL)
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -0800781 return NULL;
782
783 newdp6 = (struct dccp6_sock *)newsk;
784 newdp = dccp_sk(newsk);
785 newinet = inet_sk(newsk);
786 newinet->pinet6 = &newdp6->inet6;
787 newnp = inet6_sk(newsk);
788
789 memcpy(newnp, np, sizeof(struct ipv6_pinfo));
790
791 ipv6_addr_set(&newnp->daddr, 0, 0, htonl(0x0000FFFF),
792 newinet->daddr);
793
794 ipv6_addr_set(&newnp->saddr, 0, 0, htonl(0x0000FFFF),
795 newinet->saddr);
796
797 ipv6_addr_copy(&newnp->rcv_saddr, &newnp->saddr);
798
799 inet_csk(newsk)->icsk_af_ops = &dccp_ipv6_mapped;
800 newsk->sk_backlog_rcv = dccp_v4_do_rcv;
801 newnp->pktoptions = NULL;
802 newnp->opt = NULL;
803 newnp->mcast_oif = inet6_iif(skb);
804 newnp->mcast_hops = skb->nh.ipv6h->hop_limit;
805
806 /*
807 * No need to charge this sock to the relevant IPv6 refcnt debug socks count
808 * here, dccp_create_openreq_child now does this for us, see the comment in
809 * that function for the gory details. -acme
810 */
811
812 /* It is tricky place. Until this moment IPv4 tcp
813 worked with IPv6 icsk.icsk_af_ops.
814 Sync it now.
815 */
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800816 dccp_sync_mss(newsk, inet_csk(newsk)->icsk_pmtu_cookie);
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -0800817
818 return newsk;
819 }
820
821 opt = np->opt;
822
823 if (sk_acceptq_is_full(sk))
824 goto out_overflow;
825
Arnaldo Carvalho de Melo45329e72006-03-20 22:01:29 -0800826 if (np->rxopt.bits.osrcrt == 2 && opt == NULL && ireq6->pktopts) {
827 const struct inet6_skb_parm *rxopt = IP6CB(ireq6->pktopts);
828
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -0800829 if (rxopt->srcrt)
830 opt = ipv6_invert_rthdr(sk,
831 (struct ipv6_rt_hdr *)(ireq6->pktopts->nh.raw +
832 rxopt->srcrt));
833 }
834
835 if (dst == NULL) {
836 struct in6_addr *final_p = NULL, final;
837 struct flowi fl;
838
839 memset(&fl, 0, sizeof(fl));
840 fl.proto = IPPROTO_DCCP;
841 ipv6_addr_copy(&fl.fl6_dst, &ireq6->rmt_addr);
Arnaldo Carvalho de Melo45329e72006-03-20 22:01:29 -0800842 if (opt != NULL && opt->srcrt != NULL) {
843 const struct rt0_hdr *rt0 = (struct rt0_hdr *)opt->srcrt;
844
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -0800845 ipv6_addr_copy(&final, &fl.fl6_dst);
846 ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
847 final_p = &final;
848 }
849 ipv6_addr_copy(&fl.fl6_src, &ireq6->loc_addr);
850 fl.oif = sk->sk_bound_dev_if;
851 fl.fl_ip_dport = inet_rsk(req)->rmt_port;
852 fl.fl_ip_sport = inet_sk(sk)->sport;
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -0700853 security_sk_classify_flow(sk, &fl);
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -0800854
855 if (ip6_dst_lookup(sk, &dst, &fl))
856 goto out;
857
858 if (final_p)
859 ipv6_addr_copy(&fl.fl6_dst, final_p);
860
861 if ((xfrm_lookup(&dst, &fl, sk, 0)) < 0)
862 goto out;
Arnaldo Carvalho de Melo45329e72006-03-20 22:01:29 -0800863 }
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -0800864
865 newsk = dccp_create_openreq_child(sk, req, skb);
866 if (newsk == NULL)
867 goto out;
868
869 /*
870 * No need to charge this sock to the relevant IPv6 refcnt debug socks
871 * count here, dccp_create_openreq_child now does this for us, see the
872 * comment in that function for the gory details. -acme
873 */
874
YOSHIFUJI Hideaki8e1ef0a2006-08-29 17:15:09 -0700875 __ip6_dst_store(newsk, dst, NULL, NULL);
Arnaldo Carvalho de Melo45329e72006-03-20 22:01:29 -0800876 newsk->sk_route_caps = dst->dev->features & ~(NETIF_F_IP_CSUM |
877 NETIF_F_TSO);
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -0800878 newdp6 = (struct dccp6_sock *)newsk;
879 newinet = inet_sk(newsk);
880 newinet->pinet6 = &newdp6->inet6;
881 newdp = dccp_sk(newsk);
882 newnp = inet6_sk(newsk);
883
884 memcpy(newnp, np, sizeof(struct ipv6_pinfo));
885
886 ipv6_addr_copy(&newnp->daddr, &ireq6->rmt_addr);
887 ipv6_addr_copy(&newnp->saddr, &ireq6->loc_addr);
888 ipv6_addr_copy(&newnp->rcv_saddr, &ireq6->loc_addr);
889 newsk->sk_bound_dev_if = ireq6->iif;
890
Arnaldo Carvalho de Melo45329e72006-03-20 22:01:29 -0800891 /* Now IPv6 options...
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -0800892
893 First: no IPv4 options.
894 */
895 newinet->opt = NULL;
896
897 /* Clone RX bits */
898 newnp->rxopt.all = np->rxopt.all;
899
900 /* Clone pktoptions received with SYN */
901 newnp->pktoptions = NULL;
902 if (ireq6->pktopts != NULL) {
903 newnp->pktoptions = skb_clone(ireq6->pktopts, GFP_ATOMIC);
904 kfree_skb(ireq6->pktopts);
905 ireq6->pktopts = NULL;
906 if (newnp->pktoptions)
907 skb_set_owner_r(newnp->pktoptions, newsk);
908 }
909 newnp->opt = NULL;
910 newnp->mcast_oif = inet6_iif(skb);
911 newnp->mcast_hops = skb->nh.ipv6h->hop_limit;
912
Arnaldo Carvalho de Melo45329e72006-03-20 22:01:29 -0800913 /*
914 * Clone native IPv6 options from listening socket (if any)
915 *
916 * Yes, keeping reference count would be much more clever, but we make
917 * one more one thing there: reattach optmem to newsk.
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -0800918 */
Arnaldo Carvalho de Melo45329e72006-03-20 22:01:29 -0800919 if (opt != NULL) {
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -0800920 newnp->opt = ipv6_dup_options(newsk, opt);
921 if (opt != np->opt)
922 sock_kfree_s(sk, opt, opt->tot_len);
923 }
924
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800925 inet_csk(newsk)->icsk_ext_hdr_len = 0;
Arnaldo Carvalho de Melo45329e72006-03-20 22:01:29 -0800926 if (newnp->opt != NULL)
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800927 inet_csk(newsk)->icsk_ext_hdr_len = (newnp->opt->opt_nflen +
928 newnp->opt->opt_flen);
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -0800929
930 dccp_sync_mss(newsk, dst_mtu(dst));
931
932 newinet->daddr = newinet->saddr = newinet->rcv_saddr = LOOPBACK4_IPV6;
933
934 __inet6_hash(&dccp_hashinfo, newsk);
935 inet_inherit_port(&dccp_hashinfo, sk, newsk);
936
937 return newsk;
938
939out_overflow:
940 NET_INC_STATS_BH(LINUX_MIB_LISTENOVERFLOWS);
941out:
942 NET_INC_STATS_BH(LINUX_MIB_LISTENDROPS);
Arnaldo Carvalho de Melo45329e72006-03-20 22:01:29 -0800943 if (opt != NULL && opt != np->opt)
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -0800944 sock_kfree_s(sk, opt, opt->tot_len);
945 dst_release(dst);
946 return NULL;
947}
948
949/* The socket must have it's spinlock held when we get
950 * here.
951 *
952 * We have a potential double-lock case here, so even when
953 * doing backlog processing we use the BH locking scheme.
954 * This is because we cannot sleep with the original spinlock
955 * held.
956 */
957static int dccp_v6_do_rcv(struct sock *sk, struct sk_buff *skb)
958{
959 struct ipv6_pinfo *np = inet6_sk(sk);
960 struct sk_buff *opt_skb = NULL;
961
962 /* Imagine: socket is IPv6. IPv4 packet arrives,
963 goes to IPv4 receive handler and backlogged.
964 From backlog it always goes here. Kerboom...
965 Fortunately, dccp_rcv_established and rcv_established
966 handle them correctly, but it is not case with
967 dccp_v6_hnd_req and dccp_v6_ctl_send_reset(). --ANK
968 */
969
970 if (skb->protocol == htons(ETH_P_IP))
971 return dccp_v4_do_rcv(sk, skb);
972
Dmitry Mishinfda9ef52006-08-31 15:28:39 -0700973 if (sk_filter(sk, skb))
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -0800974 goto discard;
975
976 /*
Arnaldo Carvalho de Melo45329e72006-03-20 22:01:29 -0800977 * socket locking is here for SMP purposes as backlog rcv is currently
978 * called with bh processing disabled.
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -0800979 */
980
981 /* Do Stevens' IPV6_PKTOPTIONS.
982
983 Yes, guys, it is the only place in our code, where we
984 may make it not affecting IPv4.
985 The rest of code is protocol independent,
986 and I do not like idea to uglify IPv4.
987
988 Actually, all the idea behind IPV6_PKTOPTIONS
989 looks not very well thought. For now we latch
990 options, received in the last packet, enqueued
991 by tcp. Feel free to propose better solution.
992 --ANK (980728)
993 */
994 if (np->rxopt.all)
995 opt_skb = skb_clone(skb, GFP_ATOMIC);
996
997 if (sk->sk_state == DCCP_OPEN) { /* Fast path */
998 if (dccp_rcv_established(sk, skb, dccp_hdr(skb), skb->len))
999 goto reset;
1000 return 0;
1001 }
1002
Arnaldo Carvalho de Melo45329e72006-03-20 22:01:29 -08001003 if (sk->sk_state == DCCP_LISTEN) {
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -08001004 struct sock *nsk = dccp_v6_hnd_req(sk, skb);
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -08001005
Arnaldo Carvalho de Melo45329e72006-03-20 22:01:29 -08001006 if (nsk == NULL)
1007 goto discard;
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -08001008 /*
1009 * Queue it on the new socket if the new socket is active,
1010 * otherwise we just shortcircuit this and continue with
1011 * the new socket..
1012 */
Arnaldo Carvalho de Melo45329e72006-03-20 22:01:29 -08001013 if (nsk != sk) {
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -08001014 if (dccp_child_process(sk, nsk, skb))
1015 goto reset;
Arnaldo Carvalho de Melo45329e72006-03-20 22:01:29 -08001016 if (opt_skb != NULL)
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -08001017 __kfree_skb(opt_skb);
1018 return 0;
1019 }
1020 }
1021
1022 if (dccp_rcv_state_process(sk, skb, dccp_hdr(skb), skb->len))
1023 goto reset;
1024 return 0;
1025
1026reset:
1027 dccp_v6_ctl_send_reset(skb);
1028discard:
Arnaldo Carvalho de Melo45329e72006-03-20 22:01:29 -08001029 if (opt_skb != NULL)
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -08001030 __kfree_skb(opt_skb);
1031 kfree_skb(skb);
1032 return 0;
1033}
1034
Patrick McHardy951dbc82006-01-06 23:02:34 -08001035static int dccp_v6_rcv(struct sk_buff **pskb)
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -08001036{
1037 const struct dccp_hdr *dh;
1038 struct sk_buff *skb = *pskb;
1039 struct sock *sk;
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -08001040
1041 /* Step 1: Check header basics: */
1042
1043 if (dccp_invalid_packet(skb))
1044 goto discard_it;
1045
1046 dh = dccp_hdr(skb);
1047
1048 DCCP_SKB_CB(skb)->dccpd_seq = dccp_hdr_seq(skb);
1049 DCCP_SKB_CB(skb)->dccpd_type = dh->dccph_type;
1050
1051 if (dccp_packet_without_ack(skb))
1052 DCCP_SKB_CB(skb)->dccpd_ack_seq = DCCP_PKT_WITHOUT_ACK_SEQ;
1053 else
1054 DCCP_SKB_CB(skb)->dccpd_ack_seq = dccp_hdr_ack_seq(skb);
1055
1056 /* Step 2:
1057 * Look up flow ID in table and get corresponding socket */
1058 sk = __inet6_lookup(&dccp_hashinfo, &skb->nh.ipv6h->saddr,
1059 dh->dccph_sport,
1060 &skb->nh.ipv6h->daddr, ntohs(dh->dccph_dport),
1061 inet6_iif(skb));
Arnaldo Carvalho de Melo45329e72006-03-20 22:01:29 -08001062 /*
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -08001063 * Step 2:
1064 * If no socket ...
1065 * Generate Reset(No Connection) unless P.type == Reset
1066 * Drop packet and return
1067 */
1068 if (sk == NULL)
1069 goto no_dccp_socket;
1070
Arnaldo Carvalho de Melo45329e72006-03-20 22:01:29 -08001071 /*
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -08001072 * Step 2:
1073 * ... or S.state == TIMEWAIT,
1074 * Generate Reset(No Connection) unless P.type == Reset
1075 * Drop packet and return
1076 */
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -08001077 if (sk->sk_state == DCCP_TIME_WAIT)
Arnaldo Carvalho de Melo45329e72006-03-20 22:01:29 -08001078 goto do_time_wait;
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -08001079
1080 if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb))
1081 goto discard_and_relse;
1082
Arnaldo Carvalho de Melo25995ff2005-12-27 02:42:22 -02001083 return sk_receive_skb(sk, skb) ? -1 : 0;
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -08001084
1085no_dccp_socket:
1086 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
1087 goto discard_it;
1088 /*
1089 * Step 2:
1090 * Generate Reset(No Connection) unless P.type == Reset
1091 * Drop packet and return
1092 */
1093 if (dh->dccph_type != DCCP_PKT_RESET) {
1094 DCCP_SKB_CB(skb)->dccpd_reset_code =
1095 DCCP_RESET_CODE_NO_CONNECTION;
1096 dccp_v6_ctl_send_reset(skb);
1097 }
1098discard_it:
1099
1100 /*
1101 * Discard frame
1102 */
1103
1104 kfree_skb(skb);
1105 return 0;
1106
1107discard_and_relse:
1108 sock_put(sk);
1109 goto discard_it;
1110
1111do_time_wait:
1112 inet_twsk_put((struct inet_timewait_sock *)sk);
1113 goto no_dccp_socket;
1114}
1115
1116static struct inet_connection_sock_af_ops dccp_ipv6_af_ops = {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001117 .queue_xmit = inet6_csk_xmit,
1118 .send_check = dccp_v6_send_check,
1119 .rebuild_header = inet6_sk_rebuild_header,
1120 .conn_request = dccp_v6_conn_request,
1121 .syn_recv_sock = dccp_v6_request_recv_sock,
1122 .net_header_len = sizeof(struct ipv6hdr),
1123 .setsockopt = ipv6_setsockopt,
1124 .getsockopt = ipv6_getsockopt,
1125 .addr2sockaddr = inet6_csk_addr2sockaddr,
1126 .sockaddr_len = sizeof(struct sockaddr_in6),
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001127#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001128 .compat_setsockopt = compat_ipv6_setsockopt,
1129 .compat_getsockopt = compat_ipv6_getsockopt,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001130#endif
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -08001131};
1132
1133/*
1134 * DCCP over IPv4 via INET6 API
1135 */
1136static struct inet_connection_sock_af_ops dccp_ipv6_mapped = {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001137 .queue_xmit = ip_queue_xmit,
1138 .send_check = dccp_v4_send_check,
1139 .rebuild_header = inet_sk_rebuild_header,
1140 .conn_request = dccp_v6_conn_request,
1141 .syn_recv_sock = dccp_v6_request_recv_sock,
1142 .net_header_len = sizeof(struct iphdr),
1143 .setsockopt = ipv6_setsockopt,
1144 .getsockopt = ipv6_getsockopt,
1145 .addr2sockaddr = inet6_csk_addr2sockaddr,
1146 .sockaddr_len = sizeof(struct sockaddr_in6),
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001147#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001148 .compat_setsockopt = compat_ipv6_setsockopt,
1149 .compat_getsockopt = compat_ipv6_getsockopt,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001150#endif
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -08001151};
1152
1153/* NOTE: A lot of things set to zero explicitly by call to
1154 * sk_alloc() so need not be done here.
1155 */
1156static int dccp_v6_init_sock(struct sock *sk)
1157{
Arnaldo Carvalho de Melo72478872006-03-20 22:00:37 -08001158 static __u8 dccp_v6_ctl_sock_initialized;
1159 int err = dccp_init_sock(sk, dccp_v6_ctl_sock_initialized);
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -08001160
Arnaldo Carvalho de Melo72478872006-03-20 22:00:37 -08001161 if (err == 0) {
1162 if (unlikely(!dccp_v6_ctl_sock_initialized))
1163 dccp_v6_ctl_sock_initialized = 1;
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -08001164 inet_csk(sk)->icsk_af_ops = &dccp_ipv6_af_ops;
Arnaldo Carvalho de Melo72478872006-03-20 22:00:37 -08001165 }
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -08001166
1167 return err;
1168}
1169
1170static int dccp_v6_destroy_sock(struct sock *sk)
1171{
Arnaldo Carvalho de Melo3e0fadc2006-03-20 21:23:15 -08001172 dccp_destroy_sock(sk);
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -08001173 return inet6_destroy_sock(sk);
1174}
1175
1176static struct proto dccp_v6_prot = {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001177 .name = "DCCPv6",
1178 .owner = THIS_MODULE,
1179 .close = dccp_close,
1180 .connect = dccp_v6_connect,
1181 .disconnect = dccp_disconnect,
1182 .ioctl = dccp_ioctl,
1183 .init = dccp_v6_init_sock,
1184 .setsockopt = dccp_setsockopt,
1185 .getsockopt = dccp_getsockopt,
1186 .sendmsg = dccp_sendmsg,
1187 .recvmsg = dccp_recvmsg,
1188 .backlog_rcv = dccp_v6_do_rcv,
1189 .hash = dccp_v6_hash,
1190 .unhash = dccp_unhash,
1191 .accept = inet_csk_accept,
1192 .get_port = dccp_v6_get_port,
1193 .shutdown = dccp_shutdown,
1194 .destroy = dccp_v6_destroy_sock,
1195 .orphan_count = &dccp_orphan_count,
1196 .max_header = MAX_DCCP_HEADER,
1197 .obj_size = sizeof(struct dccp6_sock),
1198 .rsk_prot = &dccp6_request_sock_ops,
1199 .twsk_prot = &dccp6_timewait_sock_ops,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001200#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001201 .compat_setsockopt = compat_dccp_setsockopt,
1202 .compat_getsockopt = compat_dccp_getsockopt,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001203#endif
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -08001204};
1205
1206static struct inet6_protocol dccp_v6_protocol = {
Arnaldo Carvalho de Melo45329e72006-03-20 22:01:29 -08001207 .handler = dccp_v6_rcv,
1208 .err_handler = dccp_v6_err,
1209 .flags = INET6_PROTO_NOPOLICY | INET6_PROTO_FINAL,
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -08001210};
1211
1212static struct proto_ops inet6_dccp_ops = {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001213 .family = PF_INET6,
1214 .owner = THIS_MODULE,
1215 .release = inet6_release,
1216 .bind = inet6_bind,
1217 .connect = inet_stream_connect,
1218 .socketpair = sock_no_socketpair,
1219 .accept = inet_accept,
1220 .getname = inet6_getname,
1221 .poll = dccp_poll,
1222 .ioctl = inet6_ioctl,
1223 .listen = inet_dccp_listen,
1224 .shutdown = inet_shutdown,
1225 .setsockopt = sock_common_setsockopt,
1226 .getsockopt = sock_common_getsockopt,
1227 .sendmsg = inet_sendmsg,
1228 .recvmsg = sock_common_recvmsg,
1229 .mmap = sock_no_mmap,
1230 .sendpage = sock_no_sendpage,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001231#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001232 .compat_setsockopt = compat_sock_common_setsockopt,
1233 .compat_getsockopt = compat_sock_common_getsockopt,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001234#endif
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -08001235};
1236
1237static struct inet_protosw dccp_v6_protosw = {
1238 .type = SOCK_DCCP,
1239 .protocol = IPPROTO_DCCP,
1240 .prot = &dccp_v6_prot,
1241 .ops = &inet6_dccp_ops,
1242 .capability = -1,
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -08001243 .flags = INET_PROTOSW_ICSK,
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -08001244};
1245
1246static int __init dccp_v6_init(void)
1247{
1248 int err = proto_register(&dccp_v6_prot, 1);
1249
1250 if (err != 0)
1251 goto out;
1252
1253 err = inet6_add_protocol(&dccp_v6_protocol, IPPROTO_DCCP);
1254 if (err != 0)
1255 goto out_unregister_proto;
1256
1257 inet6_register_protosw(&dccp_v6_protosw);
Arnaldo Carvalho de Melo72478872006-03-20 22:00:37 -08001258
Arnaldo Carvalho de Meloc4d93902006-03-20 22:01:03 -08001259 err = inet_csk_ctl_sock_create(&dccp_v6_ctl_socket, PF_INET6,
1260 SOCK_DCCP, IPPROTO_DCCP);
1261 if (err != 0)
Arnaldo Carvalho de Melo72478872006-03-20 22:00:37 -08001262 goto out_unregister_protosw;
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -08001263out:
1264 return err;
Arnaldo Carvalho de Melo72478872006-03-20 22:00:37 -08001265out_unregister_protosw:
1266 inet6_del_protocol(&dccp_v6_protocol, IPPROTO_DCCP);
1267 inet6_unregister_protosw(&dccp_v6_protosw);
Arnaldo Carvalho de Melo3df80d92005-12-13 23:24:53 -08001268out_unregister_proto:
1269 proto_unregister(&dccp_v6_prot);
1270 goto out;
1271}
1272
1273static void __exit dccp_v6_exit(void)
1274{
1275 inet6_del_protocol(&dccp_v6_protocol, IPPROTO_DCCP);
1276 inet6_unregister_protosw(&dccp_v6_protosw);
1277 proto_unregister(&dccp_v6_prot);
1278}
1279
1280module_init(dccp_v6_init);
1281module_exit(dccp_v6_exit);
1282
1283/*
1284 * __stringify doesn't likes enums, so use SOCK_DCCP (6) and IPPROTO_DCCP (33)
1285 * values directly, Also cover the case where the protocol is not specified,
1286 * i.e. net-pf-PF_INET6-proto-0-type-SOCK_DCCP
1287 */
1288MODULE_ALIAS("net-pf-" __stringify(PF_INET6) "-proto-33-type-6");
1289MODULE_ALIAS("net-pf-" __stringify(PF_INET6) "-proto-0-type-6");
1290MODULE_LICENSE("GPL");
1291MODULE_AUTHOR("Arnaldo Carvalho de Melo <acme@mandriva.com>");
1292MODULE_DESCRIPTION("DCCPv6 - Datagram Congestion Controlled Protocol");