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);
726 DCCP_INC_STATS_BH(DCCP_MIB_ATTEMPTFAILS);
727 dcb->dccpd_reset_code = reset_code;
731 EXPORT_SYMBOL_GPL(dccp_v4_conn_request);
734 * The three way handshake has completed - we got a valid ACK or DATAACK -
735 * now create the new socket.
737 * This is the equivalent of TCP's tcp_v4_syn_recv_sock
739 struct sock *dccp_v4_request_recv_sock(struct sock *sk, struct sk_buff *skb,
740 struct request_sock *req,
741 struct dst_entry *dst)
743 struct inet_request_sock *ireq;
744 struct inet_sock *newinet;
745 struct dccp_sock *newdp;
748 if (sk_acceptq_is_full(sk))
751 if (dst == NULL && (dst = inet_csk_route_req(sk, req)) == NULL)
754 newsk = dccp_create_openreq_child(sk, req, skb);
758 sk_setup_caps(newsk, dst);
760 newdp = dccp_sk(newsk);
761 newinet = inet_sk(newsk);
762 ireq = inet_rsk(req);
763 newinet->daddr = ireq->rmt_addr;
764 newinet->rcv_saddr = ireq->loc_addr;
765 newinet->saddr = ireq->loc_addr;
766 newinet->opt = ireq->opt;
768 newinet->mc_index = inet_iif(skb);
769 newinet->mc_ttl = skb->nh.iph->ttl;
770 newinet->id = jiffies;
772 dccp_sync_mss(newsk, dst_mtu(dst));
774 __inet_hash(&dccp_hashinfo, newsk, 0);
775 __inet_inherit_port(&dccp_hashinfo, sk, newsk);
780 NET_INC_STATS_BH(LINUX_MIB_LISTENOVERFLOWS);
782 NET_INC_STATS_BH(LINUX_MIB_LISTENDROPS);
787 EXPORT_SYMBOL_GPL(dccp_v4_request_recv_sock);
789 static struct sock *dccp_v4_hnd_req(struct sock *sk, struct sk_buff *skb)
791 const struct dccp_hdr *dh = dccp_hdr(skb);
792 const struct iphdr *iph = skb->nh.iph;
794 struct request_sock **prev;
795 /* Find possible connection requests. */
796 struct request_sock *req = inet_csk_search_req(sk, &prev,
798 iph->saddr, iph->daddr);
800 return dccp_check_req(sk, skb, req, prev);
802 nsk = __inet_lookup_established(&dccp_hashinfo,
803 iph->saddr, dh->dccph_sport,
804 iph->daddr, ntohs(dh->dccph_dport),
807 if (nsk->sk_state != DCCP_TIME_WAIT) {
811 inet_twsk_put((struct inet_timewait_sock *)nsk);
818 int dccp_v4_checksum(const struct sk_buff *skb, const u32 saddr,
821 const struct dccp_hdr* dh = dccp_hdr(skb);
825 if (dh->dccph_cscov == 0)
826 checksum_len = skb->len;
828 checksum_len = (dh->dccph_cscov + dh->dccph_x) * sizeof(u32);
829 checksum_len = checksum_len < skb->len ? checksum_len :
833 tmp = csum_partial((unsigned char *)dh, checksum_len, 0);
834 return csum_tcpudp_magic(saddr, daddr, checksum_len,
838 static int dccp_v4_verify_checksum(struct sk_buff *skb,
839 const u32 saddr, const u32 daddr)
841 struct dccp_hdr *dh = dccp_hdr(skb);
845 if (dh->dccph_cscov == 0)
846 checksum_len = skb->len;
848 checksum_len = (dh->dccph_cscov + dh->dccph_x) * sizeof(u32);
849 checksum_len = checksum_len < skb->len ? checksum_len :
852 tmp = csum_partial((unsigned char *)dh, checksum_len, 0);
853 return csum_tcpudp_magic(saddr, daddr, checksum_len,
854 IPPROTO_DCCP, tmp) == 0 ? 0 : -1;
857 static struct dst_entry* dccp_v4_route_skb(struct sock *sk,
861 struct flowi fl = { .oif = ((struct rtable *)skb->dst)->rt_iif,
863 { .daddr = skb->nh.iph->saddr,
864 .saddr = skb->nh.iph->daddr,
865 .tos = RT_CONN_FLAGS(sk) } },
866 .proto = sk->sk_protocol,
868 { .sport = dccp_hdr(skb)->dccph_dport,
869 .dport = dccp_hdr(skb)->dccph_sport }
873 if (ip_route_output_flow(&rt, &fl, sk, 0)) {
874 IP_INC_STATS_BH(IPSTATS_MIB_OUTNOROUTES);
881 static void dccp_v4_ctl_send_reset(struct sk_buff *rxskb)
884 struct dccp_hdr *rxdh = dccp_hdr(rxskb), *dh;
885 const int dccp_hdr_reset_len = sizeof(struct dccp_hdr) +
886 sizeof(struct dccp_hdr_ext) +
887 sizeof(struct dccp_hdr_reset);
889 struct dst_entry *dst;
892 /* Never send a reset in response to a reset. */
893 if (rxdh->dccph_type == DCCP_PKT_RESET)
896 if (((struct rtable *)rxskb->dst)->rt_type != RTN_LOCAL)
899 dst = dccp_v4_route_skb(dccp_ctl_socket->sk, rxskb);
903 skb = alloc_skb(MAX_DCCP_HEADER + 15, GFP_ATOMIC);
907 /* Reserve space for headers. */
908 skb_reserve(skb, MAX_DCCP_HEADER);
909 skb->dst = dst_clone(dst);
911 skb->h.raw = skb_push(skb, dccp_hdr_reset_len);
913 memset(dh, 0, dccp_hdr_reset_len);
915 /* Build DCCP header and checksum it. */
916 dh->dccph_type = DCCP_PKT_RESET;
917 dh->dccph_sport = rxdh->dccph_dport;
918 dh->dccph_dport = rxdh->dccph_sport;
919 dh->dccph_doff = dccp_hdr_reset_len / 4;
921 dccp_hdr_reset(skb)->dccph_reset_code =
922 DCCP_SKB_CB(rxskb)->dccpd_reset_code;
924 /* See "8.3.1. Abnormal Termination" in draft-ietf-dccp-spec-11 */
926 if (DCCP_SKB_CB(rxskb)->dccpd_ack_seq != DCCP_PKT_WITHOUT_ACK_SEQ)
927 dccp_set_seqno(&seqno, DCCP_SKB_CB(rxskb)->dccpd_ack_seq + 1);
929 dccp_hdr_set_seq(dh, seqno);
930 dccp_hdr_set_ack(dccp_hdr_ack_bits(skb),
931 DCCP_SKB_CB(rxskb)->dccpd_seq);
933 dh->dccph_checksum = dccp_v4_checksum(skb, rxskb->nh.iph->saddr,
934 rxskb->nh.iph->daddr);
936 bh_lock_sock(dccp_ctl_socket->sk);
937 err = ip_build_and_send_pkt(skb, dccp_ctl_socket->sk,
938 rxskb->nh.iph->daddr,
939 rxskb->nh.iph->saddr, NULL);
940 bh_unlock_sock(dccp_ctl_socket->sk);
942 if (err == NET_XMIT_CN || err == 0) {
943 DCCP_INC_STATS_BH(DCCP_MIB_OUTSEGS);
944 DCCP_INC_STATS_BH(DCCP_MIB_OUTRSTS);
950 int dccp_v4_do_rcv(struct sock *sk, struct sk_buff *skb)
952 struct dccp_hdr *dh = dccp_hdr(skb);
954 if (sk->sk_state == DCCP_OPEN) { /* Fast path */
955 if (dccp_rcv_established(sk, skb, dh, skb->len))
961 * Step 3: Process LISTEN state
962 * If S.state == LISTEN,
963 * If P.type == Request or P contains a valid Init Cookie
965 * * Must scan the packet's options to check for an Init
966 * Cookie. Only the Init Cookie is processed here,
967 * however; other options are processed in Step 8. This
968 * scan need only be performed if the endpoint uses Init
970 * * Generate a new socket and switch to that socket *
971 * Set S := new socket for this port pair
973 * Choose S.ISS (initial seqno) or set from Init Cookie
974 * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init Cookie
975 * Continue with S.state == RESPOND
976 * * A Response packet will be generated in Step 11 *
978 * Generate Reset(No Connection) unless P.type == Reset
979 * Drop packet and return
981 * NOTE: the check for the packet types is done in
982 * dccp_rcv_state_process
984 if (sk->sk_state == DCCP_LISTEN) {
985 struct sock *nsk = dccp_v4_hnd_req(sk, skb);
991 if (dccp_child_process(sk, nsk, skb))
997 if (dccp_rcv_state_process(sk, skb, dh, skb->len))
1002 dccp_v4_ctl_send_reset(skb);
1008 EXPORT_SYMBOL_GPL(dccp_v4_do_rcv);
1010 int dccp_invalid_packet(struct sk_buff *skb)
1012 const struct dccp_hdr *dh;
1014 if (skb->pkt_type != PACKET_HOST)
1017 if (!pskb_may_pull(skb, sizeof(struct dccp_hdr))) {
1018 LIMIT_NETDEBUG(KERN_WARNING "DCCP: pskb_may_pull failed\n");
1024 /* If the packet type is not understood, drop packet and return */
1025 if (dh->dccph_type >= DCCP_PKT_INVALID) {
1026 LIMIT_NETDEBUG(KERN_WARNING "DCCP: invalid packet type\n");
1031 * If P.Data Offset is too small for packet type, or too large for
1032 * packet, drop packet and return
1034 if (dh->dccph_doff < dccp_hdr_len(skb) / sizeof(u32)) {
1035 LIMIT_NETDEBUG(KERN_WARNING "DCCP: P.Data Offset(%u) "
1041 if (!pskb_may_pull(skb, dh->dccph_doff * sizeof(u32))) {
1042 LIMIT_NETDEBUG(KERN_WARNING "DCCP: P.Data Offset(%u) "
1051 * If P.type is not Data, Ack, or DataAck and P.X == 0 (the packet
1052 * has short sequence numbers), drop packet and return
1054 if (dh->dccph_x == 0 &&
1055 dh->dccph_type != DCCP_PKT_DATA &&
1056 dh->dccph_type != DCCP_PKT_ACK &&
1057 dh->dccph_type != DCCP_PKT_DATAACK) {
1058 LIMIT_NETDEBUG(KERN_WARNING "DCCP: P.type (%s) not Data, Ack "
1059 "nor DataAck and P.X == 0\n",
1060 dccp_packet_name(dh->dccph_type));
1067 EXPORT_SYMBOL_GPL(dccp_invalid_packet);
1069 /* this is called when real data arrives */
1070 int dccp_v4_rcv(struct sk_buff *skb)
1072 const struct dccp_hdr *dh;
1076 /* Step 1: Check header basics: */
1078 if (dccp_invalid_packet(skb))
1081 /* If the header checksum is incorrect, drop packet and return */
1082 if (dccp_v4_verify_checksum(skb, skb->nh.iph->saddr,
1083 skb->nh.iph->daddr) < 0) {
1084 LIMIT_NETDEBUG(KERN_WARNING "%s: incorrect header checksum\n",
1091 DCCP_SKB_CB(skb)->dccpd_seq = dccp_hdr_seq(skb);
1092 DCCP_SKB_CB(skb)->dccpd_type = dh->dccph_type;
1094 dccp_pr_debug("%8.8s "
1095 "src=%u.%u.%u.%u@%-5d "
1096 "dst=%u.%u.%u.%u@%-5d seq=%llu",
1097 dccp_packet_name(dh->dccph_type),
1098 NIPQUAD(skb->nh.iph->saddr), ntohs(dh->dccph_sport),
1099 NIPQUAD(skb->nh.iph->daddr), ntohs(dh->dccph_dport),
1100 (unsigned long long) DCCP_SKB_CB(skb)->dccpd_seq);
1102 if (dccp_packet_without_ack(skb)) {
1103 DCCP_SKB_CB(skb)->dccpd_ack_seq = DCCP_PKT_WITHOUT_ACK_SEQ;
1104 dccp_pr_debug_cat("\n");
1106 DCCP_SKB_CB(skb)->dccpd_ack_seq = dccp_hdr_ack_seq(skb);
1107 dccp_pr_debug_cat(", ack=%llu\n",
1108 (unsigned long long)
1109 DCCP_SKB_CB(skb)->dccpd_ack_seq);
1113 * Look up flow ID in table and get corresponding socket */
1114 sk = __inet_lookup(&dccp_hashinfo,
1115 skb->nh.iph->saddr, dh->dccph_sport,
1116 skb->nh.iph->daddr, ntohs(dh->dccph_dport),
1122 * Generate Reset(No Connection) unless P.type == Reset
1123 * Drop packet and return
1126 dccp_pr_debug("failed to look up flow ID in table and "
1127 "get corresponding socket\n");
1128 goto no_dccp_socket;
1133 * ... or S.state == TIMEWAIT,
1134 * Generate Reset(No Connection) unless P.type == Reset
1135 * Drop packet and return
1138 if (sk->sk_state == DCCP_TIME_WAIT) {
1139 dccp_pr_debug("sk->sk_state == DCCP_TIME_WAIT: "
1144 if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb)) {
1145 dccp_pr_debug("xfrm4_policy_check failed\n");
1146 goto discard_and_relse;
1149 if (sk_filter(sk, skb, 0)) {
1150 dccp_pr_debug("sk_filter failed\n");
1151 goto discard_and_relse;
1158 if (!sock_owned_by_user(sk))
1159 rc = dccp_v4_do_rcv(sk, skb);
1161 sk_add_backlog(sk, skb);
1168 if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
1172 * Generate Reset(No Connection) unless P.type == Reset
1173 * Drop packet and return
1175 if (dh->dccph_type != DCCP_PKT_RESET) {
1176 DCCP_SKB_CB(skb)->dccpd_reset_code =
1177 DCCP_RESET_CODE_NO_CONNECTION;
1178 dccp_v4_ctl_send_reset(skb);
1182 /* Discard frame. */
1191 inet_twsk_put((struct inet_timewait_sock *)sk);
1192 goto no_dccp_socket;
1195 struct inet_connection_sock_af_ops dccp_ipv4_af_ops = {
1196 .queue_xmit = ip_queue_xmit,
1197 .send_check = dccp_v4_send_check,
1198 .rebuild_header = inet_sk_rebuild_header,
1199 .conn_request = dccp_v4_conn_request,
1200 .syn_recv_sock = dccp_v4_request_recv_sock,
1201 .net_header_len = sizeof(struct iphdr),
1202 .setsockopt = ip_setsockopt,
1203 .getsockopt = ip_getsockopt,
1204 .addr2sockaddr = inet_csk_addr2sockaddr,
1205 .sockaddr_len = sizeof(struct sockaddr_in),
1208 int dccp_v4_init_sock(struct sock *sk)
1210 struct dccp_sock *dp = dccp_sk(sk);
1211 static int dccp_ctl_socket_init = 1;
1213 dccp_options_init(&dp->dccps_options);
1214 do_gettimeofday(&dp->dccps_epoch);
1216 if (dp->dccps_options.dccpo_send_ack_vector) {
1217 dp->dccps_hc_rx_ackvec = dccp_ackvec_alloc(DCCP_MAX_ACKVEC_LEN,
1219 if (dp->dccps_hc_rx_ackvec == NULL)
1224 * FIXME: We're hardcoding the CCID, and doing this at this point makes
1225 * the listening (master) sock get CCID control blocks, which is not
1226 * necessary, but for now, to not mess with the test userspace apps,
1227 * lets leave it here, later the real solution is to do this in a
1228 * setsockopt(CCIDs-I-want/accept). -acme
1230 if (likely(!dccp_ctl_socket_init)) {
1231 dp->dccps_hc_rx_ccid = ccid_init(dp->dccps_options.dccpo_rx_ccid,
1233 dp->dccps_hc_tx_ccid = ccid_init(dp->dccps_options.dccpo_tx_ccid,
1235 if (dp->dccps_hc_rx_ccid == NULL ||
1236 dp->dccps_hc_tx_ccid == NULL) {
1237 ccid_exit(dp->dccps_hc_rx_ccid, sk);
1238 ccid_exit(dp->dccps_hc_tx_ccid, sk);
1239 if (dp->dccps_options.dccpo_send_ack_vector) {
1240 dccp_ackvec_free(dp->dccps_hc_rx_ackvec);
1241 dp->dccps_hc_rx_ackvec = NULL;
1243 dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL;
1247 dccp_ctl_socket_init = 0;
1249 dccp_init_xmit_timers(sk);
1250 inet_csk(sk)->icsk_rto = DCCP_TIMEOUT_INIT;
1251 sk->sk_state = DCCP_CLOSED;
1252 sk->sk_write_space = dccp_write_space;
1253 inet_csk(sk)->icsk_af_ops = &dccp_ipv4_af_ops;
1254 dp->dccps_mss_cache = 536;
1255 dp->dccps_role = DCCP_ROLE_UNDEFINED;
1256 dp->dccps_service = DCCP_SERVICE_INVALID_VALUE;
1261 EXPORT_SYMBOL_GPL(dccp_v4_init_sock);
1263 int dccp_v4_destroy_sock(struct sock *sk)
1265 struct dccp_sock *dp = dccp_sk(sk);
1268 * DCCP doesn't use sk_write_queue, just sk_send_head
1269 * for retransmissions
1271 if (sk->sk_send_head != NULL) {
1272 kfree_skb(sk->sk_send_head);
1273 sk->sk_send_head = NULL;
1276 /* Clean up a referenced DCCP bind bucket. */
1277 if (inet_csk(sk)->icsk_bind_hash != NULL)
1278 inet_put_port(&dccp_hashinfo, sk);
1280 kfree(dp->dccps_service_list);
1281 dp->dccps_service_list = NULL;
1283 ccid_hc_rx_exit(dp->dccps_hc_rx_ccid, sk);
1284 ccid_hc_tx_exit(dp->dccps_hc_tx_ccid, sk);
1285 if (dp->dccps_options.dccpo_send_ack_vector) {
1286 dccp_ackvec_free(dp->dccps_hc_rx_ackvec);
1287 dp->dccps_hc_rx_ackvec = NULL;
1289 ccid_exit(dp->dccps_hc_rx_ccid, sk);
1290 ccid_exit(dp->dccps_hc_tx_ccid, sk);
1291 dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL;
1296 EXPORT_SYMBOL_GPL(dccp_v4_destroy_sock);
1298 static void dccp_v4_reqsk_destructor(struct request_sock *req)
1300 kfree(inet_rsk(req)->opt);
1303 static struct request_sock_ops dccp_request_sock_ops = {
1305 .obj_size = sizeof(struct dccp_request_sock),
1306 .rtx_syn_ack = dccp_v4_send_response,
1307 .send_ack = dccp_v4_reqsk_send_ack,
1308 .destructor = dccp_v4_reqsk_destructor,
1309 .send_reset = dccp_v4_ctl_send_reset,
1312 struct proto dccp_prot = {
1314 .owner = THIS_MODULE,
1315 .close = dccp_close,
1316 .connect = dccp_v4_connect,
1317 .disconnect = dccp_disconnect,
1318 .ioctl = dccp_ioctl,
1319 .init = dccp_v4_init_sock,
1320 .setsockopt = dccp_setsockopt,
1321 .getsockopt = dccp_getsockopt,
1322 .sendmsg = dccp_sendmsg,
1323 .recvmsg = dccp_recvmsg,
1324 .backlog_rcv = dccp_v4_do_rcv,
1325 .hash = dccp_v4_hash,
1326 .unhash = dccp_unhash,
1327 .accept = inet_csk_accept,
1328 .get_port = dccp_v4_get_port,
1329 .shutdown = dccp_shutdown,
1330 .destroy = dccp_v4_destroy_sock,
1331 .orphan_count = &dccp_orphan_count,
1332 .max_header = MAX_DCCP_HEADER,
1333 .obj_size = sizeof(struct dccp_sock),
1334 .rsk_prot = &dccp_request_sock_ops,
1335 .twsk_obj_size = sizeof(struct inet_timewait_sock),