ipv4_proto.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 <misc/debug.h>
  37. #include <misc/byteorder.h>
  38. #include <misc/packed.h>
  39. #define IPV4_PROTOCOL_IGMP 2
  40. #define IPV4_PROTOCOL_UDP 17
  41. B_START_PACKED
  42. struct ipv4_header {
  43. uint8_t version4_ihl4;
  44. uint8_t ds;
  45. uint16_t total_length;
  46. //
  47. uint16_t identification;
  48. uint16_t flags3_fragmentoffset13;
  49. //
  50. uint8_t ttl;
  51. uint8_t protocol;
  52. uint16_t checksum;
  53. //
  54. uint32_t source_address;
  55. //
  56. uint32_t destination_address;
  57. } B_PACKED;
  58. B_END_PACKED
  59. #define IPV4_GET_VERSION(_header) (((_header).version4_ihl4&0xF0)>>4)
  60. #define IPV4_GET_IHL(_header) (((_header).version4_ihl4&0x0F)>>0)
  61. #define IPV4_MAKE_VERSION_IHL(size) (((size)/4) + (4 << 4))
  62. B_START_PACKED
  63. struct ipv4_short {
  64. uint16_t v;
  65. } B_PACKED;
  66. B_END_PACKED
  67. static uint16_t ipv4_checksum (uint8_t *ip_hdr, uint16_t len)
  68. {
  69. ASSERT(len % 2 == 0)
  70. struct ipv4_short *s = (struct ipv4_short *)ip_hdr;
  71. uint32_t t = 0;
  72. for (uint16_t i = 0; i < len / 2; i++) {
  73. t += ntoh16(s[i].v);
  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. // check base header
  84. if (data_len < sizeof(struct ipv4_header)) {
  85. return 0;
  86. }
  87. struct ipv4_header *header = (struct ipv4_header *)data;
  88. // check version
  89. if (IPV4_GET_VERSION(*header) != 4) {
  90. return 0;
  91. }
  92. // check options
  93. uint16_t header_len = IPV4_GET_IHL(*header) * 4;
  94. if (header_len < sizeof(struct ipv4_header)) {
  95. return 0;
  96. }
  97. if (header_len > data_len) {
  98. return 0;
  99. }
  100. // check total length
  101. uint16_t total_length = ntoh16(header->total_length);
  102. if (total_length < header_len) {
  103. return 0;
  104. }
  105. if (total_length > data_len) {
  106. return 0;
  107. }
  108. // check checksum
  109. uint16_t checksum_in_packet = header->checksum;
  110. header->checksum = hton16(0);
  111. uint16_t checksum_computed = ipv4_checksum(data, header_len);
  112. header->checksum = checksum_in_packet;
  113. if (checksum_in_packet != checksum_computed) {
  114. return 0;
  115. }
  116. *out_header = header;
  117. *out_payload = data + header_len;
  118. *out_payload_len = total_length - header_len;
  119. return 1;
  120. }
  121. #endif