xref: /dragonfly/sys/net/ip6fw/ip6_fw.h (revision b40e316c)
1 /*	$FreeBSD: src/sys/netinet6/ip6_fw.h,v 1.3.2.7 2002/04/28 05:40:27 suz Exp $	*/
2 /*	$DragonFly: src/sys/net/ip6fw/ip6_fw.h,v 1.4 2004/01/06 03:17:26 dillon Exp $	*/
3 /*	$KAME: ip6_fw.h,v 1.9 2001/08/01 04:29:57 sumikawa Exp $	*/
4 
5 /*
6  * Copyright (C) 1998, 1999, 2000 and 2001 WIDE Project.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the project nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 /*
35  * Copyright (c) 1993 Daniel Boulet
36  * Copyright (c) 1994 Ugen J.S.Antsilevich
37  *
38  * Redistribution and use in source forms, with and without modification,
39  * are permitted provided that this entire comment appears intact.
40  *
41  * Redistribution in binary form may occur without any restrictions.
42  * Obviously, it would be nice if you gave credit where credit is due
43  * but requiring it would be too onerous.
44  *
45  * This software is provided ``AS IS'' without any warranties of any kind.
46  *
47  */
48 
49 #ifndef _IP6_FW_H
50 #define _IP6_FW_H
51 
52 #include <net/if.h>
53 
54 /*
55  * This union structure identifies an interface, either explicitly
56  * by name or implicitly by IP address. The flags IP_FW_F_IIFNAME
57  * and IP_FW_F_OIFNAME say how to interpret this structure. An
58  * interface unit number of -1 matches any unit number, while an
59  * IP address of 0.0.0.0 indicates matches any interface.
60  *
61  * The receive and transmit interfaces are only compared against the
62  * the packet if the corresponding bit (IP_FW_F_IIFACE or IP_FW_F_OIFACE)
63  * is set. Note some packets lack a receive or transmit interface
64  * (in which case the missing "interface" never matches).
65  */
66 
67 union ip6_fw_if {
68     struct in6_addr fu_via_ip6;	/* Specified by IPv6 address */
69     struct {			/* Specified by interface name */
70 #define IP6FW_IFNLEN     IFNAMSIZ
71 	    char  name[IP6FW_IFNLEN];
72 	    short glob;
73     } fu_via_if;
74 };
75 
76 /*
77  * Format of an IP firewall descriptor
78  *
79  * fw_src, fw_dst, fw_smsk, fw_dmsk are always stored in network byte order.
80  * fw_flg and fw_n*p are stored in host byte order (of course).
81  * Port numbers are stored in HOST byte order.
82  * Warning: setsockopt() will fail if sizeof(struct ip_fw) > MLEN (108)
83  */
84 
85 struct ip6_fw {
86     u_long fw_pcnt,fw_bcnt;		/* Packet and byte counters */
87     struct in6_addr fw_src, fw_dst;	/* Source and destination IPv6 addr */
88     struct in6_addr fw_smsk, fw_dmsk;	/* Mask for src and dest IPv6 addr */
89     u_short fw_number;			/* Rule number */
90     u_short fw_flg;			/* Flags word */
91 #define IPV6_FW_MAX_PORTS	10	/* A reasonable maximum */
92     u_int fw_ipflg;			/* IP flags word */
93     u_short fw_pts[IPV6_FW_MAX_PORTS];	/* Array of port numbers to match */
94     u_char fw_ip6opt,fw_ip6nopt;	/* IPv6 options set/unset */
95     u_char fw_tcpf,fw_tcpnf;		/* TCP flags set/unset */
96 #define IPV6_FW_ICMPTYPES_DIM (256 / (sizeof(unsigned) * 8))
97     unsigned fw_icmp6types[IPV6_FW_ICMPTYPES_DIM]; /* ICMP types bitmap */
98     long timestamp;			/* timestamp (tv_sec) of last match */
99     union ip6_fw_if fw_in_if, fw_out_if;/* Incoming and outgoing interfaces */
100     union {
101 	u_short fu_divert_port;		/* Divert/tee port (options IP6DIVERT) */
102 	u_short fu_skipto_rule;		/* SKIPTO command rule number */
103 	u_short fu_reject_code;		/* REJECT response code */
104     } fw_un;
105     u_char fw_prot;			/* IPv6 protocol */
106     u_char fw_nports;			/* N'of src ports and # of dst ports */
107 					/* in ports array (dst ports follow */
108 					/* src ports; max of 10 ports in all; */
109 					/* count of 0 means match all ports) */
110 };
111 
112 #define IPV6_FW_GETNSRCP(rule)		((rule)->fw_nports & 0x0f)
113 #define IPV6_FW_SETNSRCP(rule, n)		do {				\
114 					  (rule)->fw_nports &= ~0x0f;	\
115 					  (rule)->fw_nports |= (n);	\
116 					} while (0)
117 #define IPV6_FW_GETNDSTP(rule)		((rule)->fw_nports >> 4)
118 #define IPV6_FW_SETNDSTP(rule, n)		do {				\
119 					  (rule)->fw_nports &= ~0xf0;	\
120 					  (rule)->fw_nports |= (n) << 4;\
121 					} while (0)
122 
123 #define fw_divert_port	fw_un.fu_divert_port
124 #define fw_skipto_rule	fw_un.fu_skipto_rule
125 #define fw_reject_code	fw_un.fu_reject_code
126 
127 struct ip6_fw_chain {
128         LIST_ENTRY(ip6_fw_chain) chain;
129         struct ip6_fw    *rule;
130 };
131 
132 /*
133  * Values for "flags" field .
134  */
135 #define IPV6_FW_F_IN	0x0001	/* Check inbound packets		*/
136 #define IPV6_FW_F_OUT	0x0002	/* Check outbound packets		*/
137 #define IPV6_FW_F_IIFACE	0x0004	/* Apply inbound interface test		*/
138 #define IPV6_FW_F_OIFACE	0x0008	/* Apply outbound interface test	*/
139 
140 #define IPV6_FW_F_COMMAND 0x0070	/* Mask for type of chain entry:	*/
141 #define IPV6_FW_F_DENY	0x0000	/* This is a deny rule			*/
142 #define IPV6_FW_F_REJECT	0x0010	/* Deny and send a response packet	*/
143 #define IPV6_FW_F_ACCEPT	0x0020	/* This is an accept rule		*/
144 #define IPV6_FW_F_COUNT	0x0030	/* This is a count rule			*/
145 #define IPV6_FW_F_DIVERT	0x0040	/* This is a divert rule		*/
146 #define IPV6_FW_F_TEE	0x0050	/* This is a tee rule			*/
147 #define IPV6_FW_F_SKIPTO	0x0060	/* This is a skipto rule		*/
148 
149 #define IPV6_FW_F_PRN	0x0080	/* Print if this rule matches		*/
150 
151 #define IPV6_FW_F_SRNG	0x0100	/* The first two src ports are a min	*
152 				 * and max range (stored in host byte	*
153 				 * order).				*/
154 
155 #define IPV6_FW_F_DRNG	0x0200	/* The first two dst ports are a min	*
156 				 * and max range (stored in host byte	*
157 				 * order).				*/
158 
159 #define IPV6_FW_F_IIFNAME	0x0400	/* In interface by name/unit (not IP)	*/
160 #define IPV6_FW_F_OIFNAME	0x0800	/* Out interface by name/unit (not IP)	*/
161 
162 #define IPV6_FW_F_INVSRC	0x1000	/* Invert sense of src check		*/
163 #define IPV6_FW_F_INVDST	0x2000	/* Invert sense of dst check		*/
164 
165 #define IPV6_FW_F_FRAG	0x4000	/* Fragment				*/
166 
167 #define IPV6_FW_F_ICMPBIT 0x8000	/* ICMP type bitmap is valid		*/
168 
169 #define IPV6_FW_F_MASK	0xFFFF	/* All possible flag bits mask		*/
170 
171 /*
172  * Flags for the 'fw_ipflg' field, for comparing values of ip and its protocols. */
173 #define	IPV6_FW_IF_TCPEST 0x00000020	/* established TCP connection	*/
174 #define IPV6_FW_IF_TCPMSK 0x00000020	/* mask of all TCP values */
175 
176 /*
177  * For backwards compatibility with rules specifying "via iface" but
178  * not restricted to only "in" or "out" packets, we define this combination
179  * of bits to represent this configuration.
180  */
181 
182 #define IF6_FW_F_VIAHACK	(IPV6_FW_F_IN|IPV6_FW_F_OUT|IPV6_FW_F_IIFACE|IPV6_FW_F_OIFACE)
183 
184 /*
185  * Definitions for REJECT response codes.
186  * Values less than 256 correspond to ICMP unreachable codes.
187  */
188 #define IPV6_FW_REJECT_RST	0x0100		/* TCP packets: send RST */
189 
190 /*
191  * Definitions for IPv6 option names.
192  */
193 #define IPV6_FW_IP6OPT_HOPOPT	0x01
194 #define IPV6_FW_IP6OPT_ROUTE	0x02
195 #define IPV6_FW_IP6OPT_FRAG	0x04
196 #define IPV6_FW_IP6OPT_ESP	0x08
197 #define IPV6_FW_IP6OPT_AH	0x10
198 #define IPV6_FW_IP6OPT_NONXT	0x20
199 #define IPV6_FW_IP6OPT_OPTS	0x40
200 
201 /*
202  * Definitions for TCP flags.
203  */
204 #define IPV6_FW_TCPF_FIN	TH_FIN
205 #define IPV6_FW_TCPF_SYN	TH_SYN
206 #define IPV6_FW_TCPF_RST	TH_RST
207 #define IPV6_FW_TCPF_PSH	TH_PUSH
208 #define IPV6_FW_TCPF_ACK	TH_ACK
209 #define IPV6_FW_TCPF_URG	TH_URG
210 
211 /*
212  * Main firewall chains definitions and global var's definitions.
213  */
214 #ifdef _KERNEL
215 
216 /*
217  * Function definitions.
218  */
219 void ip6_fw_init(void);
220 
221 /* Firewall hooks */
222 struct ip6_hdr;
223 typedef	int ip6_fw_chk_t (struct ip6_hdr**, struct ifnet*,
224 				u_short *, struct mbuf**);
225 typedef	int ip6_fw_ctl_t (int, struct mbuf**);
226 extern	ip6_fw_chk_t *ip6_fw_chk_ptr;
227 extern	ip6_fw_ctl_t *ip6_fw_ctl_ptr;
228 extern	int ip6_fw_enable;
229 
230 #endif /* _KERNEL */
231 
232 #endif /* _IP6_FW_H */
233