options.c 17 KB

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