2 * Copyright (C) 1993-2003 by Darren Reed.
4 * See the IPFILTER.LICENCE file for details on licencing.
6 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
8 * Copyright (c) 2014, Joyent, Inc. All rights reserved.
11 #if defined(KERNEL) || defined(_KERNEL)
17 #include <sys/errno.h>
18 #include <sys/types.h>
19 #include <sys/param.h>
21 #if defined(__NetBSD__)
22 # if (NetBSD >= 199905) && !defined(IPFILTER_LKM) && defined(_KERNEL)
23 # include "opt_ipfilter_log.h"
26 #if defined(_KERNEL) && defined(__FreeBSD_version) && \
27 (__FreeBSD_version >= 220000)
28 # if (__FreeBSD_version >= 400000)
29 # if !defined(IPFILTER_LKM)
30 # include "opt_inet6.h"
32 # if (__FreeBSD_version == 400019)
33 # define CSUM_DELAY_DATA
36 # include <sys/filio.h>
38 # include <sys/ioctl.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 #if !defined(_KERNEL) && defined(__FreeBSD__)
82 # include "radix_ipf.h"
84 #include <net/route.h>
85 #include <netinet/in.h>
86 #include <netinet/in_systm.h>
87 #include <netinet/ip.h>
89 # include <netinet/ip_var.h>
91 #if defined(__sgi) && defined(IFF_DRVRLOCK) /* IRIX 6 */
92 # include <sys/hashing.h>
93 # include <netinet/in_var.h>
95 #include <netinet/tcp.h>
96 #if (!defined(__sgi) && !defined(AIX)) || defined(_KERNEL)
97 # include <netinet/udp.h>
98 # include <netinet/ip_icmp.h>
101 # undef _NET_ROUTE_INCLUDED
103 #include "netinet/ip_compat.h"
105 # include <netinet/icmp6.h>
106 # if !defined(SOLARIS) && defined(_KERNEL) && !defined(__osf__) && \
108 # include <netinet6/in6_var.h>
111 #include <netinet/tcpip.h>
112 #include "netinet/ip_fil.h"
113 #include "netinet/ip_nat.h"
114 #include "netinet/ip_frag.h"
115 #include "netinet/ip_state.h"
116 #include "netinet/ip_proxy.h"
117 #include "netinet/ip_auth.h"
118 #include "netinet/ipf_stack.h"
120 # include "netinet/ip_scan.h"
123 # include "netinet/ip_sync.h"
125 #include "netinet/ip_pool.h"
126 #include "netinet/ip_htable.h"
127 #ifdef IPFILTER_COMPILED
128 # include "netinet/ip_rules.h"
130 #if defined(IPFILTER_BPF) && defined(_KERNEL)
131 # include <net/bpf.h>
133 #if defined(__FreeBSD_version) && (__FreeBSD_version >= 300000)
134 # include <sys/malloc.h>
135 # if defined(_KERNEL) && !defined(IPFILTER_LKM)
136 # include "opt_ipfilter.h"
139 #include "netinet/ipl.h"
141 #include <sys/sunddi.h>
143 /* END OF INCLUDES */
146 static const char sccsid
[] = "@(#)fil.c 1.36 6/5/96 (C) 1993-2000 Darren Reed";
147 static const char rcsid
[] = "@(#)$Id: fil.c,v 2.243.2.64 2005/08/13 05:19:59 darrenr Exp $";
153 # include "bpf-ipf.h"
156 # define FR_VERBOSE(verb_pr) verbose verb_pr
157 # define FR_DEBUG(verb_pr) debug verb_pr
158 #else /* #ifndef _KERNEL */
159 # define FR_VERBOSE(verb_pr)
160 # define FR_DEBUG(verb_pr)
164 char ipfilter_version
[] = IPL_VERSION
;
172 #ifdef IPFILTER_LOOKUP
178 #ifdef IPFILTER_COMPILED
181 #ifdef IPFILTER_CKSUM
195 #define IPF_BUMP(x) (x)++
197 static INLINE
int fr_ipfcheck
__P((fr_info_t
*, frentry_t
*, int));
198 static INLINE
int fr_ipfcheck
__P((fr_info_t
*, frentry_t
*, int));
199 static int fr_portcheck
__P((frpcmp_t
*, u_short
*));
200 static int frflushlist
__P((int, minor_t
, int *, frentry_t
**,
202 static ipfunc_t fr_findfunc
__P((ipfunc_t
));
203 static frentry_t
*fr_firewall
__P((fr_info_t
*, u_32_t
*));
204 static int fr_funcinit
__P((frentry_t
*fr
, ipf_stack_t
*));
205 static INLINE
void frpr_ah
__P((fr_info_t
*));
206 static INLINE
void frpr_esp
__P((fr_info_t
*));
207 static INLINE
void frpr_gre
__P((fr_info_t
*));
208 static INLINE
void frpr_udp
__P((fr_info_t
*));
209 static INLINE
void frpr_tcp
__P((fr_info_t
*));
210 static INLINE
void frpr_icmp
__P((fr_info_t
*));
211 static INLINE
void frpr_ipv4hdr
__P((fr_info_t
*));
212 static INLINE
int frpr_pullup
__P((fr_info_t
*, int));
213 static INLINE
void frpr_short
__P((fr_info_t
*, int));
214 static INLINE
void frpr_tcpcommon
__P((fr_info_t
*));
215 static INLINE
void frpr_udpcommon
__P((fr_info_t
*));
216 static INLINE
int fr_updateipid
__P((fr_info_t
*));
217 #ifdef IPFILTER_LOOKUP
218 static int fr_grpmapinit
__P((frentry_t
*fr
, ipf_stack_t
*));
219 static INLINE
void *fr_resolvelookup
__P((u_int
, u_int
, lookupfunc_t
*,
222 static void frsynclist
__P((int, int, void *, char *, frentry_t
*,
224 static void *fr_ifsync
__P((int, int, char *, char *,
225 void *, void *, ipf_stack_t
*));
226 static ipftuneable_t
*fr_findtunebyname
__P((const char *, ipf_stack_t
*));
227 static ipftuneable_t
*fr_findtunebycookie
__P((void *, void **, ipf_stack_t
*));
230 * bit values for identifying presence of individual IP options
231 * All of these tables should be ordered by increasing key value on the left
232 * hand side to allow for binary searching of the array and include a trailer
233 * with a 0 for the bitmask for linear searches to easily find the end with.
235 const struct optlist ipopts
[20] = {
236 { IPOPT_NOP
, 0x000001 },
237 { IPOPT_RR
, 0x000002 },
238 { IPOPT_ZSU
, 0x000004 },
239 { IPOPT_MTUP
, 0x000008 },
240 { IPOPT_MTUR
, 0x000010 },
241 { IPOPT_ENCODE
, 0x000020 },
242 { IPOPT_TS
, 0x000040 },
243 { IPOPT_TR
, 0x000080 },
244 { IPOPT_SECURITY
, 0x000100 },
245 { IPOPT_LSRR
, 0x000200 },
246 { IPOPT_E_SEC
, 0x000400 },
247 { IPOPT_CIPSO
, 0x000800 },
248 { IPOPT_SATID
, 0x001000 },
249 { IPOPT_SSRR
, 0x002000 },
250 { IPOPT_ADDEXT
, 0x004000 },
251 { IPOPT_VISA
, 0x008000 },
252 { IPOPT_IMITD
, 0x010000 },
253 { IPOPT_EIP
, 0x020000 },
254 { IPOPT_FINN
, 0x040000 },
259 struct optlist ip6exthdr
[] = {
260 { IPPROTO_HOPOPTS
, 0x000001 },
261 { IPPROTO_IPV6
, 0x000002 },
262 { IPPROTO_ROUTING
, 0x000004 },
263 { IPPROTO_FRAGMENT
, 0x000008 },
264 { IPPROTO_ESP
, 0x000010 },
265 { IPPROTO_AH
, 0x000020 },
266 { IPPROTO_NONE
, 0x000040 },
267 { IPPROTO_DSTOPTS
, 0x000080 },
272 struct optlist tcpopts
[] = {
273 { TCPOPT_NOP
, 0x000001 },
274 { TCPOPT_MAXSEG
, 0x000002 },
275 { TCPOPT_WINDOW
, 0x000004 },
276 { TCPOPT_SACK_PERMITTED
, 0x000008 },
277 { TCPOPT_SACK
, 0x000010 },
278 { TCPOPT_TIMESTAMP
, 0x000020 },
283 * bit values for identifying presence of individual IP security options
285 const struct optlist secopt
[8] = {
286 { IPSO_CLASS_RES4
, 0x01 },
287 { IPSO_CLASS_TOPS
, 0x02 },
288 { IPSO_CLASS_SECR
, 0x04 },
289 { IPSO_CLASS_RES3
, 0x08 },
290 { IPSO_CLASS_CONF
, 0x10 },
291 { IPSO_CLASS_UNCL
, 0x20 },
292 { IPSO_CLASS_RES2
, 0x40 },
293 { IPSO_CLASS_RES1
, 0x80 }
298 * Table of functions available for use with call rules.
300 static ipfunc_resolve_t fr_availfuncs
[] = {
301 #ifdef IPFILTER_LOOKUP
302 { "fr_srcgrpmap", fr_srcgrpmap
, fr_grpmapinit
},
303 { "fr_dstgrpmap", fr_dstgrpmap
, fr_grpmapinit
},
310 * Below we declare a list of constants used only by the ipf_extraflush()
311 * routine. We are placing it here, instead of in ipf_extraflush() itself,
312 * because we want to make it visible to tools such as mdb, nm etc., so the
313 * values can easily be altered during debugging.
315 static const int idletime_tab
[] = {
316 IPF_TTLVAL(30), /* 30 seconds */
317 IPF_TTLVAL(1800), /* 30 minutes */
318 IPF_TTLVAL(43200), /* 12 hours */
319 IPF_TTLVAL(345600), /* 4 days */
324 * The next section of code is a a collection of small routines that set
325 * fields in the fr_info_t structure passed based on properties of the
326 * current packet. There are different routines for the same protocol
327 * for each of IPv4 and IPv6. Adding a new protocol, for which there
328 * will "special" inspection for setup, is now more easily done by adding
329 * a new routine and expanding the frpr_ipinit*() function rather than by
330 * adding more code to a growing switch statement.
333 static INLINE
int frpr_ah6
__P((fr_info_t
*));
334 static INLINE
void frpr_esp6
__P((fr_info_t
*));
335 static INLINE
void frpr_gre6
__P((fr_info_t
*));
336 static INLINE
void frpr_udp6
__P((fr_info_t
*));
337 static INLINE
void frpr_tcp6
__P((fr_info_t
*));
338 static INLINE
void frpr_icmp6
__P((fr_info_t
*));
339 static INLINE
void frpr_ipv6hdr
__P((fr_info_t
*));
340 static INLINE
void frpr_short6
__P((fr_info_t
*, int));
341 static INLINE
int frpr_hopopts6
__P((fr_info_t
*));
342 static INLINE
int frpr_routing6
__P((fr_info_t
*));
343 static INLINE
int frpr_dstopts6
__P((fr_info_t
*));
344 static INLINE
int frpr_fragment6
__P((fr_info_t
*));
345 static INLINE
int frpr_ipv6exthdr
__P((fr_info_t
*, int, int));
348 /* ------------------------------------------------------------------------ */
349 /* Function: frpr_short6 */
351 /* Parameters: fin(I) - pointer to packet information */
354 /* This is function enforces the 'is a packet too short to be legit' rule */
355 /* for IPv6 and marks the packet with FI_SHORT if so. See function comment */
356 /* for frpr_short() for more details. */
357 /* ------------------------------------------------------------------------ */
358 static INLINE
void frpr_short6(fin
, xmin
)
363 if (fin
->fin_dlen
< xmin
)
364 fin
->fin_flx
|= FI_SHORT
;
368 /* ------------------------------------------------------------------------ */
369 /* Function: frpr_ipv6hdr */
371 /* Parameters: fin(I) - pointer to packet information */
374 /* Copy values from the IPv6 header into the fr_info_t struct and call the */
375 /* per-protocol analyzer if it exists. */
376 /* ------------------------------------------------------------------------ */
377 static INLINE
void frpr_ipv6hdr(fin
)
380 ip6_t
*ip6
= (ip6_t
*)fin
->fin_ip
;
381 int p
, go
= 1, i
, hdrcount
;
382 fr_ip_t
*fi
= &fin
->fin_fi
;
392 fi
->fi_ttl
= ip6
->ip6_hlim
;
393 fi
->fi_src
.in6
= ip6
->ip6_src
;
394 fi
->fi_dst
.in6
= ip6
->ip6_dst
;
398 while (go
&& !(fin
->fin_flx
& (FI_BAD
|FI_SHORT
))) {
411 case IPPROTO_ICMPV6
:
421 case IPPROTO_HOPOPTS
:
423 * hop by hop ext header is only allowed
424 * right after IPv6 header.
427 fin
->fin_flx
|= FI_BAD
;
430 p
= frpr_hopopts6(fin
);
434 case IPPROTO_DSTOPTS
:
435 p
= frpr_dstopts6(fin
);
438 case IPPROTO_ROUTING
:
439 p
= frpr_routing6(fin
);
452 for (i
= 0; ip6exthdr
[i
].ol_bit
!= 0; i
++)
453 if (ip6exthdr
[i
].ol_val
== p
) {
454 fin
->fin_flx
|= ip6exthdr
[i
].ol_bit
;
464 case IPPROTO_FRAGMENT
:
465 p
= frpr_fragment6(fin
);
466 if (fin
->fin_off
!= 0) /* Not the first frag */
477 * It is important to note that at this point, for the
478 * extension headers (go != 0), the entire header may not have
479 * been pulled up when the code gets to this point. This is
480 * only done for "go != 0" because the other header handlers
481 * will all pullup their complete header. The other indicator
482 * of an incomplete packet is that this was just an extension
485 if ((go
!= 0) && (p
!= IPPROTO_NONE
) &&
486 (frpr_pullup(fin
, 0) == -1)) {
495 /* ------------------------------------------------------------------------ */
496 /* Function: frpr_ipv6exthdr */
497 /* Returns: int - value of the next header or IPPROTO_NONE if error */
498 /* Parameters: fin(I) - pointer to packet information */
499 /* multiple(I) - flag indicating yes/no if multiple occurances */
500 /* of this extension header are allowed. */
501 /* proto(I) - protocol number for this extension header */
504 /* This function expects to find an IPv6 extension header at fin_dp. */
505 /* There must be at least 8 bytes of data at fin_dp for there to be a valid */
506 /* extension header present. If a good one is found, fin_dp is advanced to */
507 /* point at the first piece of data after the extension header, fin_exthdr */
508 /* points to the start of the extension header and the "protocol" of the */
509 /* *NEXT* header is returned. */
510 /* ------------------------------------------------------------------------ */
511 static INLINE
int frpr_ipv6exthdr(fin
, multiple
, proto
)
519 fin
->fin_flx
|= FI_V6EXTHDR
;
521 /* 8 is default length of extension hdr */
522 if ((fin
->fin_dlen
- 8) < 0) {
523 fin
->fin_flx
|= FI_SHORT
;
527 if (frpr_pullup(fin
, 8) == -1)
531 shift
= 8 + (hdr
->ip6e_len
<< 3);
532 if (shift
> fin
->fin_dlen
) { /* Nasty extension header length? */
533 fin
->fin_flx
|= FI_BAD
;
537 for (i
= 0; ip6exthdr
[i
].ol_bit
!= 0; i
++)
538 if (ip6exthdr
[i
].ol_val
== proto
) {
540 * Most IPv6 extension headers are only allowed once.
542 if ((multiple
== 0) &&
543 ((fin
->fin_optmsk
& ip6exthdr
[i
].ol_bit
) != 0))
544 fin
->fin_flx
|= FI_BAD
;
546 fin
->fin_optmsk
|= ip6exthdr
[i
].ol_bit
;
550 fin
->fin_dp
= (char *)fin
->fin_dp
+ shift
;
551 fin
->fin_dlen
-= shift
;
553 return hdr
->ip6e_nxt
;
557 /* ------------------------------------------------------------------------ */
558 /* Function: frpr_hopopts6 */
559 /* Returns: int - value of the next header or IPPROTO_NONE if error */
560 /* Parameters: fin(I) - pointer to packet information */
563 /* This is function checks pending hop by hop options extension header */
564 /* ------------------------------------------------------------------------ */
565 static INLINE
int frpr_hopopts6(fin
)
568 return frpr_ipv6exthdr(fin
, 0, IPPROTO_HOPOPTS
);
572 /* ------------------------------------------------------------------------ */
573 /* Function: frpr_routing6 */
574 /* Returns: int - value of the next header or IPPROTO_NONE if error */
575 /* Parameters: fin(I) - pointer to packet information */
578 /* This is function checks pending routing extension header */
579 /* ------------------------------------------------------------------------ */
580 static INLINE
int frpr_routing6(fin
)
587 if (frpr_ipv6exthdr(fin
, 0, IPPROTO_ROUTING
) == IPPROTO_NONE
)
590 shift
= 8 + (hdr
->ip6e_len
<< 3);
592 * Nasty extension header length?
594 if ((hdr
->ip6e_len
<< 3) & 15) {
595 fin
->fin_flx
|= FI_BAD
;
597 * Compensate for the changes made in frpr_ipv6exthdr()
599 fin
->fin_dlen
+= shift
;
600 fin
->fin_dp
= (char *)fin
->fin_dp
- shift
;
604 return hdr
->ip6e_nxt
;
608 /* ------------------------------------------------------------------------ */
609 /* Function: frpr_fragment6 */
610 /* Returns: int - value of the next header or IPPROTO_NONE if error */
611 /* Parameters: fin(I) - pointer to packet information */
614 /* Examine the IPv6 fragment header and extract fragment offset information.*/
616 /* We don't know where the transport layer header (or whatever is next is), */
617 /* as it could be behind destination options (amongst others). Because */
618 /* there is no fragment cache, there is no knowledge about whether or not an*/
619 /* upper layer header has been seen (or where it ends) and thus we are not */
620 /* able to continue processing beyond this header with any confidence. */
621 /* ------------------------------------------------------------------------ */
622 static INLINE
int frpr_fragment6(fin
)
625 struct ip6_frag
*frag
;
627 fin
->fin_flx
|= FI_FRAG
;
630 * A fragmented IPv6 packet implies that there must be something
631 * else after the fragment.
633 if (frpr_ipv6exthdr(fin
, 0, IPPROTO_FRAGMENT
) == IPPROTO_NONE
)
636 frag
= (struct ip6_frag
*)((char *)fin
->fin_dp
- sizeof(*frag
));
639 * If this fragment isn't the last then the packet length must
640 * be a multiple of 8.
642 if ((frag
->ip6f_offlg
& IP6F_MORE_FRAG
) != 0) {
643 fin
->fin_flx
|= FI_MOREFRAG
;
645 if ((fin
->fin_plen
& 0x7) != 0)
646 fin
->fin_flx
|= FI_BAD
;
649 fin
->fin_id
= frag
->ip6f_ident
;
650 fin
->fin_off
= ntohs(frag
->ip6f_offlg
& IP6F_OFF_MASK
);
651 if (fin
->fin_off
!= 0)
652 fin
->fin_flx
|= FI_FRAGBODY
;
654 return frag
->ip6f_nxt
;
658 /* ------------------------------------------------------------------------ */
659 /* Function: frpr_dstopts6 */
660 /* Returns: int - value of the next header or IPPROTO_NONE if error */
661 /* Parameters: fin(I) - pointer to packet information */
662 /* nextheader(I) - stores next header value */
665 /* This is function checks pending destination options extension header */
666 /* ------------------------------------------------------------------------ */
667 static INLINE
int frpr_dstopts6(fin
)
670 return frpr_ipv6exthdr(fin
, 1, IPPROTO_DSTOPTS
);
674 /* ------------------------------------------------------------------------ */
675 /* Function: frpr_icmp6 */
677 /* Parameters: fin(I) - pointer to packet information */
680 /* This routine is mainly concerned with determining the minimum valid size */
681 /* for an ICMPv6 packet. */
682 /* ------------------------------------------------------------------------ */
683 static INLINE
void frpr_icmp6(fin
)
686 int minicmpsz
= sizeof(struct icmp6_hdr
);
687 struct icmp6_hdr
*icmp6
;
689 if (frpr_pullup(fin
, ICMP6ERR_MINPKTLEN
- sizeof(ip6_t
)) == -1)
692 if (fin
->fin_dlen
> 1) {
695 fin
->fin_data
[0] = *(u_short
*)icmp6
;
697 if ((icmp6
->icmp6_type
& ICMP6_INFOMSG_MASK
) != 0)
698 fin
->fin_flx
|= FI_ICMPQUERY
;
700 switch (icmp6
->icmp6_type
)
702 case ICMP6_ECHO_REPLY
:
703 case ICMP6_ECHO_REQUEST
:
704 if (fin
->fin_dlen
>= 6)
705 fin
->fin_data
[1] = icmp6
->icmp6_id
;
706 minicmpsz
= ICMP6ERR_MINPKTLEN
- sizeof(ip6_t
);
708 case ICMP6_DST_UNREACH
:
709 case ICMP6_PACKET_TOO_BIG
:
710 case ICMP6_TIME_EXCEEDED
:
711 case ICMP6_PARAM_PROB
:
712 if ((fin
->fin_m
!= NULL
) &&
713 (M_LEN(fin
->fin_m
) < fin
->fin_plen
)) {
714 if (fr_coalesce(fin
) != 1)
717 fin
->fin_flx
|= FI_ICMPERR
;
718 minicmpsz
= ICMP6ERR_IPICMPHLEN
- sizeof(ip6_t
);
725 frpr_short6(fin
, minicmpsz
);
729 /* ------------------------------------------------------------------------ */
730 /* Function: frpr_udp6 */
732 /* Parameters: fin(I) - pointer to packet information */
735 /* Analyse the packet for IPv6/UDP properties. */
736 /* Is not expected to be called for fragmented packets. */
737 /* ------------------------------------------------------------------------ */
738 static INLINE
void frpr_udp6(fin
)
744 frpr_short6(fin
, sizeof(struct udphdr
));
745 if (frpr_pullup(fin
, sizeof(struct udphdr
)) == -1)
752 /* ------------------------------------------------------------------------ */
753 /* Function: frpr_tcp6 */
755 /* Parameters: fin(I) - pointer to packet information */
758 /* Analyse the packet for IPv6/TCP properties. */
759 /* Is not expected to be called for fragmented packets. */
760 /* ------------------------------------------------------------------------ */
761 static INLINE
void frpr_tcp6(fin
)
767 frpr_short6(fin
, sizeof(struct tcphdr
));
768 if (frpr_pullup(fin
, sizeof(struct tcphdr
)) == -1)
775 /* ------------------------------------------------------------------------ */
776 /* Function: frpr_esp6 */
778 /* Parameters: fin(I) - pointer to packet information */
781 /* Analyse the packet for ESP properties. */
782 /* The minimum length is taken to be the SPI (32bits) plus a tail (32bits) */
783 /* even though the newer ESP packets must also have a sequence number that */
784 /* is 32bits as well, it is not possible(?) to determine the version from a */
785 /* simple packet header. */
786 /* ------------------------------------------------------------------------ */
787 static INLINE
void frpr_esp6(fin
)
791 frpr_short6(fin
, sizeof(grehdr_t
));
793 (void) frpr_pullup(fin
, 8);
795 for (i
= 0; ip6exthdr
[i
].ol_bit
!= 0; i
++)
796 if (ip6exthdr
[i
].ol_val
== IPPROTO_ESP
) {
797 fin
->fin_optmsk
|= ip6exthdr
[i
].ol_bit
;
803 /* ------------------------------------------------------------------------ */
804 /* Function: frpr_ah6 */
806 /* Parameters: fin(I) - pointer to packet information */
809 /* Analyse the packet for AH properties. */
810 /* The minimum length is taken to be the combination of all fields in the */
811 /* header being present and no authentication data (null algorithm used.) */
812 /* ------------------------------------------------------------------------ */
813 static INLINE
int frpr_ah6(fin
)
819 frpr_short6(fin
, 12);
821 if (frpr_pullup(fin
, sizeof(*ah
)) == -1)
824 for (i
= 0; ip6exthdr
[i
].ol_bit
!= 0; i
++)
825 if (ip6exthdr
[i
].ol_val
== IPPROTO_AH
) {
826 fin
->fin_optmsk
|= ip6exthdr
[i
].ol_bit
;
830 ah
= (authhdr_t
*)fin
->fin_dp
;
832 shift
= (ah
->ah_plen
+ 2) * 4;
833 fin
->fin_dlen
-= shift
;
834 fin
->fin_dp
= (char*)fin
->fin_dp
+ shift
;
840 /* ------------------------------------------------------------------------ */
841 /* Function: frpr_gre6 */
843 /* Parameters: fin(I) - pointer to packet information */
845 /* Analyse the packet for GRE properties. */
846 /* ------------------------------------------------------------------------ */
847 static INLINE
void frpr_gre6(fin
)
852 frpr_short6(fin
, sizeof(grehdr_t
));
854 if (frpr_pullup(fin
, sizeof(grehdr_t
)) == -1)
858 if (GRE_REV(gre
->gr_flags
) == 1)
859 fin
->fin_data
[0] = gre
->gr_call
;
861 #endif /* USE_INET6 */
864 /* ------------------------------------------------------------------------ */
865 /* Function: frpr_pullup */
866 /* Returns: int - 0 == pullup succeeded, -1 == failure */
867 /* Parameters: fin(I) - pointer to packet information */
868 /* plen(I) - length (excluding L3 header) to pullup */
870 /* Short inline function to cut down on code duplication to perform a call */
871 /* to fr_pullup to ensure there is the required amount of data, */
872 /* consecutively in the packet buffer. */
873 /* ------------------------------------------------------------------------ */
874 static INLINE
int frpr_pullup(fin
, plen
)
879 if (fin
->fin_m
!= NULL
) {
882 ipoff
= (char *)fin
->fin_ip
- MTOD(fin
->fin_m
, char *);
884 if (fin
->fin_dp
!= NULL
)
885 plen
+= (char *)fin
->fin_dp
-
886 ((char *)fin
->fin_ip
+ fin
->fin_hlen
);
887 plen
+= fin
->fin_hlen
;
889 * We don't do 'plen += ipoff;' here. The fr_pullup() will
892 if (M_LEN(fin
->fin_m
) < plen
+ ipoff
) {
893 if (fr_pullup(fin
->fin_m
, fin
, plen
) == NULL
)
902 /* ------------------------------------------------------------------------ */
903 /* Function: frpr_short */
905 /* Parameters: fin(I) - pointer to packet information */
906 /* xmin(I) - minimum header size */
908 /* Check if a packet is "short" as defined by xmin. The rule we are */
909 /* applying here is that the packet must not be fragmented within the layer */
910 /* 4 header. That is, it must not be a fragment that has its offset set to */
911 /* start within the layer 4 header (hdrmin) or if it is at offset 0, the */
912 /* entire layer 4 header must be present (min). */
913 /* ------------------------------------------------------------------------ */
914 static INLINE
void frpr_short(fin
, xmin
)
919 if (fin
->fin_off
== 0) {
920 if (fin
->fin_dlen
< xmin
)
921 fin
->fin_flx
|= FI_SHORT
;
922 } else if (fin
->fin_off
< xmin
) {
923 fin
->fin_flx
|= FI_SHORT
;
928 /* ------------------------------------------------------------------------ */
929 /* Function: frpr_icmp */
931 /* Parameters: fin(I) - pointer to packet information */
934 /* Do a sanity check on the packet for ICMP (v4). In nearly all cases, */
935 /* except extrememly bad packets, both type and code will be present. */
936 /* The expected minimum size of an ICMP packet is very much dependent on */
937 /* the type of it. */
939 /* XXX - other ICMP sanity checks? */
940 /* ------------------------------------------------------------------------ */
941 static INLINE
void frpr_icmp(fin
)
944 int minicmpsz
= sizeof(struct icmp
);
947 ipf_stack_t
*ifs
= fin
->fin_ifs
;
949 if (fin
->fin_off
!= 0) {
950 frpr_short(fin
, ICMPERR_ICMPHLEN
);
954 if (frpr_pullup(fin
, ICMPERR_ICMPHLEN
) == -1)
960 * This is a right place to set icmp pointer, since the memory
961 * referenced by fin_dp could get reallocated. The code down below can
962 * rely on fact icmp variable always points to ICMP header.
965 fin
->fin_data
[0] = *(u_short
*)icmp
;
966 fin
->fin_data
[1] = icmp
->icmp_id
;
968 switch (icmp
->icmp_type
)
970 case ICMP_ECHOREPLY
:
972 /* Router discovery messaes - RFC 1256 */
973 case ICMP_ROUTERADVERT
:
974 case ICMP_ROUTERSOLICIT
:
975 minicmpsz
= ICMP_MINLEN
;
978 * type(1) + code(1) + cksum(2) + id(2) seq(2) +
979 * 3 * timestamp(3 * 4)
982 case ICMP_TSTAMPREPLY
:
986 * type(1) + code(1) + cksum(2) + id(2) seq(2) +
990 case ICMP_MASKREPLY
:
994 * type(1) + code(1) + cksum(2) + id(2) seq(2) + ip(20+)
997 if (icmp
->icmp_code
== ICMP_UNREACH_NEEDFRAG
) {
998 if (icmp
->icmp_nextmtu
< ifs
->ifs_fr_icmpminfragmtu
)
999 fin
->fin_flx
|= FI_BAD
;
1002 case ICMP_SOURCEQUENCH
:
1003 case ICMP_REDIRECT
:
1004 case ICMP_TIMXCEED
:
1005 case ICMP_PARAMPROB
:
1006 fin
->fin_flx
|= FI_ICMPERR
;
1007 if (fr_coalesce(fin
) != 1)
1010 * ICMP error packets should not be generated for IP
1011 * packets that are a fragment that isn't the first
1014 oip
= (ip_t
*)((char *)fin
->fin_dp
+ ICMPERR_ICMPHLEN
);
1015 if ((ntohs(oip
->ip_off
) & IP_OFFMASK
) != 0)
1016 fin
->fin_flx
|= FI_BAD
;
1022 frpr_short(fin
, minicmpsz
);
1026 /* ------------------------------------------------------------------------ */
1027 /* Function: frpr_tcpcommon */
1029 /* Parameters: fin(I) - pointer to packet information */
1031 /* TCP header sanity checking. Look for bad combinations of TCP flags, */
1032 /* and make some checks with how they interact with other fields. */
1033 /* If compiled with IPFILTER_CKSUM, check to see if the TCP checksum is */
1034 /* valid and mark the packet as bad if not. */
1035 /* ------------------------------------------------------------------------ */
1036 static INLINE
void frpr_tcpcommon(fin
)
1042 fin
->fin_flx
|= FI_TCPUDP
;
1043 if (fin
->fin_off
!= 0)
1046 if (frpr_pullup(fin
, sizeof(*tcp
)) == -1)
1050 if (fin
->fin_dlen
> 3) {
1051 fin
->fin_sport
= ntohs(tcp
->th_sport
);
1052 fin
->fin_dport
= ntohs(tcp
->th_dport
);
1055 if ((fin
->fin_flx
& FI_SHORT
) != 0)
1059 * Use of the TCP data offset *must* result in a value that is at
1060 * least the same size as the TCP header.
1062 tlen
= TCP_OFF(tcp
) << 2;
1063 if (tlen
< sizeof(tcphdr_t
)) {
1064 fin
->fin_flx
|= FI_BAD
;
1068 flags
= tcp
->th_flags
;
1069 fin
->fin_tcpf
= tcp
->th_flags
;
1072 * If the urgent flag is set, then the urgent pointer must
1073 * also be set and vice versa. Good TCP packets do not have
1074 * just one of these set.
1076 if ((flags
& TH_URG
) != 0 && (tcp
->th_urp
== 0)) {
1077 fin
->fin_flx
|= FI_BAD
;
1078 } else if ((flags
& TH_URG
) == 0 && (tcp
->th_urp
!= 0)) {
1079 /* Ignore this case, it shows up in "real" traffic with */
1080 /* bogus values in the urgent pointer field. */
1081 flags
= flags
; /* LINT */
1082 } else if (((flags
& (TH_SYN
|TH_FIN
)) != 0) &&
1083 ((flags
& (TH_RST
|TH_ACK
)) == TH_RST
)) {
1084 /* TH_FIN|TH_RST|TH_ACK seems to appear "naturally" */
1085 fin
->fin_flx
|= FI_BAD
;
1086 } else if (!(flags
& TH_ACK
)) {
1088 * If the ack bit isn't set, then either the SYN or
1089 * RST bit must be set. If the SYN bit is set, then
1090 * we expect the ACK field to be 0. If the ACK is
1091 * not set and if URG, PSH or FIN are set, consdier
1092 * that to indicate a bad TCP packet.
1094 if ((flags
== TH_SYN
) && (tcp
->th_ack
!= 0)) {
1096 * Cisco PIX sets the ACK field to a random value.
1097 * In light of this, do not set FI_BAD until a patch
1098 * is available from Cisco to ensure that
1099 * interoperability between existing systems is
1102 /*fin->fin_flx |= FI_BAD*/;
1103 flags
= flags
; /* LINT */
1104 } else if (!(flags
& (TH_RST
|TH_SYN
))) {
1105 fin
->fin_flx
|= FI_BAD
;
1106 } else if ((flags
& (TH_URG
|TH_PUSH
|TH_FIN
)) != 0) {
1107 fin
->fin_flx
|= FI_BAD
;
1112 * At this point, it's not exactly clear what is to be gained by
1113 * marking up which TCP options are and are not present. The one we
1114 * are most interested in is the TCP window scale. This is only in
1115 * a SYN packet [RFC1323] so we don't need this here...?
1116 * Now if we were to analyse the header for passive fingerprinting,
1117 * then that might add some weight to adding this...
1119 if (tlen
== sizeof(tcphdr_t
))
1122 if (frpr_pullup(fin
, tlen
) == -1)
1127 s
= (u_char
*)(tcp
+ 1);
1128 off
= IP_HL(ip
) << 2;
1130 if (fin
->fin_mp
!= NULL
) {
1131 mb_t
*m
= *fin
->fin_mp
;
1133 if (off
+ tlen
> M_LEN(m
))
1137 for (tlen
-= (int)sizeof(*tcp
); tlen
> 0; ) {
1141 else if (opt
== TCPOPT_NOP
)
1147 if (ol
< 2 || ol
> tlen
)
1151 for (i
= 9, mv
= 4; mv
>= 0; ) {
1153 if (opt
== (u_char
)op
->ol_val
) {
1154 optmsk
|= op
->ol_bit
;
1166 /* ------------------------------------------------------------------------ */
1167 /* Function: frpr_udpcommon */
1169 /* Parameters: fin(I) - pointer to packet information */
1171 /* Extract the UDP source and destination ports, if present. If compiled */
1172 /* with IPFILTER_CKSUM, check to see if the UDP checksum is valid. */
1173 /* ------------------------------------------------------------------------ */
1174 static INLINE
void frpr_udpcommon(fin
)
1179 fin
->fin_flx
|= FI_TCPUDP
;
1181 if (!fin
->fin_off
&& (fin
->fin_dlen
> 3)) {
1182 if (frpr_pullup(fin
, sizeof(*udp
)) == -1) {
1183 fin
->fin_flx
|= FI_SHORT
;
1189 fin
->fin_sport
= ntohs(udp
->uh_sport
);
1190 fin
->fin_dport
= ntohs(udp
->uh_dport
);
1195 /* ------------------------------------------------------------------------ */
1196 /* Function: frpr_tcp */
1198 /* Parameters: fin(I) - pointer to packet information */
1201 /* Analyse the packet for IPv4/TCP properties. */
1202 /* ------------------------------------------------------------------------ */
1203 static INLINE
void frpr_tcp(fin
)
1209 frpr_short(fin
, sizeof(tcphdr_t
));
1211 frpr_tcpcommon(fin
);
1215 /* ------------------------------------------------------------------------ */
1216 /* Function: frpr_udp */
1218 /* Parameters: fin(I) - pointer to packet information */
1221 /* Analyse the packet for IPv4/UDP properties. */
1222 /* ------------------------------------------------------------------------ */
1223 static INLINE
void frpr_udp(fin
)
1229 frpr_short(fin
, sizeof(udphdr_t
));
1231 frpr_udpcommon(fin
);
1235 /* ------------------------------------------------------------------------ */
1236 /* Function: frpr_esp */
1238 /* Parameters: fin(I) - pointer to packet information */
1240 /* Analyse the packet for ESP properties. */
1241 /* The minimum length is taken to be the SPI (32bits) plus a tail (32bits) */
1242 /* even though the newer ESP packets must also have a sequence number that */
1243 /* is 32bits as well, it is not possible(?) to determine the version from a */
1244 /* simple packet header. */
1245 /* ------------------------------------------------------------------------ */
1246 static INLINE
void frpr_esp(fin
)
1249 if ((fin
->fin_off
== 0) && (frpr_pullup(fin
, 8) == -1))
1256 /* ------------------------------------------------------------------------ */
1257 /* Function: frpr_ah */
1259 /* Parameters: fin(I) - pointer to packet information */
1261 /* Analyse the packet for AH properties. */
1262 /* The minimum length is taken to be the combination of all fields in the */
1263 /* header being present and no authentication data (null algorithm used.) */
1264 /* ------------------------------------------------------------------------ */
1265 static INLINE
void frpr_ah(fin
)
1271 if ((fin
->fin_off
== 0) && (frpr_pullup(fin
, sizeof(*ah
)) == -1))
1274 ah
= (authhdr_t
*)fin
->fin_dp
;
1276 len
= (ah
->ah_plen
+ 2) << 2;
1277 frpr_short(fin
, len
);
1281 /* ------------------------------------------------------------------------ */
1282 /* Function: frpr_gre */
1284 /* Parameters: fin(I) - pointer to packet information */
1286 /* Analyse the packet for GRE properties. */
1287 /* ------------------------------------------------------------------------ */
1288 static INLINE
void frpr_gre(fin
)
1293 if ((fin
->fin_off
== 0) && (frpr_pullup(fin
, sizeof(grehdr_t
)) == -1))
1296 frpr_short(fin
, sizeof(grehdr_t
));
1298 if (fin
->fin_off
== 0) {
1300 if (GRE_REV(gre
->gr_flags
) == 1)
1301 fin
->fin_data
[0] = gre
->gr_call
;
1306 /* ------------------------------------------------------------------------ */
1307 /* Function: frpr_ipv4hdr */
1309 /* Parameters: fin(I) - pointer to packet information */
1312 /* Analyze the IPv4 header and set fields in the fr_info_t structure. */
1313 /* Check all options present and flag their presence if any exist. */
1314 /* ------------------------------------------------------------------------ */
1315 static INLINE
void frpr_ipv4hdr(fin
)
1318 u_short optmsk
= 0, secmsk
= 0, auth
= 0;
1319 int hlen
, ol
, mv
, p
, i
;
1320 const struct optlist
*op
;
1327 hlen
= fin
->fin_hlen
;
1332 fi
->fi_tos
= ip
->ip_tos
;
1333 fin
->fin_id
= ip
->ip_id
;
1336 /* Get both TTL and protocol */
1337 fi
->fi_p
= ip
->ip_p
;
1338 fi
->fi_ttl
= ip
->ip_ttl
;
1340 (*(((u_short
*)fi
) + 1)) = (*(((u_short
*)ip
) + 4));
1343 /* Zero out bits not used in IPv6 address */
1344 fi
->fi_src
.i6
[1] = 0;
1345 fi
->fi_src
.i6
[2] = 0;
1346 fi
->fi_src
.i6
[3] = 0;
1347 fi
->fi_dst
.i6
[1] = 0;
1348 fi
->fi_dst
.i6
[2] = 0;
1349 fi
->fi_dst
.i6
[3] = 0;
1351 fi
->fi_saddr
= ip
->ip_src
.s_addr
;
1352 fi
->fi_daddr
= ip
->ip_dst
.s_addr
;
1355 * set packet attribute flags based on the offset and
1356 * calculate the byte offset that it represents.
1358 off
&= IP_MF
|IP_OFFMASK
;
1360 int morefrag
= off
& IP_MF
;
1362 fi
->fi_flx
|= FI_FRAG
;
1364 fi
->fi_flx
|= FI_MOREFRAG
;
1367 fin
->fin_flx
|= FI_FRAGBODY
;
1369 if ((off
+ fin
->fin_dlen
> 65535) ||
1370 (fin
->fin_dlen
== 0) ||
1371 ((morefrag
!= 0) && ((fin
->fin_dlen
& 7) != 0))) {
1373 * The length of the packet, starting at its
1374 * offset cannot exceed 65535 (0xffff) as the
1375 * length of an IP packet is only 16 bits.
1377 * Any fragment that isn't the last fragment
1378 * must have a length greater than 0 and it
1379 * must be an even multiple of 8.
1381 fi
->fi_flx
|= FI_BAD
;
1388 * Call per-protocol setup and checking
1417 * If it is a standard IP header (no options), set the flag fields
1418 * which relate to options to 0.
1420 if (hlen
== sizeof(*ip
)) {
1428 * So the IP header has some IP options attached. Walk the entire
1429 * list of options present with this packet and set flags to indicate
1430 * which ones are here and which ones are not. For the somewhat out
1431 * of date and obscure security classification options, set a flag to
1432 * represent which classification is present.
1434 fi
->fi_flx
|= FI_OPTIONS
;
1436 for (s
= (u_char
*)(ip
+ 1), hlen
-= (int)sizeof(*ip
); hlen
> 0; ) {
1440 else if (opt
== IPOPT_NOP
)
1446 if (ol
< 2 || ol
> hlen
)
1449 for (i
= 9, mv
= 4; mv
>= 0; ) {
1451 if ((opt
== (u_char
)op
->ol_val
) && (ol
> 4)) {
1452 optmsk
|= op
->ol_bit
;
1453 if (opt
== IPOPT_SECURITY
) {
1454 const struct optlist
*sp
;
1458 sec
= *(s
+ 2); /* classification */
1459 for (j
= 3, m
= 2; m
>= 0; ) {
1461 if (sec
== sp
->ol_val
) {
1462 secmsk
|= sp
->ol_bit
;
1468 if (sec
< sp
->ol_val
)
1477 if (opt
< op
->ol_val
)
1490 if (auth
&& !(auth
& 0x0100))
1492 fi
->fi_optmsk
= optmsk
;
1493 fi
->fi_secmsk
= secmsk
;
1498 /* ------------------------------------------------------------------------ */
1499 /* Function: fr_makefrip */
1500 /* Returns: int - 1 == hdr checking error, 0 == OK */
1501 /* Parameters: hlen(I) - length of IP packet header */
1502 /* ip(I) - pointer to the IP header */
1503 /* fin(IO) - pointer to packet information */
1505 /* Compact the IP header into a structure which contains just the info. */
1506 /* which is useful for comparing IP headers with and store this information */
1507 /* in the fr_info_t structure pointer to by fin. At present, it is assumed */
1508 /* this function will be called with either an IPv4 or IPv6 packet. */
1509 /* ------------------------------------------------------------------------ */
1510 int fr_makefrip(hlen
, ip
, fin
)
1518 fin
->fin_hlen
= (u_short
)hlen
;
1520 fin
->fin_rule
= 0xffffffff;
1521 fin
->fin_group
[0] = -1;
1522 fin
->fin_group
[1] = '\0';
1523 fin
->fin_dlen
= fin
->fin_plen
- hlen
;
1524 fin
->fin_dp
= (char *)ip
+ hlen
;
1533 if (fin
->fin_ip
== NULL
)
1539 /* ------------------------------------------------------------------------ */
1540 /* Function: fr_portcheck */
1541 /* Returns: int - 1 == port matched, 0 == port match failed */
1542 /* Parameters: frp(I) - pointer to port check `expression' */
1543 /* pop(I) - pointer to port number to evaluate */
1545 /* Perform a comparison of a port number against some other(s), using a */
1546 /* structure with compare information stored in it. */
1547 /* ------------------------------------------------------------------------ */
1548 static INLINE
int fr_portcheck(frp
, pop
)
1559 * Do opposite test to that required and continue if that succeeds.
1561 switch (frp
->frp_cmp
)
1564 if (tup
!= po
) /* EQUAL */
1568 if (tup
== po
) /* NOTEQUAL */
1572 if (tup
>= po
) /* LESSTHAN */
1576 if (tup
<= po
) /* GREATERTHAN */
1580 if (tup
> po
) /* LT or EQ */
1584 if (tup
< po
) /* GT or EQ */
1588 if (tup
>= po
&& tup
<= frp
->frp_top
) /* Out of range */
1592 if (tup
<= po
|| tup
>= frp
->frp_top
) /* In range */
1596 if (tup
< po
|| tup
> frp
->frp_top
) /* Inclusive range */
1606 /* ------------------------------------------------------------------------ */
1607 /* Function: fr_tcpudpchk */
1608 /* Returns: int - 1 == protocol matched, 0 == check failed */
1609 /* Parameters: fin(I) - pointer to packet information */
1610 /* ft(I) - pointer to structure with comparison data */
1612 /* Compares the current pcket (assuming it is TCP/UDP) information with a */
1613 /* structure containing information that we want to match against. */
1614 /* ------------------------------------------------------------------------ */
1615 int fr_tcpudpchk(fin
, ft
)
1622 * Both ports should *always* be in the first fragment.
1623 * So far, I cannot find any cases where they can not be.
1625 * compare destination ports
1628 err
= fr_portcheck(&ft
->ftu_dst
, &fin
->fin_dport
);
1631 * compare source ports
1633 if (err
&& ft
->ftu_scmp
)
1634 err
= fr_portcheck(&ft
->ftu_src
, &fin
->fin_sport
);
1637 * If we don't have all the TCP/UDP header, then how can we
1638 * expect to do any sort of match on it ? If we were looking for
1639 * TCP flags, then NO match. If not, then match (which should
1640 * satisfy the "short" class too).
1642 if (err
&& (fin
->fin_p
== IPPROTO_TCP
)) {
1643 if (fin
->fin_flx
& FI_SHORT
)
1644 return !(ft
->ftu_tcpf
| ft
->ftu_tcpfm
);
1646 * Match the flags ? If not, abort this match.
1648 if (ft
->ftu_tcpfm
&&
1649 ft
->ftu_tcpf
!= (fin
->fin_tcpf
& ft
->ftu_tcpfm
)) {
1650 FR_DEBUG(("f. %#x & %#x != %#x\n", fin
->fin_tcpf
,
1651 ft
->ftu_tcpfm
, ft
->ftu_tcpf
));
1659 /* ------------------------------------------------------------------------ */
1660 /* Function: fr_ipfcheck */
1661 /* Returns: int - 0 == match, 1 == no match */
1662 /* Parameters: fin(I) - pointer to packet information */
1663 /* fr(I) - pointer to filter rule */
1664 /* portcmp(I) - flag indicating whether to attempt matching on */
1665 /* TCP/UDP port data. */
1667 /* Check to see if a packet matches an IPFilter rule. Checks of addresses, */
1668 /* port numbers, etc, for "standard" IPFilter rules are all orchestrated in */
1669 /* this function. */
1670 /* ------------------------------------------------------------------------ */
1671 static INLINE
int fr_ipfcheck(fin
, fr
, portcmp
)
1676 u_32_t
*ld
, *lm
, *lip
;
1680 ipf_stack_t
*ifs
= fin
->fin_ifs
;
1685 lm
= (u_32_t
*)&fri
->fri_mip
;
1686 ld
= (u_32_t
*)&fri
->fri_ip
;
1689 * first 32 bits to check coversion:
1690 * IP version, TOS, TTL, protocol
1692 i
= ((*lip
& *lm
) != *ld
);
1693 FR_DEBUG(("0. %#08x & %#08x != %#08x\n",
1699 * Next 32 bits is a constructed bitmask indicating which IP options
1700 * are present (if any) in this packet.
1703 i
|= ((*lip
& *lm
) != *ld
);
1704 FR_DEBUG(("1. %#08x & %#08x != %#08x\n",
1711 * Unrolled loops (4 each, for 32 bits) for address checks.
1714 * Check the source address.
1716 #ifdef IPFILTER_LOOKUP
1717 if (fr
->fr_satype
== FRI_LOOKUP
) {
1718 fin
->fin_flx
|= FI_DONTCACHE
;
1719 i
= (*fr
->fr_srcfunc
)(fr
->fr_srcptr
, fi
->fi_v
, lip
, fin
, ifs
);
1727 i
= ((*lip
& *lm
) != *ld
);
1728 FR_DEBUG(("2a. %#08x & %#08x != %#08x\n",
1730 if (fi
->fi_v
== 6) {
1732 i
|= ((*lip
& *lm
) != *ld
);
1733 FR_DEBUG(("2b. %#08x & %#08x != %#08x\n",
1736 i
|= ((*lip
& *lm
) != *ld
);
1737 FR_DEBUG(("2c. %#08x & %#08x != %#08x\n",
1740 i
|= ((*lip
& *lm
) != *ld
);
1741 FR_DEBUG(("2d. %#08x & %#08x != %#08x\n",
1748 #ifdef IPFILTER_LOOKUP
1751 i
^= (fr
->fr_flags
& FR_NOTSRCIP
) >> 6;
1756 * Check the destination address.
1759 #ifdef IPFILTER_LOOKUP
1760 if (fr
->fr_datype
== FRI_LOOKUP
) {
1761 fin
->fin_flx
|= FI_DONTCACHE
;
1762 i
= (*fr
->fr_dstfunc
)(fr
->fr_dstptr
, fi
->fi_v
, lip
, fin
, ifs
);
1770 i
= ((*lip
& *lm
) != *ld
);
1771 FR_DEBUG(("3a. %#08x & %#08x != %#08x\n",
1773 if (fi
->fi_v
== 6) {
1775 i
|= ((*lip
& *lm
) != *ld
);
1776 FR_DEBUG(("3b. %#08x & %#08x != %#08x\n",
1779 i
|= ((*lip
& *lm
) != *ld
);
1780 FR_DEBUG(("3c. %#08x & %#08x != %#08x\n",
1783 i
|= ((*lip
& *lm
) != *ld
);
1784 FR_DEBUG(("3d. %#08x & %#08x != %#08x\n",
1791 #ifdef IPFILTER_LOOKUP
1794 i
^= (fr
->fr_flags
& FR_NOTDSTIP
) >> 7;
1798 * IP addresses matched. The next 32bits contains:
1799 * mast of old IP header security & authentication bits.
1802 i
|= ((*lip
& *lm
) != *ld
);
1803 FR_DEBUG(("4. %#08x & %#08x != %#08x\n",
1807 * Next we have 32 bits of packet flags.
1810 i
|= ((*lip
& *lm
) != *ld
);
1811 FR_DEBUG(("5. %#08x & %#08x != %#08x\n",
1816 * If a fragment, then only the first has what we're
1817 * looking for here...
1820 if (!fr_tcpudpchk(fin
, &fr
->fr_tuc
))
1823 if (fr
->fr_dcmp
|| fr
->fr_scmp
||
1824 fr
->fr_tcpf
|| fr
->fr_tcpfm
)
1826 if (fr
->fr_icmpm
|| fr
->fr_icmp
) {
1827 if (((fi
->fi_p
!= IPPROTO_ICMP
) &&
1828 (fi
->fi_p
!= IPPROTO_ICMPV6
)) ||
1829 fin
->fin_off
|| (fin
->fin_dlen
< 2))
1831 else if ((fin
->fin_data
[0] & fr
->fr_icmpm
) !=
1833 FR_DEBUG(("i. %#x & %#x != %#x\n",
1835 fr
->fr_icmpm
, fr
->fr_icmp
));
1845 /* ------------------------------------------------------------------------ */
1846 /* Function: fr_scanlist */
1847 /* Returns: int - result flags of scanning filter list */
1848 /* Parameters: fin(I) - pointer to packet information */
1849 /* pass(I) - default result to return for filtering */
1851 /* Check the input/output list of rules for a match to the current packet. */
1852 /* If a match is found, the value of fr_flags from the rule becomes the */
1853 /* return value and fin->fin_fr points to the matched rule. */
1855 /* This function may be called recusively upto 16 times (limit inbuilt.) */
1856 /* When unwinding, it should finish up with fin_depth as 0. */
1858 /* Could be per interface, but this gets real nasty when you don't have, */
1859 /* or can't easily change, the kernel source code to . */
1860 /* ------------------------------------------------------------------------ */
1861 int fr_scanlist(fin
, pass
)
1865 int rulen
, portcmp
, off
, logged
, skip
;
1866 struct frentry
*fr
, *fnext
;
1867 u_32_t passt
, passo
;
1868 ipf_stack_t
*ifs
= fin
->fin_ifs
;
1871 * Do not allow nesting deeper than 16 levels.
1873 if (fin
->fin_depth
>= 16)
1879 * If there are no rules in this list, return now.
1891 if ((fin
->fin_flx
& FI_TCPUDP
) && (fin
->fin_dlen
> 3) && !off
)
1894 for (rulen
= 0; fr
; fr
= fnext
, rulen
++) {
1895 fnext
= fr
->fr_next
;
1897 FR_VERBOSE(("%d (%#x)\n", skip
, fr
->fr_flags
));
1903 * In all checks below, a null (zero) value in the
1904 * filter struture is taken to mean a wildcard.
1906 * check that we are working for the right interface
1909 if (fr
->fr_ifa
&& fr
->fr_ifa
!= fin
->fin_ifp
)
1912 if (opts
& (OPT_VERBOSE
|OPT_DEBUG
))
1914 FR_VERBOSE(("%c", FR_ISSKIP(pass
) ? 's' :
1915 FR_ISPASS(pass
) ? 'p' :
1916 FR_ISACCOUNT(pass
) ? 'A' :
1917 FR_ISAUTH(pass
) ? 'a' :
1918 (pass
& FR_NOMATCH
) ? 'n' :'b'));
1919 if (fr
->fr_ifa
&& fr
->fr_ifa
!= fin
->fin_ifp
)
1924 switch (fr
->fr_type
)
1927 case FR_T_IPF
|FR_T_BUILTIN
:
1928 if (fr_ipfcheck(fin
, fr
, portcmp
))
1931 #if defined(IPFILTER_BPF)
1933 case FR_T_BPFOPC
|FR_T_BUILTIN
:
1937 if (*fin
->fin_mp
== NULL
)
1939 if (fin
->fin_v
!= fr
->fr_v
)
1941 mc
= (u_char
*)fin
->fin_m
;
1942 if (!bpf_filter(fr
->fr_data
, mc
, fin
->fin_plen
, 0))
1947 case FR_T_CALLFUNC
|FR_T_BUILTIN
:
1951 f
= (*fr
->fr_func
)(fin
, &pass
);
1962 if ((fin
->fin_out
== 0) && (fr
->fr_nattag
.ipt_num
[0] != 0)) {
1963 if (fin
->fin_nattag
== NULL
)
1965 if (fr_matchtag(&fr
->fr_nattag
, fin
->fin_nattag
) == 0)
1968 FR_VERBOSE(("=%s.%d *", fr
->fr_group
, rulen
));
1970 passt
= fr
->fr_flags
;
1973 * Allowing a rule with the "keep state" flag set to match
1974 * packets that have been tagged "out of window" by the TCP
1975 * state tracking is foolish as the attempt to add a new
1976 * state entry to the table will fail.
1978 if ((passt
& FR_KEEPSTATE
) && (fin
->fin_flx
& FI_OOW
))
1982 * If the rule is a "call now" rule, then call the function
1983 * in the rule, if it exists and use the results from that.
1984 * If the function pointer is bad, just make like we ignore
1985 * it, except for increasing the hit counter.
1987 IPF_BUMP(fr
->fr_hits
);
1988 fr
->fr_bytes
+= (U_QUAD_T
)fin
->fin_plen
;
1989 if ((passt
& FR_CALLNOW
) != 0) {
1990 if ((fr
->fr_func
!= NULL
) &&
1991 (fr
->fr_func
!= (ipfunc_t
)-1)) {
1996 fr
= (*fr
->fr_func
)(fin
, &passt
);
2001 passt
= fr
->fr_flags
;
2010 * Just log this packet...
2012 if ((passt
& FR_LOGMASK
) == FR_LOG
) {
2013 if (ipflog(fin
, passt
) == -1) {
2014 if (passt
& FR_LOGORBLOCK
) {
2015 passt
&= ~FR_CMDMASK
;
2016 passt
|= FR_BLOCK
|FR_QUICK
;
2018 IPF_BUMP(ifs
->ifs_frstats
[fin
->fin_out
].fr_skip
);
2020 IPF_BUMP(ifs
->ifs_frstats
[fin
->fin_out
].fr_pkl
);
2023 #endif /* IPFILTER_LOG */
2025 if (FR_ISSKIP(passt
))
2027 else if ((passt
& FR_LOGMASK
) != FR_LOG
)
2029 if (passt
& (FR_RETICMP
|FR_FAKEICMP
))
2030 fin
->fin_icode
= fr
->fr_icode
;
2031 FR_DEBUG(("pass %#x\n", pass
));
2032 fin
->fin_rule
= rulen
;
2033 (void) strncpy(fin
->fin_group
, fr
->fr_group
, FR_GROUPLEN
);
2034 if (fr
->fr_grp
!= NULL
) {
2035 fin
->fin_fr
= *fr
->fr_grp
;
2036 pass
= fr_scanlist(fin
, pass
);
2037 if (fin
->fin_fr
== NULL
) {
2038 fin
->fin_rule
= rulen
;
2039 (void) strncpy(fin
->fin_group
, fr
->fr_group
,
2043 if (fin
->fin_flx
& FI_DONTCACHE
)
2047 if (pass
& FR_QUICK
) {
2049 * Finally, if we've asked to track state for this
2050 * packet, set it up. Add state for "quick" rules
2051 * here so that if the action fails we can consider
2052 * the rule to "not match" and keep on processing
2055 if ((pass
& FR_KEEPSTATE
) &&
2056 !(fin
->fin_flx
& FI_STATE
)) {
2057 int out
= fin
->fin_out
;
2059 if (fr_addstate(fin
, NULL
, 0) != NULL
) {
2060 IPF_BUMP(ifs
->ifs_frstats
[out
].fr_ads
);
2062 IPF_BUMP(ifs
->ifs_frstats
[out
].fr_bads
);
2071 fin
->fin_flx
|= FI_DONTCACHE
;
2077 /* ------------------------------------------------------------------------ */
2078 /* Function: fr_acctpkt */
2079 /* Returns: frentry_t* - always returns NULL */
2080 /* Parameters: fin(I) - pointer to packet information */
2081 /* passp(IO) - pointer to current/new filter decision (unused) */
2083 /* Checks a packet against accounting rules, if there are any for the given */
2084 /* IP protocol version. */
2086 /* N.B.: this function returns NULL to match the prototype used by other */
2087 /* functions called from the IPFilter "mainline" in fr_check(). */
2088 /* ------------------------------------------------------------------------ */
2089 frentry_t
*fr_acctpkt(fin
, passp
)
2093 char group
[FR_GROUPLEN
];
2094 frentry_t
*fr
, *frsave
;
2096 ipf_stack_t
*ifs
= fin
->fin_ifs
;
2100 if (fin
->fin_v
== 6)
2101 fr
= ifs
->ifs_ipacct6
[fin
->fin_out
][ifs
->ifs_fr_active
];
2104 fr
= ifs
->ifs_ipacct
[fin
->fin_out
][ifs
->ifs_fr_active
];
2107 frsave
= fin
->fin_fr
;
2108 bcopy(fin
->fin_group
, group
, FR_GROUPLEN
);
2109 rulen
= fin
->fin_rule
;
2111 pass
= fr_scanlist(fin
, FR_NOMATCH
);
2112 if (FR_ISACCOUNT(pass
)) {
2113 IPF_BUMP(ifs
->ifs_frstats
[0].fr_acct
);
2115 fin
->fin_fr
= frsave
;
2116 bcopy(group
, fin
->fin_group
, FR_GROUPLEN
);
2117 fin
->fin_rule
= rulen
;
2123 /* ------------------------------------------------------------------------ */
2124 /* Function: fr_firewall */
2125 /* Returns: frentry_t* - returns pointer to matched rule, if no matches */
2126 /* were found, returns NULL. */
2127 /* Parameters: fin(I) - pointer to packet information */
2128 /* passp(IO) - pointer to current/new filter decision (unused) */
2130 /* Applies an appropriate set of firewall rules to the packet, to see if */
2131 /* there are any matches. The first check is to see if a match can be seen */
2132 /* in the cache. If not, then search an appropriate list of rules. Once a */
2133 /* matching rule is found, take any appropriate actions as defined by the */
2134 /* rule - except logging. */
2135 /* ------------------------------------------------------------------------ */
2136 static frentry_t
*fr_firewall(fin
, passp
)
2144 ipf_stack_t
*ifs
= fin
->fin_ifs
;
2150 if (fin
->fin_v
== 6)
2151 fin
->fin_fr
= ifs
->ifs_ipfilter6
[out
][ifs
->ifs_fr_active
];
2154 fin
->fin_fr
= ifs
->ifs_ipfilter
[out
][ifs
->ifs_fr_active
];
2157 * If there are no rules loaded skip all checks and return.
2159 if (fin
->fin_fr
== NULL
) {
2161 if ((pass
& FR_NOMATCH
)) {
2162 IPF_BUMP(ifs
->ifs_frstats
[out
].fr_nom
);
2168 fc
= &ifs
->ifs_frcache
[out
][CACHE_HASH(fin
)];
2169 READ_ENTER(&ifs
->ifs_ipf_frcache
);
2170 if (!bcmp((char *)fin
, (char *)fc
, FI_CSIZE
)) {
2172 * copy cached data so we can unlock the mutexes earlier.
2174 bcopy((char *)fc
, (char *)fin
, FI_COPYSIZE
);
2175 RWLOCK_EXIT(&ifs
->ifs_ipf_frcache
);
2176 IPF_BUMP(ifs
->ifs_frstats
[out
].fr_chit
);
2178 if ((fr
= fin
->fin_fr
) != NULL
) {
2179 IPF_BUMP(fr
->fr_hits
);
2180 fr
->fr_bytes
+= (U_QUAD_T
)fin
->fin_plen
;
2181 pass
= fr
->fr_flags
;
2184 RWLOCK_EXIT(&ifs
->ifs_ipf_frcache
);
2186 pass
= fr_scanlist(fin
, ifs
->ifs_fr_pass
);
2188 if (((pass
& FR_KEEPSTATE
) == 0) &&
2189 ((fin
->fin_flx
& FI_DONTCACHE
) == 0)) {
2190 WRITE_ENTER(&ifs
->ifs_ipf_frcache
);
2191 bcopy((char *)fin
, (char *)fc
, FI_COPYSIZE
);
2192 RWLOCK_EXIT(&ifs
->ifs_ipf_frcache
);
2198 if ((pass
& FR_NOMATCH
)) {
2199 IPF_BUMP(ifs
->ifs_frstats
[out
].fr_nom
);
2203 * Apply packets per second rate-limiting to a rule as required.
2205 if ((fr
!= NULL
) && (fr
->fr_pps
!= 0) &&
2206 !ppsratecheck(&fr
->fr_lastpkt
, &fr
->fr_curpps
, fr
->fr_pps
)) {
2207 pass
&= ~(FR_CMDMASK
|FR_DUP
|FR_RETICMP
|FR_RETRST
);
2209 IPF_BUMP(ifs
->ifs_frstats
[out
].fr_ppshit
);
2213 * If we fail to add a packet to the authorization queue, then we
2214 * drop the packet later. However, if it was added then pretend
2215 * we've dropped it already.
2217 if (FR_ISAUTH(pass
)) {
2218 if (fr_newauth(fin
->fin_m
, fin
) != 0) {
2220 fin
->fin_m
= *fin
->fin_mp
= NULL
;
2226 fin
->fin_error
= ENOSPC
;
2229 if ((fr
!= NULL
) && (fr
->fr_func
!= NULL
) &&
2230 (fr
->fr_func
!= (ipfunc_t
)-1) && !(pass
& FR_CALLNOW
))
2231 (void) (*fr
->fr_func
)(fin
, &pass
);
2234 * If a rule is a pre-auth rule, check again in the list of rules
2235 * loaded for authenticated use. It does not particulary matter
2236 * if this search fails because a "preauth" result, from a rule,
2237 * is treated as "not a pass", hence the packet is blocked.
2239 if (FR_ISPREAUTH(pass
)) {
2240 if ((fin
->fin_fr
= ifs
->ifs_ipauth
) != NULL
)
2241 pass
= fr_scanlist(fin
, ifs
->ifs_fr_pass
);
2245 * If the rule has "keep frag" and the packet is actually a fragment,
2246 * then create a fragment state entry.
2248 if ((pass
& (FR_KEEPFRAG
|FR_KEEPSTATE
)) == FR_KEEPFRAG
) {
2249 if (fin
->fin_flx
& FI_FRAG
) {
2250 if (fr_newfrag(fin
, pass
) == -1) {
2251 IPF_BUMP(ifs
->ifs_frstats
[out
].fr_bnfr
);
2253 IPF_BUMP(ifs
->ifs_frstats
[out
].fr_nfr
);
2256 IPF_BUMP(ifs
->ifs_frstats
[out
].fr_cfr
);
2261 * Finally, if we've asked to track state for this packet, set it up.
2263 if ((pass
& FR_KEEPSTATE
) && !(fin
->fin_flx
& FI_STATE
)) {
2264 if (fr_addstate(fin
, NULL
, 0) != NULL
) {
2265 IPF_BUMP(ifs
->ifs_frstats
[out
].fr_ads
);
2267 IPF_BUMP(ifs
->ifs_frstats
[out
].fr_bads
);
2268 if (FR_ISPASS(pass
)) {
2269 pass
&= ~FR_CMDMASK
;
2283 /* ------------------------------------------------------------------------ */
2284 /* Function: fr_check */
2285 /* Returns: int - 0 == packet allowed through, */
2287 /* -1 == packet blocked */
2288 /* 1 == packet not matched */
2289 /* -2 == requires authentication */
2291 /* > 0 == filter error # for packet */
2292 /* Parameters: ip(I) - pointer to start of IPv4/6 packet */
2293 /* hlen(I) - length of header */
2294 /* ifp(I) - pointer to interface this packet is on */
2295 /* out(I) - 0 == packet going in, 1 == packet going out */
2296 /* mp(IO) - pointer to caller's buffer pointer that holds this */
2298 /* Solaris & HP-UX ONLY : */
2299 /* qpi(I) - pointer to STREAMS queue information for this */
2300 /* interface & direction. */
2302 /* fr_check() is the master function for all IPFilter packet processing. */
2303 /* It orchestrates: Network Address Translation (NAT), checking for packet */
2304 /* authorisation (or pre-authorisation), presence of related state info., */
2305 /* generating log entries, IP packet accounting, routing of packets as */
2306 /* directed by firewall rules and of course whether or not to allow the */
2307 /* packet to be further processed by the kernel. */
2309 /* For packets blocked, the contents of "mp" will be NULL'd and the buffer */
2310 /* freed. Packets passed may be returned with the pointer pointed to by */
2311 /* by "mp" changed to a new buffer. */
2312 /* ------------------------------------------------------------------------ */
2313 int fr_check(ip
, hlen
, ifp
, out
2314 #if defined(_KERNEL) && defined(MENTAT)
2328 * The above really sucks, but short of writing a diff
2331 fr_info_t
*fin
= &frinfo
;
2333 frentry_t
*fr
= NULL
;
2342 qpktinfo_t
*qpi
= qif
;
2347 pass
= ifs
->ifs_fr_pass
;
2350 * The first part of fr_check() deals with making sure that what goes
2351 * into the filtering engine makes some sense. Information about the
2352 * the packet is distilled, collected into a fr_info_t structure and
2353 * the an attempt to ensure the buffer the packet is in is big enough
2354 * to hold all the required packet headers.
2363 if (ifs
->ifs_fr_running
<= 0) {
2367 bzero((char *)fin
, sizeof(*fin
));
2370 fin
->fin_flx
= qpi
->qpi_flags
& (FI_NOCKSUM
|FI_MBCAST
|FI_MULTICAST
|
2379 # if defined(M_MCAST)
2380 if ((m
->m_flags
& M_MCAST
) != 0)
2381 fin
->fin_flx
|= FI_MBCAST
|FI_MULTICAST
;
2383 # if defined(M_MLOOP)
2384 if ((m
->m_flags
& M_MLOOP
) != 0)
2385 fin
->fin_flx
|= FI_MBCAST
|FI_MULTICAST
;
2387 # if defined(M_BCAST)
2388 if ((m
->m_flags
& M_BCAST
) != 0)
2389 fin
->fin_flx
|= FI_MBCAST
|FI_BROADCAST
;
2391 # ifdef M_CANFASTFWD
2393 * XXX For now, IP Filter and fast-forwarding of cached flows
2394 * XXX are mutually exclusive. Eventually, IP Filter should
2395 * XXX get a "can-fast-forward" filter rule.
2397 m
->m_flags
&= ~M_CANFASTFWD
;
2398 # endif /* M_CANFASTFWD */
2399 # ifdef CSUM_DELAY_DATA
2401 * disable delayed checksums.
2403 if (m
->m_pkthdr
.csum_flags
& CSUM_DELAY_DATA
) {
2404 in_delayed_cksum(m
);
2405 m
->m_pkthdr
.csum_flags
&= ~CSUM_DELAY_DATA
;
2407 # endif /* CSUM_DELAY_DATA */
2408 # endif /* MENTAT */
2411 bzero((char *)fin
, sizeof(*fin
));
2413 #endif /* _KERNEL */
2421 fin
->fin_error
= ENETUNREACH
;
2422 fin
->fin_hlen
= (u_short
)hlen
;
2423 fin
->fin_dp
= (char *)ip
+ hlen
;
2424 fin
->fin_ipoff
= (char *)ip
- MTOD(m
, char *);
2431 IPF_BUMP(ifs
->ifs_frstats
[out
].fr_ipv6
);
2433 * Jumbo grams are quite likely too big for internal buffer
2434 * structures to handle comfortably, for now, so just drop
2438 fin
->fin_plen
= ntohs(ip6
->ip6_plen
);
2439 if (fin
->fin_plen
== 0) {
2440 READ_ENTER(&ifs
->ifs_ipf_mutex
);
2441 pass
= FR_BLOCK
|FR_NOMATCH
;
2444 fin
->fin_plen
+= sizeof(ip6_t
);
2448 #if (OpenBSD >= 200311) && defined(_KERNEL)
2449 ip
->ip_len
= ntohs(ip
->ip_len
);
2450 ip
->ip_off
= ntohs(ip
->ip_off
);
2452 fin
->fin_plen
= ip
->ip_len
;
2455 if (fr_makefrip(hlen
, ip
, fin
) == -1) {
2456 READ_ENTER(&ifs
->ifs_ipf_mutex
);
2462 * For at least IPv6 packets, if a m_pullup() fails then this pointer
2463 * becomes NULL and so we have no packet to free.
2465 if (*fin
->fin_mp
== NULL
)
2471 if (ifs
->ifs_fr_chksrc
&& !fr_verifysrc(fin
)) {
2472 IPF_BUMP(ifs
->ifs_frstats
[0].fr_badsrc
);
2473 fin
->fin_flx
|= FI_BADSRC
;
2476 if (fin
->fin_ip
->ip_ttl
< ifs
->ifs_fr_minttl
) {
2477 IPF_BUMP(ifs
->ifs_frstats
[0].fr_badttl
);
2478 fin
->fin_flx
|= FI_LOWTTL
;
2485 if (ifs
->ifs_fr_chksrc
&& !fr_verifysrc(fin
)) {
2486 IPF_BUMP(ifs
->ifs_frstats
[0].fr_badsrc
);
2487 fin
->fin_flx
|= FI_BADSRC
;
2490 if (ip6
->ip6_hlim
< ifs
->ifs_fr_minttl
) {
2491 IPF_BUMP(ifs
->ifs_frstats
[0].fr_badttl
);
2492 fin
->fin_flx
|= FI_LOWTTL
;
2498 if (fin
->fin_flx
& FI_SHORT
) {
2499 IPF_BUMP(ifs
->ifs_frstats
[out
].fr_short
);
2502 READ_ENTER(&ifs
->ifs_ipf_mutex
);
2505 * Check auth now. This, combined with the check below to see if apass
2506 * is 0 is to ensure that we don't count the packet twice, which can
2507 * otherwise occur when we reprocess it. As it is, we only count it
2508 * after it has no auth. table matchup. This also stops NAT from
2509 * occuring until after the packet has been auth'd.
2511 fr
= fr_checkauth(fin
, &pass
);
2516 if (fr_checknatin(fin
, &pass
) == -1) {
2517 RWLOCK_EXIT(&ifs
->ifs_ipf_mutex
);
2523 if (fr_checknat6in(fin
, &pass
) == -1) {
2524 RWLOCK_EXIT(&ifs
->ifs_ipf_mutex
);
2534 (void) fr_acctpkt(fin
, NULL
);
2537 if ((fin
->fin_flx
& (FI_FRAG
|FI_BAD
)) == FI_FRAG
)
2538 fr
= fr_knownfrag(fin
, &pass
);
2540 fr
= fr_checkstate(fin
, &pass
);
2542 if ((pass
& FR_NOMATCH
) || (fr
== NULL
))
2543 fr
= fr_firewall(fin
, &pass
);
2548 * Only count/translate packets which will be passed on, out the
2551 if (out
&& FR_ISPASS(pass
)) {
2552 (void) fr_acctpkt(fin
, NULL
);
2557 if (fr_checknatout(fin
, &pass
) == -1) {
2558 RWLOCK_EXIT(&ifs
->ifs_ipf_mutex
);
2564 if (fr_checknat6out(fin
, &pass
) == -1) {
2565 RWLOCK_EXIT(&ifs
->ifs_ipf_mutex
);
2574 if ((ifs
->ifs_fr_update_ipid
!= 0) && (v
== 4)) {
2575 if (fr_updateipid(fin
) == -1) {
2576 IPF_BUMP(ifs
->ifs_frstats
[1].fr_ipud
);
2577 pass
&= ~FR_CMDMASK
;
2580 IPF_BUMP(ifs
->ifs_frstats
[0].fr_ipud
);
2586 if ((ifs
->ifs_fr_flags
& FF_LOGGING
) || (pass
& FR_LOGMASK
)) {
2587 (void) fr_dolog(fin
, &pass
);
2592 * The FI_STATE flag is cleared here so that calling fr_checkstate
2593 * will work when called from inside of fr_fastroute. Although
2594 * there is a similar flag, FI_NATED, for NAT, it does have the same
2595 * impact on code execution.
2597 fin
->fin_flx
&= ~FI_STATE
;
2600 * Only allow FR_DUP to work if a rule matched - it makes no sense to
2601 * set FR_DUP as a "default" as there are no instructions about where
2602 * to send the packet. Use fin_m here because it may have changed
2603 * (without an update of 'm') in prior processing.
2605 if ((fr
!= NULL
) && (pass
& FR_DUP
)) {
2606 mc
= M_DUPLICATE(fin
->fin_m
);
2608 mc
->b_rptr
+= fin
->fin_ipoff
;
2613 * We don't want to send RST for packets, which are going to be
2614 * dropped, just because they don't fit into TCP window. Those packets
2615 * will be dropped silently. In other words, we want to drop packet,
2616 * while keeping session alive.
2618 if ((pass
& (FR_RETRST
|FR_RETICMP
)) && ((fin
->fin_flx
& FI_OOW
) == 0)) {
2620 * Should we return an ICMP packet to indicate error
2621 * status passing through the packet filter ?
2622 * WARNING: ICMP error packets AND TCP RST packets should
2623 * ONLY be sent in repsonse to incoming packets. Sending them
2624 * in response to outbound packets can result in a panic on
2625 * some operating systems.
2628 if (pass
& FR_RETICMP
) {
2631 if ((pass
& FR_RETMASK
) == FR_FAKEICMP
)
2635 #if defined(_KERNEL) && (SOLARIS2 >= 10)
2637 * Assume it's possible to enter insane rule:
2638 * pass return-icmp in proto udp ...
2639 * then we have no other option than to forward
2640 * packet on loopback and give up any attempt
2641 * to create a fake response.
2643 if (IPF_IS_LOOPBACK(qpi
->qpi_flags
) &&
2646 if (fr_make_icmp(fin
) == 0) {
2648 ifs
->ifs_frstats
[out
].fr_ret
);
2651 * we drop packet silently in case we
2652 * failed assemble fake response for it
2654 else if (*mp
!= NULL
) {
2660 ifs
->ifs_frstats
[out
].fr_block
);
2661 RWLOCK_EXIT(&ifs
->ifs_ipf_mutex
);
2665 #endif /* _KERNEL && SOLARIS2 >= 10 */
2667 (void) fr_send_icmp_err(ICMP_UNREACH
, fin
, dst
);
2668 IPF_BUMP(ifs
->ifs_frstats
[out
].fr_ret
);
2670 } else if (((pass
& FR_RETMASK
) == FR_RETRST
) &&
2671 !(fin
->fin_flx
& FI_SHORT
)) {
2673 #if defined(_KERNEL) && (SOLARIS2 >= 10)
2675 * Assume it's possible to enter insane rule:
2676 * pass return-rst in proto tcp ...
2677 * then we have no other option than to forward
2678 * packet on loopback and give up any attempt
2679 * to create a fake response.
2681 if (IPF_IS_LOOPBACK(qpi
->qpi_flags
) &&
2683 if (fr_make_rst(fin
) == 0) {
2685 ifs
->ifs_frstats
[out
].fr_ret
);
2687 else if (mp
!= NULL
) {
2689 * we drop packet silently in case we
2690 * failed assemble fake response for it
2697 ifs
->ifs_frstats
[out
].fr_block
);
2698 RWLOCK_EXIT(&ifs
->ifs_ipf_mutex
);
2702 #endif /* _KERNEL && _SOLARIS2 >= 10 */
2703 if (fr_send_reset(fin
) == 0) {
2704 IPF_BUMP(ifs
->ifs_frstats
[1].fr_ret
);
2708 if (pass
& FR_RETRST
)
2709 fin
->fin_error
= ECONNRESET
;
2714 * If we didn't drop off the bottom of the list of rules (and thus
2715 * the 'current' rule fr is not NULL), then we may have some extra
2716 * instructions about what to do with a packet.
2717 * Once we're finished return to our caller, freeing the packet if
2718 * we are dropping it (* BSD ONLY *).
2719 * Reassign m from fin_m as we may have a new buffer, now.
2727 fdp
= &fr
->fr_tifs
[fin
->fin_rev
];
2729 if (!out
&& (pass
& FR_FASTROUTE
)) {
2731 * For fastroute rule, no destioation interface defined
2732 * so pass NULL as the frdest_t parameter
2734 (void) fr_fastroute(m
, mp
, fin
, NULL
);
2736 } else if ((fdp
->fd_ifp
!= NULL
) &&
2737 (fdp
->fd_ifp
!= (struct ifnet
*)-1)) {
2738 /* this is for to rules: */
2739 (void) fr_fastroute(m
, mp
, fin
, fdp
);
2744 * Send a duplicated packet.
2747 #if defined(_KERNEL) && (SOLARIS2 >= 10)
2749 * We are going to compute chksum for copies of loopback packets
2750 * only. IP stack does not compute chksums at all for loopback
2751 * packets. We want to get it fixed in their copies, since those
2752 * are going to be sent to network.
2754 if (IPF_IS_LOOPBACK(qpi
->qpi_flags
))
2755 fr_calc_chksum(fin
, mc
);
2757 (void) fr_fastroute(mc
, &mc
, fin
, &fr
->fr_dif
);
2761 if (FR_ISBLOCK(pass
) && (fin
->fin_flx
& FI_NEWNAT
))
2765 * This late because the likes of fr_fastroute() use fin_fr.
2767 RWLOCK_EXIT(&ifs
->ifs_ipf_mutex
);
2770 if (!FR_ISPASS(pass
)) {
2771 IPF_BUMP(ifs
->ifs_frstats
[out
].fr_block
);
2777 IPF_BUMP(ifs
->ifs_frstats
[out
].fr_pass
);
2778 #if defined(_KERNEL) && defined(__sgi)
2779 if ((fin
->fin_hbuf
!= NULL
) &&
2780 (mtod(fin
->fin_m
, struct ip
*) != fin
->fin_ip
)) {
2781 COPYBACK(m
, 0, fin
->fin_plen
, fin
->fin_hbuf
);
2789 # if OpenBSD >= 200311
2790 if (FR_ISPASS(pass
) && (v
== 4)) {
2792 ip
->ip_len
= ntohs(ip
->ip_len
);
2793 ip
->ip_off
= ntohs(ip
->ip_off
);
2796 return (FR_ISPASS(pass
)) ? 0 : fin
->fin_error
;
2798 FR_VERBOSE(("fin_flx %#x pass %#x ", fin
->fin_flx
, pass
));
2799 if ((pass
& FR_NOMATCH
) != 0)
2802 if ((pass
& FR_RETMASK
) != 0)
2803 switch (pass
& FR_RETMASK
)
2813 switch (pass
& FR_CMDMASK
)
2827 #endif /* _KERNEL */
2832 /* ------------------------------------------------------------------------ */
2833 /* Function: fr_dolog */
2834 /* Returns: frentry_t* - returns contents of fin_fr (no change made) */
2835 /* Parameters: fin(I) - pointer to packet information */
2836 /* passp(IO) - pointer to current/new filter decision (unused) */
2838 /* Checks flags set to see how a packet should be logged, if it is to be */
2839 /* logged. Adjust statistics based on its success or not. */
2840 /* ------------------------------------------------------------------------ */
2841 frentry_t
*fr_dolog(fin
, passp
)
2847 ipf_stack_t
*ifs
= fin
->fin_ifs
;
2852 if ((ifs
->ifs_fr_flags
& FF_LOGNOMATCH
) && (pass
& FR_NOMATCH
)) {
2853 pass
|= FF_LOGNOMATCH
;
2854 IPF_BUMP(ifs
->ifs_frstats
[out
].fr_npkl
);
2856 } else if (((pass
& FR_LOGMASK
) == FR_LOGP
) ||
2857 (FR_ISPASS(pass
) && (ifs
->ifs_fr_flags
& FF_LOGPASS
))) {
2858 if ((pass
& FR_LOGMASK
) != FR_LOGP
)
2860 IPF_BUMP(ifs
->ifs_frstats
[out
].fr_ppkl
);
2862 } else if (((pass
& FR_LOGMASK
) == FR_LOGB
) ||
2863 (FR_ISBLOCK(pass
) && (ifs
->ifs_fr_flags
& FF_LOGBLOCK
))) {
2864 if ((pass
& FR_LOGMASK
) != FR_LOGB
)
2865 pass
|= FF_LOGBLOCK
;
2866 IPF_BUMP(ifs
->ifs_frstats
[out
].fr_bpkl
);
2868 if (ipflog(fin
, pass
) == -1) {
2869 IPF_BUMP(ifs
->ifs_frstats
[out
].fr_skip
);
2872 * If the "or-block" option has been used then
2873 * block the packet if we failed to log it.
2875 if ((pass
& FR_LOGORBLOCK
) &&
2877 pass
&= ~FR_CMDMASK
;
2886 #endif /* IPFILTER_LOG */
2889 /* ------------------------------------------------------------------------ */
2890 /* Function: ipf_cksum */
2891 /* Returns: u_short - IP header checksum */
2892 /* Parameters: addr(I) - pointer to start of buffer to checksum */
2893 /* len(I) - length of buffer in bytes */
2895 /* Calculate the two's complement 16 bit checksum of the buffer passed. */
2897 /* N.B.: addr should be 16bit aligned. */
2898 /* ------------------------------------------------------------------------ */
2899 u_short
ipf_cksum(addr
, len
)
2905 for (sum
= 0; len
> 1; len
-= 2)
2908 /* mop up an odd byte, if necessary */
2910 sum
+= *(u_char
*)addr
;
2913 * add back carry outs from top 16 bits to low 16 bits
2915 sum
= (sum
>> 16) + (sum
& 0xffff); /* add hi 16 to low 16 */
2916 sum
+= (sum
>> 16); /* add carry */
2917 return (u_short
)(~sum
);
2921 /* ------------------------------------------------------------------------ */
2922 /* Function: fr_cksum */
2923 /* Returns: u_short - layer 4 checksum */
2924 /* Parameters: m(I ) - pointer to buffer holding packet */
2925 /* ip(I) - pointer to IP header */
2926 /* l4proto(I) - protocol to caclulate checksum for */
2927 /* l4hdr(I) - pointer to layer 4 header */
2929 /* Calculates the TCP checksum for the packet held in "m", using the data */
2930 /* in the IP header "ip" to seed it. */
2932 /* NB: This function assumes we've pullup'd enough for all of the IP header */
2933 /* and the TCP header. We also assume that data blocks aren't allocated in */
2936 /* Expects ip_len to be in host byte order when called. */
2937 /* ------------------------------------------------------------------------ */
2938 u_short
fr_cksum(m
, ip
, l4proto
, l4hdr
)
2944 u_short
*sp
, slen
, sumsave
, l4hlen
, *csump
;
2960 * Add up IP Header portion
2963 if (IP_V(ip
) == 4) {
2965 hlen
= IP_HL(ip
) << 2;
2966 slen
= ip
->ip_len
- hlen
;
2967 sum
= htons((u_short
)l4proto
);
2969 sp
= (u_short
*)&ip
->ip_src
;
2970 sum
+= *sp
++; /* ip_src */
2972 sum
+= *sp
++; /* ip_dst */
2975 } else if (IP_V(ip
) == 6) {
2977 hlen
= sizeof(*ip6
);
2978 slen
= ntohs(ip6
->ip6_plen
);
2979 sum
= htons((u_short
)l4proto
);
2981 sp
= (u_short
*)&ip6
->ip6_src
;
2982 sum
+= *sp
++; /* ip6_src */
2990 sum
+= *sp
++; /* ip6_dst */
3004 csump
= &((udphdr_t
*)l4hdr
)->uh_sum
;
3005 l4hlen
= sizeof(udphdr_t
);
3009 csump
= &((tcphdr_t
*)l4hdr
)->th_sum
;
3010 l4hlen
= sizeof(tcphdr_t
);
3013 csump
= &((icmphdr_t
*)l4hdr
)->icmp_cksum
;
3021 if (csump
!= NULL
) {
3026 l4hlen
= l4hlen
; /* LINT */
3031 void *rp
= m
->b_rptr
;
3033 if ((unsigned char *)ip
> m
->b_rptr
&& (unsigned char *)ip
< m
->b_wptr
)
3034 m
->b_rptr
= (u_char
*)ip
;
3035 sum2
= ip_cksum(m
, hlen
, sum
); /* hlen == offset */
3037 sum2
= (sum2
& 0xffff) + (sum2
>> 16);
3038 sum2
= ~sum2
& 0xffff;
3041 # if defined(BSD) || defined(sun)
3048 sum2
= in_cksum(m
, slen
);
3056 * Both sum and sum2 are partial sums, so combine them together.
3058 sum
+= ~sum2
& 0xffff;
3059 while (sum
> 0xffff)
3060 sum
= (sum
& 0xffff) + (sum
>> 16);
3061 sum2
= ~sum
& 0xffff;
3062 # else /* defined(BSD) || defined(sun) */
3068 u_short len
= ip
->ip_len
;
3074 * Add up IP Header portion
3076 if (sp
!= (u_short
*)l4hdr
)
3077 sp
= (u_short
*)l4hdr
;
3082 sum
+= *sp
++; /* sport */
3083 sum
+= *sp
++; /* dport */
3084 sum
+= *sp
++; /* udp length */
3085 sum
+= *sp
++; /* checksum */
3089 sum
+= *sp
++; /* sport */
3090 sum
+= *sp
++; /* dport */
3091 sum
+= *sp
++; /* seq */
3093 sum
+= *sp
++; /* ack */
3095 sum
+= *sp
++; /* off */
3096 sum
+= *sp
++; /* win */
3097 sum
+= *sp
++; /* checksum */
3098 sum
+= *sp
++; /* urp */
3101 sum
= *sp
++; /* type/code */
3102 sum
+= *sp
++; /* checksum */
3108 * In case we had to copy the IP & TCP header out of mbufs,
3109 * skip over the mbuf bits which are the header
3111 if ((caddr_t
)ip
!= mtod(m
, caddr_t
)) {
3112 hlen
= (caddr_t
)sp
- (caddr_t
)ip
;
3114 add
= MIN(hlen
, m
->m_len
);
3115 sp
= (u_short
*)(mtod(m
, caddr_t
) + add
);
3117 if (add
== m
->m_len
) {
3122 sp
= mtod(m
, u_short
*);
3124 PANIC((!m
),("fr_cksum(1): not enough data"));
3130 len
-= (l4hlen
+ hlen
);
3135 if (((caddr_t
)sp
- mtod(m
, caddr_t
)) >= m
->m_len
) {
3137 PANIC((!m
),("fr_cksum(2): not enough data"));
3138 sp
= mtod(m
, u_short
*);
3140 if (((caddr_t
)(sp
+ 1) - mtod(m
, caddr_t
)) > m
->m_len
) {
3141 bytes
.c
[0] = *(u_char
*)sp
;
3143 PANIC((!m
),("fr_cksum(3): not enough data"));
3144 sp
= mtod(m
, u_short
*);
3145 bytes
.c
[1] = *(u_char
*)sp
;
3147 sp
= (u_short
*)((u_char
*)sp
+ 1);
3149 if ((u_long
)sp
& 1) {
3150 bcopy((char *)sp
++, (char *)&bytes
.s
, sizeof(bytes
.s
));
3158 sum
+= ntohs(*(u_char
*)sp
<< 8);
3160 while (sum
> 0xffff)
3161 sum
= (sum
& 0xffff) + (sum
>> 16);
3162 sum2
= (u_short
)(~sum
& 0xffff);
3164 # endif /* defined(BSD) || defined(sun) */
3165 # endif /* MENTAT */
3167 for (; slen
> 1; slen
-= 2)
3170 sum
+= ntohs(*(u_char
*)sp
<< 8);
3171 while (sum
> 0xffff)
3172 sum
= (sum
& 0xffff) + (sum
>> 16);
3173 sum2
= (u_short
)(~sum
& 0xffff);
3174 #endif /* _KERNEL */
3181 #if defined(_KERNEL) && ( ((BSD < 199103) && !defined(MENTAT)) || \
3182 defined(__sgi) ) && !defined(linux) && !defined(_AIX51)
3184 * Copyright (c) 1982, 1986, 1988, 1991, 1993
3185 * The Regents of the University of California. All rights reserved.
3187 * Redistribution and use in source and binary forms, with or without
3188 * modification, are permitted provided that the following conditions
3190 * 1. Redistributions of source code must retain the above copyright
3191 * notice, this list of conditions and the following disclaimer.
3192 * 2. Redistributions in binary form must reproduce the above copyright
3193 * notice, this list of conditions and the following disclaimer in the
3194 * documentation and/or other materials provided with the distribution.
3195 * 3. Neither the name of the University nor the names of its contributors
3196 * may be used to endorse or promote products derived from this software
3197 * without specific prior written permission.
3199 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
3200 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
3201 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
3202 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
3203 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3204 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3205 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3206 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3207 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3208 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3211 * @(#)uipc_mbuf.c 8.2 (Berkeley) 1/4/94
3212 * $Id: fil.c,v 2.243.2.64 2005/08/13 05:19:59 darrenr Exp $
3215 * Copy data from an mbuf chain starting "off" bytes from the beginning,
3216 * continuing for "len" bytes, into the indicated buffer.
3219 m_copydata(m
, off
, len
, cp
)
3227 if (off
< 0 || len
< 0)
3228 panic("m_copydata");
3231 panic("m_copydata");
3239 panic("m_copydata");
3240 count
= MIN(m
->m_len
- off
, len
);
3241 bcopy(mtod(m
, caddr_t
) + off
, cp
, count
);
3251 * Copy data from a buffer back into the indicated mbuf chain,
3252 * starting "off" bytes from the beginning, extending the mbuf
3253 * chain if necessary.
3256 m_copyback(m0
, off
, len
, cp
)
3263 struct mbuf
*m
= m0
, *n
;
3268 while (off
> (mlen
= m
->m_len
)) {
3271 if (m
->m_next
== 0) {
3272 n
= m_getclr(M_DONTWAIT
, m
->m_type
);
3275 n
->m_len
= min(MLEN
, len
+ off
);
3281 mlen
= min(m
->m_len
- off
, len
);
3282 bcopy(cp
, off
+ mtod(m
, caddr_t
), (unsigned)mlen
);
3290 if (m
->m_next
== 0) {
3291 n
= m_get(M_DONTWAIT
, m
->m_type
);
3294 n
->m_len
= min(MLEN
, len
);
3301 if (((m
= m0
)->m_flags
& M_PKTHDR
) && (m
->m_pkthdr
.len
< totlen
))
3302 m
->m_pkthdr
.len
= totlen
;
3306 #endif /* (_KERNEL) && ( ((BSD < 199103) && !MENTAT) || __sgi) */
3309 /* ------------------------------------------------------------------------ */
3310 /* Function: fr_findgroup */
3311 /* Returns: frgroup_t * - NULL = group not found, else pointer to group */
3312 /* Parameters: group(I) - group name to search for */
3313 /* unit(I) - device to which this group belongs */
3314 /* set(I) - which set of rules (inactive/inactive) this is */
3315 /* fgpp(O) - pointer to place to store pointer to the pointer */
3316 /* to where to add the next (last) group or where */
3317 /* to delete group from. */
3319 /* Search amongst the defined groups for a particular group number. */
3320 /* ------------------------------------------------------------------------ */
3321 frgroup_t
*fr_findgroup(group
, unit
, set
, fgpp
, ifs
)
3328 frgroup_t
*fg
, **fgp
;
3331 * Which list of groups to search in is dependent on which list of
3332 * rules are being operated on.
3334 fgp
= &ifs
->ifs_ipfgroups
[unit
][set
];
3336 while ((fg
= *fgp
) != NULL
) {
3337 if (strncmp(group
, fg
->fg_name
, FR_GROUPLEN
) == 0)
3348 /* ------------------------------------------------------------------------ */
3349 /* Function: fr_addgroup */
3350 /* Returns: frgroup_t * - NULL == did not create group, */
3351 /* != NULL == pointer to the group */
3352 /* Parameters: num(I) - group number to add */
3353 /* head(I) - rule pointer that is using this as the head */
3354 /* flags(I) - rule flags which describe the type of rule it is */
3355 /* unit(I) - device to which this group will belong to */
3356 /* set(I) - which set of rules (inactive/inactive) this is */
3357 /* Write Locks: ipf_mutex */
3359 /* Add a new group head, or if it already exists, increase the reference */
3361 /* ------------------------------------------------------------------------ */
3362 frgroup_t
*fr_addgroup(group
, head
, flags
, unit
, set
, ifs
)
3370 frgroup_t
*fg
, **fgp
;
3376 if (unit
== IPL_LOGIPF
&& *group
== '\0')
3380 gflags
= flags
& FR_INOUT
;
3382 fg
= fr_findgroup(group
, unit
, set
, &fgp
, ifs
);
3384 if (fg
->fg_flags
== 0)
3385 fg
->fg_flags
= gflags
;
3386 else if (gflags
!= fg
->fg_flags
)
3391 KMALLOC(fg
, frgroup_t
*);
3394 fg
->fg_start
= NULL
;
3396 bcopy(group
, fg
->fg_name
, FR_GROUPLEN
);
3397 fg
->fg_flags
= gflags
;
3405 /* ------------------------------------------------------------------------ */
3406 /* Function: fr_delgroup */
3408 /* Parameters: group(I) - group name to delete */
3409 /* unit(I) - device to which this group belongs */
3410 /* set(I) - which set of rules (inactive/inactive) this is */
3411 /* Write Locks: ipf_mutex */
3413 /* Attempt to delete a group head. */
3414 /* Only do this when its reference count reaches 0. */
3415 /* ------------------------------------------------------------------------ */
3416 void fr_delgroup(group
, unit
, set
, ifs
)
3422 frgroup_t
*fg
, **fgp
;
3424 fg
= fr_findgroup(group
, unit
, set
, &fgp
, ifs
);
3429 if (fg
->fg_ref
== 0) {
3436 /* ------------------------------------------------------------------------ */
3437 /* Function: fr_getrulen */
3438 /* Returns: frentry_t * - NULL == not found, else pointer to rule n */
3439 /* Parameters: unit(I) - device for which to count the rule's number */
3440 /* flags(I) - which set of rules to find the rule in */
3441 /* group(I) - group name */
3442 /* n(I) - rule number to find */
3444 /* Find rule # n in group # g and return a pointer to it. Return NULl if */
3445 /* group # g doesn't exist or there are less than n rules in the group. */
3446 /* ------------------------------------------------------------------------ */
3447 frentry_t
*fr_getrulen(unit
, group
, n
, ifs
)
3456 fg
= fr_findgroup(group
, unit
, ifs
->ifs_fr_active
, NULL
, ifs
);
3459 for (fr
= fg
->fg_head
; fr
&& n
; fr
= fr
->fr_next
, n
--)
3467 /* ------------------------------------------------------------------------ */
3468 /* Function: fr_rulen */
3469 /* Returns: int - >= 0 - rule number, -1 == search failed */
3470 /* Parameters: unit(I) - device for which to count the rule's number */
3471 /* fr(I) - pointer to rule to match */
3473 /* Return the number for a rule on a specific filtering device. */
3474 /* ------------------------------------------------------------------------ */
3475 int fr_rulen(unit
, fr
, ifs
)
3486 fg
= fr_findgroup(fr
->fr_group
, unit
, ifs
->ifs_fr_active
, NULL
, ifs
);
3489 for (fh
= fg
->fg_head
; fh
; n
++, fh
= fh
->fr_next
)
3498 /* ------------------------------------------------------------------------ */
3499 /* Function: frflushlist */
3500 /* Returns: int - >= 0 - number of flushed rules */
3501 /* Parameters: set(I) - which set of rules (inactive/inactive) this is */
3502 /* unit(I) - device for which to flush rules */
3503 /* flags(I) - which set of rules to flush */
3504 /* nfreedp(O) - pointer to int where flush count is stored */
3505 /* listp(I) - pointer to list to flush pointer */
3506 /* Write Locks: ipf_mutex */
3508 /* Recursively flush rules from the list, descending groups as they are */
3509 /* encountered. if a rule is the head of a group and it has lost all its */
3510 /* group members, then also delete the group reference. nfreedp is needed */
3511 /* to store the accumulating count of rules removed, whereas the returned */
3512 /* value is just the number removed from the current list. The latter is */
3513 /* needed to correctly adjust reference counts on rules that define groups. */
3515 /* NOTE: Rules not loaded from user space cannot be flushed. */
3516 /* ------------------------------------------------------------------------ */
3517 static int frflushlist(set
, unit
, nfreedp
, listp
, ifs
)
3527 while ((fp
= *listp
) != NULL
) {
3528 if ((fp
->fr_type
& FR_T_BUILTIN
) ||
3529 !(fp
->fr_flags
& FR_COPIED
)) {
3530 listp
= &fp
->fr_next
;
3533 *listp
= fp
->fr_next
;
3534 if (fp
->fr_grp
!= NULL
) {
3535 (void) frflushlist(set
, unit
, nfreedp
, fp
->fr_grp
, ifs
);
3538 if (fp
->fr_grhead
!= NULL
) {
3539 fr_delgroup(fp
->fr_grhead
, unit
, set
, ifs
);
3540 *fp
->fr_grhead
= '\0';
3543 ASSERT(fp
->fr_ref
> 0);
3545 if (fr_derefrule(&fp
, ifs
) == 0)
3553 /* ------------------------------------------------------------------------ */
3554 /* Function: frflush */
3555 /* Returns: int - >= 0 - number of flushed rules */
3556 /* Parameters: unit(I) - device for which to flush rules */
3557 /* flags(I) - which set of rules to flush */
3559 /* Calls flushlist() for all filter rules (accounting, firewall - both IPv4 */
3560 /* and IPv6) as defined by the value of flags. */
3561 /* ------------------------------------------------------------------------ */
3562 int frflush(unit
, proto
, flags
, ifs
)
3567 int flushed
= 0, set
;
3569 WRITE_ENTER(&ifs
->ifs_ipf_mutex
);
3570 bzero((char *)ifs
->ifs_frcache
, sizeof (ifs
->ifs_frcache
));
3572 set
= ifs
->ifs_fr_active
;
3573 if ((flags
& FR_INACTIVE
) == FR_INACTIVE
)
3576 if (flags
& FR_OUTQUE
) {
3577 if (proto
== 0 || proto
== 6) {
3578 (void) frflushlist(set
, unit
,
3579 &flushed
, &ifs
->ifs_ipfilter6
[1][set
], ifs
);
3580 (void) frflushlist(set
, unit
,
3581 &flushed
, &ifs
->ifs_ipacct6
[1][set
], ifs
);
3583 if (proto
== 0 || proto
== 4) {
3584 (void) frflushlist(set
, unit
,
3585 &flushed
, &ifs
->ifs_ipfilter
[1][set
], ifs
);
3586 (void) frflushlist(set
, unit
,
3587 &flushed
, &ifs
->ifs_ipacct
[1][set
], ifs
);
3590 if (flags
& FR_INQUE
) {
3591 if (proto
== 0 || proto
== 6) {
3592 (void) frflushlist(set
, unit
,
3593 &flushed
, &ifs
->ifs_ipfilter6
[0][set
], ifs
);
3594 (void) frflushlist(set
, unit
,
3595 &flushed
, &ifs
->ifs_ipacct6
[0][set
], ifs
);
3597 if (proto
== 0 || proto
== 4) {
3598 (void) frflushlist(set
, unit
,
3599 &flushed
, &ifs
->ifs_ipfilter
[0][set
], ifs
);
3600 (void) frflushlist(set
, unit
,
3601 &flushed
, &ifs
->ifs_ipacct
[0][set
], ifs
);
3604 RWLOCK_EXIT(&ifs
->ifs_ipf_mutex
);
3606 if (unit
== IPL_LOGIPF
) {
3609 tmp
= frflush(IPL_LOGCOUNT
, proto
, flags
, ifs
);
3617 /* ------------------------------------------------------------------------ */
3618 /* Function: memstr */
3619 /* Returns: char * - NULL if failed, != NULL pointer to matching bytes */
3620 /* Parameters: src(I) - pointer to byte sequence to match */
3621 /* dst(I) - pointer to byte sequence to search */
3622 /* slen(I) - match length */
3623 /* dlen(I) - length available to search in */
3625 /* Search dst for a sequence of bytes matching those at src and extend for */
3627 /* ------------------------------------------------------------------------ */
3628 char *memstr(src
, dst
, slen
, dlen
)
3634 while (dlen
>= slen
) {
3635 if (bcmp(src
, dst
, slen
) == 0) {
3644 /* ------------------------------------------------------------------------ */
3645 /* Function: fr_fixskip */
3647 /* Parameters: listp(IO) - pointer to start of list with skip rule */
3648 /* rp(I) - rule added/removed with skip in it. */
3649 /* addremove(I) - adjustment (-1/+1) to make to skip count, */
3650 /* depending on whether a rule was just added */
3653 /* Adjust all the rules in a list which would have skip'd past the position */
3654 /* where we are inserting to skip to the right place given the change. */
3655 /* ------------------------------------------------------------------------ */
3656 void fr_fixskip(listp
, rp
, addremove
)
3657 frentry_t
**listp
, *rp
;
3664 for (fp
= *listp
; (fp
!= NULL
) && (fp
!= rp
); fp
= fp
->fr_next
)
3670 for (rn
= 0, fp
= *listp
; fp
&& (fp
!= rp
); fp
= fp
->fr_next
, rn
++)
3671 if (FR_ISSKIP(fp
->fr_flags
) && (rn
+ fp
->fr_arg
>= rules
))
3672 fp
->fr_arg
+= addremove
;
3677 /* ------------------------------------------------------------------------ */
3678 /* Function: count4bits */
3679 /* Returns: int - >= 0 - number of consecutive bits in input */
3680 /* Parameters: ip(I) - 32bit IP address */
3683 /* count consecutive 1's in bit mask. If the mask generated by counting */
3684 /* consecutive 1's is different to that passed, return -1, else return # */
3686 /* ------------------------------------------------------------------------ */
3693 ip
= ipn
= ntohl(ip
);
3694 for (i
= 32; i
; i
--, ipn
*= 2)
3695 if (ipn
& 0x80000000)
3700 for (i
= 32, j
= cnt
; i
; i
--, j
--) {
3712 /* ------------------------------------------------------------------------ */
3713 /* Function: count6bits */
3714 /* Returns: int - >= 0 - number of consecutive bits in input */
3715 /* Parameters: msk(I) - pointer to start of IPv6 bitmask */
3718 /* count consecutive 1's in bit mask. */
3719 /* ------------------------------------------------------------------------ */
3726 for (k
= 3; k
>= 0; k
--)
3727 if (msk
[k
] == 0xffffffff)
3730 for (j
= msk
[k
]; j
; j
<<= 1)
3737 #endif /* _KERNEL */
3740 /* ------------------------------------------------------------------------ */
3741 /* Function: fr_ifsync */
3742 /* Returns: void * - new interface identifier */
3743 /* Parameters: action(I) - type of synchronisation to do */
3744 /* v(I) - IP version being sync'd (v4 or v6) */
3745 /* newifp(I) - interface identifier being introduced/removed */
3746 /* oldifp(I) - interface identifier in a filter rule */
3747 /* newname(I) - name associated with newifp interface */
3748 /* oldname(I) - name associated with oldifp interface */
3749 /* ifs - pointer to IPF stack instance */
3751 /* This function returns what the new value for "oldifp" should be for its */
3752 /* caller. In some cases it will not change, in some it will. */
3753 /* action == IPFSYNC_RESYNC */
3754 /* a new value for oldifp will always be looked up, according to oldname, */
3755 /* the values of newname and newifp are ignored. */
3756 /* action == IPFSYNC_NEWIFP */
3757 /* if oldname matches newname then we are doing a sync for the matching */
3758 /* interface, so we return newifp to be used in place of oldifp. If the */
3759 /* the names don't match, just return oldifp. */
3760 /* action == IPFSYNC_OLDIFP */
3761 /* if oldifp matches newifp then we are are doing a sync to remove any */
3762 /* references to oldifp, so we return "-1". */
3765 /* This function processes NIC event from PF_HOOKS. The action parameter */
3766 /* is set in ipf_nic_event_v4()/ipf_nic_event_v6() function. There is */
3767 /* one single switch statement() in ipf_nic_event_vx() function, which */
3768 /* translates the HOOK event type to action parameter passed to fr_ifsync. */
3769 /* The translation table looks as follows: */
3770 /* event | action */
3771 /* ----------------+------------- */
3772 /* NE_PLUMB | IPFSYNC_NEWIFP */
3773 /* NE_UNPLUMB | IPFSYNC_OLDIFP */
3774 /* NE_ADDRESS_CHANGE | IPFSYNC_RESYNC */
3776 /* The oldname and oldifp parameters are taken from IPF entry (rule, state */
3777 /* table entry, NAT table entry, fragment ...). The newname and newifp */
3778 /* parameters come from hook event data, parameters are taken from event */
3779 /* in ipf_nic_event_vx() functions. Any time NIC changes, the IPF is */
3780 /* notified by hook function. */
3782 /* We get NE_UNPLUMB event from PF_HOOKS even if someone coincidently tries */
3783 /* to plumb the interface, which is already plumbed. In such case we always */
3784 /* get the event from PF_HOOKS as follows: */
3785 /* event: NE_PLUMB */
3787 /* ------------------------------------------------------------------------ */
3788 static void *fr_ifsync(action
, v
, newname
, oldname
, newifp
, oldifp
, ifs
)
3790 char *newname
, *oldname
;
3791 void *newifp
, *oldifp
;
3794 void *rval
= oldifp
;
3798 case IPFSYNC_RESYNC
:
3799 if (oldname
[0] != '\0') {
3800 rval
= fr_resolvenic(oldname
, v
, ifs
);
3803 case IPFSYNC_NEWIFP
:
3804 if (!strncmp(newname
, oldname
, LIFNAMSIZ
))
3807 case IPFSYNC_OLDIFP
:
3809 * If interface gets unplumbed it must be invalidated, which
3810 * means set all existing references to the interface to -1.
3811 * We don't want to invalidate references for wildcard
3812 * (unbound) rules (entries).
3814 if (newifp
== oldifp
)
3815 rval
= (oldifp
) ? (void *)-1 : NULL
;
3823 /* ------------------------------------------------------------------------ */
3824 /* Function: frsynclist */
3826 /* Parameters: action(I) - type of synchronisation to do */
3827 /* v(I) - IP version being sync'd (v4 or v6) */
3828 /* ifp(I) - interface identifier associated with action */
3829 /* ifname(I) - name associated with ifp parameter */
3830 /* fr(I) - pointer to filter rule */
3831 /* ifs - pointer to IPF stack instance */
3832 /* Write Locks: ipf_mutex */
3834 /* Walk through a list of filter rules and resolve any interface names into */
3835 /* pointers. Where dynamic addresses are used, also update the IP address */
3836 /* used in the rule. The interface pointer is used to limit the lookups to */
3837 /* a specific set of matching names if it is non-NULL. */
3838 /* ------------------------------------------------------------------------ */
3839 static void frsynclist(action
, v
, ifp
, ifname
, fr
, ifs
)
3849 for (; fr
; fr
= fr
->fr_next
) {
3851 if (v
!= 0 && v
!= rv
)
3855 * Lookup all the interface names that are part of the rule.
3857 for (i
= 0; i
< 4; i
++) {
3858 fr
->fr_ifas
[i
] = fr_ifsync(action
, rv
, ifname
,
3860 ifp
, fr
->fr_ifas
[i
],
3864 fdp
= &fr
->fr_tifs
[0];
3865 fdp
->fd_ifp
= fr_ifsync(action
, rv
, ifname
, fdp
->fd_ifname
,
3866 ifp
, fdp
->fd_ifp
, ifs
);
3868 fdp
= &fr
->fr_tifs
[1];
3869 fdp
->fd_ifp
= fr_ifsync(action
, rv
, ifname
, fdp
->fd_ifname
,
3870 ifp
, fdp
->fd_ifp
, ifs
);
3873 fdp
->fd_ifp
= fr_ifsync(action
, rv
, ifname
, fdp
->fd_ifname
,
3874 ifp
, fdp
->fd_ifp
, ifs
);
3876 if (action
!= IPFSYNC_RESYNC
)
3879 if (fr
->fr_type
== FR_T_IPF
) {
3880 if (fr
->fr_satype
!= FRI_NORMAL
&&
3881 fr
->fr_satype
!= FRI_LOOKUP
) {
3882 (void)fr_ifpaddr(rv
, fr
->fr_satype
,
3883 fr
->fr_ifas
[fr
->fr_sifpidx
],
3884 &fr
->fr_src
, &fr
->fr_smsk
,
3887 if (fr
->fr_datype
!= FRI_NORMAL
&&
3888 fr
->fr_datype
!= FRI_LOOKUP
) {
3889 (void)fr_ifpaddr(rv
, fr
->fr_datype
,
3890 fr
->fr_ifas
[fr
->fr_difpidx
],
3891 &fr
->fr_dst
, &fr
->fr_dmsk
,
3896 #ifdef IPFILTER_LOOKUP
3897 if (fr
->fr_type
== FR_T_IPF
&& fr
->fr_satype
== FRI_LOOKUP
&&
3898 fr
->fr_srcptr
== NULL
) {
3899 fr
->fr_srcptr
= fr_resolvelookup(fr
->fr_srctype
,
3901 &fr
->fr_srcfunc
, ifs
);
3903 if (fr
->fr_type
== FR_T_IPF
&& fr
->fr_datype
== FRI_LOOKUP
&&
3904 fr
->fr_dstptr
== NULL
) {
3905 fr
->fr_dstptr
= fr_resolvelookup(fr
->fr_dsttype
,
3907 &fr
->fr_dstfunc
, ifs
);
3915 /* ------------------------------------------------------------------------ */
3916 /* Function: frsync */
3918 /* Parameters: action(I) - type of synchronisation to do */
3919 /* v(I) - IP version being sync'd (v4 or v6) */
3920 /* ifp(I) - interface identifier associated with action */
3921 /* name(I) - name associated with ifp parameter */
3923 /* frsync() is called when we suspect that the interface list or */
3924 /* information about interfaces (like IP#) has changed. Go through all */
3925 /* filter rules, NAT entries and the state table and check if anything */
3926 /* needs to be changed/updated. */
3927 /* With the filtering hooks added to Solaris, we needed to change the manner*/
3928 /* in which this was done to support three different types of sync: */
3929 /* - complete resync of all interface name/identifiers */
3930 /* - new interface being announced with its name and identifier */
3931 /* - interface removal being announced by only its identifier */
3932 /* ------------------------------------------------------------------------ */
3933 void frsync(action
, v
, ifp
, name
, ifs
)
3941 WRITE_ENTER(&ifs
->ifs_ipf_mutex
);
3942 frsynclist(action
, v
, ifp
, name
, ifs
->ifs_ipacct
[0][ifs
->ifs_fr_active
], ifs
);
3943 frsynclist(action
, v
, ifp
, name
, ifs
->ifs_ipacct
[1][ifs
->ifs_fr_active
], ifs
);
3944 frsynclist(action
, v
, ifp
, name
, ifs
->ifs_ipfilter
[0][ifs
->ifs_fr_active
], ifs
);
3945 frsynclist(action
, v
, ifp
, name
, ifs
->ifs_ipfilter
[1][ifs
->ifs_fr_active
], ifs
);
3946 frsynclist(action
, v
, ifp
, name
, ifs
->ifs_ipacct6
[0][ifs
->ifs_fr_active
], ifs
);
3947 frsynclist(action
, v
, ifp
, name
, ifs
->ifs_ipacct6
[1][ifs
->ifs_fr_active
], ifs
);
3948 frsynclist(action
, v
, ifp
, name
, ifs
->ifs_ipfilter6
[0][ifs
->ifs_fr_active
], ifs
);
3949 frsynclist(action
, v
, ifp
, name
, ifs
->ifs_ipfilter6
[1][ifs
->ifs_fr_active
], ifs
);
3951 for (i
= 0; i
< IPL_LOGSIZE
; i
++) {
3954 for (g
= ifs
->ifs_ipfgroups
[i
][0]; g
!= NULL
; g
= g
->fg_next
)
3955 frsynclist(action
, v
, ifp
, name
, g
->fg_start
, ifs
);
3956 for (g
= ifs
->ifs_ipfgroups
[i
][1]; g
!= NULL
; g
= g
->fg_next
)
3957 frsynclist(action
, v
, ifp
, name
, g
->fg_start
, ifs
);
3959 RWLOCK_EXIT(&ifs
->ifs_ipf_mutex
);
3963 /* ------------------------------------------------------------------------ */
3964 /* Function: fr_syncindex */
3966 /* Parameters: rules - list of rules to be sync'd */
3967 /* ifp - interface, which is being sync'd */
3968 /* newifp - new ifindex value for interface */
3970 /* Function updates all NIC indecis, which match ifp, in every rule. Every */
3971 /* NIC index matching ifp, will be updated to newifp. */
3972 /* ------------------------------------------------------------------------ */
3973 static void fr_syncindex(rules
, ifp
, newifp
)
3981 for (fr
= rules
; fr
!= NULL
; fr
= fr
->fr_next
) {
3983 * Lookup all the interface names that are part of the rule.
3985 for (i
= 0; i
< 4; i
++)
3986 if (fr
->fr_ifas
[i
] == ifp
)
3987 fr
->fr_ifas
[i
] = newifp
;
3989 for (i
= 0; i
< 2; i
++) {
3990 if (fr
->fr_tifs
[i
].fd_ifp
== ifp
)
3991 fr
->fr_tifs
[i
].fd_ifp
= newifp
;
3994 if (fr
->fr_dif
.fd_ifp
== ifp
)
3995 fr
->fr_dif
.fd_ifp
= newifp
;
3999 /* ------------------------------------------------------------------------ */
4000 /* Function: fr_ifindexsync */
4002 /* Parameters: ifp - interface, which is being sync'd */
4003 /* newifp - new ifindex value for interface */
4004 /* ifs - IPF's stack */
4006 /* Function assumes ipf_mutex is locked exclusively. */
4008 /* Function updates the NIC references in rules with new interfaces index */
4009 /* (newifp). Function must process active lists: */
4010 /* with accounting rules (IPv6 and IPv4) */
4011 /* with inbound rules (IPv6 and IPv4) */
4012 /* with outbound rules (IPv6 and IPv4) */
4013 /* Function also has to take care of rule groups. */
4015 /* NOTE: The ipf_mutex is grabbed exclusively by caller (which is always */
4016 /* nic_event_hook). The hook function also updates state entries, NAT rules */
4017 /* and NAT entries. We want to do all these update atomically to keep the */
4018 /* NIC references consistent. The ipf_mutex will synchronize event with */
4019 /* fr_check(), which processes packets, so no packet will enter fr_check(), */
4020 /* while NIC references will be synchronized. */
4021 /* ------------------------------------------------------------------------ */
4022 void fr_ifindexsync(ifp
, newifp
, ifs
)
4028 frentry_t
*rule_lists
[8];
4029 unsigned int rules
= sizeof (rule_lists
) / sizeof (frentry_t
*);
4031 rule_lists
[0] = ifs
->ifs_ipacct
[0][ifs
->ifs_fr_active
];
4032 rule_lists
[1] = ifs
->ifs_ipacct
[1][ifs
->ifs_fr_active
];
4033 rule_lists
[2] = ifs
->ifs_ipfilter
[0][ifs
->ifs_fr_active
];
4034 rule_lists
[3] = ifs
->ifs_ipfilter
[1][ifs
->ifs_fr_active
];
4035 rule_lists
[4] = ifs
->ifs_ipacct6
[0][ifs
->ifs_fr_active
];
4036 rule_lists
[5] = ifs
->ifs_ipacct6
[1][ifs
->ifs_fr_active
];
4037 rule_lists
[6] = ifs
->ifs_ipfilter6
[0][ifs
->ifs_fr_active
];
4038 rule_lists
[7] = ifs
->ifs_ipfilter6
[1][ifs
->ifs_fr_active
];
4040 for (i
= 0; i
< rules
; i
++) {
4041 fr_syncindex(rule_lists
[i
], ifp
, newifp
);
4045 * Update rule groups.
4047 for (i
= 0; i
< IPL_LOGSIZE
; i
++) {
4050 for (g
= ifs
->ifs_ipfgroups
[i
][0]; g
!= NULL
; g
= g
->fg_next
)
4051 fr_syncindex(g
->fg_start
, ifp
, newifp
);
4052 for (g
= ifs
->ifs_ipfgroups
[i
][1]; g
!= NULL
; g
= g
->fg_next
)
4053 fr_syncindex(g
->fg_start
, ifp
, newifp
);
4059 * In the functions below, bcopy() is called because the pointer being
4060 * copied _from_ in this instance is a pointer to a char buf (which could
4061 * end up being unaligned) and on the kernel's local stack.
4063 /* ------------------------------------------------------------------------ */
4064 /* Function: copyinptr */
4065 /* Returns: int - 0 = success, else failure */
4066 /* Parameters: src(I) - pointer to the source address */
4067 /* dst(I) - destination address */
4068 /* size(I) - number of bytes to copy */
4070 /* Copy a block of data in from user space, given a pointer to the pointer */
4071 /* to start copying from (src) and a pointer to where to store it (dst). */
4072 /* NB: src - pointer to user space pointer, dst - kernel space pointer */
4073 /* ------------------------------------------------------------------------ */
4074 int copyinptr(src
, dst
, size
)
4082 err
= COPYIN(src
, (caddr_t
)&ca
, sizeof(ca
));
4086 bcopy(src
, (caddr_t
)&ca
, sizeof(ca
));
4088 err
= COPYIN(ca
, dst
, size
);
4093 /* ------------------------------------------------------------------------ */
4094 /* Function: copyoutptr */
4095 /* Returns: int - 0 = success, else failure */
4096 /* Parameters: src(I) - pointer to the source address */
4097 /* dst(I) - destination address */
4098 /* size(I) - number of bytes to copy */
4100 /* Copy a block of data out to user space, given a pointer to the pointer */
4101 /* to start copying from (src) and a pointer to where to store it (dst). */
4102 /* NB: src - kernel space pointer, dst - pointer to user space pointer. */
4103 /* ------------------------------------------------------------------------ */
4104 int copyoutptr(src
, dst
, size
)
4112 err
= COPYIN(dst
, (caddr_t
)&ca
, sizeof(ca
));
4116 bcopy(dst
, (caddr_t
)&ca
, sizeof(ca
));
4118 err
= COPYOUT(src
, ca
, size
);
4124 /* ------------------------------------------------------------------------ */
4125 /* Function: fr_lock */
4126 /* Returns: int - 0 = success, else error */
4127 /* Parameters: data(I) - pointer to lock value to set */
4128 /* lockp(O) - pointer to location to store old lock value */
4130 /* Get the new value for the lock integer, set it and return the old value */
4132 /* ------------------------------------------------------------------------ */
4133 int fr_lock(data
, lockp
)
4139 err
= BCOPYIN(data
, (caddr_t
)&arg
, sizeof(arg
));
4142 err
= BCOPYOUT((caddr_t
)lockp
, data
, sizeof(*lockp
));
4150 /* ------------------------------------------------------------------------ */
4151 /* Function: fr_getstat */
4153 /* Parameters: fiop(I) - pointer to ipfilter stats structure */
4155 /* Stores a copy of current pointers, counters, etc, in the friostat */
4157 /* ------------------------------------------------------------------------ */
4158 void fr_getstat(fiop
, ifs
)
4164 bcopy((char *)&ifs
->ifs_frstats
, (char *)fiop
->f_st
,
4165 sizeof(filterstats_t
) * 2);
4166 fiop
->f_locks
[IPL_LOGSTATE
] = ifs
->ifs_fr_state_lock
;
4167 fiop
->f_locks
[IPL_LOGNAT
] = ifs
->ifs_fr_nat_lock
;
4168 fiop
->f_locks
[IPL_LOGIPF
] = ifs
->ifs_fr_frag_lock
;
4169 fiop
->f_locks
[IPL_LOGAUTH
] = ifs
->ifs_fr_auth_lock
;
4171 for (i
= 0; i
< 2; i
++)
4172 for (j
= 0; j
< 2; j
++) {
4173 fiop
->f_ipf
[i
][j
] = ifs
->ifs_ipfilter
[i
][j
];
4174 fiop
->f_acct
[i
][j
] = ifs
->ifs_ipacct
[i
][j
];
4175 fiop
->f_ipf6
[i
][j
] = ifs
->ifs_ipfilter6
[i
][j
];
4176 fiop
->f_acct6
[i
][j
] = ifs
->ifs_ipacct6
[i
][j
];
4179 fiop
->f_ticks
= ifs
->ifs_fr_ticks
;
4180 fiop
->f_active
= ifs
->ifs_fr_active
;
4181 fiop
->f_froute
[0] = ifs
->ifs_fr_frouteok
[0];
4182 fiop
->f_froute
[1] = ifs
->ifs_fr_frouteok
[1];
4184 fiop
->f_running
= ifs
->ifs_fr_running
;
4185 for (i
= 0; i
< IPL_LOGSIZE
; i
++) {
4186 fiop
->f_groups
[i
][0] = ifs
->ifs_ipfgroups
[i
][0];
4187 fiop
->f_groups
[i
][1] = ifs
->ifs_ipfgroups
[i
][1];
4190 fiop
->f_logging
= 1;
4192 fiop
->f_logging
= 0;
4194 fiop
->f_defpass
= ifs
->ifs_fr_pass
;
4195 fiop
->f_features
= fr_features
;
4196 (void) strncpy(fiop
->f_version
, ipfilter_version
,
4197 sizeof(fiop
->f_version
));
4202 int icmptoicmp6types
[ICMP_MAXTYPE
+1] = {
4203 ICMP6_ECHO_REPLY
, /* 0: ICMP_ECHOREPLY */
4206 ICMP6_DST_UNREACH
, /* 3: ICMP_UNREACH */
4207 -1, /* 4: ICMP_SOURCEQUENCH */
4208 ND_REDIRECT
, /* 5: ICMP_REDIRECT */
4211 ICMP6_ECHO_REQUEST
, /* 8: ICMP_ECHO */
4213 -1, /* 10: UNUSED */
4214 ICMP6_TIME_EXCEEDED
, /* 11: ICMP_TIMXCEED */
4215 ICMP6_PARAM_PROB
, /* 12: ICMP_PARAMPROB */
4216 -1, /* 13: ICMP_TSTAMP */
4217 -1, /* 14: ICMP_TSTAMPREPLY */
4218 -1, /* 15: ICMP_IREQ */
4219 -1, /* 16: ICMP_IREQREPLY */
4220 -1, /* 17: ICMP_MASKREQ */
4221 -1, /* 18: ICMP_MASKREPLY */
4225 int icmptoicmp6unreach
[ICMP_MAX_UNREACH
] = {
4226 ICMP6_DST_UNREACH_ADDR
, /* 0: ICMP_UNREACH_NET */
4227 ICMP6_DST_UNREACH_ADDR
, /* 1: ICMP_UNREACH_HOST */
4228 -1, /* 2: ICMP_UNREACH_PROTOCOL */
4229 ICMP6_DST_UNREACH_NOPORT
, /* 3: ICMP_UNREACH_PORT */
4230 -1, /* 4: ICMP_UNREACH_NEEDFRAG */
4231 ICMP6_DST_UNREACH_NOTNEIGHBOR
, /* 5: ICMP_UNREACH_SRCFAIL */
4232 ICMP6_DST_UNREACH_ADDR
, /* 6: ICMP_UNREACH_NET_UNKNOWN */
4233 ICMP6_DST_UNREACH_ADDR
, /* 7: ICMP_UNREACH_HOST_UNKNOWN */
4234 -1, /* 8: ICMP_UNREACH_ISOLATED */
4235 ICMP6_DST_UNREACH_ADMIN
, /* 9: ICMP_UNREACH_NET_PROHIB */
4236 ICMP6_DST_UNREACH_ADMIN
, /* 10: ICMP_UNREACH_HOST_PROHIB */
4237 -1, /* 11: ICMP_UNREACH_TOSNET */
4238 -1, /* 12: ICMP_UNREACH_TOSHOST */
4239 ICMP6_DST_UNREACH_ADMIN
, /* 13: ICMP_UNREACH_ADMIN_PROHIBIT */
4241 int icmpreplytype6
[ICMP6_MAXTYPE
+ 1];
4244 int icmpreplytype4
[ICMP_MAXTYPE
+ 1];
4247 /* ------------------------------------------------------------------------ */
4248 /* Function: fr_matchicmpqueryreply */
4249 /* Returns: int - 1 if "icmp" is a valid reply to "ic" else 0. */
4250 /* Parameters: v(I) - IP protocol version (4 or 6) */
4251 /* ic(I) - ICMP information */
4252 /* icmp(I) - ICMP packet header */
4253 /* rev(I) - direction (0 = forward/1 = reverse) of packet */
4255 /* Check if the ICMP packet defined by the header pointed to by icmp is a */
4256 /* reply to one as described by what's in ic. If it is a match, return 1, */
4257 /* else return 0 for no match. */
4258 /* ------------------------------------------------------------------------ */
4259 int fr_matchicmpqueryreply(v
, ic
, icmp
, rev
)
4267 ictype
= ic
->ici_type
;
4271 * If we matched its type on the way in, then when going out
4272 * it will still be the same type.
4274 if ((!rev
&& (icmp
->icmp_type
== ictype
)) ||
4275 (rev
&& (icmpreplytype4
[ictype
] == icmp
->icmp_type
))) {
4276 if (icmp
->icmp_type
!= ICMP_ECHOREPLY
)
4278 if (icmp
->icmp_id
== ic
->ici_id
)
4284 if ((!rev
&& (icmp
->icmp_type
== ictype
)) ||
4285 (rev
&& (icmpreplytype6
[ictype
] == icmp
->icmp_type
))) {
4286 if (icmp
->icmp_type
!= ICMP6_ECHO_REPLY
)
4288 if (icmp
->icmp_id
== ic
->ici_id
)
4297 #ifdef IPFILTER_LOOKUP
4298 /* ------------------------------------------------------------------------ */
4299 /* Function: fr_resolvelookup */
4300 /* Returns: void * - NULL = failure, else success. */
4301 /* Parameters: type(I) - type of lookup these parameters are for. */
4302 /* number(I) - table number to use when searching */
4303 /* funcptr(IO) - pointer to pointer for storing IP address */
4304 /* searching function. */
4305 /* ifs - ipf stack instance */
4307 /* Search for the "table" number passed in amongst those configured for */
4308 /* that particular type. If the type is recognised then the function to */
4309 /* call to do the IP address search will be change, regardless of whether */
4310 /* or not the "table" number exists. */
4311 /* ------------------------------------------------------------------------ */
4312 static void *fr_resolvelookup(type
, number
, funcptr
, ifs
)
4314 lookupfunc_t
*funcptr
;
4317 char name
[FR_GROUPLEN
];
4322 #if defined(SNPRINTF) && defined(_KERNEL)
4323 (void) SNPRINTF(name
, sizeof(name
), "%u", number
);
4325 (void) sprintf(name
, "%u", number
);
4328 READ_ENTER(&ifs
->ifs_ip_poolrw
);
4333 # if (defined(__osf__) && defined(_KERNEL))
4337 ipo
= ip_pool_find(IPL_LOGIPF
, name
, ifs
);
4340 ATOMIC_INC32(ipo
->ipo_ref
);
4342 *funcptr
= ip_pool_search
;
4346 iph
= fr_findhtable(IPL_LOGIPF
, name
, ifs
);
4349 ATOMIC_INC32(iph
->iph_ref
);
4351 *funcptr
= fr_iphmfindip
;
4358 RWLOCK_EXIT(&ifs
->ifs_ip_poolrw
);
4365 /* ------------------------------------------------------------------------ */
4366 /* Function: frrequest */
4367 /* Returns: int - 0 == success, > 0 == errno value */
4368 /* Parameters: unit(I) - device for which this is for */
4369 /* req(I) - ioctl command (SIOC*) */
4370 /* data(I) - pointr to ioctl data */
4371 /* set(I) - 1 or 0 (filter set) */
4372 /* makecopy(I) - flag indicating whether data points to a rule */
4373 /* in kernel space & hence doesn't need copying. */
4375 /* This function handles all the requests which operate on the list of */
4376 /* filter rules. This includes adding, deleting, insertion. It is also */
4377 /* responsible for creating groups when a "head" rule is loaded. Interface */
4378 /* names are resolved here and other sanity checks are made on the content */
4379 /* of the rule structure being loaded. If a rule has user defined timeouts */
4380 /* then make sure they are created and initialised before exiting. */
4381 /* ------------------------------------------------------------------------ */
4382 int frrequest(unit
, req
, data
, set
, makecopy
, ifs
)
4389 frentry_t frd
, *fp
, *f
, **fprev
, **ftail
;
4390 int error
= 0, in
, v
;
4398 if (makecopy
!= 0) {
4399 error
= fr_inobj(data
, fp
, IPFOBJ_FRENTRY
);
4402 if ((fp
->fr_flags
& FR_T_BUILTIN
) != 0)
4405 fp
->fr_flags
|= FR_COPIED
;
4407 fp
= (frentry_t
*)data
;
4408 if ((fp
->fr_type
& FR_T_BUILTIN
) == 0)
4410 fp
->fr_flags
&= ~FR_COPIED
;
4413 if (((fp
->fr_dsize
== 0) && (fp
->fr_data
!= NULL
)) ||
4414 ((fp
->fr_dsize
!= 0) && (fp
->fr_data
== NULL
)))
4421 * Only filter rules for IPv4 or IPv6 are accepted.
4434 * If the rule is being loaded from user space, i.e. we had to copy it
4435 * into kernel space, then do not trust the function pointer in the
4438 if ((makecopy
== 1) && (fp
->fr_func
!= NULL
)) {
4439 if (fr_findfunc(fp
->fr_func
) == NULL
)
4441 error
= fr_funcinit(fp
, ifs
);
4448 * Check that the group number does exist and that its use (in/out)
4449 * matches what the rule is.
4451 if (!strncmp(fp
->fr_grhead
, "0", FR_GROUPLEN
))
4452 *fp
->fr_grhead
= '\0';
4453 group
= fp
->fr_group
;
4454 if (!strncmp(group
, "0", FR_GROUPLEN
))
4457 if (FR_ISACCOUNT(fp
->fr_flags
))
4458 unit
= IPL_LOGCOUNT
;
4460 if ((req
!= (int)SIOCZRLST
) && (*group
!= '\0')) {
4461 fg
= fr_findgroup(group
, unit
, set
, NULL
, ifs
);
4464 if (fg
->fg_flags
== 0)
4465 fg
->fg_flags
= fp
->fr_flags
& FR_INOUT
;
4466 else if (fg
->fg_flags
!= (fp
->fr_flags
& FR_INOUT
))
4470 in
= (fp
->fr_flags
& FR_INQUE
) ? 0 : 1;
4473 * Work out which rule list this change is being applied to.
4477 if (unit
== IPL_LOGAUTH
)
4478 fprev
= &ifs
->ifs_ipauth
;
4480 if (FR_ISACCOUNT(fp
->fr_flags
))
4481 fprev
= &ifs
->ifs_ipacct
[in
][set
];
4482 else if ((fp
->fr_flags
& (FR_OUTQUE
|FR_INQUE
)) != 0)
4483 fprev
= &ifs
->ifs_ipfilter
[in
][set
];
4484 } else if (v
== 6) {
4485 if (FR_ISACCOUNT(fp
->fr_flags
))
4486 fprev
= &ifs
->ifs_ipacct6
[in
][set
];
4487 else if ((fp
->fr_flags
& (FR_OUTQUE
|FR_INQUE
)) != 0)
4488 fprev
= &ifs
->ifs_ipfilter6
[in
][set
];
4493 if (*group
!= '\0') {
4494 if (!fg
&& !(fg
= fr_findgroup(group
, unit
, set
, NULL
, ifs
)))
4496 fprev
= &fg
->fg_start
;
4500 for (f
= *ftail
; (f
= *ftail
) != NULL
; ftail
= &f
->fr_next
) {
4501 if (fp
->fr_collect
<= f
->fr_collect
) {
4510 * Copy in extra data for the rule.
4512 if (fp
->fr_dsize
!= 0) {
4513 if (makecopy
!= 0) {
4514 KMALLOCS(ptr
, void *, fp
->fr_dsize
);
4517 error
= COPYIN(uptr
, ptr
, fp
->fr_dsize
);
4523 KFREES(ptr
, fp
->fr_dsize
);
4531 * Perform per-rule type sanity checks of their members.
4533 switch (fp
->fr_type
& ~FR_T_BUILTIN
)
4535 #if defined(IPFILTER_BPF)
4537 if (fp
->fr_dsize
== 0)
4539 if (!bpf_validate(ptr
, fp
->fr_dsize
/sizeof(struct bpf_insn
))) {
4540 if (makecopy
&& fp
->fr_data
!= NULL
) {
4541 KFREES(fp
->fr_data
, fp
->fr_dsize
);
4548 if (fp
->fr_dsize
!= sizeof(fripf_t
)) {
4549 if (makecopy
&& fp
->fr_data
!= NULL
) {
4550 KFREES(fp
->fr_data
, fp
->fr_dsize
);
4556 * Allowing a rule with both "keep state" and "with oow" is
4557 * pointless because adding a state entry to the table will
4558 * fail with the out of window (oow) flag set.
4560 if ((fp
->fr_flags
& FR_KEEPSTATE
) && (fp
->fr_flx
& FI_OOW
)) {
4561 if (makecopy
&& fp
->fr_data
!= NULL
) {
4562 KFREES(fp
->fr_data
, fp
->fr_dsize
);
4567 switch (fp
->fr_satype
)
4569 case FRI_BROADCAST
:
4572 case FRI_NETMASKED
:
4574 if (fp
->fr_sifpidx
< 0 || fp
->fr_sifpidx
> 3) {
4575 if (makecopy
&& fp
->fr_data
!= NULL
) {
4576 KFREES(fp
->fr_data
, fp
->fr_dsize
);
4581 #ifdef IPFILTER_LOOKUP
4583 fp
->fr_srcptr
= fr_resolvelookup(fp
->fr_srctype
,
4585 &fp
->fr_srcfunc
, ifs
);
4592 switch (fp
->fr_datype
)
4594 case FRI_BROADCAST
:
4597 case FRI_NETMASKED
:
4599 if (fp
->fr_difpidx
< 0 || fp
->fr_difpidx
> 3) {
4600 if (makecopy
&& fp
->fr_data
!= NULL
) {
4601 KFREES(fp
->fr_data
, fp
->fr_dsize
);
4606 #ifdef IPFILTER_LOOKUP
4608 fp
->fr_dstptr
= fr_resolvelookup(fp
->fr_dsttype
,
4610 &fp
->fr_dstfunc
, ifs
);
4619 case FR_T_CALLFUNC
:
4624 if (makecopy
&& fp
->fr_data
!= NULL
) {
4625 KFREES(fp
->fr_data
, fp
->fr_dsize
);
4631 * Lookup all the interface names that are part of the rule.
4633 frsynclist(0, 0, NULL
, NULL
, fp
, ifs
);
4634 fp
->fr_statecnt
= 0;
4637 * Look for an existing matching filter rule, but don't include the
4638 * next or interface pointer in the comparison (fr_next, fr_ifa).
4639 * This elminates rules which are indentical being loaded. Checksum
4640 * the constant part of the filter rule to make comparisons quicker
4641 * (this meaning no pointers are included).
4643 for (fp
->fr_cksum
= 0, p
= (u_int
*)&fp
->fr_func
, pp
= &fp
->fr_cksum
;
4646 pp
= (u_int
*)(fp
->fr_caddr
+ fp
->fr_dsize
);
4647 for (p
= (u_int
*)fp
->fr_data
; p
< pp
; p
++)
4650 WRITE_ENTER(&ifs
->ifs_ipf_mutex
);
4651 bzero((char *)ifs
->ifs_frcache
, sizeof (ifs
->ifs_frcache
));
4653 for (; (f
= *ftail
) != NULL
; ftail
= &f
->fr_next
) {
4654 if ((fp
->fr_cksum
!= f
->fr_cksum
) ||
4655 (f
->fr_dsize
!= fp
->fr_dsize
))
4657 if (bcmp((char *)&f
->fr_func
, (char *)&fp
->fr_func
, FR_CMPSIZ
))
4659 if ((!ptr
&& !f
->fr_data
) ||
4660 (ptr
&& f
->fr_data
&&
4661 !bcmp((char *)ptr
, (char *)f
->fr_data
, f
->fr_dsize
)))
4666 * If zero'ing statistics, copy current to caller and zero.
4668 if (req
== (ioctlcmd_t
)SIOCZRLST
) {
4673 * Copy and reduce lock because of impending copyout.
4674 * Well we should, but if we do then the atomicity of
4675 * this call and the correctness of fr_hits and
4676 * fr_bytes cannot be guaranteed. As it is, this code
4677 * only resets them to 0 if they are successfully
4678 * copied out into user space.
4680 bcopy((char *)f
, (char *)fp
, sizeof(*f
));
4683 * When we copy this rule back out, set the data
4684 * pointer to be what it was in user space.
4687 error
= fr_outobj(data
, fp
, IPFOBJ_FRENTRY
);
4690 if ((f
->fr_dsize
!= 0) && (uptr
!= NULL
))
4691 error
= COPYOUT(f
->fr_data
, uptr
,
4700 if ((ptr
!= NULL
) && (makecopy
!= 0)) {
4701 KFREES(ptr
, fp
->fr_dsize
);
4703 RWLOCK_EXIT(&ifs
->ifs_ipf_mutex
);
4709 * At the end of this, ftail must point to the place where the
4710 * new rule is to be saved/inserted/added.
4711 * For SIOCAD*FR, this should be the last rule in the group of
4712 * rules that have equal fr_collect fields.
4713 * For SIOCIN*FR, ...
4715 if (req
== (ioctlcmd_t
)SIOCADAFR
||
4716 req
== (ioctlcmd_t
)SIOCADIFR
) {
4718 for (ftail
= fprev
; (f
= *ftail
) != NULL
; ) {
4719 if (f
->fr_collect
> fp
->fr_collect
)
4721 ftail
= &f
->fr_next
;
4726 } else if (req
== (ioctlcmd_t
)SIOCINAFR
||
4727 req
== (ioctlcmd_t
)SIOCINIFR
) {
4728 while ((f
= *fprev
) != NULL
) {
4729 if (f
->fr_collect
>= fp
->fr_collect
)
4731 fprev
= &f
->fr_next
;
4734 if (fp
->fr_hits
!= 0) {
4735 while (fp
->fr_hits
&& (f
= *ftail
)) {
4736 if (f
->fr_collect
!= fp
->fr_collect
)
4739 ftail
= &f
->fr_next
;
4750 * Request to remove a rule.
4752 if (req
== (ioctlcmd_t
)SIOCRMAFR
|| req
== (ioctlcmd_t
)SIOCRMIFR
) {
4757 * Do not allow activity from user space to interfere
4758 * with rules not loaded that way.
4760 if ((makecopy
== 1) && !(f
->fr_flags
& FR_COPIED
)) {
4766 * Return EBUSY if the rule is being reference by
4767 * something else (eg state information.
4769 if (f
->fr_ref
> 1) {
4773 #ifdef IPFILTER_SCAN
4774 if (f
->fr_isctag
[0] != '\0' &&
4775 (f
->fr_isc
!= (struct ipscan
*)-1))
4778 if (unit
== IPL_LOGAUTH
) {
4779 error
= fr_preauthcmd(req
, f
, ftail
, ifs
);
4782 if (*f
->fr_grhead
!= '\0')
4783 fr_delgroup(f
->fr_grhead
, unit
, set
, ifs
);
4784 fr_fixskip(ftail
, f
, -1);
4785 *ftail
= f
->fr_next
;
4787 (void)fr_derefrule(&f
, ifs
);
4791 * Not removing, so we must be adding/inserting a rule.
4796 if (unit
== IPL_LOGAUTH
) {
4797 error
= fr_preauthcmd(req
, fp
, ftail
, ifs
);
4801 KMALLOC(f
, frentry_t
*);
4806 bcopy((char *)fp
, (char *)f
,
4808 MUTEX_NUKE(&f
->fr_lock
);
4809 MUTEX_INIT(&f
->fr_lock
, "filter rule lock");
4810 #ifdef IPFILTER_SCAN
4811 if (f
->fr_isctag
[0] != '\0' &&
4813 f
->fr_isc
= (struct ipscan
*)-1;
4818 f
->fr_next
= *ftail
;
4820 if (req
== (ioctlcmd_t
)SIOCINIFR
||
4821 req
== (ioctlcmd_t
)SIOCINAFR
)
4822 fr_fixskip(ftail
, f
, 1);
4824 group
= f
->fr_grhead
;
4825 if (*group
!= '\0') {
4826 fg
= fr_addgroup(group
, f
, f
->fr_flags
,
4829 f
->fr_grp
= &fg
->fg_start
;
4836 RWLOCK_EXIT(&ifs
->ifs_ipf_mutex
);
4837 if ((ptr
!= NULL
) && (error
!= 0) && (makecopy
!= 0)) {
4838 KFREES(ptr
, fp
->fr_dsize
);
4844 /* ------------------------------------------------------------------------ */
4845 /* Function: fr_funcinit */
4846 /* Returns: int - 0 == success, else ESRCH: cannot resolve rule details */
4847 /* Parameters: fr(I) - pointer to filter rule */
4849 /* If a rule is a call rule, then check if the function it points to needs */
4850 /* an init function to be called now the rule has been loaded. */
4851 /* ------------------------------------------------------------------------ */
4852 static int fr_funcinit(fr
, ifs
)
4856 ipfunc_resolve_t
*ft
;
4861 for (ft
= fr_availfuncs
; ft
->ipfu_addr
!= NULL
; ft
++)
4862 if (ft
->ipfu_addr
== fr
->fr_func
) {
4864 if (ft
->ipfu_init
!= NULL
)
4865 err
= (*ft
->ipfu_init
)(fr
, ifs
);
4872 /* ------------------------------------------------------------------------ */
4873 /* Function: fr_findfunc */
4874 /* Returns: ipfunc_t - pointer to function if found, else NULL */
4875 /* Parameters: funcptr(I) - function pointer to lookup */
4877 /* Look for a function in the table of known functions. */
4878 /* ------------------------------------------------------------------------ */
4879 static ipfunc_t
fr_findfunc(funcptr
)
4882 ipfunc_resolve_t
*ft
;
4884 for (ft
= fr_availfuncs
; ft
->ipfu_addr
!= NULL
; ft
++)
4885 if (ft
->ipfu_addr
== funcptr
)
4891 /* ------------------------------------------------------------------------ */
4892 /* Function: fr_resolvefunc */
4893 /* Returns: int - 0 == success, else error */
4894 /* Parameters: data(IO) - ioctl data pointer to ipfunc_resolve_t struct */
4896 /* Copy in a ipfunc_resolve_t structure and then fill in the missing field. */
4897 /* This will either be the function name (if the pointer is set) or the */
4898 /* function pointer if the name is set. When found, fill in the other one */
4899 /* so that the entire, complete, structure can be copied back to user space.*/
4900 /* ------------------------------------------------------------------------ */
4901 int fr_resolvefunc(data
)
4904 ipfunc_resolve_t res
, *ft
;
4907 err
= BCOPYIN(data
, &res
, sizeof(res
));
4911 if (res
.ipfu_addr
== NULL
&& res
.ipfu_name
[0] != '\0') {
4912 for (ft
= fr_availfuncs
; ft
->ipfu_addr
!= NULL
; ft
++)
4913 if (strncmp(res
.ipfu_name
, ft
->ipfu_name
,
4914 sizeof(res
.ipfu_name
)) == 0) {
4915 res
.ipfu_addr
= ft
->ipfu_addr
;
4916 res
.ipfu_init
= ft
->ipfu_init
;
4917 if (COPYOUT(&res
, data
, sizeof(res
)) != 0)
4922 if (res
.ipfu_addr
!= NULL
&& res
.ipfu_name
[0] == '\0') {
4923 for (ft
= fr_availfuncs
; ft
->ipfu_addr
!= NULL
; ft
++)
4924 if (ft
->ipfu_addr
== res
.ipfu_addr
) {
4925 (void) strncpy(res
.ipfu_name
, ft
->ipfu_name
,
4926 sizeof(res
.ipfu_name
));
4927 res
.ipfu_init
= ft
->ipfu_init
;
4928 if (COPYOUT(&res
, data
, sizeof(res
)) != 0)
4937 #if !defined(_KERNEL) || (!defined(__NetBSD__) && !defined(__OpenBSD__) && !defined(__FreeBSD__)) || \
4938 (defined(__FreeBSD__) && (__FreeBSD_version < 490000)) || \
4939 (defined(__NetBSD__) && (__NetBSD_Version__ < 105000000)) || \
4940 (defined(__OpenBSD__) && (OpenBSD < 200006))
4943 * ppsratecheck(): packets (or events) per second limitation.
4946 ppsratecheck(lasttime
, curpps
, maxpps
)
4947 struct timeval
*lasttime
;
4949 int maxpps
; /* maximum pps allowed */
4951 struct timeval tv
, delta
;
4956 delta
.tv_sec
= tv
.tv_sec
- lasttime
->tv_sec
;
4957 delta
.tv_usec
= tv
.tv_usec
- lasttime
->tv_usec
;
4958 if (delta
.tv_usec
< 0) {
4960 delta
.tv_usec
+= 1000000;
4964 * check for 0,0 is so that the message will be seen at least once.
4965 * if more than one second have passed since the last update of
4966 * lasttime, reset the counter.
4968 * we do increment *curpps even in *curpps < maxpps case, as some may
4969 * try to use *curpps for stat purposes as well.
4971 if ((lasttime
->tv_sec
== 0 && lasttime
->tv_usec
== 0) ||
4972 delta
.tv_sec
>= 1) {
4976 } else if (maxpps
< 0)
4978 else if (*curpps
< maxpps
)
4982 *curpps
= *curpps
+ 1;
4989 /* ------------------------------------------------------------------------ */
4990 /* Function: fr_derefrule */
4991 /* Returns: int - 0 == rule freed up, else rule not freed */
4992 /* Parameters: fr(I) - pointer to filter rule */
4994 /* Decrement the reference counter to a rule by one. If it reaches zero, */
4995 /* free it and any associated storage space being used by it. */
4996 /* ------------------------------------------------------------------------ */
4997 int fr_derefrule(frp
, ifs
)
5005 MUTEX_ENTER(&fr
->fr_lock
);
5007 if (fr
->fr_ref
== 0) {
5008 MUTEX_EXIT(&fr
->fr_lock
);
5009 MUTEX_DESTROY(&fr
->fr_lock
);
5011 #ifdef IPFILTER_LOOKUP
5012 if (fr
->fr_type
== FR_T_IPF
&& fr
->fr_satype
== FRI_LOOKUP
)
5013 ip_lookup_deref(fr
->fr_srctype
, fr
->fr_srcptr
, ifs
);
5014 if (fr
->fr_type
== FR_T_IPF
&& fr
->fr_datype
== FRI_LOOKUP
)
5015 ip_lookup_deref(fr
->fr_dsttype
, fr
->fr_dstptr
, ifs
);
5019 KFREES(fr
->fr_data
, fr
->fr_dsize
);
5021 if ((fr
->fr_flags
& FR_COPIED
) != 0) {
5027 MUTEX_EXIT(&fr
->fr_lock
);
5034 #ifdef IPFILTER_LOOKUP
5035 /* ------------------------------------------------------------------------ */
5036 /* Function: fr_grpmapinit */
5037 /* Returns: int - 0 == success, else ESRCH because table entry not found*/
5038 /* Parameters: fr(I) - pointer to rule to find hash table for */
5040 /* Looks for group hash table fr_arg and stores a pointer to it in fr_ptr. */
5041 /* fr_ptr is later used by fr_srcgrpmap and fr_dstgrpmap. */
5042 /* ------------------------------------------------------------------------ */
5043 static int fr_grpmapinit(fr
, ifs
)
5047 char name
[FR_GROUPLEN
];
5050 #if defined(SNPRINTF) && defined(_KERNEL)
5051 (void) SNPRINTF(name
, sizeof(name
), "%d", fr
->fr_arg
);
5053 (void) sprintf(name
, "%d", fr
->fr_arg
);
5055 iph
= fr_findhtable(IPL_LOGIPF
, name
, ifs
);
5058 if ((iph
->iph_flags
& FR_INOUT
) != (fr
->fr_flags
& FR_INOUT
))
5065 /* ------------------------------------------------------------------------ */
5066 /* Function: fr_srcgrpmap */
5067 /* Returns: frentry_t * - pointer to "new last matching" rule or NULL */
5068 /* Parameters: fin(I) - pointer to packet information */
5069 /* passp(IO) - pointer to current/new filter decision (unused) */
5071 /* Look for a rule group head in a hash table, using the source address as */
5072 /* the key, and descend into that group and continue matching rules against */
5074 /* ------------------------------------------------------------------------ */
5075 frentry_t
*fr_srcgrpmap(fin
, passp
)
5081 ipf_stack_t
*ifs
= fin
->fin_ifs
;
5083 rval
= fr_iphmfindgroup(fin
->fin_fr
->fr_ptr
, fin
->fin_v
, &fin
->fin_src
, ifs
);
5088 fin
->fin_fr
= fg
->fg_start
;
5089 (void) fr_scanlist(fin
, *passp
);
5094 /* ------------------------------------------------------------------------ */
5095 /* Function: fr_dstgrpmap */
5096 /* Returns: frentry_t * - pointer to "new last matching" rule or NULL */
5097 /* Parameters: fin(I) - pointer to packet information */
5098 /* passp(IO) - pointer to current/new filter decision (unused) */
5100 /* Look for a rule group head in a hash table, using the destination */
5101 /* address as the key, and descend into that group and continue matching */
5102 /* rules against the packet. */
5103 /* ------------------------------------------------------------------------ */
5104 frentry_t
*fr_dstgrpmap(fin
, passp
)
5110 ipf_stack_t
*ifs
= fin
->fin_ifs
;
5112 rval
= fr_iphmfindgroup(fin
->fin_fr
->fr_ptr
, fin
->fin_v
, &fin
->fin_dst
, ifs
);
5117 fin
->fin_fr
= fg
->fg_start
;
5118 (void) fr_scanlist(fin
, *passp
);
5121 #endif /* IPFILTER_LOOKUP */
5126 * These functions manage objects on queues for efficient timeouts. There are
5127 * a number of system defined queues as well as user defined timeouts. It is
5128 * expected that a lock is held in the domain in which the queue belongs
5129 * (i.e. either state or NAT) when calling any of these functions that prevents
5130 * fr_freetimeoutqueue() from being called at the same time as any other.
5134 /* ------------------------------------------------------------------------ */
5135 /* Function: fr_addtimeoutqueue */
5136 /* Returns: struct ifqtq * - NULL if malloc fails, else pointer to */
5137 /* timeout queue with given interval. */
5138 /* Parameters: parent(I) - pointer to pointer to parent node of this list */
5139 /* of interface queues. */
5140 /* seconds(I) - timeout value in seconds for this queue. */
5142 /* This routine first looks for a timeout queue that matches the interval */
5143 /* being requested. If it finds one, increments the reference counter and */
5144 /* returns a pointer to it. If none are found, it allocates a new one and */
5145 /* inserts it at the top of the list. */
5148 /* It is assumed that the caller of this function has an appropriate lock */
5149 /* held (exclusively) in the domain that encompases 'parent'. */
5150 /* ------------------------------------------------------------------------ */
5151 ipftq_t
*fr_addtimeoutqueue(parent
, seconds
, ifs
)
5159 period
= seconds
* IPF_HZ_DIVIDE
;
5161 MUTEX_ENTER(&ifs
->ifs_ipf_timeoutlock
);
5162 for (ifq
= *parent
; ifq
!= NULL
; ifq
= ifq
->ifq_next
) {
5163 if (ifq
->ifq_ttl
== period
) {
5165 * Reset the delete flag, if set, so the structure
5166 * gets reused rather than freed and reallocated.
5168 MUTEX_ENTER(&ifq
->ifq_lock
);
5169 ifq
->ifq_flags
&= ~IFQF_DELETE
;
5171 MUTEX_EXIT(&ifq
->ifq_lock
);
5172 MUTEX_EXIT(&ifs
->ifs_ipf_timeoutlock
);
5178 KMALLOC(ifq
, ipftq_t
*);
5180 ifq
->ifq_ttl
= period
;
5181 ifq
->ifq_head
= NULL
;
5182 ifq
->ifq_tail
= &ifq
->ifq_head
;
5183 ifq
->ifq_next
= *parent
;
5184 ifq
->ifq_pnext
= parent
;
5186 ifq
->ifq_flags
= IFQF_USER
;
5188 ifs
->ifs_fr_userifqs
++;
5189 MUTEX_NUKE(&ifq
->ifq_lock
);
5190 MUTEX_INIT(&ifq
->ifq_lock
, "ipftq mutex");
5192 MUTEX_EXIT(&ifs
->ifs_ipf_timeoutlock
);
5197 /* ------------------------------------------------------------------------ */
5198 /* Function: fr_deletetimeoutqueue */
5199 /* Returns: int - new reference count value of the timeout queue */
5200 /* Parameters: ifq(I) - timeout queue which is losing a reference. */
5201 /* Locks: ifq->ifq_lock */
5203 /* This routine must be called when we're discarding a pointer to a timeout */
5204 /* queue object, taking care of the reference counter. */
5206 /* Now that this just sets a DELETE flag, it requires the expire code to */
5207 /* check the list of user defined timeout queues and call the free function */
5208 /* below (currently commented out) to stop memory leaking. It is done this */
5209 /* way because the locking may not be sufficient to safely do a free when */
5210 /* this function is called. */
5211 /* ------------------------------------------------------------------------ */
5212 int fr_deletetimeoutqueue(ifq
)
5217 if ((ifq
->ifq_ref
== 0) && ((ifq
->ifq_flags
& IFQF_USER
) != 0)) {
5218 ifq
->ifq_flags
|= IFQF_DELETE
;
5221 return ifq
->ifq_ref
;
5225 /* ------------------------------------------------------------------------ */
5226 /* Function: fr_freetimeoutqueue */
5227 /* Parameters: ifq(I) - timeout queue which is losing a reference. */
5231 /* It is assumed that the caller of this function has an appropriate lock */
5232 /* held (exclusively) in the domain that encompases the callers "domain". */
5233 /* The ifq_lock for this structure should not be held. */
5235 /* Remove a user definde timeout queue from the list of queues it is in and */
5236 /* tidy up after this is done. */
5237 /* ------------------------------------------------------------------------ */
5238 void fr_freetimeoutqueue(ifq
, ifs
)
5244 if (((ifq
->ifq_flags
& IFQF_DELETE
) == 0) || (ifq
->ifq_ref
!= 0) ||
5245 ((ifq
->ifq_flags
& IFQF_USER
) == 0)) {
5246 printf("fr_freetimeoutqueue(%lx) flags 0x%x ttl %d ref %d\n",
5247 (u_long
)ifq
, ifq
->ifq_flags
, ifq
->ifq_ttl
,
5253 * Remove from its position in the list.
5255 *ifq
->ifq_pnext
= ifq
->ifq_next
;
5256 if (ifq
->ifq_next
!= NULL
)
5257 ifq
->ifq_next
->ifq_pnext
= ifq
->ifq_pnext
;
5259 MUTEX_DESTROY(&ifq
->ifq_lock
);
5260 ifs
->ifs_fr_userifqs
--;
5265 /* ------------------------------------------------------------------------ */
5266 /* Function: fr_deletequeueentry */
5268 /* Parameters: tqe(I) - timeout queue entry to delete */
5269 /* ifq(I) - timeout queue to remove entry from */
5271 /* Remove a tail queue entry from its queue and make it an orphan. */
5272 /* fr_deletetimeoutqueue is called to make sure the reference count on the */
5273 /* queue is correct. We can't, however, call fr_freetimeoutqueue because */
5274 /* the correct lock(s) may not be held that would make it safe to do so. */
5275 /* ------------------------------------------------------------------------ */
5276 void fr_deletequeueentry(tqe
)
5285 MUTEX_ENTER(&ifq
->ifq_lock
);
5287 if (tqe
->tqe_pnext
!= NULL
) {
5288 *tqe
->tqe_pnext
= tqe
->tqe_next
;
5289 if (tqe
->tqe_next
!= NULL
)
5290 tqe
->tqe_next
->tqe_pnext
= tqe
->tqe_pnext
;
5291 else /* we must be the tail anyway */
5292 ifq
->ifq_tail
= tqe
->tqe_pnext
;
5294 tqe
->tqe_pnext
= NULL
;
5295 tqe
->tqe_ifq
= NULL
;
5298 (void) fr_deletetimeoutqueue(ifq
);
5300 MUTEX_EXIT(&ifq
->ifq_lock
);
5304 /* ------------------------------------------------------------------------ */
5305 /* Function: fr_queuefront */
5307 /* Parameters: tqe(I) - pointer to timeout queue entry */
5309 /* Move a queue entry to the front of the queue, if it isn't already there. */
5310 /* ------------------------------------------------------------------------ */
5311 void fr_queuefront(tqe
)
5320 MUTEX_ENTER(&ifq
->ifq_lock
);
5321 if (ifq
->ifq_head
!= tqe
) {
5322 *tqe
->tqe_pnext
= tqe
->tqe_next
;
5324 tqe
->tqe_next
->tqe_pnext
= tqe
->tqe_pnext
;
5326 ifq
->ifq_tail
= tqe
->tqe_pnext
;
5328 tqe
->tqe_next
= ifq
->ifq_head
;
5329 ifq
->ifq_head
->tqe_pnext
= &tqe
->tqe_next
;
5330 ifq
->ifq_head
= tqe
;
5331 tqe
->tqe_pnext
= &ifq
->ifq_head
;
5333 MUTEX_EXIT(&ifq
->ifq_lock
);
5337 /* ------------------------------------------------------------------------ */
5338 /* Function: fr_queueback */
5340 /* Parameters: tqe(I) - pointer to timeout queue entry */
5342 /* Move a queue entry to the back of the queue, if it isn't already there. */
5343 /* ------------------------------------------------------------------------ */
5344 void fr_queueback(tqe
, ifs
)
5353 tqe
->tqe_die
= ifs
->ifs_fr_ticks
+ ifq
->ifq_ttl
;
5355 MUTEX_ENTER(&ifq
->ifq_lock
);
5356 if (tqe
->tqe_next
== NULL
) { /* at the end already ? */
5357 MUTEX_EXIT(&ifq
->ifq_lock
);
5364 *tqe
->tqe_pnext
= tqe
->tqe_next
;
5365 tqe
->tqe_next
->tqe_pnext
= tqe
->tqe_pnext
;
5368 * Make it the last entry.
5370 tqe
->tqe_next
= NULL
;
5371 tqe
->tqe_pnext
= ifq
->ifq_tail
;
5372 *ifq
->ifq_tail
= tqe
;
5373 ifq
->ifq_tail
= &tqe
->tqe_next
;
5374 MUTEX_EXIT(&ifq
->ifq_lock
);
5378 /* ------------------------------------------------------------------------ */
5379 /* Function: fr_queueappend */
5381 /* Parameters: tqe(I) - pointer to timeout queue entry */
5382 /* ifq(I) - pointer to timeout queue */
5383 /* parent(I) - owing object pointer */
5385 /* Add a new item to this queue and put it on the very end. */
5386 /* ------------------------------------------------------------------------ */
5387 void fr_queueappend(tqe
, ifq
, parent
, ifs
)
5394 MUTEX_ENTER(&ifq
->ifq_lock
);
5395 tqe
->tqe_parent
= parent
;
5396 tqe
->tqe_pnext
= ifq
->ifq_tail
;
5397 *ifq
->ifq_tail
= tqe
;
5398 ifq
->ifq_tail
= &tqe
->tqe_next
;
5399 tqe
->tqe_next
= NULL
;
5401 tqe
->tqe_die
= ifs
->ifs_fr_ticks
+ ifq
->ifq_ttl
;
5403 MUTEX_EXIT(&ifq
->ifq_lock
);
5407 /* ------------------------------------------------------------------------ */
5408 /* Function: fr_movequeue */
5410 /* Parameters: tq(I) - pointer to timeout queue information */
5411 /* oifp(I) - old timeout queue entry was on */
5412 /* nifp(I) - new timeout queue to put entry on */
5413 /* ifs - ipf stack instance */
5415 /* Move a queue entry from one timeout queue to another timeout queue. */
5416 /* If it notices that the current entry is already last and does not need */
5417 /* to move queue, the return. */
5418 /* ------------------------------------------------------------------------ */
5419 void fr_movequeue(tqe
, oifq
, nifq
, ifs
)
5421 ipftq_t
*oifq
, *nifq
;
5425 * If the queue isn't changing, and the clock hasn't ticked
5426 * since the last update, the operation will be a no-op.
5428 if (oifq
== nifq
&& tqe
->tqe_touched
== ifs
->ifs_fr_ticks
)
5432 * Grab the lock and update the timers.
5434 MUTEX_ENTER(&oifq
->ifq_lock
);
5435 tqe
->tqe_touched
= ifs
->ifs_fr_ticks
;
5436 tqe
->tqe_die
= ifs
->ifs_fr_ticks
+ nifq
->ifq_ttl
;
5439 * The remainder of the operation can still be a no-op.
5441 * If the queue isn't changing, check to see if
5442 * an update would be meaningless.
5445 if ((tqe
->tqe_next
== NULL
) ||
5446 (tqe
->tqe_next
->tqe_die
== tqe
->tqe_die
)) {
5447 MUTEX_EXIT(&oifq
->ifq_lock
);
5453 * Remove from the old queue
5455 *tqe
->tqe_pnext
= tqe
->tqe_next
;
5457 tqe
->tqe_next
->tqe_pnext
= tqe
->tqe_pnext
;
5459 oifq
->ifq_tail
= tqe
->tqe_pnext
;
5460 tqe
->tqe_next
= NULL
;
5463 * If we're moving from one queue to another, release the lock on the
5464 * old queue and get a lock on the new queue. For user defined queues,
5465 * if we're moving off it, call delete in case it can now be freed.
5468 tqe
->tqe_ifq
= NULL
;
5470 (void) fr_deletetimeoutqueue(oifq
);
5472 MUTEX_EXIT(&oifq
->ifq_lock
);
5474 MUTEX_ENTER(&nifq
->ifq_lock
);
5476 tqe
->tqe_ifq
= nifq
;
5481 * Add to the bottom of the new queue
5483 tqe
->tqe_pnext
= nifq
->ifq_tail
;
5484 *nifq
->ifq_tail
= tqe
;
5485 nifq
->ifq_tail
= &tqe
->tqe_next
;
5486 MUTEX_EXIT(&nifq
->ifq_lock
);
5490 /* ------------------------------------------------------------------------ */
5491 /* Function: fr_updateipid */
5492 /* Returns: int - 0 == success, -1 == error (packet should be droppped) */
5493 /* Parameters: fin(I) - pointer to packet information */
5495 /* When we are doing NAT, change the IP of every packet to represent a */
5496 /* single sequence of packets coming from the host, hiding any host */
5497 /* specific sequencing that might otherwise be revealed. If the packet is */
5498 /* a fragment, then store the 'new' IPid in the fragment cache and look up */
5499 /* the fragment cache for non-leading fragments. If a non-leading fragment */
5500 /* has no match in the cache, return an error. */
5501 /* ------------------------------------------------------------------------ */
5502 static INLINE
int fr_updateipid(fin
)
5505 u_short id
, ido
, sums
;
5509 if (fin
->fin_off
!= 0) {
5510 sum
= fr_ipid_knownfrag(fin
);
5511 if (sum
== 0xffffffff)
5516 id
= fr_nextipid(fin
);
5517 if (fin
->fin_off
== 0 && (fin
->fin_flx
& FI_FRAG
) != 0)
5518 (void) fr_ipid_newfrag(fin
, (u_32_t
)id
);
5522 ido
= ntohs(ip
->ip_id
);
5525 ip
->ip_id
= htons(id
);
5526 CALC_SUMD(ido
, id
, sumd
); /* DESTRUCTIVE MACRO! id,ido change */
5527 sum
= (~ntohs(ip
->ip_sum
)) & 0xffff;
5529 sum
= (sum
>> 16) + (sum
& 0xffff);
5530 sum
= (sum
>> 16) + (sum
& 0xffff);
5531 sums
= ~(u_short
)sum
;
5532 ip
->ip_sum
= htons(sums
);
5537 #ifdef NEED_FRGETIFNAME
5538 /* ------------------------------------------------------------------------ */
5539 /* Function: fr_getifname */
5540 /* Returns: char * - pointer to interface name */
5541 /* Parameters: ifp(I) - pointer to network interface */
5542 /* buffer(O) - pointer to where to store interface name */
5544 /* Constructs an interface name in the buffer passed. The buffer passed is */
5545 /* expected to be at least LIFNAMSIZ in bytes big. If buffer is passed in */
5546 /* as a NULL pointer then return a pointer to a static array. */
5547 /* ------------------------------------------------------------------------ */
5548 char *fr_getifname(ifp
, buffer
)
5552 static char namebuf
[LIFNAMSIZ
];
5553 # if defined(MENTAT) || defined(__FreeBSD__) || defined(__osf__) || \
5554 defined(__sgi) || defined(linux) || defined(_AIX51) || \
5555 (defined(sun) && !defined(__SVR4) && !defined(__svr4__))
5561 ASSERT(buffer
!= NULL
);
5566 (void) strncpy(buffer
, ifp
->if_name
, LIFNAMSIZ
);
5567 buffer
[LIFNAMSIZ
- 1] = '\0';
5568 # if defined(MENTAT) || defined(__FreeBSD__) || defined(__osf__) || \
5569 defined(__sgi) || defined(_AIX51) || \
5570 (defined(sun) && !defined(__SVR4) && !defined(__svr4__))
5571 for (s
= buffer
; *s
; s
++)
5573 unit
= ifp
->if_unit
;
5574 space
= LIFNAMSIZ
- (s
- buffer
);
5576 # if defined(SNPRINTF) && defined(_KERNEL)
5577 (void) SNPRINTF(temp
, sizeof(temp
), "%d", unit
);
5579 (void) sprintf(temp
, "%d", unit
);
5581 (void) strncpy(s
, temp
, space
);
5589 /* ------------------------------------------------------------------------ */
5590 /* Function: fr_ioctlswitch */
5591 /* Returns: int - -1 continue processing, else ioctl return value */
5592 /* Parameters: unit(I) - device unit opened */
5593 /* data(I) - pointer to ioctl data */
5594 /* cmd(I) - ioctl command */
5595 /* mode(I) - mode value */
5597 /* Based on the value of unit, call the appropriate ioctl handler or return */
5598 /* EIO if ipfilter is not running. Also checks if write perms are req'd */
5599 /* for the device in order to execute the ioctl. */
5600 /* ------------------------------------------------------------------------ */
5601 INLINE
int fr_ioctlswitch(unit
, data
, cmd
, mode
, uid
, ctx
, ifs
)
5602 int unit
, mode
, uid
;
5615 if (ifs
->ifs_fr_running
> 0)
5616 error
= fr_nat_ioctl(data
, cmd
, mode
, uid
, ctx
, ifs
);
5621 if (ifs
->ifs_fr_running
> 0)
5622 error
= fr_state_ioctl(data
, cmd
, mode
, uid
, ctx
, ifs
);
5627 if (ifs
->ifs_fr_running
> 0) {
5628 if ((cmd
== (ioctlcmd_t
)SIOCADAFR
) ||
5629 (cmd
== (ioctlcmd_t
)SIOCRMAFR
)) {
5630 if (!(mode
& FWRITE
)) {
5633 error
= frrequest(unit
, cmd
, data
,
5634 ifs
->ifs_fr_active
, 1, ifs
);
5637 error
= fr_auth_ioctl(data
, cmd
, mode
, uid
, ctx
, ifs
);
5643 #ifdef IPFILTER_SYNC
5644 if (ifs
->ifs_fr_running
> 0)
5645 error
= fr_sync_ioctl(data
, cmd
, mode
, ifs
);
5651 #ifdef IPFILTER_SCAN
5652 if (ifs
->ifs_fr_running
> 0)
5653 error
= fr_scan_ioctl(data
, cmd
, mode
, ifs
);
5658 case IPL_LOGLOOKUP
:
5659 #ifdef IPFILTER_LOOKUP
5660 if (ifs
->ifs_fr_running
> 0)
5661 error
= ip_lookup_ioctl(data
, cmd
, mode
, uid
, ctx
, ifs
);
5676 * This array defines the expected size of objects coming into the kernel
5677 * for the various recognised object types.
5679 #define NUM_OBJ_TYPES 19
5681 static int fr_objbytes
[NUM_OBJ_TYPES
][2] = {
5682 { 1, sizeof(struct frentry
) }, /* frentry */
5683 { 0, sizeof(struct friostat
) },
5684 { 0, sizeof(struct fr_info
) },
5685 { 0, sizeof(struct fr_authstat
) },
5686 { 0, sizeof(struct ipfrstat
) },
5687 { 0, sizeof(struct ipnat
) },
5688 { 0, sizeof(struct natstat
) },
5689 { 0, sizeof(struct ipstate_save
) },
5690 { 1, sizeof(struct nat_save
) }, /* nat_save */
5691 { 0, sizeof(struct natlookup
) },
5692 { 1, sizeof(struct ipstate
) }, /* ipstate */
5693 { 0, sizeof(struct ips_stat
) },
5694 { 0, sizeof(struct frauth
) },
5695 { 0, sizeof(struct ipftune
) },
5696 { 0, sizeof(struct nat
) }, /* nat_t */
5697 { 0, sizeof(struct ipfruleiter
) },
5698 { 0, sizeof(struct ipfgeniter
) },
5699 { 0, sizeof(struct ipftable
) },
5700 { 0, sizeof(struct ipflookupiter
) }
5704 /* ------------------------------------------------------------------------ */
5705 /* Function: fr_getzoneid */
5706 /* Returns: int - 0 = success, else failure */
5707 /* Parameters: idsp(I) - pointer to ipf_devstate_t */
5708 /* data(I) - pointer to ioctl data */
5710 /* Set the zone ID in idsp based on the zone name in ipfzoneobj. Further */
5711 /* ioctls will act on the IPF stack for that zone ID. */
5712 /* ------------------------------------------------------------------------ */
5713 #if defined(_KERNEL)
5714 int fr_setzoneid(idsp
, data
)
5715 ipf_devstate_t
*idsp
;
5722 error
= BCOPYIN(data
, &ipfzo
, sizeof(ipfzo
));
5726 if (memchr(ipfzo
.ipfz_zonename
, '\0', ZONENAME_MAX
) == NULL
)
5730 * The global zone doesn't have a GZ-controlled stack, so no
5731 * sense in going any further
5733 if (strcmp(ipfzo
.ipfz_zonename
, "global") == 0)
5736 if ((zone
= zone_find_by_name(ipfzo
.ipfz_zonename
)) == NULL
)
5740 * Store the zone ID that to control, and whether it's the
5741 * GZ-controlled stack that's wanted
5743 idsp
->ipfs_zoneid
= zone
->zone_id
;
5744 idsp
->ipfs_gz
= (ipfzo
.ipfz_gz
== 1) ? B_TRUE
: B_FALSE
;
5752 /* ------------------------------------------------------------------------ */
5753 /* Function: fr_inobj */
5754 /* Returns: int - 0 = success, else failure */
5755 /* Parameters: data(I) - pointer to ioctl data */
5756 /* ptr(I) - pointer to store real data in */
5757 /* type(I) - type of structure being moved */
5759 /* Copy in the contents of what the ipfobj_t points to. In future, we */
5760 /* add things to check for version numbers, sizes, etc, to make it backward */
5761 /* compatible at the ABI for user land. */
5762 /* ------------------------------------------------------------------------ */
5763 int fr_inobj(data
, ptr
, type
)
5771 if ((type
< 0) || (type
> NUM_OBJ_TYPES
-1))
5774 error
= BCOPYIN((caddr_t
)data
, (caddr_t
)&obj
, sizeof(obj
));
5778 if (obj
.ipfo_type
!= type
)
5781 #ifndef IPFILTER_COMPAT
5782 if ((fr_objbytes
[type
][0] & 1) != 0) {
5783 if (obj
.ipfo_size
< fr_objbytes
[type
][1])
5785 } else if (obj
.ipfo_size
!= fr_objbytes
[type
][1])
5788 if (obj
.ipfo_rev
!= IPFILTER_VERSION
) {
5789 error
= fr_incomptrans(&obj
, ptr
);
5793 if ((fr_objbytes
[type
][0] & 1) != 0 &&
5794 obj
.ipfo_size
< fr_objbytes
[type
][1] ||
5795 obj
.ipfo_size
!= fr_objbytes
[type
][1])
5799 if ((fr_objbytes
[type
][0] & 1) != 0) {
5800 error
= COPYIN((caddr_t
)obj
.ipfo_ptr
, (caddr_t
)ptr
,
5801 fr_objbytes
[type
][1]);
5803 error
= COPYIN((caddr_t
)obj
.ipfo_ptr
, (caddr_t
)ptr
,
5810 /* ------------------------------------------------------------------------ */
5811 /* Function: fr_inobjsz */
5812 /* Returns: int - 0 = success, else failure */
5813 /* Parameters: data(I) - pointer to ioctl data */
5814 /* ptr(I) - pointer to store real data in */
5815 /* type(I) - type of structure being moved */
5816 /* sz(I) - size of data to copy */
5818 /* As per fr_inobj, except the size of the object to copy in is passed in */
5819 /* but it must not be smaller than the size defined for the type and the */
5820 /* type must allow for varied sized objects. The extra requirement here is */
5821 /* that sz must match the size of the object being passed in - this is not */
5822 /* not possible nor required in fr_inobj(). */
5823 /* ------------------------------------------------------------------------ */
5824 int fr_inobjsz(data
, ptr
, type
, sz
)
5832 if ((type
< 0) || (type
> NUM_OBJ_TYPES
-1))
5834 if (((fr_objbytes
[type
][0] & 1) == 0) || (sz
< fr_objbytes
[type
][1]))
5837 error
= BCOPYIN((caddr_t
)data
, (caddr_t
)&obj
, sizeof(obj
));
5841 if (obj
.ipfo_type
!= type
)
5844 #ifndef IPFILTER_COMPAT
5845 if (obj
.ipfo_size
!= sz
)
5848 if (obj
.ipfo_rev
!= IPFILTER_VERSION
)
5849 /*XXX compatibility hook here */
5851 if (obj
.ipfo_size
!= sz
)
5852 /* XXX compatibility hook here */
5856 error
= COPYIN((caddr_t
)obj
.ipfo_ptr
, (caddr_t
)ptr
, sz
);
5861 /* ------------------------------------------------------------------------ */
5862 /* Function: fr_outobjsz */
5863 /* Returns: int - 0 = success, else failure */
5864 /* Parameters: data(I) - pointer to ioctl data */
5865 /* ptr(I) - pointer to store real data in */
5866 /* type(I) - type of structure being moved */
5867 /* sz(I) - size of data to copy */
5869 /* As per fr_outobj, except the size of the object to copy out is passed in */
5870 /* but it must not be smaller than the size defined for the type and the */
5871 /* type must allow for varied sized objects. The extra requirement here is */
5872 /* that sz must match the size of the object being passed in - this is not */
5873 /* not possible nor required in fr_outobj(). */
5874 /* ------------------------------------------------------------------------ */
5875 int fr_outobjsz(data
, ptr
, type
, sz
)
5883 if ((type
< 0) || (type
> NUM_OBJ_TYPES
-1) ||
5884 ((fr_objbytes
[type
][0] & 1) == 0) ||
5885 (sz
< fr_objbytes
[type
][1]))
5888 error
= BCOPYIN((caddr_t
)data
, (caddr_t
)&obj
, sizeof(obj
));
5892 if (obj
.ipfo_type
!= type
)
5895 #ifndef IPFILTER_COMPAT
5896 if (obj
.ipfo_size
!= sz
)
5899 if (obj
.ipfo_rev
!= IPFILTER_VERSION
)
5900 /* XXX compatibility hook here */
5902 if (obj
.ipfo_size
!= sz
)
5903 /* XXX compatibility hook here */
5907 error
= COPYOUT((caddr_t
)ptr
, (caddr_t
)obj
.ipfo_ptr
, sz
);
5912 /* ------------------------------------------------------------------------ */
5913 /* Function: fr_outobj */
5914 /* Returns: int - 0 = success, else failure */
5915 /* Parameters: data(I) - pointer to ioctl data */
5916 /* ptr(I) - pointer to store real data in */
5917 /* type(I) - type of structure being moved */
5919 /* Copy out the contents of what ptr is to where ipfobj points to. In */
5920 /* future, we add things to check for version numbers, sizes, etc, to make */
5921 /* it backward compatible at the ABI for user land. */
5922 /* ------------------------------------------------------------------------ */
5923 int fr_outobj(data
, ptr
, type
)
5931 if ((type
< 0) || (type
> NUM_OBJ_TYPES
-1))
5934 error
= BCOPYIN((caddr_t
)data
, (caddr_t
)&obj
, sizeof(obj
));
5938 if (obj
.ipfo_type
!= type
)
5941 #ifndef IPFILTER_COMPAT
5942 if ((fr_objbytes
[type
][0] & 1) != 0) {
5943 if (obj
.ipfo_size
< fr_objbytes
[type
][1])
5945 } else if (obj
.ipfo_size
!= fr_objbytes
[type
][1])
5948 if (obj
.ipfo_rev
!= IPFILTER_VERSION
) {
5949 error
= fr_outcomptrans(&obj
, ptr
);
5953 if ((fr_objbytes
[type
][0] & 1) != 0 &&
5954 obj
.ipfo_size
< fr_objbytes
[type
][1] ||
5955 obj
.ipfo_size
!= fr_objbytes
[type
][1])
5959 error
= COPYOUT((caddr_t
)ptr
, (caddr_t
)obj
.ipfo_ptr
, obj
.ipfo_size
);
5964 /* ------------------------------------------------------------------------ */
5965 /* Function: fr_checkl4sum */
5966 /* Returns: int - 0 = good, -1 = bad, 1 = cannot check */
5967 /* Parameters: fin(I) - pointer to packet information */
5969 /* If possible, calculate the layer 4 checksum for the packet. If this is */
5970 /* not possible, return without indicating a failure or success but in a */
5971 /* way that is ditinguishable. */
5972 /* ------------------------------------------------------------------------ */
5973 int fr_checkl4sum(fin
)
5976 u_short sum
, hdrsum
, *csump
;
5979 ipf_stack_t
*ifs
= fin
->fin_ifs
;
5981 #if defined(SOLARIS) && defined(_KERNEL) && (SOLARIS2 >= 6)
5982 net_handle_t net_data_p
;
5983 if (fin
->fin_v
== 4)
5984 net_data_p
= ifs
->ifs_ipf_ipv4
;
5986 net_data_p
= ifs
->ifs_ipf_ipv6
;
5989 if ((fin
->fin_flx
& FI_NOCKSUM
) != 0)
5993 * If the TCP packet isn't a fragment, isn't too short and otherwise
5994 * isn't already considered "bad", then validate the checksum. If
5995 * this check fails then considered the packet to be "bad".
5997 if ((fin
->fin_flx
& (FI_FRAG
|FI_SHORT
|FI_BAD
)) != 0)
6005 #if defined(SOLARIS) && defined(_KERNEL) && (SOLARIS2 >= 6)
6006 ASSERT(fin
->fin_m
!= NULL
);
6007 if (NET_IS_HCK_L4_FULL(net_data_p
, fin
->fin_m
) ||
6008 NET_IS_HCK_L4_PART(net_data_p
, fin
->fin_m
)) {
6016 csump
= &((tcphdr_t
*)fin
->fin_dp
)->th_sum
;
6022 if (udp
->uh_sum
!= 0) {
6023 csump
= &udp
->uh_sum
;
6029 csump
= &((struct icmp
*)fin
->fin_dp
)->icmp_cksum
;
6042 sum
= fr_cksum(fin
->fin_m
, fin
->fin_ip
,
6043 fin
->fin_p
, fin
->fin_dp
);
6044 #if defined(SOLARIS) && defined(_KERNEL) && (SOLARIS2 >= 6)
6047 #if !defined(_KERNEL)
6048 if (sum
== hdrsum
) {
6049 FR_DEBUG(("checkl4sum: %hx == %hx\n", sum
, hdrsum
));
6051 FR_DEBUG(("checkl4sum: %hx != %hx\n", sum
, hdrsum
));
6060 /* ------------------------------------------------------------------------ */
6061 /* Function: fr_ifpfillv4addr */
6062 /* Returns: int - 0 = address update, -1 = address not updated */
6063 /* Parameters: atype(I) - type of network address update to perform */
6064 /* sin(I) - pointer to source of address information */
6065 /* mask(I) - pointer to source of netmask information */
6066 /* inp(I) - pointer to destination address store */
6067 /* inpmask(I) - pointer to destination netmask store */
6069 /* Given a type of network address update (atype) to perform, copy */
6070 /* information from sin/mask into inp/inpmask. If ipnmask is NULL then no */
6071 /* netmask update is performed unless FRI_NETMASKED is passed as atype, in */
6072 /* which case the operation fails. For all values of atype other than */
6073 /* FRI_NETMASKED, if inpmask is non-NULL then the mask is set to an all 1s */
6075 /* ------------------------------------------------------------------------ */
6076 int fr_ifpfillv4addr(atype
, sin
, mask
, inp
, inpmask
)
6078 struct sockaddr_in
*sin
, *mask
;
6079 struct in_addr
*inp
, *inpmask
;
6081 if (inpmask
!= NULL
&& atype
!= FRI_NETMASKED
)
6082 inpmask
->s_addr
= 0xffffffff;
6084 if (atype
== FRI_NETWORK
|| atype
== FRI_NETMASKED
) {
6085 if (atype
== FRI_NETMASKED
) {
6086 if (inpmask
== NULL
)
6088 inpmask
->s_addr
= mask
->sin_addr
.s_addr
;
6090 inp
->s_addr
= sin
->sin_addr
.s_addr
& mask
->sin_addr
.s_addr
;
6092 inp
->s_addr
= sin
->sin_addr
.s_addr
;
6099 /* ------------------------------------------------------------------------ */
6100 /* Function: fr_ifpfillv6addr */
6101 /* Returns: int - 0 = address update, -1 = address not updated */
6102 /* Parameters: atype(I) - type of network address update to perform */
6103 /* sin(I) - pointer to source of address information */
6104 /* mask(I) - pointer to source of netmask information */
6105 /* inp(I) - pointer to destination address store */
6106 /* inpmask(I) - pointer to destination netmask store */
6108 /* Given a type of network address update (atype) to perform, copy */
6109 /* information from sin/mask into inp/inpmask. If ipnmask is NULL then no */
6110 /* netmask update is performed unless FRI_NETMASKED is passed as atype, in */
6111 /* which case the operation fails. For all values of atype other than */
6112 /* FRI_NETMASKED, if inpmask is non-NULL then the mask is set to an all 1s */
6114 /* ------------------------------------------------------------------------ */
6115 int fr_ifpfillv6addr(atype
, sin
, mask
, inp
, inpmask
)
6117 struct sockaddr_in6
*sin
, *mask
;
6118 struct in_addr
*inp
, *inpmask
;
6120 i6addr_t
*src
, *dst
, *and, *dmask
;
6122 src
= (i6addr_t
*)&sin
->sin6_addr
;
6123 and = (i6addr_t
*)&mask
->sin6_addr
;
6124 dst
= (i6addr_t
*)inp
;
6125 dmask
= (i6addr_t
*)inpmask
;
6127 if (inpmask
!= NULL
&& atype
!= FRI_NETMASKED
) {
6128 dmask
->i6
[0] = 0xffffffff;
6129 dmask
->i6
[1] = 0xffffffff;
6130 dmask
->i6
[2] = 0xffffffff;
6131 dmask
->i6
[3] = 0xffffffff;
6134 if (atype
== FRI_NETWORK
|| atype
== FRI_NETMASKED
) {
6135 if (atype
== FRI_NETMASKED
) {
6136 if (inpmask
== NULL
)
6138 dmask
->i6
[0] = and->i6
[0];
6139 dmask
->i6
[1] = and->i6
[1];
6140 dmask
->i6
[2] = and->i6
[2];
6141 dmask
->i6
[3] = and->i6
[3];
6144 dst
->i6
[0] = src
->i6
[0] & and->i6
[0];
6145 dst
->i6
[1] = src
->i6
[1] & and->i6
[1];
6146 dst
->i6
[2] = src
->i6
[2] & and->i6
[2];
6147 dst
->i6
[3] = src
->i6
[3] & and->i6
[3];
6149 dst
->i6
[0] = src
->i6
[0];
6150 dst
->i6
[1] = src
->i6
[1];
6151 dst
->i6
[2] = src
->i6
[2];
6152 dst
->i6
[3] = src
->i6
[3];
6159 /* ------------------------------------------------------------------------ */
6160 /* Function: fr_matchtag */
6161 /* Returns: 0 == mismatch, 1 == match. */
6162 /* Parameters: tag1(I) - pointer to first tag to compare */
6163 /* tag2(I) - pointer to second tag to compare */
6165 /* Returns true (non-zero) or false(0) if the two tag structures can be */
6166 /* considered to be a match or not match, respectively. The tag is 16 */
6167 /* bytes long (16 characters) but that is overlayed with 4 32bit ints so */
6168 /* compare the ints instead, for speed. tag1 is the master of the */
6169 /* comparison. This function should only be called with both tag1 and tag2 */
6170 /* as non-NULL pointers. */
6171 /* ------------------------------------------------------------------------ */
6172 int fr_matchtag(tag1
, tag2
)
6173 ipftag_t
*tag1
, *tag2
;
6178 if ((tag1
->ipt_num
[0] == 0) && (tag2
->ipt_num
[0] == 0))
6181 if ((tag1
->ipt_num
[0] == tag2
->ipt_num
[0]) &&
6182 (tag1
->ipt_num
[1] == tag2
->ipt_num
[1]) &&
6183 (tag1
->ipt_num
[2] == tag2
->ipt_num
[2]) &&
6184 (tag1
->ipt_num
[3] == tag2
->ipt_num
[3]))
6190 /* ------------------------------------------------------------------------ */
6191 /* Function: fr_coalesce */
6192 /* Returns: 1 == success, -1 == failure, 0 == no change */
6193 /* Parameters: fin(I) - pointer to packet information */
6195 /* Attempt to get all of the packet data into a single, contiguous buffer. */
6196 /* If this call returns a failure then the buffers have also been freed. */
6197 /* ------------------------------------------------------------------------ */
6198 int fr_coalesce(fin
)
6201 ipf_stack_t
*ifs
= fin
->fin_ifs
;
6202 if ((fin
->fin_flx
& FI_COALESCE
) != 0)
6206 * If the mbuf pointers indicate that there is no mbuf to work with,
6207 * return but do not indicate success or failure.
6209 if (fin
->fin_m
== NULL
|| fin
->fin_mp
== NULL
)
6212 #if defined(_KERNEL)
6213 if (fr_pullup(fin
->fin_m
, fin
, fin
->fin_plen
) == NULL
) {
6214 IPF_BUMP(ifs
->ifs_fr_badcoalesces
[fin
->fin_out
]);
6216 FREE_MB_T(*fin
->fin_mp
);
6218 *fin
->fin_mp
= NULL
;
6223 fin
= fin
; /* LINT */
6230 * The following table lists all of the tunable variables that can be
6231 * accessed via SIOCIPFGET/SIOCIPFSET/SIOCIPFGETNEXT. The format of each row
6232 * in the table below is as follows:
6234 * pointer to value, name of value, minimum, maximum, size of the value's
6235 * container, value attribute flags
6237 * For convienience, IPFT_RDONLY means the value is read-only, IPFT_WRDISABLED
6238 * means the value can only be written to when IPFilter is loaded but disabled.
6239 * The obvious implication is if neither of these are set then the value can be
6240 * changed at any time without harm.
6242 ipftuneable_t lcl_ipf_tuneables
[] = {
6244 { { NULL
}, "fr_flags", 0, 0xffffffff,
6246 { { NULL
}, "fr_active", 0, 0,
6248 { { NULL
}, "fr_control_forwarding", 0, 1,
6250 { { NULL
}, "fr_update_ipid", 0, 1,
6252 { { NULL
}, "fr_chksrc", 0, 1,
6254 { { NULL
}, "fr_minttl", 0, 1,
6256 { { NULL
}, "fr_icmpminfragmtu", 0, 1,
6258 { { NULL
}, "fr_pass", 0, 0xffffffff,
6261 { { NULL
}, "ipf_loopback", 0, 1,
6262 0, IPFT_WRDISABLED
},
6265 { { NULL
}, "fr_tcpidletimeout", 1, 0x7fffffff,
6266 0, IPFT_WRDISABLED
},
6267 { { NULL
}, "fr_tcpclosewait", 1, 0x7fffffff,
6268 0, IPFT_WRDISABLED
},
6269 { { NULL
}, "fr_tcplastack", 1, 0x7fffffff,
6270 0, IPFT_WRDISABLED
},
6271 { { NULL
}, "fr_tcptimeout", 1, 0x7fffffff,
6272 0, IPFT_WRDISABLED
},
6273 { { NULL
}, "fr_tcpclosed", 1, 0x7fffffff,
6274 0, IPFT_WRDISABLED
},
6275 { { NULL
}, "fr_tcphalfclosed", 1, 0x7fffffff,
6276 0, IPFT_WRDISABLED
},
6277 { { NULL
}, "fr_udptimeout", 1, 0x7fffffff,
6278 0, IPFT_WRDISABLED
},
6279 { { NULL
}, "fr_udpacktimeout", 1, 0x7fffffff,
6280 0, IPFT_WRDISABLED
},
6281 { { NULL
}, "fr_icmptimeout", 1, 0x7fffffff,
6282 0, IPFT_WRDISABLED
},
6283 { { NULL
}, "fr_icmpacktimeout", 1, 0x7fffffff,
6284 0, IPFT_WRDISABLED
},
6285 { { NULL
}, "fr_iptimeout", 1, 0x7fffffff,
6286 0, IPFT_WRDISABLED
},
6287 { { NULL
}, "fr_statemax", 1, 0x7fffffff,
6289 { { NULL
}, "fr_statesize", 1, 0x7fffffff,
6290 0, IPFT_WRDISABLED
},
6291 { { NULL
}, "fr_state_lock", 0, 1,
6293 { { NULL
}, "fr_state_maxbucket", 1, 0x7fffffff,
6294 0, IPFT_WRDISABLED
},
6295 { { NULL
}, "fr_state_maxbucket_reset", 0, 1,
6296 0, IPFT_WRDISABLED
},
6297 { { NULL
}, "ipstate_logging", 0, 1,
6299 { { NULL
}, "state_flush_level_hi", 1, 100,
6301 { { NULL
}, "state_flush_level_lo", 1, 100,
6304 { { NULL
}, "fr_nat_lock", 0, 1,
6306 { { NULL
}, "ipf_nattable_sz", 1, 0x7fffffff,
6307 0, IPFT_WRDISABLED
},
6308 { { NULL
}, "ipf_nattable_max", 1, 0x7fffffff,
6310 { { NULL
}, "ipf_natrules_sz", 1, 0x7fffffff,
6311 0, IPFT_WRDISABLED
},
6312 { { NULL
}, "ipf_rdrrules_sz", 1, 0x7fffffff,
6313 0, IPFT_WRDISABLED
},
6314 { { NULL
}, "ipf_hostmap_sz", 1, 0x7fffffff,
6315 0, IPFT_WRDISABLED
},
6316 { { NULL
}, "fr_nat_maxbucket", 1, 0x7fffffff,
6317 0, IPFT_WRDISABLED
},
6318 { { NULL
}, "fr_nat_maxbucket_reset", 0, 1,
6319 0, IPFT_WRDISABLED
},
6320 { { NULL
}, "nat_logging", 0, 1,
6322 { { NULL
}, "fr_defnatage", 1, 0x7fffffff,
6323 0, IPFT_WRDISABLED
},
6324 { { NULL
}, "fr_defnatipage", 1, 0x7fffffff,
6325 0, IPFT_WRDISABLED
},
6326 { { NULL
}, "fr_defnaticmpage", 1, 0x7fffffff,
6327 0, IPFT_WRDISABLED
},
6328 { { NULL
}, "nat_flush_level_hi", 1, 100,
6330 { { NULL
}, "nat_flush_level_lo", 1, 100,
6333 { { NULL
}, "ipfr_size", 1, 0x7fffffff,
6334 0, IPFT_WRDISABLED
},
6335 { { NULL
}, "fr_ipfrttl", 1, 0x7fffffff,
6336 0, IPFT_WRDISABLED
},
6339 { { NULL
}, "ipl_suppress", 0, 1,
6341 { { NULL
}, "ipl_buffer_sz", 0, 0,
6343 { { NULL
}, "ipl_logmax", 0, 0x7fffffff,
6344 0, IPFT_WRDISABLED
},
6345 { { NULL
}, "ipl_logall", 0, 1,
6347 { { NULL
}, "ipl_logsize", 0, 0x80000,
6350 { { NULL
}, NULL
, 0, 0 }
6353 static ipftuneable_t
*
6354 tune_lookup(ipf_stack_t
*ifs
, char *name
)
6358 for (i
= 0; ifs
->ifs_ipf_tuneables
[i
].ipft_name
!= NULL
; i
++) {
6359 if (strcmp(ifs
->ifs_ipf_tuneables
[i
].ipft_name
, name
) == 0)
6360 return (&ifs
->ifs_ipf_tuneables
[i
]);
6366 extern dev_info_t
*ipf_dev_info
;
6367 extern int ipf_property_update
__P((dev_info_t
*, ipf_stack_t
*));
6370 /* -------------------------------------------------------------------- */
6371 /* Function: ipftuneable_setdefs() */
6373 /* Parameters: ifs - pointer to newly allocated IPF instance */
6374 /* assigned to IP instance */
6376 /* Function initializes IPF instance variables. Function is invoked */
6377 /* from ipftuneable_alloc(). ipftuneable_alloc() is called only one */
6378 /* time during IP instance lifetime - at the time of IP instance */
6379 /* creation. Anytime IP instance is being created new private IPF */
6380 /* instance is allocated and assigned to it. The moment of IP */
6381 /* instance creation is the right time to initialize those IPF */
6384 /* -------------------------------------------------------------------- */
6385 static void ipftuneable_setdefs(ipf_stack_t
*ifs
)
6387 ifs
->ifs_ipfr_size
= IPFT_SIZE
;
6388 ifs
->ifs_fr_ipfrttl
= 120; /* 60 seconds */
6390 /* it comes from fr_authinit() in IPF auth */
6391 ifs
->ifs_fr_authsize
= FR_NUMAUTH
;
6392 ifs
->ifs_fr_defaultauthage
= 600;
6394 /* it comes from fr_stateinit() in IPF state */
6395 ifs
->ifs_fr_tcpidletimeout
= IPF_TTLVAL(3600 * 24 * 5); /* five days */
6396 ifs
->ifs_fr_tcpclosewait
= IPF_TTLVAL(TCP_MSL
);
6397 ifs
->ifs_fr_tcplastack
= IPF_TTLVAL(TCP_MSL
);
6398 ifs
->ifs_fr_tcptimeout
= IPF_TTLVAL(TCP_MSL
);
6399 ifs
->ifs_fr_tcpclosed
= IPF_TTLVAL(60);
6400 ifs
->ifs_fr_tcphalfclosed
= IPF_TTLVAL(2 * 3600); /* 2 hours */
6401 ifs
->ifs_fr_udptimeout
= IPF_TTLVAL(120);
6402 ifs
->ifs_fr_udpacktimeout
= IPF_TTLVAL(12);
6403 ifs
->ifs_fr_icmptimeout
= IPF_TTLVAL(60);
6404 ifs
->ifs_fr_icmpacktimeout
= IPF_TTLVAL(6);
6405 ifs
->ifs_fr_iptimeout
= IPF_TTLVAL(60);
6406 ifs
->ifs_fr_statemax
= IPSTATE_MAX
;
6407 ifs
->ifs_fr_statesize
= IPSTATE_SIZE
;
6408 ifs
->ifs_fr_state_maxbucket_reset
= 1;
6409 ifs
->ifs_state_flush_level_hi
= ST_FLUSH_HI
;
6410 ifs
->ifs_state_flush_level_lo
= ST_FLUSH_LO
;
6412 /* it comes from fr_natinit() in ipnat */
6413 ifs
->ifs_ipf_nattable_sz
= NAT_TABLE_SZ
;
6414 ifs
->ifs_ipf_nattable_max
= NAT_TABLE_MAX
;
6415 ifs
->ifs_ipf_natrules_sz
= NAT_SIZE
;
6416 ifs
->ifs_ipf_rdrrules_sz
= RDR_SIZE
;
6417 ifs
->ifs_ipf_hostmap_sz
= HOSTMAP_SIZE
;
6418 ifs
->ifs_fr_nat_maxbucket_reset
= 1;
6419 ifs
->ifs_fr_defnatage
= DEF_NAT_AGE
;
6420 ifs
->ifs_fr_defnatipage
= 120; /* 60 seconds */
6421 ifs
->ifs_fr_defnaticmpage
= 6; /* 3 seconds */
6422 ifs
->ifs_nat_flush_level_hi
= NAT_FLUSH_HI
;
6423 ifs
->ifs_nat_flush_level_lo
= NAT_FLUSH_LO
;
6426 /* it comes from fr_loginit() in IPF log */
6427 ifs
->ifs_ipl_suppress
= 1;
6428 ifs
->ifs_ipl_logmax
= IPL_LOGMAX
;
6429 ifs
->ifs_ipl_logsize
= IPFILTER_LOGSIZE
;
6431 /* from fr_natinit() */
6432 ifs
->ifs_nat_logging
= 1;
6434 /* from fr_stateinit() */
6435 ifs
->ifs_ipstate_logging
= 1;
6437 /* from fr_natinit() */
6438 ifs
->ifs_nat_logging
= 0;
6440 /* from fr_stateinit() */
6441 ifs
->ifs_ipstate_logging
= 0;
6443 ifs
->ifs_ipf_loopback
= 0;
6447 * Allocate a per-stack tuneable and copy in the names. Then
6448 * set it to point to each of the per-stack tunables.
6451 ipftuneable_alloc(ipf_stack_t
*ifs
)
6453 ipftuneable_t
*item
;
6455 KMALLOCS(ifs
->ifs_ipf_tuneables
, ipftuneable_t
*,
6456 sizeof (lcl_ipf_tuneables
));
6457 bcopy(lcl_ipf_tuneables
, ifs
->ifs_ipf_tuneables
,
6458 sizeof (lcl_ipf_tuneables
));
6460 #define TUNE_SET(_ifs, _name, _field) \
6461 item = tune_lookup((_ifs), (_name)); \
6462 if (item != NULL) { \
6463 item->ipft_una.ipftp_int = (unsigned int *)&((_ifs)->_field); \
6464 item->ipft_sz = sizeof ((_ifs)->_field); \
6467 TUNE_SET(ifs
, "fr_flags", ifs_fr_flags
);
6468 TUNE_SET(ifs
, "fr_active", ifs_fr_active
);
6469 TUNE_SET(ifs
, "fr_control_forwarding", ifs_fr_control_forwarding
);
6470 TUNE_SET(ifs
, "fr_update_ipid", ifs_fr_update_ipid
);
6471 TUNE_SET(ifs
, "fr_chksrc", ifs_fr_chksrc
);
6472 TUNE_SET(ifs
, "fr_minttl", ifs_fr_minttl
);
6473 TUNE_SET(ifs
, "fr_icmpminfragmtu", ifs_fr_icmpminfragmtu
);
6474 TUNE_SET(ifs
, "fr_pass", ifs_fr_pass
);
6475 TUNE_SET(ifs
, "fr_tcpidletimeout", ifs_fr_tcpidletimeout
);
6476 TUNE_SET(ifs
, "fr_tcpclosewait", ifs_fr_tcpclosewait
);
6477 TUNE_SET(ifs
, "fr_tcplastack", ifs_fr_tcplastack
);
6478 TUNE_SET(ifs
, "fr_tcptimeout", ifs_fr_tcptimeout
);
6479 TUNE_SET(ifs
, "fr_tcpclosed", ifs_fr_tcpclosed
);
6480 TUNE_SET(ifs
, "fr_tcphalfclosed", ifs_fr_tcphalfclosed
);
6481 TUNE_SET(ifs
, "fr_udptimeout", ifs_fr_udptimeout
);
6482 TUNE_SET(ifs
, "fr_udpacktimeout", ifs_fr_udpacktimeout
);
6483 TUNE_SET(ifs
, "fr_icmptimeout", ifs_fr_icmptimeout
);
6484 TUNE_SET(ifs
, "fr_icmpacktimeout", ifs_fr_icmpacktimeout
);
6485 TUNE_SET(ifs
, "fr_iptimeout", ifs_fr_iptimeout
);
6486 TUNE_SET(ifs
, "fr_statemax", ifs_fr_statemax
);
6487 TUNE_SET(ifs
, "fr_statesize", ifs_fr_statesize
);
6488 TUNE_SET(ifs
, "fr_state_lock", ifs_fr_state_lock
);
6489 TUNE_SET(ifs
, "fr_state_maxbucket", ifs_fr_state_maxbucket
);
6490 TUNE_SET(ifs
, "fr_state_maxbucket_reset", ifs_fr_state_maxbucket_reset
);
6491 TUNE_SET(ifs
, "ipstate_logging", ifs_ipstate_logging
);
6492 TUNE_SET(ifs
, "fr_nat_lock", ifs_fr_nat_lock
);
6493 TUNE_SET(ifs
, "ipf_nattable_sz", ifs_ipf_nattable_sz
);
6494 TUNE_SET(ifs
, "ipf_nattable_max", ifs_ipf_nattable_max
);
6495 TUNE_SET(ifs
, "ipf_natrules_sz", ifs_ipf_natrules_sz
);
6496 TUNE_SET(ifs
, "ipf_rdrrules_sz", ifs_ipf_rdrrules_sz
);
6497 TUNE_SET(ifs
, "ipf_hostmap_sz", ifs_ipf_hostmap_sz
);
6498 TUNE_SET(ifs
, "fr_nat_maxbucket", ifs_fr_nat_maxbucket
);
6499 TUNE_SET(ifs
, "fr_nat_maxbucket_reset", ifs_fr_nat_maxbucket_reset
);
6500 TUNE_SET(ifs
, "nat_logging", ifs_nat_logging
);
6501 TUNE_SET(ifs
, "fr_defnatage", ifs_fr_defnatage
);
6502 TUNE_SET(ifs
, "fr_defnatipage", ifs_fr_defnatipage
);
6503 TUNE_SET(ifs
, "fr_defnaticmpage", ifs_fr_defnaticmpage
);
6504 TUNE_SET(ifs
, "nat_flush_level_hi", ifs_nat_flush_level_hi
);
6505 TUNE_SET(ifs
, "nat_flush_level_lo", ifs_nat_flush_level_lo
);
6506 TUNE_SET(ifs
, "state_flush_level_hi", ifs_state_flush_level_hi
);
6507 TUNE_SET(ifs
, "state_flush_level_lo", ifs_state_flush_level_lo
);
6508 TUNE_SET(ifs
, "ipfr_size", ifs_ipfr_size
);
6509 TUNE_SET(ifs
, "fr_ipfrttl", ifs_fr_ipfrttl
);
6510 TUNE_SET(ifs
, "ipf_loopback", ifs_ipf_loopback
);
6512 TUNE_SET(ifs
, "ipl_suppress", ifs_ipl_suppress
);
6513 TUNE_SET(ifs
, "ipl_buffer_sz", ifs_ipl_buffer_sz
);
6514 TUNE_SET(ifs
, "ipl_logmax", ifs_ipl_logmax
);
6515 TUNE_SET(ifs
, "ipl_logall", ifs_ipl_logall
);
6516 TUNE_SET(ifs
, "ipl_logsize", ifs_ipl_logsize
);
6520 ipftuneable_setdefs(ifs
);
6523 (void) ipf_property_update(ipf_dev_info
, ifs
);
6528 ipftuneable_free(ipf_stack_t
*ifs
)
6530 KFREES(ifs
->ifs_ipf_tuneables
, sizeof (lcl_ipf_tuneables
));
6531 ifs
->ifs_ipf_tuneables
= NULL
;
6534 /* ------------------------------------------------------------------------ */
6535 /* Function: fr_findtunebycookie */
6536 /* Returns: NULL = search failed, else pointer to tune struct */
6537 /* Parameters: cookie(I) - cookie value to search for amongst tuneables */
6538 /* next(O) - pointer to place to store the cookie for the */
6539 /* "next" tuneable, if it is desired. */
6541 /* This function is used to walk through all of the existing tunables with */
6542 /* successive calls. It searches the known tunables for the one which has */
6543 /* a matching value for "cookie" - ie its address. When returning a match, */
6544 /* the next one to be found may be returned inside next. */
6545 /* ------------------------------------------------------------------------ */
6546 static ipftuneable_t
*fr_findtunebycookie(cookie
, next
, ifs
)
6547 void *cookie
, **next
;
6550 ipftuneable_t
*ta
, **tap
;
6552 for (ta
= ifs
->ifs_ipf_tuneables
; ta
->ipft_name
!= NULL
; ta
++)
6556 * If the next entry in the array has a name
6557 * present, then return a pointer to it for
6558 * where to go next, else return a pointer to
6559 * the dynaminc list as a key to search there
6560 * next. This facilitates a weak linking of
6561 * the two "lists" together.
6563 if ((ta
+ 1)->ipft_name
!= NULL
)
6566 *next
= &ifs
->ifs_ipf_tunelist
;
6571 for (tap
= &ifs
->ifs_ipf_tunelist
; (ta
= *tap
) != NULL
; tap
= &ta
->ipft_next
)
6572 if (tap
== cookie
) {
6574 *next
= &ta
->ipft_next
;
6584 /* ------------------------------------------------------------------------ */
6585 /* Function: fr_findtunebyname */
6586 /* Returns: NULL = search failed, else pointer to tune struct */
6587 /* Parameters: name(I) - name of the tuneable entry to find. */
6589 /* Search the static array of tuneables and the list of dynamic tuneables */
6590 /* for an entry with a matching name. If we can find one, return a pointer */
6591 /* to the matching structure. */
6592 /* ------------------------------------------------------------------------ */
6593 static ipftuneable_t
*fr_findtunebyname(name
, ifs
)
6599 for (ta
= ifs
->ifs_ipf_tuneables
; ta
->ipft_name
!= NULL
; ta
++)
6600 if (!strcmp(ta
->ipft_name
, name
)) {
6604 for (ta
= ifs
->ifs_ipf_tunelist
; ta
!= NULL
; ta
= ta
->ipft_next
)
6605 if (!strcmp(ta
->ipft_name
, name
)) {
6613 /* ------------------------------------------------------------------------ */
6614 /* Function: fr_addipftune */
6615 /* Returns: int - 0 == success, else failure */
6616 /* Parameters: newtune - pointer to new tune struct to add to tuneables */
6618 /* Appends the tune structure pointer to by "newtune" to the end of the */
6619 /* current list of "dynamic" tuneable parameters. Once added, the owner */
6620 /* of the object is not expected to ever change "ipft_next". */
6621 /* ------------------------------------------------------------------------ */
6622 int fr_addipftune(newtune
, ifs
)
6623 ipftuneable_t
*newtune
;
6626 ipftuneable_t
*ta
, **tap
;
6628 ta
= fr_findtunebyname(newtune
->ipft_name
, ifs
);
6632 for (tap
= &ifs
->ifs_ipf_tunelist
; *tap
!= NULL
; tap
= &(*tap
)->ipft_next
)
6635 newtune
->ipft_next
= NULL
;
6641 /* ------------------------------------------------------------------------ */
6642 /* Function: fr_delipftune */
6643 /* Returns: int - 0 == success, else failure */
6644 /* Parameters: oldtune - pointer to tune struct to remove from the list of */
6645 /* current dynamic tuneables */
6647 /* Search for the tune structure, by pointer, in the list of those that are */
6648 /* dynamically added at run time. If found, adjust the list so that this */
6649 /* structure is no longer part of it. */
6650 /* ------------------------------------------------------------------------ */
6651 int fr_delipftune(oldtune
, ifs
)
6652 ipftuneable_t
*oldtune
;
6655 ipftuneable_t
*ta
, **tap
;
6657 for (tap
= &ifs
->ifs_ipf_tunelist
; (ta
= *tap
) != NULL
; tap
= &ta
->ipft_next
)
6658 if (ta
== oldtune
) {
6659 *tap
= oldtune
->ipft_next
;
6660 oldtune
->ipft_next
= NULL
;
6668 /* ------------------------------------------------------------------------ */
6669 /* Function: fr_ipftune */
6670 /* Returns: int - 0 == success, else failure */
6671 /* Parameters: cmd(I) - ioctl command number */
6672 /* data(I) - pointer to ioctl data structure */
6674 /* Implement handling of SIOCIPFGETNEXT, SIOCIPFGET and SIOCIPFSET. These */
6675 /* three ioctls provide the means to access and control global variables */
6676 /* within IPFilter, allowing (for example) timeouts and table sizes to be */
6677 /* changed without rebooting, reloading or recompiling. The initialisation */
6678 /* and 'destruction' routines of the various components of ipfilter are all */
6679 /* each responsible for handling their own values being too big. */
6680 /* ------------------------------------------------------------------------ */
6681 int fr_ipftune(cmd
, data
, ifs
)
6691 error
= fr_inobj(data
, &tu
, IPFOBJ_TUNEABLE
);
6695 tu
.ipft_name
[sizeof(tu
.ipft_name
) - 1] = '\0';
6696 cookie
= tu
.ipft_cookie
;
6701 case SIOCIPFGETNEXT
:
6703 * If cookie is non-NULL, assume it to be a pointer to the last
6704 * entry we looked at, so find it (if possible) and return a
6705 * pointer to the next one after it. The last entry in the
6706 * the table is a NULL entry, so when we get to it, set cookie
6707 * to NULL and return that, indicating end of list, erstwhile
6708 * if we come in with cookie set to NULL, we are starting anew
6709 * at the front of the list.
6711 if (cookie
!= NULL
) {
6712 ta
= fr_findtunebycookie(cookie
, &tu
.ipft_cookie
, ifs
);
6714 ta
= ifs
->ifs_ipf_tuneables
;
6715 tu
.ipft_cookie
= ta
+ 1;
6719 * Entry found, but does the data pointed to by that
6720 * row fit in what we can return?
6722 if (ta
->ipft_sz
> sizeof(tu
.ipft_un
))
6726 if (ta
->ipft_sz
== sizeof(u_long
))
6727 tu
.ipft_vlong
= *ta
->ipft_plong
;
6728 else if (ta
->ipft_sz
== sizeof(u_int
))
6729 tu
.ipft_vint
= *ta
->ipft_pint
;
6730 else if (ta
->ipft_sz
== sizeof(u_short
))
6731 tu
.ipft_vshort
= *ta
->ipft_pshort
;
6732 else if (ta
->ipft_sz
== sizeof(u_char
))
6733 tu
.ipft_vchar
= *ta
->ipft_pchar
;
6735 tu
.ipft_sz
= ta
->ipft_sz
;
6736 tu
.ipft_min
= ta
->ipft_min
;
6737 tu
.ipft_max
= ta
->ipft_max
;
6738 tu
.ipft_flags
= ta
->ipft_flags
;
6739 bcopy(ta
->ipft_name
, tu
.ipft_name
,
6740 MIN(sizeof(tu
.ipft_name
),
6741 strlen(ta
->ipft_name
) + 1));
6743 error
= fr_outobj(data
, &tu
, IPFOBJ_TUNEABLE
);
6749 * Search by name or by cookie value for a particular entry
6750 * in the tuning paramter table.
6753 if (cookie
!= NULL
) {
6754 ta
= fr_findtunebycookie(cookie
, NULL
, ifs
);
6757 } else if (tu
.ipft_name
[0] != '\0') {
6758 ta
= fr_findtunebyname(tu
.ipft_name
, ifs
);
6765 if (cmd
== (ioctlcmd_t
)SIOCIPFGET
) {
6767 * Fetch the tuning parameters for a particular value
6770 if (ta
->ipft_sz
== sizeof(u_long
))
6771 tu
.ipft_vlong
= *ta
->ipft_plong
;
6772 else if (ta
->ipft_sz
== sizeof(u_int
))
6773 tu
.ipft_vint
= *ta
->ipft_pint
;
6774 else if (ta
->ipft_sz
== sizeof(u_short
))
6775 tu
.ipft_vshort
= *ta
->ipft_pshort
;
6776 else if (ta
->ipft_sz
== sizeof(u_char
))
6777 tu
.ipft_vchar
= *ta
->ipft_pchar
;
6778 tu
.ipft_cookie
= ta
;
6779 tu
.ipft_sz
= ta
->ipft_sz
;
6780 tu
.ipft_min
= ta
->ipft_min
;
6781 tu
.ipft_max
= ta
->ipft_max
;
6782 tu
.ipft_flags
= ta
->ipft_flags
;
6783 error
= fr_outobj(data
, &tu
, IPFOBJ_TUNEABLE
);
6785 } else if (cmd
== (ioctlcmd_t
)SIOCIPFSET
) {
6787 * Set an internal parameter. The hard part here is
6788 * getting the new value safely and correctly out of
6789 * the kernel (given we only know its size, not type.)
6793 if (((ta
->ipft_flags
& IPFT_WRDISABLED
) != 0) &&
6794 (ifs
->ifs_fr_running
> 0)) {
6800 if (in
< ta
->ipft_min
|| in
> ta
->ipft_max
) {
6805 if (ta
->ipft_sz
== sizeof(u_long
)) {
6806 tu
.ipft_vlong
= *ta
->ipft_plong
;
6807 *ta
->ipft_plong
= in
;
6808 } else if (ta
->ipft_sz
== sizeof(u_int
)) {
6809 tu
.ipft_vint
= *ta
->ipft_pint
;
6810 *ta
->ipft_pint
= (u_int
)(in
& 0xffffffff);
6811 } else if (ta
->ipft_sz
== sizeof(u_short
)) {
6812 tu
.ipft_vshort
= *ta
->ipft_pshort
;
6813 *ta
->ipft_pshort
= (u_short
)(in
& 0xffff);
6814 } else if (ta
->ipft_sz
== sizeof(u_char
)) {
6815 tu
.ipft_vchar
= *ta
->ipft_pchar
;
6816 *ta
->ipft_pchar
= (u_char
)(in
& 0xff);
6818 error
= fr_outobj(data
, &tu
, IPFOBJ_TUNEABLE
);
6831 /* ------------------------------------------------------------------------ */
6832 /* Function: fr_initialise */
6833 /* Returns: int - 0 == success, < 0 == failure */
6834 /* Parameters: None. */
6836 /* Call of the initialise functions for all the various subsystems inside */
6837 /* of IPFilter. If any of them should fail, return immeadiately a failure */
6838 /* BUT do not try to recover from the error here. */
6839 /* ------------------------------------------------------------------------ */
6840 int fr_initialise(ifs
)
6846 i
= fr_loginit(ifs
);
6850 i
= fr_natinit(ifs
);
6854 i
= fr_stateinit(ifs
);
6858 i
= fr_authinit(ifs
);
6862 i
= fr_fraginit(ifs
);
6870 #ifdef IPFILTER_SYNC
6871 i
= ipfsync_init(ifs
);
6875 #ifdef IPFILTER_SCAN
6880 #ifdef IPFILTER_LOOKUP
6881 i
= ip_lookup_init(ifs
);
6885 #ifdef IPFILTER_COMPILED
6892 /* ------------------------------------------------------------------------ */
6893 /* Function: fr_deinitialise */
6894 /* Returns: None. */
6895 /* Parameters: None. */
6897 /* Call all the various subsystem cleanup routines to deallocate memory or */
6898 /* destroy locks or whatever they've done that they need to now undo. */
6899 /* The order here IS important as there are some cross references of */
6900 /* internal data structures. */
6901 /* ------------------------------------------------------------------------ */
6902 void fr_deinitialise(ifs
)
6908 fr_stateunload(ifs
);
6909 #ifdef IPFILTER_SCAN
6914 #ifdef IPFILTER_COMPILED
6915 ipfrule_remove(ifs
);
6918 (void) frflush(IPL_LOGIPF
, 0, FR_INQUE
|FR_OUTQUE
|FR_INACTIVE
, ifs
);
6919 (void) frflush(IPL_LOGIPF
, 0, FR_INQUE
|FR_OUTQUE
, ifs
);
6920 (void) frflush(IPL_LOGCOUNT
, 0, FR_INQUE
|FR_OUTQUE
|FR_INACTIVE
, ifs
);
6921 (void) frflush(IPL_LOGCOUNT
, 0, FR_INQUE
|FR_OUTQUE
, ifs
);
6923 #ifdef IPFILTER_LOOKUP
6924 ip_lookup_unload(ifs
);
6933 /* ------------------------------------------------------------------------ */
6934 /* Function: fr_zerostats */
6935 /* Returns: int - 0 = success, else failure */
6936 /* Parameters: data(O) - pointer to pointer for copying data back to */
6938 /* Copies the current statistics out to userspace and then zero's the */
6939 /* current ones in the kernel. The lock is only held across the bzero() as */
6940 /* the copyout may result in paging (ie network activity.) */
6941 /* ------------------------------------------------------------------------ */
6942 int fr_zerostats(data
, ifs
)
6949 fr_getstat(&fio
, ifs
);
6950 error
= copyoutptr(&fio
, data
, sizeof(fio
));
6954 WRITE_ENTER(&ifs
->ifs_ipf_mutex
);
6955 bzero((char *)ifs
->ifs_frstats
, sizeof(*ifs
->ifs_frstats
) * 2);
6956 RWLOCK_EXIT(&ifs
->ifs_ipf_mutex
);
6963 /* ------------------------------------------------------------------------ */
6964 /* Function: fr_resolvedest */
6966 /* Parameters: fdp(IO) - pointer to destination information to resolve */
6967 /* v(I) - IP protocol version to match */
6969 /* Looks up an interface name in the frdest structure pointed to by fdp and */
6970 /* if a matching name can be found for the particular IP protocol version */
6971 /* then store the interface pointer in the frdest struct. If no match is */
6972 /* found, then set the interface pointer to be -1 as NULL is considered to */
6973 /* indicate there is no information at all in the structure. */
6974 /* ------------------------------------------------------------------------ */
6975 void fr_resolvedest(fdp
, v
, ifs
)
6982 if (*fdp
->fd_ifname
!= '\0') {
6983 fdp
->fd_ifp
= GETIFP(fdp
->fd_ifname
, v
, ifs
);
6984 if (fdp
->fd_ifp
== NULL
)
6985 fdp
->fd_ifp
= (void *)-1;
6988 #endif /* _KERNEL */
6991 /* ------------------------------------------------------------------------ */
6992 /* Function: fr_resolvenic */
6993 /* Returns: void* - NULL = wildcard name, -1 = failed to find NIC, else */
6994 /* pointer to interface structure for NIC */
6995 /* Parameters: name(I) - complete interface name */
6996 /* v(I) - IP protocol version */
6998 /* Look for a network interface structure that firstly has a matching name */
6999 /* to that passed in and that is also being used for that IP protocol */
7000 /* version (necessary on some platforms where there are separate listings */
7001 /* for both IPv4 and IPv6 on the same physical NIC. */
7003 /* One might wonder why name gets terminated with a \0 byte in here. The */
7004 /* reason is an interface name could get into the kernel structures of ipf */
7005 /* in any number of ways and so long as they all use the same sized array */
7006 /* to put the name in, it makes sense to ensure it gets null terminated */
7007 /* before it is used for its intended purpose - finding its match in the */
7008 /* kernel's list of configured interfaces. */
7010 /* NOTE: This SHOULD ONLY be used with IPFilter structures that have an */
7011 /* array for the name that is LIFNAMSIZ bytes (at least) in length. */
7012 /* ------------------------------------------------------------------------ */
7013 void *fr_resolvenic(name
, v
, ifs
)
7020 if (name
[0] == '\0')
7023 if ((name
[1] == '\0') && ((name
[0] == '-') || (name
[0] == '*'))) {
7027 name
[LIFNAMSIZ
- 1] = '\0';
7029 nic
= GETIFP(name
, v
, ifs
);
7036 /* ------------------------------------------------------------------------ */
7037 /* Function: ipf_expiretokens */
7038 /* Returns: None. */
7039 /* Parameters: ifs - ipf stack instance */
7041 /* This function is run every ipf tick to see if there are any tokens that */
7042 /* have been held for too long and need to be freed up. */
7043 /* ------------------------------------------------------------------------ */
7044 void ipf_expiretokens(ifs
)
7049 WRITE_ENTER(&ifs
->ifs_ipf_tokens
);
7050 while ((it
= ifs
->ifs_ipftokenhead
) != NULL
) {
7051 if (it
->ipt_die
> ifs
->ifs_fr_ticks
)
7054 ipf_freetoken(it
, ifs
);
7056 RWLOCK_EXIT(&ifs
->ifs_ipf_tokens
);
7060 /* ------------------------------------------------------------------------ */
7061 /* Function: ipf_deltoken */
7062 /* Returns: int - 0 = success, else error */
7063 /* Parameters: type(I) - the token type to match */
7064 /* uid(I) - uid owning the token */
7065 /* ptr(I) - context pointer for the token */
7066 /* ifs - ipf stack instance */
7068 /* This function looks for a a token in the current list that matches up */
7069 /* the fields (type, uid, ptr). If none is found, ESRCH is returned, else */
7070 /* call ipf_freetoken() to remove it from the list. */
7071 /* ------------------------------------------------------------------------ */
7072 int ipf_deltoken(type
, uid
, ptr
, ifs
)
7080 WRITE_ENTER(&ifs
->ifs_ipf_tokens
);
7081 for (it
= ifs
->ifs_ipftokenhead
; it
!= NULL
; it
= it
->ipt_next
)
7082 if (ptr
== it
->ipt_ctx
&& type
== it
->ipt_type
&&
7083 uid
== it
->ipt_uid
) {
7084 ipf_freetoken(it
, ifs
);
7088 RWLOCK_EXIT(&ifs
->ifs_ipf_tokens
);
7094 /* ------------------------------------------------------------------------ */
7095 /* Function: ipf_unlinktoken */
7096 /* Returns: None. */
7097 /* Parameters: token(I) - pointer to token structure */
7098 /* ifs - ipf stack instance */
7100 /* This function unlinks a token structure from the linked list of tokens */
7101 /* that it belongs to. The head pointer never needs to be explicitly */
7102 /* adjusted, but the tail does due to the linked list implementation. */
7103 /* ------------------------------------------------------------------------ */
7104 static void ipf_unlinktoken(token
, ifs
)
7109 if (ifs
->ifs_ipftokentail
== &token
->ipt_next
)
7110 ifs
->ifs_ipftokentail
= token
->ipt_pnext
;
7112 *token
->ipt_pnext
= token
->ipt_next
;
7113 if (token
->ipt_next
!= NULL
)
7114 token
->ipt_next
->ipt_pnext
= token
->ipt_pnext
;
7118 /* ------------------------------------------------------------------------ */
7119 /* Function: ipf_findtoken */
7120 /* Returns: ipftoken_t * - NULL if no memory, else pointer to token */
7121 /* Parameters: type(I) - the token type to match */
7122 /* uid(I) - uid owning the token */
7123 /* ptr(I) - context pointer for the token */
7124 /* ifs - ipf stack instance */
7126 /* This function looks for a live token in the list of current tokens that */
7127 /* matches the tuple (type, uid, ptr). If one cannot be found then one is */
7128 /* allocated. If one is found then it is moved to the top of the list of */
7129 /* currently active tokens. */
7131 /* NOTE: It is by design that this function returns holding a read lock on */
7132 /* ipf_tokens. Callers must make sure they release it! */
7133 /* ------------------------------------------------------------------------ */
7134 ipftoken_t
*ipf_findtoken(type
, uid
, ptr
, ifs
)
7139 ipftoken_t
*it
, *new;
7141 KMALLOC(new, ipftoken_t
*);
7143 WRITE_ENTER(&ifs
->ifs_ipf_tokens
);
7144 for (it
= ifs
->ifs_ipftokenhead
; it
!= NULL
; it
= it
->ipt_next
) {
7145 if (it
->ipt_alive
== 0)
7147 if (ptr
== it
->ipt_ctx
&& type
== it
->ipt_type
&&
7157 it
->ipt_data
= NULL
;
7160 it
->ipt_type
= type
;
7161 it
->ipt_next
= NULL
;
7169 ipf_unlinktoken(it
, ifs
);
7171 it
->ipt_pnext
= ifs
->ifs_ipftokentail
;
7172 *ifs
->ifs_ipftokentail
= it
;
7173 ifs
->ifs_ipftokentail
= &it
->ipt_next
;
7174 it
->ipt_next
= NULL
;
7176 it
->ipt_die
= ifs
->ifs_fr_ticks
+ 2;
7178 MUTEX_DOWNGRADE(&ifs
->ifs_ipf_tokens
);
7184 /* ------------------------------------------------------------------------ */
7185 /* Function: ipf_freetoken */
7186 /* Returns: None. */
7187 /* Parameters: token(I) - pointer to token structure */
7188 /* ifs - ipf stack instance */
7190 /* This function unlinks a token from the linked list and on the path to */
7191 /* free'ing the data, it calls the dereference function that is associated */
7192 /* with the type of data pointed to by the token as it is considered to */
7193 /* hold a reference to it. */
7194 /* ------------------------------------------------------------------------ */
7195 void ipf_freetoken(token
, ifs
)
7199 void *data
, **datap
;
7201 ipf_unlinktoken(token
, ifs
);
7203 data
= token
->ipt_data
;
7206 if ((data
!= NULL
) && (data
!= (void *)-1)) {
7207 switch (token
->ipt_type
)
7209 case IPFGENITER_IPF
:
7210 (void)fr_derefrule((frentry_t
**)datap
, ifs
);
7212 case IPFGENITER_IPNAT
:
7213 WRITE_ENTER(&ifs
->ifs_ipf_nat
);
7214 fr_ipnatderef((ipnat_t
**)datap
, ifs
);
7215 RWLOCK_EXIT(&ifs
->ifs_ipf_nat
);
7217 case IPFGENITER_NAT
:
7218 fr_natderef((nat_t
**)datap
, ifs
);
7220 case IPFGENITER_STATE
:
7221 fr_statederef((ipstate_t
**)datap
, ifs
);
7223 case IPFGENITER_FRAG
:
7224 fr_fragderef((ipfr_t
**)datap
, &ifs
->ifs_ipf_frag
, ifs
);
7226 case IPFGENITER_NATFRAG
:
7227 fr_fragderef((ipfr_t
**)datap
,
7228 &ifs
->ifs_ipf_natfrag
, ifs
);
7230 case IPFGENITER_HOSTMAP
:
7231 WRITE_ENTER(&ifs
->ifs_ipf_nat
);
7232 fr_hostmapdel((hostmap_t
**)datap
);
7233 RWLOCK_EXIT(&ifs
->ifs_ipf_nat
);
7236 (void) ip_lookup_iterderef(token
->ipt_type
, data
, ifs
);
7245 /* ------------------------------------------------------------------------ */
7246 /* Function: ipf_getnextrule */
7247 /* Returns: int - 0 = success, else error */
7248 /* Parameters: t(I) - pointer to destination information to resolve */
7249 /* ptr(I) - pointer to ipfobj_t to copyin from user space */
7250 /* ifs - ipf stack instance */
7252 /* This function's first job is to bring in the ipfruleiter_t structure via */
7253 /* the ipfobj_t structure to determine what should be the next rule to */
7254 /* return. Once the ipfruleiter_t has been brought in, it then tries to */
7255 /* find the 'next rule'. This may include searching rule group lists or */
7256 /* just be as simple as looking at the 'next' field in the rule structure. */
7257 /* When we have found the rule to return, increase its reference count and */
7258 /* if we used an existing rule to get here, decrease its reference count. */
7259 /* ------------------------------------------------------------------------ */
7260 int ipf_getnextrule(t
, ptr
, ifs
)
7265 frentry_t
*fr
, *next
, zero
;
7266 int error
, out
, count
;
7271 if (t
== NULL
|| ptr
== NULL
)
7273 error
= fr_inobj(ptr
, &it
, IPFOBJ_IPFITER
);
7276 if ((it
.iri_ver
!= AF_INET
) && (it
.iri_ver
!= AF_INET6
))
7278 if ((it
.iri_inout
< 0) || (it
.iri_inout
> 3))
7280 if (it
.iri_nrules
== 0)
7282 if ((it
.iri_active
!= 0) && (it
.iri_active
!= 1))
7284 if (it
.iri_rule
== NULL
)
7288 * Use bitmask on it.iri_inout to determine direction.
7289 * F_OUT (1) and F_ACOUT (3) mask to out = 1, while
7290 * F_IN (0) and F_ACIN (2) mask to out = 0.
7292 out
= it
.iri_inout
& F_OUT
;
7293 READ_ENTER(&ifs
->ifs_ipf_mutex
);
7296 * Retrieve "previous" entry from token and find the next entry.
7300 if (*it
.iri_group
== '\0') {
7302 * Use bitmask again to determine accounting or not.
7303 * F_ACIN will mask to accounting cases F_ACIN (2)
7304 * or F_ACOUT (3), but not F_IN or F_OUT.
7306 if ((it
.iri_inout
& F_ACIN
) != 0) {
7307 if (it
.iri_ver
== AF_INET
)
7308 next
= ifs
->ifs_ipacct
7309 [out
][it
.iri_active
];
7311 next
= ifs
->ifs_ipacct6
7312 [out
][it
.iri_active
];
7314 if (it
.iri_ver
== AF_INET
)
7315 next
= ifs
->ifs_ipfilter
7316 [out
][it
.iri_active
];
7318 next
= ifs
->ifs_ipfilter6
7319 [out
][it
.iri_active
];
7322 fg
= fr_findgroup(it
.iri_group
, IPL_LOGIPF
,
7323 it
.iri_active
, NULL
, ifs
);
7325 next
= fg
->fg_start
;
7333 dst
= (char *)it
.iri_rule
;
7335 * The ipfruleiter may ask for more than 1 rule at a time to be
7336 * copied out, so long as that many exist in the list to start with!
7338 for (count
= it
.iri_nrules
; count
> 0; count
--) {
7340 * If we found an entry, add reference to it and update token.
7341 * Otherwise, zero out data to be returned and NULL out token.
7344 MUTEX_ENTER(&next
->fr_lock
);
7346 MUTEX_EXIT(&next
->fr_lock
);
7349 bzero(&zero
, sizeof(zero
));
7355 * Now that we have ref, it's save to give up lock.
7357 RWLOCK_EXIT(&ifs
->ifs_ipf_mutex
);
7360 * Copy out data and clean up references and token as needed.
7362 error
= COPYOUT(next
, dst
, sizeof(*next
));
7365 if (t
->ipt_data
== NULL
) {
7366 ipf_freetoken(t
, ifs
);
7370 (void) fr_derefrule(&fr
, ifs
);
7371 if (next
->fr_data
!= NULL
) {
7372 dst
+= sizeof(*next
);
7373 error
= COPYOUT(next
->fr_data
, dst
,
7378 dst
+= next
->fr_dsize
;
7380 if (next
->fr_next
== NULL
) {
7381 ipf_freetoken(t
, ifs
);
7386 if ((count
== 1) || (error
!= 0))
7389 READ_ENTER(&ifs
->ifs_ipf_mutex
);
7398 /* ------------------------------------------------------------------------ */
7399 /* Function: fr_frruleiter */
7400 /* Returns: int - 0 = success, else error */
7401 /* Parameters: data(I) - the token type to match */
7402 /* uid(I) - uid owning the token */
7403 /* ptr(I) - context pointer for the token */
7404 /* ifs - ipf stack instance */
7406 /* This function serves as a stepping stone between fr_ipf_ioctl and */
7407 /* ipf_getnextrule. It's role is to find the right token in the kernel for */
7408 /* the process doing the ioctl and use that to ask for the next rule. */
7409 /* ------------------------------------------------------------------------ */
7410 int ipf_frruleiter(data
, uid
, ctx
, ifs
)
7418 token
= ipf_findtoken(IPFGENITER_IPF
, uid
, ctx
, ifs
);
7420 error
= ipf_getnextrule(token
, data
, ifs
);
7423 RWLOCK_EXIT(&ifs
->ifs_ipf_tokens
);
7429 /* ------------------------------------------------------------------------ */
7430 /* Function: ipf_geniter */
7431 /* Returns: int - 0 = success, else error */
7432 /* Parameters: token(I) - pointer to ipftoken structure */
7433 /* itp(I) - pointer to ipfgeniter structure */
7434 /* ifs - ipf stack instance */
7436 /* Generic iterator called from ipf_genericiter. Currently only used for */
7437 /* walking through list of fragments. */
7438 /* ------------------------------------------------------------------------ */
7439 int ipf_geniter(token
, itp
, ifs
)
7446 switch (itp
->igi_type
)
7448 case IPFGENITER_FRAG
:
7449 error
= fr_nextfrag(token
, itp
, &ifs
->ifs_ipfr_list
,
7450 &ifs
->ifs_ipfr_tail
, &ifs
->ifs_ipf_frag
,
7462 /* ------------------------------------------------------------------------ */
7463 /* Function: ipf_genericiter */
7464 /* Returns: int - 0 = success, else error */
7465 /* Parameters: data(I) - the token type to match */
7466 /* uid(I) - uid owning the token */
7467 /* ptr(I) - context pointer for the token */
7468 /* ifs - ipf stack instance */
7470 /* This function serves as a stepping stone between fr_ipf_ioctl and */
7471 /* ipf_geniter when handling SIOCGENITER. It's role is to find the right */
7472 /* token in the kernel for the process using the ioctl, and to use that */
7473 /* token when calling ipf_geniter. */
7474 /* ------------------------------------------------------------------------ */
7475 int ipf_genericiter(data
, uid
, ctx
, ifs
)
7484 error
= fr_inobj(data
, &iter
, IPFOBJ_GENITER
);
7488 token
= ipf_findtoken(iter
.igi_type
, uid
, ctx
, ifs
);
7489 if (token
!= NULL
) {
7490 token
->ipt_subtype
= iter
.igi_type
;
7491 error
= ipf_geniter(token
, &iter
, ifs
);
7494 RWLOCK_EXIT(&ifs
->ifs_ipf_tokens
);
7500 /* --------------------------------------------------------------------- */
7501 /* Function: ipf_earlydrop */
7502 /* Returns: number of dropped/removed entries from the queue */
7503 /* Parameters: flushtype - which table we're cleaning (NAT or State) */
7504 /* ifq - pointer to queue with entries to be deleted */
7505 /* idletime - entry must be idle this long to be deleted */
7506 /* ifs - ipf stack instance */
7508 /* Function is invoked from state/NAT flush routines to remove entries */
7509 /* from specified timeout queue, based on how long they've sat idle, */
7510 /* without waiting for it to happen on its own. */
7511 /* --------------------------------------------------------------------- */
7512 int ipf_earlydrop(flushtype
, ifq
, idletime
, ifs
)
7518 ipftqent_t
*tqe
, *tqn
;
7519 unsigned int dropped
;
7529 * Determine the tick representing the idle time we're interested
7530 * in. If an entry exists in the queue, and it was touched before
7531 * that tick, then it's been idle longer than idletime, so it should
7534 droptick
= ifs
->ifs_fr_ticks
- idletime
;
7535 tqn
= ifq
->ifq_head
;
7536 while ((tqe
= tqn
) != NULL
&& tqe
->tqe_touched
< droptick
) {
7537 tqn
= tqe
->tqe_next
;
7538 ent
= tqe
->tqe_parent
;
7542 if (nat_delete((nat_t
*)ent
, NL_FLUSH
, ifs
) == 0)
7546 if (fr_delstate((ipstate_t
*)ent
, ISL_FLUSH
, ifs
) == 0)
7557 /* --------------------------------------------------------------------- */
7558 /* Function: ipf_flushclosing */
7559 /* Returns: int - number of entries deleted */
7560 /* Parameters: flushtype - which table we're cleaning (NAT or State) */
7561 /* stateval - TCP state at which to start removing entries */
7562 /* ipfqs - pointer to timeout queues */
7563 /* userqs - pointer to user defined queues */
7564 /* ifs - ipf stack instance */
7566 /* Remove state/NAT table entries for TCP connections which are in the */
7567 /* process of closing, and have at least reached the state specified by */
7568 /* the 'stateval' parameter. */
7569 /* --------------------------------------------------------------------- */
7570 int ipf_flushclosing(flushtype
, stateval
, ipfqs
, userqs
, ifs
)
7571 int flushtype
, stateval
;
7572 ipftq_t
*ipfqs
, *userqs
;
7575 ipftq_t
*ifq
, *ifqn
;
7576 ipftqent_t
*tqe
, *tqn
;
7585 * Start by deleting any entries in specific timeout queues.
7587 ifqn
= &ipfqs
[stateval
];
7588 while ((ifq
= ifqn
) != NULL
) {
7589 ifqn
= ifq
->ifq_next
;
7590 dropped
+= ipf_earlydrop(flushtype
, ifq
, (int)0, ifs
);
7594 * Next, look through user defined queues for closing entries.
7597 while ((ifq
= ifqn
) != NULL
) {
7598 ifqn
= ifq
->ifq_next
;
7599 tqn
= ifq
->ifq_head
;
7600 while ((tqe
= tqn
) != NULL
) {
7601 tqn
= tqe
->tqe_next
;
7602 ent
= tqe
->tqe_parent
;
7607 if ((nat
->nat_p
== IPPROTO_TCP
) &&
7608 (nat
->nat_tcpstate
[0] >= stateval
) &&
7609 (nat
->nat_tcpstate
[1] >= stateval
) &&
7610 (nat_delete(nat
, NL_EXPIRE
, ifs
) == 0))
7614 is
= (ipstate_t
*)ent
;
7615 if ((is
->is_p
== IPPROTO_TCP
) &&
7616 (is
->is_state
[0] >= stateval
) &&
7617 (is
->is_state
[1] >= stateval
) &&
7618 (fr_delstate(is
, ISL_EXPIRE
, ifs
) == 0))
7630 /* --------------------------------------------------------------------- */
7631 /* Function: ipf_extraflush */
7632 /* Returns: int - number of entries flushed (0 = none) */
7633 /* Parameters: flushtype - which table we're cleaning (NAT or State) */
7634 /* ipfqs - pointer to 'established' timeout queue */
7635 /* userqs - pointer to user defined queues */
7636 /* ifs - ipf stack instance */
7638 /* This function gets called when either NAT or state tables fill up. */
7639 /* We need to try a bit harder to free up some space. The function will */
7640 /* flush entries for TCP connections which have been idle a long time. */
7642 /* Currently, the idle time is checked using values from ideltime_tab[] */
7643 /* --------------------------------------------------------------------- */
7644 int ipf_extraflush(flushtype
, ipfqs
, userqs
, ifs
)
7646 ipftq_t
*ipfqs
, *userqs
;
7649 ipftq_t
*ifq
, *ifqn
;
7650 int idletime
, removed
, idle_idx
;
7655 * Determine initial threshold for minimum idle time based on
7656 * how long ipfilter has been running. Ipfilter needs to have
7657 * been up as long as the smallest interval to continue on.
7659 * Minimum idle times stored in idletime_tab and indexed by
7660 * idle_idx. Start at upper end of array and work backwards.
7662 * Once the index is found, set the initial idle time to the
7663 * first interval before the current ipfilter run time.
7665 if (ifs
->ifs_fr_ticks
< idletime_tab
[0])
7667 idle_idx
= (sizeof (idletime_tab
) / sizeof (int)) - 1;
7668 if (ifs
->ifs_fr_ticks
> idletime_tab
[idle_idx
]) {
7669 idletime
= idletime_tab
[idle_idx
];
7671 while ((idle_idx
> 0) &&
7672 (ifs
->ifs_fr_ticks
< idletime_tab
[idle_idx
]))
7675 idletime
= (ifs
->ifs_fr_ticks
/
7676 idletime_tab
[idle_idx
]) *
7677 idletime_tab
[idle_idx
];
7680 while (idle_idx
>= 0) {
7682 * Check to see if we need to delete more entries.
7683 * If we do, start with appropriate timeout queue.
7685 if (flushtype
== NAT_FLUSH
) {
7686 if (NAT_TAB_WATER_LEVEL(ifs
) <=
7687 ifs
->ifs_nat_flush_level_lo
)
7689 } else if (flushtype
== STATE_FLUSH
) {
7690 if (ST_TAB_WATER_LEVEL(ifs
) <=
7691 ifs
->ifs_state_flush_level_lo
)
7697 removed
+= ipf_earlydrop(flushtype
, ipfqs
, idletime
, ifs
);
7700 * Next, check the user defined queues. But first, make
7701 * certain that timeout queue deletions didn't do enough.
7703 if (flushtype
== NAT_FLUSH
) {
7704 if (NAT_TAB_WATER_LEVEL(ifs
) <=
7705 ifs
->ifs_nat_flush_level_lo
)
7708 if (ST_TAB_WATER_LEVEL(ifs
) <=
7709 ifs
->ifs_state_flush_level_lo
)
7713 while ((ifq
= ifqn
) != NULL
) {
7714 ifqn
= ifq
->ifq_next
;
7715 removed
+= ipf_earlydrop(flushtype
, ifq
, idletime
, ifs
);
7719 * Adjust the granularity of idle time.
7721 * If we reach an interval boundary, we need to
7722 * either adjust the idle time accordingly or exit
7723 * the loop altogether (if this is very last check).
7725 idletime
-= idletime_tab
[idle_idx
];
7726 if (idletime
< idletime_tab
[idle_idx
]) {
7727 if (idle_idx
!= 0) {
7728 idletime
= idletime_tab
[idle_idx
] -
7729 idletime_tab
[idle_idx
- 1];