[21580dd] | 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 |
|
---|
[4a3b501] | 29 | /** @addtogroup libnet
|
---|
| 30 | * @{
|
---|
[21580dd] | 31 | */
|
---|
| 32 |
|
---|
| 33 | /** @file
|
---|
[4a3b501] | 34 | * IP header and options definitions.
|
---|
| 35 | * Based on the RFC 791.
|
---|
[21580dd] | 36 | */
|
---|
| 37 |
|
---|
[4a3b501] | 38 | #ifndef LIBNET_IP_HEADER_H_
|
---|
| 39 | #define LIBNET_IP_HEADER_H_
|
---|
[21580dd] | 40 |
|
---|
| 41 | #include <byteorder.h>
|
---|
| 42 | #include <sys/types.h>
|
---|
| 43 |
|
---|
[a64c64d] | 44 | /** Returns the fragment offest high bits.
|
---|
[4a3b501] | 45 | * @param[in] length The prefixed data total length.
|
---|
[21580dd] | 46 | */
|
---|
[4a3b501] | 47 | #define IP_COMPUTE_FRAGMENT_OFFSET_HIGH(length) \
|
---|
| 48 | ((((length) / 8U) & 0x1f00) >> 8)
|
---|
[a64c64d] | 49 |
|
---|
| 50 | /** Returns the fragment offest low bits.
|
---|
[4a3b501] | 51 | * @param[in] length The prefixed data total length.
|
---|
[a64c64d] | 52 | */
|
---|
[4a3b501] | 53 | #define IP_COMPUTE_FRAGMENT_OFFSET_LOW(length) \
|
---|
| 54 | (((length) / 8U) & 0xff)
|
---|
[21580dd] | 55 |
|
---|
| 56 | /** Returns the IP header length.
|
---|
[4a3b501] | 57 | * @param[in] length The IP header length in bytes.
|
---|
[21580dd] | 58 | */
|
---|
[4a3b501] | 59 | #define IP_COMPUTE_HEADER_LENGTH(length) \
|
---|
| 60 | ((uint8_t) ((length) / 4U))
|
---|
[21580dd] | 61 |
|
---|
[a64c64d] | 62 | /** Returns the fragment offest.
|
---|
[4a3b501] | 63 | * @param[in] header The IP packet header.
|
---|
[21580dd] | 64 | */
|
---|
[4a3b501] | 65 | #define IP_FRAGMENT_OFFSET(header) \
|
---|
[7bf12387] | 66 | (((GET_IP_HEADER_FRAGMENT_OFFSET_HIGH(header) << 8) + \
|
---|
[4a3b501] | 67 | (header)->fragment_offset_low) * 8U)
|
---|
[a64c64d] | 68 |
|
---|
| 69 | /** Returns the IP packet header checksum.
|
---|
| 70 | * @param[in] header The IP packet header.
|
---|
| 71 | */
|
---|
[4a3b501] | 72 | #define IP_HEADER_CHECKSUM(header) \
|
---|
| 73 | (htons(ip_checksum((uint8_t *) (header), IP_HEADER_LENGTH(header))))
|
---|
[21580dd] | 74 |
|
---|
| 75 | /** Returns the actual IP packet data length.
|
---|
[4a3b501] | 76 | * @param[in] header The IP packet header.
|
---|
[21580dd] | 77 | */
|
---|
[4a3b501] | 78 | #define IP_HEADER_DATA_LENGTH(header) \
|
---|
| 79 | (IP_TOTAL_LENGTH(header) - IP_HEADER_LENGTH(header))
|
---|
[21580dd] | 80 |
|
---|
[a64c64d] | 81 | /** Returns the actual IP header length in bytes.
|
---|
[4a3b501] | 82 | * @param[in] header The IP packet header.
|
---|
[21580dd] | 83 | */
|
---|
[4a3b501] | 84 | #define IP_HEADER_LENGTH(header) \
|
---|
[7bf12387] | 85 | (GET_IP_HEADER_LENGTH(header) * 4U)
|
---|
[21580dd] | 86 |
|
---|
[a64c64d] | 87 | /** Returns the actual IP packet total length.
|
---|
[4a3b501] | 88 | * @param[in] header The IP packet header.
|
---|
[21580dd] | 89 | */
|
---|
[4a3b501] | 90 | #define IP_TOTAL_LENGTH(header) \
|
---|
| 91 | ntohs((header)->total_length)
|
---|
[21580dd] | 92 |
|
---|
[4a3b501] | 93 | /** @name IP flags definitions */
|
---|
[a64c64d] | 94 | /*@{*/
|
---|
[21580dd] | 95 |
|
---|
[4a3b501] | 96 | /** Fragment flag field shift. */
|
---|
| 97 | #define IPFLAG_FRAGMENT_SHIFT 1
|
---|
[a64c64d] | 98 |
|
---|
[4a3b501] | 99 | /** Fragmented flag field shift. */
|
---|
| 100 | #define IPFLAG_FRAGMENTED_SHIFT 0
|
---|
[a64c64d] | 101 |
|
---|
| 102 | /** Don't fragment flag value.
|
---|
[4a3b501] | 103 | * Permits the packet fragmentation.
|
---|
[a64c64d] | 104 | */
|
---|
[4a3b501] | 105 | #define IPFLAG_DONT_FRAGMENT (0x1 << IPFLAG_FRAGMENT_SHIFT)
|
---|
[a64c64d] | 106 |
|
---|
| 107 | /** Last fragment flag value.
|
---|
[4a3b501] | 108 | * Indicates the last packet fragment.
|
---|
[a64c64d] | 109 | */
|
---|
[4a3b501] | 110 | #define IPFLAG_LAST_FRAGMENT (0x0 << IPFLAG_FRAGMENTED_SHIFT)
|
---|
[a64c64d] | 111 |
|
---|
| 112 | /** May fragment flag value.
|
---|
[4a3b501] | 113 | * Allows the packet fragmentation.
|
---|
[a64c64d] | 114 | */
|
---|
[4a3b501] | 115 | #define IPFLAG_MAY_FRAGMENT (0x0 << IPFLAG_FRAGMENT_SHIFT)
|
---|
[a64c64d] | 116 |
|
---|
| 117 | /** More fragments flag value.
|
---|
[4a3b501] | 118 | * Indicates that more packet fragments follow.
|
---|
[a64c64d] | 119 | */
|
---|
[4a3b501] | 120 | #define IPFLAG_MORE_FRAGMENTS (0x1 << IPFLAG_FRAGMENTED_SHIFT)
|
---|
[a64c64d] | 121 |
|
---|
| 122 | /*@}*/
|
---|
[21580dd] | 123 |
|
---|
| 124 | /** Type definition of the internet header.
|
---|
[4a3b501] | 125 | * @see ip_header
|
---|
[21580dd] | 126 | */
|
---|
[4a3b501] | 127 | typedef struct ip_header ip_header_t;
|
---|
[21580dd] | 128 |
|
---|
[a64c64d] | 129 | /** Type definition of the internet option header.
|
---|
[4a3b501] | 130 | * @see ip_header
|
---|
[a64c64d] | 131 | */
|
---|
[4a3b501] | 132 | typedef struct ip_option ip_option_t;
|
---|
[a64c64d] | 133 |
|
---|
| 134 | /** Type definition of the internet version 4 pseudo header.
|
---|
[4a3b501] | 135 | * @see ipv4_pseudo_header
|
---|
[a64c64d] | 136 | */
|
---|
[4a3b501] | 137 | typedef struct ipv4_pseudo_header ipv4_pseudo_header_t;
|
---|
[a64c64d] | 138 |
|
---|
[21580dd] | 139 | /** Internet header.
|
---|
[4a3b501] | 140 | *
|
---|
| 141 | * The variable options should be included after the header itself and
|
---|
| 142 | * indicated by the increased header length value.
|
---|
[21580dd] | 143 | */
|
---|
[4a3b501] | 144 | struct ip_header {
|
---|
[40ffda8] | 145 | uint8_t vhl; /* version, header_length */
|
---|
| 146 |
|
---|
[7bf12387] | 147 | #define GET_IP_HEADER_VERSION(header) \
|
---|
[40ffda8] | 148 | (((header)->vhl & 0xf0) >> 4)
|
---|
[7bf12387] | 149 | #define SET_IP_HEADER_VERSION(header, version) \
|
---|
[40ffda8] | 150 | ((header)->vhl = \
|
---|
| 151 | ((version & 0x0f) << 4) | ((header)->vhl & 0x0f))
|
---|
| 152 |
|
---|
[7bf12387] | 153 | #define GET_IP_HEADER_LENGTH(header) \
|
---|
[40ffda8] | 154 | ((header)->vhl & 0x0f)
|
---|
[7bf12387] | 155 | #define SET_IP_HEADER_LENGTH(header, length) \
|
---|
[40ffda8] | 156 | ((header)->vhl = \
|
---|
| 157 | (length & 0x0f) | ((header)->vhl & 0xf0))
|
---|
[4a3b501] | 158 |
|
---|
[aadf01e] | 159 | uint8_t tos;
|
---|
| 160 | uint16_t total_length;
|
---|
| 161 | uint16_t identification;
|
---|
[4a3b501] | 162 |
|
---|
[40ffda8] | 163 | uint8_t ffoh; /* flags, fragment_offset_high */
|
---|
| 164 |
|
---|
[7bf12387] | 165 | #define GET_IP_HEADER_FLAGS(header) \
|
---|
[40ffda8] | 166 | (((header)->ffoh & 0xe0) >> 5)
|
---|
[7bf12387] | 167 | #define SET_IP_HEADER_FLAGS(header, flags) \
|
---|
[40ffda8] | 168 | ((header)->ffoh = \
|
---|
| 169 | ((flags & 0x07) << 5) | ((header)->ffoh & 0x1f))
|
---|
| 170 |
|
---|
[7bf12387] | 171 | #define GET_IP_HEADER_FRAGMENT_OFFSET_HIGH(header) \
|
---|
[40ffda8] | 172 | ((header)->ffoh & 0x1f)
|
---|
[7bf12387] | 173 | #define SET_IP_HEADER_FRAGMENT_OFFSET_HIGH(header, fragment_offset_high) \
|
---|
[40ffda8] | 174 | ((header)->ffoh = \
|
---|
| 175 | (fragment_offset_high & 0x1f) | ((header)->ffoh & 0xe0))
|
---|
[4a3b501] | 176 |
|
---|
[aadf01e] | 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;
|
---|
[21580dd] | 183 | } __attribute__ ((packed));
|
---|
| 184 |
|
---|
| 185 | /** Internet option header.
|
---|
[4a3b501] | 186 | *
|
---|
| 187 | * Only type field is always valid.
|
---|
| 188 | * Other fields' validity depends on the option type.
|
---|
[21580dd] | 189 | */
|
---|
[4a3b501] | 190 | struct ip_option {
|
---|
[aadf01e] | 191 | uint8_t type;
|
---|
| 192 | uint8_t length;
|
---|
| 193 | uint8_t pointer;
|
---|
[4a3b501] | 194 |
|
---|
[40ffda8] | 195 | uint8_t of; /* overflow, flags */
|
---|
| 196 |
|
---|
[7bf12387] | 197 | #define GET_IP_OPTION_OVERFLOW(option) \
|
---|
[40ffda8] | 198 | (((option)->of & 0xf0) >> 4)
|
---|
[7bf12387] | 199 | #define SET_IP_OPTION_OVERFLOW(option, overflow) \
|
---|
[40ffda8] | 200 | ((option)->of = \
|
---|
| 201 | ((overflow & 0x0f) << 4) | ((option)->of & 0x0f))
|
---|
| 202 |
|
---|
[7bf12387] | 203 | #define GET_IP_OPTION_FLAGS(option) \
|
---|
[40ffda8] | 204 | ((option)->of & 0x0f)
|
---|
[7bf12387] | 205 | #define SET_IP_OPTION_FLAGS(option, flags) \
|
---|
[40ffda8] | 206 | ((option)->of = \
|
---|
| 207 | (flags & 0x0f) | ((option)->of & 0xf0))
|
---|
| 208 |
|
---|
[21580dd] | 209 | } __attribute__ ((packed));
|
---|
| 210 |
|
---|
[4a3b501] | 211 | /** Internet version 4 pseudo header. */
|
---|
| 212 | struct ipv4_pseudo_header {
|
---|
[aadf01e] | 213 | uint32_t source_address;
|
---|
| 214 | uint32_t destination_address;
|
---|
| 215 | uint8_t reserved;
|
---|
| 216 | uint8_t protocol;
|
---|
| 217 | uint16_t data_length;
|
---|
[21580dd] | 218 | } __attribute__ ((packed));
|
---|
| 219 |
|
---|
| 220 | #endif
|
---|
| 221 |
|
---|
| 222 | /** @}
|
---|
| 223 | */
|
---|