4 * An implementation of the DCCP protocol
5 * Arnaldo Carvalho de Melo <acme@conectiva.com.br>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
13 #include <linux/config.h>
14 #include <linux/dccp.h>
15 #include <linux/icmp.h>
16 #include <linux/module.h>
17 #include <linux/skbuff.h>
18 #include <linux/random.h>
21 #include <net/inet_hashtables.h>
23 #include <net/tcp_states.h>
29 struct inet_hashinfo __cacheline_aligned dccp_hashinfo = {
30 .lhash_lock = RW_LOCK_UNLOCKED,
31 .lhash_users = ATOMIC_INIT(0),
32 .lhash_wait = __WAIT_QUEUE_HEAD_INITIALIZER(dccp_hashinfo.lhash_wait),
33 .portalloc_lock = SPIN_LOCK_UNLOCKED,
34 .port_rover = 1024 - 1,
37 EXPORT_SYMBOL_GPL(dccp_hashinfo);
39 static int dccp_v4_get_port(struct sock *sk, const unsigned short snum)
41 return inet_csk_get_port(&dccp_hashinfo, sk, snum);
44 static void dccp_v4_hash(struct sock *sk)
46 inet_hash(&dccp_hashinfo, sk);
49 static void dccp_v4_unhash(struct sock *sk)
51 inet_unhash(&dccp_hashinfo, sk);
54 /* called with local bh disabled */
55 static int __dccp_v4_check_established(struct sock *sk, const __u16 lport,
56 struct inet_timewait_sock **twp)
58 struct inet_sock *inet = inet_sk(sk);
59 const u32 daddr = inet->rcv_saddr;
60 const u32 saddr = inet->daddr;
61 const int dif = sk->sk_bound_dev_if;
62 INET_ADDR_COOKIE(acookie, saddr, daddr)
63 const __u32 ports = INET_COMBINED_PORTS(inet->dport, lport);
64 const int hash = inet_ehashfn(daddr, lport, saddr, inet->dport,
65 dccp_hashinfo.ehash_size);
66 struct inet_ehash_bucket *head = &dccp_hashinfo.ehash[hash];
67 const struct sock *sk2;
68 const struct hlist_node *node;
69 struct inet_timewait_sock *tw;
71 write_lock(&head->lock);
73 /* Check TIME-WAIT sockets first. */
74 sk_for_each(sk2, node, &(head + dccp_hashinfo.ehash_size)->chain) {
77 if (INET_TW_MATCH(sk2, acookie, saddr, daddr, ports, dif))
82 /* And established part... */
83 sk_for_each(sk2, node, &head->chain) {
84 if (INET_MATCH(sk2, acookie, saddr, daddr, ports, dif))
88 /* Must record num and sport now. Otherwise we will see
89 * in hash table socket with a funny identity. */
91 inet->sport = htons(lport);
92 sk->sk_hashent = hash;
93 BUG_TRAP(sk_unhashed(sk));
94 __sk_add_node(sk, &head->chain);
95 sock_prot_inc_use(sk->sk_prot);
96 write_unlock(&head->lock);
100 NET_INC_STATS_BH(LINUX_MIB_TIMEWAITRECYCLED);
101 } else if (tw != NULL) {
102 /* Silly. Should hash-dance instead... */
103 inet_twsk_deschedule(tw, &dccp_death_row);
104 NET_INC_STATS_BH(LINUX_MIB_TIMEWAITRECYCLED);
112 write_unlock(&head->lock);
113 return -EADDRNOTAVAIL;
117 * Bind a port for a connect operation and hash it.
119 static int dccp_v4_hash_connect(struct sock *sk)
121 const unsigned short snum = inet_sk(sk)->num;
122 struct inet_bind_hashbucket *head;
123 struct inet_bind_bucket *tb;
128 int low = sysctl_local_port_range[0];
129 int high = sysctl_local_port_range[1];
130 int remaining = (high - low) + 1;
131 struct hlist_node *node;
132 struct inet_timewait_sock *tw = NULL;
136 /* TODO. Actually it is not so bad idea to remove
137 * dccp_hashinfo.portalloc_lock before next submission to
139 * As soon as we touch this place at all it is time to think.
141 * Now it protects single _advisory_ variable
142 * dccp_hashinfo.port_rover, hence it is mostly useless.
143 * Code will work nicely if we just delete it, but
144 * I am afraid in contented case it will work not better or
145 * even worse: another cpu just will hit the same bucket
147 * So some cpu salt could remove both contention and
148 * memory pingpong. Any ideas how to do this in a nice way?
150 spin_lock(&dccp_hashinfo.portalloc_lock);
151 rover = dccp_hashinfo.port_rover;
155 if ((rover < low) || (rover > high))
157 head = &dccp_hashinfo.bhash[inet_bhashfn(rover,
158 dccp_hashinfo.bhash_size)];
159 spin_lock(&head->lock);
161 /* Does not bother with rcv_saddr checks,
162 * because the established check is already
165 inet_bind_bucket_for_each(tb, node, &head->chain) {
166 if (tb->port == rover) {
167 BUG_TRAP(!hlist_empty(&tb->owners));
168 if (tb->fastreuse >= 0)
170 if (!__dccp_v4_check_established(sk,
178 tb = inet_bind_bucket_create(dccp_hashinfo.bind_bucket_cachep,
181 spin_unlock(&head->lock);
188 spin_unlock(&head->lock);
189 } while (--remaining > 0);
190 dccp_hashinfo.port_rover = rover;
191 spin_unlock(&dccp_hashinfo.portalloc_lock);
195 return -EADDRNOTAVAIL;
198 /* All locks still held and bhs disabled */
199 dccp_hashinfo.port_rover = rover;
200 spin_unlock(&dccp_hashinfo.portalloc_lock);
202 inet_bind_hash(sk, tb, rover);
203 if (sk_unhashed(sk)) {
204 inet_sk(sk)->sport = htons(rover);
205 __inet_hash(&dccp_hashinfo, sk, 0);
207 spin_unlock(&head->lock);
210 inet_twsk_deschedule(tw, &dccp_death_row);
218 head = &dccp_hashinfo.bhash[inet_bhashfn(snum,
219 dccp_hashinfo.bhash_size)];
220 tb = inet_csk(sk)->icsk_bind_hash;
221 spin_lock_bh(&head->lock);
222 if (sk_head(&tb->owners) == sk && sk->sk_bind_node.next == NULL) {
223 __inet_hash(&dccp_hashinfo, sk, 0);
224 spin_unlock_bh(&head->lock);
227 spin_unlock(&head->lock);
228 /* No definite answer... Walk to established hash table */
229 ret = __dccp_v4_check_established(sk, snum, NULL);
236 static int dccp_v4_connect(struct sock *sk, struct sockaddr *uaddr,
239 struct inet_sock *inet = inet_sk(sk);
240 struct dccp_sock *dp = dccp_sk(sk);
241 const struct sockaddr_in *usin = (struct sockaddr_in *)uaddr;
247 dp->dccps_role = DCCP_ROLE_CLIENT;
249 if (addr_len < sizeof(struct sockaddr_in))
252 if (usin->sin_family != AF_INET)
253 return -EAFNOSUPPORT;
255 nexthop = daddr = usin->sin_addr.s_addr;
256 if (inet->opt != NULL && inet->opt->srr) {
259 nexthop = inet->opt->faddr;
262 tmp = ip_route_connect(&rt, nexthop, inet->saddr,
263 RT_CONN_FLAGS(sk), sk->sk_bound_dev_if,
265 inet->sport, usin->sin_port, sk);
269 if (rt->rt_flags & (RTCF_MULTICAST | RTCF_BROADCAST)) {
274 if (inet->opt == NULL || !inet->opt->srr)
277 if (inet->saddr == 0)
278 inet->saddr = rt->rt_src;
279 inet->rcv_saddr = inet->saddr;
281 inet->dport = usin->sin_port;
284 dp->dccps_ext_header_len = 0;
285 if (inet->opt != NULL)
286 dp->dccps_ext_header_len = inet->opt->optlen;
288 * Socket identity is still unknown (sport may be zero).
289 * However we set state to DCCP_REQUESTING and not releasing socket
290 * lock select source port, enter ourselves into the hash tables and
291 * complete initialization after this.
293 dccp_set_state(sk, DCCP_REQUESTING);
294 err = dccp_v4_hash_connect(sk);
298 err = ip_route_newports(&rt, inet->sport, inet->dport, sk);
302 /* OK, now commit destination to socket. */
303 sk_setup_caps(sk, &rt->u.dst);
306 dp->dccps_iss = secure_dccp_sequence_number(inet->saddr,
310 dccp_update_gss(sk, dp->dccps_iss);
313 * SWL and AWL are initially adjusted so that they are not less than
314 * the initial Sequence Numbers received and sent, respectively:
315 * SWL := max(GSR + 1 - floor(W/4), ISR),
316 * AWL := max(GSS - W' + 1, ISS).
317 * These adjustments MUST be applied only at the beginning of the
320 dccp_set_seqno(&dp->dccps_awl, max48(dp->dccps_awl, dp->dccps_iss));
322 inet->id = dp->dccps_iss ^ jiffies;
324 err = dccp_connect(sk);
332 * This unhashes the socket and releases the local port, if necessary.
334 dccp_set_state(sk, DCCP_CLOSED);
336 sk->sk_route_caps = 0;
342 * This routine does path mtu discovery as defined in RFC1191.
344 static inline void dccp_do_pmtu_discovery(struct sock *sk,
345 const struct iphdr *iph,
348 struct dst_entry *dst;
349 const struct inet_sock *inet = inet_sk(sk);
350 const struct dccp_sock *dp = dccp_sk(sk);
352 /* We are not interested in DCCP_LISTEN and request_socks (RESPONSEs
353 * send out by Linux are always < 576bytes so they should go through
356 if (sk->sk_state == DCCP_LISTEN)
359 /* We don't check in the destentry if pmtu discovery is forbidden
360 * on this route. We just assume that no packet_to_big packets
361 * are send back when pmtu discovery is not active.
362 * There is a small race when the user changes this flag in the
363 * route, but I think that's acceptable.
365 if ((dst = __sk_dst_check(sk, 0)) == NULL)
368 dst->ops->update_pmtu(dst, mtu);
370 /* Something is about to be wrong... Remember soft error
371 * for the case, if this connection will not able to recover.
373 if (mtu < dst_mtu(dst) && ip_dont_fragment(sk, dst))
374 sk->sk_err_soft = EMSGSIZE;
378 if (inet->pmtudisc != IP_PMTUDISC_DONT &&
379 dp->dccps_pmtu_cookie > mtu) {
380 dccp_sync_mss(sk, mtu);
383 * From: draft-ietf-dccp-spec-11.txt
385 * DCCP-Sync packets are the best choice for upward
386 * probing, since DCCP-Sync probes do not risk application
389 dccp_send_sync(sk, dp->dccps_gsr, DCCP_PKT_SYNC);
390 } /* else let the usual retransmit timer handle it */
393 static void dccp_v4_ctl_send_ack(struct sk_buff *rxskb)
396 struct dccp_hdr *rxdh = dccp_hdr(rxskb), *dh;
397 const int dccp_hdr_ack_len = sizeof(struct dccp_hdr) +
398 sizeof(struct dccp_hdr_ext) +
399 sizeof(struct dccp_hdr_ack_bits);
402 if (((struct rtable *)rxskb->dst)->rt_type != RTN_LOCAL)
405 skb = alloc_skb(MAX_DCCP_HEADER + 15, GFP_ATOMIC);
409 /* Reserve space for headers. */
410 skb_reserve(skb, MAX_DCCP_HEADER);
412 skb->dst = dst_clone(rxskb->dst);
414 skb->h.raw = skb_push(skb, dccp_hdr_ack_len);
416 memset(dh, 0, dccp_hdr_ack_len);
418 /* Build DCCP header and checksum it. */
419 dh->dccph_type = DCCP_PKT_ACK;
420 dh->dccph_sport = rxdh->dccph_dport;
421 dh->dccph_dport = rxdh->dccph_sport;
422 dh->dccph_doff = dccp_hdr_ack_len / 4;
425 dccp_hdr_set_seq(dh, DCCP_SKB_CB(rxskb)->dccpd_ack_seq);
426 dccp_hdr_set_ack(dccp_hdr_ack_bits(skb),
427 DCCP_SKB_CB(rxskb)->dccpd_seq);
429 bh_lock_sock(dccp_ctl_socket->sk);
430 err = ip_build_and_send_pkt(skb, dccp_ctl_socket->sk,
431 rxskb->nh.iph->daddr,
432 rxskb->nh.iph->saddr, NULL);
433 bh_unlock_sock(dccp_ctl_socket->sk);
435 if (err == NET_XMIT_CN || err == 0) {
436 DCCP_INC_STATS_BH(DCCP_MIB_OUTSEGS);
437 DCCP_INC_STATS_BH(DCCP_MIB_OUTRSTS);
441 static void dccp_v4_reqsk_send_ack(struct sk_buff *skb,
442 struct request_sock *req)
444 dccp_v4_ctl_send_ack(skb);
447 static int dccp_v4_send_response(struct sock *sk, struct request_sock *req,
448 struct dst_entry *dst)
453 /* First, grab a route. */
455 if (dst == NULL && (dst = inet_csk_route_req(sk, req)) == NULL)
458 skb = dccp_make_response(sk, dst, req);
460 const struct inet_request_sock *ireq = inet_rsk(req);
462 err = ip_build_and_send_pkt(skb, sk, ireq->loc_addr,
465 if (err == NET_XMIT_CN)
475 * This routine is called by the ICMP module when it gets some sort of error
476 * condition. If err < 0 then the socket should be closed and the error
477 * returned to the user. If err > 0 it's just the icmp type << 8 | icmp code.
478 * After adjustment header points to the first 8 bytes of the tcp header. We
479 * need to find the appropriate port.
481 * The locking strategy used here is very "optimistic". When someone else
482 * accesses the socket the ICMP is just dropped and for some paths there is no
483 * check at all. A more general error queue to queue errors for later handling
484 * is probably better.
486 void dccp_v4_err(struct sk_buff *skb, u32 info)
488 const struct iphdr *iph = (struct iphdr *)skb->data;
489 const struct dccp_hdr *dh = (struct dccp_hdr *)(skb->data +
491 struct dccp_sock *dp;
492 struct inet_sock *inet;
493 const int type = skb->h.icmph->type;
494 const int code = skb->h.icmph->code;
499 if (skb->len < (iph->ihl << 2) + 8) {
500 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
504 sk = inet_lookup(&dccp_hashinfo, iph->daddr, dh->dccph_dport,
505 iph->saddr, dh->dccph_sport, inet_iif(skb));
507 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
511 if (sk->sk_state == DCCP_TIME_WAIT) {
512 inet_twsk_put((struct inet_timewait_sock *)sk);
517 /* If too many ICMPs get dropped on busy
518 * servers this needs to be solved differently.
520 if (sock_owned_by_user(sk))
521 NET_INC_STATS_BH(LINUX_MIB_LOCKDROPPEDICMPS);
523 if (sk->sk_state == DCCP_CLOSED)
527 seq = dccp_hdr_seq(skb);
528 if (sk->sk_state != DCCP_LISTEN &&
529 !between48(seq, dp->dccps_swl, dp->dccps_swh)) {
530 NET_INC_STATS(LINUX_MIB_OUTOFWINDOWICMPS);
535 case ICMP_SOURCE_QUENCH:
536 /* Just silently ignore these. */
538 case ICMP_PARAMETERPROB:
541 case ICMP_DEST_UNREACH:
542 if (code > NR_ICMP_UNREACH)
545 if (code == ICMP_FRAG_NEEDED) { /* PMTU discovery (RFC1191) */
546 if (!sock_owned_by_user(sk))
547 dccp_do_pmtu_discovery(sk, iph, info);
551 err = icmp_err_convert[code].errno;
553 case ICMP_TIME_EXCEEDED:
560 switch (sk->sk_state) {
561 struct request_sock *req , **prev;
563 if (sock_owned_by_user(sk))
565 req = inet_csk_search_req(sk, &prev, dh->dccph_dport,
566 iph->daddr, iph->saddr);
571 * ICMPs are not backlogged, hence we cannot get an established
576 if (seq != dccp_rsk(req)->dreq_iss) {
577 NET_INC_STATS_BH(LINUX_MIB_OUTOFWINDOWICMPS);
581 * Still in RESPOND, just remove it silently.
582 * There is no good way to pass the error to the newly
583 * created socket, and POSIX does not want network
584 * errors returned from accept().
586 inet_csk_reqsk_queue_drop(sk, req, prev);
589 case DCCP_REQUESTING:
591 if (!sock_owned_by_user(sk)) {
592 DCCP_INC_STATS_BH(DCCP_MIB_ATTEMPTFAILS);
595 sk->sk_error_report(sk);
599 sk->sk_err_soft = err;
603 /* If we've already connected we will keep trying
604 * until we time out, or the user gives up.
606 * rfc1122 4.2.3.9 allows to consider as hard errors
607 * only PROTO_UNREACH and PORT_UNREACH (well, FRAG_FAILED too,
608 * but it is obsoleted by pmtu discovery).
610 * Note, that in modern internet, where routing is unreliable
611 * and in each dark corner broken firewalls sit, sending random
612 * errors ordered by their masters even this two messages finally lose
613 * their original sense (even Linux sends invalid PORT_UNREACHs)
615 * Now we are in compliance with RFCs.
620 if (!sock_owned_by_user(sk) && inet->recverr) {
622 sk->sk_error_report(sk);
623 } else /* Only an error on timeout */
624 sk->sk_err_soft = err;
630 int dccp_v4_send_reset(struct sock *sk, enum dccp_reset_codes code)
634 * FIXME: what if rebuild_header fails?
635 * Should we be doing a rebuild_header here?
637 int err = inet_sk_rebuild_header(sk);
642 skb = dccp_make_reset(sk, sk->sk_dst_cache, code);
644 const struct inet_sock *inet = inet_sk(sk);
646 err = ip_build_and_send_pkt(skb, sk,
647 inet->saddr, inet->daddr, NULL);
648 if (err == NET_XMIT_CN)
655 static inline u64 dccp_v4_init_sequence(const struct sock *sk,
656 const struct sk_buff *skb)
658 return secure_dccp_sequence_number(skb->nh.iph->daddr,
660 dccp_hdr(skb)->dccph_dport,
661 dccp_hdr(skb)->dccph_sport);
664 int dccp_v4_conn_request(struct sock *sk, struct sk_buff *skb)
666 struct inet_request_sock *ireq;
668 struct request_sock *req;
669 struct dccp_request_sock *dreq;
670 const __u32 saddr = skb->nh.iph->saddr;
671 const __u32 daddr = skb->nh.iph->daddr;
672 struct dst_entry *dst = NULL;
674 /* Never answer to DCCP_PKT_REQUESTs send to broadcast or multicast */
675 if (((struct rtable *)skb->dst)->rt_flags &
676 (RTCF_BROADCAST | RTCF_MULTICAST))
680 * TW buckets are converted to open requests without
681 * limitations, they conserve resources and peer is
682 * evidently real one.
684 if (inet_csk_reqsk_queue_is_full(sk))
688 * Accept backlog is full. If we have already queued enough
689 * of warm entries in syn queue, drop request. It is better than
690 * clogging syn queue with openreqs with exponentially increasing
693 if (sk_acceptq_is_full(sk) && inet_csk_reqsk_queue_young(sk) > 1)
696 req = reqsk_alloc(sk->sk_prot->rsk_prot);
700 /* FIXME: process options */
702 dccp_openreq_init(req, &dp, skb);
704 ireq = inet_rsk(req);
705 ireq->loc_addr = daddr;
706 ireq->rmt_addr = saddr;
707 /* FIXME: Merge Aristeu's option parsing code when ready */
708 req->rcv_wnd = 100; /* Fake, option parsing will get the
713 * Step 3: Process LISTEN state
715 * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init Cookie
717 * In fact we defer setting S.GSR, S.SWL, S.SWH to
718 * dccp_create_openreq_child.
720 dreq = dccp_rsk(req);
721 dreq->dreq_isr = DCCP_SKB_CB(skb)->dccpd_seq;
722 dreq->dreq_iss = dccp_v4_init_sequence(sk, skb);
723 dreq->dreq_service = dccp_hdr_request(skb)->dccph_req_service;
725 if (dccp_v4_send_response(sk, req, dst))
728 inet_csk_reqsk_queue_hash_add(sk, req, DCCP_TIMEOUT_INIT);
733 * FIXME: should be reqsk_free after implementing req->rsk_ops
737 DCCP_INC_STATS_BH(DCCP_MIB_ATTEMPTFAILS);
742 * The three way handshake has completed - we got a valid ACK or DATAACK -
743 * now create the new socket.
745 * This is the equivalent of TCP's tcp_v4_syn_recv_sock
747 struct sock *dccp_v4_request_recv_sock(struct sock *sk, struct sk_buff *skb,
748 struct request_sock *req,
749 struct dst_entry *dst)
751 struct inet_request_sock *ireq;
752 struct inet_sock *newinet;
753 struct dccp_sock *newdp;
756 if (sk_acceptq_is_full(sk))
759 if (dst == NULL && (dst = inet_csk_route_req(sk, req)) == NULL)
762 newsk = dccp_create_openreq_child(sk, req, skb);
766 sk_setup_caps(newsk, dst);
768 newdp = dccp_sk(newsk);
769 newinet = inet_sk(newsk);
770 ireq = inet_rsk(req);
771 newinet->daddr = ireq->rmt_addr;
772 newinet->rcv_saddr = ireq->loc_addr;
773 newinet->saddr = ireq->loc_addr;
774 newinet->opt = ireq->opt;
776 newinet->mc_index = inet_iif(skb);
777 newinet->mc_ttl = skb->nh.iph->ttl;
778 newinet->id = jiffies;
780 dccp_sync_mss(newsk, dst_mtu(dst));
782 __inet_hash(&dccp_hashinfo, newsk, 0);
783 __inet_inherit_port(&dccp_hashinfo, sk, newsk);
788 NET_INC_STATS_BH(LINUX_MIB_LISTENOVERFLOWS);
790 NET_INC_STATS_BH(LINUX_MIB_LISTENDROPS);
795 static struct sock *dccp_v4_hnd_req(struct sock *sk, struct sk_buff *skb)
797 const struct dccp_hdr *dh = dccp_hdr(skb);
798 const struct iphdr *iph = skb->nh.iph;
800 struct request_sock **prev;
801 /* Find possible connection requests. */
802 struct request_sock *req = inet_csk_search_req(sk, &prev,
804 iph->saddr, iph->daddr);
806 return dccp_check_req(sk, skb, req, prev);
808 nsk = __inet_lookup_established(&dccp_hashinfo,
809 iph->saddr, dh->dccph_sport,
810 iph->daddr, ntohs(dh->dccph_dport),
813 if (nsk->sk_state != DCCP_TIME_WAIT) {
817 inet_twsk_put((struct inet_timewait_sock *)nsk);
824 int dccp_v4_checksum(const struct sk_buff *skb, const u32 saddr,
827 const struct dccp_hdr* dh = dccp_hdr(skb);
831 if (dh->dccph_cscov == 0)
832 checksum_len = skb->len;
834 checksum_len = (dh->dccph_cscov + dh->dccph_x) * sizeof(u32);
835 checksum_len = checksum_len < skb->len ? checksum_len :
839 tmp = csum_partial((unsigned char *)dh, checksum_len, 0);
840 return csum_tcpudp_magic(saddr, daddr, checksum_len,
844 static int dccp_v4_verify_checksum(struct sk_buff *skb,
845 const u32 saddr, const u32 daddr)
847 struct dccp_hdr *dh = dccp_hdr(skb);
851 if (dh->dccph_cscov == 0)
852 checksum_len = skb->len;
854 checksum_len = (dh->dccph_cscov + dh->dccph_x) * sizeof(u32);
855 checksum_len = checksum_len < skb->len ? checksum_len :
858 tmp = csum_partial((unsigned char *)dh, checksum_len, 0);
859 return csum_tcpudp_magic(saddr, daddr, checksum_len,
860 IPPROTO_DCCP, tmp) == 0 ? 0 : -1;
863 static struct dst_entry* dccp_v4_route_skb(struct sock *sk,
867 struct flowi fl = { .oif = ((struct rtable *)skb->dst)->rt_iif,
869 { .daddr = skb->nh.iph->saddr,
870 .saddr = skb->nh.iph->daddr,
871 .tos = RT_CONN_FLAGS(sk) } },
872 .proto = sk->sk_protocol,
874 { .sport = dccp_hdr(skb)->dccph_dport,
875 .dport = dccp_hdr(skb)->dccph_sport }
879 if (ip_route_output_flow(&rt, &fl, sk, 0)) {
880 IP_INC_STATS_BH(IPSTATS_MIB_OUTNOROUTES);
887 static void dccp_v4_ctl_send_reset(struct sk_buff *rxskb)
890 struct dccp_hdr *rxdh = dccp_hdr(rxskb), *dh;
891 const int dccp_hdr_reset_len = sizeof(struct dccp_hdr) +
892 sizeof(struct dccp_hdr_ext) +
893 sizeof(struct dccp_hdr_reset);
895 struct dst_entry *dst;
898 /* Never send a reset in response to a reset. */
899 if (rxdh->dccph_type == DCCP_PKT_RESET)
902 if (((struct rtable *)rxskb->dst)->rt_type != RTN_LOCAL)
905 dst = dccp_v4_route_skb(dccp_ctl_socket->sk, rxskb);
909 skb = alloc_skb(MAX_DCCP_HEADER + 15, GFP_ATOMIC);
913 /* Reserve space for headers. */
914 skb_reserve(skb, MAX_DCCP_HEADER);
915 skb->dst = dst_clone(dst);
917 skb->h.raw = skb_push(skb, dccp_hdr_reset_len);
919 memset(dh, 0, dccp_hdr_reset_len);
921 /* Build DCCP header and checksum it. */
922 dh->dccph_type = DCCP_PKT_RESET;
923 dh->dccph_sport = rxdh->dccph_dport;
924 dh->dccph_dport = rxdh->dccph_sport;
925 dh->dccph_doff = dccp_hdr_reset_len / 4;
927 dccp_hdr_reset(skb)->dccph_reset_code =
928 DCCP_SKB_CB(rxskb)->dccpd_reset_code;
930 /* See "8.3.1. Abnormal Termination" in draft-ietf-dccp-spec-11 */
932 if (DCCP_SKB_CB(rxskb)->dccpd_ack_seq != DCCP_PKT_WITHOUT_ACK_SEQ)
933 dccp_set_seqno(&seqno, DCCP_SKB_CB(rxskb)->dccpd_ack_seq + 1);
935 dccp_hdr_set_seq(dh, seqno);
936 dccp_hdr_set_ack(dccp_hdr_ack_bits(skb),
937 DCCP_SKB_CB(rxskb)->dccpd_seq);
939 dh->dccph_checksum = dccp_v4_checksum(skb, rxskb->nh.iph->saddr,
940 rxskb->nh.iph->daddr);
942 bh_lock_sock(dccp_ctl_socket->sk);
943 err = ip_build_and_send_pkt(skb, dccp_ctl_socket->sk,
944 rxskb->nh.iph->daddr,
945 rxskb->nh.iph->saddr, NULL);
946 bh_unlock_sock(dccp_ctl_socket->sk);
948 if (err == NET_XMIT_CN || err == 0) {
949 DCCP_INC_STATS_BH(DCCP_MIB_OUTSEGS);
950 DCCP_INC_STATS_BH(DCCP_MIB_OUTRSTS);
956 int dccp_v4_do_rcv(struct sock *sk, struct sk_buff *skb)
958 struct dccp_hdr *dh = dccp_hdr(skb);
960 if (sk->sk_state == DCCP_OPEN) { /* Fast path */
961 if (dccp_rcv_established(sk, skb, dh, skb->len))
967 * Step 3: Process LISTEN state
968 * If S.state == LISTEN,
969 * If P.type == Request or P contains a valid Init Cookie
971 * * Must scan the packet's options to check for an Init
972 * Cookie. Only the Init Cookie is processed here,
973 * however; other options are processed in Step 8. This
974 * scan need only be performed if the endpoint uses Init
976 * * Generate a new socket and switch to that socket *
977 * Set S := new socket for this port pair
979 * Choose S.ISS (initial seqno) or set from Init Cookie
980 * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init Cookie
981 * Continue with S.state == RESPOND
982 * * A Response packet will be generated in Step 11 *
984 * Generate Reset(No Connection) unless P.type == Reset
985 * Drop packet and return
987 * NOTE: the check for the packet types is done in
988 * dccp_rcv_state_process
990 if (sk->sk_state == DCCP_LISTEN) {
991 struct sock *nsk = dccp_v4_hnd_req(sk, skb);
997 if (dccp_child_process(sk, nsk, skb))
1003 if (dccp_rcv_state_process(sk, skb, dh, skb->len))
1008 DCCP_SKB_CB(skb)->dccpd_reset_code = DCCP_RESET_CODE_NO_CONNECTION;
1009 dccp_v4_ctl_send_reset(skb);
1015 static inline int dccp_invalid_packet(struct sk_buff *skb)
1017 const struct dccp_hdr *dh;
1019 if (skb->pkt_type != PACKET_HOST)
1022 if (!pskb_may_pull(skb, sizeof(struct dccp_hdr))) {
1023 LIMIT_NETDEBUG(KERN_WARNING "DCCP: pskb_may_pull failed\n");
1029 /* If the packet type is not understood, drop packet and return */
1030 if (dh->dccph_type >= DCCP_PKT_INVALID) {
1031 LIMIT_NETDEBUG(KERN_WARNING "DCCP: invalid packet type\n");
1036 * If P.Data Offset is too small for packet type, or too large for
1037 * packet, drop packet and return
1039 if (dh->dccph_doff < dccp_hdr_len(skb) / sizeof(u32)) {
1040 LIMIT_NETDEBUG(KERN_WARNING "DCCP: P.Data Offset(%u) "
1046 if (!pskb_may_pull(skb, dh->dccph_doff * sizeof(u32))) {
1047 LIMIT_NETDEBUG(KERN_WARNING "DCCP: P.Data Offset(%u) "
1056 * If P.type is not Data, Ack, or DataAck and P.X == 0 (the packet
1057 * has short sequence numbers), drop packet and return
1059 if (dh->dccph_x == 0 &&
1060 dh->dccph_type != DCCP_PKT_DATA &&
1061 dh->dccph_type != DCCP_PKT_ACK &&
1062 dh->dccph_type != DCCP_PKT_DATAACK) {
1063 LIMIT_NETDEBUG(KERN_WARNING "DCCP: P.type (%s) not Data, Ack "
1064 "nor DataAck and P.X == 0\n",
1065 dccp_packet_name(dh->dccph_type));
1069 /* If the header checksum is incorrect, drop packet and return */
1070 if (dccp_v4_verify_checksum(skb, skb->nh.iph->saddr,
1071 skb->nh.iph->daddr) < 0) {
1072 LIMIT_NETDEBUG(KERN_WARNING "DCCP: header checksum is "
1080 /* this is called when real data arrives */
1081 int dccp_v4_rcv(struct sk_buff *skb)
1083 const struct dccp_hdr *dh;
1087 /* Step 1: Check header basics: */
1089 if (dccp_invalid_packet(skb))
1095 * Use something like this to simulate some DATA/DATAACK loss to test
1096 * dccp_ackpkts_add, you'll get something like this on a session that
1097 * sends 10 DATA/DATAACK packets:
1099 * ackpkts_print: 281473596467422 |0,0|3,0|0,0|3,0|0,0|3,0|0,0|3,0|0,1|
1101 * 0, 0 means: DCCP_ACKPKTS_STATE_RECEIVED, RLE == just this packet
1102 * 0, 1 means: DCCP_ACKPKTS_STATE_RECEIVED, RLE == two adjacent packets
1103 * with the same state
1104 * 3, 0 means: DCCP_ACKPKTS_STATE_NOT_RECEIVED, RLE == just this packet
1108 * 281473596467422 was received
1109 * 281473596467421 was not received
1110 * 281473596467420 was received
1111 * 281473596467419 was not received
1112 * 281473596467418 was received
1113 * 281473596467417 was not received
1114 * 281473596467416 was received
1115 * 281473596467415 was not received
1116 * 281473596467414 was received
1117 * 281473596467413 was received (this one was the 3way handshake
1121 if (dh->dccph_type == DCCP_PKT_DATA ||
1122 dh->dccph_type == DCCP_PKT_DATAACK) {
1123 static int discard = 0;
1132 DCCP_SKB_CB(skb)->dccpd_seq = dccp_hdr_seq(skb);
1133 DCCP_SKB_CB(skb)->dccpd_type = dh->dccph_type;
1135 dccp_pr_debug("%8.8s "
1136 "src=%u.%u.%u.%u@%-5d "
1137 "dst=%u.%u.%u.%u@%-5d seq=%llu",
1138 dccp_packet_name(dh->dccph_type),
1139 NIPQUAD(skb->nh.iph->saddr), ntohs(dh->dccph_sport),
1140 NIPQUAD(skb->nh.iph->daddr), ntohs(dh->dccph_dport),
1141 (unsigned long long) DCCP_SKB_CB(skb)->dccpd_seq);
1143 if (dccp_packet_without_ack(skb)) {
1144 DCCP_SKB_CB(skb)->dccpd_ack_seq = DCCP_PKT_WITHOUT_ACK_SEQ;
1145 dccp_pr_debug_cat("\n");
1147 DCCP_SKB_CB(skb)->dccpd_ack_seq = dccp_hdr_ack_seq(skb);
1148 dccp_pr_debug_cat(", ack=%llu\n",
1149 (unsigned long long)
1150 DCCP_SKB_CB(skb)->dccpd_ack_seq);
1154 * Look up flow ID in table and get corresponding socket */
1155 sk = __inet_lookup(&dccp_hashinfo,
1156 skb->nh.iph->saddr, dh->dccph_sport,
1157 skb->nh.iph->daddr, ntohs(dh->dccph_dport),
1163 * Generate Reset(No Connection) unless P.type == Reset
1164 * Drop packet and return
1167 dccp_pr_debug("failed to look up flow ID in table and "
1168 "get corresponding socket\n");
1169 goto no_dccp_socket;
1174 * ... or S.state == TIMEWAIT,
1175 * Generate Reset(No Connection) unless P.type == Reset
1176 * Drop packet and return
1179 if (sk->sk_state == DCCP_TIME_WAIT) {
1180 dccp_pr_debug("sk->sk_state == DCCP_TIME_WAIT: "
1185 if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb)) {
1186 dccp_pr_debug("xfrm4_policy_check failed\n");
1187 goto discard_and_relse;
1190 if (sk_filter(sk, skb, 0)) {
1191 dccp_pr_debug("sk_filter failed\n");
1192 goto discard_and_relse;
1199 if (!sock_owned_by_user(sk))
1200 rc = dccp_v4_do_rcv(sk, skb);
1202 sk_add_backlog(sk, skb);
1209 if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
1213 * Generate Reset(No Connection) unless P.type == Reset
1214 * Drop packet and return
1216 if (dh->dccph_type != DCCP_PKT_RESET) {
1217 DCCP_SKB_CB(skb)->dccpd_reset_code =
1218 DCCP_RESET_CODE_NO_CONNECTION;
1219 dccp_v4_ctl_send_reset(skb);
1223 /* Discard frame. */
1232 inet_twsk_put((struct inet_timewait_sock *)sk);
1233 goto no_dccp_socket;
1236 static int dccp_v4_init_sock(struct sock *sk)
1238 struct dccp_sock *dp = dccp_sk(sk);
1239 static int dccp_ctl_socket_init = 1;
1241 dccp_options_init(&dp->dccps_options);
1242 do_gettimeofday(&dp->dccps_epoch);
1244 if (dp->dccps_options.dccpo_send_ack_vector) {
1245 dp->dccps_hc_rx_ackpkts =
1246 dccp_ackpkts_alloc(DCCP_MAX_ACK_VECTOR_LEN,
1249 if (dp->dccps_hc_rx_ackpkts == NULL)
1254 * FIXME: We're hardcoding the CCID, and doing this at this point makes
1255 * the listening (master) sock get CCID control blocks, which is not
1256 * necessary, but for now, to not mess with the test userspace apps,
1257 * lets leave it here, later the real solution is to do this in a
1258 * setsockopt(CCIDs-I-want/accept). -acme
1260 if (likely(!dccp_ctl_socket_init)) {
1261 dp->dccps_hc_rx_ccid = ccid_init(dp->dccps_options.dccpo_ccid,
1263 dp->dccps_hc_tx_ccid = ccid_init(dp->dccps_options.dccpo_ccid,
1265 if (dp->dccps_hc_rx_ccid == NULL ||
1266 dp->dccps_hc_tx_ccid == NULL) {
1267 ccid_exit(dp->dccps_hc_rx_ccid, sk);
1268 ccid_exit(dp->dccps_hc_tx_ccid, sk);
1269 dccp_ackpkts_free(dp->dccps_hc_rx_ackpkts);
1270 dp->dccps_hc_rx_ackpkts = NULL;
1271 dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL;
1275 dccp_ctl_socket_init = 0;
1277 dccp_init_xmit_timers(sk);
1278 inet_csk(sk)->icsk_rto = DCCP_TIMEOUT_INIT;
1279 sk->sk_state = DCCP_CLOSED;
1280 sk->sk_write_space = dccp_write_space;
1281 dp->dccps_mss_cache = 536;
1282 dp->dccps_role = DCCP_ROLE_UNDEFINED;
1287 static int dccp_v4_destroy_sock(struct sock *sk)
1289 struct dccp_sock *dp = dccp_sk(sk);
1292 * DCCP doesn't use sk_qrite_queue, just sk_send_head
1293 * for retransmissions
1295 if (sk->sk_send_head != NULL) {
1296 kfree_skb(sk->sk_send_head);
1297 sk->sk_send_head = NULL;
1300 /* Clean up a referenced DCCP bind bucket. */
1301 if (inet_csk(sk)->icsk_bind_hash != NULL)
1302 inet_put_port(&dccp_hashinfo, sk);
1304 ccid_hc_rx_exit(dp->dccps_hc_rx_ccid, sk);
1305 ccid_hc_tx_exit(dp->dccps_hc_tx_ccid, sk);
1306 dccp_ackpkts_free(dp->dccps_hc_rx_ackpkts);
1307 dp->dccps_hc_rx_ackpkts = NULL;
1308 ccid_exit(dp->dccps_hc_rx_ccid, sk);
1309 ccid_exit(dp->dccps_hc_tx_ccid, sk);
1310 dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL;
1315 static void dccp_v4_reqsk_destructor(struct request_sock *req)
1317 kfree(inet_rsk(req)->opt);
1320 static struct request_sock_ops dccp_request_sock_ops = {
1322 .obj_size = sizeof(struct dccp_request_sock),
1323 .rtx_syn_ack = dccp_v4_send_response,
1324 .send_ack = dccp_v4_reqsk_send_ack,
1325 .destructor = dccp_v4_reqsk_destructor,
1326 .send_reset = dccp_v4_ctl_send_reset,
1329 struct proto dccp_v4_prot = {
1331 .owner = THIS_MODULE,
1332 .close = dccp_close,
1333 .connect = dccp_v4_connect,
1334 .disconnect = dccp_disconnect,
1335 .ioctl = dccp_ioctl,
1336 .init = dccp_v4_init_sock,
1337 .setsockopt = dccp_setsockopt,
1338 .getsockopt = dccp_getsockopt,
1339 .sendmsg = dccp_sendmsg,
1340 .recvmsg = dccp_recvmsg,
1341 .backlog_rcv = dccp_v4_do_rcv,
1342 .hash = dccp_v4_hash,
1343 .unhash = dccp_v4_unhash,
1344 .accept = inet_csk_accept,
1345 .get_port = dccp_v4_get_port,
1346 .shutdown = dccp_shutdown,
1347 .destroy = dccp_v4_destroy_sock,
1348 .orphan_count = &dccp_orphan_count,
1349 .max_header = MAX_DCCP_HEADER,
1350 .obj_size = sizeof(struct dccp_sock),
1351 .rsk_prot = &dccp_request_sock_ops,
1352 .twsk_obj_size = sizeof(struct inet_timewait_sock),