3 * Linux INET6 implementation
5 * Based on net/dccp6/ipv6.c
7 * Arnaldo Carvalho de Melo <acme@ghostprotocols.net>
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.
15 #include <linux/module.h>
16 #include <linux/random.h>
17 #include <linux/xfrm.h>
19 #include <net/addrconf.h>
20 #include <net/inet_common.h>
21 #include <net/inet_hashtables.h>
22 #include <net/inet_sock.h>
23 #include <net/inet6_connection_sock.h>
24 #include <net/inet6_hashtables.h>
25 #include <net/ip6_route.h>
27 #include <net/protocol.h>
28 #include <net/transp_v6.h>
29 #include <net/ip6_checksum.h>
36 /* Socket used for sending RSTs and ACKs */
37 static struct socket *dccp_v6_ctl_socket;
39 static struct inet_connection_sock_af_ops dccp_ipv6_mapped;
40 static struct inet_connection_sock_af_ops dccp_ipv6_af_ops;
42 static int dccp_v6_get_port(struct sock *sk, unsigned short snum)
44 return inet_csk_get_port(&dccp_hashinfo, sk, snum,
45 inet6_csk_bind_conflict);
48 static void dccp_v6_hash(struct sock *sk)
50 if (sk->sk_state != DCCP_CLOSED) {
51 if (inet_csk(sk)->icsk_af_ops == &dccp_ipv6_mapped) {
56 __inet6_hash(&dccp_hashinfo, sk);
61 /* add pseudo-header to DCCP checksum stored in skb->csum */
62 static inline u16 dccp_v6_csum_finish(struct sk_buff *skb,
63 struct in6_addr *saddr,
64 struct in6_addr *daddr)
66 return csum_ipv6_magic(saddr, daddr, skb->len, IPPROTO_DCCP, skb->csum);
69 static inline void dccp_v6_send_check(struct sock *sk, int unused_value,
72 struct ipv6_pinfo *np = inet6_sk(sk);
73 struct dccp_hdr *dh = dccp_hdr(skb);
75 dccp_csum_outgoing(skb);
76 dh->dccph_checksum = dccp_v6_csum_finish(skb, &np->saddr, &np->daddr);
79 static __u32 dccp_v6_init_sequence(struct sock *sk, struct sk_buff *skb)
81 const struct dccp_hdr *dh = dccp_hdr(skb);
83 if (skb->protocol == htons(ETH_P_IPV6))
84 return secure_tcpv6_sequence_number(skb->nh.ipv6h->daddr.s6_addr32,
85 skb->nh.ipv6h->saddr.s6_addr32,
89 return secure_dccp_sequence_number(skb->nh.iph->daddr,
95 static void dccp_v6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
96 int type, int code, int offset, __be32 info)
98 struct ipv6hdr *hdr = (struct ipv6hdr *)skb->data;
99 const struct dccp_hdr *dh = (struct dccp_hdr *)(skb->data + offset);
100 struct ipv6_pinfo *np;
105 sk = inet6_lookup(&dccp_hashinfo, &hdr->daddr, dh->dccph_dport,
106 &hdr->saddr, dh->dccph_sport, inet6_iif(skb));
109 ICMP6_INC_STATS_BH(__in6_dev_get(skb->dev), ICMP6_MIB_INERRORS);
113 if (sk->sk_state == DCCP_TIME_WAIT) {
114 inet_twsk_put(inet_twsk(sk));
119 if (sock_owned_by_user(sk))
120 NET_INC_STATS_BH(LINUX_MIB_LOCKDROPPEDICMPS);
122 if (sk->sk_state == DCCP_CLOSED)
127 if (type == ICMPV6_PKT_TOOBIG) {
128 struct dst_entry *dst = NULL;
130 if (sock_owned_by_user(sk))
132 if ((1 << sk->sk_state) & (DCCPF_LISTEN | DCCPF_CLOSED))
135 /* icmp should have updated the destination cache entry */
136 dst = __sk_dst_check(sk, np->dst_cookie);
138 struct inet_sock *inet = inet_sk(sk);
141 /* BUGGG_FUTURE: Again, it is not clear how
142 to handle rthdr case. Ignore this complexity
145 memset(&fl, 0, sizeof(fl));
146 fl.proto = IPPROTO_DCCP;
147 ipv6_addr_copy(&fl.fl6_dst, &np->daddr);
148 ipv6_addr_copy(&fl.fl6_src, &np->saddr);
149 fl.oif = sk->sk_bound_dev_if;
150 fl.fl_ip_dport = inet->dport;
151 fl.fl_ip_sport = inet->sport;
152 security_sk_classify_flow(sk, &fl);
154 err = ip6_dst_lookup(sk, &dst, &fl);
156 sk->sk_err_soft = -err;
160 err = xfrm_lookup(&dst, &fl, sk, 0);
162 sk->sk_err_soft = -err;
168 if (inet_csk(sk)->icsk_pmtu_cookie > dst_mtu(dst)) {
169 dccp_sync_mss(sk, dst_mtu(dst));
170 } /* else let the usual retransmit timer handle it */
175 icmpv6_err_convert(type, code, &err);
177 seq = DCCP_SKB_CB(skb)->dccpd_seq;
178 /* Might be for an request_sock */
179 switch (sk->sk_state) {
180 struct request_sock *req, **prev;
182 if (sock_owned_by_user(sk))
185 req = inet6_csk_search_req(sk, &prev, dh->dccph_dport,
186 &hdr->daddr, &hdr->saddr,
192 * ICMPs are not backlogged, hence we cannot get an established
195 BUG_TRAP(req->sk == NULL);
197 if (seq != dccp_rsk(req)->dreq_iss) {
198 NET_INC_STATS_BH(LINUX_MIB_OUTOFWINDOWICMPS);
202 inet_csk_reqsk_queue_drop(sk, req, prev);
205 case DCCP_REQUESTING:
206 case DCCP_RESPOND: /* Cannot happen.
207 It can, it SYNs are crossed. --ANK */
208 if (!sock_owned_by_user(sk)) {
209 DCCP_INC_STATS_BH(DCCP_MIB_ATTEMPTFAILS);
212 * Wake people up to see the error
213 * (see connect in sock.c)
215 sk->sk_error_report(sk);
218 sk->sk_err_soft = err;
222 if (!sock_owned_by_user(sk) && np->recverr) {
224 sk->sk_error_report(sk);
226 sk->sk_err_soft = err;
234 static int dccp_v6_send_response(struct sock *sk, struct request_sock *req,
235 struct dst_entry *dst)
237 struct inet6_request_sock *ireq6 = inet6_rsk(req);
238 struct ipv6_pinfo *np = inet6_sk(sk);
240 struct ipv6_txoptions *opt = NULL;
241 struct in6_addr *final_p = NULL, final;
245 memset(&fl, 0, sizeof(fl));
246 fl.proto = IPPROTO_DCCP;
247 ipv6_addr_copy(&fl.fl6_dst, &ireq6->rmt_addr);
248 ipv6_addr_copy(&fl.fl6_src, &ireq6->loc_addr);
249 fl.fl6_flowlabel = 0;
251 fl.fl_ip_dport = inet_rsk(req)->rmt_port;
252 fl.fl_ip_sport = inet_sk(sk)->sport;
253 security_req_classify_flow(req, &fl);
258 np->rxopt.bits.osrcrt == 2 &&
260 struct sk_buff *pktopts = ireq6->pktopts;
261 struct inet6_skb_parm *rxopt = IP6CB(pktopts);
264 opt = ipv6_invert_rthdr(sk,
265 (struct ipv6_rt_hdr *)(pktopts->nh.raw +
269 if (opt != NULL && opt->srcrt != NULL) {
270 const struct rt0_hdr *rt0 = (struct rt0_hdr *)opt->srcrt;
272 ipv6_addr_copy(&final, &fl.fl6_dst);
273 ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
277 err = ip6_dst_lookup(sk, &dst, &fl);
282 ipv6_addr_copy(&fl.fl6_dst, final_p);
284 err = xfrm_lookup(&dst, &fl, sk, 0);
289 skb = dccp_make_response(sk, dst, req);
291 struct dccp_hdr *dh = dccp_hdr(skb);
293 dh->dccph_checksum = dccp_v6_csum_finish(skb,
296 ipv6_addr_copy(&fl.fl6_dst, &ireq6->rmt_addr);
297 err = ip6_xmit(sk, skb, &fl, opt, 0);
298 if (err == NET_XMIT_CN)
303 if (opt != NULL && opt != np->opt)
304 sock_kfree_s(sk, opt, opt->tot_len);
309 static void dccp_v6_reqsk_destructor(struct request_sock *req)
311 if (inet6_rsk(req)->pktopts != NULL)
312 kfree_skb(inet6_rsk(req)->pktopts);
315 static void dccp_v6_ctl_send_reset(struct sk_buff *rxskb)
317 struct dccp_hdr *rxdh = dccp_hdr(rxskb), *dh;
318 const u32 dccp_hdr_reset_len = sizeof(struct dccp_hdr) +
319 sizeof(struct dccp_hdr_ext) +
320 sizeof(struct dccp_hdr_reset);
325 if (rxdh->dccph_type == DCCP_PKT_RESET)
328 if (!ipv6_unicast_destination(rxskb))
331 skb = alloc_skb(dccp_v6_ctl_socket->sk->sk_prot->max_header,
336 skb_reserve(skb, dccp_v6_ctl_socket->sk->sk_prot->max_header);
338 dh = dccp_zeroed_hdr(skb, dccp_hdr_reset_len);
340 /* Swap the send and the receive. */
341 dh->dccph_type = DCCP_PKT_RESET;
342 dh->dccph_sport = rxdh->dccph_dport;
343 dh->dccph_dport = rxdh->dccph_sport;
344 dh->dccph_doff = dccp_hdr_reset_len / 4;
346 dccp_hdr_reset(skb)->dccph_reset_code =
347 DCCP_SKB_CB(rxskb)->dccpd_reset_code;
349 /* See "8.3.1. Abnormal Termination" in RFC 4340 */
351 if (DCCP_SKB_CB(rxskb)->dccpd_ack_seq != DCCP_PKT_WITHOUT_ACK_SEQ)
352 dccp_set_seqno(&seqno, DCCP_SKB_CB(rxskb)->dccpd_ack_seq + 1);
354 dccp_hdr_set_seq(dh, seqno);
355 dccp_hdr_set_ack(dccp_hdr_ack_bits(skb),
356 DCCP_SKB_CB(rxskb)->dccpd_seq);
358 dccp_csum_outgoing(skb);
359 dh->dccph_checksum = dccp_v6_csum_finish(skb, &rxskb->nh.ipv6h->saddr,
360 &rxskb->nh.ipv6h->daddr);
362 memset(&fl, 0, sizeof(fl));
363 ipv6_addr_copy(&fl.fl6_dst, &rxskb->nh.ipv6h->saddr);
364 ipv6_addr_copy(&fl.fl6_src, &rxskb->nh.ipv6h->daddr);
366 fl.proto = IPPROTO_DCCP;
367 fl.oif = inet6_iif(rxskb);
368 fl.fl_ip_dport = dh->dccph_dport;
369 fl.fl_ip_sport = dh->dccph_sport;
370 security_skb_classify_flow(rxskb, &fl);
372 /* sk = NULL, but it is safe for now. RST socket required. */
373 if (!ip6_dst_lookup(NULL, &skb->dst, &fl)) {
374 if (xfrm_lookup(&skb->dst, &fl, NULL, 0) >= 0) {
375 ip6_xmit(dccp_v6_ctl_socket->sk, skb, &fl, NULL, 0);
376 DCCP_INC_STATS_BH(DCCP_MIB_OUTSEGS);
377 DCCP_INC_STATS_BH(DCCP_MIB_OUTRSTS);
385 static struct request_sock_ops dccp6_request_sock_ops = {
387 .obj_size = sizeof(struct dccp6_request_sock),
388 .rtx_syn_ack = dccp_v6_send_response,
389 .send_ack = dccp_reqsk_send_ack,
390 .destructor = dccp_v6_reqsk_destructor,
391 .send_reset = dccp_v6_ctl_send_reset,
394 static struct sock *dccp_v6_hnd_req(struct sock *sk,struct sk_buff *skb)
396 const struct dccp_hdr *dh = dccp_hdr(skb);
397 const struct ipv6hdr *iph = skb->nh.ipv6h;
399 struct request_sock **prev;
400 /* Find possible connection requests. */
401 struct request_sock *req = inet6_csk_search_req(sk, &prev,
407 return dccp_check_req(sk, skb, req, prev);
409 nsk = __inet6_lookup_established(&dccp_hashinfo,
410 &iph->saddr, dh->dccph_sport,
411 &iph->daddr, ntohs(dh->dccph_dport),
414 if (nsk->sk_state != DCCP_TIME_WAIT) {
418 inet_twsk_put(inet_twsk(nsk));
425 static int dccp_v6_conn_request(struct sock *sk, struct sk_buff *skb)
427 struct request_sock *req;
428 struct dccp_request_sock *dreq;
429 struct inet6_request_sock *ireq6;
430 struct ipv6_pinfo *np = inet6_sk(sk);
431 const __be32 service = dccp_hdr_request(skb)->dccph_req_service;
432 struct dccp_skb_cb *dcb = DCCP_SKB_CB(skb);
433 __u8 reset_code = DCCP_RESET_CODE_TOO_BUSY;
435 if (skb->protocol == htons(ETH_P_IP))
436 return dccp_v4_conn_request(sk, skb);
438 if (!ipv6_unicast_destination(skb))
441 if (dccp_bad_service_code(sk, service)) {
442 reset_code = DCCP_RESET_CODE_BAD_SERVICE_CODE;
446 * There are no SYN attacks on IPv6, yet...
448 if (inet_csk_reqsk_queue_is_full(sk))
451 if (sk_acceptq_is_full(sk) && inet_csk_reqsk_queue_young(sk) > 1)
454 req = inet6_reqsk_alloc(&dccp6_request_sock_ops);
458 if (dccp_parse_options(sk, skb))
461 dccp_reqsk_init(req, skb);
463 if (security_inet_conn_request(sk, skb, req))
466 ireq6 = inet6_rsk(req);
467 ipv6_addr_copy(&ireq6->rmt_addr, &skb->nh.ipv6h->saddr);
468 ipv6_addr_copy(&ireq6->loc_addr, &skb->nh.ipv6h->daddr);
469 ireq6->pktopts = NULL;
471 if (ipv6_opt_accepted(sk, skb) ||
472 np->rxopt.bits.rxinfo || np->rxopt.bits.rxoinfo ||
473 np->rxopt.bits.rxhlim || np->rxopt.bits.rxohlim) {
474 atomic_inc(&skb->users);
475 ireq6->pktopts = skb;
477 ireq6->iif = sk->sk_bound_dev_if;
479 /* So that link locals have meaning */
480 if (!sk->sk_bound_dev_if &&
481 ipv6_addr_type(&ireq6->rmt_addr) & IPV6_ADDR_LINKLOCAL)
482 ireq6->iif = inet6_iif(skb);
485 * Step 3: Process LISTEN state
487 * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init Cookie
489 * In fact we defer setting S.GSR, S.SWL, S.SWH to
490 * dccp_create_openreq_child.
492 dreq = dccp_rsk(req);
493 dreq->dreq_isr = dcb->dccpd_seq;
494 dreq->dreq_iss = dccp_v6_init_sequence(sk, skb);
495 dreq->dreq_service = service;
497 if (dccp_v6_send_response(sk, req, NULL))
500 inet6_csk_reqsk_queue_hash_add(sk, req, DCCP_TIMEOUT_INIT);
506 DCCP_INC_STATS_BH(DCCP_MIB_ATTEMPTFAILS);
507 dcb->dccpd_reset_code = reset_code;
511 static struct sock *dccp_v6_request_recv_sock(struct sock *sk,
513 struct request_sock *req,
514 struct dst_entry *dst)
516 struct inet6_request_sock *ireq6 = inet6_rsk(req);
517 struct ipv6_pinfo *newnp, *np = inet6_sk(sk);
518 struct inet_sock *newinet;
519 struct dccp_sock *newdp;
520 struct dccp6_sock *newdp6;
522 struct ipv6_txoptions *opt;
524 if (skb->protocol == htons(ETH_P_IP)) {
528 newsk = dccp_v4_request_recv_sock(sk, skb, req, dst);
532 newdp6 = (struct dccp6_sock *)newsk;
533 newdp = dccp_sk(newsk);
534 newinet = inet_sk(newsk);
535 newinet->pinet6 = &newdp6->inet6;
536 newnp = inet6_sk(newsk);
538 memcpy(newnp, np, sizeof(struct ipv6_pinfo));
540 ipv6_addr_set(&newnp->daddr, 0, 0, htonl(0x0000FFFF),
543 ipv6_addr_set(&newnp->saddr, 0, 0, htonl(0x0000FFFF),
546 ipv6_addr_copy(&newnp->rcv_saddr, &newnp->saddr);
548 inet_csk(newsk)->icsk_af_ops = &dccp_ipv6_mapped;
549 newsk->sk_backlog_rcv = dccp_v4_do_rcv;
550 newnp->pktoptions = NULL;
552 newnp->mcast_oif = inet6_iif(skb);
553 newnp->mcast_hops = skb->nh.ipv6h->hop_limit;
556 * No need to charge this sock to the relevant IPv6 refcnt debug socks count
557 * here, dccp_create_openreq_child now does this for us, see the comment in
558 * that function for the gory details. -acme
561 /* It is tricky place. Until this moment IPv4 tcp
562 worked with IPv6 icsk.icsk_af_ops.
565 dccp_sync_mss(newsk, inet_csk(newsk)->icsk_pmtu_cookie);
572 if (sk_acceptq_is_full(sk))
575 if (np->rxopt.bits.osrcrt == 2 && opt == NULL && ireq6->pktopts) {
576 const struct inet6_skb_parm *rxopt = IP6CB(ireq6->pktopts);
579 opt = ipv6_invert_rthdr(sk,
580 (struct ipv6_rt_hdr *)(ireq6->pktopts->nh.raw +
585 struct in6_addr *final_p = NULL, final;
588 memset(&fl, 0, sizeof(fl));
589 fl.proto = IPPROTO_DCCP;
590 ipv6_addr_copy(&fl.fl6_dst, &ireq6->rmt_addr);
591 if (opt != NULL && opt->srcrt != NULL) {
592 const struct rt0_hdr *rt0 = (struct rt0_hdr *)opt->srcrt;
594 ipv6_addr_copy(&final, &fl.fl6_dst);
595 ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
598 ipv6_addr_copy(&fl.fl6_src, &ireq6->loc_addr);
599 fl.oif = sk->sk_bound_dev_if;
600 fl.fl_ip_dport = inet_rsk(req)->rmt_port;
601 fl.fl_ip_sport = inet_sk(sk)->sport;
602 security_sk_classify_flow(sk, &fl);
604 if (ip6_dst_lookup(sk, &dst, &fl))
608 ipv6_addr_copy(&fl.fl6_dst, final_p);
610 if ((xfrm_lookup(&dst, &fl, sk, 0)) < 0)
614 newsk = dccp_create_openreq_child(sk, req, skb);
619 * No need to charge this sock to the relevant IPv6 refcnt debug socks
620 * count here, dccp_create_openreq_child now does this for us, see the
621 * comment in that function for the gory details. -acme
624 __ip6_dst_store(newsk, dst, NULL, NULL);
625 newsk->sk_route_caps = dst->dev->features & ~(NETIF_F_IP_CSUM |
627 newdp6 = (struct dccp6_sock *)newsk;
628 newinet = inet_sk(newsk);
629 newinet->pinet6 = &newdp6->inet6;
630 newdp = dccp_sk(newsk);
631 newnp = inet6_sk(newsk);
633 memcpy(newnp, np, sizeof(struct ipv6_pinfo));
635 ipv6_addr_copy(&newnp->daddr, &ireq6->rmt_addr);
636 ipv6_addr_copy(&newnp->saddr, &ireq6->loc_addr);
637 ipv6_addr_copy(&newnp->rcv_saddr, &ireq6->loc_addr);
638 newsk->sk_bound_dev_if = ireq6->iif;
640 /* Now IPv6 options...
642 First: no IPv4 options.
647 newnp->rxopt.all = np->rxopt.all;
649 /* Clone pktoptions received with SYN */
650 newnp->pktoptions = NULL;
651 if (ireq6->pktopts != NULL) {
652 newnp->pktoptions = skb_clone(ireq6->pktopts, GFP_ATOMIC);
653 kfree_skb(ireq6->pktopts);
654 ireq6->pktopts = NULL;
655 if (newnp->pktoptions)
656 skb_set_owner_r(newnp->pktoptions, newsk);
659 newnp->mcast_oif = inet6_iif(skb);
660 newnp->mcast_hops = skb->nh.ipv6h->hop_limit;
663 * Clone native IPv6 options from listening socket (if any)
665 * Yes, keeping reference count would be much more clever, but we make
666 * one more one thing there: reattach optmem to newsk.
669 newnp->opt = ipv6_dup_options(newsk, opt);
671 sock_kfree_s(sk, opt, opt->tot_len);
674 inet_csk(newsk)->icsk_ext_hdr_len = 0;
675 if (newnp->opt != NULL)
676 inet_csk(newsk)->icsk_ext_hdr_len = (newnp->opt->opt_nflen +
677 newnp->opt->opt_flen);
679 dccp_sync_mss(newsk, dst_mtu(dst));
681 newinet->daddr = newinet->saddr = newinet->rcv_saddr = LOOPBACK4_IPV6;
683 __inet6_hash(&dccp_hashinfo, newsk);
684 inet_inherit_port(&dccp_hashinfo, sk, newsk);
689 NET_INC_STATS_BH(LINUX_MIB_LISTENOVERFLOWS);
691 NET_INC_STATS_BH(LINUX_MIB_LISTENDROPS);
692 if (opt != NULL && opt != np->opt)
693 sock_kfree_s(sk, opt, opt->tot_len);
698 /* The socket must have it's spinlock held when we get
701 * We have a potential double-lock case here, so even when
702 * doing backlog processing we use the BH locking scheme.
703 * This is because we cannot sleep with the original spinlock
706 static int dccp_v6_do_rcv(struct sock *sk, struct sk_buff *skb)
708 struct ipv6_pinfo *np = inet6_sk(sk);
709 struct sk_buff *opt_skb = NULL;
711 /* Imagine: socket is IPv6. IPv4 packet arrives,
712 goes to IPv4 receive handler and backlogged.
713 From backlog it always goes here. Kerboom...
714 Fortunately, dccp_rcv_established and rcv_established
715 handle them correctly, but it is not case with
716 dccp_v6_hnd_req and dccp_v6_ctl_send_reset(). --ANK
719 if (skb->protocol == htons(ETH_P_IP))
720 return dccp_v4_do_rcv(sk, skb);
722 if (sk_filter(sk, skb))
726 * socket locking is here for SMP purposes as backlog rcv is currently
727 * called with bh processing disabled.
730 /* Do Stevens' IPV6_PKTOPTIONS.
732 Yes, guys, it is the only place in our code, where we
733 may make it not affecting IPv4.
734 The rest of code is protocol independent,
735 and I do not like idea to uglify IPv4.
737 Actually, all the idea behind IPV6_PKTOPTIONS
738 looks not very well thought. For now we latch
739 options, received in the last packet, enqueued
740 by tcp. Feel free to propose better solution.
745 * FIXME: Add handling of IPV6_PKTOPTIONS skb. See the comments below
746 * (wrt ipv6_pktopions) and net/ipv6/tcp_ipv6.c for an example.
748 opt_skb = skb_clone(skb, GFP_ATOMIC);
750 if (sk->sk_state == DCCP_OPEN) { /* Fast path */
751 if (dccp_rcv_established(sk, skb, dccp_hdr(skb), skb->len))
754 /* XXX This is where we would goto ipv6_pktoptions. */
755 __kfree_skb(opt_skb);
761 * Step 3: Process LISTEN state
762 * If S.state == LISTEN,
763 * If P.type == Request or P contains a valid Init Cookie option,
764 * (* Must scan the packet's options to check for Init
765 * Cookies. Only Init Cookies are processed here,
766 * however; other options are processed in Step 8. This
767 * scan need only be performed if the endpoint uses Init
769 * (* Generate a new socket and switch to that socket *)
770 * Set S := new socket for this port pair
772 * Choose S.ISS (initial seqno) or set from Init Cookies
773 * Initialize S.GAR := S.ISS
774 * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init Cookies
775 * Continue with S.state == RESPOND
776 * (* A Response packet will be generated in Step 11 *)
778 * Generate Reset(No Connection) unless P.type == Reset
779 * Drop packet and return
781 * NOTE: the check for the packet types is done in
782 * dccp_rcv_state_process
784 if (sk->sk_state == DCCP_LISTEN) {
785 struct sock *nsk = dccp_v6_hnd_req(sk, skb);
790 * Queue it on the new socket if the new socket is active,
791 * otherwise we just shortcircuit this and continue with
795 if (dccp_child_process(sk, nsk, skb))
798 __kfree_skb(opt_skb);
803 if (dccp_rcv_state_process(sk, skb, dccp_hdr(skb), skb->len))
806 /* XXX This is where we would goto ipv6_pktoptions. */
807 __kfree_skb(opt_skb);
812 dccp_v6_ctl_send_reset(skb);
815 __kfree_skb(opt_skb);
820 static int dccp_v6_rcv(struct sk_buff **pskb)
822 const struct dccp_hdr *dh;
823 struct sk_buff *skb = *pskb;
827 /* Step 1: Check header basics */
829 if (dccp_invalid_packet(skb))
832 /* Step 1: If header checksum is incorrect, drop packet and return. */
833 if (dccp_v6_csum_finish(skb, &skb->nh.ipv6h->saddr,
834 &skb->nh.ipv6h->daddr)) {
835 LIMIT_NETDEBUG(KERN_WARNING
836 "%s: dropped packet with invalid checksum\n",
843 DCCP_SKB_CB(skb)->dccpd_seq = dccp_hdr_seq(skb);
844 DCCP_SKB_CB(skb)->dccpd_type = dh->dccph_type;
846 if (dccp_packet_without_ack(skb))
847 DCCP_SKB_CB(skb)->dccpd_ack_seq = DCCP_PKT_WITHOUT_ACK_SEQ;
849 DCCP_SKB_CB(skb)->dccpd_ack_seq = dccp_hdr_ack_seq(skb);
852 * Look up flow ID in table and get corresponding socket */
853 sk = __inet6_lookup(&dccp_hashinfo, &skb->nh.ipv6h->saddr,
855 &skb->nh.ipv6h->daddr, ntohs(dh->dccph_dport),
862 dccp_pr_debug("failed to look up flow ID in table and "
863 "get corresponding socket\n");
869 * ... or S.state == TIMEWAIT,
870 * Generate Reset(No Connection) unless P.type == Reset
871 * Drop packet and return
873 if (sk->sk_state == DCCP_TIME_WAIT) {
874 dccp_pr_debug("sk->sk_state == DCCP_TIME_WAIT: do_time_wait\n");
875 inet_twsk_put(inet_twsk(sk));
880 * RFC 4340, sec. 9.2.1: Minimum Checksum Coverage
881 * o if MinCsCov = 0, only packets with CsCov = 0 are accepted
882 * o if MinCsCov > 0, also accept packets with CsCov >= MinCsCov
884 min_cov = dccp_sk(sk)->dccps_pcrlen;
885 if (dh->dccph_cscov && (min_cov == 0 || dh->dccph_cscov < min_cov)) {
886 dccp_pr_debug("Packet CsCov %d does not satisfy MinCsCov %d\n",
887 dh->dccph_cscov, min_cov);
888 /* FIXME: send Data Dropped option (see also dccp_v4_rcv) */
889 goto discard_and_relse;
892 if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb))
893 goto discard_and_relse;
895 return sk_receive_skb(sk, skb) ? -1 : 0;
898 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
903 * Generate Reset(No Connection) unless P.type == Reset
904 * Drop packet and return
906 if (dh->dccph_type != DCCP_PKT_RESET) {
907 DCCP_SKB_CB(skb)->dccpd_reset_code =
908 DCCP_RESET_CODE_NO_CONNECTION;
909 dccp_v6_ctl_send_reset(skb);
921 static int dccp_v6_connect(struct sock *sk, struct sockaddr *uaddr,
924 struct sockaddr_in6 *usin = (struct sockaddr_in6 *)uaddr;
925 struct inet_connection_sock *icsk = inet_csk(sk);
926 struct inet_sock *inet = inet_sk(sk);
927 struct ipv6_pinfo *np = inet6_sk(sk);
928 struct dccp_sock *dp = dccp_sk(sk);
929 struct in6_addr *saddr = NULL, *final_p = NULL, final;
931 struct dst_entry *dst;
935 dp->dccps_role = DCCP_ROLE_CLIENT;
937 if (addr_len < SIN6_LEN_RFC2133)
940 if (usin->sin6_family != AF_INET6)
941 return -EAFNOSUPPORT;
943 memset(&fl, 0, sizeof(fl));
946 fl.fl6_flowlabel = usin->sin6_flowinfo & IPV6_FLOWINFO_MASK;
947 IP6_ECN_flow_init(fl.fl6_flowlabel);
948 if (fl.fl6_flowlabel & IPV6_FLOWLABEL_MASK) {
949 struct ip6_flowlabel *flowlabel;
950 flowlabel = fl6_sock_lookup(sk, fl.fl6_flowlabel);
951 if (flowlabel == NULL)
953 ipv6_addr_copy(&usin->sin6_addr, &flowlabel->dst);
954 fl6_sock_release(flowlabel);
958 * connect() to INADDR_ANY means loopback (BSD'ism).
960 if (ipv6_addr_any(&usin->sin6_addr))
961 usin->sin6_addr.s6_addr[15] = 1;
963 addr_type = ipv6_addr_type(&usin->sin6_addr);
965 if (addr_type & IPV6_ADDR_MULTICAST)
968 if (addr_type & IPV6_ADDR_LINKLOCAL) {
969 if (addr_len >= sizeof(struct sockaddr_in6) &&
970 usin->sin6_scope_id) {
971 /* If interface is set while binding, indices
974 if (sk->sk_bound_dev_if &&
975 sk->sk_bound_dev_if != usin->sin6_scope_id)
978 sk->sk_bound_dev_if = usin->sin6_scope_id;
981 /* Connect to link-local address requires an interface */
982 if (!sk->sk_bound_dev_if)
986 ipv6_addr_copy(&np->daddr, &usin->sin6_addr);
987 np->flow_label = fl.fl6_flowlabel;
992 if (addr_type == IPV6_ADDR_MAPPED) {
993 u32 exthdrlen = icsk->icsk_ext_hdr_len;
994 struct sockaddr_in sin;
996 SOCK_DEBUG(sk, "connect: ipv4 mapped\n");
998 if (__ipv6_only_sock(sk))
1001 sin.sin_family = AF_INET;
1002 sin.sin_port = usin->sin6_port;
1003 sin.sin_addr.s_addr = usin->sin6_addr.s6_addr32[3];
1005 icsk->icsk_af_ops = &dccp_ipv6_mapped;
1006 sk->sk_backlog_rcv = dccp_v4_do_rcv;
1008 err = dccp_v4_connect(sk, (struct sockaddr *)&sin, sizeof(sin));
1010 icsk->icsk_ext_hdr_len = exthdrlen;
1011 icsk->icsk_af_ops = &dccp_ipv6_af_ops;
1012 sk->sk_backlog_rcv = dccp_v6_do_rcv;
1015 ipv6_addr_set(&np->saddr, 0, 0, htonl(0x0000FFFF),
1017 ipv6_addr_set(&np->rcv_saddr, 0, 0, htonl(0x0000FFFF),
1024 if (!ipv6_addr_any(&np->rcv_saddr))
1025 saddr = &np->rcv_saddr;
1027 fl.proto = IPPROTO_DCCP;
1028 ipv6_addr_copy(&fl.fl6_dst, &np->daddr);
1029 ipv6_addr_copy(&fl.fl6_src, saddr ? saddr : &np->saddr);
1030 fl.oif = sk->sk_bound_dev_if;
1031 fl.fl_ip_dport = usin->sin6_port;
1032 fl.fl_ip_sport = inet->sport;
1033 security_sk_classify_flow(sk, &fl);
1035 if (np->opt != NULL && np->opt->srcrt != NULL) {
1036 const struct rt0_hdr *rt0 = (struct rt0_hdr *)np->opt->srcrt;
1038 ipv6_addr_copy(&final, &fl.fl6_dst);
1039 ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
1043 err = ip6_dst_lookup(sk, &dst, &fl);
1048 ipv6_addr_copy(&fl.fl6_dst, final_p);
1050 err = xfrm_lookup(&dst, &fl, sk, 0);
1054 if (saddr == NULL) {
1055 saddr = &fl.fl6_src;
1056 ipv6_addr_copy(&np->rcv_saddr, saddr);
1059 /* set the source address */
1060 ipv6_addr_copy(&np->saddr, saddr);
1061 inet->rcv_saddr = LOOPBACK4_IPV6;
1063 __ip6_dst_store(sk, dst, NULL, NULL);
1065 icsk->icsk_ext_hdr_len = 0;
1066 if (np->opt != NULL)
1067 icsk->icsk_ext_hdr_len = (np->opt->opt_flen +
1068 np->opt->opt_nflen);
1070 inet->dport = usin->sin6_port;
1072 dccp_set_state(sk, DCCP_REQUESTING);
1073 err = inet6_hash_connect(&dccp_death_row, sk);
1078 dp->dccps_gar = secure_dccp_v6_sequence_number(np->saddr.s6_addr32,
1079 np->daddr.s6_addr32,
1083 err = dccp_connect(sk);
1090 dccp_set_state(sk, DCCP_CLOSED);
1094 sk->sk_route_caps = 0;
1098 static struct inet_connection_sock_af_ops dccp_ipv6_af_ops = {
1099 .queue_xmit = inet6_csk_xmit,
1100 .send_check = dccp_v6_send_check,
1101 .rebuild_header = inet6_sk_rebuild_header,
1102 .conn_request = dccp_v6_conn_request,
1103 .syn_recv_sock = dccp_v6_request_recv_sock,
1104 .net_header_len = sizeof(struct ipv6hdr),
1105 .setsockopt = ipv6_setsockopt,
1106 .getsockopt = ipv6_getsockopt,
1107 .addr2sockaddr = inet6_csk_addr2sockaddr,
1108 .sockaddr_len = sizeof(struct sockaddr_in6),
1109 #ifdef CONFIG_COMPAT
1110 .compat_setsockopt = compat_ipv6_setsockopt,
1111 .compat_getsockopt = compat_ipv6_getsockopt,
1116 * DCCP over IPv4 via INET6 API
1118 static struct inet_connection_sock_af_ops dccp_ipv6_mapped = {
1119 .queue_xmit = ip_queue_xmit,
1120 .send_check = dccp_v4_send_check,
1121 .rebuild_header = inet_sk_rebuild_header,
1122 .conn_request = dccp_v6_conn_request,
1123 .syn_recv_sock = dccp_v6_request_recv_sock,
1124 .net_header_len = sizeof(struct iphdr),
1125 .setsockopt = ipv6_setsockopt,
1126 .getsockopt = ipv6_getsockopt,
1127 .addr2sockaddr = inet6_csk_addr2sockaddr,
1128 .sockaddr_len = sizeof(struct sockaddr_in6),
1129 #ifdef CONFIG_COMPAT
1130 .compat_setsockopt = compat_ipv6_setsockopt,
1131 .compat_getsockopt = compat_ipv6_getsockopt,
1135 /* NOTE: A lot of things set to zero explicitly by call to
1136 * sk_alloc() so need not be done here.
1138 static int dccp_v6_init_sock(struct sock *sk)
1140 static __u8 dccp_v6_ctl_sock_initialized;
1141 int err = dccp_init_sock(sk, dccp_v6_ctl_sock_initialized);
1144 if (unlikely(!dccp_v6_ctl_sock_initialized))
1145 dccp_v6_ctl_sock_initialized = 1;
1146 inet_csk(sk)->icsk_af_ops = &dccp_ipv6_af_ops;
1152 static int dccp_v6_destroy_sock(struct sock *sk)
1154 dccp_destroy_sock(sk);
1155 return inet6_destroy_sock(sk);
1158 static struct timewait_sock_ops dccp6_timewait_sock_ops = {
1159 .twsk_obj_size = sizeof(struct dccp6_timewait_sock),
1162 static struct proto dccp_v6_prot = {
1164 .owner = THIS_MODULE,
1165 .close = dccp_close,
1166 .connect = dccp_v6_connect,
1167 .disconnect = dccp_disconnect,
1168 .ioctl = dccp_ioctl,
1169 .init = dccp_v6_init_sock,
1170 .setsockopt = dccp_setsockopt,
1171 .getsockopt = dccp_getsockopt,
1172 .sendmsg = dccp_sendmsg,
1173 .recvmsg = dccp_recvmsg,
1174 .backlog_rcv = dccp_v6_do_rcv,
1175 .hash = dccp_v6_hash,
1176 .unhash = dccp_unhash,
1177 .accept = inet_csk_accept,
1178 .get_port = dccp_v6_get_port,
1179 .shutdown = dccp_shutdown,
1180 .destroy = dccp_v6_destroy_sock,
1181 .orphan_count = &dccp_orphan_count,
1182 .max_header = MAX_DCCP_HEADER,
1183 .obj_size = sizeof(struct dccp6_sock),
1184 .rsk_prot = &dccp6_request_sock_ops,
1185 .twsk_prot = &dccp6_timewait_sock_ops,
1186 #ifdef CONFIG_COMPAT
1187 .compat_setsockopt = compat_dccp_setsockopt,
1188 .compat_getsockopt = compat_dccp_getsockopt,
1192 static struct inet6_protocol dccp_v6_protocol = {
1193 .handler = dccp_v6_rcv,
1194 .err_handler = dccp_v6_err,
1195 .flags = INET6_PROTO_NOPOLICY | INET6_PROTO_FINAL,
1198 static struct proto_ops inet6_dccp_ops = {
1200 .owner = THIS_MODULE,
1201 .release = inet6_release,
1203 .connect = inet_stream_connect,
1204 .socketpair = sock_no_socketpair,
1205 .accept = inet_accept,
1206 .getname = inet6_getname,
1208 .ioctl = inet6_ioctl,
1209 .listen = inet_dccp_listen,
1210 .shutdown = inet_shutdown,
1211 .setsockopt = sock_common_setsockopt,
1212 .getsockopt = sock_common_getsockopt,
1213 .sendmsg = inet_sendmsg,
1214 .recvmsg = sock_common_recvmsg,
1215 .mmap = sock_no_mmap,
1216 .sendpage = sock_no_sendpage,
1217 #ifdef CONFIG_COMPAT
1218 .compat_setsockopt = compat_sock_common_setsockopt,
1219 .compat_getsockopt = compat_sock_common_getsockopt,
1223 static struct inet_protosw dccp_v6_protosw = {
1225 .protocol = IPPROTO_DCCP,
1226 .prot = &dccp_v6_prot,
1227 .ops = &inet6_dccp_ops,
1229 .flags = INET_PROTOSW_ICSK,
1232 static int __init dccp_v6_init(void)
1234 int err = proto_register(&dccp_v6_prot, 1);
1239 err = inet6_add_protocol(&dccp_v6_protocol, IPPROTO_DCCP);
1241 goto out_unregister_proto;
1243 inet6_register_protosw(&dccp_v6_protosw);
1245 err = inet_csk_ctl_sock_create(&dccp_v6_ctl_socket, PF_INET6,
1246 SOCK_DCCP, IPPROTO_DCCP);
1248 goto out_unregister_protosw;
1251 out_unregister_protosw:
1252 inet6_del_protocol(&dccp_v6_protocol, IPPROTO_DCCP);
1253 inet6_unregister_protosw(&dccp_v6_protosw);
1254 out_unregister_proto:
1255 proto_unregister(&dccp_v6_prot);
1259 static void __exit dccp_v6_exit(void)
1261 inet6_del_protocol(&dccp_v6_protocol, IPPROTO_DCCP);
1262 inet6_unregister_protosw(&dccp_v6_protosw);
1263 proto_unregister(&dccp_v6_prot);
1266 module_init(dccp_v6_init);
1267 module_exit(dccp_v6_exit);
1270 * __stringify doesn't likes enums, so use SOCK_DCCP (6) and IPPROTO_DCCP (33)
1271 * values directly, Also cover the case where the protocol is not specified,
1272 * i.e. net-pf-PF_INET6-proto-0-type-SOCK_DCCP
1274 MODULE_ALIAS("net-pf-" __stringify(PF_INET6) "-proto-33-type-6");
1275 MODULE_ALIAS("net-pf-" __stringify(PF_INET6) "-proto-0-type-6");
1276 MODULE_LICENSE("GPL");
1277 MODULE_AUTHOR("Arnaldo Carvalho de Melo <acme@mandriva.com>");
1278 MODULE_DESCRIPTION("DCCPv6 - Datagram Congestion Controlled Protocol");