Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Copyright (C) 2013 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. */
  19. FILE_LICENCE ( GPL2_OR_LATER );
  20. #include <stdint.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <ipxe/retry.h>
  24. #include <ipxe/timer.h>
  25. #include <ipxe/ipstat.h>
  26. #include <ipxe/fragment.h>
  27. /** @file
  28. *
  29. * Fragment reassembly
  30. *
  31. */
  32. /**
  33. * Expire fragment reassembly buffer
  34. *
  35. * @v timer Retry timer
  36. * @v fail Failure indicator
  37. */
  38. static void fragment_expired ( struct retry_timer *timer, int fail __unused ) {
  39. struct fragment *fragment =
  40. container_of ( timer, struct fragment, timer );
  41. DBGC ( fragment, "FRAG %p expired\n", fragment );
  42. free_iob ( fragment->iobuf );
  43. list_del ( &fragment->list );
  44. fragment->fragments->stats->reasm_fails++;
  45. free ( fragment );
  46. }
  47. /**
  48. * Find fragment reassembly buffer
  49. *
  50. * @v fragments Fragment reassembler
  51. * @v iobuf I/O buffer
  52. * @v hdrlen Length of non-fragmentable potion of I/O buffer
  53. * @ret fragment Fragment reassembly buffer, or NULL if not found
  54. */
  55. static struct fragment * fragment_find ( struct fragment_reassembler *fragments,
  56. struct io_buffer *iobuf,
  57. size_t hdrlen ) {
  58. struct fragment *fragment;
  59. list_for_each_entry ( fragment, &fragments->list, list ) {
  60. if ( fragments->is_fragment ( fragment, iobuf, hdrlen ) )
  61. return fragment;
  62. }
  63. return NULL;
  64. }
  65. /**
  66. * Reassemble packet
  67. *
  68. * @v fragments Fragment reassembler
  69. * @v iobuf I/O buffer
  70. * @v hdrlen Length of non-fragmentable potion of I/O buffer
  71. * @ret iobuf Reassembled packet, or NULL
  72. *
  73. * This function takes ownership of the I/O buffer. Note that the
  74. * length of the non-fragmentable portion may be modified.
  75. */
  76. struct io_buffer * fragment_reassemble ( struct fragment_reassembler *fragments,
  77. struct io_buffer *iobuf,
  78. size_t *hdrlen ) {
  79. struct fragment *fragment;
  80. struct io_buffer *new_iobuf;
  81. size_t new_len;
  82. size_t offset;
  83. size_t expected_offset;
  84. int more_frags;
  85. /* Update statistics */
  86. fragments->stats->reasm_reqds++;
  87. /* Find matching fragment reassembly buffer, if any */
  88. fragment = fragment_find ( fragments, iobuf, *hdrlen );
  89. /* Drop out-of-order fragments */
  90. offset = fragments->fragment_offset ( iobuf, *hdrlen );
  91. expected_offset = ( fragment ? ( iob_len ( fragment->iobuf ) -
  92. fragment->hdrlen ) : 0 );
  93. if ( offset != expected_offset ) {
  94. DBGC ( fragment, "FRAG %p dropping out-of-sequence fragment "
  95. "[%zd,%zd), expected [%zd,...)\n", fragment, offset,
  96. ( offset + iob_len ( iobuf ) - *hdrlen ),
  97. expected_offset );
  98. goto drop;
  99. }
  100. /* Create or extend fragment reassembly buffer as applicable */
  101. if ( ! fragment ) {
  102. /* Create new fragment reassembly buffer */
  103. fragment = zalloc ( sizeof ( *fragment ) );
  104. if ( ! fragment )
  105. goto drop;
  106. list_add ( &fragment->list, &fragments->list );
  107. fragment->iobuf = iobuf;
  108. fragment->hdrlen = *hdrlen;
  109. timer_init ( &fragment->timer, fragment_expired, NULL );
  110. fragment->fragments = fragments;
  111. DBGC ( fragment, "FRAG %p [0,%zd)\n", fragment,
  112. ( iob_len ( iobuf ) - *hdrlen ) );
  113. } else {
  114. /* Check if this is the final fragment */
  115. more_frags = fragments->more_fragments ( iobuf, *hdrlen );
  116. DBGC ( fragment, "FRAG %p [%zd,%zd)%s\n", fragment,
  117. offset, ( offset + iob_len ( iobuf ) - *hdrlen ),
  118. ( more_frags ? "" : " complete" ) );
  119. /* Extend fragment reassembly buffer. Preserve I/O
  120. * buffer headroom to allow for code which modifies
  121. * and resends the buffer (e.g. ICMP echo responses).
  122. */
  123. iob_pull ( iobuf, *hdrlen );
  124. new_len = ( iob_headroom ( fragment->iobuf ) +
  125. iob_len ( fragment->iobuf ) + iob_len ( iobuf ) );
  126. new_iobuf = alloc_iob ( new_len );
  127. if ( ! new_iobuf ) {
  128. DBGC ( fragment, "FRAG %p could not extend reassembly "
  129. "buffer to %zd bytes\n", fragment, new_len );
  130. goto drop;
  131. }
  132. iob_reserve ( new_iobuf, iob_headroom ( fragment->iobuf ) );
  133. memcpy ( iob_put ( new_iobuf, iob_len ( fragment->iobuf ) ),
  134. fragment->iobuf->data, iob_len ( fragment->iobuf ) );
  135. memcpy ( iob_put ( new_iobuf, iob_len ( iobuf ) ),
  136. iobuf->data, iob_len ( iobuf ) );
  137. free_iob ( fragment->iobuf );
  138. fragment->iobuf = new_iobuf;
  139. free_iob ( iobuf );
  140. /* Stop fragment reassembly timer */
  141. stop_timer ( &fragment->timer );
  142. /* If this is the final fragment, return it */
  143. if ( ! more_frags ) {
  144. iobuf = fragment->iobuf;
  145. *hdrlen = fragment->hdrlen;
  146. list_del ( &fragment->list );
  147. free ( fragment );
  148. fragments->stats->reasm_oks++;
  149. return iobuf;
  150. }
  151. }
  152. /* (Re)start fragment reassembly timer */
  153. start_timer_fixed ( &fragment->timer, FRAGMENT_TIMEOUT );
  154. return NULL;
  155. drop:
  156. fragments->stats->reasm_fails++;
  157. free_iob ( iobuf );
  158. return NULL;
  159. }