options.c 15 KB

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