xref: /freebsd/sys/netinet/ip.h (revision 29363fb446372cb3f10bc98664e9767c53fbb457)
1c398230bSWarner Losh /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
4df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1993
5c59b9aa5SRobert Watson  *	The Regents of the University of California.
6c59b9aa5SRobert Watson  * All rights reserved.
7df8bae1dSRodney W. Grimes  *
8df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
9df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
10df8bae1dSRodney W. Grimes  * are met:
11df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
12df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
13df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
14df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
15df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
16fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
17df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
18df8bae1dSRodney W. Grimes  *    without specific prior written permission.
19df8bae1dSRodney W. Grimes  *
20df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
31df8bae1dSRodney W. Grimes  */
32df8bae1dSRodney W. Grimes 
33707f139eSPaul Richards #ifndef _NETINET_IP_H_
34707f139eSPaul Richards #define	_NETINET_IP_H_
35707f139eSPaul Richards 
36aecfcdb8SMaxime Henrion #include <sys/cdefs.h>
37aecfcdb8SMaxime Henrion 
38df8bae1dSRodney W. Grimes /*
39df8bae1dSRodney W. Grimes  * Definitions for internet protocol version 4.
40c59b9aa5SRobert Watson  *
41df8bae1dSRodney W. Grimes  * Per RFC 791, September 1981.
42df8bae1dSRodney W. Grimes  */
43df8bae1dSRodney W. Grimes #define	IPVERSION	4
44df8bae1dSRodney W. Grimes 
45df8bae1dSRodney W. Grimes /*
46df8bae1dSRodney W. Grimes  * Structure of an internet header, naked of options.
47df8bae1dSRodney W. Grimes  */
48df8bae1dSRodney W. Grimes struct ip {
49df8bae1dSRodney W. Grimes #if BYTE_ORDER == LITTLE_ENDIAN
50dc5fd259SLuigi Rizzo 	u_char	ip_hl:4,		/* header length */
51df8bae1dSRodney W. Grimes 		ip_v:4;			/* version */
52df8bae1dSRodney W. Grimes #endif
53df8bae1dSRodney W. Grimes #if BYTE_ORDER == BIG_ENDIAN
54dc5fd259SLuigi Rizzo 	u_char	ip_v:4,			/* version */
55df8bae1dSRodney W. Grimes 		ip_hl:4;		/* header length */
56df8bae1dSRodney W. Grimes #endif
57df8bae1dSRodney W. Grimes 	u_char	ip_tos;			/* type of service */
5864682bc2SGarrett Wollman 	u_short	ip_len;			/* total length */
59df8bae1dSRodney W. Grimes 	u_short	ip_id;			/* identification */
6064682bc2SGarrett Wollman 	u_short	ip_off;			/* fragment offset field */
61c383a33fSDima Ruban #define	IP_RF 0x8000			/* reserved fragment flag */
62df8bae1dSRodney W. Grimes #define	IP_DF 0x4000			/* dont fragment flag */
63df8bae1dSRodney W. Grimes #define	IP_MF 0x2000			/* more fragments flag */
64df8bae1dSRodney W. Grimes #define	IP_OFFMASK 0x1fff		/* mask for fragmenting bits */
65df8bae1dSRodney W. Grimes 	u_char	ip_ttl;			/* time to live */
66df8bae1dSRodney W. Grimes 	u_char	ip_p;			/* protocol */
67df8bae1dSRodney W. Grimes 	u_short	ip_sum;			/* checksum */
68df8bae1dSRodney W. Grimes 	struct	in_addr ip_src,ip_dst;	/* source and dest address */
698018ac15SZbigniew Bodek } __packed __aligned(2);
70df8bae1dSRodney W. Grimes 
71df8bae1dSRodney W. Grimes #define	IP_MAXPACKET	65535		/* maximum packet size */
72df8bae1dSRodney W. Grimes 
73df8bae1dSRodney W. Grimes /*
74c59b9aa5SRobert Watson  * Definitions for IP type of service (ip_tos).
75df8bae1dSRodney W. Grimes  */
76df8bae1dSRodney W. Grimes #define	IPTOS_LOWDELAY		0x10
77df8bae1dSRodney W. Grimes #define	IPTOS_THROUGHPUT	0x08
78df8bae1dSRodney W. Grimes #define	IPTOS_RELIABILITY	0x04
79*3b0ee680SRichard Scheffenegger #define	IPTOS_MINCOST		IPTOS_DSCP_CS0
80df8bae1dSRodney W. Grimes 
81df8bae1dSRodney W. Grimes /*
82ddee4524SKevin Lo  * Definitions for IP precedence (also in ip_tos) (deprecated).
83df8bae1dSRodney W. Grimes  */
84ddee4524SKevin Lo #define	IPTOS_PREC_NETCONTROL		IPTOS_DSCP_CS7
85ddee4524SKevin Lo #define	IPTOS_PREC_INTERNETCONTROL	IPTOS_DSCP_CS6
86ddee4524SKevin Lo #define	IPTOS_PREC_CRITIC_ECP		IPTOS_DSCP_CS5
87ddee4524SKevin Lo #define	IPTOS_PREC_FLASHOVERRIDE	IPTOS_DSCP_CS4
88ddee4524SKevin Lo #define	IPTOS_PREC_FLASH		IPTOS_DSCP_CS3
89ddee4524SKevin Lo #define	IPTOS_PREC_IMMEDIATE		IPTOS_DSCP_CS2
90ddee4524SKevin Lo #define	IPTOS_PREC_PRIORITY		IPTOS_DSCP_CS1
91ddee4524SKevin Lo #define	IPTOS_PREC_ROUTINE		IPTOS_DSCP_CS0
92df8bae1dSRodney W. Grimes 
93df8bae1dSRodney W. Grimes /*
940d3d234cSKristof Provost  * Offset of Diffserv decimal value to convert it to tos value .
950d3d234cSKristof Provost  */
960d3d234cSKristof Provost #define	IPTOS_DSCP_OFFSET		2
970d3d234cSKristof Provost 
980d3d234cSKristof Provost /*
99ddee4524SKevin Lo  * Definitions for DiffServ Codepoints as per RFC2474 and RFC5865.
10074882260SXin LI  */
10174882260SXin LI #define	IPTOS_DSCP_CS0		0x00
10274882260SXin LI #define	IPTOS_DSCP_CS1		0x20
10374882260SXin LI #define	IPTOS_DSCP_AF11		0x28
10474882260SXin LI #define	IPTOS_DSCP_AF12		0x30
10574882260SXin LI #define	IPTOS_DSCP_AF13		0x38
10674882260SXin LI #define	IPTOS_DSCP_CS2		0x40
10774882260SXin LI #define	IPTOS_DSCP_AF21		0x48
10874882260SXin LI #define	IPTOS_DSCP_AF22		0x50
10974882260SXin LI #define	IPTOS_DSCP_AF23		0x58
11074882260SXin LI #define	IPTOS_DSCP_CS3		0x60
11174882260SXin LI #define	IPTOS_DSCP_AF31		0x68
11274882260SXin LI #define	IPTOS_DSCP_AF32		0x70
11374882260SXin LI #define	IPTOS_DSCP_AF33		0x78
11474882260SXin LI #define	IPTOS_DSCP_CS4		0x80
11574882260SXin LI #define	IPTOS_DSCP_AF41		0x88
11674882260SXin LI #define	IPTOS_DSCP_AF42		0x90
11774882260SXin LI #define	IPTOS_DSCP_AF43		0x98
11874882260SXin LI #define	IPTOS_DSCP_CS5		0xa0
119ddee4524SKevin Lo #define	IPTOS_DSCP_VA		0xb0
12074882260SXin LI #define	IPTOS_DSCP_EF		0xb8
12174882260SXin LI #define	IPTOS_DSCP_CS6		0xc0
12274882260SXin LI #define	IPTOS_DSCP_CS7		0xe0
12374882260SXin LI 
12474882260SXin LI /*
125c59b9aa5SRobert Watson  * ECN (Explicit Congestion Notification) codepoints in RFC3168 mapped to the
126c59b9aa5SRobert Watson  * lower 2 bits of the TOS field.
12759dfcba4SHajimu UMEMOTO  */
12859dfcba4SHajimu UMEMOTO #define	IPTOS_ECN_NOTECT	0x00	/* not-ECT */
12959dfcba4SHajimu UMEMOTO #define	IPTOS_ECN_ECT1		0x01	/* ECN-capable transport (1) */
13059dfcba4SHajimu UMEMOTO #define	IPTOS_ECN_ECT0		0x02	/* ECN-capable transport (0) */
13159dfcba4SHajimu UMEMOTO #define	IPTOS_ECN_CE		0x03	/* congestion experienced */
13259dfcba4SHajimu UMEMOTO #define	IPTOS_ECN_MASK		0x03	/* ECN field mask */
13359dfcba4SHajimu UMEMOTO 
13459dfcba4SHajimu UMEMOTO /*
135df8bae1dSRodney W. Grimes  * Definitions for options.
136df8bae1dSRodney W. Grimes  */
137df8bae1dSRodney W. Grimes #define	IPOPT_COPIED(o)		((o)&0x80)
138df8bae1dSRodney W. Grimes #define	IPOPT_CLASS(o)		((o)&0x60)
139df8bae1dSRodney W. Grimes #define	IPOPT_NUMBER(o)		((o)&0x1f)
140df8bae1dSRodney W. Grimes 
141df8bae1dSRodney W. Grimes #define	IPOPT_CONTROL		0x00
142df8bae1dSRodney W. Grimes #define	IPOPT_RESERVED1		0x20
143df8bae1dSRodney W. Grimes #define	IPOPT_DEBMEAS		0x40
144df8bae1dSRodney W. Grimes #define	IPOPT_RESERVED2		0x60
145df8bae1dSRodney W. Grimes 
146df8bae1dSRodney W. Grimes #define	IPOPT_EOL		0		/* end of option list */
147df8bae1dSRodney W. Grimes #define	IPOPT_NOP		1		/* no operation */
148df8bae1dSRodney W. Grimes 
149df8bae1dSRodney W. Grimes #define	IPOPT_RR		7		/* record packet route */
150df8bae1dSRodney W. Grimes #define	IPOPT_TS		68		/* timestamp */
151df8bae1dSRodney W. Grimes #define	IPOPT_SECURITY		130		/* provide s,c,h,tcc */
152df8bae1dSRodney W. Grimes #define	IPOPT_LSRR		131		/* loose source route */
153365979cdSRobert Watson #define	IPOPT_ESO		133		/* extended security */
154a4641f4eSPedro F. Giffuni #define	IPOPT_CIPSO		134		/* commercial security */
155df8bae1dSRodney W. Grimes #define	IPOPT_SATID		136		/* satnet id */
156df8bae1dSRodney W. Grimes #define	IPOPT_SSRR		137		/* strict source route */
15749fa849bSBill Fenner #define	IPOPT_RA		148		/* router alert */
158df8bae1dSRodney W. Grimes 
159df8bae1dSRodney W. Grimes /*
160df8bae1dSRodney W. Grimes  * Offsets to fields in options other than EOL and NOP.
161df8bae1dSRodney W. Grimes  */
162df8bae1dSRodney W. Grimes #define	IPOPT_OPTVAL		0		/* option ID */
163df8bae1dSRodney W. Grimes #define	IPOPT_OLEN		1		/* option length */
164df8bae1dSRodney W. Grimes #define	IPOPT_OFFSET		2		/* offset within option */
165df8bae1dSRodney W. Grimes #define	IPOPT_MINOFF		4		/* min value of above */
166df8bae1dSRodney W. Grimes 
167df8bae1dSRodney W. Grimes /*
168df8bae1dSRodney W. Grimes  * Time stamp option structure.
169df8bae1dSRodney W. Grimes  */
170df8bae1dSRodney W. Grimes struct	ip_timestamp {
171df8bae1dSRodney W. Grimes 	u_char	ipt_code;		/* IPOPT_TS */
172df8bae1dSRodney W. Grimes 	u_char	ipt_len;		/* size of structure (variable) */
173df8bae1dSRodney W. Grimes 	u_char	ipt_ptr;		/* index of current entry */
174df8bae1dSRodney W. Grimes #if BYTE_ORDER == LITTLE_ENDIAN
175dc5fd259SLuigi Rizzo 	u_char	ipt_flg:4,		/* flags, see below */
176df8bae1dSRodney W. Grimes 		ipt_oflw:4;		/* overflow counter */
177df8bae1dSRodney W. Grimes #endif
178df8bae1dSRodney W. Grimes #if BYTE_ORDER == BIG_ENDIAN
179dc5fd259SLuigi Rizzo 	u_char	ipt_oflw:4,		/* overflow counter */
180df8bae1dSRodney W. Grimes 		ipt_flg:4;		/* flags, see below */
181df8bae1dSRodney W. Grimes #endif
182df8bae1dSRodney W. Grimes 	union ipt_timestamp {
183d685b6eeSLuigi Rizzo 		uint32_t	ipt_time[1];	/* network format */
184df8bae1dSRodney W. Grimes 		struct	ipt_ta {
185df8bae1dSRodney W. Grimes 			struct in_addr ipt_addr;
186d685b6eeSLuigi Rizzo 			uint32_t ipt_time;	/* network format */
187df8bae1dSRodney W. Grimes 		} ipt_ta[1];
188df8bae1dSRodney W. Grimes 	} ipt_timestamp;
189df8bae1dSRodney W. Grimes };
190df8bae1dSRodney W. Grimes 
191c59b9aa5SRobert Watson /* Flag bits for ipt_flg. */
192df8bae1dSRodney W. Grimes #define	IPOPT_TS_TSONLY		0		/* timestamps only */
193df8bae1dSRodney W. Grimes #define	IPOPT_TS_TSANDADDR	1		/* timestamps and addresses */
194df8bae1dSRodney W. Grimes #define	IPOPT_TS_PRESPEC	3		/* specified modules only */
195df8bae1dSRodney W. Grimes 
196c59b9aa5SRobert Watson /* Bits for security (not byte swapped). */
197df8bae1dSRodney W. Grimes #define	IPOPT_SECUR_UNCLASS	0x0000
198df8bae1dSRodney W. Grimes #define	IPOPT_SECUR_CONFID	0xf135
199df8bae1dSRodney W. Grimes #define	IPOPT_SECUR_EFTO	0x789a
200df8bae1dSRodney W. Grimes #define	IPOPT_SECUR_MMMM	0xbc4d
201df8bae1dSRodney W. Grimes #define	IPOPT_SECUR_RESTR	0xaf13
202df8bae1dSRodney W. Grimes #define	IPOPT_SECUR_SECRET	0xd788
203df8bae1dSRodney W. Grimes #define	IPOPT_SECUR_TOPSECRET	0x6bc5
204df8bae1dSRodney W. Grimes 
205df8bae1dSRodney W. Grimes /*
206df8bae1dSRodney W. Grimes  * Internet implementation parameters.
207df8bae1dSRodney W. Grimes  */
208df8bae1dSRodney W. Grimes #define	MAXTTL		255		/* maximum time to live (seconds) */
209df8bae1dSRodney W. Grimes #define	IPDEFTTL	64		/* default ttl, from RFC 1340 */
210df8bae1dSRodney W. Grimes #define	IPTTLDEC	1		/* subtracted when forwarding */
211df8bae1dSRodney W. Grimes #define	IP_MSS		576		/* default maximum segment size */
212707f139eSPaul Richards 
2131cfd4b53SBruce M Simpson /*
2141cfd4b53SBruce M Simpson  * This is the real IPv4 pseudo header, used for computing the TCP and UDP
2151cfd4b53SBruce M Simpson  * checksums. For the Internet checksum, struct ipovly can be used instead.
2161cfd4b53SBruce M Simpson  * For stronger checksums, the real thing must be used.
2171cfd4b53SBruce M Simpson  */
2181cfd4b53SBruce M Simpson struct ippseudo {
2191cfd4b53SBruce M Simpson 	struct	in_addr	ippseudo_src;	/* source internet address */
2201cfd4b53SBruce M Simpson 	struct	in_addr	ippseudo_dst;	/* destination internet address */
22191179f79SBruce M Simpson 	u_char		ippseudo_pad;	/* pad, must be zero */
22291179f79SBruce M Simpson 	u_char		ippseudo_p;	/* protocol */
22391179f79SBruce M Simpson 	u_short		ippseudo_len;	/* protocol length */
22491179f79SBruce M Simpson };
225707f139eSPaul Richards #endif
226