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/skbuff.h>
22 static void dccp_fin(struct sock
*sk
, struct sk_buff
*skb
)
24 sk
->sk_shutdown
|= RCV_SHUTDOWN
;
25 sock_set_flag(sk
, SOCK_DONE
);
26 __skb_pull(skb
, dccp_hdr(skb
)->dccph_doff
* 4);
27 __skb_queue_tail(&sk
->sk_receive_queue
, skb
);
28 skb_set_owner_r(skb
, sk
);
29 sk
->sk_data_ready(sk
, 0);
32 static void dccp_rcv_close(struct sock
*sk
, struct sk_buff
*skb
)
34 switch (sk
->sk_state
) {
37 dccp_v4_send_reset(sk
, DCCP_RESET_CODE_CLOSED
);
39 dccp_set_state(sk
, DCCP_CLOSED
);
44 static void dccp_rcv_closereq(struct sock
*sk
, struct sk_buff
*skb
)
47 * Step 7: Check for unexpected packet types
48 * If (S.is_server and P.type == CloseReq)
49 * Send Sync packet acknowledging P.seqno
50 * Drop packet and return
52 if (dccp_sk(sk
)->dccps_role
!= DCCP_ROLE_CLIENT
) {
53 dccp_send_sync(sk
, DCCP_SKB_CB(skb
)->dccpd_seq
);
57 switch (sk
->sk_state
) {
60 dccp_set_state(sk
, DCCP_CLOSING
);
66 static inline void dccp_event_ack_recv(struct sock
*sk
, struct sk_buff
*skb
)
68 struct dccp_sock
*dp
= dccp_sk(sk
);
70 if (dp
->dccps_options
.dccpo_send_ack_vector
)
71 dccp_ackpkts_check_rcv_ackno(dp
->dccps_hc_rx_ackpkts
, sk
,
72 DCCP_SKB_CB(skb
)->dccpd_ack_seq
);
75 static int dccp_check_seqno(struct sock
*sk
, struct sk_buff
*skb
)
77 const struct dccp_hdr
*dh
= dccp_hdr(skb
);
78 struct dccp_sock
*dp
= dccp_sk(sk
);
79 u64 lswl
= dp
->dccps_swl
;
80 u64 lawl
= dp
->dccps_awl
;
83 * Step 5: Prepare sequence numbers for Sync
84 * If P.type == Sync or P.type == SyncAck,
85 * If S.AWL <= P.ackno <= S.AWH and P.seqno >= S.SWL,
86 * / * P is valid, so update sequence number variables
87 * accordingly. After this update, P will pass the tests
88 * in Step 6. A SyncAck is generated if necessary in
90 * Update S.GSR, S.SWL, S.SWH
92 * Drop packet and return
94 if (dh
->dccph_type
== DCCP_PKT_SYNC
||
95 dh
->dccph_type
== DCCP_PKT_SYNCACK
) {
96 if (between48(DCCP_SKB_CB(skb
)->dccpd_ack_seq
, dp
->dccps_awl
, dp
->dccps_awh
) &&
97 !before48(DCCP_SKB_CB(skb
)->dccpd_seq
, dp
->dccps_swl
))
98 dccp_update_gsr(sk
, DCCP_SKB_CB(skb
)->dccpd_seq
);
102 * Step 6: Check sequence numbers
103 * Let LSWL = S.SWL and LAWL = S.AWL
104 * If P.type == CloseReq or P.type == Close or P.type == Reset,
105 * LSWL := S.GSR + 1, LAWL := S.GAR
106 * If LSWL <= P.seqno <= S.SWH
107 * and (P.ackno does not exist or LAWL <= P.ackno <= S.AWH),
108 * Update S.GSR, S.SWL, S.SWH
112 * Send Sync packet acknowledging P.seqno
113 * Drop packet and return
115 } else if (dh
->dccph_type
== DCCP_PKT_CLOSEREQ
||
116 dh
->dccph_type
== DCCP_PKT_CLOSE
||
117 dh
->dccph_type
== DCCP_PKT_RESET
) {
118 lswl
= dp
->dccps_gsr
;
119 dccp_inc_seqno(&lswl
);
120 lawl
= dp
->dccps_gar
;
123 if (between48(DCCP_SKB_CB(skb
)->dccpd_seq
, lswl
, dp
->dccps_swh
) &&
124 (DCCP_SKB_CB(skb
)->dccpd_ack_seq
== DCCP_PKT_WITHOUT_ACK_SEQ
||
125 between48(DCCP_SKB_CB(skb
)->dccpd_ack_seq
, lawl
, dp
->dccps_awh
))) {
126 dccp_update_gsr(sk
, DCCP_SKB_CB(skb
)->dccpd_seq
);
128 if (dh
->dccph_type
!= DCCP_PKT_SYNC
&&
129 DCCP_SKB_CB(skb
)->dccpd_ack_seq
!= DCCP_PKT_WITHOUT_ACK_SEQ
)
130 dp
->dccps_gar
= DCCP_SKB_CB(skb
)->dccpd_ack_seq
;
132 dccp_pr_debug("Step 6 failed, sending SYNC...\n");
133 dccp_send_sync(sk
, DCCP_SKB_CB(skb
)->dccpd_seq
);
140 int dccp_rcv_established(struct sock
*sk
, struct sk_buff
*skb
,
141 const struct dccp_hdr
*dh
, const unsigned len
)
143 struct dccp_sock
*dp
= dccp_sk(sk
);
145 if (dccp_check_seqno(sk
, skb
))
148 if (dccp_parse_options(sk
, skb
))
151 if (DCCP_SKB_CB(skb
)->dccpd_ack_seq
!= DCCP_PKT_WITHOUT_ACK_SEQ
)
152 dccp_event_ack_recv(sk
, skb
);
155 * FIXME: check ECN to see if we should use
156 * DCCP_ACKPKTS_STATE_ECN_MARKED
158 if (dp
->dccps_options
.dccpo_send_ack_vector
) {
159 struct dccp_ackpkts
*ap
= dp
->dccps_hc_rx_ackpkts
;
161 if (dccp_ackpkts_add(dp
->dccps_hc_rx_ackpkts
,
162 DCCP_SKB_CB(skb
)->dccpd_seq
,
163 DCCP_ACKPKTS_STATE_RECEIVED
)) {
164 LIMIT_NETDEBUG(pr_info("DCCP: acknowledgeable packets buffer full!\n"));
165 ap
->dccpap_ack_seqno
= DCCP_MAX_SEQNO
+ 1;
166 inet_csk_schedule_ack(sk
);
167 inet_csk_reset_xmit_timer(sk
, ICSK_TIME_DACK
, TCP_DELACK_MIN
, TCP_RTO_MAX
);
172 * FIXME: this activation is probably wrong, have to study more
173 * TCP delack machinery and how it fits into DCCP draft, but
174 * for now it kinda "works" 8)
176 if (!inet_csk_ack_scheduled(sk
)) {
177 inet_csk_schedule_ack(sk
);
178 inet_csk_reset_xmit_timer(sk
, ICSK_TIME_DACK
, 5 * HZ
, TCP_RTO_MAX
);
182 ccid_hc_rx_packet_recv(dp
->dccps_hc_rx_ccid
, sk
, skb
);
183 ccid_hc_tx_packet_recv(dp
->dccps_hc_tx_ccid
, sk
, skb
);
185 switch (dccp_hdr(skb
)->dccph_type
) {
186 case DCCP_PKT_DATAACK
:
189 * FIXME: check if sk_receive_queue is full, schedule DATA_DROPPED option
192 __skb_pull(skb
, dh
->dccph_doff
* 4);
193 __skb_queue_tail(&sk
->sk_receive_queue
, skb
);
194 skb_set_owner_r(skb
, sk
);
195 sk
->sk_data_ready(sk
, 0);
201 * Step 9: Process Reset
202 * If P.type == Reset,
203 * Tear down connection
204 * S.state := TIMEWAIT
206 * Drop packet and return
209 dccp_time_wait(sk
, DCCP_TIME_WAIT
, 0);
211 case DCCP_PKT_CLOSEREQ
:
212 dccp_rcv_closereq(sk
, skb
);
215 dccp_rcv_close(sk
, skb
);
217 case DCCP_PKT_REQUEST
:
219 * or (S.is_server and P.type == Response)
220 * or (S.is_client and P.type == Request)
221 * or (S.state >= OPEN and P.type == Request
222 * and P.seqno >= S.OSR)
223 * or (S.state >= OPEN and P.type == Response
224 * and P.seqno >= S.OSR)
225 * or (S.state == RESPOND and P.type == Data),
226 * Send Sync packet acknowledging P.seqno
227 * Drop packet and return
229 if (dp
->dccps_role
!= DCCP_ROLE_LISTEN
)
232 case DCCP_PKT_RESPONSE
:
233 if (dp
->dccps_role
!= DCCP_ROLE_CLIENT
)
236 if (!before48(DCCP_SKB_CB(skb
)->dccpd_seq
, dp
->dccps_osr
)) {
238 dccp_send_sync(sk
, DCCP_SKB_CB(skb
)->dccpd_seq
);
243 DCCP_INC_STATS_BH(DCCP_MIB_INERRS
);
249 static int dccp_rcv_request_sent_state_process(struct sock
*sk
,
251 const struct dccp_hdr
*dh
,
255 * Step 4: Prepare sequence numbers in REQUEST
256 * If S.state == REQUEST,
257 * If (P.type == Response or P.type == Reset)
258 * and S.AWL <= P.ackno <= S.AWH,
259 * / * Set sequence number variables corresponding to the
260 * other endpoint, so P will pass the tests in Step 6 * /
261 * Set S.GSR, S.ISR, S.SWL, S.SWH
262 * / * Response processing continues in Step 10; Reset
263 * processing continues in Step 9 * /
265 if (dh
->dccph_type
== DCCP_PKT_RESPONSE
) {
266 const struct inet_connection_sock
*icsk
= inet_csk(sk
);
267 struct dccp_sock
*dp
= dccp_sk(sk
);
269 /* Stop the REQUEST timer */
270 inet_csk_clear_xmit_timer(sk
, ICSK_TIME_RETRANS
);
271 BUG_TRAP(sk
->sk_send_head
!= NULL
);
272 __kfree_skb(sk
->sk_send_head
);
273 sk
->sk_send_head
= NULL
;
275 if (!between48(DCCP_SKB_CB(skb
)->dccpd_ack_seq
, dp
->dccps_awl
, dp
->dccps_awh
)) {
276 dccp_pr_debug("invalid ackno: S.AWL=%llu, P.ackno=%llu, S.AWH=%llu \n",
277 dp
->dccps_awl
, DCCP_SKB_CB(skb
)->dccpd_ack_seq
, dp
->dccps_awh
);
278 goto out_invalid_packet
;
281 dp
->dccps_isr
= DCCP_SKB_CB(skb
)->dccpd_seq
;
282 dccp_update_gsr(sk
, DCCP_SKB_CB(skb
)->dccpd_seq
);
284 if (ccid_hc_rx_init(dp
->dccps_hc_rx_ccid
, sk
) != 0 ||
285 ccid_hc_tx_init(dp
->dccps_hc_tx_ccid
, sk
) != 0) {
286 ccid_hc_rx_exit(dp
->dccps_hc_rx_ccid
, sk
);
287 ccid_hc_tx_exit(dp
->dccps_hc_tx_ccid
, sk
);
288 /* FIXME: send appropriate RESET code */
289 goto out_invalid_packet
;
292 dccp_sync_mss(sk
, dp
->dccps_pmtu_cookie
);
295 * Step 10: Process REQUEST state (second part)
296 * If S.state == REQUEST,
297 * / * If we get here, P is a valid Response from the server (see
298 * Step 4), and we should move to PARTOPEN state. PARTOPEN
299 * means send an Ack, don't send Data packets, retransmit
300 * Acks periodically, and always include any Init Cookie from
302 * S.state := PARTOPEN
304 * Continue with S.state == PARTOPEN
305 * / * Step 12 will send the Ack completing the three-way
308 dccp_set_state(sk
, DCCP_PARTOPEN
);
310 /* Make sure socket is routed, for correct metrics. */
311 inet_sk_rebuild_header(sk
);
313 if (!sock_flag(sk
, SOCK_DEAD
)) {
314 sk
->sk_state_change(sk
);
315 sk_wake_async(sk
, 0, POLL_OUT
);
318 if (sk
->sk_write_pending
|| icsk
->icsk_ack
.pingpong
||
319 icsk
->icsk_accept_queue
.rskq_defer_accept
) {
320 /* Save one ACK. Data will be ready after
321 * several ticks, if write_pending is set.
323 * It may be deleted, but with this feature tcpdumps
324 * look so _wonderfully_ clever, that I was not able
325 * to stand against the temptation 8) --ANK
328 * OK, in DCCP we can as well do a similar trick, its
329 * even in the draft, but there is no need for us to
330 * schedule an ack here, as dccp_sendmsg does this for
331 * us, also stated in the draft. -acme
341 return 1; /* dccp_v4_do_rcv will send a reset, but...
342 FIXME: the reset code should be DCCP_RESET_CODE_PACKET_ERROR */
345 static int dccp_rcv_respond_partopen_state_process(struct sock
*sk
,
347 const struct dccp_hdr
*dh
,
352 switch (dh
->dccph_type
) {
354 inet_csk_clear_xmit_timer(sk
, ICSK_TIME_DACK
);
356 case DCCP_PKT_DATAACK
:
359 * FIXME: we should be reseting the PARTOPEN (DELACK) timer here,
360 * but only if we haven't used the DELACK timer for something else,
361 * like sending a delayed ack for a TIMESTAMP echo, etc, for now
362 * were not clearing it, sending an extra ACK when there is nothing
363 * else to do in DELACK is not a big deal after all.
366 /* Stop the PARTOPEN timer */
367 if (sk
->sk_state
== DCCP_PARTOPEN
)
368 inet_csk_clear_xmit_timer(sk
, ICSK_TIME_DACK
);
370 dccp_sk(sk
)->dccps_osr
= DCCP_SKB_CB(skb
)->dccpd_seq
;
371 dccp_set_state(sk
, DCCP_OPEN
);
373 if (dh
->dccph_type
== DCCP_PKT_DATAACK
) {
374 dccp_rcv_established(sk
, skb
, dh
, len
);
375 queued
= 1; /* packet was queued (by dccp_rcv_established) */
383 int dccp_rcv_state_process(struct sock
*sk
, struct sk_buff
*skb
,
384 struct dccp_hdr
*dh
, unsigned len
)
386 struct dccp_sock
*dp
= dccp_sk(sk
);
387 const int old_state
= sk
->sk_state
;
390 if (sk
->sk_state
!= DCCP_LISTEN
&& sk
->sk_state
!= DCCP_REQUESTING
) {
391 if (dccp_check_seqno(sk
, skb
))
395 * Step 8: Process options and mark acknowledgeable
397 if (dccp_parse_options(sk
, skb
))
400 if (DCCP_SKB_CB(skb
)->dccpd_ack_seq
!= DCCP_PKT_WITHOUT_ACK_SEQ
)
401 dccp_event_ack_recv(sk
, skb
);
403 ccid_hc_rx_packet_recv(dp
->dccps_hc_rx_ccid
, sk
, skb
);
404 ccid_hc_tx_packet_recv(dp
->dccps_hc_tx_ccid
, sk
, skb
);
407 * FIXME: check ECN to see if we should use
408 * DCCP_ACKPKTS_STATE_ECN_MARKED
410 if (dp
->dccps_options
.dccpo_send_ack_vector
) {
411 if (dccp_ackpkts_add(dp
->dccps_hc_rx_ackpkts
,
412 DCCP_SKB_CB(skb
)->dccpd_seq
,
413 DCCP_ACKPKTS_STATE_RECEIVED
))
416 * FIXME: this activation is probably wrong, have to study more
417 * TCP delack machinery and how it fits into DCCP draft, but
418 * for now it kinda "works" 8)
420 if (dp
->dccps_hc_rx_ackpkts
->dccpap_ack_seqno
== DCCP_MAX_SEQNO
+ 1 &&
421 !inet_csk_ack_scheduled(sk
)) {
422 inet_csk_schedule_ack(sk
);
423 inet_csk_reset_xmit_timer(sk
, ICSK_TIME_DACK
, TCP_DELACK_MIN
, TCP_RTO_MAX
);
429 * Step 9: Process Reset
430 * If P.type == Reset,
431 * Tear down connection
432 * S.state := TIMEWAIT
434 * Drop packet and return
436 if (dh
->dccph_type
== DCCP_PKT_RESET
) {
437 /* Queue the equivalent of TCP fin so that dccp_recvmsg exits the loop */
439 dccp_time_wait(sk
, DCCP_TIME_WAIT
, 0);
442 * Step 7: Check for unexpected packet types
443 * If (S.is_server and P.type == CloseReq)
444 * or (S.is_server and P.type == Response)
445 * or (S.is_client and P.type == Request)
446 * or (S.state == RESPOND and P.type == Data),
447 * Send Sync packet acknowledging P.seqno
448 * Drop packet and return
450 } else if ((dp
->dccps_role
!= DCCP_ROLE_CLIENT
&&
451 (dh
->dccph_type
== DCCP_PKT_RESPONSE
|| dh
->dccph_type
== DCCP_PKT_CLOSEREQ
)) ||
452 (dp
->dccps_role
== DCCP_ROLE_CLIENT
&&
453 dh
->dccph_type
== DCCP_PKT_REQUEST
) ||
454 (sk
->sk_state
== DCCP_RESPOND
&& dh
->dccph_type
== DCCP_PKT_DATA
)) {
455 dccp_send_sync(sk
, DCCP_SKB_CB(skb
)->dccpd_seq
);
459 switch (sk
->sk_state
) {
464 if (dh
->dccph_type
== DCCP_PKT_ACK
||
465 dh
->dccph_type
== DCCP_PKT_DATAACK
)
468 if (dh
->dccph_type
== DCCP_PKT_RESET
)
471 if (dh
->dccph_type
== DCCP_PKT_REQUEST
) {
472 if (dccp_v4_conn_request(sk
, skb
) < 0)
475 /* FIXME: do congestion control initialization */
480 case DCCP_REQUESTING
:
481 /* FIXME: do congestion control initialization */
483 queued
= dccp_rcv_request_sent_state_process(sk
, skb
, dh
, len
);
492 queued
= dccp_rcv_respond_partopen_state_process(sk
, skb
, dh
, len
);
496 if (dh
->dccph_type
== DCCP_PKT_ACK
|| dh
->dccph_type
== DCCP_PKT_DATAACK
) {
499 sk
->sk_state_change(sk
);
500 sk_wake_async(sk
, 0, POLL_OUT
);