| 1 | /* | 
|---|
| 2 | * Copyright (c) 2009 Lukas Mejdrech | 
|---|
| 3 | * All rights reserved. | 
|---|
| 4 | * | 
|---|
| 5 | * Redistribution and use in source and binary forms, with or without | 
|---|
| 6 | * modification, are permitted provided that the following conditions | 
|---|
| 7 | * are met: | 
|---|
| 8 | * | 
|---|
| 9 | * - Redistributions of source code must retain the above copyright | 
|---|
| 10 | *   notice, this list of conditions and the following disclaimer. | 
|---|
| 11 | * - 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 | * - The name of the author may not be used to endorse or promote products | 
|---|
| 15 | *   derived from this software without specific prior written permission. | 
|---|
| 16 | * | 
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | 
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | 
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | 
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | 
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | 
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | 
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 
|---|
| 27 | */ | 
|---|
| 28 |  | 
|---|
| 29 | /** @addtogroup libnet | 
|---|
| 30 | * @{ | 
|---|
| 31 | */ | 
|---|
| 32 |  | 
|---|
| 33 | /** @file | 
|---|
| 34 | * IP header and options definitions. | 
|---|
| 35 | * Based on the RFC 791. | 
|---|
| 36 | */ | 
|---|
| 37 |  | 
|---|
| 38 | #ifndef LIBNET_IP_HEADER_H_ | 
|---|
| 39 | #define LIBNET_IP_HEADER_H_ | 
|---|
| 40 |  | 
|---|
| 41 | #include <byteorder.h> | 
|---|
| 42 | #include <sys/types.h> | 
|---|
| 43 |  | 
|---|
| 44 | /** Returns the fragment offest high bits. | 
|---|
| 45 | * @param[in] length The prefixed data total length. | 
|---|
| 46 | */ | 
|---|
| 47 | #define IP_COMPUTE_FRAGMENT_OFFSET_HIGH(length) \ | 
|---|
| 48 | ((((length) / 8U) & 0x1f00) >> 8) | 
|---|
| 49 |  | 
|---|
| 50 | /** Returns the fragment offest low bits. | 
|---|
| 51 | * @param[in] length The prefixed data total length. | 
|---|
| 52 | */ | 
|---|
| 53 | #define IP_COMPUTE_FRAGMENT_OFFSET_LOW(length) \ | 
|---|
| 54 | (((length) / 8U) & 0xff) | 
|---|
| 55 |  | 
|---|
| 56 | /** Returns the IP header length. | 
|---|
| 57 | * @param[in] length The IP header length in bytes. | 
|---|
| 58 | */ | 
|---|
| 59 | #define IP_COMPUTE_HEADER_LENGTH(length) \ | 
|---|
| 60 | ((uint8_t) ((length) / 4U)) | 
|---|
| 61 |  | 
|---|
| 62 | /** Returns the fragment offest. | 
|---|
| 63 | * @param[in] header The IP packet header. | 
|---|
| 64 | */ | 
|---|
| 65 | #define IP_FRAGMENT_OFFSET(header) \ | 
|---|
| 66 | (((GET_IP_HEADER_FRAGMENT_OFFSET_HIGH(header) << 8) + \ | 
|---|
| 67 | (header)->fragment_offset_low) * 8U) | 
|---|
| 68 |  | 
|---|
| 69 | /** Returns the IP packet header checksum. | 
|---|
| 70 | *  @param[in] header The IP packet header. | 
|---|
| 71 | */ | 
|---|
| 72 | #define IP_HEADER_CHECKSUM(header) \ | 
|---|
| 73 | (htons(ip_checksum((uint8_t *) (header), IP_HEADER_LENGTH(header)))) | 
|---|
| 74 |  | 
|---|
| 75 | /** Returns the actual IP packet data length. | 
|---|
| 76 | * @param[in] header The IP packet header. | 
|---|
| 77 | */ | 
|---|
| 78 | #define IP_HEADER_DATA_LENGTH(header) \ | 
|---|
| 79 | (IP_TOTAL_LENGTH(header) - IP_HEADER_LENGTH(header)) | 
|---|
| 80 |  | 
|---|
| 81 | /** Returns the actual IP header length in bytes. | 
|---|
| 82 | * @param[in] header The IP packet header. | 
|---|
| 83 | */ | 
|---|
| 84 | #define IP_HEADER_LENGTH(header) \ | 
|---|
| 85 | (GET_IP_HEADER_LENGTH(header) * 4U) | 
|---|
| 86 |  | 
|---|
| 87 | /** Returns the actual IP packet total length. | 
|---|
| 88 | * @param[in] header The IP packet header. | 
|---|
| 89 | */ | 
|---|
| 90 | #define IP_TOTAL_LENGTH(header) \ | 
|---|
| 91 | ntohs((header)->total_length) | 
|---|
| 92 |  | 
|---|
| 93 | /** @name IP flags definitions */ | 
|---|
| 94 | /*@{*/ | 
|---|
| 95 |  | 
|---|
| 96 | /** Fragment flag field shift. */ | 
|---|
| 97 | #define IPFLAG_FRAGMENT_SHIFT   1 | 
|---|
| 98 |  | 
|---|
| 99 | /** Fragmented flag field shift. */ | 
|---|
| 100 | #define IPFLAG_FRAGMENTED_SHIFT 0 | 
|---|
| 101 |  | 
|---|
| 102 | /** Don't fragment flag value. | 
|---|
| 103 | * Permits the packet fragmentation. | 
|---|
| 104 | */ | 
|---|
| 105 | #define IPFLAG_DONT_FRAGMENT    (0x1 << IPFLAG_FRAGMENT_SHIFT) | 
|---|
| 106 |  | 
|---|
| 107 | /** Last fragment flag value. | 
|---|
| 108 | * Indicates the last packet fragment. | 
|---|
| 109 | */ | 
|---|
| 110 | #define IPFLAG_LAST_FRAGMENT    (0x0 << IPFLAG_FRAGMENTED_SHIFT) | 
|---|
| 111 |  | 
|---|
| 112 | /** May fragment flag value. | 
|---|
| 113 | * Allows the packet fragmentation. | 
|---|
| 114 | */ | 
|---|
| 115 | #define IPFLAG_MAY_FRAGMENT     (0x0 << IPFLAG_FRAGMENT_SHIFT) | 
|---|
| 116 |  | 
|---|
| 117 | /** More fragments flag value. | 
|---|
| 118 | * Indicates that more packet fragments follow. | 
|---|
| 119 | */ | 
|---|
| 120 | #define IPFLAG_MORE_FRAGMENTS   (0x1 << IPFLAG_FRAGMENTED_SHIFT) | 
|---|
| 121 |  | 
|---|
| 122 | /*@}*/ | 
|---|
| 123 |  | 
|---|
| 124 | /** Type definition of the internet header. | 
|---|
| 125 | * @see ip_header | 
|---|
| 126 | */ | 
|---|
| 127 | typedef struct ip_header ip_header_t; | 
|---|
| 128 |  | 
|---|
| 129 | /** Type definition of the internet option header. | 
|---|
| 130 | * @see ip_header | 
|---|
| 131 | */ | 
|---|
| 132 | typedef struct ip_option ip_option_t; | 
|---|
| 133 |  | 
|---|
| 134 | /** Type definition of the internet version 4 pseudo header. | 
|---|
| 135 | * @see ipv4_pseudo_header | 
|---|
| 136 | */ | 
|---|
| 137 | typedef struct ipv4_pseudo_header ipv4_pseudo_header_t; | 
|---|
| 138 |  | 
|---|
| 139 | /** Internet header. | 
|---|
| 140 | * | 
|---|
| 141 | * The variable options should be included after the header itself and | 
|---|
| 142 | * indicated by the increased header length value. | 
|---|
| 143 | */ | 
|---|
| 144 | struct ip_header { | 
|---|
| 145 | uint8_t vhl; /* version, header_length */ | 
|---|
| 146 |  | 
|---|
| 147 | #define GET_IP_HEADER_VERSION(header) \ | 
|---|
| 148 | (((header)->vhl & 0xf0) >> 4) | 
|---|
| 149 | #define SET_IP_HEADER_VERSION(header, version) \ | 
|---|
| 150 | ((header)->vhl = \ | 
|---|
| 151 | ((version & 0x0f) << 4) | ((header)->vhl & 0x0f)) | 
|---|
| 152 |  | 
|---|
| 153 | #define GET_IP_HEADER_LENGTH(header) \ | 
|---|
| 154 | ((header)->vhl & 0x0f) | 
|---|
| 155 | #define SET_IP_HEADER_LENGTH(header, length) \ | 
|---|
| 156 | ((header)->vhl = \ | 
|---|
| 157 | (length & 0x0f) | ((header)->vhl & 0xf0)) | 
|---|
| 158 |  | 
|---|
| 159 | uint8_t tos; | 
|---|
| 160 | uint16_t total_length; | 
|---|
| 161 | uint16_t identification; | 
|---|
| 162 |  | 
|---|
| 163 | uint8_t ffoh; /* flags, fragment_offset_high */ | 
|---|
| 164 |  | 
|---|
| 165 | #define GET_IP_HEADER_FLAGS(header) \ | 
|---|
| 166 | (((header)->ffoh & 0xe0) >> 5) | 
|---|
| 167 | #define SET_IP_HEADER_FLAGS(header, flags) \ | 
|---|
| 168 | ((header)->ffoh = \ | 
|---|
| 169 | ((flags & 0x07) << 5) | ((header)->ffoh & 0x1f)) | 
|---|
| 170 |  | 
|---|
| 171 | #define GET_IP_HEADER_FRAGMENT_OFFSET_HIGH(header) \ | 
|---|
| 172 | ((header)->ffoh & 0x1f) | 
|---|
| 173 | #define SET_IP_HEADER_FRAGMENT_OFFSET_HIGH(header, fragment_offset_high) \ | 
|---|
| 174 | ((header)->ffoh = \ | 
|---|
| 175 | (fragment_offset_high & 0x1f) | ((header)->ffoh & 0xe0)) | 
|---|
| 176 |  | 
|---|
| 177 | uint8_t fragment_offset_low; | 
|---|
| 178 | uint8_t ttl; | 
|---|
| 179 | uint8_t protocol; | 
|---|
| 180 | uint16_t header_checksum; | 
|---|
| 181 | uint32_t source_address; | 
|---|
| 182 | uint32_t destination_address; | 
|---|
| 183 | } __attribute__ ((packed)); | 
|---|
| 184 |  | 
|---|
| 185 | /** Internet option header. | 
|---|
| 186 | * | 
|---|
| 187 | * Only type field is always valid. | 
|---|
| 188 | * Other fields' validity depends on the option type. | 
|---|
| 189 | */ | 
|---|
| 190 | struct ip_option { | 
|---|
| 191 | uint8_t type; | 
|---|
| 192 | uint8_t length; | 
|---|
| 193 | uint8_t pointer; | 
|---|
| 194 |  | 
|---|
| 195 | uint8_t of; /* overflow, flags */ | 
|---|
| 196 |  | 
|---|
| 197 | #define GET_IP_OPTION_OVERFLOW(option) \ | 
|---|
| 198 | (((option)->of & 0xf0) >> 4) | 
|---|
| 199 | #define SET_IP_OPTION_OVERFLOW(option, overflow) \ | 
|---|
| 200 | ((option)->of = \ | 
|---|
| 201 | ((overflow & 0x0f) << 4) | ((option)->of & 0x0f)) | 
|---|
| 202 |  | 
|---|
| 203 | #define GET_IP_OPTION_FLAGS(option) \ | 
|---|
| 204 | ((option)->of & 0x0f) | 
|---|
| 205 | #define SET_IP_OPTION_FLAGS(option, flags) \ | 
|---|
| 206 | ((option)->of = \ | 
|---|
| 207 | (flags & 0x0f) | ((option)->of & 0xf0)) | 
|---|
| 208 |  | 
|---|
| 209 | } __attribute__ ((packed)); | 
|---|
| 210 |  | 
|---|
| 211 | /** Internet version 4 pseudo header. */ | 
|---|
| 212 | struct ipv4_pseudo_header { | 
|---|
| 213 | uint32_t source_address; | 
|---|
| 214 | uint32_t destination_address; | 
|---|
| 215 | uint8_t reserved; | 
|---|
| 216 | uint8_t protocol; | 
|---|
| 217 | uint16_t data_length; | 
|---|
| 218 | } __attribute__ ((packed)); | 
|---|
| 219 |  | 
|---|
| 220 | #endif | 
|---|
| 221 |  | 
|---|
| 222 | /** @} | 
|---|
| 223 | */ | 
|---|