Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 1 | /* |
| 2 | * net/dccp/input.c |
| 3 | * |
| 4 | * An implementation of the DCCP protocol |
| 5 | * Arnaldo Carvalho de Melo <acme@conectiva.com.br> |
| 6 | * |
| 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. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/config.h> |
| 14 | #include <linux/dccp.h> |
| 15 | #include <linux/skbuff.h> |
| 16 | |
| 17 | #include <net/sock.h> |
| 18 | |
| 19 | #include "ccid.h" |
| 20 | #include "dccp.h" |
| 21 | |
| 22 | static void dccp_fin(struct sock *sk, struct sk_buff *skb) |
| 23 | { |
| 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); |
| 30 | } |
| 31 | |
| 32 | static void dccp_rcv_close(struct sock *sk, struct sk_buff *skb) |
| 33 | { |
Arnaldo Carvalho de Melo | 7ad07e7 | 2005-08-23 21:50:06 -0700 | [diff] [blame] | 34 | dccp_v4_send_reset(sk, DCCP_RESET_CODE_CLOSED); |
| 35 | dccp_fin(sk, skb); |
| 36 | dccp_set_state(sk, DCCP_CLOSED); |
Arnaldo Carvalho de Melo | 331968b | 2005-08-23 21:54:23 -0700 | [diff] [blame] | 37 | sk_wake_async(sk, 1, POLL_HUP); |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | static void dccp_rcv_closereq(struct sock *sk, struct sk_buff *skb) |
| 41 | { |
| 42 | /* |
| 43 | * Step 7: Check for unexpected packet types |
| 44 | * If (S.is_server and P.type == CloseReq) |
| 45 | * Send Sync packet acknowledging P.seqno |
| 46 | * Drop packet and return |
| 47 | */ |
| 48 | if (dccp_sk(sk)->dccps_role != DCCP_ROLE_CLIENT) { |
Arnaldo Carvalho de Melo | e92ae93 | 2005-08-17 03:10:59 -0300 | [diff] [blame] | 49 | dccp_send_sync(sk, DCCP_SKB_CB(skb)->dccpd_seq, DCCP_PKT_SYNC); |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 50 | return; |
| 51 | } |
| 52 | |
Arnaldo Carvalho de Melo | 811265b | 2005-09-13 19:03:15 -0300 | [diff] [blame] | 53 | if (sk->sk_state != DCCP_CLOSING) |
| 54 | dccp_set_state(sk, DCCP_CLOSING); |
Arnaldo Carvalho de Melo | 7ad07e7 | 2005-08-23 21:50:06 -0700 | [diff] [blame] | 55 | dccp_send_close(sk, 0); |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | static inline void dccp_event_ack_recv(struct sock *sk, struct sk_buff *skb) |
| 59 | { |
| 60 | struct dccp_sock *dp = dccp_sk(sk); |
| 61 | |
| 62 | if (dp->dccps_options.dccpo_send_ack_vector) |
| 63 | dccp_ackpkts_check_rcv_ackno(dp->dccps_hc_rx_ackpkts, sk, |
| 64 | DCCP_SKB_CB(skb)->dccpd_ack_seq); |
| 65 | } |
| 66 | |
| 67 | static int dccp_check_seqno(struct sock *sk, struct sk_buff *skb) |
| 68 | { |
| 69 | const struct dccp_hdr *dh = dccp_hdr(skb); |
| 70 | struct dccp_sock *dp = dccp_sk(sk); |
Arnaldo Carvalho de Melo | e92ae93 | 2005-08-17 03:10:59 -0300 | [diff] [blame] | 71 | u64 lswl, lawl; |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 72 | |
| 73 | /* |
| 74 | * Step 5: Prepare sequence numbers for Sync |
| 75 | * If P.type == Sync or P.type == SyncAck, |
| 76 | * If S.AWL <= P.ackno <= S.AWH and P.seqno >= S.SWL, |
| 77 | * / * P is valid, so update sequence number variables |
| 78 | * accordingly. After this update, P will pass the tests |
| 79 | * in Step 6. A SyncAck is generated if necessary in |
| 80 | * Step 15 * / |
| 81 | * Update S.GSR, S.SWL, S.SWH |
| 82 | * Otherwise, |
| 83 | * Drop packet and return |
| 84 | */ |
| 85 | if (dh->dccph_type == DCCP_PKT_SYNC || |
| 86 | dh->dccph_type == DCCP_PKT_SYNCACK) { |
Arnaldo Carvalho de Melo | 7690af3 | 2005-08-13 20:34:54 -0300 | [diff] [blame] | 87 | if (between48(DCCP_SKB_CB(skb)->dccpd_ack_seq, |
| 88 | dp->dccps_awl, dp->dccps_awh) && |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 89 | !before48(DCCP_SKB_CB(skb)->dccpd_seq, dp->dccps_swl)) |
| 90 | dccp_update_gsr(sk, DCCP_SKB_CB(skb)->dccpd_seq); |
| 91 | else |
| 92 | return -1; |
Arnaldo Carvalho de Melo | e92ae93 | 2005-08-17 03:10:59 -0300 | [diff] [blame] | 93 | } |
| 94 | |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 95 | /* |
| 96 | * Step 6: Check sequence numbers |
| 97 | * Let LSWL = S.SWL and LAWL = S.AWL |
| 98 | * If P.type == CloseReq or P.type == Close or P.type == Reset, |
| 99 | * LSWL := S.GSR + 1, LAWL := S.GAR |
| 100 | * If LSWL <= P.seqno <= S.SWH |
| 101 | * and (P.ackno does not exist or LAWL <= P.ackno <= S.AWH), |
| 102 | * Update S.GSR, S.SWL, S.SWH |
| 103 | * If P.type != Sync, |
| 104 | * Update S.GAR |
| 105 | * Otherwise, |
| 106 | * Send Sync packet acknowledging P.seqno |
| 107 | * Drop packet and return |
| 108 | */ |
Arnaldo Carvalho de Melo | e92ae93 | 2005-08-17 03:10:59 -0300 | [diff] [blame] | 109 | lswl = dp->dccps_swl; |
| 110 | lawl = dp->dccps_awl; |
| 111 | |
| 112 | if (dh->dccph_type == DCCP_PKT_CLOSEREQ || |
Arnaldo Carvalho de Melo | c59eab4 | 2005-08-18 21:12:02 -0300 | [diff] [blame] | 113 | dh->dccph_type == DCCP_PKT_CLOSE || |
| 114 | dh->dccph_type == DCCP_PKT_RESET) { |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 115 | lswl = dp->dccps_gsr; |
| 116 | dccp_inc_seqno(&lswl); |
| 117 | lawl = dp->dccps_gar; |
| 118 | } |
| 119 | |
| 120 | if (between48(DCCP_SKB_CB(skb)->dccpd_seq, lswl, dp->dccps_swh) && |
| 121 | (DCCP_SKB_CB(skb)->dccpd_ack_seq == DCCP_PKT_WITHOUT_ACK_SEQ || |
Arnaldo Carvalho de Melo | 7690af3 | 2005-08-13 20:34:54 -0300 | [diff] [blame] | 122 | between48(DCCP_SKB_CB(skb)->dccpd_ack_seq, |
| 123 | lawl, dp->dccps_awh))) { |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 124 | dccp_update_gsr(sk, DCCP_SKB_CB(skb)->dccpd_seq); |
| 125 | |
| 126 | if (dh->dccph_type != DCCP_PKT_SYNC && |
Arnaldo Carvalho de Melo | 7690af3 | 2005-08-13 20:34:54 -0300 | [diff] [blame] | 127 | (DCCP_SKB_CB(skb)->dccpd_ack_seq != |
| 128 | DCCP_PKT_WITHOUT_ACK_SEQ)) |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 129 | dp->dccps_gar = DCCP_SKB_CB(skb)->dccpd_ack_seq; |
| 130 | } else { |
Arnaldo Carvalho de Melo | a3054d4 | 2005-08-21 05:35:18 -0300 | [diff] [blame] | 131 | LIMIT_NETDEBUG(KERN_WARNING "DCCP: Step 6 failed for %s packet, " |
| 132 | "(LSWL(%llu) <= P.seqno(%llu) <= S.SWH(%llu)) and " |
| 133 | "(P.ackno %s or LAWL(%llu) <= P.ackno(%llu) <= S.AWH(%llu), " |
| 134 | "sending SYNC...\n", |
| 135 | dccp_packet_name(dh->dccph_type), |
David S. Miller | 58e4513 | 2005-08-21 23:46:01 -0700 | [diff] [blame] | 136 | (unsigned long long) lswl, |
| 137 | (unsigned long long) |
| 138 | DCCP_SKB_CB(skb)->dccpd_seq, |
| 139 | (unsigned long long) dp->dccps_swh, |
Arnaldo Carvalho de Melo | a3054d4 | 2005-08-21 05:35:18 -0300 | [diff] [blame] | 140 | (DCCP_SKB_CB(skb)->dccpd_ack_seq == |
| 141 | DCCP_PKT_WITHOUT_ACK_SEQ) ? "doesn't exist" : "exists", |
David S. Miller | 58e4513 | 2005-08-21 23:46:01 -0700 | [diff] [blame] | 142 | (unsigned long long) lawl, |
| 143 | (unsigned long long) |
| 144 | DCCP_SKB_CB(skb)->dccpd_ack_seq, |
| 145 | (unsigned long long) dp->dccps_awh); |
Arnaldo Carvalho de Melo | e92ae93 | 2005-08-17 03:10:59 -0300 | [diff] [blame] | 146 | dccp_send_sync(sk, DCCP_SKB_CB(skb)->dccpd_seq, DCCP_PKT_SYNC); |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 147 | return -1; |
| 148 | } |
| 149 | |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | int dccp_rcv_established(struct sock *sk, struct sk_buff *skb, |
| 154 | const struct dccp_hdr *dh, const unsigned len) |
| 155 | { |
| 156 | struct dccp_sock *dp = dccp_sk(sk); |
| 157 | |
| 158 | if (dccp_check_seqno(sk, skb)) |
| 159 | goto discard; |
| 160 | |
| 161 | if (dccp_parse_options(sk, skb)) |
| 162 | goto discard; |
| 163 | |
| 164 | if (DCCP_SKB_CB(skb)->dccpd_ack_seq != DCCP_PKT_WITHOUT_ACK_SEQ) |
| 165 | dccp_event_ack_recv(sk, skb); |
| 166 | |
| 167 | /* |
| 168 | * FIXME: check ECN to see if we should use |
| 169 | * DCCP_ACKPKTS_STATE_ECN_MARKED |
| 170 | */ |
| 171 | if (dp->dccps_options.dccpo_send_ack_vector) { |
| 172 | struct dccp_ackpkts *ap = dp->dccps_hc_rx_ackpkts; |
| 173 | |
Arnaldo Carvalho de Melo | b0e5678 | 2005-09-09 02:38:35 -0300 | [diff] [blame] | 174 | if (dccp_ackpkts_add(dp->dccps_hc_rx_ackpkts, sk, |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 175 | DCCP_SKB_CB(skb)->dccpd_seq, |
| 176 | DCCP_ACKPKTS_STATE_RECEIVED)) { |
Arnaldo Carvalho de Melo | c59eab4 | 2005-08-18 21:12:02 -0300 | [diff] [blame] | 177 | LIMIT_NETDEBUG(KERN_WARNING "DCCP: acknowledgeable " |
| 178 | "packets buffer full!\n"); |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 179 | ap->dccpap_ack_seqno = DCCP_MAX_SEQNO + 1; |
| 180 | inet_csk_schedule_ack(sk); |
Arnaldo Carvalho de Melo | 7690af3 | 2005-08-13 20:34:54 -0300 | [diff] [blame] | 181 | inet_csk_reset_xmit_timer(sk, ICSK_TIME_DACK, |
| 182 | TCP_DELACK_MIN, |
| 183 | DCCP_RTO_MAX); |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 184 | goto discard; |
| 185 | } |
| 186 | |
| 187 | /* |
| 188 | * FIXME: this activation is probably wrong, have to study more |
| 189 | * TCP delack machinery and how it fits into DCCP draft, but |
| 190 | * for now it kinda "works" 8) |
| 191 | */ |
| 192 | if (!inet_csk_ack_scheduled(sk)) { |
| 193 | inet_csk_schedule_ack(sk); |
Arnaldo Carvalho de Melo | 7690af3 | 2005-08-13 20:34:54 -0300 | [diff] [blame] | 194 | inet_csk_reset_xmit_timer(sk, ICSK_TIME_DACK, 5 * HZ, |
| 195 | DCCP_RTO_MAX); |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | |
| 199 | ccid_hc_rx_packet_recv(dp->dccps_hc_rx_ccid, sk, skb); |
| 200 | ccid_hc_tx_packet_recv(dp->dccps_hc_tx_ccid, sk, skb); |
| 201 | |
| 202 | switch (dccp_hdr(skb)->dccph_type) { |
| 203 | case DCCP_PKT_DATAACK: |
| 204 | case DCCP_PKT_DATA: |
| 205 | /* |
Arnaldo Carvalho de Melo | 7690af3 | 2005-08-13 20:34:54 -0300 | [diff] [blame] | 206 | * FIXME: check if sk_receive_queue is full, schedule DATA_DROPPED |
| 207 | * option if it is. |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 208 | */ |
| 209 | __skb_pull(skb, dh->dccph_doff * 4); |
| 210 | __skb_queue_tail(&sk->sk_receive_queue, skb); |
| 211 | skb_set_owner_r(skb, sk); |
| 212 | sk->sk_data_ready(sk, 0); |
| 213 | return 0; |
| 214 | case DCCP_PKT_ACK: |
| 215 | goto discard; |
| 216 | case DCCP_PKT_RESET: |
| 217 | /* |
| 218 | * Step 9: Process Reset |
| 219 | * If P.type == Reset, |
| 220 | * Tear down connection |
| 221 | * S.state := TIMEWAIT |
| 222 | * Set TIMEWAIT timer |
| 223 | * Drop packet and return |
| 224 | */ |
| 225 | dccp_fin(sk, skb); |
| 226 | dccp_time_wait(sk, DCCP_TIME_WAIT, 0); |
| 227 | return 0; |
| 228 | case DCCP_PKT_CLOSEREQ: |
| 229 | dccp_rcv_closereq(sk, skb); |
| 230 | goto discard; |
| 231 | case DCCP_PKT_CLOSE: |
| 232 | dccp_rcv_close(sk, skb); |
| 233 | return 0; |
| 234 | case DCCP_PKT_REQUEST: |
| 235 | /* Step 7 |
| 236 | * or (S.is_server and P.type == Response) |
| 237 | * or (S.is_client and P.type == Request) |
| 238 | * or (S.state >= OPEN and P.type == Request |
| 239 | * and P.seqno >= S.OSR) |
| 240 | * or (S.state >= OPEN and P.type == Response |
| 241 | * and P.seqno >= S.OSR) |
| 242 | * or (S.state == RESPOND and P.type == Data), |
| 243 | * Send Sync packet acknowledging P.seqno |
| 244 | * Drop packet and return |
| 245 | */ |
| 246 | if (dp->dccps_role != DCCP_ROLE_LISTEN) |
| 247 | goto send_sync; |
| 248 | goto check_seq; |
| 249 | case DCCP_PKT_RESPONSE: |
| 250 | if (dp->dccps_role != DCCP_ROLE_CLIENT) |
| 251 | goto send_sync; |
| 252 | check_seq: |
| 253 | if (!before48(DCCP_SKB_CB(skb)->dccpd_seq, dp->dccps_osr)) { |
| 254 | send_sync: |
Arnaldo Carvalho de Melo | e92ae93 | 2005-08-17 03:10:59 -0300 | [diff] [blame] | 255 | dccp_send_sync(sk, DCCP_SKB_CB(skb)->dccpd_seq, |
| 256 | DCCP_PKT_SYNC); |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 257 | } |
| 258 | break; |
Arnaldo Carvalho de Melo | e92ae93 | 2005-08-17 03:10:59 -0300 | [diff] [blame] | 259 | case DCCP_PKT_SYNC: |
| 260 | dccp_send_sync(sk, DCCP_SKB_CB(skb)->dccpd_seq, |
| 261 | DCCP_PKT_SYNCACK); |
| 262 | /* |
| 263 | * From the draft: |
| 264 | * |
| 265 | * As with DCCP-Ack packets, DCCP-Sync and DCCP-SyncAck packets |
| 266 | * MAY have non-zero-length application data areas, whose |
| 267 | * contents * receivers MUST ignore. |
| 268 | */ |
| 269 | goto discard; |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | DCCP_INC_STATS_BH(DCCP_MIB_INERRS); |
| 273 | discard: |
| 274 | __kfree_skb(skb); |
| 275 | return 0; |
| 276 | } |
| 277 | |
| 278 | static int dccp_rcv_request_sent_state_process(struct sock *sk, |
| 279 | struct sk_buff *skb, |
| 280 | const struct dccp_hdr *dh, |
| 281 | const unsigned len) |
| 282 | { |
| 283 | /* |
| 284 | * Step 4: Prepare sequence numbers in REQUEST |
| 285 | * If S.state == REQUEST, |
| 286 | * If (P.type == Response or P.type == Reset) |
| 287 | * and S.AWL <= P.ackno <= S.AWH, |
| 288 | * / * Set sequence number variables corresponding to the |
| 289 | * other endpoint, so P will pass the tests in Step 6 * / |
| 290 | * Set S.GSR, S.ISR, S.SWL, S.SWH |
| 291 | * / * Response processing continues in Step 10; Reset |
| 292 | * processing continues in Step 9 * / |
| 293 | */ |
| 294 | if (dh->dccph_type == DCCP_PKT_RESPONSE) { |
| 295 | const struct inet_connection_sock *icsk = inet_csk(sk); |
| 296 | struct dccp_sock *dp = dccp_sk(sk); |
| 297 | |
| 298 | /* Stop the REQUEST timer */ |
| 299 | inet_csk_clear_xmit_timer(sk, ICSK_TIME_RETRANS); |
| 300 | BUG_TRAP(sk->sk_send_head != NULL); |
| 301 | __kfree_skb(sk->sk_send_head); |
| 302 | sk->sk_send_head = NULL; |
| 303 | |
Arnaldo Carvalho de Melo | 7690af3 | 2005-08-13 20:34:54 -0300 | [diff] [blame] | 304 | if (!between48(DCCP_SKB_CB(skb)->dccpd_ack_seq, |
| 305 | dp->dccps_awl, dp->dccps_awh)) { |
| 306 | dccp_pr_debug("invalid ackno: S.AWL=%llu, " |
| 307 | "P.ackno=%llu, S.AWH=%llu \n", |
| 308 | (unsigned long long)dp->dccps_awl, |
| 309 | (unsigned long long)DCCP_SKB_CB(skb)->dccpd_ack_seq, |
| 310 | (unsigned long long)dp->dccps_awh); |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 311 | goto out_invalid_packet; |
| 312 | } |
| 313 | |
| 314 | dp->dccps_isr = DCCP_SKB_CB(skb)->dccpd_seq; |
Arnaldo Carvalho de Melo | 03ace39 | 2005-08-21 05:36:45 -0300 | [diff] [blame] | 315 | dccp_update_gsr(sk, dp->dccps_isr); |
| 316 | /* |
| 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 |
| 322 | * connection. |
| 323 | * |
| 324 | * AWL was adjusted in dccp_v4_connect -acme |
| 325 | */ |
| 326 | dccp_set_seqno(&dp->dccps_swl, |
| 327 | max48(dp->dccps_swl, dp->dccps_isr)); |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 328 | |
| 329 | if (ccid_hc_rx_init(dp->dccps_hc_rx_ccid, sk) != 0 || |
| 330 | ccid_hc_tx_init(dp->dccps_hc_tx_ccid, sk) != 0) { |
| 331 | ccid_hc_rx_exit(dp->dccps_hc_rx_ccid, sk); |
| 332 | ccid_hc_tx_exit(dp->dccps_hc_tx_ccid, sk); |
| 333 | /* FIXME: send appropriate RESET code */ |
| 334 | goto out_invalid_packet; |
| 335 | } |
| 336 | |
| 337 | dccp_sync_mss(sk, dp->dccps_pmtu_cookie); |
| 338 | |
| 339 | /* |
| 340 | * Step 10: Process REQUEST state (second part) |
| 341 | * If S.state == REQUEST, |
Arnaldo Carvalho de Melo | 7690af3 | 2005-08-13 20:34:54 -0300 | [diff] [blame] | 342 | * / * If we get here, P is a valid Response from the |
| 343 | * server (see Step 4), and we should move to |
| 344 | * PARTOPEN state. PARTOPEN means send an Ack, |
| 345 | * don't send Data packets, retransmit Acks |
| 346 | * periodically, and always include any Init Cookie |
| 347 | * from the Response * / |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 348 | * S.state := PARTOPEN |
| 349 | * Set PARTOPEN timer |
| 350 | * Continue with S.state == PARTOPEN |
Arnaldo Carvalho de Melo | 7690af3 | 2005-08-13 20:34:54 -0300 | [diff] [blame] | 351 | * / * Step 12 will send the Ack completing the |
| 352 | * three-way handshake * / |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 353 | */ |
| 354 | dccp_set_state(sk, DCCP_PARTOPEN); |
| 355 | |
| 356 | /* Make sure socket is routed, for correct metrics. */ |
| 357 | inet_sk_rebuild_header(sk); |
| 358 | |
| 359 | if (!sock_flag(sk, SOCK_DEAD)) { |
| 360 | sk->sk_state_change(sk); |
| 361 | sk_wake_async(sk, 0, POLL_OUT); |
| 362 | } |
| 363 | |
| 364 | if (sk->sk_write_pending || icsk->icsk_ack.pingpong || |
| 365 | icsk->icsk_accept_queue.rskq_defer_accept) { |
| 366 | /* Save one ACK. Data will be ready after |
| 367 | * several ticks, if write_pending is set. |
| 368 | * |
| 369 | * It may be deleted, but with this feature tcpdumps |
| 370 | * look so _wonderfully_ clever, that I was not able |
| 371 | * to stand against the temptation 8) --ANK |
| 372 | */ |
| 373 | /* |
| 374 | * OK, in DCCP we can as well do a similar trick, its |
| 375 | * even in the draft, but there is no need for us to |
| 376 | * schedule an ack here, as dccp_sendmsg does this for |
| 377 | * us, also stated in the draft. -acme |
| 378 | */ |
| 379 | __kfree_skb(skb); |
| 380 | return 0; |
| 381 | } |
| 382 | dccp_send_ack(sk); |
| 383 | return -1; |
| 384 | } |
| 385 | |
| 386 | out_invalid_packet: |
Arnaldo Carvalho de Melo | 0c10c5d | 2005-09-16 16:58:33 -0700 | [diff] [blame^] | 387 | /* dccp_v4_do_rcv will send a reset */ |
| 388 | DCCP_SKB_CB(skb)->dccpd_reset_code = DCCP_RESET_CODE_PACKET_ERROR; |
| 389 | return 1; |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | static int dccp_rcv_respond_partopen_state_process(struct sock *sk, |
| 393 | struct sk_buff *skb, |
| 394 | const struct dccp_hdr *dh, |
| 395 | const unsigned len) |
| 396 | { |
| 397 | int queued = 0; |
| 398 | |
| 399 | switch (dh->dccph_type) { |
| 400 | case DCCP_PKT_RESET: |
| 401 | inet_csk_clear_xmit_timer(sk, ICSK_TIME_DACK); |
| 402 | break; |
| 403 | case DCCP_PKT_DATAACK: |
| 404 | case DCCP_PKT_ACK: |
| 405 | /* |
Arnaldo Carvalho de Melo | 7690af3 | 2005-08-13 20:34:54 -0300 | [diff] [blame] | 406 | * FIXME: we should be reseting the PARTOPEN (DELACK) timer |
| 407 | * here but only if we haven't used the DELACK timer for |
| 408 | * something else, like sending a delayed ack for a TIMESTAMP |
| 409 | * echo, etc, for now were not clearing it, sending an extra |
| 410 | * ACK when there is nothing else to do in DELACK is not a big |
| 411 | * deal after all. |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 412 | */ |
| 413 | |
| 414 | /* Stop the PARTOPEN timer */ |
| 415 | if (sk->sk_state == DCCP_PARTOPEN) |
| 416 | inet_csk_clear_xmit_timer(sk, ICSK_TIME_DACK); |
| 417 | |
| 418 | dccp_sk(sk)->dccps_osr = DCCP_SKB_CB(skb)->dccpd_seq; |
| 419 | dccp_set_state(sk, DCCP_OPEN); |
| 420 | |
| 421 | if (dh->dccph_type == DCCP_PKT_DATAACK) { |
| 422 | dccp_rcv_established(sk, skb, dh, len); |
Arnaldo Carvalho de Melo | 7690af3 | 2005-08-13 20:34:54 -0300 | [diff] [blame] | 423 | queued = 1; /* packet was queued |
| 424 | (by dccp_rcv_established) */ |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 425 | } |
| 426 | break; |
| 427 | } |
| 428 | |
| 429 | return queued; |
| 430 | } |
| 431 | |
| 432 | int dccp_rcv_state_process(struct sock *sk, struct sk_buff *skb, |
| 433 | struct dccp_hdr *dh, unsigned len) |
| 434 | { |
| 435 | struct dccp_sock *dp = dccp_sk(sk); |
Arnaldo Carvalho de Melo | 0c10c5d | 2005-09-16 16:58:33 -0700 | [diff] [blame^] | 436 | struct dccp_skb_cb *dcb = DCCP_SKB_CB(skb); |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 437 | const int old_state = sk->sk_state; |
| 438 | int queued = 0; |
| 439 | |
Arnaldo Carvalho de Melo | 8649b0d | 2005-08-13 20:36:01 -0300 | [diff] [blame] | 440 | /* |
| 441 | * Step 3: Process LISTEN state |
| 442 | * (Continuing from dccp_v4_do_rcv and dccp_v6_do_rcv) |
| 443 | * |
| 444 | * If S.state == LISTEN, |
| 445 | * If P.type == Request or P contains a valid Init Cookie |
| 446 | * option, |
| 447 | * * Must scan the packet's options to check for an Init |
| 448 | * Cookie. Only the Init Cookie is processed here, |
| 449 | * however; other options are processed in Step 8. This |
| 450 | * scan need only be performed if the endpoint uses Init |
| 451 | * Cookies * |
| 452 | * * Generate a new socket and switch to that socket * |
| 453 | * Set S := new socket for this port pair |
| 454 | * S.state = RESPOND |
| 455 | * Choose S.ISS (initial seqno) or set from Init Cookie |
| 456 | * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init Cookie |
| 457 | * Continue with S.state == RESPOND |
| 458 | * * A Response packet will be generated in Step 11 * |
| 459 | * Otherwise, |
| 460 | * Generate Reset(No Connection) unless P.type == Reset |
| 461 | * Drop packet and return |
| 462 | * |
| 463 | * NOTE: the check for the packet types is done in |
| 464 | * dccp_rcv_state_process |
| 465 | */ |
| 466 | if (sk->sk_state == DCCP_LISTEN) { |
| 467 | if (dh->dccph_type == DCCP_PKT_REQUEST) { |
| 468 | if (dccp_v4_conn_request(sk, skb) < 0) |
| 469 | return 1; |
| 470 | |
| 471 | /* FIXME: do congestion control initialization */ |
| 472 | goto discard; |
| 473 | } |
| 474 | if (dh->dccph_type == DCCP_PKT_RESET) |
| 475 | goto discard; |
| 476 | |
Arnaldo Carvalho de Melo | 0c10c5d | 2005-09-16 16:58:33 -0700 | [diff] [blame^] | 477 | /* Caller (dccp_v4_do_rcv) will send Reset */ |
| 478 | dcb->dccpd_reset_code = DCCP_RESET_CODE_NO_CONNECTION; |
Arnaldo Carvalho de Melo | 8649b0d | 2005-08-13 20:36:01 -0300 | [diff] [blame] | 479 | return 1; |
| 480 | } |
| 481 | |
| 482 | if (sk->sk_state != DCCP_REQUESTING) { |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 483 | if (dccp_check_seqno(sk, skb)) |
| 484 | goto discard; |
| 485 | |
| 486 | /* |
| 487 | * Step 8: Process options and mark acknowledgeable |
| 488 | */ |
| 489 | if (dccp_parse_options(sk, skb)) |
| 490 | goto discard; |
| 491 | |
Arnaldo Carvalho de Melo | 0c10c5d | 2005-09-16 16:58:33 -0700 | [diff] [blame^] | 492 | if (dcb->dccpd_ack_seq != DCCP_PKT_WITHOUT_ACK_SEQ) |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 493 | dccp_event_ack_recv(sk, skb); |
| 494 | |
| 495 | ccid_hc_rx_packet_recv(dp->dccps_hc_rx_ccid, sk, skb); |
| 496 | ccid_hc_tx_packet_recv(dp->dccps_hc_tx_ccid, sk, skb); |
| 497 | |
| 498 | /* |
| 499 | * FIXME: check ECN to see if we should use |
| 500 | * DCCP_ACKPKTS_STATE_ECN_MARKED |
| 501 | */ |
| 502 | if (dp->dccps_options.dccpo_send_ack_vector) { |
Arnaldo Carvalho de Melo | b0e5678 | 2005-09-09 02:38:35 -0300 | [diff] [blame] | 503 | if (dccp_ackpkts_add(dp->dccps_hc_rx_ackpkts, sk, |
Arnaldo Carvalho de Melo | 0c10c5d | 2005-09-16 16:58:33 -0700 | [diff] [blame^] | 504 | dcb->dccpd_seq, |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 505 | DCCP_ACKPKTS_STATE_RECEIVED)) |
| 506 | goto discard; |
| 507 | /* |
Arnaldo Carvalho de Melo | 7690af3 | 2005-08-13 20:34:54 -0300 | [diff] [blame] | 508 | * FIXME: this activation is probably wrong, have to |
| 509 | * study more TCP delack machinery and how it fits into |
| 510 | * DCCP draft, but for now it kinda "works" 8) |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 511 | */ |
Arnaldo Carvalho de Melo | 7690af3 | 2005-08-13 20:34:54 -0300 | [diff] [blame] | 512 | if ((dp->dccps_hc_rx_ackpkts->dccpap_ack_seqno == |
| 513 | DCCP_MAX_SEQNO + 1) && |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 514 | !inet_csk_ack_scheduled(sk)) { |
| 515 | inet_csk_schedule_ack(sk); |
Arnaldo Carvalho de Melo | 7690af3 | 2005-08-13 20:34:54 -0300 | [diff] [blame] | 516 | inet_csk_reset_xmit_timer(sk, ICSK_TIME_DACK, |
| 517 | TCP_DELACK_MIN, |
| 518 | DCCP_RTO_MAX); |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 519 | } |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | /* |
| 524 | * Step 9: Process Reset |
| 525 | * If P.type == Reset, |
| 526 | * Tear down connection |
| 527 | * S.state := TIMEWAIT |
| 528 | * Set TIMEWAIT timer |
| 529 | * Drop packet and return |
| 530 | */ |
| 531 | if (dh->dccph_type == DCCP_PKT_RESET) { |
Arnaldo Carvalho de Melo | 7690af3 | 2005-08-13 20:34:54 -0300 | [diff] [blame] | 532 | /* |
| 533 | * Queue the equivalent of TCP fin so that dccp_recvmsg |
| 534 | * exits the loop |
| 535 | */ |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 536 | dccp_fin(sk, skb); |
| 537 | dccp_time_wait(sk, DCCP_TIME_WAIT, 0); |
| 538 | return 0; |
| 539 | /* |
| 540 | * Step 7: Check for unexpected packet types |
| 541 | * If (S.is_server and P.type == CloseReq) |
| 542 | * or (S.is_server and P.type == Response) |
| 543 | * or (S.is_client and P.type == Request) |
| 544 | * or (S.state == RESPOND and P.type == Data), |
| 545 | * Send Sync packet acknowledging P.seqno |
| 546 | * Drop packet and return |
| 547 | */ |
| 548 | } else if ((dp->dccps_role != DCCP_ROLE_CLIENT && |
Arnaldo Carvalho de Melo | 7690af3 | 2005-08-13 20:34:54 -0300 | [diff] [blame] | 549 | (dh->dccph_type == DCCP_PKT_RESPONSE || |
| 550 | dh->dccph_type == DCCP_PKT_CLOSEREQ)) || |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 551 | (dp->dccps_role == DCCP_ROLE_CLIENT && |
| 552 | dh->dccph_type == DCCP_PKT_REQUEST) || |
Arnaldo Carvalho de Melo | 7690af3 | 2005-08-13 20:34:54 -0300 | [diff] [blame] | 553 | (sk->sk_state == DCCP_RESPOND && |
| 554 | dh->dccph_type == DCCP_PKT_DATA)) { |
Arnaldo Carvalho de Melo | 0c10c5d | 2005-09-16 16:58:33 -0700 | [diff] [blame^] | 555 | dccp_send_sync(sk, dcb->dccpd_seq, DCCP_PKT_SYNC); |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 556 | goto discard; |
Arnaldo Carvalho de Melo | 7ad07e7 | 2005-08-23 21:50:06 -0700 | [diff] [blame] | 557 | } else if (dh->dccph_type == DCCP_PKT_CLOSEREQ) { |
| 558 | dccp_rcv_closereq(sk, skb); |
| 559 | goto discard; |
| 560 | } else if (dh->dccph_type == DCCP_PKT_CLOSE) { |
| 561 | dccp_rcv_close(sk, skb); |
| 562 | return 0; |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 563 | } |
| 564 | |
Arnaldo Carvalho de Melo | 2b80230 | 2005-09-13 19:05:08 -0300 | [diff] [blame] | 565 | if (unlikely(dh->dccph_type == DCCP_PKT_SYNC)) { |
Arnaldo Carvalho de Melo | 0c10c5d | 2005-09-16 16:58:33 -0700 | [diff] [blame^] | 566 | dccp_send_sync(sk, dcb->dccpd_seq, DCCP_PKT_SYNCACK); |
Arnaldo Carvalho de Melo | 2b80230 | 2005-09-13 19:05:08 -0300 | [diff] [blame] | 567 | goto discard; |
| 568 | } |
| 569 | |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 570 | switch (sk->sk_state) { |
| 571 | case DCCP_CLOSED: |
Arnaldo Carvalho de Melo | 0c10c5d | 2005-09-16 16:58:33 -0700 | [diff] [blame^] | 572 | dcb->dccpd_reset_code = DCCP_RESET_CODE_NO_CONNECTION; |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 573 | return 1; |
| 574 | |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 575 | case DCCP_REQUESTING: |
| 576 | /* FIXME: do congestion control initialization */ |
| 577 | |
| 578 | queued = dccp_rcv_request_sent_state_process(sk, skb, dh, len); |
| 579 | if (queued >= 0) |
| 580 | return queued; |
| 581 | |
| 582 | __kfree_skb(skb); |
| 583 | return 0; |
| 584 | |
| 585 | case DCCP_RESPOND: |
| 586 | case DCCP_PARTOPEN: |
Arnaldo Carvalho de Melo | 7690af3 | 2005-08-13 20:34:54 -0300 | [diff] [blame] | 587 | queued = dccp_rcv_respond_partopen_state_process(sk, skb, |
| 588 | dh, len); |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 589 | break; |
| 590 | } |
| 591 | |
Arnaldo Carvalho de Melo | 7690af3 | 2005-08-13 20:34:54 -0300 | [diff] [blame] | 592 | if (dh->dccph_type == DCCP_PKT_ACK || |
| 593 | dh->dccph_type == DCCP_PKT_DATAACK) { |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 594 | switch (old_state) { |
| 595 | case DCCP_PARTOPEN: |
| 596 | sk->sk_state_change(sk); |
| 597 | sk_wake_async(sk, 0, POLL_OUT); |
| 598 | break; |
| 599 | } |
| 600 | } |
| 601 | |
| 602 | if (!queued) { |
| 603 | discard: |
| 604 | __kfree_skb(skb); |
| 605 | } |
| 606 | return 0; |
| 607 | } |