FragmentProtoAssembler.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /**
  2. * @file FragmentProtoAssembler.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. */
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <misc/offset.h>
  25. #include <misc/debug.h>
  26. #include <misc/byteorder.h>
  27. #include <system/BLog.h>
  28. #include <flow/FragmentProtoAssembler.h>
  29. #include <generated/blog_channel_FragmentProtoAssembler.h>
  30. #define FPA_MAX_TIME UINT32_MAX
  31. static int frame_id_comparator (void *unused, fragmentproto_frameid *v1, fragmentproto_frameid *v2)
  32. {
  33. if (*v1 < *v2) {
  34. return -1;
  35. }
  36. if (*v1 > *v2) {
  37. return 1;
  38. }
  39. return 0;
  40. }
  41. static void free_frame (FragmentProtoAssembler *o, struct FragmentProtoAssembler_frame *frame)
  42. {
  43. // remove from used list
  44. LinkedList2_Remove(&o->frames_used, &frame->list_node);
  45. // remove from used tree
  46. BAVL_Remove(&o->frames_used_tree, &frame->tree_node);
  47. // append to free list
  48. LinkedList2_Append(&o->frames_free, &frame->list_node);
  49. }
  50. static void free_oldest_frame (FragmentProtoAssembler *o)
  51. {
  52. ASSERT(!LinkedList2_IsEmpty(&o->frames_used))
  53. // obtain oldest frame (first on the list)
  54. LinkedList2Node *list_node = LinkedList2_GetFirst(&o->frames_used);
  55. ASSERT(list_node)
  56. struct FragmentProtoAssembler_frame *frame = UPPER_OBJECT(list_node, struct FragmentProtoAssembler_frame, list_node);
  57. // free frame
  58. free_frame(o, frame);
  59. }
  60. static struct FragmentProtoAssembler_frame * allocate_new_frame (FragmentProtoAssembler *o, fragmentproto_frameid id)
  61. {
  62. ASSERT(!BAVL_LookupExact(&o->frames_used_tree, &id))
  63. // if there are no free entries, free the oldest used one
  64. if (LinkedList2_IsEmpty(&o->frames_free)) {
  65. BLog(BLOG_INFO, "freeing used frame");
  66. free_oldest_frame(o);
  67. }
  68. // obtain frame entry
  69. LinkedList2Node *list_node = LinkedList2_GetFirst(&o->frames_free);
  70. ASSERT(list_node)
  71. struct FragmentProtoAssembler_frame *frame = UPPER_OBJECT(list_node, struct FragmentProtoAssembler_frame, list_node);
  72. // remove from free list
  73. LinkedList2_Remove(&o->frames_free, &frame->list_node);
  74. // initialize values
  75. frame->id = id;
  76. frame->time = o->time;
  77. frame->num_chunks = 0;
  78. frame->sum = 0;
  79. frame->length = -1;
  80. frame->length_so_far = 0;
  81. // append to used list
  82. LinkedList2_Append(&o->frames_used, &frame->list_node);
  83. // insert to used tree
  84. ASSERT_EXECUTE(BAVL_Insert(&o->frames_used_tree, &frame->tree_node, NULL))
  85. return frame;
  86. }
  87. static int chunks_overlap (int c1_start, int c1_len, int c2_start, int c2_len)
  88. {
  89. return (c1_start + c1_len > c2_start && c2_start + c2_len > c1_start);
  90. }
  91. static int frame_is_timed_out (FragmentProtoAssembler *o, struct FragmentProtoAssembler_frame *frame)
  92. {
  93. ASSERT(frame->time <= o->time)
  94. return (o->time - frame->time > (uint32_t)o->time_tolerance);
  95. }
  96. static void reduce_times (FragmentProtoAssembler *o)
  97. {
  98. // find the frame with minimal time, removing timed out frames
  99. struct FragmentProtoAssembler_frame *minframe = NULL;
  100. LinkedList2Iterator it;
  101. LinkedList2Iterator_InitForward(&it, &o->frames_used);
  102. LinkedList2Node *list_node;
  103. while (list_node = LinkedList2Iterator_Next(&it)) {
  104. struct FragmentProtoAssembler_frame *frame = UPPER_OBJECT(list_node, struct FragmentProtoAssembler_frame, list_node);
  105. if (frame_is_timed_out(o, frame)) {
  106. BLog(BLOG_INFO, "freeing timed out frame (while reducing times)");
  107. free_frame(o, frame);
  108. } else {
  109. if (!minframe || frame->time < minframe->time) {
  110. minframe = frame;
  111. }
  112. }
  113. }
  114. if (!minframe) {
  115. // have no frames, set packet time to zero
  116. o->time = 0;
  117. return;
  118. }
  119. uint32_t min_time = minframe->time;
  120. // subtract minimal time from all frames
  121. LinkedList2Iterator_InitForward(&it, &o->frames_used);
  122. while (list_node = LinkedList2Iterator_Next(&it)) {
  123. struct FragmentProtoAssembler_frame *frame = UPPER_OBJECT(list_node, struct FragmentProtoAssembler_frame, list_node);
  124. frame->time -= min_time;
  125. }
  126. // subtract minimal time from packet time
  127. o->time -= min_time;
  128. }
  129. static void process_chunk (FragmentProtoAssembler *o, fragmentproto_frameid frame_id, int chunk_start, int chunk_len, int is_last, uint8_t *payload)
  130. {
  131. ASSERT(!o->output_ready)
  132. ASSERT(chunk_start >= 0)
  133. ASSERT(chunk_len >= 0)
  134. ASSERT(is_last == 0 || is_last == 1)
  135. // verify chunk
  136. // check start
  137. if (chunk_start > o->output_mtu) {
  138. BLog(BLOG_INFO, "chunk starts outside");
  139. return;
  140. }
  141. // check frame size bound
  142. if (chunk_len > o->output_mtu - chunk_start) {
  143. BLog(BLOG_INFO, "chunk ends outside");
  144. return;
  145. }
  146. // calculate end
  147. int chunk_end = chunk_start + chunk_len;
  148. // lookup frame
  149. struct FragmentProtoAssembler_frame *frame;
  150. BAVLNode *tree_node;
  151. if (!(tree_node = BAVL_LookupExact(&o->frames_used_tree, &frame_id))) {
  152. // frame not found, add a new one
  153. frame = allocate_new_frame(o, frame_id);
  154. } else {
  155. // have existing frame with that ID
  156. frame = UPPER_OBJECT(tree_node, struct FragmentProtoAssembler_frame, tree_node);
  157. // check frame time
  158. if (frame_is_timed_out(o, frame)) {
  159. // frame is timed out, remove it and use a new one
  160. BLog(BLOG_INFO, "freeing timed out frame (while processing chunk)");
  161. free_frame(o, frame);
  162. frame = allocate_new_frame(o, frame_id);
  163. }
  164. }
  165. ASSERT(frame->num_chunks < o->num_chunks)
  166. // check if the chunk overlaps with any existing chunks
  167. for (int i = 0; i < frame->num_chunks; i++) {
  168. struct FragmentProtoAssembler_chunk *chunk = &frame->chunks[i];
  169. if (chunks_overlap(chunk->start, chunk->len, chunk_start, chunk_len)) {
  170. BLog(BLOG_INFO, "chunk overlaps with existing chunk");
  171. goto fail_frame;
  172. }
  173. }
  174. if (is_last) {
  175. // this chunk is marked as last
  176. if (frame->length >= 0) {
  177. BLog(BLOG_INFO, "got last chunk, but already have one");
  178. goto fail_frame;
  179. }
  180. // check if frame size according to this packet is consistent
  181. // with existing chunks
  182. if (frame->length_so_far > chunk_end) {
  183. BLog(BLOG_INFO, "got last chunk, but already have data over its bound");
  184. goto fail_frame;
  185. }
  186. } else {
  187. // if we have length, chunk must be in its bound
  188. if (frame->length >= 0) {
  189. if (chunk_end > frame->length) {
  190. BLog(BLOG_INFO, "chunk out of length bound");
  191. goto fail_frame;
  192. }
  193. }
  194. }
  195. // chunk is good, add it
  196. // update frame time
  197. frame->time = o->time;
  198. // add chunk entry
  199. struct FragmentProtoAssembler_chunk *chunk = &frame->chunks[frame->num_chunks];
  200. chunk->start = chunk_start;
  201. chunk->len = chunk_len;
  202. frame->num_chunks++;
  203. // update sum
  204. frame->sum += chunk_len;
  205. // update length
  206. if (is_last) {
  207. frame->length = chunk_end;
  208. } else {
  209. if (frame->length < 0) {
  210. if (frame->length_so_far < chunk_end) {
  211. frame->length_so_far = chunk_end;
  212. }
  213. }
  214. }
  215. // copy chunk payload to buffer
  216. memcpy(frame->buffer + chunk_start, payload, chunk_len);
  217. // is frame incomplete?
  218. if (frame->length < 0 || frame->sum < frame->length) {
  219. // if all chunks are used, fail it
  220. if (frame->num_chunks == o->num_chunks) {
  221. BLog(BLOG_INFO, "all chunks used, but frame not complete");
  222. goto fail_frame;
  223. }
  224. return;
  225. }
  226. ASSERT(frame->sum == frame->length)
  227. BLog(BLOG_DEBUG, "frame complete");
  228. // free frame entry
  229. free_frame(o, frame);
  230. // remember frame
  231. o->output_ready = 1;
  232. o->output_packet_data = frame->buffer;
  233. o->output_packet_len = frame->length;
  234. return;
  235. fail_frame:
  236. free_frame(o, frame);
  237. }
  238. static void process_input (FragmentProtoAssembler *o)
  239. {
  240. ASSERT(o->in_len >= 0)
  241. ASSERT(!o->output_ready)
  242. // read chunks
  243. while (o->in_pos < o->in_len) {
  244. // obtain header
  245. if (o->in_len - o->in_pos < sizeof(struct fragmentproto_chunk_header)) {
  246. BLog(BLOG_INFO, "too little data for chunk header");
  247. break;
  248. }
  249. struct fragmentproto_chunk_header *header = (struct fragmentproto_chunk_header *)(o->in + o->in_pos);
  250. o->in_pos += sizeof(struct fragmentproto_chunk_header);
  251. fragmentproto_frameid frame_id = ltoh16(header->frame_id);
  252. int chunk_start = ltoh16(header->chunk_start);
  253. int chunk_len = ltoh16(header->chunk_len);
  254. // check is_last field
  255. if (!(header->is_last == 0 || header->is_last == 1)) {
  256. BLog(BLOG_INFO, "chunk is_last wrong");
  257. break;
  258. }
  259. // obtain data
  260. if (o->in_len - o->in_pos < chunk_len) {
  261. BLog(BLOG_INFO, "too little data for chunk data");
  262. break;
  263. }
  264. // process chunk
  265. process_chunk(o, frame_id, chunk_start, chunk_len, header->is_last, o->in + o->in_pos);
  266. o->in_pos += chunk_len;
  267. // if output is blocking, stop processing input
  268. if (o->output_ready) {
  269. return;
  270. }
  271. }
  272. // all input processed
  273. o->in_len = -1;
  274. // increment packet time
  275. if (o->time == FPA_MAX_TIME) {
  276. reduce_times(o);
  277. if (!LinkedList2_IsEmpty(&o->frames_used)) {
  278. ASSERT(o->time < FPA_MAX_TIME) // If there was a frame with zero time, it was removed because
  279. // time_tolerance < FPA_MAX_TIME. So something >0 was subtracted.
  280. o->time++;
  281. } else {
  282. // it was set to zero by reduce_times
  283. ASSERT(o->time == 0)
  284. }
  285. } else {
  286. o->time++;
  287. }
  288. }
  289. static void do_io (FragmentProtoAssembler *o)
  290. {
  291. ASSERT(o->in_len >= 0)
  292. ASSERT(!o->output_ready)
  293. // process input
  294. process_input(o);
  295. ASSERT((o->in_len >= 0) == o->output_ready)
  296. if (o->output_ready) {
  297. PacketPassInterface_Sender_Send(o->output, o->output_packet_data, o->output_packet_len);
  298. } else {
  299. PacketPassInterface_Done(&o->input);
  300. }
  301. }
  302. static void input_handler_send (FragmentProtoAssembler *o, uint8_t *data, int data_len)
  303. {
  304. ASSERT(o->in_len == -1)
  305. ASSERT(!o->output_ready)
  306. ASSERT(data_len >= 0)
  307. ASSERT(data_len <= o->input_mtu)
  308. DebugObject_Access(&o->d_obj);
  309. // save input packet
  310. o->in_len = data_len;
  311. o->in = data;
  312. o->in_pos = 0;
  313. do_io(o);
  314. }
  315. static void output_handler_done (FragmentProtoAssembler *o)
  316. {
  317. ASSERT(o->in_len >= 0)
  318. ASSERT(o->output_ready)
  319. DebugObject_Access(&o->d_obj);
  320. // output no longer blocking
  321. o->output_ready = 0;
  322. do_io(o);
  323. }
  324. int FragmentProtoAssembler_Init (FragmentProtoAssembler *o, int input_mtu, PacketPassInterface *output, int num_frames, int num_chunks, BPendingGroup *pg)
  325. {
  326. ASSERT(input_mtu >= 0)
  327. ASSERT(num_frames > 0)
  328. ASSERT(num_frames < FPA_MAX_TIME) // needed so we can always subtract times when packet time is maximum
  329. ASSERT(num_chunks > 0)
  330. // init arguments
  331. o->input_mtu = input_mtu;
  332. o->output = output;
  333. o->num_frames = num_frames;
  334. o->num_chunks = num_chunks;
  335. // init input
  336. PacketPassInterface_Init(&o->input, o->input_mtu, (PacketPassInterface_handler_send)input_handler_send, o, pg);
  337. // init output
  338. PacketPassInterface_Sender_Init(o->output, (PacketPassInterface_handler_done)output_handler_done, o);
  339. // remebmer output MTU
  340. o->output_mtu = PacketPassInterface_GetMTU(o->output);
  341. // set packet time to zero
  342. o->time = 0;
  343. // set time tolerance to num_frames
  344. o->time_tolerance = o->num_frames;
  345. // allocate frames
  346. if (!(o->frames_entries = malloc(o->num_frames * sizeof(struct FragmentProtoAssembler_frame)))) {
  347. goto fail1;
  348. }
  349. // allocate chunks
  350. if (!(o->frames_chunks = malloc(o->num_frames * o->num_chunks * sizeof(struct FragmentProtoAssembler_chunk)))) {
  351. goto fail2;
  352. }
  353. // allocate buffers
  354. if (!(o->frames_buffer = malloc(o->num_frames * o->output_mtu))) {
  355. goto fail3;
  356. }
  357. // init frame lists
  358. LinkedList2_Init(&o->frames_free);
  359. LinkedList2_Init(&o->frames_used);
  360. // initialize frame entries
  361. for (int i = 0; i < num_frames; i++) {
  362. struct FragmentProtoAssembler_frame *frame = &o->frames_entries[i];
  363. // set chunks array pointer
  364. frame->chunks = o->frames_chunks + i * o->num_chunks;
  365. // set buffer pointer
  366. frame->buffer = o->frames_buffer + i * o->output_mtu;
  367. // add to free list
  368. LinkedList2_Append(&o->frames_free, &frame->list_node);
  369. }
  370. // init tree
  371. BAVL_Init(&o->frames_used_tree, OFFSET_DIFF(struct FragmentProtoAssembler_frame, id, tree_node), (BAVL_comparator)frame_id_comparator, NULL);
  372. // have no input packet
  373. o->in_len = -1;
  374. // output not blocking
  375. o->output_ready = 0;
  376. DebugObject_Init(&o->d_obj);
  377. return 1;
  378. fail3:
  379. free(o->frames_chunks);
  380. fail2:
  381. free(o->frames_entries);
  382. fail1:
  383. PacketPassInterface_Free(&o->input);
  384. return 0;
  385. }
  386. void FragmentProtoAssembler_Free (FragmentProtoAssembler *o)
  387. {
  388. DebugObject_Free(&o->d_obj);
  389. // free buffers
  390. free(o->frames_buffer);
  391. // free chunks
  392. free(o->frames_chunks);
  393. // free frames
  394. free(o->frames_entries);
  395. // free input
  396. PacketPassInterface_Free(&o->input);
  397. }
  398. PacketPassInterface * FragmentProtoAssembler_GetInput (FragmentProtoAssembler *o)
  399. {
  400. DebugObject_Access(&o->d_obj);
  401. return &o->input;
  402. }