udp_proto.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /**
  2. * @file udp_proto.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * @section DESCRIPTION
  30. *
  31. * Definitions for the UDP protocol.
  32. */
  33. #ifndef BADVPN_MISC_UDP_PROTO_H
  34. #define BADVPN_MISC_UDP_PROTO_H
  35. #include <stdint.h>
  36. #include <misc/debug.h>
  37. #include <misc/byteorder.h>
  38. #include <misc/ipv4_proto.h>
  39. #include <misc/ipv6_proto.h>
  40. #include <misc/read_write_int.h>
  41. B_START_PACKED
  42. struct udp_header {
  43. uint16_t source_port;
  44. uint16_t dest_port;
  45. uint16_t length;
  46. uint16_t checksum;
  47. } B_PACKED;
  48. B_END_PACKED
  49. static uint32_t udp_checksum_summer (const char *data, uint16_t len)
  50. {
  51. ASSERT(len % 2 == 0)
  52. uint32_t t = 0;
  53. for (uint16_t i = 0; i < len / 2; i++) {
  54. t += badvpn_read_be16(data + 2 * i);
  55. }
  56. return t;
  57. }
  58. static uint16_t udp_checksum (const struct udp_header *header, const uint8_t *payload, uint16_t payload_len, uint32_t source_addr, uint32_t dest_addr)
  59. {
  60. uint32_t t = 0;
  61. t += udp_checksum_summer((char *)&source_addr, sizeof(source_addr));
  62. t += udp_checksum_summer((char *)&dest_addr, sizeof(dest_addr));
  63. uint16_t x;
  64. x = hton16(IPV4_PROTOCOL_UDP);
  65. t += udp_checksum_summer((char *)&x, sizeof(x));
  66. x = hton16(sizeof(*header) + payload_len);
  67. t += udp_checksum_summer((char *)&x, sizeof(x));
  68. t += udp_checksum_summer((const char *)header, sizeof(*header));
  69. if (payload_len % 2 == 0) {
  70. t += udp_checksum_summer((const char *)payload, payload_len);
  71. } else {
  72. t += udp_checksum_summer((const char *)payload, payload_len - 1);
  73. x = hton16(((uint16_t)payload[payload_len - 1]) << 8);
  74. t += udp_checksum_summer((char *)&x, sizeof(x));
  75. }
  76. while (t >> 16) {
  77. t = (t & 0xFFFF) + (t >> 16);
  78. }
  79. if (t == 0) {
  80. t = UINT16_MAX;
  81. }
  82. return hton16(~t);
  83. }
  84. static uint16_t udp_ip6_checksum (const struct udp_header *header, const uint8_t *payload, uint16_t payload_len, const uint8_t *source_addr, const uint8_t *dest_addr)
  85. {
  86. uint32_t t = 0;
  87. t += udp_checksum_summer((const char *)source_addr, 16);
  88. t += udp_checksum_summer((const char *)dest_addr, 16);
  89. uint32_t x;
  90. x = hton32(sizeof(*header) + payload_len);
  91. t += udp_checksum_summer((char *)&x, sizeof(x));
  92. x = hton32(IPV6_NEXT_UDP);
  93. t += udp_checksum_summer((char *)&x, sizeof(x));
  94. t += udp_checksum_summer((const char *)header, sizeof(*header));
  95. if (payload_len % 2 == 0) {
  96. t += udp_checksum_summer((const char *)payload, payload_len);
  97. } else {
  98. t += udp_checksum_summer((const char *)payload, payload_len - 1);
  99. uint16_t y;
  100. y = hton16(((uint16_t)payload[payload_len - 1]) << 8);
  101. t += udp_checksum_summer((char *)&y, sizeof(y));
  102. }
  103. while (t >> 16) {
  104. t = (t & 0xFFFF) + (t >> 16);
  105. }
  106. if (t == 0) {
  107. t = UINT16_MAX;
  108. }
  109. return hton16(~t);
  110. }
  111. static int udp_check (const uint8_t *data, int data_len, struct udp_header *out_header, uint8_t **out_payload, int *out_payload_len)
  112. {
  113. ASSERT(data_len >= 0)
  114. ASSERT(out_header)
  115. ASSERT(out_payload)
  116. ASSERT(out_payload_len)
  117. // parse UDP header
  118. if (data_len < sizeof(struct udp_header)) {
  119. return 0;
  120. }
  121. memcpy(out_header, data, sizeof(*out_header));
  122. data += sizeof(*out_header);
  123. data_len -= sizeof(*out_header);
  124. // verify UDP payload
  125. int udp_length = ntoh16(out_header->length);
  126. if (udp_length < sizeof(*out_header)) {
  127. return 0;
  128. }
  129. if (udp_length > sizeof(*out_header) + data_len) {
  130. return 0;
  131. }
  132. // ignore stray data
  133. data_len = udp_length - sizeof(*out_header);
  134. *out_payload = (uint8_t *)data;
  135. *out_payload_len = data_len;
  136. return 1;
  137. }
  138. #endif