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