xref: /netbsd-src/sys/netinet/ip_var.h (revision aaf4ece63a859a04e37cf3a7229b5fab0157cc06)
1 /*	$NetBSD: ip_var.h,v 1.76 2005/12/24 20:45:09 perry Exp $	*/
2 
3 /*
4  * Copyright (c) 1982, 1986, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  *	@(#)ip_var.h	8.2 (Berkeley) 1/9/95
32  */
33 
34 #ifndef _NETINET_IP_VAR_H_
35 #define _NETINET_IP_VAR_H_
36 
37 #include <sys/queue.h>
38 #include <net/route.h>
39 
40 /*
41  * Overlay for ip header used by other protocols (tcp, udp).
42  */
43 struct ipovly {
44 	u_int8_t  ih_x1[9];		/* (unused) */
45 	u_int8_t  ih_pr;		/* protocol */
46 	u_int16_t ih_len;		/* protocol length */
47 	struct	  in_addr ih_src;	/* source internet address */
48 	struct	  in_addr ih_dst;	/* destination internet address */
49 } __attribute__((__packed__));
50 
51 /*
52  * Ip (reassembly or sequence) queue structures.
53  *
54  * XXX -- The following explains why the ipqe_m field is here, for TCP's use:
55  * We want to avoid doing m_pullup on incoming packets but that
56  * means avoiding dtom on the tcp reassembly code.  That in turn means
57  * keeping an mbuf pointer in the reassembly queue (since we might
58  * have a cluster).  As a quick hack, the source & destination
59  * port numbers (which are no longer needed once we've located the
60  * tcpcb) are overlayed with an mbuf pointer.
61  */
62 TAILQ_HEAD(ipqehead, ipqent);
63 struct ipqent {
64 	TAILQ_ENTRY(ipqent) ipqe_q;
65 	union {
66 		struct ip	*_ip;
67 		struct tcpiphdr *_tcp;
68 	} _ipqe_u1;
69 	struct mbuf	*ipqe_m;	/* point to first mbuf */
70 	struct mbuf	*ipre_mlast;	/* point to last mbuf */
71 	u_int8_t	ipqe_mff;	/* for IP fragmentation */
72 	/*
73 	 * The following are used in TCP reassembly
74 	 */
75 	TAILQ_ENTRY(ipqent) ipqe_timeq;
76 	u_int32_t ipqe_seq;
77 	u_int32_t ipqe_len;
78 	u_int32_t ipqe_flags;
79 };
80 #define	ipqe_ip		_ipqe_u1._ip
81 #define	ipqe_tcp	_ipqe_u1._tcp
82 
83 /*
84  * Ip reassembly queue structure.  Each fragment
85  * being reassembled is attached to one of these structures.
86  * They are timed out after ipq_ttl drops to 0, and may also
87  * be reclaimed if memory becomes tight.
88  */
89 struct ipq {
90 	LIST_ENTRY(ipq) ipq_q;		/* to other reass headers */
91 	u_int8_t  ipq_ttl;		/* time for reass q to live */
92 	u_int8_t  ipq_p;		/* protocol of this fragment */
93 	u_int16_t ipq_id;		/* sequence id for reassembly */
94 	struct	  ipqehead ipq_fragq;	/* to ip fragment queue */
95 	struct	  in_addr ipq_src, ipq_dst;
96 	u_int16_t ipq_nfrags;		/* frags in this queue entry */
97 };
98 
99 /*
100  * Structure stored in mbuf in inpcb.ip_options
101  * and passed to ip_output when ip options are in use.
102  * The actual length of the options (including ipopt_dst)
103  * is in m_len.
104  */
105 #define	MAX_IPOPTLEN	40
106 
107 struct ipoption {
108 	struct	in_addr ipopt_dst;	/* first-hop dst if source routed */
109 	int8_t	ipopt_list[MAX_IPOPTLEN];	/* options proper */
110 };
111 
112 /*
113  * Structure attached to inpcb.ip_moptions and
114  * passed to ip_output when IP multicast options are in use.
115  */
116 struct ip_moptions {
117 	struct	  ifnet *imo_multicast_ifp; /* ifp for outgoing multicasts */
118 	struct in_addr imo_multicast_addr; /* ifindex/addr on MULTICAST_IF */
119 	u_int8_t  imo_multicast_ttl;	/* TTL for outgoing multicasts */
120 	u_int8_t  imo_multicast_loop;	/* 1 => hear sends if a member */
121 	u_int16_t imo_num_memberships;	/* no. memberships this socket */
122 	struct	  in_multi *imo_membership[IP_MAX_MEMBERSHIPS];
123 };
124 
125 struct	ipstat {
126 	u_quad_t ips_total;		/* total packets received */
127 	u_quad_t ips_badsum;		/* checksum bad */
128 	u_quad_t ips_tooshort;		/* packet too short */
129 	u_quad_t ips_toosmall;		/* not enough data */
130 	u_quad_t ips_badhlen;		/* ip header length < data size */
131 	u_quad_t ips_badlen;		/* ip length < ip header length */
132 	u_quad_t ips_fragments;		/* fragments received */
133 	u_quad_t ips_fragdropped;	/* frags dropped (dups, out of space) */
134 	u_quad_t ips_fragtimeout;	/* fragments timed out */
135 	u_quad_t ips_forward;		/* packets forwarded */
136 	u_quad_t ips_fastforward;	/* packets fast forwarded */
137 	u_quad_t ips_cantforward;	/* packets rcvd for unreachable dest */
138 	u_quad_t ips_redirectsent;	/* packets forwarded on same net */
139 	u_quad_t ips_noproto;		/* unknown or unsupported protocol */
140 	u_quad_t ips_delivered;		/* datagrams delivered to upper level*/
141 	u_quad_t ips_localout;		/* total ip packets generated here */
142 	u_quad_t ips_odropped;		/* lost packets due to nobufs, etc. */
143 	u_quad_t ips_reassembled;	/* total packets reassembled ok */
144 	u_quad_t ips_fragmented;	/* datagrams successfully fragmented */
145 	u_quad_t ips_ofragments;	/* output fragments created */
146 	u_quad_t ips_cantfrag;		/* don't fragment flag was set, etc. */
147 	u_quad_t ips_badoptions;	/* error in option processing */
148 	u_quad_t ips_noroute;		/* packets discarded due to no route */
149 	u_quad_t ips_badvers;		/* ip version != 4 */
150 	u_quad_t ips_rawout;		/* total raw ip packets generated */
151 	u_quad_t ips_badfrags;		/* malformed fragments (bad length) */
152 	u_quad_t ips_rcvmemdrop;	/* frags dropped for lack of memory */
153 	u_quad_t ips_toolong;		/* ip length > max ip packet size */
154 	u_quad_t ips_nogif;		/* no match gif found */
155 	u_quad_t ips_badaddr;		/* invalid address on header */
156 };
157 
158 #define	IPFLOW_HASHBITS			6 /* should not be a multiple of 8 */
159 struct ipflow {
160 	LIST_ENTRY(ipflow) ipf_list;	/* next in active list */
161 	LIST_ENTRY(ipflow) ipf_hash;	/* next ipflow in bucket */
162 	struct in_addr ipf_dst;		/* destination address */
163 	struct in_addr ipf_src;		/* source address */
164 	u_int8_t ipf_tos;		/* type-of-service */
165 	struct route ipf_ro;		/* associated route entry */
166 	u_long ipf_uses;		/* number of uses in this period */
167 	u_long ipf_last_uses;		/* number of uses in last period */
168 	u_long ipf_dropped;		/* ENOBUFS returned by if_output */
169 	u_long ipf_errors;		/* other errors returned by if_output */
170 	u_int ipf_timer;		/* lifetime timer */
171 	time_t ipf_start;		/* creation time */
172 };
173 
174 #ifdef _KERNEL
175 
176 #ifdef _KERNEL_OPT
177 #include "opt_gateway.h"
178 #include "opt_mbuftrace.h"
179 #endif
180 
181 /* flags passed to ip_output as last parameter */
182 #define	IP_FORWARDING		0x1		/* most of ip header exists */
183 #define	IP_RAWOUTPUT		0x2		/* raw ip header exists */
184 #define	IP_RETURNMTU		0x4		/* pass back mtu on EMSGSIZE */
185 #define	IP_ROUTETOIF		SO_DONTROUTE	/* bypass routing tables */
186 #define	IP_ALLOWBROADCAST	SO_BROADCAST	/* can send broadcast packets */
187 #define	IP_MTUDISC		0x0400		/* Path MTU Discovery; set DF */
188 
189 #ifdef __NO_STRICT_ALIGNMENT
190 #define	IP_HDR_ALIGNED_P(ip)	1
191 #else
192 #define	IP_HDR_ALIGNED_P(ip)	((((vaddr_t) (ip)) & 3) == 0)
193 #endif
194 
195 extern struct domain inetdomain;
196 
197 extern struct ipstat ipstat;		/* ip statistics */
198 extern LIST_HEAD(ipqhead, ipq) ipq[];	/* ip reass. queue */
199 extern int   ip_defttl;			/* default IP ttl */
200 extern int   ipforwarding;		/* ip forwarding */
201 extern int   ip_mtudisc;		/* mtu discovery */
202 extern int   ip_mtudisc_timeout;	/* seconds to timeout mtu discovery */
203 extern int   anonportmin;		/* minimum ephemeral port */
204 extern int   anonportmax;		/* maximum ephemeral port */
205 extern int   lowportmin;		/* minimum reserved port */
206 extern int   lowportmax;		/* maximum reserved port */
207 extern int   ip_do_loopback_cksum;	/* do IP checksum on loopback? */
208 extern struct rttimer_queue *ip_mtudisc_timeout_q;
209 #ifdef MBUFTRACE
210 extern struct mowner ip_rx_mowner;
211 extern struct mowner ip_tx_mowner;
212 #endif
213 #ifdef GATEWAY
214 extern int ip_maxflows;
215 #endif
216 extern struct pool inmulti_pool;
217 extern struct pool ipqent_pool;
218 struct	 inpcb;
219 
220 int	 ip_ctloutput(int, struct socket *, int, int, struct mbuf **);
221 int	 ip_dooptions(struct mbuf *);
222 void	 ip_drain(void);
223 void	 ip_forward(struct mbuf *, int);
224 void	 ip_freef(struct ipq *);
225 void	 ip_freemoptions(struct ip_moptions *);
226 int	 ip_getmoptions(int, struct ip_moptions *, struct mbuf **);
227 void	 ip_init(void);
228 int	 ip_optcopy(struct ip *, struct ip *);
229 u_int	 ip_optlen(struct inpcb *);
230 int	 ip_output(struct mbuf *, ...);
231 int	 ip_fragment(struct mbuf *, struct ifnet *, u_long);
232 int	 ip_pcbopts(struct mbuf **, struct mbuf *);
233 struct mbuf *
234 	 ip_reass(struct ipqent *, struct ipq *, struct ipqhead *);
235 struct in_ifaddr *
236 	 ip_rtaddr(struct in_addr);
237 void	 ip_savecontrol(struct inpcb *, struct mbuf **, struct ip *,
238 	   struct mbuf *);
239 int	 ip_setmoptions(int, struct ip_moptions **, struct mbuf *);
240 void	 ip_slowtimo(void);
241 struct mbuf *
242 	 ip_srcroute(void);
243 void	 ip_stripoptions(struct mbuf *, struct mbuf *);
244 int	 ip_sysctl(int *, u_int, void *, size_t *, void *, size_t);
245 void	 ipintr(void);
246 void *	 rip_ctlinput(int, struct sockaddr *, void *);
247 int	 rip_ctloutput(int, struct socket *, int, int, struct mbuf **);
248 void	 rip_init(void);
249 void	 rip_input(struct mbuf *, ...);
250 int	 rip_output(struct mbuf *, ...);
251 int	 rip_usrreq(struct socket *,
252 	    int, struct mbuf *, struct mbuf *, struct mbuf *, struct lwp *);
253 void	ipflow_init(void);
254 struct	ipflow *ipflow_reap(int);
255 void	ipflow_create(const struct route *, struct mbuf *);
256 void	ipflow_slowtimo(void);
257 void	ipflow_invalidate_all(void);
258 
259 extern uint16_t	ip_id;
260 static inline uint16_t ip_newid(void);
261 
262 u_int16_t ip_randomid(void);
263 extern int ip_do_randomid;
264 
265 /*
266  * ip_newid_range: "allocate" num contiguous ip_ids.
267  *
268  * => return the first id.
269  */
270 
271 static inline uint16_t
272 ip_newid_range(unsigned int num)
273 {
274 	uint16_t id;
275 
276 	if (ip_do_randomid) {
277 		/* XXX ignore num */
278 		return ip_randomid();
279 	}
280 
281 	id = htons(ip_id);
282 	ip_id += num;
283 
284 	return id;
285 }
286 
287 static inline uint16_t
288 ip_newid(void)
289 {
290 
291 	return ip_newid_range(1);
292 }
293 
294 #endif  /* _KERNEL */
295 
296 #endif /* !_NETINET_IP_VAR_H_ */
297