xref: /netbsd-src/sys/netinet/ip_var.h (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: ip_var.h,v 1.126 2018/07/10 15:46:58 maxv 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 } __packed;
50 
51 /*
52  * IP Flow structure
53  */
54 struct ipflow {
55 	TAILQ_ENTRY(ipflow) ipf_list;	/* next in active list */
56 	TAILQ_ENTRY(ipflow) ipf_hash;	/* next ipflow in bucket */
57 	size_t ipf_hashidx;		/* own hash index of ipflowtable[] */
58 	struct in_addr ipf_dst;		/* destination address */
59 	struct in_addr ipf_src;		/* source address */
60 	uint8_t ipf_tos;		/* type-of-service */
61 	struct route ipf_ro;		/* associated route entry */
62 	u_long ipf_uses;		/* number of uses in this period */
63 	u_long ipf_last_uses;		/* number of uses in last period */
64 	u_long ipf_dropped;		/* ENOBUFS retured by if_output */
65 	u_long ipf_errors;		/* other errors returned by if_output */
66 	u_int ipf_timer;		/* lifetime timer */
67 };
68 
69 /*
70  * TCP sequence queue structure.
71  */
72 TAILQ_HEAD(ipqehead, ipqent);
73 struct ipqent {
74 	TAILQ_ENTRY(ipqent) ipqe_q;
75 	struct mbuf *ipqe_m;
76 	TAILQ_ENTRY(ipqent) ipqe_timeq;
77 	u_int32_t ipqe_seq;
78 	u_int32_t ipqe_len;
79 	u_int32_t ipqe_flags;
80 };
81 
82 /*
83  * Structure stored in mbuf in inpcb.ip_options
84  * and passed to ip_output when ip options are in use.
85  * The actual length of the options (including ipopt_dst)
86  * is in m_len.
87  */
88 #define	MAX_IPOPTLEN	40
89 
90 struct ipoption {
91 	struct	in_addr ipopt_dst;	/* first-hop dst if source routed */
92 	int8_t	ipopt_list[MAX_IPOPTLEN];	/* options proper */
93 };
94 
95 /*
96  * Structure attached to inpcb.ip_moptions and
97  * passed to ip_output when IP multicast options are in use.
98  */
99 struct ip_moptions {
100 	if_index_t imo_multicast_if_index; /* I/F for outgoing multicasts */
101 	struct in_addr imo_multicast_addr; /* ifindex/addr on MULTICAST_IF */
102 	u_int8_t  imo_multicast_ttl;	/* TTL for outgoing multicasts */
103 	u_int8_t  imo_multicast_loop;	/* 1 => hear sends if a member */
104 	u_int16_t imo_num_memberships;	/* no. memberships this socket */
105 	struct	  in_multi *imo_membership[IP_MAX_MEMBERSHIPS];
106 };
107 
108 struct ip_pktopts {
109 	struct sockaddr_in ippo_laddr;	/* source address */
110 	struct ip_moptions *ippo_imo;	/* inp->inp_moptions or &ippo_imobuf */
111 	struct ip_moptions ippo_imobuf;	/* use when IP_PKTINFO */
112 };
113 
114 /*
115  * IP statistics.
116  * Each counter is an unsigned 64-bit value.
117  */
118 #define	IP_STAT_TOTAL		0	/* total packets received */
119 #define	IP_STAT_BADSUM		1	/* checksum bad */
120 #define	IP_STAT_TOOSHORT	2	/* packet too short */
121 #define	IP_STAT_TOOSMALL	3	/* not enough data */
122 #define	IP_STAT_BADHLEN		4	/* ip header length < data size */
123 #define	IP_STAT_BADLEN		5	/* ip length < ip header length */
124 #define	IP_STAT_FRAGMENTS	6	/* fragments received */
125 #define	IP_STAT_FRAGDROPPED	7	/* frags dropped (dups, out of space) */
126 #define	IP_STAT_FRAGTIMEOUT	8	/* fragments timed out */
127 #define	IP_STAT_FORWARD		9	/* packets forwarded */
128 #define	IP_STAT_FASTFORWARD	10	/* packets fast forwarded */
129 #define	IP_STAT_CANTFORWARD	11	/* packets rcvd for unreachable dest */
130 #define	IP_STAT_REDIRECTSENT	12	/* packets forwareded on same net */
131 #define	IP_STAT_NOPROTO		13	/* unknown or unsupported protocol */
132 #define	IP_STAT_DELIVERED	14	/* datagrams delivered to upper level */
133 #define	IP_STAT_LOCALOUT	15	/* total ip packets generated here */
134 #define	IP_STAT_ODROPPED	16	/* lost packets due to nobufs, etc. */
135 #define	IP_STAT_REASSEMBLED	17	/* total packets reassembled ok */
136 #define	IP_STAT_FRAGMENTED	18	/* datagrams successfully fragmented */
137 #define	IP_STAT_OFRAGMENTS	19	/* output fragments created */
138 #define	IP_STAT_CANTFRAG	20	/* don't fragment flag was set, etc. */
139 #define	IP_STAT_BADOPTIONS	21	/* error in option processing */
140 #define	IP_STAT_NOROUTE		22	/* packets discarded due to no route */
141 #define	IP_STAT_BADVERS		23	/* ip version != 4 */
142 #define	IP_STAT_RAWOUT		24	/* total raw ip packets generated */
143 #define	IP_STAT_BADFRAGS	25	/* malformed fragments (bad length) */
144 #define	IP_STAT_RCVMEMDROP	26	/* frags dropped for lack of memory */
145 #define	IP_STAT_TOOLONG		27	/* ip length > max ip packet size */
146 #define	IP_STAT_NOGIF		28	/* no match gif found */
147 #define	IP_STAT_BADADDR		29	/* invalid address on header */
148 #define	IP_STAT_NOL2TP		30	/* no match l2tp found */
149 #define	IP_STAT_NOIPSEC		31	/* no match ipsec(4) found */
150 
151 #define	IP_NSTATS		32
152 
153 #ifdef _KERNEL
154 
155 #ifdef _KERNEL_OPT
156 #include "opt_gateway.h"
157 #include "opt_mbuftrace.h"
158 #endif
159 
160 /*
161  * The following flags can be passed to ip_output() as last parameter
162  */
163 #define	IP_FORWARDING		0x0001		/* most of ip header exists */
164 #define	IP_RAWOUTPUT		0x0002		/* raw ip header exists */
165 #define	IP_RETURNMTU		0x0004		/* pass back mtu on EMSGSIZE */
166 #define	IP_NOIPNEWID		0x0008		/* don't fill in ip_id */
167 __CTASSERT(SO_DONTROUTE ==	0x0010);
168 __CTASSERT(SO_BROADCAST ==	0x0020);
169 #define	IP_ROUTETOIF		SO_DONTROUTE	/* bypass routing tables */
170 #define	IP_ALLOWBROADCAST	SO_BROADCAST	/* can send broadcast packets */
171 
172 #define	IP_IGMP_MCAST		0x0040		/* IGMP for mcast join/leave */
173 #define	IP_MTUDISC		0x0400		/* Path MTU Discovery; set DF */
174 #define	IP_ROUTETOIFINDEX	0x0800	/* force route imo_multicast_if_index */
175 
176 extern struct domain inetdomain;
177 extern const struct pr_usrreqs rip_usrreqs;
178 
179 extern int   ip_defttl;			/* default IP ttl */
180 extern int   ipforwarding;		/* ip forwarding */
181 extern int   ip_mtudisc;		/* mtu discovery */
182 extern int   ip_mtudisc_timeout;	/* seconds to timeout mtu discovery */
183 extern int   anonportmin;		/* minimum ephemeral port */
184 extern int   anonportmax;		/* maximum ephemeral port */
185 extern int   lowportmin;		/* minimum reserved port */
186 extern int   lowportmax;		/* maximum reserved port */
187 extern int   ip_do_loopback_cksum;	/* do IP checksum on loopback? */
188 extern struct rttimer_queue *ip_mtudisc_timeout_q;
189 #ifdef MBUFTRACE
190 extern struct mowner ip_rx_mowner;
191 extern struct mowner ip_tx_mowner;
192 #endif
193 struct	 inpcb;
194 struct   sockopt;
195 
196 void	ip_init(void);
197 void	in_init(void);
198 
199 int	 ip_ctloutput(int, struct socket *, struct sockopt *);
200 int	 ip_setpktopts(struct mbuf *, struct ip_pktopts *, int *,
201 	    struct inpcb *, kauth_cred_t);
202 void	 ip_drain(void);
203 void	 ip_drainstub(void);
204 void	 ip_freemoptions(struct ip_moptions *);
205 int	 ip_optcopy(struct ip *, struct ip *);
206 u_int	 ip_optlen(struct inpcb *);
207 int	 ip_output(struct mbuf *, struct mbuf *, struct route *, int,
208 	    struct ip_moptions *, struct inpcb *);
209 int	 ip_fragment(struct mbuf *, struct ifnet *, u_long);
210 
211 void	 ip_reass_init(void);
212 int	 ip_reass_packet(struct mbuf **);
213 void	 ip_reass_slowtimo(void);
214 void	 ip_reass_drain(void);
215 
216 void	 ip_savecontrol(struct inpcb *, struct mbuf **, struct ip *,
217 	   struct mbuf *);
218 void	 ip_slowtimo(void);
219 void	 ip_fasttimo(void);
220 struct mbuf *
221 	 ip_srcroute(struct mbuf *);
222 int	 ip_sysctl(int *, u_int, void *, size_t *, void *, size_t);
223 void	 ip_statinc(u_int);
224 void *	 rip_ctlinput(int, const struct sockaddr *, void *);
225 int	 rip_ctloutput(int, struct socket *, struct sockopt *);
226 void	 rip_init(void);
227 void	 rip_input(struct mbuf *, ...);
228 int	 rip_output(struct mbuf *, struct inpcb *, struct mbuf *, struct lwp *);
229 int	 rip_usrreq(struct socket *,
230 	    int, struct mbuf *, struct mbuf *, struct mbuf *, struct lwp *);
231 
232 int	ip_setmoptions(struct ip_moptions **, const struct sockopt *sopt);
233 int	ip_getmoptions(struct ip_moptions *, struct sockopt *sopt);
234 
235 int	ip_if_output(struct ifnet * const, struct mbuf * const,
236 	    const struct sockaddr * const, const struct rtentry *);
237 
238 /* IP Flow interface. */
239 void	ipflow_init(void);
240 void	ipflow_poolinit(void);
241 void	ipflow_create(struct route *, struct mbuf *);
242 void	ipflow_slowtimo(void);
243 int	ipflow_invalidate_all(int);
244 
245 #endif  /* _KERNEL */
246 
247 #endif /* !_NETINET_IP_VAR_H_ */
248