ipv4_proto.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /**
  2. * @file ipv4_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 IPv4 protocol.
  32. */
  33. #ifndef BADVPN_MISC_IPV4_PROTO_H
  34. #define BADVPN_MISC_IPV4_PROTO_H
  35. #include <stdint.h>
  36. #include <string.h>
  37. #include <misc/debug.h>
  38. #include <misc/byteorder.h>
  39. #include <misc/packed.h>
  40. #include <misc/read_write_int.h>
  41. #define IPV4_PROTOCOL_IGMP 2
  42. #define IPV4_PROTOCOL_UDP 17
  43. B_START_PACKED
  44. struct ipv4_header {
  45. uint8_t version4_ihl4;
  46. uint8_t ds;
  47. uint16_t total_length;
  48. //
  49. uint16_t identification;
  50. uint16_t flags3_fragmentoffset13;
  51. //
  52. uint8_t ttl;
  53. uint8_t protocol;
  54. uint16_t checksum;
  55. //
  56. uint32_t source_address;
  57. //
  58. uint32_t destination_address;
  59. } B_PACKED;
  60. B_END_PACKED
  61. #define IPV4_GET_VERSION(_header) (((_header).version4_ihl4&0xF0)>>4)
  62. #define IPV4_GET_IHL(_header) (((_header).version4_ihl4&0x0F)>>0)
  63. #define IPV4_MAKE_VERSION_IHL(size) (((size)/4) + (4 << 4))
  64. static uint16_t ipv4_checksum (const struct ipv4_header *header, const char *extra, uint16_t extra_len)
  65. {
  66. ASSERT(extra_len % 2 == 0)
  67. ASSERT(extra_len == 0 || extra)
  68. uint32_t t = 0;
  69. for (uint16_t i = 0; i < sizeof(*header) / 2; i++) {
  70. t += badvpn_read_be16((const char *)header + 2 * i);
  71. }
  72. for (uint16_t i = 0; i < extra_len / 2; i++) {
  73. t += badvpn_read_be16((const char *)extra + 2 * i);
  74. }
  75. while (t >> 16) {
  76. t = (t & 0xFFFF) + (t >> 16);
  77. }
  78. return hton16(~t);
  79. }
  80. static int ipv4_check (uint8_t *data, int data_len, struct ipv4_header *out_header, uint8_t **out_payload, int *out_payload_len)
  81. {
  82. ASSERT(data_len >= 0)
  83. ASSERT(out_header)
  84. ASSERT(out_payload)
  85. ASSERT(out_payload_len)
  86. // check base header
  87. if (data_len < sizeof(struct ipv4_header)) {
  88. return 0;
  89. }
  90. memcpy(out_header, data, sizeof(*out_header));
  91. // check version
  92. if (IPV4_GET_VERSION(*out_header) != 4) {
  93. return 0;
  94. }
  95. // check options
  96. uint16_t header_len = IPV4_GET_IHL(*out_header) * 4;
  97. if (header_len < sizeof(struct ipv4_header)) {
  98. return 0;
  99. }
  100. if (header_len > data_len) {
  101. return 0;
  102. }
  103. // check total length
  104. uint16_t total_length = ntoh16(out_header->total_length);
  105. if (total_length < header_len) {
  106. return 0;
  107. }
  108. if (total_length > data_len) {
  109. return 0;
  110. }
  111. // check checksum
  112. uint16_t checksum_in_packet = out_header->checksum;
  113. out_header->checksum = hton16(0);
  114. uint16_t checksum_computed = ipv4_checksum(out_header, (char *)data + sizeof(*out_header), header_len - sizeof(*out_header));
  115. out_header->checksum = checksum_in_packet;
  116. if (checksum_in_packet != checksum_computed) {
  117. return 0;
  118. }
  119. *out_payload = data + header_len;
  120. *out_payload_len = total_length - header_len;
  121. return 1;
  122. }
  123. #endif