ipv4_proto.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /**
  2. * @file ipv4_proto.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. *
  22. * @section DESCRIPTION
  23. *
  24. * Definitions for the IPv4 protocol.
  25. */
  26. #ifndef BADVPN_MISC_IPV4_PROTO_H
  27. #define BADVPN_MISC_IPV4_PROTO_H
  28. #include <stdint.h>
  29. #include <misc/debug.h>
  30. #include <misc/byteorder.h>
  31. #define IPV4_PROTOCOL_IGMP 2
  32. #define IPV4_PROTOCOL_UDP 17
  33. struct ipv4_header {
  34. uint8_t version4_ihl4;
  35. uint8_t ds;
  36. uint16_t total_length;
  37. //
  38. uint16_t identification;
  39. uint16_t flags3_fragmentoffset13;
  40. //
  41. uint8_t ttl;
  42. uint8_t protocol;
  43. uint16_t checksum;
  44. //
  45. uint32_t source_address;
  46. //
  47. uint32_t destination_address;
  48. } __attribute__((packed));
  49. #define IPV4_GET_VERSION(_header) (((_header).version4_ihl4&0xF0)>>4)
  50. #define IPV4_GET_IHL(_header) (((_header).version4_ihl4&0x0F)>>0)
  51. #define IPV4_MAKE_VERSION_IHL(size) (((size)/4) + (4 << 4))
  52. struct ipv4_short {
  53. uint16_t v;
  54. } __attribute__((packed));
  55. static uint16_t ipv4_checksum (uint8_t *ip_hdr, uint16_t len)
  56. {
  57. ASSERT(len % 2 == 0)
  58. struct ipv4_short *s = (void *)ip_hdr;
  59. uint32_t t = 0;
  60. for (uint16_t i = 0; i < len / 2; i++) {
  61. t += ntoh16(s[i].v);
  62. }
  63. while (t >> 16) {
  64. t = (t & 0xFFFF) + (t >> 16);
  65. }
  66. return hton16(~t);
  67. }
  68. static int ipv4_check (uint8_t *data, int data_len, struct ipv4_header **out_header, uint8_t **out_payload, int *out_payload_len)
  69. {
  70. ASSERT(data_len >= 0)
  71. // check base header
  72. if (data_len < sizeof(struct ipv4_header)) {
  73. return 0;
  74. }
  75. struct ipv4_header *header = (struct ipv4_header *)data;
  76. // check version
  77. if (IPV4_GET_VERSION(*header) != 4) {
  78. return 0;
  79. }
  80. // check options
  81. uint16_t header_len = IPV4_GET_IHL(*header) * 4;
  82. if (header_len < sizeof(struct ipv4_header)) {
  83. return 0;
  84. }
  85. if (header_len > data_len) {
  86. return 0;
  87. }
  88. // check total length
  89. uint16_t total_length = ntoh16(header->total_length);
  90. if (total_length < header_len) {
  91. return 0;
  92. }
  93. if (total_length > data_len) {
  94. return 0;
  95. }
  96. // check checksum
  97. uint16_t checksum_in_packet = header->checksum;
  98. header->checksum = hton16(0);
  99. uint16_t checksum_computed = ipv4_checksum(data, header_len);
  100. header->checksum = checksum_in_packet;
  101. if (checksum_in_packet != checksum_computed) {
  102. return 0;
  103. }
  104. *out_header = header;
  105. *out_payload = data + header_len;
  106. *out_payload_len = total_length - header_len;
  107. return 1;
  108. }
  109. #endif