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. 13 * 3. Neither the name of the University nor the names of its contributors 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 33 #ifndef _IP_H_ 34 #define _IP_H_ 35 36 #ifdef HOST_WORDS_BIGENDIAN 37 # ifndef NTOHL 38 # define NTOHL(d) 39 # endif 40 # ifndef NTOHS 41 # define NTOHS(d) 42 # endif 43 # ifndef HTONL 44 # define HTONL(d) 45 # endif 46 # ifndef HTONS 47 # define HTONS(d) 48 # endif 49 #else 50 # ifndef NTOHL 51 # define NTOHL(d) ((d) = ntohl((d))) 52 # endif 53 # ifndef NTOHS 54 # define NTOHS(d) ((d) = ntohs((u_int16_t)(d))) 55 # endif 56 # ifndef HTONL 57 # define HTONL(d) ((d) = htonl((d))) 58 # endif 59 # ifndef HTONS 60 # define HTONS(d) ((d) = htons((u_int16_t)(d))) 61 # endif 62 #endif 63 64 typedef u_int32_t n_long; /* long as received from the net */ 65 66 /* 67 * Definitions for internet protocol version 4. 68 * Per RFC 791, September 1981. 69 */ 70 #define IPVERSION 4 71 72 /* 73 * Structure of an internet header, naked of options. 74 */ 75 struct ip { 76 #ifdef HOST_WORDS_BIGENDIAN 77 u_int ip_v:4, /* version */ 78 ip_hl:4; /* header length */ 79 #else 80 u_int ip_hl:4, /* header length */ 81 ip_v:4; /* version */ 82 #endif 83 u_int8_t ip_tos; /* type of service */ 84 u_int16_t ip_len; /* total length */ 85 u_int16_t ip_id; /* identification */ 86 u_int16_t ip_off; /* fragment offset field */ 87 #define IP_DF 0x4000 /* don't fragment flag */ 88 #define IP_MF 0x2000 /* more fragments flag */ 89 #define IP_OFFMASK 0x1fff /* mask for fragmenting bits */ 90 u_int8_t ip_ttl; /* time to live */ 91 u_int8_t ip_p; /* protocol */ 92 u_int16_t ip_sum; /* checksum */ 93 struct in_addr ip_src,ip_dst; /* source and dest address */ 94 }; 95 96 #define IP_MAXPACKET 65535 /* maximum packet size */ 97 98 /* 99 * Definitions for IP type of service (ip_tos) 100 */ 101 #define IPTOS_LOWDELAY 0x10 102 #define IPTOS_THROUGHPUT 0x08 103 #define IPTOS_RELIABILITY 0x04 104 105 /* 106 * Definitions for options. 107 */ 108 #define IPOPT_COPIED(o) ((o)&0x80) 109 #define IPOPT_CLASS(o) ((o)&0x60) 110 #define IPOPT_NUMBER(o) ((o)&0x1f) 111 112 #define IPOPT_CONTROL 0x00 113 #define IPOPT_RESERVED1 0x20 114 #define IPOPT_DEBMEAS 0x40 115 #define IPOPT_RESERVED2 0x60 116 117 #define IPOPT_EOL 0 /* end of option list */ 118 #define IPOPT_NOP 1 /* no operation */ 119 120 #define IPOPT_RR 7 /* record packet route */ 121 #define IPOPT_TS 68 /* timestamp */ 122 #define IPOPT_SECURITY 130 /* provide s,c,h,tcc */ 123 #define IPOPT_LSRR 131 /* loose source route */ 124 #define IPOPT_SATID 136 /* satnet id */ 125 #define IPOPT_SSRR 137 /* strict source route */ 126 127 /* 128 * Offsets to fields in options other than EOL and NOP. 129 */ 130 #define IPOPT_OPTVAL 0 /* option ID */ 131 #define IPOPT_OLEN 1 /* option length */ 132 #define IPOPT_OFFSET 2 /* offset within option */ 133 #define IPOPT_MINOFF 4 /* min value of above */ 134 135 /* 136 * Time stamp option structure. 137 */ 138 struct ip_timestamp { 139 u_int8_t ipt_code; /* IPOPT_TS */ 140 u_int8_t ipt_len; /* size of structure (variable) */ 141 u_int8_t ipt_ptr; /* index of current entry */ 142 #ifdef HOST_WORDS_BIGENDIAN 143 u_int ipt_oflw:4, /* overflow counter */ 144 ipt_flg:4; /* flags, see below */ 145 #else 146 u_int ipt_flg:4, /* flags, see below */ 147 ipt_oflw:4; /* overflow counter */ 148 #endif 149 union ipt_timestamp { 150 n_long ipt_time[1]; 151 struct ipt_ta { 152 struct in_addr ipt_addr; 153 n_long ipt_time; 154 } ipt_ta[1]; 155 } ipt_timestamp; 156 }; 157 158 /* flag bits for ipt_flg */ 159 #define IPOPT_TS_TSONLY 0 /* timestamps only */ 160 #define IPOPT_TS_TSANDADDR 1 /* timestamps and addresses */ 161 #define IPOPT_TS_PRESPEC 3 /* specified modules only */ 162 163 /* bits for security (not byte swapped) */ 164 #define IPOPT_SECUR_UNCLASS 0x0000 165 #define IPOPT_SECUR_CONFID 0xf135 166 #define IPOPT_SECUR_EFTO 0x789a 167 #define IPOPT_SECUR_MMMM 0xbc4d 168 #define IPOPT_SECUR_RESTR 0xaf13 169 #define IPOPT_SECUR_SECRET 0xd788 170 #define IPOPT_SECUR_TOPSECRET 0x6bc5 171 172 /* 173 * Internet implementation parameters. 174 */ 175 #define MAXTTL 255 /* maximum time to live (seconds) */ 176 #define IPDEFTTL 64 /* default ttl, from RFC 1340 */ 177 #define IPFRAGTTL 60 /* time to live for frags, slowhz */ 178 #define IPTTLDEC 1 /* subtracted when forwarding */ 179 180 #define IP_MSS 576 /* default maximum segment size */ 181 182 #if SIZEOF_CHAR_P == 4 183 struct mbuf_ptr { 184 struct mbuf *mptr; 185 uint32_t dummy; 186 }; 187 #else 188 struct mbuf_ptr { 189 struct mbuf *mptr; 190 }; 191 #endif 192 struct qlink { 193 void *next, *prev; 194 }; 195 196 /* 197 * Overlay for ip header used by other protocols (tcp, udp). 198 */ 199 struct ipovly { 200 struct mbuf_ptr ih_mbuf; /* backpointer to mbuf */ 201 u_int8_t ih_x1; /* (unused) */ 202 u_int8_t ih_pr; /* protocol */ 203 u_int16_t ih_len; /* protocol length */ 204 struct in_addr ih_src; /* source internet address */ 205 struct in_addr ih_dst; /* destination internet address */ 206 } __attribute__((packed)); 207 208 /* 209 * Ip reassembly queue structure. Each fragment 210 * being reassembled is attached to one of these structures. 211 * They are timed out after ipq_ttl drops to 0, and may also 212 * be reclaimed if memory becomes tight. 213 * size 28 bytes 214 */ 215 struct ipq { 216 struct qlink frag_link; /* to ip headers of fragments */ 217 struct qlink ip_link; /* to other reass headers */ 218 u_int8_t ipq_ttl; /* time for reass q to live */ 219 u_int8_t ipq_p; /* protocol of this fragment */ 220 u_int16_t ipq_id; /* sequence id for reassembly */ 221 struct in_addr ipq_src,ipq_dst; 222 }; 223 224 /* 225 * Ip header, when holding a fragment. 226 * 227 * Note: ipf_link must be at same offset as frag_link above 228 */ 229 struct ipasfrag { 230 struct qlink ipf_link; 231 struct ip ipf_ip; 232 }; 233 234 #define ipf_off ipf_ip.ip_off 235 #define ipf_tos ipf_ip.ip_tos 236 #define ipf_len ipf_ip.ip_len 237 #define ipf_next ipf_link.next 238 #define ipf_prev ipf_link.prev 239 240 /* 241 * Structure stored in mbuf in inpcb.ip_options 242 * and passed to ip_output when ip options are in use. 243 * The actual length of the options (including ipopt_dst) 244 * is in m_len. 245 */ 246 #define MAX_IPOPTLEN 40 247 248 struct ipoption { 249 struct in_addr ipopt_dst; /* first-hop dst if source routed */ 250 int8_t ipopt_list[MAX_IPOPTLEN]; /* options proper */ 251 }; 252 253 #ifdef LOG_ENABLED 254 /* 255 * Structure attached to inpcb.ip_moptions and 256 * passed to ip_output when IP multicast options are in use. 257 */ 258 259 struct ipstat { 260 u_long ips_total; /* total packets received */ 261 u_long ips_badsum; /* checksum bad */ 262 u_long ips_tooshort; /* packet too short */ 263 u_long ips_toosmall; /* not enough data */ 264 u_long ips_badhlen; /* ip header length < data size */ 265 u_long ips_badlen; /* ip length < ip header length */ 266 u_long ips_fragments; /* fragments received */ 267 u_long ips_fragdropped; /* frags dropped (dups, out of space) */ 268 u_long ips_fragtimeout; /* fragments timed out */ 269 u_long ips_forward; /* packets forwarded */ 270 u_long ips_cantforward; /* packets rcvd for unreachable dest */ 271 u_long ips_redirectsent; /* packets forwarded on same net */ 272 u_long ips_noproto; /* unknown or unsupported protocol */ 273 u_long ips_delivered; /* datagrams delivered to upper level*/ 274 u_long ips_localout; /* total ip packets generated here */ 275 u_long ips_odropped; /* lost packets due to nobufs, etc. */ 276 u_long ips_reassembled; /* total packets reassembled ok */ 277 u_long ips_fragmented; /* datagrams successfully fragmented */ 278 u_long ips_ofragments; /* output fragments created */ 279 u_long ips_cantfrag; /* don't fragment flag was set, etc. */ 280 u_long ips_badoptions; /* error in option processing */ 281 u_long ips_noroute; /* packets discarded due to no route */ 282 u_long ips_badvers; /* ip version != 4 */ 283 u_long ips_rawout; /* total raw ip packets generated */ 284 u_long ips_unaligned; /* times the ip packet was not aligned */ 285 }; 286 287 extern struct ipstat ipstat; 288 #endif 289 290 extern struct ipq ipq; /* ip reass. queue */ 291 extern u_int16_t ip_id; /* ip packet ctr, for ids */ 292 293 #endif 294