17c657876SArnaldo Carvalho de Melo /* 27c657876SArnaldo Carvalho de Melo * net/dccp/options.c 37c657876SArnaldo Carvalho de Melo * 47c657876SArnaldo Carvalho de Melo * An implementation of the DCCP protocol 51bc09869SIan McDonald * Copyright (c) 2005 Aristeu Sergio Rozanski Filho <aris@cathedrallabs.org> 61bc09869SIan McDonald * Copyright (c) 2005 Arnaldo Carvalho de Melo <acme@ghostprotocols.net> 71bc09869SIan McDonald * Copyright (c) 2005 Ian McDonald <iam4@cs.waikato.ac.nz> 87c657876SArnaldo Carvalho de Melo * 97c657876SArnaldo Carvalho de Melo * This program is free software; you can redistribute it and/or 107c657876SArnaldo Carvalho de Melo * modify it under the terms of the GNU General Public License 117c657876SArnaldo Carvalho de Melo * as published by the Free Software Foundation; either version 127c657876SArnaldo Carvalho de Melo * 2 of the License, or (at your option) any later version. 137c657876SArnaldo Carvalho de Melo */ 147c657876SArnaldo Carvalho de Melo #include <linux/dccp.h> 157c657876SArnaldo Carvalho de Melo #include <linux/module.h> 167c657876SArnaldo Carvalho de Melo #include <linux/types.h> 177c657876SArnaldo Carvalho de Melo #include <linux/kernel.h> 187c657876SArnaldo Carvalho de Melo #include <linux/skbuff.h> 197c657876SArnaldo Carvalho de Melo 20ae31c339SArnaldo Carvalho de Melo #include "ackvec.h" 217c657876SArnaldo Carvalho de Melo #include "ccid.h" 227c657876SArnaldo Carvalho de Melo #include "dccp.h" 23afe00251SAndrea Bittau #include "feat.h" 247c657876SArnaldo Carvalho de Melo 25e55d912fSArnaldo Carvalho de Melo int dccp_feat_default_sequence_window = DCCPF_INITIAL_SEQUENCE_WINDOW; 26e55d912fSArnaldo Carvalho de Melo int dccp_feat_default_rx_ccid = DCCPF_INITIAL_CCID; 27e55d912fSArnaldo Carvalho de Melo int dccp_feat_default_tx_ccid = DCCPF_INITIAL_CCID; 28e55d912fSArnaldo Carvalho de Melo int dccp_feat_default_ack_ratio = DCCPF_INITIAL_ACK_RATIO; 29e55d912fSArnaldo Carvalho de Melo int dccp_feat_default_send_ack_vector = DCCPF_INITIAL_SEND_ACK_VECTOR; 30e55d912fSArnaldo Carvalho de Melo int dccp_feat_default_send_ndp_count = DCCPF_INITIAL_SEND_NDP_COUNT; 317c657876SArnaldo Carvalho de Melo 324b79f0afSIan McDonald EXPORT_SYMBOL_GPL(dccp_feat_default_sequence_window); 334b79f0afSIan McDonald 34a4bf3902SArnaldo Carvalho de Melo void dccp_minisock_init(struct dccp_minisock *dmsk) 357c657876SArnaldo Carvalho de Melo { 36a4bf3902SArnaldo Carvalho de Melo dmsk->dccpms_sequence_window = dccp_feat_default_sequence_window; 37a4bf3902SArnaldo Carvalho de Melo dmsk->dccpms_rx_ccid = dccp_feat_default_rx_ccid; 38a4bf3902SArnaldo Carvalho de Melo dmsk->dccpms_tx_ccid = dccp_feat_default_tx_ccid; 39a4bf3902SArnaldo Carvalho de Melo dmsk->dccpms_ack_ratio = dccp_feat_default_ack_ratio; 40a4bf3902SArnaldo Carvalho de Melo dmsk->dccpms_send_ack_vector = dccp_feat_default_send_ack_vector; 41a4bf3902SArnaldo Carvalho de Melo dmsk->dccpms_send_ndp_count = dccp_feat_default_send_ndp_count; 427c657876SArnaldo Carvalho de Melo } 437c657876SArnaldo Carvalho de Melo 447c657876SArnaldo Carvalho de Melo static u32 dccp_decode_value_var(const unsigned char *bf, const u8 len) 457c657876SArnaldo Carvalho de Melo { 467c657876SArnaldo Carvalho de Melo u32 value = 0; 477c657876SArnaldo Carvalho de Melo 487c657876SArnaldo Carvalho de Melo if (len > 3) 497c657876SArnaldo Carvalho de Melo value += *bf++ << 24; 507c657876SArnaldo Carvalho de Melo if (len > 2) 517c657876SArnaldo Carvalho de Melo value += *bf++ << 16; 527c657876SArnaldo Carvalho de Melo if (len > 1) 537c657876SArnaldo Carvalho de Melo value += *bf++ << 8; 547c657876SArnaldo Carvalho de Melo if (len > 0) 557c657876SArnaldo Carvalho de Melo value += *bf; 567c657876SArnaldo Carvalho de Melo 577c657876SArnaldo Carvalho de Melo return value; 587c657876SArnaldo Carvalho de Melo } 597c657876SArnaldo Carvalho de Melo 607c657876SArnaldo Carvalho de Melo int dccp_parse_options(struct sock *sk, struct sk_buff *skb) 617c657876SArnaldo Carvalho de Melo { 627c657876SArnaldo Carvalho de Melo struct dccp_sock *dp = dccp_sk(sk); 63725ba8eeSArnaldo Carvalho de Melo #ifdef CONFIG_IP_DCCP_DEBUG 647690af3fSArnaldo Carvalho de Melo const char *debug_prefix = dp->dccps_role == DCCP_ROLE_CLIENT ? 657690af3fSArnaldo Carvalho de Melo "CLIENT rx opt: " : "server rx opt: "; 667c657876SArnaldo Carvalho de Melo #endif 677c657876SArnaldo Carvalho de Melo const struct dccp_hdr *dh = dccp_hdr(skb); 687c657876SArnaldo Carvalho de Melo const u8 pkt_type = DCCP_SKB_CB(skb)->dccpd_type; 697c657876SArnaldo Carvalho de Melo unsigned char *options = (unsigned char *)dh + dccp_hdr_len(skb); 707c657876SArnaldo Carvalho de Melo unsigned char *opt_ptr = options; 717690af3fSArnaldo Carvalho de Melo const unsigned char *opt_end = (unsigned char *)dh + 727690af3fSArnaldo Carvalho de Melo (dh->dccph_doff * 4); 737c657876SArnaldo Carvalho de Melo struct dccp_options_received *opt_recv = &dp->dccps_options_received; 747c657876SArnaldo Carvalho de Melo unsigned char opt, len; 757c657876SArnaldo Carvalho de Melo unsigned char *value; 761c14ac0aSArnaldo Carvalho de Melo u32 elapsed_time; 77afe00251SAndrea Bittau int rc; 78afe00251SAndrea Bittau int mandatory = 0; 797c657876SArnaldo Carvalho de Melo 807c657876SArnaldo Carvalho de Melo memset(opt_recv, 0, sizeof(*opt_recv)); 817c657876SArnaldo Carvalho de Melo 82fb950496SDavid S. Miller opt = len = 0; 837c657876SArnaldo Carvalho de Melo while (opt_ptr != opt_end) { 847c657876SArnaldo Carvalho de Melo opt = *opt_ptr++; 857c657876SArnaldo Carvalho de Melo len = 0; 867c657876SArnaldo Carvalho de Melo value = NULL; 877c657876SArnaldo Carvalho de Melo 887c657876SArnaldo Carvalho de Melo /* Check if this isn't a single byte option */ 897c657876SArnaldo Carvalho de Melo if (opt > DCCPO_MAX_RESERVED) { 907c657876SArnaldo Carvalho de Melo if (opt_ptr == opt_end) 917c657876SArnaldo Carvalho de Melo goto out_invalid_option; 927c657876SArnaldo Carvalho de Melo 937c657876SArnaldo Carvalho de Melo len = *opt_ptr++; 947c657876SArnaldo Carvalho de Melo if (len < 3) 957c657876SArnaldo Carvalho de Melo goto out_invalid_option; 967c657876SArnaldo Carvalho de Melo /* 977c657876SArnaldo Carvalho de Melo * Remove the type and len fields, leaving 987c657876SArnaldo Carvalho de Melo * just the value size 997c657876SArnaldo Carvalho de Melo */ 1007c657876SArnaldo Carvalho de Melo len -= 2; 1017c657876SArnaldo Carvalho de Melo value = opt_ptr; 1027c657876SArnaldo Carvalho de Melo opt_ptr += len; 1037c657876SArnaldo Carvalho de Melo 1047c657876SArnaldo Carvalho de Melo if (opt_ptr > opt_end) 1057c657876SArnaldo Carvalho de Melo goto out_invalid_option; 1067c657876SArnaldo Carvalho de Melo } 1077c657876SArnaldo Carvalho de Melo 1087c657876SArnaldo Carvalho de Melo switch (opt) { 1097c657876SArnaldo Carvalho de Melo case DCCPO_PADDING: 1107c657876SArnaldo Carvalho de Melo break; 111afe00251SAndrea Bittau case DCCPO_MANDATORY: 112afe00251SAndrea Bittau if (mandatory) 113afe00251SAndrea Bittau goto out_invalid_option; 1146df9424aSArnaldo Carvalho de Melo if (pkt_type != DCCP_PKT_DATA) 115afe00251SAndrea Bittau mandatory = 1; 116afe00251SAndrea Bittau break; 1177c657876SArnaldo Carvalho de Melo case DCCPO_NDP_COUNT: 1187c657876SArnaldo Carvalho de Melo if (len > 3) 1197c657876SArnaldo Carvalho de Melo goto out_invalid_option; 1207c657876SArnaldo Carvalho de Melo 1217c657876SArnaldo Carvalho de Melo opt_recv->dccpor_ndp = dccp_decode_value_var(value, len); 1227690af3fSArnaldo Carvalho de Melo dccp_pr_debug("%sNDP count=%d\n", debug_prefix, 1237690af3fSArnaldo Carvalho de Melo opt_recv->dccpor_ndp); 1247c657876SArnaldo Carvalho de Melo break; 125afe00251SAndrea Bittau case DCCPO_CHANGE_L: 126afe00251SAndrea Bittau /* fall through */ 127afe00251SAndrea Bittau case DCCPO_CHANGE_R: 128afe00251SAndrea Bittau if (len < 2) 129afe00251SAndrea Bittau goto out_invalid_option; 130afe00251SAndrea Bittau rc = dccp_feat_change_recv(sk, opt, *value, value + 1, 131afe00251SAndrea Bittau len - 1); 132afe00251SAndrea Bittau /* 133afe00251SAndrea Bittau * When there is a change error, change_recv is 134afe00251SAndrea Bittau * responsible for dealing with it. i.e. reply with an 135afe00251SAndrea Bittau * empty confirm. 136afe00251SAndrea Bittau * If the change was mandatory, then we need to die. 137afe00251SAndrea Bittau */ 138afe00251SAndrea Bittau if (rc && mandatory) 139afe00251SAndrea Bittau goto out_invalid_option; 140afe00251SAndrea Bittau break; 141afe00251SAndrea Bittau case DCCPO_CONFIRM_L: 142afe00251SAndrea Bittau /* fall through */ 143afe00251SAndrea Bittau case DCCPO_CONFIRM_R: 144afe00251SAndrea Bittau if (len < 2) 145afe00251SAndrea Bittau goto out_invalid_option; 146afe00251SAndrea Bittau if (dccp_feat_confirm_recv(sk, opt, *value, 147afe00251SAndrea Bittau value + 1, len - 1)) 148afe00251SAndrea Bittau goto out_invalid_option; 149afe00251SAndrea Bittau break; 1507c657876SArnaldo Carvalho de Melo case DCCPO_ACK_VECTOR_0: 151ae31c339SArnaldo Carvalho de Melo case DCCPO_ACK_VECTOR_1: 1527c657876SArnaldo Carvalho de Melo if (pkt_type == DCCP_PKT_DATA) 153e5a6de91SArnaldo Carvalho de Melo break; 1547c657876SArnaldo Carvalho de Melo 155a4bf3902SArnaldo Carvalho de Melo if (dccp_msk(sk)->dccpms_send_ack_vector && 156ae31c339SArnaldo Carvalho de Melo dccp_ackvec_parse(sk, skb, opt, value, len)) 157ae31c339SArnaldo Carvalho de Melo goto out_invalid_option; 1587c657876SArnaldo Carvalho de Melo break; 1597c657876SArnaldo Carvalho de Melo case DCCPO_TIMESTAMP: 1607c657876SArnaldo Carvalho de Melo if (len != 4) 1617c657876SArnaldo Carvalho de Melo goto out_invalid_option; 1627c657876SArnaldo Carvalho de Melo 16360fe62e7SAndrea Bittau opt_recv->dccpor_timestamp = ntohl(*(__be32 *)value); 1647c657876SArnaldo Carvalho de Melo 1657c657876SArnaldo Carvalho de Melo dp->dccps_timestamp_echo = opt_recv->dccpor_timestamp; 166b0e56780SArnaldo Carvalho de Melo dccp_timestamp(sk, &dp->dccps_timestamp_time); 1677c657876SArnaldo Carvalho de Melo 1687c657876SArnaldo Carvalho de Melo dccp_pr_debug("%sTIMESTAMP=%u, ackno=%llu\n", 1697c657876SArnaldo Carvalho de Melo debug_prefix, opt_recv->dccpor_timestamp, 170f6ccf554SDavid S. Miller (unsigned long long) 1717c657876SArnaldo Carvalho de Melo DCCP_SKB_CB(skb)->dccpd_ack_seq); 1727c657876SArnaldo Carvalho de Melo break; 1737c657876SArnaldo Carvalho de Melo case DCCPO_TIMESTAMP_ECHO: 1741bc09869SIan McDonald if (len != 4 && len != 6 && len != 8) 1757c657876SArnaldo Carvalho de Melo goto out_invalid_option; 1767c657876SArnaldo Carvalho de Melo 17760fe62e7SAndrea Bittau opt_recv->dccpor_timestamp_echo = ntohl(*(__be32 *)value); 1787c657876SArnaldo Carvalho de Melo 1791bc09869SIan McDonald dccp_pr_debug("%sTIMESTAMP_ECHO=%u, len=%d, ackno=%llu, ", 1807690af3fSArnaldo Carvalho de Melo debug_prefix, 1817690af3fSArnaldo Carvalho de Melo opt_recv->dccpor_timestamp_echo, 182f6ccf554SDavid S. Miller len + 2, 183f6ccf554SDavid S. Miller (unsigned long long) 1841bc09869SIan McDonald DCCP_SKB_CB(skb)->dccpd_ack_seq); 1857c657876SArnaldo Carvalho de Melo 1861bc09869SIan McDonald 1871c14ac0aSArnaldo Carvalho de Melo if (len == 4) 1881c14ac0aSArnaldo Carvalho de Melo break; 1891c14ac0aSArnaldo Carvalho de Melo 1901c14ac0aSArnaldo Carvalho de Melo if (len == 6) 19160fe62e7SAndrea Bittau elapsed_time = ntohs(*(__be16 *)(value + 4)); 1921c14ac0aSArnaldo Carvalho de Melo else 19360fe62e7SAndrea Bittau elapsed_time = ntohl(*(__be32 *)(value + 4)); 1941c14ac0aSArnaldo Carvalho de Melo 1951c14ac0aSArnaldo Carvalho de Melo /* Give precedence to the biggest ELAPSED_TIME */ 1961c14ac0aSArnaldo Carvalho de Melo if (elapsed_time > opt_recv->dccpor_elapsed_time) 1971c14ac0aSArnaldo Carvalho de Melo opt_recv->dccpor_elapsed_time = elapsed_time; 1987c657876SArnaldo Carvalho de Melo break; 1997c657876SArnaldo Carvalho de Melo case DCCPO_ELAPSED_TIME: 2001bc09869SIan McDonald if (len != 2 && len != 4) 2017c657876SArnaldo Carvalho de Melo goto out_invalid_option; 2027c657876SArnaldo Carvalho de Melo 2037c657876SArnaldo Carvalho de Melo if (pkt_type == DCCP_PKT_DATA) 2047c657876SArnaldo Carvalho de Melo continue; 2051bc09869SIan McDonald 2061bc09869SIan McDonald if (len == 2) 20760fe62e7SAndrea Bittau elapsed_time = ntohs(*(__be16 *)value); 2081bc09869SIan McDonald else 20960fe62e7SAndrea Bittau elapsed_time = ntohl(*(__be32 *)value); 2101c14ac0aSArnaldo Carvalho de Melo 2111c14ac0aSArnaldo Carvalho de Melo if (elapsed_time > opt_recv->dccpor_elapsed_time) 2121c14ac0aSArnaldo Carvalho de Melo opt_recv->dccpor_elapsed_time = elapsed_time; 2131bc09869SIan McDonald 2147c657876SArnaldo Carvalho de Melo dccp_pr_debug("%sELAPSED_TIME=%d\n", debug_prefix, 2151c14ac0aSArnaldo Carvalho de Melo elapsed_time); 2167c657876SArnaldo Carvalho de Melo break; 2177c657876SArnaldo Carvalho de Melo /* 2187c657876SArnaldo Carvalho de Melo * From draft-ietf-dccp-spec-11.txt: 2197c657876SArnaldo Carvalho de Melo * 2207690af3fSArnaldo Carvalho de Melo * Option numbers 128 through 191 are for 2217690af3fSArnaldo Carvalho de Melo * options sent from the HC-Sender to the 2227690af3fSArnaldo Carvalho de Melo * HC-Receiver; option numbers 192 through 255 2237690af3fSArnaldo Carvalho de Melo * are for options sent from the HC-Receiver to 2247690af3fSArnaldo Carvalho de Melo * the HC-Sender. 2257c657876SArnaldo Carvalho de Melo */ 2267c657876SArnaldo Carvalho de Melo case 128 ... 191: { 2277c657876SArnaldo Carvalho de Melo const u16 idx = value - options; 2287c657876SArnaldo Carvalho de Melo 2297690af3fSArnaldo Carvalho de Melo if (ccid_hc_rx_parse_options(dp->dccps_hc_rx_ccid, sk, 2307690af3fSArnaldo Carvalho de Melo opt, len, idx, 2317690af3fSArnaldo Carvalho de Melo value) != 0) 2327c657876SArnaldo Carvalho de Melo goto out_invalid_option; 2337c657876SArnaldo Carvalho de Melo } 2347c657876SArnaldo Carvalho de Melo break; 2357c657876SArnaldo Carvalho de Melo case 192 ... 255: { 2367c657876SArnaldo Carvalho de Melo const u16 idx = value - options; 2377c657876SArnaldo Carvalho de Melo 2387690af3fSArnaldo Carvalho de Melo if (ccid_hc_tx_parse_options(dp->dccps_hc_tx_ccid, sk, 2397690af3fSArnaldo Carvalho de Melo opt, len, idx, 2407690af3fSArnaldo Carvalho de Melo value) != 0) 2417c657876SArnaldo Carvalho de Melo goto out_invalid_option; 2427c657876SArnaldo Carvalho de Melo } 2437c657876SArnaldo Carvalho de Melo break; 2447c657876SArnaldo Carvalho de Melo default: 2457690af3fSArnaldo Carvalho de Melo pr_info("DCCP(%p): option %d(len=%d) not " 2467690af3fSArnaldo Carvalho de Melo "implemented, ignoring\n", 2477c657876SArnaldo Carvalho de Melo sk, opt, len); 2487c657876SArnaldo Carvalho de Melo break; 2497c657876SArnaldo Carvalho de Melo } 250afe00251SAndrea Bittau 251afe00251SAndrea Bittau if (opt != DCCPO_MANDATORY) 252afe00251SAndrea Bittau mandatory = 0; 2537c657876SArnaldo Carvalho de Melo } 2547c657876SArnaldo Carvalho de Melo 2556df9424aSArnaldo Carvalho de Melo /* mandatory was the last byte in option list -> reset connection */ 2566df9424aSArnaldo Carvalho de Melo if (mandatory) 2576df9424aSArnaldo Carvalho de Melo goto out_invalid_option; 2586df9424aSArnaldo Carvalho de Melo 2597c657876SArnaldo Carvalho de Melo return 0; 2607c657876SArnaldo Carvalho de Melo 2617c657876SArnaldo Carvalho de Melo out_invalid_option: 2627c657876SArnaldo Carvalho de Melo DCCP_INC_STATS_BH(DCCP_MIB_INVALIDOPT); 2637c657876SArnaldo Carvalho de Melo DCCP_SKB_CB(skb)->dccpd_reset_code = DCCP_RESET_CODE_OPTION_ERROR; 2647c657876SArnaldo Carvalho de Melo pr_info("DCCP(%p): invalid option %d, len=%d\n", sk, opt, len); 2657c657876SArnaldo Carvalho de Melo return -1; 2667c657876SArnaldo Carvalho de Melo } 2677c657876SArnaldo Carvalho de Melo 268b61fafc4SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_parse_options); 269b61fafc4SArnaldo Carvalho de Melo 2707c657876SArnaldo Carvalho de Melo static void dccp_encode_value_var(const u32 value, unsigned char *to, 2717c657876SArnaldo Carvalho de Melo const unsigned int len) 2727c657876SArnaldo Carvalho de Melo { 2737c657876SArnaldo Carvalho de Melo if (len > 3) 2747c657876SArnaldo Carvalho de Melo *to++ = (value & 0xFF000000) >> 24; 2757c657876SArnaldo Carvalho de Melo if (len > 2) 2767c657876SArnaldo Carvalho de Melo *to++ = (value & 0xFF0000) >> 16; 2777c657876SArnaldo Carvalho de Melo if (len > 1) 2787c657876SArnaldo Carvalho de Melo *to++ = (value & 0xFF00) >> 8; 2797c657876SArnaldo Carvalho de Melo if (len > 0) 2807c657876SArnaldo Carvalho de Melo *to++ = (value & 0xFF); 2817c657876SArnaldo Carvalho de Melo } 2827c657876SArnaldo Carvalho de Melo 2837c657876SArnaldo Carvalho de Melo static inline int dccp_ndp_len(const int ndp) 2847c657876SArnaldo Carvalho de Melo { 2857c657876SArnaldo Carvalho de Melo return likely(ndp <= 0xFF) ? 1 : ndp <= 0xFFFF ? 2 : 3; 2867c657876SArnaldo Carvalho de Melo } 2877c657876SArnaldo Carvalho de Melo 2882d0817d1SArnaldo Carvalho de Melo int dccp_insert_option(struct sock *sk, struct sk_buff *skb, 2897c657876SArnaldo Carvalho de Melo const unsigned char option, 2907c657876SArnaldo Carvalho de Melo const void *value, const unsigned char len) 2917c657876SArnaldo Carvalho de Melo { 2927c657876SArnaldo Carvalho de Melo unsigned char *to; 2937c657876SArnaldo Carvalho de Melo 2942d0817d1SArnaldo Carvalho de Melo if (DCCP_SKB_CB(skb)->dccpd_opt_len + len + 2 > DCCP_MAX_OPT_LEN) 2952d0817d1SArnaldo Carvalho de Melo return -1; 2967c657876SArnaldo Carvalho de Melo 2977c657876SArnaldo Carvalho de Melo DCCP_SKB_CB(skb)->dccpd_opt_len += len + 2; 2987c657876SArnaldo Carvalho de Melo 2997c657876SArnaldo Carvalho de Melo to = skb_push(skb, len + 2); 3007c657876SArnaldo Carvalho de Melo *to++ = option; 3017c657876SArnaldo Carvalho de Melo *to++ = len + 2; 3027c657876SArnaldo Carvalho de Melo 3037c657876SArnaldo Carvalho de Melo memcpy(to, value, len); 3042d0817d1SArnaldo Carvalho de Melo return 0; 3057c657876SArnaldo Carvalho de Melo } 3067c657876SArnaldo Carvalho de Melo 3077c657876SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_insert_option); 3087c657876SArnaldo Carvalho de Melo 3092d0817d1SArnaldo Carvalho de Melo static int dccp_insert_option_ndp(struct sock *sk, struct sk_buff *skb) 3107c657876SArnaldo Carvalho de Melo { 3117c657876SArnaldo Carvalho de Melo struct dccp_sock *dp = dccp_sk(sk); 3127c657876SArnaldo Carvalho de Melo int ndp = dp->dccps_ndp_count; 3137c657876SArnaldo Carvalho de Melo 3147c657876SArnaldo Carvalho de Melo if (dccp_non_data_packet(skb)) 3157c657876SArnaldo Carvalho de Melo ++dp->dccps_ndp_count; 3167c657876SArnaldo Carvalho de Melo else 3177c657876SArnaldo Carvalho de Melo dp->dccps_ndp_count = 0; 3187c657876SArnaldo Carvalho de Melo 3197c657876SArnaldo Carvalho de Melo if (ndp > 0) { 3207c657876SArnaldo Carvalho de Melo unsigned char *ptr; 3217c657876SArnaldo Carvalho de Melo const int ndp_len = dccp_ndp_len(ndp); 3227c657876SArnaldo Carvalho de Melo const int len = ndp_len + 2; 3237c657876SArnaldo Carvalho de Melo 3247c657876SArnaldo Carvalho de Melo if (DCCP_SKB_CB(skb)->dccpd_opt_len + len > DCCP_MAX_OPT_LEN) 3252d0817d1SArnaldo Carvalho de Melo return -1; 3267c657876SArnaldo Carvalho de Melo 3277c657876SArnaldo Carvalho de Melo DCCP_SKB_CB(skb)->dccpd_opt_len += len; 3287c657876SArnaldo Carvalho de Melo 3297c657876SArnaldo Carvalho de Melo ptr = skb_push(skb, len); 3307c657876SArnaldo Carvalho de Melo *ptr++ = DCCPO_NDP_COUNT; 3317c657876SArnaldo Carvalho de Melo *ptr++ = len; 3327c657876SArnaldo Carvalho de Melo dccp_encode_value_var(ndp, ptr, ndp_len); 3337c657876SArnaldo Carvalho de Melo } 3342d0817d1SArnaldo Carvalho de Melo 3352d0817d1SArnaldo Carvalho de Melo return 0; 3367c657876SArnaldo Carvalho de Melo } 3377c657876SArnaldo Carvalho de Melo 3387c657876SArnaldo Carvalho de Melo static inline int dccp_elapsed_time_len(const u32 elapsed_time) 3397c657876SArnaldo Carvalho de Melo { 340b1c9fe7bSIan McDonald return elapsed_time == 0 ? 0 : elapsed_time <= 0xFFFF ? 2 : 4; 3417c657876SArnaldo Carvalho de Melo } 3427c657876SArnaldo Carvalho de Melo 3432d0817d1SArnaldo Carvalho de Melo int dccp_insert_option_elapsed_time(struct sock *sk, struct sk_buff *skb, 3447c657876SArnaldo Carvalho de Melo u32 elapsed_time) 3457c657876SArnaldo Carvalho de Melo { 3467c657876SArnaldo Carvalho de Melo const int elapsed_time_len = dccp_elapsed_time_len(elapsed_time); 3477c657876SArnaldo Carvalho de Melo const int len = 2 + elapsed_time_len; 3487c657876SArnaldo Carvalho de Melo unsigned char *to; 3497c657876SArnaldo Carvalho de Melo 3501bc09869SIan McDonald if (elapsed_time_len == 0) 3512d0817d1SArnaldo Carvalho de Melo return 0; 3527c657876SArnaldo Carvalho de Melo 3532d0817d1SArnaldo Carvalho de Melo if (DCCP_SKB_CB(skb)->dccpd_opt_len + len > DCCP_MAX_OPT_LEN) 3542d0817d1SArnaldo Carvalho de Melo return -1; 3557c657876SArnaldo Carvalho de Melo 3567c657876SArnaldo Carvalho de Melo DCCP_SKB_CB(skb)->dccpd_opt_len += len; 3577c657876SArnaldo Carvalho de Melo 3587c657876SArnaldo Carvalho de Melo to = skb_push(skb, len); 3597c657876SArnaldo Carvalho de Melo *to++ = DCCPO_ELAPSED_TIME; 3607c657876SArnaldo Carvalho de Melo *to++ = len; 3617c657876SArnaldo Carvalho de Melo 3621bc09869SIan McDonald if (elapsed_time_len == 2) { 36360fe62e7SAndrea Bittau const __be16 var16 = htons((u16)elapsed_time); 3641bc09869SIan McDonald memcpy(to, &var16, 2); 3651bc09869SIan McDonald } else { 36660fe62e7SAndrea Bittau const __be32 var32 = htonl(elapsed_time); 3671bc09869SIan McDonald memcpy(to, &var32, 4); 3681bc09869SIan McDonald } 3697c657876SArnaldo Carvalho de Melo 3702d0817d1SArnaldo Carvalho de Melo return 0; 3717c657876SArnaldo Carvalho de Melo } 3727c657876SArnaldo Carvalho de Melo 373d4b81ff7SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_insert_option_elapsed_time); 3747c657876SArnaldo Carvalho de Melo 375b0e56780SArnaldo Carvalho de Melo void dccp_timestamp(const struct sock *sk, struct timeval *tv) 376b0e56780SArnaldo Carvalho de Melo { 377b0e56780SArnaldo Carvalho de Melo const struct dccp_sock *dp = dccp_sk(sk); 378b0e56780SArnaldo Carvalho de Melo 379b0e56780SArnaldo Carvalho de Melo do_gettimeofday(tv); 380b0e56780SArnaldo Carvalho de Melo tv->tv_sec -= dp->dccps_epoch.tv_sec; 381b0e56780SArnaldo Carvalho de Melo tv->tv_usec -= dp->dccps_epoch.tv_usec; 382b0e56780SArnaldo Carvalho de Melo 383b0e56780SArnaldo Carvalho de Melo while (tv->tv_usec < 0) { 384b0e56780SArnaldo Carvalho de Melo tv->tv_sec--; 385b0e56780SArnaldo Carvalho de Melo tv->tv_usec += USEC_PER_SEC; 386b0e56780SArnaldo Carvalho de Melo } 387b0e56780SArnaldo Carvalho de Melo } 388b0e56780SArnaldo Carvalho de Melo 389b0e56780SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_timestamp); 390b0e56780SArnaldo Carvalho de Melo 3912d0817d1SArnaldo Carvalho de Melo int dccp_insert_option_timestamp(struct sock *sk, struct sk_buff *skb) 3927c657876SArnaldo Carvalho de Melo { 3931bc09869SIan McDonald struct timeval tv; 39460fe62e7SAndrea Bittau __be32 now; 3951bc09869SIan McDonald 396b0e56780SArnaldo Carvalho de Melo dccp_timestamp(sk, &tv); 39760fe62e7SAndrea Bittau now = htonl(timeval_usecs(&tv) / 10); 3981bc09869SIan McDonald /* yes this will overflow but that is the point as we want a 3991bc09869SIan McDonald * 10 usec 32 bit timer which mean it wraps every 11.9 hours */ 4001bc09869SIan McDonald 4012d0817d1SArnaldo Carvalho de Melo return dccp_insert_option(sk, skb, DCCPO_TIMESTAMP, &now, sizeof(now)); 4027c657876SArnaldo Carvalho de Melo } 4037c657876SArnaldo Carvalho de Melo 404d4b81ff7SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_insert_option_timestamp); 405d4b81ff7SArnaldo Carvalho de Melo 4062d0817d1SArnaldo Carvalho de Melo static int dccp_insert_option_timestamp_echo(struct sock *sk, 4077690af3fSArnaldo Carvalho de Melo struct sk_buff *skb) 4087c657876SArnaldo Carvalho de Melo { 4097c657876SArnaldo Carvalho de Melo struct dccp_sock *dp = dccp_sk(sk); 410b0e56780SArnaldo Carvalho de Melo struct timeval now; 41160fe62e7SAndrea Bittau __be32 tstamp_echo; 412b0e56780SArnaldo Carvalho de Melo u32 elapsed_time; 413b0e56780SArnaldo Carvalho de Melo int len, elapsed_time_len; 4147c657876SArnaldo Carvalho de Melo unsigned char *to; 4157c657876SArnaldo Carvalho de Melo 416b0e56780SArnaldo Carvalho de Melo dccp_timestamp(sk, &now); 417b0e56780SArnaldo Carvalho de Melo elapsed_time = timeval_delta(&now, &dp->dccps_timestamp_time) / 10; 418b0e56780SArnaldo Carvalho de Melo elapsed_time_len = dccp_elapsed_time_len(elapsed_time); 419b0e56780SArnaldo Carvalho de Melo len = 6 + elapsed_time_len; 420b0e56780SArnaldo Carvalho de Melo 4212d0817d1SArnaldo Carvalho de Melo if (DCCP_SKB_CB(skb)->dccpd_opt_len + len > DCCP_MAX_OPT_LEN) 4222d0817d1SArnaldo Carvalho de Melo return -1; 4237c657876SArnaldo Carvalho de Melo 4247c657876SArnaldo Carvalho de Melo DCCP_SKB_CB(skb)->dccpd_opt_len += len; 4257c657876SArnaldo Carvalho de Melo 4267c657876SArnaldo Carvalho de Melo to = skb_push(skb, len); 4277c657876SArnaldo Carvalho de Melo *to++ = DCCPO_TIMESTAMP_ECHO; 4287c657876SArnaldo Carvalho de Melo *to++ = len; 4297c657876SArnaldo Carvalho de Melo 4307c657876SArnaldo Carvalho de Melo tstamp_echo = htonl(dp->dccps_timestamp_echo); 4317c657876SArnaldo Carvalho de Melo memcpy(to, &tstamp_echo, 4); 4327c657876SArnaldo Carvalho de Melo to += 4; 4331bc09869SIan McDonald 4341bc09869SIan McDonald if (elapsed_time_len == 2) { 43560fe62e7SAndrea Bittau const __be16 var16 = htons((u16)elapsed_time); 4361bc09869SIan McDonald memcpy(to, &var16, 2); 4371bc09869SIan McDonald } else if (elapsed_time_len == 4) { 43860fe62e7SAndrea Bittau const __be32 var32 = htonl(elapsed_time); 4391bc09869SIan McDonald memcpy(to, &var32, 4); 4401bc09869SIan McDonald } 4417c657876SArnaldo Carvalho de Melo 4427c657876SArnaldo Carvalho de Melo dp->dccps_timestamp_echo = 0; 4431bc09869SIan McDonald dp->dccps_timestamp_time.tv_sec = 0; 4441bc09869SIan McDonald dp->dccps_timestamp_time.tv_usec = 0; 4452d0817d1SArnaldo Carvalho de Melo return 0; 4467c657876SArnaldo Carvalho de Melo } 4477c657876SArnaldo Carvalho de Melo 448afe00251SAndrea Bittau static int dccp_insert_feat_opt(struct sk_buff *skb, u8 type, u8 feat, 449afe00251SAndrea Bittau u8 *val, u8 len) 450afe00251SAndrea Bittau { 451afe00251SAndrea Bittau u8 *to; 452afe00251SAndrea Bittau 453afe00251SAndrea Bittau if (DCCP_SKB_CB(skb)->dccpd_opt_len + len + 3 > DCCP_MAX_OPT_LEN) { 454afe00251SAndrea Bittau LIMIT_NETDEBUG(KERN_INFO "DCCP: packet too small" 455afe00251SAndrea Bittau " to insert feature %d option!\n", feat); 456afe00251SAndrea Bittau return -1; 457afe00251SAndrea Bittau } 458afe00251SAndrea Bittau 459afe00251SAndrea Bittau DCCP_SKB_CB(skb)->dccpd_opt_len += len + 3; 460afe00251SAndrea Bittau 461afe00251SAndrea Bittau to = skb_push(skb, len + 3); 462afe00251SAndrea Bittau *to++ = type; 463afe00251SAndrea Bittau *to++ = len + 3; 464afe00251SAndrea Bittau *to++ = feat; 465afe00251SAndrea Bittau 466afe00251SAndrea Bittau if (len) 467afe00251SAndrea Bittau memcpy(to, val, len); 468afe00251SAndrea Bittau dccp_pr_debug("option %d feat %d len %d\n", type, feat, len); 469afe00251SAndrea Bittau 470afe00251SAndrea Bittau return 0; 471afe00251SAndrea Bittau } 472afe00251SAndrea Bittau 4732d0817d1SArnaldo Carvalho de Melo static int dccp_insert_options_feat(struct sock *sk, struct sk_buff *skb) 474afe00251SAndrea Bittau { 475afe00251SAndrea Bittau struct dccp_sock *dp = dccp_sk(sk); 476a4bf3902SArnaldo Carvalho de Melo struct dccp_minisock *dmsk = dccp_msk(sk); 477afe00251SAndrea Bittau struct dccp_opt_pend *opt, *next; 478afe00251SAndrea Bittau int change = 0; 479afe00251SAndrea Bittau 480afe00251SAndrea Bittau /* confirm any options [NN opts] */ 481a4bf3902SArnaldo Carvalho de Melo list_for_each_entry_safe(opt, next, &dmsk->dccpms_conf, dccpop_node) { 482afe00251SAndrea Bittau dccp_insert_feat_opt(skb, opt->dccpop_type, 483afe00251SAndrea Bittau opt->dccpop_feat, opt->dccpop_val, 484afe00251SAndrea Bittau opt->dccpop_len); 485afe00251SAndrea Bittau /* fear empty confirms */ 486afe00251SAndrea Bittau if (opt->dccpop_val) 487afe00251SAndrea Bittau kfree(opt->dccpop_val); 488afe00251SAndrea Bittau kfree(opt); 489afe00251SAndrea Bittau } 490a4bf3902SArnaldo Carvalho de Melo INIT_LIST_HEAD(&dmsk->dccpms_conf); 491afe00251SAndrea Bittau 492afe00251SAndrea Bittau /* see which features we need to send */ 493a4bf3902SArnaldo Carvalho de Melo list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) { 494afe00251SAndrea Bittau /* see if we need to send any confirm */ 495afe00251SAndrea Bittau if (opt->dccpop_sc) { 496afe00251SAndrea Bittau dccp_insert_feat_opt(skb, opt->dccpop_type + 1, 497afe00251SAndrea Bittau opt->dccpop_feat, 498afe00251SAndrea Bittau opt->dccpop_sc->dccpoc_val, 499afe00251SAndrea Bittau opt->dccpop_sc->dccpoc_len); 500afe00251SAndrea Bittau 501afe00251SAndrea Bittau BUG_ON(!opt->dccpop_sc->dccpoc_val); 502afe00251SAndrea Bittau kfree(opt->dccpop_sc->dccpoc_val); 503afe00251SAndrea Bittau kfree(opt->dccpop_sc); 504afe00251SAndrea Bittau opt->dccpop_sc = NULL; 505afe00251SAndrea Bittau } 506afe00251SAndrea Bittau 507afe00251SAndrea Bittau /* any option not confirmed, re-send it */ 508afe00251SAndrea Bittau if (!opt->dccpop_conf) { 509afe00251SAndrea Bittau dccp_insert_feat_opt(skb, opt->dccpop_type, 510afe00251SAndrea Bittau opt->dccpop_feat, opt->dccpop_val, 511afe00251SAndrea Bittau opt->dccpop_len); 512afe00251SAndrea Bittau change++; 513afe00251SAndrea Bittau } 514afe00251SAndrea Bittau } 515afe00251SAndrea Bittau 516afe00251SAndrea Bittau /* Retransmit timer. 517afe00251SAndrea Bittau * If this is the master listening sock, we don't set a timer on it. It 518afe00251SAndrea Bittau * should be fine because if the dude doesn't receive our RESPONSE 519afe00251SAndrea Bittau * [which will contain the CHANGE] he will send another REQUEST which 520afe00251SAndrea Bittau * will "retrnasmit" the change. 521afe00251SAndrea Bittau */ 522afe00251SAndrea Bittau if (change && dp->dccps_role != DCCP_ROLE_LISTEN) { 523afe00251SAndrea Bittau dccp_pr_debug("reset feat negotiation timer %p\n", sk); 524afe00251SAndrea Bittau 525afe00251SAndrea Bittau /* XXX don't reset the timer on re-transmissions. I.e. reset it 526afe00251SAndrea Bittau * only when sending new stuff i guess. Currently the timer 527afe00251SAndrea Bittau * never backs off because on re-transmission it just resets it! 528afe00251SAndrea Bittau */ 529afe00251SAndrea Bittau inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS, 530afe00251SAndrea Bittau inet_csk(sk)->icsk_rto, DCCP_RTO_MAX); 531afe00251SAndrea Bittau } 5322d0817d1SArnaldo Carvalho de Melo 5332d0817d1SArnaldo Carvalho de Melo return 0; 534afe00251SAndrea Bittau } 535afe00251SAndrea Bittau 5362d0817d1SArnaldo Carvalho de Melo int dccp_insert_options(struct sock *sk, struct sk_buff *skb) 5377c657876SArnaldo Carvalho de Melo { 5387c657876SArnaldo Carvalho de Melo struct dccp_sock *dp = dccp_sk(sk); 539a4bf3902SArnaldo Carvalho de Melo struct dccp_minisock *dmsk = dccp_msk(sk); 5407c657876SArnaldo Carvalho de Melo 5417c657876SArnaldo Carvalho de Melo DCCP_SKB_CB(skb)->dccpd_opt_len = 0; 5427c657876SArnaldo Carvalho de Melo 543a4bf3902SArnaldo Carvalho de Melo if (dmsk->dccpms_send_ndp_count && 5442d0817d1SArnaldo Carvalho de Melo dccp_insert_option_ndp(sk, skb)) 5452d0817d1SArnaldo Carvalho de Melo return -1; 5467c657876SArnaldo Carvalho de Melo 5477c657876SArnaldo Carvalho de Melo if (!dccp_packet_without_ack(skb)) { 548a4bf3902SArnaldo Carvalho de Melo if (dmsk->dccpms_send_ack_vector && 5492d0817d1SArnaldo Carvalho de Melo dccp_ackvec_pending(dp->dccps_hc_rx_ackvec) && 5502d0817d1SArnaldo Carvalho de Melo dccp_insert_option_ackvec(sk, skb)) 5512d0817d1SArnaldo Carvalho de Melo return -1; 5522d0817d1SArnaldo Carvalho de Melo 5532d0817d1SArnaldo Carvalho de Melo if (dp->dccps_timestamp_echo != 0 && 5542d0817d1SArnaldo Carvalho de Melo dccp_insert_option_timestamp_echo(sk, skb)) 5552d0817d1SArnaldo Carvalho de Melo return -1; 5567c657876SArnaldo Carvalho de Melo } 5577c657876SArnaldo Carvalho de Melo 558507d37cfSArnaldo Carvalho de Melo if (dp->dccps_hc_rx_insert_options) { 5592d0817d1SArnaldo Carvalho de Melo if (ccid_hc_rx_insert_options(dp->dccps_hc_rx_ccid, sk, skb)) 5602d0817d1SArnaldo Carvalho de Melo return -1; 561507d37cfSArnaldo Carvalho de Melo dp->dccps_hc_rx_insert_options = 0; 562507d37cfSArnaldo Carvalho de Melo } 563507d37cfSArnaldo Carvalho de Melo if (dp->dccps_hc_tx_insert_options) { 5642d0817d1SArnaldo Carvalho de Melo if (ccid_hc_tx_insert_options(dp->dccps_hc_tx_ccid, sk, skb)) 5652d0817d1SArnaldo Carvalho de Melo return -1; 566507d37cfSArnaldo Carvalho de Melo dp->dccps_hc_tx_insert_options = 0; 567507d37cfSArnaldo Carvalho de Melo } 5687c657876SArnaldo Carvalho de Melo 569afe00251SAndrea Bittau /* Feature negotiation */ 570afe00251SAndrea Bittau /* Data packets can't do feat negotiation */ 5712d0817d1SArnaldo Carvalho de Melo if (DCCP_SKB_CB(skb)->dccpd_type != DCCP_PKT_DATA && 5722d0817d1SArnaldo Carvalho de Melo DCCP_SKB_CB(skb)->dccpd_type != DCCP_PKT_DATAACK && 5732d0817d1SArnaldo Carvalho de Melo dccp_insert_options_feat(sk, skb)) 5742d0817d1SArnaldo Carvalho de Melo return -1; 575afe00251SAndrea Bittau 5767c657876SArnaldo Carvalho de Melo /* XXX: insert other options when appropriate */ 5777c657876SArnaldo Carvalho de Melo 5787c657876SArnaldo Carvalho de Melo if (DCCP_SKB_CB(skb)->dccpd_opt_len != 0) { 5797c657876SArnaldo Carvalho de Melo /* The length of all options has to be a multiple of 4 */ 5807c657876SArnaldo Carvalho de Melo int padding = DCCP_SKB_CB(skb)->dccpd_opt_len % 4; 5817c657876SArnaldo Carvalho de Melo 5827c657876SArnaldo Carvalho de Melo if (padding != 0) { 5837c657876SArnaldo Carvalho de Melo padding = 4 - padding; 5847c657876SArnaldo Carvalho de Melo memset(skb_push(skb, padding), 0, padding); 5857c657876SArnaldo Carvalho de Melo DCCP_SKB_CB(skb)->dccpd_opt_len += padding; 5867c657876SArnaldo Carvalho de Melo } 5877c657876SArnaldo Carvalho de Melo } 5882d0817d1SArnaldo Carvalho de Melo 5892d0817d1SArnaldo Carvalho de Melo return 0; 5907c657876SArnaldo Carvalho de Melo } 591