]>
Commit | Line | Data |
---|---|---|
7c657876 ACM |
1 | /* |
2 | * net/dccp/options.c | |
3 | * | |
4 | * An implementation of the DCCP protocol | |
5 | * Aristeu Sergio Rozanski Filho <aris@cathedrallabs.org> | |
6 | * Arnaldo Carvalho de Melo <acme@ghostprotocols.net> | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or | |
9 | * modify it under the terms of the GNU General Public License | |
10 | * as published by the Free Software Foundation; either version | |
11 | * 2 of the License, or (at your option) any later version. | |
12 | */ | |
13 | #include <linux/config.h> | |
14 | #include <linux/dccp.h> | |
15 | #include <linux/module.h> | |
16 | #include <linux/types.h> | |
17 | #include <linux/kernel.h> | |
18 | #include <linux/skbuff.h> | |
19 | ||
20 | #include "ccid.h" | |
21 | #include "dccp.h" | |
22 | ||
23 | static void dccp_ackpkts_check_rcv_ackvector(struct dccp_ackpkts *ap, | |
24 | struct sock *sk, | |
25 | const u64 ackno, | |
26 | const unsigned char len, | |
27 | const unsigned char *vector); | |
28 | ||
29 | /* stores the default values for new connection. may be changed with sysctl */ | |
30 | static const struct dccp_options dccpo_default_values = { | |
31 | .dccpo_sequence_window = DCCPF_INITIAL_SEQUENCE_WINDOW, | |
32 | .dccpo_ccid = DCCPF_INITIAL_CCID, | |
33 | .dccpo_send_ack_vector = DCCPF_INITIAL_SEND_ACK_VECTOR, | |
34 | .dccpo_send_ndp_count = DCCPF_INITIAL_SEND_NDP_COUNT, | |
35 | }; | |
36 | ||
37 | void dccp_options_init(struct dccp_options *dccpo) | |
38 | { | |
39 | memcpy(dccpo, &dccpo_default_values, sizeof(*dccpo)); | |
40 | } | |
41 | ||
42 | static u32 dccp_decode_value_var(const unsigned char *bf, const u8 len) | |
43 | { | |
44 | u32 value = 0; | |
45 | ||
46 | if (len > 3) | |
47 | value += *bf++ << 24; | |
48 | if (len > 2) | |
49 | value += *bf++ << 16; | |
50 | if (len > 1) | |
51 | value += *bf++ << 8; | |
52 | if (len > 0) | |
53 | value += *bf; | |
54 | ||
55 | return value; | |
56 | } | |
57 | ||
58 | int dccp_parse_options(struct sock *sk, struct sk_buff *skb) | |
59 | { | |
60 | struct dccp_sock *dp = dccp_sk(sk); | |
725ba8ee | 61 | #ifdef CONFIG_IP_DCCP_DEBUG |
7690af3f ACM |
62 | const char *debug_prefix = dp->dccps_role == DCCP_ROLE_CLIENT ? |
63 | "CLIENT rx opt: " : "server rx opt: "; | |
7c657876 ACM |
64 | #endif |
65 | const struct dccp_hdr *dh = dccp_hdr(skb); | |
66 | const u8 pkt_type = DCCP_SKB_CB(skb)->dccpd_type; | |
67 | unsigned char *options = (unsigned char *)dh + dccp_hdr_len(skb); | |
68 | unsigned char *opt_ptr = options; | |
7690af3f ACM |
69 | const unsigned char *opt_end = (unsigned char *)dh + |
70 | (dh->dccph_doff * 4); | |
7c657876 ACM |
71 | struct dccp_options_received *opt_recv = &dp->dccps_options_received; |
72 | unsigned char opt, len; | |
73 | unsigned char *value; | |
74 | ||
75 | memset(opt_recv, 0, sizeof(*opt_recv)); | |
76 | ||
77 | while (opt_ptr != opt_end) { | |
78 | opt = *opt_ptr++; | |
79 | len = 0; | |
80 | value = NULL; | |
81 | ||
82 | /* Check if this isn't a single byte option */ | |
83 | if (opt > DCCPO_MAX_RESERVED) { | |
84 | if (opt_ptr == opt_end) | |
85 | goto out_invalid_option; | |
86 | ||
87 | len = *opt_ptr++; | |
88 | if (len < 3) | |
89 | goto out_invalid_option; | |
90 | /* | |
91 | * Remove the type and len fields, leaving | |
92 | * just the value size | |
93 | */ | |
94 | len -= 2; | |
95 | value = opt_ptr; | |
96 | opt_ptr += len; | |
97 | ||
98 | if (opt_ptr > opt_end) | |
99 | goto out_invalid_option; | |
100 | } | |
101 | ||
102 | switch (opt) { | |
103 | case DCCPO_PADDING: | |
104 | break; | |
105 | case DCCPO_NDP_COUNT: | |
106 | if (len > 3) | |
107 | goto out_invalid_option; | |
108 | ||
109 | opt_recv->dccpor_ndp = dccp_decode_value_var(value, len); | |
7690af3f ACM |
110 | dccp_pr_debug("%sNDP count=%d\n", debug_prefix, |
111 | opt_recv->dccpor_ndp); | |
7c657876 ACM |
112 | break; |
113 | case DCCPO_ACK_VECTOR_0: | |
114 | if (len > DCCP_MAX_ACK_VECTOR_LEN) | |
115 | goto out_invalid_option; | |
116 | ||
117 | if (pkt_type == DCCP_PKT_DATA) | |
118 | continue; | |
119 | ||
120 | opt_recv->dccpor_ack_vector_len = len; | |
121 | opt_recv->dccpor_ack_vector_idx = value - options; | |
122 | ||
123 | dccp_pr_debug("%sACK vector 0, len=%d, ack_ackno=%llu\n", | |
f6ccf554 DM |
124 | debug_prefix, len, |
125 | (unsigned long long) | |
126 | DCCP_SKB_CB(skb)->dccpd_ack_seq); | |
7c657876 ACM |
127 | dccp_ackvector_print(DCCP_SKB_CB(skb)->dccpd_ack_seq, |
128 | value, len); | |
7690af3f ACM |
129 | dccp_ackpkts_check_rcv_ackvector(dp->dccps_hc_rx_ackpkts, |
130 | sk, | |
131 | DCCP_SKB_CB(skb)->dccpd_ack_seq, | |
7c657876 ACM |
132 | len, value); |
133 | break; | |
134 | case DCCPO_TIMESTAMP: | |
135 | if (len != 4) | |
136 | goto out_invalid_option; | |
137 | ||
138 | opt_recv->dccpor_timestamp = ntohl(*(u32 *)value); | |
139 | ||
140 | dp->dccps_timestamp_echo = opt_recv->dccpor_timestamp; | |
141 | dp->dccps_timestamp_time = jiffies; | |
142 | ||
143 | dccp_pr_debug("%sTIMESTAMP=%u, ackno=%llu\n", | |
144 | debug_prefix, opt_recv->dccpor_timestamp, | |
f6ccf554 | 145 | (unsigned long long) |
7c657876 ACM |
146 | DCCP_SKB_CB(skb)->dccpd_ack_seq); |
147 | break; | |
148 | case DCCPO_TIMESTAMP_ECHO: | |
149 | if (len < 4 || len > 8) | |
150 | goto out_invalid_option; | |
151 | ||
152 | opt_recv->dccpor_timestamp_echo = ntohl(*(u32 *)value); | |
153 | ||
7690af3f ACM |
154 | dccp_pr_debug("%sTIMESTAMP_ECHO=%u, len=%d, ackno=%llu, " |
155 | "diff=%u\n", | |
156 | debug_prefix, | |
157 | opt_recv->dccpor_timestamp_echo, | |
f6ccf554 DM |
158 | len + 2, |
159 | (unsigned long long) | |
160 | DCCP_SKB_CB(skb)->dccpd_ack_seq, | |
7690af3f ACM |
161 | (tcp_time_stamp - |
162 | opt_recv->dccpor_timestamp_echo)); | |
163 | ||
164 | opt_recv->dccpor_elapsed_time = | |
165 | dccp_decode_value_var(value + 4, | |
166 | len - 4); | |
167 | dccp_pr_debug("%sTIMESTAMP_ECHO ELAPSED_TIME=%d\n", | |
168 | debug_prefix, | |
7c657876 ACM |
169 | opt_recv->dccpor_elapsed_time); |
170 | break; | |
171 | case DCCPO_ELAPSED_TIME: | |
172 | if (len > 4) | |
173 | goto out_invalid_option; | |
174 | ||
175 | if (pkt_type == DCCP_PKT_DATA) | |
176 | continue; | |
7690af3f ACM |
177 | opt_recv->dccpor_elapsed_time = |
178 | dccp_decode_value_var(value, len); | |
7c657876 ACM |
179 | dccp_pr_debug("%sELAPSED_TIME=%d\n", debug_prefix, |
180 | opt_recv->dccpor_elapsed_time); | |
181 | break; | |
182 | /* | |
183 | * From draft-ietf-dccp-spec-11.txt: | |
184 | * | |
7690af3f ACM |
185 | * Option numbers 128 through 191 are for |
186 | * options sent from the HC-Sender to the | |
187 | * HC-Receiver; option numbers 192 through 255 | |
188 | * are for options sent from the HC-Receiver to | |
189 | * the HC-Sender. | |
7c657876 ACM |
190 | */ |
191 | case 128 ... 191: { | |
192 | const u16 idx = value - options; | |
193 | ||
7690af3f ACM |
194 | if (ccid_hc_rx_parse_options(dp->dccps_hc_rx_ccid, sk, |
195 | opt, len, idx, | |
196 | value) != 0) | |
7c657876 ACM |
197 | goto out_invalid_option; |
198 | } | |
199 | break; | |
200 | case 192 ... 255: { | |
201 | const u16 idx = value - options; | |
202 | ||
7690af3f ACM |
203 | if (ccid_hc_tx_parse_options(dp->dccps_hc_tx_ccid, sk, |
204 | opt, len, idx, | |
205 | value) != 0) | |
7c657876 ACM |
206 | goto out_invalid_option; |
207 | } | |
208 | break; | |
209 | default: | |
7690af3f ACM |
210 | pr_info("DCCP(%p): option %d(len=%d) not " |
211 | "implemented, ignoring\n", | |
7c657876 ACM |
212 | sk, opt, len); |
213 | break; | |
214 | } | |
215 | } | |
216 | ||
217 | return 0; | |
218 | ||
219 | out_invalid_option: | |
220 | DCCP_INC_STATS_BH(DCCP_MIB_INVALIDOPT); | |
221 | DCCP_SKB_CB(skb)->dccpd_reset_code = DCCP_RESET_CODE_OPTION_ERROR; | |
222 | pr_info("DCCP(%p): invalid option %d, len=%d\n", sk, opt, len); | |
223 | return -1; | |
224 | } | |
225 | ||
226 | static void dccp_encode_value_var(const u32 value, unsigned char *to, | |
227 | const unsigned int len) | |
228 | { | |
229 | if (len > 3) | |
230 | *to++ = (value & 0xFF000000) >> 24; | |
231 | if (len > 2) | |
232 | *to++ = (value & 0xFF0000) >> 16; | |
233 | if (len > 1) | |
234 | *to++ = (value & 0xFF00) >> 8; | |
235 | if (len > 0) | |
236 | *to++ = (value & 0xFF); | |
237 | } | |
238 | ||
239 | static inline int dccp_ndp_len(const int ndp) | |
240 | { | |
241 | return likely(ndp <= 0xFF) ? 1 : ndp <= 0xFFFF ? 2 : 3; | |
242 | } | |
243 | ||
244 | void dccp_insert_option(struct sock *sk, struct sk_buff *skb, | |
245 | const unsigned char option, | |
246 | const void *value, const unsigned char len) | |
247 | { | |
248 | unsigned char *to; | |
249 | ||
250 | if (DCCP_SKB_CB(skb)->dccpd_opt_len + len + 2 > DCCP_MAX_OPT_LEN) { | |
7690af3f ACM |
251 | LIMIT_NETDEBUG(KERN_INFO "DCCP: packet too small to insert " |
252 | "%d option!\n", option); | |
7c657876 ACM |
253 | return; |
254 | } | |
255 | ||
256 | DCCP_SKB_CB(skb)->dccpd_opt_len += len + 2; | |
257 | ||
258 | to = skb_push(skb, len + 2); | |
259 | *to++ = option; | |
260 | *to++ = len + 2; | |
261 | ||
262 | memcpy(to, value, len); | |
263 | } | |
264 | ||
265 | EXPORT_SYMBOL_GPL(dccp_insert_option); | |
266 | ||
267 | static void dccp_insert_option_ndp(struct sock *sk, struct sk_buff *skb) | |
268 | { | |
269 | struct dccp_sock *dp = dccp_sk(sk); | |
270 | int ndp = dp->dccps_ndp_count; | |
271 | ||
272 | if (dccp_non_data_packet(skb)) | |
273 | ++dp->dccps_ndp_count; | |
274 | else | |
275 | dp->dccps_ndp_count = 0; | |
276 | ||
277 | if (ndp > 0) { | |
278 | unsigned char *ptr; | |
279 | const int ndp_len = dccp_ndp_len(ndp); | |
280 | const int len = ndp_len + 2; | |
281 | ||
282 | if (DCCP_SKB_CB(skb)->dccpd_opt_len + len > DCCP_MAX_OPT_LEN) | |
283 | return; | |
284 | ||
285 | DCCP_SKB_CB(skb)->dccpd_opt_len += len; | |
286 | ||
287 | ptr = skb_push(skb, len); | |
288 | *ptr++ = DCCPO_NDP_COUNT; | |
289 | *ptr++ = len; | |
290 | dccp_encode_value_var(ndp, ptr, ndp_len); | |
291 | } | |
292 | } | |
293 | ||
294 | static inline int dccp_elapsed_time_len(const u32 elapsed_time) | |
295 | { | |
296 | return elapsed_time == 0 ? 0 : | |
297 | elapsed_time <= 0xFF ? 1 : | |
298 | elapsed_time <= 0xFFFF ? 2 : | |
299 | elapsed_time <= 0xFFFFFF ? 3 : 4; | |
300 | } | |
301 | ||
302 | void dccp_insert_option_elapsed_time(struct sock *sk, | |
303 | struct sk_buff *skb, | |
304 | u32 elapsed_time) | |
305 | { | |
725ba8ee | 306 | #ifdef CONFIG_IP_DCCP_DEBUG |
7c657876 | 307 | struct dccp_sock *dp = dccp_sk(sk); |
7690af3f ACM |
308 | const char *debug_prefix = dp->dccps_role == DCCP_ROLE_CLIENT ? |
309 | "CLIENT TX opt: " : "server TX opt: "; | |
7c657876 ACM |
310 | #endif |
311 | const int elapsed_time_len = dccp_elapsed_time_len(elapsed_time); | |
312 | const int len = 2 + elapsed_time_len; | |
313 | unsigned char *to; | |
314 | ||
315 | /* If elapsed_time == 0... */ | |
316 | if (elapsed_time_len == 2) | |
317 | return; | |
318 | ||
319 | if (DCCP_SKB_CB(skb)->dccpd_opt_len + len > DCCP_MAX_OPT_LEN) { | |
7690af3f ACM |
320 | LIMIT_NETDEBUG(KERN_INFO "DCCP: packet too small to " |
321 | "insert elapsed time!\n"); | |
7c657876 ACM |
322 | return; |
323 | } | |
324 | ||
325 | DCCP_SKB_CB(skb)->dccpd_opt_len += len; | |
326 | ||
327 | to = skb_push(skb, len); | |
328 | *to++ = DCCPO_ELAPSED_TIME; | |
329 | *to++ = len; | |
330 | ||
331 | dccp_encode_value_var(elapsed_time, to, elapsed_time_len); | |
332 | ||
333 | dccp_pr_debug("%sELAPSED_TIME=%u, len=%d, seqno=%llu\n", | |
334 | debug_prefix, elapsed_time, | |
f6ccf554 DM |
335 | len, |
336 | (unsigned long long) DCCP_SKB_CB(skb)->dccpd_seq); | |
7c657876 ACM |
337 | } |
338 | ||
339 | EXPORT_SYMBOL(dccp_insert_option_elapsed_time); | |
340 | ||
341 | static void dccp_insert_option_ack_vector(struct sock *sk, struct sk_buff *skb) | |
342 | { | |
343 | struct dccp_sock *dp = dccp_sk(sk); | |
725ba8ee | 344 | #ifdef CONFIG_IP_DCCP_DEBUG |
7690af3f ACM |
345 | const char *debug_prefix = dp->dccps_role == DCCP_ROLE_CLIENT ? |
346 | "CLIENT TX opt: " : "server TX opt: "; | |
7c657876 ACM |
347 | #endif |
348 | struct dccp_ackpkts *ap = dp->dccps_hc_rx_ackpkts; | |
349 | int len = ap->dccpap_buf_vector_len + 2; | |
350 | const u32 elapsed_time = jiffies_to_usecs(jiffies - ap->dccpap_time) / 10; | |
351 | unsigned char *to, *from; | |
352 | ||
353 | if (elapsed_time != 0) | |
354 | dccp_insert_option_elapsed_time(sk, skb, elapsed_time); | |
355 | ||
356 | if (DCCP_SKB_CB(skb)->dccpd_opt_len + len > DCCP_MAX_OPT_LEN) { | |
7690af3f ACM |
357 | LIMIT_NETDEBUG(KERN_INFO "DCCP: packet too small to " |
358 | "insert ACK Vector!\n"); | |
7c657876 ACM |
359 | return; |
360 | } | |
361 | ||
362 | /* | |
363 | * XXX: now we have just one ack vector sent record, so | |
364 | * we have to wait for it to be cleared. | |
365 | * | |
366 | * Of course this is not acceptable, but this is just for | |
367 | * basic testing now. | |
368 | */ | |
369 | if (ap->dccpap_ack_seqno != DCCP_MAX_SEQNO + 1) | |
370 | return; | |
371 | ||
372 | DCCP_SKB_CB(skb)->dccpd_opt_len += len; | |
373 | ||
374 | to = skb_push(skb, len); | |
375 | *to++ = DCCPO_ACK_VECTOR_0; | |
376 | *to++ = len; | |
377 | ||
378 | len = ap->dccpap_buf_vector_len; | |
379 | from = ap->dccpap_buf + ap->dccpap_buf_head; | |
380 | ||
381 | /* Check if buf_head wraps */ | |
382 | if (ap->dccpap_buf_head + len > ap->dccpap_buf_len) { | |
7690af3f ACM |
383 | const unsigned int tailsize = (ap->dccpap_buf_len - |
384 | ap->dccpap_buf_head); | |
7c657876 ACM |
385 | |
386 | memcpy(to, from, tailsize); | |
387 | to += tailsize; | |
388 | len -= tailsize; | |
389 | from = ap->dccpap_buf; | |
390 | } | |
391 | ||
392 | memcpy(to, from, len); | |
393 | /* | |
394 | * From draft-ietf-dccp-spec-11.txt: | |
395 | * | |
396 | * For each acknowledgement it sends, the HC-Receiver will add an | |
397 | * acknowledgement record. ack_seqno will equal the HC-Receiver | |
398 | * sequence number it used for the ack packet; ack_ptr will equal | |
7690af3f ACM |
399 | * buf_head; ack_ackno will equal buf_ackno; and ack_nonce will |
400 | * equal buf_nonce. | |
7c657876 ACM |
401 | * |
402 | * This implemention uses just one ack record for now. | |
403 | */ | |
404 | ap->dccpap_ack_seqno = DCCP_SKB_CB(skb)->dccpd_seq; | |
405 | ap->dccpap_ack_ptr = ap->dccpap_buf_head; | |
406 | ap->dccpap_ack_ackno = ap->dccpap_buf_ackno; | |
407 | ap->dccpap_ack_nonce = ap->dccpap_buf_nonce; | |
408 | ap->dccpap_ack_vector_len = ap->dccpap_buf_vector_len; | |
409 | ||
7690af3f ACM |
410 | dccp_pr_debug("%sACK Vector 0, len=%d, ack_seqno=%llu, " |
411 | "ack_ackno=%llu\n", | |
7c657876 | 412 | debug_prefix, ap->dccpap_ack_vector_len, |
f6ccf554 DM |
413 | (unsigned long long) ap->dccpap_ack_seqno, |
414 | (unsigned long long) ap->dccpap_ack_ackno); | |
7c657876 ACM |
415 | } |
416 | ||
7690af3f ACM |
417 | static inline void dccp_insert_option_timestamp(struct sock *sk, |
418 | struct sk_buff *skb) | |
7c657876 ACM |
419 | { |
420 | const u32 now = htonl(tcp_time_stamp); | |
421 | dccp_insert_option(sk, skb, DCCPO_TIMESTAMP, &now, sizeof(now)); | |
422 | } | |
423 | ||
7690af3f ACM |
424 | static void dccp_insert_option_timestamp_echo(struct sock *sk, |
425 | struct sk_buff *skb) | |
7c657876 ACM |
426 | { |
427 | struct dccp_sock *dp = dccp_sk(sk); | |
725ba8ee | 428 | #ifdef CONFIG_IP_DCCP_DEBUG |
7690af3f ACM |
429 | const char *debug_prefix = dp->dccps_role == DCCP_ROLE_CLIENT ? |
430 | "CLIENT TX opt: " : "server TX opt: "; | |
7c657876 ACM |
431 | #endif |
432 | u32 tstamp_echo; | |
7690af3f ACM |
433 | const u32 elapsed_time = jiffies_to_usecs(jiffies - |
434 | dp->dccps_timestamp_time) / 10; | |
7c657876 ACM |
435 | const int elapsed_time_len = dccp_elapsed_time_len(elapsed_time); |
436 | const int len = 6 + elapsed_time_len; | |
437 | unsigned char *to; | |
438 | ||
439 | if (DCCP_SKB_CB(skb)->dccpd_opt_len + len > DCCP_MAX_OPT_LEN) { | |
7690af3f ACM |
440 | LIMIT_NETDEBUG(KERN_INFO "DCCP: packet too small to insert " |
441 | "timestamp echo!\n"); | |
7c657876 ACM |
442 | return; |
443 | } | |
444 | ||
445 | DCCP_SKB_CB(skb)->dccpd_opt_len += len; | |
446 | ||
447 | to = skb_push(skb, len); | |
448 | *to++ = DCCPO_TIMESTAMP_ECHO; | |
449 | *to++ = len; | |
450 | ||
451 | tstamp_echo = htonl(dp->dccps_timestamp_echo); | |
452 | memcpy(to, &tstamp_echo, 4); | |
453 | to += 4; | |
454 | dccp_encode_value_var(elapsed_time, to, elapsed_time_len); | |
455 | ||
456 | dccp_pr_debug("%sTIMESTAMP_ECHO=%u, len=%d, seqno=%llu\n", | |
457 | debug_prefix, dp->dccps_timestamp_echo, | |
f6ccf554 DM |
458 | len, |
459 | (unsigned long long) DCCP_SKB_CB(skb)->dccpd_seq); | |
7c657876 ACM |
460 | |
461 | dp->dccps_timestamp_echo = 0; | |
462 | dp->dccps_timestamp_time = 0; | |
463 | } | |
464 | ||
465 | void dccp_insert_options(struct sock *sk, struct sk_buff *skb) | |
466 | { | |
467 | struct dccp_sock *dp = dccp_sk(sk); | |
468 | ||
469 | DCCP_SKB_CB(skb)->dccpd_opt_len = 0; | |
470 | ||
471 | if (dp->dccps_options.dccpo_send_ndp_count) | |
472 | dccp_insert_option_ndp(sk, skb); | |
473 | ||
474 | if (!dccp_packet_without_ack(skb)) { | |
475 | if (dp->dccps_options.dccpo_send_ack_vector && | |
7690af3f ACM |
476 | (dp->dccps_hc_rx_ackpkts->dccpap_buf_ackno != |
477 | DCCP_MAX_SEQNO + 1)) | |
7c657876 ACM |
478 | dccp_insert_option_ack_vector(sk, skb); |
479 | ||
480 | dccp_insert_option_timestamp(sk, skb); | |
481 | if (dp->dccps_timestamp_echo != 0) | |
482 | dccp_insert_option_timestamp_echo(sk, skb); | |
483 | } | |
484 | ||
485 | ccid_hc_rx_insert_options(dp->dccps_hc_rx_ccid, sk, skb); | |
486 | ccid_hc_tx_insert_options(dp->dccps_hc_tx_ccid, sk, skb); | |
487 | ||
488 | /* XXX: insert other options when appropriate */ | |
489 | ||
490 | if (DCCP_SKB_CB(skb)->dccpd_opt_len != 0) { | |
491 | /* The length of all options has to be a multiple of 4 */ | |
492 | int padding = DCCP_SKB_CB(skb)->dccpd_opt_len % 4; | |
493 | ||
494 | if (padding != 0) { | |
495 | padding = 4 - padding; | |
496 | memset(skb_push(skb, padding), 0, padding); | |
497 | DCCP_SKB_CB(skb)->dccpd_opt_len += padding; | |
498 | } | |
499 | } | |
500 | } | |
501 | ||
a1d3a355 ACM |
502 | struct dccp_ackpkts *dccp_ackpkts_alloc(const unsigned int len, |
503 | const unsigned int __nocast priority) | |
7c657876 ACM |
504 | { |
505 | struct dccp_ackpkts *ap = kmalloc(sizeof(*ap) + len, priority); | |
506 | ||
507 | if (ap != NULL) { | |
725ba8ee | 508 | #ifdef CONFIG_IP_DCCP_DEBUG |
7c657876 ACM |
509 | memset(ap->dccpap_buf, 0xFF, len); |
510 | #endif | |
7690af3f ACM |
511 | ap->dccpap_buf_len = len; |
512 | ap->dccpap_buf_head = | |
513 | ap->dccpap_buf_tail = | |
514 | ap->dccpap_buf_len - 1; | |
515 | ap->dccpap_buf_ackno = | |
516 | ap->dccpap_ack_ackno = | |
517 | ap->dccpap_ack_seqno = DCCP_MAX_SEQNO + 1; | |
518 | ap->dccpap_buf_nonce = ap->dccpap_buf_nonce = 0; | |
519 | ap->dccpap_ack_ptr = 0; | |
520 | ap->dccpap_time = 0; | |
7c657876 ACM |
521 | ap->dccpap_buf_vector_len = ap->dccpap_ack_vector_len = 0; |
522 | } | |
523 | ||
524 | return ap; | |
525 | } | |
526 | ||
527 | void dccp_ackpkts_free(struct dccp_ackpkts *ap) | |
528 | { | |
529 | if (ap != NULL) { | |
725ba8ee | 530 | #ifdef CONFIG_IP_DCCP_DEBUG |
7c657876 ACM |
531 | memset(ap, 0xFF, sizeof(*ap) + ap->dccpap_buf_len); |
532 | #endif | |
533 | kfree(ap); | |
534 | } | |
535 | } | |
536 | ||
537 | static inline u8 dccp_ackpkts_state(const struct dccp_ackpkts *ap, | |
538 | const unsigned int index) | |
539 | { | |
540 | return ap->dccpap_buf[index] & DCCP_ACKPKTS_STATE_MASK; | |
541 | } | |
542 | ||
543 | static inline u8 dccp_ackpkts_len(const struct dccp_ackpkts *ap, | |
544 | const unsigned int index) | |
545 | { | |
546 | return ap->dccpap_buf[index] & DCCP_ACKPKTS_LEN_MASK; | |
547 | } | |
548 | ||
549 | /* | |
550 | * If several packets are missing, the HC-Receiver may prefer to enter multiple | |
551 | * bytes with run length 0, rather than a single byte with a larger run length; | |
552 | * this simplifies table updates if one of the missing packets arrives. | |
553 | */ | |
554 | static inline int dccp_ackpkts_set_buf_head_state(struct dccp_ackpkts *ap, | |
555 | const unsigned int packets, | |
556 | const unsigned char state) | |
557 | { | |
558 | unsigned int gap; | |
559 | signed long new_head; | |
560 | ||
561 | if (ap->dccpap_buf_vector_len + packets > ap->dccpap_buf_len) | |
562 | return -ENOBUFS; | |
563 | ||
564 | gap = packets - 1; | |
565 | new_head = ap->dccpap_buf_head - packets; | |
566 | ||
567 | if (new_head < 0) { | |
568 | if (gap > 0) { | |
569 | memset(ap->dccpap_buf, DCCP_ACKPKTS_STATE_NOT_RECEIVED, | |
570 | gap + new_head + 1); | |
571 | gap = -new_head; | |
572 | } | |
573 | new_head += ap->dccpap_buf_len; | |
574 | } | |
575 | ||
576 | ap->dccpap_buf_head = new_head; | |
577 | ||
578 | if (gap > 0) | |
579 | memset(ap->dccpap_buf + ap->dccpap_buf_head + 1, | |
580 | DCCP_ACKPKTS_STATE_NOT_RECEIVED, gap); | |
581 | ||
582 | ap->dccpap_buf[ap->dccpap_buf_head] = state; | |
583 | ap->dccpap_buf_vector_len += packets; | |
584 | return 0; | |
585 | } | |
586 | ||
587 | /* | |
588 | * Implements the draft-ietf-dccp-spec-11.txt Appendix A | |
589 | */ | |
590 | int dccp_ackpkts_add(struct dccp_ackpkts *ap, u64 ackno, u8 state) | |
591 | { | |
592 | /* | |
593 | * Check at the right places if the buffer is full, if it is, tell the | |
594 | * caller to start dropping packets till the HC-Sender acks our ACK | |
595 | * vectors, when we will free up space in dccpap_buf. | |
596 | * | |
597 | * We may well decide to do buffer compression, etc, but for now lets | |
598 | * just drop. | |
599 | * | |
600 | * From Appendix A: | |
601 | * | |
7690af3f ACM |
602 | * Of course, the circular buffer may overflow, either when the |
603 | * HC-Sender is sending data at a very high rate, when the | |
604 | * HC-Receiver's acknowledgements are not reaching the HC-Sender, | |
605 | * or when the HC-Sender is forgetting to acknowledge those acks | |
606 | * (so the HC-Receiver is unable to clean up old state). In this | |
607 | * case, the HC-Receiver should either compress the buffer (by | |
608 | * increasing run lengths when possible), transfer its state to | |
609 | * a larger buffer, or, as a last resort, drop all received | |
610 | * packets, without processing them whatsoever, until its buffer | |
611 | * shrinks again. | |
7c657876 ACM |
612 | */ |
613 | ||
614 | /* See if this is the first ackno being inserted */ | |
615 | if (ap->dccpap_buf_vector_len == 0) { | |
616 | ap->dccpap_buf[ap->dccpap_buf_head] = state; | |
617 | ap->dccpap_buf_vector_len = 1; | |
618 | } else if (after48(ackno, ap->dccpap_buf_ackno)) { | |
7690af3f ACM |
619 | const u64 delta = dccp_delta_seqno(ap->dccpap_buf_ackno, |
620 | ackno); | |
7c657876 ACM |
621 | |
622 | /* | |
7690af3f ACM |
623 | * Look if the state of this packet is the same as the |
624 | * previous ackno and if so if we can bump the head len. | |
7c657876 ACM |
625 | */ |
626 | if (delta == 1 && | |
627 | dccp_ackpkts_state(ap, ap->dccpap_buf_head) == state && | |
7690af3f ACM |
628 | (dccp_ackpkts_len(ap, ap->dccpap_buf_head) < |
629 | DCCP_ACKPKTS_LEN_MASK)) | |
7c657876 ACM |
630 | ap->dccpap_buf[ap->dccpap_buf_head]++; |
631 | else if (dccp_ackpkts_set_buf_head_state(ap, delta, state)) | |
632 | return -ENOBUFS; | |
633 | } else { | |
634 | /* | |
635 | * A.1.2. Old Packets | |
636 | * | |
7690af3f ACM |
637 | * When a packet with Sequence Number S arrives, and |
638 | * S <= buf_ackno, the HC-Receiver will scan the table | |
639 | * for the byte corresponding to S. (Indexing structures | |
640 | * could reduce the complexity of this scan.) | |
7c657876 ACM |
641 | */ |
642 | u64 delta = dccp_delta_seqno(ackno, ap->dccpap_buf_ackno); | |
643 | unsigned int index = ap->dccpap_buf_head; | |
644 | ||
645 | while (1) { | |
646 | const u8 len = dccp_ackpkts_len(ap, index); | |
647 | const u8 state = dccp_ackpkts_state(ap, index); | |
648 | /* | |
7690af3f ACM |
649 | * valid packets not yet in dccpap_buf have a reserved |
650 | * entry, with a len equal to 0. | |
7c657876 ACM |
651 | */ |
652 | if (state == DCCP_ACKPKTS_STATE_NOT_RECEIVED && | |
7690af3f ACM |
653 | len == 0 && delta == 0) { /* Found our |
654 | reserved seat! */ | |
f6ccf554 DM |
655 | dccp_pr_debug("Found %llu reserved seat!\n", |
656 | (unsigned long long) ackno); | |
7c657876 ACM |
657 | ap->dccpap_buf[index] = state; |
658 | goto out; | |
659 | } | |
660 | /* len == 0 means one packet */ | |
661 | if (delta < len + 1) | |
662 | goto out_duplicate; | |
663 | ||
664 | delta -= len + 1; | |
665 | if (++index == ap->dccpap_buf_len) | |
666 | index = 0; | |
667 | } | |
668 | } | |
669 | ||
670 | ap->dccpap_buf_ackno = ackno; | |
671 | ap->dccpap_time = jiffies; | |
672 | out: | |
673 | dccp_pr_debug(""); | |
674 | dccp_ackpkts_print(ap); | |
675 | return 0; | |
676 | ||
677 | out_duplicate: | |
678 | /* Duplicate packet */ | |
7690af3f ACM |
679 | dccp_pr_debug("Received a dup or already considered lost " |
680 | "packet: %llu\n", (unsigned long long) ackno); | |
7c657876 ACM |
681 | return -EILSEQ; |
682 | } | |
683 | ||
725ba8ee | 684 | #ifdef CONFIG_IP_DCCP_DEBUG |
7690af3f ACM |
685 | void dccp_ackvector_print(const u64 ackno, const unsigned char *vector, |
686 | int len) | |
7c657876 ACM |
687 | { |
688 | if (!dccp_debug) | |
689 | return; | |
690 | ||
f6ccf554 DM |
691 | printk("ACK vector len=%d, ackno=%llu |", len, |
692 | (unsigned long long) ackno); | |
7c657876 ACM |
693 | |
694 | while (len--) { | |
695 | const u8 state = (*vector & DCCP_ACKPKTS_STATE_MASK) >> 6; | |
696 | const u8 rl = (*vector & DCCP_ACKPKTS_LEN_MASK); | |
697 | ||
698 | printk("%d,%d|", state, rl); | |
699 | ++vector; | |
700 | } | |
701 | ||
702 | printk("\n"); | |
703 | } | |
704 | ||
705 | void dccp_ackpkts_print(const struct dccp_ackpkts *ap) | |
706 | { | |
707 | dccp_ackvector_print(ap->dccpap_buf_ackno, | |
708 | ap->dccpap_buf + ap->dccpap_buf_head, | |
709 | ap->dccpap_buf_vector_len); | |
710 | } | |
711 | #endif | |
712 | ||
713 | static void dccp_ackpkts_trow_away_ack_record(struct dccp_ackpkts *ap) | |
714 | { | |
715 | /* | |
716 | * As we're keeping track of the ack vector size | |
717 | * (dccpap_buf_vector_len) and the sent ack vector size | |
718 | * (dccpap_ack_vector_len) we don't need dccpap_buf_tail at all, but | |
7690af3f ACM |
719 | * keep this code here as in the future we'll implement a vector of |
720 | * ack records, as suggested in draft-ietf-dccp-spec-11.txt | |
721 | * Appendix A. -acme | |
7c657876 ACM |
722 | */ |
723 | #if 0 | |
724 | ap->dccpap_buf_tail = ap->dccpap_ack_ptr + 1; | |
725 | if (ap->dccpap_buf_tail >= ap->dccpap_buf_len) | |
726 | ap->dccpap_buf_tail -= ap->dccpap_buf_len; | |
727 | #endif | |
728 | ap->dccpap_buf_vector_len -= ap->dccpap_ack_vector_len; | |
729 | } | |
730 | ||
731 | void dccp_ackpkts_check_rcv_ackno(struct dccp_ackpkts *ap, struct sock *sk, | |
732 | u64 ackno) | |
733 | { | |
734 | /* Check if we actually sent an ACK vector */ | |
735 | if (ap->dccpap_ack_seqno == DCCP_MAX_SEQNO + 1) | |
736 | return; | |
737 | ||
738 | if (ackno == ap->dccpap_ack_seqno) { | |
725ba8ee | 739 | #ifdef CONFIG_IP_DCCP_DEBUG |
7c657876 | 740 | struct dccp_sock *dp = dccp_sk(sk); |
7690af3f ACM |
741 | const char *debug_prefix = dp->dccps_role == DCCP_ROLE_CLIENT ? |
742 | "CLIENT rx ack: " : "server rx ack: "; | |
7c657876 | 743 | #endif |
7690af3f ACM |
744 | dccp_pr_debug("%sACK packet 0, len=%d, ack_seqno=%llu, " |
745 | "ack_ackno=%llu, ACKED!\n", | |
7c657876 | 746 | debug_prefix, 1, |
f6ccf554 DM |
747 | (unsigned long long) ap->dccpap_ack_seqno, |
748 | (unsigned long long) ap->dccpap_ack_ackno); | |
7c657876 ACM |
749 | dccp_ackpkts_trow_away_ack_record(ap); |
750 | ap->dccpap_ack_seqno = DCCP_MAX_SEQNO + 1; | |
751 | } | |
752 | } | |
753 | ||
754 | static void dccp_ackpkts_check_rcv_ackvector(struct dccp_ackpkts *ap, | |
755 | struct sock *sk, u64 ackno, | |
756 | const unsigned char len, | |
757 | const unsigned char *vector) | |
758 | { | |
759 | unsigned char i; | |
760 | ||
761 | /* Check if we actually sent an ACK vector */ | |
762 | if (ap->dccpap_ack_seqno == DCCP_MAX_SEQNO + 1) | |
763 | return; | |
764 | /* | |
7690af3f ACM |
765 | * We're in the receiver half connection, so if the received an ACK |
766 | * vector ackno (e.g. 50) before dccpap_ack_seqno (e.g. 52), we're | |
767 | * not interested. | |
7c657876 ACM |
768 | * |
769 | * Extra explanation with example: | |
770 | * | |
771 | * if we received an ACK vector with ackno 50, it can only be acking | |
772 | * 50, 49, 48, etc, not 52 (the seqno for the ACK vector we sent). | |
773 | */ | |
7690af3f | 774 | /* dccp_pr_debug("is %llu < %llu? ", ackno, ap->dccpap_ack_seqno); */ |
7c657876 | 775 | if (before48(ackno, ap->dccpap_ack_seqno)) { |
7690af3f | 776 | /* dccp_pr_debug_cat("yes\n"); */ |
7c657876 ACM |
777 | return; |
778 | } | |
7690af3f | 779 | /* dccp_pr_debug_cat("no\n"); */ |
7c657876 ACM |
780 | |
781 | i = len; | |
782 | while (i--) { | |
783 | const u8 rl = (*vector & DCCP_ACKPKTS_LEN_MASK); | |
784 | u64 ackno_end_rl; | |
785 | ||
786 | dccp_set_seqno(&ackno_end_rl, ackno - rl); | |
787 | ||
7690af3f ACM |
788 | /* |
789 | * dccp_pr_debug("is %llu <= %llu <= %llu? ", ackno_end_rl, | |
790 | * ap->dccpap_ack_seqno, ackno); | |
791 | */ | |
7c657876 | 792 | if (between48(ap->dccpap_ack_seqno, ackno_end_rl, ackno)) { |
7690af3f ACM |
793 | const u8 state = (*vector & |
794 | DCCP_ACKPKTS_STATE_MASK) >> 6; | |
795 | /* dccp_pr_debug_cat("yes\n"); */ | |
7c657876 ACM |
796 | |
797 | if (state != DCCP_ACKPKTS_STATE_NOT_RECEIVED) { | |
725ba8ee | 798 | #ifdef CONFIG_IP_DCCP_DEBUG |
7c657876 | 799 | struct dccp_sock *dp = dccp_sk(sk); |
7690af3f ACM |
800 | const char *debug_prefix = |
801 | dp->dccps_role == DCCP_ROLE_CLIENT ? | |
802 | "CLIENT rx ack: " : "server rx ack: "; | |
7c657876 | 803 | #endif |
7690af3f ACM |
804 | dccp_pr_debug("%sACK vector 0, len=%d, " |
805 | "ack_seqno=%llu, ack_ackno=%llu, " | |
806 | "ACKED!\n", | |
7c657876 | 807 | debug_prefix, len, |
f6ccf554 DM |
808 | (unsigned long long) |
809 | ap->dccpap_ack_seqno, | |
810 | (unsigned long long) | |
811 | ap->dccpap_ack_ackno); | |
7c657876 ACM |
812 | dccp_ackpkts_trow_away_ack_record(ap); |
813 | } | |
814 | /* | |
7690af3f ACM |
815 | * If dccpap_ack_seqno was not received, no problem |
816 | * we'll send another ACK vector. | |
7c657876 ACM |
817 | */ |
818 | ap->dccpap_ack_seqno = DCCP_MAX_SEQNO + 1; | |
819 | break; | |
820 | } | |
7690af3f | 821 | /* dccp_pr_debug_cat("no\n"); */ |
7c657876 ACM |
822 | |
823 | dccp_set_seqno(&ackno, ackno_end_rl - 1); | |
824 | ++vector; | |
825 | } | |
826 | } |