blob: 34b230a00875906176c26af4af1a3d094b9ed8a4 [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
Ian McDonald1bc09862005-08-20 00:23:43 -03005 * Copyright (c) 2005 Aristeu Sergio Rozanski Filho <aris@cathedrallabs.org>
6 * Copyright (c) 2005 Arnaldo Carvalho de Melo <acme@ghostprotocols.net>
7 * Copyright (c) 2005 Ian McDonald <iam4@cs.waikato.ac.nz>
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07008 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
13 */
14#include <linux/config.h>
15#include <linux/dccp.h>
16#include <linux/module.h>
17#include <linux/types.h>
18#include <linux/kernel.h>
19#include <linux/skbuff.h>
20
21#include "ccid.h"
22#include "dccp.h"
23
24static void dccp_ackpkts_check_rcv_ackvector(struct dccp_ackpkts *ap,
25 struct sock *sk,
26 const u64 ackno,
27 const unsigned char len,
28 const unsigned char *vector);
29
30/* stores the default values for new connection. may be changed with sysctl */
31static const struct dccp_options dccpo_default_values = {
32 .dccpo_sequence_window = DCCPF_INITIAL_SEQUENCE_WINDOW,
33 .dccpo_ccid = DCCPF_INITIAL_CCID,
34 .dccpo_send_ack_vector = DCCPF_INITIAL_SEND_ACK_VECTOR,
35 .dccpo_send_ndp_count = DCCPF_INITIAL_SEND_NDP_COUNT,
36};
37
38void dccp_options_init(struct dccp_options *dccpo)
39{
40 memcpy(dccpo, &dccpo_default_values, sizeof(*dccpo));
41}
42
43static u32 dccp_decode_value_var(const unsigned char *bf, const u8 len)
44{
45 u32 value = 0;
46
47 if (len > 3)
48 value += *bf++ << 24;
49 if (len > 2)
50 value += *bf++ << 16;
51 if (len > 1)
52 value += *bf++ << 8;
53 if (len > 0)
54 value += *bf;
55
56 return value;
57}
58
59int dccp_parse_options(struct sock *sk, struct sk_buff *skb)
60{
61 struct dccp_sock *dp = dccp_sk(sk);
Arnaldo Carvalho de Melo725ba8e2005-08-13 20:35:39 -030062#ifdef CONFIG_IP_DCCP_DEBUG
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -030063 const char *debug_prefix = dp->dccps_role == DCCP_ROLE_CLIENT ?
64 "CLIENT rx opt: " : "server rx opt: ";
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070065#endif
66 const struct dccp_hdr *dh = dccp_hdr(skb);
67 const u8 pkt_type = DCCP_SKB_CB(skb)->dccpd_type;
68 unsigned char *options = (unsigned char *)dh + dccp_hdr_len(skb);
69 unsigned char *opt_ptr = options;
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -030070 const unsigned char *opt_end = (unsigned char *)dh +
71 (dh->dccph_doff * 4);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070072 struct dccp_options_received *opt_recv = &dp->dccps_options_received;
73 unsigned char opt, len;
74 unsigned char *value;
Arnaldo Carvalho de Melo1c14ac02005-09-09 02:32:01 -030075 u32 elapsed_time;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070076
77 memset(opt_recv, 0, sizeof(*opt_recv));
78
79 while (opt_ptr != opt_end) {
80 opt = *opt_ptr++;
81 len = 0;
82 value = NULL;
83
84 /* Check if this isn't a single byte option */
85 if (opt > DCCPO_MAX_RESERVED) {
86 if (opt_ptr == opt_end)
87 goto out_invalid_option;
88
89 len = *opt_ptr++;
90 if (len < 3)
91 goto out_invalid_option;
92 /*
93 * Remove the type and len fields, leaving
94 * just the value size
95 */
96 len -= 2;
97 value = opt_ptr;
98 opt_ptr += len;
99
100 if (opt_ptr > opt_end)
101 goto out_invalid_option;
102 }
103
104 switch (opt) {
105 case DCCPO_PADDING:
106 break;
107 case DCCPO_NDP_COUNT:
108 if (len > 3)
109 goto out_invalid_option;
110
111 opt_recv->dccpor_ndp = dccp_decode_value_var(value, len);
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300112 dccp_pr_debug("%sNDP count=%d\n", debug_prefix,
113 opt_recv->dccpor_ndp);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700114 break;
115 case DCCPO_ACK_VECTOR_0:
116 if (len > DCCP_MAX_ACK_VECTOR_LEN)
117 goto out_invalid_option;
118
119 if (pkt_type == DCCP_PKT_DATA)
120 continue;
121
122 opt_recv->dccpor_ack_vector_len = len;
123 opt_recv->dccpor_ack_vector_idx = value - options;
124
125 dccp_pr_debug("%sACK vector 0, len=%d, ack_ackno=%llu\n",
David S. Millerf6ccf552005-08-09 20:27:14 -0700126 debug_prefix, len,
127 (unsigned long long)
128 DCCP_SKB_CB(skb)->dccpd_ack_seq);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700129 dccp_ackvector_print(DCCP_SKB_CB(skb)->dccpd_ack_seq,
130 value, len);
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300131 dccp_ackpkts_check_rcv_ackvector(dp->dccps_hc_rx_ackpkts,
132 sk,
133 DCCP_SKB_CB(skb)->dccpd_ack_seq,
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700134 len, value);
135 break;
136 case DCCPO_TIMESTAMP:
137 if (len != 4)
138 goto out_invalid_option;
139
140 opt_recv->dccpor_timestamp = ntohl(*(u32 *)value);
141
142 dp->dccps_timestamp_echo = opt_recv->dccpor_timestamp;
Ian McDonald1bc09862005-08-20 00:23:43 -0300143 do_gettimeofday(&dp->dccps_timestamp_time);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700144
145 dccp_pr_debug("%sTIMESTAMP=%u, ackno=%llu\n",
146 debug_prefix, opt_recv->dccpor_timestamp,
David S. Millerf6ccf552005-08-09 20:27:14 -0700147 (unsigned long long)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700148 DCCP_SKB_CB(skb)->dccpd_ack_seq);
149 break;
150 case DCCPO_TIMESTAMP_ECHO:
Ian McDonald1bc09862005-08-20 00:23:43 -0300151 if (len != 4 && len != 6 && len != 8)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700152 goto out_invalid_option;
153
154 opt_recv->dccpor_timestamp_echo = ntohl(*(u32 *)value);
155
Ian McDonald1bc09862005-08-20 00:23:43 -0300156 dccp_pr_debug("%sTIMESTAMP_ECHO=%u, len=%d, ackno=%llu, ",
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300157 debug_prefix,
158 opt_recv->dccpor_timestamp_echo,
David S. Millerf6ccf552005-08-09 20:27:14 -0700159 len + 2,
160 (unsigned long long)
Ian McDonald1bc09862005-08-20 00:23:43 -0300161 DCCP_SKB_CB(skb)->dccpd_ack_seq);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700162
Ian McDonald1bc09862005-08-20 00:23:43 -0300163
Arnaldo Carvalho de Melo1c14ac02005-09-09 02:32:01 -0300164 if (len == 4)
165 break;
166
167 if (len == 6)
168 elapsed_time = ntohs(*(u16 *)(value + 4));
169 else
170 elapsed_time = ntohl(*(u32 *)(value + 4));
171
172 /* Give precedence to the biggest ELAPSED_TIME */
173 if (elapsed_time > opt_recv->dccpor_elapsed_time)
174 opt_recv->dccpor_elapsed_time = elapsed_time;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700175 break;
176 case DCCPO_ELAPSED_TIME:
Ian McDonald1bc09862005-08-20 00:23:43 -0300177 if (len != 2 && len != 4)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700178 goto out_invalid_option;
179
180 if (pkt_type == DCCP_PKT_DATA)
181 continue;
Ian McDonald1bc09862005-08-20 00:23:43 -0300182
183 if (len == 2)
Arnaldo Carvalho de Melo1c14ac02005-09-09 02:32:01 -0300184 elapsed_time = ntohs(*(u16 *)value);
Ian McDonald1bc09862005-08-20 00:23:43 -0300185 else
Arnaldo Carvalho de Melo1c14ac02005-09-09 02:32:01 -0300186 elapsed_time = ntohl(*(u32 *)value);
187
188 if (elapsed_time > opt_recv->dccpor_elapsed_time)
189 opt_recv->dccpor_elapsed_time = elapsed_time;
Ian McDonald1bc09862005-08-20 00:23:43 -0300190
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700191 dccp_pr_debug("%sELAPSED_TIME=%d\n", debug_prefix,
Arnaldo Carvalho de Melo1c14ac02005-09-09 02:32:01 -0300192 elapsed_time);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700193 break;
194 /*
195 * From draft-ietf-dccp-spec-11.txt:
196 *
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300197 * Option numbers 128 through 191 are for
198 * options sent from the HC-Sender to the
199 * HC-Receiver; option numbers 192 through 255
200 * are for options sent from the HC-Receiver to
201 * the HC-Sender.
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700202 */
203 case 128 ... 191: {
204 const u16 idx = value - options;
205
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300206 if (ccid_hc_rx_parse_options(dp->dccps_hc_rx_ccid, sk,
207 opt, len, idx,
208 value) != 0)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700209 goto out_invalid_option;
210 }
211 break;
212 case 192 ... 255: {
213 const u16 idx = value - options;
214
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300215 if (ccid_hc_tx_parse_options(dp->dccps_hc_tx_ccid, sk,
216 opt, len, idx,
217 value) != 0)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700218 goto out_invalid_option;
219 }
220 break;
221 default:
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300222 pr_info("DCCP(%p): option %d(len=%d) not "
223 "implemented, ignoring\n",
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700224 sk, opt, len);
225 break;
226 }
227 }
228
229 return 0;
230
231out_invalid_option:
232 DCCP_INC_STATS_BH(DCCP_MIB_INVALIDOPT);
233 DCCP_SKB_CB(skb)->dccpd_reset_code = DCCP_RESET_CODE_OPTION_ERROR;
234 pr_info("DCCP(%p): invalid option %d, len=%d\n", sk, opt, len);
235 return -1;
236}
237
238static void dccp_encode_value_var(const u32 value, unsigned char *to,
239 const unsigned int len)
240{
241 if (len > 3)
242 *to++ = (value & 0xFF000000) >> 24;
243 if (len > 2)
244 *to++ = (value & 0xFF0000) >> 16;
245 if (len > 1)
246 *to++ = (value & 0xFF00) >> 8;
247 if (len > 0)
248 *to++ = (value & 0xFF);
249}
250
251static inline int dccp_ndp_len(const int ndp)
252{
253 return likely(ndp <= 0xFF) ? 1 : ndp <= 0xFFFF ? 2 : 3;
254}
255
256void dccp_insert_option(struct sock *sk, struct sk_buff *skb,
257 const unsigned char option,
258 const void *value, const unsigned char len)
259{
260 unsigned char *to;
261
262 if (DCCP_SKB_CB(skb)->dccpd_opt_len + len + 2 > DCCP_MAX_OPT_LEN) {
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300263 LIMIT_NETDEBUG(KERN_INFO "DCCP: packet too small to insert "
264 "%d option!\n", option);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700265 return;
266 }
267
268 DCCP_SKB_CB(skb)->dccpd_opt_len += len + 2;
269
270 to = skb_push(skb, len + 2);
271 *to++ = option;
272 *to++ = len + 2;
273
274 memcpy(to, value, len);
275}
276
277EXPORT_SYMBOL_GPL(dccp_insert_option);
278
279static void dccp_insert_option_ndp(struct sock *sk, struct sk_buff *skb)
280{
281 struct dccp_sock *dp = dccp_sk(sk);
282 int ndp = dp->dccps_ndp_count;
283
284 if (dccp_non_data_packet(skb))
285 ++dp->dccps_ndp_count;
286 else
287 dp->dccps_ndp_count = 0;
288
289 if (ndp > 0) {
290 unsigned char *ptr;
291 const int ndp_len = dccp_ndp_len(ndp);
292 const int len = ndp_len + 2;
293
294 if (DCCP_SKB_CB(skb)->dccpd_opt_len + len > DCCP_MAX_OPT_LEN)
295 return;
296
297 DCCP_SKB_CB(skb)->dccpd_opt_len += len;
298
299 ptr = skb_push(skb, len);
300 *ptr++ = DCCPO_NDP_COUNT;
301 *ptr++ = len;
302 dccp_encode_value_var(ndp, ptr, ndp_len);
303 }
304}
305
306static inline int dccp_elapsed_time_len(const u32 elapsed_time)
307{
Ian McDonaldb1c9fe72005-08-18 20:45:29 -0300308 return elapsed_time == 0 ? 0 : elapsed_time <= 0xFFFF ? 2 : 4;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700309}
310
311void dccp_insert_option_elapsed_time(struct sock *sk,
312 struct sk_buff *skb,
313 u32 elapsed_time)
314{
Arnaldo Carvalho de Melo725ba8e2005-08-13 20:35:39 -0300315#ifdef CONFIG_IP_DCCP_DEBUG
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700316 struct dccp_sock *dp = dccp_sk(sk);
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300317 const char *debug_prefix = dp->dccps_role == DCCP_ROLE_CLIENT ?
318 "CLIENT TX opt: " : "server TX opt: ";
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700319#endif
320 const int elapsed_time_len = dccp_elapsed_time_len(elapsed_time);
321 const int len = 2 + elapsed_time_len;
322 unsigned char *to;
323
Ian McDonald1bc09862005-08-20 00:23:43 -0300324 if (elapsed_time_len == 0)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700325 return;
326
327 if (DCCP_SKB_CB(skb)->dccpd_opt_len + len > DCCP_MAX_OPT_LEN) {
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300328 LIMIT_NETDEBUG(KERN_INFO "DCCP: packet too small to "
329 "insert elapsed time!\n");
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700330 return;
331 }
332
333 DCCP_SKB_CB(skb)->dccpd_opt_len += len;
334
335 to = skb_push(skb, len);
336 *to++ = DCCPO_ELAPSED_TIME;
337 *to++ = len;
338
Ian McDonald1bc09862005-08-20 00:23:43 -0300339 if (elapsed_time_len == 2) {
340 const u16 var16 = htons((u16)elapsed_time);
341 memcpy(to, &var16, 2);
342 } else {
343 const u32 var32 = htonl(elapsed_time);
344 memcpy(to, &var32, 4);
345 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700346
347 dccp_pr_debug("%sELAPSED_TIME=%u, len=%d, seqno=%llu\n",
348 debug_prefix, elapsed_time,
David S. Millerf6ccf552005-08-09 20:27:14 -0700349 len,
350 (unsigned long long) DCCP_SKB_CB(skb)->dccpd_seq);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700351}
352
Arnaldo Carvalho de Melod4b81ff2005-08-23 21:51:36 -0700353EXPORT_SYMBOL_GPL(dccp_insert_option_elapsed_time);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700354
355static void dccp_insert_option_ack_vector(struct sock *sk, struct sk_buff *skb)
356{
357 struct dccp_sock *dp = dccp_sk(sk);
Arnaldo Carvalho de Melo725ba8e2005-08-13 20:35:39 -0300358#ifdef CONFIG_IP_DCCP_DEBUG
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300359 const char *debug_prefix = dp->dccps_role == DCCP_ROLE_CLIENT ?
360 "CLIENT TX opt: " : "server TX opt: ";
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700361#endif
362 struct dccp_ackpkts *ap = dp->dccps_hc_rx_ackpkts;
363 int len = ap->dccpap_buf_vector_len + 2;
Arnaldo Carvalho de Melob6ee3d42005-08-27 18:18:18 -0300364 const u32 elapsed_time = timeval_now_delta(&ap->dccpap_time) / 10;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700365 unsigned char *to, *from;
366
367 if (elapsed_time != 0)
368 dccp_insert_option_elapsed_time(sk, skb, elapsed_time);
369
370 if (DCCP_SKB_CB(skb)->dccpd_opt_len + len > DCCP_MAX_OPT_LEN) {
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300371 LIMIT_NETDEBUG(KERN_INFO "DCCP: packet too small to "
372 "insert ACK Vector!\n");
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700373 return;
374 }
375
376 /*
377 * XXX: now we have just one ack vector sent record, so
378 * we have to wait for it to be cleared.
379 *
380 * Of course this is not acceptable, but this is just for
381 * basic testing now.
382 */
383 if (ap->dccpap_ack_seqno != DCCP_MAX_SEQNO + 1)
384 return;
385
386 DCCP_SKB_CB(skb)->dccpd_opt_len += len;
387
388 to = skb_push(skb, len);
389 *to++ = DCCPO_ACK_VECTOR_0;
390 *to++ = len;
391
392 len = ap->dccpap_buf_vector_len;
393 from = ap->dccpap_buf + ap->dccpap_buf_head;
394
395 /* Check if buf_head wraps */
396 if (ap->dccpap_buf_head + len > ap->dccpap_buf_len) {
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300397 const unsigned int tailsize = (ap->dccpap_buf_len -
398 ap->dccpap_buf_head);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700399
400 memcpy(to, from, tailsize);
401 to += tailsize;
402 len -= tailsize;
403 from = ap->dccpap_buf;
404 }
405
406 memcpy(to, from, len);
407 /*
408 * From draft-ietf-dccp-spec-11.txt:
409 *
410 * For each acknowledgement it sends, the HC-Receiver will add an
411 * acknowledgement record. ack_seqno will equal the HC-Receiver
412 * sequence number it used for the ack packet; ack_ptr will equal
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300413 * buf_head; ack_ackno will equal buf_ackno; and ack_nonce will
414 * equal buf_nonce.
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700415 *
416 * This implemention uses just one ack record for now.
417 */
418 ap->dccpap_ack_seqno = DCCP_SKB_CB(skb)->dccpd_seq;
419 ap->dccpap_ack_ptr = ap->dccpap_buf_head;
420 ap->dccpap_ack_ackno = ap->dccpap_buf_ackno;
421 ap->dccpap_ack_nonce = ap->dccpap_buf_nonce;
422 ap->dccpap_ack_vector_len = ap->dccpap_buf_vector_len;
423
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300424 dccp_pr_debug("%sACK Vector 0, len=%d, ack_seqno=%llu, "
425 "ack_ackno=%llu\n",
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700426 debug_prefix, ap->dccpap_ack_vector_len,
David S. Millerf6ccf552005-08-09 20:27:14 -0700427 (unsigned long long) ap->dccpap_ack_seqno,
428 (unsigned long long) ap->dccpap_ack_ackno);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700429}
430
Arnaldo Carvalho de Melod4b81ff2005-08-23 21:51:36 -0700431void dccp_insert_option_timestamp(struct sock *sk, struct sk_buff *skb)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700432{
Ian McDonald1bc09862005-08-20 00:23:43 -0300433 struct timeval tv;
434 u32 now;
435
436 do_gettimeofday(&tv);
437 now = (tv.tv_sec * USEC_PER_SEC + tv.tv_usec) / 10;
438 /* yes this will overflow but that is the point as we want a
439 * 10 usec 32 bit timer which mean it wraps every 11.9 hours */
440
441 now = htonl(now);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700442 dccp_insert_option(sk, skb, DCCPO_TIMESTAMP, &now, sizeof(now));
443}
444
Arnaldo Carvalho de Melod4b81ff2005-08-23 21:51:36 -0700445EXPORT_SYMBOL_GPL(dccp_insert_option_timestamp);
446
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300447static void dccp_insert_option_timestamp_echo(struct sock *sk,
448 struct sk_buff *skb)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700449{
450 struct dccp_sock *dp = dccp_sk(sk);
Arnaldo Carvalho de Melo725ba8e2005-08-13 20:35:39 -0300451#ifdef CONFIG_IP_DCCP_DEBUG
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300452 const char *debug_prefix = dp->dccps_role == DCCP_ROLE_CLIENT ?
453 "CLIENT TX opt: " : "server TX opt: ";
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700454#endif
455 u32 tstamp_echo;
Arnaldo Carvalho de Melob6ee3d42005-08-27 18:18:18 -0300456 const u32 elapsed_time =
457 timeval_now_delta(&dp->dccps_timestamp_time) / 10;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700458 const int elapsed_time_len = dccp_elapsed_time_len(elapsed_time);
459 const int len = 6 + elapsed_time_len;
460 unsigned char *to;
461
462 if (DCCP_SKB_CB(skb)->dccpd_opt_len + len > DCCP_MAX_OPT_LEN) {
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300463 LIMIT_NETDEBUG(KERN_INFO "DCCP: packet too small to insert "
464 "timestamp echo!\n");
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700465 return;
466 }
467
468 DCCP_SKB_CB(skb)->dccpd_opt_len += len;
469
470 to = skb_push(skb, len);
471 *to++ = DCCPO_TIMESTAMP_ECHO;
472 *to++ = len;
473
474 tstamp_echo = htonl(dp->dccps_timestamp_echo);
475 memcpy(to, &tstamp_echo, 4);
476 to += 4;
Ian McDonald1bc09862005-08-20 00:23:43 -0300477
478 if (elapsed_time_len == 2) {
479 const u16 var16 = htons((u16)elapsed_time);
480 memcpy(to, &var16, 2);
481 } else if (elapsed_time_len == 4) {
482 const u32 var32 = htonl(elapsed_time);
483 memcpy(to, &var32, 4);
484 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700485
486 dccp_pr_debug("%sTIMESTAMP_ECHO=%u, len=%d, seqno=%llu\n",
487 debug_prefix, dp->dccps_timestamp_echo,
David S. Millerf6ccf552005-08-09 20:27:14 -0700488 len,
489 (unsigned long long) DCCP_SKB_CB(skb)->dccpd_seq);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700490
491 dp->dccps_timestamp_echo = 0;
Ian McDonald1bc09862005-08-20 00:23:43 -0300492 dp->dccps_timestamp_time.tv_sec = 0;
493 dp->dccps_timestamp_time.tv_usec = 0;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700494}
495
496void dccp_insert_options(struct sock *sk, struct sk_buff *skb)
497{
498 struct dccp_sock *dp = dccp_sk(sk);
499
500 DCCP_SKB_CB(skb)->dccpd_opt_len = 0;
501
502 if (dp->dccps_options.dccpo_send_ndp_count)
503 dccp_insert_option_ndp(sk, skb);
504
505 if (!dccp_packet_without_ack(skb)) {
506 if (dp->dccps_options.dccpo_send_ack_vector &&
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300507 (dp->dccps_hc_rx_ackpkts->dccpap_buf_ackno !=
508 DCCP_MAX_SEQNO + 1))
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700509 dccp_insert_option_ack_vector(sk, skb);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700510 if (dp->dccps_timestamp_echo != 0)
511 dccp_insert_option_timestamp_echo(sk, skb);
512 }
513
Arnaldo Carvalho de Melo507d37c2005-09-09 02:30:07 -0300514 if (dp->dccps_hc_rx_insert_options) {
515 ccid_hc_rx_insert_options(dp->dccps_hc_rx_ccid, sk, skb);
516 dp->dccps_hc_rx_insert_options = 0;
517 }
518 if (dp->dccps_hc_tx_insert_options) {
519 ccid_hc_tx_insert_options(dp->dccps_hc_tx_ccid, sk, skb);
520 dp->dccps_hc_tx_insert_options = 0;
521 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700522
523 /* XXX: insert other options when appropriate */
524
525 if (DCCP_SKB_CB(skb)->dccpd_opt_len != 0) {
526 /* The length of all options has to be a multiple of 4 */
527 int padding = DCCP_SKB_CB(skb)->dccpd_opt_len % 4;
528
529 if (padding != 0) {
530 padding = 4 - padding;
531 memset(skb_push(skb, padding), 0, padding);
532 DCCP_SKB_CB(skb)->dccpd_opt_len += padding;
533 }
534 }
535}
536
Arnaldo Carvalho de Meloa1d3a352005-08-13 22:42:25 -0300537struct dccp_ackpkts *dccp_ackpkts_alloc(const unsigned int len,
538 const unsigned int __nocast priority)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700539{
540 struct dccp_ackpkts *ap = kmalloc(sizeof(*ap) + len, priority);
541
542 if (ap != NULL) {
Arnaldo Carvalho de Melo725ba8e2005-08-13 20:35:39 -0300543#ifdef CONFIG_IP_DCCP_DEBUG
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700544 memset(ap->dccpap_buf, 0xFF, len);
545#endif
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300546 ap->dccpap_buf_len = len;
547 ap->dccpap_buf_head =
548 ap->dccpap_buf_tail =
549 ap->dccpap_buf_len - 1;
550 ap->dccpap_buf_ackno =
551 ap->dccpap_ack_ackno =
552 ap->dccpap_ack_seqno = DCCP_MAX_SEQNO + 1;
553 ap->dccpap_buf_nonce = ap->dccpap_buf_nonce = 0;
554 ap->dccpap_ack_ptr = 0;
Ian McDonald1bc09862005-08-20 00:23:43 -0300555 ap->dccpap_time.tv_sec = 0;
556 ap->dccpap_time.tv_usec = 0;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700557 ap->dccpap_buf_vector_len = ap->dccpap_ack_vector_len = 0;
558 }
559
560 return ap;
561}
562
563void dccp_ackpkts_free(struct dccp_ackpkts *ap)
564{
565 if (ap != NULL) {
Arnaldo Carvalho de Melo725ba8e2005-08-13 20:35:39 -0300566#ifdef CONFIG_IP_DCCP_DEBUG
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700567 memset(ap, 0xFF, sizeof(*ap) + ap->dccpap_buf_len);
568#endif
569 kfree(ap);
570 }
571}
572
573static inline u8 dccp_ackpkts_state(const struct dccp_ackpkts *ap,
574 const unsigned int index)
575{
576 return ap->dccpap_buf[index] & DCCP_ACKPKTS_STATE_MASK;
577}
578
579static inline u8 dccp_ackpkts_len(const struct dccp_ackpkts *ap,
580 const unsigned int index)
581{
582 return ap->dccpap_buf[index] & DCCP_ACKPKTS_LEN_MASK;
583}
584
585/*
586 * If several packets are missing, the HC-Receiver may prefer to enter multiple
587 * bytes with run length 0, rather than a single byte with a larger run length;
588 * this simplifies table updates if one of the missing packets arrives.
589 */
590static inline int dccp_ackpkts_set_buf_head_state(struct dccp_ackpkts *ap,
591 const unsigned int packets,
592 const unsigned char state)
593{
594 unsigned int gap;
595 signed long new_head;
596
597 if (ap->dccpap_buf_vector_len + packets > ap->dccpap_buf_len)
598 return -ENOBUFS;
599
600 gap = packets - 1;
601 new_head = ap->dccpap_buf_head - packets;
602
603 if (new_head < 0) {
604 if (gap > 0) {
605 memset(ap->dccpap_buf, DCCP_ACKPKTS_STATE_NOT_RECEIVED,
606 gap + new_head + 1);
607 gap = -new_head;
608 }
609 new_head += ap->dccpap_buf_len;
610 }
611
612 ap->dccpap_buf_head = new_head;
613
614 if (gap > 0)
615 memset(ap->dccpap_buf + ap->dccpap_buf_head + 1,
616 DCCP_ACKPKTS_STATE_NOT_RECEIVED, gap);
617
618 ap->dccpap_buf[ap->dccpap_buf_head] = state;
619 ap->dccpap_buf_vector_len += packets;
620 return 0;
621}
622
623/*
624 * Implements the draft-ietf-dccp-spec-11.txt Appendix A
625 */
626int dccp_ackpkts_add(struct dccp_ackpkts *ap, u64 ackno, u8 state)
627{
628 /*
629 * Check at the right places if the buffer is full, if it is, tell the
630 * caller to start dropping packets till the HC-Sender acks our ACK
631 * vectors, when we will free up space in dccpap_buf.
632 *
633 * We may well decide to do buffer compression, etc, but for now lets
634 * just drop.
635 *
636 * From Appendix A:
637 *
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300638 * Of course, the circular buffer may overflow, either when the
639 * HC-Sender is sending data at a very high rate, when the
640 * HC-Receiver's acknowledgements are not reaching the HC-Sender,
641 * or when the HC-Sender is forgetting to acknowledge those acks
642 * (so the HC-Receiver is unable to clean up old state). In this
643 * case, the HC-Receiver should either compress the buffer (by
644 * increasing run lengths when possible), transfer its state to
645 * a larger buffer, or, as a last resort, drop all received
646 * packets, without processing them whatsoever, until its buffer
647 * shrinks again.
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700648 */
649
650 /* See if this is the first ackno being inserted */
651 if (ap->dccpap_buf_vector_len == 0) {
652 ap->dccpap_buf[ap->dccpap_buf_head] = state;
653 ap->dccpap_buf_vector_len = 1;
654 } else if (after48(ackno, ap->dccpap_buf_ackno)) {
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300655 const u64 delta = dccp_delta_seqno(ap->dccpap_buf_ackno,
656 ackno);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700657
658 /*
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300659 * Look if the state of this packet is the same as the
660 * previous ackno and if so if we can bump the head len.
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700661 */
662 if (delta == 1 &&
663 dccp_ackpkts_state(ap, ap->dccpap_buf_head) == state &&
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300664 (dccp_ackpkts_len(ap, ap->dccpap_buf_head) <
665 DCCP_ACKPKTS_LEN_MASK))
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700666 ap->dccpap_buf[ap->dccpap_buf_head]++;
667 else if (dccp_ackpkts_set_buf_head_state(ap, delta, state))
668 return -ENOBUFS;
669 } else {
670 /*
671 * A.1.2. Old Packets
672 *
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300673 * When a packet with Sequence Number S arrives, and
674 * S <= buf_ackno, the HC-Receiver will scan the table
675 * for the byte corresponding to S. (Indexing structures
676 * could reduce the complexity of this scan.)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700677 */
678 u64 delta = dccp_delta_seqno(ackno, ap->dccpap_buf_ackno);
679 unsigned int index = ap->dccpap_buf_head;
680
681 while (1) {
682 const u8 len = dccp_ackpkts_len(ap, index);
683 const u8 state = dccp_ackpkts_state(ap, index);
684 /*
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300685 * valid packets not yet in dccpap_buf have a reserved
686 * entry, with a len equal to 0.
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700687 */
688 if (state == DCCP_ACKPKTS_STATE_NOT_RECEIVED &&
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300689 len == 0 && delta == 0) { /* Found our
690 reserved seat! */
David S. Millerf6ccf552005-08-09 20:27:14 -0700691 dccp_pr_debug("Found %llu reserved seat!\n",
692 (unsigned long long) ackno);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700693 ap->dccpap_buf[index] = state;
694 goto out;
695 }
696 /* len == 0 means one packet */
697 if (delta < len + 1)
698 goto out_duplicate;
699
700 delta -= len + 1;
701 if (++index == ap->dccpap_buf_len)
702 index = 0;
703 }
704 }
705
706 ap->dccpap_buf_ackno = ackno;
Ian McDonald1bc09862005-08-20 00:23:43 -0300707 do_gettimeofday(&ap->dccpap_time);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700708out:
709 dccp_pr_debug("");
710 dccp_ackpkts_print(ap);
711 return 0;
712
713out_duplicate:
714 /* Duplicate packet */
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300715 dccp_pr_debug("Received a dup or already considered lost "
716 "packet: %llu\n", (unsigned long long) ackno);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700717 return -EILSEQ;
718}
719
Arnaldo Carvalho de Melo725ba8e2005-08-13 20:35:39 -0300720#ifdef CONFIG_IP_DCCP_DEBUG
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300721void dccp_ackvector_print(const u64 ackno, const unsigned char *vector,
722 int len)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700723{
724 if (!dccp_debug)
725 return;
726
David S. Millerf6ccf552005-08-09 20:27:14 -0700727 printk("ACK vector len=%d, ackno=%llu |", len,
728 (unsigned long long) ackno);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700729
730 while (len--) {
731 const u8 state = (*vector & DCCP_ACKPKTS_STATE_MASK) >> 6;
732 const u8 rl = (*vector & DCCP_ACKPKTS_LEN_MASK);
733
734 printk("%d,%d|", state, rl);
735 ++vector;
736 }
737
738 printk("\n");
739}
740
741void dccp_ackpkts_print(const struct dccp_ackpkts *ap)
742{
743 dccp_ackvector_print(ap->dccpap_buf_ackno,
744 ap->dccpap_buf + ap->dccpap_buf_head,
745 ap->dccpap_buf_vector_len);
746}
747#endif
748
749static void dccp_ackpkts_trow_away_ack_record(struct dccp_ackpkts *ap)
750{
751 /*
752 * As we're keeping track of the ack vector size
753 * (dccpap_buf_vector_len) and the sent ack vector size
754 * (dccpap_ack_vector_len) we don't need dccpap_buf_tail at all, but
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300755 * keep this code here as in the future we'll implement a vector of
756 * ack records, as suggested in draft-ietf-dccp-spec-11.txt
757 * Appendix A. -acme
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700758 */
759#if 0
760 ap->dccpap_buf_tail = ap->dccpap_ack_ptr + 1;
761 if (ap->dccpap_buf_tail >= ap->dccpap_buf_len)
762 ap->dccpap_buf_tail -= ap->dccpap_buf_len;
763#endif
764 ap->dccpap_buf_vector_len -= ap->dccpap_ack_vector_len;
765}
766
767void dccp_ackpkts_check_rcv_ackno(struct dccp_ackpkts *ap, struct sock *sk,
768 u64 ackno)
769{
770 /* Check if we actually sent an ACK vector */
771 if (ap->dccpap_ack_seqno == DCCP_MAX_SEQNO + 1)
772 return;
773
774 if (ackno == ap->dccpap_ack_seqno) {
Arnaldo Carvalho de Melo725ba8e2005-08-13 20:35:39 -0300775#ifdef CONFIG_IP_DCCP_DEBUG
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700776 struct dccp_sock *dp = dccp_sk(sk);
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300777 const char *debug_prefix = dp->dccps_role == DCCP_ROLE_CLIENT ?
778 "CLIENT rx ack: " : "server rx ack: ";
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700779#endif
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300780 dccp_pr_debug("%sACK packet 0, len=%d, ack_seqno=%llu, "
781 "ack_ackno=%llu, ACKED!\n",
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700782 debug_prefix, 1,
David S. Millerf6ccf552005-08-09 20:27:14 -0700783 (unsigned long long) ap->dccpap_ack_seqno,
784 (unsigned long long) ap->dccpap_ack_ackno);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700785 dccp_ackpkts_trow_away_ack_record(ap);
786 ap->dccpap_ack_seqno = DCCP_MAX_SEQNO + 1;
787 }
788}
789
790static void dccp_ackpkts_check_rcv_ackvector(struct dccp_ackpkts *ap,
791 struct sock *sk, u64 ackno,
792 const unsigned char len,
793 const unsigned char *vector)
794{
795 unsigned char i;
796
797 /* Check if we actually sent an ACK vector */
798 if (ap->dccpap_ack_seqno == DCCP_MAX_SEQNO + 1)
799 return;
800 /*
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300801 * We're in the receiver half connection, so if the received an ACK
802 * vector ackno (e.g. 50) before dccpap_ack_seqno (e.g. 52), we're
803 * not interested.
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700804 *
805 * Extra explanation with example:
806 *
807 * if we received an ACK vector with ackno 50, it can only be acking
808 * 50, 49, 48, etc, not 52 (the seqno for the ACK vector we sent).
809 */
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300810 /* dccp_pr_debug("is %llu < %llu? ", ackno, ap->dccpap_ack_seqno); */
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700811 if (before48(ackno, ap->dccpap_ack_seqno)) {
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300812 /* dccp_pr_debug_cat("yes\n"); */
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700813 return;
814 }
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300815 /* dccp_pr_debug_cat("no\n"); */
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700816
817 i = len;
818 while (i--) {
819 const u8 rl = (*vector & DCCP_ACKPKTS_LEN_MASK);
820 u64 ackno_end_rl;
821
822 dccp_set_seqno(&ackno_end_rl, ackno - rl);
823
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300824 /*
825 * dccp_pr_debug("is %llu <= %llu <= %llu? ", ackno_end_rl,
826 * ap->dccpap_ack_seqno, ackno);
827 */
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700828 if (between48(ap->dccpap_ack_seqno, ackno_end_rl, ackno)) {
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300829 const u8 state = (*vector &
830 DCCP_ACKPKTS_STATE_MASK) >> 6;
831 /* dccp_pr_debug_cat("yes\n"); */
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700832
833 if (state != DCCP_ACKPKTS_STATE_NOT_RECEIVED) {
Arnaldo Carvalho de Melo725ba8e2005-08-13 20:35:39 -0300834#ifdef CONFIG_IP_DCCP_DEBUG
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700835 struct dccp_sock *dp = dccp_sk(sk);
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300836 const char *debug_prefix =
837 dp->dccps_role == DCCP_ROLE_CLIENT ?
838 "CLIENT rx ack: " : "server rx ack: ";
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700839#endif
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300840 dccp_pr_debug("%sACK vector 0, len=%d, "
841 "ack_seqno=%llu, ack_ackno=%llu, "
842 "ACKED!\n",
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700843 debug_prefix, len,
David S. Millerf6ccf552005-08-09 20:27:14 -0700844 (unsigned long long)
845 ap->dccpap_ack_seqno,
846 (unsigned long long)
847 ap->dccpap_ack_ackno);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700848 dccp_ackpkts_trow_away_ack_record(ap);
849 }
850 /*
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300851 * If dccpap_ack_seqno was not received, no problem
852 * we'll send another ACK vector.
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700853 */
854 ap->dccpap_ack_seqno = DCCP_MAX_SEQNO + 1;
855 break;
856 }
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300857 /* dccp_pr_debug_cat("no\n"); */
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700858
859 dccp_set_seqno(&ackno, ackno_end_rl - 1);
860 ++vector;
861 }
862}