FragmentProtoAssembler.c 15 KB

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