]>
Commit | Line | Data |
---|---|---|
9bccf70c | 1 | /* |
39236c6e | 2 | * Copyright (c) 2008-2013 Apple Inc. All rights reserved. |
9bccf70c | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
0a7de745 | 5 | * |
2d21ac55 A |
6 | * This file contains Original Code and/or Modifications of Original Code |
7 | * as defined in and that are subject to the Apple Public Source License | |
8 | * Version 2.0 (the 'License'). You may not use this file except in | |
9 | * compliance with the License. The rights granted to you under the License | |
10 | * may not be used to create, or enable the creation or redistribution of, | |
11 | * unlawful or unlicensed copies of an Apple operating system, or to | |
12 | * circumvent, violate, or enable the circumvention or violation of, any | |
13 | * terms of an Apple operating system software license agreement. | |
0a7de745 | 14 | * |
2d21ac55 A |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
0a7de745 | 17 | * |
2d21ac55 A |
18 | * The Original Code and all software distributed under the License are |
19 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
22 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
23 | * Please see the License for the specific language governing rights and | |
24 | * limitations under the License. | |
0a7de745 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
9bccf70c | 27 | */ |
1c79356b A |
28 | /* |
29 | * Copyright (c) 1993 Daniel Boulet | |
30 | * Copyright (c) 1994 Ugen J.S.Antsilevich | |
31 | * | |
32 | * Redistribution and use in source forms, with and without modification, | |
33 | * are permitted provided that this entire comment appears intact. | |
34 | * | |
35 | * Redistribution in binary form may occur without any restrictions. | |
36 | * Obviously, it would be nice if you gave credit where credit is due | |
37 | * but requiring it would be too onerous. | |
38 | * | |
39 | * This software is provided ``AS IS'' without any warranties of any kind. | |
40 | * | |
41 | */ | |
42 | ||
43 | #ifndef _IP6_FW_H | |
44 | #define _IP6_FW_H | |
6d2010ae | 45 | #ifdef __APPLE_API_OBSOLETE |
2d21ac55 | 46 | |
b0d623f7 A |
47 | #include <sys/appleapiopts.h> |
48 | ||
2d21ac55 A |
49 | /* |
50 | * Define IPv6 Firewall event subclass, and associated events. | |
51 | */ | |
52 | ||
53 | /*! | |
0a7de745 A |
54 | * @defined KEV_IP6FW_SUBCLASS |
55 | * @discussion The kernel event subclass for IPv6 Firewall. | |
56 | */ | |
57 | #define KEV_IP6FW_SUBCLASS 2 | |
2d21ac55 A |
58 | |
59 | /*! | |
0a7de745 A |
60 | * @defined KEV_IP6FW_ADD |
61 | * @discussion The event code indicating a rule has been added. | |
62 | */ | |
63 | #define KEV_IP6FW_ADD 1 | |
2d21ac55 A |
64 | |
65 | /*! | |
0a7de745 A |
66 | * @defined KEV_IP6FW_DEL |
67 | * @discussion The event code indicating a rule has been removed. | |
68 | */ | |
69 | #define KEV_IP6FW_DEL 2 | |
2d21ac55 A |
70 | |
71 | /*! | |
0a7de745 A |
72 | * @defined KEV_IP6FW_FLUSH |
73 | * @discussion The event code indicating the rule set has been flushed. | |
74 | */ | |
75 | #define KEV_IP6FW_FLUSH 3 | |
2d21ac55 A |
76 | |
77 | /*! | |
0a7de745 A |
78 | * @defined KEV_IP6FW_FLUSH |
79 | * @discussion The event code indicating the enable flag has been changed | |
80 | */ | |
81 | #define KEV_IP6FW_ENABLE 4 | |
2d21ac55 A |
82 | |
83 | ||
1c79356b | 84 | #include <net/if.h> |
9bccf70c | 85 | |
0a7de745 | 86 | #define IPV6_FW_CURRENT_API_VERSION 20 /* Version of this API */ |
9bccf70c | 87 | |
1c79356b A |
88 | |
89 | /* | |
90 | * This union structure identifies an interface, either explicitly | |
91 | * by name or implicitly by IP address. The flags IP_FW_F_IIFNAME | |
92 | * and IP_FW_F_OIFNAME say how to interpret this structure. An | |
93 | * interface unit number of -1 matches any unit number, while an | |
94 | * IP address of 0.0.0.0 indicates matches any interface. | |
95 | * | |
96 | * The receive and transmit interfaces are only compared against the | |
97 | * the packet if the corresponding bit (IP_FW_F_IIFACE or IP_FW_F_OIFACE) | |
98 | * is set. Note some packets lack a receive or transmit interface | |
99 | * (in which case the missing "interface" never matches). | |
100 | */ | |
101 | ||
102 | union ip6_fw_if { | |
0a7de745 A |
103 | struct in6_addr fu_via_ip6; /* Specified by IPv6 address */ |
104 | struct { /* Specified by interface name */ | |
9bccf70c | 105 | #define IP6FW_IFNLEN IFNAMSIZ |
0a7de745 A |
106 | char name[IP6FW_IFNLEN]; |
107 | short unit; /* -1 means match any unit */ | |
108 | } fu_via_if; | |
1c79356b A |
109 | }; |
110 | ||
111 | /* | |
112 | * Format of an IP firewall descriptor | |
113 | * | |
114 | * fw_src, fw_dst, fw_smsk, fw_dmsk are always stored in network byte order. | |
115 | * fw_flg and fw_n*p are stored in host byte order (of course). | |
116 | * Port numbers are stored in HOST byte order. | |
117 | * Warning: setsockopt() will fail if sizeof(struct ip_fw) > MLEN (108) | |
118 | */ | |
119 | ||
b0d623f7 | 120 | |
1c79356b | 121 | struct ip6_fw { |
0a7de745 A |
122 | u_int32_t version; /* Version of this structure. Should always be */ |
123 | /* set to IP6_FW_CURRENT_API_VERSION by clients. */ | |
124 | void *context; /* Context that is usable by user processes to */ | |
125 | /* identify this rule. */ | |
126 | u_int32_t fw_pcnt, fw_bcnt; /* Packet and byte counters */ | |
127 | struct in6_addr fw_src, fw_dst; /* Source and destination IPv6 addr */ | |
128 | struct in6_addr fw_smsk, fw_dmsk; /* Mask for src and dest IPv6 addr */ | |
129 | u_short fw_number; /* Rule number */ | |
130 | u_short fw_flg; /* Flags word */ | |
131 | #define IPV6_FW_MAX_PORTS 10 /* A reasonable maximum */ | |
132 | u_int fw_ipflg; /* IP flags word */ | |
133 | u_short fw_pts[IPV6_FW_MAX_PORTS]; /* Array of port numbers to match */ | |
134 | u_char fw_ip6opt, fw_ip6nopt; /* IPv6 options set/unset */ | |
135 | u_char fw_tcpf, fw_tcpnf; /* TCP flags set/unset */ | |
9bccf70c | 136 | #define IPV6_FW_ICMPTYPES_DIM (256 / (sizeof(unsigned) * 8)) |
0a7de745 A |
137 | unsigned fw_icmp6types[IPV6_FW_ICMPTYPES_DIM]; /* ICMP types bitmap */ |
138 | long timestamp; /* timestamp (tv_sec) of last match */ | |
139 | union ip6_fw_if fw_in_if, fw_out_if;/* Incoming and outgoing interfaces */ | |
140 | union { | |
141 | u_short fu_divert_port; /* Divert/tee port (options IP6DIVERT) */ | |
142 | u_short fu_skipto_rule; /* SKIPTO command rule number */ | |
143 | u_short fu_reject_code; /* REJECT response code */ | |
144 | } fw_un; | |
145 | u_char fw_prot; /* IPv6 protocol */ | |
146 | u_char fw_nports; /* N'of src ports and # of dst ports */ | |
147 | /* in ports array (dst ports follow */ | |
148 | /* src ports; max of 10 ports in all; */ | |
149 | /* count of 0 means match all ports) */ | |
1c79356b A |
150 | }; |
151 | ||
39236c6e | 152 | #if defined(BSD_KERNEL_PRIVATE) |
b0d623f7 A |
153 | #pragma pack(4) |
154 | ||
155 | struct ip6_fw_32 { | |
0a7de745 | 156 | u_int32_t version; /* Version of this structure. Should always be */ |
b0d623f7 | 157 | /* set to IP6_FW_CURRENT_API_VERSION by clients. */ |
0a7de745 | 158 | user32_addr_t context; /* Context that is usable by user processes to */ |
b0d623f7 | 159 | /* identify this rule. */ |
0a7de745 A |
160 | u_int32_t fw_pcnt, fw_bcnt; /* Packet and byte counters */ |
161 | struct in6_addr fw_src, fw_dst; /* Source and destination IPv6 addr */ | |
162 | struct in6_addr fw_smsk, fw_dmsk; /* Mask for src and dest IPv6 addr */ | |
163 | u_short fw_number; /* Rule number */ | |
164 | u_short fw_flg; /* Flags word */ | |
165 | #define IPV6_FW_MAX_PORTS 10 /* A reasonable maximum */ | |
166 | u_int fw_ipflg; /* IP flags word */ | |
167 | u_short fw_pts[IPV6_FW_MAX_PORTS]; /* Array of port numbers to match */ | |
168 | u_char fw_ip6opt, fw_ip6nopt; /* IPv6 options set/unset */ | |
169 | u_char fw_tcpf, fw_tcpnf; /* TCP flags set/unset */ | |
b0d623f7 | 170 | #define IPV6_FW_ICMPTYPES_DIM (256 / (sizeof(unsigned) * 8)) |
0a7de745 A |
171 | unsigned fw_icmp6types[IPV6_FW_ICMPTYPES_DIM]; /* ICMP types bitmap */ |
172 | user32_time_t timestamp; /* timestamp (tv_sec) of last match */ | |
173 | union ip6_fw_if fw_in_if, fw_out_if;/* Incoming and outgoing interfaces */ | |
174 | union { | |
175 | u_short fu_divert_port; /* Divert/tee port (options IP6DIVERT) */ | |
176 | u_short fu_skipto_rule; /* SKIPTO command rule number */ | |
177 | u_short fu_reject_code; /* REJECT response code */ | |
178 | } fw_un; | |
179 | u_char fw_prot; /* IPv6 protocol */ | |
180 | u_char fw_nports; /* N'of src ports and # of dst ports */ | |
b0d623f7 A |
181 | /* in ports array (dst ports follow */ |
182 | /* src ports; max of 10 ports in all; */ | |
183 | /* count of 0 means match all ports) */ | |
184 | }; | |
185 | ||
186 | #pragma pack() | |
187 | ||
188 | struct ip6_fw_64 { | |
0a7de745 | 189 | u_int32_t version; /* Version of this structure. Should always be */ |
b0d623f7 | 190 | /* set to IP6_FW_CURRENT_API_VERSION by clients. */ |
0a7de745 | 191 | __uint64_t context __attribute__((aligned(8))); /* Context that is usable by user processes to */ |
b0d623f7 | 192 | /* identify this rule. */ |
0a7de745 A |
193 | u_int32_t fw_pcnt, fw_bcnt; /* Packet and byte counters */ |
194 | struct in6_addr fw_src, fw_dst; /* Source and destination IPv6 addr */ | |
195 | struct in6_addr fw_smsk, fw_dmsk; /* Mask for src and dest IPv6 addr */ | |
196 | u_short fw_number; /* Rule number */ | |
197 | u_short fw_flg; /* Flags word */ | |
198 | #define IPV6_FW_MAX_PORTS 10 /* A reasonable maximum */ | |
199 | u_int fw_ipflg; /* IP flags word */ | |
200 | u_short fw_pts[IPV6_FW_MAX_PORTS]; /* Array of port numbers to match */ | |
201 | u_char fw_ip6opt, fw_ip6nopt; /* IPv6 options set/unset */ | |
202 | u_char fw_tcpf, fw_tcpnf; /* TCP flags set/unset */ | |
b0d623f7 | 203 | #define IPV6_FW_ICMPTYPES_DIM (256 / (sizeof(unsigned) * 8)) |
0a7de745 A |
204 | unsigned fw_icmp6types[IPV6_FW_ICMPTYPES_DIM]; /* ICMP types bitmap */ |
205 | user64_time_t timestamp; /* timestamp (tv_sec) of last match */ | |
206 | union ip6_fw_if fw_in_if, fw_out_if;/* Incoming and outgoing interfaces */ | |
207 | union { | |
208 | u_short fu_divert_port; /* Divert/tee port (options IP6DIVERT) */ | |
209 | u_short fu_skipto_rule; /* SKIPTO command rule number */ | |
210 | u_short fu_reject_code; /* REJECT response code */ | |
211 | } fw_un; | |
212 | u_char fw_prot; /* IPv6 protocol */ | |
213 | u_char fw_nports; /* N'of src ports and # of dst ports */ | |
b0d623f7 A |
214 | /* in ports array (dst ports follow */ |
215 | /* src ports; max of 10 ports in all; */ | |
216 | /* count of 0 means match all ports) */ | |
217 | }; | |
218 | ||
219 | ||
0a7de745 | 220 | #endif /* BSD_KERNEL_PRIVATE */ |
b0d623f7 | 221 | |
0a7de745 A |
222 | #define IPV6_FW_GETNSRCP(rule) ((rule)->fw_nports & 0x0f) |
223 | #define IPV6_FW_SETNSRCP(rule, n) do { \ | |
224 | (rule)->fw_nports &= ~0x0f; \ | |
225 | (rule)->fw_nports |= (n); \ | |
226 | } while (0) | |
227 | #define IPV6_FW_GETNDSTP(rule) ((rule)->fw_nports >> 4) | |
228 | #define IPV6_FW_SETNDSTP(rule, n) do { \ | |
229 | (rule)->fw_nports &= ~0xf0; \ | |
230 | (rule)->fw_nports |= (n) << 4;\ | |
231 | } while (0) | |
1c79356b | 232 | |
0a7de745 A |
233 | #define fw_divert_port fw_un.fu_divert_port |
234 | #define fw_skipto_rule fw_un.fu_skipto_rule | |
235 | #define fw_reject_code fw_un.fu_reject_code | |
1c79356b A |
236 | |
237 | struct ip6_fw_chain { | |
0a7de745 A |
238 | LIST_ENTRY(ip6_fw_chain) chain; |
239 | struct ip6_fw *rule; | |
1c79356b A |
240 | }; |
241 | ||
242 | /* | |
243 | * Values for "flags" field . | |
244 | */ | |
0a7de745 A |
245 | #define IPV6_FW_F_IN 0x0001 /* Check inbound packets */ |
246 | #define IPV6_FW_F_OUT 0x0002 /* Check outbound packets */ | |
247 | #define IPV6_FW_F_IIFACE 0x0004 /* Apply inbound interface test */ | |
248 | #define IPV6_FW_F_OIFACE 0x0008 /* Apply outbound interface test */ | |
1c79356b | 249 | |
0a7de745 A |
250 | #define IPV6_FW_F_COMMAND 0x0070 /* Mask for type of chain entry: */ |
251 | #define IPV6_FW_F_DENY 0x0000 /* This is a deny rule */ | |
252 | #define IPV6_FW_F_REJECT 0x0010 /* Deny and send a response packet */ | |
253 | #define IPV6_FW_F_ACCEPT 0x0020 /* This is an accept rule */ | |
254 | #define IPV6_FW_F_COUNT 0x0030 /* This is a count rule */ | |
255 | #define IPV6_FW_F_DIVERT 0x0040 /* This is a divert rule */ | |
256 | #define IPV6_FW_F_TEE 0x0050 /* This is a tee rule */ | |
257 | #define IPV6_FW_F_SKIPTO 0x0060 /* This is a skipto rule */ | |
1c79356b | 258 | |
0a7de745 | 259 | #define IPV6_FW_F_PRN 0x0080 /* Print if this rule matches */ |
1c79356b | 260 | |
0a7de745 A |
261 | #define IPV6_FW_F_SRNG 0x0100 /* The first two src ports are a min * |
262 | * and max range (stored in host byte * | |
263 | * order). */ | |
1c79356b | 264 | |
0a7de745 A |
265 | #define IPV6_FW_F_DRNG 0x0200 /* The first two dst ports are a min * |
266 | * and max range (stored in host byte * | |
267 | * order). */ | |
1c79356b | 268 | |
0a7de745 A |
269 | #define IPV6_FW_F_IIFNAME 0x0400 /* In interface by name/unit (not IP) */ |
270 | #define IPV6_FW_F_OIFNAME 0x0800 /* Out interface by name/unit (not IP) */ | |
1c79356b | 271 | |
0a7de745 A |
272 | #define IPV6_FW_F_INVSRC 0x1000 /* Invert sense of src check */ |
273 | #define IPV6_FW_F_INVDST 0x2000 /* Invert sense of dst check */ | |
1c79356b | 274 | |
0a7de745 | 275 | #define IPV6_FW_F_FRAG 0x4000 /* Fragment */ |
1c79356b | 276 | |
0a7de745 | 277 | #define IPV6_FW_F_ICMPBIT 0x8000 /* ICMP type bitmap is valid */ |
1c79356b | 278 | |
0a7de745 | 279 | #define IPV6_FW_F_MASK 0xFFFF /* All possible flag bits mask */ |
1c79356b | 280 | |
0a7de745 | 281 | /* |
9bccf70c | 282 | * Flags for the 'fw_ipflg' field, for comparing values of ip and its protocols. */ |
0a7de745 A |
283 | #define IPV6_FW_IF_TCPEST 0x00000020 /* established TCP connection */ |
284 | #define IPV6_FW_IF_TCPMSK 0x00000020 /* mask of all TCP values */ | |
9bccf70c | 285 | |
1c79356b A |
286 | /* |
287 | * For backwards compatibility with rules specifying "via iface" but | |
288 | * not restricted to only "in" or "out" packets, we define this combination | |
289 | * of bits to represent this configuration. | |
290 | */ | |
291 | ||
0a7de745 | 292 | #define IF6_FW_F_VIAHACK (IPV6_FW_F_IN|IPV6_FW_F_OUT|IPV6_FW_F_IIFACE|IPV6_FW_F_OIFACE) |
1c79356b A |
293 | |
294 | /* | |
295 | * Definitions for REJECT response codes. | |
296 | * Values less than 256 correspond to ICMP unreachable codes. | |
297 | */ | |
0a7de745 | 298 | #define IPV6_FW_REJECT_RST 0x0100 /* TCP packets: send RST */ |
1c79356b A |
299 | |
300 | /* | |
301 | * Definitions for IPv6 option names. | |
302 | */ | |
0a7de745 A |
303 | #define IPV6_FW_IP6OPT_HOPOPT 0x01 |
304 | #define IPV6_FW_IP6OPT_ROUTE 0x02 | |
305 | #define IPV6_FW_IP6OPT_FRAG 0x04 | |
306 | #define IPV6_FW_IP6OPT_ESP 0x08 | |
307 | #define IPV6_FW_IP6OPT_AH 0x10 | |
308 | #define IPV6_FW_IP6OPT_NONXT 0x20 | |
309 | #define IPV6_FW_IP6OPT_OPTS 0x40 | |
1c79356b A |
310 | |
311 | /* | |
312 | * Definitions for TCP flags. | |
313 | */ | |
0a7de745 A |
314 | #define IPV6_FW_TCPF_FIN TH_FIN |
315 | #define IPV6_FW_TCPF_SYN TH_SYN | |
316 | #define IPV6_FW_TCPF_RST TH_RST | |
317 | #define IPV6_FW_TCPF_PSH TH_PUSH | |
318 | #define IPV6_FW_TCPF_ACK TH_ACK | |
319 | #define IPV6_FW_TCPF_URG TH_URG | |
1c79356b A |
320 | |
321 | /* | |
322 | * Main firewall chains definitions and global var's definitions. | |
323 | */ | |
39236c6e | 324 | #ifdef BSD_KERNEL_PRIVATE |
9bccf70c A |
325 | |
326 | #define M_IP6FW M_IPFW | |
327 | ||
1c79356b A |
328 | |
329 | /* | |
330 | * Function definitions. | |
331 | */ | |
332 | void ip6_fw_init(void); | |
060df5ea | 333 | void load_ip6fw(void); |
1c79356b A |
334 | |
335 | /* Firewall hooks */ | |
336 | struct ip6_hdr; | |
55e303ae | 337 | struct sockopt; |
0a7de745 A |
338 | typedef int ip6_fw_chk_t(struct ip6_hdr**, struct ifnet*, |
339 | u_short *, struct mbuf**); | |
340 | typedef int ip6_fw_ctl_t(struct sockopt *); | |
341 | extern ip6_fw_chk_t *ip6_fw_chk_ptr; | |
342 | extern ip6_fw_ctl_t *ip6_fw_ctl_ptr; | |
343 | extern int ip6_fw_enable; | |
1c79356b | 344 | |
39236c6e | 345 | #endif /* BSD_KERNEL_PRIVATE */ |
1c79356b | 346 | |
6d2010ae | 347 | #endif /* __APPLE_API_OBSOLETE */ |
1c79356b | 348 | #endif /* _IP6_FW_H */ |