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(struct sk_buff *skb)
807 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_fold(tmp);
822 static int dccp_v4_verify_checksum(struct sk_buff *skb)
824 struct dccp_hdr *th = dccp_hdr(skb);
825 const u16 remote_checksum = th->dccph_checksum;
828 /* FIXME: don't mess with skb payload */
829 th->dccph_checksum = 0; /* zero it for computation */
831 local_checksum = dccp_v4_checksum(skb);
833 /* FIXME: don't mess with skb payload */
834 th->dccph_checksum = remote_checksum; /* put it back */
836 return remote_checksum == local_checksum ? 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);
907 bh_lock_sock(dccp_ctl_socket->sk);
908 err = ip_build_and_send_pkt(skb, dccp_ctl_socket->sk,
909 rxskb->nh.iph->daddr, rxskb->nh.iph->saddr, NULL);
910 bh_unlock_sock(dccp_ctl_socket->sk);
912 if (err == NET_XMIT_CN || err == 0) {
913 DCCP_INC_STATS_BH(DCCP_MIB_OUTSEGS);
914 DCCP_INC_STATS_BH(DCCP_MIB_OUTRSTS);
920 int dccp_v4_do_rcv(struct sock *sk, struct sk_buff *skb)
922 struct dccp_hdr *dh = dccp_hdr(skb);
924 if (sk->sk_state == DCCP_OPEN) { /* Fast path */
925 if (dccp_rcv_established(sk, skb, dh, skb->len))
931 * Step 3: Process LISTEN state
932 * If S.state == LISTEN,
933 * If P.type == Request or P contains a valid Init Cookie option,
934 * * Must scan the packet's options to check for an Init
935 * Cookie. Only the Init Cookie is processed here,
936 * however; other options are processed in Step 8. This
937 * scan need only be performed if the endpoint uses Init
939 * * Generate a new socket and switch to that socket *
940 * Set S := new socket for this port pair
942 * Choose S.ISS (initial seqno) or set from Init Cookie
943 * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init Cookie
944 * Continue with S.state == RESPOND
945 * * A Response packet will be generated in Step 11 *
947 * Generate Reset(No Connection) unless P.type == Reset
948 * Drop packet and return
950 * NOTE: the check for the packet types is done in dccp_rcv_state_process
952 if (sk->sk_state == DCCP_LISTEN) {
953 struct sock *nsk = dccp_v4_hnd_req(sk, skb);
959 if (dccp_child_process(sk, nsk, skb))
965 if (dccp_rcv_state_process(sk, skb, dh, skb->len))
970 DCCP_SKB_CB(skb)->dccpd_reset_code = DCCP_RESET_CODE_NO_CONNECTION;
971 dccp_v4_ctl_send_reset(skb);
977 static inline int dccp_invalid_packet(struct sk_buff *skb)
979 const struct dccp_hdr *dh;
981 if (skb->pkt_type != PACKET_HOST)
984 if (!pskb_may_pull(skb, sizeof(struct dccp_hdr))) {
985 dccp_pr_debug("pskb_may_pull failed\n");
991 /* If the packet type is not understood, drop packet and return */
992 if (dh->dccph_type >= DCCP_PKT_INVALID) {
993 dccp_pr_debug("invalid packet type\n");
998 * If P.Data Offset is too small for packet type, or too large for
999 * packet, drop packet and return
1001 if (dh->dccph_doff < dccp_hdr_len(skb) / sizeof(u32)) {
1002 dccp_pr_debug("Offset(%u) too small 1\n", dh->dccph_doff);
1006 if (!pskb_may_pull(skb, dh->dccph_doff * sizeof(u32))) {
1007 dccp_pr_debug("P.Data Offset(%u) too small 2\n", dh->dccph_doff);
1014 * If P.type is not Data, Ack, or DataAck and P.X == 0 (the packet
1015 * has short sequence numbers), drop packet and return
1017 if (dh->dccph_x == 0 &&
1018 dh->dccph_type != DCCP_PKT_DATA &&
1019 dh->dccph_type != DCCP_PKT_ACK &&
1020 dh->dccph_type != DCCP_PKT_DATAACK) {
1021 dccp_pr_debug("P.type (%s) not Data, Ack nor DataAck and P.X == 0\n",
1022 dccp_packet_name(dh->dccph_type));
1026 /* If the header checksum is incorrect, drop packet and return */
1027 if (dccp_v4_verify_checksum(skb) < 0) {
1028 dccp_pr_debug("header checksum is incorrect\n");
1035 /* this is called when real data arrives */
1036 int dccp_v4_rcv(struct sk_buff *skb)
1038 const struct dccp_hdr *dh;
1042 /* Step 1: Check header basics: */
1044 if (dccp_invalid_packet(skb))
1050 * Use something like this to simulate some DATA/DATAACK loss to test
1051 * dccp_ackpkts_add, you'll get something like this on a session that
1052 * sends 10 DATA/DATAACK packets:
1054 * dccp_ackpkts_print: 281473596467422 |0,0|3,0|0,0|3,0|0,0|3,0|0,0|3,0|0,1|
1056 * 0, 0 means: DCCP_ACKPKTS_STATE_RECEIVED, RLE == just this packet
1057 * 0, 1 means: DCCP_ACKPKTS_STATE_RECEIVED, RLE == two adjacent packets with the same state
1058 * 3, 0 means: DCCP_ACKPKTS_STATE_NOT_RECEIVED, RLE == just this packet
1062 * 281473596467422 was received
1063 * 281473596467421 was not received
1064 * 281473596467420 was received
1065 * 281473596467419 was not received
1066 * 281473596467418 was received
1067 * 281473596467417 was not received
1068 * 281473596467416 was received
1069 * 281473596467415 was not received
1070 * 281473596467414 was received
1071 * 281473596467413 was received (this one was the 3way handshake RESPONSE)
1074 if (dh->dccph_type == DCCP_PKT_DATA || dh->dccph_type == DCCP_PKT_DATAACK) {
1075 static int discard = 0;
1084 DCCP_SKB_CB(skb)->dccpd_seq = dccp_hdr_seq(skb);
1085 DCCP_SKB_CB(skb)->dccpd_type = dh->dccph_type;
1087 dccp_pr_debug("%8.8s "
1088 "src=%u.%u.%u.%u@%-5d "
1089 "dst=%u.%u.%u.%u@%-5d seq=%llu",
1090 dccp_packet_name(dh->dccph_type),
1091 NIPQUAD(skb->nh.iph->saddr), ntohs(dh->dccph_sport),
1092 NIPQUAD(skb->nh.iph->daddr), ntohs(dh->dccph_dport),
1093 DCCP_SKB_CB(skb)->dccpd_seq);
1095 if (dccp_packet_without_ack(skb)) {
1096 DCCP_SKB_CB(skb)->dccpd_ack_seq = DCCP_PKT_WITHOUT_ACK_SEQ;
1097 dccp_pr_debug_cat("\n");
1099 DCCP_SKB_CB(skb)->dccpd_ack_seq = dccp_hdr_ack_seq(skb);
1100 dccp_pr_debug_cat(", ack=%llu\n", DCCP_SKB_CB(skb)->dccpd_ack_seq);
1104 * Look up flow ID in table and get corresponding socket */
1105 sk = __inet_lookup(&dccp_hashinfo,
1106 skb->nh.iph->saddr, dh->dccph_sport,
1107 skb->nh.iph->daddr, ntohs(dh->dccph_dport),
1113 * Generate Reset(No Connection) unless P.type == Reset
1114 * Drop packet and return
1117 dccp_pr_debug("failed to look up flow ID in table and "
1118 "get corresponding socket\n");
1119 goto no_dccp_socket;
1124 * ... or S.state == TIMEWAIT,
1125 * Generate Reset(No Connection) unless P.type == Reset
1126 * Drop packet and return
1129 if (sk->sk_state == DCCP_TIME_WAIT) {
1130 dccp_pr_debug("sk->sk_state == DCCP_TIME_WAIT: discard_and_relse\n");
1131 goto discard_and_relse;
1134 if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb)) {
1135 dccp_pr_debug("xfrm4_policy_check failed\n");
1136 goto discard_and_relse;
1139 if (sk_filter(sk, skb, 0)) {
1140 dccp_pr_debug("sk_filter failed\n");
1141 goto discard_and_relse;
1148 if (!sock_owned_by_user(sk))
1149 rc = dccp_v4_do_rcv(sk, skb);
1151 sk_add_backlog(sk, skb);
1158 if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
1162 * Generate Reset(No Connection) unless P.type == Reset
1163 * Drop packet and return
1165 if (dh->dccph_type != DCCP_PKT_RESET) {
1166 DCCP_SKB_CB(skb)->dccpd_reset_code = DCCP_RESET_CODE_NO_CONNECTION;
1167 dccp_v4_ctl_send_reset(skb);
1171 /* Discard frame. */
1180 static int dccp_v4_init_sock(struct sock *sk)
1182 struct dccp_sock *dp = dccp_sk(sk);
1183 static int dccp_ctl_socket_init = 1;
1185 dccp_options_init(&dp->dccps_options);
1187 if (dp->dccps_options.dccpo_send_ack_vector) {
1188 dp->dccps_hc_rx_ackpkts = dccp_ackpkts_alloc(DCCP_MAX_ACK_VECTOR_LEN,
1191 if (dp->dccps_hc_rx_ackpkts == NULL)
1196 * FIXME: We're hardcoding the CCID, and doing this at this point makes
1197 * the listening (master) sock get CCID control blocks, which is not
1198 * necessary, but for now, to not mess with the test userspace apps,
1199 * lets leave it here, later the real solution is to do this in a
1200 * setsockopt(CCIDs-I-want/accept). -acme
1202 if (likely(!dccp_ctl_socket_init)) {
1203 dp->dccps_hc_rx_ccid = ccid_init(dp->dccps_options.dccpo_ccid, sk);
1204 dp->dccps_hc_tx_ccid = ccid_init(dp->dccps_options.dccpo_ccid, sk);
1205 if (dp->dccps_hc_rx_ccid == NULL ||
1206 dp->dccps_hc_tx_ccid == NULL) {
1207 ccid_exit(dp->dccps_hc_rx_ccid, sk);
1208 ccid_exit(dp->dccps_hc_tx_ccid, sk);
1209 dccp_ackpkts_free(dp->dccps_hc_rx_ackpkts);
1210 dp->dccps_hc_rx_ackpkts = NULL;
1211 dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL;
1215 dccp_ctl_socket_init = 0;
1217 dccp_init_xmit_timers(sk);
1218 sk->sk_state = DCCP_CLOSED;
1219 dp->dccps_mss_cache = 536;
1220 dp->dccps_role = DCCP_ROLE_UNDEFINED;
1225 int dccp_v4_destroy_sock(struct sock *sk)
1227 struct dccp_sock *dp = dccp_sk(sk);
1230 * DCCP doesn't use sk_qrite_queue, just sk_send_head
1231 * for retransmissions
1233 if (sk->sk_send_head != NULL) {
1234 kfree_skb(sk->sk_send_head);
1235 sk->sk_send_head = NULL;
1238 /* Clean up a referenced DCCP bind bucket. */
1239 if (inet_csk(sk)->icsk_bind_hash != NULL)
1240 inet_put_port(&dccp_hashinfo, sk);
1242 dccp_ackpkts_free(dp->dccps_hc_rx_ackpkts);
1243 dp->dccps_hc_rx_ackpkts = NULL;
1244 ccid_exit(dp->dccps_hc_rx_ccid, sk);
1245 ccid_exit(dp->dccps_hc_tx_ccid, sk);
1246 dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL;
1251 static void dccp_v4_reqsk_destructor(struct request_sock *req)
1253 kfree(inet_rsk(req)->opt);
1256 static struct request_sock_ops dccp_request_sock_ops = {
1258 .obj_size = sizeof(struct dccp_request_sock),
1259 .rtx_syn_ack = dccp_v4_send_response,
1260 .send_ack = dccp_v4_reqsk_send_ack,
1261 .destructor = dccp_v4_reqsk_destructor,
1262 .send_reset = dccp_v4_ctl_send_reset,
1265 struct proto dccp_v4_prot = {
1267 .owner = THIS_MODULE,
1268 .close = dccp_close,
1269 .connect = dccp_v4_connect,
1270 .disconnect = dccp_disconnect,
1271 .ioctl = dccp_ioctl,
1272 .init = dccp_v4_init_sock,
1273 .setsockopt = dccp_setsockopt,
1274 .getsockopt = dccp_getsockopt,
1275 .sendmsg = dccp_sendmsg,
1276 .recvmsg = dccp_recvmsg,
1277 .backlog_rcv = dccp_v4_do_rcv,
1278 .hash = dccp_v4_hash,
1279 .unhash = dccp_v4_unhash,
1280 .accept = inet_csk_accept,
1281 .get_port = dccp_v4_get_port,
1282 .shutdown = dccp_shutdown,
1283 .destroy = dccp_v4_destroy_sock,
1284 .orphan_count = &dccp_orphan_count,
1285 .max_header = MAX_DCCP_HEADER,
1286 .obj_size = sizeof(struct dccp_sock),
1287 .rsk_prot = &dccp_request_sock_ops,
1288 .twsk_obj_size = sizeof(struct inet_timewait_sock), /* FIXME! create dccp_timewait_sock */