ip_var.h revision 241406
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: head/sys/netinet/ip_var.h 241406 2012-10-10 19:06:11Z melifaro $
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 {
1001541Srgrimes	u_long	ips_total;		/* total packets received */
1011541Srgrimes	u_long	ips_badsum;		/* checksum bad */
1021541Srgrimes	u_long	ips_tooshort;		/* packet too short */
1031541Srgrimes	u_long	ips_toosmall;		/* not enough data */
1041541Srgrimes	u_long	ips_badhlen;		/* ip header length < data size */
1051541Srgrimes	u_long	ips_badlen;		/* ip length < ip header length */
1061541Srgrimes	u_long	ips_fragments;		/* fragments received */
1071541Srgrimes	u_long	ips_fragdropped;	/* frags dropped (dups, out of space) */
1081541Srgrimes	u_long	ips_fragtimeout;	/* fragments timed out */
1091541Srgrimes	u_long	ips_forward;		/* packets forwarded */
11036192Sdg	u_long	ips_fastforward;	/* packets fast forwarded */
1111541Srgrimes	u_long	ips_cantforward;	/* packets rcvd for unreachable dest */
1121541Srgrimes	u_long	ips_redirectsent;	/* packets forwarded on same net */
1131541Srgrimes	u_long	ips_noproto;		/* unknown or unsupported protocol */
1141541Srgrimes	u_long	ips_delivered;		/* datagrams delivered to upper level*/
1151541Srgrimes	u_long	ips_localout;		/* total ip packets generated here */
1161541Srgrimes	u_long	ips_odropped;		/* lost packets due to nobufs, etc. */
1171541Srgrimes	u_long	ips_reassembled;	/* total packets reassembled ok */
11813765Smpp	u_long	ips_fragmented;		/* datagrams successfully fragmented */
1191541Srgrimes	u_long	ips_ofragments;		/* output fragments created */
1201541Srgrimes	u_long	ips_cantfrag;		/* don't fragment flag was set, etc. */
1211541Srgrimes	u_long	ips_badoptions;		/* error in option processing */
1221541Srgrimes	u_long	ips_noroute;		/* packets discarded due to no route */
1231541Srgrimes	u_long	ips_badvers;		/* ip version != 4 */
1241541Srgrimes	u_long	ips_rawout;		/* total raw ip packets generated */
12519183Sfenner	u_long	ips_toolong;		/* ip length > max ip packet size */
12621932Swollman	u_long	ips_notmember;		/* multicasts for unregistered grps */
12752904Sshin	u_long	ips_nogif;		/* no match gif found */
12878064Sume	u_long	ips_badaddr;		/* invalid address on header */
1291541Srgrimes};
1301541Srgrimes
13155205Speter#ifdef _KERNEL
13237625Sbde
133195699Srwatson#include <net/vnet.h>
134195699Srwatson
135196039Srwatson/*
136196039Srwatson * In-kernel consumers can use these accessor macros directly to update
137196039Srwatson * stats.
138196039Srwatson */
139190951Srwatson#define	IPSTAT_ADD(name, val)	V_ipstat.name += (val)
140190951Srwatson#define	IPSTAT_SUB(name, val)	V_ipstat.name -= (val)
141190951Srwatson#define	IPSTAT_INC(name)	IPSTAT_ADD(name, 1)
142190951Srwatson#define	IPSTAT_DEC(name)	IPSTAT_SUB(name, 1)
143190951Srwatson
144196039Srwatson/*
145196039Srwatson * Kernel module consumers must use this accessor macro.
146196039Srwatson */
147196039Srwatsonvoid	kmod_ipstat_inc(int statnum);
148196039Srwatson#define	KMOD_IPSTAT_INC(name)						\
149196039Srwatson	kmod_ipstat_inc(offsetof(struct ipstat, name) / sizeof(u_long))
150196039Srwatsonvoid	kmod_ipstat_dec(int statnum);
151196039Srwatson#define	KMOD_IPSTAT_DEC(name)						\
152196039Srwatson	kmod_ipstat_dec(offsetof(struct ipstat, name) / sizeof(u_long))
153196039Srwatson
154170613Sbms/* flags passed to ip_output as last parameter */
155170613Sbms#define	IP_FORWARDING		0x1		/* most of ip header exists */
156170613Sbms#define	IP_RAWOUTPUT		0x2		/* raw ip header exists */
157170613Sbms#define	IP_SENDONES		0x4		/* send all-ones broadcast */
158170613Sbms#define	IP_SENDTOIF		0x8		/* send on specific ifnet */
159168365Sandre#define IP_ROUTETOIF		SO_DONTROUTE	/* 0x10 bypass routing tables */
160168365Sandre#define IP_ALLOWBROADCAST	SO_BROADCAST	/* 0x20 can send broadcast packets */
1611541Srgrimes
162168365Sandre/*
163168365Sandre * mbuf flag used by ip_fastfwd
164168365Sandre */
165126239Smlaier#define	M_FASTFWD_OURS		M_PROTO1	/* changed dst to local */
166126239Smlaier
167147744Sthompsa#ifdef __NO_STRICT_ALIGNMENT
168147744Sthompsa#define IP_HDR_ALIGNED_P(ip)	1
169147744Sthompsa#else
170147744Sthompsa#define IP_HDR_ALIGNED_P(ip)	((((intptr_t) (ip)) & 3) == 0)
171147744Sthompsa#endif
172147744Sthompsa
17338482Swollmanstruct ip;
17419669Sbdestruct inpcb;
17518940Sbdestruct route;
17638482Swollmanstruct sockopt;
17718940Sbde
178195699SrwatsonVNET_DECLARE(struct ipstat, ipstat);
179195699SrwatsonVNET_DECLARE(u_short, ip_id);			/* ip packet ctr, for ids */
180195699SrwatsonVNET_DECLARE(int, ip_defttl);			/* default IP ttl */
181195699SrwatsonVNET_DECLARE(int, ipforwarding);		/* ip forwarding */
182122723Sandre#ifdef IPSTEALTH
183195699SrwatsonVNET_DECLARE(int, ipstealth);			/* stealth forwarding */
184122723Sandre#endif
185207369Sbzextern u_char	ip_protox[];
186195699SrwatsonVNET_DECLARE(struct socket *, ip_rsvpd);	/* reservation protocol daemon*/
187195699SrwatsonVNET_DECLARE(struct socket *, ip_mrouter);	/* multicast routing daemon */
188207369Sbzextern int	(*legal_vif_num)(int);
189207369Sbzextern u_long	(*ip_mcast_src)(int);
190207369SbzVNET_DECLARE(int, rsvp_on);
191241406SmelifaroVNET_DECLARE(int, drop_redirect);
192207369Sbzextern struct	pr_usrreqs rip_usrreqs;
193193502Sluigi
194195727Srwatson#define	V_ipstat		VNET(ipstat)
195195727Srwatson#define	V_ip_id			VNET(ip_id)
196195727Srwatson#define	V_ip_defttl		VNET(ip_defttl)
197195727Srwatson#define	V_ipforwarding		VNET(ipforwarding)
198195699Srwatson#ifdef IPSTEALTH
199195727Srwatson#define	V_ipstealth		VNET(ipstealth)
200195699Srwatson#endif
201195727Srwatson#define	V_ip_rsvpd		VNET(ip_rsvpd)
202195727Srwatson#define	V_ip_mrouter		VNET(ip_mrouter)
203207369Sbz#define	V_rsvp_on		VNET(rsvp_on)
204241406Smelifaro#define	V_drop_redirect		VNET(drop_redirect)
205195699Srwatson
206170613Sbmsvoid	inp_freemoptions(struct ip_moptions *);
207170613Sbmsint	inp_getmoptions(struct inpcb *, struct sockopt *);
208170613Sbmsint	inp_setmoptions(struct inpcb *, struct sockopt *);
209170613Sbms
210168365Sandreint	ip_ctloutput(struct socket *, struct sockopt *sopt);
211168365Sandrevoid	ip_drain(void);
212168365Sandreint	ip_fragment(struct ip *ip, struct mbuf **m_frag, int mtu,
213118622Shsu	    u_long if_hwassist_flags, int sw_csum);
214168365Sandrevoid	ip_forward(struct mbuf *m, int srcrt);
215168365Sandrevoid	ip_init(void);
216204140Sbz#ifdef VIMAGE
217204140Sbzvoid	ip_destroy(void);
218204140Sbz#endif
219168365Sandreextern int
220168365Sandre	(*ip_mforward)(struct ip *, struct ifnet *, struct mbuf *,
221168365Sandre	    struct ip_moptions *);
222168365Sandreint	ip_output(struct mbuf *,
223105194Ssam	    struct mbuf *, struct route *, int, struct ip_moptions *,
224105194Ssam	    struct inpcb *);
225212155Sbzint	ipproto_register(short);
226212155Sbzint	ipproto_unregister(short);
227133920Sandrestruct mbuf *
228168365Sandre	ip_reass(struct mbuf *);
22987120Srustruct in_ifaddr *
230178888Sjulian	ip_rtaddr(struct in_addr, u_int fibnum);
231168365Sandrevoid	ip_savecontrol(struct inpcb *, struct mbuf **, struct ip *,
232168365Sandre	    struct mbuf *);
233168365Sandrevoid	ip_slowtimo(void);
234133720Sdwmaloneu_int16_t	ip_randomid(void);
23598663Sluigiint	rip_ctloutput(struct socket *, struct sockopt *);
23698663Sluigivoid	rip_ctlinput(int, struct sockaddr *, void *);
23798663Sluigivoid	rip_init(void);
238193731Szec#ifdef VIMAGE
239193731Szecvoid	rip_destroy(void);
240193731Szec#endif
24198663Sluigivoid	rip_input(struct mbuf *, int);
24298663Sluigiint	rip_output(struct mbuf *, struct socket *, u_long);
24392723Salfredvoid	ipip_input(struct mbuf *, int);
24492723Salfredvoid	rsvp_input(struct mbuf *, int);
24592723Salfredint	ip_rsvp_init(struct socket *);
24692723Salfredint	ip_rsvp_done(void);
247106968Sluigiextern int	(*ip_rsvp_vif)(struct socket *, struct sockopt *);
248106968Sluigiextern void	(*ip_rsvp_force_done)(struct socket *);
249106968Sluigiextern void	(*rsvp_input_p)(struct mbuf *m, int off);
2507083Swollman
251197952SjulianVNET_DECLARE(struct pfil_head, inet_pfil_hook);	/* packet filter hooks */
252197952Sjulian#define	V_inet_pfil_hook	VNET(inet_pfil_hook)
253120386Ssam
25460765Sjlemonvoid	in_delayed_cksum(struct mbuf *m);
25560765Sjlemon
256201735Sluigi/* Hooks for ipfw, dummynet, divert etc. Most are declared in raw_ip.c */
257201735Sluigi/*
258201735Sluigi * Reference to an ipfw or packet filter rule that can be carried
259201735Sluigi * outside critical sections.
260201735Sluigi * A rule is identified by rulenum:rule_id which is ordered.
261201735Sluigi * In version chain_id the rule can be found in slot 'slot', so
262201735Sluigi * we don't need a lookup if chain_id == chain->id.
263201735Sluigi *
264201735Sluigi * On exit from the firewall this structure refers to the rule after
265201735Sluigi * the matching one (slot points to the new rule; rulenum:rule_id-1
266201735Sluigi * is the matching rule), and additional info (e.g. info often contains
267201735Sluigi * the insn argument or tablearg in the low 16 bits, in host format).
268201735Sluigi * On entry, the structure is valid if slot>0, and refers to the starting
269201735Sluigi * rules. 'info' contains the reason for reinject, e.g. divert port,
270201735Sluigi * divert direction, and so on.
271201735Sluigi */
272201735Sluigistruct ipfw_rule_ref {
273201735Sluigi	uint32_t	slot;		/* slot for matching rule	*/
274201735Sluigi	uint32_t	rulenum;	/* matching rule number		*/
275201735Sluigi	uint32_t	rule_id;	/* matching rule id		*/
276201735Sluigi	uint32_t	chain_id;	/* ruleset id			*/
277201735Sluigi	uint32_t	info;		/* see below			*/
278201735Sluigi};
279201735Sluigi
280201735Sluigienum {
281201735Sluigi	IPFW_INFO_MASK	= 0x0000ffff,
282201735Sluigi	IPFW_INFO_OUT	= 0x00000000,	/* outgoing, just for convenience */
283201735Sluigi	IPFW_INFO_IN	= 0x80000000,	/* incoming, overloads dir */
284201735Sluigi	IPFW_ONEPASS	= 0x40000000,	/* One-pass, do not reinject */
285201735Sluigi	IPFW_IS_MASK	= 0x30000000,	/* which source ? */
286201735Sluigi	IPFW_IS_DIVERT	= 0x20000000,
287201735Sluigi	IPFW_IS_DUMMYNET =0x10000000,
288201735Sluigi	IPFW_IS_PIPE	= 0x08000000,	/* pip1=1, queue = 0 */
289201735Sluigi};
290201735Sluigi#define MTAG_IPFW	1148380143	/* IPFW-tagged cookie */
291201735Sluigi#define MTAG_IPFW_RULE	1262273568	/* rule reference */
292223666Sae#define	MTAG_IPFW_CALL	1308397630	/* call stack */
293201735Sluigi
294193502Sluigistruct ip_fw_args;
295197952Sjuliantypedef int	(*ip_fw_chk_ptr_t)(struct ip_fw_args *args);
296197952Sjuliantypedef int	(*ip_fw_ctl_ptr_t)(struct sockopt *);
297197952SjulianVNET_DECLARE(ip_fw_ctl_ptr_t, ip_fw_ctl_ptr);
298197952Sjulian#define	V_ip_fw_ctl_ptr		VNET(ip_fw_ctl_ptr)
299197952Sjulian
300201735Sluigi/* Divert hooks. */
301201735Sluigiextern void	(*ip_divert_ptr)(struct mbuf *m, int incoming);
302201735Sluigi/* ng_ipfw hooks -- XXX make it the same as divert and dummynet */
303201735Sluigiextern int	(*ng_ipfw_input_p)(struct mbuf **, int,
304201735Sluigi			struct ip_fw_args *, int);
305201735Sluigi
306193502Sluigiextern int	(*ip_dn_ctl_ptr)(struct sockopt *);
307201735Sluigiextern int	(*ip_dn_io_ptr)(struct mbuf **, int, struct ip_fw_args *);
308195699Srwatson
309195699SrwatsonVNET_DECLARE(int, ip_do_randomid);
310195727Srwatson#define	V_ip_do_randomid	VNET(ip_do_randomid)
311195699Srwatson#define	ip_newid()	((V_ip_do_randomid != 0) ? ip_randomid() : \
312195699Srwatson			    htons(V_ip_id++))
313195699Srwatson
31455205Speter#endif /* _KERNEL */
31517072Sjulian
31637625Sbde#endif /* !_NETINET_IP_VAR_H_ */
317