fil.c revision 334202
1/* $FreeBSD: stable/10/sys/contrib/ipfilter/netinet/fil.c 334202 2018-05-25 06:26:07Z 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 (__FreeBSD_version >= 220000) 25# if (__FreeBSD_version >= 400000) 26# if !defined(IPFILTER_LKM) 27# include "opt_inet6.h" 28# endif 29# if (__FreeBSD_version == 400019) 30# define CSUM_DELAY_DATA 31# endif 32# endif 33# include <sys/filio.h> 34#else 35# include <sys/ioctl.h> 36#endif 37#if (defined(__SVR4) || defined(__svr4__)) && defined(sun) 38# include <sys/filio.h> 39#endif 40#if !defined(_AIX51) 41# include <sys/fcntl.h> 42#endif 43#if defined(_KERNEL) 44# include <sys/systm.h> 45# include <sys/file.h> 46#else 47# include <stdio.h> 48# include <string.h> 49# include <stdlib.h> 50# include <stddef.h> 51# include <sys/file.h> 52# define _KERNEL 53# ifdef __OpenBSD__ 54struct file; 55# endif 56# include <sys/uio.h> 57# undef _KERNEL 58#endif 59#if !defined(__SVR4) && !defined(__svr4__) && !defined(__hpux) && \ 60 !defined(linux) 61# include <sys/mbuf.h> 62#else 63# if !defined(linux) 64# include <sys/byteorder.h> 65# endif 66# if (SOLARIS2 < 5) && defined(sun) 67# include <sys/dditypes.h> 68# endif 69#endif 70#ifdef __hpux 71# define _NET_ROUTE_INCLUDED 72#endif 73#if !defined(linux) 74# include <sys/protosw.h> 75#endif 76#include <sys/socket.h> 77#include <net/if.h> 78#ifdef sun 79# include <net/af.h> 80#endif 81#include <netinet/in.h> 82#include <netinet/in_systm.h> 83#include <netinet/ip.h> 84#if defined(__sgi) && defined(IFF_DRVRLOCK) /* IRIX 6 */ 85# include <sys/hashing.h> 86# include <netinet/in_var.h> 87#endif 88#include <netinet/tcp.h> 89#if (!defined(__sgi) && !defined(AIX)) || defined(_KERNEL) 90# include <netinet/udp.h> 91# include <netinet/ip_icmp.h> 92#endif 93#ifdef __hpux 94# undef _NET_ROUTE_INCLUDED 95#endif 96#ifdef __osf__ 97# undef _RADIX_H_ 98#endif 99#include "netinet/ip_compat.h" 100#ifdef USE_INET6 101# include <netinet/icmp6.h> 102# if !SOLARIS && defined(_KERNEL) && !defined(__osf__) && !defined(__hpux) 103# include <netinet6/in6_var.h> 104# endif 105#endif 106#include "netinet/ip_fil.h" 107#include "netinet/ip_nat.h" 108#include "netinet/ip_frag.h" 109#include "netinet/ip_state.h" 110#include "netinet/ip_proxy.h" 111#include "netinet/ip_auth.h" 112#ifdef IPFILTER_SCAN 113# include "netinet/ip_scan.h" 114#endif 115#include "netinet/ip_sync.h" 116#include "netinet/ip_lookup.h" 117#include "netinet/ip_pool.h" 118#include "netinet/ip_htable.h" 119#ifdef IPFILTER_COMPILED 120# include "netinet/ip_rules.h" 121#endif 122#if defined(IPFILTER_BPF) && defined(_KERNEL) 123# include <net/bpf.h> 124#endif 125#if defined(__FreeBSD_version) && (__FreeBSD_version >= 300000) 126# include <sys/malloc.h> 127#endif 128#include "netinet/ipl.h" 129 130#if defined(__NetBSD__) && (__NetBSD_Version__ >= 104230000) 131# include <sys/callout.h> 132extern struct callout ipf_slowtimer_ch; 133#endif 134#if defined(__OpenBSD__) 135# include <sys/timeout.h> 136extern struct timeout ipf_slowtimer_ch; 137#endif 138/* END OF INCLUDES */ 139 140#if !defined(lint) 141static const char sccsid[] = "@(#)fil.c 1.36 6/5/96 (C) 1993-2000 Darren Reed"; 142static const char rcsid[] = "@(#)$FreeBSD: stable/10/sys/contrib/ipfilter/netinet/fil.c 334202 2018-05-25 06:26:07Z cy $"; 143/* static const char rcsid[] = "@(#)$Id: fil.c,v 2.243.2.125 2007/10/10 09:27:20 darrenr Exp $"; */ 144#endif 145 146#ifndef _KERNEL 147# include "ipf.h" 148# include "ipt.h" 149extern int opts; 150extern int blockreason; 151#endif /* _KERNEL */ 152 153#define LBUMP(x) softc->x++ 154#define LBUMPD(x, y) do { softc->x.y++; DT(y); } while (0) 155 156static INLINE int ipf_check_ipf __P((fr_info_t *, frentry_t *, int)); 157static u_32_t ipf_checkcipso __P((fr_info_t *, u_char *, int)); 158static u_32_t ipf_checkripso __P((u_char *)); 159static u_32_t ipf_decaps __P((fr_info_t *, u_32_t, int)); 160#ifdef IPFILTER_LOG 161static frentry_t *ipf_dolog __P((fr_info_t *, u_32_t *)); 162#endif 163static int ipf_flushlist __P((ipf_main_softc_t *, int *, 164 frentry_t **)); 165static int ipf_flush_groups __P((ipf_main_softc_t *, frgroup_t **, 166 int)); 167static ipfunc_t ipf_findfunc __P((ipfunc_t)); 168static void *ipf_findlookup __P((ipf_main_softc_t *, int, 169 frentry_t *, 170 i6addr_t *, i6addr_t *)); 171static frentry_t *ipf_firewall __P((fr_info_t *, u_32_t *)); 172static int ipf_fr_matcharray __P((fr_info_t *, int *)); 173static int ipf_frruleiter __P((ipf_main_softc_t *, void *, int, 174 void *)); 175static void ipf_funcfini __P((ipf_main_softc_t *, frentry_t *)); 176static int ipf_funcinit __P((ipf_main_softc_t *, frentry_t *)); 177static int ipf_geniter __P((ipf_main_softc_t *, ipftoken_t *, 178 ipfgeniter_t *)); 179static void ipf_getstat __P((ipf_main_softc_t *, 180 struct friostat *, int)); 181static int ipf_group_flush __P((ipf_main_softc_t *, frgroup_t *)); 182static void ipf_group_free __P((frgroup_t *)); 183static int ipf_grpmapfini __P((struct ipf_main_softc_s *, 184 frentry_t *)); 185static int ipf_grpmapinit __P((struct ipf_main_softc_s *, 186 frentry_t *)); 187static frentry_t *ipf_nextrule __P((ipf_main_softc_t *, int, int, 188 frentry_t *, int)); 189static int ipf_portcheck __P((frpcmp_t *, u_32_t)); 190static INLINE int ipf_pr_ah __P((fr_info_t *)); 191static INLINE void ipf_pr_esp __P((fr_info_t *)); 192static INLINE void ipf_pr_gre __P((fr_info_t *)); 193static INLINE void ipf_pr_udp __P((fr_info_t *)); 194static INLINE void ipf_pr_tcp __P((fr_info_t *)); 195static INLINE void ipf_pr_icmp __P((fr_info_t *)); 196static INLINE void ipf_pr_ipv4hdr __P((fr_info_t *)); 197static INLINE void ipf_pr_short __P((fr_info_t *, int)); 198static INLINE int ipf_pr_tcpcommon __P((fr_info_t *)); 199static INLINE int ipf_pr_udpcommon __P((fr_info_t *)); 200static void ipf_rule_delete __P((ipf_main_softc_t *, frentry_t *f, 201 int, int)); 202static void ipf_rule_expire_insert __P((ipf_main_softc_t *, 203 frentry_t *, int)); 204static int ipf_synclist __P((ipf_main_softc_t *, frentry_t *, 205 void *)); 206static void ipf_token_flush __P((ipf_main_softc_t *)); 207static void ipf_token_unlink __P((ipf_main_softc_t *, 208 ipftoken_t *)); 209static ipftuneable_t *ipf_tune_findbyname __P((ipftuneable_t *, 210 const char *)); 211static ipftuneable_t *ipf_tune_findbycookie __P((ipftuneable_t **, void *, 212 void **)); 213static int ipf_updateipid __P((fr_info_t *)); 214static int ipf_settimeout __P((struct ipf_main_softc_s *, 215 struct ipftuneable *, 216 ipftuneval_t *)); 217#if !defined(_KERNEL) || (!defined(__NetBSD__) && !defined(__OpenBSD__) && \ 218 !defined(__FreeBSD__)) || \ 219 FREEBSD_LT_REV(501000) || NETBSD_LT_REV(105000000) || \ 220 OPENBSD_LT_REV(200006) 221static int ppsratecheck(struct timeval *, int *, int); 222#endif 223 224 225/* 226 * bit values for identifying presence of individual IP options 227 * All of these tables should be ordered by increasing key value on the left 228 * hand side to allow for binary searching of the array and include a trailer 229 * with a 0 for the bitmask for linear searches to easily find the end with. 230 */ 231static const struct optlist ipopts[20] = { 232 { IPOPT_NOP, 0x000001 }, 233 { IPOPT_RR, 0x000002 }, 234 { IPOPT_ZSU, 0x000004 }, 235 { IPOPT_MTUP, 0x000008 }, 236 { IPOPT_MTUR, 0x000010 }, 237 { IPOPT_ENCODE, 0x000020 }, 238 { IPOPT_TS, 0x000040 }, 239 { IPOPT_TR, 0x000080 }, 240 { IPOPT_SECURITY, 0x000100 }, 241 { IPOPT_LSRR, 0x000200 }, 242 { IPOPT_E_SEC, 0x000400 }, 243 { IPOPT_CIPSO, 0x000800 }, 244 { IPOPT_SATID, 0x001000 }, 245 { IPOPT_SSRR, 0x002000 }, 246 { IPOPT_ADDEXT, 0x004000 }, 247 { IPOPT_VISA, 0x008000 }, 248 { IPOPT_IMITD, 0x010000 }, 249 { IPOPT_EIP, 0x020000 }, 250 { IPOPT_FINN, 0x040000 }, 251 { 0, 0x000000 } 252}; 253 254#ifdef USE_INET6 255static const struct optlist ip6exthdr[] = { 256 { IPPROTO_HOPOPTS, 0x000001 }, 257 { IPPROTO_IPV6, 0x000002 }, 258 { IPPROTO_ROUTING, 0x000004 }, 259 { IPPROTO_FRAGMENT, 0x000008 }, 260 { IPPROTO_ESP, 0x000010 }, 261 { IPPROTO_AH, 0x000020 }, 262 { IPPROTO_NONE, 0x000040 }, 263 { IPPROTO_DSTOPTS, 0x000080 }, 264 { IPPROTO_MOBILITY, 0x000100 }, 265 { 0, 0 } 266}; 267#endif 268 269/* 270 * bit values for identifying presence of individual IP security options 271 */ 272static const struct optlist secopt[8] = { 273 { IPSO_CLASS_RES4, 0x01 }, 274 { IPSO_CLASS_TOPS, 0x02 }, 275 { IPSO_CLASS_SECR, 0x04 }, 276 { IPSO_CLASS_RES3, 0x08 }, 277 { IPSO_CLASS_CONF, 0x10 }, 278 { IPSO_CLASS_UNCL, 0x20 }, 279 { IPSO_CLASS_RES2, 0x40 }, 280 { IPSO_CLASS_RES1, 0x80 } 281}; 282 283char ipfilter_version[] = IPL_VERSION; 284 285int ipf_features = 0 286#ifdef IPFILTER_LKM 287 | IPF_FEAT_LKM 288#endif 289#ifdef IPFILTER_LOG 290 | IPF_FEAT_LOG 291#endif 292 | IPF_FEAT_LOOKUP 293#ifdef IPFILTER_BPF 294 | IPF_FEAT_BPF 295#endif 296#ifdef IPFILTER_COMPILED 297 | IPF_FEAT_COMPILED 298#endif 299#ifdef IPFILTER_CKSUM 300 | IPF_FEAT_CKSUM 301#endif 302 | IPF_FEAT_SYNC 303#ifdef IPFILTER_SCAN 304 | IPF_FEAT_SCAN 305#endif 306#ifdef USE_INET6 307 | IPF_FEAT_IPV6 308#endif 309 ; 310 311 312/* 313 * Table of functions available for use with call rules. 314 */ 315static ipfunc_resolve_t ipf_availfuncs[] = { 316 { "srcgrpmap", ipf_srcgrpmap, ipf_grpmapinit, ipf_grpmapfini }, 317 { "dstgrpmap", ipf_dstgrpmap, ipf_grpmapinit, ipf_grpmapfini }, 318 { "", NULL, NULL, NULL } 319}; 320 321static ipftuneable_t ipf_main_tuneables[] = { 322 { { (void *)offsetof(struct ipf_main_softc_s, ipf_flags) }, 323 "ipf_flags", 0, 0xffffffff, 324 stsizeof(ipf_main_softc_t, ipf_flags), 325 0, NULL, NULL }, 326 { { (void *)offsetof(struct ipf_main_softc_s, ipf_active) }, 327 "active", 0, 0, 328 stsizeof(ipf_main_softc_t, ipf_active), 329 IPFT_RDONLY, NULL, NULL }, 330 { { (void *)offsetof(ipf_main_softc_t, ipf_control_forwarding) }, 331 "control_forwarding", 0, 1, 332 stsizeof(ipf_main_softc_t, ipf_control_forwarding), 333 0, NULL, NULL }, 334 { { (void *)offsetof(ipf_main_softc_t, ipf_update_ipid) }, 335 "update_ipid", 0, 1, 336 stsizeof(ipf_main_softc_t, ipf_update_ipid), 337 0, NULL, NULL }, 338 { { (void *)offsetof(ipf_main_softc_t, ipf_chksrc) }, 339 "chksrc", 0, 1, 340 stsizeof(ipf_main_softc_t, ipf_chksrc), 341 0, NULL, NULL }, 342 { { (void *)offsetof(ipf_main_softc_t, ipf_minttl) }, 343 "min_ttl", 0, 1, 344 stsizeof(ipf_main_softc_t, ipf_minttl), 345 0, NULL, NULL }, 346 { { (void *)offsetof(ipf_main_softc_t, ipf_icmpminfragmtu) }, 347 "icmp_minfragmtu", 0, 1, 348 stsizeof(ipf_main_softc_t, ipf_icmpminfragmtu), 349 0, NULL, NULL }, 350 { { (void *)offsetof(ipf_main_softc_t, ipf_pass) }, 351 "default_pass", 0, 0xffffffff, 352 stsizeof(ipf_main_softc_t, ipf_pass), 353 0, NULL, NULL }, 354 { { (void *)offsetof(ipf_main_softc_t, ipf_tcpidletimeout) }, 355 "tcp_idle_timeout", 1, 0x7fffffff, 356 stsizeof(ipf_main_softc_t, ipf_tcpidletimeout), 357 0, NULL, ipf_settimeout }, 358 { { (void *)offsetof(ipf_main_softc_t, ipf_tcpclosewait) }, 359 "tcp_close_wait", 1, 0x7fffffff, 360 stsizeof(ipf_main_softc_t, ipf_tcpclosewait), 361 0, NULL, ipf_settimeout }, 362 { { (void *)offsetof(ipf_main_softc_t, ipf_tcplastack) }, 363 "tcp_last_ack", 1, 0x7fffffff, 364 stsizeof(ipf_main_softc_t, ipf_tcplastack), 365 0, NULL, ipf_settimeout }, 366 { { (void *)offsetof(ipf_main_softc_t, ipf_tcptimeout) }, 367 "tcp_timeout", 1, 0x7fffffff, 368 stsizeof(ipf_main_softc_t, ipf_tcptimeout), 369 0, NULL, ipf_settimeout }, 370 { { (void *)offsetof(ipf_main_softc_t, ipf_tcpsynsent) }, 371 "tcp_syn_sent", 1, 0x7fffffff, 372 stsizeof(ipf_main_softc_t, ipf_tcpsynsent), 373 0, NULL, ipf_settimeout }, 374 { { (void *)offsetof(ipf_main_softc_t, ipf_tcpsynrecv) }, 375 "tcp_syn_received", 1, 0x7fffffff, 376 stsizeof(ipf_main_softc_t, ipf_tcpsynrecv), 377 0, NULL, ipf_settimeout }, 378 { { (void *)offsetof(ipf_main_softc_t, ipf_tcpclosed) }, 379 "tcp_closed", 1, 0x7fffffff, 380 stsizeof(ipf_main_softc_t, ipf_tcpclosed), 381 0, NULL, ipf_settimeout }, 382 { { (void *)offsetof(ipf_main_softc_t, ipf_tcphalfclosed) }, 383 "tcp_half_closed", 1, 0x7fffffff, 384 stsizeof(ipf_main_softc_t, ipf_tcphalfclosed), 385 0, NULL, ipf_settimeout }, 386 { { (void *)offsetof(ipf_main_softc_t, ipf_tcptimewait) }, 387 "tcp_time_wait", 1, 0x7fffffff, 388 stsizeof(ipf_main_softc_t, ipf_tcptimewait), 389 0, NULL, ipf_settimeout }, 390 { { (void *)offsetof(ipf_main_softc_t, ipf_udptimeout) }, 391 "udp_timeout", 1, 0x7fffffff, 392 stsizeof(ipf_main_softc_t, ipf_udptimeout), 393 0, NULL, ipf_settimeout }, 394 { { (void *)offsetof(ipf_main_softc_t, ipf_udpacktimeout) }, 395 "udp_ack_timeout", 1, 0x7fffffff, 396 stsizeof(ipf_main_softc_t, ipf_udpacktimeout), 397 0, NULL, ipf_settimeout }, 398 { { (void *)offsetof(ipf_main_softc_t, ipf_icmptimeout) }, 399 "icmp_timeout", 1, 0x7fffffff, 400 stsizeof(ipf_main_softc_t, ipf_icmptimeout), 401 0, NULL, ipf_settimeout }, 402 { { (void *)offsetof(ipf_main_softc_t, ipf_icmpacktimeout) }, 403 "icmp_ack_timeout", 1, 0x7fffffff, 404 stsizeof(ipf_main_softc_t, ipf_icmpacktimeout), 405 0, NULL, ipf_settimeout }, 406 { { (void *)offsetof(ipf_main_softc_t, ipf_iptimeout) }, 407 "ip_timeout", 1, 0x7fffffff, 408 stsizeof(ipf_main_softc_t, ipf_iptimeout), 409 0, NULL, ipf_settimeout }, 410#if defined(INSTANCES) && defined(_KERNEL) 411 { { (void *)offsetof(ipf_main_softc_t, ipf_get_loopback) }, 412 "intercept_loopback", 0, 1, 413 stsizeof(ipf_main_softc_t, ipf_get_loopback), 414 0, NULL, ipf_set_loopback }, 415#endif 416 { { 0 }, 417 NULL, 0, 0, 418 0, 419 0, NULL, NULL } 420}; 421 422 423/* 424 * The next section of code is a a collection of small routines that set 425 * fields in the fr_info_t structure passed based on properties of the 426 * current packet. There are different routines for the same protocol 427 * for each of IPv4 and IPv6. Adding a new protocol, for which there 428 * will "special" inspection for setup, is now more easily done by adding 429 * a new routine and expanding the ipf_pr_ipinit*() function rather than by 430 * adding more code to a growing switch statement. 431 */ 432#ifdef USE_INET6 433static INLINE int ipf_pr_ah6 __P((fr_info_t *)); 434static INLINE void ipf_pr_esp6 __P((fr_info_t *)); 435static INLINE void ipf_pr_gre6 __P((fr_info_t *)); 436static INLINE void ipf_pr_udp6 __P((fr_info_t *)); 437static INLINE void ipf_pr_tcp6 __P((fr_info_t *)); 438static INLINE void ipf_pr_icmp6 __P((fr_info_t *)); 439static INLINE void ipf_pr_ipv6hdr __P((fr_info_t *)); 440static INLINE void ipf_pr_short6 __P((fr_info_t *, int)); 441static INLINE int ipf_pr_hopopts6 __P((fr_info_t *)); 442static INLINE int ipf_pr_mobility6 __P((fr_info_t *)); 443static INLINE int ipf_pr_routing6 __P((fr_info_t *)); 444static INLINE int ipf_pr_dstopts6 __P((fr_info_t *)); 445static INLINE int ipf_pr_fragment6 __P((fr_info_t *)); 446static INLINE struct ip6_ext *ipf_pr_ipv6exthdr __P((fr_info_t *, int, int)); 447 448 449/* ------------------------------------------------------------------------ */ 450/* Function: ipf_pr_short6 */ 451/* Returns: void */ 452/* Parameters: fin(I) - pointer to packet information */ 453/* xmin(I) - minimum header size */ 454/* */ 455/* IPv6 Only */ 456/* This is function enforces the 'is a packet too short to be legit' rule */ 457/* for IPv6 and marks the packet with FI_SHORT if so. See function comment */ 458/* for ipf_pr_short() for more details. */ 459/* ------------------------------------------------------------------------ */ 460static INLINE void 461ipf_pr_short6(fin, xmin) 462 fr_info_t *fin; 463 int xmin; 464{ 465 466 if (fin->fin_dlen < xmin) 467 fin->fin_flx |= FI_SHORT; 468} 469 470 471/* ------------------------------------------------------------------------ */ 472/* Function: ipf_pr_ipv6hdr */ 473/* Returns: void */ 474/* Parameters: fin(I) - pointer to packet information */ 475/* */ 476/* IPv6 Only */ 477/* Copy values from the IPv6 header into the fr_info_t struct and call the */ 478/* per-protocol analyzer if it exists. In validating the packet, a protocol*/ 479/* analyzer may pullup or free the packet itself so we need to be vigiliant */ 480/* of that possibility arising. */ 481/* ------------------------------------------------------------------------ */ 482static INLINE void 483ipf_pr_ipv6hdr(fin) 484 fr_info_t *fin; 485{ 486 ip6_t *ip6 = (ip6_t *)fin->fin_ip; 487 int p, go = 1, i, hdrcount; 488 fr_ip_t *fi = &fin->fin_fi; 489 490 fin->fin_off = 0; 491 492 fi->fi_tos = 0; 493 fi->fi_optmsk = 0; 494 fi->fi_secmsk = 0; 495 fi->fi_auth = 0; 496 497 p = ip6->ip6_nxt; 498 fin->fin_crc = p; 499 fi->fi_ttl = ip6->ip6_hlim; 500 fi->fi_src.in6 = ip6->ip6_src; 501 fin->fin_crc += fi->fi_src.i6[0]; 502 fin->fin_crc += fi->fi_src.i6[1]; 503 fin->fin_crc += fi->fi_src.i6[2]; 504 fin->fin_crc += fi->fi_src.i6[3]; 505 fi->fi_dst.in6 = ip6->ip6_dst; 506 fin->fin_crc += fi->fi_dst.i6[0]; 507 fin->fin_crc += fi->fi_dst.i6[1]; 508 fin->fin_crc += fi->fi_dst.i6[2]; 509 fin->fin_crc += fi->fi_dst.i6[3]; 510 fin->fin_id = 0; 511 if (IN6_IS_ADDR_MULTICAST(&fi->fi_dst.in6)) 512 fin->fin_flx |= FI_MULTICAST|FI_MBCAST; 513 514 hdrcount = 0; 515 while (go && !(fin->fin_flx & FI_SHORT)) { 516 switch (p) 517 { 518 case IPPROTO_UDP : 519 ipf_pr_udp6(fin); 520 go = 0; 521 break; 522 523 case IPPROTO_TCP : 524 ipf_pr_tcp6(fin); 525 go = 0; 526 break; 527 528 case IPPROTO_ICMPV6 : 529 ipf_pr_icmp6(fin); 530 go = 0; 531 break; 532 533 case IPPROTO_GRE : 534 ipf_pr_gre6(fin); 535 go = 0; 536 break; 537 538 case IPPROTO_HOPOPTS : 539 p = ipf_pr_hopopts6(fin); 540 break; 541 542 case IPPROTO_MOBILITY : 543 p = ipf_pr_mobility6(fin); 544 break; 545 546 case IPPROTO_DSTOPTS : 547 p = ipf_pr_dstopts6(fin); 548 break; 549 550 case IPPROTO_ROUTING : 551 p = ipf_pr_routing6(fin); 552 break; 553 554 case IPPROTO_AH : 555 p = ipf_pr_ah6(fin); 556 break; 557 558 case IPPROTO_ESP : 559 ipf_pr_esp6(fin); 560 go = 0; 561 break; 562 563 case IPPROTO_IPV6 : 564 for (i = 0; ip6exthdr[i].ol_bit != 0; i++) 565 if (ip6exthdr[i].ol_val == p) { 566 fin->fin_flx |= ip6exthdr[i].ol_bit; 567 break; 568 } 569 go = 0; 570 break; 571 572 case IPPROTO_NONE : 573 go = 0; 574 break; 575 576 case IPPROTO_FRAGMENT : 577 p = ipf_pr_fragment6(fin); 578 /* 579 * Given that the only fragments we want to let through 580 * (where fin_off != 0) are those where the non-first 581 * fragments only have data, we can safely stop looking 582 * at headers if this is a non-leading fragment. 583 */ 584 if (fin->fin_off != 0) 585 go = 0; 586 break; 587 588 default : 589 go = 0; 590 break; 591 } 592 hdrcount++; 593 594 /* 595 * It is important to note that at this point, for the 596 * extension headers (go != 0), the entire header may not have 597 * been pulled up when the code gets to this point. This is 598 * only done for "go != 0" because the other header handlers 599 * will all pullup their complete header. The other indicator 600 * of an incomplete packet is that this was just an extension 601 * header. 602 */ 603 if ((go != 0) && (p != IPPROTO_NONE) && 604 (ipf_pr_pullup(fin, 0) == -1)) { 605 p = IPPROTO_NONE; 606 break; 607 } 608 } 609 610 /* 611 * Some of the above functions, like ipf_pr_esp6(), can call ipf_pullup 612 * and destroy whatever packet was here. The caller of this function 613 * expects us to return if there is a problem with ipf_pullup. 614 */ 615 if (fin->fin_m == NULL) { 616 ipf_main_softc_t *softc = fin->fin_main_soft; 617 618 LBUMPD(ipf_stats[fin->fin_out], fr_v6_bad); 619 return; 620 } 621 622 fi->fi_p = p; 623 624 /* 625 * IPv6 fragment case 1 - see comment for ipf_pr_fragment6(). 626 * "go != 0" imples the above loop hasn't arrived at a layer 4 header. 627 */ 628 if ((go != 0) && (fin->fin_flx & FI_FRAG) && (fin->fin_off == 0)) { 629 ipf_main_softc_t *softc = fin->fin_main_soft; 630 631 fin->fin_flx |= FI_BAD; 632 LBUMPD(ipf_stats[fin->fin_out], fr_v6_badfrag); 633 LBUMP(ipf_stats[fin->fin_out].fr_v6_bad); 634 } 635} 636 637 638/* ------------------------------------------------------------------------ */ 639/* Function: ipf_pr_ipv6exthdr */ 640/* Returns: struct ip6_ext * - pointer to the start of the next header */ 641/* or NULL if there is a prolblem. */ 642/* Parameters: fin(I) - pointer to packet information */ 643/* multiple(I) - flag indicating yes/no if multiple occurances */ 644/* of this extension header are allowed. */ 645/* proto(I) - protocol number for this extension header */ 646/* */ 647/* IPv6 Only */ 648/* This function embodies a number of common checks that all IPv6 extension */ 649/* headers must be subjected to. For example, making sure the packet is */ 650/* big enough for it to be in, checking if it is repeated and setting a */ 651/* flag to indicate its presence. */ 652/* ------------------------------------------------------------------------ */ 653static INLINE struct ip6_ext * 654ipf_pr_ipv6exthdr(fin, multiple, proto) 655 fr_info_t *fin; 656 int multiple, proto; 657{ 658 ipf_main_softc_t *softc = fin->fin_main_soft; 659 struct ip6_ext *hdr; 660 u_short shift; 661 int i; 662 663 fin->fin_flx |= FI_V6EXTHDR; 664 665 /* 8 is default length of extension hdr */ 666 if ((fin->fin_dlen - 8) < 0) { 667 fin->fin_flx |= FI_SHORT; 668 LBUMPD(ipf_stats[fin->fin_out], fr_v6_ext_short); 669 return NULL; 670 } 671 672 if (ipf_pr_pullup(fin, 8) == -1) { 673 LBUMPD(ipf_stats[fin->fin_out], fr_v6_ext_pullup); 674 return NULL; 675 } 676 677 hdr = fin->fin_dp; 678 switch (proto) 679 { 680 case IPPROTO_FRAGMENT : 681 shift = 8; 682 break; 683 default : 684 shift = 8 + (hdr->ip6e_len << 3); 685 break; 686 } 687 688 if (shift > fin->fin_dlen) { /* Nasty extension header length? */ 689 fin->fin_flx |= FI_BAD; 690 LBUMPD(ipf_stats[fin->fin_out], fr_v6_ext_hlen); 691 return NULL; 692 } 693 694 fin->fin_dp = (char *)fin->fin_dp + shift; 695 fin->fin_dlen -= shift; 696 697 /* 698 * If we have seen a fragment header, do not set any flags to indicate 699 * the presence of this extension header as it has no impact on the 700 * end result until after it has been defragmented. 701 */ 702 if (fin->fin_flx & FI_FRAG) 703 return hdr; 704 705 for (i = 0; ip6exthdr[i].ol_bit != 0; i++) 706 if (ip6exthdr[i].ol_val == proto) { 707 /* 708 * Most IPv6 extension headers are only allowed once. 709 */ 710 if ((multiple == 0) && 711 ((fin->fin_optmsk & ip6exthdr[i].ol_bit) != 0)) 712 fin->fin_flx |= FI_BAD; 713 else 714 fin->fin_optmsk |= ip6exthdr[i].ol_bit; 715 break; 716 } 717 718 return hdr; 719} 720 721 722/* ------------------------------------------------------------------------ */ 723/* Function: ipf_pr_hopopts6 */ 724/* Returns: int - value of the next header or IPPROTO_NONE if error */ 725/* Parameters: fin(I) - pointer to packet information */ 726/* */ 727/* IPv6 Only */ 728/* This is function checks pending hop by hop options extension header */ 729/* ------------------------------------------------------------------------ */ 730static INLINE int 731ipf_pr_hopopts6(fin) 732 fr_info_t *fin; 733{ 734 struct ip6_ext *hdr; 735 736 hdr = ipf_pr_ipv6exthdr(fin, 0, IPPROTO_HOPOPTS); 737 if (hdr == NULL) 738 return IPPROTO_NONE; 739 return hdr->ip6e_nxt; 740} 741 742 743/* ------------------------------------------------------------------------ */ 744/* Function: ipf_pr_mobility6 */ 745/* Returns: int - value of the next header or IPPROTO_NONE if error */ 746/* Parameters: fin(I) - pointer to packet information */ 747/* */ 748/* IPv6 Only */ 749/* This is function checks the IPv6 mobility extension header */ 750/* ------------------------------------------------------------------------ */ 751static INLINE int 752ipf_pr_mobility6(fin) 753 fr_info_t *fin; 754{ 755 struct ip6_ext *hdr; 756 757 hdr = ipf_pr_ipv6exthdr(fin, 0, IPPROTO_MOBILITY); 758 if (hdr == NULL) 759 return IPPROTO_NONE; 760 return hdr->ip6e_nxt; 761} 762 763 764/* ------------------------------------------------------------------------ */ 765/* Function: ipf_pr_routing6 */ 766/* Returns: int - value of the next header or IPPROTO_NONE if error */ 767/* Parameters: fin(I) - pointer to packet information */ 768/* */ 769/* IPv6 Only */ 770/* This is function checks pending routing extension header */ 771/* ------------------------------------------------------------------------ */ 772static INLINE int 773ipf_pr_routing6(fin) 774 fr_info_t *fin; 775{ 776 struct ip6_routing *hdr; 777 778 hdr = (struct ip6_routing *)ipf_pr_ipv6exthdr(fin, 0, IPPROTO_ROUTING); 779 if (hdr == NULL) 780 return IPPROTO_NONE; 781 782 switch (hdr->ip6r_type) 783 { 784 case 0 : 785 /* 786 * Nasty extension header length? 787 */ 788 if (((hdr->ip6r_len >> 1) < hdr->ip6r_segleft) || 789 (hdr->ip6r_segleft && (hdr->ip6r_len & 1))) { 790 ipf_main_softc_t *softc = fin->fin_main_soft; 791 792 fin->fin_flx |= FI_BAD; 793 LBUMPD(ipf_stats[fin->fin_out], fr_v6_rh_bad); 794 return IPPROTO_NONE; 795 } 796 break; 797 798 default : 799 break; 800 } 801 802 return hdr->ip6r_nxt; 803} 804 805 806/* ------------------------------------------------------------------------ */ 807/* Function: ipf_pr_fragment6 */ 808/* Returns: int - value of the next header or IPPROTO_NONE if error */ 809/* Parameters: fin(I) - pointer to packet information */ 810/* */ 811/* IPv6 Only */ 812/* Examine the IPv6 fragment header and extract fragment offset information.*/ 813/* */ 814/* Fragments in IPv6 are extraordinarily difficult to deal with - much more */ 815/* so than in IPv4. There are 5 cases of fragments with IPv6 that all */ 816/* packets with a fragment header can fit into. They are as follows: */ 817/* */ 818/* 1. [IPv6][0-n EH][FH][0-n EH] (no L4HDR present) */ 819/* 2. [IPV6][0-n EH][FH][0-n EH][L4HDR part] (short) */ 820/* 3. [IPV6][0-n EH][FH][L4HDR part][0-n data] (short) */ 821/* 4. [IPV6][0-n EH][FH][0-n EH][L4HDR][0-n data] */ 822/* 5. [IPV6][0-n EH][FH][data] */ 823/* */ 824/* IPV6 = IPv6 header, FH = Fragment Header, */ 825/* 0-n EH = 0 or more extension headers, 0-n data = 0 or more bytes of data */ 826/* */ 827/* Packets that match 1, 2, 3 will be dropped as the only reasonable */ 828/* scenario in which they happen is in extreme circumstances that are most */ 829/* likely to be an indication of an attack rather than normal traffic. */ 830/* A type 3 packet may be sent by an attacked after a type 4 packet. There */ 831/* are two rules that can be used to guard against type 3 packets: L4 */ 832/* headers must always be in a packet that has the offset field set to 0 */ 833/* and no packet is allowed to overlay that where offset = 0. */ 834/* ------------------------------------------------------------------------ */ 835static INLINE int 836ipf_pr_fragment6(fin) 837 fr_info_t *fin; 838{ 839 ipf_main_softc_t *softc = fin->fin_main_soft; 840 struct ip6_frag *frag; 841 842 fin->fin_flx |= FI_FRAG; 843 844 frag = (struct ip6_frag *)ipf_pr_ipv6exthdr(fin, 0, IPPROTO_FRAGMENT); 845 if (frag == NULL) { 846 LBUMPD(ipf_stats[fin->fin_out], fr_v6_frag_bad); 847 return IPPROTO_NONE; 848 } 849 850 if ((frag->ip6f_offlg & IP6F_MORE_FRAG) != 0) { 851 /* 852 * Any fragment that isn't the last fragment must have its 853 * length as a multiple of 8. 854 */ 855 if ((fin->fin_plen & 7) != 0) 856 fin->fin_flx |= FI_BAD; 857 } 858 859 fin->fin_fraghdr = frag; 860 fin->fin_id = frag->ip6f_ident; 861 fin->fin_off = ntohs(frag->ip6f_offlg & IP6F_OFF_MASK); 862 if (fin->fin_off != 0) 863 fin->fin_flx |= FI_FRAGBODY; 864 865 /* 866 * Jumbograms aren't handled, so the max. length is 64k 867 */ 868 if ((fin->fin_off << 3) + fin->fin_dlen > 65535) 869 fin->fin_flx |= FI_BAD; 870 871 /* 872 * We don't know where the transport layer header (or whatever is next 873 * is), as it could be behind destination options (amongst others) so 874 * return the fragment header as the type of packet this is. Note that 875 * this effectively disables the fragment cache for > 1 protocol at a 876 * time. 877 */ 878 return frag->ip6f_nxt; 879} 880 881 882/* ------------------------------------------------------------------------ */ 883/* Function: ipf_pr_dstopts6 */ 884/* Returns: int - value of the next header or IPPROTO_NONE if error */ 885/* Parameters: fin(I) - pointer to packet information */ 886/* */ 887/* IPv6 Only */ 888/* This is function checks pending destination options extension header */ 889/* ------------------------------------------------------------------------ */ 890static INLINE int 891ipf_pr_dstopts6(fin) 892 fr_info_t *fin; 893{ 894 ipf_main_softc_t *softc = fin->fin_main_soft; 895 struct ip6_ext *hdr; 896 897 hdr = ipf_pr_ipv6exthdr(fin, 0, IPPROTO_DSTOPTS); 898 if (hdr == NULL) { 899 LBUMPD(ipf_stats[fin->fin_out], fr_v6_dst_bad); 900 return IPPROTO_NONE; 901 } 902 return hdr->ip6e_nxt; 903} 904 905 906/* ------------------------------------------------------------------------ */ 907/* Function: ipf_pr_icmp6 */ 908/* Returns: void */ 909/* Parameters: fin(I) - pointer to packet information */ 910/* */ 911/* IPv6 Only */ 912/* This routine is mainly concerned with determining the minimum valid size */ 913/* for an ICMPv6 packet. */ 914/* ------------------------------------------------------------------------ */ 915static INLINE void 916ipf_pr_icmp6(fin) 917 fr_info_t *fin; 918{ 919 int minicmpsz = sizeof(struct icmp6_hdr); 920 struct icmp6_hdr *icmp6; 921 922 if (ipf_pr_pullup(fin, ICMP6ERR_MINPKTLEN - sizeof(ip6_t)) == -1) { 923 ipf_main_softc_t *softc = fin->fin_main_soft; 924 925 LBUMPD(ipf_stats[fin->fin_out], fr_v6_icmp6_pullup); 926 return; 927 } 928 929 if (fin->fin_dlen > 1) { 930 ip6_t *ip6; 931 932 icmp6 = fin->fin_dp; 933 934 fin->fin_data[0] = *(u_short *)icmp6; 935 936 if ((icmp6->icmp6_type & ICMP6_INFOMSG_MASK) != 0) 937 fin->fin_flx |= FI_ICMPQUERY; 938 939 switch (icmp6->icmp6_type) 940 { 941 case ICMP6_ECHO_REPLY : 942 case ICMP6_ECHO_REQUEST : 943 if (fin->fin_dlen >= 6) 944 fin->fin_data[1] = icmp6->icmp6_id; 945 minicmpsz = ICMP6ERR_MINPKTLEN - sizeof(ip6_t); 946 break; 947 948 case ICMP6_DST_UNREACH : 949 case ICMP6_PACKET_TOO_BIG : 950 case ICMP6_TIME_EXCEEDED : 951 case ICMP6_PARAM_PROB : 952 fin->fin_flx |= FI_ICMPERR; 953 minicmpsz = ICMP6ERR_IPICMPHLEN - sizeof(ip6_t); 954 if (fin->fin_plen < ICMP6ERR_IPICMPHLEN) 955 break; 956 957 if (M_LEN(fin->fin_m) < fin->fin_plen) { 958 if (ipf_coalesce(fin) != 1) 959 return; 960 } 961 962 if (ipf_pr_pullup(fin, ICMP6ERR_MINPKTLEN) == -1) 963 return; 964 965 /* 966 * If the destination of this packet doesn't match the 967 * source of the original packet then this packet is 968 * not correct. 969 */ 970 icmp6 = fin->fin_dp; 971 ip6 = (ip6_t *)((char *)icmp6 + ICMPERR_ICMPHLEN); 972 if (IP6_NEQ(&fin->fin_fi.fi_dst, 973 (i6addr_t *)&ip6->ip6_src)) 974 fin->fin_flx |= FI_BAD; 975 break; 976 default : 977 break; 978 } 979 } 980 981 ipf_pr_short6(fin, minicmpsz); 982 if ((fin->fin_flx & (FI_SHORT|FI_BAD)) == 0) { 983 u_char p = fin->fin_p; 984 985 fin->fin_p = IPPROTO_ICMPV6; 986 ipf_checkv6sum(fin); 987 fin->fin_p = p; 988 } 989} 990 991 992/* ------------------------------------------------------------------------ */ 993/* Function: ipf_pr_udp6 */ 994/* Returns: void */ 995/* Parameters: fin(I) - pointer to packet information */ 996/* */ 997/* IPv6 Only */ 998/* Analyse the packet for IPv6/UDP properties. */ 999/* Is not expected to be called for fragmented packets. */ 1000/* ------------------------------------------------------------------------ */ 1001static INLINE void 1002ipf_pr_udp6(fin) 1003 fr_info_t *fin; 1004{ 1005 1006 if (ipf_pr_udpcommon(fin) == 0) { 1007 u_char p = fin->fin_p; 1008 1009 fin->fin_p = IPPROTO_UDP; 1010 ipf_checkv6sum(fin); 1011 fin->fin_p = p; 1012 } 1013} 1014 1015 1016/* ------------------------------------------------------------------------ */ 1017/* Function: ipf_pr_tcp6 */ 1018/* Returns: void */ 1019/* Parameters: fin(I) - pointer to packet information */ 1020/* */ 1021/* IPv6 Only */ 1022/* Analyse the packet for IPv6/TCP properties. */ 1023/* Is not expected to be called for fragmented packets. */ 1024/* ------------------------------------------------------------------------ */ 1025static INLINE void 1026ipf_pr_tcp6(fin) 1027 fr_info_t *fin; 1028{ 1029 1030 if (ipf_pr_tcpcommon(fin) == 0) { 1031 u_char p = fin->fin_p; 1032 1033 fin->fin_p = IPPROTO_TCP; 1034 ipf_checkv6sum(fin); 1035 fin->fin_p = p; 1036 } 1037} 1038 1039 1040/* ------------------------------------------------------------------------ */ 1041/* Function: ipf_pr_esp6 */ 1042/* Returns: void */ 1043/* Parameters: fin(I) - pointer to packet information */ 1044/* */ 1045/* IPv6 Only */ 1046/* Analyse the packet for ESP properties. */ 1047/* The minimum length is taken to be the SPI (32bits) plus a tail (32bits) */ 1048/* even though the newer ESP packets must also have a sequence number that */ 1049/* is 32bits as well, it is not possible(?) to determine the version from a */ 1050/* simple packet header. */ 1051/* ------------------------------------------------------------------------ */ 1052static INLINE void 1053ipf_pr_esp6(fin) 1054 fr_info_t *fin; 1055{ 1056 1057 if ((fin->fin_off == 0) && (ipf_pr_pullup(fin, 8) == -1)) { 1058 ipf_main_softc_t *softc = fin->fin_main_soft; 1059 1060 LBUMPD(ipf_stats[fin->fin_out], fr_v6_esp_pullup); 1061 return; 1062 } 1063} 1064 1065 1066/* ------------------------------------------------------------------------ */ 1067/* Function: ipf_pr_ah6 */ 1068/* Returns: int - value of the next header or IPPROTO_NONE if error */ 1069/* Parameters: fin(I) - pointer to packet information */ 1070/* */ 1071/* IPv6 Only */ 1072/* Analyse the packet for AH properties. */ 1073/* The minimum length is taken to be the combination of all fields in the */ 1074/* header being present and no authentication data (null algorithm used.) */ 1075/* ------------------------------------------------------------------------ */ 1076static INLINE int 1077ipf_pr_ah6(fin) 1078 fr_info_t *fin; 1079{ 1080 authhdr_t *ah; 1081 1082 fin->fin_flx |= FI_AH; 1083 1084 ah = (authhdr_t *)ipf_pr_ipv6exthdr(fin, 0, IPPROTO_HOPOPTS); 1085 if (ah == NULL) { 1086 ipf_main_softc_t *softc = fin->fin_main_soft; 1087 1088 LBUMPD(ipf_stats[fin->fin_out], fr_v6_ah_bad); 1089 return IPPROTO_NONE; 1090 } 1091 1092 ipf_pr_short6(fin, sizeof(*ah)); 1093 1094 /* 1095 * No need for another pullup, ipf_pr_ipv6exthdr() will pullup 1096 * enough data to satisfy ah_next (the very first one.) 1097 */ 1098 return ah->ah_next; 1099} 1100 1101 1102/* ------------------------------------------------------------------------ */ 1103/* Function: ipf_pr_gre6 */ 1104/* Returns: void */ 1105/* Parameters: fin(I) - pointer to packet information */ 1106/* */ 1107/* Analyse the packet for GRE properties. */ 1108/* ------------------------------------------------------------------------ */ 1109static INLINE void 1110ipf_pr_gre6(fin) 1111 fr_info_t *fin; 1112{ 1113 grehdr_t *gre; 1114 1115 if (ipf_pr_pullup(fin, sizeof(grehdr_t)) == -1) { 1116 ipf_main_softc_t *softc = fin->fin_main_soft; 1117 1118 LBUMPD(ipf_stats[fin->fin_out], fr_v6_gre_pullup); 1119 return; 1120 } 1121 1122 gre = fin->fin_dp; 1123 if (GRE_REV(gre->gr_flags) == 1) 1124 fin->fin_data[0] = gre->gr_call; 1125} 1126#endif /* USE_INET6 */ 1127 1128 1129/* ------------------------------------------------------------------------ */ 1130/* Function: ipf_pr_pullup */ 1131/* Returns: int - 0 == pullup succeeded, -1 == failure */ 1132/* Parameters: fin(I) - pointer to packet information */ 1133/* plen(I) - length (excluding L3 header) to pullup */ 1134/* */ 1135/* Short inline function to cut down on code duplication to perform a call */ 1136/* to ipf_pullup to ensure there is the required amount of data, */ 1137/* consecutively in the packet buffer. */ 1138/* */ 1139/* This function pulls up 'extra' data at the location of fin_dp. fin_dp */ 1140/* points to the first byte after the complete layer 3 header, which will */ 1141/* include all of the known extension headers for IPv6 or options for IPv4. */ 1142/* */ 1143/* Since fr_pullup() expects the total length of bytes to be pulled up, it */ 1144/* is necessary to add those we can already assume to be pulled up (fin_dp */ 1145/* - fin_ip) to what is passed through. */ 1146/* ------------------------------------------------------------------------ */ 1147int 1148ipf_pr_pullup(fin, plen) 1149 fr_info_t *fin; 1150 int plen; 1151{ 1152 ipf_main_softc_t *softc = fin->fin_main_soft; 1153 1154 if (fin->fin_m != NULL) { 1155 if (fin->fin_dp != NULL) 1156 plen += (char *)fin->fin_dp - 1157 ((char *)fin->fin_ip + fin->fin_hlen); 1158 plen += fin->fin_hlen; 1159 if (M_LEN(fin->fin_m) < plen + fin->fin_ipoff) { 1160#if defined(_KERNEL) 1161 if (ipf_pullup(fin->fin_m, fin, plen) == NULL) { 1162 DT(ipf_pullup_fail); 1163 LBUMP(ipf_stats[fin->fin_out].fr_pull[1]); 1164 return -1; 1165 } 1166 LBUMP(ipf_stats[fin->fin_out].fr_pull[0]); 1167#else 1168 LBUMP(ipf_stats[fin->fin_out].fr_pull[1]); 1169 /* 1170 * Fake ipf_pullup failing 1171 */ 1172 fin->fin_reason = FRB_PULLUP; 1173 *fin->fin_mp = NULL; 1174 fin->fin_m = NULL; 1175 fin->fin_ip = NULL; 1176 return -1; 1177#endif 1178 } 1179 } 1180 return 0; 1181} 1182 1183 1184/* ------------------------------------------------------------------------ */ 1185/* Function: ipf_pr_short */ 1186/* Returns: void */ 1187/* Parameters: fin(I) - pointer to packet information */ 1188/* xmin(I) - minimum header size */ 1189/* */ 1190/* Check if a packet is "short" as defined by xmin. The rule we are */ 1191/* applying here is that the packet must not be fragmented within the layer */ 1192/* 4 header. That is, it must not be a fragment that has its offset set to */ 1193/* start within the layer 4 header (hdrmin) or if it is at offset 0, the */ 1194/* entire layer 4 header must be present (min). */ 1195/* ------------------------------------------------------------------------ */ 1196static INLINE void 1197ipf_pr_short(fin, xmin) 1198 fr_info_t *fin; 1199 int xmin; 1200{ 1201 1202 if (fin->fin_off == 0) { 1203 if (fin->fin_dlen < xmin) 1204 fin->fin_flx |= FI_SHORT; 1205 } else if (fin->fin_off < xmin) { 1206 fin->fin_flx |= FI_SHORT; 1207 } 1208} 1209 1210 1211/* ------------------------------------------------------------------------ */ 1212/* Function: ipf_pr_icmp */ 1213/* Returns: void */ 1214/* Parameters: fin(I) - pointer to packet information */ 1215/* */ 1216/* IPv4 Only */ 1217/* Do a sanity check on the packet for ICMP (v4). In nearly all cases, */ 1218/* except extrememly bad packets, both type and code will be present. */ 1219/* The expected minimum size of an ICMP packet is very much dependent on */ 1220/* the type of it. */ 1221/* */ 1222/* XXX - other ICMP sanity checks? */ 1223/* ------------------------------------------------------------------------ */ 1224static INLINE void 1225ipf_pr_icmp(fin) 1226 fr_info_t *fin; 1227{ 1228 ipf_main_softc_t *softc = fin->fin_main_soft; 1229 int minicmpsz = sizeof(struct icmp); 1230 icmphdr_t *icmp; 1231 ip_t *oip; 1232 1233 ipf_pr_short(fin, ICMPERR_ICMPHLEN); 1234 1235 if (fin->fin_off != 0) { 1236 LBUMPD(ipf_stats[fin->fin_out], fr_v4_icmp_frag); 1237 return; 1238 } 1239 1240 if (ipf_pr_pullup(fin, ICMPERR_ICMPHLEN) == -1) { 1241 LBUMPD(ipf_stats[fin->fin_out], fr_v4_icmp_pullup); 1242 return; 1243 } 1244 1245 icmp = fin->fin_dp; 1246 1247 fin->fin_data[0] = *(u_short *)icmp; 1248 fin->fin_data[1] = icmp->icmp_id; 1249 1250 switch (icmp->icmp_type) 1251 { 1252 case ICMP_ECHOREPLY : 1253 case ICMP_ECHO : 1254 /* Router discovery messaes - RFC 1256 */ 1255 case ICMP_ROUTERADVERT : 1256 case ICMP_ROUTERSOLICIT : 1257 fin->fin_flx |= FI_ICMPQUERY; 1258 minicmpsz = ICMP_MINLEN; 1259 break; 1260 /* 1261 * type(1) + code(1) + cksum(2) + id(2) seq(2) + 1262 * 3 * timestamp(3 * 4) 1263 */ 1264 case ICMP_TSTAMP : 1265 case ICMP_TSTAMPREPLY : 1266 fin->fin_flx |= FI_ICMPQUERY; 1267 minicmpsz = 20; 1268 break; 1269 /* 1270 * type(1) + code(1) + cksum(2) + id(2) seq(2) + 1271 * mask(4) 1272 */ 1273 case ICMP_IREQ : 1274 case ICMP_IREQREPLY : 1275 case ICMP_MASKREQ : 1276 case ICMP_MASKREPLY : 1277 fin->fin_flx |= FI_ICMPQUERY; 1278 minicmpsz = 12; 1279 break; 1280 /* 1281 * type(1) + code(1) + cksum(2) + id(2) seq(2) + ip(20+) 1282 */ 1283 case ICMP_UNREACH : 1284#ifdef icmp_nextmtu 1285 if (icmp->icmp_code == ICMP_UNREACH_NEEDFRAG) { 1286 if (icmp->icmp_nextmtu < softc->ipf_icmpminfragmtu) 1287 fin->fin_flx |= FI_BAD; 1288 } 1289#endif 1290 /* FALLTHROUGH */ 1291 case ICMP_SOURCEQUENCH : 1292 case ICMP_REDIRECT : 1293 case ICMP_TIMXCEED : 1294 case ICMP_PARAMPROB : 1295 fin->fin_flx |= FI_ICMPERR; 1296 if (ipf_coalesce(fin) != 1) { 1297 LBUMPD(ipf_stats[fin->fin_out], fr_icmp_coalesce); 1298 return; 1299 } 1300 1301 /* 1302 * ICMP error packets should not be generated for IP 1303 * packets that are a fragment that isn't the first 1304 * fragment. 1305 */ 1306 oip = (ip_t *)((char *)fin->fin_dp + ICMPERR_ICMPHLEN); 1307 if ((ntohs(oip->ip_off) & IP_OFFMASK) != 0) 1308 fin->fin_flx |= FI_BAD; 1309 1310 /* 1311 * If the destination of this packet doesn't match the 1312 * source of the original packet then this packet is 1313 * not correct. 1314 */ 1315 if (oip->ip_src.s_addr != fin->fin_daddr) 1316 fin->fin_flx |= FI_BAD; 1317 break; 1318 default : 1319 break; 1320 } 1321 1322 ipf_pr_short(fin, minicmpsz); 1323 1324 ipf_checkv4sum(fin); 1325} 1326 1327 1328/* ------------------------------------------------------------------------ */ 1329/* Function: ipf_pr_tcpcommon */ 1330/* Returns: int - 0 = header ok, 1 = bad packet, -1 = buffer error */ 1331/* Parameters: fin(I) - pointer to packet information */ 1332/* */ 1333/* TCP header sanity checking. Look for bad combinations of TCP flags, */ 1334/* and make some checks with how they interact with other fields. */ 1335/* If compiled with IPFILTER_CKSUM, check to see if the TCP checksum is */ 1336/* valid and mark the packet as bad if not. */ 1337/* ------------------------------------------------------------------------ */ 1338static INLINE int 1339ipf_pr_tcpcommon(fin) 1340 fr_info_t *fin; 1341{ 1342 ipf_main_softc_t *softc = fin->fin_main_soft; 1343 int flags, tlen; 1344 tcphdr_t *tcp; 1345 1346 fin->fin_flx |= FI_TCPUDP; 1347 if (fin->fin_off != 0) { 1348 LBUMPD(ipf_stats[fin->fin_out], fr_tcp_frag); 1349 return 0; 1350 } 1351 1352 if (ipf_pr_pullup(fin, sizeof(*tcp)) == -1) { 1353 LBUMPD(ipf_stats[fin->fin_out], fr_tcp_pullup); 1354 return -1; 1355 } 1356 1357 tcp = fin->fin_dp; 1358 if (fin->fin_dlen > 3) { 1359 fin->fin_sport = ntohs(tcp->th_sport); 1360 fin->fin_dport = ntohs(tcp->th_dport); 1361 } 1362 1363 if ((fin->fin_flx & FI_SHORT) != 0) { 1364 LBUMPD(ipf_stats[fin->fin_out], fr_tcp_short); 1365 return 1; 1366 } 1367 1368 /* 1369 * Use of the TCP data offset *must* result in a value that is at 1370 * least the same size as the TCP header. 1371 */ 1372 tlen = TCP_OFF(tcp) << 2; 1373 if (tlen < sizeof(tcphdr_t)) { 1374 LBUMPD(ipf_stats[fin->fin_out], fr_tcp_small); 1375 fin->fin_flx |= FI_BAD; 1376 return 1; 1377 } 1378 1379 flags = tcp->th_flags; 1380 fin->fin_tcpf = tcp->th_flags; 1381 1382 /* 1383 * If the urgent flag is set, then the urgent pointer must 1384 * also be set and vice versa. Good TCP packets do not have 1385 * just one of these set. 1386 */ 1387 if ((flags & TH_URG) != 0 && (tcp->th_urp == 0)) { 1388 fin->fin_flx |= FI_BAD; 1389#if 0 1390 } else if ((flags & TH_URG) == 0 && (tcp->th_urp != 0)) { 1391 /* 1392 * Ignore this case (#if 0) as it shows up in "real" 1393 * traffic with bogus values in the urgent pointer field. 1394 */ 1395 fin->fin_flx |= FI_BAD; 1396#endif 1397 } else if (((flags & (TH_SYN|TH_FIN)) != 0) && 1398 ((flags & (TH_RST|TH_ACK)) == TH_RST)) { 1399 /* TH_FIN|TH_RST|TH_ACK seems to appear "naturally" */ 1400 fin->fin_flx |= FI_BAD; 1401#if 1 1402 } else if (((flags & TH_SYN) != 0) && 1403 ((flags & (TH_URG|TH_PUSH)) != 0)) { 1404 /* 1405 * SYN with URG and PUSH set is not for normal TCP but it is 1406 * possible(?) with T/TCP...but who uses T/TCP? 1407 */ 1408 fin->fin_flx |= FI_BAD; 1409#endif 1410 } else if (!(flags & TH_ACK)) { 1411 /* 1412 * If the ack bit isn't set, then either the SYN or 1413 * RST bit must be set. If the SYN bit is set, then 1414 * we expect the ACK field to be 0. If the ACK is 1415 * not set and if URG, PSH or FIN are set, consdier 1416 * that to indicate a bad TCP packet. 1417 */ 1418 if ((flags == TH_SYN) && (tcp->th_ack != 0)) { 1419 /* 1420 * Cisco PIX sets the ACK field to a random value. 1421 * In light of this, do not set FI_BAD until a patch 1422 * is available from Cisco to ensure that 1423 * interoperability between existing systems is 1424 * achieved. 1425 */ 1426 /*fin->fin_flx |= FI_BAD*/; 1427 } else if (!(flags & (TH_RST|TH_SYN))) { 1428 fin->fin_flx |= FI_BAD; 1429 } else if ((flags & (TH_URG|TH_PUSH|TH_FIN)) != 0) { 1430 fin->fin_flx |= FI_BAD; 1431 } 1432 } 1433 if (fin->fin_flx & FI_BAD) { 1434 LBUMPD(ipf_stats[fin->fin_out], fr_tcp_bad_flags); 1435 return 1; 1436 } 1437 1438 /* 1439 * At this point, it's not exactly clear what is to be gained by 1440 * marking up which TCP options are and are not present. The one we 1441 * are most interested in is the TCP window scale. This is only in 1442 * a SYN packet [RFC1323] so we don't need this here...? 1443 * Now if we were to analyse the header for passive fingerprinting, 1444 * then that might add some weight to adding this... 1445 */ 1446 if (tlen == sizeof(tcphdr_t)) { 1447 return 0; 1448 } 1449 1450 if (ipf_pr_pullup(fin, tlen) == -1) { 1451 LBUMPD(ipf_stats[fin->fin_out], fr_tcp_pullup); 1452 return -1; 1453 } 1454 1455#if 0 1456 tcp = fin->fin_dp; 1457 ip = fin->fin_ip; 1458 s = (u_char *)(tcp + 1); 1459 off = IP_HL(ip) << 2; 1460# ifdef _KERNEL 1461 if (fin->fin_mp != NULL) { 1462 mb_t *m = *fin->fin_mp; 1463 1464 if (off + tlen > M_LEN(m)) 1465 return; 1466 } 1467# endif 1468 for (tlen -= (int)sizeof(*tcp); tlen > 0; ) { 1469 opt = *s; 1470 if (opt == '\0') 1471 break; 1472 else if (opt == TCPOPT_NOP) 1473 ol = 1; 1474 else { 1475 if (tlen < 2) 1476 break; 1477 ol = (int)*(s + 1); 1478 if (ol < 2 || ol > tlen) 1479 break; 1480 } 1481 1482 for (i = 9, mv = 4; mv >= 0; ) { 1483 op = ipopts + i; 1484 if (opt == (u_char)op->ol_val) { 1485 optmsk |= op->ol_bit; 1486 break; 1487 } 1488 } 1489 tlen -= ol; 1490 s += ol; 1491 } 1492#endif /* 0 */ 1493 1494 return 0; 1495} 1496 1497 1498 1499/* ------------------------------------------------------------------------ */ 1500/* Function: ipf_pr_udpcommon */ 1501/* Returns: int - 0 = header ok, 1 = bad packet */ 1502/* Parameters: fin(I) - pointer to packet information */ 1503/* */ 1504/* Extract the UDP source and destination ports, if present. If compiled */ 1505/* with IPFILTER_CKSUM, check to see if the UDP checksum is valid. */ 1506/* ------------------------------------------------------------------------ */ 1507static INLINE int 1508ipf_pr_udpcommon(fin) 1509 fr_info_t *fin; 1510{ 1511 udphdr_t *udp; 1512 1513 fin->fin_flx |= FI_TCPUDP; 1514 1515 if (!fin->fin_off && (fin->fin_dlen > 3)) { 1516 if (ipf_pr_pullup(fin, sizeof(*udp)) == -1) { 1517 ipf_main_softc_t *softc = fin->fin_main_soft; 1518 1519 fin->fin_flx |= FI_SHORT; 1520 LBUMPD(ipf_stats[fin->fin_out], fr_udp_pullup); 1521 return 1; 1522 } 1523 1524 udp = fin->fin_dp; 1525 1526 fin->fin_sport = ntohs(udp->uh_sport); 1527 fin->fin_dport = ntohs(udp->uh_dport); 1528 } 1529 1530 return 0; 1531} 1532 1533 1534/* ------------------------------------------------------------------------ */ 1535/* Function: ipf_pr_tcp */ 1536/* Returns: void */ 1537/* Parameters: fin(I) - pointer to packet information */ 1538/* */ 1539/* IPv4 Only */ 1540/* Analyse the packet for IPv4/TCP properties. */ 1541/* ------------------------------------------------------------------------ */ 1542static INLINE void 1543ipf_pr_tcp(fin) 1544 fr_info_t *fin; 1545{ 1546 1547 ipf_pr_short(fin, sizeof(tcphdr_t)); 1548 1549 if (ipf_pr_tcpcommon(fin) == 0) 1550 ipf_checkv4sum(fin); 1551} 1552 1553 1554/* ------------------------------------------------------------------------ */ 1555/* Function: ipf_pr_udp */ 1556/* Returns: void */ 1557/* Parameters: fin(I) - pointer to packet information */ 1558/* */ 1559/* IPv4 Only */ 1560/* Analyse the packet for IPv4/UDP properties. */ 1561/* ------------------------------------------------------------------------ */ 1562static INLINE void 1563ipf_pr_udp(fin) 1564 fr_info_t *fin; 1565{ 1566 1567 ipf_pr_short(fin, sizeof(udphdr_t)); 1568 1569 if (ipf_pr_udpcommon(fin) == 0) 1570 ipf_checkv4sum(fin); 1571} 1572 1573 1574/* ------------------------------------------------------------------------ */ 1575/* Function: ipf_pr_esp */ 1576/* Returns: void */ 1577/* Parameters: fin(I) - pointer to packet information */ 1578/* */ 1579/* Analyse the packet for ESP properties. */ 1580/* The minimum length is taken to be the SPI (32bits) plus a tail (32bits) */ 1581/* even though the newer ESP packets must also have a sequence number that */ 1582/* is 32bits as well, it is not possible(?) to determine the version from a */ 1583/* simple packet header. */ 1584/* ------------------------------------------------------------------------ */ 1585static INLINE void 1586ipf_pr_esp(fin) 1587 fr_info_t *fin; 1588{ 1589 1590 if (fin->fin_off == 0) { 1591 ipf_pr_short(fin, 8); 1592 if (ipf_pr_pullup(fin, 8) == -1) { 1593 ipf_main_softc_t *softc = fin->fin_main_soft; 1594 1595 LBUMPD(ipf_stats[fin->fin_out], fr_v4_esp_pullup); 1596 } 1597 } 1598} 1599 1600 1601/* ------------------------------------------------------------------------ */ 1602/* Function: ipf_pr_ah */ 1603/* Returns: int - value of the next header or IPPROTO_NONE if error */ 1604/* Parameters: fin(I) - pointer to packet information */ 1605/* */ 1606/* Analyse the packet for AH properties. */ 1607/* The minimum length is taken to be the combination of all fields in the */ 1608/* header being present and no authentication data (null algorithm used.) */ 1609/* ------------------------------------------------------------------------ */ 1610static INLINE int 1611ipf_pr_ah(fin) 1612 fr_info_t *fin; 1613{ 1614 ipf_main_softc_t *softc = fin->fin_main_soft; 1615 authhdr_t *ah; 1616 int len; 1617 1618 fin->fin_flx |= FI_AH; 1619 ipf_pr_short(fin, sizeof(*ah)); 1620 1621 if (((fin->fin_flx & FI_SHORT) != 0) || (fin->fin_off != 0)) { 1622 LBUMPD(ipf_stats[fin->fin_out], fr_v4_ah_bad); 1623 return IPPROTO_NONE; 1624 } 1625 1626 if (ipf_pr_pullup(fin, sizeof(*ah)) == -1) { 1627 DT(fr_v4_ah_pullup_1); 1628 LBUMP(ipf_stats[fin->fin_out].fr_v4_ah_pullup); 1629 return IPPROTO_NONE; 1630 } 1631 1632 ah = (authhdr_t *)fin->fin_dp; 1633 1634 len = (ah->ah_plen + 2) << 2; 1635 ipf_pr_short(fin, len); 1636 if (ipf_pr_pullup(fin, len) == -1) { 1637 DT(fr_v4_ah_pullup_2); 1638 LBUMP(ipf_stats[fin->fin_out].fr_v4_ah_pullup); 1639 return IPPROTO_NONE; 1640 } 1641 1642 /* 1643 * Adjust fin_dp and fin_dlen for skipping over the authentication 1644 * header. 1645 */ 1646 fin->fin_dp = (char *)fin->fin_dp + len; 1647 fin->fin_dlen -= len; 1648 return ah->ah_next; 1649} 1650 1651 1652/* ------------------------------------------------------------------------ */ 1653/* Function: ipf_pr_gre */ 1654/* Returns: void */ 1655/* Parameters: fin(I) - pointer to packet information */ 1656/* */ 1657/* Analyse the packet for GRE properties. */ 1658/* ------------------------------------------------------------------------ */ 1659static INLINE void 1660ipf_pr_gre(fin) 1661 fr_info_t *fin; 1662{ 1663 ipf_main_softc_t *softc = fin->fin_main_soft; 1664 grehdr_t *gre; 1665 1666 ipf_pr_short(fin, sizeof(grehdr_t)); 1667 1668 if (fin->fin_off != 0) { 1669 LBUMPD(ipf_stats[fin->fin_out], fr_v4_gre_frag); 1670 return; 1671 } 1672 1673 if (ipf_pr_pullup(fin, sizeof(grehdr_t)) == -1) { 1674 LBUMPD(ipf_stats[fin->fin_out], fr_v4_gre_pullup); 1675 return; 1676 } 1677 1678 gre = fin->fin_dp; 1679 if (GRE_REV(gre->gr_flags) == 1) 1680 fin->fin_data[0] = gre->gr_call; 1681} 1682 1683 1684/* ------------------------------------------------------------------------ */ 1685/* Function: ipf_pr_ipv4hdr */ 1686/* Returns: void */ 1687/* Parameters: fin(I) - pointer to packet information */ 1688/* */ 1689/* IPv4 Only */ 1690/* Analyze the IPv4 header and set fields in the fr_info_t structure. */ 1691/* Check all options present and flag their presence if any exist. */ 1692/* ------------------------------------------------------------------------ */ 1693static INLINE void 1694ipf_pr_ipv4hdr(fin) 1695 fr_info_t *fin; 1696{ 1697 u_short optmsk = 0, secmsk = 0, auth = 0; 1698 int hlen, ol, mv, p, i; 1699 const struct optlist *op; 1700 u_char *s, opt; 1701 u_short off; 1702 fr_ip_t *fi; 1703 ip_t *ip; 1704 1705 fi = &fin->fin_fi; 1706 hlen = fin->fin_hlen; 1707 1708 ip = fin->fin_ip; 1709 p = ip->ip_p; 1710 fi->fi_p = p; 1711 fin->fin_crc = p; 1712 fi->fi_tos = ip->ip_tos; 1713 fin->fin_id = ip->ip_id; 1714 off = ntohs(ip->ip_off); 1715 1716 /* Get both TTL and protocol */ 1717 fi->fi_p = ip->ip_p; 1718 fi->fi_ttl = ip->ip_ttl; 1719 1720 /* Zero out bits not used in IPv6 address */ 1721 fi->fi_src.i6[1] = 0; 1722 fi->fi_src.i6[2] = 0; 1723 fi->fi_src.i6[3] = 0; 1724 fi->fi_dst.i6[1] = 0; 1725 fi->fi_dst.i6[2] = 0; 1726 fi->fi_dst.i6[3] = 0; 1727 1728 fi->fi_saddr = ip->ip_src.s_addr; 1729 fin->fin_crc += fi->fi_saddr; 1730 fi->fi_daddr = ip->ip_dst.s_addr; 1731 fin->fin_crc += fi->fi_daddr; 1732 if (IN_CLASSD(ntohl(fi->fi_daddr))) 1733 fin->fin_flx |= FI_MULTICAST|FI_MBCAST; 1734 1735 /* 1736 * set packet attribute flags based on the offset and 1737 * calculate the byte offset that it represents. 1738 */ 1739 off &= IP_MF|IP_OFFMASK; 1740 if (off != 0) { 1741 int morefrag = off & IP_MF; 1742 1743 fi->fi_flx |= FI_FRAG; 1744 off &= IP_OFFMASK; 1745 if (off != 0) { 1746 fin->fin_flx |= FI_FRAGBODY; 1747 off <<= 3; 1748 if ((off + fin->fin_dlen > 65535) || 1749 (fin->fin_dlen == 0) || 1750 ((morefrag != 0) && ((fin->fin_dlen & 7) != 0))) { 1751 /* 1752 * The length of the packet, starting at its 1753 * offset cannot exceed 65535 (0xffff) as the 1754 * length of an IP packet is only 16 bits. 1755 * 1756 * Any fragment that isn't the last fragment 1757 * must have a length greater than 0 and it 1758 * must be an even multiple of 8. 1759 */ 1760 fi->fi_flx |= FI_BAD; 1761 } 1762 } 1763 } 1764 fin->fin_off = off; 1765 1766 /* 1767 * Call per-protocol setup and checking 1768 */ 1769 if (p == IPPROTO_AH) { 1770 /* 1771 * Treat AH differently because we expect there to be another 1772 * layer 4 header after it. 1773 */ 1774 p = ipf_pr_ah(fin); 1775 } 1776 1777 switch (p) 1778 { 1779 case IPPROTO_UDP : 1780 ipf_pr_udp(fin); 1781 break; 1782 case IPPROTO_TCP : 1783 ipf_pr_tcp(fin); 1784 break; 1785 case IPPROTO_ICMP : 1786 ipf_pr_icmp(fin); 1787 break; 1788 case IPPROTO_ESP : 1789 ipf_pr_esp(fin); 1790 break; 1791 case IPPROTO_GRE : 1792 ipf_pr_gre(fin); 1793 break; 1794 } 1795 1796 ip = fin->fin_ip; 1797 if (ip == NULL) 1798 return; 1799 1800 /* 1801 * If it is a standard IP header (no options), set the flag fields 1802 * which relate to options to 0. 1803 */ 1804 if (hlen == sizeof(*ip)) { 1805 fi->fi_optmsk = 0; 1806 fi->fi_secmsk = 0; 1807 fi->fi_auth = 0; 1808 return; 1809 } 1810 1811 /* 1812 * So the IP header has some IP options attached. Walk the entire 1813 * list of options present with this packet and set flags to indicate 1814 * which ones are here and which ones are not. For the somewhat out 1815 * of date and obscure security classification options, set a flag to 1816 * represent which classification is present. 1817 */ 1818 fi->fi_flx |= FI_OPTIONS; 1819 1820 for (s = (u_char *)(ip + 1), hlen -= (int)sizeof(*ip); hlen > 0; ) { 1821 opt = *s; 1822 if (opt == '\0') 1823 break; 1824 else if (opt == IPOPT_NOP) 1825 ol = 1; 1826 else { 1827 if (hlen < 2) 1828 break; 1829 ol = (int)*(s + 1); 1830 if (ol < 2 || ol > hlen) 1831 break; 1832 } 1833 for (i = 9, mv = 4; mv >= 0; ) { 1834 op = ipopts + i; 1835 1836 if ((opt == (u_char)op->ol_val) && (ol > 4)) { 1837 u_32_t doi; 1838 1839 switch (opt) 1840 { 1841 case IPOPT_SECURITY : 1842 if (optmsk & op->ol_bit) { 1843 fin->fin_flx |= FI_BAD; 1844 } else { 1845 doi = ipf_checkripso(s); 1846 secmsk = doi >> 16; 1847 auth = doi & 0xffff; 1848 } 1849 break; 1850 1851 case IPOPT_CIPSO : 1852 1853 if (optmsk & op->ol_bit) { 1854 fin->fin_flx |= FI_BAD; 1855 } else { 1856 doi = ipf_checkcipso(fin, 1857 s, ol); 1858 secmsk = doi >> 16; 1859 auth = doi & 0xffff; 1860 } 1861 break; 1862 } 1863 optmsk |= op->ol_bit; 1864 } 1865 1866 if (opt < op->ol_val) 1867 i -= mv; 1868 else 1869 i += mv; 1870 mv--; 1871 } 1872 hlen -= ol; 1873 s += ol; 1874 } 1875 1876 /* 1877 * 1878 */ 1879 if (auth && !(auth & 0x0100)) 1880 auth &= 0xff00; 1881 fi->fi_optmsk = optmsk; 1882 fi->fi_secmsk = secmsk; 1883 fi->fi_auth = auth; 1884} 1885 1886 1887/* ------------------------------------------------------------------------ */ 1888/* Function: ipf_checkripso */ 1889/* Returns: void */ 1890/* Parameters: s(I) - pointer to start of RIPSO option */ 1891/* */ 1892/* ------------------------------------------------------------------------ */ 1893static u_32_t 1894ipf_checkripso(s) 1895 u_char *s; 1896{ 1897 const struct optlist *sp; 1898 u_short secmsk = 0, auth = 0; 1899 u_char sec; 1900 int j, m; 1901 1902 sec = *(s + 2); /* classification */ 1903 for (j = 3, m = 2; m >= 0; ) { 1904 sp = secopt + j; 1905 if (sec == sp->ol_val) { 1906 secmsk |= sp->ol_bit; 1907 auth = *(s + 3); 1908 auth *= 256; 1909 auth += *(s + 4); 1910 break; 1911 } 1912 if (sec < sp->ol_val) 1913 j -= m; 1914 else 1915 j += m; 1916 m--; 1917 } 1918 1919 return (secmsk << 16) | auth; 1920} 1921 1922 1923/* ------------------------------------------------------------------------ */ 1924/* Function: ipf_checkcipso */ 1925/* Returns: u_32_t - 0 = failure, else the doi from the header */ 1926/* Parameters: fin(IO) - pointer to packet information */ 1927/* s(I) - pointer to start of CIPSO option */ 1928/* ol(I) - length of CIPSO option field */ 1929/* */ 1930/* This function returns the domain of integrity (DOI) field from the CIPSO */ 1931/* header and returns that whilst also storing the highest sensitivity */ 1932/* value found in the fr_info_t structure. */ 1933/* */ 1934/* No attempt is made to extract the category bitmaps as these are defined */ 1935/* by the user (rather than the protocol) and can be rather numerous on the */ 1936/* end nodes. */ 1937/* ------------------------------------------------------------------------ */ 1938static u_32_t 1939ipf_checkcipso(fin, s, ol) 1940 fr_info_t *fin; 1941 u_char *s; 1942 int ol; 1943{ 1944 ipf_main_softc_t *softc = fin->fin_main_soft; 1945 fr_ip_t *fi; 1946 u_32_t doi; 1947 u_char *t, tag, tlen, sensitivity; 1948 int len; 1949 1950 if (ol < 6 || ol > 40) { 1951 LBUMPD(ipf_stats[fin->fin_out], fr_v4_cipso_bad); 1952 fin->fin_flx |= FI_BAD; 1953 return 0; 1954 } 1955 1956 fi = &fin->fin_fi; 1957 fi->fi_sensitivity = 0; 1958 /* 1959 * The DOI field MUST be there. 1960 */ 1961 bcopy(s + 2, &doi, sizeof(doi)); 1962 1963 t = (u_char *)s + 6; 1964 for (len = ol - 6; len >= 2; len -= tlen, t+= tlen) { 1965 tag = *t; 1966 tlen = *(t + 1); 1967 if (tlen > len || tlen < 4 || tlen > 34) { 1968 LBUMPD(ipf_stats[fin->fin_out], fr_v4_cipso_tlen); 1969 fin->fin_flx |= FI_BAD; 1970 return 0; 1971 } 1972 1973 sensitivity = 0; 1974 /* 1975 * Tag numbers 0, 1, 2, 5 are laid out in the CIPSO Internet 1976 * draft (16 July 1992) that has expired. 1977 */ 1978 if (tag == 0) { 1979 fin->fin_flx |= FI_BAD; 1980 continue; 1981 } else if (tag == 1) { 1982 if (*(t + 2) != 0) { 1983 fin->fin_flx |= FI_BAD; 1984 continue; 1985 } 1986 sensitivity = *(t + 3); 1987 /* Category bitmap for categories 0-239 */ 1988 1989 } else if (tag == 4) { 1990 if (*(t + 2) != 0) { 1991 fin->fin_flx |= FI_BAD; 1992 continue; 1993 } 1994 sensitivity = *(t + 3); 1995 /* Enumerated categories, 16bits each, upto 15 */ 1996 1997 } else if (tag == 5) { 1998 if (*(t + 2) != 0) { 1999 fin->fin_flx |= FI_BAD; 2000 continue; 2001 } 2002 sensitivity = *(t + 3); 2003 /* Range of categories (2*16bits), up to 7 pairs */ 2004 2005 } else if (tag > 127) { 2006 /* Custom defined DOI */ 2007 ; 2008 } else { 2009 fin->fin_flx |= FI_BAD; 2010 continue; 2011 } 2012 2013 if (sensitivity > fi->fi_sensitivity) 2014 fi->fi_sensitivity = sensitivity; 2015 } 2016 2017 return doi; 2018} 2019 2020 2021/* ------------------------------------------------------------------------ */ 2022/* Function: ipf_makefrip */ 2023/* Returns: int - 0 == packet ok, -1 == packet freed */ 2024/* Parameters: hlen(I) - length of IP packet header */ 2025/* ip(I) - pointer to the IP header */ 2026/* fin(IO) - pointer to packet information */ 2027/* */ 2028/* Compact the IP header into a structure which contains just the info. */ 2029/* which is useful for comparing IP headers with and store this information */ 2030/* in the fr_info_t structure pointer to by fin. At present, it is assumed */ 2031/* this function will be called with either an IPv4 or IPv6 packet. */ 2032/* ------------------------------------------------------------------------ */ 2033int 2034ipf_makefrip(hlen, ip, fin) 2035 int hlen; 2036 ip_t *ip; 2037 fr_info_t *fin; 2038{ 2039 ipf_main_softc_t *softc = fin->fin_main_soft; 2040 int v; 2041 2042 fin->fin_depth = 0; 2043 fin->fin_hlen = (u_short)hlen; 2044 fin->fin_ip = ip; 2045 fin->fin_rule = 0xffffffff; 2046 fin->fin_group[0] = -1; 2047 fin->fin_group[1] = '\0'; 2048 fin->fin_dp = (char *)ip + hlen; 2049 2050 v = fin->fin_v; 2051 if (v == 4) { 2052 fin->fin_plen = ntohs(ip->ip_len); 2053 fin->fin_dlen = fin->fin_plen - hlen; 2054 ipf_pr_ipv4hdr(fin); 2055#ifdef USE_INET6 2056 } else if (v == 6) { 2057 fin->fin_plen = ntohs(((ip6_t *)ip)->ip6_plen); 2058 fin->fin_dlen = fin->fin_plen; 2059 fin->fin_plen += hlen; 2060 2061 ipf_pr_ipv6hdr(fin); 2062#endif 2063 } 2064 if (fin->fin_ip == NULL) { 2065 LBUMP(ipf_stats[fin->fin_out].fr_ip_freed); 2066 return -1; 2067 } 2068 return 0; 2069} 2070 2071 2072/* ------------------------------------------------------------------------ */ 2073/* Function: ipf_portcheck */ 2074/* Returns: int - 1 == port matched, 0 == port match failed */ 2075/* Parameters: frp(I) - pointer to port check `expression' */ 2076/* pop(I) - port number to evaluate */ 2077/* */ 2078/* Perform a comparison of a port number against some other(s), using a */ 2079/* structure with compare information stored in it. */ 2080/* ------------------------------------------------------------------------ */ 2081static INLINE int 2082ipf_portcheck(frp, pop) 2083 frpcmp_t *frp; 2084 u_32_t pop; 2085{ 2086 int err = 1; 2087 u_32_t po; 2088 2089 po = frp->frp_port; 2090 2091 /* 2092 * Do opposite test to that required and continue if that succeeds. 2093 */ 2094 switch (frp->frp_cmp) 2095 { 2096 case FR_EQUAL : 2097 if (pop != po) /* EQUAL */ 2098 err = 0; 2099 break; 2100 case FR_NEQUAL : 2101 if (pop == po) /* NOTEQUAL */ 2102 err = 0; 2103 break; 2104 case FR_LESST : 2105 if (pop >= po) /* LESSTHAN */ 2106 err = 0; 2107 break; 2108 case FR_GREATERT : 2109 if (pop <= po) /* GREATERTHAN */ 2110 err = 0; 2111 break; 2112 case FR_LESSTE : 2113 if (pop > po) /* LT or EQ */ 2114 err = 0; 2115 break; 2116 case FR_GREATERTE : 2117 if (pop < po) /* GT or EQ */ 2118 err = 0; 2119 break; 2120 case FR_OUTRANGE : 2121 if (pop >= po && pop <= frp->frp_top) /* Out of range */ 2122 err = 0; 2123 break; 2124 case FR_INRANGE : 2125 if (pop <= po || pop >= frp->frp_top) /* In range */ 2126 err = 0; 2127 break; 2128 case FR_INCRANGE : 2129 if (pop < po || pop > frp->frp_top) /* Inclusive range */ 2130 err = 0; 2131 break; 2132 default : 2133 break; 2134 } 2135 return err; 2136} 2137 2138 2139/* ------------------------------------------------------------------------ */ 2140/* Function: ipf_tcpudpchk */ 2141/* Returns: int - 1 == protocol matched, 0 == check failed */ 2142/* Parameters: fda(I) - pointer to packet information */ 2143/* ft(I) - pointer to structure with comparison data */ 2144/* */ 2145/* Compares the current pcket (assuming it is TCP/UDP) information with a */ 2146/* structure containing information that we want to match against. */ 2147/* ------------------------------------------------------------------------ */ 2148int 2149ipf_tcpudpchk(fi, ft) 2150 fr_ip_t *fi; 2151 frtuc_t *ft; 2152{ 2153 int err = 1; 2154 2155 /* 2156 * Both ports should *always* be in the first fragment. 2157 * So far, I cannot find any cases where they can not be. 2158 * 2159 * compare destination ports 2160 */ 2161 if (ft->ftu_dcmp) 2162 err = ipf_portcheck(&ft->ftu_dst, fi->fi_ports[1]); 2163 2164 /* 2165 * compare source ports 2166 */ 2167 if (err && ft->ftu_scmp) 2168 err = ipf_portcheck(&ft->ftu_src, fi->fi_ports[0]); 2169 2170 /* 2171 * If we don't have all the TCP/UDP header, then how can we 2172 * expect to do any sort of match on it ? If we were looking for 2173 * TCP flags, then NO match. If not, then match (which should 2174 * satisfy the "short" class too). 2175 */ 2176 if (err && (fi->fi_p == IPPROTO_TCP)) { 2177 if (fi->fi_flx & FI_SHORT) 2178 return !(ft->ftu_tcpf | ft->ftu_tcpfm); 2179 /* 2180 * Match the flags ? If not, abort this match. 2181 */ 2182 if (ft->ftu_tcpfm && 2183 ft->ftu_tcpf != (fi->fi_tcpf & ft->ftu_tcpfm)) { 2184 FR_DEBUG(("f. %#x & %#x != %#x\n", fi->fi_tcpf, 2185 ft->ftu_tcpfm, ft->ftu_tcpf)); 2186 err = 0; 2187 } 2188 } 2189 return err; 2190} 2191 2192 2193/* ------------------------------------------------------------------------ */ 2194/* Function: ipf_check_ipf */ 2195/* Returns: int - 0 == match, else no match */ 2196/* Parameters: fin(I) - pointer to packet information */ 2197/* fr(I) - pointer to filter rule */ 2198/* portcmp(I) - flag indicating whether to attempt matching on */ 2199/* TCP/UDP port data. */ 2200/* */ 2201/* Check to see if a packet matches an IPFilter rule. Checks of addresses, */ 2202/* port numbers, etc, for "standard" IPFilter rules are all orchestrated in */ 2203/* this function. */ 2204/* ------------------------------------------------------------------------ */ 2205static INLINE int 2206ipf_check_ipf(fin, fr, portcmp) 2207 fr_info_t *fin; 2208 frentry_t *fr; 2209 int portcmp; 2210{ 2211 u_32_t *ld, *lm, *lip; 2212 fripf_t *fri; 2213 fr_ip_t *fi; 2214 int i; 2215 2216 fi = &fin->fin_fi; 2217 fri = fr->fr_ipf; 2218 lip = (u_32_t *)fi; 2219 lm = (u_32_t *)&fri->fri_mip; 2220 ld = (u_32_t *)&fri->fri_ip; 2221 2222 /* 2223 * first 32 bits to check coversion: 2224 * IP version, TOS, TTL, protocol 2225 */ 2226 i = ((*lip & *lm) != *ld); 2227 FR_DEBUG(("0. %#08x & %#08x != %#08x\n", 2228 ntohl(*lip), ntohl(*lm), ntohl(*ld))); 2229 if (i) 2230 return 1; 2231 2232 /* 2233 * Next 32 bits is a constructed bitmask indicating which IP options 2234 * are present (if any) in this packet. 2235 */ 2236 lip++, lm++, ld++; 2237 i = ((*lip & *lm) != *ld); 2238 FR_DEBUG(("1. %#08x & %#08x != %#08x\n", 2239 ntohl(*lip), ntohl(*lm), ntohl(*ld))); 2240 if (i != 0) 2241 return 1; 2242 2243 lip++, lm++, ld++; 2244 /* 2245 * Unrolled loops (4 each, for 32 bits) for address checks. 2246 */ 2247 /* 2248 * Check the source address. 2249 */ 2250 if (fr->fr_satype == FRI_LOOKUP) { 2251 i = (*fr->fr_srcfunc)(fin->fin_main_soft, fr->fr_srcptr, 2252 fi->fi_v, lip, fin->fin_plen); 2253 if (i == -1) 2254 return 1; 2255 lip += 3; 2256 lm += 3; 2257 ld += 3; 2258 } else { 2259 i = ((*lip & *lm) != *ld); 2260 FR_DEBUG(("2a. %#08x & %#08x != %#08x\n", 2261 ntohl(*lip), ntohl(*lm), ntohl(*ld))); 2262 if (fi->fi_v == 6) { 2263 lip++, lm++, ld++; 2264 i |= ((*lip & *lm) != *ld); 2265 FR_DEBUG(("2b. %#08x & %#08x != %#08x\n", 2266 ntohl(*lip), ntohl(*lm), ntohl(*ld))); 2267 lip++, lm++, ld++; 2268 i |= ((*lip & *lm) != *ld); 2269 FR_DEBUG(("2c. %#08x & %#08x != %#08x\n", 2270 ntohl(*lip), ntohl(*lm), ntohl(*ld))); 2271 lip++, lm++, ld++; 2272 i |= ((*lip & *lm) != *ld); 2273 FR_DEBUG(("2d. %#08x & %#08x != %#08x\n", 2274 ntohl(*lip), ntohl(*lm), ntohl(*ld))); 2275 } else { 2276 lip += 3; 2277 lm += 3; 2278 ld += 3; 2279 } 2280 } 2281 i ^= (fr->fr_flags & FR_NOTSRCIP) >> 6; 2282 if (i != 0) 2283 return 1; 2284 2285 /* 2286 * Check the destination address. 2287 */ 2288 lip++, lm++, ld++; 2289 if (fr->fr_datype == FRI_LOOKUP) { 2290 i = (*fr->fr_dstfunc)(fin->fin_main_soft, fr->fr_dstptr, 2291 fi->fi_v, lip, fin->fin_plen); 2292 if (i == -1) 2293 return 1; 2294 lip += 3; 2295 lm += 3; 2296 ld += 3; 2297 } else { 2298 i = ((*lip & *lm) != *ld); 2299 FR_DEBUG(("3a. %#08x & %#08x != %#08x\n", 2300 ntohl(*lip), ntohl(*lm), ntohl(*ld))); 2301 if (fi->fi_v == 6) { 2302 lip++, lm++, ld++; 2303 i |= ((*lip & *lm) != *ld); 2304 FR_DEBUG(("3b. %#08x & %#08x != %#08x\n", 2305 ntohl(*lip), ntohl(*lm), ntohl(*ld))); 2306 lip++, lm++, ld++; 2307 i |= ((*lip & *lm) != *ld); 2308 FR_DEBUG(("3c. %#08x & %#08x != %#08x\n", 2309 ntohl(*lip), ntohl(*lm), ntohl(*ld))); 2310 lip++, lm++, ld++; 2311 i |= ((*lip & *lm) != *ld); 2312 FR_DEBUG(("3d. %#08x & %#08x != %#08x\n", 2313 ntohl(*lip), ntohl(*lm), ntohl(*ld))); 2314 } else { 2315 lip += 3; 2316 lm += 3; 2317 ld += 3; 2318 } 2319 } 2320 i ^= (fr->fr_flags & FR_NOTDSTIP) >> 7; 2321 if (i != 0) 2322 return 1; 2323 /* 2324 * IP addresses matched. The next 32bits contains: 2325 * mast of old IP header security & authentication bits. 2326 */ 2327 lip++, lm++, ld++; 2328 i = (*ld - (*lip & *lm)); 2329 FR_DEBUG(("4. %#08x & %#08x != %#08x\n", *lip, *lm, *ld)); 2330 2331 /* 2332 * Next we have 32 bits of packet flags. 2333 */ 2334 lip++, lm++, ld++; 2335 i |= (*ld - (*lip & *lm)); 2336 FR_DEBUG(("5. %#08x & %#08x != %#08x\n", *lip, *lm, *ld)); 2337 2338 if (i == 0) { 2339 /* 2340 * If a fragment, then only the first has what we're 2341 * looking for here... 2342 */ 2343 if (portcmp) { 2344 if (!ipf_tcpudpchk(&fin->fin_fi, &fr->fr_tuc)) 2345 i = 1; 2346 } else { 2347 if (fr->fr_dcmp || fr->fr_scmp || 2348 fr->fr_tcpf || fr->fr_tcpfm) 2349 i = 1; 2350 if (fr->fr_icmpm || fr->fr_icmp) { 2351 if (((fi->fi_p != IPPROTO_ICMP) && 2352 (fi->fi_p != IPPROTO_ICMPV6)) || 2353 fin->fin_off || (fin->fin_dlen < 2)) 2354 i = 1; 2355 else if ((fin->fin_data[0] & fr->fr_icmpm) != 2356 fr->fr_icmp) { 2357 FR_DEBUG(("i. %#x & %#x != %#x\n", 2358 fin->fin_data[0], 2359 fr->fr_icmpm, fr->fr_icmp)); 2360 i = 1; 2361 } 2362 } 2363 } 2364 } 2365 return i; 2366} 2367 2368 2369/* ------------------------------------------------------------------------ */ 2370/* Function: ipf_scanlist */ 2371/* Returns: int - result flags of scanning filter list */ 2372/* Parameters: fin(I) - pointer to packet information */ 2373/* pass(I) - default result to return for filtering */ 2374/* */ 2375/* Check the input/output list of rules for a match to the current packet. */ 2376/* If a match is found, the value of fr_flags from the rule becomes the */ 2377/* return value and fin->fin_fr points to the matched rule. */ 2378/* */ 2379/* This function may be called recusively upto 16 times (limit inbuilt.) */ 2380/* When unwinding, it should finish up with fin_depth as 0. */ 2381/* */ 2382/* Could be per interface, but this gets real nasty when you don't have, */ 2383/* or can't easily change, the kernel source code to . */ 2384/* ------------------------------------------------------------------------ */ 2385int 2386ipf_scanlist(fin, pass) 2387 fr_info_t *fin; 2388 u_32_t pass; 2389{ 2390 ipf_main_softc_t *softc = fin->fin_main_soft; 2391 int rulen, portcmp, off, skip; 2392 struct frentry *fr, *fnext; 2393 u_32_t passt, passo; 2394 2395 /* 2396 * Do not allow nesting deeper than 16 levels. 2397 */ 2398 if (fin->fin_depth >= 16) 2399 return pass; 2400 2401 fr = fin->fin_fr; 2402 2403 /* 2404 * If there are no rules in this list, return now. 2405 */ 2406 if (fr == NULL) 2407 return pass; 2408 2409 skip = 0; 2410 portcmp = 0; 2411 fin->fin_depth++; 2412 fin->fin_fr = NULL; 2413 off = fin->fin_off; 2414 2415 if ((fin->fin_flx & FI_TCPUDP) && (fin->fin_dlen > 3) && !off) 2416 portcmp = 1; 2417 2418 for (rulen = 0; fr; fr = fnext, rulen++) { 2419 fnext = fr->fr_next; 2420 if (skip != 0) { 2421 FR_VERBOSE(("SKIP %d (%#x)\n", skip, fr->fr_flags)); 2422 skip--; 2423 continue; 2424 } 2425 2426 /* 2427 * In all checks below, a null (zero) value in the 2428 * filter struture is taken to mean a wildcard. 2429 * 2430 * check that we are working for the right interface 2431 */ 2432#ifdef _KERNEL 2433 if (fr->fr_ifa && fr->fr_ifa != fin->fin_ifp) 2434 continue; 2435#else 2436 if (opts & (OPT_VERBOSE|OPT_DEBUG)) 2437 printf("\n"); 2438 FR_VERBOSE(("%c", FR_ISSKIP(pass) ? 's' : 2439 FR_ISPASS(pass) ? 'p' : 2440 FR_ISACCOUNT(pass) ? 'A' : 2441 FR_ISAUTH(pass) ? 'a' : 2442 (pass & FR_NOMATCH) ? 'n' :'b')); 2443 if (fr->fr_ifa && fr->fr_ifa != fin->fin_ifp) 2444 continue; 2445 FR_VERBOSE((":i")); 2446#endif 2447 2448 switch (fr->fr_type) 2449 { 2450 case FR_T_IPF : 2451 case FR_T_IPF_BUILTIN : 2452 if (ipf_check_ipf(fin, fr, portcmp)) 2453 continue; 2454 break; 2455#if defined(IPFILTER_BPF) 2456 case FR_T_BPFOPC : 2457 case FR_T_BPFOPC_BUILTIN : 2458 { 2459 u_char *mc; 2460 int wlen; 2461 2462 if (*fin->fin_mp == NULL) 2463 continue; 2464 if (fin->fin_family != fr->fr_family) 2465 continue; 2466 mc = (u_char *)fin->fin_m; 2467 wlen = fin->fin_dlen + fin->fin_hlen; 2468 if (!bpf_filter(fr->fr_data, mc, wlen, 0)) 2469 continue; 2470 break; 2471 } 2472#endif 2473 case FR_T_CALLFUNC_BUILTIN : 2474 { 2475 frentry_t *f; 2476 2477 f = (*fr->fr_func)(fin, &pass); 2478 if (f != NULL) 2479 fr = f; 2480 else 2481 continue; 2482 break; 2483 } 2484 2485 case FR_T_IPFEXPR : 2486 case FR_T_IPFEXPR_BUILTIN : 2487 if (fin->fin_family != fr->fr_family) 2488 continue; 2489 if (ipf_fr_matcharray(fin, fr->fr_data) == 0) 2490 continue; 2491 break; 2492 2493 default : 2494 break; 2495 } 2496 2497 if ((fin->fin_out == 0) && (fr->fr_nattag.ipt_num[0] != 0)) { 2498 if (fin->fin_nattag == NULL) 2499 continue; 2500 if (ipf_matchtag(&fr->fr_nattag, fin->fin_nattag) == 0) 2501 continue; 2502 } 2503 FR_VERBOSE(("=%d/%d.%d *", fr->fr_grhead, fr->fr_group, rulen)); 2504 2505 passt = fr->fr_flags; 2506 2507 /* 2508 * If the rule is a "call now" rule, then call the function 2509 * in the rule, if it exists and use the results from that. 2510 * If the function pointer is bad, just make like we ignore 2511 * it, except for increasing the hit counter. 2512 */ 2513 if ((passt & FR_CALLNOW) != 0) { 2514 frentry_t *frs; 2515 2516 ATOMIC_INC64(fr->fr_hits); 2517 if ((fr->fr_func == NULL) || 2518 (fr->fr_func == (ipfunc_t)-1)) 2519 continue; 2520 2521 frs = fin->fin_fr; 2522 fin->fin_fr = fr; 2523 fr = (*fr->fr_func)(fin, &passt); 2524 if (fr == NULL) { 2525 fin->fin_fr = frs; 2526 continue; 2527 } 2528 passt = fr->fr_flags; 2529 } 2530 fin->fin_fr = fr; 2531 2532#ifdef IPFILTER_LOG 2533 /* 2534 * Just log this packet... 2535 */ 2536 if ((passt & FR_LOGMASK) == FR_LOG) { 2537 if (ipf_log_pkt(fin, passt) == -1) { 2538 if (passt & FR_LOGORBLOCK) { 2539 DT(frb_logfail); 2540 passt &= ~FR_CMDMASK; 2541 passt |= FR_BLOCK|FR_QUICK; 2542 fin->fin_reason = FRB_LOGFAIL; 2543 } 2544 } 2545 } 2546#endif /* IPFILTER_LOG */ 2547 2548 MUTEX_ENTER(&fr->fr_lock); 2549 fr->fr_bytes += (U_QUAD_T)fin->fin_plen; 2550 fr->fr_hits++; 2551 MUTEX_EXIT(&fr->fr_lock); 2552 fin->fin_rule = rulen; 2553 2554 passo = pass; 2555 if (FR_ISSKIP(passt)) { 2556 skip = fr->fr_arg; 2557 continue; 2558 } else if (((passt & FR_LOGMASK) != FR_LOG) && 2559 ((passt & FR_LOGMASK) != FR_DECAPSULATE)) { 2560 pass = passt; 2561 } 2562 2563 if (passt & (FR_RETICMP|FR_FAKEICMP)) 2564 fin->fin_icode = fr->fr_icode; 2565 2566 if (fr->fr_group != -1) { 2567 (void) strncpy(fin->fin_group, 2568 FR_NAME(fr, fr_group), 2569 strlen(FR_NAME(fr, fr_group))); 2570 } else { 2571 fin->fin_group[0] = '\0'; 2572 } 2573 2574 FR_DEBUG(("pass %#x/%#x/%x\n", passo, pass, passt)); 2575 2576 if (fr->fr_grphead != NULL) { 2577 fin->fin_fr = fr->fr_grphead->fg_start; 2578 FR_VERBOSE(("group %s\n", FR_NAME(fr, fr_grhead))); 2579 2580 if (FR_ISDECAPS(passt)) 2581 passt = ipf_decaps(fin, pass, fr->fr_icode); 2582 else 2583 passt = ipf_scanlist(fin, pass); 2584 2585 if (fin->fin_fr == NULL) { 2586 fin->fin_rule = rulen; 2587 if (fr->fr_group != -1) 2588 (void) strncpy(fin->fin_group, 2589 fr->fr_names + 2590 fr->fr_group, 2591 strlen(fr->fr_names + 2592 fr->fr_group)); 2593 fin->fin_fr = fr; 2594 passt = pass; 2595 } 2596 pass = passt; 2597 } 2598 2599 if (pass & FR_QUICK) { 2600 /* 2601 * Finally, if we've asked to track state for this 2602 * packet, set it up. Add state for "quick" rules 2603 * here so that if the action fails we can consider 2604 * the rule to "not match" and keep on processing 2605 * filter rules. 2606 */ 2607 if ((pass & FR_KEEPSTATE) && !FR_ISAUTH(pass) && 2608 !(fin->fin_flx & FI_STATE)) { 2609 int out = fin->fin_out; 2610 2611 fin->fin_fr = fr; 2612 if (ipf_state_add(softc, fin, NULL, 0) == 0) { 2613 LBUMPD(ipf_stats[out], fr_ads); 2614 } else { 2615 LBUMPD(ipf_stats[out], fr_bads); 2616 pass = passo; 2617 continue; 2618 } 2619 } 2620 break; 2621 } 2622 } 2623 fin->fin_depth--; 2624 return pass; 2625} 2626 2627 2628/* ------------------------------------------------------------------------ */ 2629/* Function: ipf_acctpkt */ 2630/* Returns: frentry_t* - always returns NULL */ 2631/* Parameters: fin(I) - pointer to packet information */ 2632/* passp(IO) - pointer to current/new filter decision (unused) */ 2633/* */ 2634/* Checks a packet against accounting rules, if there are any for the given */ 2635/* IP protocol version. */ 2636/* */ 2637/* N.B.: this function returns NULL to match the prototype used by other */ 2638/* functions called from the IPFilter "mainline" in ipf_check(). */ 2639/* ------------------------------------------------------------------------ */ 2640frentry_t * 2641ipf_acctpkt(fin, passp) 2642 fr_info_t *fin; 2643 u_32_t *passp; 2644{ 2645 ipf_main_softc_t *softc = fin->fin_main_soft; 2646 char group[FR_GROUPLEN]; 2647 frentry_t *fr, *frsave; 2648 u_32_t pass, rulen; 2649 2650 passp = passp; 2651 fr = softc->ipf_acct[fin->fin_out][softc->ipf_active]; 2652 2653 if (fr != NULL) { 2654 frsave = fin->fin_fr; 2655 bcopy(fin->fin_group, group, FR_GROUPLEN); 2656 rulen = fin->fin_rule; 2657 fin->fin_fr = fr; 2658 pass = ipf_scanlist(fin, FR_NOMATCH); 2659 if (FR_ISACCOUNT(pass)) { 2660 LBUMPD(ipf_stats[0], fr_acct); 2661 } 2662 fin->fin_fr = frsave; 2663 bcopy(group, fin->fin_group, FR_GROUPLEN); 2664 fin->fin_rule = rulen; 2665 } 2666 return NULL; 2667} 2668 2669 2670/* ------------------------------------------------------------------------ */ 2671/* Function: ipf_firewall */ 2672/* Returns: frentry_t* - returns pointer to matched rule, if no matches */ 2673/* were found, returns NULL. */ 2674/* Parameters: fin(I) - pointer to packet information */ 2675/* passp(IO) - pointer to current/new filter decision (unused) */ 2676/* */ 2677/* Applies an appropriate set of firewall rules to the packet, to see if */ 2678/* there are any matches. The first check is to see if a match can be seen */ 2679/* in the cache. If not, then search an appropriate list of rules. Once a */ 2680/* matching rule is found, take any appropriate actions as defined by the */ 2681/* rule - except logging. */ 2682/* ------------------------------------------------------------------------ */ 2683static frentry_t * 2684ipf_firewall(fin, passp) 2685 fr_info_t *fin; 2686 u_32_t *passp; 2687{ 2688 ipf_main_softc_t *softc = fin->fin_main_soft; 2689 frentry_t *fr; 2690 u_32_t pass; 2691 int out; 2692 2693 out = fin->fin_out; 2694 pass = *passp; 2695 2696 /* 2697 * This rule cache will only affect packets that are not being 2698 * statefully filtered. 2699 */ 2700 fin->fin_fr = softc->ipf_rules[out][softc->ipf_active]; 2701 if (fin->fin_fr != NULL) 2702 pass = ipf_scanlist(fin, softc->ipf_pass); 2703 2704 if ((pass & FR_NOMATCH)) { 2705 LBUMPD(ipf_stats[out], fr_nom); 2706 } 2707 fr = fin->fin_fr; 2708 2709 /* 2710 * Apply packets per second rate-limiting to a rule as required. 2711 */ 2712 if ((fr != NULL) && (fr->fr_pps != 0) && 2713 !ppsratecheck(&fr->fr_lastpkt, &fr->fr_curpps, fr->fr_pps)) { 2714 DT2(frb_ppsrate, fr_info_t *, fin, frentry_t *, fr); 2715 pass &= ~(FR_CMDMASK|FR_RETICMP|FR_RETRST); 2716 pass |= FR_BLOCK; 2717 LBUMPD(ipf_stats[out], fr_ppshit); 2718 fin->fin_reason = FRB_PPSRATE; 2719 } 2720 2721 /* 2722 * If we fail to add a packet to the authorization queue, then we 2723 * drop the packet later. However, if it was added then pretend 2724 * we've dropped it already. 2725 */ 2726 if (FR_ISAUTH(pass)) { 2727 if (ipf_auth_new(fin->fin_m, fin) != 0) { 2728 DT1(frb_authnew, fr_info_t *, fin); 2729 fin->fin_m = *fin->fin_mp = NULL; 2730 fin->fin_reason = FRB_AUTHNEW; 2731 fin->fin_error = 0; 2732 } else { 2733 IPFERROR(1); 2734 fin->fin_error = ENOSPC; 2735 } 2736 } 2737 2738 if ((fr != NULL) && (fr->fr_func != NULL) && 2739 (fr->fr_func != (ipfunc_t)-1) && !(pass & FR_CALLNOW)) 2740 (void) (*fr->fr_func)(fin, &pass); 2741 2742 /* 2743 * If a rule is a pre-auth rule, check again in the list of rules 2744 * loaded for authenticated use. It does not particulary matter 2745 * if this search fails because a "preauth" result, from a rule, 2746 * is treated as "not a pass", hence the packet is blocked. 2747 */ 2748 if (FR_ISPREAUTH(pass)) { 2749 pass = ipf_auth_pre_scanlist(softc, fin, pass); 2750 } 2751 2752 /* 2753 * If the rule has "keep frag" and the packet is actually a fragment, 2754 * then create a fragment state entry. 2755 */ 2756 if (pass & FR_KEEPFRAG) { 2757 if (fin->fin_flx & FI_FRAG) { 2758 if (ipf_frag_new(softc, fin, pass) == -1) { 2759 LBUMP(ipf_stats[out].fr_bnfr); 2760 } else { 2761 LBUMP(ipf_stats[out].fr_nfr); 2762 } 2763 } else { 2764 LBUMP(ipf_stats[out].fr_cfr); 2765 } 2766 } 2767 2768 fr = fin->fin_fr; 2769 *passp = pass; 2770 2771 return fr; 2772} 2773 2774 2775/* ------------------------------------------------------------------------ */ 2776/* Function: ipf_check */ 2777/* Returns: int - 0 == packet allowed through, */ 2778/* User space: */ 2779/* -1 == packet blocked */ 2780/* 1 == packet not matched */ 2781/* -2 == requires authentication */ 2782/* Kernel: */ 2783/* > 0 == filter error # for packet */ 2784/* Parameters: ip(I) - pointer to start of IPv4/6 packet */ 2785/* hlen(I) - length of header */ 2786/* ifp(I) - pointer to interface this packet is on */ 2787/* out(I) - 0 == packet going in, 1 == packet going out */ 2788/* mp(IO) - pointer to caller's buffer pointer that holds this */ 2789/* IP packet. */ 2790/* Solaris & HP-UX ONLY : */ 2791/* qpi(I) - pointer to STREAMS queue information for this */ 2792/* interface & direction. */ 2793/* */ 2794/* ipf_check() is the master function for all IPFilter packet processing. */ 2795/* It orchestrates: Network Address Translation (NAT), checking for packet */ 2796/* authorisation (or pre-authorisation), presence of related state info., */ 2797/* generating log entries, IP packet accounting, routing of packets as */ 2798/* directed by firewall rules and of course whether or not to allow the */ 2799/* packet to be further processed by the kernel. */ 2800/* */ 2801/* For packets blocked, the contents of "mp" will be NULL'd and the buffer */ 2802/* freed. Packets passed may be returned with the pointer pointed to by */ 2803/* by "mp" changed to a new buffer. */ 2804/* ------------------------------------------------------------------------ */ 2805int 2806ipf_check(ctx, ip, hlen, ifp, out 2807#if defined(_KERNEL) && defined(MENTAT) 2808 , qif, mp) 2809 void *qif; 2810#else 2811 , mp) 2812#endif 2813 mb_t **mp; 2814 ip_t *ip; 2815 int hlen; 2816 void *ifp; 2817 int out; 2818 void *ctx; 2819{ 2820 /* 2821 * The above really sucks, but short of writing a diff 2822 */ 2823 ipf_main_softc_t *softc = ctx; 2824 fr_info_t frinfo; 2825 fr_info_t *fin = &frinfo; 2826 u_32_t pass = softc->ipf_pass; 2827 frentry_t *fr = NULL; 2828 int v = IP_V(ip); 2829 mb_t *mc = NULL; 2830 mb_t *m; 2831 /* 2832 * The first part of ipf_check() deals with making sure that what goes 2833 * into the filtering engine makes some sense. Information about the 2834 * the packet is distilled, collected into a fr_info_t structure and 2835 * the an attempt to ensure the buffer the packet is in is big enough 2836 * to hold all the required packet headers. 2837 */ 2838#ifdef _KERNEL 2839# ifdef MENTAT 2840 qpktinfo_t *qpi = qif; 2841 2842# ifdef __sparc 2843 if ((u_int)ip & 0x3) 2844 return 2; 2845# endif 2846# else 2847 SPL_INT(s); 2848# endif 2849 2850 if (softc->ipf_running <= 0) { 2851 return 0; 2852 } 2853 2854 bzero((char *)fin, sizeof(*fin)); 2855 2856# ifdef MENTAT 2857 if (qpi->qpi_flags & QF_BROADCAST) 2858 fin->fin_flx |= FI_MBCAST|FI_BROADCAST; 2859 if (qpi->qpi_flags & QF_MULTICAST) 2860 fin->fin_flx |= FI_MBCAST|FI_MULTICAST; 2861 m = qpi->qpi_m; 2862 fin->fin_qfm = m; 2863 fin->fin_qpi = qpi; 2864# else /* MENTAT */ 2865 2866 m = *mp; 2867 2868# if defined(M_MCAST) 2869 if ((m->m_flags & M_MCAST) != 0) 2870 fin->fin_flx |= FI_MBCAST|FI_MULTICAST; 2871# endif 2872# if defined(M_MLOOP) 2873 if ((m->m_flags & M_MLOOP) != 0) 2874 fin->fin_flx |= FI_MBCAST|FI_MULTICAST; 2875# endif 2876# if defined(M_BCAST) 2877 if ((m->m_flags & M_BCAST) != 0) 2878 fin->fin_flx |= FI_MBCAST|FI_BROADCAST; 2879# endif 2880# ifdef M_CANFASTFWD 2881 /* 2882 * XXX For now, IP Filter and fast-forwarding of cached flows 2883 * XXX are mutually exclusive. Eventually, IP Filter should 2884 * XXX get a "can-fast-forward" filter rule. 2885 */ 2886 m->m_flags &= ~M_CANFASTFWD; 2887# endif /* M_CANFASTFWD */ 2888# if defined(CSUM_DELAY_DATA) && (!defined(__FreeBSD_version) || \ 2889 (__FreeBSD_version < 501108)) 2890 /* 2891 * disable delayed checksums. 2892 */ 2893 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 2894 in_delayed_cksum(m); 2895 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 2896 } 2897# endif /* CSUM_DELAY_DATA */ 2898# endif /* MENTAT */ 2899#else 2900 bzero((char *)fin, sizeof(*fin)); 2901 m = *mp; 2902# if defined(M_MCAST) 2903 if ((m->m_flags & M_MCAST) != 0) 2904 fin->fin_flx |= FI_MBCAST|FI_MULTICAST; 2905# endif 2906# if defined(M_MLOOP) 2907 if ((m->m_flags & M_MLOOP) != 0) 2908 fin->fin_flx |= FI_MBCAST|FI_MULTICAST; 2909# endif 2910# if defined(M_BCAST) 2911 if ((m->m_flags & M_BCAST) != 0) 2912 fin->fin_flx |= FI_MBCAST|FI_BROADCAST; 2913# endif 2914#endif /* _KERNEL */ 2915 2916 fin->fin_v = v; 2917 fin->fin_m = m; 2918 fin->fin_ip = ip; 2919 fin->fin_mp = mp; 2920 fin->fin_out = out; 2921 fin->fin_ifp = ifp; 2922 fin->fin_error = ENETUNREACH; 2923 fin->fin_hlen = (u_short)hlen; 2924 fin->fin_dp = (char *)ip + hlen; 2925 fin->fin_main_soft = softc; 2926 2927 fin->fin_ipoff = (char *)ip - MTOD(m, char *); 2928 2929 SPL_NET(s); 2930 2931#ifdef USE_INET6 2932 if (v == 6) { 2933 LBUMP(ipf_stats[out].fr_ipv6); 2934 /* 2935 * Jumbo grams are quite likely too big for internal buffer 2936 * structures to handle comfortably, for now, so just drop 2937 * them. 2938 */ 2939 if (((ip6_t *)ip)->ip6_plen == 0) { 2940 DT1(frb_jumbo, ip6_t *, (ip6_t *)ip); 2941 pass = FR_BLOCK|FR_NOMATCH; 2942 fin->fin_reason = FRB_JUMBO; 2943 goto finished; 2944 } 2945 fin->fin_family = AF_INET6; 2946 } else 2947#endif 2948 { 2949 fin->fin_family = AF_INET; 2950 } 2951 2952 if (ipf_makefrip(hlen, ip, fin) == -1) { 2953 DT1(frb_makefrip, fr_info_t *, fin); 2954 pass = FR_BLOCK|FR_NOMATCH; 2955 fin->fin_reason = FRB_MAKEFRIP; 2956 goto finished; 2957 } 2958 2959 /* 2960 * For at least IPv6 packets, if a m_pullup() fails then this pointer 2961 * becomes NULL and so we have no packet to free. 2962 */ 2963 if (*fin->fin_mp == NULL) 2964 goto finished; 2965 2966 if (!out) { 2967 if (v == 4) { 2968 if (softc->ipf_chksrc && !ipf_verifysrc(fin)) { 2969 LBUMPD(ipf_stats[0], fr_v4_badsrc); 2970 fin->fin_flx |= FI_BADSRC; 2971 } 2972 if (fin->fin_ip->ip_ttl < softc->ipf_minttl) { 2973 LBUMPD(ipf_stats[0], fr_v4_badttl); 2974 fin->fin_flx |= FI_LOWTTL; 2975 } 2976 } 2977#ifdef USE_INET6 2978 else if (v == 6) { 2979 if (((ip6_t *)ip)->ip6_hlim < softc->ipf_minttl) { 2980 LBUMPD(ipf_stats[0], fr_v6_badttl); 2981 fin->fin_flx |= FI_LOWTTL; 2982 } 2983 } 2984#endif 2985 } 2986 2987 if (fin->fin_flx & FI_SHORT) { 2988 LBUMPD(ipf_stats[out], fr_short); 2989 } 2990 2991 READ_ENTER(&softc->ipf_mutex); 2992 2993 if (!out) { 2994 switch (fin->fin_v) 2995 { 2996 case 4 : 2997 if (ipf_nat_checkin(fin, &pass) == -1) { 2998 goto filterdone; 2999 } 3000 break; 3001#ifdef USE_INET6 3002 case 6 : 3003 if (ipf_nat6_checkin(fin, &pass) == -1) { 3004 goto filterdone; 3005 } 3006 break; 3007#endif 3008 default : 3009 break; 3010 } 3011 } 3012 /* 3013 * Check auth now. 3014 * If a packet is found in the auth table, then skip checking 3015 * the access lists for permission but we do need to consider 3016 * the result as if it were from the ACL's. In addition, being 3017 * found in the auth table means it has been seen before, so do 3018 * not pass it through accounting (again), lest it be counted twice. 3019 */ 3020 fr = ipf_auth_check(fin, &pass); 3021 if (!out && (fr == NULL)) 3022 (void) ipf_acctpkt(fin, NULL); 3023 3024 if (fr == NULL) { 3025 if ((fin->fin_flx & FI_FRAG) != 0) 3026 fr = ipf_frag_known(fin, &pass); 3027 3028 if (fr == NULL) 3029 fr = ipf_state_check(fin, &pass); 3030 } 3031 3032 if ((pass & FR_NOMATCH) || (fr == NULL)) 3033 fr = ipf_firewall(fin, &pass); 3034 3035 /* 3036 * If we've asked to track state for this packet, set it up. 3037 * Here rather than ipf_firewall because ipf_checkauth may decide 3038 * to return a packet for "keep state" 3039 */ 3040 if ((pass & FR_KEEPSTATE) && (fin->fin_m != NULL) && 3041 !(fin->fin_flx & FI_STATE)) { 3042 if (ipf_state_add(softc, fin, NULL, 0) == 0) { 3043 LBUMP(ipf_stats[out].fr_ads); 3044 } else { 3045 LBUMP(ipf_stats[out].fr_bads); 3046 if (FR_ISPASS(pass)) { 3047 DT(frb_stateadd); 3048 pass &= ~FR_CMDMASK; 3049 pass |= FR_BLOCK; 3050 fin->fin_reason = FRB_STATEADD; 3051 } 3052 } 3053 } 3054 3055 fin->fin_fr = fr; 3056 if ((fr != NULL) && !(fin->fin_flx & FI_STATE)) { 3057 fin->fin_dif = &fr->fr_dif; 3058 fin->fin_tif = &fr->fr_tifs[fin->fin_rev]; 3059 } 3060 3061 /* 3062 * Only count/translate packets which will be passed on, out the 3063 * interface. 3064 */ 3065 if (out && FR_ISPASS(pass)) { 3066 (void) ipf_acctpkt(fin, NULL); 3067 3068 switch (fin->fin_v) 3069 { 3070 case 4 : 3071 if (ipf_nat_checkout(fin, &pass) == -1) { 3072 ; 3073 } else if ((softc->ipf_update_ipid != 0) && (v == 4)) { 3074 if (ipf_updateipid(fin) == -1) { 3075 DT(frb_updateipid); 3076 LBUMP(ipf_stats[1].fr_ipud); 3077 pass &= ~FR_CMDMASK; 3078 pass |= FR_BLOCK; 3079 fin->fin_reason = FRB_UPDATEIPID; 3080 } else { 3081 LBUMP(ipf_stats[0].fr_ipud); 3082 } 3083 } 3084 break; 3085#ifdef USE_INET6 3086 case 6 : 3087 (void) ipf_nat6_checkout(fin, &pass); 3088 break; 3089#endif 3090 default : 3091 break; 3092 } 3093 } 3094 3095filterdone: 3096#ifdef IPFILTER_LOG 3097 if ((softc->ipf_flags & FF_LOGGING) || (pass & FR_LOGMASK)) { 3098 (void) ipf_dolog(fin, &pass); 3099 } 3100#endif 3101 3102 /* 3103 * The FI_STATE flag is cleared here so that calling ipf_state_check 3104 * will work when called from inside of fr_fastroute. Although 3105 * there is a similar flag, FI_NATED, for NAT, it does have the same 3106 * impact on code execution. 3107 */ 3108 fin->fin_flx &= ~FI_STATE; 3109 3110#if defined(FASTROUTE_RECURSION) 3111 /* 3112 * Up the reference on fr_lock and exit ipf_mutex. The generation of 3113 * a packet below can sometimes cause a recursive call into IPFilter. 3114 * On those platforms where that does happen, we need to hang onto 3115 * the filter rule just in case someone decides to remove or flush it 3116 * in the meantime. 3117 */ 3118 if (fr != NULL) { 3119 MUTEX_ENTER(&fr->fr_lock); 3120 fr->fr_ref++; 3121 MUTEX_EXIT(&fr->fr_lock); 3122 } 3123 3124 RWLOCK_EXIT(&softc->ipf_mutex); 3125#endif 3126 3127 if ((pass & FR_RETMASK) != 0) { 3128 /* 3129 * Should we return an ICMP packet to indicate error 3130 * status passing through the packet filter ? 3131 * WARNING: ICMP error packets AND TCP RST packets should 3132 * ONLY be sent in repsonse to incoming packets. Sending 3133 * them in response to outbound packets can result in a 3134 * panic on some operating systems. 3135 */ 3136 if (!out) { 3137 if (pass & FR_RETICMP) { 3138 int dst; 3139 3140 if ((pass & FR_RETMASK) == FR_FAKEICMP) 3141 dst = 1; 3142 else 3143 dst = 0; 3144 (void) ipf_send_icmp_err(ICMP_UNREACH, fin, 3145 dst); 3146 LBUMP(ipf_stats[0].fr_ret); 3147 } else if (((pass & FR_RETMASK) == FR_RETRST) && 3148 !(fin->fin_flx & FI_SHORT)) { 3149 if (((fin->fin_flx & FI_OOW) != 0) || 3150 (ipf_send_reset(fin) == 0)) { 3151 LBUMP(ipf_stats[1].fr_ret); 3152 } 3153 } 3154 3155 /* 3156 * When using return-* with auth rules, the auth code 3157 * takes over disposing of this packet. 3158 */ 3159 if (FR_ISAUTH(pass) && (fin->fin_m != NULL)) { 3160 DT1(frb_authcapture, fr_info_t *, fin); 3161 fin->fin_m = *fin->fin_mp = NULL; 3162 fin->fin_reason = FRB_AUTHCAPTURE; 3163 m = NULL; 3164 } 3165 } else { 3166 if (pass & FR_RETRST) { 3167 fin->fin_error = ECONNRESET; 3168 } 3169 } 3170 } 3171 3172 /* 3173 * After the above so that ICMP unreachables and TCP RSTs get 3174 * created properly. 3175 */ 3176 if (FR_ISBLOCK(pass) && (fin->fin_flx & FI_NEWNAT)) 3177 ipf_nat_uncreate(fin); 3178 3179 /* 3180 * If we didn't drop off the bottom of the list of rules (and thus 3181 * the 'current' rule fr is not NULL), then we may have some extra 3182 * instructions about what to do with a packet. 3183 * Once we're finished return to our caller, freeing the packet if 3184 * we are dropping it. 3185 */ 3186 if (fr != NULL) { 3187 frdest_t *fdp; 3188 3189 /* 3190 * Generate a duplicated packet first because ipf_fastroute 3191 * can lead to fin_m being free'd... not good. 3192 */ 3193 fdp = fin->fin_dif; 3194 if ((fdp != NULL) && (fdp->fd_ptr != NULL) && 3195 (fdp->fd_ptr != (void *)-1)) { 3196 mc = M_COPY(fin->fin_m); 3197 if (mc != NULL) 3198 ipf_fastroute(mc, &mc, fin, fdp); 3199 } 3200 3201 fdp = fin->fin_tif; 3202 if (!out && (pass & FR_FASTROUTE)) { 3203 /* 3204 * For fastroute rule, no destination interface defined 3205 * so pass NULL as the frdest_t parameter 3206 */ 3207 (void) ipf_fastroute(fin->fin_m, mp, fin, NULL); 3208 m = *mp = NULL; 3209 } else if ((fdp != NULL) && (fdp->fd_ptr != NULL) && 3210 (fdp->fd_ptr != (struct ifnet *)-1)) { 3211 /* this is for to rules: */ 3212 ipf_fastroute(fin->fin_m, mp, fin, fdp); 3213 m = *mp = NULL; 3214 } 3215 3216#if defined(FASTROUTE_RECURSION) 3217 (void) ipf_derefrule(softc, &fr); 3218#endif 3219 } 3220#if !defined(FASTROUTE_RECURSION) 3221 RWLOCK_EXIT(&softc->ipf_mutex); 3222#endif 3223 3224finished: 3225 if (!FR_ISPASS(pass)) { 3226 LBUMP(ipf_stats[out].fr_block); 3227 if (*mp != NULL) { 3228#ifdef _KERNEL 3229 FREE_MB_T(*mp); 3230#endif 3231 m = *mp = NULL; 3232 } 3233 } else { 3234 LBUMP(ipf_stats[out].fr_pass); 3235#if defined(_KERNEL) && defined(__sgi) 3236 if ((fin->fin_hbuf != NULL) && 3237 (mtod(fin->fin_m, struct ip *) != fin->fin_ip)) { 3238 COPYBACK(fin->fin_m, 0, fin->fin_plen, fin->fin_hbuf); 3239 } 3240#endif 3241 } 3242 3243 SPL_X(s); 3244 3245#ifdef _KERNEL 3246 if (FR_ISPASS(pass)) 3247 return 0; 3248 LBUMP(ipf_stats[out].fr_blocked[fin->fin_reason]); 3249 return fin->fin_error; 3250#else /* _KERNEL */ 3251 if (*mp != NULL) 3252 (*mp)->mb_ifp = fin->fin_ifp; 3253 blockreason = fin->fin_reason; 3254 FR_VERBOSE(("fin_flx %#x pass %#x ", fin->fin_flx, pass)); 3255 /*if ((pass & FR_CMDMASK) == (softc->ipf_pass & FR_CMDMASK))*/ 3256 if ((pass & FR_NOMATCH) != 0) 3257 return 1; 3258 3259 if ((pass & FR_RETMASK) != 0) 3260 switch (pass & FR_RETMASK) 3261 { 3262 case FR_RETRST : 3263 return 3; 3264 case FR_RETICMP : 3265 return 4; 3266 case FR_FAKEICMP : 3267 return 5; 3268 } 3269 3270 switch (pass & FR_CMDMASK) 3271 { 3272 case FR_PASS : 3273 return 0; 3274 case FR_BLOCK : 3275 return -1; 3276 case FR_AUTH : 3277 return -2; 3278 case FR_ACCOUNT : 3279 return -3; 3280 case FR_PREAUTH : 3281 return -4; 3282 } 3283 return 2; 3284#endif /* _KERNEL */ 3285} 3286 3287 3288#ifdef IPFILTER_LOG 3289/* ------------------------------------------------------------------------ */ 3290/* Function: ipf_dolog */ 3291/* Returns: frentry_t* - returns contents of fin_fr (no change made) */ 3292/* Parameters: fin(I) - pointer to packet information */ 3293/* passp(IO) - pointer to current/new filter decision (unused) */ 3294/* */ 3295/* Checks flags set to see how a packet should be logged, if it is to be */ 3296/* logged. Adjust statistics based on its success or not. */ 3297/* ------------------------------------------------------------------------ */ 3298frentry_t * 3299ipf_dolog(fin, passp) 3300 fr_info_t *fin; 3301 u_32_t *passp; 3302{ 3303 ipf_main_softc_t *softc = fin->fin_main_soft; 3304 u_32_t pass; 3305 int out; 3306 3307 out = fin->fin_out; 3308 pass = *passp; 3309 3310 if ((softc->ipf_flags & FF_LOGNOMATCH) && (pass & FR_NOMATCH)) { 3311 pass |= FF_LOGNOMATCH; 3312 LBUMPD(ipf_stats[out], fr_npkl); 3313 goto logit; 3314 3315 } else if (((pass & FR_LOGMASK) == FR_LOGP) || 3316 (FR_ISPASS(pass) && (softc->ipf_flags & FF_LOGPASS))) { 3317 if ((pass & FR_LOGMASK) != FR_LOGP) 3318 pass |= FF_LOGPASS; 3319 LBUMPD(ipf_stats[out], fr_ppkl); 3320 goto logit; 3321 3322 } else if (((pass & FR_LOGMASK) == FR_LOGB) || 3323 (FR_ISBLOCK(pass) && (softc->ipf_flags & FF_LOGBLOCK))) { 3324 if ((pass & FR_LOGMASK) != FR_LOGB) 3325 pass |= FF_LOGBLOCK; 3326 LBUMPD(ipf_stats[out], fr_bpkl); 3327 3328logit: 3329 if (ipf_log_pkt(fin, pass) == -1) { 3330 /* 3331 * If the "or-block" option has been used then 3332 * block the packet if we failed to log it. 3333 */ 3334 if ((pass & FR_LOGORBLOCK) && FR_ISPASS(pass)) { 3335 DT1(frb_logfail2, u_int, pass); 3336 pass &= ~FR_CMDMASK; 3337 pass |= FR_BLOCK; 3338 fin->fin_reason = FRB_LOGFAIL2; 3339 } 3340 } 3341 *passp = pass; 3342 } 3343 3344 return fin->fin_fr; 3345} 3346#endif /* IPFILTER_LOG */ 3347 3348 3349/* ------------------------------------------------------------------------ */ 3350/* Function: ipf_cksum */ 3351/* Returns: u_short - IP header checksum */ 3352/* Parameters: addr(I) - pointer to start of buffer to checksum */ 3353/* len(I) - length of buffer in bytes */ 3354/* */ 3355/* Calculate the two's complement 16 bit checksum of the buffer passed. */ 3356/* */ 3357/* N.B.: addr should be 16bit aligned. */ 3358/* ------------------------------------------------------------------------ */ 3359u_short 3360ipf_cksum(addr, len) 3361 u_short *addr; 3362 int len; 3363{ 3364 u_32_t sum = 0; 3365 3366 for (sum = 0; len > 1; len -= 2) 3367 sum += *addr++; 3368 3369 /* mop up an odd byte, if necessary */ 3370 if (len == 1) 3371 sum += *(u_char *)addr; 3372 3373 /* 3374 * add back carry outs from top 16 bits to low 16 bits 3375 */ 3376 sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */ 3377 sum += (sum >> 16); /* add carry */ 3378 return (u_short)(~sum); 3379} 3380 3381 3382/* ------------------------------------------------------------------------ */ 3383/* Function: fr_cksum */ 3384/* Returns: u_short - layer 4 checksum */ 3385/* Parameters: fin(I) - pointer to packet information */ 3386/* ip(I) - pointer to IP header */ 3387/* l4proto(I) - protocol to caclulate checksum for */ 3388/* l4hdr(I) - pointer to layer 4 header */ 3389/* */ 3390/* Calculates the TCP checksum for the packet held in "m", using the data */ 3391/* in the IP header "ip" to seed it. */ 3392/* */ 3393/* NB: This function assumes we've pullup'd enough for all of the IP header */ 3394/* and the TCP header. We also assume that data blocks aren't allocated in */ 3395/* odd sizes. */ 3396/* */ 3397/* Expects ip_len and ip_off to be in network byte order when called. */ 3398/* ------------------------------------------------------------------------ */ 3399u_short 3400fr_cksum(fin, ip, l4proto, l4hdr) 3401 fr_info_t *fin; 3402 ip_t *ip; 3403 int l4proto; 3404 void *l4hdr; 3405{ 3406 u_short *sp, slen, sumsave, *csump; 3407 u_int sum, sum2; 3408 int hlen; 3409 int off; 3410#ifdef USE_INET6 3411 ip6_t *ip6; 3412#endif 3413 3414 csump = NULL; 3415 sumsave = 0; 3416 sp = NULL; 3417 slen = 0; 3418 hlen = 0; 3419 sum = 0; 3420 3421 sum = htons((u_short)l4proto); 3422 /* 3423 * Add up IP Header portion 3424 */ 3425#ifdef USE_INET6 3426 if (IP_V(ip) == 4) { 3427#endif 3428 hlen = IP_HL(ip) << 2; 3429 off = hlen; 3430 sp = (u_short *)&ip->ip_src; 3431 sum += *sp++; /* ip_src */ 3432 sum += *sp++; 3433 sum += *sp++; /* ip_dst */ 3434 sum += *sp++; 3435#ifdef USE_INET6 3436 } else if (IP_V(ip) == 6) { 3437 ip6 = (ip6_t *)ip; 3438 hlen = sizeof(*ip6); 3439 off = ((char *)fin->fin_dp - (char *)fin->fin_ip); 3440 sp = (u_short *)&ip6->ip6_src; 3441 sum += *sp++; /* ip6_src */ 3442 sum += *sp++; 3443 sum += *sp++; 3444 sum += *sp++; 3445 sum += *sp++; 3446 sum += *sp++; 3447 sum += *sp++; 3448 sum += *sp++; 3449 /* This needs to be routing header aware. */ 3450 sum += *sp++; /* ip6_dst */ 3451 sum += *sp++; 3452 sum += *sp++; 3453 sum += *sp++; 3454 sum += *sp++; 3455 sum += *sp++; 3456 sum += *sp++; 3457 sum += *sp++; 3458 } else { 3459 return 0xffff; 3460 } 3461#endif 3462 slen = fin->fin_plen - off; 3463 sum += htons(slen); 3464 3465 switch (l4proto) 3466 { 3467 case IPPROTO_UDP : 3468 csump = &((udphdr_t *)l4hdr)->uh_sum; 3469 break; 3470 3471 case IPPROTO_TCP : 3472 csump = &((tcphdr_t *)l4hdr)->th_sum; 3473 break; 3474 case IPPROTO_ICMP : 3475 csump = &((icmphdr_t *)l4hdr)->icmp_cksum; 3476 sum = 0; /* Pseudo-checksum is not included */ 3477 break; 3478#ifdef USE_INET6 3479 case IPPROTO_ICMPV6 : 3480 csump = &((struct icmp6_hdr *)l4hdr)->icmp6_cksum; 3481 break; 3482#endif 3483 default : 3484 break; 3485 } 3486 3487 if (csump != NULL) { 3488 sumsave = *csump; 3489 *csump = 0; 3490 } 3491 3492 sum2 = ipf_pcksum(fin, off, sum); 3493 if (csump != NULL) 3494 *csump = sumsave; 3495 return sum2; 3496} 3497 3498 3499/* ------------------------------------------------------------------------ */ 3500/* Function: ipf_findgroup */ 3501/* Returns: frgroup_t * - NULL = group not found, else pointer to group */ 3502/* Parameters: softc(I) - pointer to soft context main structure */ 3503/* group(I) - group name to search for */ 3504/* unit(I) - device to which this group belongs */ 3505/* set(I) - which set of rules (inactive/inactive) this is */ 3506/* fgpp(O) - pointer to place to store pointer to the pointer */ 3507/* to where to add the next (last) group or where */ 3508/* to delete group from. */ 3509/* */ 3510/* Search amongst the defined groups for a particular group number. */ 3511/* ------------------------------------------------------------------------ */ 3512frgroup_t * 3513ipf_findgroup(softc, group, unit, set, fgpp) 3514 ipf_main_softc_t *softc; 3515 char *group; 3516 minor_t unit; 3517 int set; 3518 frgroup_t ***fgpp; 3519{ 3520 frgroup_t *fg, **fgp; 3521 3522 /* 3523 * Which list of groups to search in is dependent on which list of 3524 * rules are being operated on. 3525 */ 3526 fgp = &softc->ipf_groups[unit][set]; 3527 3528 while ((fg = *fgp) != NULL) { 3529 if (strncmp(group, fg->fg_name, FR_GROUPLEN) == 0) 3530 break; 3531 else 3532 fgp = &fg->fg_next; 3533 } 3534 if (fgpp != NULL) 3535 *fgpp = fgp; 3536 return fg; 3537} 3538 3539 3540/* ------------------------------------------------------------------------ */ 3541/* Function: ipf_group_add */ 3542/* Returns: frgroup_t * - NULL == did not create group, */ 3543/* != NULL == pointer to the group */ 3544/* Parameters: softc(I) - pointer to soft context main structure */ 3545/* num(I) - group number to add */ 3546/* head(I) - rule pointer that is using this as the head */ 3547/* flags(I) - rule flags which describe the type of rule it is */ 3548/* unit(I) - device to which this group will belong to */ 3549/* set(I) - which set of rules (inactive/inactive) this is */ 3550/* Write Locks: ipf_mutex */ 3551/* */ 3552/* Add a new group head, or if it already exists, increase the reference */ 3553/* count to it. */ 3554/* ------------------------------------------------------------------------ */ 3555frgroup_t * 3556ipf_group_add(softc, group, head, flags, unit, set) 3557 ipf_main_softc_t *softc; 3558 char *group; 3559 void *head; 3560 u_32_t flags; 3561 minor_t unit; 3562 int set; 3563{ 3564 frgroup_t *fg, **fgp; 3565 u_32_t gflags; 3566 3567 if (group == NULL) 3568 return NULL; 3569 3570 if (unit == IPL_LOGIPF && *group == '\0') 3571 return NULL; 3572 3573 fgp = NULL; 3574 gflags = flags & FR_INOUT; 3575 3576 fg = ipf_findgroup(softc, group, unit, set, &fgp); 3577 if (fg != NULL) { 3578 if (fg->fg_head == NULL && head != NULL) 3579 fg->fg_head = head; 3580 if (fg->fg_flags == 0) 3581 fg->fg_flags = gflags; 3582 else if (gflags != fg->fg_flags) 3583 return NULL; 3584 fg->fg_ref++; 3585 return fg; 3586 } 3587 3588 KMALLOC(fg, frgroup_t *); 3589 if (fg != NULL) { 3590 fg->fg_head = head; 3591 fg->fg_start = NULL; 3592 fg->fg_next = *fgp; 3593 bcopy(group, fg->fg_name, strlen(group) + 1); 3594 fg->fg_flags = gflags; 3595 fg->fg_ref = 1; 3596 fg->fg_set = &softc->ipf_groups[unit][set]; 3597 *fgp = fg; 3598 } 3599 return fg; 3600} 3601 3602 3603/* ------------------------------------------------------------------------ */ 3604/* Function: ipf_group_del */ 3605/* Returns: int - number of rules deleted */ 3606/* Parameters: softc(I) - pointer to soft context main structure */ 3607/* group(I) - group name to delete */ 3608/* fr(I) - filter rule from which group is referenced */ 3609/* Write Locks: ipf_mutex */ 3610/* */ 3611/* This function is called whenever a reference to a group is to be dropped */ 3612/* and thus its reference count needs to be lowered and the group free'd if */ 3613/* the reference count reaches zero. Passing in fr is really for the sole */ 3614/* purpose of knowing when the head rule is being deleted. */ 3615/* ------------------------------------------------------------------------ */ 3616void 3617ipf_group_del(softc, group, fr) 3618 ipf_main_softc_t *softc; 3619 frgroup_t *group; 3620 frentry_t *fr; 3621{ 3622 3623 if (group->fg_head == fr) 3624 group->fg_head = NULL; 3625 3626 group->fg_ref--; 3627 if ((group->fg_ref == 0) && (group->fg_start == NULL)) 3628 ipf_group_free(group); 3629} 3630 3631 3632/* ------------------------------------------------------------------------ */ 3633/* Function: ipf_group_free */ 3634/* Returns: Nil */ 3635/* Parameters: group(I) - pointer to filter rule group */ 3636/* */ 3637/* Remove the group from the list of groups and free it. */ 3638/* ------------------------------------------------------------------------ */ 3639static void 3640ipf_group_free(group) 3641 frgroup_t *group; 3642{ 3643 frgroup_t **gp; 3644 3645 for (gp = group->fg_set; *gp != NULL; gp = &(*gp)->fg_next) { 3646 if (*gp == group) { 3647 *gp = group->fg_next; 3648 break; 3649 } 3650 } 3651 KFREE(group); 3652} 3653 3654 3655/* ------------------------------------------------------------------------ */ 3656/* Function: ipf_group_flush */ 3657/* Returns: int - number of rules flush from group */ 3658/* Parameters: softc(I) - pointer to soft context main structure */ 3659/* Parameters: group(I) - pointer to filter rule group */ 3660/* */ 3661/* Remove all of the rules that currently are listed under the given group. */ 3662/* ------------------------------------------------------------------------ */ 3663static int 3664ipf_group_flush(softc, group) 3665 ipf_main_softc_t *softc; 3666 frgroup_t *group; 3667{ 3668 int gone = 0; 3669 3670 (void) ipf_flushlist(softc, &gone, &group->fg_start); 3671 3672 return gone; 3673} 3674 3675 3676/* ------------------------------------------------------------------------ */ 3677/* Function: ipf_getrulen */ 3678/* Returns: frentry_t * - NULL == not found, else pointer to rule n */ 3679/* Parameters: softc(I) - pointer to soft context main structure */ 3680/* Parameters: unit(I) - device for which to count the rule's number */ 3681/* flags(I) - which set of rules to find the rule in */ 3682/* group(I) - group name */ 3683/* n(I) - rule number to find */ 3684/* */ 3685/* Find rule # n in group # g and return a pointer to it. Return NULl if */ 3686/* group # g doesn't exist or there are less than n rules in the group. */ 3687/* ------------------------------------------------------------------------ */ 3688frentry_t * 3689ipf_getrulen(softc, unit, group, n) 3690 ipf_main_softc_t *softc; 3691 int unit; 3692 char *group; 3693 u_32_t n; 3694{ 3695 frentry_t *fr; 3696 frgroup_t *fg; 3697 3698 fg = ipf_findgroup(softc, group, unit, softc->ipf_active, NULL); 3699 if (fg == NULL) 3700 return NULL; 3701 for (fr = fg->fg_start; fr && n; fr = fr->fr_next, n--) 3702 ; 3703 if (n != 0) 3704 return NULL; 3705 return fr; 3706} 3707 3708 3709/* ------------------------------------------------------------------------ */ 3710/* Function: ipf_flushlist */ 3711/* Returns: int - >= 0 - number of flushed rules */ 3712/* Parameters: softc(I) - pointer to soft context main structure */ 3713/* nfreedp(O) - pointer to int where flush count is stored */ 3714/* listp(I) - pointer to list to flush pointer */ 3715/* Write Locks: ipf_mutex */ 3716/* */ 3717/* Recursively flush rules from the list, descending groups as they are */ 3718/* encountered. if a rule is the head of a group and it has lost all its */ 3719/* group members, then also delete the group reference. nfreedp is needed */ 3720/* to store the accumulating count of rules removed, whereas the returned */ 3721/* value is just the number removed from the current list. The latter is */ 3722/* needed to correctly adjust reference counts on rules that define groups. */ 3723/* */ 3724/* NOTE: Rules not loaded from user space cannot be flushed. */ 3725/* ------------------------------------------------------------------------ */ 3726static int 3727ipf_flushlist(softc, nfreedp, listp) 3728 ipf_main_softc_t *softc; 3729 int *nfreedp; 3730 frentry_t **listp; 3731{ 3732 int freed = 0; 3733 frentry_t *fp; 3734 3735 while ((fp = *listp) != NULL) { 3736 if ((fp->fr_type & FR_T_BUILTIN) || 3737 !(fp->fr_flags & FR_COPIED)) { 3738 listp = &fp->fr_next; 3739 continue; 3740 } 3741 *listp = fp->fr_next; 3742 if (fp->fr_next != NULL) 3743 fp->fr_next->fr_pnext = fp->fr_pnext; 3744 fp->fr_pnext = NULL; 3745 3746 if (fp->fr_grphead != NULL) { 3747 freed += ipf_group_flush(softc, fp->fr_grphead); 3748 fp->fr_names[fp->fr_grhead] = '\0'; 3749 } 3750 3751 if (fp->fr_icmpgrp != NULL) { 3752 freed += ipf_group_flush(softc, fp->fr_icmpgrp); 3753 fp->fr_names[fp->fr_icmphead] = '\0'; 3754 } 3755 3756 if (fp->fr_srctrack.ht_max_nodes) 3757 ipf_rb_ht_flush(&fp->fr_srctrack); 3758 3759 fp->fr_next = NULL; 3760 3761 ASSERT(fp->fr_ref > 0); 3762 if (ipf_derefrule(softc, &fp) == 0) 3763 freed++; 3764 } 3765 *nfreedp += freed; 3766 return freed; 3767} 3768 3769 3770/* ------------------------------------------------------------------------ */ 3771/* Function: ipf_flush */ 3772/* Returns: int - >= 0 - number of flushed rules */ 3773/* Parameters: softc(I) - pointer to soft context main structure */ 3774/* unit(I) - device for which to flush rules */ 3775/* flags(I) - which set of rules to flush */ 3776/* */ 3777/* Calls flushlist() for all filter rules (accounting, firewall - both IPv4 */ 3778/* and IPv6) as defined by the value of flags. */ 3779/* ------------------------------------------------------------------------ */ 3780int 3781ipf_flush(softc, unit, flags) 3782 ipf_main_softc_t *softc; 3783 minor_t unit; 3784 int flags; 3785{ 3786 int flushed = 0, set; 3787 3788 WRITE_ENTER(&softc->ipf_mutex); 3789 3790 set = softc->ipf_active; 3791 if ((flags & FR_INACTIVE) == FR_INACTIVE) 3792 set = 1 - set; 3793 3794 if (flags & FR_OUTQUE) { 3795 ipf_flushlist(softc, &flushed, &softc->ipf_rules[1][set]); 3796 ipf_flushlist(softc, &flushed, &softc->ipf_acct[1][set]); 3797 } 3798 if (flags & FR_INQUE) { 3799 ipf_flushlist(softc, &flushed, &softc->ipf_rules[0][set]); 3800 ipf_flushlist(softc, &flushed, &softc->ipf_acct[0][set]); 3801 } 3802 3803 flushed += ipf_flush_groups(softc, &softc->ipf_groups[unit][set], 3804 flags & (FR_INQUE|FR_OUTQUE)); 3805 3806 RWLOCK_EXIT(&softc->ipf_mutex); 3807 3808 if (unit == IPL_LOGIPF) { 3809 int tmp; 3810 3811 tmp = ipf_flush(softc, IPL_LOGCOUNT, flags); 3812 if (tmp >= 0) 3813 flushed += tmp; 3814 } 3815 return flushed; 3816} 3817 3818 3819/* ------------------------------------------------------------------------ */ 3820/* Function: ipf_flush_groups */ 3821/* Returns: int - >= 0 - number of flushed rules */ 3822/* Parameters: softc(I) - soft context pointerto work with */ 3823/* grhead(I) - pointer to the start of the group list to flush */ 3824/* flags(I) - which set of rules to flush */ 3825/* */ 3826/* Walk through all of the groups under the given group head and remove all */ 3827/* of those that match the flags passed in. The for loop here is bit more */ 3828/* complicated than usual because the removal of a rule with ipf_derefrule */ 3829/* may end up removing not only the structure pointed to by "fg" but also */ 3830/* what is fg_next and fg_next after that. So if a filter rule is actually */ 3831/* removed from the group then it is necessary to start again. */ 3832/* ------------------------------------------------------------------------ */ 3833static int 3834ipf_flush_groups(softc, grhead, flags) 3835 ipf_main_softc_t *softc; 3836 frgroup_t **grhead; 3837 int flags; 3838{ 3839 frentry_t *fr, **frp; 3840 frgroup_t *fg, **fgp; 3841 int flushed = 0; 3842 int removed = 0; 3843 3844 for (fgp = grhead; (fg = *fgp) != NULL; ) { 3845 while ((fg != NULL) && ((fg->fg_flags & flags) == 0)) 3846 fg = fg->fg_next; 3847 if (fg == NULL) 3848 break; 3849 removed = 0; 3850 frp = &fg->fg_start; 3851 while ((removed == 0) && ((fr = *frp) != NULL)) { 3852 if ((fr->fr_flags & flags) == 0) { 3853 frp = &fr->fr_next; 3854 } else { 3855 if (fr->fr_next != NULL) 3856 fr->fr_next->fr_pnext = fr->fr_pnext; 3857 *frp = fr->fr_next; 3858 fr->fr_pnext = NULL; 3859 fr->fr_next = NULL; 3860 (void) ipf_derefrule(softc, &fr); 3861 flushed++; 3862 removed++; 3863 } 3864 } 3865 if (removed == 0) 3866 fgp = &fg->fg_next; 3867 } 3868 return flushed; 3869} 3870 3871 3872/* ------------------------------------------------------------------------ */ 3873/* Function: memstr */ 3874/* Returns: char * - NULL if failed, != NULL pointer to matching bytes */ 3875/* Parameters: src(I) - pointer to byte sequence to match */ 3876/* dst(I) - pointer to byte sequence to search */ 3877/* slen(I) - match length */ 3878/* dlen(I) - length available to search in */ 3879/* */ 3880/* Search dst for a sequence of bytes matching those at src and extend for */ 3881/* slen bytes. */ 3882/* ------------------------------------------------------------------------ */ 3883char * 3884memstr(src, dst, slen, dlen) 3885 const char *src; 3886 char *dst; 3887 size_t slen, dlen; 3888{ 3889 char *s = NULL; 3890 3891 while (dlen >= slen) { 3892 if (bcmp(src, dst, slen) == 0) { 3893 s = dst; 3894 break; 3895 } 3896 dst++; 3897 dlen--; 3898 } 3899 return s; 3900} 3901/* ------------------------------------------------------------------------ */ 3902/* Function: ipf_fixskip */ 3903/* Returns: Nil */ 3904/* Parameters: listp(IO) - pointer to start of list with skip rule */ 3905/* rp(I) - rule added/removed with skip in it. */ 3906/* addremove(I) - adjustment (-1/+1) to make to skip count, */ 3907/* depending on whether a rule was just added */ 3908/* or removed. */ 3909/* */ 3910/* Adjust all the rules in a list which would have skip'd past the position */ 3911/* where we are inserting to skip to the right place given the change. */ 3912/* ------------------------------------------------------------------------ */ 3913void 3914ipf_fixskip(listp, rp, addremove) 3915 frentry_t **listp, *rp; 3916 int addremove; 3917{ 3918 int rules, rn; 3919 frentry_t *fp; 3920 3921 rules = 0; 3922 for (fp = *listp; (fp != NULL) && (fp != rp); fp = fp->fr_next) 3923 rules++; 3924 3925 if (!fp) 3926 return; 3927 3928 for (rn = 0, fp = *listp; fp && (fp != rp); fp = fp->fr_next, rn++) 3929 if (FR_ISSKIP(fp->fr_flags) && (rn + fp->fr_arg >= rules)) 3930 fp->fr_arg += addremove; 3931} 3932 3933 3934#ifdef _KERNEL 3935/* ------------------------------------------------------------------------ */ 3936/* Function: count4bits */ 3937/* Returns: int - >= 0 - number of consecutive bits in input */ 3938/* Parameters: ip(I) - 32bit IP address */ 3939/* */ 3940/* IPv4 ONLY */ 3941/* count consecutive 1's in bit mask. If the mask generated by counting */ 3942/* consecutive 1's is different to that passed, return -1, else return # */ 3943/* of bits. */ 3944/* ------------------------------------------------------------------------ */ 3945int 3946count4bits(ip) 3947 u_32_t ip; 3948{ 3949 u_32_t ipn; 3950 int cnt = 0, i, j; 3951 3952 ip = ipn = ntohl(ip); 3953 for (i = 32; i; i--, ipn *= 2) 3954 if (ipn & 0x80000000) 3955 cnt++; 3956 else 3957 break; 3958 ipn = 0; 3959 for (i = 32, j = cnt; i; i--, j--) { 3960 ipn *= 2; 3961 if (j > 0) 3962 ipn++; 3963 } 3964 if (ipn == ip) 3965 return cnt; 3966 return -1; 3967} 3968 3969 3970/* ------------------------------------------------------------------------ */ 3971/* Function: count6bits */ 3972/* Returns: int - >= 0 - number of consecutive bits in input */ 3973/* Parameters: msk(I) - pointer to start of IPv6 bitmask */ 3974/* */ 3975/* IPv6 ONLY */ 3976/* count consecutive 1's in bit mask. */ 3977/* ------------------------------------------------------------------------ */ 3978# ifdef USE_INET6 3979int 3980count6bits(msk) 3981 u_32_t *msk; 3982{ 3983 int i = 0, k; 3984 u_32_t j; 3985 3986 for (k = 3; k >= 0; k--) 3987 if (msk[k] == 0xffffffff) 3988 i += 32; 3989 else { 3990 for (j = msk[k]; j; j <<= 1) 3991 if (j & 0x80000000) 3992 i++; 3993 } 3994 return i; 3995} 3996# endif 3997#endif /* _KERNEL */ 3998 3999 4000/* ------------------------------------------------------------------------ */ 4001/* Function: ipf_synclist */ 4002/* Returns: int - 0 = no failures, else indication of first failure */ 4003/* Parameters: fr(I) - start of filter list to sync interface names for */ 4004/* ifp(I) - interface pointer for limiting sync lookups */ 4005/* Write Locks: ipf_mutex */ 4006/* */ 4007/* Walk through a list of filter rules and resolve any interface names into */ 4008/* pointers. Where dynamic addresses are used, also update the IP address */ 4009/* used in the rule. The interface pointer is used to limit the lookups to */ 4010/* a specific set of matching names if it is non-NULL. */ 4011/* Errors can occur when resolving the destination name of to/dup-to fields */ 4012/* when the name points to a pool and that pool doest not exist. If this */ 4013/* does happen then it is necessary to check if there are any lookup refs */ 4014/* that need to be dropped before returning with an error. */ 4015/* ------------------------------------------------------------------------ */ 4016static int 4017ipf_synclist(softc, fr, ifp) 4018 ipf_main_softc_t *softc; 4019 frentry_t *fr; 4020 void *ifp; 4021{ 4022 frentry_t *frt, *start = fr; 4023 frdest_t *fdp; 4024 char *name; 4025 int error; 4026 void *ifa; 4027 int v, i; 4028 4029 error = 0; 4030 4031 for (; fr; fr = fr->fr_next) { 4032 if (fr->fr_family == AF_INET) 4033 v = 4; 4034 else if (fr->fr_family == AF_INET6) 4035 v = 6; 4036 else 4037 v = 0; 4038 4039 /* 4040 * Lookup all the interface names that are part of the rule. 4041 */ 4042 for (i = 0; i < 4; i++) { 4043 if ((ifp != NULL) && (fr->fr_ifas[i] != ifp)) 4044 continue; 4045 if (fr->fr_ifnames[i] == -1) 4046 continue; 4047 name = FR_NAME(fr, fr_ifnames[i]); 4048 fr->fr_ifas[i] = ipf_resolvenic(softc, name, v); 4049 } 4050 4051 if ((fr->fr_type & ~FR_T_BUILTIN) == FR_T_IPF) { 4052 if (fr->fr_satype != FRI_NORMAL && 4053 fr->fr_satype != FRI_LOOKUP) { 4054 ifa = ipf_resolvenic(softc, fr->fr_names + 4055 fr->fr_sifpidx, v); 4056 ipf_ifpaddr(softc, v, fr->fr_satype, ifa, 4057 &fr->fr_src6, &fr->fr_smsk6); 4058 } 4059 if (fr->fr_datype != FRI_NORMAL && 4060 fr->fr_datype != FRI_LOOKUP) { 4061 ifa = ipf_resolvenic(softc, fr->fr_names + 4062 fr->fr_sifpidx, v); 4063 ipf_ifpaddr(softc, v, fr->fr_datype, ifa, 4064 &fr->fr_dst6, &fr->fr_dmsk6); 4065 } 4066 } 4067 4068 fdp = &fr->fr_tifs[0]; 4069 if ((ifp == NULL) || (fdp->fd_ptr == ifp)) { 4070 error = ipf_resolvedest(softc, fr->fr_names, fdp, v); 4071 if (error != 0) 4072 goto unwind; 4073 } 4074 4075 fdp = &fr->fr_tifs[1]; 4076 if ((ifp == NULL) || (fdp->fd_ptr == ifp)) { 4077 error = ipf_resolvedest(softc, fr->fr_names, fdp, v); 4078 if (error != 0) 4079 goto unwind; 4080 } 4081 4082 fdp = &fr->fr_dif; 4083 if ((ifp == NULL) || (fdp->fd_ptr == ifp)) { 4084 error = ipf_resolvedest(softc, fr->fr_names, fdp, v); 4085 if (error != 0) 4086 goto unwind; 4087 } 4088 4089 if (((fr->fr_type & ~FR_T_BUILTIN) == FR_T_IPF) && 4090 (fr->fr_satype == FRI_LOOKUP) && (fr->fr_srcptr == NULL)) { 4091 fr->fr_srcptr = ipf_lookup_res_num(softc, 4092 fr->fr_srctype, 4093 IPL_LOGIPF, 4094 fr->fr_srcnum, 4095 &fr->fr_srcfunc); 4096 } 4097 if (((fr->fr_type & ~FR_T_BUILTIN) == FR_T_IPF) && 4098 (fr->fr_datype == FRI_LOOKUP) && (fr->fr_dstptr == NULL)) { 4099 fr->fr_dstptr = ipf_lookup_res_num(softc, 4100 fr->fr_dsttype, 4101 IPL_LOGIPF, 4102 fr->fr_dstnum, 4103 &fr->fr_dstfunc); 4104 } 4105 } 4106 return 0; 4107 4108unwind: 4109 for (frt = start; frt != fr; fr = fr->fr_next) { 4110 if (((frt->fr_type & ~FR_T_BUILTIN) == FR_T_IPF) && 4111 (frt->fr_satype == FRI_LOOKUP) && (frt->fr_srcptr != NULL)) 4112 ipf_lookup_deref(softc, frt->fr_srctype, 4113 frt->fr_srcptr); 4114 if (((frt->fr_type & ~FR_T_BUILTIN) == FR_T_IPF) && 4115 (frt->fr_datype == FRI_LOOKUP) && (frt->fr_dstptr != NULL)) 4116 ipf_lookup_deref(softc, frt->fr_dsttype, 4117 frt->fr_dstptr); 4118 } 4119 return error; 4120} 4121 4122 4123/* ------------------------------------------------------------------------ */ 4124/* Function: ipf_sync */ 4125/* Returns: void */ 4126/* Parameters: Nil */ 4127/* */ 4128/* ipf_sync() is called when we suspect that the interface list or */ 4129/* information about interfaces (like IP#) has changed. Go through all */ 4130/* filter rules, NAT entries and the state table and check if anything */ 4131/* needs to be changed/updated. */ 4132/* ------------------------------------------------------------------------ */ 4133int 4134ipf_sync(softc, ifp) 4135 ipf_main_softc_t *softc; 4136 void *ifp; 4137{ 4138 int i; 4139 4140# if !SOLARIS 4141 ipf_nat_sync(softc, ifp); 4142 ipf_state_sync(softc, ifp); 4143 ipf_lookup_sync(softc, ifp); 4144# endif 4145 4146 WRITE_ENTER(&softc->ipf_mutex); 4147 (void) ipf_synclist(softc, softc->ipf_acct[0][softc->ipf_active], ifp); 4148 (void) ipf_synclist(softc, softc->ipf_acct[1][softc->ipf_active], ifp); 4149 (void) ipf_synclist(softc, softc->ipf_rules[0][softc->ipf_active], ifp); 4150 (void) ipf_synclist(softc, softc->ipf_rules[1][softc->ipf_active], ifp); 4151 4152 for (i = 0; i < IPL_LOGSIZE; i++) { 4153 frgroup_t *g; 4154 4155 for (g = softc->ipf_groups[i][0]; g != NULL; g = g->fg_next) 4156 (void) ipf_synclist(softc, g->fg_start, ifp); 4157 for (g = softc->ipf_groups[i][1]; g != NULL; g = g->fg_next) 4158 (void) ipf_synclist(softc, g->fg_start, ifp); 4159 } 4160 RWLOCK_EXIT(&softc->ipf_mutex); 4161 4162 return 0; 4163} 4164 4165 4166/* 4167 * In the functions below, bcopy() is called because the pointer being 4168 * copied _from_ in this instance is a pointer to a char buf (which could 4169 * end up being unaligned) and on the kernel's local stack. 4170 */ 4171/* ------------------------------------------------------------------------ */ 4172/* Function: copyinptr */ 4173/* Returns: int - 0 = success, else failure */ 4174/* Parameters: src(I) - pointer to the source address */ 4175/* dst(I) - destination address */ 4176/* size(I) - number of bytes to copy */ 4177/* */ 4178/* Copy a block of data in from user space, given a pointer to the pointer */ 4179/* to start copying from (src) and a pointer to where to store it (dst). */ 4180/* NB: src - pointer to user space pointer, dst - kernel space pointer */ 4181/* ------------------------------------------------------------------------ */ 4182int 4183copyinptr(softc, src, dst, size) 4184 ipf_main_softc_t *softc; 4185 void *src, *dst; 4186 size_t size; 4187{ 4188 caddr_t ca; 4189 int error; 4190 4191# if SOLARIS 4192 error = COPYIN(src, &ca, sizeof(ca)); 4193 if (error != 0) 4194 return error; 4195# else 4196 bcopy(src, (caddr_t)&ca, sizeof(ca)); 4197# endif 4198 error = COPYIN(ca, dst, size); 4199 if (error != 0) { 4200 IPFERROR(3); 4201 error = EFAULT; 4202 } 4203 return error; 4204} 4205 4206 4207/* ------------------------------------------------------------------------ */ 4208/* Function: copyoutptr */ 4209/* Returns: int - 0 = success, else failure */ 4210/* Parameters: src(I) - pointer to the source address */ 4211/* dst(I) - destination address */ 4212/* size(I) - number of bytes to copy */ 4213/* */ 4214/* Copy a block of data out to user space, given a pointer to the pointer */ 4215/* to start copying from (src) and a pointer to where to store it (dst). */ 4216/* NB: src - kernel space pointer, dst - pointer to user space pointer. */ 4217/* ------------------------------------------------------------------------ */ 4218int 4219copyoutptr(softc, src, dst, size) 4220 ipf_main_softc_t *softc; 4221 void *src, *dst; 4222 size_t size; 4223{ 4224 caddr_t ca; 4225 int error; 4226 4227 bcopy(dst, (caddr_t)&ca, sizeof(ca)); 4228 error = COPYOUT(src, ca, size); 4229 if (error != 0) { 4230 IPFERROR(4); 4231 error = EFAULT; 4232 } 4233 return error; 4234} 4235#ifdef _KERNEL 4236#endif 4237 4238 4239/* ------------------------------------------------------------------------ */ 4240/* Function: ipf_lock */ 4241/* Returns: int - 0 = success, else error */ 4242/* Parameters: data(I) - pointer to lock value to set */ 4243/* lockp(O) - pointer to location to store old lock value */ 4244/* */ 4245/* Get the new value for the lock integer, set it and return the old value */ 4246/* in *lockp. */ 4247/* ------------------------------------------------------------------------ */ 4248int 4249ipf_lock(data, lockp) 4250 caddr_t data; 4251 int *lockp; 4252{ 4253 int arg, err; 4254 4255 err = BCOPYIN(data, &arg, sizeof(arg)); 4256 if (err != 0) 4257 return EFAULT; 4258 err = BCOPYOUT(lockp, data, sizeof(*lockp)); 4259 if (err != 0) 4260 return EFAULT; 4261 *lockp = arg; 4262 return 0; 4263} 4264 4265 4266/* ------------------------------------------------------------------------ */ 4267/* Function: ipf_getstat */ 4268/* Returns: Nil */ 4269/* Parameters: softc(I) - pointer to soft context main structure */ 4270/* fiop(I) - pointer to ipfilter stats structure */ 4271/* rev(I) - version claim by program doing ioctl */ 4272/* */ 4273/* Stores a copy of current pointers, counters, etc, in the friostat */ 4274/* structure. */ 4275/* If IPFILTER_COMPAT is compiled, we pretend to be whatever version the */ 4276/* program is looking for. This ensure that validation of the version it */ 4277/* expects will always succeed. Thus kernels with IPFILTER_COMPAT will */ 4278/* allow older binaries to work but kernels without it will not. */ 4279/* ------------------------------------------------------------------------ */ 4280/*ARGSUSED*/ 4281static void 4282ipf_getstat(softc, fiop, rev) 4283 ipf_main_softc_t *softc; 4284 friostat_t *fiop; 4285 int rev; 4286{ 4287 int i; 4288 4289 bcopy((char *)softc->ipf_stats, (char *)fiop->f_st, 4290 sizeof(ipf_statistics_t) * 2); 4291 fiop->f_locks[IPL_LOGSTATE] = -1; 4292 fiop->f_locks[IPL_LOGNAT] = -1; 4293 fiop->f_locks[IPL_LOGIPF] = -1; 4294 fiop->f_locks[IPL_LOGAUTH] = -1; 4295 4296 fiop->f_ipf[0][0] = softc->ipf_rules[0][0]; 4297 fiop->f_acct[0][0] = softc->ipf_acct[0][0]; 4298 fiop->f_ipf[0][1] = softc->ipf_rules[0][1]; 4299 fiop->f_acct[0][1] = softc->ipf_acct[0][1]; 4300 fiop->f_ipf[1][0] = softc->ipf_rules[1][0]; 4301 fiop->f_acct[1][0] = softc->ipf_acct[1][0]; 4302 fiop->f_ipf[1][1] = softc->ipf_rules[1][1]; 4303 fiop->f_acct[1][1] = softc->ipf_acct[1][1]; 4304 4305 fiop->f_ticks = softc->ipf_ticks; 4306 fiop->f_active = softc->ipf_active; 4307 fiop->f_froute[0] = softc->ipf_frouteok[0]; 4308 fiop->f_froute[1] = softc->ipf_frouteok[1]; 4309 fiop->f_rb_no_mem = softc->ipf_rb_no_mem; 4310 fiop->f_rb_node_max = softc->ipf_rb_node_max; 4311 4312 fiop->f_running = softc->ipf_running; 4313 for (i = 0; i < IPL_LOGSIZE; i++) { 4314 fiop->f_groups[i][0] = softc->ipf_groups[i][0]; 4315 fiop->f_groups[i][1] = softc->ipf_groups[i][1]; 4316 } 4317#ifdef IPFILTER_LOG 4318 fiop->f_log_ok = ipf_log_logok(softc, IPL_LOGIPF); 4319 fiop->f_log_fail = ipf_log_failures(softc, IPL_LOGIPF); 4320 fiop->f_logging = 1; 4321#else 4322 fiop->f_log_ok = 0; 4323 fiop->f_log_fail = 0; 4324 fiop->f_logging = 0; 4325#endif 4326 fiop->f_defpass = softc->ipf_pass; 4327 fiop->f_features = ipf_features; 4328 4329#ifdef IPFILTER_COMPAT 4330 sprintf(fiop->f_version, "IP Filter: v%d.%d.%d", 4331 (rev / 1000000) % 100, 4332 (rev / 10000) % 100, 4333 (rev / 100) % 100); 4334#else 4335 rev = rev; 4336 (void) strncpy(fiop->f_version, ipfilter_version, 4337 sizeof(fiop->f_version)); 4338#endif 4339} 4340 4341 4342#ifdef USE_INET6 4343int icmptoicmp6types[ICMP_MAXTYPE+1] = { 4344 ICMP6_ECHO_REPLY, /* 0: ICMP_ECHOREPLY */ 4345 -1, /* 1: UNUSED */ 4346 -1, /* 2: UNUSED */ 4347 ICMP6_DST_UNREACH, /* 3: ICMP_UNREACH */ 4348 -1, /* 4: ICMP_SOURCEQUENCH */ 4349 ND_REDIRECT, /* 5: ICMP_REDIRECT */ 4350 -1, /* 6: UNUSED */ 4351 -1, /* 7: UNUSED */ 4352 ICMP6_ECHO_REQUEST, /* 8: ICMP_ECHO */ 4353 -1, /* 9: UNUSED */ 4354 -1, /* 10: UNUSED */ 4355 ICMP6_TIME_EXCEEDED, /* 11: ICMP_TIMXCEED */ 4356 ICMP6_PARAM_PROB, /* 12: ICMP_PARAMPROB */ 4357 -1, /* 13: ICMP_TSTAMP */ 4358 -1, /* 14: ICMP_TSTAMPREPLY */ 4359 -1, /* 15: ICMP_IREQ */ 4360 -1, /* 16: ICMP_IREQREPLY */ 4361 -1, /* 17: ICMP_MASKREQ */ 4362 -1, /* 18: ICMP_MASKREPLY */ 4363}; 4364 4365 4366int icmptoicmp6unreach[ICMP_MAX_UNREACH] = { 4367 ICMP6_DST_UNREACH_ADDR, /* 0: ICMP_UNREACH_NET */ 4368 ICMP6_DST_UNREACH_ADDR, /* 1: ICMP_UNREACH_HOST */ 4369 -1, /* 2: ICMP_UNREACH_PROTOCOL */ 4370 ICMP6_DST_UNREACH_NOPORT, /* 3: ICMP_UNREACH_PORT */ 4371 -1, /* 4: ICMP_UNREACH_NEEDFRAG */ 4372 ICMP6_DST_UNREACH_NOTNEIGHBOR, /* 5: ICMP_UNREACH_SRCFAIL */ 4373 ICMP6_DST_UNREACH_ADDR, /* 6: ICMP_UNREACH_NET_UNKNOWN */ 4374 ICMP6_DST_UNREACH_ADDR, /* 7: ICMP_UNREACH_HOST_UNKNOWN */ 4375 -1, /* 8: ICMP_UNREACH_ISOLATED */ 4376 ICMP6_DST_UNREACH_ADMIN, /* 9: ICMP_UNREACH_NET_PROHIB */ 4377 ICMP6_DST_UNREACH_ADMIN, /* 10: ICMP_UNREACH_HOST_PROHIB */ 4378 -1, /* 11: ICMP_UNREACH_TOSNET */ 4379 -1, /* 12: ICMP_UNREACH_TOSHOST */ 4380 ICMP6_DST_UNREACH_ADMIN, /* 13: ICMP_UNREACH_ADMIN_PROHIBIT */ 4381}; 4382int icmpreplytype6[ICMP6_MAXTYPE + 1]; 4383#endif 4384 4385int icmpreplytype4[ICMP_MAXTYPE + 1]; 4386 4387 4388/* ------------------------------------------------------------------------ */ 4389/* Function: ipf_matchicmpqueryreply */ 4390/* Returns: int - 1 if "icmp" is a valid reply to "ic" else 0. */ 4391/* Parameters: v(I) - IP protocol version (4 or 6) */ 4392/* ic(I) - ICMP information */ 4393/* icmp(I) - ICMP packet header */ 4394/* rev(I) - direction (0 = forward/1 = reverse) of packet */ 4395/* */ 4396/* Check if the ICMP packet defined by the header pointed to by icmp is a */ 4397/* reply to one as described by what's in ic. If it is a match, return 1, */ 4398/* else return 0 for no match. */ 4399/* ------------------------------------------------------------------------ */ 4400int 4401ipf_matchicmpqueryreply(v, ic, icmp, rev) 4402 int v; 4403 icmpinfo_t *ic; 4404 icmphdr_t *icmp; 4405 int rev; 4406{ 4407 int ictype; 4408 4409 ictype = ic->ici_type; 4410 4411 if (v == 4) { 4412 /* 4413 * If we matched its type on the way in, then when going out 4414 * it will still be the same type. 4415 */ 4416 if ((!rev && (icmp->icmp_type == ictype)) || 4417 (rev && (icmpreplytype4[ictype] == icmp->icmp_type))) { 4418 if (icmp->icmp_type != ICMP_ECHOREPLY) 4419 return 1; 4420 if (icmp->icmp_id == ic->ici_id) 4421 return 1; 4422 } 4423 } 4424#ifdef USE_INET6 4425 else if (v == 6) { 4426 if ((!rev && (icmp->icmp_type == ictype)) || 4427 (rev && (icmpreplytype6[ictype] == icmp->icmp_type))) { 4428 if (icmp->icmp_type != ICMP6_ECHO_REPLY) 4429 return 1; 4430 if (icmp->icmp_id == ic->ici_id) 4431 return 1; 4432 } 4433 } 4434#endif 4435 return 0; 4436} 4437 4438 4439/* ------------------------------------------------------------------------ */ 4440/* Function: ipf_rule_compare */ 4441/* Parameters: fr1(I) - first rule structure to compare */ 4442/* fr2(I) - second rule structure to compare */ 4443/* Returns: int - 0 == rules are the same, else mismatch */ 4444/* */ 4445/* Compare two rules and return 0 if they match or a number indicating */ 4446/* which of the individual checks failed. */ 4447/* ------------------------------------------------------------------------ */ 4448static int 4449ipf_rule_compare(frentry_t *fr1, frentry_t *fr2) 4450{ 4451 if (fr1->fr_cksum != fr2->fr_cksum) 4452 return 1; 4453 if (fr1->fr_size != fr2->fr_size) 4454 return 2; 4455 if (fr1->fr_dsize != fr2->fr_dsize) 4456 return 3; 4457 if (bcmp((char *)&fr1->fr_func, (char *)&fr2->fr_func, 4458 fr1->fr_size - offsetof(struct frentry, fr_func)) != 0) 4459 return 4; 4460 if (fr1->fr_data && !fr2->fr_data) 4461 return 5; 4462 if (!fr1->fr_data && fr2->fr_data) 4463 return 6; 4464 if (fr1->fr_data) { 4465 if (bcmp(fr1->fr_caddr, fr2->fr_caddr, fr1->fr_dsize)) 4466 return 7; 4467 } 4468 return 0; 4469} 4470 4471 4472/* ------------------------------------------------------------------------ */ 4473/* Function: frrequest */ 4474/* Returns: int - 0 == success, > 0 == errno value */ 4475/* Parameters: unit(I) - device for which this is for */ 4476/* req(I) - ioctl command (SIOC*) */ 4477/* data(I) - pointr to ioctl data */ 4478/* set(I) - 1 or 0 (filter set) */ 4479/* makecopy(I) - flag indicating whether data points to a rule */ 4480/* in kernel space & hence doesn't need copying. */ 4481/* */ 4482/* This function handles all the requests which operate on the list of */ 4483/* filter rules. This includes adding, deleting, insertion. It is also */ 4484/* responsible for creating groups when a "head" rule is loaded. Interface */ 4485/* names are resolved here and other sanity checks are made on the content */ 4486/* of the rule structure being loaded. If a rule has user defined timeouts */ 4487/* then make sure they are created and initialised before exiting. */ 4488/* ------------------------------------------------------------------------ */ 4489int 4490frrequest(softc, unit, req, data, set, makecopy) 4491 ipf_main_softc_t *softc; 4492 int unit; 4493 ioctlcmd_t req; 4494 int set, makecopy; 4495 caddr_t data; 4496{ 4497 int error = 0, in, family, addrem, need_free = 0; 4498 frentry_t frd, *fp, *f, **fprev, **ftail; 4499 void *ptr, *uptr, *cptr; 4500 u_int *p, *pp; 4501 frgroup_t *fg; 4502 char *group; 4503 4504 ptr = NULL; 4505 cptr = NULL; 4506 fg = NULL; 4507 fp = &frd; 4508 if (makecopy != 0) { 4509 bzero(fp, sizeof(frd)); 4510 error = ipf_inobj(softc, data, NULL, fp, IPFOBJ_FRENTRY); 4511 if (error) { 4512 return error; 4513 } 4514 if ((fp->fr_type & FR_T_BUILTIN) != 0) { 4515 IPFERROR(6); 4516 return EINVAL; 4517 } 4518 KMALLOCS(f, frentry_t *, fp->fr_size); 4519 if (f == NULL) { 4520 IPFERROR(131); 4521 return ENOMEM; 4522 } 4523 bzero(f, fp->fr_size); 4524 error = ipf_inobjsz(softc, data, f, IPFOBJ_FRENTRY, 4525 fp->fr_size); 4526 if (error) { 4527 KFREES(f, fp->fr_size); 4528 return error; 4529 } 4530 4531 fp = f; 4532 f = NULL; 4533 fp->fr_next = NULL; 4534 fp->fr_dnext = NULL; 4535 fp->fr_pnext = NULL; 4536 fp->fr_pdnext = NULL; 4537 fp->fr_grp = NULL; 4538 fp->fr_grphead = NULL; 4539 fp->fr_icmpgrp = NULL; 4540 fp->fr_isc = (void *)-1; 4541 fp->fr_ptr = NULL; 4542 fp->fr_ref = 0; 4543 fp->fr_flags |= FR_COPIED; 4544 } else { 4545 fp = (frentry_t *)data; 4546 if ((fp->fr_type & FR_T_BUILTIN) == 0) { 4547 IPFERROR(7); 4548 return EINVAL; 4549 } 4550 fp->fr_flags &= ~FR_COPIED; 4551 } 4552 4553 if (((fp->fr_dsize == 0) && (fp->fr_data != NULL)) || 4554 ((fp->fr_dsize != 0) && (fp->fr_data == NULL))) { 4555 IPFERROR(8); 4556 error = EINVAL; 4557 goto donenolock; 4558 } 4559 4560 family = fp->fr_family; 4561 uptr = fp->fr_data; 4562 4563 if (req == (ioctlcmd_t)SIOCINAFR || req == (ioctlcmd_t)SIOCINIFR || 4564 req == (ioctlcmd_t)SIOCADAFR || req == (ioctlcmd_t)SIOCADIFR) 4565 addrem = 0; 4566 else if (req == (ioctlcmd_t)SIOCRMAFR || req == (ioctlcmd_t)SIOCRMIFR) 4567 addrem = 1; 4568 else if (req == (ioctlcmd_t)SIOCZRLST) 4569 addrem = 2; 4570 else { 4571 IPFERROR(9); 4572 error = EINVAL; 4573 goto donenolock; 4574 } 4575 4576 /* 4577 * Only filter rules for IPv4 or IPv6 are accepted. 4578 */ 4579 if (family == AF_INET) { 4580 /*EMPTY*/; 4581#ifdef USE_INET6 4582 } else if (family == AF_INET6) { 4583 /*EMPTY*/; 4584#endif 4585 } else if (family != 0) { 4586 IPFERROR(10); 4587 error = EINVAL; 4588 goto donenolock; 4589 } 4590 4591 /* 4592 * If the rule is being loaded from user space, i.e. we had to copy it 4593 * into kernel space, then do not trust the function pointer in the 4594 * rule. 4595 */ 4596 if ((makecopy == 1) && (fp->fr_func != NULL)) { 4597 if (ipf_findfunc(fp->fr_func) == NULL) { 4598 IPFERROR(11); 4599 error = ESRCH; 4600 goto donenolock; 4601 } 4602 4603 if (addrem == 0) { 4604 error = ipf_funcinit(softc, fp); 4605 if (error != 0) 4606 goto donenolock; 4607 } 4608 } 4609 if ((fp->fr_flags & FR_CALLNOW) && 4610 ((fp->fr_func == NULL) || (fp->fr_func == (ipfunc_t)-1))) { 4611 IPFERROR(142); 4612 error = ESRCH; 4613 goto donenolock; 4614 } 4615 if (((fp->fr_flags & FR_CMDMASK) == FR_CALL) && 4616 ((fp->fr_func == NULL) || (fp->fr_func == (ipfunc_t)-1))) { 4617 IPFERROR(143); 4618 error = ESRCH; 4619 goto donenolock; 4620 } 4621 4622 ptr = NULL; 4623 cptr = NULL; 4624 4625 if (FR_ISACCOUNT(fp->fr_flags)) 4626 unit = IPL_LOGCOUNT; 4627 4628 /* 4629 * Check that each group name in the rule has a start index that 4630 * is valid. 4631 */ 4632 if (fp->fr_icmphead != -1) { 4633 if ((fp->fr_icmphead < 0) || 4634 (fp->fr_icmphead >= fp->fr_namelen)) { 4635 IPFERROR(136); 4636 error = EINVAL; 4637 goto donenolock; 4638 } 4639 if (!strcmp(FR_NAME(fp, fr_icmphead), "0")) 4640 fp->fr_names[fp->fr_icmphead] = '\0'; 4641 } 4642 4643 if (fp->fr_grhead != -1) { 4644 if ((fp->fr_grhead < 0) || 4645 (fp->fr_grhead >= fp->fr_namelen)) { 4646 IPFERROR(137); 4647 error = EINVAL; 4648 goto donenolock; 4649 } 4650 if (!strcmp(FR_NAME(fp, fr_grhead), "0")) 4651 fp->fr_names[fp->fr_grhead] = '\0'; 4652 } 4653 4654 if (fp->fr_group != -1) { 4655 if ((fp->fr_group < 0) || 4656 (fp->fr_group >= fp->fr_namelen)) { 4657 IPFERROR(138); 4658 error = EINVAL; 4659 goto donenolock; 4660 } 4661 if ((req != (int)SIOCZRLST) && (fp->fr_group != -1)) { 4662 /* 4663 * Allow loading rules that are in groups to cause 4664 * them to be created if they don't already exit. 4665 */ 4666 group = FR_NAME(fp, fr_group); 4667 if (addrem == 0) { 4668 fg = ipf_group_add(softc, group, NULL, 4669 fp->fr_flags, unit, set); 4670 fp->fr_grp = fg; 4671 } else { 4672 fg = ipf_findgroup(softc, group, unit, 4673 set, NULL); 4674 if (fg == NULL) { 4675 IPFERROR(12); 4676 error = ESRCH; 4677 goto donenolock; 4678 } 4679 } 4680 4681 if (fg->fg_flags == 0) { 4682 fg->fg_flags = fp->fr_flags & FR_INOUT; 4683 } else if (fg->fg_flags != (fp->fr_flags & FR_INOUT)) { 4684 IPFERROR(13); 4685 error = ESRCH; 4686 goto donenolock; 4687 } 4688 } 4689 } else { 4690 /* 4691 * If a rule is going to be part of a group then it does 4692 * not matter whether it is an in or out rule, but if it 4693 * isn't in a group, then it does... 4694 */ 4695 if ((fp->fr_flags & (FR_INQUE|FR_OUTQUE)) == 0) { 4696 IPFERROR(14); 4697 error = EINVAL; 4698 goto donenolock; 4699 } 4700 } 4701 in = (fp->fr_flags & FR_INQUE) ? 0 : 1; 4702 4703 /* 4704 * Work out which rule list this change is being applied to. 4705 */ 4706 ftail = NULL; 4707 fprev = NULL; 4708 if (unit == IPL_LOGAUTH) { 4709 if ((fp->fr_tifs[0].fd_ptr != NULL) || 4710 (fp->fr_tifs[1].fd_ptr != NULL) || 4711 (fp->fr_dif.fd_ptr != NULL) || 4712 (fp->fr_flags & FR_FASTROUTE)) { 4713 softc->ipf_interror = 145; 4714 error = EINVAL; 4715 goto donenolock; 4716 } 4717 fprev = ipf_auth_rulehead(softc); 4718 } else { 4719 if (FR_ISACCOUNT(fp->fr_flags)) 4720 fprev = &softc->ipf_acct[in][set]; 4721 else if ((fp->fr_flags & (FR_OUTQUE|FR_INQUE)) != 0) 4722 fprev = &softc->ipf_rules[in][set]; 4723 } 4724 if (fprev == NULL) { 4725 IPFERROR(15); 4726 error = ESRCH; 4727 goto donenolock; 4728 } 4729 4730 if (fg != NULL) 4731 fprev = &fg->fg_start; 4732 4733 /* 4734 * Copy in extra data for the rule. 4735 */ 4736 if (fp->fr_dsize != 0) { 4737 if (makecopy != 0) { 4738 KMALLOCS(ptr, void *, fp->fr_dsize); 4739 if (ptr == NULL) { 4740 IPFERROR(16); 4741 error = ENOMEM; 4742 goto donenolock; 4743 } 4744 4745 /* 4746 * The bcopy case is for when the data is appended 4747 * to the rule by ipf_in_compat(). 4748 */ 4749 if (uptr >= (void *)fp && 4750 uptr < (void *)((char *)fp + fp->fr_size)) { 4751 bcopy(uptr, ptr, fp->fr_dsize); 4752 error = 0; 4753 } else { 4754 error = COPYIN(uptr, ptr, fp->fr_dsize); 4755 if (error != 0) { 4756 IPFERROR(17); 4757 error = EFAULT; 4758 goto donenolock; 4759 } 4760 } 4761 } else { 4762 ptr = uptr; 4763 } 4764 fp->fr_data = ptr; 4765 } else { 4766 fp->fr_data = NULL; 4767 } 4768 4769 /* 4770 * Perform per-rule type sanity checks of their members. 4771 * All code after this needs to be aware that allocated memory 4772 * may need to be free'd before exiting. 4773 */ 4774 switch (fp->fr_type & ~FR_T_BUILTIN) 4775 { 4776#if defined(IPFILTER_BPF) 4777 case FR_T_BPFOPC : 4778 if (fp->fr_dsize == 0) { 4779 IPFERROR(19); 4780 error = EINVAL; 4781 break; 4782 } 4783 if (!bpf_validate(ptr, fp->fr_dsize/sizeof(struct bpf_insn))) { 4784 IPFERROR(20); 4785 error = EINVAL; 4786 break; 4787 } 4788 break; 4789#endif 4790 case FR_T_IPF : 4791 /* 4792 * Preparation for error case at the bottom of this function. 4793 */ 4794 if (fp->fr_datype == FRI_LOOKUP) 4795 fp->fr_dstptr = NULL; 4796 if (fp->fr_satype == FRI_LOOKUP) 4797 fp->fr_srcptr = NULL; 4798 4799 if (fp->fr_dsize != sizeof(fripf_t)) { 4800 IPFERROR(21); 4801 error = EINVAL; 4802 break; 4803 } 4804 4805 /* 4806 * Allowing a rule with both "keep state" and "with oow" is 4807 * pointless because adding a state entry to the table will 4808 * fail with the out of window (oow) flag set. 4809 */ 4810 if ((fp->fr_flags & FR_KEEPSTATE) && (fp->fr_flx & FI_OOW)) { 4811 IPFERROR(22); 4812 error = EINVAL; 4813 break; 4814 } 4815 4816 switch (fp->fr_satype) 4817 { 4818 case FRI_BROADCAST : 4819 case FRI_DYNAMIC : 4820 case FRI_NETWORK : 4821 case FRI_NETMASKED : 4822 case FRI_PEERADDR : 4823 if (fp->fr_sifpidx < 0) { 4824 IPFERROR(23); 4825 error = EINVAL; 4826 } 4827 break; 4828 case FRI_LOOKUP : 4829 fp->fr_srcptr = ipf_findlookup(softc, unit, fp, 4830 &fp->fr_src6, 4831 &fp->fr_smsk6); 4832 if (fp->fr_srcfunc == NULL) { 4833 IPFERROR(132); 4834 error = ESRCH; 4835 break; 4836 } 4837 break; 4838 case FRI_NORMAL : 4839 break; 4840 default : 4841 IPFERROR(133); 4842 error = EINVAL; 4843 break; 4844 } 4845 if (error != 0) 4846 break; 4847 4848 switch (fp->fr_datype) 4849 { 4850 case FRI_BROADCAST : 4851 case FRI_DYNAMIC : 4852 case FRI_NETWORK : 4853 case FRI_NETMASKED : 4854 case FRI_PEERADDR : 4855 if (fp->fr_difpidx < 0) { 4856 IPFERROR(24); 4857 error = EINVAL; 4858 } 4859 break; 4860 case FRI_LOOKUP : 4861 fp->fr_dstptr = ipf_findlookup(softc, unit, fp, 4862 &fp->fr_dst6, 4863 &fp->fr_dmsk6); 4864 if (fp->fr_dstfunc == NULL) { 4865 IPFERROR(134); 4866 error = ESRCH; 4867 } 4868 break; 4869 case FRI_NORMAL : 4870 break; 4871 default : 4872 IPFERROR(135); 4873 error = EINVAL; 4874 } 4875 break; 4876 4877 case FR_T_NONE : 4878 case FR_T_CALLFUNC : 4879 case FR_T_COMPIPF : 4880 break; 4881 4882 case FR_T_IPFEXPR : 4883 if (ipf_matcharray_verify(fp->fr_data, fp->fr_dsize) == -1) { 4884 IPFERROR(25); 4885 error = EINVAL; 4886 } 4887 break; 4888 4889 default : 4890 IPFERROR(26); 4891 error = EINVAL; 4892 break; 4893 } 4894 if (error != 0) 4895 goto donenolock; 4896 4897 if (fp->fr_tif.fd_name != -1) { 4898 if ((fp->fr_tif.fd_name < 0) || 4899 (fp->fr_tif.fd_name >= fp->fr_namelen)) { 4900 IPFERROR(139); 4901 error = EINVAL; 4902 goto donenolock; 4903 } 4904 } 4905 4906 if (fp->fr_dif.fd_name != -1) { 4907 if ((fp->fr_dif.fd_name < 0) || 4908 (fp->fr_dif.fd_name >= fp->fr_namelen)) { 4909 IPFERROR(140); 4910 error = EINVAL; 4911 goto donenolock; 4912 } 4913 } 4914 4915 if (fp->fr_rif.fd_name != -1) { 4916 if ((fp->fr_rif.fd_name < 0) || 4917 (fp->fr_rif.fd_name >= fp->fr_namelen)) { 4918 IPFERROR(141); 4919 error = EINVAL; 4920 goto donenolock; 4921 } 4922 } 4923 4924 /* 4925 * Lookup all the interface names that are part of the rule. 4926 */ 4927 error = ipf_synclist(softc, fp, NULL); 4928 if (error != 0) 4929 goto donenolock; 4930 fp->fr_statecnt = 0; 4931 if (fp->fr_srctrack.ht_max_nodes != 0) 4932 ipf_rb_ht_init(&fp->fr_srctrack); 4933 4934 /* 4935 * Look for an existing matching filter rule, but don't include the 4936 * next or interface pointer in the comparison (fr_next, fr_ifa). 4937 * This elminates rules which are indentical being loaded. Checksum 4938 * the constant part of the filter rule to make comparisons quicker 4939 * (this meaning no pointers are included). 4940 */ 4941 for (fp->fr_cksum = 0, p = (u_int *)&fp->fr_func, pp = &fp->fr_cksum; 4942 p < pp; p++) 4943 fp->fr_cksum += *p; 4944 pp = (u_int *)(fp->fr_caddr + fp->fr_dsize); 4945 for (p = (u_int *)fp->fr_data; p < pp; p++) 4946 fp->fr_cksum += *p; 4947 4948 WRITE_ENTER(&softc->ipf_mutex); 4949 4950 /* 4951 * Now that the filter rule lists are locked, we can walk the 4952 * chain of them without fear. 4953 */ 4954 ftail = fprev; 4955 for (f = *ftail; (f = *ftail) != NULL; ftail = &f->fr_next) { 4956 if (fp->fr_collect <= f->fr_collect) { 4957 ftail = fprev; 4958 f = NULL; 4959 break; 4960 } 4961 fprev = ftail; 4962 } 4963 4964 for (; (f = *ftail) != NULL; ftail = &f->fr_next) { 4965 if (ipf_rule_compare(fp, f) == 0) 4966 break; 4967 } 4968 4969 /* 4970 * If zero'ing statistics, copy current to caller and zero. 4971 */ 4972 if (addrem == 2) { 4973 if (f == NULL) { 4974 IPFERROR(27); 4975 error = ESRCH; 4976 } else { 4977 /* 4978 * Copy and reduce lock because of impending copyout. 4979 * Well we should, but if we do then the atomicity of 4980 * this call and the correctness of fr_hits and 4981 * fr_bytes cannot be guaranteed. As it is, this code 4982 * only resets them to 0 if they are successfully 4983 * copied out into user space. 4984 */ 4985 bcopy((char *)f, (char *)fp, f->fr_size); 4986 /* MUTEX_DOWNGRADE(&softc->ipf_mutex); */ 4987 4988 /* 4989 * When we copy this rule back out, set the data 4990 * pointer to be what it was in user space. 4991 */ 4992 fp->fr_data = uptr; 4993 error = ipf_outobj(softc, data, fp, IPFOBJ_FRENTRY); 4994 4995 if (error == 0) { 4996 if ((f->fr_dsize != 0) && (uptr != NULL)) 4997 error = COPYOUT(f->fr_data, uptr, 4998 f->fr_dsize); 4999 if (error != 0) { 5000 IPFERROR(28); 5001 error = EFAULT; 5002 } 5003 if (error == 0) { 5004 f->fr_hits = 0; 5005 f->fr_bytes = 0; 5006 } 5007 } 5008 } 5009 5010 if (makecopy != 0) { 5011 if (ptr != NULL) { 5012 KFREES(ptr, fp->fr_dsize); 5013 } 5014 KFREES(fp, fp->fr_size); 5015 } 5016 RWLOCK_EXIT(&softc->ipf_mutex); 5017 return error; 5018 } 5019 5020 if (!f) { 5021 /* 5022 * At the end of this, ftail must point to the place where the 5023 * new rule is to be saved/inserted/added. 5024 * For SIOCAD*FR, this should be the last rule in the group of 5025 * rules that have equal fr_collect fields. 5026 * For SIOCIN*FR, ... 5027 */ 5028 if (req == (ioctlcmd_t)SIOCADAFR || 5029 req == (ioctlcmd_t)SIOCADIFR) { 5030 5031 for (ftail = fprev; (f = *ftail) != NULL; ) { 5032 if (f->fr_collect > fp->fr_collect) 5033 break; 5034 ftail = &f->fr_next; 5035 fprev = ftail; 5036 } 5037 ftail = fprev; 5038 f = NULL; 5039 ptr = NULL; 5040 } else if (req == (ioctlcmd_t)SIOCINAFR || 5041 req == (ioctlcmd_t)SIOCINIFR) { 5042 while ((f = *fprev) != NULL) { 5043 if (f->fr_collect >= fp->fr_collect) 5044 break; 5045 fprev = &f->fr_next; 5046 } 5047 ftail = fprev; 5048 if (fp->fr_hits != 0) { 5049 while (fp->fr_hits && (f = *ftail)) { 5050 if (f->fr_collect != fp->fr_collect) 5051 break; 5052 fprev = ftail; 5053 ftail = &f->fr_next; 5054 fp->fr_hits--; 5055 } 5056 } 5057 f = NULL; 5058 ptr = NULL; 5059 } 5060 } 5061 5062 /* 5063 * Request to remove a rule. 5064 */ 5065 if (addrem == 1) { 5066 if (!f) { 5067 IPFERROR(29); 5068 error = ESRCH; 5069 } else { 5070 /* 5071 * Do not allow activity from user space to interfere 5072 * with rules not loaded that way. 5073 */ 5074 if ((makecopy == 1) && !(f->fr_flags & FR_COPIED)) { 5075 IPFERROR(30); 5076 error = EPERM; 5077 goto done; 5078 } 5079 5080 /* 5081 * Return EBUSY if the rule is being reference by 5082 * something else (eg state information.) 5083 */ 5084 if (f->fr_ref > 1) { 5085 IPFERROR(31); 5086 error = EBUSY; 5087 goto done; 5088 } 5089#ifdef IPFILTER_SCAN 5090 if (f->fr_isctag != -1 && 5091 (f->fr_isc != (struct ipscan *)-1)) 5092 ipf_scan_detachfr(f); 5093#endif 5094 5095 if (unit == IPL_LOGAUTH) { 5096 error = ipf_auth_precmd(softc, req, f, ftail); 5097 goto done; 5098 } 5099 5100 ipf_rule_delete(softc, f, unit, set); 5101 5102 need_free = makecopy; 5103 } 5104 } else { 5105 /* 5106 * Not removing, so we must be adding/inserting a rule. 5107 */ 5108 if (f != NULL) { 5109 IPFERROR(32); 5110 error = EEXIST; 5111 goto done; 5112 } 5113 if (unit == IPL_LOGAUTH) { 5114 error = ipf_auth_precmd(softc, req, fp, ftail); 5115 goto done; 5116 } 5117 5118 MUTEX_NUKE(&fp->fr_lock); 5119 MUTEX_INIT(&fp->fr_lock, "filter rule lock"); 5120 if (fp->fr_die != 0) 5121 ipf_rule_expire_insert(softc, fp, set); 5122 5123 fp->fr_hits = 0; 5124 if (makecopy != 0) 5125 fp->fr_ref = 1; 5126 fp->fr_pnext = ftail; 5127 fp->fr_next = *ftail; 5128 if (fp->fr_next != NULL) 5129 fp->fr_next->fr_pnext = &fp->fr_next; 5130 *ftail = fp; 5131 if (addrem == 0) 5132 ipf_fixskip(ftail, fp, 1); 5133 5134 fp->fr_icmpgrp = NULL; 5135 if (fp->fr_icmphead != -1) { 5136 group = FR_NAME(fp, fr_icmphead); 5137 fg = ipf_group_add(softc, group, fp, 0, unit, set); 5138 fp->fr_icmpgrp = fg; 5139 } 5140 5141 fp->fr_grphead = NULL; 5142 if (fp->fr_grhead != -1) { 5143 group = FR_NAME(fp, fr_grhead); 5144 fg = ipf_group_add(softc, group, fp, fp->fr_flags, 5145 unit, set); 5146 fp->fr_grphead = fg; 5147 } 5148 } 5149done: 5150 RWLOCK_EXIT(&softc->ipf_mutex); 5151donenolock: 5152 if (need_free || (error != 0)) { 5153 if ((fp->fr_type & ~FR_T_BUILTIN) == FR_T_IPF) { 5154 if ((fp->fr_satype == FRI_LOOKUP) && 5155 (fp->fr_srcptr != NULL)) 5156 ipf_lookup_deref(softc, fp->fr_srctype, 5157 fp->fr_srcptr); 5158 if ((fp->fr_datype == FRI_LOOKUP) && 5159 (fp->fr_dstptr != NULL)) 5160 ipf_lookup_deref(softc, fp->fr_dsttype, 5161 fp->fr_dstptr); 5162 } 5163 if (fp->fr_grp != NULL) { 5164 WRITE_ENTER(&softc->ipf_mutex); 5165 ipf_group_del(softc, fp->fr_grp, fp); 5166 RWLOCK_EXIT(&softc->ipf_mutex); 5167 } 5168 if ((ptr != NULL) && (makecopy != 0)) { 5169 KFREES(ptr, fp->fr_dsize); 5170 } 5171 KFREES(fp, fp->fr_size); 5172 } 5173 return (error); 5174} 5175 5176 5177/* ------------------------------------------------------------------------ */ 5178/* Function: ipf_rule_delete */ 5179/* Returns: Nil */ 5180/* Parameters: softc(I) - pointer to soft context main structure */ 5181/* f(I) - pointer to the rule being deleted */ 5182/* ftail(I) - pointer to the pointer to f */ 5183/* unit(I) - device for which this is for */ 5184/* set(I) - 1 or 0 (filter set) */ 5185/* */ 5186/* This function attempts to do what it can to delete a filter rule: remove */ 5187/* it from any linked lists and remove any groups it is responsible for. */ 5188/* But in the end, removing a rule can only drop the reference count - we */ 5189/* must use that as the guide for whether or not it can be freed. */ 5190/* ------------------------------------------------------------------------ */ 5191static void 5192ipf_rule_delete(softc, f, unit, set) 5193 ipf_main_softc_t *softc; 5194 frentry_t *f; 5195 int unit, set; 5196{ 5197 5198 /* 5199 * If fr_pdnext is set, then the rule is on the expire list, so 5200 * remove it from there. 5201 */ 5202 if (f->fr_pdnext != NULL) { 5203 *f->fr_pdnext = f->fr_dnext; 5204 if (f->fr_dnext != NULL) 5205 f->fr_dnext->fr_pdnext = f->fr_pdnext; 5206 f->fr_pdnext = NULL; 5207 f->fr_dnext = NULL; 5208 } 5209 5210 ipf_fixskip(f->fr_pnext, f, -1); 5211 if (f->fr_pnext != NULL) 5212 *f->fr_pnext = f->fr_next; 5213 if (f->fr_next != NULL) 5214 f->fr_next->fr_pnext = f->fr_pnext; 5215 f->fr_pnext = NULL; 5216 f->fr_next = NULL; 5217 5218 (void) ipf_derefrule(softc, &f); 5219} 5220 5221/* ------------------------------------------------------------------------ */ 5222/* Function: ipf_rule_expire_insert */ 5223/* Returns: Nil */ 5224/* Parameters: softc(I) - pointer to soft context main structure */ 5225/* f(I) - pointer to rule to be added to expire list */ 5226/* set(I) - 1 or 0 (filter set) */ 5227/* */ 5228/* If the new rule has a given expiration time, insert it into the list of */ 5229/* expiring rules with the ones to be removed first added to the front of */ 5230/* the list. The insertion is O(n) but it is kept sorted for quick scans at */ 5231/* expiration interval checks. */ 5232/* ------------------------------------------------------------------------ */ 5233static void 5234ipf_rule_expire_insert(softc, f, set) 5235 ipf_main_softc_t *softc; 5236 frentry_t *f; 5237 int set; 5238{ 5239 frentry_t *fr; 5240 5241 /* 5242 */ 5243 5244 f->fr_die = softc->ipf_ticks + IPF_TTLVAL(f->fr_die); 5245 for (fr = softc->ipf_rule_explist[set]; fr != NULL; 5246 fr = fr->fr_dnext) { 5247 if (f->fr_die < fr->fr_die) 5248 break; 5249 if (fr->fr_dnext == NULL) { 5250 /* 5251 * We've got to the last rule and everything 5252 * wanted to be expired before this new node, 5253 * so we have to tack it on the end... 5254 */ 5255 fr->fr_dnext = f; 5256 f->fr_pdnext = &fr->fr_dnext; 5257 fr = NULL; 5258 break; 5259 } 5260 } 5261 5262 if (softc->ipf_rule_explist[set] == NULL) { 5263 softc->ipf_rule_explist[set] = f; 5264 f->fr_pdnext = &softc->ipf_rule_explist[set]; 5265 } else if (fr != NULL) { 5266 f->fr_dnext = fr; 5267 f->fr_pdnext = fr->fr_pdnext; 5268 fr->fr_pdnext = &f->fr_dnext; 5269 } 5270} 5271 5272 5273/* ------------------------------------------------------------------------ */ 5274/* Function: ipf_findlookup */ 5275/* Returns: NULL = failure, else success */ 5276/* Parameters: softc(I) - pointer to soft context main structure */ 5277/* unit(I) - ipf device we want to find match for */ 5278/* fp(I) - rule for which lookup is for */ 5279/* addrp(I) - pointer to lookup information in address struct */ 5280/* maskp(O) - pointer to lookup information for storage */ 5281/* */ 5282/* When using pools and hash tables to store addresses for matching in */ 5283/* rules, it is necessary to resolve both the object referred to by the */ 5284/* name or address (and return that pointer) and also provide the means by */ 5285/* which to determine if an address belongs to that object to make the */ 5286/* packet matching quicker. */ 5287/* ------------------------------------------------------------------------ */ 5288static void * 5289ipf_findlookup(softc, unit, fr, addrp, maskp) 5290 ipf_main_softc_t *softc; 5291 int unit; 5292 frentry_t *fr; 5293 i6addr_t *addrp, *maskp; 5294{ 5295 void *ptr = NULL; 5296 5297 switch (addrp->iplookupsubtype) 5298 { 5299 case 0 : 5300 ptr = ipf_lookup_res_num(softc, unit, addrp->iplookuptype, 5301 addrp->iplookupnum, 5302 &maskp->iplookupfunc); 5303 break; 5304 case 1 : 5305 if (addrp->iplookupname < 0) 5306 break; 5307 if (addrp->iplookupname >= fr->fr_namelen) 5308 break; 5309 ptr = ipf_lookup_res_name(softc, unit, addrp->iplookuptype, 5310 fr->fr_names + addrp->iplookupname, 5311 &maskp->iplookupfunc); 5312 break; 5313 default : 5314 break; 5315 } 5316 5317 return ptr; 5318} 5319 5320 5321/* ------------------------------------------------------------------------ */ 5322/* Function: ipf_funcinit */ 5323/* Returns: int - 0 == success, else ESRCH: cannot resolve rule details */ 5324/* Parameters: softc(I) - pointer to soft context main structure */ 5325/* fr(I) - pointer to filter rule */ 5326/* */ 5327/* If a rule is a call rule, then check if the function it points to needs */ 5328/* an init function to be called now the rule has been loaded. */ 5329/* ------------------------------------------------------------------------ */ 5330static int 5331ipf_funcinit(softc, fr) 5332 ipf_main_softc_t *softc; 5333 frentry_t *fr; 5334{ 5335 ipfunc_resolve_t *ft; 5336 int err; 5337 5338 IPFERROR(34); 5339 err = ESRCH; 5340 5341 for (ft = ipf_availfuncs; ft->ipfu_addr != NULL; ft++) 5342 if (ft->ipfu_addr == fr->fr_func) { 5343 err = 0; 5344 if (ft->ipfu_init != NULL) 5345 err = (*ft->ipfu_init)(softc, fr); 5346 break; 5347 } 5348 return err; 5349} 5350 5351 5352/* ------------------------------------------------------------------------ */ 5353/* Function: ipf_funcfini */ 5354/* Returns: Nil */ 5355/* Parameters: softc(I) - pointer to soft context main structure */ 5356/* fr(I) - pointer to filter rule */ 5357/* */ 5358/* For a given filter rule, call the matching "fini" function if the rule */ 5359/* is using a known function that would have resulted in the "init" being */ 5360/* called for ealier. */ 5361/* ------------------------------------------------------------------------ */ 5362static void 5363ipf_funcfini(softc, fr) 5364 ipf_main_softc_t *softc; 5365 frentry_t *fr; 5366{ 5367 ipfunc_resolve_t *ft; 5368 5369 for (ft = ipf_availfuncs; ft->ipfu_addr != NULL; ft++) 5370 if (ft->ipfu_addr == fr->fr_func) { 5371 if (ft->ipfu_fini != NULL) 5372 (void) (*ft->ipfu_fini)(softc, fr); 5373 break; 5374 } 5375} 5376 5377 5378/* ------------------------------------------------------------------------ */ 5379/* Function: ipf_findfunc */ 5380/* Returns: ipfunc_t - pointer to function if found, else NULL */ 5381/* Parameters: funcptr(I) - function pointer to lookup */ 5382/* */ 5383/* Look for a function in the table of known functions. */ 5384/* ------------------------------------------------------------------------ */ 5385static ipfunc_t 5386ipf_findfunc(funcptr) 5387 ipfunc_t funcptr; 5388{ 5389 ipfunc_resolve_t *ft; 5390 5391 for (ft = ipf_availfuncs; ft->ipfu_addr != NULL; ft++) 5392 if (ft->ipfu_addr == funcptr) 5393 return funcptr; 5394 return NULL; 5395} 5396 5397 5398/* ------------------------------------------------------------------------ */ 5399/* Function: ipf_resolvefunc */ 5400/* Returns: int - 0 == success, else error */ 5401/* Parameters: data(IO) - ioctl data pointer to ipfunc_resolve_t struct */ 5402/* */ 5403/* Copy in a ipfunc_resolve_t structure and then fill in the missing field. */ 5404/* This will either be the function name (if the pointer is set) or the */ 5405/* function pointer if the name is set. When found, fill in the other one */ 5406/* so that the entire, complete, structure can be copied back to user space.*/ 5407/* ------------------------------------------------------------------------ */ 5408int 5409ipf_resolvefunc(softc, data) 5410 ipf_main_softc_t *softc; 5411 void *data; 5412{ 5413 ipfunc_resolve_t res, *ft; 5414 int error; 5415 5416 error = BCOPYIN(data, &res, sizeof(res)); 5417 if (error != 0) { 5418 IPFERROR(123); 5419 return EFAULT; 5420 } 5421 5422 if (res.ipfu_addr == NULL && res.ipfu_name[0] != '\0') { 5423 for (ft = ipf_availfuncs; ft->ipfu_addr != NULL; ft++) 5424 if (strncmp(res.ipfu_name, ft->ipfu_name, 5425 sizeof(res.ipfu_name)) == 0) { 5426 res.ipfu_addr = ft->ipfu_addr; 5427 res.ipfu_init = ft->ipfu_init; 5428 if (COPYOUT(&res, data, sizeof(res)) != 0) { 5429 IPFERROR(35); 5430 return EFAULT; 5431 } 5432 return 0; 5433 } 5434 } 5435 if (res.ipfu_addr != NULL && res.ipfu_name[0] == '\0') { 5436 for (ft = ipf_availfuncs; ft->ipfu_addr != NULL; ft++) 5437 if (ft->ipfu_addr == res.ipfu_addr) { 5438 (void) strncpy(res.ipfu_name, ft->ipfu_name, 5439 sizeof(res.ipfu_name)); 5440 res.ipfu_init = ft->ipfu_init; 5441 if (COPYOUT(&res, data, sizeof(res)) != 0) { 5442 IPFERROR(36); 5443 return EFAULT; 5444 } 5445 return 0; 5446 } 5447 } 5448 IPFERROR(37); 5449 return ESRCH; 5450} 5451 5452 5453#if !defined(_KERNEL) || (!defined(__NetBSD__) && !defined(__OpenBSD__) && \ 5454 !defined(__FreeBSD__)) || \ 5455 FREEBSD_LT_REV(501000) || NETBSD_LT_REV(105000000) || \ 5456 OPENBSD_LT_REV(200006) 5457/* 5458 * From: NetBSD 5459 * ppsratecheck(): packets (or events) per second limitation. 5460 */ 5461int 5462ppsratecheck(lasttime, curpps, maxpps) 5463 struct timeval *lasttime; 5464 int *curpps; 5465 int maxpps; /* maximum pps allowed */ 5466{ 5467 struct timeval tv, delta; 5468 int rv; 5469 5470 GETKTIME(&tv); 5471 5472 delta.tv_sec = tv.tv_sec - lasttime->tv_sec; 5473 delta.tv_usec = tv.tv_usec - lasttime->tv_usec; 5474 if (delta.tv_usec < 0) { 5475 delta.tv_sec--; 5476 delta.tv_usec += 1000000; 5477 } 5478 5479 /* 5480 * check for 0,0 is so that the message will be seen at least once. 5481 * if more than one second have passed since the last update of 5482 * lasttime, reset the counter. 5483 * 5484 * we do increment *curpps even in *curpps < maxpps case, as some may 5485 * try to use *curpps for stat purposes as well. 5486 */ 5487 if ((lasttime->tv_sec == 0 && lasttime->tv_usec == 0) || 5488 delta.tv_sec >= 1) { 5489 *lasttime = tv; 5490 *curpps = 0; 5491 rv = 1; 5492 } else if (maxpps < 0) 5493 rv = 1; 5494 else if (*curpps < maxpps) 5495 rv = 1; 5496 else 5497 rv = 0; 5498 *curpps = *curpps + 1; 5499 5500 return (rv); 5501} 5502#endif 5503 5504 5505/* ------------------------------------------------------------------------ */ 5506/* Function: ipf_derefrule */ 5507/* Returns: int - 0 == rule freed up, else rule not freed */ 5508/* Parameters: fr(I) - pointer to filter rule */ 5509/* */ 5510/* Decrement the reference counter to a rule by one. If it reaches zero, */ 5511/* free it and any associated storage space being used by it. */ 5512/* ------------------------------------------------------------------------ */ 5513int 5514ipf_derefrule(softc, frp) 5515 ipf_main_softc_t *softc; 5516 frentry_t **frp; 5517{ 5518 frentry_t *fr; 5519 frdest_t *fdp; 5520 5521 fr = *frp; 5522 *frp = NULL; 5523 5524 MUTEX_ENTER(&fr->fr_lock); 5525 fr->fr_ref--; 5526 if (fr->fr_ref == 0) { 5527 MUTEX_EXIT(&fr->fr_lock); 5528 MUTEX_DESTROY(&fr->fr_lock); 5529 5530 ipf_funcfini(softc, fr); 5531 5532 fdp = &fr->fr_tif; 5533 if (fdp->fd_type == FRD_DSTLIST) 5534 ipf_lookup_deref(softc, IPLT_DSTLIST, fdp->fd_ptr); 5535 5536 fdp = &fr->fr_rif; 5537 if (fdp->fd_type == FRD_DSTLIST) 5538 ipf_lookup_deref(softc, IPLT_DSTLIST, fdp->fd_ptr); 5539 5540 fdp = &fr->fr_dif; 5541 if (fdp->fd_type == FRD_DSTLIST) 5542 ipf_lookup_deref(softc, IPLT_DSTLIST, fdp->fd_ptr); 5543 5544 if ((fr->fr_type & ~FR_T_BUILTIN) == FR_T_IPF && 5545 fr->fr_satype == FRI_LOOKUP) 5546 ipf_lookup_deref(softc, fr->fr_srctype, fr->fr_srcptr); 5547 if ((fr->fr_type & ~FR_T_BUILTIN) == FR_T_IPF && 5548 fr->fr_datype == FRI_LOOKUP) 5549 ipf_lookup_deref(softc, fr->fr_dsttype, fr->fr_dstptr); 5550 5551 if (fr->fr_grp != NULL) 5552 ipf_group_del(softc, fr->fr_grp, fr); 5553 5554 if (fr->fr_grphead != NULL) 5555 ipf_group_del(softc, fr->fr_grphead, fr); 5556 5557 if (fr->fr_icmpgrp != NULL) 5558 ipf_group_del(softc, fr->fr_icmpgrp, fr); 5559 5560 if ((fr->fr_flags & FR_COPIED) != 0) { 5561 if (fr->fr_dsize) { 5562 KFREES(fr->fr_data, fr->fr_dsize); 5563 } 5564 KFREES(fr, fr->fr_size); 5565 return 0; 5566 } 5567 return 1; 5568 } else { 5569 MUTEX_EXIT(&fr->fr_lock); 5570 } 5571 return -1; 5572} 5573 5574 5575/* ------------------------------------------------------------------------ */ 5576/* Function: ipf_grpmapinit */ 5577/* Returns: int - 0 == success, else ESRCH because table entry not found*/ 5578/* Parameters: fr(I) - pointer to rule to find hash table for */ 5579/* */ 5580/* Looks for group hash table fr_arg and stores a pointer to it in fr_ptr. */ 5581/* fr_ptr is later used by ipf_srcgrpmap and ipf_dstgrpmap. */ 5582/* ------------------------------------------------------------------------ */ 5583static int 5584ipf_grpmapinit(softc, fr) 5585 ipf_main_softc_t *softc; 5586 frentry_t *fr; 5587{ 5588 char name[FR_GROUPLEN]; 5589 iphtable_t *iph; 5590 5591#if defined(SNPRINTF) && defined(_KERNEL) 5592 SNPRINTF(name, sizeof(name), "%d", fr->fr_arg); 5593#else 5594 (void) sprintf(name, "%d", fr->fr_arg); 5595#endif 5596 iph = ipf_lookup_find_htable(softc, IPL_LOGIPF, name); 5597 if (iph == NULL) { 5598 IPFERROR(38); 5599 return ESRCH; 5600 } 5601 if ((iph->iph_flags & FR_INOUT) != (fr->fr_flags & FR_INOUT)) { 5602 IPFERROR(39); 5603 return ESRCH; 5604 } 5605 iph->iph_ref++; 5606 fr->fr_ptr = iph; 5607 return 0; 5608} 5609 5610 5611/* ------------------------------------------------------------------------ */ 5612/* Function: ipf_grpmapfini */ 5613/* Returns: int - 0 == success, else ESRCH because table entry not found*/ 5614/* Parameters: softc(I) - pointer to soft context main structure */ 5615/* fr(I) - pointer to rule to release hash table for */ 5616/* */ 5617/* For rules that have had ipf_grpmapinit called, ipf_lookup_deref needs to */ 5618/* be called to undo what ipf_grpmapinit caused to be done. */ 5619/* ------------------------------------------------------------------------ */ 5620static int 5621ipf_grpmapfini(softc, fr) 5622 ipf_main_softc_t *softc; 5623 frentry_t *fr; 5624{ 5625 iphtable_t *iph; 5626 iph = fr->fr_ptr; 5627 if (iph != NULL) 5628 ipf_lookup_deref(softc, IPLT_HASH, iph); 5629 return 0; 5630} 5631 5632 5633/* ------------------------------------------------------------------------ */ 5634/* Function: ipf_srcgrpmap */ 5635/* Returns: frentry_t * - pointer to "new last matching" rule or NULL */ 5636/* Parameters: fin(I) - pointer to packet information */ 5637/* passp(IO) - pointer to current/new filter decision (unused) */ 5638/* */ 5639/* Look for a rule group head in a hash table, using the source address as */ 5640/* the key, and descend into that group and continue matching rules against */ 5641/* the packet. */ 5642/* ------------------------------------------------------------------------ */ 5643frentry_t * 5644ipf_srcgrpmap(fin, passp) 5645 fr_info_t *fin; 5646 u_32_t *passp; 5647{ 5648 frgroup_t *fg; 5649 void *rval; 5650 5651 rval = ipf_iphmfindgroup(fin->fin_main_soft, fin->fin_fr->fr_ptr, 5652 &fin->fin_src); 5653 if (rval == NULL) 5654 return NULL; 5655 5656 fg = rval; 5657 fin->fin_fr = fg->fg_start; 5658 (void) ipf_scanlist(fin, *passp); 5659 return fin->fin_fr; 5660} 5661 5662 5663/* ------------------------------------------------------------------------ */ 5664/* Function: ipf_dstgrpmap */ 5665/* Returns: frentry_t * - pointer to "new last matching" rule or NULL */ 5666/* Parameters: fin(I) - pointer to packet information */ 5667/* passp(IO) - pointer to current/new filter decision (unused) */ 5668/* */ 5669/* Look for a rule group head in a hash table, using the destination */ 5670/* address as the key, and descend into that group and continue matching */ 5671/* rules against the packet. */ 5672/* ------------------------------------------------------------------------ */ 5673frentry_t * 5674ipf_dstgrpmap(fin, passp) 5675 fr_info_t *fin; 5676 u_32_t *passp; 5677{ 5678 frgroup_t *fg; 5679 void *rval; 5680 5681 rval = ipf_iphmfindgroup(fin->fin_main_soft, fin->fin_fr->fr_ptr, 5682 &fin->fin_dst); 5683 if (rval == NULL) 5684 return NULL; 5685 5686 fg = rval; 5687 fin->fin_fr = fg->fg_start; 5688 (void) ipf_scanlist(fin, *passp); 5689 return fin->fin_fr; 5690} 5691 5692/* 5693 * Queue functions 5694 * =============== 5695 * These functions manage objects on queues for efficient timeouts. There 5696 * are a number of system defined queues as well as user defined timeouts. 5697 * It is expected that a lock is held in the domain in which the queue 5698 * belongs (i.e. either state or NAT) when calling any of these functions 5699 * that prevents ipf_freetimeoutqueue() from being called at the same time 5700 * as any other. 5701 */ 5702 5703 5704/* ------------------------------------------------------------------------ */ 5705/* Function: ipf_addtimeoutqueue */ 5706/* Returns: struct ifqtq * - NULL if malloc fails, else pointer to */ 5707/* timeout queue with given interval. */ 5708/* Parameters: parent(I) - pointer to pointer to parent node of this list */ 5709/* of interface queues. */ 5710/* seconds(I) - timeout value in seconds for this queue. */ 5711/* */ 5712/* This routine first looks for a timeout queue that matches the interval */ 5713/* being requested. If it finds one, increments the reference counter and */ 5714/* returns a pointer to it. If none are found, it allocates a new one and */ 5715/* inserts it at the top of the list. */ 5716/* */ 5717/* Locking. */ 5718/* It is assumed that the caller of this function has an appropriate lock */ 5719/* held (exclusively) in the domain that encompases 'parent'. */ 5720/* ------------------------------------------------------------------------ */ 5721ipftq_t * 5722ipf_addtimeoutqueue(softc, parent, seconds) 5723 ipf_main_softc_t *softc; 5724 ipftq_t **parent; 5725 u_int seconds; 5726{ 5727 ipftq_t *ifq; 5728 u_int period; 5729 5730 period = seconds * IPF_HZ_DIVIDE; 5731 5732 MUTEX_ENTER(&softc->ipf_timeoutlock); 5733 for (ifq = *parent; ifq != NULL; ifq = ifq->ifq_next) { 5734 if (ifq->ifq_ttl == period) { 5735 /* 5736 * Reset the delete flag, if set, so the structure 5737 * gets reused rather than freed and reallocated. 5738 */ 5739 MUTEX_ENTER(&ifq->ifq_lock); 5740 ifq->ifq_flags &= ~IFQF_DELETE; 5741 ifq->ifq_ref++; 5742 MUTEX_EXIT(&ifq->ifq_lock); 5743 MUTEX_EXIT(&softc->ipf_timeoutlock); 5744 5745 return ifq; 5746 } 5747 } 5748 5749 KMALLOC(ifq, ipftq_t *); 5750 if (ifq != NULL) { 5751 MUTEX_NUKE(&ifq->ifq_lock); 5752 IPFTQ_INIT(ifq, period, "ipftq mutex"); 5753 ifq->ifq_next = *parent; 5754 ifq->ifq_pnext = parent; 5755 ifq->ifq_flags = IFQF_USER; 5756 ifq->ifq_ref++; 5757 *parent = ifq; 5758 softc->ipf_userifqs++; 5759 } 5760 MUTEX_EXIT(&softc->ipf_timeoutlock); 5761 return ifq; 5762} 5763 5764 5765/* ------------------------------------------------------------------------ */ 5766/* Function: ipf_deletetimeoutqueue */ 5767/* Returns: int - new reference count value of the timeout queue */ 5768/* Parameters: ifq(I) - timeout queue which is losing a reference. */ 5769/* Locks: ifq->ifq_lock */ 5770/* */ 5771/* This routine must be called when we're discarding a pointer to a timeout */ 5772/* queue object, taking care of the reference counter. */ 5773/* */ 5774/* Now that this just sets a DELETE flag, it requires the expire code to */ 5775/* check the list of user defined timeout queues and call the free function */ 5776/* below (currently commented out) to stop memory leaking. It is done this */ 5777/* way because the locking may not be sufficient to safely do a free when */ 5778/* this function is called. */ 5779/* ------------------------------------------------------------------------ */ 5780int 5781ipf_deletetimeoutqueue(ifq) 5782 ipftq_t *ifq; 5783{ 5784 5785 ifq->ifq_ref--; 5786 if ((ifq->ifq_ref == 0) && ((ifq->ifq_flags & IFQF_USER) != 0)) { 5787 ifq->ifq_flags |= IFQF_DELETE; 5788 } 5789 5790 return ifq->ifq_ref; 5791} 5792 5793 5794/* ------------------------------------------------------------------------ */ 5795/* Function: ipf_freetimeoutqueue */ 5796/* Parameters: ifq(I) - timeout queue which is losing a reference. */ 5797/* Returns: Nil */ 5798/* */ 5799/* Locking: */ 5800/* It is assumed that the caller of this function has an appropriate lock */ 5801/* held (exclusively) in the domain that encompases the callers "domain". */ 5802/* The ifq_lock for this structure should not be held. */ 5803/* */ 5804/* Remove a user defined timeout queue from the list of queues it is in and */ 5805/* tidy up after this is done. */ 5806/* ------------------------------------------------------------------------ */ 5807void 5808ipf_freetimeoutqueue(softc, ifq) 5809 ipf_main_softc_t *softc; 5810 ipftq_t *ifq; 5811{ 5812 5813 if (((ifq->ifq_flags & IFQF_DELETE) == 0) || (ifq->ifq_ref != 0) || 5814 ((ifq->ifq_flags & IFQF_USER) == 0)) { 5815 printf("ipf_freetimeoutqueue(%lx) flags 0x%x ttl %d ref %d\n", 5816 (u_long)ifq, ifq->ifq_flags, ifq->ifq_ttl, 5817 ifq->ifq_ref); 5818 return; 5819 } 5820 5821 /* 5822 * Remove from its position in the list. 5823 */ 5824 *ifq->ifq_pnext = ifq->ifq_next; 5825 if (ifq->ifq_next != NULL) 5826 ifq->ifq_next->ifq_pnext = ifq->ifq_pnext; 5827 ifq->ifq_next = NULL; 5828 ifq->ifq_pnext = NULL; 5829 5830 MUTEX_DESTROY(&ifq->ifq_lock); 5831 ATOMIC_DEC(softc->ipf_userifqs); 5832 KFREE(ifq); 5833} 5834 5835 5836/* ------------------------------------------------------------------------ */ 5837/* Function: ipf_deletequeueentry */ 5838/* Returns: Nil */ 5839/* Parameters: tqe(I) - timeout queue entry to delete */ 5840/* */ 5841/* Remove a tail queue entry from its queue and make it an orphan. */ 5842/* ipf_deletetimeoutqueue is called to make sure the reference count on the */ 5843/* queue is correct. We can't, however, call ipf_freetimeoutqueue because */ 5844/* the correct lock(s) may not be held that would make it safe to do so. */ 5845/* ------------------------------------------------------------------------ */ 5846void 5847ipf_deletequeueentry(tqe) 5848 ipftqent_t *tqe; 5849{ 5850 ipftq_t *ifq; 5851 5852 ifq = tqe->tqe_ifq; 5853 5854 MUTEX_ENTER(&ifq->ifq_lock); 5855 5856 if (tqe->tqe_pnext != NULL) { 5857 *tqe->tqe_pnext = tqe->tqe_next; 5858 if (tqe->tqe_next != NULL) 5859 tqe->tqe_next->tqe_pnext = tqe->tqe_pnext; 5860 else /* we must be the tail anyway */ 5861 ifq->ifq_tail = tqe->tqe_pnext; 5862 5863 tqe->tqe_pnext = NULL; 5864 tqe->tqe_ifq = NULL; 5865 } 5866 5867 (void) ipf_deletetimeoutqueue(ifq); 5868 ASSERT(ifq->ifq_ref > 0); 5869 5870 MUTEX_EXIT(&ifq->ifq_lock); 5871} 5872 5873 5874/* ------------------------------------------------------------------------ */ 5875/* Function: ipf_queuefront */ 5876/* Returns: Nil */ 5877/* Parameters: tqe(I) - pointer to timeout queue entry */ 5878/* */ 5879/* Move a queue entry to the front of the queue, if it isn't already there. */ 5880/* ------------------------------------------------------------------------ */ 5881void 5882ipf_queuefront(tqe) 5883 ipftqent_t *tqe; 5884{ 5885 ipftq_t *ifq; 5886 5887 ifq = tqe->tqe_ifq; 5888 if (ifq == NULL) 5889 return; 5890 5891 MUTEX_ENTER(&ifq->ifq_lock); 5892 if (ifq->ifq_head != tqe) { 5893 *tqe->tqe_pnext = tqe->tqe_next; 5894 if (tqe->tqe_next) 5895 tqe->tqe_next->tqe_pnext = tqe->tqe_pnext; 5896 else 5897 ifq->ifq_tail = tqe->tqe_pnext; 5898 5899 tqe->tqe_next = ifq->ifq_head; 5900 ifq->ifq_head->tqe_pnext = &tqe->tqe_next; 5901 ifq->ifq_head = tqe; 5902 tqe->tqe_pnext = &ifq->ifq_head; 5903 } 5904 MUTEX_EXIT(&ifq->ifq_lock); 5905} 5906 5907 5908/* ------------------------------------------------------------------------ */ 5909/* Function: ipf_queueback */ 5910/* Returns: Nil */ 5911/* Parameters: ticks(I) - ipf tick time to use with this call */ 5912/* tqe(I) - pointer to timeout queue entry */ 5913/* */ 5914/* Move a queue entry to the back of the queue, if it isn't already there. */ 5915/* We use use ticks to calculate the expiration and mark for when we last */ 5916/* touched the structure. */ 5917/* ------------------------------------------------------------------------ */ 5918void 5919ipf_queueback(ticks, tqe) 5920 u_long ticks; 5921 ipftqent_t *tqe; 5922{ 5923 ipftq_t *ifq; 5924 5925 ifq = tqe->tqe_ifq; 5926 if (ifq == NULL) 5927 return; 5928 tqe->tqe_die = ticks + ifq->ifq_ttl; 5929 tqe->tqe_touched = ticks; 5930 5931 MUTEX_ENTER(&ifq->ifq_lock); 5932 if (tqe->tqe_next != NULL) { /* at the end already ? */ 5933 /* 5934 * Remove from list 5935 */ 5936 *tqe->tqe_pnext = tqe->tqe_next; 5937 tqe->tqe_next->tqe_pnext = tqe->tqe_pnext; 5938 5939 /* 5940 * Make it the last entry. 5941 */ 5942 tqe->tqe_next = NULL; 5943 tqe->tqe_pnext = ifq->ifq_tail; 5944 *ifq->ifq_tail = tqe; 5945 ifq->ifq_tail = &tqe->tqe_next; 5946 } 5947 MUTEX_EXIT(&ifq->ifq_lock); 5948} 5949 5950 5951/* ------------------------------------------------------------------------ */ 5952/* Function: ipf_queueappend */ 5953/* Returns: Nil */ 5954/* Parameters: ticks(I) - ipf tick time to use with this call */ 5955/* tqe(I) - pointer to timeout queue entry */ 5956/* ifq(I) - pointer to timeout queue */ 5957/* parent(I) - owing object pointer */ 5958/* */ 5959/* Add a new item to this queue and put it on the very end. */ 5960/* We use use ticks to calculate the expiration and mark for when we last */ 5961/* touched the structure. */ 5962/* ------------------------------------------------------------------------ */ 5963void 5964ipf_queueappend(ticks, tqe, ifq, parent) 5965 u_long ticks; 5966 ipftqent_t *tqe; 5967 ipftq_t *ifq; 5968 void *parent; 5969{ 5970 5971 MUTEX_ENTER(&ifq->ifq_lock); 5972 tqe->tqe_parent = parent; 5973 tqe->tqe_pnext = ifq->ifq_tail; 5974 *ifq->ifq_tail = tqe; 5975 ifq->ifq_tail = &tqe->tqe_next; 5976 tqe->tqe_next = NULL; 5977 tqe->tqe_ifq = ifq; 5978 tqe->tqe_die = ticks + ifq->ifq_ttl; 5979 tqe->tqe_touched = ticks; 5980 ifq->ifq_ref++; 5981 MUTEX_EXIT(&ifq->ifq_lock); 5982} 5983 5984 5985/* ------------------------------------------------------------------------ */ 5986/* Function: ipf_movequeue */ 5987/* Returns: Nil */ 5988/* Parameters: tq(I) - pointer to timeout queue information */ 5989/* oifp(I) - old timeout queue entry was on */ 5990/* nifp(I) - new timeout queue to put entry on */ 5991/* */ 5992/* Move a queue entry from one timeout queue to another timeout queue. */ 5993/* If it notices that the current entry is already last and does not need */ 5994/* to move queue, the return. */ 5995/* ------------------------------------------------------------------------ */ 5996void 5997ipf_movequeue(ticks, tqe, oifq, nifq) 5998 u_long ticks; 5999 ipftqent_t *tqe; 6000 ipftq_t *oifq, *nifq; 6001{ 6002 6003 /* 6004 * If the queue hasn't changed and we last touched this entry at the 6005 * same ipf time, then we're not going to achieve anything by either 6006 * changing the ttl or moving it on the queue. 6007 */ 6008 if (oifq == nifq && tqe->tqe_touched == ticks) 6009 return; 6010 6011 /* 6012 * For any of this to be outside the lock, there is a risk that two 6013 * packets entering simultaneously, with one changing to a different 6014 * queue and one not, could end up with things in a bizarre state. 6015 */ 6016 MUTEX_ENTER(&oifq->ifq_lock); 6017 6018 tqe->tqe_touched = ticks; 6019 tqe->tqe_die = ticks + nifq->ifq_ttl; 6020 /* 6021 * Is the operation here going to be a no-op ? 6022 */ 6023 if (oifq == nifq) { 6024 if ((tqe->tqe_next == NULL) || 6025 (tqe->tqe_next->tqe_die == tqe->tqe_die)) { 6026 MUTEX_EXIT(&oifq->ifq_lock); 6027 return; 6028 } 6029 } 6030 6031 /* 6032 * Remove from the old queue 6033 */ 6034 *tqe->tqe_pnext = tqe->tqe_next; 6035 if (tqe->tqe_next) 6036 tqe->tqe_next->tqe_pnext = tqe->tqe_pnext; 6037 else 6038 oifq->ifq_tail = tqe->tqe_pnext; 6039 tqe->tqe_next = NULL; 6040 6041 /* 6042 * If we're moving from one queue to another, release the 6043 * lock on the old queue and get a lock on the new queue. 6044 * For user defined queues, if we're moving off it, call 6045 * delete in case it can now be freed. 6046 */ 6047 if (oifq != nifq) { 6048 tqe->tqe_ifq = NULL; 6049 6050 (void) ipf_deletetimeoutqueue(oifq); 6051 6052 MUTEX_EXIT(&oifq->ifq_lock); 6053 6054 MUTEX_ENTER(&nifq->ifq_lock); 6055 6056 tqe->tqe_ifq = nifq; 6057 nifq->ifq_ref++; 6058 } 6059 6060 /* 6061 * Add to the bottom of the new queue 6062 */ 6063 tqe->tqe_pnext = nifq->ifq_tail; 6064 *nifq->ifq_tail = tqe; 6065 nifq->ifq_tail = &tqe->tqe_next; 6066 MUTEX_EXIT(&nifq->ifq_lock); 6067} 6068 6069 6070/* ------------------------------------------------------------------------ */ 6071/* Function: ipf_updateipid */ 6072/* Returns: int - 0 == success, -1 == error (packet should be droppped) */ 6073/* Parameters: fin(I) - pointer to packet information */ 6074/* */ 6075/* When we are doing NAT, change the IP of every packet to represent a */ 6076/* single sequence of packets coming from the host, hiding any host */ 6077/* specific sequencing that might otherwise be revealed. If the packet is */ 6078/* a fragment, then store the 'new' IPid in the fragment cache and look up */ 6079/* the fragment cache for non-leading fragments. If a non-leading fragment */ 6080/* has no match in the cache, return an error. */ 6081/* ------------------------------------------------------------------------ */ 6082static int 6083ipf_updateipid(fin) 6084 fr_info_t *fin; 6085{ 6086 u_short id, ido, sums; 6087 u_32_t sumd, sum; 6088 ip_t *ip; 6089 6090 if (fin->fin_off != 0) { 6091 sum = ipf_frag_ipidknown(fin); 6092 if (sum == 0xffffffff) 6093 return -1; 6094 sum &= 0xffff; 6095 id = (u_short)sum; 6096 } else { 6097 id = ipf_nextipid(fin); 6098 if (fin->fin_off == 0 && (fin->fin_flx & FI_FRAG) != 0) 6099 (void) ipf_frag_ipidnew(fin, (u_32_t)id); 6100 } 6101 6102 ip = fin->fin_ip; 6103 ido = ntohs(ip->ip_id); 6104 if (id == ido) 6105 return 0; 6106 ip->ip_id = htons(id); 6107 CALC_SUMD(ido, id, sumd); /* DESTRUCTIVE MACRO! id,ido change */ 6108 sum = (~ntohs(ip->ip_sum)) & 0xffff; 6109 sum += sumd; 6110 sum = (sum >> 16) + (sum & 0xffff); 6111 sum = (sum >> 16) + (sum & 0xffff); 6112 sums = ~(u_short)sum; 6113 ip->ip_sum = htons(sums); 6114 return 0; 6115} 6116 6117 6118#ifdef NEED_FRGETIFNAME 6119/* ------------------------------------------------------------------------ */ 6120/* Function: ipf_getifname */ 6121/* Returns: char * - pointer to interface name */ 6122/* Parameters: ifp(I) - pointer to network interface */ 6123/* buffer(O) - pointer to where to store interface name */ 6124/* */ 6125/* Constructs an interface name in the buffer passed. The buffer passed is */ 6126/* expected to be at least LIFNAMSIZ in bytes big. If buffer is passed in */ 6127/* as a NULL pointer then return a pointer to a static array. */ 6128/* ------------------------------------------------------------------------ */ 6129char * 6130ipf_getifname(ifp, buffer) 6131 struct ifnet *ifp; 6132 char *buffer; 6133{ 6134 static char namebuf[LIFNAMSIZ]; 6135# if defined(MENTAT) || defined(__FreeBSD__) || defined(__osf__) || \ 6136 defined(__sgi) || defined(linux) || defined(_AIX51) || \ 6137 (defined(sun) && !defined(__SVR4) && !defined(__svr4__)) 6138 int unit, space; 6139 char temp[20]; 6140 char *s; 6141# endif 6142 6143 if (buffer == NULL) 6144 buffer = namebuf; 6145 (void) strncpy(buffer, ifp->if_name, LIFNAMSIZ); 6146 buffer[LIFNAMSIZ - 1] = '\0'; 6147# if defined(MENTAT) || defined(__FreeBSD__) || defined(__osf__) || \ 6148 defined(__sgi) || defined(_AIX51) || \ 6149 (defined(sun) && !defined(__SVR4) && !defined(__svr4__)) 6150 for (s = buffer; *s; s++) 6151 ; 6152 unit = ifp->if_unit; 6153 space = LIFNAMSIZ - (s - buffer); 6154 if ((space > 0) && (unit >= 0)) { 6155# if defined(SNPRINTF) && defined(_KERNEL) 6156 SNPRINTF(temp, sizeof(temp), "%d", unit); 6157# else 6158 (void) sprintf(temp, "%d", unit); 6159# endif 6160 (void) strncpy(s, temp, space); 6161 } 6162# endif 6163 return buffer; 6164} 6165#endif 6166 6167 6168/* ------------------------------------------------------------------------ */ 6169/* Function: ipf_ioctlswitch */ 6170/* Returns: int - -1 continue processing, else ioctl return value */ 6171/* Parameters: unit(I) - device unit opened */ 6172/* data(I) - pointer to ioctl data */ 6173/* cmd(I) - ioctl command */ 6174/* mode(I) - mode value */ 6175/* uid(I) - uid making the ioctl call */ 6176/* ctx(I) - pointer to context data */ 6177/* */ 6178/* Based on the value of unit, call the appropriate ioctl handler or return */ 6179/* EIO if ipfilter is not running. Also checks if write perms are req'd */ 6180/* for the device in order to execute the ioctl. A special case is made */ 6181/* SIOCIPFINTERROR so that the same code isn't required in every handler. */ 6182/* The context data pointer is passed through as this is used as the key */ 6183/* for locating a matching token for continued access for walking lists, */ 6184/* etc. */ 6185/* ------------------------------------------------------------------------ */ 6186int 6187ipf_ioctlswitch(softc, unit, data, cmd, mode, uid, ctx) 6188 ipf_main_softc_t *softc; 6189 int unit, mode, uid; 6190 ioctlcmd_t cmd; 6191 void *data, *ctx; 6192{ 6193 int error = 0; 6194 6195 switch (cmd) 6196 { 6197 case SIOCIPFINTERROR : 6198 error = BCOPYOUT(&softc->ipf_interror, data, 6199 sizeof(softc->ipf_interror)); 6200 if (error != 0) { 6201 IPFERROR(40); 6202 error = EFAULT; 6203 } 6204 return error; 6205 default : 6206 break; 6207 } 6208 6209 switch (unit) 6210 { 6211 case IPL_LOGIPF : 6212 error = ipf_ipf_ioctl(softc, data, cmd, mode, uid, ctx); 6213 break; 6214 case IPL_LOGNAT : 6215 if (softc->ipf_running > 0) { 6216 error = ipf_nat_ioctl(softc, data, cmd, mode, 6217 uid, ctx); 6218 } else { 6219 IPFERROR(42); 6220 error = EIO; 6221 } 6222 break; 6223 case IPL_LOGSTATE : 6224 if (softc->ipf_running > 0) { 6225 error = ipf_state_ioctl(softc, data, cmd, mode, 6226 uid, ctx); 6227 } else { 6228 IPFERROR(43); 6229 error = EIO; 6230 } 6231 break; 6232 case IPL_LOGAUTH : 6233 if (softc->ipf_running > 0) { 6234 error = ipf_auth_ioctl(softc, data, cmd, mode, 6235 uid, ctx); 6236 } else { 6237 IPFERROR(44); 6238 error = EIO; 6239 } 6240 break; 6241 case IPL_LOGSYNC : 6242 if (softc->ipf_running > 0) { 6243 error = ipf_sync_ioctl(softc, data, cmd, mode, 6244 uid, ctx); 6245 } else { 6246 error = EIO; 6247 IPFERROR(45); 6248 } 6249 break; 6250 case IPL_LOGSCAN : 6251#ifdef IPFILTER_SCAN 6252 if (softc->ipf_running > 0) 6253 error = ipf_scan_ioctl(softc, data, cmd, mode, 6254 uid, ctx); 6255 else 6256#endif 6257 { 6258 error = EIO; 6259 IPFERROR(46); 6260 } 6261 break; 6262 case IPL_LOGLOOKUP : 6263 if (softc->ipf_running > 0) { 6264 error = ipf_lookup_ioctl(softc, data, cmd, mode, 6265 uid, ctx); 6266 } else { 6267 error = EIO; 6268 IPFERROR(47); 6269 } 6270 break; 6271 default : 6272 IPFERROR(48); 6273 error = EIO; 6274 break; 6275 } 6276 6277 return error; 6278} 6279 6280 6281/* 6282 * This array defines the expected size of objects coming into the kernel 6283 * for the various recognised object types. The first column is flags (see 6284 * below), 2nd column is current size, 3rd column is the version number of 6285 * when the current size became current. 6286 * Flags: 6287 * 1 = minimum size, not absolute size 6288 */ 6289static int ipf_objbytes[IPFOBJ_COUNT][3] = { 6290 { 1, sizeof(struct frentry), 5010000 }, /* 0 */ 6291 { 1, sizeof(struct friostat), 5010000 }, 6292 { 0, sizeof(struct fr_info), 5010000 }, 6293 { 0, sizeof(struct ipf_authstat), 4010100 }, 6294 { 0, sizeof(struct ipfrstat), 5010000 }, 6295 { 1, sizeof(struct ipnat), 5010000 }, /* 5 */ 6296 { 0, sizeof(struct natstat), 5010000 }, 6297 { 0, sizeof(struct ipstate_save), 5010000 }, 6298 { 1, sizeof(struct nat_save), 5010000 }, 6299 { 0, sizeof(struct natlookup), 5010000 }, 6300 { 1, sizeof(struct ipstate), 5010000 }, /* 10 */ 6301 { 0, sizeof(struct ips_stat), 5010000 }, 6302 { 0, sizeof(struct frauth), 5010000 }, 6303 { 0, sizeof(struct ipftune), 4010100 }, 6304 { 0, sizeof(struct nat), 5010000 }, 6305 { 0, sizeof(struct ipfruleiter), 4011400 }, /* 15 */ 6306 { 0, sizeof(struct ipfgeniter), 4011400 }, 6307 { 0, sizeof(struct ipftable), 4011400 }, 6308 { 0, sizeof(struct ipflookupiter), 4011400 }, 6309 { 0, sizeof(struct ipftq) * IPF_TCP_NSTATES }, 6310 { 1, 0, 0 }, /* IPFEXPR */ 6311 { 0, 0, 0 }, /* PROXYCTL */ 6312 { 0, sizeof (struct fripf), 5010000 } 6313}; 6314 6315 6316/* ------------------------------------------------------------------------ */ 6317/* Function: ipf_inobj */ 6318/* Returns: int - 0 = success, else failure */ 6319/* Parameters: softc(I) - soft context pointerto work with */ 6320/* data(I) - pointer to ioctl data */ 6321/* objp(O) - where to store ipfobj structure */ 6322/* ptr(I) - pointer to data to copy out */ 6323/* type(I) - type of structure being moved */ 6324/* */ 6325/* Copy in the contents of what the ipfobj_t points to. In future, we */ 6326/* add things to check for version numbers, sizes, etc, to make it backward */ 6327/* compatible at the ABI for user land. */ 6328/* If objp is not NULL then we assume that the caller wants to see what is */ 6329/* in the ipfobj_t structure being copied in. As an example, this can tell */ 6330/* the caller what version of ipfilter the ioctl program was written to. */ 6331/* ------------------------------------------------------------------------ */ 6332int 6333ipf_inobj(softc, data, objp, ptr, type) 6334 ipf_main_softc_t *softc; 6335 void *data; 6336 ipfobj_t *objp; 6337 void *ptr; 6338 int type; 6339{ 6340 ipfobj_t obj; 6341 int error; 6342 int size; 6343 6344 if ((type < 0) || (type >= IPFOBJ_COUNT)) { 6345 IPFERROR(49); 6346 return EINVAL; 6347 } 6348 6349 if (objp == NULL) 6350 objp = &obj; 6351 error = BCOPYIN(data, objp, sizeof(*objp)); 6352 if (error != 0) { 6353 IPFERROR(124); 6354 return EFAULT; 6355 } 6356 6357 if (objp->ipfo_type != type) { 6358 IPFERROR(50); 6359 return EINVAL; 6360 } 6361 6362 if (objp->ipfo_rev >= ipf_objbytes[type][2]) { 6363 if ((ipf_objbytes[type][0] & 1) != 0) { 6364 if (objp->ipfo_size < ipf_objbytes[type][1]) { 6365 IPFERROR(51); 6366 return EINVAL; 6367 } 6368 size = ipf_objbytes[type][1]; 6369 } else if (objp->ipfo_size == ipf_objbytes[type][1]) { 6370 size = objp->ipfo_size; 6371 } else { 6372 IPFERROR(52); 6373 return EINVAL; 6374 } 6375 error = COPYIN(objp->ipfo_ptr, ptr, size); 6376 if (error != 0) { 6377 IPFERROR(55); 6378 error = EFAULT; 6379 } 6380 } else { 6381#ifdef IPFILTER_COMPAT 6382 error = ipf_in_compat(softc, objp, ptr, 0); 6383#else 6384 IPFERROR(54); 6385 error = EINVAL; 6386#endif 6387 } 6388 return error; 6389} 6390 6391 6392/* ------------------------------------------------------------------------ */ 6393/* Function: ipf_inobjsz */ 6394/* Returns: int - 0 = success, else failure */ 6395/* Parameters: softc(I) - soft context pointerto work with */ 6396/* data(I) - pointer to ioctl data */ 6397/* ptr(I) - pointer to store real data in */ 6398/* type(I) - type of structure being moved */ 6399/* sz(I) - size of data to copy */ 6400/* */ 6401/* As per ipf_inobj, except the size of the object to copy in is passed in */ 6402/* but it must not be smaller than the size defined for the type and the */ 6403/* type must allow for varied sized objects. The extra requirement here is */ 6404/* that sz must match the size of the object being passed in - this is not */ 6405/* not possible nor required in ipf_inobj(). */ 6406/* ------------------------------------------------------------------------ */ 6407int 6408ipf_inobjsz(softc, data, ptr, type, sz) 6409 ipf_main_softc_t *softc; 6410 void *data; 6411 void *ptr; 6412 int type, sz; 6413{ 6414 ipfobj_t obj; 6415 int error; 6416 6417 if ((type < 0) || (type >= IPFOBJ_COUNT)) { 6418 IPFERROR(56); 6419 return EINVAL; 6420 } 6421 6422 error = BCOPYIN(data, &obj, sizeof(obj)); 6423 if (error != 0) { 6424 IPFERROR(125); 6425 return EFAULT; 6426 } 6427 6428 if (obj.ipfo_type != type) { 6429 IPFERROR(58); 6430 return EINVAL; 6431 } 6432 6433 if (obj.ipfo_rev >= ipf_objbytes[type][2]) { 6434 if (((ipf_objbytes[type][0] & 1) == 0) || 6435 (sz < ipf_objbytes[type][1])) { 6436 IPFERROR(57); 6437 return EINVAL; 6438 } 6439 error = COPYIN(obj.ipfo_ptr, ptr, sz); 6440 if (error != 0) { 6441 IPFERROR(61); 6442 error = EFAULT; 6443 } 6444 } else { 6445#ifdef IPFILTER_COMPAT 6446 error = ipf_in_compat(softc, &obj, ptr, sz); 6447#else 6448 IPFERROR(60); 6449 error = EINVAL; 6450#endif 6451 } 6452 return error; 6453} 6454 6455 6456/* ------------------------------------------------------------------------ */ 6457/* Function: ipf_outobjsz */ 6458/* Returns: int - 0 = success, else failure */ 6459/* Parameters: data(I) - pointer to ioctl data */ 6460/* ptr(I) - pointer to store real data in */ 6461/* type(I) - type of structure being moved */ 6462/* sz(I) - size of data to copy */ 6463/* */ 6464/* As per ipf_outobj, except the size of the object to copy out is passed in*/ 6465/* but it must not be smaller than the size defined for the type and the */ 6466/* type must allow for varied sized objects. The extra requirement here is */ 6467/* that sz must match the size of the object being passed in - this is not */ 6468/* not possible nor required in ipf_outobj(). */ 6469/* ------------------------------------------------------------------------ */ 6470int 6471ipf_outobjsz(softc, data, ptr, type, sz) 6472 ipf_main_softc_t *softc; 6473 void *data; 6474 void *ptr; 6475 int type, sz; 6476{ 6477 ipfobj_t obj; 6478 int error; 6479 6480 if ((type < 0) || (type >= IPFOBJ_COUNT)) { 6481 IPFERROR(62); 6482 return EINVAL; 6483 } 6484 6485 error = BCOPYIN(data, &obj, sizeof(obj)); 6486 if (error != 0) { 6487 IPFERROR(127); 6488 return EFAULT; 6489 } 6490 6491 if (obj.ipfo_type != type) { 6492 IPFERROR(63); 6493 return EINVAL; 6494 } 6495 6496 if (obj.ipfo_rev >= ipf_objbytes[type][2]) { 6497 if (((ipf_objbytes[type][0] & 1) == 0) || 6498 (sz < ipf_objbytes[type][1])) { 6499 IPFERROR(146); 6500 return EINVAL; 6501 } 6502 error = COPYOUT(ptr, obj.ipfo_ptr, sz); 6503 if (error != 0) { 6504 IPFERROR(66); 6505 error = EFAULT; 6506 } 6507 } else { 6508#ifdef IPFILTER_COMPAT 6509 error = ipf_out_compat(softc, &obj, ptr); 6510#else 6511 IPFERROR(65); 6512 error = EINVAL; 6513#endif 6514 } 6515 return error; 6516} 6517 6518 6519/* ------------------------------------------------------------------------ */ 6520/* Function: ipf_outobj */ 6521/* Returns: int - 0 = success, else failure */ 6522/* Parameters: data(I) - pointer to ioctl data */ 6523/* ptr(I) - pointer to store real data in */ 6524/* type(I) - type of structure being moved */ 6525/* */ 6526/* Copy out the contents of what ptr is to where ipfobj points to. In */ 6527/* future, we add things to check for version numbers, sizes, etc, to make */ 6528/* it backward compatible at the ABI for user land. */ 6529/* ------------------------------------------------------------------------ */ 6530int 6531ipf_outobj(softc, data, ptr, type) 6532 ipf_main_softc_t *softc; 6533 void *data; 6534 void *ptr; 6535 int type; 6536{ 6537 ipfobj_t obj; 6538 int error; 6539 6540 if ((type < 0) || (type >= IPFOBJ_COUNT)) { 6541 IPFERROR(67); 6542 return EINVAL; 6543 } 6544 6545 error = BCOPYIN(data, &obj, sizeof(obj)); 6546 if (error != 0) { 6547 IPFERROR(126); 6548 return EFAULT; 6549 } 6550 6551 if (obj.ipfo_type != type) { 6552 IPFERROR(68); 6553 return EINVAL; 6554 } 6555 6556 if (obj.ipfo_rev >= ipf_objbytes[type][2]) { 6557 if ((ipf_objbytes[type][0] & 1) != 0) { 6558 if (obj.ipfo_size < ipf_objbytes[type][1]) { 6559 IPFERROR(69); 6560 return EINVAL; 6561 } 6562 } else if (obj.ipfo_size != ipf_objbytes[type][1]) { 6563 IPFERROR(70); 6564 return EINVAL; 6565 } 6566 6567 error = COPYOUT(ptr, obj.ipfo_ptr, obj.ipfo_size); 6568 if (error != 0) { 6569 IPFERROR(73); 6570 error = EFAULT; 6571 } 6572 } else { 6573#ifdef IPFILTER_COMPAT 6574 error = ipf_out_compat(softc, &obj, ptr); 6575#else 6576 IPFERROR(72); 6577 error = EINVAL; 6578#endif 6579 } 6580 return error; 6581} 6582 6583 6584/* ------------------------------------------------------------------------ */ 6585/* Function: ipf_outobjk */ 6586/* Returns: int - 0 = success, else failure */ 6587/* Parameters: obj(I) - pointer to data description structure */ 6588/* ptr(I) - pointer to kernel data to copy out */ 6589/* */ 6590/* In the above functions, the ipfobj_t structure is copied into the kernel,*/ 6591/* telling ipfilter how to copy out data. In this instance, the ipfobj_t is */ 6592/* already populated with information and now we just need to use it. */ 6593/* There is no need for this function to have a "type" parameter as there */ 6594/* is no point in validating information that comes from the kernel with */ 6595/* itself. */ 6596/* ------------------------------------------------------------------------ */ 6597int 6598ipf_outobjk(softc, obj, ptr) 6599 ipf_main_softc_t *softc; 6600 ipfobj_t *obj; 6601 void *ptr; 6602{ 6603 int type = obj->ipfo_type; 6604 int error; 6605 6606 if ((type < 0) || (type >= IPFOBJ_COUNT)) { 6607 IPFERROR(147); 6608 return EINVAL; 6609 } 6610 6611 if (obj->ipfo_rev >= ipf_objbytes[type][2]) { 6612 if ((ipf_objbytes[type][0] & 1) != 0) { 6613 if (obj->ipfo_size < ipf_objbytes[type][1]) { 6614 IPFERROR(148); 6615 return EINVAL; 6616 } 6617 6618 } else if (obj->ipfo_size != ipf_objbytes[type][1]) { 6619 IPFERROR(149); 6620 return EINVAL; 6621 } 6622 6623 error = COPYOUT(ptr, obj->ipfo_ptr, obj->ipfo_size); 6624 if (error != 0) { 6625 IPFERROR(150); 6626 error = EFAULT; 6627 } 6628 } else { 6629#ifdef IPFILTER_COMPAT 6630 error = ipf_out_compat(softc, obj, ptr); 6631#else 6632 IPFERROR(151); 6633 error = EINVAL; 6634#endif 6635 } 6636 return error; 6637} 6638 6639 6640/* ------------------------------------------------------------------------ */ 6641/* Function: ipf_checkl4sum */ 6642/* Returns: int - 0 = good, -1 = bad, 1 = cannot check */ 6643/* Parameters: fin(I) - pointer to packet information */ 6644/* */ 6645/* If possible, calculate the layer 4 checksum for the packet. If this is */ 6646/* not possible, return without indicating a failure or success but in a */ 6647/* way that is ditinguishable. This function should only be called by the */ 6648/* ipf_checkv6sum() for each platform. */ 6649/* ------------------------------------------------------------------------ */ 6650INLINE int 6651ipf_checkl4sum(fin) 6652 fr_info_t *fin; 6653{ 6654 u_short sum, hdrsum, *csump; 6655 udphdr_t *udp; 6656 int dosum; 6657 6658 /* 6659 * If the TCP packet isn't a fragment, isn't too short and otherwise 6660 * isn't already considered "bad", then validate the checksum. If 6661 * this check fails then considered the packet to be "bad". 6662 */ 6663 if ((fin->fin_flx & (FI_FRAG|FI_SHORT|FI_BAD)) != 0) 6664 return 1; 6665 6666 csump = NULL; 6667 hdrsum = 0; 6668 dosum = 0; 6669 sum = 0; 6670 6671 switch (fin->fin_p) 6672 { 6673 case IPPROTO_TCP : 6674 csump = &((tcphdr_t *)fin->fin_dp)->th_sum; 6675 dosum = 1; 6676 break; 6677 6678 case IPPROTO_UDP : 6679 udp = fin->fin_dp; 6680 if (udp->uh_sum != 0) { 6681 csump = &udp->uh_sum; 6682 dosum = 1; 6683 } 6684 break; 6685 6686#ifdef USE_INET6 6687 case IPPROTO_ICMPV6 : 6688 csump = &((struct icmp6_hdr *)fin->fin_dp)->icmp6_cksum; 6689 dosum = 1; 6690 break; 6691#endif 6692 6693 case IPPROTO_ICMP : 6694 csump = &((struct icmp *)fin->fin_dp)->icmp_cksum; 6695 dosum = 1; 6696 break; 6697 6698 default : 6699 return 1; 6700 /*NOTREACHED*/ 6701 } 6702 6703 if (csump != NULL) 6704 hdrsum = *csump; 6705 6706 if (dosum) { 6707 sum = fr_cksum(fin, fin->fin_ip, fin->fin_p, fin->fin_dp); 6708 } 6709#if !defined(_KERNEL) 6710 if (sum == hdrsum) { 6711 FR_DEBUG(("checkl4sum: %hx == %hx\n", sum, hdrsum)); 6712 } else { 6713 FR_DEBUG(("checkl4sum: %hx != %hx\n", sum, hdrsum)); 6714 } 6715#endif 6716 DT2(l4sums, u_short, hdrsum, u_short, sum); 6717 if (hdrsum == sum) { 6718 fin->fin_cksum = FI_CK_SUMOK; 6719 return 0; 6720 } 6721 fin->fin_cksum = FI_CK_BAD; 6722 return -1; 6723} 6724 6725 6726/* ------------------------------------------------------------------------ */ 6727/* Function: ipf_ifpfillv4addr */ 6728/* Returns: int - 0 = address update, -1 = address not updated */ 6729/* Parameters: atype(I) - type of network address update to perform */ 6730/* sin(I) - pointer to source of address information */ 6731/* mask(I) - pointer to source of netmask information */ 6732/* inp(I) - pointer to destination address store */ 6733/* inpmask(I) - pointer to destination netmask store */ 6734/* */ 6735/* Given a type of network address update (atype) to perform, copy */ 6736/* information from sin/mask into inp/inpmask. If ipnmask is NULL then no */ 6737/* netmask update is performed unless FRI_NETMASKED is passed as atype, in */ 6738/* which case the operation fails. For all values of atype other than */ 6739/* FRI_NETMASKED, if inpmask is non-NULL then the mask is set to an all 1s */ 6740/* value. */ 6741/* ------------------------------------------------------------------------ */ 6742int 6743ipf_ifpfillv4addr(atype, sin, mask, inp, inpmask) 6744 int atype; 6745 struct sockaddr_in *sin, *mask; 6746 struct in_addr *inp, *inpmask; 6747{ 6748 if (inpmask != NULL && atype != FRI_NETMASKED) 6749 inpmask->s_addr = 0xffffffff; 6750 6751 if (atype == FRI_NETWORK || atype == FRI_NETMASKED) { 6752 if (atype == FRI_NETMASKED) { 6753 if (inpmask == NULL) 6754 return -1; 6755 inpmask->s_addr = mask->sin_addr.s_addr; 6756 } 6757 inp->s_addr = sin->sin_addr.s_addr & mask->sin_addr.s_addr; 6758 } else { 6759 inp->s_addr = sin->sin_addr.s_addr; 6760 } 6761 return 0; 6762} 6763 6764 6765#ifdef USE_INET6 6766/* ------------------------------------------------------------------------ */ 6767/* Function: ipf_ifpfillv6addr */ 6768/* Returns: int - 0 = address update, -1 = address not updated */ 6769/* Parameters: atype(I) - type of network address update to perform */ 6770/* sin(I) - pointer to source of address information */ 6771/* mask(I) - pointer to source of netmask information */ 6772/* inp(I) - pointer to destination address store */ 6773/* inpmask(I) - pointer to destination netmask store */ 6774/* */ 6775/* Given a type of network address update (atype) to perform, copy */ 6776/* information from sin/mask into inp/inpmask. If ipnmask is NULL then no */ 6777/* netmask update is performed unless FRI_NETMASKED is passed as atype, in */ 6778/* which case the operation fails. For all values of atype other than */ 6779/* FRI_NETMASKED, if inpmask is non-NULL then the mask is set to an all 1s */ 6780/* value. */ 6781/* ------------------------------------------------------------------------ */ 6782int 6783ipf_ifpfillv6addr(atype, sin, mask, inp, inpmask) 6784 int atype; 6785 struct sockaddr_in6 *sin, *mask; 6786 i6addr_t *inp, *inpmask; 6787{ 6788 i6addr_t *src, *and; 6789 6790 src = (i6addr_t *)&sin->sin6_addr; 6791 and = (i6addr_t *)&mask->sin6_addr; 6792 6793 if (inpmask != NULL && atype != FRI_NETMASKED) { 6794 inpmask->i6[0] = 0xffffffff; 6795 inpmask->i6[1] = 0xffffffff; 6796 inpmask->i6[2] = 0xffffffff; 6797 inpmask->i6[3] = 0xffffffff; 6798 } 6799 6800 if (atype == FRI_NETWORK || atype == FRI_NETMASKED) { 6801 if (atype == FRI_NETMASKED) { 6802 if (inpmask == NULL) 6803 return -1; 6804 inpmask->i6[0] = and->i6[0]; 6805 inpmask->i6[1] = and->i6[1]; 6806 inpmask->i6[2] = and->i6[2]; 6807 inpmask->i6[3] = and->i6[3]; 6808 } 6809 6810 inp->i6[0] = src->i6[0] & and->i6[0]; 6811 inp->i6[1] = src->i6[1] & and->i6[1]; 6812 inp->i6[2] = src->i6[2] & and->i6[2]; 6813 inp->i6[3] = src->i6[3] & and->i6[3]; 6814 } else { 6815 inp->i6[0] = src->i6[0]; 6816 inp->i6[1] = src->i6[1]; 6817 inp->i6[2] = src->i6[2]; 6818 inp->i6[3] = src->i6[3]; 6819 } 6820 return 0; 6821} 6822#endif 6823 6824 6825/* ------------------------------------------------------------------------ */ 6826/* Function: ipf_matchtag */ 6827/* Returns: 0 == mismatch, 1 == match. */ 6828/* Parameters: tag1(I) - pointer to first tag to compare */ 6829/* tag2(I) - pointer to second tag to compare */ 6830/* */ 6831/* Returns true (non-zero) or false(0) if the two tag structures can be */ 6832/* considered to be a match or not match, respectively. The tag is 16 */ 6833/* bytes long (16 characters) but that is overlayed with 4 32bit ints so */ 6834/* compare the ints instead, for speed. tag1 is the master of the */ 6835/* comparison. This function should only be called with both tag1 and tag2 */ 6836/* as non-NULL pointers. */ 6837/* ------------------------------------------------------------------------ */ 6838int 6839ipf_matchtag(tag1, tag2) 6840 ipftag_t *tag1, *tag2; 6841{ 6842 if (tag1 == tag2) 6843 return 1; 6844 6845 if ((tag1->ipt_num[0] == 0) && (tag2->ipt_num[0] == 0)) 6846 return 1; 6847 6848 if ((tag1->ipt_num[0] == tag2->ipt_num[0]) && 6849 (tag1->ipt_num[1] == tag2->ipt_num[1]) && 6850 (tag1->ipt_num[2] == tag2->ipt_num[2]) && 6851 (tag1->ipt_num[3] == tag2->ipt_num[3])) 6852 return 1; 6853 return 0; 6854} 6855 6856 6857/* ------------------------------------------------------------------------ */ 6858/* Function: ipf_coalesce */ 6859/* Returns: 1 == success, -1 == failure, 0 == no change */ 6860/* Parameters: fin(I) - pointer to packet information */ 6861/* */ 6862/* Attempt to get all of the packet data into a single, contiguous buffer. */ 6863/* If this call returns a failure then the buffers have also been freed. */ 6864/* ------------------------------------------------------------------------ */ 6865int 6866ipf_coalesce(fin) 6867 fr_info_t *fin; 6868{ 6869 6870 if ((fin->fin_flx & FI_COALESCE) != 0) 6871 return 1; 6872 6873 /* 6874 * If the mbuf pointers indicate that there is no mbuf to work with, 6875 * return but do not indicate success or failure. 6876 */ 6877 if (fin->fin_m == NULL || fin->fin_mp == NULL) 6878 return 0; 6879 6880#if defined(_KERNEL) 6881 if (ipf_pullup(fin->fin_m, fin, fin->fin_plen) == NULL) { 6882 ipf_main_softc_t *softc = fin->fin_main_soft; 6883 6884 DT1(frb_coalesce, fr_info_t *, fin); 6885 LBUMP(ipf_stats[fin->fin_out].fr_badcoalesces); 6886# ifdef MENTAT 6887 FREE_MB_T(*fin->fin_mp); 6888# endif 6889 fin->fin_reason = FRB_COALESCE; 6890 *fin->fin_mp = NULL; 6891 fin->fin_m = NULL; 6892 return -1; 6893 } 6894#else 6895 fin = fin; /* LINT */ 6896#endif 6897 return 1; 6898} 6899 6900 6901/* 6902 * The following table lists all of the tunable variables that can be 6903 * accessed via SIOCIPFGET/SIOCIPFSET/SIOCIPFGETNEXt. The format of each row 6904 * in the table below is as follows: 6905 * 6906 * pointer to value, name of value, minimum, maximum, size of the value's 6907 * container, value attribute flags 6908 * 6909 * For convienience, IPFT_RDONLY means the value is read-only, IPFT_WRDISABLED 6910 * means the value can only be written to when IPFilter is loaded but disabled. 6911 * The obvious implication is if neither of these are set then the value can be 6912 * changed at any time without harm. 6913 */ 6914 6915 6916/* ------------------------------------------------------------------------ */ 6917/* Function: ipf_tune_findbycookie */ 6918/* Returns: NULL = search failed, else pointer to tune struct */ 6919/* Parameters: cookie(I) - cookie value to search for amongst tuneables */ 6920/* next(O) - pointer to place to store the cookie for the */ 6921/* "next" tuneable, if it is desired. */ 6922/* */ 6923/* This function is used to walk through all of the existing tunables with */ 6924/* successive calls. It searches the known tunables for the one which has */ 6925/* a matching value for "cookie" - ie its address. When returning a match, */ 6926/* the next one to be found may be returned inside next. */ 6927/* ------------------------------------------------------------------------ */ 6928static ipftuneable_t * 6929ipf_tune_findbycookie(ptop, cookie, next) 6930 ipftuneable_t **ptop; 6931 void *cookie, **next; 6932{ 6933 ipftuneable_t *ta, **tap; 6934 6935 for (ta = *ptop; ta->ipft_name != NULL; ta++) 6936 if (ta == cookie) { 6937 if (next != NULL) { 6938 /* 6939 * If the next entry in the array has a name 6940 * present, then return a pointer to it for 6941 * where to go next, else return a pointer to 6942 * the dynaminc list as a key to search there 6943 * next. This facilitates a weak linking of 6944 * the two "lists" together. 6945 */ 6946 if ((ta + 1)->ipft_name != NULL) 6947 *next = ta + 1; 6948 else 6949 *next = ptop; 6950 } 6951 return ta; 6952 } 6953 6954 for (tap = ptop; (ta = *tap) != NULL; tap = &ta->ipft_next) 6955 if (tap == cookie) { 6956 if (next != NULL) 6957 *next = &ta->ipft_next; 6958 return ta; 6959 } 6960 6961 if (next != NULL) 6962 *next = NULL; 6963 return NULL; 6964} 6965 6966 6967/* ------------------------------------------------------------------------ */ 6968/* Function: ipf_tune_findbyname */ 6969/* Returns: NULL = search failed, else pointer to tune struct */ 6970/* Parameters: name(I) - name of the tuneable entry to find. */ 6971/* */ 6972/* Search the static array of tuneables and the list of dynamic tuneables */ 6973/* for an entry with a matching name. If we can find one, return a pointer */ 6974/* to the matching structure. */ 6975/* ------------------------------------------------------------------------ */ 6976static ipftuneable_t * 6977ipf_tune_findbyname(top, name) 6978 ipftuneable_t *top; 6979 const char *name; 6980{ 6981 ipftuneable_t *ta; 6982 6983 for (ta = top; ta != NULL; ta = ta->ipft_next) 6984 if (!strcmp(ta->ipft_name, name)) { 6985 return ta; 6986 } 6987 6988 return NULL; 6989} 6990 6991 6992/* ------------------------------------------------------------------------ */ 6993/* Function: ipf_tune_add_array */ 6994/* Returns: int - 0 == success, else failure */ 6995/* Parameters: newtune - pointer to new tune array to add to tuneables */ 6996/* */ 6997/* Appends tune structures from the array passed in (newtune) to the end of */ 6998/* the current list of "dynamic" tuneable parameters. */ 6999/* If any entry to be added is already present (by name) then the operation */ 7000/* is aborted - entries that have been added are removed before returning. */ 7001/* An entry with no name (NULL) is used as the indication that the end of */ 7002/* the array has been reached. */ 7003/* ------------------------------------------------------------------------ */ 7004int 7005ipf_tune_add_array(softc, newtune) 7006 ipf_main_softc_t *softc; 7007 ipftuneable_t *newtune; 7008{ 7009 ipftuneable_t *nt, *dt; 7010 int error = 0; 7011 7012 for (nt = newtune; nt->ipft_name != NULL; nt++) { 7013 error = ipf_tune_add(softc, nt); 7014 if (error != 0) { 7015 for (dt = newtune; dt != nt; dt++) { 7016 (void) ipf_tune_del(softc, dt); 7017 } 7018 } 7019 } 7020 7021 return error; 7022} 7023 7024 7025/* ------------------------------------------------------------------------ */ 7026/* Function: ipf_tune_array_link */ 7027/* Returns: 0 == success, -1 == failure */ 7028/* Parameters: softc(I) - soft context pointerto work with */ 7029/* array(I) - pointer to an array of tuneables */ 7030/* */ 7031/* Given an array of tunables (array), append them to the current list of */ 7032/* tuneables for this context (softc->ipf_tuners.) To properly prepare the */ 7033/* the array for being appended to the list, initialise all of the next */ 7034/* pointers so we don't need to walk parts of it with ++ and others with */ 7035/* next. The array is expected to have an entry with a NULL name as the */ 7036/* terminator. Trying to add an array with no non-NULL names will return as */ 7037/* a failure. */ 7038/* ------------------------------------------------------------------------ */ 7039int 7040ipf_tune_array_link(softc, array) 7041 ipf_main_softc_t *softc; 7042 ipftuneable_t *array; 7043{ 7044 ipftuneable_t *t, **p; 7045 7046 t = array; 7047 if (t->ipft_name == NULL) 7048 return -1; 7049 7050 for (; t[1].ipft_name != NULL; t++) 7051 t[0].ipft_next = &t[1]; 7052 t->ipft_next = NULL; 7053 7054 /* 7055 * Since a pointer to the last entry isn't kept, we need to find it 7056 * each time we want to add new variables to the list. 7057 */ 7058 for (p = &softc->ipf_tuners; (t = *p) != NULL; p = &t->ipft_next) 7059 if (t->ipft_name == NULL) 7060 break; 7061 *p = array; 7062 7063 return 0; 7064} 7065 7066 7067/* ------------------------------------------------------------------------ */ 7068/* Function: ipf_tune_array_unlink */ 7069/* Returns: 0 == success, -1 == failure */ 7070/* Parameters: softc(I) - soft context pointerto work with */ 7071/* array(I) - pointer to an array of tuneables */ 7072/* */ 7073/* ------------------------------------------------------------------------ */ 7074int 7075ipf_tune_array_unlink(softc, array) 7076 ipf_main_softc_t *softc; 7077 ipftuneable_t *array; 7078{ 7079 ipftuneable_t *t, **p; 7080 7081 for (p = &softc->ipf_tuners; (t = *p) != NULL; p = &t->ipft_next) 7082 if (t == array) 7083 break; 7084 if (t == NULL) 7085 return -1; 7086 7087 for (; t[1].ipft_name != NULL; t++) 7088 ; 7089 7090 *p = t->ipft_next; 7091 7092 return 0; 7093} 7094 7095 7096/* ------------------------------------------------------------------------ */ 7097/* Function: ipf_tune_array_copy */ 7098/* Returns: NULL = failure, else pointer to new array */ 7099/* Parameters: base(I) - pointer to structure base */ 7100/* size(I) - size of the array at template */ 7101/* template(I) - original array to copy */ 7102/* */ 7103/* Allocate memory for a new set of tuneable values and copy everything */ 7104/* from template into the new region of memory. The new region is full of */ 7105/* uninitialised pointers (ipft_next) so set them up. Now, ipftp_offset... */ 7106/* */ 7107/* NOTE: the following assumes that sizeof(long) == sizeof(void *) */ 7108/* In the array template, ipftp_offset is the offset (in bytes) of the */ 7109/* location of the tuneable value inside the structure pointed to by base. */ 7110/* As ipftp_offset is a union over the pointers to the tuneable values, if */ 7111/* we add base to the copy's ipftp_offset, copy ends up with a pointer in */ 7112/* ipftp_void that points to the stored value. */ 7113/* ------------------------------------------------------------------------ */ 7114ipftuneable_t * 7115ipf_tune_array_copy(base, size, template) 7116 void *base; 7117 size_t size; 7118 ipftuneable_t *template; 7119{ 7120 ipftuneable_t *copy; 7121 int i; 7122 7123 7124 KMALLOCS(copy, ipftuneable_t *, size); 7125 if (copy == NULL) { 7126 return NULL; 7127 } 7128 bcopy(template, copy, size); 7129 7130 for (i = 0; copy[i].ipft_name; i++) { 7131 copy[i].ipft_una.ipftp_offset += (u_long)base; 7132 copy[i].ipft_next = copy + i + 1; 7133 } 7134 7135 return copy; 7136} 7137 7138 7139/* ------------------------------------------------------------------------ */ 7140/* Function: ipf_tune_add */ 7141/* Returns: int - 0 == success, else failure */ 7142/* Parameters: newtune - pointer to new tune entry to add to tuneables */ 7143/* */ 7144/* Appends tune structures from the array passed in (newtune) to the end of */ 7145/* the current list of "dynamic" tuneable parameters. Once added, the */ 7146/* owner of the object is not expected to ever change "ipft_next". */ 7147/* ------------------------------------------------------------------------ */ 7148int 7149ipf_tune_add(softc, newtune) 7150 ipf_main_softc_t *softc; 7151 ipftuneable_t *newtune; 7152{ 7153 ipftuneable_t *ta, **tap; 7154 7155 ta = ipf_tune_findbyname(softc->ipf_tuners, newtune->ipft_name); 7156 if (ta != NULL) { 7157 IPFERROR(74); 7158 return EEXIST; 7159 } 7160 7161 for (tap = &softc->ipf_tuners; *tap != NULL; tap = &(*tap)->ipft_next) 7162 ; 7163 7164 newtune->ipft_next = NULL; 7165 *tap = newtune; 7166 return 0; 7167} 7168 7169 7170/* ------------------------------------------------------------------------ */ 7171/* Function: ipf_tune_del */ 7172/* Returns: int - 0 == success, else failure */ 7173/* Parameters: oldtune - pointer to tune entry to remove from the list of */ 7174/* current dynamic tuneables */ 7175/* */ 7176/* Search for the tune structure, by pointer, in the list of those that are */ 7177/* dynamically added at run time. If found, adjust the list so that this */ 7178/* structure is no longer part of it. */ 7179/* ------------------------------------------------------------------------ */ 7180int 7181ipf_tune_del(softc, oldtune) 7182 ipf_main_softc_t *softc; 7183 ipftuneable_t *oldtune; 7184{ 7185 ipftuneable_t *ta, **tap; 7186 int error = 0; 7187 7188 for (tap = &softc->ipf_tuners; (ta = *tap) != NULL; 7189 tap = &ta->ipft_next) { 7190 if (ta == oldtune) { 7191 *tap = oldtune->ipft_next; 7192 oldtune->ipft_next = NULL; 7193 break; 7194 } 7195 } 7196 7197 if (ta == NULL) { 7198 error = ESRCH; 7199 IPFERROR(75); 7200 } 7201 return error; 7202} 7203 7204 7205/* ------------------------------------------------------------------------ */ 7206/* Function: ipf_tune_del_array */ 7207/* Returns: int - 0 == success, else failure */ 7208/* Parameters: oldtune - pointer to tuneables array */ 7209/* */ 7210/* Remove each tuneable entry in the array from the list of "dynamic" */ 7211/* tunables. If one entry should fail to be found, an error will be */ 7212/* returned and no further ones removed. */ 7213/* An entry with a NULL name is used as the indicator of the last entry in */ 7214/* the array. */ 7215/* ------------------------------------------------------------------------ */ 7216int 7217ipf_tune_del_array(softc, oldtune) 7218 ipf_main_softc_t *softc; 7219 ipftuneable_t *oldtune; 7220{ 7221 ipftuneable_t *ot; 7222 int error = 0; 7223 7224 for (ot = oldtune; ot->ipft_name != NULL; ot++) { 7225 error = ipf_tune_del(softc, ot); 7226 if (error != 0) 7227 break; 7228 } 7229 7230 return error; 7231 7232} 7233 7234 7235/* ------------------------------------------------------------------------ */ 7236/* Function: ipf_tune */ 7237/* Returns: int - 0 == success, else failure */ 7238/* Parameters: cmd(I) - ioctl command number */ 7239/* data(I) - pointer to ioctl data structure */ 7240/* */ 7241/* Implement handling of SIOCIPFGETNEXT, SIOCIPFGET and SIOCIPFSET. These */ 7242/* three ioctls provide the means to access and control global variables */ 7243/* within IPFilter, allowing (for example) timeouts and table sizes to be */ 7244/* changed without rebooting, reloading or recompiling. The initialisation */ 7245/* and 'destruction' routines of the various components of ipfilter are all */ 7246/* each responsible for handling their own values being too big. */ 7247/* ------------------------------------------------------------------------ */ 7248int 7249ipf_ipftune(softc, cmd, data) 7250 ipf_main_softc_t *softc; 7251 ioctlcmd_t cmd; 7252 void *data; 7253{ 7254 ipftuneable_t *ta; 7255 ipftune_t tu; 7256 void *cookie; 7257 int error; 7258 7259 error = ipf_inobj(softc, data, NULL, &tu, IPFOBJ_TUNEABLE); 7260 if (error != 0) 7261 return error; 7262 7263 tu.ipft_name[sizeof(tu.ipft_name) - 1] = '\0'; 7264 cookie = tu.ipft_cookie; 7265 ta = NULL; 7266 7267 switch (cmd) 7268 { 7269 case SIOCIPFGETNEXT : 7270 /* 7271 * If cookie is non-NULL, assume it to be a pointer to the last 7272 * entry we looked at, so find it (if possible) and return a 7273 * pointer to the next one after it. The last entry in the 7274 * the table is a NULL entry, so when we get to it, set cookie 7275 * to NULL and return that, indicating end of list, erstwhile 7276 * if we come in with cookie set to NULL, we are starting anew 7277 * at the front of the list. 7278 */ 7279 if (cookie != NULL) { 7280 ta = ipf_tune_findbycookie(&softc->ipf_tuners, 7281 cookie, &tu.ipft_cookie); 7282 } else { 7283 ta = softc->ipf_tuners; 7284 tu.ipft_cookie = ta + 1; 7285 } 7286 if (ta != NULL) { 7287 /* 7288 * Entry found, but does the data pointed to by that 7289 * row fit in what we can return? 7290 */ 7291 if (ta->ipft_sz > sizeof(tu.ipft_un)) { 7292 IPFERROR(76); 7293 return EINVAL; 7294 } 7295 7296 tu.ipft_vlong = 0; 7297 if (ta->ipft_sz == sizeof(u_long)) 7298 tu.ipft_vlong = *ta->ipft_plong; 7299 else if (ta->ipft_sz == sizeof(u_int)) 7300 tu.ipft_vint = *ta->ipft_pint; 7301 else if (ta->ipft_sz == sizeof(u_short)) 7302 tu.ipft_vshort = *ta->ipft_pshort; 7303 else if (ta->ipft_sz == sizeof(u_char)) 7304 tu.ipft_vchar = *ta->ipft_pchar; 7305 7306 tu.ipft_sz = ta->ipft_sz; 7307 tu.ipft_min = ta->ipft_min; 7308 tu.ipft_max = ta->ipft_max; 7309 tu.ipft_flags = ta->ipft_flags; 7310 bcopy(ta->ipft_name, tu.ipft_name, 7311 MIN(sizeof(tu.ipft_name), 7312 strlen(ta->ipft_name) + 1)); 7313 } 7314 error = ipf_outobj(softc, data, &tu, IPFOBJ_TUNEABLE); 7315 break; 7316 7317 case SIOCIPFGET : 7318 case SIOCIPFSET : 7319 /* 7320 * Search by name or by cookie value for a particular entry 7321 * in the tuning paramter table. 7322 */ 7323 IPFERROR(77); 7324 error = ESRCH; 7325 if (cookie != NULL) { 7326 ta = ipf_tune_findbycookie(&softc->ipf_tuners, 7327 cookie, NULL); 7328 if (ta != NULL) 7329 error = 0; 7330 } else if (tu.ipft_name[0] != '\0') { 7331 ta = ipf_tune_findbyname(softc->ipf_tuners, 7332 tu.ipft_name); 7333 if (ta != NULL) 7334 error = 0; 7335 } 7336 if (error != 0) 7337 break; 7338 7339 if (cmd == (ioctlcmd_t)SIOCIPFGET) { 7340 /* 7341 * Fetch the tuning parameters for a particular value 7342 */ 7343 tu.ipft_vlong = 0; 7344 if (ta->ipft_sz == sizeof(u_long)) 7345 tu.ipft_vlong = *ta->ipft_plong; 7346 else if (ta->ipft_sz == sizeof(u_int)) 7347 tu.ipft_vint = *ta->ipft_pint; 7348 else if (ta->ipft_sz == sizeof(u_short)) 7349 tu.ipft_vshort = *ta->ipft_pshort; 7350 else if (ta->ipft_sz == sizeof(u_char)) 7351 tu.ipft_vchar = *ta->ipft_pchar; 7352 tu.ipft_cookie = ta; 7353 tu.ipft_sz = ta->ipft_sz; 7354 tu.ipft_min = ta->ipft_min; 7355 tu.ipft_max = ta->ipft_max; 7356 tu.ipft_flags = ta->ipft_flags; 7357 error = ipf_outobj(softc, data, &tu, IPFOBJ_TUNEABLE); 7358 7359 } else if (cmd == (ioctlcmd_t)SIOCIPFSET) { 7360 /* 7361 * Set an internal parameter. The hard part here is 7362 * getting the new value safely and correctly out of 7363 * the kernel (given we only know its size, not type.) 7364 */ 7365 u_long in; 7366 7367 if (((ta->ipft_flags & IPFT_WRDISABLED) != 0) && 7368 (softc->ipf_running > 0)) { 7369 IPFERROR(78); 7370 error = EBUSY; 7371 break; 7372 } 7373 7374 in = tu.ipft_vlong; 7375 if (in < ta->ipft_min || in > ta->ipft_max) { 7376 IPFERROR(79); 7377 error = EINVAL; 7378 break; 7379 } 7380 7381 if (ta->ipft_func != NULL) { 7382 SPL_INT(s); 7383 7384 SPL_NET(s); 7385 error = (*ta->ipft_func)(softc, ta, 7386 &tu.ipft_un); 7387 SPL_X(s); 7388 7389 } else if (ta->ipft_sz == sizeof(u_long)) { 7390 tu.ipft_vlong = *ta->ipft_plong; 7391 *ta->ipft_plong = in; 7392 7393 } else if (ta->ipft_sz == sizeof(u_int)) { 7394 tu.ipft_vint = *ta->ipft_pint; 7395 *ta->ipft_pint = (u_int)(in & 0xffffffff); 7396 7397 } else if (ta->ipft_sz == sizeof(u_short)) { 7398 tu.ipft_vshort = *ta->ipft_pshort; 7399 *ta->ipft_pshort = (u_short)(in & 0xffff); 7400 7401 } else if (ta->ipft_sz == sizeof(u_char)) { 7402 tu.ipft_vchar = *ta->ipft_pchar; 7403 *ta->ipft_pchar = (u_char)(in & 0xff); 7404 } 7405 error = ipf_outobj(softc, data, &tu, IPFOBJ_TUNEABLE); 7406 } 7407 break; 7408 7409 default : 7410 IPFERROR(80); 7411 error = EINVAL; 7412 break; 7413 } 7414 7415 return error; 7416} 7417 7418 7419/* ------------------------------------------------------------------------ */ 7420/* Function: ipf_zerostats */ 7421/* Returns: int - 0 = success, else failure */ 7422/* Parameters: data(O) - pointer to pointer for copying data back to */ 7423/* */ 7424/* Copies the current statistics out to userspace and then zero's the */ 7425/* current ones in the kernel. The lock is only held across the bzero() as */ 7426/* the copyout may result in paging (ie network activity.) */ 7427/* ------------------------------------------------------------------------ */ 7428int 7429ipf_zerostats(softc, data) 7430 ipf_main_softc_t *softc; 7431 caddr_t data; 7432{ 7433 friostat_t fio; 7434 ipfobj_t obj; 7435 int error; 7436 7437 error = ipf_inobj(softc, data, &obj, &fio, IPFOBJ_IPFSTAT); 7438 if (error != 0) 7439 return error; 7440 ipf_getstat(softc, &fio, obj.ipfo_rev); 7441 error = ipf_outobj(softc, data, &fio, IPFOBJ_IPFSTAT); 7442 if (error != 0) 7443 return error; 7444 7445 WRITE_ENTER(&softc->ipf_mutex); 7446 bzero(&softc->ipf_stats, sizeof(softc->ipf_stats)); 7447 RWLOCK_EXIT(&softc->ipf_mutex); 7448 7449 return 0; 7450} 7451 7452 7453/* ------------------------------------------------------------------------ */ 7454/* Function: ipf_resolvedest */ 7455/* Returns: Nil */ 7456/* Parameters: softc(I) - pointer to soft context main structure */ 7457/* base(I) - where strings are stored */ 7458/* fdp(IO) - pointer to destination information to resolve */ 7459/* v(I) - IP protocol version to match */ 7460/* */ 7461/* Looks up an interface name in the frdest structure pointed to by fdp and */ 7462/* if a matching name can be found for the particular IP protocol version */ 7463/* then store the interface pointer in the frdest struct. If no match is */ 7464/* found, then set the interface pointer to be -1 as NULL is considered to */ 7465/* indicate there is no information at all in the structure. */ 7466/* ------------------------------------------------------------------------ */ 7467int 7468ipf_resolvedest(softc, base, fdp, v) 7469 ipf_main_softc_t *softc; 7470 char *base; 7471 frdest_t *fdp; 7472 int v; 7473{ 7474 int errval = 0; 7475 void *ifp; 7476 7477 ifp = NULL; 7478 7479 if (fdp->fd_name != -1) { 7480 if (fdp->fd_type == FRD_DSTLIST) { 7481 ifp = ipf_lookup_res_name(softc, IPL_LOGIPF, 7482 IPLT_DSTLIST, 7483 base + fdp->fd_name, 7484 NULL); 7485 if (ifp == NULL) { 7486 IPFERROR(144); 7487 errval = ESRCH; 7488 } 7489 } else { 7490 ifp = GETIFP(base + fdp->fd_name, v); 7491 if (ifp == NULL) 7492 ifp = (void *)-1; 7493 } 7494 } 7495 fdp->fd_ptr = ifp; 7496 7497 if ((ifp != NULL) && (ifp != (void *)-1)) { 7498 fdp->fd_local = ipf_deliverlocal(softc, v, ifp, &fdp->fd_ip6); 7499 } 7500 7501 return errval; 7502} 7503 7504 7505/* ------------------------------------------------------------------------ */ 7506/* Function: ipf_resolvenic */ 7507/* Returns: void* - NULL = wildcard name, -1 = failed to find NIC, else */ 7508/* pointer to interface structure for NIC */ 7509/* Parameters: softc(I)- pointer to soft context main structure */ 7510/* name(I) - complete interface name */ 7511/* v(I) - IP protocol version */ 7512/* */ 7513/* Look for a network interface structure that firstly has a matching name */ 7514/* to that passed in and that is also being used for that IP protocol */ 7515/* version (necessary on some platforms where there are separate listings */ 7516/* for both IPv4 and IPv6 on the same physical NIC. */ 7517/* ------------------------------------------------------------------------ */ 7518void * 7519ipf_resolvenic(softc, name, v) 7520 ipf_main_softc_t *softc; 7521 char *name; 7522 int v; 7523{ 7524 void *nic; 7525 7526 softc = softc; /* gcc -Wextra */ 7527 if (name[0] == '\0') 7528 return NULL; 7529 7530 if ((name[1] == '\0') && ((name[0] == '-') || (name[0] == '*'))) { 7531 return NULL; 7532 } 7533 7534 nic = GETIFP(name, v); 7535 if (nic == NULL) 7536 nic = (void *)-1; 7537 return nic; 7538} 7539 7540 7541/* ------------------------------------------------------------------------ */ 7542/* Function: ipf_token_expire */ 7543/* Returns: None. */ 7544/* Parameters: softc(I) - pointer to soft context main structure */ 7545/* */ 7546/* This function is run every ipf tick to see if there are any tokens that */ 7547/* have been held for too long and need to be freed up. */ 7548/* ------------------------------------------------------------------------ */ 7549void 7550ipf_token_expire(softc) 7551 ipf_main_softc_t *softc; 7552{ 7553 ipftoken_t *it; 7554 7555 WRITE_ENTER(&softc->ipf_tokens); 7556 while ((it = softc->ipf_token_head) != NULL) { 7557 if (it->ipt_die > softc->ipf_ticks) 7558 break; 7559 7560 ipf_token_deref(softc, it); 7561 } 7562 RWLOCK_EXIT(&softc->ipf_tokens); 7563} 7564 7565 7566/* ------------------------------------------------------------------------ */ 7567/* Function: ipf_token_flush */ 7568/* Returns: None. */ 7569/* Parameters: softc(I) - pointer to soft context main structure */ 7570/* */ 7571/* Loop through all of the existing tokens and call deref to see if they */ 7572/* can be freed. Normally a function like this might just loop on */ 7573/* ipf_token_head but there is a chance that a token might have a ref count */ 7574/* of greater than one and in that case the the reference would drop twice */ 7575/* by code that is only entitled to drop it once. */ 7576/* ------------------------------------------------------------------------ */ 7577static void 7578ipf_token_flush(softc) 7579 ipf_main_softc_t *softc; 7580{ 7581 ipftoken_t *it, *next; 7582 7583 WRITE_ENTER(&softc->ipf_tokens); 7584 for (it = softc->ipf_token_head; it != NULL; it = next) { 7585 next = it->ipt_next; 7586 (void) ipf_token_deref(softc, it); 7587 } 7588 RWLOCK_EXIT(&softc->ipf_tokens); 7589} 7590 7591 7592/* ------------------------------------------------------------------------ */ 7593/* Function: ipf_token_del */ 7594/* Returns: int - 0 = success, else error */ 7595/* Parameters: softc(I)- pointer to soft context main structure */ 7596/* type(I) - the token type to match */ 7597/* uid(I) - uid owning the token */ 7598/* ptr(I) - context pointer for the token */ 7599/* */ 7600/* This function looks for a a token in the current list that matches up */ 7601/* the fields (type, uid, ptr). If none is found, ESRCH is returned, else */ 7602/* call ipf_token_dewref() to remove it from the list. In the event that */ 7603/* the token has a reference held elsewhere, setting ipt_complete to 2 */ 7604/* enables debugging to distinguish between the two paths that ultimately */ 7605/* lead to a token to be deleted. */ 7606/* ------------------------------------------------------------------------ */ 7607int 7608ipf_token_del(softc, type, uid, ptr) 7609 ipf_main_softc_t *softc; 7610 int type, uid; 7611 void *ptr; 7612{ 7613 ipftoken_t *it; 7614 int error; 7615 7616 IPFERROR(82); 7617 error = ESRCH; 7618 7619 WRITE_ENTER(&softc->ipf_tokens); 7620 for (it = softc->ipf_token_head; it != NULL; it = it->ipt_next) { 7621 if (ptr == it->ipt_ctx && type == it->ipt_type && 7622 uid == it->ipt_uid) { 7623 it->ipt_complete = 2; 7624 ipf_token_deref(softc, it); 7625 error = 0; 7626 break; 7627 } 7628 } 7629 RWLOCK_EXIT(&softc->ipf_tokens); 7630 7631 return error; 7632} 7633 7634 7635/* ------------------------------------------------------------------------ */ 7636/* Function: ipf_token_mark_complete */ 7637/* Returns: None. */ 7638/* Parameters: token(I) - pointer to token structure */ 7639/* */ 7640/* Mark a token as being ineligable for being found with ipf_token_find. */ 7641/* ------------------------------------------------------------------------ */ 7642void 7643ipf_token_mark_complete(token) 7644 ipftoken_t *token; 7645{ 7646 if (token->ipt_complete == 0) 7647 token->ipt_complete = 1; 7648} 7649 7650 7651/* ------------------------------------------------------------------------ */ 7652/* Function: ipf_token_find */ 7653/* Returns: ipftoken_t * - NULL if no memory, else pointer to token */ 7654/* Parameters: softc(I)- pointer to soft context main structure */ 7655/* type(I) - the token type to match */ 7656/* uid(I) - uid owning the token */ 7657/* ptr(I) - context pointer for the token */ 7658/* */ 7659/* This function looks for a live token in the list of current tokens that */ 7660/* matches the tuple (type, uid, ptr). If one cannot be found then one is */ 7661/* allocated. If one is found then it is moved to the top of the list of */ 7662/* currently active tokens. */ 7663/* ------------------------------------------------------------------------ */ 7664ipftoken_t * 7665ipf_token_find(softc, type, uid, ptr) 7666 ipf_main_softc_t *softc; 7667 int type, uid; 7668 void *ptr; 7669{ 7670 ipftoken_t *it, *new; 7671 7672 KMALLOC(new, ipftoken_t *); 7673 if (new != NULL) 7674 bzero((char *)new, sizeof(*new)); 7675 7676 WRITE_ENTER(&softc->ipf_tokens); 7677 for (it = softc->ipf_token_head; it != NULL; it = it->ipt_next) { 7678 if ((ptr == it->ipt_ctx) && (type == it->ipt_type) && 7679 (uid == it->ipt_uid) && (it->ipt_complete < 2)) 7680 break; 7681 } 7682 7683 if (it == NULL) { 7684 it = new; 7685 new = NULL; 7686 if (it == NULL) { 7687 RWLOCK_EXIT(&softc->ipf_tokens); 7688 return NULL; 7689 } 7690 it->ipt_ctx = ptr; 7691 it->ipt_uid = uid; 7692 it->ipt_type = type; 7693 it->ipt_ref = 1; 7694 } else { 7695 if (new != NULL) { 7696 KFREE(new); 7697 new = NULL; 7698 } 7699 7700 if (it->ipt_complete > 0) 7701 it = NULL; 7702 else 7703 ipf_token_unlink(softc, it); 7704 } 7705 7706 if (it != NULL) { 7707 it->ipt_pnext = softc->ipf_token_tail; 7708 *softc->ipf_token_tail = it; 7709 softc->ipf_token_tail = &it->ipt_next; 7710 it->ipt_next = NULL; 7711 it->ipt_ref++; 7712 7713 it->ipt_die = softc->ipf_ticks + 20; 7714 } 7715 7716 RWLOCK_EXIT(&softc->ipf_tokens); 7717 7718 return it; 7719} 7720 7721 7722/* ------------------------------------------------------------------------ */ 7723/* Function: ipf_token_unlink */ 7724/* Returns: None. */ 7725/* Parameters: softc(I) - pointer to soft context main structure */ 7726/* token(I) - pointer to token structure */ 7727/* Write Locks: ipf_tokens */ 7728/* */ 7729/* This function unlinks a token structure from the linked list of tokens */ 7730/* that "own" it. The head pointer never needs to be explicitly adjusted */ 7731/* but the tail does due to the linked list implementation. */ 7732/* ------------------------------------------------------------------------ */ 7733static void 7734ipf_token_unlink(softc, token) 7735 ipf_main_softc_t *softc; 7736 ipftoken_t *token; 7737{ 7738 7739 if (softc->ipf_token_tail == &token->ipt_next) 7740 softc->ipf_token_tail = token->ipt_pnext; 7741 7742 *token->ipt_pnext = token->ipt_next; 7743 if (token->ipt_next != NULL) 7744 token->ipt_next->ipt_pnext = token->ipt_pnext; 7745 token->ipt_next = NULL; 7746 token->ipt_pnext = NULL; 7747} 7748 7749 7750/* ------------------------------------------------------------------------ */ 7751/* Function: ipf_token_deref */ 7752/* Returns: int - 0 == token freed, else reference count */ 7753/* Parameters: softc(I) - pointer to soft context main structure */ 7754/* token(I) - pointer to token structure */ 7755/* Write Locks: ipf_tokens */ 7756/* */ 7757/* Drop the reference count on the token structure and if it drops to zero, */ 7758/* call the dereference function for the token type because it is then */ 7759/* possible to free the token data structure. */ 7760/* ------------------------------------------------------------------------ */ 7761int 7762ipf_token_deref(softc, token) 7763 ipf_main_softc_t *softc; 7764 ipftoken_t *token; 7765{ 7766 void *data, **datap; 7767 7768 ASSERT(token->ipt_ref > 0); 7769 token->ipt_ref--; 7770 if (token->ipt_ref > 0) 7771 return token->ipt_ref; 7772 7773 data = token->ipt_data; 7774 datap = &data; 7775 7776 if ((data != NULL) && (data != (void *)-1)) { 7777 switch (token->ipt_type) 7778 { 7779 case IPFGENITER_IPF : 7780 (void) ipf_derefrule(softc, (frentry_t **)datap); 7781 break; 7782 case IPFGENITER_IPNAT : 7783 WRITE_ENTER(&softc->ipf_nat); 7784 ipf_nat_rule_deref(softc, (ipnat_t **)datap); 7785 RWLOCK_EXIT(&softc->ipf_nat); 7786 break; 7787 case IPFGENITER_NAT : 7788 ipf_nat_deref(softc, (nat_t **)datap); 7789 break; 7790 case IPFGENITER_STATE : 7791 ipf_state_deref(softc, (ipstate_t **)datap); 7792 break; 7793 case IPFGENITER_FRAG : 7794 ipf_frag_pkt_deref(softc, (ipfr_t **)datap); 7795 break; 7796 case IPFGENITER_NATFRAG : 7797 ipf_frag_nat_deref(softc, (ipfr_t **)datap); 7798 break; 7799 case IPFGENITER_HOSTMAP : 7800 WRITE_ENTER(&softc->ipf_nat); 7801 ipf_nat_hostmapdel(softc, (hostmap_t **)datap); 7802 RWLOCK_EXIT(&softc->ipf_nat); 7803 break; 7804 default : 7805 ipf_lookup_iterderef(softc, token->ipt_type, data); 7806 break; 7807 } 7808 } 7809 7810 ipf_token_unlink(softc, token); 7811 KFREE(token); 7812 return 0; 7813} 7814 7815 7816/* ------------------------------------------------------------------------ */ 7817/* Function: ipf_nextrule */ 7818/* Returns: frentry_t * - NULL == no more rules, else pointer to next */ 7819/* Parameters: softc(I) - pointer to soft context main structure */ 7820/* fr(I) - pointer to filter rule */ 7821/* out(I) - 1 == out rules, 0 == input rules */ 7822/* */ 7823/* Starting with "fr", find the next rule to visit. This includes visiting */ 7824/* the list of rule groups if either fr is NULL (empty list) or it is the */ 7825/* last rule in the list. When walking rule lists, it is either input or */ 7826/* output rules that are returned, never both. */ 7827/* ------------------------------------------------------------------------ */ 7828static frentry_t * 7829ipf_nextrule(softc, active, unit, fr, out) 7830 ipf_main_softc_t *softc; 7831 int active, unit; 7832 frentry_t *fr; 7833 int out; 7834{ 7835 frentry_t *next; 7836 frgroup_t *fg; 7837 7838 if (fr != NULL && fr->fr_group != -1) { 7839 fg = ipf_findgroup(softc, fr->fr_names + fr->fr_group, 7840 unit, active, NULL); 7841 if (fg != NULL) 7842 fg = fg->fg_next; 7843 } else { 7844 fg = softc->ipf_groups[unit][active]; 7845 } 7846 7847 while (fg != NULL) { 7848 next = fg->fg_start; 7849 while (next != NULL) { 7850 if (out) { 7851 if (next->fr_flags & FR_OUTQUE) 7852 return next; 7853 } else if (next->fr_flags & FR_INQUE) { 7854 return next; 7855 } 7856 next = next->fr_next; 7857 } 7858 if (next == NULL) 7859 fg = fg->fg_next; 7860 } 7861 7862 return NULL; 7863} 7864 7865/* ------------------------------------------------------------------------ */ 7866/* Function: ipf_getnextrule */ 7867/* Returns: int - 0 = success, else error */ 7868/* Parameters: softc(I)- pointer to soft context main structure */ 7869/* t(I) - pointer to destination information to resolve */ 7870/* ptr(I) - pointer to ipfobj_t to copyin from user space */ 7871/* */ 7872/* This function's first job is to bring in the ipfruleiter_t structure via */ 7873/* the ipfobj_t structure to determine what should be the next rule to */ 7874/* return. Once the ipfruleiter_t has been brought in, it then tries to */ 7875/* find the 'next rule'. This may include searching rule group lists or */ 7876/* just be as simple as looking at the 'next' field in the rule structure. */ 7877/* When we have found the rule to return, increase its reference count and */ 7878/* if we used an existing rule to get here, decrease its reference count. */ 7879/* ------------------------------------------------------------------------ */ 7880int 7881ipf_getnextrule(softc, t, ptr) 7882 ipf_main_softc_t *softc; 7883 ipftoken_t *t; 7884 void *ptr; 7885{ 7886 frentry_t *fr, *next, zero; 7887 ipfruleiter_t it; 7888 int error, out; 7889 frgroup_t *fg; 7890 ipfobj_t obj; 7891 int predict; 7892 char *dst; 7893 int unit; 7894 7895 if (t == NULL || ptr == NULL) { 7896 IPFERROR(84); 7897 return EFAULT; 7898 } 7899 7900 error = ipf_inobj(softc, ptr, &obj, &it, IPFOBJ_IPFITER); 7901 if (error != 0) 7902 return error; 7903 7904 if ((it.iri_inout < 0) || (it.iri_inout > 3)) { 7905 IPFERROR(85); 7906 return EINVAL; 7907 } 7908 if ((it.iri_active != 0) && (it.iri_active != 1)) { 7909 IPFERROR(86); 7910 return EINVAL; 7911 } 7912 if (it.iri_nrules == 0) { 7913 IPFERROR(87); 7914 return ENOSPC; 7915 } 7916 if (it.iri_rule == NULL) { 7917 IPFERROR(88); 7918 return EFAULT; 7919 } 7920 7921 fg = NULL; 7922 fr = t->ipt_data; 7923 if ((it.iri_inout & F_OUT) != 0) 7924 out = 1; 7925 else 7926 out = 0; 7927 if ((it.iri_inout & F_ACIN) != 0) 7928 unit = IPL_LOGCOUNT; 7929 else 7930 unit = IPL_LOGIPF; 7931 7932 READ_ENTER(&softc->ipf_mutex); 7933 if (fr == NULL) { 7934 if (*it.iri_group == '\0') { 7935 if (unit == IPL_LOGCOUNT) { 7936 next = softc->ipf_acct[out][it.iri_active]; 7937 } else { 7938 next = softc->ipf_rules[out][it.iri_active]; 7939 } 7940 if (next == NULL) 7941 next = ipf_nextrule(softc, it.iri_active, 7942 unit, NULL, out); 7943 } else { 7944 fg = ipf_findgroup(softc, it.iri_group, unit, 7945 it.iri_active, NULL); 7946 if (fg != NULL) 7947 next = fg->fg_start; 7948 else 7949 next = NULL; 7950 } 7951 } else { 7952 next = fr->fr_next; 7953 if (next == NULL) 7954 next = ipf_nextrule(softc, it.iri_active, unit, 7955 fr, out); 7956 } 7957 7958 if (next != NULL && next->fr_next != NULL) 7959 predict = 1; 7960 else if (ipf_nextrule(softc, it.iri_active, unit, next, out) != NULL) 7961 predict = 1; 7962 else 7963 predict = 0; 7964 7965 if (fr != NULL) 7966 (void) ipf_derefrule(softc, &fr); 7967 7968 obj.ipfo_type = IPFOBJ_FRENTRY; 7969 dst = (char *)it.iri_rule; 7970 7971 if (next != NULL) { 7972 obj.ipfo_size = next->fr_size; 7973 MUTEX_ENTER(&next->fr_lock); 7974 next->fr_ref++; 7975 MUTEX_EXIT(&next->fr_lock); 7976 t->ipt_data = next; 7977 } else { 7978 obj.ipfo_size = sizeof(frentry_t); 7979 bzero(&zero, sizeof(zero)); 7980 next = &zero; 7981 t->ipt_data = NULL; 7982 } 7983 it.iri_rule = predict ? next : NULL; 7984 if (predict == 0) 7985 ipf_token_mark_complete(t); 7986 7987 RWLOCK_EXIT(&softc->ipf_mutex); 7988 7989 obj.ipfo_ptr = dst; 7990 error = ipf_outobjk(softc, &obj, next); 7991 if (error == 0 && t->ipt_data != NULL) { 7992 dst += obj.ipfo_size; 7993 if (next->fr_data != NULL) { 7994 ipfobj_t dobj; 7995 7996 if (next->fr_type == FR_T_IPFEXPR) 7997 dobj.ipfo_type = IPFOBJ_IPFEXPR; 7998 else 7999 dobj.ipfo_type = IPFOBJ_FRIPF; 8000 dobj.ipfo_size = next->fr_dsize; 8001 dobj.ipfo_rev = obj.ipfo_rev; 8002 dobj.ipfo_ptr = dst; 8003 error = ipf_outobjk(softc, &dobj, next->fr_data); 8004 } 8005 } 8006 8007 if ((fr != NULL) && (next == &zero)) 8008 (void) ipf_derefrule(softc, &fr); 8009 8010 return error; 8011} 8012 8013 8014/* ------------------------------------------------------------------------ */ 8015/* Function: ipf_frruleiter */ 8016/* Returns: int - 0 = success, else error */ 8017/* Parameters: softc(I)- pointer to soft context main structure */ 8018/* data(I) - the token type to match */ 8019/* uid(I) - uid owning the token */ 8020/* ptr(I) - context pointer for the token */ 8021/* */ 8022/* This function serves as a stepping stone between ipf_ipf_ioctl and */ 8023/* ipf_getnextrule. It's role is to find the right token in the kernel for */ 8024/* the process doing the ioctl and use that to ask for the next rule. */ 8025/* ------------------------------------------------------------------------ */ 8026static int 8027ipf_frruleiter(softc, data, uid, ctx) 8028 ipf_main_softc_t *softc; 8029 void *data, *ctx; 8030 int uid; 8031{ 8032 ipftoken_t *token; 8033 ipfruleiter_t it; 8034 ipfobj_t obj; 8035 int error; 8036 8037 token = ipf_token_find(softc, IPFGENITER_IPF, uid, ctx); 8038 if (token != NULL) { 8039 error = ipf_getnextrule(softc, token, data); 8040 WRITE_ENTER(&softc->ipf_tokens); 8041 ipf_token_deref(softc, token); 8042 RWLOCK_EXIT(&softc->ipf_tokens); 8043 } else { 8044 error = ipf_inobj(softc, data, &obj, &it, IPFOBJ_IPFITER); 8045 if (error != 0) 8046 return error; 8047 it.iri_rule = NULL; 8048 error = ipf_outobj(softc, data, &it, IPFOBJ_IPFITER); 8049 } 8050 8051 return error; 8052} 8053 8054 8055/* ------------------------------------------------------------------------ */ 8056/* Function: ipf_geniter */ 8057/* Returns: int - 0 = success, else error */ 8058/* Parameters: softc(I) - pointer to soft context main structure */ 8059/* token(I) - pointer to ipftoken_t structure */ 8060/* itp(I) - pointer to iterator data */ 8061/* */ 8062/* Decide which iterator function to call using information passed through */ 8063/* the ipfgeniter_t structure at itp. */ 8064/* ------------------------------------------------------------------------ */ 8065static int 8066ipf_geniter(softc, token, itp) 8067 ipf_main_softc_t *softc; 8068 ipftoken_t *token; 8069 ipfgeniter_t *itp; 8070{ 8071 int error; 8072 8073 switch (itp->igi_type) 8074 { 8075 case IPFGENITER_FRAG : 8076 error = ipf_frag_pkt_next(softc, token, itp); 8077 break; 8078 default : 8079 IPFERROR(92); 8080 error = EINVAL; 8081 break; 8082 } 8083 8084 return error; 8085} 8086 8087 8088/* ------------------------------------------------------------------------ */ 8089/* Function: ipf_genericiter */ 8090/* Returns: int - 0 = success, else error */ 8091/* Parameters: softc(I)- pointer to soft context main structure */ 8092/* data(I) - the token type to match */ 8093/* uid(I) - uid owning the token */ 8094/* ptr(I) - context pointer for the token */ 8095/* */ 8096/* Handle the SIOCGENITER ioctl for the ipfilter device. The primary role */ 8097/* ------------------------------------------------------------------------ */ 8098int 8099ipf_genericiter(softc, data, uid, ctx) 8100 ipf_main_softc_t *softc; 8101 void *data, *ctx; 8102 int uid; 8103{ 8104 ipftoken_t *token; 8105 ipfgeniter_t iter; 8106 int error; 8107 8108 error = ipf_inobj(softc, data, NULL, &iter, IPFOBJ_GENITER); 8109 if (error != 0) 8110 return error; 8111 8112 token = ipf_token_find(softc, iter.igi_type, uid, ctx); 8113 if (token != NULL) { 8114 token->ipt_subtype = iter.igi_type; 8115 error = ipf_geniter(softc, token, &iter); 8116 WRITE_ENTER(&softc->ipf_tokens); 8117 ipf_token_deref(softc, token); 8118 RWLOCK_EXIT(&softc->ipf_tokens); 8119 } else { 8120 IPFERROR(93); 8121 error = 0; 8122 } 8123 8124 return error; 8125} 8126 8127 8128/* ------------------------------------------------------------------------ */ 8129/* Function: ipf_ipf_ioctl */ 8130/* Returns: int - 0 = success, else error */ 8131/* Parameters: softc(I)- pointer to soft context main structure */ 8132/* data(I) - the token type to match */ 8133/* cmd(I) - the ioctl command number */ 8134/* mode(I) - mode flags for the ioctl */ 8135/* uid(I) - uid owning the token */ 8136/* ptr(I) - context pointer for the token */ 8137/* */ 8138/* This function handles all of the ioctl command that are actually isssued */ 8139/* to the /dev/ipl device. */ 8140/* ------------------------------------------------------------------------ */ 8141int 8142ipf_ipf_ioctl(softc, data, cmd, mode, uid, ctx) 8143 ipf_main_softc_t *softc; 8144 caddr_t data; 8145 ioctlcmd_t cmd; 8146 int mode, uid; 8147 void *ctx; 8148{ 8149 friostat_t fio; 8150 int error, tmp; 8151 ipfobj_t obj; 8152 SPL_INT(s); 8153 8154 switch (cmd) 8155 { 8156 case SIOCFRENB : 8157 if (!(mode & FWRITE)) { 8158 IPFERROR(94); 8159 error = EPERM; 8160 } else { 8161 error = BCOPYIN(data, &tmp, sizeof(tmp)); 8162 if (error != 0) { 8163 IPFERROR(95); 8164 error = EFAULT; 8165 break; 8166 } 8167 8168 WRITE_ENTER(&softc->ipf_global); 8169 if (tmp) { 8170 if (softc->ipf_running > 0) 8171 error = 0; 8172 else 8173 error = ipfattach(softc); 8174 if (error == 0) 8175 softc->ipf_running = 1; 8176 else 8177 (void) ipfdetach(softc); 8178 } else { 8179 if (softc->ipf_running == 1) 8180 error = ipfdetach(softc); 8181 else 8182 error = 0; 8183 if (error == 0) 8184 softc->ipf_running = -1; 8185 } 8186 RWLOCK_EXIT(&softc->ipf_global); 8187 } 8188 break; 8189 8190 case SIOCIPFSET : 8191 if (!(mode & FWRITE)) { 8192 IPFERROR(96); 8193 error = EPERM; 8194 break; 8195 } 8196 /* FALLTHRU */ 8197 case SIOCIPFGETNEXT : 8198 case SIOCIPFGET : 8199 error = ipf_ipftune(softc, cmd, (void *)data); 8200 break; 8201 8202 case SIOCSETFF : 8203 if (!(mode & FWRITE)) { 8204 IPFERROR(97); 8205 error = EPERM; 8206 } else { 8207 error = BCOPYIN(data, &softc->ipf_flags, 8208 sizeof(softc->ipf_flags)); 8209 if (error != 0) { 8210 IPFERROR(98); 8211 error = EFAULT; 8212 } 8213 } 8214 break; 8215 8216 case SIOCGETFF : 8217 error = BCOPYOUT(&softc->ipf_flags, data, 8218 sizeof(softc->ipf_flags)); 8219 if (error != 0) { 8220 IPFERROR(99); 8221 error = EFAULT; 8222 } 8223 break; 8224 8225 case SIOCFUNCL : 8226 error = ipf_resolvefunc(softc, (void *)data); 8227 break; 8228 8229 case SIOCINAFR : 8230 case SIOCRMAFR : 8231 case SIOCADAFR : 8232 case SIOCZRLST : 8233 if (!(mode & FWRITE)) { 8234 IPFERROR(100); 8235 error = EPERM; 8236 } else { 8237 error = frrequest(softc, IPL_LOGIPF, cmd, (caddr_t)data, 8238 softc->ipf_active, 1); 8239 } 8240 break; 8241 8242 case SIOCINIFR : 8243 case SIOCRMIFR : 8244 case SIOCADIFR : 8245 if (!(mode & FWRITE)) { 8246 IPFERROR(101); 8247 error = EPERM; 8248 } else { 8249 error = frrequest(softc, IPL_LOGIPF, cmd, (caddr_t)data, 8250 1 - softc->ipf_active, 1); 8251 } 8252 break; 8253 8254 case SIOCSWAPA : 8255 if (!(mode & FWRITE)) { 8256 IPFERROR(102); 8257 error = EPERM; 8258 } else { 8259 WRITE_ENTER(&softc->ipf_mutex); 8260 error = BCOPYOUT(&softc->ipf_active, data, 8261 sizeof(softc->ipf_active)); 8262 if (error != 0) { 8263 IPFERROR(103); 8264 error = EFAULT; 8265 } else { 8266 softc->ipf_active = 1 - softc->ipf_active; 8267 } 8268 RWLOCK_EXIT(&softc->ipf_mutex); 8269 } 8270 break; 8271 8272 case SIOCGETFS : 8273 error = ipf_inobj(softc, (void *)data, &obj, &fio, 8274 IPFOBJ_IPFSTAT); 8275 if (error != 0) 8276 break; 8277 ipf_getstat(softc, &fio, obj.ipfo_rev); 8278 error = ipf_outobj(softc, (void *)data, &fio, IPFOBJ_IPFSTAT); 8279 break; 8280 8281 case SIOCFRZST : 8282 if (!(mode & FWRITE)) { 8283 IPFERROR(104); 8284 error = EPERM; 8285 } else 8286 error = ipf_zerostats(softc, (caddr_t)data); 8287 break; 8288 8289 case SIOCIPFFL : 8290 if (!(mode & FWRITE)) { 8291 IPFERROR(105); 8292 error = EPERM; 8293 } else { 8294 error = BCOPYIN(data, &tmp, sizeof(tmp)); 8295 if (!error) { 8296 tmp = ipf_flush(softc, IPL_LOGIPF, tmp); 8297 error = BCOPYOUT(&tmp, data, sizeof(tmp)); 8298 if (error != 0) { 8299 IPFERROR(106); 8300 error = EFAULT; 8301 } 8302 } else { 8303 IPFERROR(107); 8304 error = EFAULT; 8305 } 8306 } 8307 break; 8308 8309#ifdef USE_INET6 8310 case SIOCIPFL6 : 8311 if (!(mode & FWRITE)) { 8312 IPFERROR(108); 8313 error = EPERM; 8314 } else { 8315 error = BCOPYIN(data, &tmp, sizeof(tmp)); 8316 if (!error) { 8317 tmp = ipf_flush(softc, IPL_LOGIPF, tmp); 8318 error = BCOPYOUT(&tmp, data, sizeof(tmp)); 8319 if (error != 0) { 8320 IPFERROR(109); 8321 error = EFAULT; 8322 } 8323 } else { 8324 IPFERROR(110); 8325 error = EFAULT; 8326 } 8327 } 8328 break; 8329#endif 8330 8331 case SIOCSTLCK : 8332 if (!(mode & FWRITE)) { 8333 IPFERROR(122); 8334 error = EPERM; 8335 } else { 8336 error = BCOPYIN(data, &tmp, sizeof(tmp)); 8337 if (error == 0) { 8338 ipf_state_setlock(softc->ipf_state_soft, tmp); 8339 ipf_nat_setlock(softc->ipf_nat_soft, tmp); 8340 ipf_frag_setlock(softc->ipf_frag_soft, tmp); 8341 ipf_auth_setlock(softc->ipf_auth_soft, tmp); 8342 } else { 8343 IPFERROR(111); 8344 error = EFAULT; 8345 } 8346 } 8347 break; 8348 8349#ifdef IPFILTER_LOG 8350 case SIOCIPFFB : 8351 if (!(mode & FWRITE)) { 8352 IPFERROR(112); 8353 error = EPERM; 8354 } else { 8355 tmp = ipf_log_clear(softc, IPL_LOGIPF); 8356 error = BCOPYOUT(&tmp, data, sizeof(tmp)); 8357 if (error) { 8358 IPFERROR(113); 8359 error = EFAULT; 8360 } 8361 } 8362 break; 8363#endif /* IPFILTER_LOG */ 8364 8365 case SIOCFRSYN : 8366 if (!(mode & FWRITE)) { 8367 IPFERROR(114); 8368 error = EPERM; 8369 } else { 8370 WRITE_ENTER(&softc->ipf_global); 8371#if (defined(MENTAT) && defined(_KERNEL)) && !defined(INSTANCES) 8372 error = ipfsync(); 8373#else 8374 ipf_sync(softc, NULL); 8375 error = 0; 8376#endif 8377 RWLOCK_EXIT(&softc->ipf_global); 8378 8379 } 8380 break; 8381 8382 case SIOCGFRST : 8383 error = ipf_outobj(softc, (void *)data, 8384 ipf_frag_stats(softc->ipf_frag_soft), 8385 IPFOBJ_FRAGSTAT); 8386 break; 8387 8388#ifdef IPFILTER_LOG 8389 case FIONREAD : 8390 tmp = ipf_log_bytesused(softc, IPL_LOGIPF); 8391 error = BCOPYOUT(&tmp, data, sizeof(tmp)); 8392 break; 8393#endif 8394 8395 case SIOCIPFITER : 8396 SPL_SCHED(s); 8397 error = ipf_frruleiter(softc, data, uid, ctx); 8398 SPL_X(s); 8399 break; 8400 8401 case SIOCGENITER : 8402 SPL_SCHED(s); 8403 error = ipf_genericiter(softc, data, uid, ctx); 8404 SPL_X(s); 8405 break; 8406 8407 case SIOCIPFDELTOK : 8408 error = BCOPYIN(data, &tmp, sizeof(tmp)); 8409 if (error == 0) { 8410 SPL_SCHED(s); 8411 error = ipf_token_del(softc, tmp, uid, ctx); 8412 SPL_X(s); 8413 } 8414 break; 8415 8416 default : 8417 IPFERROR(115); 8418 error = EINVAL; 8419 break; 8420 } 8421 8422 return error; 8423} 8424 8425 8426/* ------------------------------------------------------------------------ */ 8427/* Function: ipf_decaps */ 8428/* Returns: int - -1 == decapsulation failed, else bit mask of */ 8429/* flags indicating packet filtering decision. */ 8430/* Parameters: fin(I) - pointer to packet information */ 8431/* pass(I) - IP protocol version to match */ 8432/* l5proto(I) - layer 5 protocol to decode UDP data as. */ 8433/* */ 8434/* This function is called for packets that are wrapt up in other packets, */ 8435/* for example, an IP packet that is the entire data segment for another IP */ 8436/* packet. If the basic constraints for this are satisfied, change the */ 8437/* buffer to point to the start of the inner packet and start processing */ 8438/* rules belonging to the head group this rule specifies. */ 8439/* ------------------------------------------------------------------------ */ 8440u_32_t 8441ipf_decaps(fin, pass, l5proto) 8442 fr_info_t *fin; 8443 u_32_t pass; 8444 int l5proto; 8445{ 8446 fr_info_t fin2, *fino = NULL; 8447 int elen, hlen, nh; 8448 grehdr_t gre; 8449 ip_t *ip; 8450 mb_t *m; 8451 8452 if ((fin->fin_flx & FI_COALESCE) == 0) 8453 if (ipf_coalesce(fin) == -1) 8454 goto cantdecaps; 8455 8456 m = fin->fin_m; 8457 hlen = fin->fin_hlen; 8458 8459 switch (fin->fin_p) 8460 { 8461 case IPPROTO_UDP : 8462 /* 8463 * In this case, the specific protocol being decapsulated 8464 * inside UDP frames comes from the rule. 8465 */ 8466 nh = fin->fin_fr->fr_icode; 8467 break; 8468 8469 case IPPROTO_GRE : /* 47 */ 8470 bcopy(fin->fin_dp, (char *)&gre, sizeof(gre)); 8471 hlen += sizeof(grehdr_t); 8472 if (gre.gr_R|gre.gr_s) 8473 goto cantdecaps; 8474 if (gre.gr_C) 8475 hlen += 4; 8476 if (gre.gr_K) 8477 hlen += 4; 8478 if (gre.gr_S) 8479 hlen += 4; 8480 8481 nh = IPPROTO_IP; 8482 8483 /* 8484 * If the routing options flag is set, validate that it is 8485 * there and bounce over it. 8486 */ 8487#if 0 8488 /* This is really heavy weight and lots of room for error, */ 8489 /* so for now, put it off and get the simple stuff right. */ 8490 if (gre.gr_R) { 8491 u_char off, len, *s; 8492 u_short af; 8493 int end; 8494 8495 end = 0; 8496 s = fin->fin_dp; 8497 s += hlen; 8498 aplen = fin->fin_plen - hlen; 8499 while (aplen > 3) { 8500 af = (s[0] << 8) | s[1]; 8501 off = s[2]; 8502 len = s[3]; 8503 aplen -= 4; 8504 s += 4; 8505 if (af == 0 && len == 0) { 8506 end = 1; 8507 break; 8508 } 8509 if (aplen < len) 8510 break; 8511 s += len; 8512 aplen -= len; 8513 } 8514 if (end != 1) 8515 goto cantdecaps; 8516 hlen = s - (u_char *)fin->fin_dp; 8517 } 8518#endif 8519 break; 8520 8521#ifdef IPPROTO_IPIP 8522 case IPPROTO_IPIP : /* 4 */ 8523#endif 8524 nh = IPPROTO_IP; 8525 break; 8526 8527 default : /* Includes ESP, AH is special for IPv4 */ 8528 goto cantdecaps; 8529 } 8530 8531 switch (nh) 8532 { 8533 case IPPROTO_IP : 8534 case IPPROTO_IPV6 : 8535 break; 8536 default : 8537 goto cantdecaps; 8538 } 8539 8540 bcopy((char *)fin, (char *)&fin2, sizeof(fin2)); 8541 fino = fin; 8542 fin = &fin2; 8543 elen = hlen; 8544#if defined(MENTAT) && defined(_KERNEL) 8545 m->b_rptr += elen; 8546#else 8547 m->m_data += elen; 8548 m->m_len -= elen; 8549#endif 8550 fin->fin_plen -= elen; 8551 8552 ip = (ip_t *)((char *)fin->fin_ip + elen); 8553 8554 /* 8555 * Make sure we have at least enough data for the network layer 8556 * header. 8557 */ 8558 if (IP_V(ip) == 4) 8559 hlen = IP_HL(ip) << 2; 8560#ifdef USE_INET6 8561 else if (IP_V(ip) == 6) 8562 hlen = sizeof(ip6_t); 8563#endif 8564 else 8565 goto cantdecaps2; 8566 8567 if (fin->fin_plen < hlen) 8568 goto cantdecaps2; 8569 8570 fin->fin_dp = (char *)ip + hlen; 8571 8572 if (IP_V(ip) == 4) { 8573 /* 8574 * Perform IPv4 header checksum validation. 8575 */ 8576 if (ipf_cksum((u_short *)ip, hlen)) 8577 goto cantdecaps2; 8578 } 8579 8580 if (ipf_makefrip(hlen, ip, fin) == -1) { 8581cantdecaps2: 8582 if (m != NULL) { 8583#if defined(MENTAT) && defined(_KERNEL) 8584 m->b_rptr -= elen; 8585#else 8586 m->m_data -= elen; 8587 m->m_len += elen; 8588#endif 8589 } 8590cantdecaps: 8591 DT1(frb_decapfrip, fr_info_t *, fin); 8592 pass &= ~FR_CMDMASK; 8593 pass |= FR_BLOCK|FR_QUICK; 8594 fin->fin_reason = FRB_DECAPFRIP; 8595 return -1; 8596 } 8597 8598 pass = ipf_scanlist(fin, pass); 8599 8600 /* 8601 * Copy the packet filter "result" fields out of the fr_info_t struct 8602 * that is local to the decapsulation processing and back into the 8603 * one we were called with. 8604 */ 8605 fino->fin_flx = fin->fin_flx; 8606 fino->fin_rev = fin->fin_rev; 8607 fino->fin_icode = fin->fin_icode; 8608 fino->fin_rule = fin->fin_rule; 8609 (void) strncpy(fino->fin_group, fin->fin_group, FR_GROUPLEN); 8610 fino->fin_fr = fin->fin_fr; 8611 fino->fin_error = fin->fin_error; 8612 fino->fin_mp = fin->fin_mp; 8613 fino->fin_m = fin->fin_m; 8614 m = fin->fin_m; 8615 if (m != NULL) { 8616#if defined(MENTAT) && defined(_KERNEL) 8617 m->b_rptr -= elen; 8618#else 8619 m->m_data -= elen; 8620 m->m_len += elen; 8621#endif 8622 } 8623 return pass; 8624} 8625 8626 8627/* ------------------------------------------------------------------------ */ 8628/* Function: ipf_matcharray_load */ 8629/* Returns: int - 0 = success, else error */ 8630/* Parameters: softc(I) - pointer to soft context main structure */ 8631/* data(I) - pointer to ioctl data */ 8632/* objp(I) - ipfobj_t structure to load data into */ 8633/* arrayptr(I) - pointer to location to store array pointer */ 8634/* */ 8635/* This function loads in a mathing array through the ipfobj_t struct that */ 8636/* describes it. Sanity checking and array size limitations are enforced */ 8637/* in this function to prevent userspace from trying to load in something */ 8638/* that is insanely big. Once the size of the array is known, the memory */ 8639/* required is malloc'd and returned through changing *arrayptr. The */ 8640/* contents of the array are verified before returning. Only in the event */ 8641/* of a successful call is the caller required to free up the malloc area. */ 8642/* ------------------------------------------------------------------------ */ 8643int 8644ipf_matcharray_load(softc, data, objp, arrayptr) 8645 ipf_main_softc_t *softc; 8646 caddr_t data; 8647 ipfobj_t *objp; 8648 int **arrayptr; 8649{ 8650 int arraysize, *array, error; 8651 8652 *arrayptr = NULL; 8653 8654 error = BCOPYIN(data, objp, sizeof(*objp)); 8655 if (error != 0) { 8656 IPFERROR(116); 8657 return EFAULT; 8658 } 8659 8660 if (objp->ipfo_type != IPFOBJ_IPFEXPR) { 8661 IPFERROR(117); 8662 return EINVAL; 8663 } 8664 8665 if (((objp->ipfo_size & 3) != 0) || (objp->ipfo_size == 0) || 8666 (objp->ipfo_size > 1024)) { 8667 IPFERROR(118); 8668 return EINVAL; 8669 } 8670 8671 arraysize = objp->ipfo_size * sizeof(*array); 8672 KMALLOCS(array, int *, arraysize); 8673 if (array == NULL) { 8674 IPFERROR(119); 8675 return ENOMEM; 8676 } 8677 8678 error = COPYIN(objp->ipfo_ptr, array, arraysize); 8679 if (error != 0) { 8680 KFREES(array, arraysize); 8681 IPFERROR(120); 8682 return EFAULT; 8683 } 8684 8685 if (ipf_matcharray_verify(array, arraysize) != 0) { 8686 KFREES(array, arraysize); 8687 IPFERROR(121); 8688 return EINVAL; 8689 } 8690 8691 *arrayptr = array; 8692 return 0; 8693} 8694 8695 8696/* ------------------------------------------------------------------------ */ 8697/* Function: ipf_matcharray_verify */ 8698/* Returns: Nil */ 8699/* Parameters: array(I) - pointer to matching array */ 8700/* arraysize(I) - number of elements in the array */ 8701/* */ 8702/* Verify the contents of a matching array by stepping through each element */ 8703/* in it. The actual commands in the array are not verified for */ 8704/* correctness, only that all of the sizes are correctly within limits. */ 8705/* ------------------------------------------------------------------------ */ 8706int 8707ipf_matcharray_verify(array, arraysize) 8708 int *array, arraysize; 8709{ 8710 int i, nelem, maxidx; 8711 ipfexp_t *e; 8712 8713 nelem = arraysize / sizeof(*array); 8714 8715 /* 8716 * Currently, it makes no sense to have an array less than 6 8717 * elements long - the initial size at the from, a single operation 8718 * (minimum 4 in length) and a trailer, for a total of 6. 8719 */ 8720 if ((array[0] < 6) || (arraysize < 24) || (arraysize > 4096)) { 8721 return -1; 8722 } 8723 8724 /* 8725 * Verify the size of data pointed to by array with how long 8726 * the array claims to be itself. 8727 */ 8728 if (array[0] * sizeof(*array) != arraysize) { 8729 return -1; 8730 } 8731 8732 maxidx = nelem - 1; 8733 /* 8734 * The last opcode in this array should be an IPF_EXP_END. 8735 */ 8736 if (array[maxidx] != IPF_EXP_END) { 8737 return -1; 8738 } 8739 8740 for (i = 1; i < maxidx; ) { 8741 e = (ipfexp_t *)(array + i); 8742 8743 /* 8744 * The length of the bits to check must be at least 1 8745 * (or else there is nothing to comapre with!) and it 8746 * cannot exceed the length of the data present. 8747 */ 8748 if ((e->ipfe_size < 1 ) || 8749 (e->ipfe_size + i > maxidx)) { 8750 return -1; 8751 } 8752 i += e->ipfe_size; 8753 } 8754 return 0; 8755} 8756 8757 8758/* ------------------------------------------------------------------------ */ 8759/* Function: ipf_fr_matcharray */ 8760/* Returns: int - 0 = match failed, else positive match */ 8761/* Parameters: fin(I) - pointer to packet information */ 8762/* array(I) - pointer to matching array */ 8763/* */ 8764/* This function is used to apply a matching array against a packet and */ 8765/* return an indication of whether or not the packet successfully matches */ 8766/* all of the commands in it. */ 8767/* ------------------------------------------------------------------------ */ 8768static int 8769ipf_fr_matcharray(fin, array) 8770 fr_info_t *fin; 8771 int *array; 8772{ 8773 int i, n, *x, rv, p; 8774 ipfexp_t *e; 8775 8776 rv = 0; 8777 n = array[0]; 8778 x = array + 1; 8779 8780 for (; n > 0; x += 3 + x[3], rv = 0) { 8781 e = (ipfexp_t *)x; 8782 if (e->ipfe_cmd == IPF_EXP_END) 8783 break; 8784 n -= e->ipfe_size; 8785 8786 /* 8787 * The upper 16 bits currently store the protocol value. 8788 * This is currently used with TCP and UDP port compares and 8789 * allows "tcp.port = 80" without requiring an explicit 8790 " "ip.pr = tcp" first. 8791 */ 8792 p = e->ipfe_cmd >> 16; 8793 if ((p != 0) && (p != fin->fin_p)) 8794 break; 8795 8796 switch (e->ipfe_cmd) 8797 { 8798 case IPF_EXP_IP_PR : 8799 for (i = 0; !rv && i < e->ipfe_narg; i++) { 8800 rv |= (fin->fin_p == e->ipfe_arg0[i]); 8801 } 8802 break; 8803 8804 case IPF_EXP_IP_SRCADDR : 8805 if (fin->fin_v != 4) 8806 break; 8807 for (i = 0; !rv && i < e->ipfe_narg; i++) { 8808 rv |= ((fin->fin_saddr & 8809 e->ipfe_arg0[i * 2 + 1]) == 8810 e->ipfe_arg0[i * 2]); 8811 } 8812 break; 8813 8814 case IPF_EXP_IP_DSTADDR : 8815 if (fin->fin_v != 4) 8816 break; 8817 for (i = 0; !rv && i < e->ipfe_narg; i++) { 8818 rv |= ((fin->fin_daddr & 8819 e->ipfe_arg0[i * 2 + 1]) == 8820 e->ipfe_arg0[i * 2]); 8821 } 8822 break; 8823 8824 case IPF_EXP_IP_ADDR : 8825 if (fin->fin_v != 4) 8826 break; 8827 for (i = 0; !rv && i < e->ipfe_narg; i++) { 8828 rv |= ((fin->fin_saddr & 8829 e->ipfe_arg0[i * 2 + 1]) == 8830 e->ipfe_arg0[i * 2]) || 8831 ((fin->fin_daddr & 8832 e->ipfe_arg0[i * 2 + 1]) == 8833 e->ipfe_arg0[i * 2]); 8834 } 8835 break; 8836 8837#ifdef USE_INET6 8838 case IPF_EXP_IP6_SRCADDR : 8839 if (fin->fin_v != 6) 8840 break; 8841 for (i = 0; !rv && i < e->ipfe_narg; i++) { 8842 rv |= IP6_MASKEQ(&fin->fin_src6, 8843 &e->ipfe_arg0[i * 8 + 4], 8844 &e->ipfe_arg0[i * 8]); 8845 } 8846 break; 8847 8848 case IPF_EXP_IP6_DSTADDR : 8849 if (fin->fin_v != 6) 8850 break; 8851 for (i = 0; !rv && i < e->ipfe_narg; i++) { 8852 rv |= IP6_MASKEQ(&fin->fin_dst6, 8853 &e->ipfe_arg0[i * 8 + 4], 8854 &e->ipfe_arg0[i * 8]); 8855 } 8856 break; 8857 8858 case IPF_EXP_IP6_ADDR : 8859 if (fin->fin_v != 6) 8860 break; 8861 for (i = 0; !rv && i < e->ipfe_narg; i++) { 8862 rv |= IP6_MASKEQ(&fin->fin_src6, 8863 &e->ipfe_arg0[i * 8 + 4], 8864 &e->ipfe_arg0[i * 8]) || 8865 IP6_MASKEQ(&fin->fin_dst6, 8866 &e->ipfe_arg0[i * 8 + 4], 8867 &e->ipfe_arg0[i * 8]); 8868 } 8869 break; 8870#endif 8871 8872 case IPF_EXP_UDP_PORT : 8873 case IPF_EXP_TCP_PORT : 8874 for (i = 0; !rv && i < e->ipfe_narg; i++) { 8875 rv |= (fin->fin_sport == e->ipfe_arg0[i]) || 8876 (fin->fin_dport == e->ipfe_arg0[i]); 8877 } 8878 break; 8879 8880 case IPF_EXP_UDP_SPORT : 8881 case IPF_EXP_TCP_SPORT : 8882 for (i = 0; !rv && i < e->ipfe_narg; i++) { 8883 rv |= (fin->fin_sport == e->ipfe_arg0[i]); 8884 } 8885 break; 8886 8887 case IPF_EXP_UDP_DPORT : 8888 case IPF_EXP_TCP_DPORT : 8889 for (i = 0; !rv && i < e->ipfe_narg; i++) { 8890 rv |= (fin->fin_dport == e->ipfe_arg0[i]); 8891 } 8892 break; 8893 8894 case IPF_EXP_TCP_FLAGS : 8895 for (i = 0; !rv && i < e->ipfe_narg; i++) { 8896 rv |= ((fin->fin_tcpf & 8897 e->ipfe_arg0[i * 2 + 1]) == 8898 e->ipfe_arg0[i * 2]); 8899 } 8900 break; 8901 } 8902 rv ^= e->ipfe_not; 8903 8904 if (rv == 0) 8905 break; 8906 } 8907 8908 return rv; 8909} 8910 8911 8912/* ------------------------------------------------------------------------ */ 8913/* Function: ipf_queueflush */ 8914/* Returns: int - number of entries flushed (0 = none) */ 8915/* Parameters: softc(I) - pointer to soft context main structure */ 8916/* deletefn(I) - function to call to delete entry */ 8917/* ipfqs(I) - top of the list of ipf internal queues */ 8918/* userqs(I) - top of the list of user defined timeouts */ 8919/* */ 8920/* This fucntion gets called when the state/NAT hash tables fill up and we */ 8921/* need to try a bit harder to free up some space. The algorithm used here */ 8922/* split into two parts but both halves have the same goal: to reduce the */ 8923/* number of connections considered to be "active" to the low watermark. */ 8924/* There are two steps in doing this: */ 8925/* 1) Remove any TCP connections that are already considered to be "closed" */ 8926/* but have not yet been removed from the state table. The two states */ 8927/* TCPS_TIME_WAIT and TCPS_CLOSED are considered to be the perfect */ 8928/* candidates for this style of removal. If freeing up entries in */ 8929/* CLOSED or both CLOSED and TIME_WAIT brings us to the low watermark, */ 8930/* we do not go on to step 2. */ 8931/* */ 8932/* 2) Look for the oldest entries on each timeout queue and free them if */ 8933/* they are within the given window we are considering. Where the */ 8934/* window starts and the steps taken to increase its size depend upon */ 8935/* how long ipf has been running (ipf_ticks.) Anything modified in the */ 8936/* last 30 seconds is not touched. */ 8937/* touched */ 8938/* die ipf_ticks 30*1.5 1800*1.5 | 43200*1.5 */ 8939/* | | | | | | */ 8940/* future <--+----------+--------+-----------+-----+-----+-----------> past */ 8941/* now \_int=30s_/ \_int=1hr_/ \_int=12hr */ 8942/* */ 8943/* Points to note: */ 8944/* - tqe_die is the time, in the future, when entries die. */ 8945/* - tqe_die - ipf_ticks is how long left the connection has to live in ipf */ 8946/* ticks. */ 8947/* - tqe_touched is when the entry was last used by NAT/state */ 8948/* - the closer tqe_touched is to ipf_ticks, the further tqe_die will be */ 8949/* ipf_ticks any given timeout queue and vice versa. */ 8950/* - both tqe_die and tqe_touched increase over time */ 8951/* - timeout queues are sorted with the highest value of tqe_die at the */ 8952/* bottom and therefore the smallest values of each are at the top */ 8953/* - the pointer passed in as ipfqs should point to an array of timeout */ 8954/* queues representing each of the TCP states */ 8955/* */ 8956/* We start by setting up a maximum range to scan for things to move of */ 8957/* iend (newest) to istart (oldest) in chunks of "interval". If nothing is */ 8958/* found in that range, "interval" is adjusted (so long as it isn't 30) and */ 8959/* we start again with a new value for "iend" and "istart". This is */ 8960/* continued until we either finish the scan of 30 second intervals or the */ 8961/* low water mark is reached. */ 8962/* ------------------------------------------------------------------------ */ 8963int 8964ipf_queueflush(softc, deletefn, ipfqs, userqs, activep, size, low) 8965 ipf_main_softc_t *softc; 8966 ipftq_delete_fn_t deletefn; 8967 ipftq_t *ipfqs, *userqs; 8968 u_int *activep; 8969 int size, low; 8970{ 8971 u_long interval, istart, iend; 8972 ipftq_t *ifq, *ifqnext; 8973 ipftqent_t *tqe, *tqn; 8974 int removed = 0; 8975 8976 for (tqn = ipfqs[IPF_TCPS_CLOSED].ifq_head; ((tqe = tqn) != NULL); ) { 8977 tqn = tqe->tqe_next; 8978 if ((*deletefn)(softc, tqe->tqe_parent) == 0) 8979 removed++; 8980 } 8981 if ((*activep * 100 / size) > low) { 8982 for (tqn = ipfqs[IPF_TCPS_TIME_WAIT].ifq_head; 8983 ((tqe = tqn) != NULL); ) { 8984 tqn = tqe->tqe_next; 8985 if ((*deletefn)(softc, tqe->tqe_parent) == 0) 8986 removed++; 8987 } 8988 } 8989 8990 if ((*activep * 100 / size) <= low) { 8991 return removed; 8992 } 8993 8994 /* 8995 * NOTE: Use of "* 15 / 10" is required here because if "* 1.5" is 8996 * used then the operations are upgraded to floating point 8997 * and kernels don't like floating point... 8998 */ 8999 if (softc->ipf_ticks > IPF_TTLVAL(43200 * 15 / 10)) { 9000 istart = IPF_TTLVAL(86400 * 4); 9001 interval = IPF_TTLVAL(43200); 9002 } else if (softc->ipf_ticks > IPF_TTLVAL(1800 * 15 / 10)) { 9003 istart = IPF_TTLVAL(43200); 9004 interval = IPF_TTLVAL(1800); 9005 } else if (softc->ipf_ticks > IPF_TTLVAL(30 * 15 / 10)) { 9006 istart = IPF_TTLVAL(1800); 9007 interval = IPF_TTLVAL(30); 9008 } else { 9009 return 0; 9010 } 9011 if (istart > softc->ipf_ticks) { 9012 if (softc->ipf_ticks - interval < interval) 9013 istart = interval; 9014 else 9015 istart = (softc->ipf_ticks / interval) * interval; 9016 } 9017 9018 iend = softc->ipf_ticks - interval; 9019 9020 while ((*activep * 100 / size) > low) { 9021 u_long try; 9022 9023 try = softc->ipf_ticks - istart; 9024 9025 for (ifq = ipfqs; ifq != NULL; ifq = ifq->ifq_next) { 9026 for (tqn = ifq->ifq_head; ((tqe = tqn) != NULL); ) { 9027 if (try < tqe->tqe_touched) 9028 break; 9029 tqn = tqe->tqe_next; 9030 if ((*deletefn)(softc, tqe->tqe_parent) == 0) 9031 removed++; 9032 } 9033 } 9034 9035 for (ifq = userqs; ifq != NULL; ifq = ifqnext) { 9036 ifqnext = ifq->ifq_next; 9037 9038 for (tqn = ifq->ifq_head; ((tqe = tqn) != NULL); ) { 9039 if (try < tqe->tqe_touched) 9040 break; 9041 tqn = tqe->tqe_next; 9042 if ((*deletefn)(softc, tqe->tqe_parent) == 0) 9043 removed++; 9044 } 9045 } 9046 9047 if (try >= iend) { 9048 if (interval == IPF_TTLVAL(43200)) { 9049 interval = IPF_TTLVAL(1800); 9050 } else if (interval == IPF_TTLVAL(1800)) { 9051 interval = IPF_TTLVAL(30); 9052 } else { 9053 break; 9054 } 9055 if (interval >= softc->ipf_ticks) 9056 break; 9057 9058 iend = softc->ipf_ticks - interval; 9059 } 9060 istart -= interval; 9061 } 9062 9063 return removed; 9064} 9065 9066 9067/* ------------------------------------------------------------------------ */ 9068/* Function: ipf_deliverlocal */ 9069/* Returns: int - 1 = local address, 0 = non-local address */ 9070/* Parameters: softc(I) - pointer to soft context main structure */ 9071/* ipversion(I) - IP protocol version (4 or 6) */ 9072/* ifp(I) - network interface pointer */ 9073/* ipaddr(I) - IPv4/6 destination address */ 9074/* */ 9075/* This fucntion is used to determine in the address "ipaddr" belongs to */ 9076/* the network interface represented by ifp. */ 9077/* ------------------------------------------------------------------------ */ 9078int 9079ipf_deliverlocal(softc, ipversion, ifp, ipaddr) 9080 ipf_main_softc_t *softc; 9081 int ipversion; 9082 void *ifp; 9083 i6addr_t *ipaddr; 9084{ 9085 i6addr_t addr; 9086 int islocal = 0; 9087 9088 if (ipversion == 4) { 9089 if (ipf_ifpaddr(softc, 4, FRI_NORMAL, ifp, &addr, NULL) == 0) { 9090 if (addr.in4.s_addr == ipaddr->in4.s_addr) 9091 islocal = 1; 9092 } 9093 9094#ifdef USE_INET6 9095 } else if (ipversion == 6) { 9096 if (ipf_ifpaddr(softc, 6, FRI_NORMAL, ifp, &addr, NULL) == 0) { 9097 if (IP6_EQ(&addr, ipaddr)) 9098 islocal = 1; 9099 } 9100#endif 9101 } 9102 9103 return islocal; 9104} 9105 9106 9107/* ------------------------------------------------------------------------ */ 9108/* Function: ipf_settimeout */ 9109/* Returns: int - 0 = success, -1 = failure */ 9110/* Parameters: softc(I) - pointer to soft context main structure */ 9111/* t(I) - pointer to tuneable array entry */ 9112/* p(I) - pointer to values passed in to apply */ 9113/* */ 9114/* This function is called to set the timeout values for each distinct */ 9115/* queue timeout that is available. When called, it calls into both the */ 9116/* state and NAT code, telling them to update their timeout queues. */ 9117/* ------------------------------------------------------------------------ */ 9118static int 9119ipf_settimeout(softc, t, p) 9120 struct ipf_main_softc_s *softc; 9121 ipftuneable_t *t; 9122 ipftuneval_t *p; 9123{ 9124 9125 /* 9126 * ipf_interror should be set by the functions called here, not 9127 * by this function - it's just a middle man. 9128 */ 9129 if (ipf_state_settimeout(softc, t, p) == -1) 9130 return -1; 9131 if (ipf_nat_settimeout(softc, t, p) == -1) 9132 return -1; 9133 return 0; 9134} 9135 9136 9137/* ------------------------------------------------------------------------ */ 9138/* Function: ipf_apply_timeout */ 9139/* Returns: int - 0 = success, -1 = failure */ 9140/* Parameters: head(I) - pointer to tuneable array entry */ 9141/* seconds(I) - pointer to values passed in to apply */ 9142/* */ 9143/* This function applies a timeout of "seconds" to the timeout queue that */ 9144/* is pointed to by "head". All entries on this list have an expiration */ 9145/* set to be the current tick value of ipf plus the ttl. Given that this */ 9146/* function should only be called when the delta is non-zero, the task is */ 9147/* to walk the entire list and apply the change. The sort order will not */ 9148/* change. The only catch is that this is O(n) across the list, so if the */ 9149/* queue has lots of entries (10s of thousands or 100s of thousands), it */ 9150/* could take a relatively long time to work through them all. */ 9151/* ------------------------------------------------------------------------ */ 9152void 9153ipf_apply_timeout(head, seconds) 9154 ipftq_t *head; 9155 u_int seconds; 9156{ 9157 u_int oldtimeout, newtimeout; 9158 ipftqent_t *tqe; 9159 int delta; 9160 9161 MUTEX_ENTER(&head->ifq_lock); 9162 oldtimeout = head->ifq_ttl; 9163 newtimeout = IPF_TTLVAL(seconds); 9164 delta = oldtimeout - newtimeout; 9165 9166 head->ifq_ttl = newtimeout; 9167 9168 for (tqe = head->ifq_head; tqe != NULL; tqe = tqe->tqe_next) { 9169 tqe->tqe_die += delta; 9170 } 9171 MUTEX_EXIT(&head->ifq_lock); 9172} 9173 9174 9175/* ------------------------------------------------------------------------ */ 9176/* Function: ipf_settimeout_tcp */ 9177/* Returns: int - 0 = successfully applied, -1 = failed */ 9178/* Parameters: t(I) - pointer to tuneable to change */ 9179/* p(I) - pointer to new timeout information */ 9180/* tab(I) - pointer to table of TCP queues */ 9181/* */ 9182/* This function applies the new timeout (p) to the TCP tunable (t) and */ 9183/* updates all of the entries on the relevant timeout queue by calling */ 9184/* ipf_apply_timeout(). */ 9185/* ------------------------------------------------------------------------ */ 9186int 9187ipf_settimeout_tcp(t, p, tab) 9188 ipftuneable_t *t; 9189 ipftuneval_t *p; 9190 ipftq_t *tab; 9191{ 9192 if (!strcmp(t->ipft_name, "tcp_idle_timeout") || 9193 !strcmp(t->ipft_name, "tcp_established")) { 9194 ipf_apply_timeout(&tab[IPF_TCPS_ESTABLISHED], p->ipftu_int); 9195 } else if (!strcmp(t->ipft_name, "tcp_close_wait")) { 9196 ipf_apply_timeout(&tab[IPF_TCPS_CLOSE_WAIT], p->ipftu_int); 9197 } else if (!strcmp(t->ipft_name, "tcp_last_ack")) { 9198 ipf_apply_timeout(&tab[IPF_TCPS_LAST_ACK], p->ipftu_int); 9199 } else if (!strcmp(t->ipft_name, "tcp_timeout")) { 9200 ipf_apply_timeout(&tab[IPF_TCPS_LISTEN], p->ipftu_int); 9201 ipf_apply_timeout(&tab[IPF_TCPS_HALF_ESTAB], p->ipftu_int); 9202 ipf_apply_timeout(&tab[IPF_TCPS_CLOSING], p->ipftu_int); 9203 } else if (!strcmp(t->ipft_name, "tcp_listen")) { 9204 ipf_apply_timeout(&tab[IPF_TCPS_LISTEN], p->ipftu_int); 9205 } else if (!strcmp(t->ipft_name, "tcp_half_established")) { 9206 ipf_apply_timeout(&tab[IPF_TCPS_HALF_ESTAB], p->ipftu_int); 9207 } else if (!strcmp(t->ipft_name, "tcp_closing")) { 9208 ipf_apply_timeout(&tab[IPF_TCPS_CLOSING], p->ipftu_int); 9209 } else if (!strcmp(t->ipft_name, "tcp_syn_received")) { 9210 ipf_apply_timeout(&tab[IPF_TCPS_SYN_RECEIVED], p->ipftu_int); 9211 } else if (!strcmp(t->ipft_name, "tcp_syn_sent")) { 9212 ipf_apply_timeout(&tab[IPF_TCPS_SYN_SENT], p->ipftu_int); 9213 } else if (!strcmp(t->ipft_name, "tcp_closed")) { 9214 ipf_apply_timeout(&tab[IPF_TCPS_CLOSED], p->ipftu_int); 9215 } else if (!strcmp(t->ipft_name, "tcp_half_closed")) { 9216 ipf_apply_timeout(&tab[IPF_TCPS_CLOSED], p->ipftu_int); 9217 } else if (!strcmp(t->ipft_name, "tcp_time_wait")) { 9218 ipf_apply_timeout(&tab[IPF_TCPS_TIME_WAIT], p->ipftu_int); 9219 } else { 9220 /* 9221 * ipf_interror isn't set here because it should be set 9222 * by whatever called this function. 9223 */ 9224 return -1; 9225 } 9226 return 0; 9227} 9228 9229 9230/* ------------------------------------------------------------------------ */ 9231/* Function: ipf_main_soft_create */ 9232/* Returns: NULL = failure, else success */ 9233/* Parameters: arg(I) - pointer to soft context structure if already allocd */ 9234/* */ 9235/* Create the foundation soft context structure. In circumstances where it */ 9236/* is not required to dynamically allocate the context, a pointer can be */ 9237/* passed in (rather than NULL) to a structure to be initialised. */ 9238/* The main thing of interest is that a number of locks are initialised */ 9239/* here instead of in the where might be expected - in the relevant create */ 9240/* function elsewhere. This is done because the current locking design has */ 9241/* some areas where these locks are used outside of their module. */ 9242/* Possibly the most important exercise that is done here is setting of all */ 9243/* the timeout values, allowing them to be changed before init(). */ 9244/* ------------------------------------------------------------------------ */ 9245void * 9246ipf_main_soft_create(arg) 9247 void *arg; 9248{ 9249 ipf_main_softc_t *softc; 9250 9251 if (arg == NULL) { 9252 KMALLOC(softc, ipf_main_softc_t *); 9253 if (softc == NULL) 9254 return NULL; 9255 } else { 9256 softc = arg; 9257 } 9258 9259 bzero((char *)softc, sizeof(*softc)); 9260 9261 /* 9262 * This serves as a flag as to whether or not the softc should be 9263 * free'd when _destroy is called. 9264 */ 9265 softc->ipf_dynamic_softc = (arg == NULL) ? 1 : 0; 9266 9267 softc->ipf_tuners = ipf_tune_array_copy(softc, 9268 sizeof(ipf_main_tuneables), 9269 ipf_main_tuneables); 9270 if (softc->ipf_tuners == NULL) { 9271 ipf_main_soft_destroy(softc); 9272 return NULL; 9273 } 9274 9275 MUTEX_INIT(&softc->ipf_rw, "ipf rw mutex"); 9276 MUTEX_INIT(&softc->ipf_timeoutlock, "ipf timeout lock"); 9277 RWLOCK_INIT(&softc->ipf_global, "ipf filter load/unload mutex"); 9278 RWLOCK_INIT(&softc->ipf_mutex, "ipf filter rwlock"); 9279 RWLOCK_INIT(&softc->ipf_tokens, "ipf token rwlock"); 9280 RWLOCK_INIT(&softc->ipf_state, "ipf state rwlock"); 9281 RWLOCK_INIT(&softc->ipf_nat, "ipf IP NAT rwlock"); 9282 RWLOCK_INIT(&softc->ipf_poolrw, "ipf pool rwlock"); 9283 RWLOCK_INIT(&softc->ipf_frag, "ipf frag rwlock"); 9284 9285 softc->ipf_token_head = NULL; 9286 softc->ipf_token_tail = &softc->ipf_token_head; 9287 9288 softc->ipf_tcpidletimeout = FIVE_DAYS; 9289 softc->ipf_tcpclosewait = IPF_TTLVAL(2 * TCP_MSL); 9290 softc->ipf_tcplastack = IPF_TTLVAL(30); 9291 softc->ipf_tcptimewait = IPF_TTLVAL(2 * TCP_MSL); 9292 softc->ipf_tcptimeout = IPF_TTLVAL(2 * TCP_MSL); 9293 softc->ipf_tcpsynsent = IPF_TTLVAL(2 * TCP_MSL); 9294 softc->ipf_tcpsynrecv = IPF_TTLVAL(2 * TCP_MSL); 9295 softc->ipf_tcpclosed = IPF_TTLVAL(30); 9296 softc->ipf_tcphalfclosed = IPF_TTLVAL(2 * 3600); 9297 softc->ipf_udptimeout = IPF_TTLVAL(120); 9298 softc->ipf_udpacktimeout = IPF_TTLVAL(12); 9299 softc->ipf_icmptimeout = IPF_TTLVAL(60); 9300 softc->ipf_icmpacktimeout = IPF_TTLVAL(6); 9301 softc->ipf_iptimeout = IPF_TTLVAL(60); 9302 9303#if defined(IPFILTER_DEFAULT_BLOCK) 9304 softc->ipf_pass = FR_BLOCK|FR_NOMATCH; 9305#else 9306 softc->ipf_pass = (IPF_DEFAULT_PASS)|FR_NOMATCH; 9307#endif 9308 softc->ipf_minttl = 4; 9309 softc->ipf_icmpminfragmtu = 68; 9310 softc->ipf_flags = IPF_LOGGING; 9311 9312 return softc; 9313} 9314 9315/* ------------------------------------------------------------------------ */ 9316/* Function: ipf_main_soft_init */ 9317/* Returns: 0 = success, -1 = failure */ 9318/* Parameters: softc(I) - pointer to soft context main structure */ 9319/* */ 9320/* A null-op function that exists as a placeholder so that the flow in */ 9321/* other functions is obvious. */ 9322/* ------------------------------------------------------------------------ */ 9323/*ARGSUSED*/ 9324int 9325ipf_main_soft_init(softc) 9326 ipf_main_softc_t *softc; 9327{ 9328 return 0; 9329} 9330 9331 9332/* ------------------------------------------------------------------------ */ 9333/* Function: ipf_main_soft_destroy */ 9334/* Returns: void */ 9335/* Parameters: softc(I) - pointer to soft context main structure */ 9336/* */ 9337/* Undo everything that we did in ipf_main_soft_create. */ 9338/* */ 9339/* The most important check that needs to be made here is whether or not */ 9340/* the structure was allocated by ipf_main_soft_create() by checking what */ 9341/* value is stored in ipf_dynamic_main. */ 9342/* ------------------------------------------------------------------------ */ 9343/*ARGSUSED*/ 9344void 9345ipf_main_soft_destroy(softc) 9346 ipf_main_softc_t *softc; 9347{ 9348 9349 RW_DESTROY(&softc->ipf_frag); 9350 RW_DESTROY(&softc->ipf_poolrw); 9351 RW_DESTROY(&softc->ipf_nat); 9352 RW_DESTROY(&softc->ipf_state); 9353 RW_DESTROY(&softc->ipf_tokens); 9354 RW_DESTROY(&softc->ipf_mutex); 9355 RW_DESTROY(&softc->ipf_global); 9356 MUTEX_DESTROY(&softc->ipf_timeoutlock); 9357 MUTEX_DESTROY(&softc->ipf_rw); 9358 9359 if (softc->ipf_tuners != NULL) { 9360 KFREES(softc->ipf_tuners, sizeof(ipf_main_tuneables)); 9361 } 9362 if (softc->ipf_dynamic_softc == 1) { 9363 KFREE(softc); 9364 } 9365} 9366 9367 9368/* ------------------------------------------------------------------------ */ 9369/* Function: ipf_main_soft_fini */ 9370/* Returns: 0 = success, -1 = failure */ 9371/* Parameters: softc(I) - pointer to soft context main structure */ 9372/* */ 9373/* Clean out the rules which have been added since _init was last called, */ 9374/* the only dynamic part of the mainline. */ 9375/* ------------------------------------------------------------------------ */ 9376int 9377ipf_main_soft_fini(softc) 9378 ipf_main_softc_t *softc; 9379{ 9380 (void) ipf_flush(softc, IPL_LOGIPF, FR_INQUE|FR_OUTQUE|FR_INACTIVE); 9381 (void) ipf_flush(softc, IPL_LOGIPF, FR_INQUE|FR_OUTQUE); 9382 (void) ipf_flush(softc, IPL_LOGCOUNT, FR_INQUE|FR_OUTQUE|FR_INACTIVE); 9383 (void) ipf_flush(softc, IPL_LOGCOUNT, FR_INQUE|FR_OUTQUE); 9384 9385 return 0; 9386} 9387 9388 9389/* ------------------------------------------------------------------------ */ 9390/* Function: ipf_main_load */ 9391/* Returns: 0 = success, -1 = failure */ 9392/* Parameters: none */ 9393/* */ 9394/* Handle global initialisation that needs to be done for the base part of */ 9395/* IPFilter. At present this just amounts to initialising some ICMP lookup */ 9396/* arrays that get used by the state/NAT code. */ 9397/* ------------------------------------------------------------------------ */ 9398int 9399ipf_main_load() 9400{ 9401 int i; 9402 9403 /* fill icmp reply type table */ 9404 for (i = 0; i <= ICMP_MAXTYPE; i++) 9405 icmpreplytype4[i] = -1; 9406 icmpreplytype4[ICMP_ECHO] = ICMP_ECHOREPLY; 9407 icmpreplytype4[ICMP_TSTAMP] = ICMP_TSTAMPREPLY; 9408 icmpreplytype4[ICMP_IREQ] = ICMP_IREQREPLY; 9409 icmpreplytype4[ICMP_MASKREQ] = ICMP_MASKREPLY; 9410 9411#ifdef USE_INET6 9412 /* fill icmp reply type table */ 9413 for (i = 0; i <= ICMP6_MAXTYPE; i++) 9414 icmpreplytype6[i] = -1; 9415 icmpreplytype6[ICMP6_ECHO_REQUEST] = ICMP6_ECHO_REPLY; 9416 icmpreplytype6[ICMP6_MEMBERSHIP_QUERY] = ICMP6_MEMBERSHIP_REPORT; 9417 icmpreplytype6[ICMP6_NI_QUERY] = ICMP6_NI_REPLY; 9418 icmpreplytype6[ND_ROUTER_SOLICIT] = ND_ROUTER_ADVERT; 9419 icmpreplytype6[ND_NEIGHBOR_SOLICIT] = ND_NEIGHBOR_ADVERT; 9420#endif 9421 9422 return 0; 9423} 9424 9425 9426/* ------------------------------------------------------------------------ */ 9427/* Function: ipf_main_unload */ 9428/* Returns: 0 = success, -1 = failure */ 9429/* Parameters: none */ 9430/* */ 9431/* A null-op function that exists as a placeholder so that the flow in */ 9432/* other functions is obvious. */ 9433/* ------------------------------------------------------------------------ */ 9434int 9435ipf_main_unload() 9436{ 9437 return 0; 9438} 9439 9440 9441/* ------------------------------------------------------------------------ */ 9442/* Function: ipf_load_all */ 9443/* Returns: 0 = success, -1 = failure */ 9444/* Parameters: none */ 9445/* */ 9446/* Work through all of the subsystems inside IPFilter and call the load */ 9447/* function for each in an order that won't lead to a crash :) */ 9448/* ------------------------------------------------------------------------ */ 9449int 9450ipf_load_all() 9451{ 9452 if (ipf_main_load() == -1) 9453 return -1; 9454 9455 if (ipf_state_main_load() == -1) 9456 return -1; 9457 9458 if (ipf_nat_main_load() == -1) 9459 return -1; 9460 9461 if (ipf_frag_main_load() == -1) 9462 return -1; 9463 9464 if (ipf_auth_main_load() == -1) 9465 return -1; 9466 9467 if (ipf_proxy_main_load() == -1) 9468 return -1; 9469 9470 return 0; 9471} 9472 9473 9474/* ------------------------------------------------------------------------ */ 9475/* Function: ipf_unload_all */ 9476/* Returns: 0 = success, -1 = failure */ 9477/* Parameters: none */ 9478/* */ 9479/* Work through all of the subsystems inside IPFilter and call the unload */ 9480/* function for each in an order that won't lead to a crash :) */ 9481/* ------------------------------------------------------------------------ */ 9482int 9483ipf_unload_all() 9484{ 9485 if (ipf_proxy_main_unload() == -1) 9486 return -1; 9487 9488 if (ipf_auth_main_unload() == -1) 9489 return -1; 9490 9491 if (ipf_frag_main_unload() == -1) 9492 return -1; 9493 9494 if (ipf_nat_main_unload() == -1) 9495 return -1; 9496 9497 if (ipf_state_main_unload() == -1) 9498 return -1; 9499 9500 if (ipf_main_unload() == -1) 9501 return -1; 9502 9503 return 0; 9504} 9505 9506 9507/* ------------------------------------------------------------------------ */ 9508/* Function: ipf_create_all */ 9509/* Returns: NULL = failure, else success */ 9510/* Parameters: arg(I) - pointer to soft context main structure */ 9511/* */ 9512/* Work through all of the subsystems inside IPFilter and call the create */ 9513/* function for each in an order that won't lead to a crash :) */ 9514/* ------------------------------------------------------------------------ */ 9515ipf_main_softc_t * 9516ipf_create_all(arg) 9517 void *arg; 9518{ 9519 ipf_main_softc_t *softc; 9520 9521 softc = ipf_main_soft_create(arg); 9522 if (softc == NULL) 9523 return NULL; 9524 9525#ifdef IPFILTER_LOG 9526 softc->ipf_log_soft = ipf_log_soft_create(softc); 9527 if (softc->ipf_log_soft == NULL) { 9528 ipf_destroy_all(softc); 9529 return NULL; 9530 } 9531#endif 9532 9533 softc->ipf_lookup_soft = ipf_lookup_soft_create(softc); 9534 if (softc->ipf_lookup_soft == NULL) { 9535 ipf_destroy_all(softc); 9536 return NULL; 9537 } 9538 9539 softc->ipf_sync_soft = ipf_sync_soft_create(softc); 9540 if (softc->ipf_sync_soft == NULL) { 9541 ipf_destroy_all(softc); 9542 return NULL; 9543 } 9544 9545 softc->ipf_state_soft = ipf_state_soft_create(softc); 9546 if (softc->ipf_state_soft == NULL) { 9547 ipf_destroy_all(softc); 9548 return NULL; 9549 } 9550 9551 softc->ipf_nat_soft = ipf_nat_soft_create(softc); 9552 if (softc->ipf_nat_soft == NULL) { 9553 ipf_destroy_all(softc); 9554 return NULL; 9555 } 9556 9557 softc->ipf_frag_soft = ipf_frag_soft_create(softc); 9558 if (softc->ipf_frag_soft == NULL) { 9559 ipf_destroy_all(softc); 9560 return NULL; 9561 } 9562 9563 softc->ipf_auth_soft = ipf_auth_soft_create(softc); 9564 if (softc->ipf_auth_soft == NULL) { 9565 ipf_destroy_all(softc); 9566 return NULL; 9567 } 9568 9569 softc->ipf_proxy_soft = ipf_proxy_soft_create(softc); 9570 if (softc->ipf_proxy_soft == NULL) { 9571 ipf_destroy_all(softc); 9572 return NULL; 9573 } 9574 9575 return softc; 9576} 9577 9578 9579/* ------------------------------------------------------------------------ */ 9580/* Function: ipf_destroy_all */ 9581/* Returns: void */ 9582/* Parameters: softc(I) - pointer to soft context main structure */ 9583/* */ 9584/* Work through all of the subsystems inside IPFilter and call the destroy */ 9585/* function for each in an order that won't lead to a crash :) */ 9586/* */ 9587/* Every one of these functions is expected to succeed, so there is no */ 9588/* checking of return values. */ 9589/* ------------------------------------------------------------------------ */ 9590void 9591ipf_destroy_all(softc) 9592 ipf_main_softc_t *softc; 9593{ 9594 9595 if (softc->ipf_state_soft != NULL) { 9596 ipf_state_soft_destroy(softc, softc->ipf_state_soft); 9597 softc->ipf_state_soft = NULL; 9598 } 9599 9600 if (softc->ipf_nat_soft != NULL) { 9601 ipf_nat_soft_destroy(softc, softc->ipf_nat_soft); 9602 softc->ipf_nat_soft = NULL; 9603 } 9604 9605 if (softc->ipf_frag_soft != NULL) { 9606 ipf_frag_soft_destroy(softc, softc->ipf_frag_soft); 9607 softc->ipf_frag_soft = NULL; 9608 } 9609 9610 if (softc->ipf_auth_soft != NULL) { 9611 ipf_auth_soft_destroy(softc, softc->ipf_auth_soft); 9612 softc->ipf_auth_soft = NULL; 9613 } 9614 9615 if (softc->ipf_proxy_soft != NULL) { 9616 ipf_proxy_soft_destroy(softc, softc->ipf_proxy_soft); 9617 softc->ipf_proxy_soft = NULL; 9618 } 9619 9620 if (softc->ipf_sync_soft != NULL) { 9621 ipf_sync_soft_destroy(softc, softc->ipf_sync_soft); 9622 softc->ipf_sync_soft = NULL; 9623 } 9624 9625 if (softc->ipf_lookup_soft != NULL) { 9626 ipf_lookup_soft_destroy(softc, softc->ipf_lookup_soft); 9627 softc->ipf_lookup_soft = NULL; 9628 } 9629 9630#ifdef IPFILTER_LOG 9631 if (softc->ipf_log_soft != NULL) { 9632 ipf_log_soft_destroy(softc, softc->ipf_log_soft); 9633 softc->ipf_log_soft = NULL; 9634 } 9635#endif 9636 9637 ipf_main_soft_destroy(softc); 9638} 9639 9640 9641/* ------------------------------------------------------------------------ */ 9642/* Function: ipf_init_all */ 9643/* Returns: 0 = success, -1 = failure */ 9644/* Parameters: softc(I) - pointer to soft context main structure */ 9645/* */ 9646/* Work through all of the subsystems inside IPFilter and call the init */ 9647/* function for each in an order that won't lead to a crash :) */ 9648/* ------------------------------------------------------------------------ */ 9649int 9650ipf_init_all(softc) 9651 ipf_main_softc_t *softc; 9652{ 9653 9654 if (ipf_main_soft_init(softc) == -1) 9655 return -1; 9656 9657#ifdef IPFILTER_LOG 9658 if (ipf_log_soft_init(softc, softc->ipf_log_soft) == -1) 9659 return -1; 9660#endif 9661 9662 if (ipf_lookup_soft_init(softc, softc->ipf_lookup_soft) == -1) 9663 return -1; 9664 9665 if (ipf_sync_soft_init(softc, softc->ipf_sync_soft) == -1) 9666 return -1; 9667 9668 if (ipf_state_soft_init(softc, softc->ipf_state_soft) == -1) 9669 return -1; 9670 9671 if (ipf_nat_soft_init(softc, softc->ipf_nat_soft) == -1) 9672 return -1; 9673 9674 if (ipf_frag_soft_init(softc, softc->ipf_frag_soft) == -1) 9675 return -1; 9676 9677 if (ipf_auth_soft_init(softc, softc->ipf_auth_soft) == -1) 9678 return -1; 9679 9680 if (ipf_proxy_soft_init(softc, softc->ipf_proxy_soft) == -1) 9681 return -1; 9682 9683 return 0; 9684} 9685 9686 9687/* ------------------------------------------------------------------------ */ 9688/* Function: ipf_fini_all */ 9689/* Returns: 0 = success, -1 = failure */ 9690/* Parameters: softc(I) - pointer to soft context main structure */ 9691/* */ 9692/* Work through all of the subsystems inside IPFilter and call the fini */ 9693/* function for each in an order that won't lead to a crash :) */ 9694/* ------------------------------------------------------------------------ */ 9695int 9696ipf_fini_all(softc) 9697 ipf_main_softc_t *softc; 9698{ 9699 9700 ipf_token_flush(softc); 9701 9702 if (ipf_proxy_soft_fini(softc, softc->ipf_proxy_soft) == -1) 9703 return -1; 9704 9705 if (ipf_auth_soft_fini(softc, softc->ipf_auth_soft) == -1) 9706 return -1; 9707 9708 if (ipf_frag_soft_fini(softc, softc->ipf_frag_soft) == -1) 9709 return -1; 9710 9711 if (ipf_nat_soft_fini(softc, softc->ipf_nat_soft) == -1) 9712 return -1; 9713 9714 if (ipf_state_soft_fini(softc, softc->ipf_state_soft) == -1) 9715 return -1; 9716 9717 if (ipf_sync_soft_fini(softc, softc->ipf_sync_soft) == -1) 9718 return -1; 9719 9720 if (ipf_lookup_soft_fini(softc, softc->ipf_lookup_soft) == -1) 9721 return -1; 9722 9723#ifdef IPFILTER_LOG 9724 if (ipf_log_soft_fini(softc, softc->ipf_log_soft) == -1) 9725 return -1; 9726#endif 9727 9728 if (ipf_main_soft_fini(softc) == -1) 9729 return -1; 9730 9731 return 0; 9732} 9733 9734 9735/* ------------------------------------------------------------------------ */ 9736/* Function: ipf_rule_expire */ 9737/* Returns: Nil */ 9738/* Parameters: softc(I) - pointer to soft context main structure */ 9739/* */ 9740/* At present this function exists just to support temporary addition of */ 9741/* firewall rules. Both inactive and active lists are scanned for items to */ 9742/* purge, as by rights, the expiration is computed as soon as the rule is */ 9743/* loaded in. */ 9744/* ------------------------------------------------------------------------ */ 9745void 9746ipf_rule_expire(softc) 9747 ipf_main_softc_t *softc; 9748{ 9749 frentry_t *fr; 9750 9751 if ((softc->ipf_rule_explist[0] == NULL) && 9752 (softc->ipf_rule_explist[1] == NULL)) 9753 return; 9754 9755 WRITE_ENTER(&softc->ipf_mutex); 9756 9757 while ((fr = softc->ipf_rule_explist[0]) != NULL) { 9758 /* 9759 * Because the list is kept sorted on insertion, the fist 9760 * one that dies in the future means no more work to do. 9761 */ 9762 if (fr->fr_die > softc->ipf_ticks) 9763 break; 9764 ipf_rule_delete(softc, fr, IPL_LOGIPF, 0); 9765 } 9766 9767 while ((fr = softc->ipf_rule_explist[1]) != NULL) { 9768 /* 9769 * Because the list is kept sorted on insertion, the fist 9770 * one that dies in the future means no more work to do. 9771 */ 9772 if (fr->fr_die > softc->ipf_ticks) 9773 break; 9774 ipf_rule_delete(softc, fr, IPL_LOGIPF, 1); 9775 } 9776 9777 RWLOCK_EXIT(&softc->ipf_mutex); 9778} 9779 9780 9781static int ipf_ht_node_cmp __P((struct host_node_s *, struct host_node_s *)); 9782static void ipf_ht_node_make_key __P((host_track_t *, host_node_t *, int, 9783 i6addr_t *)); 9784 9785host_node_t RBI_ZERO(ipf_rb); 9786RBI_CODE(ipf_rb, host_node_t, hn_entry, ipf_ht_node_cmp) 9787 9788 9789/* ------------------------------------------------------------------------ */ 9790/* Function: ipf_ht_node_cmp */ 9791/* Returns: int - 0 == nodes are the same, .. */ 9792/* Parameters: k1(I) - pointer to first key to compare */ 9793/* k2(I) - pointer to second key to compare */ 9794/* */ 9795/* The "key" for the node is a combination of two fields: the address */ 9796/* family and the address itself. */ 9797/* */ 9798/* Because we're not actually interpreting the address data, it isn't */ 9799/* necessary to convert them to/from network/host byte order. The mask is */ 9800/* just used to remove bits that aren't significant - it doesn't matter */ 9801/* where they are, as long as they're always in the same place. */ 9802/* */ 9803/* As with IP6_EQ, comparing IPv6 addresses starts at the bottom because */ 9804/* this is where individual ones will differ the most - but not true for */ 9805/* for /48's, etc. */ 9806/* ------------------------------------------------------------------------ */ 9807static int 9808ipf_ht_node_cmp(k1, k2) 9809 struct host_node_s *k1, *k2; 9810{ 9811 int i; 9812 9813 i = (k2->hn_addr.adf_family - k1->hn_addr.adf_family); 9814 if (i != 0) 9815 return i; 9816 9817 if (k1->hn_addr.adf_family == AF_INET) 9818 return (k2->hn_addr.adf_addr.in4.s_addr - 9819 k1->hn_addr.adf_addr.in4.s_addr); 9820 9821 i = k2->hn_addr.adf_addr.i6[3] - k1->hn_addr.adf_addr.i6[3]; 9822 if (i != 0) 9823 return i; 9824 i = k2->hn_addr.adf_addr.i6[2] - k1->hn_addr.adf_addr.i6[2]; 9825 if (i != 0) 9826 return i; 9827 i = k2->hn_addr.adf_addr.i6[1] - k1->hn_addr.adf_addr.i6[1]; 9828 if (i != 0) 9829 return i; 9830 i = k2->hn_addr.adf_addr.i6[0] - k1->hn_addr.adf_addr.i6[0]; 9831 return i; 9832} 9833 9834 9835/* ------------------------------------------------------------------------ */ 9836/* Function: ipf_ht_node_make_key */ 9837/* Returns: Nil */ 9838/* parameters: htp(I) - pointer to address tracking structure */ 9839/* key(I) - where to store masked address for lookup */ 9840/* family(I) - protocol family of address */ 9841/* addr(I) - pointer to network address */ 9842/* */ 9843/* Using the "netmask" (number of bits) stored parent host tracking struct, */ 9844/* copy the address passed in into the key structure whilst masking out the */ 9845/* bits that we don't want. */ 9846/* */ 9847/* Because the parser will set ht_netmask to 128 if there is no protocol */ 9848/* specified (the parser doesn't know if it should be a v4 or v6 rule), we */ 9849/* have to be wary of that and not allow 32-128 to happen. */ 9850/* ------------------------------------------------------------------------ */ 9851static void 9852ipf_ht_node_make_key(htp, key, family, addr) 9853 host_track_t *htp; 9854 host_node_t *key; 9855 int family; 9856 i6addr_t *addr; 9857{ 9858 key->hn_addr.adf_family = family; 9859 if (family == AF_INET) { 9860 u_32_t mask; 9861 int bits; 9862 9863 key->hn_addr.adf_len = sizeof(key->hn_addr.adf_addr.in4); 9864 bits = htp->ht_netmask; 9865 if (bits >= 32) { 9866 mask = 0xffffffff; 9867 } else { 9868 mask = htonl(0xffffffff << (32 - bits)); 9869 } 9870 key->hn_addr.adf_addr.in4.s_addr = addr->in4.s_addr & mask; 9871#ifdef USE_INET6 9872 } else { 9873 int bits = htp->ht_netmask; 9874 9875 key->hn_addr.adf_len = sizeof(key->hn_addr.adf_addr.in6); 9876 if (bits > 96) { 9877 key->hn_addr.adf_addr.i6[3] = addr->i6[3] & 9878 htonl(0xffffffff << (128 - bits)); 9879 key->hn_addr.adf_addr.i6[2] = addr->i6[2]; 9880 key->hn_addr.adf_addr.i6[1] = addr->i6[2]; 9881 key->hn_addr.adf_addr.i6[0] = addr->i6[2]; 9882 } else if (bits > 64) { 9883 key->hn_addr.adf_addr.i6[3] = 0; 9884 key->hn_addr.adf_addr.i6[2] = addr->i6[2] & 9885 htonl(0xffffffff << (96 - bits)); 9886 key->hn_addr.adf_addr.i6[1] = addr->i6[1]; 9887 key->hn_addr.adf_addr.i6[0] = addr->i6[0]; 9888 } else if (bits > 32) { 9889 key->hn_addr.adf_addr.i6[3] = 0; 9890 key->hn_addr.adf_addr.i6[2] = 0; 9891 key->hn_addr.adf_addr.i6[1] = addr->i6[1] & 9892 htonl(0xffffffff << (64 - bits)); 9893 key->hn_addr.adf_addr.i6[0] = addr->i6[0]; 9894 } else { 9895 key->hn_addr.adf_addr.i6[3] = 0; 9896 key->hn_addr.adf_addr.i6[2] = 0; 9897 key->hn_addr.adf_addr.i6[1] = 0; 9898 key->hn_addr.adf_addr.i6[0] = addr->i6[0] & 9899 htonl(0xffffffff << (32 - bits)); 9900 } 9901#endif 9902 } 9903} 9904 9905 9906/* ------------------------------------------------------------------------ */ 9907/* Function: ipf_ht_node_add */ 9908/* Returns: int - 0 == success, -1 == failure */ 9909/* Parameters: softc(I) - pointer to soft context main structure */ 9910/* htp(I) - pointer to address tracking structure */ 9911/* family(I) - protocol family of address */ 9912/* addr(I) - pointer to network address */ 9913/* */ 9914/* NOTE: THIS FUNCTION MUST BE CALLED WITH AN EXCLUSIVE LOCK THAT PREVENTS */ 9915/* ipf_ht_node_del FROM RUNNING CONCURRENTLY ON THE SAME htp. */ 9916/* */ 9917/* After preparing the key with the address information to find, look in */ 9918/* the red-black tree to see if the address is known. A successful call to */ 9919/* this function can mean one of two things: a new node was added to the */ 9920/* tree or a matching node exists and we're able to bump up its activity. */ 9921/* ------------------------------------------------------------------------ */ 9922int 9923ipf_ht_node_add(softc, htp, family, addr) 9924 ipf_main_softc_t *softc; 9925 host_track_t *htp; 9926 int family; 9927 i6addr_t *addr; 9928{ 9929 host_node_t *h; 9930 host_node_t k; 9931 9932 ipf_ht_node_make_key(htp, &k, family, addr); 9933 9934 h = RBI_SEARCH(ipf_rb, &htp->ht_root, &k); 9935 if (h == NULL) { 9936 if (htp->ht_cur_nodes >= htp->ht_max_nodes) 9937 return -1; 9938 KMALLOC(h, host_node_t *); 9939 if (h == NULL) { 9940 DT(ipf_rb_no_mem); 9941 LBUMP(ipf_rb_no_mem); 9942 return -1; 9943 } 9944 9945 /* 9946 * If there was a macro to initialise the RB node then that 9947 * would get used here, but there isn't... 9948 */ 9949 bzero((char *)h, sizeof(*h)); 9950 h->hn_addr = k.hn_addr; 9951 h->hn_addr.adf_family = k.hn_addr.adf_family; 9952 RBI_INSERT(ipf_rb, &htp->ht_root, h); 9953 htp->ht_cur_nodes++; 9954 } else { 9955 if ((htp->ht_max_per_node != 0) && 9956 (h->hn_active >= htp->ht_max_per_node)) { 9957 DT(ipf_rb_node_max); 9958 LBUMP(ipf_rb_node_max); 9959 return -1; 9960 } 9961 } 9962 9963 h->hn_active++; 9964 9965 return 0; 9966} 9967 9968 9969/* ------------------------------------------------------------------------ */ 9970/* Function: ipf_ht_node_del */ 9971/* Returns: int - 0 == success, -1 == failure */ 9972/* parameters: htp(I) - pointer to address tracking structure */ 9973/* family(I) - protocol family of address */ 9974/* addr(I) - pointer to network address */ 9975/* */ 9976/* NOTE: THIS FUNCTION MUST BE CALLED WITH AN EXCLUSIVE LOCK THAT PREVENTS */ 9977/* ipf_ht_node_add FROM RUNNING CONCURRENTLY ON THE SAME htp. */ 9978/* */ 9979/* Try and find the address passed in amongst the leavese on this tree to */ 9980/* be friend. If found then drop the active account for that node drops by */ 9981/* one. If that count reaches 0, it is time to free it all up. */ 9982/* ------------------------------------------------------------------------ */ 9983int 9984ipf_ht_node_del(htp, family, addr) 9985 host_track_t *htp; 9986 int family; 9987 i6addr_t *addr; 9988{ 9989 host_node_t *h; 9990 host_node_t k; 9991 9992 ipf_ht_node_make_key(htp, &k, family, addr); 9993 9994 h = RBI_SEARCH(ipf_rb, &htp->ht_root, &k); 9995 if (h == NULL) { 9996 return -1; 9997 } else { 9998 h->hn_active--; 9999 if (h->hn_active == 0) { 10000 (void) RBI_DELETE(ipf_rb, &htp->ht_root, h); 10001 htp->ht_cur_nodes--; 10002 KFREE(h); 10003 } 10004 } 10005 10006 return 0; 10007} 10008 10009 10010/* ------------------------------------------------------------------------ */ 10011/* Function: ipf_rb_ht_init */ 10012/* Returns: Nil */ 10013/* Parameters: head(I) - pointer to host tracking structure */ 10014/* */ 10015/* Initialise the host tracking structure to be ready for use above. */ 10016/* ------------------------------------------------------------------------ */ 10017void 10018ipf_rb_ht_init(head) 10019 host_track_t *head; 10020{ 10021 RBI_INIT(ipf_rb, &head->ht_root); 10022} 10023 10024 10025/* ------------------------------------------------------------------------ */ 10026/* Function: ipf_rb_ht_freenode */ 10027/* Returns: Nil */ 10028/* Parameters: head(I) - pointer to host tracking structure */ 10029/* arg(I) - additional argument from walk caller */ 10030/* */ 10031/* Free an actual host_node_t structure. */ 10032/* ------------------------------------------------------------------------ */ 10033void 10034ipf_rb_ht_freenode(node, arg) 10035 host_node_t *node; 10036 void *arg; 10037{ 10038 KFREE(node); 10039} 10040 10041 10042/* ------------------------------------------------------------------------ */ 10043/* Function: ipf_rb_ht_flush */ 10044/* Returns: Nil */ 10045/* Parameters: head(I) - pointer to host tracking structure */ 10046/* */ 10047/* Remove all of the nodes in the tree tracking hosts by calling a walker */ 10048/* and free'ing each one. */ 10049/* ------------------------------------------------------------------------ */ 10050void 10051ipf_rb_ht_flush(head) 10052 host_track_t *head; 10053{ 10054 RBI_WALK(ipf_rb, &head->ht_root, ipf_rb_ht_freenode, NULL); 10055} 10056 10057 10058/* ------------------------------------------------------------------------ */ 10059/* Function: ipf_slowtimer */ 10060/* Returns: Nil */ 10061/* Parameters: ptr(I) - pointer to main ipf soft context structure */ 10062/* */ 10063/* Slowly expire held state for fragments. Timeouts are set * in */ 10064/* expectation of this being called twice per second. */ 10065/* ------------------------------------------------------------------------ */ 10066void 10067ipf_slowtimer(softc) 10068 ipf_main_softc_t *softc; 10069{ 10070 10071 ipf_token_expire(softc); 10072 ipf_frag_expire(softc); 10073 ipf_state_expire(softc); 10074 ipf_nat_expire(softc); 10075 ipf_auth_expire(softc); 10076 ipf_lookup_expire(softc); 10077 ipf_rule_expire(softc); 10078 ipf_sync_expire(softc); 10079 softc->ipf_ticks++; 10080# if defined(__OpenBSD__) 10081 timeout_add(&ipf_slowtimer_ch, hz/2); 10082# endif 10083} 10084 10085 10086/* ------------------------------------------------------------------------ */ 10087/* Function: ipf_inet_mask_add */ 10088/* Returns: Nil */ 10089/* Parameters: bits(I) - pointer to nat context information */ 10090/* mtab(I) - pointer to mask hash table structure */ 10091/* */ 10092/* When called, bits represents the mask of a new NAT rule that has just */ 10093/* been added. This function inserts a bitmask into the array of masks to */ 10094/* search when searching for a matching NAT rule for a packet. */ 10095/* Prevention of duplicate masks is achieved by checking the use count for */ 10096/* a given netmask. */ 10097/* ------------------------------------------------------------------------ */ 10098void 10099ipf_inet_mask_add(bits, mtab) 10100 int bits; 10101 ipf_v4_masktab_t *mtab; 10102{ 10103 u_32_t mask; 10104 int i, j; 10105 10106 mtab->imt4_masks[bits]++; 10107 if (mtab->imt4_masks[bits] > 1) 10108 return; 10109 10110 if (bits == 0) 10111 mask = 0; 10112 else 10113 mask = 0xffffffff << (32 - bits); 10114 10115 for (i = 0; i < 33; i++) { 10116 if (ntohl(mtab->imt4_active[i]) < mask) { 10117 for (j = 32; j > i; j--) 10118 mtab->imt4_active[j] = mtab->imt4_active[j - 1]; 10119 mtab->imt4_active[i] = htonl(mask); 10120 break; 10121 } 10122 } 10123 mtab->imt4_max++; 10124} 10125 10126 10127/* ------------------------------------------------------------------------ */ 10128/* Function: ipf_inet_mask_del */ 10129/* Returns: Nil */ 10130/* Parameters: bits(I) - number of bits set in the netmask */ 10131/* mtab(I) - pointer to mask hash table structure */ 10132/* */ 10133/* Remove the 32bit bitmask represented by "bits" from the collection of */ 10134/* netmasks stored inside of mtab. */ 10135/* ------------------------------------------------------------------------ */ 10136void 10137ipf_inet_mask_del(bits, mtab) 10138 int bits; 10139 ipf_v4_masktab_t *mtab; 10140{ 10141 u_32_t mask; 10142 int i, j; 10143 10144 mtab->imt4_masks[bits]--; 10145 if (mtab->imt4_masks[bits] > 0) 10146 return; 10147 10148 mask = htonl(0xffffffff << (32 - bits)); 10149 for (i = 0; i < 33; i++) { 10150 if (mtab->imt4_active[i] == mask) { 10151 for (j = i + 1; j < 33; j++) 10152 mtab->imt4_active[j - 1] = mtab->imt4_active[j]; 10153 break; 10154 } 10155 } 10156 mtab->imt4_max--; 10157 ASSERT(mtab->imt4_max >= 0); 10158} 10159 10160 10161#ifdef USE_INET6 10162/* ------------------------------------------------------------------------ */ 10163/* Function: ipf_inet6_mask_add */ 10164/* Returns: Nil */ 10165/* Parameters: bits(I) - number of bits set in mask */ 10166/* mask(I) - pointer to mask to add */ 10167/* mtab(I) - pointer to mask hash table structure */ 10168/* */ 10169/* When called, bitcount represents the mask of a IPv6 NAT map rule that */ 10170/* has just been added. This function inserts a bitmask into the array of */ 10171/* masks to search when searching for a matching NAT rule for a packet. */ 10172/* Prevention of duplicate masks is achieved by checking the use count for */ 10173/* a given netmask. */ 10174/* ------------------------------------------------------------------------ */ 10175void 10176ipf_inet6_mask_add(bits, mask, mtab) 10177 int bits; 10178 i6addr_t *mask; 10179 ipf_v6_masktab_t *mtab; 10180{ 10181 i6addr_t zero; 10182 int i, j; 10183 10184 mtab->imt6_masks[bits]++; 10185 if (mtab->imt6_masks[bits] > 1) 10186 return; 10187 10188 if (bits == 0) { 10189 mask = &zero; 10190 zero.i6[0] = 0; 10191 zero.i6[1] = 0; 10192 zero.i6[2] = 0; 10193 zero.i6[3] = 0; 10194 } 10195 10196 for (i = 0; i < 129; i++) { 10197 if (IP6_LT(&mtab->imt6_active[i], mask)) { 10198 for (j = 128; j > i; j--) 10199 mtab->imt6_active[j] = mtab->imt6_active[j - 1]; 10200 mtab->imt6_active[i] = *mask; 10201 break; 10202 } 10203 } 10204 mtab->imt6_max++; 10205} 10206 10207 10208/* ------------------------------------------------------------------------ */ 10209/* Function: ipf_inet6_mask_del */ 10210/* Returns: Nil */ 10211/* Parameters: bits(I) - number of bits set in mask */ 10212/* mask(I) - pointer to mask to remove */ 10213/* mtab(I) - pointer to mask hash table structure */ 10214/* */ 10215/* Remove the 128bit bitmask represented by "bits" from the collection of */ 10216/* netmasks stored inside of mtab. */ 10217/* ------------------------------------------------------------------------ */ 10218void 10219ipf_inet6_mask_del(bits, mask, mtab) 10220 int bits; 10221 i6addr_t *mask; 10222 ipf_v6_masktab_t *mtab; 10223{ 10224 i6addr_t zero; 10225 int i, j; 10226 10227 mtab->imt6_masks[bits]--; 10228 if (mtab->imt6_masks[bits] > 0) 10229 return; 10230 10231 if (bits == 0) 10232 mask = &zero; 10233 zero.i6[0] = 0; 10234 zero.i6[1] = 0; 10235 zero.i6[2] = 0; 10236 zero.i6[3] = 0; 10237 10238 for (i = 0; i < 129; i++) { 10239 if (IP6_EQ(&mtab->imt6_active[i], mask)) { 10240 for (j = i + 1; j < 129; j++) { 10241 mtab->imt6_active[j - 1] = mtab->imt6_active[j]; 10242 if (IP6_EQ(&mtab->imt6_active[j - 1], &zero)) 10243 break; 10244 } 10245 break; 10246 } 10247 } 10248 mtab->imt6_max--; 10249 ASSERT(mtab->imt6_max >= 0); 10250} 10251#endif 10252