FragmentProtoAssembler.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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 int 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_blocking)
  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 0;
  140. }
  141. // check frame size bound
  142. if (chunk_len > o->output_mtu - chunk_start) {
  143. BLog(BLOG_INFO, "chunk ends outside");
  144. return 0;
  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 0;
  225. }
  226. ASSERT(frame->sum == frame->length)
  227. BLog(BLOG_DEBUG, "frame complete");
  228. // free frame entry
  229. free_frame(o, frame);
  230. // submit frame to output
  231. // this is fine even though the frame entry was freed
  232. DEAD_ENTER(o->dead)
  233. int res = PacketPassInterface_Sender_Send(o->output, frame->buffer, frame->length);
  234. if (DEAD_LEAVE(o->dead)) {
  235. return -1;
  236. }
  237. ASSERT(res == 0 || res == 1)
  238. // if output blocked, don't accept any new input until it's done
  239. if (!res) {
  240. o->output_blocking = 1;
  241. return 0;
  242. }
  243. return 0;
  244. fail_frame:
  245. free_frame(o, frame);
  246. return 0;
  247. }
  248. static int process_input (FragmentProtoAssembler *o)
  249. {
  250. ASSERT(o->in_len >= 0)
  251. ASSERT(!o->output_blocking)
  252. // read chunks
  253. while (o->in_pos < o->in_len) {
  254. // obtain header
  255. if (o->in_len - o->in_pos < sizeof(struct fragmentproto_chunk_header)) {
  256. BLog(BLOG_INFO, "too little data for chunk header");
  257. break;
  258. }
  259. struct fragmentproto_chunk_header *header = (struct fragmentproto_chunk_header *)(o->in + o->in_pos);
  260. o->in_pos += sizeof(struct fragmentproto_chunk_header);
  261. fragmentproto_frameid frame_id = ltoh16(header->frame_id);
  262. int chunk_start = ltoh16(header->chunk_start);
  263. int chunk_len = ltoh16(header->chunk_len);
  264. // check is_last field
  265. if (!(header->is_last == 0 || header->is_last == 1)) {
  266. BLog(BLOG_INFO, "chunk is_last wrong");
  267. break;
  268. }
  269. // obtain data
  270. if (o->in_len - o->in_pos < chunk_len) {
  271. BLog(BLOG_INFO, "too little data for chunk data");
  272. break;
  273. }
  274. // process chunk
  275. if (process_chunk(o, frame_id, chunk_start, chunk_len, header->is_last, o->in + o->in_pos) < 0) {
  276. return -1;
  277. }
  278. o->in_pos += chunk_len;
  279. // if output is blocking, stop processing input
  280. if (o->output_blocking) {
  281. return 0;
  282. }
  283. }
  284. // all input processed
  285. o->in_len = -1;
  286. // increment packet time
  287. if (o->time == FPA_MAX_TIME) {
  288. reduce_times(o);
  289. if (!LinkedList2_IsEmpty(&o->frames_used)) {
  290. ASSERT(o->time < FPA_MAX_TIME) // If there was a frame with zero time, it was removed because
  291. // time_tolerance < FPA_MAX_TIME. So something >0 was subtracted.
  292. o->time++;
  293. } else {
  294. // it was set to zero by reduce_times
  295. ASSERT(o->time == 0)
  296. }
  297. } else {
  298. o->time++;
  299. }
  300. return 0;
  301. }
  302. static int input_handler_send (FragmentProtoAssembler *o, uint8_t *data, int data_len)
  303. {
  304. ASSERT(o->in_len == -1)
  305. ASSERT(!o->output_blocking)
  306. ASSERT(data_len >= 0)
  307. ASSERT(data_len <= o->input_mtu)
  308. // save input packet
  309. o->in_len = data_len;
  310. o->in = data;
  311. o->in_pos = 0;
  312. // process input
  313. if (process_input(o) < 0) {
  314. return -1;
  315. }
  316. ASSERT((o->in_len >= 0) == o->output_blocking)
  317. // if not all input was processed (output is blocking), block input
  318. if (o->in_len >= 0) {
  319. return 0;
  320. }
  321. // all input was processed
  322. return 1;
  323. }
  324. static void output_handler_done (FragmentProtoAssembler *o)
  325. {
  326. ASSERT(o->in_len >= 0)
  327. ASSERT(o->output_blocking)
  328. // output no longer blocking
  329. o->output_blocking = 0;
  330. // process any further input
  331. if (process_input(o) < 0) {
  332. return;
  333. }
  334. ASSERT((o->in_len >= 0) == o->output_blocking)
  335. // if output is again blocking, keep input blocked
  336. if (o->in_len >= 0) {
  337. return;
  338. }
  339. // tell input we're done
  340. PacketPassInterface_Done(&o->input);
  341. return;
  342. }
  343. int FragmentProtoAssembler_Init (FragmentProtoAssembler *o, int input_mtu, PacketPassInterface *output, int num_frames, int num_chunks)
  344. {
  345. ASSERT(input_mtu >= 0)
  346. ASSERT(num_frames > 0)
  347. ASSERT(num_frames < FPA_MAX_TIME) // needed so we can always subtract times when packet time is maximum
  348. ASSERT(num_chunks > 0)
  349. // init arguments
  350. o->input_mtu = input_mtu;
  351. o->output = output;
  352. o->num_frames = num_frames;
  353. o->num_chunks = num_chunks;
  354. // init dead var
  355. DEAD_INIT(o->dead);
  356. // init input
  357. PacketPassInterface_Init(&o->input, o->input_mtu, (PacketPassInterface_handler_send)input_handler_send, o);
  358. // init output
  359. PacketPassInterface_Sender_Init(o->output, (PacketPassInterface_handler_done)output_handler_done, o);
  360. // remebmer output MTU
  361. o->output_mtu = PacketPassInterface_GetMTU(o->output);
  362. // set packet time to zero
  363. o->time = 0;
  364. // set time tolerance to num_frames
  365. o->time_tolerance = o->num_frames;
  366. // allocate frames
  367. if (!(o->frames_entries = malloc(o->num_frames * sizeof(struct FragmentProtoAssembler_frame)))) {
  368. goto fail1;
  369. }
  370. // allocate chunks
  371. if (!(o->frames_chunks = malloc(o->num_frames * o->num_chunks * sizeof(struct FragmentProtoAssembler_chunk)))) {
  372. goto fail2;
  373. }
  374. // allocate buffers
  375. if (!(o->frames_buffer = malloc(o->num_frames * o->output_mtu))) {
  376. goto fail3;
  377. }
  378. // init frame lists
  379. LinkedList2_Init(&o->frames_free);
  380. LinkedList2_Init(&o->frames_used);
  381. // initialize frame entries
  382. for (int i = 0; i < num_frames; i++) {
  383. struct FragmentProtoAssembler_frame *frame = &o->frames_entries[i];
  384. // set chunks array pointer
  385. frame->chunks = o->frames_chunks + i * o->num_chunks;
  386. // set buffer pointer
  387. frame->buffer = o->frames_buffer + i * o->output_mtu;
  388. // add to free list
  389. LinkedList2_Append(&o->frames_free, &frame->list_node);
  390. }
  391. // init tree
  392. BAVL_Init(&o->frames_used_tree, OFFSET_DIFF(struct FragmentProtoAssembler_frame, id, tree_node), (BAVL_comparator)frame_id_comparator, NULL);
  393. // have no input packet
  394. o->in_len = -1;
  395. // output not blocking
  396. o->output_blocking = 0;
  397. // init debug object
  398. DebugObject_Init(&o->d_obj);
  399. return 1;
  400. fail3:
  401. free(o->frames_chunks);
  402. fail2:
  403. free(o->frames_entries);
  404. fail1:
  405. PacketPassInterface_Free(&o->input);
  406. return 0;
  407. }
  408. void FragmentProtoAssembler_Free (FragmentProtoAssembler *o)
  409. {
  410. // free debug object
  411. DebugObject_Free(&o->d_obj);
  412. // free buffers
  413. free(o->frames_buffer);
  414. // free chunks
  415. free(o->frames_chunks);
  416. // free frames
  417. free(o->frames_entries);
  418. // free input
  419. PacketPassInterface_Free(&o->input);
  420. // free dead var
  421. DEAD_KILL(o->dead);
  422. }
  423. PacketPassInterface * FragmentProtoAssembler_GetInput (FragmentProtoAssembler *o)
  424. {
  425. return &o->input;
  426. }