options.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  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_ack_ratio = DCCPF_INITIAL_ACK_RATIO;
  28. int sysctl_dccp_feat_send_ack_vector = DCCPF_INITIAL_SEND_ACK_VECTOR;
  29. int sysctl_dccp_feat_send_ndp_count = DCCPF_INITIAL_SEND_NDP_COUNT;
  30. static u32 dccp_decode_value_var(const unsigned char *bf, const u8 len)
  31. {
  32. u32 value = 0;
  33. if (len > 3)
  34. value += *bf++ << 24;
  35. if (len > 2)
  36. value += *bf++ << 16;
  37. if (len > 1)
  38. value += *bf++ << 8;
  39. if (len > 0)
  40. value += *bf;
  41. return value;
  42. }
  43. int dccp_parse_options(struct sock *sk, struct sk_buff *skb)
  44. {
  45. struct dccp_sock *dp = dccp_sk(sk);
  46. const struct dccp_hdr *dh = dccp_hdr(skb);
  47. const u8 pkt_type = DCCP_SKB_CB(skb)->dccpd_type;
  48. u64 ackno = DCCP_SKB_CB(skb)->dccpd_ack_seq;
  49. unsigned char *options = (unsigned char *)dh + dccp_hdr_len(skb);
  50. unsigned char *opt_ptr = options;
  51. const unsigned char *opt_end = (unsigned char *)dh +
  52. (dh->dccph_doff * 4);
  53. struct dccp_options_received *opt_recv = &dp->dccps_options_received;
  54. unsigned char opt, len;
  55. unsigned char *value;
  56. u32 elapsed_time;
  57. __be32 opt_val;
  58. int rc;
  59. int mandatory = 0;
  60. memset(opt_recv, 0, sizeof(*opt_recv));
  61. opt = len = 0;
  62. while (opt_ptr != opt_end) {
  63. opt = *opt_ptr++;
  64. len = 0;
  65. value = NULL;
  66. /* Check if this isn't a single byte option */
  67. if (opt > DCCPO_MAX_RESERVED) {
  68. if (opt_ptr == opt_end)
  69. goto out_invalid_option;
  70. len = *opt_ptr++;
  71. if (len < 3)
  72. goto out_invalid_option;
  73. /*
  74. * Remove the type and len fields, leaving
  75. * just the value size
  76. */
  77. len -= 2;
  78. value = opt_ptr;
  79. opt_ptr += len;
  80. if (opt_ptr > opt_end)
  81. goto out_invalid_option;
  82. }
  83. switch (opt) {
  84. case DCCPO_PADDING:
  85. break;
  86. case DCCPO_MANDATORY:
  87. if (mandatory)
  88. goto out_invalid_option;
  89. if (pkt_type != DCCP_PKT_DATA)
  90. mandatory = 1;
  91. break;
  92. case DCCPO_NDP_COUNT:
  93. if (len > 3)
  94. goto out_invalid_option;
  95. opt_recv->dccpor_ndp = dccp_decode_value_var(value, len);
  96. dccp_pr_debug("%s rx opt: NDP count=%d\n", dccp_role(sk),
  97. opt_recv->dccpor_ndp);
  98. break;
  99. case DCCPO_CHANGE_L:
  100. /* fall through */
  101. case DCCPO_CHANGE_R:
  102. if (len < 2)
  103. goto out_invalid_option;
  104. rc = dccp_feat_change_recv(sk, opt, *value, value + 1,
  105. len - 1);
  106. /*
  107. * When there is a change error, change_recv is
  108. * responsible for dealing with it. i.e. reply with an
  109. * empty confirm.
  110. * If the change was mandatory, then we need to die.
  111. */
  112. if (rc && mandatory)
  113. goto out_invalid_option;
  114. break;
  115. case DCCPO_CONFIRM_L:
  116. /* fall through */
  117. case DCCPO_CONFIRM_R:
  118. if (len < 2)
  119. goto out_invalid_option;
  120. if (dccp_feat_confirm_recv(sk, opt, *value,
  121. value + 1, len - 1))
  122. goto out_invalid_option;
  123. break;
  124. case DCCPO_ACK_VECTOR_0:
  125. case DCCPO_ACK_VECTOR_1:
  126. if (pkt_type == DCCP_PKT_DATA)
  127. break;
  128. if (dccp_msk(sk)->dccpms_send_ack_vector &&
  129. dccp_ackvec_parse(sk, skb, &ackno, opt, value, len))
  130. goto out_invalid_option;
  131. break;
  132. case DCCPO_TIMESTAMP:
  133. if (len != 4)
  134. goto out_invalid_option;
  135. opt_val = get_unaligned((__be32 *)value);
  136. opt_recv->dccpor_timestamp = ntohl(opt_val);
  137. dp->dccps_timestamp_echo = opt_recv->dccpor_timestamp;
  138. dp->dccps_timestamp_time = ktime_get_real();
  139. dccp_pr_debug("%s rx opt: TIMESTAMP=%u, ackno=%llu\n",
  140. dccp_role(sk), opt_recv->dccpor_timestamp,
  141. (unsigned long long)
  142. DCCP_SKB_CB(skb)->dccpd_ack_seq);
  143. break;
  144. case DCCPO_TIMESTAMP_ECHO:
  145. if (len != 4 && len != 6 && len != 8)
  146. goto out_invalid_option;
  147. opt_val = get_unaligned((__be32 *)value);
  148. opt_recv->dccpor_timestamp_echo = ntohl(opt_val);
  149. dccp_pr_debug("%s rx opt: TIMESTAMP_ECHO=%u, len=%d, "
  150. "ackno=%llu", dccp_role(sk),
  151. opt_recv->dccpor_timestamp_echo,
  152. len + 2,
  153. (unsigned long long)
  154. DCCP_SKB_CB(skb)->dccpd_ack_seq);
  155. value += 4;
  156. if (len == 4) { /* no elapsed time included */
  157. dccp_pr_debug_cat("\n");
  158. break;
  159. }
  160. if (len == 6) { /* 2-byte elapsed time */
  161. __be16 opt_val2 = get_unaligned((__be16 *)value);
  162. elapsed_time = ntohs(opt_val2);
  163. } else { /* 4-byte elapsed time */
  164. opt_val = get_unaligned((__be32 *)value);
  165. elapsed_time = ntohl(opt_val);
  166. }
  167. dccp_pr_debug_cat(", ELAPSED_TIME=%u\n", elapsed_time);
  168. /* Give precedence to the biggest ELAPSED_TIME */
  169. if (elapsed_time > opt_recv->dccpor_elapsed_time)
  170. opt_recv->dccpor_elapsed_time = elapsed_time;
  171. break;
  172. case DCCPO_ELAPSED_TIME:
  173. if (len != 2 && len != 4)
  174. goto out_invalid_option;
  175. if (pkt_type == DCCP_PKT_DATA)
  176. continue;
  177. if (len == 2) {
  178. __be16 opt_val2 = get_unaligned((__be16 *)value);
  179. elapsed_time = ntohs(opt_val2);
  180. } else {
  181. opt_val = get_unaligned((__be32 *)value);
  182. elapsed_time = ntohl(opt_val);
  183. }
  184. if (elapsed_time > opt_recv->dccpor_elapsed_time)
  185. opt_recv->dccpor_elapsed_time = elapsed_time;
  186. dccp_pr_debug("%s rx opt: ELAPSED_TIME=%d\n",
  187. dccp_role(sk), elapsed_time);
  188. break;
  189. /*
  190. * From RFC 4340, sec. 10.3:
  191. *
  192. * Option numbers 128 through 191 are for
  193. * options sent from the HC-Sender to the
  194. * HC-Receiver; option numbers 192 through 255
  195. * are for options sent from the HC-Receiver to
  196. * the HC-Sender.
  197. */
  198. case 128 ... 191: {
  199. const u16 idx = value - options;
  200. if (ccid_hc_rx_parse_options(dp->dccps_hc_rx_ccid, sk,
  201. opt, len, idx,
  202. value) != 0)
  203. goto out_invalid_option;
  204. }
  205. break;
  206. case 192 ... 255: {
  207. const u16 idx = value - options;
  208. if (ccid_hc_tx_parse_options(dp->dccps_hc_tx_ccid, sk,
  209. opt, len, idx,
  210. value) != 0)
  211. goto out_invalid_option;
  212. }
  213. break;
  214. default:
  215. DCCP_CRIT("DCCP(%p): option %d(len=%d) not "
  216. "implemented, ignoring", sk, opt, len);
  217. break;
  218. }
  219. if (opt != DCCPO_MANDATORY)
  220. mandatory = 0;
  221. }
  222. /* mandatory was the last byte in option list -> reset connection */
  223. if (mandatory)
  224. goto out_invalid_option;
  225. return 0;
  226. out_invalid_option:
  227. DCCP_INC_STATS_BH(DCCP_MIB_INVALIDOPT);
  228. DCCP_SKB_CB(skb)->dccpd_reset_code = DCCP_RESET_CODE_OPTION_ERROR;
  229. DCCP_WARN("DCCP(%p): invalid option %d, len=%d", sk, opt, len);
  230. return -1;
  231. }
  232. EXPORT_SYMBOL_GPL(dccp_parse_options);
  233. static void dccp_encode_value_var(const u32 value, unsigned char *to,
  234. const unsigned int len)
  235. {
  236. if (len > 3)
  237. *to++ = (value & 0xFF000000) >> 24;
  238. if (len > 2)
  239. *to++ = (value & 0xFF0000) >> 16;
  240. if (len > 1)
  241. *to++ = (value & 0xFF00) >> 8;
  242. if (len > 0)
  243. *to++ = (value & 0xFF);
  244. }
  245. static inline int dccp_ndp_len(const int ndp)
  246. {
  247. return likely(ndp <= 0xFF) ? 1 : ndp <= 0xFFFF ? 2 : 3;
  248. }
  249. int dccp_insert_option(struct sock *sk, struct sk_buff *skb,
  250. const unsigned char option,
  251. const void *value, const unsigned char len)
  252. {
  253. unsigned char *to;
  254. if (DCCP_SKB_CB(skb)->dccpd_opt_len + len + 2 > DCCP_MAX_OPT_LEN)
  255. return -1;
  256. DCCP_SKB_CB(skb)->dccpd_opt_len += len + 2;
  257. to = skb_push(skb, len + 2);
  258. *to++ = option;
  259. *to++ = len + 2;
  260. memcpy(to, value, len);
  261. return 0;
  262. }
  263. EXPORT_SYMBOL_GPL(dccp_insert_option);
  264. static int dccp_insert_option_ndp(struct sock *sk, struct sk_buff *skb)
  265. {
  266. struct dccp_sock *dp = dccp_sk(sk);
  267. int ndp = dp->dccps_ndp_count;
  268. if (dccp_non_data_packet(skb))
  269. ++dp->dccps_ndp_count;
  270. else
  271. dp->dccps_ndp_count = 0;
  272. if (ndp > 0) {
  273. unsigned char *ptr;
  274. const int ndp_len = dccp_ndp_len(ndp);
  275. const int len = ndp_len + 2;
  276. if (DCCP_SKB_CB(skb)->dccpd_opt_len + len > DCCP_MAX_OPT_LEN)
  277. return -1;
  278. DCCP_SKB_CB(skb)->dccpd_opt_len += len;
  279. ptr = skb_push(skb, len);
  280. *ptr++ = DCCPO_NDP_COUNT;
  281. *ptr++ = len;
  282. dccp_encode_value_var(ndp, ptr, ndp_len);
  283. }
  284. return 0;
  285. }
  286. static inline int dccp_elapsed_time_len(const u32 elapsed_time)
  287. {
  288. return elapsed_time == 0 ? 0 : elapsed_time <= 0xFFFF ? 2 : 4;
  289. }
  290. int dccp_insert_option_elapsed_time(struct sock *sk, struct sk_buff *skb,
  291. u32 elapsed_time)
  292. {
  293. const int elapsed_time_len = dccp_elapsed_time_len(elapsed_time);
  294. const int len = 2 + elapsed_time_len;
  295. unsigned char *to;
  296. if (elapsed_time_len == 0)
  297. return 0;
  298. if (DCCP_SKB_CB(skb)->dccpd_opt_len + len > DCCP_MAX_OPT_LEN)
  299. return -1;
  300. DCCP_SKB_CB(skb)->dccpd_opt_len += len;
  301. to = skb_push(skb, len);
  302. *to++ = DCCPO_ELAPSED_TIME;
  303. *to++ = len;
  304. if (elapsed_time_len == 2) {
  305. const __be16 var16 = htons((u16)elapsed_time);
  306. memcpy(to, &var16, 2);
  307. } else {
  308. const __be32 var32 = htonl(elapsed_time);
  309. memcpy(to, &var32, 4);
  310. }
  311. return 0;
  312. }
  313. EXPORT_SYMBOL_GPL(dccp_insert_option_elapsed_time);
  314. int dccp_insert_option_timestamp(struct sock *sk, struct sk_buff *skb)
  315. {
  316. __be32 now = htonl(dccp_timestamp());
  317. /* yes this will overflow but that is the point as we want a
  318. * 10 usec 32 bit timer which mean it wraps every 11.9 hours */
  319. return dccp_insert_option(sk, skb, DCCPO_TIMESTAMP, &now, sizeof(now));
  320. }
  321. EXPORT_SYMBOL_GPL(dccp_insert_option_timestamp);
  322. static int dccp_insert_option_timestamp_echo(struct sock *sk,
  323. struct sk_buff *skb)
  324. {
  325. struct dccp_sock *dp = dccp_sk(sk);
  326. __be32 tstamp_echo;
  327. int len, elapsed_time_len;
  328. unsigned char *to;
  329. const suseconds_t delta = ktime_us_delta(ktime_get_real(),
  330. dp->dccps_timestamp_time);
  331. u32 elapsed_time = delta / 10;
  332. elapsed_time_len = dccp_elapsed_time_len(elapsed_time);
  333. len = 6 + elapsed_time_len;
  334. if (DCCP_SKB_CB(skb)->dccpd_opt_len + len > DCCP_MAX_OPT_LEN)
  335. return -1;
  336. DCCP_SKB_CB(skb)->dccpd_opt_len += len;
  337. to = skb_push(skb, len);
  338. *to++ = DCCPO_TIMESTAMP_ECHO;
  339. *to++ = len;
  340. tstamp_echo = htonl(dp->dccps_timestamp_echo);
  341. memcpy(to, &tstamp_echo, 4);
  342. to += 4;
  343. if (elapsed_time_len == 2) {
  344. const __be16 var16 = htons((u16)elapsed_time);
  345. memcpy(to, &var16, 2);
  346. } else if (elapsed_time_len == 4) {
  347. const __be32 var32 = htonl(elapsed_time);
  348. memcpy(to, &var32, 4);
  349. }
  350. dp->dccps_timestamp_echo = 0;
  351. dp->dccps_timestamp_time = ktime_set(0, 0);
  352. return 0;
  353. }
  354. static int dccp_insert_feat_opt(struct sk_buff *skb, u8 type, u8 feat,
  355. u8 *val, u8 len)
  356. {
  357. u8 *to;
  358. if (DCCP_SKB_CB(skb)->dccpd_opt_len + len + 3 > DCCP_MAX_OPT_LEN) {
  359. DCCP_WARN("packet too small for feature %d option!\n", feat);
  360. return -1;
  361. }
  362. DCCP_SKB_CB(skb)->dccpd_opt_len += len + 3;
  363. to = skb_push(skb, len + 3);
  364. *to++ = type;
  365. *to++ = len + 3;
  366. *to++ = feat;
  367. if (len)
  368. memcpy(to, val, len);
  369. dccp_pr_debug("%s(%s (%d), ...), length %d\n",
  370. dccp_feat_typename(type),
  371. dccp_feat_name(feat), feat, len);
  372. return 0;
  373. }
  374. static int dccp_insert_options_feat(struct sock *sk, struct sk_buff *skb)
  375. {
  376. struct dccp_sock *dp = dccp_sk(sk);
  377. struct dccp_minisock *dmsk = dccp_msk(sk);
  378. struct dccp_opt_pend *opt, *next;
  379. int change = 0;
  380. /* confirm any options [NN opts] */
  381. list_for_each_entry_safe(opt, next, &dmsk->dccpms_conf, dccpop_node) {
  382. dccp_insert_feat_opt(skb, opt->dccpop_type,
  383. opt->dccpop_feat, opt->dccpop_val,
  384. opt->dccpop_len);
  385. /* fear empty confirms */
  386. if (opt->dccpop_val)
  387. kfree(opt->dccpop_val);
  388. kfree(opt);
  389. }
  390. INIT_LIST_HEAD(&dmsk->dccpms_conf);
  391. /* see which features we need to send */
  392. list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
  393. /* see if we need to send any confirm */
  394. if (opt->dccpop_sc) {
  395. dccp_insert_feat_opt(skb, opt->dccpop_type + 1,
  396. opt->dccpop_feat,
  397. opt->dccpop_sc->dccpoc_val,
  398. opt->dccpop_sc->dccpoc_len);
  399. BUG_ON(!opt->dccpop_sc->dccpoc_val);
  400. kfree(opt->dccpop_sc->dccpoc_val);
  401. kfree(opt->dccpop_sc);
  402. opt->dccpop_sc = NULL;
  403. }
  404. /* any option not confirmed, re-send it */
  405. if (!opt->dccpop_conf) {
  406. dccp_insert_feat_opt(skb, opt->dccpop_type,
  407. opt->dccpop_feat, opt->dccpop_val,
  408. opt->dccpop_len);
  409. change++;
  410. }
  411. }
  412. /* Retransmit timer.
  413. * If this is the master listening sock, we don't set a timer on it. It
  414. * should be fine because if the dude doesn't receive our RESPONSE
  415. * [which will contain the CHANGE] he will send another REQUEST which
  416. * will "retrnasmit" the change.
  417. */
  418. if (change && dp->dccps_role != DCCP_ROLE_LISTEN) {
  419. dccp_pr_debug("reset feat negotiation timer %p\n", sk);
  420. /* XXX don't reset the timer on re-transmissions. I.e. reset it
  421. * only when sending new stuff i guess. Currently the timer
  422. * never backs off because on re-transmission it just resets it!
  423. */
  424. inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
  425. inet_csk(sk)->icsk_rto, DCCP_RTO_MAX);
  426. }
  427. return 0;
  428. }
  429. int dccp_insert_options(struct sock *sk, struct sk_buff *skb)
  430. {
  431. struct dccp_sock *dp = dccp_sk(sk);
  432. struct dccp_minisock *dmsk = dccp_msk(sk);
  433. DCCP_SKB_CB(skb)->dccpd_opt_len = 0;
  434. if (dmsk->dccpms_send_ndp_count &&
  435. dccp_insert_option_ndp(sk, skb))
  436. return -1;
  437. if (!dccp_packet_without_ack(skb)) {
  438. if (dmsk->dccpms_send_ack_vector &&
  439. dccp_ackvec_pending(dp->dccps_hc_rx_ackvec) &&
  440. dccp_insert_option_ackvec(sk, skb))
  441. return -1;
  442. if (dp->dccps_timestamp_echo != 0 &&
  443. dccp_insert_option_timestamp_echo(sk, skb))
  444. return -1;
  445. }
  446. if (dp->dccps_hc_rx_insert_options) {
  447. if (ccid_hc_rx_insert_options(dp->dccps_hc_rx_ccid, sk, skb))
  448. return -1;
  449. dp->dccps_hc_rx_insert_options = 0;
  450. }
  451. /* Feature negotiation */
  452. /* Data packets can't do feat negotiation */
  453. if (DCCP_SKB_CB(skb)->dccpd_type != DCCP_PKT_DATA &&
  454. DCCP_SKB_CB(skb)->dccpd_type != DCCP_PKT_DATAACK &&
  455. dccp_insert_options_feat(sk, skb))
  456. return -1;
  457. /*
  458. * Obtain RTT sample from Request/Response exchange.
  459. * This is currently used in CCID 3 initialisation.
  460. */
  461. if (DCCP_SKB_CB(skb)->dccpd_type == DCCP_PKT_REQUEST &&
  462. dccp_insert_option_timestamp(sk, skb))
  463. return -1;
  464. /* XXX: insert other options when appropriate */
  465. if (DCCP_SKB_CB(skb)->dccpd_opt_len != 0) {
  466. /* The length of all options has to be a multiple of 4 */
  467. int padding = DCCP_SKB_CB(skb)->dccpd_opt_len % 4;
  468. if (padding != 0) {
  469. padding = 4 - padding;
  470. memset(skb_push(skb, padding), 0, padding);
  471. DCCP_SKB_CB(skb)->dccpd_opt_len += padding;
  472. }
  473. }
  474. return 0;
  475. }