options.c 15 KB

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