You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

fragment.c 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <stdint.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <ipxe/retry.h>
  28. #include <ipxe/timer.h>
  29. #include <ipxe/ipstat.h>
  30. #include <ipxe/fragment.h>
  31. /** @file
  32. *
  33. * Fragment reassembly
  34. *
  35. */
  36. /**
  37. * Expire fragment reassembly buffer
  38. *
  39. * @v timer Retry timer
  40. * @v fail Failure indicator
  41. */
  42. static void fragment_expired ( struct retry_timer *timer, int fail __unused ) {
  43. struct fragment *fragment =
  44. container_of ( timer, struct fragment, timer );
  45. DBGC ( fragment, "FRAG %p expired\n", fragment );
  46. free_iob ( fragment->iobuf );
  47. list_del ( &fragment->list );
  48. fragment->fragments->stats->reasm_fails++;
  49. free ( fragment );
  50. }
  51. /**
  52. * Find fragment reassembly buffer
  53. *
  54. * @v fragments Fragment reassembler
  55. * @v iobuf I/O buffer
  56. * @v hdrlen Length of non-fragmentable potion of I/O buffer
  57. * @ret fragment Fragment reassembly buffer, or NULL if not found
  58. */
  59. static struct fragment * fragment_find ( struct fragment_reassembler *fragments,
  60. struct io_buffer *iobuf,
  61. size_t hdrlen ) {
  62. struct fragment *fragment;
  63. list_for_each_entry ( fragment, &fragments->list, list ) {
  64. if ( fragments->is_fragment ( fragment, iobuf, hdrlen ) )
  65. return fragment;
  66. }
  67. return NULL;
  68. }
  69. /**
  70. * Reassemble packet
  71. *
  72. * @v fragments Fragment reassembler
  73. * @v iobuf I/O buffer
  74. * @v hdrlen Length of non-fragmentable potion of I/O buffer
  75. * @ret iobuf Reassembled packet, or NULL
  76. *
  77. * This function takes ownership of the I/O buffer. Note that the
  78. * length of the non-fragmentable portion may be modified.
  79. */
  80. struct io_buffer * fragment_reassemble ( struct fragment_reassembler *fragments,
  81. struct io_buffer *iobuf,
  82. size_t *hdrlen ) {
  83. struct fragment *fragment;
  84. struct io_buffer *new_iobuf;
  85. size_t new_len;
  86. size_t offset;
  87. size_t expected_offset;
  88. int more_frags;
  89. /* Update statistics */
  90. fragments->stats->reasm_reqds++;
  91. /* Find matching fragment reassembly buffer, if any */
  92. fragment = fragment_find ( fragments, iobuf, *hdrlen );
  93. /* Drop out-of-order fragments */
  94. offset = fragments->fragment_offset ( iobuf, *hdrlen );
  95. expected_offset = ( fragment ? ( iob_len ( fragment->iobuf ) -
  96. fragment->hdrlen ) : 0 );
  97. if ( offset != expected_offset ) {
  98. DBGC ( fragment, "FRAG %p dropping out-of-sequence fragment "
  99. "[%zd,%zd), expected [%zd,...)\n", fragment, offset,
  100. ( offset + iob_len ( iobuf ) - *hdrlen ),
  101. expected_offset );
  102. goto drop;
  103. }
  104. /* Create or extend fragment reassembly buffer as applicable */
  105. if ( ! fragment ) {
  106. /* Create new fragment reassembly buffer */
  107. fragment = zalloc ( sizeof ( *fragment ) );
  108. if ( ! fragment )
  109. goto drop;
  110. list_add ( &fragment->list, &fragments->list );
  111. fragment->iobuf = iobuf;
  112. fragment->hdrlen = *hdrlen;
  113. timer_init ( &fragment->timer, fragment_expired, NULL );
  114. fragment->fragments = fragments;
  115. DBGC ( fragment, "FRAG %p [0,%zd)\n", fragment,
  116. ( iob_len ( iobuf ) - *hdrlen ) );
  117. } else {
  118. /* Check if this is the final fragment */
  119. more_frags = fragments->more_fragments ( iobuf, *hdrlen );
  120. DBGC ( fragment, "FRAG %p [%zd,%zd)%s\n", fragment,
  121. offset, ( offset + iob_len ( iobuf ) - *hdrlen ),
  122. ( more_frags ? "" : " complete" ) );
  123. /* Extend fragment reassembly buffer. Preserve I/O
  124. * buffer headroom to allow for code which modifies
  125. * and resends the buffer (e.g. ICMP echo responses).
  126. */
  127. iob_pull ( iobuf, *hdrlen );
  128. new_len = ( iob_headroom ( fragment->iobuf ) +
  129. iob_len ( fragment->iobuf ) + iob_len ( iobuf ) );
  130. new_iobuf = alloc_iob ( new_len );
  131. if ( ! new_iobuf ) {
  132. DBGC ( fragment, "FRAG %p could not extend reassembly "
  133. "buffer to %zd bytes\n", fragment, new_len );
  134. goto drop;
  135. }
  136. iob_reserve ( new_iobuf, iob_headroom ( fragment->iobuf ) );
  137. memcpy ( iob_put ( new_iobuf, iob_len ( fragment->iobuf ) ),
  138. fragment->iobuf->data, iob_len ( fragment->iobuf ) );
  139. memcpy ( iob_put ( new_iobuf, iob_len ( iobuf ) ),
  140. iobuf->data, iob_len ( iobuf ) );
  141. free_iob ( fragment->iobuf );
  142. fragment->iobuf = new_iobuf;
  143. free_iob ( iobuf );
  144. /* Stop fragment reassembly timer */
  145. stop_timer ( &fragment->timer );
  146. /* If this is the final fragment, return it */
  147. if ( ! more_frags ) {
  148. iobuf = fragment->iobuf;
  149. *hdrlen = fragment->hdrlen;
  150. list_del ( &fragment->list );
  151. free ( fragment );
  152. fragments->stats->reasm_oks++;
  153. return iobuf;
  154. }
  155. }
  156. /* (Re)start fragment reassembly timer */
  157. start_timer_fixed ( &fragment->timer, FRAGMENT_TIMEOUT );
  158. return NULL;
  159. drop:
  160. fragments->stats->reasm_fails++;
  161. free_iob ( iobuf );
  162. return NULL;
  163. }