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