options.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. /*
  2. * net/dccp/options.c
  3. *
  4. * An implementation of the DCCP protocol
  5. * 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 <ian.mcdonald@jandi.co.nz>
  8. *
  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/dccp.h>
  15. #include <linux/module.h>
  16. #include <linux/types.h>
  17. #include <asm/unaligned.h>
  18. #include <linux/kernel.h>
  19. #include <linux/skbuff.h>
  20. #include "ackvec.h"
  21. #include "ccid.h"
  22. #include "dccp.h"
  23. #include "feat.h"
  24. int sysctl_dccp_feat_sequence_window = DCCPF_INITIAL_SEQUENCE_WINDOW;
  25. int sysctl_dccp_feat_rx_ccid = DCCPF_INITIAL_CCID;
  26. int sysctl_dccp_feat_tx_ccid = DCCPF_INITIAL_CCID;
  27. int sysctl_dccp_feat_send_ack_vector = DCCPF_INITIAL_SEND_ACK_VECTOR;
  28. u64 dccp_decode_value_var(const u8 *bf, const u8 len)
  29. {
  30. u64 value = 0;
  31. if (len >= DCCP_OPTVAL_MAXLEN)
  32. value += ((u64)*bf++) << 40;
  33. if (len > 4)
  34. value += ((u64)*bf++) << 32;
  35. if (len > 3)
  36. value += ((u64)*bf++) << 24;
  37. if (len > 2)
  38. value += ((u64)*bf++) << 16;
  39. if (len > 1)
  40. value += ((u64)*bf++) << 8;
  41. if (len > 0)
  42. value += *bf;
  43. return value;
  44. }
  45. /**
  46. * dccp_parse_options - Parse DCCP options present in @skb
  47. * @sk: client|server|listening dccp socket (when @dreq != NULL)
  48. * @dreq: request socket to use during connection setup, or NULL
  49. */
  50. int dccp_parse_options(struct sock *sk, struct dccp_request_sock *dreq,
  51. struct sk_buff *skb)
  52. {
  53. struct dccp_sock *dp = dccp_sk(sk);
  54. const struct dccp_hdr *dh = dccp_hdr(skb);
  55. const u8 pkt_type = DCCP_SKB_CB(skb)->dccpd_type;
  56. u64 ackno = DCCP_SKB_CB(skb)->dccpd_ack_seq;
  57. unsigned char *options = (unsigned char *)dh + dccp_hdr_len(skb);
  58. unsigned char *opt_ptr = options;
  59. const unsigned char *opt_end = (unsigned char *)dh +
  60. (dh->dccph_doff * 4);
  61. struct dccp_options_received *opt_recv = &dp->dccps_options_received;
  62. unsigned char opt, len;
  63. unsigned char *uninitialized_var(value);
  64. u32 elapsed_time;
  65. __be32 opt_val;
  66. int rc;
  67. int mandatory = 0;
  68. memset(opt_recv, 0, sizeof(*opt_recv));
  69. opt = len = 0;
  70. while (opt_ptr != opt_end) {
  71. opt = *opt_ptr++;
  72. len = 0;
  73. value = NULL;
  74. /* Check if this isn't a single byte option */
  75. if (opt > DCCPO_MAX_RESERVED) {
  76. if (opt_ptr == opt_end)
  77. goto out_nonsensical_length;
  78. len = *opt_ptr++;
  79. if (len < 2)
  80. goto out_nonsensical_length;
  81. /*
  82. * Remove the type and len fields, leaving
  83. * just the value size
  84. */
  85. len -= 2;
  86. value = opt_ptr;
  87. opt_ptr += len;
  88. if (opt_ptr > opt_end)
  89. goto out_nonsensical_length;
  90. }
  91. /*
  92. * CCID-Specific Options (from RFC 4340, sec. 10.3):
  93. *
  94. * Option numbers 128 through 191 are for options sent from the
  95. * HC-Sender to the HC-Receiver; option numbers 192 through 255
  96. * are for options sent from the HC-Receiver to the HC-Sender.
  97. *
  98. * CCID-specific options are ignored during connection setup, as
  99. * negotiation may still be in progress (see RFC 4340, 10.3).
  100. * The same applies to Ack Vectors, as these depend on the CCID.
  101. *
  102. */
  103. if (dreq != NULL && (opt >= 128 ||
  104. opt == DCCPO_ACK_VECTOR_0 || opt == DCCPO_ACK_VECTOR_1))
  105. goto ignore_option;
  106. switch (opt) {
  107. case DCCPO_PADDING:
  108. break;
  109. case DCCPO_MANDATORY:
  110. if (mandatory)
  111. goto out_invalid_option;
  112. if (pkt_type != DCCP_PKT_DATA)
  113. mandatory = 1;
  114. break;
  115. case DCCPO_NDP_COUNT:
  116. if (len > 6)
  117. goto out_invalid_option;
  118. opt_recv->dccpor_ndp = dccp_decode_value_var(value, len);
  119. dccp_pr_debug("%s opt: NDP count=%llu\n", dccp_role(sk),
  120. (unsigned long long)opt_recv->dccpor_ndp);
  121. break;
  122. case DCCPO_CHANGE_L ... DCCPO_CONFIRM_R:
  123. if (pkt_type == DCCP_PKT_DATA) /* RFC 4340, 6 */
  124. break;
  125. rc = dccp_feat_parse_options(sk, dreq, mandatory, opt,
  126. *value, value + 1, len - 1);
  127. if (rc)
  128. goto out_featneg_failed;
  129. break;
  130. case DCCPO_ACK_VECTOR_0:
  131. case DCCPO_ACK_VECTOR_1:
  132. if (dccp_packet_without_ack(skb)) /* RFC 4340, 11.4 */
  133. break;
  134. if (dccp_msk(sk)->dccpms_send_ack_vector &&
  135. dccp_ackvec_parse(sk, skb, &ackno, opt, value, len))
  136. goto out_invalid_option;
  137. break;
  138. case DCCPO_TIMESTAMP:
  139. if (len != 4)
  140. goto out_invalid_option;
  141. /*
  142. * RFC 4340 13.1: "The precise time corresponding to
  143. * Timestamp Value zero is not specified". We use
  144. * zero to indicate absence of a meaningful timestamp.
  145. */
  146. opt_val = get_unaligned((__be32 *)value);
  147. if (unlikely(opt_val == 0)) {
  148. DCCP_WARN("Timestamp with zero value\n");
  149. break;
  150. }
  151. if (dreq != NULL) {
  152. dreq->dreq_timestamp_echo = ntohl(opt_val);
  153. dreq->dreq_timestamp_time = dccp_timestamp();
  154. } else {
  155. opt_recv->dccpor_timestamp =
  156. dp->dccps_timestamp_echo = ntohl(opt_val);
  157. dp->dccps_timestamp_time = dccp_timestamp();
  158. }
  159. dccp_pr_debug("%s rx opt: TIMESTAMP=%u, ackno=%llu\n",
  160. dccp_role(sk), ntohl(opt_val),
  161. (unsigned long long)
  162. DCCP_SKB_CB(skb)->dccpd_ack_seq);
  163. break;
  164. case DCCPO_TIMESTAMP_ECHO:
  165. if (len != 4 && len != 6 && len != 8)
  166. goto out_invalid_option;
  167. opt_val = get_unaligned((__be32 *)value);
  168. opt_recv->dccpor_timestamp_echo = ntohl(opt_val);
  169. dccp_pr_debug("%s rx opt: TIMESTAMP_ECHO=%u, len=%d, "
  170. "ackno=%llu", dccp_role(sk),
  171. opt_recv->dccpor_timestamp_echo,
  172. len + 2,
  173. (unsigned long long)
  174. DCCP_SKB_CB(skb)->dccpd_ack_seq);
  175. value += 4;
  176. if (len == 4) { /* no elapsed time included */
  177. dccp_pr_debug_cat("\n");
  178. break;
  179. }
  180. if (len == 6) { /* 2-byte elapsed time */
  181. __be16 opt_val2 = get_unaligned((__be16 *)value);
  182. elapsed_time = ntohs(opt_val2);
  183. } else { /* 4-byte elapsed time */
  184. opt_val = get_unaligned((__be32 *)value);
  185. elapsed_time = ntohl(opt_val);
  186. }
  187. dccp_pr_debug_cat(", ELAPSED_TIME=%u\n", elapsed_time);
  188. /* Give precedence to the biggest ELAPSED_TIME */
  189. if (elapsed_time > opt_recv->dccpor_elapsed_time)
  190. opt_recv->dccpor_elapsed_time = elapsed_time;
  191. break;
  192. case DCCPO_ELAPSED_TIME:
  193. if (dccp_packet_without_ack(skb)) /* RFC 4340, 13.2 */
  194. break;
  195. if (len == 2) {
  196. __be16 opt_val2 = get_unaligned((__be16 *)value);
  197. elapsed_time = ntohs(opt_val2);
  198. } else if (len == 4) {
  199. opt_val = get_unaligned((__be32 *)value);
  200. elapsed_time = ntohl(opt_val);
  201. } else {
  202. goto out_invalid_option;
  203. }
  204. if (elapsed_time > opt_recv->dccpor_elapsed_time)
  205. opt_recv->dccpor_elapsed_time = elapsed_time;
  206. dccp_pr_debug("%s rx opt: ELAPSED_TIME=%d\n",
  207. dccp_role(sk), elapsed_time);
  208. break;
  209. case 128 ... 191: {
  210. const u16 idx = value - options;
  211. if (ccid_hc_rx_parse_options(dp->dccps_hc_rx_ccid, sk,
  212. opt, len, idx,
  213. value) != 0)
  214. goto out_invalid_option;
  215. }
  216. break;
  217. case 192 ... 255: {
  218. const u16 idx = value - options;
  219. if (ccid_hc_tx_parse_options(dp->dccps_hc_tx_ccid, sk,
  220. opt, len, idx,
  221. value) != 0)
  222. goto out_invalid_option;
  223. }
  224. break;
  225. default:
  226. DCCP_CRIT("DCCP(%p): option %d(len=%d) not "
  227. "implemented, ignoring", sk, opt, len);
  228. break;
  229. }
  230. ignore_option:
  231. if (opt != DCCPO_MANDATORY)
  232. mandatory = 0;
  233. }
  234. /* mandatory was the last byte in option list -> reset connection */
  235. if (mandatory)
  236. goto out_invalid_option;
  237. out_nonsensical_length:
  238. /* RFC 4340, 5.8: ignore option and all remaining option space */
  239. return 0;
  240. out_invalid_option:
  241. DCCP_INC_STATS_BH(DCCP_MIB_INVALIDOPT);
  242. rc = DCCP_RESET_CODE_OPTION_ERROR;
  243. out_featneg_failed:
  244. DCCP_WARN("DCCP(%p): Option %d (len=%d) error=%u\n", sk, opt, len, rc);
  245. DCCP_SKB_CB(skb)->dccpd_reset_code = rc;
  246. DCCP_SKB_CB(skb)->dccpd_reset_data[0] = opt;
  247. DCCP_SKB_CB(skb)->dccpd_reset_data[1] = len > 0 ? value[0] : 0;
  248. DCCP_SKB_CB(skb)->dccpd_reset_data[2] = len > 1 ? value[1] : 0;
  249. return -1;
  250. }
  251. EXPORT_SYMBOL_GPL(dccp_parse_options);
  252. void dccp_encode_value_var(const u64 value, u8 *to, const u8 len)
  253. {
  254. if (len >= DCCP_OPTVAL_MAXLEN)
  255. *to++ = (value & 0xFF0000000000ull) >> 40;
  256. if (len > 4)
  257. *to++ = (value & 0xFF00000000ull) >> 32;
  258. if (len > 3)
  259. *to++ = (value & 0xFF000000) >> 24;
  260. if (len > 2)
  261. *to++ = (value & 0xFF0000) >> 16;
  262. if (len > 1)
  263. *to++ = (value & 0xFF00) >> 8;
  264. if (len > 0)
  265. *to++ = (value & 0xFF);
  266. }
  267. static inline u8 dccp_ndp_len(const u64 ndp)
  268. {
  269. if (likely(ndp <= 0xFF))
  270. return 1;
  271. return likely(ndp <= USHORT_MAX) ? 2 : (ndp <= UINT_MAX ? 4 : 6);
  272. }
  273. int dccp_insert_option(struct sock *sk, struct sk_buff *skb,
  274. const unsigned char option,
  275. const void *value, const unsigned char len)
  276. {
  277. unsigned char *to;
  278. if (DCCP_SKB_CB(skb)->dccpd_opt_len + len + 2 > DCCP_MAX_OPT_LEN)
  279. return -1;
  280. DCCP_SKB_CB(skb)->dccpd_opt_len += len + 2;
  281. to = skb_push(skb, len + 2);
  282. *to++ = option;
  283. *to++ = len + 2;
  284. memcpy(to, value, len);
  285. return 0;
  286. }
  287. EXPORT_SYMBOL_GPL(dccp_insert_option);
  288. static int dccp_insert_option_ndp(struct sock *sk, struct sk_buff *skb)
  289. {
  290. struct dccp_sock *dp = dccp_sk(sk);
  291. u64 ndp = dp->dccps_ndp_count;
  292. if (dccp_non_data_packet(skb))
  293. ++dp->dccps_ndp_count;
  294. else
  295. dp->dccps_ndp_count = 0;
  296. if (ndp > 0) {
  297. unsigned char *ptr;
  298. const int ndp_len = dccp_ndp_len(ndp);
  299. const int len = ndp_len + 2;
  300. if (DCCP_SKB_CB(skb)->dccpd_opt_len + len > DCCP_MAX_OPT_LEN)
  301. return -1;
  302. DCCP_SKB_CB(skb)->dccpd_opt_len += len;
  303. ptr = skb_push(skb, len);
  304. *ptr++ = DCCPO_NDP_COUNT;
  305. *ptr++ = len;
  306. dccp_encode_value_var(ndp, ptr, ndp_len);
  307. }
  308. return 0;
  309. }
  310. static inline int dccp_elapsed_time_len(const u32 elapsed_time)
  311. {
  312. return elapsed_time == 0 ? 0 : elapsed_time <= 0xFFFF ? 2 : 4;
  313. }
  314. int dccp_insert_option_elapsed_time(struct sock *sk, struct sk_buff *skb,
  315. u32 elapsed_time)
  316. {
  317. const int elapsed_time_len = dccp_elapsed_time_len(elapsed_time);
  318. const int len = 2 + elapsed_time_len;
  319. unsigned char *to;
  320. if (elapsed_time_len == 0)
  321. return 0;
  322. if (DCCP_SKB_CB(skb)->dccpd_opt_len + len > DCCP_MAX_OPT_LEN)
  323. return -1;
  324. DCCP_SKB_CB(skb)->dccpd_opt_len += len;
  325. to = skb_push(skb, len);
  326. *to++ = DCCPO_ELAPSED_TIME;
  327. *to++ = len;
  328. if (elapsed_time_len == 2) {
  329. const __be16 var16 = htons((u16)elapsed_time);
  330. memcpy(to, &var16, 2);
  331. } else {
  332. const __be32 var32 = htonl(elapsed_time);
  333. memcpy(to, &var32, 4);
  334. }
  335. return 0;
  336. }
  337. EXPORT_SYMBOL_GPL(dccp_insert_option_elapsed_time);
  338. int dccp_insert_option_timestamp(struct sock *sk, struct sk_buff *skb)
  339. {
  340. __be32 now = htonl(dccp_timestamp());
  341. /* yes this will overflow but that is the point as we want a
  342. * 10 usec 32 bit timer which mean it wraps every 11.9 hours */
  343. return dccp_insert_option(sk, skb, DCCPO_TIMESTAMP, &now, sizeof(now));
  344. }
  345. EXPORT_SYMBOL_GPL(dccp_insert_option_timestamp);
  346. static int dccp_insert_option_timestamp_echo(struct dccp_sock *dp,
  347. struct dccp_request_sock *dreq,
  348. struct sk_buff *skb)
  349. {
  350. __be32 tstamp_echo;
  351. unsigned char *to;
  352. u32 elapsed_time, elapsed_time_len, len;
  353. if (dreq != NULL) {
  354. elapsed_time = dccp_timestamp() - dreq->dreq_timestamp_time;
  355. tstamp_echo = htonl(dreq->dreq_timestamp_echo);
  356. dreq->dreq_timestamp_echo = 0;
  357. } else {
  358. elapsed_time = dccp_timestamp() - dp->dccps_timestamp_time;
  359. tstamp_echo = htonl(dp->dccps_timestamp_echo);
  360. dp->dccps_timestamp_echo = 0;
  361. }
  362. elapsed_time_len = dccp_elapsed_time_len(elapsed_time);
  363. len = 6 + elapsed_time_len;
  364. if (DCCP_SKB_CB(skb)->dccpd_opt_len + len > DCCP_MAX_OPT_LEN)
  365. return -1;
  366. DCCP_SKB_CB(skb)->dccpd_opt_len += len;
  367. to = skb_push(skb, len);
  368. *to++ = DCCPO_TIMESTAMP_ECHO;
  369. *to++ = len;
  370. memcpy(to, &tstamp_echo, 4);
  371. to += 4;
  372. if (elapsed_time_len == 2) {
  373. const __be16 var16 = htons((u16)elapsed_time);
  374. memcpy(to, &var16, 2);
  375. } else if (elapsed_time_len == 4) {
  376. const __be32 var32 = htonl(elapsed_time);
  377. memcpy(to, &var32, 4);
  378. }
  379. return 0;
  380. }
  381. /**
  382. * dccp_insert_option_mandatory - Mandatory option (5.8.2)
  383. * Note that since we are using skb_push, this function needs to be called
  384. * _after_ inserting the option it is supposed to influence (stack order).
  385. */
  386. int dccp_insert_option_mandatory(struct sk_buff *skb)
  387. {
  388. if (DCCP_SKB_CB(skb)->dccpd_opt_len >= DCCP_MAX_OPT_LEN)
  389. return -1;
  390. DCCP_SKB_CB(skb)->dccpd_opt_len++;
  391. *skb_push(skb, 1) = DCCPO_MANDATORY;
  392. return 0;
  393. }
  394. /**
  395. * dccp_insert_fn_opt - Insert single Feature-Negotiation option into @skb
  396. * @type: %DCCPO_CHANGE_L, %DCCPO_CHANGE_R, %DCCPO_CONFIRM_L, %DCCPO_CONFIRM_R
  397. * @feat: one out of %dccp_feature_numbers
  398. * @val: NN value or SP array (preferred element first) to copy
  399. * @len: true length of @val in bytes (excluding first element repetition)
  400. * @repeat_first: whether to copy the first element of @val twice
  401. * The last argument is used to construct Confirm options, where the preferred
  402. * value and the preference list appear separately (RFC 4340, 6.3.1). Preference
  403. * lists are kept such that the preferred entry is always first, so we only need
  404. * to copy twice, and avoid the overhead of cloning into a bigger array.
  405. */
  406. int dccp_insert_fn_opt(struct sk_buff *skb, u8 type, u8 feat,
  407. u8 *val, u8 len, bool repeat_first)
  408. {
  409. u8 tot_len, *to;
  410. /* take the `Feature' field and possible repetition into account */
  411. if (len > (DCCP_SINGLE_OPT_MAXLEN - 2)) {
  412. DCCP_WARN("length %u for feature %u too large\n", len, feat);
  413. return -1;
  414. }
  415. if (unlikely(val == NULL || len == 0))
  416. len = repeat_first = 0;
  417. tot_len = 3 + repeat_first + len;
  418. if (DCCP_SKB_CB(skb)->dccpd_opt_len + tot_len > DCCP_MAX_OPT_LEN) {
  419. DCCP_WARN("packet too small for feature %d option!\n", feat);
  420. return -1;
  421. }
  422. DCCP_SKB_CB(skb)->dccpd_opt_len += tot_len;
  423. to = skb_push(skb, tot_len);
  424. *to++ = type;
  425. *to++ = tot_len;
  426. *to++ = feat;
  427. if (repeat_first)
  428. *to++ = *val;
  429. if (len)
  430. memcpy(to, val, len);
  431. dccp_pr_debug("%s(%s (%d), ...), length %d\n",
  432. dccp_feat_typename(type),
  433. dccp_feat_name(feat), feat, len);
  434. return 0;
  435. }
  436. /* The length of all options needs to be a multiple of 4 (5.8) */
  437. static void dccp_insert_option_padding(struct sk_buff *skb)
  438. {
  439. int padding = DCCP_SKB_CB(skb)->dccpd_opt_len % 4;
  440. if (padding != 0) {
  441. padding = 4 - padding;
  442. memset(skb_push(skb, padding), 0, padding);
  443. DCCP_SKB_CB(skb)->dccpd_opt_len += padding;
  444. }
  445. }
  446. int dccp_insert_options(struct sock *sk, struct sk_buff *skb)
  447. {
  448. struct dccp_sock *dp = dccp_sk(sk);
  449. struct dccp_minisock *dmsk = dccp_msk(sk);
  450. DCCP_SKB_CB(skb)->dccpd_opt_len = 0;
  451. if (dp->dccps_send_ndp_count && dccp_insert_option_ndp(sk, skb))
  452. return -1;
  453. if (DCCP_SKB_CB(skb)->dccpd_type != DCCP_PKT_DATA) {
  454. /* Feature Negotiation */
  455. if (dccp_feat_insert_opts(dp, NULL, skb))
  456. return -1;
  457. if (DCCP_SKB_CB(skb)->dccpd_type == DCCP_PKT_REQUEST) {
  458. /*
  459. * Obtain RTT sample from Request/Response exchange.
  460. * This is currently used in CCID 3 initialisation.
  461. */
  462. if (dccp_insert_option_timestamp(sk, skb))
  463. return -1;
  464. } else if (dmsk->dccpms_send_ack_vector &&
  465. dccp_ackvec_pending(dp->dccps_hc_rx_ackvec) &&
  466. dccp_insert_option_ackvec(sk, skb)) {
  467. return -1;
  468. }
  469. }
  470. if (dp->dccps_hc_rx_insert_options) {
  471. if (ccid_hc_rx_insert_options(dp->dccps_hc_rx_ccid, sk, skb))
  472. return -1;
  473. dp->dccps_hc_rx_insert_options = 0;
  474. }
  475. if (dp->dccps_timestamp_echo != 0 &&
  476. dccp_insert_option_timestamp_echo(dp, NULL, skb))
  477. return -1;
  478. dccp_insert_option_padding(skb);
  479. return 0;
  480. }
  481. int dccp_insert_options_rsk(struct dccp_request_sock *dreq, struct sk_buff *skb)
  482. {
  483. DCCP_SKB_CB(skb)->dccpd_opt_len = 0;
  484. if (dccp_feat_insert_opts(NULL, dreq, skb))
  485. return -1;
  486. if (dreq->dreq_timestamp_echo != 0 &&
  487. dccp_insert_option_timestamp_echo(NULL, dreq, skb))
  488. return -1;
  489. dccp_insert_option_padding(skb);
  490. return 0;
  491. }