]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - sys/contrib/ipfilter/netinet/fil.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / sys / contrib / ipfilter / netinet / fil.c
1 /*      $FreeBSD$       */
2
3 /*
4  * Copyright (C) 1993-2003 by Darren Reed.
5  *
6  * See the IPFILTER.LICENCE file for details on licencing.
7  */
8 #if defined(KERNEL) || defined(_KERNEL)
9 # undef KERNEL
10 # undef _KERNEL
11 # define        KERNEL  1
12 # define        _KERNEL 1
13 #endif
14 #include <sys/errno.h>
15 #include <sys/types.h>
16 #include <sys/param.h>
17 #include <sys/time.h>
18 #if defined(__NetBSD__)
19 # if (NetBSD >= 199905) && !defined(IPFILTER_LKM) && defined(_KERNEL)
20 #  if (__NetBSD_Version__ < 301000000)
21 #   include "opt_ipfilter_log.h"
22 #  else
23 #   include "opt_ipfilter.h"
24 #  endif
25 # endif
26 #endif
27 #if defined(_KERNEL) && defined(__FreeBSD_version) && \
28     (__FreeBSD_version >= 220000)
29 # if (__FreeBSD_version >= 400000)
30 #  if !defined(IPFILTER_LKM)
31 #   include "opt_inet6.h"
32 #  endif
33 #  if (__FreeBSD_version == 400019)
34 #   define CSUM_DELAY_DATA
35 #  endif
36 # endif
37 # include <sys/filio.h>
38 #else
39 # include <sys/ioctl.h>
40 #endif
41 #if (defined(__SVR4) || defined(__svr4__)) && defined(sun)
42 # include <sys/filio.h>
43 #endif
44 #if !defined(_AIX51)
45 # include <sys/fcntl.h>
46 #endif
47 #if defined(_KERNEL)
48 # include <sys/systm.h>
49 # include <sys/file.h>
50 #else
51 # include <stdio.h>
52 # include <string.h>
53 # include <stdlib.h>
54 # include <stddef.h>
55 # include <sys/file.h>
56 # define _KERNEL
57 # ifdef __OpenBSD__
58 struct file;
59 # endif
60 # include <sys/uio.h>
61 # undef _KERNEL
62 #endif
63 #if !defined(__SVR4) && !defined(__svr4__) && !defined(__hpux) && \
64     !defined(linux)
65 # include <sys/mbuf.h>
66 #else
67 # if !defined(linux)
68 #  include <sys/byteorder.h>
69 # endif
70 # if (SOLARIS2 < 5) && defined(sun)
71 #  include <sys/dditypes.h>
72 # endif
73 #endif
74 #ifdef __hpux
75 # define _NET_ROUTE_INCLUDED
76 #endif
77 #if !defined(linux)
78 # include <sys/protosw.h>
79 #endif
80 #include <sys/socket.h>
81 #include <net/if.h>
82 #ifdef sun
83 # include <net/af.h>
84 #endif
85 #if !defined(_KERNEL) && (defined(__FreeBSD__) || defined(SOLARIS2))
86 # if (__FreeBSD_version >= 504000)
87 #  undef _RADIX_H_
88 # endif
89 # include "radix_ipf.h"
90 #endif
91 #ifdef __osf__
92 # include "radix_ipf.h"
93 #else
94 # include <net/route.h>
95 #endif
96 #include <netinet/in.h>
97 #include <netinet/in_systm.h>
98 #include <netinet/ip.h>
99 #if !defined(linux)
100 # include <netinet/ip_var.h>
101 #endif
102 #if defined(__sgi) && defined(IFF_DRVRLOCK) /* IRIX 6 */
103 # include <sys/hashing.h>
104 # include <netinet/in_var.h>
105 #endif
106 #include <netinet/tcp.h>
107 #if (!defined(__sgi) && !defined(AIX)) || defined(_KERNEL)
108 # include <netinet/udp.h>
109 # include <netinet/ip_icmp.h>
110 #endif
111 #ifdef __hpux
112 # undef _NET_ROUTE_INCLUDED
113 #endif
114 #ifdef __osf__
115 # undef _RADIX_H_
116 #endif
117 #include "netinet/ip_compat.h"
118 #ifdef  USE_INET6
119 # include <netinet/icmp6.h>
120 # if !SOLARIS && defined(_KERNEL) && !defined(__osf__) && !defined(__hpux)
121 #  include <netinet6/in6_var.h>
122 # endif
123 #endif
124 #include <netinet/tcpip.h>
125 #include "netinet/ip_fil.h"
126 #include "netinet/ip_nat.h"
127 #include "netinet/ip_frag.h"
128 #include "netinet/ip_state.h"
129 #include "netinet/ip_proxy.h"
130 #include "netinet/ip_auth.h"
131 #ifdef IPFILTER_SCAN
132 # include "netinet/ip_scan.h"
133 #endif
134 #ifdef IPFILTER_SYNC
135 # include "netinet/ip_sync.h"
136 #endif
137 #include "netinet/ip_pool.h"
138 #include "netinet/ip_htable.h"
139 #ifdef IPFILTER_COMPILED
140 # include "netinet/ip_rules.h"
141 #endif
142 #if defined(IPFILTER_BPF) && defined(_KERNEL)
143 # include <net/bpf.h>
144 #endif
145 #if defined(__FreeBSD_version) && (__FreeBSD_version >= 300000)
146 # include <sys/malloc.h>
147 # if defined(_KERNEL) && !defined(IPFILTER_LKM)
148 #  include "opt_ipfilter.h"
149 # endif
150 #endif
151 #include "netinet/ipl.h"
152 /* END OF INCLUDES */
153
154 #include <machine/in_cksum.h>
155
156 #if !defined(lint)
157 static const char sccsid[] = "@(#)fil.c 1.36 6/5/96 (C) 1993-2000 Darren Reed";
158 static const char rcsid[] = "@(#)$FreeBSD$";
159 /* static const char rcsid[] = "@(#)$Id: fil.c,v 2.243.2.125 2007/10/10 09:27:20 darrenr Exp $"; */
160 #endif
161
162 #ifndef _KERNEL
163 # include "ipf.h"
164 # include "ipt.h"
165 # include "bpf-ipf.h"
166 extern  int     opts;
167 #endif /* _KERNEL */
168
169
170 fr_info_t       frcache[2][8];
171 struct  filterstats frstats[2];
172 struct  frentry *ipfilter[2][2] = { { NULL, NULL }, { NULL, NULL } },
173                 *ipfilter6[2][2] = { { NULL, NULL }, { NULL, NULL } },
174                 *ipacct6[2][2] = { { NULL, NULL }, { NULL, NULL } },
175                 *ipacct[2][2] = { { NULL, NULL }, { NULL, NULL } },
176                 *ipnatrules[2][2] = { { NULL, NULL }, { NULL, NULL } };
177 struct  frgroup *ipfgroups[IPL_LOGSIZE][2];
178 char    ipfilter_version[] = IPL_VERSION;
179 int     fr_refcnt = 0;
180 /*
181  * For fr_running:
182  * 0 == loading, 1 = running, -1 = disabled, -2 = unloading
183  */
184 int     fr_running = 0;
185 int     fr_flags = IPF_LOGGING;
186 int     fr_active = 0;
187 int     fr_control_forwarding = 0;
188 int     fr_update_ipid = 0;
189 u_short fr_ip_id = 0;
190 int     fr_chksrc = 0;  /* causes a system crash if enabled */
191 int     fr_minttl = 4;
192 int     fr_icmpminfragmtu = 68;
193 u_long  fr_frouteok[2] = {0, 0};
194 u_long  fr_userifqs = 0;
195 u_long  fr_badcoalesces[2] = {0, 0};
196 u_char  ipf_iss_secret[32];
197 #if defined(IPFILTER_DEFAULT_BLOCK)
198 int     fr_pass = FR_BLOCK|FR_NOMATCH;
199 #else
200 int     fr_pass = (IPF_DEFAULT_PASS)|FR_NOMATCH;
201 #endif
202 int     fr_features = 0
203 #ifdef  IPFILTER_LKM
204                 | IPF_FEAT_LKM
205 #endif
206 #ifdef  IPFILTER_LOG
207                 | IPF_FEAT_LOG
208 #endif
209 #ifdef  IPFILTER_LOOKUP
210                 | IPF_FEAT_LOOKUP
211 #endif
212 #ifdef  IPFILTER_BPF
213                 | IPF_FEAT_BPF
214 #endif
215 #ifdef  IPFILTER_COMPILED
216                 | IPF_FEAT_COMPILED
217 #endif
218 #ifdef  IPFILTER_CKSUM
219                 | IPF_FEAT_CKSUM
220 #endif
221 #ifdef  IPFILTER_SYNC
222                 | IPF_FEAT_SYNC
223 #endif
224 #ifdef  IPFILTER_SCAN
225                 | IPF_FEAT_SCAN
226 #endif
227 #ifdef  USE_INET6
228                 | IPF_FEAT_IPV6
229 #endif
230         ;
231
232 static  INLINE int      fr_ipfcheck __P((fr_info_t *, frentry_t *, int));
233 static  int             fr_portcheck __P((frpcmp_t *, u_short *));
234 static  int             frflushlist __P((int, minor_t, int *, frentry_t **));
235 static  ipfunc_t        fr_findfunc __P((ipfunc_t));
236 static  frentry_t       *fr_firewall __P((fr_info_t *, u_32_t *));
237 static  int             fr_funcinit __P((frentry_t *fr));
238 static  INLINE void     frpr_ah __P((fr_info_t *));
239 static  INLINE void     frpr_esp __P((fr_info_t *));
240 static  INLINE void     frpr_gre __P((fr_info_t *));
241 static  INLINE void     frpr_udp __P((fr_info_t *));
242 static  INLINE void     frpr_tcp __P((fr_info_t *));
243 static  INLINE void     frpr_icmp __P((fr_info_t *));
244 static  INLINE void     frpr_ipv4hdr __P((fr_info_t *));
245 static  INLINE int      frpr_pullup __P((fr_info_t *, int));
246 static  INLINE void     frpr_short __P((fr_info_t *, int));
247 static  INLINE int      frpr_tcpcommon __P((fr_info_t *));
248 static  INLINE int      frpr_udpcommon __P((fr_info_t *));
249 static  int             fr_updateipid __P((fr_info_t *));
250 #ifdef  IPFILTER_LOOKUP
251 static  int             fr_grpmapinit __P((frentry_t *fr));
252 static  INLINE void     *fr_resolvelookup __P((u_int, u_int, i6addr_t *, lookupfunc_t *));
253 #endif
254 static  void            frsynclist __P((frentry_t *, void *));
255 static  ipftuneable_t   *fr_findtunebyname __P((const char *));
256 static  ipftuneable_t   *fr_findtunebycookie __P((void *, void **));
257 static  int             ipf_geniter __P((ipftoken_t *, ipfgeniter_t *));
258 static  int             ipf_frruleiter __P((void *, int, void *));
259 static  void            ipf_unlinktoken __P((ipftoken_t *));
260
261
262 /*
263  * bit values for identifying presence of individual IP options
264  * All of these tables should be ordered by increasing key value on the left
265  * hand side to allow for binary searching of the array and include a trailer
266  * with a 0 for the bitmask for linear searches to easily find the end with.
267  */
268 const   struct  optlist ipopts[20] = {
269         { IPOPT_NOP,    0x000001 },
270         { IPOPT_RR,     0x000002 },
271         { IPOPT_ZSU,    0x000004 },
272         { IPOPT_MTUP,   0x000008 },
273         { IPOPT_MTUR,   0x000010 },
274         { IPOPT_ENCODE, 0x000020 },
275         { IPOPT_TS,     0x000040 },
276         { IPOPT_TR,     0x000080 },
277         { IPOPT_SECURITY, 0x000100 },
278         { IPOPT_LSRR,   0x000200 },
279         { IPOPT_E_SEC,  0x000400 },
280         { IPOPT_CIPSO,  0x000800 },
281         { IPOPT_SATID,  0x001000 },
282         { IPOPT_SSRR,   0x002000 },
283         { IPOPT_ADDEXT, 0x004000 },
284         { IPOPT_VISA,   0x008000 },
285         { IPOPT_IMITD,  0x010000 },
286         { IPOPT_EIP,    0x020000 },
287         { IPOPT_FINN,   0x040000 },
288         { 0,            0x000000 }
289 };
290
291 #ifdef USE_INET6
292 struct optlist ip6exthdr[] = {
293         { IPPROTO_HOPOPTS,              0x000001 },
294         { IPPROTO_IPV6,                 0x000002 },
295         { IPPROTO_ROUTING,              0x000004 },
296         { IPPROTO_FRAGMENT,             0x000008 },
297         { IPPROTO_ESP,                  0x000010 },
298         { IPPROTO_AH,                   0x000020 },
299         { IPPROTO_NONE,                 0x000040 },
300         { IPPROTO_DSTOPTS,              0x000080 },
301         { IPPROTO_MOBILITY,             0x000100 },
302         { 0,                            0 }
303 };
304 #endif
305
306 struct optlist tcpopts[] = {
307         { TCPOPT_NOP,                   0x000001 },
308         { TCPOPT_MAXSEG,                0x000002 },
309         { TCPOPT_WINDOW,                0x000004 },
310         { TCPOPT_SACK_PERMITTED,        0x000008 },
311         { TCPOPT_SACK,                  0x000010 },
312         { TCPOPT_TIMESTAMP,             0x000020 },
313         { 0,                            0x000000 }
314 };
315
316 /*
317  * bit values for identifying presence of individual IP security options
318  */
319 const   struct  optlist secopt[8] = {
320         { IPSO_CLASS_RES4,      0x01 },
321         { IPSO_CLASS_TOPS,      0x02 },
322         { IPSO_CLASS_SECR,      0x04 },
323         { IPSO_CLASS_RES3,      0x08 },
324         { IPSO_CLASS_CONF,      0x10 },
325         { IPSO_CLASS_UNCL,      0x20 },
326         { IPSO_CLASS_RES2,      0x40 },
327         { IPSO_CLASS_RES1,      0x80 }
328 };
329
330
331 /*
332  * Table of functions available for use with call rules.
333  */
334 static ipfunc_resolve_t fr_availfuncs[] = {
335 #ifdef  IPFILTER_LOOKUP
336         { "fr_srcgrpmap", fr_srcgrpmap, fr_grpmapinit },
337         { "fr_dstgrpmap", fr_dstgrpmap, fr_grpmapinit },
338 #endif
339         { "", NULL, NULL }
340 };
341
342
343 /*
344  * The next section of code is a a collection of small routines that set
345  * fields in the fr_info_t structure passed based on properties of the
346  * current packet.  There are different routines for the same protocol
347  * for each of IPv4 and IPv6.  Adding a new protocol, for which there
348  * will "special" inspection for setup, is now more easily done by adding
349  * a new routine and expanding the frpr_ipinit*() function rather than by
350  * adding more code to a growing switch statement.
351  */
352 #ifdef USE_INET6
353 static  INLINE int      frpr_ah6 __P((fr_info_t *));
354 static  INLINE void     frpr_esp6 __P((fr_info_t *));
355 static  INLINE void     frpr_gre6 __P((fr_info_t *));
356 static  INLINE void     frpr_udp6 __P((fr_info_t *));
357 static  INLINE void     frpr_tcp6 __P((fr_info_t *));
358 static  INLINE void     frpr_icmp6 __P((fr_info_t *));
359 static  INLINE int      frpr_ipv6hdr __P((fr_info_t *));
360 static  INLINE void     frpr_short6 __P((fr_info_t *, int));
361 static  INLINE int      frpr_hopopts6 __P((fr_info_t *));
362 static  INLINE int      frpr_mobility6 __P((fr_info_t *));
363 static  INLINE int      frpr_routing6 __P((fr_info_t *));
364 static  INLINE int      frpr_dstopts6 __P((fr_info_t *));
365 static  INLINE int      frpr_fragment6 __P((fr_info_t *));
366 static  INLINE int      frpr_ipv6exthdr __P((fr_info_t *, int, int));
367
368
369 /* ------------------------------------------------------------------------ */
370 /* Function:    frpr_short6                                                 */
371 /* Returns:     void                                                        */
372 /* Parameters:  fin(I) - pointer to packet information                      */
373 /*                                                                          */
374 /* IPv6 Only                                                                */
375 /* This is function enforces the 'is a packet too short to be legit' rule   */
376 /* for IPv6 and marks the packet with FI_SHORT if so.  See function comment */
377 /* for frpr_short() for more details.                                       */
378 /* ------------------------------------------------------------------------ */
379 static INLINE void frpr_short6(fin, xmin)
380 fr_info_t *fin;
381 int xmin;
382 {
383
384         if (fin->fin_dlen < xmin)
385                 fin->fin_flx |= FI_SHORT;
386 }
387
388
389 /* ------------------------------------------------------------------------ */
390 /* Function:    frpr_ipv6hdr                                                */
391 /* Returns:     int    - 0 = IPv6 packet intact, -1 = packet lost           */
392 /* Parameters:  fin(I) - pointer to packet information                      */
393 /*                                                                          */
394 /* IPv6 Only                                                                */
395 /* Copy values from the IPv6 header into the fr_info_t struct and call the  */
396 /* per-protocol analyzer if it exists.  In validating the packet, a protocol*/
397 /* analyzer may pullup or free the packet itself so we need to be vigiliant */
398 /* of that possibility arising.                                             */
399 /* ------------------------------------------------------------------------ */
400 static INLINE int frpr_ipv6hdr(fin)
401 fr_info_t *fin;
402 {
403         ip6_t *ip6 = (ip6_t *)fin->fin_ip;
404         int p, go = 1, i, hdrcount;
405         fr_ip_t *fi = &fin->fin_fi;
406
407         fin->fin_off = 0;
408
409         fi->fi_tos = 0;
410         fi->fi_optmsk = 0;
411         fi->fi_secmsk = 0;
412         fi->fi_auth = 0;
413
414         p = ip6->ip6_nxt;
415         fi->fi_ttl = ip6->ip6_hlim;
416         fi->fi_src.in6 = ip6->ip6_src;
417         fi->fi_dst.in6 = ip6->ip6_dst;
418         fin->fin_id = (u_short)(ip6->ip6_flow & 0xffff);
419
420         hdrcount = 0;
421         while (go && !(fin->fin_flx & (FI_BAD|FI_SHORT))) {
422                 switch (p)
423                 {
424                 case IPPROTO_UDP :
425                         frpr_udp6(fin);
426                         go = 0;
427                         break;
428
429                 case IPPROTO_TCP :
430                         frpr_tcp6(fin);
431                         go = 0;
432                         break;
433
434                 case IPPROTO_ICMPV6 :
435                         frpr_icmp6(fin);
436                         go = 0;
437                         break;
438
439                 case IPPROTO_GRE :
440                         frpr_gre6(fin);
441                         go = 0;
442                         break;
443
444                 case IPPROTO_HOPOPTS :
445                         p = frpr_hopopts6(fin);
446                         break;
447
448                 case IPPROTO_MOBILITY :
449                         p = frpr_mobility6(fin);
450                         break;
451
452                 case IPPROTO_DSTOPTS :
453                         p = frpr_dstopts6(fin);
454                         break;
455
456                 case IPPROTO_ROUTING :
457                         p = frpr_routing6(fin);
458                         break;
459
460                 case IPPROTO_AH :
461                         p = frpr_ah6(fin);
462                         break;
463
464                 case IPPROTO_ESP :
465                         frpr_esp6(fin);
466                         go = 0;
467                         break;
468
469                 case IPPROTO_IPV6 :
470                         for (i = 0; ip6exthdr[i].ol_bit != 0; i++)
471                                 if (ip6exthdr[i].ol_val == p) {
472                                         fin->fin_flx |= ip6exthdr[i].ol_bit;
473                                         break;
474                                 }
475                         go = 0;
476                         break;
477
478                 case IPPROTO_NONE :
479                         go = 0;
480                         break;
481
482                 case IPPROTO_FRAGMENT :
483                         p = frpr_fragment6(fin);
484                         if (fin->fin_off != 0)
485                                 go = 0;
486                         break;
487
488                 default :
489                         go = 0;
490                         break;
491                 }
492                 hdrcount++;
493
494                 /*
495                  * It is important to note that at this point, for the
496                  * extension headers (go != 0), the entire header may not have
497                  * been pulled up when the code gets to this point.  This is
498                  * only done for "go != 0" because the other header handlers
499                  * will all pullup their complete header.  The other indicator
500                  * of an incomplete packet is that this was just an extension
501                  * header.
502                  */
503                 if ((go != 0) && (p != IPPROTO_NONE) &&
504                     (frpr_pullup(fin, 0) == -1)) {
505                         p = IPPROTO_NONE;
506                         go = 0;
507                 }
508         }
509         fi->fi_p = p;
510
511         /*
512          * Some of the above functions, like frpr_esp6(), can call fr_pullup
513          * and destroy whatever packet was here.  The caller of this function
514          * expects us to return -1 if there is a problem with fr_pullup.
515          */
516         if (fin->fin_m == NULL)
517                 return -1;
518
519         return 0;
520 }
521
522
523 /* ------------------------------------------------------------------------ */
524 /* Function:    frpr_ipv6exthdr                                             */
525 /* Returns:     int    - value of the next header or IPPROTO_NONE if error  */
526 /* Parameters:  fin(I)      - pointer to packet information                 */
527 /*              multiple(I) - flag indicating yes/no if multiple occurances */
528 /*                            of this extension header are allowed.         */
529 /*              proto(I)    - protocol number for this extension header     */
530 /*                                                                          */
531 /* IPv6 Only                                                                */
532 /* ------------------------------------------------------------------------ */
533 static INLINE int frpr_ipv6exthdr(fin, multiple, proto)
534 fr_info_t *fin;
535 int multiple, proto;
536 {
537         struct ip6_ext *hdr;
538         u_short shift;
539         int i;
540
541         fin->fin_flx |= FI_V6EXTHDR;
542
543                                 /* 8 is default length of extension hdr */
544         if ((fin->fin_dlen - 8) < 0) {
545                 fin->fin_flx |= FI_SHORT;
546                 return IPPROTO_NONE;
547         }
548
549         if (frpr_pullup(fin, 8) == -1)
550                 return IPPROTO_NONE;
551
552         hdr = fin->fin_dp;
553         switch (proto)
554         {
555         case IPPROTO_FRAGMENT :
556                 shift = 8;
557                 break;
558         default :
559                 shift = 8 + (hdr->ip6e_len << 3);
560                 break;
561         }
562
563         if (shift > fin->fin_dlen) {    /* Nasty extension header length? */
564                 fin->fin_flx |= FI_BAD;
565                 return IPPROTO_NONE;
566         }
567
568         for (i = 0; ip6exthdr[i].ol_bit != 0; i++)
569                 if (ip6exthdr[i].ol_val == proto) {
570                         /*
571                          * Most IPv6 extension headers are only allowed once.
572                          */
573                         if ((multiple == 0) &&
574                             ((fin->fin_optmsk & ip6exthdr[i].ol_bit) != 0))
575                                 fin->fin_flx |= FI_BAD;
576                         else
577                                 fin->fin_optmsk |= ip6exthdr[i].ol_bit;
578                         break;
579                 }
580
581         fin->fin_exthdr = fin->fin_dp;
582         fin->fin_dp = (char *)fin->fin_dp + shift;
583         fin->fin_dlen -= shift;
584
585         return hdr->ip6e_nxt;
586 }
587
588
589 /* ------------------------------------------------------------------------ */
590 /* Function:    frpr_hopopts6                                               */
591 /* Returns:     int    - value of the next header or IPPROTO_NONE if error  */
592 /* Parameters:  fin(I) - pointer to packet information                      */
593 /*                                                                          */
594 /* IPv6 Only                                                                */
595 /* This is function checks pending hop by hop options extension header      */
596 /* ------------------------------------------------------------------------ */
597 static INLINE int frpr_hopopts6(fin)
598 fr_info_t *fin;
599 {
600         return frpr_ipv6exthdr(fin, 0, IPPROTO_HOPOPTS);
601 }
602
603
604 /* ------------------------------------------------------------------------ */
605 /* Function:    frpr_mobility6                                              */
606 /* Returns:     int    - value of the next header or IPPROTO_NONE if error  */
607 /* Parameters:  fin(I) - pointer to packet information                      */
608 /*                                                                          */
609 /* IPv6 Only                                                                */
610 /* This is function checks the IPv6 mobility extension header               */
611 /* ------------------------------------------------------------------------ */
612 static INLINE int frpr_mobility6(fin)
613 fr_info_t *fin;
614 {
615         return frpr_ipv6exthdr(fin, 0, IPPROTO_MOBILITY);
616 }
617
618
619 /* ------------------------------------------------------------------------ */
620 /* Function:    frpr_routing6                                               */
621 /* Returns:     int    - value of the next header or IPPROTO_NONE if error  */
622 /* Parameters:  fin(I) - pointer to packet information                      */
623 /*                                                                          */
624 /* IPv6 Only                                                                */
625 /* This is function checks pending routing extension header                 */
626 /* ------------------------------------------------------------------------ */
627 static INLINE int frpr_routing6(fin)
628 fr_info_t *fin;
629 {
630         struct ip6_ext *hdr;
631
632         if (frpr_ipv6exthdr(fin, 0, IPPROTO_ROUTING) == IPPROTO_NONE)
633                 return IPPROTO_NONE;
634         hdr = fin->fin_exthdr;
635
636         if ((hdr->ip6e_len & 1) != 0) {
637                 /*
638                  * The routing header data is made up of 128 bit IPv6 addresses
639                  * which means it must be a multiple of 2 lots of 8 in length.
640                  */
641                 fin->fin_flx |= FI_BAD;
642                 /*
643                  * Compensate for the changes made in frpr_ipv6exthdr()
644                  */
645                 fin->fin_dlen += 8 + (hdr->ip6e_len << 3);
646                 fin->fin_dp = hdr;
647                 return IPPROTO_NONE;
648         }
649
650         return hdr->ip6e_nxt;
651 }
652
653
654 /* ------------------------------------------------------------------------ */
655 /* Function:    frpr_fragment6                                              */
656 /* Returns:     int    - value of the next header or IPPROTO_NONE if error  */
657 /* Parameters:  fin(I) - pointer to packet information                      */
658 /*                                                                          */
659 /* IPv6 Only                                                                */
660 /* Examine the IPv6 fragment header and extract fragment offset information.*/
661 /*                                                                          */
662 /* We don't know where the transport layer header (or whatever is next is), */
663 /* as it could be behind destination options (amongst others).  Because     */
664 /* there is no fragment cache, there is no knowledge about whether or not an*/
665 /* upper layer header has been seen (or where it ends) and thus we are not  */
666 /* able to continue processing beyond this header with any confidence.      */
667 /* ------------------------------------------------------------------------ */
668 static INLINE int frpr_fragment6(fin)
669 fr_info_t *fin;
670 {
671         struct ip6_frag *frag;
672         int extoff;
673
674         fin->fin_flx |= FI_FRAG;
675
676         if (frpr_ipv6exthdr(fin, 0, IPPROTO_FRAGMENT) == IPPROTO_NONE)
677                 return IPPROTO_NONE;
678
679         extoff = (char *)fin->fin_exthdr - (char *)fin->fin_dp;
680
681         if (frpr_pullup(fin, sizeof(*frag)) == -1)
682                 return IPPROTO_NONE;
683
684         fin->fin_exthdr = (char *)fin->fin_dp + extoff;
685         frag = fin->fin_exthdr;
686         /*
687          * Fragment but no fragmentation info set?  Bad packet...
688          */
689         if (frag->ip6f_offlg == 0) {
690                 fin->fin_flx |= FI_BAD;
691                 return IPPROTO_NONE;
692         }
693
694         fin->fin_off = ntohs(frag->ip6f_offlg & IP6F_OFF_MASK);
695         fin->fin_off <<= 3;
696         if (fin->fin_off != 0)
697                 fin->fin_flx |= FI_FRAGBODY;
698
699         fin->fin_dp = (char *)fin->fin_dp + sizeof(*frag);
700         fin->fin_dlen -= sizeof(*frag);
701
702         return frag->ip6f_nxt;
703 }
704
705
706 /* ------------------------------------------------------------------------ */
707 /* Function:    frpr_dstopts6                                               */
708 /* Returns:     int    - value of the next header or IPPROTO_NONE if error  */
709 /* Parameters:  fin(I) - pointer to packet information                      */
710 /*              nextheader(I) - stores next header value                    */
711 /*                                                                          */
712 /* IPv6 Only                                                                */
713 /* This is function checks pending destination options extension header     */
714 /* ------------------------------------------------------------------------ */
715 static INLINE int frpr_dstopts6(fin)
716 fr_info_t *fin;
717 {
718         return frpr_ipv6exthdr(fin, 1, IPPROTO_DSTOPTS);
719 }
720
721
722 /* ------------------------------------------------------------------------ */
723 /* Function:    frpr_icmp6                                                  */
724 /* Returns:     void                                                        */
725 /* Parameters:  fin(I) - pointer to packet information                      */
726 /*                                                                          */
727 /* IPv6 Only                                                                */
728 /* This routine is mainly concerned with determining the minimum valid size */
729 /* for an ICMPv6 packet.                                                    */
730 /* ------------------------------------------------------------------------ */
731 static INLINE void frpr_icmp6(fin)
732 fr_info_t *fin;
733 {
734         int minicmpsz = sizeof(struct icmp6_hdr);
735         struct icmp6_hdr *icmp6;
736
737         if (frpr_pullup(fin, ICMP6ERR_MINPKTLEN - sizeof(ip6_t)) == -1)
738                 return;
739
740         if (fin->fin_dlen > 1) {
741                 ip6_t *ip6;
742
743                 icmp6 = fin->fin_dp;
744
745                 fin->fin_data[0] = *(u_short *)icmp6;
746
747                 switch (icmp6->icmp6_type)
748                 {
749                 case ICMP6_ECHO_REPLY :
750                 case ICMP6_ECHO_REQUEST :
751                         minicmpsz = ICMP6ERR_MINPKTLEN - sizeof(ip6_t);
752                         break;
753                 case ICMP6_DST_UNREACH :
754                 case ICMP6_PACKET_TOO_BIG :
755                 case ICMP6_TIME_EXCEEDED :
756                 case ICMP6_PARAM_PROB :
757                         fin->fin_flx |= FI_ICMPERR;
758                         minicmpsz = ICMP6ERR_IPICMPHLEN - sizeof(ip6_t);
759                         if (fin->fin_plen < ICMP6ERR_IPICMPHLEN)
760                                 break;
761
762                         if (M_LEN(fin->fin_m) < fin->fin_plen) {
763                                 if (fr_coalesce(fin) != 1)
764                                         return;
765                         }
766
767                         /*
768                          * If the destination of this packet doesn't match the
769                          * source of the original packet then this packet is
770                          * not correct.
771                          */
772                         icmp6 = fin->fin_dp;
773                         ip6 = (ip6_t *)((char *)icmp6 + ICMPERR_ICMPHLEN);
774                         if (IP6_NEQ(&fin->fin_fi.fi_dst,
775                                     (i6addr_t *)&ip6->ip6_src))
776                                 fin->fin_flx |= FI_BAD;
777
778                         break;
779                 default :
780                         break;
781                 }
782         }
783
784         frpr_short6(fin, minicmpsz);
785 }
786
787
788 /* ------------------------------------------------------------------------ */
789 /* Function:    frpr_udp6                                                   */
790 /* Returns:     void                                                        */
791 /* Parameters:  fin(I) - pointer to packet information                      */
792 /*                                                                          */
793 /* IPv6 Only                                                                */
794 /* Analyse the packet for IPv6/UDP properties.                              */
795 /* Is not expected to be called for fragmented packets.                     */
796 /* ------------------------------------------------------------------------ */
797 static INLINE void frpr_udp6(fin)
798 fr_info_t *fin;
799 {
800
801         frpr_short6(fin, sizeof(struct udphdr));
802
803         if (frpr_udpcommon(fin) == 0) {
804                 u_char p = fin->fin_p;
805
806                 fin->fin_p = IPPROTO_UDP;
807                 fr_checkv6sum(fin);
808                 fin->fin_p = p;
809         }
810 }
811
812
813 /* ------------------------------------------------------------------------ */
814 /* Function:    frpr_tcp6                                                   */
815 /* Returns:     void                                                        */
816 /* Parameters:  fin(I) - pointer to packet information                      */
817 /*                                                                          */
818 /* IPv6 Only                                                                */
819 /* Analyse the packet for IPv6/TCP properties.                              */
820 /* Is not expected to be called for fragmented packets.                     */
821 /* ------------------------------------------------------------------------ */
822 static INLINE void frpr_tcp6(fin)
823 fr_info_t *fin;
824 {
825
826         frpr_short6(fin, sizeof(struct tcphdr));
827
828         if (frpr_tcpcommon(fin) == 0) {
829                 u_char p = fin->fin_p;
830
831                 fin->fin_p = IPPROTO_TCP;
832                 fr_checkv6sum(fin);
833                 fin->fin_p = p;
834         }
835 }
836
837
838 /* ------------------------------------------------------------------------ */
839 /* Function:    frpr_esp6                                                   */
840 /* Returns:     void                                                        */
841 /* Parameters:  fin(I) - pointer to packet information                      */
842 /*                                                                          */
843 /* IPv6 Only                                                                */
844 /* Analyse the packet for ESP properties.                                   */
845 /* The minimum length is taken to be the SPI (32bits) plus a tail (32bits)  */
846 /* even though the newer ESP packets must also have a sequence number that  */
847 /* is 32bits as well, it is not possible(?) to determine the version from a */
848 /* simple packet header.                                                    */
849 /* ------------------------------------------------------------------------ */
850 static INLINE void frpr_esp6(fin)
851 fr_info_t *fin;
852 {
853
854         frpr_short6(fin, sizeof(grehdr_t));
855
856         (void) frpr_pullup(fin, 8);
857 }
858
859
860 /* ------------------------------------------------------------------------ */
861 /* Function:    frpr_ah6                                                    */
862 /* Returns:     void                                                        */
863 /* Parameters:  fin(I) - pointer to packet information                      */
864 /*                                                                          */
865 /* IPv6 Only                                                                */
866 /* Analyse the packet for AH properties.                                    */
867 /* The minimum length is taken to be the combination of all fields in the   */
868 /* header being present and no authentication data (null algorithm used.)   */
869 /* ------------------------------------------------------------------------ */
870 static INLINE int frpr_ah6(fin)
871 fr_info_t *fin;
872 {
873         authhdr_t *ah;
874
875         frpr_short6(fin, 12);
876
877         if (frpr_pullup(fin, sizeof(*ah)) == -1)
878                 return IPPROTO_NONE;
879
880         ah = (authhdr_t *)fin->fin_dp;
881         return ah->ah_next;
882 }
883
884
885 /* ------------------------------------------------------------------------ */
886 /* Function:    frpr_gre6                                                   */
887 /* Returns:     void                                                        */
888 /* Parameters:  fin(I) - pointer to packet information                      */
889 /*                                                                          */
890 /* Analyse the packet for GRE properties.                                   */
891 /* ------------------------------------------------------------------------ */
892 static INLINE void frpr_gre6(fin)
893 fr_info_t *fin;
894 {
895         grehdr_t *gre;
896
897         frpr_short6(fin, sizeof(grehdr_t));
898
899         if (frpr_pullup(fin, sizeof(grehdr_t)) == -1)
900                 return;
901
902         gre = fin->fin_dp;
903         if (GRE_REV(gre->gr_flags) == 1)
904                 fin->fin_data[0] = gre->gr_call;
905 }
906 #endif  /* USE_INET6 */
907
908
909 /* ------------------------------------------------------------------------ */
910 /* Function:    frpr_pullup                                                 */
911 /* Returns:     int     - 0 == pullup succeeded, -1 == failure              */
912 /* Parameters:  fin(I)  - pointer to packet information                     */
913 /*              plen(I) - length (excluding L3 header) to pullup            */
914 /*                                                                          */
915 /* Short inline function to cut down on code duplication to perform a call  */
916 /* to fr_pullup to ensure there is the required amount of data,             */
917 /* consecutively in the packet buffer.                                      */
918 /*                                                                          */
919 /* This function pulls up 'extra' data at the location of fin_dp.  fin_dp   */
920 /* points to the first byte after the complete layer 3 header, which will   */
921 /* include all of the known extension headers for IPv6 or options for IPv4. */
922 /*                                                                          */
923 /* Since fr_pullup() expects the total length of bytes to be pulled up, it  */
924 /* is necessary to add those we can already assume to be pulled up (fin_dp  */
925 /* - fin_ip) to what is passed through.                                     */
926 /* ------------------------------------------------------------------------ */
927 static INLINE int frpr_pullup(fin, plen)
928 fr_info_t *fin;
929 int plen;
930 {
931         if (fin->fin_m != NULL) {
932                 if (fin->fin_dp != NULL)
933                         plen += (char *)fin->fin_dp -
934                                 ((char *)fin->fin_ip + fin->fin_hlen);
935                 plen += fin->fin_hlen;
936                 if (M_LEN(fin->fin_m) < plen) {
937 #if defined(_KERNEL)
938                         if (fr_pullup(fin->fin_m, fin, plen) == NULL)
939                                 return -1;
940 #else
941                         /*
942                          * Fake fr_pullup failing
943                          */
944                         *fin->fin_mp = NULL;
945                         fin->fin_m = NULL;
946                         fin->fin_ip = NULL;
947                         return -1;
948 #endif
949                 }
950         }
951         return 0;
952 }
953
954
955 /* ------------------------------------------------------------------------ */
956 /* Function:    frpr_short                                                  */
957 /* Returns:     void                                                        */
958 /* Parameters:  fin(I)  - pointer to packet information                     */
959 /*              xmin(I) - minimum header size                               */
960 /*                                                                          */
961 /* Check if a packet is "short" as defined by xmin.  The rule we are        */
962 /* applying here is that the packet must not be fragmented within the layer */
963 /* 4 header.  That is, it must not be a fragment that has its offset set to */
964 /* start within the layer 4 header (hdrmin) or if it is at offset 0, the    */
965 /* entire layer 4 header must be present (min).                             */
966 /* ------------------------------------------------------------------------ */
967 static INLINE void frpr_short(fin, xmin)
968 fr_info_t *fin;
969 int xmin;
970 {
971
972         if (fin->fin_off == 0) {
973                 if (fin->fin_dlen < xmin)
974                         fin->fin_flx |= FI_SHORT;
975         } else if (fin->fin_off < xmin) {
976                 fin->fin_flx |= FI_SHORT;
977         }
978 }
979
980
981 /* ------------------------------------------------------------------------ */
982 /* Function:    frpr_icmp                                                   */
983 /* Returns:     void                                                        */
984 /* Parameters:  fin(I) - pointer to packet information                      */
985 /*                                                                          */
986 /* IPv4 Only                                                                */
987 /* Do a sanity check on the packet for ICMP (v4).  In nearly all cases,     */
988 /* except extrememly bad packets, both type and code will be present.       */
989 /* The expected minimum size of an ICMP packet is very much dependent on    */
990 /* the type of it.                                                          */
991 /*                                                                          */
992 /* XXX - other ICMP sanity checks?                                          */
993 /* ------------------------------------------------------------------------ */
994 static INLINE void frpr_icmp(fin)
995 fr_info_t *fin;
996 {
997         int minicmpsz = sizeof(struct icmp);
998         icmphdr_t *icmp;
999         ip_t *oip;
1000
1001         if (fin->fin_off != 0) {
1002                 frpr_short(fin, ICMPERR_ICMPHLEN);
1003                 return;
1004         }
1005
1006         if (frpr_pullup(fin, ICMPERR_ICMPHLEN) == -1)
1007                 return;
1008
1009         if (fin->fin_dlen > 1) {
1010                 icmp = fin->fin_dp;
1011
1012                 fin->fin_data[0] = *(u_short *)icmp;
1013
1014                 if (fin->fin_dlen >= 6)                         /* ID field */
1015                         fin->fin_data[1] = icmp->icmp_id;
1016
1017                 switch (icmp->icmp_type)
1018                 {
1019                 case ICMP_ECHOREPLY :
1020                 case ICMP_ECHO :
1021                 /* Router discovery messaes - RFC 1256 */
1022                 case ICMP_ROUTERADVERT :
1023                 case ICMP_ROUTERSOLICIT :
1024                         minicmpsz = ICMP_MINLEN;
1025                         break;
1026                 /*
1027                  * type(1) + code(1) + cksum(2) + id(2) seq(2) +
1028                  * 3 * timestamp(3 * 4)
1029                  */
1030                 case ICMP_TSTAMP :
1031                 case ICMP_TSTAMPREPLY :
1032                         minicmpsz = 20;
1033                         break;
1034                 /*
1035                  * type(1) + code(1) + cksum(2) + id(2) seq(2) +
1036                  * mask(4)
1037                  */
1038                 case ICMP_MASKREQ :
1039                 case ICMP_MASKREPLY :
1040                         minicmpsz = 12;
1041                         break;
1042                 /*
1043                  * type(1) + code(1) + cksum(2) + id(2) seq(2) + ip(20+)
1044                  */
1045                 case ICMP_UNREACH :
1046 #ifdef icmp_nextmtu
1047                         if (icmp->icmp_code == ICMP_UNREACH_NEEDFRAG) {
1048                                 if (icmp->icmp_nextmtu < fr_icmpminfragmtu)
1049                                         fin->fin_flx |= FI_BAD;
1050                         }
1051 #endif
1052                 case ICMP_SOURCEQUENCH :
1053                 case ICMP_REDIRECT :
1054                 case ICMP_TIMXCEED :
1055                 case ICMP_PARAMPROB :
1056                         fin->fin_flx |= FI_ICMPERR;
1057                         if (fr_coalesce(fin) != 1)
1058                                 return;
1059                         /*
1060                          * ICMP error packets should not be generated for IP
1061                          * packets that are a fragment that isn't the first
1062                          * fragment.
1063                          */
1064                         oip = (ip_t *)((char *)fin->fin_dp + ICMPERR_ICMPHLEN);
1065                         if ((ntohs(oip->ip_off) & IP_OFFMASK) != 0)
1066                                 fin->fin_flx |= FI_BAD;
1067
1068                         /*
1069                          * If the destination of this packet doesn't match the
1070                          * source of the original packet then this packet is
1071                          * not correct.
1072                          */
1073                         if (oip->ip_src.s_addr != fin->fin_daddr)
1074                                 fin->fin_flx |= FI_BAD;
1075
1076                         /*
1077                          * If the destination of this packet doesn't match the
1078                          * source of the original packet then this packet is
1079                          * not correct.
1080                          */
1081                         if (oip->ip_src.s_addr != fin->fin_daddr)
1082                                 fin->fin_flx |= FI_BAD;
1083                         break;
1084                 default :
1085                         break;
1086                 }
1087         }
1088
1089         frpr_short(fin, minicmpsz);
1090
1091         if ((fin->fin_flx & FI_FRAG) == 0)
1092                 fr_checkv4sum(fin);
1093 }
1094
1095
1096 /* ------------------------------------------------------------------------ */
1097 /* Function:    frpr_tcpcommon                                              */
1098 /* Returns:     int    - 0 = header ok, 1 = bad packet, -1 = buffer error   */
1099 /* Parameters:  fin(I) - pointer to packet information                      */
1100 /*                                                                          */
1101 /* TCP header sanity checking.  Look for bad combinations of TCP flags,     */
1102 /* and make some checks with how they interact with other fields.           */
1103 /* If compiled with IPFILTER_CKSUM, check to see if the TCP checksum is     */
1104 /* valid and mark the packet as bad if not.                                 */
1105 /* ------------------------------------------------------------------------ */
1106 static INLINE int frpr_tcpcommon(fin)
1107 fr_info_t *fin;
1108 {
1109         int flags, tlen;
1110         tcphdr_t *tcp;
1111
1112         fin->fin_flx |= FI_TCPUDP;
1113         if (fin->fin_off != 0)
1114                 return 0;
1115
1116         if (frpr_pullup(fin, sizeof(*tcp)) == -1)
1117                 return -1;
1118         tcp = fin->fin_dp;
1119
1120         if (fin->fin_dlen > 3) {
1121                 fin->fin_sport = ntohs(tcp->th_sport);
1122                 fin->fin_dport = ntohs(tcp->th_dport);
1123         }
1124
1125         if ((fin->fin_flx & FI_SHORT) != 0)
1126                 return 1;
1127
1128         /*
1129          * Use of the TCP data offset *must* result in a value that is at
1130          * least the same size as the TCP header.
1131          */
1132         tlen = TCP_OFF(tcp) << 2;
1133         if (tlen < sizeof(tcphdr_t)) {
1134                 fin->fin_flx |= FI_BAD;
1135                 return 1;
1136         }
1137
1138         flags = tcp->th_flags;
1139         fin->fin_tcpf = tcp->th_flags;
1140
1141         /*
1142          * If the urgent flag is set, then the urgent pointer must
1143          * also be set and vice versa.  Good TCP packets do not have
1144          * just one of these set.
1145          */
1146         if ((flags & TH_URG) != 0 && (tcp->th_urp == 0)) {
1147                 fin->fin_flx |= FI_BAD;
1148 #if 0
1149         } else if ((flags & TH_URG) == 0 && (tcp->th_urp != 0)) {
1150                 /*
1151                  * Ignore this case (#if 0) as it shows up in "real"
1152                  * traffic with bogus values in the urgent pointer field.
1153                  */
1154                 fin->fin_flx |= FI_BAD;
1155 #endif
1156         } else if (((flags & (TH_SYN|TH_FIN)) != 0) &&
1157                    ((flags & (TH_RST|TH_ACK)) == TH_RST)) {
1158                 /* TH_FIN|TH_RST|TH_ACK seems to appear "naturally" */
1159                 fin->fin_flx |= FI_BAD;
1160 #if 1
1161         } else if (((flags & TH_SYN) != 0) &&
1162                    ((flags & (TH_URG|TH_PUSH)) != 0)) {
1163                 /*
1164                  * SYN with URG and PUSH set is not for normal TCP but it is
1165                  * possible(?) with T/TCP...but who uses T/TCP?
1166                  */
1167                 fin->fin_flx |= FI_BAD;
1168 #endif
1169         } else if (!(flags & TH_ACK)) {
1170                 /*
1171                  * If the ack bit isn't set, then either the SYN or
1172                  * RST bit must be set.  If the SYN bit is set, then
1173                  * we expect the ACK field to be 0.  If the ACK is
1174                  * not set and if URG, PSH or FIN are set, consdier
1175                  * that to indicate a bad TCP packet.
1176                  */
1177                 if ((flags == TH_SYN) && (tcp->th_ack != 0)) {
1178                         /*
1179                          * Cisco PIX sets the ACK field to a random value.
1180                          * In light of this, do not set FI_BAD until a patch
1181                          * is available from Cisco to ensure that
1182                          * interoperability between existing systems is
1183                          * achieved.
1184                          */
1185                         /*fin->fin_flx |= FI_BAD*/;
1186                 } else if (!(flags & (TH_RST|TH_SYN))) {
1187                         fin->fin_flx |= FI_BAD;
1188                 } else if ((flags & (TH_URG|TH_PUSH|TH_FIN)) != 0) {
1189                         fin->fin_flx |= FI_BAD;
1190                 }
1191         }
1192
1193         /*
1194          * At this point, it's not exactly clear what is to be gained by
1195          * marking up which TCP options are and are not present.  The one we
1196          * are most interested in is the TCP window scale.  This is only in
1197          * a SYN packet [RFC1323] so we don't need this here...?
1198          * Now if we were to analyse the header for passive fingerprinting,
1199          * then that might add some weight to adding this...
1200          */
1201         if (tlen == sizeof(tcphdr_t))
1202                 return 0;
1203
1204         if (frpr_pullup(fin, tlen) == -1)
1205                 return -1;
1206
1207 #if 0
1208         tcp = fin->fin_dp;
1209         ip = fin->fin_ip;
1210         s = (u_char *)(tcp + 1);
1211         off = IP_HL(ip) << 2;
1212 # ifdef _KERNEL
1213         if (fin->fin_mp != NULL) {
1214                 mb_t *m = *fin->fin_mp;
1215
1216                 if (off + tlen > M_LEN(m))
1217                         return;
1218         }
1219 # endif
1220         for (tlen -= (int)sizeof(*tcp); tlen > 0; ) {
1221                 opt = *s;
1222                 if (opt == '\0')
1223                         break;
1224                 else if (opt == TCPOPT_NOP)
1225                         ol = 1;
1226                 else {
1227                         if (tlen < 2)
1228                                 break;
1229                         ol = (int)*(s + 1);
1230                         if (ol < 2 || ol > tlen)
1231                                 break;
1232                 }
1233
1234                 for (i = 9, mv = 4; mv >= 0; ) {
1235                         op = ipopts + i;
1236                         if (opt == (u_char)op->ol_val) {
1237                                 optmsk |= op->ol_bit;
1238                                 break;
1239                         }
1240                 }
1241                 tlen -= ol;
1242                 s += ol;
1243         }
1244 #endif /* 0 */
1245
1246         return 0;
1247 }
1248
1249
1250
1251 /* ------------------------------------------------------------------------ */
1252 /* Function:    frpr_udpcommon                                              */
1253 /* Returns:     int    - 0 = header ok, 1 = bad packet                      */
1254 /* Parameters:  fin(I) - pointer to packet information                      */
1255 /*                                                                          */
1256 /* Extract the UDP source and destination ports, if present.  If compiled   */
1257 /* with IPFILTER_CKSUM, check to see if the UDP checksum is valid.          */
1258 /* ------------------------------------------------------------------------ */
1259 static INLINE int frpr_udpcommon(fin)
1260 fr_info_t *fin;
1261 {
1262         udphdr_t *udp;
1263
1264         fin->fin_flx |= FI_TCPUDP;
1265
1266         if (!fin->fin_off && (fin->fin_dlen > 3)) {
1267                 if (frpr_pullup(fin, sizeof(*udp)) == -1) {
1268                         fin->fin_flx |= FI_SHORT;
1269                         return 1;
1270                 }
1271
1272                 udp = fin->fin_dp;
1273
1274                 fin->fin_sport = ntohs(udp->uh_sport);
1275                 fin->fin_dport = ntohs(udp->uh_dport);
1276         }
1277
1278         return 0;
1279 }
1280
1281
1282 /* ------------------------------------------------------------------------ */
1283 /* Function:    frpr_tcp                                                    */
1284 /* Returns:     void                                                        */
1285 /* Parameters:  fin(I) - pointer to packet information                      */
1286 /*                                                                          */
1287 /* IPv4 Only                                                                */
1288 /* Analyse the packet for IPv4/TCP properties.                              */
1289 /* ------------------------------------------------------------------------ */
1290 static INLINE void frpr_tcp(fin)
1291 fr_info_t *fin;
1292 {
1293
1294         frpr_short(fin, sizeof(tcphdr_t));
1295
1296         if (frpr_tcpcommon(fin) == 0) {
1297                 if ((fin->fin_flx & FI_FRAG) == 0)
1298                         fr_checkv4sum(fin);
1299         }
1300 }
1301
1302
1303 /* ------------------------------------------------------------------------ */
1304 /* Function:    frpr_udp                                                    */
1305 /* Returns:     void                                                        */
1306 /* Parameters:  fin(I) - pointer to packet information                      */
1307 /*                                                                          */
1308 /* IPv4 Only                                                                */
1309 /* Analyse the packet for IPv4/UDP properties.                              */
1310 /* ------------------------------------------------------------------------ */
1311 static INLINE void frpr_udp(fin)
1312 fr_info_t *fin;
1313 {
1314
1315         frpr_short(fin, sizeof(udphdr_t));
1316
1317         if (frpr_udpcommon(fin) == 0) {
1318                 if ((fin->fin_flx & FI_FRAG) == 0)
1319                         fr_checkv4sum(fin);
1320         }
1321 }
1322
1323
1324 /* ------------------------------------------------------------------------ */
1325 /* Function:    frpr_esp                                                    */
1326 /* Returns:     void                                                        */
1327 /* Parameters:  fin(I) - pointer to packet information                      */
1328 /*                                                                          */
1329 /* Analyse the packet for ESP properties.                                   */
1330 /* The minimum length is taken to be the SPI (32bits) plus a tail (32bits)  */
1331 /* even though the newer ESP packets must also have a sequence number that  */
1332 /* is 32bits as well, it is not possible(?) to determine the version from a */
1333 /* simple packet header.                                                    */
1334 /* ------------------------------------------------------------------------ */
1335 static INLINE void frpr_esp(fin)
1336 fr_info_t *fin;
1337 {
1338
1339         if (fin->fin_off == 0) {
1340                 frpr_short(fin, 8);
1341                 (void) frpr_pullup(fin, 8);
1342         }
1343
1344 }
1345
1346
1347 /* ------------------------------------------------------------------------ */
1348 /* Function:    frpr_ah                                                     */
1349 /* Returns:     void                                                        */
1350 /* Parameters:  fin(I) - pointer to packet information                      */
1351 /*                                                                          */
1352 /* Analyse the packet for AH properties.                                    */
1353 /* The minimum length is taken to be the combination of all fields in the   */
1354 /* header being present and no authentication data (null algorithm used.)   */
1355 /* ------------------------------------------------------------------------ */
1356 static INLINE void frpr_ah(fin)
1357 fr_info_t *fin;
1358 {
1359         authhdr_t *ah;
1360         int len;
1361
1362         frpr_short(fin, sizeof(*ah));
1363
1364         if (((fin->fin_flx & FI_SHORT) != 0) || (fin->fin_off != 0))
1365                 return;
1366
1367         if (frpr_pullup(fin, sizeof(*ah)) == -1)
1368                 return;
1369
1370         ah = (authhdr_t *)fin->fin_dp;
1371
1372         len = (ah->ah_plen + 2) << 2;
1373         frpr_short(fin, len);
1374 }
1375
1376
1377 /* ------------------------------------------------------------------------ */
1378 /* Function:    frpr_gre                                                    */
1379 /* Returns:     void                                                        */
1380 /* Parameters:  fin(I) - pointer to packet information                      */
1381 /*                                                                          */
1382 /* Analyse the packet for GRE properties.                                   */
1383 /* ------------------------------------------------------------------------ */
1384 static INLINE void frpr_gre(fin)
1385 fr_info_t *fin;
1386 {
1387         grehdr_t *gre;
1388
1389         frpr_short(fin, sizeof(*gre));
1390
1391         if (fin->fin_off != 0)
1392                 return;
1393
1394         if (frpr_pullup(fin, sizeof(*gre)) == -1)
1395                 return;
1396
1397         if (fin->fin_off == 0) {
1398                 gre = fin->fin_dp;
1399                 if (GRE_REV(gre->gr_flags) == 1)
1400                         fin->fin_data[0] = gre->gr_call;
1401         }
1402 }
1403
1404
1405 /* ------------------------------------------------------------------------ */
1406 /* Function:    frpr_ipv4hdr                                                */
1407 /* Returns:     void                                                        */
1408 /* Parameters:  fin(I) - pointer to packet information                      */
1409 /*                                                                          */
1410 /* IPv4 Only                                                                */
1411 /* Analyze the IPv4 header and set fields in the fr_info_t structure.       */
1412 /* Check all options present and flag their presence if any exist.          */
1413 /* ------------------------------------------------------------------------ */
1414 static INLINE void frpr_ipv4hdr(fin)
1415 fr_info_t *fin;
1416 {
1417         u_short optmsk = 0, secmsk = 0, auth = 0;
1418         int hlen, ol, mv, p, i;
1419         const struct optlist *op;
1420         u_char *s, opt;
1421         u_short off;
1422         fr_ip_t *fi;
1423         ip_t *ip;
1424
1425         fi = &fin->fin_fi;
1426         hlen = fin->fin_hlen;
1427
1428         ip = fin->fin_ip;
1429         p = ip->ip_p;
1430         fi->fi_p = p;
1431         fi->fi_tos = ip->ip_tos;
1432         fin->fin_id = ip->ip_id;
1433         off = ip->ip_off;
1434
1435         /* Get both TTL and protocol */
1436         fi->fi_p = ip->ip_p;
1437         fi->fi_ttl = ip->ip_ttl;
1438 #if 0
1439         (*(((u_short *)fi) + 1)) = (*(((u_short *)ip) + 4));
1440 #endif
1441
1442         /* Zero out bits not used in IPv6 address */
1443         fi->fi_src.i6[1] = 0;
1444         fi->fi_src.i6[2] = 0;
1445         fi->fi_src.i6[3] = 0;
1446         fi->fi_dst.i6[1] = 0;
1447         fi->fi_dst.i6[2] = 0;
1448         fi->fi_dst.i6[3] = 0;
1449
1450         fi->fi_saddr = ip->ip_src.s_addr;
1451         fi->fi_daddr = ip->ip_dst.s_addr;
1452
1453         /*
1454          * set packet attribute flags based on the offset and
1455          * calculate the byte offset that it represents.
1456          */
1457         off &= IP_MF|IP_OFFMASK;
1458         if (off != 0) {
1459                 int morefrag = off & IP_MF;
1460
1461                 fi->fi_flx |= FI_FRAG;
1462                 off &= IP_OFFMASK;
1463                 if (off != 0) {
1464                         fin->fin_flx |= FI_FRAGBODY;
1465                         off <<= 3;
1466                         if ((off + fin->fin_dlen > 65535) || 
1467                             (fin->fin_dlen == 0) ||
1468                             ((morefrag != 0) && ((fin->fin_dlen & 7) != 0))) {
1469                                 /* 
1470                                  * The length of the packet, starting at its
1471                                  * offset cannot exceed 65535 (0xffff) as the 
1472                                  * length of an IP packet is only 16 bits.
1473                                  *
1474                                  * Any fragment that isn't the last fragment
1475                                  * must have a length greater than 0 and it
1476                                  * must be an even multiple of 8.
1477                                  */
1478                                 fi->fi_flx |= FI_BAD;
1479                         }
1480                 }
1481         }
1482         fin->fin_off = off;
1483
1484         /*
1485          * Call per-protocol setup and checking
1486          */
1487         switch (p)
1488         {
1489         case IPPROTO_UDP :
1490                 frpr_udp(fin);
1491                 break;
1492         case IPPROTO_TCP :
1493                 frpr_tcp(fin);
1494                 break;
1495         case IPPROTO_ICMP :
1496                 frpr_icmp(fin);
1497                 break;
1498         case IPPROTO_AH :
1499                 frpr_ah(fin);
1500                 break;
1501         case IPPROTO_ESP :
1502                 frpr_esp(fin);
1503                 break;
1504         case IPPROTO_GRE :
1505                 frpr_gre(fin);
1506                 break;
1507         }
1508
1509         ip = fin->fin_ip;
1510         if (ip == NULL)
1511                 return;
1512
1513         /*
1514          * If it is a standard IP header (no options), set the flag fields
1515          * which relate to options to 0.
1516          */
1517         if (hlen == sizeof(*ip)) {
1518                 fi->fi_optmsk = 0;
1519                 fi->fi_secmsk = 0;
1520                 fi->fi_auth = 0;
1521                 return;
1522         }
1523
1524         /*
1525          * So the IP header has some IP options attached.  Walk the entire
1526          * list of options present with this packet and set flags to indicate
1527          * which ones are here and which ones are not.  For the somewhat out
1528          * of date and obscure security classification options, set a flag to
1529          * represent which classification is present.
1530          */
1531         fi->fi_flx |= FI_OPTIONS;
1532
1533         for (s = (u_char *)(ip + 1), hlen -= (int)sizeof(*ip); hlen > 0; ) {
1534                 opt = *s;
1535                 if (opt == '\0')
1536                         break;
1537                 else if (opt == IPOPT_NOP)
1538                         ol = 1;
1539                 else {
1540                         if (hlen < 2)
1541                                 break;
1542                         ol = (int)*(s + 1);
1543                         if (ol < 2 || ol > hlen)
1544                                 break;
1545                 }
1546                 for (i = 9, mv = 4; mv >= 0; ) {
1547                         op = ipopts + i;
1548                         if ((opt == (u_char)op->ol_val) && (ol > 4)) {
1549                                 optmsk |= op->ol_bit;
1550                                 if (opt == IPOPT_SECURITY) {
1551                                         const struct optlist *sp;
1552                                         u_char  sec;
1553                                         int j, m;
1554
1555                                         sec = *(s + 2); /* classification */
1556                                         for (j = 3, m = 2; m >= 0; ) {
1557                                                 sp = secopt + j;
1558                                                 if (sec == sp->ol_val) {
1559                                                         secmsk |= sp->ol_bit;
1560                                                         auth = *(s + 3);
1561                                                         auth *= 256;
1562                                                         auth += *(s + 4);
1563                                                         break;
1564                                                 }
1565                                                 if (sec < sp->ol_val)
1566                                                         j -= m;
1567                                                 else
1568                                                         j += m;
1569                                                 m--;
1570                                         }
1571                                 }
1572                                 break;
1573                         }
1574                         if (opt < op->ol_val)
1575                                 i -= mv;
1576                         else
1577                                 i += mv;
1578                         mv--;
1579                 }
1580                 hlen -= ol;
1581                 s += ol;
1582         }
1583
1584         /*
1585          *
1586          */
1587         if (auth && !(auth & 0x0100))
1588                 auth &= 0xff00;
1589         fi->fi_optmsk = optmsk;
1590         fi->fi_secmsk = secmsk;
1591         fi->fi_auth = auth;
1592 }
1593
1594
1595 /* ------------------------------------------------------------------------ */
1596 /* Function:    fr_makefrip                                                 */
1597 /* Returns:     void                                                        */
1598 /* Parameters:  hlen(I) - length of IP packet header                        */
1599 /*              ip(I)   - pointer to the IP header                          */
1600 /*              fin(IO) - pointer to packet information                     */
1601 /*                                                                          */
1602 /* Compact the IP header into a structure which contains just the info.     */
1603 /* which is useful for comparing IP headers with and store this information */
1604 /* in the fr_info_t structure pointer to by fin.  At present, it is assumed */
1605 /* this function will be called with either an IPv4 or IPv6 packet.         */
1606 /* ------------------------------------------------------------------------ */
1607 int     fr_makefrip(hlen, ip, fin)
1608 int hlen;
1609 ip_t *ip;
1610 fr_info_t *fin;
1611 {
1612         int v;
1613
1614         fin->fin_nat = NULL;
1615         fin->fin_state = NULL;
1616         fin->fin_depth = 0;
1617         fin->fin_hlen = (u_short)hlen;
1618         fin->fin_ip = ip;
1619         fin->fin_rule = 0xffffffff;
1620         fin->fin_group[0] = -1;
1621         fin->fin_group[1] = '\0';
1622         fin->fin_dp = (char *)ip + hlen;
1623
1624         v = fin->fin_v;
1625         if (v == 4) {
1626                 fin->fin_plen = ip->ip_len;
1627                 fin->fin_dlen = fin->fin_plen - hlen;
1628
1629                 frpr_ipv4hdr(fin);
1630 #ifdef  USE_INET6
1631         } else if (v == 6) {
1632                 fin->fin_plen = ntohs(((ip6_t *)ip)->ip6_plen);
1633                 fin->fin_dlen = fin->fin_plen;
1634                 fin->fin_plen += hlen;
1635
1636                 if (frpr_ipv6hdr(fin) == -1)
1637                         return -1;
1638 #endif
1639         }
1640         if (fin->fin_ip == NULL)
1641                 return -1;
1642         return 0;
1643 }
1644
1645
1646 /* ------------------------------------------------------------------------ */
1647 /* Function:    fr_portcheck                                                */
1648 /* Returns:     int - 1 == port matched, 0 == port match failed             */
1649 /* Parameters:  frp(I) - pointer to port check `expression'                 */
1650 /*              pop(I) - pointer to port number to evaluate                 */
1651 /*                                                                          */
1652 /* Perform a comparison of a port number against some other(s), using a     */
1653 /* structure with compare information stored in it.                         */
1654 /* ------------------------------------------------------------------------ */
1655 static INLINE int fr_portcheck(frp, pop)
1656 frpcmp_t *frp;
1657 u_short *pop;
1658 {
1659         u_short tup, po;
1660         int err = 1;
1661
1662         tup = *pop;
1663         po = frp->frp_port;
1664
1665         /*
1666          * Do opposite test to that required and continue if that succeeds.
1667          */
1668         switch (frp->frp_cmp)
1669         {
1670         case FR_EQUAL :
1671                 if (tup != po) /* EQUAL */
1672                         err = 0;
1673                 break;
1674         case FR_NEQUAL :
1675                 if (tup == po) /* NOTEQUAL */
1676                         err = 0;
1677                 break;
1678         case FR_LESST :
1679                 if (tup >= po) /* LESSTHAN */
1680                         err = 0;
1681                 break;
1682         case FR_GREATERT :
1683                 if (tup <= po) /* GREATERTHAN */
1684                         err = 0;
1685                 break;
1686         case FR_LESSTE :
1687                 if (tup > po) /* LT or EQ */
1688                         err = 0;
1689                 break;
1690         case FR_GREATERTE :
1691                 if (tup < po) /* GT or EQ */
1692                         err = 0;
1693                 break;
1694         case FR_OUTRANGE :
1695                 if (tup >= po && tup <= frp->frp_top) /* Out of range */
1696                         err = 0;
1697                 break;
1698         case FR_INRANGE :
1699                 if (tup <= po || tup >= frp->frp_top) /* In range */
1700                         err = 0;
1701                 break;
1702         case FR_INCRANGE :
1703                 if (tup < po || tup > frp->frp_top) /* Inclusive range */
1704                         err = 0;
1705                 break;
1706         default :
1707                 break;
1708         }
1709         return err;
1710 }
1711
1712
1713 /* ------------------------------------------------------------------------ */
1714 /* Function:    fr_tcpudpchk                                                */
1715 /* Returns:     int - 1 == protocol matched, 0 == check failed              */
1716 /* Parameters:  fin(I) - pointer to packet information                      */
1717 /*              ft(I)  - pointer to structure with comparison data          */
1718 /*                                                                          */
1719 /* Compares the current pcket (assuming it is TCP/UDP) information with a   */
1720 /* structure containing information that we want to match against.          */
1721 /* ------------------------------------------------------------------------ */
1722 int fr_tcpudpchk(fin, ft)
1723 fr_info_t *fin;
1724 frtuc_t *ft;
1725 {
1726         int err = 1;
1727
1728         /*
1729          * Both ports should *always* be in the first fragment.
1730          * So far, I cannot find any cases where they can not be.
1731          *
1732          * compare destination ports
1733          */
1734         if (ft->ftu_dcmp)
1735                 err = fr_portcheck(&ft->ftu_dst, &fin->fin_dport);
1736
1737         /*
1738          * compare source ports
1739          */
1740         if (err && ft->ftu_scmp)
1741                 err = fr_portcheck(&ft->ftu_src, &fin->fin_sport);
1742
1743         /*
1744          * If we don't have all the TCP/UDP header, then how can we
1745          * expect to do any sort of match on it ?  If we were looking for
1746          * TCP flags, then NO match.  If not, then match (which should
1747          * satisfy the "short" class too).
1748          */
1749         if (err && (fin->fin_p == IPPROTO_TCP)) {
1750                 if (fin->fin_flx & FI_SHORT)
1751                         return !(ft->ftu_tcpf | ft->ftu_tcpfm);
1752                 /*
1753                  * Match the flags ?  If not, abort this match.
1754                  */
1755                 if (ft->ftu_tcpfm &&
1756                     ft->ftu_tcpf != (fin->fin_tcpf & ft->ftu_tcpfm)) {
1757                         FR_DEBUG(("f. %#x & %#x != %#x\n", fin->fin_tcpf,
1758                                  ft->ftu_tcpfm, ft->ftu_tcpf));
1759                         err = 0;
1760                 }
1761         }
1762         return err;
1763 }
1764
1765
1766
1767 /* ------------------------------------------------------------------------ */
1768 /* Function:    fr_ipfcheck                                                 */
1769 /* Returns:     int - 0 == match, 1 == no match                             */
1770 /* Parameters:  fin(I)     - pointer to packet information                  */
1771 /*              fr(I)      - pointer to filter rule                         */
1772 /*              portcmp(I) - flag indicating whether to attempt matching on */
1773 /*                           TCP/UDP port data.                             */
1774 /*                                                                          */
1775 /* Check to see if a packet matches an IPFilter rule.  Checks of addresses, */
1776 /* port numbers, etc, for "standard" IPFilter rules are all orchestrated in */
1777 /* this function.                                                           */
1778 /* ------------------------------------------------------------------------ */
1779 static INLINE int fr_ipfcheck(fin, fr, portcmp)
1780 fr_info_t *fin;
1781 frentry_t *fr;
1782 int portcmp;
1783 {
1784         u_32_t  *ld, *lm, *lip;
1785         fripf_t *fri;
1786         fr_ip_t *fi;
1787         int i;
1788
1789         fi = &fin->fin_fi;
1790         fri = fr->fr_ipf;
1791         lip = (u_32_t *)fi;
1792         lm = (u_32_t *)&fri->fri_mip;
1793         ld = (u_32_t *)&fri->fri_ip;
1794
1795         /*
1796          * first 32 bits to check coversion:
1797          * IP version, TOS, TTL, protocol
1798          */
1799         i = ((*lip & *lm) != *ld);
1800         FR_DEBUG(("0. %#08x & %#08x != %#08x\n",
1801                    ntohl(*lip), ntohl(*lm), ntohl(*ld)));
1802         if (i)
1803                 return 1;
1804
1805         /*
1806          * Next 32 bits is a constructed bitmask indicating which IP options
1807          * are present (if any) in this packet.
1808          */
1809         lip++, lm++, ld++;
1810         i |= ((*lip & *lm) != *ld);
1811         FR_DEBUG(("1. %#08x & %#08x != %#08x\n",
1812                    ntohl(*lip), ntohl(*lm), ntohl(*ld)));
1813         if (i)
1814                 return 1;
1815
1816         lip++, lm++, ld++;
1817         /*
1818          * Unrolled loops (4 each, for 32 bits) for address checks.
1819          */
1820         /*
1821          * Check the source address.
1822          */
1823 #ifdef  IPFILTER_LOOKUP
1824         if (fr->fr_satype == FRI_LOOKUP) {
1825                 i = (*fr->fr_srcfunc)(fr->fr_srcptr, fi->fi_v, lip);
1826                 if (i == -1)
1827                         return 1;
1828                 lip += 3;
1829                 lm += 3;
1830                 ld += 3;
1831         } else {
1832 #endif
1833                 i = ((*lip & *lm) != *ld);
1834                 FR_DEBUG(("2a. %#08x & %#08x != %#08x\n",
1835                            ntohl(*lip), ntohl(*lm), ntohl(*ld)));
1836                 if (fi->fi_v == 6) {
1837                         lip++, lm++, ld++;
1838                         i |= ((*lip & *lm) != *ld);
1839                         FR_DEBUG(("2b. %#08x & %#08x != %#08x\n",
1840                                    ntohl(*lip), ntohl(*lm), ntohl(*ld)));
1841                         lip++, lm++, ld++;
1842                         i |= ((*lip & *lm) != *ld);
1843                         FR_DEBUG(("2c. %#08x & %#08x != %#08x\n",
1844                                    ntohl(*lip), ntohl(*lm), ntohl(*ld)));
1845                         lip++, lm++, ld++;
1846                         i |= ((*lip & *lm) != *ld);
1847                         FR_DEBUG(("2d. %#08x & %#08x != %#08x\n",
1848                                    ntohl(*lip), ntohl(*lm), ntohl(*ld)));
1849                 } else {
1850                         lip += 3;
1851                         lm += 3;
1852                         ld += 3;
1853                 }
1854 #ifdef  IPFILTER_LOOKUP
1855         }
1856 #endif
1857         i ^= (fr->fr_flags & FR_NOTSRCIP) >> 6;
1858         if (i)
1859                 return 1;
1860
1861         /*
1862          * Check the destination address.
1863          */
1864         lip++, lm++, ld++;
1865 #ifdef  IPFILTER_LOOKUP
1866         if (fr->fr_datype == FRI_LOOKUP) {
1867                 i = (*fr->fr_dstfunc)(fr->fr_dstptr, fi->fi_v, lip);
1868                 if (i == -1)
1869                         return 1;
1870                 lip += 3;
1871                 lm += 3;
1872                 ld += 3;
1873         } else {
1874 #endif
1875                 i = ((*lip & *lm) != *ld);
1876                 FR_DEBUG(("3a. %#08x & %#08x != %#08x\n",
1877                            ntohl(*lip), ntohl(*lm), ntohl(*ld)));
1878                 if (fi->fi_v == 6) {
1879                         lip++, lm++, ld++;
1880                         i |= ((*lip & *lm) != *ld);
1881                         FR_DEBUG(("3b. %#08x & %#08x != %#08x\n",
1882                                    ntohl(*lip), ntohl(*lm), ntohl(*ld)));
1883                         lip++, lm++, ld++;
1884                         i |= ((*lip & *lm) != *ld);
1885                         FR_DEBUG(("3c. %#08x & %#08x != %#08x\n",
1886                                    ntohl(*lip), ntohl(*lm), ntohl(*ld)));
1887                         lip++, lm++, ld++;
1888                         i |= ((*lip & *lm) != *ld);
1889                         FR_DEBUG(("3d. %#08x & %#08x != %#08x\n",
1890                                    ntohl(*lip), ntohl(*lm), ntohl(*ld)));
1891                 } else {
1892                         lip += 3;
1893                         lm += 3;
1894                         ld += 3;
1895                 }
1896 #ifdef  IPFILTER_LOOKUP
1897         }
1898 #endif
1899         i ^= (fr->fr_flags & FR_NOTDSTIP) >> 7;
1900         if (i)
1901                 return 1;
1902         /*
1903          * IP addresses matched.  The next 32bits contains:
1904          * mast of old IP header security & authentication bits.
1905          */
1906         lip++, lm++, ld++;
1907         i |= ((*lip & *lm) != *ld);
1908         FR_DEBUG(("4. %#08x & %#08x != %#08x\n",
1909                    *lip, *lm, *ld));
1910
1911         /*
1912          * Next we have 32 bits of packet flags.
1913          */
1914         lip++, lm++, ld++;
1915         i |= ((*lip & *lm) != *ld);
1916         FR_DEBUG(("5. %#08x & %#08x != %#08x\n",
1917                    *lip, *lm, *ld));
1918
1919         if (i == 0) {
1920                 /*
1921                  * If a fragment, then only the first has what we're
1922                  * looking for here...
1923                  */
1924                 if (portcmp) {
1925                         if (!fr_tcpudpchk(fin, &fr->fr_tuc))
1926                                 i = 1;
1927                 } else {
1928                         if (fr->fr_dcmp || fr->fr_scmp ||
1929                             fr->fr_tcpf || fr->fr_tcpfm)
1930                                 i = 1;
1931                         if (fr->fr_icmpm || fr->fr_icmp) {
1932                                 if (((fi->fi_p != IPPROTO_ICMP) &&
1933                                      (fi->fi_p != IPPROTO_ICMPV6)) ||
1934                                     fin->fin_off || (fin->fin_dlen < 2))
1935                                         i = 1;
1936                                 else if ((fin->fin_data[0] & fr->fr_icmpm) !=
1937                                          fr->fr_icmp) {
1938                                         FR_DEBUG(("i. %#x & %#x != %#x\n",
1939                                                  fin->fin_data[0],
1940                                                  fr->fr_icmpm, fr->fr_icmp));
1941                                         i = 1;
1942                                 }
1943                         }
1944                 }
1945         }
1946         return i;
1947 }
1948
1949
1950 /* ------------------------------------------------------------------------ */
1951 /* Function:    fr_scanlist                                                 */
1952 /* Returns:     int - result flags of scanning filter list                  */
1953 /* Parameters:  fin(I) - pointer to packet information                      */
1954 /*              pass(I) - default result to return for filtering            */
1955 /*                                                                          */
1956 /* Check the input/output list of rules for a match to the current packet.  */
1957 /* If a match is found, the value of fr_flags from the rule becomes the     */
1958 /* return value and fin->fin_fr points to the matched rule.                 */
1959 /*                                                                          */
1960 /* This function may be called recusively upto 16 times (limit inbuilt.)    */
1961 /* When unwinding, it should finish up with fin_depth as 0.                 */
1962 /*                                                                          */
1963 /* Could be per interface, but this gets real nasty when you don't have,    */
1964 /* or can't easily change, the kernel source code to .                      */
1965 /* ------------------------------------------------------------------------ */
1966 int fr_scanlist(fin, pass)
1967 fr_info_t *fin;
1968 u_32_t pass;
1969 {
1970         int rulen, portcmp, off, skip;
1971         struct frentry *fr, *fnext;
1972         u_32_t passt, passo;
1973
1974         /*
1975          * Do not allow nesting deeper than 16 levels.
1976          */
1977         if (fin->fin_depth >= 16)
1978                 return pass;
1979
1980         fr = fin->fin_fr;
1981
1982         /*
1983          * If there are no rules in this list, return now.
1984          */
1985         if (fr == NULL)
1986                 return pass;
1987
1988         skip = 0;
1989         portcmp = 0;
1990         fin->fin_depth++;
1991         fin->fin_fr = NULL;
1992         off = fin->fin_off;
1993
1994         if ((fin->fin_flx & FI_TCPUDP) && (fin->fin_dlen > 3) && !off)
1995                 portcmp = 1;
1996
1997         for (rulen = 0; fr; fr = fnext, rulen++) {
1998                 fnext = fr->fr_next;
1999                 if (skip != 0) {
2000                         FR_VERBOSE(("%d (%#x)\n", skip, fr->fr_flags));
2001                         skip--;
2002                         continue;
2003                 }
2004
2005                 /*
2006                  * In all checks below, a null (zero) value in the
2007                  * filter struture is taken to mean a wildcard.
2008                  *
2009                  * check that we are working for the right interface
2010                  */
2011 #ifdef  _KERNEL
2012                 if (fr->fr_ifa && fr->fr_ifa != fin->fin_ifp)
2013                         continue;
2014 #else
2015                 if (opts & (OPT_VERBOSE|OPT_DEBUG))
2016                         printf("\n");
2017                 FR_VERBOSE(("%c", FR_ISSKIP(pass) ? 's' :
2018                                   FR_ISPASS(pass) ? 'p' :
2019                                   FR_ISACCOUNT(pass) ? 'A' :
2020                                   FR_ISAUTH(pass) ? 'a' :
2021                                   (pass & FR_NOMATCH) ? 'n' :'b'));
2022                 if (fr->fr_ifa && fr->fr_ifa != fin->fin_ifp)
2023                         continue;
2024                 FR_VERBOSE((":i"));
2025 #endif
2026
2027                 switch (fr->fr_type)
2028                 {
2029                 case FR_T_IPF :
2030                 case FR_T_IPF|FR_T_BUILTIN :
2031                         if (fr_ipfcheck(fin, fr, portcmp))
2032                                 continue;
2033                         break;
2034 #if defined(IPFILTER_BPF)
2035                 case FR_T_BPFOPC :
2036                 case FR_T_BPFOPC|FR_T_BUILTIN :
2037                     {
2038                         u_char *mc;
2039
2040                         if (*fin->fin_mp == NULL)
2041                                 continue;
2042                         if (fin->fin_v != fr->fr_v)
2043                                 continue;
2044                         mc = (u_char *)fin->fin_m;
2045                         if (!bpf_filter(fr->fr_data, mc, fin->fin_plen, 0))
2046                                 continue;
2047                         break;
2048                     }
2049 #endif
2050                 case FR_T_CALLFUNC|FR_T_BUILTIN :
2051                     {
2052                         frentry_t *f;
2053
2054                         f = (*fr->fr_func)(fin, &pass);
2055                         if (f != NULL)
2056                                 fr = f;
2057                         else
2058                                 continue;
2059                         break;
2060                     }
2061                 default :
2062                         break;
2063                 }
2064
2065                 if ((fin->fin_out == 0) && (fr->fr_nattag.ipt_num[0] != 0)) {
2066                         if (fin->fin_nattag == NULL)
2067                                 continue;
2068                         if (fr_matchtag(&fr->fr_nattag, fin->fin_nattag) == 0)
2069                                 continue;
2070                 }
2071                 FR_VERBOSE(("=%s.%d *", fr->fr_group, rulen));
2072
2073                 passt = fr->fr_flags;
2074
2075                 /*
2076                  * Allowing a rule with the "keep state" flag set to match
2077                  * packets that have been tagged "out of window" by the TCP
2078                  * state tracking is foolish as the attempt to add a new
2079                  * state entry to the table will fail.
2080                  */
2081                 if ((passt & FR_KEEPSTATE) && (fin->fin_flx & FI_OOW))
2082                         continue;
2083
2084                 /*
2085                  * If the rule is a "call now" rule, then call the function
2086                  * in the rule, if it exists and use the results from that.
2087                  * If the function pointer is bad, just make like we ignore
2088                  * it, except for increasing the hit counter.
2089                  */
2090                 if ((passt & FR_CALLNOW) != 0) {
2091                         frentry_t *frs;
2092
2093                         ATOMIC_INC64(fr->fr_hits);
2094                         if ((fr->fr_func != NULL) &&
2095                             (fr->fr_func == (ipfunc_t)-1))
2096                                 continue;
2097
2098                         frs = fin->fin_fr;
2099                         fin->fin_fr = fr;
2100                         fr = (*fr->fr_func)(fin, &passt);
2101                         if (fr == NULL) {
2102                                 fin->fin_fr = frs;
2103                                 continue;
2104                         }
2105                         passt = fr->fr_flags;
2106                 }
2107                 fin->fin_fr = fr;
2108
2109 #ifdef  IPFILTER_LOG
2110                 /*
2111                  * Just log this packet...
2112                  */
2113                 if ((passt & FR_LOGMASK) == FR_LOG) {
2114                         if (ipflog(fin, passt) == -1) {
2115                                 if (passt & FR_LOGORBLOCK) {
2116                                         passt &= ~FR_CMDMASK;
2117                                         passt |= FR_BLOCK|FR_QUICK;
2118                                 }
2119                                 ATOMIC_INCL(frstats[fin->fin_out].fr_skip);
2120                         }
2121                         ATOMIC_INCL(frstats[fin->fin_out].fr_pkl);
2122                         fin->fin_flx |= FI_DONTCACHE;
2123                 }
2124 #endif /* IPFILTER_LOG */
2125                 fr->fr_bytes += (U_QUAD_T)fin->fin_plen;
2126                 passo = pass;
2127                 if (FR_ISSKIP(passt))
2128                         skip = fr->fr_arg;
2129                 else if ((passt & FR_LOGMASK) != FR_LOG)
2130                         pass = passt;
2131                 if (passt & (FR_RETICMP|FR_FAKEICMP))
2132                         fin->fin_icode = fr->fr_icode;
2133                 FR_DEBUG(("pass %#x\n", pass));
2134                 ATOMIC_INC64(fr->fr_hits);
2135                 fin->fin_rule = rulen;
2136                 (void) strncpy(fin->fin_group, fr->fr_group, FR_GROUPLEN);
2137                 if (fr->fr_grp != NULL) {
2138                         fin->fin_fr = *fr->fr_grp;
2139                         passt = fr_scanlist(fin, pass);
2140                         if (fin->fin_fr == NULL) {
2141                                 fin->fin_rule = rulen;
2142                                 (void) strncpy(fin->fin_group, fr->fr_group,
2143                                                FR_GROUPLEN);
2144                                 fin->fin_fr = fr;
2145                                 passt = pass;
2146                         }
2147                         pass = passt;
2148                 }
2149
2150                 if (passt & FR_QUICK) {
2151                         /*
2152                          * Finally, if we've asked to track state for this
2153                          * packet, set it up.  Add state for "quick" rules
2154                          * here so that if the action fails we can consider
2155                          * the rule to "not match" and keep on processing
2156                          * filter rules.
2157                          */
2158                         if ((pass & FR_KEEPSTATE) &&
2159                             !(fin->fin_flx & FI_STATE)) {
2160                                 int out = fin->fin_out;
2161
2162                                 fin->fin_fr = fr;
2163                                 if (fr_addstate(fin, NULL, 0) != NULL) {
2164                                         ATOMIC_INCL(frstats[out].fr_ads);
2165                                 } else {
2166                                         ATOMIC_INCL(frstats[out].fr_bads);
2167                                         pass = passo;
2168                                         continue;
2169                                 }
2170                         }
2171                         break;
2172                 }
2173         }
2174         fin->fin_depth--;
2175         return pass;
2176 }
2177
2178
2179 /* ------------------------------------------------------------------------ */
2180 /* Function:    fr_acctpkt                                                  */
2181 /* Returns:     frentry_t* - always returns NULL                            */
2182 /* Parameters:  fin(I) - pointer to packet information                      */
2183 /*              passp(IO) - pointer to current/new filter decision (unused) */
2184 /*                                                                          */
2185 /* Checks a packet against accounting rules, if there are any for the given */
2186 /* IP protocol version.                                                     */
2187 /*                                                                          */
2188 /* N.B.: this function returns NULL to match the prototype used by other    */
2189 /* functions called from the IPFilter "mainline" in fr_check().             */
2190 /* ------------------------------------------------------------------------ */
2191 frentry_t *fr_acctpkt(fin, passp)
2192 fr_info_t *fin;
2193 u_32_t *passp;
2194 {
2195         char group[FR_GROUPLEN];
2196         frentry_t *fr, *frsave;
2197         u_32_t pass, rulen;
2198
2199         passp = passp;
2200 #ifdef  USE_INET6
2201         if (fin->fin_v == 6)
2202                 fr = ipacct6[fin->fin_out][fr_active];
2203         else
2204 #endif
2205                 fr = ipacct[fin->fin_out][fr_active];
2206
2207         if (fr != NULL) {
2208                 frsave = fin->fin_fr;
2209                 bcopy(fin->fin_group, group, FR_GROUPLEN);
2210                 rulen = fin->fin_rule;
2211                 fin->fin_fr = fr;
2212                 pass = fr_scanlist(fin, FR_NOMATCH);
2213                 if (FR_ISACCOUNT(pass)) {
2214                         ATOMIC_INCL(frstats[0].fr_acct);
2215                 }
2216                 fin->fin_fr = frsave;
2217                 bcopy(group, fin->fin_group, FR_GROUPLEN);
2218                 fin->fin_rule = rulen;
2219         }
2220         return NULL;
2221 }
2222
2223
2224 /* ------------------------------------------------------------------------ */
2225 /* Function:    fr_firewall                                                 */
2226 /* Returns:     frentry_t* - returns pointer to matched rule, if no matches */
2227 /*                           were found, returns NULL.                      */
2228 /* Parameters:  fin(I) - pointer to packet information                      */
2229 /*              passp(IO) - pointer to current/new filter decision (unused) */
2230 /*                                                                          */
2231 /* Applies an appropriate set of firewall rules to the packet, to see if    */
2232 /* there are any matches.  The first check is to see if a match can be seen */
2233 /* in the cache.  If not, then search an appropriate list of rules.  Once a */
2234 /* matching rule is found, take any appropriate actions as defined by the   */
2235 /* rule - except logging.                                                   */
2236 /* ------------------------------------------------------------------------ */
2237 static frentry_t *fr_firewall(fin, passp)
2238 fr_info_t *fin;
2239 u_32_t *passp;
2240 {
2241         frentry_t *fr;
2242         fr_info_t *fc;
2243         u_32_t pass;
2244         int out;
2245
2246         out = fin->fin_out;
2247         pass = *passp;
2248
2249         /*
2250          * If a packet is found in the auth table, then skip checking
2251          * the access lists for permission but we do need to consider
2252          * the result as if it were from the ACL's.
2253          */
2254         fc = &frcache[out][CACHE_HASH(fin)];
2255         READ_ENTER(&ipf_frcache);
2256         if (!bcmp((char *)fin, (char *)fc, FI_CSIZE)) {
2257                 /*
2258                  * copy cached data so we can unlock the mutexes earlier.
2259                  */
2260                 bcopy((char *)fc, (char *)fin, FI_COPYSIZE);
2261                 RWLOCK_EXIT(&ipf_frcache);
2262                 ATOMIC_INCL(frstats[out].fr_chit);
2263
2264                 if ((fr = fin->fin_fr) != NULL) {
2265                         ATOMIC_INC64(fr->fr_hits);
2266                         pass = fr->fr_flags;
2267                 }
2268         } else {
2269                 RWLOCK_EXIT(&ipf_frcache);
2270
2271 #ifdef  USE_INET6
2272                 if (fin->fin_v == 6)
2273                         fin->fin_fr = ipfilter6[out][fr_active];
2274                 else
2275 #endif
2276                         fin->fin_fr = ipfilter[out][fr_active];
2277                 if (fin->fin_fr != NULL)
2278                         pass = fr_scanlist(fin, fr_pass);
2279
2280                 if (((pass & FR_KEEPSTATE) == 0) &&
2281                     ((fin->fin_flx & FI_DONTCACHE) == 0)) {
2282                         WRITE_ENTER(&ipf_frcache);
2283                         bcopy((char *)fin, (char *)fc, FI_COPYSIZE);
2284                         RWLOCK_EXIT(&ipf_frcache);
2285                 }
2286                 if ((pass & FR_NOMATCH)) {
2287                         ATOMIC_INCL(frstats[out].fr_nom);
2288                 }
2289                 fr = fin->fin_fr;
2290         }
2291
2292         /*
2293          * Apply packets per second rate-limiting to a rule as required.
2294          */
2295         if ((fr != NULL) && (fr->fr_pps != 0) &&
2296             !ppsratecheck(&fr->fr_lastpkt, &fr->fr_curpps, fr->fr_pps)) {
2297                 pass &= ~(FR_CMDMASK|FR_DUP|FR_RETICMP|FR_RETRST);
2298                 pass |= FR_BLOCK;
2299                 ATOMIC_INCL(frstats[out].fr_ppshit);
2300         }
2301
2302         /*
2303          * If we fail to add a packet to the authorization queue, then we
2304          * drop the packet later.  However, if it was added then pretend
2305          * we've dropped it already.
2306          */
2307         if (FR_ISAUTH(pass)) {
2308                 if (fr_newauth(fin->fin_m, fin) != 0) {
2309 #ifdef  _KERNEL
2310                         fin->fin_m = *fin->fin_mp = NULL;
2311 #else
2312                         ;
2313 #endif
2314                         fin->fin_error = 0;
2315                 } else
2316                         fin->fin_error = ENOSPC;
2317         }
2318
2319         if ((fr != NULL) && (fr->fr_func != NULL) &&
2320             (fr->fr_func != (ipfunc_t)-1) && !(pass & FR_CALLNOW))
2321                 (void) (*fr->fr_func)(fin, &pass);
2322
2323         /*
2324          * If a rule is a pre-auth rule, check again in the list of rules
2325          * loaded for authenticated use.  It does not particulary matter
2326          * if this search fails because a "preauth" result, from a rule,
2327          * is treated as "not a pass", hence the packet is blocked.
2328          */
2329         if (FR_ISPREAUTH(pass)) {
2330                 if ((fin->fin_fr = ipauth) != NULL)
2331                         pass = fr_scanlist(fin, fr_pass);
2332         }
2333
2334         /*
2335          * If the rule has "keep frag" and the packet is actually a fragment,
2336          * then create a fragment state entry.
2337          */
2338         if ((pass & (FR_KEEPFRAG|FR_KEEPSTATE)) == FR_KEEPFRAG) {
2339                 if (fin->fin_flx & FI_FRAG) {
2340                         if (fr_newfrag(fin, pass) == -1) {
2341                                 ATOMIC_INCL(frstats[out].fr_bnfr);
2342                         } else {
2343                                 ATOMIC_INCL(frstats[out].fr_nfr);
2344                         }
2345                 } else {
2346                         ATOMIC_INCL(frstats[out].fr_cfr);
2347                 }
2348         }
2349
2350         fr = fin->fin_fr;
2351
2352         if (passp != NULL)
2353                 *passp = pass;
2354
2355         return fr;
2356 }
2357
2358
2359 /* ------------------------------------------------------------------------ */
2360 /* Function:    fr_check                                                    */
2361 /* Returns:     int -  0 == packet allowed through,                         */
2362 /*              User space:                                                 */
2363 /*                    -1 == packet blocked                                  */
2364 /*                     1 == packet not matched                              */
2365 /*                    -2 == requires authentication                         */
2366 /*              Kernel:                                                     */
2367 /*                   > 0 == filter error # for packet                       */
2368 /* Parameters: ip(I)   - pointer to start of IPv4/6 packet                  */
2369 /*             hlen(I) - length of header                                   */
2370 /*             ifp(I)  - pointer to interface this packet is on             */
2371 /*             out(I)  - 0 == packet going in, 1 == packet going out        */
2372 /*             mp(IO)  - pointer to caller's buffer pointer that holds this */
2373 /*                       IP packet.                                         */
2374 /* Solaris & HP-UX ONLY :                                                   */
2375 /*             qpi(I)  - pointer to STREAMS queue information for this      */
2376 /*                       interface & direction.                             */
2377 /*                                                                          */
2378 /* fr_check() is the master function for all IPFilter packet processing.    */
2379 /* It orchestrates: Network Address Translation (NAT), checking for packet  */
2380 /* authorisation (or pre-authorisation), presence of related state info.,   */
2381 /* generating log entries, IP packet accounting, routing of packets as      */
2382 /* directed by firewall rules and of course whether or not to allow the     */
2383 /* packet to be further processed by the kernel.                            */
2384 /*                                                                          */
2385 /* For packets blocked, the contents of "mp" will be NULL'd and the buffer  */
2386 /* freed.  Packets passed may be returned with the pointer pointed to by    */
2387 /* by "mp" changed to a new buffer.                                         */
2388 /* ------------------------------------------------------------------------ */
2389 int fr_check(ip, hlen, ifp, out
2390 #if defined(_KERNEL) && defined(MENTAT)
2391 , qif, mp)
2392 void *qif;
2393 #else
2394 , mp)
2395 #endif
2396 mb_t **mp;
2397 ip_t *ip;
2398 int hlen;
2399 void *ifp;
2400 int out;
2401 {
2402         /*
2403          * The above really sucks, but short of writing a diff
2404          */
2405         fr_info_t frinfo;
2406         fr_info_t *fin = &frinfo;
2407         u_32_t pass = fr_pass;
2408         frentry_t *fr = NULL;
2409         int v = IP_V(ip);
2410         mb_t *mc = NULL;
2411         mb_t *m;
2412         /*
2413          * The first part of fr_check() deals with making sure that what goes
2414          * into the filtering engine makes some sense.  Information about the
2415          * the packet is distilled, collected into a fr_info_t structure and
2416          * the an attempt to ensure the buffer the packet is in is big enough
2417          * to hold all the required packet headers.
2418          */
2419 #ifdef  _KERNEL
2420 # ifdef MENTAT
2421         qpktinfo_t *qpi = qif;
2422
2423 #  if !defined(_INET_IP_STACK_H)
2424         if ((u_int)ip & 0x3)
2425                 return 2;
2426 #  endif
2427 # else
2428         SPL_INT(s);
2429 # endif
2430
2431         READ_ENTER(&ipf_global);
2432
2433         if (fr_running <= 0) {
2434                 RWLOCK_EXIT(&ipf_global);
2435                 return 0;
2436         }
2437
2438         bzero((char *)fin, sizeof(*fin));
2439
2440 # ifdef MENTAT
2441         if (qpi->qpi_flags & QF_GROUP)
2442                 fin->fin_flx |= FI_MBCAST;
2443         m = qpi->qpi_m;
2444         fin->fin_qfm = m;
2445         fin->fin_qpi = qpi;
2446 # else /* MENTAT */
2447
2448         m = *mp;
2449
2450 #  if defined(M_MCAST)
2451         if ((m->m_flags & M_MCAST) != 0)
2452                 fin->fin_flx |= FI_MBCAST|FI_MULTICAST;
2453 #  endif
2454 #  if defined(M_MLOOP)
2455         if ((m->m_flags & M_MLOOP) != 0)
2456                 fin->fin_flx |= FI_MBCAST|FI_MULTICAST;
2457 #  endif
2458 #  if defined(M_BCAST)
2459         if ((m->m_flags & M_BCAST) != 0)
2460                 fin->fin_flx |= FI_MBCAST|FI_BROADCAST;
2461 #  endif
2462 #  ifdef M_CANFASTFWD
2463         /*
2464          * XXX For now, IP Filter and fast-forwarding of cached flows
2465          * XXX are mutually exclusive.  Eventually, IP Filter should
2466          * XXX get a "can-fast-forward" filter rule.
2467          */
2468         m->m_flags &= ~M_CANFASTFWD;
2469 #  endif /* M_CANFASTFWD */
2470 #  ifdef CSUM_DELAY_DATA
2471         /*
2472          * disable delayed checksums.
2473          */
2474         if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
2475                 in_delayed_cksum(m);
2476                 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
2477         }
2478 #  endif /* CSUM_DELAY_DATA */
2479 # endif /* MENTAT */
2480 #else
2481         READ_ENTER(&ipf_global);
2482
2483         bzero((char *)fin, sizeof(*fin));
2484         m = *mp;
2485 #endif /* _KERNEL */
2486
2487         fin->fin_v = v;
2488         fin->fin_m = m;
2489         fin->fin_ip = ip;
2490         fin->fin_mp = mp;
2491         fin->fin_out = out;
2492         fin->fin_ifp = ifp;
2493         fin->fin_error = ENETUNREACH;
2494         fin->fin_hlen = (u_short)hlen;
2495         fin->fin_dp = (char *)ip + hlen;
2496
2497         fin->fin_ipoff = (char *)ip - MTOD(m, char *);
2498
2499         SPL_NET(s);
2500
2501 #ifdef  USE_INET6
2502         if (v == 6) {
2503                 ATOMIC_INCL(frstats[out].fr_ipv6);
2504                 /*
2505                  * Jumbo grams are quite likely too big for internal buffer
2506                  * structures to handle comfortably, for now, so just drop
2507                  * them.
2508                  */
2509                 if (((ip6_t *)ip)->ip6_plen == 0) {
2510                         pass = FR_BLOCK|FR_NOMATCH;
2511                         goto finished;
2512                 }
2513         } else
2514 #endif
2515         {
2516 #if (defined(OpenBSD) && (OpenBSD >= 200311)) && defined(_KERNEL)
2517                 ip->ip_len = ntohs(ip->ip_len);
2518                 ip->ip_off = ntohs(ip->ip_off);
2519 #endif
2520         }
2521
2522         if (fr_makefrip(hlen, ip, fin) == -1) {
2523                 pass = FR_BLOCK|FR_NOMATCH;
2524                 goto finished;
2525         }
2526
2527         /*
2528          * For at least IPv6 packets, if a m_pullup() fails then this pointer
2529          * becomes NULL and so we have no packet to free.
2530          */
2531         if (*fin->fin_mp == NULL)
2532                 goto finished;
2533
2534         if (!out) {
2535                 if (v == 4) {
2536 #ifdef _KERNEL
2537                         if (fr_chksrc && !fr_verifysrc(fin)) {
2538                                 ATOMIC_INCL(frstats[0].fr_badsrc);
2539                                 fin->fin_flx |= FI_BADSRC;
2540                         }
2541 #endif
2542                         if (fin->fin_ip->ip_ttl < fr_minttl) {
2543                                 ATOMIC_INCL(frstats[0].fr_badttl);
2544                                 fin->fin_flx |= FI_LOWTTL;
2545                         }
2546                 }
2547 #ifdef USE_INET6
2548                 else  if (v == 6) {
2549                         if (((ip6_t *)ip)->ip6_hlim < fr_minttl) {
2550                                 ATOMIC_INCL(frstats[0].fr_badttl);
2551                                 fin->fin_flx |= FI_LOWTTL;
2552                         }
2553                 }
2554 #endif
2555         }
2556
2557         if (fin->fin_flx & FI_SHORT) {
2558                 ATOMIC_INCL(frstats[out].fr_short);
2559         }
2560
2561         READ_ENTER(&ipf_mutex);
2562
2563         /*
2564          * Check auth now.  This, combined with the check below to see if apass
2565          * is 0 is to ensure that we don't count the packet twice, which can
2566          * otherwise occur when we reprocess it.  As it is, we only count it
2567          * after it has no auth. table matchup.  This also stops NAT from
2568          * occuring until after the packet has been auth'd.
2569          */
2570         fr = fr_checkauth(fin, &pass);
2571         if (!out) {
2572                 if (fr_checknatin(fin, &pass) == -1) {
2573                         goto filterdone;
2574                 }
2575         }
2576         if (!out)
2577                 (void) fr_acctpkt(fin, NULL);
2578
2579         if (fr == NULL) {
2580                 if ((fin->fin_flx & (FI_FRAG|FI_BAD)) == FI_FRAG) {
2581                         fr = fr_knownfrag(fin, &pass);
2582                         /*
2583                          * Reset the keep state flag here so that we don't
2584                          * try and add a new state entry because of it, leading
2585                          * to a blocked packet because the add will fail.
2586                          */
2587                         if (fr != NULL)
2588                                 pass &= ~FR_KEEPSTATE;
2589                 }
2590                 if (fr == NULL)
2591                         fr = fr_checkstate(fin, &pass);
2592         }
2593
2594         if ((pass & FR_NOMATCH) || (fr == NULL))
2595                 fr = fr_firewall(fin, &pass);
2596
2597         /*
2598          * If we've asked to track state for this packet, set it up.
2599          * Here rather than fr_firewall because fr_checkauth may decide
2600          * to return a packet for "keep state"
2601          */
2602         if ((pass & FR_KEEPSTATE) && (fin->fin_m != NULL) &&
2603             !(fin->fin_flx & FI_STATE)) {
2604                 if (fr_addstate(fin, NULL, 0) != NULL) {
2605                         ATOMIC_INCL(frstats[out].fr_ads);
2606                 } else {
2607                         ATOMIC_INCL(frstats[out].fr_bads);
2608                         if (FR_ISPASS(pass)) {
2609                                 pass &= ~FR_CMDMASK;
2610                                 pass |= FR_BLOCK;
2611                         }
2612                 }
2613         }
2614
2615         fin->fin_fr = fr;
2616
2617         /*
2618          * Only count/translate packets which will be passed on, out the
2619          * interface.
2620          */
2621         if (out && FR_ISPASS(pass)) {
2622                 (void) fr_acctpkt(fin, NULL);
2623
2624                 if (fr_checknatout(fin, &pass) == -1) {
2625                         ;
2626                 } else if ((fr_update_ipid != 0) && (v == 4)) {
2627                         if (fr_updateipid(fin) == -1) {
2628                                 ATOMIC_INCL(frstats[1].fr_ipud);
2629                                 pass &= ~FR_CMDMASK;
2630                                 pass |= FR_BLOCK;
2631                         } else {
2632                                 ATOMIC_INCL(frstats[0].fr_ipud);
2633                         }
2634                 }
2635         }
2636
2637 filterdone:
2638 #ifdef  IPFILTER_LOG
2639         if ((fr_flags & FF_LOGGING) || (pass & FR_LOGMASK)) {
2640                 (void) fr_dolog(fin, &pass);
2641         }
2642 #endif
2643
2644         /*
2645          * The FI_STATE flag is cleared here so that calling fr_checkstate
2646          * will work when called from inside of fr_fastroute.  Although
2647          * there is a similar flag, FI_NATED, for NAT, it does have the same
2648          * impact on code execution.
2649          */
2650         if (fin->fin_state != NULL) {
2651                 fr_statederef((ipstate_t **)&fin->fin_state);
2652                 fin->fin_flx ^= FI_STATE;
2653         }
2654
2655         if (fin->fin_nat != NULL) {
2656                 if (FR_ISBLOCK(pass) && (fin->fin_flx & FI_NEWNAT)) {
2657                         WRITE_ENTER(&ipf_nat);
2658                         nat_delete((nat_t *)fin->fin_nat, NL_DESTROY);
2659                         RWLOCK_EXIT(&ipf_nat);
2660                         fin->fin_nat = NULL;
2661                 } else {
2662                         fr_natderef((nat_t **)&fin->fin_nat);
2663                 }
2664         }
2665
2666         /*
2667          * Up the reference on fr_lock and exit ipf_mutex.  fr_fastroute
2668          * only frees up the lock on ipf_global and the generation of a
2669          * packet below could cause a recursive call into IPFilter.
2670          * Hang onto the filter rule just in case someone decides to remove
2671          * or flush it in the meantime.
2672          */
2673         if (fr != NULL) {
2674                 MUTEX_ENTER(&fr->fr_lock);
2675                 fr->fr_ref++;
2676                 MUTEX_EXIT(&fr->fr_lock);
2677         }
2678
2679         RWLOCK_EXIT(&ipf_mutex);
2680
2681         if ((pass & FR_RETMASK) != 0) {
2682                 /*
2683                  * Should we return an ICMP packet to indicate error
2684                  * status passing through the packet filter ?
2685                  * WARNING: ICMP error packets AND TCP RST packets should
2686                  * ONLY be sent in repsonse to incoming packets.  Sending them
2687                  * in response to outbound packets can result in a panic on
2688                  * some operating systems.
2689                  */
2690                 if (!out) {
2691                         if (pass & FR_RETICMP) {
2692                                 int dst;
2693
2694                                 if ((pass & FR_RETMASK) == FR_FAKEICMP)
2695                                         dst = 1;
2696                                 else
2697                                         dst = 0;
2698                                 (void) fr_send_icmp_err(ICMP_UNREACH, fin, dst);
2699                                 ATOMIC_INCL(frstats[0].fr_ret);
2700                         } else if (((pass & FR_RETMASK) == FR_RETRST) &&
2701                                    !(fin->fin_flx & FI_SHORT)) {
2702                                 if (((fin->fin_flx & FI_OOW) != 0) ||
2703                                     (fr_send_reset(fin) == 0)) {
2704                                         ATOMIC_INCL(frstats[1].fr_ret);
2705                                 }
2706                         }
2707
2708                         /*
2709                          * When using return-* with auth rules, the auth code
2710                          * takes over disposing of this packet.
2711                          */
2712                         if (FR_ISAUTH(pass) && (fin->fin_m != NULL)) {
2713                                 fin->fin_m = *fin->fin_mp = NULL;
2714                         }
2715                 } else {
2716                         if (pass & FR_RETRST)
2717                                 fin->fin_error = ECONNRESET;
2718                 }
2719         }
2720
2721         /*
2722          * If we didn't drop off the bottom of the list of rules (and thus
2723          * the 'current' rule fr is not NULL), then we may have some extra
2724          * instructions about what to do with a packet.
2725          * Once we're finished return to our caller, freeing the packet if
2726          * we are dropping it (* BSD ONLY *).
2727          */
2728         if (fr != NULL) {
2729                 frdest_t *fdp;
2730
2731                 fdp = &fr->fr_tifs[fin->fin_rev];
2732
2733                 if (!out && (pass & FR_FASTROUTE)) {
2734                         /*
2735                          * For fastroute rule, no destioation interface defined
2736                          * so pass NULL as the frdest_t parameter
2737                          */
2738                         (void) fr_fastroute(fin->fin_m, mp, fin, NULL);
2739                         m = *mp = NULL;
2740                 } else if ((fdp->fd_ifp != NULL) &&
2741                            (fdp->fd_ifp != (struct ifnet *)-1)) {
2742                         /* this is for to rules: */
2743                         (void) fr_fastroute(fin->fin_m, mp, fin, fdp);
2744                         m = *mp = NULL;
2745                 }
2746
2747                 /*
2748                  * Generate a duplicated packet.
2749                  */
2750                 if ((pass & FR_DUP) != 0) {
2751                         mc = M_DUPLICATE(fin->fin_m);
2752                         if (mc != NULL)
2753                                 (void) fr_fastroute(mc, &mc, fin, &fr->fr_dif);
2754                 }
2755
2756                 (void) fr_derefrule(&fr);
2757         }
2758
2759 finished:
2760         if (!FR_ISPASS(pass)) {
2761                 ATOMIC_INCL(frstats[out].fr_block);
2762                 if (*mp != NULL) {
2763                         FREE_MB_T(*mp);
2764                         m = *mp = NULL;
2765                 }
2766         } else {
2767                 ATOMIC_INCL(frstats[out].fr_pass);
2768 #if defined(_KERNEL) && defined(__sgi)
2769                 if ((fin->fin_hbuf != NULL) &&
2770                     (mtod(fin->fin_m, struct ip *) != fin->fin_ip)) {
2771                         COPYBACK(fin->fin_m, 0, fin->fin_plen, fin->fin_hbuf);
2772                 }
2773 #endif
2774         }
2775
2776         SPL_X(s);
2777         RWLOCK_EXIT(&ipf_global);
2778
2779 #ifdef _KERNEL
2780 # if (defined(OpenBSD) && (OpenBSD >= 200311))
2781         if (FR_ISPASS(pass) && (v == 4)) {
2782                 ip = fin->fin_ip;
2783                 ip->ip_len = ntohs(ip->ip_len);
2784                 ip->ip_off = ntohs(ip->ip_off);
2785         }
2786 # endif
2787         return (FR_ISPASS(pass)) ? 0 : fin->fin_error;
2788 #else /* _KERNEL */
2789         FR_VERBOSE(("fin_flx %#x pass %#x ", fin->fin_flx, pass));
2790         if ((pass & FR_NOMATCH) != 0)
2791                 return 1;
2792
2793         if ((pass & FR_RETMASK) != 0)
2794                 switch (pass & FR_RETMASK)
2795                 {
2796                 case FR_RETRST :
2797                         return 3;
2798                 case FR_RETICMP :
2799                         return 4;
2800                 case FR_FAKEICMP :
2801                         return 5;
2802                 }
2803
2804         switch (pass & FR_CMDMASK)
2805         {
2806         case FR_PASS :
2807                 return 0;
2808         case FR_BLOCK :
2809                 return -1;
2810         case FR_AUTH :
2811                 return -2;
2812         case FR_ACCOUNT :
2813                 return -3;
2814         case FR_PREAUTH :
2815                 return -4;
2816         }
2817         return 2;
2818 #endif /* _KERNEL */
2819 }
2820
2821
2822 #ifdef  IPFILTER_LOG
2823 /* ------------------------------------------------------------------------ */
2824 /* Function:    fr_dolog                                                    */
2825 /* Returns:     frentry_t* - returns contents of fin_fr (no change made)    */
2826 /* Parameters:  fin(I) - pointer to packet information                      */
2827 /*              passp(IO) - pointer to current/new filter decision (unused) */
2828 /*                                                                          */
2829 /* Checks flags set to see how a packet should be logged, if it is to be    */
2830 /* logged.  Adjust statistics based on its success or not.                  */
2831 /* ------------------------------------------------------------------------ */
2832 frentry_t *fr_dolog(fin, passp)
2833 fr_info_t *fin;
2834 u_32_t *passp;
2835 {
2836         u_32_t pass;
2837         int out;
2838
2839         out = fin->fin_out;
2840         pass = *passp;
2841
2842         if ((fr_flags & FF_LOGNOMATCH) && (pass & FR_NOMATCH)) {
2843                 pass |= FF_LOGNOMATCH;
2844                 ATOMIC_INCL(frstats[out].fr_npkl);
2845                 goto logit;
2846         } else if (((pass & FR_LOGMASK) == FR_LOGP) ||
2847             (FR_ISPASS(pass) && (fr_flags & FF_LOGPASS))) {
2848                 if ((pass & FR_LOGMASK) != FR_LOGP)
2849                         pass |= FF_LOGPASS;
2850                 ATOMIC_INCL(frstats[out].fr_ppkl);
2851                 goto logit;
2852         } else if (((pass & FR_LOGMASK) == FR_LOGB) ||
2853                    (FR_ISBLOCK(pass) && (fr_flags & FF_LOGBLOCK))) {
2854                 if ((pass & FR_LOGMASK) != FR_LOGB)
2855                         pass |= FF_LOGBLOCK;
2856                 ATOMIC_INCL(frstats[out].fr_bpkl);
2857 logit:
2858                 if (ipflog(fin, pass) == -1) {
2859                         ATOMIC_INCL(frstats[out].fr_skip);
2860
2861                         /*
2862                          * If the "or-block" option has been used then
2863                          * block the packet if we failed to log it.
2864                          */
2865                         if ((pass & FR_LOGORBLOCK) &&
2866                             FR_ISPASS(pass)) {
2867                                 pass &= ~FR_CMDMASK;
2868                                 pass |= FR_BLOCK;
2869                         }
2870                 }
2871                 *passp = pass;
2872         }
2873
2874         return fin->fin_fr;
2875 }
2876 #endif /* IPFILTER_LOG */
2877
2878
2879 /* ------------------------------------------------------------------------ */
2880 /* Function:    ipf_cksum                                                   */
2881 /* Returns:     u_short - IP header checksum                                */
2882 /* Parameters:  addr(I) - pointer to start of buffer to checksum            */
2883 /*              len(I)  - length of buffer in bytes                         */
2884 /*                                                                          */
2885 /* Calculate the two's complement 16 bit checksum of the buffer passed.     */
2886 /*                                                                          */
2887 /* N.B.: addr should be 16bit aligned.                                      */
2888 /* ------------------------------------------------------------------------ */
2889 u_short ipf_cksum(addr, len)
2890 u_short *addr;
2891 int len;
2892 {
2893         u_32_t sum = 0;
2894
2895         for (sum = 0; len > 1; len -= 2)
2896                 sum += *addr++;
2897
2898         /* mop up an odd byte, if necessary */
2899         if (len == 1)
2900                 sum += *(u_char *)addr;
2901
2902         /*
2903          * add back carry outs from top 16 bits to low 16 bits
2904          */
2905         sum = (sum >> 16) + (sum & 0xffff);     /* add hi 16 to low 16 */
2906         sum += (sum >> 16);                     /* add carry */
2907         return (u_short)(~sum);
2908 }
2909
2910
2911 /* ------------------------------------------------------------------------ */
2912 /* Function:    fr_cksum                                                    */
2913 /* Returns:     u_short - layer 4 checksum                                  */
2914 /* Parameters:  m(I  )     - pointer to buffer holding packet               */
2915 /*              ip(I)      - pointer to IP header                           */
2916 /*              l4proto(I) - protocol to caclulate checksum for             */
2917 /*              l4hdr(I)   - pointer to layer 4 header                      */
2918 /*              l3len(I)   - length of layer 4 data plus layer 3 header     */
2919 /*                                                                          */
2920 /* Calculates the TCP checksum for the packet held in "m", using the data   */
2921 /* in the IP header "ip" to seed it.                                        */
2922 /*                                                                          */
2923 /* NB: This function assumes we've pullup'd enough for all of the IP header */
2924 /* and the TCP header.  We also assume that data blocks aren't allocated in */
2925 /* odd sizes.                                                               */
2926 /*                                                                          */
2927 /* For IPv6, l3len excludes extension header size.                          */
2928 /*                                                                          */
2929 /* Expects ip_len to be in host byte order when called.                     */
2930 /* ------------------------------------------------------------------------ */
2931 u_short fr_cksum(m, ip, l4proto, l4hdr, l3len)
2932 mb_t *m;
2933 ip_t *ip;
2934 int l4proto, l3len;
2935 void *l4hdr;
2936 {
2937         u_short *sp, slen, sumsave, l4hlen, *csump;
2938         u_int sum, sum2;
2939         int hlen;
2940 #ifdef  USE_INET6
2941         ip6_t *ip6;
2942 #endif
2943
2944         csump = NULL;
2945         sumsave = 0;
2946         l4hlen = 0;
2947         sp = NULL;
2948         slen = 0;
2949         hlen = 0;
2950         sum = 0;
2951
2952         /*
2953          * Add up IP Header portion
2954          */
2955 #ifdef  USE_INET6
2956         if (IP_V(ip) == 4) {
2957 #endif
2958                 hlen = IP_HL(ip) << 2;
2959                 slen = l3len - hlen;
2960                 sum = htons((u_short)l4proto);
2961                 sum += htons(slen);
2962                 sp = (u_short *)&ip->ip_src;
2963                 sum += *sp++;   /* ip_src */
2964                 sum += *sp++;
2965                 sum += *sp++;   /* ip_dst */
2966                 sum += *sp++;
2967 #ifdef  USE_INET6
2968         } else if (IP_V(ip) == 6) {
2969                 ip6 = (ip6_t *)ip;
2970                 hlen = sizeof(*ip6);
2971                 slen = l3len - hlen;
2972                 sum = htons((u_short)l4proto);
2973                 sum += htons(slen);
2974                 sp = (u_short *)&ip6->ip6_src;
2975                 sum += *sp++;   /* ip6_src */
2976                 sum += *sp++;
2977                 sum += *sp++;
2978                 sum += *sp++;
2979                 sum += *sp++;
2980                 sum += *sp++;
2981                 sum += *sp++;
2982                 sum += *sp++;
2983                 sum += *sp++;   /* ip6_dst */
2984                 sum += *sp++;
2985                 sum += *sp++;
2986                 sum += *sp++;
2987                 sum += *sp++;
2988                 sum += *sp++;
2989                 sum += *sp++;
2990                 sum += *sp++;
2991         }
2992 #endif
2993
2994         switch (l4proto)
2995         {
2996         case IPPROTO_UDP :
2997                 csump = &((udphdr_t *)l4hdr)->uh_sum;
2998                 l4hlen = sizeof(udphdr_t);
2999                 break;
3000
3001         case IPPROTO_TCP :
3002                 csump = &((tcphdr_t *)l4hdr)->th_sum;
3003                 l4hlen = sizeof(tcphdr_t);
3004                 break;
3005         case IPPROTO_ICMP :
3006                 csump = &((icmphdr_t *)l4hdr)->icmp_cksum;
3007                 l4hlen = 4;
3008                 sum = 0;
3009                 break;
3010         default :
3011                 break;
3012         }
3013
3014         if (csump != NULL) {
3015                 sumsave = *csump;
3016                 *csump = 0;
3017         }
3018
3019         l4hlen = l4hlen;        /* LINT */
3020
3021 #ifdef  _KERNEL
3022 # ifdef MENTAT
3023         {
3024         void *rp = m->b_rptr;
3025
3026         if ((unsigned char *)ip > m->b_rptr && (unsigned char *)ip < m->b_wptr)
3027                 m->b_rptr = (u_char *)ip;
3028         sum2 = ip_cksum(m, hlen, sum);  /* hlen == offset */
3029         m->b_rptr = rp;
3030         sum2 = (u_short)(~sum2 & 0xffff);
3031         }
3032 # else /* MENTAT */
3033 #  if defined(BSD) || defined(sun)
3034 #   if BSD >= 199103
3035         m->m_data += hlen;
3036 #   else
3037         m->m_off += hlen;
3038 #   endif
3039         m->m_len -= hlen;
3040         sum2 = in_cksum(m, slen);
3041         m->m_len += hlen;
3042 #   if BSD >= 199103
3043         m->m_data -= hlen;
3044 #   else
3045         m->m_off -= hlen;
3046 #   endif
3047         /*
3048          * Both sum and sum2 are partial sums, so combine them together.
3049          */
3050         sum += ~sum2 & 0xffff;
3051         while (sum > 0xffff)
3052                 sum = (sum & 0xffff) + (sum >> 16);
3053         sum2 = ~sum & 0xffff;
3054 #  else /* defined(BSD) || defined(sun) */
3055 {
3056         union {
3057                 u_char  c[2];
3058                 u_short s;
3059         } bytes;
3060         u_short len = ip->ip_len;
3061 #   if defined(__sgi)
3062         int add;
3063 #   endif
3064
3065         /*
3066          * Add up IP Header portion
3067          */
3068         if (sp != (u_short *)l4hdr)
3069                 sp = (u_short *)l4hdr;
3070
3071         switch (l4proto)
3072         {
3073         case IPPROTO_UDP :
3074                 sum += *sp++;   /* sport */
3075                 sum += *sp++;   /* dport */
3076                 sum += *sp++;   /* udp length */
3077                 sum += *sp++;   /* checksum */
3078                 break;
3079
3080         case IPPROTO_TCP :
3081                 sum += *sp++;   /* sport */
3082                 sum += *sp++;   /* dport */
3083                 sum += *sp++;   /* seq */
3084                 sum += *sp++;
3085                 sum += *sp++;   /* ack */
3086                 sum += *sp++;
3087                 sum += *sp++;   /* off */
3088                 sum += *sp++;   /* win */
3089                 sum += *sp++;   /* checksum */
3090                 sum += *sp++;   /* urp */
3091                 break;
3092         case IPPROTO_ICMP :
3093                 sum = *sp++;    /* type/code */
3094                 sum += *sp++;   /* checksum */
3095                 break;
3096         }
3097
3098 #   ifdef       __sgi
3099         /*
3100          * In case we had to copy the IP & TCP header out of mbufs,
3101          * skip over the mbuf bits which are the header
3102          */
3103         if ((char *)ip != mtod(m, char *)) {
3104                 hlen = (char *)sp - (char *)ip;
3105                 while (hlen) {
3106                         add = MIN(hlen, m->m_len);
3107                         sp = (u_short *)(mtod(m, caddr_t) + add);
3108                         hlen -= add;
3109                         if (add == m->m_len) {
3110                                 m = m->m_next;
3111                                 if (!hlen) {
3112                                         if (!m)
3113                                                 break;
3114                                         sp = mtod(m, u_short *);
3115                                 }
3116                                 PANIC((!m),("fr_cksum(1): not enough data"));
3117                         }
3118                 }
3119         }
3120 #   endif
3121
3122         len -= (l4hlen + hlen);
3123         if (len <= 0)
3124                 goto nodata;
3125
3126         while (len > 1) {
3127                 if (((char *)sp - mtod(m, char *)) >= m->m_len) {
3128                         m = m->m_next;
3129                         PANIC((!m),("fr_cksum(2): not enough data"));
3130                         sp = mtod(m, u_short *);
3131                 }
3132                 if (((char *)(sp + 1) - mtod(m, char *)) > m->m_len) {
3133                         bytes.c[0] = *(u_char *)sp;
3134                         m = m->m_next;
3135                         PANIC((!m),("fr_cksum(3): not enough data"));
3136                         sp = mtod(m, u_short *);
3137                         bytes.c[1] = *(u_char *)sp;
3138                         sum += bytes.s;
3139                         sp = (u_short *)((u_char *)sp + 1);
3140                 }
3141                 if ((u_long)sp & 1) {
3142                         bcopy((char *)sp++, (char *)&bytes.s, sizeof(bytes.s));
3143                         sum += bytes.s;
3144                 } else
3145                         sum += *sp++;
3146                 len -= 2;
3147         }
3148
3149         if (len != 0)
3150                 sum += ntohs(*(u_char *)sp << 8);
3151 nodata:
3152         while (sum > 0xffff)
3153                 sum = (sum & 0xffff) + (sum >> 16);
3154         sum2 = (u_short)(~sum & 0xffff);
3155 }
3156 #  endif /*  defined(BSD) || defined(sun) */
3157 # endif /* MENTAT */
3158 #else /* _KERNEL */
3159         /*
3160          * Add up IP Header portion
3161          */
3162         if (sp != (u_short *)l4hdr)
3163                 sp = (u_short *)l4hdr;
3164
3165         for (; slen > 1; slen -= 2)
3166                 sum += *sp++;
3167         if (slen)
3168                 sum += ntohs(*(u_char *)sp << 8);
3169         while (sum > 0xffff)
3170                 sum = (sum & 0xffff) + (sum >> 16);
3171         sum2 = (u_short)(~sum & 0xffff);
3172 #endif /* _KERNEL */
3173         if (csump != NULL)
3174                 *csump = sumsave;
3175         return sum2;
3176 }
3177
3178
3179 #if defined(_KERNEL) && ( ((BSD < 199103) && !defined(MENTAT)) || \
3180     defined(__sgi) ) && !defined(linux) && !defined(_AIX51)
3181 /*
3182  * Copyright (c) 1982, 1986, 1988, 1991, 1993
3183  *      The Regents of the University of California.  All rights reserved.
3184  *
3185  * Redistribution and use in source and binary forms, with or without
3186  * modification, are permitted provided that the following conditions
3187  * are met:
3188  * 1. Redistributions of source code must retain the above copyright
3189  *    notice, this list of conditions and the following disclaimer.
3190  * 2. Redistributions in binary form must reproduce the above copyright
3191  *    notice, this list of conditions and the following disclaimer in the
3192  *    documentation and/or other materials provided with the distribution.
3193  * 3. Neither the name of the University nor the names of its contributors
3194  *    may be used to endorse or promote products derived from this software
3195  *    without specific prior written permission.
3196  *
3197  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
3198  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
3199  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
3200  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
3201  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3202  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3203  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3204  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3205  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3206  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3207  * SUCH DAMAGE.
3208  *
3209  *      @(#)uipc_mbuf.c 8.2 (Berkeley) 1/4/94
3210  * $Id: fil.c,v 2.243.2.125 2007/10/10 09:27:20 darrenr Exp $
3211  */
3212 /*
3213  * Copy data from an mbuf chain starting "off" bytes from the beginning,
3214  * continuing for "len" bytes, into the indicated buffer.
3215  */
3216 void
3217 m_copydata(m, off, len, cp)
3218         mb_t *m;
3219         int off;
3220         int len;
3221         caddr_t cp;
3222 {
3223         unsigned count;
3224
3225         if (off < 0 || len < 0)
3226                 panic("m_copydata");
3227         while (off > 0) {
3228                 if (m == 0)
3229                         panic("m_copydata");
3230                 if (off < m->m_len)
3231                         break;
3232                 off -= m->m_len;
3233                 m = m->m_next;
3234         }
3235         while (len > 0) {
3236                 if (m == 0)
3237                         panic("m_copydata");
3238                 count = MIN(m->m_len - off, len);
3239                 bcopy(mtod(m, caddr_t) + off, cp, count);
3240                 len -= count;
3241                 cp += count;
3242                 off = 0;
3243                 m = m->m_next;
3244         }
3245 }
3246
3247
3248 /*
3249  * Copy data from a buffer back into the indicated mbuf chain,
3250  * starting "off" bytes from the beginning, extending the mbuf
3251  * chain if necessary.
3252  */
3253 void
3254 m_copyback(m0, off, len, cp)
3255         struct  mbuf *m0;
3256         int off;
3257         int len;
3258         caddr_t cp;
3259 {
3260         int mlen;
3261         struct mbuf *m = m0, *n;
3262         int totlen = 0;
3263
3264         if (m0 == 0)
3265                 return;
3266         while (off > (mlen = m->m_len)) {
3267                 off -= mlen;
3268                 totlen += mlen;
3269                 if (m->m_next == 0) {
3270                         n = m_getclr(M_DONTWAIT, m->m_type);
3271                         if (n == 0)
3272                                 goto out;
3273                         n->m_len = min(MLEN, len + off);
3274                         m->m_next = n;
3275                 }
3276                 m = m->m_next;
3277         }
3278         while (len > 0) {
3279                 mlen = min(m->m_len - off, len);
3280                 bcopy(cp, off + mtod(m, caddr_t), (unsigned)mlen);
3281                 cp += mlen;
3282                 len -= mlen;
3283                 mlen += off;
3284                 off = 0;
3285                 totlen += mlen;
3286                 if (len == 0)
3287                         break;
3288                 if (m->m_next == 0) {
3289                         n = m_get(M_DONTWAIT, m->m_type);
3290                         if (n == 0)
3291                                 break;
3292                         n->m_len = min(MLEN, len);
3293                         m->m_next = n;
3294                 }
3295                 m = m->m_next;
3296         }
3297 out:
3298 #if 0
3299         if (((m = m0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen))
3300                 m->m_pkthdr.len = totlen;
3301 #endif
3302         return;
3303 }
3304 #endif /* (_KERNEL) && ( ((BSD < 199103) && !MENTAT) || __sgi) */
3305
3306
3307 /* ------------------------------------------------------------------------ */
3308 /* Function:    fr_findgroup                                                */
3309 /* Returns:     frgroup_t * - NULL = group not found, else pointer to group */
3310 /* Parameters:  group(I) - group name to search for                         */
3311 /*              unit(I)  - device to which this group belongs               */
3312 /*              set(I)   - which set of rules (inactive/inactive) this is   */
3313 /*              fgpp(O)  - pointer to place to store pointer to the pointer */
3314 /*                         to where to add the next (last) group or where   */
3315 /*                         to delete group from.                            */
3316 /*                                                                          */
3317 /* Search amongst the defined groups for a particular group number.         */
3318 /* ------------------------------------------------------------------------ */
3319 frgroup_t *fr_findgroup(group, unit, set, fgpp)
3320 char *group;
3321 minor_t unit;
3322 int set;
3323 frgroup_t ***fgpp;
3324 {
3325         frgroup_t *fg, **fgp;
3326
3327         /*
3328          * Which list of groups to search in is dependent on which list of
3329          * rules are being operated on.
3330          */
3331         fgp = &ipfgroups[unit][set];
3332
3333         while ((fg = *fgp) != NULL) {
3334                 if (strncmp(group, fg->fg_name, FR_GROUPLEN) == 0)
3335                         break;
3336                 else
3337                         fgp = &fg->fg_next;
3338         }
3339         if (fgpp != NULL)
3340                 *fgpp = fgp;
3341         return fg;
3342 }
3343
3344
3345 /* ------------------------------------------------------------------------ */
3346 /* Function:    fr_addgroup                                                 */
3347 /* Returns:     frgroup_t * - NULL == did not create group,                 */
3348 /*                            != NULL == pointer to the group               */
3349 /* Parameters:  num(I)   - group number to add                              */
3350 /*              head(I)  - rule pointer that is using this as the head      */
3351 /*              flags(I) - rule flags which describe the type of rule it is */
3352 /*              unit(I)  - device to which this group will belong to        */
3353 /*              set(I)   - which set of rules (inactive/inactive) this is   */
3354 /* Write Locks: ipf_mutex                                                   */
3355 /*                                                                          */
3356 /* Add a new group head, or if it already exists, increase the reference    */
3357 /* count to it.                                                             */
3358 /* ------------------------------------------------------------------------ */
3359 frgroup_t *fr_addgroup(group, head, flags, unit, set)
3360 char *group;
3361 void *head;
3362 u_32_t flags;
3363 minor_t unit;
3364 int set;
3365 {
3366         frgroup_t *fg, **fgp;
3367         u_32_t gflags;
3368
3369         if (group == NULL)
3370                 return NULL;
3371
3372         if (unit == IPL_LOGIPF && *group == '\0')
3373                 return NULL;
3374
3375         fgp = NULL;
3376         gflags = flags & FR_INOUT;
3377
3378         fg = fr_findgroup(group, unit, set, &fgp);
3379         if (fg != NULL) {
3380                 if (fg->fg_flags == 0)
3381                         fg->fg_flags = gflags;
3382                 else if (gflags != fg->fg_flags)
3383                         return NULL;
3384                 fg->fg_ref++;
3385                 return fg;
3386         }
3387         KMALLOC(fg, frgroup_t *);
3388         if (fg != NULL) {
3389                 fg->fg_head = head;
3390                 fg->fg_start = NULL;
3391                 fg->fg_next = *fgp;
3392                 bcopy(group, fg->fg_name, FR_GROUPLEN);
3393                 fg->fg_flags = gflags;
3394                 fg->fg_ref = 1;
3395                 *fgp = fg;
3396         }
3397         return fg;
3398 }
3399
3400
3401 /* ------------------------------------------------------------------------ */
3402 /* Function:    fr_delgroup                                                 */
3403 /* Returns:     Nil                                                         */
3404 /* Parameters:  group(I) - group name to delete                             */
3405 /*              unit(I)  - device to which this group belongs               */
3406 /*              set(I)   - which set of rules (inactive/inactive) this is   */
3407 /* Write Locks: ipf_mutex                                                   */
3408 /*                                                                          */
3409 /* Attempt to delete a group head.                                          */
3410 /* Only do this when its reference count reaches 0.                         */
3411 /* ------------------------------------------------------------------------ */
3412 void fr_delgroup(group, unit, set)
3413 char *group;
3414 minor_t unit;
3415 int set;
3416 {
3417         frgroup_t *fg, **fgp;
3418
3419         fg = fr_findgroup(group, unit, set, &fgp);
3420         if (fg == NULL)
3421                 return;
3422
3423         fg->fg_ref--;
3424         if (fg->fg_ref == 0) {
3425                 *fgp = fg->fg_next;
3426                 KFREE(fg);
3427         }
3428 }
3429
3430
3431 /* ------------------------------------------------------------------------ */
3432 /* Function:    fr_getrulen                                                 */
3433 /* Returns:     frentry_t * - NULL == not found, else pointer to rule n     */
3434 /* Parameters:  unit(I)  - device for which to count the rule's number      */
3435 /*              flags(I) - which set of rules to find the rule in           */
3436 /*              group(I) - group name                                       */
3437 /*              n(I)     - rule number to find                              */
3438 /*                                                                          */
3439 /* Find rule # n in group # g and return a pointer to it.  Return NULl if   */
3440 /* group # g doesn't exist or there are less than n rules in the group.     */
3441 /* ------------------------------------------------------------------------ */
3442 frentry_t *fr_getrulen(unit, group, n)
3443 int unit;
3444 char *group;
3445 u_32_t n;
3446 {
3447         frentry_t *fr;
3448         frgroup_t *fg;
3449
3450         fg = fr_findgroup(group, unit, fr_active, NULL);
3451         if (fg == NULL)
3452                 return NULL;
3453         for (fr = fg->fg_head; fr && n; fr = fr->fr_next, n--)
3454                 ;
3455         if (n != 0)
3456                 return NULL;
3457         return fr;
3458 }
3459
3460
3461 /* ------------------------------------------------------------------------ */
3462 /* Function:    fr_rulen                                                    */
3463 /* Returns:     int - >= 0 - rule number, -1 == search failed               */
3464 /* Parameters:  unit(I) - device for which to count the rule's number       */
3465 /*              fr(I)   - pointer to rule to match                          */
3466 /*                                                                          */
3467 /* Return the number for a rule on a specific filtering device.             */
3468 /* ------------------------------------------------------------------------ */
3469 int fr_rulen(unit, fr)
3470 int unit;
3471 frentry_t *fr;
3472 {
3473         frentry_t *fh;
3474         frgroup_t *fg;
3475         u_32_t n = 0;
3476
3477         if (fr == NULL)
3478                 return -1;
3479         fg = fr_findgroup(fr->fr_group, unit, fr_active, NULL);
3480         if (fg == NULL)
3481                 return -1;
3482         for (fh = fg->fg_head; fh; n++, fh = fh->fr_next)
3483                 if (fh == fr)
3484                         break;
3485         if (fh == NULL)
3486                 return -1;
3487         return n;
3488 }
3489
3490
3491 /* ------------------------------------------------------------------------ */
3492 /* Function:    frflushlist                                                 */
3493 /* Returns:     int - >= 0 - number of flushed rules                        */
3494 /* Parameters:  set(I)   - which set of rules (inactive/inactive) this is   */
3495 /*              unit(I)  - device for which to flush rules                  */
3496 /*              flags(I) - which set of rules to flush                      */
3497 /*              nfreedp(O) - pointer to int where flush count is stored     */
3498 /*              listp(I)   - pointer to list to flush pointer               */
3499 /* Write Locks: ipf_mutex                                                   */
3500 /*                                                                          */
3501 /* Recursively flush rules from the list, descending groups as they are     */
3502 /* encountered.  if a rule is the head of a group and it has lost all its   */
3503 /* group members, then also delete the group reference.  nfreedp is needed  */
3504 /* to store the accumulating count of rules removed, whereas the returned   */
3505 /* value is just the number removed from the current list.  The latter is   */
3506 /* needed to correctly adjust reference counts on rules that define groups. */
3507 /*                                                                          */
3508 /* NOTE: Rules not loaded from user space cannot be flushed.                */
3509 /* ------------------------------------------------------------------------ */
3510 static int frflushlist(set, unit, nfreedp, listp)
3511 int set;
3512 minor_t unit;
3513 int *nfreedp;
3514 frentry_t **listp;
3515 {
3516         int freed = 0;
3517         frentry_t *fp;
3518
3519         while ((fp = *listp) != NULL) {
3520                 if ((fp->fr_type & FR_T_BUILTIN) ||
3521                     !(fp->fr_flags & FR_COPIED)) {
3522                         listp = &fp->fr_next;
3523                         continue;
3524                 }
3525                 *listp = fp->fr_next;
3526                 if (fp->fr_grp != NULL) {
3527                         (void) frflushlist(set, unit, nfreedp, fp->fr_grp);
3528                 }
3529
3530                 if (fp->fr_grhead != NULL) {
3531                         fr_delgroup(fp->fr_grhead, unit, set);
3532                         *fp->fr_grhead = '\0';
3533                 }
3534
3535                 ASSERT(fp->fr_ref > 0);
3536                 fp->fr_next = NULL;
3537                 if (fr_derefrule(&fp) == 0)
3538                         freed++;
3539         }
3540         *nfreedp += freed;
3541         return freed;
3542 }
3543
3544
3545 /* ------------------------------------------------------------------------ */
3546 /* Function:    frflush                                                     */
3547 /* Returns:     int - >= 0 - number of flushed rules                        */
3548 /* Parameters:  unit(I)  - device for which to flush rules                  */
3549 /*              flags(I) - which set of rules to flush                      */
3550 /*                                                                          */
3551 /* Calls flushlist() for all filter rules (accounting, firewall - both IPv4 */
3552 /* and IPv6) as defined by the value of flags.                              */
3553 /* ------------------------------------------------------------------------ */
3554 int frflush(unit, proto, flags)
3555 minor_t unit;
3556 int proto, flags;
3557 {
3558         int flushed = 0, set;
3559
3560         WRITE_ENTER(&ipf_mutex);
3561         bzero((char *)frcache, sizeof(frcache));
3562
3563         set = fr_active;
3564         if ((flags & FR_INACTIVE) == FR_INACTIVE)
3565                 set = 1 - set;
3566
3567         if (flags & FR_OUTQUE) {
3568                 if (proto == 0 || proto == 6) {
3569                         (void) frflushlist(set, unit,
3570                             &flushed, &ipfilter6[1][set]);
3571                         (void) frflushlist(set, unit,
3572                             &flushed, &ipacct6[1][set]);
3573                 }
3574                 if (proto == 0 || proto == 4) {
3575                         (void) frflushlist(set, unit,
3576                             &flushed, &ipfilter[1][set]);
3577                         (void) frflushlist(set, unit,
3578                             &flushed, &ipacct[1][set]);
3579                 }
3580         }
3581         if (flags & FR_INQUE) {
3582                 if (proto == 0 || proto == 6) {
3583                         (void) frflushlist(set, unit,
3584                             &flushed, &ipfilter6[0][set]);
3585                         (void) frflushlist(set, unit,
3586                             &flushed, &ipacct6[0][set]);
3587                 }
3588                 if (proto == 0 || proto == 4) {
3589                         (void) frflushlist(set, unit,
3590                             &flushed, &ipfilter[0][set]);
3591                         (void) frflushlist(set, unit,
3592                             &flushed, &ipacct[0][set]);
3593                 }
3594         }
3595         RWLOCK_EXIT(&ipf_mutex);
3596
3597         if (unit == IPL_LOGIPF) {
3598                 int tmp;
3599
3600                 tmp = frflush(IPL_LOGCOUNT, proto, flags);
3601                 if (tmp >= 0)
3602                         flushed += tmp;
3603         }
3604         return flushed;
3605 }
3606
3607
3608 /* ------------------------------------------------------------------------ */
3609 /* Function:    memstr                                                      */
3610 /* Returns:     char *  - NULL if failed, != NULL pointer to matching bytes */
3611 /* Parameters:  src(I)  - pointer to byte sequence to match                 */
3612 /*              dst(I)  - pointer to byte sequence to search                */
3613 /*              slen(I) - match length                                      */
3614 /*              dlen(I) - length available to search in                     */
3615 /*                                                                          */
3616 /* Search dst for a sequence of bytes matching those at src and extend for  */
3617 /* slen bytes.                                                              */
3618 /* ------------------------------------------------------------------------ */
3619 char *memstr(src, dst, slen, dlen)
3620 const char *src;
3621 char *dst;
3622 size_t slen, dlen;
3623 {
3624         char *s = NULL;
3625
3626         while (dlen >= slen) {
3627                 if (bcmp(src, dst, slen) == 0) {
3628                         s = dst;
3629                         break;
3630                 }
3631                 dst++;
3632                 dlen--;
3633         }
3634         return s;
3635 }
3636 /* ------------------------------------------------------------------------ */
3637 /* Function:    fr_fixskip                                                  */
3638 /* Returns:     Nil                                                         */
3639 /* Parameters:  listp(IO)    - pointer to start of list with skip rule      */
3640 /*              rp(I)        - rule added/removed with skip in it.          */
3641 /*              addremove(I) - adjustment (-1/+1) to make to skip count,    */
3642 /*                             depending on whether a rule was just added   */
3643 /*                             or removed.                                  */
3644 /*                                                                          */
3645 /* Adjust all the rules in a list which would have skip'd past the position */
3646 /* where we are inserting to skip to the right place given the change.      */
3647 /* ------------------------------------------------------------------------ */
3648 void fr_fixskip(listp, rp, addremove)
3649 frentry_t **listp, *rp;
3650 int addremove;
3651 {
3652         int rules, rn;
3653         frentry_t *fp;
3654
3655         rules = 0;
3656         for (fp = *listp; (fp != NULL) && (fp != rp); fp = fp->fr_next)
3657                 rules++;
3658
3659         if (!fp)
3660                 return;
3661
3662         for (rn = 0, fp = *listp; fp && (fp != rp); fp = fp->fr_next, rn++)
3663                 if (FR_ISSKIP(fp->fr_flags) && (rn + fp->fr_arg >= rules))
3664                         fp->fr_arg += addremove;
3665 }
3666
3667
3668 #ifdef  _KERNEL
3669 /* ------------------------------------------------------------------------ */
3670 /* Function:    count4bits                                                  */
3671 /* Returns:     int - >= 0 - number of consecutive bits in input            */
3672 /* Parameters:  ip(I) - 32bit IP address                                    */
3673 /*                                                                          */
3674 /* IPv4 ONLY                                                                */
3675 /* count consecutive 1's in bit mask.  If the mask generated by counting    */
3676 /* consecutive 1's is different to that passed, return -1, else return #    */
3677 /* of bits.                                                                 */
3678 /* ------------------------------------------------------------------------ */
3679 int     count4bits(ip)
3680 u_32_t  ip;
3681 {
3682         u_32_t  ipn;
3683         int     cnt = 0, i, j;
3684
3685         ip = ipn = ntohl(ip);
3686         for (i = 32; i; i--, ipn *= 2)
3687                 if (ipn & 0x80000000)
3688                         cnt++;
3689                 else
3690                         break;
3691         ipn = 0;
3692         for (i = 32, j = cnt; i; i--, j--) {
3693                 ipn *= 2;
3694                 if (j > 0)
3695                         ipn++;
3696         }
3697         if (ipn == ip)
3698                 return cnt;
3699         return -1;
3700 }
3701
3702
3703 # if 0
3704 /* ------------------------------------------------------------------------ */
3705 /* Function:    count6bits                                                  */
3706 /* Returns:     int - >= 0 - number of consecutive bits in input            */
3707 /* Parameters:  msk(I) - pointer to start of IPv6 bitmask                   */
3708 /*                                                                          */
3709 /* IPv6 ONLY                                                                */
3710 /* count consecutive 1's in bit mask.                                       */
3711 /* ------------------------------------------------------------------------ */
3712 int count6bits(msk)
3713 u_32_t *msk;
3714 {
3715         int i = 0, k;
3716         u_32_t j;
3717
3718         for (k = 3; k >= 0; k--)
3719                 if (msk[k] == 0xffffffff)
3720                         i += 32;
3721                 else {
3722                         for (j = msk[k]; j; j <<= 1)
3723                                 if (j & 0x80000000)
3724                                         i++;
3725                 }
3726         return i;
3727 }
3728 # endif
3729 #endif /* _KERNEL */
3730
3731
3732 /* ------------------------------------------------------------------------ */
3733 /* Function:    frsynclist                                                  */
3734 /* Returns:     void                                                        */
3735 /* Parameters:  fr(I)  - start of filter list to sync interface names for   */
3736 /*              ifp(I) - interface pointer for limiting sync lookups        */
3737 /* Write Locks: ipf_mutex                                                   */
3738 /*                                                                          */
3739 /* Walk through a list of filter rules and resolve any interface names into */
3740 /* pointers.  Where dynamic addresses are used, also update the IP address  */
3741 /* used in the rule.  The interface pointer is used to limit the lookups to */
3742 /* a specific set of matching names if it is non-NULL.                      */
3743 /* ------------------------------------------------------------------------ */
3744 static void frsynclist(fr, ifp)
3745 frentry_t *fr;
3746 void *ifp;
3747 {
3748         frdest_t *fdp;
3749         int v, i;
3750
3751         for (; fr; fr = fr->fr_next) {
3752                 v = fr->fr_v;
3753
3754                 /*
3755                  * Lookup all the interface names that are part of the rule.
3756                  */
3757                 for (i = 0; i < 4; i++) {
3758                         if ((ifp != NULL) && (fr->fr_ifas[i] != ifp))
3759                                 continue;
3760                         fr->fr_ifas[i] = fr_resolvenic(fr->fr_ifnames[i], v);
3761                 }
3762
3763                 if (fr->fr_type == FR_T_IPF) {
3764                         if (fr->fr_satype != FRI_NORMAL &&
3765                             fr->fr_satype != FRI_LOOKUP) {
3766                                 (void)fr_ifpaddr(v, fr->fr_satype,
3767                                                  fr->fr_ifas[fr->fr_sifpidx],
3768                                                  &fr->fr_src, &fr->fr_smsk);
3769                         }
3770                         if (fr->fr_datype != FRI_NORMAL &&
3771                             fr->fr_datype != FRI_LOOKUP) {
3772                                 (void)fr_ifpaddr(v, fr->fr_datype,
3773                                                  fr->fr_ifas[fr->fr_difpidx],
3774                                                  &fr->fr_dst, &fr->fr_dmsk);
3775                         }
3776                 }
3777
3778                 fdp = &fr->fr_tifs[0];
3779                 if ((ifp == NULL) || (fdp->fd_ifp == ifp))
3780                         fr_resolvedest(fdp, v);
3781
3782                 fdp = &fr->fr_tifs[1];
3783                 if ((ifp == NULL) || (fdp->fd_ifp == ifp))
3784                         fr_resolvedest(fdp, v);
3785
3786                 fdp = &fr->fr_dif;
3787                 if ((ifp == NULL) || (fdp->fd_ifp == ifp)) {
3788                         fr_resolvedest(fdp, v);
3789
3790                         fr->fr_flags &= ~FR_DUP;
3791                         if ((fdp->fd_ifp != (void *)-1) &&
3792                             (fdp->fd_ifp != NULL))
3793                                 fr->fr_flags |= FR_DUP;
3794                 }
3795
3796 #ifdef  IPFILTER_LOOKUP
3797                 if (fr->fr_type == FR_T_IPF && fr->fr_satype == FRI_LOOKUP &&
3798                     fr->fr_srcptr == NULL) {
3799                         fr->fr_srcptr = fr_resolvelookup(fr->fr_srctype,
3800                                                          fr->fr_srcsubtype,
3801                                                          &fr->fr_slookup,
3802                                                          &fr->fr_srcfunc);
3803                 }
3804                 if (fr->fr_type == FR_T_IPF && fr->fr_datype == FRI_LOOKUP &&
3805                     fr->fr_dstptr == NULL) {
3806                         fr->fr_dstptr = fr_resolvelookup(fr->fr_dsttype,
3807                                                          fr->fr_dstsubtype,
3808                                                          &fr->fr_dlookup,
3809                                                          &fr->fr_dstfunc);
3810                 }
3811 #endif
3812         }
3813 }
3814
3815
3816 #ifdef  _KERNEL
3817 /* ------------------------------------------------------------------------ */
3818 /* Function:    frsync                                                      */
3819 /* Returns:     void                                                        */
3820 /* Parameters:  Nil                                                         */
3821 /*                                                                          */
3822 /* frsync() is called when we suspect that the interface list or            */
3823 /* information about interfaces (like IP#) has changed.  Go through all     */
3824 /* filter rules, NAT entries and the state table and check if anything      */
3825 /* needs to be changed/updated.                                             */
3826 /* ------------------------------------------------------------------------ */
3827 void frsync(ifp)
3828 void *ifp;
3829 {
3830         int i;
3831
3832 # if !SOLARIS
3833         fr_natsync(ifp);
3834         fr_statesync(ifp);
3835 # endif
3836
3837         WRITE_ENTER(&ipf_mutex);
3838         frsynclist(ipacct[0][fr_active], ifp);
3839         frsynclist(ipacct[1][fr_active], ifp);
3840         frsynclist(ipfilter[0][fr_active], ifp);
3841         frsynclist(ipfilter[1][fr_active], ifp);
3842         frsynclist(ipacct6[0][fr_active], ifp);
3843         frsynclist(ipacct6[1][fr_active], ifp);
3844         frsynclist(ipfilter6[0][fr_active], ifp);
3845         frsynclist(ipfilter6[1][fr_active], ifp);
3846
3847         for (i = 0; i < IPL_LOGSIZE; i++) {
3848                 frgroup_t *g;
3849
3850                 for (g = ipfgroups[i][0]; g != NULL; g = g->fg_next)
3851                         frsynclist(g->fg_start, ifp);
3852                 for (g = ipfgroups[i][1]; g != NULL; g = g->fg_next)
3853                         frsynclist(g->fg_start, ifp);
3854         }
3855         RWLOCK_EXIT(&ipf_mutex);
3856 }
3857
3858
3859 /*
3860  * In the functions below, bcopy() is called because the pointer being
3861  * copied _from_ in this instance is a pointer to a char buf (which could
3862  * end up being unaligned) and on the kernel's local stack.
3863  */
3864 /* ------------------------------------------------------------------------ */
3865 /* Function:    copyinptr                                                   */
3866 /* Returns:     int - 0 = success, else failure                             */
3867 /* Parameters:  src(I)  - pointer to the source address                     */
3868 /*              dst(I)  - destination address                               */
3869 /*              size(I) - number of bytes to copy                           */
3870 /*                                                                          */
3871 /* Copy a block of data in from user space, given a pointer to the pointer  */
3872 /* to start copying from (src) and a pointer to where to store it (dst).    */
3873 /* NB: src - pointer to user space pointer, dst - kernel space pointer      */
3874 /* ------------------------------------------------------------------------ */
3875 int copyinptr(src, dst, size)
3876 void *src, *dst;
3877 size_t size;
3878 {
3879         caddr_t ca;
3880         int error;
3881
3882 # if SOLARIS
3883         error = COPYIN(src, &ca, sizeof(ca));
3884         if (error != 0)
3885                 return error;
3886 # else
3887         bcopy(src, (caddr_t)&ca, sizeof(ca));
3888 # endif
3889         error = COPYIN(ca, dst, size);
3890         if (error != 0)
3891                 error = EFAULT;
3892         return error;
3893 }
3894
3895
3896 /* ------------------------------------------------------------------------ */
3897 /* Function:    copyoutptr                                                  */
3898 /* Returns:     int - 0 = success, else failure                             */
3899 /* Parameters:  src(I)  - pointer to the source address                     */
3900 /*              dst(I)  - destination address                               */
3901 /*              size(I) - number of bytes to copy                           */
3902 /*                                                                          */
3903 /* Copy a block of data out to user space, given a pointer to the pointer   */
3904 /* to start copying from (src) and a pointer to where to store it (dst).    */
3905 /* NB: src - kernel space pointer, dst - pointer to user space pointer.     */
3906 /* ------------------------------------------------------------------------ */
3907 int copyoutptr(src, dst, size)
3908 void *src, *dst;
3909 size_t size;
3910 {
3911         caddr_t ca;
3912         int error;
3913
3914         bcopy(dst, (caddr_t)&ca, sizeof(ca));
3915         error = COPYOUT(src, ca, size);
3916         if (error != 0)
3917                 error = EFAULT;
3918         return error;
3919 }
3920 #endif
3921
3922
3923 /* ------------------------------------------------------------------------ */
3924 /* Function:    fr_lock                                                     */
3925 /* Returns:     int      - 0 = success, else error                          */
3926 /* Parameters:  data(I)  - pointer to lock value to set                     */
3927 /*              lockp(O) - pointer to location to store old lock value      */
3928 /*                                                                          */
3929 /* Get the new value for the lock integer, set it and return the old value  */
3930 /* in *lockp.                                                               */
3931 /* ------------------------------------------------------------------------ */
3932 int fr_lock(data, lockp)
3933 caddr_t data;
3934 int *lockp;
3935 {
3936         int arg, err;
3937
3938         err = BCOPYIN(data, &arg, sizeof(arg));
3939         if (err != 0)
3940                 return EFAULT;
3941         err = BCOPYOUT(lockp, data, sizeof(*lockp));
3942         if (err != 0)
3943                 return EFAULT;
3944         *lockp = arg;
3945         return 0;
3946 }
3947
3948
3949 /* ------------------------------------------------------------------------ */
3950 /* Function:    fr_getstat                                                  */
3951 /* Returns:     Nil                                                         */
3952 /* Parameters:  fiop(I)  - pointer to ipfilter stats structure              */
3953 /*                                                                          */
3954 /* Stores a copy of current pointers, counters, etc, in the friostat        */
3955 /* structure.                                                               */
3956 /* ------------------------------------------------------------------------ */
3957 void fr_getstat(fiop)
3958 friostat_t *fiop;
3959 {
3960         int i, j;
3961
3962         bcopy((char *)frstats, (char *)fiop->f_st, sizeof(filterstats_t) * 2);
3963         fiop->f_locks[IPL_LOGSTATE] = fr_state_lock;
3964         fiop->f_locks[IPL_LOGNAT] = fr_nat_lock;
3965         fiop->f_locks[IPL_LOGIPF] = fr_frag_lock;
3966         fiop->f_locks[IPL_LOGAUTH] = fr_auth_lock;
3967
3968         for (i = 0; i < 2; i++)
3969                 for (j = 0; j < 2; j++) {
3970                         fiop->f_ipf[i][j] = ipfilter[i][j];
3971                         fiop->f_acct[i][j] = ipacct[i][j];
3972                         fiop->f_ipf6[i][j] = ipfilter6[i][j];
3973                         fiop->f_acct6[i][j] = ipacct6[i][j];
3974                 }
3975
3976         fiop->f_ticks = fr_ticks;
3977         fiop->f_active = fr_active;
3978         fiop->f_froute[0] = fr_frouteok[0];
3979         fiop->f_froute[1] = fr_frouteok[1];
3980
3981         fiop->f_running = fr_running;
3982         for (i = 0; i < IPL_LOGSIZE; i++) {
3983                 fiop->f_groups[i][0] = ipfgroups[i][0];
3984                 fiop->f_groups[i][1] = ipfgroups[i][1];
3985         }
3986 #ifdef  IPFILTER_LOG
3987         fiop->f_logging = 1;
3988 #else
3989         fiop->f_logging = 0;
3990 #endif
3991         fiop->f_defpass = fr_pass;
3992         fiop->f_features = fr_features;
3993         (void) strncpy(fiop->f_version, ipfilter_version,
3994                        sizeof(fiop->f_version));
3995 }
3996
3997
3998 #ifdef  USE_INET6
3999 int icmptoicmp6types[ICMP_MAXTYPE+1] = {
4000         ICMP6_ECHO_REPLY,       /* 0: ICMP_ECHOREPLY */
4001         -1,                     /* 1: UNUSED */
4002         -1,                     /* 2: UNUSED */
4003         ICMP6_DST_UNREACH,      /* 3: ICMP_UNREACH */
4004         -1,                     /* 4: ICMP_SOURCEQUENCH */
4005         ND_REDIRECT,            /* 5: ICMP_REDIRECT */
4006         -1,                     /* 6: UNUSED */
4007         -1,                     /* 7: UNUSED */
4008         ICMP6_ECHO_REQUEST,     /* 8: ICMP_ECHO */
4009         -1,                     /* 9: UNUSED */
4010         -1,                     /* 10: UNUSED */
4011         ICMP6_TIME_EXCEEDED,    /* 11: ICMP_TIMXCEED */
4012         ICMP6_PARAM_PROB,       /* 12: ICMP_PARAMPROB */
4013         -1,                     /* 13: ICMP_TSTAMP */
4014         -1,                     /* 14: ICMP_TSTAMPREPLY */
4015         -1,                     /* 15: ICMP_IREQ */
4016         -1,                     /* 16: ICMP_IREQREPLY */
4017         -1,                     /* 17: ICMP_MASKREQ */
4018         -1,                     /* 18: ICMP_MASKREPLY */
4019 };
4020
4021
4022 int     icmptoicmp6unreach[ICMP_MAX_UNREACH] = {
4023         ICMP6_DST_UNREACH_ADDR,         /* 0: ICMP_UNREACH_NET */
4024         ICMP6_DST_UNREACH_ADDR,         /* 1: ICMP_UNREACH_HOST */
4025         -1,                             /* 2: ICMP_UNREACH_PROTOCOL */
4026         ICMP6_DST_UNREACH_NOPORT,       /* 3: ICMP_UNREACH_PORT */
4027         -1,                             /* 4: ICMP_UNREACH_NEEDFRAG */
4028         ICMP6_DST_UNREACH_NOTNEIGHBOR,  /* 5: ICMP_UNREACH_SRCFAIL */
4029         ICMP6_DST_UNREACH_ADDR,         /* 6: ICMP_UNREACH_NET_UNKNOWN */
4030         ICMP6_DST_UNREACH_ADDR,         /* 7: ICMP_UNREACH_HOST_UNKNOWN */
4031         -1,                             /* 8: ICMP_UNREACH_ISOLATED */
4032         ICMP6_DST_UNREACH_ADMIN,        /* 9: ICMP_UNREACH_NET_PROHIB */
4033         ICMP6_DST_UNREACH_ADMIN,        /* 10: ICMP_UNREACH_HOST_PROHIB */
4034         -1,                             /* 11: ICMP_UNREACH_TOSNET */
4035         -1,                             /* 12: ICMP_UNREACH_TOSHOST */
4036         ICMP6_DST_UNREACH_ADMIN,        /* 13: ICMP_UNREACH_ADMIN_PROHIBIT */
4037 };
4038 int     icmpreplytype6[ICMP6_MAXTYPE + 1];
4039 #endif
4040
4041 int     icmpreplytype4[ICMP_MAXTYPE + 1];
4042
4043
4044 /* ------------------------------------------------------------------------ */
4045 /* Function:    fr_matchicmpqueryreply                                      */
4046 /* Returns:     int - 1 if "icmp" is a valid reply to "ic" else 0.          */
4047 /* Parameters:  v(I)    - IP protocol version (4 or 6)                      */
4048 /*              ic(I)   - ICMP information                                  */
4049 /*              icmp(I) - ICMP packet header                                */
4050 /*              rev(I)  - direction (0 = forward/1 = reverse) of packet     */
4051 /*                                                                          */
4052 /* Check if the ICMP packet defined by the header pointed to by icmp is a   */
4053 /* reply to one as described by what's in ic.  If it is a match, return 1,  */
4054 /* else return 0 for no match.                                              */
4055 /* ------------------------------------------------------------------------ */
4056 int fr_matchicmpqueryreply(v, ic, icmp, rev)
4057 int v;
4058 icmpinfo_t *ic;
4059 icmphdr_t *icmp;
4060 int rev;
4061 {
4062         int ictype;
4063
4064         ictype = ic->ici_type;
4065
4066         if (v == 4) {
4067                 /*
4068                  * If we matched its type on the way in, then when going out
4069                  * it will still be the same type.
4070                  */
4071                 if ((!rev && (icmp->icmp_type == ictype)) ||
4072                     (rev && (icmpreplytype4[ictype] == icmp->icmp_type))) {
4073                         if (icmp->icmp_type != ICMP_ECHOREPLY)
4074                                 return 1;
4075                         if (icmp->icmp_id == ic->ici_id)
4076                                 return 1;
4077                 }
4078         }
4079 #ifdef  USE_INET6
4080         else if (v == 6) {
4081                 if ((!rev && (icmp->icmp_type == ictype)) ||
4082                     (rev && (icmpreplytype6[ictype] == icmp->icmp_type))) {
4083                         if (icmp->icmp_type != ICMP6_ECHO_REPLY)
4084                                 return 1;
4085                         if (icmp->icmp_id == ic->ici_id)
4086                                 return 1;
4087                 }
4088         }
4089 #endif
4090         return 0;
4091 }
4092
4093
4094 #ifdef  IPFILTER_LOOKUP
4095 /* ------------------------------------------------------------------------ */
4096 /* Function:    fr_resolvelookup                                            */
4097 /* Returns:     void * - NULL = failure, else success.                      */
4098 /* Parameters:  type(I)     - type of lookup these parameters are for.      */
4099 /*              subtype(I)  - whether the info below contains number/name   */
4100 /*              info(I)     - pointer to name/number of the lookup data     */
4101 /*              funcptr(IO) - pointer to pointer for storing IP address     */
4102 /*                           searching function.                            */
4103 /*                                                                          */
4104 /* Search for the "table" number passed in amongst those configured for     */
4105 /* that particular type.  If the type is recognised then the function to    */
4106 /* call to do the IP address search will be change, regardless of whether   */
4107 /* or not the "table" number exists.                                        */
4108 /* ------------------------------------------------------------------------ */
4109 static void *fr_resolvelookup(type, subtype, info, funcptr)
4110 u_int type, subtype;
4111 i6addr_t *info;
4112 lookupfunc_t *funcptr;
4113 {
4114         char label[FR_GROUPLEN], *name;
4115         iphtable_t *iph;
4116         ip_pool_t *ipo;
4117         void *ptr;
4118
4119         if (subtype == 0) {
4120 #if defined(SNPRINTF) && defined(_KERNEL)
4121                 SNPRINTF(label, sizeof(label), "%u", info->iplookupnum);
4122 #else
4123                 (void) sprintf(label, "%u", info->iplookupnum);
4124 #endif
4125                 name = label;
4126         } else if (subtype == 1) {
4127                 /*
4128                  * Because iplookupname is currently only a 12 character
4129                  * string and FR_GROUPLEN is 16, copy all of it into the
4130                  * label buffer and add on a NULL at the end.
4131                  */
4132                 strncpy(label, info->iplookupname, sizeof(info->iplookupname));
4133                 label[sizeof(info->iplookupname)] = '\0';
4134                 name = label;
4135         } else {
4136                 return NULL;
4137         }
4138
4139         READ_ENTER(&ip_poolrw);
4140
4141         switch (type)
4142         {
4143         case IPLT_POOL :
4144 # if (defined(__osf__) && defined(_KERNEL))
4145                 ptr = NULL;
4146                 *funcptr = NULL;
4147 # else
4148                 ipo = ip_pool_find(IPL_LOGIPF, name);
4149                 ptr = ipo;
4150                 if (ipo != NULL) {
4151                         ATOMIC_INC32(ipo->ipo_ref);
4152                 }
4153                 *funcptr = ip_pool_search;
4154 # endif
4155                 break;
4156         case IPLT_HASH :
4157                 iph = fr_findhtable(IPL_LOGIPF, name);
4158                 ptr = iph;
4159                 if (iph != NULL) {
4160                         ATOMIC_INC32(iph->iph_ref);
4161                 }
4162                 *funcptr = fr_iphmfindip;
4163                 break;
4164         default:
4165                 ptr = NULL;
4166                 *funcptr = NULL;
4167                 break;
4168         }
4169         RWLOCK_EXIT(&ip_poolrw);
4170
4171         return ptr;
4172 }
4173 #endif
4174
4175
4176 /* ------------------------------------------------------------------------ */
4177 /* Function:    frrequest                                                   */
4178 /* Returns:     int - 0 == success, > 0 == errno value                      */
4179 /* Parameters:  unit(I)     - device for which this is for                  */
4180 /*              req(I)      - ioctl command (SIOC*)                         */
4181 /*              data(I)     - pointr to ioctl data                          */
4182 /*              set(I)      - 1 or 0 (filter set)                           */
4183 /*              makecopy(I) - flag indicating whether data points to a rule */
4184 /*                            in kernel space & hence doesn't need copying. */
4185 /*                                                                          */
4186 /* This function handles all the requests which operate on the list of      */
4187 /* filter rules.  This includes adding, deleting, insertion.  It is also    */
4188 /* responsible for creating groups when a "head" rule is loaded.  Interface */
4189 /* names are resolved here and other sanity checks are made on the content  */
4190 /* of the rule structure being loaded.  If a rule has user defined timeouts */
4191 /* then make sure they are created and initialised before exiting.          */
4192 /* ------------------------------------------------------------------------ */
4193 int frrequest(unit, req, data, set, makecopy)
4194 int unit;
4195 ioctlcmd_t req;
4196 int set, makecopy;
4197 caddr_t data;
4198 {
4199         frentry_t frd, *fp, *f, **fprev, **ftail;
4200         int error = 0, in, v;
4201         void *ptr, *uptr;
4202         u_int *p, *pp;
4203         frgroup_t *fg;
4204         char *group;
4205
4206         fg = NULL;
4207         fp = &frd;
4208         if (makecopy != 0) {
4209                 error = fr_inobj(data, fp, IPFOBJ_FRENTRY);
4210                 if (error)
4211                         return EFAULT;
4212                 if ((fp->fr_flags & FR_T_BUILTIN) != 0)
4213                         return EINVAL;
4214                 fp->fr_ref = 0;
4215                 fp->fr_flags |= FR_COPIED;
4216         } else {
4217                 fp = (frentry_t *)data;
4218                 if ((fp->fr_type & FR_T_BUILTIN) == 0)
4219                         return EINVAL;
4220                 fp->fr_flags &= ~FR_COPIED;
4221         }
4222
4223         if (((fp->fr_dsize == 0) && (fp->fr_data != NULL)) ||
4224             ((fp->fr_dsize != 0) && (fp->fr_data == NULL)))
4225                 return EINVAL;
4226
4227         v = fp->fr_v;
4228         uptr = fp->fr_data;
4229
4230         /*
4231          * Only filter rules for IPv4 or IPv6 are accepted.
4232          */
4233         if (v == 4)
4234                 /*EMPTY*/;
4235 #ifdef  USE_INET6
4236         else if (v == 6)
4237                 /*EMPTY*/;
4238 #endif
4239         else {
4240                 return EINVAL;
4241         }
4242
4243         /*
4244          * If the rule is being loaded from user space, i.e. we had to copy it
4245          * into kernel space, then do not trust the function pointer in the
4246          * rule.
4247          */
4248         if ((makecopy == 1) && (fp->fr_func != NULL)) {
4249                 if (fr_findfunc(fp->fr_func) == NULL)
4250                         return ESRCH;
4251                 error = fr_funcinit(fp);
4252                 if (error != 0)
4253                         return error;
4254         }
4255
4256         ptr = NULL;
4257         /*
4258          * Check that the group number does exist and that its use (in/out)
4259          * matches what the rule is.
4260          */
4261         if (!strncmp(fp->fr_grhead, "0", FR_GROUPLEN))
4262                 *fp->fr_grhead = '\0';
4263         group = fp->fr_group;
4264         if (!strncmp(group, "0", FR_GROUPLEN))
4265                 *group = '\0';
4266
4267         if (FR_ISACCOUNT(fp->fr_flags))
4268                 unit = IPL_LOGCOUNT;
4269
4270         if ((req != (int)SIOCZRLST) && (*group != '\0')) {
4271                 fg = fr_findgroup(group, unit, set, NULL);
4272                 if (fg == NULL)
4273                         return ESRCH;
4274                 if (fg->fg_flags == 0)
4275                         fg->fg_flags = fp->fr_flags & FR_INOUT;
4276                 else if (fg->fg_flags != (fp->fr_flags & FR_INOUT))
4277                         return ESRCH;
4278         }
4279
4280         in = (fp->fr_flags & FR_INQUE) ? 0 : 1;
4281
4282         /*
4283          * Work out which rule list this change is being applied to.
4284          */
4285         ftail = NULL;
4286         fprev = NULL;
4287         if (unit == IPL_LOGAUTH)
4288                 fprev = &ipauth;
4289         else if (v == 4) {
4290                 if (FR_ISACCOUNT(fp->fr_flags))
4291                         fprev = &ipacct[in][set];
4292                 else if ((fp->fr_flags & (FR_OUTQUE|FR_INQUE)) != 0)
4293                         fprev = &ipfilter[in][set];
4294         } else if (v == 6) {
4295                 if (FR_ISACCOUNT(fp->fr_flags))
4296                         fprev = &ipacct6[in][set];
4297                 else if ((fp->fr_flags & (FR_OUTQUE|FR_INQUE)) != 0)
4298                         fprev = &ipfilter6[in][set];
4299         }
4300         if (fprev == NULL)
4301                 return ESRCH;
4302
4303         if (*group != '\0') {
4304                 if (!fg && !(fg = fr_findgroup(group, unit, set, NULL)))
4305                         return ESRCH;
4306                 fprev = &fg->fg_start;
4307         }
4308
4309         /*
4310          * Copy in extra data for the rule.
4311          */
4312         if (fp->fr_dsize != 0) {
4313                 if (makecopy != 0) {
4314                         KMALLOCS(ptr, void *, fp->fr_dsize);
4315                         if (!ptr)
4316                                 return ENOMEM;
4317                         error = COPYIN(uptr, ptr, fp->fr_dsize);
4318                         if (error != 0)
4319                                 error = EFAULT;
4320                 } else {
4321                         ptr = uptr;
4322                         error = 0;
4323                 }
4324                 if (error != 0) {
4325                         KFREES(ptr, fp->fr_dsize);
4326                         return ENOMEM;
4327                 }
4328                 fp->fr_data = ptr;
4329         } else
4330                 fp->fr_data = NULL;
4331
4332         /*
4333          * Perform per-rule type sanity checks of their members.
4334          */
4335         switch (fp->fr_type & ~FR_T_BUILTIN)
4336         {
4337 #if defined(IPFILTER_BPF)
4338         case FR_T_BPFOPC :
4339                 if (fp->fr_dsize == 0)
4340                         return EINVAL;
4341                 if (!bpf_validate(ptr, fp->fr_dsize/sizeof(struct bpf_insn))) {
4342                         if (makecopy && fp->fr_data != NULL) {
4343                                 KFREES(fp->fr_data, fp->fr_dsize);
4344                         }
4345                         return EINVAL;
4346                 }
4347                 break;
4348 #endif
4349         case FR_T_IPF :
4350                 if (fp->fr_dsize != sizeof(fripf_t))
4351                         return EINVAL;
4352
4353                 /*
4354                  * Allowing a rule with both "keep state" and "with oow" is
4355                  * pointless because adding a state entry to the table will
4356                  * fail with the out of window (oow) flag set.
4357                  */
4358                 if ((fp->fr_flags & FR_KEEPSTATE) && (fp->fr_flx & FI_OOW))
4359                         return EINVAL;
4360
4361                 switch (fp->fr_satype)
4362                 {
4363                 case FRI_BROADCAST :
4364                 case FRI_DYNAMIC :
4365                 case FRI_NETWORK :
4366                 case FRI_NETMASKED :
4367                 case FRI_PEERADDR :
4368                         if (fp->fr_sifpidx < 0 || fp->fr_sifpidx > 3) {
4369                                 if (makecopy && fp->fr_data != NULL) {
4370                                         KFREES(fp->fr_data, fp->fr_dsize);
4371                                 }
4372                                 return EINVAL;
4373                         }
4374                         break;
4375 #ifdef  IPFILTER_LOOKUP
4376                 case FRI_LOOKUP :
4377                         fp->fr_srcptr = fr_resolvelookup(fp->fr_srctype,
4378                                                          fp->fr_srcsubtype,
4379                                                          &fp->fr_slookup,
4380                                                          &fp->fr_srcfunc);
4381                         if (fp->fr_srcptr == NULL)
4382                                 return ESRCH;
4383                         break;
4384 #endif
4385                 default :
4386                         break;
4387                 }
4388
4389                 switch (fp->fr_datype)
4390                 {
4391                 case FRI_BROADCAST :
4392                 case FRI_DYNAMIC :
4393                 case FRI_NETWORK :
4394                 case FRI_NETMASKED :
4395                 case FRI_PEERADDR :
4396                         if (fp->fr_difpidx < 0 || fp->fr_difpidx > 3) {
4397                                 if (makecopy && fp->fr_data != NULL) {
4398                                         KFREES(fp->fr_data, fp->fr_dsize);
4399                                 }
4400                                 return EINVAL;
4401                         }
4402                         break;
4403 #ifdef  IPFILTER_LOOKUP
4404                 case FRI_LOOKUP :
4405                         fp->fr_dstptr = fr_resolvelookup(fp->fr_dsttype,
4406                                                          fp->fr_dstsubtype,
4407                                                          &fp->fr_dlookup,
4408                                                          &fp->fr_dstfunc);
4409                         if (fp->fr_dstptr == NULL)
4410                                 return ESRCH;
4411                         break;
4412 #endif
4413                 default :
4414                         break;
4415                 }
4416                 break;
4417         case FR_T_NONE :
4418                 break;
4419         case FR_T_CALLFUNC :
4420                 break;
4421         case FR_T_COMPIPF :
4422                 break;
4423         default :
4424                 if (makecopy && fp->fr_data != NULL) {
4425                         KFREES(fp->fr_data, fp->fr_dsize);
4426                 }
4427                 return EINVAL;
4428         }
4429
4430         /*
4431          * Lookup all the interface names that are part of the rule.
4432          */
4433         frsynclist(fp, NULL);
4434         fp->fr_statecnt = 0;
4435
4436         /*
4437          * Look for an existing matching filter rule, but don't include the
4438          * next or interface pointer in the comparison (fr_next, fr_ifa).
4439          * This elminates rules which are indentical being loaded.  Checksum
4440          * the constant part of the filter rule to make comparisons quicker
4441          * (this meaning no pointers are included).
4442          */
4443         for (fp->fr_cksum = 0, p = (u_int *)&fp->fr_func, pp = &fp->fr_cksum;
4444              p < pp; p++)
4445                 fp->fr_cksum += *p;
4446         pp = (u_int *)(fp->fr_caddr + fp->fr_dsize);
4447         for (p = (u_int *)fp->fr_data; p < pp; p++)
4448                 fp->fr_cksum += *p;
4449
4450         WRITE_ENTER(&ipf_mutex);
4451
4452         /*
4453          * Now that the filter rule lists are locked, we can walk the
4454          * chain of them without fear.
4455          */
4456         ftail = fprev;
4457         for (f = *ftail; (f = *ftail) != NULL; ftail = &f->fr_next) {
4458                 if (fp->fr_collect <= f->fr_collect) {
4459                         ftail = fprev;
4460                         f = NULL;
4461                         break;
4462                 }
4463                 fprev = ftail;
4464         }
4465         bzero((char *)frcache, sizeof(frcache));
4466
4467         for (; (f = *ftail) != NULL; ftail = &f->fr_next) {
4468                 if ((fp->fr_cksum != f->fr_cksum) ||
4469                     (f->fr_dsize != fp->fr_dsize))
4470                         continue;
4471                 if (bcmp((char *)&f->fr_func, (char *)&fp->fr_func, FR_CMPSIZ))
4472                         continue;
4473                 if ((!ptr && !f->fr_data) ||
4474                     (ptr && f->fr_data &&
4475                      !bcmp((char *)ptr, (char *)f->fr_data, f->fr_dsize)))
4476                         break;
4477         }
4478
4479         /*
4480          * If zero'ing statistics, copy current to caller and zero.
4481          */
4482         if (req == (ioctlcmd_t)SIOCZRLST) {
4483                 if (f == NULL)
4484                         error = ESRCH;
4485                 else {
4486                         /*
4487                          * Copy and reduce lock because of impending copyout.
4488                          * Well we should, but if we do then the atomicity of
4489                          * this call and the correctness of fr_hits and
4490                          * fr_bytes cannot be guaranteed.  As it is, this code
4491                          * only resets them to 0 if they are successfully
4492                          * copied out into user space.
4493                          */
4494                         bcopy((char *)f, (char *)fp, sizeof(*f));
4495                         /* MUTEX_DOWNGRADE(&ipf_mutex); */
4496
4497                         /*
4498                          * When we copy this rule back out, set the data
4499                          * pointer to be what it was in user space.
4500                          */
4501                         fp->fr_data = uptr;
4502                         error = fr_outobj(data, fp, IPFOBJ_FRENTRY);
4503
4504                         if (error == 0) {
4505                                 if ((f->fr_dsize != 0) && (uptr != NULL))
4506                                         error = COPYOUT(f->fr_data, uptr,
4507                                                         f->fr_dsize);
4508                                         if (error != 0)
4509                                                 error = EFAULT;
4510                                 if (error == 0) {
4511                                         f->fr_hits = 0;
4512                                         f->fr_bytes = 0;
4513                                 }
4514                         }
4515                 }
4516
4517                 if ((ptr != NULL) && (makecopy != 0)) {
4518                         KFREES(ptr, fp->fr_dsize);
4519                 }
4520                 RWLOCK_EXIT(&ipf_mutex);
4521                 return error;
4522         }
4523
4524         if (!f) {
4525                 /*
4526                  * At the end of this, ftail must point to the place where the
4527                  * new rule is to be saved/inserted/added.
4528                  * For SIOCAD*FR, this should be the last rule in the group of
4529                  * rules that have equal fr_collect fields.
4530                  * For SIOCIN*FR, ...
4531                  */
4532                 if (req == (ioctlcmd_t)SIOCADAFR ||
4533                     req == (ioctlcmd_t)SIOCADIFR) {
4534
4535                         for (ftail = fprev; (f = *ftail) != NULL; ) {
4536                                 if (f->fr_collect > fp->fr_collect)
4537                                         break;
4538                                 ftail = &f->fr_next;
4539                         }
4540                         f = NULL;
4541                         ptr = NULL;
4542                         error = 0;
4543                 } else if (req == (ioctlcmd_t)SIOCINAFR ||
4544                            req == (ioctlcmd_t)SIOCINIFR) {
4545                         while ((f = *fprev) != NULL) {
4546                                 if (f->fr_collect >= fp->fr_collect)
4547                                         break;
4548                                 fprev = &f->fr_next;
4549                         }
4550                         ftail = fprev;
4551                         if (fp->fr_hits != 0) {
4552                                 while (fp->fr_hits && (f = *ftail)) {
4553                                         if (f->fr_collect != fp->fr_collect)
4554                                                 break;
4555                                         fprev = ftail;
4556                                         ftail = &f->fr_next;
4557                                         fp->fr_hits--;
4558                                 }
4559                         }
4560                         f = NULL;
4561                         ptr = NULL;
4562                         error = 0;
4563                 }
4564         }
4565
4566         /*
4567          * Request to remove a rule.
4568          */
4569         if (req == (ioctlcmd_t)SIOCRMAFR || req == (ioctlcmd_t)SIOCRMIFR) {
4570                 if (!f)
4571                         error = ESRCH;
4572                 else {
4573                         /*
4574                          * Do not allow activity from user space to interfere
4575                          * with rules not loaded that way.
4576                          */
4577                         if ((makecopy == 1) && !(f->fr_flags & FR_COPIED)) {
4578                                 error = EPERM;
4579                                 goto done;
4580                         }
4581
4582                         /*
4583                          * Return EBUSY if the rule is being reference by
4584                          * something else (eg state information.)
4585                          */
4586                         if (f->fr_ref > 1) {
4587                                 error = EBUSY;
4588                                 goto done;
4589                         }
4590 #ifdef  IPFILTER_SCAN
4591                         if (f->fr_isctag[0] != '\0' &&
4592                             (f->fr_isc != (struct ipscan *)-1))
4593                                 ipsc_detachfr(f);
4594 #endif
4595                         if (unit == IPL_LOGAUTH) {
4596                                 error = fr_preauthcmd(req, f, ftail);
4597                                 goto done;
4598                         }
4599                         if (*f->fr_grhead != '\0')
4600                                 fr_delgroup(f->fr_grhead, unit, set);
4601                         fr_fixskip(ftail, f, -1);
4602                         *ftail = f->fr_next;
4603                         f->fr_next = NULL;
4604                         (void) fr_derefrule(&f);
4605                 }
4606         } else {
4607                 /*
4608                  * Not removing, so we must be adding/inserting a rule.
4609                  */
4610                 if (f)
4611                         error = EEXIST;
4612                 else {
4613                         if (unit == IPL_LOGAUTH) {
4614                                 error = fr_preauthcmd(req, fp, ftail);
4615                                 goto done;
4616                         }
4617                         if (makecopy) {
4618                                 KMALLOC(f, frentry_t *);
4619                         } else
4620                                 f = fp;
4621                         if (f != NULL) {
4622                                 if (fp != f)
4623                                         bcopy((char *)fp, (char *)f,
4624                                               sizeof(*f));
4625                                 MUTEX_NUKE(&f->fr_lock);
4626                                 MUTEX_INIT(&f->fr_lock, "filter rule lock");
4627 #ifdef  IPFILTER_SCAN
4628                                 if (f->fr_isctag[0] != '\0' &&
4629                                     ipsc_attachfr(f))
4630                                         f->fr_isc = (struct ipscan *)-1;
4631 #endif
4632                                 f->fr_hits = 0;
4633                                 if (makecopy != 0)
4634                                         f->fr_ref = 1;
4635                                 f->fr_next = *ftail;
4636                                 *ftail = f;
4637                                 if (req == (ioctlcmd_t)SIOCINIFR ||
4638                                     req == (ioctlcmd_t)SIOCINAFR)
4639                                         fr_fixskip(ftail, f, 1);
4640                                 f->fr_grp = NULL;
4641                                 group = f->fr_grhead;
4642                                 if (*group != '\0') {
4643                                         fg = fr_addgroup(group, f, f->fr_flags,
4644                                                          unit, set);
4645                                         if (fg != NULL)
4646                                                 f->fr_grp = &fg->fg_start;
4647                                 }
4648                         } else
4649                                 error = ENOMEM;
4650                 }
4651         }
4652 done:
4653         RWLOCK_EXIT(&ipf_mutex);
4654         if ((ptr != NULL) && (error != 0) && (makecopy != 0)) {
4655                 KFREES(ptr, fp->fr_dsize);
4656         }
4657         return (error);
4658 }
4659
4660
4661 /* ------------------------------------------------------------------------ */
4662 /* Function:    fr_funcinit                                                 */
4663 /* Returns:     int - 0 == success, else ESRCH: cannot resolve rule details */
4664 /* Parameters:  fr(I) - pointer to filter rule                              */
4665 /*                                                                          */
4666 /* If a rule is a call rule, then check if the function it points to needs  */
4667 /* an init function to be called now the rule has been loaded.              */
4668 /* ------------------------------------------------------------------------ */
4669 static int fr_funcinit(fr)
4670 frentry_t *fr;
4671 {
4672         ipfunc_resolve_t *ft;
4673         int err;
4674
4675         err = ESRCH;
4676
4677         for (ft = fr_availfuncs; ft->ipfu_addr != NULL; ft++)
4678                 if (ft->ipfu_addr == fr->fr_func) {
4679                         err = 0;
4680                         if (ft->ipfu_init != NULL)
4681                                 err = (*ft->ipfu_init)(fr);
4682                         break;
4683                 }
4684         return err;
4685 }
4686
4687
4688 /* ------------------------------------------------------------------------ */
4689 /* Function:    fr_findfunc                                                 */
4690 /* Returns:     ipfunc_t - pointer to function if found, else NULL          */
4691 /* Parameters:  funcptr(I) - function pointer to lookup                     */
4692 /*                                                                          */
4693 /* Look for a function in the table of known functions.                     */
4694 /* ------------------------------------------------------------------------ */
4695 static ipfunc_t fr_findfunc(funcptr)
4696 ipfunc_t funcptr;
4697 {
4698         ipfunc_resolve_t *ft;
4699
4700         for (ft = fr_availfuncs; ft->ipfu_addr != NULL; ft++)
4701                 if (ft->ipfu_addr == funcptr)
4702                         return funcptr;
4703         return NULL;
4704 }
4705
4706
4707 /* ------------------------------------------------------------------------ */
4708 /* Function:    fr_resolvefunc                                              */
4709 /* Returns:     int - 0 == success, else error                              */
4710 /* Parameters:  data(IO) - ioctl data pointer to ipfunc_resolve_t struct    */
4711 /*                                                                          */
4712 /* Copy in a ipfunc_resolve_t structure and then fill in the missing field. */
4713 /* This will either be the function name (if the pointer is set) or the     */
4714 /* function pointer if the name is set.  When found, fill in the other one  */
4715 /* so that the entire, complete, structure can be copied back to user space.*/
4716 /* ------------------------------------------------------------------------ */
4717 int fr_resolvefunc(data)
4718 void *data;
4719 {
4720         ipfunc_resolve_t res, *ft;
4721         int err;
4722
4723         err = BCOPYIN(data, &res, sizeof(res));
4724         if (err != 0)
4725                 return EFAULT;
4726
4727         if (res.ipfu_addr == NULL && res.ipfu_name[0] != '\0') {
4728                 for (ft = fr_availfuncs; ft->ipfu_addr != NULL; ft++)
4729                         if (strncmp(res.ipfu_name, ft->ipfu_name,
4730                                     sizeof(res.ipfu_name)) == 0) {
4731                                 res.ipfu_addr = ft->ipfu_addr;
4732                                 res.ipfu_init = ft->ipfu_init;
4733                                 if (COPYOUT(&res, data, sizeof(res)) != 0)
4734                                         return EFAULT;
4735                                 return 0;
4736                         }
4737         }
4738         if (res.ipfu_addr != NULL && res.ipfu_name[0] == '\0') {
4739                 for (ft = fr_availfuncs; ft->ipfu_addr != NULL; ft++)
4740                         if (ft->ipfu_addr == res.ipfu_addr) {
4741                                 (void) strncpy(res.ipfu_name, ft->ipfu_name,
4742                                                sizeof(res.ipfu_name));
4743                                 res.ipfu_init = ft->ipfu_init;
4744                                 if (COPYOUT(&res, data, sizeof(res)) != 0)
4745                                         return EFAULT;
4746                                 return 0;
4747                         }
4748         }
4749         return ESRCH;
4750 }
4751
4752
4753 #if !defined(_KERNEL) || (!defined(__NetBSD__) && !defined(__OpenBSD__) && !defined(__FreeBSD__)) || \
4754     (defined(__FreeBSD__) && (__FreeBSD_version < 501000)) || \
4755     (defined(__NetBSD__) && (__NetBSD_Version__ < 105000000)) || \
4756     (defined(__OpenBSD__) && (OpenBSD < 200006))
4757 /*
4758  * From: NetBSD
4759  * ppsratecheck(): packets (or events) per second limitation.
4760  */
4761 int
4762 ppsratecheck(lasttime, curpps, maxpps)
4763         struct timeval *lasttime;
4764         int *curpps;
4765         int maxpps;     /* maximum pps allowed */
4766 {
4767         struct timeval tv, delta;
4768         int rv;
4769
4770         GETKTIME(&tv);
4771
4772         delta.tv_sec = tv.tv_sec - lasttime->tv_sec;
4773         delta.tv_usec = tv.tv_usec - lasttime->tv_usec;
4774         if (delta.tv_usec < 0) {
4775                 delta.tv_sec--;
4776                 delta.tv_usec += 1000000;
4777         }
4778
4779         /*
4780          * check for 0,0 is so that the message will be seen at least once.
4781          * if more than one second have passed since the last update of
4782          * lasttime, reset the counter.
4783          *
4784          * we do increment *curpps even in *curpps < maxpps case, as some may
4785          * try to use *curpps for stat purposes as well.
4786          */
4787         if ((lasttime->tv_sec == 0 && lasttime->tv_usec == 0) ||
4788             delta.tv_sec >= 1) {
4789                 *lasttime = tv;
4790                 *curpps = 0;
4791                 rv = 1;
4792         } else if (maxpps < 0)
4793                 rv = 1;
4794         else if (*curpps < maxpps)
4795                 rv = 1;
4796         else
4797                 rv = 0;
4798         *curpps = *curpps + 1;
4799
4800         return (rv);
4801 }
4802 #endif
4803
4804
4805 /* ------------------------------------------------------------------------ */
4806 /* Function:    fr_derefrule                                                */
4807 /* Returns:     int   - 0 == rule freed up, else rule not freed             */
4808 /* Parameters:  fr(I) - pointer to filter rule                              */
4809 /*                                                                          */
4810 /* Decrement the reference counter to a rule by one.  If it reaches zero,   */
4811 /* free it and any associated storage space being used by it.               */
4812 /* ------------------------------------------------------------------------ */
4813 int fr_derefrule(frp)
4814 frentry_t **frp;
4815 {
4816         frentry_t *fr;
4817
4818         fr = *frp;
4819         *frp = NULL;
4820
4821         MUTEX_ENTER(&fr->fr_lock);
4822         fr->fr_ref--;
4823         if (fr->fr_ref == 0) {
4824                 MUTEX_EXIT(&fr->fr_lock);
4825                 MUTEX_DESTROY(&fr->fr_lock);
4826
4827 #ifdef IPFILTER_LOOKUP
4828                 if (fr->fr_type == FR_T_IPF && fr->fr_satype == FRI_LOOKUP)
4829                         ip_lookup_deref(fr->fr_srctype, fr->fr_srcptr);
4830                 if (fr->fr_type == FR_T_IPF && fr->fr_datype == FRI_LOOKUP)
4831                         ip_lookup_deref(fr->fr_dsttype, fr->fr_dstptr);
4832 #endif
4833
4834                 if (fr->fr_dsize) {
4835                         KFREES(fr->fr_data, fr->fr_dsize);
4836                 }
4837                 if ((fr->fr_flags & FR_COPIED) != 0) {
4838                         KFREE(fr);
4839                         return 0;
4840                 }
4841                 return 1;
4842         } else {
4843                 MUTEX_EXIT(&fr->fr_lock);
4844         }
4845         return -1;
4846 }
4847
4848
4849 #ifdef  IPFILTER_LOOKUP
4850 /* ------------------------------------------------------------------------ */
4851 /* Function:    fr_grpmapinit                                               */
4852 /* Returns:     int - 0 == success, else ESRCH because table entry not found*/
4853 /* Parameters:  fr(I) - pointer to rule to find hash table for              */
4854 /*                                                                          */
4855 /* Looks for group hash table fr_arg and stores a pointer to it in fr_ptr.  */
4856 /* fr_ptr is later used by fr_srcgrpmap and fr_dstgrpmap.                   */
4857 /* ------------------------------------------------------------------------ */
4858 static int fr_grpmapinit(fr)
4859 frentry_t *fr;
4860 {
4861         char name[FR_GROUPLEN];
4862         iphtable_t *iph;
4863
4864 #if defined(SNPRINTF) && defined(_KERNEL)
4865         SNPRINTF(name, sizeof(name), "%d", fr->fr_arg);
4866 #else
4867         (void) sprintf(name, "%d", fr->fr_arg);
4868 #endif
4869         iph = fr_findhtable(IPL_LOGIPF, name);
4870         if (iph == NULL)
4871                 return ESRCH;
4872         if ((iph->iph_flags & FR_INOUT) != (fr->fr_flags & FR_INOUT))
4873                 return ESRCH;
4874         fr->fr_ptr = iph;
4875         return 0;
4876 }
4877
4878
4879 /* ------------------------------------------------------------------------ */
4880 /* Function:    fr_srcgrpmap                                                */
4881 /* Returns:     frentry_t * - pointer to "new last matching" rule or NULL   */
4882 /* Parameters:  fin(I)    - pointer to packet information                   */
4883 /*              passp(IO) - pointer to current/new filter decision (unused) */
4884 /*                                                                          */
4885 /* Look for a rule group head in a hash table, using the source address as  */
4886 /* the key, and descend into that group and continue matching rules against */
4887 /* the packet.                                                              */
4888 /* ------------------------------------------------------------------------ */
4889 frentry_t *fr_srcgrpmap(fin, passp)
4890 fr_info_t *fin;
4891 u_32_t *passp;
4892 {
4893         frgroup_t *fg;
4894         void *rval;
4895
4896         rval = fr_iphmfindgroup(fin->fin_fr->fr_ptr, &fin->fin_src);
4897         if (rval == NULL)
4898                 return NULL;
4899
4900         fg = rval;
4901         fin->fin_fr = fg->fg_start;
4902         (void) fr_scanlist(fin, *passp);
4903         return fin->fin_fr;
4904 }
4905
4906
4907 /* ------------------------------------------------------------------------ */
4908 /* Function:    fr_dstgrpmap                                                */
4909 /* Returns:     frentry_t * - pointer to "new last matching" rule or NULL   */
4910 /* Parameters:  fin(I)    - pointer to packet information                   */
4911 /*              passp(IO) - pointer to current/new filter decision (unused) */
4912 /*                                                                          */
4913 /* Look for a rule group head in a hash table, using the destination        */
4914 /* address as the key, and descend into that group and continue matching    */
4915 /* rules against  the packet.                                               */
4916 /* ------------------------------------------------------------------------ */
4917 frentry_t *fr_dstgrpmap(fin, passp)
4918 fr_info_t *fin;
4919 u_32_t *passp;
4920 {
4921         frgroup_t *fg;
4922         void *rval;
4923
4924         rval = fr_iphmfindgroup(fin->fin_fr->fr_ptr, &fin->fin_dst);
4925         if (rval == NULL)
4926                 return NULL;
4927
4928         fg = rval;
4929         fin->fin_fr = fg->fg_start;
4930         (void) fr_scanlist(fin, *passp);
4931         return fin->fin_fr;
4932 }
4933 #endif /* IPFILTER_LOOKUP */
4934
4935 /*
4936  * Queue functions
4937  * ===============
4938  * These functions manage objects on queues for efficient timeouts.  There are
4939  * a number of system defined queues as well as user defined timeouts.  It is
4940  * expected that a lock is held in the domain in which the queue belongs
4941  * (i.e. either state or NAT) when calling any of these functions that prevents
4942  * fr_freetimeoutqueue() from being called at the same time as any other.
4943  */
4944
4945
4946 /* ------------------------------------------------------------------------ */
4947 /* Function:    fr_addtimeoutqueue                                          */
4948 /* Returns:     struct ifqtq * - NULL if malloc fails, else pointer to      */
4949 /*                               timeout queue with given interval.         */
4950 /* Parameters:  parent(I)  - pointer to pointer to parent node of this list */
4951 /*                           of interface queues.                           */
4952 /*              seconds(I) - timeout value in seconds for this queue.       */
4953 /*                                                                          */
4954 /* This routine first looks for a timeout queue that matches the interval   */
4955 /* being requested.  If it finds one, increments the reference counter and  */
4956 /* returns a pointer to it.  If none are found, it allocates a new one and  */
4957 /* inserts it at the top of the list.                                       */
4958 /*                                                                          */
4959 /* Locking.                                                                 */
4960 /* It is assumed that the caller of this function has an appropriate lock   */
4961 /* held (exclusively) in the domain that encompases 'parent'.               */
4962 /* ------------------------------------------------------------------------ */
4963 ipftq_t *fr_addtimeoutqueue(parent, seconds)
4964 ipftq_t **parent;
4965 u_int seconds;
4966 {
4967         ipftq_t *ifq;
4968         u_int period;
4969
4970         period = seconds * IPF_HZ_DIVIDE;
4971
4972         MUTEX_ENTER(&ipf_timeoutlock);
4973         for (ifq = *parent; ifq != NULL; ifq = ifq->ifq_next) {
4974                 if (ifq->ifq_ttl == period) {
4975                         /*
4976                          * Reset the delete flag, if set, so the structure
4977                          * gets reused rather than freed and reallocated.
4978                          */
4979                         MUTEX_ENTER(&ifq->ifq_lock);
4980                         ifq->ifq_flags &= ~IFQF_DELETE;
4981                         ifq->ifq_ref++;
4982                         MUTEX_EXIT(&ifq->ifq_lock);
4983                         MUTEX_EXIT(&ipf_timeoutlock);
4984
4985                         return ifq;
4986                 }
4987         }
4988
4989         KMALLOC(ifq, ipftq_t *);
4990         if (ifq != NULL) {
4991                 ifq->ifq_ttl = period;
4992                 ifq->ifq_head = NULL;
4993                 ifq->ifq_tail = &ifq->ifq_head;
4994                 ifq->ifq_next = *parent;
4995                 ifq->ifq_pnext = parent;
4996                 ifq->ifq_ref = 1;
4997                 ifq->ifq_flags = IFQF_USER;
4998                 *parent = ifq;
4999                 fr_userifqs++;
5000                 MUTEX_NUKE(&ifq->ifq_lock);
5001                 MUTEX_INIT(&ifq->ifq_lock, "ipftq mutex");
5002         }
5003         MUTEX_EXIT(&ipf_timeoutlock);
5004         return ifq;
5005 }
5006
5007
5008 /* ------------------------------------------------------------------------ */
5009 /* Function:    fr_deletetimeoutqueue                                       */
5010 /* Returns:     int    - new reference count value of the timeout queue     */
5011 /* Parameters:  ifq(I) - timeout queue which is losing a reference.         */
5012 /* Locks:       ifq->ifq_lock                                               */
5013 /*                                                                          */
5014 /* This routine must be called when we're discarding a pointer to a timeout */
5015 /* queue object, taking care of the reference counter.                      */
5016 /*                                                                          */
5017 /* Now that this just sets a DELETE flag, it requires the expire code to    */
5018 /* check the list of user defined timeout queues and call the free function */
5019 /* below (currently commented out) to stop memory leaking.  It is done this */
5020 /* way because the locking may not be sufficient to safely do a free when   */
5021 /* this function is called.                                                 */
5022 /* ------------------------------------------------------------------------ */
5023 int fr_deletetimeoutqueue(ifq)
5024 ipftq_t *ifq;
5025 {
5026
5027         ifq->ifq_ref--;
5028         if ((ifq->ifq_ref == 0) && ((ifq->ifq_flags & IFQF_USER) != 0)) {
5029                 ifq->ifq_flags |= IFQF_DELETE;
5030         }
5031
5032         return ifq->ifq_ref;
5033 }
5034
5035
5036 /* ------------------------------------------------------------------------ */
5037 /* Function:    fr_freetimeoutqueue                                         */
5038 /* Parameters:  ifq(I) - timeout queue which is losing a reference.         */
5039 /* Returns:     Nil                                                         */
5040 /*                                                                          */
5041 /* Locking:                                                                 */
5042 /* It is assumed that the caller of this function has an appropriate lock   */
5043 /* held (exclusively) in the domain that encompases the callers "domain".   */
5044 /* The ifq_lock for this structure should not be held.                      */
5045 /*                                                                          */
5046 /* Remove a user definde timeout queue from the list of queues it is in and */
5047 /* tidy up after this is done.                                              */
5048 /* ------------------------------------------------------------------------ */
5049 void fr_freetimeoutqueue(ifq)
5050 ipftq_t *ifq;
5051 {
5052
5053
5054         if (((ifq->ifq_flags & IFQF_DELETE) == 0) || (ifq->ifq_ref != 0) ||
5055             ((ifq->ifq_flags & IFQF_USER) == 0)) {
5056                 printf("fr_freetimeoutqueue(%lx) flags 0x%x ttl %d ref %d\n",
5057                        (u_long)ifq, ifq->ifq_flags, ifq->ifq_ttl,
5058                        ifq->ifq_ref);
5059                 return;
5060         }
5061
5062         /*
5063          * Remove from its position in the list.
5064          */
5065         *ifq->ifq_pnext = ifq->ifq_next;
5066         if (ifq->ifq_next != NULL)
5067                 ifq->ifq_next->ifq_pnext = ifq->ifq_pnext;
5068
5069         MUTEX_DESTROY(&ifq->ifq_lock);
5070         ATOMIC_DEC(fr_userifqs);
5071         KFREE(ifq);
5072 }
5073
5074
5075 /* ------------------------------------------------------------------------ */
5076 /* Function:    fr_deletequeueentry                                         */
5077 /* Returns:     Nil                                                         */
5078 /* Parameters:  tqe(I) - timeout queue entry to delete                      */
5079 /*              ifq(I) - timeout queue to remove entry from                 */
5080 /*                                                                          */
5081 /* Remove a tail queue entry from its queue and make it an orphan.          */
5082 /* fr_deletetimeoutqueue is called to make sure the reference count on the  */
5083 /* queue is correct.  We can't, however, call fr_freetimeoutqueue because   */
5084 /* the correct lock(s) may not be held that would make it safe to do so.    */
5085 /* ------------------------------------------------------------------------ */
5086 void fr_deletequeueentry(tqe)
5087 ipftqent_t *tqe;
5088 {
5089         ipftq_t *ifq;
5090
5091         ifq = tqe->tqe_ifq;
5092
5093         MUTEX_ENTER(&ifq->ifq_lock);
5094
5095         if (tqe->tqe_pnext != NULL) {
5096                 *tqe->tqe_pnext = tqe->tqe_next;
5097                 if (tqe->tqe_next != NULL)
5098                         tqe->tqe_next->tqe_pnext = tqe->tqe_pnext;
5099                 else    /* we must be the tail anyway */
5100                         ifq->ifq_tail = tqe->tqe_pnext;
5101
5102                 tqe->tqe_pnext = NULL;
5103                 tqe->tqe_ifq = NULL;
5104         }
5105
5106         (void) fr_deletetimeoutqueue(ifq);
5107
5108         MUTEX_EXIT(&ifq->ifq_lock);
5109 }
5110
5111
5112 /* ------------------------------------------------------------------------ */
5113 /* Function:    fr_queuefront                                               */
5114 /* Returns:     Nil                                                         */
5115 /* Parameters:  tqe(I) - pointer to timeout queue entry                     */
5116 /*                                                                          */
5117 /* Move a queue entry to the front of the queue, if it isn't already there. */
5118 /* ------------------------------------------------------------------------ */
5119 void fr_queuefront(tqe)
5120 ipftqent_t *tqe;
5121 {
5122         ipftq_t *ifq;
5123
5124         ifq = tqe->tqe_ifq;
5125         if (ifq == NULL)
5126                 return;
5127
5128         MUTEX_ENTER(&ifq->ifq_lock);
5129         if (ifq->ifq_head != tqe) {
5130                 *tqe->tqe_pnext = tqe->tqe_next;
5131                 if (tqe->tqe_next)
5132                         tqe->tqe_next->tqe_pnext = tqe->tqe_pnext;
5133                 else
5134                         ifq->ifq_tail = tqe->tqe_pnext;
5135
5136                 tqe->tqe_next = ifq->ifq_head;
5137                 ifq->ifq_head->tqe_pnext = &tqe->tqe_next;
5138                 ifq->ifq_head = tqe;
5139                 tqe->tqe_pnext = &ifq->ifq_head;
5140         }
5141         MUTEX_EXIT(&ifq->ifq_lock);
5142 }
5143
5144
5145 /* ------------------------------------------------------------------------ */
5146 /* Function:    fr_queueback                                                */
5147 /* Returns:     Nil                                                         */
5148 /* Parameters:  tqe(I) - pointer to timeout queue entry                     */
5149 /*                                                                          */
5150 /* Move a queue entry to the back of the queue, if it isn't already there.  */
5151 /* ------------------------------------------------------------------------ */
5152 void fr_queueback(tqe)
5153 ipftqent_t *tqe;
5154 {
5155         ipftq_t *ifq;
5156
5157         ifq = tqe->tqe_ifq;
5158         if (ifq == NULL)
5159                 return;
5160         tqe->tqe_die = fr_ticks + ifq->ifq_ttl;
5161
5162         MUTEX_ENTER(&ifq->ifq_lock);
5163         if (tqe->tqe_next != NULL) {            /* at the end already ? */
5164                 /*
5165                  * Remove from list
5166                  */
5167                 *tqe->tqe_pnext = tqe->tqe_next;
5168                 tqe->tqe_next->tqe_pnext = tqe->tqe_pnext;
5169
5170                 /*
5171                  * Make it the last entry.
5172                  */
5173                 tqe->tqe_next = NULL;
5174                 tqe->tqe_pnext = ifq->ifq_tail;
5175                 *ifq->ifq_tail = tqe;
5176                 ifq->ifq_tail = &tqe->tqe_next;
5177         }
5178         MUTEX_EXIT(&ifq->ifq_lock);
5179 }
5180
5181
5182 /* ------------------------------------------------------------------------ */
5183 /* Function:    fr_queueappend                                              */
5184 /* Returns:     Nil                                                         */
5185 /* Parameters:  tqe(I)    - pointer to timeout queue entry                  */
5186 /*              ifq(I)    - pointer to timeout queue                        */
5187 /*              parent(I) - owing object pointer                            */
5188 /*                                                                          */
5189 /* Add a new item to this queue and put it on the very end.                 */
5190 /* ------------------------------------------------------------------------ */
5191 void fr_queueappend(tqe, ifq, parent)
5192 ipftqent_t *tqe;
5193 ipftq_t *ifq;
5194 void *parent;
5195 {
5196
5197         MUTEX_ENTER(&ifq->ifq_lock);
5198         tqe->tqe_parent = parent;
5199         tqe->tqe_pnext = ifq->ifq_tail;
5200         *ifq->ifq_tail = tqe;
5201         ifq->ifq_tail = &tqe->tqe_next;
5202         tqe->tqe_next = NULL;
5203         tqe->tqe_ifq = ifq;
5204         tqe->tqe_die = fr_ticks + ifq->ifq_ttl;
5205         ifq->ifq_ref++;
5206         MUTEX_EXIT(&ifq->ifq_lock);
5207 }
5208
5209
5210 /* ------------------------------------------------------------------------ */
5211 /* Function:    fr_movequeue                                                */
5212 /* Returns:     Nil                                                         */
5213 /* Parameters:  tq(I)   - pointer to timeout queue information              */
5214 /*              oifp(I) - old timeout queue entry was on                    */
5215 /*              nifp(I) - new timeout queue to put entry on                 */
5216 /*                                                                          */
5217 /* Move a queue entry from one timeout queue to another timeout queue.      */
5218 /* If it notices that the current entry is already last and does not need   */
5219 /* to move queue, the return.                                               */
5220 /* ------------------------------------------------------------------------ */
5221 void fr_movequeue(tqe, oifq, nifq)
5222 ipftqent_t *tqe;
5223 ipftq_t *oifq, *nifq;
5224 {
5225         /*
5226          * Is the operation here going to be a no-op ?
5227          */
5228         MUTEX_ENTER(&oifq->ifq_lock);
5229         if ((oifq != nifq) || (*oifq->ifq_tail != tqe)) {
5230                 /*
5231                  * Remove from the old queue
5232                  */
5233                 *tqe->tqe_pnext = tqe->tqe_next;
5234                 if (tqe->tqe_next)
5235                         tqe->tqe_next->tqe_pnext = tqe->tqe_pnext;
5236                 else
5237                         oifq->ifq_tail = tqe->tqe_pnext;
5238                 tqe->tqe_next = NULL;
5239
5240                 /*
5241                  * If we're moving from one queue to another, release the
5242                  * lock on the old queue and get a lock on the new queue.
5243                  * For user defined queues, if we're moving off it, call
5244                  * delete in case it can now be freed.
5245                  */
5246                 if (oifq != nifq) {
5247                         tqe->tqe_ifq = NULL;
5248
5249                         (void) fr_deletetimeoutqueue(oifq);
5250
5251                         MUTEX_EXIT(&oifq->ifq_lock);
5252
5253                         MUTEX_ENTER(&nifq->ifq_lock);
5254
5255                         tqe->tqe_ifq = nifq;
5256                         nifq->ifq_ref++;
5257                 }
5258
5259                 /*
5260                  * Add to the bottom of the new queue
5261                  */
5262                 tqe->tqe_die = fr_ticks + nifq->ifq_ttl;
5263                 tqe->tqe_pnext = nifq->ifq_tail;
5264                 *nifq->ifq_tail = tqe;
5265                 nifq->ifq_tail = &tqe->tqe_next;
5266         }
5267         MUTEX_EXIT(&nifq->ifq_lock);
5268 }
5269
5270
5271 /* ------------------------------------------------------------------------ */
5272 /* Function:    fr_updateipid                                               */
5273 /* Returns:     int - 0 == success, -1 == error (packet should be droppped) */
5274 /* Parameters:  fin(I) - pointer to packet information                      */
5275 /*                                                                          */
5276 /* When we are doing NAT, change the IP of every packet to represent a      */
5277 /* single sequence of packets coming from the host, hiding any host         */
5278 /* specific sequencing that might otherwise be revealed.  If the packet is  */
5279 /* a fragment, then store the 'new' IPid in the fragment cache and look up  */
5280 /* the fragment cache for non-leading fragments.  If a non-leading fragment */
5281 /* has no match in the cache, return an error.                              */
5282 /* ------------------------------------------------------------------------ */
5283 static int fr_updateipid(fin)
5284 fr_info_t *fin;
5285 {
5286         u_short id, ido, sums;
5287         u_32_t sumd, sum;
5288         ip_t *ip;
5289
5290         if (fin->fin_off != 0) {
5291                 sum = fr_ipid_knownfrag(fin);
5292                 if (sum == 0xffffffff)
5293                         return -1;
5294                 sum &= 0xffff;
5295                 id = (u_short)sum;
5296         } else {
5297                 id = fr_nextipid(fin);
5298                 if (fin->fin_off == 0 && (fin->fin_flx & FI_FRAG) != 0)
5299                         (void) fr_ipid_newfrag(fin, (u_32_t)id);
5300         }
5301
5302         ip = fin->fin_ip;
5303         ido = ntohs(ip->ip_id);
5304         if (id == ido)
5305                 return 0;
5306         ip->ip_id = htons(id);
5307         CALC_SUMD(ido, id, sumd);       /* DESTRUCTIVE MACRO! id,ido change */
5308         sum = (~ntohs(ip->ip_sum)) & 0xffff;
5309         sum += sumd;
5310         sum = (sum >> 16) + (sum & 0xffff);
5311         sum = (sum >> 16) + (sum & 0xffff);
5312         sums = ~(u_short)sum;
5313         ip->ip_sum = htons(sums);
5314         return 0;
5315 }
5316
5317
5318 #ifdef  NEED_FRGETIFNAME
5319 /* ------------------------------------------------------------------------ */
5320 /* Function:    fr_getifname                                                */
5321 /* Returns:     char *    - pointer to interface name                       */
5322 /* Parameters:  ifp(I)    - pointer to network interface                    */
5323 /*              buffer(O) - pointer to where to store interface name        */
5324 /*                                                                          */
5325 /* Constructs an interface name in the buffer passed.  The buffer passed is */
5326 /* expected to be at least LIFNAMSIZ in bytes big.  If buffer is passed in  */
5327 /* as a NULL pointer then return a pointer to a static array.               */
5328 /* ------------------------------------------------------------------------ */
5329 char *fr_getifname(ifp, buffer)
5330 struct ifnet *ifp;
5331 char *buffer;
5332 {
5333         static char namebuf[LIFNAMSIZ];
5334 # if defined(MENTAT) || defined(__FreeBSD__) || defined(__osf__) || \
5335      defined(__sgi) || defined(linux) || defined(_AIX51) || \
5336      (defined(sun) && !defined(__SVR4) && !defined(__svr4__))
5337         int unit, space;
5338         char temp[20];
5339         char *s;
5340 # endif
5341
5342         if (buffer == NULL)
5343                 buffer = namebuf;
5344         (void) strncpy(buffer, ifp->if_name, LIFNAMSIZ);
5345         buffer[LIFNAMSIZ - 1] = '\0';
5346 # if defined(MENTAT) || defined(__FreeBSD__) || defined(__osf__) || \
5347      defined(__sgi) || defined(_AIX51) || \
5348      (defined(sun) && !defined(__SVR4) && !defined(__svr4__))
5349         for (s = buffer; *s; s++)
5350                 ;
5351         unit = ifp->if_unit;
5352         space = LIFNAMSIZ - (s - buffer);
5353         if (space > 0) {
5354 #  if defined(SNPRINTF) && defined(_KERNEL)
5355                 SNPRINTF(temp, sizeof(temp), "%d", unit);
5356 #  else
5357                 (void) sprintf(temp, "%d", unit);
5358 #  endif
5359                 (void) strncpy(s, temp, space);
5360         }
5361 # endif
5362         return buffer;
5363 }
5364 #endif
5365
5366
5367 /* ------------------------------------------------------------------------ */
5368 /* Function:    fr_ioctlswitch                                              */
5369 /* Returns:     int     - -1 continue processing, else ioctl return value   */
5370 /* Parameters:  unit(I) - device unit opened                                */
5371 /*              data(I) - pointer to ioctl data                             */
5372 /*              cmd(I)  - ioctl command                                     */
5373 /*              mode(I) - mode value                                        */
5374 /*              uid(I)  - uid making the ioctl call                         */
5375 /*              ctx(I)  - pointer to context data                           */
5376 /*                                                                          */
5377 /* Based on the value of unit, call the appropriate ioctl handler or return */
5378 /* EIO if ipfilter is not running.   Also checks if write perms are req'd   */
5379 /* for the device in order to execute the ioctl.                            */
5380 /* ------------------------------------------------------------------------ */
5381 int fr_ioctlswitch(unit, data, cmd, mode, uid, ctx)
5382 int unit, mode, uid;
5383 ioctlcmd_t cmd;
5384 void *data, *ctx;
5385 {
5386         int error = 0;
5387
5388         switch (unit)
5389         {
5390         case IPL_LOGIPF :
5391                 error = fr_ipf_ioctl(data, cmd, mode, uid, ctx);
5392                 break;
5393         case IPL_LOGNAT :
5394                 if (fr_running > 0)
5395                         error = fr_nat_ioctl(data, cmd, mode, uid, ctx);
5396                 else
5397                         error = EIO;
5398                 break;
5399         case IPL_LOGSTATE :
5400                 if (fr_running > 0)
5401                         error = fr_state_ioctl(data, cmd, mode, uid, ctx);
5402                 else
5403                         error = EIO;
5404                 break;
5405         case IPL_LOGAUTH :
5406                 if (fr_running > 0)
5407                         error = fr_auth_ioctl(data, cmd, mode, uid, ctx);
5408                 else
5409                         error = EIO;
5410                 break;
5411         case IPL_LOGSYNC :
5412 #ifdef IPFILTER_SYNC
5413                 if (fr_running > 0)
5414                         error = fr_sync_ioctl(data, cmd, mode, uid, ctx);
5415                 else
5416 #endif
5417                         error = EIO;
5418                 break;
5419         case IPL_LOGSCAN :
5420 #ifdef IPFILTER_SCAN
5421                 if (fr_running > 0)
5422                         error = fr_scan_ioctl(data, cmd, mode, uid, ctx);
5423                 else
5424 #endif
5425                         error = EIO;
5426                 break;
5427         case IPL_LOGLOOKUP :
5428 #ifdef IPFILTER_LOOKUP
5429                 if (fr_running > 0)
5430                         error = ip_lookup_ioctl(data, cmd, mode, uid, ctx);
5431                 else
5432 #endif
5433                         error = EIO;
5434                 break;
5435         default :
5436                 error = EIO;
5437                 break;
5438         }
5439
5440         return error;
5441 }
5442
5443
5444 /*
5445  * This array defines the expected size of objects coming into the kernel
5446  * for the various recognised object types.
5447  */
5448 static  int     fr_objbytes[IPFOBJ_COUNT][2] = {
5449         { 1,    sizeof(struct frentry) },               /* frentry */
5450         { 0,    sizeof(struct friostat) },
5451         { 0,    sizeof(struct fr_info) },
5452         { 0,    sizeof(struct fr_authstat) },
5453         { 0,    sizeof(struct ipfrstat) },
5454         { 0,    sizeof(struct ipnat) },
5455         { 0,    sizeof(struct natstat) },
5456         { 0,    sizeof(struct ipstate_save) },
5457         { 1,    sizeof(struct nat_save) },              /* nat_save */
5458         { 0,    sizeof(struct natlookup) },
5459         { 1,    sizeof(struct ipstate) },               /* ipstate */
5460         { 0,    sizeof(struct ips_stat) },
5461         { 0,    sizeof(struct frauth) },
5462         { 0,    sizeof(struct ipftune) },
5463         { 0,    sizeof(struct nat) },                   /* nat_t */
5464         { 0,    sizeof(struct ipfruleiter) },
5465         { 0,    sizeof(struct ipfgeniter) },
5466         { 0,    sizeof(struct ipftable) },
5467         { 0,    sizeof(struct ipflookupiter) },
5468         { 0,    sizeof(struct ipftq) * IPF_TCP_NSTATES },
5469 };
5470
5471
5472 /* ------------------------------------------------------------------------ */
5473 /* Function:    fr_inobj                                                    */
5474 /* Returns:     int     - 0 = success, else failure                         */
5475 /* Parameters:  data(I) - pointer to ioctl data                             */
5476 /*              ptr(I)  - pointer to store real data in                     */
5477 /*              type(I) - type of structure being moved                     */
5478 /*                                                                          */
5479 /* Copy in the contents of what the ipfobj_t points to.  In future, we      */
5480 /* add things to check for version numbers, sizes, etc, to make it backward */
5481 /* compatible at the ABI for user land.                                     */
5482 /* ------------------------------------------------------------------------ */
5483 int fr_inobj(data, ptr, type)
5484 void *data;
5485 void *ptr;
5486 int type;
5487 {
5488         ipfobj_t obj;
5489         int error = 0;
5490
5491         if ((type < 0) || (type >= IPFOBJ_COUNT))
5492                 return EINVAL;
5493
5494         error = BCOPYIN(data, &obj, sizeof(obj));
5495         if (error != 0)
5496                 return EFAULT;
5497
5498         if (obj.ipfo_type != type)
5499                 return EINVAL;
5500
5501 #ifndef IPFILTER_COMPAT
5502         if ((fr_objbytes[type][0] & 1) != 0) {
5503                 if (obj.ipfo_size < fr_objbytes[type][1])
5504                         return EINVAL;
5505         } else if (obj.ipfo_size != fr_objbytes[type][1]) {
5506                 return EINVAL;
5507         }
5508 #else
5509         if (obj.ipfo_rev != IPFILTER_VERSION)
5510                 /* XXX compatibility hook here */
5511                 ;
5512         if ((fr_objbytes[type][0] & 1) != 0) {
5513                 if (obj.ipfo_size < fr_objbytes[type][1])
5514                         /* XXX compatibility hook here */
5515                         return EINVAL;
5516         } else if (obj.ipfo_size != fr_objbytes[type][1])
5517                 /* XXX compatibility hook here */
5518                 return EINVAL;
5519 #endif
5520
5521         if ((fr_objbytes[type][0] & 1) != 0) {
5522                 error = COPYIN(obj.ipfo_ptr, ptr, fr_objbytes[type][1]);
5523         } else {
5524                 error = COPYIN(obj.ipfo_ptr, ptr, obj.ipfo_size);
5525         }
5526         if (error != 0)
5527                 error = EFAULT;
5528         return error;
5529 }
5530
5531
5532 /* ------------------------------------------------------------------------ */
5533 /* Function:    fr_inobjsz                                                  */
5534 /* Returns:     int     - 0 = success, else failure                         */
5535 /* Parameters:  data(I) - pointer to ioctl data                             */
5536 /*              ptr(I)  - pointer to store real data in                     */
5537 /*              type(I) - type of structure being moved                     */
5538 /*              sz(I)   - size of data to copy                              */
5539 /*                                                                          */
5540 /* As per fr_inobj, except the size of the object to copy in is passed in   */
5541 /* but it must not be smaller than the size defined for the type and the    */
5542 /* type must allow for varied sized objects.  The extra requirement here is */
5543 /* that sz must match the size of the object being passed in - this is not  */
5544 /* not possible nor required in fr_inobj().                                 */
5545 /* ------------------------------------------------------------------------ */
5546 int fr_inobjsz(data, ptr, type, sz)
5547 void *data;
5548 void *ptr;
5549 int type, sz;
5550 {
5551         ipfobj_t obj;
5552         int error;
5553
5554         if ((type < 0) || (type >= IPFOBJ_COUNT))
5555                 return EINVAL;
5556         if (((fr_objbytes[type][0] & 1) == 0) || (sz < fr_objbytes[type][1]))
5557                 return EINVAL;
5558
5559         error = BCOPYIN(data, &obj, sizeof(obj));
5560         if (error != 0)
5561                 return EFAULT;
5562
5563         if (obj.ipfo_type != type)
5564                 return EINVAL;
5565
5566 #ifndef IPFILTER_COMPAT
5567         if (obj.ipfo_size != sz)
5568                 return EINVAL;
5569 #else
5570         if (obj.ipfo_rev != IPFILTER_VERSION)
5571                 /* XXX compatibility hook here */
5572                 ;
5573         if (obj.ipfo_size != sz)
5574                 /* XXX compatibility hook here */
5575                 return EINVAL;
5576 #endif
5577
5578         error = COPYIN(obj.ipfo_ptr, ptr, sz);
5579         if (error != 0)
5580                 error = EFAULT;
5581         return error;
5582 }
5583
5584
5585 /* ------------------------------------------------------------------------ */
5586 /* Function:    fr_outobjsz                                                 */
5587 /* Returns:     int     - 0 = success, else failure                         */
5588 /* Parameters:  data(I) - pointer to ioctl data                             */
5589 /*              ptr(I)  - pointer to store real data in                     */
5590 /*              type(I) - type of structure being moved                     */
5591 /*              sz(I)   - size of data to copy                              */
5592 /*                                                                          */
5593 /* As per fr_outobj, except the size of the object to copy out is passed in */
5594 /* but it must not be smaller than the size defined for the type and the    */
5595 /* type must allow for varied sized objects.  The extra requirement here is */
5596 /* that sz must match the size of the object being passed in - this is not  */
5597 /* not possible nor required in fr_outobj().                                */
5598 /* ------------------------------------------------------------------------ */
5599 int fr_outobjsz(data, ptr, type, sz)
5600 void *data;
5601 void *ptr;
5602 int type, sz;
5603 {
5604         ipfobj_t obj;
5605         int error;
5606
5607         if ((type < 0) || (type >= IPFOBJ_COUNT) ||
5608             ((fr_objbytes[type][0] & 1) == 0) ||
5609             (sz < fr_objbytes[type][1]))
5610                 return EINVAL;
5611
5612         error = BCOPYIN(data, &obj, sizeof(obj));
5613         if (error != 0)
5614                 return EFAULT;
5615
5616         if (obj.ipfo_type != type)
5617                 return EINVAL;
5618
5619 #ifndef IPFILTER_COMPAT
5620         if (obj.ipfo_size != sz)
5621                 return EINVAL;
5622 #else
5623         if (obj.ipfo_rev != IPFILTER_VERSION)
5624                 /* XXX compatibility hook here */
5625                 ;
5626         if (obj.ipfo_size != sz)
5627                 /* XXX compatibility hook here */
5628                 return EINVAL;
5629 #endif
5630
5631         error = COPYOUT(ptr, obj.ipfo_ptr, sz);
5632         if (error != 0)
5633                 error = EFAULT;
5634         return error;
5635 }
5636
5637
5638 /* ------------------------------------------------------------------------ */
5639 /* Function:    fr_outobj                                                   */
5640 /* Returns:     int     - 0 = success, else failure                         */
5641 /* Parameters:  data(I) - pointer to ioctl data                             */
5642 /*              ptr(I)  - pointer to store real data in                     */
5643 /*              type(I) - type of structure being moved                     */
5644 /*                                                                          */
5645 /* Copy out the contents of what ptr is to where ipfobj points to.  In      */
5646 /* future, we add things to check for version numbers, sizes, etc, to make  */
5647 /* it backward  compatible at the ABI for user land.                        */
5648 /* ------------------------------------------------------------------------ */
5649 int fr_outobj(data, ptr, type)
5650 void *data;
5651 void *ptr;
5652 int type;
5653 {
5654         ipfobj_t obj;
5655         int error;
5656
5657         if ((type < 0) || (type >= IPFOBJ_COUNT))
5658                 return EINVAL;
5659
5660         error = BCOPYIN(data, &obj, sizeof(obj));
5661         if (error != 0)
5662                 return EFAULT;
5663
5664         if (obj.ipfo_type != type)
5665                 return EINVAL;
5666
5667 #ifndef IPFILTER_COMPAT
5668         if ((fr_objbytes[type][0] & 1) != 0) {
5669                 if (obj.ipfo_size < fr_objbytes[type][1])
5670                         return EINVAL;
5671         } else if (obj.ipfo_size != fr_objbytes[type][1])
5672                 return EINVAL;
5673 #else
5674         if (obj.ipfo_rev != IPFILTER_VERSION)
5675                 /* XXX compatibility hook here */
5676                 ;
5677         if ((fr_objbytes[type][0] & 1) != 0) {
5678                 if (obj.ipfo_size < fr_objbytes[type][1])
5679                         /* XXX compatibility hook here */
5680                         return EINVAL;
5681         } else if (obj.ipfo_size != fr_objbytes[type][1])
5682                 /* XXX compatibility hook here */
5683                 return EINVAL;
5684 #endif
5685
5686         error = COPYOUT(ptr, obj.ipfo_ptr, obj.ipfo_size);
5687         if (error != 0)
5688                 error = EFAULT;
5689         return error;
5690 }
5691
5692
5693 /* ------------------------------------------------------------------------ */
5694 /* Function:    fr_checkl4sum                                               */
5695 /* Returns:     int     - 0 = good, -1 = bad, 1 = cannot check              */
5696 /* Parameters:  fin(I) - pointer to packet information                      */
5697 /*                                                                          */
5698 /* If possible, calculate the layer 4 checksum for the packet.  If this is  */
5699 /* not possible, return without indicating a failure or success but in a    */
5700 /* way that is ditinguishable.                                              */
5701 /* ------------------------------------------------------------------------ */
5702 int fr_checkl4sum(fin)
5703 fr_info_t *fin;
5704 {
5705         u_short sum, hdrsum, *csump;
5706         udphdr_t *udp;
5707         int dosum;
5708
5709         if ((fin->fin_flx & FI_NOCKSUM) != 0)
5710                 return 0;
5711
5712         if (fin->fin_cksum == 1)
5713                 return 0;
5714
5715         if (fin->fin_cksum == -1)
5716                 return -1;
5717
5718         /*
5719          * If the TCP packet isn't a fragment, isn't too short and otherwise
5720          * isn't already considered "bad", then validate the checksum.  If
5721          * this check fails then considered the packet to be "bad".
5722          */
5723         if ((fin->fin_flx & (FI_FRAG|FI_SHORT|FI_BAD)) != 0)
5724                 return 1;
5725
5726         csump = NULL;
5727         hdrsum = 0;
5728         dosum = 0;
5729         sum = 0;
5730
5731 #if SOLARIS && defined(_KERNEL) && (SOLARIS2 >= 6) && defined(ICK_VALID)
5732         if (dohwcksum && ((*fin->fin_mp)->b_ick_flag == ICK_VALID)) {
5733                 hdrsum = 0;
5734                 sum = 0;
5735         } else {
5736 #endif
5737                 switch (fin->fin_p)
5738                 {
5739                 case IPPROTO_TCP :
5740                         csump = &((tcphdr_t *)fin->fin_dp)->th_sum;
5741                         dosum = 1;
5742                         break;
5743
5744                 case IPPROTO_UDP :
5745                         udp = fin->fin_dp;
5746                         if (udp->uh_sum != 0) {
5747                                 csump = &udp->uh_sum;
5748                                 dosum = 1;
5749                         }
5750                         break;
5751
5752                 case IPPROTO_ICMP :
5753                         csump = &((struct icmp *)fin->fin_dp)->icmp_cksum;
5754                         dosum = 1;
5755                         break;
5756
5757                 default :
5758                         return 1;
5759                         /*NOTREACHED*/
5760                 }
5761
5762                 if (csump != NULL)
5763                         hdrsum = *csump;
5764
5765                 if (dosum) {
5766                         sum = fr_cksum(fin->fin_m, fin->fin_ip,
5767                                        fin->fin_p, fin->fin_dp,
5768                                        fin->fin_dlen + fin->fin_hlen);
5769                 }
5770 #if SOLARIS && defined(_KERNEL) && (SOLARIS2 >= 6) && defined(ICK_VALID)
5771         }
5772 #endif
5773 #if !defined(_KERNEL)
5774         if (sum == hdrsum) {
5775                 FR_DEBUG(("checkl4sum: %hx == %hx\n", sum, hdrsum));
5776         } else {
5777                 FR_DEBUG(("checkl4sum: %hx != %hx\n", sum, hdrsum));
5778         }
5779 #endif
5780         if (hdrsum == sum) {
5781                 fin->fin_cksum = 1;
5782                 return 0;
5783         }
5784         fin->fin_cksum = -1;
5785         return -1;
5786 }
5787
5788
5789 /* ------------------------------------------------------------------------ */
5790 /* Function:    fr_ifpfillv4addr                                            */
5791 /* Returns:     int     - 0 = address update, -1 = address not updated      */
5792 /* Parameters:  atype(I)   - type of network address update to perform      */
5793 /*              sin(I)     - pointer to source of address information       */
5794 /*              mask(I)    - pointer to source of netmask information       */
5795 /*              inp(I)     - pointer to destination address store           */
5796 /*              inpmask(I) - pointer to destination netmask store           */
5797 /*                                                                          */
5798 /* Given a type of network address update (atype) to perform, copy          */
5799 /* information from sin/mask into inp/inpmask.  If ipnmask is NULL then no  */
5800 /* netmask update is performed unless FRI_NETMASKED is passed as atype, in  */
5801 /* which case the operation fails.  For all values of atype other than      */
5802 /* FRI_NETMASKED, if inpmask is non-NULL then the mask is set to an all 1s  */
5803 /* value.                                                                   */
5804 /* ------------------------------------------------------------------------ */
5805 int fr_ifpfillv4addr(atype, sin, mask, inp, inpmask)
5806 int atype;
5807 struct sockaddr_in *sin, *mask;
5808 struct in_addr *inp, *inpmask;
5809 {
5810         if (inpmask != NULL && atype != FRI_NETMASKED)
5811                 inpmask->s_addr = 0xffffffff;
5812
5813         if (atype == FRI_NETWORK || atype == FRI_NETMASKED) {
5814                 if (atype == FRI_NETMASKED) {
5815                         if (inpmask == NULL)
5816                                 return -1;
5817                         inpmask->s_addr = mask->sin_addr.s_addr;
5818                 }
5819                 inp->s_addr = sin->sin_addr.s_addr & mask->sin_addr.s_addr;
5820         } else {
5821                 inp->s_addr = sin->sin_addr.s_addr;
5822         }
5823         return 0;
5824 }
5825
5826
5827 #ifdef  USE_INET6
5828 /* ------------------------------------------------------------------------ */
5829 /* Function:    fr_ifpfillv6addr                                            */
5830 /* Returns:     int     - 0 = address update, -1 = address not updated      */
5831 /* Parameters:  atype(I)   - type of network address update to perform      */
5832 /*              sin(I)     - pointer to source of address information       */
5833 /*              mask(I)    - pointer to source of netmask information       */
5834 /*              inp(I)     - pointer to destination address store           */
5835 /*              inpmask(I) - pointer to destination netmask store           */
5836 /*                                                                          */
5837 /* Given a type of network address update (atype) to perform, copy          */
5838 /* information from sin/mask into inp/inpmask.  If ipnmask is NULL then no  */
5839 /* netmask update is performed unless FRI_NETMASKED is passed as atype, in  */
5840 /* which case the operation fails.  For all values of atype other than      */
5841 /* FRI_NETMASKED, if inpmask is non-NULL then the mask is set to an all 1s  */
5842 /* value.                                                                   */
5843 /* ------------------------------------------------------------------------ */
5844 int fr_ifpfillv6addr(atype, sin, mask, inp, inpmask)
5845 int atype;
5846 struct sockaddr_in6 *sin, *mask;
5847 struct in_addr *inp, *inpmask;
5848 {
5849         i6addr_t *src, *dst, *and, *dmask;
5850
5851         src = (i6addr_t *)&sin->sin6_addr;
5852         and = (i6addr_t *)&mask->sin6_addr;
5853         dst = (i6addr_t *)inp;
5854         dmask = (i6addr_t *)inpmask;
5855
5856         if (inpmask != NULL && atype != FRI_NETMASKED) {
5857                 dmask->i6[0] = 0xffffffff;
5858                 dmask->i6[1] = 0xffffffff;
5859                 dmask->i6[2] = 0xffffffff;
5860                 dmask->i6[3] = 0xffffffff;
5861         }
5862
5863         if (atype == FRI_NETWORK || atype == FRI_NETMASKED) {
5864                 if (atype == FRI_NETMASKED) {
5865                         if (inpmask == NULL)
5866                                 return -1;
5867                         dmask->i6[0] = and->i6[0];
5868                         dmask->i6[1] = and->i6[1];
5869                         dmask->i6[2] = and->i6[2];
5870                         dmask->i6[3] = and->i6[3];
5871                 }
5872
5873                 dst->i6[0] = src->i6[0] & and->i6[0];
5874                 dst->i6[1] = src->i6[1] & and->i6[1];
5875                 dst->i6[2] = src->i6[2] & and->i6[2];
5876                 dst->i6[3] = src->i6[3] & and->i6[3];
5877         } else {
5878                 dst->i6[0] = src->i6[0];
5879                 dst->i6[1] = src->i6[1];
5880                 dst->i6[2] = src->i6[2];
5881                 dst->i6[3] = src->i6[3];
5882         }
5883         return 0;
5884 }
5885 #endif
5886
5887
5888 /* ------------------------------------------------------------------------ */
5889 /* Function:    fr_matchtag                                                 */
5890 /* Returns:     0 == mismatch, 1 == match.                                  */
5891 /* Parameters:  tag1(I) - pointer to first tag to compare                   */
5892 /*              tag2(I) - pointer to second tag to compare                  */
5893 /*                                                                          */
5894 /* Returns true (non-zero) or false(0) if the two tag structures can be     */
5895 /* considered to be a match or not match, respectively.  The tag is 16      */
5896 /* bytes long (16 characters) but that is overlayed with 4 32bit ints so    */
5897 /* compare the ints instead, for speed. tag1 is the master of the           */
5898 /* comparison.  This function should only be called with both tag1 and tag2 */
5899 /* as non-NULL pointers.                                                    */
5900 /* ------------------------------------------------------------------------ */
5901 int fr_matchtag(tag1, tag2)
5902 ipftag_t *tag1, *tag2;
5903 {
5904         if (tag1 == tag2)
5905                 return 1;
5906
5907         if ((tag1->ipt_num[0] == 0) && (tag2->ipt_num[0] == 0))
5908                 return 1;
5909
5910         if ((tag1->ipt_num[0] == tag2->ipt_num[0]) &&
5911             (tag1->ipt_num[1] == tag2->ipt_num[1]) &&
5912             (tag1->ipt_num[2] == tag2->ipt_num[2]) &&
5913             (tag1->ipt_num[3] == tag2->ipt_num[3]))
5914                 return 1;
5915         return 0;
5916 }
5917
5918
5919 /* ------------------------------------------------------------------------ */
5920 /* Function:    fr_coalesce                                                 */
5921 /* Returns:     1 == success, -1 == failure, 0 == no change                 */
5922 /* Parameters:  fin(I) - pointer to packet information                      */
5923 /*                                                                          */
5924 /* Attempt to get all of the packet data into a single, contiguous buffer.  */
5925 /* If this call returns a failure then the buffers have also been freed.    */
5926 /* ------------------------------------------------------------------------ */
5927 int fr_coalesce(fin)
5928 fr_info_t *fin;
5929 {
5930         if ((fin->fin_flx & FI_COALESCE) != 0)
5931                 return 1;
5932
5933         /*
5934          * If the mbuf pointers indicate that there is no mbuf to work with,
5935          * return but do not indicate success or failure.
5936          */
5937         if (fin->fin_m == NULL || fin->fin_mp == NULL)
5938                 return 0;
5939
5940 #if defined(_KERNEL)
5941         if (fr_pullup(fin->fin_m, fin, fin->fin_plen) == NULL) {
5942                 ATOMIC_INCL(fr_badcoalesces[fin->fin_out]);
5943 # ifdef MENTAT
5944                 FREE_MB_T(*fin->fin_mp);
5945 # endif
5946                 *fin->fin_mp = NULL;
5947                 fin->fin_m = NULL;
5948                 return -1;
5949         }
5950 #else
5951         fin = fin;      /* LINT */
5952 #endif
5953         return 1;
5954 }
5955
5956
5957 /*
5958  * The following table lists all of the tunable variables that can be
5959  * accessed via SIOCIPFGET/SIOCIPFSET/SIOCIPFGETNEXt.  The format of each row
5960  * in the table below is as follows:
5961  *
5962  * pointer to value, name of value, minimum, maximum, size of the value's
5963  *     container, value attribute flags
5964  *
5965  * For convienience, IPFT_RDONLY means the value is read-only, IPFT_WRDISABLED
5966  * means the value can only be written to when IPFilter is loaded but disabled.
5967  * The obvious implication is if neither of these are set then the value can be
5968  * changed at any time without harm.
5969  */
5970 ipftuneable_t ipf_tuneables[] = {
5971         /* filtering */
5972         { { &fr_flags },        "fr_flags",             0,      0xffffffff,
5973                 sizeof(fr_flags),               0,      NULL },
5974         { { &fr_active },       "fr_active",            0,      0,
5975                 sizeof(fr_active),              IPFT_RDONLY,    NULL },
5976         { { &fr_control_forwarding },   "fr_control_forwarding",        0, 1,
5977                 sizeof(fr_control_forwarding),  0,      NULL },
5978         { { &fr_update_ipid },  "fr_update_ipid",       0,      1,
5979                 sizeof(fr_update_ipid),         0,      NULL },
5980         { { &fr_chksrc },       "fr_chksrc",            0,      1,
5981                 sizeof(fr_chksrc),              0,      NULL },
5982         { { &fr_minttl },       "fr_minttl",            0,      1,
5983                 sizeof(fr_minttl),              0,      NULL },
5984         { { &fr_icmpminfragmtu }, "fr_icmpminfragmtu",  0,      1,
5985                 sizeof(fr_icmpminfragmtu),      0,      NULL },
5986         { { &fr_pass },         "fr_pass",              0,      0xffffffff,
5987                 sizeof(fr_pass),                0,      NULL },
5988         /* state */
5989         { { &fr_tcpidletimeout }, "fr_tcpidletimeout",  1,      0x7fffffff,
5990                 sizeof(fr_tcpidletimeout),      IPFT_WRDISABLED,        NULL },
5991         { { &fr_tcpclosewait }, "fr_tcpclosewait",      1,      0x7fffffff,
5992                 sizeof(fr_tcpclosewait),        IPFT_WRDISABLED,        NULL },
5993         { { &fr_tcplastack },   "fr_tcplastack",        1,      0x7fffffff,
5994                 sizeof(fr_tcplastack),          IPFT_WRDISABLED,        NULL },
5995         { { &fr_tcptimeout },   "fr_tcptimeout",        1,      0x7fffffff,
5996                 sizeof(fr_tcptimeout),          IPFT_WRDISABLED,        NULL },
5997         { { &fr_tcpclosed },    "fr_tcpclosed",         1,      0x7fffffff,
5998                 sizeof(fr_tcpclosed),           IPFT_WRDISABLED,        NULL },
5999         { { &fr_tcphalfclosed }, "fr_tcphalfclosed",    1,      0x7fffffff,
6000                 sizeof(fr_tcphalfclosed),       IPFT_WRDISABLED,        NULL },
6001         { { &fr_udptimeout },   "fr_udptimeout",        1,      0x7fffffff,
6002                 sizeof(fr_udptimeout),          IPFT_WRDISABLED,        NULL },
6003         { { &fr_udpacktimeout }, "fr_udpacktimeout",    1,      0x7fffffff,
6004                 sizeof(fr_udpacktimeout),       IPFT_WRDISABLED,        NULL },
6005         { { &fr_icmptimeout },  "fr_icmptimeout",       1,      0x7fffffff,
6006                 sizeof(fr_icmptimeout),         IPFT_WRDISABLED,        NULL },
6007         { { &fr_icmpacktimeout }, "fr_icmpacktimeout",  1,      0x7fffffff,
6008                 sizeof(fr_icmpacktimeout),      IPFT_WRDISABLED,        NULL },
6009         { { &fr_iptimeout }, "fr_iptimeout",            1,      0x7fffffff,
6010                 sizeof(fr_iptimeout),           IPFT_WRDISABLED,        NULL },
6011         { { &fr_statemax },     "fr_statemax",          1,      0x7fffffff,
6012                 sizeof(fr_statemax),            0,      NULL },
6013         { { &fr_statesize },    "fr_statesize",         1,      0x7fffffff,
6014                 sizeof(fr_statesize),           IPFT_WRDISABLED,        NULL },
6015         { { &fr_state_lock },   "fr_state_lock",        0,      1,
6016                 sizeof(fr_state_lock),          IPFT_RDONLY,    NULL },
6017         { { &fr_state_maxbucket }, "fr_state_maxbucket", 1,     0x7fffffff,
6018                 sizeof(fr_state_maxbucket),     IPFT_WRDISABLED,        NULL },
6019         { { &fr_state_maxbucket_reset }, "fr_state_maxbucket_reset",    0, 1,
6020                 sizeof(fr_state_maxbucket_reset), IPFT_WRDISABLED,      NULL },
6021         { { &ipstate_logging }, "ipstate_logging",      0,      1,
6022                 sizeof(ipstate_logging),        0,      NULL },
6023         /* nat */
6024         { { &fr_nat_lock },             "fr_nat_lock",          0,      1,
6025                 sizeof(fr_nat_lock),            IPFT_RDONLY,    NULL },
6026         { { &ipf_nattable_sz }, "ipf_nattable_sz",      1,      0x7fffffff,
6027                 sizeof(ipf_nattable_sz),        IPFT_WRDISABLED,        NULL },
6028         { { &ipf_nattable_max }, "ipf_nattable_max",    1,      0x7fffffff,
6029                 sizeof(ipf_nattable_max),       0,      NULL },
6030         { { &ipf_natrules_sz }, "ipf_natrules_sz",      1,      0x7fffffff,
6031                 sizeof(ipf_natrules_sz),        IPFT_WRDISABLED,        NULL },
6032         { { &ipf_rdrrules_sz }, "ipf_rdrrules_sz",      1,      0x7fffffff,
6033                 sizeof(ipf_rdrrules_sz),        IPFT_WRDISABLED,        NULL },
6034         { { &ipf_hostmap_sz },  "ipf_hostmap_sz",       1,      0x7fffffff,
6035                 sizeof(ipf_hostmap_sz),         IPFT_WRDISABLED,        NULL },
6036         { { &fr_nat_maxbucket }, "fr_nat_maxbucket",    1,      0x7fffffff,
6037                 sizeof(fr_nat_maxbucket),       0,                      NULL },
6038         { { &fr_nat_maxbucket_reset },  "fr_nat_maxbucket_reset",       0, 1,
6039                 sizeof(fr_nat_maxbucket_reset), IPFT_WRDISABLED,        NULL },
6040         { { &nat_logging },             "nat_logging",          0,      1,
6041                 sizeof(nat_logging),            0,      NULL },
6042         { { &fr_defnatage },    "fr_defnatage",         1,      0x7fffffff,
6043                 sizeof(fr_defnatage),           IPFT_WRDISABLED,        NULL },
6044         { { &fr_defnatipage },  "fr_defnatipage",       1,      0x7fffffff,
6045                 sizeof(fr_defnatipage),         IPFT_WRDISABLED,        NULL },
6046         { { &fr_defnaticmpage }, "fr_defnaticmpage",    1,      0x7fffffff,
6047                 sizeof(fr_defnaticmpage),       IPFT_WRDISABLED,        NULL },
6048         { { &fr_nat_doflush }, "fr_nat_doflush",        0,      1,
6049                 sizeof(fr_nat_doflush),         0,      NULL },
6050         /* proxy */
6051         { { &ipf_proxy_debug }, "ipf_proxy_debug",      0,      10,
6052                 sizeof(ipf_proxy_debug),        0,      0 },
6053         /* frag */
6054         { { &ipfr_size },       "ipfr_size",            1,      0x7fffffff,
6055                 sizeof(ipfr_size),              IPFT_WRDISABLED,        NULL },
6056         { { &fr_ipfrttl },      "fr_ipfrttl",           1,      0x7fffffff,
6057                 sizeof(fr_ipfrttl),             IPFT_WRDISABLED,        NULL },
6058 #ifdef IPFILTER_LOG
6059         /* log */
6060         { { &ipl_suppress },    "ipl_suppress",         0,      1,
6061                 sizeof(ipl_suppress),           0,      NULL },
6062         { { &ipl_logmax },      "ipl_logmax",           0,      0x7fffffff,
6063                 sizeof(ipl_logmax),             IPFT_WRDISABLED,        NULL },
6064         { { &ipl_logall },      "ipl_logall",           0,      1,
6065                 sizeof(ipl_logall),             0,      NULL },
6066         { { &ipl_logsize },     "ipl_logsize",          0,      0x80000,
6067                 sizeof(ipl_logsize),            0,      NULL },
6068 #endif
6069         { { NULL },             NULL,                   0,      0,
6070                 0,                              0,      NULL }
6071 };
6072
6073 static ipftuneable_t *ipf_tunelist = NULL;
6074
6075
6076 /* ------------------------------------------------------------------------ */
6077 /* Function:    fr_findtunebycookie                                         */
6078 /* Returns:     NULL = search failed, else pointer to tune struct           */
6079 /* Parameters:  cookie(I) - cookie value to search for amongst tuneables    */
6080 /*              next(O)   - pointer to place to store the cookie for the    */
6081 /*                          "next" tuneable, if it is desired.              */
6082 /*                                                                          */
6083 /* This function is used to walk through all of the existing tunables with  */
6084 /* successive calls.  It searches the known tunables for the one which has  */
6085 /* a matching value for "cookie" - ie its address.  When returning a match, */
6086 /* the next one to be found may be returned inside next.                    */
6087 /* ------------------------------------------------------------------------ */
6088 static ipftuneable_t *fr_findtunebycookie(cookie, next)
6089 void *cookie, **next;
6090 {
6091         ipftuneable_t *ta, **tap;
6092
6093         for (ta = ipf_tuneables; ta->ipft_name != NULL; ta++)
6094                 if (ta == cookie) {
6095                         if (next != NULL) {
6096                                 /*
6097                                  * If the next entry in the array has a name
6098                                  * present, then return a pointer to it for
6099                                  * where to go next, else return a pointer to
6100                                  * the dynaminc list as a key to search there
6101                                  * next.  This facilitates a weak linking of
6102                                  * the two "lists" together.
6103                                  */
6104                                 if ((ta + 1)->ipft_name != NULL)
6105                                         *next = ta + 1;
6106                                 else
6107                                         *next = &ipf_tunelist;
6108                         }
6109                         return ta;
6110                 }
6111
6112         for (tap = &ipf_tunelist; (ta = *tap) != NULL; tap = &ta->ipft_next)
6113                 if (tap == cookie) {
6114                         if (next != NULL)
6115                                 *next = &ta->ipft_next;
6116                         return ta;
6117                 }
6118
6119         if (next != NULL)
6120                 *next = NULL;
6121         return NULL;
6122 }
6123
6124
6125 /* ------------------------------------------------------------------------ */
6126 /* Function:    fr_findtunebyname                                           */
6127 /* Returns:     NULL = search failed, else pointer to tune struct           */
6128 /* Parameters:  name(I) - name of the tuneable entry to find.               */
6129 /*                                                                          */
6130 /* Search the static array of tuneables and the list of dynamic tuneables   */
6131 /* for an entry with a matching name.  If we can find one, return a pointer */
6132 /* to the matching structure.                                               */
6133 /* ------------------------------------------------------------------------ */
6134 static ipftuneable_t *fr_findtunebyname(name)
6135 const char *name;
6136 {
6137         ipftuneable_t *ta;
6138
6139         for (ta = ipf_tuneables; ta->ipft_name != NULL; ta++)
6140                 if (!strcmp(ta->ipft_name, name)) {
6141                         return ta;
6142                 }
6143
6144         for (ta = ipf_tunelist; ta != NULL; ta = ta->ipft_next)
6145                 if (!strcmp(ta->ipft_name, name)) {
6146                         return ta;
6147                 }
6148
6149         return NULL;
6150 }
6151
6152
6153 /* ------------------------------------------------------------------------ */
6154 /* Function:    fr_addipftune                                               */
6155 /* Returns:     int - 0 == success, else failure                            */
6156 /* Parameters:  newtune - pointer to new tune struct to add to tuneables    */
6157 /*                                                                          */
6158 /* Appends the tune structure pointer to by "newtune" to the end of the     */
6159 /* current list of "dynamic" tuneable parameters.  Once added, the owner    */
6160 /* of the object is not expected to ever change "ipft_next".                */
6161 /* ------------------------------------------------------------------------ */
6162 int fr_addipftune(newtune)
6163 ipftuneable_t *newtune;
6164 {
6165         ipftuneable_t *ta, **tap;
6166
6167         ta = fr_findtunebyname(newtune->ipft_name);
6168         if (ta != NULL)
6169                 return EEXIST;
6170
6171         for (tap = &ipf_tunelist; *tap != NULL; tap = &(*tap)->ipft_next)
6172                 ;
6173
6174         newtune->ipft_next = NULL;
6175         *tap = newtune;
6176         return 0;
6177 }
6178
6179
6180 /* ------------------------------------------------------------------------ */
6181 /* Function:    fr_delipftune                                               */
6182 /* Returns:     int - 0 == success, else failure                            */
6183 /* Parameters:  oldtune - pointer to tune struct to remove from the list of */
6184 /*                        current dynamic tuneables                         */
6185 /*                                                                          */
6186 /* Search for the tune structure, by pointer, in the list of those that are */
6187 /* dynamically added at run time.  If found, adjust the list so that this   */
6188 /* structure is no longer part of it.                                       */
6189 /* ------------------------------------------------------------------------ */
6190 int fr_delipftune(oldtune)
6191 ipftuneable_t *oldtune;
6192 {
6193         ipftuneable_t *ta, **tap;
6194
6195         for (tap = &ipf_tunelist; (ta = *tap) != NULL; tap = &ta->ipft_next)
6196                 if (ta == oldtune) {
6197                         *tap = oldtune->ipft_next;
6198                         oldtune->ipft_next = NULL;
6199                         return 0;
6200                 }
6201
6202         return ESRCH;
6203 }
6204
6205
6206 /* ------------------------------------------------------------------------ */
6207 /* Function:    fr_ipftune                                                  */
6208 /* Returns:     int - 0 == success, else failure                            */
6209 /* Parameters:  cmd(I)  - ioctl command number                              */
6210 /*              data(I) - pointer to ioctl data structure                   */
6211 /*                                                                          */
6212 /* Implement handling of SIOCIPFGETNEXT, SIOCIPFGET and SIOCIPFSET.  These  */
6213 /* three ioctls provide the means to access and control global variables    */
6214 /* within IPFilter, allowing (for example) timeouts and table sizes to be   */
6215 /* changed without rebooting, reloading or recompiling.  The initialisation */
6216 /* and 'destruction' routines of the various components of ipfilter are all */
6217 /* each responsible for handling their own values being too big.            */
6218 /* ------------------------------------------------------------------------ */
6219 int fr_ipftune(cmd, data)
6220 ioctlcmd_t cmd;
6221 void *data;
6222 {
6223         ipftuneable_t *ta;
6224         ipftune_t tu;
6225         void *cookie;
6226         int error;
6227
6228         error = fr_inobj(data, &tu, IPFOBJ_TUNEABLE);
6229         if (error != 0)
6230                 return error;
6231
6232         tu.ipft_name[sizeof(tu.ipft_name) - 1] = '\0';
6233         cookie = tu.ipft_cookie;
6234         ta = NULL;
6235
6236         switch (cmd)
6237         {
6238         case SIOCIPFGETNEXT :
6239                 /*
6240                  * If cookie is non-NULL, assume it to be a pointer to the last
6241                  * entry we looked at, so find it (if possible) and return a
6242                  * pointer to the next one after it.  The last entry in the
6243                  * the table is a NULL entry, so when we get to it, set cookie
6244                  * to NULL and return that, indicating end of list, erstwhile
6245                  * if we come in with cookie set to NULL, we are starting anew
6246                  * at the front of the list.
6247                  */
6248                 if (cookie != NULL) {
6249                         ta = fr_findtunebycookie(cookie, &tu.ipft_cookie);
6250                 } else {
6251                         ta = ipf_tuneables;
6252                         tu.ipft_cookie = ta + 1;
6253                 }
6254                 if (ta != NULL) {
6255                         /*
6256                          * Entry found, but does the data pointed to by that
6257                          * row fit in what we can return?
6258                          */
6259                         if (ta->ipft_sz > sizeof(tu.ipft_un))
6260                                 return EINVAL;
6261
6262                         tu.ipft_vlong = 0;
6263                         if (ta->ipft_sz == sizeof(u_long))
6264                                 tu.ipft_vlong = *ta->ipft_plong;
6265                         else if (ta->ipft_sz == sizeof(u_int))
6266                                 tu.ipft_vint = *ta->ipft_pint;
6267                         else if (ta->ipft_sz == sizeof(u_short))
6268                                 tu.ipft_vshort = *ta->ipft_pshort;
6269                         else if (ta->ipft_sz == sizeof(u_char))
6270                                 tu.ipft_vchar = *ta->ipft_pchar;
6271
6272                         tu.ipft_sz = ta->ipft_sz;
6273                         tu.ipft_min = ta->ipft_min;
6274                         tu.ipft_max = ta->ipft_max;
6275                         tu.ipft_flags = ta->ipft_flags;
6276                         bcopy(ta->ipft_name, tu.ipft_name,
6277                               MIN(sizeof(tu.ipft_name),
6278                                   strlen(ta->ipft_name) + 1));
6279                 }
6280                 error = fr_outobj(data, &tu, IPFOBJ_TUNEABLE);
6281                 break;
6282
6283         case SIOCIPFGET :
6284         case SIOCIPFSET :
6285                 /*
6286                  * Search by name or by cookie value for a particular entry
6287                  * in the tuning paramter table.
6288                  */
6289                 error = ESRCH;
6290                 if (cookie != NULL) {
6291                         ta = fr_findtunebycookie(cookie, NULL);
6292                         if (ta != NULL)
6293                                 error = 0;
6294                 } else if (tu.ipft_name[0] != '\0') {
6295                         ta = fr_findtunebyname(tu.ipft_name);
6296                         if (ta != NULL)
6297                                 error = 0;
6298                 }
6299                 if (error != 0)
6300                         break;
6301
6302                 if (cmd == (ioctlcmd_t)SIOCIPFGET) {
6303                         /*
6304                          * Fetch the tuning parameters for a particular value
6305                          */
6306                         tu.ipft_vlong = 0;
6307                         if (ta->ipft_sz == sizeof(u_long))
6308                                 tu.ipft_vlong = *ta->ipft_plong;
6309                         else if (ta->ipft_sz == sizeof(u_int))
6310                                 tu.ipft_vint = *ta->ipft_pint;
6311                         else if (ta->ipft_sz == sizeof(u_short))
6312                                 tu.ipft_vshort = *ta->ipft_pshort;
6313                         else if (ta->ipft_sz == sizeof(u_char))
6314                                 tu.ipft_vchar = *ta->ipft_pchar;
6315                         tu.ipft_cookie = ta;
6316                         tu.ipft_sz = ta->ipft_sz;
6317                         tu.ipft_min = ta->ipft_min;
6318                         tu.ipft_max = ta->ipft_max;
6319                         tu.ipft_flags = ta->ipft_flags;
6320                         error = fr_outobj(data, &tu, IPFOBJ_TUNEABLE);
6321
6322                 } else if (cmd == (ioctlcmd_t)SIOCIPFSET) {
6323                         /*
6324                          * Set an internal parameter.  The hard part here is
6325                          * getting the new value safely and correctly out of
6326                          * the kernel (given we only know its size, not type.)
6327                          */
6328                         u_long in;
6329
6330                         if (((ta->ipft_flags & IPFT_WRDISABLED) != 0) &&
6331                             (fr_running > 0)) {
6332                                 error = EBUSY;
6333                                 break;
6334                         }
6335
6336                         in = tu.ipft_vlong;
6337                         if (in < ta->ipft_min || in > ta->ipft_max) {
6338                                 error = EINVAL;
6339                                 break;
6340                         }
6341
6342                         if (ta->ipft_sz == sizeof(u_long)) {
6343                                 tu.ipft_vlong = *ta->ipft_plong;
6344                                 *ta->ipft_plong = in;
6345                         } else if (ta->ipft_sz == sizeof(u_int)) {
6346                                 tu.ipft_vint = *ta->ipft_pint;
6347                                 *ta->ipft_pint = (u_int)(in & 0xffffffff);
6348                         } else if (ta->ipft_sz == sizeof(u_short)) {
6349                                 tu.ipft_vshort = *ta->ipft_pshort;
6350                                 *ta->ipft_pshort = (u_short)(in & 0xffff);
6351                         } else if (ta->ipft_sz == sizeof(u_char)) {
6352                                 tu.ipft_vchar = *ta->ipft_pchar;
6353                                 *ta->ipft_pchar = (u_char)(in & 0xff);
6354                         }
6355                         error = fr_outobj(data, &tu, IPFOBJ_TUNEABLE);
6356                 }
6357                 break;
6358
6359         default :
6360                 error = EINVAL;
6361                 break;
6362         }
6363
6364         return error;
6365 }
6366
6367
6368 /* ------------------------------------------------------------------------ */
6369 /* Function:    fr_initialise                                               */
6370 /* Returns:     int - 0 == success,  < 0 == failure                         */
6371 /* Parameters:  None.                                                       */
6372 /*                                                                          */
6373 /* Call of the initialise functions for all the various subsystems inside   */
6374 /* of IPFilter.  If any of them should fail, return immeadiately a failure  */
6375 /* BUT do not try to recover from the error here.                           */
6376 /* ------------------------------------------------------------------------ */
6377 int fr_initialise()
6378 {
6379         int i;
6380
6381         bzero(&frstats, sizeof(frstats));
6382
6383 #ifdef IPFILTER_LOG
6384         i = fr_loginit();
6385         if (i < 0)
6386                 return -10 + i;
6387 #endif
6388         i = fr_natinit();
6389         if (i < 0)
6390                 return -20 + i;
6391
6392         i = fr_stateinit();
6393         if (i < 0)
6394                 return -30 + i;
6395
6396         i = fr_authinit();
6397         if (i < 0)
6398                 return -40 + i;
6399
6400         i = fr_fraginit();
6401         if (i < 0)
6402                 return -50 + i;
6403
6404         i = appr_init();
6405         if (i < 0)
6406                 return -60 + i;
6407
6408 #ifdef IPFILTER_SYNC
6409         i = ipfsync_init();
6410         if (i < 0)
6411                 return -70 + i;
6412 #endif
6413 #ifdef IPFILTER_SCAN
6414         i = ipsc_init();
6415         if (i < 0)
6416                 return -80 + i;
6417 #endif
6418 #ifdef IPFILTER_LOOKUP
6419         i = ip_lookup_init();
6420         if (i < 0)
6421                 return -90 + i;
6422 #endif
6423 #ifdef IPFILTER_COMPILED
6424         ipfrule_add();
6425 #endif
6426         return 0;
6427 }
6428
6429
6430 /* ------------------------------------------------------------------------ */
6431 /* Function:    fr_deinitialise                                             */
6432 /* Returns:     None.                                                       */
6433 /* Parameters:  None.                                                       */
6434 /*                                                                          */
6435 /* Call all the various subsystem cleanup routines to deallocate memory or  */
6436 /* destroy locks or whatever they've done that they need to now undo.       */
6437 /* The order here IS important as there are some cross references of        */
6438 /* internal data structures.                                                */
6439 /* ------------------------------------------------------------------------ */
6440 void fr_deinitialise()
6441 {
6442         fr_fragunload();
6443         fr_authunload();
6444         fr_natunload();
6445         fr_stateunload();
6446 #ifdef IPFILTER_SCAN
6447         fr_scanunload();
6448 #endif
6449         appr_unload();
6450
6451 #ifdef IPFILTER_COMPILED
6452         ipfrule_remove();
6453 #endif
6454
6455         (void) frflush(IPL_LOGIPF, 0, FR_INQUE|FR_OUTQUE|FR_INACTIVE);
6456         (void) frflush(IPL_LOGIPF, 0, FR_INQUE|FR_OUTQUE);
6457         (void) frflush(IPL_LOGCOUNT, 0, FR_INQUE|FR_OUTQUE|FR_INACTIVE);
6458         (void) frflush(IPL_LOGCOUNT, 0, FR_INQUE|FR_OUTQUE);
6459
6460 #ifdef IPFILTER_LOOKUP
6461         ip_lookup_unload();
6462 #endif
6463
6464 #ifdef IPFILTER_LOG
6465         fr_logunload();
6466 #endif
6467 }
6468
6469
6470 /* ------------------------------------------------------------------------ */
6471 /* Function:    fr_zerostats                                                */
6472 /* Returns:     int - 0 = success, else failure                             */
6473 /* Parameters:  data(O) - pointer to pointer for copying data back to       */
6474 /*                                                                          */
6475 /* Copies the current statistics out to userspace and then zero's the       */
6476 /* current ones in the kernel. The lock is only held across the bzero() as  */
6477 /* the copyout may result in paging (ie network activity.)                  */
6478 /* ------------------------------------------------------------------------ */
6479 int     fr_zerostats(data)
6480 void    *data;
6481 {
6482         friostat_t fio;
6483         int error;
6484
6485         fr_getstat(&fio);
6486         error = fr_outobj(data, &fio, IPFOBJ_IPFSTAT);
6487         if (error)
6488                 return EFAULT;
6489
6490         WRITE_ENTER(&ipf_mutex);
6491         bzero(&frstats, sizeof(frstats));
6492         RWLOCK_EXIT(&ipf_mutex);
6493
6494         return 0;
6495 }
6496
6497
6498 /* ------------------------------------------------------------------------ */
6499 /* Function:    fr_resolvedest                                              */
6500 /* Returns:     Nil                                                         */
6501 /* Parameters:  fdp(IO) - pointer to destination information to resolve     */
6502 /*              v(I)    - IP protocol version to match                      */
6503 /*                                                                          */
6504 /* Looks up an interface name in the frdest structure pointed to by fdp and */
6505 /* if a matching name can be found for the particular IP protocol version   */
6506 /* then store the interface pointer in the frdest struct.  If no match is   */
6507 /* found, then set the interface pointer to be -1 as NULL is considered to  */
6508 /* indicate there is no information at all in the structure.                */
6509 /* ------------------------------------------------------------------------ */
6510 void fr_resolvedest(fdp, v)
6511 frdest_t *fdp;
6512 int v;
6513 {
6514         void *ifp;
6515
6516         ifp = NULL;
6517         v = v;          /* LINT */
6518
6519         if (*fdp->fd_ifname != '\0') {
6520                 ifp = GETIFP(fdp->fd_ifname, v);
6521                 if (ifp == NULL)
6522                         ifp = (void *)-1;
6523         }
6524         fdp->fd_ifp = ifp;
6525 }
6526
6527
6528 /* ------------------------------------------------------------------------ */
6529 /* Function:    fr_resolvenic                                               */
6530 /* Returns:     void* - NULL = wildcard name, -1 = failed to find NIC, else */
6531 /*                      pointer to interface structure for NIC              */
6532 /* Parameters:  name(I) - complete interface name                           */
6533 /*              v(I)    - IP protocol version                               */
6534 /*                                                                          */
6535 /* Look for a network interface structure that firstly has a matching name  */
6536 /* to that passed in and that is also being used for that IP protocol       */
6537 /* version (necessary on some platforms where there are separate listings   */
6538 /* for both IPv4 and IPv6 on the same physical NIC.                         */
6539 /*                                                                          */
6540 /* One might wonder why name gets terminated with a \0 byte in here.  The   */
6541 /* reason is an interface name could get into the kernel structures of ipf  */
6542 /* in any number of ways and so long as they all use the same sized array   */
6543 /* to put the name in, it makes sense to ensure it gets null terminated     */
6544 /* before it is used for its intended purpose - finding its match in the    */
6545 /* kernel's list of configured interfaces.                                  */
6546 /*                                                                          */
6547 /* NOTE: This SHOULD ONLY be used with IPFilter structures that have an     */
6548 /*       array for the name that is LIFNAMSIZ bytes (at least) in length.   */
6549 /* ------------------------------------------------------------------------ */
6550 void *fr_resolvenic(name, v)
6551 char *name;
6552 int v;
6553 {
6554         void *nic;
6555
6556         if (name[0] == '\0')
6557                 return NULL;
6558
6559         if ((name[1] == '\0') && ((name[0] == '-') || (name[0] == '*'))) {
6560                 return NULL;
6561         }
6562
6563         name[LIFNAMSIZ - 1] = '\0';
6564
6565         nic = GETIFP(name, v);
6566         if (nic == NULL)
6567                 nic = (void *)-1;
6568         return nic;
6569 }
6570
6571
6572 ipftoken_t *ipftokenhead = NULL, **ipftokentail = &ipftokenhead;
6573
6574
6575 /* ------------------------------------------------------------------------ */
6576 /* Function:    ipf_expiretokens                                            */
6577 /* Returns:     None.                                                       */
6578 /* Parameters:  None.                                                       */
6579 /*                                                                          */
6580 /* This function is run every ipf tick to see if there are any tokens that  */
6581 /* have been held for too long and need to be freed up.                     */
6582 /* ------------------------------------------------------------------------ */
6583 void ipf_expiretokens()
6584 {
6585         ipftoken_t *it;
6586
6587         WRITE_ENTER(&ipf_tokens);
6588         while ((it = ipftokenhead) != NULL) {
6589                 if (it->ipt_die > fr_ticks)
6590                         break;
6591
6592                 ipf_freetoken(it);
6593         }
6594         RWLOCK_EXIT(&ipf_tokens);
6595 }
6596
6597
6598 /* ------------------------------------------------------------------------ */
6599 /* Function:    ipf_deltoken                                                */
6600 /* Returns:     int     - 0 = success, else error                           */
6601 /* Parameters:  type(I) - the token type to match                           */
6602 /*              uid(I)  - uid owning the token                              */
6603 /*              ptr(I)  - context pointer for the token                     */
6604 /*                                                                          */
6605 /* This function looks for a a token in the current list that matches up    */
6606 /* the fields (type, uid, ptr).  If none is found, ESRCH is returned, else  */
6607 /* call ipf_freetoken() to remove it from the list.                         */
6608 /* ------------------------------------------------------------------------ */
6609 int ipf_deltoken(type, uid, ptr)
6610 int type, uid;
6611 void *ptr;
6612 {
6613         ipftoken_t *it;
6614         int error = ESRCH;
6615
6616         WRITE_ENTER(&ipf_tokens);
6617         for (it = ipftokenhead; it != NULL; it = it->ipt_next)
6618                 if (ptr == it->ipt_ctx && type == it->ipt_type &&
6619                     uid == it->ipt_uid) {
6620                         ipf_freetoken(it);
6621                         error = 0;
6622                         break;
6623         }
6624         RWLOCK_EXIT(&ipf_tokens);
6625
6626         return error;
6627 }
6628
6629
6630 /* ------------------------------------------------------------------------ */
6631 /* Function:    ipf_findtoken                                               */
6632 /* Returns:     ipftoken_t * - NULL if no memory, else pointer to token     */
6633 /* Parameters:  type(I) - the token type to match                           */
6634 /*              uid(I)  - uid owning the token                              */
6635 /*              ptr(I)  - context pointer for the token                     */
6636 /*                                                                          */
6637 /* This function looks for a live token in the list of current tokens that  */
6638 /* matches the tuple (type, uid, ptr).  If one cannot be found then one is  */
6639 /* allocated.  If one is found then it is moved to the top of the list of   */
6640 /* currently active tokens.                                                 */
6641 /*                                                                          */
6642 /* NOTE: It is by design that this function returns holding a read lock on  */
6643 /*       ipf_tokens.  Callers must make sure they release it!               */
6644 /* ------------------------------------------------------------------------ */
6645 ipftoken_t *ipf_findtoken(type, uid, ptr)
6646 int type, uid;
6647 void *ptr;
6648 {
6649         ipftoken_t *it, *new;
6650
6651         KMALLOC(new, ipftoken_t *);
6652
6653         WRITE_ENTER(&ipf_tokens);
6654         for (it = ipftokenhead; it != NULL; it = it->ipt_next) {
6655                 if (it->ipt_alive == 0)
6656                         continue;
6657                 if (ptr == it->ipt_ctx && type == it->ipt_type &&
6658                     uid == it->ipt_uid)
6659                         break;
6660         }
6661
6662         if (it == NULL) {
6663                 it = new;
6664                 new = NULL;
6665                 if (it == NULL)
6666                         return NULL;
6667                 it->ipt_data = NULL;
6668                 it->ipt_ctx = ptr;
6669                 it->ipt_uid = uid;
6670                 it->ipt_type = type;
6671                 it->ipt_next = NULL;
6672                 it->ipt_alive = 1;
6673         } else {
6674                 if (new != NULL) {
6675                         KFREE(new);
6676                         new = NULL;
6677                 }
6678
6679                 ipf_unlinktoken(it);
6680         }
6681         it->ipt_pnext = ipftokentail;
6682         *ipftokentail = it;
6683         ipftokentail = &it->ipt_next;
6684         it->ipt_next = NULL;
6685
6686         it->ipt_die = fr_ticks + 2;
6687
6688         MUTEX_DOWNGRADE(&ipf_tokens);
6689
6690         return it;
6691 }
6692
6693
6694 /* ------------------------------------------------------------------------ */
6695 /* Function:    ipf_unlinktoken                                             */
6696 /* Returns:     None.                                                       */
6697 /* Parameters:  token(I) - pointer to token structure                       */
6698 /*                                                                          */
6699 /* This function unlinks a token structure from the linked list of tokens   */
6700 /* that "own" it.  The head pointer never needs to be explicitly adjusted   */
6701 /* but the tail does due to the linked list implementation.                 */
6702 /* ------------------------------------------------------------------------ */
6703 static void ipf_unlinktoken(token)
6704 ipftoken_t *token;
6705 {
6706
6707         if (ipftokentail == &token->ipt_next)
6708                 ipftokentail = token->ipt_pnext;
6709
6710         *token->ipt_pnext = token->ipt_next;
6711         if (token->ipt_next != NULL)
6712                 token->ipt_next->ipt_pnext = token->ipt_pnext;
6713 }
6714
6715
6716 /* ------------------------------------------------------------------------ */
6717 /* Function:    ipf_freetoken                                               */
6718 /* Returns:     None.                                                       */
6719 /* Parameters:  token(I) - pointer to token structure                       */
6720 /*                                                                          */
6721 /* This function unlinks a token from the linked list and on the path to    */
6722 /* free'ing the data, it calls the dereference function that is associated  */
6723 /* with the type of data pointed to by the token as it is considered to     */
6724 /* hold a reference to it.                                                  */
6725 /* ------------------------------------------------------------------------ */
6726 void ipf_freetoken(token)
6727 ipftoken_t *token;
6728 {
6729         void *data, **datap;
6730
6731         ipf_unlinktoken(token);
6732
6733         data = token->ipt_data;
6734         datap = &data;
6735
6736         if ((data != NULL) && (data != (void *)-1)) {
6737                 switch (token->ipt_type)
6738                 {
6739                 case IPFGENITER_IPF :
6740                         (void) fr_derefrule((frentry_t **)datap);
6741                         break;
6742                 case IPFGENITER_IPNAT :
6743                         WRITE_ENTER(&ipf_nat);
6744                         fr_ipnatderef((ipnat_t **)datap);
6745                         RWLOCK_EXIT(&ipf_nat);
6746                         break;
6747                 case IPFGENITER_NAT :
6748                         fr_natderef((nat_t **)datap);
6749                         break;
6750                 case IPFGENITER_STATE :
6751                         fr_statederef((ipstate_t **)datap);
6752                         break;
6753                 case IPFGENITER_FRAG :
6754 #ifdef USE_MUTEXES
6755                         fr_fragderef((ipfr_t **)datap, &ipf_frag);
6756 #else
6757                         fr_fragderef((ipfr_t **)datap);
6758 #endif
6759                         break;
6760                 case IPFGENITER_NATFRAG :
6761 #ifdef USE_MUTEXES
6762                         fr_fragderef((ipfr_t **)datap, &ipf_natfrag);
6763 #else
6764                         fr_fragderef((ipfr_t **)datap);
6765 #endif
6766                         break;
6767                 case IPFGENITER_HOSTMAP :
6768                         WRITE_ENTER(&ipf_nat);
6769                         fr_hostmapdel((hostmap_t **)datap);
6770                         RWLOCK_EXIT(&ipf_nat);
6771                         break;
6772                 default :
6773 #ifdef IPFILTER_LOOKUP
6774                         ip_lookup_iterderef(token->ipt_type, data);
6775 #endif
6776                         break;
6777                 }
6778         }
6779
6780         KFREE(token);
6781 }
6782
6783
6784 /* ------------------------------------------------------------------------ */
6785 /* Function:    ipf_getnextrule                                             */
6786 /* Returns:     int - 0 = success, else error                               */
6787 /* Parameters:  t(I)   - pointer to destination information to resolve      */
6788 /*              ptr(I) - pointer to ipfobj_t to copyin from user space      */
6789 /*                                                                          */
6790 /* This function's first job is to bring in the ipfruleiter_t structure via */
6791 /* the ipfobj_t structure to determine what should be the next rule to      */
6792 /* return. Once the ipfruleiter_t has been brought in, it then tries to     */
6793 /* find the 'next rule'.  This may include searching rule group lists or    */
6794 /* just be as simple as looking at the 'next' field in the rule structure.  */
6795 /* When we have found the rule to return, increase its reference count and  */
6796 /* if we used an existing rule to get here, decrease its reference count.   */
6797 /* ------------------------------------------------------------------------ */
6798 int ipf_getnextrule(ipftoken_t *t, void *ptr)
6799 {
6800         frentry_t *fr, *next, zero;
6801         int error, count, out;
6802         ipfruleiter_t it;
6803         frgroup_t *fg;
6804         char *dst;
6805
6806         if (t == NULL || ptr == NULL)
6807                 return EFAULT;
6808         error = fr_inobj(ptr, &it, IPFOBJ_IPFITER);
6809         if (error != 0)
6810                 return error;
6811         if ((it.iri_inout < 0) || (it.iri_inout > 3))
6812                 return EINVAL;
6813         if ((it.iri_active != 0) && (it.iri_active != 1))
6814                 return EINVAL;
6815         if (it.iri_nrules == 0)
6816                 return ENOSPC;
6817         if (it.iri_rule == NULL)
6818                 return EFAULT;
6819
6820         out = it.iri_inout & F_OUT;
6821         fr = t->ipt_data;
6822         READ_ENTER(&ipf_mutex);
6823         if (fr == NULL) {
6824                 if (*it.iri_group == '\0') {
6825                         if ((it.iri_inout & F_ACIN) != 0) {
6826                                 if (it.iri_v == 4)
6827                                         next = ipacct[out][it.iri_active];
6828                                 else
6829                                         next = ipacct6[out][it.iri_active];
6830                         } else {
6831                                 if (it.iri_v == 4)
6832                                         next = ipfilter[out][it.iri_active];
6833                                 else
6834                                         next = ipfilter6[out][it.iri_active];
6835                         }
6836                 } else {
6837                         fg = fr_findgroup(it.iri_group, IPL_LOGIPF,
6838                                           it.iri_active, NULL);
6839                         if (fg != NULL)
6840                                 next = fg->fg_start;
6841                         else
6842                                 next = NULL;
6843                 }
6844         } else {
6845                 next = fr->fr_next;
6846         }
6847
6848         dst = (char *)it.iri_rule;
6849         count = it.iri_nrules;
6850         /*
6851          * The ipfruleiter may ask for more than 1 rule at a time to be
6852          * copied out, so long as that many exist in the list to start with!
6853          */
6854         for (;;) {
6855                 if (next != NULL) {
6856                         if (count == 1) {
6857                                 MUTEX_ENTER(&next->fr_lock);
6858                                 next->fr_ref++;
6859                                 MUTEX_EXIT(&next->fr_lock);
6860                                 t->ipt_data = next;
6861                         }
6862                 } else {
6863                         bzero(&zero, sizeof(zero));
6864                         next = &zero;
6865                         count = 1;
6866                         t->ipt_data = NULL;
6867                 }
6868                 RWLOCK_EXIT(&ipf_mutex);
6869
6870                 error = COPYOUT(next, dst, sizeof(*next));
6871                 if (error != 0)
6872                         return EFAULT;
6873
6874                 if (next->fr_data != NULL) {
6875                         dst += sizeof(*next);
6876                         error = COPYOUT(next->fr_data, dst, next->fr_dsize);
6877                         if (error != 0)
6878                                 error = EFAULT;
6879                         else
6880                                 dst += next->fr_dsize;
6881                 }
6882
6883                 if ((count == 1) || (error != 0))
6884                         break;
6885
6886                 count--;
6887
6888                 READ_ENTER(&ipf_mutex);
6889                 next = next->fr_next;
6890         }
6891
6892         if (fr != NULL) {
6893                 (void) fr_derefrule(&fr);
6894         }
6895
6896         return error;
6897 }
6898
6899
6900 /* ------------------------------------------------------------------------ */
6901 /* Function:    fr_frruleiter                                               */
6902 /* Returns:     int - 0 = success, else error                               */
6903 /* Parameters:  data(I) - the token type to match                           */
6904 /*              uid(I)  - uid owning the token                              */
6905 /*              ptr(I)  - context pointer for the token                     */
6906 /*                                                                          */
6907 /* This function serves as a stepping stone between fr_ipf_ioctl and        */
6908 /* ipf_getnextrule.  It's role is to find the right token in the kernel for */
6909 /* the process doing the ioctl and use that to ask for the next rule.       */
6910 /* ------------------------------------------------------------------------ */
6911 static int ipf_frruleiter(data, uid, ctx)
6912 void *data, *ctx;
6913 int uid;
6914 {
6915         ipftoken_t *token;
6916         int error;
6917
6918         token = ipf_findtoken(IPFGENITER_IPF, uid, ctx);
6919         if (token != NULL)
6920                 error = ipf_getnextrule(token, data);
6921         else
6922                 error = EFAULT;
6923         RWLOCK_EXIT(&ipf_tokens);
6924
6925         return error;
6926 }
6927
6928
6929 /* ------------------------------------------------------------------------ */
6930 /* Function:    fr_geniter                                                  */
6931 /* Returns:     int - 0 = success, else error                               */
6932 /* Parameters:  token(I) - pointer to ipftoken_t structure                  */
6933 /*              itp(I)   -                                                  */
6934 /*                                                                          */
6935 /* ------------------------------------------------------------------------ */
6936 static int ipf_geniter(token, itp)
6937 ipftoken_t *token;
6938 ipfgeniter_t *itp;
6939 {
6940         int error;
6941
6942         switch (itp->igi_type)
6943         {
6944         case IPFGENITER_FRAG :
6945 #ifdef USE_MUTEXES
6946                 error = fr_nextfrag(token, itp,
6947                                     &ipfr_list, &ipfr_tail, &ipf_frag);
6948 #else
6949                 error = fr_nextfrag(token, itp, &ipfr_list, &ipfr_tail);
6950 #endif
6951                 break;
6952         default :
6953                 error = EINVAL;
6954                 break;
6955         }
6956
6957         return error;
6958 }
6959
6960
6961 /* ------------------------------------------------------------------------ */
6962 /* Function:    fr_genericiter                                              */
6963 /* Returns:     int - 0 = success, else error                               */
6964 /* Parameters:  data(I) - the token type to match                           */
6965 /*              uid(I)  - uid owning the token                              */
6966 /*              ptr(I)  - context pointer for the token                     */
6967 /*                                                                          */
6968 /* ------------------------------------------------------------------------ */
6969 int ipf_genericiter(data, uid, ctx)
6970 void *data, *ctx;
6971 int uid;
6972 {
6973         ipftoken_t *token;
6974         ipfgeniter_t iter;
6975         int error;
6976
6977         error = fr_inobj(data, &iter, IPFOBJ_GENITER);
6978         if (error != 0)
6979                 return error;
6980
6981         token = ipf_findtoken(iter.igi_type, uid, ctx);
6982         if (token != NULL) {
6983                 token->ipt_subtype = iter.igi_type;
6984                 error = ipf_geniter(token, &iter);
6985         } else
6986                 error = EFAULT;
6987         RWLOCK_EXIT(&ipf_tokens);
6988
6989         return error;
6990 }
6991
6992
6993 /* ------------------------------------------------------------------------ */
6994 /* Function:    fr_ipf_ioctl                                                */
6995 /* Returns:     int - 0 = success, else error                               */
6996 /* Parameters:  data(I) - the token type to match                           */
6997 /*              cmd(I)  - the ioctl command number                          */
6998 /*              mode(I) - mode flags for the ioctl                          */
6999 /*              uid(I)  - uid owning the token                              */
7000 /*              ptr(I)  - context pointer for the token                     */
7001 /*                                                                          */
7002 /* This function handles all of the ioctl command that are actually isssued */
7003 /* to the /dev/ipl device.                                                  */
7004 /* ------------------------------------------------------------------------ */
7005 int fr_ipf_ioctl(data, cmd, mode, uid, ctx)
7006 caddr_t data;
7007 ioctlcmd_t cmd;
7008 int mode, uid;
7009 void *ctx;
7010 {
7011         friostat_t fio;
7012         int error, tmp;
7013         SPL_INT(s);
7014
7015         switch (cmd)
7016         {
7017         case SIOCFRENB :
7018                 if (!(mode & FWRITE))
7019                         error = EPERM;
7020                 else {
7021                         error = BCOPYIN(data, &tmp, sizeof(tmp));
7022                         if (error != 0) {
7023                                 error = EFAULT;
7024                                 break;
7025                         }
7026
7027                         WRITE_ENTER(&ipf_global);
7028                         if (tmp) {
7029                                 if (fr_running > 0)
7030                                         error = 0;
7031                                 else
7032                                         error = ipfattach();
7033                                 if (error == 0)
7034                                         fr_running = 1;
7035                                 else
7036                                         (void) ipfdetach();
7037                         } else {
7038                                 error = ipfdetach();
7039                                 if (error == 0)
7040                                         fr_running = -1;
7041                         }
7042                         RWLOCK_EXIT(&ipf_global);
7043                 }
7044                 break;
7045
7046         case SIOCIPFSET :
7047                 if (!(mode & FWRITE)) {
7048                         error = EPERM;
7049                         break;
7050                 }
7051                 /* FALLTHRU */
7052         case SIOCIPFGETNEXT :
7053         case SIOCIPFGET :
7054                 error = fr_ipftune(cmd, (void *)data);
7055                 break;
7056
7057         case SIOCSETFF :
7058                 if (!(mode & FWRITE))
7059                         error = EPERM;
7060                 else {
7061                         error = BCOPYIN(data, &fr_flags, sizeof(fr_flags));
7062                         if (error != 0)
7063                                 error = EFAULT;
7064                 }
7065                 break;
7066
7067         case SIOCGETFF :
7068                 error = BCOPYOUT(&fr_flags, data, sizeof(fr_flags));
7069                 if (error != 0)
7070                         error = EFAULT;
7071                 break;
7072
7073         case SIOCFUNCL :
7074                 error = fr_resolvefunc((void *)data);
7075                 break;
7076
7077         case SIOCINAFR :
7078         case SIOCRMAFR :
7079         case SIOCADAFR :
7080         case SIOCZRLST :
7081                 if (!(mode & FWRITE))
7082                         error = EPERM;
7083                 else
7084                         error = frrequest(IPL_LOGIPF, cmd, data, fr_active, 1);
7085                 break;
7086
7087         case SIOCINIFR :
7088         case SIOCRMIFR :
7089         case SIOCADIFR :
7090                 if (!(mode & FWRITE))
7091                         error = EPERM;
7092                 else
7093                         error = frrequest(IPL_LOGIPF, cmd, data,
7094                                           1 - fr_active, 1);
7095                 break;
7096
7097         case SIOCSWAPA :
7098                 if (!(mode & FWRITE))
7099                         error = EPERM;
7100                 else {
7101                         WRITE_ENTER(&ipf_mutex);
7102                         bzero((char *)frcache, sizeof(frcache[0]) * 2);
7103                         error = BCOPYOUT(&fr_active, data, sizeof(fr_active));
7104                         if (error != 0)
7105                                 error = EFAULT;
7106                         else
7107                                 fr_active = 1 - fr_active;
7108                         RWLOCK_EXIT(&ipf_mutex);
7109                 }
7110                 break;
7111
7112         case SIOCGETFS :
7113                 fr_getstat(&fio);
7114                 error = fr_outobj((void *)data, &fio, IPFOBJ_IPFSTAT);
7115                 break;
7116
7117         case SIOCFRZST :
7118                 if (!(mode & FWRITE))
7119                         error = EPERM;
7120                 else
7121                         error = fr_zerostats(data);
7122                 break;
7123
7124         case SIOCIPFFL :
7125                 if (!(mode & FWRITE))
7126                         error = EPERM;
7127                 else {
7128                         error = BCOPYIN(data, &tmp, sizeof(tmp));
7129                         if (!error) {
7130                                 tmp = frflush(IPL_LOGIPF, 4, tmp);
7131                                 error = BCOPYOUT(&tmp, data, sizeof(tmp));
7132                                 if (error != 0)
7133                                         error = EFAULT;
7134                         } else
7135                                 error = EFAULT;
7136                 }
7137                 break;
7138
7139 #ifdef USE_INET6
7140         case SIOCIPFL6 :
7141                 if (!(mode & FWRITE))
7142                         error = EPERM;
7143                 else {
7144                         error = BCOPYIN(data, &tmp, sizeof(tmp));
7145                         if (!error) {
7146                                 tmp = frflush(IPL_LOGIPF, 6, tmp);
7147                                 error = BCOPYOUT(&tmp, data, sizeof(tmp));
7148                                 if (error != 0)
7149                                         error = EFAULT;
7150                         } else
7151                                 error = EFAULT;
7152                 }
7153                 break;
7154 #endif
7155
7156         case SIOCSTLCK :
7157                 error = BCOPYIN(data, &tmp, sizeof(tmp));
7158                 if (error == 0) {
7159                         fr_state_lock = tmp;
7160                         fr_nat_lock = tmp;
7161                         fr_frag_lock = tmp;
7162                         fr_auth_lock = tmp;
7163                 } else
7164                         error = EFAULT;
7165                 break;
7166
7167 #ifdef  IPFILTER_LOG
7168         case SIOCIPFFB :
7169                 if (!(mode & FWRITE))
7170                         error = EPERM;
7171                 else {
7172                         tmp = ipflog_clear(IPL_LOGIPF);
7173                         error = BCOPYOUT(&tmp, data, sizeof(tmp));
7174                         if (error)
7175                                 error = EFAULT;
7176                 }
7177                 break;
7178 #endif /* IPFILTER_LOG */
7179
7180         case SIOCFRSYN :
7181                 if (!(mode & FWRITE))
7182                         error = EPERM;
7183                 else {
7184                         WRITE_ENTER(&ipf_global);
7185 #ifdef MENTAT
7186                         error = ipfsync();
7187 #else
7188                         frsync(NULL);
7189                         error = 0;
7190 #endif
7191                         RWLOCK_EXIT(&ipf_global);
7192
7193                 }
7194                 break;
7195
7196         case SIOCGFRST :
7197                 error = fr_outobj((void *)data, fr_fragstats(),
7198                                   IPFOBJ_FRAGSTAT);
7199                 break;
7200
7201 #ifdef  IPFILTER_LOG
7202         case FIONREAD :
7203                 tmp = (int)iplused[IPL_LOGIPF];
7204
7205                 error = BCOPYOUT(&tmp, data, sizeof(tmp));
7206                 break;
7207 #endif
7208
7209         case SIOCIPFITER :
7210                 SPL_SCHED(s);
7211                 error = ipf_frruleiter(data, uid, ctx);
7212                 SPL_X(s);
7213                 break;
7214
7215         case SIOCGENITER :
7216                 SPL_SCHED(s);
7217                 error = ipf_genericiter(data, uid, ctx);
7218                 SPL_X(s);
7219                 break;
7220
7221         case SIOCIPFDELTOK :
7222                 SPL_SCHED(s);
7223                 error = BCOPYIN(data, &tmp, sizeof(tmp));
7224                 if (error == 0)
7225                         error = ipf_deltoken(tmp, uid, ctx);
7226                 SPL_X(s);
7227                 break;
7228
7229         default :
7230                 error = EINVAL;
7231                 break;
7232         }
7233
7234         return error;
7235 }
7236
7237
7238 /* ------------------------------------------------------------------------ */
7239 /* Function:    ipf_queueflush                                              */
7240 /* Returns:     int - number of entries flushed (0 = none)                  */
7241 /* Parameters:  deletefn(I) - function to call to delete entry              */
7242 /*              ipfqs(I)    - top of the list of ipf internal queues        */
7243 /*              userqs(I)   - top of the list of user defined timeouts      */
7244 /*                                                                          */
7245 /* This fucntion gets called when the state/NAT hash tables fill up and we  */
7246 /* need to try a bit harder to free up some space.  The algorithm used is   */
7247 /* to look for the oldest entries on each timeout queue and free them if    */
7248 /* they are within the given window we are considering.  Where the window   */
7249 /* starts and the steps taken to increase its size depend upon how long ipf */
7250 /* has been running (fr_ticks.)  Anything modified in the last 30 seconds   */
7251 /* is not touched.                                                          */
7252 /*                                              touched                     */
7253 /*         die     fr_ticks   30*1.5    1800*1.5   |  43200*1.5             */
7254 /*           |          |        |           |     |     |                  */
7255 /* future <--+----------+--------+-----------+-----+-----+-----------> past */
7256 /*                     now        \_int=30s_/ \_int=1hr_/ \_int=12hr        */
7257 /*                                                                          */
7258 /* Points to note:                                                          */
7259 /* - tqe_die is the time, in the future, when entries die.                  */
7260 /* - tqe_die - fr_ticks is how long left the connection has to live in ipf  */
7261 /*   ticks.                                                                 */
7262 /* - tqe_touched is when the entry was last used by NAT/state               */
7263 /* - the closer tqe_touched is to fr_ticks, the further tqe_die will be for */
7264 /*   any given timeout queue and vice versa.                                */
7265 /* - both tqe_die and tqe_touched increase over time                        */
7266 /* - timeout queues are sorted with the highest value of tqe_die at the     */
7267 /*   bottom and therefore the smallest values of each are at the top        */
7268 /*                                                                          */
7269 /* We start by setting up a maximum range to scan for things to move of     */
7270 /* iend (newest) to istart (oldest) in chunks of "interval".  If nothing is */
7271 /* found in that range, "interval" is adjusted (so long as it isn't 30) and */
7272 /* we start again with a new value for "iend" and "istart".  The downside   */
7273 /* of the current implementation is that it may return removing just 1 entry*/
7274 /* every time (pathological case) where it could remove more.               */
7275 /* ------------------------------------------------------------------------ */
7276 int ipf_queueflush(deletefn, ipfqs, userqs)
7277 ipftq_delete_fn_t deletefn;
7278 ipftq_t *ipfqs, *userqs;
7279 {
7280         u_long interval, istart, iend;
7281         ipftq_t *ifq, *ifqnext;
7282         ipftqent_t *tqe, *tqn;
7283         int removed;
7284
7285         /*
7286          * NOTE: Use of "* 15 / 10" is required here because if "* 1.5" is
7287          *       used then the operations are upgraded to floating point
7288          *       and kernels don't like floating point...
7289          */
7290         if (fr_ticks > IPF_TTLVAL(43200 * 15 / 10)) {
7291                 istart = IPF_TTLVAL(86400 * 4);
7292                 interval = IPF_TTLVAL(43200);
7293         } else if (fr_ticks > IPF_TTLVAL(1800 * 15 / 10)) {
7294                 istart = IPF_TTLVAL(43200);
7295                 interval = IPF_TTLVAL(1800);
7296         } else if (fr_ticks > IPF_TTLVAL(30 * 15 / 10)) {
7297                 istart = IPF_TTLVAL(1800);
7298                 interval = IPF_TTLVAL(30);
7299         } else {
7300                 return 0;
7301         }
7302         if (istart > fr_ticks) {
7303                 if (fr_ticks - interval < interval)
7304                         istart = interval;
7305                 else
7306                         istart = (fr_ticks / interval) * interval;
7307         }
7308
7309         iend = fr_ticks - interval;
7310         removed = 0;
7311
7312         for (;;) {
7313                 u_long try;
7314
7315                 try = fr_ticks - istart; 
7316
7317                 for (ifq = ipfqs; ifq != NULL; ifq = ifq->ifq_next) {
7318                         for (tqn = ifq->ifq_head; ((tqe = tqn) != NULL); ) {
7319                                 if (try < tqe->tqe_touched)
7320                                         break;
7321                                 tqn = tqe->tqe_next;
7322                                 if ((*deletefn)(tqe->tqe_parent) == 0)
7323                                         removed++;
7324                         }
7325                 }
7326
7327                 for (ifq = userqs; ifq != NULL; ifq = ifqnext) {
7328                         ifqnext = ifq->ifq_next;
7329
7330                         for (tqn = ifq->ifq_head; ((tqe = tqn) != NULL); ) {
7331                                 if (try < tqe->tqe_touched)
7332                                         break;
7333                                 tqn = tqe->tqe_next;
7334                                 if ((*deletefn)(tqe->tqe_parent) == 0)
7335                                         removed++;
7336                         }
7337                 }
7338
7339                 if (try >= iend) {
7340                         if (removed > 0)
7341                                 break;
7342                         if (interval == IPF_TTLVAL(43200)) {
7343                                 interval = IPF_TTLVAL(1800);
7344                         } else if (interval == IPF_TTLVAL(1800)) {
7345                                 interval = IPF_TTLVAL(30);
7346                         } else {
7347                                 break;
7348                         }
7349                         if (interval >= fr_ticks)
7350                                 break;
7351
7352                         iend = fr_ticks - interval;
7353                 }
7354                 istart -= interval;
7355         }
7356
7357         return removed;
7358 }