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 static int dccp_v4_get_port(struct sock *sk, const unsigned short snum)
39 return inet_csk_get_port(&dccp_hashinfo, sk, snum);
42 static void dccp_v4_hash(struct sock *sk)
44 inet_hash(&dccp_hashinfo, sk);
47 static void dccp_v4_unhash(struct sock *sk)
49 inet_unhash(&dccp_hashinfo, sk);
52 /* called with local bh disabled */
53 static int __dccp_v4_check_established(struct sock *sk, const __u16 lport,
54 struct inet_timewait_sock **twp)
56 struct inet_sock *inet = inet_sk(sk);
57 const u32 daddr = inet->rcv_saddr;
58 const u32 saddr = inet->daddr;
59 const int dif = sk->sk_bound_dev_if;
60 INET_ADDR_COOKIE(acookie, saddr, daddr)
61 const __u32 ports = INET_COMBINED_PORTS(inet->dport, lport);
62 const int hash = inet_ehashfn(daddr, lport, saddr, inet->dport, dccp_hashinfo.ehash_size);
63 struct inet_ehash_bucket *head = &dccp_hashinfo.ehash[hash];
64 const struct sock *sk2;
65 const struct hlist_node *node;
66 struct inet_timewait_sock *tw;
68 write_lock(&head->lock);
70 /* Check TIME-WAIT sockets first. */
71 sk_for_each(sk2, node, &(head + dccp_hashinfo.ehash_size)->chain) {
74 if (INET_TW_MATCH(sk2, acookie, saddr, daddr, ports, dif))
79 /* And established part... */
80 sk_for_each(sk2, node, &head->chain) {
81 if (INET_MATCH(sk2, acookie, saddr, daddr, ports, dif))
85 /* Must record num and sport now. Otherwise we will see
86 * in hash table socket with a funny identity. */
88 inet->sport = htons(lport);
89 sk->sk_hashent = hash;
90 BUG_TRAP(sk_unhashed(sk));
91 __sk_add_node(sk, &head->chain);
92 sock_prot_inc_use(sk->sk_prot);
93 write_unlock(&head->lock);
97 NET_INC_STATS_BH(LINUX_MIB_TIMEWAITRECYCLED);
98 } else if (tw != NULL) {
99 /* Silly. Should hash-dance instead... */
100 dccp_tw_deschedule(tw);
101 NET_INC_STATS_BH(LINUX_MIB_TIMEWAITRECYCLED);
109 write_unlock(&head->lock);
110 return -EADDRNOTAVAIL;
114 * Bind a port for a connect operation and hash it.
116 static int dccp_v4_hash_connect(struct sock *sk)
118 const unsigned short snum = inet_sk(sk)->num;
119 struct inet_bind_hashbucket *head;
120 struct inet_bind_bucket *tb;
125 int low = sysctl_local_port_range[0];
126 int high = sysctl_local_port_range[1];
127 int remaining = (high - low) + 1;
128 struct hlist_node *node;
129 struct inet_timewait_sock *tw = NULL;
133 /* TODO. Actually it is not so bad idea to remove
134 * dccp_hashinfo.portalloc_lock before next submission to Linus.
135 * As soon as we touch this place at all it is time to think.
137 * Now it protects single _advisory_ variable dccp_hashinfo.port_rover,
138 * hence it is mostly useless.
139 * Code will work nicely if we just delete it, but
140 * I am afraid in contented case it will work not better or
141 * even worse: another cpu just will hit the same bucket
143 * So some cpu salt could remove both contention and
144 * memory pingpong. Any ideas how to do this in a nice way?
146 spin_lock(&dccp_hashinfo.portalloc_lock);
147 rover = dccp_hashinfo.port_rover;
151 if ((rover < low) || (rover > high))
153 head = &dccp_hashinfo.bhash[inet_bhashfn(rover, dccp_hashinfo.bhash_size)];
154 spin_lock(&head->lock);
156 /* Does not bother with rcv_saddr checks,
157 * because the established check is already
160 inet_bind_bucket_for_each(tb, node, &head->chain) {
161 if (tb->port == rover) {
162 BUG_TRAP(!hlist_empty(&tb->owners));
163 if (tb->fastreuse >= 0)
165 if (!__dccp_v4_check_established(sk,
173 tb = inet_bind_bucket_create(dccp_hashinfo.bind_bucket_cachep, head, rover);
175 spin_unlock(&head->lock);
182 spin_unlock(&head->lock);
183 } while (--remaining > 0);
184 dccp_hashinfo.port_rover = rover;
185 spin_unlock(&dccp_hashinfo.portalloc_lock);
189 return -EADDRNOTAVAIL;
192 /* All locks still held and bhs disabled */
193 dccp_hashinfo.port_rover = rover;
194 spin_unlock(&dccp_hashinfo.portalloc_lock);
196 inet_bind_hash(sk, tb, rover);
197 if (sk_unhashed(sk)) {
198 inet_sk(sk)->sport = htons(rover);
199 __inet_hash(&dccp_hashinfo, sk, 0);
201 spin_unlock(&head->lock);
204 dccp_tw_deschedule(tw);
212 head = &dccp_hashinfo.bhash[inet_bhashfn(snum, dccp_hashinfo.bhash_size)];
213 tb = inet_csk(sk)->icsk_bind_hash;
214 spin_lock_bh(&head->lock);
215 if (sk_head(&tb->owners) == sk && sk->sk_bind_node.next == NULL) {
216 __inet_hash(&dccp_hashinfo, sk, 0);
217 spin_unlock_bh(&head->lock);
220 spin_unlock(&head->lock);
221 /* No definite answer... Walk to established hash table */
222 ret = __dccp_v4_check_established(sk, snum, NULL);
229 static int dccp_v4_connect(struct sock *sk, struct sockaddr *uaddr,
232 struct inet_sock *inet = inet_sk(sk);
233 struct dccp_sock *dp = dccp_sk(sk);
234 const struct sockaddr_in *usin = (struct sockaddr_in *)uaddr;
240 dp->dccps_role = DCCP_ROLE_CLIENT;
242 if (addr_len < sizeof(struct sockaddr_in))
245 if (usin->sin_family != AF_INET)
246 return -EAFNOSUPPORT;
248 nexthop = daddr = usin->sin_addr.s_addr;
249 if (inet->opt != NULL && inet->opt->srr) {
252 nexthop = inet->opt->faddr;
255 tmp = ip_route_connect(&rt, nexthop, inet->saddr,
256 RT_CONN_FLAGS(sk), sk->sk_bound_dev_if,
258 inet->sport, usin->sin_port, sk);
262 if (rt->rt_flags & (RTCF_MULTICAST | RTCF_BROADCAST)) {
267 if (inet->opt == NULL || !inet->opt->srr)
270 if (inet->saddr == 0)
271 inet->saddr = rt->rt_src;
272 inet->rcv_saddr = inet->saddr;
274 inet->dport = usin->sin_port;
277 dp->dccps_ext_header_len = 0;
278 if (inet->opt != NULL)
279 dp->dccps_ext_header_len = inet->opt->optlen;
281 * Socket identity is still unknown (sport may be zero).
282 * However we set state to DCCP_REQUESTING and not releasing socket
283 * lock select source port, enter ourselves into the hash tables and
284 * complete initialization after this.
286 dccp_set_state(sk, DCCP_REQUESTING);
287 err = dccp_v4_hash_connect(sk);
291 err = ip_route_newports(&rt, inet->sport, inet->dport, sk);
295 /* OK, now commit destination to socket. */
296 sk_setup_caps(sk, &rt->u.dst);
299 dp->dccps_iss = secure_dccp_sequence_number(inet->saddr,
303 dccp_update_gss(sk, dp->dccps_iss);
305 inet->id = dp->dccps_iss ^ jiffies;
307 err = dccp_connect(sk);
314 /* This unhashes the socket and releases the local port, if necessary. */
315 dccp_set_state(sk, DCCP_CLOSED);
317 sk->sk_route_caps = 0;
323 * This routine does path mtu discovery as defined in RFC1191.
325 static inline void dccp_do_pmtu_discovery(struct sock *sk,
326 const struct iphdr *iph,
329 struct dst_entry *dst;
330 const struct inet_sock *inet = inet_sk(sk);
331 const struct dccp_sock *dp = dccp_sk(sk);
333 /* We are not interested in DCCP_LISTEN and request_socks (RESPONSEs
334 * send out by Linux are always < 576bytes so they should go through
337 if (sk->sk_state == DCCP_LISTEN)
340 /* We don't check in the destentry if pmtu discovery is forbidden
341 * on this route. We just assume that no packet_to_big packets
342 * are send back when pmtu discovery is not active.
343 * There is a small race when the user changes this flag in the
344 * route, but I think that's acceptable.
346 if ((dst = __sk_dst_check(sk, 0)) == NULL)
349 dst->ops->update_pmtu(dst, mtu);
351 /* Something is about to be wrong... Remember soft error
352 * for the case, if this connection will not able to recover.
354 if (mtu < dst_mtu(dst) && ip_dont_fragment(sk, dst))
355 sk->sk_err_soft = EMSGSIZE;
359 if (inet->pmtudisc != IP_PMTUDISC_DONT &&
360 dp->dccps_pmtu_cookie > mtu) {
361 dccp_sync_mss(sk, mtu);
364 * From: draft-ietf-dccp-spec-11.txt
366 * DCCP-Sync packets are the best choice for upward probing,
367 * since DCCP-Sync probes do not risk application data loss.
369 dccp_send_sync(sk, dp->dccps_gsr);
370 } /* else let the usual retransmit timer handle it */
373 static void dccp_v4_ctl_send_ack(struct sk_buff *rxskb)
376 struct dccp_hdr *rxdh = dccp_hdr(rxskb), *dh;
377 const int dccp_hdr_ack_len = sizeof(struct dccp_hdr) +
378 sizeof(struct dccp_hdr_ext) +
379 sizeof(struct dccp_hdr_ack_bits);
382 if (((struct rtable *)rxskb->dst)->rt_type != RTN_LOCAL)
385 skb = alloc_skb(MAX_DCCP_HEADER + 15, GFP_ATOMIC);
389 /* Reserve space for headers. */
390 skb_reserve(skb, MAX_DCCP_HEADER);
392 skb->dst = dst_clone(rxskb->dst);
394 skb->h.raw = skb_push(skb, dccp_hdr_ack_len);
396 memset(dh, 0, dccp_hdr_ack_len);
398 /* Build DCCP header and checksum it. */
399 dh->dccph_type = DCCP_PKT_ACK;
400 dh->dccph_sport = rxdh->dccph_dport;
401 dh->dccph_dport = rxdh->dccph_sport;
402 dh->dccph_doff = dccp_hdr_ack_len / 4;
405 dccp_hdr_set_seq(dh, DCCP_SKB_CB(rxskb)->dccpd_ack_seq);
406 dccp_hdr_set_ack(dccp_hdr_ack_bits(skb), DCCP_SKB_CB(rxskb)->dccpd_seq);
408 bh_lock_sock(dccp_ctl_socket->sk);
409 err = ip_build_and_send_pkt(skb, dccp_ctl_socket->sk,
410 rxskb->nh.iph->daddr, rxskb->nh.iph->saddr, NULL);
411 bh_unlock_sock(dccp_ctl_socket->sk);
413 if (err == NET_XMIT_CN || err == 0) {
414 DCCP_INC_STATS_BH(DCCP_MIB_OUTSEGS);
415 DCCP_INC_STATS_BH(DCCP_MIB_OUTRSTS);
419 static void dccp_v4_reqsk_send_ack(struct sk_buff *skb, struct request_sock *req)
421 dccp_v4_ctl_send_ack(skb);
424 static int dccp_v4_send_response(struct sock *sk, struct request_sock *req,
425 struct dst_entry *dst)
430 /* First, grab a route. */
432 if (dst == NULL && (dst = inet_csk_route_req(sk, req)) == NULL)
435 skb = dccp_make_response(sk, dst, req);
437 const struct inet_request_sock *ireq = inet_rsk(req);
439 err = ip_build_and_send_pkt(skb, sk, ireq->loc_addr,
442 if (err == NET_XMIT_CN)
452 * This routine is called by the ICMP module when it gets some sort of error
453 * condition. If err < 0 then the socket should be closed and the error
454 * returned to the user. If err > 0 it's just the icmp type << 8 | icmp code.
455 * After adjustment header points to the first 8 bytes of the tcp header. We
456 * need to find the appropriate port.
458 * The locking strategy used here is very "optimistic". When someone else
459 * accesses the socket the ICMP is just dropped and for some paths there is no
460 * check at all. A more general error queue to queue errors for later handling
461 * is probably better.
463 void dccp_v4_err(struct sk_buff *skb, u32 info)
465 const struct iphdr *iph = (struct iphdr *)skb->data;
466 const struct dccp_hdr *dh = (struct dccp_hdr *)(skb->data + (iph->ihl << 2));
467 struct dccp_sock *dp;
468 struct inet_sock *inet;
469 const int type = skb->h.icmph->type;
470 const int code = skb->h.icmph->code;
475 if (skb->len < (iph->ihl << 2) + 8) {
476 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
480 sk = inet_lookup(&dccp_hashinfo, iph->daddr, dh->dccph_dport,
481 iph->saddr, dh->dccph_sport, inet_iif(skb));
483 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
487 if (sk->sk_state == DCCP_TIME_WAIT) {
488 inet_twsk_put((struct inet_timewait_sock *)sk);
493 /* If too many ICMPs get dropped on busy
494 * servers this needs to be solved differently.
496 if (sock_owned_by_user(sk))
497 NET_INC_STATS_BH(LINUX_MIB_LOCKDROPPEDICMPS);
499 if (sk->sk_state == DCCP_CLOSED)
503 seq = dccp_hdr_seq(skb);
504 if (sk->sk_state != DCCP_LISTEN &&
505 !between48(seq, dp->dccps_swl, dp->dccps_swh)) {
506 NET_INC_STATS(LINUX_MIB_OUTOFWINDOWICMPS);
511 case ICMP_SOURCE_QUENCH:
512 /* Just silently ignore these. */
514 case ICMP_PARAMETERPROB:
517 case ICMP_DEST_UNREACH:
518 if (code > NR_ICMP_UNREACH)
521 if (code == ICMP_FRAG_NEEDED) { /* PMTU discovery (RFC1191) */
522 if (!sock_owned_by_user(sk))
523 dccp_do_pmtu_discovery(sk, iph, info);
527 err = icmp_err_convert[code].errno;
529 case ICMP_TIME_EXCEEDED:
536 switch (sk->sk_state) {
537 struct request_sock *req , **prev;
539 if (sock_owned_by_user(sk))
541 req = inet_csk_search_req(sk, &prev, dh->dccph_dport,
542 iph->daddr, iph->saddr);
547 * ICMPs are not backlogged, hence we cannot get an established
552 if (seq != dccp_rsk(req)->dreq_iss) {
553 NET_INC_STATS_BH(LINUX_MIB_OUTOFWINDOWICMPS);
557 * Still in RESPOND, just remove it silently.
558 * There is no good way to pass the error to the newly
559 * created socket, and POSIX does not want network
560 * errors returned from accept().
562 inet_csk_reqsk_queue_drop(sk, req, prev);
565 case DCCP_REQUESTING:
567 if (!sock_owned_by_user(sk)) {
568 DCCP_INC_STATS_BH(DCCP_MIB_ATTEMPTFAILS);
571 sk->sk_error_report(sk);
575 sk->sk_err_soft = err;
579 /* If we've already connected we will keep trying
580 * until we time out, or the user gives up.
582 * rfc1122 4.2.3.9 allows to consider as hard errors
583 * only PROTO_UNREACH and PORT_UNREACH (well, FRAG_FAILED too,
584 * but it is obsoleted by pmtu discovery).
586 * Note, that in modern internet, where routing is unreliable
587 * and in each dark corner broken firewalls sit, sending random
588 * errors ordered by their masters even this two messages finally lose
589 * their original sense (even Linux sends invalid PORT_UNREACHs)
591 * Now we are in compliance with RFCs.
596 if (!sock_owned_by_user(sk) && inet->recverr) {
598 sk->sk_error_report(sk);
599 } else /* Only an error on timeout */
600 sk->sk_err_soft = err;
606 extern struct sk_buff *dccp_make_reset(struct sock *sk, struct dst_entry *dst, enum dccp_reset_codes code);
608 int dccp_v4_send_reset(struct sock *sk, enum dccp_reset_codes code)
612 * FIXME: what if rebuild_header fails?
613 * Should we be doing a rebuild_header here?
615 int err = inet_sk_rebuild_header(sk);
620 skb = dccp_make_reset(sk, sk->sk_dst_cache, code);
622 const struct dccp_sock *dp = dccp_sk(sk);
623 const struct inet_sock *inet = inet_sk(sk);
625 err = ip_build_and_send_pkt(skb, sk,
626 inet->saddr, inet->daddr, NULL);
627 if (err == NET_XMIT_CN)
630 ccid_hc_rx_exit(dp->dccps_hc_rx_ccid, sk);
631 ccid_hc_tx_exit(dp->dccps_hc_tx_ccid, sk);
637 static inline u64 dccp_v4_init_sequence(const struct sock *sk,
638 const struct sk_buff *skb)
640 return secure_dccp_sequence_number(skb->nh.iph->daddr,
642 dccp_hdr(skb)->dccph_dport,
643 dccp_hdr(skb)->dccph_sport);
646 int dccp_v4_conn_request(struct sock *sk, struct sk_buff *skb)
648 struct inet_request_sock *ireq;
650 struct request_sock *req;
651 struct dccp_request_sock *dreq;
652 const __u32 saddr = skb->nh.iph->saddr;
653 const __u32 daddr = skb->nh.iph->daddr;
654 struct dst_entry *dst = NULL;
656 /* Never answer to DCCP_PKT_REQUESTs send to broadcast or multicast */
657 if (((struct rtable *)skb->dst)->rt_flags &
658 (RTCF_BROADCAST | RTCF_MULTICAST))
662 * TW buckets are converted to open requests without
663 * limitations, they conserve resources and peer is
664 * evidently real one.
666 if (inet_csk_reqsk_queue_is_full(sk))
670 * Accept backlog is full. If we have already queued enough
671 * of warm entries in syn queue, drop request. It is better than
672 * clogging syn queue with openreqs with exponentially increasing
675 if (sk_acceptq_is_full(sk) && inet_csk_reqsk_queue_young(sk) > 1)
678 req = reqsk_alloc(sk->sk_prot->rsk_prot);
682 /* FIXME: process options */
684 dccp_openreq_init(req, &dp, skb);
686 ireq = inet_rsk(req);
687 ireq->loc_addr = daddr;
688 ireq->rmt_addr = saddr;
689 /* FIXME: Merge Aristeu's option parsing code when ready */
690 req->rcv_wnd = 100; /* Fake, option parsing will get the right value */
694 * Step 3: Process LISTEN state
696 * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init Cookie
698 * In fact we defer setting S.GSR, S.SWL, S.SWH to
699 * dccp_create_openreq_child.
701 dreq = dccp_rsk(req);
702 dreq->dreq_isr = DCCP_SKB_CB(skb)->dccpd_seq;
703 dreq->dreq_iss = dccp_v4_init_sequence(sk, skb);
704 dreq->dreq_service = dccp_hdr_request(skb)->dccph_req_service;
706 if (dccp_v4_send_response(sk, req, dst))
709 inet_csk_reqsk_queue_hash_add(sk, req, DCCP_TIMEOUT_INIT);
714 * FIXME: should be reqsk_free after implementing req->rsk_ops
718 DCCP_INC_STATS_BH(DCCP_MIB_ATTEMPTFAILS);
723 * The three way handshake has completed - we got a valid ACK or DATAACK -
724 * now create the new socket.
726 * This is the equivalent of TCP's tcp_v4_syn_recv_sock
728 struct sock *dccp_v4_request_recv_sock(struct sock *sk, struct sk_buff *skb,
729 struct request_sock *req,
730 struct dst_entry *dst)
732 struct inet_request_sock *ireq;
733 struct inet_sock *newinet;
734 struct dccp_sock *newdp;
737 if (sk_acceptq_is_full(sk))
740 if (dst == NULL && (dst = inet_csk_route_req(sk, req)) == NULL)
743 newsk = dccp_create_openreq_child(sk, req, skb);
747 sk_setup_caps(newsk, dst);
749 newdp = dccp_sk(newsk);
750 newinet = inet_sk(newsk);
751 ireq = inet_rsk(req);
752 newinet->daddr = ireq->rmt_addr;
753 newinet->rcv_saddr = ireq->loc_addr;
754 newinet->saddr = ireq->loc_addr;
755 newinet->opt = ireq->opt;
757 newinet->mc_index = inet_iif(skb);
758 newinet->mc_ttl = skb->nh.iph->ttl;
759 newinet->id = jiffies;
761 dccp_sync_mss(newsk, dst_mtu(dst));
763 __inet_hash(&dccp_hashinfo, newsk, 0);
764 __inet_inherit_port(&dccp_hashinfo, sk, newsk);
769 NET_INC_STATS_BH(LINUX_MIB_LISTENOVERFLOWS);
771 NET_INC_STATS_BH(LINUX_MIB_LISTENDROPS);
776 static struct sock *dccp_v4_hnd_req(struct sock *sk, struct sk_buff *skb)
778 const struct dccp_hdr *dh = dccp_hdr(skb);
779 const struct iphdr *iph = skb->nh.iph;
781 struct request_sock **prev;
782 /* Find possible connection requests. */
783 struct request_sock *req = inet_csk_search_req(sk, &prev,
785 iph->saddr, iph->daddr);
787 return dccp_check_req(sk, skb, req, prev);
789 nsk = __inet_lookup_established(&dccp_hashinfo,
790 iph->saddr, dh->dccph_sport,
791 iph->daddr, ntohs(dh->dccph_dport),
794 if (nsk->sk_state != DCCP_TIME_WAIT) {
798 inet_twsk_put((struct inet_timewait_sock *)nsk);
805 int dccp_v4_checksum(const struct sk_buff *skb, const u32 saddr, const u32 daddr)
807 const struct dccp_hdr* dh = dccp_hdr(skb);
811 if (dh->dccph_cscov == 0)
812 checksum_len = skb->len;
814 checksum_len = (dh->dccph_cscov + dh->dccph_x) * sizeof(u32);
815 checksum_len = checksum_len < skb->len ? checksum_len : skb->len;
818 tmp = csum_partial((unsigned char *)dh, checksum_len, 0);
819 return csum_tcpudp_magic(saddr, daddr, checksum_len, IPPROTO_DCCP, tmp);
822 static int dccp_v4_verify_checksum(struct sk_buff *skb,
823 const u32 saddr, const u32 daddr)
825 struct dccp_hdr *dh = dccp_hdr(skb);
829 if (dh->dccph_cscov == 0)
830 checksum_len = skb->len;
832 checksum_len = (dh->dccph_cscov + dh->dccph_x) * sizeof(u32);
833 checksum_len = checksum_len < skb->len ? checksum_len : skb->len;
835 tmp = csum_partial((unsigned char *)dh, checksum_len, 0);
836 return csum_tcpudp_magic(saddr, daddr, checksum_len, IPPROTO_DCCP, tmp) == 0 ? 0 : -1;
839 static struct dst_entry* dccp_v4_route_skb(struct sock *sk,
843 struct flowi fl = { .oif = ((struct rtable *)skb->dst)->rt_iif,
845 { .daddr = skb->nh.iph->saddr,
846 .saddr = skb->nh.iph->daddr,
847 .tos = RT_CONN_FLAGS(sk) } },
848 .proto = sk->sk_protocol,
850 { .sport = dccp_hdr(skb)->dccph_dport,
851 .dport = dccp_hdr(skb)->dccph_sport } } };
853 if (ip_route_output_flow(&rt, &fl, sk, 0)) {
854 IP_INC_STATS_BH(IPSTATS_MIB_OUTNOROUTES);
861 void dccp_v4_ctl_send_reset(struct sk_buff *rxskb)
864 struct dccp_hdr *rxdh = dccp_hdr(rxskb), *dh;
865 const int dccp_hdr_reset_len = sizeof(struct dccp_hdr) +
866 sizeof(struct dccp_hdr_ext) +
867 sizeof(struct dccp_hdr_reset);
869 struct dst_entry *dst;
871 /* Never send a reset in response to a reset. */
872 if (rxdh->dccph_type == DCCP_PKT_RESET)
875 if (((struct rtable *)rxskb->dst)->rt_type != RTN_LOCAL)
878 dst = dccp_v4_route_skb(dccp_ctl_socket->sk, rxskb);
882 skb = alloc_skb(MAX_DCCP_HEADER + 15, GFP_ATOMIC);
886 /* Reserve space for headers. */
887 skb_reserve(skb, MAX_DCCP_HEADER);
888 skb->dst = dst_clone(dst);
890 skb->h.raw = skb_push(skb, dccp_hdr_reset_len);
892 memset(dh, 0, dccp_hdr_reset_len);
894 /* Build DCCP header and checksum it. */
895 dh->dccph_type = DCCP_PKT_RESET;
896 dh->dccph_sport = rxdh->dccph_dport;
897 dh->dccph_dport = rxdh->dccph_sport;
898 dh->dccph_doff = dccp_hdr_reset_len / 4;
900 dccp_hdr_reset(skb)->dccph_reset_code = DCCP_SKB_CB(rxskb)->dccpd_reset_code;
902 dccp_hdr_set_seq(dh, DCCP_SKB_CB(rxskb)->dccpd_ack_seq);
903 dccp_hdr_set_ack(dccp_hdr_ack_bits(skb), DCCP_SKB_CB(rxskb)->dccpd_seq);
905 dh->dccph_checksum = dccp_v4_checksum(skb, rxskb->nh.iph->saddr,
906 rxskb->nh.iph->daddr);
908 bh_lock_sock(dccp_ctl_socket->sk);
909 err = ip_build_and_send_pkt(skb, dccp_ctl_socket->sk,
910 rxskb->nh.iph->daddr, rxskb->nh.iph->saddr, NULL);
911 bh_unlock_sock(dccp_ctl_socket->sk);
913 if (err == NET_XMIT_CN || err == 0) {
914 DCCP_INC_STATS_BH(DCCP_MIB_OUTSEGS);
915 DCCP_INC_STATS_BH(DCCP_MIB_OUTRSTS);
921 int dccp_v4_do_rcv(struct sock *sk, struct sk_buff *skb)
923 struct dccp_hdr *dh = dccp_hdr(skb);
925 if (sk->sk_state == DCCP_OPEN) { /* Fast path */
926 if (dccp_rcv_established(sk, skb, dh, skb->len))
932 * Step 3: Process LISTEN state
933 * If S.state == LISTEN,
934 * If P.type == Request or P contains a valid Init Cookie option,
935 * * Must scan the packet's options to check for an Init
936 * Cookie. Only the Init Cookie is processed here,
937 * however; other options are processed in Step 8. This
938 * scan need only be performed if the endpoint uses Init
940 * * Generate a new socket and switch to that socket *
941 * Set S := new socket for this port pair
943 * Choose S.ISS (initial seqno) or set from Init Cookie
944 * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init Cookie
945 * Continue with S.state == RESPOND
946 * * A Response packet will be generated in Step 11 *
948 * Generate Reset(No Connection) unless P.type == Reset
949 * Drop packet and return
951 * NOTE: the check for the packet types is done in dccp_rcv_state_process
953 if (sk->sk_state == DCCP_LISTEN) {
954 struct sock *nsk = dccp_v4_hnd_req(sk, skb);
960 if (dccp_child_process(sk, nsk, skb))
966 if (dccp_rcv_state_process(sk, skb, dh, skb->len))
971 DCCP_SKB_CB(skb)->dccpd_reset_code = DCCP_RESET_CODE_NO_CONNECTION;
972 dccp_v4_ctl_send_reset(skb);
978 static inline int dccp_invalid_packet(struct sk_buff *skb)
980 const struct dccp_hdr *dh;
982 if (skb->pkt_type != PACKET_HOST)
985 if (!pskb_may_pull(skb, sizeof(struct dccp_hdr))) {
986 dccp_pr_debug("pskb_may_pull failed\n");
992 /* If the packet type is not understood, drop packet and return */
993 if (dh->dccph_type >= DCCP_PKT_INVALID) {
994 dccp_pr_debug("invalid packet type\n");
999 * If P.Data Offset is too small for packet type, or too large for
1000 * packet, drop packet and return
1002 if (dh->dccph_doff < dccp_hdr_len(skb) / sizeof(u32)) {
1003 dccp_pr_debug("Offset(%u) too small 1\n", dh->dccph_doff);
1007 if (!pskb_may_pull(skb, dh->dccph_doff * sizeof(u32))) {
1008 dccp_pr_debug("P.Data Offset(%u) too small 2\n", dh->dccph_doff);
1015 * If P.type is not Data, Ack, or DataAck and P.X == 0 (the packet
1016 * has short sequence numbers), drop packet and return
1018 if (dh->dccph_x == 0 &&
1019 dh->dccph_type != DCCP_PKT_DATA &&
1020 dh->dccph_type != DCCP_PKT_ACK &&
1021 dh->dccph_type != DCCP_PKT_DATAACK) {
1022 dccp_pr_debug("P.type (%s) not Data, Ack nor DataAck and P.X == 0\n",
1023 dccp_packet_name(dh->dccph_type));
1027 /* If the header checksum is incorrect, drop packet and return */
1028 if (dccp_v4_verify_checksum(skb, skb->nh.iph->saddr,
1029 skb->nh.iph->daddr) < 0) {
1030 dccp_pr_debug("header checksum is incorrect\n");
1037 /* this is called when real data arrives */
1038 int dccp_v4_rcv(struct sk_buff *skb)
1040 const struct dccp_hdr *dh;
1044 /* Step 1: Check header basics: */
1046 if (dccp_invalid_packet(skb))
1052 * Use something like this to simulate some DATA/DATAACK loss to test
1053 * dccp_ackpkts_add, you'll get something like this on a session that
1054 * sends 10 DATA/DATAACK packets:
1056 * dccp_ackpkts_print: 281473596467422 |0,0|3,0|0,0|3,0|0,0|3,0|0,0|3,0|0,1|
1058 * 0, 0 means: DCCP_ACKPKTS_STATE_RECEIVED, RLE == just this packet
1059 * 0, 1 means: DCCP_ACKPKTS_STATE_RECEIVED, RLE == two adjacent packets with the same state
1060 * 3, 0 means: DCCP_ACKPKTS_STATE_NOT_RECEIVED, RLE == just this packet
1064 * 281473596467422 was received
1065 * 281473596467421 was not received
1066 * 281473596467420 was received
1067 * 281473596467419 was not received
1068 * 281473596467418 was received
1069 * 281473596467417 was not received
1070 * 281473596467416 was received
1071 * 281473596467415 was not received
1072 * 281473596467414 was received
1073 * 281473596467413 was received (this one was the 3way handshake RESPONSE)
1076 if (dh->dccph_type == DCCP_PKT_DATA || dh->dccph_type == DCCP_PKT_DATAACK) {
1077 static int discard = 0;
1086 DCCP_SKB_CB(skb)->dccpd_seq = dccp_hdr_seq(skb);
1087 DCCP_SKB_CB(skb)->dccpd_type = dh->dccph_type;
1089 dccp_pr_debug("%8.8s "
1090 "src=%u.%u.%u.%u@%-5d "
1091 "dst=%u.%u.%u.%u@%-5d seq=%llu",
1092 dccp_packet_name(dh->dccph_type),
1093 NIPQUAD(skb->nh.iph->saddr), ntohs(dh->dccph_sport),
1094 NIPQUAD(skb->nh.iph->daddr), ntohs(dh->dccph_dport),
1095 (unsigned long long) DCCP_SKB_CB(skb)->dccpd_seq);
1097 if (dccp_packet_without_ack(skb)) {
1098 DCCP_SKB_CB(skb)->dccpd_ack_seq = DCCP_PKT_WITHOUT_ACK_SEQ;
1099 dccp_pr_debug_cat("\n");
1101 DCCP_SKB_CB(skb)->dccpd_ack_seq = dccp_hdr_ack_seq(skb);
1102 dccp_pr_debug_cat(", ack=%llu\n",
1103 (unsigned long long)
1104 DCCP_SKB_CB(skb)->dccpd_ack_seq);
1108 * Look up flow ID in table and get corresponding socket */
1109 sk = __inet_lookup(&dccp_hashinfo,
1110 skb->nh.iph->saddr, dh->dccph_sport,
1111 skb->nh.iph->daddr, ntohs(dh->dccph_dport),
1117 * Generate Reset(No Connection) unless P.type == Reset
1118 * Drop packet and return
1121 dccp_pr_debug("failed to look up flow ID in table and "
1122 "get corresponding socket\n");
1123 goto no_dccp_socket;
1128 * ... or S.state == TIMEWAIT,
1129 * Generate Reset(No Connection) unless P.type == Reset
1130 * Drop packet and return
1133 if (sk->sk_state == DCCP_TIME_WAIT) {
1134 dccp_pr_debug("sk->sk_state == DCCP_TIME_WAIT: discard_and_relse\n");
1135 goto discard_and_relse;
1138 if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb)) {
1139 dccp_pr_debug("xfrm4_policy_check failed\n");
1140 goto discard_and_relse;
1143 if (sk_filter(sk, skb, 0)) {
1144 dccp_pr_debug("sk_filter failed\n");
1145 goto discard_and_relse;
1152 if (!sock_owned_by_user(sk))
1153 rc = dccp_v4_do_rcv(sk, skb);
1155 sk_add_backlog(sk, skb);
1162 if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
1166 * Generate Reset(No Connection) unless P.type == Reset
1167 * Drop packet and return
1169 if (dh->dccph_type != DCCP_PKT_RESET) {
1170 DCCP_SKB_CB(skb)->dccpd_reset_code = DCCP_RESET_CODE_NO_CONNECTION;
1171 dccp_v4_ctl_send_reset(skb);
1175 /* Discard frame. */
1184 static int dccp_v4_init_sock(struct sock *sk)
1186 struct dccp_sock *dp = dccp_sk(sk);
1187 static int dccp_ctl_socket_init = 1;
1189 dccp_options_init(&dp->dccps_options);
1191 if (dp->dccps_options.dccpo_send_ack_vector) {
1192 dp->dccps_hc_rx_ackpkts = dccp_ackpkts_alloc(DCCP_MAX_ACK_VECTOR_LEN,
1195 if (dp->dccps_hc_rx_ackpkts == NULL)
1200 * FIXME: We're hardcoding the CCID, and doing this at this point makes
1201 * the listening (master) sock get CCID control blocks, which is not
1202 * necessary, but for now, to not mess with the test userspace apps,
1203 * lets leave it here, later the real solution is to do this in a
1204 * setsockopt(CCIDs-I-want/accept). -acme
1206 if (likely(!dccp_ctl_socket_init)) {
1207 dp->dccps_hc_rx_ccid = ccid_init(dp->dccps_options.dccpo_ccid, sk);
1208 dp->dccps_hc_tx_ccid = ccid_init(dp->dccps_options.dccpo_ccid, sk);
1209 if (dp->dccps_hc_rx_ccid == NULL ||
1210 dp->dccps_hc_tx_ccid == NULL) {
1211 ccid_exit(dp->dccps_hc_rx_ccid, sk);
1212 ccid_exit(dp->dccps_hc_tx_ccid, sk);
1213 dccp_ackpkts_free(dp->dccps_hc_rx_ackpkts);
1214 dp->dccps_hc_rx_ackpkts = NULL;
1215 dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL;
1219 dccp_ctl_socket_init = 0;
1221 dccp_init_xmit_timers(sk);
1222 inet_csk(sk)->icsk_rto = DCCP_TIMEOUT_INIT;
1223 sk->sk_state = DCCP_CLOSED;
1224 dp->dccps_mss_cache = 536;
1225 dp->dccps_role = DCCP_ROLE_UNDEFINED;
1230 int dccp_v4_destroy_sock(struct sock *sk)
1232 struct dccp_sock *dp = dccp_sk(sk);
1235 * DCCP doesn't use sk_qrite_queue, just sk_send_head
1236 * for retransmissions
1238 if (sk->sk_send_head != NULL) {
1239 kfree_skb(sk->sk_send_head);
1240 sk->sk_send_head = NULL;
1243 /* Clean up a referenced DCCP bind bucket. */
1244 if (inet_csk(sk)->icsk_bind_hash != NULL)
1245 inet_put_port(&dccp_hashinfo, sk);
1247 dccp_ackpkts_free(dp->dccps_hc_rx_ackpkts);
1248 dp->dccps_hc_rx_ackpkts = NULL;
1249 ccid_exit(dp->dccps_hc_rx_ccid, sk);
1250 ccid_exit(dp->dccps_hc_tx_ccid, sk);
1251 dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL;
1256 static void dccp_v4_reqsk_destructor(struct request_sock *req)
1258 kfree(inet_rsk(req)->opt);
1261 static struct request_sock_ops dccp_request_sock_ops = {
1263 .obj_size = sizeof(struct dccp_request_sock),
1264 .rtx_syn_ack = dccp_v4_send_response,
1265 .send_ack = dccp_v4_reqsk_send_ack,
1266 .destructor = dccp_v4_reqsk_destructor,
1267 .send_reset = dccp_v4_ctl_send_reset,
1270 struct proto dccp_v4_prot = {
1272 .owner = THIS_MODULE,
1273 .close = dccp_close,
1274 .connect = dccp_v4_connect,
1275 .disconnect = dccp_disconnect,
1276 .ioctl = dccp_ioctl,
1277 .init = dccp_v4_init_sock,
1278 .setsockopt = dccp_setsockopt,
1279 .getsockopt = dccp_getsockopt,
1280 .sendmsg = dccp_sendmsg,
1281 .recvmsg = dccp_recvmsg,
1282 .backlog_rcv = dccp_v4_do_rcv,
1283 .hash = dccp_v4_hash,
1284 .unhash = dccp_v4_unhash,
1285 .accept = inet_csk_accept,
1286 .get_port = dccp_v4_get_port,
1287 .shutdown = dccp_shutdown,
1288 .destroy = dccp_v4_destroy_sock,
1289 .orphan_count = &dccp_orphan_count,
1290 .max_header = MAX_DCCP_HEADER,
1291 .obj_size = sizeof(struct dccp_sock),
1292 .rsk_prot = &dccp_request_sock_ops,
1293 .twsk_obj_size = sizeof(struct inet_timewait_sock), /* FIXME! create dccp_timewait_sock */