[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) \
|
---|
| 66 | ((((header)->fragment_offset_high << 8) + \
|
---|
| 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) \
|
---|
| 85 | ((header)->header_length * 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 {
|
---|
[21580dd] | 145 | #ifdef ARCH_IS_BIG_ENDIAN
|
---|
[4a3b501] | 146 | uint8_t version : 4;
|
---|
| 147 | uint8_t header_length : 4;
|
---|
[21580dd] | 148 | #else
|
---|
[4a3b501] | 149 | uint8_t header_length : 4;
|
---|
| 150 | uint8_t version : 4;
|
---|
[21580dd] | 151 | #endif
|
---|
[4a3b501] | 152 |
|
---|
[aadf01e] | 153 | uint8_t tos;
|
---|
| 154 | uint16_t total_length;
|
---|
| 155 | uint16_t identification;
|
---|
[4a3b501] | 156 |
|
---|
[21580dd] | 157 | #ifdef ARCH_IS_BIG_ENDIAN
|
---|
[4a3b501] | 158 | uint8_t flags : 3;
|
---|
| 159 | uint8_t fragment_offset_high : 5;
|
---|
[21580dd] | 160 | #else
|
---|
[4a3b501] | 161 | uint8_t fragment_offset_high : 5;
|
---|
| 162 | uint8_t flags : 3;
|
---|
[21580dd] | 163 | #endif
|
---|
[4a3b501] | 164 |
|
---|
[aadf01e] | 165 | uint8_t fragment_offset_low;
|
---|
| 166 | uint8_t ttl;
|
---|
| 167 | uint8_t protocol;
|
---|
| 168 | uint16_t header_checksum;
|
---|
| 169 | uint32_t source_address;
|
---|
| 170 | uint32_t destination_address;
|
---|
[21580dd] | 171 | } __attribute__ ((packed));
|
---|
| 172 |
|
---|
| 173 | /** Internet option header.
|
---|
[4a3b501] | 174 | *
|
---|
| 175 | * Only type field is always valid.
|
---|
| 176 | * Other fields' validity depends on the option type.
|
---|
[21580dd] | 177 | */
|
---|
[4a3b501] | 178 | struct ip_option {
|
---|
[aadf01e] | 179 | uint8_t type;
|
---|
| 180 | uint8_t length;
|
---|
| 181 | uint8_t pointer;
|
---|
[4a3b501] | 182 |
|
---|
[21580dd] | 183 | #ifdef ARCH_IS_BIG_ENDIAN
|
---|
[4a3b501] | 184 | uint8_t overflow : 4;
|
---|
| 185 | uint8_t flags : 4;
|
---|
[21580dd] | 186 | #else
|
---|
[4a3b501] | 187 | uint8_t flags : 4;
|
---|
| 188 | uint8_t overflow : 4;
|
---|
[21580dd] | 189 | #endif
|
---|
| 190 | } __attribute__ ((packed));
|
---|
| 191 |
|
---|
[4a3b501] | 192 | /** Internet version 4 pseudo header. */
|
---|
| 193 | struct ipv4_pseudo_header {
|
---|
[aadf01e] | 194 | uint32_t source_address;
|
---|
| 195 | uint32_t destination_address;
|
---|
| 196 | uint8_t reserved;
|
---|
| 197 | uint8_t protocol;
|
---|
| 198 | uint16_t data_length;
|
---|
[21580dd] | 199 | } __attribute__ ((packed));
|
---|
| 200 |
|
---|
| 201 | #endif
|
---|
| 202 |
|
---|
| 203 | /** @}
|
---|
| 204 | */
|
---|