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, DCCP_PKT_SYNC);
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);
82 * Step 5: Prepare sequence numbers for Sync
83 * If P.type == Sync or P.type == SyncAck,
84 * If S.AWL <= P.ackno <= S.AWH and P.seqno >= S.SWL,
85 * / * P is valid, so update sequence number variables
86 * accordingly. After this update, P will pass the tests
87 * in Step 6. A SyncAck is generated if necessary in
89 * Update S.GSR, S.SWL, S.SWH
91 * Drop packet and return
93 if (dh->dccph_type == DCCP_PKT_SYNC ||
94 dh->dccph_type == DCCP_PKT_SYNCACK) {
95 if (between48(DCCP_SKB_CB(skb)->dccpd_ack_seq,
96 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);
104 * Step 6: Check sequence numbers
105 * Let LSWL = S.SWL and LAWL = S.AWL
106 * If P.type == CloseReq or P.type == Close or P.type == Reset,
107 * LSWL := S.GSR + 1, LAWL := S.GAR
108 * If LSWL <= P.seqno <= S.SWH
109 * and (P.ackno does not exist or LAWL <= P.ackno <= S.AWH),
110 * Update S.GSR, S.SWL, S.SWH
114 * Send Sync packet acknowledging P.seqno
115 * Drop packet and return
117 lswl = dp->dccps_swl;
118 lawl = dp->dccps_awl;
120 if (dh->dccph_type == DCCP_PKT_CLOSEREQ ||
121 dh->dccph_type == DCCP_PKT_CLOSE ||
122 dh->dccph_type == DCCP_PKT_RESET) {
123 lswl = dp->dccps_gsr;
124 dccp_inc_seqno(&lswl);
125 lawl = dp->dccps_gar;
128 if (between48(DCCP_SKB_CB(skb)->dccpd_seq, lswl, dp->dccps_swh) &&
129 (DCCP_SKB_CB(skb)->dccpd_ack_seq == DCCP_PKT_WITHOUT_ACK_SEQ ||
130 between48(DCCP_SKB_CB(skb)->dccpd_ack_seq,
131 lawl, dp->dccps_awh))) {
132 dccp_update_gsr(sk, DCCP_SKB_CB(skb)->dccpd_seq);
134 if (dh->dccph_type != DCCP_PKT_SYNC &&
135 (DCCP_SKB_CB(skb)->dccpd_ack_seq !=
136 DCCP_PKT_WITHOUT_ACK_SEQ))
137 dp->dccps_gar = DCCP_SKB_CB(skb)->dccpd_ack_seq;
139 LIMIT_NETDEBUG("Step 6 failed, sending SYNC...\n");
140 dccp_send_sync(sk, DCCP_SKB_CB(skb)->dccpd_seq, DCCP_PKT_SYNC);
147 int dccp_rcv_established(struct sock *sk, struct sk_buff *skb,
148 const struct dccp_hdr *dh, const unsigned len)
150 struct dccp_sock *dp = dccp_sk(sk);
152 if (dccp_check_seqno(sk, skb))
155 if (dccp_parse_options(sk, skb))
158 if (DCCP_SKB_CB(skb)->dccpd_ack_seq != DCCP_PKT_WITHOUT_ACK_SEQ)
159 dccp_event_ack_recv(sk, skb);
162 * FIXME: check ECN to see if we should use
163 * DCCP_ACKPKTS_STATE_ECN_MARKED
165 if (dp->dccps_options.dccpo_send_ack_vector) {
166 struct dccp_ackpkts *ap = dp->dccps_hc_rx_ackpkts;
168 if (dccp_ackpkts_add(dp->dccps_hc_rx_ackpkts,
169 DCCP_SKB_CB(skb)->dccpd_seq,
170 DCCP_ACKPKTS_STATE_RECEIVED)) {
171 LIMIT_NETDEBUG(KERN_INFO "DCCP: acknowledgeable "
172 "packets buffer full!\n");
173 ap->dccpap_ack_seqno = DCCP_MAX_SEQNO + 1;
174 inet_csk_schedule_ack(sk);
175 inet_csk_reset_xmit_timer(sk, ICSK_TIME_DACK,
182 * FIXME: this activation is probably wrong, have to study more
183 * TCP delack machinery and how it fits into DCCP draft, but
184 * for now it kinda "works" 8)
186 if (!inet_csk_ack_scheduled(sk)) {
187 inet_csk_schedule_ack(sk);
188 inet_csk_reset_xmit_timer(sk, ICSK_TIME_DACK, 5 * HZ,
193 ccid_hc_rx_packet_recv(dp->dccps_hc_rx_ccid, sk, skb);
194 ccid_hc_tx_packet_recv(dp->dccps_hc_tx_ccid, sk, skb);
196 switch (dccp_hdr(skb)->dccph_type) {
197 case DCCP_PKT_DATAACK:
200 * FIXME: check if sk_receive_queue is full, schedule DATA_DROPPED
203 __skb_pull(skb, dh->dccph_doff * 4);
204 __skb_queue_tail(&sk->sk_receive_queue, skb);
205 skb_set_owner_r(skb, sk);
206 sk->sk_data_ready(sk, 0);
212 * Step 9: Process Reset
213 * If P.type == Reset,
214 * Tear down connection
215 * S.state := TIMEWAIT
217 * Drop packet and return
220 dccp_time_wait(sk, DCCP_TIME_WAIT, 0);
222 case DCCP_PKT_CLOSEREQ:
223 dccp_rcv_closereq(sk, skb);
226 dccp_rcv_close(sk, skb);
228 case DCCP_PKT_REQUEST:
230 * or (S.is_server and P.type == Response)
231 * or (S.is_client and P.type == Request)
232 * or (S.state >= OPEN and P.type == Request
233 * and P.seqno >= S.OSR)
234 * or (S.state >= OPEN and P.type == Response
235 * and P.seqno >= S.OSR)
236 * or (S.state == RESPOND and P.type == Data),
237 * Send Sync packet acknowledging P.seqno
238 * Drop packet and return
240 if (dp->dccps_role != DCCP_ROLE_LISTEN)
243 case DCCP_PKT_RESPONSE:
244 if (dp->dccps_role != DCCP_ROLE_CLIENT)
247 if (!before48(DCCP_SKB_CB(skb)->dccpd_seq, dp->dccps_osr)) {
249 dccp_send_sync(sk, DCCP_SKB_CB(skb)->dccpd_seq,
254 dccp_send_sync(sk, DCCP_SKB_CB(skb)->dccpd_seq,
259 * As with DCCP-Ack packets, DCCP-Sync and DCCP-SyncAck packets
260 * MAY have non-zero-length application data areas, whose
261 * contents * receivers MUST ignore.
266 DCCP_INC_STATS_BH(DCCP_MIB_INERRS);
272 static int dccp_rcv_request_sent_state_process(struct sock *sk,
274 const struct dccp_hdr *dh,
278 * Step 4: Prepare sequence numbers in REQUEST
279 * If S.state == REQUEST,
280 * If (P.type == Response or P.type == Reset)
281 * and S.AWL <= P.ackno <= S.AWH,
282 * / * Set sequence number variables corresponding to the
283 * other endpoint, so P will pass the tests in Step 6 * /
284 * Set S.GSR, S.ISR, S.SWL, S.SWH
285 * / * Response processing continues in Step 10; Reset
286 * processing continues in Step 9 * /
288 if (dh->dccph_type == DCCP_PKT_RESPONSE) {
289 const struct inet_connection_sock *icsk = inet_csk(sk);
290 struct dccp_sock *dp = dccp_sk(sk);
292 /* Stop the REQUEST timer */
293 inet_csk_clear_xmit_timer(sk, ICSK_TIME_RETRANS);
294 BUG_TRAP(sk->sk_send_head != NULL);
295 __kfree_skb(sk->sk_send_head);
296 sk->sk_send_head = NULL;
298 if (!between48(DCCP_SKB_CB(skb)->dccpd_ack_seq,
299 dp->dccps_awl, dp->dccps_awh)) {
300 dccp_pr_debug("invalid ackno: S.AWL=%llu, "
301 "P.ackno=%llu, S.AWH=%llu \n",
302 (unsigned long long)dp->dccps_awl,
303 (unsigned long long)DCCP_SKB_CB(skb)->dccpd_ack_seq,
304 (unsigned long long)dp->dccps_awh);
305 goto out_invalid_packet;
308 dp->dccps_isr = DCCP_SKB_CB(skb)->dccpd_seq;
309 dccp_update_gsr(sk, DCCP_SKB_CB(skb)->dccpd_seq);
311 if (ccid_hc_rx_init(dp->dccps_hc_rx_ccid, sk) != 0 ||
312 ccid_hc_tx_init(dp->dccps_hc_tx_ccid, sk) != 0) {
313 ccid_hc_rx_exit(dp->dccps_hc_rx_ccid, sk);
314 ccid_hc_tx_exit(dp->dccps_hc_tx_ccid, sk);
315 /* FIXME: send appropriate RESET code */
316 goto out_invalid_packet;
319 dccp_sync_mss(sk, dp->dccps_pmtu_cookie);
322 * Step 10: Process REQUEST state (second part)
323 * If S.state == REQUEST,
324 * / * If we get here, P is a valid Response from the
325 * server (see Step 4), and we should move to
326 * PARTOPEN state. PARTOPEN means send an Ack,
327 * don't send Data packets, retransmit Acks
328 * periodically, and always include any Init Cookie
329 * from the Response * /
330 * S.state := PARTOPEN
332 * Continue with S.state == PARTOPEN
333 * / * Step 12 will send the Ack completing the
334 * three-way handshake * /
336 dccp_set_state(sk, DCCP_PARTOPEN);
338 /* Make sure socket is routed, for correct metrics. */
339 inet_sk_rebuild_header(sk);
341 if (!sock_flag(sk, SOCK_DEAD)) {
342 sk->sk_state_change(sk);
343 sk_wake_async(sk, 0, POLL_OUT);
346 if (sk->sk_write_pending || icsk->icsk_ack.pingpong ||
347 icsk->icsk_accept_queue.rskq_defer_accept) {
348 /* Save one ACK. Data will be ready after
349 * several ticks, if write_pending is set.
351 * It may be deleted, but with this feature tcpdumps
352 * look so _wonderfully_ clever, that I was not able
353 * to stand against the temptation 8) --ANK
356 * OK, in DCCP we can as well do a similar trick, its
357 * even in the draft, but there is no need for us to
358 * schedule an ack here, as dccp_sendmsg does this for
359 * us, also stated in the draft. -acme
369 return 1; /* dccp_v4_do_rcv will send a reset, but...
370 FIXME: the reset code should be
371 DCCP_RESET_CODE_PACKET_ERROR */
374 static int dccp_rcv_respond_partopen_state_process(struct sock *sk,
376 const struct dccp_hdr *dh,
381 switch (dh->dccph_type) {
383 inet_csk_clear_xmit_timer(sk, ICSK_TIME_DACK);
385 case DCCP_PKT_DATAACK:
388 * FIXME: we should be reseting the PARTOPEN (DELACK) timer
389 * here but only if we haven't used the DELACK timer for
390 * something else, like sending a delayed ack for a TIMESTAMP
391 * echo, etc, for now were not clearing it, sending an extra
392 * ACK when there is nothing else to do in DELACK is not a big
396 /* Stop the PARTOPEN timer */
397 if (sk->sk_state == DCCP_PARTOPEN)
398 inet_csk_clear_xmit_timer(sk, ICSK_TIME_DACK);
400 dccp_sk(sk)->dccps_osr = DCCP_SKB_CB(skb)->dccpd_seq;
401 dccp_set_state(sk, DCCP_OPEN);
403 if (dh->dccph_type == DCCP_PKT_DATAACK) {
404 dccp_rcv_established(sk, skb, dh, len);
405 queued = 1; /* packet was queued
406 (by dccp_rcv_established) */
414 int dccp_rcv_state_process(struct sock *sk, struct sk_buff *skb,
415 struct dccp_hdr *dh, unsigned len)
417 struct dccp_sock *dp = dccp_sk(sk);
418 const int old_state = sk->sk_state;
422 * Step 3: Process LISTEN state
423 * (Continuing from dccp_v4_do_rcv and dccp_v6_do_rcv)
425 * If S.state == LISTEN,
426 * If P.type == Request or P contains a valid Init Cookie
428 * * Must scan the packet's options to check for an Init
429 * Cookie. Only the Init Cookie is processed here,
430 * however; other options are processed in Step 8. This
431 * scan need only be performed if the endpoint uses Init
433 * * Generate a new socket and switch to that socket *
434 * Set S := new socket for this port pair
436 * Choose S.ISS (initial seqno) or set from Init Cookie
437 * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init Cookie
438 * Continue with S.state == RESPOND
439 * * A Response packet will be generated in Step 11 *
441 * Generate Reset(No Connection) unless P.type == Reset
442 * Drop packet and return
444 * NOTE: the check for the packet types is done in
445 * dccp_rcv_state_process
447 if (sk->sk_state == DCCP_LISTEN) {
448 if (dh->dccph_type == DCCP_PKT_REQUEST) {
449 if (dccp_v4_conn_request(sk, skb) < 0)
452 /* FIXME: do congestion control initialization */
455 if (dh->dccph_type == DCCP_PKT_RESET)
458 /* Caller (dccp_v4_do_rcv) will send Reset(No Connection)*/
462 if (sk->sk_state != DCCP_REQUESTING) {
463 if (dccp_check_seqno(sk, skb))
467 * Step 8: Process options and mark acknowledgeable
469 if (dccp_parse_options(sk, skb))
472 if (DCCP_SKB_CB(skb)->dccpd_ack_seq !=
473 DCCP_PKT_WITHOUT_ACK_SEQ)
474 dccp_event_ack_recv(sk, skb);
476 ccid_hc_rx_packet_recv(dp->dccps_hc_rx_ccid, sk, skb);
477 ccid_hc_tx_packet_recv(dp->dccps_hc_tx_ccid, sk, skb);
480 * FIXME: check ECN to see if we should use
481 * DCCP_ACKPKTS_STATE_ECN_MARKED
483 if (dp->dccps_options.dccpo_send_ack_vector) {
484 if (dccp_ackpkts_add(dp->dccps_hc_rx_ackpkts,
485 DCCP_SKB_CB(skb)->dccpd_seq,
486 DCCP_ACKPKTS_STATE_RECEIVED))
489 * FIXME: this activation is probably wrong, have to
490 * study more TCP delack machinery and how it fits into
491 * DCCP draft, but for now it kinda "works" 8)
493 if ((dp->dccps_hc_rx_ackpkts->dccpap_ack_seqno ==
494 DCCP_MAX_SEQNO + 1) &&
495 !inet_csk_ack_scheduled(sk)) {
496 inet_csk_schedule_ack(sk);
497 inet_csk_reset_xmit_timer(sk, ICSK_TIME_DACK,
505 * Step 9: Process Reset
506 * If P.type == Reset,
507 * Tear down connection
508 * S.state := TIMEWAIT
510 * Drop packet and return
512 if (dh->dccph_type == DCCP_PKT_RESET) {
514 * Queue the equivalent of TCP fin so that dccp_recvmsg
518 dccp_time_wait(sk, DCCP_TIME_WAIT, 0);
521 * Step 7: Check for unexpected packet types
522 * If (S.is_server and P.type == CloseReq)
523 * or (S.is_server and P.type == Response)
524 * or (S.is_client and P.type == Request)
525 * or (S.state == RESPOND and P.type == Data),
526 * Send Sync packet acknowledging P.seqno
527 * Drop packet and return
529 } else if ((dp->dccps_role != DCCP_ROLE_CLIENT &&
530 (dh->dccph_type == DCCP_PKT_RESPONSE ||
531 dh->dccph_type == DCCP_PKT_CLOSEREQ)) ||
532 (dp->dccps_role == DCCP_ROLE_CLIENT &&
533 dh->dccph_type == DCCP_PKT_REQUEST) ||
534 (sk->sk_state == DCCP_RESPOND &&
535 dh->dccph_type == DCCP_PKT_DATA)) {
536 dccp_send_sync(sk, DCCP_SKB_CB(skb)->dccpd_seq,
541 switch (sk->sk_state) {
545 case DCCP_REQUESTING:
546 /* FIXME: do congestion control initialization */
548 queued = dccp_rcv_request_sent_state_process(sk, skb, dh, len);
557 queued = dccp_rcv_respond_partopen_state_process(sk, skb,
562 if (dh->dccph_type == DCCP_PKT_ACK ||
563 dh->dccph_type == DCCP_PKT_DATAACK) {
566 sk->sk_state_change(sk);
567 sk_wake_async(sk, 0, POLL_OUT);