in.h revision 19622
11541Srgrimes/*
21541Srgrimes * Copyright (c) 1982, 1986, 1990, 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 * 3. All advertising materials mentioning features or use of this software
141541Srgrimes *    must display the following acknowledgement:
151541Srgrimes *	This product includes software developed by the University of
161541Srgrimes *	California, Berkeley and its contributors.
171541Srgrimes * 4. Neither the name of the University nor the names of its contributors
181541Srgrimes *    may be used to endorse or promote products derived from this software
191541Srgrimes *    without specific prior written permission.
201541Srgrimes *
211541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311541Srgrimes * SUCH DAMAGE.
321541Srgrimes *
331541Srgrimes *	@(#)in.h	8.3 (Berkeley) 1/3/94
3419622Sfenner * $Id: in.h,v 1.22 1996/10/22 22:25:54 sos Exp $
351541Srgrimes */
361541Srgrimes
372169Spaul#ifndef _NETINET_IN_H_
382169Spaul#define _NETINET_IN_H_
392169Spaul
401541Srgrimes/*
411541Srgrimes * Constants and structures defined by the internet system,
421541Srgrimes * Per RFC 790, September 1981, and numerous additions.
431541Srgrimes */
441541Srgrimes
451541Srgrimes/*
461541Srgrimes * Protocols
471541Srgrimes */
481541Srgrimes#define	IPPROTO_IP		0		/* dummy for IP */
491541Srgrimes#define	IPPROTO_ICMP		1		/* control message protocol */
501541Srgrimes#define	IPPROTO_IGMP		2		/* group mgmt protocol */
511541Srgrimes#define	IPPROTO_GGP		3		/* gateway^2 (deprecated) */
529209Swollman#define IPPROTO_IPIP		4 		/* IP encapsulation in IP */
531541Srgrimes#define	IPPROTO_TCP		6		/* tcp */
541541Srgrimes#define	IPPROTO_EGP		8		/* exterior gateway protocol */
551541Srgrimes#define	IPPROTO_PUP		12		/* pup */
561541Srgrimes#define	IPPROTO_UDP		17		/* user datagram protocol */
571541Srgrimes#define	IPPROTO_IDP		22		/* xns idp */
581541Srgrimes#define	IPPROTO_TP		29 		/* tp-4 w/ class negotiation */
592531Swollman#define IPPROTO_RSVP		46 		/* resource reservation */
601541Srgrimes#define	IPPROTO_EON		80		/* ISO cnlp */
611541Srgrimes#define	IPPROTO_ENCAP		98		/* encapsulation header */
621541Srgrimes
6317072Sjulian#define	IPPROTO_DIVERT		254		/* divert pseudo-protocol */
641541Srgrimes#define	IPPROTO_RAW		255		/* raw IP packet */
651541Srgrimes#define	IPPROTO_MAX		256
661541Srgrimes
671541Srgrimes
681541Srgrimes/*
691541Srgrimes * Local port number conventions:
7014195Speter *
7114195Speter * When a user does a bind(2) or connect(2) with a port number of zero,
7214195Speter * a non-conflicting local port address is chosen.
7314195Speter * The default range is IPPORT_RESERVED through
7414195Speter * IPPORT_USERRESERVED, although that is settable by sysctl.
7514195Speter *
7614195Speter * A user may set the IPPROTO_IP option IP_PORTRANGE to change this
7714195Speter * default assignment range.
7814195Speter *
7914195Speter * The value IP_PORTRANGE_DEFAULT causes the default behavior.
8014195Speter *
8114195Speter * The value IP_PORTRANGE_HIGH changes the range of candidate port numbers
8214195Speter * into the "high" range.  These are reserved for client outbound connections
8314195Speter * which do not want to be filtered by any firewalls.
8414195Speter *
8514195Speter * The value IP_PORTRANGE_LOW changes the range to the "low" are
8614195Speter * that is (by convention) restricted to privileged processes.  This
8714195Speter * convention is based on "vouchsafe" principles only.  It is only secure
8814195Speter * if you trust the remote host to restrict these ports.
8914195Speter *
9014195Speter * The default range of ports and the high range can be changed by
9117541Speter * sysctl(3).  (net.inet.ip.port{hi,low}{first,last}_auto)
9214195Speter *
9314195Speter * Changing those values has bad security implications if you are
9414195Speter * using a a stateless firewall that is allowing packets outside of that
9514195Speter * range in order to allow transparent outgoing connections.
9614195Speter *
9714195Speter * Such a firewall configuration will generally depend on the use of these
9814195Speter * default values.  If you change them, you may find your Security
9914195Speter * Administrator looking for you with a heavy object.
10014195Speter */
10114195Speter
10214195Speter/*
1031541Srgrimes * Ports < IPPORT_RESERVED are reserved for
10414195Speter * privileged processes (e.g. root).         (IP_PORTRANGE_LOW)
1051541Srgrimes * Ports > IPPORT_USERRESERVED are reserved
10614195Speter * for servers, not necessarily privileged.  (IP_PORTRANGE_DEFAULT)
1071541Srgrimes */
1081541Srgrimes#define	IPPORT_RESERVED		1024
1091541Srgrimes#define	IPPORT_USERRESERVED	5000
1101541Srgrimes
1111541Srgrimes/*
11214195Speter * Default local port range to use by setting IP_PORTRANGE_HIGH
11313491Speter */
11414195Speter#define	IPPORT_HIFIRSTAUTO	40000
11514195Speter#define	IPPORT_HILASTAUTO	44999
11613491Speter
11713491Speter/*
11817541Speter * Scanning for a free reserved port return a value below IPPORT_RESERVED,
11917541Speter * but higher than IPPORT_RESERVEDSTART.  Traditionally the start value was
12017541Speter * 512, but that conflicts with some well-known-services that firewalls may
12117541Speter * have a fit if we use.
12217541Speter */
12317541Speter#define IPPORT_RESERVEDSTART	600
12417541Speter
12517541Speter/*
1261541Srgrimes * Internet address (a structure for historical reasons)
1271541Srgrimes */
1281541Srgrimesstruct in_addr {
1291541Srgrimes	u_long s_addr;
1301541Srgrimes};
1311541Srgrimes
1321541Srgrimes/*
1331541Srgrimes * Definitions of bits in internet address integers.
1341541Srgrimes * On subnets, the decomposition of addresses to host and net parts
1351541Srgrimes * is done according to subnet mask, not the masks here.
1361541Srgrimes */
1371541Srgrimes#define	IN_CLASSA(i)		(((long)(i) & 0x80000000) == 0)
1381541Srgrimes#define	IN_CLASSA_NET		0xff000000
1391541Srgrimes#define	IN_CLASSA_NSHIFT	24
1401541Srgrimes#define	IN_CLASSA_HOST		0x00ffffff
1411541Srgrimes#define	IN_CLASSA_MAX		128
1421541Srgrimes
1431541Srgrimes#define	IN_CLASSB(i)		(((long)(i) & 0xc0000000) == 0x80000000)
1441541Srgrimes#define	IN_CLASSB_NET		0xffff0000
1451541Srgrimes#define	IN_CLASSB_NSHIFT	16
1461541Srgrimes#define	IN_CLASSB_HOST		0x0000ffff
1471541Srgrimes#define	IN_CLASSB_MAX		65536
1481541Srgrimes
1491541Srgrimes#define	IN_CLASSC(i)		(((long)(i) & 0xe0000000) == 0xc0000000)
1501541Srgrimes#define	IN_CLASSC_NET		0xffffff00
1511541Srgrimes#define	IN_CLASSC_NSHIFT	8
1521541Srgrimes#define	IN_CLASSC_HOST		0x000000ff
1531541Srgrimes
1541541Srgrimes#define	IN_CLASSD(i)		(((long)(i) & 0xf0000000) == 0xe0000000)
1551541Srgrimes#define	IN_CLASSD_NET		0xf0000000	/* These ones aren't really */
1561541Srgrimes#define	IN_CLASSD_NSHIFT	28		/* net and host fields, but */
1571541Srgrimes#define	IN_CLASSD_HOST		0x0fffffff	/* routing needn't know.    */
1581541Srgrimes#define	IN_MULTICAST(i)		IN_CLASSD(i)
1591541Srgrimes
1601541Srgrimes#define	IN_EXPERIMENTAL(i)	(((long)(i) & 0xf0000000) == 0xf0000000)
1611541Srgrimes#define	IN_BADCLASS(i)		(((long)(i) & 0xf0000000) == 0xf0000000)
1621541Srgrimes
1631541Srgrimes#define	INADDR_ANY		(u_long)0x00000000
1641541Srgrimes#define	INADDR_BROADCAST	(u_long)0xffffffff	/* must be masked */
1651541Srgrimes#ifndef KERNEL
1661541Srgrimes#define	INADDR_NONE		0xffffffff		/* -1 return */
1671541Srgrimes#endif
1681541Srgrimes
1691541Srgrimes#define	INADDR_UNSPEC_GROUP	(u_long)0xe0000000	/* 224.0.0.0 */
1701541Srgrimes#define	INADDR_ALLHOSTS_GROUP	(u_long)0xe0000001	/* 224.0.0.1 */
17114622Sfenner#define	INADDR_ALLRTRS_GROUP	(u_long)0xe0000002	/* 224.0.0.2 */
1721541Srgrimes#define	INADDR_MAX_LOCAL_GROUP	(u_long)0xe00000ff	/* 224.0.0.255 */
1731541Srgrimes
1741541Srgrimes#define	IN_LOOPBACKNET		127			/* official! */
1751541Srgrimes
1761541Srgrimes/*
1771541Srgrimes * Socket address, internet style.
1781541Srgrimes */
1791541Srgrimesstruct sockaddr_in {
1801541Srgrimes	u_char	sin_len;
1811541Srgrimes	u_char	sin_family;
1821541Srgrimes	u_short	sin_port;
1831541Srgrimes	struct	in_addr sin_addr;
1841541Srgrimes	char	sin_zero[8];
1851541Srgrimes};
1861541Srgrimes
1871541Srgrimes/*
1881541Srgrimes * Structure used to describe IP options.
1891541Srgrimes * Used to store options internally, to pass them to a process,
1901541Srgrimes * or to restore options retrieved earlier.
1911541Srgrimes * The ip_dst is used for the first-hop gateway when using a source route
1921541Srgrimes * (this gets put into the header proper).
1931541Srgrimes */
1941541Srgrimesstruct ip_opts {
1951541Srgrimes	struct	in_addr ip_dst;		/* first hop, 0 w/o src rt */
1961541Srgrimes	char	ip_opts[40];		/* actually variable in size */
1971541Srgrimes};
1981541Srgrimes
1991541Srgrimes/*
2001541Srgrimes * Options for use with [gs]etsockopt at the IP level.
2011541Srgrimes * First word of comment is data type; bool is stored in int.
2021541Srgrimes */
2031541Srgrimes#define	IP_OPTIONS		1    /* buf/ip_opts; set/get IP options */
2041541Srgrimes#define	IP_HDRINCL		2    /* int; header is included with data */
2051541Srgrimes#define	IP_TOS			3    /* int; IP type of service and preced. */
2061541Srgrimes#define	IP_TTL			4    /* int; IP time to live */
2071541Srgrimes#define	IP_RECVOPTS		5    /* bool; receive all IP opts w/dgram */
2081541Srgrimes#define	IP_RECVRETOPTS		6    /* bool; receive IP opts for response */
2091541Srgrimes#define	IP_RECVDSTADDR		7    /* bool; receive IP dst addr w/dgram */
2101541Srgrimes#define	IP_RETOPTS		8    /* ip_opts; set/get IP options */
2111541Srgrimes#define	IP_MULTICAST_IF		9    /* u_char; set/get IP multicast i/f  */
2121541Srgrimes#define	IP_MULTICAST_TTL	10   /* u_char; set/get IP multicast ttl */
2131541Srgrimes#define	IP_MULTICAST_LOOP	11   /* u_char; set/get IP multicast loopback */
2141541Srgrimes#define	IP_ADD_MEMBERSHIP	12   /* ip_mreq; add an IP group membership */
2151541Srgrimes#define	IP_DROP_MEMBERSHIP	13   /* ip_mreq; drop an IP group membership */
2162531Swollman#define IP_MULTICAST_VIF	14   /* set/get IP mcast virt. iface */
2172531Swollman#define IP_RSVP_ON		15   /* enable RSVP in kernel */
2182531Swollman#define IP_RSVP_OFF		16   /* disable RSVP in kernel */
2199209Swollman#define IP_RSVP_VIF_ON		17   /* set RSVP per-vif socket */
2209209Swollman#define IP_RSVP_VIF_OFF		18   /* unset RSVP per-vif socket */
22114195Speter#define IP_PORTRANGE		19   /* int; range to choose for unspec port */
22219622Sfenner#define	IP_RECVIF		20   /* bool; receive reception if w/dgram */
2231541Srgrimes
22417758Ssos#define IP_FW_ADD     		50   /* add a firewall rule to chain */
22517758Ssos#define IP_FW_DEL    		51   /* delete a firewall rule from chain */
22617758Ssos#define IP_FW_FLUSH   		52   /* flush firewall rule chain */
22719035Salex#define IP_FW_ZERO    		53   /* clear single/all firewall counter(s) */
22817758Ssos#define IP_FW_GET     		54   /* get entire firewall rule chain */
22917758Ssos#define IP_NAT			55   /* set/get NAT opts */
23017758Ssos
2311541Srgrimes/*
2321541Srgrimes * Defaults and limits for options
2331541Srgrimes */
2341541Srgrimes#define	IP_DEFAULT_MULTICAST_TTL  1	/* normally limit m'casts to 1 hop  */
2351541Srgrimes#define	IP_DEFAULT_MULTICAST_LOOP 1	/* normally hear sends if a member  */
2369209Swollman#define	IP_MAX_MEMBERSHIPS	20	/* per socket */
2371541Srgrimes
2381541Srgrimes/*
2391541Srgrimes * Argument structure for IP_ADD_MEMBERSHIP and IP_DROP_MEMBERSHIP.
2401541Srgrimes */
2411541Srgrimesstruct ip_mreq {
2421541Srgrimes	struct	in_addr imr_multiaddr;	/* IP multicast address of group */
2431541Srgrimes	struct	in_addr imr_interface;	/* local IP address of interface */
2441541Srgrimes};
2451541Srgrimes
2461541Srgrimes/*
24714195Speter * Argument for IP_PORTRANGE:
24814195Speter * - which range to search when port is unspecified at bind() or connect()
24914195Speter */
25014195Speter#define	IP_PORTRANGE_DEFAULT	0	/* default range */
25114195Speter#define	IP_PORTRANGE_HIGH	1	/* "high" - request firewall bypass */
25214195Speter#define	IP_PORTRANGE_LOW	2	/* "low" - vouchsafe security */
25314195Speter
25414195Speter/*
2551541Srgrimes * Definitions for inet sysctl operations.
2561541Srgrimes *
2571541Srgrimes * Third level is protocol number.
2581541Srgrimes * Fourth level is desired variable within that protocol.
2591541Srgrimes */
2601541Srgrimes#define	IPPROTO_MAXID	(IPPROTO_IDP + 1)	/* don't list to IPPROTO_MAX */
2611541Srgrimes
2621541Srgrimes#define	CTL_IPPROTO_NAMES { \
2631541Srgrimes	{ "ip", CTLTYPE_NODE }, \
2641541Srgrimes	{ "icmp", CTLTYPE_NODE }, \
2651541Srgrimes	{ "igmp", CTLTYPE_NODE }, \
2661541Srgrimes	{ "ggp", CTLTYPE_NODE }, \
2671541Srgrimes	{ 0, 0 }, \
2681541Srgrimes	{ 0, 0 }, \
2691541Srgrimes	{ "tcp", CTLTYPE_NODE }, \
2701541Srgrimes	{ 0, 0 }, \
2711541Srgrimes	{ "egp", CTLTYPE_NODE }, \
2721541Srgrimes	{ 0, 0 }, \
2731541Srgrimes	{ 0, 0 }, \
2741541Srgrimes	{ 0, 0 }, \
2751541Srgrimes	{ "pup", CTLTYPE_NODE }, \
2761541Srgrimes	{ 0, 0 }, \
2771541Srgrimes	{ 0, 0 }, \
2781541Srgrimes	{ 0, 0 }, \
2791541Srgrimes	{ 0, 0 }, \
2801541Srgrimes	{ "udp", CTLTYPE_NODE }, \
2811541Srgrimes	{ 0, 0 }, \
2821541Srgrimes	{ 0, 0 }, \
2831541Srgrimes	{ 0, 0 }, \
2841541Srgrimes	{ 0, 0 }, \
2851541Srgrimes	{ "idp", CTLTYPE_NODE }, \
2861541Srgrimes}
2871541Srgrimes
2881541Srgrimes/*
2891541Srgrimes * Names for IP sysctl objects
2901541Srgrimes */
2911541Srgrimes#define	IPCTL_FORWARDING	1	/* act as router */
2921541Srgrimes#define	IPCTL_SENDREDIRECTS	2	/* may send redirects when forwarding */
2931541Srgrimes#define	IPCTL_DEFTTL		3	/* default TTL */
2941541Srgrimes#ifdef notyet
2951541Srgrimes#define	IPCTL_DEFMTU		4	/* default MTU */
2961541Srgrimes#endif
2975109Swollman#define IPCTL_RTEXPIRE		5	/* cloned route expiration time */
2986399Swollman#define IPCTL_RTMINEXPIRE	6	/* min value for expiration time */
2996399Swollman#define IPCTL_RTMAXCACHE	7	/* trigger level for dynamic expire */
3007091Swollman#define	IPCTL_SOURCEROUTE	8	/* may perform source routes */
3019575Speter#define	IPCTL_DIRECTEDBROADCAST	9	/* may re-broadcast received packets */
30212003Swollman#define IPCTL_INTRQMAXLEN	10	/* max length of netisr queue */
30312003Swollman#define IPCTL_INTRQDROPS	11	/* number of netisr q drops */
30412003Swollman#define	IPCTL_MAXID		12
3051541Srgrimes
3061541Srgrimes#define	IPCTL_NAMES { \
3071541Srgrimes	{ 0, 0 }, \
3081541Srgrimes	{ "forwarding", CTLTYPE_INT }, \
3091541Srgrimes	{ "redirect", CTLTYPE_INT }, \
3101541Srgrimes	{ "ttl", CTLTYPE_INT }, \
3111541Srgrimes	{ "mtu", CTLTYPE_INT }, \
3125109Swollman	{ "rtexpire", CTLTYPE_INT }, \
3136399Swollman	{ "rtminexpire", CTLTYPE_INT }, \
3146399Swollman	{ "rtmaxcache", CTLTYPE_INT }, \
3157091Swollman	{ "sourceroute", CTLTYPE_INT }, \
3169575Speter 	{ "directed-broadcast", CTLTYPE_INT }, \
31712003Swollman	{ "intr-queue-maxlen", CTLTYPE_INT }, \
31812003Swollman	{ "intr-queue-drops", CTLTYPE_INT }, \
3191541Srgrimes}
3201541Srgrimes
3211541Srgrimes
3221541Srgrimes#ifdef KERNEL
3237088Swollmanstruct ifnet; struct mbuf;	/* forward declarations for Standard C */
3247088Swollman
3251541Srgrimesint	 in_broadcast __P((struct in_addr, struct ifnet *));
3261541Srgrimesint	 in_canforward __P((struct in_addr));
3271541Srgrimesint	 in_cksum __P((struct mbuf *, int));
3281541Srgrimesint	 in_localaddr __P((struct in_addr));
3297088Swollmanchar 	*inet_ntoa __P((struct in_addr)); /* in libkern */
3302169Spaul
33115026Sphk/* Firewall hooks */
33215026Sphkstruct ip;
33317758Ssostypedef	int ip_fw_chk_t __P((struct ip**, int, struct ifnet*, int, struct mbuf**));
33417758Ssostypedef	int ip_fw_ctl_t __P((int, struct mbuf**));
33517758Ssosextern	ip_fw_chk_t *ip_fw_chk_ptr;
33617758Ssosextern	ip_fw_ctl_t *ip_fw_ctl_ptr;
33717758Ssos
33819035Salex/* IP NAT hooks */
33919113Ssostypedef	int ip_nat_t __P((struct ip**, struct mbuf**, struct ifnet*, int));
34017758Ssostypedef	int ip_nat_ctl_t __P((int, struct mbuf**));
34117758Ssosextern	ip_nat_t *ip_nat_ptr;
34217758Ssosextern	ip_nat_ctl_t *ip_nat_ctl_ptr;
34317758Ssos#define	IP_NAT_IN	0x00000001
34417758Ssos#define	IP_NAT_OUT	0x00000002
34517758Ssos
34615026Sphk#endif /* KERNEL */
34715026Sphk
3482169Spaul#endif
349