blob: fc363aaeedaf96175fc65a303d666b3cb097ccf9 [file] [log] [blame]
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001/*
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
23static 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 */
30static 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
37void dccp_options_init(struct dccp_options *dccpo)
38{
39 memcpy(dccpo, &dccpo_default_values, sizeof(*dccpo));
40}
41
42static 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
58int dccp_parse_options(struct sock *sk, struct sk_buff *skb)
59{
60 struct dccp_sock *dp = dccp_sk(sk);
Arnaldo Carvalho de Melo725ba8e2005-08-13 20:35:39 -030061#ifdef CONFIG_IP_DCCP_DEBUG
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -030062 const char *debug_prefix = dp->dccps_role == DCCP_ROLE_CLIENT ?
63 "CLIENT rx opt: " : "server rx opt: ";
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070064#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;
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -030069 const unsigned char *opt_end = (unsigned char *)dh +
70 (dh->dccph_doff * 4);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070071 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);
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300110 dccp_pr_debug("%sNDP count=%d\n", debug_prefix,
111 opt_recv->dccpor_ndp);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700112 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",
David S. Millerf6ccf552005-08-09 20:27:14 -0700124 debug_prefix, len,
125 (unsigned long long)
126 DCCP_SKB_CB(skb)->dccpd_ack_seq);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700127 dccp_ackvector_print(DCCP_SKB_CB(skb)->dccpd_ack_seq,
128 value, len);
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300129 dccp_ackpkts_check_rcv_ackvector(dp->dccps_hc_rx_ackpkts,
130 sk,
131 DCCP_SKB_CB(skb)->dccpd_ack_seq,
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700132 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,
David S. Millerf6ccf552005-08-09 20:27:14 -0700145 (unsigned long long)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700146 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
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300154 dccp_pr_debug("%sTIMESTAMP_ECHO=%u, len=%d, ackno=%llu, "
155 "diff=%u\n",
156 debug_prefix,
157 opt_recv->dccpor_timestamp_echo,
David S. Millerf6ccf552005-08-09 20:27:14 -0700158 len + 2,
159 (unsigned long long)
160 DCCP_SKB_CB(skb)->dccpd_ack_seq,
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300161 (tcp_time_stamp -
162 opt_recv->dccpor_timestamp_echo));
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700163
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300164 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,
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700169 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;
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300177 opt_recv->dccpor_elapsed_time =
178 dccp_decode_value_var(value, len);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700179 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 *
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300185 * 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.
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700190 */
191 case 128 ... 191: {
192 const u16 idx = value - options;
193
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300194 if (ccid_hc_rx_parse_options(dp->dccps_hc_rx_ccid, sk,
195 opt, len, idx,
196 value) != 0)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700197 goto out_invalid_option;
198 }
199 break;
200 case 192 ... 255: {
201 const u16 idx = value - options;
202
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300203 if (ccid_hc_tx_parse_options(dp->dccps_hc_tx_ccid, sk,
204 opt, len, idx,
205 value) != 0)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700206 goto out_invalid_option;
207 }
208 break;
209 default:
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300210 pr_info("DCCP(%p): option %d(len=%d) not "
211 "implemented, ignoring\n",
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700212 sk, opt, len);
213 break;
214 }
215 }
216
217 return 0;
218
219out_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
226static 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
239static inline int dccp_ndp_len(const int ndp)
240{
241 return likely(ndp <= 0xFF) ? 1 : ndp <= 0xFFFF ? 2 : 3;
242}
243
244void 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) {
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300251 LIMIT_NETDEBUG(KERN_INFO "DCCP: packet too small to insert "
252 "%d option!\n", option);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700253 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
265EXPORT_SYMBOL_GPL(dccp_insert_option);
266
267static 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
294static 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
302void dccp_insert_option_elapsed_time(struct sock *sk,
303 struct sk_buff *skb,
304 u32 elapsed_time)
305{
Arnaldo Carvalho de Melo725ba8e2005-08-13 20:35:39 -0300306#ifdef CONFIG_IP_DCCP_DEBUG
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700307 struct dccp_sock *dp = dccp_sk(sk);
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300308 const char *debug_prefix = dp->dccps_role == DCCP_ROLE_CLIENT ?
309 "CLIENT TX opt: " : "server TX opt: ";
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700310#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) {
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300320 LIMIT_NETDEBUG(KERN_INFO "DCCP: packet too small to "
321 "insert elapsed time!\n");
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700322 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,
David S. Millerf6ccf552005-08-09 20:27:14 -0700335 len,
336 (unsigned long long) DCCP_SKB_CB(skb)->dccpd_seq);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700337}
338
339EXPORT_SYMBOL(dccp_insert_option_elapsed_time);
340
341static void dccp_insert_option_ack_vector(struct sock *sk, struct sk_buff *skb)
342{
343 struct dccp_sock *dp = dccp_sk(sk);
Arnaldo Carvalho de Melo725ba8e2005-08-13 20:35:39 -0300344#ifdef CONFIG_IP_DCCP_DEBUG
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300345 const char *debug_prefix = dp->dccps_role == DCCP_ROLE_CLIENT ?
346 "CLIENT TX opt: " : "server TX opt: ";
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700347#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) {
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300357 LIMIT_NETDEBUG(KERN_INFO "DCCP: packet too small to "
358 "insert ACK Vector!\n");
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700359 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) {
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300383 const unsigned int tailsize = (ap->dccpap_buf_len -
384 ap->dccpap_buf_head);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700385
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
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300399 * buf_head; ack_ackno will equal buf_ackno; and ack_nonce will
400 * equal buf_nonce.
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700401 *
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
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300410 dccp_pr_debug("%sACK Vector 0, len=%d, ack_seqno=%llu, "
411 "ack_ackno=%llu\n",
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700412 debug_prefix, ap->dccpap_ack_vector_len,
David S. Millerf6ccf552005-08-09 20:27:14 -0700413 (unsigned long long) ap->dccpap_ack_seqno,
414 (unsigned long long) ap->dccpap_ack_ackno);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700415}
416
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300417static inline void dccp_insert_option_timestamp(struct sock *sk,
418 struct sk_buff *skb)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700419{
420 const u32 now = htonl(tcp_time_stamp);
421 dccp_insert_option(sk, skb, DCCPO_TIMESTAMP, &now, sizeof(now));
422}
423
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300424static void dccp_insert_option_timestamp_echo(struct sock *sk,
425 struct sk_buff *skb)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700426{
427 struct dccp_sock *dp = dccp_sk(sk);
Arnaldo Carvalho de Melo725ba8e2005-08-13 20:35:39 -0300428#ifdef CONFIG_IP_DCCP_DEBUG
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300429 const char *debug_prefix = dp->dccps_role == DCCP_ROLE_CLIENT ?
430 "CLIENT TX opt: " : "server TX opt: ";
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700431#endif
432 u32 tstamp_echo;
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300433 const u32 elapsed_time = jiffies_to_usecs(jiffies -
434 dp->dccps_timestamp_time) / 10;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700435 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) {
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300440 LIMIT_NETDEBUG(KERN_INFO "DCCP: packet too small to insert "
441 "timestamp echo!\n");
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700442 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,
David S. Millerf6ccf552005-08-09 20:27:14 -0700458 len,
459 (unsigned long long) DCCP_SKB_CB(skb)->dccpd_seq);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700460
461 dp->dccps_timestamp_echo = 0;
462 dp->dccps_timestamp_time = 0;
463}
464
465void 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 &&
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300476 (dp->dccps_hc_rx_ackpkts->dccpap_buf_ackno !=
477 DCCP_MAX_SEQNO + 1))
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700478 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
502struct dccp_ackpkts *dccp_ackpkts_alloc(unsigned int len, int priority)
503{
504 struct dccp_ackpkts *ap = kmalloc(sizeof(*ap) + len, priority);
505
506 if (ap != NULL) {
Arnaldo Carvalho de Melo725ba8e2005-08-13 20:35:39 -0300507#ifdef CONFIG_IP_DCCP_DEBUG
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700508 memset(ap->dccpap_buf, 0xFF, len);
509#endif
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300510 ap->dccpap_buf_len = len;
511 ap->dccpap_buf_head =
512 ap->dccpap_buf_tail =
513 ap->dccpap_buf_len - 1;
514 ap->dccpap_buf_ackno =
515 ap->dccpap_ack_ackno =
516 ap->dccpap_ack_seqno = DCCP_MAX_SEQNO + 1;
517 ap->dccpap_buf_nonce = ap->dccpap_buf_nonce = 0;
518 ap->dccpap_ack_ptr = 0;
519 ap->dccpap_time = 0;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700520 ap->dccpap_buf_vector_len = ap->dccpap_ack_vector_len = 0;
521 }
522
523 return ap;
524}
525
526void dccp_ackpkts_free(struct dccp_ackpkts *ap)
527{
528 if (ap != NULL) {
Arnaldo Carvalho de Melo725ba8e2005-08-13 20:35:39 -0300529#ifdef CONFIG_IP_DCCP_DEBUG
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700530 memset(ap, 0xFF, sizeof(*ap) + ap->dccpap_buf_len);
531#endif
532 kfree(ap);
533 }
534}
535
536static inline u8 dccp_ackpkts_state(const struct dccp_ackpkts *ap,
537 const unsigned int index)
538{
539 return ap->dccpap_buf[index] & DCCP_ACKPKTS_STATE_MASK;
540}
541
542static inline u8 dccp_ackpkts_len(const struct dccp_ackpkts *ap,
543 const unsigned int index)
544{
545 return ap->dccpap_buf[index] & DCCP_ACKPKTS_LEN_MASK;
546}
547
548/*
549 * If several packets are missing, the HC-Receiver may prefer to enter multiple
550 * bytes with run length 0, rather than a single byte with a larger run length;
551 * this simplifies table updates if one of the missing packets arrives.
552 */
553static inline int dccp_ackpkts_set_buf_head_state(struct dccp_ackpkts *ap,
554 const unsigned int packets,
555 const unsigned char state)
556{
557 unsigned int gap;
558 signed long new_head;
559
560 if (ap->dccpap_buf_vector_len + packets > ap->dccpap_buf_len)
561 return -ENOBUFS;
562
563 gap = packets - 1;
564 new_head = ap->dccpap_buf_head - packets;
565
566 if (new_head < 0) {
567 if (gap > 0) {
568 memset(ap->dccpap_buf, DCCP_ACKPKTS_STATE_NOT_RECEIVED,
569 gap + new_head + 1);
570 gap = -new_head;
571 }
572 new_head += ap->dccpap_buf_len;
573 }
574
575 ap->dccpap_buf_head = new_head;
576
577 if (gap > 0)
578 memset(ap->dccpap_buf + ap->dccpap_buf_head + 1,
579 DCCP_ACKPKTS_STATE_NOT_RECEIVED, gap);
580
581 ap->dccpap_buf[ap->dccpap_buf_head] = state;
582 ap->dccpap_buf_vector_len += packets;
583 return 0;
584}
585
586/*
587 * Implements the draft-ietf-dccp-spec-11.txt Appendix A
588 */
589int dccp_ackpkts_add(struct dccp_ackpkts *ap, u64 ackno, u8 state)
590{
591 /*
592 * Check at the right places if the buffer is full, if it is, tell the
593 * caller to start dropping packets till the HC-Sender acks our ACK
594 * vectors, when we will free up space in dccpap_buf.
595 *
596 * We may well decide to do buffer compression, etc, but for now lets
597 * just drop.
598 *
599 * From Appendix A:
600 *
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300601 * Of course, the circular buffer may overflow, either when the
602 * HC-Sender is sending data at a very high rate, when the
603 * HC-Receiver's acknowledgements are not reaching the HC-Sender,
604 * or when the HC-Sender is forgetting to acknowledge those acks
605 * (so the HC-Receiver is unable to clean up old state). In this
606 * case, the HC-Receiver should either compress the buffer (by
607 * increasing run lengths when possible), transfer its state to
608 * a larger buffer, or, as a last resort, drop all received
609 * packets, without processing them whatsoever, until its buffer
610 * shrinks again.
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700611 */
612
613 /* See if this is the first ackno being inserted */
614 if (ap->dccpap_buf_vector_len == 0) {
615 ap->dccpap_buf[ap->dccpap_buf_head] = state;
616 ap->dccpap_buf_vector_len = 1;
617 } else if (after48(ackno, ap->dccpap_buf_ackno)) {
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300618 const u64 delta = dccp_delta_seqno(ap->dccpap_buf_ackno,
619 ackno);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700620
621 /*
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300622 * Look if the state of this packet is the same as the
623 * previous ackno and if so if we can bump the head len.
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700624 */
625 if (delta == 1 &&
626 dccp_ackpkts_state(ap, ap->dccpap_buf_head) == state &&
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300627 (dccp_ackpkts_len(ap, ap->dccpap_buf_head) <
628 DCCP_ACKPKTS_LEN_MASK))
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700629 ap->dccpap_buf[ap->dccpap_buf_head]++;
630 else if (dccp_ackpkts_set_buf_head_state(ap, delta, state))
631 return -ENOBUFS;
632 } else {
633 /*
634 * A.1.2. Old Packets
635 *
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300636 * When a packet with Sequence Number S arrives, and
637 * S <= buf_ackno, the HC-Receiver will scan the table
638 * for the byte corresponding to S. (Indexing structures
639 * could reduce the complexity of this scan.)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700640 */
641 u64 delta = dccp_delta_seqno(ackno, ap->dccpap_buf_ackno);
642 unsigned int index = ap->dccpap_buf_head;
643
644 while (1) {
645 const u8 len = dccp_ackpkts_len(ap, index);
646 const u8 state = dccp_ackpkts_state(ap, index);
647 /*
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300648 * valid packets not yet in dccpap_buf have a reserved
649 * entry, with a len equal to 0.
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700650 */
651 if (state == DCCP_ACKPKTS_STATE_NOT_RECEIVED &&
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300652 len == 0 && delta == 0) { /* Found our
653 reserved seat! */
David S. Millerf6ccf552005-08-09 20:27:14 -0700654 dccp_pr_debug("Found %llu reserved seat!\n",
655 (unsigned long long) ackno);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700656 ap->dccpap_buf[index] = state;
657 goto out;
658 }
659 /* len == 0 means one packet */
660 if (delta < len + 1)
661 goto out_duplicate;
662
663 delta -= len + 1;
664 if (++index == ap->dccpap_buf_len)
665 index = 0;
666 }
667 }
668
669 ap->dccpap_buf_ackno = ackno;
670 ap->dccpap_time = jiffies;
671out:
672 dccp_pr_debug("");
673 dccp_ackpkts_print(ap);
674 return 0;
675
676out_duplicate:
677 /* Duplicate packet */
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300678 dccp_pr_debug("Received a dup or already considered lost "
679 "packet: %llu\n", (unsigned long long) ackno);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700680 return -EILSEQ;
681}
682
Arnaldo Carvalho de Melo725ba8e2005-08-13 20:35:39 -0300683#ifdef CONFIG_IP_DCCP_DEBUG
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300684void dccp_ackvector_print(const u64 ackno, const unsigned char *vector,
685 int len)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700686{
687 if (!dccp_debug)
688 return;
689
David S. Millerf6ccf552005-08-09 20:27:14 -0700690 printk("ACK vector len=%d, ackno=%llu |", len,
691 (unsigned long long) ackno);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700692
693 while (len--) {
694 const u8 state = (*vector & DCCP_ACKPKTS_STATE_MASK) >> 6;
695 const u8 rl = (*vector & DCCP_ACKPKTS_LEN_MASK);
696
697 printk("%d,%d|", state, rl);
698 ++vector;
699 }
700
701 printk("\n");
702}
703
704void dccp_ackpkts_print(const struct dccp_ackpkts *ap)
705{
706 dccp_ackvector_print(ap->dccpap_buf_ackno,
707 ap->dccpap_buf + ap->dccpap_buf_head,
708 ap->dccpap_buf_vector_len);
709}
710#endif
711
712static void dccp_ackpkts_trow_away_ack_record(struct dccp_ackpkts *ap)
713{
714 /*
715 * As we're keeping track of the ack vector size
716 * (dccpap_buf_vector_len) and the sent ack vector size
717 * (dccpap_ack_vector_len) we don't need dccpap_buf_tail at all, but
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300718 * keep this code here as in the future we'll implement a vector of
719 * ack records, as suggested in draft-ietf-dccp-spec-11.txt
720 * Appendix A. -acme
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700721 */
722#if 0
723 ap->dccpap_buf_tail = ap->dccpap_ack_ptr + 1;
724 if (ap->dccpap_buf_tail >= ap->dccpap_buf_len)
725 ap->dccpap_buf_tail -= ap->dccpap_buf_len;
726#endif
727 ap->dccpap_buf_vector_len -= ap->dccpap_ack_vector_len;
728}
729
730void dccp_ackpkts_check_rcv_ackno(struct dccp_ackpkts *ap, struct sock *sk,
731 u64 ackno)
732{
733 /* Check if we actually sent an ACK vector */
734 if (ap->dccpap_ack_seqno == DCCP_MAX_SEQNO + 1)
735 return;
736
737 if (ackno == ap->dccpap_ack_seqno) {
Arnaldo Carvalho de Melo725ba8e2005-08-13 20:35:39 -0300738#ifdef CONFIG_IP_DCCP_DEBUG
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700739 struct dccp_sock *dp = dccp_sk(sk);
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300740 const char *debug_prefix = dp->dccps_role == DCCP_ROLE_CLIENT ?
741 "CLIENT rx ack: " : "server rx ack: ";
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700742#endif
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300743 dccp_pr_debug("%sACK packet 0, len=%d, ack_seqno=%llu, "
744 "ack_ackno=%llu, ACKED!\n",
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700745 debug_prefix, 1,
David S. Millerf6ccf552005-08-09 20:27:14 -0700746 (unsigned long long) ap->dccpap_ack_seqno,
747 (unsigned long long) ap->dccpap_ack_ackno);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700748 dccp_ackpkts_trow_away_ack_record(ap);
749 ap->dccpap_ack_seqno = DCCP_MAX_SEQNO + 1;
750 }
751}
752
753static void dccp_ackpkts_check_rcv_ackvector(struct dccp_ackpkts *ap,
754 struct sock *sk, u64 ackno,
755 const unsigned char len,
756 const unsigned char *vector)
757{
758 unsigned char i;
759
760 /* Check if we actually sent an ACK vector */
761 if (ap->dccpap_ack_seqno == DCCP_MAX_SEQNO + 1)
762 return;
763 /*
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300764 * We're in the receiver half connection, so if the received an ACK
765 * vector ackno (e.g. 50) before dccpap_ack_seqno (e.g. 52), we're
766 * not interested.
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700767 *
768 * Extra explanation with example:
769 *
770 * if we received an ACK vector with ackno 50, it can only be acking
771 * 50, 49, 48, etc, not 52 (the seqno for the ACK vector we sent).
772 */
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300773 /* dccp_pr_debug("is %llu < %llu? ", ackno, ap->dccpap_ack_seqno); */
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700774 if (before48(ackno, ap->dccpap_ack_seqno)) {
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300775 /* dccp_pr_debug_cat("yes\n"); */
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700776 return;
777 }
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300778 /* dccp_pr_debug_cat("no\n"); */
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700779
780 i = len;
781 while (i--) {
782 const u8 rl = (*vector & DCCP_ACKPKTS_LEN_MASK);
783 u64 ackno_end_rl;
784
785 dccp_set_seqno(&ackno_end_rl, ackno - rl);
786
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300787 /*
788 * dccp_pr_debug("is %llu <= %llu <= %llu? ", ackno_end_rl,
789 * ap->dccpap_ack_seqno, ackno);
790 */
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700791 if (between48(ap->dccpap_ack_seqno, ackno_end_rl, ackno)) {
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300792 const u8 state = (*vector &
793 DCCP_ACKPKTS_STATE_MASK) >> 6;
794 /* dccp_pr_debug_cat("yes\n"); */
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700795
796 if (state != DCCP_ACKPKTS_STATE_NOT_RECEIVED) {
Arnaldo Carvalho de Melo725ba8e2005-08-13 20:35:39 -0300797#ifdef CONFIG_IP_DCCP_DEBUG
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700798 struct dccp_sock *dp = dccp_sk(sk);
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300799 const char *debug_prefix =
800 dp->dccps_role == DCCP_ROLE_CLIENT ?
801 "CLIENT rx ack: " : "server rx ack: ";
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700802#endif
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300803 dccp_pr_debug("%sACK vector 0, len=%d, "
804 "ack_seqno=%llu, ack_ackno=%llu, "
805 "ACKED!\n",
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700806 debug_prefix, len,
David S. Millerf6ccf552005-08-09 20:27:14 -0700807 (unsigned long long)
808 ap->dccpap_ack_seqno,
809 (unsigned long long)
810 ap->dccpap_ack_ackno);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700811 dccp_ackpkts_trow_away_ack_record(ap);
812 }
813 /*
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300814 * If dccpap_ack_seqno was not received, no problem
815 * we'll send another ACK vector.
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700816 */
817 ap->dccpap_ack_seqno = DCCP_MAX_SEQNO + 1;
818 break;
819 }
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300820 /* dccp_pr_debug_cat("no\n"); */
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700821
822 dccp_set_seqno(&ackno, ackno_end_rl - 1);
823 ++vector;
824 }
825}