bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 1982, 1986, 1993 |
| 3 | * The Regents of the University of California. 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 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
aliguori | 2f5f899 | 2009-01-26 19:37:41 +0000 | [diff] [blame] | 13 | * 3. Neither the name of the University nor the names of its contributors |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 14 | * may be used to endorse or promote products derived from this software |
| 15 | * without specific prior written permission. |
| 16 | * |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 27 | * SUCH DAMAGE. |
| 28 | * |
| 29 | * @(#)ip.h 8.1 (Berkeley) 6/10/93 |
| 30 | * ip.h,v 1.3 1994/08/21 05:27:30 paul Exp |
| 31 | */ |
| 32 | |
Markus Armbruster | 2a6a407 | 2016-06-29 13:47:03 +0200 | [diff] [blame] | 33 | #ifndef IP_H |
| 34 | #define IP_H |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 35 | |
Marc-André Lureau | e429f29 | 2018-11-14 16:36:26 +0400 | [diff] [blame] | 36 | #include <glib.h> |
| 37 | |
| 38 | #if G_BYTE_ORDER == G_BIG_ENDIAN |
Andreas Färber | 9b24d8e | 2012-05-28 19:34:20 +0200 | [diff] [blame] | 39 | # undef NTOHL |
| 40 | # undef NTOHS |
| 41 | # undef HTONL |
| 42 | # undef HTONS |
| 43 | # define NTOHL(d) |
| 44 | # define NTOHS(d) |
| 45 | # define HTONL(d) |
| 46 | # define HTONS(d) |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 47 | #else |
| 48 | # ifndef NTOHL |
| 49 | # define NTOHL(d) ((d) = ntohl((d))) |
| 50 | # endif |
| 51 | # ifndef NTOHS |
Stefan Weil | b6dce92 | 2010-07-22 22:15:23 +0200 | [diff] [blame] | 52 | # define NTOHS(d) ((d) = ntohs((uint16_t)(d))) |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 53 | # endif |
| 54 | # ifndef HTONL |
| 55 | # define HTONL(d) ((d) = htonl((d))) |
| 56 | # endif |
| 57 | # ifndef HTONS |
Stefan Weil | b6dce92 | 2010-07-22 22:15:23 +0200 | [diff] [blame] | 58 | # define HTONS(d) ((d) = htons((uint16_t)(d))) |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 59 | # endif |
| 60 | #endif |
| 61 | |
Stefan Weil | b6dce92 | 2010-07-22 22:15:23 +0200 | [diff] [blame] | 62 | typedef uint32_t n_long; /* long as received from the net */ |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 63 | |
| 64 | /* |
| 65 | * Definitions for internet protocol version 4. |
| 66 | * Per RFC 791, September 1981. |
| 67 | */ |
| 68 | #define IPVERSION 4 |
| 69 | |
| 70 | /* |
| 71 | * Structure of an internet header, naked of options. |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 72 | */ |
| 73 | struct ip { |
Marc-André Lureau | e429f29 | 2018-11-14 16:36:26 +0400 | [diff] [blame] | 74 | #if G_BYTE_ORDER == G_BIG_ENDIAN |
Jan Kiszka | 1b930bf | 2011-08-06 14:23:29 +0200 | [diff] [blame] | 75 | uint8_t ip_v:4, /* version */ |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 76 | ip_hl:4; /* header length */ |
| 77 | #else |
Jan Kiszka | 1b930bf | 2011-08-06 14:23:29 +0200 | [diff] [blame] | 78 | uint8_t ip_hl:4, /* header length */ |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 79 | ip_v:4; /* version */ |
| 80 | #endif |
Stefan Weil | b6dce92 | 2010-07-22 22:15:23 +0200 | [diff] [blame] | 81 | uint8_t ip_tos; /* type of service */ |
| 82 | uint16_t ip_len; /* total length */ |
| 83 | uint16_t ip_id; /* identification */ |
| 84 | uint16_t ip_off; /* fragment offset field */ |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 85 | #define IP_DF 0x4000 /* don't fragment flag */ |
| 86 | #define IP_MF 0x2000 /* more fragments flag */ |
| 87 | #define IP_OFFMASK 0x1fff /* mask for fragmenting bits */ |
Stefan Weil | b6dce92 | 2010-07-22 22:15:23 +0200 | [diff] [blame] | 88 | uint8_t ip_ttl; /* time to live */ |
| 89 | uint8_t ip_p; /* protocol */ |
| 90 | uint16_t ip_sum; /* checksum */ |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 91 | struct in_addr ip_src,ip_dst; /* source and dest address */ |
Stefan Weil | 541dc0d | 2011-08-31 12:38:01 +0200 | [diff] [blame] | 92 | } QEMU_PACKED; |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 93 | |
| 94 | #define IP_MAXPACKET 65535 /* maximum packet size */ |
| 95 | |
| 96 | /* |
| 97 | * Definitions for IP type of service (ip_tos) |
| 98 | */ |
| 99 | #define IPTOS_LOWDELAY 0x10 |
| 100 | #define IPTOS_THROUGHPUT 0x08 |
| 101 | #define IPTOS_RELIABILITY 0x04 |
| 102 | |
| 103 | /* |
| 104 | * Definitions for options. |
| 105 | */ |
| 106 | #define IPOPT_COPIED(o) ((o)&0x80) |
| 107 | #define IPOPT_CLASS(o) ((o)&0x60) |
| 108 | #define IPOPT_NUMBER(o) ((o)&0x1f) |
| 109 | |
| 110 | #define IPOPT_CONTROL 0x00 |
| 111 | #define IPOPT_RESERVED1 0x20 |
| 112 | #define IPOPT_DEBMEAS 0x40 |
| 113 | #define IPOPT_RESERVED2 0x60 |
| 114 | |
| 115 | #define IPOPT_EOL 0 /* end of option list */ |
| 116 | #define IPOPT_NOP 1 /* no operation */ |
| 117 | |
| 118 | #define IPOPT_RR 7 /* record packet route */ |
| 119 | #define IPOPT_TS 68 /* timestamp */ |
| 120 | #define IPOPT_SECURITY 130 /* provide s,c,h,tcc */ |
| 121 | #define IPOPT_LSRR 131 /* loose source route */ |
| 122 | #define IPOPT_SATID 136 /* satnet id */ |
| 123 | #define IPOPT_SSRR 137 /* strict source route */ |
| 124 | |
| 125 | /* |
| 126 | * Offsets to fields in options other than EOL and NOP. |
| 127 | */ |
| 128 | #define IPOPT_OPTVAL 0 /* option ID */ |
| 129 | #define IPOPT_OLEN 1 /* option length */ |
| 130 | #define IPOPT_OFFSET 2 /* offset within option */ |
| 131 | #define IPOPT_MINOFF 4 /* min value of above */ |
| 132 | |
| 133 | /* |
| 134 | * Time stamp option structure. |
| 135 | */ |
| 136 | struct ip_timestamp { |
Stefan Weil | b6dce92 | 2010-07-22 22:15:23 +0200 | [diff] [blame] | 137 | uint8_t ipt_code; /* IPOPT_TS */ |
| 138 | uint8_t ipt_len; /* size of structure (variable) */ |
| 139 | uint8_t ipt_ptr; /* index of current entry */ |
Marc-André Lureau | e429f29 | 2018-11-14 16:36:26 +0400 | [diff] [blame] | 140 | #if G_BYTE_ORDER == G_BIG_ENDIAN |
Jan Kiszka | 1b930bf | 2011-08-06 14:23:29 +0200 | [diff] [blame] | 141 | uint8_t ipt_oflw:4, /* overflow counter */ |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 142 | ipt_flg:4; /* flags, see below */ |
| 143 | #else |
Jan Kiszka | 1b930bf | 2011-08-06 14:23:29 +0200 | [diff] [blame] | 144 | uint8_t ipt_flg:4, /* flags, see below */ |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 145 | ipt_oflw:4; /* overflow counter */ |
| 146 | #endif |
| 147 | union ipt_timestamp { |
| 148 | n_long ipt_time[1]; |
| 149 | struct ipt_ta { |
| 150 | struct in_addr ipt_addr; |
| 151 | n_long ipt_time; |
| 152 | } ipt_ta[1]; |
| 153 | } ipt_timestamp; |
Stefan Weil | 541dc0d | 2011-08-31 12:38:01 +0200 | [diff] [blame] | 154 | } QEMU_PACKED; |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 155 | |
| 156 | /* flag bits for ipt_flg */ |
| 157 | #define IPOPT_TS_TSONLY 0 /* timestamps only */ |
| 158 | #define IPOPT_TS_TSANDADDR 1 /* timestamps and addresses */ |
| 159 | #define IPOPT_TS_PRESPEC 3 /* specified modules only */ |
| 160 | |
| 161 | /* bits for security (not byte swapped) */ |
| 162 | #define IPOPT_SECUR_UNCLASS 0x0000 |
| 163 | #define IPOPT_SECUR_CONFID 0xf135 |
| 164 | #define IPOPT_SECUR_EFTO 0x789a |
| 165 | #define IPOPT_SECUR_MMMM 0xbc4d |
| 166 | #define IPOPT_SECUR_RESTR 0xaf13 |
| 167 | #define IPOPT_SECUR_SECRET 0xd788 |
| 168 | #define IPOPT_SECUR_TOPSECRET 0x6bc5 |
| 169 | |
| 170 | /* |
| 171 | * Internet implementation parameters. |
| 172 | */ |
| 173 | #define MAXTTL 255 /* maximum time to live (seconds) */ |
| 174 | #define IPDEFTTL 64 /* default ttl, from RFC 1340 */ |
| 175 | #define IPFRAGTTL 60 /* time to live for frags, slowhz */ |
| 176 | #define IPTTLDEC 1 /* subtracted when forwarding */ |
| 177 | |
| 178 | #define IP_MSS 576 /* default maximum segment size */ |
| 179 | |
Marc-André Lureau | 0826c24 | 2018-11-14 16:36:27 +0400 | [diff] [blame] | 180 | #if GLIB_SIZEOF_VOID_P == 4 |
blueswir1 | 429d0a3 | 2009-01-13 19:48:42 +0000 | [diff] [blame] | 181 | struct mbuf_ptr { |
| 182 | struct mbuf *mptr; |
| 183 | uint32_t dummy; |
Stefan Weil | 541dc0d | 2011-08-31 12:38:01 +0200 | [diff] [blame] | 184 | } QEMU_PACKED; |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 185 | #else |
blueswir1 | 429d0a3 | 2009-01-13 19:48:42 +0000 | [diff] [blame] | 186 | struct mbuf_ptr { |
| 187 | struct mbuf *mptr; |
Stefan Weil | 541dc0d | 2011-08-31 12:38:01 +0200 | [diff] [blame] | 188 | } QEMU_PACKED; |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 189 | #endif |
blueswir1 | 429d0a3 | 2009-01-13 19:48:42 +0000 | [diff] [blame] | 190 | struct qlink { |
| 191 | void *next, *prev; |
| 192 | }; |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 193 | |
| 194 | /* |
| 195 | * Overlay for ip header used by other protocols (tcp, udp). |
| 196 | */ |
| 197 | struct ipovly { |
blueswir1 | 429d0a3 | 2009-01-13 19:48:42 +0000 | [diff] [blame] | 198 | struct mbuf_ptr ih_mbuf; /* backpointer to mbuf */ |
Stefan Weil | b6dce92 | 2010-07-22 22:15:23 +0200 | [diff] [blame] | 199 | uint8_t ih_x1; /* (unused) */ |
| 200 | uint8_t ih_pr; /* protocol */ |
| 201 | uint16_t ih_len; /* protocol length */ |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 202 | struct in_addr ih_src; /* source internet address */ |
| 203 | struct in_addr ih_dst; /* destination internet address */ |
Stefan Weil | 541dc0d | 2011-08-31 12:38:01 +0200 | [diff] [blame] | 204 | } QEMU_PACKED; |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 205 | |
| 206 | /* |
| 207 | * Ip reassembly queue structure. Each fragment |
| 208 | * being reassembled is attached to one of these structures. |
| 209 | * They are timed out after ipq_ttl drops to 0, and may also |
| 210 | * be reclaimed if memory becomes tight. |
| 211 | * size 28 bytes |
| 212 | */ |
| 213 | struct ipq { |
blueswir1 | 429d0a3 | 2009-01-13 19:48:42 +0000 | [diff] [blame] | 214 | struct qlink frag_link; /* to ip headers of fragments */ |
| 215 | struct qlink ip_link; /* to other reass headers */ |
Stefan Weil | b6dce92 | 2010-07-22 22:15:23 +0200 | [diff] [blame] | 216 | uint8_t ipq_ttl; /* time for reass q to live */ |
| 217 | uint8_t ipq_p; /* protocol of this fragment */ |
| 218 | uint16_t ipq_id; /* sequence id for reassembly */ |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 219 | struct in_addr ipq_src,ipq_dst; |
Stefan Weil | 541dc0d | 2011-08-31 12:38:01 +0200 | [diff] [blame] | 220 | } QEMU_PACKED; |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 221 | |
| 222 | /* |
| 223 | * Ip header, when holding a fragment. |
| 224 | * |
blueswir1 | 429d0a3 | 2009-01-13 19:48:42 +0000 | [diff] [blame] | 225 | * Note: ipf_link must be at same offset as frag_link above |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 226 | */ |
| 227 | struct ipasfrag { |
blueswir1 | 429d0a3 | 2009-01-13 19:48:42 +0000 | [diff] [blame] | 228 | struct qlink ipf_link; |
| 229 | struct ip ipf_ip; |
Stefan Weil | 541dc0d | 2011-08-31 12:38:01 +0200 | [diff] [blame] | 230 | } QEMU_PACKED; |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 231 | |
blueswir1 | 429d0a3 | 2009-01-13 19:48:42 +0000 | [diff] [blame] | 232 | #define ipf_off ipf_ip.ip_off |
| 233 | #define ipf_tos ipf_ip.ip_tos |
| 234 | #define ipf_len ipf_ip.ip_len |
| 235 | #define ipf_next ipf_link.next |
Stefan Weil | b6dce92 | 2010-07-22 22:15:23 +0200 | [diff] [blame] | 236 | #define ipf_prev ipf_link.prev |
blueswir1 | 429d0a3 | 2009-01-13 19:48:42 +0000 | [diff] [blame] | 237 | |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 238 | #endif |