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/timewait_sock.h>
24 #include <net/tcp_states.h>
31 struct inet_hashinfo __cacheline_aligned dccp_hashinfo = {
32 .lhash_lock = RW_LOCK_UNLOCKED,
33 .lhash_users = ATOMIC_INIT(0),
34 .lhash_wait = __WAIT_QUEUE_HEAD_INITIALIZER(dccp_hashinfo.lhash_wait),
37 EXPORT_SYMBOL_GPL(dccp_hashinfo);
39 static int dccp_v4_get_port(struct sock *sk, const unsigned short snum)
41 return inet_csk_get_port(&dccp_hashinfo, sk, snum,
42 inet_csk_bind_conflict);
45 static void dccp_v4_hash(struct sock *sk)
47 inet_hash(&dccp_hashinfo, sk);
50 void dccp_unhash(struct sock *sk)
52 inet_unhash(&dccp_hashinfo, sk);
55 EXPORT_SYMBOL_GPL(dccp_unhash);
57 int dccp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
59 struct inet_sock *inet = inet_sk(sk);
60 struct dccp_sock *dp = dccp_sk(sk);
61 const struct sockaddr_in *usin = (struct sockaddr_in *)uaddr;
67 dp->dccps_role = DCCP_ROLE_CLIENT;
69 if (dccp_service_not_initialized(sk))
72 if (addr_len < sizeof(struct sockaddr_in))
75 if (usin->sin_family != AF_INET)
78 nexthop = daddr = usin->sin_addr.s_addr;
79 if (inet->opt != NULL && inet->opt->srr) {
82 nexthop = inet->opt->faddr;
85 tmp = ip_route_connect(&rt, nexthop, inet->saddr,
86 RT_CONN_FLAGS(sk), sk->sk_bound_dev_if,
88 inet->sport, usin->sin_port, sk);
92 if (rt->rt_flags & (RTCF_MULTICAST | RTCF_BROADCAST)) {
97 if (inet->opt == NULL || !inet->opt->srr)
100 if (inet->saddr == 0)
101 inet->saddr = rt->rt_src;
102 inet->rcv_saddr = inet->saddr;
104 inet->dport = usin->sin_port;
107 inet_csk(sk)->icsk_ext_hdr_len = 0;
108 if (inet->opt != NULL)
109 inet_csk(sk)->icsk_ext_hdr_len = inet->opt->optlen;
111 * Socket identity is still unknown (sport may be zero).
112 * However we set state to DCCP_REQUESTING and not releasing socket
113 * lock select source port, enter ourselves into the hash tables and
114 * complete initialization after this.
116 dccp_set_state(sk, DCCP_REQUESTING);
117 err = inet_hash_connect(&dccp_death_row, sk);
121 err = ip_route_newports(&rt, inet->sport, inet->dport, sk);
125 /* OK, now commit destination to socket. */
126 sk_setup_caps(sk, &rt->u.dst);
129 dp->dccps_iss = secure_dccp_sequence_number(inet->saddr,
133 dccp_update_gss(sk, dp->dccps_iss);
135 inet->id = dp->dccps_iss ^ jiffies;
137 err = dccp_connect(sk);
145 * This unhashes the socket and releases the local port, if necessary.
147 dccp_set_state(sk, DCCP_CLOSED);
149 sk->sk_route_caps = 0;
154 EXPORT_SYMBOL_GPL(dccp_v4_connect);
157 * This routine does path mtu discovery as defined in RFC1191.
159 static inline void dccp_do_pmtu_discovery(struct sock *sk,
160 const struct iphdr *iph,
163 struct dst_entry *dst;
164 const struct inet_sock *inet = inet_sk(sk);
165 const struct dccp_sock *dp = dccp_sk(sk);
167 /* We are not interested in DCCP_LISTEN and request_socks (RESPONSEs
168 * send out by Linux are always < 576bytes so they should go through
171 if (sk->sk_state == DCCP_LISTEN)
174 /* We don't check in the destentry if pmtu discovery is forbidden
175 * on this route. We just assume that no packet_to_big packets
176 * are send back when pmtu discovery is not active.
177 * There is a small race when the user changes this flag in the
178 * route, but I think that's acceptable.
180 if ((dst = __sk_dst_check(sk, 0)) == NULL)
183 dst->ops->update_pmtu(dst, mtu);
185 /* Something is about to be wrong... Remember soft error
186 * for the case, if this connection will not able to recover.
188 if (mtu < dst_mtu(dst) && ip_dont_fragment(sk, dst))
189 sk->sk_err_soft = EMSGSIZE;
193 if (inet->pmtudisc != IP_PMTUDISC_DONT &&
194 inet_csk(sk)->icsk_pmtu_cookie > mtu) {
195 dccp_sync_mss(sk, mtu);
198 * From: draft-ietf-dccp-spec-11.txt
200 * DCCP-Sync packets are the best choice for upward
201 * probing, since DCCP-Sync probes do not risk application
204 dccp_send_sync(sk, dp->dccps_gsr, DCCP_PKT_SYNC);
205 } /* else let the usual retransmit timer handle it */
208 static void dccp_v4_ctl_send_ack(struct sk_buff *rxskb)
211 struct dccp_hdr *rxdh = dccp_hdr(rxskb), *dh;
212 const int dccp_hdr_ack_len = sizeof(struct dccp_hdr) +
213 sizeof(struct dccp_hdr_ext) +
214 sizeof(struct dccp_hdr_ack_bits);
217 if (((struct rtable *)rxskb->dst)->rt_type != RTN_LOCAL)
220 skb = alloc_skb(MAX_DCCP_HEADER + 15, GFP_ATOMIC);
224 /* Reserve space for headers. */
225 skb_reserve(skb, MAX_DCCP_HEADER);
227 skb->dst = dst_clone(rxskb->dst);
229 skb->h.raw = skb_push(skb, dccp_hdr_ack_len);
231 memset(dh, 0, dccp_hdr_ack_len);
233 /* Build DCCP header and checksum it. */
234 dh->dccph_type = DCCP_PKT_ACK;
235 dh->dccph_sport = rxdh->dccph_dport;
236 dh->dccph_dport = rxdh->dccph_sport;
237 dh->dccph_doff = dccp_hdr_ack_len / 4;
240 dccp_hdr_set_seq(dh, DCCP_SKB_CB(rxskb)->dccpd_ack_seq);
241 dccp_hdr_set_ack(dccp_hdr_ack_bits(skb),
242 DCCP_SKB_CB(rxskb)->dccpd_seq);
244 bh_lock_sock(dccp_ctl_socket->sk);
245 err = ip_build_and_send_pkt(skb, dccp_ctl_socket->sk,
246 rxskb->nh.iph->daddr,
247 rxskb->nh.iph->saddr, NULL);
248 bh_unlock_sock(dccp_ctl_socket->sk);
250 if (err == NET_XMIT_CN || err == 0) {
251 DCCP_INC_STATS_BH(DCCP_MIB_OUTSEGS);
252 DCCP_INC_STATS_BH(DCCP_MIB_OUTRSTS);
256 static void dccp_v4_reqsk_send_ack(struct sk_buff *skb,
257 struct request_sock *req)
259 dccp_v4_ctl_send_ack(skb);
262 static int dccp_v4_send_response(struct sock *sk, struct request_sock *req,
263 struct dst_entry *dst)
268 /* First, grab a route. */
270 if (dst == NULL && (dst = inet_csk_route_req(sk, req)) == NULL)
273 skb = dccp_make_response(sk, dst, req);
275 const struct inet_request_sock *ireq = inet_rsk(req);
277 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
278 err = ip_build_and_send_pkt(skb, sk, ireq->loc_addr,
281 if (err == NET_XMIT_CN)
291 * This routine is called by the ICMP module when it gets some sort of error
292 * condition. If err < 0 then the socket should be closed and the error
293 * returned to the user. If err > 0 it's just the icmp type << 8 | icmp code.
294 * After adjustment header points to the first 8 bytes of the tcp header. We
295 * need to find the appropriate port.
297 * The locking strategy used here is very "optimistic". When someone else
298 * accesses the socket the ICMP is just dropped and for some paths there is no
299 * check at all. A more general error queue to queue errors for later handling
300 * is probably better.
302 void dccp_v4_err(struct sk_buff *skb, u32 info)
304 const struct iphdr *iph = (struct iphdr *)skb->data;
305 const struct dccp_hdr *dh = (struct dccp_hdr *)(skb->data +
307 struct dccp_sock *dp;
308 struct inet_sock *inet;
309 const int type = skb->h.icmph->type;
310 const int code = skb->h.icmph->code;
315 if (skb->len < (iph->ihl << 2) + 8) {
316 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
320 sk = inet_lookup(&dccp_hashinfo, iph->daddr, dh->dccph_dport,
321 iph->saddr, dh->dccph_sport, inet_iif(skb));
323 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
327 if (sk->sk_state == DCCP_TIME_WAIT) {
328 inet_twsk_put((struct inet_timewait_sock *)sk);
333 /* If too many ICMPs get dropped on busy
334 * servers this needs to be solved differently.
336 if (sock_owned_by_user(sk))
337 NET_INC_STATS_BH(LINUX_MIB_LOCKDROPPEDICMPS);
339 if (sk->sk_state == DCCP_CLOSED)
343 seq = dccp_hdr_seq(skb);
344 if (sk->sk_state != DCCP_LISTEN &&
345 !between48(seq, dp->dccps_swl, dp->dccps_swh)) {
346 NET_INC_STATS(LINUX_MIB_OUTOFWINDOWICMPS);
351 case ICMP_SOURCE_QUENCH:
352 /* Just silently ignore these. */
354 case ICMP_PARAMETERPROB:
357 case ICMP_DEST_UNREACH:
358 if (code > NR_ICMP_UNREACH)
361 if (code == ICMP_FRAG_NEEDED) { /* PMTU discovery (RFC1191) */
362 if (!sock_owned_by_user(sk))
363 dccp_do_pmtu_discovery(sk, iph, info);
367 err = icmp_err_convert[code].errno;
369 case ICMP_TIME_EXCEEDED:
376 switch (sk->sk_state) {
377 struct request_sock *req , **prev;
379 if (sock_owned_by_user(sk))
381 req = inet_csk_search_req(sk, &prev, dh->dccph_dport,
382 iph->daddr, iph->saddr);
387 * ICMPs are not backlogged, hence we cannot get an established
392 if (seq != dccp_rsk(req)->dreq_iss) {
393 NET_INC_STATS_BH(LINUX_MIB_OUTOFWINDOWICMPS);
397 * Still in RESPOND, just remove it silently.
398 * There is no good way to pass the error to the newly
399 * created socket, and POSIX does not want network
400 * errors returned from accept().
402 inet_csk_reqsk_queue_drop(sk, req, prev);
405 case DCCP_REQUESTING:
407 if (!sock_owned_by_user(sk)) {
408 DCCP_INC_STATS_BH(DCCP_MIB_ATTEMPTFAILS);
411 sk->sk_error_report(sk);
415 sk->sk_err_soft = err;
419 /* If we've already connected we will keep trying
420 * until we time out, or the user gives up.
422 * rfc1122 4.2.3.9 allows to consider as hard errors
423 * only PROTO_UNREACH and PORT_UNREACH (well, FRAG_FAILED too,
424 * but it is obsoleted by pmtu discovery).
426 * Note, that in modern internet, where routing is unreliable
427 * and in each dark corner broken firewalls sit, sending random
428 * errors ordered by their masters even this two messages finally lose
429 * their original sense (even Linux sends invalid PORT_UNREACHs)
431 * Now we are in compliance with RFCs.
436 if (!sock_owned_by_user(sk) && inet->recverr) {
438 sk->sk_error_report(sk);
439 } else /* Only an error on timeout */
440 sk->sk_err_soft = err;
446 /* This routine computes an IPv4 DCCP checksum. */
447 void dccp_v4_send_check(struct sock *sk, int len, struct sk_buff *skb)
449 const struct inet_sock *inet = inet_sk(sk);
450 struct dccp_hdr *dh = dccp_hdr(skb);
452 dh->dccph_checksum = dccp_v4_checksum(skb, inet->saddr, inet->daddr);
455 EXPORT_SYMBOL_GPL(dccp_v4_send_check);
457 int dccp_v4_send_reset(struct sock *sk, enum dccp_reset_codes code)
461 * FIXME: what if rebuild_header fails?
462 * Should we be doing a rebuild_header here?
464 int err = inet_sk_rebuild_header(sk);
469 skb = dccp_make_reset(sk, sk->sk_dst_cache, code);
471 const struct inet_sock *inet = inet_sk(sk);
473 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
474 err = ip_build_and_send_pkt(skb, sk,
475 inet->saddr, inet->daddr, NULL);
476 if (err == NET_XMIT_CN)
483 static inline u64 dccp_v4_init_sequence(const struct sock *sk,
484 const struct sk_buff *skb)
486 return secure_dccp_sequence_number(skb->nh.iph->daddr,
488 dccp_hdr(skb)->dccph_dport,
489 dccp_hdr(skb)->dccph_sport);
492 int dccp_v4_conn_request(struct sock *sk, struct sk_buff *skb)
494 struct inet_request_sock *ireq;
496 struct request_sock *req;
497 struct dccp_request_sock *dreq;
498 const __u32 saddr = skb->nh.iph->saddr;
499 const __u32 daddr = skb->nh.iph->daddr;
500 const __u32 service = dccp_hdr_request(skb)->dccph_req_service;
501 struct dccp_skb_cb *dcb = DCCP_SKB_CB(skb);
502 __u8 reset_code = DCCP_RESET_CODE_TOO_BUSY;
504 /* Never answer to DCCP_PKT_REQUESTs send to broadcast or multicast */
505 if (((struct rtable *)skb->dst)->rt_flags &
506 (RTCF_BROADCAST | RTCF_MULTICAST)) {
507 reset_code = DCCP_RESET_CODE_NO_CONNECTION;
511 if (dccp_bad_service_code(sk, service)) {
512 reset_code = DCCP_RESET_CODE_BAD_SERVICE_CODE;
516 * TW buckets are converted to open requests without
517 * limitations, they conserve resources and peer is
518 * evidently real one.
520 if (inet_csk_reqsk_queue_is_full(sk))
524 * Accept backlog is full. If we have already queued enough
525 * of warm entries in syn queue, drop request. It is better than
526 * clogging syn queue with openreqs with exponentially increasing
529 if (sk_acceptq_is_full(sk) && inet_csk_reqsk_queue_young(sk) > 1)
532 req = reqsk_alloc(sk->sk_prot->rsk_prot);
536 /* FIXME: process options */
538 dccp_openreq_init(req, &dp, skb);
540 ireq = inet_rsk(req);
541 ireq->loc_addr = daddr;
542 ireq->rmt_addr = saddr;
543 req->rcv_wnd = 100; /* Fake, option parsing will get the
548 * Step 3: Process LISTEN state
550 * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init Cookie
552 * In fact we defer setting S.GSR, S.SWL, S.SWH to
553 * dccp_create_openreq_child.
555 dreq = dccp_rsk(req);
556 dreq->dreq_isr = dcb->dccpd_seq;
557 dreq->dreq_iss = dccp_v4_init_sequence(sk, skb);
558 dreq->dreq_service = service;
560 if (dccp_v4_send_response(sk, req, NULL))
563 inet_csk_reqsk_queue_hash_add(sk, req, DCCP_TIMEOUT_INIT);
569 DCCP_INC_STATS_BH(DCCP_MIB_ATTEMPTFAILS);
570 dcb->dccpd_reset_code = reset_code;
574 EXPORT_SYMBOL_GPL(dccp_v4_conn_request);
577 * The three way handshake has completed - we got a valid ACK or DATAACK -
578 * now create the new socket.
580 * This is the equivalent of TCP's tcp_v4_syn_recv_sock
582 struct sock *dccp_v4_request_recv_sock(struct sock *sk, struct sk_buff *skb,
583 struct request_sock *req,
584 struct dst_entry *dst)
586 struct inet_request_sock *ireq;
587 struct inet_sock *newinet;
588 struct dccp_sock *newdp;
591 if (sk_acceptq_is_full(sk))
594 if (dst == NULL && (dst = inet_csk_route_req(sk, req)) == NULL)
597 newsk = dccp_create_openreq_child(sk, req, skb);
601 sk_setup_caps(newsk, dst);
603 newdp = dccp_sk(newsk);
604 newinet = inet_sk(newsk);
605 ireq = inet_rsk(req);
606 newinet->daddr = ireq->rmt_addr;
607 newinet->rcv_saddr = ireq->loc_addr;
608 newinet->saddr = ireq->loc_addr;
609 newinet->opt = ireq->opt;
611 newinet->mc_index = inet_iif(skb);
612 newinet->mc_ttl = skb->nh.iph->ttl;
613 newinet->id = jiffies;
615 dccp_sync_mss(newsk, dst_mtu(dst));
617 __inet_hash(&dccp_hashinfo, newsk, 0);
618 __inet_inherit_port(&dccp_hashinfo, sk, newsk);
623 NET_INC_STATS_BH(LINUX_MIB_LISTENOVERFLOWS);
625 NET_INC_STATS_BH(LINUX_MIB_LISTENDROPS);
630 EXPORT_SYMBOL_GPL(dccp_v4_request_recv_sock);
632 static struct sock *dccp_v4_hnd_req(struct sock *sk, struct sk_buff *skb)
634 const struct dccp_hdr *dh = dccp_hdr(skb);
635 const struct iphdr *iph = skb->nh.iph;
637 struct request_sock **prev;
638 /* Find possible connection requests. */
639 struct request_sock *req = inet_csk_search_req(sk, &prev,
641 iph->saddr, iph->daddr);
643 return dccp_check_req(sk, skb, req, prev);
645 nsk = __inet_lookup_established(&dccp_hashinfo,
646 iph->saddr, dh->dccph_sport,
647 iph->daddr, ntohs(dh->dccph_dport),
650 if (nsk->sk_state != DCCP_TIME_WAIT) {
654 inet_twsk_put((struct inet_timewait_sock *)nsk);
661 int dccp_v4_checksum(const struct sk_buff *skb, const u32 saddr,
664 const struct dccp_hdr* dh = dccp_hdr(skb);
668 if (dh->dccph_cscov == 0)
669 checksum_len = skb->len;
671 checksum_len = (dh->dccph_cscov + dh->dccph_x) * sizeof(u32);
672 checksum_len = checksum_len < skb->len ? checksum_len :
676 tmp = csum_partial((unsigned char *)dh, checksum_len, 0);
677 return csum_tcpudp_magic(saddr, daddr, checksum_len,
681 static int dccp_v4_verify_checksum(struct sk_buff *skb,
682 const u32 saddr, const u32 daddr)
684 struct dccp_hdr *dh = dccp_hdr(skb);
688 if (dh->dccph_cscov == 0)
689 checksum_len = skb->len;
691 checksum_len = (dh->dccph_cscov + dh->dccph_x) * sizeof(u32);
692 checksum_len = checksum_len < skb->len ? checksum_len :
695 tmp = csum_partial((unsigned char *)dh, checksum_len, 0);
696 return csum_tcpudp_magic(saddr, daddr, checksum_len,
697 IPPROTO_DCCP, tmp) == 0 ? 0 : -1;
700 static struct dst_entry* dccp_v4_route_skb(struct sock *sk,
704 struct flowi fl = { .oif = ((struct rtable *)skb->dst)->rt_iif,
706 { .daddr = skb->nh.iph->saddr,
707 .saddr = skb->nh.iph->daddr,
708 .tos = RT_CONN_FLAGS(sk) } },
709 .proto = sk->sk_protocol,
711 { .sport = dccp_hdr(skb)->dccph_dport,
712 .dport = dccp_hdr(skb)->dccph_sport }
716 if (ip_route_output_flow(&rt, &fl, sk, 0)) {
717 IP_INC_STATS_BH(IPSTATS_MIB_OUTNOROUTES);
724 static void dccp_v4_ctl_send_reset(struct sk_buff *rxskb)
727 struct dccp_hdr *rxdh = dccp_hdr(rxskb), *dh;
728 const int dccp_hdr_reset_len = sizeof(struct dccp_hdr) +
729 sizeof(struct dccp_hdr_ext) +
730 sizeof(struct dccp_hdr_reset);
732 struct dst_entry *dst;
735 /* Never send a reset in response to a reset. */
736 if (rxdh->dccph_type == DCCP_PKT_RESET)
739 if (((struct rtable *)rxskb->dst)->rt_type != RTN_LOCAL)
742 dst = dccp_v4_route_skb(dccp_ctl_socket->sk, rxskb);
746 skb = alloc_skb(MAX_DCCP_HEADER + 15, GFP_ATOMIC);
750 /* Reserve space for headers. */
751 skb_reserve(skb, MAX_DCCP_HEADER);
752 skb->dst = dst_clone(dst);
754 skb->h.raw = skb_push(skb, dccp_hdr_reset_len);
756 memset(dh, 0, dccp_hdr_reset_len);
758 /* Build DCCP header and checksum it. */
759 dh->dccph_type = DCCP_PKT_RESET;
760 dh->dccph_sport = rxdh->dccph_dport;
761 dh->dccph_dport = rxdh->dccph_sport;
762 dh->dccph_doff = dccp_hdr_reset_len / 4;
764 dccp_hdr_reset(skb)->dccph_reset_code =
765 DCCP_SKB_CB(rxskb)->dccpd_reset_code;
767 /* See "8.3.1. Abnormal Termination" in draft-ietf-dccp-spec-11 */
769 if (DCCP_SKB_CB(rxskb)->dccpd_ack_seq != DCCP_PKT_WITHOUT_ACK_SEQ)
770 dccp_set_seqno(&seqno, DCCP_SKB_CB(rxskb)->dccpd_ack_seq + 1);
772 dccp_hdr_set_seq(dh, seqno);
773 dccp_hdr_set_ack(dccp_hdr_ack_bits(skb),
774 DCCP_SKB_CB(rxskb)->dccpd_seq);
776 dh->dccph_checksum = dccp_v4_checksum(skb, rxskb->nh.iph->saddr,
777 rxskb->nh.iph->daddr);
779 bh_lock_sock(dccp_ctl_socket->sk);
780 err = ip_build_and_send_pkt(skb, dccp_ctl_socket->sk,
781 rxskb->nh.iph->daddr,
782 rxskb->nh.iph->saddr, NULL);
783 bh_unlock_sock(dccp_ctl_socket->sk);
785 if (err == NET_XMIT_CN || err == 0) {
786 DCCP_INC_STATS_BH(DCCP_MIB_OUTSEGS);
787 DCCP_INC_STATS_BH(DCCP_MIB_OUTRSTS);
793 int dccp_v4_do_rcv(struct sock *sk, struct sk_buff *skb)
795 struct dccp_hdr *dh = dccp_hdr(skb);
797 if (sk->sk_state == DCCP_OPEN) { /* Fast path */
798 if (dccp_rcv_established(sk, skb, dh, skb->len))
804 * Step 3: Process LISTEN state
805 * If S.state == LISTEN,
806 * If P.type == Request or P contains a valid Init Cookie
808 * * Must scan the packet's options to check for an Init
809 * Cookie. Only the Init Cookie is processed here,
810 * however; other options are processed in Step 8. This
811 * scan need only be performed if the endpoint uses Init
813 * * Generate a new socket and switch to that socket *
814 * Set S := new socket for this port pair
816 * Choose S.ISS (initial seqno) or set from Init Cookie
817 * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init Cookie
818 * Continue with S.state == RESPOND
819 * * A Response packet will be generated in Step 11 *
821 * Generate Reset(No Connection) unless P.type == Reset
822 * Drop packet and return
824 * NOTE: the check for the packet types is done in
825 * dccp_rcv_state_process
827 if (sk->sk_state == DCCP_LISTEN) {
828 struct sock *nsk = dccp_v4_hnd_req(sk, skb);
834 if (dccp_child_process(sk, nsk, skb))
840 if (dccp_rcv_state_process(sk, skb, dh, skb->len))
845 dccp_v4_ctl_send_reset(skb);
851 EXPORT_SYMBOL_GPL(dccp_v4_do_rcv);
853 int dccp_invalid_packet(struct sk_buff *skb)
855 const struct dccp_hdr *dh;
857 if (skb->pkt_type != PACKET_HOST)
860 if (!pskb_may_pull(skb, sizeof(struct dccp_hdr))) {
861 LIMIT_NETDEBUG(KERN_WARNING "DCCP: pskb_may_pull failed\n");
867 /* If the packet type is not understood, drop packet and return */
868 if (dh->dccph_type >= DCCP_PKT_INVALID) {
869 LIMIT_NETDEBUG(KERN_WARNING "DCCP: invalid packet type\n");
874 * If P.Data Offset is too small for packet type, or too large for
875 * packet, drop packet and return
877 if (dh->dccph_doff < dccp_hdr_len(skb) / sizeof(u32)) {
878 LIMIT_NETDEBUG(KERN_WARNING "DCCP: P.Data Offset(%u) "
884 if (!pskb_may_pull(skb, dh->dccph_doff * sizeof(u32))) {
885 LIMIT_NETDEBUG(KERN_WARNING "DCCP: P.Data Offset(%u) "
894 * If P.type is not Data, Ack, or DataAck and P.X == 0 (the packet
895 * has short sequence numbers), drop packet and return
897 if (dh->dccph_x == 0 &&
898 dh->dccph_type != DCCP_PKT_DATA &&
899 dh->dccph_type != DCCP_PKT_ACK &&
900 dh->dccph_type != DCCP_PKT_DATAACK) {
901 LIMIT_NETDEBUG(KERN_WARNING "DCCP: P.type (%s) not Data, Ack "
902 "nor DataAck and P.X == 0\n",
903 dccp_packet_name(dh->dccph_type));
910 EXPORT_SYMBOL_GPL(dccp_invalid_packet);
912 /* this is called when real data arrives */
913 int dccp_v4_rcv(struct sk_buff *skb)
915 const struct dccp_hdr *dh;
919 /* Step 1: Check header basics: */
921 if (dccp_invalid_packet(skb))
924 /* If the header checksum is incorrect, drop packet and return */
925 if (dccp_v4_verify_checksum(skb, skb->nh.iph->saddr,
926 skb->nh.iph->daddr) < 0) {
927 LIMIT_NETDEBUG(KERN_WARNING "%s: incorrect header checksum\n",
934 DCCP_SKB_CB(skb)->dccpd_seq = dccp_hdr_seq(skb);
935 DCCP_SKB_CB(skb)->dccpd_type = dh->dccph_type;
937 dccp_pr_debug("%8.8s "
938 "src=%u.%u.%u.%u@%-5d "
939 "dst=%u.%u.%u.%u@%-5d seq=%llu",
940 dccp_packet_name(dh->dccph_type),
941 NIPQUAD(skb->nh.iph->saddr), ntohs(dh->dccph_sport),
942 NIPQUAD(skb->nh.iph->daddr), ntohs(dh->dccph_dport),
943 (unsigned long long) DCCP_SKB_CB(skb)->dccpd_seq);
945 if (dccp_packet_without_ack(skb)) {
946 DCCP_SKB_CB(skb)->dccpd_ack_seq = DCCP_PKT_WITHOUT_ACK_SEQ;
947 dccp_pr_debug_cat("\n");
949 DCCP_SKB_CB(skb)->dccpd_ack_seq = dccp_hdr_ack_seq(skb);
950 dccp_pr_debug_cat(", ack=%llu\n",
952 DCCP_SKB_CB(skb)->dccpd_ack_seq);
956 * Look up flow ID in table and get corresponding socket */
957 sk = __inet_lookup(&dccp_hashinfo,
958 skb->nh.iph->saddr, dh->dccph_sport,
959 skb->nh.iph->daddr, ntohs(dh->dccph_dport),
965 * Generate Reset(No Connection) unless P.type == Reset
966 * Drop packet and return
969 dccp_pr_debug("failed to look up flow ID in table and "
970 "get corresponding socket\n");
976 * ... or S.state == TIMEWAIT,
977 * Generate Reset(No Connection) unless P.type == Reset
978 * Drop packet and return
981 if (sk->sk_state == DCCP_TIME_WAIT) {
982 dccp_pr_debug("sk->sk_state == DCCP_TIME_WAIT: "
987 if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb)) {
988 dccp_pr_debug("xfrm4_policy_check failed\n");
989 goto discard_and_relse;
992 if (sk_filter(sk, skb, 0)) {
993 dccp_pr_debug("sk_filter failed\n");
994 goto discard_and_relse;
1001 if (!sock_owned_by_user(sk))
1002 rc = dccp_v4_do_rcv(sk, skb);
1004 sk_add_backlog(sk, skb);
1011 if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
1015 * Generate Reset(No Connection) unless P.type == Reset
1016 * Drop packet and return
1018 if (dh->dccph_type != DCCP_PKT_RESET) {
1019 DCCP_SKB_CB(skb)->dccpd_reset_code =
1020 DCCP_RESET_CODE_NO_CONNECTION;
1021 dccp_v4_ctl_send_reset(skb);
1025 /* Discard frame. */
1034 inet_twsk_put((struct inet_timewait_sock *)sk);
1035 goto no_dccp_socket;
1038 struct inet_connection_sock_af_ops dccp_ipv4_af_ops = {
1039 .queue_xmit = ip_queue_xmit,
1040 .send_check = dccp_v4_send_check,
1041 .rebuild_header = inet_sk_rebuild_header,
1042 .conn_request = dccp_v4_conn_request,
1043 .syn_recv_sock = dccp_v4_request_recv_sock,
1044 .net_header_len = sizeof(struct iphdr),
1045 .setsockopt = ip_setsockopt,
1046 .getsockopt = ip_getsockopt,
1047 .addr2sockaddr = inet_csk_addr2sockaddr,
1048 .sockaddr_len = sizeof(struct sockaddr_in),
1051 int dccp_v4_init_sock(struct sock *sk)
1053 struct dccp_sock *dp = dccp_sk(sk);
1054 struct inet_connection_sock *icsk = inet_csk(sk);
1055 static int dccp_ctl_socket_init = 1;
1057 dccp_options_init(&dp->dccps_options);
1058 do_gettimeofday(&dp->dccps_epoch);
1060 if (dp->dccps_options.dccpo_send_ack_vector) {
1061 dp->dccps_hc_rx_ackvec = dccp_ackvec_alloc(DCCP_MAX_ACKVEC_LEN,
1063 if (dp->dccps_hc_rx_ackvec == NULL)
1068 * FIXME: We're hardcoding the CCID, and doing this at this point makes
1069 * the listening (master) sock get CCID control blocks, which is not
1070 * necessary, but for now, to not mess with the test userspace apps,
1071 * lets leave it here, later the real solution is to do this in a
1072 * setsockopt(CCIDs-I-want/accept). -acme
1074 if (likely(!dccp_ctl_socket_init)) {
1075 dp->dccps_hc_rx_ccid = ccid_init(dp->dccps_options.dccpo_rx_ccid,
1077 dp->dccps_hc_tx_ccid = ccid_init(dp->dccps_options.dccpo_tx_ccid,
1079 if (dp->dccps_hc_rx_ccid == NULL ||
1080 dp->dccps_hc_tx_ccid == NULL) {
1081 ccid_exit(dp->dccps_hc_rx_ccid, sk);
1082 ccid_exit(dp->dccps_hc_tx_ccid, sk);
1083 if (dp->dccps_options.dccpo_send_ack_vector) {
1084 dccp_ackvec_free(dp->dccps_hc_rx_ackvec);
1085 dp->dccps_hc_rx_ackvec = NULL;
1087 dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL;
1091 dccp_ctl_socket_init = 0;
1093 dccp_init_xmit_timers(sk);
1094 icsk->icsk_rto = DCCP_TIMEOUT_INIT;
1095 sk->sk_state = DCCP_CLOSED;
1096 sk->sk_write_space = dccp_write_space;
1097 icsk->icsk_af_ops = &dccp_ipv4_af_ops;
1098 icsk->icsk_sync_mss = dccp_sync_mss;
1099 dp->dccps_mss_cache = 536;
1100 dp->dccps_role = DCCP_ROLE_UNDEFINED;
1101 dp->dccps_service = DCCP_SERVICE_INVALID_VALUE;
1106 EXPORT_SYMBOL_GPL(dccp_v4_init_sock);
1108 int dccp_v4_destroy_sock(struct sock *sk)
1110 struct dccp_sock *dp = dccp_sk(sk);
1113 * DCCP doesn't use sk_write_queue, just sk_send_head
1114 * for retransmissions
1116 if (sk->sk_send_head != NULL) {
1117 kfree_skb(sk->sk_send_head);
1118 sk->sk_send_head = NULL;
1121 /* Clean up a referenced DCCP bind bucket. */
1122 if (inet_csk(sk)->icsk_bind_hash != NULL)
1123 inet_put_port(&dccp_hashinfo, sk);
1125 kfree(dp->dccps_service_list);
1126 dp->dccps_service_list = NULL;
1128 ccid_hc_rx_exit(dp->dccps_hc_rx_ccid, sk);
1129 ccid_hc_tx_exit(dp->dccps_hc_tx_ccid, sk);
1130 if (dp->dccps_options.dccpo_send_ack_vector) {
1131 dccp_ackvec_free(dp->dccps_hc_rx_ackvec);
1132 dp->dccps_hc_rx_ackvec = NULL;
1134 ccid_exit(dp->dccps_hc_rx_ccid, sk);
1135 ccid_exit(dp->dccps_hc_tx_ccid, sk);
1136 dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL;
1141 EXPORT_SYMBOL_GPL(dccp_v4_destroy_sock);
1143 static void dccp_v4_reqsk_destructor(struct request_sock *req)
1145 kfree(inet_rsk(req)->opt);
1148 static struct request_sock_ops dccp_request_sock_ops = {
1150 .obj_size = sizeof(struct dccp_request_sock),
1151 .rtx_syn_ack = dccp_v4_send_response,
1152 .send_ack = dccp_v4_reqsk_send_ack,
1153 .destructor = dccp_v4_reqsk_destructor,
1154 .send_reset = dccp_v4_ctl_send_reset,
1157 static struct timewait_sock_ops dccp_timewait_sock_ops = {
1158 .twsk_obj_size = sizeof(struct inet_timewait_sock),
1161 struct proto dccp_prot = {
1163 .owner = THIS_MODULE,
1164 .close = dccp_close,
1165 .connect = dccp_v4_connect,
1166 .disconnect = dccp_disconnect,
1167 .ioctl = dccp_ioctl,
1168 .init = dccp_v4_init_sock,
1169 .setsockopt = dccp_setsockopt,
1170 .getsockopt = dccp_getsockopt,
1171 .sendmsg = dccp_sendmsg,
1172 .recvmsg = dccp_recvmsg,
1173 .backlog_rcv = dccp_v4_do_rcv,
1174 .hash = dccp_v4_hash,
1175 .unhash = dccp_unhash,
1176 .accept = inet_csk_accept,
1177 .get_port = dccp_v4_get_port,
1178 .shutdown = dccp_shutdown,
1179 .destroy = dccp_v4_destroy_sock,
1180 .orphan_count = &dccp_orphan_count,
1181 .max_header = MAX_DCCP_HEADER,
1182 .obj_size = sizeof(struct dccp_sock),
1183 .rsk_prot = &dccp_request_sock_ops,
1184 .twsk_prot = &dccp_timewait_sock_ops,
1187 EXPORT_SYMBOL_GPL(dccp_prot);