noisydgrambio.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /*
  2. * Copyright 2023 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <openssl/bio.h>
  10. #include "quictestlib.h"
  11. #include "../testutil.h"
  12. #define MSG_DATA_LEN_MAX 1472
  13. struct noisy_dgram_st {
  14. uint64_t this_dgram;
  15. BIO_MSG msg;
  16. uint64_t reinject_dgram;
  17. int backoff;
  18. };
  19. static long noisy_dgram_ctrl(BIO *bio, int cmd, long num, void *ptr)
  20. {
  21. long ret;
  22. BIO *next = BIO_next(bio);
  23. if (next == NULL)
  24. return 0;
  25. switch (cmd) {
  26. case BIO_CTRL_DUP:
  27. ret = 0L;
  28. break;
  29. case BIO_CTRL_NOISE_BACK_OFF: {
  30. struct noisy_dgram_st *data;
  31. data = BIO_get_data(bio);
  32. if (!TEST_ptr(data))
  33. return 0;
  34. data->backoff = 1;
  35. ret = 1;
  36. break;
  37. }
  38. default:
  39. ret = BIO_ctrl(next, cmd, num, ptr);
  40. break;
  41. }
  42. return ret;
  43. }
  44. static int noisy_dgram_sendmmsg(BIO *bio, BIO_MSG *msg, size_t stride,
  45. size_t num_msg, uint64_t flags,
  46. size_t *msgs_processed)
  47. {
  48. BIO *next = BIO_next(bio);
  49. if (next == NULL)
  50. return 0;
  51. /*
  52. * We only introduce noise when receiving messages. We just pass this on
  53. * to the underlying BIO.
  54. */
  55. return BIO_sendmmsg(next, msg, stride, num_msg, flags, msgs_processed);
  56. }
  57. /* 1 in NOISE_RATE datagrams will be noisy. With a value of 5 that is 20% */
  58. #define NOISE_RATE 5
  59. /*
  60. * We have 3 different types of noise: drop, duplicate and delay
  61. * Each of these have equal probability.
  62. */
  63. #define NOISE_TYPE_DROP 0
  64. #define NOISE_TYPE_DUPLICATE 1
  65. #define NOISE_TYPE_DELAY 2
  66. #define NOISE_TYPE_BITFLIPS 3
  67. #define NUM_NOISE_TYPES 4
  68. /*
  69. * When a duplicate occurs we reinject the new datagram after up to
  70. * MAX_DGRAM_REINJECT datagrams have been sent. A reinject of 1 means that the
  71. * duplicate follows immediately after the original datagram. A reinject of 4
  72. * means that original datagram plus 3 other datagrams are sent before the
  73. * reinjected datagram is inserted.
  74. * This also controls when a delay (not a duplicate) occurs. In that case
  75. * we add 1 to the number because there is no point in skipping the current
  76. * datagram only to immediately reinject it in the next datagram.
  77. */
  78. #define MAX_DGRAM_REINJECT 4
  79. static void get_noise(int long_header, uint64_t *reinject, int *should_drop,
  80. uint16_t *flip, size_t *flip_offset)
  81. {
  82. uint32_t type;
  83. *flip = 0;
  84. if (test_random() % NOISE_RATE != 0) {
  85. *reinject = 0;
  86. *should_drop = 0;
  87. return;
  88. }
  89. type = test_random() % NUM_NOISE_TYPES;
  90. /*
  91. * Of noisy datagrams, 25% drop, 25% duplicate, 25% delay, 25% flip bits
  92. * A duplicated datagram keeps the current datagram and reinjects a new
  93. * identical one after up to MAX_DGRAM_DELAY datagrams have been sent.
  94. * A delayed datagram is implemented as both a reinject and a drop, i.e. an
  95. * identical datagram is reinjected after the given number of datagrams have
  96. * been sent and the current datagram is dropped.
  97. */
  98. *should_drop = (type == NOISE_TYPE_DROP || type == NOISE_TYPE_DELAY);
  99. /*
  100. * Where a duplicate occurs we reinject the copy of the datagram up to
  101. * MAX_DGRAM_DELAY datagrams later
  102. */
  103. *reinject = (type == NOISE_TYPE_DUPLICATE || type == NOISE_TYPE_DELAY)
  104. ? (uint64_t)((test_random() % MAX_DGRAM_REINJECT) + 1)
  105. : 0;
  106. /*
  107. * No point in reinjecting after 1 datagram if the current datagram is also
  108. * dropped (i.e. this is a delay not a duplicate), so we reinject after an
  109. * extra datagram in that case
  110. */
  111. *reinject += type == NOISE_TYPE_DELAY;
  112. /* flip some bits in the header */
  113. if (type == NOISE_TYPE_BITFLIPS) {
  114. /* we flip at most 8 bits of the 16 bit value at once */
  115. *flip = (test_random() % 255 + 1) << (test_random() % 8);
  116. /*
  117. * 25/50 bytes of guesstimated header size (it depends on CID length)
  118. * It does not matter much if it is overestimated.
  119. */
  120. *flip_offset = test_random() % (25 * (1 + long_header));
  121. }
  122. }
  123. static void flip_bits(unsigned char *msg, size_t msg_len, uint16_t flip,
  124. size_t flip_offset)
  125. {
  126. if (flip == 0)
  127. return;
  128. /* None of these border conditions should happen but check them anyway */
  129. if (msg_len < 2)
  130. return;
  131. if (msg_len < flip_offset + 2)
  132. flip_offset = msg_len - 2;
  133. #ifdef OSSL_NOISY_DGRAM_DEBUG
  134. printf("**Flipping bits in a datagram at offset %u\n",
  135. (unsigned int)flip_offset);
  136. BIO_dump_fp(stdout, msg, msg_len);
  137. printf("\n");
  138. #endif
  139. msg[flip_offset] ^= flip >> 8;
  140. msg[flip_offset + 1] ^= flip & 0xff;
  141. }
  142. static int noisy_dgram_recvmmsg(BIO *bio, BIO_MSG *msg, size_t stride,
  143. size_t num_msg, uint64_t flags,
  144. size_t *msgs_processed)
  145. {
  146. BIO *next = BIO_next(bio);
  147. size_t i, j, data_len = 0, msg_cnt = 0;
  148. BIO_MSG *thismsg;
  149. struct noisy_dgram_st *data;
  150. if (!TEST_ptr(next))
  151. return 0;
  152. data = BIO_get_data(bio);
  153. if (!TEST_ptr(data))
  154. return 0;
  155. /*
  156. * For simplicity we assume that all elements in the msg array have the
  157. * same data_len. They are not required to by the API, but it would be quite
  158. * strange for that not to be the case - and our code that calls
  159. * BIO_recvmmsg does do this (which is all that is important for this test
  160. * code). We test the invariant here.
  161. */
  162. for (i = 0; i < num_msg; i++) {
  163. if (i == 0) {
  164. data_len = msg[i].data_len;
  165. if (!TEST_size_t_le(data_len, MSG_DATA_LEN_MAX))
  166. return 0;
  167. } else if (!TEST_size_t_eq(msg[i].data_len, data_len)) {
  168. return 0;
  169. }
  170. }
  171. if (!BIO_recvmmsg(next, msg, stride, num_msg, flags, msgs_processed))
  172. return 0;
  173. #ifdef OSSL_NOISY_DGRAM_DEBUG
  174. printf("Pre-filter datagram list:\n");
  175. for (i = 0; i < *msgs_processed; i++) {
  176. printf("Pre-filter Datagram:\n");
  177. BIO_dump_fp(stdout, msg[i].data, msg[i].data_len);
  178. printf("\n");
  179. }
  180. printf("End of pre-filter datagram list\nApplying noise filters:\n");
  181. #endif
  182. msg_cnt = *msgs_processed;
  183. /* Introduce noise */
  184. for (i = 0, thismsg = msg;
  185. i < msg_cnt;
  186. i++, thismsg++, data->this_dgram++) {
  187. uint64_t reinject;
  188. int should_drop;
  189. uint16_t flip;
  190. size_t flip_offset;
  191. /* If we have a message to reinject then insert it now */
  192. if (data->reinject_dgram > 0
  193. && data->reinject_dgram == data->this_dgram) {
  194. if (msg_cnt < num_msg) {
  195. /* Make space for the injected message */
  196. for (j = msg_cnt; j > i; j--) {
  197. if (!bio_msg_copy(&msg[j], &msg[j - 1]))
  198. return 0;
  199. }
  200. if (!bio_msg_copy(thismsg, &data->msg))
  201. return 0;
  202. msg_cnt++;
  203. data->reinject_dgram = 0;
  204. #ifdef OSSL_NOISY_DGRAM_DEBUG
  205. printf("**Injecting a datagram\n");
  206. BIO_dump_fp(stdout, thismsg->data, thismsg->data_len);
  207. printf("\n");
  208. #endif
  209. continue;
  210. } /* else we have no space for the injection, so just drop it */
  211. data->reinject_dgram = 0;
  212. }
  213. get_noise(/* long header */ (((uint8_t *)thismsg->data)[0] & 0x80) != 0,
  214. &reinject, &should_drop, &flip, &flip_offset);
  215. if (data->backoff) {
  216. /*
  217. * We might be asked to back off on introducing too much noise if
  218. * there is a danger that the connection will fail. In that case
  219. * we always ensure that the next datagram does not get dropped so
  220. * that the connection always survives. After that we can resume
  221. * with normal noise
  222. */
  223. #ifdef OSSL_NOISY_DGRAM_DEBUG
  224. printf("**Back off applied\n");
  225. #endif
  226. should_drop = 0;
  227. flip = 0;
  228. data->backoff = 0;
  229. }
  230. flip_bits(thismsg->data, thismsg->data_len, flip, flip_offset);
  231. /*
  232. * We ignore reinjection if a message is already waiting to be
  233. * reinjected
  234. */
  235. if (reinject > 0 && data->reinject_dgram == 0) {
  236. /*
  237. * Both duplicated and delayed datagrams get reintroduced after the
  238. * delay period. Datagrams that are delayed only (not duplicated)
  239. * will also have the current copy of the datagram dropped (i.e
  240. * should_drop below will be true).
  241. */
  242. if (!bio_msg_copy(&data->msg, thismsg))
  243. return 0;
  244. data->reinject_dgram = data->this_dgram + reinject;
  245. #ifdef OSSL_NOISY_DGRAM_DEBUG
  246. printf("**Scheduling a reinject after %u messages%s\n",
  247. (unsigned int)reinject, should_drop ? "" : "(duplicating)");
  248. BIO_dump_fp(stdout, thismsg->data, thismsg->data_len);
  249. printf("\n");
  250. #endif
  251. }
  252. if (should_drop) {
  253. #ifdef OSSL_NOISY_DGRAM_DEBUG
  254. printf("**Dropping a datagram\n");
  255. BIO_dump_fp(stdout, thismsg->data, thismsg->data_len);
  256. printf("\n");
  257. #endif
  258. for (j = i + 1; j < msg_cnt; j++) {
  259. if (!bio_msg_copy(&msg[j - 1], &msg[j]))
  260. return 0;
  261. }
  262. msg_cnt--;
  263. }
  264. }
  265. #ifdef OSSL_NOISY_DGRAM_DEBUG
  266. printf("End of noise filters\nPost-filter datagram list:\n");
  267. for (i = 0; i < msg_cnt; i++) {
  268. printf("Post-filter Datagram:\n");
  269. BIO_dump_fp(stdout, msg[i].data, msg[i].data_len);
  270. printf("\n");
  271. }
  272. printf("End of post-filter datagram list\n");
  273. #endif
  274. *msgs_processed = msg_cnt;
  275. if (msg_cnt == 0) {
  276. ERR_raise(ERR_LIB_BIO, BIO_R_NON_FATAL);
  277. return 0;
  278. }
  279. return 1;
  280. }
  281. static void data_free(struct noisy_dgram_st *data)
  282. {
  283. if (data == NULL)
  284. return;
  285. OPENSSL_free(data->msg.data);
  286. BIO_ADDR_free(data->msg.peer);
  287. BIO_ADDR_free(data->msg.local);
  288. OPENSSL_free(data);
  289. }
  290. static int noisy_dgram_new(BIO *bio)
  291. {
  292. struct noisy_dgram_st *data = OPENSSL_zalloc(sizeof(*data));
  293. if (!TEST_ptr(data))
  294. return 0;
  295. data->msg.data = OPENSSL_malloc(MSG_DATA_LEN_MAX);
  296. data->msg.peer = BIO_ADDR_new();
  297. data->msg.local = BIO_ADDR_new();
  298. if (data->msg.data == NULL
  299. || data->msg.peer == NULL
  300. || data->msg.local == NULL) {
  301. data_free(data);
  302. return 0;
  303. }
  304. BIO_set_data(bio, data);
  305. BIO_set_init(bio, 1);
  306. return 1;
  307. }
  308. static int noisy_dgram_free(BIO *bio)
  309. {
  310. data_free(BIO_get_data(bio));
  311. BIO_set_data(bio, NULL);
  312. BIO_set_init(bio, 0);
  313. return 1;
  314. }
  315. /* Choose a sufficiently large type likely to be unused for this custom BIO */
  316. #define BIO_TYPE_NOISY_DGRAM_FILTER (0x80 | BIO_TYPE_FILTER)
  317. static BIO_METHOD *method_noisy_dgram = NULL;
  318. /* Note: Not thread safe! */
  319. const BIO_METHOD *bio_f_noisy_dgram_filter(void)
  320. {
  321. if (method_noisy_dgram == NULL) {
  322. method_noisy_dgram = BIO_meth_new(BIO_TYPE_NOISY_DGRAM_FILTER,
  323. "Nosiy datagram filter");
  324. if (method_noisy_dgram == NULL
  325. || !BIO_meth_set_ctrl(method_noisy_dgram, noisy_dgram_ctrl)
  326. || !BIO_meth_set_sendmmsg(method_noisy_dgram, noisy_dgram_sendmmsg)
  327. || !BIO_meth_set_recvmmsg(method_noisy_dgram, noisy_dgram_recvmmsg)
  328. || !BIO_meth_set_create(method_noisy_dgram, noisy_dgram_new)
  329. || !BIO_meth_set_destroy(method_noisy_dgram, noisy_dgram_free))
  330. return NULL;
  331. }
  332. return method_noisy_dgram;
  333. }
  334. void bio_f_noisy_dgram_filter_free(void)
  335. {
  336. BIO_meth_free(method_noisy_dgram);
  337. }