FragmentProtoAssembler.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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. 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 > 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(chunk_start >= 0)
  132. ASSERT(chunk_len >= 0)
  133. ASSERT(is_last == 0 || is_last == 1)
  134. // verify chunk
  135. // check start
  136. if (chunk_start > o->output_mtu) {
  137. BLog(BLOG_INFO, "chunk starts outside");
  138. return 0;
  139. }
  140. // check frame size bound
  141. if (chunk_len > o->output_mtu - chunk_start) {
  142. BLog(BLOG_INFO, "chunk ends outside");
  143. return 0;
  144. }
  145. // calculate end
  146. int chunk_end = chunk_start + chunk_len;
  147. ASSERT(chunk_end >= 0)
  148. ASSERT(chunk_end <= o->output_mtu)
  149. // lookup frame
  150. struct FragmentProtoAssembler_frame *frame;
  151. BAVLNode *tree_node;
  152. if (!(tree_node = BAVL_LookupExact(&o->frames_used_tree, &frame_id))) {
  153. // frame not found, add a new one
  154. frame = allocate_new_frame(o, frame_id);
  155. } else {
  156. // have existing frame with that ID
  157. frame = UPPER_OBJECT(tree_node, struct FragmentProtoAssembler_frame, tree_node);
  158. // check frame time
  159. if (frame_is_timed_out(o, frame)) {
  160. // frame is timed out, remove it and use a new one
  161. BLog(BLOG_INFO, "freeing timed out frame (while processing chunk)");
  162. free_frame(o, frame);
  163. frame = allocate_new_frame(o, frame_id);
  164. }
  165. }
  166. ASSERT(frame->num_chunks < o->num_chunks)
  167. // check if the chunk overlaps with any existing chunks
  168. for (int i = 0; i < frame->num_chunks; i++) {
  169. struct FragmentProtoAssembler_chunk *chunk = &frame->chunks[i];
  170. if (chunks_overlap(chunk->start, chunk->len, chunk_start, chunk_len)) {
  171. BLog(BLOG_INFO, "chunk overlaps with existing chunk");
  172. goto fail_frame;
  173. }
  174. }
  175. if (is_last) {
  176. // this chunk is marked as last
  177. if (frame->length >= 0) {
  178. BLog(BLOG_INFO, "got last chunk, but already have one");
  179. goto fail_frame;
  180. }
  181. // check if frame size according to this packet is consistent
  182. // with existing chunks
  183. if (frame->length_so_far > chunk_end) {
  184. BLog(BLOG_INFO, "got last chunk, but already have data over its bound");
  185. goto fail_frame;
  186. }
  187. } else {
  188. // if we have length, chunk must be in its bound
  189. if (frame->length >= 0) {
  190. if (chunk_end > frame->length) {
  191. BLog(BLOG_INFO, "chunk out of length bound");
  192. goto fail_frame;
  193. }
  194. }
  195. }
  196. // chunk is good, add it
  197. // update frame time
  198. frame->time = o->time;
  199. // add chunk entry
  200. struct FragmentProtoAssembler_chunk *chunk = &frame->chunks[frame->num_chunks];
  201. chunk->start = chunk_start;
  202. chunk->len = chunk_len;
  203. frame->num_chunks++;
  204. // update sum
  205. frame->sum += chunk_len;
  206. // update length
  207. if (is_last) {
  208. frame->length = chunk_end;
  209. } else {
  210. if (frame->length < 0) {
  211. if (frame->length_so_far < chunk_end) {
  212. frame->length_so_far = chunk_end;
  213. }
  214. }
  215. }
  216. // copy chunk payload to buffer
  217. memcpy(frame->buffer + chunk_start, payload, chunk_len);
  218. // is frame incomplete?
  219. if (frame->length < 0 || frame->sum < frame->length) {
  220. // if all chunks are used, fail it
  221. if (frame->num_chunks == o->num_chunks) {
  222. BLog(BLOG_INFO, "all chunks used, but frame not complete");
  223. goto fail_frame;
  224. }
  225. // wait for more chunks
  226. return 0;
  227. }
  228. ASSERT(frame->sum == frame->length)
  229. BLog(BLOG_DEBUG, "frame complete");
  230. // free frame entry
  231. free_frame(o, frame);
  232. // send frame
  233. PacketPassInterface_Sender_Send(o->output, frame->buffer, frame->length);
  234. return 1;
  235. fail_frame:
  236. free_frame(o, frame);
  237. return 0;
  238. }
  239. static void process_input (FragmentProtoAssembler *o)
  240. {
  241. ASSERT(o->in_len >= 0)
  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. int is_last = ltoh8(header->is_last);
  255. // check is_last field
  256. if (!(is_last == 0 || is_last == 1)) {
  257. BLog(BLOG_INFO, "chunk is_last wrong");
  258. break;
  259. }
  260. // obtain data
  261. if (o->in_len - o->in_pos < chunk_len) {
  262. BLog(BLOG_INFO, "too little data for chunk data");
  263. break;
  264. }
  265. // process chunk
  266. int res = process_chunk(o, frame_id, chunk_start, chunk_len, is_last, o->in + o->in_pos);
  267. o->in_pos += chunk_len;
  268. if (res) {
  269. // sending complete frame, stop processing input
  270. return;
  271. }
  272. }
  273. // increment packet time
  274. if (o->time == FPA_MAX_TIME) {
  275. reduce_times(o);
  276. if (!LinkedList2_IsEmpty(&o->frames_used)) {
  277. ASSERT(o->time < FPA_MAX_TIME) // If there was a frame with zero time, it was removed because
  278. // time_tolerance < FPA_MAX_TIME. So something >0 was subtracted.
  279. o->time++;
  280. } else {
  281. // it was set to zero by reduce_times
  282. ASSERT(o->time == 0)
  283. }
  284. } else {
  285. o->time++;
  286. }
  287. // set no input packet
  288. o->in_len = -1;
  289. // finish input
  290. PacketPassInterface_Done(&o->input);
  291. }
  292. static void input_handler_send (FragmentProtoAssembler *o, uint8_t *data, int data_len)
  293. {
  294. ASSERT(data_len >= 0)
  295. ASSERT(o->in_len == -1)
  296. DebugObject_Access(&o->d_obj);
  297. // save input packet
  298. o->in_len = data_len;
  299. o->in = data;
  300. o->in_pos = 0;
  301. process_input(o);
  302. }
  303. static void output_handler_done (FragmentProtoAssembler *o)
  304. {
  305. ASSERT(o->in_len >= 0)
  306. DebugObject_Access(&o->d_obj);
  307. process_input(o);
  308. }
  309. int FragmentProtoAssembler_Init (FragmentProtoAssembler *o, int input_mtu, PacketPassInterface *output, int num_frames, int num_chunks, BPendingGroup *pg)
  310. {
  311. ASSERT(input_mtu >= 0)
  312. ASSERT(num_frames > 0)
  313. ASSERT(num_frames < FPA_MAX_TIME) // needed so we can always subtract times when packet time is maximum
  314. ASSERT(num_chunks > 0)
  315. // init arguments
  316. o->output = output;
  317. o->num_chunks = num_chunks;
  318. // init input
  319. PacketPassInterface_Init(&o->input, input_mtu, (PacketPassInterface_handler_send)input_handler_send, o, pg);
  320. // init output
  321. PacketPassInterface_Sender_Init(o->output, (PacketPassInterface_handler_done)output_handler_done, o);
  322. // remebmer output MTU
  323. o->output_mtu = PacketPassInterface_GetMTU(o->output);
  324. // set packet time to zero
  325. o->time = 0;
  326. // set time tolerance to num_frames
  327. o->time_tolerance = num_frames;
  328. // allocate frames
  329. if (!(o->frames_entries = BAllocArray(num_frames, sizeof(struct FragmentProtoAssembler_frame)))) {
  330. goto fail1;
  331. }
  332. // allocate chunks
  333. if (!(o->frames_chunks = BAllocArray2(num_frames, o->num_chunks, sizeof(struct FragmentProtoAssembler_chunk)))) {
  334. goto fail2;
  335. }
  336. // allocate buffers
  337. if (!(o->frames_buffer = BAllocArray(num_frames, o->output_mtu))) {
  338. goto fail3;
  339. }
  340. // init frame lists
  341. LinkedList2_Init(&o->frames_free);
  342. LinkedList2_Init(&o->frames_used);
  343. // initialize frame entries
  344. for (int i = 0; i < num_frames; i++) {
  345. struct FragmentProtoAssembler_frame *frame = &o->frames_entries[i];
  346. // set chunks array pointer
  347. frame->chunks = o->frames_chunks + i * o->num_chunks;
  348. // set buffer pointer
  349. frame->buffer = o->frames_buffer + i * o->output_mtu;
  350. // add to free list
  351. LinkedList2_Append(&o->frames_free, &frame->list_node);
  352. }
  353. // init tree
  354. BAVL_Init(&o->frames_used_tree, OFFSET_DIFF(struct FragmentProtoAssembler_frame, id, tree_node), (BAVL_comparator)frame_id_comparator, NULL);
  355. // have no input packet
  356. o->in_len = -1;
  357. DebugObject_Init(&o->d_obj);
  358. return 1;
  359. fail3:
  360. BFree(o->frames_chunks);
  361. fail2:
  362. BFree(o->frames_entries);
  363. fail1:
  364. PacketPassInterface_Free(&o->input);
  365. return 0;
  366. }
  367. void FragmentProtoAssembler_Free (FragmentProtoAssembler *o)
  368. {
  369. DebugObject_Free(&o->d_obj);
  370. // free buffers
  371. BFree(o->frames_buffer);
  372. // free chunks
  373. BFree(o->frames_chunks);
  374. // free frames
  375. BFree(o->frames_entries);
  376. // free input
  377. PacketPassInterface_Free(&o->input);
  378. }
  379. PacketPassInterface * FragmentProtoAssembler_GetInput (FragmentProtoAssembler *o)
  380. {
  381. DebugObject_Access(&o->d_obj);
  382. return &o->input;
  383. }