1
2
3
4
5
6
7
8
9
10
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
23int sysctl_dccp_sync_ratelimit __read_mostly = HZ / 8;
24
25static void dccp_fin(struct sock *sk, struct sk_buff *skb)
26{
27 sk->sk_shutdown |= RCV_SHUTDOWN;
28 sock_set_flag(sk, SOCK_DONE);
29 __skb_pull(skb, dccp_hdr(skb)->dccph_doff * 4);
30 __skb_queue_tail(&sk->sk_receive_queue, skb);
31 skb_set_owner_r(skb, sk);
32 sk->sk_data_ready(sk, 0);
33}
34
35static void dccp_rcv_close(struct sock *sk, struct sk_buff *skb)
36{
37 dccp_send_reset(sk, DCCP_RESET_CODE_CLOSED);
38 dccp_fin(sk, skb);
39 dccp_set_state(sk, DCCP_CLOSED);
40 sk_wake_async(sk, 1, POLL_HUP);
41}
42
43static void dccp_rcv_closereq(struct sock *sk, struct sk_buff *skb)
44{
45
46
47
48
49
50
51 if (dccp_sk(sk)->dccps_role != DCCP_ROLE_CLIENT) {
52 dccp_send_sync(sk, DCCP_SKB_CB(skb)->dccpd_seq, DCCP_PKT_SYNC);
53 return;
54 }
55
56 if (sk->sk_state != DCCP_CLOSING)
57 dccp_set_state(sk, DCCP_CLOSING);
58 dccp_send_close(sk, 0);
59}
60
61static u8 dccp_reset_code_convert(const u8 code)
62{
63 const u8 error_code[] = {
64 [DCCP_RESET_CODE_CLOSED] = 0,
65 [DCCP_RESET_CODE_UNSPECIFIED] = 0,
66 [DCCP_RESET_CODE_ABORTED] = ECONNRESET,
67
68 [DCCP_RESET_CODE_NO_CONNECTION] = ECONNREFUSED,
69 [DCCP_RESET_CODE_CONNECTION_REFUSED] = ECONNREFUSED,
70 [DCCP_RESET_CODE_TOO_BUSY] = EUSERS,
71 [DCCP_RESET_CODE_AGGRESSION_PENALTY] = EDQUOT,
72
73 [DCCP_RESET_CODE_PACKET_ERROR] = ENOMSG,
74 [DCCP_RESET_CODE_BAD_INIT_COOKIE] = EBADR,
75 [DCCP_RESET_CODE_BAD_SERVICE_CODE] = EBADRQC,
76 [DCCP_RESET_CODE_OPTION_ERROR] = EILSEQ,
77 [DCCP_RESET_CODE_MANDATORY_ERROR] = EOPNOTSUPP,
78 };
79
80 return code >= DCCP_MAX_RESET_CODES ? 0 : error_code[code];
81}
82
83static void dccp_rcv_reset(struct sock *sk, struct sk_buff *skb)
84{
85 u8 err = dccp_reset_code_convert(dccp_hdr_reset(skb)->dccph_reset_code);
86
87 sk->sk_err = err;
88
89
90 dccp_fin(sk, skb);
91
92 if (err && !sock_flag(sk, SOCK_DEAD))
93 sk_wake_async(sk, 0, POLL_ERR);
94 dccp_time_wait(sk, DCCP_TIME_WAIT, 0);
95}
96
97static void dccp_event_ack_recv(struct sock *sk, struct sk_buff *skb)
98{
99 struct dccp_sock *dp = dccp_sk(sk);
100
101 if (dccp_msk(sk)->dccpms_send_ack_vector)
102 dccp_ackvec_check_rcv_ackno(dp->dccps_hc_rx_ackvec, sk,
103 DCCP_SKB_CB(skb)->dccpd_ack_seq);
104}
105
106static int dccp_check_seqno(struct sock *sk, struct sk_buff *skb)
107{
108 const struct dccp_hdr *dh = dccp_hdr(skb);
109 struct dccp_sock *dp = dccp_sk(sk);
110 u64 lswl, lawl, seqno = DCCP_SKB_CB(skb)->dccpd_seq,
111 ackno = DCCP_SKB_CB(skb)->dccpd_ack_seq;
112
113
114
115
116
117
118
119
120
121
122
123
124
125 if (dh->dccph_type == DCCP_PKT_SYNC ||
126 dh->dccph_type == DCCP_PKT_SYNCACK) {
127 if (between48(ackno, dp->dccps_awl, dp->dccps_awh) &&
128 dccp_delta_seqno(dp->dccps_swl, seqno) >= 0)
129 dccp_update_gsr(sk, seqno);
130 else
131 return -1;
132 }
133
134
135
136
137
138
139
140
141
142
143
144
145 lswl = dp->dccps_swl;
146 lawl = dp->dccps_awl;
147
148 if (dh->dccph_type == DCCP_PKT_CLOSEREQ ||
149 dh->dccph_type == DCCP_PKT_CLOSE ||
150 dh->dccph_type == DCCP_PKT_RESET) {
151 lswl = ADD48(dp->dccps_gsr, 1);
152 lawl = dp->dccps_gar;
153 }
154
155 if (between48(seqno, lswl, dp->dccps_swh) &&
156 (ackno == DCCP_PKT_WITHOUT_ACK_SEQ ||
157 between48(ackno, lawl, dp->dccps_awh))) {
158 dccp_update_gsr(sk, seqno);
159
160 if (dh->dccph_type != DCCP_PKT_SYNC &&
161 (ackno != DCCP_PKT_WITHOUT_ACK_SEQ))
162 dp->dccps_gar = ackno;
163 } else {
164 unsigned long now = jiffies;
165
166
167
168
169
170
171
172
173
174
175
176
177 if (time_before(now, (dp->dccps_rate_last +
178 sysctl_dccp_sync_ratelimit)))
179 return 0;
180
181 DCCP_WARN("DCCP: Step 6 failed for %s packet, "
182 "(LSWL(%llu) <= P.seqno(%llu) <= S.SWH(%llu)) and "
183 "(P.ackno %s or LAWL(%llu) <= P.ackno(%llu) <= S.AWH(%llu), "
184 "sending SYNC...\n", dccp_packet_name(dh->dccph_type),
185 (unsigned long long) lswl, (unsigned long long) seqno,
186 (unsigned long long) dp->dccps_swh,
187 (ackno == DCCP_PKT_WITHOUT_ACK_SEQ) ? "doesn't exist"
188 : "exists",
189 (unsigned long long) lawl, (unsigned long long) ackno,
190 (unsigned long long) dp->dccps_awh);
191
192 dp->dccps_rate_last = now;
193
194 if (dh->dccph_type == DCCP_PKT_RESET)
195 seqno = dp->dccps_gsr;
196 dccp_send_sync(sk, seqno, DCCP_PKT_SYNC);
197 return -1;
198 }
199
200 return 0;
201}
202
203static int __dccp_rcv_established(struct sock *sk, struct sk_buff *skb,
204 const struct dccp_hdr *dh, const unsigned len)
205{
206 struct dccp_sock *dp = dccp_sk(sk);
207
208 switch (dccp_hdr(skb)->dccph_type) {
209 case DCCP_PKT_DATAACK:
210 case DCCP_PKT_DATA:
211
212
213
214
215 __skb_pull(skb, dh->dccph_doff * 4);
216 __skb_queue_tail(&sk->sk_receive_queue, skb);
217 skb_set_owner_r(skb, sk);
218 sk->sk_data_ready(sk, 0);
219 return 0;
220 case DCCP_PKT_ACK:
221 goto discard;
222 case DCCP_PKT_RESET:
223
224
225
226
227
228
229
230
231 dccp_rcv_reset(sk, skb);
232 return 0;
233 case DCCP_PKT_CLOSEREQ:
234 dccp_rcv_closereq(sk, skb);
235 goto discard;
236 case DCCP_PKT_CLOSE:
237 dccp_rcv_close(sk, skb);
238 return 0;
239 case DCCP_PKT_REQUEST:
240
241
242
243
244
245
246
247
248
249
250
251 if (dp->dccps_role != DCCP_ROLE_LISTEN)
252 goto send_sync;
253 goto check_seq;
254 case DCCP_PKT_RESPONSE:
255 if (dp->dccps_role != DCCP_ROLE_CLIENT)
256 goto send_sync;
257check_seq:
258 if (dccp_delta_seqno(dp->dccps_osr,
259 DCCP_SKB_CB(skb)->dccpd_seq) >= 0) {
260send_sync:
261 dccp_send_sync(sk, DCCP_SKB_CB(skb)->dccpd_seq,
262 DCCP_PKT_SYNC);
263 }
264 break;
265 case DCCP_PKT_SYNC:
266 dccp_send_sync(sk, DCCP_SKB_CB(skb)->dccpd_seq,
267 DCCP_PKT_SYNCACK);
268
269
270
271
272
273
274
275 goto discard;
276 }
277
278 DCCP_INC_STATS_BH(DCCP_MIB_INERRS);
279discard:
280 __kfree_skb(skb);
281 return 0;
282}
283
284int dccp_rcv_established(struct sock *sk, struct sk_buff *skb,
285 const struct dccp_hdr *dh, const unsigned len)
286{
287 struct dccp_sock *dp = dccp_sk(sk);
288
289 if (dccp_check_seqno(sk, skb))
290 goto discard;
291
292 if (dccp_parse_options(sk, skb))
293 goto discard;
294
295 if (DCCP_SKB_CB(skb)->dccpd_ack_seq != DCCP_PKT_WITHOUT_ACK_SEQ)
296 dccp_event_ack_recv(sk, skb);
297
298 if (dccp_msk(sk)->dccpms_send_ack_vector &&
299 dccp_ackvec_add(dp->dccps_hc_rx_ackvec, sk,
300 DCCP_SKB_CB(skb)->dccpd_seq,
301 DCCP_ACKVEC_STATE_RECEIVED))
302 goto discard;
303
304 ccid_hc_rx_packet_recv(dp->dccps_hc_rx_ccid, sk, skb);
305 ccid_hc_tx_packet_recv(dp->dccps_hc_tx_ccid, sk, skb);
306
307 return __dccp_rcv_established(sk, skb, dh, len);
308discard:
309 __kfree_skb(skb);
310 return 0;
311}
312
313EXPORT_SYMBOL_GPL(dccp_rcv_established);
314
315static int dccp_rcv_request_sent_state_process(struct sock *sk,
316 struct sk_buff *skb,
317 const struct dccp_hdr *dh,
318 const unsigned len)
319{
320
321
322
323
324
325
326
327
328
329
330
331 if (dh->dccph_type == DCCP_PKT_RESPONSE) {
332 const struct inet_connection_sock *icsk = inet_csk(sk);
333 struct dccp_sock *dp = dccp_sk(sk);
334 long tstamp = dccp_timestamp();
335
336
337 inet_csk_clear_xmit_timer(sk, ICSK_TIME_RETRANS);
338 BUG_TRAP(sk->sk_send_head != NULL);
339 __kfree_skb(sk->sk_send_head);
340 sk->sk_send_head = NULL;
341
342 if (!between48(DCCP_SKB_CB(skb)->dccpd_ack_seq,
343 dp->dccps_awl, dp->dccps_awh)) {
344 dccp_pr_debug("invalid ackno: S.AWL=%llu, "
345 "P.ackno=%llu, S.AWH=%llu \n",
346 (unsigned long long)dp->dccps_awl,
347 (unsigned long long)DCCP_SKB_CB(skb)->dccpd_ack_seq,
348 (unsigned long long)dp->dccps_awh);
349 goto out_invalid_packet;
350 }
351
352 if (dccp_parse_options(sk, skb))
353 goto out_invalid_packet;
354
355
356 if (likely(dp->dccps_options_received.dccpor_timestamp_echo))
357 dp->dccps_syn_rtt = dccp_sample_rtt(sk, 10 * (tstamp -
358 dp->dccps_options_received.dccpor_timestamp_echo));
359
360 if (dccp_msk(sk)->dccpms_send_ack_vector &&
361 dccp_ackvec_add(dp->dccps_hc_rx_ackvec, sk,
362 DCCP_SKB_CB(skb)->dccpd_seq,
363 DCCP_ACKVEC_STATE_RECEIVED))
364 goto out_invalid_packet;
365
366 dp->dccps_isr = DCCP_SKB_CB(skb)->dccpd_seq;
367 dccp_update_gsr(sk, dp->dccps_isr);
368
369
370
371
372
373
374
375
376
377
378 dccp_set_seqno(&dp->dccps_swl,
379 max48(dp->dccps_swl, dp->dccps_isr));
380
381 dccp_sync_mss(sk, icsk->icsk_pmtu_cookie);
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398 dccp_set_state(sk, DCCP_PARTOPEN);
399
400
401 icsk->icsk_af_ops->rebuild_header(sk);
402
403 if (!sock_flag(sk, SOCK_DEAD)) {
404 sk->sk_state_change(sk);
405 sk_wake_async(sk, 0, POLL_OUT);
406 }
407
408 if (sk->sk_write_pending || icsk->icsk_ack.pingpong ||
409 icsk->icsk_accept_queue.rskq_defer_accept) {
410
411
412
413
414
415
416
417
418
419
420
421
422
423 __kfree_skb(skb);
424 return 0;
425 }
426 dccp_send_ack(sk);
427 return -1;
428 }
429
430out_invalid_packet:
431
432 DCCP_SKB_CB(skb)->dccpd_reset_code = DCCP_RESET_CODE_PACKET_ERROR;
433 return 1;
434}
435
436static int dccp_rcv_respond_partopen_state_process(struct sock *sk,
437 struct sk_buff *skb,
438 const struct dccp_hdr *dh,
439 const unsigned len)
440{
441 int queued = 0;
442
443 switch (dh->dccph_type) {
444 case DCCP_PKT_RESET:
445 inet_csk_clear_xmit_timer(sk, ICSK_TIME_DACK);
446 break;
447 case DCCP_PKT_DATA:
448 if (sk->sk_state == DCCP_RESPOND)
449 break;
450 case DCCP_PKT_DATAACK:
451 case DCCP_PKT_ACK:
452
453
454
455
456
457
458
459
460
461
462 if (sk->sk_state == DCCP_PARTOPEN)
463 inet_csk_clear_xmit_timer(sk, ICSK_TIME_DACK);
464
465 dccp_sk(sk)->dccps_osr = DCCP_SKB_CB(skb)->dccpd_seq;
466 dccp_set_state(sk, DCCP_OPEN);
467
468 if (dh->dccph_type == DCCP_PKT_DATAACK ||
469 dh->dccph_type == DCCP_PKT_DATA) {
470 __dccp_rcv_established(sk, skb, dh, len);
471 queued = 1;
472
473 }
474 break;
475 }
476
477 return queued;
478}
479
480int dccp_rcv_state_process(struct sock *sk, struct sk_buff *skb,
481 struct dccp_hdr *dh, unsigned len)
482{
483 struct dccp_sock *dp = dccp_sk(sk);
484 struct dccp_skb_cb *dcb = DCCP_SKB_CB(skb);
485 const int old_state = sk->sk_state;
486 int queued = 0;
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510 if (sk->sk_state == DCCP_LISTEN) {
511 if (dh->dccph_type == DCCP_PKT_REQUEST) {
512 if (inet_csk(sk)->icsk_af_ops->conn_request(sk,
513 skb) < 0)
514 return 1;
515
516
517 goto discard;
518 }
519 if (dh->dccph_type == DCCP_PKT_RESET)
520 goto discard;
521
522
523 dcb->dccpd_reset_code = DCCP_RESET_CODE_NO_CONNECTION;
524 return 1;
525 }
526
527 if (sk->sk_state != DCCP_REQUESTING) {
528 if (dccp_check_seqno(sk, skb))
529 goto discard;
530
531
532
533
534 if (dccp_parse_options(sk, skb))
535 goto discard;
536
537 if (dcb->dccpd_ack_seq != DCCP_PKT_WITHOUT_ACK_SEQ)
538 dccp_event_ack_recv(sk, skb);
539
540 if (dccp_msk(sk)->dccpms_send_ack_vector &&
541 dccp_ackvec_add(dp->dccps_hc_rx_ackvec, sk,
542 DCCP_SKB_CB(skb)->dccpd_seq,
543 DCCP_ACKVEC_STATE_RECEIVED))
544 goto discard;
545
546 ccid_hc_rx_packet_recv(dp->dccps_hc_rx_ccid, sk, skb);
547 ccid_hc_tx_packet_recv(dp->dccps_hc_tx_ccid, sk, skb);
548 }
549
550
551
552
553
554
555
556
557
558 if (dh->dccph_type == DCCP_PKT_RESET) {
559 dccp_rcv_reset(sk, skb);
560 return 0;
561
562
563
564
565
566
567
568
569
570 } else if ((dp->dccps_role != DCCP_ROLE_CLIENT &&
571 (dh->dccph_type == DCCP_PKT_RESPONSE ||
572 dh->dccph_type == DCCP_PKT_CLOSEREQ)) ||
573 (dp->dccps_role == DCCP_ROLE_CLIENT &&
574 dh->dccph_type == DCCP_PKT_REQUEST) ||
575 (sk->sk_state == DCCP_RESPOND &&
576 dh->dccph_type == DCCP_PKT_DATA)) {
577 dccp_send_sync(sk, dcb->dccpd_seq, DCCP_PKT_SYNC);
578 goto discard;
579 } else if (dh->dccph_type == DCCP_PKT_CLOSEREQ) {
580 dccp_rcv_closereq(sk, skb);
581 goto discard;
582 } else if (dh->dccph_type == DCCP_PKT_CLOSE) {
583 dccp_rcv_close(sk, skb);
584 return 0;
585 }
586
587 switch (sk->sk_state) {
588 case DCCP_CLOSED:
589 dcb->dccpd_reset_code = DCCP_RESET_CODE_NO_CONNECTION;
590 return 1;
591
592 case DCCP_REQUESTING:
593
594
595 queued = dccp_rcv_request_sent_state_process(sk, skb, dh, len);
596 if (queued >= 0)
597 return queued;
598
599 __kfree_skb(skb);
600 return 0;
601
602 case DCCP_RESPOND:
603 case DCCP_PARTOPEN:
604 queued = dccp_rcv_respond_partopen_state_process(sk, skb,
605 dh, len);
606 break;
607 }
608
609 if (dh->dccph_type == DCCP_PKT_ACK ||
610 dh->dccph_type == DCCP_PKT_DATAACK) {
611 switch (old_state) {
612 case DCCP_PARTOPEN:
613 sk->sk_state_change(sk);
614 sk_wake_async(sk, 0, POLL_OUT);
615 break;
616 }
617 } else if (unlikely(dh->dccph_type == DCCP_PKT_SYNC)) {
618 dccp_send_sync(sk, dcb->dccpd_seq, DCCP_PKT_SYNCACK);
619 goto discard;
620 }
621
622 if (!queued) {
623discard:
624 __kfree_skb(skb);
625 }
626 return 0;
627}
628
629EXPORT_SYMBOL_GPL(dccp_rcv_state_process);
630
631
632
633
634
635
636
637u32 dccp_sample_rtt(struct sock *sk, long delta)
638{
639
640 delta -= dccp_sk(sk)->dccps_options_received.dccpor_elapsed_time * 10;
641
642 if (unlikely(delta <= 0)) {
643 DCCP_WARN("unusable RTT sample %ld, using min\n", delta);
644 return DCCP_SANE_RTT_MIN;
645 }
646 if (unlikely(delta > DCCP_SANE_RTT_MAX)) {
647 DCCP_WARN("RTT sample %ld too large, using max\n", delta);
648 return DCCP_SANE_RTT_MAX;
649 }
650
651 return delta;
652}
653
654EXPORT_SYMBOL_GPL(dccp_sample_rtt);
655