1 /* $FreeBSD: stable/12/sys/netpfil/ipfilter/netinet/fil.c 373220 2023-09-27 02:23:13Z cy $ */
2
3 /*
4 * Copyright (C) 2012 by Darren Reed.
5 *
6 * See the IPFILTER.LICENCE file for details on licencing.
7 *
8 * Copyright 2008 Sun Microsystems.
9 *
10 * $Id$
11 *
12 */
13 #if defined(KERNEL) || defined(_KERNEL)
14 # undef KERNEL
15 # undef _KERNEL
16 # define KERNEL 1
17 # define _KERNEL 1
18 #endif
19 #include <sys/errno.h>
20 #include <sys/types.h>
21 #include <sys/param.h>
22 #include <sys/time.h>
23 #if defined(_KERNEL) && defined(__FreeBSD__)
24 # if !defined(IPFILTER_LKM)
25 # include "opt_inet6.h"
26 # endif
27 # include <sys/filio.h>
28 #else
29 # include <sys/ioctl.h>
30 #endif
31 #if defined(__SVR4) || defined(sun) /* SOLARIS */
32 # include <sys/filio.h>
33 #endif
34 # include <sys/fcntl.h>
35 #if defined(_KERNEL)
36 # include <sys/systm.h>
37 # include <sys/file.h>
38 #else
39 # include <stdio.h>
40 # include <string.h>
41 # include <stdlib.h>
42 # include <stddef.h>
43 # include <sys/file.h>
44 # define _KERNEL
45 # include <sys/uio.h>
46 # undef _KERNEL
47 #endif
48 #if !defined(__SVR4)
49 # include <sys/mbuf.h>
50 #else
51 # include <sys/byteorder.h>
52 # if (SOLARIS2 < 5) && defined(sun)
53 # include <sys/dditypes.h>
54 # endif
55 #endif
56 # include <sys/protosw.h>
57 #include <sys/socket.h>
58 #include <net/if.h>
59 #ifdef sun
60 # include <net/af.h>
61 #endif
62 #include <netinet/in.h>
63 #include <netinet/in_systm.h>
64 #include <netinet/ip.h>
65 #include <netinet/tcp.h>
66 # include <netinet/udp.h>
67 # include <netinet/ip_icmp.h>
68 #include "netinet/ip_compat.h"
69 #ifdef USE_INET6
70 # include <netinet/icmp6.h>
71 # if !SOLARIS && defined(_KERNEL)
72 # include <netinet6/in6_var.h>
73 # endif
74 #endif
75 #include "netinet/ip_fil.h"
76 #include "netinet/ip_nat.h"
77 #include "netinet/ip_frag.h"
78 #include "netinet/ip_state.h"
79 #include "netinet/ip_proxy.h"
80 #include "netinet/ip_auth.h"
81 #ifdef IPFILTER_SCAN
82 # include "netinet/ip_scan.h"
83 #endif
84 #include "netinet/ip_sync.h"
85 #include "netinet/ip_lookup.h"
86 #include "netinet/ip_pool.h"
87 #include "netinet/ip_htable.h"
88 #ifdef IPFILTER_COMPILED
89 # include "netinet/ip_rules.h"
90 #endif
91 #if defined(IPFILTER_BPF) && defined(_KERNEL)
92 # include <net/bpf.h>
93 #endif
94 #if defined(__FreeBSD__)
95 # include <sys/malloc.h>
96 #endif
97 #include "netinet/ipl.h"
98
99 #if defined(__NetBSD__) && (__NetBSD_Version__ >= 104230000)
100 # include <sys/callout.h>
101 extern struct callout ipf_slowtimer_ch;
102 #endif
103 /* END OF INCLUDES */
104
105 #if !defined(lint)
106 static const char sccsid[] = "@(#)fil.c 1.36 6/5/96 (C) 1993-2000 Darren Reed";
107 static const char rcsid[] = "@(#)$FreeBSD: stable/12/sys/netpfil/ipfilter/netinet/fil.c 373220 2023-09-27 02:23:13Z cy $";
108 /* static const char rcsid[] = "@(#)$Id: fil.c,v 2.243.2.125 2007/10/10 09:27:20 darrenr Exp $"; */
109 #endif
110
111 #ifndef _KERNEL
112 # include "ipf.h"
113 # include "ipt.h"
114 extern int opts;
115 extern int blockreason;
116 #endif /* _KERNEL */
117
118 #define FASTROUTE_RECURSION
119
120 #define LBUMP(x) softc->x++
121 #define LBUMPD(x, y) do { softc->x.y++; DT(y); } while (0)
122
123 static inline int ipf_check_ipf(fr_info_t *, frentry_t *, int);
124 static u_32_t ipf_checkcipso(fr_info_t *, u_char *, int);
125 static u_32_t ipf_checkripso(u_char *);
126 static u_32_t ipf_decaps(fr_info_t *, u_32_t, int);
127 #ifdef IPFILTER_LOG
128 static frentry_t *ipf_dolog(fr_info_t *, u_32_t *);
129 #endif
130 static int ipf_flushlist(ipf_main_softc_t *, int *, frentry_t **);
131 static int ipf_flush_groups(ipf_main_softc_t *, frgroup_t **,
132 int);
133 static ipfunc_t ipf_findfunc(ipfunc_t);
134 static void *ipf_findlookup(ipf_main_softc_t *, int, frentry_t *,
135 i6addr_t *, i6addr_t *);
136 static frentry_t *ipf_firewall(fr_info_t *, u_32_t *);
137 static int ipf_fr_matcharray(fr_info_t *, int *);
138 static int ipf_frruleiter(ipf_main_softc_t *, void *, int,
139 void *);
140 static void ipf_funcfini(ipf_main_softc_t *, frentry_t *);
141 static int ipf_funcinit(ipf_main_softc_t *, frentry_t *);
142 static int ipf_geniter(ipf_main_softc_t *, ipftoken_t *,
143 ipfgeniter_t *);
144 static void ipf_getstat(ipf_main_softc_t *,
145 struct friostat *, int);
146 static int ipf_group_flush(ipf_main_softc_t *, frgroup_t *);
147 static void ipf_group_free(frgroup_t *);
148 static int ipf_grpmapfini(struct ipf_main_softc_s *,
149 frentry_t *);
150 static int ipf_grpmapinit(struct ipf_main_softc_s *,
151 frentry_t *);
152 static frentry_t *ipf_nextrule(ipf_main_softc_t *, int, int,
153 frentry_t *, int);
154 static int ipf_portcheck(frpcmp_t *, u_32_t);
155 static inline int ipf_pr_ah(fr_info_t *);
156 static inline void ipf_pr_esp(fr_info_t *);
157 static inline void ipf_pr_gre(fr_info_t *);
158 static inline void ipf_pr_udp(fr_info_t *);
159 static inline void ipf_pr_tcp(fr_info_t *);
160 static inline void ipf_pr_icmp(fr_info_t *);
161 static inline void ipf_pr_ipv4hdr(fr_info_t *);
162 static inline void ipf_pr_short(fr_info_t *, int);
163 static inline int ipf_pr_tcpcommon(fr_info_t *);
164 static inline int ipf_pr_udpcommon(fr_info_t *);
165 static void ipf_rule_delete(ipf_main_softc_t *, frentry_t *f,
166 int, int);
167 static void ipf_rule_expire_insert(ipf_main_softc_t *,
168 frentry_t *, int);
169 static int ipf_synclist(ipf_main_softc_t *, frentry_t *,
170 void *);
171 static void ipf_token_flush(ipf_main_softc_t *);
172 static void ipf_token_unlink(ipf_main_softc_t *,
173 ipftoken_t *);
174 static ipftuneable_t *ipf_tune_findbyname(ipftuneable_t *,
175 const char *);
176 static ipftuneable_t *ipf_tune_findbycookie(ipftuneable_t **, void *,
177 void **);
178 static int ipf_updateipid(fr_info_t *);
179 static int ipf_settimeout(struct ipf_main_softc_s *,
180 struct ipftuneable *,
181 ipftuneval_t *);
182 #if !defined(_KERNEL) || SOLARIS
183 static int ppsratecheck(struct timeval *, int *, int);
184 #endif
185
186
187 /*
188 * bit values for identifying presence of individual IP options
189 * All of these tables should be ordered by increasing key value on the left
190 * hand side to allow for binary searching of the array and include a trailer
191 * with a 0 for the bitmask for linear searches to easily find the end with.
192 */
193 static const struct optlist ipopts[] = {
194 { IPOPT_NOP, 0x000001 },
195 { IPOPT_RR, 0x000002 },
196 { IPOPT_ZSU, 0x000004 },
197 { IPOPT_MTUP, 0x000008 },
198 { IPOPT_MTUR, 0x000010 },
199 { IPOPT_ENCODE, 0x000020 },
200 { IPOPT_TS, 0x000040 },
201 { IPOPT_TR, 0x000080 },
202 { IPOPT_SECURITY, 0x000100 },
203 { IPOPT_LSRR, 0x000200 },
204 { IPOPT_E_SEC, 0x000400 },
205 { IPOPT_CIPSO, 0x000800 },
206 { IPOPT_SATID, 0x001000 },
207 { IPOPT_SSRR, 0x002000 },
208 { IPOPT_ADDEXT, 0x004000 },
209 { IPOPT_VISA, 0x008000 },
210 { IPOPT_IMITD, 0x010000 },
211 { IPOPT_EIP, 0x020000 },
212 { IPOPT_FINN, 0x040000 },
213 { 0, 0x000000 }
214 };
215
216 #ifdef USE_INET6
217 static const struct optlist ip6exthdr[] = {
218 { IPPROTO_HOPOPTS, 0x000001 },
219 { IPPROTO_IPV6, 0x000002 },
220 { IPPROTO_ROUTING, 0x000004 },
221 { IPPROTO_FRAGMENT, 0x000008 },
222 { IPPROTO_ESP, 0x000010 },
223 { IPPROTO_AH, 0x000020 },
224 { IPPROTO_NONE, 0x000040 },
225 { IPPROTO_DSTOPTS, 0x000080 },
226 { IPPROTO_MOBILITY, 0x000100 },
227 { 0, 0 }
228 };
229 #endif
230
231 /*
232 * bit values for identifying presence of individual IP security options
233 */
234 static const struct optlist secopt[] = {
235 { IPSO_CLASS_RES4, 0x01 },
236 { IPSO_CLASS_TOPS, 0x02 },
237 { IPSO_CLASS_SECR, 0x04 },
238 { IPSO_CLASS_RES3, 0x08 },
239 { IPSO_CLASS_CONF, 0x10 },
240 { IPSO_CLASS_UNCL, 0x20 },
241 { IPSO_CLASS_RES2, 0x40 },
242 { IPSO_CLASS_RES1, 0x80 }
243 };
244
245 char ipfilter_version[] = IPL_VERSION;
246
247 int ipf_features = 0
248 #ifdef IPFILTER_LKM
249 | IPF_FEAT_LKM
250 #endif
251 #ifdef IPFILTER_LOG
252 | IPF_FEAT_LOG
253 #endif
254 | IPF_FEAT_LOOKUP
255 #ifdef IPFILTER_BPF
256 | IPF_FEAT_BPF
257 #endif
258 #ifdef IPFILTER_COMPILED
259 | IPF_FEAT_COMPILED
260 #endif
261 #ifdef IPFILTER_CKSUM
262 | IPF_FEAT_CKSUM
263 #endif
264 | IPF_FEAT_SYNC
265 #ifdef IPFILTER_SCAN
266 | IPF_FEAT_SCAN
267 #endif
268 #ifdef USE_INET6
269 | IPF_FEAT_IPV6
270 #endif
271 ;
272
273
274 /*
275 * Table of functions available for use with call rules.
276 */
277 static ipfunc_resolve_t ipf_availfuncs[] = {
278 { "srcgrpmap", ipf_srcgrpmap, ipf_grpmapinit, ipf_grpmapfini },
279 { "dstgrpmap", ipf_dstgrpmap, ipf_grpmapinit, ipf_grpmapfini },
280 { "", NULL, NULL, NULL }
281 };
282
283 static ipftuneable_t ipf_main_tuneables[] = {
284 { { (void *)offsetof(struct ipf_main_softc_s, ipf_flags) },
285 "ipf_flags", 0, 0xffffffff,
286 stsizeof(ipf_main_softc_t, ipf_flags),
287 0, NULL, NULL },
288 { { (void *)offsetof(struct ipf_main_softc_s, ipf_active) },
289 "active", 0, 0,
290 stsizeof(ipf_main_softc_t, ipf_active),
291 IPFT_RDONLY, NULL, NULL },
292 { { (void *)offsetof(ipf_main_softc_t, ipf_control_forwarding) },
293 "control_forwarding", 0, 1,
294 stsizeof(ipf_main_softc_t, ipf_control_forwarding),
295 0, NULL, NULL },
296 { { (void *)offsetof(ipf_main_softc_t, ipf_update_ipid) },
297 "update_ipid", 0, 1,
298 stsizeof(ipf_main_softc_t, ipf_update_ipid),
299 0, NULL, NULL },
300 { { (void *)offsetof(ipf_main_softc_t, ipf_chksrc) },
301 "chksrc", 0, 1,
302 stsizeof(ipf_main_softc_t, ipf_chksrc),
303 0, NULL, NULL },
304 { { (void *)offsetof(ipf_main_softc_t, ipf_minttl) },
305 "min_ttl", 0, 1,
306 stsizeof(ipf_main_softc_t, ipf_minttl),
307 0, NULL, NULL },
308 { { (void *)offsetof(ipf_main_softc_t, ipf_icmpminfragmtu) },
309 "icmp_minfragmtu", 0, 1,
310 stsizeof(ipf_main_softc_t, ipf_icmpminfragmtu),
311 0, NULL, NULL },
312 { { (void *)offsetof(ipf_main_softc_t, ipf_pass) },
313 "default_pass", 0, 0xffffffff,
314 stsizeof(ipf_main_softc_t, ipf_pass),
315 0, NULL, NULL },
316 { { (void *)offsetof(ipf_main_softc_t, ipf_tcpidletimeout) },
317 "tcp_idle_timeout", 1, 0x7fffffff,
318 stsizeof(ipf_main_softc_t, ipf_tcpidletimeout),
319 0, NULL, ipf_settimeout },
320 { { (void *)offsetof(ipf_main_softc_t, ipf_tcpclosewait) },
321 "tcp_close_wait", 1, 0x7fffffff,
322 stsizeof(ipf_main_softc_t, ipf_tcpclosewait),
323 0, NULL, ipf_settimeout },
324 { { (void *)offsetof(ipf_main_softc_t, ipf_tcplastack) },
325 "tcp_last_ack", 1, 0x7fffffff,
326 stsizeof(ipf_main_softc_t, ipf_tcplastack),
327 0, NULL, ipf_settimeout },
328 { { (void *)offsetof(ipf_main_softc_t, ipf_tcptimeout) },
329 "tcp_timeout", 1, 0x7fffffff,
330 stsizeof(ipf_main_softc_t, ipf_tcptimeout),
331 0, NULL, ipf_settimeout },
332 { { (void *)offsetof(ipf_main_softc_t, ipf_tcpsynsent) },
333 "tcp_syn_sent", 1, 0x7fffffff,
334 stsizeof(ipf_main_softc_t, ipf_tcpsynsent),
335 0, NULL, ipf_settimeout },
336 { { (void *)offsetof(ipf_main_softc_t, ipf_tcpsynrecv) },
337 "tcp_syn_received", 1, 0x7fffffff,
338 stsizeof(ipf_main_softc_t, ipf_tcpsynrecv),
339 0, NULL, ipf_settimeout },
340 { { (void *)offsetof(ipf_main_softc_t, ipf_tcpclosed) },
341 "tcp_closed", 1, 0x7fffffff,
342 stsizeof(ipf_main_softc_t, ipf_tcpclosed),
343 0, NULL, ipf_settimeout },
344 { { (void *)offsetof(ipf_main_softc_t, ipf_tcphalfclosed) },
345 "tcp_half_closed", 1, 0x7fffffff,
346 stsizeof(ipf_main_softc_t, ipf_tcphalfclosed),
347 0, NULL, ipf_settimeout },
348 { { (void *)offsetof(ipf_main_softc_t, ipf_tcptimewait) },
349 "tcp_time_wait", 1, 0x7fffffff,
350 stsizeof(ipf_main_softc_t, ipf_tcptimewait),
351 0, NULL, ipf_settimeout },
352 { { (void *)offsetof(ipf_main_softc_t, ipf_udptimeout) },
353 "udp_timeout", 1, 0x7fffffff,
354 stsizeof(ipf_main_softc_t, ipf_udptimeout),
355 0, NULL, ipf_settimeout },
356 { { (void *)offsetof(ipf_main_softc_t, ipf_udpacktimeout) },
357 "udp_ack_timeout", 1, 0x7fffffff,
358 stsizeof(ipf_main_softc_t, ipf_udpacktimeout),
359 0, NULL, ipf_settimeout },
360 { { (void *)offsetof(ipf_main_softc_t, ipf_icmptimeout) },
361 "icmp_timeout", 1, 0x7fffffff,
362 stsizeof(ipf_main_softc_t, ipf_icmptimeout),
363 0, NULL, ipf_settimeout },
364 { { (void *)offsetof(ipf_main_softc_t, ipf_icmpacktimeout) },
365 "icmp_ack_timeout", 1, 0x7fffffff,
366 stsizeof(ipf_main_softc_t, ipf_icmpacktimeout),
367 0, NULL, ipf_settimeout },
368 { { (void *)offsetof(ipf_main_softc_t, ipf_iptimeout) },
369 "ip_timeout", 1, 0x7fffffff,
370 stsizeof(ipf_main_softc_t, ipf_iptimeout),
371 0, NULL, ipf_settimeout },
372 #if defined(INSTANCES) && defined(_KERNEL)
373 { { (void *)offsetof(ipf_main_softc_t, ipf_get_loopback) },
374 "intercept_loopback", 0, 1,
375 stsizeof(ipf_main_softc_t, ipf_get_loopback),
376 0, NULL, ipf_set_loopback },
377 #endif
378 { { 0 },
379 NULL, 0, 0,
380 0,
381 0, NULL, NULL }
382 };
383
384
385 /*
386 * The next section of code is a collection of small routines that set
387 * fields in the fr_info_t structure passed based on properties of the
388 * current packet. There are different routines for the same protocol
389 * for each of IPv4 and IPv6. Adding a new protocol, for which there
390 * will "special" inspection for setup, is now more easily done by adding
391 * a new routine and expanding the ipf_pr_ipinit*() function rather than by
392 * adding more code to a growing switch statement.
393 */
394 #ifdef USE_INET6
395 static inline int ipf_pr_ah6(fr_info_t *);
396 static inline void ipf_pr_esp6(fr_info_t *);
397 static inline void ipf_pr_gre6(fr_info_t *);
398 static inline void ipf_pr_udp6(fr_info_t *);
399 static inline void ipf_pr_tcp6(fr_info_t *);
400 static inline void ipf_pr_icmp6(fr_info_t *);
401 static inline void ipf_pr_ipv6hdr(fr_info_t *);
402 static inline void ipf_pr_short6(fr_info_t *, int);
403 static inline int ipf_pr_hopopts6(fr_info_t *);
404 static inline int ipf_pr_mobility6(fr_info_t *);
405 static inline int ipf_pr_routing6(fr_info_t *);
406 static inline int ipf_pr_dstopts6(fr_info_t *);
407 static inline int ipf_pr_fragment6(fr_info_t *);
408 static inline struct ip6_ext *ipf_pr_ipv6exthdr(fr_info_t *, int, int);
409
410
411 /* ------------------------------------------------------------------------ */
412 /* Function: ipf_pr_short6 */
413 /* Returns: void */
414 /* Parameters: fin(I) - pointer to packet information */
415 /* xmin(I) - minimum header size */
416 /* */
417 /* IPv6 Only */
418 /* This is function enforces the 'is a packet too short to be legit' rule */
419 /* for IPv6 and marks the packet with FI_SHORT if so. See function comment */
420 /* for ipf_pr_short() for more details. */
421 /* ------------------------------------------------------------------------ */
422 static inline void
ipf_pr_short6(fr_info_t * fin,int xmin)423 ipf_pr_short6(fr_info_t *fin, int xmin)
424 {
425
426 if (fin->fin_dlen < xmin)
427 fin->fin_flx |= FI_SHORT;
428 }
429
430
431 /* ------------------------------------------------------------------------ */
432 /* Function: ipf_pr_ipv6hdr */
433 /* Returns: void */
434 /* Parameters: fin(I) - pointer to packet information */
435 /* */
436 /* IPv6 Only */
437 /* Copy values from the IPv6 header into the fr_info_t struct and call the */
438 /* per-protocol analyzer if it exists. In validating the packet, a protocol*/
439 /* analyzer may pullup or free the packet itself so we need to be vigiliant */
440 /* of that possibility arising. */
441 /* ------------------------------------------------------------------------ */
442 static inline void
ipf_pr_ipv6hdr(fr_info_t * fin)443 ipf_pr_ipv6hdr(fr_info_t *fin)
444 {
445 ip6_t *ip6 = (ip6_t *)fin->fin_ip;
446 int p, go = 1, i, hdrcount;
447 fr_ip_t *fi = &fin->fin_fi;
448
449 fin->fin_off = 0;
450
451 fi->fi_tos = 0;
452 fi->fi_optmsk = 0;
453 fi->fi_secmsk = 0;
454 fi->fi_auth = 0;
455
456 p = ip6->ip6_nxt;
457 fin->fin_crc = p;
458 fi->fi_ttl = ip6->ip6_hlim;
459 fi->fi_src.in6 = ip6->ip6_src;
460 fin->fin_crc += fi->fi_src.i6[0];
461 fin->fin_crc += fi->fi_src.i6[1];
462 fin->fin_crc += fi->fi_src.i6[2];
463 fin->fin_crc += fi->fi_src.i6[3];
464 fi->fi_dst.in6 = ip6->ip6_dst;
465 fin->fin_crc += fi->fi_dst.i6[0];
466 fin->fin_crc += fi->fi_dst.i6[1];
467 fin->fin_crc += fi->fi_dst.i6[2];
468 fin->fin_crc += fi->fi_dst.i6[3];
469 fin->fin_id = 0;
470 if (IN6_IS_ADDR_MULTICAST(&fi->fi_dst.in6))
471 fin->fin_flx |= FI_MULTICAST|FI_MBCAST;
472
473 hdrcount = 0;
474 while (go && !(fin->fin_flx & FI_SHORT)) {
475 switch (p)
476 {
477 case IPPROTO_UDP :
478 ipf_pr_udp6(fin);
479 go = 0;
480 break;
481
482 case IPPROTO_TCP :
483 ipf_pr_tcp6(fin);
484 go = 0;
485 break;
486
487 case IPPROTO_ICMPV6 :
488 ipf_pr_icmp6(fin);
489 go = 0;
490 break;
491
492 case IPPROTO_GRE :
493 ipf_pr_gre6(fin);
494 go = 0;
495 break;
496
497 case IPPROTO_HOPOPTS :
498 p = ipf_pr_hopopts6(fin);
499 break;
500
501 case IPPROTO_MOBILITY :
502 p = ipf_pr_mobility6(fin);
503 break;
504
505 case IPPROTO_DSTOPTS :
506 p = ipf_pr_dstopts6(fin);
507 break;
508
509 case IPPROTO_ROUTING :
510 p = ipf_pr_routing6(fin);
511 break;
512
513 case IPPROTO_AH :
514 p = ipf_pr_ah6(fin);
515 break;
516
517 case IPPROTO_ESP :
518 ipf_pr_esp6(fin);
519 go = 0;
520 break;
521
522 case IPPROTO_IPV6 :
523 for (i = 0; ip6exthdr[i].ol_bit != 0; i++)
524 if (ip6exthdr[i].ol_val == p) {
525 fin->fin_flx |= ip6exthdr[i].ol_bit;
526 break;
527 }
528 go = 0;
529 break;
530
531 case IPPROTO_NONE :
532 go = 0;
533 break;
534
535 case IPPROTO_FRAGMENT :
536 p = ipf_pr_fragment6(fin);
537 /*
538 * Given that the only fragments we want to let through
539 * (where fin_off != 0) are those where the non-first
540 * fragments only have data, we can safely stop looking
541 * at headers if this is a non-leading fragment.
542 */
543 if (fin->fin_off != 0)
544 go = 0;
545 break;
546
547 default :
548 go = 0;
549 break;
550 }
551 hdrcount++;
552
553 /*
554 * It is important to note that at this point, for the
555 * extension headers (go != 0), the entire header may not have
556 * been pulled up when the code gets to this point. This is
557 * only done for "go != 0" because the other header handlers
558 * will all pullup their complete header. The other indicator
559 * of an incomplete packet is that this was just an extension
560 * header.
561 */
562 if ((go != 0) && (p != IPPROTO_NONE) &&
563 (ipf_pr_pullup(fin, 0) == -1)) {
564 p = IPPROTO_NONE;
565 break;
566 }
567 }
568
569 /*
570 * Some of the above functions, like ipf_pr_esp6(), can call ipf_pullup
571 * and destroy whatever packet was here. The caller of this function
572 * expects us to return if there is a problem with ipf_pullup.
573 */
574 if (fin->fin_m == NULL) {
575 ipf_main_softc_t *softc = fin->fin_main_soft;
576
577 LBUMPD(ipf_stats[fin->fin_out], fr_v6_bad);
578 return;
579 }
580
581 fi->fi_p = p;
582
583 /*
584 * IPv6 fragment case 1 - see comment for ipf_pr_fragment6().
585 * "go != 0" implies the above loop hasn't arrived at a layer 4 header.
586 */
587 if ((go != 0) && (fin->fin_flx & FI_FRAG) && (fin->fin_off == 0)) {
588 ipf_main_softc_t *softc = fin->fin_main_soft;
589
590 fin->fin_flx |= FI_BAD;
591 DT2(ipf_fi_bad_ipv6_frag_1, fr_info_t *, fin, int, go);
592 LBUMPD(ipf_stats[fin->fin_out], fr_v6_badfrag);
593 LBUMP(ipf_stats[fin->fin_out].fr_v6_bad);
594 }
595 }
596
597
598 /* ------------------------------------------------------------------------ */
599 /* Function: ipf_pr_ipv6exthdr */
600 /* Returns: struct ip6_ext * - pointer to the start of the next header */
601 /* or NULL if there is a prolblem. */
602 /* Parameters: fin(I) - pointer to packet information */
603 /* multiple(I) - flag indicating yes/no if multiple occurances */
604 /* of this extension header are allowed. */
605 /* proto(I) - protocol number for this extension header */
606 /* */
607 /* IPv6 Only */
608 /* This function embodies a number of common checks that all IPv6 extension */
609 /* headers must be subjected to. For example, making sure the packet is */
610 /* big enough for it to be in, checking if it is repeated and setting a */
611 /* flag to indicate its presence. */
612 /* ------------------------------------------------------------------------ */
613 static inline struct ip6_ext *
ipf_pr_ipv6exthdr(fr_info_t * fin,int multiple,int proto)614 ipf_pr_ipv6exthdr(fr_info_t *fin, int multiple, int proto)
615 {
616 ipf_main_softc_t *softc = fin->fin_main_soft;
617 struct ip6_ext *hdr;
618 u_short shift;
619 int i;
620
621 fin->fin_flx |= FI_V6EXTHDR;
622
623 /* 8 is default length of extension hdr */
624 if ((fin->fin_dlen - 8) < 0) {
625 fin->fin_flx |= FI_SHORT;
626 LBUMPD(ipf_stats[fin->fin_out], fr_v6_ext_short);
627 return (NULL);
628 }
629
630 if (ipf_pr_pullup(fin, 8) == -1) {
631 LBUMPD(ipf_stats[fin->fin_out], fr_v6_ext_pullup);
632 return (NULL);
633 }
634
635 hdr = fin->fin_dp;
636 switch (proto)
637 {
638 case IPPROTO_FRAGMENT :
639 shift = 8;
640 break;
641 default :
642 shift = 8 + (hdr->ip6e_len << 3);
643 break;
644 }
645
646 if (shift > fin->fin_dlen) { /* Nasty extension header length? */
647 fin->fin_flx |= FI_BAD;
648 DT3(ipf_fi_bad_pr_ipv6exthdr_len, fr_info_t *, fin, u_short, shift, u_short, fin->fin_dlen);
649 LBUMPD(ipf_stats[fin->fin_out], fr_v6_ext_hlen);
650 return (NULL);
651 }
652
653 fin->fin_dp = (char *)fin->fin_dp + shift;
654 fin->fin_dlen -= shift;
655
656 /*
657 * If we have seen a fragment header, do not set any flags to indicate
658 * the presence of this extension header as it has no impact on the
659 * end result until after it has been defragmented.
660 */
661 if (fin->fin_flx & FI_FRAG)
662 return (hdr);
663
664 for (i = 0; ip6exthdr[i].ol_bit != 0; i++)
665 if (ip6exthdr[i].ol_val == proto) {
666 /*
667 * Most IPv6 extension headers are only allowed once.
668 */
669 if ((multiple == 0) &&
670 ((fin->fin_optmsk & ip6exthdr[i].ol_bit) != 0)) {
671 fin->fin_flx |= FI_BAD;
672 DT2(ipf_fi_bad_ipv6exthdr_once, fr_info_t *, fin, u_int, (fin->fin_optmsk & ip6exthdr[i].ol_bit));
673 } else
674 fin->fin_optmsk |= ip6exthdr[i].ol_bit;
675 break;
676 }
677
678 return (hdr);
679 }
680
681
682 /* ------------------------------------------------------------------------ */
683 /* Function: ipf_pr_hopopts6 */
684 /* Returns: int - value of the next header or IPPROTO_NONE if error */
685 /* Parameters: fin(I) - pointer to packet information */
686 /* */
687 /* IPv6 Only */
688 /* This is function checks pending hop by hop options extension header */
689 /* ------------------------------------------------------------------------ */
690 static inline int
ipf_pr_hopopts6(fr_info_t * fin)691 ipf_pr_hopopts6(fr_info_t *fin)
692 {
693 struct ip6_ext *hdr;
694
695 hdr = ipf_pr_ipv6exthdr(fin, 0, IPPROTO_HOPOPTS);
696 if (hdr == NULL)
697 return (IPPROTO_NONE);
698 return (hdr->ip6e_nxt);
699 }
700
701
702 /* ------------------------------------------------------------------------ */
703 /* Function: ipf_pr_mobility6 */
704 /* Returns: int - value of the next header or IPPROTO_NONE if error */
705 /* Parameters: fin(I) - pointer to packet information */
706 /* */
707 /* IPv6 Only */
708 /* This is function checks the IPv6 mobility extension header */
709 /* ------------------------------------------------------------------------ */
710 static inline int
ipf_pr_mobility6(fr_info_t * fin)711 ipf_pr_mobility6(fr_info_t *fin)
712 {
713 struct ip6_ext *hdr;
714
715 hdr = ipf_pr_ipv6exthdr(fin, 0, IPPROTO_MOBILITY);
716 if (hdr == NULL)
717 return (IPPROTO_NONE);
718 return (hdr->ip6e_nxt);
719 }
720
721
722 /* ------------------------------------------------------------------------ */
723 /* Function: ipf_pr_routing6 */
724 /* Returns: int - value of the next header or IPPROTO_NONE if error */
725 /* Parameters: fin(I) - pointer to packet information */
726 /* */
727 /* IPv6 Only */
728 /* This is function checks pending routing extension header */
729 /* ------------------------------------------------------------------------ */
730 static inline int
ipf_pr_routing6(fr_info_t * fin)731 ipf_pr_routing6(fr_info_t *fin)
732 {
733 struct ip6_routing *hdr;
734
735 hdr = (struct ip6_routing *)ipf_pr_ipv6exthdr(fin, 0, IPPROTO_ROUTING);
736 if (hdr == NULL)
737 return (IPPROTO_NONE);
738
739 switch (hdr->ip6r_type)
740 {
741 case 0 :
742 /*
743 * Nasty extension header length?
744 */
745 if (((hdr->ip6r_len >> 1) < hdr->ip6r_segleft) ||
746 (hdr->ip6r_segleft && (hdr->ip6r_len & 1))) {
747 ipf_main_softc_t *softc = fin->fin_main_soft;
748
749 fin->fin_flx |= FI_BAD;
750 DT1(ipf_fi_bad_routing6, fr_info_t *, fin);
751 LBUMPD(ipf_stats[fin->fin_out], fr_v6_rh_bad);
752 return (IPPROTO_NONE);
753 }
754 break;
755
756 default :
757 break;
758 }
759
760 return (hdr->ip6r_nxt);
761 }
762
763
764 /* ------------------------------------------------------------------------ */
765 /* Function: ipf_pr_fragment6 */
766 /* Returns: int - value of the next header or IPPROTO_NONE if error */
767 /* Parameters: fin(I) - pointer to packet information */
768 /* */
769 /* IPv6 Only */
770 /* Examine the IPv6 fragment header and extract fragment offset information.*/
771 /* */
772 /* Fragments in IPv6 are extraordinarily difficult to deal with - much more */
773 /* so than in IPv4. There are 5 cases of fragments with IPv6 that all */
774 /* packets with a fragment header can fit into. They are as follows: */
775 /* */
776 /* 1. [IPv6][0-n EH][FH][0-n EH] (no L4HDR present) */
777 /* 2. [IPV6][0-n EH][FH][0-n EH][L4HDR part] (short) */
778 /* 3. [IPV6][0-n EH][FH][L4HDR part][0-n data] (short) */
779 /* 4. [IPV6][0-n EH][FH][0-n EH][L4HDR][0-n data] */
780 /* 5. [IPV6][0-n EH][FH][data] */
781 /* */
782 /* IPV6 = IPv6 header, FH = Fragment Header, */
783 /* 0-n EH = 0 or more extension headers, 0-n data = 0 or more bytes of data */
784 /* */
785 /* Packets that match 1, 2, 3 will be dropped as the only reasonable */
786 /* scenario in which they happen is in extreme circumstances that are most */
787 /* likely to be an indication of an attack rather than normal traffic. */
788 /* A type 3 packet may be sent by an attacked after a type 4 packet. There */
789 /* are two rules that can be used to guard against type 3 packets: L4 */
790 /* headers must always be in a packet that has the offset field set to 0 */
791 /* and no packet is allowed to overlay that where offset = 0. */
792 /* ------------------------------------------------------------------------ */
793 static inline int
ipf_pr_fragment6(fr_info_t * fin)794 ipf_pr_fragment6(fr_info_t *fin)
795 {
796 ipf_main_softc_t *softc = fin->fin_main_soft;
797 struct ip6_frag *frag;
798
799 fin->fin_flx |= FI_FRAG;
800
801 frag = (struct ip6_frag *)ipf_pr_ipv6exthdr(fin, 0, IPPROTO_FRAGMENT);
802 if (frag == NULL) {
803 LBUMPD(ipf_stats[fin->fin_out], fr_v6_frag_bad);
804 return (IPPROTO_NONE);
805 }
806
807 if ((frag->ip6f_offlg & IP6F_MORE_FRAG) != 0) {
808 /*
809 * Any fragment that isn't the last fragment must have its
810 * length as a multiple of 8.
811 */
812 if ((fin->fin_plen & 7) != 0) {
813 fin->fin_flx |= FI_BAD;
814 DT2(ipf_fi_bad_frag_not_8, fr_info_t *, fin, u_int, (fin->fin_plen & 7));
815 }
816 }
817
818 fin->fin_fraghdr = frag;
819 fin->fin_id = frag->ip6f_ident;
820 fin->fin_off = ntohs(frag->ip6f_offlg & IP6F_OFF_MASK);
821 if (fin->fin_off != 0)
822 fin->fin_flx |= FI_FRAGBODY;
823
824 /*
825 * Jumbograms aren't handled, so the max. length is 64k
826 */
827 if ((fin->fin_off << 3) + fin->fin_dlen > 65535) {
828 fin->fin_flx |= FI_BAD;
829 DT2(ipf_fi_bad_jumbogram, fr_info_t *, fin, u_int, ((fin->fin_off << 3) + fin->fin_dlen));
830 }
831
832 /*
833 * We don't know where the transport layer header (or whatever is next
834 * is), as it could be behind destination options (amongst others) so
835 * return the fragment header as the type of packet this is. Note that
836 * this effectively disables the fragment cache for > 1 protocol at a
837 * time.
838 */
839 return (frag->ip6f_nxt);
840 }
841
842
843 /* ------------------------------------------------------------------------ */
844 /* Function: ipf_pr_dstopts6 */
845 /* Returns: int - value of the next header or IPPROTO_NONE if error */
846 /* Parameters: fin(I) - pointer to packet information */
847 /* */
848 /* IPv6 Only */
849 /* This is function checks pending destination options extension header */
850 /* ------------------------------------------------------------------------ */
851 static inline int
ipf_pr_dstopts6(fr_info_t * fin)852 ipf_pr_dstopts6(fr_info_t *fin)
853 {
854 ipf_main_softc_t *softc = fin->fin_main_soft;
855 struct ip6_ext *hdr;
856
857 hdr = ipf_pr_ipv6exthdr(fin, 0, IPPROTO_DSTOPTS);
858 if (hdr == NULL) {
859 LBUMPD(ipf_stats[fin->fin_out], fr_v6_dst_bad);
860 return (IPPROTO_NONE);
861 }
862 return (hdr->ip6e_nxt);
863 }
864
865
866 /* ------------------------------------------------------------------------ */
867 /* Function: ipf_pr_icmp6 */
868 /* Returns: void */
869 /* Parameters: fin(I) - pointer to packet information */
870 /* */
871 /* IPv6 Only */
872 /* This routine is mainly concerned with determining the minimum valid size */
873 /* for an ICMPv6 packet. */
874 /* ------------------------------------------------------------------------ */
875 static inline void
ipf_pr_icmp6(fr_info_t * fin)876 ipf_pr_icmp6(fr_info_t *fin)
877 {
878 int minicmpsz = sizeof(struct icmp6_hdr);
879 struct icmp6_hdr *icmp6;
880
881 if (ipf_pr_pullup(fin, ICMP6ERR_MINPKTLEN - sizeof(ip6_t)) == -1) {
882 ipf_main_softc_t *softc = fin->fin_main_soft;
883
884 LBUMPD(ipf_stats[fin->fin_out], fr_v6_icmp6_pullup);
885 return;
886 }
887
888 if (fin->fin_dlen > 1) {
889 ip6_t *ip6;
890
891 icmp6 = fin->fin_dp;
892
893 fin->fin_data[0] = *(u_short *)icmp6;
894
895 if ((icmp6->icmp6_type & ICMP6_INFOMSG_MASK) != 0)
896 fin->fin_flx |= FI_ICMPQUERY;
897
898 switch (icmp6->icmp6_type)
899 {
900 case ICMP6_ECHO_REPLY :
901 case ICMP6_ECHO_REQUEST :
902 if (fin->fin_dlen >= 6)
903 fin->fin_data[1] = icmp6->icmp6_id;
904 minicmpsz = ICMP6ERR_MINPKTLEN - sizeof(ip6_t);
905 break;
906
907 case ICMP6_DST_UNREACH :
908 case ICMP6_PACKET_TOO_BIG :
909 case ICMP6_TIME_EXCEEDED :
910 case ICMP6_PARAM_PROB :
911 fin->fin_flx |= FI_ICMPERR;
912 minicmpsz = ICMP6ERR_IPICMPHLEN - sizeof(ip6_t);
913 if (fin->fin_plen < ICMP6ERR_IPICMPHLEN)
914 break;
915
916 if (M_LEN(fin->fin_m) < fin->fin_plen) {
917 if (ipf_coalesce(fin) != 1)
918 return;
919 }
920
921 if (ipf_pr_pullup(fin, ICMP6ERR_MINPKTLEN) == -1)
922 return;
923
924 /*
925 * If the destination of this packet doesn't match the
926 * source of the original packet then this packet is
927 * not correct.
928 */
929 icmp6 = fin->fin_dp;
930 ip6 = (ip6_t *)((char *)icmp6 + ICMPERR_ICMPHLEN);
931 if (IP6_NEQ(&fin->fin_fi.fi_dst,
932 (i6addr_t *)&ip6->ip6_src)) {
933 fin->fin_flx |= FI_BAD;
934 DT1(ipf_fi_bad_icmp6, fr_info_t *, fin);
935 }
936 break;
937 default :
938 break;
939 }
940 }
941
942 ipf_pr_short6(fin, minicmpsz);
943 if ((fin->fin_flx & (FI_SHORT|FI_BAD)) == 0) {
944 u_char p = fin->fin_p;
945
946 fin->fin_p = IPPROTO_ICMPV6;
947 ipf_checkv6sum(fin);
948 fin->fin_p = p;
949 }
950 }
951
952
953 /* ------------------------------------------------------------------------ */
954 /* Function: ipf_pr_udp6 */
955 /* Returns: void */
956 /* Parameters: fin(I) - pointer to packet information */
957 /* */
958 /* IPv6 Only */
959 /* Analyse the packet for IPv6/UDP properties. */
960 /* Is not expected to be called for fragmented packets. */
961 /* ------------------------------------------------------------------------ */
962 static inline void
ipf_pr_udp6(fr_info_t * fin)963 ipf_pr_udp6(fr_info_t *fin)
964 {
965
966 if (ipf_pr_udpcommon(fin) == 0) {
967 u_char p = fin->fin_p;
968
969 fin->fin_p = IPPROTO_UDP;
970 ipf_checkv6sum(fin);
971 fin->fin_p = p;
972 }
973 }
974
975
976 /* ------------------------------------------------------------------------ */
977 /* Function: ipf_pr_tcp6 */
978 /* Returns: void */
979 /* Parameters: fin(I) - pointer to packet information */
980 /* */
981 /* IPv6 Only */
982 /* Analyse the packet for IPv6/TCP properties. */
983 /* Is not expected to be called for fragmented packets. */
984 /* ------------------------------------------------------------------------ */
985 static inline void
ipf_pr_tcp6(fr_info_t * fin)986 ipf_pr_tcp6(fr_info_t *fin)
987 {
988
989 if (ipf_pr_tcpcommon(fin) == 0) {
990 u_char p = fin->fin_p;
991
992 fin->fin_p = IPPROTO_TCP;
993 ipf_checkv6sum(fin);
994 fin->fin_p = p;
995 }
996 }
997
998
999 /* ------------------------------------------------------------------------ */
1000 /* Function: ipf_pr_esp6 */
1001 /* Returns: void */
1002 /* Parameters: fin(I) - pointer to packet information */
1003 /* */
1004 /* IPv6 Only */
1005 /* Analyse the packet for ESP properties. */
1006 /* The minimum length is taken to be the SPI (32bits) plus a tail (32bits) */
1007 /* even though the newer ESP packets must also have a sequence number that */
1008 /* is 32bits as well, it is not possible(?) to determine the version from a */
1009 /* simple packet header. */
1010 /* ------------------------------------------------------------------------ */
1011 static inline void
ipf_pr_esp6(fr_info_t * fin)1012 ipf_pr_esp6(fr_info_t *fin)
1013 {
1014
1015 if ((fin->fin_off == 0) && (ipf_pr_pullup(fin, 8) == -1)) {
1016 ipf_main_softc_t *softc = fin->fin_main_soft;
1017
1018 LBUMPD(ipf_stats[fin->fin_out], fr_v6_esp_pullup);
1019 return;
1020 }
1021 }
1022
1023
1024 /* ------------------------------------------------------------------------ */
1025 /* Function: ipf_pr_ah6 */
1026 /* Returns: int - value of the next header or IPPROTO_NONE if error */
1027 /* Parameters: fin(I) - pointer to packet information */
1028 /* */
1029 /* IPv6 Only */
1030 /* Analyse the packet for AH properties. */
1031 /* The minimum length is taken to be the combination of all fields in the */
1032 /* header being present and no authentication data (null algorithm used.) */
1033 /* ------------------------------------------------------------------------ */
1034 static inline int
ipf_pr_ah6(fr_info_t * fin)1035 ipf_pr_ah6(fr_info_t *fin)
1036 {
1037 authhdr_t *ah;
1038
1039 fin->fin_flx |= FI_AH;
1040
1041 ah = (authhdr_t *)ipf_pr_ipv6exthdr(fin, 0, IPPROTO_HOPOPTS);
1042 if (ah == NULL) {
1043 ipf_main_softc_t *softc = fin->fin_main_soft;
1044
1045 LBUMPD(ipf_stats[fin->fin_out], fr_v6_ah_bad);
1046 return (IPPROTO_NONE);
1047 }
1048
1049 ipf_pr_short6(fin, sizeof(*ah));
1050
1051 /*
1052 * No need for another pullup, ipf_pr_ipv6exthdr() will pullup
1053 * enough data to satisfy ah_next (the very first one.)
1054 */
1055 return (ah->ah_next);
1056 }
1057
1058
1059 /* ------------------------------------------------------------------------ */
1060 /* Function: ipf_pr_gre6 */
1061 /* Returns: void */
1062 /* Parameters: fin(I) - pointer to packet information */
1063 /* */
1064 /* Analyse the packet for GRE properties. */
1065 /* ------------------------------------------------------------------------ */
1066 static inline void
ipf_pr_gre6(fr_info_t * fin)1067 ipf_pr_gre6(fr_info_t *fin)
1068 {
1069 grehdr_t *gre;
1070
1071 if (ipf_pr_pullup(fin, sizeof(grehdr_t)) == -1) {
1072 ipf_main_softc_t *softc = fin->fin_main_soft;
1073
1074 LBUMPD(ipf_stats[fin->fin_out], fr_v6_gre_pullup);
1075 return;
1076 }
1077
1078 gre = fin->fin_dp;
1079 if (GRE_REV(gre->gr_flags) == 1)
1080 fin->fin_data[0] = gre->gr_call;
1081 }
1082 #endif /* USE_INET6 */
1083
1084
1085 /* ------------------------------------------------------------------------ */
1086 /* Function: ipf_pr_pullup */
1087 /* Returns: int - 0 == pullup succeeded, -1 == failure */
1088 /* Parameters: fin(I) - pointer to packet information */
1089 /* plen(I) - length (excluding L3 header) to pullup */
1090 /* */
1091 /* Short inline function to cut down on code duplication to perform a call */
1092 /* to ipf_pullup to ensure there is the required amount of data, */
1093 /* consecutively in the packet buffer. */
1094 /* */
1095 /* This function pulls up 'extra' data at the location of fin_dp. fin_dp */
1096 /* points to the first byte after the complete layer 3 header, which will */
1097 /* include all of the known extension headers for IPv6 or options for IPv4. */
1098 /* */
1099 /* Since fr_pullup() expects the total length of bytes to be pulled up, it */
1100 /* is necessary to add those we can already assume to be pulled up (fin_dp */
1101 /* - fin_ip) to what is passed through. */
1102 /* ------------------------------------------------------------------------ */
1103 int
ipf_pr_pullup(fr_info_t * fin,int plen)1104 ipf_pr_pullup(fr_info_t *fin, int plen)
1105 {
1106 ipf_main_softc_t *softc = fin->fin_main_soft;
1107
1108 if (fin->fin_m != NULL) {
1109 if (fin->fin_dp != NULL)
1110 plen += (char *)fin->fin_dp -
1111 ((char *)fin->fin_ip + fin->fin_hlen);
1112 plen += fin->fin_hlen;
1113 if (M_LEN(fin->fin_m) < plen + fin->fin_ipoff) {
1114 #if defined(_KERNEL)
1115 if (ipf_pullup(fin->fin_m, fin, plen) == NULL) {
1116 DT1(ipf_pullup_fail, fr_info_t *, fin);
1117 LBUMP(ipf_stats[fin->fin_out].fr_pull[1]);
1118 fin->fin_reason = FRB_PULLUP;
1119 fin->fin_flx |= FI_BAD;
1120 return (-1);
1121 }
1122 LBUMP(ipf_stats[fin->fin_out].fr_pull[0]);
1123 #else
1124 LBUMP(ipf_stats[fin->fin_out].fr_pull[1]);
1125 /*
1126 * Fake ipf_pullup failing
1127 */
1128 fin->fin_reason = FRB_PULLUP;
1129 *fin->fin_mp = NULL;
1130 fin->fin_m = NULL;
1131 fin->fin_ip = NULL;
1132 fin->fin_flx |= FI_BAD;
1133 return (-1);
1134 #endif
1135 }
1136 }
1137 return (0);
1138 }
1139
1140
1141 /* ------------------------------------------------------------------------ */
1142 /* Function: ipf_pr_short */
1143 /* Returns: void */
1144 /* Parameters: fin(I) - pointer to packet information */
1145 /* xmin(I) - minimum header size */
1146 /* */
1147 /* Check if a packet is "short" as defined by xmin. The rule we are */
1148 /* applying here is that the packet must not be fragmented within the layer */
1149 /* 4 header. That is, it must not be a fragment that has its offset set to */
1150 /* start within the layer 4 header (hdrmin) or if it is at offset 0, the */
1151 /* entire layer 4 header must be present (min). */
1152 /* ------------------------------------------------------------------------ */
1153 static inline void
ipf_pr_short(fr_info_t * fin,int xmin)1154 ipf_pr_short(fr_info_t *fin, int xmin)
1155 {
1156
1157 if (fin->fin_off == 0) {
1158 if (fin->fin_dlen < xmin)
1159 fin->fin_flx |= FI_SHORT;
1160 } else if (fin->fin_off < xmin) {
1161 fin->fin_flx |= FI_SHORT;
1162 }
1163 }
1164
1165
1166 /* ------------------------------------------------------------------------ */
1167 /* Function: ipf_pr_icmp */
1168 /* Returns: void */
1169 /* Parameters: fin(I) - pointer to packet information */
1170 /* */
1171 /* IPv4 Only */
1172 /* Do a sanity check on the packet for ICMP (v4). In nearly all cases, */
1173 /* except extrememly bad packets, both type and code will be present. */
1174 /* The expected minimum size of an ICMP packet is very much dependent on */
1175 /* the type of it. */
1176 /* */
1177 /* XXX - other ICMP sanity checks? */
1178 /* ------------------------------------------------------------------------ */
1179 static inline void
ipf_pr_icmp(fr_info_t * fin)1180 ipf_pr_icmp(fr_info_t *fin)
1181 {
1182 ipf_main_softc_t *softc = fin->fin_main_soft;
1183 int minicmpsz = sizeof(struct icmp);
1184 icmphdr_t *icmp;
1185 ip_t *oip;
1186
1187 ipf_pr_short(fin, ICMPERR_ICMPHLEN);
1188
1189 if (fin->fin_off != 0) {
1190 LBUMPD(ipf_stats[fin->fin_out], fr_v4_icmp_frag);
1191 return;
1192 }
1193
1194 if (ipf_pr_pullup(fin, ICMPERR_ICMPHLEN) == -1) {
1195 LBUMPD(ipf_stats[fin->fin_out], fr_v4_icmp_pullup);
1196 return;
1197 }
1198
1199 icmp = fin->fin_dp;
1200
1201 fin->fin_data[0] = *(u_short *)icmp;
1202 fin->fin_data[1] = icmp->icmp_id;
1203
1204 switch (icmp->icmp_type)
1205 {
1206 case ICMP_ECHOREPLY :
1207 case ICMP_ECHO :
1208 /* Router discovery messaes - RFC 1256 */
1209 case ICMP_ROUTERADVERT :
1210 case ICMP_ROUTERSOLICIT :
1211 fin->fin_flx |= FI_ICMPQUERY;
1212 minicmpsz = ICMP_MINLEN;
1213 break;
1214 /*
1215 * type(1) + code(1) + cksum(2) + id(2) seq(2) +
1216 * 3 * timestamp(3 * 4)
1217 */
1218 case ICMP_TSTAMP :
1219 case ICMP_TSTAMPREPLY :
1220 fin->fin_flx |= FI_ICMPQUERY;
1221 minicmpsz = 20;
1222 break;
1223 /*
1224 * type(1) + code(1) + cksum(2) + id(2) seq(2) +
1225 * mask(4)
1226 */
1227 case ICMP_IREQ :
1228 case ICMP_IREQREPLY :
1229 case ICMP_MASKREQ :
1230 case ICMP_MASKREPLY :
1231 fin->fin_flx |= FI_ICMPQUERY;
1232 minicmpsz = 12;
1233 break;
1234 /*
1235 * type(1) + code(1) + cksum(2) + id(2) seq(2) + ip(20+)
1236 */
1237 case ICMP_UNREACH :
1238 #ifdef icmp_nextmtu
1239 if (icmp->icmp_code == ICMP_UNREACH_NEEDFRAG) {
1240 if (icmp->icmp_nextmtu < softc->ipf_icmpminfragmtu) {
1241 fin->fin_flx |= FI_BAD;
1242 DT3(ipf_fi_bad_icmp_nextmtu, fr_info_t *, fin, u_int, icmp->icmp_nextmtu, u_int, softc->ipf_icmpminfragmtu);
1243 }
1244 }
1245 #endif
1246 /* FALLTHROUGH */
1247 case ICMP_SOURCEQUENCH :
1248 case ICMP_REDIRECT :
1249 case ICMP_TIMXCEED :
1250 case ICMP_PARAMPROB :
1251 fin->fin_flx |= FI_ICMPERR;
1252 if (ipf_coalesce(fin) != 1) {
1253 LBUMPD(ipf_stats[fin->fin_out], fr_icmp_coalesce);
1254 return;
1255 }
1256
1257 /*
1258 * ICMP error packets should not be generated for IP
1259 * packets that are a fragment that isn't the first
1260 * fragment.
1261 */
1262 oip = (ip_t *)((char *)fin->fin_dp + ICMPERR_ICMPHLEN);
1263 if ((ntohs(oip->ip_off) & IP_OFFMASK) != 0) {
1264 fin->fin_flx |= FI_BAD;
1265 DT2(ipf_fi_bad_icmp_err, fr_info_t, fin, u_int, (ntohs(oip->ip_off) & IP_OFFMASK));
1266 }
1267
1268 /*
1269 * If the destination of this packet doesn't match the
1270 * source of the original packet then this packet is
1271 * not correct.
1272 */
1273 if (oip->ip_src.s_addr != fin->fin_daddr) {
1274 fin->fin_flx |= FI_BAD;
1275 DT1(ipf_fi_bad_src_ne_dst, fr_info_t *, fin);
1276 }
1277 break;
1278 default :
1279 break;
1280 }
1281
1282 ipf_pr_short(fin, minicmpsz);
1283
1284 ipf_checkv4sum(fin);
1285 }
1286
1287
1288 /* ------------------------------------------------------------------------ */
1289 /* Function: ipf_pr_tcpcommon */
1290 /* Returns: int - 0 = header ok, 1 = bad packet, -1 = buffer error */
1291 /* Parameters: fin(I) - pointer to packet information */
1292 /* */
1293 /* TCP header sanity checking. Look for bad combinations of TCP flags, */
1294 /* and make some checks with how they interact with other fields. */
1295 /* If compiled with IPFILTER_CKSUM, check to see if the TCP checksum is */
1296 /* valid and mark the packet as bad if not. */
1297 /* ------------------------------------------------------------------------ */
1298 static inline int
ipf_pr_tcpcommon(fr_info_t * fin)1299 ipf_pr_tcpcommon(fr_info_t *fin)
1300 {
1301 ipf_main_softc_t *softc = fin->fin_main_soft;
1302 int flags, tlen;
1303 tcphdr_t *tcp;
1304
1305 fin->fin_flx |= FI_TCPUDP;
1306 if (fin->fin_off != 0) {
1307 LBUMPD(ipf_stats[fin->fin_out], fr_tcp_frag);
1308 return (0);
1309 }
1310
1311 if (ipf_pr_pullup(fin, sizeof(*tcp)) == -1) {
1312 LBUMPD(ipf_stats[fin->fin_out], fr_tcp_pullup);
1313 return (-1);
1314 }
1315
1316 tcp = fin->fin_dp;
1317 if (fin->fin_dlen > 3) {
1318 fin->fin_sport = ntohs(tcp->th_sport);
1319 fin->fin_dport = ntohs(tcp->th_dport);
1320 }
1321
1322 if ((fin->fin_flx & FI_SHORT) != 0) {
1323 LBUMPD(ipf_stats[fin->fin_out], fr_tcp_short);
1324 return (1);
1325 }
1326
1327 /*
1328 * Use of the TCP data offset *must* result in a value that is at
1329 * least the same size as the TCP header.
1330 */
1331 tlen = TCP_OFF(tcp) << 2;
1332 if (tlen < sizeof(tcphdr_t)) {
1333 LBUMPD(ipf_stats[fin->fin_out], fr_tcp_small);
1334 fin->fin_flx |= FI_BAD;
1335 DT3(ipf_fi_bad_tlen, fr_info_t, fin, u_int, tlen, u_int, sizeof(tcphdr_t));
1336 return (1);
1337 }
1338
1339 flags = tcp->th_flags;
1340 fin->fin_tcpf = tcp->th_flags;
1341
1342 /*
1343 * If the urgent flag is set, then the urgent pointer must
1344 * also be set and vice versa. Good TCP packets do not have
1345 * just one of these set.
1346 */
1347 if ((flags & TH_URG) != 0 && (tcp->th_urp == 0)) {
1348 fin->fin_flx |= FI_BAD;
1349 DT3(ipf_fi_bad_th_urg, fr_info_t*, fin, u_int, (flags & TH_URG), u_int, tcp->th_urp);
1350 #if 0
1351 } else if ((flags & TH_URG) == 0 && (tcp->th_urp != 0)) {
1352 /*
1353 * Ignore this case (#if 0) as it shows up in "real"
1354 * traffic with bogus values in the urgent pointer field.
1355 */
1356 fin->fin_flx |= FI_BAD;
1357 DT3(ipf_fi_bad_th_urg0, fr_info_t *, fin, u_int, (flags & TH_URG), u_int, tcp->th_urp);
1358 #endif
1359 } else if (((flags & (TH_SYN|TH_FIN)) != 0) &&
1360 ((flags & (TH_RST|TH_ACK)) == TH_RST)) {
1361 /* TH_FIN|TH_RST|TH_ACK seems to appear "naturally" */
1362 fin->fin_flx |= FI_BAD;
1363 DT1(ipf_fi_bad_th_fin_rst_ack, fr_info_t, fin);
1364 #if 1
1365 } else if (((flags & TH_SYN) != 0) &&
1366 ((flags & (TH_URG|TH_PUSH)) != 0)) {
1367 /*
1368 * SYN with URG and PUSH set is not for normal TCP but it is
1369 * possible(?) with T/TCP...but who uses T/TCP?
1370 */
1371 fin->fin_flx |= FI_BAD;
1372 DT1(ipf_fi_bad_th_syn_urg_psh, fr_info_t *, fin);
1373 #endif
1374 } else if (!(flags & TH_ACK)) {
1375 /*
1376 * If the ack bit isn't set, then either the SYN or
1377 * RST bit must be set. If the SYN bit is set, then
1378 * we expect the ACK field to be 0. If the ACK is
1379 * not set and if URG, PSH or FIN are set, consdier
1380 * that to indicate a bad TCP packet.
1381 */
1382 if ((flags == TH_SYN) && (tcp->th_ack != 0)) {
1383 /*
1384 * Cisco PIX sets the ACK field to a random value.
1385 * In light of this, do not set FI_BAD until a patch
1386 * is available from Cisco to ensure that
1387 * interoperability between existing systems is
1388 * achieved.
1389 */
1390 /*fin->fin_flx |= FI_BAD*/;
1391 /*DT1(ipf_fi_bad_th_syn_ack, fr_info_t *, fin);*/
1392 } else if (!(flags & (TH_RST|TH_SYN))) {
1393 fin->fin_flx |= FI_BAD;
1394 DT1(ipf_fi_bad_th_rst_syn, fr_info_t *, fin);
1395 } else if ((flags & (TH_URG|TH_PUSH|TH_FIN)) != 0) {
1396 fin->fin_flx |= FI_BAD;
1397 DT1(ipf_fi_bad_th_urg_push_fin, fr_info_t *, fin);
1398 }
1399 }
1400 if (fin->fin_flx & FI_BAD) {
1401 LBUMPD(ipf_stats[fin->fin_out], fr_tcp_bad_flags);
1402 return (1);
1403 }
1404
1405 /*
1406 * At this point, it's not exactly clear what is to be gained by
1407 * marking up which TCP options are and are not present. The one we
1408 * are most interested in is the TCP window scale. This is only in
1409 * a SYN packet [RFC1323] so we don't need this here...?
1410 * Now if we were to analyse the header for passive fingerprinting,
1411 * then that might add some weight to adding this...
1412 */
1413 if (tlen == sizeof(tcphdr_t)) {
1414 return (0);
1415 }
1416
1417 if (ipf_pr_pullup(fin, tlen) == -1) {
1418 LBUMPD(ipf_stats[fin->fin_out], fr_tcp_pullup);
1419 return (-1);
1420 }
1421
1422 #if 0
1423 tcp = fin->fin_dp;
1424 ip = fin->fin_ip;
1425 s = (u_char *)(tcp + 1);
1426 off = IP_HL(ip) << 2;
1427 # ifdef _KERNEL
1428 if (fin->fin_mp != NULL) {
1429 mb_t *m = *fin->fin_mp;
1430
1431 if (off + tlen > M_LEN(m))
1432 return;
1433 }
1434 # endif
1435 for (tlen -= (int)sizeof(*tcp); tlen > 0; ) {
1436 opt = *s;
1437 if (opt == '\0')
1438 break;
1439 else if (opt == TCPOPT_NOP)
1440 ol = 1;
1441 else {
1442 if (tlen < 2)
1443 break;
1444 ol = (int)*(s + 1);
1445 if (ol < 2 || ol > tlen)
1446 break;
1447 }
1448
1449 for (i = 9, mv = 4; mv >= 0; ) {
1450 op = ipopts + i;
1451 if (opt == (u_char)op->ol_val) {
1452 optmsk |= op->ol_bit;
1453 break;
1454 }
1455 }
1456 tlen -= ol;
1457 s += ol;
1458 }
1459 #endif /* 0 */
1460
1461 return (0);
1462 }
1463
1464
1465
1466 /* ------------------------------------------------------------------------ */
1467 /* Function: ipf_pr_udpcommon */
1468 /* Returns: int - 0 = header ok, 1 = bad packet */
1469 /* Parameters: fin(I) - pointer to packet information */
1470 /* */
1471 /* Extract the UDP source and destination ports, if present. If compiled */
1472 /* with IPFILTER_CKSUM, check to see if the UDP checksum is valid. */
1473 /* ------------------------------------------------------------------------ */
1474 static inline int
ipf_pr_udpcommon(fr_info_t * fin)1475 ipf_pr_udpcommon(fr_info_t *fin)
1476 {
1477 udphdr_t *udp;
1478
1479 fin->fin_flx |= FI_TCPUDP;
1480
1481 if (!fin->fin_off && (fin->fin_dlen > 3)) {
1482 if (ipf_pr_pullup(fin, sizeof(*udp)) == -1) {
1483 ipf_main_softc_t *softc = fin->fin_main_soft;
1484
1485 fin->fin_flx |= FI_SHORT;
1486 LBUMPD(ipf_stats[fin->fin_out], fr_udp_pullup);
1487 return (1);
1488 }
1489
1490 udp = fin->fin_dp;
1491
1492 fin->fin_sport = ntohs(udp->uh_sport);
1493 fin->fin_dport = ntohs(udp->uh_dport);
1494 }
1495
1496 return (0);
1497 }
1498
1499
1500 /* ------------------------------------------------------------------------ */
1501 /* Function: ipf_pr_tcp */
1502 /* Returns: void */
1503 /* Parameters: fin(I) - pointer to packet information */
1504 /* */
1505 /* IPv4 Only */
1506 /* Analyse the packet for IPv4/TCP properties. */
1507 /* ------------------------------------------------------------------------ */
1508 static inline void
ipf_pr_tcp(fr_info_t * fin)1509 ipf_pr_tcp(fr_info_t *fin)
1510 {
1511
1512 ipf_pr_short(fin, sizeof(tcphdr_t));
1513
1514 if (ipf_pr_tcpcommon(fin) == 0)
1515 ipf_checkv4sum(fin);
1516 }
1517
1518
1519 /* ------------------------------------------------------------------------ */
1520 /* Function: ipf_pr_udp */
1521 /* Returns: void */
1522 /* Parameters: fin(I) - pointer to packet information */
1523 /* */
1524 /* IPv4 Only */
1525 /* Analyse the packet for IPv4/UDP properties. */
1526 /* ------------------------------------------------------------------------ */
1527 static inline void
ipf_pr_udp(fr_info_t * fin)1528 ipf_pr_udp(fr_info_t *fin)
1529 {
1530
1531 ipf_pr_short(fin, sizeof(udphdr_t));
1532
1533 if (ipf_pr_udpcommon(fin) == 0)
1534 ipf_checkv4sum(fin);
1535 }
1536
1537
1538 /* ------------------------------------------------------------------------ */
1539 /* Function: ipf_pr_esp */
1540 /* Returns: void */
1541 /* Parameters: fin(I) - pointer to packet information */
1542 /* */
1543 /* Analyse the packet for ESP properties. */
1544 /* The minimum length is taken to be the SPI (32bits) plus a tail (32bits) */
1545 /* even though the newer ESP packets must also have a sequence number that */
1546 /* is 32bits as well, it is not possible(?) to determine the version from a */
1547 /* simple packet header. */
1548 /* ------------------------------------------------------------------------ */
1549 static inline void
ipf_pr_esp(fr_info_t * fin)1550 ipf_pr_esp(fr_info_t *fin)
1551 {
1552
1553 if (fin->fin_off == 0) {
1554 ipf_pr_short(fin, 8);
1555 if (ipf_pr_pullup(fin, 8) == -1) {
1556 ipf_main_softc_t *softc = fin->fin_main_soft;
1557
1558 LBUMPD(ipf_stats[fin->fin_out], fr_v4_esp_pullup);
1559 }
1560 }
1561 }
1562
1563
1564 /* ------------------------------------------------------------------------ */
1565 /* Function: ipf_pr_ah */
1566 /* Returns: int - value of the next header or IPPROTO_NONE if error */
1567 /* Parameters: fin(I) - pointer to packet information */
1568 /* */
1569 /* Analyse the packet for AH properties. */
1570 /* The minimum length is taken to be the combination of all fields in the */
1571 /* header being present and no authentication data (null algorithm used.) */
1572 /* ------------------------------------------------------------------------ */
1573 static inline int
ipf_pr_ah(fr_info_t * fin)1574 ipf_pr_ah(fr_info_t *fin)
1575 {
1576 ipf_main_softc_t *softc = fin->fin_main_soft;
1577 authhdr_t *ah;
1578 int len;
1579
1580 fin->fin_flx |= FI_AH;
1581 ipf_pr_short(fin, sizeof(*ah));
1582
1583 if (((fin->fin_flx & FI_SHORT) != 0) || (fin->fin_off != 0)) {
1584 LBUMPD(ipf_stats[fin->fin_out], fr_v4_ah_bad);
1585 return (IPPROTO_NONE);
1586 }
1587
1588 if (ipf_pr_pullup(fin, sizeof(*ah)) == -1) {
1589 DT(fr_v4_ah_pullup_1);
1590 LBUMP(ipf_stats[fin->fin_out].fr_v4_ah_pullup);
1591 return (IPPROTO_NONE);
1592 }
1593
1594 ah = (authhdr_t *)fin->fin_dp;
1595
1596 len = (ah->ah_plen + 2) << 2;
1597 ipf_pr_short(fin, len);
1598 if (ipf_pr_pullup(fin, len) == -1) {
1599 DT(fr_v4_ah_pullup_2);
1600 LBUMP(ipf_stats[fin->fin_out].fr_v4_ah_pullup);
1601 return (IPPROTO_NONE);
1602 }
1603
1604 /*
1605 * Adjust fin_dp and fin_dlen for skipping over the authentication
1606 * header.
1607 */
1608 fin->fin_dp = (char *)fin->fin_dp + len;
1609 fin->fin_dlen -= len;
1610 return (ah->ah_next);
1611 }
1612
1613
1614 /* ------------------------------------------------------------------------ */
1615 /* Function: ipf_pr_gre */
1616 /* Returns: void */
1617 /* Parameters: fin(I) - pointer to packet information */
1618 /* */
1619 /* Analyse the packet for GRE properties. */
1620 /* ------------------------------------------------------------------------ */
1621 static inline void
ipf_pr_gre(fr_info_t * fin)1622 ipf_pr_gre(fr_info_t *fin)
1623 {
1624 ipf_main_softc_t *softc = fin->fin_main_soft;
1625 grehdr_t *gre;
1626
1627 ipf_pr_short(fin, sizeof(grehdr_t));
1628
1629 if (fin->fin_off != 0) {
1630 LBUMPD(ipf_stats[fin->fin_out], fr_v4_gre_frag);
1631 return;
1632 }
1633
1634 if (ipf_pr_pullup(fin, sizeof(grehdr_t)) == -1) {
1635 LBUMPD(ipf_stats[fin->fin_out], fr_v4_gre_pullup);
1636 return;
1637 }
1638
1639 gre = fin->fin_dp;
1640 if (GRE_REV(gre->gr_flags) == 1)
1641 fin->fin_data[0] = gre->gr_call;
1642 }
1643
1644
1645 /* ------------------------------------------------------------------------ */
1646 /* Function: ipf_pr_ipv4hdr */
1647 /* Returns: void */
1648 /* Parameters: fin(I) - pointer to packet information */
1649 /* */
1650 /* IPv4 Only */
1651 /* Analyze the IPv4 header and set fields in the fr_info_t structure. */
1652 /* Check all options present and flag their presence if any exist. */
1653 /* ------------------------------------------------------------------------ */
1654 static inline void
ipf_pr_ipv4hdr(fr_info_t * fin)1655 ipf_pr_ipv4hdr(fr_info_t *fin)
1656 {
1657 u_short optmsk = 0, secmsk = 0, auth = 0;
1658 int hlen, ol, mv, p, i;
1659 const struct optlist *op;
1660 u_char *s, opt;
1661 u_short off;
1662 fr_ip_t *fi;
1663 ip_t *ip;
1664
1665 fi = &fin->fin_fi;
1666 hlen = fin->fin_hlen;
1667
1668 ip = fin->fin_ip;
1669 p = ip->ip_p;
1670 fi->fi_p = p;
1671 fin->fin_crc = p;
1672 fi->fi_tos = ip->ip_tos;
1673 fin->fin_id = ntohs(ip->ip_id);
1674 off = ntohs(ip->ip_off);
1675
1676 /* Get both TTL and protocol */
1677 fi->fi_p = ip->ip_p;
1678 fi->fi_ttl = ip->ip_ttl;
1679
1680 /* Zero out bits not used in IPv6 address */
1681 fi->fi_src.i6[1] = 0;
1682 fi->fi_src.i6[2] = 0;
1683 fi->fi_src.i6[3] = 0;
1684 fi->fi_dst.i6[1] = 0;
1685 fi->fi_dst.i6[2] = 0;
1686 fi->fi_dst.i6[3] = 0;
1687
1688 fi->fi_saddr = ip->ip_src.s_addr;
1689 fin->fin_crc += fi->fi_saddr;
1690 fi->fi_daddr = ip->ip_dst.s_addr;
1691 fin->fin_crc += fi->fi_daddr;
1692 if (IN_CLASSD(ntohl(fi->fi_daddr)))
1693 fin->fin_flx |= FI_MULTICAST|FI_MBCAST;
1694
1695 /*
1696 * set packet attribute flags based on the offset and
1697 * calculate the byte offset that it represents.
1698 */
1699 off &= IP_MF|IP_OFFMASK;
1700 if (off != 0) {
1701 int morefrag = off & IP_MF;
1702
1703 fi->fi_flx |= FI_FRAG;
1704 off &= IP_OFFMASK;
1705 if (off == 1 && p == IPPROTO_TCP) {
1706 fin->fin_flx |= FI_SHORT; /* RFC 3128 */
1707 DT1(ipf_fi_tcp_frag_off_1, fr_info_t *, fin);
1708 }
1709 if (off != 0) {
1710 fin->fin_flx |= FI_FRAGBODY;
1711 off <<= 3;
1712 if ((off + fin->fin_dlen > 65535) ||
1713 (fin->fin_dlen == 0) ||
1714 ((morefrag != 0) && ((fin->fin_dlen & 7) != 0))) {
1715 /*
1716 * The length of the packet, starting at its
1717 * offset cannot exceed 65535 (0xffff) as the
1718 * length of an IP packet is only 16 bits.
1719 *
1720 * Any fragment that isn't the last fragment
1721 * must have a length greater than 0 and it
1722 * must be an even multiple of 8.
1723 */
1724 fi->fi_flx |= FI_BAD;
1725 DT1(ipf_fi_bad_fragbody_gt_65535, fr_info_t *, fin);
1726 }
1727 }
1728 }
1729 fin->fin_off = off;
1730
1731 /*
1732 * Call per-protocol setup and checking
1733 */
1734 if (p == IPPROTO_AH) {
1735 /*
1736 * Treat AH differently because we expect there to be another
1737 * layer 4 header after it.
1738 */
1739 p = ipf_pr_ah(fin);
1740 }
1741
1742 switch (p)
1743 {
1744 case IPPROTO_UDP :
1745 ipf_pr_udp(fin);
1746 break;
1747 case IPPROTO_TCP :
1748 ipf_pr_tcp(fin);
1749 break;
1750 case IPPROTO_ICMP :
1751 ipf_pr_icmp(fin);
1752 break;
1753 case IPPROTO_ESP :
1754 ipf_pr_esp(fin);
1755 break;
1756 case IPPROTO_GRE :
1757 ipf_pr_gre(fin);
1758 break;
1759 }
1760
1761 ip = fin->fin_ip;
1762 if (ip == NULL)
1763 return;
1764
1765 /*
1766 * If it is a standard IP header (no options), set the flag fields
1767 * which relate to options to 0.
1768 */
1769 if (hlen == sizeof(*ip)) {
1770 fi->fi_optmsk = 0;
1771 fi->fi_secmsk = 0;
1772 fi->fi_auth = 0;
1773 return;
1774 }
1775
1776 /*
1777 * So the IP header has some IP options attached. Walk the entire
1778 * list of options present with this packet and set flags to indicate
1779 * which ones are here and which ones are not. For the somewhat out
1780 * of date and obscure security classification options, set a flag to
1781 * represent which classification is present.
1782 */
1783 fi->fi_flx |= FI_OPTIONS;
1784
1785 for (s = (u_char *)(ip + 1), hlen -= (int)sizeof(*ip); hlen > 0; ) {
1786 opt = *s;
1787 if (opt == '\0')
1788 break;
1789 else if (opt == IPOPT_NOP)
1790 ol = 1;
1791 else {
1792 if (hlen < 2)
1793 break;
1794 ol = (int)*(s + 1);
1795 if (ol < 2 || ol > hlen)
1796 break;
1797 }
1798 for (i = 9, mv = 4; mv >= 0; ) {
1799 op = ipopts + i;
1800
1801 if ((opt == (u_char)op->ol_val) && (ol > 4)) {
1802 u_32_t doi;
1803
1804 switch (opt)
1805 {
1806 case IPOPT_SECURITY :
1807 if (optmsk & op->ol_bit) {
1808 fin->fin_flx |= FI_BAD;
1809 DT2(ipf_fi_bad_ipopt_security, fr_info_t *, fin, u_short, (optmsk & op->ol_bit));
1810 } else {
1811 doi = ipf_checkripso(s);
1812 secmsk = doi >> 16;
1813 auth = doi & 0xffff;
1814 }
1815 break;
1816
1817 case IPOPT_CIPSO :
1818
1819 if (optmsk & op->ol_bit) {
1820 fin->fin_flx |= FI_BAD;
1821 DT2(ipf_fi_bad_ipopt_cipso, fr_info_t *, fin, u_short, (optmsk & op->ol_bit));
1822 } else {
1823 doi = ipf_checkcipso(fin,
1824 s, ol);
1825 secmsk = doi >> 16;
1826 auth = doi & 0xffff;
1827 }
1828 break;
1829 }
1830 optmsk |= op->ol_bit;
1831 }
1832
1833 if (opt < op->ol_val)
1834 i -= mv;
1835 else
1836 i += mv;
1837 mv--;
1838 }
1839 hlen -= ol;
1840 s += ol;
1841 }
1842
1843 /*
1844 *
1845 */
1846 if (auth && !(auth & 0x0100))
1847 auth &= 0xff00;
1848 fi->fi_optmsk = optmsk;
1849 fi->fi_secmsk = secmsk;
1850 fi->fi_auth = auth;
1851 }
1852
1853
1854 /* ------------------------------------------------------------------------ */
1855 /* Function: ipf_checkripso */
1856 /* Returns: void */
1857 /* Parameters: s(I) - pointer to start of RIPSO option */
1858 /* */
1859 /* ------------------------------------------------------------------------ */
1860 static u_32_t
ipf_checkripso(u_char * s)1861 ipf_checkripso(u_char *s)
1862 {
1863 const struct optlist *sp;
1864 u_short secmsk = 0, auth = 0;
1865 u_char sec;
1866 int j, m;
1867
1868 sec = *(s + 2); /* classification */
1869 for (j = 3, m = 2; m >= 0; ) {
1870 sp = secopt + j;
1871 if (sec == sp->ol_val) {
1872 secmsk |= sp->ol_bit;
1873 auth = *(s + 3);
1874 auth *= 256;
1875 auth += *(s + 4);
1876 break;
1877 }
1878 if (sec < sp->ol_val)
1879 j -= m;
1880 else
1881 j += m;
1882 m--;
1883 }
1884
1885 return (secmsk << 16) | auth;
1886 }
1887
1888
1889 /* ------------------------------------------------------------------------ */
1890 /* Function: ipf_checkcipso */
1891 /* Returns: u_32_t - 0 = failure, else the doi from the header */
1892 /* Parameters: fin(IO) - pointer to packet information */
1893 /* s(I) - pointer to start of CIPSO option */
1894 /* ol(I) - length of CIPSO option field */
1895 /* */
1896 /* This function returns the domain of integrity (DOI) field from the CIPSO */
1897 /* header and returns that whilst also storing the highest sensitivity */
1898 /* value found in the fr_info_t structure. */
1899 /* */
1900 /* No attempt is made to extract the category bitmaps as these are defined */
1901 /* by the user (rather than the protocol) and can be rather numerous on the */
1902 /* end nodes. */
1903 /* ------------------------------------------------------------------------ */
1904 static u_32_t
ipf_checkcipso(fr_info_t * fin,u_char * s,int ol)1905 ipf_checkcipso(fr_info_t *fin, u_char *s, int ol)
1906 {
1907 ipf_main_softc_t *softc = fin->fin_main_soft;
1908 fr_ip_t *fi;
1909 u_32_t doi;
1910 u_char *t, tag, tlen, sensitivity;
1911 int len;
1912
1913 if (ol < 6 || ol > 40) {
1914 LBUMPD(ipf_stats[fin->fin_out], fr_v4_cipso_bad);
1915 fin->fin_flx |= FI_BAD;
1916 DT2(ipf_fi_bad_checkcipso_ol, fr_info_t *, fin, u_int, ol);
1917 return (0);
1918 }
1919
1920 fi = &fin->fin_fi;
1921 fi->fi_sensitivity = 0;
1922 /*
1923 * The DOI field MUST be there.
1924 */
1925 bcopy(s + 2, &doi, sizeof(doi));
1926
1927 t = (u_char *)s + 6;
1928 for (len = ol - 6; len >= 2; len -= tlen, t+= tlen) {
1929 tag = *t;
1930 tlen = *(t + 1);
1931 if (tlen > len || tlen < 4 || tlen > 34) {
1932 LBUMPD(ipf_stats[fin->fin_out], fr_v4_cipso_tlen);
1933 fin->fin_flx |= FI_BAD;
1934 DT2(ipf_fi_bad_checkcipso_tlen, fr_info_t *, fin, u_int, tlen);
1935 return (0);
1936 }
1937
1938 sensitivity = 0;
1939 /*
1940 * Tag numbers 0, 1, 2, 5 are laid out in the CIPSO Internet
1941 * draft (16 July 1992) that has expired.
1942 */
1943 if (tag == 0) {
1944 fin->fin_flx |= FI_BAD;
1945 DT2(ipf_fi_bad_checkcipso_tag, fr_info_t *, fin, u_int, tag);
1946 continue;
1947 } else if (tag == 1) {
1948 if (*(t + 2) != 0) {
1949 fin->fin_flx |= FI_BAD;
1950 DT2(ipf_fi_bad_checkcipso_tag1_t2, fr_info_t *, fin, u_int, (*t + 2));
1951 continue;
1952 }
1953 sensitivity = *(t + 3);
1954 /* Category bitmap for categories 0-239 */
1955
1956 } else if (tag == 4) {
1957 if (*(t + 2) != 0) {
1958 fin->fin_flx |= FI_BAD;
1959 DT2(ipf_fi_bad_checkcipso_tag4_t2, fr_info_t *, fin, u_int, (*t + 2));
1960 continue;
1961 }
1962 sensitivity = *(t + 3);
1963 /* Enumerated categories, 16bits each, upto 15 */
1964
1965 } else if (tag == 5) {
1966 if (*(t + 2) != 0) {
1967 fin->fin_flx |= FI_BAD;
1968 DT2(ipf_fi_bad_checkcipso_tag5_t2, fr_info_t *, fin, u_int, (*t + 2));
1969 continue;
1970 }
1971 sensitivity = *(t + 3);
1972 /* Range of categories (2*16bits), up to 7 pairs */
1973
1974 } else if (tag > 127) {
1975 /* Custom defined DOI */
1976 ;
1977 } else {
1978 fin->fin_flx |= FI_BAD;
1979 DT2(ipf_fi_bad_checkcipso_tag127, fr_info_t *, fin, u_int, tag);
1980 continue;
1981 }
1982
1983 if (sensitivity > fi->fi_sensitivity)
1984 fi->fi_sensitivity = sensitivity;
1985 }
1986
1987 return (doi);
1988 }
1989
1990
1991 /* ------------------------------------------------------------------------ */
1992 /* Function: ipf_makefrip */
1993 /* Returns: int - 0 == packet ok, -1 == packet freed */
1994 /* Parameters: hlen(I) - length of IP packet header */
1995 /* ip(I) - pointer to the IP header */
1996 /* fin(IO) - pointer to packet information */
1997 /* */
1998 /* Compact the IP header into a structure which contains just the info. */
1999 /* which is useful for comparing IP headers with and store this information */
2000 /* in the fr_info_t structure pointer to by fin. At present, it is assumed */
2001 /* this function will be called with either an IPv4 or IPv6 packet. */
2002 /* ------------------------------------------------------------------------ */
2003 int
ipf_makefrip(int hlen,ip_t * ip,fr_info_t * fin)2004 ipf_makefrip(int hlen, ip_t *ip, fr_info_t *fin)
2005 {
2006 ipf_main_softc_t *softc = fin->fin_main_soft;
2007 int v;
2008
2009 fin->fin_depth = 0;
2010 fin->fin_hlen = (u_short)hlen;
2011 fin->fin_ip = ip;
2012 fin->fin_rule = 0xffffffff;
2013 fin->fin_group[0] = -1;
2014 fin->fin_group[1] = '\0';
2015 fin->fin_dp = (char *)ip + hlen;
2016
2017 v = fin->fin_v;
2018 if (v == 4) {
2019 fin->fin_plen = ntohs(ip->ip_len);
2020 fin->fin_dlen = fin->fin_plen - hlen;
2021 ipf_pr_ipv4hdr(fin);
2022 #ifdef USE_INET6
2023 } else if (v == 6) {
2024 fin->fin_plen = ntohs(((ip6_t *)ip)->ip6_plen);
2025 fin->fin_dlen = fin->fin_plen;
2026 fin->fin_plen += hlen;
2027
2028 ipf_pr_ipv6hdr(fin);
2029 #endif
2030 }
2031 if (fin->fin_ip == NULL) {
2032 LBUMP(ipf_stats[fin->fin_out].fr_ip_freed);
2033 return (-1);
2034 }
2035 return (0);
2036 }
2037
2038
2039 /* ------------------------------------------------------------------------ */
2040 /* Function: ipf_portcheck */
2041 /* Returns: int - 1 == port matched, 0 == port match failed */
2042 /* Parameters: frp(I) - pointer to port check `expression' */
2043 /* pop(I) - port number to evaluate */
2044 /* */
2045 /* Perform a comparison of a port number against some other(s), using a */
2046 /* structure with compare information stored in it. */
2047 /* ------------------------------------------------------------------------ */
2048 static inline int
ipf_portcheck(frpcmp_t * frp,u_32_t pop)2049 ipf_portcheck(frpcmp_t *frp, u_32_t pop)
2050 {
2051 int err = 1;
2052 u_32_t po;
2053
2054 po = frp->frp_port;
2055
2056 /*
2057 * Do opposite test to that required and continue if that succeeds.
2058 */
2059 switch (frp->frp_cmp)
2060 {
2061 case FR_EQUAL :
2062 if (pop != po) /* EQUAL */
2063 err = 0;
2064 break;
2065 case FR_NEQUAL :
2066 if (pop == po) /* NOTEQUAL */
2067 err = 0;
2068 break;
2069 case FR_LESST :
2070 if (pop >= po) /* LESSTHAN */
2071 err = 0;
2072 break;
2073 case FR_GREATERT :
2074 if (pop <= po) /* GREATERTHAN */
2075 err = 0;
2076 break;
2077 case FR_LESSTE :
2078 if (pop > po) /* LT or EQ */
2079 err = 0;
2080 break;
2081 case FR_GREATERTE :
2082 if (pop < po) /* GT or EQ */
2083 err = 0;
2084 break;
2085 case FR_OUTRANGE :
2086 if (pop >= po && pop <= frp->frp_top) /* Out of range */
2087 err = 0;
2088 break;
2089 case FR_INRANGE :
2090 if (pop <= po || pop >= frp->frp_top) /* In range */
2091 err = 0;
2092 break;
2093 case FR_INCRANGE :
2094 if (pop < po || pop > frp->frp_top) /* Inclusive range */
2095 err = 0;
2096 break;
2097 default :
2098 break;
2099 }
2100 return (err);
2101 }
2102
2103
2104 /* ------------------------------------------------------------------------ */
2105 /* Function: ipf_tcpudpchk */
2106 /* Returns: int - 1 == protocol matched, 0 == check failed */
2107 /* Parameters: fda(I) - pointer to packet information */
2108 /* ft(I) - pointer to structure with comparison data */
2109 /* */
2110 /* Compares the current pcket (assuming it is TCP/UDP) information with a */
2111 /* structure containing information that we want to match against. */
2112 /* ------------------------------------------------------------------------ */
2113 int
ipf_tcpudpchk(fr_ip_t * fi,frtuc_t * ft)2114 ipf_tcpudpchk(fr_ip_t *fi, frtuc_t *ft)
2115 {
2116 int err = 1;
2117
2118 /*
2119 * Both ports should *always* be in the first fragment.
2120 * So far, I cannot find any cases where they can not be.
2121 *
2122 * compare destination ports
2123 */
2124 if (ft->ftu_dcmp)
2125 err = ipf_portcheck(&ft->ftu_dst, fi->fi_ports[1]);
2126
2127 /*
2128 * compare source ports
2129 */
2130 if (err && ft->ftu_scmp)
2131 err = ipf_portcheck(&ft->ftu_src, fi->fi_ports[0]);
2132
2133 /*
2134 * If we don't have all the TCP/UDP header, then how can we
2135 * expect to do any sort of match on it ? If we were looking for
2136 * TCP flags, then NO match. If not, then match (which should
2137 * satisfy the "short" class too).
2138 */
2139 if (err && (fi->fi_p == IPPROTO_TCP)) {
2140 if (fi->fi_flx & FI_SHORT)
2141 return (!(ft->ftu_tcpf | ft->ftu_tcpfm));
2142 /*
2143 * Match the flags ? If not, abort this match.
2144 */
2145 if (ft->ftu_tcpfm &&
2146 ft->ftu_tcpf != (fi->fi_tcpf & ft->ftu_tcpfm)) {
2147 FR_DEBUG(("f. %#x & %#x != %#x\n", fi->fi_tcpf,
2148 ft->ftu_tcpfm, ft->ftu_tcpf));
2149 err = 0;
2150 }
2151 }
2152 return (err);
2153 }
2154
2155
2156 /* ------------------------------------------------------------------------ */
2157 /* Function: ipf_check_ipf */
2158 /* Returns: int - 0 == match, else no match */
2159 /* Parameters: fin(I) - pointer to packet information */
2160 /* fr(I) - pointer to filter rule */
2161 /* portcmp(I) - flag indicating whether to attempt matching on */
2162 /* TCP/UDP port data. */
2163 /* */
2164 /* Check to see if a packet matches an IPFilter rule. Checks of addresses, */
2165 /* port numbers, etc, for "standard" IPFilter rules are all orchestrated in */
2166 /* this function. */
2167 /* ------------------------------------------------------------------------ */
2168 static inline int
ipf_check_ipf(fr_info_t * fin,frentry_t * fr,int portcmp)2169 ipf_check_ipf(fr_info_t *fin, frentry_t *fr, int portcmp)
2170 {
2171 u_32_t *ld, *lm, *lip;
2172 fripf_t *fri;
2173 fr_ip_t *fi;
2174 int i;
2175
2176 fi = &fin->fin_fi;
2177 fri = fr->fr_ipf;
2178 lip = (u_32_t *)fi;
2179 lm = (u_32_t *)&fri->fri_mip;
2180 ld = (u_32_t *)&fri->fri_ip;
2181
2182 /*
2183 * first 32 bits to check coversion:
2184 * IP version, TOS, TTL, protocol
2185 */
2186 i = ((*lip & *lm) != *ld);
2187 FR_DEBUG(("0. %#08x & %#08x != %#08x\n",
2188 ntohl(*lip), ntohl(*lm), ntohl(*ld)));
2189 if (i)
2190 return (1);
2191
2192 /*
2193 * Next 32 bits is a constructed bitmask indicating which IP options
2194 * are present (if any) in this packet.
2195 */
2196 lip++, lm++, ld++;
2197 i = ((*lip & *lm) != *ld);
2198 FR_DEBUG(("1. %#08x & %#08x != %#08x\n",
2199 ntohl(*lip), ntohl(*lm), ntohl(*ld)));
2200 if (i != 0)
2201 return (1);
2202
2203 lip++, lm++, ld++;
2204 /*
2205 * Unrolled loops (4 each, for 32 bits) for address checks.
2206 */
2207 /*
2208 * Check the source address.
2209 */
2210 if (fr->fr_satype == FRI_LOOKUP) {
2211 i = (*fr->fr_srcfunc)(fin->fin_main_soft, fr->fr_srcptr,
2212 fi->fi_v, lip, fin->fin_plen);
2213 if (i == -1)
2214 return (1);
2215 lip += 3;
2216 lm += 3;
2217 ld += 3;
2218 } else {
2219 i = ((*lip & *lm) != *ld);
2220 FR_DEBUG(("2a. %#08x & %#08x != %#08x\n",
2221 ntohl(*lip), ntohl(*lm), ntohl(*ld)));
2222 if (fi->fi_v == 6) {
2223 lip++, lm++, ld++;
2224 i |= ((*lip & *lm) != *ld);
2225 FR_DEBUG(("2b. %#08x & %#08x != %#08x\n",
2226 ntohl(*lip), ntohl(*lm), ntohl(*ld)));
2227 lip++, lm++, ld++;
2228 i |= ((*lip & *lm) != *ld);
2229 FR_DEBUG(("2c. %#08x & %#08x != %#08x\n",
2230 ntohl(*lip), ntohl(*lm), ntohl(*ld)));
2231 lip++, lm++, ld++;
2232 i |= ((*lip & *lm) != *ld);
2233 FR_DEBUG(("2d. %#08x & %#08x != %#08x\n",
2234 ntohl(*lip), ntohl(*lm), ntohl(*ld)));
2235 } else {
2236 lip += 3;
2237 lm += 3;
2238 ld += 3;
2239 }
2240 }
2241 i ^= (fr->fr_flags & FR_NOTSRCIP) >> 6;
2242 if (i != 0)
2243 return (1);
2244
2245 /*
2246 * Check the destination address.
2247 */
2248 lip++, lm++, ld++;
2249 if (fr->fr_datype == FRI_LOOKUP) {
2250 i = (*fr->fr_dstfunc)(fin->fin_main_soft, fr->fr_dstptr,
2251 fi->fi_v, lip, fin->fin_plen);
2252 if (i == -1)
2253 return (1);
2254 lip += 3;
2255 lm += 3;
2256 ld += 3;
2257 } else {
2258 i = ((*lip & *lm) != *ld);
2259 FR_DEBUG(("3a. %#08x & %#08x != %#08x\n",
2260 ntohl(*lip), ntohl(*lm), ntohl(*ld)));
2261 if (fi->fi_v == 6) {
2262 lip++, lm++, ld++;
2263 i |= ((*lip & *lm) != *ld);
2264 FR_DEBUG(("3b. %#08x & %#08x != %#08x\n",
2265 ntohl(*lip), ntohl(*lm), ntohl(*ld)));
2266 lip++, lm++, ld++;
2267 i |= ((*lip & *lm) != *ld);
2268 FR_DEBUG(("3c. %#08x & %#08x != %#08x\n",
2269 ntohl(*lip), ntohl(*lm), ntohl(*ld)));
2270 lip++, lm++, ld++;
2271 i |= ((*lip & *lm) != *ld);
2272 FR_DEBUG(("3d. %#08x & %#08x != %#08x\n",
2273 ntohl(*lip), ntohl(*lm), ntohl(*ld)));
2274 } else {
2275 lip += 3;
2276 lm += 3;
2277 ld += 3;
2278 }
2279 }
2280 i ^= (fr->fr_flags & FR_NOTDSTIP) >> 7;
2281 if (i != 0)
2282 return (1);
2283 /*
2284 * IP addresses matched. The next 32bits contains:
2285 * mast of old IP header security & authentication bits.
2286 */
2287 lip++, lm++, ld++;
2288 i = (*ld - (*lip & *lm));
2289 FR_DEBUG(("4. %#08x & %#08x != %#08x\n", *lip, *lm, *ld));
2290
2291 /*
2292 * Next we have 32 bits of packet flags.
2293 */
2294 lip++, lm++, ld++;
2295 i |= (*ld - (*lip & *lm));
2296 FR_DEBUG(("5. %#08x & %#08x != %#08x\n", *lip, *lm, *ld));
2297
2298 if (i == 0) {
2299 /*
2300 * If a fragment, then only the first has what we're
2301 * looking for here...
2302 */
2303 if (portcmp) {
2304 if (!ipf_tcpudpchk(&fin->fin_fi, &fr->fr_tuc))
2305 i = 1;
2306 } else {
2307 if (fr->fr_dcmp || fr->fr_scmp ||
2308 fr->fr_tcpf || fr->fr_tcpfm)
2309 i = 1;
2310 if (fr->fr_icmpm || fr->fr_icmp) {
2311 if (((fi->fi_p != IPPROTO_ICMP) &&
2312 (fi->fi_p != IPPROTO_ICMPV6)) ||
2313 fin->fin_off || (fin->fin_dlen < 2))
2314 i = 1;
2315 else if ((fin->fin_data[0] & fr->fr_icmpm) !=
2316 fr->fr_icmp) {
2317 FR_DEBUG(("i. %#x & %#x != %#x\n",
2318 fin->fin_data[0],
2319 fr->fr_icmpm, fr->fr_icmp));
2320 i = 1;
2321 }
2322 }
2323 }
2324 }
2325 return (i);
2326 }
2327
2328
2329 /* ------------------------------------------------------------------------ */
2330 /* Function: ipf_scanlist */
2331 /* Returns: int - result flags of scanning filter list */
2332 /* Parameters: fin(I) - pointer to packet information */
2333 /* pass(I) - default result to return for filtering */
2334 /* */
2335 /* Check the input/output list of rules for a match to the current packet. */
2336 /* If a match is found, the value of fr_flags from the rule becomes the */
2337 /* return value and fin->fin_fr points to the matched rule. */
2338 /* */
2339 /* This function may be called recursively upto 16 times (limit inbuilt.) */
2340 /* When unwinding, it should finish up with fin_depth as 0. */
2341 /* */
2342 /* Could be per interface, but this gets real nasty when you don't have, */
2343 /* or can't easily change, the kernel source code to . */
2344 /* ------------------------------------------------------------------------ */
2345 int
ipf_scanlist(fr_info_t * fin,u_32_t pass)2346 ipf_scanlist(fr_info_t *fin, u_32_t pass)
2347 {
2348 ipf_main_softc_t *softc = fin->fin_main_soft;
2349 int rulen, portcmp, off, skip;
2350 struct frentry *fr, *fnext;
2351 u_32_t passt, passo;
2352
2353 /*
2354 * Do not allow nesting deeper than 16 levels.
2355 */
2356 if (fin->fin_depth >= 16)
2357 return (pass);
2358
2359 fr = fin->fin_fr;
2360
2361 /*
2362 * If there are no rules in this list, return now.
2363 */
2364 if (fr == NULL)
2365 return (pass);
2366
2367 skip = 0;
2368 portcmp = 0;
2369 fin->fin_depth++;
2370 fin->fin_fr = NULL;
2371 off = fin->fin_off;
2372
2373 if ((fin->fin_flx & FI_TCPUDP) && (fin->fin_dlen > 3) && !off)
2374 portcmp = 1;
2375
2376 for (rulen = 0; fr; fr = fnext, rulen++) {
2377 fnext = fr->fr_next;
2378 if (skip != 0) {
2379 FR_VERBOSE(("SKIP %d (%#x)\n", skip, fr->fr_flags));
2380 skip--;
2381 continue;
2382 }
2383
2384 /*
2385 * In all checks below, a null (zero) value in the
2386 * filter struture is taken to mean a wildcard.
2387 *
2388 * check that we are working for the right interface
2389 */
2390 #ifdef _KERNEL
2391 if (fr->fr_ifa && fr->fr_ifa != fin->fin_ifp)
2392 continue;
2393 #else
2394 if (opts & (OPT_VERBOSE|OPT_DEBUG))
2395 printf("\n");
2396 FR_VERBOSE(("%c", FR_ISSKIP(pass) ? 's' :
2397 FR_ISPASS(pass) ? 'p' :
2398 FR_ISACCOUNT(pass) ? 'A' :
2399 FR_ISAUTH(pass) ? 'a' :
2400 (pass & FR_NOMATCH) ? 'n' :'b'));
2401 if (fr->fr_ifa && fr->fr_ifa != fin->fin_ifp)
2402 continue;
2403 FR_VERBOSE((":i"));
2404 #endif
2405
2406 switch (fr->fr_type)
2407 {
2408 case FR_T_IPF :
2409 case FR_T_IPF_BUILTIN :
2410 if (ipf_check_ipf(fin, fr, portcmp))
2411 continue;
2412 break;
2413 #if defined(IPFILTER_BPF)
2414 case FR_T_BPFOPC :
2415 case FR_T_BPFOPC_BUILTIN :
2416 {
2417 u_char *mc;
2418 int wlen;
2419
2420 if (*fin->fin_mp == NULL)
2421 continue;
2422 if (fin->fin_family != fr->fr_family)
2423 continue;
2424 mc = (u_char *)fin->fin_m;
2425 wlen = fin->fin_dlen + fin->fin_hlen;
2426 if (!bpf_filter(fr->fr_data, mc, wlen, 0))
2427 continue;
2428 break;
2429 }
2430 #endif
2431 case FR_T_CALLFUNC_BUILTIN :
2432 {
2433 frentry_t *f;
2434
2435 f = (*fr->fr_func)(fin, &pass);
2436 if (f != NULL)
2437 fr = f;
2438 else
2439 continue;
2440 break;
2441 }
2442
2443 case FR_T_IPFEXPR :
2444 case FR_T_IPFEXPR_BUILTIN :
2445 if (fin->fin_family != fr->fr_family)
2446 continue;
2447 if (ipf_fr_matcharray(fin, fr->fr_data) == 0)
2448 continue;
2449 break;
2450
2451 default :
2452 break;
2453 }
2454
2455 if ((fin->fin_out == 0) && (fr->fr_nattag.ipt_num[0] != 0)) {
2456 if (fin->fin_nattag == NULL)
2457 continue;
2458 if (ipf_matchtag(&fr->fr_nattag, fin->fin_nattag) == 0)
2459 continue;
2460 }
2461 FR_VERBOSE(("=%d/%d.%d *", fr->fr_grhead, fr->fr_group, rulen));
2462
2463 passt = fr->fr_flags;
2464
2465 /*
2466 * If the rule is a "call now" rule, then call the function
2467 * in the rule, if it exists and use the results from that.
2468 * If the function pointer is bad, just make like we ignore
2469 * it, except for increasing the hit counter.
2470 */
2471 if ((passt & FR_CALLNOW) != 0) {
2472 frentry_t *frs;
2473
2474 ATOMIC_INC64(fr->fr_hits);
2475 if ((fr->fr_func == NULL) ||
2476 (fr->fr_func == (ipfunc_t)-1))
2477 continue;
2478
2479 frs = fin->fin_fr;
2480 fin->fin_fr = fr;
2481 fr = (*fr->fr_func)(fin, &passt);
2482 if (fr == NULL) {
2483 fin->fin_fr = frs;
2484 continue;
2485 }
2486 passt = fr->fr_flags;
2487 }
2488 fin->fin_fr = fr;
2489
2490 #ifdef IPFILTER_LOG
2491 /*
2492 * Just log this packet...
2493 */
2494 if ((passt & FR_LOGMASK) == FR_LOG) {
2495 if (ipf_log_pkt(fin, passt) == -1) {
2496 if (passt & FR_LOGORBLOCK) {
2497 DT(frb_logfail);
2498 passt &= ~FR_CMDMASK;
2499 passt |= FR_BLOCK|FR_QUICK;
2500 fin->fin_reason = FRB_LOGFAIL;
2501 }
2502 }
2503 }
2504 #endif /* IPFILTER_LOG */
2505
2506 MUTEX_ENTER(&fr->fr_lock);
2507 fr->fr_bytes += (U_QUAD_T)fin->fin_plen;
2508 fr->fr_hits++;
2509 MUTEX_EXIT(&fr->fr_lock);
2510 fin->fin_rule = rulen;
2511
2512 passo = pass;
2513 if (FR_ISSKIP(passt)) {
2514 skip = fr->fr_arg;
2515 continue;
2516 } else if (((passt & FR_LOGMASK) != FR_LOG) &&
2517 ((passt & FR_LOGMASK) != FR_DECAPSULATE)) {
2518 pass = passt;
2519 }
2520
2521 if (passt & (FR_RETICMP|FR_FAKEICMP))
2522 fin->fin_icode = fr->fr_icode;
2523
2524 if (fr->fr_group != -1) {
2525 (void) strncpy(fin->fin_group,
2526 FR_NAME(fr, fr_group),
2527 strlen(FR_NAME(fr, fr_group)));
2528 } else {
2529 fin->fin_group[0] = '\0';
2530 }
2531
2532 FR_DEBUG(("pass %#x/%#x/%x\n", passo, pass, passt));
2533
2534 if (fr->fr_grphead != NULL) {
2535 fin->fin_fr = fr->fr_grphead->fg_start;
2536 FR_VERBOSE(("group %s\n", FR_NAME(fr, fr_grhead)));
2537
2538 if (FR_ISDECAPS(passt))
2539 passt = ipf_decaps(fin, pass, fr->fr_icode);
2540 else
2541 passt = ipf_scanlist(fin, pass);
2542
2543 if (fin->fin_fr == NULL) {
2544 fin->fin_rule = rulen;
2545 if (fr->fr_group != -1)
2546 (void) strncpy(fin->fin_group,
2547 fr->fr_names +
2548 fr->fr_group,
2549 strlen(fr->fr_names +
2550 fr->fr_group));
2551 fin->fin_fr = fr;
2552 passt = pass;
2553 }
2554 pass = passt;
2555 }
2556
2557 if (pass & FR_QUICK) {
2558 /*
2559 * Finally, if we've asked to track state for this
2560 * packet, set it up. Add state for "quick" rules
2561 * here so that if the action fails we can consider
2562 * the rule to "not match" and keep on processing
2563 * filter rules.
2564 */
2565 if ((pass & FR_KEEPSTATE) && !FR_ISAUTH(pass) &&
2566 !(fin->fin_flx & FI_STATE)) {
2567 int out = fin->fin_out;
2568
2569 fin->fin_fr = fr;
2570 if (ipf_state_add(softc, fin, NULL, 0) == 0) {
2571 LBUMPD(ipf_stats[out], fr_ads);
2572 } else {
2573 LBUMPD(ipf_stats[out], fr_bads);
2574 pass = passo;
2575 continue;
2576 }
2577 }
2578 break;
2579 }
2580 }
2581 fin->fin_depth--;
2582 return (pass);
2583 }
2584
2585
2586 /* ------------------------------------------------------------------------ */
2587 /* Function: ipf_acctpkt */
2588 /* Returns: frentry_t* - always returns NULL */
2589 /* Parameters: fin(I) - pointer to packet information */
2590 /* passp(IO) - pointer to current/new filter decision (unused) */
2591 /* */
2592 /* Checks a packet against accounting rules, if there are any for the given */
2593 /* IP protocol version. */
2594 /* */
2595 /* N.B.: this function returns NULL to match the prototype used by other */
2596 /* functions called from the IPFilter "mainline" in ipf_check(). */
2597 /* ------------------------------------------------------------------------ */
2598 frentry_t *
ipf_acctpkt(fr_info_t * fin,u_32_t * passp)2599 ipf_acctpkt(fr_info_t *fin, u_32_t *passp)
2600 {
2601 ipf_main_softc_t *softc = fin->fin_main_soft;
2602 char group[FR_GROUPLEN];
2603 frentry_t *fr, *frsave;
2604 u_32_t pass, rulen;
2605
2606 passp = passp;
2607 fr = softc->ipf_acct[fin->fin_out][softc->ipf_active];
2608
2609 if (fr != NULL) {
2610 frsave = fin->fin_fr;
2611 bcopy(fin->fin_group, group, FR_GROUPLEN);
2612 rulen = fin->fin_rule;
2613 fin->fin_fr = fr;
2614 pass = ipf_scanlist(fin, FR_NOMATCH);
2615 if (FR_ISACCOUNT(pass)) {
2616 LBUMPD(ipf_stats[0], fr_acct);
2617 }
2618 fin->fin_fr = frsave;
2619 bcopy(group, fin->fin_group, FR_GROUPLEN);
2620 fin->fin_rule = rulen;
2621 }
2622 return (NULL);
2623 }
2624
2625
2626 /* ------------------------------------------------------------------------ */
2627 /* Function: ipf_firewall */
2628 /* Returns: frentry_t* - returns pointer to matched rule, if no matches */
2629 /* were found, returns NULL. */
2630 /* Parameters: fin(I) - pointer to packet information */
2631 /* passp(IO) - pointer to current/new filter decision (unused) */
2632 /* */
2633 /* Applies an appropriate set of firewall rules to the packet, to see if */
2634 /* there are any matches. The first check is to see if a match can be seen */
2635 /* in the cache. If not, then search an appropriate list of rules. Once a */
2636 /* matching rule is found, take any appropriate actions as defined by the */
2637 /* rule - except logging. */
2638 /* ------------------------------------------------------------------------ */
2639 static frentry_t *
ipf_firewall(fr_info_t * fin,u_32_t * passp)2640 ipf_firewall(fr_info_t *fin, u_32_t *passp)
2641 {
2642 ipf_main_softc_t *softc = fin->fin_main_soft;
2643 frentry_t *fr;
2644 u_32_t pass;
2645 int out;
2646
2647 out = fin->fin_out;
2648 pass = *passp;
2649
2650 /*
2651 * This rule cache will only affect packets that are not being
2652 * statefully filtered.
2653 */
2654 fin->fin_fr = softc->ipf_rules[out][softc->ipf_active];
2655 if (fin->fin_fr != NULL)
2656 pass = ipf_scanlist(fin, softc->ipf_pass);
2657
2658 if ((pass & FR_NOMATCH)) {
2659 LBUMPD(ipf_stats[out], fr_nom);
2660 }
2661 fr = fin->fin_fr;
2662
2663 /*
2664 * Apply packets per second rate-limiting to a rule as required.
2665 */
2666 if ((fr != NULL) && (fr->fr_pps != 0) &&
2667 !ppsratecheck(&fr->fr_lastpkt, &fr->fr_curpps, fr->fr_pps)) {
2668 DT2(frb_ppsrate, fr_info_t *, fin, frentry_t *, fr);
2669 pass &= ~(FR_CMDMASK|FR_RETICMP|FR_RETRST);
2670 pass |= FR_BLOCK;
2671 LBUMPD(ipf_stats[out], fr_ppshit);
2672 fin->fin_reason = FRB_PPSRATE;
2673 }
2674
2675 /*
2676 * If we fail to add a packet to the authorization queue, then we
2677 * drop the packet later. However, if it was added then pretend
2678 * we've dropped it already.
2679 */
2680 if (FR_ISAUTH(pass)) {
2681 if (ipf_auth_new(fin->fin_m, fin) != 0) {
2682 DT1(frb_authnew, fr_info_t *, fin);
2683 fin->fin_m = *fin->fin_mp = NULL;
2684 fin->fin_reason = FRB_AUTHNEW;
2685 fin->fin_error = 0;
2686 } else {
2687 IPFERROR(1);
2688 fin->fin_error = ENOSPC;
2689 }
2690 }
2691
2692 if ((fr != NULL) && (fr->fr_func != NULL) &&
2693 (fr->fr_func != (ipfunc_t)-1) && !(pass & FR_CALLNOW))
2694 (void) (*fr->fr_func)(fin, &pass);
2695
2696 /*
2697 * If a rule is a pre-auth rule, check again in the list of rules
2698 * loaded for authenticated use. It does not particulary matter
2699 * if this search fails because a "preauth" result, from a rule,
2700 * is treated as "not a pass", hence the packet is blocked.
2701 */
2702 if (FR_ISPREAUTH(pass)) {
2703 pass = ipf_auth_pre_scanlist(softc, fin, pass);
2704 }
2705
2706 /*
2707 * If the rule has "keep frag" and the packet is actually a fragment,
2708 * then create a fragment state entry.
2709 */
2710 if (pass & FR_KEEPFRAG) {
2711 if (fin->fin_flx & FI_FRAG) {
2712 if (ipf_frag_new(softc, fin, pass) == -1) {
2713 LBUMP(ipf_stats[out].fr_bnfr);
2714 } else {
2715 LBUMP(ipf_stats[out].fr_nfr);
2716 }
2717 } else {
2718 LBUMP(ipf_stats[out].fr_cfr);
2719 }
2720 }
2721
2722 fr = fin->fin_fr;
2723 *passp = pass;
2724
2725 return (fr);
2726 }
2727
2728
2729 /* ------------------------------------------------------------------------ */
2730 /* Function: ipf_check */
2731 /* Returns: int - 0 == packet allowed through, */
2732 /* User space: */
2733 /* -1 == packet blocked */
2734 /* 1 == packet not matched */
2735 /* -2 == requires authentication */
2736 /* Kernel: */
2737 /* > 0 == filter error # for packet */
2738 /* Parameters: ctx(I) - pointer to the instance context */
2739 /* ip(I) - pointer to start of IPv4/6 packet */
2740 /* hlen(I) - length of header */
2741 /* ifp(I) - pointer to interface this packet is on */
2742 /* out(I) - 0 == packet going in, 1 == packet going out */
2743 /* mp(IO) - pointer to caller's buffer pointer that holds this */
2744 /* IP packet. */
2745 /* Solaris: */
2746 /* qpi(I) - pointer to STREAMS queue information for this */
2747 /* interface & direction. */
2748 /* */
2749 /* ipf_check() is the master function for all IPFilter packet processing. */
2750 /* It orchestrates: Network Address Translation (NAT), checking for packet */
2751 /* authorisation (or pre-authorisation), presence of related state info., */
2752 /* generating log entries, IP packet accounting, routing of packets as */
2753 /* directed by firewall rules and of course whether or not to allow the */
2754 /* packet to be further processed by the kernel. */
2755 /* */
2756 /* For packets blocked, the contents of "mp" will be NULL'd and the buffer */
2757 /* freed. Packets passed may be returned with the pointer pointed to by */
2758 /* by "mp" changed to a new buffer. */
2759 /* ------------------------------------------------------------------------ */
2760 int
ipf_check(void * ctx,ip_t * ip,int hlen,struct ifnet * ifp,int out,void * qif,mb_t ** mp)2761 ipf_check(void *ctx, ip_t *ip, int hlen, struct ifnet *ifp, int out
2762 #if defined(_KERNEL) && SOLARIS
2763 , void* qif, mb_t **mp)
2764 #else
2765 , mb_t **mp)
2766 #endif
2767 {
2768 /*
2769 * The above really sucks, but short of writing a diff
2770 */
2771 ipf_main_softc_t *softc = ctx;
2772 fr_info_t frinfo;
2773 fr_info_t *fin = &frinfo;
2774 u_32_t pass = softc->ipf_pass;
2775 frentry_t *fr = NULL;
2776 int v = IP_V(ip);
2777 mb_t *mc = NULL;
2778 mb_t *m;
2779 /*
2780 * The first part of ipf_check() deals with making sure that what goes
2781 * into the filtering engine makes some sense. Information about the
2782 * the packet is distilled, collected into a fr_info_t structure and
2783 * the an attempt to ensure the buffer the packet is in is big enough
2784 * to hold all the required packet headers.
2785 */
2786 #ifdef _KERNEL
2787 # if SOLARIS
2788 qpktinfo_t *qpi = qif;
2789
2790 # ifdef __sparc
2791 if ((u_int)ip & 0x3)
2792 return (2);
2793 # endif
2794 # else
2795 SPL_INT(s);
2796 # endif
2797
2798 if (softc->ipf_running <= 0) {
2799 return (0);
2800 }
2801
2802 bzero((char *)fin, sizeof(*fin));
2803
2804 # if SOLARIS
2805 if (qpi->qpi_flags & QF_BROADCAST)
2806 fin->fin_flx |= FI_MBCAST|FI_BROADCAST;
2807 if (qpi->qpi_flags & QF_MULTICAST)
2808 fin->fin_flx |= FI_MBCAST|FI_MULTICAST;
2809 m = qpi->qpi_m;
2810 fin->fin_qfm = m;
2811 fin->fin_qpi = qpi;
2812 # else /* SOLARIS */
2813
2814 m = *mp;
2815
2816 # if defined(M_MCAST)
2817 if ((m->m_flags & M_MCAST) != 0)
2818 fin->fin_flx |= FI_MBCAST|FI_MULTICAST;
2819 # endif
2820 # if defined(M_MLOOP)
2821 if ((m->m_flags & M_MLOOP) != 0)
2822 fin->fin_flx |= FI_MBCAST|FI_MULTICAST;
2823 # endif
2824 # if defined(M_BCAST)
2825 if ((m->m_flags & M_BCAST) != 0)
2826 fin->fin_flx |= FI_MBCAST|FI_BROADCAST;
2827 # endif
2828 # ifdef M_CANFASTFWD
2829 /*
2830 * XXX For now, IP Filter and fast-forwarding of cached flows
2831 * XXX are mutually exclusive. Eventually, IP Filter should
2832 * XXX get a "can-fast-forward" filter rule.
2833 */
2834 m->m_flags &= ~M_CANFASTFWD;
2835 # endif /* M_CANFASTFWD */
2836 # if defined(CSUM_DELAY_DATA) && !defined(__FreeBSD__)
2837 /*
2838 * disable delayed checksums.
2839 */
2840 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
2841 in_delayed_cksum(m);
2842 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
2843 }
2844 # endif /* CSUM_DELAY_DATA */
2845 # endif /* SOLARIS */
2846 #else
2847 bzero((char *)fin, sizeof(*fin));
2848 m = *mp;
2849 # if defined(M_MCAST)
2850 if ((m->m_flags & M_MCAST) != 0)
2851 fin->fin_flx |= FI_MBCAST|FI_MULTICAST;
2852 # endif
2853 # if defined(M_MLOOP)
2854 if ((m->m_flags & M_MLOOP) != 0)
2855 fin->fin_flx |= FI_MBCAST|FI_MULTICAST;
2856 # endif
2857 # if defined(M_BCAST)
2858 if ((m->m_flags & M_BCAST) != 0)
2859 fin->fin_flx |= FI_MBCAST|FI_BROADCAST;
2860 # endif
2861 #endif /* _KERNEL */
2862
2863 fin->fin_v = v;
2864 fin->fin_m = m;
2865 fin->fin_ip = ip;
2866 fin->fin_mp = mp;
2867 fin->fin_out = out;
2868 fin->fin_ifp = ifp;
2869 fin->fin_error = ENETUNREACH;
2870 fin->fin_hlen = (u_short)hlen;
2871 fin->fin_dp = (char *)ip + hlen;
2872 fin->fin_main_soft = softc;
2873
2874 fin->fin_ipoff = (char *)ip - MTOD(m, char *);
2875
2876 SPL_NET(s);
2877
2878 #ifdef USE_INET6
2879 if (v == 6) {
2880 LBUMP(ipf_stats[out].fr_ipv6);
2881 /*
2882 * Jumbo grams are quite likely too big for internal buffer
2883 * structures to handle comfortably, for now, so just drop
2884 * them.
2885 */
2886 if (((ip6_t *)ip)->ip6_plen == 0) {
2887 DT1(frb_jumbo, ip6_t *, (ip6_t *)ip);
2888 pass = FR_BLOCK|FR_NOMATCH;
2889 fin->fin_reason = FRB_JUMBO;
2890 goto finished;
2891 }
2892 fin->fin_family = AF_INET6;
2893 } else
2894 #endif
2895 {
2896 fin->fin_family = AF_INET;
2897 }
2898
2899 if (ipf_makefrip(hlen, ip, fin) == -1) {
2900 DT1(frb_makefrip, fr_info_t *, fin);
2901 pass = FR_BLOCK|FR_NOMATCH;
2902 fin->fin_reason = FRB_MAKEFRIP;
2903 goto finished;
2904 }
2905
2906 /*
2907 * For at least IPv6 packets, if a m_pullup() fails then this pointer
2908 * becomes NULL and so we have no packet to free.
2909 */
2910 if (*fin->fin_mp == NULL)
2911 goto finished;
2912
2913 if (!out) {
2914 if (v == 4) {
2915 if (softc->ipf_chksrc && !ipf_verifysrc(fin)) {
2916 LBUMPD(ipf_stats[0], fr_v4_badsrc);
2917 fin->fin_flx |= FI_BADSRC;
2918 }
2919 if (fin->fin_ip->ip_ttl < softc->ipf_minttl) {
2920 LBUMPD(ipf_stats[0], fr_v4_badttl);
2921 fin->fin_flx |= FI_LOWTTL;
2922 }
2923 }
2924 #ifdef USE_INET6
2925 else if (v == 6) {
2926 if (((ip6_t *)ip)->ip6_hlim < softc->ipf_minttl) {
2927 LBUMPD(ipf_stats[0], fr_v6_badttl);
2928 fin->fin_flx |= FI_LOWTTL;
2929 }
2930 }
2931 #endif
2932 }
2933
2934 if (fin->fin_flx & FI_SHORT) {
2935 LBUMPD(ipf_stats[out], fr_short);
2936 }
2937
2938 READ_ENTER(&softc->ipf_mutex);
2939
2940 if (!out) {
2941 switch (fin->fin_v)
2942 {
2943 case 4 :
2944 if (ipf_nat_checkin(fin, &pass) == -1) {
2945 goto filterdone;
2946 }
2947 break;
2948 #ifdef USE_INET6
2949 case 6 :
2950 if (ipf_nat6_checkin(fin, &pass) == -1) {
2951 goto filterdone;
2952 }
2953 break;
2954 #endif
2955 default :
2956 break;
2957 }
2958 }
2959 /*
2960 * Check auth now.
2961 * If a packet is found in the auth table, then skip checking
2962 * the access lists for permission but we do need to consider
2963 * the result as if it were from the ACL's. In addition, being
2964 * found in the auth table means it has been seen before, so do
2965 * not pass it through accounting (again), lest it be counted twice.
2966 */
2967 fr = ipf_auth_check(fin, &pass);
2968 if (!out && (fr == NULL))
2969 (void) ipf_acctpkt(fin, NULL);
2970
2971 if (fr == NULL) {
2972 if ((fin->fin_flx & FI_FRAG) != 0)
2973 fr = ipf_frag_known(fin, &pass);
2974
2975 if (fr == NULL)
2976 fr = ipf_state_check(fin, &pass);
2977 }
2978
2979 if ((pass & FR_NOMATCH) || (fr == NULL))
2980 fr = ipf_firewall(fin, &pass);
2981
2982 /*
2983 * If we've asked to track state for this packet, set it up.
2984 * Here rather than ipf_firewall because ipf_checkauth may decide
2985 * to return a packet for "keep state"
2986 */
2987 if ((pass & FR_KEEPSTATE) && (fin->fin_m != NULL) &&
2988 !(fin->fin_flx & FI_STATE)) {
2989 if (ipf_state_add(softc, fin, NULL, 0) == 0) {
2990 LBUMP(ipf_stats[out].fr_ads);
2991 } else {
2992 LBUMP(ipf_stats[out].fr_bads);
2993 if (FR_ISPASS(pass)) {
2994 DT(frb_stateadd);
2995 pass &= ~FR_CMDMASK;
2996 pass |= FR_BLOCK;
2997 fin->fin_reason = FRB_STATEADD;
2998 }
2999 }
3000 }
3001
3002 fin->fin_fr = fr;
3003 if ((fr != NULL) && !(fin->fin_flx & FI_STATE)) {
3004 fin->fin_dif = &fr->fr_dif;
3005 fin->fin_tif = &fr->fr_tifs[fin->fin_rev];
3006 }
3007
3008 /*
3009 * Only count/translate packets which will be passed on, out the
3010 * interface.
3011 */
3012 if (out && FR_ISPASS(pass)) {
3013 (void) ipf_acctpkt(fin, NULL);
3014
3015 switch (fin->fin_v)
3016 {
3017 case 4 :
3018 if (ipf_nat_checkout(fin, &pass) == -1) {
3019 ;
3020 } else if ((softc->ipf_update_ipid != 0) && (v == 4)) {
3021 if (ipf_updateipid(fin) == -1) {
3022 DT(frb_updateipid);
3023 LBUMP(ipf_stats[1].fr_ipud);
3024 pass &= ~FR_CMDMASK;
3025 pass |= FR_BLOCK;
3026 fin->fin_reason = FRB_UPDATEIPID;
3027 } else {
3028 LBUMP(ipf_stats[0].fr_ipud);
3029 }
3030 }
3031 break;
3032 #ifdef USE_INET6
3033 case 6 :
3034 (void) ipf_nat6_checkout(fin, &pass);
3035 break;
3036 #endif
3037 default :
3038 break;
3039 }
3040 }
3041
3042 filterdone:
3043 #ifdef IPFILTER_LOG
3044 if ((softc->ipf_flags & FF_LOGGING) || (pass & FR_LOGMASK)) {
3045 (void) ipf_dolog(fin, &pass);
3046 }
3047 #endif
3048
3049 /*
3050 * The FI_STATE flag is cleared here so that calling ipf_state_check
3051 * will work when called from inside of fr_fastroute. Although
3052 * there is a similar flag, FI_NATED, for NAT, it does have the same
3053 * impact on code execution.
3054 */
3055 fin->fin_flx &= ~FI_STATE;
3056
3057 #if defined(FASTROUTE_RECURSION)
3058 /*
3059 * Up the reference on fr_lock and exit ipf_mutex. The generation of
3060 * a packet below can sometimes cause a recursive call into IPFilter.
3061 * On those platforms where that does happen, we need to hang onto
3062 * the filter rule just in case someone decides to remove or flush it
3063 * in the meantime.
3064 */
3065 if (fr != NULL) {
3066 MUTEX_ENTER(&fr->fr_lock);
3067 fr->fr_ref++;
3068 MUTEX_EXIT(&fr->fr_lock);
3069 }
3070
3071 RWLOCK_EXIT(&softc->ipf_mutex);
3072 #endif
3073
3074 if ((pass & FR_RETMASK) != 0) {
3075 /*
3076 * Should we return an ICMP packet to indicate error
3077 * status passing through the packet filter ?
3078 * WARNING: ICMP error packets AND TCP RST packets should
3079 * ONLY be sent in repsonse to incoming packets. Sending
3080 * them in response to outbound packets can result in a
3081 * panic on some operating systems.
3082 */
3083 if (!out) {
3084 if (pass & FR_RETICMP) {
3085 int dst;
3086
3087 if ((pass & FR_RETMASK) == FR_FAKEICMP)
3088 dst = 1;
3089 else
3090 dst = 0;
3091 (void) ipf_send_icmp_err(ICMP_UNREACH, fin,
3092 dst);
3093 LBUMP(ipf_stats[0].fr_ret);
3094 } else if (((pass & FR_RETMASK) == FR_RETRST) &&
3095 !(fin->fin_flx & FI_SHORT)) {
3096 if (((fin->fin_flx & FI_OOW) != 0) ||
3097 (ipf_send_reset(fin) == 0)) {
3098 LBUMP(ipf_stats[1].fr_ret);
3099 }
3100 }
3101
3102 /*
3103 * When using return-* with auth rules, the auth code
3104 * takes over disposing of this packet.
3105 */
3106 if (FR_ISAUTH(pass) && (fin->fin_m != NULL)) {
3107 DT1(frb_authcapture, fr_info_t *, fin);
3108 fin->fin_m = *fin->fin_mp = NULL;
3109 fin->fin_reason = FRB_AUTHCAPTURE;
3110 m = NULL;
3111 }
3112 } else {
3113 if (pass & FR_RETRST) {
3114 fin->fin_error = ECONNRESET;
3115 }
3116 }
3117 }
3118
3119 /*
3120 * After the above so that ICMP unreachables and TCP RSTs get
3121 * created properly.
3122 */
3123 if (FR_ISBLOCK(pass) && (fin->fin_flx & FI_NEWNAT))
3124 ipf_nat_uncreate(fin);
3125
3126 /*
3127 * If we didn't drop off the bottom of the list of rules (and thus
3128 * the 'current' rule fr is not NULL), then we may have some extra
3129 * instructions about what to do with a packet.
3130 * Once we're finished return to our caller, freeing the packet if
3131 * we are dropping it.
3132 */
3133 if (fr != NULL) {
3134 frdest_t *fdp;
3135
3136 /*
3137 * Generate a duplicated packet first because ipf_fastroute
3138 * can lead to fin_m being free'd... not good.
3139 */
3140 fdp = fin->fin_dif;
3141 if ((fdp != NULL) && (fdp->fd_ptr != NULL) &&
3142 (fdp->fd_ptr != (void *)-1)) {
3143 mc = M_COPY(fin->fin_m);
3144 if (mc != NULL)
3145 ipf_fastroute(mc, &mc, fin, fdp);
3146 }
3147
3148 fdp = fin->fin_tif;
3149 if (!out && (pass & FR_FASTROUTE)) {
3150 /*
3151 * For fastroute rule, no destination interface defined
3152 * so pass NULL as the frdest_t parameter
3153 */
3154 (void) ipf_fastroute(fin->fin_m, mp, fin, NULL);
3155 m = *mp = NULL;
3156 } else if ((fdp != NULL) && (fdp->fd_ptr != NULL) &&
3157 (fdp->fd_ptr != (struct ifnet *)-1)) {
3158 /* this is for to rules: */
3159 ipf_fastroute(fin->fin_m, mp, fin, fdp);
3160 m = *mp = NULL;
3161 }
3162
3163 #if defined(FASTROUTE_RECURSION)
3164 (void) ipf_derefrule(softc, &fr);
3165 #endif
3166 }
3167 #if !defined(FASTROUTE_RECURSION)
3168 RWLOCK_EXIT(&softc->ipf_mutex);
3169 #endif
3170
3171 finished:
3172 if (!FR_ISPASS(pass)) {
3173 LBUMP(ipf_stats[out].fr_block);
3174 if (*mp != NULL) {
3175 #ifdef _KERNEL
3176 FREE_MB_T(*mp);
3177 #endif
3178 m = *mp = NULL;
3179 }
3180 } else {
3181 LBUMP(ipf_stats[out].fr_pass);
3182 }
3183
3184 SPL_X(s);
3185
3186 if (fin->fin_m == NULL && fin->fin_flx & FI_BAD &&
3187 fin->fin_reason == FRB_PULLUP) {
3188 /* m_pullup() has freed the mbuf */
3189 LBUMP(ipf_stats[out].fr_blocked[fin->fin_reason]);
3190 return (-1);
3191 }
3192
3193
3194 #ifdef _KERNEL
3195 if (FR_ISPASS(pass))
3196 return (0);
3197 LBUMP(ipf_stats[out].fr_blocked[fin->fin_reason]);
3198 return (fin->fin_error);
3199 #else /* _KERNEL */
3200 if (*mp != NULL)
3201 (*mp)->mb_ifp = fin->fin_ifp;
3202 blockreason = fin->fin_reason;
3203 FR_VERBOSE(("fin_flx %#x pass %#x ", fin->fin_flx, pass));
3204 /*if ((pass & FR_CMDMASK) == (softc->ipf_pass & FR_CMDMASK))*/
3205 if ((pass & FR_NOMATCH) != 0)
3206 return (1);
3207
3208 if ((pass & FR_RETMASK) != 0)
3209 switch (pass & FR_RETMASK)
3210 {
3211 case FR_RETRST :
3212 return (3);
3213 case FR_RETICMP :
3214 return (4);
3215 case FR_FAKEICMP :
3216 return (5);
3217 }
3218
3219 switch (pass & FR_CMDMASK)
3220 {
3221 case FR_PASS :
3222 return (0);
3223 case FR_BLOCK :
3224 return (-1);
3225 case FR_AUTH :
3226 return (-2);
3227 case FR_ACCOUNT :
3228 return (-3);
3229 case FR_PREAUTH :
3230 return (-4);
3231 }
3232 return (2);
3233 #endif /* _KERNEL */
3234 }
3235
3236
3237 #ifdef IPFILTER_LOG
3238 /* ------------------------------------------------------------------------ */
3239 /* Function: ipf_dolog */
3240 /* Returns: frentry_t* - returns contents of fin_fr (no change made) */
3241 /* Parameters: fin(I) - pointer to packet information */
3242 /* passp(IO) - pointer to current/new filter decision (unused) */
3243 /* */
3244 /* Checks flags set to see how a packet should be logged, if it is to be */
3245 /* logged. Adjust statistics based on its success or not. */
3246 /* ------------------------------------------------------------------------ */
3247 frentry_t *
ipf_dolog(fr_info_t * fin,u_32_t * passp)3248 ipf_dolog(fr_info_t *fin, u_32_t *passp)
3249 {
3250 ipf_main_softc_t *softc = fin->fin_main_soft;
3251 u_32_t pass;
3252 int out;
3253
3254 out = fin->fin_out;
3255 pass = *passp;
3256
3257 if ((softc->ipf_flags & FF_LOGNOMATCH) && (pass & FR_NOMATCH)) {
3258 pass |= FF_LOGNOMATCH;
3259 LBUMPD(ipf_stats[out], fr_npkl);
3260 goto logit;
3261
3262 } else if (((pass & FR_LOGMASK) == FR_LOGP) ||
3263 (FR_ISPASS(pass) && (softc->ipf_flags & FF_LOGPASS))) {
3264 if ((pass & FR_LOGMASK) != FR_LOGP)
3265 pass |= FF_LOGPASS;
3266 LBUMPD(ipf_stats[out], fr_ppkl);
3267 goto logit;
3268
3269 } else if (((pass & FR_LOGMASK) == FR_LOGB) ||
3270 (FR_ISBLOCK(pass) && (softc->ipf_flags & FF_LOGBLOCK))) {
3271 if ((pass & FR_LOGMASK) != FR_LOGB)
3272 pass |= FF_LOGBLOCK;
3273 LBUMPD(ipf_stats[out], fr_bpkl);
3274
3275 logit:
3276 if (ipf_log_pkt(fin, pass) == -1) {
3277 /*
3278 * If the "or-block" option has been used then
3279 * block the packet if we failed to log it.
3280 */
3281 if ((pass & FR_LOGORBLOCK) && FR_ISPASS(pass)) {
3282 DT1(frb_logfail2, u_int, pass);
3283 pass &= ~FR_CMDMASK;
3284 pass |= FR_BLOCK;
3285 fin->fin_reason = FRB_LOGFAIL2;
3286 }
3287 }
3288 *passp = pass;
3289 }
3290
3291 return (fin->fin_fr);
3292 }
3293 #endif /* IPFILTER_LOG */
3294
3295
3296 /* ------------------------------------------------------------------------ */
3297 /* Function: ipf_cksum */
3298 /* Returns: u_short - IP header checksum */
3299 /* Parameters: addr(I) - pointer to start of buffer to checksum */
3300 /* len(I) - length of buffer in bytes */
3301 /* */
3302 /* Calculate the two's complement 16 bit checksum of the buffer passed. */
3303 /* */
3304 /* N.B.: addr should be 16bit aligned. */
3305 /* ------------------------------------------------------------------------ */
3306 u_short
ipf_cksum(u_short * addr,int len)3307 ipf_cksum(u_short *addr, int len)
3308 {
3309 u_32_t sum = 0;
3310
3311 for (sum = 0; len > 1; len -= 2)
3312 sum += *addr++;
3313
3314 /* mop up an odd byte, if necessary */
3315 if (len == 1)
3316 sum += *(u_char *)addr;
3317
3318 /*
3319 * add back carry outs from top 16 bits to low 16 bits
3320 */
3321 sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */
3322 sum += (sum >> 16); /* add carry */
3323 return (u_short)(~sum);
3324 }
3325
3326
3327 /* ------------------------------------------------------------------------ */
3328 /* Function: fr_cksum */
3329 /* Returns: u_short - layer 4 checksum */
3330 /* Parameters: fin(I) - pointer to packet information */
3331 /* ip(I) - pointer to IP header */
3332 /* l4proto(I) - protocol to caclulate checksum for */
3333 /* l4hdr(I) - pointer to layer 4 header */
3334 /* */
3335 /* Calculates the TCP checksum for the packet held in "m", using the data */
3336 /* in the IP header "ip" to seed it. */
3337 /* */
3338 /* NB: This function assumes we've pullup'd enough for all of the IP header */
3339 /* and the TCP header. We also assume that data blocks aren't allocated in */
3340 /* odd sizes. */
3341 /* */
3342 /* Expects ip_len and ip_off to be in network byte order when called. */
3343 /* ------------------------------------------------------------------------ */
3344 u_short
fr_cksum(fr_info_t * fin,ip_t * ip,int l4proto,void * l4hdr)3345 fr_cksum(fr_info_t *fin, ip_t *ip, int l4proto, void *l4hdr)
3346 {
3347 u_short *sp, slen, sumsave, *csump;
3348 u_int sum, sum2;
3349 int hlen;
3350 int off;
3351 #ifdef USE_INET6
3352 ip6_t *ip6;
3353 #endif
3354
3355 csump = NULL;
3356 sumsave = 0;
3357 sp = NULL;
3358 slen = 0;
3359 hlen = 0;
3360 sum = 0;
3361
3362 sum = htons((u_short)l4proto);
3363 /*
3364 * Add up IP Header portion
3365 */
3366 #ifdef USE_INET6
3367 if (IP_V(ip) == 4) {
3368 #endif
3369 hlen = IP_HL(ip) << 2;
3370 off = hlen;
3371 sp = (u_short *)&ip->ip_src;
3372 sum += *sp++; /* ip_src */
3373 sum += *sp++;
3374 sum += *sp++; /* ip_dst */
3375 sum += *sp++;
3376 slen = fin->fin_plen - off;
3377 sum += htons(slen);
3378 #ifdef USE_INET6
3379 } else if (IP_V(ip) == 6) {
3380 mb_t *m;
3381
3382 m = fin->fin_m;
3383 ip6 = (ip6_t *)ip;
3384 off = ((caddr_t)ip6 - m->m_data) + sizeof(struct ip6_hdr);
3385 int len = ntohs(ip6->ip6_plen) - (off - sizeof(*ip6));
3386 return (ipf_pcksum6(m, ip6, off, len));
3387 } else {
3388 return (0xffff);
3389 }
3390 #endif
3391
3392 switch (l4proto)
3393 {
3394 case IPPROTO_UDP :
3395 csump = &((udphdr_t *)l4hdr)->uh_sum;
3396 break;
3397
3398 case IPPROTO_TCP :
3399 csump = &((tcphdr_t *)l4hdr)->th_sum;
3400 break;
3401 case IPPROTO_ICMP :
3402 csump = &((icmphdr_t *)l4hdr)->icmp_cksum;
3403 sum = 0; /* Pseudo-checksum is not included */
3404 break;
3405 #ifdef USE_INET6
3406 case IPPROTO_ICMPV6 :
3407 csump = &((struct icmp6_hdr *)l4hdr)->icmp6_cksum;
3408 break;
3409 #endif
3410 default :
3411 break;
3412 }
3413
3414 if (csump != NULL) {
3415 sumsave = *csump;
3416 *csump = 0;
3417 }
3418
3419 sum2 = ipf_pcksum(fin, off, sum);
3420 if (csump != NULL)
3421 *csump = sumsave;
3422 return (sum2);
3423 }
3424
3425
3426 /* ------------------------------------------------------------------------ */
3427 /* Function: ipf_findgroup */
3428 /* Returns: frgroup_t * - NULL = group not found, else pointer to group */
3429 /* Parameters: softc(I) - pointer to soft context main structure */
3430 /* group(I) - group name to search for */
3431 /* unit(I) - device to which this group belongs */
3432 /* set(I) - which set of rules (inactive/inactive) this is */
3433 /* fgpp(O) - pointer to place to store pointer to the pointer */
3434 /* to where to add the next (last) group or where */
3435 /* to delete group from. */
3436 /* */
3437 /* Search amongst the defined groups for a particular group number. */
3438 /* ------------------------------------------------------------------------ */
3439 frgroup_t *
ipf_findgroup(ipf_main_softc_t * softc,char * group,minor_t unit,int set,frgroup_t *** fgpp)3440 ipf_findgroup(ipf_main_softc_t *softc, char *group, minor_t unit, int set,
3441 frgroup_t ***fgpp)
3442 {
3443 frgroup_t *fg, **fgp;
3444
3445 /*
3446 * Which list of groups to search in is dependent on which list of
3447 * rules are being operated on.
3448 */
3449 fgp = &softc->ipf_groups[unit][set];
3450
3451 while ((fg = *fgp) != NULL) {
3452 if (strncmp(group, fg->fg_name, FR_GROUPLEN) == 0)
3453 break;
3454 else
3455 fgp = &fg->fg_next;
3456 }
3457 if (fgpp != NULL)
3458 *fgpp = fgp;
3459 return (fg);
3460 }
3461
3462
3463 /* ------------------------------------------------------------------------ */
3464 /* Function: ipf_group_add */
3465 /* Returns: frgroup_t * - NULL == did not create group, */
3466 /* != NULL == pointer to the group */
3467 /* Parameters: softc(I) - pointer to soft context main structure */
3468 /* num(I) - group number to add */
3469 /* head(I) - rule pointer that is using this as the head */
3470 /* flags(I) - rule flags which describe the type of rule it is */
3471 /* unit(I) - device to which this group will belong to */
3472 /* set(I) - which set of rules (inactive/inactive) this is */
3473 /* Write Locks: ipf_mutex */
3474 /* */
3475 /* Add a new group head, or if it already exists, increase the reference */
3476 /* count to it. */
3477 /* ------------------------------------------------------------------------ */
3478 frgroup_t *
ipf_group_add(ipf_main_softc_t * softc,char * group,void * head,u_32_t flags,minor_t unit,int set)3479 ipf_group_add(ipf_main_softc_t *softc, char *group, void *head, u_32_t flags,
3480 minor_t unit, int set)
3481 {
3482 frgroup_t *fg, **fgp;
3483 u_32_t gflags;
3484
3485 if (group == NULL)
3486 return (NULL);
3487
3488 if (unit == IPL_LOGIPF && *group == '\0')
3489 return (NULL);
3490
3491 fgp = NULL;
3492 gflags = flags & FR_INOUT;
3493
3494 fg = ipf_findgroup(softc, group, unit, set, &fgp);
3495 if (fg != NULL) {
3496 if (fg->fg_head == NULL && head != NULL)
3497 fg->fg_head = head;
3498 if (fg->fg_flags == 0)
3499 fg->fg_flags = gflags;
3500 else if (gflags != fg->fg_flags)
3501 return (NULL);
3502 fg->fg_ref++;
3503 return (fg);
3504 }
3505
3506 KMALLOC(fg, frgroup_t *);
3507 if (fg != NULL) {
3508 fg->fg_head = head;
3509 fg->fg_start = NULL;
3510 fg->fg_next = *fgp;
3511 bcopy(group, fg->fg_name, strlen(group) + 1);
3512 fg->fg_flags = gflags;
3513 fg->fg_ref = 1;
3514 fg->fg_set = &softc->ipf_groups[unit][set];
3515 *fgp = fg;
3516 }
3517 return (fg);
3518 }
3519
3520
3521 /* ------------------------------------------------------------------------ */
3522 /* Function: ipf_group_del */
3523 /* Returns: int - number of rules deleted */
3524 /* Parameters: softc(I) - pointer to soft context main structure */
3525 /* group(I) - group name to delete */
3526 /* fr(I) - filter rule from which group is referenced */
3527 /* Write Locks: ipf_mutex */
3528 /* */
3529 /* This function is called whenever a reference to a group is to be dropped */
3530 /* and thus its reference count needs to be lowered and the group free'd if */
3531 /* the reference count reaches zero. Passing in fr is really for the sole */
3532 /* purpose of knowing when the head rule is being deleted. */
3533 /* ------------------------------------------------------------------------ */
3534 void
ipf_group_del(ipf_main_softc_t * softc,frgroup_t * group,frentry_t * fr)3535 ipf_group_del(ipf_main_softc_t *softc, frgroup_t *group, frentry_t *fr)
3536 {
3537
3538 if (group->fg_head == fr)
3539 group->fg_head = NULL;
3540
3541 group->fg_ref--;
3542 if ((group->fg_ref == 0) && (group->fg_start == NULL))
3543 ipf_group_free(group);
3544 }
3545
3546
3547 /* ------------------------------------------------------------------------ */
3548 /* Function: ipf_group_free */
3549 /* Returns: Nil */
3550 /* Parameters: group(I) - pointer to filter rule group */
3551 /* */
3552 /* Remove the group from the list of groups and free it. */
3553 /* ------------------------------------------------------------------------ */
3554 static void
ipf_group_free(frgroup_t * group)3555 ipf_group_free(frgroup_t *group)
3556 {
3557 frgroup_t **gp;
3558
3559 for (gp = group->fg_set; *gp != NULL; gp = &(*gp)->fg_next) {
3560 if (*gp == group) {
3561 *gp = group->fg_next;
3562 break;
3563 }
3564 }
3565 KFREE(group);
3566 }
3567
3568
3569 /* ------------------------------------------------------------------------ */
3570 /* Function: ipf_group_flush */
3571 /* Returns: int - number of rules flush from group */
3572 /* Parameters: softc(I) - pointer to soft context main structure */
3573 /* Parameters: group(I) - pointer to filter rule group */
3574 /* */
3575 /* Remove all of the rules that currently are listed under the given group. */
3576 /* ------------------------------------------------------------------------ */
3577 static int
ipf_group_flush(ipf_main_softc_t * softc,frgroup_t * group)3578 ipf_group_flush(ipf_main_softc_t *softc, frgroup_t *group)
3579 {
3580 int gone = 0;
3581
3582 (void) ipf_flushlist(softc, &gone, &group->fg_start);
3583
3584 return (gone);
3585 }
3586
3587
3588 /* ------------------------------------------------------------------------ */
3589 /* Function: ipf_getrulen */
3590 /* Returns: frentry_t * - NULL == not found, else pointer to rule n */
3591 /* Parameters: softc(I) - pointer to soft context main structure */
3592 /* Parameters: unit(I) - device for which to count the rule's number */
3593 /* flags(I) - which set of rules to find the rule in */
3594 /* group(I) - group name */
3595 /* n(I) - rule number to find */
3596 /* */
3597 /* Find rule # n in group # g and return a pointer to it. Return NULl if */
3598 /* group # g doesn't exist or there are less than n rules in the group. */
3599 /* ------------------------------------------------------------------------ */
3600 frentry_t *
ipf_getrulen(ipf_main_softc_t * softc,int unit,char * group,u_32_t n)3601 ipf_getrulen(ipf_main_softc_t *softc, int unit, char *group, u_32_t n)
3602 {
3603 frentry_t *fr;
3604 frgroup_t *fg;
3605
3606 fg = ipf_findgroup(softc, group, unit, softc->ipf_active, NULL);
3607 if (fg == NULL)
3608 return (NULL);
3609 for (fr = fg->fg_start; fr && n; fr = fr->fr_next, n--)
3610 ;
3611 if (n != 0)
3612 return (NULL);
3613 return (fr);
3614 }
3615
3616
3617 /* ------------------------------------------------------------------------ */
3618 /* Function: ipf_flushlist */
3619 /* Returns: int - >= 0 - number of flushed rules */
3620 /* Parameters: softc(I) - pointer to soft context main structure */
3621 /* nfreedp(O) - pointer to int where flush count is stored */
3622 /* listp(I) - pointer to list to flush pointer */
3623 /* Write Locks: ipf_mutex */
3624 /* */
3625 /* Recursively flush rules from the list, descending groups as they are */
3626 /* encountered. if a rule is the head of a group and it has lost all its */
3627 /* group members, then also delete the group reference. nfreedp is needed */
3628 /* to store the accumulating count of rules removed, whereas the returned */
3629 /* value is just the number removed from the current list. The latter is */
3630 /* needed to correctly adjust reference counts on rules that define groups. */
3631 /* */
3632 /* NOTE: Rules not loaded from user space cannot be flushed. */
3633 /* ------------------------------------------------------------------------ */
3634 static int
ipf_flushlist(ipf_main_softc_t * softc,int * nfreedp,frentry_t ** listp)3635 ipf_flushlist(ipf_main_softc_t *softc, int *nfreedp, frentry_t **listp)
3636 {
3637 int freed = 0;
3638 frentry_t *fp;
3639
3640 while ((fp = *listp) != NULL) {
3641 if ((fp->fr_type & FR_T_BUILTIN) ||
3642 !(fp->fr_flags & FR_COPIED)) {
3643 listp = &fp->fr_next;
3644 continue;
3645 }
3646 *listp = fp->fr_next;
3647 if (fp->fr_next != NULL)
3648 fp->fr_next->fr_pnext = fp->fr_pnext;
3649 fp->fr_pnext = NULL;
3650
3651 if (fp->fr_grphead != NULL) {
3652 freed += ipf_group_flush(softc, fp->fr_grphead);
3653 fp->fr_names[fp->fr_grhead] = '\0';
3654 }
3655
3656 if (fp->fr_icmpgrp != NULL) {
3657 freed += ipf_group_flush(softc, fp->fr_icmpgrp);
3658 fp->fr_names[fp->fr_icmphead] = '\0';
3659 }
3660
3661 if (fp->fr_srctrack.ht_max_nodes)
3662 ipf_rb_ht_flush(&fp->fr_srctrack);
3663
3664 fp->fr_next = NULL;
3665
3666 ASSERT(fp->fr_ref > 0);
3667 if (ipf_derefrule(softc, &fp) == 0)
3668 freed++;
3669 }
3670 *nfreedp += freed;
3671 return (freed);
3672 }
3673
3674
3675 /* ------------------------------------------------------------------------ */
3676 /* Function: ipf_flush */
3677 /* Returns: int - >= 0 - number of flushed rules */
3678 /* Parameters: softc(I) - pointer to soft context main structure */
3679 /* unit(I) - device for which to flush rules */
3680 /* flags(I) - which set of rules to flush */
3681 /* */
3682 /* Calls flushlist() for all filter rules (accounting, firewall - both IPv4 */
3683 /* and IPv6) as defined by the value of flags. */
3684 /* ------------------------------------------------------------------------ */
3685 int
ipf_flush(ipf_main_softc_t * softc,minor_t unit,int flags)3686 ipf_flush(ipf_main_softc_t *softc, minor_t unit, int flags)
3687 {
3688 int flushed = 0, set;
3689
3690 WRITE_ENTER(&softc->ipf_mutex);
3691
3692 set = softc->ipf_active;
3693 if ((flags & FR_INACTIVE) == FR_INACTIVE)
3694 set = 1 - set;
3695
3696 if (flags & FR_OUTQUE) {
3697 ipf_flushlist(softc, &flushed, &softc->ipf_rules[1][set]);
3698 ipf_flushlist(softc, &flushed, &softc->ipf_acct[1][set]);
3699 }
3700 if (flags & FR_INQUE) {
3701 ipf_flushlist(softc, &flushed, &softc->ipf_rules[0][set]);
3702 ipf_flushlist(softc, &flushed, &softc->ipf_acct[0][set]);
3703 }
3704
3705 flushed += ipf_flush_groups(softc, &softc->ipf_groups[unit][set],
3706 flags & (FR_INQUE|FR_OUTQUE));
3707
3708 RWLOCK_EXIT(&softc->ipf_mutex);
3709
3710 if (unit == IPL_LOGIPF) {
3711 int tmp;
3712
3713 tmp = ipf_flush(softc, IPL_LOGCOUNT, flags);
3714 if (tmp >= 0)
3715 flushed += tmp;
3716 }
3717 return (flushed);
3718 }
3719
3720
3721 /* ------------------------------------------------------------------------ */
3722 /* Function: ipf_flush_groups */
3723 /* Returns: int - >= 0 - number of flushed rules */
3724 /* Parameters: softc(I) - soft context pointerto work with */
3725 /* grhead(I) - pointer to the start of the group list to flush */
3726 /* flags(I) - which set of rules to flush */
3727 /* */
3728 /* Walk through all of the groups under the given group head and remove all */
3729 /* of those that match the flags passed in. The for loop here is bit more */
3730 /* complicated than usual because the removal of a rule with ipf_derefrule */
3731 /* may end up removing not only the structure pointed to by "fg" but also */
3732 /* what is fg_next and fg_next after that. So if a filter rule is actually */
3733 /* removed from the group then it is necessary to start again. */
3734 /* ------------------------------------------------------------------------ */
3735 static int
ipf_flush_groups(ipf_main_softc_t * softc,frgroup_t ** grhead,int flags)3736 ipf_flush_groups(ipf_main_softc_t *softc, frgroup_t **grhead, int flags)
3737 {
3738 frentry_t *fr, **frp;
3739 frgroup_t *fg, **fgp;
3740 int flushed = 0;
3741 int removed = 0;
3742
3743 for (fgp = grhead; (fg = *fgp) != NULL; ) {
3744 while ((fg != NULL) && ((fg->fg_flags & flags) == 0))
3745 fg = fg->fg_next;
3746 if (fg == NULL)
3747 break;
3748 removed = 0;
3749 frp = &fg->fg_start;
3750 while ((removed == 0) && ((fr = *frp) != NULL)) {
3751 if ((fr->fr_flags & flags) == 0) {
3752 frp = &fr->fr_next;
3753 } else {
3754 if (fr->fr_next != NULL)
3755 fr->fr_next->fr_pnext = fr->fr_pnext;
3756 *frp = fr->fr_next;
3757 fr->fr_pnext = NULL;
3758 fr->fr_next = NULL;
3759 (void) ipf_derefrule(softc, &fr);
3760 flushed++;
3761 removed++;
3762 }
3763 }
3764 if (removed == 0)
3765 fgp = &fg->fg_next;
3766 }
3767 return (flushed);
3768 }
3769
3770
3771 /* ------------------------------------------------------------------------ */
3772 /* Function: memstr */
3773 /* Returns: char * - NULL if failed, != NULL pointer to matching bytes */
3774 /* Parameters: src(I) - pointer to byte sequence to match */
3775 /* dst(I) - pointer to byte sequence to search */
3776 /* slen(I) - match length */
3777 /* dlen(I) - length available to search in */
3778 /* */
3779 /* Search dst for a sequence of bytes matching those at src and extend for */
3780 /* slen bytes. */
3781 /* ------------------------------------------------------------------------ */
3782 char *
memstr(const char * src,char * dst,size_t slen,size_t dlen)3783 memstr(const char *src, char *dst, size_t slen, size_t dlen)
3784 {
3785 char *s = NULL;
3786
3787 while (dlen >= slen) {
3788 if (bcmp(src, dst, slen) == 0) {
3789 s = dst;
3790 break;
3791 }
3792 dst++;
3793 dlen--;
3794 }
3795 return (s);
3796 }
3797 /* ------------------------------------------------------------------------ */
3798 /* Function: ipf_fixskip */
3799 /* Returns: Nil */
3800 /* Parameters: listp(IO) - pointer to start of list with skip rule */
3801 /* rp(I) - rule added/removed with skip in it. */
3802 /* addremove(I) - adjustment (-1/+1) to make to skip count, */
3803 /* depending on whether a rule was just added */
3804 /* or removed. */
3805 /* */
3806 /* Adjust all the rules in a list which would have skip'd past the position */
3807 /* where we are inserting to skip to the right place given the change. */
3808 /* ------------------------------------------------------------------------ */
3809 void
ipf_fixskip(frentry_t ** listp,frentry_t * rp,int addremove)3810 ipf_fixskip(frentry_t **listp, frentry_t *rp, int addremove)
3811 {
3812 int rules, rn;
3813 frentry_t *fp;
3814
3815 rules = 0;
3816 for (fp = *listp; (fp != NULL) && (fp != rp); fp = fp->fr_next)
3817 rules++;
3818
3819 if (fp == NULL)
3820 return;
3821
3822 for (rn = 0, fp = *listp; fp && (fp != rp); fp = fp->fr_next, rn++)
3823 if (FR_ISSKIP(fp->fr_flags) && (rn + fp->fr_arg >= rules))
3824 fp->fr_arg += addremove;
3825 }
3826
3827
3828 #ifdef _KERNEL
3829 /* ------------------------------------------------------------------------ */
3830 /* Function: count4bits */
3831 /* Returns: int - >= 0 - number of consecutive bits in input */
3832 /* Parameters: ip(I) - 32bit IP address */
3833 /* */
3834 /* IPv4 ONLY */
3835 /* count consecutive 1's in bit mask. If the mask generated by counting */
3836 /* consecutive 1's is different to that passed, return -1, else return # */
3837 /* of bits. */
3838 /* ------------------------------------------------------------------------ */
3839 int
count4bits(u_32_t ip)3840 count4bits(u_32_t ip)
3841 {
3842 u_32_t ipn;
3843 int cnt = 0, i, j;
3844
3845 ip = ipn = ntohl(ip);
3846 for (i = 32; i; i--, ipn *= 2)
3847 if (ipn & 0x80000000)
3848 cnt++;
3849 else
3850 break;
3851 ipn = 0;
3852 for (i = 32, j = cnt; i; i--, j--) {
3853 ipn *= 2;
3854 if (j > 0)
3855 ipn++;
3856 }
3857 if (ipn == ip)
3858 return (cnt);
3859 return (-1);
3860 }
3861
3862
3863 /* ------------------------------------------------------------------------ */
3864 /* Function: count6bits */
3865 /* Returns: int - >= 0 - number of consecutive bits in input */
3866 /* Parameters: msk(I) - pointer to start of IPv6 bitmask */
3867 /* */
3868 /* IPv6 ONLY */
3869 /* count consecutive 1's in bit mask. */
3870 /* ------------------------------------------------------------------------ */
3871 # ifdef USE_INET6
3872 int
count6bits(u_32_t * msk)3873 count6bits(u_32_t *msk)
3874 {
3875 int i = 0, k;
3876 u_32_t j;
3877
3878 for (k = 3; k >= 0; k--)
3879 if (msk[k] == 0xffffffff)
3880 i += 32;
3881 else {
3882 for (j = msk[k]; j; j <<= 1)
3883 if (j & 0x80000000)
3884 i++;
3885 }
3886 return (i);
3887 }
3888 # endif
3889 #endif /* _KERNEL */
3890
3891
3892 /* ------------------------------------------------------------------------ */
3893 /* Function: ipf_synclist */
3894 /* Returns: int - 0 = no failures, else indication of first failure */
3895 /* Parameters: fr(I) - start of filter list to sync interface names for */
3896 /* ifp(I) - interface pointer for limiting sync lookups */
3897 /* Write Locks: ipf_mutex */
3898 /* */
3899 /* Walk through a list of filter rules and resolve any interface names into */
3900 /* pointers. Where dynamic addresses are used, also update the IP address */
3901 /* used in the rule. The interface pointer is used to limit the lookups to */
3902 /* a specific set of matching names if it is non-NULL. */
3903 /* Errors can occur when resolving the destination name of to/dup-to fields */
3904 /* when the name points to a pool and that pool doest not exist. If this */
3905 /* does happen then it is necessary to check if there are any lookup refs */
3906 /* that need to be dropped before returning with an error. */
3907 /* ------------------------------------------------------------------------ */
3908 static int
ipf_synclist(ipf_main_softc_t * softc,frentry_t * fr,void * ifp)3909 ipf_synclist(ipf_main_softc_t *softc, frentry_t *fr, void *ifp)
3910 {
3911 frentry_t *frt, *start = fr;
3912 frdest_t *fdp;
3913 char *name;
3914 int error;
3915 void *ifa;
3916 int v, i;
3917
3918 error = 0;
3919
3920 for (; fr; fr = fr->fr_next) {
3921 if (fr->fr_family == AF_INET)
3922 v = 4;
3923 else if (fr->fr_family == AF_INET6)
3924 v = 6;
3925 else
3926 v = 0;
3927
3928 /*
3929 * Lookup all the interface names that are part of the rule.
3930 */
3931 for (i = 0; i < FR_NUM(fr->fr_ifas); i++) {
3932 if ((ifp != NULL) && (fr->fr_ifas[i] != ifp))
3933 continue;
3934 if (fr->fr_ifnames[i] == -1)
3935 continue;
3936 name = FR_NAME(fr, fr_ifnames[i]);
3937 fr->fr_ifas[i] = ipf_resolvenic(softc, name, v);
3938 }
3939
3940 if ((fr->fr_type & ~FR_T_BUILTIN) == FR_T_IPF) {
3941 if (fr->fr_satype != FRI_NORMAL &&
3942 fr->fr_satype != FRI_LOOKUP) {
3943 ifa = ipf_resolvenic(softc, fr->fr_names +
3944 fr->fr_sifpidx, v);
3945 ipf_ifpaddr(softc, v, fr->fr_satype, ifa,
3946 &fr->fr_src6, &fr->fr_smsk6);
3947 }
3948 if (fr->fr_datype != FRI_NORMAL &&
3949 fr->fr_datype != FRI_LOOKUP) {
3950 ifa = ipf_resolvenic(softc, fr->fr_names +
3951 fr->fr_sifpidx, v);
3952 ipf_ifpaddr(softc, v, fr->fr_datype, ifa,
3953 &fr->fr_dst6, &fr->fr_dmsk6);
3954 }
3955 }
3956
3957 fdp = &fr->fr_tifs[0];
3958 if ((ifp == NULL) || (fdp->fd_ptr == ifp)) {
3959 error = ipf_resolvedest(softc, fr->fr_names, fdp, v);
3960 if (error != 0)
3961 goto unwind;
3962 }
3963
3964 fdp = &fr->fr_tifs[1];
3965 if ((ifp == NULL) || (fdp->fd_ptr == ifp)) {
3966 error = ipf_resolvedest(softc, fr->fr_names, fdp, v);
3967 if (error != 0)
3968 goto unwind;
3969 }
3970
3971 fdp = &fr->fr_dif;
3972 if ((ifp == NULL) || (fdp->fd_ptr == ifp)) {
3973 error = ipf_resolvedest(softc, fr->fr_names, fdp, v);
3974 if (error != 0)
3975 goto unwind;
3976 }
3977
3978 if (((fr->fr_type & ~FR_T_BUILTIN) == FR_T_IPF) &&
3979 (fr->fr_satype == FRI_LOOKUP) && (fr->fr_srcptr == NULL)) {
3980 fr->fr_srcptr = ipf_lookup_res_num(softc,
3981 fr->fr_srctype,
3982 IPL_LOGIPF,
3983 fr->fr_srcnum,
3984 &fr->fr_srcfunc);
3985 }
3986 if (((fr->fr_type & ~FR_T_BUILTIN) == FR_T_IPF) &&
3987 (fr->fr_datype == FRI_LOOKUP) && (fr->fr_dstptr == NULL)) {
3988 fr->fr_dstptr = ipf_lookup_res_num(softc,
3989 fr->fr_dsttype,
3990 IPL_LOGIPF,
3991 fr->fr_dstnum,
3992 &fr->fr_dstfunc);
3993 }
3994 }
3995 return (0);
3996
3997 unwind:
3998 for (frt = start; frt != fr; fr = fr->fr_next) {
3999 if (((frt->fr_type & ~FR_T_BUILTIN) == FR_T_IPF) &&
4000 (frt->fr_satype == FRI_LOOKUP) && (frt->fr_srcptr != NULL))
4001 ipf_lookup_deref(softc, frt->fr_srctype,
4002 frt->fr_srcptr);
4003 if (((frt->fr_type & ~FR_T_BUILTIN) == FR_T_IPF) &&
4004 (frt->fr_datype == FRI_LOOKUP) && (frt->fr_dstptr != NULL))
4005 ipf_lookup_deref(softc, frt->fr_dsttype,
4006 frt->fr_dstptr);
4007 }
4008 return (error);
4009 }
4010
4011
4012 /* ------------------------------------------------------------------------ */
4013 /* Function: ipf_sync */
4014 /* Returns: void */
4015 /* Parameters: Nil */
4016 /* */
4017 /* ipf_sync() is called when we suspect that the interface list or */
4018 /* information about interfaces (like IP#) has changed. Go through all */
4019 /* filter rules, NAT entries and the state table and check if anything */
4020 /* needs to be changed/updated. */
4021 /* ------------------------------------------------------------------------ */
4022 int
ipf_sync(ipf_main_softc_t * softc,void * ifp)4023 ipf_sync(ipf_main_softc_t *softc, void *ifp)
4024 {
4025 int i;
4026
4027 #if !SOLARIS
4028 ipf_nat_sync(softc, ifp);
4029 ipf_state_sync(softc, ifp);
4030 ipf_lookup_sync(softc, ifp);
4031 #endif
4032
4033 WRITE_ENTER(&softc->ipf_mutex);
4034 (void) ipf_synclist(softc, softc->ipf_acct[0][softc->ipf_active], ifp);
4035 (void) ipf_synclist(softc, softc->ipf_acct[1][softc->ipf_active], ifp);
4036 (void) ipf_synclist(softc, softc->ipf_rules[0][softc->ipf_active], ifp);
4037 (void) ipf_synclist(softc, softc->ipf_rules[1][softc->ipf_active], ifp);
4038
4039 for (i = 0; i < IPL_LOGSIZE; i++) {
4040 frgroup_t *g;
4041
4042 for (g = softc->ipf_groups[i][0]; g != NULL; g = g->fg_next)
4043 (void) ipf_synclist(softc, g->fg_start, ifp);
4044 for (g = softc->ipf_groups[i][1]; g != NULL; g = g->fg_next)
4045 (void) ipf_synclist(softc, g->fg_start, ifp);
4046 }
4047 RWLOCK_EXIT(&softc->ipf_mutex);
4048
4049 return (0);
4050 }
4051
4052
4053 /*
4054 * In the functions below, bcopy() is called because the pointer being
4055 * copied _from_ in this instance is a pointer to a char buf (which could
4056 * end up being unaligned) and on the kernel's local stack.
4057 */
4058 /* ------------------------------------------------------------------------ */
4059 /* Function: copyinptr */
4060 /* Returns: int - 0 = success, else failure */
4061 /* Parameters: src(I) - pointer to the source address */
4062 /* dst(I) - destination address */
4063 /* size(I) - number of bytes to copy */
4064 /* */
4065 /* Copy a block of data in from user space, given a pointer to the pointer */
4066 /* to start copying from (src) and a pointer to where to store it (dst). */
4067 /* NB: src - pointer to user space pointer, dst - kernel space pointer */
4068 /* ------------------------------------------------------------------------ */
4069 int
copyinptr(ipf_main_softc_t * softc,void * src,void * dst,size_t size)4070 copyinptr(ipf_main_softc_t *softc, void *src, void *dst, size_t size)
4071 {
4072 caddr_t ca;
4073 int error;
4074
4075 #if SOLARIS
4076 error = COPYIN(src, &ca, sizeof(ca));
4077 if (error != 0)
4078 return (error);
4079 #else
4080 bcopy(src, (caddr_t)&ca, sizeof(ca));
4081 #endif
4082 error = COPYIN(ca, dst, size);
4083 if (error != 0) {
4084 IPFERROR(3);
4085 error = EFAULT;
4086 }
4087 return (error);
4088 }
4089
4090
4091 /* ------------------------------------------------------------------------ */
4092 /* Function: copyoutptr */
4093 /* Returns: int - 0 = success, else failure */
4094 /* Parameters: src(I) - pointer to the source address */
4095 /* dst(I) - destination address */
4096 /* size(I) - number of bytes to copy */
4097 /* */
4098 /* Copy a block of data out to user space, given a pointer to the pointer */
4099 /* to start copying from (src) and a pointer to where to store it (dst). */
4100 /* NB: src - kernel space pointer, dst - pointer to user space pointer. */
4101 /* ------------------------------------------------------------------------ */
4102 int
copyoutptr(ipf_main_softc_t * softc,void * src,void * dst,size_t size)4103 copyoutptr(ipf_main_softc_t *softc, void *src, void *dst, size_t size)
4104 {
4105 caddr_t ca;
4106 int error;
4107
4108 bcopy(dst, (caddr_t)&ca, sizeof(ca));
4109 error = COPYOUT(src, ca, size);
4110 if (error != 0) {
4111 IPFERROR(4);
4112 error = EFAULT;
4113 }
4114 return (error);
4115 }
4116
4117
4118 /* ------------------------------------------------------------------------ */
4119 /* Function: ipf_lock */
4120 /* Returns: int - 0 = success, else error */
4121 /* Parameters: data(I) - pointer to lock value to set */
4122 /* lockp(O) - pointer to location to store old lock value */
4123 /* */
4124 /* Get the new value for the lock integer, set it and return the old value */
4125 /* in *lockp. */
4126 /* ------------------------------------------------------------------------ */
4127 int
ipf_lock(caddr_t data,int * lockp)4128 ipf_lock(caddr_t data, int *lockp)
4129 {
4130 int arg, err;
4131
4132 err = BCOPYIN(data, &arg, sizeof(arg));
4133 if (err != 0)
4134 return (EFAULT);
4135 err = BCOPYOUT(lockp, data, sizeof(*lockp));
4136 if (err != 0)
4137 return (EFAULT);
4138 *lockp = arg;
4139 return (0);
4140 }
4141
4142
4143 /* ------------------------------------------------------------------------ */
4144 /* Function: ipf_getstat */
4145 /* Returns: Nil */
4146 /* Parameters: softc(I) - pointer to soft context main structure */
4147 /* fiop(I) - pointer to ipfilter stats structure */
4148 /* rev(I) - version claim by program doing ioctl */
4149 /* */
4150 /* Stores a copy of current pointers, counters, etc, in the friostat */
4151 /* structure. */
4152 /* If IPFILTER_COMPAT is compiled, we pretend to be whatever version the */
4153 /* program is looking for. This ensure that validation of the version it */
4154 /* expects will always succeed. Thus kernels with IPFILTER_COMPAT will */
4155 /* allow older binaries to work but kernels without it will not. */
4156 /* ------------------------------------------------------------------------ */
4157 /*ARGSUSED*/
4158 static void
ipf_getstat(ipf_main_softc_t * softc,friostat_t * fiop,int rev)4159 ipf_getstat(ipf_main_softc_t *softc, friostat_t *fiop, int rev)
4160 {
4161 int i;
4162
4163 bcopy((char *)softc->ipf_stats, (char *)fiop->f_st,
4164 sizeof(ipf_statistics_t) * 2);
4165 fiop->f_locks[IPL_LOGSTATE] = -1;
4166 fiop->f_locks[IPL_LOGNAT] = -1;
4167 fiop->f_locks[IPL_LOGIPF] = -1;
4168 fiop->f_locks[IPL_LOGAUTH] = -1;
4169
4170 fiop->f_ipf[0][0] = softc->ipf_rules[0][0];
4171 fiop->f_acct[0][0] = softc->ipf_acct[0][0];
4172 fiop->f_ipf[0][1] = softc->ipf_rules[0][1];
4173 fiop->f_acct[0][1] = softc->ipf_acct[0][1];
4174 fiop->f_ipf[1][0] = softc->ipf_rules[1][0];
4175 fiop->f_acct[1][0] = softc->ipf_acct[1][0];
4176 fiop->f_ipf[1][1] = softc->ipf_rules[1][1];
4177 fiop->f_acct[1][1] = softc->ipf_acct[1][1];
4178
4179 fiop->f_ticks = softc->ipf_ticks;
4180 fiop->f_active = softc->ipf_active;
4181 fiop->f_froute[0] = softc->ipf_frouteok[0];
4182 fiop->f_froute[1] = softc->ipf_frouteok[1];
4183 fiop->f_rb_no_mem = softc->ipf_rb_no_mem;
4184 fiop->f_rb_node_max = softc->ipf_rb_node_max;
4185
4186 fiop->f_running = softc->ipf_running;
4187 for (i = 0; i < IPL_LOGSIZE; i++) {
4188 fiop->f_groups[i][0] = softc->ipf_groups[i][0];
4189 fiop->f_groups[i][1] = softc->ipf_groups[i][1];
4190 }
4191 #ifdef IPFILTER_LOG
4192 fiop->f_log_ok = ipf_log_logok(softc, IPL_LOGIPF);
4193 fiop->f_log_fail = ipf_log_failures(softc, IPL_LOGIPF);
4194 fiop->f_logging = 1;
4195 #else
4196 fiop->f_log_ok = 0;
4197 fiop->f_log_fail = 0;
4198 fiop->f_logging = 0;
4199 #endif
4200 fiop->f_defpass = softc->ipf_pass;
4201 fiop->f_features = ipf_features;
4202
4203 #ifdef IPFILTER_COMPAT
4204 snprintf(fiop->f_version, sizeof(friostat.f_version), "IP Filter: v%d.%d.%d",
4205 (rev / 1000000) % 100,
4206 (rev / 10000) % 100,
4207 (rev / 100) % 100);
4208 #else
4209 rev = rev;
4210 (void) strncpy(fiop->f_version, ipfilter_version,
4211 sizeof(fiop->f_version));
4212 #endif
4213 }
4214
4215
4216 #ifdef USE_INET6
4217 int icmptoicmp6types[ICMP_MAXTYPE+1] = {
4218 ICMP6_ECHO_REPLY, /* 0: ICMP_ECHOREPLY */
4219 -1, /* 1: UNUSED */
4220 -1, /* 2: UNUSED */
4221 ICMP6_DST_UNREACH, /* 3: ICMP_UNREACH */
4222 -1, /* 4: ICMP_SOURCEQUENCH */
4223 ND_REDIRECT, /* 5: ICMP_REDIRECT */
4224 -1, /* 6: UNUSED */
4225 -1, /* 7: UNUSED */
4226 ICMP6_ECHO_REQUEST, /* 8: ICMP_ECHO */
4227 -1, /* 9: UNUSED */
4228 -1, /* 10: UNUSED */
4229 ICMP6_TIME_EXCEEDED, /* 11: ICMP_TIMXCEED */
4230 ICMP6_PARAM_PROB, /* 12: ICMP_PARAMPROB */
4231 -1, /* 13: ICMP_TSTAMP */
4232 -1, /* 14: ICMP_TSTAMPREPLY */
4233 -1, /* 15: ICMP_IREQ */
4234 -1, /* 16: ICMP_IREQREPLY */
4235 -1, /* 17: ICMP_MASKREQ */
4236 -1, /* 18: ICMP_MASKREPLY */
4237 };
4238
4239
4240 int icmptoicmp6unreach[ICMP_MAX_UNREACH] = {
4241 ICMP6_DST_UNREACH_ADDR, /* 0: ICMP_UNREACH_NET */
4242 ICMP6_DST_UNREACH_ADDR, /* 1: ICMP_UNREACH_HOST */
4243 -1, /* 2: ICMP_UNREACH_PROTOCOL */
4244 ICMP6_DST_UNREACH_NOPORT, /* 3: ICMP_UNREACH_PORT */
4245 -1, /* 4: ICMP_UNREACH_NEEDFRAG */
4246 ICMP6_DST_UNREACH_NOTNEIGHBOR, /* 5: ICMP_UNREACH_SRCFAIL */
4247 ICMP6_DST_UNREACH_ADDR, /* 6: ICMP_UNREACH_NET_UNKNOWN */
4248 ICMP6_DST_UNREACH_ADDR, /* 7: ICMP_UNREACH_HOST_UNKNOWN */
4249 -1, /* 8: ICMP_UNREACH_ISOLATED */
4250 ICMP6_DST_UNREACH_ADMIN, /* 9: ICMP_UNREACH_NET_PROHIB */
4251 ICMP6_DST_UNREACH_ADMIN, /* 10: ICMP_UNREACH_HOST_PROHIB */
4252 -1, /* 11: ICMP_UNREACH_TOSNET */
4253 -1, /* 12: ICMP_UNREACH_TOSHOST */
4254 ICMP6_DST_UNREACH_ADMIN, /* 13: ICMP_UNREACH_ADMIN_PROHIBIT */
4255 };
4256 int icmpreplytype6[ICMP6_MAXTYPE + 1];
4257 #endif
4258
4259 int icmpreplytype4[ICMP_MAXTYPE + 1];
4260
4261
4262 /* ------------------------------------------------------------------------ */
4263 /* Function: ipf_matchicmpqueryreply */
4264 /* Returns: int - 1 if "icmp" is a valid reply to "ic" else 0. */
4265 /* Parameters: v(I) - IP protocol version (4 or 6) */
4266 /* ic(I) - ICMP information */
4267 /* icmp(I) - ICMP packet header */
4268 /* rev(I) - direction (0 = forward/1 = reverse) of packet */
4269 /* */
4270 /* Check if the ICMP packet defined by the header pointed to by icmp is a */
4271 /* reply to one as described by what's in ic. If it is a match, return 1, */
4272 /* else return 0 for no match. */
4273 /* ------------------------------------------------------------------------ */
4274 int
ipf_matchicmpqueryreply(int v,icmpinfo_t * ic,icmphdr_t * icmp,int rev)4275 ipf_matchicmpqueryreply(int v, icmpinfo_t *ic, icmphdr_t *icmp, int rev)
4276 {
4277 int ictype;
4278
4279 ictype = ic->ici_type;
4280
4281 if (v == 4) {
4282 /*
4283 * If we matched its type on the way in, then when going out
4284 * it will still be the same type.
4285 */
4286 if ((!rev && (icmp->icmp_type == ictype)) ||
4287 (rev && (icmpreplytype4[ictype] == icmp->icmp_type))) {
4288 if (icmp->icmp_type != ICMP_ECHOREPLY)
4289 return (1);
4290 if (icmp->icmp_id == ic->ici_id)
4291 return (1);
4292 }
4293 }
4294 #ifdef USE_INET6
4295 else if (v == 6) {
4296 if ((!rev && (icmp->icmp_type == ictype)) ||
4297 (rev && (icmpreplytype6[ictype] == icmp->icmp_type))) {
4298 if (icmp->icmp_type != ICMP6_ECHO_REPLY)
4299 return (1);
4300 if (icmp->icmp_id == ic->ici_id)
4301 return (1);
4302 }
4303 }
4304 #endif
4305 return (0);
4306 }
4307
4308
4309 /*
4310 * IFNAMES are located in the variable length field starting at
4311 * frentry.fr_names. As pointers within the struct cannot be passed
4312 * to the kernel from ipf(8), an offset is used. An offset of -1 means it
4313 * is unused (invalid). If it is used (valid) it is an offset to the
4314 * character string of an interface name or a comment. The following
4315 * macros will assist those who follow to understand the code.
4316 */
4317 #define IPF_IFNAME_VALID(_a) (_a != -1)
4318 #define IPF_IFNAME_INVALID(_a) (_a == -1)
4319 #define IPF_IFNAMES_DIFFERENT(_a) \
4320 !((IPF_IFNAME_INVALID(fr1->_a) && \
4321 IPF_IFNAME_INVALID(fr2->_a)) || \
4322 (IPF_IFNAME_VALID(fr1->_a) && \
4323 IPF_IFNAME_VALID(fr2->_a) && \
4324 !strcmp(FR_NAME(fr1, _a), FR_NAME(fr2, _a))))
4325 #define IPF_FRDEST_DIFFERENT(_a) \
4326 (memcmp(&fr1->_a.fd_addr, &fr2->_a.fd_addr, \
4327 offsetof(frdest_t, fd_name) - offsetof(frdest_t, fd_addr)) || \
4328 IPF_IFNAMES_DIFFERENT(_a.fd_name))
4329
4330
4331 /* ------------------------------------------------------------------------ */
4332 /* Function: ipf_rule_compare */
4333 /* Parameters: fr1(I) - first rule structure to compare */
4334 /* fr2(I) - second rule structure to compare */
4335 /* Returns: int - 0 == rules are the same, else mismatch */
4336 /* */
4337 /* Compare two rules and return 0 if they match or a number indicating */
4338 /* which of the individual checks failed. */
4339 /* ------------------------------------------------------------------------ */
4340 static int
ipf_rule_compare(frentry_t * fr1,frentry_t * fr2)4341 ipf_rule_compare(frentry_t *fr1, frentry_t *fr2)
4342 {
4343 int i;
4344
4345 if (fr1->fr_cksum != fr2->fr_cksum)
4346 return (1);
4347 if (fr1->fr_size != fr2->fr_size)
4348 return (2);
4349 if (fr1->fr_dsize != fr2->fr_dsize)
4350 return (3);
4351 if (bcmp((char *)&fr1->fr_func, (char *)&fr2->fr_func, FR_CMPSIZ)
4352 != 0)
4353 return (4);
4354 /*
4355 * XXX: There is still a bug here as different rules with the
4356 * the same interfaces but in a different order will compare
4357 * differently. But since multiple interfaces in a rule doesn't
4358 * work anyway a simple straightforward compare is performed
4359 * here. Ultimately frentry_t creation will need to be
4360 * revisited in ipf_y.y. While the other issue, recognition
4361 * of only the first interface in a list of interfaces will
4362 * need to be separately addressed along with why only four.
4363 */
4364 for (i = 0; i < FR_NUM(fr1->fr_ifnames); i++) {
4365 /*
4366 * XXX: It's either the same index or uninitialized.
4367 * We assume this because multiple interfaces
4368 * referenced by the same rule doesn't work anyway.
4369 */
4370 if (IPF_IFNAMES_DIFFERENT(fr_ifnames[i]))
4371 return (5);
4372 }
4373
4374 if (IPF_FRDEST_DIFFERENT(fr_tif))
4375 return (6);
4376 if (IPF_FRDEST_DIFFERENT(fr_rif))
4377 return (7);
4378 if (IPF_FRDEST_DIFFERENT(fr_dif))
4379 return (8);
4380 if (!fr1->fr_data && !fr2->fr_data)
4381 return (0); /* move along, nothing to see here */
4382 if (fr1->fr_data && fr2->fr_data) {
4383 if (bcmp(fr1->fr_caddr, fr2->fr_caddr, fr1->fr_dsize) == 0)
4384 return (0); /* same */
4385 }
4386 return (9);
4387 }
4388
4389
4390 /* ------------------------------------------------------------------------ */
4391 /* Function: frrequest */
4392 /* Returns: int - 0 == success, > 0 == errno value */
4393 /* Parameters: unit(I) - device for which this is for */
4394 /* req(I) - ioctl command (SIOC*) */
4395 /* data(I) - pointr to ioctl data */
4396 /* set(I) - 1 or 0 (filter set) */
4397 /* makecopy(I) - flag indicating whether data points to a rule */
4398 /* in kernel space & hence doesn't need copying. */
4399 /* */
4400 /* This function handles all the requests which operate on the list of */
4401 /* filter rules. This includes adding, deleting, insertion. It is also */
4402 /* responsible for creating groups when a "head" rule is loaded. Interface */
4403 /* names are resolved here and other sanity checks are made on the content */
4404 /* of the rule structure being loaded. If a rule has user defined timeouts */
4405 /* then make sure they are created and initialised before exiting. */
4406 /* ------------------------------------------------------------------------ */
4407 int
frrequest(ipf_main_softc_t * softc,int unit,ioctlcmd_t req,caddr_t data,int set,int makecopy)4408 frrequest(ipf_main_softc_t *softc, int unit, ioctlcmd_t req, caddr_t data,
4409 int set, int makecopy)
4410 {
4411 int error = 0, in, family, need_free = 0;
4412 enum { OP_ADD, /* add rule */
4413 OP_REM, /* remove rule */
4414 OP_ZERO /* zero statistics and counters */ }
4415 addrem = OP_ADD;
4416 frentry_t frd, *fp, *f, **fprev, **ftail;
4417 void *ptr, *uptr, *cptr;
4418 u_int *p, *pp;
4419 frgroup_t *fg;
4420 char *group;
4421
4422 ptr = NULL;
4423 cptr = NULL;
4424 fg = NULL;
4425 fp = &frd;
4426 if (makecopy != 0) {
4427 bzero(fp, sizeof(frd));
4428 error = ipf_inobj(softc, data, NULL, fp, IPFOBJ_FRENTRY);
4429 if (error) {
4430 return (error);
4431 }
4432 if ((fp->fr_type & FR_T_BUILTIN) != 0) {
4433 IPFERROR(6);
4434 return (EINVAL);
4435 }
4436 KMALLOCS(f, frentry_t *, fp->fr_size);
4437 if (f == NULL) {
4438 IPFERROR(131);
4439 return (ENOMEM);
4440 }
4441 bzero(f, fp->fr_size);
4442 error = ipf_inobjsz(softc, data, f, IPFOBJ_FRENTRY,
4443 fp->fr_size);
4444 if (error) {
4445 KFREES(f, fp->fr_size);
4446 return (error);
4447 }
4448
4449 fp = f;
4450 f = NULL;
4451 fp->fr_next = NULL;
4452 fp->fr_dnext = NULL;
4453 fp->fr_pnext = NULL;
4454 fp->fr_pdnext = NULL;
4455 fp->fr_grp = NULL;
4456 fp->fr_grphead = NULL;
4457 fp->fr_icmpgrp = NULL;
4458 fp->fr_isc = (void *)-1;
4459 fp->fr_ptr = NULL;
4460 fp->fr_ref = 0;
4461 fp->fr_flags |= FR_COPIED;
4462 } else {
4463 fp = (frentry_t *)data;
4464 if ((fp->fr_type & FR_T_BUILTIN) == 0) {
4465 IPFERROR(7);
4466 return (EINVAL);
4467 }
4468 fp->fr_flags &= ~FR_COPIED;
4469 }
4470
4471 if (((fp->fr_dsize == 0) && (fp->fr_data != NULL)) ||
4472 ((fp->fr_dsize != 0) && (fp->fr_data == NULL))) {
4473 IPFERROR(8);
4474 error = EINVAL;
4475 goto donenolock;
4476 }
4477
4478 family = fp->fr_family;
4479 uptr = fp->fr_data;
4480
4481 if (req == (ioctlcmd_t)SIOCINAFR || req == (ioctlcmd_t)SIOCINIFR ||
4482 req == (ioctlcmd_t)SIOCADAFR || req == (ioctlcmd_t)SIOCADIFR)
4483 addrem = OP_ADD; /* Add rule */
4484 else if (req == (ioctlcmd_t)SIOCRMAFR || req == (ioctlcmd_t)SIOCRMIFR)
4485 addrem = OP_REM; /* Remove rule */
4486 else if (req == (ioctlcmd_t)SIOCZRLST)
4487 addrem = OP_ZERO; /* Zero statistics and counters */
4488 else {
4489 IPFERROR(9);
4490 error = EINVAL;
4491 goto donenolock;
4492 }
4493
4494 /*
4495 * Only filter rules for IPv4 or IPv6 are accepted.
4496 */
4497 if (family == AF_INET) {
4498 /*EMPTY*/;
4499 #ifdef USE_INET6
4500 } else if (family == AF_INET6) {
4501 /*EMPTY*/;
4502 #endif
4503 } else if (family != 0) {
4504 IPFERROR(10);
4505 error = EINVAL;
4506 goto donenolock;
4507 }
4508
4509 /*
4510 * If the rule is being loaded from user space, i.e. we had to copy it
4511 * into kernel space, then do not trust the function pointer in the
4512 * rule.
4513 */
4514 if ((makecopy == 1) && (fp->fr_func != NULL)) {
4515 if (ipf_findfunc(fp->fr_func) == NULL) {
4516 IPFERROR(11);
4517 error = ESRCH;
4518 goto donenolock;
4519 }
4520
4521 if (addrem == OP_ADD) {
4522 error = ipf_funcinit(softc, fp);
4523 if (error != 0)
4524 goto donenolock;
4525 }
4526 }
4527 if ((fp->fr_flags & FR_CALLNOW) &&
4528 ((fp->fr_func == NULL) || (fp->fr_func == (ipfunc_t)-1))) {
4529 IPFERROR(142);
4530 error = ESRCH;
4531 goto donenolock;
4532 }
4533 if (((fp->fr_flags & FR_CMDMASK) == FR_CALL) &&
4534 ((fp->fr_func == NULL) || (fp->fr_func == (ipfunc_t)-1))) {
4535 IPFERROR(143);
4536 error = ESRCH;
4537 goto donenolock;
4538 }
4539
4540 ptr = NULL;
4541 cptr = NULL;
4542
4543 if (FR_ISACCOUNT(fp->fr_flags))
4544 unit = IPL_LOGCOUNT;
4545
4546 /*
4547 * Check that each group name in the rule has a start index that
4548 * is valid.
4549 */
4550 if (fp->fr_icmphead != -1) {
4551 if ((fp->fr_icmphead < 0) ||
4552 (fp->fr_icmphead >= fp->fr_namelen)) {
4553 IPFERROR(136);
4554 error = EINVAL;
4555 goto donenolock;
4556 }
4557 if (!strcmp(FR_NAME(fp, fr_icmphead), "0"))
4558 fp->fr_names[fp->fr_icmphead] = '\0';
4559 }
4560
4561 if (fp->fr_grhead != -1) {
4562 if ((fp->fr_grhead < 0) ||
4563 (fp->fr_grhead >= fp->fr_namelen)) {
4564 IPFERROR(137);
4565 error = EINVAL;
4566 goto donenolock;
4567 }
4568 if (!strcmp(FR_NAME(fp, fr_grhead), "0"))
4569 fp->fr_names[fp->fr_grhead] = '\0';
4570 }
4571
4572 if (fp->fr_group != -1) {
4573 if ((fp->fr_group < 0) ||
4574 (fp->fr_group >= fp->fr_namelen)) {
4575 IPFERROR(138);
4576 error = EINVAL;
4577 goto donenolock;
4578 }
4579 if ((req != (int)SIOCZRLST) && (fp->fr_group != -1)) {
4580 /*
4581 * Allow loading rules that are in groups to cause
4582 * them to be created if they don't already exit.
4583 */
4584 group = FR_NAME(fp, fr_group);
4585 if (addrem == OP_ADD) {
4586 fg = ipf_group_add(softc, group, NULL,
4587 fp->fr_flags, unit, set);
4588 fp->fr_grp = fg;
4589 } else {
4590 fg = ipf_findgroup(softc, group, unit,
4591 set, NULL);
4592 if (fg == NULL) {
4593 IPFERROR(12);
4594 error = ESRCH;
4595 goto donenolock;
4596 }
4597 }
4598
4599 if (fg->fg_flags == 0) {
4600 fg->fg_flags = fp->fr_flags & FR_INOUT;
4601 } else if (fg->fg_flags != (fp->fr_flags & FR_INOUT)) {
4602 IPFERROR(13);
4603 error = ESRCH;
4604 goto donenolock;
4605 }
4606 }
4607 } else {
4608 /*
4609 * If a rule is going to be part of a group then it does
4610 * not matter whether it is an in or out rule, but if it
4611 * isn't in a group, then it does...
4612 */
4613 if ((fp->fr_flags & (FR_INQUE|FR_OUTQUE)) == 0) {
4614 IPFERROR(14);
4615 error = EINVAL;
4616 goto donenolock;
4617 }
4618 }
4619 in = (fp->fr_flags & FR_INQUE) ? 0 : 1;
4620
4621 /*
4622 * Work out which rule list this change is being applied to.
4623 */
4624 ftail = NULL;
4625 fprev = NULL;
4626 if (unit == IPL_LOGAUTH) {
4627 if ((fp->fr_tifs[0].fd_ptr != NULL) ||
4628 (fp->fr_tifs[1].fd_ptr != NULL) ||
4629 (fp->fr_dif.fd_ptr != NULL) ||
4630 (fp->fr_flags & FR_FASTROUTE)) {
4631 softc->ipf_interror = 145;
4632 error = EINVAL;
4633 goto donenolock;
4634 }
4635 fprev = ipf_auth_rulehead(softc);
4636 } else {
4637 if (FR_ISACCOUNT(fp->fr_flags))
4638 fprev = &softc->ipf_acct[in][set];
4639 else if ((fp->fr_flags & (FR_OUTQUE|FR_INQUE)) != 0)
4640 fprev = &softc->ipf_rules[in][set];
4641 }
4642 if (fprev == NULL) {
4643 IPFERROR(15);
4644 error = ESRCH;
4645 goto donenolock;
4646 }
4647
4648 if (fg != NULL)
4649 fprev = &fg->fg_start;
4650
4651 /*
4652 * Copy in extra data for the rule.
4653 */
4654 if (fp->fr_dsize != 0) {
4655 if (makecopy != 0) {
4656 KMALLOCS(ptr, void *, fp->fr_dsize);
4657 if (ptr == NULL) {
4658 IPFERROR(16);
4659 error = ENOMEM;
4660 goto donenolock;
4661 }
4662
4663 /*
4664 * The bcopy case is for when the data is appended
4665 * to the rule by ipf_in_compat().
4666 */
4667 if (uptr >= (void *)fp &&
4668 uptr < (void *)((char *)fp + fp->fr_size)) {
4669 bcopy(uptr, ptr, fp->fr_dsize);
4670 error = 0;
4671 } else {
4672 error = COPYIN(uptr, ptr, fp->fr_dsize);
4673 if (error != 0) {
4674 IPFERROR(17);
4675 error = EFAULT;
4676 goto donenolock;
4677 }
4678 }
4679 } else {
4680 ptr = uptr;
4681 }
4682 fp->fr_data = ptr;
4683 } else {
4684 fp->fr_data = NULL;
4685 }
4686
4687 /*
4688 * Perform per-rule type sanity checks of their members.
4689 * All code after this needs to be aware that allocated memory
4690 * may need to be free'd before exiting.
4691 */
4692 switch (fp->fr_type & ~FR_T_BUILTIN)
4693 {
4694 #if defined(IPFILTER_BPF)
4695 case FR_T_BPFOPC :
4696 if (fp->fr_dsize == 0) {
4697 IPFERROR(19);
4698 error = EINVAL;
4699 break;
4700 }
4701 if (!bpf_validate(ptr, fp->fr_dsize/sizeof(struct bpf_insn))) {
4702 IPFERROR(20);
4703 error = EINVAL;
4704 break;
4705 }
4706 break;
4707 #endif
4708 case FR_T_IPF :
4709 /*
4710 * Preparation for error case at the bottom of this function.
4711 */
4712 if (fp->fr_datype == FRI_LOOKUP)
4713 fp->fr_dstptr = NULL;
4714 if (fp->fr_satype == FRI_LOOKUP)
4715 fp->fr_srcptr = NULL;
4716
4717 if (fp->fr_dsize != sizeof(fripf_t)) {
4718 IPFERROR(21);
4719 error = EINVAL;
4720 break;
4721 }
4722
4723 /*
4724 * Allowing a rule with both "keep state" and "with oow" is
4725 * pointless because adding a state entry to the table will
4726 * fail with the out of window (oow) flag set.
4727 */
4728 if ((fp->fr_flags & FR_KEEPSTATE) && (fp->fr_flx & FI_OOW)) {
4729 IPFERROR(22);
4730 error = EINVAL;
4731 break;
4732 }
4733
4734 switch (fp->fr_satype)
4735 {
4736 case FRI_BROADCAST :
4737 case FRI_DYNAMIC :
4738 case FRI_NETWORK :
4739 case FRI_NETMASKED :
4740 case FRI_PEERADDR :
4741 if (fp->fr_sifpidx < 0) {
4742 IPFERROR(23);
4743 error = EINVAL;
4744 }
4745 break;
4746 case FRI_LOOKUP :
4747 fp->fr_srcptr = ipf_findlookup(softc, unit, fp,
4748 &fp->fr_src6,
4749 &fp->fr_smsk6);
4750 if (fp->fr_srcfunc == NULL) {
4751 IPFERROR(132);
4752 error = ESRCH;
4753 break;
4754 }
4755 break;
4756 case FRI_NORMAL :
4757 break;
4758 default :
4759 IPFERROR(133);
4760 error = EINVAL;
4761 break;
4762 }
4763 if (error != 0)
4764 break;
4765
4766 switch (fp->fr_datype)
4767 {
4768 case FRI_BROADCAST :
4769 case FRI_DYNAMIC :
4770 case FRI_NETWORK :
4771 case FRI_NETMASKED :
4772 case FRI_PEERADDR :
4773 if (fp->fr_difpidx < 0) {
4774 IPFERROR(24);
4775 error = EINVAL;
4776 }
4777 break;
4778 case FRI_LOOKUP :
4779 fp->fr_dstptr = ipf_findlookup(softc, unit, fp,
4780 &fp->fr_dst6,
4781 &fp->fr_dmsk6);
4782 if (fp->fr_dstfunc == NULL) {
4783 IPFERROR(134);
4784 error = ESRCH;
4785 }
4786 break;
4787 case FRI_NORMAL :
4788 break;
4789 default :
4790 IPFERROR(135);
4791 error = EINVAL;
4792 }
4793 break;
4794
4795 case FR_T_NONE :
4796 case FR_T_CALLFUNC :
4797 case FR_T_COMPIPF :
4798 break;
4799
4800 case FR_T_IPFEXPR :
4801 if (ipf_matcharray_verify(fp->fr_data, fp->fr_dsize) == -1) {
4802 IPFERROR(25);
4803 error = EINVAL;
4804 }
4805 break;
4806
4807 default :
4808 IPFERROR(26);
4809 error = EINVAL;
4810 break;
4811 }
4812 if (error != 0)
4813 goto donenolock;
4814
4815 if (fp->fr_tif.fd_name != -1) {
4816 if ((fp->fr_tif.fd_name < 0) ||
4817 (fp->fr_tif.fd_name >= fp->fr_namelen)) {
4818 IPFERROR(139);
4819 error = EINVAL;
4820 goto donenolock;
4821 }
4822 }
4823
4824 if (fp->fr_dif.fd_name != -1) {
4825 if ((fp->fr_dif.fd_name < 0) ||
4826 (fp->fr_dif.fd_name >= fp->fr_namelen)) {
4827 IPFERROR(140);
4828 error = EINVAL;
4829 goto donenolock;
4830 }
4831 }
4832
4833 if (fp->fr_rif.fd_name != -1) {
4834 if ((fp->fr_rif.fd_name < 0) ||
4835 (fp->fr_rif.fd_name >= fp->fr_namelen)) {
4836 IPFERROR(141);
4837 error = EINVAL;
4838 goto donenolock;
4839 }
4840 }
4841
4842 /*
4843 * Lookup all the interface names that are part of the rule.
4844 */
4845 error = ipf_synclist(softc, fp, NULL);
4846 if (error != 0)
4847 goto donenolock;
4848 fp->fr_statecnt = 0;
4849 if (fp->fr_srctrack.ht_max_nodes != 0)
4850 ipf_rb_ht_init(&fp->fr_srctrack);
4851
4852 /*
4853 * Look for an existing matching filter rule, but don't include the
4854 * next or interface pointer in the comparison (fr_next, fr_ifa).
4855 * This elminates rules which are indentical being loaded. Checksum
4856 * the constant part of the filter rule to make comparisons quicker
4857 * (this meaning no pointers are included).
4858 */
4859 pp = (u_int *)(fp->fr_caddr + fp->fr_dsize);
4860 for (fp->fr_cksum = 0, p = (u_int *)fp->fr_data; p < pp; p++)
4861 fp->fr_cksum += *p;
4862
4863 WRITE_ENTER(&softc->ipf_mutex);
4864
4865 /*
4866 * Now that the filter rule lists are locked, we can walk the
4867 * chain of them without fear.
4868 */
4869 ftail = fprev;
4870 for (f = *ftail; (f = *ftail) != NULL; ftail = &f->fr_next) {
4871 if (fp->fr_collect <= f->fr_collect) {
4872 ftail = fprev;
4873 f = NULL;
4874 break;
4875 }
4876 fprev = ftail;
4877 }
4878
4879 for (; (f = *ftail) != NULL; ftail = &f->fr_next) {
4880 if (ipf_rule_compare(fp, f) == 0)
4881 break;
4882 }
4883
4884 /*
4885 * If zero'ing statistics, copy current to caller and zero.
4886 */
4887 if (addrem == OP_ZERO) {
4888 if (f == NULL) {
4889 IPFERROR(27);
4890 error = ESRCH;
4891 } else {
4892 /*
4893 * Copy and reduce lock because of impending copyout.
4894 * Well we should, but if we do then the atomicity of
4895 * this call and the correctness of fr_hits and
4896 * fr_bytes cannot be guaranteed. As it is, this code
4897 * only resets them to 0 if they are successfully
4898 * copied out into user space.
4899 */
4900 bcopy((char *)f, (char *)fp, f->fr_size);
4901 /* MUTEX_DOWNGRADE(&softc->ipf_mutex); */
4902
4903 /*
4904 * When we copy this rule back out, set the data
4905 * pointer to be what it was in user space.
4906 */
4907 fp->fr_data = uptr;
4908 error = ipf_outobj(softc, data, fp, IPFOBJ_FRENTRY);
4909
4910 if (error == 0) {
4911 if ((f->fr_dsize != 0) && (uptr != NULL)) {
4912 error = COPYOUT(f->fr_data, uptr,
4913 f->fr_dsize);
4914 if (error == 0) {
4915 f->fr_hits = 0;
4916 f->fr_bytes = 0;
4917 } else {
4918 IPFERROR(28);
4919 error = EFAULT;
4920 }
4921 }
4922 }
4923 }
4924
4925 if (makecopy != 0) {
4926 if (ptr != NULL) {
4927 KFREES(ptr, fp->fr_dsize);
4928 }
4929 KFREES(fp, fp->fr_size);
4930 }
4931 RWLOCK_EXIT(&softc->ipf_mutex);
4932 return (error);
4933 }
4934
4935 if (f == NULL) {
4936 /*
4937 * At the end of this, ftail must point to the place where the
4938 * new rule is to be saved/inserted/added.
4939 * For SIOCAD*FR, this should be the last rule in the group of
4940 * rules that have equal fr_collect fields.
4941 * For SIOCIN*FR, ...
4942 */
4943 if (req == (ioctlcmd_t)SIOCADAFR ||
4944 req == (ioctlcmd_t)SIOCADIFR) {
4945
4946 for (ftail = fprev; (f = *ftail) != NULL; ) {
4947 if (f->fr_collect > fp->fr_collect)
4948 break;
4949 ftail = &f->fr_next;
4950 fprev = ftail;
4951 }
4952 ftail = fprev;
4953 f = NULL;
4954 ptr = NULL;
4955 } else if (req == (ioctlcmd_t)SIOCINAFR ||
4956 req == (ioctlcmd_t)SIOCINIFR) {
4957 while ((f = *fprev) != NULL) {
4958 if (f->fr_collect >= fp->fr_collect)
4959 break;
4960 fprev = &f->fr_next;
4961 }
4962 ftail = fprev;
4963 if (fp->fr_hits != 0) {
4964 while (fp->fr_hits && (f = *ftail)) {
4965 if (f->fr_collect != fp->fr_collect)
4966 break;
4967 fprev = ftail;
4968 ftail = &f->fr_next;
4969 fp->fr_hits--;
4970 }
4971 }
4972 f = NULL;
4973 ptr = NULL;
4974 }
4975 }
4976
4977 /*
4978 * Request to remove a rule.
4979 */
4980 if (addrem == OP_REM) {
4981 if (f == NULL) {
4982 IPFERROR(29);
4983 error = ESRCH;
4984 } else {
4985 /*
4986 * Do not allow activity from user space to interfere
4987 * with rules not loaded that way.
4988 */
4989 if ((makecopy == 1) && !(f->fr_flags & FR_COPIED)) {
4990 IPFERROR(30);
4991 error = EPERM;
4992 goto done;
4993 }
4994
4995 /*
4996 * Return EBUSY if the rule is being reference by
4997 * something else (eg state information.)
4998 */
4999 if (f->fr_ref > 1) {
5000 IPFERROR(31);
5001 error = EBUSY;
5002 goto done;
5003 }
5004 #ifdef IPFILTER_SCAN
5005 if (f->fr_isctag != -1 &&
5006 (f->fr_isc != (struct ipscan *)-1))
5007 ipf_scan_detachfr(f);
5008 #endif
5009
5010 if (unit == IPL_LOGAUTH) {
5011 error = ipf_auth_precmd(softc, req, f, ftail);
5012 goto done;
5013 }
5014
5015 ipf_rule_delete(softc, f, unit, set);
5016
5017 need_free = makecopy;
5018 }
5019 } else {
5020 /*
5021 * Not removing, so we must be adding/inserting a rule.
5022 */
5023 if (f != NULL) {
5024 IPFERROR(32);
5025 error = EEXIST;
5026 goto done;
5027 }
5028 if (unit == IPL_LOGAUTH) {
5029 error = ipf_auth_precmd(softc, req, fp, ftail);
5030 goto done;
5031 }
5032
5033 MUTEX_NUKE(&fp->fr_lock);
5034 MUTEX_INIT(&fp->fr_lock, "filter rule lock");
5035 if (fp->fr_die != 0)
5036 ipf_rule_expire_insert(softc, fp, set);
5037
5038 fp->fr_hits = 0;
5039 if (makecopy != 0)
5040 fp->fr_ref = 1;
5041 fp->fr_pnext = ftail;
5042 fp->fr_next = *ftail;
5043 if (fp->fr_next != NULL)
5044 fp->fr_next->fr_pnext = &fp->fr_next;
5045 *ftail = fp;
5046 ipf_fixskip(ftail, fp, 1);
5047
5048 fp->fr_icmpgrp = NULL;
5049 if (fp->fr_icmphead != -1) {
5050 group = FR_NAME(fp, fr_icmphead);
5051 fg = ipf_group_add(softc, group, fp, 0, unit, set);
5052 fp->fr_icmpgrp = fg;
5053 }
5054
5055 fp->fr_grphead = NULL;
5056 if (fp->fr_grhead != -1) {
5057 group = FR_NAME(fp, fr_grhead);
5058 fg = ipf_group_add(softc, group, fp, fp->fr_flags,
5059 unit, set);
5060 fp->fr_grphead = fg;
5061 }
5062 }
5063 done:
5064 RWLOCK_EXIT(&softc->ipf_mutex);
5065 donenolock:
5066 if (need_free || (error != 0)) {
5067 if ((fp->fr_type & ~FR_T_BUILTIN) == FR_T_IPF) {
5068 if ((fp->fr_satype == FRI_LOOKUP) &&
5069 (fp->fr_srcptr != NULL))
5070 ipf_lookup_deref(softc, fp->fr_srctype,
5071 fp->fr_srcptr);
5072 if ((fp->fr_datype == FRI_LOOKUP) &&
5073 (fp->fr_dstptr != NULL))
5074 ipf_lookup_deref(softc, fp->fr_dsttype,
5075 fp->fr_dstptr);
5076 }
5077 if (fp->fr_grp != NULL) {
5078 WRITE_ENTER(&softc->ipf_mutex);
5079 ipf_group_del(softc, fp->fr_grp, fp);
5080 RWLOCK_EXIT(&softc->ipf_mutex);
5081 }
5082 if ((ptr != NULL) && (makecopy != 0)) {
5083 KFREES(ptr, fp->fr_dsize);
5084 }
5085 KFREES(fp, fp->fr_size);
5086 }
5087 return (error);
5088 }
5089
5090
5091 /* ------------------------------------------------------------------------ */
5092 /* Function: ipf_rule_delete */
5093 /* Returns: Nil */
5094 /* Parameters: softc(I) - pointer to soft context main structure */
5095 /* f(I) - pointer to the rule being deleted */
5096 /* ftail(I) - pointer to the pointer to f */
5097 /* unit(I) - device for which this is for */
5098 /* set(I) - 1 or 0 (filter set) */
5099 /* */
5100 /* This function attempts to do what it can to delete a filter rule: remove */
5101 /* it from any linked lists and remove any groups it is responsible for. */
5102 /* But in the end, removing a rule can only drop the reference count - we */
5103 /* must use that as the guide for whether or not it can be freed. */
5104 /* ------------------------------------------------------------------------ */
5105 static void
ipf_rule_delete(ipf_main_softc_t * softc,frentry_t * f,int unit,int set)5106 ipf_rule_delete(ipf_main_softc_t *softc, frentry_t *f, int unit, int set)
5107 {
5108
5109 /*
5110 * If fr_pdnext is set, then the rule is on the expire list, so
5111 * remove it from there.
5112 */
5113 if (f->fr_pdnext != NULL) {
5114 *f->fr_pdnext = f->fr_dnext;
5115 if (f->fr_dnext != NULL)
5116 f->fr_dnext->fr_pdnext = f->fr_pdnext;
5117 f->fr_pdnext = NULL;
5118 f->fr_dnext = NULL;
5119 }
5120
5121 ipf_fixskip(f->fr_pnext, f, -1);
5122 if (f->fr_pnext != NULL)
5123 *f->fr_pnext = f->fr_next;
5124 if (f->fr_next != NULL)
5125 f->fr_next->fr_pnext = f->fr_pnext;
5126 f->fr_pnext = NULL;
5127 f->fr_next = NULL;
5128
5129 (void) ipf_derefrule(softc, &f);
5130 }
5131
5132 /* ------------------------------------------------------------------------ */
5133 /* Function: ipf_rule_expire_insert */
5134 /* Returns: Nil */
5135 /* Parameters: softc(I) - pointer to soft context main structure */
5136 /* f(I) - pointer to rule to be added to expire list */
5137 /* set(I) - 1 or 0 (filter set) */
5138 /* */
5139 /* If the new rule has a given expiration time, insert it into the list of */
5140 /* expiring rules with the ones to be removed first added to the front of */
5141 /* the list. The insertion is O(n) but it is kept sorted for quick scans at */
5142 /* expiration interval checks. */
5143 /* ------------------------------------------------------------------------ */
5144 static void
ipf_rule_expire_insert(ipf_main_softc_t * softc,frentry_t * f,int set)5145 ipf_rule_expire_insert(ipf_main_softc_t *softc, frentry_t *f, int set)
5146 {
5147 frentry_t *fr;
5148
5149 /*
5150 */
5151
5152 f->fr_die = softc->ipf_ticks + IPF_TTLVAL(f->fr_die);
5153 for (fr = softc->ipf_rule_explist[set]; fr != NULL;
5154 fr = fr->fr_dnext) {
5155 if (f->fr_die < fr->fr_die)
5156 break;
5157 if (fr->fr_dnext == NULL) {
5158 /*
5159 * We've got to the last rule and everything
5160 * wanted to be expired before this new node,
5161 * so we have to tack it on the end...
5162 */
5163 fr->fr_dnext = f;
5164 f->fr_pdnext = &fr->fr_dnext;
5165 fr = NULL;
5166 break;
5167 }
5168 }
5169
5170 if (softc->ipf_rule_explist[set] == NULL) {
5171 softc->ipf_rule_explist[set] = f;
5172 f->fr_pdnext = &softc->ipf_rule_explist[set];
5173 } else if (fr != NULL) {
5174 f->fr_dnext = fr;
5175 f->fr_pdnext = fr->fr_pdnext;
5176 fr->fr_pdnext = &f->fr_dnext;
5177 }
5178 }
5179
5180
5181 /* ------------------------------------------------------------------------ */
5182 /* Function: ipf_findlookup */
5183 /* Returns: NULL = failure, else success */
5184 /* Parameters: softc(I) - pointer to soft context main structure */
5185 /* unit(I) - ipf device we want to find match for */
5186 /* fp(I) - rule for which lookup is for */
5187 /* addrp(I) - pointer to lookup information in address struct */
5188 /* maskp(O) - pointer to lookup information for storage */
5189 /* */
5190 /* When using pools and hash tables to store addresses for matching in */
5191 /* rules, it is necessary to resolve both the object referred to by the */
5192 /* name or address (and return that pointer) and also provide the means by */
5193 /* which to determine if an address belongs to that object to make the */
5194 /* packet matching quicker. */
5195 /* ------------------------------------------------------------------------ */
5196 static void *
ipf_findlookup(ipf_main_softc_t * softc,int unit,frentry_t * fr,i6addr_t * addrp,i6addr_t * maskp)5197 ipf_findlookup(ipf_main_softc_t *softc, int unit, frentry_t *fr,
5198 i6addr_t *addrp, i6addr_t *maskp)
5199 {
5200 void *ptr = NULL;
5201
5202 switch (addrp->iplookupsubtype)
5203 {
5204 case 0 :
5205 ptr = ipf_lookup_res_num(softc, unit, addrp->iplookuptype,
5206 addrp->iplookupnum,
5207 &maskp->iplookupfunc);
5208 break;
5209 case 1 :
5210 if (addrp->iplookupname < 0)
5211 break;
5212 if (addrp->iplookupname >= fr->fr_namelen)
5213 break;
5214 ptr = ipf_lookup_res_name(softc, unit, addrp->iplookuptype,
5215 fr->fr_names + addrp->iplookupname,
5216 &maskp->iplookupfunc);
5217 break;
5218 default :
5219 break;
5220 }
5221
5222 return (ptr);
5223 }
5224
5225
5226 /* ------------------------------------------------------------------------ */
5227 /* Function: ipf_funcinit */
5228 /* Returns: int - 0 == success, else ESRCH: cannot resolve rule details */
5229 /* Parameters: softc(I) - pointer to soft context main structure */
5230 /* fr(I) - pointer to filter rule */
5231 /* */
5232 /* If a rule is a call rule, then check if the function it points to needs */
5233 /* an init function to be called now the rule has been loaded. */
5234 /* ------------------------------------------------------------------------ */
5235 static int
ipf_funcinit(ipf_main_softc_t * softc,frentry_t * fr)5236 ipf_funcinit(ipf_main_softc_t *softc, frentry_t *fr)
5237 {
5238 ipfunc_resolve_t *ft;
5239 int err;
5240
5241 IPFERROR(34);
5242 err = ESRCH;
5243
5244 for (ft = ipf_availfuncs; ft->ipfu_addr != NULL; ft++)
5245 if (ft->ipfu_addr == fr->fr_func) {
5246 err = 0;
5247 if (ft->ipfu_init != NULL)
5248 err = (*ft->ipfu_init)(softc, fr);
5249 break;
5250 }
5251 return (err);
5252 }
5253
5254
5255 /* ------------------------------------------------------------------------ */
5256 /* Function: ipf_funcfini */
5257 /* Returns: Nil */
5258 /* Parameters: softc(I) - pointer to soft context main structure */
5259 /* fr(I) - pointer to filter rule */
5260 /* */
5261 /* For a given filter rule, call the matching "fini" function if the rule */
5262 /* is using a known function that would have resulted in the "init" being */
5263 /* called for ealier. */
5264 /* ------------------------------------------------------------------------ */
5265 static void
ipf_funcfini(ipf_main_softc_t * softc,frentry_t * fr)5266 ipf_funcfini(ipf_main_softc_t *softc, frentry_t *fr)
5267 {
5268 ipfunc_resolve_t *ft;
5269
5270 for (ft = ipf_availfuncs; ft->ipfu_addr != NULL; ft++)
5271 if (ft->ipfu_addr == fr->fr_func) {
5272 if (ft->ipfu_fini != NULL)
5273 (void) (*ft->ipfu_fini)(softc, fr);
5274 break;
5275 }
5276 }
5277
5278
5279 /* ------------------------------------------------------------------------ */
5280 /* Function: ipf_findfunc */
5281 /* Returns: ipfunc_t - pointer to function if found, else NULL */
5282 /* Parameters: funcptr(I) - function pointer to lookup */
5283 /* */
5284 /* Look for a function in the table of known functions. */
5285 /* ------------------------------------------------------------------------ */
5286 static ipfunc_t
ipf_findfunc(ipfunc_t funcptr)5287 ipf_findfunc(ipfunc_t funcptr)
5288 {
5289 ipfunc_resolve_t *ft;
5290
5291 for (ft = ipf_availfuncs; ft->ipfu_addr != NULL; ft++)
5292 if (ft->ipfu_addr == funcptr)
5293 return (funcptr);
5294 return (NULL);
5295 }
5296
5297
5298 /* ------------------------------------------------------------------------ */
5299 /* Function: ipf_resolvefunc */
5300 /* Returns: int - 0 == success, else error */
5301 /* Parameters: data(IO) - ioctl data pointer to ipfunc_resolve_t struct */
5302 /* */
5303 /* Copy in a ipfunc_resolve_t structure and then fill in the missing field. */
5304 /* This will either be the function name (if the pointer is set) or the */
5305 /* function pointer if the name is set. When found, fill in the other one */
5306 /* so that the entire, complete, structure can be copied back to user space.*/
5307 /* ------------------------------------------------------------------------ */
5308 int
ipf_resolvefunc(ipf_main_softc_t * softc,void * data)5309 ipf_resolvefunc(ipf_main_softc_t *softc, void *data)
5310 {
5311 ipfunc_resolve_t res, *ft;
5312 int error;
5313
5314 error = BCOPYIN(data, &res, sizeof(res));
5315 if (error != 0) {
5316 IPFERROR(123);
5317 return (EFAULT);
5318 }
5319
5320 if (res.ipfu_addr == NULL && res.ipfu_name[0] != '\0') {
5321 for (ft = ipf_availfuncs; ft->ipfu_addr != NULL; ft++)
5322 if (strncmp(res.ipfu_name, ft->ipfu_name,
5323 sizeof(res.ipfu_name)) == 0) {
5324 res.ipfu_addr = ft->ipfu_addr;
5325 res.ipfu_init = ft->ipfu_init;
5326 if (COPYOUT(&res, data, sizeof(res)) != 0) {
5327 IPFERROR(35);
5328 return (EFAULT);
5329 }
5330 return (0);
5331 }
5332 }
5333 if (res.ipfu_addr != NULL && res.ipfu_name[0] == '\0') {
5334 for (ft = ipf_availfuncs; ft->ipfu_addr != NULL; ft++)
5335 if (ft->ipfu_addr == res.ipfu_addr) {
5336 (void) strncpy(res.ipfu_name, ft->ipfu_name,
5337 sizeof(res.ipfu_name));
5338 res.ipfu_init = ft->ipfu_init;
5339 if (COPYOUT(&res, data, sizeof(res)) != 0) {
5340 IPFERROR(36);
5341 return (EFAULT);
5342 }
5343 return (0);
5344 }
5345 }
5346 IPFERROR(37);
5347 return (ESRCH);
5348 }
5349
5350
5351 #if !defined(_KERNEL) || SOLARIS
5352 /*
5353 * From: NetBSD
5354 * ppsratecheck(): packets (or events) per second limitation.
5355 */
5356 int
ppsratecheck(struct timeval * lasttime,int * curpps,int maxpps)5357 ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps)
5358 /* maxpps: maximum pps allowed */
5359 {
5360 struct timeval tv, delta;
5361 int rv;
5362
5363 GETKTIME(&tv);
5364
5365 delta.tv_sec = tv.tv_sec - lasttime->tv_sec;
5366 delta.tv_usec = tv.tv_usec - lasttime->tv_usec;
5367 if (delta.tv_usec < 0) {
5368 delta.tv_sec--;
5369 delta.tv_usec += 1000000;
5370 }
5371
5372 /*
5373 * check for 0,0 is so that the message will be seen at least once.
5374 * if more than one second have passed since the last update of
5375 * lasttime, reset the counter.
5376 *
5377 * we do increment *curpps even in *curpps < maxpps case, as some may
5378 * try to use *curpps for stat purposes as well.
5379 */
5380 if ((lasttime->tv_sec == 0 && lasttime->tv_usec == 0) ||
5381 delta.tv_sec >= 1) {
5382 *lasttime = tv;
5383 *curpps = 0;
5384 rv = 1;
5385 } else if (maxpps < 0)
5386 rv = 1;
5387 else if (*curpps < maxpps)
5388 rv = 1;
5389 else
5390 rv = 0;
5391 *curpps = *curpps + 1;
5392
5393 return (rv);
5394 }
5395 #endif
5396
5397
5398 /* ------------------------------------------------------------------------ */
5399 /* Function: ipf_derefrule */
5400 /* Returns: int - 0 == rule freed up, else rule not freed */
5401 /* Parameters: fr(I) - pointer to filter rule */
5402 /* */
5403 /* Decrement the reference counter to a rule by one. If it reaches zero, */
5404 /* free it and any associated storage space being used by it. */
5405 /* ------------------------------------------------------------------------ */
5406 int
ipf_derefrule(ipf_main_softc_t * softc,frentry_t ** frp)5407 ipf_derefrule(ipf_main_softc_t *softc, frentry_t **frp)
5408 {
5409 frentry_t *fr;
5410 frdest_t *fdp;
5411
5412 fr = *frp;
5413 *frp = NULL;
5414
5415 MUTEX_ENTER(&fr->fr_lock);
5416 fr->fr_ref--;
5417 if (fr->fr_ref == 0) {
5418 MUTEX_EXIT(&fr->fr_lock);
5419 MUTEX_DESTROY(&fr->fr_lock);
5420
5421 ipf_funcfini(softc, fr);
5422
5423 fdp = &fr->fr_tif;
5424 if (fdp->fd_type == FRD_DSTLIST)
5425 ipf_lookup_deref(softc, IPLT_DSTLIST, fdp->fd_ptr);
5426
5427 fdp = &fr->fr_rif;
5428 if (fdp->fd_type == FRD_DSTLIST)
5429 ipf_lookup_deref(softc, IPLT_DSTLIST, fdp->fd_ptr);
5430
5431 fdp = &fr->fr_dif;
5432 if (fdp->fd_type == FRD_DSTLIST)
5433 ipf_lookup_deref(softc, IPLT_DSTLIST, fdp->fd_ptr);
5434
5435 if ((fr->fr_type & ~FR_T_BUILTIN) == FR_T_IPF &&
5436 fr->fr_satype == FRI_LOOKUP)
5437 ipf_lookup_deref(softc, fr->fr_srctype, fr->fr_srcptr);
5438 if ((fr->fr_type & ~FR_T_BUILTIN) == FR_T_IPF &&
5439 fr->fr_datype == FRI_LOOKUP)
5440 ipf_lookup_deref(softc, fr->fr_dsttype, fr->fr_dstptr);
5441
5442 if (fr->fr_grp != NULL)
5443 ipf_group_del(softc, fr->fr_grp, fr);
5444
5445 if (fr->fr_grphead != NULL)
5446 ipf_group_del(softc, fr->fr_grphead, fr);
5447
5448 if (fr->fr_icmpgrp != NULL)
5449 ipf_group_del(softc, fr->fr_icmpgrp, fr);
5450
5451 if ((fr->fr_flags & FR_COPIED) != 0) {
5452 if (fr->fr_dsize) {
5453 KFREES(fr->fr_data, fr->fr_dsize);
5454 }
5455 KFREES(fr, fr->fr_size);
5456 return (0);
5457 }
5458 return (1);
5459 } else {
5460 MUTEX_EXIT(&fr->fr_lock);
5461 }
5462 return (-1);
5463 }
5464
5465
5466 /* ------------------------------------------------------------------------ */
5467 /* Function: ipf_grpmapinit */
5468 /* Returns: int - 0 == success, else ESRCH because table entry not found*/
5469 /* Parameters: fr(I) - pointer to rule to find hash table for */
5470 /* */
5471 /* Looks for group hash table fr_arg and stores a pointer to it in fr_ptr. */
5472 /* fr_ptr is later used by ipf_srcgrpmap and ipf_dstgrpmap. */
5473 /* ------------------------------------------------------------------------ */
5474 static int
ipf_grpmapinit(ipf_main_softc_t * softc,frentry_t * fr)5475 ipf_grpmapinit(ipf_main_softc_t *softc, frentry_t *fr)
5476 {
5477 char name[FR_GROUPLEN];
5478 iphtable_t *iph;
5479
5480 (void) snprintf(name, sizeof(name), "%d", fr->fr_arg);
5481 iph = ipf_lookup_find_htable(softc, IPL_LOGIPF, name);
5482 if (iph == NULL) {
5483 IPFERROR(38);
5484 return (ESRCH);
5485 }
5486 if ((iph->iph_flags & FR_INOUT) != (fr->fr_flags & FR_INOUT)) {
5487 IPFERROR(39);
5488 return (ESRCH);
5489 }
5490 iph->iph_ref++;
5491 fr->fr_ptr = iph;
5492 return (0);
5493 }
5494
5495
5496 /* ------------------------------------------------------------------------ */
5497 /* Function: ipf_grpmapfini */
5498 /* Returns: int - 0 == success, else ESRCH because table entry not found*/
5499 /* Parameters: softc(I) - pointer to soft context main structure */
5500 /* fr(I) - pointer to rule to release hash table for */
5501 /* */
5502 /* For rules that have had ipf_grpmapinit called, ipf_lookup_deref needs to */
5503 /* be called to undo what ipf_grpmapinit caused to be done. */
5504 /* ------------------------------------------------------------------------ */
5505 static int
ipf_grpmapfini(ipf_main_softc_t * softc,frentry_t * fr)5506 ipf_grpmapfini(ipf_main_softc_t *softc, frentry_t *fr)
5507 {
5508 iphtable_t *iph;
5509 iph = fr->fr_ptr;
5510 if (iph != NULL)
5511 ipf_lookup_deref(softc, IPLT_HASH, iph);
5512 return (0);
5513 }
5514
5515
5516 /* ------------------------------------------------------------------------ */
5517 /* Function: ipf_srcgrpmap */
5518 /* Returns: frentry_t * - pointer to "new last matching" rule or NULL */
5519 /* Parameters: fin(I) - pointer to packet information */
5520 /* passp(IO) - pointer to current/new filter decision (unused) */
5521 /* */
5522 /* Look for a rule group head in a hash table, using the source address as */
5523 /* the key, and descend into that group and continue matching rules against */
5524 /* the packet. */
5525 /* ------------------------------------------------------------------------ */
5526 frentry_t *
ipf_srcgrpmap(fr_info_t * fin,u_32_t * passp)5527 ipf_srcgrpmap(fr_info_t *fin, u_32_t *passp)
5528 {
5529 frgroup_t *fg;
5530 void *rval;
5531
5532 rval = ipf_iphmfindgroup(fin->fin_main_soft, fin->fin_fr->fr_ptr,
5533 &fin->fin_src);
5534 if (rval == NULL)
5535 return (NULL);
5536
5537 fg = rval;
5538 fin->fin_fr = fg->fg_start;
5539 (void) ipf_scanlist(fin, *passp);
5540 return (fin->fin_fr);
5541 }
5542
5543
5544 /* ------------------------------------------------------------------------ */
5545 /* Function: ipf_dstgrpmap */
5546 /* Returns: frentry_t * - pointer to "new last matching" rule or NULL */
5547 /* Parameters: fin(I) - pointer to packet information */
5548 /* passp(IO) - pointer to current/new filter decision (unused) */
5549 /* */
5550 /* Look for a rule group head in a hash table, using the destination */
5551 /* address as the key, and descend into that group and continue matching */
5552 /* rules against the packet. */
5553 /* ------------------------------------------------------------------------ */
5554 frentry_t *
ipf_dstgrpmap(fr_info_t * fin,u_32_t * passp)5555 ipf_dstgrpmap(fr_info_t *fin, u_32_t *passp)
5556 {
5557 frgroup_t *fg;
5558 void *rval;
5559
5560 rval = ipf_iphmfindgroup(fin->fin_main_soft, fin->fin_fr->fr_ptr,
5561 &fin->fin_dst);
5562 if (rval == NULL)
5563 return (NULL);
5564
5565 fg = rval;
5566 fin->fin_fr = fg->fg_start;
5567 (void) ipf_scanlist(fin, *passp);
5568 return (fin->fin_fr);
5569 }
5570
5571 /*
5572 * Queue functions
5573 * ===============
5574 * These functions manage objects on queues for efficient timeouts. There
5575 * are a number of system defined queues as well as user defined timeouts.
5576 * It is expected that a lock is held in the domain in which the queue
5577 * belongs (i.e. either state or NAT) when calling any of these functions
5578 * that prevents ipf_freetimeoutqueue() from being called at the same time
5579 * as any other.
5580 */
5581
5582
5583 /* ------------------------------------------------------------------------ */
5584 /* Function: ipf_addtimeoutqueue */
5585 /* Returns: struct ifqtq * - NULL if malloc fails, else pointer to */
5586 /* timeout queue with given interval. */
5587 /* Parameters: parent(I) - pointer to pointer to parent node of this list */
5588 /* of interface queues. */
5589 /* seconds(I) - timeout value in seconds for this queue. */
5590 /* */
5591 /* This routine first looks for a timeout queue that matches the interval */
5592 /* being requested. If it finds one, increments the reference counter and */
5593 /* returns a pointer to it. If none are found, it allocates a new one and */
5594 /* inserts it at the top of the list. */
5595 /* */
5596 /* Locking. */
5597 /* It is assumed that the caller of this function has an appropriate lock */
5598 /* held (exclusively) in the domain that encompases 'parent'. */
5599 /* ------------------------------------------------------------------------ */
5600 ipftq_t *
ipf_addtimeoutqueue(ipf_main_softc_t * softc,ipftq_t ** parent,u_int seconds)5601 ipf_addtimeoutqueue(ipf_main_softc_t *softc, ipftq_t **parent, u_int seconds)
5602 {
5603 ipftq_t *ifq;
5604 u_int period;
5605
5606 period = seconds * IPF_HZ_DIVIDE;
5607
5608 MUTEX_ENTER(&softc->ipf_timeoutlock);
5609 for (ifq = *parent; ifq != NULL; ifq = ifq->ifq_next) {
5610 if (ifq->ifq_ttl == period) {
5611 /*
5612 * Reset the delete flag, if set, so the structure
5613 * gets reused rather than freed and reallocated.
5614 */
5615 MUTEX_ENTER(&ifq->ifq_lock);
5616 ifq->ifq_flags &= ~IFQF_DELETE;
5617 ifq->ifq_ref++;
5618 MUTEX_EXIT(&ifq->ifq_lock);
5619 MUTEX_EXIT(&softc->ipf_timeoutlock);
5620
5621 return (ifq);
5622 }
5623 }
5624
5625 KMALLOC(ifq, ipftq_t *);
5626 if (ifq != NULL) {
5627 MUTEX_NUKE(&ifq->ifq_lock);
5628 IPFTQ_INIT(ifq, period, "ipftq mutex");
5629 ifq->ifq_next = *parent;
5630 ifq->ifq_pnext = parent;
5631 ifq->ifq_flags = IFQF_USER;
5632 ifq->ifq_ref++;
5633 *parent = ifq;
5634 softc->ipf_userifqs++;
5635 }
5636 MUTEX_EXIT(&softc->ipf_timeoutlock);
5637 return (ifq);
5638 }
5639
5640
5641 /* ------------------------------------------------------------------------ */
5642 /* Function: ipf_deletetimeoutqueue */
5643 /* Returns: int - new reference count value of the timeout queue */
5644 /* Parameters: ifq(I) - timeout queue which is losing a reference. */
5645 /* Locks: ifq->ifq_lock */
5646 /* */
5647 /* This routine must be called when we're discarding a pointer to a timeout */
5648 /* queue object, taking care of the reference counter. */
5649 /* */
5650 /* Now that this just sets a DELETE flag, it requires the expire code to */
5651 /* check the list of user defined timeout queues and call the free function */
5652 /* below (currently commented out) to stop memory leaking. It is done this */
5653 /* way because the locking may not be sufficient to safely do a free when */
5654 /* this function is called. */
5655 /* ------------------------------------------------------------------------ */
5656 int
ipf_deletetimeoutqueue(ipftq_t * ifq)5657 ipf_deletetimeoutqueue(ipftq_t *ifq)
5658 {
5659
5660 ifq->ifq_ref--;
5661 if ((ifq->ifq_ref == 0) && ((ifq->ifq_flags & IFQF_USER) != 0)) {
5662 ifq->ifq_flags |= IFQF_DELETE;
5663 }
5664
5665 return (ifq->ifq_ref);
5666 }
5667
5668
5669 /* ------------------------------------------------------------------------ */
5670 /* Function: ipf_freetimeoutqueue */
5671 /* Parameters: ifq(I) - timeout queue which is losing a reference. */
5672 /* Returns: Nil */
5673 /* */
5674 /* Locking: */
5675 /* It is assumed that the caller of this function has an appropriate lock */
5676 /* held (exclusively) in the domain that encompases the callers "domain". */
5677 /* The ifq_lock for this structure should not be held. */
5678 /* */
5679 /* Remove a user defined timeout queue from the list of queues it is in and */
5680 /* tidy up after this is done. */
5681 /* ------------------------------------------------------------------------ */
5682 void
ipf_freetimeoutqueue(ipf_main_softc_t * softc,ipftq_t * ifq)5683 ipf_freetimeoutqueue(ipf_main_softc_t *softc, ipftq_t *ifq)
5684 {
5685
5686 if (((ifq->ifq_flags & IFQF_DELETE) == 0) || (ifq->ifq_ref != 0) ||
5687 ((ifq->ifq_flags & IFQF_USER) == 0)) {
5688 printf("ipf_freetimeoutqueue(%lx) flags 0x%x ttl %d ref %d\n",
5689 (u_long)ifq, ifq->ifq_flags, ifq->ifq_ttl,
5690 ifq->ifq_ref);
5691 return;
5692 }
5693
5694 /*
5695 * Remove from its position in the list.
5696 */
5697 *ifq->ifq_pnext = ifq->ifq_next;
5698 if (ifq->ifq_next != NULL)
5699 ifq->ifq_next->ifq_pnext = ifq->ifq_pnext;
5700 ifq->ifq_next = NULL;
5701 ifq->ifq_pnext = NULL;
5702
5703 MUTEX_DESTROY(&ifq->ifq_lock);
5704 ATOMIC_DEC(softc->ipf_userifqs);
5705 KFREE(ifq);
5706 }
5707
5708
5709 /* ------------------------------------------------------------------------ */
5710 /* Function: ipf_deletequeueentry */
5711 /* Returns: Nil */
5712 /* Parameters: tqe(I) - timeout queue entry to delete */
5713 /* */
5714 /* Remove a tail queue entry from its queue and make it an orphan. */
5715 /* ipf_deletetimeoutqueue is called to make sure the reference count on the */
5716 /* queue is correct. We can't, however, call ipf_freetimeoutqueue because */
5717 /* the correct lock(s) may not be held that would make it safe to do so. */
5718 /* ------------------------------------------------------------------------ */
5719 void
ipf_deletequeueentry(ipftqent_t * tqe)5720 ipf_deletequeueentry(ipftqent_t *tqe)
5721 {
5722 ipftq_t *ifq;
5723
5724 ifq = tqe->tqe_ifq;
5725
5726 MUTEX_ENTER(&ifq->ifq_lock);
5727
5728 if (tqe->tqe_pnext != NULL) {
5729 *tqe->tqe_pnext = tqe->tqe_next;
5730 if (tqe->tqe_next != NULL)
5731 tqe->tqe_next->tqe_pnext = tqe->tqe_pnext;
5732 else /* we must be the tail anyway */
5733 ifq->ifq_tail = tqe->tqe_pnext;
5734
5735 tqe->tqe_pnext = NULL;
5736 tqe->tqe_ifq = NULL;
5737 }
5738
5739 (void) ipf_deletetimeoutqueue(ifq);
5740 ASSERT(ifq->ifq_ref > 0);
5741
5742 MUTEX_EXIT(&ifq->ifq_lock);
5743 }
5744
5745
5746 /* ------------------------------------------------------------------------ */
5747 /* Function: ipf_queuefront */
5748 /* Returns: Nil */
5749 /* Parameters: tqe(I) - pointer to timeout queue entry */
5750 /* */
5751 /* Move a queue entry to the front of the queue, if it isn't already there. */
5752 /* ------------------------------------------------------------------------ */
5753 void
ipf_queuefront(ipftqent_t * tqe)5754 ipf_queuefront(ipftqent_t *tqe)
5755 {
5756 ipftq_t *ifq;
5757
5758 ifq = tqe->tqe_ifq;
5759 if (ifq == NULL)
5760 return;
5761
5762 MUTEX_ENTER(&ifq->ifq_lock);
5763 if (ifq->ifq_head != tqe) {
5764 *tqe->tqe_pnext = tqe->tqe_next;
5765 if (tqe->tqe_next)
5766 tqe->tqe_next->tqe_pnext = tqe->tqe_pnext;
5767 else
5768 ifq->ifq_tail = tqe->tqe_pnext;
5769
5770 tqe->tqe_next = ifq->ifq_head;
5771 ifq->ifq_head->tqe_pnext = &tqe->tqe_next;
5772 ifq->ifq_head = tqe;
5773 tqe->tqe_pnext = &ifq->ifq_head;
5774 }
5775 MUTEX_EXIT(&ifq->ifq_lock);
5776 }
5777
5778
5779 /* ------------------------------------------------------------------------ */
5780 /* Function: ipf_queueback */
5781 /* Returns: Nil */
5782 /* Parameters: ticks(I) - ipf tick time to use with this call */
5783 /* tqe(I) - pointer to timeout queue entry */
5784 /* */
5785 /* Move a queue entry to the back of the queue, if it isn't already there. */
5786 /* We use use ticks to calculate the expiration and mark for when we last */
5787 /* touched the structure. */
5788 /* ------------------------------------------------------------------------ */
5789 void
ipf_queueback(u_long ticks,ipftqent_t * tqe)5790 ipf_queueback(u_long ticks, ipftqent_t *tqe)
5791 {
5792 ipftq_t *ifq;
5793
5794 ifq = tqe->tqe_ifq;
5795 if (ifq == NULL)
5796 return;
5797 tqe->tqe_die = ticks + ifq->ifq_ttl;
5798 tqe->tqe_touched = ticks;
5799
5800 MUTEX_ENTER(&ifq->ifq_lock);
5801 if (tqe->tqe_next != NULL) { /* at the end already ? */
5802 /*
5803 * Remove from list
5804 */
5805 *tqe->tqe_pnext = tqe->tqe_next;
5806 tqe->tqe_next->tqe_pnext = tqe->tqe_pnext;
5807
5808 /*
5809 * Make it the last entry.
5810 */
5811 tqe->tqe_next = NULL;
5812 tqe->tqe_pnext = ifq->ifq_tail;
5813 *ifq->ifq_tail = tqe;
5814 ifq->ifq_tail = &tqe->tqe_next;
5815 }
5816 MUTEX_EXIT(&ifq->ifq_lock);
5817 }
5818
5819
5820 /* ------------------------------------------------------------------------ */
5821 /* Function: ipf_queueappend */
5822 /* Returns: Nil */
5823 /* Parameters: ticks(I) - ipf tick time to use with this call */
5824 /* tqe(I) - pointer to timeout queue entry */
5825 /* ifq(I) - pointer to timeout queue */
5826 /* parent(I) - owing object pointer */
5827 /* */
5828 /* Add a new item to this queue and put it on the very end. */
5829 /* We use use ticks to calculate the expiration and mark for when we last */
5830 /* touched the structure. */
5831 /* ------------------------------------------------------------------------ */
5832 void
ipf_queueappend(u_long ticks,ipftqent_t * tqe,ipftq_t * ifq,void * parent)5833 ipf_queueappend(u_long ticks, ipftqent_t *tqe, ipftq_t *ifq, void *parent)
5834 {
5835
5836 MUTEX_ENTER(&ifq->ifq_lock);
5837 tqe->tqe_parent = parent;
5838 tqe->tqe_pnext = ifq->ifq_tail;
5839 *ifq->ifq_tail = tqe;
5840 ifq->ifq_tail = &tqe->tqe_next;
5841 tqe->tqe_next = NULL;
5842 tqe->tqe_ifq = ifq;
5843 tqe->tqe_die = ticks + ifq->ifq_ttl;
5844 tqe->tqe_touched = ticks;
5845 ifq->ifq_ref++;
5846 MUTEX_EXIT(&ifq->ifq_lock);
5847 }
5848
5849
5850 /* ------------------------------------------------------------------------ */
5851 /* Function: ipf_movequeue */
5852 /* Returns: Nil */
5853 /* Parameters: tq(I) - pointer to timeout queue information */
5854 /* oifp(I) - old timeout queue entry was on */
5855 /* nifp(I) - new timeout queue to put entry on */
5856 /* */
5857 /* Move a queue entry from one timeout queue to another timeout queue. */
5858 /* If it notices that the current entry is already last and does not need */
5859 /* to move queue, the return. */
5860 /* ------------------------------------------------------------------------ */
5861 void
ipf_movequeue(u_long ticks,ipftqent_t * tqe,ipftq_t * oifq,ipftq_t * nifq)5862 ipf_movequeue(u_long ticks, ipftqent_t *tqe, ipftq_t *oifq, ipftq_t *nifq)
5863 {
5864
5865 /*
5866 * If the queue hasn't changed and we last touched this entry at the
5867 * same ipf time, then we're not going to achieve anything by either
5868 * changing the ttl or moving it on the queue.
5869 */
5870 if (oifq == nifq && tqe->tqe_touched == ticks)
5871 return;
5872
5873 /*
5874 * For any of this to be outside the lock, there is a risk that two
5875 * packets entering simultaneously, with one changing to a different
5876 * queue and one not, could end up with things in a bizarre state.
5877 */
5878 MUTEX_ENTER(&oifq->ifq_lock);
5879
5880 tqe->tqe_touched = ticks;
5881 tqe->tqe_die = ticks + nifq->ifq_ttl;
5882 /*
5883 * Is the operation here going to be a no-op ?
5884 */
5885 if (oifq == nifq) {
5886 if ((tqe->tqe_next == NULL) ||
5887 (tqe->tqe_next->tqe_die == tqe->tqe_die)) {
5888 MUTEX_EXIT(&oifq->ifq_lock);
5889 return;
5890 }
5891 }
5892
5893 /*
5894 * Remove from the old queue
5895 */
5896 *tqe->tqe_pnext = tqe->tqe_next;
5897 if (tqe->tqe_next)
5898 tqe->tqe_next->tqe_pnext = tqe->tqe_pnext;
5899 else
5900 oifq->ifq_tail = tqe->tqe_pnext;
5901 tqe->tqe_next = NULL;
5902
5903 /*
5904 * If we're moving from one queue to another, release the
5905 * lock on the old queue and get a lock on the new queue.
5906 * For user defined queues, if we're moving off it, call
5907 * delete in case it can now be freed.
5908 */
5909 if (oifq != nifq) {
5910 tqe->tqe_ifq = NULL;
5911
5912 (void) ipf_deletetimeoutqueue(oifq);
5913
5914 MUTEX_EXIT(&oifq->ifq_lock);
5915
5916 MUTEX_ENTER(&nifq->ifq_lock);
5917
5918 tqe->tqe_ifq = nifq;
5919 nifq->ifq_ref++;
5920 }
5921
5922 /*
5923 * Add to the bottom of the new queue
5924 */
5925 tqe->tqe_pnext = nifq->ifq_tail;
5926 *nifq->ifq_tail = tqe;
5927 nifq->ifq_tail = &tqe->tqe_next;
5928 MUTEX_EXIT(&nifq->ifq_lock);
5929 }
5930
5931
5932 /* ------------------------------------------------------------------------ */
5933 /* Function: ipf_updateipid */
5934 /* Returns: int - 0 == success, -1 == error (packet should be droppped) */
5935 /* Parameters: fin(I) - pointer to packet information */
5936 /* */
5937 /* When we are doing NAT, change the IP of every packet to represent a */
5938 /* single sequence of packets coming from the host, hiding any host */
5939 /* specific sequencing that might otherwise be revealed. If the packet is */
5940 /* a fragment, then store the 'new' IPid in the fragment cache and look up */
5941 /* the fragment cache for non-leading fragments. If a non-leading fragment */
5942 /* has no match in the cache, return an error. */
5943 /* ------------------------------------------------------------------------ */
5944 static int
ipf_updateipid(fr_info_t * fin)5945 ipf_updateipid(fr_info_t *fin)
5946 {
5947 u_short id, ido, sums;
5948 u_32_t sumd, sum;
5949 ip_t *ip;
5950
5951 ip = fin->fin_ip;
5952 ido = ntohs(ip->ip_id);
5953 if (fin->fin_off != 0) {
5954 sum = ipf_frag_ipidknown(fin);
5955 if (sum == 0xffffffff)
5956 return (-1);
5957 sum &= 0xffff;
5958 id = (u_short)sum;
5959 ip->ip_id = htons(id);
5960 } else {
5961 ip_fillid(ip);
5962 id = ntohs(ip->ip_id);
5963 if ((fin->fin_flx & FI_FRAG) != 0)
5964 (void) ipf_frag_ipidnew(fin, (u_32_t)id);
5965 }
5966
5967 if (id == ido)
5968 return (0);
5969 CALC_SUMD(ido, id, sumd); /* DESTRUCTIVE MACRO! id,ido change */
5970 sum = (~ntohs(ip->ip_sum)) & 0xffff;
5971 sum += sumd;
5972 sum = (sum >> 16) + (sum & 0xffff);
5973 sum = (sum >> 16) + (sum & 0xffff);
5974 sums = ~(u_short)sum;
5975 ip->ip_sum = htons(sums);
5976 return (0);
5977 }
5978
5979
5980 #ifdef NEED_FRGETIFNAME
5981 /* ------------------------------------------------------------------------ */
5982 /* Function: ipf_getifname */
5983 /* Returns: char * - pointer to interface name */
5984 /* Parameters: ifp(I) - pointer to network interface */
5985 /* buffer(O) - pointer to where to store interface name */
5986 /* */
5987 /* Constructs an interface name in the buffer passed. The buffer passed is */
5988 /* expected to be at least LIFNAMSIZ in bytes big. If buffer is passed in */
5989 /* as a NULL pointer then return a pointer to a static array. */
5990 /* ------------------------------------------------------------------------ */
5991 char *
ipf_getifname(struct ifnet * ifp,char * buffer)5992 ipf_getifname(struct ifnet *ifp, char *buffer)
5993 {
5994 static char namebuf[LIFNAMSIZ];
5995 # if SOLARIS || defined(__FreeBSD__)
5996 int unit, space;
5997 char temp[20];
5998 char *s;
5999 # endif
6000
6001 if (buffer == NULL)
6002 buffer = namebuf;
6003 (void) strncpy(buffer, ifp->if_name, LIFNAMSIZ);
6004 buffer[LIFNAMSIZ - 1] = '\0';
6005 # if SOLARIS || defined(__FreeBSD__)
6006 for (s = buffer; *s; s++)
6007 ;
6008 unit = ifp->if_unit;
6009 space = LIFNAMSIZ - (s - buffer);
6010 if ((space > 0) && (unit >= 0)) {
6011 (void) snprintf(temp, sizeof(name), "%d", unit);
6012 (void) strncpy(s, temp, space);
6013 }
6014 # endif
6015 return (buffer);
6016 }
6017 #endif
6018
6019
6020 /* ------------------------------------------------------------------------ */
6021 /* Function: ipf_ioctlswitch */
6022 /* Returns: int - -1 continue processing, else ioctl return value */
6023 /* Parameters: unit(I) - device unit opened */
6024 /* data(I) - pointer to ioctl data */
6025 /* cmd(I) - ioctl command */
6026 /* mode(I) - mode value */
6027 /* uid(I) - uid making the ioctl call */
6028 /* ctx(I) - pointer to context data */
6029 /* */
6030 /* Based on the value of unit, call the appropriate ioctl handler or return */
6031 /* EIO if ipfilter is not running. Also checks if write perms are req'd */
6032 /* for the device in order to execute the ioctl. A special case is made */
6033 /* SIOCIPFINTERROR so that the same code isn't required in every handler. */
6034 /* The context data pointer is passed through as this is used as the key */
6035 /* for locating a matching token for continued access for walking lists, */
6036 /* etc. */
6037 /* ------------------------------------------------------------------------ */
6038 int
ipf_ioctlswitch(ipf_main_softc_t * softc,int unit,void * data,ioctlcmd_t cmd,int mode,int uid,void * ctx)6039 ipf_ioctlswitch(ipf_main_softc_t *softc, int unit, void *data, ioctlcmd_t cmd,
6040 int mode, int uid, void *ctx)
6041 {
6042 int error = 0;
6043
6044 switch (cmd)
6045 {
6046 case SIOCIPFINTERROR :
6047 error = BCOPYOUT(&softc->ipf_interror, data,
6048 sizeof(softc->ipf_interror));
6049 if (error != 0) {
6050 IPFERROR(40);
6051 error = EFAULT;
6052 }
6053 return (error);
6054 default :
6055 break;
6056 }
6057
6058 switch (unit)
6059 {
6060 case IPL_LOGIPF :
6061 error = ipf_ipf_ioctl(softc, data, cmd, mode, uid, ctx);
6062 break;
6063 case IPL_LOGNAT :
6064 if (softc->ipf_running > 0) {
6065 error = ipf_nat_ioctl(softc, data, cmd, mode,
6066 uid, ctx);
6067 } else {
6068 IPFERROR(42);
6069 error = EIO;
6070 }
6071 break;
6072 case IPL_LOGSTATE :
6073 if (softc->ipf_running > 0) {
6074 error = ipf_state_ioctl(softc, data, cmd, mode,
6075 uid, ctx);
6076 } else {
6077 IPFERROR(43);
6078 error = EIO;
6079 }
6080 break;
6081 case IPL_LOGAUTH :
6082 if (softc->ipf_running > 0) {
6083 error = ipf_auth_ioctl(softc, data, cmd, mode,
6084 uid, ctx);
6085 } else {
6086 IPFERROR(44);
6087 error = EIO;
6088 }
6089 break;
6090 case IPL_LOGSYNC :
6091 if (softc->ipf_running > 0) {
6092 error = ipf_sync_ioctl(softc, data, cmd, mode,
6093 uid, ctx);
6094 } else {
6095 error = EIO;
6096 IPFERROR(45);
6097 }
6098 break;
6099 case IPL_LOGSCAN :
6100 #ifdef IPFILTER_SCAN
6101 if (softc->ipf_running > 0)
6102 error = ipf_scan_ioctl(softc, data, cmd, mode,
6103 uid, ctx);
6104 else
6105 #endif
6106 {
6107 error = EIO;
6108 IPFERROR(46);
6109 }
6110 break;
6111 case IPL_LOGLOOKUP :
6112 if (softc->ipf_running > 0) {
6113 error = ipf_lookup_ioctl(softc, data, cmd, mode,
6114 uid, ctx);
6115 } else {
6116 error = EIO;
6117 IPFERROR(47);
6118 }
6119 break;
6120 default :
6121 IPFERROR(48);
6122 error = EIO;
6123 break;
6124 }
6125
6126 return (error);
6127 }
6128
6129
6130 /*
6131 * This array defines the expected size of objects coming into the kernel
6132 * for the various recognised object types. The first column is flags (see
6133 * below), 2nd column is current size, 3rd column is the version number of
6134 * when the current size became current.
6135 * Flags:
6136 * 1 = minimum size, not absolute size
6137 */
6138 static const int ipf_objbytes[IPFOBJ_COUNT][3] = {
6139 { 1, sizeof(struct frentry), 5010000 }, /* 0 */
6140 { 1, sizeof(struct friostat), 5010000 },
6141 { 0, sizeof(struct fr_info), 5010000 },
6142 { 0, sizeof(struct ipf_authstat), 4010100 },
6143 { 0, sizeof(struct ipfrstat), 5010000 },
6144 { 1, sizeof(struct ipnat), 5010000 }, /* 5 */
6145 { 0, sizeof(struct natstat), 5010000 },
6146 { 0, sizeof(struct ipstate_save), 5010000 },
6147 { 1, sizeof(struct nat_save), 5010000 },
6148 { 0, sizeof(struct natlookup), 5010000 },
6149 { 1, sizeof(struct ipstate), 5010000 }, /* 10 */
6150 { 0, sizeof(struct ips_stat), 5010000 },
6151 { 0, sizeof(struct frauth), 5010000 },
6152 { 0, sizeof(struct ipftune), 4010100 },
6153 { 0, sizeof(struct nat), 5010000 },
6154 { 0, sizeof(struct ipfruleiter), 4011400 }, /* 15 */
6155 { 0, sizeof(struct ipfgeniter), 4011400 },
6156 { 0, sizeof(struct ipftable), 4011400 },
6157 { 0, sizeof(struct ipflookupiter), 4011400 },
6158 { 0, sizeof(struct ipftq) * IPF_TCP_NSTATES },
6159 { 1, 0, 0 }, /* IPFEXPR */
6160 { 0, 0, 0 }, /* PROXYCTL */
6161 { 0, sizeof (struct fripf), 5010000 }
6162 };
6163
6164
6165 /* ------------------------------------------------------------------------ */
6166 /* Function: ipf_inobj */
6167 /* Returns: int - 0 = success, else failure */
6168 /* Parameters: softc(I) - soft context pointerto work with */
6169 /* data(I) - pointer to ioctl data */
6170 /* objp(O) - where to store ipfobj structure */
6171 /* ptr(I) - pointer to data to copy out */
6172 /* type(I) - type of structure being moved */
6173 /* */
6174 /* Copy in the contents of what the ipfobj_t points to. In future, we */
6175 /* add things to check for version numbers, sizes, etc, to make it backward */
6176 /* compatible at the ABI for user land. */
6177 /* If objp is not NULL then we assume that the caller wants to see what is */
6178 /* in the ipfobj_t structure being copied in. As an example, this can tell */
6179 /* the caller what version of ipfilter the ioctl program was written to. */
6180 /* ------------------------------------------------------------------------ */
6181 int
ipf_inobj(ipf_main_softc_t * softc,void * data,ipfobj_t * objp,void * ptr,int type)6182 ipf_inobj(ipf_main_softc_t *softc, void *data, ipfobj_t *objp, void *ptr,
6183 int type)
6184 {
6185 ipfobj_t obj;
6186 int error;
6187 int size;
6188
6189 if ((type < 0) || (type >= IPFOBJ_COUNT)) {
6190 IPFERROR(49);
6191 return (EINVAL);
6192 }
6193
6194 if (objp == NULL)
6195 objp = &obj;
6196 error = BCOPYIN(data, objp, sizeof(*objp));
6197 if (error != 0) {
6198 IPFERROR(124);
6199 return (EFAULT);
6200 }
6201
6202 if (objp->ipfo_type != type) {
6203 IPFERROR(50);
6204 return (EINVAL);
6205 }
6206
6207 if (objp->ipfo_rev >= ipf_objbytes[type][2]) {
6208 if ((ipf_objbytes[type][0] & 1) != 0) {
6209 if (objp->ipfo_size < ipf_objbytes[type][1]) {
6210 IPFERROR(51);
6211 return (EINVAL);
6212 }
6213 size = ipf_objbytes[type][1];
6214 } else if (objp->ipfo_size == ipf_objbytes[type][1]) {
6215 size = objp->ipfo_size;
6216 } else {
6217 IPFERROR(52);
6218 return (EINVAL);
6219 }
6220 error = COPYIN(objp->ipfo_ptr, ptr, size);
6221 if (error != 0) {
6222 IPFERROR(55);
6223 error = EFAULT;
6224 }
6225 } else {
6226 #ifdef IPFILTER_COMPAT
6227 error = ipf_in_compat(softc, objp, ptr, 0);
6228 #else
6229 IPFERROR(54);
6230 error = EINVAL;
6231 #endif
6232 }
6233 return (error);
6234 }
6235
6236
6237 /* ------------------------------------------------------------------------ */
6238 /* Function: ipf_inobjsz */
6239 /* Returns: int - 0 = success, else failure */
6240 /* Parameters: softc(I) - soft context pointerto work with */
6241 /* data(I) - pointer to ioctl data */
6242 /* ptr(I) - pointer to store real data in */
6243 /* type(I) - type of structure being moved */
6244 /* sz(I) - size of data to copy */
6245 /* */
6246 /* As per ipf_inobj, except the size of the object to copy in is passed in */
6247 /* but it must not be smaller than the size defined for the type and the */
6248 /* type must allow for varied sized objects. The extra requirement here is */
6249 /* that sz must match the size of the object being passed in - this is not */
6250 /* not possible nor required in ipf_inobj(). */
6251 /* ------------------------------------------------------------------------ */
6252 int
ipf_inobjsz(ipf_main_softc_t * softc,void * data,void * ptr,int type,int sz)6253 ipf_inobjsz(ipf_main_softc_t *softc, void *data, void *ptr, int type, int sz)
6254 {
6255 ipfobj_t obj;
6256 int error;
6257
6258 if ((type < 0) || (type >= IPFOBJ_COUNT)) {
6259 IPFERROR(56);
6260 return (EINVAL);
6261 }
6262
6263 error = BCOPYIN(data, &obj, sizeof(obj));
6264 if (error != 0) {
6265 IPFERROR(125);
6266 return (EFAULT);
6267 }
6268
6269 if (obj.ipfo_type != type) {
6270 IPFERROR(58);
6271 return (EINVAL);
6272 }
6273
6274 if (obj.ipfo_rev >= ipf_objbytes[type][2]) {
6275 if (((ipf_objbytes[type][0] & 1) == 0) ||
6276 (sz < ipf_objbytes[type][1])) {
6277 IPFERROR(57);
6278 return (EINVAL);
6279 }
6280 error = COPYIN(obj.ipfo_ptr, ptr, sz);
6281 if (error != 0) {
6282 IPFERROR(61);
6283 error = EFAULT;
6284 }
6285 } else {
6286 #ifdef IPFILTER_COMPAT
6287 error = ipf_in_compat(softc, &obj, ptr, sz);
6288 #else
6289 IPFERROR(60);
6290 error = EINVAL;
6291 #endif
6292 }
6293 return (error);
6294 }
6295
6296
6297 /* ------------------------------------------------------------------------ */
6298 /* Function: ipf_outobjsz */
6299 /* Returns: int - 0 = success, else failure */
6300 /* Parameters: data(I) - pointer to ioctl data */
6301 /* ptr(I) - pointer to store real data in */
6302 /* type(I) - type of structure being moved */
6303 /* sz(I) - size of data to copy */
6304 /* */
6305 /* As per ipf_outobj, except the size of the object to copy out is passed in*/
6306 /* but it must not be smaller than the size defined for the type and the */
6307 /* type must allow for varied sized objects. The extra requirement here is */
6308 /* that sz must match the size of the object being passed in - this is not */
6309 /* not possible nor required in ipf_outobj(). */
6310 /* ------------------------------------------------------------------------ */
6311 int
ipf_outobjsz(ipf_main_softc_t * softc,void * data,void * ptr,int type,int sz)6312 ipf_outobjsz(ipf_main_softc_t *softc, void *data, void *ptr, int type, int sz)
6313 {
6314 ipfobj_t obj;
6315 int error;
6316
6317 if ((type < 0) || (type >= IPFOBJ_COUNT)) {
6318 IPFERROR(62);
6319 return (EINVAL);
6320 }
6321
6322 error = BCOPYIN(data, &obj, sizeof(obj));
6323 if (error != 0) {
6324 IPFERROR(127);
6325 return (EFAULT);
6326 }
6327
6328 if (obj.ipfo_type != type) {
6329 IPFERROR(63);
6330 return (EINVAL);
6331 }
6332
6333 if (obj.ipfo_rev >= ipf_objbytes[type][2]) {
6334 if (((ipf_objbytes[type][0] & 1) == 0) ||
6335 (sz < ipf_objbytes[type][1])) {
6336 IPFERROR(146);
6337 return (EINVAL);
6338 }
6339 error = COPYOUT(ptr, obj.ipfo_ptr, sz);
6340 if (error != 0) {
6341 IPFERROR(66);
6342 error = EFAULT;
6343 }
6344 } else {
6345 #ifdef IPFILTER_COMPAT
6346 error = ipf_out_compat(softc, &obj, ptr);
6347 #else
6348 IPFERROR(65);
6349 error = EINVAL;
6350 #endif
6351 }
6352 return (error);
6353 }
6354
6355
6356 /* ------------------------------------------------------------------------ */
6357 /* Function: ipf_outobj */
6358 /* Returns: int - 0 = success, else failure */
6359 /* Parameters: data(I) - pointer to ioctl data */
6360 /* ptr(I) - pointer to store real data in */
6361 /* type(I) - type of structure being moved */
6362 /* */
6363 /* Copy out the contents of what ptr is to where ipfobj points to. In */
6364 /* future, we add things to check for version numbers, sizes, etc, to make */
6365 /* it backward compatible at the ABI for user land. */
6366 /* ------------------------------------------------------------------------ */
6367 int
ipf_outobj(ipf_main_softc_t * softc,void * data,void * ptr,int type)6368 ipf_outobj(ipf_main_softc_t *softc, void *data, void *ptr, int type)
6369 {
6370 ipfobj_t obj;
6371 int error;
6372
6373 if ((type < 0) || (type >= IPFOBJ_COUNT)) {
6374 IPFERROR(67);
6375 return (EINVAL);
6376 }
6377
6378 error = BCOPYIN(data, &obj, sizeof(obj));
6379 if (error != 0) {
6380 IPFERROR(126);
6381 return (EFAULT);
6382 }
6383
6384 if (obj.ipfo_type != type) {
6385 IPFERROR(68);
6386 return (EINVAL);
6387 }
6388
6389 if (obj.ipfo_rev >= ipf_objbytes[type][2]) {
6390 if ((ipf_objbytes[type][0] & 1) != 0) {
6391 if (obj.ipfo_size < ipf_objbytes[type][1]) {
6392 IPFERROR(69);
6393 return (EINVAL);
6394 }
6395 } else if (obj.ipfo_size != ipf_objbytes[type][1]) {
6396 IPFERROR(70);
6397 return (EINVAL);
6398 }
6399
6400 error = COPYOUT(ptr, obj.ipfo_ptr, obj.ipfo_size);
6401 if (error != 0) {
6402 IPFERROR(73);
6403 error = EFAULT;
6404 }
6405 } else {
6406 #ifdef IPFILTER_COMPAT
6407 error = ipf_out_compat(softc, &obj, ptr);
6408 #else
6409 IPFERROR(72);
6410 error = EINVAL;
6411 #endif
6412 }
6413 return (error);
6414 }
6415
6416
6417 /* ------------------------------------------------------------------------ */
6418 /* Function: ipf_outobjk */
6419 /* Returns: int - 0 = success, else failure */
6420 /* Parameters: obj(I) - pointer to data description structure */
6421 /* ptr(I) - pointer to kernel data to copy out */
6422 /* */
6423 /* In the above functions, the ipfobj_t structure is copied into the kernel,*/
6424 /* telling ipfilter how to copy out data. In this instance, the ipfobj_t is */
6425 /* already populated with information and now we just need to use it. */
6426 /* There is no need for this function to have a "type" parameter as there */
6427 /* is no point in validating information that comes from the kernel with */
6428 /* itself. */
6429 /* ------------------------------------------------------------------------ */
6430 int
ipf_outobjk(ipf_main_softc_t * softc,ipfobj_t * obj,void * ptr)6431 ipf_outobjk(ipf_main_softc_t *softc, ipfobj_t *obj, void *ptr)
6432 {
6433 int type = obj->ipfo_type;
6434 int error;
6435
6436 if ((type < 0) || (type >= IPFOBJ_COUNT)) {
6437 IPFERROR(147);
6438 return (EINVAL);
6439 }
6440
6441 if (obj->ipfo_rev >= ipf_objbytes[type][2]) {
6442 if ((ipf_objbytes[type][0] & 1) != 0) {
6443 if (obj->ipfo_size < ipf_objbytes[type][1]) {
6444 IPFERROR(148);
6445 return (EINVAL);
6446 }
6447
6448 } else if (obj->ipfo_size != ipf_objbytes[type][1]) {
6449 IPFERROR(149);
6450 return (EINVAL);
6451 }
6452
6453 error = COPYOUT(ptr, obj->ipfo_ptr, obj->ipfo_size);
6454 if (error != 0) {
6455 IPFERROR(150);
6456 error = EFAULT;
6457 }
6458 } else {
6459 #ifdef IPFILTER_COMPAT
6460 error = ipf_out_compat(softc, obj, ptr);
6461 #else
6462 IPFERROR(151);
6463 error = EINVAL;
6464 #endif
6465 }
6466 return (error);
6467 }
6468
6469
6470 /* ------------------------------------------------------------------------ */
6471 /* Function: ipf_checkl4sum */
6472 /* Returns: int - 0 = good, -1 = bad, 1 = cannot check */
6473 /* Parameters: fin(I) - pointer to packet information */
6474 /* */
6475 /* If possible, calculate the layer 4 checksum for the packet. If this is */
6476 /* not possible, return without indicating a failure or success but in a */
6477 /* way that is ditinguishable. This function should only be called by the */
6478 /* ipf_checkv6sum() for each platform. */
6479 /* ------------------------------------------------------------------------ */
6480 inline int
ipf_checkl4sum(fr_info_t * fin)6481 ipf_checkl4sum(fr_info_t *fin)
6482 {
6483 u_short sum, hdrsum, *csump;
6484 udphdr_t *udp;
6485 int dosum;
6486
6487 /*
6488 * If the TCP packet isn't a fragment, isn't too short and otherwise
6489 * isn't already considered "bad", then validate the checksum. If
6490 * this check fails then considered the packet to be "bad".
6491 */
6492 if ((fin->fin_flx & (FI_FRAG|FI_SHORT|FI_BAD)) != 0)
6493 return (1);
6494
6495 DT2(l4sumo, int, fin->fin_out, int, (int)fin->fin_p);
6496 if (fin->fin_out == 1) {
6497 fin->fin_cksum = FI_CK_SUMOK;
6498 return (0);
6499 }
6500
6501 csump = NULL;
6502 hdrsum = 0;
6503 dosum = 0;
6504 sum = 0;
6505
6506 switch (fin->fin_p)
6507 {
6508 case IPPROTO_TCP :
6509 csump = &((tcphdr_t *)fin->fin_dp)->th_sum;
6510 dosum = 1;
6511 break;
6512
6513 case IPPROTO_UDP :
6514 udp = fin->fin_dp;
6515 if (udp->uh_sum != 0) {
6516 csump = &udp->uh_sum;
6517 dosum = 1;
6518 }
6519 break;
6520
6521 #ifdef USE_INET6
6522 case IPPROTO_ICMPV6 :
6523 csump = &((struct icmp6_hdr *)fin->fin_dp)->icmp6_cksum;
6524 dosum = 1;
6525 break;
6526 #endif
6527
6528 case IPPROTO_ICMP :
6529 csump = &((struct icmp *)fin->fin_dp)->icmp_cksum;
6530 dosum = 1;
6531 break;
6532
6533 default :
6534 return (1);
6535 /*NOTREACHED*/
6536 }
6537
6538 if (csump != NULL) {
6539 hdrsum = *csump;
6540 if (fin->fin_p == IPPROTO_UDP && hdrsum == 0xffff)
6541 hdrsum = 0x0000;
6542 }
6543
6544 if (dosum) {
6545 sum = fr_cksum(fin, fin->fin_ip, fin->fin_p, fin->fin_dp);
6546 }
6547 #if !defined(_KERNEL)
6548 if (sum == hdrsum) {
6549 FR_DEBUG(("checkl4sum: %hx == %hx\n", sum, hdrsum));
6550 } else {
6551 FR_DEBUG(("checkl4sum: %hx != %hx\n", sum, hdrsum));
6552 }
6553 #endif
6554 DT3(l4sums, u_short, hdrsum, u_short, sum, fr_info_t *, fin);
6555 #ifdef USE_INET6
6556 if (hdrsum == sum || (sum == 0 && IP_V(fin->fin_ip) == 6)) {
6557 #else
6558 if (hdrsum == sum) {
6559 #endif
6560 fin->fin_cksum = FI_CK_SUMOK;
6561 return (0);
6562 }
6563 fin->fin_cksum = FI_CK_BAD;
6564 return (-1);
6565 }
6566
6567
6568 /* ------------------------------------------------------------------------ */
6569 /* Function: ipf_ifpfillv4addr */
6570 /* Returns: int - 0 = address update, -1 = address not updated */
6571 /* Parameters: atype(I) - type of network address update to perform */
6572 /* sin(I) - pointer to source of address information */
6573 /* mask(I) - pointer to source of netmask information */
6574 /* inp(I) - pointer to destination address store */
6575 /* inpmask(I) - pointer to destination netmask store */
6576 /* */
6577 /* Given a type of network address update (atype) to perform, copy */
6578 /* information from sin/mask into inp/inpmask. If ipnmask is NULL then no */
6579 /* netmask update is performed unless FRI_NETMASKED is passed as atype, in */
6580 /* which case the operation fails. For all values of atype other than */
6581 /* FRI_NETMASKED, if inpmask is non-NULL then the mask is set to an all 1s */
6582 /* value. */
6583 /* ------------------------------------------------------------------------ */
6584 int
6585 ipf_ifpfillv4addr(int atype, struct sockaddr_in *sin, struct sockaddr_in *mask,
6586 struct in_addr *inp, struct in_addr *inpmask)
6587 {
6588 if (inpmask != NULL && atype != FRI_NETMASKED)
6589 inpmask->s_addr = 0xffffffff;
6590
6591 if (atype == FRI_NETWORK || atype == FRI_NETMASKED) {
6592 if (atype == FRI_NETMASKED) {
6593 if (inpmask == NULL)
6594 return (-1);
6595 inpmask->s_addr = mask->sin_addr.s_addr;
6596 }
6597 inp->s_addr = sin->sin_addr.s_addr & mask->sin_addr.s_addr;
6598 } else {
6599 inp->s_addr = sin->sin_addr.s_addr;
6600 }
6601 return (0);
6602 }
6603
6604
6605 #ifdef USE_INET6
6606 /* ------------------------------------------------------------------------ */
6607 /* Function: ipf_ifpfillv6addr */
6608 /* Returns: int - 0 = address update, -1 = address not updated */
6609 /* Parameters: atype(I) - type of network address update to perform */
6610 /* sin(I) - pointer to source of address information */
6611 /* mask(I) - pointer to source of netmask information */
6612 /* inp(I) - pointer to destination address store */
6613 /* inpmask(I) - pointer to destination netmask store */
6614 /* */
6615 /* Given a type of network address update (atype) to perform, copy */
6616 /* information from sin/mask into inp/inpmask. If ipnmask is NULL then no */
6617 /* netmask update is performed unless FRI_NETMASKED is passed as atype, in */
6618 /* which case the operation fails. For all values of atype other than */
6619 /* FRI_NETMASKED, if inpmask is non-NULL then the mask is set to an all 1s */
6620 /* value. */
6621 /* ------------------------------------------------------------------------ */
6622 int
6623 ipf_ifpfillv6addr(int atype, struct sockaddr_in6 *sin,
6624 struct sockaddr_in6 *mask, i6addr_t *inp, i6addr_t *inpmask)
6625 {
6626 i6addr_t *src, *and;
6627
6628 src = (i6addr_t *)&sin->sin6_addr;
6629 and = (i6addr_t *)&mask->sin6_addr;
6630
6631 if (inpmask != NULL && atype != FRI_NETMASKED) {
6632 inpmask->i6[0] = 0xffffffff;
6633 inpmask->i6[1] = 0xffffffff;
6634 inpmask->i6[2] = 0xffffffff;
6635 inpmask->i6[3] = 0xffffffff;
6636 }
6637
6638 if (atype == FRI_NETWORK || atype == FRI_NETMASKED) {
6639 if (atype == FRI_NETMASKED) {
6640 if (inpmask == NULL)
6641 return (-1);
6642 inpmask->i6[0] = and->i6[0];
6643 inpmask->i6[1] = and->i6[1];
6644 inpmask->i6[2] = and->i6[2];
6645 inpmask->i6[3] = and->i6[3];
6646 }
6647
6648 inp->i6[0] = src->i6[0] & and->i6[0];
6649 inp->i6[1] = src->i6[1] & and->i6[1];
6650 inp->i6[2] = src->i6[2] & and->i6[2];
6651 inp->i6[3] = src->i6[3] & and->i6[3];
6652 } else {
6653 inp->i6[0] = src->i6[0];
6654 inp->i6[1] = src->i6[1];
6655 inp->i6[2] = src->i6[2];
6656 inp->i6[3] = src->i6[3];
6657 }
6658 return (0);
6659 }
6660 #endif
6661
6662
6663 /* ------------------------------------------------------------------------ */
6664 /* Function: ipf_matchtag */
6665 /* Returns: 0 == mismatch, 1 == match. */
6666 /* Parameters: tag1(I) - pointer to first tag to compare */
6667 /* tag2(I) - pointer to second tag to compare */
6668 /* */
6669 /* Returns true (non-zero) or false(0) if the two tag structures can be */
6670 /* considered to be a match or not match, respectively. The tag is 16 */
6671 /* bytes long (16 characters) but that is overlayed with 4 32bit ints so */
6672 /* compare the ints instead, for speed. tag1 is the master of the */
6673 /* comparison. This function should only be called with both tag1 and tag2 */
6674 /* as non-NULL pointers. */
6675 /* ------------------------------------------------------------------------ */
6676 int
6677 ipf_matchtag(ipftag_t *tag1, ipftag_t *tag2)
6678 {
6679 if (tag1 == tag2)
6680 return (1);
6681
6682 if ((tag1->ipt_num[0] == 0) && (tag2->ipt_num[0] == 0))
6683 return (1);
6684
6685 if ((tag1->ipt_num[0] == tag2->ipt_num[0]) &&
6686 (tag1->ipt_num[1] == tag2->ipt_num[1]) &&
6687 (tag1->ipt_num[2] == tag2->ipt_num[2]) &&
6688 (tag1->ipt_num[3] == tag2->ipt_num[3]))
6689 return (1);
6690 return (0);
6691 }
6692
6693
6694 /* ------------------------------------------------------------------------ */
6695 /* Function: ipf_coalesce */
6696 /* Returns: 1 == success, -1 == failure, 0 == no change */
6697 /* Parameters: fin(I) - pointer to packet information */
6698 /* */
6699 /* Attempt to get all of the packet data into a single, contiguous buffer. */
6700 /* If this call returns a failure then the buffers have also been freed. */
6701 /* ------------------------------------------------------------------------ */
6702 int
6703 ipf_coalesce(fr_info_t *fin)
6704 {
6705
6706 if ((fin->fin_flx & FI_COALESCE) != 0)
6707 return (1);
6708
6709 /*
6710 * If the mbuf pointers indicate that there is no mbuf to work with,
6711 * return but do not indicate success or failure.
6712 */
6713 if (fin->fin_m == NULL || fin->fin_mp == NULL)
6714 return (0);
6715
6716 #if defined(_KERNEL)
6717 if (ipf_pullup(fin->fin_m, fin, fin->fin_plen) == NULL) {
6718 ipf_main_softc_t *softc = fin->fin_main_soft;
6719
6720 DT1(frb_coalesce, fr_info_t *, fin);
6721 LBUMP(ipf_stats[fin->fin_out].fr_badcoalesces);
6722 # if SOLARIS
6723 FREE_MB_T(*fin->fin_mp);
6724 # endif
6725 fin->fin_reason = FRB_COALESCE;
6726 *fin->fin_mp = NULL;
6727 fin->fin_m = NULL;
6728 return (-1);
6729 }
6730 #else
6731 fin = fin; /* LINT */
6732 #endif
6733 return (1);
6734 }
6735
6736
6737 /*
6738 * The following table lists all of the tunable variables that can be
6739 * accessed via SIOCIPFGET/SIOCIPFSET/SIOCIPFGETNEXt. The format of each row
6740 * in the table below is as follows:
6741 *
6742 * pointer to value, name of value, minimum, maximum, size of the value's
6743 * container, value attribute flags
6744 *
6745 * For convienience, IPFT_RDONLY means the value is read-only, IPFT_WRDISABLED
6746 * means the value can only be written to when IPFilter is loaded but disabled.
6747 * The obvious implication is if neither of these are set then the value can be
6748 * changed at any time without harm.
6749 */
6750
6751
6752 /* ------------------------------------------------------------------------ */
6753 /* Function: ipf_tune_findbycookie */
6754 /* Returns: NULL = search failed, else pointer to tune struct */
6755 /* Parameters: cookie(I) - cookie value to search for amongst tuneables */
6756 /* next(O) - pointer to place to store the cookie for the */
6757 /* "next" tuneable, if it is desired. */
6758 /* */
6759 /* This function is used to walk through all of the existing tunables with */
6760 /* successive calls. It searches the known tunables for the one which has */
6761 /* a matching value for "cookie" - ie its address. When returning a match, */
6762 /* the next one to be found may be returned inside next. */
6763 /* ------------------------------------------------------------------------ */
6764 static ipftuneable_t *
6765 ipf_tune_findbycookie(ipftuneable_t **ptop, void *cookie, void **next)
6766 {
6767 ipftuneable_t *ta, **tap;
6768
6769 for (ta = *ptop; ta->ipft_name != NULL; ta++)
6770 if (ta == cookie) {
6771 if (next != NULL) {
6772 /*
6773 * If the next entry in the array has a name
6774 * present, then return a pointer to it for
6775 * where to go next, else return a pointer to
6776 * the dynaminc list as a key to search there
6777 * next. This facilitates a weak linking of
6778 * the two "lists" together.
6779 */
6780 if ((ta + 1)->ipft_name != NULL)
6781 *next = ta + 1;
6782 else
6783 *next = ptop;
6784 }
6785 return (ta);
6786 }
6787
6788 for (tap = ptop; (ta = *tap) != NULL; tap = &ta->ipft_next)
6789 if (tap == cookie) {
6790 if (next != NULL)
6791 *next = &ta->ipft_next;
6792 return (ta);
6793 }
6794
6795 if (next != NULL)
6796 *next = NULL;
6797 return (NULL);
6798 }
6799
6800
6801 /* ------------------------------------------------------------------------ */
6802 /* Function: ipf_tune_findbyname */
6803 /* Returns: NULL = search failed, else pointer to tune struct */
6804 /* Parameters: name(I) - name of the tuneable entry to find. */
6805 /* */
6806 /* Search the static array of tuneables and the list of dynamic tuneables */
6807 /* for an entry with a matching name. If we can find one, return a pointer */
6808 /* to the matching structure. */
6809 /* ------------------------------------------------------------------------ */
6810 static ipftuneable_t *
6811 ipf_tune_findbyname(ipftuneable_t *top, const char *name)
6812 {
6813 ipftuneable_t *ta;
6814
6815 for (ta = top; ta != NULL; ta = ta->ipft_next)
6816 if (!strcmp(ta->ipft_name, name)) {
6817 return (ta);
6818 }
6819
6820 return (NULL);
6821 }
6822
6823
6824 /* ------------------------------------------------------------------------ */
6825 /* Function: ipf_tune_add_array */
6826 /* Returns: int - 0 == success, else failure */
6827 /* Parameters: newtune - pointer to new tune array to add to tuneables */
6828 /* */
6829 /* Appends tune structures from the array passed in (newtune) to the end of */
6830 /* the current list of "dynamic" tuneable parameters. */
6831 /* If any entry to be added is already present (by name) then the operation */
6832 /* is aborted - entries that have been added are removed before returning. */
6833 /* An entry with no name (NULL) is used as the indication that the end of */
6834 /* the array has been reached. */
6835 /* ------------------------------------------------------------------------ */
6836 int
6837 ipf_tune_add_array(ipf_main_softc_t *softc, ipftuneable_t *newtune)
6838 {
6839 ipftuneable_t *nt, *dt;
6840 int error = 0;
6841
6842 for (nt = newtune; nt->ipft_name != NULL; nt++) {
6843 error = ipf_tune_add(softc, nt);
6844 if (error != 0) {
6845 for (dt = newtune; dt != nt; dt++) {
6846 (void) ipf_tune_del(softc, dt);
6847 }
6848 }
6849 }
6850
6851 return (error);
6852 }
6853
6854
6855 /* ------------------------------------------------------------------------ */
6856 /* Function: ipf_tune_array_link */
6857 /* Returns: 0 == success, -1 == failure */
6858 /* Parameters: softc(I) - soft context pointerto work with */
6859 /* array(I) - pointer to an array of tuneables */
6860 /* */
6861 /* Given an array of tunables (array), append them to the current list of */
6862 /* tuneables for this context (softc->ipf_tuners.) To properly prepare the */
6863 /* the array for being appended to the list, initialise all of the next */
6864 /* pointers so we don't need to walk parts of it with ++ and others with */
6865 /* next. The array is expected to have an entry with a NULL name as the */
6866 /* terminator. Trying to add an array with no non-NULL names will return as */
6867 /* a failure. */
6868 /* ------------------------------------------------------------------------ */
6869 int
6870 ipf_tune_array_link(ipf_main_softc_t *softc, ipftuneable_t *array)
6871 {
6872 ipftuneable_t *t, **p;
6873
6874 t = array;
6875 if (t->ipft_name == NULL)
6876 return (-1);
6877
6878 for (; t[1].ipft_name != NULL; t++)
6879 t[0].ipft_next = &t[1];
6880 t->ipft_next = NULL;
6881
6882 /*
6883 * Since a pointer to the last entry isn't kept, we need to find it
6884 * each time we want to add new variables to the list.
6885 */
6886 for (p = &softc->ipf_tuners; (t = *p) != NULL; p = &t->ipft_next)
6887 if (t->ipft_name == NULL)
6888 break;
6889 *p = array;
6890
6891 return (0);
6892 }
6893
6894
6895 /* ------------------------------------------------------------------------ */
6896 /* Function: ipf_tune_array_unlink */
6897 /* Returns: 0 == success, -1 == failure */
6898 /* Parameters: softc(I) - soft context pointerto work with */
6899 /* array(I) - pointer to an array of tuneables */
6900 /* */
6901 /* ------------------------------------------------------------------------ */
6902 int
6903 ipf_tune_array_unlink(ipf_main_softc_t *softc, ipftuneable_t *array)
6904 {
6905 ipftuneable_t *t, **p;
6906
6907 for (p = &softc->ipf_tuners; (t = *p) != NULL; p = &t->ipft_next)
6908 if (t == array)
6909 break;
6910 if (t == NULL)
6911 return (-1);
6912
6913 for (; t[1].ipft_name != NULL; t++)
6914 ;
6915
6916 *p = t->ipft_next;
6917
6918 return (0);
6919 }
6920
6921
6922 /* ------------------------------------------------------------------------ */
6923 /* Function: ipf_tune_array_copy */
6924 /* Returns: NULL = failure, else pointer to new array */
6925 /* Parameters: base(I) - pointer to structure base */
6926 /* size(I) - size of the array at template */
6927 /* template(I) - original array to copy */
6928 /* */
6929 /* Allocate memory for a new set of tuneable values and copy everything */
6930 /* from template into the new region of memory. The new region is full of */
6931 /* uninitialised pointers (ipft_next) so set them up. Now, ipftp_offset... */
6932 /* */
6933 /* NOTE: the following assumes that sizeof(long) == sizeof(void *) */
6934 /* In the array template, ipftp_offset is the offset (in bytes) of the */
6935 /* location of the tuneable value inside the structure pointed to by base. */
6936 /* As ipftp_offset is a union over the pointers to the tuneable values, if */
6937 /* we add base to the copy's ipftp_offset, copy ends up with a pointer in */
6938 /* ipftp_void that points to the stored value. */
6939 /* ------------------------------------------------------------------------ */
6940 ipftuneable_t *
6941 ipf_tune_array_copy(void *base, size_t size, ipftuneable_t *template)
6942 {
6943 ipftuneable_t *copy;
6944 int i;
6945
6946
6947 KMALLOCS(copy, ipftuneable_t *, size);
6948 if (copy == NULL) {
6949 return (NULL);
6950 }
6951 bcopy(template, copy, size);
6952
6953 for (i = 0; copy[i].ipft_name; i++) {
6954 copy[i].ipft_una.ipftp_offset += (u_long)base;
6955 copy[i].ipft_next = copy + i + 1;
6956 }
6957
6958 return (copy);
6959 }
6960
6961
6962 /* ------------------------------------------------------------------------ */
6963 /* Function: ipf_tune_add */
6964 /* Returns: int - 0 == success, else failure */
6965 /* Parameters: newtune - pointer to new tune entry to add to tuneables */
6966 /* */
6967 /* Appends tune structures from the array passed in (newtune) to the end of */
6968 /* the current list of "dynamic" tuneable parameters. Once added, the */
6969 /* owner of the object is not expected to ever change "ipft_next". */
6970 /* ------------------------------------------------------------------------ */
6971 int
6972 ipf_tune_add(ipf_main_softc_t *softc, ipftuneable_t *newtune)
6973 {
6974 ipftuneable_t *ta, **tap;
6975
6976 ta = ipf_tune_findbyname(softc->ipf_tuners, newtune->ipft_name);
6977 if (ta != NULL) {
6978 IPFERROR(74);
6979 return (EEXIST);
6980 }
6981
6982 for (tap = &softc->ipf_tuners; *tap != NULL; tap = &(*tap)->ipft_next)
6983 ;
6984
6985 newtune->ipft_next = NULL;
6986 *tap = newtune;
6987 return (0);
6988 }
6989
6990
6991 /* ------------------------------------------------------------------------ */
6992 /* Function: ipf_tune_del */
6993 /* Returns: int - 0 == success, else failure */
6994 /* Parameters: oldtune - pointer to tune entry to remove from the list of */
6995 /* current dynamic tuneables */
6996 /* */
6997 /* Search for the tune structure, by pointer, in the list of those that are */
6998 /* dynamically added at run time. If found, adjust the list so that this */
6999 /* structure is no longer part of it. */
7000 /* ------------------------------------------------------------------------ */
7001 int
7002 ipf_tune_del(ipf_main_softc_t *softc, ipftuneable_t *oldtune)
7003 {
7004 ipftuneable_t *ta, **tap;
7005 int error = 0;
7006
7007 for (tap = &softc->ipf_tuners; (ta = *tap) != NULL;
7008 tap = &ta->ipft_next) {
7009 if (ta == oldtune) {
7010 *tap = oldtune->ipft_next;
7011 oldtune->ipft_next = NULL;
7012 break;
7013 }
7014 }
7015
7016 if (ta == NULL) {
7017 error = ESRCH;
7018 IPFERROR(75);
7019 }
7020 return (error);
7021 }
7022
7023
7024 /* ------------------------------------------------------------------------ */
7025 /* Function: ipf_tune_del_array */
7026 /* Returns: int - 0 == success, else failure */
7027 /* Parameters: oldtune - pointer to tuneables array */
7028 /* */
7029 /* Remove each tuneable entry in the array from the list of "dynamic" */
7030 /* tunables. If one entry should fail to be found, an error will be */
7031 /* returned and no further ones removed. */
7032 /* An entry with a NULL name is used as the indicator of the last entry in */
7033 /* the array. */
7034 /* ------------------------------------------------------------------------ */
7035 int
7036 ipf_tune_del_array(ipf_main_softc_t *softc, ipftuneable_t *oldtune)
7037 {
7038 ipftuneable_t *ot;
7039 int error = 0;
7040
7041 for (ot = oldtune; ot->ipft_name != NULL; ot++) {
7042 error = ipf_tune_del(softc, ot);
7043 if (error != 0)
7044 break;
7045 }
7046
7047 return (error);
7048
7049 }
7050
7051
7052 /* ------------------------------------------------------------------------ */
7053 /* Function: ipf_tune */
7054 /* Returns: int - 0 == success, else failure */
7055 /* Parameters: cmd(I) - ioctl command number */
7056 /* data(I) - pointer to ioctl data structure */
7057 /* */
7058 /* Implement handling of SIOCIPFGETNEXT, SIOCIPFGET and SIOCIPFSET. These */
7059 /* three ioctls provide the means to access and control global variables */
7060 /* within IPFilter, allowing (for example) timeouts and table sizes to be */
7061 /* changed without rebooting, reloading or recompiling. The initialisation */
7062 /* and 'destruction' routines of the various components of ipfilter are all */
7063 /* each responsible for handling their own values being too big. */
7064 /* ------------------------------------------------------------------------ */
7065 int
7066 ipf_ipftune(ipf_main_softc_t *softc, ioctlcmd_t cmd, void *data)
7067 {
7068 ipftuneable_t *ta;
7069 ipftune_t tu;
7070 void *cookie;
7071 int error;
7072
7073 error = ipf_inobj(softc, data, NULL, &tu, IPFOBJ_TUNEABLE);
7074 if (error != 0)
7075 return (error);
7076
7077 tu.ipft_name[sizeof(tu.ipft_name) - 1] = '\0';
7078 cookie = tu.ipft_cookie;
7079 ta = NULL;
7080
7081 switch (cmd)
7082 {
7083 case SIOCIPFGETNEXT :
7084 /*
7085 * If cookie is non-NULL, assume it to be a pointer to the last
7086 * entry we looked at, so find it (if possible) and return a
7087 * pointer to the next one after it. The last entry in the
7088 * the table is a NULL entry, so when we get to it, set cookie
7089 * to NULL and return that, indicating end of list, erstwhile
7090 * if we come in with cookie set to NULL, we are starting anew
7091 * at the front of the list.
7092 */
7093 if (cookie != NULL) {
7094 ta = ipf_tune_findbycookie(&softc->ipf_tuners,
7095 cookie, &tu.ipft_cookie);
7096 } else {
7097 ta = softc->ipf_tuners;
7098 tu.ipft_cookie = ta + 1;
7099 }
7100 if (ta != NULL) {
7101 /*
7102 * Entry found, but does the data pointed to by that
7103 * row fit in what we can return?
7104 */
7105 if (ta->ipft_sz > sizeof(tu.ipft_un)) {
7106 IPFERROR(76);
7107 return (EINVAL);
7108 }
7109
7110 tu.ipft_vlong = 0;
7111 if (ta->ipft_sz == sizeof(u_long))
7112 tu.ipft_vlong = *ta->ipft_plong;
7113 else if (ta->ipft_sz == sizeof(u_int))
7114 tu.ipft_vint = *ta->ipft_pint;
7115 else if (ta->ipft_sz == sizeof(u_short))
7116 tu.ipft_vshort = *ta->ipft_pshort;
7117 else if (ta->ipft_sz == sizeof(u_char))
7118 tu.ipft_vchar = *ta->ipft_pchar;
7119
7120 tu.ipft_sz = ta->ipft_sz;
7121 tu.ipft_min = ta->ipft_min;
7122 tu.ipft_max = ta->ipft_max;
7123 tu.ipft_flags = ta->ipft_flags;
7124 bcopy(ta->ipft_name, tu.ipft_name,
7125 MIN(sizeof(tu.ipft_name),
7126 strlen(ta->ipft_name) + 1));
7127 }
7128 error = ipf_outobj(softc, data, &tu, IPFOBJ_TUNEABLE);
7129 break;
7130
7131 case SIOCIPFGET :
7132 case SIOCIPFSET :
7133 /*
7134 * Search by name or by cookie value for a particular entry
7135 * in the tuning parameter table.
7136 */
7137 IPFERROR(77);
7138 error = ESRCH;
7139 if (cookie != NULL) {
7140 ta = ipf_tune_findbycookie(&softc->ipf_tuners,
7141 cookie, NULL);
7142 if (ta != NULL)
7143 error = 0;
7144 } else if (tu.ipft_name[0] != '\0') {
7145 ta = ipf_tune_findbyname(softc->ipf_tuners,
7146 tu.ipft_name);
7147 if (ta != NULL)
7148 error = 0;
7149 }
7150 if (error != 0)
7151 break;
7152
7153 if (cmd == (ioctlcmd_t)SIOCIPFGET) {
7154 /*
7155 * Fetch the tuning parameters for a particular value
7156 */
7157 tu.ipft_vlong = 0;
7158 if (ta->ipft_sz == sizeof(u_long))
7159 tu.ipft_vlong = *ta->ipft_plong;
7160 else if (ta->ipft_sz == sizeof(u_int))
7161 tu.ipft_vint = *ta->ipft_pint;
7162 else if (ta->ipft_sz == sizeof(u_short))
7163 tu.ipft_vshort = *ta->ipft_pshort;
7164 else if (ta->ipft_sz == sizeof(u_char))
7165 tu.ipft_vchar = *ta->ipft_pchar;
7166 tu.ipft_cookie = ta;
7167 tu.ipft_sz = ta->ipft_sz;
7168 tu.ipft_min = ta->ipft_min;
7169 tu.ipft_max = ta->ipft_max;
7170 tu.ipft_flags = ta->ipft_flags;
7171 error = ipf_outobj(softc, data, &tu, IPFOBJ_TUNEABLE);
7172
7173 } else if (cmd == (ioctlcmd_t)SIOCIPFSET) {
7174 /*
7175 * Set an internal parameter. The hard part here is
7176 * getting the new value safely and correctly out of
7177 * the kernel (given we only know its size, not type.)
7178 */
7179 u_long in;
7180
7181 if (((ta->ipft_flags & IPFT_WRDISABLED) != 0) &&
7182 (softc->ipf_running > 0)) {
7183 IPFERROR(78);
7184 error = EBUSY;
7185 break;
7186 }
7187
7188 in = tu.ipft_vlong;
7189 if (in < ta->ipft_min || in > ta->ipft_max) {
7190 IPFERROR(79);
7191 error = EINVAL;
7192 break;
7193 }
7194
7195 if (ta->ipft_func != NULL) {
7196 SPL_INT(s);
7197
7198 SPL_NET(s);
7199 error = (*ta->ipft_func)(softc, ta,
7200 &tu.ipft_un);
7201 SPL_X(s);
7202
7203 } else if (ta->ipft_sz == sizeof(u_long)) {
7204 tu.ipft_vlong = *ta->ipft_plong;
7205 *ta->ipft_plong = in;
7206
7207 } else if (ta->ipft_sz == sizeof(u_int)) {
7208 tu.ipft_vint = *ta->ipft_pint;
7209 *ta->ipft_pint = (u_int)(in & 0xffffffff);
7210
7211 } else if (ta->ipft_sz == sizeof(u_short)) {
7212 tu.ipft_vshort = *ta->ipft_pshort;
7213 *ta->ipft_pshort = (u_short)(in & 0xffff);
7214
7215 } else if (ta->ipft_sz == sizeof(u_char)) {
7216 tu.ipft_vchar = *ta->ipft_pchar;
7217 *ta->ipft_pchar = (u_char)(in & 0xff);
7218 }
7219 error = ipf_outobj(softc, data, &tu, IPFOBJ_TUNEABLE);
7220 }
7221 break;
7222
7223 default :
7224 IPFERROR(80);
7225 error = EINVAL;
7226 break;
7227 }
7228
7229 return (error);
7230 }
7231
7232
7233 /* ------------------------------------------------------------------------ */
7234 /* Function: ipf_zerostats */
7235 /* Returns: int - 0 = success, else failure */
7236 /* Parameters: data(O) - pointer to pointer for copying data back to */
7237 /* */
7238 /* Copies the current statistics out to userspace and then zero's the */
7239 /* current ones in the kernel. The lock is only held across the bzero() as */
7240 /* the copyout may result in paging (ie network activity.) */
7241 /* ------------------------------------------------------------------------ */
7242 int
7243 ipf_zerostats(ipf_main_softc_t *softc, caddr_t data)
7244 {
7245 friostat_t fio;
7246 ipfobj_t obj;
7247 int error;
7248
7249 error = ipf_inobj(softc, data, &obj, &fio, IPFOBJ_IPFSTAT);
7250 if (error != 0)
7251 return (error);
7252 ipf_getstat(softc, &fio, obj.ipfo_rev);
7253 error = ipf_outobj(softc, data, &fio, IPFOBJ_IPFSTAT);
7254 if (error != 0)
7255 return (error);
7256
7257 WRITE_ENTER(&softc->ipf_mutex);
7258 bzero(&softc->ipf_stats, sizeof(softc->ipf_stats));
7259 RWLOCK_EXIT(&softc->ipf_mutex);
7260
7261 return (0);
7262 }
7263
7264
7265 /* ------------------------------------------------------------------------ */
7266 /* Function: ipf_resolvedest */
7267 /* Returns: Nil */
7268 /* Parameters: softc(I) - pointer to soft context main structure */
7269 /* base(I) - where strings are stored */
7270 /* fdp(IO) - pointer to destination information to resolve */
7271 /* v(I) - IP protocol version to match */
7272 /* */
7273 /* Looks up an interface name in the frdest structure pointed to by fdp and */
7274 /* if a matching name can be found for the particular IP protocol version */
7275 /* then store the interface pointer in the frdest struct. If no match is */
7276 /* found, then set the interface pointer to be -1 as NULL is considered to */
7277 /* indicate there is no information at all in the structure. */
7278 /* ------------------------------------------------------------------------ */
7279 int
7280 ipf_resolvedest(ipf_main_softc_t *softc, char *base, frdest_t *fdp, int v)
7281 {
7282 int errval = 0;
7283 void *ifp;
7284
7285 ifp = NULL;
7286
7287 if (fdp->fd_name != -1) {
7288 if (fdp->fd_type == FRD_DSTLIST) {
7289 ifp = ipf_lookup_res_name(softc, IPL_LOGIPF,
7290 IPLT_DSTLIST,
7291 base + fdp->fd_name,
7292 NULL);
7293 if (ifp == NULL) {
7294 IPFERROR(144);
7295 errval = ESRCH;
7296 }
7297 } else {
7298 ifp = GETIFP(base + fdp->fd_name, v);
7299 if (ifp == NULL)
7300 ifp = (void *)-1;
7301 }
7302 }
7303 fdp->fd_ptr = ifp;
7304
7305 return (errval);
7306 }
7307
7308
7309 /* ------------------------------------------------------------------------ */
7310 /* Function: ipf_resolvenic */
7311 /* Returns: void* - NULL = wildcard name, -1 = failed to find NIC, else */
7312 /* pointer to interface structure for NIC */
7313 /* Parameters: softc(I)- pointer to soft context main structure */
7314 /* name(I) - complete interface name */
7315 /* v(I) - IP protocol version */
7316 /* */
7317 /* Look for a network interface structure that firstly has a matching name */
7318 /* to that passed in and that is also being used for that IP protocol */
7319 /* version (necessary on some platforms where there are separate listings */
7320 /* for both IPv4 and IPv6 on the same physical NIC. */
7321 /* ------------------------------------------------------------------------ */
7322 void *
7323 ipf_resolvenic(ipf_main_softc_t *softc, char *name, int v)
7324 {
7325 void *nic;
7326
7327 softc = softc; /* gcc -Wextra */
7328 if (name[0] == '\0')
7329 return (NULL);
7330
7331 if ((name[1] == '\0') && ((name[0] == '-') || (name[0] == '*'))) {
7332 return (NULL);
7333 }
7334
7335 nic = GETIFP(name, v);
7336 if (nic == NULL)
7337 nic = (void *)-1;
7338 return (nic);
7339 }
7340
7341
7342 /* ------------------------------------------------------------------------ */
7343 /* Function: ipf_token_expire */
7344 /* Returns: None. */
7345 /* Parameters: softc(I) - pointer to soft context main structure */
7346 /* */
7347 /* This function is run every ipf tick to see if there are any tokens that */
7348 /* have been held for too long and need to be freed up. */
7349 /* ------------------------------------------------------------------------ */
7350 void
7351 ipf_token_expire(ipf_main_softc_t *softc)
7352 {
7353 ipftoken_t *it;
7354
7355 WRITE_ENTER(&softc->ipf_tokens);
7356 while ((it = softc->ipf_token_head) != NULL) {
7357 if (it->ipt_die > softc->ipf_ticks)
7358 break;
7359
7360 ipf_token_deref(softc, it);
7361 }
7362 RWLOCK_EXIT(&softc->ipf_tokens);
7363 }
7364
7365
7366 /* ------------------------------------------------------------------------ */
7367 /* Function: ipf_token_flush */
7368 /* Returns: None. */
7369 /* Parameters: softc(I) - pointer to soft context main structure */
7370 /* */
7371 /* Loop through all of the existing tokens and call deref to see if they */
7372 /* can be freed. Normally a function like this might just loop on */
7373 /* ipf_token_head but there is a chance that a token might have a ref count */
7374 /* of greater than one and in that case the reference would drop twice */
7375 /* by code that is only entitled to drop it once. */
7376 /* ------------------------------------------------------------------------ */
7377 static void
7378 ipf_token_flush(ipf_main_softc_t *softc)
7379 {
7380 ipftoken_t *it, *next;
7381
7382 WRITE_ENTER(&softc->ipf_tokens);
7383 for (it = softc->ipf_token_head; it != NULL; it = next) {
7384 next = it->ipt_next;
7385 (void) ipf_token_deref(softc, it);
7386 }
7387 RWLOCK_EXIT(&softc->ipf_tokens);
7388 }
7389
7390
7391 /* ------------------------------------------------------------------------ */
7392 /* Function: ipf_token_del */
7393 /* Returns: int - 0 = success, else error */
7394 /* Parameters: softc(I)- pointer to soft context main structure */
7395 /* type(I) - the token type to match */
7396 /* uid(I) - uid owning the token */
7397 /* ptr(I) - context pointer for the token */
7398 /* */
7399 /* This function looks for a token in the current list that matches up */
7400 /* the fields (type, uid, ptr). If none is found, ESRCH is returned, else */
7401 /* call ipf_token_dewref() to remove it from the list. In the event that */
7402 /* the token has a reference held elsewhere, setting ipt_complete to 2 */
7403 /* enables debugging to distinguish between the two paths that ultimately */
7404 /* lead to a token to be deleted. */
7405 /* ------------------------------------------------------------------------ */
7406 int
7407 ipf_token_del(ipf_main_softc_t *softc, int type, int uid, void *ptr)
7408 {
7409 ipftoken_t *it;
7410 int error;
7411
7412 IPFERROR(82);
7413 error = ESRCH;
7414
7415 WRITE_ENTER(&softc->ipf_tokens);
7416 for (it = softc->ipf_token_head; it != NULL; it = it->ipt_next) {
7417 if (ptr == it->ipt_ctx && type == it->ipt_type &&
7418 uid == it->ipt_uid) {
7419 it->ipt_complete = 2;
7420 ipf_token_deref(softc, it);
7421 error = 0;
7422 break;
7423 }
7424 }
7425 RWLOCK_EXIT(&softc->ipf_tokens);
7426
7427 return (error);
7428 }
7429
7430
7431 /* ------------------------------------------------------------------------ */
7432 /* Function: ipf_token_mark_complete */
7433 /* Returns: None. */
7434 /* Parameters: token(I) - pointer to token structure */
7435 /* */
7436 /* Mark a token as being ineligable for being found with ipf_token_find. */
7437 /* ------------------------------------------------------------------------ */
7438 void
7439 ipf_token_mark_complete(ipftoken_t *token)
7440 {
7441 if (token->ipt_complete == 0)
7442 token->ipt_complete = 1;
7443 }
7444
7445
7446 /* ------------------------------------------------------------------------ */
7447 /* Function: ipf_token_find */
7448 /* Returns: ipftoken_t * - NULL if no memory, else pointer to token */
7449 /* Parameters: softc(I)- pointer to soft context main structure */
7450 /* type(I) - the token type to match */
7451 /* uid(I) - uid owning the token */
7452 /* ptr(I) - context pointer for the token */
7453 /* */
7454 /* This function looks for a live token in the list of current tokens that */
7455 /* matches the tuple (type, uid, ptr). If one cannot be found then one is */
7456 /* allocated. If one is found then it is moved to the top of the list of */
7457 /* currently active tokens. */
7458 /* ------------------------------------------------------------------------ */
7459 ipftoken_t *
7460 ipf_token_find(ipf_main_softc_t *softc, int type, int uid, void *ptr)
7461 {
7462 ipftoken_t *it, *new;
7463
7464 WRITE_ENTER(&softc->ipf_tokens);
7465 for (it = softc->ipf_token_head; it != NULL; it = it->ipt_next) {
7466 if ((ptr == it->ipt_ctx) && (type == it->ipt_type) &&
7467 (uid == it->ipt_uid) && (it->ipt_complete < 2))
7468 break;
7469 }
7470
7471 if (it == NULL) {
7472 KMALLOC(new, ipftoken_t *);
7473 if (new != NULL)
7474 bzero((char *)new, sizeof(*new));
7475
7476 it = new;
7477 new = NULL;
7478 if (it == NULL) {
7479 RWLOCK_EXIT(&softc->ipf_tokens);
7480 return (NULL);
7481 }
7482 it->ipt_ctx = ptr;
7483 it->ipt_uid = uid;
7484 it->ipt_type = type;
7485 it->ipt_ref = 1;
7486 } else {
7487 if (it->ipt_complete > 0)
7488 it = NULL;
7489 else
7490 ipf_token_unlink(softc, it);
7491 }
7492
7493 if (it != NULL) {
7494 it->ipt_pnext = softc->ipf_token_tail;
7495 *softc->ipf_token_tail = it;
7496 softc->ipf_token_tail = &it->ipt_next;
7497 it->ipt_next = NULL;
7498 it->ipt_ref++;
7499
7500 it->ipt_die = softc->ipf_ticks + 20;
7501 }
7502
7503 RWLOCK_EXIT(&softc->ipf_tokens);
7504
7505 return (it);
7506 }
7507
7508
7509 /* ------------------------------------------------------------------------ */
7510 /* Function: ipf_token_unlink */
7511 /* Returns: None. */
7512 /* Parameters: softc(I) - pointer to soft context main structure */
7513 /* token(I) - pointer to token structure */
7514 /* Write Locks: ipf_tokens */
7515 /* */
7516 /* This function unlinks a token structure from the linked list of tokens */
7517 /* that "own" it. The head pointer never needs to be explicitly adjusted */
7518 /* but the tail does due to the linked list implementation. */
7519 /* ------------------------------------------------------------------------ */
7520 static void
7521 ipf_token_unlink(ipf_main_softc_t *softc, ipftoken_t *token)
7522 {
7523
7524 if (softc->ipf_token_tail == &token->ipt_next)
7525 softc->ipf_token_tail = token->ipt_pnext;
7526
7527 *token->ipt_pnext = token->ipt_next;
7528 if (token->ipt_next != NULL)
7529 token->ipt_next->ipt_pnext = token->ipt_pnext;
7530 token->ipt_next = NULL;
7531 token->ipt_pnext = NULL;
7532 }
7533
7534
7535 /* ------------------------------------------------------------------------ */
7536 /* Function: ipf_token_deref */
7537 /* Returns: int - 0 == token freed, else reference count */
7538 /* Parameters: softc(I) - pointer to soft context main structure */
7539 /* token(I) - pointer to token structure */
7540 /* Write Locks: ipf_tokens */
7541 /* */
7542 /* Drop the reference count on the token structure and if it drops to zero, */
7543 /* call the dereference function for the token type because it is then */
7544 /* possible to free the token data structure. */
7545 /* ------------------------------------------------------------------------ */
7546 int
7547 ipf_token_deref(ipf_main_softc_t *softc, ipftoken_t *token)
7548 {
7549 void *data, **datap;
7550
7551 ASSERT(token->ipt_ref > 0);
7552 token->ipt_ref--;
7553 if (token->ipt_ref > 0)
7554 return (token->ipt_ref);
7555
7556 data = token->ipt_data;
7557 datap = &data;
7558
7559 if ((data != NULL) && (data != (void *)-1)) {
7560 switch (token->ipt_type)
7561 {
7562 case IPFGENITER_IPF :
7563 (void) ipf_derefrule(softc, (frentry_t **)datap);
7564 break;
7565 case IPFGENITER_IPNAT :
7566 WRITE_ENTER(&softc->ipf_nat);
7567 ipf_nat_rule_deref(softc, (ipnat_t **)datap);
7568 RWLOCK_EXIT(&softc->ipf_nat);
7569 break;
7570 case IPFGENITER_NAT :
7571 ipf_nat_deref(softc, (nat_t **)datap);
7572 break;
7573 case IPFGENITER_STATE :
7574 ipf_state_deref(softc, (ipstate_t **)datap);
7575 break;
7576 case IPFGENITER_FRAG :
7577 ipf_frag_pkt_deref(softc, (ipfr_t **)datap);
7578 break;
7579 case IPFGENITER_NATFRAG :
7580 ipf_frag_nat_deref(softc, (ipfr_t **)datap);
7581 break;
7582 case IPFGENITER_HOSTMAP :
7583 WRITE_ENTER(&softc->ipf_nat);
7584 ipf_nat_hostmapdel(softc, (hostmap_t **)datap);
7585 RWLOCK_EXIT(&softc->ipf_nat);
7586 break;
7587 default :
7588 ipf_lookup_iterderef(softc, token->ipt_type, data);
7589 break;
7590 }
7591 }
7592
7593 ipf_token_unlink(softc, token);
7594 KFREE(token);
7595 return (0);
7596 }
7597
7598
7599 /* ------------------------------------------------------------------------ */
7600 /* Function: ipf_nextrule */
7601 /* Returns: frentry_t * - NULL == no more rules, else pointer to next */
7602 /* Parameters: softc(I) - pointer to soft context main structure */
7603 /* fr(I) - pointer to filter rule */
7604 /* out(I) - 1 == out rules, 0 == input rules */
7605 /* */
7606 /* Starting with "fr", find the next rule to visit. This includes visiting */
7607 /* the list of rule groups if either fr is NULL (empty list) or it is the */
7608 /* last rule in the list. When walking rule lists, it is either input or */
7609 /* output rules that are returned, never both. */
7610 /* ------------------------------------------------------------------------ */
7611 static frentry_t *
7612 ipf_nextrule(ipf_main_softc_t *softc, int active, int unit, frentry_t *fr,
7613 int out)
7614 {
7615 frentry_t *next;
7616 frgroup_t *fg;
7617
7618 if (fr != NULL && fr->fr_group != -1) {
7619 fg = ipf_findgroup(softc, fr->fr_names + fr->fr_group,
7620 unit, active, NULL);
7621 if (fg != NULL)
7622 fg = fg->fg_next;
7623 } else {
7624 fg = softc->ipf_groups[unit][active];
7625 }
7626
7627 while (fg != NULL) {
7628 next = fg->fg_start;
7629 while (next != NULL) {
7630 if (out) {
7631 if (next->fr_flags & FR_OUTQUE)
7632 return (next);
7633 } else if (next->fr_flags & FR_INQUE) {
7634 return (next);
7635 }
7636 next = next->fr_next;
7637 }
7638 if (next == NULL)
7639 fg = fg->fg_next;
7640 }
7641
7642 return (NULL);
7643 }
7644
7645 /* ------------------------------------------------------------------------ */
7646 /* Function: ipf_getnextrule */
7647 /* Returns: int - 0 = success, else error */
7648 /* Parameters: softc(I)- pointer to soft context main structure */
7649 /* t(I) - pointer to destination information to resolve */
7650 /* ptr(I) - pointer to ipfobj_t to copyin from user space */
7651 /* */
7652 /* This function's first job is to bring in the ipfruleiter_t structure via */
7653 /* the ipfobj_t structure to determine what should be the next rule to */
7654 /* return. Once the ipfruleiter_t has been brought in, it then tries to */
7655 /* find the 'next rule'. This may include searching rule group lists or */
7656 /* just be as simple as looking at the 'next' field in the rule structure. */
7657 /* When we have found the rule to return, increase its reference count and */
7658 /* if we used an existing rule to get here, decrease its reference count. */
7659 /* ------------------------------------------------------------------------ */
7660 int
7661 ipf_getnextrule(ipf_main_softc_t *softc, ipftoken_t *t, void *ptr)
7662 {
7663 frentry_t *fr, *next, zero;
7664 ipfruleiter_t it;
7665 int error, out;
7666 frgroup_t *fg;
7667 ipfobj_t obj;
7668 int predict;
7669 char *dst;
7670 int unit;
7671
7672 if (t == NULL || ptr == NULL) {
7673 IPFERROR(84);
7674 return (EFAULT);
7675 }
7676
7677 error = ipf_inobj(softc, ptr, &obj, &it, IPFOBJ_IPFITER);
7678 if (error != 0)
7679 return (error);
7680
7681 if ((it.iri_inout < 0) || (it.iri_inout > 3)) {
7682 IPFERROR(85);
7683 return (EINVAL);
7684 }
7685 if ((it.iri_active != 0) && (it.iri_active != 1)) {
7686 IPFERROR(86);
7687 return (EINVAL);
7688 }
7689 if (it.iri_nrules == 0) {
7690 IPFERROR(87);
7691 return (ENOSPC);
7692 }
7693 if (it.iri_rule == NULL) {
7694 IPFERROR(88);
7695 return (EFAULT);
7696 }
7697
7698 fg = NULL;
7699 fr = t->ipt_data;
7700 if ((it.iri_inout & F_OUT) != 0)
7701 out = 1;
7702 else
7703 out = 0;
7704 if ((it.iri_inout & F_ACIN) != 0)
7705 unit = IPL_LOGCOUNT;
7706 else
7707 unit = IPL_LOGIPF;
7708
7709 READ_ENTER(&softc->ipf_mutex);
7710 if (fr == NULL) {
7711 if (*it.iri_group == '\0') {
7712 if (unit == IPL_LOGCOUNT) {
7713 next = softc->ipf_acct[out][it.iri_active];
7714 } else {
7715 next = softc->ipf_rules[out][it.iri_active];
7716 }
7717 if (next == NULL)
7718 next = ipf_nextrule(softc, it.iri_active,
7719 unit, NULL, out);
7720 } else {
7721 fg = ipf_findgroup(softc, it.iri_group, unit,
7722 it.iri_active, NULL);
7723 if (fg != NULL)
7724 next = fg->fg_start;
7725 else
7726 next = NULL;
7727 }
7728 } else {
7729 next = fr->fr_next;
7730 if (next == NULL)
7731 next = ipf_nextrule(softc, it.iri_active, unit,
7732 fr, out);
7733 }
7734
7735 if (next != NULL && next->fr_next != NULL)
7736 predict = 1;
7737 else if (ipf_nextrule(softc, it.iri_active, unit, next, out) != NULL)
7738 predict = 1;
7739 else
7740 predict = 0;
7741
7742 if (fr != NULL)
7743 (void) ipf_derefrule(softc, &fr);
7744
7745 obj.ipfo_type = IPFOBJ_FRENTRY;
7746 dst = (char *)it.iri_rule;
7747
7748 if (next != NULL) {
7749 obj.ipfo_size = next->fr_size;
7750 MUTEX_ENTER(&next->fr_lock);
7751 next->fr_ref++;
7752 MUTEX_EXIT(&next->fr_lock);
7753 t->ipt_data = next;
7754 } else {
7755 obj.ipfo_size = sizeof(frentry_t);
7756 bzero(&zero, sizeof(zero));
7757 next = &zero;
7758 t->ipt_data = NULL;
7759 }
7760 it.iri_rule = predict ? next : NULL;
7761 if (predict == 0)
7762 ipf_token_mark_complete(t);
7763
7764 RWLOCK_EXIT(&softc->ipf_mutex);
7765
7766 obj.ipfo_ptr = dst;
7767 error = ipf_outobjk(softc, &obj, next);
7768 if (error == 0 && t->ipt_data != NULL) {
7769 dst += obj.ipfo_size;
7770 if (next->fr_data != NULL) {
7771 ipfobj_t dobj;
7772
7773 if (next->fr_type == FR_T_IPFEXPR)
7774 dobj.ipfo_type = IPFOBJ_IPFEXPR;
7775 else
7776 dobj.ipfo_type = IPFOBJ_FRIPF;
7777 dobj.ipfo_size = next->fr_dsize;
7778 dobj.ipfo_rev = obj.ipfo_rev;
7779 dobj.ipfo_ptr = dst;
7780 error = ipf_outobjk(softc, &dobj, next->fr_data);
7781 }
7782 }
7783
7784 if ((fr != NULL) && (next == &zero))
7785 (void) ipf_derefrule(softc, &fr);
7786
7787 return (error);
7788 }
7789
7790
7791 /* ------------------------------------------------------------------------ */
7792 /* Function: ipf_frruleiter */
7793 /* Returns: int - 0 = success, else error */
7794 /* Parameters: softc(I)- pointer to soft context main structure */
7795 /* data(I) - the token type to match */
7796 /* uid(I) - uid owning the token */
7797 /* ptr(I) - context pointer for the token */
7798 /* */
7799 /* This function serves as a stepping stone between ipf_ipf_ioctl and */
7800 /* ipf_getnextrule. It's role is to find the right token in the kernel for */
7801 /* the process doing the ioctl and use that to ask for the next rule. */
7802 /* ------------------------------------------------------------------------ */
7803 static int
7804 ipf_frruleiter(ipf_main_softc_t *softc, void *data, int uid, void *ctx)
7805 {
7806 ipftoken_t *token;
7807 ipfruleiter_t it;
7808 ipfobj_t obj;
7809 int error;
7810
7811 token = ipf_token_find(softc, IPFGENITER_IPF, uid, ctx);
7812 if (token != NULL) {
7813 error = ipf_getnextrule(softc, token, data);
7814 WRITE_ENTER(&softc->ipf_tokens);
7815 ipf_token_deref(softc, token);
7816 RWLOCK_EXIT(&softc->ipf_tokens);
7817 } else {
7818 error = ipf_inobj(softc, data, &obj, &it, IPFOBJ_IPFITER);
7819 if (error != 0)
7820 return (error);
7821 it.iri_rule = NULL;
7822 error = ipf_outobj(softc, data, &it, IPFOBJ_IPFITER);
7823 }
7824
7825 return (error);
7826 }
7827
7828
7829 /* ------------------------------------------------------------------------ */
7830 /* Function: ipf_geniter */
7831 /* Returns: int - 0 = success, else error */
7832 /* Parameters: softc(I) - pointer to soft context main structure */
7833 /* token(I) - pointer to ipftoken_t structure */
7834 /* itp(I) - pointer to iterator data */
7835 /* */
7836 /* Decide which iterator function to call using information passed through */
7837 /* the ipfgeniter_t structure at itp. */
7838 /* ------------------------------------------------------------------------ */
7839 static int
7840 ipf_geniter(ipf_main_softc_t *softc, ipftoken_t *token, ipfgeniter_t *itp)
7841 {
7842 int error;
7843
7844 switch (itp->igi_type)
7845 {
7846 case IPFGENITER_FRAG :
7847 error = ipf_frag_pkt_next(softc, token, itp);
7848 break;
7849 default :
7850 IPFERROR(92);
7851 error = EINVAL;
7852 break;
7853 }
7854
7855 return (error);
7856 }
7857
7858
7859 /* ------------------------------------------------------------------------ */
7860 /* Function: ipf_genericiter */
7861 /* Returns: int - 0 = success, else error */
7862 /* Parameters: softc(I)- pointer to soft context main structure */
7863 /* data(I) - the token type to match */
7864 /* uid(I) - uid owning the token */
7865 /* ptr(I) - context pointer for the token */
7866 /* */
7867 /* Handle the SIOCGENITER ioctl for the ipfilter device. The primary role */
7868 /* ------------------------------------------------------------------------ */
7869 int
7870 ipf_genericiter(ipf_main_softc_t *softc, void *data, int uid, void *ctx)
7871 {
7872 ipftoken_t *token;
7873 ipfgeniter_t iter;
7874 int error;
7875
7876 error = ipf_inobj(softc, data, NULL, &iter, IPFOBJ_GENITER);
7877 if (error != 0)
7878 return (error);
7879
7880 token = ipf_token_find(softc, iter.igi_type, uid, ctx);
7881 if (token != NULL) {
7882 token->ipt_subtype = iter.igi_type;
7883 error = ipf_geniter(softc, token, &iter);
7884 WRITE_ENTER(&softc->ipf_tokens);
7885 ipf_token_deref(softc, token);
7886 RWLOCK_EXIT(&softc->ipf_tokens);
7887 } else {
7888 IPFERROR(93);
7889 error = 0;
7890 }
7891
7892 return (error);
7893 }
7894
7895
7896 /* ------------------------------------------------------------------------ */
7897 /* Function: ipf_ipf_ioctl */
7898 /* Returns: int - 0 = success, else error */
7899 /* Parameters: softc(I)- pointer to soft context main structure */
7900 /* data(I) - the token type to match */
7901 /* cmd(I) - the ioctl command number */
7902 /* mode(I) - mode flags for the ioctl */
7903 /* uid(I) - uid owning the token */
7904 /* ptr(I) - context pointer for the token */
7905 /* */
7906 /* This function handles all of the ioctl command that are actually isssued */
7907 /* to the /dev/ipl device. */
7908 /* ------------------------------------------------------------------------ */
7909 int
7910 ipf_ipf_ioctl(ipf_main_softc_t *softc, caddr_t data, ioctlcmd_t cmd, int mode,
7911 int uid, void *ctx)
7912 {
7913 friostat_t fio;
7914 int error, tmp;
7915 ipfobj_t obj;
7916 SPL_INT(s);
7917
7918 switch (cmd)
7919 {
7920 case SIOCFRENB :
7921 if (!(mode & FWRITE)) {
7922 IPFERROR(94);
7923 error = EPERM;
7924 } else {
7925 error = BCOPYIN(data, &tmp, sizeof(tmp));
7926 if (error != 0) {
7927 IPFERROR(95);
7928 error = EFAULT;
7929 break;
7930 }
7931
7932 WRITE_ENTER(&softc->ipf_global);
7933 if (tmp) {
7934 if (softc->ipf_running > 0)
7935 error = 0;
7936 else
7937 error = ipfattach(softc);
7938 if (error == 0)
7939 softc->ipf_running = 1;
7940 else
7941 (void) ipfdetach(softc);
7942 } else {
7943 if (softc->ipf_running == 1)
7944 error = ipfdetach(softc);
7945 else
7946 error = 0;
7947 if (error == 0)
7948 softc->ipf_running = -1;
7949 }
7950 RWLOCK_EXIT(&softc->ipf_global);
7951 }
7952 break;
7953
7954 case SIOCIPFSET :
7955 if (!(mode & FWRITE)) {
7956 IPFERROR(96);
7957 error = EPERM;
7958 break;
7959 }
7960 /* FALLTHRU */
7961 case SIOCIPFGETNEXT :
7962 case SIOCIPFGET :
7963 error = ipf_ipftune(softc, cmd, (void *)data);
7964 break;
7965
7966 case SIOCSETFF :
7967 if (!(mode & FWRITE)) {
7968 IPFERROR(97);
7969 error = EPERM;
7970 } else {
7971 error = BCOPYIN(data, &softc->ipf_flags,
7972 sizeof(softc->ipf_flags));
7973 if (error != 0) {
7974 IPFERROR(98);
7975 error = EFAULT;
7976 }
7977 }
7978 break;
7979
7980 case SIOCGETFF :
7981 error = BCOPYOUT(&softc->ipf_flags, data,
7982 sizeof(softc->ipf_flags));
7983 if (error != 0) {
7984 IPFERROR(99);
7985 error = EFAULT;
7986 }
7987 break;
7988
7989 case SIOCFUNCL :
7990 error = ipf_resolvefunc(softc, (void *)data);
7991 break;
7992
7993 case SIOCINAFR :
7994 case SIOCRMAFR :
7995 case SIOCADAFR :
7996 case SIOCZRLST :
7997 if (!(mode & FWRITE)) {
7998 IPFERROR(100);
7999 error = EPERM;
8000 } else {
8001 error = frrequest(softc, IPL_LOGIPF, cmd, (caddr_t)data,
8002 softc->ipf_active, 1);
8003 }
8004 break;
8005
8006 case SIOCINIFR :
8007 case SIOCRMIFR :
8008 case SIOCADIFR :
8009 if (!(mode & FWRITE)) {
8010 IPFERROR(101);
8011 error = EPERM;
8012 } else {
8013 error = frrequest(softc, IPL_LOGIPF, cmd, (caddr_t)data,
8014 1 - softc->ipf_active, 1);
8015 }
8016 break;
8017
8018 case SIOCSWAPA :
8019 if (!(mode & FWRITE)) {
8020 IPFERROR(102);
8021 error = EPERM;
8022 } else {
8023 WRITE_ENTER(&softc->ipf_mutex);
8024 error = BCOPYOUT(&softc->ipf_active, data,
8025 sizeof(softc->ipf_active));
8026 if (error != 0) {
8027 IPFERROR(103);
8028 error = EFAULT;
8029 } else {
8030 softc->ipf_active = 1 - softc->ipf_active;
8031 }
8032 RWLOCK_EXIT(&softc->ipf_mutex);
8033 }
8034 break;
8035
8036 case SIOCGETFS :
8037 error = ipf_inobj(softc, (void *)data, &obj, &fio,
8038 IPFOBJ_IPFSTAT);
8039 if (error != 0)
8040 break;
8041 ipf_getstat(softc, &fio, obj.ipfo_rev);
8042 error = ipf_outobj(softc, (void *)data, &fio, IPFOBJ_IPFSTAT);
8043 break;
8044
8045 case SIOCFRZST :
8046 if (!(mode & FWRITE)) {
8047 IPFERROR(104);
8048 error = EPERM;
8049 } else
8050 error = ipf_zerostats(softc, (caddr_t)data);
8051 break;
8052
8053 case SIOCIPFFL :
8054 if (!(mode & FWRITE)) {
8055 IPFERROR(105);
8056 error = EPERM;
8057 } else {
8058 error = BCOPYIN(data, &tmp, sizeof(tmp));
8059 if (!error) {
8060 tmp = ipf_flush(softc, IPL_LOGIPF, tmp);
8061 error = BCOPYOUT(&tmp, data, sizeof(tmp));
8062 if (error != 0) {
8063 IPFERROR(106);
8064 error = EFAULT;
8065 }
8066 } else {
8067 IPFERROR(107);
8068 error = EFAULT;
8069 }
8070 }
8071 break;
8072
8073 #ifdef USE_INET6
8074 case SIOCIPFL6 :
8075 if (!(mode & FWRITE)) {
8076 IPFERROR(108);
8077 error = EPERM;
8078 } else {
8079 error = BCOPYIN(data, &tmp, sizeof(tmp));
8080 if (!error) {
8081 tmp = ipf_flush(softc, IPL_LOGIPF, tmp);
8082 error = BCOPYOUT(&tmp, data, sizeof(tmp));
8083 if (error != 0) {
8084 IPFERROR(109);
8085 error = EFAULT;
8086 }
8087 } else {
8088 IPFERROR(110);
8089 error = EFAULT;
8090 }
8091 }
8092 break;
8093 #endif
8094
8095 case SIOCSTLCK :
8096 if (!(mode & FWRITE)) {
8097 IPFERROR(122);
8098 error = EPERM;
8099 } else {
8100 error = BCOPYIN(data, &tmp, sizeof(tmp));
8101 if (error == 0) {
8102 ipf_state_setlock(softc->ipf_state_soft, tmp);
8103 ipf_nat_setlock(softc->ipf_nat_soft, tmp);
8104 ipf_frag_setlock(softc->ipf_frag_soft, tmp);
8105 ipf_auth_setlock(softc->ipf_auth_soft, tmp);
8106 } else {
8107 IPFERROR(111);
8108 error = EFAULT;
8109 }
8110 }
8111 break;
8112
8113 #ifdef IPFILTER_LOG
8114 case SIOCIPFFB :
8115 if (!(mode & FWRITE)) {
8116 IPFERROR(112);
8117 error = EPERM;
8118 } else {
8119 tmp = ipf_log_clear(softc, IPL_LOGIPF);
8120 error = BCOPYOUT(&tmp, data, sizeof(tmp));
8121 if (error) {
8122 IPFERROR(113);
8123 error = EFAULT;
8124 }
8125 }
8126 break;
8127 #endif /* IPFILTER_LOG */
8128
8129 case SIOCFRSYN :
8130 if (!(mode & FWRITE)) {
8131 IPFERROR(114);
8132 error = EPERM;
8133 } else {
8134 WRITE_ENTER(&softc->ipf_global);
8135 #if (SOLARIS && defined(_KERNEL)) && !defined(INSTANCES)
8136 error = ipfsync();
8137 #else
8138 ipf_sync(softc, NULL);
8139 error = 0;
8140 #endif
8141 RWLOCK_EXIT(&softc->ipf_global);
8142
8143 }
8144 break;
8145
8146 case SIOCGFRST :
8147 error = ipf_outobj(softc, (void *)data,
8148 ipf_frag_stats(softc->ipf_frag_soft),
8149 IPFOBJ_FRAGSTAT);
8150 break;
8151
8152 #ifdef IPFILTER_LOG
8153 case FIONREAD :
8154 tmp = ipf_log_bytesused(softc, IPL_LOGIPF);
8155 error = BCOPYOUT(&tmp, data, sizeof(tmp));
8156 break;
8157 #endif
8158
8159 case SIOCIPFITER :
8160 SPL_SCHED(s);
8161 error = ipf_frruleiter(softc, data, uid, ctx);
8162 SPL_X(s);
8163 break;
8164
8165 case SIOCGENITER :
8166 SPL_SCHED(s);
8167 error = ipf_genericiter(softc, data, uid, ctx);
8168 SPL_X(s);
8169 break;
8170
8171 case SIOCIPFDELTOK :
8172 error = BCOPYIN(data, &tmp, sizeof(tmp));
8173 if (error == 0) {
8174 SPL_SCHED(s);
8175 error = ipf_token_del(softc, tmp, uid, ctx);
8176 SPL_X(s);
8177 }
8178 break;
8179
8180 default :
8181 IPFERROR(115);
8182 error = EINVAL;
8183 break;
8184 }
8185
8186 return (error);
8187 }
8188
8189
8190 /* ------------------------------------------------------------------------ */
8191 /* Function: ipf_decaps */
8192 /* Returns: int - -1 == decapsulation failed, else bit mask of */
8193 /* flags indicating packet filtering decision. */
8194 /* Parameters: fin(I) - pointer to packet information */
8195 /* pass(I) - IP protocol version to match */
8196 /* l5proto(I) - layer 5 protocol to decode UDP data as. */
8197 /* */
8198 /* This function is called for packets that are wrapt up in other packets, */
8199 /* for example, an IP packet that is the entire data segment for another IP */
8200 /* packet. If the basic constraints for this are satisfied, change the */
8201 /* buffer to point to the start of the inner packet and start processing */
8202 /* rules belonging to the head group this rule specifies. */
8203 /* ------------------------------------------------------------------------ */
8204 u_32_t
8205 ipf_decaps(fr_info_t *fin, u_32_t pass, int l5proto)
8206 {
8207 fr_info_t fin2, *fino = NULL;
8208 int elen, hlen, nh;
8209 grehdr_t gre;
8210 ip_t *ip;
8211 mb_t *m;
8212
8213 if ((fin->fin_flx & FI_COALESCE) == 0)
8214 if (ipf_coalesce(fin) == -1)
8215 goto cantdecaps;
8216
8217 m = fin->fin_m;
8218 hlen = fin->fin_hlen;
8219
8220 switch (fin->fin_p)
8221 {
8222 case IPPROTO_UDP :
8223 /*
8224 * In this case, the specific protocol being decapsulated
8225 * inside UDP frames comes from the rule.
8226 */
8227 nh = fin->fin_fr->fr_icode;
8228 break;
8229
8230 case IPPROTO_GRE : /* 47 */
8231 bcopy(fin->fin_dp, (char *)&gre, sizeof(gre));
8232 hlen += sizeof(grehdr_t);
8233 if (gre.gr_R|gre.gr_s)
8234 goto cantdecaps;
8235 if (gre.gr_C)
8236 hlen += 4;
8237 if (gre.gr_K)
8238 hlen += 4;
8239 if (gre.gr_S)
8240 hlen += 4;
8241
8242 nh = IPPROTO_IP;
8243
8244 /*
8245 * If the routing options flag is set, validate that it is
8246 * there and bounce over it.
8247 */
8248 #if 0
8249 /* This is really heavy weight and lots of room for error, */
8250 /* so for now, put it off and get the simple stuff right. */
8251 if (gre.gr_R) {
8252 u_char off, len, *s;
8253 u_short af;
8254 int end;
8255
8256 end = 0;
8257 s = fin->fin_dp;
8258 s += hlen;
8259 aplen = fin->fin_plen - hlen;
8260 while (aplen > 3) {
8261 af = (s[0] << 8) | s[1];
8262 off = s[2];
8263 len = s[3];
8264 aplen -= 4;
8265 s += 4;
8266 if (af == 0 && len == 0) {
8267 end = 1;
8268 break;
8269 }
8270 if (aplen < len)
8271 break;
8272 s += len;
8273 aplen -= len;
8274 }
8275 if (end != 1)
8276 goto cantdecaps;
8277 hlen = s - (u_char *)fin->fin_dp;
8278 }
8279 #endif
8280 break;
8281
8282 #ifdef IPPROTO_IPIP
8283 case IPPROTO_IPIP : /* 4 */
8284 #endif
8285 nh = IPPROTO_IP;
8286 break;
8287
8288 default : /* Includes ESP, AH is special for IPv4 */
8289 goto cantdecaps;
8290 }
8291
8292 switch (nh)
8293 {
8294 case IPPROTO_IP :
8295 case IPPROTO_IPV6 :
8296 break;
8297 default :
8298 goto cantdecaps;
8299 }
8300
8301 bcopy((char *)fin, (char *)&fin2, sizeof(fin2));
8302 fino = fin;
8303 fin = &fin2;
8304 elen = hlen;
8305 #if SOLARIS && defined(_KERNEL)
8306 m->b_rptr += elen;
8307 #else
8308 m->m_data += elen;
8309 m->m_len -= elen;
8310 #endif
8311 fin->fin_plen -= elen;
8312
8313 ip = (ip_t *)((char *)fin->fin_ip + elen);
8314
8315 /*
8316 * Make sure we have at least enough data for the network layer
8317 * header.
8318 */
8319 if (IP_V(ip) == 4)
8320 hlen = IP_HL(ip) << 2;
8321 #ifdef USE_INET6
8322 else if (IP_V(ip) == 6)
8323 hlen = sizeof(ip6_t);
8324 #endif
8325 else
8326 goto cantdecaps2;
8327
8328 if (fin->fin_plen < hlen)
8329 goto cantdecaps2;
8330
8331 fin->fin_dp = (char *)ip + hlen;
8332
8333 if (IP_V(ip) == 4) {
8334 /*
8335 * Perform IPv4 header checksum validation.
8336 */
8337 if (ipf_cksum((u_short *)ip, hlen))
8338 goto cantdecaps2;
8339 }
8340
8341 if (ipf_makefrip(hlen, ip, fin) == -1) {
8342 cantdecaps2:
8343 if (m != NULL) {
8344 #if SOLARIS && defined(_KERNEL)
8345 m->b_rptr -= elen;
8346 #else
8347 m->m_data -= elen;
8348 m->m_len += elen;
8349 #endif
8350 }
8351 cantdecaps:
8352 DT1(frb_decapfrip, fr_info_t *, fin);
8353 pass &= ~FR_CMDMASK;
8354 pass |= FR_BLOCK|FR_QUICK;
8355 fin->fin_reason = FRB_DECAPFRIP;
8356 return (-1);
8357 }
8358
8359 pass = ipf_scanlist(fin, pass);
8360
8361 /*
8362 * Copy the packet filter "result" fields out of the fr_info_t struct
8363 * that is local to the decapsulation processing and back into the
8364 * one we were called with.
8365 */
8366 fino->fin_flx = fin->fin_flx;
8367 fino->fin_rev = fin->fin_rev;
8368 fino->fin_icode = fin->fin_icode;
8369 fino->fin_rule = fin->fin_rule;
8370 (void) strncpy(fino->fin_group, fin->fin_group, FR_GROUPLEN);
8371 fino->fin_fr = fin->fin_fr;
8372 fino->fin_error = fin->fin_error;
8373 fino->fin_mp = fin->fin_mp;
8374 fino->fin_m = fin->fin_m;
8375 m = fin->fin_m;
8376 if (m != NULL) {
8377 #if SOLARIS && defined(_KERNEL)
8378 m->b_rptr -= elen;
8379 #else
8380 m->m_data -= elen;
8381 m->m_len += elen;
8382 #endif
8383 }
8384 return (pass);
8385 }
8386
8387
8388 /* ------------------------------------------------------------------------ */
8389 /* Function: ipf_matcharray_load */
8390 /* Returns: int - 0 = success, else error */
8391 /* Parameters: softc(I) - pointer to soft context main structure */
8392 /* data(I) - pointer to ioctl data */
8393 /* objp(I) - ipfobj_t structure to load data into */
8394 /* arrayptr(I) - pointer to location to store array pointer */
8395 /* */
8396 /* This function loads in a mathing array through the ipfobj_t struct that */
8397 /* describes it. Sanity checking and array size limitations are enforced */
8398 /* in this function to prevent userspace from trying to load in something */
8399 /* that is insanely big. Once the size of the array is known, the memory */
8400 /* required is malloc'd and returned through changing *arrayptr. The */
8401 /* contents of the array are verified before returning. Only in the event */
8402 /* of a successful call is the caller required to free up the malloc area. */
8403 /* ------------------------------------------------------------------------ */
8404 int
8405 ipf_matcharray_load(ipf_main_softc_t *softc, caddr_t data, ipfobj_t *objp,
8406 int **arrayptr)
8407 {
8408 int arraysize, *array, error;
8409
8410 *arrayptr = NULL;
8411
8412 error = BCOPYIN(data, objp, sizeof(*objp));
8413 if (error != 0) {
8414 IPFERROR(116);
8415 return (EFAULT);
8416 }
8417
8418 if (objp->ipfo_type != IPFOBJ_IPFEXPR) {
8419 IPFERROR(117);
8420 return (EINVAL);
8421 }
8422
8423 if (((objp->ipfo_size & 3) != 0) || (objp->ipfo_size == 0) ||
8424 (objp->ipfo_size > 1024)) {
8425 IPFERROR(118);
8426 return (EINVAL);
8427 }
8428
8429 arraysize = objp->ipfo_size * sizeof(*array);
8430 KMALLOCS(array, int *, arraysize);
8431 if (array == NULL) {
8432 IPFERROR(119);
8433 return (ENOMEM);
8434 }
8435
8436 error = COPYIN(objp->ipfo_ptr, array, arraysize);
8437 if (error != 0) {
8438 KFREES(array, arraysize);
8439 IPFERROR(120);
8440 return (EFAULT);
8441 }
8442
8443 if (ipf_matcharray_verify(array, arraysize) != 0) {
8444 KFREES(array, arraysize);
8445 IPFERROR(121);
8446 return (EINVAL);
8447 }
8448
8449 *arrayptr = array;
8450 return (0);
8451 }
8452
8453
8454 /* ------------------------------------------------------------------------ */
8455 /* Function: ipf_matcharray_verify */
8456 /* Returns: Nil */
8457 /* Parameters: array(I) - pointer to matching array */
8458 /* arraysize(I) - number of elements in the array */
8459 /* */
8460 /* Verify the contents of a matching array by stepping through each element */
8461 /* in it. The actual commands in the array are not verified for */
8462 /* correctness, only that all of the sizes are correctly within limits. */
8463 /* ------------------------------------------------------------------------ */
8464 int
8465 ipf_matcharray_verify(int *array, int arraysize)
8466 {
8467 int i, nelem, maxidx;
8468 ipfexp_t *e;
8469
8470 nelem = arraysize / sizeof(*array);
8471
8472 /*
8473 * Currently, it makes no sense to have an array less than 6
8474 * elements long - the initial size at the from, a single operation
8475 * (minimum 4 in length) and a trailer, for a total of 6.
8476 */
8477 if ((array[0] < 6) || (arraysize < 24) || (arraysize > 4096)) {
8478 return (-1);
8479 }
8480
8481 /*
8482 * Verify the size of data pointed to by array with how long
8483 * the array claims to be itself.
8484 */
8485 if (array[0] * sizeof(*array) != arraysize) {
8486 return (-1);
8487 }
8488
8489 maxidx = nelem - 1;
8490 /*
8491 * The last opcode in this array should be an IPF_EXP_END.
8492 */
8493 if (array[maxidx] != IPF_EXP_END) {
8494 return (-1);
8495 }
8496
8497 for (i = 1; i < maxidx; ) {
8498 e = (ipfexp_t *)(array + i);
8499
8500 /*
8501 * The length of the bits to check must be at least 1
8502 * (or else there is nothing to comapre with!) and it
8503 * cannot exceed the length of the data present.
8504 */
8505 if ((e->ipfe_size < 1 ) ||
8506 (e->ipfe_size + i > maxidx)) {
8507 return (-1);
8508 }
8509 i += e->ipfe_size;
8510 }
8511 return (0);
8512 }
8513
8514
8515 /* ------------------------------------------------------------------------ */
8516 /* Function: ipf_fr_matcharray */
8517 /* Returns: int - 0 = match failed, else positive match */
8518 /* Parameters: fin(I) - pointer to packet information */
8519 /* array(I) - pointer to matching array */
8520 /* */
8521 /* This function is used to apply a matching array against a packet and */
8522 /* return an indication of whether or not the packet successfully matches */
8523 /* all of the commands in it. */
8524 /* ------------------------------------------------------------------------ */
8525 static int
8526 ipf_fr_matcharray(fr_info_t *fin, int *array)
8527 {
8528 int i, n, *x, rv, p;
8529 ipfexp_t *e;
8530
8531 rv = 0;
8532 n = array[0];
8533 x = array + 1;
8534
8535 for (; n > 0; x += 3 + x[3], rv = 0) {
8536 e = (ipfexp_t *)x;
8537 if (e->ipfe_cmd == IPF_EXP_END)
8538 break;
8539 n -= e->ipfe_size;
8540
8541 /*
8542 * The upper 16 bits currently store the protocol value.
8543 * This is currently used with TCP and UDP port compares and
8544 * allows "tcp.port = 80" without requiring an explicit
8545 " "ip.pr = tcp" first.
8546 */
8547 p = e->ipfe_cmd >> 16;
8548 if ((p != 0) && (p != fin->fin_p))
8549 break;
8550
8551 switch (e->ipfe_cmd)
8552 {
8553 case IPF_EXP_IP_PR :
8554 for (i = 0; !rv && i < e->ipfe_narg; i++) {
8555 rv |= (fin->fin_p == e->ipfe_arg0[i]);
8556 }
8557 break;
8558
8559 case IPF_EXP_IP_SRCADDR :
8560 if (fin->fin_v != 4)
8561 break;
8562 for (i = 0; !rv && i < e->ipfe_narg; i++) {
8563 rv |= ((fin->fin_saddr &
8564 e->ipfe_arg0[i * 2 + 1]) ==
8565 e->ipfe_arg0[i * 2]);
8566 }
8567 break;
8568
8569 case IPF_EXP_IP_DSTADDR :
8570 if (fin->fin_v != 4)
8571 break;
8572 for (i = 0; !rv && i < e->ipfe_narg; i++) {
8573 rv |= ((fin->fin_daddr &
8574 e->ipfe_arg0[i * 2 + 1]) ==
8575 e->ipfe_arg0[i * 2]);
8576 }
8577 break;
8578
8579 case IPF_EXP_IP_ADDR :
8580 if (fin->fin_v != 4)
8581 break;
8582 for (i = 0; !rv && i < e->ipfe_narg; i++) {
8583 rv |= ((fin->fin_saddr &
8584 e->ipfe_arg0[i * 2 + 1]) ==
8585 e->ipfe_arg0[i * 2]) ||
8586 ((fin->fin_daddr &
8587 e->ipfe_arg0[i * 2 + 1]) ==
8588 e->ipfe_arg0[i * 2]);
8589 }
8590 break;
8591
8592 #ifdef USE_INET6
8593 case IPF_EXP_IP6_SRCADDR :
8594 if (fin->fin_v != 6)
8595 break;
8596 for (i = 0; !rv && i < e->ipfe_narg; i++) {
8597 rv |= IP6_MASKEQ(&fin->fin_src6,
8598 &e->ipfe_arg0[i * 8 + 4],
8599 &e->ipfe_arg0[i * 8]);
8600 }
8601 break;
8602
8603 case IPF_EXP_IP6_DSTADDR :
8604 if (fin->fin_v != 6)
8605 break;
8606 for (i = 0; !rv && i < e->ipfe_narg; i++) {
8607 rv |= IP6_MASKEQ(&fin->fin_dst6,
8608 &e->ipfe_arg0[i * 8 + 4],
8609 &e->ipfe_arg0[i * 8]);
8610 }
8611 break;
8612
8613 case IPF_EXP_IP6_ADDR :
8614 if (fin->fin_v != 6)
8615 break;
8616 for (i = 0; !rv && i < e->ipfe_narg; i++) {
8617 rv |= IP6_MASKEQ(&fin->fin_src6,
8618 &e->ipfe_arg0[i * 8 + 4],
8619 &e->ipfe_arg0[i * 8]) ||
8620 IP6_MASKEQ(&fin->fin_dst6,
8621 &e->ipfe_arg0[i * 8 + 4],
8622 &e->ipfe_arg0[i * 8]);
8623 }
8624 break;
8625 #endif
8626
8627 case IPF_EXP_UDP_PORT :
8628 case IPF_EXP_TCP_PORT :
8629 for (i = 0; !rv && i < e->ipfe_narg; i++) {
8630 rv |= (fin->fin_sport == e->ipfe_arg0[i]) ||
8631 (fin->fin_dport == e->ipfe_arg0[i]);
8632 }
8633 break;
8634
8635 case IPF_EXP_UDP_SPORT :
8636 case IPF_EXP_TCP_SPORT :
8637 for (i = 0; !rv && i < e->ipfe_narg; i++) {
8638 rv |= (fin->fin_sport == e->ipfe_arg0[i]);
8639 }
8640 break;
8641
8642 case IPF_EXP_UDP_DPORT :
8643 case IPF_EXP_TCP_DPORT :
8644 for (i = 0; !rv && i < e->ipfe_narg; i++) {
8645 rv |= (fin->fin_dport == e->ipfe_arg0[i]);
8646 }
8647 break;
8648
8649 case IPF_EXP_TCP_FLAGS :
8650 for (i = 0; !rv && i < e->ipfe_narg; i++) {
8651 rv |= ((fin->fin_tcpf &
8652 e->ipfe_arg0[i * 2 + 1]) ==
8653 e->ipfe_arg0[i * 2]);
8654 }
8655 break;
8656 }
8657 rv ^= e->ipfe_not;
8658
8659 if (rv == 0)
8660 break;
8661 }
8662
8663 return (rv);
8664 }
8665
8666
8667 /* ------------------------------------------------------------------------ */
8668 /* Function: ipf_queueflush */
8669 /* Returns: int - number of entries flushed (0 = none) */
8670 /* Parameters: softc(I) - pointer to soft context main structure */
8671 /* deletefn(I) - function to call to delete entry */
8672 /* ipfqs(I) - top of the list of ipf internal queues */
8673 /* userqs(I) - top of the list of user defined timeouts */
8674 /* */
8675 /* This fucntion gets called when the state/NAT hash tables fill up and we */
8676 /* need to try a bit harder to free up some space. The algorithm used here */
8677 /* split into two parts but both halves have the same goal: to reduce the */
8678 /* number of connections considered to be "active" to the low watermark. */
8679 /* There are two steps in doing this: */
8680 /* 1) Remove any TCP connections that are already considered to be "closed" */
8681 /* but have not yet been removed from the state table. The two states */
8682 /* TCPS_TIME_WAIT and TCPS_CLOSED are considered to be the perfect */
8683 /* candidates for this style of removal. If freeing up entries in */
8684 /* CLOSED or both CLOSED and TIME_WAIT brings us to the low watermark, */
8685 /* we do not go on to step 2. */
8686 /* */
8687 /* 2) Look for the oldest entries on each timeout queue and free them if */
8688 /* they are within the given window we are considering. Where the */
8689 /* window starts and the steps taken to increase its size depend upon */
8690 /* how long ipf has been running (ipf_ticks.) Anything modified in the */
8691 /* last 30 seconds is not touched. */
8692 /* touched */
8693 /* die ipf_ticks 30*1.5 1800*1.5 | 43200*1.5 */
8694 /* | | | | | | */
8695 /* future <--+----------+--------+-----------+-----+-----+-----------> past */
8696 /* now \_int=30s_/ \_int=1hr_/ \_int=12hr */
8697 /* */
8698 /* Points to note: */
8699 /* - tqe_die is the time, in the future, when entries die. */
8700 /* - tqe_die - ipf_ticks is how long left the connection has to live in ipf */
8701 /* ticks. */
8702 /* - tqe_touched is when the entry was last used by NAT/state */
8703 /* - the closer tqe_touched is to ipf_ticks, the further tqe_die will be */
8704 /* ipf_ticks any given timeout queue and vice versa. */
8705 /* - both tqe_die and tqe_touched increase over time */
8706 /* - timeout queues are sorted with the highest value of tqe_die at the */
8707 /* bottom and therefore the smallest values of each are at the top */
8708 /* - the pointer passed in as ipfqs should point to an array of timeout */
8709 /* queues representing each of the TCP states */
8710 /* */
8711 /* We start by setting up a maximum range to scan for things to move of */
8712 /* iend (newest) to istart (oldest) in chunks of "interval". If nothing is */
8713 /* found in that range, "interval" is adjusted (so long as it isn't 30) and */
8714 /* we start again with a new value for "iend" and "istart". This is */
8715 /* continued until we either finish the scan of 30 second intervals or the */
8716 /* low water mark is reached. */
8717 /* ------------------------------------------------------------------------ */
8718 int
8719 ipf_queueflush(ipf_main_softc_t *softc, ipftq_delete_fn_t deletefn,
8720 ipftq_t *ipfqs, ipftq_t *userqs, u_int *activep, int size, int low)
8721 {
8722 u_long interval, istart, iend;
8723 ipftq_t *ifq, *ifqnext;
8724 ipftqent_t *tqe, *tqn;
8725 int removed = 0;
8726
8727 for (tqn = ipfqs[IPF_TCPS_CLOSED].ifq_head; ((tqe = tqn) != NULL); ) {
8728 tqn = tqe->tqe_next;
8729 if ((*deletefn)(softc, tqe->tqe_parent) == 0)
8730 removed++;
8731 }
8732 if ((*activep * 100 / size) > low) {
8733 for (tqn = ipfqs[IPF_TCPS_TIME_WAIT].ifq_head;
8734 ((tqe = tqn) != NULL); ) {
8735 tqn = tqe->tqe_next;
8736 if ((*deletefn)(softc, tqe->tqe_parent) == 0)
8737 removed++;
8738 }
8739 }
8740
8741 if ((*activep * 100 / size) <= low) {
8742 return (removed);
8743 }
8744
8745 /*
8746 * NOTE: Use of "* 15 / 10" is required here because if "* 1.5" is
8747 * used then the operations are upgraded to floating point
8748 * and kernels don't like floating point...
8749 */
8750 if (softc->ipf_ticks > IPF_TTLVAL(43200 * 15 / 10)) {
8751 istart = IPF_TTLVAL(86400 * 4);
8752 interval = IPF_TTLVAL(43200);
8753 } else if (softc->ipf_ticks > IPF_TTLVAL(1800 * 15 / 10)) {
8754 istart = IPF_TTLVAL(43200);
8755 interval = IPF_TTLVAL(1800);
8756 } else if (softc->ipf_ticks > IPF_TTLVAL(30 * 15 / 10)) {
8757 istart = IPF_TTLVAL(1800);
8758 interval = IPF_TTLVAL(30);
8759 } else {
8760 return (0);
8761 }
8762 if (istart > softc->ipf_ticks) {
8763 if (softc->ipf_ticks - interval < interval)
8764 istart = interval;
8765 else
8766 istart = (softc->ipf_ticks / interval) * interval;
8767 }
8768
8769 iend = softc->ipf_ticks - interval;
8770
8771 while ((*activep * 100 / size) > low) {
8772 u_long try;
8773
8774 try = softc->ipf_ticks - istart;
8775
8776 for (ifq = ipfqs; ifq != NULL; ifq = ifq->ifq_next) {
8777 for (tqn = ifq->ifq_head; ((tqe = tqn) != NULL); ) {
8778 if (try < tqe->tqe_touched)
8779 break;
8780 tqn = tqe->tqe_next;
8781 if ((*deletefn)(softc, tqe->tqe_parent) == 0)
8782 removed++;
8783 }
8784 }
8785
8786 for (ifq = userqs; ifq != NULL; ifq = ifqnext) {
8787 ifqnext = ifq->ifq_next;
8788
8789 for (tqn = ifq->ifq_head; ((tqe = tqn) != NULL); ) {
8790 if (try < tqe->tqe_touched)
8791 break;
8792 tqn = tqe->tqe_next;
8793 if ((*deletefn)(softc, tqe->tqe_parent) == 0)
8794 removed++;
8795 }
8796 }
8797
8798 if (try >= iend) {
8799 if (interval == IPF_TTLVAL(43200)) {
8800 interval = IPF_TTLVAL(1800);
8801 } else if (interval == IPF_TTLVAL(1800)) {
8802 interval = IPF_TTLVAL(30);
8803 } else {
8804 break;
8805 }
8806 if (interval >= softc->ipf_ticks)
8807 break;
8808
8809 iend = softc->ipf_ticks - interval;
8810 }
8811 istart -= interval;
8812 }
8813
8814 return (removed);
8815 }
8816
8817
8818 /* ------------------------------------------------------------------------ */
8819 /* Function: ipf_deliverlocal */
8820 /* Returns: int - 1 = local address, 0 = non-local address */
8821 /* Parameters: softc(I) - pointer to soft context main structure */
8822 /* ipversion(I) - IP protocol version (4 or 6) */
8823 /* ifp(I) - network interface pointer */
8824 /* ipaddr(I) - IPv4/6 destination address */
8825 /* */
8826 /* This fucntion is used to determine in the address "ipaddr" belongs to */
8827 /* the network interface represented by ifp. */
8828 /* ------------------------------------------------------------------------ */
8829 int
8830 ipf_deliverlocal(ipf_main_softc_t *softc, int ipversion, void *ifp,
8831 i6addr_t *ipaddr)
8832 {
8833 i6addr_t addr;
8834 int islocal = 0;
8835
8836 if (ipversion == 4) {
8837 if (ipf_ifpaddr(softc, 4, FRI_NORMAL, ifp, &addr, NULL) == 0) {
8838 if (addr.in4.s_addr == ipaddr->in4.s_addr)
8839 islocal = 1;
8840 }
8841
8842 #ifdef USE_INET6
8843 } else if (ipversion == 6) {
8844 if (ipf_ifpaddr(softc, 6, FRI_NORMAL, ifp, &addr, NULL) == 0) {
8845 if (IP6_EQ(&addr, ipaddr))
8846 islocal = 1;
8847 }
8848 #endif
8849 }
8850
8851 return (islocal);
8852 }
8853
8854
8855 /* ------------------------------------------------------------------------ */
8856 /* Function: ipf_settimeout */
8857 /* Returns: int - 0 = success, -1 = failure */
8858 /* Parameters: softc(I) - pointer to soft context main structure */
8859 /* t(I) - pointer to tuneable array entry */
8860 /* p(I) - pointer to values passed in to apply */
8861 /* */
8862 /* This function is called to set the timeout values for each distinct */
8863 /* queue timeout that is available. When called, it calls into both the */
8864 /* state and NAT code, telling them to update their timeout queues. */
8865 /* ------------------------------------------------------------------------ */
8866 static int
8867 ipf_settimeout(struct ipf_main_softc_s *softc, ipftuneable_t *t,
8868 ipftuneval_t *p)
8869 {
8870
8871 /*
8872 * ipf_interror should be set by the functions called here, not
8873 * by this function - it's just a middle man.
8874 */
8875 if (ipf_state_settimeout(softc, t, p) == -1)
8876 return (-1);
8877 if (ipf_nat_settimeout(softc, t, p) == -1)
8878 return (-1);
8879 return (0);
8880 }
8881
8882
8883 /* ------------------------------------------------------------------------ */
8884 /* Function: ipf_apply_timeout */
8885 /* Returns: int - 0 = success, -1 = failure */
8886 /* Parameters: head(I) - pointer to tuneable array entry */
8887 /* seconds(I) - pointer to values passed in to apply */
8888 /* */
8889 /* This function applies a timeout of "seconds" to the timeout queue that */
8890 /* is pointed to by "head". All entries on this list have an expiration */
8891 /* set to be the current tick value of ipf plus the ttl. Given that this */
8892 /* function should only be called when the delta is non-zero, the task is */
8893 /* to walk the entire list and apply the change. The sort order will not */
8894 /* change. The only catch is that this is O(n) across the list, so if the */
8895 /* queue has lots of entries (10s of thousands or 100s of thousands), it */
8896 /* could take a relatively long time to work through them all. */
8897 /* ------------------------------------------------------------------------ */
8898 void
8899 ipf_apply_timeout(ipftq_t *head, u_int seconds)
8900 {
8901 u_int oldtimeout, newtimeout;
8902 ipftqent_t *tqe;
8903 int delta;
8904
8905 MUTEX_ENTER(&head->ifq_lock);
8906 oldtimeout = head->ifq_ttl;
8907 newtimeout = IPF_TTLVAL(seconds);
8908 delta = oldtimeout - newtimeout;
8909
8910 head->ifq_ttl = newtimeout;
8911
8912 for (tqe = head->ifq_head; tqe != NULL; tqe = tqe->tqe_next) {
8913 tqe->tqe_die += delta;
8914 }
8915 MUTEX_EXIT(&head->ifq_lock);
8916 }
8917
8918
8919 /* ------------------------------------------------------------------------ */
8920 /* Function: ipf_settimeout_tcp */
8921 /* Returns: int - 0 = successfully applied, -1 = failed */
8922 /* Parameters: t(I) - pointer to tuneable to change */
8923 /* p(I) - pointer to new timeout information */
8924 /* tab(I) - pointer to table of TCP queues */
8925 /* */
8926 /* This function applies the new timeout (p) to the TCP tunable (t) and */
8927 /* updates all of the entries on the relevant timeout queue by calling */
8928 /* ipf_apply_timeout(). */
8929 /* ------------------------------------------------------------------------ */
8930 int
8931 ipf_settimeout_tcp(ipftuneable_t *t, ipftuneval_t *p, ipftq_t *tab)
8932 {
8933 if (!strcmp(t->ipft_name, "tcp_idle_timeout") ||
8934 !strcmp(t->ipft_name, "tcp_established")) {
8935 ipf_apply_timeout(&tab[IPF_TCPS_ESTABLISHED], p->ipftu_int);
8936 } else if (!strcmp(t->ipft_name, "tcp_close_wait")) {
8937 ipf_apply_timeout(&tab[IPF_TCPS_CLOSE_WAIT], p->ipftu_int);
8938 } else if (!strcmp(t->ipft_name, "tcp_last_ack")) {
8939 ipf_apply_timeout(&tab[IPF_TCPS_LAST_ACK], p->ipftu_int);
8940 } else if (!strcmp(t->ipft_name, "tcp_timeout")) {
8941 ipf_apply_timeout(&tab[IPF_TCPS_LISTEN], p->ipftu_int);
8942 ipf_apply_timeout(&tab[IPF_TCPS_HALF_ESTAB], p->ipftu_int);
8943 ipf_apply_timeout(&tab[IPF_TCPS_CLOSING], p->ipftu_int);
8944 } else if (!strcmp(t->ipft_name, "tcp_listen")) {
8945 ipf_apply_timeout(&tab[IPF_TCPS_LISTEN], p->ipftu_int);
8946 } else if (!strcmp(t->ipft_name, "tcp_half_established")) {
8947 ipf_apply_timeout(&tab[IPF_TCPS_HALF_ESTAB], p->ipftu_int);
8948 } else if (!strcmp(t->ipft_name, "tcp_closing")) {
8949 ipf_apply_timeout(&tab[IPF_TCPS_CLOSING], p->ipftu_int);
8950 } else if (!strcmp(t->ipft_name, "tcp_syn_received")) {
8951 ipf_apply_timeout(&tab[IPF_TCPS_SYN_RECEIVED], p->ipftu_int);
8952 } else if (!strcmp(t->ipft_name, "tcp_syn_sent")) {
8953 ipf_apply_timeout(&tab[IPF_TCPS_SYN_SENT], p->ipftu_int);
8954 } else if (!strcmp(t->ipft_name, "tcp_closed")) {
8955 ipf_apply_timeout(&tab[IPF_TCPS_CLOSED], p->ipftu_int);
8956 } else if (!strcmp(t->ipft_name, "tcp_half_closed")) {
8957 ipf_apply_timeout(&tab[IPF_TCPS_CLOSED], p->ipftu_int);
8958 } else if (!strcmp(t->ipft_name, "tcp_time_wait")) {
8959 ipf_apply_timeout(&tab[IPF_TCPS_TIME_WAIT], p->ipftu_int);
8960 } else {
8961 /*
8962 * ipf_interror isn't set here because it should be set
8963 * by whatever called this function.
8964 */
8965 return (-1);
8966 }
8967 return (0);
8968 }
8969
8970
8971 /* ------------------------------------------------------------------------ */
8972 /* Function: ipf_main_soft_create */
8973 /* Returns: NULL = failure, else success */
8974 /* Parameters: arg(I) - pointer to soft context structure if already allocd */
8975 /* */
8976 /* Create the foundation soft context structure. In circumstances where it */
8977 /* is not required to dynamically allocate the context, a pointer can be */
8978 /* passed in (rather than NULL) to a structure to be initialised. */
8979 /* The main thing of interest is that a number of locks are initialised */
8980 /* here instead of in the where might be expected - in the relevant create */
8981 /* function elsewhere. This is done because the current locking design has */
8982 /* some areas where these locks are used outside of their module. */
8983 /* Possibly the most important exercise that is done here is setting of all */
8984 /* the timeout values, allowing them to be changed before init(). */
8985 /* ------------------------------------------------------------------------ */
8986 void *
8987 ipf_main_soft_create(void *arg)
8988 {
8989 ipf_main_softc_t *softc;
8990
8991 if (arg == NULL) {
8992 KMALLOC(softc, ipf_main_softc_t *);
8993 if (softc == NULL)
8994 return (NULL);
8995 } else {
8996 softc = arg;
8997 }
8998
8999 bzero((char *)softc, sizeof(*softc));
9000
9001 /*
9002 * This serves as a flag as to whether or not the softc should be
9003 * free'd when _destroy is called.
9004 */
9005 softc->ipf_dynamic_softc = (arg == NULL) ? 1 : 0;
9006
9007 softc->ipf_tuners = ipf_tune_array_copy(softc,
9008 sizeof(ipf_main_tuneables),
9009 ipf_main_tuneables);
9010 if (softc->ipf_tuners == NULL) {
9011 ipf_main_soft_destroy(softc);
9012 return (NULL);
9013 }
9014
9015 MUTEX_INIT(&softc->ipf_rw, "ipf rw mutex");
9016 MUTEX_INIT(&softc->ipf_timeoutlock, "ipf timeout lock");
9017 RWLOCK_INIT(&softc->ipf_global, "ipf filter load/unload mutex");
9018 RWLOCK_INIT(&softc->ipf_mutex, "ipf filter rwlock");
9019 RWLOCK_INIT(&softc->ipf_tokens, "ipf token rwlock");
9020 RWLOCK_INIT(&softc->ipf_state, "ipf state rwlock");
9021 RWLOCK_INIT(&softc->ipf_nat, "ipf IP NAT rwlock");
9022 RWLOCK_INIT(&softc->ipf_poolrw, "ipf pool rwlock");
9023 RWLOCK_INIT(&softc->ipf_frag, "ipf frag rwlock");
9024
9025 softc->ipf_token_head = NULL;
9026 softc->ipf_token_tail = &softc->ipf_token_head;
9027
9028 softc->ipf_tcpidletimeout = FIVE_DAYS;
9029 softc->ipf_tcpclosewait = IPF_TTLVAL(2 * TCP_MSL);
9030 softc->ipf_tcplastack = IPF_TTLVAL(30);
9031 softc->ipf_tcptimewait = IPF_TTLVAL(2 * TCP_MSL);
9032 softc->ipf_tcptimeout = IPF_TTLVAL(2 * TCP_MSL);
9033 softc->ipf_tcpsynsent = IPF_TTLVAL(2 * TCP_MSL);
9034 softc->ipf_tcpsynrecv = IPF_TTLVAL(2 * TCP_MSL);
9035 softc->ipf_tcpclosed = IPF_TTLVAL(30);
9036 softc->ipf_tcphalfclosed = IPF_TTLVAL(2 * 3600);
9037 softc->ipf_udptimeout = IPF_TTLVAL(120);
9038 softc->ipf_udpacktimeout = IPF_TTLVAL(12);
9039 softc->ipf_icmptimeout = IPF_TTLVAL(60);
9040 softc->ipf_icmpacktimeout = IPF_TTLVAL(6);
9041 softc->ipf_iptimeout = IPF_TTLVAL(60);
9042
9043 #if defined(IPFILTER_DEFAULT_BLOCK)
9044 softc->ipf_pass = FR_BLOCK|FR_NOMATCH;
9045 #else
9046 softc->ipf_pass = (IPF_DEFAULT_PASS)|FR_NOMATCH;
9047 #endif
9048 softc->ipf_minttl = 4;
9049 softc->ipf_icmpminfragmtu = 68;
9050 softc->ipf_flags = IPF_LOGGING;
9051
9052 #ifdef LARGE_NAT
9053 softc->ipf_large_nat = 1;
9054 #endif
9055 ipf_fbsd_kenv_get(softc);
9056
9057 return (softc);
9058 }
9059
9060 /* ------------------------------------------------------------------------ */
9061 /* Function: ipf_main_soft_init */
9062 /* Returns: 0 = success, -1 = failure */
9063 /* Parameters: softc(I) - pointer to soft context main structure */
9064 /* */
9065 /* A null-op function that exists as a placeholder so that the flow in */
9066 /* other functions is obvious. */
9067 /* ------------------------------------------------------------------------ */
9068 /*ARGSUSED*/
9069 int
9070 ipf_main_soft_init(ipf_main_softc_t *softc)
9071 {
9072 return (0);
9073 }
9074
9075
9076 /* ------------------------------------------------------------------------ */
9077 /* Function: ipf_main_soft_destroy */
9078 /* Returns: void */
9079 /* Parameters: softc(I) - pointer to soft context main structure */
9080 /* */
9081 /* Undo everything that we did in ipf_main_soft_create. */
9082 /* */
9083 /* The most important check that needs to be made here is whether or not */
9084 /* the structure was allocated by ipf_main_soft_create() by checking what */
9085 /* value is stored in ipf_dynamic_main. */
9086 /* ------------------------------------------------------------------------ */
9087 /*ARGSUSED*/
9088 void
9089 ipf_main_soft_destroy(ipf_main_softc_t *softc)
9090 {
9091
9092 RW_DESTROY(&softc->ipf_frag);
9093 RW_DESTROY(&softc->ipf_poolrw);
9094 RW_DESTROY(&softc->ipf_nat);
9095 RW_DESTROY(&softc->ipf_state);
9096 RW_DESTROY(&softc->ipf_tokens);
9097 RW_DESTROY(&softc->ipf_mutex);
9098 RW_DESTROY(&softc->ipf_global);
9099 MUTEX_DESTROY(&softc->ipf_timeoutlock);
9100 MUTEX_DESTROY(&softc->ipf_rw);
9101
9102 if (softc->ipf_tuners != NULL) {
9103 KFREES(softc->ipf_tuners, sizeof(ipf_main_tuneables));
9104 }
9105 if (softc->ipf_dynamic_softc == 1) {
9106 KFREE(softc);
9107 }
9108 }
9109
9110
9111 /* ------------------------------------------------------------------------ */
9112 /* Function: ipf_main_soft_fini */
9113 /* Returns: 0 = success, -1 = failure */
9114 /* Parameters: softc(I) - pointer to soft context main structure */
9115 /* */
9116 /* Clean out the rules which have been added since _init was last called, */
9117 /* the only dynamic part of the mainline. */
9118 /* ------------------------------------------------------------------------ */
9119 int
9120 ipf_main_soft_fini(ipf_main_softc_t *softc)
9121 {
9122 (void) ipf_flush(softc, IPL_LOGIPF, FR_INQUE|FR_OUTQUE|FR_INACTIVE);
9123 (void) ipf_flush(softc, IPL_LOGIPF, FR_INQUE|FR_OUTQUE);
9124 (void) ipf_flush(softc, IPL_LOGCOUNT, FR_INQUE|FR_OUTQUE|FR_INACTIVE);
9125 (void) ipf_flush(softc, IPL_LOGCOUNT, FR_INQUE|FR_OUTQUE);
9126
9127 return (0);
9128 }
9129
9130
9131 /* ------------------------------------------------------------------------ */
9132 /* Function: ipf_main_load */
9133 /* Returns: 0 = success, -1 = failure */
9134 /* Parameters: none */
9135 /* */
9136 /* Handle global initialisation that needs to be done for the base part of */
9137 /* IPFilter. At present this just amounts to initialising some ICMP lookup */
9138 /* arrays that get used by the state/NAT code. */
9139 /* ------------------------------------------------------------------------ */
9140 int
9141 ipf_main_load(void)
9142 {
9143 int i;
9144
9145 /* fill icmp reply type table */
9146 for (i = 0; i <= ICMP_MAXTYPE; i++)
9147 icmpreplytype4[i] = -1;
9148 icmpreplytype4[ICMP_ECHO] = ICMP_ECHOREPLY;
9149 icmpreplytype4[ICMP_TSTAMP] = ICMP_TSTAMPREPLY;
9150 icmpreplytype4[ICMP_IREQ] = ICMP_IREQREPLY;
9151 icmpreplytype4[ICMP_MASKREQ] = ICMP_MASKREPLY;
9152
9153 #ifdef USE_INET6
9154 /* fill icmp reply type table */
9155 for (i = 0; i <= ICMP6_MAXTYPE; i++)
9156 icmpreplytype6[i] = -1;
9157 icmpreplytype6[ICMP6_ECHO_REQUEST] = ICMP6_ECHO_REPLY;
9158 icmpreplytype6[ICMP6_MEMBERSHIP_QUERY] = ICMP6_MEMBERSHIP_REPORT;
9159 icmpreplytype6[ICMP6_NI_QUERY] = ICMP6_NI_REPLY;
9160 icmpreplytype6[ND_ROUTER_SOLICIT] = ND_ROUTER_ADVERT;
9161 icmpreplytype6[ND_NEIGHBOR_SOLICIT] = ND_NEIGHBOR_ADVERT;
9162 #endif
9163
9164 return (0);
9165 }
9166
9167
9168 /* ------------------------------------------------------------------------ */
9169 /* Function: ipf_main_unload */
9170 /* Returns: 0 = success, -1 = failure */
9171 /* Parameters: none */
9172 /* */
9173 /* A null-op function that exists as a placeholder so that the flow in */
9174 /* other functions is obvious. */
9175 /* ------------------------------------------------------------------------ */
9176 int
9177 ipf_main_unload(void)
9178 {
9179 return (0);
9180 }
9181
9182
9183 /* ------------------------------------------------------------------------ */
9184 /* Function: ipf_load_all */
9185 /* Returns: 0 = success, -1 = failure */
9186 /* Parameters: none */
9187 /* */
9188 /* Work through all of the subsystems inside IPFilter and call the load */
9189 /* function for each in an order that won't lead to a crash :) */
9190 /* ------------------------------------------------------------------------ */
9191 int
9192 ipf_load_all(void)
9193 {
9194 if (ipf_main_load() == -1)
9195 return (-1);
9196
9197 if (ipf_state_main_load() == -1)
9198 return (-1);
9199
9200 if (ipf_nat_main_load() == -1)
9201 return (-1);
9202
9203 if (ipf_frag_main_load() == -1)
9204 return (-1);
9205
9206 if (ipf_auth_main_load() == -1)
9207 return (-1);
9208
9209 if (ipf_proxy_main_load() == -1)
9210 return (-1);
9211
9212 return (0);
9213 }
9214
9215
9216 /* ------------------------------------------------------------------------ */
9217 /* Function: ipf_unload_all */
9218 /* Returns: 0 = success, -1 = failure */
9219 /* Parameters: none */
9220 /* */
9221 /* Work through all of the subsystems inside IPFilter and call the unload */
9222 /* function for each in an order that won't lead to a crash :) */
9223 /* ------------------------------------------------------------------------ */
9224 int
9225 ipf_unload_all(void)
9226 {
9227 if (ipf_proxy_main_unload() == -1)
9228 return (-1);
9229
9230 if (ipf_auth_main_unload() == -1)
9231 return (-1);
9232
9233 if (ipf_frag_main_unload() == -1)
9234 return (-1);
9235
9236 if (ipf_nat_main_unload() == -1)
9237 return (-1);
9238
9239 if (ipf_state_main_unload() == -1)
9240 return (-1);
9241
9242 if (ipf_main_unload() == -1)
9243 return (-1);
9244
9245 return (0);
9246 }
9247
9248
9249 /* ------------------------------------------------------------------------ */
9250 /* Function: ipf_create_all */
9251 /* Returns: NULL = failure, else success */
9252 /* Parameters: arg(I) - pointer to soft context main structure */
9253 /* */
9254 /* Work through all of the subsystems inside IPFilter and call the create */
9255 /* function for each in an order that won't lead to a crash :) */
9256 /* ------------------------------------------------------------------------ */
9257 ipf_main_softc_t *
9258 ipf_create_all(void *arg)
9259 {
9260 ipf_main_softc_t *softc;
9261
9262 softc = ipf_main_soft_create(arg);
9263 if (softc == NULL)
9264 return (NULL);
9265
9266 #ifdef IPFILTER_LOG
9267 softc->ipf_log_soft = ipf_log_soft_create(softc);
9268 if (softc->ipf_log_soft == NULL) {
9269 ipf_destroy_all(softc);
9270 return (NULL);
9271 }
9272 #endif
9273
9274 softc->ipf_lookup_soft = ipf_lookup_soft_create(softc);
9275 if (softc->ipf_lookup_soft == NULL) {
9276 ipf_destroy_all(softc);
9277 return (NULL);
9278 }
9279
9280 softc->ipf_sync_soft = ipf_sync_soft_create(softc);
9281 if (softc->ipf_sync_soft == NULL) {
9282 ipf_destroy_all(softc);
9283 return (NULL);
9284 }
9285
9286 softc->ipf_state_soft = ipf_state_soft_create(softc);
9287 if (softc->ipf_state_soft == NULL) {
9288 ipf_destroy_all(softc);
9289 return (NULL);
9290 }
9291
9292 softc->ipf_nat_soft = ipf_nat_soft_create(softc);
9293 if (softc->ipf_nat_soft == NULL) {
9294 ipf_destroy_all(softc);
9295 return (NULL);
9296 }
9297
9298 softc->ipf_frag_soft = ipf_frag_soft_create(softc);
9299 if (softc->ipf_frag_soft == NULL) {
9300 ipf_destroy_all(softc);
9301 return (NULL);
9302 }
9303
9304 softc->ipf_auth_soft = ipf_auth_soft_create(softc);
9305 if (softc->ipf_auth_soft == NULL) {
9306 ipf_destroy_all(softc);
9307 return (NULL);
9308 }
9309
9310 softc->ipf_proxy_soft = ipf_proxy_soft_create(softc);
9311 if (softc->ipf_proxy_soft == NULL) {
9312 ipf_destroy_all(softc);
9313 return (NULL);
9314 }
9315
9316 return (softc);
9317 }
9318
9319
9320 /* ------------------------------------------------------------------------ */
9321 /* Function: ipf_destroy_all */
9322 /* Returns: void */
9323 /* Parameters: softc(I) - pointer to soft context main structure */
9324 /* */
9325 /* Work through all of the subsystems inside IPFilter and call the destroy */
9326 /* function for each in an order that won't lead to a crash :) */
9327 /* */
9328 /* Every one of these functions is expected to succeed, so there is no */
9329 /* checking of return values. */
9330 /* ------------------------------------------------------------------------ */
9331 void
9332 ipf_destroy_all(ipf_main_softc_t *softc)
9333 {
9334
9335 if (softc->ipf_state_soft != NULL) {
9336 ipf_state_soft_destroy(softc, softc->ipf_state_soft);
9337 softc->ipf_state_soft = NULL;
9338 }
9339
9340 if (softc->ipf_nat_soft != NULL) {
9341 ipf_nat_soft_destroy(softc, softc->ipf_nat_soft);
9342 softc->ipf_nat_soft = NULL;
9343 }
9344
9345 if (softc->ipf_frag_soft != NULL) {
9346 ipf_frag_soft_destroy(softc, softc->ipf_frag_soft);
9347 softc->ipf_frag_soft = NULL;
9348 }
9349
9350 if (softc->ipf_auth_soft != NULL) {
9351 ipf_auth_soft_destroy(softc, softc->ipf_auth_soft);
9352 softc->ipf_auth_soft = NULL;
9353 }
9354
9355 if (softc->ipf_proxy_soft != NULL) {
9356 ipf_proxy_soft_destroy(softc, softc->ipf_proxy_soft);
9357 softc->ipf_proxy_soft = NULL;
9358 }
9359
9360 if (softc->ipf_sync_soft != NULL) {
9361 ipf_sync_soft_destroy(softc, softc->ipf_sync_soft);
9362 softc->ipf_sync_soft = NULL;
9363 }
9364
9365 if (softc->ipf_lookup_soft != NULL) {
9366 ipf_lookup_soft_destroy(softc, softc->ipf_lookup_soft);
9367 softc->ipf_lookup_soft = NULL;
9368 }
9369
9370 #ifdef IPFILTER_LOG
9371 if (softc->ipf_log_soft != NULL) {
9372 ipf_log_soft_destroy(softc, softc->ipf_log_soft);
9373 softc->ipf_log_soft = NULL;
9374 }
9375 #endif
9376
9377 ipf_main_soft_destroy(softc);
9378 }
9379
9380
9381 /* ------------------------------------------------------------------------ */
9382 /* Function: ipf_init_all */
9383 /* Returns: 0 = success, -1 = failure */
9384 /* Parameters: softc(I) - pointer to soft context main structure */
9385 /* */
9386 /* Work through all of the subsystems inside IPFilter and call the init */
9387 /* function for each in an order that won't lead to a crash :) */
9388 /* ------------------------------------------------------------------------ */
9389 int
9390 ipf_init_all(ipf_main_softc_t *softc)
9391 {
9392
9393 if (ipf_main_soft_init(softc) == -1)
9394 return (-1);
9395
9396 #ifdef IPFILTER_LOG
9397 if (ipf_log_soft_init(softc, softc->ipf_log_soft) == -1)
9398 return (-1);
9399 #endif
9400
9401 if (ipf_lookup_soft_init(softc, softc->ipf_lookup_soft) == -1)
9402 return (-1);
9403
9404 if (ipf_sync_soft_init(softc, softc->ipf_sync_soft) == -1)
9405 return (-1);
9406
9407 if (ipf_state_soft_init(softc, softc->ipf_state_soft) == -1)
9408 return (-1);
9409
9410 if (ipf_nat_soft_init(softc, softc->ipf_nat_soft) == -1)
9411 return (-1);
9412
9413 if (ipf_frag_soft_init(softc, softc->ipf_frag_soft) == -1)
9414 return (-1);
9415
9416 if (ipf_auth_soft_init(softc, softc->ipf_auth_soft) == -1)
9417 return (-1);
9418
9419 if (ipf_proxy_soft_init(softc, softc->ipf_proxy_soft) == -1)
9420 return (-1);
9421
9422 return (0);
9423 }
9424
9425
9426 /* ------------------------------------------------------------------------ */
9427 /* Function: ipf_fini_all */
9428 /* Returns: 0 = success, -1 = failure */
9429 /* Parameters: softc(I) - pointer to soft context main structure */
9430 /* */
9431 /* Work through all of the subsystems inside IPFilter and call the fini */
9432 /* function for each in an order that won't lead to a crash :) */
9433 /* ------------------------------------------------------------------------ */
9434 int
9435 ipf_fini_all(ipf_main_softc_t *softc)
9436 {
9437
9438 ipf_token_flush(softc);
9439
9440 if (ipf_proxy_soft_fini(softc, softc->ipf_proxy_soft) == -1)
9441 return (-1);
9442
9443 if (ipf_auth_soft_fini(softc, softc->ipf_auth_soft) == -1)
9444 return (-1);
9445
9446 if (ipf_frag_soft_fini(softc, softc->ipf_frag_soft) == -1)
9447 return (-1);
9448
9449 if (ipf_nat_soft_fini(softc, softc->ipf_nat_soft) == -1)
9450 return (-1);
9451
9452 if (ipf_state_soft_fini(softc, softc->ipf_state_soft) == -1)
9453 return (-1);
9454
9455 if (ipf_sync_soft_fini(softc, softc->ipf_sync_soft) == -1)
9456 return (-1);
9457
9458 if (ipf_lookup_soft_fini(softc, softc->ipf_lookup_soft) == -1)
9459 return (-1);
9460
9461 #ifdef IPFILTER_LOG
9462 if (ipf_log_soft_fini(softc, softc->ipf_log_soft) == -1)
9463 return (-1);
9464 #endif
9465
9466 if (ipf_main_soft_fini(softc) == -1)
9467 return (-1);
9468
9469 return (0);
9470 }
9471
9472
9473 /* ------------------------------------------------------------------------ */
9474 /* Function: ipf_rule_expire */
9475 /* Returns: Nil */
9476 /* Parameters: softc(I) - pointer to soft context main structure */
9477 /* */
9478 /* At present this function exists just to support temporary addition of */
9479 /* firewall rules. Both inactive and active lists are scanned for items to */
9480 /* purge, as by rights, the expiration is computed as soon as the rule is */
9481 /* loaded in. */
9482 /* ------------------------------------------------------------------------ */
9483 void
9484 ipf_rule_expire(ipf_main_softc_t *softc)
9485 {
9486 frentry_t *fr;
9487
9488 if ((softc->ipf_rule_explist[0] == NULL) &&
9489 (softc->ipf_rule_explist[1] == NULL))
9490 return;
9491
9492 WRITE_ENTER(&softc->ipf_mutex);
9493
9494 while ((fr = softc->ipf_rule_explist[0]) != NULL) {
9495 /*
9496 * Because the list is kept sorted on insertion, the fist
9497 * one that dies in the future means no more work to do.
9498 */
9499 if (fr->fr_die > softc->ipf_ticks)
9500 break;
9501 ipf_rule_delete(softc, fr, IPL_LOGIPF, 0);
9502 }
9503
9504 while ((fr = softc->ipf_rule_explist[1]) != NULL) {
9505 /*
9506 * Because the list is kept sorted on insertion, the fist
9507 * one that dies in the future means no more work to do.
9508 */
9509 if (fr->fr_die > softc->ipf_ticks)
9510 break;
9511 ipf_rule_delete(softc, fr, IPL_LOGIPF, 1);
9512 }
9513
9514 RWLOCK_EXIT(&softc->ipf_mutex);
9515 }
9516
9517
9518 static int ipf_ht_node_cmp(struct host_node_s *, struct host_node_s *);
9519 static void ipf_ht_node_make_key(host_track_t *, host_node_t *, int,
9520 i6addr_t *);
9521
9522 host_node_t RBI_ZERO(ipf_rb);
9523 RBI_CODE(ipf_rb, host_node_t, hn_entry, ipf_ht_node_cmp)
9524
9525
9526 /* ------------------------------------------------------------------------ */
9527 /* Function: ipf_ht_node_cmp */
9528 /* Returns: int - 0 == nodes are the same, .. */
9529 /* Parameters: k1(I) - pointer to first key to compare */
9530 /* k2(I) - pointer to second key to compare */
9531 /* */
9532 /* The "key" for the node is a combination of two fields: the address */
9533 /* family and the address itself. */
9534 /* */
9535 /* Because we're not actually interpreting the address data, it isn't */
9536 /* necessary to convert them to/from network/host byte order. The mask is */
9537 /* just used to remove bits that aren't significant - it doesn't matter */
9538 /* where they are, as long as they're always in the same place. */
9539 /* */
9540 /* As with IP6_EQ, comparing IPv6 addresses starts at the bottom because */
9541 /* this is where individual ones will differ the most - but not true for */
9542 /* for /48's, etc. */
9543 /* ------------------------------------------------------------------------ */
9544 static int
9545 ipf_ht_node_cmp(struct host_node_s *k1, struct host_node_s *k2)
9546 {
9547 int i;
9548
9549 i = (k2->hn_addr.adf_family - k1->hn_addr.adf_family);
9550 if (i != 0)
9551 return (i);
9552
9553 if (k1->hn_addr.adf_family == AF_INET)
9554 return (k2->hn_addr.adf_addr.in4.s_addr -
9555 k1->hn_addr.adf_addr.in4.s_addr);
9556
9557 i = k2->hn_addr.adf_addr.i6[3] - k1->hn_addr.adf_addr.i6[3];
9558 if (i != 0)
9559 return (i);
9560 i = k2->hn_addr.adf_addr.i6[2] - k1->hn_addr.adf_addr.i6[2];
9561 if (i != 0)
9562 return (i);
9563 i = k2->hn_addr.adf_addr.i6[1] - k1->hn_addr.adf_addr.i6[1];
9564 if (i != 0)
9565 return (i);
9566 i = k2->hn_addr.adf_addr.i6[0] - k1->hn_addr.adf_addr.i6[0];
9567 return (i);
9568 }
9569
9570
9571 /* ------------------------------------------------------------------------ */
9572 /* Function: ipf_ht_node_make_key */
9573 /* Returns: Nil */
9574 /* parameters: htp(I) - pointer to address tracking structure */
9575 /* key(I) - where to store masked address for lookup */
9576 /* family(I) - protocol family of address */
9577 /* addr(I) - pointer to network address */
9578 /* */
9579 /* Using the "netmask" (number of bits) stored parent host tracking struct, */
9580 /* copy the address passed in into the key structure whilst masking out the */
9581 /* bits that we don't want. */
9582 /* */
9583 /* Because the parser will set ht_netmask to 128 if there is no protocol */
9584 /* specified (the parser doesn't know if it should be a v4 or v6 rule), we */
9585 /* have to be wary of that and not allow 32-128 to happen. */
9586 /* ------------------------------------------------------------------------ */
9587 static void
9588 ipf_ht_node_make_key(host_track_t *htp, host_node_t *key, int family,
9589 i6addr_t *addr)
9590 {
9591 key->hn_addr.adf_family = family;
9592 if (family == AF_INET) {
9593 u_32_t mask;
9594 int bits;
9595
9596 key->hn_addr.adf_len = sizeof(key->hn_addr.adf_addr.in4);
9597 bits = htp->ht_netmask;
9598 if (bits >= 32) {
9599 mask = 0xffffffff;
9600 } else {
9601 mask = htonl(0xffffffff << (32 - bits));
9602 }
9603 key->hn_addr.adf_addr.in4.s_addr = addr->in4.s_addr & mask;
9604 #ifdef USE_INET6
9605 } else {
9606 int bits = htp->ht_netmask;
9607
9608 key->hn_addr.adf_len = sizeof(key->hn_addr.adf_addr.in6);
9609 if (bits > 96) {
9610 key->hn_addr.adf_addr.i6[3] = addr->i6[3] &
9611 htonl(0xffffffff << (128 - bits));
9612 key->hn_addr.adf_addr.i6[2] = addr->i6[2];
9613 key->hn_addr.adf_addr.i6[1] = addr->i6[2];
9614 key->hn_addr.adf_addr.i6[0] = addr->i6[2];
9615 } else if (bits > 64) {
9616 key->hn_addr.adf_addr.i6[3] = 0;
9617 key->hn_addr.adf_addr.i6[2] = addr->i6[2] &
9618 htonl(0xffffffff << (96 - bits));
9619 key->hn_addr.adf_addr.i6[1] = addr->i6[1];
9620 key->hn_addr.adf_addr.i6[0] = addr->i6[0];
9621 } else if (bits > 32) {
9622 key->hn_addr.adf_addr.i6[3] = 0;
9623 key->hn_addr.adf_addr.i6[2] = 0;
9624 key->hn_addr.adf_addr.i6[1] = addr->i6[1] &
9625 htonl(0xffffffff << (64 - bits));
9626 key->hn_addr.adf_addr.i6[0] = addr->i6[0];
9627 } else {
9628 key->hn_addr.adf_addr.i6[3] = 0;
9629 key->hn_addr.adf_addr.i6[2] = 0;
9630 key->hn_addr.adf_addr.i6[1] = 0;
9631 key->hn_addr.adf_addr.i6[0] = addr->i6[0] &
9632 htonl(0xffffffff << (32 - bits));
9633 }
9634 #endif
9635 }
9636 }
9637
9638
9639 /* ------------------------------------------------------------------------ */
9640 /* Function: ipf_ht_node_add */
9641 /* Returns: int - 0 == success, -1 == failure */
9642 /* Parameters: softc(I) - pointer to soft context main structure */
9643 /* htp(I) - pointer to address tracking structure */
9644 /* family(I) - protocol family of address */
9645 /* addr(I) - pointer to network address */
9646 /* */
9647 /* NOTE: THIS FUNCTION MUST BE CALLED WITH AN EXCLUSIVE LOCK THAT PREVENTS */
9648 /* ipf_ht_node_del FROM RUNNING CONCURRENTLY ON THE SAME htp. */
9649 /* */
9650 /* After preparing the key with the address information to find, look in */
9651 /* the red-black tree to see if the address is known. A successful call to */
9652 /* this function can mean one of two things: a new node was added to the */
9653 /* tree or a matching node exists and we're able to bump up its activity. */
9654 /* ------------------------------------------------------------------------ */
9655 int
9656 ipf_ht_node_add(ipf_main_softc_t *softc, host_track_t *htp, int family,
9657 i6addr_t *addr)
9658 {
9659 host_node_t *h;
9660 host_node_t k;
9661
9662 ipf_ht_node_make_key(htp, &k, family, addr);
9663
9664 h = RBI_SEARCH(ipf_rb, &htp->ht_root, &k);
9665 if (h == NULL) {
9666 if (htp->ht_cur_nodes >= htp->ht_max_nodes)
9667 return (-1);
9668 KMALLOC(h, host_node_t *);
9669 if (h == NULL) {
9670 DT(ipf_rb_no_mem);
9671 LBUMP(ipf_rb_no_mem);
9672 return (-1);
9673 }
9674
9675 /*
9676 * If there was a macro to initialise the RB node then that
9677 * would get used here, but there isn't...
9678 */
9679 bzero((char *)h, sizeof(*h));
9680 h->hn_addr = k.hn_addr;
9681 h->hn_addr.adf_family = k.hn_addr.adf_family;
9682 RBI_INSERT(ipf_rb, &htp->ht_root, h);
9683 htp->ht_cur_nodes++;
9684 } else {
9685 if ((htp->ht_max_per_node != 0) &&
9686 (h->hn_active >= htp->ht_max_per_node)) {
9687 DT(ipf_rb_node_max);
9688 LBUMP(ipf_rb_node_max);
9689 return (-1);
9690 }
9691 }
9692
9693 h->hn_active++;
9694
9695 return (0);
9696 }
9697
9698
9699 /* ------------------------------------------------------------------------ */
9700 /* Function: ipf_ht_node_del */
9701 /* Returns: int - 0 == success, -1 == failure */
9702 /* parameters: htp(I) - pointer to address tracking structure */
9703 /* family(I) - protocol family of address */
9704 /* addr(I) - pointer to network address */
9705 /* */
9706 /* NOTE: THIS FUNCTION MUST BE CALLED WITH AN EXCLUSIVE LOCK THAT PREVENTS */
9707 /* ipf_ht_node_add FROM RUNNING CONCURRENTLY ON THE SAME htp. */
9708 /* */
9709 /* Try and find the address passed in amongst the leavese on this tree to */
9710 /* be friend. If found then drop the active account for that node drops by */
9711 /* one. If that count reaches 0, it is time to free it all up. */
9712 /* ------------------------------------------------------------------------ */
9713 int
9714 ipf_ht_node_del(host_track_t *htp, int family, i6addr_t *addr)
9715 {
9716 host_node_t *h;
9717 host_node_t k;
9718
9719 ipf_ht_node_make_key(htp, &k, family, addr);
9720
9721 h = RBI_SEARCH(ipf_rb, &htp->ht_root, &k);
9722 if (h == NULL) {
9723 return (-1);
9724 } else {
9725 h->hn_active--;
9726 if (h->hn_active == 0) {
9727 (void) RBI_DELETE(ipf_rb, &htp->ht_root, h);
9728 htp->ht_cur_nodes--;
9729 KFREE(h);
9730 }
9731 }
9732
9733 return (0);
9734 }
9735
9736
9737 /* ------------------------------------------------------------------------ */
9738 /* Function: ipf_rb_ht_init */
9739 /* Returns: Nil */
9740 /* Parameters: head(I) - pointer to host tracking structure */
9741 /* */
9742 /* Initialise the host tracking structure to be ready for use above. */
9743 /* ------------------------------------------------------------------------ */
9744 void
9745 ipf_rb_ht_init(host_track_t *head)
9746 {
9747 RBI_INIT(ipf_rb, &head->ht_root);
9748 }
9749
9750
9751 /* ------------------------------------------------------------------------ */
9752 /* Function: ipf_rb_ht_freenode */
9753 /* Returns: Nil */
9754 /* Parameters: head(I) - pointer to host tracking structure */
9755 /* arg(I) - additional argument from walk caller */
9756 /* */
9757 /* Free an actual host_node_t structure. */
9758 /* ------------------------------------------------------------------------ */
9759 void
9760 ipf_rb_ht_freenode(host_node_t *node, void *arg)
9761 {
9762 KFREE(node);
9763 }
9764
9765
9766 /* ------------------------------------------------------------------------ */
9767 /* Function: ipf_rb_ht_flush */
9768 /* Returns: Nil */
9769 /* Parameters: head(I) - pointer to host tracking structure */
9770 /* */
9771 /* Remove all of the nodes in the tree tracking hosts by calling a walker */
9772 /* and free'ing each one. */
9773 /* ------------------------------------------------------------------------ */
9774 void
9775 ipf_rb_ht_flush(host_track_t *head)
9776 {
9777 RBI_WALK(ipf_rb, &head->ht_root, ipf_rb_ht_freenode, NULL);
9778 }
9779
9780
9781 /* ------------------------------------------------------------------------ */
9782 /* Function: ipf_slowtimer */
9783 /* Returns: Nil */
9784 /* Parameters: ptr(I) - pointer to main ipf soft context structure */
9785 /* */
9786 /* Slowly expire held state for fragments. Timeouts are set * in */
9787 /* expectation of this being called twice per second. */
9788 /* ------------------------------------------------------------------------ */
9789 void
9790 ipf_slowtimer(ipf_main_softc_t *softc)
9791 {
9792
9793 ipf_token_expire(softc);
9794 ipf_frag_expire(softc);
9795 ipf_state_expire(softc);
9796 ipf_nat_expire(softc);
9797 ipf_auth_expire(softc);
9798 ipf_lookup_expire(softc);
9799 ipf_rule_expire(softc);
9800 ipf_sync_expire(softc);
9801 softc->ipf_ticks++;
9802 }
9803
9804
9805 /* ------------------------------------------------------------------------ */
9806 /* Function: ipf_inet_mask_add */
9807 /* Returns: Nil */
9808 /* Parameters: bits(I) - pointer to nat context information */
9809 /* mtab(I) - pointer to mask hash table structure */
9810 /* */
9811 /* When called, bits represents the mask of a new NAT rule that has just */
9812 /* been added. This function inserts a bitmask into the array of masks to */
9813 /* search when searching for a matching NAT rule for a packet. */
9814 /* Prevention of duplicate masks is achieved by checking the use count for */
9815 /* a given netmask. */
9816 /* ------------------------------------------------------------------------ */
9817 void
9818 ipf_inet_mask_add(int bits, ipf_v4_masktab_t *mtab)
9819 {
9820 u_32_t mask;
9821 int i, j;
9822
9823 mtab->imt4_masks[bits]++;
9824 if (mtab->imt4_masks[bits] > 1)
9825 return;
9826
9827 if (bits == 0)
9828 mask = 0;
9829 else
9830 mask = 0xffffffff << (32 - bits);
9831
9832 for (i = 0; i < 33; i++) {
9833 if (ntohl(mtab->imt4_active[i]) < mask) {
9834 for (j = 32; j > i; j--)
9835 mtab->imt4_active[j] = mtab->imt4_active[j - 1];
9836 mtab->imt4_active[i] = htonl(mask);
9837 break;
9838 }
9839 }
9840 mtab->imt4_max++;
9841 }
9842
9843
9844 /* ------------------------------------------------------------------------ */
9845 /* Function: ipf_inet_mask_del */
9846 /* Returns: Nil */
9847 /* Parameters: bits(I) - number of bits set in the netmask */
9848 /* mtab(I) - pointer to mask hash table structure */
9849 /* */
9850 /* Remove the 32bit bitmask represented by "bits" from the collection of */
9851 /* netmasks stored inside of mtab. */
9852 /* ------------------------------------------------------------------------ */
9853 void
9854 ipf_inet_mask_del(int bits, ipf_v4_masktab_t *mtab)
9855 {
9856 u_32_t mask;
9857 int i, j;
9858
9859 mtab->imt4_masks[bits]--;
9860 if (mtab->imt4_masks[bits] > 0)
9861 return;
9862
9863 mask = htonl(0xffffffff << (32 - bits));
9864 for (i = 0; i < 33; i++) {
9865 if (mtab->imt4_active[i] == mask) {
9866 for (j = i + 1; j < 33; j++)
9867 mtab->imt4_active[j - 1] = mtab->imt4_active[j];
9868 break;
9869 }
9870 }
9871 mtab->imt4_max--;
9872 ASSERT(mtab->imt4_max >= 0);
9873 }
9874
9875
9876 #ifdef USE_INET6
9877 /* ------------------------------------------------------------------------ */
9878 /* Function: ipf_inet6_mask_add */
9879 /* Returns: Nil */
9880 /* Parameters: bits(I) - number of bits set in mask */
9881 /* mask(I) - pointer to mask to add */
9882 /* mtab(I) - pointer to mask hash table structure */
9883 /* */
9884 /* When called, bitcount represents the mask of a IPv6 NAT map rule that */
9885 /* has just been added. This function inserts a bitmask into the array of */
9886 /* masks to search when searching for a matching NAT rule for a packet. */
9887 /* Prevention of duplicate masks is achieved by checking the use count for */
9888 /* a given netmask. */
9889 /* ------------------------------------------------------------------------ */
9890 void
9891 ipf_inet6_mask_add(int bits, i6addr_t *mask, ipf_v6_masktab_t *mtab)
9892 {
9893 i6addr_t zero;
9894 int i, j;
9895
9896 mtab->imt6_masks[bits]++;
9897 if (mtab->imt6_masks[bits] > 1)
9898 return;
9899
9900 if (bits == 0) {
9901 mask = &zero;
9902 zero.i6[0] = 0;
9903 zero.i6[1] = 0;
9904 zero.i6[2] = 0;
9905 zero.i6[3] = 0;
9906 }
9907
9908 for (i = 0; i < 129; i++) {
9909 if (IP6_LT(&mtab->imt6_active[i], mask)) {
9910 for (j = 128; j > i; j--)
9911 mtab->imt6_active[j] = mtab->imt6_active[j - 1];
9912 mtab->imt6_active[i] = *mask;
9913 break;
9914 }
9915 }
9916 mtab->imt6_max++;
9917 }
9918
9919
9920 /* ------------------------------------------------------------------------ */
9921 /* Function: ipf_inet6_mask_del */
9922 /* Returns: Nil */
9923 /* Parameters: bits(I) - number of bits set in mask */
9924 /* mask(I) - pointer to mask to remove */
9925 /* mtab(I) - pointer to mask hash table structure */
9926 /* */
9927 /* Remove the 128bit bitmask represented by "bits" from the collection of */
9928 /* netmasks stored inside of mtab. */
9929 /* ------------------------------------------------------------------------ */
9930 void
9931 ipf_inet6_mask_del(int bits, i6addr_t *mask, ipf_v6_masktab_t *mtab)
9932 {
9933 i6addr_t zero;
9934 int i, j;
9935
9936 mtab->imt6_masks[bits]--;
9937 if (mtab->imt6_masks[bits] > 0)
9938 return;
9939
9940 if (bits == 0)
9941 mask = &zero;
9942 zero.i6[0] = 0;
9943 zero.i6[1] = 0;
9944 zero.i6[2] = 0;
9945 zero.i6[3] = 0;
9946
9947 for (i = 0; i < 129; i++) {
9948 if (IP6_EQ(&mtab->imt6_active[i], mask)) {
9949 for (j = i + 1; j < 129; j++) {
9950 mtab->imt6_active[j - 1] = mtab->imt6_active[j];
9951 if (IP6_EQ(&mtab->imt6_active[j - 1], &zero))
9952 break;
9953 }
9954 break;
9955 }
9956 }
9957 mtab->imt6_max--;
9958 ASSERT(mtab->imt6_max >= 0);
9959 }
9960 #endif
9961