4 * Copyright (C) 2012 by Darren Reed.
6 * See the IPFILTER.LICENCE file for details on licencing.
8 * Copyright 2008 Sun Microsystems.
13 #if defined(KERNEL) || defined(_KERNEL)
19 #include <sys/errno.h>
20 #include <sys/types.h>
21 #include <sys/param.h>
23 #if defined(_KERNEL) && defined(__FreeBSD_version) && \
24 (__FreeBSD_version >= 220000)
25 # if (__FreeBSD_version >= 400000)
26 # if !defined(IPFILTER_LKM)
27 # include "opt_inet6.h"
29 # if (__FreeBSD_version == 400019)
30 # define CSUM_DELAY_DATA
33 # include <sys/filio.h>
35 # include <sys/ioctl.h>
37 #if (defined(__SVR4) || defined(__svr4__)) && defined(sun)
38 # include <sys/filio.h>
41 # include <sys/fcntl.h>
44 # include <sys/systm.h>
45 # include <sys/file.h>
51 # include <sys/file.h>
59 #if !defined(__SVR4) && !defined(__svr4__) && !defined(__hpux) && \
61 # include <sys/mbuf.h>
64 # include <sys/byteorder.h>
66 # if (SOLARIS2 < 5) && defined(sun)
67 # include <sys/dditypes.h>
71 # define _NET_ROUTE_INCLUDED
74 # include <sys/protosw.h>
76 #include <sys/socket.h>
81 #include <netinet/in.h>
82 #include <netinet/in_systm.h>
83 #include <netinet/ip.h>
84 #if defined(__sgi) && defined(IFF_DRVRLOCK) /* IRIX 6 */
85 # include <sys/hashing.h>
86 # include <netinet/in_var.h>
88 #include <netinet/tcp.h>
89 #if (!defined(__sgi) && !defined(AIX)) || defined(_KERNEL)
90 # include <netinet/udp.h>
91 # include <netinet/ip_icmp.h>
94 # undef _NET_ROUTE_INCLUDED
99 #include "netinet/ip_compat.h"
101 # include <netinet/icmp6.h>
102 # if !SOLARIS && defined(_KERNEL) && !defined(__osf__) && !defined(__hpux)
103 # include <netinet6/in6_var.h>
106 #include "netinet/ip_fil.h"
107 #include "netinet/ip_nat.h"
108 #include "netinet/ip_frag.h"
109 #include "netinet/ip_state.h"
110 #include "netinet/ip_proxy.h"
111 #include "netinet/ip_auth.h"
113 # include "netinet/ip_scan.h"
115 #include "netinet/ip_sync.h"
116 #include "netinet/ip_lookup.h"
117 #include "netinet/ip_pool.h"
118 #include "netinet/ip_htable.h"
119 #ifdef IPFILTER_COMPILED
120 # include "netinet/ip_rules.h"
122 #if defined(IPFILTER_BPF) && defined(_KERNEL)
123 # include <net/bpf.h>
125 #if defined(__FreeBSD_version) && (__FreeBSD_version >= 300000)
126 # include <sys/malloc.h>
128 #include "netinet/ipl.h"
130 #if defined(__NetBSD__) && (__NetBSD_Version__ >= 104230000)
131 # include <sys/callout.h>
132 extern struct callout ipf_slowtimer_ch;
134 #if defined(__OpenBSD__)
135 # include <sys/timeout.h>
136 extern struct timeout ipf_slowtimer_ch;
138 /* END OF INCLUDES */
141 static const char sccsid[] = "@(#)fil.c 1.36 6/5/96 (C) 1993-2000 Darren Reed";
142 static const char rcsid[] = "@(#)$FreeBSD$";
143 /* static const char rcsid[] = "@(#)$Id: fil.c,v 2.243.2.125 2007/10/10 09:27:20 darrenr Exp $"; */
150 extern int blockreason;
153 #define LBUMP(x) softc->x++
154 #define LBUMPD(x, y) do { softc->x.y++; DT(y); } while (0)
156 static INLINE int ipf_check_ipf __P((fr_info_t *, frentry_t *, int));
157 static u_32_t ipf_checkcipso __P((fr_info_t *, u_char *, int));
158 static u_32_t ipf_checkripso __P((u_char *));
159 static u_32_t ipf_decaps __P((fr_info_t *, u_32_t, int));
161 static frentry_t *ipf_dolog __P((fr_info_t *, u_32_t *));
163 static int ipf_flushlist __P((ipf_main_softc_t *, int *,
165 static int ipf_flush_groups __P((ipf_main_softc_t *, frgroup_t **,
167 static ipfunc_t ipf_findfunc __P((ipfunc_t));
168 static void *ipf_findlookup __P((ipf_main_softc_t *, int,
170 i6addr_t *, i6addr_t *));
171 static frentry_t *ipf_firewall __P((fr_info_t *, u_32_t *));
172 static int ipf_fr_matcharray __P((fr_info_t *, int *));
173 static int ipf_frruleiter __P((ipf_main_softc_t *, void *, int,
175 static void ipf_funcfini __P((ipf_main_softc_t *, frentry_t *));
176 static int ipf_funcinit __P((ipf_main_softc_t *, frentry_t *));
177 static int ipf_geniter __P((ipf_main_softc_t *, ipftoken_t *,
179 static void ipf_getstat __P((ipf_main_softc_t *,
180 struct friostat *, int));
181 static int ipf_group_flush __P((ipf_main_softc_t *, frgroup_t *));
182 static void ipf_group_free __P((frgroup_t *));
183 static int ipf_grpmapfini __P((struct ipf_main_softc_s *,
185 static int ipf_grpmapinit __P((struct ipf_main_softc_s *,
187 static frentry_t *ipf_nextrule __P((ipf_main_softc_t *, int, int,
189 static int ipf_portcheck __P((frpcmp_t *, u_32_t));
190 static INLINE int ipf_pr_ah __P((fr_info_t *));
191 static INLINE void ipf_pr_esp __P((fr_info_t *));
192 static INLINE void ipf_pr_gre __P((fr_info_t *));
193 static INLINE void ipf_pr_udp __P((fr_info_t *));
194 static INLINE void ipf_pr_tcp __P((fr_info_t *));
195 static INLINE void ipf_pr_icmp __P((fr_info_t *));
196 static INLINE void ipf_pr_ipv4hdr __P((fr_info_t *));
197 static INLINE void ipf_pr_short __P((fr_info_t *, int));
198 static INLINE int ipf_pr_tcpcommon __P((fr_info_t *));
199 static INLINE int ipf_pr_udpcommon __P((fr_info_t *));
200 static void ipf_rule_delete __P((ipf_main_softc_t *, frentry_t *f,
202 static void ipf_rule_expire_insert __P((ipf_main_softc_t *,
204 static int ipf_synclist __P((ipf_main_softc_t *, frentry_t *,
206 static void ipf_token_flush __P((ipf_main_softc_t *));
207 static void ipf_token_unlink __P((ipf_main_softc_t *,
209 static ipftuneable_t *ipf_tune_findbyname __P((ipftuneable_t *,
211 static ipftuneable_t *ipf_tune_findbycookie __P((ipftuneable_t **, void *,
213 static int ipf_updateipid __P((fr_info_t *));
214 static int ipf_settimeout __P((struct ipf_main_softc_s *,
215 struct ipftuneable *,
217 #if !defined(_KERNEL) || (!defined(__NetBSD__) && !defined(__OpenBSD__) && \
218 !defined(__FreeBSD__)) || \
219 FREEBSD_LT_REV(501000) || NETBSD_LT_REV(105000000) || \
220 OPENBSD_LT_REV(200006)
221 static int ppsratecheck(struct timeval *, int *, int);
226 * bit values for identifying presence of individual IP options
227 * All of these tables should be ordered by increasing key value on the left
228 * hand side to allow for binary searching of the array and include a trailer
229 * with a 0 for the bitmask for linear searches to easily find the end with.
231 static const struct optlist ipopts[20] = {
232 { IPOPT_NOP, 0x000001 },
233 { IPOPT_RR, 0x000002 },
234 { IPOPT_ZSU, 0x000004 },
235 { IPOPT_MTUP, 0x000008 },
236 { IPOPT_MTUR, 0x000010 },
237 { IPOPT_ENCODE, 0x000020 },
238 { IPOPT_TS, 0x000040 },
239 { IPOPT_TR, 0x000080 },
240 { IPOPT_SECURITY, 0x000100 },
241 { IPOPT_LSRR, 0x000200 },
242 { IPOPT_E_SEC, 0x000400 },
243 { IPOPT_CIPSO, 0x000800 },
244 { IPOPT_SATID, 0x001000 },
245 { IPOPT_SSRR, 0x002000 },
246 { IPOPT_ADDEXT, 0x004000 },
247 { IPOPT_VISA, 0x008000 },
248 { IPOPT_IMITD, 0x010000 },
249 { IPOPT_EIP, 0x020000 },
250 { IPOPT_FINN, 0x040000 },
255 static const struct optlist ip6exthdr[] = {
256 { IPPROTO_HOPOPTS, 0x000001 },
257 { IPPROTO_IPV6, 0x000002 },
258 { IPPROTO_ROUTING, 0x000004 },
259 { IPPROTO_FRAGMENT, 0x000008 },
260 { IPPROTO_ESP, 0x000010 },
261 { IPPROTO_AH, 0x000020 },
262 { IPPROTO_NONE, 0x000040 },
263 { IPPROTO_DSTOPTS, 0x000080 },
264 { IPPROTO_MOBILITY, 0x000100 },
270 * bit values for identifying presence of individual IP security options
272 static const struct optlist secopt[8] = {
273 { IPSO_CLASS_RES4, 0x01 },
274 { IPSO_CLASS_TOPS, 0x02 },
275 { IPSO_CLASS_SECR, 0x04 },
276 { IPSO_CLASS_RES3, 0x08 },
277 { IPSO_CLASS_CONF, 0x10 },
278 { IPSO_CLASS_UNCL, 0x20 },
279 { IPSO_CLASS_RES2, 0x40 },
280 { IPSO_CLASS_RES1, 0x80 }
283 char ipfilter_version[] = IPL_VERSION;
296 #ifdef IPFILTER_COMPILED
299 #ifdef IPFILTER_CKSUM
313 * Table of functions available for use with call rules.
315 static ipfunc_resolve_t ipf_availfuncs[] = {
316 { "srcgrpmap", ipf_srcgrpmap, ipf_grpmapinit, ipf_grpmapfini },
317 { "dstgrpmap", ipf_dstgrpmap, ipf_grpmapinit, ipf_grpmapfini },
318 { "", NULL, NULL, NULL }
321 static ipftuneable_t ipf_main_tuneables[] = {
322 { { (void *)offsetof(struct ipf_main_softc_s, ipf_flags) },
323 "ipf_flags", 0, 0xffffffff,
324 stsizeof(ipf_main_softc_t, ipf_flags),
326 { { (void *)offsetof(struct ipf_main_softc_s, ipf_active) },
328 stsizeof(ipf_main_softc_t, ipf_active),
329 IPFT_RDONLY, NULL, NULL },
330 { { (void *)offsetof(ipf_main_softc_t, ipf_control_forwarding) },
331 "control_forwarding", 0, 1,
332 stsizeof(ipf_main_softc_t, ipf_control_forwarding),
334 { { (void *)offsetof(ipf_main_softc_t, ipf_update_ipid) },
336 stsizeof(ipf_main_softc_t, ipf_update_ipid),
338 { { (void *)offsetof(ipf_main_softc_t, ipf_chksrc) },
340 stsizeof(ipf_main_softc_t, ipf_chksrc),
342 { { (void *)offsetof(ipf_main_softc_t, ipf_minttl) },
344 stsizeof(ipf_main_softc_t, ipf_minttl),
346 { { (void *)offsetof(ipf_main_softc_t, ipf_icmpminfragmtu) },
347 "icmp_minfragmtu", 0, 1,
348 stsizeof(ipf_main_softc_t, ipf_icmpminfragmtu),
350 { { (void *)offsetof(ipf_main_softc_t, ipf_pass) },
351 "default_pass", 0, 0xffffffff,
352 stsizeof(ipf_main_softc_t, ipf_pass),
354 { { (void *)offsetof(ipf_main_softc_t, ipf_tcpidletimeout) },
355 "tcp_idle_timeout", 1, 0x7fffffff,
356 stsizeof(ipf_main_softc_t, ipf_tcpidletimeout),
357 0, NULL, ipf_settimeout },
358 { { (void *)offsetof(ipf_main_softc_t, ipf_tcpclosewait) },
359 "tcp_close_wait", 1, 0x7fffffff,
360 stsizeof(ipf_main_softc_t, ipf_tcpclosewait),
361 0, NULL, ipf_settimeout },
362 { { (void *)offsetof(ipf_main_softc_t, ipf_tcplastack) },
363 "tcp_last_ack", 1, 0x7fffffff,
364 stsizeof(ipf_main_softc_t, ipf_tcplastack),
365 0, NULL, ipf_settimeout },
366 { { (void *)offsetof(ipf_main_softc_t, ipf_tcptimeout) },
367 "tcp_timeout", 1, 0x7fffffff,
368 stsizeof(ipf_main_softc_t, ipf_tcptimeout),
369 0, NULL, ipf_settimeout },
370 { { (void *)offsetof(ipf_main_softc_t, ipf_tcpsynsent) },
371 "tcp_syn_sent", 1, 0x7fffffff,
372 stsizeof(ipf_main_softc_t, ipf_tcpsynsent),
373 0, NULL, ipf_settimeout },
374 { { (void *)offsetof(ipf_main_softc_t, ipf_tcpsynrecv) },
375 "tcp_syn_received", 1, 0x7fffffff,
376 stsizeof(ipf_main_softc_t, ipf_tcpsynrecv),
377 0, NULL, ipf_settimeout },
378 { { (void *)offsetof(ipf_main_softc_t, ipf_tcpclosed) },
379 "tcp_closed", 1, 0x7fffffff,
380 stsizeof(ipf_main_softc_t, ipf_tcpclosed),
381 0, NULL, ipf_settimeout },
382 { { (void *)offsetof(ipf_main_softc_t, ipf_tcphalfclosed) },
383 "tcp_half_closed", 1, 0x7fffffff,
384 stsizeof(ipf_main_softc_t, ipf_tcphalfclosed),
385 0, NULL, ipf_settimeout },
386 { { (void *)offsetof(ipf_main_softc_t, ipf_tcptimewait) },
387 "tcp_time_wait", 1, 0x7fffffff,
388 stsizeof(ipf_main_softc_t, ipf_tcptimewait),
389 0, NULL, ipf_settimeout },
390 { { (void *)offsetof(ipf_main_softc_t, ipf_udptimeout) },
391 "udp_timeout", 1, 0x7fffffff,
392 stsizeof(ipf_main_softc_t, ipf_udptimeout),
393 0, NULL, ipf_settimeout },
394 { { (void *)offsetof(ipf_main_softc_t, ipf_udpacktimeout) },
395 "udp_ack_timeout", 1, 0x7fffffff,
396 stsizeof(ipf_main_softc_t, ipf_udpacktimeout),
397 0, NULL, ipf_settimeout },
398 { { (void *)offsetof(ipf_main_softc_t, ipf_icmptimeout) },
399 "icmp_timeout", 1, 0x7fffffff,
400 stsizeof(ipf_main_softc_t, ipf_icmptimeout),
401 0, NULL, ipf_settimeout },
402 { { (void *)offsetof(ipf_main_softc_t, ipf_icmpacktimeout) },
403 "icmp_ack_timeout", 1, 0x7fffffff,
404 stsizeof(ipf_main_softc_t, ipf_icmpacktimeout),
405 0, NULL, ipf_settimeout },
406 { { (void *)offsetof(ipf_main_softc_t, ipf_iptimeout) },
407 "ip_timeout", 1, 0x7fffffff,
408 stsizeof(ipf_main_softc_t, ipf_iptimeout),
409 0, NULL, ipf_settimeout },
410 #if defined(INSTANCES) && defined(_KERNEL)
411 { { (void *)offsetof(ipf_main_softc_t, ipf_get_loopback) },
412 "intercept_loopback", 0, 1,
413 stsizeof(ipf_main_softc_t, ipf_get_loopback),
414 0, NULL, ipf_set_loopback },
424 * The next section of code is a collection of small routines that set
425 * fields in the fr_info_t structure passed based on properties of the
426 * current packet. There are different routines for the same protocol
427 * for each of IPv4 and IPv6. Adding a new protocol, for which there
428 * will "special" inspection for setup, is now more easily done by adding
429 * a new routine and expanding the ipf_pr_ipinit*() function rather than by
430 * adding more code to a growing switch statement.
433 static INLINE int ipf_pr_ah6 __P((fr_info_t *));
434 static INLINE void ipf_pr_esp6 __P((fr_info_t *));
435 static INLINE void ipf_pr_gre6 __P((fr_info_t *));
436 static INLINE void ipf_pr_udp6 __P((fr_info_t *));
437 static INLINE void ipf_pr_tcp6 __P((fr_info_t *));
438 static INLINE void ipf_pr_icmp6 __P((fr_info_t *));
439 static INLINE void ipf_pr_ipv6hdr __P((fr_info_t *));
440 static INLINE void ipf_pr_short6 __P((fr_info_t *, int));
441 static INLINE int ipf_pr_hopopts6 __P((fr_info_t *));
442 static INLINE int ipf_pr_mobility6 __P((fr_info_t *));
443 static INLINE int ipf_pr_routing6 __P((fr_info_t *));
444 static INLINE int ipf_pr_dstopts6 __P((fr_info_t *));
445 static INLINE int ipf_pr_fragment6 __P((fr_info_t *));
446 static INLINE struct ip6_ext *ipf_pr_ipv6exthdr __P((fr_info_t *, int, int));
449 /* ------------------------------------------------------------------------ */
450 /* Function: ipf_pr_short6 */
452 /* Parameters: fin(I) - pointer to packet information */
453 /* xmin(I) - minimum header size */
456 /* This is function enforces the 'is a packet too short to be legit' rule */
457 /* for IPv6 and marks the packet with FI_SHORT if so. See function comment */
458 /* for ipf_pr_short() for more details. */
459 /* ------------------------------------------------------------------------ */
461 ipf_pr_short6(fin, xmin)
466 if (fin->fin_dlen < xmin)
467 fin->fin_flx |= FI_SHORT;
471 /* ------------------------------------------------------------------------ */
472 /* Function: ipf_pr_ipv6hdr */
474 /* Parameters: fin(I) - pointer to packet information */
477 /* Copy values from the IPv6 header into the fr_info_t struct and call the */
478 /* per-protocol analyzer if it exists. In validating the packet, a protocol*/
479 /* analyzer may pullup or free the packet itself so we need to be vigiliant */
480 /* of that possibility arising. */
481 /* ------------------------------------------------------------------------ */
486 ip6_t *ip6 = (ip6_t *)fin->fin_ip;
487 int p, go = 1, i, hdrcount;
488 fr_ip_t *fi = &fin->fin_fi;
499 fi->fi_ttl = ip6->ip6_hlim;
500 fi->fi_src.in6 = ip6->ip6_src;
501 fin->fin_crc += fi->fi_src.i6[0];
502 fin->fin_crc += fi->fi_src.i6[1];
503 fin->fin_crc += fi->fi_src.i6[2];
504 fin->fin_crc += fi->fi_src.i6[3];
505 fi->fi_dst.in6 = ip6->ip6_dst;
506 fin->fin_crc += fi->fi_dst.i6[0];
507 fin->fin_crc += fi->fi_dst.i6[1];
508 fin->fin_crc += fi->fi_dst.i6[2];
509 fin->fin_crc += fi->fi_dst.i6[3];
511 if (IN6_IS_ADDR_MULTICAST(&fi->fi_dst.in6))
512 fin->fin_flx |= FI_MULTICAST|FI_MBCAST;
515 while (go && !(fin->fin_flx & FI_SHORT)) {
528 case IPPROTO_ICMPV6 :
538 case IPPROTO_HOPOPTS :
539 p = ipf_pr_hopopts6(fin);
542 case IPPROTO_MOBILITY :
543 p = ipf_pr_mobility6(fin);
546 case IPPROTO_DSTOPTS :
547 p = ipf_pr_dstopts6(fin);
550 case IPPROTO_ROUTING :
551 p = ipf_pr_routing6(fin);
564 for (i = 0; ip6exthdr[i].ol_bit != 0; i++)
565 if (ip6exthdr[i].ol_val == p) {
566 fin->fin_flx |= ip6exthdr[i].ol_bit;
576 case IPPROTO_FRAGMENT :
577 p = ipf_pr_fragment6(fin);
579 * Given that the only fragments we want to let through
580 * (where fin_off != 0) are those where the non-first
581 * fragments only have data, we can safely stop looking
582 * at headers if this is a non-leading fragment.
584 if (fin->fin_off != 0)
595 * It is important to note that at this point, for the
596 * extension headers (go != 0), the entire header may not have
597 * been pulled up when the code gets to this point. This is
598 * only done for "go != 0" because the other header handlers
599 * will all pullup their complete header. The other indicator
600 * of an incomplete packet is that this was just an extension
603 if ((go != 0) && (p != IPPROTO_NONE) &&
604 (ipf_pr_pullup(fin, 0) == -1)) {
611 * Some of the above functions, like ipf_pr_esp6(), can call ipf_pullup
612 * and destroy whatever packet was here. The caller of this function
613 * expects us to return if there is a problem with ipf_pullup.
615 if (fin->fin_m == NULL) {
616 ipf_main_softc_t *softc = fin->fin_main_soft;
618 LBUMPD(ipf_stats[fin->fin_out], fr_v6_bad);
625 * IPv6 fragment case 1 - see comment for ipf_pr_fragment6().
626 * "go != 0" imples the above loop hasn't arrived at a layer 4 header.
628 if ((go != 0) && (fin->fin_flx & FI_FRAG) && (fin->fin_off == 0)) {
629 ipf_main_softc_t *softc = fin->fin_main_soft;
631 fin->fin_flx |= FI_BAD;
632 DT2(ipf_fi_bad_ipv6_frag_1, fr_info_t *, fin, int, go);
633 LBUMPD(ipf_stats[fin->fin_out], fr_v6_badfrag);
634 LBUMP(ipf_stats[fin->fin_out].fr_v6_bad);
639 /* ------------------------------------------------------------------------ */
640 /* Function: ipf_pr_ipv6exthdr */
641 /* Returns: struct ip6_ext * - pointer to the start of the next header */
642 /* or NULL if there is a prolblem. */
643 /* Parameters: fin(I) - pointer to packet information */
644 /* multiple(I) - flag indicating yes/no if multiple occurances */
645 /* of this extension header are allowed. */
646 /* proto(I) - protocol number for this extension header */
649 /* This function embodies a number of common checks that all IPv6 extension */
650 /* headers must be subjected to. For example, making sure the packet is */
651 /* big enough for it to be in, checking if it is repeated and setting a */
652 /* flag to indicate its presence. */
653 /* ------------------------------------------------------------------------ */
654 static INLINE struct ip6_ext *
655 ipf_pr_ipv6exthdr(fin, multiple, proto)
659 ipf_main_softc_t *softc = fin->fin_main_soft;
664 fin->fin_flx |= FI_V6EXTHDR;
666 /* 8 is default length of extension hdr */
667 if ((fin->fin_dlen - 8) < 0) {
668 fin->fin_flx |= FI_SHORT;
669 LBUMPD(ipf_stats[fin->fin_out], fr_v6_ext_short);
673 if (ipf_pr_pullup(fin, 8) == -1) {
674 LBUMPD(ipf_stats[fin->fin_out], fr_v6_ext_pullup);
681 case IPPROTO_FRAGMENT :
685 shift = 8 + (hdr->ip6e_len << 3);
689 if (shift > fin->fin_dlen) { /* Nasty extension header length? */
690 fin->fin_flx |= FI_BAD;
691 DT3(ipf_fi_bad_pr_ipv6exthdr_len, fr_info_t *, fin, u_short, shift, u_short, fin->fin_dlen);
692 LBUMPD(ipf_stats[fin->fin_out], fr_v6_ext_hlen);
696 fin->fin_dp = (char *)fin->fin_dp + shift;
697 fin->fin_dlen -= shift;
700 * If we have seen a fragment header, do not set any flags to indicate
701 * the presence of this extension header as it has no impact on the
702 * end result until after it has been defragmented.
704 if (fin->fin_flx & FI_FRAG)
707 for (i = 0; ip6exthdr[i].ol_bit != 0; i++)
708 if (ip6exthdr[i].ol_val == proto) {
710 * Most IPv6 extension headers are only allowed once.
712 if ((multiple == 0) &&
713 ((fin->fin_optmsk & ip6exthdr[i].ol_bit) != 0)) {
714 fin->fin_flx |= FI_BAD;
715 DT2(ipf_fi_bad_ipv6exthdr_once, fr_info_t *, fin, u_int, (fin->fin_optmsk & ip6exthdr[i].ol_bit));
717 fin->fin_optmsk |= ip6exthdr[i].ol_bit;
725 /* ------------------------------------------------------------------------ */
726 /* Function: ipf_pr_hopopts6 */
727 /* Returns: int - value of the next header or IPPROTO_NONE if error */
728 /* Parameters: fin(I) - pointer to packet information */
731 /* This is function checks pending hop by hop options extension header */
732 /* ------------------------------------------------------------------------ */
739 hdr = ipf_pr_ipv6exthdr(fin, 0, IPPROTO_HOPOPTS);
742 return hdr->ip6e_nxt;
746 /* ------------------------------------------------------------------------ */
747 /* Function: ipf_pr_mobility6 */
748 /* Returns: int - value of the next header or IPPROTO_NONE if error */
749 /* Parameters: fin(I) - pointer to packet information */
752 /* This is function checks the IPv6 mobility extension header */
753 /* ------------------------------------------------------------------------ */
755 ipf_pr_mobility6(fin)
760 hdr = ipf_pr_ipv6exthdr(fin, 0, IPPROTO_MOBILITY);
763 return hdr->ip6e_nxt;
767 /* ------------------------------------------------------------------------ */
768 /* Function: ipf_pr_routing6 */
769 /* Returns: int - value of the next header or IPPROTO_NONE if error */
770 /* Parameters: fin(I) - pointer to packet information */
773 /* This is function checks pending routing extension header */
774 /* ------------------------------------------------------------------------ */
779 struct ip6_routing *hdr;
781 hdr = (struct ip6_routing *)ipf_pr_ipv6exthdr(fin, 0, IPPROTO_ROUTING);
785 switch (hdr->ip6r_type)
789 * Nasty extension header length?
791 if (((hdr->ip6r_len >> 1) < hdr->ip6r_segleft) ||
792 (hdr->ip6r_segleft && (hdr->ip6r_len & 1))) {
793 ipf_main_softc_t *softc = fin->fin_main_soft;
795 fin->fin_flx |= FI_BAD;
796 DT1(ipf_fi_bad_routing6, fr_info_t *, fin);
797 LBUMPD(ipf_stats[fin->fin_out], fr_v6_rh_bad);
806 return hdr->ip6r_nxt;
810 /* ------------------------------------------------------------------------ */
811 /* Function: ipf_pr_fragment6 */
812 /* Returns: int - value of the next header or IPPROTO_NONE if error */
813 /* Parameters: fin(I) - pointer to packet information */
816 /* Examine the IPv6 fragment header and extract fragment offset information.*/
818 /* Fragments in IPv6 are extraordinarily difficult to deal with - much more */
819 /* so than in IPv4. There are 5 cases of fragments with IPv6 that all */
820 /* packets with a fragment header can fit into. They are as follows: */
822 /* 1. [IPv6][0-n EH][FH][0-n EH] (no L4HDR present) */
823 /* 2. [IPV6][0-n EH][FH][0-n EH][L4HDR part] (short) */
824 /* 3. [IPV6][0-n EH][FH][L4HDR part][0-n data] (short) */
825 /* 4. [IPV6][0-n EH][FH][0-n EH][L4HDR][0-n data] */
826 /* 5. [IPV6][0-n EH][FH][data] */
828 /* IPV6 = IPv6 header, FH = Fragment Header, */
829 /* 0-n EH = 0 or more extension headers, 0-n data = 0 or more bytes of data */
831 /* Packets that match 1, 2, 3 will be dropped as the only reasonable */
832 /* scenario in which they happen is in extreme circumstances that are most */
833 /* likely to be an indication of an attack rather than normal traffic. */
834 /* A type 3 packet may be sent by an attacked after a type 4 packet. There */
835 /* are two rules that can be used to guard against type 3 packets: L4 */
836 /* headers must always be in a packet that has the offset field set to 0 */
837 /* and no packet is allowed to overlay that where offset = 0. */
838 /* ------------------------------------------------------------------------ */
840 ipf_pr_fragment6(fin)
843 ipf_main_softc_t *softc = fin->fin_main_soft;
844 struct ip6_frag *frag;
846 fin->fin_flx |= FI_FRAG;
848 frag = (struct ip6_frag *)ipf_pr_ipv6exthdr(fin, 0, IPPROTO_FRAGMENT);
850 LBUMPD(ipf_stats[fin->fin_out], fr_v6_frag_bad);
854 if ((frag->ip6f_offlg & IP6F_MORE_FRAG) != 0) {
856 * Any fragment that isn't the last fragment must have its
857 * length as a multiple of 8.
859 if ((fin->fin_plen & 7) != 0) {
860 fin->fin_flx |= FI_BAD;
861 DT2(ipf_fi_bad_frag_not_8, fr_info_t *, fin, u_int, (fin->fin_plen & 7));
865 fin->fin_fraghdr = frag;
866 fin->fin_id = frag->ip6f_ident;
867 fin->fin_off = ntohs(frag->ip6f_offlg & IP6F_OFF_MASK);
868 if (fin->fin_off != 0)
869 fin->fin_flx |= FI_FRAGBODY;
872 * Jumbograms aren't handled, so the max. length is 64k
874 if ((fin->fin_off << 3) + fin->fin_dlen > 65535) {
875 fin->fin_flx |= FI_BAD;
876 DT2(ipf_fi_bad_jumbogram, fr_info_t *, fin, u_int, ((fin->fin_off << 3) + fin->fin_dlen));
880 * We don't know where the transport layer header (or whatever is next
881 * is), as it could be behind destination options (amongst others) so
882 * return the fragment header as the type of packet this is. Note that
883 * this effectively disables the fragment cache for > 1 protocol at a
886 return frag->ip6f_nxt;
890 /* ------------------------------------------------------------------------ */
891 /* Function: ipf_pr_dstopts6 */
892 /* Returns: int - value of the next header or IPPROTO_NONE if error */
893 /* Parameters: fin(I) - pointer to packet information */
896 /* This is function checks pending destination options extension header */
897 /* ------------------------------------------------------------------------ */
902 ipf_main_softc_t *softc = fin->fin_main_soft;
905 hdr = ipf_pr_ipv6exthdr(fin, 0, IPPROTO_DSTOPTS);
907 LBUMPD(ipf_stats[fin->fin_out], fr_v6_dst_bad);
910 return hdr->ip6e_nxt;
914 /* ------------------------------------------------------------------------ */
915 /* Function: ipf_pr_icmp6 */
917 /* Parameters: fin(I) - pointer to packet information */
920 /* This routine is mainly concerned with determining the minimum valid size */
921 /* for an ICMPv6 packet. */
922 /* ------------------------------------------------------------------------ */
927 int minicmpsz = sizeof(struct icmp6_hdr);
928 struct icmp6_hdr *icmp6;
930 if (ipf_pr_pullup(fin, ICMP6ERR_MINPKTLEN - sizeof(ip6_t)) == -1) {
931 ipf_main_softc_t *softc = fin->fin_main_soft;
933 LBUMPD(ipf_stats[fin->fin_out], fr_v6_icmp6_pullup);
937 if (fin->fin_dlen > 1) {
942 fin->fin_data[0] = *(u_short *)icmp6;
944 if ((icmp6->icmp6_type & ICMP6_INFOMSG_MASK) != 0)
945 fin->fin_flx |= FI_ICMPQUERY;
947 switch (icmp6->icmp6_type)
949 case ICMP6_ECHO_REPLY :
950 case ICMP6_ECHO_REQUEST :
951 if (fin->fin_dlen >= 6)
952 fin->fin_data[1] = icmp6->icmp6_id;
953 minicmpsz = ICMP6ERR_MINPKTLEN - sizeof(ip6_t);
956 case ICMP6_DST_UNREACH :
957 case ICMP6_PACKET_TOO_BIG :
958 case ICMP6_TIME_EXCEEDED :
959 case ICMP6_PARAM_PROB :
960 fin->fin_flx |= FI_ICMPERR;
961 minicmpsz = ICMP6ERR_IPICMPHLEN - sizeof(ip6_t);
962 if (fin->fin_plen < ICMP6ERR_IPICMPHLEN)
965 if (M_LEN(fin->fin_m) < fin->fin_plen) {
966 if (ipf_coalesce(fin) != 1)
970 if (ipf_pr_pullup(fin, ICMP6ERR_MINPKTLEN) == -1)
974 * If the destination of this packet doesn't match the
975 * source of the original packet then this packet is
979 ip6 = (ip6_t *)((char *)icmp6 + ICMPERR_ICMPHLEN);
980 if (IP6_NEQ(&fin->fin_fi.fi_dst,
981 (i6addr_t *)&ip6->ip6_src)) {
982 fin->fin_flx |= FI_BAD;
983 DT1(ipf_fi_bad_icmp6, fr_info_t *, fin);
991 ipf_pr_short6(fin, minicmpsz);
992 if ((fin->fin_flx & (FI_SHORT|FI_BAD)) == 0) {
993 u_char p = fin->fin_p;
995 fin->fin_p = IPPROTO_ICMPV6;
1002 /* ------------------------------------------------------------------------ */
1003 /* Function: ipf_pr_udp6 */
1005 /* Parameters: fin(I) - pointer to packet information */
1008 /* Analyse the packet for IPv6/UDP properties. */
1009 /* Is not expected to be called for fragmented packets. */
1010 /* ------------------------------------------------------------------------ */
1016 if (ipf_pr_udpcommon(fin) == 0) {
1017 u_char p = fin->fin_p;
1019 fin->fin_p = IPPROTO_UDP;
1020 ipf_checkv6sum(fin);
1026 /* ------------------------------------------------------------------------ */
1027 /* Function: ipf_pr_tcp6 */
1029 /* Parameters: fin(I) - pointer to packet information */
1032 /* Analyse the packet for IPv6/TCP properties. */
1033 /* Is not expected to be called for fragmented packets. */
1034 /* ------------------------------------------------------------------------ */
1040 if (ipf_pr_tcpcommon(fin) == 0) {
1041 u_char p = fin->fin_p;
1043 fin->fin_p = IPPROTO_TCP;
1044 ipf_checkv6sum(fin);
1050 /* ------------------------------------------------------------------------ */
1051 /* Function: ipf_pr_esp6 */
1053 /* Parameters: fin(I) - pointer to packet information */
1056 /* Analyse the packet for ESP properties. */
1057 /* The minimum length is taken to be the SPI (32bits) plus a tail (32bits) */
1058 /* even though the newer ESP packets must also have a sequence number that */
1059 /* is 32bits as well, it is not possible(?) to determine the version from a */
1060 /* simple packet header. */
1061 /* ------------------------------------------------------------------------ */
1067 if ((fin->fin_off == 0) && (ipf_pr_pullup(fin, 8) == -1)) {
1068 ipf_main_softc_t *softc = fin->fin_main_soft;
1070 LBUMPD(ipf_stats[fin->fin_out], fr_v6_esp_pullup);
1076 /* ------------------------------------------------------------------------ */
1077 /* Function: ipf_pr_ah6 */
1078 /* Returns: int - value of the next header or IPPROTO_NONE if error */
1079 /* Parameters: fin(I) - pointer to packet information */
1082 /* Analyse the packet for AH properties. */
1083 /* The minimum length is taken to be the combination of all fields in the */
1084 /* header being present and no authentication data (null algorithm used.) */
1085 /* ------------------------------------------------------------------------ */
1092 fin->fin_flx |= FI_AH;
1094 ah = (authhdr_t *)ipf_pr_ipv6exthdr(fin, 0, IPPROTO_HOPOPTS);
1096 ipf_main_softc_t *softc = fin->fin_main_soft;
1098 LBUMPD(ipf_stats[fin->fin_out], fr_v6_ah_bad);
1099 return IPPROTO_NONE;
1102 ipf_pr_short6(fin, sizeof(*ah));
1105 * No need for another pullup, ipf_pr_ipv6exthdr() will pullup
1106 * enough data to satisfy ah_next (the very first one.)
1112 /* ------------------------------------------------------------------------ */
1113 /* Function: ipf_pr_gre6 */
1115 /* Parameters: fin(I) - pointer to packet information */
1117 /* Analyse the packet for GRE properties. */
1118 /* ------------------------------------------------------------------------ */
1125 if (ipf_pr_pullup(fin, sizeof(grehdr_t)) == -1) {
1126 ipf_main_softc_t *softc = fin->fin_main_soft;
1128 LBUMPD(ipf_stats[fin->fin_out], fr_v6_gre_pullup);
1133 if (GRE_REV(gre->gr_flags) == 1)
1134 fin->fin_data[0] = gre->gr_call;
1136 #endif /* USE_INET6 */
1139 /* ------------------------------------------------------------------------ */
1140 /* Function: ipf_pr_pullup */
1141 /* Returns: int - 0 == pullup succeeded, -1 == failure */
1142 /* Parameters: fin(I) - pointer to packet information */
1143 /* plen(I) - length (excluding L3 header) to pullup */
1145 /* Short inline function to cut down on code duplication to perform a call */
1146 /* to ipf_pullup to ensure there is the required amount of data, */
1147 /* consecutively in the packet buffer. */
1149 /* This function pulls up 'extra' data at the location of fin_dp. fin_dp */
1150 /* points to the first byte after the complete layer 3 header, which will */
1151 /* include all of the known extension headers for IPv6 or options for IPv4. */
1153 /* Since fr_pullup() expects the total length of bytes to be pulled up, it */
1154 /* is necessary to add those we can already assume to be pulled up (fin_dp */
1155 /* - fin_ip) to what is passed through. */
1156 /* ------------------------------------------------------------------------ */
1158 ipf_pr_pullup(fin, plen)
1162 ipf_main_softc_t *softc = fin->fin_main_soft;
1164 if (fin->fin_m != NULL) {
1165 if (fin->fin_dp != NULL)
1166 plen += (char *)fin->fin_dp -
1167 ((char *)fin->fin_ip + fin->fin_hlen);
1168 plen += fin->fin_hlen;
1169 if (M_LEN(fin->fin_m) < plen + fin->fin_ipoff) {
1170 #if defined(_KERNEL)
1171 if (ipf_pullup(fin->fin_m, fin, plen) == NULL) {
1172 DT(ipf_pullup_fail);
1173 LBUMP(ipf_stats[fin->fin_out].fr_pull[1]);
1176 LBUMP(ipf_stats[fin->fin_out].fr_pull[0]);
1178 LBUMP(ipf_stats[fin->fin_out].fr_pull[1]);
1180 * Fake ipf_pullup failing
1182 fin->fin_reason = FRB_PULLUP;
1183 *fin->fin_mp = NULL;
1194 /* ------------------------------------------------------------------------ */
1195 /* Function: ipf_pr_short */
1197 /* Parameters: fin(I) - pointer to packet information */
1198 /* xmin(I) - minimum header size */
1200 /* Check if a packet is "short" as defined by xmin. The rule we are */
1201 /* applying here is that the packet must not be fragmented within the layer */
1202 /* 4 header. That is, it must not be a fragment that has its offset set to */
1203 /* start within the layer 4 header (hdrmin) or if it is at offset 0, the */
1204 /* entire layer 4 header must be present (min). */
1205 /* ------------------------------------------------------------------------ */
1207 ipf_pr_short(fin, xmin)
1212 if (fin->fin_off == 0) {
1213 if (fin->fin_dlen < xmin)
1214 fin->fin_flx |= FI_SHORT;
1215 } else if (fin->fin_off < xmin) {
1216 fin->fin_flx |= FI_SHORT;
1221 /* ------------------------------------------------------------------------ */
1222 /* Function: ipf_pr_icmp */
1224 /* Parameters: fin(I) - pointer to packet information */
1227 /* Do a sanity check on the packet for ICMP (v4). In nearly all cases, */
1228 /* except extrememly bad packets, both type and code will be present. */
1229 /* The expected minimum size of an ICMP packet is very much dependent on */
1230 /* the type of it. */
1232 /* XXX - other ICMP sanity checks? */
1233 /* ------------------------------------------------------------------------ */
1238 ipf_main_softc_t *softc = fin->fin_main_soft;
1239 int minicmpsz = sizeof(struct icmp);
1243 ipf_pr_short(fin, ICMPERR_ICMPHLEN);
1245 if (fin->fin_off != 0) {
1246 LBUMPD(ipf_stats[fin->fin_out], fr_v4_icmp_frag);
1250 if (ipf_pr_pullup(fin, ICMPERR_ICMPHLEN) == -1) {
1251 LBUMPD(ipf_stats[fin->fin_out], fr_v4_icmp_pullup);
1257 fin->fin_data[0] = *(u_short *)icmp;
1258 fin->fin_data[1] = icmp->icmp_id;
1260 switch (icmp->icmp_type)
1262 case ICMP_ECHOREPLY :
1264 /* Router discovery messaes - RFC 1256 */
1265 case ICMP_ROUTERADVERT :
1266 case ICMP_ROUTERSOLICIT :
1267 fin->fin_flx |= FI_ICMPQUERY;
1268 minicmpsz = ICMP_MINLEN;
1271 * type(1) + code(1) + cksum(2) + id(2) seq(2) +
1272 * 3 * timestamp(3 * 4)
1275 case ICMP_TSTAMPREPLY :
1276 fin->fin_flx |= FI_ICMPQUERY;
1280 * type(1) + code(1) + cksum(2) + id(2) seq(2) +
1284 case ICMP_IREQREPLY :
1286 case ICMP_MASKREPLY :
1287 fin->fin_flx |= FI_ICMPQUERY;
1291 * type(1) + code(1) + cksum(2) + id(2) seq(2) + ip(20+)
1295 if (icmp->icmp_code == ICMP_UNREACH_NEEDFRAG) {
1296 if (icmp->icmp_nextmtu < softc->ipf_icmpminfragmtu) {
1297 fin->fin_flx |= FI_BAD;
1298 DT3(ipf_fi_bad_icmp_nextmtu, fr_info_t *, fin, u_int, icmp->icmp_nextmtu, u_int, softc->ipf_icmpminfragmtu);
1303 case ICMP_SOURCEQUENCH :
1304 case ICMP_REDIRECT :
1305 case ICMP_TIMXCEED :
1306 case ICMP_PARAMPROB :
1307 fin->fin_flx |= FI_ICMPERR;
1308 if (ipf_coalesce(fin) != 1) {
1309 LBUMPD(ipf_stats[fin->fin_out], fr_icmp_coalesce);
1314 * ICMP error packets should not be generated for IP
1315 * packets that are a fragment that isn't the first
1318 oip = (ip_t *)((char *)fin->fin_dp + ICMPERR_ICMPHLEN);
1319 if ((ntohs(oip->ip_off) & IP_OFFMASK) != 0) {
1320 fin->fin_flx |= FI_BAD;
1321 DT2(ipf_fi_bad_icmp_err, fr_info_t, fin, u_int, (ntohs(oip->ip_off) & IP_OFFMASK));
1325 * If the destination of this packet doesn't match the
1326 * source of the original packet then this packet is
1329 if (oip->ip_src.s_addr != fin->fin_daddr) {
1330 fin->fin_flx |= FI_BAD;
1331 DT1(ipf_fi_bad_src_ne_dst, fr_info_t *, fin);
1338 ipf_pr_short(fin, minicmpsz);
1340 ipf_checkv4sum(fin);
1344 /* ------------------------------------------------------------------------ */
1345 /* Function: ipf_pr_tcpcommon */
1346 /* Returns: int - 0 = header ok, 1 = bad packet, -1 = buffer error */
1347 /* Parameters: fin(I) - pointer to packet information */
1349 /* TCP header sanity checking. Look for bad combinations of TCP flags, */
1350 /* and make some checks with how they interact with other fields. */
1351 /* If compiled with IPFILTER_CKSUM, check to see if the TCP checksum is */
1352 /* valid and mark the packet as bad if not. */
1353 /* ------------------------------------------------------------------------ */
1355 ipf_pr_tcpcommon(fin)
1358 ipf_main_softc_t *softc = fin->fin_main_soft;
1362 fin->fin_flx |= FI_TCPUDP;
1363 if (fin->fin_off != 0) {
1364 LBUMPD(ipf_stats[fin->fin_out], fr_tcp_frag);
1368 if (ipf_pr_pullup(fin, sizeof(*tcp)) == -1) {
1369 LBUMPD(ipf_stats[fin->fin_out], fr_tcp_pullup);
1374 if (fin->fin_dlen > 3) {
1375 fin->fin_sport = ntohs(tcp->th_sport);
1376 fin->fin_dport = ntohs(tcp->th_dport);
1379 if ((fin->fin_flx & FI_SHORT) != 0) {
1380 LBUMPD(ipf_stats[fin->fin_out], fr_tcp_short);
1385 * Use of the TCP data offset *must* result in a value that is at
1386 * least the same size as the TCP header.
1388 tlen = TCP_OFF(tcp) << 2;
1389 if (tlen < sizeof(tcphdr_t)) {
1390 LBUMPD(ipf_stats[fin->fin_out], fr_tcp_small);
1391 fin->fin_flx |= FI_BAD;
1392 DT3(ipf_fi_bad_tlen, fr_info_t, fin, u_int, tlen, u_int, sizeof(tcphdr_t));
1396 flags = tcp->th_flags;
1397 fin->fin_tcpf = tcp->th_flags;
1400 * If the urgent flag is set, then the urgent pointer must
1401 * also be set and vice versa. Good TCP packets do not have
1402 * just one of these set.
1404 if ((flags & TH_URG) != 0 && (tcp->th_urp == 0)) {
1405 fin->fin_flx |= FI_BAD;
1406 DT3(ipf_fi_bad_th_urg, fr_info_t*, fin, u_int, (flags & TH_URG), u_int, tcp->th_urp);
1408 } else if ((flags & TH_URG) == 0 && (tcp->th_urp != 0)) {
1410 * Ignore this case (#if 0) as it shows up in "real"
1411 * traffic with bogus values in the urgent pointer field.
1413 fin->fin_flx |= FI_BAD;
1414 DT3(ipf_fi_bad_th_urg0, fr_info_t *, fin, u_int, (flags & TH_URG), u_int, tcp->th_urp);
1416 } else if (((flags & (TH_SYN|TH_FIN)) != 0) &&
1417 ((flags & (TH_RST|TH_ACK)) == TH_RST)) {
1418 /* TH_FIN|TH_RST|TH_ACK seems to appear "naturally" */
1419 fin->fin_flx |= FI_BAD;
1420 DT1(ipf_fi_bad_th_fin_rst_ack, fr_info_t, fin);
1422 } else if (((flags & TH_SYN) != 0) &&
1423 ((flags & (TH_URG|TH_PUSH)) != 0)) {
1425 * SYN with URG and PUSH set is not for normal TCP but it is
1426 * possible(?) with T/TCP...but who uses T/TCP?
1428 fin->fin_flx |= FI_BAD;
1429 DT1(ipf_fi_bad_th_syn_urg_psh, fr_info_t *, fin);
1431 } else if (!(flags & TH_ACK)) {
1433 * If the ack bit isn't set, then either the SYN or
1434 * RST bit must be set. If the SYN bit is set, then
1435 * we expect the ACK field to be 0. If the ACK is
1436 * not set and if URG, PSH or FIN are set, consdier
1437 * that to indicate a bad TCP packet.
1439 if ((flags == TH_SYN) && (tcp->th_ack != 0)) {
1441 * Cisco PIX sets the ACK field to a random value.
1442 * In light of this, do not set FI_BAD until a patch
1443 * is available from Cisco to ensure that
1444 * interoperability between existing systems is
1447 /*fin->fin_flx |= FI_BAD*/;
1448 /*DT1(ipf_fi_bad_th_syn_ack, fr_info_t *, fin);*/
1449 } else if (!(flags & (TH_RST|TH_SYN))) {
1450 fin->fin_flx |= FI_BAD;
1451 DT1(ipf_fi_bad_th_rst_syn, fr_info_t *, fin);
1452 } else if ((flags & (TH_URG|TH_PUSH|TH_FIN)) != 0) {
1453 fin->fin_flx |= FI_BAD;
1454 DT1(ipf_fi_bad_th_urg_push_fin, fr_info_t *, fin);
1457 if (fin->fin_flx & FI_BAD) {
1458 LBUMPD(ipf_stats[fin->fin_out], fr_tcp_bad_flags);
1463 * At this point, it's not exactly clear what is to be gained by
1464 * marking up which TCP options are and are not present. The one we
1465 * are most interested in is the TCP window scale. This is only in
1466 * a SYN packet [RFC1323] so we don't need this here...?
1467 * Now if we were to analyse the header for passive fingerprinting,
1468 * then that might add some weight to adding this...
1470 if (tlen == sizeof(tcphdr_t)) {
1474 if (ipf_pr_pullup(fin, tlen) == -1) {
1475 LBUMPD(ipf_stats[fin->fin_out], fr_tcp_pullup);
1482 s = (u_char *)(tcp + 1);
1483 off = IP_HL(ip) << 2;
1485 if (fin->fin_mp != NULL) {
1486 mb_t *m = *fin->fin_mp;
1488 if (off + tlen > M_LEN(m))
1492 for (tlen -= (int)sizeof(*tcp); tlen > 0; ) {
1496 else if (opt == TCPOPT_NOP)
1502 if (ol < 2 || ol > tlen)
1506 for (i = 9, mv = 4; mv >= 0; ) {
1508 if (opt == (u_char)op->ol_val) {
1509 optmsk |= op->ol_bit;
1523 /* ------------------------------------------------------------------------ */
1524 /* Function: ipf_pr_udpcommon */
1525 /* Returns: int - 0 = header ok, 1 = bad packet */
1526 /* Parameters: fin(I) - pointer to packet information */
1528 /* Extract the UDP source and destination ports, if present. If compiled */
1529 /* with IPFILTER_CKSUM, check to see if the UDP checksum is valid. */
1530 /* ------------------------------------------------------------------------ */
1532 ipf_pr_udpcommon(fin)
1537 fin->fin_flx |= FI_TCPUDP;
1539 if (!fin->fin_off && (fin->fin_dlen > 3)) {
1540 if (ipf_pr_pullup(fin, sizeof(*udp)) == -1) {
1541 ipf_main_softc_t *softc = fin->fin_main_soft;
1543 fin->fin_flx |= FI_SHORT;
1544 LBUMPD(ipf_stats[fin->fin_out], fr_udp_pullup);
1550 fin->fin_sport = ntohs(udp->uh_sport);
1551 fin->fin_dport = ntohs(udp->uh_dport);
1558 /* ------------------------------------------------------------------------ */
1559 /* Function: ipf_pr_tcp */
1561 /* Parameters: fin(I) - pointer to packet information */
1564 /* Analyse the packet for IPv4/TCP properties. */
1565 /* ------------------------------------------------------------------------ */
1571 ipf_pr_short(fin, sizeof(tcphdr_t));
1573 if (ipf_pr_tcpcommon(fin) == 0)
1574 ipf_checkv4sum(fin);
1578 /* ------------------------------------------------------------------------ */
1579 /* Function: ipf_pr_udp */
1581 /* Parameters: fin(I) - pointer to packet information */
1584 /* Analyse the packet for IPv4/UDP properties. */
1585 /* ------------------------------------------------------------------------ */
1591 ipf_pr_short(fin, sizeof(udphdr_t));
1593 if (ipf_pr_udpcommon(fin) == 0)
1594 ipf_checkv4sum(fin);
1598 /* ------------------------------------------------------------------------ */
1599 /* Function: ipf_pr_esp */
1601 /* Parameters: fin(I) - pointer to packet information */
1603 /* Analyse the packet for ESP properties. */
1604 /* The minimum length is taken to be the SPI (32bits) plus a tail (32bits) */
1605 /* even though the newer ESP packets must also have a sequence number that */
1606 /* is 32bits as well, it is not possible(?) to determine the version from a */
1607 /* simple packet header. */
1608 /* ------------------------------------------------------------------------ */
1614 if (fin->fin_off == 0) {
1615 ipf_pr_short(fin, 8);
1616 if (ipf_pr_pullup(fin, 8) == -1) {
1617 ipf_main_softc_t *softc = fin->fin_main_soft;
1619 LBUMPD(ipf_stats[fin->fin_out], fr_v4_esp_pullup);
1625 /* ------------------------------------------------------------------------ */
1626 /* Function: ipf_pr_ah */
1627 /* Returns: int - value of the next header or IPPROTO_NONE if error */
1628 /* Parameters: fin(I) - pointer to packet information */
1630 /* Analyse the packet for AH properties. */
1631 /* The minimum length is taken to be the combination of all fields in the */
1632 /* header being present and no authentication data (null algorithm used.) */
1633 /* ------------------------------------------------------------------------ */
1638 ipf_main_softc_t *softc = fin->fin_main_soft;
1642 fin->fin_flx |= FI_AH;
1643 ipf_pr_short(fin, sizeof(*ah));
1645 if (((fin->fin_flx & FI_SHORT) != 0) || (fin->fin_off != 0)) {
1646 LBUMPD(ipf_stats[fin->fin_out], fr_v4_ah_bad);
1647 return IPPROTO_NONE;
1650 if (ipf_pr_pullup(fin, sizeof(*ah)) == -1) {
1651 DT(fr_v4_ah_pullup_1);
1652 LBUMP(ipf_stats[fin->fin_out].fr_v4_ah_pullup);
1653 return IPPROTO_NONE;
1656 ah = (authhdr_t *)fin->fin_dp;
1658 len = (ah->ah_plen + 2) << 2;
1659 ipf_pr_short(fin, len);
1660 if (ipf_pr_pullup(fin, len) == -1) {
1661 DT(fr_v4_ah_pullup_2);
1662 LBUMP(ipf_stats[fin->fin_out].fr_v4_ah_pullup);
1663 return IPPROTO_NONE;
1667 * Adjust fin_dp and fin_dlen for skipping over the authentication
1670 fin->fin_dp = (char *)fin->fin_dp + len;
1671 fin->fin_dlen -= len;
1676 /* ------------------------------------------------------------------------ */
1677 /* Function: ipf_pr_gre */
1679 /* Parameters: fin(I) - pointer to packet information */
1681 /* Analyse the packet for GRE properties. */
1682 /* ------------------------------------------------------------------------ */
1687 ipf_main_softc_t *softc = fin->fin_main_soft;
1690 ipf_pr_short(fin, sizeof(grehdr_t));
1692 if (fin->fin_off != 0) {
1693 LBUMPD(ipf_stats[fin->fin_out], fr_v4_gre_frag);
1697 if (ipf_pr_pullup(fin, sizeof(grehdr_t)) == -1) {
1698 LBUMPD(ipf_stats[fin->fin_out], fr_v4_gre_pullup);
1703 if (GRE_REV(gre->gr_flags) == 1)
1704 fin->fin_data[0] = gre->gr_call;
1708 /* ------------------------------------------------------------------------ */
1709 /* Function: ipf_pr_ipv4hdr */
1711 /* Parameters: fin(I) - pointer to packet information */
1714 /* Analyze the IPv4 header and set fields in the fr_info_t structure. */
1715 /* Check all options present and flag their presence if any exist. */
1716 /* ------------------------------------------------------------------------ */
1721 u_short optmsk = 0, secmsk = 0, auth = 0;
1722 int hlen, ol, mv, p, i;
1723 const struct optlist *op;
1730 hlen = fin->fin_hlen;
1736 fi->fi_tos = ip->ip_tos;
1737 fin->fin_id = ip->ip_id;
1738 off = ntohs(ip->ip_off);
1740 /* Get both TTL and protocol */
1741 fi->fi_p = ip->ip_p;
1742 fi->fi_ttl = ip->ip_ttl;
1744 /* Zero out bits not used in IPv6 address */
1745 fi->fi_src.i6[1] = 0;
1746 fi->fi_src.i6[2] = 0;
1747 fi->fi_src.i6[3] = 0;
1748 fi->fi_dst.i6[1] = 0;
1749 fi->fi_dst.i6[2] = 0;
1750 fi->fi_dst.i6[3] = 0;
1752 fi->fi_saddr = ip->ip_src.s_addr;
1753 fin->fin_crc += fi->fi_saddr;
1754 fi->fi_daddr = ip->ip_dst.s_addr;
1755 fin->fin_crc += fi->fi_daddr;
1756 if (IN_CLASSD(ntohl(fi->fi_daddr)))
1757 fin->fin_flx |= FI_MULTICAST|FI_MBCAST;
1760 * set packet attribute flags based on the offset and
1761 * calculate the byte offset that it represents.
1763 off &= IP_MF|IP_OFFMASK;
1765 int morefrag = off & IP_MF;
1767 fi->fi_flx |= FI_FRAG;
1770 fin->fin_flx |= FI_FRAGBODY;
1772 if ((off + fin->fin_dlen > 65535) ||
1773 (fin->fin_dlen == 0) ||
1774 ((morefrag != 0) && ((fin->fin_dlen & 7) != 0))) {
1776 * The length of the packet, starting at its
1777 * offset cannot exceed 65535 (0xffff) as the
1778 * length of an IP packet is only 16 bits.
1780 * Any fragment that isn't the last fragment
1781 * must have a length greater than 0 and it
1782 * must be an even multiple of 8.
1784 fi->fi_flx |= FI_BAD;
1785 DT1(ipf_fi_bad_fragbody_gt_65535, fr_info_t *, fin);
1792 * Call per-protocol setup and checking
1794 if (p == IPPROTO_AH) {
1796 * Treat AH differently because we expect there to be another
1797 * layer 4 header after it.
1826 * If it is a standard IP header (no options), set the flag fields
1827 * which relate to options to 0.
1829 if (hlen == sizeof(*ip)) {
1837 * So the IP header has some IP options attached. Walk the entire
1838 * list of options present with this packet and set flags to indicate
1839 * which ones are here and which ones are not. For the somewhat out
1840 * of date and obscure security classification options, set a flag to
1841 * represent which classification is present.
1843 fi->fi_flx |= FI_OPTIONS;
1845 for (s = (u_char *)(ip + 1), hlen -= (int)sizeof(*ip); hlen > 0; ) {
1849 else if (opt == IPOPT_NOP)
1855 if (ol < 2 || ol > hlen)
1858 for (i = 9, mv = 4; mv >= 0; ) {
1861 if ((opt == (u_char)op->ol_val) && (ol > 4)) {
1866 case IPOPT_SECURITY :
1867 if (optmsk & op->ol_bit) {
1868 fin->fin_flx |= FI_BAD;
1869 DT2(ipf_fi_bad_ipopt_security, fr_info_t *, fin, u_short, (optmsk & op->ol_bit));
1871 doi = ipf_checkripso(s);
1873 auth = doi & 0xffff;
1879 if (optmsk & op->ol_bit) {
1880 fin->fin_flx |= FI_BAD;
1881 DT2(ipf_fi_bad_ipopt_cipso, fr_info_t *, fin, u_short, (optmsk & op->ol_bit));
1883 doi = ipf_checkcipso(fin,
1886 auth = doi & 0xffff;
1890 optmsk |= op->ol_bit;
1893 if (opt < op->ol_val)
1906 if (auth && !(auth & 0x0100))
1908 fi->fi_optmsk = optmsk;
1909 fi->fi_secmsk = secmsk;
1914 /* ------------------------------------------------------------------------ */
1915 /* Function: ipf_checkripso */
1917 /* Parameters: s(I) - pointer to start of RIPSO option */
1919 /* ------------------------------------------------------------------------ */
1924 const struct optlist *sp;
1925 u_short secmsk = 0, auth = 0;
1929 sec = *(s + 2); /* classification */
1930 for (j = 3, m = 2; m >= 0; ) {
1932 if (sec == sp->ol_val) {
1933 secmsk |= sp->ol_bit;
1939 if (sec < sp->ol_val)
1946 return (secmsk << 16) | auth;
1950 /* ------------------------------------------------------------------------ */
1951 /* Function: ipf_checkcipso */
1952 /* Returns: u_32_t - 0 = failure, else the doi from the header */
1953 /* Parameters: fin(IO) - pointer to packet information */
1954 /* s(I) - pointer to start of CIPSO option */
1955 /* ol(I) - length of CIPSO option field */
1957 /* This function returns the domain of integrity (DOI) field from the CIPSO */
1958 /* header and returns that whilst also storing the highest sensitivity */
1959 /* value found in the fr_info_t structure. */
1961 /* No attempt is made to extract the category bitmaps as these are defined */
1962 /* by the user (rather than the protocol) and can be rather numerous on the */
1964 /* ------------------------------------------------------------------------ */
1966 ipf_checkcipso(fin, s, ol)
1971 ipf_main_softc_t *softc = fin->fin_main_soft;
1974 u_char *t, tag, tlen, sensitivity;
1977 if (ol < 6 || ol > 40) {
1978 LBUMPD(ipf_stats[fin->fin_out], fr_v4_cipso_bad);
1979 fin->fin_flx |= FI_BAD;
1980 DT2(ipf_fi_bad_checkcipso_ol, fr_info_t *, fin, u_int, ol);
1985 fi->fi_sensitivity = 0;
1987 * The DOI field MUST be there.
1989 bcopy(s + 2, &doi, sizeof(doi));
1991 t = (u_char *)s + 6;
1992 for (len = ol - 6; len >= 2; len -= tlen, t+= tlen) {
1995 if (tlen > len || tlen < 4 || tlen > 34) {
1996 LBUMPD(ipf_stats[fin->fin_out], fr_v4_cipso_tlen);
1997 fin->fin_flx |= FI_BAD;
1998 DT2(ipf_fi_bad_checkcipso_tlen, fr_info_t *, fin, u_int, tlen);
2004 * Tag numbers 0, 1, 2, 5 are laid out in the CIPSO Internet
2005 * draft (16 July 1992) that has expired.
2008 fin->fin_flx |= FI_BAD;
2009 DT2(ipf_fi_bad_checkcipso_tag, fr_info_t *, fin, u_int, tag);
2011 } else if (tag == 1) {
2012 if (*(t + 2) != 0) {
2013 fin->fin_flx |= FI_BAD;
2014 DT2(ipf_fi_bad_checkcipso_tag1_t2, fr_info_t *, fin, u_int, (*t + 2));
2017 sensitivity = *(t + 3);
2018 /* Category bitmap for categories 0-239 */
2020 } else if (tag == 4) {
2021 if (*(t + 2) != 0) {
2022 fin->fin_flx |= FI_BAD;
2023 DT2(ipf_fi_bad_checkcipso_tag4_t2, fr_info_t *, fin, u_int, (*t + 2));
2026 sensitivity = *(t + 3);
2027 /* Enumerated categories, 16bits each, upto 15 */
2029 } else if (tag == 5) {
2030 if (*(t + 2) != 0) {
2031 fin->fin_flx |= FI_BAD;
2032 DT2(ipf_fi_bad_checkcipso_tag5_t2, fr_info_t *, fin, u_int, (*t + 2));
2035 sensitivity = *(t + 3);
2036 /* Range of categories (2*16bits), up to 7 pairs */
2038 } else if (tag > 127) {
2039 /* Custom defined DOI */
2042 fin->fin_flx |= FI_BAD;
2043 DT2(ipf_fi_bad_checkcipso_tag127, fr_info_t *, fin, u_int, tag);
2047 if (sensitivity > fi->fi_sensitivity)
2048 fi->fi_sensitivity = sensitivity;
2055 /* ------------------------------------------------------------------------ */
2056 /* Function: ipf_makefrip */
2057 /* Returns: int - 0 == packet ok, -1 == packet freed */
2058 /* Parameters: hlen(I) - length of IP packet header */
2059 /* ip(I) - pointer to the IP header */
2060 /* fin(IO) - pointer to packet information */
2062 /* Compact the IP header into a structure which contains just the info. */
2063 /* which is useful for comparing IP headers with and store this information */
2064 /* in the fr_info_t structure pointer to by fin. At present, it is assumed */
2065 /* this function will be called with either an IPv4 or IPv6 packet. */
2066 /* ------------------------------------------------------------------------ */
2068 ipf_makefrip(hlen, ip, fin)
2073 ipf_main_softc_t *softc = fin->fin_main_soft;
2077 fin->fin_hlen = (u_short)hlen;
2079 fin->fin_rule = 0xffffffff;
2080 fin->fin_group[0] = -1;
2081 fin->fin_group[1] = '\0';
2082 fin->fin_dp = (char *)ip + hlen;
2086 fin->fin_plen = ntohs(ip->ip_len);
2087 fin->fin_dlen = fin->fin_plen - hlen;
2088 ipf_pr_ipv4hdr(fin);
2090 } else if (v == 6) {
2091 fin->fin_plen = ntohs(((ip6_t *)ip)->ip6_plen);
2092 fin->fin_dlen = fin->fin_plen;
2093 fin->fin_plen += hlen;
2095 ipf_pr_ipv6hdr(fin);
2098 if (fin->fin_ip == NULL) {
2099 LBUMP(ipf_stats[fin->fin_out].fr_ip_freed);
2106 /* ------------------------------------------------------------------------ */
2107 /* Function: ipf_portcheck */
2108 /* Returns: int - 1 == port matched, 0 == port match failed */
2109 /* Parameters: frp(I) - pointer to port check `expression' */
2110 /* pop(I) - port number to evaluate */
2112 /* Perform a comparison of a port number against some other(s), using a */
2113 /* structure with compare information stored in it. */
2114 /* ------------------------------------------------------------------------ */
2116 ipf_portcheck(frp, pop)
2126 * Do opposite test to that required and continue if that succeeds.
2128 switch (frp->frp_cmp)
2131 if (pop != po) /* EQUAL */
2135 if (pop == po) /* NOTEQUAL */
2139 if (pop >= po) /* LESSTHAN */
2143 if (pop <= po) /* GREATERTHAN */
2147 if (pop > po) /* LT or EQ */
2151 if (pop < po) /* GT or EQ */
2155 if (pop >= po && pop <= frp->frp_top) /* Out of range */
2159 if (pop <= po || pop >= frp->frp_top) /* In range */
2163 if (pop < po || pop > frp->frp_top) /* Inclusive range */
2173 /* ------------------------------------------------------------------------ */
2174 /* Function: ipf_tcpudpchk */
2175 /* Returns: int - 1 == protocol matched, 0 == check failed */
2176 /* Parameters: fda(I) - pointer to packet information */
2177 /* ft(I) - pointer to structure with comparison data */
2179 /* Compares the current pcket (assuming it is TCP/UDP) information with a */
2180 /* structure containing information that we want to match against. */
2181 /* ------------------------------------------------------------------------ */
2183 ipf_tcpudpchk(fi, ft)
2190 * Both ports should *always* be in the first fragment.
2191 * So far, I cannot find any cases where they can not be.
2193 * compare destination ports
2196 err = ipf_portcheck(&ft->ftu_dst, fi->fi_ports[1]);
2199 * compare source ports
2201 if (err && ft->ftu_scmp)
2202 err = ipf_portcheck(&ft->ftu_src, fi->fi_ports[0]);
2205 * If we don't have all the TCP/UDP header, then how can we
2206 * expect to do any sort of match on it ? If we were looking for
2207 * TCP flags, then NO match. If not, then match (which should
2208 * satisfy the "short" class too).
2210 if (err && (fi->fi_p == IPPROTO_TCP)) {
2211 if (fi->fi_flx & FI_SHORT)
2212 return !(ft->ftu_tcpf | ft->ftu_tcpfm);
2214 * Match the flags ? If not, abort this match.
2216 if (ft->ftu_tcpfm &&
2217 ft->ftu_tcpf != (fi->fi_tcpf & ft->ftu_tcpfm)) {
2218 FR_DEBUG(("f. %#x & %#x != %#x\n", fi->fi_tcpf,
2219 ft->ftu_tcpfm, ft->ftu_tcpf));
2227 /* ------------------------------------------------------------------------ */
2228 /* Function: ipf_check_ipf */
2229 /* Returns: int - 0 == match, else no match */
2230 /* Parameters: fin(I) - pointer to packet information */
2231 /* fr(I) - pointer to filter rule */
2232 /* portcmp(I) - flag indicating whether to attempt matching on */
2233 /* TCP/UDP port data. */
2235 /* Check to see if a packet matches an IPFilter rule. Checks of addresses, */
2236 /* port numbers, etc, for "standard" IPFilter rules are all orchestrated in */
2237 /* this function. */
2238 /* ------------------------------------------------------------------------ */
2240 ipf_check_ipf(fin, fr, portcmp)
2245 u_32_t *ld, *lm, *lip;
2253 lm = (u_32_t *)&fri->fri_mip;
2254 ld = (u_32_t *)&fri->fri_ip;
2257 * first 32 bits to check coversion:
2258 * IP version, TOS, TTL, protocol
2260 i = ((*lip & *lm) != *ld);
2261 FR_DEBUG(("0. %#08x & %#08x != %#08x\n",
2262 ntohl(*lip), ntohl(*lm), ntohl(*ld)));
2267 * Next 32 bits is a constructed bitmask indicating which IP options
2268 * are present (if any) in this packet.
2271 i = ((*lip & *lm) != *ld);
2272 FR_DEBUG(("1. %#08x & %#08x != %#08x\n",
2273 ntohl(*lip), ntohl(*lm), ntohl(*ld)));
2279 * Unrolled loops (4 each, for 32 bits) for address checks.
2282 * Check the source address.
2284 if (fr->fr_satype == FRI_LOOKUP) {
2285 i = (*fr->fr_srcfunc)(fin->fin_main_soft, fr->fr_srcptr,
2286 fi->fi_v, lip, fin->fin_plen);
2293 i = ((*lip & *lm) != *ld);
2294 FR_DEBUG(("2a. %#08x & %#08x != %#08x\n",
2295 ntohl(*lip), ntohl(*lm), ntohl(*ld)));
2296 if (fi->fi_v == 6) {
2298 i |= ((*lip & *lm) != *ld);
2299 FR_DEBUG(("2b. %#08x & %#08x != %#08x\n",
2300 ntohl(*lip), ntohl(*lm), ntohl(*ld)));
2302 i |= ((*lip & *lm) != *ld);
2303 FR_DEBUG(("2c. %#08x & %#08x != %#08x\n",
2304 ntohl(*lip), ntohl(*lm), ntohl(*ld)));
2306 i |= ((*lip & *lm) != *ld);
2307 FR_DEBUG(("2d. %#08x & %#08x != %#08x\n",
2308 ntohl(*lip), ntohl(*lm), ntohl(*ld)));
2315 i ^= (fr->fr_flags & FR_NOTSRCIP) >> 6;
2320 * Check the destination address.
2323 if (fr->fr_datype == FRI_LOOKUP) {
2324 i = (*fr->fr_dstfunc)(fin->fin_main_soft, fr->fr_dstptr,
2325 fi->fi_v, lip, fin->fin_plen);
2332 i = ((*lip & *lm) != *ld);
2333 FR_DEBUG(("3a. %#08x & %#08x != %#08x\n",
2334 ntohl(*lip), ntohl(*lm), ntohl(*ld)));
2335 if (fi->fi_v == 6) {
2337 i |= ((*lip & *lm) != *ld);
2338 FR_DEBUG(("3b. %#08x & %#08x != %#08x\n",
2339 ntohl(*lip), ntohl(*lm), ntohl(*ld)));
2341 i |= ((*lip & *lm) != *ld);
2342 FR_DEBUG(("3c. %#08x & %#08x != %#08x\n",
2343 ntohl(*lip), ntohl(*lm), ntohl(*ld)));
2345 i |= ((*lip & *lm) != *ld);
2346 FR_DEBUG(("3d. %#08x & %#08x != %#08x\n",
2347 ntohl(*lip), ntohl(*lm), ntohl(*ld)));
2354 i ^= (fr->fr_flags & FR_NOTDSTIP) >> 7;
2358 * IP addresses matched. The next 32bits contains:
2359 * mast of old IP header security & authentication bits.
2362 i = (*ld - (*lip & *lm));
2363 FR_DEBUG(("4. %#08x & %#08x != %#08x\n", *lip, *lm, *ld));
2366 * Next we have 32 bits of packet flags.
2369 i |= (*ld - (*lip & *lm));
2370 FR_DEBUG(("5. %#08x & %#08x != %#08x\n", *lip, *lm, *ld));
2374 * If a fragment, then only the first has what we're
2375 * looking for here...
2378 if (!ipf_tcpudpchk(&fin->fin_fi, &fr->fr_tuc))
2381 if (fr->fr_dcmp || fr->fr_scmp ||
2382 fr->fr_tcpf || fr->fr_tcpfm)
2384 if (fr->fr_icmpm || fr->fr_icmp) {
2385 if (((fi->fi_p != IPPROTO_ICMP) &&
2386 (fi->fi_p != IPPROTO_ICMPV6)) ||
2387 fin->fin_off || (fin->fin_dlen < 2))
2389 else if ((fin->fin_data[0] & fr->fr_icmpm) !=
2391 FR_DEBUG(("i. %#x & %#x != %#x\n",
2393 fr->fr_icmpm, fr->fr_icmp));
2403 /* ------------------------------------------------------------------------ */
2404 /* Function: ipf_scanlist */
2405 /* Returns: int - result flags of scanning filter list */
2406 /* Parameters: fin(I) - pointer to packet information */
2407 /* pass(I) - default result to return for filtering */
2409 /* Check the input/output list of rules for a match to the current packet. */
2410 /* If a match is found, the value of fr_flags from the rule becomes the */
2411 /* return value and fin->fin_fr points to the matched rule. */
2413 /* This function may be called recusively upto 16 times (limit inbuilt.) */
2414 /* When unwinding, it should finish up with fin_depth as 0. */
2416 /* Could be per interface, but this gets real nasty when you don't have, */
2417 /* or can't easily change, the kernel source code to . */
2418 /* ------------------------------------------------------------------------ */
2420 ipf_scanlist(fin, pass)
2424 ipf_main_softc_t *softc = fin->fin_main_soft;
2425 int rulen, portcmp, off, skip;
2426 struct frentry *fr, *fnext;
2427 u_32_t passt, passo;
2430 * Do not allow nesting deeper than 16 levels.
2432 if (fin->fin_depth >= 16)
2438 * If there are no rules in this list, return now.
2449 if ((fin->fin_flx & FI_TCPUDP) && (fin->fin_dlen > 3) && !off)
2452 for (rulen = 0; fr; fr = fnext, rulen++) {
2453 fnext = fr->fr_next;
2455 FR_VERBOSE(("SKIP %d (%#x)\n", skip, fr->fr_flags));
2461 * In all checks below, a null (zero) value in the
2462 * filter struture is taken to mean a wildcard.
2464 * check that we are working for the right interface
2467 if (fr->fr_ifa && fr->fr_ifa != fin->fin_ifp)
2470 if (opts & (OPT_VERBOSE|OPT_DEBUG))
2472 FR_VERBOSE(("%c", FR_ISSKIP(pass) ? 's' :
2473 FR_ISPASS(pass) ? 'p' :
2474 FR_ISACCOUNT(pass) ? 'A' :
2475 FR_ISAUTH(pass) ? 'a' :
2476 (pass & FR_NOMATCH) ? 'n' :'b'));
2477 if (fr->fr_ifa && fr->fr_ifa != fin->fin_ifp)
2482 switch (fr->fr_type)
2485 case FR_T_IPF_BUILTIN :
2486 if (ipf_check_ipf(fin, fr, portcmp))
2489 #if defined(IPFILTER_BPF)
2491 case FR_T_BPFOPC_BUILTIN :
2496 if (*fin->fin_mp == NULL)
2498 if (fin->fin_family != fr->fr_family)
2500 mc = (u_char *)fin->fin_m;
2501 wlen = fin->fin_dlen + fin->fin_hlen;
2502 if (!bpf_filter(fr->fr_data, mc, wlen, 0))
2507 case FR_T_CALLFUNC_BUILTIN :
2511 f = (*fr->fr_func)(fin, &pass);
2520 case FR_T_IPFEXPR_BUILTIN :
2521 if (fin->fin_family != fr->fr_family)
2523 if (ipf_fr_matcharray(fin, fr->fr_data) == 0)
2531 if ((fin->fin_out == 0) && (fr->fr_nattag.ipt_num[0] != 0)) {
2532 if (fin->fin_nattag == NULL)
2534 if (ipf_matchtag(&fr->fr_nattag, fin->fin_nattag) == 0)
2537 FR_VERBOSE(("=%d/%d.%d *", fr->fr_grhead, fr->fr_group, rulen));
2539 passt = fr->fr_flags;
2542 * If the rule is a "call now" rule, then call the function
2543 * in the rule, if it exists and use the results from that.
2544 * If the function pointer is bad, just make like we ignore
2545 * it, except for increasing the hit counter.
2547 if ((passt & FR_CALLNOW) != 0) {
2550 ATOMIC_INC64(fr->fr_hits);
2551 if ((fr->fr_func == NULL) ||
2552 (fr->fr_func == (ipfunc_t)-1))
2557 fr = (*fr->fr_func)(fin, &passt);
2562 passt = fr->fr_flags;
2568 * Just log this packet...
2570 if ((passt & FR_LOGMASK) == FR_LOG) {
2571 if (ipf_log_pkt(fin, passt) == -1) {
2572 if (passt & FR_LOGORBLOCK) {
2574 passt &= ~FR_CMDMASK;
2575 passt |= FR_BLOCK|FR_QUICK;
2576 fin->fin_reason = FRB_LOGFAIL;
2580 #endif /* IPFILTER_LOG */
2582 MUTEX_ENTER(&fr->fr_lock);
2583 fr->fr_bytes += (U_QUAD_T)fin->fin_plen;
2585 MUTEX_EXIT(&fr->fr_lock);
2586 fin->fin_rule = rulen;
2589 if (FR_ISSKIP(passt)) {
2592 } else if (((passt & FR_LOGMASK) != FR_LOG) &&
2593 ((passt & FR_LOGMASK) != FR_DECAPSULATE)) {
2597 if (passt & (FR_RETICMP|FR_FAKEICMP))
2598 fin->fin_icode = fr->fr_icode;
2600 if (fr->fr_group != -1) {
2601 (void) strncpy(fin->fin_group,
2602 FR_NAME(fr, fr_group),
2603 strlen(FR_NAME(fr, fr_group)));
2605 fin->fin_group[0] = '\0';
2608 FR_DEBUG(("pass %#x/%#x/%x\n", passo, pass, passt));
2610 if (fr->fr_grphead != NULL) {
2611 fin->fin_fr = fr->fr_grphead->fg_start;
2612 FR_VERBOSE(("group %s\n", FR_NAME(fr, fr_grhead)));
2614 if (FR_ISDECAPS(passt))
2615 passt = ipf_decaps(fin, pass, fr->fr_icode);
2617 passt = ipf_scanlist(fin, pass);
2619 if (fin->fin_fr == NULL) {
2620 fin->fin_rule = rulen;
2621 if (fr->fr_group != -1)
2622 (void) strncpy(fin->fin_group,
2625 strlen(fr->fr_names +
2633 if (pass & FR_QUICK) {
2635 * Finally, if we've asked to track state for this
2636 * packet, set it up. Add state for "quick" rules
2637 * here so that if the action fails we can consider
2638 * the rule to "not match" and keep on processing
2641 if ((pass & FR_KEEPSTATE) && !FR_ISAUTH(pass) &&
2642 !(fin->fin_flx & FI_STATE)) {
2643 int out = fin->fin_out;
2646 if (ipf_state_add(softc, fin, NULL, 0) == 0) {
2647 LBUMPD(ipf_stats[out], fr_ads);
2649 LBUMPD(ipf_stats[out], fr_bads);
2662 /* ------------------------------------------------------------------------ */
2663 /* Function: ipf_acctpkt */
2664 /* Returns: frentry_t* - always returns NULL */
2665 /* Parameters: fin(I) - pointer to packet information */
2666 /* passp(IO) - pointer to current/new filter decision (unused) */
2668 /* Checks a packet against accounting rules, if there are any for the given */
2669 /* IP protocol version. */
2671 /* N.B.: this function returns NULL to match the prototype used by other */
2672 /* functions called from the IPFilter "mainline" in ipf_check(). */
2673 /* ------------------------------------------------------------------------ */
2675 ipf_acctpkt(fin, passp)
2679 ipf_main_softc_t *softc = fin->fin_main_soft;
2680 char group[FR_GROUPLEN];
2681 frentry_t *fr, *frsave;
2685 fr = softc->ipf_acct[fin->fin_out][softc->ipf_active];
2688 frsave = fin->fin_fr;
2689 bcopy(fin->fin_group, group, FR_GROUPLEN);
2690 rulen = fin->fin_rule;
2692 pass = ipf_scanlist(fin, FR_NOMATCH);
2693 if (FR_ISACCOUNT(pass)) {
2694 LBUMPD(ipf_stats[0], fr_acct);
2696 fin->fin_fr = frsave;
2697 bcopy(group, fin->fin_group, FR_GROUPLEN);
2698 fin->fin_rule = rulen;
2704 /* ------------------------------------------------------------------------ */
2705 /* Function: ipf_firewall */
2706 /* Returns: frentry_t* - returns pointer to matched rule, if no matches */
2707 /* were found, returns NULL. */
2708 /* Parameters: fin(I) - pointer to packet information */
2709 /* passp(IO) - pointer to current/new filter decision (unused) */
2711 /* Applies an appropriate set of firewall rules to the packet, to see if */
2712 /* there are any matches. The first check is to see if a match can be seen */
2713 /* in the cache. If not, then search an appropriate list of rules. Once a */
2714 /* matching rule is found, take any appropriate actions as defined by the */
2715 /* rule - except logging. */
2716 /* ------------------------------------------------------------------------ */
2718 ipf_firewall(fin, passp)
2722 ipf_main_softc_t *softc = fin->fin_main_soft;
2731 * This rule cache will only affect packets that are not being
2732 * statefully filtered.
2734 fin->fin_fr = softc->ipf_rules[out][softc->ipf_active];
2735 if (fin->fin_fr != NULL)
2736 pass = ipf_scanlist(fin, softc->ipf_pass);
2738 if ((pass & FR_NOMATCH)) {
2739 LBUMPD(ipf_stats[out], fr_nom);
2744 * Apply packets per second rate-limiting to a rule as required.
2746 if ((fr != NULL) && (fr->fr_pps != 0) &&
2747 !ppsratecheck(&fr->fr_lastpkt, &fr->fr_curpps, fr->fr_pps)) {
2748 DT2(frb_ppsrate, fr_info_t *, fin, frentry_t *, fr);
2749 pass &= ~(FR_CMDMASK|FR_RETICMP|FR_RETRST);
2751 LBUMPD(ipf_stats[out], fr_ppshit);
2752 fin->fin_reason = FRB_PPSRATE;
2756 * If we fail to add a packet to the authorization queue, then we
2757 * drop the packet later. However, if it was added then pretend
2758 * we've dropped it already.
2760 if (FR_ISAUTH(pass)) {
2761 if (ipf_auth_new(fin->fin_m, fin) != 0) {
2762 DT1(frb_authnew, fr_info_t *, fin);
2763 fin->fin_m = *fin->fin_mp = NULL;
2764 fin->fin_reason = FRB_AUTHNEW;
2768 fin->fin_error = ENOSPC;
2772 if ((fr != NULL) && (fr->fr_func != NULL) &&
2773 (fr->fr_func != (ipfunc_t)-1) && !(pass & FR_CALLNOW))
2774 (void) (*fr->fr_func)(fin, &pass);
2777 * If a rule is a pre-auth rule, check again in the list of rules
2778 * loaded for authenticated use. It does not particulary matter
2779 * if this search fails because a "preauth" result, from a rule,
2780 * is treated as "not a pass", hence the packet is blocked.
2782 if (FR_ISPREAUTH(pass)) {
2783 pass = ipf_auth_pre_scanlist(softc, fin, pass);
2787 * If the rule has "keep frag" and the packet is actually a fragment,
2788 * then create a fragment state entry.
2790 if (pass & FR_KEEPFRAG) {
2791 if (fin->fin_flx & FI_FRAG) {
2792 if (ipf_frag_new(softc, fin, pass) == -1) {
2793 LBUMP(ipf_stats[out].fr_bnfr);
2795 LBUMP(ipf_stats[out].fr_nfr);
2798 LBUMP(ipf_stats[out].fr_cfr);
2809 /* ------------------------------------------------------------------------ */
2810 /* Function: ipf_check */
2811 /* Returns: int - 0 == packet allowed through, */
2813 /* -1 == packet blocked */
2814 /* 1 == packet not matched */
2815 /* -2 == requires authentication */
2817 /* > 0 == filter error # for packet */
2818 /* Parameters: ip(I) - pointer to start of IPv4/6 packet */
2819 /* hlen(I) - length of header */
2820 /* ifp(I) - pointer to interface this packet is on */
2821 /* out(I) - 0 == packet going in, 1 == packet going out */
2822 /* mp(IO) - pointer to caller's buffer pointer that holds this */
2824 /* Solaris & HP-UX ONLY : */
2825 /* qpi(I) - pointer to STREAMS queue information for this */
2826 /* interface & direction. */
2828 /* ipf_check() is the master function for all IPFilter packet processing. */
2829 /* It orchestrates: Network Address Translation (NAT), checking for packet */
2830 /* authorisation (or pre-authorisation), presence of related state info., */
2831 /* generating log entries, IP packet accounting, routing of packets as */
2832 /* directed by firewall rules and of course whether or not to allow the */
2833 /* packet to be further processed by the kernel. */
2835 /* For packets blocked, the contents of "mp" will be NULL'd and the buffer */
2836 /* freed. Packets passed may be returned with the pointer pointed to by */
2837 /* by "mp" changed to a new buffer. */
2838 /* ------------------------------------------------------------------------ */
2840 ipf_check(ctx, ip, hlen, ifp, out
2841 #if defined(_KERNEL) && defined(MENTAT)
2855 * The above really sucks, but short of writing a diff
2857 ipf_main_softc_t *softc = ctx;
2859 fr_info_t *fin = &frinfo;
2860 u_32_t pass = softc->ipf_pass;
2861 frentry_t *fr = NULL;
2866 * The first part of ipf_check() deals with making sure that what goes
2867 * into the filtering engine makes some sense. Information about the
2868 * the packet is distilled, collected into a fr_info_t structure and
2869 * the an attempt to ensure the buffer the packet is in is big enough
2870 * to hold all the required packet headers.
2874 qpktinfo_t *qpi = qif;
2877 if ((u_int)ip & 0x3)
2884 if (softc->ipf_running <= 0) {
2888 bzero((char *)fin, sizeof(*fin));
2891 if (qpi->qpi_flags & QF_BROADCAST)
2892 fin->fin_flx |= FI_MBCAST|FI_BROADCAST;
2893 if (qpi->qpi_flags & QF_MULTICAST)
2894 fin->fin_flx |= FI_MBCAST|FI_MULTICAST;
2902 # if defined(M_MCAST)
2903 if ((m->m_flags & M_MCAST) != 0)
2904 fin->fin_flx |= FI_MBCAST|FI_MULTICAST;
2906 # if defined(M_MLOOP)
2907 if ((m->m_flags & M_MLOOP) != 0)
2908 fin->fin_flx |= FI_MBCAST|FI_MULTICAST;
2910 # if defined(M_BCAST)
2911 if ((m->m_flags & M_BCAST) != 0)
2912 fin->fin_flx |= FI_MBCAST|FI_BROADCAST;
2914 # ifdef M_CANFASTFWD
2916 * XXX For now, IP Filter and fast-forwarding of cached flows
2917 * XXX are mutually exclusive. Eventually, IP Filter should
2918 * XXX get a "can-fast-forward" filter rule.
2920 m->m_flags &= ~M_CANFASTFWD;
2921 # endif /* M_CANFASTFWD */
2922 # if defined(CSUM_DELAY_DATA) && (!defined(__FreeBSD_version) || \
2923 (__FreeBSD_version < 501108))
2925 * disable delayed checksums.
2927 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
2928 in_delayed_cksum(m);
2929 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
2931 # endif /* CSUM_DELAY_DATA */
2932 # endif /* MENTAT */
2934 bzero((char *)fin, sizeof(*fin));
2936 # if defined(M_MCAST)
2937 if ((m->m_flags & M_MCAST) != 0)
2938 fin->fin_flx |= FI_MBCAST|FI_MULTICAST;
2940 # if defined(M_MLOOP)
2941 if ((m->m_flags & M_MLOOP) != 0)
2942 fin->fin_flx |= FI_MBCAST|FI_MULTICAST;
2944 # if defined(M_BCAST)
2945 if ((m->m_flags & M_BCAST) != 0)
2946 fin->fin_flx |= FI_MBCAST|FI_BROADCAST;
2948 #endif /* _KERNEL */
2956 fin->fin_error = ENETUNREACH;
2957 fin->fin_hlen = (u_short)hlen;
2958 fin->fin_dp = (char *)ip + hlen;
2959 fin->fin_main_soft = softc;
2961 fin->fin_ipoff = (char *)ip - MTOD(m, char *);
2967 LBUMP(ipf_stats[out].fr_ipv6);
2969 * Jumbo grams are quite likely too big for internal buffer
2970 * structures to handle comfortably, for now, so just drop
2973 if (((ip6_t *)ip)->ip6_plen == 0) {
2974 DT1(frb_jumbo, ip6_t *, (ip6_t *)ip);
2975 pass = FR_BLOCK|FR_NOMATCH;
2976 fin->fin_reason = FRB_JUMBO;
2979 fin->fin_family = AF_INET6;
2983 fin->fin_family = AF_INET;
2986 if (ipf_makefrip(hlen, ip, fin) == -1) {
2987 DT1(frb_makefrip, fr_info_t *, fin);
2988 pass = FR_BLOCK|FR_NOMATCH;
2989 fin->fin_reason = FRB_MAKEFRIP;
2994 * For at least IPv6 packets, if a m_pullup() fails then this pointer
2995 * becomes NULL and so we have no packet to free.
2997 if (*fin->fin_mp == NULL)
3002 if (softc->ipf_chksrc && !ipf_verifysrc(fin)) {
3003 LBUMPD(ipf_stats[0], fr_v4_badsrc);
3004 fin->fin_flx |= FI_BADSRC;
3006 if (fin->fin_ip->ip_ttl < softc->ipf_minttl) {
3007 LBUMPD(ipf_stats[0], fr_v4_badttl);
3008 fin->fin_flx |= FI_LOWTTL;
3013 if (((ip6_t *)ip)->ip6_hlim < softc->ipf_minttl) {
3014 LBUMPD(ipf_stats[0], fr_v6_badttl);
3015 fin->fin_flx |= FI_LOWTTL;
3021 if (fin->fin_flx & FI_SHORT) {
3022 LBUMPD(ipf_stats[out], fr_short);
3025 READ_ENTER(&softc->ipf_mutex);
3031 if (ipf_nat_checkin(fin, &pass) == -1) {
3037 if (ipf_nat6_checkin(fin, &pass) == -1) {
3048 * If a packet is found in the auth table, then skip checking
3049 * the access lists for permission but we do need to consider
3050 * the result as if it were from the ACL's. In addition, being
3051 * found in the auth table means it has been seen before, so do
3052 * not pass it through accounting (again), lest it be counted twice.
3054 fr = ipf_auth_check(fin, &pass);
3055 if (!out && (fr == NULL))
3056 (void) ipf_acctpkt(fin, NULL);
3059 if ((fin->fin_flx & FI_FRAG) != 0)
3060 fr = ipf_frag_known(fin, &pass);
3063 fr = ipf_state_check(fin, &pass);
3066 if ((pass & FR_NOMATCH) || (fr == NULL))
3067 fr = ipf_firewall(fin, &pass);
3070 * If we've asked to track state for this packet, set it up.
3071 * Here rather than ipf_firewall because ipf_checkauth may decide
3072 * to return a packet for "keep state"
3074 if ((pass & FR_KEEPSTATE) && (fin->fin_m != NULL) &&
3075 !(fin->fin_flx & FI_STATE)) {
3076 if (ipf_state_add(softc, fin, NULL, 0) == 0) {
3077 LBUMP(ipf_stats[out].fr_ads);
3079 LBUMP(ipf_stats[out].fr_bads);
3080 if (FR_ISPASS(pass)) {
3082 pass &= ~FR_CMDMASK;
3084 fin->fin_reason = FRB_STATEADD;
3090 if ((fr != NULL) && !(fin->fin_flx & FI_STATE)) {
3091 fin->fin_dif = &fr->fr_dif;
3092 fin->fin_tif = &fr->fr_tifs[fin->fin_rev];
3096 * Only count/translate packets which will be passed on, out the
3099 if (out && FR_ISPASS(pass)) {
3100 (void) ipf_acctpkt(fin, NULL);
3105 if (ipf_nat_checkout(fin, &pass) == -1) {
3107 } else if ((softc->ipf_update_ipid != 0) && (v == 4)) {
3108 if (ipf_updateipid(fin) == -1) {
3110 LBUMP(ipf_stats[1].fr_ipud);
3111 pass &= ~FR_CMDMASK;
3113 fin->fin_reason = FRB_UPDATEIPID;
3115 LBUMP(ipf_stats[0].fr_ipud);
3121 (void) ipf_nat6_checkout(fin, &pass);
3131 if ((softc->ipf_flags & FF_LOGGING) || (pass & FR_LOGMASK)) {
3132 (void) ipf_dolog(fin, &pass);
3137 * The FI_STATE flag is cleared here so that calling ipf_state_check
3138 * will work when called from inside of fr_fastroute. Although
3139 * there is a similar flag, FI_NATED, for NAT, it does have the same
3140 * impact on code execution.
3142 fin->fin_flx &= ~FI_STATE;
3144 #if defined(FASTROUTE_RECURSION)
3146 * Up the reference on fr_lock and exit ipf_mutex. The generation of
3147 * a packet below can sometimes cause a recursive call into IPFilter.
3148 * On those platforms where that does happen, we need to hang onto
3149 * the filter rule just in case someone decides to remove or flush it
3153 MUTEX_ENTER(&fr->fr_lock);
3155 MUTEX_EXIT(&fr->fr_lock);
3158 RWLOCK_EXIT(&softc->ipf_mutex);
3161 if ((pass & FR_RETMASK) != 0) {
3163 * Should we return an ICMP packet to indicate error
3164 * status passing through the packet filter ?
3165 * WARNING: ICMP error packets AND TCP RST packets should
3166 * ONLY be sent in repsonse to incoming packets. Sending
3167 * them in response to outbound packets can result in a
3168 * panic on some operating systems.
3171 if (pass & FR_RETICMP) {
3174 if ((pass & FR_RETMASK) == FR_FAKEICMP)
3178 (void) ipf_send_icmp_err(ICMP_UNREACH, fin,
3180 LBUMP(ipf_stats[0].fr_ret);
3181 } else if (((pass & FR_RETMASK) == FR_RETRST) &&
3182 !(fin->fin_flx & FI_SHORT)) {
3183 if (((fin->fin_flx & FI_OOW) != 0) ||
3184 (ipf_send_reset(fin) == 0)) {
3185 LBUMP(ipf_stats[1].fr_ret);
3190 * When using return-* with auth rules, the auth code
3191 * takes over disposing of this packet.
3193 if (FR_ISAUTH(pass) && (fin->fin_m != NULL)) {
3194 DT1(frb_authcapture, fr_info_t *, fin);
3195 fin->fin_m = *fin->fin_mp = NULL;
3196 fin->fin_reason = FRB_AUTHCAPTURE;
3200 if (pass & FR_RETRST) {
3201 fin->fin_error = ECONNRESET;
3207 * After the above so that ICMP unreachables and TCP RSTs get
3210 if (FR_ISBLOCK(pass) && (fin->fin_flx & FI_NEWNAT))
3211 ipf_nat_uncreate(fin);
3214 * If we didn't drop off the bottom of the list of rules (and thus
3215 * the 'current' rule fr is not NULL), then we may have some extra
3216 * instructions about what to do with a packet.
3217 * Once we're finished return to our caller, freeing the packet if
3218 * we are dropping it.
3224 * Generate a duplicated packet first because ipf_fastroute
3225 * can lead to fin_m being free'd... not good.
3228 if ((fdp != NULL) && (fdp->fd_ptr != NULL) &&
3229 (fdp->fd_ptr != (void *)-1)) {
3230 mc = M_COPY(fin->fin_m);
3232 ipf_fastroute(mc, &mc, fin, fdp);
3236 if (!out && (pass & FR_FASTROUTE)) {
3238 * For fastroute rule, no destination interface defined
3239 * so pass NULL as the frdest_t parameter
3241 (void) ipf_fastroute(fin->fin_m, mp, fin, NULL);
3243 } else if ((fdp != NULL) && (fdp->fd_ptr != NULL) &&
3244 (fdp->fd_ptr != (struct ifnet *)-1)) {
3245 /* this is for to rules: */
3246 ipf_fastroute(fin->fin_m, mp, fin, fdp);
3250 #if defined(FASTROUTE_RECURSION)
3251 (void) ipf_derefrule(softc, &fr);
3254 #if !defined(FASTROUTE_RECURSION)
3255 RWLOCK_EXIT(&softc->ipf_mutex);
3259 if (!FR_ISPASS(pass)) {
3260 LBUMP(ipf_stats[out].fr_block);
3268 LBUMP(ipf_stats[out].fr_pass);
3269 #if defined(_KERNEL) && defined(__sgi)
3270 if ((fin->fin_hbuf != NULL) &&
3271 (mtod(fin->fin_m, struct ip *) != fin->fin_ip)) {
3272 COPYBACK(fin->fin_m, 0, fin->fin_plen, fin->fin_hbuf);
3280 if (FR_ISPASS(pass))
3282 LBUMP(ipf_stats[out].fr_blocked[fin->fin_reason]);
3283 return fin->fin_error;
3286 (*mp)->mb_ifp = fin->fin_ifp;
3287 blockreason = fin->fin_reason;
3288 FR_VERBOSE(("fin_flx %#x pass %#x ", fin->fin_flx, pass));
3289 /*if ((pass & FR_CMDMASK) == (softc->ipf_pass & FR_CMDMASK))*/
3290 if ((pass & FR_NOMATCH) != 0)
3293 if ((pass & FR_RETMASK) != 0)
3294 switch (pass & FR_RETMASK)
3304 switch (pass & FR_CMDMASK)
3318 #endif /* _KERNEL */
3323 /* ------------------------------------------------------------------------ */
3324 /* Function: ipf_dolog */
3325 /* Returns: frentry_t* - returns contents of fin_fr (no change made) */
3326 /* Parameters: fin(I) - pointer to packet information */
3327 /* passp(IO) - pointer to current/new filter decision (unused) */
3329 /* Checks flags set to see how a packet should be logged, if it is to be */
3330 /* logged. Adjust statistics based on its success or not. */
3331 /* ------------------------------------------------------------------------ */
3333 ipf_dolog(fin, passp)
3337 ipf_main_softc_t *softc = fin->fin_main_soft;
3344 if ((softc->ipf_flags & FF_LOGNOMATCH) && (pass & FR_NOMATCH)) {
3345 pass |= FF_LOGNOMATCH;
3346 LBUMPD(ipf_stats[out], fr_npkl);
3349 } else if (((pass & FR_LOGMASK) == FR_LOGP) ||
3350 (FR_ISPASS(pass) && (softc->ipf_flags & FF_LOGPASS))) {
3351 if ((pass & FR_LOGMASK) != FR_LOGP)
3353 LBUMPD(ipf_stats[out], fr_ppkl);
3356 } else if (((pass & FR_LOGMASK) == FR_LOGB) ||
3357 (FR_ISBLOCK(pass) && (softc->ipf_flags & FF_LOGBLOCK))) {
3358 if ((pass & FR_LOGMASK) != FR_LOGB)
3359 pass |= FF_LOGBLOCK;
3360 LBUMPD(ipf_stats[out], fr_bpkl);
3363 if (ipf_log_pkt(fin, pass) == -1) {
3365 * If the "or-block" option has been used then
3366 * block the packet if we failed to log it.
3368 if ((pass & FR_LOGORBLOCK) && FR_ISPASS(pass)) {
3369 DT1(frb_logfail2, u_int, pass);
3370 pass &= ~FR_CMDMASK;
3372 fin->fin_reason = FRB_LOGFAIL2;
3380 #endif /* IPFILTER_LOG */
3383 /* ------------------------------------------------------------------------ */
3384 /* Function: ipf_cksum */
3385 /* Returns: u_short - IP header checksum */
3386 /* Parameters: addr(I) - pointer to start of buffer to checksum */
3387 /* len(I) - length of buffer in bytes */
3389 /* Calculate the two's complement 16 bit checksum of the buffer passed. */
3391 /* N.B.: addr should be 16bit aligned. */
3392 /* ------------------------------------------------------------------------ */
3394 ipf_cksum(addr, len)
3400 for (sum = 0; len > 1; len -= 2)
3403 /* mop up an odd byte, if necessary */
3405 sum += *(u_char *)addr;
3408 * add back carry outs from top 16 bits to low 16 bits
3410 sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */
3411 sum += (sum >> 16); /* add carry */
3412 return (u_short)(~sum);
3416 /* ------------------------------------------------------------------------ */
3417 /* Function: fr_cksum */
3418 /* Returns: u_short - layer 4 checksum */
3419 /* Parameters: fin(I) - pointer to packet information */
3420 /* ip(I) - pointer to IP header */
3421 /* l4proto(I) - protocol to caclulate checksum for */
3422 /* l4hdr(I) - pointer to layer 4 header */
3424 /* Calculates the TCP checksum for the packet held in "m", using the data */
3425 /* in the IP header "ip" to seed it. */
3427 /* NB: This function assumes we've pullup'd enough for all of the IP header */
3428 /* and the TCP header. We also assume that data blocks aren't allocated in */
3431 /* Expects ip_len and ip_off to be in network byte order when called. */
3432 /* ------------------------------------------------------------------------ */
3434 fr_cksum(fin, ip, l4proto, l4hdr)
3440 u_short *sp, slen, sumsave, *csump;
3455 sum = htons((u_short)l4proto);
3457 * Add up IP Header portion
3460 if (IP_V(ip) == 4) {
3462 hlen = IP_HL(ip) << 2;
3464 sp = (u_short *)&ip->ip_src;
3465 sum += *sp++; /* ip_src */
3467 sum += *sp++; /* ip_dst */
3470 } else if (IP_V(ip) == 6) {
3472 hlen = sizeof(*ip6);
3473 off = ((char *)fin->fin_dp - (char *)fin->fin_ip);
3474 sp = (u_short *)&ip6->ip6_src;
3475 sum += *sp++; /* ip6_src */
3483 /* This needs to be routing header aware. */
3484 sum += *sp++; /* ip6_dst */
3496 slen = fin->fin_plen - off;
3502 csump = &((udphdr_t *)l4hdr)->uh_sum;
3506 csump = &((tcphdr_t *)l4hdr)->th_sum;
3509 csump = &((icmphdr_t *)l4hdr)->icmp_cksum;
3510 sum = 0; /* Pseudo-checksum is not included */
3513 case IPPROTO_ICMPV6 :
3514 csump = &((struct icmp6_hdr *)l4hdr)->icmp6_cksum;
3521 if (csump != NULL) {
3526 sum2 = ipf_pcksum(fin, off, sum);
3533 /* ------------------------------------------------------------------------ */
3534 /* Function: ipf_findgroup */
3535 /* Returns: frgroup_t * - NULL = group not found, else pointer to group */
3536 /* Parameters: softc(I) - pointer to soft context main structure */
3537 /* group(I) - group name to search for */
3538 /* unit(I) - device to which this group belongs */
3539 /* set(I) - which set of rules (inactive/inactive) this is */
3540 /* fgpp(O) - pointer to place to store pointer to the pointer */
3541 /* to where to add the next (last) group or where */
3542 /* to delete group from. */
3544 /* Search amongst the defined groups for a particular group number. */
3545 /* ------------------------------------------------------------------------ */
3547 ipf_findgroup(softc, group, unit, set, fgpp)
3548 ipf_main_softc_t *softc;
3554 frgroup_t *fg, **fgp;
3557 * Which list of groups to search in is dependent on which list of
3558 * rules are being operated on.
3560 fgp = &softc->ipf_groups[unit][set];
3562 while ((fg = *fgp) != NULL) {
3563 if (strncmp(group, fg->fg_name, FR_GROUPLEN) == 0)
3574 /* ------------------------------------------------------------------------ */
3575 /* Function: ipf_group_add */
3576 /* Returns: frgroup_t * - NULL == did not create group, */
3577 /* != NULL == pointer to the group */
3578 /* Parameters: softc(I) - pointer to soft context main structure */
3579 /* num(I) - group number to add */
3580 /* head(I) - rule pointer that is using this as the head */
3581 /* flags(I) - rule flags which describe the type of rule it is */
3582 /* unit(I) - device to which this group will belong to */
3583 /* set(I) - which set of rules (inactive/inactive) this is */
3584 /* Write Locks: ipf_mutex */
3586 /* Add a new group head, or if it already exists, increase the reference */
3588 /* ------------------------------------------------------------------------ */
3590 ipf_group_add(softc, group, head, flags, unit, set)
3591 ipf_main_softc_t *softc;
3598 frgroup_t *fg, **fgp;
3604 if (unit == IPL_LOGIPF && *group == '\0')
3608 gflags = flags & FR_INOUT;
3610 fg = ipf_findgroup(softc, group, unit, set, &fgp);
3612 if (fg->fg_head == NULL && head != NULL)
3614 if (fg->fg_flags == 0)
3615 fg->fg_flags = gflags;
3616 else if (gflags != fg->fg_flags)
3622 KMALLOC(fg, frgroup_t *);
3625 fg->fg_start = NULL;
3627 bcopy(group, fg->fg_name, strlen(group) + 1);
3628 fg->fg_flags = gflags;
3630 fg->fg_set = &softc->ipf_groups[unit][set];
3637 /* ------------------------------------------------------------------------ */
3638 /* Function: ipf_group_del */
3639 /* Returns: int - number of rules deleted */
3640 /* Parameters: softc(I) - pointer to soft context main structure */
3641 /* group(I) - group name to delete */
3642 /* fr(I) - filter rule from which group is referenced */
3643 /* Write Locks: ipf_mutex */
3645 /* This function is called whenever a reference to a group is to be dropped */
3646 /* and thus its reference count needs to be lowered and the group free'd if */
3647 /* the reference count reaches zero. Passing in fr is really for the sole */
3648 /* purpose of knowing when the head rule is being deleted. */
3649 /* ------------------------------------------------------------------------ */
3651 ipf_group_del(softc, group, fr)
3652 ipf_main_softc_t *softc;
3657 if (group->fg_head == fr)
3658 group->fg_head = NULL;
3661 if ((group->fg_ref == 0) && (group->fg_start == NULL))
3662 ipf_group_free(group);
3666 /* ------------------------------------------------------------------------ */
3667 /* Function: ipf_group_free */
3669 /* Parameters: group(I) - pointer to filter rule group */
3671 /* Remove the group from the list of groups and free it. */
3672 /* ------------------------------------------------------------------------ */
3674 ipf_group_free(group)
3679 for (gp = group->fg_set; *gp != NULL; gp = &(*gp)->fg_next) {
3681 *gp = group->fg_next;
3689 /* ------------------------------------------------------------------------ */
3690 /* Function: ipf_group_flush */
3691 /* Returns: int - number of rules flush from group */
3692 /* Parameters: softc(I) - pointer to soft context main structure */
3693 /* Parameters: group(I) - pointer to filter rule group */
3695 /* Remove all of the rules that currently are listed under the given group. */
3696 /* ------------------------------------------------------------------------ */
3698 ipf_group_flush(softc, group)
3699 ipf_main_softc_t *softc;
3704 (void) ipf_flushlist(softc, &gone, &group->fg_start);
3710 /* ------------------------------------------------------------------------ */
3711 /* Function: ipf_getrulen */
3712 /* Returns: frentry_t * - NULL == not found, else pointer to rule n */
3713 /* Parameters: softc(I) - pointer to soft context main structure */
3714 /* Parameters: unit(I) - device for which to count the rule's number */
3715 /* flags(I) - which set of rules to find the rule in */
3716 /* group(I) - group name */
3717 /* n(I) - rule number to find */
3719 /* Find rule # n in group # g and return a pointer to it. Return NULl if */
3720 /* group # g doesn't exist or there are less than n rules in the group. */
3721 /* ------------------------------------------------------------------------ */
3723 ipf_getrulen(softc, unit, group, n)
3724 ipf_main_softc_t *softc;
3732 fg = ipf_findgroup(softc, group, unit, softc->ipf_active, NULL);
3735 for (fr = fg->fg_start; fr && n; fr = fr->fr_next, n--)
3743 /* ------------------------------------------------------------------------ */
3744 /* Function: ipf_flushlist */
3745 /* Returns: int - >= 0 - number of flushed rules */
3746 /* Parameters: softc(I) - pointer to soft context main structure */
3747 /* nfreedp(O) - pointer to int where flush count is stored */
3748 /* listp(I) - pointer to list to flush pointer */
3749 /* Write Locks: ipf_mutex */
3751 /* Recursively flush rules from the list, descending groups as they are */
3752 /* encountered. if a rule is the head of a group and it has lost all its */
3753 /* group members, then also delete the group reference. nfreedp is needed */
3754 /* to store the accumulating count of rules removed, whereas the returned */
3755 /* value is just the number removed from the current list. The latter is */
3756 /* needed to correctly adjust reference counts on rules that define groups. */
3758 /* NOTE: Rules not loaded from user space cannot be flushed. */
3759 /* ------------------------------------------------------------------------ */
3761 ipf_flushlist(softc, nfreedp, listp)
3762 ipf_main_softc_t *softc;
3769 while ((fp = *listp) != NULL) {
3770 if ((fp->fr_type & FR_T_BUILTIN) ||
3771 !(fp->fr_flags & FR_COPIED)) {
3772 listp = &fp->fr_next;
3775 *listp = fp->fr_next;
3776 if (fp->fr_next != NULL)
3777 fp->fr_next->fr_pnext = fp->fr_pnext;
3778 fp->fr_pnext = NULL;
3780 if (fp->fr_grphead != NULL) {
3781 freed += ipf_group_flush(softc, fp->fr_grphead);
3782 fp->fr_names[fp->fr_grhead] = '\0';
3785 if (fp->fr_icmpgrp != NULL) {
3786 freed += ipf_group_flush(softc, fp->fr_icmpgrp);
3787 fp->fr_names[fp->fr_icmphead] = '\0';
3790 if (fp->fr_srctrack.ht_max_nodes)
3791 ipf_rb_ht_flush(&fp->fr_srctrack);
3795 ASSERT(fp->fr_ref > 0);
3796 if (ipf_derefrule(softc, &fp) == 0)
3804 /* ------------------------------------------------------------------------ */
3805 /* Function: ipf_flush */
3806 /* Returns: int - >= 0 - number of flushed rules */
3807 /* Parameters: softc(I) - pointer to soft context main structure */
3808 /* unit(I) - device for which to flush rules */
3809 /* flags(I) - which set of rules to flush */
3811 /* Calls flushlist() for all filter rules (accounting, firewall - both IPv4 */
3812 /* and IPv6) as defined by the value of flags. */
3813 /* ------------------------------------------------------------------------ */
3815 ipf_flush(softc, unit, flags)
3816 ipf_main_softc_t *softc;
3820 int flushed = 0, set;
3822 WRITE_ENTER(&softc->ipf_mutex);
3824 set = softc->ipf_active;
3825 if ((flags & FR_INACTIVE) == FR_INACTIVE)
3828 if (flags & FR_OUTQUE) {
3829 ipf_flushlist(softc, &flushed, &softc->ipf_rules[1][set]);
3830 ipf_flushlist(softc, &flushed, &softc->ipf_acct[1][set]);
3832 if (flags & FR_INQUE) {
3833 ipf_flushlist(softc, &flushed, &softc->ipf_rules[0][set]);
3834 ipf_flushlist(softc, &flushed, &softc->ipf_acct[0][set]);
3837 flushed += ipf_flush_groups(softc, &softc->ipf_groups[unit][set],
3838 flags & (FR_INQUE|FR_OUTQUE));
3840 RWLOCK_EXIT(&softc->ipf_mutex);
3842 if (unit == IPL_LOGIPF) {
3845 tmp = ipf_flush(softc, IPL_LOGCOUNT, flags);
3853 /* ------------------------------------------------------------------------ */
3854 /* Function: ipf_flush_groups */
3855 /* Returns: int - >= 0 - number of flushed rules */
3856 /* Parameters: softc(I) - soft context pointerto work with */
3857 /* grhead(I) - pointer to the start of the group list to flush */
3858 /* flags(I) - which set of rules to flush */
3860 /* Walk through all of the groups under the given group head and remove all */
3861 /* of those that match the flags passed in. The for loop here is bit more */
3862 /* complicated than usual because the removal of a rule with ipf_derefrule */
3863 /* may end up removing not only the structure pointed to by "fg" but also */
3864 /* what is fg_next and fg_next after that. So if a filter rule is actually */
3865 /* removed from the group then it is necessary to start again. */
3866 /* ------------------------------------------------------------------------ */
3868 ipf_flush_groups(softc, grhead, flags)
3869 ipf_main_softc_t *softc;
3873 frentry_t *fr, **frp;
3874 frgroup_t *fg, **fgp;
3878 for (fgp = grhead; (fg = *fgp) != NULL; ) {
3879 while ((fg != NULL) && ((fg->fg_flags & flags) == 0))
3884 frp = &fg->fg_start;
3885 while ((removed == 0) && ((fr = *frp) != NULL)) {
3886 if ((fr->fr_flags & flags) == 0) {
3889 if (fr->fr_next != NULL)
3890 fr->fr_next->fr_pnext = fr->fr_pnext;
3892 fr->fr_pnext = NULL;
3894 (void) ipf_derefrule(softc, &fr);
3906 /* ------------------------------------------------------------------------ */
3907 /* Function: memstr */
3908 /* Returns: char * - NULL if failed, != NULL pointer to matching bytes */
3909 /* Parameters: src(I) - pointer to byte sequence to match */
3910 /* dst(I) - pointer to byte sequence to search */
3911 /* slen(I) - match length */
3912 /* dlen(I) - length available to search in */
3914 /* Search dst for a sequence of bytes matching those at src and extend for */
3916 /* ------------------------------------------------------------------------ */
3918 memstr(src, dst, slen, dlen)
3925 while (dlen >= slen) {
3926 if (bcmp(src, dst, slen) == 0) {
3935 /* ------------------------------------------------------------------------ */
3936 /* Function: ipf_fixskip */
3938 /* Parameters: listp(IO) - pointer to start of list with skip rule */
3939 /* rp(I) - rule added/removed with skip in it. */
3940 /* addremove(I) - adjustment (-1/+1) to make to skip count, */
3941 /* depending on whether a rule was just added */
3944 /* Adjust all the rules in a list which would have skip'd past the position */
3945 /* where we are inserting to skip to the right place given the change. */
3946 /* ------------------------------------------------------------------------ */
3948 ipf_fixskip(listp, rp, addremove)
3949 frentry_t **listp, *rp;
3956 for (fp = *listp; (fp != NULL) && (fp != rp); fp = fp->fr_next)
3962 for (rn = 0, fp = *listp; fp && (fp != rp); fp = fp->fr_next, rn++)
3963 if (FR_ISSKIP(fp->fr_flags) && (rn + fp->fr_arg >= rules))
3964 fp->fr_arg += addremove;
3969 /* ------------------------------------------------------------------------ */
3970 /* Function: count4bits */
3971 /* Returns: int - >= 0 - number of consecutive bits in input */
3972 /* Parameters: ip(I) - 32bit IP address */
3975 /* count consecutive 1's in bit mask. If the mask generated by counting */
3976 /* consecutive 1's is different to that passed, return -1, else return # */
3978 /* ------------------------------------------------------------------------ */
3986 ip = ipn = ntohl(ip);
3987 for (i = 32; i; i--, ipn *= 2)
3988 if (ipn & 0x80000000)
3993 for (i = 32, j = cnt; i; i--, j--) {
4004 /* ------------------------------------------------------------------------ */
4005 /* Function: count6bits */
4006 /* Returns: int - >= 0 - number of consecutive bits in input */
4007 /* Parameters: msk(I) - pointer to start of IPv6 bitmask */
4010 /* count consecutive 1's in bit mask. */
4011 /* ------------------------------------------------------------------------ */
4020 for (k = 3; k >= 0; k--)
4021 if (msk[k] == 0xffffffff)
4024 for (j = msk[k]; j; j <<= 1)
4031 #endif /* _KERNEL */
4034 /* ------------------------------------------------------------------------ */
4035 /* Function: ipf_synclist */
4036 /* Returns: int - 0 = no failures, else indication of first failure */
4037 /* Parameters: fr(I) - start of filter list to sync interface names for */
4038 /* ifp(I) - interface pointer for limiting sync lookups */
4039 /* Write Locks: ipf_mutex */
4041 /* Walk through a list of filter rules and resolve any interface names into */
4042 /* pointers. Where dynamic addresses are used, also update the IP address */
4043 /* used in the rule. The interface pointer is used to limit the lookups to */
4044 /* a specific set of matching names if it is non-NULL. */
4045 /* Errors can occur when resolving the destination name of to/dup-to fields */
4046 /* when the name points to a pool and that pool doest not exist. If this */
4047 /* does happen then it is necessary to check if there are any lookup refs */
4048 /* that need to be dropped before returning with an error. */
4049 /* ------------------------------------------------------------------------ */
4051 ipf_synclist(softc, fr, ifp)
4052 ipf_main_softc_t *softc;
4056 frentry_t *frt, *start = fr;
4065 for (; fr; fr = fr->fr_next) {
4066 if (fr->fr_family == AF_INET)
4068 else if (fr->fr_family == AF_INET6)
4074 * Lookup all the interface names that are part of the rule.
4076 for (i = 0; i < 4; i++) {
4077 if ((ifp != NULL) && (fr->fr_ifas[i] != ifp))
4079 if (fr->fr_ifnames[i] == -1)
4081 name = FR_NAME(fr, fr_ifnames[i]);
4082 fr->fr_ifas[i] = ipf_resolvenic(softc, name, v);
4085 if ((fr->fr_type & ~FR_T_BUILTIN) == FR_T_IPF) {
4086 if (fr->fr_satype != FRI_NORMAL &&
4087 fr->fr_satype != FRI_LOOKUP) {
4088 ifa = ipf_resolvenic(softc, fr->fr_names +
4090 ipf_ifpaddr(softc, v, fr->fr_satype, ifa,
4091 &fr->fr_src6, &fr->fr_smsk6);
4093 if (fr->fr_datype != FRI_NORMAL &&
4094 fr->fr_datype != FRI_LOOKUP) {
4095 ifa = ipf_resolvenic(softc, fr->fr_names +
4097 ipf_ifpaddr(softc, v, fr->fr_datype, ifa,
4098 &fr->fr_dst6, &fr->fr_dmsk6);
4102 fdp = &fr->fr_tifs[0];
4103 if ((ifp == NULL) || (fdp->fd_ptr == ifp)) {
4104 error = ipf_resolvedest(softc, fr->fr_names, fdp, v);
4109 fdp = &fr->fr_tifs[1];
4110 if ((ifp == NULL) || (fdp->fd_ptr == ifp)) {
4111 error = ipf_resolvedest(softc, fr->fr_names, fdp, v);
4117 if ((ifp == NULL) || (fdp->fd_ptr == ifp)) {
4118 error = ipf_resolvedest(softc, fr->fr_names, fdp, v);
4123 if (((fr->fr_type & ~FR_T_BUILTIN) == FR_T_IPF) &&
4124 (fr->fr_satype == FRI_LOOKUP) && (fr->fr_srcptr == NULL)) {
4125 fr->fr_srcptr = ipf_lookup_res_num(softc,
4131 if (((fr->fr_type & ~FR_T_BUILTIN) == FR_T_IPF) &&
4132 (fr->fr_datype == FRI_LOOKUP) && (fr->fr_dstptr == NULL)) {
4133 fr->fr_dstptr = ipf_lookup_res_num(softc,
4143 for (frt = start; frt != fr; fr = fr->fr_next) {
4144 if (((frt->fr_type & ~FR_T_BUILTIN) == FR_T_IPF) &&
4145 (frt->fr_satype == FRI_LOOKUP) && (frt->fr_srcptr != NULL))
4146 ipf_lookup_deref(softc, frt->fr_srctype,
4148 if (((frt->fr_type & ~FR_T_BUILTIN) == FR_T_IPF) &&
4149 (frt->fr_datype == FRI_LOOKUP) && (frt->fr_dstptr != NULL))
4150 ipf_lookup_deref(softc, frt->fr_dsttype,
4157 /* ------------------------------------------------------------------------ */
4158 /* Function: ipf_sync */
4160 /* Parameters: Nil */
4162 /* ipf_sync() is called when we suspect that the interface list or */
4163 /* information about interfaces (like IP#) has changed. Go through all */
4164 /* filter rules, NAT entries and the state table and check if anything */
4165 /* needs to be changed/updated. */
4166 /* ------------------------------------------------------------------------ */
4168 ipf_sync(softc, ifp)
4169 ipf_main_softc_t *softc;
4175 ipf_nat_sync(softc, ifp);
4176 ipf_state_sync(softc, ifp);
4177 ipf_lookup_sync(softc, ifp);
4180 WRITE_ENTER(&softc->ipf_mutex);
4181 (void) ipf_synclist(softc, softc->ipf_acct[0][softc->ipf_active], ifp);
4182 (void) ipf_synclist(softc, softc->ipf_acct[1][softc->ipf_active], ifp);
4183 (void) ipf_synclist(softc, softc->ipf_rules[0][softc->ipf_active], ifp);
4184 (void) ipf_synclist(softc, softc->ipf_rules[1][softc->ipf_active], ifp);
4186 for (i = 0; i < IPL_LOGSIZE; i++) {
4189 for (g = softc->ipf_groups[i][0]; g != NULL; g = g->fg_next)
4190 (void) ipf_synclist(softc, g->fg_start, ifp);
4191 for (g = softc->ipf_groups[i][1]; g != NULL; g = g->fg_next)
4192 (void) ipf_synclist(softc, g->fg_start, ifp);
4194 RWLOCK_EXIT(&softc->ipf_mutex);
4201 * In the functions below, bcopy() is called because the pointer being
4202 * copied _from_ in this instance is a pointer to a char buf (which could
4203 * end up being unaligned) and on the kernel's local stack.
4205 /* ------------------------------------------------------------------------ */
4206 /* Function: copyinptr */
4207 /* Returns: int - 0 = success, else failure */
4208 /* Parameters: src(I) - pointer to the source address */
4209 /* dst(I) - destination address */
4210 /* size(I) - number of bytes to copy */
4212 /* Copy a block of data in from user space, given a pointer to the pointer */
4213 /* to start copying from (src) and a pointer to where to store it (dst). */
4214 /* NB: src - pointer to user space pointer, dst - kernel space pointer */
4215 /* ------------------------------------------------------------------------ */
4217 copyinptr(softc, src, dst, size)
4218 ipf_main_softc_t *softc;
4226 error = COPYIN(src, &ca, sizeof(ca));
4230 bcopy(src, (caddr_t)&ca, sizeof(ca));
4232 error = COPYIN(ca, dst, size);
4241 /* ------------------------------------------------------------------------ */
4242 /* Function: copyoutptr */
4243 /* Returns: int - 0 = success, else failure */
4244 /* Parameters: src(I) - pointer to the source address */
4245 /* dst(I) - destination address */
4246 /* size(I) - number of bytes to copy */
4248 /* Copy a block of data out to user space, given a pointer to the pointer */
4249 /* to start copying from (src) and a pointer to where to store it (dst). */
4250 /* NB: src - kernel space pointer, dst - pointer to user space pointer. */
4251 /* ------------------------------------------------------------------------ */
4253 copyoutptr(softc, src, dst, size)
4254 ipf_main_softc_t *softc;
4261 bcopy(dst, (caddr_t)&ca, sizeof(ca));
4262 error = COPYOUT(src, ca, size);
4271 /* ------------------------------------------------------------------------ */
4272 /* Function: ipf_lock */
4273 /* Returns: int - 0 = success, else error */
4274 /* Parameters: data(I) - pointer to lock value to set */
4275 /* lockp(O) - pointer to location to store old lock value */
4277 /* Get the new value for the lock integer, set it and return the old value */
4279 /* ------------------------------------------------------------------------ */
4281 ipf_lock(data, lockp)
4287 err = BCOPYIN(data, &arg, sizeof(arg));
4290 err = BCOPYOUT(lockp, data, sizeof(*lockp));
4298 /* ------------------------------------------------------------------------ */
4299 /* Function: ipf_getstat */
4301 /* Parameters: softc(I) - pointer to soft context main structure */
4302 /* fiop(I) - pointer to ipfilter stats structure */
4303 /* rev(I) - version claim by program doing ioctl */
4305 /* Stores a copy of current pointers, counters, etc, in the friostat */
4307 /* If IPFILTER_COMPAT is compiled, we pretend to be whatever version the */
4308 /* program is looking for. This ensure that validation of the version it */
4309 /* expects will always succeed. Thus kernels with IPFILTER_COMPAT will */
4310 /* allow older binaries to work but kernels without it will not. */
4311 /* ------------------------------------------------------------------------ */
4314 ipf_getstat(softc, fiop, rev)
4315 ipf_main_softc_t *softc;
4321 bcopy((char *)softc->ipf_stats, (char *)fiop->f_st,
4322 sizeof(ipf_statistics_t) * 2);
4323 fiop->f_locks[IPL_LOGSTATE] = -1;
4324 fiop->f_locks[IPL_LOGNAT] = -1;
4325 fiop->f_locks[IPL_LOGIPF] = -1;
4326 fiop->f_locks[IPL_LOGAUTH] = -1;
4328 fiop->f_ipf[0][0] = softc->ipf_rules[0][0];
4329 fiop->f_acct[0][0] = softc->ipf_acct[0][0];
4330 fiop->f_ipf[0][1] = softc->ipf_rules[0][1];
4331 fiop->f_acct[0][1] = softc->ipf_acct[0][1];
4332 fiop->f_ipf[1][0] = softc->ipf_rules[1][0];
4333 fiop->f_acct[1][0] = softc->ipf_acct[1][0];
4334 fiop->f_ipf[1][1] = softc->ipf_rules[1][1];
4335 fiop->f_acct[1][1] = softc->ipf_acct[1][1];
4337 fiop->f_ticks = softc->ipf_ticks;
4338 fiop->f_active = softc->ipf_active;
4339 fiop->f_froute[0] = softc->ipf_frouteok[0];
4340 fiop->f_froute[1] = softc->ipf_frouteok[1];
4341 fiop->f_rb_no_mem = softc->ipf_rb_no_mem;
4342 fiop->f_rb_node_max = softc->ipf_rb_node_max;
4344 fiop->f_running = softc->ipf_running;
4345 for (i = 0; i < IPL_LOGSIZE; i++) {
4346 fiop->f_groups[i][0] = softc->ipf_groups[i][0];
4347 fiop->f_groups[i][1] = softc->ipf_groups[i][1];
4350 fiop->f_log_ok = ipf_log_logok(softc, IPL_LOGIPF);
4351 fiop->f_log_fail = ipf_log_failures(softc, IPL_LOGIPF);
4352 fiop->f_logging = 1;
4355 fiop->f_log_fail = 0;
4356 fiop->f_logging = 0;
4358 fiop->f_defpass = softc->ipf_pass;
4359 fiop->f_features = ipf_features;
4361 #ifdef IPFILTER_COMPAT
4362 sprintf(fiop->f_version, "IP Filter: v%d.%d.%d",
4363 (rev / 1000000) % 100,
4364 (rev / 10000) % 100,
4368 (void) strncpy(fiop->f_version, ipfilter_version,
4369 sizeof(fiop->f_version));
4375 int icmptoicmp6types[ICMP_MAXTYPE+1] = {
4376 ICMP6_ECHO_REPLY, /* 0: ICMP_ECHOREPLY */
4379 ICMP6_DST_UNREACH, /* 3: ICMP_UNREACH */
4380 -1, /* 4: ICMP_SOURCEQUENCH */
4381 ND_REDIRECT, /* 5: ICMP_REDIRECT */
4384 ICMP6_ECHO_REQUEST, /* 8: ICMP_ECHO */
4386 -1, /* 10: UNUSED */
4387 ICMP6_TIME_EXCEEDED, /* 11: ICMP_TIMXCEED */
4388 ICMP6_PARAM_PROB, /* 12: ICMP_PARAMPROB */
4389 -1, /* 13: ICMP_TSTAMP */
4390 -1, /* 14: ICMP_TSTAMPREPLY */
4391 -1, /* 15: ICMP_IREQ */
4392 -1, /* 16: ICMP_IREQREPLY */
4393 -1, /* 17: ICMP_MASKREQ */
4394 -1, /* 18: ICMP_MASKREPLY */
4398 int icmptoicmp6unreach[ICMP_MAX_UNREACH] = {
4399 ICMP6_DST_UNREACH_ADDR, /* 0: ICMP_UNREACH_NET */
4400 ICMP6_DST_UNREACH_ADDR, /* 1: ICMP_UNREACH_HOST */
4401 -1, /* 2: ICMP_UNREACH_PROTOCOL */
4402 ICMP6_DST_UNREACH_NOPORT, /* 3: ICMP_UNREACH_PORT */
4403 -1, /* 4: ICMP_UNREACH_NEEDFRAG */
4404 ICMP6_DST_UNREACH_NOTNEIGHBOR, /* 5: ICMP_UNREACH_SRCFAIL */
4405 ICMP6_DST_UNREACH_ADDR, /* 6: ICMP_UNREACH_NET_UNKNOWN */
4406 ICMP6_DST_UNREACH_ADDR, /* 7: ICMP_UNREACH_HOST_UNKNOWN */
4407 -1, /* 8: ICMP_UNREACH_ISOLATED */
4408 ICMP6_DST_UNREACH_ADMIN, /* 9: ICMP_UNREACH_NET_PROHIB */
4409 ICMP6_DST_UNREACH_ADMIN, /* 10: ICMP_UNREACH_HOST_PROHIB */
4410 -1, /* 11: ICMP_UNREACH_TOSNET */
4411 -1, /* 12: ICMP_UNREACH_TOSHOST */
4412 ICMP6_DST_UNREACH_ADMIN, /* 13: ICMP_UNREACH_ADMIN_PROHIBIT */
4414 int icmpreplytype6[ICMP6_MAXTYPE + 1];
4417 int icmpreplytype4[ICMP_MAXTYPE + 1];
4420 /* ------------------------------------------------------------------------ */
4421 /* Function: ipf_matchicmpqueryreply */
4422 /* Returns: int - 1 if "icmp" is a valid reply to "ic" else 0. */
4423 /* Parameters: v(I) - IP protocol version (4 or 6) */
4424 /* ic(I) - ICMP information */
4425 /* icmp(I) - ICMP packet header */
4426 /* rev(I) - direction (0 = forward/1 = reverse) of packet */
4428 /* Check if the ICMP packet defined by the header pointed to by icmp is a */
4429 /* reply to one as described by what's in ic. If it is a match, return 1, */
4430 /* else return 0 for no match. */
4431 /* ------------------------------------------------------------------------ */
4433 ipf_matchicmpqueryreply(v, ic, icmp, rev)
4441 ictype = ic->ici_type;
4445 * If we matched its type on the way in, then when going out
4446 * it will still be the same type.
4448 if ((!rev && (icmp->icmp_type == ictype)) ||
4449 (rev && (icmpreplytype4[ictype] == icmp->icmp_type))) {
4450 if (icmp->icmp_type != ICMP_ECHOREPLY)
4452 if (icmp->icmp_id == ic->ici_id)
4458 if ((!rev && (icmp->icmp_type == ictype)) ||
4459 (rev && (icmpreplytype6[ictype] == icmp->icmp_type))) {
4460 if (icmp->icmp_type != ICMP6_ECHO_REPLY)
4462 if (icmp->icmp_id == ic->ici_id)
4471 /* ------------------------------------------------------------------------ */
4472 /* Function: ipf_rule_compare */
4473 /* Parameters: fr1(I) - first rule structure to compare */
4474 /* fr2(I) - second rule structure to compare */
4475 /* Returns: int - 0 == rules are the same, else mismatch */
4477 /* Compare two rules and return 0 if they match or a number indicating */
4478 /* which of the individual checks failed. */
4479 /* ------------------------------------------------------------------------ */
4481 ipf_rule_compare(frentry_t *fr1, frentry_t *fr2)
4483 if (fr1->fr_cksum != fr2->fr_cksum)
4485 if (fr1->fr_size != fr2->fr_size)
4487 if (fr1->fr_dsize != fr2->fr_dsize)
4489 if (bcmp((char *)&fr1->fr_func, (char *)&fr2->fr_func,
4490 fr1->fr_size - offsetof(struct frentry, fr_func)) != 0)
4492 if (fr1->fr_data && !fr2->fr_data)
4494 if (!fr1->fr_data && fr2->fr_data)
4497 if (bcmp(fr1->fr_caddr, fr2->fr_caddr, fr1->fr_dsize))
4504 /* ------------------------------------------------------------------------ */
4505 /* Function: frrequest */
4506 /* Returns: int - 0 == success, > 0 == errno value */
4507 /* Parameters: unit(I) - device for which this is for */
4508 /* req(I) - ioctl command (SIOC*) */
4509 /* data(I) - pointr to ioctl data */
4510 /* set(I) - 1 or 0 (filter set) */
4511 /* makecopy(I) - flag indicating whether data points to a rule */
4512 /* in kernel space & hence doesn't need copying. */
4514 /* This function handles all the requests which operate on the list of */
4515 /* filter rules. This includes adding, deleting, insertion. It is also */
4516 /* responsible for creating groups when a "head" rule is loaded. Interface */
4517 /* names are resolved here and other sanity checks are made on the content */
4518 /* of the rule structure being loaded. If a rule has user defined timeouts */
4519 /* then make sure they are created and initialised before exiting. */
4520 /* ------------------------------------------------------------------------ */
4522 frrequest(softc, unit, req, data, set, makecopy)
4523 ipf_main_softc_t *softc;
4529 int error = 0, in, family, addrem, need_free = 0;
4530 frentry_t frd, *fp, *f, **fprev, **ftail;
4531 void *ptr, *uptr, *cptr;
4540 if (makecopy != 0) {
4541 bzero(fp, sizeof(frd));
4542 error = ipf_inobj(softc, data, NULL, fp, IPFOBJ_FRENTRY);
4546 if ((fp->fr_type & FR_T_BUILTIN) != 0) {
4550 KMALLOCS(f, frentry_t *, fp->fr_size);
4555 bzero(f, fp->fr_size);
4556 error = ipf_inobjsz(softc, data, f, IPFOBJ_FRENTRY,
4559 KFREES(f, fp->fr_size);
4566 fp->fr_dnext = NULL;
4567 fp->fr_pnext = NULL;
4568 fp->fr_pdnext = NULL;
4570 fp->fr_grphead = NULL;
4571 fp->fr_icmpgrp = NULL;
4572 fp->fr_isc = (void *)-1;
4575 fp->fr_flags |= FR_COPIED;
4577 fp = (frentry_t *)data;
4578 if ((fp->fr_type & FR_T_BUILTIN) == 0) {
4582 fp->fr_flags &= ~FR_COPIED;
4585 if (((fp->fr_dsize == 0) && (fp->fr_data != NULL)) ||
4586 ((fp->fr_dsize != 0) && (fp->fr_data == NULL))) {
4592 family = fp->fr_family;
4595 if (req == (ioctlcmd_t)SIOCINAFR || req == (ioctlcmd_t)SIOCINIFR ||
4596 req == (ioctlcmd_t)SIOCADAFR || req == (ioctlcmd_t)SIOCADIFR)
4598 else if (req == (ioctlcmd_t)SIOCRMAFR || req == (ioctlcmd_t)SIOCRMIFR)
4600 else if (req == (ioctlcmd_t)SIOCZRLST)
4609 * Only filter rules for IPv4 or IPv6 are accepted.
4611 if (family == AF_INET) {
4614 } else if (family == AF_INET6) {
4617 } else if (family != 0) {
4624 * If the rule is being loaded from user space, i.e. we had to copy it
4625 * into kernel space, then do not trust the function pointer in the
4628 if ((makecopy == 1) && (fp->fr_func != NULL)) {
4629 if (ipf_findfunc(fp->fr_func) == NULL) {
4636 error = ipf_funcinit(softc, fp);
4641 if ((fp->fr_flags & FR_CALLNOW) &&
4642 ((fp->fr_func == NULL) || (fp->fr_func == (ipfunc_t)-1))) {
4647 if (((fp->fr_flags & FR_CMDMASK) == FR_CALL) &&
4648 ((fp->fr_func == NULL) || (fp->fr_func == (ipfunc_t)-1))) {
4657 if (FR_ISACCOUNT(fp->fr_flags))
4658 unit = IPL_LOGCOUNT;
4661 * Check that each group name in the rule has a start index that
4664 if (fp->fr_icmphead != -1) {
4665 if ((fp->fr_icmphead < 0) ||
4666 (fp->fr_icmphead >= fp->fr_namelen)) {
4671 if (!strcmp(FR_NAME(fp, fr_icmphead), "0"))
4672 fp->fr_names[fp->fr_icmphead] = '\0';
4675 if (fp->fr_grhead != -1) {
4676 if ((fp->fr_grhead < 0) ||
4677 (fp->fr_grhead >= fp->fr_namelen)) {
4682 if (!strcmp(FR_NAME(fp, fr_grhead), "0"))
4683 fp->fr_names[fp->fr_grhead] = '\0';
4686 if (fp->fr_group != -1) {
4687 if ((fp->fr_group < 0) ||
4688 (fp->fr_group >= fp->fr_namelen)) {
4693 if ((req != (int)SIOCZRLST) && (fp->fr_group != -1)) {
4695 * Allow loading rules that are in groups to cause
4696 * them to be created if they don't already exit.
4698 group = FR_NAME(fp, fr_group);
4700 fg = ipf_group_add(softc, group, NULL,
4701 fp->fr_flags, unit, set);
4704 fg = ipf_findgroup(softc, group, unit,
4713 if (fg->fg_flags == 0) {
4714 fg->fg_flags = fp->fr_flags & FR_INOUT;
4715 } else if (fg->fg_flags != (fp->fr_flags & FR_INOUT)) {
4723 * If a rule is going to be part of a group then it does
4724 * not matter whether it is an in or out rule, but if it
4725 * isn't in a group, then it does...
4727 if ((fp->fr_flags & (FR_INQUE|FR_OUTQUE)) == 0) {
4733 in = (fp->fr_flags & FR_INQUE) ? 0 : 1;
4736 * Work out which rule list this change is being applied to.
4740 if (unit == IPL_LOGAUTH) {
4741 if ((fp->fr_tifs[0].fd_ptr != NULL) ||
4742 (fp->fr_tifs[1].fd_ptr != NULL) ||
4743 (fp->fr_dif.fd_ptr != NULL) ||
4744 (fp->fr_flags & FR_FASTROUTE)) {
4745 softc->ipf_interror = 145;
4749 fprev = ipf_auth_rulehead(softc);
4751 if (FR_ISACCOUNT(fp->fr_flags))
4752 fprev = &softc->ipf_acct[in][set];
4753 else if ((fp->fr_flags & (FR_OUTQUE|FR_INQUE)) != 0)
4754 fprev = &softc->ipf_rules[in][set];
4756 if (fprev == NULL) {
4763 fprev = &fg->fg_start;
4766 * Copy in extra data for the rule.
4768 if (fp->fr_dsize != 0) {
4769 if (makecopy != 0) {
4770 KMALLOCS(ptr, void *, fp->fr_dsize);
4778 * The bcopy case is for when the data is appended
4779 * to the rule by ipf_in_compat().
4781 if (uptr >= (void *)fp &&
4782 uptr < (void *)((char *)fp + fp->fr_size)) {
4783 bcopy(uptr, ptr, fp->fr_dsize);
4786 error = COPYIN(uptr, ptr, fp->fr_dsize);
4802 * Perform per-rule type sanity checks of their members.
4803 * All code after this needs to be aware that allocated memory
4804 * may need to be free'd before exiting.
4806 switch (fp->fr_type & ~FR_T_BUILTIN)
4808 #if defined(IPFILTER_BPF)
4810 if (fp->fr_dsize == 0) {
4815 if (!bpf_validate(ptr, fp->fr_dsize/sizeof(struct bpf_insn))) {
4824 * Preparation for error case at the bottom of this function.
4826 if (fp->fr_datype == FRI_LOOKUP)
4827 fp->fr_dstptr = NULL;
4828 if (fp->fr_satype == FRI_LOOKUP)
4829 fp->fr_srcptr = NULL;
4831 if (fp->fr_dsize != sizeof(fripf_t)) {
4838 * Allowing a rule with both "keep state" and "with oow" is
4839 * pointless because adding a state entry to the table will
4840 * fail with the out of window (oow) flag set.
4842 if ((fp->fr_flags & FR_KEEPSTATE) && (fp->fr_flx & FI_OOW)) {
4848 switch (fp->fr_satype)
4850 case FRI_BROADCAST :
4853 case FRI_NETMASKED :
4855 if (fp->fr_sifpidx < 0) {
4861 fp->fr_srcptr = ipf_findlookup(softc, unit, fp,
4864 if (fp->fr_srcfunc == NULL) {
4880 switch (fp->fr_datype)
4882 case FRI_BROADCAST :
4885 case FRI_NETMASKED :
4887 if (fp->fr_difpidx < 0) {
4893 fp->fr_dstptr = ipf_findlookup(softc, unit, fp,
4896 if (fp->fr_dstfunc == NULL) {
4910 case FR_T_CALLFUNC :
4915 if (ipf_matcharray_verify(fp->fr_data, fp->fr_dsize) == -1) {
4929 if (fp->fr_tif.fd_name != -1) {
4930 if ((fp->fr_tif.fd_name < 0) ||
4931 (fp->fr_tif.fd_name >= fp->fr_namelen)) {
4938 if (fp->fr_dif.fd_name != -1) {
4939 if ((fp->fr_dif.fd_name < 0) ||
4940 (fp->fr_dif.fd_name >= fp->fr_namelen)) {
4947 if (fp->fr_rif.fd_name != -1) {
4948 if ((fp->fr_rif.fd_name < 0) ||
4949 (fp->fr_rif.fd_name >= fp->fr_namelen)) {
4957 * Lookup all the interface names that are part of the rule.
4959 error = ipf_synclist(softc, fp, NULL);
4962 fp->fr_statecnt = 0;
4963 if (fp->fr_srctrack.ht_max_nodes != 0)
4964 ipf_rb_ht_init(&fp->fr_srctrack);
4967 * Look for an existing matching filter rule, but don't include the
4968 * next or interface pointer in the comparison (fr_next, fr_ifa).
4969 * This elminates rules which are indentical being loaded. Checksum
4970 * the constant part of the filter rule to make comparisons quicker
4971 * (this meaning no pointers are included).
4973 for (fp->fr_cksum = 0, p = (u_int *)&fp->fr_func, pp = &fp->fr_cksum;
4976 pp = (u_int *)(fp->fr_caddr + fp->fr_dsize);
4977 for (p = (u_int *)fp->fr_data; p < pp; p++)
4980 WRITE_ENTER(&softc->ipf_mutex);
4983 * Now that the filter rule lists are locked, we can walk the
4984 * chain of them without fear.
4987 for (f = *ftail; (f = *ftail) != NULL; ftail = &f->fr_next) {
4988 if (fp->fr_collect <= f->fr_collect) {
4996 for (; (f = *ftail) != NULL; ftail = &f->fr_next) {
4997 if (ipf_rule_compare(fp, f) == 0)
5002 * If zero'ing statistics, copy current to caller and zero.
5010 * Copy and reduce lock because of impending copyout.
5011 * Well we should, but if we do then the atomicity of
5012 * this call and the correctness of fr_hits and
5013 * fr_bytes cannot be guaranteed. As it is, this code
5014 * only resets them to 0 if they are successfully
5015 * copied out into user space.
5017 bcopy((char *)f, (char *)fp, f->fr_size);
5018 /* MUTEX_DOWNGRADE(&softc->ipf_mutex); */
5021 * When we copy this rule back out, set the data
5022 * pointer to be what it was in user space.
5025 error = ipf_outobj(softc, data, fp, IPFOBJ_FRENTRY);
5028 if ((f->fr_dsize != 0) && (uptr != NULL))
5029 error = COPYOUT(f->fr_data, uptr,
5042 if (makecopy != 0) {
5044 KFREES(ptr, fp->fr_dsize);
5046 KFREES(fp, fp->fr_size);
5048 RWLOCK_EXIT(&softc->ipf_mutex);
5054 * At the end of this, ftail must point to the place where the
5055 * new rule is to be saved/inserted/added.
5056 * For SIOCAD*FR, this should be the last rule in the group of
5057 * rules that have equal fr_collect fields.
5058 * For SIOCIN*FR, ...
5060 if (req == (ioctlcmd_t)SIOCADAFR ||
5061 req == (ioctlcmd_t)SIOCADIFR) {
5063 for (ftail = fprev; (f = *ftail) != NULL; ) {
5064 if (f->fr_collect > fp->fr_collect)
5066 ftail = &f->fr_next;
5072 } else if (req == (ioctlcmd_t)SIOCINAFR ||
5073 req == (ioctlcmd_t)SIOCINIFR) {
5074 while ((f = *fprev) != NULL) {
5075 if (f->fr_collect >= fp->fr_collect)
5077 fprev = &f->fr_next;
5080 if (fp->fr_hits != 0) {
5081 while (fp->fr_hits && (f = *ftail)) {
5082 if (f->fr_collect != fp->fr_collect)
5085 ftail = &f->fr_next;
5095 * Request to remove a rule.
5103 * Do not allow activity from user space to interfere
5104 * with rules not loaded that way.
5106 if ((makecopy == 1) && !(f->fr_flags & FR_COPIED)) {
5113 * Return EBUSY if the rule is being reference by
5114 * something else (eg state information.)
5116 if (f->fr_ref > 1) {
5121 #ifdef IPFILTER_SCAN
5122 if (f->fr_isctag != -1 &&
5123 (f->fr_isc != (struct ipscan *)-1))
5124 ipf_scan_detachfr(f);
5127 if (unit == IPL_LOGAUTH) {
5128 error = ipf_auth_precmd(softc, req, f, ftail);
5132 ipf_rule_delete(softc, f, unit, set);
5134 need_free = makecopy;
5138 * Not removing, so we must be adding/inserting a rule.
5145 if (unit == IPL_LOGAUTH) {
5146 error = ipf_auth_precmd(softc, req, fp, ftail);
5150 MUTEX_NUKE(&fp->fr_lock);
5151 MUTEX_INIT(&fp->fr_lock, "filter rule lock");
5152 if (fp->fr_die != 0)
5153 ipf_rule_expire_insert(softc, fp, set);
5158 fp->fr_pnext = ftail;
5159 fp->fr_next = *ftail;
5160 if (fp->fr_next != NULL)
5161 fp->fr_next->fr_pnext = &fp->fr_next;
5164 ipf_fixskip(ftail, fp, 1);
5166 fp->fr_icmpgrp = NULL;
5167 if (fp->fr_icmphead != -1) {
5168 group = FR_NAME(fp, fr_icmphead);
5169 fg = ipf_group_add(softc, group, fp, 0, unit, set);
5170 fp->fr_icmpgrp = fg;
5173 fp->fr_grphead = NULL;
5174 if (fp->fr_grhead != -1) {
5175 group = FR_NAME(fp, fr_grhead);
5176 fg = ipf_group_add(softc, group, fp, fp->fr_flags,
5178 fp->fr_grphead = fg;
5182 RWLOCK_EXIT(&softc->ipf_mutex);
5184 if (need_free || (error != 0)) {
5185 if ((fp->fr_type & ~FR_T_BUILTIN) == FR_T_IPF) {
5186 if ((fp->fr_satype == FRI_LOOKUP) &&
5187 (fp->fr_srcptr != NULL))
5188 ipf_lookup_deref(softc, fp->fr_srctype,
5190 if ((fp->fr_datype == FRI_LOOKUP) &&
5191 (fp->fr_dstptr != NULL))
5192 ipf_lookup_deref(softc, fp->fr_dsttype,
5195 if (fp->fr_grp != NULL) {
5196 WRITE_ENTER(&softc->ipf_mutex);
5197 ipf_group_del(softc, fp->fr_grp, fp);
5198 RWLOCK_EXIT(&softc->ipf_mutex);
5200 if ((ptr != NULL) && (makecopy != 0)) {
5201 KFREES(ptr, fp->fr_dsize);
5203 KFREES(fp, fp->fr_size);
5209 /* ------------------------------------------------------------------------ */
5210 /* Function: ipf_rule_delete */
5212 /* Parameters: softc(I) - pointer to soft context main structure */
5213 /* f(I) - pointer to the rule being deleted */
5214 /* ftail(I) - pointer to the pointer to f */
5215 /* unit(I) - device for which this is for */
5216 /* set(I) - 1 or 0 (filter set) */
5218 /* This function attempts to do what it can to delete a filter rule: remove */
5219 /* it from any linked lists and remove any groups it is responsible for. */
5220 /* But in the end, removing a rule can only drop the reference count - we */
5221 /* must use that as the guide for whether or not it can be freed. */
5222 /* ------------------------------------------------------------------------ */
5224 ipf_rule_delete(softc, f, unit, set)
5225 ipf_main_softc_t *softc;
5231 * If fr_pdnext is set, then the rule is on the expire list, so
5232 * remove it from there.
5234 if (f->fr_pdnext != NULL) {
5235 *f->fr_pdnext = f->fr_dnext;
5236 if (f->fr_dnext != NULL)
5237 f->fr_dnext->fr_pdnext = f->fr_pdnext;
5238 f->fr_pdnext = NULL;
5242 ipf_fixskip(f->fr_pnext, f, -1);
5243 if (f->fr_pnext != NULL)
5244 *f->fr_pnext = f->fr_next;
5245 if (f->fr_next != NULL)
5246 f->fr_next->fr_pnext = f->fr_pnext;
5250 (void) ipf_derefrule(softc, &f);
5253 /* ------------------------------------------------------------------------ */
5254 /* Function: ipf_rule_expire_insert */
5256 /* Parameters: softc(I) - pointer to soft context main structure */
5257 /* f(I) - pointer to rule to be added to expire list */
5258 /* set(I) - 1 or 0 (filter set) */
5260 /* If the new rule has a given expiration time, insert it into the list of */
5261 /* expiring rules with the ones to be removed first added to the front of */
5262 /* the list. The insertion is O(n) but it is kept sorted for quick scans at */
5263 /* expiration interval checks. */
5264 /* ------------------------------------------------------------------------ */
5266 ipf_rule_expire_insert(softc, f, set)
5267 ipf_main_softc_t *softc;
5276 f->fr_die = softc->ipf_ticks + IPF_TTLVAL(f->fr_die);
5277 for (fr = softc->ipf_rule_explist[set]; fr != NULL;
5278 fr = fr->fr_dnext) {
5279 if (f->fr_die < fr->fr_die)
5281 if (fr->fr_dnext == NULL) {
5283 * We've got to the last rule and everything
5284 * wanted to be expired before this new node,
5285 * so we have to tack it on the end...
5288 f->fr_pdnext = &fr->fr_dnext;
5294 if (softc->ipf_rule_explist[set] == NULL) {
5295 softc->ipf_rule_explist[set] = f;
5296 f->fr_pdnext = &softc->ipf_rule_explist[set];
5297 } else if (fr != NULL) {
5299 f->fr_pdnext = fr->fr_pdnext;
5300 fr->fr_pdnext = &f->fr_dnext;
5305 /* ------------------------------------------------------------------------ */
5306 /* Function: ipf_findlookup */
5307 /* Returns: NULL = failure, else success */
5308 /* Parameters: softc(I) - pointer to soft context main structure */
5309 /* unit(I) - ipf device we want to find match for */
5310 /* fp(I) - rule for which lookup is for */
5311 /* addrp(I) - pointer to lookup information in address struct */
5312 /* maskp(O) - pointer to lookup information for storage */
5314 /* When using pools and hash tables to store addresses for matching in */
5315 /* rules, it is necessary to resolve both the object referred to by the */
5316 /* name or address (and return that pointer) and also provide the means by */
5317 /* which to determine if an address belongs to that object to make the */
5318 /* packet matching quicker. */
5319 /* ------------------------------------------------------------------------ */
5321 ipf_findlookup(softc, unit, fr, addrp, maskp)
5322 ipf_main_softc_t *softc;
5325 i6addr_t *addrp, *maskp;
5329 switch (addrp->iplookupsubtype)
5332 ptr = ipf_lookup_res_num(softc, unit, addrp->iplookuptype,
5334 &maskp->iplookupfunc);
5337 if (addrp->iplookupname < 0)
5339 if (addrp->iplookupname >= fr->fr_namelen)
5341 ptr = ipf_lookup_res_name(softc, unit, addrp->iplookuptype,
5342 fr->fr_names + addrp->iplookupname,
5343 &maskp->iplookupfunc);
5353 /* ------------------------------------------------------------------------ */
5354 /* Function: ipf_funcinit */
5355 /* Returns: int - 0 == success, else ESRCH: cannot resolve rule details */
5356 /* Parameters: softc(I) - pointer to soft context main structure */
5357 /* fr(I) - pointer to filter rule */
5359 /* If a rule is a call rule, then check if the function it points to needs */
5360 /* an init function to be called now the rule has been loaded. */
5361 /* ------------------------------------------------------------------------ */
5363 ipf_funcinit(softc, fr)
5364 ipf_main_softc_t *softc;
5367 ipfunc_resolve_t *ft;
5373 for (ft = ipf_availfuncs; ft->ipfu_addr != NULL; ft++)
5374 if (ft->ipfu_addr == fr->fr_func) {
5376 if (ft->ipfu_init != NULL)
5377 err = (*ft->ipfu_init)(softc, fr);
5384 /* ------------------------------------------------------------------------ */
5385 /* Function: ipf_funcfini */
5387 /* Parameters: softc(I) - pointer to soft context main structure */
5388 /* fr(I) - pointer to filter rule */
5390 /* For a given filter rule, call the matching "fini" function if the rule */
5391 /* is using a known function that would have resulted in the "init" being */
5392 /* called for ealier. */
5393 /* ------------------------------------------------------------------------ */
5395 ipf_funcfini(softc, fr)
5396 ipf_main_softc_t *softc;
5399 ipfunc_resolve_t *ft;
5401 for (ft = ipf_availfuncs; ft->ipfu_addr != NULL; ft++)
5402 if (ft->ipfu_addr == fr->fr_func) {
5403 if (ft->ipfu_fini != NULL)
5404 (void) (*ft->ipfu_fini)(softc, fr);
5410 /* ------------------------------------------------------------------------ */
5411 /* Function: ipf_findfunc */
5412 /* Returns: ipfunc_t - pointer to function if found, else NULL */
5413 /* Parameters: funcptr(I) - function pointer to lookup */
5415 /* Look for a function in the table of known functions. */
5416 /* ------------------------------------------------------------------------ */
5418 ipf_findfunc(funcptr)
5421 ipfunc_resolve_t *ft;
5423 for (ft = ipf_availfuncs; ft->ipfu_addr != NULL; ft++)
5424 if (ft->ipfu_addr == funcptr)
5430 /* ------------------------------------------------------------------------ */
5431 /* Function: ipf_resolvefunc */
5432 /* Returns: int - 0 == success, else error */
5433 /* Parameters: data(IO) - ioctl data pointer to ipfunc_resolve_t struct */
5435 /* Copy in a ipfunc_resolve_t structure and then fill in the missing field. */
5436 /* This will either be the function name (if the pointer is set) or the */
5437 /* function pointer if the name is set. When found, fill in the other one */
5438 /* so that the entire, complete, structure can be copied back to user space.*/
5439 /* ------------------------------------------------------------------------ */
5441 ipf_resolvefunc(softc, data)
5442 ipf_main_softc_t *softc;
5445 ipfunc_resolve_t res, *ft;
5448 error = BCOPYIN(data, &res, sizeof(res));
5454 if (res.ipfu_addr == NULL && res.ipfu_name[0] != '\0') {
5455 for (ft = ipf_availfuncs; ft->ipfu_addr != NULL; ft++)
5456 if (strncmp(res.ipfu_name, ft->ipfu_name,
5457 sizeof(res.ipfu_name)) == 0) {
5458 res.ipfu_addr = ft->ipfu_addr;
5459 res.ipfu_init = ft->ipfu_init;
5460 if (COPYOUT(&res, data, sizeof(res)) != 0) {
5467 if (res.ipfu_addr != NULL && res.ipfu_name[0] == '\0') {
5468 for (ft = ipf_availfuncs; ft->ipfu_addr != NULL; ft++)
5469 if (ft->ipfu_addr == res.ipfu_addr) {
5470 (void) strncpy(res.ipfu_name, ft->ipfu_name,
5471 sizeof(res.ipfu_name));
5472 res.ipfu_init = ft->ipfu_init;
5473 if (COPYOUT(&res, data, sizeof(res)) != 0) {
5485 #if !defined(_KERNEL) || (!defined(__NetBSD__) && !defined(__OpenBSD__) && \
5486 !defined(__FreeBSD__)) || \
5487 FREEBSD_LT_REV(501000) || NETBSD_LT_REV(105000000) || \
5488 OPENBSD_LT_REV(200006)
5491 * ppsratecheck(): packets (or events) per second limitation.
5494 ppsratecheck(lasttime, curpps, maxpps)
5495 struct timeval *lasttime;
5497 int maxpps; /* maximum pps allowed */
5499 struct timeval tv, delta;
5504 delta.tv_sec = tv.tv_sec - lasttime->tv_sec;
5505 delta.tv_usec = tv.tv_usec - lasttime->tv_usec;
5506 if (delta.tv_usec < 0) {
5508 delta.tv_usec += 1000000;
5512 * check for 0,0 is so that the message will be seen at least once.
5513 * if more than one second have passed since the last update of
5514 * lasttime, reset the counter.
5516 * we do increment *curpps even in *curpps < maxpps case, as some may
5517 * try to use *curpps for stat purposes as well.
5519 if ((lasttime->tv_sec == 0 && lasttime->tv_usec == 0) ||
5520 delta.tv_sec >= 1) {
5524 } else if (maxpps < 0)
5526 else if (*curpps < maxpps)
5530 *curpps = *curpps + 1;
5537 /* ------------------------------------------------------------------------ */
5538 /* Function: ipf_derefrule */
5539 /* Returns: int - 0 == rule freed up, else rule not freed */
5540 /* Parameters: fr(I) - pointer to filter rule */
5542 /* Decrement the reference counter to a rule by one. If it reaches zero, */
5543 /* free it and any associated storage space being used by it. */
5544 /* ------------------------------------------------------------------------ */
5546 ipf_derefrule(softc, frp)
5547 ipf_main_softc_t *softc;
5556 MUTEX_ENTER(&fr->fr_lock);
5558 if (fr->fr_ref == 0) {
5559 MUTEX_EXIT(&fr->fr_lock);
5560 MUTEX_DESTROY(&fr->fr_lock);
5562 ipf_funcfini(softc, fr);
5565 if (fdp->fd_type == FRD_DSTLIST)
5566 ipf_lookup_deref(softc, IPLT_DSTLIST, fdp->fd_ptr);
5569 if (fdp->fd_type == FRD_DSTLIST)
5570 ipf_lookup_deref(softc, IPLT_DSTLIST, fdp->fd_ptr);
5573 if (fdp->fd_type == FRD_DSTLIST)
5574 ipf_lookup_deref(softc, IPLT_DSTLIST, fdp->fd_ptr);
5576 if ((fr->fr_type & ~FR_T_BUILTIN) == FR_T_IPF &&
5577 fr->fr_satype == FRI_LOOKUP)
5578 ipf_lookup_deref(softc, fr->fr_srctype, fr->fr_srcptr);
5579 if ((fr->fr_type & ~FR_T_BUILTIN) == FR_T_IPF &&
5580 fr->fr_datype == FRI_LOOKUP)
5581 ipf_lookup_deref(softc, fr->fr_dsttype, fr->fr_dstptr);
5583 if (fr->fr_grp != NULL)
5584 ipf_group_del(softc, fr->fr_grp, fr);
5586 if (fr->fr_grphead != NULL)
5587 ipf_group_del(softc, fr->fr_grphead, fr);
5589 if (fr->fr_icmpgrp != NULL)
5590 ipf_group_del(softc, fr->fr_icmpgrp, fr);
5592 if ((fr->fr_flags & FR_COPIED) != 0) {
5594 KFREES(fr->fr_data, fr->fr_dsize);
5596 KFREES(fr, fr->fr_size);
5601 MUTEX_EXIT(&fr->fr_lock);
5607 /* ------------------------------------------------------------------------ */
5608 /* Function: ipf_grpmapinit */
5609 /* Returns: int - 0 == success, else ESRCH because table entry not found*/
5610 /* Parameters: fr(I) - pointer to rule to find hash table for */
5612 /* Looks for group hash table fr_arg and stores a pointer to it in fr_ptr. */
5613 /* fr_ptr is later used by ipf_srcgrpmap and ipf_dstgrpmap. */
5614 /* ------------------------------------------------------------------------ */
5616 ipf_grpmapinit(softc, fr)
5617 ipf_main_softc_t *softc;
5620 char name[FR_GROUPLEN];
5623 #if defined(SNPRINTF) && defined(_KERNEL)
5624 SNPRINTF(name, sizeof(name), "%d", fr->fr_arg);
5626 (void) sprintf(name, "%d", fr->fr_arg);
5628 iph = ipf_lookup_find_htable(softc, IPL_LOGIPF, name);
5633 if ((iph->iph_flags & FR_INOUT) != (fr->fr_flags & FR_INOUT)) {
5643 /* ------------------------------------------------------------------------ */
5644 /* Function: ipf_grpmapfini */
5645 /* Returns: int - 0 == success, else ESRCH because table entry not found*/
5646 /* Parameters: softc(I) - pointer to soft context main structure */
5647 /* fr(I) - pointer to rule to release hash table for */
5649 /* For rules that have had ipf_grpmapinit called, ipf_lookup_deref needs to */
5650 /* be called to undo what ipf_grpmapinit caused to be done. */
5651 /* ------------------------------------------------------------------------ */
5653 ipf_grpmapfini(softc, fr)
5654 ipf_main_softc_t *softc;
5660 ipf_lookup_deref(softc, IPLT_HASH, iph);
5665 /* ------------------------------------------------------------------------ */
5666 /* Function: ipf_srcgrpmap */
5667 /* Returns: frentry_t * - pointer to "new last matching" rule or NULL */
5668 /* Parameters: fin(I) - pointer to packet information */
5669 /* passp(IO) - pointer to current/new filter decision (unused) */
5671 /* Look for a rule group head in a hash table, using the source address as */
5672 /* the key, and descend into that group and continue matching rules against */
5674 /* ------------------------------------------------------------------------ */
5676 ipf_srcgrpmap(fin, passp)
5683 rval = ipf_iphmfindgroup(fin->fin_main_soft, fin->fin_fr->fr_ptr,
5689 fin->fin_fr = fg->fg_start;
5690 (void) ipf_scanlist(fin, *passp);
5695 /* ------------------------------------------------------------------------ */
5696 /* Function: ipf_dstgrpmap */
5697 /* Returns: frentry_t * - pointer to "new last matching" rule or NULL */
5698 /* Parameters: fin(I) - pointer to packet information */
5699 /* passp(IO) - pointer to current/new filter decision (unused) */
5701 /* Look for a rule group head in a hash table, using the destination */
5702 /* address as the key, and descend into that group and continue matching */
5703 /* rules against the packet. */
5704 /* ------------------------------------------------------------------------ */
5706 ipf_dstgrpmap(fin, passp)
5713 rval = ipf_iphmfindgroup(fin->fin_main_soft, fin->fin_fr->fr_ptr,
5719 fin->fin_fr = fg->fg_start;
5720 (void) ipf_scanlist(fin, *passp);
5727 * These functions manage objects on queues for efficient timeouts. There
5728 * are a number of system defined queues as well as user defined timeouts.
5729 * It is expected that a lock is held in the domain in which the queue
5730 * belongs (i.e. either state or NAT) when calling any of these functions
5731 * that prevents ipf_freetimeoutqueue() from being called at the same time
5736 /* ------------------------------------------------------------------------ */
5737 /* Function: ipf_addtimeoutqueue */
5738 /* Returns: struct ifqtq * - NULL if malloc fails, else pointer to */
5739 /* timeout queue with given interval. */
5740 /* Parameters: parent(I) - pointer to pointer to parent node of this list */
5741 /* of interface queues. */
5742 /* seconds(I) - timeout value in seconds for this queue. */
5744 /* This routine first looks for a timeout queue that matches the interval */
5745 /* being requested. If it finds one, increments the reference counter and */
5746 /* returns a pointer to it. If none are found, it allocates a new one and */
5747 /* inserts it at the top of the list. */
5750 /* It is assumed that the caller of this function has an appropriate lock */
5751 /* held (exclusively) in the domain that encompases 'parent'. */
5752 /* ------------------------------------------------------------------------ */
5754 ipf_addtimeoutqueue(softc, parent, seconds)
5755 ipf_main_softc_t *softc;
5762 period = seconds * IPF_HZ_DIVIDE;
5764 MUTEX_ENTER(&softc->ipf_timeoutlock);
5765 for (ifq = *parent; ifq != NULL; ifq = ifq->ifq_next) {
5766 if (ifq->ifq_ttl == period) {
5768 * Reset the delete flag, if set, so the structure
5769 * gets reused rather than freed and reallocated.
5771 MUTEX_ENTER(&ifq->ifq_lock);
5772 ifq->ifq_flags &= ~IFQF_DELETE;
5774 MUTEX_EXIT(&ifq->ifq_lock);
5775 MUTEX_EXIT(&softc->ipf_timeoutlock);
5781 KMALLOC(ifq, ipftq_t *);
5783 MUTEX_NUKE(&ifq->ifq_lock);
5784 IPFTQ_INIT(ifq, period, "ipftq mutex");
5785 ifq->ifq_next = *parent;
5786 ifq->ifq_pnext = parent;
5787 ifq->ifq_flags = IFQF_USER;
5790 softc->ipf_userifqs++;
5792 MUTEX_EXIT(&softc->ipf_timeoutlock);
5797 /* ------------------------------------------------------------------------ */
5798 /* Function: ipf_deletetimeoutqueue */
5799 /* Returns: int - new reference count value of the timeout queue */
5800 /* Parameters: ifq(I) - timeout queue which is losing a reference. */
5801 /* Locks: ifq->ifq_lock */
5803 /* This routine must be called when we're discarding a pointer to a timeout */
5804 /* queue object, taking care of the reference counter. */
5806 /* Now that this just sets a DELETE flag, it requires the expire code to */
5807 /* check the list of user defined timeout queues and call the free function */
5808 /* below (currently commented out) to stop memory leaking. It is done this */
5809 /* way because the locking may not be sufficient to safely do a free when */
5810 /* this function is called. */
5811 /* ------------------------------------------------------------------------ */
5813 ipf_deletetimeoutqueue(ifq)
5818 if ((ifq->ifq_ref == 0) && ((ifq->ifq_flags & IFQF_USER) != 0)) {
5819 ifq->ifq_flags |= IFQF_DELETE;
5822 return ifq->ifq_ref;
5826 /* ------------------------------------------------------------------------ */
5827 /* Function: ipf_freetimeoutqueue */
5828 /* Parameters: ifq(I) - timeout queue which is losing a reference. */
5832 /* It is assumed that the caller of this function has an appropriate lock */
5833 /* held (exclusively) in the domain that encompases the callers "domain". */
5834 /* The ifq_lock for this structure should not be held. */
5836 /* Remove a user defined timeout queue from the list of queues it is in and */
5837 /* tidy up after this is done. */
5838 /* ------------------------------------------------------------------------ */
5840 ipf_freetimeoutqueue(softc, ifq)
5841 ipf_main_softc_t *softc;
5845 if (((ifq->ifq_flags & IFQF_DELETE) == 0) || (ifq->ifq_ref != 0) ||
5846 ((ifq->ifq_flags & IFQF_USER) == 0)) {
5847 printf("ipf_freetimeoutqueue(%lx) flags 0x%x ttl %d ref %d\n",
5848 (u_long)ifq, ifq->ifq_flags, ifq->ifq_ttl,
5854 * Remove from its position in the list.
5856 *ifq->ifq_pnext = ifq->ifq_next;
5857 if (ifq->ifq_next != NULL)
5858 ifq->ifq_next->ifq_pnext = ifq->ifq_pnext;
5859 ifq->ifq_next = NULL;
5860 ifq->ifq_pnext = NULL;
5862 MUTEX_DESTROY(&ifq->ifq_lock);
5863 ATOMIC_DEC(softc->ipf_userifqs);
5868 /* ------------------------------------------------------------------------ */
5869 /* Function: ipf_deletequeueentry */
5871 /* Parameters: tqe(I) - timeout queue entry to delete */
5873 /* Remove a tail queue entry from its queue and make it an orphan. */
5874 /* ipf_deletetimeoutqueue is called to make sure the reference count on the */
5875 /* queue is correct. We can't, however, call ipf_freetimeoutqueue because */
5876 /* the correct lock(s) may not be held that would make it safe to do so. */
5877 /* ------------------------------------------------------------------------ */
5879 ipf_deletequeueentry(tqe)
5886 MUTEX_ENTER(&ifq->ifq_lock);
5888 if (tqe->tqe_pnext != NULL) {
5889 *tqe->tqe_pnext = tqe->tqe_next;
5890 if (tqe->tqe_next != NULL)
5891 tqe->tqe_next->tqe_pnext = tqe->tqe_pnext;
5892 else /* we must be the tail anyway */
5893 ifq->ifq_tail = tqe->tqe_pnext;
5895 tqe->tqe_pnext = NULL;
5896 tqe->tqe_ifq = NULL;
5899 (void) ipf_deletetimeoutqueue(ifq);
5900 ASSERT(ifq->ifq_ref > 0);
5902 MUTEX_EXIT(&ifq->ifq_lock);
5906 /* ------------------------------------------------------------------------ */
5907 /* Function: ipf_queuefront */
5909 /* Parameters: tqe(I) - pointer to timeout queue entry */
5911 /* Move a queue entry to the front of the queue, if it isn't already there. */
5912 /* ------------------------------------------------------------------------ */
5923 MUTEX_ENTER(&ifq->ifq_lock);
5924 if (ifq->ifq_head != tqe) {
5925 *tqe->tqe_pnext = tqe->tqe_next;
5927 tqe->tqe_next->tqe_pnext = tqe->tqe_pnext;
5929 ifq->ifq_tail = tqe->tqe_pnext;
5931 tqe->tqe_next = ifq->ifq_head;
5932 ifq->ifq_head->tqe_pnext = &tqe->tqe_next;
5933 ifq->ifq_head = tqe;
5934 tqe->tqe_pnext = &ifq->ifq_head;
5936 MUTEX_EXIT(&ifq->ifq_lock);
5940 /* ------------------------------------------------------------------------ */
5941 /* Function: ipf_queueback */
5943 /* Parameters: ticks(I) - ipf tick time to use with this call */
5944 /* tqe(I) - pointer to timeout queue entry */
5946 /* Move a queue entry to the back of the queue, if it isn't already there. */
5947 /* We use use ticks to calculate the expiration and mark for when we last */
5948 /* touched the structure. */
5949 /* ------------------------------------------------------------------------ */
5951 ipf_queueback(ticks, tqe)
5960 tqe->tqe_die = ticks + ifq->ifq_ttl;
5961 tqe->tqe_touched = ticks;
5963 MUTEX_ENTER(&ifq->ifq_lock);
5964 if (tqe->tqe_next != NULL) { /* at the end already ? */
5968 *tqe->tqe_pnext = tqe->tqe_next;
5969 tqe->tqe_next->tqe_pnext = tqe->tqe_pnext;
5972 * Make it the last entry.
5974 tqe->tqe_next = NULL;
5975 tqe->tqe_pnext = ifq->ifq_tail;
5976 *ifq->ifq_tail = tqe;
5977 ifq->ifq_tail = &tqe->tqe_next;
5979 MUTEX_EXIT(&ifq->ifq_lock);
5983 /* ------------------------------------------------------------------------ */
5984 /* Function: ipf_queueappend */
5986 /* Parameters: ticks(I) - ipf tick time to use with this call */
5987 /* tqe(I) - pointer to timeout queue entry */
5988 /* ifq(I) - pointer to timeout queue */
5989 /* parent(I) - owing object pointer */
5991 /* Add a new item to this queue and put it on the very end. */
5992 /* We use use ticks to calculate the expiration and mark for when we last */
5993 /* touched the structure. */
5994 /* ------------------------------------------------------------------------ */
5996 ipf_queueappend(ticks, tqe, ifq, parent)
6003 MUTEX_ENTER(&ifq->ifq_lock);
6004 tqe->tqe_parent = parent;
6005 tqe->tqe_pnext = ifq->ifq_tail;
6006 *ifq->ifq_tail = tqe;
6007 ifq->ifq_tail = &tqe->tqe_next;
6008 tqe->tqe_next = NULL;
6010 tqe->tqe_die = ticks + ifq->ifq_ttl;
6011 tqe->tqe_touched = ticks;
6013 MUTEX_EXIT(&ifq->ifq_lock);
6017 /* ------------------------------------------------------------------------ */
6018 /* Function: ipf_movequeue */
6020 /* Parameters: tq(I) - pointer to timeout queue information */
6021 /* oifp(I) - old timeout queue entry was on */
6022 /* nifp(I) - new timeout queue to put entry on */
6024 /* Move a queue entry from one timeout queue to another timeout queue. */
6025 /* If it notices that the current entry is already last and does not need */
6026 /* to move queue, the return. */
6027 /* ------------------------------------------------------------------------ */
6029 ipf_movequeue(ticks, tqe, oifq, nifq)
6032 ipftq_t *oifq, *nifq;
6036 * If the queue hasn't changed and we last touched this entry at the
6037 * same ipf time, then we're not going to achieve anything by either
6038 * changing the ttl or moving it on the queue.
6040 if (oifq == nifq && tqe->tqe_touched == ticks)
6044 * For any of this to be outside the lock, there is a risk that two
6045 * packets entering simultaneously, with one changing to a different
6046 * queue and one not, could end up with things in a bizarre state.
6048 MUTEX_ENTER(&oifq->ifq_lock);
6050 tqe->tqe_touched = ticks;
6051 tqe->tqe_die = ticks + nifq->ifq_ttl;
6053 * Is the operation here going to be a no-op ?
6056 if ((tqe->tqe_next == NULL) ||
6057 (tqe->tqe_next->tqe_die == tqe->tqe_die)) {
6058 MUTEX_EXIT(&oifq->ifq_lock);
6064 * Remove from the old queue
6066 *tqe->tqe_pnext = tqe->tqe_next;
6068 tqe->tqe_next->tqe_pnext = tqe->tqe_pnext;
6070 oifq->ifq_tail = tqe->tqe_pnext;
6071 tqe->tqe_next = NULL;
6074 * If we're moving from one queue to another, release the
6075 * lock on the old queue and get a lock on the new queue.
6076 * For user defined queues, if we're moving off it, call
6077 * delete in case it can now be freed.
6080 tqe->tqe_ifq = NULL;
6082 (void) ipf_deletetimeoutqueue(oifq);
6084 MUTEX_EXIT(&oifq->ifq_lock);
6086 MUTEX_ENTER(&nifq->ifq_lock);
6088 tqe->tqe_ifq = nifq;
6093 * Add to the bottom of the new queue
6095 tqe->tqe_pnext = nifq->ifq_tail;
6096 *nifq->ifq_tail = tqe;
6097 nifq->ifq_tail = &tqe->tqe_next;
6098 MUTEX_EXIT(&nifq->ifq_lock);
6102 /* ------------------------------------------------------------------------ */
6103 /* Function: ipf_updateipid */
6104 /* Returns: int - 0 == success, -1 == error (packet should be droppped) */
6105 /* Parameters: fin(I) - pointer to packet information */
6107 /* When we are doing NAT, change the IP of every packet to represent a */
6108 /* single sequence of packets coming from the host, hiding any host */
6109 /* specific sequencing that might otherwise be revealed. If the packet is */
6110 /* a fragment, then store the 'new' IPid in the fragment cache and look up */
6111 /* the fragment cache for non-leading fragments. If a non-leading fragment */
6112 /* has no match in the cache, return an error. */
6113 /* ------------------------------------------------------------------------ */
6118 u_short id, ido, sums;
6123 ido = ntohs(ip->ip_id);
6124 if (fin->fin_off != 0) {
6125 sum = ipf_frag_ipidknown(fin);
6126 if (sum == 0xffffffff)
6130 ip->ip_id = htons(id);
6133 id = ntohs(ip->ip_id);
6134 if ((fin->fin_flx & FI_FRAG) != 0)
6135 (void) ipf_frag_ipidnew(fin, (u_32_t)id);
6140 CALC_SUMD(ido, id, sumd); /* DESTRUCTIVE MACRO! id,ido change */
6141 sum = (~ntohs(ip->ip_sum)) & 0xffff;
6143 sum = (sum >> 16) + (sum & 0xffff);
6144 sum = (sum >> 16) + (sum & 0xffff);
6145 sums = ~(u_short)sum;
6146 ip->ip_sum = htons(sums);
6151 #ifdef NEED_FRGETIFNAME
6152 /* ------------------------------------------------------------------------ */
6153 /* Function: ipf_getifname */
6154 /* Returns: char * - pointer to interface name */
6155 /* Parameters: ifp(I) - pointer to network interface */
6156 /* buffer(O) - pointer to where to store interface name */
6158 /* Constructs an interface name in the buffer passed. The buffer passed is */
6159 /* expected to be at least LIFNAMSIZ in bytes big. If buffer is passed in */
6160 /* as a NULL pointer then return a pointer to a static array. */
6161 /* ------------------------------------------------------------------------ */
6163 ipf_getifname(ifp, buffer)
6167 static char namebuf[LIFNAMSIZ];
6168 # if defined(MENTAT) || defined(__FreeBSD__) || defined(__osf__) || \
6169 defined(__sgi) || defined(linux) || defined(_AIX51) || \
6170 (defined(sun) && !defined(__SVR4) && !defined(__svr4__))
6178 (void) strncpy(buffer, ifp->if_name, LIFNAMSIZ);
6179 buffer[LIFNAMSIZ - 1] = '\0';
6180 # if defined(MENTAT) || defined(__FreeBSD__) || defined(__osf__) || \
6181 defined(__sgi) || defined(_AIX51) || \
6182 (defined(sun) && !defined(__SVR4) && !defined(__svr4__))
6183 for (s = buffer; *s; s++)
6185 unit = ifp->if_unit;
6186 space = LIFNAMSIZ - (s - buffer);
6187 if ((space > 0) && (unit >= 0)) {
6188 # if defined(SNPRINTF) && defined(_KERNEL)
6189 SNPRINTF(temp, sizeof(temp), "%d", unit);
6191 (void) sprintf(temp, "%d", unit);
6193 (void) strncpy(s, temp, space);
6201 /* ------------------------------------------------------------------------ */
6202 /* Function: ipf_ioctlswitch */
6203 /* Returns: int - -1 continue processing, else ioctl return value */
6204 /* Parameters: unit(I) - device unit opened */
6205 /* data(I) - pointer to ioctl data */
6206 /* cmd(I) - ioctl command */
6207 /* mode(I) - mode value */
6208 /* uid(I) - uid making the ioctl call */
6209 /* ctx(I) - pointer to context data */
6211 /* Based on the value of unit, call the appropriate ioctl handler or return */
6212 /* EIO if ipfilter is not running. Also checks if write perms are req'd */
6213 /* for the device in order to execute the ioctl. A special case is made */
6214 /* SIOCIPFINTERROR so that the same code isn't required in every handler. */
6215 /* The context data pointer is passed through as this is used as the key */
6216 /* for locating a matching token for continued access for walking lists, */
6218 /* ------------------------------------------------------------------------ */
6220 ipf_ioctlswitch(softc, unit, data, cmd, mode, uid, ctx)
6221 ipf_main_softc_t *softc;
6222 int unit, mode, uid;
6230 case SIOCIPFINTERROR :
6231 error = BCOPYOUT(&softc->ipf_interror, data,
6232 sizeof(softc->ipf_interror));
6245 error = ipf_ipf_ioctl(softc, data, cmd, mode, uid, ctx);
6248 if (softc->ipf_running > 0) {
6249 error = ipf_nat_ioctl(softc, data, cmd, mode,
6257 if (softc->ipf_running > 0) {
6258 error = ipf_state_ioctl(softc, data, cmd, mode,
6266 if (softc->ipf_running > 0) {
6267 error = ipf_auth_ioctl(softc, data, cmd, mode,
6275 if (softc->ipf_running > 0) {
6276 error = ipf_sync_ioctl(softc, data, cmd, mode,
6284 #ifdef IPFILTER_SCAN
6285 if (softc->ipf_running > 0)
6286 error = ipf_scan_ioctl(softc, data, cmd, mode,
6295 case IPL_LOGLOOKUP :
6296 if (softc->ipf_running > 0) {
6297 error = ipf_lookup_ioctl(softc, data, cmd, mode,
6315 * This array defines the expected size of objects coming into the kernel
6316 * for the various recognised object types. The first column is flags (see
6317 * below), 2nd column is current size, 3rd column is the version number of
6318 * when the current size became current.
6320 * 1 = minimum size, not absolute size
6322 static int ipf_objbytes[IPFOBJ_COUNT][3] = {
6323 { 1, sizeof(struct frentry), 5010000 }, /* 0 */
6324 { 1, sizeof(struct friostat), 5010000 },
6325 { 0, sizeof(struct fr_info), 5010000 },
6326 { 0, sizeof(struct ipf_authstat), 4010100 },
6327 { 0, sizeof(struct ipfrstat), 5010000 },
6328 { 1, sizeof(struct ipnat), 5010000 }, /* 5 */
6329 { 0, sizeof(struct natstat), 5010000 },
6330 { 0, sizeof(struct ipstate_save), 5010000 },
6331 { 1, sizeof(struct nat_save), 5010000 },
6332 { 0, sizeof(struct natlookup), 5010000 },
6333 { 1, sizeof(struct ipstate), 5010000 }, /* 10 */
6334 { 0, sizeof(struct ips_stat), 5010000 },
6335 { 0, sizeof(struct frauth), 5010000 },
6336 { 0, sizeof(struct ipftune), 4010100 },
6337 { 0, sizeof(struct nat), 5010000 },
6338 { 0, sizeof(struct ipfruleiter), 4011400 }, /* 15 */
6339 { 0, sizeof(struct ipfgeniter), 4011400 },
6340 { 0, sizeof(struct ipftable), 4011400 },
6341 { 0, sizeof(struct ipflookupiter), 4011400 },
6342 { 0, sizeof(struct ipftq) * IPF_TCP_NSTATES },
6343 { 1, 0, 0 }, /* IPFEXPR */
6344 { 0, 0, 0 }, /* PROXYCTL */
6345 { 0, sizeof (struct fripf), 5010000 }
6349 /* ------------------------------------------------------------------------ */
6350 /* Function: ipf_inobj */
6351 /* Returns: int - 0 = success, else failure */
6352 /* Parameters: softc(I) - soft context pointerto work with */
6353 /* data(I) - pointer to ioctl data */
6354 /* objp(O) - where to store ipfobj structure */
6355 /* ptr(I) - pointer to data to copy out */
6356 /* type(I) - type of structure being moved */
6358 /* Copy in the contents of what the ipfobj_t points to. In future, we */
6359 /* add things to check for version numbers, sizes, etc, to make it backward */
6360 /* compatible at the ABI for user land. */
6361 /* If objp is not NULL then we assume that the caller wants to see what is */
6362 /* in the ipfobj_t structure being copied in. As an example, this can tell */
6363 /* the caller what version of ipfilter the ioctl program was written to. */
6364 /* ------------------------------------------------------------------------ */
6366 ipf_inobj(softc, data, objp, ptr, type)
6367 ipf_main_softc_t *softc;
6377 if ((type < 0) || (type >= IPFOBJ_COUNT)) {
6384 error = BCOPYIN(data, objp, sizeof(*objp));
6390 if (objp->ipfo_type != type) {
6395 if (objp->ipfo_rev >= ipf_objbytes[type][2]) {
6396 if ((ipf_objbytes[type][0] & 1) != 0) {
6397 if (objp->ipfo_size < ipf_objbytes[type][1]) {
6401 size = ipf_objbytes[type][1];
6402 } else if (objp->ipfo_size == ipf_objbytes[type][1]) {
6403 size = objp->ipfo_size;
6408 error = COPYIN(objp->ipfo_ptr, ptr, size);
6414 #ifdef IPFILTER_COMPAT
6415 error = ipf_in_compat(softc, objp, ptr, 0);
6425 /* ------------------------------------------------------------------------ */
6426 /* Function: ipf_inobjsz */
6427 /* Returns: int - 0 = success, else failure */
6428 /* Parameters: softc(I) - soft context pointerto work with */
6429 /* data(I) - pointer to ioctl data */
6430 /* ptr(I) - pointer to store real data in */
6431 /* type(I) - type of structure being moved */
6432 /* sz(I) - size of data to copy */
6434 /* As per ipf_inobj, except the size of the object to copy in is passed in */
6435 /* but it must not be smaller than the size defined for the type and the */
6436 /* type must allow for varied sized objects. The extra requirement here is */
6437 /* that sz must match the size of the object being passed in - this is not */
6438 /* not possible nor required in ipf_inobj(). */
6439 /* ------------------------------------------------------------------------ */
6441 ipf_inobjsz(softc, data, ptr, type, sz)
6442 ipf_main_softc_t *softc;
6450 if ((type < 0) || (type >= IPFOBJ_COUNT)) {
6455 error = BCOPYIN(data, &obj, sizeof(obj));
6461 if (obj.ipfo_type != type) {
6466 if (obj.ipfo_rev >= ipf_objbytes[type][2]) {
6467 if (((ipf_objbytes[type][0] & 1) == 0) ||
6468 (sz < ipf_objbytes[type][1])) {
6472 error = COPYIN(obj.ipfo_ptr, ptr, sz);
6478 #ifdef IPFILTER_COMPAT
6479 error = ipf_in_compat(softc, &obj, ptr, sz);
6489 /* ------------------------------------------------------------------------ */
6490 /* Function: ipf_outobjsz */
6491 /* Returns: int - 0 = success, else failure */
6492 /* Parameters: data(I) - pointer to ioctl data */
6493 /* ptr(I) - pointer to store real data in */
6494 /* type(I) - type of structure being moved */
6495 /* sz(I) - size of data to copy */
6497 /* As per ipf_outobj, except the size of the object to copy out is passed in*/
6498 /* but it must not be smaller than the size defined for the type and the */
6499 /* type must allow for varied sized objects. The extra requirement here is */
6500 /* that sz must match the size of the object being passed in - this is not */
6501 /* not possible nor required in ipf_outobj(). */
6502 /* ------------------------------------------------------------------------ */
6504 ipf_outobjsz(softc, data, ptr, type, sz)
6505 ipf_main_softc_t *softc;
6513 if ((type < 0) || (type >= IPFOBJ_COUNT)) {
6518 error = BCOPYIN(data, &obj, sizeof(obj));
6524 if (obj.ipfo_type != type) {
6529 if (obj.ipfo_rev >= ipf_objbytes[type][2]) {
6530 if (((ipf_objbytes[type][0] & 1) == 0) ||
6531 (sz < ipf_objbytes[type][1])) {
6535 error = COPYOUT(ptr, obj.ipfo_ptr, sz);
6541 #ifdef IPFILTER_COMPAT
6542 error = ipf_out_compat(softc, &obj, ptr);
6552 /* ------------------------------------------------------------------------ */
6553 /* Function: ipf_outobj */
6554 /* Returns: int - 0 = success, else failure */
6555 /* Parameters: data(I) - pointer to ioctl data */
6556 /* ptr(I) - pointer to store real data in */
6557 /* type(I) - type of structure being moved */
6559 /* Copy out the contents of what ptr is to where ipfobj points to. In */
6560 /* future, we add things to check for version numbers, sizes, etc, to make */
6561 /* it backward compatible at the ABI for user land. */
6562 /* ------------------------------------------------------------------------ */
6564 ipf_outobj(softc, data, ptr, type)
6565 ipf_main_softc_t *softc;
6573 if ((type < 0) || (type >= IPFOBJ_COUNT)) {
6578 error = BCOPYIN(data, &obj, sizeof(obj));
6584 if (obj.ipfo_type != type) {
6589 if (obj.ipfo_rev >= ipf_objbytes[type][2]) {
6590 if ((ipf_objbytes[type][0] & 1) != 0) {
6591 if (obj.ipfo_size < ipf_objbytes[type][1]) {
6595 } else if (obj.ipfo_size != ipf_objbytes[type][1]) {
6600 error = COPYOUT(ptr, obj.ipfo_ptr, obj.ipfo_size);
6606 #ifdef IPFILTER_COMPAT
6607 error = ipf_out_compat(softc, &obj, ptr);
6617 /* ------------------------------------------------------------------------ */
6618 /* Function: ipf_outobjk */
6619 /* Returns: int - 0 = success, else failure */
6620 /* Parameters: obj(I) - pointer to data description structure */
6621 /* ptr(I) - pointer to kernel data to copy out */
6623 /* In the above functions, the ipfobj_t structure is copied into the kernel,*/
6624 /* telling ipfilter how to copy out data. In this instance, the ipfobj_t is */
6625 /* already populated with information and now we just need to use it. */
6626 /* There is no need for this function to have a "type" parameter as there */
6627 /* is no point in validating information that comes from the kernel with */
6629 /* ------------------------------------------------------------------------ */
6631 ipf_outobjk(softc, obj, ptr)
6632 ipf_main_softc_t *softc;
6636 int type = obj->ipfo_type;
6639 if ((type < 0) || (type >= IPFOBJ_COUNT)) {
6644 if (obj->ipfo_rev >= ipf_objbytes[type][2]) {
6645 if ((ipf_objbytes[type][0] & 1) != 0) {
6646 if (obj->ipfo_size < ipf_objbytes[type][1]) {
6651 } else if (obj->ipfo_size != ipf_objbytes[type][1]) {
6656 error = COPYOUT(ptr, obj->ipfo_ptr, obj->ipfo_size);
6662 #ifdef IPFILTER_COMPAT
6663 error = ipf_out_compat(softc, obj, ptr);
6673 /* ------------------------------------------------------------------------ */
6674 /* Function: ipf_checkl4sum */
6675 /* Returns: int - 0 = good, -1 = bad, 1 = cannot check */
6676 /* Parameters: fin(I) - pointer to packet information */
6678 /* If possible, calculate the layer 4 checksum for the packet. If this is */
6679 /* not possible, return without indicating a failure or success but in a */
6680 /* way that is ditinguishable. This function should only be called by the */
6681 /* ipf_checkv6sum() for each platform. */
6682 /* ------------------------------------------------------------------------ */
6687 u_short sum, hdrsum, *csump;
6692 * If the TCP packet isn't a fragment, isn't too short and otherwise
6693 * isn't already considered "bad", then validate the checksum. If
6694 * this check fails then considered the packet to be "bad".
6696 if ((fin->fin_flx & (FI_FRAG|FI_SHORT|FI_BAD)) != 0)
6707 csump = &((tcphdr_t *)fin->fin_dp)->th_sum;
6713 if (udp->uh_sum != 0) {
6714 csump = &udp->uh_sum;
6720 case IPPROTO_ICMPV6 :
6721 csump = &((struct icmp6_hdr *)fin->fin_dp)->icmp6_cksum;
6727 csump = &((struct icmp *)fin->fin_dp)->icmp_cksum;
6740 sum = fr_cksum(fin, fin->fin_ip, fin->fin_p, fin->fin_dp);
6742 #if !defined(_KERNEL)
6743 if (sum == hdrsum) {
6744 FR_DEBUG(("checkl4sum: %hx == %hx\n", sum, hdrsum));
6746 FR_DEBUG(("checkl4sum: %hx != %hx\n", sum, hdrsum));
6749 DT2(l4sums, u_short, hdrsum, u_short, sum);
6750 if (hdrsum == sum) {
6751 fin->fin_cksum = FI_CK_SUMOK;
6754 fin->fin_cksum = FI_CK_BAD;
6759 /* ------------------------------------------------------------------------ */
6760 /* Function: ipf_ifpfillv4addr */
6761 /* Returns: int - 0 = address update, -1 = address not updated */
6762 /* Parameters: atype(I) - type of network address update to perform */
6763 /* sin(I) - pointer to source of address information */
6764 /* mask(I) - pointer to source of netmask information */
6765 /* inp(I) - pointer to destination address store */
6766 /* inpmask(I) - pointer to destination netmask store */
6768 /* Given a type of network address update (atype) to perform, copy */
6769 /* information from sin/mask into inp/inpmask. If ipnmask is NULL then no */
6770 /* netmask update is performed unless FRI_NETMASKED is passed as atype, in */
6771 /* which case the operation fails. For all values of atype other than */
6772 /* FRI_NETMASKED, if inpmask is non-NULL then the mask is set to an all 1s */
6774 /* ------------------------------------------------------------------------ */
6776 ipf_ifpfillv4addr(atype, sin, mask, inp, inpmask)
6778 struct sockaddr_in *sin, *mask;
6779 struct in_addr *inp, *inpmask;
6781 if (inpmask != NULL && atype != FRI_NETMASKED)
6782 inpmask->s_addr = 0xffffffff;
6784 if (atype == FRI_NETWORK || atype == FRI_NETMASKED) {
6785 if (atype == FRI_NETMASKED) {
6786 if (inpmask == NULL)
6788 inpmask->s_addr = mask->sin_addr.s_addr;
6790 inp->s_addr = sin->sin_addr.s_addr & mask->sin_addr.s_addr;
6792 inp->s_addr = sin->sin_addr.s_addr;
6799 /* ------------------------------------------------------------------------ */
6800 /* Function: ipf_ifpfillv6addr */
6801 /* Returns: int - 0 = address update, -1 = address not updated */
6802 /* Parameters: atype(I) - type of network address update to perform */
6803 /* sin(I) - pointer to source of address information */
6804 /* mask(I) - pointer to source of netmask information */
6805 /* inp(I) - pointer to destination address store */
6806 /* inpmask(I) - pointer to destination netmask store */
6808 /* Given a type of network address update (atype) to perform, copy */
6809 /* information from sin/mask into inp/inpmask. If ipnmask is NULL then no */
6810 /* netmask update is performed unless FRI_NETMASKED is passed as atype, in */
6811 /* which case the operation fails. For all values of atype other than */
6812 /* FRI_NETMASKED, if inpmask is non-NULL then the mask is set to an all 1s */
6814 /* ------------------------------------------------------------------------ */
6816 ipf_ifpfillv6addr(atype, sin, mask, inp, inpmask)
6818 struct sockaddr_in6 *sin, *mask;
6819 i6addr_t *inp, *inpmask;
6821 i6addr_t *src, *and;
6823 src = (i6addr_t *)&sin->sin6_addr;
6824 and = (i6addr_t *)&mask->sin6_addr;
6826 if (inpmask != NULL && atype != FRI_NETMASKED) {
6827 inpmask->i6[0] = 0xffffffff;
6828 inpmask->i6[1] = 0xffffffff;
6829 inpmask->i6[2] = 0xffffffff;
6830 inpmask->i6[3] = 0xffffffff;
6833 if (atype == FRI_NETWORK || atype == FRI_NETMASKED) {
6834 if (atype == FRI_NETMASKED) {
6835 if (inpmask == NULL)
6837 inpmask->i6[0] = and->i6[0];
6838 inpmask->i6[1] = and->i6[1];
6839 inpmask->i6[2] = and->i6[2];
6840 inpmask->i6[3] = and->i6[3];
6843 inp->i6[0] = src->i6[0] & and->i6[0];
6844 inp->i6[1] = src->i6[1] & and->i6[1];
6845 inp->i6[2] = src->i6[2] & and->i6[2];
6846 inp->i6[3] = src->i6[3] & and->i6[3];
6848 inp->i6[0] = src->i6[0];
6849 inp->i6[1] = src->i6[1];
6850 inp->i6[2] = src->i6[2];
6851 inp->i6[3] = src->i6[3];
6858 /* ------------------------------------------------------------------------ */
6859 /* Function: ipf_matchtag */
6860 /* Returns: 0 == mismatch, 1 == match. */
6861 /* Parameters: tag1(I) - pointer to first tag to compare */
6862 /* tag2(I) - pointer to second tag to compare */
6864 /* Returns true (non-zero) or false(0) if the two tag structures can be */
6865 /* considered to be a match or not match, respectively. The tag is 16 */
6866 /* bytes long (16 characters) but that is overlayed with 4 32bit ints so */
6867 /* compare the ints instead, for speed. tag1 is the master of the */
6868 /* comparison. This function should only be called with both tag1 and tag2 */
6869 /* as non-NULL pointers. */
6870 /* ------------------------------------------------------------------------ */
6872 ipf_matchtag(tag1, tag2)
6873 ipftag_t *tag1, *tag2;
6878 if ((tag1->ipt_num[0] == 0) && (tag2->ipt_num[0] == 0))
6881 if ((tag1->ipt_num[0] == tag2->ipt_num[0]) &&
6882 (tag1->ipt_num[1] == tag2->ipt_num[1]) &&
6883 (tag1->ipt_num[2] == tag2->ipt_num[2]) &&
6884 (tag1->ipt_num[3] == tag2->ipt_num[3]))
6890 /* ------------------------------------------------------------------------ */
6891 /* Function: ipf_coalesce */
6892 /* Returns: 1 == success, -1 == failure, 0 == no change */
6893 /* Parameters: fin(I) - pointer to packet information */
6895 /* Attempt to get all of the packet data into a single, contiguous buffer. */
6896 /* If this call returns a failure then the buffers have also been freed. */
6897 /* ------------------------------------------------------------------------ */
6903 if ((fin->fin_flx & FI_COALESCE) != 0)
6907 * If the mbuf pointers indicate that there is no mbuf to work with,
6908 * return but do not indicate success or failure.
6910 if (fin->fin_m == NULL || fin->fin_mp == NULL)
6913 #if defined(_KERNEL)
6914 if (ipf_pullup(fin->fin_m, fin, fin->fin_plen) == NULL) {
6915 ipf_main_softc_t *softc = fin->fin_main_soft;
6917 DT1(frb_coalesce, fr_info_t *, fin);
6918 LBUMP(ipf_stats[fin->fin_out].fr_badcoalesces);
6920 FREE_MB_T(*fin->fin_mp);
6922 fin->fin_reason = FRB_COALESCE;
6923 *fin->fin_mp = NULL;
6928 fin = fin; /* LINT */
6935 * The following table lists all of the tunable variables that can be
6936 * accessed via SIOCIPFGET/SIOCIPFSET/SIOCIPFGETNEXt. The format of each row
6937 * in the table below is as follows:
6939 * pointer to value, name of value, minimum, maximum, size of the value's
6940 * container, value attribute flags
6942 * For convienience, IPFT_RDONLY means the value is read-only, IPFT_WRDISABLED
6943 * means the value can only be written to when IPFilter is loaded but disabled.
6944 * The obvious implication is if neither of these are set then the value can be
6945 * changed at any time without harm.
6949 /* ------------------------------------------------------------------------ */
6950 /* Function: ipf_tune_findbycookie */
6951 /* Returns: NULL = search failed, else pointer to tune struct */
6952 /* Parameters: cookie(I) - cookie value to search for amongst tuneables */
6953 /* next(O) - pointer to place to store the cookie for the */
6954 /* "next" tuneable, if it is desired. */
6956 /* This function is used to walk through all of the existing tunables with */
6957 /* successive calls. It searches the known tunables for the one which has */
6958 /* a matching value for "cookie" - ie its address. When returning a match, */
6959 /* the next one to be found may be returned inside next. */
6960 /* ------------------------------------------------------------------------ */
6961 static ipftuneable_t *
6962 ipf_tune_findbycookie(ptop, cookie, next)
6963 ipftuneable_t **ptop;
6964 void *cookie, **next;
6966 ipftuneable_t *ta, **tap;
6968 for (ta = *ptop; ta->ipft_name != NULL; ta++)
6972 * If the next entry in the array has a name
6973 * present, then return a pointer to it for
6974 * where to go next, else return a pointer to
6975 * the dynaminc list as a key to search there
6976 * next. This facilitates a weak linking of
6977 * the two "lists" together.
6979 if ((ta + 1)->ipft_name != NULL)
6987 for (tap = ptop; (ta = *tap) != NULL; tap = &ta->ipft_next)
6988 if (tap == cookie) {
6990 *next = &ta->ipft_next;
7000 /* ------------------------------------------------------------------------ */
7001 /* Function: ipf_tune_findbyname */
7002 /* Returns: NULL = search failed, else pointer to tune struct */
7003 /* Parameters: name(I) - name of the tuneable entry to find. */
7005 /* Search the static array of tuneables and the list of dynamic tuneables */
7006 /* for an entry with a matching name. If we can find one, return a pointer */
7007 /* to the matching structure. */
7008 /* ------------------------------------------------------------------------ */
7009 static ipftuneable_t *
7010 ipf_tune_findbyname(top, name)
7016 for (ta = top; ta != NULL; ta = ta->ipft_next)
7017 if (!strcmp(ta->ipft_name, name)) {
7025 /* ------------------------------------------------------------------------ */
7026 /* Function: ipf_tune_add_array */
7027 /* Returns: int - 0 == success, else failure */
7028 /* Parameters: newtune - pointer to new tune array to add to tuneables */
7030 /* Appends tune structures from the array passed in (newtune) to the end of */
7031 /* the current list of "dynamic" tuneable parameters. */
7032 /* If any entry to be added is already present (by name) then the operation */
7033 /* is aborted - entries that have been added are removed before returning. */
7034 /* An entry with no name (NULL) is used as the indication that the end of */
7035 /* the array has been reached. */
7036 /* ------------------------------------------------------------------------ */
7038 ipf_tune_add_array(softc, newtune)
7039 ipf_main_softc_t *softc;
7040 ipftuneable_t *newtune;
7042 ipftuneable_t *nt, *dt;
7045 for (nt = newtune; nt->ipft_name != NULL; nt++) {
7046 error = ipf_tune_add(softc, nt);
7048 for (dt = newtune; dt != nt; dt++) {
7049 (void) ipf_tune_del(softc, dt);
7058 /* ------------------------------------------------------------------------ */
7059 /* Function: ipf_tune_array_link */
7060 /* Returns: 0 == success, -1 == failure */
7061 /* Parameters: softc(I) - soft context pointerto work with */
7062 /* array(I) - pointer to an array of tuneables */
7064 /* Given an array of tunables (array), append them to the current list of */
7065 /* tuneables for this context (softc->ipf_tuners.) To properly prepare the */
7066 /* the array for being appended to the list, initialise all of the next */
7067 /* pointers so we don't need to walk parts of it with ++ and others with */
7068 /* next. The array is expected to have an entry with a NULL name as the */
7069 /* terminator. Trying to add an array with no non-NULL names will return as */
7071 /* ------------------------------------------------------------------------ */
7073 ipf_tune_array_link(softc, array)
7074 ipf_main_softc_t *softc;
7075 ipftuneable_t *array;
7077 ipftuneable_t *t, **p;
7080 if (t->ipft_name == NULL)
7083 for (; t[1].ipft_name != NULL; t++)
7084 t[0].ipft_next = &t[1];
7085 t->ipft_next = NULL;
7088 * Since a pointer to the last entry isn't kept, we need to find it
7089 * each time we want to add new variables to the list.
7091 for (p = &softc->ipf_tuners; (t = *p) != NULL; p = &t->ipft_next)
7092 if (t->ipft_name == NULL)
7100 /* ------------------------------------------------------------------------ */
7101 /* Function: ipf_tune_array_unlink */
7102 /* Returns: 0 == success, -1 == failure */
7103 /* Parameters: softc(I) - soft context pointerto work with */
7104 /* array(I) - pointer to an array of tuneables */
7106 /* ------------------------------------------------------------------------ */
7108 ipf_tune_array_unlink(softc, array)
7109 ipf_main_softc_t *softc;
7110 ipftuneable_t *array;
7112 ipftuneable_t *t, **p;
7114 for (p = &softc->ipf_tuners; (t = *p) != NULL; p = &t->ipft_next)
7120 for (; t[1].ipft_name != NULL; t++)
7129 /* ------------------------------------------------------------------------ */
7130 /* Function: ipf_tune_array_copy */
7131 /* Returns: NULL = failure, else pointer to new array */
7132 /* Parameters: base(I) - pointer to structure base */
7133 /* size(I) - size of the array at template */
7134 /* template(I) - original array to copy */
7136 /* Allocate memory for a new set of tuneable values and copy everything */
7137 /* from template into the new region of memory. The new region is full of */
7138 /* uninitialised pointers (ipft_next) so set them up. Now, ipftp_offset... */
7140 /* NOTE: the following assumes that sizeof(long) == sizeof(void *) */
7141 /* In the array template, ipftp_offset is the offset (in bytes) of the */
7142 /* location of the tuneable value inside the structure pointed to by base. */
7143 /* As ipftp_offset is a union over the pointers to the tuneable values, if */
7144 /* we add base to the copy's ipftp_offset, copy ends up with a pointer in */
7145 /* ipftp_void that points to the stored value. */
7146 /* ------------------------------------------------------------------------ */
7148 ipf_tune_array_copy(base, size, template)
7151 ipftuneable_t *template;
7153 ipftuneable_t *copy;
7157 KMALLOCS(copy, ipftuneable_t *, size);
7161 bcopy(template, copy, size);
7163 for (i = 0; copy[i].ipft_name; i++) {
7164 copy[i].ipft_una.ipftp_offset += (u_long)base;
7165 copy[i].ipft_next = copy + i + 1;
7172 /* ------------------------------------------------------------------------ */
7173 /* Function: ipf_tune_add */
7174 /* Returns: int - 0 == success, else failure */
7175 /* Parameters: newtune - pointer to new tune entry to add to tuneables */
7177 /* Appends tune structures from the array passed in (newtune) to the end of */
7178 /* the current list of "dynamic" tuneable parameters. Once added, the */
7179 /* owner of the object is not expected to ever change "ipft_next". */
7180 /* ------------------------------------------------------------------------ */
7182 ipf_tune_add(softc, newtune)
7183 ipf_main_softc_t *softc;
7184 ipftuneable_t *newtune;
7186 ipftuneable_t *ta, **tap;
7188 ta = ipf_tune_findbyname(softc->ipf_tuners, newtune->ipft_name);
7194 for (tap = &softc->ipf_tuners; *tap != NULL; tap = &(*tap)->ipft_next)
7197 newtune->ipft_next = NULL;
7203 /* ------------------------------------------------------------------------ */
7204 /* Function: ipf_tune_del */
7205 /* Returns: int - 0 == success, else failure */
7206 /* Parameters: oldtune - pointer to tune entry to remove from the list of */
7207 /* current dynamic tuneables */
7209 /* Search for the tune structure, by pointer, in the list of those that are */
7210 /* dynamically added at run time. If found, adjust the list so that this */
7211 /* structure is no longer part of it. */
7212 /* ------------------------------------------------------------------------ */
7214 ipf_tune_del(softc, oldtune)
7215 ipf_main_softc_t *softc;
7216 ipftuneable_t *oldtune;
7218 ipftuneable_t *ta, **tap;
7221 for (tap = &softc->ipf_tuners; (ta = *tap) != NULL;
7222 tap = &ta->ipft_next) {
7223 if (ta == oldtune) {
7224 *tap = oldtune->ipft_next;
7225 oldtune->ipft_next = NULL;
7238 /* ------------------------------------------------------------------------ */
7239 /* Function: ipf_tune_del_array */
7240 /* Returns: int - 0 == success, else failure */
7241 /* Parameters: oldtune - pointer to tuneables array */
7243 /* Remove each tuneable entry in the array from the list of "dynamic" */
7244 /* tunables. If one entry should fail to be found, an error will be */
7245 /* returned and no further ones removed. */
7246 /* An entry with a NULL name is used as the indicator of the last entry in */
7248 /* ------------------------------------------------------------------------ */
7250 ipf_tune_del_array(softc, oldtune)
7251 ipf_main_softc_t *softc;
7252 ipftuneable_t *oldtune;
7257 for (ot = oldtune; ot->ipft_name != NULL; ot++) {
7258 error = ipf_tune_del(softc, ot);
7268 /* ------------------------------------------------------------------------ */
7269 /* Function: ipf_tune */
7270 /* Returns: int - 0 == success, else failure */
7271 /* Parameters: cmd(I) - ioctl command number */
7272 /* data(I) - pointer to ioctl data structure */
7274 /* Implement handling of SIOCIPFGETNEXT, SIOCIPFGET and SIOCIPFSET. These */
7275 /* three ioctls provide the means to access and control global variables */
7276 /* within IPFilter, allowing (for example) timeouts and table sizes to be */
7277 /* changed without rebooting, reloading or recompiling. The initialisation */
7278 /* and 'destruction' routines of the various components of ipfilter are all */
7279 /* each responsible for handling their own values being too big. */
7280 /* ------------------------------------------------------------------------ */
7282 ipf_ipftune(softc, cmd, data)
7283 ipf_main_softc_t *softc;
7292 error = ipf_inobj(softc, data, NULL, &tu, IPFOBJ_TUNEABLE);
7296 tu.ipft_name[sizeof(tu.ipft_name) - 1] = '\0';
7297 cookie = tu.ipft_cookie;
7302 case SIOCIPFGETNEXT :
7304 * If cookie is non-NULL, assume it to be a pointer to the last
7305 * entry we looked at, so find it (if possible) and return a
7306 * pointer to the next one after it. The last entry in the
7307 * the table is a NULL entry, so when we get to it, set cookie
7308 * to NULL and return that, indicating end of list, erstwhile
7309 * if we come in with cookie set to NULL, we are starting anew
7310 * at the front of the list.
7312 if (cookie != NULL) {
7313 ta = ipf_tune_findbycookie(&softc->ipf_tuners,
7314 cookie, &tu.ipft_cookie);
7316 ta = softc->ipf_tuners;
7317 tu.ipft_cookie = ta + 1;
7321 * Entry found, but does the data pointed to by that
7322 * row fit in what we can return?
7324 if (ta->ipft_sz > sizeof(tu.ipft_un)) {
7330 if (ta->ipft_sz == sizeof(u_long))
7331 tu.ipft_vlong = *ta->ipft_plong;
7332 else if (ta->ipft_sz == sizeof(u_int))
7333 tu.ipft_vint = *ta->ipft_pint;
7334 else if (ta->ipft_sz == sizeof(u_short))
7335 tu.ipft_vshort = *ta->ipft_pshort;
7336 else if (ta->ipft_sz == sizeof(u_char))
7337 tu.ipft_vchar = *ta->ipft_pchar;
7339 tu.ipft_sz = ta->ipft_sz;
7340 tu.ipft_min = ta->ipft_min;
7341 tu.ipft_max = ta->ipft_max;
7342 tu.ipft_flags = ta->ipft_flags;
7343 bcopy(ta->ipft_name, tu.ipft_name,
7344 MIN(sizeof(tu.ipft_name),
7345 strlen(ta->ipft_name) + 1));
7347 error = ipf_outobj(softc, data, &tu, IPFOBJ_TUNEABLE);
7353 * Search by name or by cookie value for a particular entry
7354 * in the tuning paramter table.
7358 if (cookie != NULL) {
7359 ta = ipf_tune_findbycookie(&softc->ipf_tuners,
7363 } else if (tu.ipft_name[0] != '\0') {
7364 ta = ipf_tune_findbyname(softc->ipf_tuners,
7372 if (cmd == (ioctlcmd_t)SIOCIPFGET) {
7374 * Fetch the tuning parameters for a particular value
7377 if (ta->ipft_sz == sizeof(u_long))
7378 tu.ipft_vlong = *ta->ipft_plong;
7379 else if (ta->ipft_sz == sizeof(u_int))
7380 tu.ipft_vint = *ta->ipft_pint;
7381 else if (ta->ipft_sz == sizeof(u_short))
7382 tu.ipft_vshort = *ta->ipft_pshort;
7383 else if (ta->ipft_sz == sizeof(u_char))
7384 tu.ipft_vchar = *ta->ipft_pchar;
7385 tu.ipft_cookie = ta;
7386 tu.ipft_sz = ta->ipft_sz;
7387 tu.ipft_min = ta->ipft_min;
7388 tu.ipft_max = ta->ipft_max;
7389 tu.ipft_flags = ta->ipft_flags;
7390 error = ipf_outobj(softc, data, &tu, IPFOBJ_TUNEABLE);
7392 } else if (cmd == (ioctlcmd_t)SIOCIPFSET) {
7394 * Set an internal parameter. The hard part here is
7395 * getting the new value safely and correctly out of
7396 * the kernel (given we only know its size, not type.)
7400 if (((ta->ipft_flags & IPFT_WRDISABLED) != 0) &&
7401 (softc->ipf_running > 0)) {
7408 if (in < ta->ipft_min || in > ta->ipft_max) {
7414 if (ta->ipft_func != NULL) {
7418 error = (*ta->ipft_func)(softc, ta,
7422 } else if (ta->ipft_sz == sizeof(u_long)) {
7423 tu.ipft_vlong = *ta->ipft_plong;
7424 *ta->ipft_plong = in;
7426 } else if (ta->ipft_sz == sizeof(u_int)) {
7427 tu.ipft_vint = *ta->ipft_pint;
7428 *ta->ipft_pint = (u_int)(in & 0xffffffff);
7430 } else if (ta->ipft_sz == sizeof(u_short)) {
7431 tu.ipft_vshort = *ta->ipft_pshort;
7432 *ta->ipft_pshort = (u_short)(in & 0xffff);
7434 } else if (ta->ipft_sz == sizeof(u_char)) {
7435 tu.ipft_vchar = *ta->ipft_pchar;
7436 *ta->ipft_pchar = (u_char)(in & 0xff);
7438 error = ipf_outobj(softc, data, &tu, IPFOBJ_TUNEABLE);
7452 /* ------------------------------------------------------------------------ */
7453 /* Function: ipf_zerostats */
7454 /* Returns: int - 0 = success, else failure */
7455 /* Parameters: data(O) - pointer to pointer for copying data back to */
7457 /* Copies the current statistics out to userspace and then zero's the */
7458 /* current ones in the kernel. The lock is only held across the bzero() as */
7459 /* the copyout may result in paging (ie network activity.) */
7460 /* ------------------------------------------------------------------------ */
7462 ipf_zerostats(softc, data)
7463 ipf_main_softc_t *softc;
7470 error = ipf_inobj(softc, data, &obj, &fio, IPFOBJ_IPFSTAT);
7473 ipf_getstat(softc, &fio, obj.ipfo_rev);
7474 error = ipf_outobj(softc, data, &fio, IPFOBJ_IPFSTAT);
7478 WRITE_ENTER(&softc->ipf_mutex);
7479 bzero(&softc->ipf_stats, sizeof(softc->ipf_stats));
7480 RWLOCK_EXIT(&softc->ipf_mutex);
7486 /* ------------------------------------------------------------------------ */
7487 /* Function: ipf_resolvedest */
7489 /* Parameters: softc(I) - pointer to soft context main structure */
7490 /* base(I) - where strings are stored */
7491 /* fdp(IO) - pointer to destination information to resolve */
7492 /* v(I) - IP protocol version to match */
7494 /* Looks up an interface name in the frdest structure pointed to by fdp and */
7495 /* if a matching name can be found for the particular IP protocol version */
7496 /* then store the interface pointer in the frdest struct. If no match is */
7497 /* found, then set the interface pointer to be -1 as NULL is considered to */
7498 /* indicate there is no information at all in the structure. */
7499 /* ------------------------------------------------------------------------ */
7501 ipf_resolvedest(softc, base, fdp, v)
7502 ipf_main_softc_t *softc;
7512 if (fdp->fd_name != -1) {
7513 if (fdp->fd_type == FRD_DSTLIST) {
7514 ifp = ipf_lookup_res_name(softc, IPL_LOGIPF,
7516 base + fdp->fd_name,
7523 ifp = GETIFP(base + fdp->fd_name, v);
7530 if ((ifp != NULL) && (ifp != (void *)-1)) {
7531 fdp->fd_local = ipf_deliverlocal(softc, v, ifp, &fdp->fd_ip6);
7538 /* ------------------------------------------------------------------------ */
7539 /* Function: ipf_resolvenic */
7540 /* Returns: void* - NULL = wildcard name, -1 = failed to find NIC, else */
7541 /* pointer to interface structure for NIC */
7542 /* Parameters: softc(I)- pointer to soft context main structure */
7543 /* name(I) - complete interface name */
7544 /* v(I) - IP protocol version */
7546 /* Look for a network interface structure that firstly has a matching name */
7547 /* to that passed in and that is also being used for that IP protocol */
7548 /* version (necessary on some platforms where there are separate listings */
7549 /* for both IPv4 and IPv6 on the same physical NIC. */
7550 /* ------------------------------------------------------------------------ */
7552 ipf_resolvenic(softc, name, v)
7553 ipf_main_softc_t *softc;
7559 softc = softc; /* gcc -Wextra */
7560 if (name[0] == '\0')
7563 if ((name[1] == '\0') && ((name[0] == '-') || (name[0] == '*'))) {
7567 nic = GETIFP(name, v);
7574 /* ------------------------------------------------------------------------ */
7575 /* Function: ipf_token_expire */
7576 /* Returns: None. */
7577 /* Parameters: softc(I) - pointer to soft context main structure */
7579 /* This function is run every ipf tick to see if there are any tokens that */
7580 /* have been held for too long and need to be freed up. */
7581 /* ------------------------------------------------------------------------ */
7583 ipf_token_expire(softc)
7584 ipf_main_softc_t *softc;
7588 WRITE_ENTER(&softc->ipf_tokens);
7589 while ((it = softc->ipf_token_head) != NULL) {
7590 if (it->ipt_die > softc->ipf_ticks)
7593 ipf_token_deref(softc, it);
7595 RWLOCK_EXIT(&softc->ipf_tokens);
7599 /* ------------------------------------------------------------------------ */
7600 /* Function: ipf_token_flush */
7601 /* Returns: None. */
7602 /* Parameters: softc(I) - pointer to soft context main structure */
7604 /* Loop through all of the existing tokens and call deref to see if they */
7605 /* can be freed. Normally a function like this might just loop on */
7606 /* ipf_token_head but there is a chance that a token might have a ref count */
7607 /* of greater than one and in that case the the reference would drop twice */
7608 /* by code that is only entitled to drop it once. */
7609 /* ------------------------------------------------------------------------ */
7611 ipf_token_flush(softc)
7612 ipf_main_softc_t *softc;
7614 ipftoken_t *it, *next;
7616 WRITE_ENTER(&softc->ipf_tokens);
7617 for (it = softc->ipf_token_head; it != NULL; it = next) {
7618 next = it->ipt_next;
7619 (void) ipf_token_deref(softc, it);
7621 RWLOCK_EXIT(&softc->ipf_tokens);
7625 /* ------------------------------------------------------------------------ */
7626 /* Function: ipf_token_del */
7627 /* Returns: int - 0 = success, else error */
7628 /* Parameters: softc(I)- pointer to soft context main structure */
7629 /* type(I) - the token type to match */
7630 /* uid(I) - uid owning the token */
7631 /* ptr(I) - context pointer for the token */
7633 /* This function looks for a a token in the current list that matches up */
7634 /* the fields (type, uid, ptr). If none is found, ESRCH is returned, else */
7635 /* call ipf_token_dewref() to remove it from the list. In the event that */
7636 /* the token has a reference held elsewhere, setting ipt_complete to 2 */
7637 /* enables debugging to distinguish between the two paths that ultimately */
7638 /* lead to a token to be deleted. */
7639 /* ------------------------------------------------------------------------ */
7641 ipf_token_del(softc, type, uid, ptr)
7642 ipf_main_softc_t *softc;
7652 WRITE_ENTER(&softc->ipf_tokens);
7653 for (it = softc->ipf_token_head; it != NULL; it = it->ipt_next) {
7654 if (ptr == it->ipt_ctx && type == it->ipt_type &&
7655 uid == it->ipt_uid) {
7656 it->ipt_complete = 2;
7657 ipf_token_deref(softc, it);
7662 RWLOCK_EXIT(&softc->ipf_tokens);
7668 /* ------------------------------------------------------------------------ */
7669 /* Function: ipf_token_mark_complete */
7670 /* Returns: None. */
7671 /* Parameters: token(I) - pointer to token structure */
7673 /* Mark a token as being ineligable for being found with ipf_token_find. */
7674 /* ------------------------------------------------------------------------ */
7676 ipf_token_mark_complete(token)
7679 if (token->ipt_complete == 0)
7680 token->ipt_complete = 1;
7684 /* ------------------------------------------------------------------------ */
7685 /* Function: ipf_token_find */
7686 /* Returns: ipftoken_t * - NULL if no memory, else pointer to token */
7687 /* Parameters: softc(I)- pointer to soft context main structure */
7688 /* type(I) - the token type to match */
7689 /* uid(I) - uid owning the token */
7690 /* ptr(I) - context pointer for the token */
7692 /* This function looks for a live token in the list of current tokens that */
7693 /* matches the tuple (type, uid, ptr). If one cannot be found then one is */
7694 /* allocated. If one is found then it is moved to the top of the list of */
7695 /* currently active tokens. */
7696 /* ------------------------------------------------------------------------ */
7698 ipf_token_find(softc, type, uid, ptr)
7699 ipf_main_softc_t *softc;
7703 ipftoken_t *it, *new;
7705 KMALLOC(new, ipftoken_t *);
7707 bzero((char *)new, sizeof(*new));
7709 WRITE_ENTER(&softc->ipf_tokens);
7710 for (it = softc->ipf_token_head; it != NULL; it = it->ipt_next) {
7711 if ((ptr == it->ipt_ctx) && (type == it->ipt_type) &&
7712 (uid == it->ipt_uid) && (it->ipt_complete < 2))
7720 RWLOCK_EXIT(&softc->ipf_tokens);
7725 it->ipt_type = type;
7733 if (it->ipt_complete > 0)
7736 ipf_token_unlink(softc, it);
7740 it->ipt_pnext = softc->ipf_token_tail;
7741 *softc->ipf_token_tail = it;
7742 softc->ipf_token_tail = &it->ipt_next;
7743 it->ipt_next = NULL;
7746 it->ipt_die = softc->ipf_ticks + 20;
7749 RWLOCK_EXIT(&softc->ipf_tokens);
7755 /* ------------------------------------------------------------------------ */
7756 /* Function: ipf_token_unlink */
7757 /* Returns: None. */
7758 /* Parameters: softc(I) - pointer to soft context main structure */
7759 /* token(I) - pointer to token structure */
7760 /* Write Locks: ipf_tokens */
7762 /* This function unlinks a token structure from the linked list of tokens */
7763 /* that "own" it. The head pointer never needs to be explicitly adjusted */
7764 /* but the tail does due to the linked list implementation. */
7765 /* ------------------------------------------------------------------------ */
7767 ipf_token_unlink(softc, token)
7768 ipf_main_softc_t *softc;
7772 if (softc->ipf_token_tail == &token->ipt_next)
7773 softc->ipf_token_tail = token->ipt_pnext;
7775 *token->ipt_pnext = token->ipt_next;
7776 if (token->ipt_next != NULL)
7777 token->ipt_next->ipt_pnext = token->ipt_pnext;
7778 token->ipt_next = NULL;
7779 token->ipt_pnext = NULL;
7783 /* ------------------------------------------------------------------------ */
7784 /* Function: ipf_token_deref */
7785 /* Returns: int - 0 == token freed, else reference count */
7786 /* Parameters: softc(I) - pointer to soft context main structure */
7787 /* token(I) - pointer to token structure */
7788 /* Write Locks: ipf_tokens */
7790 /* Drop the reference count on the token structure and if it drops to zero, */
7791 /* call the dereference function for the token type because it is then */
7792 /* possible to free the token data structure. */
7793 /* ------------------------------------------------------------------------ */
7795 ipf_token_deref(softc, token)
7796 ipf_main_softc_t *softc;
7799 void *data, **datap;
7801 ASSERT(token->ipt_ref > 0);
7803 if (token->ipt_ref > 0)
7804 return token->ipt_ref;
7806 data = token->ipt_data;
7809 if ((data != NULL) && (data != (void *)-1)) {
7810 switch (token->ipt_type)
7812 case IPFGENITER_IPF :
7813 (void) ipf_derefrule(softc, (frentry_t **)datap);
7815 case IPFGENITER_IPNAT :
7816 WRITE_ENTER(&softc->ipf_nat);
7817 ipf_nat_rule_deref(softc, (ipnat_t **)datap);
7818 RWLOCK_EXIT(&softc->ipf_nat);
7820 case IPFGENITER_NAT :
7821 ipf_nat_deref(softc, (nat_t **)datap);
7823 case IPFGENITER_STATE :
7824 ipf_state_deref(softc, (ipstate_t **)datap);
7826 case IPFGENITER_FRAG :
7827 ipf_frag_pkt_deref(softc, (ipfr_t **)datap);
7829 case IPFGENITER_NATFRAG :
7830 ipf_frag_nat_deref(softc, (ipfr_t **)datap);
7832 case IPFGENITER_HOSTMAP :
7833 WRITE_ENTER(&softc->ipf_nat);
7834 ipf_nat_hostmapdel(softc, (hostmap_t **)datap);
7835 RWLOCK_EXIT(&softc->ipf_nat);
7838 ipf_lookup_iterderef(softc, token->ipt_type, data);
7843 ipf_token_unlink(softc, token);
7849 /* ------------------------------------------------------------------------ */
7850 /* Function: ipf_nextrule */
7851 /* Returns: frentry_t * - NULL == no more rules, else pointer to next */
7852 /* Parameters: softc(I) - pointer to soft context main structure */
7853 /* fr(I) - pointer to filter rule */
7854 /* out(I) - 1 == out rules, 0 == input rules */
7856 /* Starting with "fr", find the next rule to visit. This includes visiting */
7857 /* the list of rule groups if either fr is NULL (empty list) or it is the */
7858 /* last rule in the list. When walking rule lists, it is either input or */
7859 /* output rules that are returned, never both. */
7860 /* ------------------------------------------------------------------------ */
7862 ipf_nextrule(softc, active, unit, fr, out)
7863 ipf_main_softc_t *softc;
7871 if (fr != NULL && fr->fr_group != -1) {
7872 fg = ipf_findgroup(softc, fr->fr_names + fr->fr_group,
7873 unit, active, NULL);
7877 fg = softc->ipf_groups[unit][active];
7880 while (fg != NULL) {
7881 next = fg->fg_start;
7882 while (next != NULL) {
7884 if (next->fr_flags & FR_OUTQUE)
7886 } else if (next->fr_flags & FR_INQUE) {
7889 next = next->fr_next;
7898 /* ------------------------------------------------------------------------ */
7899 /* Function: ipf_getnextrule */
7900 /* Returns: int - 0 = success, else error */
7901 /* Parameters: softc(I)- pointer to soft context main structure */
7902 /* t(I) - pointer to destination information to resolve */
7903 /* ptr(I) - pointer to ipfobj_t to copyin from user space */
7905 /* This function's first job is to bring in the ipfruleiter_t structure via */
7906 /* the ipfobj_t structure to determine what should be the next rule to */
7907 /* return. Once the ipfruleiter_t has been brought in, it then tries to */
7908 /* find the 'next rule'. This may include searching rule group lists or */
7909 /* just be as simple as looking at the 'next' field in the rule structure. */
7910 /* When we have found the rule to return, increase its reference count and */
7911 /* if we used an existing rule to get here, decrease its reference count. */
7912 /* ------------------------------------------------------------------------ */
7914 ipf_getnextrule(softc, t, ptr)
7915 ipf_main_softc_t *softc;
7919 frentry_t *fr, *next, zero;
7928 if (t == NULL || ptr == NULL) {
7933 error = ipf_inobj(softc, ptr, &obj, &it, IPFOBJ_IPFITER);
7937 if ((it.iri_inout < 0) || (it.iri_inout > 3)) {
7941 if ((it.iri_active != 0) && (it.iri_active != 1)) {
7945 if (it.iri_nrules == 0) {
7949 if (it.iri_rule == NULL) {
7956 if ((it.iri_inout & F_OUT) != 0)
7960 if ((it.iri_inout & F_ACIN) != 0)
7961 unit = IPL_LOGCOUNT;
7965 READ_ENTER(&softc->ipf_mutex);
7967 if (*it.iri_group == '\0') {
7968 if (unit == IPL_LOGCOUNT) {
7969 next = softc->ipf_acct[out][it.iri_active];
7971 next = softc->ipf_rules[out][it.iri_active];
7974 next = ipf_nextrule(softc, it.iri_active,
7977 fg = ipf_findgroup(softc, it.iri_group, unit,
7978 it.iri_active, NULL);
7980 next = fg->fg_start;
7987 next = ipf_nextrule(softc, it.iri_active, unit,
7991 if (next != NULL && next->fr_next != NULL)
7993 else if (ipf_nextrule(softc, it.iri_active, unit, next, out) != NULL)
7999 (void) ipf_derefrule(softc, &fr);
8001 obj.ipfo_type = IPFOBJ_FRENTRY;
8002 dst = (char *)it.iri_rule;
8005 obj.ipfo_size = next->fr_size;
8006 MUTEX_ENTER(&next->fr_lock);
8008 MUTEX_EXIT(&next->fr_lock);
8011 obj.ipfo_size = sizeof(frentry_t);
8012 bzero(&zero, sizeof(zero));
8016 it.iri_rule = predict ? next : NULL;
8018 ipf_token_mark_complete(t);
8020 RWLOCK_EXIT(&softc->ipf_mutex);
8023 error = ipf_outobjk(softc, &obj, next);
8024 if (error == 0 && t->ipt_data != NULL) {
8025 dst += obj.ipfo_size;
8026 if (next->fr_data != NULL) {
8029 if (next->fr_type == FR_T_IPFEXPR)
8030 dobj.ipfo_type = IPFOBJ_IPFEXPR;
8032 dobj.ipfo_type = IPFOBJ_FRIPF;
8033 dobj.ipfo_size = next->fr_dsize;
8034 dobj.ipfo_rev = obj.ipfo_rev;
8035 dobj.ipfo_ptr = dst;
8036 error = ipf_outobjk(softc, &dobj, next->fr_data);
8040 if ((fr != NULL) && (next == &zero))
8041 (void) ipf_derefrule(softc, &fr);
8047 /* ------------------------------------------------------------------------ */
8048 /* Function: ipf_frruleiter */
8049 /* Returns: int - 0 = success, else error */
8050 /* Parameters: softc(I)- pointer to soft context main structure */
8051 /* data(I) - the token type to match */
8052 /* uid(I) - uid owning the token */
8053 /* ptr(I) - context pointer for the token */
8055 /* This function serves as a stepping stone between ipf_ipf_ioctl and */
8056 /* ipf_getnextrule. It's role is to find the right token in the kernel for */
8057 /* the process doing the ioctl and use that to ask for the next rule. */
8058 /* ------------------------------------------------------------------------ */
8060 ipf_frruleiter(softc, data, uid, ctx)
8061 ipf_main_softc_t *softc;
8070 token = ipf_token_find(softc, IPFGENITER_IPF, uid, ctx);
8071 if (token != NULL) {
8072 error = ipf_getnextrule(softc, token, data);
8073 WRITE_ENTER(&softc->ipf_tokens);
8074 ipf_token_deref(softc, token);
8075 RWLOCK_EXIT(&softc->ipf_tokens);
8077 error = ipf_inobj(softc, data, &obj, &it, IPFOBJ_IPFITER);
8081 error = ipf_outobj(softc, data, &it, IPFOBJ_IPFITER);
8088 /* ------------------------------------------------------------------------ */
8089 /* Function: ipf_geniter */
8090 /* Returns: int - 0 = success, else error */
8091 /* Parameters: softc(I) - pointer to soft context main structure */
8092 /* token(I) - pointer to ipftoken_t structure */
8093 /* itp(I) - pointer to iterator data */
8095 /* Decide which iterator function to call using information passed through */
8096 /* the ipfgeniter_t structure at itp. */
8097 /* ------------------------------------------------------------------------ */
8099 ipf_geniter(softc, token, itp)
8100 ipf_main_softc_t *softc;
8106 switch (itp->igi_type)
8108 case IPFGENITER_FRAG :
8109 error = ipf_frag_pkt_next(softc, token, itp);
8121 /* ------------------------------------------------------------------------ */
8122 /* Function: ipf_genericiter */
8123 /* Returns: int - 0 = success, else error */
8124 /* Parameters: softc(I)- pointer to soft context main structure */
8125 /* data(I) - the token type to match */
8126 /* uid(I) - uid owning the token */
8127 /* ptr(I) - context pointer for the token */
8129 /* Handle the SIOCGENITER ioctl for the ipfilter device. The primary role */
8130 /* ------------------------------------------------------------------------ */
8132 ipf_genericiter(softc, data, uid, ctx)
8133 ipf_main_softc_t *softc;
8141 error = ipf_inobj(softc, data, NULL, &iter, IPFOBJ_GENITER);
8145 token = ipf_token_find(softc, iter.igi_type, uid, ctx);
8146 if (token != NULL) {
8147 token->ipt_subtype = iter.igi_type;
8148 error = ipf_geniter(softc, token, &iter);
8149 WRITE_ENTER(&softc->ipf_tokens);
8150 ipf_token_deref(softc, token);
8151 RWLOCK_EXIT(&softc->ipf_tokens);
8161 /* ------------------------------------------------------------------------ */
8162 /* Function: ipf_ipf_ioctl */
8163 /* Returns: int - 0 = success, else error */
8164 /* Parameters: softc(I)- pointer to soft context main structure */
8165 /* data(I) - the token type to match */
8166 /* cmd(I) - the ioctl command number */
8167 /* mode(I) - mode flags for the ioctl */
8168 /* uid(I) - uid owning the token */
8169 /* ptr(I) - context pointer for the token */
8171 /* This function handles all of the ioctl command that are actually isssued */
8172 /* to the /dev/ipl device. */
8173 /* ------------------------------------------------------------------------ */
8175 ipf_ipf_ioctl(softc, data, cmd, mode, uid, ctx)
8176 ipf_main_softc_t *softc;
8190 if (!(mode & FWRITE)) {
8194 error = BCOPYIN(data, &tmp, sizeof(tmp));
8201 WRITE_ENTER(&softc->ipf_global);
8203 if (softc->ipf_running > 0)
8206 error = ipfattach(softc);
8208 softc->ipf_running = 1;
8210 (void) ipfdetach(softc);
8212 if (softc->ipf_running == 1)
8213 error = ipfdetach(softc);
8217 softc->ipf_running = -1;
8219 RWLOCK_EXIT(&softc->ipf_global);
8224 if (!(mode & FWRITE)) {
8230 case SIOCIPFGETNEXT :
8232 error = ipf_ipftune(softc, cmd, (void *)data);
8236 if (!(mode & FWRITE)) {
8240 error = BCOPYIN(data, &softc->ipf_flags,
8241 sizeof(softc->ipf_flags));
8250 error = BCOPYOUT(&softc->ipf_flags, data,
8251 sizeof(softc->ipf_flags));
8259 error = ipf_resolvefunc(softc, (void *)data);
8266 if (!(mode & FWRITE)) {
8270 error = frrequest(softc, IPL_LOGIPF, cmd, (caddr_t)data,
8271 softc->ipf_active, 1);
8278 if (!(mode & FWRITE)) {
8282 error = frrequest(softc, IPL_LOGIPF, cmd, (caddr_t)data,
8283 1 - softc->ipf_active, 1);
8288 if (!(mode & FWRITE)) {
8292 WRITE_ENTER(&softc->ipf_mutex);
8293 error = BCOPYOUT(&softc->ipf_active, data,
8294 sizeof(softc->ipf_active));
8299 softc->ipf_active = 1 - softc->ipf_active;
8301 RWLOCK_EXIT(&softc->ipf_mutex);
8306 error = ipf_inobj(softc, (void *)data, &obj, &fio,
8310 ipf_getstat(softc, &fio, obj.ipfo_rev);
8311 error = ipf_outobj(softc, (void *)data, &fio, IPFOBJ_IPFSTAT);
8315 if (!(mode & FWRITE)) {
8319 error = ipf_zerostats(softc, (caddr_t)data);
8323 if (!(mode & FWRITE)) {
8327 error = BCOPYIN(data, &tmp, sizeof(tmp));
8329 tmp = ipf_flush(softc, IPL_LOGIPF, tmp);
8330 error = BCOPYOUT(&tmp, data, sizeof(tmp));
8344 if (!(mode & FWRITE)) {
8348 error = BCOPYIN(data, &tmp, sizeof(tmp));
8350 tmp = ipf_flush(softc, IPL_LOGIPF, tmp);
8351 error = BCOPYOUT(&tmp, data, sizeof(tmp));
8365 if (!(mode & FWRITE)) {
8369 error = BCOPYIN(data, &tmp, sizeof(tmp));
8371 ipf_state_setlock(softc->ipf_state_soft, tmp);
8372 ipf_nat_setlock(softc->ipf_nat_soft, tmp);
8373 ipf_frag_setlock(softc->ipf_frag_soft, tmp);
8374 ipf_auth_setlock(softc->ipf_auth_soft, tmp);
8384 if (!(mode & FWRITE)) {
8388 tmp = ipf_log_clear(softc, IPL_LOGIPF);
8389 error = BCOPYOUT(&tmp, data, sizeof(tmp));
8396 #endif /* IPFILTER_LOG */
8399 if (!(mode & FWRITE)) {
8403 WRITE_ENTER(&softc->ipf_global);
8404 #if (defined(MENTAT) && defined(_KERNEL)) && !defined(INSTANCES)
8407 ipf_sync(softc, NULL);
8410 RWLOCK_EXIT(&softc->ipf_global);
8416 error = ipf_outobj(softc, (void *)data,
8417 ipf_frag_stats(softc->ipf_frag_soft),
8423 tmp = ipf_log_bytesused(softc, IPL_LOGIPF);
8424 error = BCOPYOUT(&tmp, data, sizeof(tmp));
8430 error = ipf_frruleiter(softc, data, uid, ctx);
8436 error = ipf_genericiter(softc, data, uid, ctx);
8440 case SIOCIPFDELTOK :
8441 error = BCOPYIN(data, &tmp, sizeof(tmp));
8444 error = ipf_token_del(softc, tmp, uid, ctx);
8459 /* ------------------------------------------------------------------------ */
8460 /* Function: ipf_decaps */
8461 /* Returns: int - -1 == decapsulation failed, else bit mask of */
8462 /* flags indicating packet filtering decision. */
8463 /* Parameters: fin(I) - pointer to packet information */
8464 /* pass(I) - IP protocol version to match */
8465 /* l5proto(I) - layer 5 protocol to decode UDP data as. */
8467 /* This function is called for packets that are wrapt up in other packets, */
8468 /* for example, an IP packet that is the entire data segment for another IP */
8469 /* packet. If the basic constraints for this are satisfied, change the */
8470 /* buffer to point to the start of the inner packet and start processing */
8471 /* rules belonging to the head group this rule specifies. */
8472 /* ------------------------------------------------------------------------ */
8474 ipf_decaps(fin, pass, l5proto)
8479 fr_info_t fin2, *fino = NULL;
8485 if ((fin->fin_flx & FI_COALESCE) == 0)
8486 if (ipf_coalesce(fin) == -1)
8490 hlen = fin->fin_hlen;
8496 * In this case, the specific protocol being decapsulated
8497 * inside UDP frames comes from the rule.
8499 nh = fin->fin_fr->fr_icode;
8502 case IPPROTO_GRE : /* 47 */
8503 bcopy(fin->fin_dp, (char *)&gre, sizeof(gre));
8504 hlen += sizeof(grehdr_t);
8505 if (gre.gr_R|gre.gr_s)
8517 * If the routing options flag is set, validate that it is
8518 * there and bounce over it.
8521 /* This is really heavy weight and lots of room for error, */
8522 /* so for now, put it off and get the simple stuff right. */
8524 u_char off, len, *s;
8531 aplen = fin->fin_plen - hlen;
8533 af = (s[0] << 8) | s[1];
8538 if (af == 0 && len == 0) {
8549 hlen = s - (u_char *)fin->fin_dp;
8555 case IPPROTO_IPIP : /* 4 */
8560 default : /* Includes ESP, AH is special for IPv4 */
8573 bcopy((char *)fin, (char *)&fin2, sizeof(fin2));
8577 #if defined(MENTAT) && defined(_KERNEL)
8583 fin->fin_plen -= elen;
8585 ip = (ip_t *)((char *)fin->fin_ip + elen);
8588 * Make sure we have at least enough data for the network layer
8592 hlen = IP_HL(ip) << 2;
8594 else if (IP_V(ip) == 6)
8595 hlen = sizeof(ip6_t);
8600 if (fin->fin_plen < hlen)
8603 fin->fin_dp = (char *)ip + hlen;
8605 if (IP_V(ip) == 4) {
8607 * Perform IPv4 header checksum validation.
8609 if (ipf_cksum((u_short *)ip, hlen))
8613 if (ipf_makefrip(hlen, ip, fin) == -1) {
8616 #if defined(MENTAT) && defined(_KERNEL)
8624 DT1(frb_decapfrip, fr_info_t *, fin);
8625 pass &= ~FR_CMDMASK;
8626 pass |= FR_BLOCK|FR_QUICK;
8627 fin->fin_reason = FRB_DECAPFRIP;
8631 pass = ipf_scanlist(fin, pass);
8634 * Copy the packet filter "result" fields out of the fr_info_t struct
8635 * that is local to the decapsulation processing and back into the
8636 * one we were called with.
8638 fino->fin_flx = fin->fin_flx;
8639 fino->fin_rev = fin->fin_rev;
8640 fino->fin_icode = fin->fin_icode;
8641 fino->fin_rule = fin->fin_rule;
8642 (void) strncpy(fino->fin_group, fin->fin_group, FR_GROUPLEN);
8643 fino->fin_fr = fin->fin_fr;
8644 fino->fin_error = fin->fin_error;
8645 fino->fin_mp = fin->fin_mp;
8646 fino->fin_m = fin->fin_m;
8649 #if defined(MENTAT) && defined(_KERNEL)
8660 /* ------------------------------------------------------------------------ */
8661 /* Function: ipf_matcharray_load */
8662 /* Returns: int - 0 = success, else error */
8663 /* Parameters: softc(I) - pointer to soft context main structure */
8664 /* data(I) - pointer to ioctl data */
8665 /* objp(I) - ipfobj_t structure to load data into */
8666 /* arrayptr(I) - pointer to location to store array pointer */
8668 /* This function loads in a mathing array through the ipfobj_t struct that */
8669 /* describes it. Sanity checking and array size limitations are enforced */
8670 /* in this function to prevent userspace from trying to load in something */
8671 /* that is insanely big. Once the size of the array is known, the memory */
8672 /* required is malloc'd and returned through changing *arrayptr. The */
8673 /* contents of the array are verified before returning. Only in the event */
8674 /* of a successful call is the caller required to free up the malloc area. */
8675 /* ------------------------------------------------------------------------ */
8677 ipf_matcharray_load(softc, data, objp, arrayptr)
8678 ipf_main_softc_t *softc;
8683 int arraysize, *array, error;
8687 error = BCOPYIN(data, objp, sizeof(*objp));
8693 if (objp->ipfo_type != IPFOBJ_IPFEXPR) {
8698 if (((objp->ipfo_size & 3) != 0) || (objp->ipfo_size == 0) ||
8699 (objp->ipfo_size > 1024)) {
8704 arraysize = objp->ipfo_size * sizeof(*array);
8705 KMALLOCS(array, int *, arraysize);
8706 if (array == NULL) {
8711 error = COPYIN(objp->ipfo_ptr, array, arraysize);
8713 KFREES(array, arraysize);
8718 if (ipf_matcharray_verify(array, arraysize) != 0) {
8719 KFREES(array, arraysize);
8729 /* ------------------------------------------------------------------------ */
8730 /* Function: ipf_matcharray_verify */
8732 /* Parameters: array(I) - pointer to matching array */
8733 /* arraysize(I) - number of elements in the array */
8735 /* Verify the contents of a matching array by stepping through each element */
8736 /* in it. The actual commands in the array are not verified for */
8737 /* correctness, only that all of the sizes are correctly within limits. */
8738 /* ------------------------------------------------------------------------ */
8740 ipf_matcharray_verify(array, arraysize)
8741 int *array, arraysize;
8743 int i, nelem, maxidx;
8746 nelem = arraysize / sizeof(*array);
8749 * Currently, it makes no sense to have an array less than 6
8750 * elements long - the initial size at the from, a single operation
8751 * (minimum 4 in length) and a trailer, for a total of 6.
8753 if ((array[0] < 6) || (arraysize < 24) || (arraysize > 4096)) {
8758 * Verify the size of data pointed to by array with how long
8759 * the array claims to be itself.
8761 if (array[0] * sizeof(*array) != arraysize) {
8767 * The last opcode in this array should be an IPF_EXP_END.
8769 if (array[maxidx] != IPF_EXP_END) {
8773 for (i = 1; i < maxidx; ) {
8774 e = (ipfexp_t *)(array + i);
8777 * The length of the bits to check must be at least 1
8778 * (or else there is nothing to comapre with!) and it
8779 * cannot exceed the length of the data present.
8781 if ((e->ipfe_size < 1 ) ||
8782 (e->ipfe_size + i > maxidx)) {
8791 /* ------------------------------------------------------------------------ */
8792 /* Function: ipf_fr_matcharray */
8793 /* Returns: int - 0 = match failed, else positive match */
8794 /* Parameters: fin(I) - pointer to packet information */
8795 /* array(I) - pointer to matching array */
8797 /* This function is used to apply a matching array against a packet and */
8798 /* return an indication of whether or not the packet successfully matches */
8799 /* all of the commands in it. */
8800 /* ------------------------------------------------------------------------ */
8802 ipf_fr_matcharray(fin, array)
8806 int i, n, *x, rv, p;
8813 for (; n > 0; x += 3 + x[3], rv = 0) {
8815 if (e->ipfe_cmd == IPF_EXP_END)
8820 * The upper 16 bits currently store the protocol value.
8821 * This is currently used with TCP and UDP port compares and
8822 * allows "tcp.port = 80" without requiring an explicit
8823 " "ip.pr = tcp" first.
8825 p = e->ipfe_cmd >> 16;
8826 if ((p != 0) && (p != fin->fin_p))
8829 switch (e->ipfe_cmd)
8831 case IPF_EXP_IP_PR :
8832 for (i = 0; !rv && i < e->ipfe_narg; i++) {
8833 rv |= (fin->fin_p == e->ipfe_arg0[i]);
8837 case IPF_EXP_IP_SRCADDR :
8838 if (fin->fin_v != 4)
8840 for (i = 0; !rv && i < e->ipfe_narg; i++) {
8841 rv |= ((fin->fin_saddr &
8842 e->ipfe_arg0[i * 2 + 1]) ==
8843 e->ipfe_arg0[i * 2]);
8847 case IPF_EXP_IP_DSTADDR :
8848 if (fin->fin_v != 4)
8850 for (i = 0; !rv && i < e->ipfe_narg; i++) {
8851 rv |= ((fin->fin_daddr &
8852 e->ipfe_arg0[i * 2 + 1]) ==
8853 e->ipfe_arg0[i * 2]);
8857 case IPF_EXP_IP_ADDR :
8858 if (fin->fin_v != 4)
8860 for (i = 0; !rv && i < e->ipfe_narg; i++) {
8861 rv |= ((fin->fin_saddr &
8862 e->ipfe_arg0[i * 2 + 1]) ==
8863 e->ipfe_arg0[i * 2]) ||
8865 e->ipfe_arg0[i * 2 + 1]) ==
8866 e->ipfe_arg0[i * 2]);
8871 case IPF_EXP_IP6_SRCADDR :
8872 if (fin->fin_v != 6)
8874 for (i = 0; !rv && i < e->ipfe_narg; i++) {
8875 rv |= IP6_MASKEQ(&fin->fin_src6,
8876 &e->ipfe_arg0[i * 8 + 4],
8877 &e->ipfe_arg0[i * 8]);
8881 case IPF_EXP_IP6_DSTADDR :
8882 if (fin->fin_v != 6)
8884 for (i = 0; !rv && i < e->ipfe_narg; i++) {
8885 rv |= IP6_MASKEQ(&fin->fin_dst6,
8886 &e->ipfe_arg0[i * 8 + 4],
8887 &e->ipfe_arg0[i * 8]);
8891 case IPF_EXP_IP6_ADDR :
8892 if (fin->fin_v != 6)
8894 for (i = 0; !rv && i < e->ipfe_narg; i++) {
8895 rv |= IP6_MASKEQ(&fin->fin_src6,
8896 &e->ipfe_arg0[i * 8 + 4],
8897 &e->ipfe_arg0[i * 8]) ||
8898 IP6_MASKEQ(&fin->fin_dst6,
8899 &e->ipfe_arg0[i * 8 + 4],
8900 &e->ipfe_arg0[i * 8]);
8905 case IPF_EXP_UDP_PORT :
8906 case IPF_EXP_TCP_PORT :
8907 for (i = 0; !rv && i < e->ipfe_narg; i++) {
8908 rv |= (fin->fin_sport == e->ipfe_arg0[i]) ||
8909 (fin->fin_dport == e->ipfe_arg0[i]);
8913 case IPF_EXP_UDP_SPORT :
8914 case IPF_EXP_TCP_SPORT :
8915 for (i = 0; !rv && i < e->ipfe_narg; i++) {
8916 rv |= (fin->fin_sport == e->ipfe_arg0[i]);
8920 case IPF_EXP_UDP_DPORT :
8921 case IPF_EXP_TCP_DPORT :
8922 for (i = 0; !rv && i < e->ipfe_narg; i++) {
8923 rv |= (fin->fin_dport == e->ipfe_arg0[i]);
8927 case IPF_EXP_TCP_FLAGS :
8928 for (i = 0; !rv && i < e->ipfe_narg; i++) {
8929 rv |= ((fin->fin_tcpf &
8930 e->ipfe_arg0[i * 2 + 1]) ==
8931 e->ipfe_arg0[i * 2]);
8945 /* ------------------------------------------------------------------------ */
8946 /* Function: ipf_queueflush */
8947 /* Returns: int - number of entries flushed (0 = none) */
8948 /* Parameters: softc(I) - pointer to soft context main structure */
8949 /* deletefn(I) - function to call to delete entry */
8950 /* ipfqs(I) - top of the list of ipf internal queues */
8951 /* userqs(I) - top of the list of user defined timeouts */
8953 /* This fucntion gets called when the state/NAT hash tables fill up and we */
8954 /* need to try a bit harder to free up some space. The algorithm used here */
8955 /* split into two parts but both halves have the same goal: to reduce the */
8956 /* number of connections considered to be "active" to the low watermark. */
8957 /* There are two steps in doing this: */
8958 /* 1) Remove any TCP connections that are already considered to be "closed" */
8959 /* but have not yet been removed from the state table. The two states */
8960 /* TCPS_TIME_WAIT and TCPS_CLOSED are considered to be the perfect */
8961 /* candidates for this style of removal. If freeing up entries in */
8962 /* CLOSED or both CLOSED and TIME_WAIT brings us to the low watermark, */
8963 /* we do not go on to step 2. */
8965 /* 2) Look for the oldest entries on each timeout queue and free them if */
8966 /* they are within the given window we are considering. Where the */
8967 /* window starts and the steps taken to increase its size depend upon */
8968 /* how long ipf has been running (ipf_ticks.) Anything modified in the */
8969 /* last 30 seconds is not touched. */
8971 /* die ipf_ticks 30*1.5 1800*1.5 | 43200*1.5 */
8973 /* future <--+----------+--------+-----------+-----+-----+-----------> past */
8974 /* now \_int=30s_/ \_int=1hr_/ \_int=12hr */
8976 /* Points to note: */
8977 /* - tqe_die is the time, in the future, when entries die. */
8978 /* - tqe_die - ipf_ticks is how long left the connection has to live in ipf */
8980 /* - tqe_touched is when the entry was last used by NAT/state */
8981 /* - the closer tqe_touched is to ipf_ticks, the further tqe_die will be */
8982 /* ipf_ticks any given timeout queue and vice versa. */
8983 /* - both tqe_die and tqe_touched increase over time */
8984 /* - timeout queues are sorted with the highest value of tqe_die at the */
8985 /* bottom and therefore the smallest values of each are at the top */
8986 /* - the pointer passed in as ipfqs should point to an array of timeout */
8987 /* queues representing each of the TCP states */
8989 /* We start by setting up a maximum range to scan for things to move of */
8990 /* iend (newest) to istart (oldest) in chunks of "interval". If nothing is */
8991 /* found in that range, "interval" is adjusted (so long as it isn't 30) and */
8992 /* we start again with a new value for "iend" and "istart". This is */
8993 /* continued until we either finish the scan of 30 second intervals or the */
8994 /* low water mark is reached. */
8995 /* ------------------------------------------------------------------------ */
8997 ipf_queueflush(softc, deletefn, ipfqs, userqs, activep, size, low)
8998 ipf_main_softc_t *softc;
8999 ipftq_delete_fn_t deletefn;
9000 ipftq_t *ipfqs, *userqs;
9004 u_long interval, istart, iend;
9005 ipftq_t *ifq, *ifqnext;
9006 ipftqent_t *tqe, *tqn;
9009 for (tqn = ipfqs[IPF_TCPS_CLOSED].ifq_head; ((tqe = tqn) != NULL); ) {
9010 tqn = tqe->tqe_next;
9011 if ((*deletefn)(softc, tqe->tqe_parent) == 0)
9014 if ((*activep * 100 / size) > low) {
9015 for (tqn = ipfqs[IPF_TCPS_TIME_WAIT].ifq_head;
9016 ((tqe = tqn) != NULL); ) {
9017 tqn = tqe->tqe_next;
9018 if ((*deletefn)(softc, tqe->tqe_parent) == 0)
9023 if ((*activep * 100 / size) <= low) {
9028 * NOTE: Use of "* 15 / 10" is required here because if "* 1.5" is
9029 * used then the operations are upgraded to floating point
9030 * and kernels don't like floating point...
9032 if (softc->ipf_ticks > IPF_TTLVAL(43200 * 15 / 10)) {
9033 istart = IPF_TTLVAL(86400 * 4);
9034 interval = IPF_TTLVAL(43200);
9035 } else if (softc->ipf_ticks > IPF_TTLVAL(1800 * 15 / 10)) {
9036 istart = IPF_TTLVAL(43200);
9037 interval = IPF_TTLVAL(1800);
9038 } else if (softc->ipf_ticks > IPF_TTLVAL(30 * 15 / 10)) {
9039 istart = IPF_TTLVAL(1800);
9040 interval = IPF_TTLVAL(30);
9044 if (istart > softc->ipf_ticks) {
9045 if (softc->ipf_ticks - interval < interval)
9048 istart = (softc->ipf_ticks / interval) * interval;
9051 iend = softc->ipf_ticks - interval;
9053 while ((*activep * 100 / size) > low) {
9056 try = softc->ipf_ticks - istart;
9058 for (ifq = ipfqs; ifq != NULL; ifq = ifq->ifq_next) {
9059 for (tqn = ifq->ifq_head; ((tqe = tqn) != NULL); ) {
9060 if (try < tqe->tqe_touched)
9062 tqn = tqe->tqe_next;
9063 if ((*deletefn)(softc, tqe->tqe_parent) == 0)
9068 for (ifq = userqs; ifq != NULL; ifq = ifqnext) {
9069 ifqnext = ifq->ifq_next;
9071 for (tqn = ifq->ifq_head; ((tqe = tqn) != NULL); ) {
9072 if (try < tqe->tqe_touched)
9074 tqn = tqe->tqe_next;
9075 if ((*deletefn)(softc, tqe->tqe_parent) == 0)
9081 if (interval == IPF_TTLVAL(43200)) {
9082 interval = IPF_TTLVAL(1800);
9083 } else if (interval == IPF_TTLVAL(1800)) {
9084 interval = IPF_TTLVAL(30);
9088 if (interval >= softc->ipf_ticks)
9091 iend = softc->ipf_ticks - interval;
9100 /* ------------------------------------------------------------------------ */
9101 /* Function: ipf_deliverlocal */
9102 /* Returns: int - 1 = local address, 0 = non-local address */
9103 /* Parameters: softc(I) - pointer to soft context main structure */
9104 /* ipversion(I) - IP protocol version (4 or 6) */
9105 /* ifp(I) - network interface pointer */
9106 /* ipaddr(I) - IPv4/6 destination address */
9108 /* This fucntion is used to determine in the address "ipaddr" belongs to */
9109 /* the network interface represented by ifp. */
9110 /* ------------------------------------------------------------------------ */
9112 ipf_deliverlocal(softc, ipversion, ifp, ipaddr)
9113 ipf_main_softc_t *softc;
9121 if (ipversion == 4) {
9122 if (ipf_ifpaddr(softc, 4, FRI_NORMAL, ifp, &addr, NULL) == 0) {
9123 if (addr.in4.s_addr == ipaddr->in4.s_addr)
9128 } else if (ipversion == 6) {
9129 if (ipf_ifpaddr(softc, 6, FRI_NORMAL, ifp, &addr, NULL) == 0) {
9130 if (IP6_EQ(&addr, ipaddr))
9140 /* ------------------------------------------------------------------------ */
9141 /* Function: ipf_settimeout */
9142 /* Returns: int - 0 = success, -1 = failure */
9143 /* Parameters: softc(I) - pointer to soft context main structure */
9144 /* t(I) - pointer to tuneable array entry */
9145 /* p(I) - pointer to values passed in to apply */
9147 /* This function is called to set the timeout values for each distinct */
9148 /* queue timeout that is available. When called, it calls into both the */
9149 /* state and NAT code, telling them to update their timeout queues. */
9150 /* ------------------------------------------------------------------------ */
9152 ipf_settimeout(softc, t, p)
9153 struct ipf_main_softc_s *softc;
9159 * ipf_interror should be set by the functions called here, not
9160 * by this function - it's just a middle man.
9162 if (ipf_state_settimeout(softc, t, p) == -1)
9164 if (ipf_nat_settimeout(softc, t, p) == -1)
9170 /* ------------------------------------------------------------------------ */
9171 /* Function: ipf_apply_timeout */
9172 /* Returns: int - 0 = success, -1 = failure */
9173 /* Parameters: head(I) - pointer to tuneable array entry */
9174 /* seconds(I) - pointer to values passed in to apply */
9176 /* This function applies a timeout of "seconds" to the timeout queue that */
9177 /* is pointed to by "head". All entries on this list have an expiration */
9178 /* set to be the current tick value of ipf plus the ttl. Given that this */
9179 /* function should only be called when the delta is non-zero, the task is */
9180 /* to walk the entire list and apply the change. The sort order will not */
9181 /* change. The only catch is that this is O(n) across the list, so if the */
9182 /* queue has lots of entries (10s of thousands or 100s of thousands), it */
9183 /* could take a relatively long time to work through them all. */
9184 /* ------------------------------------------------------------------------ */
9186 ipf_apply_timeout(head, seconds)
9190 u_int oldtimeout, newtimeout;
9194 MUTEX_ENTER(&head->ifq_lock);
9195 oldtimeout = head->ifq_ttl;
9196 newtimeout = IPF_TTLVAL(seconds);
9197 delta = oldtimeout - newtimeout;
9199 head->ifq_ttl = newtimeout;
9201 for (tqe = head->ifq_head; tqe != NULL; tqe = tqe->tqe_next) {
9202 tqe->tqe_die += delta;
9204 MUTEX_EXIT(&head->ifq_lock);
9208 /* ------------------------------------------------------------------------ */
9209 /* Function: ipf_settimeout_tcp */
9210 /* Returns: int - 0 = successfully applied, -1 = failed */
9211 /* Parameters: t(I) - pointer to tuneable to change */
9212 /* p(I) - pointer to new timeout information */
9213 /* tab(I) - pointer to table of TCP queues */
9215 /* This function applies the new timeout (p) to the TCP tunable (t) and */
9216 /* updates all of the entries on the relevant timeout queue by calling */
9217 /* ipf_apply_timeout(). */
9218 /* ------------------------------------------------------------------------ */
9220 ipf_settimeout_tcp(t, p, tab)
9225 if (!strcmp(t->ipft_name, "tcp_idle_timeout") ||
9226 !strcmp(t->ipft_name, "tcp_established")) {
9227 ipf_apply_timeout(&tab[IPF_TCPS_ESTABLISHED], p->ipftu_int);
9228 } else if (!strcmp(t->ipft_name, "tcp_close_wait")) {
9229 ipf_apply_timeout(&tab[IPF_TCPS_CLOSE_WAIT], p->ipftu_int);
9230 } else if (!strcmp(t->ipft_name, "tcp_last_ack")) {
9231 ipf_apply_timeout(&tab[IPF_TCPS_LAST_ACK], p->ipftu_int);
9232 } else if (!strcmp(t->ipft_name, "tcp_timeout")) {
9233 ipf_apply_timeout(&tab[IPF_TCPS_LISTEN], p->ipftu_int);
9234 ipf_apply_timeout(&tab[IPF_TCPS_HALF_ESTAB], p->ipftu_int);
9235 ipf_apply_timeout(&tab[IPF_TCPS_CLOSING], p->ipftu_int);
9236 } else if (!strcmp(t->ipft_name, "tcp_listen")) {
9237 ipf_apply_timeout(&tab[IPF_TCPS_LISTEN], p->ipftu_int);
9238 } else if (!strcmp(t->ipft_name, "tcp_half_established")) {
9239 ipf_apply_timeout(&tab[IPF_TCPS_HALF_ESTAB], p->ipftu_int);
9240 } else if (!strcmp(t->ipft_name, "tcp_closing")) {
9241 ipf_apply_timeout(&tab[IPF_TCPS_CLOSING], p->ipftu_int);
9242 } else if (!strcmp(t->ipft_name, "tcp_syn_received")) {
9243 ipf_apply_timeout(&tab[IPF_TCPS_SYN_RECEIVED], p->ipftu_int);
9244 } else if (!strcmp(t->ipft_name, "tcp_syn_sent")) {
9245 ipf_apply_timeout(&tab[IPF_TCPS_SYN_SENT], p->ipftu_int);
9246 } else if (!strcmp(t->ipft_name, "tcp_closed")) {
9247 ipf_apply_timeout(&tab[IPF_TCPS_CLOSED], p->ipftu_int);
9248 } else if (!strcmp(t->ipft_name, "tcp_half_closed")) {
9249 ipf_apply_timeout(&tab[IPF_TCPS_CLOSED], p->ipftu_int);
9250 } else if (!strcmp(t->ipft_name, "tcp_time_wait")) {
9251 ipf_apply_timeout(&tab[IPF_TCPS_TIME_WAIT], p->ipftu_int);
9254 * ipf_interror isn't set here because it should be set
9255 * by whatever called this function.
9263 /* ------------------------------------------------------------------------ */
9264 /* Function: ipf_main_soft_create */
9265 /* Returns: NULL = failure, else success */
9266 /* Parameters: arg(I) - pointer to soft context structure if already allocd */
9268 /* Create the foundation soft context structure. In circumstances where it */
9269 /* is not required to dynamically allocate the context, a pointer can be */
9270 /* passed in (rather than NULL) to a structure to be initialised. */
9271 /* The main thing of interest is that a number of locks are initialised */
9272 /* here instead of in the where might be expected - in the relevant create */
9273 /* function elsewhere. This is done because the current locking design has */
9274 /* some areas where these locks are used outside of their module. */
9275 /* Possibly the most important exercise that is done here is setting of all */
9276 /* the timeout values, allowing them to be changed before init(). */
9277 /* ------------------------------------------------------------------------ */
9279 ipf_main_soft_create(arg)
9282 ipf_main_softc_t *softc;
9285 KMALLOC(softc, ipf_main_softc_t *);
9292 bzero((char *)softc, sizeof(*softc));
9295 * This serves as a flag as to whether or not the softc should be
9296 * free'd when _destroy is called.
9298 softc->ipf_dynamic_softc = (arg == NULL) ? 1 : 0;
9300 softc->ipf_tuners = ipf_tune_array_copy(softc,
9301 sizeof(ipf_main_tuneables),
9302 ipf_main_tuneables);
9303 if (softc->ipf_tuners == NULL) {
9304 ipf_main_soft_destroy(softc);
9308 MUTEX_INIT(&softc->ipf_rw, "ipf rw mutex");
9309 MUTEX_INIT(&softc->ipf_timeoutlock, "ipf timeout lock");
9310 RWLOCK_INIT(&softc->ipf_global, "ipf filter load/unload mutex");
9311 RWLOCK_INIT(&softc->ipf_mutex, "ipf filter rwlock");
9312 RWLOCK_INIT(&softc->ipf_tokens, "ipf token rwlock");
9313 RWLOCK_INIT(&softc->ipf_state, "ipf state rwlock");
9314 RWLOCK_INIT(&softc->ipf_nat, "ipf IP NAT rwlock");
9315 RWLOCK_INIT(&softc->ipf_poolrw, "ipf pool rwlock");
9316 RWLOCK_INIT(&softc->ipf_frag, "ipf frag rwlock");
9318 softc->ipf_token_head = NULL;
9319 softc->ipf_token_tail = &softc->ipf_token_head;
9321 softc->ipf_tcpidletimeout = FIVE_DAYS;
9322 softc->ipf_tcpclosewait = IPF_TTLVAL(2 * TCP_MSL);
9323 softc->ipf_tcplastack = IPF_TTLVAL(30);
9324 softc->ipf_tcptimewait = IPF_TTLVAL(2 * TCP_MSL);
9325 softc->ipf_tcptimeout = IPF_TTLVAL(2 * TCP_MSL);
9326 softc->ipf_tcpsynsent = IPF_TTLVAL(2 * TCP_MSL);
9327 softc->ipf_tcpsynrecv = IPF_TTLVAL(2 * TCP_MSL);
9328 softc->ipf_tcpclosed = IPF_TTLVAL(30);
9329 softc->ipf_tcphalfclosed = IPF_TTLVAL(2 * 3600);
9330 softc->ipf_udptimeout = IPF_TTLVAL(120);
9331 softc->ipf_udpacktimeout = IPF_TTLVAL(12);
9332 softc->ipf_icmptimeout = IPF_TTLVAL(60);
9333 softc->ipf_icmpacktimeout = IPF_TTLVAL(6);
9334 softc->ipf_iptimeout = IPF_TTLVAL(60);
9336 #if defined(IPFILTER_DEFAULT_BLOCK)
9337 softc->ipf_pass = FR_BLOCK|FR_NOMATCH;
9339 softc->ipf_pass = (IPF_DEFAULT_PASS)|FR_NOMATCH;
9341 softc->ipf_minttl = 4;
9342 softc->ipf_icmpminfragmtu = 68;
9343 softc->ipf_flags = IPF_LOGGING;
9348 /* ------------------------------------------------------------------------ */
9349 /* Function: ipf_main_soft_init */
9350 /* Returns: 0 = success, -1 = failure */
9351 /* Parameters: softc(I) - pointer to soft context main structure */
9353 /* A null-op function that exists as a placeholder so that the flow in */
9354 /* other functions is obvious. */
9355 /* ------------------------------------------------------------------------ */
9358 ipf_main_soft_init(softc)
9359 ipf_main_softc_t *softc;
9365 /* ------------------------------------------------------------------------ */
9366 /* Function: ipf_main_soft_destroy */
9368 /* Parameters: softc(I) - pointer to soft context main structure */
9370 /* Undo everything that we did in ipf_main_soft_create. */
9372 /* The most important check that needs to be made here is whether or not */
9373 /* the structure was allocated by ipf_main_soft_create() by checking what */
9374 /* value is stored in ipf_dynamic_main. */
9375 /* ------------------------------------------------------------------------ */
9378 ipf_main_soft_destroy(softc)
9379 ipf_main_softc_t *softc;
9382 RW_DESTROY(&softc->ipf_frag);
9383 RW_DESTROY(&softc->ipf_poolrw);
9384 RW_DESTROY(&softc->ipf_nat);
9385 RW_DESTROY(&softc->ipf_state);
9386 RW_DESTROY(&softc->ipf_tokens);
9387 RW_DESTROY(&softc->ipf_mutex);
9388 RW_DESTROY(&softc->ipf_global);
9389 MUTEX_DESTROY(&softc->ipf_timeoutlock);
9390 MUTEX_DESTROY(&softc->ipf_rw);
9392 if (softc->ipf_tuners != NULL) {
9393 KFREES(softc->ipf_tuners, sizeof(ipf_main_tuneables));
9395 if (softc->ipf_dynamic_softc == 1) {
9401 /* ------------------------------------------------------------------------ */
9402 /* Function: ipf_main_soft_fini */
9403 /* Returns: 0 = success, -1 = failure */
9404 /* Parameters: softc(I) - pointer to soft context main structure */
9406 /* Clean out the rules which have been added since _init was last called, */
9407 /* the only dynamic part of the mainline. */
9408 /* ------------------------------------------------------------------------ */
9410 ipf_main_soft_fini(softc)
9411 ipf_main_softc_t *softc;
9413 (void) ipf_flush(softc, IPL_LOGIPF, FR_INQUE|FR_OUTQUE|FR_INACTIVE);
9414 (void) ipf_flush(softc, IPL_LOGIPF, FR_INQUE|FR_OUTQUE);
9415 (void) ipf_flush(softc, IPL_LOGCOUNT, FR_INQUE|FR_OUTQUE|FR_INACTIVE);
9416 (void) ipf_flush(softc, IPL_LOGCOUNT, FR_INQUE|FR_OUTQUE);
9422 /* ------------------------------------------------------------------------ */
9423 /* Function: ipf_main_load */
9424 /* Returns: 0 = success, -1 = failure */
9425 /* Parameters: none */
9427 /* Handle global initialisation that needs to be done for the base part of */
9428 /* IPFilter. At present this just amounts to initialising some ICMP lookup */
9429 /* arrays that get used by the state/NAT code. */
9430 /* ------------------------------------------------------------------------ */
9436 /* fill icmp reply type table */
9437 for (i = 0; i <= ICMP_MAXTYPE; i++)
9438 icmpreplytype4[i] = -1;
9439 icmpreplytype4[ICMP_ECHO] = ICMP_ECHOREPLY;
9440 icmpreplytype4[ICMP_TSTAMP] = ICMP_TSTAMPREPLY;
9441 icmpreplytype4[ICMP_IREQ] = ICMP_IREQREPLY;
9442 icmpreplytype4[ICMP_MASKREQ] = ICMP_MASKREPLY;
9445 /* fill icmp reply type table */
9446 for (i = 0; i <= ICMP6_MAXTYPE; i++)
9447 icmpreplytype6[i] = -1;
9448 icmpreplytype6[ICMP6_ECHO_REQUEST] = ICMP6_ECHO_REPLY;
9449 icmpreplytype6[ICMP6_MEMBERSHIP_QUERY] = ICMP6_MEMBERSHIP_REPORT;
9450 icmpreplytype6[ICMP6_NI_QUERY] = ICMP6_NI_REPLY;
9451 icmpreplytype6[ND_ROUTER_SOLICIT] = ND_ROUTER_ADVERT;
9452 icmpreplytype6[ND_NEIGHBOR_SOLICIT] = ND_NEIGHBOR_ADVERT;
9459 /* ------------------------------------------------------------------------ */
9460 /* Function: ipf_main_unload */
9461 /* Returns: 0 = success, -1 = failure */
9462 /* Parameters: none */
9464 /* A null-op function that exists as a placeholder so that the flow in */
9465 /* other functions is obvious. */
9466 /* ------------------------------------------------------------------------ */
9474 /* ------------------------------------------------------------------------ */
9475 /* Function: ipf_load_all */
9476 /* Returns: 0 = success, -1 = failure */
9477 /* Parameters: none */
9479 /* Work through all of the subsystems inside IPFilter and call the load */
9480 /* function for each in an order that won't lead to a crash :) */
9481 /* ------------------------------------------------------------------------ */
9485 if (ipf_main_load() == -1)
9488 if (ipf_state_main_load() == -1)
9491 if (ipf_nat_main_load() == -1)
9494 if (ipf_frag_main_load() == -1)
9497 if (ipf_auth_main_load() == -1)
9500 if (ipf_proxy_main_load() == -1)
9507 /* ------------------------------------------------------------------------ */
9508 /* Function: ipf_unload_all */
9509 /* Returns: 0 = success, -1 = failure */
9510 /* Parameters: none */
9512 /* Work through all of the subsystems inside IPFilter and call the unload */
9513 /* function for each in an order that won't lead to a crash :) */
9514 /* ------------------------------------------------------------------------ */
9518 if (ipf_proxy_main_unload() == -1)
9521 if (ipf_auth_main_unload() == -1)
9524 if (ipf_frag_main_unload() == -1)
9527 if (ipf_nat_main_unload() == -1)
9530 if (ipf_state_main_unload() == -1)
9533 if (ipf_main_unload() == -1)
9540 /* ------------------------------------------------------------------------ */
9541 /* Function: ipf_create_all */
9542 /* Returns: NULL = failure, else success */
9543 /* Parameters: arg(I) - pointer to soft context main structure */
9545 /* Work through all of the subsystems inside IPFilter and call the create */
9546 /* function for each in an order that won't lead to a crash :) */
9547 /* ------------------------------------------------------------------------ */
9552 ipf_main_softc_t *softc;
9554 softc = ipf_main_soft_create(arg);
9559 softc->ipf_log_soft = ipf_log_soft_create(softc);
9560 if (softc->ipf_log_soft == NULL) {
9561 ipf_destroy_all(softc);
9566 softc->ipf_lookup_soft = ipf_lookup_soft_create(softc);
9567 if (softc->ipf_lookup_soft == NULL) {
9568 ipf_destroy_all(softc);
9572 softc->ipf_sync_soft = ipf_sync_soft_create(softc);
9573 if (softc->ipf_sync_soft == NULL) {
9574 ipf_destroy_all(softc);
9578 softc->ipf_state_soft = ipf_state_soft_create(softc);
9579 if (softc->ipf_state_soft == NULL) {
9580 ipf_destroy_all(softc);
9584 softc->ipf_nat_soft = ipf_nat_soft_create(softc);
9585 if (softc->ipf_nat_soft == NULL) {
9586 ipf_destroy_all(softc);
9590 softc->ipf_frag_soft = ipf_frag_soft_create(softc);
9591 if (softc->ipf_frag_soft == NULL) {
9592 ipf_destroy_all(softc);
9596 softc->ipf_auth_soft = ipf_auth_soft_create(softc);
9597 if (softc->ipf_auth_soft == NULL) {
9598 ipf_destroy_all(softc);
9602 softc->ipf_proxy_soft = ipf_proxy_soft_create(softc);
9603 if (softc->ipf_proxy_soft == NULL) {
9604 ipf_destroy_all(softc);
9612 /* ------------------------------------------------------------------------ */
9613 /* Function: ipf_destroy_all */
9615 /* Parameters: softc(I) - pointer to soft context main structure */
9617 /* Work through all of the subsystems inside IPFilter and call the destroy */
9618 /* function for each in an order that won't lead to a crash :) */
9620 /* Every one of these functions is expected to succeed, so there is no */
9621 /* checking of return values. */
9622 /* ------------------------------------------------------------------------ */
9624 ipf_destroy_all(softc)
9625 ipf_main_softc_t *softc;
9628 if (softc->ipf_state_soft != NULL) {
9629 ipf_state_soft_destroy(softc, softc->ipf_state_soft);
9630 softc->ipf_state_soft = NULL;
9633 if (softc->ipf_nat_soft != NULL) {
9634 ipf_nat_soft_destroy(softc, softc->ipf_nat_soft);
9635 softc->ipf_nat_soft = NULL;
9638 if (softc->ipf_frag_soft != NULL) {
9639 ipf_frag_soft_destroy(softc, softc->ipf_frag_soft);
9640 softc->ipf_frag_soft = NULL;
9643 if (softc->ipf_auth_soft != NULL) {
9644 ipf_auth_soft_destroy(softc, softc->ipf_auth_soft);
9645 softc->ipf_auth_soft = NULL;
9648 if (softc->ipf_proxy_soft != NULL) {
9649 ipf_proxy_soft_destroy(softc, softc->ipf_proxy_soft);
9650 softc->ipf_proxy_soft = NULL;
9653 if (softc->ipf_sync_soft != NULL) {
9654 ipf_sync_soft_destroy(softc, softc->ipf_sync_soft);
9655 softc->ipf_sync_soft = NULL;
9658 if (softc->ipf_lookup_soft != NULL) {
9659 ipf_lookup_soft_destroy(softc, softc->ipf_lookup_soft);
9660 softc->ipf_lookup_soft = NULL;
9664 if (softc->ipf_log_soft != NULL) {
9665 ipf_log_soft_destroy(softc, softc->ipf_log_soft);
9666 softc->ipf_log_soft = NULL;
9670 ipf_main_soft_destroy(softc);
9674 /* ------------------------------------------------------------------------ */
9675 /* Function: ipf_init_all */
9676 /* Returns: 0 = success, -1 = failure */
9677 /* Parameters: softc(I) - pointer to soft context main structure */
9679 /* Work through all of the subsystems inside IPFilter and call the init */
9680 /* function for each in an order that won't lead to a crash :) */
9681 /* ------------------------------------------------------------------------ */
9684 ipf_main_softc_t *softc;
9687 if (ipf_main_soft_init(softc) == -1)
9691 if (ipf_log_soft_init(softc, softc->ipf_log_soft) == -1)
9695 if (ipf_lookup_soft_init(softc, softc->ipf_lookup_soft) == -1)
9698 if (ipf_sync_soft_init(softc, softc->ipf_sync_soft) == -1)
9701 if (ipf_state_soft_init(softc, softc->ipf_state_soft) == -1)
9704 if (ipf_nat_soft_init(softc, softc->ipf_nat_soft) == -1)
9707 if (ipf_frag_soft_init(softc, softc->ipf_frag_soft) == -1)
9710 if (ipf_auth_soft_init(softc, softc->ipf_auth_soft) == -1)
9713 if (ipf_proxy_soft_init(softc, softc->ipf_proxy_soft) == -1)
9720 /* ------------------------------------------------------------------------ */
9721 /* Function: ipf_fini_all */
9722 /* Returns: 0 = success, -1 = failure */
9723 /* Parameters: softc(I) - pointer to soft context main structure */
9725 /* Work through all of the subsystems inside IPFilter and call the fini */
9726 /* function for each in an order that won't lead to a crash :) */
9727 /* ------------------------------------------------------------------------ */
9730 ipf_main_softc_t *softc;
9733 ipf_token_flush(softc);
9735 if (ipf_proxy_soft_fini(softc, softc->ipf_proxy_soft) == -1)
9738 if (ipf_auth_soft_fini(softc, softc->ipf_auth_soft) == -1)
9741 if (ipf_frag_soft_fini(softc, softc->ipf_frag_soft) == -1)
9744 if (ipf_nat_soft_fini(softc, softc->ipf_nat_soft) == -1)
9747 if (ipf_state_soft_fini(softc, softc->ipf_state_soft) == -1)
9750 if (ipf_sync_soft_fini(softc, softc->ipf_sync_soft) == -1)
9753 if (ipf_lookup_soft_fini(softc, softc->ipf_lookup_soft) == -1)
9757 if (ipf_log_soft_fini(softc, softc->ipf_log_soft) == -1)
9761 if (ipf_main_soft_fini(softc) == -1)
9768 /* ------------------------------------------------------------------------ */
9769 /* Function: ipf_rule_expire */
9771 /* Parameters: softc(I) - pointer to soft context main structure */
9773 /* At present this function exists just to support temporary addition of */
9774 /* firewall rules. Both inactive and active lists are scanned for items to */
9775 /* purge, as by rights, the expiration is computed as soon as the rule is */
9777 /* ------------------------------------------------------------------------ */
9779 ipf_rule_expire(softc)
9780 ipf_main_softc_t *softc;
9784 if ((softc->ipf_rule_explist[0] == NULL) &&
9785 (softc->ipf_rule_explist[1] == NULL))
9788 WRITE_ENTER(&softc->ipf_mutex);
9790 while ((fr = softc->ipf_rule_explist[0]) != NULL) {
9792 * Because the list is kept sorted on insertion, the fist
9793 * one that dies in the future means no more work to do.
9795 if (fr->fr_die > softc->ipf_ticks)
9797 ipf_rule_delete(softc, fr, IPL_LOGIPF, 0);
9800 while ((fr = softc->ipf_rule_explist[1]) != NULL) {
9802 * Because the list is kept sorted on insertion, the fist
9803 * one that dies in the future means no more work to do.
9805 if (fr->fr_die > softc->ipf_ticks)
9807 ipf_rule_delete(softc, fr, IPL_LOGIPF, 1);
9810 RWLOCK_EXIT(&softc->ipf_mutex);
9814 static int ipf_ht_node_cmp __P((struct host_node_s *, struct host_node_s *));
9815 static void ipf_ht_node_make_key __P((host_track_t *, host_node_t *, int,
9818 host_node_t RBI_ZERO(ipf_rb);
9819 RBI_CODE(ipf_rb, host_node_t, hn_entry, ipf_ht_node_cmp)
9822 /* ------------------------------------------------------------------------ */
9823 /* Function: ipf_ht_node_cmp */
9824 /* Returns: int - 0 == nodes are the same, .. */
9825 /* Parameters: k1(I) - pointer to first key to compare */
9826 /* k2(I) - pointer to second key to compare */
9828 /* The "key" for the node is a combination of two fields: the address */
9829 /* family and the address itself. */
9831 /* Because we're not actually interpreting the address data, it isn't */
9832 /* necessary to convert them to/from network/host byte order. The mask is */
9833 /* just used to remove bits that aren't significant - it doesn't matter */
9834 /* where they are, as long as they're always in the same place. */
9836 /* As with IP6_EQ, comparing IPv6 addresses starts at the bottom because */
9837 /* this is where individual ones will differ the most - but not true for */
9838 /* for /48's, etc. */
9839 /* ------------------------------------------------------------------------ */
9841 ipf_ht_node_cmp(k1, k2)
9842 struct host_node_s *k1, *k2;
9846 i = (k2->hn_addr.adf_family - k1->hn_addr.adf_family);
9850 if (k1->hn_addr.adf_family == AF_INET)
9851 return (k2->hn_addr.adf_addr.in4.s_addr -
9852 k1->hn_addr.adf_addr.in4.s_addr);
9854 i = k2->hn_addr.adf_addr.i6[3] - k1->hn_addr.adf_addr.i6[3];
9857 i = k2->hn_addr.adf_addr.i6[2] - k1->hn_addr.adf_addr.i6[2];
9860 i = k2->hn_addr.adf_addr.i6[1] - k1->hn_addr.adf_addr.i6[1];
9863 i = k2->hn_addr.adf_addr.i6[0] - k1->hn_addr.adf_addr.i6[0];
9868 /* ------------------------------------------------------------------------ */
9869 /* Function: ipf_ht_node_make_key */
9871 /* parameters: htp(I) - pointer to address tracking structure */
9872 /* key(I) - where to store masked address for lookup */
9873 /* family(I) - protocol family of address */
9874 /* addr(I) - pointer to network address */
9876 /* Using the "netmask" (number of bits) stored parent host tracking struct, */
9877 /* copy the address passed in into the key structure whilst masking out the */
9878 /* bits that we don't want. */
9880 /* Because the parser will set ht_netmask to 128 if there is no protocol */
9881 /* specified (the parser doesn't know if it should be a v4 or v6 rule), we */
9882 /* have to be wary of that and not allow 32-128 to happen. */
9883 /* ------------------------------------------------------------------------ */
9885 ipf_ht_node_make_key(htp, key, family, addr)
9891 key->hn_addr.adf_family = family;
9892 if (family == AF_INET) {
9896 key->hn_addr.adf_len = sizeof(key->hn_addr.adf_addr.in4);
9897 bits = htp->ht_netmask;
9901 mask = htonl(0xffffffff << (32 - bits));
9903 key->hn_addr.adf_addr.in4.s_addr = addr->in4.s_addr & mask;
9906 int bits = htp->ht_netmask;
9908 key->hn_addr.adf_len = sizeof(key->hn_addr.adf_addr.in6);
9910 key->hn_addr.adf_addr.i6[3] = addr->i6[3] &
9911 htonl(0xffffffff << (128 - bits));
9912 key->hn_addr.adf_addr.i6[2] = addr->i6[2];
9913 key->hn_addr.adf_addr.i6[1] = addr->i6[2];
9914 key->hn_addr.adf_addr.i6[0] = addr->i6[2];
9915 } else if (bits > 64) {
9916 key->hn_addr.adf_addr.i6[3] = 0;
9917 key->hn_addr.adf_addr.i6[2] = addr->i6[2] &
9918 htonl(0xffffffff << (96 - bits));
9919 key->hn_addr.adf_addr.i6[1] = addr->i6[1];
9920 key->hn_addr.adf_addr.i6[0] = addr->i6[0];
9921 } else if (bits > 32) {
9922 key->hn_addr.adf_addr.i6[3] = 0;
9923 key->hn_addr.adf_addr.i6[2] = 0;
9924 key->hn_addr.adf_addr.i6[1] = addr->i6[1] &
9925 htonl(0xffffffff << (64 - bits));
9926 key->hn_addr.adf_addr.i6[0] = addr->i6[0];
9928 key->hn_addr.adf_addr.i6[3] = 0;
9929 key->hn_addr.adf_addr.i6[2] = 0;
9930 key->hn_addr.adf_addr.i6[1] = 0;
9931 key->hn_addr.adf_addr.i6[0] = addr->i6[0] &
9932 htonl(0xffffffff << (32 - bits));
9939 /* ------------------------------------------------------------------------ */
9940 /* Function: ipf_ht_node_add */
9941 /* Returns: int - 0 == success, -1 == failure */
9942 /* Parameters: softc(I) - pointer to soft context main structure */
9943 /* htp(I) - pointer to address tracking structure */
9944 /* family(I) - protocol family of address */
9945 /* addr(I) - pointer to network address */
9947 /* NOTE: THIS FUNCTION MUST BE CALLED WITH AN EXCLUSIVE LOCK THAT PREVENTS */
9948 /* ipf_ht_node_del FROM RUNNING CONCURRENTLY ON THE SAME htp. */
9950 /* After preparing the key with the address information to find, look in */
9951 /* the red-black tree to see if the address is known. A successful call to */
9952 /* this function can mean one of two things: a new node was added to the */
9953 /* tree or a matching node exists and we're able to bump up its activity. */
9954 /* ------------------------------------------------------------------------ */
9956 ipf_ht_node_add(softc, htp, family, addr)
9957 ipf_main_softc_t *softc;
9965 ipf_ht_node_make_key(htp, &k, family, addr);
9967 h = RBI_SEARCH(ipf_rb, &htp->ht_root, &k);
9969 if (htp->ht_cur_nodes >= htp->ht_max_nodes)
9971 KMALLOC(h, host_node_t *);
9974 LBUMP(ipf_rb_no_mem);
9979 * If there was a macro to initialise the RB node then that
9980 * would get used here, but there isn't...
9982 bzero((char *)h, sizeof(*h));
9983 h->hn_addr = k.hn_addr;
9984 h->hn_addr.adf_family = k.hn_addr.adf_family;
9985 RBI_INSERT(ipf_rb, &htp->ht_root, h);
9986 htp->ht_cur_nodes++;
9988 if ((htp->ht_max_per_node != 0) &&
9989 (h->hn_active >= htp->ht_max_per_node)) {
9990 DT(ipf_rb_node_max);
9991 LBUMP(ipf_rb_node_max);
10002 /* ------------------------------------------------------------------------ */
10003 /* Function: ipf_ht_node_del */
10004 /* Returns: int - 0 == success, -1 == failure */
10005 /* parameters: htp(I) - pointer to address tracking structure */
10006 /* family(I) - protocol family of address */
10007 /* addr(I) - pointer to network address */
10009 /* NOTE: THIS FUNCTION MUST BE CALLED WITH AN EXCLUSIVE LOCK THAT PREVENTS */
10010 /* ipf_ht_node_add FROM RUNNING CONCURRENTLY ON THE SAME htp. */
10012 /* Try and find the address passed in amongst the leavese on this tree to */
10013 /* be friend. If found then drop the active account for that node drops by */
10014 /* one. If that count reaches 0, it is time to free it all up. */
10015 /* ------------------------------------------------------------------------ */
10017 ipf_ht_node_del(htp, family, addr)
10025 ipf_ht_node_make_key(htp, &k, family, addr);
10027 h = RBI_SEARCH(ipf_rb, &htp->ht_root, &k);
10032 if (h->hn_active == 0) {
10033 (void) RBI_DELETE(ipf_rb, &htp->ht_root, h);
10034 htp->ht_cur_nodes--;
10043 /* ------------------------------------------------------------------------ */
10044 /* Function: ipf_rb_ht_init */
10046 /* Parameters: head(I) - pointer to host tracking structure */
10048 /* Initialise the host tracking structure to be ready for use above. */
10049 /* ------------------------------------------------------------------------ */
10051 ipf_rb_ht_init(head)
10052 host_track_t *head;
10054 RBI_INIT(ipf_rb, &head->ht_root);
10058 /* ------------------------------------------------------------------------ */
10059 /* Function: ipf_rb_ht_freenode */
10061 /* Parameters: head(I) - pointer to host tracking structure */
10062 /* arg(I) - additional argument from walk caller */
10064 /* Free an actual host_node_t structure. */
10065 /* ------------------------------------------------------------------------ */
10067 ipf_rb_ht_freenode(node, arg)
10075 /* ------------------------------------------------------------------------ */
10076 /* Function: ipf_rb_ht_flush */
10078 /* Parameters: head(I) - pointer to host tracking structure */
10080 /* Remove all of the nodes in the tree tracking hosts by calling a walker */
10081 /* and free'ing each one. */
10082 /* ------------------------------------------------------------------------ */
10084 ipf_rb_ht_flush(head)
10085 host_track_t *head;
10087 RBI_WALK(ipf_rb, &head->ht_root, ipf_rb_ht_freenode, NULL);
10091 /* ------------------------------------------------------------------------ */
10092 /* Function: ipf_slowtimer */
10094 /* Parameters: ptr(I) - pointer to main ipf soft context structure */
10096 /* Slowly expire held state for fragments. Timeouts are set * in */
10097 /* expectation of this being called twice per second. */
10098 /* ------------------------------------------------------------------------ */
10100 ipf_slowtimer(softc)
10101 ipf_main_softc_t *softc;
10104 ipf_token_expire(softc);
10105 ipf_frag_expire(softc);
10106 ipf_state_expire(softc);
10107 ipf_nat_expire(softc);
10108 ipf_auth_expire(softc);
10109 ipf_lookup_expire(softc);
10110 ipf_rule_expire(softc);
10111 ipf_sync_expire(softc);
10112 softc->ipf_ticks++;
10113 # if defined(__OpenBSD__)
10114 timeout_add(&ipf_slowtimer_ch, hz/2);
10119 /* ------------------------------------------------------------------------ */
10120 /* Function: ipf_inet_mask_add */
10122 /* Parameters: bits(I) - pointer to nat context information */
10123 /* mtab(I) - pointer to mask hash table structure */
10125 /* When called, bits represents the mask of a new NAT rule that has just */
10126 /* been added. This function inserts a bitmask into the array of masks to */
10127 /* search when searching for a matching NAT rule for a packet. */
10128 /* Prevention of duplicate masks is achieved by checking the use count for */
10129 /* a given netmask. */
10130 /* ------------------------------------------------------------------------ */
10132 ipf_inet_mask_add(bits, mtab)
10134 ipf_v4_masktab_t *mtab;
10139 mtab->imt4_masks[bits]++;
10140 if (mtab->imt4_masks[bits] > 1)
10146 mask = 0xffffffff << (32 - bits);
10148 for (i = 0; i < 33; i++) {
10149 if (ntohl(mtab->imt4_active[i]) < mask) {
10150 for (j = 32; j > i; j--)
10151 mtab->imt4_active[j] = mtab->imt4_active[j - 1];
10152 mtab->imt4_active[i] = htonl(mask);
10160 /* ------------------------------------------------------------------------ */
10161 /* Function: ipf_inet_mask_del */
10163 /* Parameters: bits(I) - number of bits set in the netmask */
10164 /* mtab(I) - pointer to mask hash table structure */
10166 /* Remove the 32bit bitmask represented by "bits" from the collection of */
10167 /* netmasks stored inside of mtab. */
10168 /* ------------------------------------------------------------------------ */
10170 ipf_inet_mask_del(bits, mtab)
10172 ipf_v4_masktab_t *mtab;
10177 mtab->imt4_masks[bits]--;
10178 if (mtab->imt4_masks[bits] > 0)
10181 mask = htonl(0xffffffff << (32 - bits));
10182 for (i = 0; i < 33; i++) {
10183 if (mtab->imt4_active[i] == mask) {
10184 for (j = i + 1; j < 33; j++)
10185 mtab->imt4_active[j - 1] = mtab->imt4_active[j];
10190 ASSERT(mtab->imt4_max >= 0);
10195 /* ------------------------------------------------------------------------ */
10196 /* Function: ipf_inet6_mask_add */
10198 /* Parameters: bits(I) - number of bits set in mask */
10199 /* mask(I) - pointer to mask to add */
10200 /* mtab(I) - pointer to mask hash table structure */
10202 /* When called, bitcount represents the mask of a IPv6 NAT map rule that */
10203 /* has just been added. This function inserts a bitmask into the array of */
10204 /* masks to search when searching for a matching NAT rule for a packet. */
10205 /* Prevention of duplicate masks is achieved by checking the use count for */
10206 /* a given netmask. */
10207 /* ------------------------------------------------------------------------ */
10209 ipf_inet6_mask_add(bits, mask, mtab)
10212 ipf_v6_masktab_t *mtab;
10217 mtab->imt6_masks[bits]++;
10218 if (mtab->imt6_masks[bits] > 1)
10229 for (i = 0; i < 129; i++) {
10230 if (IP6_LT(&mtab->imt6_active[i], mask)) {
10231 for (j = 128; j > i; j--)
10232 mtab->imt6_active[j] = mtab->imt6_active[j - 1];
10233 mtab->imt6_active[i] = *mask;
10241 /* ------------------------------------------------------------------------ */
10242 /* Function: ipf_inet6_mask_del */
10244 /* Parameters: bits(I) - number of bits set in mask */
10245 /* mask(I) - pointer to mask to remove */
10246 /* mtab(I) - pointer to mask hash table structure */
10248 /* Remove the 128bit bitmask represented by "bits" from the collection of */
10249 /* netmasks stored inside of mtab. */
10250 /* ------------------------------------------------------------------------ */
10252 ipf_inet6_mask_del(bits, mask, mtab)
10255 ipf_v6_masktab_t *mtab;
10260 mtab->imt6_masks[bits]--;
10261 if (mtab->imt6_masks[bits] > 0)
10271 for (i = 0; i < 129; i++) {
10272 if (IP6_EQ(&mtab->imt6_active[i], mask)) {
10273 for (j = i + 1; j < 129; j++) {
10274 mtab->imt6_active[j - 1] = mtab->imt6_active[j];
10275 if (IP6_EQ(&mtab->imt6_active[j - 1], &zero))
10282 ASSERT(mtab->imt6_max >= 0);