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 static inline u16 dccp_v6_check(struct dccp_hdr *dh, int len,
62 struct in6_addr *saddr,
63 struct in6_addr *daddr,
66 return csum_ipv6_magic(saddr, daddr, len, IPPROTO_DCCP, base);
69 static __u32 dccp_v6_init_sequence(struct sock *sk, struct sk_buff *skb)
71 const struct dccp_hdr *dh = dccp_hdr(skb);
73 if (skb->protocol == htons(ETH_P_IPV6))
74 return secure_tcpv6_sequence_number(skb->nh.ipv6h->daddr.s6_addr32,
75 skb->nh.ipv6h->saddr.s6_addr32,
79 return secure_dccp_sequence_number(skb->nh.iph->daddr,
85 static void dccp_v6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
86 int type, int code, int offset, __be32 info)
88 struct ipv6hdr *hdr = (struct ipv6hdr *)skb->data;
89 const struct dccp_hdr *dh = (struct dccp_hdr *)(skb->data + offset);
90 struct ipv6_pinfo *np;
95 sk = inet6_lookup(&dccp_hashinfo, &hdr->daddr, dh->dccph_dport,
96 &hdr->saddr, dh->dccph_sport, inet6_iif(skb));
99 ICMP6_INC_STATS_BH(__in6_dev_get(skb->dev), ICMP6_MIB_INERRORS);
103 if (sk->sk_state == DCCP_TIME_WAIT) {
104 inet_twsk_put(inet_twsk(sk));
109 if (sock_owned_by_user(sk))
110 NET_INC_STATS_BH(LINUX_MIB_LOCKDROPPEDICMPS);
112 if (sk->sk_state == DCCP_CLOSED)
117 if (type == ICMPV6_PKT_TOOBIG) {
118 struct dst_entry *dst = NULL;
120 if (sock_owned_by_user(sk))
122 if ((1 << sk->sk_state) & (DCCPF_LISTEN | DCCPF_CLOSED))
125 /* icmp should have updated the destination cache entry */
126 dst = __sk_dst_check(sk, np->dst_cookie);
128 struct inet_sock *inet = inet_sk(sk);
131 /* BUGGG_FUTURE: Again, it is not clear how
132 to handle rthdr case. Ignore this complexity
135 memset(&fl, 0, sizeof(fl));
136 fl.proto = IPPROTO_DCCP;
137 ipv6_addr_copy(&fl.fl6_dst, &np->daddr);
138 ipv6_addr_copy(&fl.fl6_src, &np->saddr);
139 fl.oif = sk->sk_bound_dev_if;
140 fl.fl_ip_dport = inet->dport;
141 fl.fl_ip_sport = inet->sport;
142 security_sk_classify_flow(sk, &fl);
144 err = ip6_dst_lookup(sk, &dst, &fl);
146 sk->sk_err_soft = -err;
150 err = xfrm_lookup(&dst, &fl, sk, 0);
152 sk->sk_err_soft = -err;
158 if (inet_csk(sk)->icsk_pmtu_cookie > dst_mtu(dst)) {
159 dccp_sync_mss(sk, dst_mtu(dst));
160 } /* else let the usual retransmit timer handle it */
165 icmpv6_err_convert(type, code, &err);
167 seq = DCCP_SKB_CB(skb)->dccpd_seq;
168 /* Might be for an request_sock */
169 switch (sk->sk_state) {
170 struct request_sock *req, **prev;
172 if (sock_owned_by_user(sk))
175 req = inet6_csk_search_req(sk, &prev, dh->dccph_dport,
176 &hdr->daddr, &hdr->saddr,
182 * ICMPs are not backlogged, hence we cannot get an established
185 BUG_TRAP(req->sk == NULL);
187 if (seq != dccp_rsk(req)->dreq_iss) {
188 NET_INC_STATS_BH(LINUX_MIB_OUTOFWINDOWICMPS);
192 inet_csk_reqsk_queue_drop(sk, req, prev);
195 case DCCP_REQUESTING:
196 case DCCP_RESPOND: /* Cannot happen.
197 It can, it SYNs are crossed. --ANK */
198 if (!sock_owned_by_user(sk)) {
199 DCCP_INC_STATS_BH(DCCP_MIB_ATTEMPTFAILS);
202 * Wake people up to see the error
203 * (see connect in sock.c)
205 sk->sk_error_report(sk);
208 sk->sk_err_soft = err;
212 if (!sock_owned_by_user(sk) && np->recverr) {
214 sk->sk_error_report(sk);
216 sk->sk_err_soft = err;
224 static int dccp_v6_send_response(struct sock *sk, struct request_sock *req,
225 struct dst_entry *dst)
227 struct inet6_request_sock *ireq6 = inet6_rsk(req);
228 struct ipv6_pinfo *np = inet6_sk(sk);
230 struct ipv6_txoptions *opt = NULL;
231 struct in6_addr *final_p = NULL, final;
235 memset(&fl, 0, sizeof(fl));
236 fl.proto = IPPROTO_DCCP;
237 ipv6_addr_copy(&fl.fl6_dst, &ireq6->rmt_addr);
238 ipv6_addr_copy(&fl.fl6_src, &ireq6->loc_addr);
239 fl.fl6_flowlabel = 0;
241 fl.fl_ip_dport = inet_rsk(req)->rmt_port;
242 fl.fl_ip_sport = inet_sk(sk)->sport;
243 security_req_classify_flow(req, &fl);
248 np->rxopt.bits.osrcrt == 2 &&
250 struct sk_buff *pktopts = ireq6->pktopts;
251 struct inet6_skb_parm *rxopt = IP6CB(pktopts);
254 opt = ipv6_invert_rthdr(sk,
255 (struct ipv6_rt_hdr *)(pktopts->nh.raw +
259 if (opt != NULL && opt->srcrt != NULL) {
260 const struct rt0_hdr *rt0 = (struct rt0_hdr *)opt->srcrt;
262 ipv6_addr_copy(&final, &fl.fl6_dst);
263 ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
267 err = ip6_dst_lookup(sk, &dst, &fl);
272 ipv6_addr_copy(&fl.fl6_dst, final_p);
274 err = xfrm_lookup(&dst, &fl, sk, 0);
279 skb = dccp_make_response(sk, dst, req);
281 struct dccp_hdr *dh = dccp_hdr(skb);
283 dh->dccph_checksum = dccp_v6_check(dh, skb->len,
286 csum_partial((char *)dh,
289 ipv6_addr_copy(&fl.fl6_dst, &ireq6->rmt_addr);
290 err = ip6_xmit(sk, skb, &fl, opt, 0);
291 if (err == NET_XMIT_CN)
296 if (opt != NULL && opt != np->opt)
297 sock_kfree_s(sk, opt, opt->tot_len);
302 static void dccp_v6_reqsk_destructor(struct request_sock *req)
304 if (inet6_rsk(req)->pktopts != NULL)
305 kfree_skb(inet6_rsk(req)->pktopts);
308 static void dccp_v6_send_check(struct sock *sk, int len, struct sk_buff *skb)
310 struct ipv6_pinfo *np = inet6_sk(sk);
311 struct dccp_hdr *dh = dccp_hdr(skb);
313 dh->dccph_checksum = csum_ipv6_magic(&np->saddr, &np->daddr,
315 csum_partial((char *)dh,
320 static void dccp_v6_ctl_send_reset(struct sk_buff *rxskb)
322 struct dccp_hdr *rxdh = dccp_hdr(rxskb), *dh;
323 const u32 dccp_hdr_reset_len = sizeof(struct dccp_hdr) +
324 sizeof(struct dccp_hdr_ext) +
325 sizeof(struct dccp_hdr_reset);
330 if (rxdh->dccph_type == DCCP_PKT_RESET)
333 if (!ipv6_unicast_destination(rxskb))
336 skb = alloc_skb(dccp_v6_ctl_socket->sk->sk_prot->max_header,
341 skb_reserve(skb, dccp_v6_ctl_socket->sk->sk_prot->max_header);
343 dh = dccp_zeroed_hdr(skb, dccp_hdr_reset_len);
345 /* Swap the send and the receive. */
346 dh->dccph_type = DCCP_PKT_RESET;
347 dh->dccph_sport = rxdh->dccph_dport;
348 dh->dccph_dport = rxdh->dccph_sport;
349 dh->dccph_doff = dccp_hdr_reset_len / 4;
351 dccp_hdr_reset(skb)->dccph_reset_code =
352 DCCP_SKB_CB(rxskb)->dccpd_reset_code;
354 /* See "8.3.1. Abnormal Termination" in RFC 4340 */
356 if (DCCP_SKB_CB(rxskb)->dccpd_ack_seq != DCCP_PKT_WITHOUT_ACK_SEQ)
357 dccp_set_seqno(&seqno, DCCP_SKB_CB(rxskb)->dccpd_ack_seq + 1);
359 dccp_hdr_set_seq(dh, seqno);
360 dccp_hdr_set_ack(dccp_hdr_ack_bits(skb),
361 DCCP_SKB_CB(rxskb)->dccpd_seq);
363 memset(&fl, 0, sizeof(fl));
364 ipv6_addr_copy(&fl.fl6_dst, &rxskb->nh.ipv6h->saddr);
365 ipv6_addr_copy(&fl.fl6_src, &rxskb->nh.ipv6h->daddr);
366 dh->dccph_checksum = csum_ipv6_magic(&fl.fl6_src, &fl.fl6_dst,
367 sizeof(*dh), IPPROTO_DCCP,
369 fl.proto = IPPROTO_DCCP;
370 fl.oif = inet6_iif(rxskb);
371 fl.fl_ip_dport = dh->dccph_dport;
372 fl.fl_ip_sport = dh->dccph_sport;
373 security_skb_classify_flow(rxskb, &fl);
375 /* sk = NULL, but it is safe for now. RST socket required. */
376 if (!ip6_dst_lookup(NULL, &skb->dst, &fl)) {
377 if (xfrm_lookup(&skb->dst, &fl, NULL, 0) >= 0) {
378 ip6_xmit(dccp_v6_ctl_socket->sk, skb, &fl, NULL, 0);
379 DCCP_INC_STATS_BH(DCCP_MIB_OUTSEGS);
380 DCCP_INC_STATS_BH(DCCP_MIB_OUTRSTS);
388 static struct request_sock_ops dccp6_request_sock_ops = {
390 .obj_size = sizeof(struct dccp6_request_sock),
391 .rtx_syn_ack = dccp_v6_send_response,
392 .send_ack = dccp_reqsk_send_ack,
393 .destructor = dccp_v6_reqsk_destructor,
394 .send_reset = dccp_v6_ctl_send_reset,
397 static struct sock *dccp_v6_hnd_req(struct sock *sk,struct sk_buff *skb)
399 const struct dccp_hdr *dh = dccp_hdr(skb);
400 const struct ipv6hdr *iph = skb->nh.ipv6h;
402 struct request_sock **prev;
403 /* Find possible connection requests. */
404 struct request_sock *req = inet6_csk_search_req(sk, &prev,
410 return dccp_check_req(sk, skb, req, prev);
412 nsk = __inet6_lookup_established(&dccp_hashinfo,
413 &iph->saddr, dh->dccph_sport,
414 &iph->daddr, ntohs(dh->dccph_dport),
417 if (nsk->sk_state != DCCP_TIME_WAIT) {
421 inet_twsk_put(inet_twsk(nsk));
428 static int dccp_v6_conn_request(struct sock *sk, struct sk_buff *skb)
430 struct request_sock *req;
431 struct dccp_request_sock *dreq;
432 struct inet6_request_sock *ireq6;
433 struct ipv6_pinfo *np = inet6_sk(sk);
434 const __be32 service = dccp_hdr_request(skb)->dccph_req_service;
435 struct dccp_skb_cb *dcb = DCCP_SKB_CB(skb);
436 __u8 reset_code = DCCP_RESET_CODE_TOO_BUSY;
438 if (skb->protocol == htons(ETH_P_IP))
439 return dccp_v4_conn_request(sk, skb);
441 if (!ipv6_unicast_destination(skb))
444 if (dccp_bad_service_code(sk, service)) {
445 reset_code = DCCP_RESET_CODE_BAD_SERVICE_CODE;
449 * There are no SYN attacks on IPv6, yet...
451 if (inet_csk_reqsk_queue_is_full(sk))
454 if (sk_acceptq_is_full(sk) && inet_csk_reqsk_queue_young(sk) > 1)
457 req = inet6_reqsk_alloc(&dccp6_request_sock_ops);
461 if (dccp_parse_options(sk, skb))
464 dccp_reqsk_init(req, skb);
466 if (security_inet_conn_request(sk, skb, req))
469 ireq6 = inet6_rsk(req);
470 ipv6_addr_copy(&ireq6->rmt_addr, &skb->nh.ipv6h->saddr);
471 ipv6_addr_copy(&ireq6->loc_addr, &skb->nh.ipv6h->daddr);
472 ireq6->pktopts = NULL;
474 if (ipv6_opt_accepted(sk, skb) ||
475 np->rxopt.bits.rxinfo || np->rxopt.bits.rxoinfo ||
476 np->rxopt.bits.rxhlim || np->rxopt.bits.rxohlim) {
477 atomic_inc(&skb->users);
478 ireq6->pktopts = skb;
480 ireq6->iif = sk->sk_bound_dev_if;
482 /* So that link locals have meaning */
483 if (!sk->sk_bound_dev_if &&
484 ipv6_addr_type(&ireq6->rmt_addr) & IPV6_ADDR_LINKLOCAL)
485 ireq6->iif = inet6_iif(skb);
488 * Step 3: Process LISTEN state
490 * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init Cookie
492 * In fact we defer setting S.GSR, S.SWL, S.SWH to
493 * dccp_create_openreq_child.
495 dreq = dccp_rsk(req);
496 dreq->dreq_isr = dcb->dccpd_seq;
497 dreq->dreq_iss = dccp_v6_init_sequence(sk, skb);
498 dreq->dreq_service = service;
500 if (dccp_v6_send_response(sk, req, NULL))
503 inet6_csk_reqsk_queue_hash_add(sk, req, DCCP_TIMEOUT_INIT);
509 DCCP_INC_STATS_BH(DCCP_MIB_ATTEMPTFAILS);
510 dcb->dccpd_reset_code = reset_code;
514 static struct sock *dccp_v6_request_recv_sock(struct sock *sk,
516 struct request_sock *req,
517 struct dst_entry *dst)
519 struct inet6_request_sock *ireq6 = inet6_rsk(req);
520 struct ipv6_pinfo *newnp, *np = inet6_sk(sk);
521 struct inet_sock *newinet;
522 struct dccp_sock *newdp;
523 struct dccp6_sock *newdp6;
525 struct ipv6_txoptions *opt;
527 if (skb->protocol == htons(ETH_P_IP)) {
531 newsk = dccp_v4_request_recv_sock(sk, skb, req, dst);
535 newdp6 = (struct dccp6_sock *)newsk;
536 newdp = dccp_sk(newsk);
537 newinet = inet_sk(newsk);
538 newinet->pinet6 = &newdp6->inet6;
539 newnp = inet6_sk(newsk);
541 memcpy(newnp, np, sizeof(struct ipv6_pinfo));
543 ipv6_addr_set(&newnp->daddr, 0, 0, htonl(0x0000FFFF),
546 ipv6_addr_set(&newnp->saddr, 0, 0, htonl(0x0000FFFF),
549 ipv6_addr_copy(&newnp->rcv_saddr, &newnp->saddr);
551 inet_csk(newsk)->icsk_af_ops = &dccp_ipv6_mapped;
552 newsk->sk_backlog_rcv = dccp_v4_do_rcv;
553 newnp->pktoptions = NULL;
555 newnp->mcast_oif = inet6_iif(skb);
556 newnp->mcast_hops = skb->nh.ipv6h->hop_limit;
559 * No need to charge this sock to the relevant IPv6 refcnt debug socks count
560 * here, dccp_create_openreq_child now does this for us, see the comment in
561 * that function for the gory details. -acme
564 /* It is tricky place. Until this moment IPv4 tcp
565 worked with IPv6 icsk.icsk_af_ops.
568 dccp_sync_mss(newsk, inet_csk(newsk)->icsk_pmtu_cookie);
575 if (sk_acceptq_is_full(sk))
578 if (np->rxopt.bits.osrcrt == 2 && opt == NULL && ireq6->pktopts) {
579 const struct inet6_skb_parm *rxopt = IP6CB(ireq6->pktopts);
582 opt = ipv6_invert_rthdr(sk,
583 (struct ipv6_rt_hdr *)(ireq6->pktopts->nh.raw +
588 struct in6_addr *final_p = NULL, final;
591 memset(&fl, 0, sizeof(fl));
592 fl.proto = IPPROTO_DCCP;
593 ipv6_addr_copy(&fl.fl6_dst, &ireq6->rmt_addr);
594 if (opt != NULL && opt->srcrt != NULL) {
595 const struct rt0_hdr *rt0 = (struct rt0_hdr *)opt->srcrt;
597 ipv6_addr_copy(&final, &fl.fl6_dst);
598 ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
601 ipv6_addr_copy(&fl.fl6_src, &ireq6->loc_addr);
602 fl.oif = sk->sk_bound_dev_if;
603 fl.fl_ip_dport = inet_rsk(req)->rmt_port;
604 fl.fl_ip_sport = inet_sk(sk)->sport;
605 security_sk_classify_flow(sk, &fl);
607 if (ip6_dst_lookup(sk, &dst, &fl))
611 ipv6_addr_copy(&fl.fl6_dst, final_p);
613 if ((xfrm_lookup(&dst, &fl, sk, 0)) < 0)
617 newsk = dccp_create_openreq_child(sk, req, skb);
622 * No need to charge this sock to the relevant IPv6 refcnt debug socks
623 * count here, dccp_create_openreq_child now does this for us, see the
624 * comment in that function for the gory details. -acme
627 __ip6_dst_store(newsk, dst, NULL, NULL);
628 newsk->sk_route_caps = dst->dev->features & ~(NETIF_F_IP_CSUM |
630 newdp6 = (struct dccp6_sock *)newsk;
631 newinet = inet_sk(newsk);
632 newinet->pinet6 = &newdp6->inet6;
633 newdp = dccp_sk(newsk);
634 newnp = inet6_sk(newsk);
636 memcpy(newnp, np, sizeof(struct ipv6_pinfo));
638 ipv6_addr_copy(&newnp->daddr, &ireq6->rmt_addr);
639 ipv6_addr_copy(&newnp->saddr, &ireq6->loc_addr);
640 ipv6_addr_copy(&newnp->rcv_saddr, &ireq6->loc_addr);
641 newsk->sk_bound_dev_if = ireq6->iif;
643 /* Now IPv6 options...
645 First: no IPv4 options.
650 newnp->rxopt.all = np->rxopt.all;
652 /* Clone pktoptions received with SYN */
653 newnp->pktoptions = NULL;
654 if (ireq6->pktopts != NULL) {
655 newnp->pktoptions = skb_clone(ireq6->pktopts, GFP_ATOMIC);
656 kfree_skb(ireq6->pktopts);
657 ireq6->pktopts = NULL;
658 if (newnp->pktoptions)
659 skb_set_owner_r(newnp->pktoptions, newsk);
662 newnp->mcast_oif = inet6_iif(skb);
663 newnp->mcast_hops = skb->nh.ipv6h->hop_limit;
666 * Clone native IPv6 options from listening socket (if any)
668 * Yes, keeping reference count would be much more clever, but we make
669 * one more one thing there: reattach optmem to newsk.
672 newnp->opt = ipv6_dup_options(newsk, opt);
674 sock_kfree_s(sk, opt, opt->tot_len);
677 inet_csk(newsk)->icsk_ext_hdr_len = 0;
678 if (newnp->opt != NULL)
679 inet_csk(newsk)->icsk_ext_hdr_len = (newnp->opt->opt_nflen +
680 newnp->opt->opt_flen);
682 dccp_sync_mss(newsk, dst_mtu(dst));
684 newinet->daddr = newinet->saddr = newinet->rcv_saddr = LOOPBACK4_IPV6;
686 __inet6_hash(&dccp_hashinfo, newsk);
687 inet_inherit_port(&dccp_hashinfo, sk, newsk);
692 NET_INC_STATS_BH(LINUX_MIB_LISTENOVERFLOWS);
694 NET_INC_STATS_BH(LINUX_MIB_LISTENDROPS);
695 if (opt != NULL && opt != np->opt)
696 sock_kfree_s(sk, opt, opt->tot_len);
701 /* The socket must have it's spinlock held when we get
704 * We have a potential double-lock case here, so even when
705 * doing backlog processing we use the BH locking scheme.
706 * This is because we cannot sleep with the original spinlock
709 static int dccp_v6_do_rcv(struct sock *sk, struct sk_buff *skb)
711 struct ipv6_pinfo *np = inet6_sk(sk);
712 struct sk_buff *opt_skb = NULL;
714 /* Imagine: socket is IPv6. IPv4 packet arrives,
715 goes to IPv4 receive handler and backlogged.
716 From backlog it always goes here. Kerboom...
717 Fortunately, dccp_rcv_established and rcv_established
718 handle them correctly, but it is not case with
719 dccp_v6_hnd_req and dccp_v6_ctl_send_reset(). --ANK
722 if (skb->protocol == htons(ETH_P_IP))
723 return dccp_v4_do_rcv(sk, skb);
725 if (sk_filter(sk, skb))
729 * socket locking is here for SMP purposes as backlog rcv is currently
730 * called with bh processing disabled.
733 /* Do Stevens' IPV6_PKTOPTIONS.
735 Yes, guys, it is the only place in our code, where we
736 may make it not affecting IPv4.
737 The rest of code is protocol independent,
738 and I do not like idea to uglify IPv4.
740 Actually, all the idea behind IPV6_PKTOPTIONS
741 looks not very well thought. For now we latch
742 options, received in the last packet, enqueued
743 by tcp. Feel free to propose better solution.
748 * FIXME: Add handling of IPV6_PKTOPTIONS skb. See the comments below
749 * (wrt ipv6_pktopions) and net/ipv6/tcp_ipv6.c for an example.
751 opt_skb = skb_clone(skb, GFP_ATOMIC);
753 if (sk->sk_state == DCCP_OPEN) { /* Fast path */
754 if (dccp_rcv_established(sk, skb, dccp_hdr(skb), skb->len))
757 /* XXX This is where we would goto ipv6_pktoptions. */
758 __kfree_skb(opt_skb);
764 * Step 3: Process LISTEN state
765 * If S.state == LISTEN,
766 * If P.type == Request or P contains a valid Init Cookie option,
767 * (* Must scan the packet's options to check for Init
768 * Cookies. Only Init Cookies are processed here,
769 * however; other options are processed in Step 8. This
770 * scan need only be performed if the endpoint uses Init
772 * (* Generate a new socket and switch to that socket *)
773 * Set S := new socket for this port pair
775 * Choose S.ISS (initial seqno) or set from Init Cookies
776 * Initialize S.GAR := S.ISS
777 * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init Cookies
778 * Continue with S.state == RESPOND
779 * (* A Response packet will be generated in Step 11 *)
781 * Generate Reset(No Connection) unless P.type == Reset
782 * Drop packet and return
784 * NOTE: the check for the packet types is done in
785 * dccp_rcv_state_process
787 if (sk->sk_state == DCCP_LISTEN) {
788 struct sock *nsk = dccp_v6_hnd_req(sk, skb);
793 * Queue it on the new socket if the new socket is active,
794 * otherwise we just shortcircuit this and continue with
798 if (dccp_child_process(sk, nsk, skb))
801 __kfree_skb(opt_skb);
806 if (dccp_rcv_state_process(sk, skb, dccp_hdr(skb), skb->len))
809 /* XXX This is where we would goto ipv6_pktoptions. */
810 __kfree_skb(opt_skb);
815 dccp_v6_ctl_send_reset(skb);
818 __kfree_skb(opt_skb);
823 static int dccp_v6_rcv(struct sk_buff **pskb)
825 const struct dccp_hdr *dh;
826 struct sk_buff *skb = *pskb;
829 /* Step 1: Check header basics: */
831 if (dccp_invalid_packet(skb))
836 DCCP_SKB_CB(skb)->dccpd_seq = dccp_hdr_seq(skb);
837 DCCP_SKB_CB(skb)->dccpd_type = dh->dccph_type;
839 if (dccp_packet_without_ack(skb))
840 DCCP_SKB_CB(skb)->dccpd_ack_seq = DCCP_PKT_WITHOUT_ACK_SEQ;
842 DCCP_SKB_CB(skb)->dccpd_ack_seq = dccp_hdr_ack_seq(skb);
845 * Look up flow ID in table and get corresponding socket */
846 sk = __inet6_lookup(&dccp_hashinfo, &skb->nh.ipv6h->saddr,
848 &skb->nh.ipv6h->daddr, ntohs(dh->dccph_dport),
855 dccp_pr_debug("failed to look up flow ID in table and "
856 "get corresponding socket\n");
862 * ... or S.state == TIMEWAIT,
863 * Generate Reset(No Connection) unless P.type == Reset
864 * Drop packet and return
866 if (sk->sk_state == DCCP_TIME_WAIT) {
867 dccp_pr_debug("sk->sk_state == DCCP_TIME_WAIT: do_time_wait\n");
868 inet_twsk_put(inet_twsk(sk));
872 if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb))
873 goto discard_and_relse;
875 return sk_receive_skb(sk, skb) ? -1 : 0;
878 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
883 * Generate Reset(No Connection) unless P.type == Reset
884 * Drop packet and return
886 if (dh->dccph_type != DCCP_PKT_RESET) {
887 DCCP_SKB_CB(skb)->dccpd_reset_code =
888 DCCP_RESET_CODE_NO_CONNECTION;
889 dccp_v6_ctl_send_reset(skb);
901 static int dccp_v6_connect(struct sock *sk, struct sockaddr *uaddr,
904 struct sockaddr_in6 *usin = (struct sockaddr_in6 *)uaddr;
905 struct inet_connection_sock *icsk = inet_csk(sk);
906 struct inet_sock *inet = inet_sk(sk);
907 struct ipv6_pinfo *np = inet6_sk(sk);
908 struct dccp_sock *dp = dccp_sk(sk);
909 struct in6_addr *saddr = NULL, *final_p = NULL, final;
911 struct dst_entry *dst;
915 dp->dccps_role = DCCP_ROLE_CLIENT;
917 if (addr_len < SIN6_LEN_RFC2133)
920 if (usin->sin6_family != AF_INET6)
921 return -EAFNOSUPPORT;
923 memset(&fl, 0, sizeof(fl));
926 fl.fl6_flowlabel = usin->sin6_flowinfo & IPV6_FLOWINFO_MASK;
927 IP6_ECN_flow_init(fl.fl6_flowlabel);
928 if (fl.fl6_flowlabel & IPV6_FLOWLABEL_MASK) {
929 struct ip6_flowlabel *flowlabel;
930 flowlabel = fl6_sock_lookup(sk, fl.fl6_flowlabel);
931 if (flowlabel == NULL)
933 ipv6_addr_copy(&usin->sin6_addr, &flowlabel->dst);
934 fl6_sock_release(flowlabel);
938 * connect() to INADDR_ANY means loopback (BSD'ism).
940 if (ipv6_addr_any(&usin->sin6_addr))
941 usin->sin6_addr.s6_addr[15] = 1;
943 addr_type = ipv6_addr_type(&usin->sin6_addr);
945 if (addr_type & IPV6_ADDR_MULTICAST)
948 if (addr_type & IPV6_ADDR_LINKLOCAL) {
949 if (addr_len >= sizeof(struct sockaddr_in6) &&
950 usin->sin6_scope_id) {
951 /* If interface is set while binding, indices
954 if (sk->sk_bound_dev_if &&
955 sk->sk_bound_dev_if != usin->sin6_scope_id)
958 sk->sk_bound_dev_if = usin->sin6_scope_id;
961 /* Connect to link-local address requires an interface */
962 if (!sk->sk_bound_dev_if)
966 ipv6_addr_copy(&np->daddr, &usin->sin6_addr);
967 np->flow_label = fl.fl6_flowlabel;
972 if (addr_type == IPV6_ADDR_MAPPED) {
973 u32 exthdrlen = icsk->icsk_ext_hdr_len;
974 struct sockaddr_in sin;
976 SOCK_DEBUG(sk, "connect: ipv4 mapped\n");
978 if (__ipv6_only_sock(sk))
981 sin.sin_family = AF_INET;
982 sin.sin_port = usin->sin6_port;
983 sin.sin_addr.s_addr = usin->sin6_addr.s6_addr32[3];
985 icsk->icsk_af_ops = &dccp_ipv6_mapped;
986 sk->sk_backlog_rcv = dccp_v4_do_rcv;
988 err = dccp_v4_connect(sk, (struct sockaddr *)&sin, sizeof(sin));
990 icsk->icsk_ext_hdr_len = exthdrlen;
991 icsk->icsk_af_ops = &dccp_ipv6_af_ops;
992 sk->sk_backlog_rcv = dccp_v6_do_rcv;
995 ipv6_addr_set(&np->saddr, 0, 0, htonl(0x0000FFFF),
997 ipv6_addr_set(&np->rcv_saddr, 0, 0, htonl(0x0000FFFF),
1004 if (!ipv6_addr_any(&np->rcv_saddr))
1005 saddr = &np->rcv_saddr;
1007 fl.proto = IPPROTO_DCCP;
1008 ipv6_addr_copy(&fl.fl6_dst, &np->daddr);
1009 ipv6_addr_copy(&fl.fl6_src, saddr ? saddr : &np->saddr);
1010 fl.oif = sk->sk_bound_dev_if;
1011 fl.fl_ip_dport = usin->sin6_port;
1012 fl.fl_ip_sport = inet->sport;
1013 security_sk_classify_flow(sk, &fl);
1015 if (np->opt != NULL && np->opt->srcrt != NULL) {
1016 const struct rt0_hdr *rt0 = (struct rt0_hdr *)np->opt->srcrt;
1018 ipv6_addr_copy(&final, &fl.fl6_dst);
1019 ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
1023 err = ip6_dst_lookup(sk, &dst, &fl);
1028 ipv6_addr_copy(&fl.fl6_dst, final_p);
1030 err = xfrm_lookup(&dst, &fl, sk, 0);
1034 if (saddr == NULL) {
1035 saddr = &fl.fl6_src;
1036 ipv6_addr_copy(&np->rcv_saddr, saddr);
1039 /* set the source address */
1040 ipv6_addr_copy(&np->saddr, saddr);
1041 inet->rcv_saddr = LOOPBACK4_IPV6;
1043 __ip6_dst_store(sk, dst, NULL, NULL);
1045 icsk->icsk_ext_hdr_len = 0;
1046 if (np->opt != NULL)
1047 icsk->icsk_ext_hdr_len = (np->opt->opt_flen +
1048 np->opt->opt_nflen);
1050 inet->dport = usin->sin6_port;
1052 dccp_set_state(sk, DCCP_REQUESTING);
1053 err = inet6_hash_connect(&dccp_death_row, sk);
1058 dp->dccps_gar = secure_dccp_v6_sequence_number(np->saddr.s6_addr32,
1059 np->daddr.s6_addr32,
1063 err = dccp_connect(sk);
1070 dccp_set_state(sk, DCCP_CLOSED);
1074 sk->sk_route_caps = 0;
1078 static struct inet_connection_sock_af_ops dccp_ipv6_af_ops = {
1079 .queue_xmit = inet6_csk_xmit,
1080 .send_check = dccp_v6_send_check,
1081 .rebuild_header = inet6_sk_rebuild_header,
1082 .conn_request = dccp_v6_conn_request,
1083 .syn_recv_sock = dccp_v6_request_recv_sock,
1084 .net_header_len = sizeof(struct ipv6hdr),
1085 .setsockopt = ipv6_setsockopt,
1086 .getsockopt = ipv6_getsockopt,
1087 .addr2sockaddr = inet6_csk_addr2sockaddr,
1088 .sockaddr_len = sizeof(struct sockaddr_in6),
1089 #ifdef CONFIG_COMPAT
1090 .compat_setsockopt = compat_ipv6_setsockopt,
1091 .compat_getsockopt = compat_ipv6_getsockopt,
1096 * DCCP over IPv4 via INET6 API
1098 static struct inet_connection_sock_af_ops dccp_ipv6_mapped = {
1099 .queue_xmit = ip_queue_xmit,
1100 .send_check = dccp_v4_send_check,
1101 .rebuild_header = inet_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 iphdr),
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,
1115 /* NOTE: A lot of things set to zero explicitly by call to
1116 * sk_alloc() so need not be done here.
1118 static int dccp_v6_init_sock(struct sock *sk)
1120 static __u8 dccp_v6_ctl_sock_initialized;
1121 int err = dccp_init_sock(sk, dccp_v6_ctl_sock_initialized);
1124 if (unlikely(!dccp_v6_ctl_sock_initialized))
1125 dccp_v6_ctl_sock_initialized = 1;
1126 inet_csk(sk)->icsk_af_ops = &dccp_ipv6_af_ops;
1132 static int dccp_v6_destroy_sock(struct sock *sk)
1134 dccp_destroy_sock(sk);
1135 return inet6_destroy_sock(sk);
1138 static struct timewait_sock_ops dccp6_timewait_sock_ops = {
1139 .twsk_obj_size = sizeof(struct dccp6_timewait_sock),
1142 static struct proto dccp_v6_prot = {
1144 .owner = THIS_MODULE,
1145 .close = dccp_close,
1146 .connect = dccp_v6_connect,
1147 .disconnect = dccp_disconnect,
1148 .ioctl = dccp_ioctl,
1149 .init = dccp_v6_init_sock,
1150 .setsockopt = dccp_setsockopt,
1151 .getsockopt = dccp_getsockopt,
1152 .sendmsg = dccp_sendmsg,
1153 .recvmsg = dccp_recvmsg,
1154 .backlog_rcv = dccp_v6_do_rcv,
1155 .hash = dccp_v6_hash,
1156 .unhash = dccp_unhash,
1157 .accept = inet_csk_accept,
1158 .get_port = dccp_v6_get_port,
1159 .shutdown = dccp_shutdown,
1160 .destroy = dccp_v6_destroy_sock,
1161 .orphan_count = &dccp_orphan_count,
1162 .max_header = MAX_DCCP_HEADER,
1163 .obj_size = sizeof(struct dccp6_sock),
1164 .rsk_prot = &dccp6_request_sock_ops,
1165 .twsk_prot = &dccp6_timewait_sock_ops,
1166 #ifdef CONFIG_COMPAT
1167 .compat_setsockopt = compat_dccp_setsockopt,
1168 .compat_getsockopt = compat_dccp_getsockopt,
1172 static struct inet6_protocol dccp_v6_protocol = {
1173 .handler = dccp_v6_rcv,
1174 .err_handler = dccp_v6_err,
1175 .flags = INET6_PROTO_NOPOLICY | INET6_PROTO_FINAL,
1178 static struct proto_ops inet6_dccp_ops = {
1180 .owner = THIS_MODULE,
1181 .release = inet6_release,
1183 .connect = inet_stream_connect,
1184 .socketpair = sock_no_socketpair,
1185 .accept = inet_accept,
1186 .getname = inet6_getname,
1188 .ioctl = inet6_ioctl,
1189 .listen = inet_dccp_listen,
1190 .shutdown = inet_shutdown,
1191 .setsockopt = sock_common_setsockopt,
1192 .getsockopt = sock_common_getsockopt,
1193 .sendmsg = inet_sendmsg,
1194 .recvmsg = sock_common_recvmsg,
1195 .mmap = sock_no_mmap,
1196 .sendpage = sock_no_sendpage,
1197 #ifdef CONFIG_COMPAT
1198 .compat_setsockopt = compat_sock_common_setsockopt,
1199 .compat_getsockopt = compat_sock_common_getsockopt,
1203 static struct inet_protosw dccp_v6_protosw = {
1205 .protocol = IPPROTO_DCCP,
1206 .prot = &dccp_v6_prot,
1207 .ops = &inet6_dccp_ops,
1209 .flags = INET_PROTOSW_ICSK,
1212 static int __init dccp_v6_init(void)
1214 int err = proto_register(&dccp_v6_prot, 1);
1219 err = inet6_add_protocol(&dccp_v6_protocol, IPPROTO_DCCP);
1221 goto out_unregister_proto;
1223 inet6_register_protosw(&dccp_v6_protosw);
1225 err = inet_csk_ctl_sock_create(&dccp_v6_ctl_socket, PF_INET6,
1226 SOCK_DCCP, IPPROTO_DCCP);
1228 goto out_unregister_protosw;
1231 out_unregister_protosw:
1232 inet6_del_protocol(&dccp_v6_protocol, IPPROTO_DCCP);
1233 inet6_unregister_protosw(&dccp_v6_protosw);
1234 out_unregister_proto:
1235 proto_unregister(&dccp_v6_prot);
1239 static void __exit dccp_v6_exit(void)
1241 inet6_del_protocol(&dccp_v6_protocol, IPPROTO_DCCP);
1242 inet6_unregister_protosw(&dccp_v6_protosw);
1243 proto_unregister(&dccp_v6_prot);
1246 module_init(dccp_v6_init);
1247 module_exit(dccp_v6_exit);
1250 * __stringify doesn't likes enums, so use SOCK_DCCP (6) and IPPROTO_DCCP (33)
1251 * values directly, Also cover the case where the protocol is not specified,
1252 * i.e. net-pf-PF_INET6-proto-0-type-SOCK_DCCP
1254 MODULE_ALIAS("net-pf-" __stringify(PF_INET6) "-proto-33-type-6");
1255 MODULE_ALIAS("net-pf-" __stringify(PF_INET6) "-proto-0-type-6");
1256 MODULE_LICENSE("GPL");
1257 MODULE_AUTHOR("Arnaldo Carvalho de Melo <acme@mandriva.com>");
1258 MODULE_DESCRIPTION("DCCPv6 - Datagram Congestion Controlled Protocol");