blob: 382c5894acb20590ba4943181130ffdae9dd0f19 [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;
75
76 memset(opt_recv, 0, sizeof(*opt_recv));
77
78 while (opt_ptr != opt_end) {
79 opt = *opt_ptr++;
80 len = 0;
81 value = NULL;
82
83 /* Check if this isn't a single byte option */
84 if (opt > DCCPO_MAX_RESERVED) {
85 if (opt_ptr == opt_end)
86 goto out_invalid_option;
87
88 len = *opt_ptr++;
89 if (len < 3)
90 goto out_invalid_option;
91 /*
92 * Remove the type and len fields, leaving
93 * just the value size
94 */
95 len -= 2;
96 value = opt_ptr;
97 opt_ptr += len;
98
99 if (opt_ptr > opt_end)
100 goto out_invalid_option;
101 }
102
103 switch (opt) {
104 case DCCPO_PADDING:
105 break;
106 case DCCPO_NDP_COUNT:
107 if (len > 3)
108 goto out_invalid_option;
109
110 opt_recv->dccpor_ndp = dccp_decode_value_var(value, len);
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300111 dccp_pr_debug("%sNDP count=%d\n", debug_prefix,
112 opt_recv->dccpor_ndp);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700113 break;
114 case DCCPO_ACK_VECTOR_0:
115 if (len > DCCP_MAX_ACK_VECTOR_LEN)
116 goto out_invalid_option;
117
118 if (pkt_type == DCCP_PKT_DATA)
119 continue;
120
121 opt_recv->dccpor_ack_vector_len = len;
122 opt_recv->dccpor_ack_vector_idx = value - options;
123
124 dccp_pr_debug("%sACK vector 0, len=%d, ack_ackno=%llu\n",
David S. Millerf6ccf552005-08-09 20:27:14 -0700125 debug_prefix, len,
126 (unsigned long long)
127 DCCP_SKB_CB(skb)->dccpd_ack_seq);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700128 dccp_ackvector_print(DCCP_SKB_CB(skb)->dccpd_ack_seq,
129 value, len);
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300130 dccp_ackpkts_check_rcv_ackvector(dp->dccps_hc_rx_ackpkts,
131 sk,
132 DCCP_SKB_CB(skb)->dccpd_ack_seq,
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700133 len, value);
134 break;
135 case DCCPO_TIMESTAMP:
136 if (len != 4)
137 goto out_invalid_option;
138
139 opt_recv->dccpor_timestamp = ntohl(*(u32 *)value);
140
141 dp->dccps_timestamp_echo = opt_recv->dccpor_timestamp;
Ian McDonald1bc09862005-08-20 00:23:43 -0300142 do_gettimeofday(&dp->dccps_timestamp_time);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700143
144 dccp_pr_debug("%sTIMESTAMP=%u, ackno=%llu\n",
145 debug_prefix, opt_recv->dccpor_timestamp,
David S. Millerf6ccf552005-08-09 20:27:14 -0700146 (unsigned long long)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700147 DCCP_SKB_CB(skb)->dccpd_ack_seq);
148 break;
149 case DCCPO_TIMESTAMP_ECHO:
Ian McDonald1bc09862005-08-20 00:23:43 -0300150 if (len != 4 && len != 6 && len != 8)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700151 goto out_invalid_option;
152
153 opt_recv->dccpor_timestamp_echo = ntohl(*(u32 *)value);
154
Ian McDonald1bc09862005-08-20 00:23:43 -0300155 dccp_pr_debug("%sTIMESTAMP_ECHO=%u, len=%d, ackno=%llu, ",
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300156 debug_prefix,
157 opt_recv->dccpor_timestamp_echo,
David S. Millerf6ccf552005-08-09 20:27:14 -0700158 len + 2,
159 (unsigned long long)
Ian McDonald1bc09862005-08-20 00:23:43 -0300160 DCCP_SKB_CB(skb)->dccpd_ack_seq);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700161
Ian McDonald1bc09862005-08-20 00:23:43 -0300162 if (len > 4) {
163 if (len == 6)
164 opt_recv->dccpor_elapsed_time =
165 ntohs(*(u16 *)(value + 4));
166 else
167 opt_recv->dccpor_elapsed_time =
168 ntohl(*(u32 *)(value + 4));
169
170 dccp_pr_debug("%sTIMESTAMP_ECHO ELAPSED_TIME=%d\n",
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300171 debug_prefix,
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700172 opt_recv->dccpor_elapsed_time);
Ian McDonald1bc09862005-08-20 00:23:43 -0300173 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700174 break;
175 case DCCPO_ELAPSED_TIME:
Ian McDonald1bc09862005-08-20 00:23:43 -0300176 if (len != 2 && len != 4)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700177 goto out_invalid_option;
178
179 if (pkt_type == DCCP_PKT_DATA)
180 continue;
Ian McDonald1bc09862005-08-20 00:23:43 -0300181
182 if (len == 2)
183 opt_recv->dccpor_elapsed_time =
184 ntohs(*(u16 *)value);
185 else
186 opt_recv->dccpor_elapsed_time =
187 ntohl(*(u32 *)value);
188
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700189 dccp_pr_debug("%sELAPSED_TIME=%d\n", debug_prefix,
190 opt_recv->dccpor_elapsed_time);
191 break;
192 /*
193 * From draft-ietf-dccp-spec-11.txt:
194 *
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300195 * Option numbers 128 through 191 are for
196 * options sent from the HC-Sender to the
197 * HC-Receiver; option numbers 192 through 255
198 * are for options sent from the HC-Receiver to
199 * the HC-Sender.
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700200 */
201 case 128 ... 191: {
202 const u16 idx = value - options;
203
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300204 if (ccid_hc_rx_parse_options(dp->dccps_hc_rx_ccid, sk,
205 opt, len, idx,
206 value) != 0)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700207 goto out_invalid_option;
208 }
209 break;
210 case 192 ... 255: {
211 const u16 idx = value - options;
212
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300213 if (ccid_hc_tx_parse_options(dp->dccps_hc_tx_ccid, sk,
214 opt, len, idx,
215 value) != 0)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700216 goto out_invalid_option;
217 }
218 break;
219 default:
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300220 pr_info("DCCP(%p): option %d(len=%d) not "
221 "implemented, ignoring\n",
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700222 sk, opt, len);
223 break;
224 }
225 }
226
227 return 0;
228
229out_invalid_option:
230 DCCP_INC_STATS_BH(DCCP_MIB_INVALIDOPT);
231 DCCP_SKB_CB(skb)->dccpd_reset_code = DCCP_RESET_CODE_OPTION_ERROR;
232 pr_info("DCCP(%p): invalid option %d, len=%d\n", sk, opt, len);
233 return -1;
234}
235
236static void dccp_encode_value_var(const u32 value, unsigned char *to,
237 const unsigned int len)
238{
239 if (len > 3)
240 *to++ = (value & 0xFF000000) >> 24;
241 if (len > 2)
242 *to++ = (value & 0xFF0000) >> 16;
243 if (len > 1)
244 *to++ = (value & 0xFF00) >> 8;
245 if (len > 0)
246 *to++ = (value & 0xFF);
247}
248
249static inline int dccp_ndp_len(const int ndp)
250{
251 return likely(ndp <= 0xFF) ? 1 : ndp <= 0xFFFF ? 2 : 3;
252}
253
254void dccp_insert_option(struct sock *sk, struct sk_buff *skb,
255 const unsigned char option,
256 const void *value, const unsigned char len)
257{
258 unsigned char *to;
259
260 if (DCCP_SKB_CB(skb)->dccpd_opt_len + len + 2 > DCCP_MAX_OPT_LEN) {
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300261 LIMIT_NETDEBUG(KERN_INFO "DCCP: packet too small to insert "
262 "%d option!\n", option);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700263 return;
264 }
265
266 DCCP_SKB_CB(skb)->dccpd_opt_len += len + 2;
267
268 to = skb_push(skb, len + 2);
269 *to++ = option;
270 *to++ = len + 2;
271
272 memcpy(to, value, len);
273}
274
275EXPORT_SYMBOL_GPL(dccp_insert_option);
276
277static void dccp_insert_option_ndp(struct sock *sk, struct sk_buff *skb)
278{
279 struct dccp_sock *dp = dccp_sk(sk);
280 int ndp = dp->dccps_ndp_count;
281
282 if (dccp_non_data_packet(skb))
283 ++dp->dccps_ndp_count;
284 else
285 dp->dccps_ndp_count = 0;
286
287 if (ndp > 0) {
288 unsigned char *ptr;
289 const int ndp_len = dccp_ndp_len(ndp);
290 const int len = ndp_len + 2;
291
292 if (DCCP_SKB_CB(skb)->dccpd_opt_len + len > DCCP_MAX_OPT_LEN)
293 return;
294
295 DCCP_SKB_CB(skb)->dccpd_opt_len += len;
296
297 ptr = skb_push(skb, len);
298 *ptr++ = DCCPO_NDP_COUNT;
299 *ptr++ = len;
300 dccp_encode_value_var(ndp, ptr, ndp_len);
301 }
302}
303
304static inline int dccp_elapsed_time_len(const u32 elapsed_time)
305{
Ian McDonaldb1c9fe72005-08-18 20:45:29 -0300306 return elapsed_time == 0 ? 0 : elapsed_time <= 0xFFFF ? 2 : 4;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700307}
308
309void dccp_insert_option_elapsed_time(struct sock *sk,
310 struct sk_buff *skb,
311 u32 elapsed_time)
312{
Arnaldo Carvalho de Melo725ba8e2005-08-13 20:35:39 -0300313#ifdef CONFIG_IP_DCCP_DEBUG
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700314 struct dccp_sock *dp = dccp_sk(sk);
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300315 const char *debug_prefix = dp->dccps_role == DCCP_ROLE_CLIENT ?
316 "CLIENT TX opt: " : "server TX opt: ";
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700317#endif
318 const int elapsed_time_len = dccp_elapsed_time_len(elapsed_time);
319 const int len = 2 + elapsed_time_len;
320 unsigned char *to;
321
Ian McDonald1bc09862005-08-20 00:23:43 -0300322 if (elapsed_time_len == 0)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700323 return;
324
325 if (DCCP_SKB_CB(skb)->dccpd_opt_len + len > DCCP_MAX_OPT_LEN) {
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300326 LIMIT_NETDEBUG(KERN_INFO "DCCP: packet too small to "
327 "insert elapsed time!\n");
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700328 return;
329 }
330
331 DCCP_SKB_CB(skb)->dccpd_opt_len += len;
332
333 to = skb_push(skb, len);
334 *to++ = DCCPO_ELAPSED_TIME;
335 *to++ = len;
336
Ian McDonald1bc09862005-08-20 00:23:43 -0300337 if (elapsed_time_len == 2) {
338 const u16 var16 = htons((u16)elapsed_time);
339 memcpy(to, &var16, 2);
340 } else {
341 const u32 var32 = htonl(elapsed_time);
342 memcpy(to, &var32, 4);
343 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700344
345 dccp_pr_debug("%sELAPSED_TIME=%u, len=%d, seqno=%llu\n",
346 debug_prefix, elapsed_time,
David S. Millerf6ccf552005-08-09 20:27:14 -0700347 len,
348 (unsigned long long) DCCP_SKB_CB(skb)->dccpd_seq);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700349}
350
Arnaldo Carvalho de Melod4b81ff2005-08-23 21:51:36 -0700351EXPORT_SYMBOL_GPL(dccp_insert_option_elapsed_time);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700352
353static void dccp_insert_option_ack_vector(struct sock *sk, struct sk_buff *skb)
354{
355 struct dccp_sock *dp = dccp_sk(sk);
Arnaldo Carvalho de Melo725ba8e2005-08-13 20:35:39 -0300356#ifdef CONFIG_IP_DCCP_DEBUG
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300357 const char *debug_prefix = dp->dccps_role == DCCP_ROLE_CLIENT ?
358 "CLIENT TX opt: " : "server TX opt: ";
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700359#endif
360 struct dccp_ackpkts *ap = dp->dccps_hc_rx_ackpkts;
361 int len = ap->dccpap_buf_vector_len + 2;
Arnaldo Carvalho de Melob6ee3d42005-08-27 18:18:18 -0300362 const u32 elapsed_time = timeval_now_delta(&ap->dccpap_time) / 10;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700363 unsigned char *to, *from;
364
365 if (elapsed_time != 0)
366 dccp_insert_option_elapsed_time(sk, skb, elapsed_time);
367
368 if (DCCP_SKB_CB(skb)->dccpd_opt_len + len > DCCP_MAX_OPT_LEN) {
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300369 LIMIT_NETDEBUG(KERN_INFO "DCCP: packet too small to "
370 "insert ACK Vector!\n");
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700371 return;
372 }
373
374 /*
375 * XXX: now we have just one ack vector sent record, so
376 * we have to wait for it to be cleared.
377 *
378 * Of course this is not acceptable, but this is just for
379 * basic testing now.
380 */
381 if (ap->dccpap_ack_seqno != DCCP_MAX_SEQNO + 1)
382 return;
383
384 DCCP_SKB_CB(skb)->dccpd_opt_len += len;
385
386 to = skb_push(skb, len);
387 *to++ = DCCPO_ACK_VECTOR_0;
388 *to++ = len;
389
390 len = ap->dccpap_buf_vector_len;
391 from = ap->dccpap_buf + ap->dccpap_buf_head;
392
393 /* Check if buf_head wraps */
394 if (ap->dccpap_buf_head + len > ap->dccpap_buf_len) {
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300395 const unsigned int tailsize = (ap->dccpap_buf_len -
396 ap->dccpap_buf_head);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700397
398 memcpy(to, from, tailsize);
399 to += tailsize;
400 len -= tailsize;
401 from = ap->dccpap_buf;
402 }
403
404 memcpy(to, from, len);
405 /*
406 * From draft-ietf-dccp-spec-11.txt:
407 *
408 * For each acknowledgement it sends, the HC-Receiver will add an
409 * acknowledgement record. ack_seqno will equal the HC-Receiver
410 * sequence number it used for the ack packet; ack_ptr will equal
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300411 * buf_head; ack_ackno will equal buf_ackno; and ack_nonce will
412 * equal buf_nonce.
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700413 *
414 * This implemention uses just one ack record for now.
415 */
416 ap->dccpap_ack_seqno = DCCP_SKB_CB(skb)->dccpd_seq;
417 ap->dccpap_ack_ptr = ap->dccpap_buf_head;
418 ap->dccpap_ack_ackno = ap->dccpap_buf_ackno;
419 ap->dccpap_ack_nonce = ap->dccpap_buf_nonce;
420 ap->dccpap_ack_vector_len = ap->dccpap_buf_vector_len;
421
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300422 dccp_pr_debug("%sACK Vector 0, len=%d, ack_seqno=%llu, "
423 "ack_ackno=%llu\n",
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700424 debug_prefix, ap->dccpap_ack_vector_len,
David S. Millerf6ccf552005-08-09 20:27:14 -0700425 (unsigned long long) ap->dccpap_ack_seqno,
426 (unsigned long long) ap->dccpap_ack_ackno);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700427}
428
Arnaldo Carvalho de Melod4b81ff2005-08-23 21:51:36 -0700429void dccp_insert_option_timestamp(struct sock *sk, struct sk_buff *skb)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700430{
Ian McDonald1bc09862005-08-20 00:23:43 -0300431 struct timeval tv;
432 u32 now;
433
434 do_gettimeofday(&tv);
435 now = (tv.tv_sec * USEC_PER_SEC + tv.tv_usec) / 10;
436 /* yes this will overflow but that is the point as we want a
437 * 10 usec 32 bit timer which mean it wraps every 11.9 hours */
438
439 now = htonl(now);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700440 dccp_insert_option(sk, skb, DCCPO_TIMESTAMP, &now, sizeof(now));
441}
442
Arnaldo Carvalho de Melod4b81ff2005-08-23 21:51:36 -0700443EXPORT_SYMBOL_GPL(dccp_insert_option_timestamp);
444
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300445static void dccp_insert_option_timestamp_echo(struct sock *sk,
446 struct sk_buff *skb)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700447{
448 struct dccp_sock *dp = dccp_sk(sk);
Arnaldo Carvalho de Melo725ba8e2005-08-13 20:35:39 -0300449#ifdef CONFIG_IP_DCCP_DEBUG
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300450 const char *debug_prefix = dp->dccps_role == DCCP_ROLE_CLIENT ?
451 "CLIENT TX opt: " : "server TX opt: ";
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700452#endif
453 u32 tstamp_echo;
Arnaldo Carvalho de Melob6ee3d42005-08-27 18:18:18 -0300454 const u32 elapsed_time =
455 timeval_now_delta(&dp->dccps_timestamp_time) / 10;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700456 const int elapsed_time_len = dccp_elapsed_time_len(elapsed_time);
457 const int len = 6 + elapsed_time_len;
458 unsigned char *to;
459
460 if (DCCP_SKB_CB(skb)->dccpd_opt_len + len > DCCP_MAX_OPT_LEN) {
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300461 LIMIT_NETDEBUG(KERN_INFO "DCCP: packet too small to insert "
462 "timestamp echo!\n");
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700463 return;
464 }
465
466 DCCP_SKB_CB(skb)->dccpd_opt_len += len;
467
468 to = skb_push(skb, len);
469 *to++ = DCCPO_TIMESTAMP_ECHO;
470 *to++ = len;
471
472 tstamp_echo = htonl(dp->dccps_timestamp_echo);
473 memcpy(to, &tstamp_echo, 4);
474 to += 4;
Ian McDonald1bc09862005-08-20 00:23:43 -0300475
476 if (elapsed_time_len == 2) {
477 const u16 var16 = htons((u16)elapsed_time);
478 memcpy(to, &var16, 2);
479 } else if (elapsed_time_len == 4) {
480 const u32 var32 = htonl(elapsed_time);
481 memcpy(to, &var32, 4);
482 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700483
484 dccp_pr_debug("%sTIMESTAMP_ECHO=%u, len=%d, seqno=%llu\n",
485 debug_prefix, dp->dccps_timestamp_echo,
David S. Millerf6ccf552005-08-09 20:27:14 -0700486 len,
487 (unsigned long long) DCCP_SKB_CB(skb)->dccpd_seq);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700488
489 dp->dccps_timestamp_echo = 0;
Ian McDonald1bc09862005-08-20 00:23:43 -0300490 dp->dccps_timestamp_time.tv_sec = 0;
491 dp->dccps_timestamp_time.tv_usec = 0;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700492}
493
494void dccp_insert_options(struct sock *sk, struct sk_buff *skb)
495{
496 struct dccp_sock *dp = dccp_sk(sk);
497
498 DCCP_SKB_CB(skb)->dccpd_opt_len = 0;
499
500 if (dp->dccps_options.dccpo_send_ndp_count)
501 dccp_insert_option_ndp(sk, skb);
502
503 if (!dccp_packet_without_ack(skb)) {
504 if (dp->dccps_options.dccpo_send_ack_vector &&
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300505 (dp->dccps_hc_rx_ackpkts->dccpap_buf_ackno !=
506 DCCP_MAX_SEQNO + 1))
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700507 dccp_insert_option_ack_vector(sk, skb);
508
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700509 if (dp->dccps_timestamp_echo != 0)
510 dccp_insert_option_timestamp_echo(sk, skb);
511 }
512
513 ccid_hc_rx_insert_options(dp->dccps_hc_rx_ccid, sk, skb);
514 ccid_hc_tx_insert_options(dp->dccps_hc_tx_ccid, sk, skb);
515
516 /* XXX: insert other options when appropriate */
517
518 if (DCCP_SKB_CB(skb)->dccpd_opt_len != 0) {
519 /* The length of all options has to be a multiple of 4 */
520 int padding = DCCP_SKB_CB(skb)->dccpd_opt_len % 4;
521
522 if (padding != 0) {
523 padding = 4 - padding;
524 memset(skb_push(skb, padding), 0, padding);
525 DCCP_SKB_CB(skb)->dccpd_opt_len += padding;
526 }
527 }
528}
529
Arnaldo Carvalho de Meloa1d3a352005-08-13 22:42:25 -0300530struct dccp_ackpkts *dccp_ackpkts_alloc(const unsigned int len,
531 const unsigned int __nocast priority)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700532{
533 struct dccp_ackpkts *ap = kmalloc(sizeof(*ap) + len, priority);
534
535 if (ap != NULL) {
Arnaldo Carvalho de Melo725ba8e2005-08-13 20:35:39 -0300536#ifdef CONFIG_IP_DCCP_DEBUG
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700537 memset(ap->dccpap_buf, 0xFF, len);
538#endif
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300539 ap->dccpap_buf_len = len;
540 ap->dccpap_buf_head =
541 ap->dccpap_buf_tail =
542 ap->dccpap_buf_len - 1;
543 ap->dccpap_buf_ackno =
544 ap->dccpap_ack_ackno =
545 ap->dccpap_ack_seqno = DCCP_MAX_SEQNO + 1;
546 ap->dccpap_buf_nonce = ap->dccpap_buf_nonce = 0;
547 ap->dccpap_ack_ptr = 0;
Ian McDonald1bc09862005-08-20 00:23:43 -0300548 ap->dccpap_time.tv_sec = 0;
549 ap->dccpap_time.tv_usec = 0;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700550 ap->dccpap_buf_vector_len = ap->dccpap_ack_vector_len = 0;
551 }
552
553 return ap;
554}
555
556void dccp_ackpkts_free(struct dccp_ackpkts *ap)
557{
558 if (ap != NULL) {
Arnaldo Carvalho de Melo725ba8e2005-08-13 20:35:39 -0300559#ifdef CONFIG_IP_DCCP_DEBUG
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700560 memset(ap, 0xFF, sizeof(*ap) + ap->dccpap_buf_len);
561#endif
562 kfree(ap);
563 }
564}
565
566static inline u8 dccp_ackpkts_state(const struct dccp_ackpkts *ap,
567 const unsigned int index)
568{
569 return ap->dccpap_buf[index] & DCCP_ACKPKTS_STATE_MASK;
570}
571
572static inline u8 dccp_ackpkts_len(const struct dccp_ackpkts *ap,
573 const unsigned int index)
574{
575 return ap->dccpap_buf[index] & DCCP_ACKPKTS_LEN_MASK;
576}
577
578/*
579 * If several packets are missing, the HC-Receiver may prefer to enter multiple
580 * bytes with run length 0, rather than a single byte with a larger run length;
581 * this simplifies table updates if one of the missing packets arrives.
582 */
583static inline int dccp_ackpkts_set_buf_head_state(struct dccp_ackpkts *ap,
584 const unsigned int packets,
585 const unsigned char state)
586{
587 unsigned int gap;
588 signed long new_head;
589
590 if (ap->dccpap_buf_vector_len + packets > ap->dccpap_buf_len)
591 return -ENOBUFS;
592
593 gap = packets - 1;
594 new_head = ap->dccpap_buf_head - packets;
595
596 if (new_head < 0) {
597 if (gap > 0) {
598 memset(ap->dccpap_buf, DCCP_ACKPKTS_STATE_NOT_RECEIVED,
599 gap + new_head + 1);
600 gap = -new_head;
601 }
602 new_head += ap->dccpap_buf_len;
603 }
604
605 ap->dccpap_buf_head = new_head;
606
607 if (gap > 0)
608 memset(ap->dccpap_buf + ap->dccpap_buf_head + 1,
609 DCCP_ACKPKTS_STATE_NOT_RECEIVED, gap);
610
611 ap->dccpap_buf[ap->dccpap_buf_head] = state;
612 ap->dccpap_buf_vector_len += packets;
613 return 0;
614}
615
616/*
617 * Implements the draft-ietf-dccp-spec-11.txt Appendix A
618 */
619int dccp_ackpkts_add(struct dccp_ackpkts *ap, u64 ackno, u8 state)
620{
621 /*
622 * Check at the right places if the buffer is full, if it is, tell the
623 * caller to start dropping packets till the HC-Sender acks our ACK
624 * vectors, when we will free up space in dccpap_buf.
625 *
626 * We may well decide to do buffer compression, etc, but for now lets
627 * just drop.
628 *
629 * From Appendix A:
630 *
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300631 * Of course, the circular buffer may overflow, either when the
632 * HC-Sender is sending data at a very high rate, when the
633 * HC-Receiver's acknowledgements are not reaching the HC-Sender,
634 * or when the HC-Sender is forgetting to acknowledge those acks
635 * (so the HC-Receiver is unable to clean up old state). In this
636 * case, the HC-Receiver should either compress the buffer (by
637 * increasing run lengths when possible), transfer its state to
638 * a larger buffer, or, as a last resort, drop all received
639 * packets, without processing them whatsoever, until its buffer
640 * shrinks again.
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700641 */
642
643 /* See if this is the first ackno being inserted */
644 if (ap->dccpap_buf_vector_len == 0) {
645 ap->dccpap_buf[ap->dccpap_buf_head] = state;
646 ap->dccpap_buf_vector_len = 1;
647 } else if (after48(ackno, ap->dccpap_buf_ackno)) {
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300648 const u64 delta = dccp_delta_seqno(ap->dccpap_buf_ackno,
649 ackno);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700650
651 /*
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300652 * Look if the state of this packet is the same as the
653 * previous ackno and if so if we can bump the head len.
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700654 */
655 if (delta == 1 &&
656 dccp_ackpkts_state(ap, ap->dccpap_buf_head) == state &&
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300657 (dccp_ackpkts_len(ap, ap->dccpap_buf_head) <
658 DCCP_ACKPKTS_LEN_MASK))
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700659 ap->dccpap_buf[ap->dccpap_buf_head]++;
660 else if (dccp_ackpkts_set_buf_head_state(ap, delta, state))
661 return -ENOBUFS;
662 } else {
663 /*
664 * A.1.2. Old Packets
665 *
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300666 * When a packet with Sequence Number S arrives, and
667 * S <= buf_ackno, the HC-Receiver will scan the table
668 * for the byte corresponding to S. (Indexing structures
669 * could reduce the complexity of this scan.)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700670 */
671 u64 delta = dccp_delta_seqno(ackno, ap->dccpap_buf_ackno);
672 unsigned int index = ap->dccpap_buf_head;
673
674 while (1) {
675 const u8 len = dccp_ackpkts_len(ap, index);
676 const u8 state = dccp_ackpkts_state(ap, index);
677 /*
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300678 * valid packets not yet in dccpap_buf have a reserved
679 * entry, with a len equal to 0.
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700680 */
681 if (state == DCCP_ACKPKTS_STATE_NOT_RECEIVED &&
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300682 len == 0 && delta == 0) { /* Found our
683 reserved seat! */
David S. Millerf6ccf552005-08-09 20:27:14 -0700684 dccp_pr_debug("Found %llu reserved seat!\n",
685 (unsigned long long) ackno);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700686 ap->dccpap_buf[index] = state;
687 goto out;
688 }
689 /* len == 0 means one packet */
690 if (delta < len + 1)
691 goto out_duplicate;
692
693 delta -= len + 1;
694 if (++index == ap->dccpap_buf_len)
695 index = 0;
696 }
697 }
698
699 ap->dccpap_buf_ackno = ackno;
Ian McDonald1bc09862005-08-20 00:23:43 -0300700 do_gettimeofday(&ap->dccpap_time);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700701out:
702 dccp_pr_debug("");
703 dccp_ackpkts_print(ap);
704 return 0;
705
706out_duplicate:
707 /* Duplicate packet */
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300708 dccp_pr_debug("Received a dup or already considered lost "
709 "packet: %llu\n", (unsigned long long) ackno);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700710 return -EILSEQ;
711}
712
Arnaldo Carvalho de Melo725ba8e2005-08-13 20:35:39 -0300713#ifdef CONFIG_IP_DCCP_DEBUG
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300714void dccp_ackvector_print(const u64 ackno, const unsigned char *vector,
715 int len)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700716{
717 if (!dccp_debug)
718 return;
719
David S. Millerf6ccf552005-08-09 20:27:14 -0700720 printk("ACK vector len=%d, ackno=%llu |", len,
721 (unsigned long long) ackno);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700722
723 while (len--) {
724 const u8 state = (*vector & DCCP_ACKPKTS_STATE_MASK) >> 6;
725 const u8 rl = (*vector & DCCP_ACKPKTS_LEN_MASK);
726
727 printk("%d,%d|", state, rl);
728 ++vector;
729 }
730
731 printk("\n");
732}
733
734void dccp_ackpkts_print(const struct dccp_ackpkts *ap)
735{
736 dccp_ackvector_print(ap->dccpap_buf_ackno,
737 ap->dccpap_buf + ap->dccpap_buf_head,
738 ap->dccpap_buf_vector_len);
739}
740#endif
741
742static void dccp_ackpkts_trow_away_ack_record(struct dccp_ackpkts *ap)
743{
744 /*
745 * As we're keeping track of the ack vector size
746 * (dccpap_buf_vector_len) and the sent ack vector size
747 * (dccpap_ack_vector_len) we don't need dccpap_buf_tail at all, but
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300748 * keep this code here as in the future we'll implement a vector of
749 * ack records, as suggested in draft-ietf-dccp-spec-11.txt
750 * Appendix A. -acme
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700751 */
752#if 0
753 ap->dccpap_buf_tail = ap->dccpap_ack_ptr + 1;
754 if (ap->dccpap_buf_tail >= ap->dccpap_buf_len)
755 ap->dccpap_buf_tail -= ap->dccpap_buf_len;
756#endif
757 ap->dccpap_buf_vector_len -= ap->dccpap_ack_vector_len;
758}
759
760void dccp_ackpkts_check_rcv_ackno(struct dccp_ackpkts *ap, struct sock *sk,
761 u64 ackno)
762{
763 /* Check if we actually sent an ACK vector */
764 if (ap->dccpap_ack_seqno == DCCP_MAX_SEQNO + 1)
765 return;
766
767 if (ackno == ap->dccpap_ack_seqno) {
Arnaldo Carvalho de Melo725ba8e2005-08-13 20:35:39 -0300768#ifdef CONFIG_IP_DCCP_DEBUG
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700769 struct dccp_sock *dp = dccp_sk(sk);
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300770 const char *debug_prefix = dp->dccps_role == DCCP_ROLE_CLIENT ?
771 "CLIENT rx ack: " : "server rx ack: ";
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700772#endif
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300773 dccp_pr_debug("%sACK packet 0, len=%d, ack_seqno=%llu, "
774 "ack_ackno=%llu, ACKED!\n",
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700775 debug_prefix, 1,
David S. Millerf6ccf552005-08-09 20:27:14 -0700776 (unsigned long long) ap->dccpap_ack_seqno,
777 (unsigned long long) ap->dccpap_ack_ackno);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700778 dccp_ackpkts_trow_away_ack_record(ap);
779 ap->dccpap_ack_seqno = DCCP_MAX_SEQNO + 1;
780 }
781}
782
783static void dccp_ackpkts_check_rcv_ackvector(struct dccp_ackpkts *ap,
784 struct sock *sk, u64 ackno,
785 const unsigned char len,
786 const unsigned char *vector)
787{
788 unsigned char i;
789
790 /* Check if we actually sent an ACK vector */
791 if (ap->dccpap_ack_seqno == DCCP_MAX_SEQNO + 1)
792 return;
793 /*
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300794 * We're in the receiver half connection, so if the received an ACK
795 * vector ackno (e.g. 50) before dccpap_ack_seqno (e.g. 52), we're
796 * not interested.
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700797 *
798 * Extra explanation with example:
799 *
800 * if we received an ACK vector with ackno 50, it can only be acking
801 * 50, 49, 48, etc, not 52 (the seqno for the ACK vector we sent).
802 */
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300803 /* dccp_pr_debug("is %llu < %llu? ", ackno, ap->dccpap_ack_seqno); */
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700804 if (before48(ackno, ap->dccpap_ack_seqno)) {
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300805 /* dccp_pr_debug_cat("yes\n"); */
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700806 return;
807 }
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300808 /* dccp_pr_debug_cat("no\n"); */
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700809
810 i = len;
811 while (i--) {
812 const u8 rl = (*vector & DCCP_ACKPKTS_LEN_MASK);
813 u64 ackno_end_rl;
814
815 dccp_set_seqno(&ackno_end_rl, ackno - rl);
816
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300817 /*
818 * dccp_pr_debug("is %llu <= %llu <= %llu? ", ackno_end_rl,
819 * ap->dccpap_ack_seqno, ackno);
820 */
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700821 if (between48(ap->dccpap_ack_seqno, ackno_end_rl, ackno)) {
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300822 const u8 state = (*vector &
823 DCCP_ACKPKTS_STATE_MASK) >> 6;
824 /* dccp_pr_debug_cat("yes\n"); */
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700825
826 if (state != DCCP_ACKPKTS_STATE_NOT_RECEIVED) {
Arnaldo Carvalho de Melo725ba8e2005-08-13 20:35:39 -0300827#ifdef CONFIG_IP_DCCP_DEBUG
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700828 struct dccp_sock *dp = dccp_sk(sk);
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300829 const char *debug_prefix =
830 dp->dccps_role == DCCP_ROLE_CLIENT ?
831 "CLIENT rx ack: " : "server rx ack: ";
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700832#endif
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300833 dccp_pr_debug("%sACK vector 0, len=%d, "
834 "ack_seqno=%llu, ack_ackno=%llu, "
835 "ACKED!\n",
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700836 debug_prefix, len,
David S. Millerf6ccf552005-08-09 20:27:14 -0700837 (unsigned long long)
838 ap->dccpap_ack_seqno,
839 (unsigned long long)
840 ap->dccpap_ack_ackno);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700841 dccp_ackpkts_trow_away_ack_record(ap);
842 }
843 /*
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300844 * If dccpap_ack_seqno was not received, no problem
845 * we'll send another ACK vector.
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700846 */
847 ap->dccpap_ack_seqno = DCCP_MAX_SEQNO + 1;
848 break;
849 }
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300850 /* dccp_pr_debug_cat("no\n"); */
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700851
852 dccp_set_seqno(&ackno, ackno_end_rl - 1);
853 ++vector;
854 }
855}