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