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