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)
431 struct request_sock *req;
432 struct dccp_request_sock *dreq;
433 struct inet6_request_sock *ireq6;
434 struct ipv6_pinfo *np = inet6_sk(sk);
435 const __be32 service = dccp_hdr_request(skb)->dccph_req_service;
436 struct dccp_skb_cb *dcb = DCCP_SKB_CB(skb);
437 __u8 reset_code = DCCP_RESET_CODE_TOO_BUSY;
439 if (skb->protocol == htons(ETH_P_IP))
440 return dccp_v4_conn_request(sk, skb);
442 if (!ipv6_unicast_destination(skb))
445 if (dccp_bad_service_code(sk, service)) {
446 reset_code = DCCP_RESET_CODE_BAD_SERVICE_CODE;
450 * There are no SYN attacks on IPv6, yet...
452 if (inet_csk_reqsk_queue_is_full(sk))
455 if (sk_acceptq_is_full(sk) && inet_csk_reqsk_queue_young(sk) > 1)
458 req = inet6_reqsk_alloc(&dccp6_request_sock_ops);
462 /* FIXME: process options */
464 dccp_openreq_init(req, &dp, 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 req->rcv_wnd = dccp_feat_default_sequence_window;
473 ireq6->pktopts = NULL;
475 if (ipv6_opt_accepted(sk, skb) ||
476 np->rxopt.bits.rxinfo || np->rxopt.bits.rxoinfo ||
477 np->rxopt.bits.rxhlim || np->rxopt.bits.rxohlim) {
478 atomic_inc(&skb->users);
479 ireq6->pktopts = skb;
481 ireq6->iif = sk->sk_bound_dev_if;
483 /* So that link locals have meaning */
484 if (!sk->sk_bound_dev_if &&
485 ipv6_addr_type(&ireq6->rmt_addr) & IPV6_ADDR_LINKLOCAL)
486 ireq6->iif = inet6_iif(skb);
489 * Step 3: Process LISTEN state
491 * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init Cookie
493 * In fact we defer setting S.GSR, S.SWL, S.SWH to
494 * dccp_create_openreq_child.
496 dreq = dccp_rsk(req);
497 dreq->dreq_isr = dcb->dccpd_seq;
498 dreq->dreq_iss = dccp_v6_init_sequence(sk, skb);
499 dreq->dreq_service = service;
501 if (dccp_v6_send_response(sk, req, NULL))
504 inet6_csk_reqsk_queue_hash_add(sk, req, DCCP_TIMEOUT_INIT);
510 DCCP_INC_STATS_BH(DCCP_MIB_ATTEMPTFAILS);
511 dcb->dccpd_reset_code = reset_code;
515 static struct sock *dccp_v6_request_recv_sock(struct sock *sk,
517 struct request_sock *req,
518 struct dst_entry *dst)
520 struct inet6_request_sock *ireq6 = inet6_rsk(req);
521 struct ipv6_pinfo *newnp, *np = inet6_sk(sk);
522 struct inet_sock *newinet;
523 struct dccp_sock *newdp;
524 struct dccp6_sock *newdp6;
526 struct ipv6_txoptions *opt;
528 if (skb->protocol == htons(ETH_P_IP)) {
532 newsk = dccp_v4_request_recv_sock(sk, skb, req, dst);
536 newdp6 = (struct dccp6_sock *)newsk;
537 newdp = dccp_sk(newsk);
538 newinet = inet_sk(newsk);
539 newinet->pinet6 = &newdp6->inet6;
540 newnp = inet6_sk(newsk);
542 memcpy(newnp, np, sizeof(struct ipv6_pinfo));
544 ipv6_addr_set(&newnp->daddr, 0, 0, htonl(0x0000FFFF),
547 ipv6_addr_set(&newnp->saddr, 0, 0, htonl(0x0000FFFF),
550 ipv6_addr_copy(&newnp->rcv_saddr, &newnp->saddr);
552 inet_csk(newsk)->icsk_af_ops = &dccp_ipv6_mapped;
553 newsk->sk_backlog_rcv = dccp_v4_do_rcv;
554 newnp->pktoptions = NULL;
556 newnp->mcast_oif = inet6_iif(skb);
557 newnp->mcast_hops = skb->nh.ipv6h->hop_limit;
560 * No need to charge this sock to the relevant IPv6 refcnt debug socks count
561 * here, dccp_create_openreq_child now does this for us, see the comment in
562 * that function for the gory details. -acme
565 /* It is tricky place. Until this moment IPv4 tcp
566 worked with IPv6 icsk.icsk_af_ops.
569 dccp_sync_mss(newsk, inet_csk(newsk)->icsk_pmtu_cookie);
576 if (sk_acceptq_is_full(sk))
579 if (np->rxopt.bits.osrcrt == 2 && opt == NULL && ireq6->pktopts) {
580 const struct inet6_skb_parm *rxopt = IP6CB(ireq6->pktopts);
583 opt = ipv6_invert_rthdr(sk,
584 (struct ipv6_rt_hdr *)(ireq6->pktopts->nh.raw +
589 struct in6_addr *final_p = NULL, final;
592 memset(&fl, 0, sizeof(fl));
593 fl.proto = IPPROTO_DCCP;
594 ipv6_addr_copy(&fl.fl6_dst, &ireq6->rmt_addr);
595 if (opt != NULL && opt->srcrt != NULL) {
596 const struct rt0_hdr *rt0 = (struct rt0_hdr *)opt->srcrt;
598 ipv6_addr_copy(&final, &fl.fl6_dst);
599 ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
602 ipv6_addr_copy(&fl.fl6_src, &ireq6->loc_addr);
603 fl.oif = sk->sk_bound_dev_if;
604 fl.fl_ip_dport = inet_rsk(req)->rmt_port;
605 fl.fl_ip_sport = inet_sk(sk)->sport;
606 security_sk_classify_flow(sk, &fl);
608 if (ip6_dst_lookup(sk, &dst, &fl))
612 ipv6_addr_copy(&fl.fl6_dst, final_p);
614 if ((xfrm_lookup(&dst, &fl, sk, 0)) < 0)
618 newsk = dccp_create_openreq_child(sk, req, skb);
623 * No need to charge this sock to the relevant IPv6 refcnt debug socks
624 * count here, dccp_create_openreq_child now does this for us, see the
625 * comment in that function for the gory details. -acme
628 __ip6_dst_store(newsk, dst, NULL, NULL);
629 newsk->sk_route_caps = dst->dev->features & ~(NETIF_F_IP_CSUM |
631 newdp6 = (struct dccp6_sock *)newsk;
632 newinet = inet_sk(newsk);
633 newinet->pinet6 = &newdp6->inet6;
634 newdp = dccp_sk(newsk);
635 newnp = inet6_sk(newsk);
637 memcpy(newnp, np, sizeof(struct ipv6_pinfo));
639 ipv6_addr_copy(&newnp->daddr, &ireq6->rmt_addr);
640 ipv6_addr_copy(&newnp->saddr, &ireq6->loc_addr);
641 ipv6_addr_copy(&newnp->rcv_saddr, &ireq6->loc_addr);
642 newsk->sk_bound_dev_if = ireq6->iif;
644 /* Now IPv6 options...
646 First: no IPv4 options.
651 newnp->rxopt.all = np->rxopt.all;
653 /* Clone pktoptions received with SYN */
654 newnp->pktoptions = NULL;
655 if (ireq6->pktopts != NULL) {
656 newnp->pktoptions = skb_clone(ireq6->pktopts, GFP_ATOMIC);
657 kfree_skb(ireq6->pktopts);
658 ireq6->pktopts = NULL;
659 if (newnp->pktoptions)
660 skb_set_owner_r(newnp->pktoptions, newsk);
663 newnp->mcast_oif = inet6_iif(skb);
664 newnp->mcast_hops = skb->nh.ipv6h->hop_limit;
667 * Clone native IPv6 options from listening socket (if any)
669 * Yes, keeping reference count would be much more clever, but we make
670 * one more one thing there: reattach optmem to newsk.
673 newnp->opt = ipv6_dup_options(newsk, opt);
675 sock_kfree_s(sk, opt, opt->tot_len);
678 inet_csk(newsk)->icsk_ext_hdr_len = 0;
679 if (newnp->opt != NULL)
680 inet_csk(newsk)->icsk_ext_hdr_len = (newnp->opt->opt_nflen +
681 newnp->opt->opt_flen);
683 dccp_sync_mss(newsk, dst_mtu(dst));
685 newinet->daddr = newinet->saddr = newinet->rcv_saddr = LOOPBACK4_IPV6;
687 __inet6_hash(&dccp_hashinfo, newsk);
688 inet_inherit_port(&dccp_hashinfo, sk, newsk);
693 NET_INC_STATS_BH(LINUX_MIB_LISTENOVERFLOWS);
695 NET_INC_STATS_BH(LINUX_MIB_LISTENDROPS);
696 if (opt != NULL && opt != np->opt)
697 sock_kfree_s(sk, opt, opt->tot_len);
702 /* The socket must have it's spinlock held when we get
705 * We have a potential double-lock case here, so even when
706 * doing backlog processing we use the BH locking scheme.
707 * This is because we cannot sleep with the original spinlock
710 static int dccp_v6_do_rcv(struct sock *sk, struct sk_buff *skb)
712 struct ipv6_pinfo *np = inet6_sk(sk);
713 struct sk_buff *opt_skb = NULL;
715 /* Imagine: socket is IPv6. IPv4 packet arrives,
716 goes to IPv4 receive handler and backlogged.
717 From backlog it always goes here. Kerboom...
718 Fortunately, dccp_rcv_established and rcv_established
719 handle them correctly, but it is not case with
720 dccp_v6_hnd_req and dccp_v6_ctl_send_reset(). --ANK
723 if (skb->protocol == htons(ETH_P_IP))
724 return dccp_v4_do_rcv(sk, skb);
726 if (sk_filter(sk, skb))
730 * socket locking is here for SMP purposes as backlog rcv is currently
731 * called with bh processing disabled.
734 /* Do Stevens' IPV6_PKTOPTIONS.
736 Yes, guys, it is the only place in our code, where we
737 may make it not affecting IPv4.
738 The rest of code is protocol independent,
739 and I do not like idea to uglify IPv4.
741 Actually, all the idea behind IPV6_PKTOPTIONS
742 looks not very well thought. For now we latch
743 options, received in the last packet, enqueued
744 by tcp. Feel free to propose better solution.
749 * FIXME: Add handling of IPV6_PKTOPTIONS skb. See the comments below
750 * (wrt ipv6_pktopions) and net/ipv6/tcp_ipv6.c for an example.
752 opt_skb = skb_clone(skb, GFP_ATOMIC);
754 if (sk->sk_state == DCCP_OPEN) { /* Fast path */
755 if (dccp_rcv_established(sk, skb, dccp_hdr(skb), skb->len))
758 /* XXX This is where we would goto ipv6_pktoptions. */
759 __kfree_skb(opt_skb);
764 if (sk->sk_state == DCCP_LISTEN) {
765 struct sock *nsk = dccp_v6_hnd_req(sk, skb);
770 * Queue it on the new socket if the new socket is active,
771 * otherwise we just shortcircuit this and continue with
775 if (dccp_child_process(sk, nsk, skb))
778 __kfree_skb(opt_skb);
783 if (dccp_rcv_state_process(sk, skb, dccp_hdr(skb), skb->len))
786 /* XXX This is where we would goto ipv6_pktoptions. */
787 __kfree_skb(opt_skb);
792 dccp_v6_ctl_send_reset(skb);
795 __kfree_skb(opt_skb);
800 static int dccp_v6_rcv(struct sk_buff **pskb)
802 const struct dccp_hdr *dh;
803 struct sk_buff *skb = *pskb;
806 /* Step 1: Check header basics: */
808 if (dccp_invalid_packet(skb))
813 DCCP_SKB_CB(skb)->dccpd_seq = dccp_hdr_seq(skb);
814 DCCP_SKB_CB(skb)->dccpd_type = dh->dccph_type;
816 if (dccp_packet_without_ack(skb))
817 DCCP_SKB_CB(skb)->dccpd_ack_seq = DCCP_PKT_WITHOUT_ACK_SEQ;
819 DCCP_SKB_CB(skb)->dccpd_ack_seq = dccp_hdr_ack_seq(skb);
822 * Look up flow ID in table and get corresponding socket */
823 sk = __inet6_lookup(&dccp_hashinfo, &skb->nh.ipv6h->saddr,
825 &skb->nh.ipv6h->daddr, ntohs(dh->dccph_dport),
830 * Generate Reset(No Connection) unless P.type == Reset
831 * Drop packet and return
834 dccp_pr_debug("failed to look up flow ID in table and "
835 "get corresponding socket\n");
841 * ... or S.state == TIMEWAIT,
842 * Generate Reset(No Connection) unless P.type == Reset
843 * Drop packet and return
845 if (sk->sk_state == DCCP_TIME_WAIT) {
846 dccp_pr_debug("sk->sk_state == DCCP_TIME_WAIT: do_time_wait\n");
847 inet_twsk_put(inet_twsk(sk));
851 if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb))
852 goto discard_and_relse;
854 return sk_receive_skb(sk, skb) ? -1 : 0;
857 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
861 * Generate Reset(No Connection) unless P.type == Reset
862 * Drop packet and return
864 if (dh->dccph_type != DCCP_PKT_RESET) {
865 DCCP_SKB_CB(skb)->dccpd_reset_code =
866 DCCP_RESET_CODE_NO_CONNECTION;
867 dccp_v6_ctl_send_reset(skb);
879 static int dccp_v6_connect(struct sock *sk, struct sockaddr *uaddr,
882 struct sockaddr_in6 *usin = (struct sockaddr_in6 *)uaddr;
883 struct inet_connection_sock *icsk = inet_csk(sk);
884 struct inet_sock *inet = inet_sk(sk);
885 struct ipv6_pinfo *np = inet6_sk(sk);
886 struct dccp_sock *dp = dccp_sk(sk);
887 struct in6_addr *saddr = NULL, *final_p = NULL, final;
889 struct dst_entry *dst;
893 dp->dccps_role = DCCP_ROLE_CLIENT;
895 if (addr_len < SIN6_LEN_RFC2133)
898 if (usin->sin6_family != AF_INET6)
899 return -EAFNOSUPPORT;
901 memset(&fl, 0, sizeof(fl));
904 fl.fl6_flowlabel = usin->sin6_flowinfo & IPV6_FLOWINFO_MASK;
905 IP6_ECN_flow_init(fl.fl6_flowlabel);
906 if (fl.fl6_flowlabel & IPV6_FLOWLABEL_MASK) {
907 struct ip6_flowlabel *flowlabel;
908 flowlabel = fl6_sock_lookup(sk, fl.fl6_flowlabel);
909 if (flowlabel == NULL)
911 ipv6_addr_copy(&usin->sin6_addr, &flowlabel->dst);
912 fl6_sock_release(flowlabel);
916 * connect() to INADDR_ANY means loopback (BSD'ism).
918 if (ipv6_addr_any(&usin->sin6_addr))
919 usin->sin6_addr.s6_addr[15] = 1;
921 addr_type = ipv6_addr_type(&usin->sin6_addr);
923 if (addr_type & IPV6_ADDR_MULTICAST)
926 if (addr_type & IPV6_ADDR_LINKLOCAL) {
927 if (addr_len >= sizeof(struct sockaddr_in6) &&
928 usin->sin6_scope_id) {
929 /* If interface is set while binding, indices
932 if (sk->sk_bound_dev_if &&
933 sk->sk_bound_dev_if != usin->sin6_scope_id)
936 sk->sk_bound_dev_if = usin->sin6_scope_id;
939 /* Connect to link-local address requires an interface */
940 if (!sk->sk_bound_dev_if)
944 ipv6_addr_copy(&np->daddr, &usin->sin6_addr);
945 np->flow_label = fl.fl6_flowlabel;
950 if (addr_type == IPV6_ADDR_MAPPED) {
951 u32 exthdrlen = icsk->icsk_ext_hdr_len;
952 struct sockaddr_in sin;
954 SOCK_DEBUG(sk, "connect: ipv4 mapped\n");
956 if (__ipv6_only_sock(sk))
959 sin.sin_family = AF_INET;
960 sin.sin_port = usin->sin6_port;
961 sin.sin_addr.s_addr = usin->sin6_addr.s6_addr32[3];
963 icsk->icsk_af_ops = &dccp_ipv6_mapped;
964 sk->sk_backlog_rcv = dccp_v4_do_rcv;
966 err = dccp_v4_connect(sk, (struct sockaddr *)&sin, sizeof(sin));
968 icsk->icsk_ext_hdr_len = exthdrlen;
969 icsk->icsk_af_ops = &dccp_ipv6_af_ops;
970 sk->sk_backlog_rcv = dccp_v6_do_rcv;
973 ipv6_addr_set(&np->saddr, 0, 0, htonl(0x0000FFFF),
975 ipv6_addr_set(&np->rcv_saddr, 0, 0, htonl(0x0000FFFF),
982 if (!ipv6_addr_any(&np->rcv_saddr))
983 saddr = &np->rcv_saddr;
985 fl.proto = IPPROTO_DCCP;
986 ipv6_addr_copy(&fl.fl6_dst, &np->daddr);
987 ipv6_addr_copy(&fl.fl6_src, saddr ? saddr : &np->saddr);
988 fl.oif = sk->sk_bound_dev_if;
989 fl.fl_ip_dport = usin->sin6_port;
990 fl.fl_ip_sport = inet->sport;
991 security_sk_classify_flow(sk, &fl);
993 if (np->opt != NULL && np->opt->srcrt != NULL) {
994 const struct rt0_hdr *rt0 = (struct rt0_hdr *)np->opt->srcrt;
996 ipv6_addr_copy(&final, &fl.fl6_dst);
997 ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
1001 err = ip6_dst_lookup(sk, &dst, &fl);
1006 ipv6_addr_copy(&fl.fl6_dst, final_p);
1008 err = xfrm_lookup(&dst, &fl, sk, 0);
1012 if (saddr == NULL) {
1013 saddr = &fl.fl6_src;
1014 ipv6_addr_copy(&np->rcv_saddr, saddr);
1017 /* set the source address */
1018 ipv6_addr_copy(&np->saddr, saddr);
1019 inet->rcv_saddr = LOOPBACK4_IPV6;
1021 __ip6_dst_store(sk, dst, NULL, NULL);
1023 icsk->icsk_ext_hdr_len = 0;
1024 if (np->opt != NULL)
1025 icsk->icsk_ext_hdr_len = (np->opt->opt_flen +
1026 np->opt->opt_nflen);
1028 inet->dport = usin->sin6_port;
1030 dccp_set_state(sk, DCCP_REQUESTING);
1031 err = inet6_hash_connect(&dccp_death_row, sk);
1036 dp->dccps_gar = secure_dccp_v6_sequence_number(np->saddr.s6_addr32,
1037 np->daddr.s6_addr32,
1041 err = dccp_connect(sk);
1048 dccp_set_state(sk, DCCP_CLOSED);
1052 sk->sk_route_caps = 0;
1056 static struct inet_connection_sock_af_ops dccp_ipv6_af_ops = {
1057 .queue_xmit = inet6_csk_xmit,
1058 .send_check = dccp_v6_send_check,
1059 .rebuild_header = inet6_sk_rebuild_header,
1060 .conn_request = dccp_v6_conn_request,
1061 .syn_recv_sock = dccp_v6_request_recv_sock,
1062 .net_header_len = sizeof(struct ipv6hdr),
1063 .setsockopt = ipv6_setsockopt,
1064 .getsockopt = ipv6_getsockopt,
1065 .addr2sockaddr = inet6_csk_addr2sockaddr,
1066 .sockaddr_len = sizeof(struct sockaddr_in6),
1067 #ifdef CONFIG_COMPAT
1068 .compat_setsockopt = compat_ipv6_setsockopt,
1069 .compat_getsockopt = compat_ipv6_getsockopt,
1074 * DCCP over IPv4 via INET6 API
1076 static struct inet_connection_sock_af_ops dccp_ipv6_mapped = {
1077 .queue_xmit = ip_queue_xmit,
1078 .send_check = dccp_v4_send_check,
1079 .rebuild_header = inet_sk_rebuild_header,
1080 .conn_request = dccp_v6_conn_request,
1081 .syn_recv_sock = dccp_v6_request_recv_sock,
1082 .net_header_len = sizeof(struct iphdr),
1083 .setsockopt = ipv6_setsockopt,
1084 .getsockopt = ipv6_getsockopt,
1085 .addr2sockaddr = inet6_csk_addr2sockaddr,
1086 .sockaddr_len = sizeof(struct sockaddr_in6),
1087 #ifdef CONFIG_COMPAT
1088 .compat_setsockopt = compat_ipv6_setsockopt,
1089 .compat_getsockopt = compat_ipv6_getsockopt,
1093 /* NOTE: A lot of things set to zero explicitly by call to
1094 * sk_alloc() so need not be done here.
1096 static int dccp_v6_init_sock(struct sock *sk)
1098 static __u8 dccp_v6_ctl_sock_initialized;
1099 int err = dccp_init_sock(sk, dccp_v6_ctl_sock_initialized);
1102 if (unlikely(!dccp_v6_ctl_sock_initialized))
1103 dccp_v6_ctl_sock_initialized = 1;
1104 inet_csk(sk)->icsk_af_ops = &dccp_ipv6_af_ops;
1110 static int dccp_v6_destroy_sock(struct sock *sk)
1112 dccp_destroy_sock(sk);
1113 return inet6_destroy_sock(sk);
1116 static struct timewait_sock_ops dccp6_timewait_sock_ops = {
1117 .twsk_obj_size = sizeof(struct dccp6_timewait_sock),
1120 static struct proto dccp_v6_prot = {
1122 .owner = THIS_MODULE,
1123 .close = dccp_close,
1124 .connect = dccp_v6_connect,
1125 .disconnect = dccp_disconnect,
1126 .ioctl = dccp_ioctl,
1127 .init = dccp_v6_init_sock,
1128 .setsockopt = dccp_setsockopt,
1129 .getsockopt = dccp_getsockopt,
1130 .sendmsg = dccp_sendmsg,
1131 .recvmsg = dccp_recvmsg,
1132 .backlog_rcv = dccp_v6_do_rcv,
1133 .hash = dccp_v6_hash,
1134 .unhash = dccp_unhash,
1135 .accept = inet_csk_accept,
1136 .get_port = dccp_v6_get_port,
1137 .shutdown = dccp_shutdown,
1138 .destroy = dccp_v6_destroy_sock,
1139 .orphan_count = &dccp_orphan_count,
1140 .max_header = MAX_DCCP_HEADER,
1141 .obj_size = sizeof(struct dccp6_sock),
1142 .rsk_prot = &dccp6_request_sock_ops,
1143 .twsk_prot = &dccp6_timewait_sock_ops,
1144 #ifdef CONFIG_COMPAT
1145 .compat_setsockopt = compat_dccp_setsockopt,
1146 .compat_getsockopt = compat_dccp_getsockopt,
1150 static struct inet6_protocol dccp_v6_protocol = {
1151 .handler = dccp_v6_rcv,
1152 .err_handler = dccp_v6_err,
1153 .flags = INET6_PROTO_NOPOLICY | INET6_PROTO_FINAL,
1156 static struct proto_ops inet6_dccp_ops = {
1158 .owner = THIS_MODULE,
1159 .release = inet6_release,
1161 .connect = inet_stream_connect,
1162 .socketpair = sock_no_socketpair,
1163 .accept = inet_accept,
1164 .getname = inet6_getname,
1166 .ioctl = inet6_ioctl,
1167 .listen = inet_dccp_listen,
1168 .shutdown = inet_shutdown,
1169 .setsockopt = sock_common_setsockopt,
1170 .getsockopt = sock_common_getsockopt,
1171 .sendmsg = inet_sendmsg,
1172 .recvmsg = sock_common_recvmsg,
1173 .mmap = sock_no_mmap,
1174 .sendpage = sock_no_sendpage,
1175 #ifdef CONFIG_COMPAT
1176 .compat_setsockopt = compat_sock_common_setsockopt,
1177 .compat_getsockopt = compat_sock_common_getsockopt,
1181 static struct inet_protosw dccp_v6_protosw = {
1183 .protocol = IPPROTO_DCCP,
1184 .prot = &dccp_v6_prot,
1185 .ops = &inet6_dccp_ops,
1187 .flags = INET_PROTOSW_ICSK,
1190 static int __init dccp_v6_init(void)
1192 int err = proto_register(&dccp_v6_prot, 1);
1197 err = inet6_add_protocol(&dccp_v6_protocol, IPPROTO_DCCP);
1199 goto out_unregister_proto;
1201 inet6_register_protosw(&dccp_v6_protosw);
1203 err = inet_csk_ctl_sock_create(&dccp_v6_ctl_socket, PF_INET6,
1204 SOCK_DCCP, IPPROTO_DCCP);
1206 goto out_unregister_protosw;
1209 out_unregister_protosw:
1210 inet6_del_protocol(&dccp_v6_protocol, IPPROTO_DCCP);
1211 inet6_unregister_protosw(&dccp_v6_protosw);
1212 out_unregister_proto:
1213 proto_unregister(&dccp_v6_prot);
1217 static void __exit dccp_v6_exit(void)
1219 inet6_del_protocol(&dccp_v6_protocol, IPPROTO_DCCP);
1220 inet6_unregister_protosw(&dccp_v6_protosw);
1221 proto_unregister(&dccp_v6_prot);
1224 module_init(dccp_v6_init);
1225 module_exit(dccp_v6_exit);
1228 * __stringify doesn't likes enums, so use SOCK_DCCP (6) and IPPROTO_DCCP (33)
1229 * values directly, Also cover the case where the protocol is not specified,
1230 * i.e. net-pf-PF_INET6-proto-0-type-SOCK_DCCP
1232 MODULE_ALIAS("net-pf-" __stringify(PF_INET6) "-proto-33-type-6");
1233 MODULE_ALIAS("net-pf-" __stringify(PF_INET6) "-proto-0-type-6");
1234 MODULE_LICENSE("GPL");
1235 MODULE_AUTHOR("Arnaldo Carvalho de Melo <acme@mandriva.com>");
1236 MODULE_DESCRIPTION("DCCPv6 - Datagram Congestion Controlled Protocol");