fil.c revision 349223
1/* $FreeBSD: stable/11/sys/contrib/ipfilter/netinet/fil.c 349223 2019-06-20 05:01:35Z cy $ */ 2 3/* 4 * Copyright (C) 2012 by Darren Reed. 5 * 6 * See the IPFILTER.LICENCE file for details on licencing. 7 * 8 * Copyright 2008 Sun Microsystems. 9 * 10 * $Id$ 11 * 12 */ 13#if defined(KERNEL) || defined(_KERNEL) 14# undef KERNEL 15# undef _KERNEL 16# define KERNEL 1 17# define _KERNEL 1 18#endif 19#include <sys/errno.h> 20#include <sys/types.h> 21#include <sys/param.h> 22#include <sys/time.h> 23#if defined(_KERNEL) && defined(__FreeBSD_version) 24# if !defined(IPFILTER_LKM) 25# include "opt_inet6.h" 26# endif 27# include <sys/filio.h> 28#else 29# include <sys/ioctl.h> 30#endif 31#if defined(__SVR4) || defined(sun) /* SOLARIS */ 32# include <sys/filio.h> 33#endif 34# include <sys/fcntl.h> 35#if defined(_KERNEL) 36# include <sys/systm.h> 37# include <sys/file.h> 38#else 39# include <stdio.h> 40# include <string.h> 41# include <stdlib.h> 42# include <stddef.h> 43# include <sys/file.h> 44# define _KERNEL 45# include <sys/uio.h> 46# undef _KERNEL 47#endif 48#if !defined(__SVR4) 49# include <sys/mbuf.h> 50#else 51# include <sys/byteorder.h> 52# if (SOLARIS2 < 5) && defined(sun) 53# include <sys/dditypes.h> 54# endif 55#endif 56# include <sys/protosw.h> 57#include <sys/socket.h> 58#include <net/if.h> 59#ifdef sun 60# include <net/af.h> 61#endif 62#include <netinet/in.h> 63#include <netinet/in_systm.h> 64#include <netinet/ip.h> 65#include <netinet/tcp.h> 66# include <netinet/udp.h> 67# include <netinet/ip_icmp.h> 68#include "netinet/ip_compat.h" 69#ifdef USE_INET6 70# include <netinet/icmp6.h> 71# if !SOLARIS && defined(_KERNEL) 72# include <netinet6/in6_var.h> 73# endif 74#endif 75#include "netinet/ip_fil.h" 76#include "netinet/ip_nat.h" 77#include "netinet/ip_frag.h" 78#include "netinet/ip_state.h" 79#include "netinet/ip_proxy.h" 80#include "netinet/ip_auth.h" 81#ifdef IPFILTER_SCAN 82# include "netinet/ip_scan.h" 83#endif 84#include "netinet/ip_sync.h" 85#include "netinet/ip_lookup.h" 86#include "netinet/ip_pool.h" 87#include "netinet/ip_htable.h" 88#ifdef IPFILTER_COMPILED 89# include "netinet/ip_rules.h" 90#endif 91#if defined(IPFILTER_BPF) && defined(_KERNEL) 92# include <net/bpf.h> 93#endif 94#if defined(__FreeBSD_version) 95# include <sys/malloc.h> 96#endif 97#include "netinet/ipl.h" 98 99#if defined(__NetBSD__) && (__NetBSD_Version__ >= 104230000) 100# include <sys/callout.h> 101extern struct callout ipf_slowtimer_ch; 102#endif 103/* END OF INCLUDES */ 104 105#if !defined(lint) 106static const char sccsid[] = "@(#)fil.c 1.36 6/5/96 (C) 1993-2000 Darren Reed"; 107static const char rcsid[] = "@(#)$FreeBSD: stable/11/sys/contrib/ipfilter/netinet/fil.c 349223 2019-06-20 05:01:35Z cy $"; 108/* static const char rcsid[] = "@(#)$Id: fil.c,v 2.243.2.125 2007/10/10 09:27:20 darrenr Exp $"; */ 109#endif 110 111#ifndef _KERNEL 112# include "ipf.h" 113# include "ipt.h" 114extern int opts; 115extern int blockreason; 116#endif /* _KERNEL */ 117 118#define LBUMP(x) softc->x++ 119#define LBUMPD(x, y) do { softc->x.y++; DT(y); } while (0) 120 121static INLINE int ipf_check_ipf __P((fr_info_t *, frentry_t *, int)); 122static u_32_t ipf_checkcipso __P((fr_info_t *, u_char *, int)); 123static u_32_t ipf_checkripso __P((u_char *)); 124static u_32_t ipf_decaps __P((fr_info_t *, u_32_t, int)); 125#ifdef IPFILTER_LOG 126static frentry_t *ipf_dolog __P((fr_info_t *, u_32_t *)); 127#endif 128static int ipf_flushlist __P((ipf_main_softc_t *, int *, 129 frentry_t **)); 130static int ipf_flush_groups __P((ipf_main_softc_t *, frgroup_t **, 131 int)); 132static ipfunc_t ipf_findfunc __P((ipfunc_t)); 133static void *ipf_findlookup __P((ipf_main_softc_t *, int, 134 frentry_t *, 135 i6addr_t *, i6addr_t *)); 136static frentry_t *ipf_firewall __P((fr_info_t *, u_32_t *)); 137static int ipf_fr_matcharray __P((fr_info_t *, int *)); 138static int ipf_frruleiter __P((ipf_main_softc_t *, void *, int, 139 void *)); 140static void ipf_funcfini __P((ipf_main_softc_t *, frentry_t *)); 141static int ipf_funcinit __P((ipf_main_softc_t *, frentry_t *)); 142static int ipf_geniter __P((ipf_main_softc_t *, ipftoken_t *, 143 ipfgeniter_t *)); 144static void ipf_getstat __P((ipf_main_softc_t *, 145 struct friostat *, int)); 146static int ipf_group_flush __P((ipf_main_softc_t *, frgroup_t *)); 147static void ipf_group_free __P((frgroup_t *)); 148static int ipf_grpmapfini __P((struct ipf_main_softc_s *, 149 frentry_t *)); 150static int ipf_grpmapinit __P((struct ipf_main_softc_s *, 151 frentry_t *)); 152static frentry_t *ipf_nextrule __P((ipf_main_softc_t *, int, int, 153 frentry_t *, int)); 154static int ipf_portcheck __P((frpcmp_t *, u_32_t)); 155static INLINE int ipf_pr_ah __P((fr_info_t *)); 156static INLINE void ipf_pr_esp __P((fr_info_t *)); 157static INLINE void ipf_pr_gre __P((fr_info_t *)); 158static INLINE void ipf_pr_udp __P((fr_info_t *)); 159static INLINE void ipf_pr_tcp __P((fr_info_t *)); 160static INLINE void ipf_pr_icmp __P((fr_info_t *)); 161static INLINE void ipf_pr_ipv4hdr __P((fr_info_t *)); 162static INLINE void ipf_pr_short __P((fr_info_t *, int)); 163static INLINE int ipf_pr_tcpcommon __P((fr_info_t *)); 164static INLINE int ipf_pr_udpcommon __P((fr_info_t *)); 165static void ipf_rule_delete __P((ipf_main_softc_t *, frentry_t *f, 166 int, int)); 167static void ipf_rule_expire_insert __P((ipf_main_softc_t *, 168 frentry_t *, int)); 169static int ipf_synclist __P((ipf_main_softc_t *, frentry_t *, 170 void *)); 171static void ipf_token_flush __P((ipf_main_softc_t *)); 172static void ipf_token_unlink __P((ipf_main_softc_t *, 173 ipftoken_t *)); 174static ipftuneable_t *ipf_tune_findbyname __P((ipftuneable_t *, 175 const char *)); 176static ipftuneable_t *ipf_tune_findbycookie __P((ipftuneable_t **, void *, 177 void **)); 178static int ipf_updateipid __P((fr_info_t *)); 179static int ipf_settimeout __P((struct ipf_main_softc_s *, 180 struct ipftuneable *, 181 ipftuneval_t *)); 182#if !defined(_KERNEL) || SOLARIS 183static int ppsratecheck(struct timeval *, int *, int); 184#endif 185 186 187/* 188 * bit values for identifying presence of individual IP options 189 * All of these tables should be ordered by increasing key value on the left 190 * hand side to allow for binary searching of the array and include a trailer 191 * with a 0 for the bitmask for linear searches to easily find the end with. 192 */ 193static const struct optlist ipopts[20] = { 194 { IPOPT_NOP, 0x000001 }, 195 { IPOPT_RR, 0x000002 }, 196 { IPOPT_ZSU, 0x000004 }, 197 { IPOPT_MTUP, 0x000008 }, 198 { IPOPT_MTUR, 0x000010 }, 199 { IPOPT_ENCODE, 0x000020 }, 200 { IPOPT_TS, 0x000040 }, 201 { IPOPT_TR, 0x000080 }, 202 { IPOPT_SECURITY, 0x000100 }, 203 { IPOPT_LSRR, 0x000200 }, 204 { IPOPT_E_SEC, 0x000400 }, 205 { IPOPT_CIPSO, 0x000800 }, 206 { IPOPT_SATID, 0x001000 }, 207 { IPOPT_SSRR, 0x002000 }, 208 { IPOPT_ADDEXT, 0x004000 }, 209 { IPOPT_VISA, 0x008000 }, 210 { IPOPT_IMITD, 0x010000 }, 211 { IPOPT_EIP, 0x020000 }, 212 { IPOPT_FINN, 0x040000 }, 213 { 0, 0x000000 } 214}; 215 216#ifdef USE_INET6 217static const struct optlist ip6exthdr[] = { 218 { IPPROTO_HOPOPTS, 0x000001 }, 219 { IPPROTO_IPV6, 0x000002 }, 220 { IPPROTO_ROUTING, 0x000004 }, 221 { IPPROTO_FRAGMENT, 0x000008 }, 222 { IPPROTO_ESP, 0x000010 }, 223 { IPPROTO_AH, 0x000020 }, 224 { IPPROTO_NONE, 0x000040 }, 225 { IPPROTO_DSTOPTS, 0x000080 }, 226 { IPPROTO_MOBILITY, 0x000100 }, 227 { 0, 0 } 228}; 229#endif 230 231/* 232 * bit values for identifying presence of individual IP security options 233 */ 234static const struct optlist secopt[8] = { 235 { IPSO_CLASS_RES4, 0x01 }, 236 { IPSO_CLASS_TOPS, 0x02 }, 237 { IPSO_CLASS_SECR, 0x04 }, 238 { IPSO_CLASS_RES3, 0x08 }, 239 { IPSO_CLASS_CONF, 0x10 }, 240 { IPSO_CLASS_UNCL, 0x20 }, 241 { IPSO_CLASS_RES2, 0x40 }, 242 { IPSO_CLASS_RES1, 0x80 } 243}; 244 245char ipfilter_version[] = IPL_VERSION; 246 247int ipf_features = 0 248#ifdef IPFILTER_LKM 249 | IPF_FEAT_LKM 250#endif 251#ifdef IPFILTER_LOG 252 | IPF_FEAT_LOG 253#endif 254 | IPF_FEAT_LOOKUP 255#ifdef IPFILTER_BPF 256 | IPF_FEAT_BPF 257#endif 258#ifdef IPFILTER_COMPILED 259 | IPF_FEAT_COMPILED 260#endif 261#ifdef IPFILTER_CKSUM 262 | IPF_FEAT_CKSUM 263#endif 264 | IPF_FEAT_SYNC 265#ifdef IPFILTER_SCAN 266 | IPF_FEAT_SCAN 267#endif 268#ifdef USE_INET6 269 | IPF_FEAT_IPV6 270#endif 271 ; 272 273 274/* 275 * Table of functions available for use with call rules. 276 */ 277static ipfunc_resolve_t ipf_availfuncs[] = { 278 { "srcgrpmap", ipf_srcgrpmap, ipf_grpmapinit, ipf_grpmapfini }, 279 { "dstgrpmap", ipf_dstgrpmap, ipf_grpmapinit, ipf_grpmapfini }, 280 { "", NULL, NULL, NULL } 281}; 282 283static ipftuneable_t ipf_main_tuneables[] = { 284 { { (void *)offsetof(struct ipf_main_softc_s, ipf_flags) }, 285 "ipf_flags", 0, 0xffffffff, 286 stsizeof(ipf_main_softc_t, ipf_flags), 287 0, NULL, NULL }, 288 { { (void *)offsetof(struct ipf_main_softc_s, ipf_active) }, 289 "active", 0, 0, 290 stsizeof(ipf_main_softc_t, ipf_active), 291 IPFT_RDONLY, NULL, NULL }, 292 { { (void *)offsetof(ipf_main_softc_t, ipf_control_forwarding) }, 293 "control_forwarding", 0, 1, 294 stsizeof(ipf_main_softc_t, ipf_control_forwarding), 295 0, NULL, NULL }, 296 { { (void *)offsetof(ipf_main_softc_t, ipf_update_ipid) }, 297 "update_ipid", 0, 1, 298 stsizeof(ipf_main_softc_t, ipf_update_ipid), 299 0, NULL, NULL }, 300 { { (void *)offsetof(ipf_main_softc_t, ipf_chksrc) }, 301 "chksrc", 0, 1, 302 stsizeof(ipf_main_softc_t, ipf_chksrc), 303 0, NULL, NULL }, 304 { { (void *)offsetof(ipf_main_softc_t, ipf_minttl) }, 305 "min_ttl", 0, 1, 306 stsizeof(ipf_main_softc_t, ipf_minttl), 307 0, NULL, NULL }, 308 { { (void *)offsetof(ipf_main_softc_t, ipf_icmpminfragmtu) }, 309 "icmp_minfragmtu", 0, 1, 310 stsizeof(ipf_main_softc_t, ipf_icmpminfragmtu), 311 0, NULL, NULL }, 312 { { (void *)offsetof(ipf_main_softc_t, ipf_pass) }, 313 "default_pass", 0, 0xffffffff, 314 stsizeof(ipf_main_softc_t, ipf_pass), 315 0, NULL, NULL }, 316 { { (void *)offsetof(ipf_main_softc_t, ipf_tcpidletimeout) }, 317 "tcp_idle_timeout", 1, 0x7fffffff, 318 stsizeof(ipf_main_softc_t, ipf_tcpidletimeout), 319 0, NULL, ipf_settimeout }, 320 { { (void *)offsetof(ipf_main_softc_t, ipf_tcpclosewait) }, 321 "tcp_close_wait", 1, 0x7fffffff, 322 stsizeof(ipf_main_softc_t, ipf_tcpclosewait), 323 0, NULL, ipf_settimeout }, 324 { { (void *)offsetof(ipf_main_softc_t, ipf_tcplastack) }, 325 "tcp_last_ack", 1, 0x7fffffff, 326 stsizeof(ipf_main_softc_t, ipf_tcplastack), 327 0, NULL, ipf_settimeout }, 328 { { (void *)offsetof(ipf_main_softc_t, ipf_tcptimeout) }, 329 "tcp_timeout", 1, 0x7fffffff, 330 stsizeof(ipf_main_softc_t, ipf_tcptimeout), 331 0, NULL, ipf_settimeout }, 332 { { (void *)offsetof(ipf_main_softc_t, ipf_tcpsynsent) }, 333 "tcp_syn_sent", 1, 0x7fffffff, 334 stsizeof(ipf_main_softc_t, ipf_tcpsynsent), 335 0, NULL, ipf_settimeout }, 336 { { (void *)offsetof(ipf_main_softc_t, ipf_tcpsynrecv) }, 337 "tcp_syn_received", 1, 0x7fffffff, 338 stsizeof(ipf_main_softc_t, ipf_tcpsynrecv), 339 0, NULL, ipf_settimeout }, 340 { { (void *)offsetof(ipf_main_softc_t, ipf_tcpclosed) }, 341 "tcp_closed", 1, 0x7fffffff, 342 stsizeof(ipf_main_softc_t, ipf_tcpclosed), 343 0, NULL, ipf_settimeout }, 344 { { (void *)offsetof(ipf_main_softc_t, ipf_tcphalfclosed) }, 345 "tcp_half_closed", 1, 0x7fffffff, 346 stsizeof(ipf_main_softc_t, ipf_tcphalfclosed), 347 0, NULL, ipf_settimeout }, 348 { { (void *)offsetof(ipf_main_softc_t, ipf_tcptimewait) }, 349 "tcp_time_wait", 1, 0x7fffffff, 350 stsizeof(ipf_main_softc_t, ipf_tcptimewait), 351 0, NULL, ipf_settimeout }, 352 { { (void *)offsetof(ipf_main_softc_t, ipf_udptimeout) }, 353 "udp_timeout", 1, 0x7fffffff, 354 stsizeof(ipf_main_softc_t, ipf_udptimeout), 355 0, NULL, ipf_settimeout }, 356 { { (void *)offsetof(ipf_main_softc_t, ipf_udpacktimeout) }, 357 "udp_ack_timeout", 1, 0x7fffffff, 358 stsizeof(ipf_main_softc_t, ipf_udpacktimeout), 359 0, NULL, ipf_settimeout }, 360 { { (void *)offsetof(ipf_main_softc_t, ipf_icmptimeout) }, 361 "icmp_timeout", 1, 0x7fffffff, 362 stsizeof(ipf_main_softc_t, ipf_icmptimeout), 363 0, NULL, ipf_settimeout }, 364 { { (void *)offsetof(ipf_main_softc_t, ipf_icmpacktimeout) }, 365 "icmp_ack_timeout", 1, 0x7fffffff, 366 stsizeof(ipf_main_softc_t, ipf_icmpacktimeout), 367 0, NULL, ipf_settimeout }, 368 { { (void *)offsetof(ipf_main_softc_t, ipf_iptimeout) }, 369 "ip_timeout", 1, 0x7fffffff, 370 stsizeof(ipf_main_softc_t, ipf_iptimeout), 371 0, NULL, ipf_settimeout }, 372#if defined(INSTANCES) && defined(_KERNEL) 373 { { (void *)offsetof(ipf_main_softc_t, ipf_get_loopback) }, 374 "intercept_loopback", 0, 1, 375 stsizeof(ipf_main_softc_t, ipf_get_loopback), 376 0, NULL, ipf_set_loopback }, 377#endif 378 { { 0 }, 379 NULL, 0, 0, 380 0, 381 0, NULL, NULL } 382}; 383 384 385/* 386 * The next section of code is a collection of small routines that set 387 * fields in the fr_info_t structure passed based on properties of the 388 * current packet. There are different routines for the same protocol 389 * for each of IPv4 and IPv6. Adding a new protocol, for which there 390 * will "special" inspection for setup, is now more easily done by adding 391 * a new routine and expanding the ipf_pr_ipinit*() function rather than by 392 * adding more code to a growing switch statement. 393 */ 394#ifdef USE_INET6 395static INLINE int ipf_pr_ah6 __P((fr_info_t *)); 396static INLINE void ipf_pr_esp6 __P((fr_info_t *)); 397static INLINE void ipf_pr_gre6 __P((fr_info_t *)); 398static INLINE void ipf_pr_udp6 __P((fr_info_t *)); 399static INLINE void ipf_pr_tcp6 __P((fr_info_t *)); 400static INLINE void ipf_pr_icmp6 __P((fr_info_t *)); 401static INLINE void ipf_pr_ipv6hdr __P((fr_info_t *)); 402static INLINE void ipf_pr_short6 __P((fr_info_t *, int)); 403static INLINE int ipf_pr_hopopts6 __P((fr_info_t *)); 404static INLINE int ipf_pr_mobility6 __P((fr_info_t *)); 405static INLINE int ipf_pr_routing6 __P((fr_info_t *)); 406static INLINE int ipf_pr_dstopts6 __P((fr_info_t *)); 407static INLINE int ipf_pr_fragment6 __P((fr_info_t *)); 408static INLINE struct ip6_ext *ipf_pr_ipv6exthdr __P((fr_info_t *, int, int)); 409 410 411/* ------------------------------------------------------------------------ */ 412/* Function: ipf_pr_short6 */ 413/* Returns: void */ 414/* Parameters: fin(I) - pointer to packet information */ 415/* xmin(I) - minimum header size */ 416/* */ 417/* IPv6 Only */ 418/* This is function enforces the 'is a packet too short to be legit' rule */ 419/* for IPv6 and marks the packet with FI_SHORT if so. See function comment */ 420/* for ipf_pr_short() for more details. */ 421/* ------------------------------------------------------------------------ */ 422static INLINE void 423ipf_pr_short6(fin, xmin) 424 fr_info_t *fin; 425 int xmin; 426{ 427 428 if (fin->fin_dlen < xmin) 429 fin->fin_flx |= FI_SHORT; 430} 431 432 433/* ------------------------------------------------------------------------ */ 434/* Function: ipf_pr_ipv6hdr */ 435/* Returns: void */ 436/* Parameters: fin(I) - pointer to packet information */ 437/* */ 438/* IPv6 Only */ 439/* Copy values from the IPv6 header into the fr_info_t struct and call the */ 440/* per-protocol analyzer if it exists. In validating the packet, a protocol*/ 441/* analyzer may pullup or free the packet itself so we need to be vigiliant */ 442/* of that possibility arising. */ 443/* ------------------------------------------------------------------------ */ 444static INLINE void 445ipf_pr_ipv6hdr(fin) 446 fr_info_t *fin; 447{ 448 ip6_t *ip6 = (ip6_t *)fin->fin_ip; 449 int p, go = 1, i, hdrcount; 450 fr_ip_t *fi = &fin->fin_fi; 451 452 fin->fin_off = 0; 453 454 fi->fi_tos = 0; 455 fi->fi_optmsk = 0; 456 fi->fi_secmsk = 0; 457 fi->fi_auth = 0; 458 459 p = ip6->ip6_nxt; 460 fin->fin_crc = p; 461 fi->fi_ttl = ip6->ip6_hlim; 462 fi->fi_src.in6 = ip6->ip6_src; 463 fin->fin_crc += fi->fi_src.i6[0]; 464 fin->fin_crc += fi->fi_src.i6[1]; 465 fin->fin_crc += fi->fi_src.i6[2]; 466 fin->fin_crc += fi->fi_src.i6[3]; 467 fi->fi_dst.in6 = ip6->ip6_dst; 468 fin->fin_crc += fi->fi_dst.i6[0]; 469 fin->fin_crc += fi->fi_dst.i6[1]; 470 fin->fin_crc += fi->fi_dst.i6[2]; 471 fin->fin_crc += fi->fi_dst.i6[3]; 472 fin->fin_id = 0; 473 if (IN6_IS_ADDR_MULTICAST(&fi->fi_dst.in6)) 474 fin->fin_flx |= FI_MULTICAST|FI_MBCAST; 475 476 hdrcount = 0; 477 while (go && !(fin->fin_flx & FI_SHORT)) { 478 switch (p) 479 { 480 case IPPROTO_UDP : 481 ipf_pr_udp6(fin); 482 go = 0; 483 break; 484 485 case IPPROTO_TCP : 486 ipf_pr_tcp6(fin); 487 go = 0; 488 break; 489 490 case IPPROTO_ICMPV6 : 491 ipf_pr_icmp6(fin); 492 go = 0; 493 break; 494 495 case IPPROTO_GRE : 496 ipf_pr_gre6(fin); 497 go = 0; 498 break; 499 500 case IPPROTO_HOPOPTS : 501 p = ipf_pr_hopopts6(fin); 502 break; 503 504 case IPPROTO_MOBILITY : 505 p = ipf_pr_mobility6(fin); 506 break; 507 508 case IPPROTO_DSTOPTS : 509 p = ipf_pr_dstopts6(fin); 510 break; 511 512 case IPPROTO_ROUTING : 513 p = ipf_pr_routing6(fin); 514 break; 515 516 case IPPROTO_AH : 517 p = ipf_pr_ah6(fin); 518 break; 519 520 case IPPROTO_ESP : 521 ipf_pr_esp6(fin); 522 go = 0; 523 break; 524 525 case IPPROTO_IPV6 : 526 for (i = 0; ip6exthdr[i].ol_bit != 0; i++) 527 if (ip6exthdr[i].ol_val == p) { 528 fin->fin_flx |= ip6exthdr[i].ol_bit; 529 break; 530 } 531 go = 0; 532 break; 533 534 case IPPROTO_NONE : 535 go = 0; 536 break; 537 538 case IPPROTO_FRAGMENT : 539 p = ipf_pr_fragment6(fin); 540 /* 541 * Given that the only fragments we want to let through 542 * (where fin_off != 0) are those where the non-first 543 * fragments only have data, we can safely stop looking 544 * at headers if this is a non-leading fragment. 545 */ 546 if (fin->fin_off != 0) 547 go = 0; 548 break; 549 550 default : 551 go = 0; 552 break; 553 } 554 hdrcount++; 555 556 /* 557 * It is important to note that at this point, for the 558 * extension headers (go != 0), the entire header may not have 559 * been pulled up when the code gets to this point. This is 560 * only done for "go != 0" because the other header handlers 561 * will all pullup their complete header. The other indicator 562 * of an incomplete packet is that this was just an extension 563 * header. 564 */ 565 if ((go != 0) && (p != IPPROTO_NONE) && 566 (ipf_pr_pullup(fin, 0) == -1)) { 567 p = IPPROTO_NONE; 568 break; 569 } 570 } 571 572 /* 573 * Some of the above functions, like ipf_pr_esp6(), can call ipf_pullup 574 * and destroy whatever packet was here. The caller of this function 575 * expects us to return if there is a problem with ipf_pullup. 576 */ 577 if (fin->fin_m == NULL) { 578 ipf_main_softc_t *softc = fin->fin_main_soft; 579 580 LBUMPD(ipf_stats[fin->fin_out], fr_v6_bad); 581 return; 582 } 583 584 fi->fi_p = p; 585 586 /* 587 * IPv6 fragment case 1 - see comment for ipf_pr_fragment6(). 588 * "go != 0" imples the above loop hasn't arrived at a layer 4 header. 589 */ 590 if ((go != 0) && (fin->fin_flx & FI_FRAG) && (fin->fin_off == 0)) { 591 ipf_main_softc_t *softc = fin->fin_main_soft; 592 593 fin->fin_flx |= FI_BAD; 594 DT2(ipf_fi_bad_ipv6_frag_1, fr_info_t *, fin, int, go); 595 LBUMPD(ipf_stats[fin->fin_out], fr_v6_badfrag); 596 LBUMP(ipf_stats[fin->fin_out].fr_v6_bad); 597 } 598} 599 600 601/* ------------------------------------------------------------------------ */ 602/* Function: ipf_pr_ipv6exthdr */ 603/* Returns: struct ip6_ext * - pointer to the start of the next header */ 604/* or NULL if there is a prolblem. */ 605/* Parameters: fin(I) - pointer to packet information */ 606/* multiple(I) - flag indicating yes/no if multiple occurances */ 607/* of this extension header are allowed. */ 608/* proto(I) - protocol number for this extension header */ 609/* */ 610/* IPv6 Only */ 611/* This function embodies a number of common checks that all IPv6 extension */ 612/* headers must be subjected to. For example, making sure the packet is */ 613/* big enough for it to be in, checking if it is repeated and setting a */ 614/* flag to indicate its presence. */ 615/* ------------------------------------------------------------------------ */ 616static INLINE struct ip6_ext * 617ipf_pr_ipv6exthdr(fin, multiple, proto) 618 fr_info_t *fin; 619 int multiple, proto; 620{ 621 ipf_main_softc_t *softc = fin->fin_main_soft; 622 struct ip6_ext *hdr; 623 u_short shift; 624 int i; 625 626 fin->fin_flx |= FI_V6EXTHDR; 627 628 /* 8 is default length of extension hdr */ 629 if ((fin->fin_dlen - 8) < 0) { 630 fin->fin_flx |= FI_SHORT; 631 LBUMPD(ipf_stats[fin->fin_out], fr_v6_ext_short); 632 return NULL; 633 } 634 635 if (ipf_pr_pullup(fin, 8) == -1) { 636 LBUMPD(ipf_stats[fin->fin_out], fr_v6_ext_pullup); 637 return NULL; 638 } 639 640 hdr = fin->fin_dp; 641 switch (proto) 642 { 643 case IPPROTO_FRAGMENT : 644 shift = 8; 645 break; 646 default : 647 shift = 8 + (hdr->ip6e_len << 3); 648 break; 649 } 650 651 if (shift > fin->fin_dlen) { /* Nasty extension header length? */ 652 fin->fin_flx |= FI_BAD; 653 DT3(ipf_fi_bad_pr_ipv6exthdr_len, fr_info_t *, fin, u_short, shift, u_short, fin->fin_dlen); 654 LBUMPD(ipf_stats[fin->fin_out], fr_v6_ext_hlen); 655 return NULL; 656 } 657 658 fin->fin_dp = (char *)fin->fin_dp + shift; 659 fin->fin_dlen -= shift; 660 661 /* 662 * If we have seen a fragment header, do not set any flags to indicate 663 * the presence of this extension header as it has no impact on the 664 * end result until after it has been defragmented. 665 */ 666 if (fin->fin_flx & FI_FRAG) 667 return hdr; 668 669 for (i = 0; ip6exthdr[i].ol_bit != 0; i++) 670 if (ip6exthdr[i].ol_val == proto) { 671 /* 672 * Most IPv6 extension headers are only allowed once. 673 */ 674 if ((multiple == 0) && 675 ((fin->fin_optmsk & ip6exthdr[i].ol_bit) != 0)) { 676 fin->fin_flx |= FI_BAD; 677 DT2(ipf_fi_bad_ipv6exthdr_once, fr_info_t *, fin, u_int, (fin->fin_optmsk & ip6exthdr[i].ol_bit)); 678 } else 679 fin->fin_optmsk |= ip6exthdr[i].ol_bit; 680 break; 681 } 682 683 return hdr; 684} 685 686 687/* ------------------------------------------------------------------------ */ 688/* Function: ipf_pr_hopopts6 */ 689/* Returns: int - value of the next header or IPPROTO_NONE if error */ 690/* Parameters: fin(I) - pointer to packet information */ 691/* */ 692/* IPv6 Only */ 693/* This is function checks pending hop by hop options extension header */ 694/* ------------------------------------------------------------------------ */ 695static INLINE int 696ipf_pr_hopopts6(fin) 697 fr_info_t *fin; 698{ 699 struct ip6_ext *hdr; 700 701 hdr = ipf_pr_ipv6exthdr(fin, 0, IPPROTO_HOPOPTS); 702 if (hdr == NULL) 703 return IPPROTO_NONE; 704 return hdr->ip6e_nxt; 705} 706 707 708/* ------------------------------------------------------------------------ */ 709/* Function: ipf_pr_mobility6 */ 710/* Returns: int - value of the next header or IPPROTO_NONE if error */ 711/* Parameters: fin(I) - pointer to packet information */ 712/* */ 713/* IPv6 Only */ 714/* This is function checks the IPv6 mobility extension header */ 715/* ------------------------------------------------------------------------ */ 716static INLINE int 717ipf_pr_mobility6(fin) 718 fr_info_t *fin; 719{ 720 struct ip6_ext *hdr; 721 722 hdr = ipf_pr_ipv6exthdr(fin, 0, IPPROTO_MOBILITY); 723 if (hdr == NULL) 724 return IPPROTO_NONE; 725 return hdr->ip6e_nxt; 726} 727 728 729/* ------------------------------------------------------------------------ */ 730/* Function: ipf_pr_routing6 */ 731/* Returns: int - value of the next header or IPPROTO_NONE if error */ 732/* Parameters: fin(I) - pointer to packet information */ 733/* */ 734/* IPv6 Only */ 735/* This is function checks pending routing extension header */ 736/* ------------------------------------------------------------------------ */ 737static INLINE int 738ipf_pr_routing6(fin) 739 fr_info_t *fin; 740{ 741 struct ip6_routing *hdr; 742 743 hdr = (struct ip6_routing *)ipf_pr_ipv6exthdr(fin, 0, IPPROTO_ROUTING); 744 if (hdr == NULL) 745 return IPPROTO_NONE; 746 747 switch (hdr->ip6r_type) 748 { 749 case 0 : 750 /* 751 * Nasty extension header length? 752 */ 753 if (((hdr->ip6r_len >> 1) < hdr->ip6r_segleft) || 754 (hdr->ip6r_segleft && (hdr->ip6r_len & 1))) { 755 ipf_main_softc_t *softc = fin->fin_main_soft; 756 757 fin->fin_flx |= FI_BAD; 758 DT1(ipf_fi_bad_routing6, fr_info_t *, fin); 759 LBUMPD(ipf_stats[fin->fin_out], fr_v6_rh_bad); 760 return IPPROTO_NONE; 761 } 762 break; 763 764 default : 765 break; 766 } 767 768 return hdr->ip6r_nxt; 769} 770 771 772/* ------------------------------------------------------------------------ */ 773/* Function: ipf_pr_fragment6 */ 774/* Returns: int - value of the next header or IPPROTO_NONE if error */ 775/* Parameters: fin(I) - pointer to packet information */ 776/* */ 777/* IPv6 Only */ 778/* Examine the IPv6 fragment header and extract fragment offset information.*/ 779/* */ 780/* Fragments in IPv6 are extraordinarily difficult to deal with - much more */ 781/* so than in IPv4. There are 5 cases of fragments with IPv6 that all */ 782/* packets with a fragment header can fit into. They are as follows: */ 783/* */ 784/* 1. [IPv6][0-n EH][FH][0-n EH] (no L4HDR present) */ 785/* 2. [IPV6][0-n EH][FH][0-n EH][L4HDR part] (short) */ 786/* 3. [IPV6][0-n EH][FH][L4HDR part][0-n data] (short) */ 787/* 4. [IPV6][0-n EH][FH][0-n EH][L4HDR][0-n data] */ 788/* 5. [IPV6][0-n EH][FH][data] */ 789/* */ 790/* IPV6 = IPv6 header, FH = Fragment Header, */ 791/* 0-n EH = 0 or more extension headers, 0-n data = 0 or more bytes of data */ 792/* */ 793/* Packets that match 1, 2, 3 will be dropped as the only reasonable */ 794/* scenario in which they happen is in extreme circumstances that are most */ 795/* likely to be an indication of an attack rather than normal traffic. */ 796/* A type 3 packet may be sent by an attacked after a type 4 packet. There */ 797/* are two rules that can be used to guard against type 3 packets: L4 */ 798/* headers must always be in a packet that has the offset field set to 0 */ 799/* and no packet is allowed to overlay that where offset = 0. */ 800/* ------------------------------------------------------------------------ */ 801static INLINE int 802ipf_pr_fragment6(fin) 803 fr_info_t *fin; 804{ 805 ipf_main_softc_t *softc = fin->fin_main_soft; 806 struct ip6_frag *frag; 807 808 fin->fin_flx |= FI_FRAG; 809 810 frag = (struct ip6_frag *)ipf_pr_ipv6exthdr(fin, 0, IPPROTO_FRAGMENT); 811 if (frag == NULL) { 812 LBUMPD(ipf_stats[fin->fin_out], fr_v6_frag_bad); 813 return IPPROTO_NONE; 814 } 815 816 if ((frag->ip6f_offlg & IP6F_MORE_FRAG) != 0) { 817 /* 818 * Any fragment that isn't the last fragment must have its 819 * length as a multiple of 8. 820 */ 821 if ((fin->fin_plen & 7) != 0) { 822 fin->fin_flx |= FI_BAD; 823 DT2(ipf_fi_bad_frag_not_8, fr_info_t *, fin, u_int, (fin->fin_plen & 7)); 824 } 825 } 826 827 fin->fin_fraghdr = frag; 828 fin->fin_id = frag->ip6f_ident; 829 fin->fin_off = ntohs(frag->ip6f_offlg & IP6F_OFF_MASK); 830 if (fin->fin_off != 0) 831 fin->fin_flx |= FI_FRAGBODY; 832 833 /* 834 * Jumbograms aren't handled, so the max. length is 64k 835 */ 836 if ((fin->fin_off << 3) + fin->fin_dlen > 65535) { 837 fin->fin_flx |= FI_BAD; 838 DT2(ipf_fi_bad_jumbogram, fr_info_t *, fin, u_int, ((fin->fin_off << 3) + fin->fin_dlen)); 839 } 840 841 /* 842 * We don't know where the transport layer header (or whatever is next 843 * is), as it could be behind destination options (amongst others) so 844 * return the fragment header as the type of packet this is. Note that 845 * this effectively disables the fragment cache for > 1 protocol at a 846 * time. 847 */ 848 return frag->ip6f_nxt; 849} 850 851 852/* ------------------------------------------------------------------------ */ 853/* Function: ipf_pr_dstopts6 */ 854/* Returns: int - value of the next header or IPPROTO_NONE if error */ 855/* Parameters: fin(I) - pointer to packet information */ 856/* */ 857/* IPv6 Only */ 858/* This is function checks pending destination options extension header */ 859/* ------------------------------------------------------------------------ */ 860static INLINE int 861ipf_pr_dstopts6(fin) 862 fr_info_t *fin; 863{ 864 ipf_main_softc_t *softc = fin->fin_main_soft; 865 struct ip6_ext *hdr; 866 867 hdr = ipf_pr_ipv6exthdr(fin, 0, IPPROTO_DSTOPTS); 868 if (hdr == NULL) { 869 LBUMPD(ipf_stats[fin->fin_out], fr_v6_dst_bad); 870 return IPPROTO_NONE; 871 } 872 return hdr->ip6e_nxt; 873} 874 875 876/* ------------------------------------------------------------------------ */ 877/* Function: ipf_pr_icmp6 */ 878/* Returns: void */ 879/* Parameters: fin(I) - pointer to packet information */ 880/* */ 881/* IPv6 Only */ 882/* This routine is mainly concerned with determining the minimum valid size */ 883/* for an ICMPv6 packet. */ 884/* ------------------------------------------------------------------------ */ 885static INLINE void 886ipf_pr_icmp6(fin) 887 fr_info_t *fin; 888{ 889 int minicmpsz = sizeof(struct icmp6_hdr); 890 struct icmp6_hdr *icmp6; 891 892 if (ipf_pr_pullup(fin, ICMP6ERR_MINPKTLEN - sizeof(ip6_t)) == -1) { 893 ipf_main_softc_t *softc = fin->fin_main_soft; 894 895 LBUMPD(ipf_stats[fin->fin_out], fr_v6_icmp6_pullup); 896 return; 897 } 898 899 if (fin->fin_dlen > 1) { 900 ip6_t *ip6; 901 902 icmp6 = fin->fin_dp; 903 904 fin->fin_data[0] = *(u_short *)icmp6; 905 906 if ((icmp6->icmp6_type & ICMP6_INFOMSG_MASK) != 0) 907 fin->fin_flx |= FI_ICMPQUERY; 908 909 switch (icmp6->icmp6_type) 910 { 911 case ICMP6_ECHO_REPLY : 912 case ICMP6_ECHO_REQUEST : 913 if (fin->fin_dlen >= 6) 914 fin->fin_data[1] = icmp6->icmp6_id; 915 minicmpsz = ICMP6ERR_MINPKTLEN - sizeof(ip6_t); 916 break; 917 918 case ICMP6_DST_UNREACH : 919 case ICMP6_PACKET_TOO_BIG : 920 case ICMP6_TIME_EXCEEDED : 921 case ICMP6_PARAM_PROB : 922 fin->fin_flx |= FI_ICMPERR; 923 minicmpsz = ICMP6ERR_IPICMPHLEN - sizeof(ip6_t); 924 if (fin->fin_plen < ICMP6ERR_IPICMPHLEN) 925 break; 926 927 if (M_LEN(fin->fin_m) < fin->fin_plen) { 928 if (ipf_coalesce(fin) != 1) 929 return; 930 } 931 932 if (ipf_pr_pullup(fin, ICMP6ERR_MINPKTLEN) == -1) 933 return; 934 935 /* 936 * If the destination of this packet doesn't match the 937 * source of the original packet then this packet is 938 * not correct. 939 */ 940 icmp6 = fin->fin_dp; 941 ip6 = (ip6_t *)((char *)icmp6 + ICMPERR_ICMPHLEN); 942 if (IP6_NEQ(&fin->fin_fi.fi_dst, 943 (i6addr_t *)&ip6->ip6_src)) { 944 fin->fin_flx |= FI_BAD; 945 DT1(ipf_fi_bad_icmp6, fr_info_t *, fin); 946 } 947 break; 948 default : 949 break; 950 } 951 } 952 953 ipf_pr_short6(fin, minicmpsz); 954 if ((fin->fin_flx & (FI_SHORT|FI_BAD)) == 0) { 955 u_char p = fin->fin_p; 956 957 fin->fin_p = IPPROTO_ICMPV6; 958 ipf_checkv6sum(fin); 959 fin->fin_p = p; 960 } 961} 962 963 964/* ------------------------------------------------------------------------ */ 965/* Function: ipf_pr_udp6 */ 966/* Returns: void */ 967/* Parameters: fin(I) - pointer to packet information */ 968/* */ 969/* IPv6 Only */ 970/* Analyse the packet for IPv6/UDP properties. */ 971/* Is not expected to be called for fragmented packets. */ 972/* ------------------------------------------------------------------------ */ 973static INLINE void 974ipf_pr_udp6(fin) 975 fr_info_t *fin; 976{ 977 978 if (ipf_pr_udpcommon(fin) == 0) { 979 u_char p = fin->fin_p; 980 981 fin->fin_p = IPPROTO_UDP; 982 ipf_checkv6sum(fin); 983 fin->fin_p = p; 984 } 985} 986 987 988/* ------------------------------------------------------------------------ */ 989/* Function: ipf_pr_tcp6 */ 990/* Returns: void */ 991/* Parameters: fin(I) - pointer to packet information */ 992/* */ 993/* IPv6 Only */ 994/* Analyse the packet for IPv6/TCP properties. */ 995/* Is not expected to be called for fragmented packets. */ 996/* ------------------------------------------------------------------------ */ 997static INLINE void 998ipf_pr_tcp6(fin) 999 fr_info_t *fin; 1000{ 1001 1002 if (ipf_pr_tcpcommon(fin) == 0) { 1003 u_char p = fin->fin_p; 1004 1005 fin->fin_p = IPPROTO_TCP; 1006 ipf_checkv6sum(fin); 1007 fin->fin_p = p; 1008 } 1009} 1010 1011 1012/* ------------------------------------------------------------------------ */ 1013/* Function: ipf_pr_esp6 */ 1014/* Returns: void */ 1015/* Parameters: fin(I) - pointer to packet information */ 1016/* */ 1017/* IPv6 Only */ 1018/* Analyse the packet for ESP properties. */ 1019/* The minimum length is taken to be the SPI (32bits) plus a tail (32bits) */ 1020/* even though the newer ESP packets must also have a sequence number that */ 1021/* is 32bits as well, it is not possible(?) to determine the version from a */ 1022/* simple packet header. */ 1023/* ------------------------------------------------------------------------ */ 1024static INLINE void 1025ipf_pr_esp6(fin) 1026 fr_info_t *fin; 1027{ 1028 1029 if ((fin->fin_off == 0) && (ipf_pr_pullup(fin, 8) == -1)) { 1030 ipf_main_softc_t *softc = fin->fin_main_soft; 1031 1032 LBUMPD(ipf_stats[fin->fin_out], fr_v6_esp_pullup); 1033 return; 1034 } 1035} 1036 1037 1038/* ------------------------------------------------------------------------ */ 1039/* Function: ipf_pr_ah6 */ 1040/* Returns: int - value of the next header or IPPROTO_NONE if error */ 1041/* Parameters: fin(I) - pointer to packet information */ 1042/* */ 1043/* IPv6 Only */ 1044/* Analyse the packet for AH properties. */ 1045/* The minimum length is taken to be the combination of all fields in the */ 1046/* header being present and no authentication data (null algorithm used.) */ 1047/* ------------------------------------------------------------------------ */ 1048static INLINE int 1049ipf_pr_ah6(fin) 1050 fr_info_t *fin; 1051{ 1052 authhdr_t *ah; 1053 1054 fin->fin_flx |= FI_AH; 1055 1056 ah = (authhdr_t *)ipf_pr_ipv6exthdr(fin, 0, IPPROTO_HOPOPTS); 1057 if (ah == NULL) { 1058 ipf_main_softc_t *softc = fin->fin_main_soft; 1059 1060 LBUMPD(ipf_stats[fin->fin_out], fr_v6_ah_bad); 1061 return IPPROTO_NONE; 1062 } 1063 1064 ipf_pr_short6(fin, sizeof(*ah)); 1065 1066 /* 1067 * No need for another pullup, ipf_pr_ipv6exthdr() will pullup 1068 * enough data to satisfy ah_next (the very first one.) 1069 */ 1070 return ah->ah_next; 1071} 1072 1073 1074/* ------------------------------------------------------------------------ */ 1075/* Function: ipf_pr_gre6 */ 1076/* Returns: void */ 1077/* Parameters: fin(I) - pointer to packet information */ 1078/* */ 1079/* Analyse the packet for GRE properties. */ 1080/* ------------------------------------------------------------------------ */ 1081static INLINE void 1082ipf_pr_gre6(fin) 1083 fr_info_t *fin; 1084{ 1085 grehdr_t *gre; 1086 1087 if (ipf_pr_pullup(fin, sizeof(grehdr_t)) == -1) { 1088 ipf_main_softc_t *softc = fin->fin_main_soft; 1089 1090 LBUMPD(ipf_stats[fin->fin_out], fr_v6_gre_pullup); 1091 return; 1092 } 1093 1094 gre = fin->fin_dp; 1095 if (GRE_REV(gre->gr_flags) == 1) 1096 fin->fin_data[0] = gre->gr_call; 1097} 1098#endif /* USE_INET6 */ 1099 1100 1101/* ------------------------------------------------------------------------ */ 1102/* Function: ipf_pr_pullup */ 1103/* Returns: int - 0 == pullup succeeded, -1 == failure */ 1104/* Parameters: fin(I) - pointer to packet information */ 1105/* plen(I) - length (excluding L3 header) to pullup */ 1106/* */ 1107/* Short inline function to cut down on code duplication to perform a call */ 1108/* to ipf_pullup to ensure there is the required amount of data, */ 1109/* consecutively in the packet buffer. */ 1110/* */ 1111/* This function pulls up 'extra' data at the location of fin_dp. fin_dp */ 1112/* points to the first byte after the complete layer 3 header, which will */ 1113/* include all of the known extension headers for IPv6 or options for IPv4. */ 1114/* */ 1115/* Since fr_pullup() expects the total length of bytes to be pulled up, it */ 1116/* is necessary to add those we can already assume to be pulled up (fin_dp */ 1117/* - fin_ip) to what is passed through. */ 1118/* ------------------------------------------------------------------------ */ 1119int 1120ipf_pr_pullup(fin, plen) 1121 fr_info_t *fin; 1122 int plen; 1123{ 1124 ipf_main_softc_t *softc = fin->fin_main_soft; 1125 1126 if (fin->fin_m != NULL) { 1127 if (fin->fin_dp != NULL) 1128 plen += (char *)fin->fin_dp - 1129 ((char *)fin->fin_ip + fin->fin_hlen); 1130 plen += fin->fin_hlen; 1131 if (M_LEN(fin->fin_m) < plen + fin->fin_ipoff) { 1132#if defined(_KERNEL) 1133 if (ipf_pullup(fin->fin_m, fin, plen) == NULL) { 1134 DT(ipf_pullup_fail); 1135 LBUMP(ipf_stats[fin->fin_out].fr_pull[1]); 1136 return -1; 1137 } 1138 LBUMP(ipf_stats[fin->fin_out].fr_pull[0]); 1139#else 1140 LBUMP(ipf_stats[fin->fin_out].fr_pull[1]); 1141 /* 1142 * Fake ipf_pullup failing 1143 */ 1144 fin->fin_reason = FRB_PULLUP; 1145 *fin->fin_mp = NULL; 1146 fin->fin_m = NULL; 1147 fin->fin_ip = NULL; 1148 return -1; 1149#endif 1150 } 1151 } 1152 return 0; 1153} 1154 1155 1156/* ------------------------------------------------------------------------ */ 1157/* Function: ipf_pr_short */ 1158/* Returns: void */ 1159/* Parameters: fin(I) - pointer to packet information */ 1160/* xmin(I) - minimum header size */ 1161/* */ 1162/* Check if a packet is "short" as defined by xmin. The rule we are */ 1163/* applying here is that the packet must not be fragmented within the layer */ 1164/* 4 header. That is, it must not be a fragment that has its offset set to */ 1165/* start within the layer 4 header (hdrmin) or if it is at offset 0, the */ 1166/* entire layer 4 header must be present (min). */ 1167/* ------------------------------------------------------------------------ */ 1168static INLINE void 1169ipf_pr_short(fin, xmin) 1170 fr_info_t *fin; 1171 int xmin; 1172{ 1173 1174 if (fin->fin_off == 0) { 1175 if (fin->fin_dlen < xmin) 1176 fin->fin_flx |= FI_SHORT; 1177 } else if (fin->fin_off < xmin) { 1178 fin->fin_flx |= FI_SHORT; 1179 } 1180} 1181 1182 1183/* ------------------------------------------------------------------------ */ 1184/* Function: ipf_pr_icmp */ 1185/* Returns: void */ 1186/* Parameters: fin(I) - pointer to packet information */ 1187/* */ 1188/* IPv4 Only */ 1189/* Do a sanity check on the packet for ICMP (v4). In nearly all cases, */ 1190/* except extrememly bad packets, both type and code will be present. */ 1191/* The expected minimum size of an ICMP packet is very much dependent on */ 1192/* the type of it. */ 1193/* */ 1194/* XXX - other ICMP sanity checks? */ 1195/* ------------------------------------------------------------------------ */ 1196static INLINE void 1197ipf_pr_icmp(fin) 1198 fr_info_t *fin; 1199{ 1200 ipf_main_softc_t *softc = fin->fin_main_soft; 1201 int minicmpsz = sizeof(struct icmp); 1202 icmphdr_t *icmp; 1203 ip_t *oip; 1204 1205 ipf_pr_short(fin, ICMPERR_ICMPHLEN); 1206 1207 if (fin->fin_off != 0) { 1208 LBUMPD(ipf_stats[fin->fin_out], fr_v4_icmp_frag); 1209 return; 1210 } 1211 1212 if (ipf_pr_pullup(fin, ICMPERR_ICMPHLEN) == -1) { 1213 LBUMPD(ipf_stats[fin->fin_out], fr_v4_icmp_pullup); 1214 return; 1215 } 1216 1217 icmp = fin->fin_dp; 1218 1219 fin->fin_data[0] = *(u_short *)icmp; 1220 fin->fin_data[1] = icmp->icmp_id; 1221 1222 switch (icmp->icmp_type) 1223 { 1224 case ICMP_ECHOREPLY : 1225 case ICMP_ECHO : 1226 /* Router discovery messaes - RFC 1256 */ 1227 case ICMP_ROUTERADVERT : 1228 case ICMP_ROUTERSOLICIT : 1229 fin->fin_flx |= FI_ICMPQUERY; 1230 minicmpsz = ICMP_MINLEN; 1231 break; 1232 /* 1233 * type(1) + code(1) + cksum(2) + id(2) seq(2) + 1234 * 3 * timestamp(3 * 4) 1235 */ 1236 case ICMP_TSTAMP : 1237 case ICMP_TSTAMPREPLY : 1238 fin->fin_flx |= FI_ICMPQUERY; 1239 minicmpsz = 20; 1240 break; 1241 /* 1242 * type(1) + code(1) + cksum(2) + id(2) seq(2) + 1243 * mask(4) 1244 */ 1245 case ICMP_IREQ : 1246 case ICMP_IREQREPLY : 1247 case ICMP_MASKREQ : 1248 case ICMP_MASKREPLY : 1249 fin->fin_flx |= FI_ICMPQUERY; 1250 minicmpsz = 12; 1251 break; 1252 /* 1253 * type(1) + code(1) + cksum(2) + id(2) seq(2) + ip(20+) 1254 */ 1255 case ICMP_UNREACH : 1256#ifdef icmp_nextmtu 1257 if (icmp->icmp_code == ICMP_UNREACH_NEEDFRAG) { 1258 if (icmp->icmp_nextmtu < softc->ipf_icmpminfragmtu) { 1259 fin->fin_flx |= FI_BAD; 1260 DT3(ipf_fi_bad_icmp_nextmtu, fr_info_t *, fin, u_int, icmp->icmp_nextmtu, u_int, softc->ipf_icmpminfragmtu); 1261 } 1262 } 1263#endif 1264 /* FALLTHROUGH */ 1265 case ICMP_SOURCEQUENCH : 1266 case ICMP_REDIRECT : 1267 case ICMP_TIMXCEED : 1268 case ICMP_PARAMPROB : 1269 fin->fin_flx |= FI_ICMPERR; 1270 if (ipf_coalesce(fin) != 1) { 1271 LBUMPD(ipf_stats[fin->fin_out], fr_icmp_coalesce); 1272 return; 1273 } 1274 1275 /* 1276 * ICMP error packets should not be generated for IP 1277 * packets that are a fragment that isn't the first 1278 * fragment. 1279 */ 1280 oip = (ip_t *)((char *)fin->fin_dp + ICMPERR_ICMPHLEN); 1281 if ((ntohs(oip->ip_off) & IP_OFFMASK) != 0) { 1282 fin->fin_flx |= FI_BAD; 1283 DT2(ipf_fi_bad_icmp_err, fr_info_t, fin, u_int, (ntohs(oip->ip_off) & IP_OFFMASK)); 1284 } 1285 1286 /* 1287 * If the destination of this packet doesn't match the 1288 * source of the original packet then this packet is 1289 * not correct. 1290 */ 1291 if (oip->ip_src.s_addr != fin->fin_daddr) { 1292 fin->fin_flx |= FI_BAD; 1293 DT1(ipf_fi_bad_src_ne_dst, fr_info_t *, fin); 1294 } 1295 break; 1296 default : 1297 break; 1298 } 1299 1300 ipf_pr_short(fin, minicmpsz); 1301 1302 ipf_checkv4sum(fin); 1303} 1304 1305 1306/* ------------------------------------------------------------------------ */ 1307/* Function: ipf_pr_tcpcommon */ 1308/* Returns: int - 0 = header ok, 1 = bad packet, -1 = buffer error */ 1309/* Parameters: fin(I) - pointer to packet information */ 1310/* */ 1311/* TCP header sanity checking. Look for bad combinations of TCP flags, */ 1312/* and make some checks with how they interact with other fields. */ 1313/* If compiled with IPFILTER_CKSUM, check to see if the TCP checksum is */ 1314/* valid and mark the packet as bad if not. */ 1315/* ------------------------------------------------------------------------ */ 1316static INLINE int 1317ipf_pr_tcpcommon(fin) 1318 fr_info_t *fin; 1319{ 1320 ipf_main_softc_t *softc = fin->fin_main_soft; 1321 int flags, tlen; 1322 tcphdr_t *tcp; 1323 1324 fin->fin_flx |= FI_TCPUDP; 1325 if (fin->fin_off != 0) { 1326 LBUMPD(ipf_stats[fin->fin_out], fr_tcp_frag); 1327 return 0; 1328 } 1329 1330 if (ipf_pr_pullup(fin, sizeof(*tcp)) == -1) { 1331 LBUMPD(ipf_stats[fin->fin_out], fr_tcp_pullup); 1332 return -1; 1333 } 1334 1335 tcp = fin->fin_dp; 1336 if (fin->fin_dlen > 3) { 1337 fin->fin_sport = ntohs(tcp->th_sport); 1338 fin->fin_dport = ntohs(tcp->th_dport); 1339 } 1340 1341 if ((fin->fin_flx & FI_SHORT) != 0) { 1342 LBUMPD(ipf_stats[fin->fin_out], fr_tcp_short); 1343 return 1; 1344 } 1345 1346 /* 1347 * Use of the TCP data offset *must* result in a value that is at 1348 * least the same size as the TCP header. 1349 */ 1350 tlen = TCP_OFF(tcp) << 2; 1351 if (tlen < sizeof(tcphdr_t)) { 1352 LBUMPD(ipf_stats[fin->fin_out], fr_tcp_small); 1353 fin->fin_flx |= FI_BAD; 1354 DT3(ipf_fi_bad_tlen, fr_info_t, fin, u_int, tlen, u_int, sizeof(tcphdr_t)); 1355 return 1; 1356 } 1357 1358 flags = tcp->th_flags; 1359 fin->fin_tcpf = tcp->th_flags; 1360 1361 /* 1362 * If the urgent flag is set, then the urgent pointer must 1363 * also be set and vice versa. Good TCP packets do not have 1364 * just one of these set. 1365 */ 1366 if ((flags & TH_URG) != 0 && (tcp->th_urp == 0)) { 1367 fin->fin_flx |= FI_BAD; 1368 DT3(ipf_fi_bad_th_urg, fr_info_t*, fin, u_int, (flags & TH_URG), u_int, tcp->th_urp); 1369#if 0 1370 } else if ((flags & TH_URG) == 0 && (tcp->th_urp != 0)) { 1371 /* 1372 * Ignore this case (#if 0) as it shows up in "real" 1373 * traffic with bogus values in the urgent pointer field. 1374 */ 1375 fin->fin_flx |= FI_BAD; 1376 DT3(ipf_fi_bad_th_urg0, fr_info_t *, fin, u_int, (flags & TH_URG), u_int, tcp->th_urp); 1377#endif 1378 } else if (((flags & (TH_SYN|TH_FIN)) != 0) && 1379 ((flags & (TH_RST|TH_ACK)) == TH_RST)) { 1380 /* TH_FIN|TH_RST|TH_ACK seems to appear "naturally" */ 1381 fin->fin_flx |= FI_BAD; 1382 DT1(ipf_fi_bad_th_fin_rst_ack, fr_info_t, fin); 1383#if 1 1384 } else if (((flags & TH_SYN) != 0) && 1385 ((flags & (TH_URG|TH_PUSH)) != 0)) { 1386 /* 1387 * SYN with URG and PUSH set is not for normal TCP but it is 1388 * possible(?) with T/TCP...but who uses T/TCP? 1389 */ 1390 fin->fin_flx |= FI_BAD; 1391 DT1(ipf_fi_bad_th_syn_urg_psh, fr_info_t *, fin); 1392#endif 1393 } else if (!(flags & TH_ACK)) { 1394 /* 1395 * If the ack bit isn't set, then either the SYN or 1396 * RST bit must be set. If the SYN bit is set, then 1397 * we expect the ACK field to be 0. If the ACK is 1398 * not set and if URG, PSH or FIN are set, consdier 1399 * that to indicate a bad TCP packet. 1400 */ 1401 if ((flags == TH_SYN) && (tcp->th_ack != 0)) { 1402 /* 1403 * Cisco PIX sets the ACK field to a random value. 1404 * In light of this, do not set FI_BAD until a patch 1405 * is available from Cisco to ensure that 1406 * interoperability between existing systems is 1407 * achieved. 1408 */ 1409 /*fin->fin_flx |= FI_BAD*/; 1410 /*DT1(ipf_fi_bad_th_syn_ack, fr_info_t *, fin);*/ 1411 } else if (!(flags & (TH_RST|TH_SYN))) { 1412 fin->fin_flx |= FI_BAD; 1413 DT1(ipf_fi_bad_th_rst_syn, fr_info_t *, fin); 1414 } else if ((flags & (TH_URG|TH_PUSH|TH_FIN)) != 0) { 1415 fin->fin_flx |= FI_BAD; 1416 DT1(ipf_fi_bad_th_urg_push_fin, fr_info_t *, fin); 1417 } 1418 } 1419 if (fin->fin_flx & FI_BAD) { 1420 LBUMPD(ipf_stats[fin->fin_out], fr_tcp_bad_flags); 1421 return 1; 1422 } 1423 1424 /* 1425 * At this point, it's not exactly clear what is to be gained by 1426 * marking up which TCP options are and are not present. The one we 1427 * are most interested in is the TCP window scale. This is only in 1428 * a SYN packet [RFC1323] so we don't need this here...? 1429 * Now if we were to analyse the header for passive fingerprinting, 1430 * then that might add some weight to adding this... 1431 */ 1432 if (tlen == sizeof(tcphdr_t)) { 1433 return 0; 1434 } 1435 1436 if (ipf_pr_pullup(fin, tlen) == -1) { 1437 LBUMPD(ipf_stats[fin->fin_out], fr_tcp_pullup); 1438 return -1; 1439 } 1440 1441#if 0 1442 tcp = fin->fin_dp; 1443 ip = fin->fin_ip; 1444 s = (u_char *)(tcp + 1); 1445 off = IP_HL(ip) << 2; 1446# ifdef _KERNEL 1447 if (fin->fin_mp != NULL) { 1448 mb_t *m = *fin->fin_mp; 1449 1450 if (off + tlen > M_LEN(m)) 1451 return; 1452 } 1453# endif 1454 for (tlen -= (int)sizeof(*tcp); tlen > 0; ) { 1455 opt = *s; 1456 if (opt == '\0') 1457 break; 1458 else if (opt == TCPOPT_NOP) 1459 ol = 1; 1460 else { 1461 if (tlen < 2) 1462 break; 1463 ol = (int)*(s + 1); 1464 if (ol < 2 || ol > tlen) 1465 break; 1466 } 1467 1468 for (i = 9, mv = 4; mv >= 0; ) { 1469 op = ipopts + i; 1470 if (opt == (u_char)op->ol_val) { 1471 optmsk |= op->ol_bit; 1472 break; 1473 } 1474 } 1475 tlen -= ol; 1476 s += ol; 1477 } 1478#endif /* 0 */ 1479 1480 return 0; 1481} 1482 1483 1484 1485/* ------------------------------------------------------------------------ */ 1486/* Function: ipf_pr_udpcommon */ 1487/* Returns: int - 0 = header ok, 1 = bad packet */ 1488/* Parameters: fin(I) - pointer to packet information */ 1489/* */ 1490/* Extract the UDP source and destination ports, if present. If compiled */ 1491/* with IPFILTER_CKSUM, check to see if the UDP checksum is valid. */ 1492/* ------------------------------------------------------------------------ */ 1493static INLINE int 1494ipf_pr_udpcommon(fin) 1495 fr_info_t *fin; 1496{ 1497 udphdr_t *udp; 1498 1499 fin->fin_flx |= FI_TCPUDP; 1500 1501 if (!fin->fin_off && (fin->fin_dlen > 3)) { 1502 if (ipf_pr_pullup(fin, sizeof(*udp)) == -1) { 1503 ipf_main_softc_t *softc = fin->fin_main_soft; 1504 1505 fin->fin_flx |= FI_SHORT; 1506 LBUMPD(ipf_stats[fin->fin_out], fr_udp_pullup); 1507 return 1; 1508 } 1509 1510 udp = fin->fin_dp; 1511 1512 fin->fin_sport = ntohs(udp->uh_sport); 1513 fin->fin_dport = ntohs(udp->uh_dport); 1514 } 1515 1516 return 0; 1517} 1518 1519 1520/* ------------------------------------------------------------------------ */ 1521/* Function: ipf_pr_tcp */ 1522/* Returns: void */ 1523/* Parameters: fin(I) - pointer to packet information */ 1524/* */ 1525/* IPv4 Only */ 1526/* Analyse the packet for IPv4/TCP properties. */ 1527/* ------------------------------------------------------------------------ */ 1528static INLINE void 1529ipf_pr_tcp(fin) 1530 fr_info_t *fin; 1531{ 1532 1533 ipf_pr_short(fin, sizeof(tcphdr_t)); 1534 1535 if (ipf_pr_tcpcommon(fin) == 0) 1536 ipf_checkv4sum(fin); 1537} 1538 1539 1540/* ------------------------------------------------------------------------ */ 1541/* Function: ipf_pr_udp */ 1542/* Returns: void */ 1543/* Parameters: fin(I) - pointer to packet information */ 1544/* */ 1545/* IPv4 Only */ 1546/* Analyse the packet for IPv4/UDP properties. */ 1547/* ------------------------------------------------------------------------ */ 1548static INLINE void 1549ipf_pr_udp(fin) 1550 fr_info_t *fin; 1551{ 1552 1553 ipf_pr_short(fin, sizeof(udphdr_t)); 1554 1555 if (ipf_pr_udpcommon(fin) == 0) 1556 ipf_checkv4sum(fin); 1557} 1558 1559 1560/* ------------------------------------------------------------------------ */ 1561/* Function: ipf_pr_esp */ 1562/* Returns: void */ 1563/* Parameters: fin(I) - pointer to packet information */ 1564/* */ 1565/* Analyse the packet for ESP properties. */ 1566/* The minimum length is taken to be the SPI (32bits) plus a tail (32bits) */ 1567/* even though the newer ESP packets must also have a sequence number that */ 1568/* is 32bits as well, it is not possible(?) to determine the version from a */ 1569/* simple packet header. */ 1570/* ------------------------------------------------------------------------ */ 1571static INLINE void 1572ipf_pr_esp(fin) 1573 fr_info_t *fin; 1574{ 1575 1576 if (fin->fin_off == 0) { 1577 ipf_pr_short(fin, 8); 1578 if (ipf_pr_pullup(fin, 8) == -1) { 1579 ipf_main_softc_t *softc = fin->fin_main_soft; 1580 1581 LBUMPD(ipf_stats[fin->fin_out], fr_v4_esp_pullup); 1582 } 1583 } 1584} 1585 1586 1587/* ------------------------------------------------------------------------ */ 1588/* Function: ipf_pr_ah */ 1589/* Returns: int - value of the next header or IPPROTO_NONE if error */ 1590/* Parameters: fin(I) - pointer to packet information */ 1591/* */ 1592/* Analyse the packet for AH properties. */ 1593/* The minimum length is taken to be the combination of all fields in the */ 1594/* header being present and no authentication data (null algorithm used.) */ 1595/* ------------------------------------------------------------------------ */ 1596static INLINE int 1597ipf_pr_ah(fin) 1598 fr_info_t *fin; 1599{ 1600 ipf_main_softc_t *softc = fin->fin_main_soft; 1601 authhdr_t *ah; 1602 int len; 1603 1604 fin->fin_flx |= FI_AH; 1605 ipf_pr_short(fin, sizeof(*ah)); 1606 1607 if (((fin->fin_flx & FI_SHORT) != 0) || (fin->fin_off != 0)) { 1608 LBUMPD(ipf_stats[fin->fin_out], fr_v4_ah_bad); 1609 return IPPROTO_NONE; 1610 } 1611 1612 if (ipf_pr_pullup(fin, sizeof(*ah)) == -1) { 1613 DT(fr_v4_ah_pullup_1); 1614 LBUMP(ipf_stats[fin->fin_out].fr_v4_ah_pullup); 1615 return IPPROTO_NONE; 1616 } 1617 1618 ah = (authhdr_t *)fin->fin_dp; 1619 1620 len = (ah->ah_plen + 2) << 2; 1621 ipf_pr_short(fin, len); 1622 if (ipf_pr_pullup(fin, len) == -1) { 1623 DT(fr_v4_ah_pullup_2); 1624 LBUMP(ipf_stats[fin->fin_out].fr_v4_ah_pullup); 1625 return IPPROTO_NONE; 1626 } 1627 1628 /* 1629 * Adjust fin_dp and fin_dlen for skipping over the authentication 1630 * header. 1631 */ 1632 fin->fin_dp = (char *)fin->fin_dp + len; 1633 fin->fin_dlen -= len; 1634 return ah->ah_next; 1635} 1636 1637 1638/* ------------------------------------------------------------------------ */ 1639/* Function: ipf_pr_gre */ 1640/* Returns: void */ 1641/* Parameters: fin(I) - pointer to packet information */ 1642/* */ 1643/* Analyse the packet for GRE properties. */ 1644/* ------------------------------------------------------------------------ */ 1645static INLINE void 1646ipf_pr_gre(fin) 1647 fr_info_t *fin; 1648{ 1649 ipf_main_softc_t *softc = fin->fin_main_soft; 1650 grehdr_t *gre; 1651 1652 ipf_pr_short(fin, sizeof(grehdr_t)); 1653 1654 if (fin->fin_off != 0) { 1655 LBUMPD(ipf_stats[fin->fin_out], fr_v4_gre_frag); 1656 return; 1657 } 1658 1659 if (ipf_pr_pullup(fin, sizeof(grehdr_t)) == -1) { 1660 LBUMPD(ipf_stats[fin->fin_out], fr_v4_gre_pullup); 1661 return; 1662 } 1663 1664 gre = fin->fin_dp; 1665 if (GRE_REV(gre->gr_flags) == 1) 1666 fin->fin_data[0] = gre->gr_call; 1667} 1668 1669 1670/* ------------------------------------------------------------------------ */ 1671/* Function: ipf_pr_ipv4hdr */ 1672/* Returns: void */ 1673/* Parameters: fin(I) - pointer to packet information */ 1674/* */ 1675/* IPv4 Only */ 1676/* Analyze the IPv4 header and set fields in the fr_info_t structure. */ 1677/* Check all options present and flag their presence if any exist. */ 1678/* ------------------------------------------------------------------------ */ 1679static INLINE void 1680ipf_pr_ipv4hdr(fin) 1681 fr_info_t *fin; 1682{ 1683 u_short optmsk = 0, secmsk = 0, auth = 0; 1684 int hlen, ol, mv, p, i; 1685 const struct optlist *op; 1686 u_char *s, opt; 1687 u_short off; 1688 fr_ip_t *fi; 1689 ip_t *ip; 1690 1691 fi = &fin->fin_fi; 1692 hlen = fin->fin_hlen; 1693 1694 ip = fin->fin_ip; 1695 p = ip->ip_p; 1696 fi->fi_p = p; 1697 fin->fin_crc = p; 1698 fi->fi_tos = ip->ip_tos; 1699 fin->fin_id = ip->ip_id; 1700 off = ntohs(ip->ip_off); 1701 1702 /* Get both TTL and protocol */ 1703 fi->fi_p = ip->ip_p; 1704 fi->fi_ttl = ip->ip_ttl; 1705 1706 /* Zero out bits not used in IPv6 address */ 1707 fi->fi_src.i6[1] = 0; 1708 fi->fi_src.i6[2] = 0; 1709 fi->fi_src.i6[3] = 0; 1710 fi->fi_dst.i6[1] = 0; 1711 fi->fi_dst.i6[2] = 0; 1712 fi->fi_dst.i6[3] = 0; 1713 1714 fi->fi_saddr = ip->ip_src.s_addr; 1715 fin->fin_crc += fi->fi_saddr; 1716 fi->fi_daddr = ip->ip_dst.s_addr; 1717 fin->fin_crc += fi->fi_daddr; 1718 if (IN_CLASSD(ntohl(fi->fi_daddr))) 1719 fin->fin_flx |= FI_MULTICAST|FI_MBCAST; 1720 1721 /* 1722 * set packet attribute flags based on the offset and 1723 * calculate the byte offset that it represents. 1724 */ 1725 off &= IP_MF|IP_OFFMASK; 1726 if (off != 0) { 1727 int morefrag = off & IP_MF; 1728 1729 fi->fi_flx |= FI_FRAG; 1730 off &= IP_OFFMASK; 1731 if (off != 0) { 1732 fin->fin_flx |= FI_FRAGBODY; 1733 off <<= 3; 1734 if ((off + fin->fin_dlen > 65535) || 1735 (fin->fin_dlen == 0) || 1736 ((morefrag != 0) && ((fin->fin_dlen & 7) != 0))) { 1737 /* 1738 * The length of the packet, starting at its 1739 * offset cannot exceed 65535 (0xffff) as the 1740 * length of an IP packet is only 16 bits. 1741 * 1742 * Any fragment that isn't the last fragment 1743 * must have a length greater than 0 and it 1744 * must be an even multiple of 8. 1745 */ 1746 fi->fi_flx |= FI_BAD; 1747 DT1(ipf_fi_bad_fragbody_gt_65535, fr_info_t *, fin); 1748 } 1749 } 1750 } 1751 fin->fin_off = off; 1752 1753 /* 1754 * Call per-protocol setup and checking 1755 */ 1756 if (p == IPPROTO_AH) { 1757 /* 1758 * Treat AH differently because we expect there to be another 1759 * layer 4 header after it. 1760 */ 1761 p = ipf_pr_ah(fin); 1762 } 1763 1764 switch (p) 1765 { 1766 case IPPROTO_UDP : 1767 ipf_pr_udp(fin); 1768 break; 1769 case IPPROTO_TCP : 1770 ipf_pr_tcp(fin); 1771 break; 1772 case IPPROTO_ICMP : 1773 ipf_pr_icmp(fin); 1774 break; 1775 case IPPROTO_ESP : 1776 ipf_pr_esp(fin); 1777 break; 1778 case IPPROTO_GRE : 1779 ipf_pr_gre(fin); 1780 break; 1781 } 1782 1783 ip = fin->fin_ip; 1784 if (ip == NULL) 1785 return; 1786 1787 /* 1788 * If it is a standard IP header (no options), set the flag fields 1789 * which relate to options to 0. 1790 */ 1791 if (hlen == sizeof(*ip)) { 1792 fi->fi_optmsk = 0; 1793 fi->fi_secmsk = 0; 1794 fi->fi_auth = 0; 1795 return; 1796 } 1797 1798 /* 1799 * So the IP header has some IP options attached. Walk the entire 1800 * list of options present with this packet and set flags to indicate 1801 * which ones are here and which ones are not. For the somewhat out 1802 * of date and obscure security classification options, set a flag to 1803 * represent which classification is present. 1804 */ 1805 fi->fi_flx |= FI_OPTIONS; 1806 1807 for (s = (u_char *)(ip + 1), hlen -= (int)sizeof(*ip); hlen > 0; ) { 1808 opt = *s; 1809 if (opt == '\0') 1810 break; 1811 else if (opt == IPOPT_NOP) 1812 ol = 1; 1813 else { 1814 if (hlen < 2) 1815 break; 1816 ol = (int)*(s + 1); 1817 if (ol < 2 || ol > hlen) 1818 break; 1819 } 1820 for (i = 9, mv = 4; mv >= 0; ) { 1821 op = ipopts + i; 1822 1823 if ((opt == (u_char)op->ol_val) && (ol > 4)) { 1824 u_32_t doi; 1825 1826 switch (opt) 1827 { 1828 case IPOPT_SECURITY : 1829 if (optmsk & op->ol_bit) { 1830 fin->fin_flx |= FI_BAD; 1831 DT2(ipf_fi_bad_ipopt_security, fr_info_t *, fin, u_short, (optmsk & op->ol_bit)); 1832 } else { 1833 doi = ipf_checkripso(s); 1834 secmsk = doi >> 16; 1835 auth = doi & 0xffff; 1836 } 1837 break; 1838 1839 case IPOPT_CIPSO : 1840 1841 if (optmsk & op->ol_bit) { 1842 fin->fin_flx |= FI_BAD; 1843 DT2(ipf_fi_bad_ipopt_cipso, fr_info_t *, fin, u_short, (optmsk & op->ol_bit)); 1844 } else { 1845 doi = ipf_checkcipso(fin, 1846 s, ol); 1847 secmsk = doi >> 16; 1848 auth = doi & 0xffff; 1849 } 1850 break; 1851 } 1852 optmsk |= op->ol_bit; 1853 } 1854 1855 if (opt < op->ol_val) 1856 i -= mv; 1857 else 1858 i += mv; 1859 mv--; 1860 } 1861 hlen -= ol; 1862 s += ol; 1863 } 1864 1865 /* 1866 * 1867 */ 1868 if (auth && !(auth & 0x0100)) 1869 auth &= 0xff00; 1870 fi->fi_optmsk = optmsk; 1871 fi->fi_secmsk = secmsk; 1872 fi->fi_auth = auth; 1873} 1874 1875 1876/* ------------------------------------------------------------------------ */ 1877/* Function: ipf_checkripso */ 1878/* Returns: void */ 1879/* Parameters: s(I) - pointer to start of RIPSO option */ 1880/* */ 1881/* ------------------------------------------------------------------------ */ 1882static u_32_t 1883ipf_checkripso(s) 1884 u_char *s; 1885{ 1886 const struct optlist *sp; 1887 u_short secmsk = 0, auth = 0; 1888 u_char sec; 1889 int j, m; 1890 1891 sec = *(s + 2); /* classification */ 1892 for (j = 3, m = 2; m >= 0; ) { 1893 sp = secopt + j; 1894 if (sec == sp->ol_val) { 1895 secmsk |= sp->ol_bit; 1896 auth = *(s + 3); 1897 auth *= 256; 1898 auth += *(s + 4); 1899 break; 1900 } 1901 if (sec < sp->ol_val) 1902 j -= m; 1903 else 1904 j += m; 1905 m--; 1906 } 1907 1908 return (secmsk << 16) | auth; 1909} 1910 1911 1912/* ------------------------------------------------------------------------ */ 1913/* Function: ipf_checkcipso */ 1914/* Returns: u_32_t - 0 = failure, else the doi from the header */ 1915/* Parameters: fin(IO) - pointer to packet information */ 1916/* s(I) - pointer to start of CIPSO option */ 1917/* ol(I) - length of CIPSO option field */ 1918/* */ 1919/* This function returns the domain of integrity (DOI) field from the CIPSO */ 1920/* header and returns that whilst also storing the highest sensitivity */ 1921/* value found in the fr_info_t structure. */ 1922/* */ 1923/* No attempt is made to extract the category bitmaps as these are defined */ 1924/* by the user (rather than the protocol) and can be rather numerous on the */ 1925/* end nodes. */ 1926/* ------------------------------------------------------------------------ */ 1927static u_32_t 1928ipf_checkcipso(fin, s, ol) 1929 fr_info_t *fin; 1930 u_char *s; 1931 int ol; 1932{ 1933 ipf_main_softc_t *softc = fin->fin_main_soft; 1934 fr_ip_t *fi; 1935 u_32_t doi; 1936 u_char *t, tag, tlen, sensitivity; 1937 int len; 1938 1939 if (ol < 6 || ol > 40) { 1940 LBUMPD(ipf_stats[fin->fin_out], fr_v4_cipso_bad); 1941 fin->fin_flx |= FI_BAD; 1942 DT2(ipf_fi_bad_checkcipso_ol, fr_info_t *, fin, u_int, ol); 1943 return 0; 1944 } 1945 1946 fi = &fin->fin_fi; 1947 fi->fi_sensitivity = 0; 1948 /* 1949 * The DOI field MUST be there. 1950 */ 1951 bcopy(s + 2, &doi, sizeof(doi)); 1952 1953 t = (u_char *)s + 6; 1954 for (len = ol - 6; len >= 2; len -= tlen, t+= tlen) { 1955 tag = *t; 1956 tlen = *(t + 1); 1957 if (tlen > len || tlen < 4 || tlen > 34) { 1958 LBUMPD(ipf_stats[fin->fin_out], fr_v4_cipso_tlen); 1959 fin->fin_flx |= FI_BAD; 1960 DT2(ipf_fi_bad_checkcipso_tlen, fr_info_t *, fin, u_int, tlen); 1961 return 0; 1962 } 1963 1964 sensitivity = 0; 1965 /* 1966 * Tag numbers 0, 1, 2, 5 are laid out in the CIPSO Internet 1967 * draft (16 July 1992) that has expired. 1968 */ 1969 if (tag == 0) { 1970 fin->fin_flx |= FI_BAD; 1971 DT2(ipf_fi_bad_checkcipso_tag, fr_info_t *, fin, u_int, tag); 1972 continue; 1973 } else if (tag == 1) { 1974 if (*(t + 2) != 0) { 1975 fin->fin_flx |= FI_BAD; 1976 DT2(ipf_fi_bad_checkcipso_tag1_t2, fr_info_t *, fin, u_int, (*t + 2)); 1977 continue; 1978 } 1979 sensitivity = *(t + 3); 1980 /* Category bitmap for categories 0-239 */ 1981 1982 } else if (tag == 4) { 1983 if (*(t + 2) != 0) { 1984 fin->fin_flx |= FI_BAD; 1985 DT2(ipf_fi_bad_checkcipso_tag4_t2, fr_info_t *, fin, u_int, (*t + 2)); 1986 continue; 1987 } 1988 sensitivity = *(t + 3); 1989 /* Enumerated categories, 16bits each, upto 15 */ 1990 1991 } else if (tag == 5) { 1992 if (*(t + 2) != 0) { 1993 fin->fin_flx |= FI_BAD; 1994 DT2(ipf_fi_bad_checkcipso_tag5_t2, fr_info_t *, fin, u_int, (*t + 2)); 1995 continue; 1996 } 1997 sensitivity = *(t + 3); 1998 /* Range of categories (2*16bits), up to 7 pairs */ 1999 2000 } else if (tag > 127) { 2001 /* Custom defined DOI */ 2002 ; 2003 } else { 2004 fin->fin_flx |= FI_BAD; 2005 DT2(ipf_fi_bad_checkcipso_tag127, fr_info_t *, fin, u_int, tag); 2006 continue; 2007 } 2008 2009 if (sensitivity > fi->fi_sensitivity) 2010 fi->fi_sensitivity = sensitivity; 2011 } 2012 2013 return doi; 2014} 2015 2016 2017/* ------------------------------------------------------------------------ */ 2018/* Function: ipf_makefrip */ 2019/* Returns: int - 0 == packet ok, -1 == packet freed */ 2020/* Parameters: hlen(I) - length of IP packet header */ 2021/* ip(I) - pointer to the IP header */ 2022/* fin(IO) - pointer to packet information */ 2023/* */ 2024/* Compact the IP header into a structure which contains just the info. */ 2025/* which is useful for comparing IP headers with and store this information */ 2026/* in the fr_info_t structure pointer to by fin. At present, it is assumed */ 2027/* this function will be called with either an IPv4 or IPv6 packet. */ 2028/* ------------------------------------------------------------------------ */ 2029int 2030ipf_makefrip(hlen, ip, fin) 2031 int hlen; 2032 ip_t *ip; 2033 fr_info_t *fin; 2034{ 2035 ipf_main_softc_t *softc = fin->fin_main_soft; 2036 int v; 2037 2038 fin->fin_depth = 0; 2039 fin->fin_hlen = (u_short)hlen; 2040 fin->fin_ip = ip; 2041 fin->fin_rule = 0xffffffff; 2042 fin->fin_group[0] = -1; 2043 fin->fin_group[1] = '\0'; 2044 fin->fin_dp = (char *)ip + hlen; 2045 2046 v = fin->fin_v; 2047 if (v == 4) { 2048 fin->fin_plen = ntohs(ip->ip_len); 2049 fin->fin_dlen = fin->fin_plen - hlen; 2050 ipf_pr_ipv4hdr(fin); 2051#ifdef USE_INET6 2052 } else if (v == 6) { 2053 fin->fin_plen = ntohs(((ip6_t *)ip)->ip6_plen); 2054 fin->fin_dlen = fin->fin_plen; 2055 fin->fin_plen += hlen; 2056 2057 ipf_pr_ipv6hdr(fin); 2058#endif 2059 } 2060 if (fin->fin_ip == NULL) { 2061 LBUMP(ipf_stats[fin->fin_out].fr_ip_freed); 2062 return -1; 2063 } 2064 return 0; 2065} 2066 2067 2068/* ------------------------------------------------------------------------ */ 2069/* Function: ipf_portcheck */ 2070/* Returns: int - 1 == port matched, 0 == port match failed */ 2071/* Parameters: frp(I) - pointer to port check `expression' */ 2072/* pop(I) - port number to evaluate */ 2073/* */ 2074/* Perform a comparison of a port number against some other(s), using a */ 2075/* structure with compare information stored in it. */ 2076/* ------------------------------------------------------------------------ */ 2077static INLINE int 2078ipf_portcheck(frp, pop) 2079 frpcmp_t *frp; 2080 u_32_t pop; 2081{ 2082 int err = 1; 2083 u_32_t po; 2084 2085 po = frp->frp_port; 2086 2087 /* 2088 * Do opposite test to that required and continue if that succeeds. 2089 */ 2090 switch (frp->frp_cmp) 2091 { 2092 case FR_EQUAL : 2093 if (pop != po) /* EQUAL */ 2094 err = 0; 2095 break; 2096 case FR_NEQUAL : 2097 if (pop == po) /* NOTEQUAL */ 2098 err = 0; 2099 break; 2100 case FR_LESST : 2101 if (pop >= po) /* LESSTHAN */ 2102 err = 0; 2103 break; 2104 case FR_GREATERT : 2105 if (pop <= po) /* GREATERTHAN */ 2106 err = 0; 2107 break; 2108 case FR_LESSTE : 2109 if (pop > po) /* LT or EQ */ 2110 err = 0; 2111 break; 2112 case FR_GREATERTE : 2113 if (pop < po) /* GT or EQ */ 2114 err = 0; 2115 break; 2116 case FR_OUTRANGE : 2117 if (pop >= po && pop <= frp->frp_top) /* Out of range */ 2118 err = 0; 2119 break; 2120 case FR_INRANGE : 2121 if (pop <= po || pop >= frp->frp_top) /* In range */ 2122 err = 0; 2123 break; 2124 case FR_INCRANGE : 2125 if (pop < po || pop > frp->frp_top) /* Inclusive range */ 2126 err = 0; 2127 break; 2128 default : 2129 break; 2130 } 2131 return err; 2132} 2133 2134 2135/* ------------------------------------------------------------------------ */ 2136/* Function: ipf_tcpudpchk */ 2137/* Returns: int - 1 == protocol matched, 0 == check failed */ 2138/* Parameters: fda(I) - pointer to packet information */ 2139/* ft(I) - pointer to structure with comparison data */ 2140/* */ 2141/* Compares the current pcket (assuming it is TCP/UDP) information with a */ 2142/* structure containing information that we want to match against. */ 2143/* ------------------------------------------------------------------------ */ 2144int 2145ipf_tcpudpchk(fi, ft) 2146 fr_ip_t *fi; 2147 frtuc_t *ft; 2148{ 2149 int err = 1; 2150 2151 /* 2152 * Both ports should *always* be in the first fragment. 2153 * So far, I cannot find any cases where they can not be. 2154 * 2155 * compare destination ports 2156 */ 2157 if (ft->ftu_dcmp) 2158 err = ipf_portcheck(&ft->ftu_dst, fi->fi_ports[1]); 2159 2160 /* 2161 * compare source ports 2162 */ 2163 if (err && ft->ftu_scmp) 2164 err = ipf_portcheck(&ft->ftu_src, fi->fi_ports[0]); 2165 2166 /* 2167 * If we don't have all the TCP/UDP header, then how can we 2168 * expect to do any sort of match on it ? If we were looking for 2169 * TCP flags, then NO match. If not, then match (which should 2170 * satisfy the "short" class too). 2171 */ 2172 if (err && (fi->fi_p == IPPROTO_TCP)) { 2173 if (fi->fi_flx & FI_SHORT) 2174 return !(ft->ftu_tcpf | ft->ftu_tcpfm); 2175 /* 2176 * Match the flags ? If not, abort this match. 2177 */ 2178 if (ft->ftu_tcpfm && 2179 ft->ftu_tcpf != (fi->fi_tcpf & ft->ftu_tcpfm)) { 2180 FR_DEBUG(("f. %#x & %#x != %#x\n", fi->fi_tcpf, 2181 ft->ftu_tcpfm, ft->ftu_tcpf)); 2182 err = 0; 2183 } 2184 } 2185 return err; 2186} 2187 2188 2189/* ------------------------------------------------------------------------ */ 2190/* Function: ipf_check_ipf */ 2191/* Returns: int - 0 == match, else no match */ 2192/* Parameters: fin(I) - pointer to packet information */ 2193/* fr(I) - pointer to filter rule */ 2194/* portcmp(I) - flag indicating whether to attempt matching on */ 2195/* TCP/UDP port data. */ 2196/* */ 2197/* Check to see if a packet matches an IPFilter rule. Checks of addresses, */ 2198/* port numbers, etc, for "standard" IPFilter rules are all orchestrated in */ 2199/* this function. */ 2200/* ------------------------------------------------------------------------ */ 2201static INLINE int 2202ipf_check_ipf(fin, fr, portcmp) 2203 fr_info_t *fin; 2204 frentry_t *fr; 2205 int portcmp; 2206{ 2207 u_32_t *ld, *lm, *lip; 2208 fripf_t *fri; 2209 fr_ip_t *fi; 2210 int i; 2211 2212 fi = &fin->fin_fi; 2213 fri = fr->fr_ipf; 2214 lip = (u_32_t *)fi; 2215 lm = (u_32_t *)&fri->fri_mip; 2216 ld = (u_32_t *)&fri->fri_ip; 2217 2218 /* 2219 * first 32 bits to check coversion: 2220 * IP version, TOS, TTL, protocol 2221 */ 2222 i = ((*lip & *lm) != *ld); 2223 FR_DEBUG(("0. %#08x & %#08x != %#08x\n", 2224 ntohl(*lip), ntohl(*lm), ntohl(*ld))); 2225 if (i) 2226 return 1; 2227 2228 /* 2229 * Next 32 bits is a constructed bitmask indicating which IP options 2230 * are present (if any) in this packet. 2231 */ 2232 lip++, lm++, ld++; 2233 i = ((*lip & *lm) != *ld); 2234 FR_DEBUG(("1. %#08x & %#08x != %#08x\n", 2235 ntohl(*lip), ntohl(*lm), ntohl(*ld))); 2236 if (i != 0) 2237 return 1; 2238 2239 lip++, lm++, ld++; 2240 /* 2241 * Unrolled loops (4 each, for 32 bits) for address checks. 2242 */ 2243 /* 2244 * Check the source address. 2245 */ 2246 if (fr->fr_satype == FRI_LOOKUP) { 2247 i = (*fr->fr_srcfunc)(fin->fin_main_soft, fr->fr_srcptr, 2248 fi->fi_v, lip, fin->fin_plen); 2249 if (i == -1) 2250 return 1; 2251 lip += 3; 2252 lm += 3; 2253 ld += 3; 2254 } else { 2255 i = ((*lip & *lm) != *ld); 2256 FR_DEBUG(("2a. %#08x & %#08x != %#08x\n", 2257 ntohl(*lip), ntohl(*lm), ntohl(*ld))); 2258 if (fi->fi_v == 6) { 2259 lip++, lm++, ld++; 2260 i |= ((*lip & *lm) != *ld); 2261 FR_DEBUG(("2b. %#08x & %#08x != %#08x\n", 2262 ntohl(*lip), ntohl(*lm), ntohl(*ld))); 2263 lip++, lm++, ld++; 2264 i |= ((*lip & *lm) != *ld); 2265 FR_DEBUG(("2c. %#08x & %#08x != %#08x\n", 2266 ntohl(*lip), ntohl(*lm), ntohl(*ld))); 2267 lip++, lm++, ld++; 2268 i |= ((*lip & *lm) != *ld); 2269 FR_DEBUG(("2d. %#08x & %#08x != %#08x\n", 2270 ntohl(*lip), ntohl(*lm), ntohl(*ld))); 2271 } else { 2272 lip += 3; 2273 lm += 3; 2274 ld += 3; 2275 } 2276 } 2277 i ^= (fr->fr_flags & FR_NOTSRCIP) >> 6; 2278 if (i != 0) 2279 return 1; 2280 2281 /* 2282 * Check the destination address. 2283 */ 2284 lip++, lm++, ld++; 2285 if (fr->fr_datype == FRI_LOOKUP) { 2286 i = (*fr->fr_dstfunc)(fin->fin_main_soft, fr->fr_dstptr, 2287 fi->fi_v, lip, fin->fin_plen); 2288 if (i == -1) 2289 return 1; 2290 lip += 3; 2291 lm += 3; 2292 ld += 3; 2293 } else { 2294 i = ((*lip & *lm) != *ld); 2295 FR_DEBUG(("3a. %#08x & %#08x != %#08x\n", 2296 ntohl(*lip), ntohl(*lm), ntohl(*ld))); 2297 if (fi->fi_v == 6) { 2298 lip++, lm++, ld++; 2299 i |= ((*lip & *lm) != *ld); 2300 FR_DEBUG(("3b. %#08x & %#08x != %#08x\n", 2301 ntohl(*lip), ntohl(*lm), ntohl(*ld))); 2302 lip++, lm++, ld++; 2303 i |= ((*lip & *lm) != *ld); 2304 FR_DEBUG(("3c. %#08x & %#08x != %#08x\n", 2305 ntohl(*lip), ntohl(*lm), ntohl(*ld))); 2306 lip++, lm++, ld++; 2307 i |= ((*lip & *lm) != *ld); 2308 FR_DEBUG(("3d. %#08x & %#08x != %#08x\n", 2309 ntohl(*lip), ntohl(*lm), ntohl(*ld))); 2310 } else { 2311 lip += 3; 2312 lm += 3; 2313 ld += 3; 2314 } 2315 } 2316 i ^= (fr->fr_flags & FR_NOTDSTIP) >> 7; 2317 if (i != 0) 2318 return 1; 2319 /* 2320 * IP addresses matched. The next 32bits contains: 2321 * mast of old IP header security & authentication bits. 2322 */ 2323 lip++, lm++, ld++; 2324 i = (*ld - (*lip & *lm)); 2325 FR_DEBUG(("4. %#08x & %#08x != %#08x\n", *lip, *lm, *ld)); 2326 2327 /* 2328 * Next we have 32 bits of packet flags. 2329 */ 2330 lip++, lm++, ld++; 2331 i |= (*ld - (*lip & *lm)); 2332 FR_DEBUG(("5. %#08x & %#08x != %#08x\n", *lip, *lm, *ld)); 2333 2334 if (i == 0) { 2335 /* 2336 * If a fragment, then only the first has what we're 2337 * looking for here... 2338 */ 2339 if (portcmp) { 2340 if (!ipf_tcpudpchk(&fin->fin_fi, &fr->fr_tuc)) 2341 i = 1; 2342 } else { 2343 if (fr->fr_dcmp || fr->fr_scmp || 2344 fr->fr_tcpf || fr->fr_tcpfm) 2345 i = 1; 2346 if (fr->fr_icmpm || fr->fr_icmp) { 2347 if (((fi->fi_p != IPPROTO_ICMP) && 2348 (fi->fi_p != IPPROTO_ICMPV6)) || 2349 fin->fin_off || (fin->fin_dlen < 2)) 2350 i = 1; 2351 else if ((fin->fin_data[0] & fr->fr_icmpm) != 2352 fr->fr_icmp) { 2353 FR_DEBUG(("i. %#x & %#x != %#x\n", 2354 fin->fin_data[0], 2355 fr->fr_icmpm, fr->fr_icmp)); 2356 i = 1; 2357 } 2358 } 2359 } 2360 } 2361 return i; 2362} 2363 2364 2365/* ------------------------------------------------------------------------ */ 2366/* Function: ipf_scanlist */ 2367/* Returns: int - result flags of scanning filter list */ 2368/* Parameters: fin(I) - pointer to packet information */ 2369/* pass(I) - default result to return for filtering */ 2370/* */ 2371/* Check the input/output list of rules for a match to the current packet. */ 2372/* If a match is found, the value of fr_flags from the rule becomes the */ 2373/* return value and fin->fin_fr points to the matched rule. */ 2374/* */ 2375/* This function may be called recusively upto 16 times (limit inbuilt.) */ 2376/* When unwinding, it should finish up with fin_depth as 0. */ 2377/* */ 2378/* Could be per interface, but this gets real nasty when you don't have, */ 2379/* or can't easily change, the kernel source code to . */ 2380/* ------------------------------------------------------------------------ */ 2381int 2382ipf_scanlist(fin, pass) 2383 fr_info_t *fin; 2384 u_32_t pass; 2385{ 2386 ipf_main_softc_t *softc = fin->fin_main_soft; 2387 int rulen, portcmp, off, skip; 2388 struct frentry *fr, *fnext; 2389 u_32_t passt, passo; 2390 2391 /* 2392 * Do not allow nesting deeper than 16 levels. 2393 */ 2394 if (fin->fin_depth >= 16) 2395 return pass; 2396 2397 fr = fin->fin_fr; 2398 2399 /* 2400 * If there are no rules in this list, return now. 2401 */ 2402 if (fr == NULL) 2403 return pass; 2404 2405 skip = 0; 2406 portcmp = 0; 2407 fin->fin_depth++; 2408 fin->fin_fr = NULL; 2409 off = fin->fin_off; 2410 2411 if ((fin->fin_flx & FI_TCPUDP) && (fin->fin_dlen > 3) && !off) 2412 portcmp = 1; 2413 2414 for (rulen = 0; fr; fr = fnext, rulen++) { 2415 fnext = fr->fr_next; 2416 if (skip != 0) { 2417 FR_VERBOSE(("SKIP %d (%#x)\n", skip, fr->fr_flags)); 2418 skip--; 2419 continue; 2420 } 2421 2422 /* 2423 * In all checks below, a null (zero) value in the 2424 * filter struture is taken to mean a wildcard. 2425 * 2426 * check that we are working for the right interface 2427 */ 2428#ifdef _KERNEL 2429 if (fr->fr_ifa && fr->fr_ifa != fin->fin_ifp) 2430 continue; 2431#else 2432 if (opts & (OPT_VERBOSE|OPT_DEBUG)) 2433 printf("\n"); 2434 FR_VERBOSE(("%c", FR_ISSKIP(pass) ? 's' : 2435 FR_ISPASS(pass) ? 'p' : 2436 FR_ISACCOUNT(pass) ? 'A' : 2437 FR_ISAUTH(pass) ? 'a' : 2438 (pass & FR_NOMATCH) ? 'n' :'b')); 2439 if (fr->fr_ifa && fr->fr_ifa != fin->fin_ifp) 2440 continue; 2441 FR_VERBOSE((":i")); 2442#endif 2443 2444 switch (fr->fr_type) 2445 { 2446 case FR_T_IPF : 2447 case FR_T_IPF_BUILTIN : 2448 if (ipf_check_ipf(fin, fr, portcmp)) 2449 continue; 2450 break; 2451#if defined(IPFILTER_BPF) 2452 case FR_T_BPFOPC : 2453 case FR_T_BPFOPC_BUILTIN : 2454 { 2455 u_char *mc; 2456 int wlen; 2457 2458 if (*fin->fin_mp == NULL) 2459 continue; 2460 if (fin->fin_family != fr->fr_family) 2461 continue; 2462 mc = (u_char *)fin->fin_m; 2463 wlen = fin->fin_dlen + fin->fin_hlen; 2464 if (!bpf_filter(fr->fr_data, mc, wlen, 0)) 2465 continue; 2466 break; 2467 } 2468#endif 2469 case FR_T_CALLFUNC_BUILTIN : 2470 { 2471 frentry_t *f; 2472 2473 f = (*fr->fr_func)(fin, &pass); 2474 if (f != NULL) 2475 fr = f; 2476 else 2477 continue; 2478 break; 2479 } 2480 2481 case FR_T_IPFEXPR : 2482 case FR_T_IPFEXPR_BUILTIN : 2483 if (fin->fin_family != fr->fr_family) 2484 continue; 2485 if (ipf_fr_matcharray(fin, fr->fr_data) == 0) 2486 continue; 2487 break; 2488 2489 default : 2490 break; 2491 } 2492 2493 if ((fin->fin_out == 0) && (fr->fr_nattag.ipt_num[0] != 0)) { 2494 if (fin->fin_nattag == NULL) 2495 continue; 2496 if (ipf_matchtag(&fr->fr_nattag, fin->fin_nattag) == 0) 2497 continue; 2498 } 2499 FR_VERBOSE(("=%d/%d.%d *", fr->fr_grhead, fr->fr_group, rulen)); 2500 2501 passt = fr->fr_flags; 2502 2503 /* 2504 * If the rule is a "call now" rule, then call the function 2505 * in the rule, if it exists and use the results from that. 2506 * If the function pointer is bad, just make like we ignore 2507 * it, except for increasing the hit counter. 2508 */ 2509 if ((passt & FR_CALLNOW) != 0) { 2510 frentry_t *frs; 2511 2512 ATOMIC_INC64(fr->fr_hits); 2513 if ((fr->fr_func == NULL) || 2514 (fr->fr_func == (ipfunc_t)-1)) 2515 continue; 2516 2517 frs = fin->fin_fr; 2518 fin->fin_fr = fr; 2519 fr = (*fr->fr_func)(fin, &passt); 2520 if (fr == NULL) { 2521 fin->fin_fr = frs; 2522 continue; 2523 } 2524 passt = fr->fr_flags; 2525 } 2526 fin->fin_fr = fr; 2527 2528#ifdef IPFILTER_LOG 2529 /* 2530 * Just log this packet... 2531 */ 2532 if ((passt & FR_LOGMASK) == FR_LOG) { 2533 if (ipf_log_pkt(fin, passt) == -1) { 2534 if (passt & FR_LOGORBLOCK) { 2535 DT(frb_logfail); 2536 passt &= ~FR_CMDMASK; 2537 passt |= FR_BLOCK|FR_QUICK; 2538 fin->fin_reason = FRB_LOGFAIL; 2539 } 2540 } 2541 } 2542#endif /* IPFILTER_LOG */ 2543 2544 MUTEX_ENTER(&fr->fr_lock); 2545 fr->fr_bytes += (U_QUAD_T)fin->fin_plen; 2546 fr->fr_hits++; 2547 MUTEX_EXIT(&fr->fr_lock); 2548 fin->fin_rule = rulen; 2549 2550 passo = pass; 2551 if (FR_ISSKIP(passt)) { 2552 skip = fr->fr_arg; 2553 continue; 2554 } else if (((passt & FR_LOGMASK) != FR_LOG) && 2555 ((passt & FR_LOGMASK) != FR_DECAPSULATE)) { 2556 pass = passt; 2557 } 2558 2559 if (passt & (FR_RETICMP|FR_FAKEICMP)) 2560 fin->fin_icode = fr->fr_icode; 2561 2562 if (fr->fr_group != -1) { 2563 (void) strncpy(fin->fin_group, 2564 FR_NAME(fr, fr_group), 2565 strlen(FR_NAME(fr, fr_group))); 2566 } else { 2567 fin->fin_group[0] = '\0'; 2568 } 2569 2570 FR_DEBUG(("pass %#x/%#x/%x\n", passo, pass, passt)); 2571 2572 if (fr->fr_grphead != NULL) { 2573 fin->fin_fr = fr->fr_grphead->fg_start; 2574 FR_VERBOSE(("group %s\n", FR_NAME(fr, fr_grhead))); 2575 2576 if (FR_ISDECAPS(passt)) 2577 passt = ipf_decaps(fin, pass, fr->fr_icode); 2578 else 2579 passt = ipf_scanlist(fin, pass); 2580 2581 if (fin->fin_fr == NULL) { 2582 fin->fin_rule = rulen; 2583 if (fr->fr_group != -1) 2584 (void) strncpy(fin->fin_group, 2585 fr->fr_names + 2586 fr->fr_group, 2587 strlen(fr->fr_names + 2588 fr->fr_group)); 2589 fin->fin_fr = fr; 2590 passt = pass; 2591 } 2592 pass = passt; 2593 } 2594 2595 if (pass & FR_QUICK) { 2596 /* 2597 * Finally, if we've asked to track state for this 2598 * packet, set it up. Add state for "quick" rules 2599 * here so that if the action fails we can consider 2600 * the rule to "not match" and keep on processing 2601 * filter rules. 2602 */ 2603 if ((pass & FR_KEEPSTATE) && !FR_ISAUTH(pass) && 2604 !(fin->fin_flx & FI_STATE)) { 2605 int out = fin->fin_out; 2606 2607 fin->fin_fr = fr; 2608 if (ipf_state_add(softc, fin, NULL, 0) == 0) { 2609 LBUMPD(ipf_stats[out], fr_ads); 2610 } else { 2611 LBUMPD(ipf_stats[out], fr_bads); 2612 pass = passo; 2613 continue; 2614 } 2615 } 2616 break; 2617 } 2618 } 2619 fin->fin_depth--; 2620 return pass; 2621} 2622 2623 2624/* ------------------------------------------------------------------------ */ 2625/* Function: ipf_acctpkt */ 2626/* Returns: frentry_t* - always returns NULL */ 2627/* Parameters: fin(I) - pointer to packet information */ 2628/* passp(IO) - pointer to current/new filter decision (unused) */ 2629/* */ 2630/* Checks a packet against accounting rules, if there are any for the given */ 2631/* IP protocol version. */ 2632/* */ 2633/* N.B.: this function returns NULL to match the prototype used by other */ 2634/* functions called from the IPFilter "mainline" in ipf_check(). */ 2635/* ------------------------------------------------------------------------ */ 2636frentry_t * 2637ipf_acctpkt(fin, passp) 2638 fr_info_t *fin; 2639 u_32_t *passp; 2640{ 2641 ipf_main_softc_t *softc = fin->fin_main_soft; 2642 char group[FR_GROUPLEN]; 2643 frentry_t *fr, *frsave; 2644 u_32_t pass, rulen; 2645 2646 passp = passp; 2647 fr = softc->ipf_acct[fin->fin_out][softc->ipf_active]; 2648 2649 if (fr != NULL) { 2650 frsave = fin->fin_fr; 2651 bcopy(fin->fin_group, group, FR_GROUPLEN); 2652 rulen = fin->fin_rule; 2653 fin->fin_fr = fr; 2654 pass = ipf_scanlist(fin, FR_NOMATCH); 2655 if (FR_ISACCOUNT(pass)) { 2656 LBUMPD(ipf_stats[0], fr_acct); 2657 } 2658 fin->fin_fr = frsave; 2659 bcopy(group, fin->fin_group, FR_GROUPLEN); 2660 fin->fin_rule = rulen; 2661 } 2662 return NULL; 2663} 2664 2665 2666/* ------------------------------------------------------------------------ */ 2667/* Function: ipf_firewall */ 2668/* Returns: frentry_t* - returns pointer to matched rule, if no matches */ 2669/* were found, returns NULL. */ 2670/* Parameters: fin(I) - pointer to packet information */ 2671/* passp(IO) - pointer to current/new filter decision (unused) */ 2672/* */ 2673/* Applies an appropriate set of firewall rules to the packet, to see if */ 2674/* there are any matches. The first check is to see if a match can be seen */ 2675/* in the cache. If not, then search an appropriate list of rules. Once a */ 2676/* matching rule is found, take any appropriate actions as defined by the */ 2677/* rule - except logging. */ 2678/* ------------------------------------------------------------------------ */ 2679static frentry_t * 2680ipf_firewall(fin, passp) 2681 fr_info_t *fin; 2682 u_32_t *passp; 2683{ 2684 ipf_main_softc_t *softc = fin->fin_main_soft; 2685 frentry_t *fr; 2686 u_32_t pass; 2687 int out; 2688 2689 out = fin->fin_out; 2690 pass = *passp; 2691 2692 /* 2693 * This rule cache will only affect packets that are not being 2694 * statefully filtered. 2695 */ 2696 fin->fin_fr = softc->ipf_rules[out][softc->ipf_active]; 2697 if (fin->fin_fr != NULL) 2698 pass = ipf_scanlist(fin, softc->ipf_pass); 2699 2700 if ((pass & FR_NOMATCH)) { 2701 LBUMPD(ipf_stats[out], fr_nom); 2702 } 2703 fr = fin->fin_fr; 2704 2705 /* 2706 * Apply packets per second rate-limiting to a rule as required. 2707 */ 2708 if ((fr != NULL) && (fr->fr_pps != 0) && 2709 !ppsratecheck(&fr->fr_lastpkt, &fr->fr_curpps, fr->fr_pps)) { 2710 DT2(frb_ppsrate, fr_info_t *, fin, frentry_t *, fr); 2711 pass &= ~(FR_CMDMASK|FR_RETICMP|FR_RETRST); 2712 pass |= FR_BLOCK; 2713 LBUMPD(ipf_stats[out], fr_ppshit); 2714 fin->fin_reason = FRB_PPSRATE; 2715 } 2716 2717 /* 2718 * If we fail to add a packet to the authorization queue, then we 2719 * drop the packet later. However, if it was added then pretend 2720 * we've dropped it already. 2721 */ 2722 if (FR_ISAUTH(pass)) { 2723 if (ipf_auth_new(fin->fin_m, fin) != 0) { 2724 DT1(frb_authnew, fr_info_t *, fin); 2725 fin->fin_m = *fin->fin_mp = NULL; 2726 fin->fin_reason = FRB_AUTHNEW; 2727 fin->fin_error = 0; 2728 } else { 2729 IPFERROR(1); 2730 fin->fin_error = ENOSPC; 2731 } 2732 } 2733 2734 if ((fr != NULL) && (fr->fr_func != NULL) && 2735 (fr->fr_func != (ipfunc_t)-1) && !(pass & FR_CALLNOW)) 2736 (void) (*fr->fr_func)(fin, &pass); 2737 2738 /* 2739 * If a rule is a pre-auth rule, check again in the list of rules 2740 * loaded for authenticated use. It does not particulary matter 2741 * if this search fails because a "preauth" result, from a rule, 2742 * is treated as "not a pass", hence the packet is blocked. 2743 */ 2744 if (FR_ISPREAUTH(pass)) { 2745 pass = ipf_auth_pre_scanlist(softc, fin, pass); 2746 } 2747 2748 /* 2749 * If the rule has "keep frag" and the packet is actually a fragment, 2750 * then create a fragment state entry. 2751 */ 2752 if (pass & FR_KEEPFRAG) { 2753 if (fin->fin_flx & FI_FRAG) { 2754 if (ipf_frag_new(softc, fin, pass) == -1) { 2755 LBUMP(ipf_stats[out].fr_bnfr); 2756 } else { 2757 LBUMP(ipf_stats[out].fr_nfr); 2758 } 2759 } else { 2760 LBUMP(ipf_stats[out].fr_cfr); 2761 } 2762 } 2763 2764 fr = fin->fin_fr; 2765 *passp = pass; 2766 2767 return fr; 2768} 2769 2770 2771/* ------------------------------------------------------------------------ */ 2772/* Function: ipf_check */ 2773/* Returns: int - 0 == packet allowed through, */ 2774/* User space: */ 2775/* -1 == packet blocked */ 2776/* 1 == packet not matched */ 2777/* -2 == requires authentication */ 2778/* Kernel: */ 2779/* > 0 == filter error # for packet */ 2780/* Parameters: ctx(I) - pointer to the instance context */ 2781/* ip(I) - pointer to start of IPv4/6 packet */ 2782/* hlen(I) - length of header */ 2783/* ifp(I) - pointer to interface this packet is on */ 2784/* out(I) - 0 == packet going in, 1 == packet going out */ 2785/* mp(IO) - pointer to caller's buffer pointer that holds this */ 2786/* IP packet. */ 2787/* Solaris: */ 2788/* qpi(I) - pointer to STREAMS queue information for this */ 2789/* interface & direction. */ 2790/* */ 2791/* ipf_check() is the master function for all IPFilter packet processing. */ 2792/* It orchestrates: Network Address Translation (NAT), checking for packet */ 2793/* authorisation (or pre-authorisation), presence of related state info., */ 2794/* generating log entries, IP packet accounting, routing of packets as */ 2795/* directed by firewall rules and of course whether or not to allow the */ 2796/* packet to be further processed by the kernel. */ 2797/* */ 2798/* For packets blocked, the contents of "mp" will be NULL'd and the buffer */ 2799/* freed. Packets passed may be returned with the pointer pointed to by */ 2800/* by "mp" changed to a new buffer. */ 2801/* ------------------------------------------------------------------------ */ 2802int 2803ipf_check(ctx, ip, hlen, ifp, out 2804#if defined(_KERNEL) && defined(MENTAT) 2805 , qif, mp) 2806 void *qif; 2807#else 2808 , mp) 2809#endif 2810 mb_t **mp; 2811 ip_t *ip; 2812 int hlen; 2813 struct ifnet *ifp; 2814 int out; 2815 void *ctx; 2816{ 2817 /* 2818 * The above really sucks, but short of writing a diff 2819 */ 2820 ipf_main_softc_t *softc = ctx; 2821 fr_info_t frinfo; 2822 fr_info_t *fin = &frinfo; 2823 u_32_t pass = softc->ipf_pass; 2824 frentry_t *fr = NULL; 2825 int v = IP_V(ip); 2826 mb_t *mc = NULL; 2827 mb_t *m; 2828 /* 2829 * The first part of ipf_check() deals with making sure that what goes 2830 * into the filtering engine makes some sense. Information about the 2831 * the packet is distilled, collected into a fr_info_t structure and 2832 * the an attempt to ensure the buffer the packet is in is big enough 2833 * to hold all the required packet headers. 2834 */ 2835#ifdef _KERNEL 2836# ifdef MENTAT 2837 qpktinfo_t *qpi = qif; 2838 2839# ifdef __sparc 2840 if ((u_int)ip & 0x3) 2841 return 2; 2842# endif 2843# else 2844 SPL_INT(s); 2845# endif 2846 2847 if (softc->ipf_running <= 0) { 2848 return 0; 2849 } 2850 2851 bzero((char *)fin, sizeof(*fin)); 2852 2853# ifdef MENTAT 2854 if (qpi->qpi_flags & QF_BROADCAST) 2855 fin->fin_flx |= FI_MBCAST|FI_BROADCAST; 2856 if (qpi->qpi_flags & QF_MULTICAST) 2857 fin->fin_flx |= FI_MBCAST|FI_MULTICAST; 2858 m = qpi->qpi_m; 2859 fin->fin_qfm = m; 2860 fin->fin_qpi = qpi; 2861# else /* MENTAT */ 2862 2863 m = *mp; 2864 2865# if defined(M_MCAST) 2866 if ((m->m_flags & M_MCAST) != 0) 2867 fin->fin_flx |= FI_MBCAST|FI_MULTICAST; 2868# endif 2869# if defined(M_MLOOP) 2870 if ((m->m_flags & M_MLOOP) != 0) 2871 fin->fin_flx |= FI_MBCAST|FI_MULTICAST; 2872# endif 2873# if defined(M_BCAST) 2874 if ((m->m_flags & M_BCAST) != 0) 2875 fin->fin_flx |= FI_MBCAST|FI_BROADCAST; 2876# endif 2877# ifdef M_CANFASTFWD 2878 /* 2879 * XXX For now, IP Filter and fast-forwarding of cached flows 2880 * XXX are mutually exclusive. Eventually, IP Filter should 2881 * XXX get a "can-fast-forward" filter rule. 2882 */ 2883 m->m_flags &= ~M_CANFASTFWD; 2884# endif /* M_CANFASTFWD */ 2885# if defined(CSUM_DELAY_DATA) && !defined(__FreeBSD_version) 2886 /* 2887 * disable delayed checksums. 2888 */ 2889 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 2890 in_delayed_cksum(m); 2891 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 2892 } 2893# endif /* CSUM_DELAY_DATA */ 2894# endif /* MENTAT */ 2895#else 2896 bzero((char *)fin, sizeof(*fin)); 2897 m = *mp; 2898# if defined(M_MCAST) 2899 if ((m->m_flags & M_MCAST) != 0) 2900 fin->fin_flx |= FI_MBCAST|FI_MULTICAST; 2901# endif 2902# if defined(M_MLOOP) 2903 if ((m->m_flags & M_MLOOP) != 0) 2904 fin->fin_flx |= FI_MBCAST|FI_MULTICAST; 2905# endif 2906# if defined(M_BCAST) 2907 if ((m->m_flags & M_BCAST) != 0) 2908 fin->fin_flx |= FI_MBCAST|FI_BROADCAST; 2909# endif 2910#endif /* _KERNEL */ 2911 2912 fin->fin_v = v; 2913 fin->fin_m = m; 2914 fin->fin_ip = ip; 2915 fin->fin_mp = mp; 2916 fin->fin_out = out; 2917 fin->fin_ifp = ifp; 2918 fin->fin_error = ENETUNREACH; 2919 fin->fin_hlen = (u_short)hlen; 2920 fin->fin_dp = (char *)ip + hlen; 2921 fin->fin_main_soft = softc; 2922 2923 fin->fin_ipoff = (char *)ip - MTOD(m, char *); 2924 2925 SPL_NET(s); 2926 2927#ifdef USE_INET6 2928 if (v == 6) { 2929 LBUMP(ipf_stats[out].fr_ipv6); 2930 /* 2931 * Jumbo grams are quite likely too big for internal buffer 2932 * structures to handle comfortably, for now, so just drop 2933 * them. 2934 */ 2935 if (((ip6_t *)ip)->ip6_plen == 0) { 2936 DT1(frb_jumbo, ip6_t *, (ip6_t *)ip); 2937 pass = FR_BLOCK|FR_NOMATCH; 2938 fin->fin_reason = FRB_JUMBO; 2939 goto finished; 2940 } 2941 fin->fin_family = AF_INET6; 2942 } else 2943#endif 2944 { 2945 fin->fin_family = AF_INET; 2946 } 2947 2948 if (ipf_makefrip(hlen, ip, fin) == -1) { 2949 DT1(frb_makefrip, fr_info_t *, fin); 2950 pass = FR_BLOCK|FR_NOMATCH; 2951 fin->fin_reason = FRB_MAKEFRIP; 2952 goto finished; 2953 } 2954 2955 /* 2956 * For at least IPv6 packets, if a m_pullup() fails then this pointer 2957 * becomes NULL and so we have no packet to free. 2958 */ 2959 if (*fin->fin_mp == NULL) 2960 goto finished; 2961 2962 if (!out) { 2963 if (v == 4) { 2964 if (softc->ipf_chksrc && !ipf_verifysrc(fin)) { 2965 LBUMPD(ipf_stats[0], fr_v4_badsrc); 2966 fin->fin_flx |= FI_BADSRC; 2967 } 2968 if (fin->fin_ip->ip_ttl < softc->ipf_minttl) { 2969 LBUMPD(ipf_stats[0], fr_v4_badttl); 2970 fin->fin_flx |= FI_LOWTTL; 2971 } 2972 } 2973#ifdef USE_INET6 2974 else if (v == 6) { 2975 if (((ip6_t *)ip)->ip6_hlim < softc->ipf_minttl) { 2976 LBUMPD(ipf_stats[0], fr_v6_badttl); 2977 fin->fin_flx |= FI_LOWTTL; 2978 } 2979 } 2980#endif 2981 } 2982 2983 if (fin->fin_flx & FI_SHORT) { 2984 LBUMPD(ipf_stats[out], fr_short); 2985 } 2986 2987 READ_ENTER(&softc->ipf_mutex); 2988 2989 if (!out) { 2990 switch (fin->fin_v) 2991 { 2992 case 4 : 2993 if (ipf_nat_checkin(fin, &pass) == -1) { 2994 goto filterdone; 2995 } 2996 break; 2997#ifdef USE_INET6 2998 case 6 : 2999 if (ipf_nat6_checkin(fin, &pass) == -1) { 3000 goto filterdone; 3001 } 3002 break; 3003#endif 3004 default : 3005 break; 3006 } 3007 } 3008 /* 3009 * Check auth now. 3010 * If a packet is found in the auth table, then skip checking 3011 * the access lists for permission but we do need to consider 3012 * the result as if it were from the ACL's. In addition, being 3013 * found in the auth table means it has been seen before, so do 3014 * not pass it through accounting (again), lest it be counted twice. 3015 */ 3016 fr = ipf_auth_check(fin, &pass); 3017 if (!out && (fr == NULL)) 3018 (void) ipf_acctpkt(fin, NULL); 3019 3020 if (fr == NULL) { 3021 if ((fin->fin_flx & FI_FRAG) != 0) 3022 fr = ipf_frag_known(fin, &pass); 3023 3024 if (fr == NULL) 3025 fr = ipf_state_check(fin, &pass); 3026 } 3027 3028 if ((pass & FR_NOMATCH) || (fr == NULL)) 3029 fr = ipf_firewall(fin, &pass); 3030 3031 /* 3032 * If we've asked to track state for this packet, set it up. 3033 * Here rather than ipf_firewall because ipf_checkauth may decide 3034 * to return a packet for "keep state" 3035 */ 3036 if ((pass & FR_KEEPSTATE) && (fin->fin_m != NULL) && 3037 !(fin->fin_flx & FI_STATE)) { 3038 if (ipf_state_add(softc, fin, NULL, 0) == 0) { 3039 LBUMP(ipf_stats[out].fr_ads); 3040 } else { 3041 LBUMP(ipf_stats[out].fr_bads); 3042 if (FR_ISPASS(pass)) { 3043 DT(frb_stateadd); 3044 pass &= ~FR_CMDMASK; 3045 pass |= FR_BLOCK; 3046 fin->fin_reason = FRB_STATEADD; 3047 } 3048 } 3049 } 3050 3051 fin->fin_fr = fr; 3052 if ((fr != NULL) && !(fin->fin_flx & FI_STATE)) { 3053 fin->fin_dif = &fr->fr_dif; 3054 fin->fin_tif = &fr->fr_tifs[fin->fin_rev]; 3055 } 3056 3057 /* 3058 * Only count/translate packets which will be passed on, out the 3059 * interface. 3060 */ 3061 if (out && FR_ISPASS(pass)) { 3062 (void) ipf_acctpkt(fin, NULL); 3063 3064 switch (fin->fin_v) 3065 { 3066 case 4 : 3067 if (ipf_nat_checkout(fin, &pass) == -1) { 3068 ; 3069 } else if ((softc->ipf_update_ipid != 0) && (v == 4)) { 3070 if (ipf_updateipid(fin) == -1) { 3071 DT(frb_updateipid); 3072 LBUMP(ipf_stats[1].fr_ipud); 3073 pass &= ~FR_CMDMASK; 3074 pass |= FR_BLOCK; 3075 fin->fin_reason = FRB_UPDATEIPID; 3076 } else { 3077 LBUMP(ipf_stats[0].fr_ipud); 3078 } 3079 } 3080 break; 3081#ifdef USE_INET6 3082 case 6 : 3083 (void) ipf_nat6_checkout(fin, &pass); 3084 break; 3085#endif 3086 default : 3087 break; 3088 } 3089 } 3090 3091filterdone: 3092#ifdef IPFILTER_LOG 3093 if ((softc->ipf_flags & FF_LOGGING) || (pass & FR_LOGMASK)) { 3094 (void) ipf_dolog(fin, &pass); 3095 } 3096#endif 3097 3098 /* 3099 * The FI_STATE flag is cleared here so that calling ipf_state_check 3100 * will work when called from inside of fr_fastroute. Although 3101 * there is a similar flag, FI_NATED, for NAT, it does have the same 3102 * impact on code execution. 3103 */ 3104 fin->fin_flx &= ~FI_STATE; 3105 3106#if defined(FASTROUTE_RECURSION) 3107 /* 3108 * Up the reference on fr_lock and exit ipf_mutex. The generation of 3109 * a packet below can sometimes cause a recursive call into IPFilter. 3110 * On those platforms where that does happen, we need to hang onto 3111 * the filter rule just in case someone decides to remove or flush it 3112 * in the meantime. 3113 */ 3114 if (fr != NULL) { 3115 MUTEX_ENTER(&fr->fr_lock); 3116 fr->fr_ref++; 3117 MUTEX_EXIT(&fr->fr_lock); 3118 } 3119 3120 RWLOCK_EXIT(&softc->ipf_mutex); 3121#endif 3122 3123 if ((pass & FR_RETMASK) != 0) { 3124 /* 3125 * Should we return an ICMP packet to indicate error 3126 * status passing through the packet filter ? 3127 * WARNING: ICMP error packets AND TCP RST packets should 3128 * ONLY be sent in repsonse to incoming packets. Sending 3129 * them in response to outbound packets can result in a 3130 * panic on some operating systems. 3131 */ 3132 if (!out) { 3133 if (pass & FR_RETICMP) { 3134 int dst; 3135 3136 if ((pass & FR_RETMASK) == FR_FAKEICMP) 3137 dst = 1; 3138 else 3139 dst = 0; 3140 (void) ipf_send_icmp_err(ICMP_UNREACH, fin, 3141 dst); 3142 LBUMP(ipf_stats[0].fr_ret); 3143 } else if (((pass & FR_RETMASK) == FR_RETRST) && 3144 !(fin->fin_flx & FI_SHORT)) { 3145 if (((fin->fin_flx & FI_OOW) != 0) || 3146 (ipf_send_reset(fin) == 0)) { 3147 LBUMP(ipf_stats[1].fr_ret); 3148 } 3149 } 3150 3151 /* 3152 * When using return-* with auth rules, the auth code 3153 * takes over disposing of this packet. 3154 */ 3155 if (FR_ISAUTH(pass) && (fin->fin_m != NULL)) { 3156 DT1(frb_authcapture, fr_info_t *, fin); 3157 fin->fin_m = *fin->fin_mp = NULL; 3158 fin->fin_reason = FRB_AUTHCAPTURE; 3159 m = NULL; 3160 } 3161 } else { 3162 if (pass & FR_RETRST) { 3163 fin->fin_error = ECONNRESET; 3164 } 3165 } 3166 } 3167 3168 /* 3169 * After the above so that ICMP unreachables and TCP RSTs get 3170 * created properly. 3171 */ 3172 if (FR_ISBLOCK(pass) && (fin->fin_flx & FI_NEWNAT)) 3173 ipf_nat_uncreate(fin); 3174 3175 /* 3176 * If we didn't drop off the bottom of the list of rules (and thus 3177 * the 'current' rule fr is not NULL), then we may have some extra 3178 * instructions about what to do with a packet. 3179 * Once we're finished return to our caller, freeing the packet if 3180 * we are dropping it. 3181 */ 3182 if (fr != NULL) { 3183 frdest_t *fdp; 3184 3185 /* 3186 * Generate a duplicated packet first because ipf_fastroute 3187 * can lead to fin_m being free'd... not good. 3188 */ 3189 fdp = fin->fin_dif; 3190 if ((fdp != NULL) && (fdp->fd_ptr != NULL) && 3191 (fdp->fd_ptr != (void *)-1)) { 3192 mc = M_COPY(fin->fin_m); 3193 if (mc != NULL) 3194 ipf_fastroute(mc, &mc, fin, fdp); 3195 } 3196 3197 fdp = fin->fin_tif; 3198 if (!out && (pass & FR_FASTROUTE)) { 3199 /* 3200 * For fastroute rule, no destination interface defined 3201 * so pass NULL as the frdest_t parameter 3202 */ 3203 (void) ipf_fastroute(fin->fin_m, mp, fin, NULL); 3204 m = *mp = NULL; 3205 } else if ((fdp != NULL) && (fdp->fd_ptr != NULL) && 3206 (fdp->fd_ptr != (struct ifnet *)-1)) { 3207 /* this is for to rules: */ 3208 ipf_fastroute(fin->fin_m, mp, fin, fdp); 3209 m = *mp = NULL; 3210 } 3211 3212#if defined(FASTROUTE_RECURSION) 3213 (void) ipf_derefrule(softc, &fr); 3214#endif 3215 } 3216#if !defined(FASTROUTE_RECURSION) 3217 RWLOCK_EXIT(&softc->ipf_mutex); 3218#endif 3219 3220finished: 3221 if (!FR_ISPASS(pass)) { 3222 LBUMP(ipf_stats[out].fr_block); 3223 if (*mp != NULL) { 3224#ifdef _KERNEL 3225 FREE_MB_T(*mp); 3226#endif 3227 m = *mp = NULL; 3228 } 3229 } else { 3230 LBUMP(ipf_stats[out].fr_pass); 3231 } 3232 3233 SPL_X(s); 3234 3235#ifdef _KERNEL 3236 if (FR_ISPASS(pass)) 3237 return 0; 3238 LBUMP(ipf_stats[out].fr_blocked[fin->fin_reason]); 3239 return fin->fin_error; 3240#else /* _KERNEL */ 3241 if (*mp != NULL) 3242 (*mp)->mb_ifp = fin->fin_ifp; 3243 blockreason = fin->fin_reason; 3244 FR_VERBOSE(("fin_flx %#x pass %#x ", fin->fin_flx, pass)); 3245 /*if ((pass & FR_CMDMASK) == (softc->ipf_pass & FR_CMDMASK))*/ 3246 if ((pass & FR_NOMATCH) != 0) 3247 return 1; 3248 3249 if ((pass & FR_RETMASK) != 0) 3250 switch (pass & FR_RETMASK) 3251 { 3252 case FR_RETRST : 3253 return 3; 3254 case FR_RETICMP : 3255 return 4; 3256 case FR_FAKEICMP : 3257 return 5; 3258 } 3259 3260 switch (pass & FR_CMDMASK) 3261 { 3262 case FR_PASS : 3263 return 0; 3264 case FR_BLOCK : 3265 return -1; 3266 case FR_AUTH : 3267 return -2; 3268 case FR_ACCOUNT : 3269 return -3; 3270 case FR_PREAUTH : 3271 return -4; 3272 } 3273 return 2; 3274#endif /* _KERNEL */ 3275} 3276 3277 3278#ifdef IPFILTER_LOG 3279/* ------------------------------------------------------------------------ */ 3280/* Function: ipf_dolog */ 3281/* Returns: frentry_t* - returns contents of fin_fr (no change made) */ 3282/* Parameters: fin(I) - pointer to packet information */ 3283/* passp(IO) - pointer to current/new filter decision (unused) */ 3284/* */ 3285/* Checks flags set to see how a packet should be logged, if it is to be */ 3286/* logged. Adjust statistics based on its success or not. */ 3287/* ------------------------------------------------------------------------ */ 3288frentry_t * 3289ipf_dolog(fin, passp) 3290 fr_info_t *fin; 3291 u_32_t *passp; 3292{ 3293 ipf_main_softc_t *softc = fin->fin_main_soft; 3294 u_32_t pass; 3295 int out; 3296 3297 out = fin->fin_out; 3298 pass = *passp; 3299 3300 if ((softc->ipf_flags & FF_LOGNOMATCH) && (pass & FR_NOMATCH)) { 3301 pass |= FF_LOGNOMATCH; 3302 LBUMPD(ipf_stats[out], fr_npkl); 3303 goto logit; 3304 3305 } else if (((pass & FR_LOGMASK) == FR_LOGP) || 3306 (FR_ISPASS(pass) && (softc->ipf_flags & FF_LOGPASS))) { 3307 if ((pass & FR_LOGMASK) != FR_LOGP) 3308 pass |= FF_LOGPASS; 3309 LBUMPD(ipf_stats[out], fr_ppkl); 3310 goto logit; 3311 3312 } else if (((pass & FR_LOGMASK) == FR_LOGB) || 3313 (FR_ISBLOCK(pass) && (softc->ipf_flags & FF_LOGBLOCK))) { 3314 if ((pass & FR_LOGMASK) != FR_LOGB) 3315 pass |= FF_LOGBLOCK; 3316 LBUMPD(ipf_stats[out], fr_bpkl); 3317 3318logit: 3319 if (ipf_log_pkt(fin, pass) == -1) { 3320 /* 3321 * If the "or-block" option has been used then 3322 * block the packet if we failed to log it. 3323 */ 3324 if ((pass & FR_LOGORBLOCK) && FR_ISPASS(pass)) { 3325 DT1(frb_logfail2, u_int, pass); 3326 pass &= ~FR_CMDMASK; 3327 pass |= FR_BLOCK; 3328 fin->fin_reason = FRB_LOGFAIL2; 3329 } 3330 } 3331 *passp = pass; 3332 } 3333 3334 return fin->fin_fr; 3335} 3336#endif /* IPFILTER_LOG */ 3337 3338 3339/* ------------------------------------------------------------------------ */ 3340/* Function: ipf_cksum */ 3341/* Returns: u_short - IP header checksum */ 3342/* Parameters: addr(I) - pointer to start of buffer to checksum */ 3343/* len(I) - length of buffer in bytes */ 3344/* */ 3345/* Calculate the two's complement 16 bit checksum of the buffer passed. */ 3346/* */ 3347/* N.B.: addr should be 16bit aligned. */ 3348/* ------------------------------------------------------------------------ */ 3349u_short 3350ipf_cksum(addr, len) 3351 u_short *addr; 3352 int len; 3353{ 3354 u_32_t sum = 0; 3355 3356 for (sum = 0; len > 1; len -= 2) 3357 sum += *addr++; 3358 3359 /* mop up an odd byte, if necessary */ 3360 if (len == 1) 3361 sum += *(u_char *)addr; 3362 3363 /* 3364 * add back carry outs from top 16 bits to low 16 bits 3365 */ 3366 sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */ 3367 sum += (sum >> 16); /* add carry */ 3368 return (u_short)(~sum); 3369} 3370 3371 3372/* ------------------------------------------------------------------------ */ 3373/* Function: fr_cksum */ 3374/* Returns: u_short - layer 4 checksum */ 3375/* Parameters: fin(I) - pointer to packet information */ 3376/* ip(I) - pointer to IP header */ 3377/* l4proto(I) - protocol to caclulate checksum for */ 3378/* l4hdr(I) - pointer to layer 4 header */ 3379/* */ 3380/* Calculates the TCP checksum for the packet held in "m", using the data */ 3381/* in the IP header "ip" to seed it. */ 3382/* */ 3383/* NB: This function assumes we've pullup'd enough for all of the IP header */ 3384/* and the TCP header. We also assume that data blocks aren't allocated in */ 3385/* odd sizes. */ 3386/* */ 3387/* Expects ip_len and ip_off to be in network byte order when called. */ 3388/* ------------------------------------------------------------------------ */ 3389u_short 3390fr_cksum(fin, ip, l4proto, l4hdr) 3391 fr_info_t *fin; 3392 ip_t *ip; 3393 int l4proto; 3394 void *l4hdr; 3395{ 3396 u_short *sp, slen, sumsave, *csump; 3397 u_int sum, sum2; 3398 int hlen; 3399 int off; 3400#ifdef USE_INET6 3401 ip6_t *ip6; 3402#endif 3403 3404 csump = NULL; 3405 sumsave = 0; 3406 sp = NULL; 3407 slen = 0; 3408 hlen = 0; 3409 sum = 0; 3410 3411 sum = htons((u_short)l4proto); 3412 /* 3413 * Add up IP Header portion 3414 */ 3415#ifdef USE_INET6 3416 if (IP_V(ip) == 4) { 3417#endif 3418 hlen = IP_HL(ip) << 2; 3419 off = hlen; 3420 sp = (u_short *)&ip->ip_src; 3421 sum += *sp++; /* ip_src */ 3422 sum += *sp++; 3423 sum += *sp++; /* ip_dst */ 3424 sum += *sp++; 3425#ifdef USE_INET6 3426 } else if (IP_V(ip) == 6) { 3427 ip6 = (ip6_t *)ip; 3428 hlen = sizeof(*ip6); 3429 off = ((char *)fin->fin_dp - (char *)fin->fin_ip); 3430 sp = (u_short *)&ip6->ip6_src; 3431 sum += *sp++; /* ip6_src */ 3432 sum += *sp++; 3433 sum += *sp++; 3434 sum += *sp++; 3435 sum += *sp++; 3436 sum += *sp++; 3437 sum += *sp++; 3438 sum += *sp++; 3439 /* This needs to be routing header aware. */ 3440 sum += *sp++; /* ip6_dst */ 3441 sum += *sp++; 3442 sum += *sp++; 3443 sum += *sp++; 3444 sum += *sp++; 3445 sum += *sp++; 3446 sum += *sp++; 3447 sum += *sp++; 3448 } else { 3449 return 0xffff; 3450 } 3451#endif 3452 slen = fin->fin_plen - off; 3453 sum += htons(slen); 3454 3455 switch (l4proto) 3456 { 3457 case IPPROTO_UDP : 3458 csump = &((udphdr_t *)l4hdr)->uh_sum; 3459 break; 3460 3461 case IPPROTO_TCP : 3462 csump = &((tcphdr_t *)l4hdr)->th_sum; 3463 break; 3464 case IPPROTO_ICMP : 3465 csump = &((icmphdr_t *)l4hdr)->icmp_cksum; 3466 sum = 0; /* Pseudo-checksum is not included */ 3467 break; 3468#ifdef USE_INET6 3469 case IPPROTO_ICMPV6 : 3470 csump = &((struct icmp6_hdr *)l4hdr)->icmp6_cksum; 3471 break; 3472#endif 3473 default : 3474 break; 3475 } 3476 3477 if (csump != NULL) { 3478 sumsave = *csump; 3479 *csump = 0; 3480 } 3481 3482 sum2 = ipf_pcksum(fin, off, sum); 3483 if (csump != NULL) 3484 *csump = sumsave; 3485 return sum2; 3486} 3487 3488 3489/* ------------------------------------------------------------------------ */ 3490/* Function: ipf_findgroup */ 3491/* Returns: frgroup_t * - NULL = group not found, else pointer to group */ 3492/* Parameters: softc(I) - pointer to soft context main structure */ 3493/* group(I) - group name to search for */ 3494/* unit(I) - device to which this group belongs */ 3495/* set(I) - which set of rules (inactive/inactive) this is */ 3496/* fgpp(O) - pointer to place to store pointer to the pointer */ 3497/* to where to add the next (last) group or where */ 3498/* to delete group from. */ 3499/* */ 3500/* Search amongst the defined groups for a particular group number. */ 3501/* ------------------------------------------------------------------------ */ 3502frgroup_t * 3503ipf_findgroup(softc, group, unit, set, fgpp) 3504 ipf_main_softc_t *softc; 3505 char *group; 3506 minor_t unit; 3507 int set; 3508 frgroup_t ***fgpp; 3509{ 3510 frgroup_t *fg, **fgp; 3511 3512 /* 3513 * Which list of groups to search in is dependent on which list of 3514 * rules are being operated on. 3515 */ 3516 fgp = &softc->ipf_groups[unit][set]; 3517 3518 while ((fg = *fgp) != NULL) { 3519 if (strncmp(group, fg->fg_name, FR_GROUPLEN) == 0) 3520 break; 3521 else 3522 fgp = &fg->fg_next; 3523 } 3524 if (fgpp != NULL) 3525 *fgpp = fgp; 3526 return fg; 3527} 3528 3529 3530/* ------------------------------------------------------------------------ */ 3531/* Function: ipf_group_add */ 3532/* Returns: frgroup_t * - NULL == did not create group, */ 3533/* != NULL == pointer to the group */ 3534/* Parameters: softc(I) - pointer to soft context main structure */ 3535/* num(I) - group number to add */ 3536/* head(I) - rule pointer that is using this as the head */ 3537/* flags(I) - rule flags which describe the type of rule it is */ 3538/* unit(I) - device to which this group will belong to */ 3539/* set(I) - which set of rules (inactive/inactive) this is */ 3540/* Write Locks: ipf_mutex */ 3541/* */ 3542/* Add a new group head, or if it already exists, increase the reference */ 3543/* count to it. */ 3544/* ------------------------------------------------------------------------ */ 3545frgroup_t * 3546ipf_group_add(softc, group, head, flags, unit, set) 3547 ipf_main_softc_t *softc; 3548 char *group; 3549 void *head; 3550 u_32_t flags; 3551 minor_t unit; 3552 int set; 3553{ 3554 frgroup_t *fg, **fgp; 3555 u_32_t gflags; 3556 3557 if (group == NULL) 3558 return NULL; 3559 3560 if (unit == IPL_LOGIPF && *group == '\0') 3561 return NULL; 3562 3563 fgp = NULL; 3564 gflags = flags & FR_INOUT; 3565 3566 fg = ipf_findgroup(softc, group, unit, set, &fgp); 3567 if (fg != NULL) { 3568 if (fg->fg_head == NULL && head != NULL) 3569 fg->fg_head = head; 3570 if (fg->fg_flags == 0) 3571 fg->fg_flags = gflags; 3572 else if (gflags != fg->fg_flags) 3573 return NULL; 3574 fg->fg_ref++; 3575 return fg; 3576 } 3577 3578 KMALLOC(fg, frgroup_t *); 3579 if (fg != NULL) { 3580 fg->fg_head = head; 3581 fg->fg_start = NULL; 3582 fg->fg_next = *fgp; 3583 bcopy(group, fg->fg_name, strlen(group) + 1); 3584 fg->fg_flags = gflags; 3585 fg->fg_ref = 1; 3586 fg->fg_set = &softc->ipf_groups[unit][set]; 3587 *fgp = fg; 3588 } 3589 return fg; 3590} 3591 3592 3593/* ------------------------------------------------------------------------ */ 3594/* Function: ipf_group_del */ 3595/* Returns: int - number of rules deleted */ 3596/* Parameters: softc(I) - pointer to soft context main structure */ 3597/* group(I) - group name to delete */ 3598/* fr(I) - filter rule from which group is referenced */ 3599/* Write Locks: ipf_mutex */ 3600/* */ 3601/* This function is called whenever a reference to a group is to be dropped */ 3602/* and thus its reference count needs to be lowered and the group free'd if */ 3603/* the reference count reaches zero. Passing in fr is really for the sole */ 3604/* purpose of knowing when the head rule is being deleted. */ 3605/* ------------------------------------------------------------------------ */ 3606void 3607ipf_group_del(softc, group, fr) 3608 ipf_main_softc_t *softc; 3609 frgroup_t *group; 3610 frentry_t *fr; 3611{ 3612 3613 if (group->fg_head == fr) 3614 group->fg_head = NULL; 3615 3616 group->fg_ref--; 3617 if ((group->fg_ref == 0) && (group->fg_start == NULL)) 3618 ipf_group_free(group); 3619} 3620 3621 3622/* ------------------------------------------------------------------------ */ 3623/* Function: ipf_group_free */ 3624/* Returns: Nil */ 3625/* Parameters: group(I) - pointer to filter rule group */ 3626/* */ 3627/* Remove the group from the list of groups and free it. */ 3628/* ------------------------------------------------------------------------ */ 3629static void 3630ipf_group_free(group) 3631 frgroup_t *group; 3632{ 3633 frgroup_t **gp; 3634 3635 for (gp = group->fg_set; *gp != NULL; gp = &(*gp)->fg_next) { 3636 if (*gp == group) { 3637 *gp = group->fg_next; 3638 break; 3639 } 3640 } 3641 KFREE(group); 3642} 3643 3644 3645/* ------------------------------------------------------------------------ */ 3646/* Function: ipf_group_flush */ 3647/* Returns: int - number of rules flush from group */ 3648/* Parameters: softc(I) - pointer to soft context main structure */ 3649/* Parameters: group(I) - pointer to filter rule group */ 3650/* */ 3651/* Remove all of the rules that currently are listed under the given group. */ 3652/* ------------------------------------------------------------------------ */ 3653static int 3654ipf_group_flush(softc, group) 3655 ipf_main_softc_t *softc; 3656 frgroup_t *group; 3657{ 3658 int gone = 0; 3659 3660 (void) ipf_flushlist(softc, &gone, &group->fg_start); 3661 3662 return gone; 3663} 3664 3665 3666/* ------------------------------------------------------------------------ */ 3667/* Function: ipf_getrulen */ 3668/* Returns: frentry_t * - NULL == not found, else pointer to rule n */ 3669/* Parameters: softc(I) - pointer to soft context main structure */ 3670/* Parameters: unit(I) - device for which to count the rule's number */ 3671/* flags(I) - which set of rules to find the rule in */ 3672/* group(I) - group name */ 3673/* n(I) - rule number to find */ 3674/* */ 3675/* Find rule # n in group # g and return a pointer to it. Return NULl if */ 3676/* group # g doesn't exist or there are less than n rules in the group. */ 3677/* ------------------------------------------------------------------------ */ 3678frentry_t * 3679ipf_getrulen(softc, unit, group, n) 3680 ipf_main_softc_t *softc; 3681 int unit; 3682 char *group; 3683 u_32_t n; 3684{ 3685 frentry_t *fr; 3686 frgroup_t *fg; 3687 3688 fg = ipf_findgroup(softc, group, unit, softc->ipf_active, NULL); 3689 if (fg == NULL) 3690 return NULL; 3691 for (fr = fg->fg_start; fr && n; fr = fr->fr_next, n--) 3692 ; 3693 if (n != 0) 3694 return NULL; 3695 return fr; 3696} 3697 3698 3699/* ------------------------------------------------------------------------ */ 3700/* Function: ipf_flushlist */ 3701/* Returns: int - >= 0 - number of flushed rules */ 3702/* Parameters: softc(I) - pointer to soft context main structure */ 3703/* nfreedp(O) - pointer to int where flush count is stored */ 3704/* listp(I) - pointer to list to flush pointer */ 3705/* Write Locks: ipf_mutex */ 3706/* */ 3707/* Recursively flush rules from the list, descending groups as they are */ 3708/* encountered. if a rule is the head of a group and it has lost all its */ 3709/* group members, then also delete the group reference. nfreedp is needed */ 3710/* to store the accumulating count of rules removed, whereas the returned */ 3711/* value is just the number removed from the current list. The latter is */ 3712/* needed to correctly adjust reference counts on rules that define groups. */ 3713/* */ 3714/* NOTE: Rules not loaded from user space cannot be flushed. */ 3715/* ------------------------------------------------------------------------ */ 3716static int 3717ipf_flushlist(softc, nfreedp, listp) 3718 ipf_main_softc_t *softc; 3719 int *nfreedp; 3720 frentry_t **listp; 3721{ 3722 int freed = 0; 3723 frentry_t *fp; 3724 3725 while ((fp = *listp) != NULL) { 3726 if ((fp->fr_type & FR_T_BUILTIN) || 3727 !(fp->fr_flags & FR_COPIED)) { 3728 listp = &fp->fr_next; 3729 continue; 3730 } 3731 *listp = fp->fr_next; 3732 if (fp->fr_next != NULL) 3733 fp->fr_next->fr_pnext = fp->fr_pnext; 3734 fp->fr_pnext = NULL; 3735 3736 if (fp->fr_grphead != NULL) { 3737 freed += ipf_group_flush(softc, fp->fr_grphead); 3738 fp->fr_names[fp->fr_grhead] = '\0'; 3739 } 3740 3741 if (fp->fr_icmpgrp != NULL) { 3742 freed += ipf_group_flush(softc, fp->fr_icmpgrp); 3743 fp->fr_names[fp->fr_icmphead] = '\0'; 3744 } 3745 3746 if (fp->fr_srctrack.ht_max_nodes) 3747 ipf_rb_ht_flush(&fp->fr_srctrack); 3748 3749 fp->fr_next = NULL; 3750 3751 ASSERT(fp->fr_ref > 0); 3752 if (ipf_derefrule(softc, &fp) == 0) 3753 freed++; 3754 } 3755 *nfreedp += freed; 3756 return freed; 3757} 3758 3759 3760/* ------------------------------------------------------------------------ */ 3761/* Function: ipf_flush */ 3762/* Returns: int - >= 0 - number of flushed rules */ 3763/* Parameters: softc(I) - pointer to soft context main structure */ 3764/* unit(I) - device for which to flush rules */ 3765/* flags(I) - which set of rules to flush */ 3766/* */ 3767/* Calls flushlist() for all filter rules (accounting, firewall - both IPv4 */ 3768/* and IPv6) as defined by the value of flags. */ 3769/* ------------------------------------------------------------------------ */ 3770int 3771ipf_flush(softc, unit, flags) 3772 ipf_main_softc_t *softc; 3773 minor_t unit; 3774 int flags; 3775{ 3776 int flushed = 0, set; 3777 3778 WRITE_ENTER(&softc->ipf_mutex); 3779 3780 set = softc->ipf_active; 3781 if ((flags & FR_INACTIVE) == FR_INACTIVE) 3782 set = 1 - set; 3783 3784 if (flags & FR_OUTQUE) { 3785 ipf_flushlist(softc, &flushed, &softc->ipf_rules[1][set]); 3786 ipf_flushlist(softc, &flushed, &softc->ipf_acct[1][set]); 3787 } 3788 if (flags & FR_INQUE) { 3789 ipf_flushlist(softc, &flushed, &softc->ipf_rules[0][set]); 3790 ipf_flushlist(softc, &flushed, &softc->ipf_acct[0][set]); 3791 } 3792 3793 flushed += ipf_flush_groups(softc, &softc->ipf_groups[unit][set], 3794 flags & (FR_INQUE|FR_OUTQUE)); 3795 3796 RWLOCK_EXIT(&softc->ipf_mutex); 3797 3798 if (unit == IPL_LOGIPF) { 3799 int tmp; 3800 3801 tmp = ipf_flush(softc, IPL_LOGCOUNT, flags); 3802 if (tmp >= 0) 3803 flushed += tmp; 3804 } 3805 return flushed; 3806} 3807 3808 3809/* ------------------------------------------------------------------------ */ 3810/* Function: ipf_flush_groups */ 3811/* Returns: int - >= 0 - number of flushed rules */ 3812/* Parameters: softc(I) - soft context pointerto work with */ 3813/* grhead(I) - pointer to the start of the group list to flush */ 3814/* flags(I) - which set of rules to flush */ 3815/* */ 3816/* Walk through all of the groups under the given group head and remove all */ 3817/* of those that match the flags passed in. The for loop here is bit more */ 3818/* complicated than usual because the removal of a rule with ipf_derefrule */ 3819/* may end up removing not only the structure pointed to by "fg" but also */ 3820/* what is fg_next and fg_next after that. So if a filter rule is actually */ 3821/* removed from the group then it is necessary to start again. */ 3822/* ------------------------------------------------------------------------ */ 3823static int 3824ipf_flush_groups(softc, grhead, flags) 3825 ipf_main_softc_t *softc; 3826 frgroup_t **grhead; 3827 int flags; 3828{ 3829 frentry_t *fr, **frp; 3830 frgroup_t *fg, **fgp; 3831 int flushed = 0; 3832 int removed = 0; 3833 3834 for (fgp = grhead; (fg = *fgp) != NULL; ) { 3835 while ((fg != NULL) && ((fg->fg_flags & flags) == 0)) 3836 fg = fg->fg_next; 3837 if (fg == NULL) 3838 break; 3839 removed = 0; 3840 frp = &fg->fg_start; 3841 while ((removed == 0) && ((fr = *frp) != NULL)) { 3842 if ((fr->fr_flags & flags) == 0) { 3843 frp = &fr->fr_next; 3844 } else { 3845 if (fr->fr_next != NULL) 3846 fr->fr_next->fr_pnext = fr->fr_pnext; 3847 *frp = fr->fr_next; 3848 fr->fr_pnext = NULL; 3849 fr->fr_next = NULL; 3850 (void) ipf_derefrule(softc, &fr); 3851 flushed++; 3852 removed++; 3853 } 3854 } 3855 if (removed == 0) 3856 fgp = &fg->fg_next; 3857 } 3858 return flushed; 3859} 3860 3861 3862/* ------------------------------------------------------------------------ */ 3863/* Function: memstr */ 3864/* Returns: char * - NULL if failed, != NULL pointer to matching bytes */ 3865/* Parameters: src(I) - pointer to byte sequence to match */ 3866/* dst(I) - pointer to byte sequence to search */ 3867/* slen(I) - match length */ 3868/* dlen(I) - length available to search in */ 3869/* */ 3870/* Search dst for a sequence of bytes matching those at src and extend for */ 3871/* slen bytes. */ 3872/* ------------------------------------------------------------------------ */ 3873char * 3874memstr(src, dst, slen, dlen) 3875 const char *src; 3876 char *dst; 3877 size_t slen, dlen; 3878{ 3879 char *s = NULL; 3880 3881 while (dlen >= slen) { 3882 if (bcmp(src, dst, slen) == 0) { 3883 s = dst; 3884 break; 3885 } 3886 dst++; 3887 dlen--; 3888 } 3889 return s; 3890} 3891/* ------------------------------------------------------------------------ */ 3892/* Function: ipf_fixskip */ 3893/* Returns: Nil */ 3894/* Parameters: listp(IO) - pointer to start of list with skip rule */ 3895/* rp(I) - rule added/removed with skip in it. */ 3896/* addremove(I) - adjustment (-1/+1) to make to skip count, */ 3897/* depending on whether a rule was just added */ 3898/* or removed. */ 3899/* */ 3900/* Adjust all the rules in a list which would have skip'd past the position */ 3901/* where we are inserting to skip to the right place given the change. */ 3902/* ------------------------------------------------------------------------ */ 3903void 3904ipf_fixskip(listp, rp, addremove) 3905 frentry_t **listp, *rp; 3906 int addremove; 3907{ 3908 int rules, rn; 3909 frentry_t *fp; 3910 3911 rules = 0; 3912 for (fp = *listp; (fp != NULL) && (fp != rp); fp = fp->fr_next) 3913 rules++; 3914 3915 if (fp == NULL) 3916 return; 3917 3918 for (rn = 0, fp = *listp; fp && (fp != rp); fp = fp->fr_next, rn++) 3919 if (FR_ISSKIP(fp->fr_flags) && (rn + fp->fr_arg >= rules)) 3920 fp->fr_arg += addremove; 3921} 3922 3923 3924#ifdef _KERNEL 3925/* ------------------------------------------------------------------------ */ 3926/* Function: count4bits */ 3927/* Returns: int - >= 0 - number of consecutive bits in input */ 3928/* Parameters: ip(I) - 32bit IP address */ 3929/* */ 3930/* IPv4 ONLY */ 3931/* count consecutive 1's in bit mask. If the mask generated by counting */ 3932/* consecutive 1's is different to that passed, return -1, else return # */ 3933/* of bits. */ 3934/* ------------------------------------------------------------------------ */ 3935int 3936count4bits(ip) 3937 u_32_t ip; 3938{ 3939 u_32_t ipn; 3940 int cnt = 0, i, j; 3941 3942 ip = ipn = ntohl(ip); 3943 for (i = 32; i; i--, ipn *= 2) 3944 if (ipn & 0x80000000) 3945 cnt++; 3946 else 3947 break; 3948 ipn = 0; 3949 for (i = 32, j = cnt; i; i--, j--) { 3950 ipn *= 2; 3951 if (j > 0) 3952 ipn++; 3953 } 3954 if (ipn == ip) 3955 return cnt; 3956 return -1; 3957} 3958 3959 3960/* ------------------------------------------------------------------------ */ 3961/* Function: count6bits */ 3962/* Returns: int - >= 0 - number of consecutive bits in input */ 3963/* Parameters: msk(I) - pointer to start of IPv6 bitmask */ 3964/* */ 3965/* IPv6 ONLY */ 3966/* count consecutive 1's in bit mask. */ 3967/* ------------------------------------------------------------------------ */ 3968# ifdef USE_INET6 3969int 3970count6bits(msk) 3971 u_32_t *msk; 3972{ 3973 int i = 0, k; 3974 u_32_t j; 3975 3976 for (k = 3; k >= 0; k--) 3977 if (msk[k] == 0xffffffff) 3978 i += 32; 3979 else { 3980 for (j = msk[k]; j; j <<= 1) 3981 if (j & 0x80000000) 3982 i++; 3983 } 3984 return i; 3985} 3986# endif 3987#endif /* _KERNEL */ 3988 3989 3990/* ------------------------------------------------------------------------ */ 3991/* Function: ipf_synclist */ 3992/* Returns: int - 0 = no failures, else indication of first failure */ 3993/* Parameters: fr(I) - start of filter list to sync interface names for */ 3994/* ifp(I) - interface pointer for limiting sync lookups */ 3995/* Write Locks: ipf_mutex */ 3996/* */ 3997/* Walk through a list of filter rules and resolve any interface names into */ 3998/* pointers. Where dynamic addresses are used, also update the IP address */ 3999/* used in the rule. The interface pointer is used to limit the lookups to */ 4000/* a specific set of matching names if it is non-NULL. */ 4001/* Errors can occur when resolving the destination name of to/dup-to fields */ 4002/* when the name points to a pool and that pool doest not exist. If this */ 4003/* does happen then it is necessary to check if there are any lookup refs */ 4004/* that need to be dropped before returning with an error. */ 4005/* ------------------------------------------------------------------------ */ 4006static int 4007ipf_synclist(softc, fr, ifp) 4008 ipf_main_softc_t *softc; 4009 frentry_t *fr; 4010 void *ifp; 4011{ 4012 frentry_t *frt, *start = fr; 4013 frdest_t *fdp; 4014 char *name; 4015 int error; 4016 void *ifa; 4017 int v, i; 4018 4019 error = 0; 4020 4021 for (; fr; fr = fr->fr_next) { 4022 if (fr->fr_family == AF_INET) 4023 v = 4; 4024 else if (fr->fr_family == AF_INET6) 4025 v = 6; 4026 else 4027 v = 0; 4028 4029 /* 4030 * Lookup all the interface names that are part of the rule. 4031 */ 4032 for (i = 0; i < 4; i++) { 4033 if ((ifp != NULL) && (fr->fr_ifas[i] != ifp)) 4034 continue; 4035 if (fr->fr_ifnames[i] == -1) 4036 continue; 4037 name = FR_NAME(fr, fr_ifnames[i]); 4038 fr->fr_ifas[i] = ipf_resolvenic(softc, name, v); 4039 } 4040 4041 if ((fr->fr_type & ~FR_T_BUILTIN) == FR_T_IPF) { 4042 if (fr->fr_satype != FRI_NORMAL && 4043 fr->fr_satype != FRI_LOOKUP) { 4044 ifa = ipf_resolvenic(softc, fr->fr_names + 4045 fr->fr_sifpidx, v); 4046 ipf_ifpaddr(softc, v, fr->fr_satype, ifa, 4047 &fr->fr_src6, &fr->fr_smsk6); 4048 } 4049 if (fr->fr_datype != FRI_NORMAL && 4050 fr->fr_datype != FRI_LOOKUP) { 4051 ifa = ipf_resolvenic(softc, fr->fr_names + 4052 fr->fr_sifpidx, v); 4053 ipf_ifpaddr(softc, v, fr->fr_datype, ifa, 4054 &fr->fr_dst6, &fr->fr_dmsk6); 4055 } 4056 } 4057 4058 fdp = &fr->fr_tifs[0]; 4059 if ((ifp == NULL) || (fdp->fd_ptr == ifp)) { 4060 error = ipf_resolvedest(softc, fr->fr_names, fdp, v); 4061 if (error != 0) 4062 goto unwind; 4063 } 4064 4065 fdp = &fr->fr_tifs[1]; 4066 if ((ifp == NULL) || (fdp->fd_ptr == ifp)) { 4067 error = ipf_resolvedest(softc, fr->fr_names, fdp, v); 4068 if (error != 0) 4069 goto unwind; 4070 } 4071 4072 fdp = &fr->fr_dif; 4073 if ((ifp == NULL) || (fdp->fd_ptr == ifp)) { 4074 error = ipf_resolvedest(softc, fr->fr_names, fdp, v); 4075 if (error != 0) 4076 goto unwind; 4077 } 4078 4079 if (((fr->fr_type & ~FR_T_BUILTIN) == FR_T_IPF) && 4080 (fr->fr_satype == FRI_LOOKUP) && (fr->fr_srcptr == NULL)) { 4081 fr->fr_srcptr = ipf_lookup_res_num(softc, 4082 fr->fr_srctype, 4083 IPL_LOGIPF, 4084 fr->fr_srcnum, 4085 &fr->fr_srcfunc); 4086 } 4087 if (((fr->fr_type & ~FR_T_BUILTIN) == FR_T_IPF) && 4088 (fr->fr_datype == FRI_LOOKUP) && (fr->fr_dstptr == NULL)) { 4089 fr->fr_dstptr = ipf_lookup_res_num(softc, 4090 fr->fr_dsttype, 4091 IPL_LOGIPF, 4092 fr->fr_dstnum, 4093 &fr->fr_dstfunc); 4094 } 4095 } 4096 return 0; 4097 4098unwind: 4099 for (frt = start; frt != fr; fr = fr->fr_next) { 4100 if (((frt->fr_type & ~FR_T_BUILTIN) == FR_T_IPF) && 4101 (frt->fr_satype == FRI_LOOKUP) && (frt->fr_srcptr != NULL)) 4102 ipf_lookup_deref(softc, frt->fr_srctype, 4103 frt->fr_srcptr); 4104 if (((frt->fr_type & ~FR_T_BUILTIN) == FR_T_IPF) && 4105 (frt->fr_datype == FRI_LOOKUP) && (frt->fr_dstptr != NULL)) 4106 ipf_lookup_deref(softc, frt->fr_dsttype, 4107 frt->fr_dstptr); 4108 } 4109 return error; 4110} 4111 4112 4113/* ------------------------------------------------------------------------ */ 4114/* Function: ipf_sync */ 4115/* Returns: void */ 4116/* Parameters: Nil */ 4117/* */ 4118/* ipf_sync() is called when we suspect that the interface list or */ 4119/* information about interfaces (like IP#) has changed. Go through all */ 4120/* filter rules, NAT entries and the state table and check if anything */ 4121/* needs to be changed/updated. */ 4122/* ------------------------------------------------------------------------ */ 4123int 4124ipf_sync(softc, ifp) 4125 ipf_main_softc_t *softc; 4126 void *ifp; 4127{ 4128 int i; 4129 4130# if !SOLARIS 4131 ipf_nat_sync(softc, ifp); 4132 ipf_state_sync(softc, ifp); 4133 ipf_lookup_sync(softc, ifp); 4134# endif 4135 4136 WRITE_ENTER(&softc->ipf_mutex); 4137 (void) ipf_synclist(softc, softc->ipf_acct[0][softc->ipf_active], ifp); 4138 (void) ipf_synclist(softc, softc->ipf_acct[1][softc->ipf_active], ifp); 4139 (void) ipf_synclist(softc, softc->ipf_rules[0][softc->ipf_active], ifp); 4140 (void) ipf_synclist(softc, softc->ipf_rules[1][softc->ipf_active], ifp); 4141 4142 for (i = 0; i < IPL_LOGSIZE; i++) { 4143 frgroup_t *g; 4144 4145 for (g = softc->ipf_groups[i][0]; g != NULL; g = g->fg_next) 4146 (void) ipf_synclist(softc, g->fg_start, ifp); 4147 for (g = softc->ipf_groups[i][1]; g != NULL; g = g->fg_next) 4148 (void) ipf_synclist(softc, g->fg_start, ifp); 4149 } 4150 RWLOCK_EXIT(&softc->ipf_mutex); 4151 4152 return 0; 4153} 4154 4155 4156/* 4157 * In the functions below, bcopy() is called because the pointer being 4158 * copied _from_ in this instance is a pointer to a char buf (which could 4159 * end up being unaligned) and on the kernel's local stack. 4160 */ 4161/* ------------------------------------------------------------------------ */ 4162/* Function: copyinptr */ 4163/* Returns: int - 0 = success, else failure */ 4164/* Parameters: src(I) - pointer to the source address */ 4165/* dst(I) - destination address */ 4166/* size(I) - number of bytes to copy */ 4167/* */ 4168/* Copy a block of data in from user space, given a pointer to the pointer */ 4169/* to start copying from (src) and a pointer to where to store it (dst). */ 4170/* NB: src - pointer to user space pointer, dst - kernel space pointer */ 4171/* ------------------------------------------------------------------------ */ 4172int 4173copyinptr(softc, src, dst, size) 4174 ipf_main_softc_t *softc; 4175 void *src, *dst; 4176 size_t size; 4177{ 4178 caddr_t ca; 4179 int error; 4180 4181# if SOLARIS 4182 error = COPYIN(src, &ca, sizeof(ca)); 4183 if (error != 0) 4184 return error; 4185# else 4186 bcopy(src, (caddr_t)&ca, sizeof(ca)); 4187# endif 4188 error = COPYIN(ca, dst, size); 4189 if (error != 0) { 4190 IPFERROR(3); 4191 error = EFAULT; 4192 } 4193 return error; 4194} 4195 4196 4197/* ------------------------------------------------------------------------ */ 4198/* Function: copyoutptr */ 4199/* Returns: int - 0 = success, else failure */ 4200/* Parameters: src(I) - pointer to the source address */ 4201/* dst(I) - destination address */ 4202/* size(I) - number of bytes to copy */ 4203/* */ 4204/* Copy a block of data out to user space, given a pointer to the pointer */ 4205/* to start copying from (src) and a pointer to where to store it (dst). */ 4206/* NB: src - kernel space pointer, dst - pointer to user space pointer. */ 4207/* ------------------------------------------------------------------------ */ 4208int 4209copyoutptr(softc, src, dst, size) 4210 ipf_main_softc_t *softc; 4211 void *src, *dst; 4212 size_t size; 4213{ 4214 caddr_t ca; 4215 int error; 4216 4217 bcopy(dst, (caddr_t)&ca, sizeof(ca)); 4218 error = COPYOUT(src, ca, size); 4219 if (error != 0) { 4220 IPFERROR(4); 4221 error = EFAULT; 4222 } 4223 return error; 4224} 4225 4226 4227/* ------------------------------------------------------------------------ */ 4228/* Function: ipf_lock */ 4229/* Returns: int - 0 = success, else error */ 4230/* Parameters: data(I) - pointer to lock value to set */ 4231/* lockp(O) - pointer to location to store old lock value */ 4232/* */ 4233/* Get the new value for the lock integer, set it and return the old value */ 4234/* in *lockp. */ 4235/* ------------------------------------------------------------------------ */ 4236int 4237ipf_lock(data, lockp) 4238 caddr_t data; 4239 int *lockp; 4240{ 4241 int arg, err; 4242 4243 err = BCOPYIN(data, &arg, sizeof(arg)); 4244 if (err != 0) 4245 return EFAULT; 4246 err = BCOPYOUT(lockp, data, sizeof(*lockp)); 4247 if (err != 0) 4248 return EFAULT; 4249 *lockp = arg; 4250 return 0; 4251} 4252 4253 4254/* ------------------------------------------------------------------------ */ 4255/* Function: ipf_getstat */ 4256/* Returns: Nil */ 4257/* Parameters: softc(I) - pointer to soft context main structure */ 4258/* fiop(I) - pointer to ipfilter stats structure */ 4259/* rev(I) - version claim by program doing ioctl */ 4260/* */ 4261/* Stores a copy of current pointers, counters, etc, in the friostat */ 4262/* structure. */ 4263/* If IPFILTER_COMPAT is compiled, we pretend to be whatever version the */ 4264/* program is looking for. This ensure that validation of the version it */ 4265/* expects will always succeed. Thus kernels with IPFILTER_COMPAT will */ 4266/* allow older binaries to work but kernels without it will not. */ 4267/* ------------------------------------------------------------------------ */ 4268/*ARGSUSED*/ 4269static void 4270ipf_getstat(softc, fiop, rev) 4271 ipf_main_softc_t *softc; 4272 friostat_t *fiop; 4273 int rev; 4274{ 4275 int i; 4276 4277 bcopy((char *)softc->ipf_stats, (char *)fiop->f_st, 4278 sizeof(ipf_statistics_t) * 2); 4279 fiop->f_locks[IPL_LOGSTATE] = -1; 4280 fiop->f_locks[IPL_LOGNAT] = -1; 4281 fiop->f_locks[IPL_LOGIPF] = -1; 4282 fiop->f_locks[IPL_LOGAUTH] = -1; 4283 4284 fiop->f_ipf[0][0] = softc->ipf_rules[0][0]; 4285 fiop->f_acct[0][0] = softc->ipf_acct[0][0]; 4286 fiop->f_ipf[0][1] = softc->ipf_rules[0][1]; 4287 fiop->f_acct[0][1] = softc->ipf_acct[0][1]; 4288 fiop->f_ipf[1][0] = softc->ipf_rules[1][0]; 4289 fiop->f_acct[1][0] = softc->ipf_acct[1][0]; 4290 fiop->f_ipf[1][1] = softc->ipf_rules[1][1]; 4291 fiop->f_acct[1][1] = softc->ipf_acct[1][1]; 4292 4293 fiop->f_ticks = softc->ipf_ticks; 4294 fiop->f_active = softc->ipf_active; 4295 fiop->f_froute[0] = softc->ipf_frouteok[0]; 4296 fiop->f_froute[1] = softc->ipf_frouteok[1]; 4297 fiop->f_rb_no_mem = softc->ipf_rb_no_mem; 4298 fiop->f_rb_node_max = softc->ipf_rb_node_max; 4299 4300 fiop->f_running = softc->ipf_running; 4301 for (i = 0; i < IPL_LOGSIZE; i++) { 4302 fiop->f_groups[i][0] = softc->ipf_groups[i][0]; 4303 fiop->f_groups[i][1] = softc->ipf_groups[i][1]; 4304 } 4305#ifdef IPFILTER_LOG 4306 fiop->f_log_ok = ipf_log_logok(softc, IPL_LOGIPF); 4307 fiop->f_log_fail = ipf_log_failures(softc, IPL_LOGIPF); 4308 fiop->f_logging = 1; 4309#else 4310 fiop->f_log_ok = 0; 4311 fiop->f_log_fail = 0; 4312 fiop->f_logging = 0; 4313#endif 4314 fiop->f_defpass = softc->ipf_pass; 4315 fiop->f_features = ipf_features; 4316 4317#ifdef IPFILTER_COMPAT 4318 sprintf(fiop->f_version, "IP Filter: v%d.%d.%d", 4319 (rev / 1000000) % 100, 4320 (rev / 10000) % 100, 4321 (rev / 100) % 100); 4322#else 4323 rev = rev; 4324 (void) strncpy(fiop->f_version, ipfilter_version, 4325 sizeof(fiop->f_version)); 4326#endif 4327} 4328 4329 4330#ifdef USE_INET6 4331int icmptoicmp6types[ICMP_MAXTYPE+1] = { 4332 ICMP6_ECHO_REPLY, /* 0: ICMP_ECHOREPLY */ 4333 -1, /* 1: UNUSED */ 4334 -1, /* 2: UNUSED */ 4335 ICMP6_DST_UNREACH, /* 3: ICMP_UNREACH */ 4336 -1, /* 4: ICMP_SOURCEQUENCH */ 4337 ND_REDIRECT, /* 5: ICMP_REDIRECT */ 4338 -1, /* 6: UNUSED */ 4339 -1, /* 7: UNUSED */ 4340 ICMP6_ECHO_REQUEST, /* 8: ICMP_ECHO */ 4341 -1, /* 9: UNUSED */ 4342 -1, /* 10: UNUSED */ 4343 ICMP6_TIME_EXCEEDED, /* 11: ICMP_TIMXCEED */ 4344 ICMP6_PARAM_PROB, /* 12: ICMP_PARAMPROB */ 4345 -1, /* 13: ICMP_TSTAMP */ 4346 -1, /* 14: ICMP_TSTAMPREPLY */ 4347 -1, /* 15: ICMP_IREQ */ 4348 -1, /* 16: ICMP_IREQREPLY */ 4349 -1, /* 17: ICMP_MASKREQ */ 4350 -1, /* 18: ICMP_MASKREPLY */ 4351}; 4352 4353 4354int icmptoicmp6unreach[ICMP_MAX_UNREACH] = { 4355 ICMP6_DST_UNREACH_ADDR, /* 0: ICMP_UNREACH_NET */ 4356 ICMP6_DST_UNREACH_ADDR, /* 1: ICMP_UNREACH_HOST */ 4357 -1, /* 2: ICMP_UNREACH_PROTOCOL */ 4358 ICMP6_DST_UNREACH_NOPORT, /* 3: ICMP_UNREACH_PORT */ 4359 -1, /* 4: ICMP_UNREACH_NEEDFRAG */ 4360 ICMP6_DST_UNREACH_NOTNEIGHBOR, /* 5: ICMP_UNREACH_SRCFAIL */ 4361 ICMP6_DST_UNREACH_ADDR, /* 6: ICMP_UNREACH_NET_UNKNOWN */ 4362 ICMP6_DST_UNREACH_ADDR, /* 7: ICMP_UNREACH_HOST_UNKNOWN */ 4363 -1, /* 8: ICMP_UNREACH_ISOLATED */ 4364 ICMP6_DST_UNREACH_ADMIN, /* 9: ICMP_UNREACH_NET_PROHIB */ 4365 ICMP6_DST_UNREACH_ADMIN, /* 10: ICMP_UNREACH_HOST_PROHIB */ 4366 -1, /* 11: ICMP_UNREACH_TOSNET */ 4367 -1, /* 12: ICMP_UNREACH_TOSHOST */ 4368 ICMP6_DST_UNREACH_ADMIN, /* 13: ICMP_UNREACH_ADMIN_PROHIBIT */ 4369}; 4370int icmpreplytype6[ICMP6_MAXTYPE + 1]; 4371#endif 4372 4373int icmpreplytype4[ICMP_MAXTYPE + 1]; 4374 4375 4376/* ------------------------------------------------------------------------ */ 4377/* Function: ipf_matchicmpqueryreply */ 4378/* Returns: int - 1 if "icmp" is a valid reply to "ic" else 0. */ 4379/* Parameters: v(I) - IP protocol version (4 or 6) */ 4380/* ic(I) - ICMP information */ 4381/* icmp(I) - ICMP packet header */ 4382/* rev(I) - direction (0 = forward/1 = reverse) of packet */ 4383/* */ 4384/* Check if the ICMP packet defined by the header pointed to by icmp is a */ 4385/* reply to one as described by what's in ic. If it is a match, return 1, */ 4386/* else return 0 for no match. */ 4387/* ------------------------------------------------------------------------ */ 4388int 4389ipf_matchicmpqueryreply(v, ic, icmp, rev) 4390 int v; 4391 icmpinfo_t *ic; 4392 icmphdr_t *icmp; 4393 int rev; 4394{ 4395 int ictype; 4396 4397 ictype = ic->ici_type; 4398 4399 if (v == 4) { 4400 /* 4401 * If we matched its type on the way in, then when going out 4402 * it will still be the same type. 4403 */ 4404 if ((!rev && (icmp->icmp_type == ictype)) || 4405 (rev && (icmpreplytype4[ictype] == icmp->icmp_type))) { 4406 if (icmp->icmp_type != ICMP_ECHOREPLY) 4407 return 1; 4408 if (icmp->icmp_id == ic->ici_id) 4409 return 1; 4410 } 4411 } 4412#ifdef USE_INET6 4413 else if (v == 6) { 4414 if ((!rev && (icmp->icmp_type == ictype)) || 4415 (rev && (icmpreplytype6[ictype] == icmp->icmp_type))) { 4416 if (icmp->icmp_type != ICMP6_ECHO_REPLY) 4417 return 1; 4418 if (icmp->icmp_id == ic->ici_id) 4419 return 1; 4420 } 4421 } 4422#endif 4423 return 0; 4424} 4425 4426 4427/* ------------------------------------------------------------------------ */ 4428/* Function: ipf_rule_compare */ 4429/* Parameters: fr1(I) - first rule structure to compare */ 4430/* fr2(I) - second rule structure to compare */ 4431/* Returns: int - 0 == rules are the same, else mismatch */ 4432/* */ 4433/* Compare two rules and return 0 if they match or a number indicating */ 4434/* which of the individual checks failed. */ 4435/* ------------------------------------------------------------------------ */ 4436static int 4437ipf_rule_compare(frentry_t *fr1, frentry_t *fr2) 4438{ 4439 if (fr1->fr_cksum != fr2->fr_cksum) 4440 return 1; 4441 if (fr1->fr_size != fr2->fr_size) 4442 return 2; 4443 if (fr1->fr_dsize != fr2->fr_dsize) 4444 return 3; 4445 if (bcmp((char *)&fr1->fr_func, (char *)&fr2->fr_func, 4446 fr1->fr_size - offsetof(struct frentry, fr_func)) != 0) 4447 return 4; 4448 if (fr1->fr_data && !fr2->fr_data) 4449 return 5; 4450 if (!fr1->fr_data && fr2->fr_data) 4451 return 6; 4452 if (fr1->fr_data) { 4453 if (bcmp(fr1->fr_caddr, fr2->fr_caddr, fr1->fr_dsize)) 4454 return 7; 4455 } 4456 return 0; 4457} 4458 4459 4460/* ------------------------------------------------------------------------ */ 4461/* Function: frrequest */ 4462/* Returns: int - 0 == success, > 0 == errno value */ 4463/* Parameters: unit(I) - device for which this is for */ 4464/* req(I) - ioctl command (SIOC*) */ 4465/* data(I) - pointr to ioctl data */ 4466/* set(I) - 1 or 0 (filter set) */ 4467/* makecopy(I) - flag indicating whether data points to a rule */ 4468/* in kernel space & hence doesn't need copying. */ 4469/* */ 4470/* This function handles all the requests which operate on the list of */ 4471/* filter rules. This includes adding, deleting, insertion. It is also */ 4472/* responsible for creating groups when a "head" rule is loaded. Interface */ 4473/* names are resolved here and other sanity checks are made on the content */ 4474/* of the rule structure being loaded. If a rule has user defined timeouts */ 4475/* then make sure they are created and initialised before exiting. */ 4476/* ------------------------------------------------------------------------ */ 4477int 4478frrequest(softc, unit, req, data, set, makecopy) 4479 ipf_main_softc_t *softc; 4480 int unit; 4481 ioctlcmd_t req; 4482 int set, makecopy; 4483 caddr_t data; 4484{ 4485 int error = 0, in, family, addrem, need_free = 0; 4486 frentry_t frd, *fp, *f, **fprev, **ftail; 4487 void *ptr, *uptr, *cptr; 4488 u_int *p, *pp; 4489 frgroup_t *fg; 4490 char *group; 4491 4492 ptr = NULL; 4493 cptr = NULL; 4494 fg = NULL; 4495 fp = &frd; 4496 if (makecopy != 0) { 4497 bzero(fp, sizeof(frd)); 4498 error = ipf_inobj(softc, data, NULL, fp, IPFOBJ_FRENTRY); 4499 if (error) { 4500 return error; 4501 } 4502 if ((fp->fr_type & FR_T_BUILTIN) != 0) { 4503 IPFERROR(6); 4504 return EINVAL; 4505 } 4506 KMALLOCS(f, frentry_t *, fp->fr_size); 4507 if (f == NULL) { 4508 IPFERROR(131); 4509 return ENOMEM; 4510 } 4511 bzero(f, fp->fr_size); 4512 error = ipf_inobjsz(softc, data, f, IPFOBJ_FRENTRY, 4513 fp->fr_size); 4514 if (error) { 4515 KFREES(f, fp->fr_size); 4516 return error; 4517 } 4518 4519 fp = f; 4520 f = NULL; 4521 fp->fr_next = NULL; 4522 fp->fr_dnext = NULL; 4523 fp->fr_pnext = NULL; 4524 fp->fr_pdnext = NULL; 4525 fp->fr_grp = NULL; 4526 fp->fr_grphead = NULL; 4527 fp->fr_icmpgrp = NULL; 4528 fp->fr_isc = (void *)-1; 4529 fp->fr_ptr = NULL; 4530 fp->fr_ref = 0; 4531 fp->fr_flags |= FR_COPIED; 4532 } else { 4533 fp = (frentry_t *)data; 4534 if ((fp->fr_type & FR_T_BUILTIN) == 0) { 4535 IPFERROR(7); 4536 return EINVAL; 4537 } 4538 fp->fr_flags &= ~FR_COPIED; 4539 } 4540 4541 if (((fp->fr_dsize == 0) && (fp->fr_data != NULL)) || 4542 ((fp->fr_dsize != 0) && (fp->fr_data == NULL))) { 4543 IPFERROR(8); 4544 error = EINVAL; 4545 goto donenolock; 4546 } 4547 4548 family = fp->fr_family; 4549 uptr = fp->fr_data; 4550 4551 if (req == (ioctlcmd_t)SIOCINAFR || req == (ioctlcmd_t)SIOCINIFR || 4552 req == (ioctlcmd_t)SIOCADAFR || req == (ioctlcmd_t)SIOCADIFR) 4553 addrem = 0; 4554 else if (req == (ioctlcmd_t)SIOCRMAFR || req == (ioctlcmd_t)SIOCRMIFR) 4555 addrem = 1; 4556 else if (req == (ioctlcmd_t)SIOCZRLST) 4557 addrem = 2; 4558 else { 4559 IPFERROR(9); 4560 error = EINVAL; 4561 goto donenolock; 4562 } 4563 4564 /* 4565 * Only filter rules for IPv4 or IPv6 are accepted. 4566 */ 4567 if (family == AF_INET) { 4568 /*EMPTY*/; 4569#ifdef USE_INET6 4570 } else if (family == AF_INET6) { 4571 /*EMPTY*/; 4572#endif 4573 } else if (family != 0) { 4574 IPFERROR(10); 4575 error = EINVAL; 4576 goto donenolock; 4577 } 4578 4579 /* 4580 * If the rule is being loaded from user space, i.e. we had to copy it 4581 * into kernel space, then do not trust the function pointer in the 4582 * rule. 4583 */ 4584 if ((makecopy == 1) && (fp->fr_func != NULL)) { 4585 if (ipf_findfunc(fp->fr_func) == NULL) { 4586 IPFERROR(11); 4587 error = ESRCH; 4588 goto donenolock; 4589 } 4590 4591 if (addrem == 0) { 4592 error = ipf_funcinit(softc, fp); 4593 if (error != 0) 4594 goto donenolock; 4595 } 4596 } 4597 if ((fp->fr_flags & FR_CALLNOW) && 4598 ((fp->fr_func == NULL) || (fp->fr_func == (ipfunc_t)-1))) { 4599 IPFERROR(142); 4600 error = ESRCH; 4601 goto donenolock; 4602 } 4603 if (((fp->fr_flags & FR_CMDMASK) == FR_CALL) && 4604 ((fp->fr_func == NULL) || (fp->fr_func == (ipfunc_t)-1))) { 4605 IPFERROR(143); 4606 error = ESRCH; 4607 goto donenolock; 4608 } 4609 4610 ptr = NULL; 4611 cptr = NULL; 4612 4613 if (FR_ISACCOUNT(fp->fr_flags)) 4614 unit = IPL_LOGCOUNT; 4615 4616 /* 4617 * Check that each group name in the rule has a start index that 4618 * is valid. 4619 */ 4620 if (fp->fr_icmphead != -1) { 4621 if ((fp->fr_icmphead < 0) || 4622 (fp->fr_icmphead >= fp->fr_namelen)) { 4623 IPFERROR(136); 4624 error = EINVAL; 4625 goto donenolock; 4626 } 4627 if (!strcmp(FR_NAME(fp, fr_icmphead), "0")) 4628 fp->fr_names[fp->fr_icmphead] = '\0'; 4629 } 4630 4631 if (fp->fr_grhead != -1) { 4632 if ((fp->fr_grhead < 0) || 4633 (fp->fr_grhead >= fp->fr_namelen)) { 4634 IPFERROR(137); 4635 error = EINVAL; 4636 goto donenolock; 4637 } 4638 if (!strcmp(FR_NAME(fp, fr_grhead), "0")) 4639 fp->fr_names[fp->fr_grhead] = '\0'; 4640 } 4641 4642 if (fp->fr_group != -1) { 4643 if ((fp->fr_group < 0) || 4644 (fp->fr_group >= fp->fr_namelen)) { 4645 IPFERROR(138); 4646 error = EINVAL; 4647 goto donenolock; 4648 } 4649 if ((req != (int)SIOCZRLST) && (fp->fr_group != -1)) { 4650 /* 4651 * Allow loading rules that are in groups to cause 4652 * them to be created if they don't already exit. 4653 */ 4654 group = FR_NAME(fp, fr_group); 4655 if (addrem == 0) { 4656 fg = ipf_group_add(softc, group, NULL, 4657 fp->fr_flags, unit, set); 4658 fp->fr_grp = fg; 4659 } else { 4660 fg = ipf_findgroup(softc, group, unit, 4661 set, NULL); 4662 if (fg == NULL) { 4663 IPFERROR(12); 4664 error = ESRCH; 4665 goto donenolock; 4666 } 4667 } 4668 4669 if (fg->fg_flags == 0) { 4670 fg->fg_flags = fp->fr_flags & FR_INOUT; 4671 } else if (fg->fg_flags != (fp->fr_flags & FR_INOUT)) { 4672 IPFERROR(13); 4673 error = ESRCH; 4674 goto donenolock; 4675 } 4676 } 4677 } else { 4678 /* 4679 * If a rule is going to be part of a group then it does 4680 * not matter whether it is an in or out rule, but if it 4681 * isn't in a group, then it does... 4682 */ 4683 if ((fp->fr_flags & (FR_INQUE|FR_OUTQUE)) == 0) { 4684 IPFERROR(14); 4685 error = EINVAL; 4686 goto donenolock; 4687 } 4688 } 4689 in = (fp->fr_flags & FR_INQUE) ? 0 : 1; 4690 4691 /* 4692 * Work out which rule list this change is being applied to. 4693 */ 4694 ftail = NULL; 4695 fprev = NULL; 4696 if (unit == IPL_LOGAUTH) { 4697 if ((fp->fr_tifs[0].fd_ptr != NULL) || 4698 (fp->fr_tifs[1].fd_ptr != NULL) || 4699 (fp->fr_dif.fd_ptr != NULL) || 4700 (fp->fr_flags & FR_FASTROUTE)) { 4701 softc->ipf_interror = 145; 4702 error = EINVAL; 4703 goto donenolock; 4704 } 4705 fprev = ipf_auth_rulehead(softc); 4706 } else { 4707 if (FR_ISACCOUNT(fp->fr_flags)) 4708 fprev = &softc->ipf_acct[in][set]; 4709 else if ((fp->fr_flags & (FR_OUTQUE|FR_INQUE)) != 0) 4710 fprev = &softc->ipf_rules[in][set]; 4711 } 4712 if (fprev == NULL) { 4713 IPFERROR(15); 4714 error = ESRCH; 4715 goto donenolock; 4716 } 4717 4718 if (fg != NULL) 4719 fprev = &fg->fg_start; 4720 4721 /* 4722 * Copy in extra data for the rule. 4723 */ 4724 if (fp->fr_dsize != 0) { 4725 if (makecopy != 0) { 4726 KMALLOCS(ptr, void *, fp->fr_dsize); 4727 if (ptr == NULL) { 4728 IPFERROR(16); 4729 error = ENOMEM; 4730 goto donenolock; 4731 } 4732 4733 /* 4734 * The bcopy case is for when the data is appended 4735 * to the rule by ipf_in_compat(). 4736 */ 4737 if (uptr >= (void *)fp && 4738 uptr < (void *)((char *)fp + fp->fr_size)) { 4739 bcopy(uptr, ptr, fp->fr_dsize); 4740 error = 0; 4741 } else { 4742 error = COPYIN(uptr, ptr, fp->fr_dsize); 4743 if (error != 0) { 4744 IPFERROR(17); 4745 error = EFAULT; 4746 goto donenolock; 4747 } 4748 } 4749 } else { 4750 ptr = uptr; 4751 } 4752 fp->fr_data = ptr; 4753 } else { 4754 fp->fr_data = NULL; 4755 } 4756 4757 /* 4758 * Perform per-rule type sanity checks of their members. 4759 * All code after this needs to be aware that allocated memory 4760 * may need to be free'd before exiting. 4761 */ 4762 switch (fp->fr_type & ~FR_T_BUILTIN) 4763 { 4764#if defined(IPFILTER_BPF) 4765 case FR_T_BPFOPC : 4766 if (fp->fr_dsize == 0) { 4767 IPFERROR(19); 4768 error = EINVAL; 4769 break; 4770 } 4771 if (!bpf_validate(ptr, fp->fr_dsize/sizeof(struct bpf_insn))) { 4772 IPFERROR(20); 4773 error = EINVAL; 4774 break; 4775 } 4776 break; 4777#endif 4778 case FR_T_IPF : 4779 /* 4780 * Preparation for error case at the bottom of this function. 4781 */ 4782 if (fp->fr_datype == FRI_LOOKUP) 4783 fp->fr_dstptr = NULL; 4784 if (fp->fr_satype == FRI_LOOKUP) 4785 fp->fr_srcptr = NULL; 4786 4787 if (fp->fr_dsize != sizeof(fripf_t)) { 4788 IPFERROR(21); 4789 error = EINVAL; 4790 break; 4791 } 4792 4793 /* 4794 * Allowing a rule with both "keep state" and "with oow" is 4795 * pointless because adding a state entry to the table will 4796 * fail with the out of window (oow) flag set. 4797 */ 4798 if ((fp->fr_flags & FR_KEEPSTATE) && (fp->fr_flx & FI_OOW)) { 4799 IPFERROR(22); 4800 error = EINVAL; 4801 break; 4802 } 4803 4804 switch (fp->fr_satype) 4805 { 4806 case FRI_BROADCAST : 4807 case FRI_DYNAMIC : 4808 case FRI_NETWORK : 4809 case FRI_NETMASKED : 4810 case FRI_PEERADDR : 4811 if (fp->fr_sifpidx < 0) { 4812 IPFERROR(23); 4813 error = EINVAL; 4814 } 4815 break; 4816 case FRI_LOOKUP : 4817 fp->fr_srcptr = ipf_findlookup(softc, unit, fp, 4818 &fp->fr_src6, 4819 &fp->fr_smsk6); 4820 if (fp->fr_srcfunc == NULL) { 4821 IPFERROR(132); 4822 error = ESRCH; 4823 break; 4824 } 4825 break; 4826 case FRI_NORMAL : 4827 break; 4828 default : 4829 IPFERROR(133); 4830 error = EINVAL; 4831 break; 4832 } 4833 if (error != 0) 4834 break; 4835 4836 switch (fp->fr_datype) 4837 { 4838 case FRI_BROADCAST : 4839 case FRI_DYNAMIC : 4840 case FRI_NETWORK : 4841 case FRI_NETMASKED : 4842 case FRI_PEERADDR : 4843 if (fp->fr_difpidx < 0) { 4844 IPFERROR(24); 4845 error = EINVAL; 4846 } 4847 break; 4848 case FRI_LOOKUP : 4849 fp->fr_dstptr = ipf_findlookup(softc, unit, fp, 4850 &fp->fr_dst6, 4851 &fp->fr_dmsk6); 4852 if (fp->fr_dstfunc == NULL) { 4853 IPFERROR(134); 4854 error = ESRCH; 4855 } 4856 break; 4857 case FRI_NORMAL : 4858 break; 4859 default : 4860 IPFERROR(135); 4861 error = EINVAL; 4862 } 4863 break; 4864 4865 case FR_T_NONE : 4866 case FR_T_CALLFUNC : 4867 case FR_T_COMPIPF : 4868 break; 4869 4870 case FR_T_IPFEXPR : 4871 if (ipf_matcharray_verify(fp->fr_data, fp->fr_dsize) == -1) { 4872 IPFERROR(25); 4873 error = EINVAL; 4874 } 4875 break; 4876 4877 default : 4878 IPFERROR(26); 4879 error = EINVAL; 4880 break; 4881 } 4882 if (error != 0) 4883 goto donenolock; 4884 4885 if (fp->fr_tif.fd_name != -1) { 4886 if ((fp->fr_tif.fd_name < 0) || 4887 (fp->fr_tif.fd_name >= fp->fr_namelen)) { 4888 IPFERROR(139); 4889 error = EINVAL; 4890 goto donenolock; 4891 } 4892 } 4893 4894 if (fp->fr_dif.fd_name != -1) { 4895 if ((fp->fr_dif.fd_name < 0) || 4896 (fp->fr_dif.fd_name >= fp->fr_namelen)) { 4897 IPFERROR(140); 4898 error = EINVAL; 4899 goto donenolock; 4900 } 4901 } 4902 4903 if (fp->fr_rif.fd_name != -1) { 4904 if ((fp->fr_rif.fd_name < 0) || 4905 (fp->fr_rif.fd_name >= fp->fr_namelen)) { 4906 IPFERROR(141); 4907 error = EINVAL; 4908 goto donenolock; 4909 } 4910 } 4911 4912 /* 4913 * Lookup all the interface names that are part of the rule. 4914 */ 4915 error = ipf_synclist(softc, fp, NULL); 4916 if (error != 0) 4917 goto donenolock; 4918 fp->fr_statecnt = 0; 4919 if (fp->fr_srctrack.ht_max_nodes != 0) 4920 ipf_rb_ht_init(&fp->fr_srctrack); 4921 4922 /* 4923 * Look for an existing matching filter rule, but don't include the 4924 * next or interface pointer in the comparison (fr_next, fr_ifa). 4925 * This elminates rules which are indentical being loaded. Checksum 4926 * the constant part of the filter rule to make comparisons quicker 4927 * (this meaning no pointers are included). 4928 */ 4929 for (fp->fr_cksum = 0, p = (u_int *)&fp->fr_func, pp = &fp->fr_cksum; 4930 p < pp; p++) 4931 fp->fr_cksum += *p; 4932 pp = (u_int *)(fp->fr_caddr + fp->fr_dsize); 4933 for (p = (u_int *)fp->fr_data; p < pp; p++) 4934 fp->fr_cksum += *p; 4935 4936 WRITE_ENTER(&softc->ipf_mutex); 4937 4938 /* 4939 * Now that the filter rule lists are locked, we can walk the 4940 * chain of them without fear. 4941 */ 4942 ftail = fprev; 4943 for (f = *ftail; (f = *ftail) != NULL; ftail = &f->fr_next) { 4944 if (fp->fr_collect <= f->fr_collect) { 4945 ftail = fprev; 4946 f = NULL; 4947 break; 4948 } 4949 fprev = ftail; 4950 } 4951 4952 for (; (f = *ftail) != NULL; ftail = &f->fr_next) { 4953 if (ipf_rule_compare(fp, f) == 0) 4954 break; 4955 } 4956 4957 /* 4958 * If zero'ing statistics, copy current to caller and zero. 4959 */ 4960 if (addrem == 2) { 4961 if (f == NULL) { 4962 IPFERROR(27); 4963 error = ESRCH; 4964 } else { 4965 /* 4966 * Copy and reduce lock because of impending copyout. 4967 * Well we should, but if we do then the atomicity of 4968 * this call and the correctness of fr_hits and 4969 * fr_bytes cannot be guaranteed. As it is, this code 4970 * only resets them to 0 if they are successfully 4971 * copied out into user space. 4972 */ 4973 bcopy((char *)f, (char *)fp, f->fr_size); 4974 /* MUTEX_DOWNGRADE(&softc->ipf_mutex); */ 4975 4976 /* 4977 * When we copy this rule back out, set the data 4978 * pointer to be what it was in user space. 4979 */ 4980 fp->fr_data = uptr; 4981 error = ipf_outobj(softc, data, fp, IPFOBJ_FRENTRY); 4982 4983 if (error == 0) { 4984 if ((f->fr_dsize != 0) && (uptr != NULL)) { 4985 error = COPYOUT(f->fr_data, uptr, 4986 f->fr_dsize); 4987 if (error == 0) { 4988 f->fr_hits = 0; 4989 f->fr_bytes = 0; 4990 } else { 4991 IPFERROR(28); 4992 error = EFAULT; 4993 } 4994 } 4995 } 4996 } 4997 4998 if (makecopy != 0) { 4999 if (ptr != NULL) { 5000 KFREES(ptr, fp->fr_dsize); 5001 } 5002 KFREES(fp, fp->fr_size); 5003 } 5004 RWLOCK_EXIT(&softc->ipf_mutex); 5005 return error; 5006 } 5007 5008 if (f == NULL) { 5009 /* 5010 * At the end of this, ftail must point to the place where the 5011 * new rule is to be saved/inserted/added. 5012 * For SIOCAD*FR, this should be the last rule in the group of 5013 * rules that have equal fr_collect fields. 5014 * For SIOCIN*FR, ... 5015 */ 5016 if (req == (ioctlcmd_t)SIOCADAFR || 5017 req == (ioctlcmd_t)SIOCADIFR) { 5018 5019 for (ftail = fprev; (f = *ftail) != NULL; ) { 5020 if (f->fr_collect > fp->fr_collect) 5021 break; 5022 ftail = &f->fr_next; 5023 fprev = ftail; 5024 } 5025 ftail = fprev; 5026 f = NULL; 5027 ptr = NULL; 5028 } else if (req == (ioctlcmd_t)SIOCINAFR || 5029 req == (ioctlcmd_t)SIOCINIFR) { 5030 while ((f = *fprev) != NULL) { 5031 if (f->fr_collect >= fp->fr_collect) 5032 break; 5033 fprev = &f->fr_next; 5034 } 5035 ftail = fprev; 5036 if (fp->fr_hits != 0) { 5037 while (fp->fr_hits && (f = *ftail)) { 5038 if (f->fr_collect != fp->fr_collect) 5039 break; 5040 fprev = ftail; 5041 ftail = &f->fr_next; 5042 fp->fr_hits--; 5043 } 5044 } 5045 f = NULL; 5046 ptr = NULL; 5047 } 5048 } 5049 5050 /* 5051 * Request to remove a rule. 5052 */ 5053 if (addrem == 1) { 5054 if (f == NULL) { 5055 IPFERROR(29); 5056 error = ESRCH; 5057 } else { 5058 /* 5059 * Do not allow activity from user space to interfere 5060 * with rules not loaded that way. 5061 */ 5062 if ((makecopy == 1) && !(f->fr_flags & FR_COPIED)) { 5063 IPFERROR(30); 5064 error = EPERM; 5065 goto done; 5066 } 5067 5068 /* 5069 * Return EBUSY if the rule is being reference by 5070 * something else (eg state information.) 5071 */ 5072 if (f->fr_ref > 1) { 5073 IPFERROR(31); 5074 error = EBUSY; 5075 goto done; 5076 } 5077#ifdef IPFILTER_SCAN 5078 if (f->fr_isctag != -1 && 5079 (f->fr_isc != (struct ipscan *)-1)) 5080 ipf_scan_detachfr(f); 5081#endif 5082 5083 if (unit == IPL_LOGAUTH) { 5084 error = ipf_auth_precmd(softc, req, f, ftail); 5085 goto done; 5086 } 5087 5088 ipf_rule_delete(softc, f, unit, set); 5089 5090 need_free = makecopy; 5091 } 5092 } else { 5093 /* 5094 * Not removing, so we must be adding/inserting a rule. 5095 */ 5096 if (f != NULL) { 5097 IPFERROR(32); 5098 error = EEXIST; 5099 goto done; 5100 } 5101 if (unit == IPL_LOGAUTH) { 5102 error = ipf_auth_precmd(softc, req, fp, ftail); 5103 goto done; 5104 } 5105 5106 MUTEX_NUKE(&fp->fr_lock); 5107 MUTEX_INIT(&fp->fr_lock, "filter rule lock"); 5108 if (fp->fr_die != 0) 5109 ipf_rule_expire_insert(softc, fp, set); 5110 5111 fp->fr_hits = 0; 5112 if (makecopy != 0) 5113 fp->fr_ref = 1; 5114 fp->fr_pnext = ftail; 5115 fp->fr_next = *ftail; 5116 if (fp->fr_next != NULL) 5117 fp->fr_next->fr_pnext = &fp->fr_next; 5118 *ftail = fp; 5119 if (addrem == 0) 5120 ipf_fixskip(ftail, fp, 1); 5121 5122 fp->fr_icmpgrp = NULL; 5123 if (fp->fr_icmphead != -1) { 5124 group = FR_NAME(fp, fr_icmphead); 5125 fg = ipf_group_add(softc, group, fp, 0, unit, set); 5126 fp->fr_icmpgrp = fg; 5127 } 5128 5129 fp->fr_grphead = NULL; 5130 if (fp->fr_grhead != -1) { 5131 group = FR_NAME(fp, fr_grhead); 5132 fg = ipf_group_add(softc, group, fp, fp->fr_flags, 5133 unit, set); 5134 fp->fr_grphead = fg; 5135 } 5136 } 5137done: 5138 RWLOCK_EXIT(&softc->ipf_mutex); 5139donenolock: 5140 if (need_free || (error != 0)) { 5141 if ((fp->fr_type & ~FR_T_BUILTIN) == FR_T_IPF) { 5142 if ((fp->fr_satype == FRI_LOOKUP) && 5143 (fp->fr_srcptr != NULL)) 5144 ipf_lookup_deref(softc, fp->fr_srctype, 5145 fp->fr_srcptr); 5146 if ((fp->fr_datype == FRI_LOOKUP) && 5147 (fp->fr_dstptr != NULL)) 5148 ipf_lookup_deref(softc, fp->fr_dsttype, 5149 fp->fr_dstptr); 5150 } 5151 if (fp->fr_grp != NULL) { 5152 WRITE_ENTER(&softc->ipf_mutex); 5153 ipf_group_del(softc, fp->fr_grp, fp); 5154 RWLOCK_EXIT(&softc->ipf_mutex); 5155 } 5156 if ((ptr != NULL) && (makecopy != 0)) { 5157 KFREES(ptr, fp->fr_dsize); 5158 } 5159 KFREES(fp, fp->fr_size); 5160 } 5161 return (error); 5162} 5163 5164 5165/* ------------------------------------------------------------------------ */ 5166/* Function: ipf_rule_delete */ 5167/* Returns: Nil */ 5168/* Parameters: softc(I) - pointer to soft context main structure */ 5169/* f(I) - pointer to the rule being deleted */ 5170/* ftail(I) - pointer to the pointer to f */ 5171/* unit(I) - device for which this is for */ 5172/* set(I) - 1 or 0 (filter set) */ 5173/* */ 5174/* This function attempts to do what it can to delete a filter rule: remove */ 5175/* it from any linked lists and remove any groups it is responsible for. */ 5176/* But in the end, removing a rule can only drop the reference count - we */ 5177/* must use that as the guide for whether or not it can be freed. */ 5178/* ------------------------------------------------------------------------ */ 5179static void 5180ipf_rule_delete(softc, f, unit, set) 5181 ipf_main_softc_t *softc; 5182 frentry_t *f; 5183 int unit, set; 5184{ 5185 5186 /* 5187 * If fr_pdnext is set, then the rule is on the expire list, so 5188 * remove it from there. 5189 */ 5190 if (f->fr_pdnext != NULL) { 5191 *f->fr_pdnext = f->fr_dnext; 5192 if (f->fr_dnext != NULL) 5193 f->fr_dnext->fr_pdnext = f->fr_pdnext; 5194 f->fr_pdnext = NULL; 5195 f->fr_dnext = NULL; 5196 } 5197 5198 ipf_fixskip(f->fr_pnext, f, -1); 5199 if (f->fr_pnext != NULL) 5200 *f->fr_pnext = f->fr_next; 5201 if (f->fr_next != NULL) 5202 f->fr_next->fr_pnext = f->fr_pnext; 5203 f->fr_pnext = NULL; 5204 f->fr_next = NULL; 5205 5206 (void) ipf_derefrule(softc, &f); 5207} 5208 5209/* ------------------------------------------------------------------------ */ 5210/* Function: ipf_rule_expire_insert */ 5211/* Returns: Nil */ 5212/* Parameters: softc(I) - pointer to soft context main structure */ 5213/* f(I) - pointer to rule to be added to expire list */ 5214/* set(I) - 1 or 0 (filter set) */ 5215/* */ 5216/* If the new rule has a given expiration time, insert it into the list of */ 5217/* expiring rules with the ones to be removed first added to the front of */ 5218/* the list. The insertion is O(n) but it is kept sorted for quick scans at */ 5219/* expiration interval checks. */ 5220/* ------------------------------------------------------------------------ */ 5221static void 5222ipf_rule_expire_insert(softc, f, set) 5223 ipf_main_softc_t *softc; 5224 frentry_t *f; 5225 int set; 5226{ 5227 frentry_t *fr; 5228 5229 /* 5230 */ 5231 5232 f->fr_die = softc->ipf_ticks + IPF_TTLVAL(f->fr_die); 5233 for (fr = softc->ipf_rule_explist[set]; fr != NULL; 5234 fr = fr->fr_dnext) { 5235 if (f->fr_die < fr->fr_die) 5236 break; 5237 if (fr->fr_dnext == NULL) { 5238 /* 5239 * We've got to the last rule and everything 5240 * wanted to be expired before this new node, 5241 * so we have to tack it on the end... 5242 */ 5243 fr->fr_dnext = f; 5244 f->fr_pdnext = &fr->fr_dnext; 5245 fr = NULL; 5246 break; 5247 } 5248 } 5249 5250 if (softc->ipf_rule_explist[set] == NULL) { 5251 softc->ipf_rule_explist[set] = f; 5252 f->fr_pdnext = &softc->ipf_rule_explist[set]; 5253 } else if (fr != NULL) { 5254 f->fr_dnext = fr; 5255 f->fr_pdnext = fr->fr_pdnext; 5256 fr->fr_pdnext = &f->fr_dnext; 5257 } 5258} 5259 5260 5261/* ------------------------------------------------------------------------ */ 5262/* Function: ipf_findlookup */ 5263/* Returns: NULL = failure, else success */ 5264/* Parameters: softc(I) - pointer to soft context main structure */ 5265/* unit(I) - ipf device we want to find match for */ 5266/* fp(I) - rule for which lookup is for */ 5267/* addrp(I) - pointer to lookup information in address struct */ 5268/* maskp(O) - pointer to lookup information for storage */ 5269/* */ 5270/* When using pools and hash tables to store addresses for matching in */ 5271/* rules, it is necessary to resolve both the object referred to by the */ 5272/* name or address (and return that pointer) and also provide the means by */ 5273/* which to determine if an address belongs to that object to make the */ 5274/* packet matching quicker. */ 5275/* ------------------------------------------------------------------------ */ 5276static void * 5277ipf_findlookup(softc, unit, fr, addrp, maskp) 5278 ipf_main_softc_t *softc; 5279 int unit; 5280 frentry_t *fr; 5281 i6addr_t *addrp, *maskp; 5282{ 5283 void *ptr = NULL; 5284 5285 switch (addrp->iplookupsubtype) 5286 { 5287 case 0 : 5288 ptr = ipf_lookup_res_num(softc, unit, addrp->iplookuptype, 5289 addrp->iplookupnum, 5290 &maskp->iplookupfunc); 5291 break; 5292 case 1 : 5293 if (addrp->iplookupname < 0) 5294 break; 5295 if (addrp->iplookupname >= fr->fr_namelen) 5296 break; 5297 ptr = ipf_lookup_res_name(softc, unit, addrp->iplookuptype, 5298 fr->fr_names + addrp->iplookupname, 5299 &maskp->iplookupfunc); 5300 break; 5301 default : 5302 break; 5303 } 5304 5305 return ptr; 5306} 5307 5308 5309/* ------------------------------------------------------------------------ */ 5310/* Function: ipf_funcinit */ 5311/* Returns: int - 0 == success, else ESRCH: cannot resolve rule details */ 5312/* Parameters: softc(I) - pointer to soft context main structure */ 5313/* fr(I) - pointer to filter rule */ 5314/* */ 5315/* If a rule is a call rule, then check if the function it points to needs */ 5316/* an init function to be called now the rule has been loaded. */ 5317/* ------------------------------------------------------------------------ */ 5318static int 5319ipf_funcinit(softc, fr) 5320 ipf_main_softc_t *softc; 5321 frentry_t *fr; 5322{ 5323 ipfunc_resolve_t *ft; 5324 int err; 5325 5326 IPFERROR(34); 5327 err = ESRCH; 5328 5329 for (ft = ipf_availfuncs; ft->ipfu_addr != NULL; ft++) 5330 if (ft->ipfu_addr == fr->fr_func) { 5331 err = 0; 5332 if (ft->ipfu_init != NULL) 5333 err = (*ft->ipfu_init)(softc, fr); 5334 break; 5335 } 5336 return err; 5337} 5338 5339 5340/* ------------------------------------------------------------------------ */ 5341/* Function: ipf_funcfini */ 5342/* Returns: Nil */ 5343/* Parameters: softc(I) - pointer to soft context main structure */ 5344/* fr(I) - pointer to filter rule */ 5345/* */ 5346/* For a given filter rule, call the matching "fini" function if the rule */ 5347/* is using a known function that would have resulted in the "init" being */ 5348/* called for ealier. */ 5349/* ------------------------------------------------------------------------ */ 5350static void 5351ipf_funcfini(softc, fr) 5352 ipf_main_softc_t *softc; 5353 frentry_t *fr; 5354{ 5355 ipfunc_resolve_t *ft; 5356 5357 for (ft = ipf_availfuncs; ft->ipfu_addr != NULL; ft++) 5358 if (ft->ipfu_addr == fr->fr_func) { 5359 if (ft->ipfu_fini != NULL) 5360 (void) (*ft->ipfu_fini)(softc, fr); 5361 break; 5362 } 5363} 5364 5365 5366/* ------------------------------------------------------------------------ */ 5367/* Function: ipf_findfunc */ 5368/* Returns: ipfunc_t - pointer to function if found, else NULL */ 5369/* Parameters: funcptr(I) - function pointer to lookup */ 5370/* */ 5371/* Look for a function in the table of known functions. */ 5372/* ------------------------------------------------------------------------ */ 5373static ipfunc_t 5374ipf_findfunc(funcptr) 5375 ipfunc_t funcptr; 5376{ 5377 ipfunc_resolve_t *ft; 5378 5379 for (ft = ipf_availfuncs; ft->ipfu_addr != NULL; ft++) 5380 if (ft->ipfu_addr == funcptr) 5381 return funcptr; 5382 return NULL; 5383} 5384 5385 5386/* ------------------------------------------------------------------------ */ 5387/* Function: ipf_resolvefunc */ 5388/* Returns: int - 0 == success, else error */ 5389/* Parameters: data(IO) - ioctl data pointer to ipfunc_resolve_t struct */ 5390/* */ 5391/* Copy in a ipfunc_resolve_t structure and then fill in the missing field. */ 5392/* This will either be the function name (if the pointer is set) or the */ 5393/* function pointer if the name is set. When found, fill in the other one */ 5394/* so that the entire, complete, structure can be copied back to user space.*/ 5395/* ------------------------------------------------------------------------ */ 5396int 5397ipf_resolvefunc(softc, data) 5398 ipf_main_softc_t *softc; 5399 void *data; 5400{ 5401 ipfunc_resolve_t res, *ft; 5402 int error; 5403 5404 error = BCOPYIN(data, &res, sizeof(res)); 5405 if (error != 0) { 5406 IPFERROR(123); 5407 return EFAULT; 5408 } 5409 5410 if (res.ipfu_addr == NULL && res.ipfu_name[0] != '\0') { 5411 for (ft = ipf_availfuncs; ft->ipfu_addr != NULL; ft++) 5412 if (strncmp(res.ipfu_name, ft->ipfu_name, 5413 sizeof(res.ipfu_name)) == 0) { 5414 res.ipfu_addr = ft->ipfu_addr; 5415 res.ipfu_init = ft->ipfu_init; 5416 if (COPYOUT(&res, data, sizeof(res)) != 0) { 5417 IPFERROR(35); 5418 return EFAULT; 5419 } 5420 return 0; 5421 } 5422 } 5423 if (res.ipfu_addr != NULL && res.ipfu_name[0] == '\0') { 5424 for (ft = ipf_availfuncs; ft->ipfu_addr != NULL; ft++) 5425 if (ft->ipfu_addr == res.ipfu_addr) { 5426 (void) strncpy(res.ipfu_name, ft->ipfu_name, 5427 sizeof(res.ipfu_name)); 5428 res.ipfu_init = ft->ipfu_init; 5429 if (COPYOUT(&res, data, sizeof(res)) != 0) { 5430 IPFERROR(36); 5431 return EFAULT; 5432 } 5433 return 0; 5434 } 5435 } 5436 IPFERROR(37); 5437 return ESRCH; 5438} 5439 5440 5441#if !defined(_KERNEL) || SOLARIS 5442/* 5443 * From: NetBSD 5444 * ppsratecheck(): packets (or events) per second limitation. 5445 */ 5446int 5447ppsratecheck(lasttime, curpps, maxpps) 5448 struct timeval *lasttime; 5449 int *curpps; 5450 int maxpps; /* maximum pps allowed */ 5451{ 5452 struct timeval tv, delta; 5453 int rv; 5454 5455 GETKTIME(&tv); 5456 5457 delta.tv_sec = tv.tv_sec - lasttime->tv_sec; 5458 delta.tv_usec = tv.tv_usec - lasttime->tv_usec; 5459 if (delta.tv_usec < 0) { 5460 delta.tv_sec--; 5461 delta.tv_usec += 1000000; 5462 } 5463 5464 /* 5465 * check for 0,0 is so that the message will be seen at least once. 5466 * if more than one second have passed since the last update of 5467 * lasttime, reset the counter. 5468 * 5469 * we do increment *curpps even in *curpps < maxpps case, as some may 5470 * try to use *curpps for stat purposes as well. 5471 */ 5472 if ((lasttime->tv_sec == 0 && lasttime->tv_usec == 0) || 5473 delta.tv_sec >= 1) { 5474 *lasttime = tv; 5475 *curpps = 0; 5476 rv = 1; 5477 } else if (maxpps < 0) 5478 rv = 1; 5479 else if (*curpps < maxpps) 5480 rv = 1; 5481 else 5482 rv = 0; 5483 *curpps = *curpps + 1; 5484 5485 return (rv); 5486} 5487#endif 5488 5489 5490/* ------------------------------------------------------------------------ */ 5491/* Function: ipf_derefrule */ 5492/* Returns: int - 0 == rule freed up, else rule not freed */ 5493/* Parameters: fr(I) - pointer to filter rule */ 5494/* */ 5495/* Decrement the reference counter to a rule by one. If it reaches zero, */ 5496/* free it and any associated storage space being used by it. */ 5497/* ------------------------------------------------------------------------ */ 5498int 5499ipf_derefrule(softc, frp) 5500 ipf_main_softc_t *softc; 5501 frentry_t **frp; 5502{ 5503 frentry_t *fr; 5504 frdest_t *fdp; 5505 5506 fr = *frp; 5507 *frp = NULL; 5508 5509 MUTEX_ENTER(&fr->fr_lock); 5510 fr->fr_ref--; 5511 if (fr->fr_ref == 0) { 5512 MUTEX_EXIT(&fr->fr_lock); 5513 MUTEX_DESTROY(&fr->fr_lock); 5514 5515 ipf_funcfini(softc, fr); 5516 5517 fdp = &fr->fr_tif; 5518 if (fdp->fd_type == FRD_DSTLIST) 5519 ipf_lookup_deref(softc, IPLT_DSTLIST, fdp->fd_ptr); 5520 5521 fdp = &fr->fr_rif; 5522 if (fdp->fd_type == FRD_DSTLIST) 5523 ipf_lookup_deref(softc, IPLT_DSTLIST, fdp->fd_ptr); 5524 5525 fdp = &fr->fr_dif; 5526 if (fdp->fd_type == FRD_DSTLIST) 5527 ipf_lookup_deref(softc, IPLT_DSTLIST, fdp->fd_ptr); 5528 5529 if ((fr->fr_type & ~FR_T_BUILTIN) == FR_T_IPF && 5530 fr->fr_satype == FRI_LOOKUP) 5531 ipf_lookup_deref(softc, fr->fr_srctype, fr->fr_srcptr); 5532 if ((fr->fr_type & ~FR_T_BUILTIN) == FR_T_IPF && 5533 fr->fr_datype == FRI_LOOKUP) 5534 ipf_lookup_deref(softc, fr->fr_dsttype, fr->fr_dstptr); 5535 5536 if (fr->fr_grp != NULL) 5537 ipf_group_del(softc, fr->fr_grp, fr); 5538 5539 if (fr->fr_grphead != NULL) 5540 ipf_group_del(softc, fr->fr_grphead, fr); 5541 5542 if (fr->fr_icmpgrp != NULL) 5543 ipf_group_del(softc, fr->fr_icmpgrp, fr); 5544 5545 if ((fr->fr_flags & FR_COPIED) != 0) { 5546 if (fr->fr_dsize) { 5547 KFREES(fr->fr_data, fr->fr_dsize); 5548 } 5549 KFREES(fr, fr->fr_size); 5550 return 0; 5551 } 5552 return 1; 5553 } else { 5554 MUTEX_EXIT(&fr->fr_lock); 5555 } 5556 return -1; 5557} 5558 5559 5560/* ------------------------------------------------------------------------ */ 5561/* Function: ipf_grpmapinit */ 5562/* Returns: int - 0 == success, else ESRCH because table entry not found*/ 5563/* Parameters: fr(I) - pointer to rule to find hash table for */ 5564/* */ 5565/* Looks for group hash table fr_arg and stores a pointer to it in fr_ptr. */ 5566/* fr_ptr is later used by ipf_srcgrpmap and ipf_dstgrpmap. */ 5567/* ------------------------------------------------------------------------ */ 5568static int 5569ipf_grpmapinit(softc, fr) 5570 ipf_main_softc_t *softc; 5571 frentry_t *fr; 5572{ 5573 char name[FR_GROUPLEN]; 5574 iphtable_t *iph; 5575 5576#if defined(SNPRINTF) && defined(_KERNEL) 5577 SNPRINTF(name, sizeof(name), "%d", fr->fr_arg); 5578#else 5579 (void) sprintf(name, "%d", fr->fr_arg); 5580#endif 5581 iph = ipf_lookup_find_htable(softc, IPL_LOGIPF, name); 5582 if (iph == NULL) { 5583 IPFERROR(38); 5584 return ESRCH; 5585 } 5586 if ((iph->iph_flags & FR_INOUT) != (fr->fr_flags & FR_INOUT)) { 5587 IPFERROR(39); 5588 return ESRCH; 5589 } 5590 iph->iph_ref++; 5591 fr->fr_ptr = iph; 5592 return 0; 5593} 5594 5595 5596/* ------------------------------------------------------------------------ */ 5597/* Function: ipf_grpmapfini */ 5598/* Returns: int - 0 == success, else ESRCH because table entry not found*/ 5599/* Parameters: softc(I) - pointer to soft context main structure */ 5600/* fr(I) - pointer to rule to release hash table for */ 5601/* */ 5602/* For rules that have had ipf_grpmapinit called, ipf_lookup_deref needs to */ 5603/* be called to undo what ipf_grpmapinit caused to be done. */ 5604/* ------------------------------------------------------------------------ */ 5605static int 5606ipf_grpmapfini(softc, fr) 5607 ipf_main_softc_t *softc; 5608 frentry_t *fr; 5609{ 5610 iphtable_t *iph; 5611 iph = fr->fr_ptr; 5612 if (iph != NULL) 5613 ipf_lookup_deref(softc, IPLT_HASH, iph); 5614 return 0; 5615} 5616 5617 5618/* ------------------------------------------------------------------------ */ 5619/* Function: ipf_srcgrpmap */ 5620/* Returns: frentry_t * - pointer to "new last matching" rule or NULL */ 5621/* Parameters: fin(I) - pointer to packet information */ 5622/* passp(IO) - pointer to current/new filter decision (unused) */ 5623/* */ 5624/* Look for a rule group head in a hash table, using the source address as */ 5625/* the key, and descend into that group and continue matching rules against */ 5626/* the packet. */ 5627/* ------------------------------------------------------------------------ */ 5628frentry_t * 5629ipf_srcgrpmap(fin, passp) 5630 fr_info_t *fin; 5631 u_32_t *passp; 5632{ 5633 frgroup_t *fg; 5634 void *rval; 5635 5636 rval = ipf_iphmfindgroup(fin->fin_main_soft, fin->fin_fr->fr_ptr, 5637 &fin->fin_src); 5638 if (rval == NULL) 5639 return NULL; 5640 5641 fg = rval; 5642 fin->fin_fr = fg->fg_start; 5643 (void) ipf_scanlist(fin, *passp); 5644 return fin->fin_fr; 5645} 5646 5647 5648/* ------------------------------------------------------------------------ */ 5649/* Function: ipf_dstgrpmap */ 5650/* Returns: frentry_t * - pointer to "new last matching" rule or NULL */ 5651/* Parameters: fin(I) - pointer to packet information */ 5652/* passp(IO) - pointer to current/new filter decision (unused) */ 5653/* */ 5654/* Look for a rule group head in a hash table, using the destination */ 5655/* address as the key, and descend into that group and continue matching */ 5656/* rules against the packet. */ 5657/* ------------------------------------------------------------------------ */ 5658frentry_t * 5659ipf_dstgrpmap(fin, passp) 5660 fr_info_t *fin; 5661 u_32_t *passp; 5662{ 5663 frgroup_t *fg; 5664 void *rval; 5665 5666 rval = ipf_iphmfindgroup(fin->fin_main_soft, fin->fin_fr->fr_ptr, 5667 &fin->fin_dst); 5668 if (rval == NULL) 5669 return NULL; 5670 5671 fg = rval; 5672 fin->fin_fr = fg->fg_start; 5673 (void) ipf_scanlist(fin, *passp); 5674 return fin->fin_fr; 5675} 5676 5677/* 5678 * Queue functions 5679 * =============== 5680 * These functions manage objects on queues for efficient timeouts. There 5681 * are a number of system defined queues as well as user defined timeouts. 5682 * It is expected that a lock is held in the domain in which the queue 5683 * belongs (i.e. either state or NAT) when calling any of these functions 5684 * that prevents ipf_freetimeoutqueue() from being called at the same time 5685 * as any other. 5686 */ 5687 5688 5689/* ------------------------------------------------------------------------ */ 5690/* Function: ipf_addtimeoutqueue */ 5691/* Returns: struct ifqtq * - NULL if malloc fails, else pointer to */ 5692/* timeout queue with given interval. */ 5693/* Parameters: parent(I) - pointer to pointer to parent node of this list */ 5694/* of interface queues. */ 5695/* seconds(I) - timeout value in seconds for this queue. */ 5696/* */ 5697/* This routine first looks for a timeout queue that matches the interval */ 5698/* being requested. If it finds one, increments the reference counter and */ 5699/* returns a pointer to it. If none are found, it allocates a new one and */ 5700/* inserts it at the top of the list. */ 5701/* */ 5702/* Locking. */ 5703/* It is assumed that the caller of this function has an appropriate lock */ 5704/* held (exclusively) in the domain that encompases 'parent'. */ 5705/* ------------------------------------------------------------------------ */ 5706ipftq_t * 5707ipf_addtimeoutqueue(softc, parent, seconds) 5708 ipf_main_softc_t *softc; 5709 ipftq_t **parent; 5710 u_int seconds; 5711{ 5712 ipftq_t *ifq; 5713 u_int period; 5714 5715 period = seconds * IPF_HZ_DIVIDE; 5716 5717 MUTEX_ENTER(&softc->ipf_timeoutlock); 5718 for (ifq = *parent; ifq != NULL; ifq = ifq->ifq_next) { 5719 if (ifq->ifq_ttl == period) { 5720 /* 5721 * Reset the delete flag, if set, so the structure 5722 * gets reused rather than freed and reallocated. 5723 */ 5724 MUTEX_ENTER(&ifq->ifq_lock); 5725 ifq->ifq_flags &= ~IFQF_DELETE; 5726 ifq->ifq_ref++; 5727 MUTEX_EXIT(&ifq->ifq_lock); 5728 MUTEX_EXIT(&softc->ipf_timeoutlock); 5729 5730 return ifq; 5731 } 5732 } 5733 5734 KMALLOC(ifq, ipftq_t *); 5735 if (ifq != NULL) { 5736 MUTEX_NUKE(&ifq->ifq_lock); 5737 IPFTQ_INIT(ifq, period, "ipftq mutex"); 5738 ifq->ifq_next = *parent; 5739 ifq->ifq_pnext = parent; 5740 ifq->ifq_flags = IFQF_USER; 5741 ifq->ifq_ref++; 5742 *parent = ifq; 5743 softc->ipf_userifqs++; 5744 } 5745 MUTEX_EXIT(&softc->ipf_timeoutlock); 5746 return ifq; 5747} 5748 5749 5750/* ------------------------------------------------------------------------ */ 5751/* Function: ipf_deletetimeoutqueue */ 5752/* Returns: int - new reference count value of the timeout queue */ 5753/* Parameters: ifq(I) - timeout queue which is losing a reference. */ 5754/* Locks: ifq->ifq_lock */ 5755/* */ 5756/* This routine must be called when we're discarding a pointer to a timeout */ 5757/* queue object, taking care of the reference counter. */ 5758/* */ 5759/* Now that this just sets a DELETE flag, it requires the expire code to */ 5760/* check the list of user defined timeout queues and call the free function */ 5761/* below (currently commented out) to stop memory leaking. It is done this */ 5762/* way because the locking may not be sufficient to safely do a free when */ 5763/* this function is called. */ 5764/* ------------------------------------------------------------------------ */ 5765int 5766ipf_deletetimeoutqueue(ifq) 5767 ipftq_t *ifq; 5768{ 5769 5770 ifq->ifq_ref--; 5771 if ((ifq->ifq_ref == 0) && ((ifq->ifq_flags & IFQF_USER) != 0)) { 5772 ifq->ifq_flags |= IFQF_DELETE; 5773 } 5774 5775 return ifq->ifq_ref; 5776} 5777 5778 5779/* ------------------------------------------------------------------------ */ 5780/* Function: ipf_freetimeoutqueue */ 5781/* Parameters: ifq(I) - timeout queue which is losing a reference. */ 5782/* Returns: Nil */ 5783/* */ 5784/* Locking: */ 5785/* It is assumed that the caller of this function has an appropriate lock */ 5786/* held (exclusively) in the domain that encompases the callers "domain". */ 5787/* The ifq_lock for this structure should not be held. */ 5788/* */ 5789/* Remove a user defined timeout queue from the list of queues it is in and */ 5790/* tidy up after this is done. */ 5791/* ------------------------------------------------------------------------ */ 5792void 5793ipf_freetimeoutqueue(softc, ifq) 5794 ipf_main_softc_t *softc; 5795 ipftq_t *ifq; 5796{ 5797 5798 if (((ifq->ifq_flags & IFQF_DELETE) == 0) || (ifq->ifq_ref != 0) || 5799 ((ifq->ifq_flags & IFQF_USER) == 0)) { 5800 printf("ipf_freetimeoutqueue(%lx) flags 0x%x ttl %d ref %d\n", 5801 (u_long)ifq, ifq->ifq_flags, ifq->ifq_ttl, 5802 ifq->ifq_ref); 5803 return; 5804 } 5805 5806 /* 5807 * Remove from its position in the list. 5808 */ 5809 *ifq->ifq_pnext = ifq->ifq_next; 5810 if (ifq->ifq_next != NULL) 5811 ifq->ifq_next->ifq_pnext = ifq->ifq_pnext; 5812 ifq->ifq_next = NULL; 5813 ifq->ifq_pnext = NULL; 5814 5815 MUTEX_DESTROY(&ifq->ifq_lock); 5816 ATOMIC_DEC(softc->ipf_userifqs); 5817 KFREE(ifq); 5818} 5819 5820 5821/* ------------------------------------------------------------------------ */ 5822/* Function: ipf_deletequeueentry */ 5823/* Returns: Nil */ 5824/* Parameters: tqe(I) - timeout queue entry to delete */ 5825/* */ 5826/* Remove a tail queue entry from its queue and make it an orphan. */ 5827/* ipf_deletetimeoutqueue is called to make sure the reference count on the */ 5828/* queue is correct. We can't, however, call ipf_freetimeoutqueue because */ 5829/* the correct lock(s) may not be held that would make it safe to do so. */ 5830/* ------------------------------------------------------------------------ */ 5831void 5832ipf_deletequeueentry(tqe) 5833 ipftqent_t *tqe; 5834{ 5835 ipftq_t *ifq; 5836 5837 ifq = tqe->tqe_ifq; 5838 5839 MUTEX_ENTER(&ifq->ifq_lock); 5840 5841 if (tqe->tqe_pnext != NULL) { 5842 *tqe->tqe_pnext = tqe->tqe_next; 5843 if (tqe->tqe_next != NULL) 5844 tqe->tqe_next->tqe_pnext = tqe->tqe_pnext; 5845 else /* we must be the tail anyway */ 5846 ifq->ifq_tail = tqe->tqe_pnext; 5847 5848 tqe->tqe_pnext = NULL; 5849 tqe->tqe_ifq = NULL; 5850 } 5851 5852 (void) ipf_deletetimeoutqueue(ifq); 5853 ASSERT(ifq->ifq_ref > 0); 5854 5855 MUTEX_EXIT(&ifq->ifq_lock); 5856} 5857 5858 5859/* ------------------------------------------------------------------------ */ 5860/* Function: ipf_queuefront */ 5861/* Returns: Nil */ 5862/* Parameters: tqe(I) - pointer to timeout queue entry */ 5863/* */ 5864/* Move a queue entry to the front of the queue, if it isn't already there. */ 5865/* ------------------------------------------------------------------------ */ 5866void 5867ipf_queuefront(tqe) 5868 ipftqent_t *tqe; 5869{ 5870 ipftq_t *ifq; 5871 5872 ifq = tqe->tqe_ifq; 5873 if (ifq == NULL) 5874 return; 5875 5876 MUTEX_ENTER(&ifq->ifq_lock); 5877 if (ifq->ifq_head != tqe) { 5878 *tqe->tqe_pnext = tqe->tqe_next; 5879 if (tqe->tqe_next) 5880 tqe->tqe_next->tqe_pnext = tqe->tqe_pnext; 5881 else 5882 ifq->ifq_tail = tqe->tqe_pnext; 5883 5884 tqe->tqe_next = ifq->ifq_head; 5885 ifq->ifq_head->tqe_pnext = &tqe->tqe_next; 5886 ifq->ifq_head = tqe; 5887 tqe->tqe_pnext = &ifq->ifq_head; 5888 } 5889 MUTEX_EXIT(&ifq->ifq_lock); 5890} 5891 5892 5893/* ------------------------------------------------------------------------ */ 5894/* Function: ipf_queueback */ 5895/* Returns: Nil */ 5896/* Parameters: ticks(I) - ipf tick time to use with this call */ 5897/* tqe(I) - pointer to timeout queue entry */ 5898/* */ 5899/* Move a queue entry to the back of the queue, if it isn't already there. */ 5900/* We use use ticks to calculate the expiration and mark for when we last */ 5901/* touched the structure. */ 5902/* ------------------------------------------------------------------------ */ 5903void 5904ipf_queueback(ticks, tqe) 5905 u_long ticks; 5906 ipftqent_t *tqe; 5907{ 5908 ipftq_t *ifq; 5909 5910 ifq = tqe->tqe_ifq; 5911 if (ifq == NULL) 5912 return; 5913 tqe->tqe_die = ticks + ifq->ifq_ttl; 5914 tqe->tqe_touched = ticks; 5915 5916 MUTEX_ENTER(&ifq->ifq_lock); 5917 if (tqe->tqe_next != NULL) { /* at the end already ? */ 5918 /* 5919 * Remove from list 5920 */ 5921 *tqe->tqe_pnext = tqe->tqe_next; 5922 tqe->tqe_next->tqe_pnext = tqe->tqe_pnext; 5923 5924 /* 5925 * Make it the last entry. 5926 */ 5927 tqe->tqe_next = NULL; 5928 tqe->tqe_pnext = ifq->ifq_tail; 5929 *ifq->ifq_tail = tqe; 5930 ifq->ifq_tail = &tqe->tqe_next; 5931 } 5932 MUTEX_EXIT(&ifq->ifq_lock); 5933} 5934 5935 5936/* ------------------------------------------------------------------------ */ 5937/* Function: ipf_queueappend */ 5938/* Returns: Nil */ 5939/* Parameters: ticks(I) - ipf tick time to use with this call */ 5940/* tqe(I) - pointer to timeout queue entry */ 5941/* ifq(I) - pointer to timeout queue */ 5942/* parent(I) - owing object pointer */ 5943/* */ 5944/* Add a new item to this queue and put it on the very end. */ 5945/* We use use ticks to calculate the expiration and mark for when we last */ 5946/* touched the structure. */ 5947/* ------------------------------------------------------------------------ */ 5948void 5949ipf_queueappend(ticks, tqe, ifq, parent) 5950 u_long ticks; 5951 ipftqent_t *tqe; 5952 ipftq_t *ifq; 5953 void *parent; 5954{ 5955 5956 MUTEX_ENTER(&ifq->ifq_lock); 5957 tqe->tqe_parent = parent; 5958 tqe->tqe_pnext = ifq->ifq_tail; 5959 *ifq->ifq_tail = tqe; 5960 ifq->ifq_tail = &tqe->tqe_next; 5961 tqe->tqe_next = NULL; 5962 tqe->tqe_ifq = ifq; 5963 tqe->tqe_die = ticks + ifq->ifq_ttl; 5964 tqe->tqe_touched = ticks; 5965 ifq->ifq_ref++; 5966 MUTEX_EXIT(&ifq->ifq_lock); 5967} 5968 5969 5970/* ------------------------------------------------------------------------ */ 5971/* Function: ipf_movequeue */ 5972/* Returns: Nil */ 5973/* Parameters: tq(I) - pointer to timeout queue information */ 5974/* oifp(I) - old timeout queue entry was on */ 5975/* nifp(I) - new timeout queue to put entry on */ 5976/* */ 5977/* Move a queue entry from one timeout queue to another timeout queue. */ 5978/* If it notices that the current entry is already last and does not need */ 5979/* to move queue, the return. */ 5980/* ------------------------------------------------------------------------ */ 5981void 5982ipf_movequeue(ticks, tqe, oifq, nifq) 5983 u_long ticks; 5984 ipftqent_t *tqe; 5985 ipftq_t *oifq, *nifq; 5986{ 5987 5988 /* 5989 * If the queue hasn't changed and we last touched this entry at the 5990 * same ipf time, then we're not going to achieve anything by either 5991 * changing the ttl or moving it on the queue. 5992 */ 5993 if (oifq == nifq && tqe->tqe_touched == ticks) 5994 return; 5995 5996 /* 5997 * For any of this to be outside the lock, there is a risk that two 5998 * packets entering simultaneously, with one changing to a different 5999 * queue and one not, could end up with things in a bizarre state. 6000 */ 6001 MUTEX_ENTER(&oifq->ifq_lock); 6002 6003 tqe->tqe_touched = ticks; 6004 tqe->tqe_die = ticks + nifq->ifq_ttl; 6005 /* 6006 * Is the operation here going to be a no-op ? 6007 */ 6008 if (oifq == nifq) { 6009 if ((tqe->tqe_next == NULL) || 6010 (tqe->tqe_next->tqe_die == tqe->tqe_die)) { 6011 MUTEX_EXIT(&oifq->ifq_lock); 6012 return; 6013 } 6014 } 6015 6016 /* 6017 * Remove from the old queue 6018 */ 6019 *tqe->tqe_pnext = tqe->tqe_next; 6020 if (tqe->tqe_next) 6021 tqe->tqe_next->tqe_pnext = tqe->tqe_pnext; 6022 else 6023 oifq->ifq_tail = tqe->tqe_pnext; 6024 tqe->tqe_next = NULL; 6025 6026 /* 6027 * If we're moving from one queue to another, release the 6028 * lock on the old queue and get a lock on the new queue. 6029 * For user defined queues, if we're moving off it, call 6030 * delete in case it can now be freed. 6031 */ 6032 if (oifq != nifq) { 6033 tqe->tqe_ifq = NULL; 6034 6035 (void) ipf_deletetimeoutqueue(oifq); 6036 6037 MUTEX_EXIT(&oifq->ifq_lock); 6038 6039 MUTEX_ENTER(&nifq->ifq_lock); 6040 6041 tqe->tqe_ifq = nifq; 6042 nifq->ifq_ref++; 6043 } 6044 6045 /* 6046 * Add to the bottom of the new queue 6047 */ 6048 tqe->tqe_pnext = nifq->ifq_tail; 6049 *nifq->ifq_tail = tqe; 6050 nifq->ifq_tail = &tqe->tqe_next; 6051 MUTEX_EXIT(&nifq->ifq_lock); 6052} 6053 6054 6055/* ------------------------------------------------------------------------ */ 6056/* Function: ipf_updateipid */ 6057/* Returns: int - 0 == success, -1 == error (packet should be droppped) */ 6058/* Parameters: fin(I) - pointer to packet information */ 6059/* */ 6060/* When we are doing NAT, change the IP of every packet to represent a */ 6061/* single sequence of packets coming from the host, hiding any host */ 6062/* specific sequencing that might otherwise be revealed. If the packet is */ 6063/* a fragment, then store the 'new' IPid in the fragment cache and look up */ 6064/* the fragment cache for non-leading fragments. If a non-leading fragment */ 6065/* has no match in the cache, return an error. */ 6066/* ------------------------------------------------------------------------ */ 6067static int 6068ipf_updateipid(fin) 6069 fr_info_t *fin; 6070{ 6071 u_short id, ido, sums; 6072 u_32_t sumd, sum; 6073 ip_t *ip; 6074 6075 ip = fin->fin_ip; 6076 ido = ntohs(ip->ip_id); 6077 if (fin->fin_off != 0) { 6078 sum = ipf_frag_ipidknown(fin); 6079 if (sum == 0xffffffff) 6080 return -1; 6081 sum &= 0xffff; 6082 id = (u_short)sum; 6083 ip->ip_id = htons(id); 6084 } else { 6085 ip_fillid(ip); 6086 id = ntohs(ip->ip_id); 6087 if ((fin->fin_flx & FI_FRAG) != 0) 6088 (void) ipf_frag_ipidnew(fin, (u_32_t)id); 6089 } 6090 6091 if (id == ido) 6092 return 0; 6093 CALC_SUMD(ido, id, sumd); /* DESTRUCTIVE MACRO! id,ido change */ 6094 sum = (~ntohs(ip->ip_sum)) & 0xffff; 6095 sum += sumd; 6096 sum = (sum >> 16) + (sum & 0xffff); 6097 sum = (sum >> 16) + (sum & 0xffff); 6098 sums = ~(u_short)sum; 6099 ip->ip_sum = htons(sums); 6100 return 0; 6101} 6102 6103 6104#ifdef NEED_FRGETIFNAME 6105/* ------------------------------------------------------------------------ */ 6106/* Function: ipf_getifname */ 6107/* Returns: char * - pointer to interface name */ 6108/* Parameters: ifp(I) - pointer to network interface */ 6109/* buffer(O) - pointer to where to store interface name */ 6110/* */ 6111/* Constructs an interface name in the buffer passed. The buffer passed is */ 6112/* expected to be at least LIFNAMSIZ in bytes big. If buffer is passed in */ 6113/* as a NULL pointer then return a pointer to a static array. */ 6114/* ------------------------------------------------------------------------ */ 6115char * 6116ipf_getifname(ifp, buffer) 6117 struct ifnet *ifp; 6118 char *buffer; 6119{ 6120 static char namebuf[LIFNAMSIZ]; 6121# if defined(MENTAT) || defined(__FreeBSD__) 6122 int unit, space; 6123 char temp[20]; 6124 char *s; 6125# endif 6126 6127 if (buffer == NULL) 6128 buffer = namebuf; 6129 (void) strncpy(buffer, ifp->if_name, LIFNAMSIZ); 6130 buffer[LIFNAMSIZ - 1] = '\0'; 6131# if defined(MENTAT) || defined(__FreeBSD__) 6132 for (s = buffer; *s; s++) 6133 ; 6134 unit = ifp->if_unit; 6135 space = LIFNAMSIZ - (s - buffer); 6136 if ((space > 0) && (unit >= 0)) { 6137# if defined(SNPRINTF) && defined(_KERNEL) 6138 SNPRINTF(temp, sizeof(temp), "%d", unit); 6139# else 6140 (void) sprintf(temp, "%d", unit); 6141# endif 6142 (void) strncpy(s, temp, space); 6143 } 6144# endif 6145 return buffer; 6146} 6147#endif 6148 6149 6150/* ------------------------------------------------------------------------ */ 6151/* Function: ipf_ioctlswitch */ 6152/* Returns: int - -1 continue processing, else ioctl return value */ 6153/* Parameters: unit(I) - device unit opened */ 6154/* data(I) - pointer to ioctl data */ 6155/* cmd(I) - ioctl command */ 6156/* mode(I) - mode value */ 6157/* uid(I) - uid making the ioctl call */ 6158/* ctx(I) - pointer to context data */ 6159/* */ 6160/* Based on the value of unit, call the appropriate ioctl handler or return */ 6161/* EIO if ipfilter is not running. Also checks if write perms are req'd */ 6162/* for the device in order to execute the ioctl. A special case is made */ 6163/* SIOCIPFINTERROR so that the same code isn't required in every handler. */ 6164/* The context data pointer is passed through as this is used as the key */ 6165/* for locating a matching token for continued access for walking lists, */ 6166/* etc. */ 6167/* ------------------------------------------------------------------------ */ 6168int 6169ipf_ioctlswitch(softc, unit, data, cmd, mode, uid, ctx) 6170 ipf_main_softc_t *softc; 6171 int unit, mode, uid; 6172 ioctlcmd_t cmd; 6173 void *data, *ctx; 6174{ 6175 int error = 0; 6176 6177 switch (cmd) 6178 { 6179 case SIOCIPFINTERROR : 6180 error = BCOPYOUT(&softc->ipf_interror, data, 6181 sizeof(softc->ipf_interror)); 6182 if (error != 0) { 6183 IPFERROR(40); 6184 error = EFAULT; 6185 } 6186 return error; 6187 default : 6188 break; 6189 } 6190 6191 switch (unit) 6192 { 6193 case IPL_LOGIPF : 6194 error = ipf_ipf_ioctl(softc, data, cmd, mode, uid, ctx); 6195 break; 6196 case IPL_LOGNAT : 6197 if (softc->ipf_running > 0) { 6198 error = ipf_nat_ioctl(softc, data, cmd, mode, 6199 uid, ctx); 6200 } else { 6201 IPFERROR(42); 6202 error = EIO; 6203 } 6204 break; 6205 case IPL_LOGSTATE : 6206 if (softc->ipf_running > 0) { 6207 error = ipf_state_ioctl(softc, data, cmd, mode, 6208 uid, ctx); 6209 } else { 6210 IPFERROR(43); 6211 error = EIO; 6212 } 6213 break; 6214 case IPL_LOGAUTH : 6215 if (softc->ipf_running > 0) { 6216 error = ipf_auth_ioctl(softc, data, cmd, mode, 6217 uid, ctx); 6218 } else { 6219 IPFERROR(44); 6220 error = EIO; 6221 } 6222 break; 6223 case IPL_LOGSYNC : 6224 if (softc->ipf_running > 0) { 6225 error = ipf_sync_ioctl(softc, data, cmd, mode, 6226 uid, ctx); 6227 } else { 6228 error = EIO; 6229 IPFERROR(45); 6230 } 6231 break; 6232 case IPL_LOGSCAN : 6233#ifdef IPFILTER_SCAN 6234 if (softc->ipf_running > 0) 6235 error = ipf_scan_ioctl(softc, data, cmd, mode, 6236 uid, ctx); 6237 else 6238#endif 6239 { 6240 error = EIO; 6241 IPFERROR(46); 6242 } 6243 break; 6244 case IPL_LOGLOOKUP : 6245 if (softc->ipf_running > 0) { 6246 error = ipf_lookup_ioctl(softc, data, cmd, mode, 6247 uid, ctx); 6248 } else { 6249 error = EIO; 6250 IPFERROR(47); 6251 } 6252 break; 6253 default : 6254 IPFERROR(48); 6255 error = EIO; 6256 break; 6257 } 6258 6259 return error; 6260} 6261 6262 6263/* 6264 * This array defines the expected size of objects coming into the kernel 6265 * for the various recognised object types. The first column is flags (see 6266 * below), 2nd column is current size, 3rd column is the version number of 6267 * when the current size became current. 6268 * Flags: 6269 * 1 = minimum size, not absolute size 6270 */ 6271static const int ipf_objbytes[IPFOBJ_COUNT][3] = { 6272 { 1, sizeof(struct frentry), 5010000 }, /* 0 */ 6273 { 1, sizeof(struct friostat), 5010000 }, 6274 { 0, sizeof(struct fr_info), 5010000 }, 6275 { 0, sizeof(struct ipf_authstat), 4010100 }, 6276 { 0, sizeof(struct ipfrstat), 5010000 }, 6277 { 1, sizeof(struct ipnat), 5010000 }, /* 5 */ 6278 { 0, sizeof(struct natstat), 5010000 }, 6279 { 0, sizeof(struct ipstate_save), 5010000 }, 6280 { 1, sizeof(struct nat_save), 5010000 }, 6281 { 0, sizeof(struct natlookup), 5010000 }, 6282 { 1, sizeof(struct ipstate), 5010000 }, /* 10 */ 6283 { 0, sizeof(struct ips_stat), 5010000 }, 6284 { 0, sizeof(struct frauth), 5010000 }, 6285 { 0, sizeof(struct ipftune), 4010100 }, 6286 { 0, sizeof(struct nat), 5010000 }, 6287 { 0, sizeof(struct ipfruleiter), 4011400 }, /* 15 */ 6288 { 0, sizeof(struct ipfgeniter), 4011400 }, 6289 { 0, sizeof(struct ipftable), 4011400 }, 6290 { 0, sizeof(struct ipflookupiter), 4011400 }, 6291 { 0, sizeof(struct ipftq) * IPF_TCP_NSTATES }, 6292 { 1, 0, 0 }, /* IPFEXPR */ 6293 { 0, 0, 0 }, /* PROXYCTL */ 6294 { 0, sizeof (struct fripf), 5010000 } 6295}; 6296 6297 6298/* ------------------------------------------------------------------------ */ 6299/* Function: ipf_inobj */ 6300/* Returns: int - 0 = success, else failure */ 6301/* Parameters: softc(I) - soft context pointerto work with */ 6302/* data(I) - pointer to ioctl data */ 6303/* objp(O) - where to store ipfobj structure */ 6304/* ptr(I) - pointer to data to copy out */ 6305/* type(I) - type of structure being moved */ 6306/* */ 6307/* Copy in the contents of what the ipfobj_t points to. In future, we */ 6308/* add things to check for version numbers, sizes, etc, to make it backward */ 6309/* compatible at the ABI for user land. */ 6310/* If objp is not NULL then we assume that the caller wants to see what is */ 6311/* in the ipfobj_t structure being copied in. As an example, this can tell */ 6312/* the caller what version of ipfilter the ioctl program was written to. */ 6313/* ------------------------------------------------------------------------ */ 6314int 6315ipf_inobj(softc, data, objp, ptr, type) 6316 ipf_main_softc_t *softc; 6317 void *data; 6318 ipfobj_t *objp; 6319 void *ptr; 6320 int type; 6321{ 6322 ipfobj_t obj; 6323 int error; 6324 int size; 6325 6326 if ((type < 0) || (type >= IPFOBJ_COUNT)) { 6327 IPFERROR(49); 6328 return EINVAL; 6329 } 6330 6331 if (objp == NULL) 6332 objp = &obj; 6333 error = BCOPYIN(data, objp, sizeof(*objp)); 6334 if (error != 0) { 6335 IPFERROR(124); 6336 return EFAULT; 6337 } 6338 6339 if (objp->ipfo_type != type) { 6340 IPFERROR(50); 6341 return EINVAL; 6342 } 6343 6344 if (objp->ipfo_rev >= ipf_objbytes[type][2]) { 6345 if ((ipf_objbytes[type][0] & 1) != 0) { 6346 if (objp->ipfo_size < ipf_objbytes[type][1]) { 6347 IPFERROR(51); 6348 return EINVAL; 6349 } 6350 size = ipf_objbytes[type][1]; 6351 } else if (objp->ipfo_size == ipf_objbytes[type][1]) { 6352 size = objp->ipfo_size; 6353 } else { 6354 IPFERROR(52); 6355 return EINVAL; 6356 } 6357 error = COPYIN(objp->ipfo_ptr, ptr, size); 6358 if (error != 0) { 6359 IPFERROR(55); 6360 error = EFAULT; 6361 } 6362 } else { 6363#ifdef IPFILTER_COMPAT 6364 error = ipf_in_compat(softc, objp, ptr, 0); 6365#else 6366 IPFERROR(54); 6367 error = EINVAL; 6368#endif 6369 } 6370 return error; 6371} 6372 6373 6374/* ------------------------------------------------------------------------ */ 6375/* Function: ipf_inobjsz */ 6376/* Returns: int - 0 = success, else failure */ 6377/* Parameters: softc(I) - soft context pointerto work with */ 6378/* data(I) - pointer to ioctl data */ 6379/* ptr(I) - pointer to store real data in */ 6380/* type(I) - type of structure being moved */ 6381/* sz(I) - size of data to copy */ 6382/* */ 6383/* As per ipf_inobj, except the size of the object to copy in is passed in */ 6384/* but it must not be smaller than the size defined for the type and the */ 6385/* type must allow for varied sized objects. The extra requirement here is */ 6386/* that sz must match the size of the object being passed in - this is not */ 6387/* not possible nor required in ipf_inobj(). */ 6388/* ------------------------------------------------------------------------ */ 6389int 6390ipf_inobjsz(softc, data, ptr, type, sz) 6391 ipf_main_softc_t *softc; 6392 void *data; 6393 void *ptr; 6394 int type, sz; 6395{ 6396 ipfobj_t obj; 6397 int error; 6398 6399 if ((type < 0) || (type >= IPFOBJ_COUNT)) { 6400 IPFERROR(56); 6401 return EINVAL; 6402 } 6403 6404 error = BCOPYIN(data, &obj, sizeof(obj)); 6405 if (error != 0) { 6406 IPFERROR(125); 6407 return EFAULT; 6408 } 6409 6410 if (obj.ipfo_type != type) { 6411 IPFERROR(58); 6412 return EINVAL; 6413 } 6414 6415 if (obj.ipfo_rev >= ipf_objbytes[type][2]) { 6416 if (((ipf_objbytes[type][0] & 1) == 0) || 6417 (sz < ipf_objbytes[type][1])) { 6418 IPFERROR(57); 6419 return EINVAL; 6420 } 6421 error = COPYIN(obj.ipfo_ptr, ptr, sz); 6422 if (error != 0) { 6423 IPFERROR(61); 6424 error = EFAULT; 6425 } 6426 } else { 6427#ifdef IPFILTER_COMPAT 6428 error = ipf_in_compat(softc, &obj, ptr, sz); 6429#else 6430 IPFERROR(60); 6431 error = EINVAL; 6432#endif 6433 } 6434 return error; 6435} 6436 6437 6438/* ------------------------------------------------------------------------ */ 6439/* Function: ipf_outobjsz */ 6440/* Returns: int - 0 = success, else failure */ 6441/* Parameters: data(I) - pointer to ioctl data */ 6442/* ptr(I) - pointer to store real data in */ 6443/* type(I) - type of structure being moved */ 6444/* sz(I) - size of data to copy */ 6445/* */ 6446/* As per ipf_outobj, except the size of the object to copy out is passed in*/ 6447/* but it must not be smaller than the size defined for the type and the */ 6448/* type must allow for varied sized objects. The extra requirement here is */ 6449/* that sz must match the size of the object being passed in - this is not */ 6450/* not possible nor required in ipf_outobj(). */ 6451/* ------------------------------------------------------------------------ */ 6452int 6453ipf_outobjsz(softc, data, ptr, type, sz) 6454 ipf_main_softc_t *softc; 6455 void *data; 6456 void *ptr; 6457 int type, sz; 6458{ 6459 ipfobj_t obj; 6460 int error; 6461 6462 if ((type < 0) || (type >= IPFOBJ_COUNT)) { 6463 IPFERROR(62); 6464 return EINVAL; 6465 } 6466 6467 error = BCOPYIN(data, &obj, sizeof(obj)); 6468 if (error != 0) { 6469 IPFERROR(127); 6470 return EFAULT; 6471 } 6472 6473 if (obj.ipfo_type != type) { 6474 IPFERROR(63); 6475 return EINVAL; 6476 } 6477 6478 if (obj.ipfo_rev >= ipf_objbytes[type][2]) { 6479 if (((ipf_objbytes[type][0] & 1) == 0) || 6480 (sz < ipf_objbytes[type][1])) { 6481 IPFERROR(146); 6482 return EINVAL; 6483 } 6484 error = COPYOUT(ptr, obj.ipfo_ptr, sz); 6485 if (error != 0) { 6486 IPFERROR(66); 6487 error = EFAULT; 6488 } 6489 } else { 6490#ifdef IPFILTER_COMPAT 6491 error = ipf_out_compat(softc, &obj, ptr); 6492#else 6493 IPFERROR(65); 6494 error = EINVAL; 6495#endif 6496 } 6497 return error; 6498} 6499 6500 6501/* ------------------------------------------------------------------------ */ 6502/* Function: ipf_outobj */ 6503/* Returns: int - 0 = success, else failure */ 6504/* Parameters: data(I) - pointer to ioctl data */ 6505/* ptr(I) - pointer to store real data in */ 6506/* type(I) - type of structure being moved */ 6507/* */ 6508/* Copy out the contents of what ptr is to where ipfobj points to. In */ 6509/* future, we add things to check for version numbers, sizes, etc, to make */ 6510/* it backward compatible at the ABI for user land. */ 6511/* ------------------------------------------------------------------------ */ 6512int 6513ipf_outobj(softc, data, ptr, type) 6514 ipf_main_softc_t *softc; 6515 void *data; 6516 void *ptr; 6517 int type; 6518{ 6519 ipfobj_t obj; 6520 int error; 6521 6522 if ((type < 0) || (type >= IPFOBJ_COUNT)) { 6523 IPFERROR(67); 6524 return EINVAL; 6525 } 6526 6527 error = BCOPYIN(data, &obj, sizeof(obj)); 6528 if (error != 0) { 6529 IPFERROR(126); 6530 return EFAULT; 6531 } 6532 6533 if (obj.ipfo_type != type) { 6534 IPFERROR(68); 6535 return EINVAL; 6536 } 6537 6538 if (obj.ipfo_rev >= ipf_objbytes[type][2]) { 6539 if ((ipf_objbytes[type][0] & 1) != 0) { 6540 if (obj.ipfo_size < ipf_objbytes[type][1]) { 6541 IPFERROR(69); 6542 return EINVAL; 6543 } 6544 } else if (obj.ipfo_size != ipf_objbytes[type][1]) { 6545 IPFERROR(70); 6546 return EINVAL; 6547 } 6548 6549 error = COPYOUT(ptr, obj.ipfo_ptr, obj.ipfo_size); 6550 if (error != 0) { 6551 IPFERROR(73); 6552 error = EFAULT; 6553 } 6554 } else { 6555#ifdef IPFILTER_COMPAT 6556 error = ipf_out_compat(softc, &obj, ptr); 6557#else 6558 IPFERROR(72); 6559 error = EINVAL; 6560#endif 6561 } 6562 return error; 6563} 6564 6565 6566/* ------------------------------------------------------------------------ */ 6567/* Function: ipf_outobjk */ 6568/* Returns: int - 0 = success, else failure */ 6569/* Parameters: obj(I) - pointer to data description structure */ 6570/* ptr(I) - pointer to kernel data to copy out */ 6571/* */ 6572/* In the above functions, the ipfobj_t structure is copied into the kernel,*/ 6573/* telling ipfilter how to copy out data. In this instance, the ipfobj_t is */ 6574/* already populated with information and now we just need to use it. */ 6575/* There is no need for this function to have a "type" parameter as there */ 6576/* is no point in validating information that comes from the kernel with */ 6577/* itself. */ 6578/* ------------------------------------------------------------------------ */ 6579int 6580ipf_outobjk(softc, obj, ptr) 6581 ipf_main_softc_t *softc; 6582 ipfobj_t *obj; 6583 void *ptr; 6584{ 6585 int type = obj->ipfo_type; 6586 int error; 6587 6588 if ((type < 0) || (type >= IPFOBJ_COUNT)) { 6589 IPFERROR(147); 6590 return EINVAL; 6591 } 6592 6593 if (obj->ipfo_rev >= ipf_objbytes[type][2]) { 6594 if ((ipf_objbytes[type][0] & 1) != 0) { 6595 if (obj->ipfo_size < ipf_objbytes[type][1]) { 6596 IPFERROR(148); 6597 return EINVAL; 6598 } 6599 6600 } else if (obj->ipfo_size != ipf_objbytes[type][1]) { 6601 IPFERROR(149); 6602 return EINVAL; 6603 } 6604 6605 error = COPYOUT(ptr, obj->ipfo_ptr, obj->ipfo_size); 6606 if (error != 0) { 6607 IPFERROR(150); 6608 error = EFAULT; 6609 } 6610 } else { 6611#ifdef IPFILTER_COMPAT 6612 error = ipf_out_compat(softc, obj, ptr); 6613#else 6614 IPFERROR(151); 6615 error = EINVAL; 6616#endif 6617 } 6618 return error; 6619} 6620 6621 6622/* ------------------------------------------------------------------------ */ 6623/* Function: ipf_checkl4sum */ 6624/* Returns: int - 0 = good, -1 = bad, 1 = cannot check */ 6625/* Parameters: fin(I) - pointer to packet information */ 6626/* */ 6627/* If possible, calculate the layer 4 checksum for the packet. If this is */ 6628/* not possible, return without indicating a failure or success but in a */ 6629/* way that is ditinguishable. This function should only be called by the */ 6630/* ipf_checkv6sum() for each platform. */ 6631/* ------------------------------------------------------------------------ */ 6632INLINE int 6633ipf_checkl4sum(fin) 6634 fr_info_t *fin; 6635{ 6636 u_short sum, hdrsum, *csump; 6637 udphdr_t *udp; 6638 int dosum; 6639 6640 /* 6641 * If the TCP packet isn't a fragment, isn't too short and otherwise 6642 * isn't already considered "bad", then validate the checksum. If 6643 * this check fails then considered the packet to be "bad". 6644 */ 6645 if ((fin->fin_flx & (FI_FRAG|FI_SHORT|FI_BAD)) != 0) 6646 return 1; 6647 6648 csump = NULL; 6649 hdrsum = 0; 6650 dosum = 0; 6651 sum = 0; 6652 6653 switch (fin->fin_p) 6654 { 6655 case IPPROTO_TCP : 6656 csump = &((tcphdr_t *)fin->fin_dp)->th_sum; 6657 dosum = 1; 6658 break; 6659 6660 case IPPROTO_UDP : 6661 udp = fin->fin_dp; 6662 if (udp->uh_sum != 0) { 6663 csump = &udp->uh_sum; 6664 dosum = 1; 6665 } 6666 break; 6667 6668#ifdef USE_INET6 6669 case IPPROTO_ICMPV6 : 6670 csump = &((struct icmp6_hdr *)fin->fin_dp)->icmp6_cksum; 6671 dosum = 1; 6672 break; 6673#endif 6674 6675 case IPPROTO_ICMP : 6676 csump = &((struct icmp *)fin->fin_dp)->icmp_cksum; 6677 dosum = 1; 6678 break; 6679 6680 default : 6681 return 1; 6682 /*NOTREACHED*/ 6683 } 6684 6685 if (csump != NULL) 6686 hdrsum = *csump; 6687 6688 if (dosum) { 6689 sum = fr_cksum(fin, fin->fin_ip, fin->fin_p, fin->fin_dp); 6690 } 6691#if !defined(_KERNEL) 6692 if (sum == hdrsum) { 6693 FR_DEBUG(("checkl4sum: %hx == %hx\n", sum, hdrsum)); 6694 } else { 6695 FR_DEBUG(("checkl4sum: %hx != %hx\n", sum, hdrsum)); 6696 } 6697#endif 6698 DT2(l4sums, u_short, hdrsum, u_short, sum); 6699 if (hdrsum == sum) { 6700 fin->fin_cksum = FI_CK_SUMOK; 6701 return 0; 6702 } 6703 fin->fin_cksum = FI_CK_BAD; 6704 return -1; 6705} 6706 6707 6708/* ------------------------------------------------------------------------ */ 6709/* Function: ipf_ifpfillv4addr */ 6710/* Returns: int - 0 = address update, -1 = address not updated */ 6711/* Parameters: atype(I) - type of network address update to perform */ 6712/* sin(I) - pointer to source of address information */ 6713/* mask(I) - pointer to source of netmask information */ 6714/* inp(I) - pointer to destination address store */ 6715/* inpmask(I) - pointer to destination netmask store */ 6716/* */ 6717/* Given a type of network address update (atype) to perform, copy */ 6718/* information from sin/mask into inp/inpmask. If ipnmask is NULL then no */ 6719/* netmask update is performed unless FRI_NETMASKED is passed as atype, in */ 6720/* which case the operation fails. For all values of atype other than */ 6721/* FRI_NETMASKED, if inpmask is non-NULL then the mask is set to an all 1s */ 6722/* value. */ 6723/* ------------------------------------------------------------------------ */ 6724int 6725ipf_ifpfillv4addr(atype, sin, mask, inp, inpmask) 6726 int atype; 6727 struct sockaddr_in *sin, *mask; 6728 struct in_addr *inp, *inpmask; 6729{ 6730 if (inpmask != NULL && atype != FRI_NETMASKED) 6731 inpmask->s_addr = 0xffffffff; 6732 6733 if (atype == FRI_NETWORK || atype == FRI_NETMASKED) { 6734 if (atype == FRI_NETMASKED) { 6735 if (inpmask == NULL) 6736 return -1; 6737 inpmask->s_addr = mask->sin_addr.s_addr; 6738 } 6739 inp->s_addr = sin->sin_addr.s_addr & mask->sin_addr.s_addr; 6740 } else { 6741 inp->s_addr = sin->sin_addr.s_addr; 6742 } 6743 return 0; 6744} 6745 6746 6747#ifdef USE_INET6 6748/* ------------------------------------------------------------------------ */ 6749/* Function: ipf_ifpfillv6addr */ 6750/* Returns: int - 0 = address update, -1 = address not updated */ 6751/* Parameters: atype(I) - type of network address update to perform */ 6752/* sin(I) - pointer to source of address information */ 6753/* mask(I) - pointer to source of netmask information */ 6754/* inp(I) - pointer to destination address store */ 6755/* inpmask(I) - pointer to destination netmask store */ 6756/* */ 6757/* Given a type of network address update (atype) to perform, copy */ 6758/* information from sin/mask into inp/inpmask. If ipnmask is NULL then no */ 6759/* netmask update is performed unless FRI_NETMASKED is passed as atype, in */ 6760/* which case the operation fails. For all values of atype other than */ 6761/* FRI_NETMASKED, if inpmask is non-NULL then the mask is set to an all 1s */ 6762/* value. */ 6763/* ------------------------------------------------------------------------ */ 6764int 6765ipf_ifpfillv6addr(atype, sin, mask, inp, inpmask) 6766 int atype; 6767 struct sockaddr_in6 *sin, *mask; 6768 i6addr_t *inp, *inpmask; 6769{ 6770 i6addr_t *src, *and; 6771 6772 src = (i6addr_t *)&sin->sin6_addr; 6773 and = (i6addr_t *)&mask->sin6_addr; 6774 6775 if (inpmask != NULL && atype != FRI_NETMASKED) { 6776 inpmask->i6[0] = 0xffffffff; 6777 inpmask->i6[1] = 0xffffffff; 6778 inpmask->i6[2] = 0xffffffff; 6779 inpmask->i6[3] = 0xffffffff; 6780 } 6781 6782 if (atype == FRI_NETWORK || atype == FRI_NETMASKED) { 6783 if (atype == FRI_NETMASKED) { 6784 if (inpmask == NULL) 6785 return -1; 6786 inpmask->i6[0] = and->i6[0]; 6787 inpmask->i6[1] = and->i6[1]; 6788 inpmask->i6[2] = and->i6[2]; 6789 inpmask->i6[3] = and->i6[3]; 6790 } 6791 6792 inp->i6[0] = src->i6[0] & and->i6[0]; 6793 inp->i6[1] = src->i6[1] & and->i6[1]; 6794 inp->i6[2] = src->i6[2] & and->i6[2]; 6795 inp->i6[3] = src->i6[3] & and->i6[3]; 6796 } else { 6797 inp->i6[0] = src->i6[0]; 6798 inp->i6[1] = src->i6[1]; 6799 inp->i6[2] = src->i6[2]; 6800 inp->i6[3] = src->i6[3]; 6801 } 6802 return 0; 6803} 6804#endif 6805 6806 6807/* ------------------------------------------------------------------------ */ 6808/* Function: ipf_matchtag */ 6809/* Returns: 0 == mismatch, 1 == match. */ 6810/* Parameters: tag1(I) - pointer to first tag to compare */ 6811/* tag2(I) - pointer to second tag to compare */ 6812/* */ 6813/* Returns true (non-zero) or false(0) if the two tag structures can be */ 6814/* considered to be a match or not match, respectively. The tag is 16 */ 6815/* bytes long (16 characters) but that is overlayed with 4 32bit ints so */ 6816/* compare the ints instead, for speed. tag1 is the master of the */ 6817/* comparison. This function should only be called with both tag1 and tag2 */ 6818/* as non-NULL pointers. */ 6819/* ------------------------------------------------------------------------ */ 6820int 6821ipf_matchtag(tag1, tag2) 6822 ipftag_t *tag1, *tag2; 6823{ 6824 if (tag1 == tag2) 6825 return 1; 6826 6827 if ((tag1->ipt_num[0] == 0) && (tag2->ipt_num[0] == 0)) 6828 return 1; 6829 6830 if ((tag1->ipt_num[0] == tag2->ipt_num[0]) && 6831 (tag1->ipt_num[1] == tag2->ipt_num[1]) && 6832 (tag1->ipt_num[2] == tag2->ipt_num[2]) && 6833 (tag1->ipt_num[3] == tag2->ipt_num[3])) 6834 return 1; 6835 return 0; 6836} 6837 6838 6839/* ------------------------------------------------------------------------ */ 6840/* Function: ipf_coalesce */ 6841/* Returns: 1 == success, -1 == failure, 0 == no change */ 6842/* Parameters: fin(I) - pointer to packet information */ 6843/* */ 6844/* Attempt to get all of the packet data into a single, contiguous buffer. */ 6845/* If this call returns a failure then the buffers have also been freed. */ 6846/* ------------------------------------------------------------------------ */ 6847int 6848ipf_coalesce(fin) 6849 fr_info_t *fin; 6850{ 6851 6852 if ((fin->fin_flx & FI_COALESCE) != 0) 6853 return 1; 6854 6855 /* 6856 * If the mbuf pointers indicate that there is no mbuf to work with, 6857 * return but do not indicate success or failure. 6858 */ 6859 if (fin->fin_m == NULL || fin->fin_mp == NULL) 6860 return 0; 6861 6862#if defined(_KERNEL) 6863 if (ipf_pullup(fin->fin_m, fin, fin->fin_plen) == NULL) { 6864 ipf_main_softc_t *softc = fin->fin_main_soft; 6865 6866 DT1(frb_coalesce, fr_info_t *, fin); 6867 LBUMP(ipf_stats[fin->fin_out].fr_badcoalesces); 6868# ifdef MENTAT 6869 FREE_MB_T(*fin->fin_mp); 6870# endif 6871 fin->fin_reason = FRB_COALESCE; 6872 *fin->fin_mp = NULL; 6873 fin->fin_m = NULL; 6874 return -1; 6875 } 6876#else 6877 fin = fin; /* LINT */ 6878#endif 6879 return 1; 6880} 6881 6882 6883/* 6884 * The following table lists all of the tunable variables that can be 6885 * accessed via SIOCIPFGET/SIOCIPFSET/SIOCIPFGETNEXt. The format of each row 6886 * in the table below is as follows: 6887 * 6888 * pointer to value, name of value, minimum, maximum, size of the value's 6889 * container, value attribute flags 6890 * 6891 * For convienience, IPFT_RDONLY means the value is read-only, IPFT_WRDISABLED 6892 * means the value can only be written to when IPFilter is loaded but disabled. 6893 * The obvious implication is if neither of these are set then the value can be 6894 * changed at any time without harm. 6895 */ 6896 6897 6898/* ------------------------------------------------------------------------ */ 6899/* Function: ipf_tune_findbycookie */ 6900/* Returns: NULL = search failed, else pointer to tune struct */ 6901/* Parameters: cookie(I) - cookie value to search for amongst tuneables */ 6902/* next(O) - pointer to place to store the cookie for the */ 6903/* "next" tuneable, if it is desired. */ 6904/* */ 6905/* This function is used to walk through all of the existing tunables with */ 6906/* successive calls. It searches the known tunables for the one which has */ 6907/* a matching value for "cookie" - ie its address. When returning a match, */ 6908/* the next one to be found may be returned inside next. */ 6909/* ------------------------------------------------------------------------ */ 6910static ipftuneable_t * 6911ipf_tune_findbycookie(ptop, cookie, next) 6912 ipftuneable_t **ptop; 6913 void *cookie, **next; 6914{ 6915 ipftuneable_t *ta, **tap; 6916 6917 for (ta = *ptop; ta->ipft_name != NULL; ta++) 6918 if (ta == cookie) { 6919 if (next != NULL) { 6920 /* 6921 * If the next entry in the array has a name 6922 * present, then return a pointer to it for 6923 * where to go next, else return a pointer to 6924 * the dynaminc list as a key to search there 6925 * next. This facilitates a weak linking of 6926 * the two "lists" together. 6927 */ 6928 if ((ta + 1)->ipft_name != NULL) 6929 *next = ta + 1; 6930 else 6931 *next = ptop; 6932 } 6933 return ta; 6934 } 6935 6936 for (tap = ptop; (ta = *tap) != NULL; tap = &ta->ipft_next) 6937 if (tap == cookie) { 6938 if (next != NULL) 6939 *next = &ta->ipft_next; 6940 return ta; 6941 } 6942 6943 if (next != NULL) 6944 *next = NULL; 6945 return NULL; 6946} 6947 6948 6949/* ------------------------------------------------------------------------ */ 6950/* Function: ipf_tune_findbyname */ 6951/* Returns: NULL = search failed, else pointer to tune struct */ 6952/* Parameters: name(I) - name of the tuneable entry to find. */ 6953/* */ 6954/* Search the static array of tuneables and the list of dynamic tuneables */ 6955/* for an entry with a matching name. If we can find one, return a pointer */ 6956/* to the matching structure. */ 6957/* ------------------------------------------------------------------------ */ 6958static ipftuneable_t * 6959ipf_tune_findbyname(top, name) 6960 ipftuneable_t *top; 6961 const char *name; 6962{ 6963 ipftuneable_t *ta; 6964 6965 for (ta = top; ta != NULL; ta = ta->ipft_next) 6966 if (!strcmp(ta->ipft_name, name)) { 6967 return ta; 6968 } 6969 6970 return NULL; 6971} 6972 6973 6974/* ------------------------------------------------------------------------ */ 6975/* Function: ipf_tune_add_array */ 6976/* Returns: int - 0 == success, else failure */ 6977/* Parameters: newtune - pointer to new tune array to add to tuneables */ 6978/* */ 6979/* Appends tune structures from the array passed in (newtune) to the end of */ 6980/* the current list of "dynamic" tuneable parameters. */ 6981/* If any entry to be added is already present (by name) then the operation */ 6982/* is aborted - entries that have been added are removed before returning. */ 6983/* An entry with no name (NULL) is used as the indication that the end of */ 6984/* the array has been reached. */ 6985/* ------------------------------------------------------------------------ */ 6986int 6987ipf_tune_add_array(softc, newtune) 6988 ipf_main_softc_t *softc; 6989 ipftuneable_t *newtune; 6990{ 6991 ipftuneable_t *nt, *dt; 6992 int error = 0; 6993 6994 for (nt = newtune; nt->ipft_name != NULL; nt++) { 6995 error = ipf_tune_add(softc, nt); 6996 if (error != 0) { 6997 for (dt = newtune; dt != nt; dt++) { 6998 (void) ipf_tune_del(softc, dt); 6999 } 7000 } 7001 } 7002 7003 return error; 7004} 7005 7006 7007/* ------------------------------------------------------------------------ */ 7008/* Function: ipf_tune_array_link */ 7009/* Returns: 0 == success, -1 == failure */ 7010/* Parameters: softc(I) - soft context pointerto work with */ 7011/* array(I) - pointer to an array of tuneables */ 7012/* */ 7013/* Given an array of tunables (array), append them to the current list of */ 7014/* tuneables for this context (softc->ipf_tuners.) To properly prepare the */ 7015/* the array for being appended to the list, initialise all of the next */ 7016/* pointers so we don't need to walk parts of it with ++ and others with */ 7017/* next. The array is expected to have an entry with a NULL name as the */ 7018/* terminator. Trying to add an array with no non-NULL names will return as */ 7019/* a failure. */ 7020/* ------------------------------------------------------------------------ */ 7021int 7022ipf_tune_array_link(softc, array) 7023 ipf_main_softc_t *softc; 7024 ipftuneable_t *array; 7025{ 7026 ipftuneable_t *t, **p; 7027 7028 t = array; 7029 if (t->ipft_name == NULL) 7030 return -1; 7031 7032 for (; t[1].ipft_name != NULL; t++) 7033 t[0].ipft_next = &t[1]; 7034 t->ipft_next = NULL; 7035 7036 /* 7037 * Since a pointer to the last entry isn't kept, we need to find it 7038 * each time we want to add new variables to the list. 7039 */ 7040 for (p = &softc->ipf_tuners; (t = *p) != NULL; p = &t->ipft_next) 7041 if (t->ipft_name == NULL) 7042 break; 7043 *p = array; 7044 7045 return 0; 7046} 7047 7048 7049/* ------------------------------------------------------------------------ */ 7050/* Function: ipf_tune_array_unlink */ 7051/* Returns: 0 == success, -1 == failure */ 7052/* Parameters: softc(I) - soft context pointerto work with */ 7053/* array(I) - pointer to an array of tuneables */ 7054/* */ 7055/* ------------------------------------------------------------------------ */ 7056int 7057ipf_tune_array_unlink(softc, array) 7058 ipf_main_softc_t *softc; 7059 ipftuneable_t *array; 7060{ 7061 ipftuneable_t *t, **p; 7062 7063 for (p = &softc->ipf_tuners; (t = *p) != NULL; p = &t->ipft_next) 7064 if (t == array) 7065 break; 7066 if (t == NULL) 7067 return -1; 7068 7069 for (; t[1].ipft_name != NULL; t++) 7070 ; 7071 7072 *p = t->ipft_next; 7073 7074 return 0; 7075} 7076 7077 7078/* ------------------------------------------------------------------------ */ 7079/* Function: ipf_tune_array_copy */ 7080/* Returns: NULL = failure, else pointer to new array */ 7081/* Parameters: base(I) - pointer to structure base */ 7082/* size(I) - size of the array at template */ 7083/* template(I) - original array to copy */ 7084/* */ 7085/* Allocate memory for a new set of tuneable values and copy everything */ 7086/* from template into the new region of memory. The new region is full of */ 7087/* uninitialised pointers (ipft_next) so set them up. Now, ipftp_offset... */ 7088/* */ 7089/* NOTE: the following assumes that sizeof(long) == sizeof(void *) */ 7090/* In the array template, ipftp_offset is the offset (in bytes) of the */ 7091/* location of the tuneable value inside the structure pointed to by base. */ 7092/* As ipftp_offset is a union over the pointers to the tuneable values, if */ 7093/* we add base to the copy's ipftp_offset, copy ends up with a pointer in */ 7094/* ipftp_void that points to the stored value. */ 7095/* ------------------------------------------------------------------------ */ 7096ipftuneable_t * 7097ipf_tune_array_copy(base, size, template) 7098 void *base; 7099 size_t size; 7100 ipftuneable_t *template; 7101{ 7102 ipftuneable_t *copy; 7103 int i; 7104 7105 7106 KMALLOCS(copy, ipftuneable_t *, size); 7107 if (copy == NULL) { 7108 return NULL; 7109 } 7110 bcopy(template, copy, size); 7111 7112 for (i = 0; copy[i].ipft_name; i++) { 7113 copy[i].ipft_una.ipftp_offset += (u_long)base; 7114 copy[i].ipft_next = copy + i + 1; 7115 } 7116 7117 return copy; 7118} 7119 7120 7121/* ------------------------------------------------------------------------ */ 7122/* Function: ipf_tune_add */ 7123/* Returns: int - 0 == success, else failure */ 7124/* Parameters: newtune - pointer to new tune entry to add to tuneables */ 7125/* */ 7126/* Appends tune structures from the array passed in (newtune) to the end of */ 7127/* the current list of "dynamic" tuneable parameters. Once added, the */ 7128/* owner of the object is not expected to ever change "ipft_next". */ 7129/* ------------------------------------------------------------------------ */ 7130int 7131ipf_tune_add(softc, newtune) 7132 ipf_main_softc_t *softc; 7133 ipftuneable_t *newtune; 7134{ 7135 ipftuneable_t *ta, **tap; 7136 7137 ta = ipf_tune_findbyname(softc->ipf_tuners, newtune->ipft_name); 7138 if (ta != NULL) { 7139 IPFERROR(74); 7140 return EEXIST; 7141 } 7142 7143 for (tap = &softc->ipf_tuners; *tap != NULL; tap = &(*tap)->ipft_next) 7144 ; 7145 7146 newtune->ipft_next = NULL; 7147 *tap = newtune; 7148 return 0; 7149} 7150 7151 7152/* ------------------------------------------------------------------------ */ 7153/* Function: ipf_tune_del */ 7154/* Returns: int - 0 == success, else failure */ 7155/* Parameters: oldtune - pointer to tune entry to remove from the list of */ 7156/* current dynamic tuneables */ 7157/* */ 7158/* Search for the tune structure, by pointer, in the list of those that are */ 7159/* dynamically added at run time. If found, adjust the list so that this */ 7160/* structure is no longer part of it. */ 7161/* ------------------------------------------------------------------------ */ 7162int 7163ipf_tune_del(softc, oldtune) 7164 ipf_main_softc_t *softc; 7165 ipftuneable_t *oldtune; 7166{ 7167 ipftuneable_t *ta, **tap; 7168 int error = 0; 7169 7170 for (tap = &softc->ipf_tuners; (ta = *tap) != NULL; 7171 tap = &ta->ipft_next) { 7172 if (ta == oldtune) { 7173 *tap = oldtune->ipft_next; 7174 oldtune->ipft_next = NULL; 7175 break; 7176 } 7177 } 7178 7179 if (ta == NULL) { 7180 error = ESRCH; 7181 IPFERROR(75); 7182 } 7183 return error; 7184} 7185 7186 7187/* ------------------------------------------------------------------------ */ 7188/* Function: ipf_tune_del_array */ 7189/* Returns: int - 0 == success, else failure */ 7190/* Parameters: oldtune - pointer to tuneables array */ 7191/* */ 7192/* Remove each tuneable entry in the array from the list of "dynamic" */ 7193/* tunables. If one entry should fail to be found, an error will be */ 7194/* returned and no further ones removed. */ 7195/* An entry with a NULL name is used as the indicator of the last entry in */ 7196/* the array. */ 7197/* ------------------------------------------------------------------------ */ 7198int 7199ipf_tune_del_array(softc, oldtune) 7200 ipf_main_softc_t *softc; 7201 ipftuneable_t *oldtune; 7202{ 7203 ipftuneable_t *ot; 7204 int error = 0; 7205 7206 for (ot = oldtune; ot->ipft_name != NULL; ot++) { 7207 error = ipf_tune_del(softc, ot); 7208 if (error != 0) 7209 break; 7210 } 7211 7212 return error; 7213 7214} 7215 7216 7217/* ------------------------------------------------------------------------ */ 7218/* Function: ipf_tune */ 7219/* Returns: int - 0 == success, else failure */ 7220/* Parameters: cmd(I) - ioctl command number */ 7221/* data(I) - pointer to ioctl data structure */ 7222/* */ 7223/* Implement handling of SIOCIPFGETNEXT, SIOCIPFGET and SIOCIPFSET. These */ 7224/* three ioctls provide the means to access and control global variables */ 7225/* within IPFilter, allowing (for example) timeouts and table sizes to be */ 7226/* changed without rebooting, reloading or recompiling. The initialisation */ 7227/* and 'destruction' routines of the various components of ipfilter are all */ 7228/* each responsible for handling their own values being too big. */ 7229/* ------------------------------------------------------------------------ */ 7230int 7231ipf_ipftune(softc, cmd, data) 7232 ipf_main_softc_t *softc; 7233 ioctlcmd_t cmd; 7234 void *data; 7235{ 7236 ipftuneable_t *ta; 7237 ipftune_t tu; 7238 void *cookie; 7239 int error; 7240 7241 error = ipf_inobj(softc, data, NULL, &tu, IPFOBJ_TUNEABLE); 7242 if (error != 0) 7243 return error; 7244 7245 tu.ipft_name[sizeof(tu.ipft_name) - 1] = '\0'; 7246 cookie = tu.ipft_cookie; 7247 ta = NULL; 7248 7249 switch (cmd) 7250 { 7251 case SIOCIPFGETNEXT : 7252 /* 7253 * If cookie is non-NULL, assume it to be a pointer to the last 7254 * entry we looked at, so find it (if possible) and return a 7255 * pointer to the next one after it. The last entry in the 7256 * the table is a NULL entry, so when we get to it, set cookie 7257 * to NULL and return that, indicating end of list, erstwhile 7258 * if we come in with cookie set to NULL, we are starting anew 7259 * at the front of the list. 7260 */ 7261 if (cookie != NULL) { 7262 ta = ipf_tune_findbycookie(&softc->ipf_tuners, 7263 cookie, &tu.ipft_cookie); 7264 } else { 7265 ta = softc->ipf_tuners; 7266 tu.ipft_cookie = ta + 1; 7267 } 7268 if (ta != NULL) { 7269 /* 7270 * Entry found, but does the data pointed to by that 7271 * row fit in what we can return? 7272 */ 7273 if (ta->ipft_sz > sizeof(tu.ipft_un)) { 7274 IPFERROR(76); 7275 return EINVAL; 7276 } 7277 7278 tu.ipft_vlong = 0; 7279 if (ta->ipft_sz == sizeof(u_long)) 7280 tu.ipft_vlong = *ta->ipft_plong; 7281 else if (ta->ipft_sz == sizeof(u_int)) 7282 tu.ipft_vint = *ta->ipft_pint; 7283 else if (ta->ipft_sz == sizeof(u_short)) 7284 tu.ipft_vshort = *ta->ipft_pshort; 7285 else if (ta->ipft_sz == sizeof(u_char)) 7286 tu.ipft_vchar = *ta->ipft_pchar; 7287 7288 tu.ipft_sz = ta->ipft_sz; 7289 tu.ipft_min = ta->ipft_min; 7290 tu.ipft_max = ta->ipft_max; 7291 tu.ipft_flags = ta->ipft_flags; 7292 bcopy(ta->ipft_name, tu.ipft_name, 7293 MIN(sizeof(tu.ipft_name), 7294 strlen(ta->ipft_name) + 1)); 7295 } 7296 error = ipf_outobj(softc, data, &tu, IPFOBJ_TUNEABLE); 7297 break; 7298 7299 case SIOCIPFGET : 7300 case SIOCIPFSET : 7301 /* 7302 * Search by name or by cookie value for a particular entry 7303 * in the tuning paramter table. 7304 */ 7305 IPFERROR(77); 7306 error = ESRCH; 7307 if (cookie != NULL) { 7308 ta = ipf_tune_findbycookie(&softc->ipf_tuners, 7309 cookie, NULL); 7310 if (ta != NULL) 7311 error = 0; 7312 } else if (tu.ipft_name[0] != '\0') { 7313 ta = ipf_tune_findbyname(softc->ipf_tuners, 7314 tu.ipft_name); 7315 if (ta != NULL) 7316 error = 0; 7317 } 7318 if (error != 0) 7319 break; 7320 7321 if (cmd == (ioctlcmd_t)SIOCIPFGET) { 7322 /* 7323 * Fetch the tuning parameters for a particular value 7324 */ 7325 tu.ipft_vlong = 0; 7326 if (ta->ipft_sz == sizeof(u_long)) 7327 tu.ipft_vlong = *ta->ipft_plong; 7328 else if (ta->ipft_sz == sizeof(u_int)) 7329 tu.ipft_vint = *ta->ipft_pint; 7330 else if (ta->ipft_sz == sizeof(u_short)) 7331 tu.ipft_vshort = *ta->ipft_pshort; 7332 else if (ta->ipft_sz == sizeof(u_char)) 7333 tu.ipft_vchar = *ta->ipft_pchar; 7334 tu.ipft_cookie = ta; 7335 tu.ipft_sz = ta->ipft_sz; 7336 tu.ipft_min = ta->ipft_min; 7337 tu.ipft_max = ta->ipft_max; 7338 tu.ipft_flags = ta->ipft_flags; 7339 error = ipf_outobj(softc, data, &tu, IPFOBJ_TUNEABLE); 7340 7341 } else if (cmd == (ioctlcmd_t)SIOCIPFSET) { 7342 /* 7343 * Set an internal parameter. The hard part here is 7344 * getting the new value safely and correctly out of 7345 * the kernel (given we only know its size, not type.) 7346 */ 7347 u_long in; 7348 7349 if (((ta->ipft_flags & IPFT_WRDISABLED) != 0) && 7350 (softc->ipf_running > 0)) { 7351 IPFERROR(78); 7352 error = EBUSY; 7353 break; 7354 } 7355 7356 in = tu.ipft_vlong; 7357 if (in < ta->ipft_min || in > ta->ipft_max) { 7358 IPFERROR(79); 7359 error = EINVAL; 7360 break; 7361 } 7362 7363 if (ta->ipft_func != NULL) { 7364 SPL_INT(s); 7365 7366 SPL_NET(s); 7367 error = (*ta->ipft_func)(softc, ta, 7368 &tu.ipft_un); 7369 SPL_X(s); 7370 7371 } else if (ta->ipft_sz == sizeof(u_long)) { 7372 tu.ipft_vlong = *ta->ipft_plong; 7373 *ta->ipft_plong = in; 7374 7375 } else if (ta->ipft_sz == sizeof(u_int)) { 7376 tu.ipft_vint = *ta->ipft_pint; 7377 *ta->ipft_pint = (u_int)(in & 0xffffffff); 7378 7379 } else if (ta->ipft_sz == sizeof(u_short)) { 7380 tu.ipft_vshort = *ta->ipft_pshort; 7381 *ta->ipft_pshort = (u_short)(in & 0xffff); 7382 7383 } else if (ta->ipft_sz == sizeof(u_char)) { 7384 tu.ipft_vchar = *ta->ipft_pchar; 7385 *ta->ipft_pchar = (u_char)(in & 0xff); 7386 } 7387 error = ipf_outobj(softc, data, &tu, IPFOBJ_TUNEABLE); 7388 } 7389 break; 7390 7391 default : 7392 IPFERROR(80); 7393 error = EINVAL; 7394 break; 7395 } 7396 7397 return error; 7398} 7399 7400 7401/* ------------------------------------------------------------------------ */ 7402/* Function: ipf_zerostats */ 7403/* Returns: int - 0 = success, else failure */ 7404/* Parameters: data(O) - pointer to pointer for copying data back to */ 7405/* */ 7406/* Copies the current statistics out to userspace and then zero's the */ 7407/* current ones in the kernel. The lock is only held across the bzero() as */ 7408/* the copyout may result in paging (ie network activity.) */ 7409/* ------------------------------------------------------------------------ */ 7410int 7411ipf_zerostats(softc, data) 7412 ipf_main_softc_t *softc; 7413 caddr_t data; 7414{ 7415 friostat_t fio; 7416 ipfobj_t obj; 7417 int error; 7418 7419 error = ipf_inobj(softc, data, &obj, &fio, IPFOBJ_IPFSTAT); 7420 if (error != 0) 7421 return error; 7422 ipf_getstat(softc, &fio, obj.ipfo_rev); 7423 error = ipf_outobj(softc, data, &fio, IPFOBJ_IPFSTAT); 7424 if (error != 0) 7425 return error; 7426 7427 WRITE_ENTER(&softc->ipf_mutex); 7428 bzero(&softc->ipf_stats, sizeof(softc->ipf_stats)); 7429 RWLOCK_EXIT(&softc->ipf_mutex); 7430 7431 return 0; 7432} 7433 7434 7435/* ------------------------------------------------------------------------ */ 7436/* Function: ipf_resolvedest */ 7437/* Returns: Nil */ 7438/* Parameters: softc(I) - pointer to soft context main structure */ 7439/* base(I) - where strings are stored */ 7440/* fdp(IO) - pointer to destination information to resolve */ 7441/* v(I) - IP protocol version to match */ 7442/* */ 7443/* Looks up an interface name in the frdest structure pointed to by fdp and */ 7444/* if a matching name can be found for the particular IP protocol version */ 7445/* then store the interface pointer in the frdest struct. If no match is */ 7446/* found, then set the interface pointer to be -1 as NULL is considered to */ 7447/* indicate there is no information at all in the structure. */ 7448/* ------------------------------------------------------------------------ */ 7449int 7450ipf_resolvedest(softc, base, fdp, v) 7451 ipf_main_softc_t *softc; 7452 char *base; 7453 frdest_t *fdp; 7454 int v; 7455{ 7456 int errval = 0; 7457 void *ifp; 7458 7459 ifp = NULL; 7460 7461 if (fdp->fd_name != -1) { 7462 if (fdp->fd_type == FRD_DSTLIST) { 7463 ifp = ipf_lookup_res_name(softc, IPL_LOGIPF, 7464 IPLT_DSTLIST, 7465 base + fdp->fd_name, 7466 NULL); 7467 if (ifp == NULL) { 7468 IPFERROR(144); 7469 errval = ESRCH; 7470 } 7471 } else { 7472 ifp = GETIFP(base + fdp->fd_name, v); 7473 if (ifp == NULL) 7474 ifp = (void *)-1; 7475 } 7476 } 7477 fdp->fd_ptr = ifp; 7478 7479 if ((ifp != NULL) && (ifp != (void *)-1)) { 7480 fdp->fd_local = ipf_deliverlocal(softc, v, ifp, &fdp->fd_ip6); 7481 } 7482 7483 return errval; 7484} 7485 7486 7487/* ------------------------------------------------------------------------ */ 7488/* Function: ipf_resolvenic */ 7489/* Returns: void* - NULL = wildcard name, -1 = failed to find NIC, else */ 7490/* pointer to interface structure for NIC */ 7491/* Parameters: softc(I)- pointer to soft context main structure */ 7492/* name(I) - complete interface name */ 7493/* v(I) - IP protocol version */ 7494/* */ 7495/* Look for a network interface structure that firstly has a matching name */ 7496/* to that passed in and that is also being used for that IP protocol */ 7497/* version (necessary on some platforms where there are separate listings */ 7498/* for both IPv4 and IPv6 on the same physical NIC. */ 7499/* ------------------------------------------------------------------------ */ 7500void * 7501ipf_resolvenic(softc, name, v) 7502 ipf_main_softc_t *softc; 7503 char *name; 7504 int v; 7505{ 7506 void *nic; 7507 7508 softc = softc; /* gcc -Wextra */ 7509 if (name[0] == '\0') 7510 return NULL; 7511 7512 if ((name[1] == '\0') && ((name[0] == '-') || (name[0] == '*'))) { 7513 return NULL; 7514 } 7515 7516 nic = GETIFP(name, v); 7517 if (nic == NULL) 7518 nic = (void *)-1; 7519 return nic; 7520} 7521 7522 7523/* ------------------------------------------------------------------------ */ 7524/* Function: ipf_token_expire */ 7525/* Returns: None. */ 7526/* Parameters: softc(I) - pointer to soft context main structure */ 7527/* */ 7528/* This function is run every ipf tick to see if there are any tokens that */ 7529/* have been held for too long and need to be freed up. */ 7530/* ------------------------------------------------------------------------ */ 7531void 7532ipf_token_expire(softc) 7533 ipf_main_softc_t *softc; 7534{ 7535 ipftoken_t *it; 7536 7537 WRITE_ENTER(&softc->ipf_tokens); 7538 while ((it = softc->ipf_token_head) != NULL) { 7539 if (it->ipt_die > softc->ipf_ticks) 7540 break; 7541 7542 ipf_token_deref(softc, it); 7543 } 7544 RWLOCK_EXIT(&softc->ipf_tokens); 7545} 7546 7547 7548/* ------------------------------------------------------------------------ */ 7549/* Function: ipf_token_flush */ 7550/* Returns: None. */ 7551/* Parameters: softc(I) - pointer to soft context main structure */ 7552/* */ 7553/* Loop through all of the existing tokens and call deref to see if they */ 7554/* can be freed. Normally a function like this might just loop on */ 7555/* ipf_token_head but there is a chance that a token might have a ref count */ 7556/* of greater than one and in that case the the reference would drop twice */ 7557/* by code that is only entitled to drop it once. */ 7558/* ------------------------------------------------------------------------ */ 7559static void 7560ipf_token_flush(softc) 7561 ipf_main_softc_t *softc; 7562{ 7563 ipftoken_t *it, *next; 7564 7565 WRITE_ENTER(&softc->ipf_tokens); 7566 for (it = softc->ipf_token_head; it != NULL; it = next) { 7567 next = it->ipt_next; 7568 (void) ipf_token_deref(softc, it); 7569 } 7570 RWLOCK_EXIT(&softc->ipf_tokens); 7571} 7572 7573 7574/* ------------------------------------------------------------------------ */ 7575/* Function: ipf_token_del */ 7576/* Returns: int - 0 = success, else error */ 7577/* Parameters: softc(I)- pointer to soft context main structure */ 7578/* type(I) - the token type to match */ 7579/* uid(I) - uid owning the token */ 7580/* ptr(I) - context pointer for the token */ 7581/* */ 7582/* This function looks for a a token in the current list that matches up */ 7583/* the fields (type, uid, ptr). If none is found, ESRCH is returned, else */ 7584/* call ipf_token_dewref() to remove it from the list. In the event that */ 7585/* the token has a reference held elsewhere, setting ipt_complete to 2 */ 7586/* enables debugging to distinguish between the two paths that ultimately */ 7587/* lead to a token to be deleted. */ 7588/* ------------------------------------------------------------------------ */ 7589int 7590ipf_token_del(softc, type, uid, ptr) 7591 ipf_main_softc_t *softc; 7592 int type, uid; 7593 void *ptr; 7594{ 7595 ipftoken_t *it; 7596 int error; 7597 7598 IPFERROR(82); 7599 error = ESRCH; 7600 7601 WRITE_ENTER(&softc->ipf_tokens); 7602 for (it = softc->ipf_token_head; it != NULL; it = it->ipt_next) { 7603 if (ptr == it->ipt_ctx && type == it->ipt_type && 7604 uid == it->ipt_uid) { 7605 it->ipt_complete = 2; 7606 ipf_token_deref(softc, it); 7607 error = 0; 7608 break; 7609 } 7610 } 7611 RWLOCK_EXIT(&softc->ipf_tokens); 7612 7613 return error; 7614} 7615 7616 7617/* ------------------------------------------------------------------------ */ 7618/* Function: ipf_token_mark_complete */ 7619/* Returns: None. */ 7620/* Parameters: token(I) - pointer to token structure */ 7621/* */ 7622/* Mark a token as being ineligable for being found with ipf_token_find. */ 7623/* ------------------------------------------------------------------------ */ 7624void 7625ipf_token_mark_complete(token) 7626 ipftoken_t *token; 7627{ 7628 if (token->ipt_complete == 0) 7629 token->ipt_complete = 1; 7630} 7631 7632 7633/* ------------------------------------------------------------------------ */ 7634/* Function: ipf_token_find */ 7635/* Returns: ipftoken_t * - NULL if no memory, else pointer to token */ 7636/* Parameters: softc(I)- pointer to soft context main structure */ 7637/* type(I) - the token type to match */ 7638/* uid(I) - uid owning the token */ 7639/* ptr(I) - context pointer for the token */ 7640/* */ 7641/* This function looks for a live token in the list of current tokens that */ 7642/* matches the tuple (type, uid, ptr). If one cannot be found then one is */ 7643/* allocated. If one is found then it is moved to the top of the list of */ 7644/* currently active tokens. */ 7645/* ------------------------------------------------------------------------ */ 7646ipftoken_t * 7647ipf_token_find(softc, type, uid, ptr) 7648 ipf_main_softc_t *softc; 7649 int type, uid; 7650 void *ptr; 7651{ 7652 ipftoken_t *it, *new; 7653 7654 KMALLOC(new, ipftoken_t *); 7655 if (new != NULL) 7656 bzero((char *)new, sizeof(*new)); 7657 7658 WRITE_ENTER(&softc->ipf_tokens); 7659 for (it = softc->ipf_token_head; it != NULL; it = it->ipt_next) { 7660 if ((ptr == it->ipt_ctx) && (type == it->ipt_type) && 7661 (uid == it->ipt_uid) && (it->ipt_complete < 2)) 7662 break; 7663 } 7664 7665 if (it == NULL) { 7666 it = new; 7667 new = NULL; 7668 if (it == NULL) { 7669 RWLOCK_EXIT(&softc->ipf_tokens); 7670 return NULL; 7671 } 7672 it->ipt_ctx = ptr; 7673 it->ipt_uid = uid; 7674 it->ipt_type = type; 7675 it->ipt_ref = 1; 7676 } else { 7677 if (new != NULL) { 7678 KFREE(new); 7679 new = NULL; 7680 } 7681 7682 if (it->ipt_complete > 0) 7683 it = NULL; 7684 else 7685 ipf_token_unlink(softc, it); 7686 } 7687 7688 if (it != NULL) { 7689 it->ipt_pnext = softc->ipf_token_tail; 7690 *softc->ipf_token_tail = it; 7691 softc->ipf_token_tail = &it->ipt_next; 7692 it->ipt_next = NULL; 7693 it->ipt_ref++; 7694 7695 it->ipt_die = softc->ipf_ticks + 20; 7696 } 7697 7698 RWLOCK_EXIT(&softc->ipf_tokens); 7699 7700 return it; 7701} 7702 7703 7704/* ------------------------------------------------------------------------ */ 7705/* Function: ipf_token_unlink */ 7706/* Returns: None. */ 7707/* Parameters: softc(I) - pointer to soft context main structure */ 7708/* token(I) - pointer to token structure */ 7709/* Write Locks: ipf_tokens */ 7710/* */ 7711/* This function unlinks a token structure from the linked list of tokens */ 7712/* that "own" it. The head pointer never needs to be explicitly adjusted */ 7713/* but the tail does due to the linked list implementation. */ 7714/* ------------------------------------------------------------------------ */ 7715static void 7716ipf_token_unlink(softc, token) 7717 ipf_main_softc_t *softc; 7718 ipftoken_t *token; 7719{ 7720 7721 if (softc->ipf_token_tail == &token->ipt_next) 7722 softc->ipf_token_tail = token->ipt_pnext; 7723 7724 *token->ipt_pnext = token->ipt_next; 7725 if (token->ipt_next != NULL) 7726 token->ipt_next->ipt_pnext = token->ipt_pnext; 7727 token->ipt_next = NULL; 7728 token->ipt_pnext = NULL; 7729} 7730 7731 7732/* ------------------------------------------------------------------------ */ 7733/* Function: ipf_token_deref */ 7734/* Returns: int - 0 == token freed, else reference count */ 7735/* Parameters: softc(I) - pointer to soft context main structure */ 7736/* token(I) - pointer to token structure */ 7737/* Write Locks: ipf_tokens */ 7738/* */ 7739/* Drop the reference count on the token structure and if it drops to zero, */ 7740/* call the dereference function for the token type because it is then */ 7741/* possible to free the token data structure. */ 7742/* ------------------------------------------------------------------------ */ 7743int 7744ipf_token_deref(softc, token) 7745 ipf_main_softc_t *softc; 7746 ipftoken_t *token; 7747{ 7748 void *data, **datap; 7749 7750 ASSERT(token->ipt_ref > 0); 7751 token->ipt_ref--; 7752 if (token->ipt_ref > 0) 7753 return token->ipt_ref; 7754 7755 data = token->ipt_data; 7756 datap = &data; 7757 7758 if ((data != NULL) && (data != (void *)-1)) { 7759 switch (token->ipt_type) 7760 { 7761 case IPFGENITER_IPF : 7762 (void) ipf_derefrule(softc, (frentry_t **)datap); 7763 break; 7764 case IPFGENITER_IPNAT : 7765 WRITE_ENTER(&softc->ipf_nat); 7766 ipf_nat_rule_deref(softc, (ipnat_t **)datap); 7767 RWLOCK_EXIT(&softc->ipf_nat); 7768 break; 7769 case IPFGENITER_NAT : 7770 ipf_nat_deref(softc, (nat_t **)datap); 7771 break; 7772 case IPFGENITER_STATE : 7773 ipf_state_deref(softc, (ipstate_t **)datap); 7774 break; 7775 case IPFGENITER_FRAG : 7776 ipf_frag_pkt_deref(softc, (ipfr_t **)datap); 7777 break; 7778 case IPFGENITER_NATFRAG : 7779 ipf_frag_nat_deref(softc, (ipfr_t **)datap); 7780 break; 7781 case IPFGENITER_HOSTMAP : 7782 WRITE_ENTER(&softc->ipf_nat); 7783 ipf_nat_hostmapdel(softc, (hostmap_t **)datap); 7784 RWLOCK_EXIT(&softc->ipf_nat); 7785 break; 7786 default : 7787 ipf_lookup_iterderef(softc, token->ipt_type, data); 7788 break; 7789 } 7790 } 7791 7792 ipf_token_unlink(softc, token); 7793 KFREE(token); 7794 return 0; 7795} 7796 7797 7798/* ------------------------------------------------------------------------ */ 7799/* Function: ipf_nextrule */ 7800/* Returns: frentry_t * - NULL == no more rules, else pointer to next */ 7801/* Parameters: softc(I) - pointer to soft context main structure */ 7802/* fr(I) - pointer to filter rule */ 7803/* out(I) - 1 == out rules, 0 == input rules */ 7804/* */ 7805/* Starting with "fr", find the next rule to visit. This includes visiting */ 7806/* the list of rule groups if either fr is NULL (empty list) or it is the */ 7807/* last rule in the list. When walking rule lists, it is either input or */ 7808/* output rules that are returned, never both. */ 7809/* ------------------------------------------------------------------------ */ 7810static frentry_t * 7811ipf_nextrule(softc, active, unit, fr, out) 7812 ipf_main_softc_t *softc; 7813 int active, unit; 7814 frentry_t *fr; 7815 int out; 7816{ 7817 frentry_t *next; 7818 frgroup_t *fg; 7819 7820 if (fr != NULL && fr->fr_group != -1) { 7821 fg = ipf_findgroup(softc, fr->fr_names + fr->fr_group, 7822 unit, active, NULL); 7823 if (fg != NULL) 7824 fg = fg->fg_next; 7825 } else { 7826 fg = softc->ipf_groups[unit][active]; 7827 } 7828 7829 while (fg != NULL) { 7830 next = fg->fg_start; 7831 while (next != NULL) { 7832 if (out) { 7833 if (next->fr_flags & FR_OUTQUE) 7834 return next; 7835 } else if (next->fr_flags & FR_INQUE) { 7836 return next; 7837 } 7838 next = next->fr_next; 7839 } 7840 if (next == NULL) 7841 fg = fg->fg_next; 7842 } 7843 7844 return NULL; 7845} 7846 7847/* ------------------------------------------------------------------------ */ 7848/* Function: ipf_getnextrule */ 7849/* Returns: int - 0 = success, else error */ 7850/* Parameters: softc(I)- pointer to soft context main structure */ 7851/* t(I) - pointer to destination information to resolve */ 7852/* ptr(I) - pointer to ipfobj_t to copyin from user space */ 7853/* */ 7854/* This function's first job is to bring in the ipfruleiter_t structure via */ 7855/* the ipfobj_t structure to determine what should be the next rule to */ 7856/* return. Once the ipfruleiter_t has been brought in, it then tries to */ 7857/* find the 'next rule'. This may include searching rule group lists or */ 7858/* just be as simple as looking at the 'next' field in the rule structure. */ 7859/* When we have found the rule to return, increase its reference count and */ 7860/* if we used an existing rule to get here, decrease its reference count. */ 7861/* ------------------------------------------------------------------------ */ 7862int 7863ipf_getnextrule(softc, t, ptr) 7864 ipf_main_softc_t *softc; 7865 ipftoken_t *t; 7866 void *ptr; 7867{ 7868 frentry_t *fr, *next, zero; 7869 ipfruleiter_t it; 7870 int error, out; 7871 frgroup_t *fg; 7872 ipfobj_t obj; 7873 int predict; 7874 char *dst; 7875 int unit; 7876 7877 if (t == NULL || ptr == NULL) { 7878 IPFERROR(84); 7879 return EFAULT; 7880 } 7881 7882 error = ipf_inobj(softc, ptr, &obj, &it, IPFOBJ_IPFITER); 7883 if (error != 0) 7884 return error; 7885 7886 if ((it.iri_inout < 0) || (it.iri_inout > 3)) { 7887 IPFERROR(85); 7888 return EINVAL; 7889 } 7890 if ((it.iri_active != 0) && (it.iri_active != 1)) { 7891 IPFERROR(86); 7892 return EINVAL; 7893 } 7894 if (it.iri_nrules == 0) { 7895 IPFERROR(87); 7896 return ENOSPC; 7897 } 7898 if (it.iri_rule == NULL) { 7899 IPFERROR(88); 7900 return EFAULT; 7901 } 7902 7903 fg = NULL; 7904 fr = t->ipt_data; 7905 if ((it.iri_inout & F_OUT) != 0) 7906 out = 1; 7907 else 7908 out = 0; 7909 if ((it.iri_inout & F_ACIN) != 0) 7910 unit = IPL_LOGCOUNT; 7911 else 7912 unit = IPL_LOGIPF; 7913 7914 READ_ENTER(&softc->ipf_mutex); 7915 if (fr == NULL) { 7916 if (*it.iri_group == '\0') { 7917 if (unit == IPL_LOGCOUNT) { 7918 next = softc->ipf_acct[out][it.iri_active]; 7919 } else { 7920 next = softc->ipf_rules[out][it.iri_active]; 7921 } 7922 if (next == NULL) 7923 next = ipf_nextrule(softc, it.iri_active, 7924 unit, NULL, out); 7925 } else { 7926 fg = ipf_findgroup(softc, it.iri_group, unit, 7927 it.iri_active, NULL); 7928 if (fg != NULL) 7929 next = fg->fg_start; 7930 else 7931 next = NULL; 7932 } 7933 } else { 7934 next = fr->fr_next; 7935 if (next == NULL) 7936 next = ipf_nextrule(softc, it.iri_active, unit, 7937 fr, out); 7938 } 7939 7940 if (next != NULL && next->fr_next != NULL) 7941 predict = 1; 7942 else if (ipf_nextrule(softc, it.iri_active, unit, next, out) != NULL) 7943 predict = 1; 7944 else 7945 predict = 0; 7946 7947 if (fr != NULL) 7948 (void) ipf_derefrule(softc, &fr); 7949 7950 obj.ipfo_type = IPFOBJ_FRENTRY; 7951 dst = (char *)it.iri_rule; 7952 7953 if (next != NULL) { 7954 obj.ipfo_size = next->fr_size; 7955 MUTEX_ENTER(&next->fr_lock); 7956 next->fr_ref++; 7957 MUTEX_EXIT(&next->fr_lock); 7958 t->ipt_data = next; 7959 } else { 7960 obj.ipfo_size = sizeof(frentry_t); 7961 bzero(&zero, sizeof(zero)); 7962 next = &zero; 7963 t->ipt_data = NULL; 7964 } 7965 it.iri_rule = predict ? next : NULL; 7966 if (predict == 0) 7967 ipf_token_mark_complete(t); 7968 7969 RWLOCK_EXIT(&softc->ipf_mutex); 7970 7971 obj.ipfo_ptr = dst; 7972 error = ipf_outobjk(softc, &obj, next); 7973 if (error == 0 && t->ipt_data != NULL) { 7974 dst += obj.ipfo_size; 7975 if (next->fr_data != NULL) { 7976 ipfobj_t dobj; 7977 7978 if (next->fr_type == FR_T_IPFEXPR) 7979 dobj.ipfo_type = IPFOBJ_IPFEXPR; 7980 else 7981 dobj.ipfo_type = IPFOBJ_FRIPF; 7982 dobj.ipfo_size = next->fr_dsize; 7983 dobj.ipfo_rev = obj.ipfo_rev; 7984 dobj.ipfo_ptr = dst; 7985 error = ipf_outobjk(softc, &dobj, next->fr_data); 7986 } 7987 } 7988 7989 if ((fr != NULL) && (next == &zero)) 7990 (void) ipf_derefrule(softc, &fr); 7991 7992 return error; 7993} 7994 7995 7996/* ------------------------------------------------------------------------ */ 7997/* Function: ipf_frruleiter */ 7998/* Returns: int - 0 = success, else error */ 7999/* Parameters: softc(I)- pointer to soft context main structure */ 8000/* data(I) - the token type to match */ 8001/* uid(I) - uid owning the token */ 8002/* ptr(I) - context pointer for the token */ 8003/* */ 8004/* This function serves as a stepping stone between ipf_ipf_ioctl and */ 8005/* ipf_getnextrule. It's role is to find the right token in the kernel for */ 8006/* the process doing the ioctl and use that to ask for the next rule. */ 8007/* ------------------------------------------------------------------------ */ 8008static int 8009ipf_frruleiter(softc, data, uid, ctx) 8010 ipf_main_softc_t *softc; 8011 void *data, *ctx; 8012 int uid; 8013{ 8014 ipftoken_t *token; 8015 ipfruleiter_t it; 8016 ipfobj_t obj; 8017 int error; 8018 8019 token = ipf_token_find(softc, IPFGENITER_IPF, uid, ctx); 8020 if (token != NULL) { 8021 error = ipf_getnextrule(softc, token, data); 8022 WRITE_ENTER(&softc->ipf_tokens); 8023 ipf_token_deref(softc, token); 8024 RWLOCK_EXIT(&softc->ipf_tokens); 8025 } else { 8026 error = ipf_inobj(softc, data, &obj, &it, IPFOBJ_IPFITER); 8027 if (error != 0) 8028 return error; 8029 it.iri_rule = NULL; 8030 error = ipf_outobj(softc, data, &it, IPFOBJ_IPFITER); 8031 } 8032 8033 return error; 8034} 8035 8036 8037/* ------------------------------------------------------------------------ */ 8038/* Function: ipf_geniter */ 8039/* Returns: int - 0 = success, else error */ 8040/* Parameters: softc(I) - pointer to soft context main structure */ 8041/* token(I) - pointer to ipftoken_t structure */ 8042/* itp(I) - pointer to iterator data */ 8043/* */ 8044/* Decide which iterator function to call using information passed through */ 8045/* the ipfgeniter_t structure at itp. */ 8046/* ------------------------------------------------------------------------ */ 8047static int 8048ipf_geniter(softc, token, itp) 8049 ipf_main_softc_t *softc; 8050 ipftoken_t *token; 8051 ipfgeniter_t *itp; 8052{ 8053 int error; 8054 8055 switch (itp->igi_type) 8056 { 8057 case IPFGENITER_FRAG : 8058 error = ipf_frag_pkt_next(softc, token, itp); 8059 break; 8060 default : 8061 IPFERROR(92); 8062 error = EINVAL; 8063 break; 8064 } 8065 8066 return error; 8067} 8068 8069 8070/* ------------------------------------------------------------------------ */ 8071/* Function: ipf_genericiter */ 8072/* Returns: int - 0 = success, else error */ 8073/* Parameters: softc(I)- pointer to soft context main structure */ 8074/* data(I) - the token type to match */ 8075/* uid(I) - uid owning the token */ 8076/* ptr(I) - context pointer for the token */ 8077/* */ 8078/* Handle the SIOCGENITER ioctl for the ipfilter device. The primary role */ 8079/* ------------------------------------------------------------------------ */ 8080int 8081ipf_genericiter(softc, data, uid, ctx) 8082 ipf_main_softc_t *softc; 8083 void *data, *ctx; 8084 int uid; 8085{ 8086 ipftoken_t *token; 8087 ipfgeniter_t iter; 8088 int error; 8089 8090 error = ipf_inobj(softc, data, NULL, &iter, IPFOBJ_GENITER); 8091 if (error != 0) 8092 return error; 8093 8094 token = ipf_token_find(softc, iter.igi_type, uid, ctx); 8095 if (token != NULL) { 8096 token->ipt_subtype = iter.igi_type; 8097 error = ipf_geniter(softc, token, &iter); 8098 WRITE_ENTER(&softc->ipf_tokens); 8099 ipf_token_deref(softc, token); 8100 RWLOCK_EXIT(&softc->ipf_tokens); 8101 } else { 8102 IPFERROR(93); 8103 error = 0; 8104 } 8105 8106 return error; 8107} 8108 8109 8110/* ------------------------------------------------------------------------ */ 8111/* Function: ipf_ipf_ioctl */ 8112/* Returns: int - 0 = success, else error */ 8113/* Parameters: softc(I)- pointer to soft context main structure */ 8114/* data(I) - the token type to match */ 8115/* cmd(I) - the ioctl command number */ 8116/* mode(I) - mode flags for the ioctl */ 8117/* uid(I) - uid owning the token */ 8118/* ptr(I) - context pointer for the token */ 8119/* */ 8120/* This function handles all of the ioctl command that are actually isssued */ 8121/* to the /dev/ipl device. */ 8122/* ------------------------------------------------------------------------ */ 8123int 8124ipf_ipf_ioctl(softc, data, cmd, mode, uid, ctx) 8125 ipf_main_softc_t *softc; 8126 caddr_t data; 8127 ioctlcmd_t cmd; 8128 int mode, uid; 8129 void *ctx; 8130{ 8131 friostat_t fio; 8132 int error, tmp; 8133 ipfobj_t obj; 8134 SPL_INT(s); 8135 8136 switch (cmd) 8137 { 8138 case SIOCFRENB : 8139 if (!(mode & FWRITE)) { 8140 IPFERROR(94); 8141 error = EPERM; 8142 } else { 8143 error = BCOPYIN(data, &tmp, sizeof(tmp)); 8144 if (error != 0) { 8145 IPFERROR(95); 8146 error = EFAULT; 8147 break; 8148 } 8149 8150 WRITE_ENTER(&softc->ipf_global); 8151 if (tmp) { 8152 if (softc->ipf_running > 0) 8153 error = 0; 8154 else 8155 error = ipfattach(softc); 8156 if (error == 0) 8157 softc->ipf_running = 1; 8158 else 8159 (void) ipfdetach(softc); 8160 } else { 8161 if (softc->ipf_running == 1) 8162 error = ipfdetach(softc); 8163 else 8164 error = 0; 8165 if (error == 0) 8166 softc->ipf_running = -1; 8167 } 8168 RWLOCK_EXIT(&softc->ipf_global); 8169 } 8170 break; 8171 8172 case SIOCIPFSET : 8173 if (!(mode & FWRITE)) { 8174 IPFERROR(96); 8175 error = EPERM; 8176 break; 8177 } 8178 /* FALLTHRU */ 8179 case SIOCIPFGETNEXT : 8180 case SIOCIPFGET : 8181 error = ipf_ipftune(softc, cmd, (void *)data); 8182 break; 8183 8184 case SIOCSETFF : 8185 if (!(mode & FWRITE)) { 8186 IPFERROR(97); 8187 error = EPERM; 8188 } else { 8189 error = BCOPYIN(data, &softc->ipf_flags, 8190 sizeof(softc->ipf_flags)); 8191 if (error != 0) { 8192 IPFERROR(98); 8193 error = EFAULT; 8194 } 8195 } 8196 break; 8197 8198 case SIOCGETFF : 8199 error = BCOPYOUT(&softc->ipf_flags, data, 8200 sizeof(softc->ipf_flags)); 8201 if (error != 0) { 8202 IPFERROR(99); 8203 error = EFAULT; 8204 } 8205 break; 8206 8207 case SIOCFUNCL : 8208 error = ipf_resolvefunc(softc, (void *)data); 8209 break; 8210 8211 case SIOCINAFR : 8212 case SIOCRMAFR : 8213 case SIOCADAFR : 8214 case SIOCZRLST : 8215 if (!(mode & FWRITE)) { 8216 IPFERROR(100); 8217 error = EPERM; 8218 } else { 8219 error = frrequest(softc, IPL_LOGIPF, cmd, (caddr_t)data, 8220 softc->ipf_active, 1); 8221 } 8222 break; 8223 8224 case SIOCINIFR : 8225 case SIOCRMIFR : 8226 case SIOCADIFR : 8227 if (!(mode & FWRITE)) { 8228 IPFERROR(101); 8229 error = EPERM; 8230 } else { 8231 error = frrequest(softc, IPL_LOGIPF, cmd, (caddr_t)data, 8232 1 - softc->ipf_active, 1); 8233 } 8234 break; 8235 8236 case SIOCSWAPA : 8237 if (!(mode & FWRITE)) { 8238 IPFERROR(102); 8239 error = EPERM; 8240 } else { 8241 WRITE_ENTER(&softc->ipf_mutex); 8242 error = BCOPYOUT(&softc->ipf_active, data, 8243 sizeof(softc->ipf_active)); 8244 if (error != 0) { 8245 IPFERROR(103); 8246 error = EFAULT; 8247 } else { 8248 softc->ipf_active = 1 - softc->ipf_active; 8249 } 8250 RWLOCK_EXIT(&softc->ipf_mutex); 8251 } 8252 break; 8253 8254 case SIOCGETFS : 8255 error = ipf_inobj(softc, (void *)data, &obj, &fio, 8256 IPFOBJ_IPFSTAT); 8257 if (error != 0) 8258 break; 8259 ipf_getstat(softc, &fio, obj.ipfo_rev); 8260 error = ipf_outobj(softc, (void *)data, &fio, IPFOBJ_IPFSTAT); 8261 break; 8262 8263 case SIOCFRZST : 8264 if (!(mode & FWRITE)) { 8265 IPFERROR(104); 8266 error = EPERM; 8267 } else 8268 error = ipf_zerostats(softc, (caddr_t)data); 8269 break; 8270 8271 case SIOCIPFFL : 8272 if (!(mode & FWRITE)) { 8273 IPFERROR(105); 8274 error = EPERM; 8275 } else { 8276 error = BCOPYIN(data, &tmp, sizeof(tmp)); 8277 if (!error) { 8278 tmp = ipf_flush(softc, IPL_LOGIPF, tmp); 8279 error = BCOPYOUT(&tmp, data, sizeof(tmp)); 8280 if (error != 0) { 8281 IPFERROR(106); 8282 error = EFAULT; 8283 } 8284 } else { 8285 IPFERROR(107); 8286 error = EFAULT; 8287 } 8288 } 8289 break; 8290 8291#ifdef USE_INET6 8292 case SIOCIPFL6 : 8293 if (!(mode & FWRITE)) { 8294 IPFERROR(108); 8295 error = EPERM; 8296 } else { 8297 error = BCOPYIN(data, &tmp, sizeof(tmp)); 8298 if (!error) { 8299 tmp = ipf_flush(softc, IPL_LOGIPF, tmp); 8300 error = BCOPYOUT(&tmp, data, sizeof(tmp)); 8301 if (error != 0) { 8302 IPFERROR(109); 8303 error = EFAULT; 8304 } 8305 } else { 8306 IPFERROR(110); 8307 error = EFAULT; 8308 } 8309 } 8310 break; 8311#endif 8312 8313 case SIOCSTLCK : 8314 if (!(mode & FWRITE)) { 8315 IPFERROR(122); 8316 error = EPERM; 8317 } else { 8318 error = BCOPYIN(data, &tmp, sizeof(tmp)); 8319 if (error == 0) { 8320 ipf_state_setlock(softc->ipf_state_soft, tmp); 8321 ipf_nat_setlock(softc->ipf_nat_soft, tmp); 8322 ipf_frag_setlock(softc->ipf_frag_soft, tmp); 8323 ipf_auth_setlock(softc->ipf_auth_soft, tmp); 8324 } else { 8325 IPFERROR(111); 8326 error = EFAULT; 8327 } 8328 } 8329 break; 8330 8331#ifdef IPFILTER_LOG 8332 case SIOCIPFFB : 8333 if (!(mode & FWRITE)) { 8334 IPFERROR(112); 8335 error = EPERM; 8336 } else { 8337 tmp = ipf_log_clear(softc, IPL_LOGIPF); 8338 error = BCOPYOUT(&tmp, data, sizeof(tmp)); 8339 if (error) { 8340 IPFERROR(113); 8341 error = EFAULT; 8342 } 8343 } 8344 break; 8345#endif /* IPFILTER_LOG */ 8346 8347 case SIOCFRSYN : 8348 if (!(mode & FWRITE)) { 8349 IPFERROR(114); 8350 error = EPERM; 8351 } else { 8352 WRITE_ENTER(&softc->ipf_global); 8353#if (defined(MENTAT) && defined(_KERNEL)) && !defined(INSTANCES) 8354 error = ipfsync(); 8355#else 8356 ipf_sync(softc, NULL); 8357 error = 0; 8358#endif 8359 RWLOCK_EXIT(&softc->ipf_global); 8360 8361 } 8362 break; 8363 8364 case SIOCGFRST : 8365 error = ipf_outobj(softc, (void *)data, 8366 ipf_frag_stats(softc->ipf_frag_soft), 8367 IPFOBJ_FRAGSTAT); 8368 break; 8369 8370#ifdef IPFILTER_LOG 8371 case FIONREAD : 8372 tmp = ipf_log_bytesused(softc, IPL_LOGIPF); 8373 error = BCOPYOUT(&tmp, data, sizeof(tmp)); 8374 break; 8375#endif 8376 8377 case SIOCIPFITER : 8378 SPL_SCHED(s); 8379 error = ipf_frruleiter(softc, data, uid, ctx); 8380 SPL_X(s); 8381 break; 8382 8383 case SIOCGENITER : 8384 SPL_SCHED(s); 8385 error = ipf_genericiter(softc, data, uid, ctx); 8386 SPL_X(s); 8387 break; 8388 8389 case SIOCIPFDELTOK : 8390 error = BCOPYIN(data, &tmp, sizeof(tmp)); 8391 if (error == 0) { 8392 SPL_SCHED(s); 8393 error = ipf_token_del(softc, tmp, uid, ctx); 8394 SPL_X(s); 8395 } 8396 break; 8397 8398 default : 8399 IPFERROR(115); 8400 error = EINVAL; 8401 break; 8402 } 8403 8404 return error; 8405} 8406 8407 8408/* ------------------------------------------------------------------------ */ 8409/* Function: ipf_decaps */ 8410/* Returns: int - -1 == decapsulation failed, else bit mask of */ 8411/* flags indicating packet filtering decision. */ 8412/* Parameters: fin(I) - pointer to packet information */ 8413/* pass(I) - IP protocol version to match */ 8414/* l5proto(I) - layer 5 protocol to decode UDP data as. */ 8415/* */ 8416/* This function is called for packets that are wrapt up in other packets, */ 8417/* for example, an IP packet that is the entire data segment for another IP */ 8418/* packet. If the basic constraints for this are satisfied, change the */ 8419/* buffer to point to the start of the inner packet and start processing */ 8420/* rules belonging to the head group this rule specifies. */ 8421/* ------------------------------------------------------------------------ */ 8422u_32_t 8423ipf_decaps(fin, pass, l5proto) 8424 fr_info_t *fin; 8425 u_32_t pass; 8426 int l5proto; 8427{ 8428 fr_info_t fin2, *fino = NULL; 8429 int elen, hlen, nh; 8430 grehdr_t gre; 8431 ip_t *ip; 8432 mb_t *m; 8433 8434 if ((fin->fin_flx & FI_COALESCE) == 0) 8435 if (ipf_coalesce(fin) == -1) 8436 goto cantdecaps; 8437 8438 m = fin->fin_m; 8439 hlen = fin->fin_hlen; 8440 8441 switch (fin->fin_p) 8442 { 8443 case IPPROTO_UDP : 8444 /* 8445 * In this case, the specific protocol being decapsulated 8446 * inside UDP frames comes from the rule. 8447 */ 8448 nh = fin->fin_fr->fr_icode; 8449 break; 8450 8451 case IPPROTO_GRE : /* 47 */ 8452 bcopy(fin->fin_dp, (char *)&gre, sizeof(gre)); 8453 hlen += sizeof(grehdr_t); 8454 if (gre.gr_R|gre.gr_s) 8455 goto cantdecaps; 8456 if (gre.gr_C) 8457 hlen += 4; 8458 if (gre.gr_K) 8459 hlen += 4; 8460 if (gre.gr_S) 8461 hlen += 4; 8462 8463 nh = IPPROTO_IP; 8464 8465 /* 8466 * If the routing options flag is set, validate that it is 8467 * there and bounce over it. 8468 */ 8469#if 0 8470 /* This is really heavy weight and lots of room for error, */ 8471 /* so for now, put it off and get the simple stuff right. */ 8472 if (gre.gr_R) { 8473 u_char off, len, *s; 8474 u_short af; 8475 int end; 8476 8477 end = 0; 8478 s = fin->fin_dp; 8479 s += hlen; 8480 aplen = fin->fin_plen - hlen; 8481 while (aplen > 3) { 8482 af = (s[0] << 8) | s[1]; 8483 off = s[2]; 8484 len = s[3]; 8485 aplen -= 4; 8486 s += 4; 8487 if (af == 0 && len == 0) { 8488 end = 1; 8489 break; 8490 } 8491 if (aplen < len) 8492 break; 8493 s += len; 8494 aplen -= len; 8495 } 8496 if (end != 1) 8497 goto cantdecaps; 8498 hlen = s - (u_char *)fin->fin_dp; 8499 } 8500#endif 8501 break; 8502 8503#ifdef IPPROTO_IPIP 8504 case IPPROTO_IPIP : /* 4 */ 8505#endif 8506 nh = IPPROTO_IP; 8507 break; 8508 8509 default : /* Includes ESP, AH is special for IPv4 */ 8510 goto cantdecaps; 8511 } 8512 8513 switch (nh) 8514 { 8515 case IPPROTO_IP : 8516 case IPPROTO_IPV6 : 8517 break; 8518 default : 8519 goto cantdecaps; 8520 } 8521 8522 bcopy((char *)fin, (char *)&fin2, sizeof(fin2)); 8523 fino = fin; 8524 fin = &fin2; 8525 elen = hlen; 8526#if defined(MENTAT) && defined(_KERNEL) 8527 m->b_rptr += elen; 8528#else 8529 m->m_data += elen; 8530 m->m_len -= elen; 8531#endif 8532 fin->fin_plen -= elen; 8533 8534 ip = (ip_t *)((char *)fin->fin_ip + elen); 8535 8536 /* 8537 * Make sure we have at least enough data for the network layer 8538 * header. 8539 */ 8540 if (IP_V(ip) == 4) 8541 hlen = IP_HL(ip) << 2; 8542#ifdef USE_INET6 8543 else if (IP_V(ip) == 6) 8544 hlen = sizeof(ip6_t); 8545#endif 8546 else 8547 goto cantdecaps2; 8548 8549 if (fin->fin_plen < hlen) 8550 goto cantdecaps2; 8551 8552 fin->fin_dp = (char *)ip + hlen; 8553 8554 if (IP_V(ip) == 4) { 8555 /* 8556 * Perform IPv4 header checksum validation. 8557 */ 8558 if (ipf_cksum((u_short *)ip, hlen)) 8559 goto cantdecaps2; 8560 } 8561 8562 if (ipf_makefrip(hlen, ip, fin) == -1) { 8563cantdecaps2: 8564 if (m != NULL) { 8565#if defined(MENTAT) && defined(_KERNEL) 8566 m->b_rptr -= elen; 8567#else 8568 m->m_data -= elen; 8569 m->m_len += elen; 8570#endif 8571 } 8572cantdecaps: 8573 DT1(frb_decapfrip, fr_info_t *, fin); 8574 pass &= ~FR_CMDMASK; 8575 pass |= FR_BLOCK|FR_QUICK; 8576 fin->fin_reason = FRB_DECAPFRIP; 8577 return -1; 8578 } 8579 8580 pass = ipf_scanlist(fin, pass); 8581 8582 /* 8583 * Copy the packet filter "result" fields out of the fr_info_t struct 8584 * that is local to the decapsulation processing and back into the 8585 * one we were called with. 8586 */ 8587 fino->fin_flx = fin->fin_flx; 8588 fino->fin_rev = fin->fin_rev; 8589 fino->fin_icode = fin->fin_icode; 8590 fino->fin_rule = fin->fin_rule; 8591 (void) strncpy(fino->fin_group, fin->fin_group, FR_GROUPLEN); 8592 fino->fin_fr = fin->fin_fr; 8593 fino->fin_error = fin->fin_error; 8594 fino->fin_mp = fin->fin_mp; 8595 fino->fin_m = fin->fin_m; 8596 m = fin->fin_m; 8597 if (m != NULL) { 8598#if defined(MENTAT) && defined(_KERNEL) 8599 m->b_rptr -= elen; 8600#else 8601 m->m_data -= elen; 8602 m->m_len += elen; 8603#endif 8604 } 8605 return pass; 8606} 8607 8608 8609/* ------------------------------------------------------------------------ */ 8610/* Function: ipf_matcharray_load */ 8611/* Returns: int - 0 = success, else error */ 8612/* Parameters: softc(I) - pointer to soft context main structure */ 8613/* data(I) - pointer to ioctl data */ 8614/* objp(I) - ipfobj_t structure to load data into */ 8615/* arrayptr(I) - pointer to location to store array pointer */ 8616/* */ 8617/* This function loads in a mathing array through the ipfobj_t struct that */ 8618/* describes it. Sanity checking and array size limitations are enforced */ 8619/* in this function to prevent userspace from trying to load in something */ 8620/* that is insanely big. Once the size of the array is known, the memory */ 8621/* required is malloc'd and returned through changing *arrayptr. The */ 8622/* contents of the array are verified before returning. Only in the event */ 8623/* of a successful call is the caller required to free up the malloc area. */ 8624/* ------------------------------------------------------------------------ */ 8625int 8626ipf_matcharray_load(softc, data, objp, arrayptr) 8627 ipf_main_softc_t *softc; 8628 caddr_t data; 8629 ipfobj_t *objp; 8630 int **arrayptr; 8631{ 8632 int arraysize, *array, error; 8633 8634 *arrayptr = NULL; 8635 8636 error = BCOPYIN(data, objp, sizeof(*objp)); 8637 if (error != 0) { 8638 IPFERROR(116); 8639 return EFAULT; 8640 } 8641 8642 if (objp->ipfo_type != IPFOBJ_IPFEXPR) { 8643 IPFERROR(117); 8644 return EINVAL; 8645 } 8646 8647 if (((objp->ipfo_size & 3) != 0) || (objp->ipfo_size == 0) || 8648 (objp->ipfo_size > 1024)) { 8649 IPFERROR(118); 8650 return EINVAL; 8651 } 8652 8653 arraysize = objp->ipfo_size * sizeof(*array); 8654 KMALLOCS(array, int *, arraysize); 8655 if (array == NULL) { 8656 IPFERROR(119); 8657 return ENOMEM; 8658 } 8659 8660 error = COPYIN(objp->ipfo_ptr, array, arraysize); 8661 if (error != 0) { 8662 KFREES(array, arraysize); 8663 IPFERROR(120); 8664 return EFAULT; 8665 } 8666 8667 if (ipf_matcharray_verify(array, arraysize) != 0) { 8668 KFREES(array, arraysize); 8669 IPFERROR(121); 8670 return EINVAL; 8671 } 8672 8673 *arrayptr = array; 8674 return 0; 8675} 8676 8677 8678/* ------------------------------------------------------------------------ */ 8679/* Function: ipf_matcharray_verify */ 8680/* Returns: Nil */ 8681/* Parameters: array(I) - pointer to matching array */ 8682/* arraysize(I) - number of elements in the array */ 8683/* */ 8684/* Verify the contents of a matching array by stepping through each element */ 8685/* in it. The actual commands in the array are not verified for */ 8686/* correctness, only that all of the sizes are correctly within limits. */ 8687/* ------------------------------------------------------------------------ */ 8688int 8689ipf_matcharray_verify(array, arraysize) 8690 int *array, arraysize; 8691{ 8692 int i, nelem, maxidx; 8693 ipfexp_t *e; 8694 8695 nelem = arraysize / sizeof(*array); 8696 8697 /* 8698 * Currently, it makes no sense to have an array less than 6 8699 * elements long - the initial size at the from, a single operation 8700 * (minimum 4 in length) and a trailer, for a total of 6. 8701 */ 8702 if ((array[0] < 6) || (arraysize < 24) || (arraysize > 4096)) { 8703 return -1; 8704 } 8705 8706 /* 8707 * Verify the size of data pointed to by array with how long 8708 * the array claims to be itself. 8709 */ 8710 if (array[0] * sizeof(*array) != arraysize) { 8711 return -1; 8712 } 8713 8714 maxidx = nelem - 1; 8715 /* 8716 * The last opcode in this array should be an IPF_EXP_END. 8717 */ 8718 if (array[maxidx] != IPF_EXP_END) { 8719 return -1; 8720 } 8721 8722 for (i = 1; i < maxidx; ) { 8723 e = (ipfexp_t *)(array + i); 8724 8725 /* 8726 * The length of the bits to check must be at least 1 8727 * (or else there is nothing to comapre with!) and it 8728 * cannot exceed the length of the data present. 8729 */ 8730 if ((e->ipfe_size < 1 ) || 8731 (e->ipfe_size + i > maxidx)) { 8732 return -1; 8733 } 8734 i += e->ipfe_size; 8735 } 8736 return 0; 8737} 8738 8739 8740/* ------------------------------------------------------------------------ */ 8741/* Function: ipf_fr_matcharray */ 8742/* Returns: int - 0 = match failed, else positive match */ 8743/* Parameters: fin(I) - pointer to packet information */ 8744/* array(I) - pointer to matching array */ 8745/* */ 8746/* This function is used to apply a matching array against a packet and */ 8747/* return an indication of whether or not the packet successfully matches */ 8748/* all of the commands in it. */ 8749/* ------------------------------------------------------------------------ */ 8750static int 8751ipf_fr_matcharray(fin, array) 8752 fr_info_t *fin; 8753 int *array; 8754{ 8755 int i, n, *x, rv, p; 8756 ipfexp_t *e; 8757 8758 rv = 0; 8759 n = array[0]; 8760 x = array + 1; 8761 8762 for (; n > 0; x += 3 + x[3], rv = 0) { 8763 e = (ipfexp_t *)x; 8764 if (e->ipfe_cmd == IPF_EXP_END) 8765 break; 8766 n -= e->ipfe_size; 8767 8768 /* 8769 * The upper 16 bits currently store the protocol value. 8770 * This is currently used with TCP and UDP port compares and 8771 * allows "tcp.port = 80" without requiring an explicit 8772 " "ip.pr = tcp" first. 8773 */ 8774 p = e->ipfe_cmd >> 16; 8775 if ((p != 0) && (p != fin->fin_p)) 8776 break; 8777 8778 switch (e->ipfe_cmd) 8779 { 8780 case IPF_EXP_IP_PR : 8781 for (i = 0; !rv && i < e->ipfe_narg; i++) { 8782 rv |= (fin->fin_p == e->ipfe_arg0[i]); 8783 } 8784 break; 8785 8786 case IPF_EXP_IP_SRCADDR : 8787 if (fin->fin_v != 4) 8788 break; 8789 for (i = 0; !rv && i < e->ipfe_narg; i++) { 8790 rv |= ((fin->fin_saddr & 8791 e->ipfe_arg0[i * 2 + 1]) == 8792 e->ipfe_arg0[i * 2]); 8793 } 8794 break; 8795 8796 case IPF_EXP_IP_DSTADDR : 8797 if (fin->fin_v != 4) 8798 break; 8799 for (i = 0; !rv && i < e->ipfe_narg; i++) { 8800 rv |= ((fin->fin_daddr & 8801 e->ipfe_arg0[i * 2 + 1]) == 8802 e->ipfe_arg0[i * 2]); 8803 } 8804 break; 8805 8806 case IPF_EXP_IP_ADDR : 8807 if (fin->fin_v != 4) 8808 break; 8809 for (i = 0; !rv && i < e->ipfe_narg; i++) { 8810 rv |= ((fin->fin_saddr & 8811 e->ipfe_arg0[i * 2 + 1]) == 8812 e->ipfe_arg0[i * 2]) || 8813 ((fin->fin_daddr & 8814 e->ipfe_arg0[i * 2 + 1]) == 8815 e->ipfe_arg0[i * 2]); 8816 } 8817 break; 8818 8819#ifdef USE_INET6 8820 case IPF_EXP_IP6_SRCADDR : 8821 if (fin->fin_v != 6) 8822 break; 8823 for (i = 0; !rv && i < e->ipfe_narg; i++) { 8824 rv |= IP6_MASKEQ(&fin->fin_src6, 8825 &e->ipfe_arg0[i * 8 + 4], 8826 &e->ipfe_arg0[i * 8]); 8827 } 8828 break; 8829 8830 case IPF_EXP_IP6_DSTADDR : 8831 if (fin->fin_v != 6) 8832 break; 8833 for (i = 0; !rv && i < e->ipfe_narg; i++) { 8834 rv |= IP6_MASKEQ(&fin->fin_dst6, 8835 &e->ipfe_arg0[i * 8 + 4], 8836 &e->ipfe_arg0[i * 8]); 8837 } 8838 break; 8839 8840 case IPF_EXP_IP6_ADDR : 8841 if (fin->fin_v != 6) 8842 break; 8843 for (i = 0; !rv && i < e->ipfe_narg; i++) { 8844 rv |= IP6_MASKEQ(&fin->fin_src6, 8845 &e->ipfe_arg0[i * 8 + 4], 8846 &e->ipfe_arg0[i * 8]) || 8847 IP6_MASKEQ(&fin->fin_dst6, 8848 &e->ipfe_arg0[i * 8 + 4], 8849 &e->ipfe_arg0[i * 8]); 8850 } 8851 break; 8852#endif 8853 8854 case IPF_EXP_UDP_PORT : 8855 case IPF_EXP_TCP_PORT : 8856 for (i = 0; !rv && i < e->ipfe_narg; i++) { 8857 rv |= (fin->fin_sport == e->ipfe_arg0[i]) || 8858 (fin->fin_dport == e->ipfe_arg0[i]); 8859 } 8860 break; 8861 8862 case IPF_EXP_UDP_SPORT : 8863 case IPF_EXP_TCP_SPORT : 8864 for (i = 0; !rv && i < e->ipfe_narg; i++) { 8865 rv |= (fin->fin_sport == e->ipfe_arg0[i]); 8866 } 8867 break; 8868 8869 case IPF_EXP_UDP_DPORT : 8870 case IPF_EXP_TCP_DPORT : 8871 for (i = 0; !rv && i < e->ipfe_narg; i++) { 8872 rv |= (fin->fin_dport == e->ipfe_arg0[i]); 8873 } 8874 break; 8875 8876 case IPF_EXP_TCP_FLAGS : 8877 for (i = 0; !rv && i < e->ipfe_narg; i++) { 8878 rv |= ((fin->fin_tcpf & 8879 e->ipfe_arg0[i * 2 + 1]) == 8880 e->ipfe_arg0[i * 2]); 8881 } 8882 break; 8883 } 8884 rv ^= e->ipfe_not; 8885 8886 if (rv == 0) 8887 break; 8888 } 8889 8890 return rv; 8891} 8892 8893 8894/* ------------------------------------------------------------------------ */ 8895/* Function: ipf_queueflush */ 8896/* Returns: int - number of entries flushed (0 = none) */ 8897/* Parameters: softc(I) - pointer to soft context main structure */ 8898/* deletefn(I) - function to call to delete entry */ 8899/* ipfqs(I) - top of the list of ipf internal queues */ 8900/* userqs(I) - top of the list of user defined timeouts */ 8901/* */ 8902/* This fucntion gets called when the state/NAT hash tables fill up and we */ 8903/* need to try a bit harder to free up some space. The algorithm used here */ 8904/* split into two parts but both halves have the same goal: to reduce the */ 8905/* number of connections considered to be "active" to the low watermark. */ 8906/* There are two steps in doing this: */ 8907/* 1) Remove any TCP connections that are already considered to be "closed" */ 8908/* but have not yet been removed from the state table. The two states */ 8909/* TCPS_TIME_WAIT and TCPS_CLOSED are considered to be the perfect */ 8910/* candidates for this style of removal. If freeing up entries in */ 8911/* CLOSED or both CLOSED and TIME_WAIT brings us to the low watermark, */ 8912/* we do not go on to step 2. */ 8913/* */ 8914/* 2) Look for the oldest entries on each timeout queue and free them if */ 8915/* they are within the given window we are considering. Where the */ 8916/* window starts and the steps taken to increase its size depend upon */ 8917/* how long ipf has been running (ipf_ticks.) Anything modified in the */ 8918/* last 30 seconds is not touched. */ 8919/* touched */ 8920/* die ipf_ticks 30*1.5 1800*1.5 | 43200*1.5 */ 8921/* | | | | | | */ 8922/* future <--+----------+--------+-----------+-----+-----+-----------> past */ 8923/* now \_int=30s_/ \_int=1hr_/ \_int=12hr */ 8924/* */ 8925/* Points to note: */ 8926/* - tqe_die is the time, in the future, when entries die. */ 8927/* - tqe_die - ipf_ticks is how long left the connection has to live in ipf */ 8928/* ticks. */ 8929/* - tqe_touched is when the entry was last used by NAT/state */ 8930/* - the closer tqe_touched is to ipf_ticks, the further tqe_die will be */ 8931/* ipf_ticks any given timeout queue and vice versa. */ 8932/* - both tqe_die and tqe_touched increase over time */ 8933/* - timeout queues are sorted with the highest value of tqe_die at the */ 8934/* bottom and therefore the smallest values of each are at the top */ 8935/* - the pointer passed in as ipfqs should point to an array of timeout */ 8936/* queues representing each of the TCP states */ 8937/* */ 8938/* We start by setting up a maximum range to scan for things to move of */ 8939/* iend (newest) to istart (oldest) in chunks of "interval". If nothing is */ 8940/* found in that range, "interval" is adjusted (so long as it isn't 30) and */ 8941/* we start again with a new value for "iend" and "istart". This is */ 8942/* continued until we either finish the scan of 30 second intervals or the */ 8943/* low water mark is reached. */ 8944/* ------------------------------------------------------------------------ */ 8945int 8946ipf_queueflush(softc, deletefn, ipfqs, userqs, activep, size, low) 8947 ipf_main_softc_t *softc; 8948 ipftq_delete_fn_t deletefn; 8949 ipftq_t *ipfqs, *userqs; 8950 u_int *activep; 8951 int size, low; 8952{ 8953 u_long interval, istart, iend; 8954 ipftq_t *ifq, *ifqnext; 8955 ipftqent_t *tqe, *tqn; 8956 int removed = 0; 8957 8958 for (tqn = ipfqs[IPF_TCPS_CLOSED].ifq_head; ((tqe = tqn) != NULL); ) { 8959 tqn = tqe->tqe_next; 8960 if ((*deletefn)(softc, tqe->tqe_parent) == 0) 8961 removed++; 8962 } 8963 if ((*activep * 100 / size) > low) { 8964 for (tqn = ipfqs[IPF_TCPS_TIME_WAIT].ifq_head; 8965 ((tqe = tqn) != NULL); ) { 8966 tqn = tqe->tqe_next; 8967 if ((*deletefn)(softc, tqe->tqe_parent) == 0) 8968 removed++; 8969 } 8970 } 8971 8972 if ((*activep * 100 / size) <= low) { 8973 return removed; 8974 } 8975 8976 /* 8977 * NOTE: Use of "* 15 / 10" is required here because if "* 1.5" is 8978 * used then the operations are upgraded to floating point 8979 * and kernels don't like floating point... 8980 */ 8981 if (softc->ipf_ticks > IPF_TTLVAL(43200 * 15 / 10)) { 8982 istart = IPF_TTLVAL(86400 * 4); 8983 interval = IPF_TTLVAL(43200); 8984 } else if (softc->ipf_ticks > IPF_TTLVAL(1800 * 15 / 10)) { 8985 istart = IPF_TTLVAL(43200); 8986 interval = IPF_TTLVAL(1800); 8987 } else if (softc->ipf_ticks > IPF_TTLVAL(30 * 15 / 10)) { 8988 istart = IPF_TTLVAL(1800); 8989 interval = IPF_TTLVAL(30); 8990 } else { 8991 return 0; 8992 } 8993 if (istart > softc->ipf_ticks) { 8994 if (softc->ipf_ticks - interval < interval) 8995 istart = interval; 8996 else 8997 istart = (softc->ipf_ticks / interval) * interval; 8998 } 8999 9000 iend = softc->ipf_ticks - interval; 9001 9002 while ((*activep * 100 / size) > low) { 9003 u_long try; 9004 9005 try = softc->ipf_ticks - istart; 9006 9007 for (ifq = ipfqs; ifq != NULL; ifq = ifq->ifq_next) { 9008 for (tqn = ifq->ifq_head; ((tqe = tqn) != NULL); ) { 9009 if (try < tqe->tqe_touched) 9010 break; 9011 tqn = tqe->tqe_next; 9012 if ((*deletefn)(softc, tqe->tqe_parent) == 0) 9013 removed++; 9014 } 9015 } 9016 9017 for (ifq = userqs; ifq != NULL; ifq = ifqnext) { 9018 ifqnext = ifq->ifq_next; 9019 9020 for (tqn = ifq->ifq_head; ((tqe = tqn) != NULL); ) { 9021 if (try < tqe->tqe_touched) 9022 break; 9023 tqn = tqe->tqe_next; 9024 if ((*deletefn)(softc, tqe->tqe_parent) == 0) 9025 removed++; 9026 } 9027 } 9028 9029 if (try >= iend) { 9030 if (interval == IPF_TTLVAL(43200)) { 9031 interval = IPF_TTLVAL(1800); 9032 } else if (interval == IPF_TTLVAL(1800)) { 9033 interval = IPF_TTLVAL(30); 9034 } else { 9035 break; 9036 } 9037 if (interval >= softc->ipf_ticks) 9038 break; 9039 9040 iend = softc->ipf_ticks - interval; 9041 } 9042 istart -= interval; 9043 } 9044 9045 return removed; 9046} 9047 9048 9049/* ------------------------------------------------------------------------ */ 9050/* Function: ipf_deliverlocal */ 9051/* Returns: int - 1 = local address, 0 = non-local address */ 9052/* Parameters: softc(I) - pointer to soft context main structure */ 9053/* ipversion(I) - IP protocol version (4 or 6) */ 9054/* ifp(I) - network interface pointer */ 9055/* ipaddr(I) - IPv4/6 destination address */ 9056/* */ 9057/* This fucntion is used to determine in the address "ipaddr" belongs to */ 9058/* the network interface represented by ifp. */ 9059/* ------------------------------------------------------------------------ */ 9060int 9061ipf_deliverlocal(softc, ipversion, ifp, ipaddr) 9062 ipf_main_softc_t *softc; 9063 int ipversion; 9064 void *ifp; 9065 i6addr_t *ipaddr; 9066{ 9067 i6addr_t addr; 9068 int islocal = 0; 9069 9070 if (ipversion == 4) { 9071 if (ipf_ifpaddr(softc, 4, FRI_NORMAL, ifp, &addr, NULL) == 0) { 9072 if (addr.in4.s_addr == ipaddr->in4.s_addr) 9073 islocal = 1; 9074 } 9075 9076#ifdef USE_INET6 9077 } else if (ipversion == 6) { 9078 if (ipf_ifpaddr(softc, 6, FRI_NORMAL, ifp, &addr, NULL) == 0) { 9079 if (IP6_EQ(&addr, ipaddr)) 9080 islocal = 1; 9081 } 9082#endif 9083 } 9084 9085 return islocal; 9086} 9087 9088 9089/* ------------------------------------------------------------------------ */ 9090/* Function: ipf_settimeout */ 9091/* Returns: int - 0 = success, -1 = failure */ 9092/* Parameters: softc(I) - pointer to soft context main structure */ 9093/* t(I) - pointer to tuneable array entry */ 9094/* p(I) - pointer to values passed in to apply */ 9095/* */ 9096/* This function is called to set the timeout values for each distinct */ 9097/* queue timeout that is available. When called, it calls into both the */ 9098/* state and NAT code, telling them to update their timeout queues. */ 9099/* ------------------------------------------------------------------------ */ 9100static int 9101ipf_settimeout(softc, t, p) 9102 struct ipf_main_softc_s *softc; 9103 ipftuneable_t *t; 9104 ipftuneval_t *p; 9105{ 9106 9107 /* 9108 * ipf_interror should be set by the functions called here, not 9109 * by this function - it's just a middle man. 9110 */ 9111 if (ipf_state_settimeout(softc, t, p) == -1) 9112 return -1; 9113 if (ipf_nat_settimeout(softc, t, p) == -1) 9114 return -1; 9115 return 0; 9116} 9117 9118 9119/* ------------------------------------------------------------------------ */ 9120/* Function: ipf_apply_timeout */ 9121/* Returns: int - 0 = success, -1 = failure */ 9122/* Parameters: head(I) - pointer to tuneable array entry */ 9123/* seconds(I) - pointer to values passed in to apply */ 9124/* */ 9125/* This function applies a timeout of "seconds" to the timeout queue that */ 9126/* is pointed to by "head". All entries on this list have an expiration */ 9127/* set to be the current tick value of ipf plus the ttl. Given that this */ 9128/* function should only be called when the delta is non-zero, the task is */ 9129/* to walk the entire list and apply the change. The sort order will not */ 9130/* change. The only catch is that this is O(n) across the list, so if the */ 9131/* queue has lots of entries (10s of thousands or 100s of thousands), it */ 9132/* could take a relatively long time to work through them all. */ 9133/* ------------------------------------------------------------------------ */ 9134void 9135ipf_apply_timeout(head, seconds) 9136 ipftq_t *head; 9137 u_int seconds; 9138{ 9139 u_int oldtimeout, newtimeout; 9140 ipftqent_t *tqe; 9141 int delta; 9142 9143 MUTEX_ENTER(&head->ifq_lock); 9144 oldtimeout = head->ifq_ttl; 9145 newtimeout = IPF_TTLVAL(seconds); 9146 delta = oldtimeout - newtimeout; 9147 9148 head->ifq_ttl = newtimeout; 9149 9150 for (tqe = head->ifq_head; tqe != NULL; tqe = tqe->tqe_next) { 9151 tqe->tqe_die += delta; 9152 } 9153 MUTEX_EXIT(&head->ifq_lock); 9154} 9155 9156 9157/* ------------------------------------------------------------------------ */ 9158/* Function: ipf_settimeout_tcp */ 9159/* Returns: int - 0 = successfully applied, -1 = failed */ 9160/* Parameters: t(I) - pointer to tuneable to change */ 9161/* p(I) - pointer to new timeout information */ 9162/* tab(I) - pointer to table of TCP queues */ 9163/* */ 9164/* This function applies the new timeout (p) to the TCP tunable (t) and */ 9165/* updates all of the entries on the relevant timeout queue by calling */ 9166/* ipf_apply_timeout(). */ 9167/* ------------------------------------------------------------------------ */ 9168int 9169ipf_settimeout_tcp(t, p, tab) 9170 ipftuneable_t *t; 9171 ipftuneval_t *p; 9172 ipftq_t *tab; 9173{ 9174 if (!strcmp(t->ipft_name, "tcp_idle_timeout") || 9175 !strcmp(t->ipft_name, "tcp_established")) { 9176 ipf_apply_timeout(&tab[IPF_TCPS_ESTABLISHED], p->ipftu_int); 9177 } else if (!strcmp(t->ipft_name, "tcp_close_wait")) { 9178 ipf_apply_timeout(&tab[IPF_TCPS_CLOSE_WAIT], p->ipftu_int); 9179 } else if (!strcmp(t->ipft_name, "tcp_last_ack")) { 9180 ipf_apply_timeout(&tab[IPF_TCPS_LAST_ACK], p->ipftu_int); 9181 } else if (!strcmp(t->ipft_name, "tcp_timeout")) { 9182 ipf_apply_timeout(&tab[IPF_TCPS_LISTEN], p->ipftu_int); 9183 ipf_apply_timeout(&tab[IPF_TCPS_HALF_ESTAB], p->ipftu_int); 9184 ipf_apply_timeout(&tab[IPF_TCPS_CLOSING], p->ipftu_int); 9185 } else if (!strcmp(t->ipft_name, "tcp_listen")) { 9186 ipf_apply_timeout(&tab[IPF_TCPS_LISTEN], p->ipftu_int); 9187 } else if (!strcmp(t->ipft_name, "tcp_half_established")) { 9188 ipf_apply_timeout(&tab[IPF_TCPS_HALF_ESTAB], p->ipftu_int); 9189 } else if (!strcmp(t->ipft_name, "tcp_closing")) { 9190 ipf_apply_timeout(&tab[IPF_TCPS_CLOSING], p->ipftu_int); 9191 } else if (!strcmp(t->ipft_name, "tcp_syn_received")) { 9192 ipf_apply_timeout(&tab[IPF_TCPS_SYN_RECEIVED], p->ipftu_int); 9193 } else if (!strcmp(t->ipft_name, "tcp_syn_sent")) { 9194 ipf_apply_timeout(&tab[IPF_TCPS_SYN_SENT], p->ipftu_int); 9195 } else if (!strcmp(t->ipft_name, "tcp_closed")) { 9196 ipf_apply_timeout(&tab[IPF_TCPS_CLOSED], p->ipftu_int); 9197 } else if (!strcmp(t->ipft_name, "tcp_half_closed")) { 9198 ipf_apply_timeout(&tab[IPF_TCPS_CLOSED], p->ipftu_int); 9199 } else if (!strcmp(t->ipft_name, "tcp_time_wait")) { 9200 ipf_apply_timeout(&tab[IPF_TCPS_TIME_WAIT], p->ipftu_int); 9201 } else { 9202 /* 9203 * ipf_interror isn't set here because it should be set 9204 * by whatever called this function. 9205 */ 9206 return -1; 9207 } 9208 return 0; 9209} 9210 9211 9212/* ------------------------------------------------------------------------ */ 9213/* Function: ipf_main_soft_create */ 9214/* Returns: NULL = failure, else success */ 9215/* Parameters: arg(I) - pointer to soft context structure if already allocd */ 9216/* */ 9217/* Create the foundation soft context structure. In circumstances where it */ 9218/* is not required to dynamically allocate the context, a pointer can be */ 9219/* passed in (rather than NULL) to a structure to be initialised. */ 9220/* The main thing of interest is that a number of locks are initialised */ 9221/* here instead of in the where might be expected - in the relevant create */ 9222/* function elsewhere. This is done because the current locking design has */ 9223/* some areas where these locks are used outside of their module. */ 9224/* Possibly the most important exercise that is done here is setting of all */ 9225/* the timeout values, allowing them to be changed before init(). */ 9226/* ------------------------------------------------------------------------ */ 9227void * 9228ipf_main_soft_create(arg) 9229 void *arg; 9230{ 9231 ipf_main_softc_t *softc; 9232 9233 if (arg == NULL) { 9234 KMALLOC(softc, ipf_main_softc_t *); 9235 if (softc == NULL) 9236 return NULL; 9237 } else { 9238 softc = arg; 9239 } 9240 9241 bzero((char *)softc, sizeof(*softc)); 9242 9243 /* 9244 * This serves as a flag as to whether or not the softc should be 9245 * free'd when _destroy is called. 9246 */ 9247 softc->ipf_dynamic_softc = (arg == NULL) ? 1 : 0; 9248 9249 softc->ipf_tuners = ipf_tune_array_copy(softc, 9250 sizeof(ipf_main_tuneables), 9251 ipf_main_tuneables); 9252 if (softc->ipf_tuners == NULL) { 9253 ipf_main_soft_destroy(softc); 9254 return NULL; 9255 } 9256 9257 MUTEX_INIT(&softc->ipf_rw, "ipf rw mutex"); 9258 MUTEX_INIT(&softc->ipf_timeoutlock, "ipf timeout lock"); 9259 RWLOCK_INIT(&softc->ipf_global, "ipf filter load/unload mutex"); 9260 RWLOCK_INIT(&softc->ipf_mutex, "ipf filter rwlock"); 9261 RWLOCK_INIT(&softc->ipf_tokens, "ipf token rwlock"); 9262 RWLOCK_INIT(&softc->ipf_state, "ipf state rwlock"); 9263 RWLOCK_INIT(&softc->ipf_nat, "ipf IP NAT rwlock"); 9264 RWLOCK_INIT(&softc->ipf_poolrw, "ipf pool rwlock"); 9265 RWLOCK_INIT(&softc->ipf_frag, "ipf frag rwlock"); 9266 9267 softc->ipf_token_head = NULL; 9268 softc->ipf_token_tail = &softc->ipf_token_head; 9269 9270 softc->ipf_tcpidletimeout = FIVE_DAYS; 9271 softc->ipf_tcpclosewait = IPF_TTLVAL(2 * TCP_MSL); 9272 softc->ipf_tcplastack = IPF_TTLVAL(30); 9273 softc->ipf_tcptimewait = IPF_TTLVAL(2 * TCP_MSL); 9274 softc->ipf_tcptimeout = IPF_TTLVAL(2 * TCP_MSL); 9275 softc->ipf_tcpsynsent = IPF_TTLVAL(2 * TCP_MSL); 9276 softc->ipf_tcpsynrecv = IPF_TTLVAL(2 * TCP_MSL); 9277 softc->ipf_tcpclosed = IPF_TTLVAL(30); 9278 softc->ipf_tcphalfclosed = IPF_TTLVAL(2 * 3600); 9279 softc->ipf_udptimeout = IPF_TTLVAL(120); 9280 softc->ipf_udpacktimeout = IPF_TTLVAL(12); 9281 softc->ipf_icmptimeout = IPF_TTLVAL(60); 9282 softc->ipf_icmpacktimeout = IPF_TTLVAL(6); 9283 softc->ipf_iptimeout = IPF_TTLVAL(60); 9284 9285#if defined(IPFILTER_DEFAULT_BLOCK) 9286 softc->ipf_pass = FR_BLOCK|FR_NOMATCH; 9287#else 9288 softc->ipf_pass = (IPF_DEFAULT_PASS)|FR_NOMATCH; 9289#endif 9290 softc->ipf_minttl = 4; 9291 softc->ipf_icmpminfragmtu = 68; 9292 softc->ipf_flags = IPF_LOGGING; 9293 9294 return softc; 9295} 9296 9297/* ------------------------------------------------------------------------ */ 9298/* Function: ipf_main_soft_init */ 9299/* Returns: 0 = success, -1 = failure */ 9300/* Parameters: softc(I) - pointer to soft context main structure */ 9301/* */ 9302/* A null-op function that exists as a placeholder so that the flow in */ 9303/* other functions is obvious. */ 9304/* ------------------------------------------------------------------------ */ 9305/*ARGSUSED*/ 9306int 9307ipf_main_soft_init(softc) 9308 ipf_main_softc_t *softc; 9309{ 9310 return 0; 9311} 9312 9313 9314/* ------------------------------------------------------------------------ */ 9315/* Function: ipf_main_soft_destroy */ 9316/* Returns: void */ 9317/* Parameters: softc(I) - pointer to soft context main structure */ 9318/* */ 9319/* Undo everything that we did in ipf_main_soft_create. */ 9320/* */ 9321/* The most important check that needs to be made here is whether or not */ 9322/* the structure was allocated by ipf_main_soft_create() by checking what */ 9323/* value is stored in ipf_dynamic_main. */ 9324/* ------------------------------------------------------------------------ */ 9325/*ARGSUSED*/ 9326void 9327ipf_main_soft_destroy(softc) 9328 ipf_main_softc_t *softc; 9329{ 9330 9331 RW_DESTROY(&softc->ipf_frag); 9332 RW_DESTROY(&softc->ipf_poolrw); 9333 RW_DESTROY(&softc->ipf_nat); 9334 RW_DESTROY(&softc->ipf_state); 9335 RW_DESTROY(&softc->ipf_tokens); 9336 RW_DESTROY(&softc->ipf_mutex); 9337 RW_DESTROY(&softc->ipf_global); 9338 MUTEX_DESTROY(&softc->ipf_timeoutlock); 9339 MUTEX_DESTROY(&softc->ipf_rw); 9340 9341 if (softc->ipf_tuners != NULL) { 9342 KFREES(softc->ipf_tuners, sizeof(ipf_main_tuneables)); 9343 } 9344 if (softc->ipf_dynamic_softc == 1) { 9345 KFREE(softc); 9346 } 9347} 9348 9349 9350/* ------------------------------------------------------------------------ */ 9351/* Function: ipf_main_soft_fini */ 9352/* Returns: 0 = success, -1 = failure */ 9353/* Parameters: softc(I) - pointer to soft context main structure */ 9354/* */ 9355/* Clean out the rules which have been added since _init was last called, */ 9356/* the only dynamic part of the mainline. */ 9357/* ------------------------------------------------------------------------ */ 9358int 9359ipf_main_soft_fini(softc) 9360 ipf_main_softc_t *softc; 9361{ 9362 (void) ipf_flush(softc, IPL_LOGIPF, FR_INQUE|FR_OUTQUE|FR_INACTIVE); 9363 (void) ipf_flush(softc, IPL_LOGIPF, FR_INQUE|FR_OUTQUE); 9364 (void) ipf_flush(softc, IPL_LOGCOUNT, FR_INQUE|FR_OUTQUE|FR_INACTIVE); 9365 (void) ipf_flush(softc, IPL_LOGCOUNT, FR_INQUE|FR_OUTQUE); 9366 9367 return 0; 9368} 9369 9370 9371/* ------------------------------------------------------------------------ */ 9372/* Function: ipf_main_load */ 9373/* Returns: 0 = success, -1 = failure */ 9374/* Parameters: none */ 9375/* */ 9376/* Handle global initialisation that needs to be done for the base part of */ 9377/* IPFilter. At present this just amounts to initialising some ICMP lookup */ 9378/* arrays that get used by the state/NAT code. */ 9379/* ------------------------------------------------------------------------ */ 9380int 9381ipf_main_load() 9382{ 9383 int i; 9384 9385 /* fill icmp reply type table */ 9386 for (i = 0; i <= ICMP_MAXTYPE; i++) 9387 icmpreplytype4[i] = -1; 9388 icmpreplytype4[ICMP_ECHO] = ICMP_ECHOREPLY; 9389 icmpreplytype4[ICMP_TSTAMP] = ICMP_TSTAMPREPLY; 9390 icmpreplytype4[ICMP_IREQ] = ICMP_IREQREPLY; 9391 icmpreplytype4[ICMP_MASKREQ] = ICMP_MASKREPLY; 9392 9393#ifdef USE_INET6 9394 /* fill icmp reply type table */ 9395 for (i = 0; i <= ICMP6_MAXTYPE; i++) 9396 icmpreplytype6[i] = -1; 9397 icmpreplytype6[ICMP6_ECHO_REQUEST] = ICMP6_ECHO_REPLY; 9398 icmpreplytype6[ICMP6_MEMBERSHIP_QUERY] = ICMP6_MEMBERSHIP_REPORT; 9399 icmpreplytype6[ICMP6_NI_QUERY] = ICMP6_NI_REPLY; 9400 icmpreplytype6[ND_ROUTER_SOLICIT] = ND_ROUTER_ADVERT; 9401 icmpreplytype6[ND_NEIGHBOR_SOLICIT] = ND_NEIGHBOR_ADVERT; 9402#endif 9403 9404 return 0; 9405} 9406 9407 9408/* ------------------------------------------------------------------------ */ 9409/* Function: ipf_main_unload */ 9410/* Returns: 0 = success, -1 = failure */ 9411/* Parameters: none */ 9412/* */ 9413/* A null-op function that exists as a placeholder so that the flow in */ 9414/* other functions is obvious. */ 9415/* ------------------------------------------------------------------------ */ 9416int 9417ipf_main_unload() 9418{ 9419 return 0; 9420} 9421 9422 9423/* ------------------------------------------------------------------------ */ 9424/* Function: ipf_load_all */ 9425/* Returns: 0 = success, -1 = failure */ 9426/* Parameters: none */ 9427/* */ 9428/* Work through all of the subsystems inside IPFilter and call the load */ 9429/* function for each in an order that won't lead to a crash :) */ 9430/* ------------------------------------------------------------------------ */ 9431int 9432ipf_load_all() 9433{ 9434 if (ipf_main_load() == -1) 9435 return -1; 9436 9437 if (ipf_state_main_load() == -1) 9438 return -1; 9439 9440 if (ipf_nat_main_load() == -1) 9441 return -1; 9442 9443 if (ipf_frag_main_load() == -1) 9444 return -1; 9445 9446 if (ipf_auth_main_load() == -1) 9447 return -1; 9448 9449 if (ipf_proxy_main_load() == -1) 9450 return -1; 9451 9452 return 0; 9453} 9454 9455 9456/* ------------------------------------------------------------------------ */ 9457/* Function: ipf_unload_all */ 9458/* Returns: 0 = success, -1 = failure */ 9459/* Parameters: none */ 9460/* */ 9461/* Work through all of the subsystems inside IPFilter and call the unload */ 9462/* function for each in an order that won't lead to a crash :) */ 9463/* ------------------------------------------------------------------------ */ 9464int 9465ipf_unload_all() 9466{ 9467 if (ipf_proxy_main_unload() == -1) 9468 return -1; 9469 9470 if (ipf_auth_main_unload() == -1) 9471 return -1; 9472 9473 if (ipf_frag_main_unload() == -1) 9474 return -1; 9475 9476 if (ipf_nat_main_unload() == -1) 9477 return -1; 9478 9479 if (ipf_state_main_unload() == -1) 9480 return -1; 9481 9482 if (ipf_main_unload() == -1) 9483 return -1; 9484 9485 return 0; 9486} 9487 9488 9489/* ------------------------------------------------------------------------ */ 9490/* Function: ipf_create_all */ 9491/* Returns: NULL = failure, else success */ 9492/* Parameters: arg(I) - pointer to soft context main structure */ 9493/* */ 9494/* Work through all of the subsystems inside IPFilter and call the create */ 9495/* function for each in an order that won't lead to a crash :) */ 9496/* ------------------------------------------------------------------------ */ 9497ipf_main_softc_t * 9498ipf_create_all(arg) 9499 void *arg; 9500{ 9501 ipf_main_softc_t *softc; 9502 9503 softc = ipf_main_soft_create(arg); 9504 if (softc == NULL) 9505 return NULL; 9506 9507#ifdef IPFILTER_LOG 9508 softc->ipf_log_soft = ipf_log_soft_create(softc); 9509 if (softc->ipf_log_soft == NULL) { 9510 ipf_destroy_all(softc); 9511 return NULL; 9512 } 9513#endif 9514 9515 softc->ipf_lookup_soft = ipf_lookup_soft_create(softc); 9516 if (softc->ipf_lookup_soft == NULL) { 9517 ipf_destroy_all(softc); 9518 return NULL; 9519 } 9520 9521 softc->ipf_sync_soft = ipf_sync_soft_create(softc); 9522 if (softc->ipf_sync_soft == NULL) { 9523 ipf_destroy_all(softc); 9524 return NULL; 9525 } 9526 9527 softc->ipf_state_soft = ipf_state_soft_create(softc); 9528 if (softc->ipf_state_soft == NULL) { 9529 ipf_destroy_all(softc); 9530 return NULL; 9531 } 9532 9533 softc->ipf_nat_soft = ipf_nat_soft_create(softc); 9534 if (softc->ipf_nat_soft == NULL) { 9535 ipf_destroy_all(softc); 9536 return NULL; 9537 } 9538 9539 softc->ipf_frag_soft = ipf_frag_soft_create(softc); 9540 if (softc->ipf_frag_soft == NULL) { 9541 ipf_destroy_all(softc); 9542 return NULL; 9543 } 9544 9545 softc->ipf_auth_soft = ipf_auth_soft_create(softc); 9546 if (softc->ipf_auth_soft == NULL) { 9547 ipf_destroy_all(softc); 9548 return NULL; 9549 } 9550 9551 softc->ipf_proxy_soft = ipf_proxy_soft_create(softc); 9552 if (softc->ipf_proxy_soft == NULL) { 9553 ipf_destroy_all(softc); 9554 return NULL; 9555 } 9556 9557 return softc; 9558} 9559 9560 9561/* ------------------------------------------------------------------------ */ 9562/* Function: ipf_destroy_all */ 9563/* Returns: void */ 9564/* Parameters: softc(I) - pointer to soft context main structure */ 9565/* */ 9566/* Work through all of the subsystems inside IPFilter and call the destroy */ 9567/* function for each in an order that won't lead to a crash :) */ 9568/* */ 9569/* Every one of these functions is expected to succeed, so there is no */ 9570/* checking of return values. */ 9571/* ------------------------------------------------------------------------ */ 9572void 9573ipf_destroy_all(softc) 9574 ipf_main_softc_t *softc; 9575{ 9576 9577 if (softc->ipf_state_soft != NULL) { 9578 ipf_state_soft_destroy(softc, softc->ipf_state_soft); 9579 softc->ipf_state_soft = NULL; 9580 } 9581 9582 if (softc->ipf_nat_soft != NULL) { 9583 ipf_nat_soft_destroy(softc, softc->ipf_nat_soft); 9584 softc->ipf_nat_soft = NULL; 9585 } 9586 9587 if (softc->ipf_frag_soft != NULL) { 9588 ipf_frag_soft_destroy(softc, softc->ipf_frag_soft); 9589 softc->ipf_frag_soft = NULL; 9590 } 9591 9592 if (softc->ipf_auth_soft != NULL) { 9593 ipf_auth_soft_destroy(softc, softc->ipf_auth_soft); 9594 softc->ipf_auth_soft = NULL; 9595 } 9596 9597 if (softc->ipf_proxy_soft != NULL) { 9598 ipf_proxy_soft_destroy(softc, softc->ipf_proxy_soft); 9599 softc->ipf_proxy_soft = NULL; 9600 } 9601 9602 if (softc->ipf_sync_soft != NULL) { 9603 ipf_sync_soft_destroy(softc, softc->ipf_sync_soft); 9604 softc->ipf_sync_soft = NULL; 9605 } 9606 9607 if (softc->ipf_lookup_soft != NULL) { 9608 ipf_lookup_soft_destroy(softc, softc->ipf_lookup_soft); 9609 softc->ipf_lookup_soft = NULL; 9610 } 9611 9612#ifdef IPFILTER_LOG 9613 if (softc->ipf_log_soft != NULL) { 9614 ipf_log_soft_destroy(softc, softc->ipf_log_soft); 9615 softc->ipf_log_soft = NULL; 9616 } 9617#endif 9618 9619 ipf_main_soft_destroy(softc); 9620} 9621 9622 9623/* ------------------------------------------------------------------------ */ 9624/* Function: ipf_init_all */ 9625/* Returns: 0 = success, -1 = failure */ 9626/* Parameters: softc(I) - pointer to soft context main structure */ 9627/* */ 9628/* Work through all of the subsystems inside IPFilter and call the init */ 9629/* function for each in an order that won't lead to a crash :) */ 9630/* ------------------------------------------------------------------------ */ 9631int 9632ipf_init_all(softc) 9633 ipf_main_softc_t *softc; 9634{ 9635 9636 if (ipf_main_soft_init(softc) == -1) 9637 return -1; 9638 9639#ifdef IPFILTER_LOG 9640 if (ipf_log_soft_init(softc, softc->ipf_log_soft) == -1) 9641 return -1; 9642#endif 9643 9644 if (ipf_lookup_soft_init(softc, softc->ipf_lookup_soft) == -1) 9645 return -1; 9646 9647 if (ipf_sync_soft_init(softc, softc->ipf_sync_soft) == -1) 9648 return -1; 9649 9650 if (ipf_state_soft_init(softc, softc->ipf_state_soft) == -1) 9651 return -1; 9652 9653 if (ipf_nat_soft_init(softc, softc->ipf_nat_soft) == -1) 9654 return -1; 9655 9656 if (ipf_frag_soft_init(softc, softc->ipf_frag_soft) == -1) 9657 return -1; 9658 9659 if (ipf_auth_soft_init(softc, softc->ipf_auth_soft) == -1) 9660 return -1; 9661 9662 if (ipf_proxy_soft_init(softc, softc->ipf_proxy_soft) == -1) 9663 return -1; 9664 9665 return 0; 9666} 9667 9668 9669/* ------------------------------------------------------------------------ */ 9670/* Function: ipf_fini_all */ 9671/* Returns: 0 = success, -1 = failure */ 9672/* Parameters: softc(I) - pointer to soft context main structure */ 9673/* */ 9674/* Work through all of the subsystems inside IPFilter and call the fini */ 9675/* function for each in an order that won't lead to a crash :) */ 9676/* ------------------------------------------------------------------------ */ 9677int 9678ipf_fini_all(softc) 9679 ipf_main_softc_t *softc; 9680{ 9681 9682 ipf_token_flush(softc); 9683 9684 if (ipf_proxy_soft_fini(softc, softc->ipf_proxy_soft) == -1) 9685 return -1; 9686 9687 if (ipf_auth_soft_fini(softc, softc->ipf_auth_soft) == -1) 9688 return -1; 9689 9690 if (ipf_frag_soft_fini(softc, softc->ipf_frag_soft) == -1) 9691 return -1; 9692 9693 if (ipf_nat_soft_fini(softc, softc->ipf_nat_soft) == -1) 9694 return -1; 9695 9696 if (ipf_state_soft_fini(softc, softc->ipf_state_soft) == -1) 9697 return -1; 9698 9699 if (ipf_sync_soft_fini(softc, softc->ipf_sync_soft) == -1) 9700 return -1; 9701 9702 if (ipf_lookup_soft_fini(softc, softc->ipf_lookup_soft) == -1) 9703 return -1; 9704 9705#ifdef IPFILTER_LOG 9706 if (ipf_log_soft_fini(softc, softc->ipf_log_soft) == -1) 9707 return -1; 9708#endif 9709 9710 if (ipf_main_soft_fini(softc) == -1) 9711 return -1; 9712 9713 return 0; 9714} 9715 9716 9717/* ------------------------------------------------------------------------ */ 9718/* Function: ipf_rule_expire */ 9719/* Returns: Nil */ 9720/* Parameters: softc(I) - pointer to soft context main structure */ 9721/* */ 9722/* At present this function exists just to support temporary addition of */ 9723/* firewall rules. Both inactive and active lists are scanned for items to */ 9724/* purge, as by rights, the expiration is computed as soon as the rule is */ 9725/* loaded in. */ 9726/* ------------------------------------------------------------------------ */ 9727void 9728ipf_rule_expire(softc) 9729 ipf_main_softc_t *softc; 9730{ 9731 frentry_t *fr; 9732 9733 if ((softc->ipf_rule_explist[0] == NULL) && 9734 (softc->ipf_rule_explist[1] == NULL)) 9735 return; 9736 9737 WRITE_ENTER(&softc->ipf_mutex); 9738 9739 while ((fr = softc->ipf_rule_explist[0]) != NULL) { 9740 /* 9741 * Because the list is kept sorted on insertion, the fist 9742 * one that dies in the future means no more work to do. 9743 */ 9744 if (fr->fr_die > softc->ipf_ticks) 9745 break; 9746 ipf_rule_delete(softc, fr, IPL_LOGIPF, 0); 9747 } 9748 9749 while ((fr = softc->ipf_rule_explist[1]) != NULL) { 9750 /* 9751 * Because the list is kept sorted on insertion, the fist 9752 * one that dies in the future means no more work to do. 9753 */ 9754 if (fr->fr_die > softc->ipf_ticks) 9755 break; 9756 ipf_rule_delete(softc, fr, IPL_LOGIPF, 1); 9757 } 9758 9759 RWLOCK_EXIT(&softc->ipf_mutex); 9760} 9761 9762 9763static int ipf_ht_node_cmp __P((struct host_node_s *, struct host_node_s *)); 9764static void ipf_ht_node_make_key __P((host_track_t *, host_node_t *, int, 9765 i6addr_t *)); 9766 9767host_node_t RBI_ZERO(ipf_rb); 9768RBI_CODE(ipf_rb, host_node_t, hn_entry, ipf_ht_node_cmp) 9769 9770 9771/* ------------------------------------------------------------------------ */ 9772/* Function: ipf_ht_node_cmp */ 9773/* Returns: int - 0 == nodes are the same, .. */ 9774/* Parameters: k1(I) - pointer to first key to compare */ 9775/* k2(I) - pointer to second key to compare */ 9776/* */ 9777/* The "key" for the node is a combination of two fields: the address */ 9778/* family and the address itself. */ 9779/* */ 9780/* Because we're not actually interpreting the address data, it isn't */ 9781/* necessary to convert them to/from network/host byte order. The mask is */ 9782/* just used to remove bits that aren't significant - it doesn't matter */ 9783/* where they are, as long as they're always in the same place. */ 9784/* */ 9785/* As with IP6_EQ, comparing IPv6 addresses starts at the bottom because */ 9786/* this is where individual ones will differ the most - but not true for */ 9787/* for /48's, etc. */ 9788/* ------------------------------------------------------------------------ */ 9789static int 9790ipf_ht_node_cmp(k1, k2) 9791 struct host_node_s *k1, *k2; 9792{ 9793 int i; 9794 9795 i = (k2->hn_addr.adf_family - k1->hn_addr.adf_family); 9796 if (i != 0) 9797 return i; 9798 9799 if (k1->hn_addr.adf_family == AF_INET) 9800 return (k2->hn_addr.adf_addr.in4.s_addr - 9801 k1->hn_addr.adf_addr.in4.s_addr); 9802 9803 i = k2->hn_addr.adf_addr.i6[3] - k1->hn_addr.adf_addr.i6[3]; 9804 if (i != 0) 9805 return i; 9806 i = k2->hn_addr.adf_addr.i6[2] - k1->hn_addr.adf_addr.i6[2]; 9807 if (i != 0) 9808 return i; 9809 i = k2->hn_addr.adf_addr.i6[1] - k1->hn_addr.adf_addr.i6[1]; 9810 if (i != 0) 9811 return i; 9812 i = k2->hn_addr.adf_addr.i6[0] - k1->hn_addr.adf_addr.i6[0]; 9813 return i; 9814} 9815 9816 9817/* ------------------------------------------------------------------------ */ 9818/* Function: ipf_ht_node_make_key */ 9819/* Returns: Nil */ 9820/* parameters: htp(I) - pointer to address tracking structure */ 9821/* key(I) - where to store masked address for lookup */ 9822/* family(I) - protocol family of address */ 9823/* addr(I) - pointer to network address */ 9824/* */ 9825/* Using the "netmask" (number of bits) stored parent host tracking struct, */ 9826/* copy the address passed in into the key structure whilst masking out the */ 9827/* bits that we don't want. */ 9828/* */ 9829/* Because the parser will set ht_netmask to 128 if there is no protocol */ 9830/* specified (the parser doesn't know if it should be a v4 or v6 rule), we */ 9831/* have to be wary of that and not allow 32-128 to happen. */ 9832/* ------------------------------------------------------------------------ */ 9833static void 9834ipf_ht_node_make_key(htp, key, family, addr) 9835 host_track_t *htp; 9836 host_node_t *key; 9837 int family; 9838 i6addr_t *addr; 9839{ 9840 key->hn_addr.adf_family = family; 9841 if (family == AF_INET) { 9842 u_32_t mask; 9843 int bits; 9844 9845 key->hn_addr.adf_len = sizeof(key->hn_addr.adf_addr.in4); 9846 bits = htp->ht_netmask; 9847 if (bits >= 32) { 9848 mask = 0xffffffff; 9849 } else { 9850 mask = htonl(0xffffffff << (32 - bits)); 9851 } 9852 key->hn_addr.adf_addr.in4.s_addr = addr->in4.s_addr & mask; 9853#ifdef USE_INET6 9854 } else { 9855 int bits = htp->ht_netmask; 9856 9857 key->hn_addr.adf_len = sizeof(key->hn_addr.adf_addr.in6); 9858 if (bits > 96) { 9859 key->hn_addr.adf_addr.i6[3] = addr->i6[3] & 9860 htonl(0xffffffff << (128 - bits)); 9861 key->hn_addr.adf_addr.i6[2] = addr->i6[2]; 9862 key->hn_addr.adf_addr.i6[1] = addr->i6[2]; 9863 key->hn_addr.adf_addr.i6[0] = addr->i6[2]; 9864 } else if (bits > 64) { 9865 key->hn_addr.adf_addr.i6[3] = 0; 9866 key->hn_addr.adf_addr.i6[2] = addr->i6[2] & 9867 htonl(0xffffffff << (96 - bits)); 9868 key->hn_addr.adf_addr.i6[1] = addr->i6[1]; 9869 key->hn_addr.adf_addr.i6[0] = addr->i6[0]; 9870 } else if (bits > 32) { 9871 key->hn_addr.adf_addr.i6[3] = 0; 9872 key->hn_addr.adf_addr.i6[2] = 0; 9873 key->hn_addr.adf_addr.i6[1] = addr->i6[1] & 9874 htonl(0xffffffff << (64 - bits)); 9875 key->hn_addr.adf_addr.i6[0] = addr->i6[0]; 9876 } else { 9877 key->hn_addr.adf_addr.i6[3] = 0; 9878 key->hn_addr.adf_addr.i6[2] = 0; 9879 key->hn_addr.adf_addr.i6[1] = 0; 9880 key->hn_addr.adf_addr.i6[0] = addr->i6[0] & 9881 htonl(0xffffffff << (32 - bits)); 9882 } 9883#endif 9884 } 9885} 9886 9887 9888/* ------------------------------------------------------------------------ */ 9889/* Function: ipf_ht_node_add */ 9890/* Returns: int - 0 == success, -1 == failure */ 9891/* Parameters: softc(I) - pointer to soft context main structure */ 9892/* htp(I) - pointer to address tracking structure */ 9893/* family(I) - protocol family of address */ 9894/* addr(I) - pointer to network address */ 9895/* */ 9896/* NOTE: THIS FUNCTION MUST BE CALLED WITH AN EXCLUSIVE LOCK THAT PREVENTS */ 9897/* ipf_ht_node_del FROM RUNNING CONCURRENTLY ON THE SAME htp. */ 9898/* */ 9899/* After preparing the key with the address information to find, look in */ 9900/* the red-black tree to see if the address is known. A successful call to */ 9901/* this function can mean one of two things: a new node was added to the */ 9902/* tree or a matching node exists and we're able to bump up its activity. */ 9903/* ------------------------------------------------------------------------ */ 9904int 9905ipf_ht_node_add(softc, htp, family, addr) 9906 ipf_main_softc_t *softc; 9907 host_track_t *htp; 9908 int family; 9909 i6addr_t *addr; 9910{ 9911 host_node_t *h; 9912 host_node_t k; 9913 9914 ipf_ht_node_make_key(htp, &k, family, addr); 9915 9916 h = RBI_SEARCH(ipf_rb, &htp->ht_root, &k); 9917 if (h == NULL) { 9918 if (htp->ht_cur_nodes >= htp->ht_max_nodes) 9919 return -1; 9920 KMALLOC(h, host_node_t *); 9921 if (h == NULL) { 9922 DT(ipf_rb_no_mem); 9923 LBUMP(ipf_rb_no_mem); 9924 return -1; 9925 } 9926 9927 /* 9928 * If there was a macro to initialise the RB node then that 9929 * would get used here, but there isn't... 9930 */ 9931 bzero((char *)h, sizeof(*h)); 9932 h->hn_addr = k.hn_addr; 9933 h->hn_addr.adf_family = k.hn_addr.adf_family; 9934 RBI_INSERT(ipf_rb, &htp->ht_root, h); 9935 htp->ht_cur_nodes++; 9936 } else { 9937 if ((htp->ht_max_per_node != 0) && 9938 (h->hn_active >= htp->ht_max_per_node)) { 9939 DT(ipf_rb_node_max); 9940 LBUMP(ipf_rb_node_max); 9941 return -1; 9942 } 9943 } 9944 9945 h->hn_active++; 9946 9947 return 0; 9948} 9949 9950 9951/* ------------------------------------------------------------------------ */ 9952/* Function: ipf_ht_node_del */ 9953/* Returns: int - 0 == success, -1 == failure */ 9954/* parameters: htp(I) - pointer to address tracking structure */ 9955/* family(I) - protocol family of address */ 9956/* addr(I) - pointer to network address */ 9957/* */ 9958/* NOTE: THIS FUNCTION MUST BE CALLED WITH AN EXCLUSIVE LOCK THAT PREVENTS */ 9959/* ipf_ht_node_add FROM RUNNING CONCURRENTLY ON THE SAME htp. */ 9960/* */ 9961/* Try and find the address passed in amongst the leavese on this tree to */ 9962/* be friend. If found then drop the active account for that node drops by */ 9963/* one. If that count reaches 0, it is time to free it all up. */ 9964/* ------------------------------------------------------------------------ */ 9965int 9966ipf_ht_node_del(htp, family, addr) 9967 host_track_t *htp; 9968 int family; 9969 i6addr_t *addr; 9970{ 9971 host_node_t *h; 9972 host_node_t k; 9973 9974 ipf_ht_node_make_key(htp, &k, family, addr); 9975 9976 h = RBI_SEARCH(ipf_rb, &htp->ht_root, &k); 9977 if (h == NULL) { 9978 return -1; 9979 } else { 9980 h->hn_active--; 9981 if (h->hn_active == 0) { 9982 (void) RBI_DELETE(ipf_rb, &htp->ht_root, h); 9983 htp->ht_cur_nodes--; 9984 KFREE(h); 9985 } 9986 } 9987 9988 return 0; 9989} 9990 9991 9992/* ------------------------------------------------------------------------ */ 9993/* Function: ipf_rb_ht_init */ 9994/* Returns: Nil */ 9995/* Parameters: head(I) - pointer to host tracking structure */ 9996/* */ 9997/* Initialise the host tracking structure to be ready for use above. */ 9998/* ------------------------------------------------------------------------ */ 9999void 10000ipf_rb_ht_init(head) 10001 host_track_t *head; 10002{ 10003 RBI_INIT(ipf_rb, &head->ht_root); 10004} 10005 10006 10007/* ------------------------------------------------------------------------ */ 10008/* Function: ipf_rb_ht_freenode */ 10009/* Returns: Nil */ 10010/* Parameters: head(I) - pointer to host tracking structure */ 10011/* arg(I) - additional argument from walk caller */ 10012/* */ 10013/* Free an actual host_node_t structure. */ 10014/* ------------------------------------------------------------------------ */ 10015void 10016ipf_rb_ht_freenode(node, arg) 10017 host_node_t *node; 10018 void *arg; 10019{ 10020 KFREE(node); 10021} 10022 10023 10024/* ------------------------------------------------------------------------ */ 10025/* Function: ipf_rb_ht_flush */ 10026/* Returns: Nil */ 10027/* Parameters: head(I) - pointer to host tracking structure */ 10028/* */ 10029/* Remove all of the nodes in the tree tracking hosts by calling a walker */ 10030/* and free'ing each one. */ 10031/* ------------------------------------------------------------------------ */ 10032void 10033ipf_rb_ht_flush(head) 10034 host_track_t *head; 10035{ 10036 RBI_WALK(ipf_rb, &head->ht_root, ipf_rb_ht_freenode, NULL); 10037} 10038 10039 10040/* ------------------------------------------------------------------------ */ 10041/* Function: ipf_slowtimer */ 10042/* Returns: Nil */ 10043/* Parameters: ptr(I) - pointer to main ipf soft context structure */ 10044/* */ 10045/* Slowly expire held state for fragments. Timeouts are set * in */ 10046/* expectation of this being called twice per second. */ 10047/* ------------------------------------------------------------------------ */ 10048void 10049ipf_slowtimer(softc) 10050 ipf_main_softc_t *softc; 10051{ 10052 10053 ipf_token_expire(softc); 10054 ipf_frag_expire(softc); 10055 ipf_state_expire(softc); 10056 ipf_nat_expire(softc); 10057 ipf_auth_expire(softc); 10058 ipf_lookup_expire(softc); 10059 ipf_rule_expire(softc); 10060 ipf_sync_expire(softc); 10061 softc->ipf_ticks++; 10062} 10063 10064 10065/* ------------------------------------------------------------------------ */ 10066/* Function: ipf_inet_mask_add */ 10067/* Returns: Nil */ 10068/* Parameters: bits(I) - pointer to nat context information */ 10069/* mtab(I) - pointer to mask hash table structure */ 10070/* */ 10071/* When called, bits represents the mask of a new NAT rule that has just */ 10072/* been added. This function inserts a bitmask into the array of masks to */ 10073/* search when searching for a matching NAT rule for a packet. */ 10074/* Prevention of duplicate masks is achieved by checking the use count for */ 10075/* a given netmask. */ 10076/* ------------------------------------------------------------------------ */ 10077void 10078ipf_inet_mask_add(bits, mtab) 10079 int bits; 10080 ipf_v4_masktab_t *mtab; 10081{ 10082 u_32_t mask; 10083 int i, j; 10084 10085 mtab->imt4_masks[bits]++; 10086 if (mtab->imt4_masks[bits] > 1) 10087 return; 10088 10089 if (bits == 0) 10090 mask = 0; 10091 else 10092 mask = 0xffffffff << (32 - bits); 10093 10094 for (i = 0; i < 33; i++) { 10095 if (ntohl(mtab->imt4_active[i]) < mask) { 10096 for (j = 32; j > i; j--) 10097 mtab->imt4_active[j] = mtab->imt4_active[j - 1]; 10098 mtab->imt4_active[i] = htonl(mask); 10099 break; 10100 } 10101 } 10102 mtab->imt4_max++; 10103} 10104 10105 10106/* ------------------------------------------------------------------------ */ 10107/* Function: ipf_inet_mask_del */ 10108/* Returns: Nil */ 10109/* Parameters: bits(I) - number of bits set in the netmask */ 10110/* mtab(I) - pointer to mask hash table structure */ 10111/* */ 10112/* Remove the 32bit bitmask represented by "bits" from the collection of */ 10113/* netmasks stored inside of mtab. */ 10114/* ------------------------------------------------------------------------ */ 10115void 10116ipf_inet_mask_del(bits, mtab) 10117 int bits; 10118 ipf_v4_masktab_t *mtab; 10119{ 10120 u_32_t mask; 10121 int i, j; 10122 10123 mtab->imt4_masks[bits]--; 10124 if (mtab->imt4_masks[bits] > 0) 10125 return; 10126 10127 mask = htonl(0xffffffff << (32 - bits)); 10128 for (i = 0; i < 33; i++) { 10129 if (mtab->imt4_active[i] == mask) { 10130 for (j = i + 1; j < 33; j++) 10131 mtab->imt4_active[j - 1] = mtab->imt4_active[j]; 10132 break; 10133 } 10134 } 10135 mtab->imt4_max--; 10136 ASSERT(mtab->imt4_max >= 0); 10137} 10138 10139 10140#ifdef USE_INET6 10141/* ------------------------------------------------------------------------ */ 10142/* Function: ipf_inet6_mask_add */ 10143/* Returns: Nil */ 10144/* Parameters: bits(I) - number of bits set in mask */ 10145/* mask(I) - pointer to mask to add */ 10146/* mtab(I) - pointer to mask hash table structure */ 10147/* */ 10148/* When called, bitcount represents the mask of a IPv6 NAT map rule that */ 10149/* has just been added. This function inserts a bitmask into the array of */ 10150/* masks to search when searching for a matching NAT rule for a packet. */ 10151/* Prevention of duplicate masks is achieved by checking the use count for */ 10152/* a given netmask. */ 10153/* ------------------------------------------------------------------------ */ 10154void 10155ipf_inet6_mask_add(bits, mask, mtab) 10156 int bits; 10157 i6addr_t *mask; 10158 ipf_v6_masktab_t *mtab; 10159{ 10160 i6addr_t zero; 10161 int i, j; 10162 10163 mtab->imt6_masks[bits]++; 10164 if (mtab->imt6_masks[bits] > 1) 10165 return; 10166 10167 if (bits == 0) { 10168 mask = &zero; 10169 zero.i6[0] = 0; 10170 zero.i6[1] = 0; 10171 zero.i6[2] = 0; 10172 zero.i6[3] = 0; 10173 } 10174 10175 for (i = 0; i < 129; i++) { 10176 if (IP6_LT(&mtab->imt6_active[i], mask)) { 10177 for (j = 128; j > i; j--) 10178 mtab->imt6_active[j] = mtab->imt6_active[j - 1]; 10179 mtab->imt6_active[i] = *mask; 10180 break; 10181 } 10182 } 10183 mtab->imt6_max++; 10184} 10185 10186 10187/* ------------------------------------------------------------------------ */ 10188/* Function: ipf_inet6_mask_del */ 10189/* Returns: Nil */ 10190/* Parameters: bits(I) - number of bits set in mask */ 10191/* mask(I) - pointer to mask to remove */ 10192/* mtab(I) - pointer to mask hash table structure */ 10193/* */ 10194/* Remove the 128bit bitmask represented by "bits" from the collection of */ 10195/* netmasks stored inside of mtab. */ 10196/* ------------------------------------------------------------------------ */ 10197void 10198ipf_inet6_mask_del(bits, mask, mtab) 10199 int bits; 10200 i6addr_t *mask; 10201 ipf_v6_masktab_t *mtab; 10202{ 10203 i6addr_t zero; 10204 int i, j; 10205 10206 mtab->imt6_masks[bits]--; 10207 if (mtab->imt6_masks[bits] > 0) 10208 return; 10209 10210 if (bits == 0) 10211 mask = &zero; 10212 zero.i6[0] = 0; 10213 zero.i6[1] = 0; 10214 zero.i6[2] = 0; 10215 zero.i6[3] = 0; 10216 10217 for (i = 0; i < 129; i++) { 10218 if (IP6_EQ(&mtab->imt6_active[i], mask)) { 10219 for (j = i + 1; j < 129; j++) { 10220 mtab->imt6_active[j - 1] = mtab->imt6_active[j]; 10221 if (IP6_EQ(&mtab->imt6_active[j - 1], &zero)) 10222 break; 10223 } 10224 break; 10225 } 10226 } 10227 mtab->imt6_max--; 10228 ASSERT(mtab->imt6_max >= 0); 10229} 10230#endif 10231