| 1 | /*- |
| 2 | * SPDX-License-Identifier: BSD-2-Clause |
| 3 | * |
| 4 | * Copyright (c) 2001 Daniel Hartmeier |
| 5 | * All rights reserved. |
| 6 | * |
| 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following conditions |
| 9 | * are met: |
| 10 | * |
| 11 | * - Redistributions of source code must retain the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer. |
| 13 | * - Redistributions in binary form must reproduce the above |
| 14 | * copyright notice, this list of conditions and the following |
| 15 | * disclaimer in the documentation and/or other materials provided |
| 16 | * with the distribution. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 21 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 22 | * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 23 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 24 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 25 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 26 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 28 | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 29 | * POSSIBILITY OF SUCH DAMAGE. |
| 30 | * |
| 31 | *	$OpenBSD: pfvar.h,v 1.282 2009/01/29 15:12:28 pyr Exp $ |
| 32 | */ |
| 33 | |
| 34 | #ifndef _NET_PFVAR_H_ |
| 35 | #define _NET_PFVAR_H_ |
| 36 | |
| 37 | #include <sys/param.h> |
| 38 | #include <sys/queue.h> |
| 39 | #include <sys/counter.h> |
| 40 | #include <sys/cpuset.h> |
| 41 | #include <sys/epoch.h> |
| 42 | #include <sys/malloc.h> |
| 43 | #include <sys/nv.h> |
| 44 | #include <sys/refcount.h> |
| 45 | #include <sys/sdt.h> |
| 46 | #include <sys/sysctl.h> |
| 47 | #include <sys/smp.h> |
| 48 | #include <sys/lock.h> |
| 49 | #include <sys/rmlock.h> |
| 50 | #include <sys/tree.h> |
| 51 | #include <sys/seqc.h> |
| 52 | #include <vm/uma.h> |
| 53 | |
| 54 | #include <net/if.h> |
| 55 | #include <net/ethernet.h> |
| 56 | #include <net/radix.h> |
| 57 | #include <netinet/in.h> |
| 58 | #ifdef _KERNEL |
| 59 | #include <netinet/ip.h> |
| 60 | #include <netinet/tcp.h> |
| 61 | #include <netinet/udp.h> |
| 62 | #include <netinet/sctp.h> |
| 63 | #include <netinet/ip_icmp.h> |
| 64 | #include <netinet/icmp6.h> |
| 65 | #endif |
| 66 | |
| 67 | #include <netpfil/pf/pf.h> |
| 68 | #include <netpfil/pf/pf_altq.h> |
| 69 | #include <netpfil/pf/pf_mtag.h> |
| 70 | |
| 71 | #ifdef _KERNEL |
| 72 | |
| 73 | #define PF_PFIL_NOREFRAGMENT 0x80000000 |
| 74 | |
| 75 | #if defined(__arm__) |
| 76 | #define PF_WANT_32_TO_64_COUNTER |
| 77 | #endif |
| 78 | |
| 79 | /* |
| 80 | * A hybrid of 32-bit and 64-bit counters which can be used on platforms where |
| 81 | * counter(9) is very expensive. |
| 82 | * |
| 83 | * As 32-bit counters are expected to overflow, a periodic job sums them up to |
| 84 | * a saved 64-bit state. Fetching the value still walks all CPUs to get the most |
| 85 | * current snapshot. |
| 86 | */ |
| 87 | #ifdef PF_WANT_32_TO_64_COUNTER |
| 88 | struct pf_counter_u64_pcpu { |
| 89 | 	u_int32_t current; |
| 90 | 	u_int32_t snapshot; |
| 91 | }; |
| 92 | |
| 93 | struct pf_counter_u64 { |
| 94 | 	struct pf_counter_u64_pcpu *pfcu64_pcpu; |
| 95 | 	u_int64_t pfcu64_value; |
| 96 | 	seqc_t	pfcu64_seqc; |
| 97 | }; |
| 98 | |
| 99 | static inline int |
| 100 | pf_counter_u64_init(struct pf_counter_u64 *pfcu64, int flags) |
| 101 | { |
| 102 | |
| 103 | 	pfcu64->pfcu64_value = 0; |
| 104 | 	pfcu64->pfcu64_seqc = 0; |
| 105 | 	pfcu64->pfcu64_pcpu = uma_zalloc_pcpu(pcpu_zone_8, flags | M_ZERO); |
| 106 | 	if (__predict_false(pfcu64->pfcu64_pcpu == NULL)) |
| 107 | 		return (ENOMEM); |
| 108 | 	return (0); |
| 109 | } |
| 110 | |
| 111 | static inline void |
| 112 | pf_counter_u64_deinit(struct pf_counter_u64 *pfcu64) |
| 113 | { |
| 114 | |
| 115 | 	uma_zfree_pcpu(pcpu_zone_8, pfcu64->pfcu64_pcpu); |
| 116 | } |
| 117 | |
| 118 | static inline void |
| 119 | pf_counter_u64_critical_enter(void) |
| 120 | { |
| 121 | |
| 122 | 	critical_enter(); |
| 123 | } |
| 124 | |
| 125 | static inline void |
| 126 | pf_counter_u64_critical_exit(void) |
| 127 | { |
| 128 | |
| 129 | 	critical_exit(); |
| 130 | } |
| 131 | |
| 132 | static inline void |
| 133 | pf_counter_u64_rollup_protected(struct pf_counter_u64 *pfcu64, uint64_t n) |
| 134 | { |
| 135 | |
| 136 | 	MPASS(curthread->td_critnest > 0); |
| 137 | 	pfcu64->pfcu64_value += n; |
| 138 | } |
| 139 | |
| 140 | static inline void |
| 141 | pf_counter_u64_add_protected(struct pf_counter_u64 *pfcu64, uint32_t n) |
| 142 | { |
| 143 | 	struct pf_counter_u64_pcpu *pcpu; |
| 144 | 	u_int32_t val; |
| 145 | |
| 146 | 	MPASS(curthread->td_critnest > 0); |
| 147 | 	pcpu = zpcpu_get(pfcu64->pfcu64_pcpu); |
| 148 | 	val = atomic_load_int(&pcpu->current); |
| 149 | 	atomic_store_int(&pcpu->current, val + n); |
| 150 | } |
| 151 | |
| 152 | static inline void |
| 153 | pf_counter_u64_add(struct pf_counter_u64 *pfcu64, uint32_t n) |
| 154 | { |
| 155 | |
| 156 | 	critical_enter(); |
| 157 | 	pf_counter_u64_add_protected(pfcu64, n); |
| 158 | 	critical_exit(); |
| 159 | } |
| 160 | |
| 161 | static inline u_int64_t |
| 162 | pf_counter_u64_periodic(struct pf_counter_u64 *pfcu64) |
| 163 | { |
| 164 | 	struct pf_counter_u64_pcpu *pcpu; |
| 165 | 	u_int64_t sum; |
| 166 | 	u_int32_t val; |
| 167 | 	int cpu; |
| 168 | |
| 169 | 	MPASS(curthread->td_critnest > 0); |
| 170 | 	seqc_write_begin(&pfcu64->pfcu64_seqc); |
| 171 | 	sum = pfcu64->pfcu64_value; |
| 172 | 	CPU_FOREACH(cpu) { |
| 173 | 		pcpu = zpcpu_get_cpu(pfcu64->pfcu64_pcpu, cpu); |
| 174 | 		val = atomic_load_int(&pcpu->current); |
| 175 | 		sum += (uint32_t)(val - pcpu->snapshot); |
| 176 | 		pcpu->snapshot = val; |
| 177 | 	} |
| 178 | 	pfcu64->pfcu64_value = sum; |
| 179 | 	seqc_write_end(&pfcu64->pfcu64_seqc); |
| 180 | 	return (sum); |
| 181 | } |
| 182 | |
| 183 | static inline u_int64_t |
| 184 | pf_counter_u64_fetch(const struct pf_counter_u64 *pfcu64) |
| 185 | { |
| 186 | 	struct pf_counter_u64_pcpu *pcpu; |
| 187 | 	u_int64_t sum; |
| 188 | 	seqc_t seqc; |
| 189 | 	int cpu; |
| 190 | |
| 191 | 	for (;;) { |
| 192 | 		seqc = seqc_read(&pfcu64->pfcu64_seqc); |
| 193 | 		sum = 0; |
| 194 | 		CPU_FOREACH(cpu) { |
| 195 | 			pcpu = zpcpu_get_cpu(pfcu64->pfcu64_pcpu, cpu); |
| 196 | 			sum += (uint32_t)(atomic_load_int(&pcpu->current) -pcpu->snapshot); |
| 197 | 		} |
| 198 | 		sum += pfcu64->pfcu64_value; |
| 199 | 		if (seqc_consistent(&pfcu64->pfcu64_seqc, seqc)) |
| 200 | 			break; |
| 201 | 	} |
| 202 | 	return (sum); |
| 203 | } |
| 204 | |
| 205 | static inline void |
| 206 | pf_counter_u64_zero_protected(struct pf_counter_u64 *pfcu64) |
| 207 | { |
| 208 | 	struct pf_counter_u64_pcpu *pcpu; |
| 209 | 	int cpu; |
| 210 | |
| 211 | 	MPASS(curthread->td_critnest > 0); |
| 212 | 	seqc_write_begin(&pfcu64->pfcu64_seqc); |
| 213 | 	CPU_FOREACH(cpu) { |
| 214 | 		pcpu = zpcpu_get_cpu(pfcu64->pfcu64_pcpu, cpu); |
| 215 | 		pcpu->snapshot = atomic_load_int(&pcpu->current); |
| 216 | 	} |
| 217 | 	pfcu64->pfcu64_value = 0; |
| 218 | 	seqc_write_end(&pfcu64->pfcu64_seqc); |
| 219 | } |
| 220 | |
| 221 | static inline void |
| 222 | pf_counter_u64_zero(struct pf_counter_u64 *pfcu64) |
| 223 | { |
| 224 | |
| 225 | 	critical_enter(); |
| 226 | 	pf_counter_u64_zero_protected(pfcu64); |
| 227 | 	critical_exit(); |
| 228 | } |
| 229 | #else |
| 230 | struct pf_counter_u64 { |
| 231 | 	counter_u64_t counter; |
| 232 | }; |
| 233 | |
| 234 | static inline int |
| 235 | pf_counter_u64_init(struct pf_counter_u64 *pfcu64, int flags) |
| 236 | { |
| 237 | |
| 238 | 	pfcu64->counter = counter_u64_alloc(flags); |
| 239 | 	if (__predict_false(pfcu64->counter == NULL)) |
| 240 | 		return (ENOMEM); |
| 241 | 	return (0); |
| 242 | } |
| 243 | |
| 244 | static inline void |
| 245 | pf_counter_u64_deinit(struct pf_counter_u64 *pfcu64) |
| 246 | { |
| 247 | |
| 248 | 	counter_u64_free(pfcu64->counter); |
| 249 | } |
| 250 | |
| 251 | static inline void |
| 252 | pf_counter_u64_critical_enter(void) |
| 253 | { |
| 254 | |
| 255 | } |
| 256 | |
| 257 | static inline void |
| 258 | pf_counter_u64_critical_exit(void) |
| 259 | { |
| 260 | |
| 261 | } |
| 262 | |
| 263 | static inline void |
| 264 | pf_counter_u64_rollup_protected(struct pf_counter_u64 *pfcu64, uint64_t n) |
| 265 | { |
| 266 | |
| 267 | 	counter_u64_add(pfcu64->counter, n); |
| 268 | } |
| 269 | |
| 270 | static inline void |
| 271 | pf_counter_u64_add_protected(struct pf_counter_u64 *pfcu64, uint32_t n) |
| 272 | { |
| 273 | |
| 274 | 	counter_u64_add(pfcu64->counter, n); |
| 275 | } |
| 276 | |
| 277 | static inline void |
| 278 | pf_counter_u64_add(struct pf_counter_u64 *pfcu64, uint32_t n) |
| 279 | { |
| 280 | |
| 281 | 	pf_counter_u64_add_protected(pfcu64, n); |
| 282 | } |
| 283 | |
| 284 | static inline u_int64_t |
| 285 | pf_counter_u64_fetch(const struct pf_counter_u64 *pfcu64) |
| 286 | { |
| 287 | |
| 288 | 	return (counter_u64_fetch(pfcu64->counter)); |
| 289 | } |
| 290 | |
| 291 | static inline void |
| 292 | pf_counter_u64_zero_protected(struct pf_counter_u64 *pfcu64) |
| 293 | { |
| 294 | |
| 295 | 	counter_u64_zero(pfcu64->counter); |
| 296 | } |
| 297 | |
| 298 | static inline void |
| 299 | pf_counter_u64_zero(struct pf_counter_u64 *pfcu64) |
| 300 | { |
| 301 | |
| 302 | 	pf_counter_u64_zero_protected(pfcu64); |
| 303 | } |
| 304 | #endif |
| 305 | |
| 306 | #define pf_get_timestamp(prule)({					\ |
| 307 | 	uint32_t _ts = 0;						\ |
| 308 | 	uint32_t __ts;							\ |
| 309 | 	int cpu;							\ |
| 310 | 	CPU_FOREACH(cpu) {						\ |
| 311 | 		__ts = *zpcpu_get_cpu(prule->timestamp, cpu);		\ |
| 312 | 		if (__ts > _ts)						\ |
| 313 | 			_ts = __ts;					\ |
| 314 | 	}								\ |
| 315 | 	_ts;								\ |
| 316 | }) |
| 317 | |
| 318 | #define pf_update_timestamp(prule)					\ |
| 319 | 	do {								\ |
| 320 | 		critical_enter();					\ |
| 321 | 		*zpcpu_get((prule)->timestamp) = time_second;		\ |
| 322 | 		critical_exit();					\ |
| 323 | 	} while (0) |
| 324 | |
| 325 | #define pf_timestamp_pcpu_zone	(sizeof(time_t) == 4 ? pcpu_zone_4 : pcpu_zone_8) |
| 326 | _Static_assert(sizeof(time_t) == 4 || sizeof(time_t) == 8, "unexpected time_t size"); |
| 327 | |
| 328 | SYSCTL_DECL(_net_pf); |
| 329 | MALLOC_DECLARE(M_PF); |
| 330 | MALLOC_DECLARE(M_PFHASH); |
| 331 | MALLOC_DECLARE(M_PF_RULE_ITEM); |
| 332 | |
| 333 | SDT_PROVIDER_DECLARE(pf); |
| 334 | SDT_PROBE_DECLARE(pf, , test, reason_set); |
| 335 | SDT_PROBE_DECLARE(pf, , log, log); |
| 336 | |
| 337 | #define DPFPRINTF(n, fmt, x...)				\ |
| 338 | 	do {						\ |
| 339 | 		SDT_PROBE2(pf, , log, log, (n), fmt);	\ |
| 340 | 		if (V_pf_status.debug >= (n))	 	\ |
| 341 | 			printf(fmt "\n", ##x); 		\ |
| 342 | 	} while (0) |
| 343 | |
| 344 | struct pfi_dynaddr { |
| 345 | 	TAILQ_ENTRY(pfi_dynaddr)	 entry; |
| 346 | 	struct pf_addr			 pfid_addr4; |
| 347 | 	struct pf_addr			 pfid_mask4; |
| 348 | 	struct pf_addr			 pfid_addr6; |
| 349 | 	struct pf_addr			 pfid_mask6; |
| 350 | 	struct pfr_ktable		*pfid_kt; |
| 351 | 	struct pfi_kkif			*pfid_kif; |
| 352 | 	int				 pfid_net;	/* mask or 128 */ |
| 353 | 	int				 pfid_acnt4;	/* address count IPv4 */ |
| 354 | 	int				 pfid_acnt6;	/* address count IPv6 */ |
| 355 | 	sa_family_t			 pfid_af;	/* rule af */ |
| 356 | 	u_int8_t			 pfid_iflags;	/* PFI_AFLAG_* */ |
| 357 | }; |
| 358 | |
| 359 | #define	PF_NAME		"pf" |
| 360 | |
| 361 | #define	PF_HASHROW_ASSERT(h)	mtx_assert(&(h)->lock, MA_OWNED) |
| 362 | #define	PF_HASHROW_LOCK(h)	mtx_lock(&(h)->lock) |
| 363 | #define	PF_HASHROW_UNLOCK(h)	mtx_unlock(&(h)->lock) |
| 364 | |
| 365 | #ifdef INVARIANTS |
| 366 | #define	PF_STATE_LOCK(s)						\ |
| 367 | 	do {								\ |
| 368 | 		struct pf_kstate *_s = (s);				\ |
| 369 | 		struct pf_idhash *_ih = &V_pf_idhash[PF_IDHASH(_s)];	\ |
| 370 | 		MPASS(_s->lock == &_ih->lock);				\ |
| 371 | 		mtx_lock(_s->lock);					\ |
| 372 | 	} while (0) |
| 373 | #define	PF_STATE_UNLOCK(s)						\ |
| 374 | 	do {								\ |
| 375 | 		struct pf_kstate *_s = (s);				\ |
| 376 | 		struct pf_idhash *_ih = &V_pf_idhash[PF_IDHASH(_s)];	\ |
| 377 | 		MPASS(_s->lock == &_ih->lock);				\ |
| 378 | 		mtx_unlock(_s->lock);					\ |
| 379 | 	} while (0) |
| 380 | #else |
| 381 | #define	PF_STATE_LOCK(s)	mtx_lock((s)->lock) |
| 382 | #define	PF_STATE_UNLOCK(s)	mtx_unlock((s)->lock) |
| 383 | #endif |
| 384 | |
| 385 | #ifdef INVARIANTS |
| 386 | #define	PF_STATE_LOCK_ASSERT(s)						\ |
| 387 | 	do {								\ |
| 388 | 		struct pf_kstate *_s = (s);				\ |
| 389 | 		struct pf_idhash *_ih = &V_pf_idhash[PF_IDHASH(_s)];	\ |
| 390 | 		MPASS(_s->lock == &_ih->lock);				\ |
| 391 | 		PF_HASHROW_ASSERT(_ih);					\ |
| 392 | 	} while (0) |
| 393 | #else /* !INVARIANTS */ |
| 394 | #define	PF_STATE_LOCK_ASSERT(s)		do {} while (0) |
| 395 | #endif /* INVARIANTS */ |
| 396 | |
| 397 | #ifdef INVARIANTS |
| 398 | #define	PF_SRC_NODE_LOCK(sn)						\ |
| 399 | 	do {								\ |
| 400 | 		struct pf_ksrc_node *_sn = (sn);			\ |
| 401 | 		struct pf_srchash *_sh = &V_pf_srchash[			\ |
| 402 | 		 pf_hashsrc(&_sn->addr, _sn->af)];			\ |
| 403 | 		MPASS(_sn->lock == &_sh->lock);				\ |
| 404 | 		mtx_lock(_sn->lock);					\ |
| 405 | 	} while (0) |
| 406 | #define	PF_SRC_NODE_UNLOCK(sn)						\ |
| 407 | 	do {								\ |
| 408 | 		struct pf_ksrc_node *_sn = (sn);			\ |
| 409 | 		struct pf_srchash *_sh = &V_pf_srchash[			\ |
| 410 | 		 pf_hashsrc(&_sn->addr, _sn->af)];			\ |
| 411 | 		MPASS(_sn->lock == &_sh->lock);				\ |
| 412 | 		mtx_unlock(_sn->lock);					\ |
| 413 | 	} while (0) |
| 414 | #else |
| 415 | #define	PF_SRC_NODE_LOCK(sn)	mtx_lock((sn)->lock) |
| 416 | #define	PF_SRC_NODE_UNLOCK(sn)	mtx_unlock((sn)->lock) |
| 417 | #endif |
| 418 | |
| 419 | #ifdef INVARIANTS |
| 420 | #define	PF_SRC_NODE_LOCK_ASSERT(sn)					\ |
| 421 | 	do {								\ |
| 422 | 		struct pf_ksrc_node *_sn = (sn);			\ |
| 423 | 		struct pf_srchash *_sh = &V_pf_srchash[			\ |
| 424 | 		 pf_hashsrc(&_sn->addr, _sn->af)];			\ |
| 425 | 		MPASS(_sn->lock == &_sh->lock);				\ |
| 426 | 		PF_HASHROW_ASSERT(_sh);					\ |
| 427 | 	} while (0) |
| 428 | #else /* !INVARIANTS */ |
| 429 | #define	PF_SRC_NODE_LOCK_ASSERT(sn)		do {} while (0) |
| 430 | #endif /* INVARIANTS */ |
| 431 | |
| 432 | extern struct mtx_padalign pf_unlnkdrules_mtx; |
| 433 | #define	PF_UNLNKDRULES_LOCK()	mtx_lock(&pf_unlnkdrules_mtx) |
| 434 | #define	PF_UNLNKDRULES_UNLOCK()	mtx_unlock(&pf_unlnkdrules_mtx) |
| 435 | #define	PF_UNLNKDRULES_ASSERT()	mtx_assert(&pf_unlnkdrules_mtx, MA_OWNED) |
| 436 | |
| 437 | extern struct sx pf_config_lock; |
| 438 | #define	PF_CONFIG_LOCK()	sx_xlock(&pf_config_lock) |
| 439 | #define	PF_CONFIG_UNLOCK()	sx_xunlock(&pf_config_lock) |
| 440 | #define	PF_CONFIG_ASSERT()	sx_assert(&pf_config_lock, SA_XLOCKED) |
| 441 | |
| 442 | VNET_DECLARE(struct rmlock, pf_rules_lock); |
| 443 | #define	V_pf_rules_lock		VNET(pf_rules_lock) |
| 444 | |
| 445 | #define	PF_RULES_RLOCK_TRACKER	struct rm_priotracker _pf_rules_tracker |
| 446 | #define	PF_RULES_RLOCK()	rm_rlock(&V_pf_rules_lock, &_pf_rules_tracker) |
| 447 | #define	PF_RULES_RUNLOCK()	rm_runlock(&V_pf_rules_lock, &_pf_rules_tracker) |
| 448 | #define	PF_RULES_WLOCK()	rm_wlock(&V_pf_rules_lock) |
| 449 | #define	PF_RULES_WUNLOCK()	rm_wunlock(&V_pf_rules_lock) |
| 450 | #define	PF_RULES_WOWNED()	rm_wowned(&V_pf_rules_lock) |
| 451 | #define	PF_RULES_ASSERT()	rm_assert(&V_pf_rules_lock, RA_LOCKED) |
| 452 | #define	PF_RULES_RASSERT()	rm_assert(&V_pf_rules_lock, RA_RLOCKED) |
| 453 | #define	PF_RULES_WASSERT()	rm_assert(&V_pf_rules_lock, RA_WLOCKED) |
| 454 | |
| 455 | VNET_DECLARE(struct rmlock, pf_tags_lock); |
| 456 | #define	V_pf_tags_lock		VNET(pf_tags_lock) |
| 457 | |
| 458 | #define	PF_TAGS_RLOCK_TRACKER	struct rm_priotracker _pf_tags_tracker |
| 459 | #define	PF_TAGS_RLOCK()		rm_rlock(&V_pf_tags_lock, &_pf_tags_tracker) |
| 460 | #define	PF_TAGS_RUNLOCK()	rm_runlock(&V_pf_tags_lock, &_pf_tags_tracker) |
| 461 | #define	PF_TAGS_WLOCK()		rm_wlock(&V_pf_tags_lock) |
| 462 | #define	PF_TAGS_WUNLOCK()	rm_wunlock(&V_pf_tags_lock) |
| 463 | #define	PF_TAGS_WASSERT()	rm_assert(&V_pf_tags_lock, RA_WLOCKED) |
| 464 | |
| 465 | extern struct mtx_padalign pf_table_stats_lock; |
| 466 | #define	PF_TABLE_STATS_LOCK()	mtx_lock(&pf_table_stats_lock) |
| 467 | #define	PF_TABLE_STATS_UNLOCK()	mtx_unlock(&pf_table_stats_lock) |
| 468 | #define	PF_TABLE_STATS_OWNED()	mtx_owned(&pf_table_stats_lock) |
| 469 | #define	PF_TABLE_STATS_ASSERT()	mtx_assert(&pf_table_stats_lock, MA_OWNED) |
| 470 | |
| 471 | extern struct sx pf_end_lock; |
| 472 | |
| 473 | #define	PF_MODVER	1 |
| 474 | #define	PFLOG_MODVER	1 |
| 475 | #define	PFSYNC_MODVER	1 |
| 476 | |
| 477 | #define	PFLOG_MINVER	1 |
| 478 | #define	PFLOG_PREFVER	PFLOG_MODVER |
| 479 | #define	PFLOG_MAXVER	1 |
| 480 | #define	PFSYNC_MINVER	1 |
| 481 | #define	PFSYNC_PREFVER	PFSYNC_MODVER |
| 482 | #define	PFSYNC_MAXVER	1 |
| 483 | |
| 484 | #ifdef INET |
| 485 | #ifndef INET6 |
| 486 | #define	PF_INET_ONLY |
| 487 | #endif /* ! INET6 */ |
| 488 | #endif /* INET */ |
| 489 | |
| 490 | #ifdef INET6 |
| 491 | #ifndef INET |
| 492 | #define	PF_INET6_ONLY |
| 493 | #endif /* ! INET */ |
| 494 | #endif /* INET6 */ |
| 495 | |
| 496 | #ifdef INET |
| 497 | #ifdef INET6 |
| 498 | #define	PF_INET_INET6 |
| 499 | #endif /* INET6 */ |
| 500 | #endif /* INET */ |
| 501 | |
| 502 | #else |
| 503 | |
| 504 | #define	PF_INET_INET6 |
| 505 | |
| 506 | #endif /* _KERNEL */ |
| 507 | |
| 508 | /* Both IPv4 and IPv6 */ |
| 509 | #ifdef PF_INET_INET6 |
| 510 | |
| 511 | #define PF_AEQ(a, b, c) \ |
| 512 | 	((c == AF_INET && (a)->addr32[0] == (b)->addr32[0]) || \ |
| 513 | 	(c == AF_INET6 && (a)->addr32[3] == (b)->addr32[3] && \ |
| 514 | 	(a)->addr32[2] == (b)->addr32[2] && \ |
| 515 | 	(a)->addr32[1] == (b)->addr32[1] && \ |
| 516 | 	(a)->addr32[0] == (b)->addr32[0])) \ |
| 517 | |
| 518 | #define PF_ANEQ(a, b, c) \ |
| 519 | 	((c == AF_INET && (a)->addr32[0] != (b)->addr32[0]) || \ |
| 520 | 	(c == AF_INET6 && ((a)->addr32[0] != (b)->addr32[0] || \ |
| 521 | 	(a)->addr32[1] != (b)->addr32[1] || \ |
| 522 | 	(a)->addr32[2] != (b)->addr32[2] || \ |
| 523 | 	(a)->addr32[3] != (b)->addr32[3]))) \ |
| 524 | |
| 525 | #define PF_AZERO(a, c) \ |
| 526 | 	((c == AF_INET && !(a)->addr32[0]) || \ |
| 527 | 	(c == AF_INET6 && !(a)->addr32[0] && !(a)->addr32[1] && \ |
| 528 | 	!(a)->addr32[2] && !(a)->addr32[3] )) \ |
| 529 | |
| 530 | #else |
| 531 | |
| 532 | /* Just IPv6 */ |
| 533 | |
| 534 | #ifdef PF_INET6_ONLY |
| 535 | |
| 536 | #define PF_AEQ(a, b, c) \ |
| 537 | 	((a)->addr32[3] == (b)->addr32[3] && \ |
| 538 | 	(a)->addr32[2] == (b)->addr32[2] && \ |
| 539 | 	(a)->addr32[1] == (b)->addr32[1] && \ |
| 540 | 	(a)->addr32[0] == (b)->addr32[0]) \ |
| 541 | |
| 542 | #define PF_ANEQ(a, b, c) \ |
| 543 | 	((a)->addr32[3] != (b)->addr32[3] || \ |
| 544 | 	(a)->addr32[2] != (b)->addr32[2] || \ |
| 545 | 	(a)->addr32[1] != (b)->addr32[1] || \ |
| 546 | 	(a)->addr32[0] != (b)->addr32[0]) \ |
| 547 | |
| 548 | #define PF_AZERO(a, c) \ |
| 549 | 	(!(a)->addr32[0] && \ |
| 550 | 	!(a)->addr32[1] && \ |
| 551 | 	!(a)->addr32[2] && \ |
| 552 | 	!(a)->addr32[3] ) \ |
| 553 | |
| 554 | #else |
| 555 | |
| 556 | /* Just IPv4 */ |
| 557 | #ifdef PF_INET_ONLY |
| 558 | |
| 559 | #define PF_AEQ(a, b, c) \ |
| 560 | 	((a)->addr32[0] == (b)->addr32[0]) |
| 561 | |
| 562 | #define PF_ANEQ(a, b, c) \ |
| 563 | 	((a)->addr32[0] != (b)->addr32[0]) |
| 564 | |
| 565 | #define PF_AZERO(a, c) \ |
| 566 | 	(!(a)->addr32[0]) |
| 567 | |
| 568 | #endif /* PF_INET_ONLY */ |
| 569 | #endif /* PF_INET6_ONLY */ |
| 570 | #endif /* PF_INET_INET6 */ |
| 571 | |
| 572 | #ifdef _KERNEL |
| 573 | |
| 574 | void				 unhandled_af(int) __dead2; |
| 575 | |
| 576 | static void inline |
| 577 | pf_addrcpy(struct pf_addr *dst, const struct pf_addr *src, sa_family_t af) |
| 578 | { |
| 579 | 	switch (af) { |
| 580 | #ifdef INET |
| 581 | 	case AF_INET: |
| 582 | 		memcpy(&dst->v4, &src->v4, sizeof(dst->v4)); |
| 583 | 		break; |
| 584 | #endif /* INET */ |
| 585 | #ifdef INET6 |
| 586 | 	case AF_INET6: |
| 587 | 		memcpy(&dst->v6, &src->v6, sizeof(dst->v6)); |
| 588 | 		break; |
| 589 | #endif /* INET6 */ |
| 590 | 	default: |
| 591 | 		unhandled_af(af); |
| 592 | 	} |
| 593 | } |
| 594 | #endif |
| 595 | |
| 596 | /* |
| 597 | * XXX callers not FIB-aware in our version of pf yet. |
| 598 | * OpenBSD fixed it later it seems, 2010/05/07 13:33:16 claudio. |
| 599 | */ |
| 600 | #define	PF_MISMATCHAW(aw, x, af, neg, ifp, rtid)			\ |
| 601 | 	(								\ |
| 602 | 		(((aw)->type == PF_ADDR_NOROUTE &&			\ |
| 603 | 		 pf_routable((x), (af), NULL, (rtid))) ||		\ |
| 604 | 		(((aw)->type == PF_ADDR_URPFFAILED && (ifp) != NULL &&	\ |
| 605 | 		 pf_routable((x), (af), (ifp), (rtid))) ||		\ |
| 606 | 		((aw)->type == PF_ADDR_TABLE &&				\ |
| 607 | 		 !pfr_match_addr((aw)->p.tbl, (x), (af))) ||		\ |
| 608 | 		((aw)->type == PF_ADDR_DYNIFTL &&			\ |
| 609 | 		 !pfi_match_addr((aw)->p.dyn, (x), (af))) ||		\ |
| 610 | 		((aw)->type == PF_ADDR_RANGE &&				\ |
| 611 | 		 !pf_match_addr_range(&(aw)->v.a.addr,		\ |
| 612 | 		 &(aw)->v.a.mask, (x), (af))) ||			\ |
| 613 | 		((aw)->type == PF_ADDR_ADDRMASK &&			\ |
| 614 | 		 !PF_AZERO(&(aw)->v.a.mask, (af)) &&			\ |
| 615 | 		 !pf_match_addr(0, &(aw)->v.a.addr,			\ |
| 616 | 		 &(aw)->v.a.mask, (x), (af))))) !=			\ |
| 617 | 		(neg)							\ |
| 618 | 	) |
| 619 | |
| 620 | #define PF_ALGNMNT(off) (((off) % 2) == 0) |
| 621 | |
| 622 | /* |
| 623 | * At the moment there are no rules which have both NAT and RDR actions, |
| 624 | * apart from af-to rules, but those don't to source tracking for address |
| 625 | * translation. And the r->rdr pool is used for both NAT and RDR. |
| 626 | * So there is no PF_SN_RDR. |
| 627 | */ |
| 628 | enum pf_sn_types { PF_SN_LIMIT, PF_SN_NAT, PF_SN_ROUTE, PF_SN_MAX }; |
| 629 | typedef enum pf_sn_types pf_sn_types_t; |
| 630 | #define PF_SN_TYPE_NAMES { \ |
| 631 | 	"limit source-track", \ |
| 632 | 	"NAT/RDR sticky-address", \ |
| 633 | 	"route sticky-address", \ |
| 634 | 	NULL \ |
| 635 | } |
| 636 | |
| 637 | #ifdef _KERNEL |
| 638 | |
| 639 | struct pf_kpooladdr { |
| 640 | 	struct pf_addr_wrap		 addr; |
| 641 | 	TAILQ_ENTRY(pf_kpooladdr)	 entries; |
| 642 | 	char				 ifname[IFNAMSIZ]; |
| 643 | 	sa_family_t		 	 af; |
| 644 | 	struct pfi_kkif			*kif; |
| 645 | }; |
| 646 | |
| 647 | TAILQ_HEAD(pf_kpalist, pf_kpooladdr); |
| 648 | |
| 649 | struct pf_kpool { |
| 650 | 	struct mtx		 mtx; |
| 651 | 	struct pf_kpalist	 list; |
| 652 | 	struct pf_kpooladdr	*cur; |
| 653 | 	struct pf_poolhashkey	 key; |
| 654 | 	struct pf_addr		 counter; |
| 655 | 	struct pf_mape_portset	 mape; |
| 656 | 	int			 tblidx; |
| 657 | 	u_int16_t		 proxy_port[2]; |
| 658 | 	u_int8_t		 opts; |
| 659 | 	sa_family_t		 ipv6_nexthop_af; |
| 660 | }; |
| 661 | |
| 662 | struct pf_rule_actions { |
| 663 | 	struct pf_addr	 rt_addr; |
| 664 | 	struct pfi_kkif	*rt_kif; |
| 665 | 	int32_t		 rtableid; |
| 666 | 	uint32_t	 flags; |
| 667 | 	uint16_t	 qid; |
| 668 | 	uint16_t	 pqid; |
| 669 | 	uint16_t	 max_mss; |
| 670 | 	uint16_t	 dnpipe; |
| 671 | 	uint16_t	 dnrpipe;	/* Reverse direction pipe */ |
| 672 | 	sa_family_t	 rt_af; |
| 673 | 	uint8_t		 log; |
| 674 | 	uint8_t		 set_tos; |
| 675 | 	uint8_t		 min_ttl; |
| 676 | 	uint8_t		 set_prio[2]; |
| 677 | 	uint8_t		 rt; |
| 678 | 	uint8_t		 allow_opts; |
| 679 | 	uint16_t	 max_pkt_size; |
| 680 | }; |
| 681 | |
| 682 | union pf_keth_rule_ptr { |
| 683 | 	struct pf_keth_rule	*ptr; |
| 684 | 	uint32_t		nr; |
| 685 | }; |
| 686 | |
| 687 | struct pf_keth_rule_addr { |
| 688 | 	uint8_t	addr[ETHER_ADDR_LEN]; |
| 689 | 	uint8_t	mask[ETHER_ADDR_LEN]; |
| 690 | 	bool neg; |
| 691 | 	uint8_t	isset; |
| 692 | }; |
| 693 | |
| 694 | struct pf_keth_anchor; |
| 695 | |
| 696 | TAILQ_HEAD(pf_keth_ruleq, pf_keth_rule); |
| 697 | |
| 698 | struct pf_keth_ruleset { |
| 699 | 	struct pf_keth_ruleq		 rules[2]; |
| 700 | 	struct pf_keth_rules { |
| 701 | 		struct pf_keth_ruleq	*rules; |
| 702 | 		int			 open; |
| 703 | 		uint32_t		 ticket; |
| 704 | 	} active, inactive; |
| 705 | 	struct vnet		*vnet; |
| 706 | 	struct pf_keth_anchor	*anchor; |
| 707 | }; |
| 708 | |
| 709 | RB_HEAD(pf_keth_anchor_global, pf_keth_anchor); |
| 710 | RB_HEAD(pf_keth_anchor_node, pf_keth_anchor); |
| 711 | struct pf_keth_anchor { |
| 712 | 	RB_ENTRY(pf_keth_anchor)	 entry_node; |
| 713 | 	RB_ENTRY(pf_keth_anchor)	 entry_global; |
| 714 | 	struct pf_keth_anchor		*parent; |
| 715 | 	struct pf_keth_anchor_node	 children; |
| 716 | 	char				 name[PF_ANCHOR_NAME_SIZE]; |
| 717 | 	char				 path[MAXPATHLEN]; |
| 718 | 	struct pf_keth_ruleset		 ruleset; |
| 719 | 	int				 refcnt;	/* anchor rules */ |
| 720 | 	uint8_t				 anchor_relative; |
| 721 | 	uint8_t				 anchor_wildcard; |
| 722 | }; |
| 723 | RB_PROTOTYPE(pf_keth_anchor_node, pf_keth_anchor, entry_node, |
| 724 | pf_keth_anchor_compare); |
| 725 | RB_PROTOTYPE(pf_keth_anchor_global, pf_keth_anchor, entry_global, |
| 726 | pf_keth_anchor_compare); |
| 727 | |
| 728 | struct pf_keth_rule { |
| 729 | #define PFE_SKIP_IFP		0 |
| 730 | #define PFE_SKIP_DIR		1 |
| 731 | #define PFE_SKIP_PROTO		2 |
| 732 | #define PFE_SKIP_SRC_ADDR	3 |
| 733 | #define PFE_SKIP_DST_ADDR	4 |
| 734 | #define PFE_SKIP_SRC_IP_ADDR	5 |
| 735 | #define PFE_SKIP_DST_IP_ADDR	6 |
| 736 | #define PFE_SKIP_COUNT		7 |
| 737 | 	union pf_keth_rule_ptr	 skip[PFE_SKIP_COUNT]; |
| 738 | |
| 739 | 	TAILQ_ENTRY(pf_keth_rule)	entries; |
| 740 | |
| 741 | 	struct pf_keth_anchor	*anchor; |
| 742 | 	u_int8_t		 anchor_relative; |
| 743 | 	u_int8_t		 anchor_wildcard; |
| 744 | |
| 745 | 	uint32_t		 nr; |
| 746 | |
| 747 | 	bool			 quick; |
| 748 | |
| 749 | 	/* Filter */ |
| 750 | 	char			 ifname[IFNAMSIZ]; |
| 751 | 	struct pfi_kkif		*kif; |
| 752 | 	bool			 ifnot; |
| 753 | 	uint8_t			 direction; |
| 754 | 	uint16_t		 proto; |
| 755 | 	struct pf_keth_rule_addr src, dst; |
| 756 | 	struct pf_rule_addr	 ipsrc, ipdst; |
| 757 | 	char			 match_tagname[PF_TAG_NAME_SIZE]; |
| 758 | 	uint16_t		 match_tag; |
| 759 | 	bool			 match_tag_not; |
| 760 | |
| 761 | |
| 762 | 	/* Stats */ |
| 763 | 	counter_u64_t		 evaluations; |
| 764 | 	counter_u64_t		 packets[2]; |
| 765 | 	counter_u64_t		 bytes[2]; |
| 766 | 	time_t			*timestamp; |
| 767 | |
| 768 | 	/* Action */ |
| 769 | 	char			 qname[PF_QNAME_SIZE]; |
| 770 | 	int			 qid; |
| 771 | 	char			 tagname[PF_TAG_NAME_SIZE]; |
| 772 | 	uint16_t		 tag; |
| 773 | 	char			 bridge_to_name[IFNAMSIZ]; |
| 774 | 	struct pfi_kkif		*bridge_to; |
| 775 | 	uint8_t			 action; |
| 776 | 	uint16_t		 dnpipe; |
| 777 | 	uint32_t		 dnflags; |
| 778 | |
| 779 | 	char			label[PF_RULE_MAX_LABEL_COUNT][PF_RULE_LABEL_SIZE]; |
| 780 | 	uint32_t		ridentifier; |
| 781 | }; |
| 782 | |
| 783 | struct pf_kthreshold { |
| 784 | 	uint32_t		 limit; |
| 785 | 	uint32_t		 seconds; |
| 786 | 	struct counter_rate	*cr; |
| 787 | }; |
| 788 | |
| 789 | RB_HEAD(pf_krule_global, pf_krule); |
| 790 | RB_PROTOTYPE(pf_krule_global, pf_krule, entry_global, pf_krule_compare); |
| 791 | |
| 792 | struct pf_krule { |
| 793 | 	struct pf_rule_addr	 src; |
| 794 | 	struct pf_rule_addr	 dst; |
| 795 | 	struct pf_krule		*skip[PF_SKIP_COUNT]; |
| 796 | 	char			 label[PF_RULE_MAX_LABEL_COUNT][PF_RULE_LABEL_SIZE]; |
| 797 | 	uint32_t		 ridentifier; |
| 798 | 	char			 ifname[IFNAMSIZ]; |
| 799 | 	char			 rcv_ifname[IFNAMSIZ]; |
| 800 | 	char			 qname[PF_QNAME_SIZE]; |
| 801 | 	char			 pqname[PF_QNAME_SIZE]; |
| 802 | 	char			 tagname[PF_TAG_NAME_SIZE]; |
| 803 | 	char			 match_tagname[PF_TAG_NAME_SIZE]; |
| 804 | |
| 805 | 	char			 overload_tblname[PF_TABLE_NAME_SIZE]; |
| 806 | |
| 807 | 	TAILQ_ENTRY(pf_krule)	 entries; |
| 808 | 	struct pf_kpool		 nat; |
| 809 | 	struct pf_kpool		 rdr; |
| 810 | 	struct pf_kpool		 route; |
| 811 | 	struct pf_kthreshold	 pktrate; |
| 812 | |
| 813 | 	struct pf_counter_u64	 evaluations; |
| 814 | 	struct pf_counter_u64	 packets[2]; |
| 815 | 	struct pf_counter_u64	 bytes[2]; |
| 816 | 	time_t			*timestamp; |
| 817 | |
| 818 | 	struct pfi_kkif		*kif; |
| 819 | 	struct pfi_kkif		*rcv_kif; |
| 820 | 	struct pf_kanchor	*anchor; |
| 821 | 	struct pfr_ktable	*overload_tbl; |
| 822 | |
| 823 | 	pf_osfp_t		 os_fingerprint; |
| 824 | |
| 825 | 	int32_t			 rtableid; |
| 826 | 	u_int32_t		 timeout[PFTM_MAX]; |
| 827 | 	u_int32_t		 max_states; |
| 828 | 	u_int32_t		 max_src_nodes; |
| 829 | 	u_int32_t		 max_src_states; |
| 830 | 	u_int32_t		 max_src_conn; |
| 831 | 	struct { |
| 832 | 		u_int32_t		limit; |
| 833 | 		u_int32_t		seconds; |
| 834 | 	}			 max_src_conn_rate; |
| 835 | 	uint16_t		 max_pkt_size; |
| 836 | 	u_int16_t		 qid; |
| 837 | 	u_int16_t		 pqid; |
| 838 | 	u_int16_t		 dnpipe; |
| 839 | 	u_int16_t		 dnrpipe; |
| 840 | 	u_int32_t		 free_flags; |
| 841 | 	u_int32_t		 nr; |
| 842 | 	u_int32_t		 prob; |
| 843 | 	uid_t			 cuid; |
| 844 | 	pid_t			 cpid; |
| 845 | |
| 846 | 	counter_u64_t		 states_cur; |
| 847 | 	counter_u64_t		 states_tot; |
| 848 | 	counter_u64_t		 src_nodes[PF_SN_MAX]; |
| 849 | |
| 850 | 	u_int16_t		 return_icmp; |
| 851 | 	u_int16_t		 return_icmp6; |
| 852 | 	u_int16_t		 max_mss; |
| 853 | 	u_int16_t		 tag; |
| 854 | 	u_int16_t		 match_tag; |
| 855 | 	u_int16_t		 scrub_flags; |
| 856 | |
| 857 | 	struct pf_rule_uid	 uid; |
| 858 | 	struct pf_rule_gid	 gid; |
| 859 | |
| 860 | 	u_int32_t		 rule_flag; |
| 861 | 	uint32_t		 rule_ref; |
| 862 | 	u_int8_t		 action; |
| 863 | 	u_int8_t		 direction; |
| 864 | 	u_int8_t		 log; |
| 865 | 	u_int8_t		 logif; |
| 866 | 	u_int8_t		 quick; |
| 867 | 	u_int8_t		 ifnot; |
| 868 | 	u_int8_t		 match_tag_not; |
| 869 | 	u_int8_t		 natpass; |
| 870 | |
| 871 | 	u_int8_t		 keep_state; |
| 872 | 	sa_family_t		 af; |
| 873 | 	u_int8_t		 proto; |
| 874 | 	uint16_t		 type; |
| 875 | 	uint16_t		 code; |
| 876 | 	u_int8_t		 flags; |
| 877 | 	u_int8_t		 flagset; |
| 878 | 	u_int8_t		 min_ttl; |
| 879 | 	u_int8_t		 allow_opts; |
| 880 | 	u_int8_t		 rt; |
| 881 | 	u_int8_t		 return_ttl; |
| 882 | 	u_int8_t		 tos; |
| 883 | 	u_int8_t		 set_tos; |
| 884 | 	u_int8_t		 anchor_relative; |
| 885 | 	u_int8_t		 anchor_wildcard; |
| 886 | |
| 887 | 	u_int8_t		 flush; |
| 888 | 	u_int8_t		 prio; |
| 889 | 	u_int8_t		 set_prio[2]; |
| 890 | 	sa_family_t		 naf; |
| 891 | 	u_int8_t		 rcvifnot; |
| 892 | |
| 893 | 	struct { |
| 894 | 		struct pf_addr		addr; |
| 895 | 		u_int16_t		port; |
| 896 | 	}			divert; |
| 897 | 	u_int8_t		 md5sum[PF_MD5_DIGEST_LENGTH]; |
| 898 | 	RB_ENTRY(pf_krule)	 entry_global; |
| 899 | |
| 900 | #ifdef PF_WANT_32_TO_64_COUNTER |
| 901 | 	LIST_ENTRY(pf_krule)	 allrulelist; |
| 902 | 	bool			 allrulelinked; |
| 903 | #endif |
| 904 | }; |
| 905 | |
| 906 | struct pf_krule_item { |
| 907 | 	SLIST_ENTRY(pf_krule_item)	 entry; |
| 908 | 	struct pf_krule			*r; |
| 909 | }; |
| 910 | |
| 911 | SLIST_HEAD(pf_krule_slist, pf_krule_item); |
| 912 | |
| 913 | struct pf_ksrc_node { |
| 914 | 	LIST_ENTRY(pf_ksrc_node) entry; |
| 915 | 	struct pf_addr		 addr; |
| 916 | 	struct pf_addr		 raddr; |
| 917 | 	struct pf_krule_slist	 match_rules; |
| 918 | 	struct pf_krule		*rule; |
| 919 | 	struct pfi_kkif		*rkif; |
| 920 | 	counter_u64_t		 bytes[2]; |
| 921 | 	counter_u64_t		 packets[2]; |
| 922 | 	u_int32_t		 states; |
| 923 | 	u_int32_t		 conn; |
| 924 | 	struct pf_kthreshold	 conn_rate; |
| 925 | 	u_int32_t		 creation; |
| 926 | 	u_int32_t		 expire; |
| 927 | 	sa_family_t		 af; |
| 928 | 	sa_family_t		 raf; |
| 929 | 	u_int8_t		 ruletype; |
| 930 | 	pf_sn_types_t		 type; |
| 931 | 	struct mtx		*lock; |
| 932 | }; |
| 933 | #endif |
| 934 | |
| 935 | struct pf_state_scrub { |
| 936 | 	struct timeval	pfss_last;	/* time received last packet	*/ |
| 937 | 	u_int32_t	pfss_tsecr;	/* last echoed timestamp	*/ |
| 938 | 	u_int32_t	pfss_tsval;	/* largest timestamp		*/ |
| 939 | 	u_int32_t	pfss_tsval0;	/* original timestamp		*/ |
| 940 | 	u_int16_t	pfss_flags; |
| 941 | #define PFSS_TIMESTAMP	0x0001		/* modulate timestamp		*/ |
| 942 | #define PFSS_PAWS	0x0010		/* stricter PAWS checks		*/ |
| 943 | #define PFSS_PAWS_IDLED	0x0020		/* was idle too long. no PAWS	*/ |
| 944 | #define PFSS_DATA_TS	0x0040		/* timestamp on data packets	*/ |
| 945 | #define PFSS_DATA_NOTS	0x0080		/* no timestamp on data packets	*/ |
| 946 | 	u_int8_t	pfss_ttl;	/* stashed TTL			*/ |
| 947 | 	u_int8_t	pad; |
| 948 | 	union { |
| 949 | 		u_int32_t	pfss_ts_mod;	/* timestamp modulation		*/ |
| 950 | 		u_int32_t	pfss_v_tag;	/* SCTP verification tag	*/ |
| 951 | 	}; |
| 952 | }; |
| 953 | |
| 954 | struct pf_state_host { |
| 955 | 	struct pf_addr	addr; |
| 956 | 	u_int16_t	port; |
| 957 | 	u_int16_t	pad; |
| 958 | }; |
| 959 | |
| 960 | struct pf_state_peer { |
| 961 | 	struct pf_state_scrub	*scrub;	/* state is scrubbed		*/ |
| 962 | 	u_int32_t	seqlo;		/* Max sequence number sent	*/ |
| 963 | 	u_int32_t	seqhi;		/* Max the other end ACKd + win	*/ |
| 964 | 	u_int32_t	seqdiff;	/* Sequence number modulator	*/ |
| 965 | 	u_int16_t	max_win;	/* largest window (pre scaling)	*/ |
| 966 | 	u_int16_t	mss;		/* Maximum segment size option	*/ |
| 967 | 	u_int8_t	state;		/* active state level		*/ |
| 968 | 	u_int8_t	wscale;		/* window scaling factor	*/ |
| 969 | 	u_int8_t	tcp_est;	/* Did we reach TCPS_ESTABLISHED */ |
| 970 | 	u_int8_t	pad[1]; |
| 971 | }; |
| 972 | |
| 973 | /* Keep synced with struct pf_udp_endpoint. */ |
| 974 | struct pf_udp_endpoint_cmp { |
| 975 | 	struct pf_addr	addr; |
| 976 | 	uint16_t	port; |
| 977 | 	sa_family_t	af; |
| 978 | 	uint8_t		pad[1]; |
| 979 | }; |
| 980 | |
| 981 | struct pf_udp_endpoint { |
| 982 | 	struct pf_addr	addr; |
| 983 | 	uint16_t	port; |
| 984 | 	sa_family_t	af; |
| 985 | 	uint8_t		pad[1]; |
| 986 | |
| 987 | 	struct pf_udp_mapping *mapping; |
| 988 | 	LIST_ENTRY(pf_udp_endpoint) entry; |
| 989 | }; |
| 990 | |
| 991 | struct pf_udp_mapping { |
| 992 | 	struct pf_udp_endpoint endpoints[2]; |
| 993 | 	u_int refs; |
| 994 | }; |
| 995 | |
| 996 | /* Keep synced with struct pf_state_key. */ |
| 997 | struct pf_state_key_cmp { |
| 998 | 	struct pf_addr	 addr[2]; |
| 999 | 	u_int16_t	 port[2]; |
| 1000 | 	sa_family_t	 af; |
| 1001 | 	u_int8_t	 proto; |
| 1002 | 	u_int8_t	 pad[2]; |
| 1003 | }; |
| 1004 | |
| 1005 | struct pf_state_key { |
| 1006 | 	struct pf_addr	 addr[2]; |
| 1007 | 	u_int16_t	 port[2]; |
| 1008 | 	sa_family_t	 af; |
| 1009 | 	u_int8_t	 proto; |
| 1010 | 	u_int8_t	 pad[2]; |
| 1011 | |
| 1012 | 	LIST_ENTRY(pf_state_key) entry; |
| 1013 | 	TAILQ_HEAD(, pf_kstate)	 states[2]; |
| 1014 | }; |
| 1015 | |
| 1016 | #define PF_REVERSED_KEY(state, family)					\ |
| 1017 | 	(((state)->key[PF_SK_WIRE]->af != (state)->key[PF_SK_STACK]->af) &&	\ |
| 1018 | 	 ((state)->key[PF_SK_WIRE]->af != (family)) &&			\ |
| 1019 | 	 ((state)->direction == PF_IN)) |
| 1020 | |
| 1021 | /* Keep synced with struct pf_kstate. */ |
| 1022 | struct pf_state_cmp { |
| 1023 | 	u_int64_t		 id; |
| 1024 | 	u_int32_t		 creatorid; |
| 1025 | 	u_int8_t		 direction; |
| 1026 | 	u_int8_t		 pad[3]; |
| 1027 | }; |
| 1028 | |
| 1029 | struct pf_state_scrub_export { |
| 1030 | 	uint16_t	pfss_flags; |
| 1031 | 	uint8_t		pfss_ttl;	/* stashed TTL		*/ |
| 1032 | #define PF_SCRUB_FLAG_VALID		0x01 |
| 1033 | 	uint8_t		scrub_flag; |
| 1034 | 	uint32_t	pfss_ts_mod;	/* timestamp modulation	*/ |
| 1035 | } __packed; |
| 1036 | |
| 1037 | struct pf_state_key_export { |
| 1038 | 	struct pf_addr	 addr[2]; |
| 1039 | 	uint16_t	 port[2]; |
| 1040 | }; |
| 1041 | |
| 1042 | struct pf_state_peer_export { |
| 1043 | 	struct pf_state_scrub_export	scrub;	/* state is scrubbed	*/ |
| 1044 | 	uint32_t	seqlo;		/* Max sequence number sent	*/ |
| 1045 | 	uint32_t	seqhi;		/* Max the other end ACKd + win	*/ |
| 1046 | 	uint32_t	seqdiff;	/* Sequence number modulator	*/ |
| 1047 | 	uint16_t	max_win;	/* largest window (pre scaling)	*/ |
| 1048 | 	uint16_t	mss;		/* Maximum segment size option	*/ |
| 1049 | 	uint8_t		state;		/* active state level		*/ |
| 1050 | 	uint8_t		wscale;		/* window scaling factor	*/ |
| 1051 | 	uint8_t		dummy[6]; |
| 1052 | } __packed; |
| 1053 | _Static_assert(sizeof(struct pf_state_peer_export) == 32, "size incorrect"); |
| 1054 | |
| 1055 | struct pf_state_export { |
| 1056 | 	uint64_t	 version; |
| 1057 | #define	PF_STATE_VERSION	20230404 |
| 1058 | 	uint64_t	 id; |
| 1059 | 	char		 ifname[IFNAMSIZ]; |
| 1060 | 	char		 orig_ifname[IFNAMSIZ]; |
| 1061 | 	struct pf_state_key_export	 key[2]; |
| 1062 | 	struct pf_state_peer_export	 src; |
| 1063 | 	struct pf_state_peer_export	 dst; |
| 1064 | 	struct pf_addr	 rt_addr; |
| 1065 | 	uint32_t	 rule; |
| 1066 | 	uint32_t	 anchor; |
| 1067 | 	uint32_t	 nat_rule; |
| 1068 | 	uint32_t	 creation; |
| 1069 | 	uint32_t	 expire; |
| 1070 | 	uint32_t	 spare0; |
| 1071 | 	uint64_t	 packets[2]; |
| 1072 | 	uint64_t	 bytes[2]; |
| 1073 | 	uint32_t	 creatorid; |
| 1074 | 	uint32_t	 spare1; |
| 1075 | 	sa_family_t	 af; |
| 1076 | 	uint8_t		 proto; |
| 1077 | 	uint8_t		 direction; |
| 1078 | 	uint8_t		 log; |
| 1079 | 	uint8_t		 state_flags_compat; |
| 1080 | 	uint8_t		 timeout; |
| 1081 | 	uint8_t		 sync_flags; |
| 1082 | 	uint8_t		 updates; |
| 1083 | 	uint16_t	 state_flags; |
| 1084 | 	uint16_t	 qid; |
| 1085 | 	uint16_t	 pqid; |
| 1086 | 	uint16_t	 dnpipe; |
| 1087 | 	uint16_t	 dnrpipe; |
| 1088 | 	int32_t		 rtableid; |
| 1089 | 	uint8_t		 min_ttl; |
| 1090 | 	uint8_t		 set_tos; |
| 1091 | 	uint16_t	 max_mss; |
| 1092 | 	uint8_t		 set_prio[2]; |
| 1093 | 	uint8_t		 rt; |
| 1094 | 	char		 rt_ifname[IFNAMSIZ]; |
| 1095 | |
| 1096 | 	uint8_t		 spare[72]; |
| 1097 | }; |
| 1098 | _Static_assert(sizeof(struct pf_state_export) == 384, "size incorrect"); |
| 1099 | |
| 1100 | #ifdef _KERNEL |
| 1101 | struct pf_kstate { |
| 1102 | 	/* |
| 1103 | 	 * Area shared with pf_state_cmp |
| 1104 | 	 */ |
| 1105 | 	u_int64_t		 id; |
| 1106 | 	u_int32_t		 creatorid; |
| 1107 | 	u_int8_t		 direction; |
| 1108 | 	u_int8_t		 pad[3]; |
| 1109 | 	/* |
| 1110 | 	 * end of the area |
| 1111 | 	 */ |
| 1112 | |
| 1113 | 	u_int16_t		 state_flags; |
| 1114 | 	u_int8_t		 timeout; |
| 1115 | 	u_int8_t		 sync_state; /* PFSYNC_S_x */ |
| 1116 | 	u_int8_t		 sync_updates; |
| 1117 | 	u_int			 refs; |
| 1118 | 	struct mtx		*lock; |
| 1119 | 	TAILQ_ENTRY(pf_kstate)	 sync_list; |
| 1120 | 	TAILQ_ENTRY(pf_kstate)	 key_list[2]; |
| 1121 | 	LIST_ENTRY(pf_kstate)	 entry; |
| 1122 | 	struct pf_state_peer	 src; |
| 1123 | 	struct pf_state_peer	 dst; |
| 1124 | 	struct pf_krule_slist	 match_rules; |
| 1125 | 	struct pf_krule		*rule; |
| 1126 | 	struct pf_krule		*anchor; |
| 1127 | 	struct pf_krule		*nat_rule; |
| 1128 | 	struct pf_state_key	*key[2];	/* addresses stack and wire */ |
| 1129 | 	struct pf_udp_mapping	*udp_mapping; |
| 1130 | 	struct pfi_kkif		*kif; |
| 1131 | 	struct pfi_kkif		*orig_kif;	/* The real kif, even if we're a floating state (i.e. if == V_pfi_all). */ |
| 1132 | 	struct pf_ksrc_node	*sns[PF_SN_MAX];/* source nodes */ |
| 1133 | 	u_int64_t		 packets[2]; |
| 1134 | 	u_int64_t		 bytes[2]; |
| 1135 | 	u_int64_t		 creation; |
| 1136 | 	u_int64_t	 	 expire; |
| 1137 | 	u_int32_t		 pfsync_time; |
| 1138 | 	struct pf_rule_actions	 act; |
| 1139 | 	u_int16_t		 tag; |
| 1140 | 	u_int16_t		 if_index_in; |
| 1141 | 	u_int16_t		 if_index_out; |
| 1142 | }; |
| 1143 | |
| 1144 | /* |
| 1145 | * 6 cache lines per struct, 10 structs per page. |
| 1146 | * Try to not grow the struct beyond that. |
| 1147 | */ |
| 1148 | _Static_assert(sizeof(struct pf_kstate) <= 384, "pf_kstate size crosses 384 bytes"); |
| 1149 | |
| 1150 | enum pf_test_status { |
| 1151 | 	PF_TEST_FAIL = -1, |
| 1152 | 	PF_TEST_OK, |
| 1153 | 	PF_TEST_QUICK |
| 1154 | }; |
| 1155 | |
| 1156 | struct pf_test_ctx { |
| 1157 | 	enum pf_test_status	 test_status; |
| 1158 | 	struct pf_pdesc		*pd; |
| 1159 | 	struct pf_rule_actions	 act; |
| 1160 | 	uint8_t			 icmpcode; |
| 1161 | 	uint8_t			 icmptype; |
| 1162 | 	int			 icmp_dir; |
| 1163 | 	int			 state_icmp; |
| 1164 | 	int			 tag; |
| 1165 | 	int			 rewrite; |
| 1166 | 	u_short			 reason; |
| 1167 | 	struct pf_src_node	*sns[PF_SN_MAX]; |
| 1168 | 	struct pf_krule		*nr; |
| 1169 | 	struct pf_krule		*tr; |
| 1170 | 	struct pf_krule		**rm; |
| 1171 | 	struct pf_krule		*a; |
| 1172 | 	struct pf_krule		**am; |
| 1173 | 	struct pf_kruleset	**rsm; |
| 1174 | 	struct pf_kruleset	*arsm; |
| 1175 | 	struct pf_kruleset	*aruleset; |
| 1176 | 	struct pf_state_key	*sk; |
| 1177 | 	struct pf_state_key	*nk; |
| 1178 | 	struct tcphdr		*th; |
| 1179 | 	struct pf_udp_mapping	*udp_mapping; |
| 1180 | 	struct pf_kpool		*nat_pool; |
| 1181 | 	uint16_t		 virtual_type; |
| 1182 | 	uint16_t		 virtual_id; |
| 1183 | 	int			 depth; |
| 1184 | }; |
| 1185 | |
| 1186 | #define	PF_ANCHOR_STACK_MAX	32 |
| 1187 | #endif |
| 1188 | |
| 1189 | /* |
| 1190 | * Unified state structures for pulling states out of the kernel |
| 1191 | * used by pfsync(4) and the pf(4) ioctl. |
| 1192 | */ |
| 1193 | struct pfsync_state_key { |
| 1194 | 	struct pf_addr	 addr[2]; |
| 1195 | 	u_int16_t	 port[2]; |
| 1196 | }; |
| 1197 | |
| 1198 | struct pfsync_state_1301 { |
| 1199 | 	u_int64_t	 id; |
| 1200 | 	char		 ifname[IFNAMSIZ]; |
| 1201 | 	struct pfsync_state_key	key[2]; |
| 1202 | 	struct pf_state_peer_export src; |
| 1203 | 	struct pf_state_peer_export dst; |
| 1204 | 	struct pf_addr	 rt_addr; |
| 1205 | 	u_int32_t	 rule; |
| 1206 | 	u_int32_t	 anchor; |
| 1207 | 	u_int32_t	 nat_rule; |
| 1208 | 	u_int32_t	 creation; |
| 1209 | 	u_int32_t	 expire; |
| 1210 | 	u_int32_t	 packets[2][2]; |
| 1211 | 	u_int32_t	 bytes[2][2]; |
| 1212 | 	u_int32_t	 creatorid; |
| 1213 | 	sa_family_t	 af; |
| 1214 | 	u_int8_t	 proto; |
| 1215 | 	u_int8_t	 direction; |
| 1216 | 	u_int8_t	 __spare[2]; |
| 1217 | 	u_int8_t	 log; |
| 1218 | 	u_int8_t	 state_flags; |
| 1219 | 	u_int8_t	 timeout; |
| 1220 | 	u_int8_t	 sync_flags; |
| 1221 | 	u_int8_t	 updates;	/* unused */ |
| 1222 | } __packed; |
| 1223 | |
| 1224 | struct pfsync_state_1400 { |
| 1225 | 	/* The beginning of the struct is compatible with pfsync_state_1301 */ |
| 1226 | 	u_int64_t	 id; |
| 1227 | 	char		 ifname[IFNAMSIZ]; |
| 1228 | 	struct pfsync_state_key	key[2]; |
| 1229 | 	struct pf_state_peer_export src; |
| 1230 | 	struct pf_state_peer_export dst; |
| 1231 | 	struct pf_addr	 rt_addr; |
| 1232 | 	u_int32_t	 rule; |
| 1233 | 	u_int32_t	 anchor; |
| 1234 | 	u_int32_t	 nat_rule; |
| 1235 | 	u_int32_t	 creation; |
| 1236 | 	u_int32_t	 expire; |
| 1237 | 	u_int32_t	 packets[2][2]; |
| 1238 | 	u_int32_t	 bytes[2][2]; |
| 1239 | 	u_int32_t	 creatorid; |
| 1240 | 	sa_family_t	 af; |
| 1241 | 	u_int8_t	 proto; |
| 1242 | 	u_int8_t	 direction; |
| 1243 | 	u_int16_t	 state_flags; |
| 1244 | 	u_int8_t	 log; |
| 1245 | 	u_int8_t	 __spare; |
| 1246 | 	u_int8_t	 timeout; |
| 1247 | 	u_int8_t	 sync_flags; |
| 1248 | 	u_int8_t	 updates;	/* unused */ |
| 1249 | 	/* The rest is not */ |
| 1250 | 	u_int16_t	 qid; |
| 1251 | 	u_int16_t	 pqid; |
| 1252 | 	u_int16_t	 dnpipe; |
| 1253 | 	u_int16_t	 dnrpipe; |
| 1254 | 	int32_t		 rtableid; |
| 1255 | 	u_int8_t	 min_ttl; |
| 1256 | 	u_int8_t	 set_tos; |
| 1257 | 	u_int16_t	 max_mss; |
| 1258 | 	u_int8_t	 set_prio[2]; |
| 1259 | 	u_int8_t	 rt; |
| 1260 | 	char		 rt_ifname[IFNAMSIZ]; |
| 1261 | } __packed; |
| 1262 | |
| 1263 | struct pfsync_state_1500 { |
| 1264 | 	/* The beginning of the struct is compatible with pfsync_state_1301 */ |
| 1265 | 	u_int64_t	 id; |
| 1266 | 	char		 ifname[IFNAMSIZ]; |
| 1267 | 	struct pfsync_state_key	key[2]; |
| 1268 | 	struct pf_state_peer_export src; |
| 1269 | 	struct pf_state_peer_export dst; |
| 1270 | 	struct pf_addr	 rt_addr; |
| 1271 | 	u_int32_t	 rule; |
| 1272 | 	u_int32_t	 anchor; |
| 1273 | 	u_int32_t	 nat_rule; |
| 1274 | 	u_int32_t	 creation; |
| 1275 | 	u_int32_t	 expire; |
| 1276 | 	u_int32_t	 packets[2][2]; |
| 1277 | 	u_int32_t	 bytes[2][2]; |
| 1278 | 	u_int32_t	 creatorid; |
| 1279 | 	/* The rest is not, use the opportunity to fix alignment */ |
| 1280 | 	char		 tagname[PF_TAG_NAME_SIZE]; |
| 1281 | 	char		 rt_ifname[IFNAMSIZ]; |
| 1282 | 	char		 orig_ifname[IFNAMSIZ]; |
| 1283 | 	int32_t		 rtableid; |
| 1284 | 	u_int16_t	 state_flags; |
| 1285 | 	u_int16_t	 qid; |
| 1286 | 	u_int16_t	 pqid; |
| 1287 | 	u_int16_t	 dnpipe; |
| 1288 | 	u_int16_t	 dnrpipe; |
| 1289 | 	u_int16_t	 max_mss; |
| 1290 | 	sa_family_t	 wire_af; |
| 1291 | 	sa_family_t	 stack_af; |
| 1292 | 	sa_family_t	 rt_af; |
| 1293 | 	u_int8_t	 wire_proto; |
| 1294 | 	u_int8_t	 stack_proto; |
| 1295 | 	u_int8_t	 log; |
| 1296 | 	u_int8_t	 timeout; |
| 1297 | 	u_int8_t	 direction; |
| 1298 | 	u_int8_t	 rt; |
| 1299 | 	u_int8_t	 min_ttl; |
| 1300 | 	u_int8_t	 set_tos; |
| 1301 | 	u_int8_t	 set_prio[2]; |
| 1302 | 	u_int8_t	 spare[3];	/* Improve struct alignment */ |
| 1303 | } __packed; |
| 1304 | |
| 1305 | union pfsync_state_union { |
| 1306 | 	struct pfsync_state_1301 pfs_1301; |
| 1307 | 	struct pfsync_state_1400 pfs_1400; |
| 1308 | 	struct pfsync_state_1500 pfs_1500; |
| 1309 | } __packed; |
| 1310 | |
| 1311 | #ifdef _KERNEL |
| 1312 | /* pfsync */ |
| 1313 | typedef int		pfsync_state_import_t(union pfsync_state_union *, int, int); |
| 1314 | typedef	void		pfsync_insert_state_t(struct pf_kstate *); |
| 1315 | typedef	void		pfsync_update_state_t(struct pf_kstate *); |
| 1316 | typedef	void		pfsync_delete_state_t(struct pf_kstate *); |
| 1317 | typedef void		pfsync_clear_states_t(u_int32_t, const char *); |
| 1318 | typedef int		pfsync_defer_t(struct pf_kstate *, struct mbuf *); |
| 1319 | typedef void		pfsync_detach_ifnet_t(struct ifnet *); |
| 1320 | typedef void		pflow_export_state_t(const struct pf_kstate *); |
| 1321 | typedef bool		pf_addr_filter_func_t(const sa_family_t, const struct pf_addr *); |
| 1322 | |
| 1323 | VNET_DECLARE(pfsync_state_import_t *, pfsync_state_import_ptr); |
| 1324 | #define V_pfsync_state_import_ptr	VNET(pfsync_state_import_ptr) |
| 1325 | VNET_DECLARE(pfsync_insert_state_t *, pfsync_insert_state_ptr); |
| 1326 | #define V_pfsync_insert_state_ptr	VNET(pfsync_insert_state_ptr) |
| 1327 | VNET_DECLARE(pfsync_update_state_t *, pfsync_update_state_ptr); |
| 1328 | #define V_pfsync_update_state_ptr	VNET(pfsync_update_state_ptr) |
| 1329 | VNET_DECLARE(pfsync_delete_state_t *, pfsync_delete_state_ptr); |
| 1330 | #define V_pfsync_delete_state_ptr	VNET(pfsync_delete_state_ptr) |
| 1331 | VNET_DECLARE(pfsync_clear_states_t *, pfsync_clear_states_ptr); |
| 1332 | #define V_pfsync_clear_states_ptr	VNET(pfsync_clear_states_ptr) |
| 1333 | VNET_DECLARE(pfsync_defer_t *, pfsync_defer_ptr); |
| 1334 | #define V_pfsync_defer_ptr		VNET(pfsync_defer_ptr) |
| 1335 | VNET_DECLARE(pflow_export_state_t *,	pflow_export_state_ptr); |
| 1336 | #define V_pflow_export_state_ptr	VNET(pflow_export_state_ptr) |
| 1337 | extern pfsync_detach_ifnet_t	*pfsync_detach_ifnet_ptr; |
| 1338 | |
| 1339 | void			pfsync_state_export(union pfsync_state_union *, |
| 1340 | 			 struct pf_kstate *, int); |
| 1341 | void			pf_state_export(struct pf_state_export *, |
| 1342 | 			 struct pf_kstate *); |
| 1343 | |
| 1344 | /* pflog */ |
| 1345 | struct pf_kruleset; |
| 1346 | struct pf_pdesc; |
| 1347 | typedef int pflog_packet_t(uint8_t, u_int8_t, |
| 1348 | struct pf_krule *, struct pf_krule *, struct pf_kruleset *, |
| 1349 | struct pf_pdesc *, int, struct pf_krule *); |
| 1350 | extern pflog_packet_t		*pflog_packet_ptr; |
| 1351 | |
| 1352 | #endif /* _KERNEL */ |
| 1353 | |
| 1354 | #define	PFSYNC_FLAG_SRCNODE	0x04 |
| 1355 | #define	PFSYNC_FLAG_NATSRCNODE	0x08 |
| 1356 | |
| 1357 | /* for copies to/from network byte order */ |
| 1358 | /* ioctl interface also uses network byte order */ |
| 1359 | void	 pf_state_peer_hton(const struct pf_state_peer *, |
| 1360 | 	 struct pf_state_peer_export *); |
| 1361 | void	 pf_state_peer_ntoh(const struct pf_state_peer_export *, |
| 1362 | 	 struct pf_state_peer *); |
| 1363 | |
| 1364 | #define pf_state_counter_hton(s,d) do {				\ |
| 1365 | 	d[0] = htonl((s>>32)&0xffffffff);			\ |
| 1366 | 	d[1] = htonl(s&0xffffffff);				\ |
| 1367 | } while (0) |
| 1368 | |
| 1369 | #define pf_state_counter_from_pfsync(s)				\ |
| 1370 | 	(((u_int64_t)(s[0])<<32) | (u_int64_t)(s[1])) |
| 1371 | |
| 1372 | #define pf_state_counter_ntoh(s,d) do {				\ |
| 1373 | 	d = ntohl(s[0]);					\ |
| 1374 | 	d = d<<32;						\ |
| 1375 | 	d += ntohl(s[1]);					\ |
| 1376 | } while (0) |
| 1377 | |
| 1378 | TAILQ_HEAD(pf_krulequeue, pf_krule); |
| 1379 | |
| 1380 | struct pf_kanchor; |
| 1381 | |
| 1382 | struct pf_kruleset { |
| 1383 | 	struct { |
| 1384 | 		struct pf_krulequeue	 queues[2]; |
| 1385 | 		struct { |
| 1386 | 			struct pf_krulequeue	*ptr; |
| 1387 | 			u_int32_t		 rcount; |
| 1388 | 			u_int32_t		 ticket; |
| 1389 | 			int			 open; |
| 1390 | 			struct pf_krule_global 	 *tree; |
| 1391 | 		}			 active, inactive; |
| 1392 | 	}			 rules[PF_RULESET_MAX]; |
| 1393 | 	struct pf_kanchor	*anchor; |
| 1394 | 	u_int32_t		 tticket; |
| 1395 | 	int			 tables; |
| 1396 | 	int			 topen; |
| 1397 | }; |
| 1398 | |
| 1399 | RB_HEAD(pf_kanchor_global, pf_kanchor); |
| 1400 | RB_HEAD(pf_kanchor_node, pf_kanchor); |
| 1401 | struct pf_kanchor { |
| 1402 | 	RB_ENTRY(pf_kanchor)	 entry_global; |
| 1403 | 	RB_ENTRY(pf_kanchor)	 entry_node; |
| 1404 | 	struct pf_kanchor	*parent; |
| 1405 | 	struct pf_kanchor_node	 children; |
| 1406 | 	char			 name[PF_ANCHOR_NAME_SIZE]; |
| 1407 | 	char			 path[MAXPATHLEN]; |
| 1408 | 	struct pf_kruleset	 ruleset; |
| 1409 | 	int			 refcnt;	/* anchor rules */ |
| 1410 | }; |
| 1411 | RB_PROTOTYPE(pf_kanchor_global, pf_kanchor, entry_global, pf_anchor_compare); |
| 1412 | RB_PROTOTYPE(pf_kanchor_node, pf_kanchor, entry_node, pf_kanchor_compare); |
| 1413 | |
| 1414 | #define PF_RESERVED_ANCHOR	"_pf" |
| 1415 | |
| 1416 | #define PFR_TFLAG_PERSIST	0x00000001 |
| 1417 | #define PFR_TFLAG_CONST		0x00000002 |
| 1418 | #define PFR_TFLAG_ACTIVE	0x00000004 |
| 1419 | #define PFR_TFLAG_INACTIVE	0x00000008 |
| 1420 | #define PFR_TFLAG_REFERENCED	0x00000010 |
| 1421 | #define PFR_TFLAG_REFDANCHOR	0x00000020 |
| 1422 | #define PFR_TFLAG_COUNTERS	0x00000040 |
| 1423 | /* Adjust masks below when adding flags. */ |
| 1424 | #define PFR_TFLAG_USRMASK	(PFR_TFLAG_PERSIST	| \ |
| 1425 | 				 PFR_TFLAG_CONST	| \ |
| 1426 | 				 PFR_TFLAG_COUNTERS) |
| 1427 | #define PFR_TFLAG_SETMASK	(PFR_TFLAG_ACTIVE	| \ |
| 1428 | 				 PFR_TFLAG_INACTIVE	| \ |
| 1429 | 				 PFR_TFLAG_REFERENCED	| \ |
| 1430 | 				 PFR_TFLAG_REFDANCHOR) |
| 1431 | #define PFR_TFLAG_ALLMASK	(PFR_TFLAG_PERSIST	| \ |
| 1432 | 				 PFR_TFLAG_CONST	| \ |
| 1433 | 				 PFR_TFLAG_ACTIVE	| \ |
| 1434 | 				 PFR_TFLAG_INACTIVE	| \ |
| 1435 | 				 PFR_TFLAG_REFERENCED	| \ |
| 1436 | 				 PFR_TFLAG_REFDANCHOR	| \ |
| 1437 | 				 PFR_TFLAG_COUNTERS) |
| 1438 | |
| 1439 | struct pf_keth_anchor_stackframe; |
| 1440 | |
| 1441 | struct pfr_table { |
| 1442 | 	char			 pfrt_anchor[MAXPATHLEN]; |
| 1443 | 	char			 pfrt_name[PF_TABLE_NAME_SIZE]; |
| 1444 | 	u_int32_t		 pfrt_flags; |
| 1445 | 	u_int8_t		 pfrt_fback; |
| 1446 | }; |
| 1447 | |
| 1448 | enum { PFR_FB_NONE, PFR_FB_MATCH, PFR_FB_ADDED, PFR_FB_DELETED, |
| 1449 | 	PFR_FB_CHANGED, PFR_FB_CLEARED, PFR_FB_DUPLICATE, |
| 1450 | 	PFR_FB_NOTMATCH, PFR_FB_CONFLICT, PFR_FB_NOCOUNT, PFR_FB_MAX }; |
| 1451 | |
| 1452 | struct pfr_addr { |
| 1453 | 	union { |
| 1454 | 		struct in_addr	 _pfra_ip4addr; |
| 1455 | 		struct in6_addr	 _pfra_ip6addr; |
| 1456 | 	}		 pfra_u; |
| 1457 | 	u_int8_t	 pfra_af; |
| 1458 | 	u_int8_t	 pfra_net; |
| 1459 | 	u_int8_t	 pfra_not; |
| 1460 | 	u_int8_t	 pfra_fback; |
| 1461 | }; |
| 1462 | #define	pfra_ip4addr	pfra_u._pfra_ip4addr |
| 1463 | #define	pfra_ip6addr	pfra_u._pfra_ip6addr |
| 1464 | |
| 1465 | enum { PFR_DIR_IN, PFR_DIR_OUT, PFR_DIR_MAX }; |
| 1466 | enum { PFR_OP_BLOCK, PFR_OP_PASS, PFR_OP_ADDR_MAX, PFR_OP_TABLE_MAX }; |
| 1467 | enum { PFR_TYPE_PACKETS, PFR_TYPE_BYTES, PFR_TYPE_MAX }; |
| 1468 | #define	PFR_NUM_COUNTERS	(PFR_DIR_MAX * PFR_OP_ADDR_MAX * PFR_TYPE_MAX) |
| 1469 | #define PFR_OP_XPASS	PFR_OP_ADDR_MAX |
| 1470 | |
| 1471 | struct pfr_astats { |
| 1472 | 	struct pfr_addr	 pfras_a; |
| 1473 | 	u_int64_t	 pfras_packets[PFR_DIR_MAX][PFR_OP_ADDR_MAX]; |
| 1474 | 	u_int64_t	 pfras_bytes[PFR_DIR_MAX][PFR_OP_ADDR_MAX]; |
| 1475 | 	time_t		 pfras_tzero; |
| 1476 | }; |
| 1477 | |
| 1478 | enum { PFR_REFCNT_RULE, PFR_REFCNT_ANCHOR, PFR_REFCNT_MAX }; |
| 1479 | |
| 1480 | struct pfr_tstats { |
| 1481 | 	struct pfr_table pfrts_t; |
| 1482 | 	u_int64_t	 pfrts_packets[PFR_DIR_MAX][PFR_OP_TABLE_MAX]; |
| 1483 | 	u_int64_t	 pfrts_bytes[PFR_DIR_MAX][PFR_OP_TABLE_MAX]; |
| 1484 | 	u_int64_t	 pfrts_match; |
| 1485 | 	u_int64_t	 pfrts_nomatch; |
| 1486 | 	time_t		 pfrts_tzero; |
| 1487 | 	int		 pfrts_cnt; |
| 1488 | 	int		 pfrts_refcnt[PFR_REFCNT_MAX]; |
| 1489 | }; |
| 1490 | |
| 1491 | #ifdef _KERNEL |
| 1492 | |
| 1493 | struct pfr_kstate_counter { |
| 1494 | 	counter_u64_t	pkc_pcpu; |
| 1495 | 	u_int64_t	pkc_zero; |
| 1496 | }; |
| 1497 | |
| 1498 | static inline int |
| 1499 | pfr_kstate_counter_init(struct pfr_kstate_counter *pfrc, int flags) |
| 1500 | { |
| 1501 | |
| 1502 | 	pfrc->pkc_zero = 0; |
| 1503 | 	pfrc->pkc_pcpu = counter_u64_alloc(flags); |
| 1504 | 	if (pfrc->pkc_pcpu == NULL) |
| 1505 | 		return (ENOMEM); |
| 1506 | 	return (0); |
| 1507 | } |
| 1508 | |
| 1509 | static inline void |
| 1510 | pfr_kstate_counter_deinit(struct pfr_kstate_counter *pfrc) |
| 1511 | { |
| 1512 | |
| 1513 | 	counter_u64_free(pfrc->pkc_pcpu); |
| 1514 | } |
| 1515 | |
| 1516 | static inline u_int64_t |
| 1517 | pfr_kstate_counter_fetch(struct pfr_kstate_counter *pfrc) |
| 1518 | { |
| 1519 | 	u_int64_t c; |
| 1520 | |
| 1521 | 	c = counter_u64_fetch(pfrc->pkc_pcpu); |
| 1522 | 	c -= pfrc->pkc_zero; |
| 1523 | 	return (c); |
| 1524 | } |
| 1525 | |
| 1526 | static inline void |
| 1527 | pfr_kstate_counter_zero(struct pfr_kstate_counter *pfrc) |
| 1528 | { |
| 1529 | 	u_int64_t c; |
| 1530 | |
| 1531 | 	c = counter_u64_fetch(pfrc->pkc_pcpu); |
| 1532 | 	pfrc->pkc_zero = c; |
| 1533 | } |
| 1534 | |
| 1535 | static inline void |
| 1536 | pfr_kstate_counter_add(struct pfr_kstate_counter *pfrc, int64_t n) |
| 1537 | { |
| 1538 | |
| 1539 | 	counter_u64_add(pfrc->pkc_pcpu, n); |
| 1540 | } |
| 1541 | |
| 1542 | struct pfr_ktstats { |
| 1543 | 	struct pfr_table pfrts_t; |
| 1544 | 	struct pfr_kstate_counter	 pfrkts_packets[PFR_DIR_MAX][PFR_OP_TABLE_MAX]; |
| 1545 | 	struct pfr_kstate_counter	 pfrkts_bytes[PFR_DIR_MAX][PFR_OP_TABLE_MAX]; |
| 1546 | 	struct pfr_kstate_counter	 pfrkts_match; |
| 1547 | 	struct pfr_kstate_counter	 pfrkts_nomatch; |
| 1548 | 	time_t		 pfrkts_tzero; |
| 1549 | 	int		 pfrkts_cnt; |
| 1550 | 	int		 pfrkts_refcnt[PFR_REFCNT_MAX]; |
| 1551 | }; |
| 1552 | |
| 1553 | #endif /* _KERNEL */ |
| 1554 | |
| 1555 | #define	pfrts_name	pfrts_t.pfrt_name |
| 1556 | #define pfrts_flags	pfrts_t.pfrt_flags |
| 1557 | |
| 1558 | #ifndef _SOCKADDR_UNION_DEFINED |
| 1559 | #define	_SOCKADDR_UNION_DEFINED |
| 1560 | union sockaddr_union { |
| 1561 | 	struct sockaddr		sa; |
| 1562 | 	struct sockaddr_in	sin; |
| 1563 | 	struct sockaddr_in6	sin6; |
| 1564 | }; |
| 1565 | #endif /* _SOCKADDR_UNION_DEFINED */ |
| 1566 | |
| 1567 | struct pfr_kcounters { |
| 1568 | 	counter_u64_t		 pfrkc_counters; |
| 1569 | 	time_t			 pfrkc_tzero; |
| 1570 | }; |
| 1571 | #define	pfr_kentry_counter(kc, dir, op, t)		\ |
| 1572 | 	((kc)->pfrkc_counters +				\ |
| 1573 | 	 (dir) * PFR_OP_ADDR_MAX * PFR_TYPE_MAX + (op) * PFR_TYPE_MAX + (t)) |
| 1574 | |
| 1575 | #ifdef _KERNEL |
| 1576 | SLIST_HEAD(pfr_kentryworkq, pfr_kentry); |
| 1577 | struct pfr_kentry { |
| 1578 | 	struct radix_node	 pfrke_node[2]; |
| 1579 | 	union sockaddr_union	 pfrke_sa; |
| 1580 | 	SLIST_ENTRY(pfr_kentry)	 pfrke_workq; |
| 1581 | 	struct pfr_kcounters	 pfrke_counters; |
| 1582 | 	u_int8_t		 pfrke_af; |
| 1583 | 	u_int8_t		 pfrke_net; |
| 1584 | 	u_int8_t		 pfrke_not; |
| 1585 | 	u_int8_t		 pfrke_mark; |
| 1586 | }; |
| 1587 | |
| 1588 | SLIST_HEAD(pfr_ktableworkq, pfr_ktable); |
| 1589 | RB_HEAD(pfr_ktablehead, pfr_ktable); |
| 1590 | struct pfr_ktable { |
| 1591 | 	struct pfr_ktstats	 pfrkt_kts; |
| 1592 | 	RB_ENTRY(pfr_ktable)	 pfrkt_tree; |
| 1593 | 	SLIST_ENTRY(pfr_ktable)	 pfrkt_workq; |
| 1594 | 	struct radix_node_head	*pfrkt_ip4; |
| 1595 | 	struct radix_node_head	*pfrkt_ip6; |
| 1596 | 	struct pfr_ktable	*pfrkt_shadow; |
| 1597 | 	struct pfr_ktable	*pfrkt_root; |
| 1598 | 	struct pf_kruleset	*pfrkt_rs; |
| 1599 | 	long			 pfrkt_larg; |
| 1600 | 	int			 pfrkt_nflags; |
| 1601 | }; |
| 1602 | #define pfrkt_t		pfrkt_kts.pfrts_t |
| 1603 | #define pfrkt_name	pfrkt_t.pfrt_name |
| 1604 | #define pfrkt_anchor	pfrkt_t.pfrt_anchor |
| 1605 | #define pfrkt_ruleset	pfrkt_t.pfrt_ruleset |
| 1606 | #define pfrkt_flags	pfrkt_t.pfrt_flags |
| 1607 | #define pfrkt_cnt	pfrkt_kts.pfrkts_cnt |
| 1608 | #define pfrkt_refcnt	pfrkt_kts.pfrkts_refcnt |
| 1609 | #define pfrkt_packets	pfrkt_kts.pfrkts_packets |
| 1610 | #define pfrkt_bytes	pfrkt_kts.pfrkts_bytes |
| 1611 | #define pfrkt_match	pfrkt_kts.pfrkts_match |
| 1612 | #define pfrkt_nomatch	pfrkt_kts.pfrkts_nomatch |
| 1613 | #define pfrkt_tzero	pfrkt_kts.pfrkts_tzero |
| 1614 | #endif |
| 1615 | |
| 1616 | #ifdef _KERNEL |
| 1617 | struct pfi_kkif { |
| 1618 | 	char				 pfik_name[IFNAMSIZ]; |
| 1619 | 	union { |
| 1620 | 		RB_ENTRY(pfi_kkif)	 _pfik_tree; |
| 1621 | 		LIST_ENTRY(pfi_kkif)	 _pfik_list; |
| 1622 | 	} _pfik_glue; |
| 1623 | #define	pfik_tree	_pfik_glue._pfik_tree |
| 1624 | #define	pfik_list	_pfik_glue._pfik_list |
| 1625 | 	struct pf_counter_u64		 pfik_packets[2][2][2]; |
| 1626 | 	struct pf_counter_u64		 pfik_bytes[2][2][2]; |
| 1627 | 	time_t				 pfik_tzero; |
| 1628 | 	u_int				 pfik_flags; |
| 1629 | 	struct ifnet			*pfik_ifp; |
| 1630 | 	struct ifg_group		*pfik_group; |
| 1631 | 	u_int				 pfik_rulerefs; |
| 1632 | 	TAILQ_HEAD(, pfi_dynaddr)	 pfik_dynaddrs; |
| 1633 | #ifdef PF_WANT_32_TO_64_COUNTER |
| 1634 | 	LIST_ENTRY(pfi_kkif)		 pfik_allkiflist; |
| 1635 | #endif |
| 1636 | }; |
| 1637 | #endif |
| 1638 | |
| 1639 | #define	PFI_IFLAG_REFS		0x0001	/* has state references */ |
| 1640 | #define PFI_IFLAG_SKIP		0x0100	/* skip filtering on interface */ |
| 1641 | #define	PFI_IFLAG_ANY 		0x0200	/* match any non-loopback interface */ |
| 1642 | |
| 1643 | #ifdef _KERNEL |
| 1644 | struct pf_sctp_multihome_job; |
| 1645 | TAILQ_HEAD(pf_sctp_multihome_jobs, pf_sctp_multihome_job); |
| 1646 | |
| 1647 | struct pf_pdesc { |
| 1648 | 	struct { |
| 1649 | 		int	 done; |
| 1650 | 		uid_t	 uid; |
| 1651 | 		gid_t	 gid; |
| 1652 | 	}		 lookup; |
| 1653 | 	u_int64_t	 tot_len;	/* Make Mickey money */ |
| 1654 | 	union pf_headers { |
| 1655 | 		struct tcphdr		tcp; |
| 1656 | 		struct udphdr		udp; |
| 1657 | 		struct sctphdr		sctp; |
| 1658 | 		struct icmp		icmp; |
| 1659 | #ifdef INET6 |
| 1660 | 		struct icmp6_hdr	icmp6; |
| 1661 | #endif /* INET6 */ |
| 1662 | 		char any[0]; |
| 1663 | 	} hdr; |
| 1664 | |
| 1665 | 	struct pf_addr	 nsaddr;	/* src address after NAT */ |
| 1666 | 	struct pf_addr	 ndaddr;	/* dst address after NAT */ |
| 1667 | |
| 1668 | 	struct pfi_kkif	*kif;		/* incomming interface */ |
| 1669 | 	struct mbuf	*m; |
| 1670 | |
| 1671 | 	struct pf_addr	*src;		/* src address */ |
| 1672 | 	struct pf_addr	*dst;		/* dst address */ |
| 1673 | 	struct pf_addr	 osrc; |
| 1674 | 	struct pf_addr	 odst; |
| 1675 | 	u_int16_t	*pcksum;	/* proto cksum */ |
| 1676 | 	u_int16_t	*sport; |
| 1677 | 	u_int16_t	*dport; |
| 1678 | 	u_int16_t	 osport; |
| 1679 | 	u_int16_t	 odport; |
| 1680 | 	u_int16_t	 nsport;	/* src port after NAT */ |
| 1681 | 	u_int16_t	 ndport;	/* dst port after NAT */ |
| 1682 | 	struct pf_mtag	*pf_mtag; |
| 1683 | 	struct pf_rule_actions	act; |
| 1684 | |
| 1685 | 	u_int32_t	 off;		/* protocol header offset */ |
| 1686 | 	bool		 df;		/* IPv4 Don't fragment flag. */ |
| 1687 | 	u_int32_t	 hdrlen;	/* protocol header length */ |
| 1688 | 	u_int32_t	 p_len;		/* total length of protocol payload */ |
| 1689 | 	u_int32_t	 extoff;	/* extentsion header offset */ |
| 1690 | 	u_int32_t	 fragoff;	/* fragment header offset */ |
| 1691 | 	u_int32_t	 jumbolen;	/* length from v6 jumbo header */ |
| 1692 | 	u_int32_t	 badopts;	/* v4 options or v6 routing headers */ |
| 1693 | #define	PF_OPT_OTHER		0x0001 |
| 1694 | #define	PF_OPT_JUMBO		0x0002 |
| 1695 | #define	PF_OPT_ROUTER_ALERT	0x0004 |
| 1696 | |
| 1697 | 	u_int16_t	*ip_sum; |
| 1698 | 	u_int16_t	 flags;		/* Let SCRUB trigger behavior in |
| 1699 | 					 * state code. Easier than tags */ |
| 1700 | #define PFDESC_TCP_NORM	0x0001		/* TCP shall be statefully scrubbed */ |
| 1701 | 	u_int16_t	 virtual_proto; |
| 1702 | #define PF_VPROTO_FRAGMENT	256 |
| 1703 | 	sa_family_t	 af; |
| 1704 | 	sa_family_t	 naf; |
| 1705 | 	u_int8_t	 proto; |
| 1706 | 	u_int8_t	 tos; |
| 1707 | 	u_int8_t	 ttl; |
| 1708 | 	u_int8_t	 dir;		/* direction */ |
| 1709 | 	u_int8_t	 sidx;		/* key index for source */ |
| 1710 | 	u_int8_t	 didx;		/* key index for destination */ |
| 1711 | #define PFDESC_SCTP_INIT	0x0001 |
| 1712 | #define PFDESC_SCTP_INIT_ACK	0x0002 |
| 1713 | #define PFDESC_SCTP_COOKIE	0x0004 |
| 1714 | #define PFDESC_SCTP_COOKIE_ACK	0x0008 |
| 1715 | #define PFDESC_SCTP_ABORT	0x0010 |
| 1716 | #define PFDESC_SCTP_SHUTDOWN	0x0020 |
| 1717 | #define PFDESC_SCTP_SHUTDOWN_COMPLETE	0x0040 |
| 1718 | #define PFDESC_SCTP_DATA	0x0080 |
| 1719 | #define PFDESC_SCTP_ASCONF	0x0100 |
| 1720 | #define PFDESC_SCTP_HEARTBEAT	0x0200 |
| 1721 | #define PFDESC_SCTP_HEARTBEAT_ACK	0x0400 |
| 1722 | #define PFDESC_SCTP_OTHER	0x0800 |
| 1723 | #define PFDESC_SCTP_ADD_IP	0x1000 |
| 1724 | 	u_int16_t	 sctp_flags; |
| 1725 | 	u_int32_t	 sctp_initiate_tag; |
| 1726 | 	u_int16_t	 sctp_dummy_sum; |
| 1727 | 	struct pf_krule	*related_rule; |
| 1728 | |
| 1729 | 	struct pf_sctp_multihome_jobs	sctp_multihome_jobs; |
| 1730 | }; |
| 1731 | |
| 1732 | struct pf_sctp_multihome_job { |
| 1733 | 	TAILQ_ENTRY(pf_sctp_multihome_job)	next; |
| 1734 | 	struct pf_pdesc				 pd; |
| 1735 | 	struct pf_addr				 src; |
| 1736 | 	struct pf_addr				 dst; |
| 1737 | 	int					 op; |
| 1738 | }; |
| 1739 | |
| 1740 | #endif |
| 1741 | |
| 1742 | /* flags for RDR options */ |
| 1743 | #define PF_DPORT_RANGE	0x01		/* Dest port uses range */ |
| 1744 | #define PF_RPORT_RANGE	0x02		/* RDR'ed port uses range */ |
| 1745 | |
| 1746 | /* UDP state enumeration */ |
| 1747 | #define PFUDPS_NO_TRAFFIC	0 |
| 1748 | #define PFUDPS_SINGLE		1 |
| 1749 | #define PFUDPS_MULTIPLE		2 |
| 1750 | |
| 1751 | #define PFUDPS_NSTATES		3	/* number of state levels */ |
| 1752 | |
| 1753 | #define PFUDPS_NAMES { \ |
| 1754 | 	"NO_TRAFFIC", \ |
| 1755 | 	"SINGLE", \ |
| 1756 | 	"MULTIPLE", \ |
| 1757 | 	NULL \ |
| 1758 | } |
| 1759 | |
| 1760 | /* Other protocol state enumeration */ |
| 1761 | #define PFOTHERS_NO_TRAFFIC	0 |
| 1762 | #define PFOTHERS_SINGLE		1 |
| 1763 | #define PFOTHERS_MULTIPLE	2 |
| 1764 | |
| 1765 | #define PFOTHERS_NSTATES	3	/* number of state levels */ |
| 1766 | |
| 1767 | #define PFOTHERS_NAMES { \ |
| 1768 | 	"NO_TRAFFIC", \ |
| 1769 | 	"SINGLE", \ |
| 1770 | 	"MULTIPLE", \ |
| 1771 | 	NULL \ |
| 1772 | } |
| 1773 | |
| 1774 | #define ACTION_SET(a, x) \ |
| 1775 | 	do { \ |
| 1776 | 		if ((a) != NULL) \ |
| 1777 | 			*(a) = (x); \ |
| 1778 | 	} while (0) |
| 1779 | |
| 1780 | #define REASON_SET(a, x) \ |
| 1781 | 	do { \ |
| 1782 | 		SDT_PROBE2(pf, , test, reason_set, x, __LINE__); \ |
| 1783 | 		if ((a) != NULL) \ |
| 1784 | 			*(a) = (x); \ |
| 1785 | 		if (x < PFRES_MAX) \ |
| 1786 | 			counter_u64_add(V_pf_status.counters[x], 1); \ |
| 1787 | 	} while (0) |
| 1788 | |
| 1789 | enum pf_syncookies_mode { |
| 1790 | 	PF_SYNCOOKIES_NEVER = 0, |
| 1791 | 	PF_SYNCOOKIES_ALWAYS = 1, |
| 1792 | 	PF_SYNCOOKIES_ADAPTIVE = 2, |
| 1793 | 	PF_SYNCOOKIES_MODE_MAX = PF_SYNCOOKIES_ADAPTIVE |
| 1794 | }; |
| 1795 | |
| 1796 | #define	PF_SYNCOOKIES_HIWATPCT	25 |
| 1797 | #define	PF_SYNCOOKIES_LOWATPCT	(PF_SYNCOOKIES_HIWATPCT / 2) |
| 1798 | |
| 1799 | #ifdef _KERNEL |
| 1800 | struct pf_kstatus { |
| 1801 | 	counter_u64_t	counters[PFRES_MAX]; /* reason for passing/dropping */ |
| 1802 | 	counter_u64_t	lcounters[KLCNT_MAX]; /* limit counters */ |
| 1803 | 	struct pf_counter_u64	fcounters[FCNT_MAX]; /* state operation counters */ |
| 1804 | 	counter_u64_t	scounters[SCNT_MAX]; /* src_node operation counters */ |
| 1805 | 	uint32_t	states; |
| 1806 | 	uint32_t	src_nodes; |
| 1807 | 	uint32_t	running; |
| 1808 | 	uint32_t	since; |
| 1809 | 	uint32_t	debug; |
| 1810 | 	uint32_t	hostid; |
| 1811 | 	char		ifname[IFNAMSIZ]; |
| 1812 | 	uint8_t		pf_chksum[PF_MD5_DIGEST_LENGTH]; |
| 1813 | 	bool		keep_counters; |
| 1814 | 	enum pf_syncookies_mode	syncookies_mode; |
| 1815 | 	bool		syncookies_active; |
| 1816 | 	uint64_t	syncookies_inflight[2]; |
| 1817 | 	uint32_t	states_halfopen; |
| 1818 | 	uint32_t	reass; |
| 1819 | }; |
| 1820 | #endif |
| 1821 | |
| 1822 | struct pf_divert { |
| 1823 | 	union { |
| 1824 | 		struct in_addr	ipv4; |
| 1825 | 		struct in6_addr	ipv6; |
| 1826 | 	}		addr; |
| 1827 | 	u_int16_t	port; |
| 1828 | }; |
| 1829 | |
| 1830 | #define PFFRAG_FRENT_HIWAT	5000	/* Number of fragment entries */ |
| 1831 | #define PFR_KENTRY_HIWAT	200000	/* Number of table entries */ |
| 1832 | |
| 1833 | struct pf_fragment_tag { |
| 1834 | 	uint16_t	ft_hdrlen;	/* header length of reassembled pkt */ |
| 1835 | 	uint16_t	ft_extoff;	/* last extension header offset or 0 */ |
| 1836 | 	uint16_t	ft_maxlen;	/* maximum fragment payload length */ |
| 1837 | 	uint32_t	ft_id;		/* fragment id */ |
| 1838 | }; |
| 1839 | |
| 1840 | /* |
| 1841 | * Limit the length of the fragment queue traversal. Remember |
| 1842 | * search entry points based on the fragment offset. |
| 1843 | */ |
| 1844 | #define PF_FRAG_ENTRY_POINTS		16 |
| 1845 | |
| 1846 | /* |
| 1847 | * The number of entries in the fragment queue must be limited |
| 1848 | * to avoid DoS by linear searching. Instead of a global limit, |
| 1849 | * use a limit per entry point. For large packets these sum up. |
| 1850 | */ |
| 1851 | #define PF_FRAG_ENTRY_LIMIT		64 |
| 1852 | |
| 1853 | /* |
| 1854 | * ioctl parameter structures |
| 1855 | */ |
| 1856 | |
| 1857 | struct pfioc_pooladdr { |
| 1858 | 	u_int32_t		 action; |
| 1859 | 	u_int32_t		 ticket; |
| 1860 | 	u_int32_t		 nr; |
| 1861 | 	u_int32_t		 r_num; |
| 1862 | 	u_int8_t		 r_action; |
| 1863 | 	u_int8_t		 r_last; |
| 1864 | 	u_int8_t		 af; |
| 1865 | 	char			 anchor[MAXPATHLEN]; |
| 1866 | 	struct pf_pooladdr	 addr; |
| 1867 | }; |
| 1868 | |
| 1869 | struct pfioc_rule { |
| 1870 | 	u_int32_t	 action; |
| 1871 | 	u_int32_t	 ticket; |
| 1872 | 	u_int32_t	 pool_ticket; |
| 1873 | 	u_int32_t	 nr; |
| 1874 | 	char		 anchor[MAXPATHLEN]; |
| 1875 | 	char		 anchor_call[MAXPATHLEN]; |
| 1876 | 	struct pf_rule	 rule; |
| 1877 | }; |
| 1878 | |
| 1879 | struct pfioc_natlook { |
| 1880 | 	struct pf_addr	 saddr; |
| 1881 | 	struct pf_addr	 daddr; |
| 1882 | 	struct pf_addr	 rsaddr; |
| 1883 | 	struct pf_addr	 rdaddr; |
| 1884 | 	u_int16_t	 sport; |
| 1885 | 	u_int16_t	 dport; |
| 1886 | 	u_int16_t	 rsport; |
| 1887 | 	u_int16_t	 rdport; |
| 1888 | 	sa_family_t	 af; |
| 1889 | 	u_int8_t	 proto; |
| 1890 | 	u_int8_t	 direction; |
| 1891 | }; |
| 1892 | |
| 1893 | struct pfioc_state { |
| 1894 | 	struct pfsync_state_1301	state; |
| 1895 | }; |
| 1896 | |
| 1897 | struct pfioc_src_node_kill { |
| 1898 | 	sa_family_t psnk_af; |
| 1899 | 	struct pf_rule_addr psnk_src; |
| 1900 | 	struct pf_rule_addr psnk_dst; |
| 1901 | 	u_int		 psnk_killed; |
| 1902 | }; |
| 1903 | |
| 1904 | #ifdef _KERNEL |
| 1905 | struct pf_kstate_kill { |
| 1906 | 	struct pf_state_cmp	psk_pfcmp; |
| 1907 | 	sa_family_t		psk_af; |
| 1908 | 	int			psk_proto; |
| 1909 | 	struct pf_rule_addr	psk_src; |
| 1910 | 	struct pf_rule_addr	psk_dst; |
| 1911 | 	struct pf_rule_addr	psk_rt_addr; |
| 1912 | 	char			psk_ifname[IFNAMSIZ]; |
| 1913 | 	char			psk_label[PF_RULE_LABEL_SIZE]; |
| 1914 | 	u_int			psk_killed; |
| 1915 | 	bool			psk_kill_match; |
| 1916 | 	bool			psk_nat; |
| 1917 | }; |
| 1918 | #endif |
| 1919 | |
| 1920 | struct pfioc_state_kill { |
| 1921 | 	struct pf_state_cmp	psk_pfcmp; |
| 1922 | 	sa_family_t		psk_af; |
| 1923 | 	int			psk_proto; |
| 1924 | 	struct pf_rule_addr	psk_src; |
| 1925 | 	struct pf_rule_addr	psk_dst; |
| 1926 | 	char			psk_ifname[IFNAMSIZ]; |
| 1927 | 	char			psk_label[PF_RULE_LABEL_SIZE]; |
| 1928 | 	u_int			psk_killed; |
| 1929 | }; |
| 1930 | |
| 1931 | struct pfioc_states { |
| 1932 | 	int	ps_len; |
| 1933 | 	union { |
| 1934 | 		void				*ps_buf; |
| 1935 | 		struct pfsync_state_1301	*ps_states; |
| 1936 | 	}; |
| 1937 | }; |
| 1938 | |
| 1939 | struct pfioc_states_v2 { |
| 1940 | 	int		ps_len; |
| 1941 | 	uint64_t	ps_req_version; |
| 1942 | 	union { |
| 1943 | 		void			*ps_buf; |
| 1944 | 		struct pf_state_export	*ps_states; |
| 1945 | 	}; |
| 1946 | }; |
| 1947 | |
| 1948 | struct pfioc_src_nodes { |
| 1949 | 	int	psn_len; |
| 1950 | 	union { |
| 1951 | 		void		*psn_buf; |
| 1952 | 		struct pf_src_node	*psn_src_nodes; |
| 1953 | 	}; |
| 1954 | }; |
| 1955 | |
| 1956 | struct pfioc_if { |
| 1957 | 	char		 ifname[IFNAMSIZ]; |
| 1958 | }; |
| 1959 | |
| 1960 | struct pfioc_tm { |
| 1961 | 	int		 timeout; |
| 1962 | 	int		 seconds; |
| 1963 | }; |
| 1964 | |
| 1965 | struct pfioc_limit { |
| 1966 | 	int		 index; |
| 1967 | 	unsigned	 limit; |
| 1968 | }; |
| 1969 | |
| 1970 | struct pfioc_altq_v0 { |
| 1971 | 	u_int32_t	 action; |
| 1972 | 	u_int32_t	 ticket; |
| 1973 | 	u_int32_t	 nr; |
| 1974 | 	struct pf_altq_v0 altq; |
| 1975 | }; |
| 1976 | |
| 1977 | struct pfioc_altq_v1 { |
| 1978 | 	u_int32_t	 action; |
| 1979 | 	u_int32_t	 ticket; |
| 1980 | 	u_int32_t	 nr; |
| 1981 | 	/* |
| 1982 | 	 * Placed here so code that only uses the above parameters can be |
| 1983 | 	 * written entirely in terms of the v0 or v1 type. |
| 1984 | 	 */ |
| 1985 | 	u_int32_t	 version; |
| 1986 | 	struct pf_altq_v1 altq; |
| 1987 | }; |
| 1988 | |
| 1989 | /* |
| 1990 | * Latest version of struct pfioc_altq_vX. This must move in lock-step with |
| 1991 | * the latest version of struct pf_altq_vX as it has that struct as a |
| 1992 | * member. |
| 1993 | */ |
| 1994 | #define PFIOC_ALTQ_VERSION	PF_ALTQ_VERSION |
| 1995 | |
| 1996 | struct pfioc_qstats_v0 { |
| 1997 | 	u_int32_t	 ticket; |
| 1998 | 	u_int32_t	 nr; |
| 1999 | 	void		*buf; |
| 2000 | 	int		 nbytes; |
| 2001 | 	u_int8_t	 scheduler; |
| 2002 | }; |
| 2003 | |
| 2004 | struct pfioc_qstats_v1 { |
| 2005 | 	u_int32_t	 ticket; |
| 2006 | 	u_int32_t	 nr; |
| 2007 | 	void		*buf; |
| 2008 | 	int		 nbytes; |
| 2009 | 	u_int8_t	 scheduler; |
| 2010 | 	/* |
| 2011 | 	 * Placed here so code that only uses the above parameters can be |
| 2012 | 	 * written entirely in terms of the v0 or v1 type. |
| 2013 | 	 */ |
| 2014 | 	u_int32_t	 version; /* Requested version of stats struct */ |
| 2015 | }; |
| 2016 | |
| 2017 | /* Latest version of struct pfioc_qstats_vX */ |
| 2018 | #define PFIOC_QSTATS_VERSION	1 |
| 2019 | |
| 2020 | struct pfioc_ruleset { |
| 2021 | 	u_int32_t	 nr; |
| 2022 | 	char		 path[MAXPATHLEN]; |
| 2023 | 	char		 name[PF_ANCHOR_NAME_SIZE]; |
| 2024 | }; |
| 2025 | |
| 2026 | #define PF_RULESET_ALTQ		(PF_RULESET_MAX) |
| 2027 | #define PF_RULESET_TABLE	(PF_RULESET_MAX+1) |
| 2028 | #define PF_RULESET_ETH		(PF_RULESET_MAX+2) |
| 2029 | struct pfioc_trans { |
| 2030 | 	int		 size;	/* number of elements */ |
| 2031 | 	int		 esize; /* size of each element in bytes */ |
| 2032 | 	struct pfioc_trans_e { |
| 2033 | 		int		rs_num; |
| 2034 | 		char		anchor[MAXPATHLEN]; |
| 2035 | 		u_int32_t	ticket; |
| 2036 | 	}		*array; |
| 2037 | }; |
| 2038 | |
| 2039 | #define PFR_FLAG_ATOMIC		0x00000001	/* unused */ |
| 2040 | #define PFR_FLAG_DUMMY		0x00000002 |
| 2041 | #define PFR_FLAG_FEEDBACK	0x00000004 |
| 2042 | #define PFR_FLAG_CLSTATS	0x00000008 |
| 2043 | #define PFR_FLAG_ADDRSTOO	0x00000010 |
| 2044 | #define PFR_FLAG_REPLACE	0x00000020 |
| 2045 | #define PFR_FLAG_ALLRSETS	0x00000040 |
| 2046 | #define PFR_FLAG_ALLMASK	0x0000007F |
| 2047 | #ifdef _KERNEL |
| 2048 | #define PFR_FLAG_USERIOCTL	0x10000000 |
| 2049 | #endif |
| 2050 | |
| 2051 | struct pfioc_table { |
| 2052 | 	struct pfr_table	 pfrio_table; |
| 2053 | 	void			*pfrio_buffer; |
| 2054 | 	int			 pfrio_esize; |
| 2055 | 	int			 pfrio_size; |
| 2056 | 	int			 pfrio_size2; |
| 2057 | 	int			 pfrio_nadd; |
| 2058 | 	int			 pfrio_ndel; |
| 2059 | 	int			 pfrio_nchange; |
| 2060 | 	int			 pfrio_flags; |
| 2061 | 	u_int32_t		 pfrio_ticket; |
| 2062 | }; |
| 2063 | #define	pfrio_exists	pfrio_nadd |
| 2064 | #define	pfrio_nzero	pfrio_nadd |
| 2065 | #define	pfrio_nmatch	pfrio_nadd |
| 2066 | #define pfrio_naddr	pfrio_size2 |
| 2067 | #define pfrio_setflag	pfrio_size2 |
| 2068 | #define pfrio_clrflag	pfrio_nadd |
| 2069 | |
| 2070 | struct pfioc_iface { |
| 2071 | 	char	 pfiio_name[IFNAMSIZ]; |
| 2072 | 	void	*pfiio_buffer; |
| 2073 | 	int	 pfiio_esize; |
| 2074 | 	int	 pfiio_size; |
| 2075 | 	int	 pfiio_nzero; |
| 2076 | 	int	 pfiio_flags; |
| 2077 | }; |
| 2078 | |
| 2079 | /* |
| 2080 | * ioctl operations |
| 2081 | */ |
| 2082 | |
| 2083 | #define DIOCSTART	_IO ('D', 1) |
| 2084 | #define DIOCSTOP	_IO ('D', 2) |
| 2085 | #define DIOCADDRULE	_IOWR('D', 4, struct pfioc_rule) |
| 2086 | #define DIOCADDRULENV	_IOWR('D', 4, struct pfioc_nv) |
| 2087 | #define DIOCGETRULES	_IOWR('D', 6, struct pfioc_rule) |
| 2088 | #define DIOCGETRULENV	_IOWR('D', 7, struct pfioc_nv) |
| 2089 | #define DIOCCLRSTATESNV	_IOWR('D', 18, struct pfioc_nv) |
| 2090 | #define DIOCGETSTATE	_IOWR('D', 19, struct pfioc_state) |
| 2091 | #define DIOCGETSTATENV	_IOWR('D', 19, struct pfioc_nv) |
| 2092 | #define DIOCSETSTATUSIF _IOWR('D', 20, struct pfioc_if) |
| 2093 | #define DIOCGETSTATUSNV	_IOWR('D', 21, struct pfioc_nv) |
| 2094 | #define DIOCCLRSTATUS	_IO ('D', 22) |
| 2095 | #define DIOCNATLOOK	_IOWR('D', 23, struct pfioc_natlook) |
| 2096 | #define DIOCSETDEBUG	_IOWR('D', 24, u_int32_t) |
| 2097 | #ifdef COMPAT_FREEBSD14 |
| 2098 | #define DIOCGETSTATES	_IOWR('D', 25, struct pfioc_states) |
| 2099 | #endif |
| 2100 | #define DIOCCHANGERULE	_IOWR('D', 26, struct pfioc_rule) |
| 2101 | #define DIOCSETTIMEOUT	_IOWR('D', 29, struct pfioc_tm) |
| 2102 | #define DIOCGETTIMEOUT	_IOWR('D', 30, struct pfioc_tm) |
| 2103 | #define DIOCADDSTATE	_IOWR('D', 37, struct pfioc_state) |
| 2104 | #define DIOCCLRRULECTRS	_IO ('D', 38) |
| 2105 | #define DIOCGETLIMIT	_IOWR('D', 39, struct pfioc_limit) |
| 2106 | #define DIOCSETLIMIT	_IOWR('D', 40, struct pfioc_limit) |
| 2107 | #define DIOCKILLSTATESNV	_IOWR('D', 41, struct pfioc_nv) |
| 2108 | #define DIOCSTARTALTQ	_IO ('D', 42) |
| 2109 | #define DIOCSTOPALTQ	_IO ('D', 43) |
| 2110 | #define DIOCADDALTQV0	_IOWR('D', 45, struct pfioc_altq_v0) |
| 2111 | #define DIOCADDALTQV1	_IOWR('D', 45, struct pfioc_altq_v1) |
| 2112 | #define DIOCGETALTQSV0	_IOWR('D', 47, struct pfioc_altq_v0) |
| 2113 | #define DIOCGETALTQSV1	_IOWR('D', 47, struct pfioc_altq_v1) |
| 2114 | #define DIOCGETALTQV0	_IOWR('D', 48, struct pfioc_altq_v0) |
| 2115 | #define DIOCGETALTQV1	_IOWR('D', 48, struct pfioc_altq_v1) |
| 2116 | #define DIOCCHANGEALTQV0 _IOWR('D', 49, struct pfioc_altq_v0) |
| 2117 | #define DIOCCHANGEALTQV1 _IOWR('D', 49, struct pfioc_altq_v1) |
| 2118 | #define DIOCGETQSTATSV0	_IOWR('D', 50, struct pfioc_qstats_v0) |
| 2119 | #define DIOCGETQSTATSV1	_IOWR('D', 50, struct pfioc_qstats_v1) |
| 2120 | #define DIOCBEGINADDRS	_IOWR('D', 51, struct pfioc_pooladdr) |
| 2121 | #define DIOCADDADDR	_IOWR('D', 52, struct pfioc_pooladdr) |
| 2122 | #define DIOCGETADDRS	_IOWR('D', 53, struct pfioc_pooladdr) |
| 2123 | #define DIOCGETADDR	_IOWR('D', 54, struct pfioc_pooladdr) |
| 2124 | #define DIOCCHANGEADDR	_IOWR('D', 55, struct pfioc_pooladdr) |
| 2125 | #define	DIOCGETRULESETS	_IOWR('D', 58, struct pfioc_ruleset) |
| 2126 | #define	DIOCGETRULESET	_IOWR('D', 59, struct pfioc_ruleset) |
| 2127 | #define	DIOCRCLRTABLES	_IOWR('D', 60, struct pfioc_table) |
| 2128 | #define	DIOCRADDTABLES	_IOWR('D', 61, struct pfioc_table) |
| 2129 | #define	DIOCRDELTABLES	_IOWR('D', 62, struct pfioc_table) |
| 2130 | #define	DIOCRGETTABLES	_IOWR('D', 63, struct pfioc_table) |
| 2131 | #define	DIOCRGETTSTATS	_IOWR('D', 64, struct pfioc_table) |
| 2132 | #define DIOCRCLRTSTATS	_IOWR('D', 65, struct pfioc_table) |
| 2133 | #define	DIOCRCLRADDRS	_IOWR('D', 66, struct pfioc_table) |
| 2134 | #define	DIOCRADDADDRS	_IOWR('D', 67, struct pfioc_table) |
| 2135 | #define	DIOCRDELADDRS	_IOWR('D', 68, struct pfioc_table) |
| 2136 | #define	DIOCRSETADDRS	_IOWR('D', 69, struct pfioc_table) |
| 2137 | #define	DIOCRGETADDRS	_IOWR('D', 70, struct pfioc_table) |
| 2138 | #define	DIOCRGETASTATS	_IOWR('D', 71, struct pfioc_table) |
| 2139 | #define	DIOCRCLRASTATS	_IOWR('D', 72, struct pfioc_table) |
| 2140 | #define	DIOCRTSTADDRS	_IOWR('D', 73, struct pfioc_table) |
| 2141 | #define	DIOCRSETTFLAGS	_IOWR('D', 74, struct pfioc_table) |
| 2142 | #define	DIOCRINADEFINE	_IOWR('D', 77, struct pfioc_table) |
| 2143 | #define	DIOCOSFPFLUSH	_IO('D', 78) |
| 2144 | #define	DIOCOSFPADD	_IOWR('D', 79, struct pf_osfp_ioctl) |
| 2145 | #define	DIOCOSFPGET	_IOWR('D', 80, struct pf_osfp_ioctl) |
| 2146 | #define	DIOCXBEGIN	_IOWR('D', 81, struct pfioc_trans) |
| 2147 | #define	DIOCXCOMMIT	_IOWR('D', 82, struct pfioc_trans) |
| 2148 | #define	DIOCXROLLBACK	_IOWR('D', 83, struct pfioc_trans) |
| 2149 | #define	DIOCGETSRCNODES	_IOWR('D', 84, struct pfioc_src_nodes) |
| 2150 | #define	DIOCCLRSRCNODES	_IO('D', 85) |
| 2151 | #define	DIOCSETHOSTID	_IOWR('D', 86, u_int32_t) |
| 2152 | #define	DIOCIGETIFACES	_IOWR('D', 87, struct pfioc_iface) |
| 2153 | #define	DIOCSETIFFLAG	_IOWR('D', 89, struct pfioc_iface) |
| 2154 | #define	DIOCCLRIFFLAG	_IOWR('D', 90, struct pfioc_iface) |
| 2155 | #define	DIOCKILLSRCNODES	_IOWR('D', 91, struct pfioc_src_node_kill) |
| 2156 | #define	DIOCGIFSPEEDV0	_IOWR('D', 92, struct pf_ifspeed_v0) |
| 2157 | #define	DIOCGIFSPEEDV1	_IOWR('D', 92, struct pf_ifspeed_v1) |
| 2158 | #ifdef COMPAT_FREEBSD14 |
| 2159 | #define DIOCGETSTATESV2	_IOWR('D', 93, struct pfioc_states_v2) |
| 2160 | #endif |
| 2161 | #define	DIOCGETSYNCOOKIES	_IOWR('D', 94, struct pfioc_nv) |
| 2162 | #define	DIOCSETSYNCOOKIES	_IOWR('D', 95, struct pfioc_nv) |
| 2163 | #define	DIOCKEEPCOUNTERS	_IOWR('D', 96, struct pfioc_nv) |
| 2164 | #define	DIOCKEEPCOUNTERS_FREEBSD13	_IOWR('D', 92, struct pfioc_nv) |
| 2165 | #define	DIOCADDETHRULE		_IOWR('D', 97, struct pfioc_nv) |
| 2166 | #define	DIOCGETETHRULE		_IOWR('D', 98, struct pfioc_nv) |
| 2167 | #define	DIOCGETETHRULES		_IOWR('D', 99, struct pfioc_nv) |
| 2168 | #define	DIOCGETETHRULESETS	_IOWR('D', 100, struct pfioc_nv) |
| 2169 | #define	DIOCGETETHRULESET	_IOWR('D', 101, struct pfioc_nv) |
| 2170 | #define DIOCSETREASS		_IOWR('D', 102, u_int32_t) |
| 2171 | |
| 2172 | struct pf_ifspeed_v0 { |
| 2173 | 	char			ifname[IFNAMSIZ]; |
| 2174 | 	u_int32_t		baudrate; |
| 2175 | }; |
| 2176 | |
| 2177 | struct pf_ifspeed_v1 { |
| 2178 | 	char			ifname[IFNAMSIZ]; |
| 2179 | 	u_int32_t		baudrate32; |
| 2180 | 	/* layout identical to struct pf_ifspeed_v0 up to this point */ |
| 2181 | 	u_int64_t		baudrate; |
| 2182 | }; |
| 2183 | |
| 2184 | /* Latest version of struct pf_ifspeed_vX */ |
| 2185 | #define PF_IFSPEED_VERSION	1 |
| 2186 | |
| 2187 | /* |
| 2188 | * Compatibility and convenience macros |
| 2189 | */ |
| 2190 | #ifndef _KERNEL |
| 2191 | #ifdef PFIOC_USE_LATEST |
| 2192 | /* |
| 2193 | * Maintaining in-tree consumers of the ioctl interface is easier when that |
| 2194 | * code can be written in terms old names that refer to the latest interface |
| 2195 | * version as that reduces the required changes in the consumers to those |
| 2196 | * that are functionally necessary to accommodate a new interface version. |
| 2197 | */ |
| 2198 | #define	pfioc_altq	__CONCAT(pfioc_altq_v, PFIOC_ALTQ_VERSION) |
| 2199 | #define	pfioc_qstats	__CONCAT(pfioc_qstats_v, PFIOC_QSTATS_VERSION) |
| 2200 | #define	pf_ifspeed	__CONCAT(pf_ifspeed_v, PF_IFSPEED_VERSION) |
| 2201 | |
| 2202 | #define	DIOCADDALTQ	__CONCAT(DIOCADDALTQV, PFIOC_ALTQ_VERSION) |
| 2203 | #define	DIOCGETALTQS	__CONCAT(DIOCGETALTQSV, PFIOC_ALTQ_VERSION) |
| 2204 | #define	DIOCGETALTQ	__CONCAT(DIOCGETALTQV, PFIOC_ALTQ_VERSION) |
| 2205 | #define	DIOCCHANGEALTQ	__CONCAT(DIOCCHANGEALTQV, PFIOC_ALTQ_VERSION) |
| 2206 | #define	DIOCGETQSTATS	__CONCAT(DIOCGETQSTATSV, PFIOC_QSTATS_VERSION) |
| 2207 | #define	DIOCGIFSPEED	__CONCAT(DIOCGIFSPEEDV, PF_IFSPEED_VERSION) |
| 2208 | #else |
| 2209 | /* |
| 2210 | * When building out-of-tree code that is written for the old interface, |
| 2211 | * such as may exist in ports for example, resolve the old struct tags and |
| 2212 | * ioctl command names to the v0 versions. |
| 2213 | */ |
| 2214 | #define	pfioc_altq	__CONCAT(pfioc_altq_v, 0) |
| 2215 | #define	pfioc_qstats	__CONCAT(pfioc_qstats_v, 0) |
| 2216 | #define	pf_ifspeed	__CONCAT(pf_ifspeed_v, 0) |
| 2217 | |
| 2218 | #define	DIOCADDALTQ	__CONCAT(DIOCADDALTQV, 0) |
| 2219 | #define	DIOCGETALTQS	__CONCAT(DIOCGETALTQSV, 0) |
| 2220 | #define	DIOCGETALTQ	__CONCAT(DIOCGETALTQV, 0) |
| 2221 | #define	DIOCCHANGEALTQ	__CONCAT(DIOCCHANGEALTQV, 0) |
| 2222 | #define	DIOCGETQSTATS	__CONCAT(DIOCGETQSTATSV, 0) |
| 2223 | #define	DIOCGIFSPEED	__CONCAT(DIOCGIFSPEEDV, 0) |
| 2224 | #endif /* PFIOC_USE_LATEST */ |
| 2225 | #endif /* _KERNEL */ |
| 2226 | |
| 2227 | #ifdef _KERNEL |
| 2228 | LIST_HEAD(pf_ksrc_node_list, pf_ksrc_node); |
| 2229 | struct pf_srchash { |
| 2230 | 	struct pf_ksrc_node_list		nodes; |
| 2231 | 	struct mtx			lock; |
| 2232 | }; |
| 2233 | |
| 2234 | struct pf_keyhash { |
| 2235 | 	LIST_HEAD(, pf_state_key)	keys; |
| 2236 | 	struct mtx			lock; |
| 2237 | }; |
| 2238 | |
| 2239 | struct pf_idhash { |
| 2240 | 	LIST_HEAD(, pf_kstate)		states; |
| 2241 | 	struct mtx			lock; |
| 2242 | }; |
| 2243 | |
| 2244 | struct pf_udpendpointhash { |
| 2245 | 	LIST_HEAD(, pf_udp_endpoint)	endpoints; |
| 2246 | 	/* refcont is synchronized on the source endpoint's row lock */ |
| 2247 | 	struct mtx			lock; |
| 2248 | }; |
| 2249 | |
| 2250 | extern u_long		pf_ioctl_maxcount; |
| 2251 | VNET_DECLARE(u_long, pf_hashmask); |
| 2252 | #define V_pf_hashmask	VNET(pf_hashmask) |
| 2253 | VNET_DECLARE(u_long, pf_srchashmask); |
| 2254 | #define V_pf_srchashmask	VNET(pf_srchashmask) |
| 2255 | VNET_DECLARE(u_long, pf_udpendpointhashmask); |
| 2256 | #define V_pf_udpendpointhashmask	VNET(pf_udpendpointhashmask) |
| 2257 | #define	PF_HASHSIZ	(131072) |
| 2258 | #define	PF_SRCHASHSIZ	(PF_HASHSIZ/4) |
| 2259 | #define	PF_UDPENDHASHSIZ	(PF_HASHSIZ/4) |
| 2260 | VNET_DECLARE(struct pf_keyhash *, pf_keyhash); |
| 2261 | VNET_DECLARE(struct pf_idhash *, pf_idhash); |
| 2262 | VNET_DECLARE(struct pf_udpendpointhash *, pf_udpendpointhash); |
| 2263 | #define V_pf_keyhash	VNET(pf_keyhash) |
| 2264 | #define	V_pf_idhash	VNET(pf_idhash) |
| 2265 | #define	V_pf_udpendpointhash	VNET(pf_udpendpointhash) |
| 2266 | VNET_DECLARE(struct pf_srchash *, pf_srchash); |
| 2267 | #define	V_pf_srchash	VNET(pf_srchash) |
| 2268 | |
| 2269 | #define	PF_IDHASHID(id)	(be64toh(id) % (V_pf_hashmask + 1)) |
| 2270 | #define	PF_IDHASH(s)	PF_IDHASHID((s)->id) |
| 2271 | |
| 2272 | VNET_DECLARE(void *, pf_swi_cookie); |
| 2273 | #define V_pf_swi_cookie	VNET(pf_swi_cookie) |
| 2274 | VNET_DECLARE(struct intr_event *, pf_swi_ie); |
| 2275 | #define	V_pf_swi_ie	VNET(pf_swi_ie) |
| 2276 | |
| 2277 | VNET_DECLARE(struct unrhdr64, pf_stateid); |
| 2278 | #define	V_pf_stateid	VNET(pf_stateid) |
| 2279 | |
| 2280 | TAILQ_HEAD(pf_altqqueue, pf_altq); |
| 2281 | VNET_DECLARE(struct pf_altqqueue,	 pf_altqs[4]); |
| 2282 | #define	V_pf_altqs			 VNET(pf_altqs) |
| 2283 | VNET_DECLARE(struct pf_kpalist,		 pf_pabuf[3]); |
| 2284 | #define	V_pf_pabuf			 VNET(pf_pabuf) |
| 2285 | |
| 2286 | VNET_DECLARE(u_int32_t,			 ticket_altqs_active); |
| 2287 | #define	V_ticket_altqs_active		 VNET(ticket_altqs_active) |
| 2288 | VNET_DECLARE(u_int32_t,			 ticket_altqs_inactive); |
| 2289 | #define	V_ticket_altqs_inactive		 VNET(ticket_altqs_inactive) |
| 2290 | VNET_DECLARE(int,			 altqs_inactive_open); |
| 2291 | #define	V_altqs_inactive_open		 VNET(altqs_inactive_open) |
| 2292 | VNET_DECLARE(u_int32_t,			 ticket_pabuf); |
| 2293 | #define	V_ticket_pabuf			 VNET(ticket_pabuf) |
| 2294 | VNET_DECLARE(struct pf_altqqueue *,	 pf_altqs_active); |
| 2295 | #define	V_pf_altqs_active		 VNET(pf_altqs_active) |
| 2296 | VNET_DECLARE(struct pf_altqqueue *,	 pf_altq_ifs_active); |
| 2297 | #define	V_pf_altq_ifs_active		 VNET(pf_altq_ifs_active) |
| 2298 | VNET_DECLARE(struct pf_altqqueue *,	 pf_altqs_inactive); |
| 2299 | #define	V_pf_altqs_inactive		 VNET(pf_altqs_inactive) |
| 2300 | VNET_DECLARE(struct pf_altqqueue *,	 pf_altq_ifs_inactive); |
| 2301 | #define	V_pf_altq_ifs_inactive		 VNET(pf_altq_ifs_inactive) |
| 2302 | |
| 2303 | VNET_DECLARE(struct pf_krulequeue, pf_unlinked_rules); |
| 2304 | #define	V_pf_unlinked_rules	VNET(pf_unlinked_rules) |
| 2305 | |
| 2306 | #ifdef PF_WANT_32_TO_64_COUNTER |
| 2307 | LIST_HEAD(allkiflist_head, pfi_kkif); |
| 2308 | VNET_DECLARE(struct allkiflist_head, pf_allkiflist); |
| 2309 | #define V_pf_allkiflist VNET(pf_allkiflist) |
| 2310 | VNET_DECLARE(size_t, pf_allkifcount); |
| 2311 | #define V_pf_allkifcount VNET(pf_allkifcount) |
| 2312 | VNET_DECLARE(struct pfi_kkif *, pf_kifmarker); |
| 2313 | #define V_pf_kifmarker VNET(pf_kifmarker) |
| 2314 | |
| 2315 | LIST_HEAD(allrulelist_head, pf_krule); |
| 2316 | VNET_DECLARE(struct allrulelist_head, pf_allrulelist); |
| 2317 | #define V_pf_allrulelist VNET(pf_allrulelist) |
| 2318 | VNET_DECLARE(size_t, pf_allrulecount); |
| 2319 | #define V_pf_allrulecount VNET(pf_allrulecount) |
| 2320 | VNET_DECLARE(struct pf_krule *, pf_rulemarker); |
| 2321 | #define V_pf_rulemarker VNET(pf_rulemarker) |
| 2322 | #endif |
| 2323 | |
| 2324 | int				 pf_start(void); |
| 2325 | int				 pf_stop(void); |
| 2326 | void				 pf_initialize(void); |
| 2327 | void				 pf_mtag_initialize(void); |
| 2328 | void				 pf_mtag_cleanup(void); |
| 2329 | void				 pf_cleanup(void); |
| 2330 | |
| 2331 | struct pf_mtag			*pf_get_mtag(struct mbuf *); |
| 2332 | |
| 2333 | extern void			 pf_calc_skip_steps(struct pf_krulequeue *); |
| 2334 | #ifdef ALTQ |
| 2335 | extern	void			 pf_altq_ifnet_event(struct ifnet *, int); |
| 2336 | #endif |
| 2337 | VNET_DECLARE(uma_zone_t,	 pf_state_z); |
| 2338 | #define	V_pf_state_z		 VNET(pf_state_z) |
| 2339 | VNET_DECLARE(uma_zone_t,	 pf_state_key_z); |
| 2340 | #define	V_pf_state_key_z	 VNET(pf_state_key_z) |
| 2341 | VNET_DECLARE(uma_zone_t,	 pf_udp_mapping_z); |
| 2342 | #define	V_pf_udp_mapping_z	 VNET(pf_udp_mapping_z) |
| 2343 | VNET_DECLARE(uma_zone_t,	 pf_state_scrub_z); |
| 2344 | #define	V_pf_state_scrub_z	 VNET(pf_state_scrub_z) |
| 2345 | VNET_DECLARE(uma_zone_t,	 pf_anchor_z); |
| 2346 | #define	V_pf_anchor_z		 VNET(pf_anchor_z) |
| 2347 | VNET_DECLARE(uma_zone_t,	 pf_eth_anchor_z); |
| 2348 | #define	V_pf_eth_anchor_z	 VNET(pf_eth_anchor_z) |
| 2349 | |
| 2350 | extern void			 pf_purge_thread(void *); |
| 2351 | extern void			 pf_unload_vnet_purge(void); |
| 2352 | extern void			 pf_intr(void *); |
| 2353 | extern void			 pf_purge_expired_src_nodes(void); |
| 2354 | |
| 2355 | extern int			 pf_remove_state(struct pf_kstate *); |
| 2356 | extern int			 pf_state_insert(struct pfi_kkif *, |
| 2357 | 				 struct pfi_kkif *, |
| 2358 | 				 struct pf_state_key *, |
| 2359 | 				 struct pf_state_key *, |
| 2360 | 				 struct pf_kstate *); |
| 2361 | extern struct pf_kstate		*pf_alloc_state(int); |
| 2362 | extern void			 pf_free_state(struct pf_kstate *); |
| 2363 | extern void			 pf_killstates(struct pf_kstate_kill *, |
| 2364 | 				 unsigned int *); |
| 2365 | extern unsigned int		 pf_clear_states(const struct pf_kstate_kill *); |
| 2366 | |
| 2367 | static __inline void |
| 2368 | pf_ref_state(struct pf_kstate *s) |
| 2369 | { |
| 2370 | |
| 2371 | 	refcount_acquire(&s->refs); |
| 2372 | } |
| 2373 | |
| 2374 | static __inline int |
| 2375 | pf_release_state(struct pf_kstate *s) |
| 2376 | { |
| 2377 | |
| 2378 | 	if (refcount_release(&s->refs)) { |
| 2379 | 		pf_free_state(s); |
| 2380 | 		return (1); |
| 2381 | 	} else |
| 2382 | 		return (0); |
| 2383 | } |
| 2384 | |
| 2385 | static __inline int |
| 2386 | pf_release_staten(struct pf_kstate *s, u_int n) |
| 2387 | { |
| 2388 | |
| 2389 | 	if (refcount_releasen(&s->refs, n)) { |
| 2390 | 		pf_free_state(s); |
| 2391 | 		return (1); |
| 2392 | 	} else |
| 2393 | 		return (0); |
| 2394 | } |
| 2395 | |
| 2396 | static __inline uint64_t |
| 2397 | pf_get_uptime(void) |
| 2398 | { |
| 2399 | 	struct timeval t; |
| 2400 | 	microuptime(&t); |
| 2401 | 	return ((t.tv_sec * 1000) + (t.tv_usec / 1000)); |
| 2402 | } |
| 2403 | |
| 2404 | static __inline uint64_t |
| 2405 | pf_get_time(void) |
| 2406 | { |
| 2407 | 	struct timeval t; |
| 2408 | 	microtime(&t); |
| 2409 | 	return ((t.tv_sec * 1000) + (t.tv_usec / 1000)); |
| 2410 | } |
| 2411 | |
| 2412 | extern struct pf_kstate		*pf_find_state_byid(uint64_t, uint32_t); |
| 2413 | extern struct pf_kstate		*pf_find_state_all( |
| 2414 | 				 const struct pf_state_key_cmp *, |
| 2415 | 				 u_int, int *); |
| 2416 | extern bool			pf_find_state_all_exists( |
| 2417 | 				 const struct pf_state_key_cmp *, |
| 2418 | 				 u_int); |
| 2419 | extern struct pf_udp_mapping	*pf_udp_mapping_find(struct pf_udp_endpoint_cmp |
| 2420 | 				 *endpoint); |
| 2421 | extern struct pf_udp_mapping	*pf_udp_mapping_create(sa_family_t af, |
| 2422 | 				 struct pf_addr *src_addr, uint16_t src_port, |
| 2423 | 				 struct pf_addr *nat_addr, uint16_t nat_port); |
| 2424 | extern int			 pf_udp_mapping_insert(struct pf_udp_mapping |
| 2425 | 				 *mapping); |
| 2426 | extern void			 pf_udp_mapping_release(struct pf_udp_mapping |
| 2427 | 				 *mapping); |
| 2428 | uint32_t			 pf_hashsrc(struct pf_addr *, sa_family_t); |
| 2429 | extern bool			 pf_src_node_exists(struct pf_ksrc_node **, |
| 2430 | 				 struct pf_srchash *); |
| 2431 | extern struct pf_ksrc_node	*pf_find_src_node(struct pf_addr *, |
| 2432 | 				 struct pf_krule *, sa_family_t, |
| 2433 | 				 struct pf_srchash **, pf_sn_types_t, bool); |
| 2434 | extern void			 pf_unlink_src_node(struct pf_ksrc_node *); |
| 2435 | extern u_int			 pf_free_src_nodes(struct pf_ksrc_node_list *); |
| 2436 | extern void			 pf_print_state(struct pf_kstate *); |
| 2437 | extern void			 pf_print_flags(uint16_t); |
| 2438 | extern int			 pf_addr_wrap_neq(struct pf_addr_wrap *, |
| 2439 | 				 struct pf_addr_wrap *); |
| 2440 | extern u_int16_t		 pf_cksum_fixup(u_int16_t, u_int16_t, u_int16_t, |
| 2441 | 				 u_int8_t); |
| 2442 | extern u_int16_t		 pf_proto_cksum_fixup(struct mbuf *, u_int16_t, |
| 2443 | 				 u_int16_t, u_int16_t, u_int8_t); |
| 2444 | |
| 2445 | VNET_DECLARE(struct ifnet *,		 sync_ifp); |
| 2446 | #define	V_sync_ifp		 	 VNET(sync_ifp); |
| 2447 | VNET_DECLARE(struct pf_krule,		 pf_default_rule); |
| 2448 | #define	V_pf_default_rule		 VNET(pf_default_rule) |
| 2449 | extern void			 pf_addrcpy(struct pf_addr *, const struct pf_addr *, |
| 2450 | 				 sa_family_t); |
| 2451 | void				pf_free_rule(struct pf_krule *); |
| 2452 | |
| 2453 | int	pf_test_eth(int, int, struct ifnet *, struct mbuf **, struct inpcb *); |
| 2454 | int	pf_scan_sctp(struct pf_pdesc *); |
| 2455 | #if defined(INET) || defined(INET6) |
| 2456 | int	pf_test(sa_family_t, int, int, struct ifnet *, struct mbuf **, struct inpcb *, |
| 2457 | 	 struct pf_rule_actions *); |
| 2458 | #endif |
| 2459 | #ifdef INET |
| 2460 | int	pf_normalize_ip(u_short *, struct pf_pdesc *); |
| 2461 | #endif /* INET */ |
| 2462 | |
| 2463 | void	pf_poolmask(struct pf_addr *, struct pf_addr*, |
| 2464 | 	 struct pf_addr *, struct pf_addr *, sa_family_t); |
| 2465 | void	pf_addr_inc(struct pf_addr *, sa_family_t); |
| 2466 | #ifdef INET6 |
| 2467 | int	pf_normalize_ip6(int, u_short *, struct pf_pdesc *); |
| 2468 | int	pf_max_frag_size(struct mbuf *); |
| 2469 | int	pf_refragment6(struct ifnet *, struct mbuf **, struct m_tag *, |
| 2470 | 	 struct ifnet *, bool); |
| 2471 | #endif /* INET6 */ |
| 2472 | |
| 2473 | int	pf_multihome_scan_init(int, int, struct pf_pdesc *); |
| 2474 | int	pf_multihome_scan_asconf(int, int, struct pf_pdesc *); |
| 2475 | |
| 2476 | u_int32_t	pf_new_isn(struct pf_kstate *); |
| 2477 | void *pf_pull_hdr(const struct mbuf *, int, void *, int, u_short *, u_short *, |
| 2478 | 	 sa_family_t); |
| 2479 | void	pf_change_a(void *, u_int16_t *, u_int32_t, u_int8_t); |
| 2480 | void	pf_change_proto_a(struct mbuf *, void *, u_int16_t *, u_int32_t, |
| 2481 | 	 u_int8_t); |
| 2482 | void	pf_change_tcp_a(struct mbuf *, void *, u_int16_t *, u_int32_t); |
| 2483 | int	pf_patch_16(struct pf_pdesc *, void *, u_int16_t, bool); |
| 2484 | int	pf_patch_32(struct pf_pdesc *, void *, u_int32_t, bool); |
| 2485 | void	pf_send_deferred_syn(struct pf_kstate *); |
| 2486 | int	pf_match_addr(u_int8_t, const struct pf_addr *, |
| 2487 | 	 const struct pf_addr *, const struct pf_addr *, sa_family_t); |
| 2488 | int	pf_match_addr_range(const struct pf_addr *, const struct pf_addr *, |
| 2489 | 	 const struct pf_addr *, sa_family_t); |
| 2490 | int	pf_match_port(u_int8_t, u_int16_t, u_int16_t, u_int16_t); |
| 2491 | |
| 2492 | void	pf_normalize_init(void); |
| 2493 | void	pf_normalize_cleanup(void); |
| 2494 | int	pf_normalize_tcp(struct pf_pdesc *); |
| 2495 | void	pf_normalize_tcp_cleanup(struct pf_kstate *); |
| 2496 | int	pf_normalize_tcp_init(struct pf_pdesc *, |
| 2497 | 	 struct tcphdr *, struct pf_state_peer *); |
| 2498 | int	pf_normalize_tcp_stateful(struct pf_pdesc *, |
| 2499 | 	 u_short *, struct tcphdr *, struct pf_kstate *, |
| 2500 | 	 struct pf_state_peer *, struct pf_state_peer *, int *); |
| 2501 | int	pf_normalize_sctp_init(struct pf_pdesc *, |
| 2502 | 	 struct pf_state_peer *, struct pf_state_peer *); |
| 2503 | int	pf_normalize_sctp(struct pf_pdesc *); |
| 2504 | u_int32_t |
| 2505 | 	pf_state_expires(const struct pf_kstate *); |
| 2506 | void	pf_purge_expired_fragments(void); |
| 2507 | void	pf_purge_fragments(uint32_t); |
| 2508 | int	pf_routable(struct pf_addr *addr, sa_family_t af, struct pfi_kkif *, |
| 2509 | 	 int); |
| 2510 | int	pf_socket_lookup(struct pf_pdesc *); |
| 2511 | struct pf_state_key *pf_alloc_state_key(int); |
| 2512 | int	pf_translate(struct pf_pdesc *, struct pf_addr *, u_int16_t, |
| 2513 | 	 struct pf_addr *, u_int16_t, u_int16_t, int); |
| 2514 | int	pf_translate_af(struct pf_pdesc *); |
| 2515 | bool	pf_init_threshold(struct pf_kthreshold *, uint32_t, uint32_t); |
| 2516 | uint16_t	pf_tagname2tag(const char *); |
| 2517 | #ifdef ALTQ |
| 2518 | uint16_t	pf_qname2qid(const char *, bool); |
| 2519 | #endif /* ALTQ */ |
| 2520 | |
| 2521 | void	pfr_initialize(void); |
| 2522 | void	pfr_cleanup(void); |
| 2523 | int	pfr_match_addr(struct pfr_ktable *, struct pf_addr *, sa_family_t); |
| 2524 | void	pfr_update_stats(struct pfr_ktable *, struct pf_addr *, sa_family_t, |
| 2525 | 	 u_int64_t, int, int, int); |
| 2526 | int	pfr_pool_get(struct pfr_ktable *, int *, struct pf_addr *, sa_family_t, |
| 2527 | 	 pf_addr_filter_func_t, bool); |
| 2528 | void	pfr_dynaddr_update(struct pfr_ktable *, struct pfi_dynaddr *); |
| 2529 | struct pfr_ktable * |
| 2530 | 	pfr_attach_table(struct pf_kruleset *, char *); |
| 2531 | struct pfr_ktable * |
| 2532 | 	pfr_eth_attach_table(struct pf_keth_ruleset *, char *); |
| 2533 | void	pfr_detach_table(struct pfr_ktable *); |
| 2534 | int	pfr_clr_tables(struct pfr_table *, int *, int); |
| 2535 | int	pfr_add_tables(struct pfr_table *, int, int *, int); |
| 2536 | int	pfr_del_tables(struct pfr_table *, int, int *, int); |
| 2537 | int	pfr_table_count(struct pfr_table *, int); |
| 2538 | int	pfr_get_tables(struct pfr_table *, struct pfr_table *, int *, int); |
| 2539 | int	pfr_get_tstats(struct pfr_table *, struct pfr_tstats *, int *, int); |
| 2540 | int	pfr_clr_tstats(struct pfr_table *, int, int *, int); |
| 2541 | int	pfr_set_tflags(struct pfr_table *, int, int, int, int *, int *, int); |
| 2542 | int	pfr_clr_addrs(struct pfr_table *, int *, int); |
| 2543 | int	pfr_insert_kentry(struct pfr_ktable *, struct pfr_addr *, time_t); |
| 2544 | int	pfr_add_addrs(struct pfr_table *, struct pfr_addr *, int, int *, |
| 2545 | 	 int); |
| 2546 | int	pfr_del_addrs(struct pfr_table *, struct pfr_addr *, int, int *, |
| 2547 | 	 int); |
| 2548 | int	pfr_set_addrs(struct pfr_table *, struct pfr_addr *, int, int *, |
| 2549 | 	 int *, int *, int *, int, u_int32_t); |
| 2550 | int	pfr_get_addrs(struct pfr_table *, struct pfr_addr *, int *, int); |
| 2551 | int	pfr_get_astats(struct pfr_table *, struct pfr_astats *, int *, int); |
| 2552 | int	pfr_clr_astats(struct pfr_table *, struct pfr_addr *, int, int *, |
| 2553 | 	 int); |
| 2554 | int	pfr_tst_addrs(struct pfr_table *, struct pfr_addr *, int, int *, |
| 2555 | 	 int); |
| 2556 | int	pfr_ina_begin(struct pfr_table *, u_int32_t *, int *, int); |
| 2557 | int	pfr_ina_rollback(struct pfr_table *, u_int32_t, int *, int); |
| 2558 | int	pfr_ina_commit(struct pfr_table *, u_int32_t, int *, int *, int); |
| 2559 | int	pfr_ina_define(struct pfr_table *, struct pfr_addr *, int, int *, |
| 2560 | 	 int *, u_int32_t, int); |
| 2561 | struct pfr_ktable |
| 2562 | 	*pfr_ktable_select_active(struct pfr_ktable *); |
| 2563 | |
| 2564 | MALLOC_DECLARE(PFI_MTYPE); |
| 2565 | VNET_DECLARE(struct pfi_kkif *,		 pfi_all); |
| 2566 | #define	V_pfi_all	 		 VNET(pfi_all) |
| 2567 | |
| 2568 | void		 pfi_initialize(void); |
| 2569 | void		 pfi_initialize_vnet(void); |
| 2570 | void		 pfi_cleanup(void); |
| 2571 | void		 pfi_cleanup_vnet(void); |
| 2572 | void		 pfi_kkif_ref(struct pfi_kkif *); |
| 2573 | void		 pfi_kkif_unref(struct pfi_kkif *); |
| 2574 | struct pfi_kkif	*pfi_kkif_find(const char *); |
| 2575 | struct pfi_kkif	*pfi_kkif_attach(struct pfi_kkif *, const char *); |
| 2576 | int		 pfi_kkif_match(struct pfi_kkif *, struct pfi_kkif *); |
| 2577 | void		 pfi_kkif_purge(void); |
| 2578 | int		 pfi_match_addr(struct pfi_dynaddr *, struct pf_addr *, |
| 2579 | 		 sa_family_t); |
| 2580 | int		 pfi_dynaddr_setup(struct pf_addr_wrap *, sa_family_t); |
| 2581 | void		 pfi_dynaddr_remove(struct pfi_dynaddr *); |
| 2582 | void		 pfi_dynaddr_copyout(struct pf_addr_wrap *); |
| 2583 | void		 pfi_update_status(const char *, struct pf_status *); |
| 2584 | void		 pfi_get_ifaces(const char *, struct pfi_kif *, int *); |
| 2585 | int		 pfi_set_flags(const char *, int); |
| 2586 | int		 pfi_clear_flags(const char *, int); |
| 2587 | |
| 2588 | int		 pf_match_tag(struct mbuf *, struct pf_krule *, int *, int); |
| 2589 | int		 pf_tag_packet(struct pf_pdesc *, int); |
| 2590 | int		 pf_addr_cmp(struct pf_addr *, struct pf_addr *, |
| 2591 | 		 sa_family_t); |
| 2592 | |
| 2593 | uint8_t*	 pf_find_tcpopt(u_int8_t *, u_int8_t *, size_t, |
| 2594 | 		 u_int8_t, u_int8_t); |
| 2595 | u_int16_t	 pf_get_mss(struct pf_pdesc *); |
| 2596 | u_int8_t	 pf_get_wscale(struct pf_pdesc *); |
| 2597 | struct mbuf 	*pf_build_tcp(const struct pf_krule *, sa_family_t, |
| 2598 | 		 const struct pf_addr *, const struct pf_addr *, |
| 2599 | 		 u_int16_t, u_int16_t, u_int32_t, u_int32_t, |
| 2600 | 		 u_int8_t, u_int16_t, u_int16_t, u_int8_t, int, |
| 2601 | 		 u_int16_t, u_int16_t, u_int, int); |
| 2602 | void		 pf_send_tcp(const struct pf_krule *, sa_family_t, |
| 2603 | 			 const struct pf_addr *, const struct pf_addr *, |
| 2604 | 			 u_int16_t, u_int16_t, u_int32_t, u_int32_t, |
| 2605 | 			 u_int8_t, u_int16_t, u_int16_t, u_int8_t, int, |
| 2606 | 			 u_int16_t, u_int16_t, int); |
| 2607 | |
| 2608 | void			 pf_syncookies_init(void); |
| 2609 | void			 pf_syncookies_cleanup(void); |
| 2610 | int			 pf_get_syncookies(struct pfioc_nv *); |
| 2611 | int			 pf_set_syncookies(struct pfioc_nv *); |
| 2612 | int			 pf_synflood_check(struct pf_pdesc *); |
| 2613 | void			 pf_syncookie_send(struct pf_pdesc *); |
| 2614 | bool			 pf_syncookie_check(struct pf_pdesc *); |
| 2615 | u_int8_t		 pf_syncookie_validate(struct pf_pdesc *); |
| 2616 | struct mbuf *		 pf_syncookie_recreate_syn(struct pf_pdesc *); |
| 2617 | |
| 2618 | VNET_DECLARE(struct pf_kstatus, pf_status); |
| 2619 | #define	V_pf_status	VNET(pf_status) |
| 2620 | |
| 2621 | struct pf_limit { |
| 2622 | 	uma_zone_t	zone; |
| 2623 | 	u_int		limit; |
| 2624 | }; |
| 2625 | VNET_DECLARE(struct pf_limit, pf_limits[PF_LIMIT_MAX]); |
| 2626 | #define	V_pf_limits VNET(pf_limits) |
| 2627 | |
| 2628 | #endif /* _KERNEL */ |
| 2629 | |
| 2630 | #ifdef _KERNEL |
| 2631 | struct pf_nl_pooladdr { |
| 2632 | 	u_int32_t		 action; |
| 2633 | 	u_int32_t		 ticket; |
| 2634 | 	u_int32_t		 nr; |
| 2635 | 	u_int32_t		 r_num; |
| 2636 | 	u_int8_t		 r_action; |
| 2637 | 	u_int8_t		 r_last; |
| 2638 | 	u_int8_t		 af; |
| 2639 | 	char			 anchor[MAXPATHLEN]; |
| 2640 | 	struct pf_pooladdr	 addr; |
| 2641 | 	/* Above this is identical to pfioc_pooladdr */ |
| 2642 | 	int			 which; |
| 2643 | }; |
| 2644 | |
| 2645 | VNET_DECLARE(struct pf_kanchor_global,		 pf_anchors); |
| 2646 | #define	V_pf_anchors				 VNET(pf_anchors) |
| 2647 | VNET_DECLARE(struct pf_kanchor,			 pf_main_anchor); |
| 2648 | #define	V_pf_main_anchor			 VNET(pf_main_anchor) |
| 2649 | VNET_DECLARE(struct pf_keth_anchor_global,	 pf_keth_anchors); |
| 2650 | #define	V_pf_keth_anchors			 VNET(pf_keth_anchors) |
| 2651 | #define pf_main_ruleset	V_pf_main_anchor.ruleset |
| 2652 | |
| 2653 | VNET_DECLARE(struct pf_keth_anchor,		 pf_main_keth_anchor); |
| 2654 | #define V_pf_main_keth_anchor			 VNET(pf_main_keth_anchor) |
| 2655 | VNET_DECLARE(struct pf_keth_ruleset*,		 pf_keth); |
| 2656 | #define	V_pf_keth				 VNET(pf_keth) |
| 2657 | |
| 2658 | void			 pf_init_kruleset(struct pf_kruleset *); |
| 2659 | void			 pf_init_keth(struct pf_keth_ruleset *); |
| 2660 | int			 pf_kanchor_setup(struct pf_krule *, |
| 2661 | 			 const struct pf_kruleset *, const char *); |
| 2662 | int			 pf_kanchor_copyout(const struct pf_kruleset *, |
| 2663 | 			 const struct pf_krule *, char *, size_t); |
| 2664 | int			 pf_kanchor_nvcopyout(const struct pf_kruleset *, |
| 2665 | 			 const struct pf_krule *, nvlist_t *); |
| 2666 | void			 pf_remove_kanchor(struct pf_krule *); |
| 2667 | void			 pf_remove_if_empty_kruleset(struct pf_kruleset *); |
| 2668 | struct pf_kruleset	*pf_find_kruleset(const char *); |
| 2669 | struct pf_kruleset	*pf_get_leaf_kruleset(char *, char **); |
| 2670 | struct pf_kruleset	*pf_find_or_create_kruleset(const char *); |
| 2671 | void			 pf_rs_initialize(void); |
| 2672 | void			 pf_rule_tree_free(struct pf_krule_global *); |
| 2673 | |
| 2674 | |
| 2675 | struct pf_krule		*pf_krule_alloc(void); |
| 2676 | |
| 2677 | void			 pf_remove_if_empty_keth_ruleset( |
| 2678 | 			 struct pf_keth_ruleset *); |
| 2679 | struct pf_keth_ruleset	*pf_find_keth_ruleset(const char *); |
| 2680 | struct pf_keth_anchor	*pf_find_keth_anchor(const char *); |
| 2681 | int			 pf_keth_anchor_setup(struct pf_keth_rule *, |
| 2682 | 			 const struct pf_keth_ruleset *, const char *); |
| 2683 | int			 pf_keth_anchor_nvcopyout( |
| 2684 | 			 const struct pf_keth_ruleset *, |
| 2685 | 			 const struct pf_keth_rule *, nvlist_t *); |
| 2686 | struct pf_keth_ruleset	*pf_find_or_create_keth_ruleset(const char *); |
| 2687 | void			 pf_keth_anchor_remove(struct pf_keth_rule *); |
| 2688 | |
| 2689 | int			 pf_ioctl_getrules(struct pfioc_rule *); |
| 2690 | int			 pf_ioctl_addrule(struct pf_krule *, uint32_t, |
| 2691 | 			 uint32_t, const char *, const char *, uid_t uid, |
| 2692 | 			 pid_t); |
| 2693 | void			 pf_ioctl_clear_status(void); |
| 2694 | int			 pf_ioctl_get_timeout(int, int *); |
| 2695 | int			 pf_ioctl_set_timeout(int, int, int *); |
| 2696 | int			 pf_ioctl_get_limit(int, unsigned int *); |
| 2697 | int			 pf_ioctl_set_limit(int, unsigned int, unsigned int *); |
| 2698 | int			 pf_ioctl_begin_addrs(uint32_t *); |
| 2699 | int			 pf_ioctl_add_addr(struct pf_nl_pooladdr *); |
| 2700 | int			 pf_ioctl_get_addrs(struct pf_nl_pooladdr *); |
| 2701 | int			 pf_ioctl_get_addr(struct pf_nl_pooladdr *); |
| 2702 | int			 pf_ioctl_get_rulesets(struct pfioc_ruleset *); |
| 2703 | int			 pf_ioctl_get_ruleset(struct pfioc_ruleset *); |
| 2704 | int			 pf_ioctl_natlook(struct pfioc_natlook *); |
| 2705 | |
| 2706 | void			 pf_krule_free(struct pf_krule *); |
| 2707 | void			 pf_krule_clear_counters(struct pf_krule *); |
| 2708 | void			 pf_addr_copyout(struct pf_addr_wrap *); |
| 2709 | #endif |
| 2710 | |
| 2711 | /* The fingerprint functions can be linked into userland programs (tcpdump) */ |
| 2712 | int	pf_osfp_add(struct pf_osfp_ioctl *); |
| 2713 | #ifdef _KERNEL |
| 2714 | struct pf_osfp_enlist * |
| 2715 | 	pf_osfp_fingerprint(struct pf_pdesc *, const struct tcphdr *); |
| 2716 | #endif /* _KERNEL */ |
| 2717 | void	pf_osfp_flush(void); |
| 2718 | int	pf_osfp_get(struct pf_osfp_ioctl *); |
| 2719 | int	pf_osfp_match(struct pf_osfp_enlist *, pf_osfp_t); |
| 2720 | |
| 2721 | #ifdef _KERNEL |
| 2722 | void			 pf_print_host(struct pf_addr *, u_int16_t, sa_family_t); |
| 2723 | |
| 2724 | enum pf_test_status	 pf_step_into_anchor(struct pf_test_ctx *, struct pf_krule *, |
| 2725 | 			 struct pf_krule_slist *match_rules); |
| 2726 | enum pf_test_status	 pf_match_rule(struct pf_test_ctx *, struct pf_kruleset *, |
| 2727 | 			 struct pf_krule_slist *); |
| 2728 | void			 pf_step_into_keth_anchor(struct pf_keth_anchor_stackframe *, |
| 2729 | 			 int *, struct pf_keth_ruleset **, |
| 2730 | 			 struct pf_keth_rule **, struct pf_keth_rule **, |
| 2731 | 			 int *); |
| 2732 | int			 pf_step_out_of_keth_anchor(struct pf_keth_anchor_stackframe *, |
| 2733 | 			 int *, struct pf_keth_ruleset **, |
| 2734 | 			 struct pf_keth_rule **, struct pf_keth_rule **, |
| 2735 | 			 int *); |
| 2736 | |
| 2737 | u_short			 pf_map_addr(sa_family_t, struct pf_krule *, |
| 2738 | 			 struct pf_addr *, struct pf_addr *, |
| 2739 | 			 struct pfi_kkif **nkif, sa_family_t *, |
| 2740 | 			 struct pf_addr *, struct pf_kpool *); |
| 2741 | u_short			 pf_map_addr_sn(u_int8_t, struct pf_krule *, |
| 2742 | 			 struct pf_addr *, struct pf_addr *, |
| 2743 | 			 sa_family_t *, struct pfi_kkif **, |
| 2744 | 			 struct pf_addr *, struct pf_kpool *, |
| 2745 | 			 pf_sn_types_t); |
| 2746 | int			 pf_get_transaddr_af(struct pf_krule *, |
| 2747 | 			 struct pf_pdesc *); |
| 2748 | u_short			 pf_get_translation(struct pf_test_ctx *); |
| 2749 | u_short			 pf_get_transaddr(struct pf_test_ctx *, |
| 2750 | 			 struct pf_krule *, |
| 2751 | 			 u_int8_t, struct pf_kpool *); |
| 2752 | int			 pf_translate_compat(struct pf_test_ctx *); |
| 2753 | |
| 2754 | int			 pf_state_key_setup(struct pf_pdesc *, |
| 2755 | 			 u_int16_t, u_int16_t, |
| 2756 | 			 struct pf_state_key **sk, struct pf_state_key **nk); |
| 2757 | struct pf_state_key	*pf_state_key_clone(const struct pf_state_key *); |
| 2758 | void			 pf_rule_to_actions(struct pf_krule *, |
| 2759 | 			 struct pf_rule_actions *); |
| 2760 | int			 pf_normalize_mss(struct pf_pdesc *pd); |
| 2761 | #if defined(INET) || defined(INET6) |
| 2762 | void	pf_scrub(struct pf_pdesc *); |
| 2763 | #endif |
| 2764 | |
| 2765 | struct pfi_kkif		*pf_kkif_create(int); |
| 2766 | void			 pf_kkif_free(struct pfi_kkif *); |
| 2767 | void			 pf_kkif_zero(struct pfi_kkif *); |
| 2768 | |
| 2769 | |
| 2770 | /* NAT64 functions. */ |
| 2771 | int	 inet_nat64(int, const void *, void *, const void *, u_int8_t); |
| 2772 | int	 inet_nat64_inet(const void *, void *, const void *, u_int8_t); |
| 2773 | int	 inet_nat64_inet6(const void *, void *, const void *, u_int8_t); |
| 2774 | |
| 2775 | int	 inet_nat46(int, const void *, void *, const void *, u_int8_t); |
| 2776 | int	 inet_nat46_inet(const void *, void *, const void *, u_int8_t); |
| 2777 | int	 inet_nat46_inet6(const void *, void *, const void *, u_int8_t); |
| 2778 | |
| 2779 | #endif /* _KERNEL */ |
| 2780 | |
| 2781 | #endif /* _NET_PFVAR_H_ */ |