netbuff.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2010 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <grub/err.h>
  19. #include <grub/misc.h>
  20. #include <grub/mm.h>
  21. #include <grub/net/netbuff.h>
  22. grub_err_t
  23. grub_netbuff_put (struct grub_net_buff *nb, grub_size_t len)
  24. {
  25. nb->tail += len;
  26. if (nb->tail > nb->end)
  27. return grub_error (GRUB_ERR_BUG, "put out of the packet range.");
  28. return GRUB_ERR_NONE;
  29. }
  30. grub_err_t
  31. grub_netbuff_unput (struct grub_net_buff *nb, grub_size_t len)
  32. {
  33. nb->tail -= len;
  34. if (nb->tail < nb->head)
  35. return grub_error (GRUB_ERR_BUG,
  36. "unput out of the packet range.");
  37. return GRUB_ERR_NONE;
  38. }
  39. grub_err_t
  40. grub_netbuff_push (struct grub_net_buff *nb, grub_size_t len)
  41. {
  42. nb->data -= len;
  43. if (nb->data < nb->head)
  44. return grub_error (GRUB_ERR_BUG,
  45. "push out of the packet range.");
  46. return GRUB_ERR_NONE;
  47. }
  48. grub_err_t
  49. grub_netbuff_pull (struct grub_net_buff *nb, grub_size_t len)
  50. {
  51. nb->data += len;
  52. if (nb->data > nb->end || nb->data > nb->tail)
  53. return grub_error (GRUB_ERR_BUG,
  54. "pull out of the packet range.");
  55. return GRUB_ERR_NONE;
  56. }
  57. grub_err_t
  58. grub_netbuff_reserve (struct grub_net_buff *nb, grub_size_t len)
  59. {
  60. nb->data += len;
  61. nb->tail += len;
  62. if ((nb->tail > nb->end) || (nb->data > nb->end))
  63. return grub_error (GRUB_ERR_BUG,
  64. "reserve out of the packet range.");
  65. return GRUB_ERR_NONE;
  66. }
  67. struct grub_net_buff *
  68. grub_netbuff_alloc (grub_size_t len)
  69. {
  70. struct grub_net_buff *nb;
  71. void *data;
  72. COMPILE_TIME_ASSERT (NETBUFF_ALIGN % sizeof (grub_properly_aligned_t) == 0);
  73. /*
  74. * The largest size of a TCP packet is 64 KiB, and everything else
  75. * should be a lot smaller - most MTUs are 1500 or less. Cap data
  76. * size at 64 KiB + a buffer.
  77. */
  78. if (len > 0xffffUL + 0x1000UL)
  79. {
  80. grub_error (GRUB_ERR_BUG,
  81. "attempted to allocate a packet that is too big");
  82. return NULL;
  83. }
  84. if (len < NETBUFFMINLEN)
  85. len = NETBUFFMINLEN;
  86. len = ALIGN_UP (len, NETBUFF_ALIGN);
  87. #ifdef GRUB_MACHINE_EMU
  88. data = grub_malloc (len + sizeof (*nb));
  89. #else
  90. data = grub_memalign (NETBUFF_ALIGN, len + sizeof (*nb));
  91. #endif
  92. if (!data)
  93. return NULL;
  94. nb = (struct grub_net_buff *) ((grub_properly_aligned_t *) data
  95. + len / sizeof (grub_properly_aligned_t));
  96. nb->head = nb->data = nb->tail = data;
  97. nb->end = (grub_uint8_t *) nb;
  98. return nb;
  99. }
  100. struct grub_net_buff *
  101. grub_netbuff_make_pkt (grub_size_t len)
  102. {
  103. struct grub_net_buff *nb;
  104. grub_err_t err;
  105. nb = grub_netbuff_alloc (len + 512);
  106. if (!nb)
  107. return NULL;
  108. err = grub_netbuff_reserve (nb, len + 512);
  109. if (err)
  110. goto fail;
  111. err = grub_netbuff_push (nb, len);
  112. if (err)
  113. goto fail;
  114. return nb;
  115. fail:
  116. grub_netbuff_free (nb);
  117. return NULL;
  118. }
  119. void
  120. grub_netbuff_free (struct grub_net_buff *nb)
  121. {
  122. if (!nb)
  123. return;
  124. grub_free (nb->head);
  125. }
  126. grub_err_t
  127. grub_netbuff_clear (struct grub_net_buff *nb)
  128. {
  129. nb->data = nb->tail = nb->head;
  130. return GRUB_ERR_NONE;
  131. }