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
),
34 .portalloc_lock
= SPIN_LOCK_UNLOCKED
,
35 .port_rover
= 1024 - 1,
38 EXPORT_SYMBOL_GPL(dccp_hashinfo
);
40 static int dccp_v4_get_port(struct sock
*sk
, const unsigned short snum
)
42 return inet_csk_get_port(&dccp_hashinfo
, sk
, snum
);
45 static void dccp_v4_hash(struct sock
*sk
)
47 inet_hash(&dccp_hashinfo
, sk
);
50 static void dccp_v4_unhash(struct sock
*sk
)
52 inet_unhash(&dccp_hashinfo
, sk
);
55 /* called with local bh disabled */
56 static int __dccp_v4_check_established(struct sock
*sk
, const __u16 lport
,
57 struct inet_timewait_sock
**twp
)
59 struct inet_sock
*inet
= inet_sk(sk
);
60 const u32 daddr
= inet
->rcv_saddr
;
61 const u32 saddr
= inet
->daddr
;
62 const int dif
= sk
->sk_bound_dev_if
;
63 INET_ADDR_COOKIE(acookie
, saddr
, daddr
)
64 const __u32 ports
= INET_COMBINED_PORTS(inet
->dport
, lport
);
65 const int hash
= inet_ehashfn(daddr
, lport
, saddr
, inet
->dport
,
66 dccp_hashinfo
.ehash_size
);
67 struct inet_ehash_bucket
*head
= &dccp_hashinfo
.ehash
[hash
];
68 const struct sock
*sk2
;
69 const struct hlist_node
*node
;
70 struct inet_timewait_sock
*tw
;
72 write_lock(&head
->lock
);
74 /* Check TIME-WAIT sockets first. */
75 sk_for_each(sk2
, node
, &(head
+ dccp_hashinfo
.ehash_size
)->chain
) {
78 if (INET_TW_MATCH(sk2
, acookie
, saddr
, daddr
, ports
, dif
))
83 /* And established part... */
84 sk_for_each(sk2
, node
, &head
->chain
) {
85 if (INET_MATCH(sk2
, acookie
, saddr
, daddr
, ports
, dif
))
89 /* Must record num and sport now. Otherwise we will see
90 * in hash table socket with a funny identity. */
92 inet
->sport
= htons(lport
);
93 sk
->sk_hashent
= hash
;
94 BUG_TRAP(sk_unhashed(sk
));
95 __sk_add_node(sk
, &head
->chain
);
96 sock_prot_inc_use(sk
->sk_prot
);
97 write_unlock(&head
->lock
);
101 NET_INC_STATS_BH(LINUX_MIB_TIMEWAITRECYCLED
);
102 } else if (tw
!= NULL
) {
103 /* Silly. Should hash-dance instead... */
104 inet_twsk_deschedule(tw
, &dccp_death_row
);
105 NET_INC_STATS_BH(LINUX_MIB_TIMEWAITRECYCLED
);
113 write_unlock(&head
->lock
);
114 return -EADDRNOTAVAIL
;
118 * Bind a port for a connect operation and hash it.
120 static int dccp_v4_hash_connect(struct sock
*sk
)
122 const unsigned short snum
= inet_sk(sk
)->num
;
123 struct inet_bind_hashbucket
*head
;
124 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 struct hlist_node
*node
;
133 struct inet_timewait_sock
*tw
= NULL
;
137 /* TODO. Actually it is not so bad idea to remove
138 * dccp_hashinfo.portalloc_lock before next submission to
140 * As soon as we touch this place at all it is time to think.
142 * Now it protects single _advisory_ variable
143 * dccp_hashinfo.port_rover, hence it is mostly useless.
144 * Code will work nicely if we just delete it, but
145 * I am afraid in contented case it will work not better or
146 * even worse: another cpu just will hit the same bucket
148 * So some cpu salt could remove both contention and
149 * memory pingpong. Any ideas how to do this in a nice way?
151 spin_lock(&dccp_hashinfo
.portalloc_lock
);
152 rover
= dccp_hashinfo
.port_rover
;
156 if ((rover
< low
) || (rover
> high
))
158 head
= &dccp_hashinfo
.bhash
[inet_bhashfn(rover
,
159 dccp_hashinfo
.bhash_size
)];
160 spin_lock(&head
->lock
);
162 /* Does not bother with rcv_saddr checks,
163 * because the established check is already
166 inet_bind_bucket_for_each(tb
, node
, &head
->chain
) {
167 if (tb
->port
== rover
) {
168 BUG_TRAP(!hlist_empty(&tb
->owners
));
169 if (tb
->fastreuse
>= 0)
171 if (!__dccp_v4_check_established(sk
,
179 tb
= inet_bind_bucket_create(dccp_hashinfo
.bind_bucket_cachep
,
182 spin_unlock(&head
->lock
);
189 spin_unlock(&head
->lock
);
190 } while (--remaining
> 0);
191 dccp_hashinfo
.port_rover
= rover
;
192 spin_unlock(&dccp_hashinfo
.portalloc_lock
);
196 return -EADDRNOTAVAIL
;
199 /* All locks still held and bhs disabled */
200 dccp_hashinfo
.port_rover
= rover
;
201 spin_unlock(&dccp_hashinfo
.portalloc_lock
);
203 inet_bind_hash(sk
, tb
, rover
);
204 if (sk_unhashed(sk
)) {
205 inet_sk(sk
)->sport
= htons(rover
);
206 __inet_hash(&dccp_hashinfo
, sk
, 0);
208 spin_unlock(&head
->lock
);
211 inet_twsk_deschedule(tw
, &dccp_death_row
);
219 head
= &dccp_hashinfo
.bhash
[inet_bhashfn(snum
,
220 dccp_hashinfo
.bhash_size
)];
221 tb
= inet_csk(sk
)->icsk_bind_hash
;
222 spin_lock_bh(&head
->lock
);
223 if (sk_head(&tb
->owners
) == sk
&& sk
->sk_bind_node
.next
== NULL
) {
224 __inet_hash(&dccp_hashinfo
, sk
, 0);
225 spin_unlock_bh(&head
->lock
);
228 spin_unlock(&head
->lock
);
229 /* No definite answer... Walk to established hash table */
230 ret
= __dccp_v4_check_established(sk
, snum
, NULL
);
237 static int dccp_v4_connect(struct sock
*sk
, struct sockaddr
*uaddr
,
240 struct inet_sock
*inet
= inet_sk(sk
);
241 struct dccp_sock
*dp
= dccp_sk(sk
);
242 const struct sockaddr_in
*usin
= (struct sockaddr_in
*)uaddr
;
248 dp
->dccps_role
= DCCP_ROLE_CLIENT
;
250 if (dccp_service_not_initialized(sk
))
253 if (addr_len
< sizeof(struct sockaddr_in
))
256 if (usin
->sin_family
!= AF_INET
)
257 return -EAFNOSUPPORT
;
259 nexthop
= daddr
= usin
->sin_addr
.s_addr
;
260 if (inet
->opt
!= NULL
&& inet
->opt
->srr
) {
263 nexthop
= inet
->opt
->faddr
;
266 tmp
= ip_route_connect(&rt
, nexthop
, inet
->saddr
,
267 RT_CONN_FLAGS(sk
), sk
->sk_bound_dev_if
,
269 inet
->sport
, usin
->sin_port
, sk
);
273 if (rt
->rt_flags
& (RTCF_MULTICAST
| RTCF_BROADCAST
)) {
278 if (inet
->opt
== NULL
|| !inet
->opt
->srr
)
281 if (inet
->saddr
== 0)
282 inet
->saddr
= rt
->rt_src
;
283 inet
->rcv_saddr
= inet
->saddr
;
285 inet
->dport
= usin
->sin_port
;
288 dp
->dccps_ext_header_len
= 0;
289 if (inet
->opt
!= NULL
)
290 dp
->dccps_ext_header_len
= inet
->opt
->optlen
;
292 * Socket identity is still unknown (sport may be zero).
293 * However we set state to DCCP_REQUESTING and not releasing socket
294 * lock select source port, enter ourselves into the hash tables and
295 * complete initialization after this.
297 dccp_set_state(sk
, DCCP_REQUESTING
);
298 err
= dccp_v4_hash_connect(sk
);
302 err
= ip_route_newports(&rt
, inet
->sport
, inet
->dport
, sk
);
306 /* OK, now commit destination to socket. */
307 sk_setup_caps(sk
, &rt
->u
.dst
);
310 dp
->dccps_iss
= secure_dccp_sequence_number(inet
->saddr
,
314 dccp_update_gss(sk
, dp
->dccps_iss
);
317 * SWL and AWL are initially adjusted so that they are not less than
318 * the initial Sequence Numbers received and sent, respectively:
319 * SWL := max(GSR + 1 - floor(W/4), ISR),
320 * AWL := max(GSS - W' + 1, ISS).
321 * These adjustments MUST be applied only at the beginning of the
324 dccp_set_seqno(&dp
->dccps_awl
, max48(dp
->dccps_awl
, dp
->dccps_iss
));
326 inet
->id
= dp
->dccps_iss
^ jiffies
;
328 err
= dccp_connect(sk
);
336 * This unhashes the socket and releases the local port, if necessary.
338 dccp_set_state(sk
, DCCP_CLOSED
);
340 sk
->sk_route_caps
= 0;
346 * This routine does path mtu discovery as defined in RFC1191.
348 static inline void dccp_do_pmtu_discovery(struct sock
*sk
,
349 const struct iphdr
*iph
,
352 struct dst_entry
*dst
;
353 const struct inet_sock
*inet
= inet_sk(sk
);
354 const struct dccp_sock
*dp
= dccp_sk(sk
);
356 /* We are not interested in DCCP_LISTEN and request_socks (RESPONSEs
357 * send out by Linux are always < 576bytes so they should go through
360 if (sk
->sk_state
== DCCP_LISTEN
)
363 /* We don't check in the destentry if pmtu discovery is forbidden
364 * on this route. We just assume that no packet_to_big packets
365 * are send back when pmtu discovery is not active.
366 * There is a small race when the user changes this flag in the
367 * route, but I think that's acceptable.
369 if ((dst
= __sk_dst_check(sk
, 0)) == NULL
)
372 dst
->ops
->update_pmtu(dst
, mtu
);
374 /* Something is about to be wrong... Remember soft error
375 * for the case, if this connection will not able to recover.
377 if (mtu
< dst_mtu(dst
) && ip_dont_fragment(sk
, dst
))
378 sk
->sk_err_soft
= EMSGSIZE
;
382 if (inet
->pmtudisc
!= IP_PMTUDISC_DONT
&&
383 dp
->dccps_pmtu_cookie
> mtu
) {
384 dccp_sync_mss(sk
, mtu
);
387 * From: draft-ietf-dccp-spec-11.txt
389 * DCCP-Sync packets are the best choice for upward
390 * probing, since DCCP-Sync probes do not risk application
393 dccp_send_sync(sk
, dp
->dccps_gsr
, DCCP_PKT_SYNC
);
394 } /* else let the usual retransmit timer handle it */
397 static void dccp_v4_ctl_send_ack(struct sk_buff
*rxskb
)
400 struct dccp_hdr
*rxdh
= dccp_hdr(rxskb
), *dh
;
401 const int dccp_hdr_ack_len
= sizeof(struct dccp_hdr
) +
402 sizeof(struct dccp_hdr_ext
) +
403 sizeof(struct dccp_hdr_ack_bits
);
406 if (((struct rtable
*)rxskb
->dst
)->rt_type
!= RTN_LOCAL
)
409 skb
= alloc_skb(MAX_DCCP_HEADER
+ 15, GFP_ATOMIC
);
413 /* Reserve space for headers. */
414 skb_reserve(skb
, MAX_DCCP_HEADER
);
416 skb
->dst
= dst_clone(rxskb
->dst
);
418 skb
->h
.raw
= skb_push(skb
, dccp_hdr_ack_len
);
420 memset(dh
, 0, dccp_hdr_ack_len
);
422 /* Build DCCP header and checksum it. */
423 dh
->dccph_type
= DCCP_PKT_ACK
;
424 dh
->dccph_sport
= rxdh
->dccph_dport
;
425 dh
->dccph_dport
= rxdh
->dccph_sport
;
426 dh
->dccph_doff
= dccp_hdr_ack_len
/ 4;
429 dccp_hdr_set_seq(dh
, DCCP_SKB_CB(rxskb
)->dccpd_ack_seq
);
430 dccp_hdr_set_ack(dccp_hdr_ack_bits(skb
),
431 DCCP_SKB_CB(rxskb
)->dccpd_seq
);
433 bh_lock_sock(dccp_ctl_socket
->sk
);
434 err
= ip_build_and_send_pkt(skb
, dccp_ctl_socket
->sk
,
435 rxskb
->nh
.iph
->daddr
,
436 rxskb
->nh
.iph
->saddr
, NULL
);
437 bh_unlock_sock(dccp_ctl_socket
->sk
);
439 if (err
== NET_XMIT_CN
|| err
== 0) {
440 DCCP_INC_STATS_BH(DCCP_MIB_OUTSEGS
);
441 DCCP_INC_STATS_BH(DCCP_MIB_OUTRSTS
);
445 static void dccp_v4_reqsk_send_ack(struct sk_buff
*skb
,
446 struct request_sock
*req
)
448 dccp_v4_ctl_send_ack(skb
);
451 static int dccp_v4_send_response(struct sock
*sk
, struct request_sock
*req
,
452 struct dst_entry
*dst
)
457 /* First, grab a route. */
459 if (dst
== NULL
&& (dst
= inet_csk_route_req(sk
, req
)) == NULL
)
462 skb
= dccp_make_response(sk
, dst
, req
);
464 const struct inet_request_sock
*ireq
= inet_rsk(req
);
466 err
= ip_build_and_send_pkt(skb
, sk
, ireq
->loc_addr
,
469 if (err
== NET_XMIT_CN
)
479 * This routine is called by the ICMP module when it gets some sort of error
480 * condition. If err < 0 then the socket should be closed and the error
481 * returned to the user. If err > 0 it's just the icmp type << 8 | icmp code.
482 * After adjustment header points to the first 8 bytes of the tcp header. We
483 * need to find the appropriate port.
485 * The locking strategy used here is very "optimistic". When someone else
486 * accesses the socket the ICMP is just dropped and for some paths there is no
487 * check at all. A more general error queue to queue errors for later handling
488 * is probably better.
490 void dccp_v4_err(struct sk_buff
*skb
, u32 info
)
492 const struct iphdr
*iph
= (struct iphdr
*)skb
->data
;
493 const struct dccp_hdr
*dh
= (struct dccp_hdr
*)(skb
->data
+
495 struct dccp_sock
*dp
;
496 struct inet_sock
*inet
;
497 const int type
= skb
->h
.icmph
->type
;
498 const int code
= skb
->h
.icmph
->code
;
503 if (skb
->len
< (iph
->ihl
<< 2) + 8) {
504 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS
);
508 sk
= inet_lookup(&dccp_hashinfo
, iph
->daddr
, dh
->dccph_dport
,
509 iph
->saddr
, dh
->dccph_sport
, inet_iif(skb
));
511 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS
);
515 if (sk
->sk_state
== DCCP_TIME_WAIT
) {
516 inet_twsk_put((struct inet_timewait_sock
*)sk
);
521 /* If too many ICMPs get dropped on busy
522 * servers this needs to be solved differently.
524 if (sock_owned_by_user(sk
))
525 NET_INC_STATS_BH(LINUX_MIB_LOCKDROPPEDICMPS
);
527 if (sk
->sk_state
== DCCP_CLOSED
)
531 seq
= dccp_hdr_seq(skb
);
532 if (sk
->sk_state
!= DCCP_LISTEN
&&
533 !between48(seq
, dp
->dccps_swl
, dp
->dccps_swh
)) {
534 NET_INC_STATS(LINUX_MIB_OUTOFWINDOWICMPS
);
539 case ICMP_SOURCE_QUENCH
:
540 /* Just silently ignore these. */
542 case ICMP_PARAMETERPROB
:
545 case ICMP_DEST_UNREACH
:
546 if (code
> NR_ICMP_UNREACH
)
549 if (code
== ICMP_FRAG_NEEDED
) { /* PMTU discovery (RFC1191) */
550 if (!sock_owned_by_user(sk
))
551 dccp_do_pmtu_discovery(sk
, iph
, info
);
555 err
= icmp_err_convert
[code
].errno
;
557 case ICMP_TIME_EXCEEDED
:
564 switch (sk
->sk_state
) {
565 struct request_sock
*req
, **prev
;
567 if (sock_owned_by_user(sk
))
569 req
= inet_csk_search_req(sk
, &prev
, dh
->dccph_dport
,
570 iph
->daddr
, iph
->saddr
);
575 * ICMPs are not backlogged, hence we cannot get an established
580 if (seq
!= dccp_rsk(req
)->dreq_iss
) {
581 NET_INC_STATS_BH(LINUX_MIB_OUTOFWINDOWICMPS
);
585 * Still in RESPOND, just remove it silently.
586 * There is no good way to pass the error to the newly
587 * created socket, and POSIX does not want network
588 * errors returned from accept().
590 inet_csk_reqsk_queue_drop(sk
, req
, prev
);
593 case DCCP_REQUESTING
:
595 if (!sock_owned_by_user(sk
)) {
596 DCCP_INC_STATS_BH(DCCP_MIB_ATTEMPTFAILS
);
599 sk
->sk_error_report(sk
);
603 sk
->sk_err_soft
= err
;
607 /* If we've already connected we will keep trying
608 * until we time out, or the user gives up.
610 * rfc1122 4.2.3.9 allows to consider as hard errors
611 * only PROTO_UNREACH and PORT_UNREACH (well, FRAG_FAILED too,
612 * but it is obsoleted by pmtu discovery).
614 * Note, that in modern internet, where routing is unreliable
615 * and in each dark corner broken firewalls sit, sending random
616 * errors ordered by their masters even this two messages finally lose
617 * their original sense (even Linux sends invalid PORT_UNREACHs)
619 * Now we are in compliance with RFCs.
624 if (!sock_owned_by_user(sk
) && inet
->recverr
) {
626 sk
->sk_error_report(sk
);
627 } else /* Only an error on timeout */
628 sk
->sk_err_soft
= err
;
634 int dccp_v4_send_reset(struct sock
*sk
, enum dccp_reset_codes code
)
638 * FIXME: what if rebuild_header fails?
639 * Should we be doing a rebuild_header here?
641 int err
= inet_sk_rebuild_header(sk
);
646 skb
= dccp_make_reset(sk
, sk
->sk_dst_cache
, code
);
648 const struct inet_sock
*inet
= inet_sk(sk
);
650 err
= ip_build_and_send_pkt(skb
, sk
,
651 inet
->saddr
, inet
->daddr
, NULL
);
652 if (err
== NET_XMIT_CN
)
659 static inline u64
dccp_v4_init_sequence(const struct sock
*sk
,
660 const struct sk_buff
*skb
)
662 return secure_dccp_sequence_number(skb
->nh
.iph
->daddr
,
664 dccp_hdr(skb
)->dccph_dport
,
665 dccp_hdr(skb
)->dccph_sport
);
668 static inline int dccp_bad_service_code(const struct sock
*sk
,
671 const struct dccp_sock
*dp
= dccp_sk(sk
);
673 if (dp
->dccps_service
== service
)
675 return !dccp_list_has_service(dp
->dccps_service_list
, service
);
678 int dccp_v4_conn_request(struct sock
*sk
, struct sk_buff
*skb
)
680 struct inet_request_sock
*ireq
;
682 struct request_sock
*req
;
683 struct dccp_request_sock
*dreq
;
684 const __u32 saddr
= skb
->nh
.iph
->saddr
;
685 const __u32 daddr
= skb
->nh
.iph
->daddr
;
686 const __u32 service
= dccp_hdr_request(skb
)->dccph_req_service
;
687 struct dccp_skb_cb
*dcb
= DCCP_SKB_CB(skb
);
688 __u8 reset_code
= DCCP_RESET_CODE_TOO_BUSY
;
689 struct dst_entry
*dst
= NULL
;
691 /* Never answer to DCCP_PKT_REQUESTs send to broadcast or multicast */
692 if (((struct rtable
*)skb
->dst
)->rt_flags
&
693 (RTCF_BROADCAST
| RTCF_MULTICAST
)) {
694 reset_code
= DCCP_RESET_CODE_NO_CONNECTION
;
698 if (dccp_bad_service_code(sk
, service
)) {
699 reset_code
= DCCP_RESET_CODE_BAD_SERVICE_CODE
;
703 * TW buckets are converted to open requests without
704 * limitations, they conserve resources and peer is
705 * evidently real one.
707 if (inet_csk_reqsk_queue_is_full(sk
))
711 * Accept backlog is full. If we have already queued enough
712 * of warm entries in syn queue, drop request. It is better than
713 * clogging syn queue with openreqs with exponentially increasing
716 if (sk_acceptq_is_full(sk
) && inet_csk_reqsk_queue_young(sk
) > 1)
719 req
= reqsk_alloc(sk
->sk_prot
->rsk_prot
);
723 /* FIXME: process options */
725 dccp_openreq_init(req
, &dp
, skb
);
727 ireq
= inet_rsk(req
);
728 ireq
->loc_addr
= daddr
;
729 ireq
->rmt_addr
= saddr
;
730 /* FIXME: Merge Aristeu's option parsing code when ready */
731 req
->rcv_wnd
= 100; /* Fake, option parsing will get the
736 * Step 3: Process LISTEN state
738 * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init Cookie
740 * In fact we defer setting S.GSR, S.SWL, S.SWH to
741 * dccp_create_openreq_child.
743 dreq
= dccp_rsk(req
);
744 dreq
->dreq_isr
= dcb
->dccpd_seq
;
745 dreq
->dreq_iss
= dccp_v4_init_sequence(sk
, skb
);
746 dreq
->dreq_service
= service
;
748 if (dccp_v4_send_response(sk
, req
, dst
))
751 inet_csk_reqsk_queue_hash_add(sk
, req
, DCCP_TIMEOUT_INIT
);
756 * FIXME: should be reqsk_free after implementing req->rsk_ops
760 DCCP_INC_STATS_BH(DCCP_MIB_ATTEMPTFAILS
);
761 dcb
->dccpd_reset_code
= reset_code
;
766 * The three way handshake has completed - we got a valid ACK or DATAACK -
767 * now create the new socket.
769 * This is the equivalent of TCP's tcp_v4_syn_recv_sock
771 struct sock
*dccp_v4_request_recv_sock(struct sock
*sk
, struct sk_buff
*skb
,
772 struct request_sock
*req
,
773 struct dst_entry
*dst
)
775 struct inet_request_sock
*ireq
;
776 struct inet_sock
*newinet
;
777 struct dccp_sock
*newdp
;
780 if (sk_acceptq_is_full(sk
))
783 if (dst
== NULL
&& (dst
= inet_csk_route_req(sk
, req
)) == NULL
)
786 newsk
= dccp_create_openreq_child(sk
, req
, skb
);
790 sk_setup_caps(newsk
, dst
);
792 newdp
= dccp_sk(newsk
);
793 newinet
= inet_sk(newsk
);
794 ireq
= inet_rsk(req
);
795 newinet
->daddr
= ireq
->rmt_addr
;
796 newinet
->rcv_saddr
= ireq
->loc_addr
;
797 newinet
->saddr
= ireq
->loc_addr
;
798 newinet
->opt
= ireq
->opt
;
800 newinet
->mc_index
= inet_iif(skb
);
801 newinet
->mc_ttl
= skb
->nh
.iph
->ttl
;
802 newinet
->id
= jiffies
;
804 dccp_sync_mss(newsk
, dst_mtu(dst
));
806 __inet_hash(&dccp_hashinfo
, newsk
, 0);
807 __inet_inherit_port(&dccp_hashinfo
, sk
, newsk
);
812 NET_INC_STATS_BH(LINUX_MIB_LISTENOVERFLOWS
);
814 NET_INC_STATS_BH(LINUX_MIB_LISTENDROPS
);
819 static struct sock
*dccp_v4_hnd_req(struct sock
*sk
, struct sk_buff
*skb
)
821 const struct dccp_hdr
*dh
= dccp_hdr(skb
);
822 const struct iphdr
*iph
= skb
->nh
.iph
;
824 struct request_sock
**prev
;
825 /* Find possible connection requests. */
826 struct request_sock
*req
= inet_csk_search_req(sk
, &prev
,
828 iph
->saddr
, iph
->daddr
);
830 return dccp_check_req(sk
, skb
, req
, prev
);
832 nsk
= __inet_lookup_established(&dccp_hashinfo
,
833 iph
->saddr
, dh
->dccph_sport
,
834 iph
->daddr
, ntohs(dh
->dccph_dport
),
837 if (nsk
->sk_state
!= DCCP_TIME_WAIT
) {
841 inet_twsk_put((struct inet_timewait_sock
*)nsk
);
848 int dccp_v4_checksum(const struct sk_buff
*skb
, const u32 saddr
,
851 const struct dccp_hdr
* dh
= dccp_hdr(skb
);
855 if (dh
->dccph_cscov
== 0)
856 checksum_len
= skb
->len
;
858 checksum_len
= (dh
->dccph_cscov
+ dh
->dccph_x
) * sizeof(u32
);
859 checksum_len
= checksum_len
< skb
->len
? checksum_len
:
863 tmp
= csum_partial((unsigned char *)dh
, checksum_len
, 0);
864 return csum_tcpudp_magic(saddr
, daddr
, checksum_len
,
868 static int dccp_v4_verify_checksum(struct sk_buff
*skb
,
869 const u32 saddr
, const u32 daddr
)
871 struct dccp_hdr
*dh
= dccp_hdr(skb
);
875 if (dh
->dccph_cscov
== 0)
876 checksum_len
= skb
->len
;
878 checksum_len
= (dh
->dccph_cscov
+ dh
->dccph_x
) * sizeof(u32
);
879 checksum_len
= checksum_len
< skb
->len
? checksum_len
:
882 tmp
= csum_partial((unsigned char *)dh
, checksum_len
, 0);
883 return csum_tcpudp_magic(saddr
, daddr
, checksum_len
,
884 IPPROTO_DCCP
, tmp
) == 0 ? 0 : -1;
887 static struct dst_entry
* dccp_v4_route_skb(struct sock
*sk
,
891 struct flowi fl
= { .oif
= ((struct rtable
*)skb
->dst
)->rt_iif
,
893 { .daddr
= skb
->nh
.iph
->saddr
,
894 .saddr
= skb
->nh
.iph
->daddr
,
895 .tos
= RT_CONN_FLAGS(sk
) } },
896 .proto
= sk
->sk_protocol
,
898 { .sport
= dccp_hdr(skb
)->dccph_dport
,
899 .dport
= dccp_hdr(skb
)->dccph_sport
}
903 if (ip_route_output_flow(&rt
, &fl
, sk
, 0)) {
904 IP_INC_STATS_BH(IPSTATS_MIB_OUTNOROUTES
);
911 static void dccp_v4_ctl_send_reset(struct sk_buff
*rxskb
)
914 struct dccp_hdr
*rxdh
= dccp_hdr(rxskb
), *dh
;
915 const int dccp_hdr_reset_len
= sizeof(struct dccp_hdr
) +
916 sizeof(struct dccp_hdr_ext
) +
917 sizeof(struct dccp_hdr_reset
);
919 struct dst_entry
*dst
;
922 /* Never send a reset in response to a reset. */
923 if (rxdh
->dccph_type
== DCCP_PKT_RESET
)
926 if (((struct rtable
*)rxskb
->dst
)->rt_type
!= RTN_LOCAL
)
929 dst
= dccp_v4_route_skb(dccp_ctl_socket
->sk
, rxskb
);
933 skb
= alloc_skb(MAX_DCCP_HEADER
+ 15, GFP_ATOMIC
);
937 /* Reserve space for headers. */
938 skb_reserve(skb
, MAX_DCCP_HEADER
);
939 skb
->dst
= dst_clone(dst
);
941 skb
->h
.raw
= skb_push(skb
, dccp_hdr_reset_len
);
943 memset(dh
, 0, dccp_hdr_reset_len
);
945 /* Build DCCP header and checksum it. */
946 dh
->dccph_type
= DCCP_PKT_RESET
;
947 dh
->dccph_sport
= rxdh
->dccph_dport
;
948 dh
->dccph_dport
= rxdh
->dccph_sport
;
949 dh
->dccph_doff
= dccp_hdr_reset_len
/ 4;
951 dccp_hdr_reset(skb
)->dccph_reset_code
=
952 DCCP_SKB_CB(rxskb
)->dccpd_reset_code
;
954 /* See "8.3.1. Abnormal Termination" in draft-ietf-dccp-spec-11 */
956 if (DCCP_SKB_CB(rxskb
)->dccpd_ack_seq
!= DCCP_PKT_WITHOUT_ACK_SEQ
)
957 dccp_set_seqno(&seqno
, DCCP_SKB_CB(rxskb
)->dccpd_ack_seq
+ 1);
959 dccp_hdr_set_seq(dh
, seqno
);
960 dccp_hdr_set_ack(dccp_hdr_ack_bits(skb
),
961 DCCP_SKB_CB(rxskb
)->dccpd_seq
);
963 dh
->dccph_checksum
= dccp_v4_checksum(skb
, rxskb
->nh
.iph
->saddr
,
964 rxskb
->nh
.iph
->daddr
);
966 bh_lock_sock(dccp_ctl_socket
->sk
);
967 err
= ip_build_and_send_pkt(skb
, dccp_ctl_socket
->sk
,
968 rxskb
->nh
.iph
->daddr
,
969 rxskb
->nh
.iph
->saddr
, NULL
);
970 bh_unlock_sock(dccp_ctl_socket
->sk
);
972 if (err
== NET_XMIT_CN
|| err
== 0) {
973 DCCP_INC_STATS_BH(DCCP_MIB_OUTSEGS
);
974 DCCP_INC_STATS_BH(DCCP_MIB_OUTRSTS
);
980 int dccp_v4_do_rcv(struct sock
*sk
, struct sk_buff
*skb
)
982 struct dccp_hdr
*dh
= dccp_hdr(skb
);
984 if (sk
->sk_state
== DCCP_OPEN
) { /* Fast path */
985 if (dccp_rcv_established(sk
, skb
, dh
, skb
->len
))
991 * Step 3: Process LISTEN state
992 * If S.state == LISTEN,
993 * If P.type == Request or P contains a valid Init Cookie
995 * * Must scan the packet's options to check for an Init
996 * Cookie. Only the Init Cookie is processed here,
997 * however; other options are processed in Step 8. This
998 * scan need only be performed if the endpoint uses Init
1000 * * Generate a new socket and switch to that socket *
1001 * Set S := new socket for this port pair
1003 * Choose S.ISS (initial seqno) or set from Init Cookie
1004 * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init Cookie
1005 * Continue with S.state == RESPOND
1006 * * A Response packet will be generated in Step 11 *
1008 * Generate Reset(No Connection) unless P.type == Reset
1009 * Drop packet and return
1011 * NOTE: the check for the packet types is done in
1012 * dccp_rcv_state_process
1014 if (sk
->sk_state
== DCCP_LISTEN
) {
1015 struct sock
*nsk
= dccp_v4_hnd_req(sk
, skb
);
1021 if (dccp_child_process(sk
, nsk
, skb
))
1027 if (dccp_rcv_state_process(sk
, skb
, dh
, skb
->len
))
1032 dccp_v4_ctl_send_reset(skb
);
1038 static inline int dccp_invalid_packet(struct sk_buff
*skb
)
1040 const struct dccp_hdr
*dh
;
1042 if (skb
->pkt_type
!= PACKET_HOST
)
1045 if (!pskb_may_pull(skb
, sizeof(struct dccp_hdr
))) {
1046 LIMIT_NETDEBUG(KERN_WARNING
"DCCP: pskb_may_pull failed\n");
1052 /* If the packet type is not understood, drop packet and return */
1053 if (dh
->dccph_type
>= DCCP_PKT_INVALID
) {
1054 LIMIT_NETDEBUG(KERN_WARNING
"DCCP: invalid packet type\n");
1059 * If P.Data Offset is too small for packet type, or too large for
1060 * packet, drop packet and return
1062 if (dh
->dccph_doff
< dccp_hdr_len(skb
) / sizeof(u32
)) {
1063 LIMIT_NETDEBUG(KERN_WARNING
"DCCP: P.Data Offset(%u) "
1069 if (!pskb_may_pull(skb
, dh
->dccph_doff
* sizeof(u32
))) {
1070 LIMIT_NETDEBUG(KERN_WARNING
"DCCP: P.Data Offset(%u) "
1079 * If P.type is not Data, Ack, or DataAck and P.X == 0 (the packet
1080 * has short sequence numbers), drop packet and return
1082 if (dh
->dccph_x
== 0 &&
1083 dh
->dccph_type
!= DCCP_PKT_DATA
&&
1084 dh
->dccph_type
!= DCCP_PKT_ACK
&&
1085 dh
->dccph_type
!= DCCP_PKT_DATAACK
) {
1086 LIMIT_NETDEBUG(KERN_WARNING
"DCCP: P.type (%s) not Data, Ack "
1087 "nor DataAck and P.X == 0\n",
1088 dccp_packet_name(dh
->dccph_type
));
1092 /* If the header checksum is incorrect, drop packet and return */
1093 if (dccp_v4_verify_checksum(skb
, skb
->nh
.iph
->saddr
,
1094 skb
->nh
.iph
->daddr
) < 0) {
1095 LIMIT_NETDEBUG(KERN_WARNING
"DCCP: header checksum is "
1103 /* this is called when real data arrives */
1104 int dccp_v4_rcv(struct sk_buff
*skb
)
1106 const struct dccp_hdr
*dh
;
1110 /* Step 1: Check header basics: */
1112 if (dccp_invalid_packet(skb
))
1117 DCCP_SKB_CB(skb
)->dccpd_seq
= dccp_hdr_seq(skb
);
1118 DCCP_SKB_CB(skb
)->dccpd_type
= dh
->dccph_type
;
1120 dccp_pr_debug("%8.8s "
1121 "src=%u.%u.%u.%u@%-5d "
1122 "dst=%u.%u.%u.%u@%-5d seq=%llu",
1123 dccp_packet_name(dh
->dccph_type
),
1124 NIPQUAD(skb
->nh
.iph
->saddr
), ntohs(dh
->dccph_sport
),
1125 NIPQUAD(skb
->nh
.iph
->daddr
), ntohs(dh
->dccph_dport
),
1126 (unsigned long long) DCCP_SKB_CB(skb
)->dccpd_seq
);
1128 if (dccp_packet_without_ack(skb
)) {
1129 DCCP_SKB_CB(skb
)->dccpd_ack_seq
= DCCP_PKT_WITHOUT_ACK_SEQ
;
1130 dccp_pr_debug_cat("\n");
1132 DCCP_SKB_CB(skb
)->dccpd_ack_seq
= dccp_hdr_ack_seq(skb
);
1133 dccp_pr_debug_cat(", ack=%llu\n",
1134 (unsigned long long)
1135 DCCP_SKB_CB(skb
)->dccpd_ack_seq
);
1139 * Look up flow ID in table and get corresponding socket */
1140 sk
= __inet_lookup(&dccp_hashinfo
,
1141 skb
->nh
.iph
->saddr
, dh
->dccph_sport
,
1142 skb
->nh
.iph
->daddr
, ntohs(dh
->dccph_dport
),
1148 * Generate Reset(No Connection) unless P.type == Reset
1149 * Drop packet and return
1152 dccp_pr_debug("failed to look up flow ID in table and "
1153 "get corresponding socket\n");
1154 goto no_dccp_socket
;
1159 * ... or S.state == TIMEWAIT,
1160 * Generate Reset(No Connection) unless P.type == Reset
1161 * Drop packet and return
1164 if (sk
->sk_state
== DCCP_TIME_WAIT
) {
1165 dccp_pr_debug("sk->sk_state == DCCP_TIME_WAIT: "
1170 if (!xfrm4_policy_check(sk
, XFRM_POLICY_IN
, skb
)) {
1171 dccp_pr_debug("xfrm4_policy_check failed\n");
1172 goto discard_and_relse
;
1175 if (sk_filter(sk
, skb
, 0)) {
1176 dccp_pr_debug("sk_filter failed\n");
1177 goto discard_and_relse
;
1184 if (!sock_owned_by_user(sk
))
1185 rc
= dccp_v4_do_rcv(sk
, skb
);
1187 sk_add_backlog(sk
, skb
);
1194 if (!xfrm4_policy_check(NULL
, XFRM_POLICY_IN
, skb
))
1198 * Generate Reset(No Connection) unless P.type == Reset
1199 * Drop packet and return
1201 if (dh
->dccph_type
!= DCCP_PKT_RESET
) {
1202 DCCP_SKB_CB(skb
)->dccpd_reset_code
=
1203 DCCP_RESET_CODE_NO_CONNECTION
;
1204 dccp_v4_ctl_send_reset(skb
);
1208 /* Discard frame. */
1217 inet_twsk_put((struct inet_timewait_sock
*)sk
);
1218 goto no_dccp_socket
;
1221 static int dccp_v4_init_sock(struct sock
*sk
)
1223 struct dccp_sock
*dp
= dccp_sk(sk
);
1224 static int dccp_ctl_socket_init
= 1;
1226 dccp_options_init(&dp
->dccps_options
);
1227 do_gettimeofday(&dp
->dccps_epoch
);
1229 if (dp
->dccps_options
.dccpo_send_ack_vector
) {
1230 dp
->dccps_hc_rx_ackvec
= dccp_ackvec_alloc(DCCP_MAX_ACKVEC_LEN
,
1232 if (dp
->dccps_hc_rx_ackvec
== NULL
)
1237 * FIXME: We're hardcoding the CCID, and doing this at this point makes
1238 * the listening (master) sock get CCID control blocks, which is not
1239 * necessary, but for now, to not mess with the test userspace apps,
1240 * lets leave it here, later the real solution is to do this in a
1241 * setsockopt(CCIDs-I-want/accept). -acme
1243 if (likely(!dccp_ctl_socket_init
)) {
1244 dp
->dccps_hc_rx_ccid
= ccid_init(dp
->dccps_options
.dccpo_rx_ccid
,
1246 dp
->dccps_hc_tx_ccid
= ccid_init(dp
->dccps_options
.dccpo_tx_ccid
,
1248 if (dp
->dccps_hc_rx_ccid
== NULL
||
1249 dp
->dccps_hc_tx_ccid
== NULL
) {
1250 ccid_exit(dp
->dccps_hc_rx_ccid
, sk
);
1251 ccid_exit(dp
->dccps_hc_tx_ccid
, sk
);
1252 if (dp
->dccps_options
.dccpo_send_ack_vector
) {
1253 dccp_ackvec_free(dp
->dccps_hc_rx_ackvec
);
1254 dp
->dccps_hc_rx_ackvec
= NULL
;
1256 dp
->dccps_hc_rx_ccid
= dp
->dccps_hc_tx_ccid
= NULL
;
1260 dccp_ctl_socket_init
= 0;
1262 dccp_init_xmit_timers(sk
);
1263 inet_csk(sk
)->icsk_rto
= DCCP_TIMEOUT_INIT
;
1264 sk
->sk_state
= DCCP_CLOSED
;
1265 sk
->sk_write_space
= dccp_write_space
;
1266 dp
->dccps_mss_cache
= 536;
1267 dp
->dccps_role
= DCCP_ROLE_UNDEFINED
;
1268 dp
->dccps_service
= DCCP_SERVICE_INVALID_VALUE
;
1273 static int dccp_v4_destroy_sock(struct sock
*sk
)
1275 struct dccp_sock
*dp
= dccp_sk(sk
);
1278 * DCCP doesn't use sk_qrite_queue, just sk_send_head
1279 * for retransmissions
1281 if (sk
->sk_send_head
!= NULL
) {
1282 kfree_skb(sk
->sk_send_head
);
1283 sk
->sk_send_head
= NULL
;
1286 /* Clean up a referenced DCCP bind bucket. */
1287 if (inet_csk(sk
)->icsk_bind_hash
!= NULL
)
1288 inet_put_port(&dccp_hashinfo
, sk
);
1290 if (dp
->dccps_service_list
!= NULL
) {
1291 kfree(dp
->dccps_service_list
);
1292 dp
->dccps_service_list
= NULL
;
1295 ccid_hc_rx_exit(dp
->dccps_hc_rx_ccid
, sk
);
1296 ccid_hc_tx_exit(dp
->dccps_hc_tx_ccid
, sk
);
1297 if (dp
->dccps_options
.dccpo_send_ack_vector
) {
1298 dccp_ackvec_free(dp
->dccps_hc_rx_ackvec
);
1299 dp
->dccps_hc_rx_ackvec
= NULL
;
1301 ccid_exit(dp
->dccps_hc_rx_ccid
, sk
);
1302 ccid_exit(dp
->dccps_hc_tx_ccid
, sk
);
1303 dp
->dccps_hc_rx_ccid
= dp
->dccps_hc_tx_ccid
= NULL
;
1308 static void dccp_v4_reqsk_destructor(struct request_sock
*req
)
1310 kfree(inet_rsk(req
)->opt
);
1313 static struct request_sock_ops dccp_request_sock_ops
= {
1315 .obj_size
= sizeof(struct dccp_request_sock
),
1316 .rtx_syn_ack
= dccp_v4_send_response
,
1317 .send_ack
= dccp_v4_reqsk_send_ack
,
1318 .destructor
= dccp_v4_reqsk_destructor
,
1319 .send_reset
= dccp_v4_ctl_send_reset
,
1322 struct proto dccp_v4_prot
= {
1324 .owner
= THIS_MODULE
,
1325 .close
= dccp_close
,
1326 .connect
= dccp_v4_connect
,
1327 .disconnect
= dccp_disconnect
,
1328 .ioctl
= dccp_ioctl
,
1329 .init
= dccp_v4_init_sock
,
1330 .setsockopt
= dccp_setsockopt
,
1331 .getsockopt
= dccp_getsockopt
,
1332 .sendmsg
= dccp_sendmsg
,
1333 .recvmsg
= dccp_recvmsg
,
1334 .backlog_rcv
= dccp_v4_do_rcv
,
1335 .hash
= dccp_v4_hash
,
1336 .unhash
= dccp_v4_unhash
,
1337 .accept
= inet_csk_accept
,
1338 .get_port
= dccp_v4_get_port
,
1339 .shutdown
= dccp_shutdown
,
1340 .destroy
= dccp_v4_destroy_sock
,
1341 .orphan_count
= &dccp_orphan_count
,
1342 .max_header
= MAX_DCCP_HEADER
,
1343 .obj_size
= sizeof(struct dccp_sock
),
1344 .rsk_prot
= &dccp_request_sock_ops
,
1345 .twsk_obj_size
= sizeof(struct inet_timewait_sock
),