1139823Simp/*-
21541Srgrimes * Copyright (c) 1982, 1986, 1993
31541Srgrimes *	The Regents of the University of California.  All rights reserved.
41541Srgrimes *
51541Srgrimes * Redistribution and use in source and binary forms, with or without
61541Srgrimes * modification, are permitted provided that the following conditions
71541Srgrimes * are met:
81541Srgrimes * 1. Redistributions of source code must retain the above copyright
91541Srgrimes *    notice, this list of conditions and the following disclaimer.
101541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111541Srgrimes *    notice, this list of conditions and the following disclaimer in the
121541Srgrimes *    documentation and/or other materials provided with the distribution.
131541Srgrimes * 4. Neither the name of the University nor the names of its contributors
141541Srgrimes *    may be used to endorse or promote products derived from this software
151541Srgrimes *    without specific prior written permission.
161541Srgrimes *
171541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271541Srgrimes * SUCH DAMAGE.
281541Srgrimes *
2910942Swollman *	@(#)ip_var.h	8.2 (Berkeley) 1/9/95
3050477Speter * $FreeBSD$
311541Srgrimes */
321541Srgrimes
332169Spaul#ifndef _NETINET_IP_VAR_H_
3418940Sbde#define	_NETINET_IP_VAR_H_
352169Spaul
3678064Sume#include <sys/queue.h>
3778064Sume
381541Srgrimes/*
391541Srgrimes * Overlay for ip header used by other protocols (tcp, udp).
401541Srgrimes */
411541Srgrimesstruct ipovly {
4238513Sdfr	u_char	ih_x1[9];		/* (unused) */
431541Srgrimes	u_char	ih_pr;			/* protocol */
4419183Sfenner	u_short	ih_len;			/* protocol length */
451541Srgrimes	struct	in_addr ih_src;		/* source internet address */
461541Srgrimes	struct	in_addr ih_dst;		/* destination internet address */
471541Srgrimes};
481541Srgrimes
49100419Srwatson#ifdef _KERNEL
501541Srgrimes/*
511541Srgrimes * Ip reassembly queue structure.  Each fragment
521541Srgrimes * being reassembled is attached to one of these structures.
531541Srgrimes * They are timed out after ipq_ttl drops to 0, and may also
541541Srgrimes * be reclaimed if memory becomes tight.
551541Srgrimes */
561541Srgrimesstruct ipq {
5774362Sphk	TAILQ_ENTRY(ipq) ipq_list;	/* to other reass headers */
581541Srgrimes	u_char	ipq_ttl;		/* time for reass q to live */
591541Srgrimes	u_char	ipq_p;			/* protocol of this fragment */
601541Srgrimes	u_short	ipq_id;			/* sequence id for reassembly */
6138513Sdfr	struct mbuf *ipq_frags;		/* to ip headers of fragments */
621541Srgrimes	struct	in_addr ipq_src,ipq_dst;
63111244Ssilby	u_char	ipq_nfrags;		/* # frags in this packet */
64168365Sandre	struct label *ipq_label;	/* MAC label */
651541Srgrimes};
66100419Srwatson#endif /* _KERNEL */
671541Srgrimes
681541Srgrimes/*
69152608Sandre * Structure stored in mbuf in inpcb.ip_options
70152608Sandre * and passed to ip_output when ip options are in use.
71152608Sandre * The actual length of the options (including ipopt_dst)
72152608Sandre * is in m_len.
73152608Sandre */
74152608Sandre#define MAX_IPOPTLEN	40
75152608Sandre
76152608Sandrestruct ipoption {
77152608Sandre	struct	in_addr ipopt_dst;	/* first-hop dst if source routed */
78152608Sandre	char	ipopt_list[MAX_IPOPTLEN];	/* options proper */
79152608Sandre};
80152608Sandre
81152608Sandre/*
821541Srgrimes * Structure attached to inpcb.ip_moptions and
831541Srgrimes * passed to ip_output when IP multicast options are in use.
84170613Sbms * This structure is lazy-allocated.
851541Srgrimes */
861541Srgrimesstruct ip_moptions {
871541Srgrimes	struct	ifnet *imo_multicast_ifp; /* ifp for outgoing multicasts */
8878064Sume	struct in_addr imo_multicast_addr; /* ifindex/addr on MULTICAST_IF */
89158563Sbms	u_long	imo_multicast_vif;	/* vif num outgoing multicasts */
901541Srgrimes	u_char	imo_multicast_ttl;	/* TTL for outgoing multicasts */
911541Srgrimes	u_char	imo_multicast_loop;	/* 1 => hear sends if a member */
921541Srgrimes	u_short	imo_num_memberships;	/* no. memberships this socket */
93158563Sbms	u_short	imo_max_memberships;	/* max memberships this socket */
94158563Sbms	struct	in_multi **imo_membership;	/* group memberships */
95170613Sbms	struct	in_mfilter *imo_mfilters;	/* source filters */
96228969Sjhb	STAILQ_ENTRY(ip_moptions) imo_link;
971541Srgrimes};
981541Srgrimes
991541Srgrimesstruct	ipstat {
100249276Sglebius	uint64_t ips_total;		/* total packets received */
101249276Sglebius	uint64_t ips_badsum;		/* checksum bad */
102249276Sglebius	uint64_t ips_tooshort;		/* packet too short */
103249276Sglebius	uint64_t ips_toosmall;		/* not enough data */
104249276Sglebius	uint64_t ips_badhlen;		/* ip header length < data size */
105249276Sglebius	uint64_t ips_badlen;		/* ip length < ip header length */
106249276Sglebius	uint64_t ips_fragments;		/* fragments received */
107249276Sglebius	uint64_t ips_fragdropped;	/* frags dropped (dups, out of space) */
108249276Sglebius	uint64_t ips_fragtimeout;	/* fragments timed out */
109249276Sglebius	uint64_t ips_forward;		/* packets forwarded */
110249276Sglebius	uint64_t ips_fastforward;	/* packets fast forwarded */
111249276Sglebius	uint64_t ips_cantforward;	/* packets rcvd for unreachable dest */
112249276Sglebius	uint64_t ips_redirectsent;	/* packets forwarded on same net */
113249276Sglebius	uint64_t ips_noproto;		/* unknown or unsupported protocol */
114249276Sglebius	uint64_t ips_delivered;		/* datagrams delivered to upper level*/
115249276Sglebius	uint64_t ips_localout;		/* total ip packets generated here */
116249276Sglebius	uint64_t ips_odropped;		/* lost packets due to nobufs, etc. */
117249276Sglebius	uint64_t ips_reassembled;	/* total packets reassembled ok */
118249276Sglebius	uint64_t ips_fragmented;	/* datagrams successfully fragmented */
119249276Sglebius	uint64_t ips_ofragments;	/* output fragments created */
120249276Sglebius	uint64_t ips_cantfrag;		/* don't fragment flag was set, etc. */
121249276Sglebius	uint64_t ips_badoptions;		/* error in option processing */
122249276Sglebius	uint64_t ips_noroute;		/* packets discarded due to no route */
123249276Sglebius	uint64_t ips_badvers;		/* ip version != 4 */
124249276Sglebius	uint64_t ips_rawout;		/* total raw ip packets generated */
125249276Sglebius	uint64_t ips_toolong;		/* ip length > max ip packet size */
126249276Sglebius	uint64_t ips_notmember;		/* multicasts for unregistered grps */
127249276Sglebius	uint64_t ips_nogif;		/* no match gif found */
128249276Sglebius	uint64_t ips_badaddr;		/* invalid address on header */
1291541Srgrimes};
1301541Srgrimes
13155205Speter#ifdef _KERNEL
13237625Sbde
133249276Sglebius#include <sys/counter.h>
134195699Srwatson#include <net/vnet.h>
135195699Srwatson
136253083SaeVNET_PCPUSTAT_DECLARE(struct ipstat, ipstat);
137196039Srwatson/*
138196039Srwatson * In-kernel consumers can use these accessor macros directly to update
139196039Srwatson * stats.
140196039Srwatson */
141253083Sae#define	IPSTAT_ADD(name, val)	\
142253083Sae    VNET_PCPUSTAT_ADD(struct ipstat, ipstat, name, (val))
143249411Sae#define	IPSTAT_SUB(name, val)	IPSTAT_ADD(name, -(val))
144190951Srwatson#define	IPSTAT_INC(name)	IPSTAT_ADD(name, 1)
145249411Sae#define	IPSTAT_DEC(name)	IPSTAT_SUB(name, 1)
146190951Srwatson
147196039Srwatson/*
148196039Srwatson * Kernel module consumers must use this accessor macro.
149196039Srwatson */
150196039Srwatsonvoid	kmod_ipstat_inc(int statnum);
151253083Sae#define	KMOD_IPSTAT_INC(name)	\
152253083Sae    kmod_ipstat_inc(offsetof(struct ipstat, name) / sizeof(uint64_t))
153196039Srwatsonvoid	kmod_ipstat_dec(int statnum);
154253083Sae#define	KMOD_IPSTAT_DEC(name)	\
155253083Sae    kmod_ipstat_dec(offsetof(struct ipstat, name) / sizeof(uint64_t))
156196039Srwatson
157170613Sbms/* flags passed to ip_output as last parameter */
158170613Sbms#define	IP_FORWARDING		0x1		/* most of ip header exists */
159170613Sbms#define	IP_RAWOUTPUT		0x2		/* raw ip header exists */
160170613Sbms#define	IP_SENDONES		0x4		/* send all-ones broadcast */
161170613Sbms#define	IP_SENDTOIF		0x8		/* send on specific ifnet */
162168365Sandre#define IP_ROUTETOIF		SO_DONTROUTE	/* 0x10 bypass routing tables */
163168365Sandre#define IP_ALLOWBROADCAST	SO_BROADCAST	/* 0x20 can send broadcast packets */
1641541Srgrimes
165147744Sthompsa#ifdef __NO_STRICT_ALIGNMENT
166147744Sthompsa#define IP_HDR_ALIGNED_P(ip)	1
167147744Sthompsa#else
168147744Sthompsa#define IP_HDR_ALIGNED_P(ip)	((((intptr_t) (ip)) & 3) == 0)
169147744Sthompsa#endif
170147744Sthompsa
17138482Swollmanstruct ip;
17219669Sbdestruct inpcb;
17318940Sbdestruct route;
17438482Swollmanstruct sockopt;
17518940Sbde
176195699SrwatsonVNET_DECLARE(u_short, ip_id);			/* ip packet ctr, for ids */
177195699SrwatsonVNET_DECLARE(int, ip_defttl);			/* default IP ttl */
178195699SrwatsonVNET_DECLARE(int, ipforwarding);		/* ip forwarding */
179122723Sandre#ifdef IPSTEALTH
180195699SrwatsonVNET_DECLARE(int, ipstealth);			/* stealth forwarding */
181122723Sandre#endif
182207369Sbzextern u_char	ip_protox[];
183195699SrwatsonVNET_DECLARE(struct socket *, ip_rsvpd);	/* reservation protocol daemon*/
184195699SrwatsonVNET_DECLARE(struct socket *, ip_mrouter);	/* multicast routing daemon */
185207369Sbzextern int	(*legal_vif_num)(int);
186207369Sbzextern u_long	(*ip_mcast_src)(int);
187207369SbzVNET_DECLARE(int, rsvp_on);
188241406SmelifaroVNET_DECLARE(int, drop_redirect);
189207369Sbzextern struct	pr_usrreqs rip_usrreqs;
190193502Sluigi
191195727Srwatson#define	V_ip_id			VNET(ip_id)
192195727Srwatson#define	V_ip_defttl		VNET(ip_defttl)
193195727Srwatson#define	V_ipforwarding		VNET(ipforwarding)
194195699Srwatson#ifdef IPSTEALTH
195195727Srwatson#define	V_ipstealth		VNET(ipstealth)
196195699Srwatson#endif
197195727Srwatson#define	V_ip_rsvpd		VNET(ip_rsvpd)
198195727Srwatson#define	V_ip_mrouter		VNET(ip_mrouter)
199207369Sbz#define	V_rsvp_on		VNET(rsvp_on)
200241406Smelifaro#define	V_drop_redirect		VNET(drop_redirect)
201195699Srwatson
202170613Sbmsvoid	inp_freemoptions(struct ip_moptions *);
203170613Sbmsint	inp_getmoptions(struct inpcb *, struct sockopt *);
204170613Sbmsint	inp_setmoptions(struct inpcb *, struct sockopt *);
205170613Sbms
206168365Sandreint	ip_ctloutput(struct socket *, struct sockopt *sopt);
207168365Sandrevoid	ip_drain(void);
208168365Sandreint	ip_fragment(struct ip *ip, struct mbuf **m_frag, int mtu,
209242161Sglebius	    u_long if_hwassist_flags);
210168365Sandrevoid	ip_forward(struct mbuf *m, int srcrt);
211168365Sandrevoid	ip_init(void);
212204140Sbz#ifdef VIMAGE
213204140Sbzvoid	ip_destroy(void);
214204140Sbz#endif
215168365Sandreextern int
216168365Sandre	(*ip_mforward)(struct ip *, struct ifnet *, struct mbuf *,
217168365Sandre	    struct ip_moptions *);
218168365Sandreint	ip_output(struct mbuf *,
219105194Ssam	    struct mbuf *, struct route *, int, struct ip_moptions *,
220105194Ssam	    struct inpcb *);
221212155Sbzint	ipproto_register(short);
222212155Sbzint	ipproto_unregister(short);
223133920Sandrestruct mbuf *
224168365Sandre	ip_reass(struct mbuf *);
22587120Srustruct in_ifaddr *
226178888Sjulian	ip_rtaddr(struct in_addr, u_int fibnum);
227168365Sandrevoid	ip_savecontrol(struct inpcb *, struct mbuf **, struct ip *,
228168365Sandre	    struct mbuf *);
229168365Sandrevoid	ip_slowtimo(void);
230133720Sdwmaloneu_int16_t	ip_randomid(void);
23198663Sluigiint	rip_ctloutput(struct socket *, struct sockopt *);
23298663Sluigivoid	rip_ctlinput(int, struct sockaddr *, void *);
23398663Sluigivoid	rip_init(void);
234193731Szec#ifdef VIMAGE
235193731Szecvoid	rip_destroy(void);
236193731Szec#endif
23798663Sluigivoid	rip_input(struct mbuf *, int);
23898663Sluigiint	rip_output(struct mbuf *, struct socket *, u_long);
23992723Salfredvoid	ipip_input(struct mbuf *, int);
24092723Salfredvoid	rsvp_input(struct mbuf *, int);
24192723Salfredint	ip_rsvp_init(struct socket *);
24292723Salfredint	ip_rsvp_done(void);
243106968Sluigiextern int	(*ip_rsvp_vif)(struct socket *, struct sockopt *);
244106968Sluigiextern void	(*ip_rsvp_force_done)(struct socket *);
245106968Sluigiextern void	(*rsvp_input_p)(struct mbuf *m, int off);
2467083Swollman
247197952SjulianVNET_DECLARE(struct pfil_head, inet_pfil_hook);	/* packet filter hooks */
248197952Sjulian#define	V_inet_pfil_hook	VNET(inet_pfil_hook)
249120386Ssam
25060765Sjlemonvoid	in_delayed_cksum(struct mbuf *m);
25160765Sjlemon
252201735Sluigi/* Hooks for ipfw, dummynet, divert etc. Most are declared in raw_ip.c */
253201735Sluigi/*
254201735Sluigi * Reference to an ipfw or packet filter rule that can be carried
255201735Sluigi * outside critical sections.
256201735Sluigi * A rule is identified by rulenum:rule_id which is ordered.
257201735Sluigi * In version chain_id the rule can be found in slot 'slot', so
258201735Sluigi * we don't need a lookup if chain_id == chain->id.
259201735Sluigi *
260201735Sluigi * On exit from the firewall this structure refers to the rule after
261201735Sluigi * the matching one (slot points to the new rule; rulenum:rule_id-1
262201735Sluigi * is the matching rule), and additional info (e.g. info often contains
263201735Sluigi * the insn argument or tablearg in the low 16 bits, in host format).
264201735Sluigi * On entry, the structure is valid if slot>0, and refers to the starting
265201735Sluigi * rules. 'info' contains the reason for reinject, e.g. divert port,
266201735Sluigi * divert direction, and so on.
267201735Sluigi */
268201735Sluigistruct ipfw_rule_ref {
269201735Sluigi	uint32_t	slot;		/* slot for matching rule	*/
270201735Sluigi	uint32_t	rulenum;	/* matching rule number		*/
271201735Sluigi	uint32_t	rule_id;	/* matching rule id		*/
272201735Sluigi	uint32_t	chain_id;	/* ruleset id			*/
273201735Sluigi	uint32_t	info;		/* see below			*/
274201735Sluigi};
275201735Sluigi
276201735Sluigienum {
277201735Sluigi	IPFW_INFO_MASK	= 0x0000ffff,
278201735Sluigi	IPFW_INFO_OUT	= 0x00000000,	/* outgoing, just for convenience */
279201735Sluigi	IPFW_INFO_IN	= 0x80000000,	/* incoming, overloads dir */
280201735Sluigi	IPFW_ONEPASS	= 0x40000000,	/* One-pass, do not reinject */
281201735Sluigi	IPFW_IS_MASK	= 0x30000000,	/* which source ? */
282201735Sluigi	IPFW_IS_DIVERT	= 0x20000000,
283201735Sluigi	IPFW_IS_DUMMYNET =0x10000000,
284201735Sluigi	IPFW_IS_PIPE	= 0x08000000,	/* pip1=1, queue = 0 */
285201735Sluigi};
286201735Sluigi#define MTAG_IPFW	1148380143	/* IPFW-tagged cookie */
287201735Sluigi#define MTAG_IPFW_RULE	1262273568	/* rule reference */
288223666Sae#define	MTAG_IPFW_CALL	1308397630	/* call stack */
289201735Sluigi
290193502Sluigistruct ip_fw_args;
291197952Sjuliantypedef int	(*ip_fw_chk_ptr_t)(struct ip_fw_args *args);
292197952Sjuliantypedef int	(*ip_fw_ctl_ptr_t)(struct sockopt *);
293197952SjulianVNET_DECLARE(ip_fw_ctl_ptr_t, ip_fw_ctl_ptr);
294197952Sjulian#define	V_ip_fw_ctl_ptr		VNET(ip_fw_ctl_ptr)
295197952Sjulian
296201735Sluigi/* Divert hooks. */
297201735Sluigiextern void	(*ip_divert_ptr)(struct mbuf *m, int incoming);
298201735Sluigi/* ng_ipfw hooks -- XXX make it the same as divert and dummynet */
299201735Sluigiextern int	(*ng_ipfw_input_p)(struct mbuf **, int,
300201735Sluigi			struct ip_fw_args *, int);
301201735Sluigi
302193502Sluigiextern int	(*ip_dn_ctl_ptr)(struct sockopt *);
303201735Sluigiextern int	(*ip_dn_io_ptr)(struct mbuf **, int, struct ip_fw_args *);
304195699Srwatson
305195699SrwatsonVNET_DECLARE(int, ip_do_randomid);
306195727Srwatson#define	V_ip_do_randomid	VNET(ip_do_randomid)
307195699Srwatson#define	ip_newid()	((V_ip_do_randomid != 0) ? ip_randomid() : \
308195699Srwatson			    htons(V_ip_id++))
309195699Srwatson
31055205Speter#endif /* _KERNEL */
31117072Sjulian
31237625Sbde#endif /* !_NETINET_IP_VAR_H_ */
313