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