udp_proto.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. t = ~t;
  80. if (t == 0) {
  81. t = UINT16_MAX;
  82. }
  83. return hton16(t);
  84. }
  85. 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)
  86. {
  87. uint32_t t = 0;
  88. t += udp_checksum_summer((const char *)source_addr, 16);
  89. t += udp_checksum_summer((const char *)dest_addr, 16);
  90. uint32_t x;
  91. x = hton32(sizeof(*header) + payload_len);
  92. t += udp_checksum_summer((char *)&x, sizeof(x));
  93. x = hton32(IPV6_NEXT_UDP);
  94. t += udp_checksum_summer((char *)&x, sizeof(x));
  95. t += udp_checksum_summer((const char *)header, sizeof(*header));
  96. if (payload_len % 2 == 0) {
  97. t += udp_checksum_summer((const char *)payload, payload_len);
  98. } else {
  99. t += udp_checksum_summer((const char *)payload, payload_len - 1);
  100. uint16_t y;
  101. y = hton16(((uint16_t)payload[payload_len - 1]) << 8);
  102. t += udp_checksum_summer((char *)&y, sizeof(y));
  103. }
  104. while (t >> 16) {
  105. t = (t & 0xFFFF) + (t >> 16);
  106. }
  107. t = ~t;
  108. if (t == 0) {
  109. t = UINT16_MAX;
  110. }
  111. return hton16(t);
  112. }
  113. static int udp_check (const uint8_t *data, int data_len, struct udp_header *out_header, uint8_t **out_payload, int *out_payload_len)
  114. {
  115. ASSERT(data_len >= 0)
  116. ASSERT(out_header)
  117. ASSERT(out_payload)
  118. ASSERT(out_payload_len)
  119. // parse UDP header
  120. if (data_len < sizeof(struct udp_header)) {
  121. return 0;
  122. }
  123. memcpy(out_header, data, sizeof(*out_header));
  124. data += sizeof(*out_header);
  125. data_len -= sizeof(*out_header);
  126. // verify UDP payload
  127. int udp_length = ntoh16(out_header->length);
  128. if (udp_length < sizeof(*out_header)) {
  129. return 0;
  130. }
  131. if (udp_length > sizeof(*out_header) + data_len) {
  132. return 0;
  133. }
  134. // ignore stray data
  135. data_len = udp_length - sizeof(*out_header);
  136. *out_payload = (uint8_t *)data;
  137. *out_payload_len = data_len;
  138. return 1;
  139. }
  140. #endif