Deleted Added
full compact
1/*
2 * Copyright (C) 1993-2001 by Darren Reed.
3 *
4 * See the IPFILTER.LICENCE file for details on licencing.
5 */
6#ifdef __sgi
7# include <sys/ptimers.h>
8#endif
9#include <sys/errno.h>
10#include <sys/types.h>
11#include <sys/param.h>
12#include <sys/time.h>
13#include <sys/file.h>
14#if defined(__NetBSD__) && (NetBSD >= 199905) && !defined(IPFILTER_LKM) && \
15 defined(_KERNEL)
16# include "opt_ipfilter_log.h"
17#endif
18#if (defined(KERNEL) || defined(_KERNEL)) && defined(__FreeBSD_version) && \
19 (__FreeBSD_version >= 220000)
20# if (__FreeBSD_version >= 400000)
21# ifndef KLD_MODULE
22# include "opt_inet6.h"
23# endif
24# if (__FreeBSD_version == 400019)
25# define CSUM_DELAY_DATA
26# endif
27# endif
28# include <sys/filio.h>
29# include <sys/fcntl.h>
30#else
31# include <sys/ioctl.h>
32#endif
33#if (defined(_KERNEL) || defined(KERNEL)) && !defined(linux)
34# include <sys/systm.h>
35#else
36# include <stdio.h>
37# include <string.h>
38# include <stdlib.h>
39#endif
40#if !defined(__SVR4) && !defined(__svr4__)
41# ifndef linux
42# include <sys/mbuf.h>
43# endif
44#else
45# include <sys/byteorder.h>
46# if SOLARIS2 < 5
47# include <sys/dditypes.h>
48# endif
49# include <sys/stream.h>
50#endif
51#ifndef linux
52# include <sys/protosw.h>
53# include <sys/socket.h>
54#endif
55#include <net/if.h>
56#ifdef sun
57# include <net/af.h>
58#endif
59#include <net/route.h>
60#include <netinet/in.h>
61#include <netinet/in_systm.h>
62#include <netinet/ip.h>
63#ifndef linux
64# include <netinet/ip_var.h>
65#endif
66#if defined(__sgi) && defined(IFF_DRVRLOCK) /* IRIX 6 */
67# include <sys/hashing.h>
68# include <netinet/in_var.h>
69#endif
70#include <netinet/tcp.h>
71#include <netinet/udp.h>
72#include <netinet/ip_icmp.h>
73#include "netinet/ip_compat.h"
74#ifdef USE_INET6
75# include <netinet/icmp6.h>
76# if !SOLARIS && defined(_KERNEL)
77# include <netinet6/in6_var.h>
78# endif
79#endif
80#include <netinet/tcpip.h>
81#include "netinet/ip_fil.h"
82#include "netinet/ip_nat.h"
83#include "netinet/ip_frag.h"
84#include "netinet/ip_state.h"
85#include "netinet/ip_proxy.h"
86#include "netinet/ip_auth.h"
87# if defined(__FreeBSD_version) && (__FreeBSD_version >= 300000)
88# include <sys/malloc.h>
89# if defined(_KERNEL) && !defined(IPFILTER_LKM)
90# include "opt_ipfilter.h"
91# endif
92# endif
93#ifndef MIN
94# define MIN(a,b) (((a)<(b))?(a):(b))
95#endif
96#include "netinet/ipl.h"
97
98#include <machine/in_cksum.h>
99
100#if !defined(lint)
101static const char sccsid[] = "@(#)fil.c 1.36 6/5/96 (C) 1993-2000 Darren Reed";
102static const char rcsid[] = "@(#)$FreeBSD: head/sys/contrib/ipfilter/netinet/fil.c 92685 2002-03-19 11:44:16Z darrenr $";
102static const char rcsid[] = "@(#)$FreeBSD: head/sys/contrib/ipfilter/netinet/fil.c 95418 2002-04-25 03:31:39Z darrenr $";
103#endif
104
105#ifndef _KERNEL
106# include "ipf.h"
107# include "ipt.h"
108extern int opts;
109
110# define FR_VERBOSE(verb_pr) verbose verb_pr
111# define FR_DEBUG(verb_pr) debug verb_pr
112# define IPLLOG(a, c, d, e) ipflog(a, c, d, e)
113#else /* #ifndef _KERNEL */
114# define FR_VERBOSE(verb_pr)
115# define FR_DEBUG(verb_pr)
116# define IPLLOG(a, c, d, e) ipflog(a, c, d, e)
117# if SOLARIS || defined(__sgi)
118extern KRWLOCK_T ipf_mutex, ipf_auth, ipf_nat;
119extern kmutex_t ipf_rw;
120# endif /* SOLARIS || __sgi */
121#endif /* _KERNEL */
122
123
124struct filterstats frstats[2] = {{0,0,0,0,0},{0,0,0,0,0}};
125struct frentry *ipfilter[2][2] = { { NULL, NULL }, { NULL, NULL } },
126#ifdef USE_INET6
127 *ipfilter6[2][2] = { { NULL, NULL }, { NULL, NULL } },
128 *ipacct6[2][2] = { { NULL, NULL }, { NULL, NULL } },
129#endif
130 *ipacct[2][2] = { { NULL, NULL }, { NULL, NULL } };
131struct frgroup *ipfgroups[3][2];
132int fr_flags = IPF_LOGGING;
133int fr_active = 0;
134int fr_chksrc = 0;
135int fr_minttl = 3;
136int fr_minttllog = 1;
137#if defined(IPFILTER_DEFAULT_BLOCK)
138int fr_pass = FR_NOMATCH|FR_BLOCK;
139#else
140int fr_pass = (IPF_DEFAULT_PASS|FR_NOMATCH);
141#endif
142char ipfilter_version[] = IPL_VERSION;
143
144fr_info_t frcache[2];
145
146static int frflushlist __P((int, minor_t, int *, frentry_t **));
147#ifdef _KERNEL
148static void frsynclist __P((frentry_t *));
149#endif
150
151
152/*
153 * bit values for identifying presence of individual IP options
154 */
155struct optlist ipopts[20] = {
156 { IPOPT_NOP, 0x000001 },
157 { IPOPT_RR, 0x000002 },
158 { IPOPT_ZSU, 0x000004 },
159 { IPOPT_MTUP, 0x000008 },
160 { IPOPT_MTUR, 0x000010 },
161 { IPOPT_ENCODE, 0x000020 },
162 { IPOPT_TS, 0x000040 },
163 { IPOPT_TR, 0x000080 },
164 { IPOPT_SECURITY, 0x000100 },
165 { IPOPT_LSRR, 0x000200 },
166 { IPOPT_E_SEC, 0x000400 },
167 { IPOPT_CIPSO, 0x000800 },
168 { IPOPT_SATID, 0x001000 },
169 { IPOPT_SSRR, 0x002000 },
170 { IPOPT_ADDEXT, 0x004000 },
171 { IPOPT_VISA, 0x008000 },
172 { IPOPT_IMITD, 0x010000 },
173 { IPOPT_EIP, 0x020000 },
174 { IPOPT_FINN, 0x040000 },
175 { 0, 0x000000 }
176};
177
178/*
179 * bit values for identifying presence of individual IP security options
180 */
181struct optlist secopt[8] = {
182 { IPSO_CLASS_RES4, 0x01 },
183 { IPSO_CLASS_TOPS, 0x02 },
184 { IPSO_CLASS_SECR, 0x04 },
185 { IPSO_CLASS_RES3, 0x08 },
186 { IPSO_CLASS_CONF, 0x10 },
187 { IPSO_CLASS_UNCL, 0x20 },
188 { IPSO_CLASS_RES2, 0x40 },
189 { IPSO_CLASS_RES1, 0x80 }
190};
191
192
193/*
194 * compact the IP header into a structure which contains just the info.
195 * which is useful for comparing IP headers with.
196 */
197void fr_makefrip(hlen, ip, fin)
198int hlen;
199ip_t *ip;
200fr_info_t *fin;
201{
202 u_short optmsk = 0, secmsk = 0, auth = 0;
203 int i, mv, ol, off, p, plen, v;
204 fr_ip_t *fi = &fin->fin_fi;
205 struct optlist *op;
206 u_char *s, opt;
207 tcphdr_t *tcp;
208
209 fin->fin_rev = 0;
210 fin->fin_fr = NULL;
211 fin->fin_tcpf = 0;
212 fin->fin_data[0] = 0;
213 fin->fin_data[1] = 0;
214 fin->fin_rule = -1;
215 fin->fin_group = -1;
216#ifdef _KERNEL
216 fin->fin_icode = ipl_unreach;
218#endif
217 v = fin->fin_v;
218 fi->fi_v = v;
219 fin->fin_hlen = hlen;
220 if (v == 4) {
221 fin->fin_id = ip->ip_id;
222 fi->fi_tos = ip->ip_tos;
223 off = (ip->ip_off & IP_OFFMASK);
224 tcp = (tcphdr_t *)((char *)ip + hlen);
225 (*(((u_short *)fi) + 1)) = (*(((u_short *)ip) + 4));
226 fi->fi_src.i6[1] = 0;
227 fi->fi_src.i6[2] = 0;
228 fi->fi_src.i6[3] = 0;
229 fi->fi_dst.i6[1] = 0;
230 fi->fi_dst.i6[2] = 0;
231 fi->fi_dst.i6[3] = 0;
232 fi->fi_saddr = ip->ip_src.s_addr;
233 fi->fi_daddr = ip->ip_dst.s_addr;
234 p = ip->ip_p;
235 fi->fi_fl = (hlen > sizeof(ip_t)) ? FI_OPTIONS : 0;
236 if (ip->ip_off & (IP_MF|IP_OFFMASK))
237 fi->fi_fl |= FI_FRAG;
238 plen = ip->ip_len;
239 fin->fin_dlen = plen - hlen;
240 }
241#ifdef USE_INET6
242 else if (v == 6) {
243 ip6_t *ip6 = (ip6_t *)ip;
244
245 off = 0;
246 p = ip6->ip6_nxt;
247 fi->fi_p = p;
248 fi->fi_ttl = ip6->ip6_hlim;
249 tcp = (tcphdr_t *)(ip6 + 1);
250 fi->fi_src.in6 = ip6->ip6_src;
251 fi->fi_dst.in6 = ip6->ip6_dst;
252 fin->fin_id = (u_short)(ip6->ip6_flow & 0xffff);
253 fi->fi_tos = 0;
254 fi->fi_fl = 0;
255 plen = ntohs(ip6->ip6_plen);
256 fin->fin_dlen = plen;
257 plen += sizeof(*ip6);
258 }
259#endif
260 else
261 return;
262
263 fin->fin_off = off;
264 fin->fin_plen = plen;
265 fin->fin_dp = (char *)tcp;
266 fin->fin_misc = 0;
267 off <<= 3;
268
269 switch (p)
270 {
271#ifdef USE_INET6
272 case IPPROTO_ICMPV6 :
273 {
274 int minicmpsz = sizeof(struct icmp6_hdr);
275 struct icmp6_hdr *icmp6;
276
277 if (fin->fin_dlen > 1) {
278 fin->fin_data[0] = *(u_short *)tcp;
279
280 icmp6 = (struct icmp6_hdr *)tcp;
281
282 switch (icmp6->icmp6_type)
283 {
284 case ICMP6_ECHO_REPLY :
285 case ICMP6_ECHO_REQUEST :
286 minicmpsz = ICMP6_MINLEN;
287 break;
288 case ICMP6_DST_UNREACH :
289 case ICMP6_PACKET_TOO_BIG :
290 case ICMP6_TIME_EXCEEDED :
291 case ICMP6_PARAM_PROB :
292 minicmpsz = ICMP6ERR_IPICMPHLEN;
293 break;
294 default :
295 break;
296 }
297 }
298
300 if (!(plen >= hlen + minicmpsz))
299 if (!(plen >= minicmpsz))
300 fi->fi_fl |= FI_SHORT;
301
302 break;
303 }
304#endif
305 case IPPROTO_ICMP :
306 {
307 int minicmpsz = sizeof(struct icmp);
308 icmphdr_t *icmp;
309
310 if (!off && (fin->fin_dlen > 1)) {
311 fin->fin_data[0] = *(u_short *)tcp;
312
313 icmp = (icmphdr_t *)tcp;
314
315 switch (icmp->icmp_type)
316 {
317 case ICMP_ECHOREPLY :
318 case ICMP_ECHO :
319 /* Router discovery messages - RFC 1256 */
320 case ICMP_ROUTERADVERT :
321 case ICMP_ROUTERSOLICIT :
322 minicmpsz = ICMP_MINLEN;
323 break;
324 /*
325 * type(1) + code(1) + cksum(2) + id(2) seq(2) +
326 * 3*timestamp(3*4)
327 */
328 case ICMP_TSTAMP :
329 case ICMP_TSTAMPREPLY :
330 minicmpsz = 20;
331 break;
332 /*
333 * type(1) + code(1) + cksum(2) + id(2) seq(2) +
334 * mask(4)
335 */
336 case ICMP_MASKREQ :
337 case ICMP_MASKREPLY :
338 minicmpsz = 12;
339 break;
340 default :
341 break;
342 }
343 }
344
345 if ((!(plen >= hlen + minicmpsz) && !off) ||
346 (off && off < sizeof(struct icmp)))
347 fi->fi_fl |= FI_SHORT;
348
349 break;
350 }
351 case IPPROTO_TCP :
352 fi->fi_fl |= FI_TCPUDP;
353#ifdef USE_INET6
354 if (v == 6) {
355 if (plen < sizeof(struct tcphdr))
356 fi->fi_fl |= FI_SHORT;
357 } else
358#endif
359 if (v == 4) {
360 if ((!IPMINLEN(ip, tcphdr) && !off) ||
361 (off && off < sizeof(struct tcphdr)))
362 fi->fi_fl |= FI_SHORT;
363 }
364 if (!(fi->fi_fl & FI_SHORT) && !off)
365 fin->fin_tcpf = tcp->th_flags;
366 goto getports;
367 case IPPROTO_UDP :
368 fi->fi_fl |= FI_TCPUDP;
369#ifdef USE_INET6
370 if (v == 6) {
371 if (plen < sizeof(struct udphdr))
372 fi->fi_fl |= FI_SHORT;
373 } else
374#endif
375 if (v == 4) {
376 if ((!IPMINLEN(ip, udphdr) && !off) ||
377 (off && off < sizeof(struct udphdr)))
378 fi->fi_fl |= FI_SHORT;
379 }
380getports:
381 if (!off && (fin->fin_dlen > 3)) {
382 fin->fin_data[0] = ntohs(tcp->th_sport);
383 fin->fin_data[1] = ntohs(tcp->th_dport);
384 }
385 break;
386 case IPPROTO_ESP :
387#ifdef USE_INET6
388 if (v == 6) {
389 if (plen < 8)
390 fi->fi_fl |= FI_SHORT;
391 } else
392#endif
393 if (v == 4) {
394 if (((ip->ip_len < hlen + 8) && !off) ||
395 (off && off < 8))
396 fi->fi_fl |= FI_SHORT;
397 }
398 break;
399 default :
400 break;
401 }
402
403#ifdef USE_INET6
404 if (v == 6) {
405 fi->fi_optmsk = 0;
406 fi->fi_secmsk = 0;
407 fi->fi_auth = 0;
408 return;
409 }
410#endif
411
412 for (s = (u_char *)(ip + 1), hlen -= (int)sizeof(*ip); hlen > 0; ) {
413 opt = *s;
414 if (opt == '\0')
415 break;
416 else if (opt == IPOPT_NOP)
417 ol = 1;
418 else {
419 if (hlen < 2)
420 break;
421 ol = (int)*(s + 1);
422 if (ol < 2 || ol > hlen)
423 break;
424 }
425 for (i = 9, mv = 4; mv >= 0; ) {
426 op = ipopts + i;
427 if (opt == (u_char)op->ol_val) {
428 optmsk |= op->ol_bit;
429 if (opt == IPOPT_SECURITY) {
430 struct optlist *sp;
431 u_char sec;
432 int j, m;
433
434 sec = *(s + 2); /* classification */
435 for (j = 3, m = 2; m >= 0; ) {
436 sp = secopt + j;
437 if (sec == sp->ol_val) {
438 secmsk |= sp->ol_bit;
439 auth = *(s + 3);
440 auth *= 256;
441 auth += *(s + 4);
442 break;
443 }
444 if (sec < sp->ol_val)
445 j -= m--;
446 else
447 j += m--;
448 }
449 }
450 break;
451 }
452 if (opt < op->ol_val)
453 i -= mv--;
454 else
455 i += mv--;
456 }
457 hlen -= ol;
458 s += ol;
459 }
460 if (auth && !(auth & 0x0100))
461 auth &= 0xff00;
462 fi->fi_optmsk = optmsk;
463 fi->fi_secmsk = secmsk;
464 fi->fi_auth = auth;
465}
466
467
468/*
469 * check an IP packet for TCP/UDP characteristics such as ports and flags.
470 */
471int fr_tcpudpchk(ft, fin)
472frtuc_t *ft;
473fr_info_t *fin;
474{
475 register u_short po, tup;
476 register char i;
477 register int err = 1;
478
479 /*
480 * Both ports should *always* be in the first fragment.
481 * So far, I cannot find any cases where they can not be.
482 *
483 * compare destination ports
484 */
485 if ((i = (int)ft->ftu_dcmp)) {
486 po = ft->ftu_dport;
487 tup = fin->fin_data[1];
488 /*
489 * Do opposite test to that required and
490 * continue if that succeeds.
491 */
492 if (!--i && tup != po) /* EQUAL */
493 err = 0;
494 else if (!--i && tup == po) /* NOTEQUAL */
495 err = 0;
496 else if (!--i && tup >= po) /* LESSTHAN */
497 err = 0;
498 else if (!--i && tup <= po) /* GREATERTHAN */
499 err = 0;
500 else if (!--i && tup > po) /* LT or EQ */
501 err = 0;
502 else if (!--i && tup < po) /* GT or EQ */
503 err = 0;
504 else if (!--i && /* Out of range */
505 (tup >= po && tup <= ft->ftu_dtop))
506 err = 0;
507 else if (!--i && /* In range */
508 (tup <= po || tup >= ft->ftu_dtop))
509 err = 0;
510 }
511 /*
512 * compare source ports
513 */
514 if (err && (i = (int)ft->ftu_scmp)) {
515 po = ft->ftu_sport;
516 tup = fin->fin_data[0];
517 if (!--i && tup != po)
518 err = 0;
519 else if (!--i && tup == po)
520 err = 0;
521 else if (!--i && tup >= po)
522 err = 0;
523 else if (!--i && tup <= po)
524 err = 0;
525 else if (!--i && tup > po)
526 err = 0;
527 else if (!--i && tup < po)
528 err = 0;
529 else if (!--i && /* Out of range */
530 (tup >= po && tup <= ft->ftu_stop))
531 err = 0;
532 else if (!--i && /* In range */
533 (tup <= po || tup >= ft->ftu_stop))
534 err = 0;
535 }
536
537 /*
538 * If we don't have all the TCP/UDP header, then how can we
539 * expect to do any sort of match on it ? If we were looking for
540 * TCP flags, then NO match. If not, then match (which should
541 * satisfy the "short" class too).
542 */
543 if (err && (fin->fin_fi.fi_p == IPPROTO_TCP)) {
544 if (fin->fin_fl & FI_SHORT)
545 return !(ft->ftu_tcpf | ft->ftu_tcpfm);
546 /*
547 * Match the flags ? If not, abort this match.
548 */
549 if (ft->ftu_tcpfm &&
550 ft->ftu_tcpf != (fin->fin_tcpf & ft->ftu_tcpfm)) {
551 FR_DEBUG(("f. %#x & %#x != %#x\n", fin->fin_tcpf,
552 ft->ftu_tcpfm, ft->ftu_tcpf));
553 err = 0;
554 }
555 }
556 return err;
557}
558
559/*
560 * Check the input/output list of rules for a match and result.
561 * Could be per interface, but this gets real nasty when you don't have
562 * kernel sauce.
563 */
564int fr_scanlist(passin, ip, fin, m)
565u_32_t passin;
566ip_t *ip;
567register fr_info_t *fin;
568void *m;
569{
570 register struct frentry *fr;
571 register fr_ip_t *fi = &fin->fin_fi;
572 int rulen, portcmp = 0, off, skip = 0, logged = 0;
573 u_32_t pass, passt, passl;
574 frentry_t *frl;
575
576 frl = NULL;
577 pass = passin;
578 fr = fin->fin_fr;
579 fin->fin_fr = NULL;
580 off = fin->fin_off;
581
582 if ((fi->fi_fl & FI_TCPUDP) && (fin->fin_dlen > 3) && !off)
583 portcmp = 1;
584
585 for (rulen = 0; fr; fr = fr->fr_next, rulen++) {
586 if (skip) {
587 FR_VERBOSE(("%d (%#x)\n", skip, fr->fr_flags));
588 skip--;
589 continue;
590 }
591 /*
592 * In all checks below, a null (zero) value in the
593 * filter struture is taken to mean a wildcard.
594 *
595 * check that we are working for the right interface
596 */
597#ifdef _KERNEL
598# if (BSD >= 199306)
599 if (fin->fin_out != 0) {
600 if ((fr->fr_oifa &&
601 (fr->fr_oifa != ((mb_t *)m)->m_pkthdr.rcvif)))
602 continue;
603 }
604# endif
605#else
606 if (opts & (OPT_VERBOSE|OPT_DEBUG))
607 printf("\n");
608#endif
609
610 FR_VERBOSE(("%c", fr->fr_skip ? 's' :
611 (pass & FR_PASS) ? 'p' :
612 (pass & FR_AUTH) ? 'a' :
613 (pass & FR_ACCOUNT) ? 'A' :
614 (pass & FR_NOMATCH) ? 'n' : 'b'));
615
616 if (fr->fr_ifa && fr->fr_ifa != fin->fin_ifp)
617 continue;
618
619 FR_VERBOSE((":i"));
620 {
621 register u_32_t *ld, *lm, *lip;
622 register int i;
623
624 lip = (u_32_t *)fi;
625 lm = (u_32_t *)&fr->fr_mip;
626 ld = (u_32_t *)&fr->fr_ip;
627 i = ((*lip & *lm) != *ld);
628 FR_DEBUG(("0. %#08x & %#08x != %#08x\n",
629 *lip, *lm, *ld));
630 if (i)
631 continue;
632 /*
633 * We now know whether the packet version and the
634 * rule version match, along with protocol, ttl and
635 * tos.
636 */
637 lip++, lm++, ld++;
638 /*
639 * Unrolled loops (4 each, for 32 bits).
640 */
641 FR_DEBUG(("1a. %#08x & %#08x != %#08x\n",
642 *lip, *lm, *ld));
643 i |= ((*lip++ & *lm++) != *ld++) << 5;
644 if (fi->fi_v == 6) {
645 FR_DEBUG(("1b. %#08x & %#08x != %#08x\n",
646 *lip, *lm, *ld));
647 i |= ((*lip++ & *lm++) != *ld++) << 5;
648 FR_DEBUG(("1c. %#08x & %#08x != %#08x\n",
649 *lip, *lm, *ld));
650 i |= ((*lip++ & *lm++) != *ld++) << 5;
651 FR_DEBUG(("1d. %#08x & %#08x != %#08x\n",
652 *lip, *lm, *ld));
653 i |= ((*lip++ & *lm++) != *ld++) << 5;
654 } else {
655 lip += 3;
656 lm += 3;
657 ld += 3;
658 }
659 i ^= (fr->fr_flags & FR_NOTSRCIP);
660 if (i)
661 continue;
662 FR_DEBUG(("2a. %#08x & %#08x != %#08x\n",
663 *lip, *lm, *ld));
664 i |= ((*lip++ & *lm++) != *ld++) << 6;
665 if (fi->fi_v == 6) {
666 FR_DEBUG(("2b. %#08x & %#08x != %#08x\n",
667 *lip, *lm, *ld));
668 i |= ((*lip++ & *lm++) != *ld++) << 6;
669 FR_DEBUG(("2c. %#08x & %#08x != %#08x\n",
670 *lip, *lm, *ld));
671 i |= ((*lip++ & *lm++) != *ld++) << 6;
672 FR_DEBUG(("2d. %#08x & %#08x != %#08x\n",
673 *lip, *lm, *ld));
674 i |= ((*lip++ & *lm++) != *ld++) << 6;
675 } else {
676 lip += 3;
677 lm += 3;
678 ld += 3;
679 }
680 i ^= (fr->fr_flags & FR_NOTDSTIP);
681 if (i)
682 continue;
683 FR_DEBUG(("3. %#08x & %#08x != %#08x\n",
684 *lip, *lm, *ld));
685 i |= ((*lip++ & *lm++) != *ld++);
686 FR_DEBUG(("4. %#08x & %#08x != %#08x\n",
687 *lip, *lm, *ld));
688 i |= ((*lip & *lm) != *ld);
689 if (i)
690 continue;
691 }
692
693 /*
694 * If a fragment, then only the first has what we're looking
695 * for here...
696 */
697 if (!portcmp && (fr->fr_dcmp || fr->fr_scmp || fr->fr_tcpf ||
698 fr->fr_tcpfm))
699 continue;
700 if (fi->fi_fl & FI_TCPUDP) {
701 if (!fr_tcpudpchk(&fr->fr_tuc, fin))
702 continue;
703 } else if (fr->fr_icmpm || fr->fr_icmp) {
704 if ((fi->fi_p != IPPROTO_ICMP) || off ||
705 (fin->fin_dlen < 2))
706 continue;
707 if ((fin->fin_data[0] & fr->fr_icmpm) != fr->fr_icmp) {
708 FR_DEBUG(("i. %#x & %#x != %#x\n",
709 fin->fin_data[0], fr->fr_icmpm,
710 fr->fr_icmp));
711 continue;
712 }
713 }
714 FR_VERBOSE(("*"));
715
716 if (fr->fr_flags & FR_NOMATCH) {
717 passt = passl;
718 passl = passin;
719 fin->fin_fr = frl;
720 frl = NULL;
721 if (fr->fr_flags & FR_QUICK)
722 break;
723 continue;
724 }
725
726 passl = passt;
727 passt = fr->fr_flags;
728 frl = fin->fin_fr;
729 fin->fin_fr = fr;
730#if (BSD >= 199306) && (defined(_KERNEL) || defined(KERNEL))
731 if (securelevel <= 0)
732#endif
733 if ((passt & FR_CALLNOW) && fr->fr_func)
734 passt = (*fr->fr_func)(passt, ip, fin);
735#ifdef IPFILTER_LOG
736 /*
737 * Just log this packet...
738 */
739 if ((passt & FR_LOGMASK) == FR_LOG) {
740 if (!IPLLOG(passt, ip, fin, m)) {
741 if (passt & FR_LOGORBLOCK)
742 passt |= FR_BLOCK|FR_QUICK;
743 ATOMIC_INCL(frstats[fin->fin_out].fr_skip);
744 }
745 ATOMIC_INCL(frstats[fin->fin_out].fr_pkl);
746 logged = 1;
747 }
748#endif /* IPFILTER_LOG */
749 ATOMIC_INCL(fr->fr_hits);
750 if (passt & FR_ACCOUNT)
751 fr->fr_bytes += (U_QUAD_T)ip->ip_len;
752 else
753 fin->fin_icode = fr->fr_icode;
754 fin->fin_rule = rulen;
755 fin->fin_group = fr->fr_group;
756 if (fr->fr_grp != NULL) {
757 fin->fin_fr = fr->fr_grp;
758 passt = fr_scanlist(passt, ip, fin, m);
759 if (fin->fin_fr == NULL) {
760 fin->fin_rule = rulen;
761 fin->fin_group = fr->fr_group;
762 fin->fin_fr = fr;
763 }
764 if (passt & FR_DONTCACHE)
765 logged = 1;
766 }
767 if (!(skip = fr->fr_skip) && (passt & FR_LOGMASK) != FR_LOG)
768 pass = passt;
769 FR_DEBUG(("pass %#x\n", pass));
770 if (passt & FR_QUICK)
771 break;
772 }
773 if (logged)
774 pass |= FR_DONTCACHE;
775 pass |= (fi->fi_fl << 24);
776 return pass;
777}
778
779
780/*
781 * frcheck - filter check
782 * check using source and destination addresses/ports in a packet whether
783 * or not to pass it on or not.
784 */
785int fr_check(ip, hlen, ifp, out
786#if defined(_KERNEL) && SOLARIS
787, qif, mp)
788qif_t *qif;
789#else
790, mp)
791#endif
792mb_t **mp;
793ip_t *ip;
794int hlen;
795void *ifp;
796int out;
797{
798 /*
799 * The above really sucks, but short of writing a diff
800 */
801 fr_info_t frinfo, *fc;
802 register fr_info_t *fin = &frinfo;
803 int changed, error = EHOSTUNREACH, v = ip->ip_v;
804 frentry_t *fr = NULL, *list;
805 u_32_t pass, apass;
806#if !SOLARIS || !defined(_KERNEL)
807 register mb_t *m = *mp;
808#endif
809
810#ifdef _KERNEL
811 int p, len, drop = 0, logit = 0;
812 mb_t *mc = NULL;
813# if !defined(__SVR4) && !defined(__svr4__)
814# ifdef __sgi
815 char hbuf[128];
816# endif
817 int up;
818
819# ifdef M_CANFASTFWD
820 /*
821 * XXX For now, IP Filter and fast-forwarding of cached flows
822 * XXX are mutually exclusive. Eventually, IP Filter should
823 * XXX get a "can-fast-forward" filter rule.
824 */
825 m->m_flags &= ~M_CANFASTFWD;
826# endif /* M_CANFASTFWD */
827# ifdef CSUM_DELAY_DATA
828 /*
829 * disable delayed checksums.
830 */
831 if ((out != 0) && (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)) {
832 in_delayed_cksum(m);
833 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
834 }
835# endif /* CSUM_DELAY_DATA */
836
837# ifdef USE_INET6
838 if (v == 6) {
839 len = ntohs(((ip6_t*)ip)->ip6_plen);
840 if (!len)
841 return -1; /* potential jumbo gram */
842 len += sizeof(ip6_t);
843 p = ((ip6_t *)ip)->ip6_nxt;
844 } else
845# endif
846 {
847 p = ip->ip_p;
848 len = ip->ip_len;
849 }
850
851 if ((p == IPPROTO_TCP || p == IPPROTO_UDP ||
852 (v == 4 && p == IPPROTO_ICMP)
853# ifdef USE_INET6
854 || (v == 6 && p == IPPROTO_ICMPV6)
855# endif
856 )) {
857 int plen = 0;
858
859 if ((v == 6) || (ip->ip_off & IP_OFFMASK) == 0)
860 switch(p)
861 {
862 case IPPROTO_TCP:
863 plen = sizeof(tcphdr_t);
864 break;
865 case IPPROTO_UDP:
866 plen = sizeof(udphdr_t);
867 break;
868 /* 96 - enough for complete ICMP error IP header */
869 case IPPROTO_ICMP:
870 plen = ICMPERR_MAXPKTLEN - sizeof(ip_t);
871 break;
872 case IPPROTO_ESP:
873 plen = 8;
874 break;
875# ifdef USE_INET6
876 case IPPROTO_ICMPV6 :
877 /*
878 * XXX does not take intermediate header
879 * into account
880 */
881 plen = ICMP6ERR_MINPKTLEN + 8 - sizeof(ip6_t);
882 break;
883# endif
884 }
885 up = MIN(hlen + plen, len);
886
887 if (up > m->m_len) {
888# ifdef __sgi
889 /* Under IRIX, avoid m_pullup as it makes ping <hostname> panic */
890 if ((up > sizeof(hbuf)) || (m_length(m) < up)) {
891 ATOMIC_INCL(frstats[out].fr_pull[1]);
892 return -1;
893 }
894 m_copydata(m, 0, up, hbuf);
895 ATOMIC_INCL(frstats[out].fr_pull[0]);
896 ip = (ip_t *)hbuf;
897# else /* __ sgi */
898# ifndef linux
899 if ((*mp = m_pullup(m, up)) == 0) {
900 ATOMIC_INCL(frstats[out].fr_pull[1]);
901 return -1;
902 } else {
903 ATOMIC_INCL(frstats[out].fr_pull[0]);
904 m = *mp;
905 ip = mtod(m, ip_t *);
906 }
907# endif /* !linux */
908# endif /* __sgi */
909 } else
910 up = 0;
911 } else
912 up = 0;
913# endif /* !defined(__SVR4) && !defined(__svr4__) */
914# if SOLARIS
915 mb_t *m = qif->qf_m;
916
917 if ((u_int)ip & 0x3)
918 return 2;
919 fin->fin_qfm = m;
920 fin->fin_qif = qif;
921# endif
922#endif /* _KERNEL */
923
924#ifndef __FreeBSD__
925 /*
926 * Be careful here: ip_id is in network byte order when called
927 * from ip_output()
928 */
929 if ((out) && (v == 4))
930 ip->ip_id = ntohs(ip->ip_id);
931#endif
932
933 changed = 0;
934 fin->fin_ifp = ifp;
935 fin->fin_v = v;
936 fin->fin_out = out;
937 fin->fin_mp = mp;
938 fr_makefrip(hlen, ip, fin);
939
940#ifdef _KERNEL
941# ifdef USE_INET6
942 if (v == 6) {
943 ATOMIC_INCL(frstats[0].fr_ipv6[out]);
944 if (((ip6_t *)ip)->ip6_hlim < fr_minttl) {
945 ATOMIC_INCL(frstats[0].fr_badttl);
946 if (fr_minttllog & 1)
947 logit = -3;
948 if (fr_minttllog & 2)
949 drop = 1;
950 }
951 } else
952# endif
953 if (!out) {
954 if (fr_chksrc && !fr_verifysrc(ip->ip_src, ifp)) {
955 ATOMIC_INCL(frstats[0].fr_badsrc);
956 if (fr_chksrc & 1)
957 drop = 1;
958 if (fr_chksrc & 2)
959 logit = -2;
960 } else if (ip->ip_ttl < fr_minttl) {
961 ATOMIC_INCL(frstats[0].fr_badttl);
962 if (fr_minttllog & 1)
963 logit = -3;
964 if (fr_minttllog & 2)
965 drop = 1;
966 }
967 }
968 if (drop) {
969# ifdef IPFILTER_LOG
970 if (logit) {
971 fin->fin_group = logit;
972 pass = FR_INQUE|FR_NOMATCH|FR_LOGB;
973 (void) IPLLOG(pass, ip, fin, m);
974 }
975# endif
976# if !SOLARIS
977 m_freem(m);
978# endif
979 return error;
980 }
981#endif
982 pass = fr_pass;
983 if (fin->fin_fl & FI_SHORT) {
984 ATOMIC_INCL(frstats[out].fr_short);
985 }
986
987 READ_ENTER(&ipf_mutex);
988
989 /*
990 * Check auth now. This, combined with the check below to see if apass
991 * is 0 is to ensure that we don't count the packet twice, which can
992 * otherwise occur when we reprocess it. As it is, we only count it
993 * after it has no auth. table matchup. This also stops NAT from
994 * occuring until after the packet has been auth'd.
995 */
996 apass = fr_checkauth(ip, fin);
997
998 if (!out) {
999#ifdef USE_INET6
1000 if (v == 6)
1001 list = ipacct6[0][fr_active];
1002 else
1003#endif
1004 list = ipacct[0][fr_active];
1005 changed = ip_natin(ip, fin);
1006 if (!apass && (fin->fin_fr = list) &&
1007 (fr_scanlist(FR_NOMATCH, ip, fin, m) & FR_ACCOUNT)) {
1008 ATOMIC_INCL(frstats[0].fr_acct);
1009 }
1010 }
1011
1012 if (!apass) {
1013 if ((fin->fin_fl & FI_FRAG) == FI_FRAG)
1014 fr = ipfr_knownfrag(ip, fin);
1015 if (!fr && !(fin->fin_fl & FI_SHORT))
1016 fr = fr_checkstate(ip, fin);
1017 if (fr != NULL)
1018 pass = fr->fr_flags;
1019 if (fr && (pass & FR_LOGFIRST))
1020 pass &= ~(FR_LOGFIRST|FR_LOG);
1021 }
1022
1023 if (apass || !fr) {
1024 /*
1025 * If a packet is found in the auth table, then skip checking
1026 * the access lists for permission but we do need to consider
1027 * the result as if it were from the ACL's.
1028 */
1029 if (!apass) {
1030 fc = frcache + out;
1031 if (!bcmp((char *)fin, (char *)fc, FI_CSIZE)) {
1032 /*
1033 * copy cached data so we can unlock the mutex
1034 * earlier.
1035 */
1036 bcopy((char *)fc, (char *)fin, FI_COPYSIZE);
1037 ATOMIC_INCL(frstats[out].fr_chit);
1038 if ((fr = fin->fin_fr)) {
1039 ATOMIC_INCL(fr->fr_hits);
1040 pass = fr->fr_flags;
1041 }
1042 } else {
1043#ifdef USE_INET6
1044 if (v == 6)
1045 list = ipfilter6[out][fr_active];
1046 else
1047#endif
1048 list = ipfilter[out][fr_active];
1049 if ((fin->fin_fr = list))
1050 pass = fr_scanlist(fr_pass, ip, fin, m);
1051 if (!(pass & (FR_KEEPSTATE|FR_DONTCACHE)))
1052 bcopy((char *)fin, (char *)fc,
1053 FI_COPYSIZE);
1054 if (pass & FR_NOMATCH) {
1055 ATOMIC_INCL(frstats[out].fr_nom);
1056 fin->fin_fr = NULL;
1057 }
1058 }
1059 } else
1060 pass = apass;
1061 fr = fin->fin_fr;
1062
1063 /*
1064 * If we fail to add a packet to the authorization queue,
1065 * then we drop the packet later. However, if it was added
1066 * then pretend we've dropped it already.
1067 */
1068 if ((pass & FR_AUTH)) {
1069 if (fr_newauth((mb_t *)m, fin, ip) != 0) {
1070 m = *mp = NULL;
1071 error = 0;
1072 } else
1073 error = ENOSPC;
1074 }
1075
1076 if (pass & FR_PREAUTH) {
1077 READ_ENTER(&ipf_auth);
1078 if ((fin->fin_fr = ipauth) &&
1079 (pass = fr_scanlist(0, ip, fin, m))) {
1080 ATOMIC_INCL(fr_authstats.fas_hits);
1081 } else {
1082 ATOMIC_INCL(fr_authstats.fas_miss);
1083 }
1084 RWLOCK_EXIT(&ipf_auth);
1085 }
1086
1087 fin->fin_fr = fr;
1088 if ((pass & (FR_KEEPFRAG|FR_KEEPSTATE)) == FR_KEEPFRAG) {
1089 if (fin->fin_fl & FI_FRAG) {
1090 if (ipfr_newfrag(ip, fin, pass) == -1) {
1091 ATOMIC_INCL(frstats[out].fr_bnfr);
1092 } else {
1093 ATOMIC_INCL(frstats[out].fr_nfr);
1094 }
1095 } else {
1096 ATOMIC_INCL(frstats[out].fr_cfr);
1097 }
1098 }
1099 if (pass & FR_KEEPSTATE) {
1100 if (fr_addstate(ip, fin, NULL, 0) == NULL) {
1101 ATOMIC_INCL(frstats[out].fr_bads);
1102 } else {
1103 ATOMIC_INCL(frstats[out].fr_ads);
1104 }
1105 }
1106 } else if (fr != NULL) {
1107 pass = fr->fr_flags;
1108 if (pass & FR_LOGFIRST)
1109 pass &= ~(FR_LOGFIRST|FR_LOG);
1110 }
1111
1112#if (BSD >= 199306) && (defined(_KERNEL) || defined(KERNEL))
1113 if (securelevel <= 0)
1114#endif
1115 if (fr && fr->fr_func && !(pass & FR_CALLNOW))
1116 pass = (*fr->fr_func)(pass, ip, fin);
1117
1118 /*
1119 * Only count/translate packets which will be passed on, out the
1120 * interface.
1121 */
1122 if (out && (pass & FR_PASS)) {
1123#ifdef USE_INET6
1124 if (v == 6)
1125 list = ipacct6[1][fr_active];
1126 else
1127#endif
1128 list = ipacct[1][fr_active];
1129 if (list != NULL) {
1130 u_32_t sg, sr;
1131
1132 fin->fin_fr = list;
1133 sg = fin->fin_group;
1134 sr = fin->fin_rule;
1135 if (fr_scanlist(FR_NOMATCH, ip, fin, m) & FR_ACCOUNT) {
1136 ATOMIC_INCL(frstats[1].fr_acct);
1137 }
1138 fin->fin_group = sg;
1139 fin->fin_rule = sr;
1140 fin->fin_fr = fr;
1141 }
1142 changed = ip_natout(ip, fin);
1143 } else
1144 fin->fin_fr = fr;
1145 RWLOCK_EXIT(&ipf_mutex);
1146
1147#ifdef IPFILTER_LOG
1148 if ((fr_flags & FF_LOGGING) || (pass & FR_LOGMASK)) {
1149 if ((fr_flags & FF_LOGNOMATCH) && (pass & FR_NOMATCH)) {
1150 pass |= FF_LOGNOMATCH;
1151 ATOMIC_INCL(frstats[out].fr_npkl);
1152 goto logit;
1153 } else if (((pass & FR_LOGMASK) == FR_LOGP) ||
1154 ((pass & FR_PASS) && (fr_flags & FF_LOGPASS))) {
1155 if ((pass & FR_LOGMASK) != FR_LOGP)
1156 pass |= FF_LOGPASS;
1157 ATOMIC_INCL(frstats[out].fr_ppkl);
1158 goto logit;
1159 } else if (((pass & FR_LOGMASK) == FR_LOGB) ||
1160 ((pass & FR_BLOCK) && (fr_flags & FF_LOGBLOCK))) {
1161 if ((pass & FR_LOGMASK) != FR_LOGB)
1162 pass |= FF_LOGBLOCK;
1163 ATOMIC_INCL(frstats[out].fr_bpkl);
1164logit:
1165 if (!IPLLOG(pass, ip, fin, m)) {
1166 ATOMIC_INCL(frstats[out].fr_skip);
1167 if ((pass & (FR_PASS|FR_LOGORBLOCK)) ==
1168 (FR_PASS|FR_LOGORBLOCK))
1169 pass ^= FR_PASS|FR_BLOCK;
1170 }
1171 }
1172 }
1173#endif /* IPFILTER_LOG */
1174
1175#ifndef __FreeBSD__
1176 if ((out) && (v == 4))
1177 ip->ip_id = htons(ip->ip_id);
1178#endif
1179
1180#ifdef _KERNEL
1181 /*
1182 * Only allow FR_DUP to work if a rule matched - it makes no sense to
1183 * set FR_DUP as a "default" as there are no instructions about where
1184 * to send the packet.
1185 */
1186 if (fr && (pass & FR_DUP))
1187# if SOLARIS
1188 mc = dupmsg(m);
1189# else
1190# if defined(__OpenBSD__) && (OpenBSD >= 199905)
1191 mc = m_copym2(m, 0, M_COPYALL, M_DONTWAIT);
1192# else
1193 mc = m_copy(m, 0, M_COPYALL);
1194# endif
1195# endif
1196#endif
1197 if (pass & FR_PASS) {
1198 ATOMIC_INCL(frstats[out].fr_pass);
1199 } else if (pass & FR_BLOCK) {
1200 ATOMIC_INCL(frstats[out].fr_block);
1201 /*
1202 * Should we return an ICMP packet to indicate error
1203 * status passing through the packet filter ?
1204 * WARNING: ICMP error packets AND TCP RST packets should
1205 * ONLY be sent in repsonse to incoming packets. Sending them
1206 * in response to outbound packets can result in a panic on
1207 * some operating systems.
1208 */
1209 if (!out) {
1210 if (pass & FR_RETICMP) {
1211 int dst;
1212
1213 if ((pass & FR_RETMASK) == FR_FAKEICMP)
1214 dst = 1;
1215 else
1216 dst = 0;
1217 send_icmp_err(ip, ICMP_UNREACH, fin, dst);
1218 ATOMIC_INCL(frstats[0].fr_ret);
1219 } else if (((pass & FR_RETMASK) == FR_RETRST) &&
1220 !(fin->fin_fl & FI_SHORT)) {
1221 if (send_reset(ip, fin) == 0) {
1222 ATOMIC_INCL(frstats[1].fr_ret);
1223 }
1224 }
1225 } else {
1226 if (pass & FR_RETRST)
1227 error = ECONNRESET;
1228 }
1229 }
1230
1231 /*
1232 * If we didn't drop off the bottom of the list of rules (and thus
1233 * the 'current' rule fr is not NULL), then we may have some extra
1234 * instructions about what to do with a packet.
1235 * Once we're finished return to our caller, freeing the packet if
1236 * we are dropping it (* BSD ONLY *).
1237 */
1238 if ((changed == -1) && (pass & FR_PASS)) {
1239 pass &= ~FR_PASS;
1240 pass |= FR_BLOCK;
1241 }
1242#if defined(_KERNEL)
1243# if !SOLARIS
1244# if !defined(linux)
1245 if (fr) {
1246 frdest_t *fdp = &fr->fr_tif;
1247
1248 if (((pass & FR_FASTROUTE) && !out) ||
1249 (fdp->fd_ifp && fdp->fd_ifp != (struct ifnet *)-1)) {
1250 (void) ipfr_fastroute(m, mp, fin, fdp);
1251 m = *mp;
1252 }
1253
1254 if (mc != NULL)
1255 (void) ipfr_fastroute(mc, &mc, fin, &fr->fr_dif);
1256 }
1257
1258 if (!(pass & FR_PASS) && m) {
1259 m_freem(m);
1260 m = *mp = NULL;
1261 }
1262# ifdef __sgi
1263 else if (changed && up && m)
1264 m_copyback(m, 0, up, hbuf);
1265# endif
1266# endif /* !linux */
1267# else /* !SOLARIS */
1268 if (fr) {
1269 frdest_t *fdp = &fr->fr_tif;
1270
1271 if (((pass & FR_FASTROUTE) && !out) ||
1272 (fdp->fd_ifp && fdp->fd_ifp != (struct ifnet *)-1))
1273 (void) ipfr_fastroute(ip, m, mp, fin, fdp);
1274
1275 if (mc != NULL)
1276 (void) ipfr_fastroute(ip, mc, &mc, fin, &fr->fr_dif);
1277 }
1278# endif /* !SOLARIS */
1279 return (pass & FR_PASS) ? 0 : error;
1280#else /* _KERNEL */
1281 if (pass & FR_NOMATCH)
1282 return 1;
1283 if (pass & FR_PASS)
1284 return 0;
1285 if (pass & FR_AUTH)
1286 return -2;
1287 if ((pass & FR_RETMASK) == FR_RETRST)
1288 return -3;
1289 if ((pass & FR_RETMASK) == FR_RETICMP)
1290 return -4;
1291 if ((pass & FR_RETMASK) == FR_FAKEICMP)
1292 return -5;
1293 return -1;
1294#endif /* _KERNEL */
1295}
1296
1297
1298/*
1299 * ipf_cksum
1300 * addr should be 16bit aligned and len is in bytes.
1301 * length is in bytes
1302 */
1303u_short ipf_cksum(addr, len)
1304register u_short *addr;
1305register int len;
1306{
1307 register u_32_t sum = 0;
1308
1309 for (sum = 0; len > 1; len -= 2)
1310 sum += *addr++;
1311
1312 /* mop up an odd byte, if necessary */
1313 if (len == 1)
1314 sum += *(u_char *)addr;
1315
1316 /*
1317 * add back carry outs from top 16 bits to low 16 bits
1318 */
1319 sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */
1320 sum += (sum >> 16); /* add carry */
1321 return (u_short)(~sum);
1322}
1323
1324
1325/*
1326 * NB: This function assumes we've pullup'd enough for all of the IP header
1327 * and the TCP header. We also assume that data blocks aren't allocated in
1328 * odd sizes.
1329 */
1330u_short fr_tcpsum(m, ip, tcp)
1331mb_t *m;
1332ip_t *ip;
1333tcphdr_t *tcp;
1334{
1335 u_short *sp, slen, ts;
1336 u_int sum, sum2;
1337 int hlen;
1338
1339 /*
1340 * Add up IP Header portion
1341 */
1342 hlen = ip->ip_hl << 2;
1343 slen = ip->ip_len - hlen;
1344 sum = htons((u_short)ip->ip_p);
1345 sum += htons(slen);
1346 sp = (u_short *)&ip->ip_src;
1347 sum += *sp++; /* ip_src */
1348 sum += *sp++;
1349 sum += *sp++; /* ip_dst */
1350 sum += *sp++;
1351 ts = tcp->th_sum;
1352 tcp->th_sum = 0;
1353#ifdef KERNEL
1354# if SOLARIS
1355 sum2 = ip_cksum(m, hlen, sum); /* hlen == offset */
1356 sum2 = (sum2 & 0xffff) + (sum2 >> 16);
1357 sum2 = ~sum2 & 0xffff;
1358# else /* SOLARIS */
1359# if defined(BSD) || defined(sun)
1360# if BSD >= 199306
1361 m->m_data += hlen;
1362# else
1363 m->m_off += hlen;
1364# endif
1365 m->m_len -= hlen;
1366 sum2 = in_cksum(m, slen);
1367 m->m_len += hlen;
1368# if BSD >= 199306
1369 m->m_data -= hlen;
1370# else
1371 m->m_off -= hlen;
1372# endif
1373 /*
1374 * Both sum and sum2 are partial sums, so combine them together.
1375 */
1376 sum = (sum & 0xffff) + (sum >> 16);
1377 sum = ~sum & 0xffff;
1378 sum2 += sum;
1379 sum2 = (sum2 & 0xffff) + (sum2 >> 16);
1380# else /* defined(BSD) || defined(sun) */
1381{
1382 union {
1383 u_char c[2];
1384 u_short s;
1385 } bytes;
1386 u_short len = ip->ip_len;
1387# if defined(__sgi)
1388 int add;
1389# endif
1390
1391 /*
1392 * Add up IP Header portion
1393 */
1394 sp = (u_short *)&ip->ip_src;
1395 len -= (ip->ip_hl << 2);
1396 sum = ntohs(IPPROTO_TCP);
1397 sum += htons(len);
1398 sum += *sp++; /* ip_src */
1399 sum += *sp++;
1400 sum += *sp++; /* ip_dst */
1401 sum += *sp++;
1402 if (sp != (u_short *)tcp)
1403 sp = (u_short *)tcp;
1404 sum += *sp++; /* sport */
1405 sum += *sp++; /* dport */
1406 sum += *sp++; /* seq */
1407 sum += *sp++;
1408 sum += *sp++; /* ack */
1409 sum += *sp++;
1410 sum += *sp++; /* off */
1411 sum += *sp++; /* win */
1412 sum += *sp++; /* Skip over checksum */
1413 sum += *sp++; /* urp */
1414
1415# ifdef __sgi
1416 /*
1417 * In case we had to copy the IP & TCP header out of mbufs,
1418 * skip over the mbuf bits which are the header
1419 */
1420 if ((caddr_t)ip != mtod(m, caddr_t)) {
1421 hlen = (caddr_t)sp - (caddr_t)ip;
1422 while (hlen) {
1423 add = MIN(hlen, m->m_len);
1424 sp = (u_short *)(mtod(m, caddr_t) + add);
1425 hlen -= add;
1426 if (add == m->m_len) {
1427 m = m->m_next;
1428 if (!hlen) {
1429 if (!m)
1430 break;
1431 sp = mtod(m, u_short *);
1432 }
1433 PANIC((!m),("fr_tcpsum(1): not enough data"));
1434 }
1435 }
1436 }
1437# endif
1438
1439 if (!(len -= sizeof(*tcp)))
1440 goto nodata;
1441 while (len > 1) {
1442 if (((caddr_t)sp - mtod(m, caddr_t)) >= m->m_len) {
1443 m = m->m_next;
1444 PANIC((!m),("fr_tcpsum(2): not enough data"));
1445 sp = mtod(m, u_short *);
1446 }
1447 if (((caddr_t)(sp + 1) - mtod(m, caddr_t)) > m->m_len) {
1448 bytes.c[0] = *(u_char *)sp;
1449 m = m->m_next;
1450 PANIC((!m),("fr_tcpsum(3): not enough data"));
1451 sp = mtod(m, u_short *);
1452 bytes.c[1] = *(u_char *)sp;
1453 sum += bytes.s;
1454 sp = (u_short *)((u_char *)sp + 1);
1455 }
1456 if ((u_long)sp & 1) {
1457 bcopy((char *)sp++, (char *)&bytes.s, sizeof(bytes.s));
1458 sum += bytes.s;
1459 } else
1460 sum += *sp++;
1461 len -= 2;
1462 }
1463 if (len)
1464 sum += ntohs(*(u_char *)sp << 8);
1465nodata:
1466 while (sum > 0xffff)
1467 sum = (sum & 0xffff) + (sum >> 16);
1468 sum2 = (u_short)(~sum & 0xffff);
1469}
1470# endif /* defined(BSD) || defined(sun) */
1471# endif /* SOLARIS */
1472#else /* KERNEL */
1473 sum2 = 0;
1474#endif /* KERNEL */
1475 tcp->th_sum = ts;
1476 return sum2;
1477}
1478
1479
1480#if defined(_KERNEL) && ( ((BSD < 199306) && !SOLARIS) || defined(__sgi) )
1481/*
1482 * Copyright (c) 1982, 1986, 1988, 1991, 1993
1483 * The Regents of the University of California. All rights reserved.
1484 *
1485 * Redistribution and use in source and binary forms, with or without
1486 * modification, are permitted provided that the following conditions
1487 * are met:
1488 * 1. Redistributions of source code must retain the above copyright
1489 * notice, this list of conditions and the following disclaimer.
1490 * 2. Redistributions in binary form must reproduce the above copyright
1491 * notice, this list of conditions and the following disclaimer in the
1492 * documentation and/or other materials provided with the distribution.
1493 * 3. All advertising materials mentioning features or use of this software
1494 * must display the following acknowledgement:
1495 * This product includes software developed by the University of
1496 * California, Berkeley and its contributors.
1497 * 4. Neither the name of the University nor the names of its contributors
1498 * may be used to endorse or promote products derived from this software
1499 * without specific prior written permission.
1500 *
1501 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1502 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1503 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1504 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
1505 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1506 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1507 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1508 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1509 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1510 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1511 * SUCH DAMAGE.
1512 *
1513 * @(#)uipc_mbuf.c 8.2 (Berkeley) 1/4/94
1515 * $Id: fil.c,v 2.35.2.58 2002/03/13 02:23:13 darrenr Exp $
1514 * $Id: fil.c,v 2.35.2.59 2002/03/25 11:07:37 darrenr Exp $
1515 */
1516/*
1517 * Copy data from an mbuf chain starting "off" bytes from the beginning,
1518 * continuing for "len" bytes, into the indicated buffer.
1519 */
1520void
1521m_copydata(m, off, len, cp)
1522 register mb_t *m;
1523 register int off;
1524 register int len;
1525 caddr_t cp;
1526{
1527 register unsigned count;
1528
1529 if (off < 0 || len < 0)
1530 panic("m_copydata");
1531 while (off > 0) {
1532 if (m == 0)
1533 panic("m_copydata");
1534 if (off < m->m_len)
1535 break;
1536 off -= m->m_len;
1537 m = m->m_next;
1538 }
1539 while (len > 0) {
1540 if (m == 0)
1541 panic("m_copydata");
1542 count = MIN(m->m_len - off, len);
1543 bcopy(mtod(m, caddr_t) + off, cp, count);
1544 len -= count;
1545 cp += count;
1546 off = 0;
1547 m = m->m_next;
1548 }
1549}
1550
1551
1552# ifndef linux
1553/*
1554 * Copy data from a buffer back into the indicated mbuf chain,
1555 * starting "off" bytes from the beginning, extending the mbuf
1556 * chain if necessary.
1557 */
1558void
1559m_copyback(m0, off, len, cp)
1560 struct mbuf *m0;
1561 register int off;
1562 register int len;
1563 caddr_t cp;
1564{
1565 register int mlen;
1566 register struct mbuf *m = m0, *n;
1567 int totlen = 0;
1568
1569 if (m0 == 0)
1570 return;
1571 while (off > (mlen = m->m_len)) {
1572 off -= mlen;
1573 totlen += mlen;
1574 if (m->m_next == 0) {
1575 n = m_getclr(M_DONTWAIT, m->m_type);
1576 if (n == 0)
1577 goto out;
1578 n->m_len = min(MLEN, len + off);
1579 m->m_next = n;
1580 }
1581 m = m->m_next;
1582 }
1583 while (len > 0) {
1584 mlen = min (m->m_len - off, len);
1585 bcopy(cp, off + mtod(m, caddr_t), (unsigned)mlen);
1586 cp += mlen;
1587 len -= mlen;
1588 mlen += off;
1589 off = 0;
1590 totlen += mlen;
1591 if (len == 0)
1592 break;
1593 if (m->m_next == 0) {
1594 n = m_get(M_DONTWAIT, m->m_type);
1595 if (n == 0)
1596 break;
1597 n->m_len = min(MLEN, len);
1598 m->m_next = n;
1599 }
1600 m = m->m_next;
1601 }
1602out:
1603#if 0
1604 if (((m = m0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen))
1605 m->m_pkthdr.len = totlen;
1606#endif
1607 return;
1608}
1609# endif /* linux */
1610#endif /* (_KERNEL) && ( ((BSD < 199306) && !SOLARIS) || __sgi) */
1611
1612
1613frgroup_t *fr_findgroup(num, flags, which, set, fgpp)
1614u_32_t num, flags;
1615minor_t which;
1616int set;
1617frgroup_t ***fgpp;
1618{
1619 frgroup_t *fg, **fgp;
1620
1621 if (which == IPL_LOGAUTH)
1622 fgp = &ipfgroups[2][set];
1623 else if (flags & FR_ACCOUNT)
1624 fgp = &ipfgroups[1][set];
1625 else if (flags & (FR_OUTQUE|FR_INQUE))
1626 fgp = &ipfgroups[0][set];
1627 else
1628 return NULL;
1629 num &= 0xffff;
1630
1631 while ((fg = *fgp))
1632 if (fg->fg_num == num)
1633 break;
1634 else
1635 fgp = &fg->fg_next;
1636 if (fgpp)
1637 *fgpp = fgp;
1638 return fg;
1639}
1640
1641
1642frgroup_t *fr_addgroup(num, fp, which, set)
1643u_32_t num;
1644frentry_t *fp;
1645minor_t which;
1646int set;
1647{
1648 frgroup_t *fg, **fgp;
1649
1650 if ((fg = fr_findgroup(num, fp->fr_flags, which, set, &fgp)))
1651 return fg;
1652
1653 KMALLOC(fg, frgroup_t *);
1654 if (fg) {
1655 fg->fg_num = num;
1656 fg->fg_next = *fgp;
1657 fg->fg_head = fp;
1658 fg->fg_start = &fp->fr_grp;
1659 *fgp = fg;
1660 }
1661 return fg;
1662}
1663
1664
1665void fr_delgroup(num, flags, which, set)
1666u_32_t num, flags;
1667minor_t which;
1668int set;
1669{
1670 frgroup_t *fg, **fgp;
1671
1672 if (!(fg = fr_findgroup(num, flags, which, set, &fgp)))
1673 return;
1674
1675 *fgp = fg->fg_next;
1676 KFREE(fg);
1677}
1678
1679
1680
1681/*
1682 * recursively flush rules from the list, descending groups as they are
1683 * encountered. if a rule is the head of a group and it has lost all its
1684 * group members, then also delete the group reference.
1685 */
1686static int frflushlist(set, unit, nfreedp, listp)
1687int set;
1688minor_t unit;
1689int *nfreedp;
1690frentry_t **listp;
1691{
1692 register int freed = 0, i;
1693 register frentry_t *fp;
1694
1695 while ((fp = *listp)) {
1696 *listp = fp->fr_next;
1697 if (fp->fr_grp) {
1698 i = frflushlist(set, unit, nfreedp, &fp->fr_grp);
1699 MUTEX_ENTER(&ipf_rw);
1700 fp->fr_ref -= i;
1701 MUTEX_EXIT(&ipf_rw);
1702 }
1703
1704 ATOMIC_DEC32(fp->fr_ref);
1705 if (fp->fr_grhead) {
1706 fr_delgroup(fp->fr_grhead, fp->fr_flags,
1707 unit, set);
1708 fp->fr_grhead = 0;
1709 }
1710 if (fp->fr_ref == 0) {
1711 KFREE(fp);
1712 freed++;
1713 } else
1714 fp->fr_next = NULL;
1715 }
1716 *nfreedp += freed;
1717 return freed;
1718}
1719
1720
1721int frflush(unit, flags)
1722minor_t unit;
1723int flags;
1724{
1725 int flushed = 0, set;
1726
1727 if (unit != IPL_LOGIPF)
1728 return 0;
1729 WRITE_ENTER(&ipf_mutex);
1730 bzero((char *)frcache, sizeof(frcache[0]) * 2);
1731
1732 set = fr_active;
1733 if (flags & FR_INACTIVE)
1734 set = 1 - set;
1735
1736 if (flags & FR_OUTQUE) {
1737#ifdef USE_INET6
1738 (void) frflushlist(set, unit, &flushed, &ipfilter6[1][set]);
1739 (void) frflushlist(set, unit, &flushed, &ipacct6[1][set]);
1740#endif
1741 (void) frflushlist(set, unit, &flushed, &ipfilter[1][set]);
1742 (void) frflushlist(set, unit, &flushed, &ipacct[1][set]);
1743 }
1744 if (flags & FR_INQUE) {
1745#ifdef USE_INET6
1746 (void) frflushlist(set, unit, &flushed, &ipfilter6[0][set]);
1747 (void) frflushlist(set, unit, &flushed, &ipacct6[0][set]);
1748#endif
1749 (void) frflushlist(set, unit, &flushed, &ipfilter[0][set]);
1750 (void) frflushlist(set, unit, &flushed, &ipacct[0][set]);
1751 }
1752 RWLOCK_EXIT(&ipf_mutex);
1753 return flushed;
1754}
1755
1756
1757char *memstr(src, dst, slen, dlen)
1758char *src, *dst;
1759int slen, dlen;
1760{
1761 char *s = NULL;
1762
1763 while (dlen >= slen) {
1764 if (bcmp(src, dst, slen) == 0) {
1765 s = dst;
1766 break;
1767 }
1768 dst++;
1769 dlen--;
1770 }
1771 return s;
1772}
1773
1774
1775void fixskip(listp, rp, addremove)
1776frentry_t **listp, *rp;
1777int addremove;
1778{
1779 frentry_t *fp;
1780 int rules = 0, rn = 0;
1781
1782 for (fp = *listp; fp && (fp != rp); fp = fp->fr_next, rules++)
1783 ;
1784
1785 if (!fp)
1786 return;
1787
1788 for (fp = *listp; fp && (fp != rp); fp = fp->fr_next, rn++)
1789 if (fp->fr_skip && (rn + fp->fr_skip >= rules))
1790 fp->fr_skip += addremove;
1791}
1792
1793
1794#ifdef _KERNEL
1795/*
1796 * count consecutive 1's in bit mask. If the mask generated by counting
1797 * consecutive 1's is different to that passed, return -1, else return #
1798 * of bits.
1799 */
1800int countbits(ip)
1801u_32_t ip;
1802{
1803 u_32_t ipn;
1804 int cnt = 0, i, j;
1805
1806 ip = ipn = ntohl(ip);
1807 for (i = 32; i; i--, ipn *= 2)
1808 if (ipn & 0x80000000)
1809 cnt++;
1810 else
1811 break;
1812 ipn = 0;
1813 for (i = 32, j = cnt; i; i--, j--) {
1814 ipn *= 2;
1815 if (j > 0)
1816 ipn++;
1817 }
1818 if (ipn == ip)
1819 return cnt;
1820 return -1;
1821}
1822
1823
1824/*
1825 * return the first IP Address associated with an interface
1826 */
1827int fr_ifpaddr(v, ifptr, inp)
1828int v;
1829void *ifptr;
1830struct in_addr *inp;
1831{
1832# ifdef USE_INET6
1833 struct in6_addr *inp6 = NULL;
1834# endif
1835# if SOLARIS
1836 ill_t *ill = ifptr;
1837# else
1838 struct ifnet *ifp = ifptr;
1839# endif
1840 struct in_addr in;
1841
1842# if SOLARIS
1843# ifdef USE_INET6
1844 if (v == 6) {
1845 struct in6_addr in6;
1846
1847 /*
1848 * First is always link local.
1849 */
1850 if (ill->ill_ipif->ipif_next)
1851 in6 = ill->ill_ipif->ipif_next->ipif_v6lcl_addr;
1852 else
1853 bzero((char *)&in6, sizeof(in6));
1854 bcopy((char *)&in6, (char *)inp, sizeof(in6));
1855 } else
1856# endif
1857 {
1858 in.s_addr = ill->ill_ipif->ipif_local_addr;
1859 *inp = in;
1860 }
1861# else /* SOLARIS */
1862# if linux
1863 ;
1864# else /* linux */
1865 struct sockaddr_in *sin;
1866 struct ifaddr *ifa;
1867
1868# if (__FreeBSD_version >= 300000)
1869 ifa = TAILQ_FIRST(&ifp->if_addrhead);
1870# else
1871# if defined(__NetBSD__) || defined(__OpenBSD__)
1872 ifa = ifp->if_addrlist.tqh_first;
1873# else
1874# if defined(__sgi) && defined(IFF_DRVRLOCK) /* IRIX 6 */
1875 ifa = &((struct in_ifaddr *)ifp->in_ifaddr)->ia_ifa;
1876# else
1877 ifa = ifp->if_addrlist;
1878# endif
1879# endif /* __NetBSD__ || __OpenBSD__ */
1880# endif /* __FreeBSD_version >= 300000 */
1881# if (BSD < 199306) && !(/*IRIX6*/defined(__sgi) && defined(IFF_DRVRLOCK))
1882 sin = (struct sockaddr_in *)&ifa->ifa_addr;
1883# else
1884 sin = (struct sockaddr_in *)ifa->ifa_addr;
1885 while (sin && ifa) {
1886 if ((v == 4) && (sin->sin_family == AF_INET))
1887 break;
1888# ifdef USE_INET6
1889 if ((v == 6) && (sin->sin_family == AF_INET6)) {
1890 inp6 = &((struct sockaddr_in6 *)sin)->sin6_addr;
1891 if (!IN6_IS_ADDR_LINKLOCAL(inp6) &&
1892 !IN6_IS_ADDR_LOOPBACK(inp6))
1893 break;
1894 }
1895# endif
1896# if (__FreeBSD_version >= 300000)
1897 ifa = TAILQ_NEXT(ifa, ifa_link);
1898# else
1899# if defined(__NetBSD__) || defined(__OpenBSD__)
1900 ifa = ifa->ifa_list.tqe_next;
1901# else
1902 ifa = ifa->ifa_next;
1903# endif
1904# endif /* __FreeBSD_version >= 300000 */
1905 if (ifa)
1906 sin = (struct sockaddr_in *)ifa->ifa_addr;
1907 }
1908 if (ifa == NULL)
1909 sin = NULL;
1910 if (sin == NULL)
1911 return -1;
1912# endif /* (BSD < 199306) && (!__sgi && IFF_DRVLOCK) */
1913# ifdef USE_INET6
1914 if (v == 6)
1915 bcopy((char *)inp6, (char *)inp, sizeof(*inp6));
1916 else
1917# endif
1918 {
1919 in = sin->sin_addr;
1920 *inp = in;
1921 }
1922# endif /* linux */
1923# endif /* SOLARIS */
1924 return 0;
1925}
1926
1927
1928static void frsynclist(fr)
1929register frentry_t *fr;
1930{
1931 for (; fr; fr = fr->fr_next) {
1932 if (fr->fr_ifa != NULL) {
1933 fr->fr_ifa = GETUNIT(fr->fr_ifname, fr->fr_ip.fi_v);
1934 if (fr->fr_ifa == NULL)
1935 fr->fr_ifa = (void *)-1;
1936 }
1937 if (fr->fr_grp)
1938 frsynclist(fr->fr_grp);
1939 }
1940}
1941
1942
1943void frsync()
1944{
1945# if !SOLARIS
1946 register struct ifnet *ifp;
1947
1948# if defined(__OpenBSD__) || ((NetBSD >= 199511) && (NetBSD < 1991011)) || \
1949 (defined(__FreeBSD_version) && (__FreeBSD_version >= 300000))
1950# if (NetBSD >= 199905) || defined(__OpenBSD__)
1951 for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_list.tqe_next)
1952# else
1953 for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next)
1954# endif
1955# else
1956 for (ifp = ifnet; ifp; ifp = ifp->if_next)
1957# endif
1958 {
1959 ip_natsync(ifp);
1960 ip_statesync(ifp);
1961 }
1962 ip_natsync((struct ifnet *)-1);
1963# endif /* !SOLARIS */
1964
1965 WRITE_ENTER(&ipf_mutex);
1966 frsynclist(ipacct[0][fr_active]);
1967 frsynclist(ipacct[1][fr_active]);
1968 frsynclist(ipfilter[0][fr_active]);
1969 frsynclist(ipfilter[1][fr_active]);
1970#ifdef USE_INET6
1971 frsynclist(ipacct6[0][fr_active]);
1972 frsynclist(ipacct6[1][fr_active]);
1973 frsynclist(ipfilter6[0][fr_active]);
1974 frsynclist(ipfilter6[1][fr_active]);
1975#endif
1976 RWLOCK_EXIT(&ipf_mutex);
1977}
1978
1979
1980/*
1981 * In the functions below, bcopy() is called because the pointer being
1982 * copied _from_ in this instance is a pointer to a char buf (which could
1983 * end up being unaligned) and on the kernel's local stack.
1984 */
1985int ircopyptr(a, b, c)
1986void *a, *b;
1987size_t c;
1988{
1989 caddr_t ca;
1990 int err;
1991
1992#if SOLARIS
1993 if (copyin(a, (char *)&ca, sizeof(ca)))
1994 return EFAULT;
1995#else
1996 bcopy(a, &ca, sizeof(ca));
1997#endif
1998 err = copyin(ca, b, c);
1999 if (err)
2000 err = EFAULT;
2001 return err;
2002}
2003
2004
2005int iwcopyptr(a, b, c)
2006void *a, *b;
2007size_t c;
2008{
2009 caddr_t ca;
2010 int err;
2011
2012#if SOLARIS
2013 if (copyin(b, (char *)&ca, sizeof(ca)))
2014 return EFAULT;
2015#else
2016 bcopy(b, &ca, sizeof(ca));
2017#endif
2018 err = copyout(a, ca, c);
2019 if (err)
2020 err = EFAULT;
2021 return err;
2022}
2023
2024#else /* _KERNEL */
2025
2026
2027/*
2028 * return the first IP Address associated with an interface
2029 */
2030int fr_ifpaddr(v, ifptr, inp)
2031int v;
2032void *ifptr;
2033struct in_addr *inp;
2034{
2035 return 0;
2036}
2037
2038
2039int ircopyptr(a, b, c)
2040void *a, *b;
2041size_t c;
2042{
2043 caddr_t ca;
2044
2045 bcopy(a, &ca, sizeof(ca));
2046 bcopy(ca, b, c);
2047 return 0;
2048}
2049
2050
2051int iwcopyptr(a, b, c)
2052void *a, *b;
2053size_t c;
2054{
2055 caddr_t ca;
2056
2057 bcopy(b, &ca, sizeof(ca));
2058 bcopy(a, ca, c);
2059 return 0;
2060}
2061
2062
2063#endif
2064
2065
2066int fr_lock(data, lockp)
2067caddr_t data;
2068int *lockp;
2069{
2070 int arg, error;
2071
2072 error = IRCOPY(data, (caddr_t)&arg, sizeof(arg));
2073 if (!error) {
2074 error = IWCOPY((caddr_t)lockp, data, sizeof(*lockp));
2075 if (!error)
2076 *lockp = arg;
2077 }
2078 return error;
2079}
2080
2081
2082void fr_getstat(fiop)
2083friostat_t *fiop;
2084{
2085 bcopy((char *)frstats, (char *)fiop->f_st, sizeof(filterstats_t) * 2);
2086 fiop->f_locks[0] = fr_state_lock;
2087 fiop->f_locks[1] = fr_nat_lock;
2088 fiop->f_locks[2] = fr_frag_lock;
2089 fiop->f_locks[3] = fr_auth_lock;
2090 fiop->f_fin[0] = ipfilter[0][0];
2091 fiop->f_fin[1] = ipfilter[0][1];
2092 fiop->f_fout[0] = ipfilter[1][0];
2093 fiop->f_fout[1] = ipfilter[1][1];
2094 fiop->f_acctin[0] = ipacct[0][0];
2095 fiop->f_acctin[1] = ipacct[0][1];
2096 fiop->f_acctout[0] = ipacct[1][0];
2097 fiop->f_acctout[1] = ipacct[1][1];
2098#ifdef USE_INET6
2099 fiop->f_fin6[0] = ipfilter6[0][0];
2100 fiop->f_fin6[1] = ipfilter6[0][1];
2101 fiop->f_fout6[0] = ipfilter6[1][0];
2102 fiop->f_fout6[1] = ipfilter6[1][1];
2103 fiop->f_acctin6[0] = ipacct6[0][0];
2104 fiop->f_acctin6[1] = ipacct6[0][1];
2105 fiop->f_acctout6[0] = ipacct6[1][0];
2106 fiop->f_acctout6[1] = ipacct6[1][1];
2107#else
2108 fiop->f_fin6[0] = NULL;
2109 fiop->f_fin6[1] = NULL;
2110 fiop->f_fout6[0] = NULL;
2111 fiop->f_fout6[1] = NULL;
2112 fiop->f_acctin6[0] = NULL;
2113 fiop->f_acctin6[1] = NULL;
2114 fiop->f_acctout6[0] = NULL;
2115 fiop->f_acctout6[1] = NULL;
2116#endif
2117 fiop->f_active = fr_active;
2118 fiop->f_froute[0] = ipl_frouteok[0];
2119 fiop->f_froute[1] = ipl_frouteok[1];
2120
2121 fiop->f_running = fr_running;
2122 fiop->f_groups[0][0] = ipfgroups[0][0];
2123 fiop->f_groups[0][1] = ipfgroups[0][1];
2124 fiop->f_groups[1][0] = ipfgroups[1][0];
2125 fiop->f_groups[1][1] = ipfgroups[1][1];
2126 fiop->f_groups[2][0] = ipfgroups[2][0];
2127 fiop->f_groups[2][1] = ipfgroups[2][1];
2128#ifdef IPFILTER_LOG
2129 fiop->f_logging = 1;
2130#else
2131 fiop->f_logging = 0;
2132#endif
2133 fiop->f_defpass = fr_pass;
2134 strncpy(fiop->f_version, ipfilter_version, sizeof(fiop->f_version));
2135}
2136
2137
2138#ifdef USE_INET6
2139int icmptoicmp6types[ICMP_MAXTYPE+1] = {
2140 ICMP6_ECHO_REPLY, /* 0: ICMP_ECHOREPLY */
2141 -1, /* 1: UNUSED */
2142 -1, /* 2: UNUSED */
2143 ICMP6_DST_UNREACH, /* 3: ICMP_UNREACH */
2144 -1, /* 4: ICMP_SOURCEQUENCH */
2145 ND_REDIRECT, /* 5: ICMP_REDIRECT */
2146 -1, /* 6: UNUSED */
2147 -1, /* 7: UNUSED */
2148 ICMP6_ECHO_REQUEST, /* 8: ICMP_ECHO */
2149 -1, /* 9: UNUSED */
2150 -1, /* 10: UNUSED */
2151 ICMP6_TIME_EXCEEDED, /* 11: ICMP_TIMXCEED */
2152 ICMP6_PARAM_PROB, /* 12: ICMP_PARAMPROB */
2153 -1, /* 13: ICMP_TSTAMP */
2154 -1, /* 14: ICMP_TSTAMPREPLY */
2155 -1, /* 15: ICMP_IREQ */
2156 -1, /* 16: ICMP_IREQREPLY */
2157 -1, /* 17: ICMP_MASKREQ */
2158 -1, /* 18: ICMP_MASKREPLY */
2159};
2160
2161
2162int icmptoicmp6unreach[ICMP_MAX_UNREACH] = {
2163 ICMP6_DST_UNREACH_ADDR, /* 0: ICMP_UNREACH_NET */
2164 ICMP6_DST_UNREACH_ADDR, /* 1: ICMP_UNREACH_HOST */
2165 -1, /* 2: ICMP_UNREACH_PROTOCOL */
2166 ICMP6_DST_UNREACH_NOPORT, /* 3: ICMP_UNREACH_PORT */
2167 -1, /* 4: ICMP_UNREACH_NEEDFRAG */
2168 ICMP6_DST_UNREACH_NOTNEIGHBOR, /* 5: ICMP_UNREACH_SRCFAIL */
2169 ICMP6_DST_UNREACH_ADDR, /* 6: ICMP_UNREACH_NET_UNKNOWN */
2170 ICMP6_DST_UNREACH_ADDR, /* 7: ICMP_UNREACH_HOST_UNKNOWN */
2171 -1, /* 8: ICMP_UNREACH_ISOLATED */
2172 ICMP6_DST_UNREACH_ADMIN, /* 9: ICMP_UNREACH_NET_PROHIB */
2173 ICMP6_DST_UNREACH_ADMIN, /* 10: ICMP_UNREACH_HOST_PROHIB */
2174 -1, /* 11: ICMP_UNREACH_TOSNET */
2175 -1, /* 12: ICMP_UNREACH_TOSHOST */
2176 ICMP6_DST_UNREACH_ADMIN, /* 13: ICMP_UNREACH_ADMIN_PROHIBIT */
2177};
2178#endif