1 /*
2 * DCCP over IPv6
3 * Linux INET6 implementation
4 *
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
15 #include <linux/module.h>
16 #include <linux/random.h>
17 #include <linux/slab.h>
18 #include <linux/xfrm.h>
19
20 #include <net/addrconf.h>
21 #include <net/inet_common.h>
22 #include <net/inet_hashtables.h>
23 #include <net/inet_sock.h>
24 #include <net/inet6_connection_sock.h>
25 #include <net/inet6_hashtables.h>
26 #include <net/ip6_route.h>
27 #include <net/ipv6.h>
28 #include <net/protocol.h>
29 #include <net/transp_v6.h>
30 #include <net/ip6_checksum.h>
31 #include <net/xfrm.h>
32 #include <net/secure_seq.h>
33
34 #include "dccp.h"
35 #include "ipv6.h"
36 #include "feat.h"
37
38 /* The per-net dccp.v6_ctl_sk is used for sending RSTs and ACKs */
39
40 static const struct inet_connection_sock_af_ops dccp_ipv6_mapped;
41 static const struct inet_connection_sock_af_ops dccp_ipv6_af_ops;
42
dccp_v6_hash(struct sock * sk)43 static void dccp_v6_hash(struct sock *sk)
44 {
45 if (sk->sk_state != DCCP_CLOSED) {
46 if (inet_csk(sk)->icsk_af_ops == &dccp_ipv6_mapped) {
47 inet_hash(sk);
48 return;
49 }
50 local_bh_disable();
51 __inet6_hash(sk, NULL);
52 local_bh_enable();
53 }
54 }
55
56 /* add pseudo-header to DCCP checksum stored in skb->csum */
dccp_v6_csum_finish(struct sk_buff * skb,const struct in6_addr * saddr,const struct in6_addr * daddr)57 static inline __sum16 dccp_v6_csum_finish(struct sk_buff *skb,
58 const struct in6_addr *saddr,
59 const struct in6_addr *daddr)
60 {
61 return csum_ipv6_magic(saddr, daddr, skb->len, IPPROTO_DCCP, skb->csum);
62 }
63
dccp_v6_send_check(struct sock * sk,struct sk_buff * skb)64 static inline void dccp_v6_send_check(struct sock *sk, struct sk_buff *skb)
65 {
66 struct ipv6_pinfo *np = inet6_sk(sk);
67 struct dccp_hdr *dh = dccp_hdr(skb);
68
69 dccp_csum_outgoing(skb);
70 dh->dccph_checksum = dccp_v6_csum_finish(skb, &np->saddr, &np->daddr);
71 }
72
dccp_v6_init_sequence(struct sk_buff * skb)73 static inline __u64 dccp_v6_init_sequence(struct sk_buff *skb)
74 {
75 return secure_dccpv6_sequence_number(ipv6_hdr(skb)->daddr.s6_addr32,
76 ipv6_hdr(skb)->saddr.s6_addr32,
77 dccp_hdr(skb)->dccph_dport,
78 dccp_hdr(skb)->dccph_sport );
79
80 }
81
dccp_v6_err(struct sk_buff * skb,struct inet6_skb_parm * opt,u8 type,u8 code,int offset,__be32 info)82 static void dccp_v6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
83 u8 type, u8 code, int offset, __be32 info)
84 {
85 const struct ipv6hdr *hdr = (const struct ipv6hdr *)skb->data;
86 const struct dccp_hdr *dh = (struct dccp_hdr *)(skb->data + offset);
87 struct dccp_sock *dp;
88 struct ipv6_pinfo *np;
89 struct sock *sk;
90 int err;
91 __u64 seq;
92 struct net *net = dev_net(skb->dev);
93
94 if (skb->len < offset + sizeof(*dh) ||
95 skb->len < offset + __dccp_basic_hdr_len(dh)) {
96 ICMP6_INC_STATS_BH(net, __in6_dev_get(skb->dev),
97 ICMP6_MIB_INERRORS);
98 return;
99 }
100
101 sk = inet6_lookup(net, &dccp_hashinfo,
102 &hdr->daddr, dh->dccph_dport,
103 &hdr->saddr, dh->dccph_sport, inet6_iif(skb));
104
105 if (sk == NULL) {
106 ICMP6_INC_STATS_BH(net, __in6_dev_get(skb->dev),
107 ICMP6_MIB_INERRORS);
108 return;
109 }
110
111 if (sk->sk_state == DCCP_TIME_WAIT) {
112 inet_twsk_put(inet_twsk(sk));
113 return;
114 }
115
116 bh_lock_sock(sk);
117 if (sock_owned_by_user(sk))
118 NET_INC_STATS_BH(net, LINUX_MIB_LOCKDROPPEDICMPS);
119
120 if (sk->sk_state == DCCP_CLOSED)
121 goto out;
122
123 dp = dccp_sk(sk);
124 seq = dccp_hdr_seq(dh);
125 if ((1 << sk->sk_state) & ~(DCCPF_REQUESTING | DCCPF_LISTEN) &&
126 !between48(seq, dp->dccps_awl, dp->dccps_awh)) {
127 NET_INC_STATS_BH(net, LINUX_MIB_OUTOFWINDOWICMPS);
128 goto out;
129 }
130
131 np = inet6_sk(sk);
132
133 if (type == NDISC_REDIRECT) {
134 struct dst_entry *dst = __sk_dst_check(sk, np->dst_cookie);
135
136 if (dst)
137 dst->ops->redirect(dst, sk, skb);
138 }
139
140 if (type == ICMPV6_PKT_TOOBIG) {
141 struct dst_entry *dst = NULL;
142
143 if (sock_owned_by_user(sk))
144 goto out;
145 if ((1 << sk->sk_state) & (DCCPF_LISTEN | DCCPF_CLOSED))
146 goto out;
147
148 dst = inet6_csk_update_pmtu(sk, ntohl(info));
149 if (!dst)
150 goto out;
151
152 if (inet_csk(sk)->icsk_pmtu_cookie > dst_mtu(dst))
153 dccp_sync_mss(sk, dst_mtu(dst));
154 goto out;
155 }
156
157 icmpv6_err_convert(type, code, &err);
158
159 /* Might be for an request_sock */
160 switch (sk->sk_state) {
161 struct request_sock *req, **prev;
162 case DCCP_LISTEN:
163 if (sock_owned_by_user(sk))
164 goto out;
165
166 req = inet6_csk_search_req(sk, &prev, dh->dccph_dport,
167 &hdr->daddr, &hdr->saddr,
168 inet6_iif(skb));
169 if (req == NULL)
170 goto out;
171
172 /*
173 * ICMPs are not backlogged, hence we cannot get an established
174 * socket here.
175 */
176 WARN_ON(req->sk != NULL);
177
178 if (!between48(seq, dccp_rsk(req)->dreq_iss,
179 dccp_rsk(req)->dreq_gss)) {
180 NET_INC_STATS_BH(net, LINUX_MIB_OUTOFWINDOWICMPS);
181 goto out;
182 }
183
184 inet_csk_reqsk_queue_drop(sk, req, prev);
185 goto out;
186
187 case DCCP_REQUESTING:
188 case DCCP_RESPOND: /* Cannot happen.
189 It can, it SYNs are crossed. --ANK */
190 if (!sock_owned_by_user(sk)) {
191 DCCP_INC_STATS_BH(DCCP_MIB_ATTEMPTFAILS);
192 sk->sk_err = err;
193 /*
194 * Wake people up to see the error
195 * (see connect in sock.c)
196 */
197 sk->sk_error_report(sk);
198 dccp_done(sk);
199 } else
200 sk->sk_err_soft = err;
201 goto out;
202 }
203
204 if (!sock_owned_by_user(sk) && np->recverr) {
205 sk->sk_err = err;
206 sk->sk_error_report(sk);
207 } else
208 sk->sk_err_soft = err;
209
210 out:
211 bh_unlock_sock(sk);
212 sock_put(sk);
213 }
214
215
dccp_v6_send_response(struct sock * sk,struct request_sock * req)216 static int dccp_v6_send_response(struct sock *sk, struct request_sock *req)
217 {
218 struct inet6_request_sock *ireq6 = inet6_rsk(req);
219 struct ipv6_pinfo *np = inet6_sk(sk);
220 struct sk_buff *skb;
221 struct in6_addr *final_p, final;
222 struct flowi6 fl6;
223 int err = -1;
224 struct dst_entry *dst;
225
226 memset(&fl6, 0, sizeof(fl6));
227 fl6.flowi6_proto = IPPROTO_DCCP;
228 fl6.daddr = ireq6->rmt_addr;
229 fl6.saddr = ireq6->loc_addr;
230 fl6.flowlabel = 0;
231 fl6.flowi6_oif = ireq6->iif;
232 fl6.fl6_dport = inet_rsk(req)->rmt_port;
233 fl6.fl6_sport = inet_rsk(req)->loc_port;
234 security_req_classify_flow(req, flowi6_to_flowi(&fl6));
235
236
237 rcu_read_lock();
238 final_p = fl6_update_dst(&fl6, rcu_dereference(np->opt), &final);
239 rcu_read_unlock();
240
241 dst = ip6_dst_lookup_flow(sk, &fl6, final_p, false);
242 if (IS_ERR(dst)) {
243 err = PTR_ERR(dst);
244 dst = NULL;
245 goto done;
246 }
247
248 skb = dccp_make_response(sk, dst, req);
249 if (skb != NULL) {
250 struct dccp_hdr *dh = dccp_hdr(skb);
251
252 dh->dccph_checksum = dccp_v6_csum_finish(skb,
253 &ireq6->loc_addr,
254 &ireq6->rmt_addr);
255 fl6.daddr = ireq6->rmt_addr;
256 rcu_read_lock();
257 err = ip6_xmit(sk, skb, &fl6, rcu_dereference(np->opt),
258 np->tclass);
259 rcu_read_unlock();
260 err = net_xmit_eval(err);
261 }
262
263 done:
264 dst_release(dst);
265 return err;
266 }
267
dccp_v6_reqsk_destructor(struct request_sock * req)268 static void dccp_v6_reqsk_destructor(struct request_sock *req)
269 {
270 dccp_feat_list_purge(&dccp_rsk(req)->dreq_featneg);
271 if (inet6_rsk(req)->pktopts != NULL)
272 kfree_skb(inet6_rsk(req)->pktopts);
273 }
274
dccp_v6_ctl_send_reset(struct sock * sk,struct sk_buff * rxskb)275 static void dccp_v6_ctl_send_reset(struct sock *sk, struct sk_buff *rxskb)
276 {
277 const struct ipv6hdr *rxip6h;
278 struct sk_buff *skb;
279 struct flowi6 fl6;
280 struct net *net = dev_net(skb_dst(rxskb)->dev);
281 struct sock *ctl_sk = net->dccp.v6_ctl_sk;
282 struct dst_entry *dst;
283
284 if (dccp_hdr(rxskb)->dccph_type == DCCP_PKT_RESET)
285 return;
286
287 if (!ipv6_unicast_destination(rxskb))
288 return;
289
290 skb = dccp_ctl_make_reset(ctl_sk, rxskb);
291 if (skb == NULL)
292 return;
293
294 rxip6h = ipv6_hdr(rxskb);
295 dccp_hdr(skb)->dccph_checksum = dccp_v6_csum_finish(skb, &rxip6h->saddr,
296 &rxip6h->daddr);
297
298 memset(&fl6, 0, sizeof(fl6));
299 fl6.daddr = rxip6h->saddr;
300 fl6.saddr = rxip6h->daddr;
301
302 fl6.flowi6_proto = IPPROTO_DCCP;
303 fl6.flowi6_oif = inet6_iif(rxskb);
304 fl6.fl6_dport = dccp_hdr(skb)->dccph_dport;
305 fl6.fl6_sport = dccp_hdr(skb)->dccph_sport;
306 security_skb_classify_flow(rxskb, flowi6_to_flowi(&fl6));
307
308 /* sk = NULL, but it is safe for now. RST socket required. */
309 dst = ip6_dst_lookup_flow(ctl_sk, &fl6, NULL, false);
310 if (!IS_ERR(dst)) {
311 skb_dst_set(skb, dst);
312 ip6_xmit(ctl_sk, skb, &fl6, NULL, 0);
313 DCCP_INC_STATS_BH(DCCP_MIB_OUTSEGS);
314 DCCP_INC_STATS_BH(DCCP_MIB_OUTRSTS);
315 return;
316 }
317
318 kfree_skb(skb);
319 }
320
321 static struct request_sock_ops dccp6_request_sock_ops = {
322 .family = AF_INET6,
323 .obj_size = sizeof(struct dccp6_request_sock),
324 .rtx_syn_ack = dccp_v6_send_response,
325 .send_ack = dccp_reqsk_send_ack,
326 .destructor = dccp_v6_reqsk_destructor,
327 .send_reset = dccp_v6_ctl_send_reset,
328 .syn_ack_timeout = dccp_syn_ack_timeout,
329 };
330
dccp_v6_hnd_req(struct sock * sk,struct sk_buff * skb)331 static struct sock *dccp_v6_hnd_req(struct sock *sk,struct sk_buff *skb)
332 {
333 const struct dccp_hdr *dh = dccp_hdr(skb);
334 const struct ipv6hdr *iph = ipv6_hdr(skb);
335 struct sock *nsk;
336 struct request_sock **prev;
337 /* Find possible connection requests. */
338 struct request_sock *req = inet6_csk_search_req(sk, &prev,
339 dh->dccph_sport,
340 &iph->saddr,
341 &iph->daddr,
342 inet6_iif(skb));
343 if (req != NULL)
344 return dccp_check_req(sk, skb, req, prev);
345
346 nsk = __inet6_lookup_established(sock_net(sk), &dccp_hashinfo,
347 &iph->saddr, dh->dccph_sport,
348 &iph->daddr, ntohs(dh->dccph_dport),
349 inet6_iif(skb));
350 if (nsk != NULL) {
351 if (nsk->sk_state != DCCP_TIME_WAIT) {
352 bh_lock_sock(nsk);
353 return nsk;
354 }
355 inet_twsk_put(inet_twsk(nsk));
356 return NULL;
357 }
358
359 return sk;
360 }
361
dccp_v6_conn_request(struct sock * sk,struct sk_buff * skb)362 static int dccp_v6_conn_request(struct sock *sk, struct sk_buff *skb)
363 {
364 struct request_sock *req;
365 struct dccp_request_sock *dreq;
366 struct inet6_request_sock *ireq6;
367 struct ipv6_pinfo *np = inet6_sk(sk);
368 const __be32 service = dccp_hdr_request(skb)->dccph_req_service;
369 struct dccp_skb_cb *dcb = DCCP_SKB_CB(skb);
370
371 if (skb->protocol == htons(ETH_P_IP))
372 return dccp_v4_conn_request(sk, skb);
373
374 if (!ipv6_unicast_destination(skb))
375 return 0; /* discard, don't send a reset here */
376
377 if (dccp_bad_service_code(sk, service)) {
378 dcb->dccpd_reset_code = DCCP_RESET_CODE_BAD_SERVICE_CODE;
379 goto drop;
380 }
381 /*
382 * There are no SYN attacks on IPv6, yet...
383 */
384 dcb->dccpd_reset_code = DCCP_RESET_CODE_TOO_BUSY;
385 if (inet_csk_reqsk_queue_is_full(sk))
386 goto drop;
387
388 if (sk_acceptq_is_full(sk) && inet_csk_reqsk_queue_young(sk) > 1)
389 goto drop;
390
391 req = inet6_reqsk_alloc(&dccp6_request_sock_ops);
392 if (req == NULL)
393 goto drop;
394
395 if (dccp_reqsk_init(req, dccp_sk(sk), skb))
396 goto drop_and_free;
397
398 dreq = dccp_rsk(req);
399 if (dccp_parse_options(sk, dreq, skb))
400 goto drop_and_free;
401
402 if (security_inet_conn_request(sk, skb, req))
403 goto drop_and_free;
404
405 ireq6 = inet6_rsk(req);
406 ireq6->rmt_addr = ipv6_hdr(skb)->saddr;
407 ireq6->loc_addr = ipv6_hdr(skb)->daddr;
408
409 if (ipv6_opt_accepted(sk, skb) ||
410 np->rxopt.bits.rxinfo || np->rxopt.bits.rxoinfo ||
411 np->rxopt.bits.rxhlim || np->rxopt.bits.rxohlim) {
412 atomic_inc(&skb->users);
413 ireq6->pktopts = skb;
414 }
415 ireq6->iif = sk->sk_bound_dev_if;
416
417 /* So that link locals have meaning */
418 if (!sk->sk_bound_dev_if &&
419 ipv6_addr_type(&ireq6->rmt_addr) & IPV6_ADDR_LINKLOCAL)
420 ireq6->iif = inet6_iif(skb);
421
422 /*
423 * Step 3: Process LISTEN state
424 *
425 * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init Cookie
426 *
427 * Setting S.SWL/S.SWH to is deferred to dccp_create_openreq_child().
428 */
429 dreq->dreq_isr = dcb->dccpd_seq;
430 dreq->dreq_gsr = dreq->dreq_isr;
431 dreq->dreq_iss = dccp_v6_init_sequence(skb);
432 dreq->dreq_gss = dreq->dreq_iss;
433 dreq->dreq_service = service;
434
435 if (dccp_v6_send_response(sk, req))
436 goto drop_and_free;
437
438 inet6_csk_reqsk_queue_hash_add(sk, req, DCCP_TIMEOUT_INIT);
439 return 0;
440
441 drop_and_free:
442 reqsk_free(req);
443 drop:
444 DCCP_INC_STATS_BH(DCCP_MIB_ATTEMPTFAILS);
445 return -1;
446 }
447
dccp_v6_request_recv_sock(struct sock * sk,struct sk_buff * skb,struct request_sock * req,struct dst_entry * dst)448 static struct sock *dccp_v6_request_recv_sock(struct sock *sk,
449 struct sk_buff *skb,
450 struct request_sock *req,
451 struct dst_entry *dst)
452 {
453 struct inet6_request_sock *ireq6 = inet6_rsk(req);
454 struct ipv6_pinfo *newnp, *np = inet6_sk(sk);
455 struct ipv6_txoptions *opt;
456 struct inet_sock *newinet;
457 struct dccp6_sock *newdp6;
458 struct sock *newsk;
459
460 if (skb->protocol == htons(ETH_P_IP)) {
461 /*
462 * v6 mapped
463 */
464 newsk = dccp_v4_request_recv_sock(sk, skb, req, dst);
465 if (newsk == NULL)
466 return NULL;
467
468 newdp6 = (struct dccp6_sock *)newsk;
469 newinet = inet_sk(newsk);
470 newinet->pinet6 = &newdp6->inet6;
471 newnp = inet6_sk(newsk);
472
473 memcpy(newnp, np, sizeof(struct ipv6_pinfo));
474
475 ipv6_addr_set_v4mapped(newinet->inet_daddr, &newnp->daddr);
476
477 ipv6_addr_set_v4mapped(newinet->inet_saddr, &newnp->saddr);
478
479 newnp->rcv_saddr = newnp->saddr;
480
481 inet_csk(newsk)->icsk_af_ops = &dccp_ipv6_mapped;
482 newsk->sk_backlog_rcv = dccp_v4_do_rcv;
483 newnp->pktoptions = NULL;
484 newnp->opt = NULL;
485 newnp->ipv6_mc_list = NULL;
486 newnp->ipv6_ac_list = NULL;
487 newnp->ipv6_fl_list = NULL;
488 newnp->mcast_oif = inet6_iif(skb);
489 newnp->mcast_hops = ipv6_hdr(skb)->hop_limit;
490
491 /*
492 * No need to charge this sock to the relevant IPv6 refcnt debug socks count
493 * here, dccp_create_openreq_child now does this for us, see the comment in
494 * that function for the gory details. -acme
495 */
496
497 /* It is tricky place. Until this moment IPv4 tcp
498 worked with IPv6 icsk.icsk_af_ops.
499 Sync it now.
500 */
501 dccp_sync_mss(newsk, inet_csk(newsk)->icsk_pmtu_cookie);
502
503 return newsk;
504 }
505
506
507 if (sk_acceptq_is_full(sk))
508 goto out_overflow;
509
510 if (dst == NULL) {
511 struct in6_addr *final_p, final;
512 struct flowi6 fl6;
513
514 memset(&fl6, 0, sizeof(fl6));
515 fl6.flowi6_proto = IPPROTO_DCCP;
516 fl6.daddr = ireq6->rmt_addr;
517 final_p = fl6_update_dst(&fl6, np->opt, &final);
518 fl6.saddr = ireq6->loc_addr;
519 fl6.flowi6_oif = sk->sk_bound_dev_if;
520 fl6.fl6_dport = inet_rsk(req)->rmt_port;
521 fl6.fl6_sport = inet_rsk(req)->loc_port;
522 security_sk_classify_flow(sk, flowi6_to_flowi(&fl6));
523
524 dst = ip6_dst_lookup_flow(sk, &fl6, final_p, false);
525 if (IS_ERR(dst))
526 goto out;
527 }
528
529 newsk = dccp_create_openreq_child(sk, req, skb);
530 if (newsk == NULL)
531 goto out_nonewsk;
532
533 /*
534 * No need to charge this sock to the relevant IPv6 refcnt debug socks
535 * count here, dccp_create_openreq_child now does this for us, see the
536 * comment in that function for the gory details. -acme
537 */
538
539 __ip6_dst_store(newsk, dst, NULL, NULL);
540 newsk->sk_route_caps = dst->dev->features & ~(NETIF_F_IP_CSUM |
541 NETIF_F_TSO);
542 newdp6 = (struct dccp6_sock *)newsk;
543 newinet = inet_sk(newsk);
544 newinet->pinet6 = &newdp6->inet6;
545 newnp = inet6_sk(newsk);
546
547 memcpy(newnp, np, sizeof(struct ipv6_pinfo));
548
549 newnp->daddr = ireq6->rmt_addr;
550 newnp->saddr = ireq6->loc_addr;
551 newnp->rcv_saddr = ireq6->loc_addr;
552 newsk->sk_bound_dev_if = ireq6->iif;
553
554 /* Now IPv6 options...
555
556 First: no IPv4 options.
557 */
558 newinet->inet_opt = NULL;
559
560 /* Clone RX bits */
561 newnp->rxopt.all = np->rxopt.all;
562
563 newnp->ipv6_mc_list = NULL;
564 newnp->ipv6_ac_list = NULL;
565 newnp->ipv6_fl_list = NULL;
566
567 /* Clone pktoptions received with SYN */
568 newnp->pktoptions = NULL;
569 if (ireq6->pktopts != NULL) {
570 newnp->pktoptions = skb_clone(ireq6->pktopts, GFP_ATOMIC);
571 consume_skb(ireq6->pktopts);
572 ireq6->pktopts = NULL;
573 if (newnp->pktoptions)
574 skb_set_owner_r(newnp->pktoptions, newsk);
575 }
576 newnp->opt = NULL;
577 newnp->mcast_oif = inet6_iif(skb);
578 newnp->mcast_hops = ipv6_hdr(skb)->hop_limit;
579
580 /*
581 * Clone native IPv6 options from listening socket (if any)
582 *
583 * Yes, keeping reference count would be much more clever, but we make
584 * one more one thing there: reattach optmem to newsk.
585 */
586 opt = rcu_dereference(np->opt);
587 if (opt) {
588 opt = ipv6_dup_options(newsk, opt);
589 RCU_INIT_POINTER(newnp->opt, opt);
590 }
591 inet_csk(newsk)->icsk_ext_hdr_len = 0;
592 if (opt)
593 inet_csk(newsk)->icsk_ext_hdr_len = opt->opt_nflen +
594 opt->opt_flen;
595
596 dccp_sync_mss(newsk, dst_mtu(dst));
597
598 newinet->inet_daddr = newinet->inet_saddr = LOOPBACK4_IPV6;
599 newinet->inet_rcv_saddr = LOOPBACK4_IPV6;
600
601 if (__inet_inherit_port(sk, newsk) < 0) {
602 inet_csk_prepare_forced_close(newsk);
603 dccp_done(newsk);
604 goto out;
605 }
606 __inet6_hash(newsk, NULL);
607
608 return newsk;
609
610 out_overflow:
611 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_LISTENOVERFLOWS);
612 out_nonewsk:
613 dst_release(dst);
614 out:
615 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_LISTENDROPS);
616 return NULL;
617 }
618
619 /* The socket must have it's spinlock held when we get
620 * here.
621 *
622 * We have a potential double-lock case here, so even when
623 * doing backlog processing we use the BH locking scheme.
624 * This is because we cannot sleep with the original spinlock
625 * held.
626 */
dccp_v6_do_rcv(struct sock * sk,struct sk_buff * skb)627 static int dccp_v6_do_rcv(struct sock *sk, struct sk_buff *skb)
628 {
629 struct ipv6_pinfo *np = inet6_sk(sk);
630 struct sk_buff *opt_skb = NULL;
631
632 /* Imagine: socket is IPv6. IPv4 packet arrives,
633 goes to IPv4 receive handler and backlogged.
634 From backlog it always goes here. Kerboom...
635 Fortunately, dccp_rcv_established and rcv_established
636 handle them correctly, but it is not case with
637 dccp_v6_hnd_req and dccp_v6_ctl_send_reset(). --ANK
638 */
639
640 if (skb->protocol == htons(ETH_P_IP))
641 return dccp_v4_do_rcv(sk, skb);
642
643 if (sk_filter(sk, skb))
644 goto discard;
645
646 /*
647 * socket locking is here for SMP purposes as backlog rcv is currently
648 * called with bh processing disabled.
649 */
650
651 /* Do Stevens' IPV6_PKTOPTIONS.
652
653 Yes, guys, it is the only place in our code, where we
654 may make it not affecting IPv4.
655 The rest of code is protocol independent,
656 and I do not like idea to uglify IPv4.
657
658 Actually, all the idea behind IPV6_PKTOPTIONS
659 looks not very well thought. For now we latch
660 options, received in the last packet, enqueued
661 by tcp. Feel free to propose better solution.
662 --ANK (980728)
663 */
664 if (np->rxopt.all)
665 /*
666 * FIXME: Add handling of IPV6_PKTOPTIONS skb. See the comments below
667 * (wrt ipv6_pktopions) and net/ipv6/tcp_ipv6.c for an example.
668 */
669 opt_skb = skb_clone(skb, GFP_ATOMIC);
670
671 if (sk->sk_state == DCCP_OPEN) { /* Fast path */
672 if (dccp_rcv_established(sk, skb, dccp_hdr(skb), skb->len))
673 goto reset;
674 if (opt_skb) {
675 /* XXX This is where we would goto ipv6_pktoptions. */
676 __kfree_skb(opt_skb);
677 }
678 return 0;
679 }
680
681 /*
682 * Step 3: Process LISTEN state
683 * If S.state == LISTEN,
684 * If P.type == Request or P contains a valid Init Cookie option,
685 * (* Must scan the packet's options to check for Init
686 * Cookies. Only Init Cookies are processed here,
687 * however; other options are processed in Step 8. This
688 * scan need only be performed if the endpoint uses Init
689 * Cookies *)
690 * (* Generate a new socket and switch to that socket *)
691 * Set S := new socket for this port pair
692 * S.state = RESPOND
693 * Choose S.ISS (initial seqno) or set from Init Cookies
694 * Initialize S.GAR := S.ISS
695 * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init Cookies
696 * Continue with S.state == RESPOND
697 * (* A Response packet will be generated in Step 11 *)
698 * Otherwise,
699 * Generate Reset(No Connection) unless P.type == Reset
700 * Drop packet and return
701 *
702 * NOTE: the check for the packet types is done in
703 * dccp_rcv_state_process
704 */
705 if (sk->sk_state == DCCP_LISTEN) {
706 struct sock *nsk = dccp_v6_hnd_req(sk, skb);
707
708 if (nsk == NULL)
709 goto discard;
710 /*
711 * Queue it on the new socket if the new socket is active,
712 * otherwise we just shortcircuit this and continue with
713 * the new socket..
714 */
715 if (nsk != sk) {
716 if (dccp_child_process(sk, nsk, skb))
717 goto reset;
718 if (opt_skb != NULL)
719 __kfree_skb(opt_skb);
720 return 0;
721 }
722 }
723
724 if (dccp_rcv_state_process(sk, skb, dccp_hdr(skb), skb->len))
725 goto reset;
726 if (opt_skb) {
727 /* XXX This is where we would goto ipv6_pktoptions. */
728 __kfree_skb(opt_skb);
729 }
730 return 0;
731
732 reset:
733 dccp_v6_ctl_send_reset(sk, skb);
734 discard:
735 if (opt_skb != NULL)
736 __kfree_skb(opt_skb);
737 kfree_skb(skb);
738 return 0;
739 }
740
dccp_v6_rcv(struct sk_buff * skb)741 static int dccp_v6_rcv(struct sk_buff *skb)
742 {
743 const struct dccp_hdr *dh;
744 struct sock *sk;
745 int min_cov;
746
747 /* Step 1: Check header basics */
748
749 if (dccp_invalid_packet(skb))
750 goto discard_it;
751
752 /* Step 1: If header checksum is incorrect, drop packet and return. */
753 if (dccp_v6_csum_finish(skb, &ipv6_hdr(skb)->saddr,
754 &ipv6_hdr(skb)->daddr)) {
755 DCCP_WARN("dropped packet with invalid checksum\n");
756 goto discard_it;
757 }
758
759 dh = dccp_hdr(skb);
760
761 DCCP_SKB_CB(skb)->dccpd_seq = dccp_hdr_seq(dh);
762 DCCP_SKB_CB(skb)->dccpd_type = dh->dccph_type;
763
764 if (dccp_packet_without_ack(skb))
765 DCCP_SKB_CB(skb)->dccpd_ack_seq = DCCP_PKT_WITHOUT_ACK_SEQ;
766 else
767 DCCP_SKB_CB(skb)->dccpd_ack_seq = dccp_hdr_ack_seq(skb);
768
769 /* Step 2:
770 * Look up flow ID in table and get corresponding socket */
771 sk = __inet6_lookup_skb(&dccp_hashinfo, skb,
772 dh->dccph_sport, dh->dccph_dport);
773 /*
774 * Step 2:
775 * If no socket ...
776 */
777 if (sk == NULL) {
778 dccp_pr_debug("failed to look up flow ID in table and "
779 "get corresponding socket\n");
780 goto no_dccp_socket;
781 }
782
783 /*
784 * Step 2:
785 * ... or S.state == TIMEWAIT,
786 * Generate Reset(No Connection) unless P.type == Reset
787 * Drop packet and return
788 */
789 if (sk->sk_state == DCCP_TIME_WAIT) {
790 dccp_pr_debug("sk->sk_state == DCCP_TIME_WAIT: do_time_wait\n");
791 inet_twsk_put(inet_twsk(sk));
792 goto no_dccp_socket;
793 }
794
795 /*
796 * RFC 4340, sec. 9.2.1: Minimum Checksum Coverage
797 * o if MinCsCov = 0, only packets with CsCov = 0 are accepted
798 * o if MinCsCov > 0, also accept packets with CsCov >= MinCsCov
799 */
800 min_cov = dccp_sk(sk)->dccps_pcrlen;
801 if (dh->dccph_cscov && (min_cov == 0 || dh->dccph_cscov < min_cov)) {
802 dccp_pr_debug("Packet CsCov %d does not satisfy MinCsCov %d\n",
803 dh->dccph_cscov, min_cov);
804 /* FIXME: send Data Dropped option (see also dccp_v4_rcv) */
805 goto discard_and_relse;
806 }
807
808 if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb))
809 goto discard_and_relse;
810
811 return sk_receive_skb(sk, skb, 1) ? -1 : 0;
812
813 no_dccp_socket:
814 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
815 goto discard_it;
816 /*
817 * Step 2:
818 * If no socket ...
819 * Generate Reset(No Connection) unless P.type == Reset
820 * Drop packet and return
821 */
822 if (dh->dccph_type != DCCP_PKT_RESET) {
823 DCCP_SKB_CB(skb)->dccpd_reset_code =
824 DCCP_RESET_CODE_NO_CONNECTION;
825 dccp_v6_ctl_send_reset(sk, skb);
826 }
827
828 discard_it:
829 kfree_skb(skb);
830 return 0;
831
832 discard_and_relse:
833 sock_put(sk);
834 goto discard_it;
835 }
836
dccp_v6_connect(struct sock * sk,struct sockaddr * uaddr,int addr_len)837 static int dccp_v6_connect(struct sock *sk, struct sockaddr *uaddr,
838 int addr_len)
839 {
840 struct sockaddr_in6 *usin = (struct sockaddr_in6 *)uaddr;
841 struct inet_connection_sock *icsk = inet_csk(sk);
842 struct inet_sock *inet = inet_sk(sk);
843 struct ipv6_pinfo *np = inet6_sk(sk);
844 struct dccp_sock *dp = dccp_sk(sk);
845 struct in6_addr *saddr = NULL, *final_p, final;
846 struct ipv6_txoptions *opt;
847 struct flowi6 fl6;
848 struct dst_entry *dst;
849 int addr_type;
850 int err;
851
852 dp->dccps_role = DCCP_ROLE_CLIENT;
853
854 if (addr_len < SIN6_LEN_RFC2133)
855 return -EINVAL;
856
857 if (usin->sin6_family != AF_INET6)
858 return -EAFNOSUPPORT;
859
860 memset(&fl6, 0, sizeof(fl6));
861
862 if (np->sndflow) {
863 fl6.flowlabel = usin->sin6_flowinfo & IPV6_FLOWINFO_MASK;
864 IP6_ECN_flow_init(fl6.flowlabel);
865 if (fl6.flowlabel & IPV6_FLOWLABEL_MASK) {
866 struct ip6_flowlabel *flowlabel;
867 flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
868 if (flowlabel == NULL)
869 return -EINVAL;
870 usin->sin6_addr = flowlabel->dst;
871 fl6_sock_release(flowlabel);
872 }
873 }
874 /*
875 * connect() to INADDR_ANY means loopback (BSD'ism).
876 */
877 if (ipv6_addr_any(&usin->sin6_addr))
878 usin->sin6_addr.s6_addr[15] = 1;
879
880 addr_type = ipv6_addr_type(&usin->sin6_addr);
881
882 if (addr_type & IPV6_ADDR_MULTICAST)
883 return -ENETUNREACH;
884
885 if (addr_type & IPV6_ADDR_LINKLOCAL) {
886 if (addr_len >= sizeof(struct sockaddr_in6) &&
887 usin->sin6_scope_id) {
888 /* If interface is set while binding, indices
889 * must coincide.
890 */
891 if (sk->sk_bound_dev_if &&
892 sk->sk_bound_dev_if != usin->sin6_scope_id)
893 return -EINVAL;
894
895 sk->sk_bound_dev_if = usin->sin6_scope_id;
896 }
897
898 /* Connect to link-local address requires an interface */
899 if (!sk->sk_bound_dev_if)
900 return -EINVAL;
901 }
902
903 np->daddr = usin->sin6_addr;
904 np->flow_label = fl6.flowlabel;
905
906 /*
907 * DCCP over IPv4
908 */
909 if (addr_type == IPV6_ADDR_MAPPED) {
910 u32 exthdrlen = icsk->icsk_ext_hdr_len;
911 struct sockaddr_in sin;
912
913 SOCK_DEBUG(sk, "connect: ipv4 mapped\n");
914
915 if (__ipv6_only_sock(sk))
916 return -ENETUNREACH;
917
918 sin.sin_family = AF_INET;
919 sin.sin_port = usin->sin6_port;
920 sin.sin_addr.s_addr = usin->sin6_addr.s6_addr32[3];
921
922 icsk->icsk_af_ops = &dccp_ipv6_mapped;
923 sk->sk_backlog_rcv = dccp_v4_do_rcv;
924
925 err = dccp_v4_connect(sk, (struct sockaddr *)&sin, sizeof(sin));
926 if (err) {
927 icsk->icsk_ext_hdr_len = exthdrlen;
928 icsk->icsk_af_ops = &dccp_ipv6_af_ops;
929 sk->sk_backlog_rcv = dccp_v6_do_rcv;
930 goto failure;
931 }
932 ipv6_addr_set_v4mapped(inet->inet_saddr, &np->saddr);
933 ipv6_addr_set_v4mapped(inet->inet_rcv_saddr, &np->rcv_saddr);
934
935 return err;
936 }
937
938 if (!ipv6_addr_any(&np->rcv_saddr))
939 saddr = &np->rcv_saddr;
940
941 fl6.flowi6_proto = IPPROTO_DCCP;
942 fl6.daddr = np->daddr;
943 fl6.saddr = saddr ? *saddr : np->saddr;
944 fl6.flowi6_oif = sk->sk_bound_dev_if;
945 fl6.fl6_dport = usin->sin6_port;
946 fl6.fl6_sport = inet->inet_sport;
947 security_sk_classify_flow(sk, flowi6_to_flowi(&fl6));
948
949 opt = rcu_dereference_protected(np->opt, sock_owned_by_user(sk));
950 final_p = fl6_update_dst(&fl6, opt, &final);
951
952 dst = ip6_dst_lookup_flow(sk, &fl6, final_p, true);
953 if (IS_ERR(dst)) {
954 err = PTR_ERR(dst);
955 goto failure;
956 }
957
958 if (saddr == NULL) {
959 saddr = &fl6.saddr;
960 np->rcv_saddr = *saddr;
961 }
962
963 /* set the source address */
964 np->saddr = *saddr;
965 inet->inet_rcv_saddr = LOOPBACK4_IPV6;
966
967 __ip6_dst_store(sk, dst, NULL, NULL);
968
969 icsk->icsk_ext_hdr_len = 0;
970 if (opt)
971 icsk->icsk_ext_hdr_len = opt->opt_flen + opt->opt_nflen;
972
973 inet->inet_dport = usin->sin6_port;
974
975 dccp_set_state(sk, DCCP_REQUESTING);
976 err = inet6_hash_connect(&dccp_death_row, sk);
977 if (err)
978 goto late_failure;
979
980 dp->dccps_iss = secure_dccpv6_sequence_number(np->saddr.s6_addr32,
981 np->daddr.s6_addr32,
982 inet->inet_sport,
983 inet->inet_dport);
984 err = dccp_connect(sk);
985 if (err)
986 goto late_failure;
987
988 return 0;
989
990 late_failure:
991 dccp_set_state(sk, DCCP_CLOSED);
992 __sk_dst_reset(sk);
993 failure:
994 inet->inet_dport = 0;
995 sk->sk_route_caps = 0;
996 return err;
997 }
998
999 static const struct inet_connection_sock_af_ops dccp_ipv6_af_ops = {
1000 .queue_xmit = inet6_csk_xmit,
1001 .send_check = dccp_v6_send_check,
1002 .rebuild_header = inet6_sk_rebuild_header,
1003 .conn_request = dccp_v6_conn_request,
1004 .syn_recv_sock = dccp_v6_request_recv_sock,
1005 .net_header_len = sizeof(struct ipv6hdr),
1006 .setsockopt = ipv6_setsockopt,
1007 .getsockopt = ipv6_getsockopt,
1008 .addr2sockaddr = inet6_csk_addr2sockaddr,
1009 .sockaddr_len = sizeof(struct sockaddr_in6),
1010 .bind_conflict = inet6_csk_bind_conflict,
1011 #ifdef CONFIG_COMPAT
1012 .compat_setsockopt = compat_ipv6_setsockopt,
1013 .compat_getsockopt = compat_ipv6_getsockopt,
1014 #endif
1015 };
1016
1017 /*
1018 * DCCP over IPv4 via INET6 API
1019 */
1020 static const struct inet_connection_sock_af_ops dccp_ipv6_mapped = {
1021 .queue_xmit = ip_queue_xmit,
1022 .send_check = dccp_v4_send_check,
1023 .rebuild_header = inet_sk_rebuild_header,
1024 .conn_request = dccp_v6_conn_request,
1025 .syn_recv_sock = dccp_v6_request_recv_sock,
1026 .net_header_len = sizeof(struct iphdr),
1027 .setsockopt = ipv6_setsockopt,
1028 .getsockopt = ipv6_getsockopt,
1029 .addr2sockaddr = inet6_csk_addr2sockaddr,
1030 .sockaddr_len = sizeof(struct sockaddr_in6),
1031 #ifdef CONFIG_COMPAT
1032 .compat_setsockopt = compat_ipv6_setsockopt,
1033 .compat_getsockopt = compat_ipv6_getsockopt,
1034 #endif
1035 };
1036
1037 /* NOTE: A lot of things set to zero explicitly by call to
1038 * sk_alloc() so need not be done here.
1039 */
dccp_v6_init_sock(struct sock * sk)1040 static int dccp_v6_init_sock(struct sock *sk)
1041 {
1042 static __u8 dccp_v6_ctl_sock_initialized;
1043 int err = dccp_init_sock(sk, dccp_v6_ctl_sock_initialized);
1044
1045 if (err == 0) {
1046 if (unlikely(!dccp_v6_ctl_sock_initialized))
1047 dccp_v6_ctl_sock_initialized = 1;
1048 inet_csk(sk)->icsk_af_ops = &dccp_ipv6_af_ops;
1049 }
1050
1051 return err;
1052 }
1053
dccp_v6_destroy_sock(struct sock * sk)1054 static void dccp_v6_destroy_sock(struct sock *sk)
1055 {
1056 dccp_destroy_sock(sk);
1057 inet6_destroy_sock(sk);
1058 }
1059
1060 static struct timewait_sock_ops dccp6_timewait_sock_ops = {
1061 .twsk_obj_size = sizeof(struct dccp6_timewait_sock),
1062 };
1063
1064 static struct proto dccp_v6_prot = {
1065 .name = "DCCPv6",
1066 .owner = THIS_MODULE,
1067 .close = dccp_close,
1068 .connect = dccp_v6_connect,
1069 .disconnect = dccp_disconnect,
1070 .ioctl = dccp_ioctl,
1071 .init = dccp_v6_init_sock,
1072 .setsockopt = dccp_setsockopt,
1073 .getsockopt = dccp_getsockopt,
1074 .sendmsg = dccp_sendmsg,
1075 .recvmsg = dccp_recvmsg,
1076 .backlog_rcv = dccp_v6_do_rcv,
1077 .hash = dccp_v6_hash,
1078 .unhash = inet_unhash,
1079 .accept = inet_csk_accept,
1080 .get_port = inet_csk_get_port,
1081 .shutdown = dccp_shutdown,
1082 .destroy = dccp_v6_destroy_sock,
1083 .orphan_count = &dccp_orphan_count,
1084 .max_header = MAX_DCCP_HEADER,
1085 .obj_size = sizeof(struct dccp6_sock),
1086 .slab_flags = SLAB_DESTROY_BY_RCU,
1087 .rsk_prot = &dccp6_request_sock_ops,
1088 .twsk_prot = &dccp6_timewait_sock_ops,
1089 .h.hashinfo = &dccp_hashinfo,
1090 #ifdef CONFIG_COMPAT
1091 .compat_setsockopt = compat_dccp_setsockopt,
1092 .compat_getsockopt = compat_dccp_getsockopt,
1093 #endif
1094 };
1095
1096 static const struct inet6_protocol dccp_v6_protocol = {
1097 .handler = dccp_v6_rcv,
1098 .err_handler = dccp_v6_err,
1099 .flags = INET6_PROTO_NOPOLICY | INET6_PROTO_FINAL,
1100 };
1101
1102 static const struct proto_ops inet6_dccp_ops = {
1103 .family = PF_INET6,
1104 .owner = THIS_MODULE,
1105 .release = inet6_release,
1106 .bind = inet6_bind,
1107 .connect = inet_stream_connect,
1108 .socketpair = sock_no_socketpair,
1109 .accept = inet_accept,
1110 .getname = inet6_getname,
1111 .poll = dccp_poll,
1112 .ioctl = inet6_ioctl,
1113 .listen = inet_dccp_listen,
1114 .shutdown = inet_shutdown,
1115 .setsockopt = sock_common_setsockopt,
1116 .getsockopt = sock_common_getsockopt,
1117 .sendmsg = inet_sendmsg,
1118 .recvmsg = sock_common_recvmsg,
1119 .mmap = sock_no_mmap,
1120 .sendpage = sock_no_sendpage,
1121 #ifdef CONFIG_COMPAT
1122 .compat_setsockopt = compat_sock_common_setsockopt,
1123 .compat_getsockopt = compat_sock_common_getsockopt,
1124 #endif
1125 };
1126
1127 static struct inet_protosw dccp_v6_protosw = {
1128 .type = SOCK_DCCP,
1129 .protocol = IPPROTO_DCCP,
1130 .prot = &dccp_v6_prot,
1131 .ops = &inet6_dccp_ops,
1132 .flags = INET_PROTOSW_ICSK,
1133 };
1134
dccp_v6_init_net(struct net * net)1135 static int __net_init dccp_v6_init_net(struct net *net)
1136 {
1137 if (dccp_hashinfo.bhash == NULL)
1138 return -ESOCKTNOSUPPORT;
1139
1140 return inet_ctl_sock_create(&net->dccp.v6_ctl_sk, PF_INET6,
1141 SOCK_DCCP, IPPROTO_DCCP, net);
1142 }
1143
dccp_v6_exit_net(struct net * net)1144 static void __net_exit dccp_v6_exit_net(struct net *net)
1145 {
1146 inet_ctl_sock_destroy(net->dccp.v6_ctl_sk);
1147 }
1148
1149 static struct pernet_operations dccp_v6_ops = {
1150 .init = dccp_v6_init_net,
1151 .exit = dccp_v6_exit_net,
1152 };
1153
dccp_v6_init(void)1154 static int __init dccp_v6_init(void)
1155 {
1156 int err = proto_register(&dccp_v6_prot, 1);
1157
1158 if (err != 0)
1159 goto out;
1160
1161 err = inet6_add_protocol(&dccp_v6_protocol, IPPROTO_DCCP);
1162 if (err != 0)
1163 goto out_unregister_proto;
1164
1165 inet6_register_protosw(&dccp_v6_protosw);
1166
1167 err = register_pernet_subsys(&dccp_v6_ops);
1168 if (err != 0)
1169 goto out_destroy_ctl_sock;
1170 out:
1171 return err;
1172
1173 out_destroy_ctl_sock:
1174 inet6_del_protocol(&dccp_v6_protocol, IPPROTO_DCCP);
1175 inet6_unregister_protosw(&dccp_v6_protosw);
1176 out_unregister_proto:
1177 proto_unregister(&dccp_v6_prot);
1178 goto out;
1179 }
1180
dccp_v6_exit(void)1181 static void __exit dccp_v6_exit(void)
1182 {
1183 unregister_pernet_subsys(&dccp_v6_ops);
1184 inet6_del_protocol(&dccp_v6_protocol, IPPROTO_DCCP);
1185 inet6_unregister_protosw(&dccp_v6_protosw);
1186 proto_unregister(&dccp_v6_prot);
1187 }
1188
1189 module_init(dccp_v6_init);
1190 module_exit(dccp_v6_exit);
1191
1192 /*
1193 * __stringify doesn't likes enums, so use SOCK_DCCP (6) and IPPROTO_DCCP (33)
1194 * values directly, Also cover the case where the protocol is not specified,
1195 * i.e. net-pf-PF_INET6-proto-0-type-SOCK_DCCP
1196 */
1197 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_INET6, 33, 6);
1198 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_INET6, 0, 6);
1199 MODULE_LICENSE("GPL");
1200 MODULE_AUTHOR("Arnaldo Carvalho de Melo <acme@mandriva.com>");
1201 MODULE_DESCRIPTION("DCCPv6 - Datagram Congestion Controlled Protocol");
1202