options.c 14 KB

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