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>
30 struct inet_hashinfo __cacheline_aligned dccp_hashinfo = {
31 .lhash_lock = RW_LOCK_UNLOCKED,
32 .lhash_users = ATOMIC_INIT(0),
33 .lhash_wait = __WAIT_QUEUE_HEAD_INITIALIZER(dccp_hashinfo.lhash_wait),
36 EXPORT_SYMBOL_GPL(dccp_hashinfo);
38 static int dccp_v4_get_port(struct sock *sk, const unsigned short snum)
40 return inet_csk_get_port(&dccp_hashinfo, sk, snum,
41 inet_csk_bind_conflict);
44 static void dccp_v4_hash(struct sock *sk)
46 inet_hash(&dccp_hashinfo, sk);
49 void dccp_unhash(struct sock *sk)
51 inet_unhash(&dccp_hashinfo, sk);
54 EXPORT_SYMBOL_GPL(dccp_unhash);
56 /* called with local bh disabled */
57 static int __dccp_v4_check_established(struct sock *sk, const __u16 lport,
58 struct inet_timewait_sock **twp)
60 struct inet_sock *inet = inet_sk(sk);
61 const u32 daddr = inet->rcv_saddr;
62 const u32 saddr = inet->daddr;
63 const int dif = sk->sk_bound_dev_if;
64 INET_ADDR_COOKIE(acookie, saddr, daddr)
65 const __u32 ports = INET_COMBINED_PORTS(inet->dport, lport);
66 unsigned int hash = inet_ehashfn(daddr, lport, saddr, inet->dport);
67 struct inet_ehash_bucket *head = inet_ehash_bucket(&dccp_hashinfo, hash);
68 const struct sock *sk2;
69 const struct hlist_node *node;
70 struct inet_timewait_sock *tw;
72 prefetch(head->chain.first);
73 write_lock(&head->lock);
75 /* Check TIME-WAIT sockets first. */
76 sk_for_each(sk2, node, &(head + dccp_hashinfo.ehash_size)->chain) {
79 if (INET_TW_MATCH(sk2, hash, acookie, saddr, daddr, ports, dif))
84 /* And established part... */
85 sk_for_each(sk2, node, &head->chain) {
86 if (INET_MATCH(sk2, hash, acookie, saddr, daddr, ports, dif))
90 /* Must record num and sport now. Otherwise we will see
91 * in hash table socket with a funny identity. */
93 inet->sport = htons(lport);
95 BUG_TRAP(sk_unhashed(sk));
96 __sk_add_node(sk, &head->chain);
97 sock_prot_inc_use(sk->sk_prot);
98 write_unlock(&head->lock);
102 NET_INC_STATS_BH(LINUX_MIB_TIMEWAITRECYCLED);
103 } else if (tw != NULL) {
104 /* Silly. Should hash-dance instead... */
105 inet_twsk_deschedule(tw, &dccp_death_row);
106 NET_INC_STATS_BH(LINUX_MIB_TIMEWAITRECYCLED);
114 write_unlock(&head->lock);
115 return -EADDRNOTAVAIL;
119 * Bind a port for a connect operation and hash it.
121 static int dccp_v4_hash_connect(struct sock *sk)
123 const unsigned short snum = inet_sk(sk)->num;
124 struct inet_bind_hashbucket *head;
125 struct inet_bind_bucket *tb;
129 int low = sysctl_local_port_range[0];
130 int high = sysctl_local_port_range[1];
131 int remaining = (high - low) + 1;
132 int rover = net_random() % (high - low) + low;
133 struct hlist_node *node;
134 struct inet_timewait_sock *tw = NULL;
138 head = &dccp_hashinfo.bhash[inet_bhashfn(rover,
139 dccp_hashinfo.bhash_size)];
140 spin_lock(&head->lock);
142 /* Does not bother with rcv_saddr checks,
143 * because the established check is already
146 inet_bind_bucket_for_each(tb, node, &head->chain) {
147 if (tb->port == rover) {
148 BUG_TRAP(!hlist_empty(&tb->owners));
149 if (tb->fastreuse >= 0)
151 if (!__dccp_v4_check_established(sk,
159 tb = inet_bind_bucket_create(dccp_hashinfo.bind_bucket_cachep,
162 spin_unlock(&head->lock);
169 spin_unlock(&head->lock);
172 } while (--remaining > 0);
176 return -EADDRNOTAVAIL;
179 /* All locks still held and bhs disabled */
180 inet_bind_hash(sk, tb, rover);
181 if (sk_unhashed(sk)) {
182 inet_sk(sk)->sport = htons(rover);
183 __inet_hash(&dccp_hashinfo, sk, 0);
185 spin_unlock(&head->lock);
188 inet_twsk_deschedule(tw, &dccp_death_row);
196 head = &dccp_hashinfo.bhash[inet_bhashfn(snum,
197 dccp_hashinfo.bhash_size)];
198 tb = inet_csk(sk)->icsk_bind_hash;
199 spin_lock_bh(&head->lock);
200 if (sk_head(&tb->owners) == sk && sk->sk_bind_node.next == NULL) {
201 __inet_hash(&dccp_hashinfo, sk, 0);
202 spin_unlock_bh(&head->lock);
205 spin_unlock(&head->lock);
206 /* No definite answer... Walk to established hash table */
207 ret = __dccp_v4_check_established(sk, snum, NULL);
214 int dccp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
216 struct inet_sock *inet = inet_sk(sk);
217 struct dccp_sock *dp = dccp_sk(sk);
218 const struct sockaddr_in *usin = (struct sockaddr_in *)uaddr;
224 dp->dccps_role = DCCP_ROLE_CLIENT;
226 if (dccp_service_not_initialized(sk))
229 if (addr_len < sizeof(struct sockaddr_in))
232 if (usin->sin_family != AF_INET)
233 return -EAFNOSUPPORT;
235 nexthop = daddr = usin->sin_addr.s_addr;
236 if (inet->opt != NULL && inet->opt->srr) {
239 nexthop = inet->opt->faddr;
242 tmp = ip_route_connect(&rt, nexthop, inet->saddr,
243 RT_CONN_FLAGS(sk), sk->sk_bound_dev_if,
245 inet->sport, usin->sin_port, sk);
249 if (rt->rt_flags & (RTCF_MULTICAST | RTCF_BROADCAST)) {
254 if (inet->opt == NULL || !inet->opt->srr)
257 if (inet->saddr == 0)
258 inet->saddr = rt->rt_src;
259 inet->rcv_saddr = inet->saddr;
261 inet->dport = usin->sin_port;
264 dp->dccps_ext_header_len = 0;
265 if (inet->opt != NULL)
266 dp->dccps_ext_header_len = inet->opt->optlen;
268 * Socket identity is still unknown (sport may be zero).
269 * However we set state to DCCP_REQUESTING and not releasing socket
270 * lock select source port, enter ourselves into the hash tables and
271 * complete initialization after this.
273 dccp_set_state(sk, DCCP_REQUESTING);
274 err = dccp_v4_hash_connect(sk);
278 err = ip_route_newports(&rt, inet->sport, inet->dport, sk);
282 /* OK, now commit destination to socket. */
283 sk_setup_caps(sk, &rt->u.dst);
286 dp->dccps_iss = secure_dccp_sequence_number(inet->saddr,
290 dccp_update_gss(sk, dp->dccps_iss);
292 inet->id = dp->dccps_iss ^ jiffies;
294 err = dccp_connect(sk);
302 * This unhashes the socket and releases the local port, if necessary.
304 dccp_set_state(sk, DCCP_CLOSED);
306 sk->sk_route_caps = 0;
311 EXPORT_SYMBOL_GPL(dccp_v4_connect);
314 * This routine does path mtu discovery as defined in RFC1191.
316 static inline void dccp_do_pmtu_discovery(struct sock *sk,
317 const struct iphdr *iph,
320 struct dst_entry *dst;
321 const struct inet_sock *inet = inet_sk(sk);
322 const struct dccp_sock *dp = dccp_sk(sk);
324 /* We are not interested in DCCP_LISTEN and request_socks (RESPONSEs
325 * send out by Linux are always < 576bytes so they should go through
328 if (sk->sk_state == DCCP_LISTEN)
331 /* We don't check in the destentry if pmtu discovery is forbidden
332 * on this route. We just assume that no packet_to_big packets
333 * are send back when pmtu discovery is not active.
334 * There is a small race when the user changes this flag in the
335 * route, but I think that's acceptable.
337 if ((dst = __sk_dst_check(sk, 0)) == NULL)
340 dst->ops->update_pmtu(dst, mtu);
342 /* Something is about to be wrong... Remember soft error
343 * for the case, if this connection will not able to recover.
345 if (mtu < dst_mtu(dst) && ip_dont_fragment(sk, dst))
346 sk->sk_err_soft = EMSGSIZE;
350 if (inet->pmtudisc != IP_PMTUDISC_DONT &&
351 dp->dccps_pmtu_cookie > mtu) {
352 dccp_sync_mss(sk, mtu);
355 * From: draft-ietf-dccp-spec-11.txt
357 * DCCP-Sync packets are the best choice for upward
358 * probing, since DCCP-Sync probes do not risk application
361 dccp_send_sync(sk, dp->dccps_gsr, DCCP_PKT_SYNC);
362 } /* else let the usual retransmit timer handle it */
365 static void dccp_v4_ctl_send_ack(struct sk_buff *rxskb)
368 struct dccp_hdr *rxdh = dccp_hdr(rxskb), *dh;
369 const int dccp_hdr_ack_len = sizeof(struct dccp_hdr) +
370 sizeof(struct dccp_hdr_ext) +
371 sizeof(struct dccp_hdr_ack_bits);
374 if (((struct rtable *)rxskb->dst)->rt_type != RTN_LOCAL)
377 skb = alloc_skb(MAX_DCCP_HEADER + 15, GFP_ATOMIC);
381 /* Reserve space for headers. */
382 skb_reserve(skb, MAX_DCCP_HEADER);
384 skb->dst = dst_clone(rxskb->dst);
386 skb->h.raw = skb_push(skb, dccp_hdr_ack_len);
388 memset(dh, 0, dccp_hdr_ack_len);
390 /* Build DCCP header and checksum it. */
391 dh->dccph_type = DCCP_PKT_ACK;
392 dh->dccph_sport = rxdh->dccph_dport;
393 dh->dccph_dport = rxdh->dccph_sport;
394 dh->dccph_doff = dccp_hdr_ack_len / 4;
397 dccp_hdr_set_seq(dh, DCCP_SKB_CB(rxskb)->dccpd_ack_seq);
398 dccp_hdr_set_ack(dccp_hdr_ack_bits(skb),
399 DCCP_SKB_CB(rxskb)->dccpd_seq);
401 bh_lock_sock(dccp_ctl_socket->sk);
402 err = ip_build_and_send_pkt(skb, dccp_ctl_socket->sk,
403 rxskb->nh.iph->daddr,
404 rxskb->nh.iph->saddr, NULL);
405 bh_unlock_sock(dccp_ctl_socket->sk);
407 if (err == NET_XMIT_CN || err == 0) {
408 DCCP_INC_STATS_BH(DCCP_MIB_OUTSEGS);
409 DCCP_INC_STATS_BH(DCCP_MIB_OUTRSTS);
413 static void dccp_v4_reqsk_send_ack(struct sk_buff *skb,
414 struct request_sock *req)
416 dccp_v4_ctl_send_ack(skb);
419 static int dccp_v4_send_response(struct sock *sk, struct request_sock *req,
420 struct dst_entry *dst)
425 /* First, grab a route. */
427 if (dst == NULL && (dst = inet_csk_route_req(sk, req)) == NULL)
430 skb = dccp_make_response(sk, dst, req);
432 const struct inet_request_sock *ireq = inet_rsk(req);
434 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
435 err = ip_build_and_send_pkt(skb, sk, ireq->loc_addr,
438 if (err == NET_XMIT_CN)
448 * This routine is called by the ICMP module when it gets some sort of error
449 * condition. If err < 0 then the socket should be closed and the error
450 * returned to the user. If err > 0 it's just the icmp type << 8 | icmp code.
451 * After adjustment header points to the first 8 bytes of the tcp header. We
452 * need to find the appropriate port.
454 * The locking strategy used here is very "optimistic". When someone else
455 * accesses the socket the ICMP is just dropped and for some paths there is no
456 * check at all. A more general error queue to queue errors for later handling
457 * is probably better.
459 void dccp_v4_err(struct sk_buff *skb, u32 info)
461 const struct iphdr *iph = (struct iphdr *)skb->data;
462 const struct dccp_hdr *dh = (struct dccp_hdr *)(skb->data +
464 struct dccp_sock *dp;
465 struct inet_sock *inet;
466 const int type = skb->h.icmph->type;
467 const int code = skb->h.icmph->code;
472 if (skb->len < (iph->ihl << 2) + 8) {
473 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
477 sk = inet_lookup(&dccp_hashinfo, iph->daddr, dh->dccph_dport,
478 iph->saddr, dh->dccph_sport, inet_iif(skb));
480 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
484 if (sk->sk_state == DCCP_TIME_WAIT) {
485 inet_twsk_put((struct inet_timewait_sock *)sk);
490 /* If too many ICMPs get dropped on busy
491 * servers this needs to be solved differently.
493 if (sock_owned_by_user(sk))
494 NET_INC_STATS_BH(LINUX_MIB_LOCKDROPPEDICMPS);
496 if (sk->sk_state == DCCP_CLOSED)
500 seq = dccp_hdr_seq(skb);
501 if (sk->sk_state != DCCP_LISTEN &&
502 !between48(seq, dp->dccps_swl, dp->dccps_swh)) {
503 NET_INC_STATS(LINUX_MIB_OUTOFWINDOWICMPS);
508 case ICMP_SOURCE_QUENCH:
509 /* Just silently ignore these. */
511 case ICMP_PARAMETERPROB:
514 case ICMP_DEST_UNREACH:
515 if (code > NR_ICMP_UNREACH)
518 if (code == ICMP_FRAG_NEEDED) { /* PMTU discovery (RFC1191) */
519 if (!sock_owned_by_user(sk))
520 dccp_do_pmtu_discovery(sk, iph, info);
524 err = icmp_err_convert[code].errno;
526 case ICMP_TIME_EXCEEDED:
533 switch (sk->sk_state) {
534 struct request_sock *req , **prev;
536 if (sock_owned_by_user(sk))
538 req = inet_csk_search_req(sk, &prev, dh->dccph_dport,
539 iph->daddr, iph->saddr);
544 * ICMPs are not backlogged, hence we cannot get an established
549 if (seq != dccp_rsk(req)->dreq_iss) {
550 NET_INC_STATS_BH(LINUX_MIB_OUTOFWINDOWICMPS);
554 * Still in RESPOND, just remove it silently.
555 * There is no good way to pass the error to the newly
556 * created socket, and POSIX does not want network
557 * errors returned from accept().
559 inet_csk_reqsk_queue_drop(sk, req, prev);
562 case DCCP_REQUESTING:
564 if (!sock_owned_by_user(sk)) {
565 DCCP_INC_STATS_BH(DCCP_MIB_ATTEMPTFAILS);
568 sk->sk_error_report(sk);
572 sk->sk_err_soft = err;
576 /* If we've already connected we will keep trying
577 * until we time out, or the user gives up.
579 * rfc1122 4.2.3.9 allows to consider as hard errors
580 * only PROTO_UNREACH and PORT_UNREACH (well, FRAG_FAILED too,
581 * but it is obsoleted by pmtu discovery).
583 * Note, that in modern internet, where routing is unreliable
584 * and in each dark corner broken firewalls sit, sending random
585 * errors ordered by their masters even this two messages finally lose
586 * their original sense (even Linux sends invalid PORT_UNREACHs)
588 * Now we are in compliance with RFCs.
593 if (!sock_owned_by_user(sk) && inet->recverr) {
595 sk->sk_error_report(sk);
596 } else /* Only an error on timeout */
597 sk->sk_err_soft = err;
603 /* This routine computes an IPv4 DCCP checksum. */
604 void dccp_v4_send_check(struct sock *sk, int len, struct sk_buff *skb)
606 const struct inet_sock *inet = inet_sk(sk);
607 struct dccp_hdr *dh = dccp_hdr(skb);
609 dh->dccph_checksum = dccp_v4_checksum(skb, inet->saddr, inet->daddr);
612 EXPORT_SYMBOL_GPL(dccp_v4_send_check);
614 int dccp_v4_send_reset(struct sock *sk, enum dccp_reset_codes code)
618 * FIXME: what if rebuild_header fails?
619 * Should we be doing a rebuild_header here?
621 int err = inet_sk_rebuild_header(sk);
626 skb = dccp_make_reset(sk, sk->sk_dst_cache, code);
628 const struct inet_sock *inet = inet_sk(sk);
630 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
631 err = ip_build_and_send_pkt(skb, sk,
632 inet->saddr, inet->daddr, NULL);
633 if (err == NET_XMIT_CN)
640 static inline u64 dccp_v4_init_sequence(const struct sock *sk,
641 const struct sk_buff *skb)
643 return secure_dccp_sequence_number(skb->nh.iph->daddr,
645 dccp_hdr(skb)->dccph_dport,
646 dccp_hdr(skb)->dccph_sport);
649 int dccp_v4_conn_request(struct sock *sk, struct sk_buff *skb)
651 struct inet_request_sock *ireq;
653 struct request_sock *req;
654 struct dccp_request_sock *dreq;
655 const __u32 saddr = skb->nh.iph->saddr;
656 const __u32 daddr = skb->nh.iph->daddr;
657 const __u32 service = dccp_hdr_request(skb)->dccph_req_service;
658 struct dccp_skb_cb *dcb = DCCP_SKB_CB(skb);
659 __u8 reset_code = DCCP_RESET_CODE_TOO_BUSY;
661 /* Never answer to DCCP_PKT_REQUESTs send to broadcast or multicast */
662 if (((struct rtable *)skb->dst)->rt_flags &
663 (RTCF_BROADCAST | RTCF_MULTICAST)) {
664 reset_code = DCCP_RESET_CODE_NO_CONNECTION;
668 if (dccp_bad_service_code(sk, service)) {
669 reset_code = DCCP_RESET_CODE_BAD_SERVICE_CODE;
673 * TW buckets are converted to open requests without
674 * limitations, they conserve resources and peer is
675 * evidently real one.
677 if (inet_csk_reqsk_queue_is_full(sk))
681 * Accept backlog is full. If we have already queued enough
682 * of warm entries in syn queue, drop request. It is better than
683 * clogging syn queue with openreqs with exponentially increasing
686 if (sk_acceptq_is_full(sk) && inet_csk_reqsk_queue_young(sk) > 1)
689 req = reqsk_alloc(sk->sk_prot->rsk_prot);
693 /* FIXME: process options */
695 dccp_openreq_init(req, &dp, skb);
697 ireq = inet_rsk(req);
698 ireq->loc_addr = daddr;
699 ireq->rmt_addr = saddr;
700 req->rcv_wnd = 100; /* Fake, option parsing will get the
705 * Step 3: Process LISTEN state
707 * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init Cookie
709 * In fact we defer setting S.GSR, S.SWL, S.SWH to
710 * dccp_create_openreq_child.
712 dreq = dccp_rsk(req);
713 dreq->dreq_isr = dcb->dccpd_seq;
714 dreq->dreq_iss = dccp_v4_init_sequence(sk, skb);
715 dreq->dreq_service = service;
717 if (dccp_v4_send_response(sk, req, NULL))
720 inet_csk_reqsk_queue_hash_add(sk, req, DCCP_TIMEOUT_INIT);
725 * FIXME: should be reqsk_free after implementing req->rsk_ops
729 DCCP_INC_STATS_BH(DCCP_MIB_ATTEMPTFAILS);
730 dcb->dccpd_reset_code = reset_code;
734 EXPORT_SYMBOL_GPL(dccp_v4_conn_request);
737 * The three way handshake has completed - we got a valid ACK or DATAACK -
738 * now create the new socket.
740 * This is the equivalent of TCP's tcp_v4_syn_recv_sock
742 struct sock *dccp_v4_request_recv_sock(struct sock *sk, struct sk_buff *skb,
743 struct request_sock *req,
744 struct dst_entry *dst)
746 struct inet_request_sock *ireq;
747 struct inet_sock *newinet;
748 struct dccp_sock *newdp;
751 if (sk_acceptq_is_full(sk))
754 if (dst == NULL && (dst = inet_csk_route_req(sk, req)) == NULL)
757 newsk = dccp_create_openreq_child(sk, req, skb);
761 sk_setup_caps(newsk, dst);
763 newdp = dccp_sk(newsk);
764 newinet = inet_sk(newsk);
765 ireq = inet_rsk(req);
766 newinet->daddr = ireq->rmt_addr;
767 newinet->rcv_saddr = ireq->loc_addr;
768 newinet->saddr = ireq->loc_addr;
769 newinet->opt = ireq->opt;
771 newinet->mc_index = inet_iif(skb);
772 newinet->mc_ttl = skb->nh.iph->ttl;
773 newinet->id = jiffies;
775 dccp_sync_mss(newsk, dst_mtu(dst));
777 __inet_hash(&dccp_hashinfo, newsk, 0);
778 __inet_inherit_port(&dccp_hashinfo, sk, newsk);
783 NET_INC_STATS_BH(LINUX_MIB_LISTENOVERFLOWS);
785 NET_INC_STATS_BH(LINUX_MIB_LISTENDROPS);
790 EXPORT_SYMBOL_GPL(dccp_v4_request_recv_sock);
792 static struct sock *dccp_v4_hnd_req(struct sock *sk, struct sk_buff *skb)
794 const struct dccp_hdr *dh = dccp_hdr(skb);
795 const struct iphdr *iph = skb->nh.iph;
797 struct request_sock **prev;
798 /* Find possible connection requests. */
799 struct request_sock *req = inet_csk_search_req(sk, &prev,
801 iph->saddr, iph->daddr);
803 return dccp_check_req(sk, skb, req, prev);
805 nsk = __inet_lookup_established(&dccp_hashinfo,
806 iph->saddr, dh->dccph_sport,
807 iph->daddr, ntohs(dh->dccph_dport),
810 if (nsk->sk_state != DCCP_TIME_WAIT) {
814 inet_twsk_put((struct inet_timewait_sock *)nsk);
821 int dccp_v4_checksum(const struct sk_buff *skb, const u32 saddr,
824 const struct dccp_hdr* dh = dccp_hdr(skb);
828 if (dh->dccph_cscov == 0)
829 checksum_len = skb->len;
831 checksum_len = (dh->dccph_cscov + dh->dccph_x) * sizeof(u32);
832 checksum_len = checksum_len < skb->len ? checksum_len :
836 tmp = csum_partial((unsigned char *)dh, checksum_len, 0);
837 return csum_tcpudp_magic(saddr, daddr, checksum_len,
841 static int dccp_v4_verify_checksum(struct sk_buff *skb,
842 const u32 saddr, const u32 daddr)
844 struct dccp_hdr *dh = dccp_hdr(skb);
848 if (dh->dccph_cscov == 0)
849 checksum_len = skb->len;
851 checksum_len = (dh->dccph_cscov + dh->dccph_x) * sizeof(u32);
852 checksum_len = checksum_len < skb->len ? checksum_len :
855 tmp = csum_partial((unsigned char *)dh, checksum_len, 0);
856 return csum_tcpudp_magic(saddr, daddr, checksum_len,
857 IPPROTO_DCCP, tmp) == 0 ? 0 : -1;
860 static struct dst_entry* dccp_v4_route_skb(struct sock *sk,
864 struct flowi fl = { .oif = ((struct rtable *)skb->dst)->rt_iif,
866 { .daddr = skb->nh.iph->saddr,
867 .saddr = skb->nh.iph->daddr,
868 .tos = RT_CONN_FLAGS(sk) } },
869 .proto = sk->sk_protocol,
871 { .sport = dccp_hdr(skb)->dccph_dport,
872 .dport = dccp_hdr(skb)->dccph_sport }
876 if (ip_route_output_flow(&rt, &fl, sk, 0)) {
877 IP_INC_STATS_BH(IPSTATS_MIB_OUTNOROUTES);
884 static void dccp_v4_ctl_send_reset(struct sk_buff *rxskb)
887 struct dccp_hdr *rxdh = dccp_hdr(rxskb), *dh;
888 const int dccp_hdr_reset_len = sizeof(struct dccp_hdr) +
889 sizeof(struct dccp_hdr_ext) +
890 sizeof(struct dccp_hdr_reset);
892 struct dst_entry *dst;
895 /* Never send a reset in response to a reset. */
896 if (rxdh->dccph_type == DCCP_PKT_RESET)
899 if (((struct rtable *)rxskb->dst)->rt_type != RTN_LOCAL)
902 dst = dccp_v4_route_skb(dccp_ctl_socket->sk, rxskb);
906 skb = alloc_skb(MAX_DCCP_HEADER + 15, GFP_ATOMIC);
910 /* Reserve space for headers. */
911 skb_reserve(skb, MAX_DCCP_HEADER);
912 skb->dst = dst_clone(dst);
914 skb->h.raw = skb_push(skb, dccp_hdr_reset_len);
916 memset(dh, 0, dccp_hdr_reset_len);
918 /* Build DCCP header and checksum it. */
919 dh->dccph_type = DCCP_PKT_RESET;
920 dh->dccph_sport = rxdh->dccph_dport;
921 dh->dccph_dport = rxdh->dccph_sport;
922 dh->dccph_doff = dccp_hdr_reset_len / 4;
924 dccp_hdr_reset(skb)->dccph_reset_code =
925 DCCP_SKB_CB(rxskb)->dccpd_reset_code;
927 /* See "8.3.1. Abnormal Termination" in draft-ietf-dccp-spec-11 */
929 if (DCCP_SKB_CB(rxskb)->dccpd_ack_seq != DCCP_PKT_WITHOUT_ACK_SEQ)
930 dccp_set_seqno(&seqno, DCCP_SKB_CB(rxskb)->dccpd_ack_seq + 1);
932 dccp_hdr_set_seq(dh, seqno);
933 dccp_hdr_set_ack(dccp_hdr_ack_bits(skb),
934 DCCP_SKB_CB(rxskb)->dccpd_seq);
936 dh->dccph_checksum = dccp_v4_checksum(skb, rxskb->nh.iph->saddr,
937 rxskb->nh.iph->daddr);
939 bh_lock_sock(dccp_ctl_socket->sk);
940 err = ip_build_and_send_pkt(skb, dccp_ctl_socket->sk,
941 rxskb->nh.iph->daddr,
942 rxskb->nh.iph->saddr, NULL);
943 bh_unlock_sock(dccp_ctl_socket->sk);
945 if (err == NET_XMIT_CN || err == 0) {
946 DCCP_INC_STATS_BH(DCCP_MIB_OUTSEGS);
947 DCCP_INC_STATS_BH(DCCP_MIB_OUTRSTS);
953 int dccp_v4_do_rcv(struct sock *sk, struct sk_buff *skb)
955 struct dccp_hdr *dh = dccp_hdr(skb);
957 if (sk->sk_state == DCCP_OPEN) { /* Fast path */
958 if (dccp_rcv_established(sk, skb, dh, skb->len))
964 * Step 3: Process LISTEN state
965 * If S.state == LISTEN,
966 * If P.type == Request or P contains a valid Init Cookie
968 * * Must scan the packet's options to check for an Init
969 * Cookie. Only the Init Cookie is processed here,
970 * however; other options are processed in Step 8. This
971 * scan need only be performed if the endpoint uses Init
973 * * Generate a new socket and switch to that socket *
974 * Set S := new socket for this port pair
976 * Choose S.ISS (initial seqno) or set from Init Cookie
977 * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init Cookie
978 * Continue with S.state == RESPOND
979 * * A Response packet will be generated in Step 11 *
981 * Generate Reset(No Connection) unless P.type == Reset
982 * Drop packet and return
984 * NOTE: the check for the packet types is done in
985 * dccp_rcv_state_process
987 if (sk->sk_state == DCCP_LISTEN) {
988 struct sock *nsk = dccp_v4_hnd_req(sk, skb);
994 if (dccp_child_process(sk, nsk, skb))
1000 if (dccp_rcv_state_process(sk, skb, dh, skb->len))
1005 dccp_v4_ctl_send_reset(skb);
1011 EXPORT_SYMBOL_GPL(dccp_v4_do_rcv);
1013 int dccp_invalid_packet(struct sk_buff *skb)
1015 const struct dccp_hdr *dh;
1017 if (skb->pkt_type != PACKET_HOST)
1020 if (!pskb_may_pull(skb, sizeof(struct dccp_hdr))) {
1021 LIMIT_NETDEBUG(KERN_WARNING "DCCP: pskb_may_pull failed\n");
1027 /* If the packet type is not understood, drop packet and return */
1028 if (dh->dccph_type >= DCCP_PKT_INVALID) {
1029 LIMIT_NETDEBUG(KERN_WARNING "DCCP: invalid packet type\n");
1034 * If P.Data Offset is too small for packet type, or too large for
1035 * packet, drop packet and return
1037 if (dh->dccph_doff < dccp_hdr_len(skb) / sizeof(u32)) {
1038 LIMIT_NETDEBUG(KERN_WARNING "DCCP: P.Data Offset(%u) "
1044 if (!pskb_may_pull(skb, dh->dccph_doff * sizeof(u32))) {
1045 LIMIT_NETDEBUG(KERN_WARNING "DCCP: P.Data Offset(%u) "
1054 * If P.type is not Data, Ack, or DataAck and P.X == 0 (the packet
1055 * has short sequence numbers), drop packet and return
1057 if (dh->dccph_x == 0 &&
1058 dh->dccph_type != DCCP_PKT_DATA &&
1059 dh->dccph_type != DCCP_PKT_ACK &&
1060 dh->dccph_type != DCCP_PKT_DATAACK) {
1061 LIMIT_NETDEBUG(KERN_WARNING "DCCP: P.type (%s) not Data, Ack "
1062 "nor DataAck and P.X == 0\n",
1063 dccp_packet_name(dh->dccph_type));
1070 EXPORT_SYMBOL_GPL(dccp_invalid_packet);
1072 /* this is called when real data arrives */
1073 int dccp_v4_rcv(struct sk_buff *skb)
1075 const struct dccp_hdr *dh;
1079 /* Step 1: Check header basics: */
1081 if (dccp_invalid_packet(skb))
1084 /* If the header checksum is incorrect, drop packet and return */
1085 if (dccp_v4_verify_checksum(skb, skb->nh.iph->saddr,
1086 skb->nh.iph->daddr) < 0) {
1087 LIMIT_NETDEBUG(KERN_WARNING "%s: incorrect header checksum\n",
1094 DCCP_SKB_CB(skb)->dccpd_seq = dccp_hdr_seq(skb);
1095 DCCP_SKB_CB(skb)->dccpd_type = dh->dccph_type;
1097 dccp_pr_debug("%8.8s "
1098 "src=%u.%u.%u.%u@%-5d "
1099 "dst=%u.%u.%u.%u@%-5d seq=%llu",
1100 dccp_packet_name(dh->dccph_type),
1101 NIPQUAD(skb->nh.iph->saddr), ntohs(dh->dccph_sport),
1102 NIPQUAD(skb->nh.iph->daddr), ntohs(dh->dccph_dport),
1103 (unsigned long long) DCCP_SKB_CB(skb)->dccpd_seq);
1105 if (dccp_packet_without_ack(skb)) {
1106 DCCP_SKB_CB(skb)->dccpd_ack_seq = DCCP_PKT_WITHOUT_ACK_SEQ;
1107 dccp_pr_debug_cat("\n");
1109 DCCP_SKB_CB(skb)->dccpd_ack_seq = dccp_hdr_ack_seq(skb);
1110 dccp_pr_debug_cat(", ack=%llu\n",
1111 (unsigned long long)
1112 DCCP_SKB_CB(skb)->dccpd_ack_seq);
1116 * Look up flow ID in table and get corresponding socket */
1117 sk = __inet_lookup(&dccp_hashinfo,
1118 skb->nh.iph->saddr, dh->dccph_sport,
1119 skb->nh.iph->daddr, ntohs(dh->dccph_dport),
1125 * Generate Reset(No Connection) unless P.type == Reset
1126 * Drop packet and return
1129 dccp_pr_debug("failed to look up flow ID in table and "
1130 "get corresponding socket\n");
1131 goto no_dccp_socket;
1136 * ... or S.state == TIMEWAIT,
1137 * Generate Reset(No Connection) unless P.type == Reset
1138 * Drop packet and return
1141 if (sk->sk_state == DCCP_TIME_WAIT) {
1142 dccp_pr_debug("sk->sk_state == DCCP_TIME_WAIT: "
1147 if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb)) {
1148 dccp_pr_debug("xfrm4_policy_check failed\n");
1149 goto discard_and_relse;
1152 if (sk_filter(sk, skb, 0)) {
1153 dccp_pr_debug("sk_filter failed\n");
1154 goto discard_and_relse;
1161 if (!sock_owned_by_user(sk))
1162 rc = dccp_v4_do_rcv(sk, skb);
1164 sk_add_backlog(sk, skb);
1171 if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
1175 * Generate Reset(No Connection) unless P.type == Reset
1176 * Drop packet and return
1178 if (dh->dccph_type != DCCP_PKT_RESET) {
1179 DCCP_SKB_CB(skb)->dccpd_reset_code =
1180 DCCP_RESET_CODE_NO_CONNECTION;
1181 dccp_v4_ctl_send_reset(skb);
1185 /* Discard frame. */
1194 inet_twsk_put((struct inet_timewait_sock *)sk);
1195 goto no_dccp_socket;
1198 struct inet_connection_sock_af_ops dccp_ipv4_af_ops = {
1199 .queue_xmit = ip_queue_xmit,
1200 .send_check = dccp_v4_send_check,
1201 .rebuild_header = inet_sk_rebuild_header,
1202 .conn_request = dccp_v4_conn_request,
1203 .syn_recv_sock = dccp_v4_request_recv_sock,
1204 .net_header_len = sizeof(struct iphdr),
1205 .setsockopt = ip_setsockopt,
1206 .getsockopt = ip_getsockopt,
1207 .addr2sockaddr = inet_csk_addr2sockaddr,
1208 .sockaddr_len = sizeof(struct sockaddr_in),
1211 int dccp_v4_init_sock(struct sock *sk)
1213 struct dccp_sock *dp = dccp_sk(sk);
1214 static int dccp_ctl_socket_init = 1;
1216 dccp_options_init(&dp->dccps_options);
1217 do_gettimeofday(&dp->dccps_epoch);
1219 if (dp->dccps_options.dccpo_send_ack_vector) {
1220 dp->dccps_hc_rx_ackvec = dccp_ackvec_alloc(DCCP_MAX_ACKVEC_LEN,
1222 if (dp->dccps_hc_rx_ackvec == NULL)
1227 * FIXME: We're hardcoding the CCID, and doing this at this point makes
1228 * the listening (master) sock get CCID control blocks, which is not
1229 * necessary, but for now, to not mess with the test userspace apps,
1230 * lets leave it here, later the real solution is to do this in a
1231 * setsockopt(CCIDs-I-want/accept). -acme
1233 if (likely(!dccp_ctl_socket_init)) {
1234 dp->dccps_hc_rx_ccid = ccid_init(dp->dccps_options.dccpo_rx_ccid,
1236 dp->dccps_hc_tx_ccid = ccid_init(dp->dccps_options.dccpo_tx_ccid,
1238 if (dp->dccps_hc_rx_ccid == NULL ||
1239 dp->dccps_hc_tx_ccid == NULL) {
1240 ccid_exit(dp->dccps_hc_rx_ccid, sk);
1241 ccid_exit(dp->dccps_hc_tx_ccid, sk);
1242 if (dp->dccps_options.dccpo_send_ack_vector) {
1243 dccp_ackvec_free(dp->dccps_hc_rx_ackvec);
1244 dp->dccps_hc_rx_ackvec = NULL;
1246 dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL;
1250 dccp_ctl_socket_init = 0;
1252 dccp_init_xmit_timers(sk);
1253 inet_csk(sk)->icsk_rto = DCCP_TIMEOUT_INIT;
1254 sk->sk_state = DCCP_CLOSED;
1255 sk->sk_write_space = dccp_write_space;
1256 inet_csk(sk)->icsk_af_ops = &dccp_ipv4_af_ops;
1257 dp->dccps_mss_cache = 536;
1258 dp->dccps_role = DCCP_ROLE_UNDEFINED;
1259 dp->dccps_service = DCCP_SERVICE_INVALID_VALUE;
1264 EXPORT_SYMBOL_GPL(dccp_v4_init_sock);
1266 int dccp_v4_destroy_sock(struct sock *sk)
1268 struct dccp_sock *dp = dccp_sk(sk);
1271 * DCCP doesn't use sk_write_queue, just sk_send_head
1272 * for retransmissions
1274 if (sk->sk_send_head != NULL) {
1275 kfree_skb(sk->sk_send_head);
1276 sk->sk_send_head = NULL;
1279 /* Clean up a referenced DCCP bind bucket. */
1280 if (inet_csk(sk)->icsk_bind_hash != NULL)
1281 inet_put_port(&dccp_hashinfo, sk);
1283 kfree(dp->dccps_service_list);
1284 dp->dccps_service_list = NULL;
1286 ccid_hc_rx_exit(dp->dccps_hc_rx_ccid, sk);
1287 ccid_hc_tx_exit(dp->dccps_hc_tx_ccid, sk);
1288 if (dp->dccps_options.dccpo_send_ack_vector) {
1289 dccp_ackvec_free(dp->dccps_hc_rx_ackvec);
1290 dp->dccps_hc_rx_ackvec = NULL;
1292 ccid_exit(dp->dccps_hc_rx_ccid, sk);
1293 ccid_exit(dp->dccps_hc_tx_ccid, sk);
1294 dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL;
1299 EXPORT_SYMBOL_GPL(dccp_v4_destroy_sock);
1301 static void dccp_v4_reqsk_destructor(struct request_sock *req)
1303 kfree(inet_rsk(req)->opt);
1306 static struct request_sock_ops dccp_request_sock_ops = {
1308 .obj_size = sizeof(struct dccp_request_sock),
1309 .rtx_syn_ack = dccp_v4_send_response,
1310 .send_ack = dccp_v4_reqsk_send_ack,
1311 .destructor = dccp_v4_reqsk_destructor,
1312 .send_reset = dccp_v4_ctl_send_reset,
1315 struct proto dccp_prot = {
1317 .owner = THIS_MODULE,
1318 .close = dccp_close,
1319 .connect = dccp_v4_connect,
1320 .disconnect = dccp_disconnect,
1321 .ioctl = dccp_ioctl,
1322 .init = dccp_v4_init_sock,
1323 .setsockopt = dccp_setsockopt,
1324 .getsockopt = dccp_getsockopt,
1325 .sendmsg = dccp_sendmsg,
1326 .recvmsg = dccp_recvmsg,
1327 .backlog_rcv = dccp_v4_do_rcv,
1328 .hash = dccp_v4_hash,
1329 .unhash = dccp_unhash,
1330 .accept = inet_csk_accept,
1331 .get_port = dccp_v4_get_port,
1332 .shutdown = dccp_shutdown,
1333 .destroy = dccp_v4_destroy_sock,
1334 .orphan_count = &dccp_orphan_count,
1335 .max_header = MAX_DCCP_HEADER,
1336 .obj_size = sizeof(struct dccp_sock),
1337 .rsk_prot = &dccp_request_sock_ops,
1338 .twsk_obj_size = sizeof(struct inet_timewait_sock),