[TCP]: MD5 Signature Option (RFC2385) support.
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / net / dccp / input.c
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/dccp.h>
14 #include <linux/skbuff.h>
15
16 #include <net/sock.h>
17
18 #include "ackvec.h"
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 {
34 dccp_send_reset(sk, DCCP_RESET_CODE_CLOSED);
35 dccp_fin(sk, skb);
36 dccp_set_state(sk, DCCP_CLOSED);
37 sk_wake_async(sk, 1, POLL_HUP);
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) {
49 dccp_send_sync(sk, DCCP_SKB_CB(skb)->dccpd_seq, DCCP_PKT_SYNC);
50 return;
51 }
52
53 if (sk->sk_state != DCCP_CLOSING)
54 dccp_set_state(sk, DCCP_CLOSING);
55 dccp_send_close(sk, 0);
56 }
57
58 static void dccp_event_ack_recv(struct sock *sk, struct sk_buff *skb)
59 {
60 struct dccp_sock *dp = dccp_sk(sk);
61
62 if (dccp_msk(sk)->dccpms_send_ack_vector)
63 dccp_ackvec_check_rcv_ackno(dp->dccps_hc_rx_ackvec, 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);
71 u64 lswl, lawl;
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) {
87 if (between48(DCCP_SKB_CB(skb)->dccpd_ack_seq,
88 dp->dccps_awl, dp->dccps_awh) &&
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;
93 }
94
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 */
109 lswl = dp->dccps_swl;
110 lawl = dp->dccps_awl;
111
112 if (dh->dccph_type == DCCP_PKT_CLOSEREQ ||
113 dh->dccph_type == DCCP_PKT_CLOSE ||
114 dh->dccph_type == DCCP_PKT_RESET) {
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 ||
122 between48(DCCP_SKB_CB(skb)->dccpd_ack_seq,
123 lawl, dp->dccps_awh))) {
124 dccp_update_gsr(sk, DCCP_SKB_CB(skb)->dccpd_seq);
125
126 if (dh->dccph_type != DCCP_PKT_SYNC &&
127 (DCCP_SKB_CB(skb)->dccpd_ack_seq !=
128 DCCP_PKT_WITHOUT_ACK_SEQ))
129 dp->dccps_gar = DCCP_SKB_CB(skb)->dccpd_ack_seq;
130 } else {
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),
136 (unsigned long long) lswl,
137 (unsigned long long)
138 DCCP_SKB_CB(skb)->dccpd_seq,
139 (unsigned long long) dp->dccps_swh,
140 (DCCP_SKB_CB(skb)->dccpd_ack_seq ==
141 DCCP_PKT_WITHOUT_ACK_SEQ) ? "doesn't exist" : "exists",
142 (unsigned long long) lawl,
143 (unsigned long long)
144 DCCP_SKB_CB(skb)->dccpd_ack_seq,
145 (unsigned long long) dp->dccps_awh);
146 dccp_send_sync(sk, DCCP_SKB_CB(skb)->dccpd_seq, DCCP_PKT_SYNC);
147 return -1;
148 }
149
150 return 0;
151 }
152
153 static 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 switch (dccp_hdr(skb)->dccph_type) {
159 case DCCP_PKT_DATAACK:
160 case DCCP_PKT_DATA:
161 /*
162 * FIXME: check if sk_receive_queue is full, schedule DATA_DROPPED
163 * option if it is.
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:
211 dccp_send_sync(sk, DCCP_SKB_CB(skb)->dccpd_seq,
212 DCCP_PKT_SYNC);
213 }
214 break;
215 case DCCP_PKT_SYNC:
216 dccp_send_sync(sk, DCCP_SKB_CB(skb)->dccpd_seq,
217 DCCP_PKT_SYNCACK);
218 /*
219 * From RFC 4340, sec. 5.7
220 *
221 * As with DCCP-Ack packets, DCCP-Sync and DCCP-SyncAck packets
222 * MAY have non-zero-length application data areas, whose
223 * contents receivers MUST ignore.
224 */
225 goto discard;
226 }
227
228 DCCP_INC_STATS_BH(DCCP_MIB_INERRS);
229 discard:
230 __kfree_skb(skb);
231 return 0;
232 }
233
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
248 if (dccp_msk(sk)->dccpms_send_ack_vector &&
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
263 EXPORT_SYMBOL_GPL(dccp_rcv_established);
264
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
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);
298 goto out_invalid_packet;
299 }
300
301 if (dccp_parse_options(sk, skb))
302 goto out_invalid_packet;
303
304 if (dccp_msk(sk)->dccpms_send_ack_vector &&
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
310 dp->dccps_isr = DCCP_SKB_CB(skb)->dccpd_seq;
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));
324
325 dccp_sync_mss(sk, icsk->icsk_pmtu_cookie);
326
327 /*
328 * Step 10: Process REQUEST state (second part)
329 * If S.state == REQUEST,
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 * /
336 * S.state := PARTOPEN
337 * Set PARTOPEN timer
338 * Continue with S.state == PARTOPEN
339 * / * Step 12 will send the Ack completing the
340 * three-way handshake * /
341 */
342 dccp_set_state(sk, DCCP_PARTOPEN);
343
344 /* Make sure socket is routed, for correct metrics. */
345 icsk->icsk_af_ops->rebuild_header(sk);
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:
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;
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;
391 case DCCP_PKT_DATA:
392 if (sk->sk_state == DCCP_RESPOND)
393 break;
394 case DCCP_PKT_DATAACK:
395 case DCCP_PKT_ACK:
396 /*
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.
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
412 if (dh->dccph_type == DCCP_PKT_DATAACK ||
413 dh->dccph_type == DCCP_PKT_DATA) {
414 __dccp_rcv_established(sk, skb, dh, len);
415 queued = 1; /* packet was queued
416 (by __dccp_rcv_established) */
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);
428 struct dccp_skb_cb *dcb = DCCP_SKB_CB(skb);
429 const int old_state = sk->sk_state;
430 int queued = 0;
431
432 /*
433 * Step 3: Process LISTEN state
434 *
435 * If S.state == LISTEN,
436 * If P.type == Request or P contains a valid Init Cookie option,
437 * (* Must scan the packet's options to check for Init
438 * Cookies. Only Init Cookies are processed here,
439 * however; other options are processed in Step 8. This
440 * scan need only be performed if the endpoint uses Init
441 * Cookies *)
442 * (* Generate a new socket and switch to that socket *)
443 * Set S := new socket for this port pair
444 * S.state = RESPOND
445 * Choose S.ISS (initial seqno) or set from Init Cookies
446 * Initialize S.GAR := S.ISS
447 * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init
448 * Cookies Continue with S.state == RESPOND
449 * (* A Response packet will be generated in Step 11 *)
450 * Otherwise,
451 * Generate Reset(No Connection) unless P.type == Reset
452 * Drop packet and return
453 */
454 if (sk->sk_state == DCCP_LISTEN) {
455 if (dh->dccph_type == DCCP_PKT_REQUEST) {
456 if (inet_csk(sk)->icsk_af_ops->conn_request(sk,
457 skb) < 0)
458 return 1;
459
460 /* FIXME: do congestion control initialization */
461 goto discard;
462 }
463 if (dh->dccph_type == DCCP_PKT_RESET)
464 goto discard;
465
466 /* Caller (dccp_v4_do_rcv) will send Reset */
467 dcb->dccpd_reset_code = DCCP_RESET_CODE_NO_CONNECTION;
468 return 1;
469 }
470
471 if (sk->sk_state != DCCP_REQUESTING) {
472 if (dccp_check_seqno(sk, skb))
473 goto discard;
474
475 /*
476 * Step 8: Process options and mark acknowledgeable
477 */
478 if (dccp_parse_options(sk, skb))
479 goto discard;
480
481 if (dcb->dccpd_ack_seq != DCCP_PKT_WITHOUT_ACK_SEQ)
482 dccp_event_ack_recv(sk, skb);
483
484 if (dccp_msk(sk)->dccpms_send_ack_vector &&
485 dccp_ackvec_add(dp->dccps_hc_rx_ackvec, sk,
486 DCCP_SKB_CB(skb)->dccpd_seq,
487 DCCP_ACKVEC_STATE_RECEIVED))
488 goto discard;
489
490 ccid_hc_rx_packet_recv(dp->dccps_hc_rx_ccid, sk, skb);
491 ccid_hc_tx_packet_recv(dp->dccps_hc_tx_ccid, sk, skb);
492 }
493
494 /*
495 * Step 9: Process Reset
496 * If P.type == Reset,
497 * Tear down connection
498 * S.state := TIMEWAIT
499 * Set TIMEWAIT timer
500 * Drop packet and return
501 */
502 if (dh->dccph_type == DCCP_PKT_RESET) {
503 /*
504 * Queue the equivalent of TCP fin so that dccp_recvmsg
505 * exits the loop
506 */
507 dccp_fin(sk, skb);
508 dccp_time_wait(sk, DCCP_TIME_WAIT, 0);
509 return 0;
510 /*
511 * Step 7: Check for unexpected packet types
512 * If (S.is_server and P.type == CloseReq)
513 * or (S.is_server and P.type == Response)
514 * or (S.is_client and P.type == Request)
515 * or (S.state == RESPOND and P.type == Data),
516 * Send Sync packet acknowledging P.seqno
517 * Drop packet and return
518 */
519 } else if ((dp->dccps_role != DCCP_ROLE_CLIENT &&
520 (dh->dccph_type == DCCP_PKT_RESPONSE ||
521 dh->dccph_type == DCCP_PKT_CLOSEREQ)) ||
522 (dp->dccps_role == DCCP_ROLE_CLIENT &&
523 dh->dccph_type == DCCP_PKT_REQUEST) ||
524 (sk->sk_state == DCCP_RESPOND &&
525 dh->dccph_type == DCCP_PKT_DATA)) {
526 dccp_send_sync(sk, dcb->dccpd_seq, DCCP_PKT_SYNC);
527 goto discard;
528 } else if (dh->dccph_type == DCCP_PKT_CLOSEREQ) {
529 dccp_rcv_closereq(sk, skb);
530 goto discard;
531 } else if (dh->dccph_type == DCCP_PKT_CLOSE) {
532 dccp_rcv_close(sk, skb);
533 return 0;
534 }
535
536 if (unlikely(dh->dccph_type == DCCP_PKT_SYNC)) {
537 dccp_send_sync(sk, dcb->dccpd_seq, DCCP_PKT_SYNCACK);
538 goto discard;
539 }
540
541 switch (sk->sk_state) {
542 case DCCP_CLOSED:
543 dcb->dccpd_reset_code = DCCP_RESET_CODE_NO_CONNECTION;
544 return 1;
545
546 case DCCP_REQUESTING:
547 /* FIXME: do congestion control initialization */
548
549 queued = dccp_rcv_request_sent_state_process(sk, skb, dh, len);
550 if (queued >= 0)
551 return queued;
552
553 __kfree_skb(skb);
554 return 0;
555
556 case DCCP_RESPOND:
557 case DCCP_PARTOPEN:
558 queued = dccp_rcv_respond_partopen_state_process(sk, skb,
559 dh, len);
560 break;
561 }
562
563 if (dh->dccph_type == DCCP_PKT_ACK ||
564 dh->dccph_type == DCCP_PKT_DATAACK) {
565 switch (old_state) {
566 case DCCP_PARTOPEN:
567 sk->sk_state_change(sk);
568 sk_wake_async(sk, 0, POLL_OUT);
569 break;
570 }
571 }
572
573 if (!queued) {
574 discard:
575 __kfree_skb(skb);
576 }
577 return 0;
578 }
579
580 EXPORT_SYMBOL_GPL(dccp_rcv_state_process);