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 dccp_v4_send_reset(sk, DCCP_RESET_CODE_CLOSED);
36 dccp_set_state(sk, DCCP_CLOSED);
39 static void dccp_rcv_closereq(struct sock *sk, struct sk_buff *skb)
42 * Step 7: Check for unexpected packet types
43 * If (S.is_server and P.type == CloseReq)
44 * Send Sync packet acknowledging P.seqno
45 * Drop packet and return
47 if (dccp_sk(sk)->dccps_role != DCCP_ROLE_CLIENT) {
48 dccp_send_sync(sk, DCCP_SKB_CB(skb)->dccpd_seq, DCCP_PKT_SYNC);
52 dccp_set_state(sk, DCCP_CLOSING);
53 dccp_send_close(sk, 0);
56 static inline void dccp_event_ack_recv(struct sock *sk, struct sk_buff *skb)
58 struct dccp_sock *dp = dccp_sk(sk);
60 if (dp->dccps_options.dccpo_send_ack_vector)
61 dccp_ackpkts_check_rcv_ackno(dp->dccps_hc_rx_ackpkts, sk,
62 DCCP_SKB_CB(skb)->dccpd_ack_seq);
65 static int dccp_check_seqno(struct sock *sk, struct sk_buff *skb)
67 const struct dccp_hdr *dh = dccp_hdr(skb);
68 struct dccp_sock *dp = dccp_sk(sk);
72 * Step 5: Prepare sequence numbers for Sync
73 * If P.type == Sync or P.type == SyncAck,
74 * If S.AWL <= P.ackno <= S.AWH and P.seqno >= S.SWL,
75 * / * P is valid, so update sequence number variables
76 * accordingly. After this update, P will pass the tests
77 * in Step 6. A SyncAck is generated if necessary in
79 * Update S.GSR, S.SWL, S.SWH
81 * Drop packet and return
83 if (dh->dccph_type == DCCP_PKT_SYNC ||
84 dh->dccph_type == DCCP_PKT_SYNCACK) {
85 if (between48(DCCP_SKB_CB(skb)->dccpd_ack_seq,
86 dp->dccps_awl, dp->dccps_awh) &&
87 !before48(DCCP_SKB_CB(skb)->dccpd_seq, dp->dccps_swl))
88 dccp_update_gsr(sk, DCCP_SKB_CB(skb)->dccpd_seq);
94 * Step 6: Check sequence numbers
95 * Let LSWL = S.SWL and LAWL = S.AWL
96 * If P.type == CloseReq or P.type == Close or P.type == Reset,
97 * LSWL := S.GSR + 1, LAWL := S.GAR
98 * If LSWL <= P.seqno <= S.SWH
99 * and (P.ackno does not exist or LAWL <= P.ackno <= S.AWH),
100 * Update S.GSR, S.SWL, S.SWH
104 * Send Sync packet acknowledging P.seqno
105 * Drop packet and return
107 lswl = dp->dccps_swl;
108 lawl = dp->dccps_awl;
110 if (dh->dccph_type == DCCP_PKT_CLOSEREQ ||
111 dh->dccph_type == DCCP_PKT_CLOSE ||
112 dh->dccph_type == DCCP_PKT_RESET) {
113 lswl = dp->dccps_gsr;
114 dccp_inc_seqno(&lswl);
115 lawl = dp->dccps_gar;
118 if (between48(DCCP_SKB_CB(skb)->dccpd_seq, lswl, dp->dccps_swh) &&
119 (DCCP_SKB_CB(skb)->dccpd_ack_seq == DCCP_PKT_WITHOUT_ACK_SEQ ||
120 between48(DCCP_SKB_CB(skb)->dccpd_ack_seq,
121 lawl, dp->dccps_awh))) {
122 dccp_update_gsr(sk, DCCP_SKB_CB(skb)->dccpd_seq);
124 if (dh->dccph_type != DCCP_PKT_SYNC &&
125 (DCCP_SKB_CB(skb)->dccpd_ack_seq !=
126 DCCP_PKT_WITHOUT_ACK_SEQ))
127 dp->dccps_gar = DCCP_SKB_CB(skb)->dccpd_ack_seq;
129 LIMIT_NETDEBUG(KERN_WARNING "DCCP: Step 6 failed for %s packet, "
130 "(LSWL(%llu) <= P.seqno(%llu) <= S.SWH(%llu)) and "
131 "(P.ackno %s or LAWL(%llu) <= P.ackno(%llu) <= S.AWH(%llu), "
133 dccp_packet_name(dh->dccph_type),
134 (unsigned long long) lswl,
136 DCCP_SKB_CB(skb)->dccpd_seq,
137 (unsigned long long) dp->dccps_swh,
138 (DCCP_SKB_CB(skb)->dccpd_ack_seq ==
139 DCCP_PKT_WITHOUT_ACK_SEQ) ? "doesn't exist" : "exists",
140 (unsigned long long) lawl,
142 DCCP_SKB_CB(skb)->dccpd_ack_seq,
143 (unsigned long long) dp->dccps_awh);
144 dccp_send_sync(sk, DCCP_SKB_CB(skb)->dccpd_seq, DCCP_PKT_SYNC);
151 int dccp_rcv_established(struct sock *sk, struct sk_buff *skb,
152 const struct dccp_hdr *dh, const unsigned len)
154 struct dccp_sock *dp = dccp_sk(sk);
156 if (dccp_check_seqno(sk, skb))
159 if (dccp_parse_options(sk, skb))
162 if (DCCP_SKB_CB(skb)->dccpd_ack_seq != DCCP_PKT_WITHOUT_ACK_SEQ)
163 dccp_event_ack_recv(sk, skb);
166 * FIXME: check ECN to see if we should use
167 * DCCP_ACKPKTS_STATE_ECN_MARKED
169 if (dp->dccps_options.dccpo_send_ack_vector) {
170 struct dccp_ackpkts *ap = dp->dccps_hc_rx_ackpkts;
172 if (dccp_ackpkts_add(dp->dccps_hc_rx_ackpkts,
173 DCCP_SKB_CB(skb)->dccpd_seq,
174 DCCP_ACKPKTS_STATE_RECEIVED)) {
175 LIMIT_NETDEBUG(KERN_WARNING "DCCP: acknowledgeable "
176 "packets buffer full!\n");
177 ap->dccpap_ack_seqno = DCCP_MAX_SEQNO + 1;
178 inet_csk_schedule_ack(sk);
179 inet_csk_reset_xmit_timer(sk, ICSK_TIME_DACK,
186 * FIXME: this activation is probably wrong, have to study more
187 * TCP delack machinery and how it fits into DCCP draft, but
188 * for now it kinda "works" 8)
190 if (!inet_csk_ack_scheduled(sk)) {
191 inet_csk_schedule_ack(sk);
192 inet_csk_reset_xmit_timer(sk, ICSK_TIME_DACK, 5 * HZ,
197 ccid_hc_rx_packet_recv(dp->dccps_hc_rx_ccid, sk, skb);
198 ccid_hc_tx_packet_recv(dp->dccps_hc_tx_ccid, sk, skb);
200 switch (dccp_hdr(skb)->dccph_type) {
201 case DCCP_PKT_DATAACK:
204 * FIXME: check if sk_receive_queue is full, schedule DATA_DROPPED
207 __skb_pull(skb, dh->dccph_doff * 4);
208 __skb_queue_tail(&sk->sk_receive_queue, skb);
209 skb_set_owner_r(skb, sk);
210 sk->sk_data_ready(sk, 0);
216 * Step 9: Process Reset
217 * If P.type == Reset,
218 * Tear down connection
219 * S.state := TIMEWAIT
221 * Drop packet and return
224 dccp_time_wait(sk, DCCP_TIME_WAIT, 0);
226 case DCCP_PKT_CLOSEREQ:
227 dccp_rcv_closereq(sk, skb);
230 dccp_rcv_close(sk, skb);
232 case DCCP_PKT_REQUEST:
234 * or (S.is_server and P.type == Response)
235 * or (S.is_client and P.type == Request)
236 * or (S.state >= OPEN and P.type == Request
237 * and P.seqno >= S.OSR)
238 * or (S.state >= OPEN and P.type == Response
239 * and P.seqno >= S.OSR)
240 * or (S.state == RESPOND and P.type == Data),
241 * Send Sync packet acknowledging P.seqno
242 * Drop packet and return
244 if (dp->dccps_role != DCCP_ROLE_LISTEN)
247 case DCCP_PKT_RESPONSE:
248 if (dp->dccps_role != DCCP_ROLE_CLIENT)
251 if (!before48(DCCP_SKB_CB(skb)->dccpd_seq, dp->dccps_osr)) {
253 dccp_send_sync(sk, DCCP_SKB_CB(skb)->dccpd_seq,
258 dccp_send_sync(sk, DCCP_SKB_CB(skb)->dccpd_seq,
263 * As with DCCP-Ack packets, DCCP-Sync and DCCP-SyncAck packets
264 * MAY have non-zero-length application data areas, whose
265 * contents * receivers MUST ignore.
270 DCCP_INC_STATS_BH(DCCP_MIB_INERRS);
276 static int dccp_rcv_request_sent_state_process(struct sock *sk,
278 const struct dccp_hdr *dh,
282 * Step 4: Prepare sequence numbers in REQUEST
283 * If S.state == REQUEST,
284 * If (P.type == Response or P.type == Reset)
285 * and S.AWL <= P.ackno <= S.AWH,
286 * / * Set sequence number variables corresponding to the
287 * other endpoint, so P will pass the tests in Step 6 * /
288 * Set S.GSR, S.ISR, S.SWL, S.SWH
289 * / * Response processing continues in Step 10; Reset
290 * processing continues in Step 9 * /
292 if (dh->dccph_type == DCCP_PKT_RESPONSE) {
293 const struct inet_connection_sock *icsk = inet_csk(sk);
294 struct dccp_sock *dp = dccp_sk(sk);
296 /* Stop the REQUEST timer */
297 inet_csk_clear_xmit_timer(sk, ICSK_TIME_RETRANS);
298 BUG_TRAP(sk->sk_send_head != NULL);
299 __kfree_skb(sk->sk_send_head);
300 sk->sk_send_head = NULL;
302 if (!between48(DCCP_SKB_CB(skb)->dccpd_ack_seq,
303 dp->dccps_awl, dp->dccps_awh)) {
304 dccp_pr_debug("invalid ackno: S.AWL=%llu, "
305 "P.ackno=%llu, S.AWH=%llu \n",
306 (unsigned long long)dp->dccps_awl,
307 (unsigned long long)DCCP_SKB_CB(skb)->dccpd_ack_seq,
308 (unsigned long long)dp->dccps_awh);
309 goto out_invalid_packet;
312 dp->dccps_isr = DCCP_SKB_CB(skb)->dccpd_seq;
313 dccp_update_gsr(sk, dp->dccps_isr);
315 * SWL and AWL are initially adjusted so that they are not less than
316 * the initial Sequence Numbers received and sent, respectively:
317 * SWL := max(GSR + 1 - floor(W/4), ISR),
318 * AWL := max(GSS - W' + 1, ISS).
319 * These adjustments MUST be applied only at the beginning of the
322 * AWL was adjusted in dccp_v4_connect -acme
324 dccp_set_seqno(&dp->dccps_swl,
325 max48(dp->dccps_swl, dp->dccps_isr));
327 if (ccid_hc_rx_init(dp->dccps_hc_rx_ccid, sk) != 0 ||
328 ccid_hc_tx_init(dp->dccps_hc_tx_ccid, sk) != 0) {
329 ccid_hc_rx_exit(dp->dccps_hc_rx_ccid, sk);
330 ccid_hc_tx_exit(dp->dccps_hc_tx_ccid, sk);
331 /* FIXME: send appropriate RESET code */
332 goto out_invalid_packet;
335 dccp_sync_mss(sk, dp->dccps_pmtu_cookie);
338 * Step 10: Process REQUEST state (second part)
339 * If S.state == REQUEST,
340 * / * If we get here, P is a valid Response from the
341 * server (see Step 4), and we should move to
342 * PARTOPEN state. PARTOPEN means send an Ack,
343 * don't send Data packets, retransmit Acks
344 * periodically, and always include any Init Cookie
345 * from the Response * /
346 * S.state := PARTOPEN
348 * Continue with S.state == PARTOPEN
349 * / * Step 12 will send the Ack completing the
350 * three-way handshake * /
352 dccp_set_state(sk, DCCP_PARTOPEN);
354 /* Make sure socket is routed, for correct metrics. */
355 inet_sk_rebuild_header(sk);
357 if (!sock_flag(sk, SOCK_DEAD)) {
358 sk->sk_state_change(sk);
359 sk_wake_async(sk, 0, POLL_OUT);
362 if (sk->sk_write_pending || icsk->icsk_ack.pingpong ||
363 icsk->icsk_accept_queue.rskq_defer_accept) {
364 /* Save one ACK. Data will be ready after
365 * several ticks, if write_pending is set.
367 * It may be deleted, but with this feature tcpdumps
368 * look so _wonderfully_ clever, that I was not able
369 * to stand against the temptation 8) --ANK
372 * OK, in DCCP we can as well do a similar trick, its
373 * even in the draft, but there is no need for us to
374 * schedule an ack here, as dccp_sendmsg does this for
375 * us, also stated in the draft. -acme
385 return 1; /* dccp_v4_do_rcv will send a reset, but...
386 FIXME: the reset code should be
387 DCCP_RESET_CODE_PACKET_ERROR */
390 static int dccp_rcv_respond_partopen_state_process(struct sock *sk,
392 const struct dccp_hdr *dh,
397 switch (dh->dccph_type) {
399 inet_csk_clear_xmit_timer(sk, ICSK_TIME_DACK);
401 case DCCP_PKT_DATAACK:
404 * FIXME: we should be reseting the PARTOPEN (DELACK) timer
405 * here but only if we haven't used the DELACK timer for
406 * something else, like sending a delayed ack for a TIMESTAMP
407 * echo, etc, for now were not clearing it, sending an extra
408 * ACK when there is nothing else to do in DELACK is not a big
412 /* Stop the PARTOPEN timer */
413 if (sk->sk_state == DCCP_PARTOPEN)
414 inet_csk_clear_xmit_timer(sk, ICSK_TIME_DACK);
416 dccp_sk(sk)->dccps_osr = DCCP_SKB_CB(skb)->dccpd_seq;
417 dccp_set_state(sk, DCCP_OPEN);
419 if (dh->dccph_type == DCCP_PKT_DATAACK) {
420 dccp_rcv_established(sk, skb, dh, len);
421 queued = 1; /* packet was queued
422 (by dccp_rcv_established) */
430 int dccp_rcv_state_process(struct sock *sk, struct sk_buff *skb,
431 struct dccp_hdr *dh, unsigned len)
433 struct dccp_sock *dp = dccp_sk(sk);
434 const int old_state = sk->sk_state;
438 * Step 3: Process LISTEN state
439 * (Continuing from dccp_v4_do_rcv and dccp_v6_do_rcv)
441 * If S.state == LISTEN,
442 * If P.type == Request or P contains a valid Init Cookie
444 * * Must scan the packet's options to check for an Init
445 * Cookie. Only the Init Cookie is processed here,
446 * however; other options are processed in Step 8. This
447 * scan need only be performed if the endpoint uses Init
449 * * Generate a new socket and switch to that socket *
450 * Set S := new socket for this port pair
452 * Choose S.ISS (initial seqno) or set from Init Cookie
453 * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init Cookie
454 * Continue with S.state == RESPOND
455 * * A Response packet will be generated in Step 11 *
457 * Generate Reset(No Connection) unless P.type == Reset
458 * Drop packet and return
460 * NOTE: the check for the packet types is done in
461 * dccp_rcv_state_process
463 if (sk->sk_state == DCCP_LISTEN) {
464 if (dh->dccph_type == DCCP_PKT_REQUEST) {
465 if (dccp_v4_conn_request(sk, skb) < 0)
468 /* FIXME: do congestion control initialization */
471 if (dh->dccph_type == DCCP_PKT_RESET)
474 /* Caller (dccp_v4_do_rcv) will send Reset(No Connection)*/
478 if (sk->sk_state != DCCP_REQUESTING) {
479 if (dccp_check_seqno(sk, skb))
483 * Step 8: Process options and mark acknowledgeable
485 if (dccp_parse_options(sk, skb))
488 if (DCCP_SKB_CB(skb)->dccpd_ack_seq !=
489 DCCP_PKT_WITHOUT_ACK_SEQ)
490 dccp_event_ack_recv(sk, skb);
492 ccid_hc_rx_packet_recv(dp->dccps_hc_rx_ccid, sk, skb);
493 ccid_hc_tx_packet_recv(dp->dccps_hc_tx_ccid, sk, skb);
496 * FIXME: check ECN to see if we should use
497 * DCCP_ACKPKTS_STATE_ECN_MARKED
499 if (dp->dccps_options.dccpo_send_ack_vector) {
500 if (dccp_ackpkts_add(dp->dccps_hc_rx_ackpkts,
501 DCCP_SKB_CB(skb)->dccpd_seq,
502 DCCP_ACKPKTS_STATE_RECEIVED))
505 * FIXME: this activation is probably wrong, have to
506 * study more TCP delack machinery and how it fits into
507 * DCCP draft, but for now it kinda "works" 8)
509 if ((dp->dccps_hc_rx_ackpkts->dccpap_ack_seqno ==
510 DCCP_MAX_SEQNO + 1) &&
511 !inet_csk_ack_scheduled(sk)) {
512 inet_csk_schedule_ack(sk);
513 inet_csk_reset_xmit_timer(sk, ICSK_TIME_DACK,
521 * Step 9: Process Reset
522 * If P.type == Reset,
523 * Tear down connection
524 * S.state := TIMEWAIT
526 * Drop packet and return
528 if (dh->dccph_type == DCCP_PKT_RESET) {
530 * Queue the equivalent of TCP fin so that dccp_recvmsg
534 dccp_time_wait(sk, DCCP_TIME_WAIT, 0);
537 * Step 7: Check for unexpected packet types
538 * If (S.is_server and P.type == CloseReq)
539 * or (S.is_server and P.type == Response)
540 * or (S.is_client and P.type == Request)
541 * or (S.state == RESPOND and P.type == Data),
542 * Send Sync packet acknowledging P.seqno
543 * Drop packet and return
545 } else if ((dp->dccps_role != DCCP_ROLE_CLIENT &&
546 (dh->dccph_type == DCCP_PKT_RESPONSE ||
547 dh->dccph_type == DCCP_PKT_CLOSEREQ)) ||
548 (dp->dccps_role == DCCP_ROLE_CLIENT &&
549 dh->dccph_type == DCCP_PKT_REQUEST) ||
550 (sk->sk_state == DCCP_RESPOND &&
551 dh->dccph_type == DCCP_PKT_DATA)) {
552 dccp_send_sync(sk, DCCP_SKB_CB(skb)->dccpd_seq,
555 } else if (dh->dccph_type == DCCP_PKT_CLOSEREQ) {
556 dccp_rcv_closereq(sk, skb);
558 } else if (dh->dccph_type == DCCP_PKT_CLOSE) {
559 dccp_rcv_close(sk, skb);
563 switch (sk->sk_state) {
567 case DCCP_REQUESTING:
568 /* FIXME: do congestion control initialization */
570 queued = dccp_rcv_request_sent_state_process(sk, skb, dh, len);
579 queued = dccp_rcv_respond_partopen_state_process(sk, skb,
584 if (dh->dccph_type == DCCP_PKT_ACK ||
585 dh->dccph_type == DCCP_PKT_DATAACK) {
588 sk->sk_state_change(sk);
589 sk_wake_async(sk, 0, POLL_OUT);