pf: Revert commit 5165ac2
[dragonfly.git] / sys / net / pf / pf.c
CommitLineData
05ac5751 1/* $OpenBSD: pf.c,v 1.552.2.1 2007/11/27 16:37:57 henning Exp $ */
21fe214d
JL
2/* add $OpenBSD: pf.c,v 1.553 2007/08/23 11:15:49 dhartmei Exp $ */
3/* add $OpenBSD: pf.c,v 1.554 2007/08/28 16:09:12 henning Exp $ */
4
02742ec6
JS
5
6/*
7 * Copyright (c) 2004 The DragonFly Project. All rights reserved.
8 *
9 * Copyright (c) 2001 Daniel Hartmeier
10 * Copyright (c) 2002,2003 Henning Brauer
11 * All rights reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 *
17 * - Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * - Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials provided
22 * with the distribution.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 *
37 * Effort sponsored in part by the Defense Advanced Research Projects
38 * Agency (DARPA) and Air Force Research Laboratory, Air Force
39 * Materiel Command, USAF, under agreement number F30602-01-2-0537.
40 *
41 */
42
43#include "opt_inet.h"
44#include "opt_inet6.h"
45#include "use_pfsync.h"
46
47#include <sys/param.h>
48#include <sys/systm.h>
49#include <sys/malloc.h>
50#include <sys/mbuf.h>
51#include <sys/filio.h>
52#include <sys/socket.h>
53#include <sys/socketvar.h>
54#include <sys/kernel.h>
55#include <sys/time.h>
56#include <sys/sysctl.h>
57#include <sys/endian.h>
58#include <vm/vm_zone.h>
70224baa
JL
59#include <sys/proc.h>
60#include <sys/kthread.h>
02742ec6 61
cd8ab232
MD
62#include <sys/mplock2.h>
63
02742ec6
JS
64#include <machine/inttypes.h>
65
66#include <net/if.h>
67#include <net/if_types.h>
68#include <net/bpf.h>
4599cf19 69#include <net/netisr.h>
02742ec6
JS
70#include <net/route.h>
71
72#include <netinet/in.h>
73#include <netinet/in_var.h>
74#include <netinet/in_systm.h>
75#include <netinet/ip.h>
76#include <netinet/ip_var.h>
77#include <netinet/tcp.h>
78#include <netinet/tcp_seq.h>
79#include <netinet/udp.h>
80#include <netinet/ip_icmp.h>
81#include <netinet/in_pcb.h>
82#include <netinet/tcp_timer.h>
83#include <netinet/tcp_var.h>
84#include <netinet/udp_var.h>
85#include <netinet/icmp_var.h>
70224baa 86#include <netinet/if_ether.h>
02742ec6
JS
87
88#include <net/pf/pfvar.h>
89#include <net/pf/if_pflog.h>
90
91#if NPFSYNC > 0
92#include <net/pf/if_pfsync.h>
93#endif /* NPFSYNC > 0 */
94
95#ifdef INET6
96#include <netinet/ip6.h>
97#include <netinet/in_pcb.h>
98#include <netinet/icmp6.h>
99#include <netinet6/nd6.h>
100#include <netinet6/ip6_var.h>
101#include <netinet6/in6_pcb.h>
102#endif /* INET6 */
103
104#include <sys/in_cksum.h>
4599cf19 105#include <sys/ucred.h>
02742ec6
JS
106#include <machine/limits.h>
107#include <sys/msgport2.h>
4599cf19 108#include <net/netmsg2.h>
02742ec6
JS
109
110extern int ip_optcopy(struct ip *, struct ip *);
70224baa 111extern int debug_pfugidhack;
02742ec6 112
4b1cf444 113#define DPFPRINTF(n, x) if (pf_status.debug >= (n)) kprintf x
02742ec6
JS
114
115/*
116 * Global variables
117 */
118
315a7da3
JL
119/* state tables */
120struct pf_state_tree_lan_ext pf_statetbl_lan_ext;
121struct pf_state_tree_ext_gwy pf_statetbl_ext_gwy;
122
02742ec6
JS
123struct pf_altqqueue pf_altqs[2];
124struct pf_palist pf_pabuf;
125struct pf_altqqueue *pf_altqs_active;
126struct pf_altqqueue *pf_altqs_inactive;
127struct pf_status pf_status;
128
129u_int32_t ticket_altqs_active;
130u_int32_t ticket_altqs_inactive;
131int altqs_inactive_open;
132u_int32_t ticket_pabuf;
133
70224baa
JL
134struct pf_anchor_stackframe {
135 struct pf_ruleset *rs;
136 struct pf_rule *r;
137 struct pf_anchor_node *parent;
138 struct pf_anchor *child;
139} pf_anchor_stack[64];
02742ec6 140
315a7da3
JL
141vm_zone_t pf_src_tree_pl, pf_rule_pl, pf_pooladdr_pl;
142vm_zone_t pf_state_pl, pf_state_key_pl;
143vm_zone_t pf_altq_pl;
02742ec6
JS
144
145void pf_print_host(struct pf_addr *, u_int16_t, u_int8_t);
02742ec6 146
70224baa
JL
147void pf_init_threshold(struct pf_threshold *, u_int32_t,
148 u_int32_t);
149void pf_add_threshold(struct pf_threshold *);
150int pf_check_threshold(struct pf_threshold *);
151
02742ec6
JS
152void pf_change_ap(struct pf_addr *, u_int16_t *,
153 u_int16_t *, u_int16_t *, struct pf_addr *,
154 u_int16_t, u_int8_t, sa_family_t);
70224baa
JL
155int pf_modulate_sack(struct mbuf *, int, struct pf_pdesc *,
156 struct tcphdr *, struct pf_state_peer *);
02742ec6
JS
157#ifdef INET6
158void pf_change_a6(struct pf_addr *, u_int16_t *,
159 struct pf_addr *, u_int8_t);
160#endif /* INET6 */
161void pf_change_icmp(struct pf_addr *, u_int16_t *,
162 struct pf_addr *, struct pf_addr *, u_int16_t,
163 u_int16_t *, u_int16_t *, u_int16_t *,
164 u_int16_t *, u_int8_t, sa_family_t);
165void pf_send_tcp(const struct pf_rule *, sa_family_t,
166 const struct pf_addr *, const struct pf_addr *,
167 u_int16_t, u_int16_t, u_int32_t, u_int32_t,
70224baa
JL
168 u_int8_t, u_int16_t, u_int16_t, u_int8_t, int,
169 u_int16_t, struct ether_header *, struct ifnet *);
02742ec6
JS
170void pf_send_icmp(struct mbuf *, u_int8_t, u_int8_t,
171 sa_family_t, struct pf_rule *);
172struct pf_rule *pf_match_translation(struct pf_pdesc *, struct mbuf *,
173 int, int, struct pfi_kif *,
174 struct pf_addr *, u_int16_t, struct pf_addr *,
175 u_int16_t, int);
176struct pf_rule *pf_get_translation(struct pf_pdesc *, struct mbuf *,
177 int, int, struct pfi_kif *, struct pf_src_node **,
178 struct pf_addr *, u_int16_t,
179 struct pf_addr *, u_int16_t,
180 struct pf_addr *, u_int16_t *);
315a7da3
JL
181void pf_attach_state(struct pf_state_key *,
182 struct pf_state *, int);
183void pf_detach_state(struct pf_state *, int);
184int pf_test_rule(struct pf_rule **, struct pf_state **,
02742ec6
JS
185 int, struct pfi_kif *, struct mbuf *, int,
186 void *, struct pf_pdesc *, struct pf_rule **,
70224baa 187 struct pf_ruleset **, struct ifqueue *, struct inpcb *);
02742ec6
JS
188int pf_test_fragment(struct pf_rule **, int,
189 struct pfi_kif *, struct mbuf *, void *,
190 struct pf_pdesc *, struct pf_rule **,
191 struct pf_ruleset **);
192int pf_test_state_tcp(struct pf_state **, int,
193 struct pfi_kif *, struct mbuf *, int,
194 void *, struct pf_pdesc *, u_short *);
195int pf_test_state_udp(struct pf_state **, int,
196 struct pfi_kif *, struct mbuf *, int,
197 void *, struct pf_pdesc *);
198int pf_test_state_icmp(struct pf_state **, int,
199 struct pfi_kif *, struct mbuf *, int,
70224baa 200 void *, struct pf_pdesc *, u_short *);
02742ec6
JS
201int pf_test_state_other(struct pf_state **, int,
202 struct pfi_kif *, struct pf_pdesc *);
315a7da3
JL
203int pf_match_tag(struct mbuf *, struct pf_rule *, int *);
204void pf_step_into_anchor(int *, struct pf_ruleset **, int,
205 struct pf_rule **, struct pf_rule **, int *);
70224baa
JL
206int pf_step_out_of_anchor(int *, struct pf_ruleset **,
207 int, struct pf_rule **, struct pf_rule **,
208 int *);
02742ec6
JS
209void pf_hash(struct pf_addr *, struct pf_addr *,
210 struct pf_poolhashkey *, sa_family_t);
211int pf_map_addr(u_int8_t, struct pf_rule *,
212 struct pf_addr *, struct pf_addr *,
213 struct pf_addr *, struct pf_src_node **);
214int pf_get_sport(sa_family_t, u_int8_t, struct pf_rule *,
215 struct pf_addr *, struct pf_addr *, u_int16_t,
216 struct pf_addr *, u_int16_t*, u_int16_t, u_int16_t,
217 struct pf_src_node **);
218void pf_route(struct mbuf **, struct pf_rule *, int,
70224baa
JL
219 struct ifnet *, struct pf_state *,
220 struct pf_pdesc *);
02742ec6 221void pf_route6(struct mbuf **, struct pf_rule *, int,
70224baa
JL
222 struct ifnet *, struct pf_state *,
223 struct pf_pdesc *);
02742ec6
JS
224u_int8_t pf_get_wscale(struct mbuf *, int, u_int16_t,
225 sa_family_t);
226u_int16_t pf_get_mss(struct mbuf *, int, u_int16_t,
227 sa_family_t);
228u_int16_t pf_calc_mss(struct pf_addr *, sa_family_t,
229 u_int16_t);
230void pf_set_rt_ifp(struct pf_state *,
231 struct pf_addr *);
232int pf_check_proto_cksum(struct mbuf *, int, int,
233 u_int8_t, sa_family_t);
234int pf_addr_wrap_neq(struct pf_addr_wrap *,
235 struct pf_addr_wrap *);
315a7da3
JL
236struct pf_state *pf_find_state(struct pfi_kif *,
237 struct pf_state_key_cmp *, u_int8_t);
70224baa 238int pf_src_connlimit(struct pf_state **);
315a7da3
JL
239void pf_stateins_err(const char *, struct pf_state *,
240 struct pfi_kif *);
70224baa
JL
241int pf_check_congestion(struct ifqueue *);
242
243extern int pf_end_threads;
244
245struct pf_pool_limit pf_pool_limits[PF_LIMIT_MAX] = {
246 { &pf_state_pl, PFSTATE_HIWAT },
247 { &pf_src_tree_pl, PFSNODE_HIWAT },
248 { &pf_frent_pl, PFFRAG_FRENT_HIWAT },
249 { &pfr_ktable_pl, PFR_KTABLE_HIWAT },
250 { &pfr_kentry_pl, PFR_KENTRY_HIWAT }
251};
02742ec6
JS
252
253#define STATE_LOOKUP() \
254 do { \
255 if (direction == PF_IN) \
315a7da3 256 *state = pf_find_state(kif, &key, PF_EXT_GWY); \
02742ec6 257 else \
315a7da3 258 *state = pf_find_state(kif, &key, PF_LAN_EXT); \
70224baa 259 if (*state == NULL || (*state)->timeout == PFTM_PURGE) \
02742ec6
JS
260 return (PF_DROP); \
261 if (direction == PF_OUT && \
262 (((*state)->rule.ptr->rt == PF_ROUTETO && \
263 (*state)->rule.ptr->direction == PF_OUT) || \
264 ((*state)->rule.ptr->rt == PF_REPLYTO && \
265 (*state)->rule.ptr->direction == PF_IN)) && \
266 (*state)->rt_kif != NULL && \
267 (*state)->rt_kif != kif) \
268 return (PF_PASS); \
269 } while (0)
270
315a7da3
JL
271#define STATE_TRANSLATE(sk) \
272 (sk)->lan.addr.addr32[0] != (sk)->gwy.addr.addr32[0] || \
273 ((sk)->af == AF_INET6 && \
274 ((sk)->lan.addr.addr32[1] != (sk)->gwy.addr.addr32[1] || \
275 (sk)->lan.addr.addr32[2] != (sk)->gwy.addr.addr32[2] || \
276 (sk)->lan.addr.addr32[3] != (sk)->gwy.addr.addr32[3])) || \
277 (sk)->lan.port != (sk)->gwy.port
02742ec6 278
70224baa
JL
279#define BOUND_IFACE(r, k) \
280 ((r)->rule_flag & PFRULE_IFBOUND) ? (k) : pfi_all
281
282#define STATE_INC_COUNTERS(s) \
283 do { \
284 s->rule.ptr->states++; \
285 if (s->anchor.ptr != NULL) \
286 s->anchor.ptr->states++; \
287 if (s->nat_rule.ptr != NULL) \
288 s->nat_rule.ptr->states++; \
289 } while (0)
290
291#define STATE_DEC_COUNTERS(s) \
292 do { \
293 if (s->nat_rule.ptr != NULL) \
294 s->nat_rule.ptr->states--; \
295 if (s->anchor.ptr != NULL) \
296 s->anchor.ptr->states--; \
297 s->rule.ptr->states--; \
298 } while (0)
02742ec6 299
70224baa 300static __inline int pf_src_compare(struct pf_src_node *, struct pf_src_node *);
315a7da3
JL
301static __inline int pf_state_compare_lan_ext(struct pf_state_key *,
302 struct pf_state_key *);
303static __inline int pf_state_compare_ext_gwy(struct pf_state_key *,
304 struct pf_state_key *);
70224baa 305static __inline int pf_state_compare_id(struct pf_state *,
02742ec6
JS
306 struct pf_state *);
307
308struct pf_src_tree tree_src_tracking;
309
310struct pf_state_tree_id tree_id;
70224baa 311struct pf_state_queue state_list;
02742ec6
JS
312
313RB_GENERATE(pf_src_tree, pf_src_node, entry, pf_src_compare);
315a7da3
JL
314RB_GENERATE(pf_state_tree_lan_ext, pf_state_key,
315 entry_lan_ext, pf_state_compare_lan_ext);
316RB_GENERATE(pf_state_tree_ext_gwy, pf_state_key,
317 entry_ext_gwy, pf_state_compare_ext_gwy);
02742ec6 318RB_GENERATE(pf_state_tree_id, pf_state,
315a7da3
JL
319 entry_id, pf_state_compare_id);
320
321#define PF_DT_SKIP_LANEXT 0x01
322#define PF_DT_SKIP_EXTGWY 0x02
02742ec6 323
70224baa 324static __inline int
02742ec6
JS
325pf_src_compare(struct pf_src_node *a, struct pf_src_node *b)
326{
327 int diff;
328
329 if (a->rule.ptr > b->rule.ptr)
330 return (1);
331 if (a->rule.ptr < b->rule.ptr)
332 return (-1);
333 if ((diff = a->af - b->af) != 0)
334 return (diff);
335 switch (a->af) {
336#ifdef INET
337 case AF_INET:
338 if (a->addr.addr32[0] > b->addr.addr32[0])
339 return (1);
340 if (a->addr.addr32[0] < b->addr.addr32[0])
341 return (-1);
342 break;
343#endif /* INET */
344#ifdef INET6
345 case AF_INET6:
346 if (a->addr.addr32[3] > b->addr.addr32[3])
347 return (1);
348 if (a->addr.addr32[3] < b->addr.addr32[3])
349 return (-1);
350 if (a->addr.addr32[2] > b->addr.addr32[2])
351 return (1);
352 if (a->addr.addr32[2] < b->addr.addr32[2])
353 return (-1);
354 if (a->addr.addr32[1] > b->addr.addr32[1])
355 return (1);
356 if (a->addr.addr32[1] < b->addr.addr32[1])
357 return (-1);
358 if (a->addr.addr32[0] > b->addr.addr32[0])
359 return (1);
360 if (a->addr.addr32[0] < b->addr.addr32[0])
361 return (-1);
362 break;
363#endif /* INET6 */
364 }
365 return (0);
366}
367
a814431a 368u_int32_t
315a7da3 369pf_state_hash(struct pf_state_key *sk)
5950bf01 370{
315a7da3 371 u_int32_t hv = (intptr_t)sk / sizeof(*sk);
5950bf01 372
315a7da3
JL
373 hv ^= crc32(&sk->lan, sizeof(sk->lan));
374 hv ^= crc32(&sk->gwy, sizeof(sk->gwy));
375 hv ^= crc32(&sk->ext, sizeof(sk->ext));
a814431a
MD
376 if (hv == 0) /* disallow 0 */
377 hv = 1;
5950bf01
MD
378 return(hv);
379}
380
70224baa 381static __inline int
315a7da3 382pf_state_compare_lan_ext(struct pf_state_key *a, struct pf_state_key *b)
02742ec6
JS
383{
384 int diff;
385
386 if ((diff = a->proto - b->proto) != 0)
387 return (diff);
388 if ((diff = a->af - b->af) != 0)
389 return (diff);
390 switch (a->af) {
391#ifdef INET
392 case AF_INET:
393 if (a->lan.addr.addr32[0] > b->lan.addr.addr32[0])
394 return (1);
395 if (a->lan.addr.addr32[0] < b->lan.addr.addr32[0])
396 return (-1);
397 if (a->ext.addr.addr32[0] > b->ext.addr.addr32[0])
398 return (1);
399 if (a->ext.addr.addr32[0] < b->ext.addr.addr32[0])
400 return (-1);
401 break;
402#endif /* INET */
403#ifdef INET6
404 case AF_INET6:
405 if (a->lan.addr.addr32[3] > b->lan.addr.addr32[3])
406 return (1);
407 if (a->lan.addr.addr32[3] < b->lan.addr.addr32[3])
408 return (-1);
409 if (a->ext.addr.addr32[3] > b->ext.addr.addr32[3])
410 return (1);
411 if (a->ext.addr.addr32[3] < b->ext.addr.addr32[3])
412 return (-1);
413 if (a->lan.addr.addr32[2] > b->lan.addr.addr32[2])
414 return (1);
415 if (a->lan.addr.addr32[2] < b->lan.addr.addr32[2])
416 return (-1);
417 if (a->ext.addr.addr32[2] > b->ext.addr.addr32[2])
418 return (1);
419 if (a->ext.addr.addr32[2] < b->ext.addr.addr32[2])
420 return (-1);
421 if (a->lan.addr.addr32[1] > b->lan.addr.addr32[1])
422 return (1);
423 if (a->lan.addr.addr32[1] < b->lan.addr.addr32[1])
424 return (-1);
425 if (a->ext.addr.addr32[1] > b->ext.addr.addr32[1])
426 return (1);
427 if (a->ext.addr.addr32[1] < b->ext.addr.addr32[1])
428 return (-1);
429 if (a->lan.addr.addr32[0] > b->lan.addr.addr32[0])
430 return (1);
431 if (a->lan.addr.addr32[0] < b->lan.addr.addr32[0])
432 return (-1);
433 if (a->ext.addr.addr32[0] > b->ext.addr.addr32[0])
434 return (1);
435 if (a->ext.addr.addr32[0] < b->ext.addr.addr32[0])
436 return (-1);
437 break;
438#endif /* INET6 */
439 }
440
441 if ((diff = a->lan.port - b->lan.port) != 0)
442 return (diff);
443 if ((diff = a->ext.port - b->ext.port) != 0)
444 return (diff);
445
446 return (0);
447}
448
70224baa 449static __inline int
315a7da3 450pf_state_compare_ext_gwy(struct pf_state_key *a, struct pf_state_key *b)
02742ec6
JS
451{
452 int diff;
453
454 if ((diff = a->proto - b->proto) != 0)
455 return (diff);
456 if ((diff = a->af - b->af) != 0)
457 return (diff);
458 switch (a->af) {
459#ifdef INET
460 case AF_INET:
461 if (a->ext.addr.addr32[0] > b->ext.addr.addr32[0])
462 return (1);
463 if (a->ext.addr.addr32[0] < b->ext.addr.addr32[0])
464 return (-1);
465 if (a->gwy.addr.addr32[0] > b->gwy.addr.addr32[0])
466 return (1);
467 if (a->gwy.addr.addr32[0] < b->gwy.addr.addr32[0])
468 return (-1);
469 break;
470#endif /* INET */
471#ifdef INET6
472 case AF_INET6:
473 if (a->ext.addr.addr32[3] > b->ext.addr.addr32[3])
474 return (1);
475 if (a->ext.addr.addr32[3] < b->ext.addr.addr32[3])
476 return (-1);
477 if (a->gwy.addr.addr32[3] > b->gwy.addr.addr32[3])
478 return (1);
479 if (a->gwy.addr.addr32[3] < b->gwy.addr.addr32[3])
480 return (-1);
481 if (a->ext.addr.addr32[2] > b->ext.addr.addr32[2])
482 return (1);
483 if (a->ext.addr.addr32[2] < b->ext.addr.addr32[2])
484 return (-1);
485 if (a->gwy.addr.addr32[2] > b->gwy.addr.addr32[2])
486 return (1);
487 if (a->gwy.addr.addr32[2] < b->gwy.addr.addr32[2])
488 return (-1);
489 if (a->ext.addr.addr32[1] > b->ext.addr.addr32[1])
490 return (1);
491 if (a->ext.addr.addr32[1] < b->ext.addr.addr32[1])
492 return (-1);
493 if (a->gwy.addr.addr32[1] > b->gwy.addr.addr32[1])
494 return (1);
495 if (a->gwy.addr.addr32[1] < b->gwy.addr.addr32[1])
496 return (-1);
497 if (a->ext.addr.addr32[0] > b->ext.addr.addr32[0])
498 return (1);
499 if (a->ext.addr.addr32[0] < b->ext.addr.addr32[0])
500 return (-1);
501 if (a->gwy.addr.addr32[0] > b->gwy.addr.addr32[0])
502 return (1);
503 if (a->gwy.addr.addr32[0] < b->gwy.addr.addr32[0])
504 return (-1);
505 break;
506#endif /* INET6 */
507 }
508
509 if ((diff = a->ext.port - b->ext.port) != 0)
510 return (diff);
511 if ((diff = a->gwy.port - b->gwy.port) != 0)
512 return (diff);
513
514 return (0);
515}
516
70224baa 517static __inline int
02742ec6
JS
518pf_state_compare_id(struct pf_state *a, struct pf_state *b)
519{
520 if (a->id > b->id)
521 return (1);
522 if (a->id < b->id)
523 return (-1);
524 if (a->creatorid > b->creatorid)
525 return (1);
526 if (a->creatorid < b->creatorid)
527 return (-1);
528
529 return (0);
530}
531
532#ifdef INET6
533void
534pf_addrcpy(struct pf_addr *dst, struct pf_addr *src, sa_family_t af)
535{
536 switch (af) {
537#ifdef INET
538 case AF_INET:
539 dst->addr32[0] = src->addr32[0];
540 break;
541#endif /* INET */
542 case AF_INET6:
543 dst->addr32[0] = src->addr32[0];
544 dst->addr32[1] = src->addr32[1];
545 dst->addr32[2] = src->addr32[2];
546 dst->addr32[3] = src->addr32[3];
547 break;
548 }
549}
70224baa 550#endif /* INET6 */
02742ec6
JS
551
552struct pf_state *
70224baa 553pf_find_state_byid(struct pf_state_cmp *key)
02742ec6
JS
554{
555 pf_status.fcounters[FCNT_STATE_SEARCH]++;
315a7da3 556
70224baa 557 return (RB_FIND(pf_state_tree_id, &tree_id, (struct pf_state *)key));
02742ec6
JS
558}
559
560struct pf_state *
315a7da3 561pf_find_state(struct pfi_kif *kif, struct pf_state_key_cmp *key, u_int8_t tree)
02742ec6 562{
315a7da3
JL
563 struct pf_state_key *sk;
564 struct pf_state *s;
02742ec6
JS
565
566 pf_status.fcounters[FCNT_STATE_SEARCH]++;
567
568 switch (tree) {
569 case PF_LAN_EXT:
315a7da3
JL
570 sk = RB_FIND(pf_state_tree_lan_ext, &pf_statetbl_lan_ext,
571 (struct pf_state_key *)key);
572 break;
02742ec6 573 case PF_EXT_GWY:
315a7da3
JL
574 sk = RB_FIND(pf_state_tree_ext_gwy, &pf_statetbl_ext_gwy,
575 (struct pf_state_key *)key);
576 break;
02742ec6 577 default:
315a7da3 578 panic("pf_find_state");
02742ec6 579 }
315a7da3
JL
580
581 /* list is sorted, if-bound states before floating ones */
582 if (sk != NULL)
583 TAILQ_FOREACH(s, &sk->states, next)
584 if (s->kif == pfi_all || s->kif == kif)
585 return (s);
586
587 return (NULL);
02742ec6
JS
588}
589
590struct pf_state *
315a7da3 591pf_find_state_all(struct pf_state_key_cmp *key, u_int8_t tree, int *more)
02742ec6 592{
315a7da3
JL
593 struct pf_state_key *sk;
594 struct pf_state *s, *ret = NULL;
02742ec6
JS
595
596 pf_status.fcounters[FCNT_STATE_SEARCH]++;
597
598 switch (tree) {
599 case PF_LAN_EXT:
315a7da3
JL
600 sk = RB_FIND(pf_state_tree_lan_ext,
601 &pf_statetbl_lan_ext, (struct pf_state_key *)key);
602 break;
02742ec6 603 case PF_EXT_GWY:
315a7da3
JL
604 sk = RB_FIND(pf_state_tree_ext_gwy,
605 &pf_statetbl_ext_gwy, (struct pf_state_key *)key);
606 break;
02742ec6
JS
607 default:
608 panic("pf_find_state_all");
609 }
315a7da3
JL
610
611 if (sk != NULL) {
612 ret = TAILQ_FIRST(&sk->states);
613 if (more == NULL)
614 return (ret);
615
616 TAILQ_FOREACH(s, &sk->states, next)
617 (*more)++;
618 }
619
620 return (ret);
02742ec6
JS
621}
622
70224baa
JL
623void
624pf_init_threshold(struct pf_threshold *threshold,
625 u_int32_t limit, u_int32_t seconds)
626{
627 threshold->limit = limit * PF_THRESHOLD_MULT;
628 threshold->seconds = seconds;
629 threshold->count = 0;
630 threshold->last = time_second;
631}
632
633void
634pf_add_threshold(struct pf_threshold *threshold)
635{
636 u_int32_t t = time_second, diff = t - threshold->last;
637
638 if (diff >= threshold->seconds)
639 threshold->count = 0;
640 else
641 threshold->count -= threshold->count * diff /
642 threshold->seconds;
643 threshold->count += PF_THRESHOLD_MULT;
644 threshold->last = t;
645}
646
647int
648pf_check_threshold(struct pf_threshold *threshold)
649{
650 return (threshold->count > threshold->limit);
651}
652
653int
654pf_src_connlimit(struct pf_state **state)
655{
70224baa
JL
656 int bad = 0;
657
658 (*state)->src_node->conn++;
659 (*state)->src.tcp_est = 1;
660 pf_add_threshold(&(*state)->src_node->conn_rate);
661
662 if ((*state)->rule.ptr->max_src_conn &&
663 (*state)->rule.ptr->max_src_conn <
664 (*state)->src_node->conn) {
665 pf_status.lcounters[LCNT_SRCCONN]++;
666 bad++;
667 }
668
669 if ((*state)->rule.ptr->max_src_conn_rate.limit &&
670 pf_check_threshold(&(*state)->src_node->conn_rate)) {
671 pf_status.lcounters[LCNT_SRCCONNRATE]++;
672 bad++;
673 }
674
675 if (!bad)
676 return (0);
677
678 if ((*state)->rule.ptr->overload_tbl) {
679 struct pfr_addr p;
680 u_int32_t killed = 0;
681
682 pf_status.lcounters[LCNT_OVERLOAD_TABLE]++;
683 if (pf_status.debug >= PF_DEBUG_MISC) {
684 kprintf("pf_src_connlimit: blocking address ");
685 pf_print_host(&(*state)->src_node->addr, 0,
315a7da3 686 (*state)->state_key->af);
70224baa
JL
687 }
688
689 bzero(&p, sizeof(p));
315a7da3
JL
690 p.pfra_af = (*state)->state_key->af;
691 switch ((*state)->state_key->af) {
70224baa
JL
692#ifdef INET
693 case AF_INET:
694 p.pfra_net = 32;
695 p.pfra_ip4addr = (*state)->src_node->addr.v4;
696 break;
697#endif /* INET */
698#ifdef INET6
699 case AF_INET6:
700 p.pfra_net = 128;
701 p.pfra_ip6addr = (*state)->src_node->addr.v6;
702 break;
703#endif /* INET6 */
704 }
705
706 pfr_insert_kentry((*state)->rule.ptr->overload_tbl,
707 &p, time_second);
708
709 /* kill existing states if that's required. */
710 if ((*state)->rule.ptr->flush) {
315a7da3
JL
711 struct pf_state_key *sk;
712 struct pf_state *st;
70224baa 713
315a7da3
JL
714 pf_status.lcounters[LCNT_OVERLOAD_FLUSH]++;
715 RB_FOREACH(st, pf_state_tree_id, &tree_id) {
716 sk = st->state_key;
70224baa
JL
717 /*
718 * Kill states from this source. (Only those
719 * from the same rule if PF_FLUSH_GLOBAL is not
720 * set)
721 */
315a7da3
JL
722 if (sk->af ==
723 (*state)->state_key->af &&
724 (((*state)->state_key->direction ==
725 PF_OUT &&
70224baa 726 PF_AEQ(&(*state)->src_node->addr,
315a7da3
JL
727 &sk->lan.addr, sk->af)) ||
728 ((*state)->state_key->direction == PF_IN &&
70224baa 729 PF_AEQ(&(*state)->src_node->addr,
315a7da3 730 &sk->ext.addr, sk->af))) &&
70224baa
JL
731 ((*state)->rule.ptr->flush &
732 PF_FLUSH_GLOBAL ||
315a7da3
JL
733 (*state)->rule.ptr == st->rule.ptr)) {
734 st->timeout = PFTM_PURGE;
735 st->src.state = st->dst.state =
70224baa
JL
736 TCPS_CLOSED;
737 killed++;
738 }
739 }
740 if (pf_status.debug >= PF_DEBUG_MISC)
741 kprintf(", %u states killed", killed);
742 }
743 if (pf_status.debug >= PF_DEBUG_MISC)
744 kprintf("\n");
745 }
746
747 /* kill this state */
748 (*state)->timeout = PFTM_PURGE;
749 (*state)->src.state = (*state)->dst.state = TCPS_CLOSED;
750 return (1);
751}
752
02742ec6
JS
753int
754pf_insert_src_node(struct pf_src_node **sn, struct pf_rule *rule,
755 struct pf_addr *src, sa_family_t af)
756{
757 struct pf_src_node k;
758
759 if (*sn == NULL) {
760 k.af = af;
761 PF_ACPY(&k.addr, src, af);
762 if (rule->rule_flag & PFRULE_RULESRCTRACK ||
763 rule->rpool.opts & PF_POOL_STICKYADDR)
764 k.rule.ptr = rule;
765 else
766 k.rule.ptr = NULL;
767 pf_status.scounters[SCNT_SRC_NODE_SEARCH]++;
768 *sn = RB_FIND(pf_src_tree, &tree_src_tracking, &k);
769 }
770 if (*sn == NULL) {
771 if (!rule->max_src_nodes ||
772 rule->src_nodes < rule->max_src_nodes)
773 (*sn) = pool_get(&pf_src_tree_pl, PR_NOWAIT);
70224baa
JL
774 else
775 pf_status.lcounters[LCNT_SRCNODES]++;
02742ec6
JS
776 if ((*sn) == NULL)
777 return (-1);
778 bzero(*sn, sizeof(struct pf_src_node));
70224baa
JL
779
780 pf_init_threshold(&(*sn)->conn_rate,
781 rule->max_src_conn_rate.limit,
782 rule->max_src_conn_rate.seconds);
783
02742ec6
JS
784 (*sn)->af = af;
785 if (rule->rule_flag & PFRULE_RULESRCTRACK ||
786 rule->rpool.opts & PF_POOL_STICKYADDR)
787 (*sn)->rule.ptr = rule;
788 else
789 (*sn)->rule.ptr = NULL;
790 PF_ACPY(&(*sn)->addr, src, af);
791 if (RB_INSERT(pf_src_tree,
792 &tree_src_tracking, *sn) != NULL) {
793 if (pf_status.debug >= PF_DEBUG_MISC) {
4b1cf444 794 kprintf("pf: src_tree insert failed: ");
02742ec6 795 pf_print_host(&(*sn)->addr, 0, af);
4b1cf444 796 kprintf("\n");
02742ec6
JS
797 }
798 pool_put(&pf_src_tree_pl, *sn);
799 return (-1);
800 }
801 (*sn)->creation = time_second;
802 (*sn)->ruletype = rule->action;
803 if ((*sn)->rule.ptr != NULL)
804 (*sn)->rule.ptr->src_nodes++;
805 pf_status.scounters[SCNT_SRC_NODE_INSERT]++;
806 pf_status.src_nodes++;
807 } else {
808 if (rule->max_src_states &&
70224baa
JL
809 (*sn)->states >= rule->max_src_states) {
810 pf_status.lcounters[LCNT_SRCSTATES]++;
02742ec6 811 return (-1);
70224baa 812 }
02742ec6
JS
813 }
814 return (0);
815}
816
315a7da3
JL
817void
818pf_stateins_err(const char *tree, struct pf_state *s, struct pfi_kif *kif)
819{
820 struct pf_state_key *sk = s->state_key;
821
822 if (pf_status.debug >= PF_DEBUG_MISC) {
823 kprintf("pf: state insert failed: %s %s", tree, kif->pfik_name);
824 kprintf(" lan: ");
825 pf_print_host(&sk->lan.addr, sk->lan.port,
826 sk->af);
827 kprintf(" gwy: ");
828 pf_print_host(&sk->gwy.addr, sk->gwy.port,
829 sk->af);
830 kprintf(" ext: ");
831 pf_print_host(&sk->ext.addr, sk->ext.port,
832 sk->af);
833 if (s->sync_flags & PFSTATE_FROMSYNC)
834 kprintf(" (from sync)");
835 kprintf("\n");
836 }
837}
838
02742ec6 839int
315a7da3 840pf_insert_state(struct pfi_kif *kif, struct pf_state *s)
02742ec6 841{
315a7da3
JL
842 struct pf_state_key *cur;
843 struct pf_state *sp;
844
845 KKASSERT(s->state_key != NULL);
846 s->kif = kif;
847
848 if ((cur = RB_INSERT(pf_state_tree_lan_ext, &pf_statetbl_lan_ext,
849 s->state_key)) != NULL) {
850 /* key exists. check for same kif, if none, add to key */
851 TAILQ_FOREACH(sp, &cur->states, next)
852 if (sp->kif == kif) { /* collision! */
853 pf_stateins_err("tree_lan_ext", s, kif);
05ac5751
JL
854 pf_detach_state(s,
855 PF_DT_SKIP_LANEXT|PF_DT_SKIP_EXTGWY);
315a7da3
JL
856 return (-1);
857 }
858 pf_detach_state(s, PF_DT_SKIP_LANEXT|PF_DT_SKIP_EXTGWY);
859 pf_attach_state(cur, s, kif == pfi_all ? 1 : 0);
02742ec6
JS
860 }
861
315a7da3
JL
862 /* if cur != NULL, we already found a state key and attached to it */
863 if (cur == NULL && (cur = RB_INSERT(pf_state_tree_ext_gwy,
864 &pf_statetbl_ext_gwy, s->state_key)) != NULL) {
865 /* must not happen. we must have found the sk above! */
866 pf_stateins_err("tree_ext_gwy", s, kif);
867 pf_detach_state(s, PF_DT_SKIP_EXTGWY);
02742ec6
JS
868 return (-1);
869 }
870
315a7da3
JL
871 if (s->id == 0 && s->creatorid == 0) {
872 s->id = htobe64(pf_status.stateid++);
873 s->creatorid = pf_status.hostid;
02742ec6 874 }
315a7da3 875 if (RB_INSERT(pf_state_tree_id, &tree_id, s) != NULL) {
02742ec6 876 if (pf_status.debug >= PF_DEBUG_MISC) {
4b1cf444 877 kprintf("pf: state insert failed: "
315a7da3
JL
878 "id: %016llx creatorid: %08x",
879 be64toh(s->id), ntohl(s->creatorid));
880 if (s->sync_flags & PFSTATE_FROMSYNC)
4b1cf444
SW
881 kprintf(" (from sync)");
882 kprintf("\n");
02742ec6 883 }
315a7da3 884 pf_detach_state(s, 0);
02742ec6
JS
885 return (-1);
886 }
315a7da3 887 TAILQ_INSERT_TAIL(&state_list, s, entry_list);
02742ec6
JS
888 pf_status.fcounters[FCNT_STATE_INSERT]++;
889 pf_status.states++;
70224baa 890 pfi_kif_ref(kif, PFI_KIF_REF_STATE);
02742ec6 891#if NPFSYNC
315a7da3 892 pfsync_insert_state(s);
02742ec6
JS
893#endif
894 return (0);
895}
896
897void
70224baa 898pf_purge_thread(void *v)
02742ec6 899{
70224baa
JL
900 int nloops = 0;
901 int locked = 0;
902
cd8ab232 903 get_mplock();
70224baa
JL
904 for (;;) {
905 tsleep(pf_purge_thread, PWAIT, "pftm", 1 * hz);
906
907 lockmgr(&pf_consistency_lock, LK_EXCLUSIVE);
908
909 if (pf_end_threads) {
910 pf_purge_expired_states(pf_status.states, 1);
911 pf_purge_expired_fragments();
912 pf_purge_expired_src_nodes(1);
913 pf_end_threads++;
914
915 lockmgr(&pf_consistency_lock, LK_RELEASE);
916 wakeup(pf_purge_thread);
917 kthread_exit();
918 }
919 crit_enter();
920
921 /* process a fraction of the state table every second */
922 if(!pf_purge_expired_states(1 + (pf_status.states
923 / pf_default_rule.timeout[PFTM_INTERVAL]), 0)) {
02742ec6 924
70224baa
JL
925 pf_purge_expired_states(1 + (pf_status.states
926 / pf_default_rule.timeout[PFTM_INTERVAL]), 1);
927 }
02742ec6 928
70224baa
JL
929 /* purge other expired types every PFTM_INTERVAL seconds */
930 if (++nloops >= pf_default_rule.timeout[PFTM_INTERVAL]) {
931 pf_purge_expired_fragments();
932 if (!pf_purge_expired_src_nodes(locked)) {
933 pf_purge_expired_src_nodes(1);
934 }
935 nloops = 0;
936 }
937 crit_exit();
938 lockmgr(&pf_consistency_lock, LK_RELEASE);
939 }
cd8ab232 940 rel_mplock();
02742ec6
JS
941}
942
943u_int32_t
944pf_state_expires(const struct pf_state *state)
945{
946 u_int32_t timeout;
947 u_int32_t start;
948 u_int32_t end;
949 u_int32_t states;
950
951 /* handle all PFTM_* > PFTM_MAX here */
952 if (state->timeout == PFTM_PURGE)
953 return (time_second);
954 if (state->timeout == PFTM_UNTIL_PACKET)
955 return (0);
70224baa
JL
956 KKASSERT(state->timeout != PFTM_UNLINKED);
957 KASSERT((state->timeout < PFTM_MAX),
958 ("pf_state_expires: timeout > PFTM_MAX"));
02742ec6
JS
959 timeout = state->rule.ptr->timeout[state->timeout];
960 if (!timeout)
961 timeout = pf_default_rule.timeout[state->timeout];
962 start = state->rule.ptr->timeout[PFTM_ADAPTIVE_START];
963 if (start) {
964 end = state->rule.ptr->timeout[PFTM_ADAPTIVE_END];
965 states = state->rule.ptr->states;
966 } else {
967 start = pf_default_rule.timeout[PFTM_ADAPTIVE_START];
968 end = pf_default_rule.timeout[PFTM_ADAPTIVE_END];
969 states = pf_status.states;
970 }
971 if (end && states > start && start < end) {
972 if (states < end)
973 return (state->expire + timeout * (end - states) /
974 (end - start));
975 else
976 return (time_second);
977 }
978 return (state->expire + timeout);
979}
980
70224baa
JL
981int
982pf_purge_expired_src_nodes(int waslocked)
02742ec6
JS
983{
984 struct pf_src_node *cur, *next;
70224baa 985 int locked = waslocked;
02742ec6
JS
986
987 for (cur = RB_MIN(pf_src_tree, &tree_src_tracking); cur; cur = next) {
988 next = RB_NEXT(pf_src_tree, &tree_src_tracking, cur);
989
990 if (cur->states <= 0 && cur->expire <= time_second) {
70224baa
JL
991 if (! locked) {
992 lockmgr(&pf_consistency_lock, LK_EXCLUSIVE);
993 next = RB_NEXT(pf_src_tree,
994 &tree_src_tracking, cur);
995 locked = 1;
996 }
02742ec6
JS
997 if (cur->rule.ptr != NULL) {
998 cur->rule.ptr->src_nodes--;
999 if (cur->rule.ptr->states <= 0 &&
1000 cur->rule.ptr->max_src_nodes <= 0)
1001 pf_rm_rule(NULL, cur->rule.ptr);
1002 }
1003 RB_REMOVE(pf_src_tree, &tree_src_tracking, cur);
1004 pf_status.scounters[SCNT_SRC_NODE_REMOVALS]++;
1005 pf_status.src_nodes--;
1006 pool_put(&pf_src_tree_pl, cur);
1007 }
1008 }
70224baa
JL
1009
1010 if (locked && !waslocked)
1011 lockmgr(&pf_consistency_lock, LK_RELEASE);
1012 return(1);
02742ec6
JS
1013}
1014
1015void
1016pf_src_tree_remove_state(struct pf_state *s)
1017{
1018 u_int32_t timeout;
1019
1020 if (s->src_node != NULL) {
05ac5751
JL
1021 if (s->src.tcp_est)
1022 --s->src_node->conn;
02742ec6
JS
1023 if (--s->src_node->states <= 0) {
1024 timeout = s->rule.ptr->timeout[PFTM_SRC_NODE];
1025 if (!timeout)
1026 timeout =
1027 pf_default_rule.timeout[PFTM_SRC_NODE];
1028 s->src_node->expire = time_second + timeout;
1029 }
1030 }
1031 if (s->nat_src_node != s->src_node && s->nat_src_node != NULL) {
1032 if (--s->nat_src_node->states <= 0) {
1033 timeout = s->rule.ptr->timeout[PFTM_SRC_NODE];
1034 if (!timeout)
1035 timeout =
1036 pf_default_rule.timeout[PFTM_SRC_NODE];
1037 s->nat_src_node->expire = time_second + timeout;
1038 }
1039 }
1040 s->src_node = s->nat_src_node = NULL;
1041}
1042
70224baa
JL
1043/* callers should be at crit_enter() */
1044void
1045pf_unlink_state(struct pf_state *cur)
02742ec6 1046{
70224baa 1047 if (cur->src.state == PF_TCPS_PROXY_DST) {
315a7da3
JL
1048 pf_send_tcp(cur->rule.ptr, cur->state_key->af,
1049 &cur->state_key->ext.addr, &cur->state_key->lan.addr,
1050 cur->state_key->ext.port, cur->state_key->lan.port,
70224baa
JL
1051 cur->src.seqhi, cur->src.seqlo + 1,
1052 TH_RST|TH_ACK, 0, 0, 0, 1, cur->tag, NULL, NULL);
1053 }
70224baa 1054 RB_REMOVE(pf_state_tree_id, &tree_id, cur);
02742ec6 1055#if NPFSYNC
70224baa 1056 if (cur->creatorid == pf_status.hostid)
f0ea6854 1057 pfsync_delete_state(cur);
02742ec6 1058#endif
70224baa
JL
1059 cur->timeout = PFTM_UNLINKED;
1060 pf_src_tree_remove_state(cur);
315a7da3 1061 pf_detach_state(cur, 0);
02742ec6
JS
1062}
1063
70224baa
JL
1064/* callers should be at crit_enter() and hold the
1065 * write_lock on pf_consistency_lock */
f0ea6854 1066void
70224baa 1067pf_free_state(struct pf_state *cur)
f0ea6854 1068{
70224baa
JL
1069#if NPFSYNC
1070 if (pfsyncif != NULL &&
1071 (pfsyncif->sc_bulk_send_next == cur ||
1072 pfsyncif->sc_bulk_terminator == cur))
1073 return;
1074#endif
1075 KKASSERT(cur->timeout == PFTM_UNLINKED);
1076 if (--cur->rule.ptr->states <= 0 &&
1077 cur->rule.ptr->src_nodes <= 0)
1078 pf_rm_rule(NULL, cur->rule.ptr);
1079 if (cur->nat_rule.ptr != NULL)
1080 if (--cur->nat_rule.ptr->states <= 0 &&
1081 cur->nat_rule.ptr->src_nodes <= 0)
1082 pf_rm_rule(NULL, cur->nat_rule.ptr);
1083 if (cur->anchor.ptr != NULL)
1084 if (--cur->anchor.ptr->states <= 0)
1085 pf_rm_rule(NULL, cur->anchor.ptr);
1086 pf_normalize_tcp_cleanup(cur);
315a7da3
JL
1087 pfi_kif_unref(cur->kif, PFI_KIF_REF_STATE);
1088 TAILQ_REMOVE(&state_list, cur, entry_list);
70224baa
JL
1089 if (cur->tag)
1090 pf_tag_unref(cur->tag);
1091 pool_put(&pf_state_pl, cur);
1092 pf_status.fcounters[FCNT_STATE_REMOVALS]++;
1093 pf_status.states--;
f0ea6854
MD
1094}
1095
70224baa
JL
1096int
1097pf_purge_expired_states(u_int32_t maxcheck, int waslocked)
1098{
1099 static struct pf_state *cur = NULL;
1100 struct pf_state *next;
1101 int locked = waslocked;
1102
1103 while (maxcheck--) {
1104 /* wrap to start of list when we hit the end */
1105 if (cur == NULL) {
1106 cur = TAILQ_FIRST(&state_list);
1107 if (cur == NULL)
1108 break; /* list empty */
1109 }
1110
1111 /* get next state, as cur may get deleted */
315a7da3 1112 next = TAILQ_NEXT(cur, entry_list);
70224baa
JL
1113
1114 if (cur->timeout == PFTM_UNLINKED) {
1115 /* free unlinked state */
1116 if (! locked) {
1117 lockmgr(&pf_consistency_lock, LK_EXCLUSIVE);
1118 locked = 1;
1119 }
1120 pf_free_state(cur);
1121 } else if (pf_state_expires(cur) <= time_second) {
1122 /* unlink and free expired state */
1123 pf_unlink_state(cur);
1124 if (! locked) {
1125 if (!lockmgr(&pf_consistency_lock, LK_EXCLUSIVE))
1126 return (0);
1127 locked = 1;
1128 }
1129 pf_free_state(cur);
1130 }
1131 cur = next;
1132 }
1133
1134 if (locked)
1135 lockmgr(&pf_consistency_lock, LK_RELEASE);
1136 return (1);
1137}
f0ea6854 1138
02742ec6
JS
1139int
1140pf_tbladdr_setup(struct pf_ruleset *rs, struct pf_addr_wrap *aw)
1141{
1142 if (aw->type != PF_ADDR_TABLE)
1143 return (0);
1144 if ((aw->p.tbl = pfr_attach_table(rs, aw->v.tblname)) == NULL)
1145 return (1);
1146 return (0);
1147}
1148
1149void
1150pf_tbladdr_remove(struct pf_addr_wrap *aw)
1151{
1152 if (aw->type != PF_ADDR_TABLE || aw->p.tbl == NULL)
1153 return;
1154 pfr_detach_table(aw->p.tbl);
1155 aw->p.tbl = NULL;
1156}
1157
1158void
1159pf_tbladdr_copyout(struct pf_addr_wrap *aw)
1160{
1161 struct pfr_ktable *kt = aw->p.tbl;
1162
1163 if (aw->type != PF_ADDR_TABLE || kt == NULL)
1164 return;
1165 if (!(kt->pfrkt_flags & PFR_TFLAG_ACTIVE) && kt->pfrkt_root != NULL)
1166 kt = kt->pfrkt_root;
1167 aw->p.tbl = NULL;
1168 aw->p.tblcnt = (kt->pfrkt_flags & PFR_TFLAG_ACTIVE) ?
1169 kt->pfrkt_cnt : -1;
1170}
1171
1172void
1173pf_print_host(struct pf_addr *addr, u_int16_t p, sa_family_t af)
1174{
1175 switch (af) {
1176#ifdef INET
1177 case AF_INET: {
1178 u_int32_t a = ntohl(addr->addr32[0]);
4b1cf444 1179 kprintf("%u.%u.%u.%u", (a>>24)&255, (a>>16)&255,
02742ec6
JS
1180 (a>>8)&255, a&255);
1181 if (p) {
1182 p = ntohs(p);
4b1cf444 1183 kprintf(":%u", p);
02742ec6
JS
1184 }
1185 break;
1186 }
1187#endif /* INET */
1188#ifdef INET6
1189 case AF_INET6: {
1190 u_int16_t b;
1191 u_int8_t i, curstart = 255, curend = 0,
1192 maxstart = 0, maxend = 0;
1193 for (i = 0; i < 8; i++) {
1194 if (!addr->addr16[i]) {
1195 if (curstart == 255)
1196 curstart = i;
1197 else
1198 curend = i;
1199 } else {
1200 if (curstart) {
1201 if ((curend - curstart) >
1202 (maxend - maxstart)) {
1203 maxstart = curstart;
1204 maxend = curend;
1205 curstart = 255;
1206 }
1207 }
1208 }
1209 }
1210 for (i = 0; i < 8; i++) {
1211 if (i >= maxstart && i <= maxend) {
1212 if (maxend != 7) {
1213 if (i == maxstart)
4b1cf444 1214 kprintf(":");
02742ec6
JS
1215 } else {
1216 if (i == maxend)
4b1cf444 1217 kprintf(":");
02742ec6
JS
1218 }
1219 } else {
1220 b = ntohs(addr->addr16[i]);
4b1cf444 1221 kprintf("%x", b);
02742ec6 1222 if (i < 7)
4b1cf444 1223 kprintf(":");
02742ec6
JS
1224 }
1225 }
1226 if (p) {
1227 p = ntohs(p);
4b1cf444 1228 kprintf("[%u]", p);
02742ec6
JS
1229 }
1230 break;
1231 }
1232#endif /* INET6 */
1233 }
1234}
1235
1236void
1237pf_print_state(struct pf_state *s)
1238{
315a7da3
JL
1239 struct pf_state_key *sk = s->state_key;
1240 switch (sk->proto) {
02742ec6 1241 case IPPROTO_TCP:
4b1cf444 1242 kprintf("TCP ");
02742ec6
JS
1243 break;
1244 case IPPROTO_UDP:
4b1cf444 1245 kprintf("UDP ");
02742ec6
JS
1246 break;
1247 case IPPROTO_ICMP:
4b1cf444 1248 kprintf("ICMP ");
02742ec6
JS
1249 break;
1250 case IPPROTO_ICMPV6:
4b1cf444 1251 kprintf("ICMPV6 ");
02742ec6
JS
1252 break;
1253 default:
315a7da3 1254 kprintf("%u ", sk->proto);
02742ec6
JS
1255 break;
1256 }
315a7da3 1257 pf_print_host(&sk->lan.addr, sk->lan.port, sk->af);
4b1cf444 1258 kprintf(" ");
315a7da3 1259 pf_print_host(&sk->gwy.addr, sk->gwy.port, sk->af);
4b1cf444 1260 kprintf(" ");
315a7da3 1261 pf_print_host(&sk->ext.addr, sk->ext.port, sk->af);
4b1cf444 1262 kprintf(" [lo=%u high=%u win=%u modulator=%u", s->src.seqlo,
02742ec6
JS
1263 s->src.seqhi, s->src.max_win, s->src.seqdiff);
1264 if (s->src.wscale && s->dst.wscale)
4b1cf444
SW
1265 kprintf(" wscale=%u", s->src.wscale & PF_WSCALE_MASK);
1266 kprintf("]");
1267 kprintf(" [lo=%u high=%u win=%u modulator=%u", s->dst.seqlo,
02742ec6
JS
1268 s->dst.seqhi, s->dst.max_win, s->dst.seqdiff);
1269 if (s->src.wscale && s->dst.wscale)
4b1cf444
SW
1270 kprintf(" wscale=%u", s->dst.wscale & PF_WSCALE_MASK);
1271 kprintf("]");
1272 kprintf(" %u:%u", s->src.state, s->dst.state);
02742ec6
JS
1273}
1274
1275void
1276pf_print_flags(u_int8_t f)
1277{
1278 if (f)
4b1cf444 1279 kprintf(" ");
02742ec6 1280 if (f & TH_FIN)
4b1cf444 1281 kprintf("F");
02742ec6 1282 if (f & TH_SYN)
4b1cf444 1283 kprintf("S");
02742ec6 1284 if (f & TH_RST)
4b1cf444 1285 kprintf("R");
02742ec6 1286 if (f & TH_PUSH)
4b1cf444 1287 kprintf("P");
02742ec6 1288 if (f & TH_ACK)
4b1cf444 1289 kprintf("A");
02742ec6 1290 if (f & TH_URG)
4b1cf444 1291 kprintf("U");
02742ec6 1292 if (f & TH_ECE)
4b1cf444 1293 kprintf("E");
02742ec6 1294 if (f & TH_CWR)
4b1cf444 1295 kprintf("W");
02742ec6
JS
1296}
1297
1298#define PF_SET_SKIP_STEPS(i) \
1299 do { \
1300 while (head[i] != cur) { \
1301 head[i]->skip[i].ptr = cur; \
1302 head[i] = TAILQ_NEXT(head[i], entries); \
1303 } \
1304 } while (0)
1305
1306void
1307pf_calc_skip_steps(struct pf_rulequeue *rules)
1308{
1309 struct pf_rule *cur, *prev, *head[PF_SKIP_COUNT];
1310 int i;
1311
1312 cur = TAILQ_FIRST(rules);
1313 prev = cur;
1314 for (i = 0; i < PF_SKIP_COUNT; ++i)
1315 head[i] = cur;
1316 while (cur != NULL) {
1317
1318 if (cur->kif != prev->kif || cur->ifnot != prev->ifnot)
1319 PF_SET_SKIP_STEPS(PF_SKIP_IFP);
1320 if (cur->direction != prev->direction)
1321 PF_SET_SKIP_STEPS(PF_SKIP_DIR);
1322 if (cur->af != prev->af)
1323 PF_SET_SKIP_STEPS(PF_SKIP_AF);
1324 if (cur->proto != prev->proto)
1325 PF_SET_SKIP_STEPS(PF_SKIP_PROTO);
70224baa 1326 if (cur->src.neg != prev->src.neg ||
02742ec6
JS
1327 pf_addr_wrap_neq(&cur->src.addr, &prev->src.addr))
1328 PF_SET_SKIP_STEPS(PF_SKIP_SRC_ADDR);
1329 if (cur->src.port[0] != prev->src.port[0] ||
1330 cur->src.port[1] != prev->src.port[1] ||
1331 cur->src.port_op != prev->src.port_op)
1332 PF_SET_SKIP_STEPS(PF_SKIP_SRC_PORT);
70224baa 1333 if (cur->dst.neg != prev->dst.neg ||
02742ec6
JS
1334 pf_addr_wrap_neq(&cur->dst.addr, &prev->dst.addr))
1335 PF_SET_SKIP_STEPS(PF_SKIP_DST_ADDR);
1336 if (cur->dst.port[0] != prev->dst.port[0] ||
1337 cur->dst.port[1] != prev->dst.port[1] ||
1338 cur->dst.port_op != prev->dst.port_op)
1339 PF_SET_SKIP_STEPS(PF_SKIP_DST_PORT);
1340
1341 prev = cur;
1342 cur = TAILQ_NEXT(cur, entries);
1343 }
1344 for (i = 0; i < PF_SKIP_COUNT; ++i)
1345 PF_SET_SKIP_STEPS(i);
1346}
1347
1348int
1349pf_addr_wrap_neq(struct pf_addr_wrap *aw1, struct pf_addr_wrap *aw2)
1350{
1351 if (aw1->type != aw2->type)
1352 return (1);
1353 switch (aw1->type) {
1354 case PF_ADDR_ADDRMASK:
1355 if (PF_ANEQ(&aw1->v.a.addr, &aw2->v.a.addr, 0))
1356 return (1);
1357 if (PF_ANEQ(&aw1->v.a.mask, &aw2->v.a.mask, 0))
1358 return (1);
1359 return (0);
1360 case PF_ADDR_DYNIFTL:
1361 return (aw1->p.dyn->pfid_kt != aw2->p.dyn->pfid_kt);
1362 case PF_ADDR_NOROUTE:
70224baa 1363 case PF_ADDR_URPFFAILED:
02742ec6
JS
1364 return (0);
1365 case PF_ADDR_TABLE:
1366 return (aw1->p.tbl != aw2->p.tbl);
70224baa
JL
1367 case PF_ADDR_RTLABEL:
1368 return (aw1->v.rtlabel != aw2->v.rtlabel);
02742ec6 1369 default:
4b1cf444 1370 kprintf("invalid address type: %d\n", aw1->type);
02742ec6
JS
1371 return (1);
1372 }
1373}
1374
02742ec6
JS
1375u_int16_t
1376pf_cksum_fixup(u_int16_t cksum, u_int16_t old, u_int16_t new, u_int8_t udp)
1377{
1378 u_int32_t l;
1379
1380 if (udp && !cksum)
1381 return (0x0000);
1382 l = cksum + old - new;
1383 l = (l >> 16) + (l & 65535);
1384 l = l & 65535;
1385 if (udp && !l)
1386 return (0xFFFF);
1387 return (l);
1388}
1389
1390void
1391pf_change_ap(struct pf_addr *a, u_int16_t *p, u_int16_t *ic, u_int16_t *pc,
1392 struct pf_addr *an, u_int16_t pn, u_int8_t u, sa_family_t af)
1393{
1394 struct pf_addr ao;
1395 u_int16_t po = *p;
1396
1397 PF_ACPY(&ao, a, af);
1398 PF_ACPY(a, an, af);
1399
1400 *p = pn;
1401
1402 switch (af) {
1403#ifdef INET
1404 case AF_INET:
1405 *ic = pf_cksum_fixup(pf_cksum_fixup(*ic,
1406 ao.addr16[0], an->addr16[0], 0),
1407 ao.addr16[1], an->addr16[1], 0);
1408 *p = pn;
1409 *pc = pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(*pc,
1410 ao.addr16[0], an->addr16[0], u),
1411 ao.addr16[1], an->addr16[1], u),
1412 po, pn, u);
1413 break;
1414#endif /* INET */
1415#ifdef INET6
1416 case AF_INET6:
1417 *pc = pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
1418 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
1419 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(*pc,
1420 ao.addr16[0], an->addr16[0], u),
1421 ao.addr16[1], an->addr16[1], u),
1422 ao.addr16[2], an->addr16[2], u),
1423 ao.addr16[3], an->addr16[3], u),
1424 ao.addr16[4], an->addr16[4], u),
1425 ao.addr16[5], an->addr16[5], u),
1426 ao.addr16[6], an->addr16[6], u),
1427 ao.addr16[7], an->addr16[7], u),
1428 po, pn, u);
1429 break;
1430#endif /* INET6 */
1431 }
1432}
1433
1434
1435/* Changes a u_int32_t. Uses a void * so there are no align restrictions */
1436void
1437pf_change_a(void *a, u_int16_t *c, u_int32_t an, u_int8_t u)
1438{
1439 u_int32_t ao;
1440
1441 memcpy(&ao, a, sizeof(ao));
1442 memcpy(a, &an, sizeof(u_int32_t));
1443 *c = pf_cksum_fixup(pf_cksum_fixup(*c, ao / 65536, an / 65536, u),
1444 ao % 65536, an % 65536, u);
1445}
1446
1447#ifdef INET6
1448void
1449pf_change_a6(struct pf_addr *a, u_int16_t *c, struct pf_addr *an, u_int8_t u)
1450{
1451 struct pf_addr ao;
1452
1453 PF_ACPY(&ao, a, AF_INET6);
1454 PF_ACPY(a, an, AF_INET6);
1455
1456 *c = pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
1457 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
1458 pf_cksum_fixup(pf_cksum_fixup(*c,
1459 ao.addr16[0], an->addr16[0], u),
1460 ao.addr16[1], an->addr16[1], u),
1461 ao.addr16[2], an->addr16[2], u),
1462 ao.addr16[3], an->addr16[3], u),
1463 ao.addr16[4], an->addr16[4], u),
1464 ao.addr16[5], an->addr16[5], u),
1465 ao.addr16[6], an->addr16[6], u),
1466 ao.addr16[7], an->addr16[7], u);
1467}
1468#endif /* INET6 */
1469
1470void
1471pf_change_icmp(struct pf_addr *ia, u_int16_t *ip, struct pf_addr *oa,
1472 struct pf_addr *na, u_int16_t np, u_int16_t *pc, u_int16_t *h2c,
1473 u_int16_t *ic, u_int16_t *hc, u_int8_t u, sa_family_t af)
1474{
1475 struct pf_addr oia, ooa;
1476
1477 PF_ACPY(&oia, ia, af);
1478 PF_ACPY(&ooa, oa, af);
1479
1480 /* Change inner protocol port, fix inner protocol checksum. */
1481 if (ip != NULL) {
1482 u_int16_t oip = *ip;
1483 u_int32_t opc = 0;
1484
1485 if (pc != NULL)
1486 opc = *pc;
1487 *ip = np;
1488 if (pc != NULL)
1489 *pc = pf_cksum_fixup(*pc, oip, *ip, u);
1490 *ic = pf_cksum_fixup(*ic, oip, *ip, 0);
1491 if (pc != NULL)
1492 *ic = pf_cksum_fixup(*ic, opc, *pc, 0);
1493 }
1494 /* Change inner ip address, fix inner ip and icmp checksums. */
1495 PF_ACPY(ia, na, af);
1496 switch (af) {
1497#ifdef INET
1498 case AF_INET: {
1499 u_int32_t oh2c = *h2c;
1500
1501 *h2c = pf_cksum_fixup(pf_cksum_fixup(*h2c,
1502 oia.addr16[0], ia->addr16[0], 0),
1503 oia.addr16[1], ia->addr16[1], 0);
1504 *ic = pf_cksum_fixup(pf_cksum_fixup(*ic,
1505 oia.addr16[0], ia->addr16[0], 0),
1506 oia.addr16[1], ia->addr16[1], 0);
1507 *ic = pf_cksum_fixup(*ic, oh2c, *h2c, 0);
1508 break;
1509 }
1510#endif /* INET */
1511#ifdef INET6
1512 case AF_INET6:
1513 *ic = pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
1514 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
1515 pf_cksum_fixup(pf_cksum_fixup(*ic,
1516 oia.addr16[0], ia->addr16[0], u),
1517 oia.addr16[1], ia->addr16[1], u),
1518 oia.addr16[2], ia->addr16[2], u),
1519 oia.addr16[3], ia->addr16[3], u),
1520 oia.addr16[4], ia->addr16[4], u),
1521 oia.addr16[5], ia->addr16[5], u),
1522 oia.addr16[6], ia->addr16[6], u),
1523 oia.addr16[7], ia->addr16[7], u);
1524 break;
1525#endif /* INET6 */
1526 }
1527 /* Change outer ip address, fix outer ip or icmpv6 checksum. */
1528 PF_ACPY(oa, na, af);
1529 switch (af) {
1530#ifdef INET
1531 case AF_INET:
1532 *hc = pf_cksum_fixup(pf_cksum_fixup(*hc,
1533 ooa.addr16[0], oa->addr16[0], 0),
1534 ooa.addr16[1], oa->addr16[1], 0);
1535 break;
1536#endif /* INET */
1537#ifdef INET6
1538 case AF_INET6:
1539 *ic = pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
1540 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
1541 pf_cksum_fixup(pf_cksum_fixup(*ic,
1542 ooa.addr16[0], oa->addr16[0], u),
1543 ooa.addr16[1], oa->addr16[1], u),
1544 ooa.addr16[2], oa->addr16[2], u),
1545 ooa.addr16[3], oa->addr16[3], u),
1546 ooa.addr16[4], oa->addr16[4], u),
1547 ooa.addr16[5], oa->addr16[5], u),
1548 ooa.addr16[6], oa->addr16[6], u),
1549 ooa.addr16[7], oa->addr16[7], u);
1550 break;
1551#endif /* INET6 */
1552 }
1553}
1554
70224baa
JL
1555
1556/*
1557 * Need to modulate the sequence numbers in the TCP SACK option
1558 * (credits to Krzysztof Pfaff for report and patch)
1559 */
1560int
1561pf_modulate_sack(struct mbuf *m, int off, struct pf_pdesc *pd,
1562 struct tcphdr *th, struct pf_state_peer *dst)
1563{
1564 int hlen = (th->th_off << 2) - sizeof(*th), thoptlen = hlen;
1565 u_int8_t opts[TCP_MAXOLEN], *opt = opts;
1566 int copyback = 0, i, olen;
1567 struct raw_sackblock sack;
1568
1569#define TCPOLEN_SACKLEN (TCPOLEN_SACK + 2)
1570 if (hlen < TCPOLEN_SACKLEN ||
1571 !pf_pull_hdr(m, off + sizeof(*th), opts, hlen, NULL, NULL, pd->af))
1572 return 0;
1573
1574 while (hlen >= TCPOLEN_SACKLEN) {
1575 olen = opt[1];
1576 switch (*opt) {
1577 case TCPOPT_EOL: /* FALLTHROUGH */
1578 case TCPOPT_NOP:
1579 opt++;
1580 hlen--;
1581 break;
1582 case TCPOPT_SACK:
1583 if (olen > hlen)
1584 olen = hlen;
1585 if (olen >= TCPOLEN_SACKLEN) {
1586 for (i = 2; i + TCPOLEN_SACK <= olen;
1587 i += TCPOLEN_SACK) {
1588 memcpy(&sack, &opt[i], sizeof(sack));
1589 pf_change_a(&sack.rblk_start, &th->th_sum,
4fc5aa1c 1590 htonl(ntohl(sack.rblk_start) -
70224baa
JL
1591 dst->seqdiff), 0);
1592 pf_change_a(&sack.rblk_end, &th->th_sum,
4fc5aa1c 1593 htonl(ntohl(sack.rblk_end) -
70224baa
JL
1594 dst->seqdiff), 0);
1595 memcpy(&opt[i], &sack, sizeof(sack));
1596 }
1597 copyback = 1;
1598 }
1599 /* FALLTHROUGH */
1600 default:
1601 if (olen < 2)
1602 olen = 2;
1603 hlen -= olen;
1604 opt += olen;
1605 }
1606 }
1607
1608 if (copyback)
1609 m_copyback(m, off + sizeof(*th), thoptlen, opts);
1610 return (copyback);
1611}
1612
02742ec6
JS
1613void
1614pf_send_tcp(const struct pf_rule *r, sa_family_t af,
1615 const struct pf_addr *saddr, const struct pf_addr *daddr,
1616 u_int16_t sport, u_int16_t dport, u_int32_t seq, u_int32_t ack,
70224baa
JL
1617 u_int8_t flags, u_int16_t win, u_int16_t mss, u_int8_t ttl, int tag,
1618 u_int16_t rtag, struct ether_header *eh, struct ifnet *ifp)
02742ec6
JS
1619{
1620 struct mbuf *m;
1621 int len = 0, tlen;
1622#ifdef INET
1623 struct ip *h = NULL;
1624#endif /* INET */
1625#ifdef INET6
1626 struct ip6_hdr *h6 = NULL;
1627#endif /* INET6 */
1628 struct tcphdr *th = NULL;
70224baa 1629 char *opt;
02742ec6
JS
1630
1631 /* maximum segment size tcp option */
1632 tlen = sizeof(struct tcphdr);
1633 if (mss)
1634 tlen += 4;
1635
1636 switch (af) {
1637#ifdef INET
1638 case AF_INET:
1639 len = sizeof(struct ip) + tlen;
1640 break;
1641#endif /* INET */
1642#ifdef INET6
1643 case AF_INET6:
1644 len = sizeof(struct ip6_hdr) + tlen;
1645 break;
1646#endif /* INET6 */
1647 }
1648
1649 /* create outgoing mbuf */
1650 m = m_gethdr(MB_DONTWAIT, MT_HEADER);
1651 if (m == NULL)
1652 return;
70224baa 1653 if (tag)
315a7da3
JL
1654 m->m_pkthdr.pf.flags |= PF_TAG_GENERATED;
1655 m->m_pkthdr.pf.tag = rtag;
70224baa
JL
1656
1657 if (r != NULL && r->rtableid >= 0)
02dd99a9 1658 m->m_pkthdr.pf.rtableid = r->rtableid;
70224baa 1659
02742ec6
JS
1660#ifdef ALTQ
1661 if (r != NULL && r->qid) {
315a7da3
JL
1662 m->m_pkthdr.fw_flags |= PF_MBUF_STRUCTURE;
1663 m->m_pkthdr.pf.qid = r->qid;
1664 m->m_pkthdr.pf.ecn_af = af;
1665 m->m_pkthdr.pf.hdr = mtod(m, struct ip *);
02742ec6 1666 }
70224baa 1667#endif /* ALTQ */
02742ec6
JS
1668 m->m_data += max_linkhdr;
1669 m->m_pkthdr.len = m->m_len = len;
1670 m->m_pkthdr.rcvif = NULL;
1671 bzero(m->m_data, len);
1672 switch (af) {
1673#ifdef INET
1674 case AF_INET:
1675 h = mtod(m, struct ip *);
1676
1677 /* IP header fields included in the TCP checksum */
1678 h->ip_p = IPPROTO_TCP;
1679 h->ip_len = tlen;
1680 h->ip_src.s_addr = saddr->v4.s_addr;
1681 h->ip_dst.s_addr = daddr->v4.s_addr;
1682
1683 th = (struct tcphdr *)((caddr_t)h + sizeof(struct ip));
1684 break;
1685#endif /* INET */
1686#ifdef INET6
1687 case AF_INET6:
1688 h6 = mtod(m, struct ip6_hdr *);
1689
1690 /* IP header fields included in the TCP checksum */
1691 h6->ip6_nxt = IPPROTO_TCP;
1692 h6->ip6_plen = htons(tlen);
1693 memcpy(&h6->ip6_src, &saddr->v6, sizeof(struct in6_addr));
1694 memcpy(&h6->ip6_dst, &daddr->v6, sizeof(struct in6_addr));
1695
1696 th = (struct tcphdr *)((caddr_t)h6 + sizeof(struct ip6_hdr));
1697 break;
1698#endif /* INET6 */
1699 }
1700
1701 /* TCP header */
1702 th->th_sport = sport;
1703 th->th_dport = dport;
1704 th->th_seq = htonl(seq);
1705 th->th_ack = htonl(ack);
1706 th->th_off = tlen >> 2;
1707 th->th_flags = flags;
1708 th->th_win = htons(win);
1709
1710 if (mss) {
1711 opt = (char *)(th + 1);
1712 opt[0] = TCPOPT_MAXSEG;
1713 opt[1] = 4;
1714 mss = htons(mss);
1715 bcopy((caddr_t)&mss, (caddr_t)(opt + 2), 2);
1716 }
1717
1718 switch (af) {
1719#ifdef INET
1720 case AF_INET:
1721 /* TCP checksum */
1722 th->th_sum = in_cksum(m, len);
1723
1724 /* Finish the IP header */
1725 h->ip_v = 4;
1726 h->ip_hl = sizeof(*h) >> 2;
1727 h->ip_tos = IPTOS_LOWDELAY;
1728 h->ip_len = len;
1729 h->ip_off = path_mtu_discovery ? IP_DF : 0;
1730 h->ip_ttl = ttl ? ttl : ip_defttl;
1731 h->ip_sum = 0;
70224baa
JL
1732 if (eh == NULL) {
1733 ip_output(m, NULL, NULL, 0, NULL, NULL);
1734 } else {
1735 struct route ro;
1736 struct rtentry rt;
1737 struct ether_header *e = (void *)ro.ro_dst.sa_data;
1738
1739 if (ifp == NULL) {
1740 m_freem(m);
1741 return;
1742 }
1743 rt.rt_ifp = ifp;
1744 ro.ro_rt = &rt;
1745 ro.ro_dst.sa_len = sizeof(ro.ro_dst);
1746 ro.ro_dst.sa_family = pseudo_AF_HDRCMPLT;
1747 bcopy(eh->ether_dhost, e->ether_shost, ETHER_ADDR_LEN);
1748 bcopy(eh->ether_shost, e->ether_dhost, ETHER_ADDR_LEN);
1749 e->ether_type = eh->ether_type;
1750 /* XXX_IMPORT: later */
1751 ip_output(m, (void *)NULL, &ro, 0,
1752 (void *)NULL, (void *)NULL);
1753 }
02742ec6
JS
1754 break;
1755#endif /* INET */
1756#ifdef INET6
1757 case AF_INET6:
1758 /* TCP checksum */
1759 th->th_sum = in6_cksum(m, IPPROTO_TCP,
1760 sizeof(struct ip6_hdr), tlen);
1761
1762 h6->ip6_vfc |= IPV6_VERSION;
1763 h6->ip6_hlim = IPV6_DEFHLIM;
1764
1765 ip6_output(m, NULL, NULL, 0, NULL, NULL, NULL);
1766 break;
1767#endif /* INET6 */
1768 }
1769}
1770
1771void
1772pf_send_icmp(struct mbuf *m, u_int8_t type, u_int8_t code, sa_family_t af,
1773 struct pf_rule *r)
1774{
1775 struct mbuf *m0;
1776
70224baa 1777 m0 = m_copy(m, 0, M_COPYALL);
315a7da3 1778 m0->m_pkthdr.pf.flags |= PF_TAG_GENERATED;
70224baa
JL
1779
1780 if (r->rtableid >= 0)
315a7da3 1781 m0->m_pkthdr.pf.rtableid = r->rtableid;
02742ec6
JS
1782
1783#ifdef ALTQ
1784 if (r->qid) {
315a7da3
JL
1785 m->m_pkthdr.fw_flags |= PF_MBUF_STRUCTURE;
1786 m0->m_pkthdr.pf.qid = r->qid;
1787 m0->m_pkthdr.pf.ecn_af = af;
1788 m0->m_pkthdr.pf.hdr = mtod(m0, struct ip *);
02742ec6 1789 }
70224baa 1790#endif /* ALTQ */
02742ec6
JS
1791
1792 switch (af) {
1793#ifdef INET
1794 case AF_INET:
745a4a5d 1795 icmp_error(m0, type, code, 0, 0);
02742ec6
JS
1796 break;
1797#endif /* INET */
1798#ifdef INET6
1799 case AF_INET6:
1800 icmp6_error(m0, type, code, 0);
1801 break;
1802#endif /* INET6 */
1803 }
1804}
1805
1806/*
1807 * Return 1 if the addresses a and b match (with mask m), otherwise return 0.
1808 * If n is 0, they match if they are equal. If n is != 0, they match if they
1809 * are different.
1810 */
1811int
1812pf_match_addr(u_int8_t n, struct pf_addr *a, struct pf_addr *m,
1813 struct pf_addr *b, sa_family_t af)
1814{
1815 int match = 0;
1816
1817 switch (af) {
1818#ifdef INET
1819 case AF_INET:
1820 if ((a->addr32[0] & m->addr32[0]) ==
1821 (b->addr32[0] & m->addr32[0]))
1822 match++;
1823 break;
1824#endif /* INET */
1825#ifdef INET6
1826 case AF_INET6:
1827 if (((a->addr32[0] & m->addr32[0]) ==
1828 (b->addr32[0] & m->addr32[0])) &&
1829 ((a->addr32[1] & m->addr32[1]) ==
1830 (b->addr32[1] & m->addr32[1])) &&
1831 ((a->addr32[2] & m->addr32[2]) ==
1832 (b->addr32[2] & m->addr32[2])) &&
1833 ((a->addr32[3] & m->addr32[3]) ==
1834 (b->addr32[3] & m->addr32[3])))
1835 match++;
1836 break;
1837#endif /* INET6 */
1838 }
1839 if (match) {
1840 if (n)
1841 return (0);
1842 else
1843 return (1);
1844 } else {
1845 if (n)
1846 return (1);
1847 else
1848 return (0);
1849 }
1850}
1851
1852int
1853pf_match(u_int8_t op, u_int32_t a1, u_int32_t a2, u_int32_t p)
1854{
1855 switch (op) {
1856 case PF_OP_IRG:
1857 return ((p > a1) && (p < a2));
1858 case PF_OP_XRG:
1859 return ((p < a1) || (p > a2));
1860 case PF_OP_RRG:
1861 return ((p >= a1) && (p <= a2));
1862 case PF_OP_EQ:
1863 return (p == a1);
1864 case PF_OP_NE:
1865 return (p != a1);
1866 case PF_OP_LT:
1867 return (p < a1);
1868 case PF_OP_LE:
1869 return (p <= a1);
1870 case PF_OP_GT:
1871 return (p > a1);
1872 case PF_OP_GE:
1873 return (p >= a1);
1874 }
1875 return (0); /* never reached */
1876}
1877
1878int
1879pf_match_port(u_int8_t op, u_int16_t a1, u_int16_t a2, u_int16_t p)
1880{
1881 a1 = ntohs(a1);
1882 a2 = ntohs(a2);
1883 p = ntohs(p);
1884 return (pf_match(op, a1, a2, p));
1885}
1886
1887int
1888pf_match_uid(u_int8_t op, uid_t a1, uid_t a2, uid_t u)
1889{
1890 if (u == UID_MAX && op != PF_OP_EQ && op != PF_OP_NE)
1891 return (0);
1892 return (pf_match(op, a1, a2, u));
1893}
1894
1895int
1896pf_match_gid(u_int8_t op, gid_t a1, gid_t a2, gid_t g)
1897{
1898 if (g == GID_MAX && op != PF_OP_EQ && op != PF_OP_NE)
1899 return (0);
1900 return (pf_match(op, a1, a2, g));
1901}
1902
70224baa 1903int
315a7da3 1904pf_match_tag(struct mbuf *m, struct pf_rule *r, int *tag)
70224baa
JL
1905{
1906 if (*tag == -1)
315a7da3 1907 *tag = m->m_pkthdr.pf.tag;
70224baa 1908
02742ec6
JS
1909 return ((!r->match_tag_not && r->match_tag == *tag) ||
1910 (r->match_tag_not && r->match_tag != *tag));
1911}
1912
70224baa 1913int
315a7da3 1914pf_tag_packet(struct mbuf *m, int tag, int rtableid)
02742ec6 1915{
70224baa
JL
1916 if (tag <= 0 && rtableid < 0)
1917 return (0);
1918
70224baa 1919 if (tag > 0)
315a7da3 1920 m->m_pkthdr.pf.tag = tag;
70224baa 1921 if (rtableid >= 0)
315a7da3 1922 m->m_pkthdr.pf.rtableid = rtableid;
02742ec6 1923
70224baa 1924 return (0);
02742ec6
JS
1925}
1926
315a7da3 1927void
70224baa
JL
1928pf_step_into_anchor(int *depth, struct pf_ruleset **rs, int n,
1929 struct pf_rule **r, struct pf_rule **a, int *match)
1930{
1931 struct pf_anchor_stackframe *f;
1932
1933 (*r)->anchor->match = 0;
1934 if (match)
1935 *match = 0;
1936 if (*depth >= sizeof(pf_anchor_stack) /
1937 sizeof(pf_anchor_stack[0])) {
1938 kprintf("pf_step_into_anchor: stack overflow\n");
1939 *r = TAILQ_NEXT(*r, entries);
1940 return;
1941 } else if (*depth == 0 && a != NULL)
1942 *a = *r;
1943 f = pf_anchor_stack + (*depth)++;
1944 f->rs = *rs;
1945 f->r = *r;
1946 if ((*r)->anchor_wildcard) {
1947 f->parent = &(*r)->anchor->children;
1948 if ((f->child = RB_MIN(pf_anchor_node, f->parent)) ==
1949 NULL) {
1950 *r = NULL;
1951 return;
1952 }
1953 *rs = &f->child->ruleset;
1954 } else {
1955 f->parent = NULL;
1956 f->child = NULL;
1957 *rs = &(*r)->anchor->ruleset;
1958 }
1959 *r = TAILQ_FIRST((*rs)->rules[n].active.ptr);
1960}
02742ec6 1961
70224baa
JL
1962int
1963pf_step_out_of_anchor(int *depth, struct pf_ruleset **rs, int n,
1964 struct pf_rule **r, struct pf_rule **a, int *match)
1965{
1966 struct pf_anchor_stackframe *f;
1967 int quick = 0;
1968
1969 do {
1970 if (*depth <= 0)
1971 break;
1972 f = pf_anchor_stack + *depth - 1;
1973 if (f->parent != NULL && f->child != NULL) {
1974 if (f->child->match ||
1975 (match != NULL && *match)) {
1976 f->r->anchor->match = 1;
1977 *match = 0;
1978 }
1979 f->child = RB_NEXT(pf_anchor_node, f->parent, f->child);
1980 if (f->child != NULL) {
1981 *rs = &f->child->ruleset;
1982 *r = TAILQ_FIRST((*rs)->rules[n].active.ptr);
1983 if (*r == NULL)
1984 continue;
1985 else
1986 break;
1987 }
1988 }
1989 (*depth)--;
1990 if (*depth == 0 && a != NULL)
1991 *a = NULL;
1992 *rs = f->rs;
1993 if (f->r->anchor->match || (match != NULL && *match))
1994 quick = f->r->quick;
1995 *r = TAILQ_NEXT(f->r, entries);
1996 } while (*r == NULL);
1997
1998 return (quick);
1999}
02742ec6
JS
2000
2001#ifdef INET6
2002void
2003pf_poolmask(struct pf_addr *naddr, struct pf_addr *raddr,
2004 struct pf_addr *rmask, struct pf_addr *saddr, sa_family_t af)
2005{
2006 switch (af) {
2007#ifdef INET
2008 case AF_INET:
2009 naddr->addr32[0] = (raddr->addr32[0] & rmask->addr32[0]) |
2010 ((rmask->addr32[0] ^ 0xffffffff ) & saddr->addr32[0]);
2011 break;
2012#endif /* INET */
2013 case AF_INET6:
2014 naddr->addr32[0] = (raddr->addr32[0] & rmask->addr32[0]) |
2015 ((rmask->addr32[0] ^ 0xffffffff ) & saddr->addr32[0]);
2016 naddr->addr32[1] = (raddr->addr32[1] & rmask->addr32[1]) |
2017 ((rmask->addr32[1] ^ 0xffffffff ) & saddr->addr32[1]);
2018 naddr->addr32[2] = (raddr->addr32[2] & rmask->addr32[2]) |
2019 ((rmask->addr32[2] ^ 0xffffffff ) & saddr->addr32[2]);
2020 naddr->addr32[3] = (raddr->addr32[3] & rmask->addr32[3]) |
2021 ((rmask->addr32[3] ^ 0xffffffff ) & saddr->addr32[3]);
2022 break;
2023 }
2024}
2025
2026void
2027pf_addr_inc(struct pf_addr *addr, sa_family_t af)
2028{
2029 switch (af) {
2030#ifdef INET
2031 case AF_INET:
2032 addr->addr32[0] = htonl(ntohl(addr->addr32[0]) + 1);
2033 break;
2034#endif /* INET */
2035 case AF_INET6:
2036 if (addr->addr32[3] == 0xffffffff) {
2037 addr->addr32[3] = 0;
2038 if (addr->addr32[2] == 0xffffffff) {
2039 addr->addr32[2] = 0;
2040 if (addr->addr32[1] == 0xffffffff) {
2041 addr->addr32[1] = 0;
2042 addr->addr32[0] =
2043 htonl(ntohl(addr->addr32[0]) + 1);
2044 } else
2045 addr->addr32[1] =
2046 htonl(ntohl(addr->addr32[1]) + 1);
2047 } else
2048 addr->addr32[2] =
2049 htonl(ntohl(addr->addr32[2]) + 1);
2050 } else
2051 addr->addr32[3] =
2052 htonl(ntohl(addr->addr32[3]) + 1);
2053 break;
2054 }
2055}
2056#endif /* INET6 */
2057
2058#define mix(a,b,c) \
2059 do { \
2060 a -= b; a -= c; a ^= (c >> 13); \
2061 b -= c; b -= a; b ^= (a << 8); \
2062 c -= a; c -= b; c ^= (b >> 13); \
2063 a -= b; a -= c; a ^= (c >> 12); \
2064 b -= c; b -= a; b ^= (a << 16); \
2065 c -= a; c -= b; c ^= (b >> 5); \
2066 a -= b; a -= c; a ^= (c >> 3); \
2067 b -= c; b -= a; b ^= (a << 10); \
2068 c -= a; c -= b; c ^= (b >> 15); \
2069 } while (0)
2070
2071/*
2072 * hash function based on bridge_hash in if_bridge.c
2073 */
2074void
2075pf_hash(struct pf_addr *inaddr, struct pf_addr *hash,
2076 struct pf_poolhashkey *key, sa_family_t af)
2077{
2078 u_int32_t a = 0x9e3779b9, b = 0x9e3779b9, c = key->key32[0];
2079
2080 switch (af) {
2081#ifdef INET
2082 case AF_INET:
2083 a += inaddr->addr32[0];
2084 b += key->key32[1];
2085 mix(a, b, c);
2086 hash->addr32[0] = c + key->key32[2];
2087 break;
2088#endif /* INET */
2089#ifdef INET6
2090 case AF_INET6:
2091 a += inaddr->addr32[0];
2092 b += inaddr->addr32[2];
2093 mix(a, b, c);
2094 hash->addr32[0] = c;
2095 a += inaddr->addr32[1];
2096 b += inaddr->addr32[3];
2097 c += key->key32[1];
2098 mix(a, b, c);
2099 hash->addr32[1] = c;
2100 a += inaddr->addr32[2];
2101 b += inaddr->addr32[1];
2102 c += key->key32[2];
2103 mix(a, b, c);
2104 hash->addr32[2] = c;
2105 a += inaddr->addr32[3];
2106 b += inaddr->addr32[0];
2107 c += key->key32[3];
2108 mix(a, b, c);
2109 hash->addr32[3] = c;
2110 break;
2111#endif /* INET6 */
2112 }
2113}
2114
2115int
2116pf_map_addr(sa_family_t af, struct pf_rule *r, struct pf_addr *saddr,
2117 struct pf_addr *naddr, struct pf_addr *init_addr, struct pf_src_node **sn)
2118{
2119 unsigned char hash[16];
2120 struct pf_pool *rpool = &r->rpool;
2121 struct pf_addr *raddr = &rpool->cur->addr.v.a.addr;
2122 struct pf_addr *rmask = &rpool->cur->addr.v.a.mask;
2123 struct pf_pooladdr *acur = rpool->cur;
2124 struct pf_src_node k;
2125
2126 if (*sn == NULL && r->rpool.opts & PF_POOL_STICKYADDR &&
2127 (r->rpool.opts & PF_POOL_TYPEMASK) != PF_POOL_NONE) {
2128 k.af = af;
2129 PF_ACPY(&k.addr, saddr, af);
2130 if (r->rule_flag & PFRULE_RULESRCTRACK ||
2131 r->rpool.opts & PF_POOL_STICKYADDR)
2132 k.rule.ptr = r;
2133 else
2134 k.rule.ptr = NULL;
2135 pf_status.scounters[SCNT_SRC_NODE_SEARCH]++;
2136 *sn = RB_FIND(pf_src_tree, &tree_src_tracking, &k);
2137 if (*sn != NULL && !PF_AZERO(&(*sn)->raddr, af)) {
2138 PF_ACPY(naddr, &(*sn)->raddr, af);
2139 if (pf_status.debug >= PF_DEBUG_MISC) {
4b1cf444 2140 kprintf("pf_map_addr: src tracking maps ");
02742ec6 2141 pf_print_host(&k.addr, 0, af);
4b1cf444 2142 kprintf(" to ");
02742ec6 2143 pf_print_host(naddr, 0, af);
4b1cf444 2144 kprintf("\n");
02742ec6
JS
2145 }
2146 return (0);
2147 }
2148 }
2149
2150 if (rpool->cur->addr.type == PF_ADDR_NOROUTE)
2151 return (1);
2152 if (rpool->cur->addr.type == PF_ADDR_DYNIFTL) {
70224baa
JL
2153 switch (af) {
2154#ifdef INET
2155 case AF_INET:
02742ec6
JS
2156 if (rpool->cur->addr.p.dyn->pfid_acnt4 < 1 &&
2157 (rpool->opts & PF_POOL_TYPEMASK) !=
2158 PF_POOL_ROUNDROBIN)
2159 return (1);
2160 raddr = &rpool->cur->addr.p.dyn->pfid_addr4;
2161 rmask = &rpool->cur->addr.p.dyn->pfid_mask4;
70224baa
JL
2162 break;
2163#endif /* INET */
2164#ifdef INET6
2165 case AF_INET6:
02742ec6
JS
2166 if (rpool->cur->addr.p.dyn->pfid_acnt6 < 1 &&
2167 (rpool->opts & PF_POOL_TYPEMASK) !=
2168 PF_POOL_ROUNDROBIN)
2169 return (1);
2170 raddr = &rpool->cur->addr.p.dyn->pfid_addr6;
2171 rmask = &rpool->cur->addr.p.dyn->pfid_mask6;
70224baa
JL
2172 break;
2173#endif /* INET6 */
02742ec6
JS
2174 }
2175 } else if (rpool->cur->addr.type == PF_ADDR_TABLE) {
2176 if ((rpool->opts & PF_POOL_TYPEMASK) != PF_POOL_ROUNDROBIN)
2177 return (1); /* unsupported */
2178 } else {
2179 raddr = &rpool->cur->addr.v.a.addr;
2180 rmask = &rpool->cur->addr.v.a.mask;
2181 }
2182
2183 switch (rpool->opts & PF_POOL_TYPEMASK) {
2184 case PF_POOL_NONE:
2185 PF_ACPY(naddr, raddr, af);
2186 break;
2187 case PF_POOL_BITMASK:
2188 PF_POOLMASK(naddr, raddr, rmask, saddr, af);
2189 break;
2190 case PF_POOL_RANDOM:
2191 if (init_addr != NULL && PF_AZERO(init_addr, af)) {
2192 switch (af) {
2193#ifdef INET
2194 case AF_INET:
4fc5aa1c 2195 rpool->counter.addr32[0] = htonl(karc4random());
02742ec6
JS
2196 break;
2197#endif /* INET */
2198#ifdef INET6
2199 case AF_INET6:
2200 if (rmask->addr32[3] != 0xffffffff)
70224baa 2201 rpool->counter.addr32[3] =
4fc5aa1c 2202 htonl(karc4random());
02742ec6
JS
2203 else
2204 break;
2205 if (rmask->addr32[2] != 0xffffffff)
70224baa 2206 rpool->counter.addr32[2] =
4fc5aa1c 2207 htonl(karc4random());
02742ec6
JS
2208 else
2209 break;
2210 if (rmask->addr32[1] != 0xffffffff)
70224baa 2211 rpool->counter.addr32[1] =
4fc5aa1c 2212 htonl(karc4random());
02742ec6
JS
2213 else
2214 break;
2215 if (rmask->addr32[0] != 0xffffffff)
70224baa 2216 rpool->counter.addr32[0] =
4fc5aa1c 2217 htonl(karc4random());
02742ec6
JS
2218 break;
2219#endif /* INET6 */
2220 }
2221 PF_POOLMASK(naddr, raddr, rmask, &rpool->counter, af);
2222 PF_ACPY(init_addr, naddr, af);
2223
2224 } else {
2225 PF_AINC(&rpool->counter, af);
2226 PF_POOLMASK(naddr, raddr, rmask, &rpool->counter, af);
2227 }
2228 break;
2229 case PF_POOL_SRCHASH:
2230 pf_hash(saddr, (struct pf_addr *)&hash, &rpool->key, af);
2231 PF_POOLMASK(naddr, raddr, rmask, (struct pf_addr *)&hash, af);
2232 break;
2233 case PF_POOL_ROUNDROBIN:
2234 if (rpool->cur->addr.type == PF_ADDR_TABLE) {
2235 if (!pfr_pool_get(rpool->cur->addr.p.tbl,
2236 &rpool->tblidx, &rpool->counter,
2237 &raddr, &rmask, af))
2238 goto get_addr;
2239 } else if (rpool->cur->addr.type == PF_ADDR_DYNIFTL) {
2240 if (!pfr_pool_get(rpool->cur->addr.p.dyn->pfid_kt,
2241 &rpool->tblidx, &rpool->counter,
2242 &raddr, &rmask, af))
2243 goto get_addr;
2244 } else if (pf_match_addr(0, raddr, rmask, &rpool->counter, af))
2245 goto get_addr;
2246
2247 try_next:
2248 if ((rpool->cur = TAILQ_NEXT(rpool->cur, entries)) == NULL)
2249 rpool->cur = TAILQ_FIRST(&rpool->list);
2250 if (rpool->cur->addr.type == PF_ADDR_TABLE) {
2251 rpool->tblidx = -1;
2252 if (pfr_pool_get(rpool->cur->addr.p.tbl,
2253 &rpool->tblidx, &rpool->counter,
2254 &raddr, &rmask, af)) {
2255 /* table contains no address of type 'af' */
2256 if (rpool->cur != acur)
2257 goto try_next;
2258 return (1);
2259 }
2260 } else if (rpool->cur->addr.type == PF_ADDR_DYNIFTL) {
2261 rpool->tblidx = -1;
2262 if (pfr_pool_get(rpool->cur->addr.p.dyn->pfid_kt,
2263 &rpool->tblidx, &rpool->counter,
2264 &raddr, &rmask, af)) {
2265 /* table contains no address of type 'af' */
2266 if (rpool->cur != acur)
2267 goto try_next;
2268 return (1);
2269 }
2270 } else {
2271 raddr = &rpool->cur->addr.v.a.addr;
2272 rmask = &rpool->cur->addr.v.a.mask;
2273 PF_ACPY(&rpool->counter, raddr, af);
2274 }
2275
2276 get_addr:
2277 PF_ACPY(naddr, &rpool->counter, af);
70224baa
JL
2278 if (init_addr != NULL && PF_AZERO(init_addr, af))
2279 PF_ACPY(init_addr, naddr, af);
02742ec6
JS
2280 PF_AINC(&rpool->counter, af);
2281 break;
2282 }
2283 if (*sn != NULL)
2284 PF_ACPY(&(*sn)->raddr, naddr, af);
2285
2286 if (pf_status.debug >= PF_DEBUG_MISC &&
2287 (rpool->opts & PF_POOL_TYPEMASK) != PF_POOL_NONE) {
4b1cf444 2288 kprintf("pf_map_addr: selected address ");
02742ec6 2289 pf_print_host(naddr, 0, af);
4b1cf444 2290 kprintf("\n");
02742ec6
JS
2291 }
2292
2293 return (0);
2294}
2295
2296int
2297pf_get_sport(sa_family_t af, u_int8_t proto, struct pf_rule *r,
2298 struct pf_addr *saddr, struct pf_addr *daddr, u_int16_t dport,
2299 struct pf_addr *naddr, u_int16_t *nport, u_int16_t low, u_int16_t high,
2300 struct pf_src_node **sn)
2301{
315a7da3 2302 struct pf_state_key_cmp key;
02742ec6
JS
2303 struct pf_addr init_addr;
2304 u_int16_t cut;
2305
2306 bzero(&init_addr, sizeof(init_addr));
2307 if (pf_map_addr(af, r, saddr, naddr, &init_addr, sn))
2308 return (1);
2309
70224baa
JL
2310 if (proto == IPPROTO_ICMP) {
2311 low = 1;
2312 high = 65535;
2313 }
2314
02742ec6
JS
2315 do {
2316 key.af = af;
2317 key.proto = proto;
2318 PF_ACPY(&key.ext.addr, daddr, key.af);
2319 PF_ACPY(&key.gwy.addr, naddr, key.af);
2320 key.ext.port = dport;
2321
2322 /*
2323 * port search; start random, step;
2324 * similar 2 portloop in in_pcbbind
2325 */
70224baa
JL
2326 if (!(proto == IPPROTO_TCP || proto == IPPROTO_UDP ||
2327 proto == IPPROTO_ICMP)) {
2328 key.gwy.port = dport;
02742ec6
JS
2329 if (pf_find_state_all(&key, PF_EXT_GWY, NULL) == NULL)
2330 return (0);
2331 } else if (low == 0 && high == 0) {
2332 key.gwy.port = *nport;
2333 if (pf_find_state_all(&key, PF_EXT_GWY, NULL) == NULL)
2334 return (0);
2335 } else if (low == high) {
2336 key.gwy.port = htons(low);
2337 if (pf_find_state_all(&key, PF_EXT_GWY, NULL) == NULL) {
2338 *nport = htons(low);
2339 return (0);
2340 }
2341 } else {
2342 u_int16_t tmp;
2343
2344 if (low > high) {
2345 tmp = low;
2346 low = high;
2347 high = tmp;
2348 }
2349 /* low < high */
4fc5aa1c 2350 cut = htonl(karc4random()) % (1 + high - low) + low;
02742ec6
JS
2351 /* low <= cut <= high */
2352 for (tmp = cut; tmp <= high; ++(tmp)) {
2353 key.gwy.port = htons(tmp);
2354 if (pf_find_state_all(&key, PF_EXT_GWY, NULL) ==
2355 NULL) {
2356 *nport = htons(tmp);
2357 return (0);
2358 }
2359 }
2360 for (tmp = cut - 1; tmp >= low; --(tmp)) {
2361 key.gwy.port = htons(tmp);
2362 if (pf_find_state_all(&key, PF_EXT_GWY, NULL) ==
2363 NULL) {
2364 *nport = htons(tmp);
2365 return (0);
2366 }
2367 }
2368 }
2369
2370 switch (r->rpool.opts & PF_POOL_TYPEMASK) {
2371 case PF_POOL_RANDOM:
2372 case PF_POOL_ROUNDROBIN:
2373 if (pf_map_addr(af, r, saddr, naddr, &init_addr, sn))
2374 return (1);
2375 break;
2376 case PF_POOL_NONE:
2377 case PF_POOL_SRCHASH:
2378 case PF_POOL_BITMASK:
2379 default:
2380 return (1);
2381 }
2382 } while (! PF_AEQ(&init_addr, naddr, af) );
2383
2384 return (1); /* none available */
2385}
2386
2387struct pf_rule *
2388pf_match_translation(struct pf_pdesc *pd, struct mbuf *m, int off,
2389 int direction, struct pfi_kif *kif, struct pf_addr *saddr, u_int16_t sport,
2390 struct pf_addr *daddr, u_int16_t dport, int rs_num)
2391{
70224baa 2392 struct pf_rule *r, *rm = NULL;
02742ec6 2393 struct pf_ruleset *ruleset = NULL;
70224baa
JL
2394 int tag = -1;
2395 int rtableid = -1;
2396 int asd = 0;
02742ec6
JS
2397
2398 r = TAILQ_FIRST(pf_main_ruleset.rules[rs_num].active.ptr);
2399 while (r && rm == NULL) {
2400 struct pf_rule_addr *src = NULL, *dst = NULL;
2401 struct pf_addr_wrap *xdst = NULL;
2402
2403 if (r->action == PF_BINAT && direction == PF_IN) {
2404 src = &r->dst;
2405 if (r->rpool.cur != NULL)
2406 xdst = &r->rpool.cur->addr;
2407 } else {
2408 src = &r->src;
2409 dst = &r->dst;
2410 }
2411
2412 r->evaluations++;
70224baa 2413 if (pfi_kif_match(r->kif, kif) == r->ifnot)
02742ec6
JS
2414 r = r->skip[PF_SKIP_IFP].ptr;
2415 else if (r->direction && r->direction != direction)
2416 r = r->skip[PF_SKIP_DIR].ptr;
2417 else if (r->af && r->af != pd->af)
2418 r = r->skip[PF_SKIP_AF].ptr;
2419 else if (r->proto && r->proto != pd->proto)
2420 r = r->skip[PF_SKIP_PROTO].ptr;
70224baa
JL
2421 else if (PF_MISMATCHAW(&src->addr, saddr, pd->af,
2422 src->neg, kif))
02742ec6
JS
2423 r = r->skip[src == &r->src ? PF_SKIP_SRC_ADDR :
2424 PF_SKIP_DST_ADDR].ptr;
2425 else if (src->port_op && !pf_match_port(src->port_op,
2426 src->port[0], src->port[1], sport))
2427 r = r->skip[src == &r->src ? PF_SKIP_SRC_PORT :
2428 PF_SKIP_DST_PORT].ptr;
2429 else if (dst != NULL &&
70224baa 2430 PF_MISMATCHAW(&dst->addr, daddr, pd->af, dst->neg, NULL))
02742ec6 2431 r = r->skip[PF_SKIP_DST_ADDR].ptr;
70224baa
JL
2432 else if (xdst != NULL && PF_MISMATCHAW(xdst, daddr, pd->af,
2433 0, NULL))
02742ec6
JS
2434 r = TAILQ_NEXT(r, entries);
2435 else if (dst != NULL && dst->port_op &&
2436 !pf_match_port(dst->port_op, dst->port[0],
2437 dst->port[1], dport))
2438 r = r->skip[PF_SKIP_DST_PORT].ptr;
315a7da3 2439 else if (r->match_tag && !pf_match_tag(m, r, &tag))
70224baa 2440 r = TAILQ_NEXT(r, entries);
02742ec6
JS
2441 else if (r->os_fingerprint != PF_OSFP_ANY && (pd->proto !=
2442 IPPROTO_TCP || !pf_osfp_match(pf_osfp_fingerprint(pd, m,
2443 off, pd->hdr.tcp), r->os_fingerprint)))
2444 r = TAILQ_NEXT(r, entries);
70224baa
JL
2445 else {
2446 if (r->tag)
2447 tag = r->tag;
2448 if (r->rtableid >= 0)
2449 rtableid = r->rtableid;
2450 if (r->anchor == NULL) {
02742ec6 2451 rm = r;
70224baa
JL
2452 } else
2453 pf_step_into_anchor(&asd, &ruleset, rs_num,
2454 &r, NULL, NULL);
2455 }
2456 if (r == NULL)
2457 pf_step_out_of_anchor(&asd, &ruleset, rs_num, &r,
2458 NULL, NULL);
02742ec6 2459 }
315a7da3 2460 if (pf_tag_packet(m, tag, rtableid))
70224baa 2461 return (NULL);
02742ec6
JS
2462 if (rm != NULL && (rm->action == PF_NONAT ||
2463 rm->action == PF_NORDR || rm->action == PF_NOBINAT))
2464 return (NULL);
2465 return (rm);
2466}
2467
2468struct pf_rule *
2469pf_get_translation(struct pf_pdesc *pd, struct mbuf *m, int off, int direction,
2470 struct pfi_kif *kif, struct pf_src_node **sn,
2471 struct pf_addr *saddr, u_int16_t sport,
2472 struct pf_addr *daddr, u_int16_t dport,
2473 struct pf_addr *naddr, u_int16_t *nport)
2474{
2475 struct pf_rule *r = NULL;
2476
2477 if (direction == PF_OUT) {
2478 r = pf_match_translation(pd, m, off, direction, kif, saddr,
2479 sport, daddr, dport, PF_RULESET_BINAT);
2480 if (r == NULL)
2481 r = pf_match_translation(pd, m, off, direction, kif,
2482 saddr, sport, daddr, dport, PF_RULESET_NAT);
2483 } else {
2484 r = pf_match_translation(pd, m, off, direction, kif, saddr,
2485 sport, daddr, dport, PF_RULESET_RDR);
2486 if (r == NULL)
2487 r = pf_match_translation(pd, m, off, direction, kif,
2488 saddr, sport, daddr, dport, PF_RULESET_BINAT);
2489 }
2490
2491 if (r != NULL) {
2492 switch (r->action) {
2493 case PF_NONAT:
2494 case PF_NOBINAT:
2495 case PF_NORDR:
2496 return (NULL);
2497 case PF_NAT:
2498 if (pf_get_sport(pd->af, pd->proto, r, saddr,
2499 daddr, dport, naddr, nport, r->rpool.proxy_port[0],
2500 r->rpool.proxy_port[1], sn)) {
2501 DPFPRINTF(PF_DEBUG_MISC,
2502 ("pf: NAT proxy port allocation "
2503 "(%u-%u) failed\n",
2504 r->rpool.proxy_port[0],
2505 r->rpool.proxy_port[1]));
2506 return (NULL);
2507 }
2508 break;
2509 case PF_BINAT:
2510 switch (direction) {
2511 case PF_OUT:
2512 if (r->rpool.cur->addr.type == PF_ADDR_DYNIFTL){
70224baa
JL
2513 switch (pd->af) {
2514#ifdef INET
2515 case AF_INET:
02742ec6
JS
2516 if (r->rpool.cur->addr.p.dyn->
2517 pfid_acnt4 < 1)
2518 return (NULL);
2519 PF_POOLMASK(naddr,
2520 &r->rpool.cur->addr.p.dyn->
2521 pfid_addr4,
2522 &r->rpool.cur->addr.p.dyn->
2523 pfid_mask4,
2524 saddr, AF_INET);
70224baa
JL
2525 break;
2526#endif /* INET */
2527#ifdef INET6
2528 case AF_INET6:
02742ec6
JS
2529 if (r->rpool.cur->addr.p.dyn->
2530 pfid_acnt6 < 1)
2531 return (NULL);
2532 PF_POOLMASK(naddr,
2533 &r->rpool.cur->addr.p.dyn->
2534 pfid_addr6,
2535 &r->rpool.cur->addr.p.dyn->
2536 pfid_mask6,
2537 saddr, AF_INET6);
70224baa
JL
2538 break;
2539#endif /* INET6 */
02742ec6
JS
2540 }
2541 } else
2542 PF_POOLMASK(naddr,
2543 &r->rpool.cur->addr.v.a.addr,
2544 &r->rpool.cur->addr.v.a.mask,
2545 saddr, pd->af);
2546 break;
2547 case PF_IN:
70224baa
JL
2548 if (r->src.addr.type == PF_ADDR_DYNIFTL) {
2549 switch (pd->af) {
2550#ifdef INET
2551 case AF_INET:
02742ec6
JS
2552 if (r->src.addr.p.dyn->
2553 pfid_acnt4 < 1)
2554 return (NULL);
2555 PF_POOLMASK(naddr,
2556 &r->src.addr.p.dyn->
2557 pfid_addr4,
2558 &r->src.addr.p.dyn->
2559 pfid_mask4,
2560 daddr, AF_INET);
70224baa
JL
2561 break;
2562#endif /* INET */
2563#ifdef INET6
2564 case AF_INET6:
02742ec6
JS
2565 if (r->src.addr.p.dyn->
2566 pfid_acnt6 < 1)
2567 return (NULL);
2568 PF_POOLMASK(naddr,
2569 &r->src.addr.p.dyn->
2570 pfid_addr6,
2571 &r->src.addr.p.dyn->
2572 pfid_mask6,
2573 daddr, AF_INET6);
70224baa
JL
2574 break;
2575#endif /* INET6 */
02742ec6
JS
2576 }
2577 } else
2578 PF_POOLMASK(naddr,
2579 &r->src.addr.v.a.addr,
2580 &r->src.addr.v.a.mask, daddr,
2581 pd->af);
2582 break;
2583 }
2584 break;
2585 case PF_RDR: {
70224baa 2586 if (pf_map_addr(pd->af, r, saddr, naddr, NULL, sn))
02742ec6 2587 return (NULL);
70224baa
JL
2588 if ((r->rpool.opts & PF_POOL_TYPEMASK) ==
2589 PF_POOL_BITMASK)
2590 PF_POOLMASK(naddr, naddr,
2591 &r->rpool.cur->addr.v.a.mask, daddr,
2592 pd->af);
315a7da3 2593
02742ec6
JS
2594 if (r->rpool.proxy_port[1]) {
2595 u_int32_t tmp_nport;
2596
2597 tmp_nport = ((ntohs(dport) -
2598 ntohs(r->dst.port[0])) %
2599 (r->rpool.proxy_port[1] -
2600 r->rpool.proxy_port[0] + 1)) +
2601 r->rpool.proxy_port[0];
2602
2603 /* wrap around if necessary */
2604 if (tmp_nport > 65535)
2605 tmp_nport -= 65535;
2606 *nport = htons((u_int16_t)tmp_nport);
2607 } else if (r->rpool.proxy_port[0])
2608 *nport = htons(r->rpool.proxy_port[0]);
2609 break;
2610 }
2611 default:
2612 return (NULL);
2613 }
2614 }
2615
2616 return (r);
2617}
2618
2619#ifdef SMP
2620struct netmsg_hashlookup {
4599cf19 2621 struct netmsg nm_netmsg;
02742ec6
JS
2622 struct inpcb **nm_pinp;
2623 struct inpcbinfo *nm_pcbinfo;
2624 struct pf_addr *nm_saddr;
2625 struct pf_addr *nm_daddr;
2626 uint16_t nm_sport;
2627 uint16_t nm_dport;
2628 sa_family_t nm_af;
2629};
2630
4599cf19
MD
2631static void
2632in_pcblookup_hash_handler(struct netmsg *msg0)
02742ec6
JS
2633{
2634 struct netmsg_hashlookup *msg = (struct netmsg_hashlookup *)msg0;
2635
2636 if (msg->nm_af == AF_INET)
2637 *msg->nm_pinp = in_pcblookup_hash(msg->nm_pcbinfo,
2638 msg->nm_saddr->v4, msg->nm_sport, msg->nm_daddr->v4,
2639 msg->nm_dport, INPLOOKUP_WILDCARD, NULL);
2640#ifdef INET6
2641 else
2642 *msg->nm_pinp = in6_pcblookup_hash(msg->nm_pcbinfo,
2643 &msg->nm_saddr->v6, msg->nm_sport, &msg->nm_daddr->v6,
2644 msg->nm_dport, INPLOOKUP_WILDCARD, NULL);
2645#endif /* INET6 */
4599cf19 2646 lwkt_replymsg(&msg->nm_netmsg.nm_lmsg, 0);
02742ec6
JS
2647}
2648#endif /* SMP */
2649
2650int
315a7da3 2651pf_socket_lookup(int direction, struct pf_pdesc *pd)
02742ec6
JS
2652{
2653 struct pf_addr *saddr, *daddr;
2654 u_int16_t sport, dport;
2655 struct inpcbinfo *pi;
2656 struct inpcb *inp;
2657#ifdef SMP
2658 struct netmsg_hashlookup *msg = NULL;
2659#endif
2660 int pi_cpu = 0;
2661
70224baa
JL
2662 if (pd == NULL)
2663 return (-1);
2664 pd->lookup.uid = UID_MAX;
2665 pd->lookup.gid = GID_MAX;
2666 pd->lookup.pid = NO_PID;
02742ec6
JS
2667 if (direction == PF_IN) {
2668 saddr = pd->src;
2669 daddr = pd->dst;
2670 } else {
2671 saddr = pd->dst;
2672 daddr = pd->src;
2673 }
2674 switch (pd->proto) {
2675 case IPPROTO_TCP:
315a7da3
JL
2676 if (pd->hdr.tcp == NULL)
2677 return (-1);
02742ec6
JS
2678 sport = pd->hdr.tcp->th_sport;
2679 dport = pd->hdr.tcp->th_dport;
2680
2681 pi_cpu = tcp_addrcpu(saddr->v4.s_addr, sport, daddr->v4.s_addr, dport);
2682 pi = &tcbinfo[pi_cpu];
2683#ifdef SMP
2684 /*
2685 * Our netstack runs lockless on MP systems
2686 * (only for TCP connections at the moment).
2687 *
2688 * As we are not allowed to read another CPU's tcbinfo,
2689 * we have to ask that CPU via remote call to search the
2690 * table for us.
2691 *
2692 * Prepare a msg iff data belongs to another CPU.
2693 */
2694 if (pi_cpu != mycpu->gd_cpuid) {
efda3bd0 2695 msg = kmalloc(sizeof(*msg), M_LWKTMSG, M_INTWAIT);
48e7b118
MD
2696 netmsg_init(&msg->nm_netmsg, NULL, &netisr_afree_rport,
2697 0, in_pcblookup_hash_handler);
02742ec6
JS
2698 msg->nm_pinp = &inp;
2699 msg->nm_pcbinfo = pi;
2700 msg->nm_saddr = saddr;
2701 msg->nm_sport = sport;
2702 msg->nm_daddr = daddr;
2703 msg->nm_dport = dport;
2704 msg->nm_af = pd->af;
2705 }
2706#endif /* SMP */
2707 break;
2708 case IPPROTO_UDP:
315a7da3
JL
2709 if (pd->hdr.udp == NULL)
2710 return (-1);
02742ec6
JS
2711 sport = pd->hdr.udp->uh_sport;
2712 dport = pd->hdr.udp->uh_dport;
2713 pi = &udbinfo;
2714 break;
2715 default:
315a7da3 2716 return (-1);
02742ec6
JS
2717 }
2718 if (direction != PF_IN) {
2719 u_int16_t p;
2720
2721 p = sport;
2722 sport = dport;
2723 dport = p;
2724 }
2725 switch (pd->af) {
2726#ifdef INET6
2727 case AF_INET6:
2728#ifdef SMP
2729 /*
2730 * Query other CPU, second part
2731 *
2732 * msg only gets initialized when:
2733 * 1) packet is TCP
2734 * 2) the info belongs to another CPU
2735 *
2736 * Use some switch/case magic to avoid code duplication.
2737 */
2738 if (msg == NULL)
2739#endif /* SMP */
2740 {
2741 inp = in6_pcblookup_hash(pi, &saddr->v6, sport,
2742 &daddr->v6, dport, INPLOOKUP_WILDCARD, NULL);
2743
2744 if (inp == NULL)
70224baa 2745 return (-1);
02742ec6
JS
2746 break;
2747 }
2748 /* FALLTHROUGH if SMP and on other CPU */
2749#endif /* INET6 */
2750 case AF_INET:
2751#ifdef SMP
2752 if (msg != NULL) {
93bffeca 2753 lwkt_sendmsg(cpu_portfn(pi_cpu),
4599cf19 2754 &msg->nm_netmsg.nm_lmsg);
02742ec6
JS
2755 } else
2756#endif /* SMP */
2757 {
2758 inp = in_pcblookup_hash(pi, saddr->v4, sport, daddr->v4,
2759 dport, INPLOOKUP_WILDCARD, NULL);
2760 }
2761 if (inp == NULL)
315a7da3 2762 return (-1);
02742ec6
JS
2763 break;
2764
2765 default:
70224baa 2766 return (-1);
02742ec6 2767 }
70224baa
JL
2768 pd->lookup.uid = inp->inp_socket->so_cred->cr_uid;
2769 pd->lookup.gid = inp->inp_socket->so_cred->cr_groups[0];
02742ec6
JS
2770 return (1);
2771}
2772
2773u_int8_t
2774pf_get_wscale(struct mbuf *m, int off, u_int16_t th_off, sa_family_t af)
2775{
2776 int hlen;
2777 u_int8_t hdr[60];
2778 u_int8_t *opt, optlen;
2779 u_int8_t wscale = 0;
2780
2781 hlen = th_off << 2; /* hlen <= sizeof(hdr) */
2782 if (hlen <= sizeof(struct tcphdr))
2783 return (0);
2784 if (!pf_pull_hdr(m, off, hdr, hlen, NULL, NULL, af))
2785 return (0);
2786 opt = hdr + sizeof(struct tcphdr);
2787 hlen -= sizeof(struct tcphdr);
2788 while (hlen >= 3) {
2789 switch (*opt) {
2790 case TCPOPT_EOL:
2791 case TCPOPT_NOP:
2792 ++opt;
2793 --hlen;
2794 break;
2795 case TCPOPT_WINDOW:
2796 wscale = opt[2];
2797 if (wscale > TCP_MAX_WINSHIFT)
2798 wscale = TCP_MAX_WINSHIFT;
2799 wscale |= PF_WSCALE_FLAG;
2800 /* FALLTHROUGH */
2801 default:
2802 optlen = opt[1];
2803 if (optlen < 2)
2804 optlen = 2;
2805 hlen -= optlen;
2806 opt += optlen;
2807 break;
2808 }
2809 }
2810 return (wscale);
2811}
2812
2813u_int16_t
2814pf_get_mss(struct mbuf *m, int off, u_int16_t th_off, sa_family_t af)
2815{
2816 int hlen;
2817 u_int8_t hdr[60];
2818 u_int8_t *opt, optlen;
2819 u_int16_t mss = tcp_mssdflt;
2820
2821 hlen = th_off << 2; /* hlen <= sizeof(hdr) */
2822 if (hlen <= sizeof(struct tcphdr))
2823 return (0);
2824 if (!pf_pull_hdr(m, off, hdr, hlen, NULL, NULL, af))
2825 return (0);
2826 opt = hdr + sizeof(struct tcphdr);
2827 hlen -= sizeof(struct tcphdr);
2828 while (hlen >= TCPOLEN_MAXSEG) {
2829 switch (*opt) {
2830 case TCPOPT_EOL:
2831 case TCPOPT_NOP:
2832 ++opt;
2833 --hlen;
2834 break;
2835 case TCPOPT_MAXSEG:
2836 bcopy((caddr_t)(opt + 2), (caddr_t)&mss, 2);
2837 /* FALLTHROUGH */
2838 default:
2839 optlen = opt[1];
2840 if (optlen < 2)
2841 optlen = 2;
2842 hlen -= optlen;
2843 opt += optlen;
2844 break;
2845 }
2846 }
2847 return (mss);
2848}
2849
2850u_int16_t
2851pf_calc_mss(struct pf_addr *addr, sa_family_t af, u_int16_t offer)
2852{
2853#ifdef INET
2854 struct sockaddr_in *dst;
2855 struct route ro;
2856#endif /* INET */
2857#ifdef INET6
2858 struct sockaddr_in6 *dst6;
2859 struct route_in6 ro6;
2860#endif /* INET6 */
2861 struct rtentry *rt = NULL;
2862 int hlen = 0;
2863 u_int16_t mss = tcp_mssdflt;
2864
2865 switch (af) {
2866#ifdef INET
2867 case AF_INET:
2868 hlen = sizeof(struct ip);
2869 bzero(&ro, sizeof(ro));
2870 dst = (struct sockaddr_in *)&ro.ro_dst;
2871 dst->sin_family = AF_INET;
2872 dst->sin_len = sizeof(*dst);
2873 dst->sin_addr = addr->v4;
2874 rtalloc_ign(&ro, (RTF_CLONING | RTF_PRCLONING));
2875 rt = ro.ro_rt;
2876 break;
2877#endif /* INET */
2878#ifdef INET6
2879 case AF_INET6:
2880 hlen = sizeof(struct ip6_hdr);
2881 bzero(&ro6, sizeof(ro6));
2882 dst6 = (struct sockaddr_in6 *)&ro6.ro_dst;
2883 dst6->sin6_family = AF_INET6;
2884 dst6->sin6_len = sizeof(*dst6);
2885 dst6->sin6_addr = addr->v6;
2886 rtalloc_ign((struct route *)&ro6, (RTF_CLONING | RTF_PRCLONING));
2887 rt = ro6.ro_rt;
2888 break;
2889#endif /* INET6 */
2890 }
2891
2892 if (rt && rt->rt_ifp) {
2893 mss = rt->rt_ifp->if_mtu - hlen - sizeof(struct tcphdr);
2894 mss = max(tcp_mssdflt, mss);
2895 RTFREE(rt);
2896 }
2897 mss = min(mss, offer);
2898 mss = max(mss, 64); /* sanity - at least max opt space */
2899 return (mss);
2900}
2901
2902void
2903pf_set_rt_ifp(struct pf_state *s, struct pf_addr *saddr)
2904{
2905 struct pf_rule *r = s->rule.ptr;
2906
2907 s->rt_kif = NULL;
2908 if (!r->rt || r->rt == PF_FASTROUTE)
2909 return;
315a7da3 2910 switch (s->state_key->af) {
02742ec6
JS
2911#ifdef INET
2912 case AF_INET:
2913 pf_map_addr(AF_INET, r, saddr, &s->rt_addr, NULL,
2914 &s->nat_src_node);
2915 s->rt_kif = r->rpool.cur->kif;
2916 break;
2917#endif /* INET */
2918#ifdef INET6
2919 case AF_INET6:
2920 pf_map_addr(AF_INET6, r, saddr, &s->rt_addr, NULL,
2921 &s->nat_src_node);
2922 s->rt_kif = r->rpool.cur->kif;
2923 break;
2924#endif /* INET6 */
2925 }
2926}
2927
315a7da3
JL
2928void
2929pf_attach_state(struct pf_state_key *sk, struct pf_state *s, int tail)
2930{
2931 s->state_key = sk;
2932 sk->refcnt++;
2933
2934 /* list is sorted, if-bound states before floating */
2935 if (tail)
2936 TAILQ_INSERT_TAIL(&sk->states, s, next);
2937 else
2938 TAILQ_INSERT_HEAD(&sk->states, s, next);
2939}
2940
2941void
2942pf_detach_state(struct pf_state *s, int flags)
2943{
2944 struct pf_state_key *sk = s->state_key;
2945
2946 if (sk == NULL)
2947 return;
2948
2949 s->state_key = NULL;
2950 TAILQ_REMOVE(&sk->states, s, next);
2951 if (--sk->refcnt == 0) {
2952 if (!(flags & PF_DT_SKIP_EXTGWY))
2953 RB_REMOVE(pf_state_tree_ext_gwy,
2954 &pf_statetbl_ext_gwy, sk);
2955 if (!(flags & PF_DT_SKIP_LANEXT))
2956 RB_REMOVE(pf_state_tree_lan_ext,
2957 &pf_statetbl_lan_ext, sk);
2958 pool_put(&pf_state_key_pl, sk);
2959 }
2960}
2961
2962struct pf_state_key *
2963pf_alloc_state_key(struct pf_state *s)
2964{
2965 struct pf_state_key *sk;
2966
2967 if ((sk = pool_get(&pf_state_key_pl, PR_NOWAIT)) == NULL)
2968 return (NULL);
2969 bzero(sk, sizeof(*sk));
2970 TAILQ_INIT(&sk->states);
2971 pf_attach_state(sk, s, 0);
2972
2973 return (sk);
2974}
2975
02742ec6 2976int
315a7da3 2977pf_test_rule(struct pf_rule **rm, struct pf_state **sm, int direction,
02742ec6 2978 struct pfi_kif *kif, struct mbuf *m, int off, void *h,
70224baa
JL
2979 struct pf_pdesc *pd, struct pf_rule **am, struct pf_ruleset **rsm,
2980 struct ifqueue *ifq, struct inpcb *inp)
02742ec6
JS
2981{
2982 struct pf_rule *nr = NULL;
2983 struct pf_addr *saddr = pd->src, *daddr = pd->dst;
02742ec6
JS
2984 u_int16_t bport, nport = 0;
2985 sa_family_t af = pd->af;
02742ec6
JS
2986 struct pf_rule *r, *a = NULL;
2987 struct pf_ruleset *ruleset = NULL;
2988 struct pf_src_node *nsn = NULL;
315a7da3 2989 struct tcphdr *th = pd->hdr.tcp;
02742ec6 2990 u_short reason;
315a7da3 2991 int rewrite = 0, hdrlen = 0;
70224baa 2992 int tag = -1, rtableid = -1;
70224baa
JL
2993 int asd = 0;
2994 int match = 0;
315a7da3
JL
2995 int state_icmp = 0;
2996 u_int16_t mss = tcp_mssdflt;
2997 u_int16_t sport, dport;
2998 u_int8_t icmptype = 0, icmpcode = 0;
70224baa 2999
315a7da3 3000 if (direction == PF_IN && pf_check_congestion(ifq)) {
70224baa
JL
3001 REASON_SET(&reason, PFRES_CONGEST);
3002 return (PF_DROP);
3003 }
3004
3005 if (inp != NULL)
315a7da3
JL
3006 pd->lookup.done = pf_socket_lookup(direction, pd);
3007 else if (debug_pfugidhack) {
70224baa
JL
3008 crit_exit();
3009 DPFPRINTF(PF_DEBUG_MISC, ("pf: unlocked lookup\n"));
315a7da3 3010 pd->lookup.done = pf_socket_lookup(direction, pd);
70224baa
JL
3011 crit_enter();
3012 }
315a7da3
JL
3013
3014 sport = dport = hdrlen = 0;
3015
3016 switch (pd->proto) {
3017 case IPPROTO_TCP:
3018 sport = th->th_sport;
3019 dport = th->th_dport;
3020 hdrlen = sizeof(*th);
3021 break;
3022 case IPPROTO_UDP:
3023 sport = pd->hdr.udp->uh_sport;
3024 dport = pd->hdr.udp->uh_dport;
3025 hdrlen = sizeof(*pd->hdr.udp);
3026 break;
3027#ifdef INET
3028 case IPPROTO_ICMP:
3029 if (pd->af != AF_INET)
3030 break;
3031 sport = dport = pd->hdr.icmp->icmp_id;
3032 icmptype = pd->hdr.icmp->icmp_type;
3033 icmpcode = pd->hdr.icmp->icmp_code;
3034
3035 if (icmptype == ICMP_UNREACH ||
3036 icmptype == ICMP_SOURCEQUENCH ||
3037 icmptype == ICMP_REDIRECT ||
3038 icmptype == ICMP_TIMXCEED ||
3039 icmptype == ICMP_PARAMPROB)
3040 state_icmp++;
3041 break;
3042#endif /* INET */
3043#ifdef INET6
3044 case IPPROTO_ICMPV6:
3045 if (pd->af != AF_INET6)
3046 break;
3047 sport = dport = pd->hdr.icmp6->icmp6_id;
3048 hdrlen = sizeof(*pd->hdr.icmp6);
3049 icmptype = pd->hdr.icmp6->icmp6_type;
3050 icmpcode = pd->hdr.icmp6->icmp6_code;
3051
3052 if (icmptype == ICMP6_DST_UNREACH ||
3053 icmptype == ICMP6_PACKET_TOO_BIG ||
3054 icmptype == ICMP6_TIME_EXCEEDED ||
3055 icmptype == ICMP6_PARAM_PROB)
3056 state_icmp++;
3057 break;
3058#endif /* INET6 */
3059 }
02742ec6
JS
3060
3061 r = TAILQ_FIRST(pf_main_ruleset.rules[PF_RULESET_FILTER].active.ptr);
3062
3063 if (direction == PF_OUT) {
315a7da3 3064 bport = nport = sport;
02742ec6
JS
3065 /* check outgoing packet for BINAT/NAT */
3066 if ((nr = pf_get_translation(pd, m, off, PF_OUT, kif, &nsn,
315a7da3 3067 saddr, sport, daddr, dport, &pd->naddr, &nport)) != NULL) {
02742ec6 3068 PF_ACPY(&pd->baddr, saddr, af);
315a7da3
JL
3069 switch (pd->proto) {
3070 case IPPROTO_TCP:
3071 pf_change_ap(saddr, &th->th_sport, pd->ip_sum,
3072 &th->th_sum, &pd->naddr, nport, 0, af);
3073 sport = th->th_sport;
3074 rewrite++;
3075 break;
3076 case IPPROTO_UDP:
3077 pf_change_ap(saddr, &pd->hdr.udp->uh_sport,
3078 pd->ip_sum, &pd->hdr.udp->uh_sum,
3079 &pd->naddr, nport, 1, af);
3080 sport = pd->hdr.udp->uh_sport;
3081 rewrite++;
3082 break;
3083#ifdef INET
3084 case IPPROTO_ICMP:
3085 pf_change_a(&saddr->v4.s_addr, pd->ip_sum,
3086 pd->naddr.v4.s_addr, 0);
3087 pd->hdr.icmp->icmp_cksum = pf_cksum_fixup(
3088 pd->hdr.icmp->icmp_cksum, sport, nport, 0);
3089 pd->hdr.icmp->icmp_id = nport;
3090 m_copyback(m, off, ICMP_MINLEN, (caddr_t)pd->hdr.icmp);
3091 break;
3092#endif /* INET */
3093#ifdef INET6
3094 case IPPROTO_ICMPV6:
3095 pf_change_a6(saddr, &pd->hdr.icmp6->icmp6_cksum,
3096 &pd->naddr, 0);
3097 rewrite++;
3098 break;
3099#endif /* INET */
3100 default:
3101 switch (af) {
3102#ifdef INET
3103 case AF_INET:
3104 pf_change_a(&saddr->v4.s_addr,
3105 pd->ip_sum, pd->naddr.v4.s_addr, 0);
3106 break;
3107#endif /* INET */
3108#ifdef INET6
3109 case AF_INET6:
3110 PF_ACPY(saddr, &pd->naddr, af);
3111 break;
3112#endif /* INET */
3113 }
3114 break;
3115 }
3116
02742ec6
JS
3117 if (nr->natpass)
3118 r = NULL;
3119 pd->nat_rule = nr;
3120 }
3121 } else {
315a7da3 3122 bport = nport = dport;
02742ec6
JS
3123 /* check incoming packet for BINAT/RDR */
3124 if ((nr = pf_get_translation(pd, m, off, PF_IN, kif, &nsn,
315a7da3 3125 saddr, sport, daddr, dport, &pd->naddr, &nport)) != NULL) {
02742ec6 3126 PF_ACPY(&pd->baddr, daddr, af);
315a7da3
JL
3127 switch (pd->proto) {
3128 case IPPROTO_TCP:
3129 pf_change_ap(daddr, &th->th_dport, pd->ip_sum,
3130 &th->th_sum, &pd->naddr, nport, 0, af);
3131 dport = th->th_dport;
3132 rewrite++;
3133 break;
3134 case IPPROTO_UDP:
3135 pf_change_ap(daddr, &pd->hdr.udp->uh_dport,
3136 pd->ip_sum, &pd->hdr.udp->uh_sum,
3137 &pd->naddr, nport, 1, af);
3138 dport = pd->hdr.udp->uh_dport;
3139 rewrite++;
3140 break;
3141#ifdef INET
3142 case IPPROTO_ICMP:
3143 pf_change_a(&daddr->v4.s_addr, pd->ip_sum,
3144 pd->naddr.v4.s_addr, 0);
3145 break;
3146#endif /* INET */
3147#ifdef INET6
3148 case IPPROTO_ICMPV6:
3149 pf_change_a6(daddr, &pd->hdr.icmp6->icmp6_cksum,
3150 &pd->naddr, 0);
3151 rewrite++;
3152 break;
3153#endif /* INET6 */
3154 default:
3155 switch (af) {
3156#ifdef INET
3157 case AF_INET:
3158 pf_change_a(&daddr->v4.s_addr,
3159 pd->ip_sum, pd->naddr.v4.s_addr, 0);
3160 break;
3161#endif /* INET */
3162#ifdef INET6
3163 case AF_INET6:
3164 PF_ACPY(daddr, &pd->naddr, af);
3165 break;
3166#endif /* INET */
3167 }
3168 break;
3169 }
3170
02742ec6
JS
3171 if (nr->natpass)
3172 r = NULL;
3173 pd->nat_rule = nr;
3174 }
3175 }
3176
3177 while (r != NULL) {
3178 r->evaluations++;
70224baa 3179 if (pfi_kif_match(r->kif, kif) == r->ifnot)
02742ec6
JS
3180 r = r->skip[PF_SKIP_IFP].ptr;
3181 else if (r->direction && r->direction != direction)
3182 r = r->skip[PF_SKIP_DIR].ptr;
3183 else if (r->af && r->af != af)
3184 r = r->skip[PF_SKIP_AF].ptr;
315a7da3 3185 else if (r->proto && r->proto != pd->proto)
02742ec6 3186 r = r->skip[PF_SKIP_PROTO].ptr;
70224baa
JL
3187 else if (PF_MISMATCHAW(&r->src.addr, saddr, af,
3188 r->src.neg, kif))
02742ec6 3189 r = r->skip[PF_SKIP_SRC_ADDR].ptr;
315a7da3 3190 /* tcp/udp only. port_op always 0 in other cases */
02742ec6 3191 else if (r->src.port_op && !pf_match_port(r->src.port_op,
315a7da3 3192 r->src.port[0], r->src.port[1], sport))
02742ec6 3193 r = r->skip[PF_SKIP_SRC_PORT].ptr;
70224baa
JL
3194 else if (PF_MISMATCHAW(&r->dst.addr, daddr, af,
3195 r->dst.neg, NULL))
02742ec6 3196 r = r->skip[PF_SKIP_DST_ADDR].ptr;
315a7da3 3197 /* tcp/udp only. port_op always 0 in other cases */
02742ec6 3198 else if (r->dst.port_op && !pf_match_port(r->dst.port_op,
315a7da3 3199 r->dst.port[0], r->dst.port[1], dport))
02742ec6 3200 r = r->skip[PF_SKIP_DST_PORT].ptr;
315a7da3
JL
3201 /* icmp only. type always 0 in other cases */
3202 else if (r->type && r->type != icmptype + 1)
3203 r = TAILQ_NEXT(r, entries);
3204 /* icmp only. type always 0 in other cases */
3205 else if (r->code && r->code != icmpcode + 1)
3206 r = TAILQ_NEXT(r, entries);
70224baa 3207 else if (r->tos && !(r->tos == pd->tos))
02742ec6
JS
3208 r = TAILQ_NEXT(r, entries);
3209 else if (r->rule_flag & PFRULE_FRAGMENT)
3210 r = TAILQ_NEXT(r, entries);
315a7da3
JL
3211 else if (pd->proto == IPPROTO_TCP &&
3212 (r->flagset & th->th_flags) != r->flags)
02742ec6 3213 r = TAILQ_NEXT(r, entries);
315a7da3 3214 /* tcp/udp only. uid.op always 0 in other cases */
70224baa 3215 else if (r->uid.op && (pd->lookup.done || (pd->lookup.done =
315a7da3 3216 pf_socket_lookup(direction, pd), 1)) &&
02742ec6 3217 !pf_match_uid(r->uid.op, r->uid.uid[0], r->uid.uid[1],
70224baa 3218 pd->lookup.uid))
02742ec6 3219 r = TAILQ_NEXT(r, entries);
315a7da3 3220 /* tcp/udp only. gid.op always 0 in other cases */
70224baa 3221 else if (r->gid.op && (pd->lookup.done || (pd->lookup.done =
315a7da3 3222 pf_socket_lookup(direction, pd), 1)) &&
02742ec6 3223 !pf_match_gid(r->gid.op, r->gid.gid[0], r->gid.gid[1],
70224baa 3224 pd->lookup.gid))
02742ec6 3225 r = TAILQ_NEXT(r, entries);
75fda04a
MD
3226 else if (r->prob && r->prob <= karc4random())
3227 r = TAILQ_NEXT(r, entries);
315a7da3 3228 else if (r->match_tag && !pf_match_tag(m, r, &tag))
02742ec6 3229 r = TAILQ_NEXT(r, entries);
315a7da3
JL
3230 else if (r->os_fingerprint != PF_OSFP_ANY &&
3231 (pd->proto != IPPROTO_TCP || !pf_osfp_match(
3232 pf_osfp_fingerprint(pd, m, off, th),
3233 r->os_fingerprint)))
02742ec6
JS
3234 r = TAILQ_NEXT(r, entries);
3235 else {
3236 if (r->tag)
3237 tag = r->tag;
70224baa
JL
3238 if (r->rtableid >= 0)
3239 rtableid = r->rtableid;
02742ec6 3240 if (r->anchor == NULL) {
70224baa 3241 match = 1;
02742ec6
JS
3242 *rm = r;
3243 *am = a;
3244 *rsm = ruleset;
3245 if ((*rm)->quick)
3246 break;
3247 r = TAILQ_NEXT(r, entries);
3248 } else
70224baa
JL
3249 pf_step_into_anchor(&asd, &ruleset,
3250 PF_RULESET_FILTER, &r, &a, &match);
02742ec6 3251 }
70224baa
JL
3252 if (r == NULL && pf_step_out_of_anchor(&asd, &ruleset,
3253 PF_RULESET_FILTER, &r, &a, &match))
3254 break;
02742ec6
JS
3255 }
3256 r = *rm;
3257 a = *am;
3258 ruleset = *rsm;
3259
3260 REASON_SET(&reason, PFRES_MATCH);
3261
315a7da3 3262 if (r->log || (nr != NULL && nr->log)) {
02742ec6 3263 if (rewrite)
315a7da3 3264 m_copyback(m, off, hdrlen, pd->hdr.any);
70224baa
JL
3265 PFLOG_PACKET(kif, h, m, af, direction, reason, r->log ? r : nr,
3266 a, ruleset, pd);
02742ec6
JS
3267 }
3268
3269 if ((r->action == PF_DROP) &&
3270 ((r->rule_flag & PFRULE_RETURNRST) ||
3271 (r->rule_flag & PFRULE_RETURNICMP) ||
3272 (r->rule_flag & PFRULE_RETURN))) {
3273 /* undo NAT changes, if they have taken place */
3274 if (nr != NULL) {
3275 if (direction == PF_OUT) {
315a7da3
JL
3276 switch (pd->proto) {
3277 case IPPROTO_TCP:
3278 pf_change_ap(saddr, &th->th_sport,
3279 pd->ip_sum, &th->th_sum,
3280 &pd->baddr, bport, 0, af);
3281 sport = th->th_sport;
3282 rewrite++;
3283 break;
3284 case IPPROTO_UDP:
3285 pf_change_ap(saddr,
3286 &pd->hdr.udp->uh_sport, pd->ip_sum,
3287 &pd->hdr.udp->uh_sum, &pd->baddr,
3288 bport, 1, af);
3289 sport = pd->hdr.udp->uh_sport;
3290 rewrite++;
3291 break;
3292 case IPPROTO_ICMP:
3293#ifdef INET6
3294 case IPPROTO_ICMPV6:
3295#endif
3296 /* nothing! */
3297 break;
3298 default:
3299 switch (af) {
3300 case AF_INET:
3301 pf_change_a(&saddr->v4.s_addr,
3302 pd->ip_sum,
3303 pd->baddr.v4.s_addr, 0);
3304 break;
3305 case AF_INET6:
3306 PF_ACPY(saddr, &pd->baddr, af);
3307 break;
3308 }
3309 }
02742ec6 3310 } else {
315a7da3
JL
3311 switch (pd->proto) {
3312 case IPPROTO_TCP:
3313 pf_change_ap(daddr, &th->th_dport,
3314 pd->ip_sum, &th->th_sum,
3315 &pd->baddr, bport, 0, af);
3316 dport = th->th_dport;
3317 rewrite++;
3318 break;
3319 case IPPROTO_UDP:
3320 pf_change_ap(daddr,
3321 &pd->hdr.udp->uh_dport, pd->ip_sum,
3322 &pd->hdr.udp->uh_sum, &pd->baddr,
3323 bport, 1, af);
3324 dport = pd->hdr.udp->uh_dport;
3325 rewrite++;
3326 break;
3327 case IPPROTO_ICMP:
3328#ifdef INET6
3329 case IPPROTO_ICMPV6:
3330#endif
3331 /* nothing! */
3332 break;
3333 default:
3334 switch (af) {
3335 case AF_INET:
3336 pf_change_a(&daddr->v4.s_addr,
3337 pd->ip_sum,
3338 pd->baddr.v4.s_addr, 0);
3339 break;
3340 case AF_INET6:
3341 PF_ACPY(daddr, &pd->baddr, af);
3342 break;
3343 }
3344 }
02742ec6
JS
3345 }
3346 }
315a7da3
JL
3347 if (pd->proto == IPPROTO_TCP &&
3348 ((r->rule_flag & PFRULE_RETURNRST) ||
02742ec6
JS
3349 (r->rule_flag & PFRULE_RETURN)) &&
3350 !(th->th_flags & TH_RST)) {
315a7da3
JL
3351 u_int32_t ack = ntohl(th->th_seq) + pd->p_len;
3352 struct ip *h = mtod(m, struct ip *);
02742ec6 3353
315a7da3
JL
3354 if (pf_check_proto_cksum(m, off,
3355 h->ip_len - off, IPPROTO_TCP, AF_INET))
3356 REASON_SET(&reason, PFRES_PROTCKSUM);
3357 else {
3358 if (th->th_flags & TH_SYN)
3359 ack++;
3360 if (th->th_flags & TH_FIN)
3361 ack++;
3362 pf_send_tcp(r, af, pd->dst,
3363 pd->src, th->th_dport, th->th_sport,
3364 ntohl(th->th_ack), ack, TH_RST|TH_ACK, 0, 0,
3365 r->return_ttl, 1, 0, pd->eh, kif->pfik_ifp);
3366 }
02742ec6
JS
3367 } else if ((af == AF_INET) && r->return_icmp)
3368 pf_send_icmp(m, r->return_icmp >> 8,
3369 r->return_icmp & 255, af, r);
3370 else if ((af == AF_INET6) && r->return_icmp6)
3371 pf_send_icmp(m, r->return_icmp6 >> 8,
3372 r->return_icmp6 & 255, af, r);
3373 }
3374
315a7da3 3375 if (r->action == PF_DROP)
02742ec6
JS
3376 return (PF_DROP);
3377
315a7da3 3378 if (pf_tag_packet(m, tag, rtableid)) {
70224baa
JL
3379 REASON_SET(&reason, PFRES_MEMORY);
3380 return (PF_DROP);
3381 }
02742ec6 3382
315a7da3
JL
3383 if (!state_icmp && (r->keep_state || nr != NULL ||
3384 (pd->flags & PFDESC_TCP_NORM))) {
02742ec6 3385 /* create new state */
315a7da3 3386 u_int16_t len = 0;
02742ec6 3387 struct pf_state *s = NULL;
315a7da3 3388 struct pf_state_key *sk = NULL;
02742ec6
JS
3389 struct pf_src_node *sn = NULL;
3390
02742ec6 3391 /* check maximums */
70224baa
JL
3392 if (r->max_states && (r->states >= r->max_states)) {
3393 pf_status.lcounters[LCNT_STATES]++;
3394 REASON_SET(&reason, PFRES_MAXSTATES);
02742ec6 3395 goto cleanup;
70224baa
JL
3396 }
3397 /* src node for filter rule */
02742ec6
JS
3398 if ((r->rule_flag & PFRULE_SRCTRACK ||
3399 r->rpool.opts & PF_POOL_STICKYADDR) &&
70224baa
JL
3400 pf_insert_src_node(&sn, r, saddr, af) != 0) {
3401 REASON_SET(&reason, PFRES_SRCLIMIT);
02742ec6 3402 goto cleanup;
70224baa 3403 }
02742ec6
JS
3404 /* src node for translation rule */
3405 if (nr != NULL && (nr->rpool.opts & PF_POOL_STICKYADDR) &&
3406 ((direction == PF_OUT &&
3407 pf_insert_src_node(&nsn, nr, &pd->baddr, af) != 0) ||
70224baa
JL
3408 (pf_insert_src_node(&nsn, nr, saddr, af) != 0))) {
3409 REASON_SET(&reason, PFRES_SRCLIMIT);
02742ec6 3410 goto cleanup;
70224baa 3411 }
02742ec6
JS
3412 s = pool_get(&pf_state_pl, PR_NOWAIT);
3413 if (s == NULL) {
70224baa 3414 REASON_SET(&reason, PFRES_MEMORY);
02742ec6
JS
3415cleanup:
3416 if (sn != NULL && sn->states == 0 && sn->expire == 0) {
3417 RB_REMOVE(pf_src_tree, &tree_src_tracking, sn);
3418 pf_status.scounters[SCNT_SRC_NODE_REMOVALS]++;
3419 pf_status.src_nodes--;
3420 pool_put(&pf_src_tree_pl, sn);
3421 }
3422 if (nsn != sn && nsn != NULL && nsn->states == 0 &&
3423 nsn->expire == 0) {
3424 RB_REMOVE(pf_src_tree, &tree_src_tracking, nsn);
3425 pf_status.scounters[SCNT_SRC_NODE_REMOVALS]++;
3426 pf_status.src_nodes--;
3427 pool_put(&pf_src_tree_pl, nsn);
3428 }
315a7da3
JL
3429 if (sk != NULL) {
3430 pool_put(&pf_state_key_pl, sk);
3431 }
02742ec6
JS
3432 return (PF_DROP);
3433 }
3434 bzero(s, sizeof(*s));
02742ec6
JS
3435 s->rule.ptr = r;
3436 s->nat_rule.ptr = nr;
02742ec6 3437 s->anchor.ptr = a;
70224baa 3438 STATE_INC_COUNTERS(s);
02742ec6 3439 s->allow_opts = r->allow_opts;
70224baa
JL
3440 s->log = r->log & PF_LOG_ALL;
3441 if (nr != NULL)
3442 s->log |= nr->log & PF_LOG_ALL;
315a7da3
JL
3443 switch (pd->proto) {
3444 case IPPROTO_TCP:
3445 len = pd->tot_len - off - (th->th_off << 2);
3446 s->src.seqlo = ntohl(th->th_seq);
3447 s->src.seqhi = s->src.seqlo + len + 1;
3448 if ((th->th_flags & (TH_SYN|TH_ACK)) ==
3449 TH_SYN && r->keep_state == PF_STATE_MODULATE) {
3450 /* Generate sequence number modulator */
3451 while ((s->src.seqdiff =
3452 pf_new_isn(sk) - s->src.seqlo) == 0)
3453 ;
3454 pf_change_a(&th->th_seq, &th->th_sum,
3455 htonl(s->src.seqlo + s->src.seqdiff), 0);
3456 rewrite = 1;
3457 } else
3458 s->src.seqdiff = 0;
3459 if (th->th_flags & TH_SYN) {
3460 s->src.seqhi++;
3461 s->src.wscale = pf_get_wscale(m, off,
3462 th->th_off, af);
02742ec6 3463 }
315a7da3
JL
3464 s->src.max_win = MAX(ntohs(th->th_win), 1);
3465 if (s->src.wscale & PF_WSCALE_MASK) {
3466 /* Remove scale factor from initial window */
3467 int win = s->src.max_win;
3468 win += 1 << (s->src.wscale & PF_WSCALE_MASK);
3469 s->src.max_win = (win - 1) >>
3470 (s->src.wscale & PF_WSCALE_MASK);
02742ec6 3471 }
315a7da3
JL
3472 if (th->th_flags & TH_FIN)
3473 s->src.seqhi++;
3474 s->dst.seqhi = 1;
3475 s->dst.max_win = 1;
3476 s->src.state = TCPS_SYN_SENT;
3477 s->dst.state = TCPS_CLOSED;
3478 s->timeout = PFTM_TCP_FIRST_PACKET;
70224baa 3479 break;
315a7da3
JL
3480 case IPPROTO_UDP:
3481 s->src.state = PFUDPS_SINGLE;
3482 s->dst.state = PFUDPS_NO_TRAFFIC;
3483 s->timeout = PFTM_UDP_FIRST_PACKET;
3484 break;
3485 case IPPROTO_ICMP:
02742ec6 3486#ifdef INET6
315a7da3
JL
3487 case IPPROTO_ICMPV6:
3488#endif
3489 s->timeout = PFTM_ICMP_FIRST_PACKET;
3490 break;
3491 default:
3492 s->src.state = PFOTHERS_SINGLE;
3493 s->dst.state = PFOTHERS_NO_TRAFFIC;
3494 s->timeout = PFTM_OTHER_FIRST_PACKET;
02742ec6 3495 }
02742ec6 3496
02742ec6
JS
3497 s->creation = time_second;
3498 s->expire = time_second;
315a7da3 3499
02742ec6
JS
3500 if (sn != NULL) {
3501 s->src_node = sn;
3502 s->src_node->states++;
3503 }
3504 if (nsn != NULL) {
3505 PF_ACPY(&nsn->raddr, &pd->naddr, af);
3506 s->nat_src_node = nsn;
3507 s->nat_src_node->states++;
3508 }
315a7da3
JL
3509 if (pd->proto == IPPROTO_TCP) {
3510 if ((pd->flags & PFDESC_TCP_NORM) &&
3511 pf_normalize_tcp_init(m, off, pd, th, &s->src,
3512 &s->dst)) {
3513 REASON_SET(&reason, PFRES_MEMORY);
3514 pf_src_tree_remove_state(s);
3515 STATE_DEC_COUNTERS(s);
3516 pool_put(&pf_state_pl, s);
3517 return (PF_DROP);
3518 }
3519 if ((pd->flags & PFDESC_TCP_NORM) && s->src.scrub &&
3520 pf_normalize_tcp_stateful(m, off, pd, &reason,
3521 th, s, &s->src, &s->dst, &rewrite)) {
3522 /* This really shouldn't happen!!! */
3523 DPFPRINTF(PF_DEBUG_URGENT,
3524 ("pf_normalize_tcp_stateful failed on "
3525 "first pkt"));
3526 pf_normalize_tcp_cleanup(s);
3527 pf_src_tree_remove_state(s);
3528 STATE_DEC_COUNTERS(s);
3529 pool_put(&pf_state_pl, s);
3530 return (PF_DROP);
3531 }
3532 }
3533
3534 if ((sk = pf_alloc_state_key(s)) == NULL) {
3535 REASON_SET(&reason, PFRES_MEMORY);
3536 goto cleanup;
3537 }
3538
3539 sk->proto = pd->proto;
3540 sk->direction = direction;
3541 sk->af = af;
3542 if (direction == PF_OUT) {
3543 PF_ACPY(&sk->gwy.addr, saddr, af);
3544 PF_ACPY(&sk->ext.addr, daddr, af);
3545 switch (pd->proto) {
3546 case IPPROTO_ICMP:
3547#ifdef INET6
3548 case IPPROTO_ICMPV6:
3549#endif
3550 sk->gwy.port = nport;
3551 sk->ext.port = 0;
3552 break;
3553 default:
3554 sk->gwy.port = sport;
3555 sk->ext.port = dport;
3556 }
3557 if (nr != NULL) {
3558 PF_ACPY(&sk->lan.addr, &pd->baddr, af);
3559 sk->lan.port = bport;
3560 } else {
3561 PF_ACPY(&sk->lan.addr, &sk->gwy.addr, af);
3562 sk->lan.port = sk->gwy.port;
3563 }
3564 } else {
3565 PF_ACPY(&sk->lan.addr, daddr, af);
3566 PF_ACPY(&sk->ext.addr, saddr, af);
3567 switch (pd->proto) {
3568 case IPPROTO_ICMP:
3569#ifdef INET6
3570 case IPPROTO_ICMPV6:
3571#endif
3572 sk->lan.port = nport;
3573 sk->ext.port = 0;
3574 break;
3575 default:
3576 sk->lan.port = dport;
3577 sk->ext.port = sport;
3578 }
3579 if (nr != NULL) {
3580 PF_ACPY(&sk->gwy.addr, &pd->baddr, af);
3581 sk->gwy.port = bport;
3582 } else {
3583 PF_ACPY(&sk->gwy.addr, &sk->lan.addr, af);
3584 sk->gwy.port = sk->lan.port;
3585 }
3586 }
3587
3588 s->hash = pf_state_hash(sk);
3589 s->pickup_mode = r->pickup_mode;
3590
3591 pf_set_rt_ifp(s, saddr); /* needs s->state_key set */
3592
02742ec6 3593 if (pf_insert_state(BOUND_IFACE(r, kif), s)) {
315a7da3
JL
3594 if (pd->proto == IPPROTO_TCP)
3595 pf_normalize_tcp_cleanup(s);
70224baa 3596 REASON_SET(&reason, PFRES_STATEINS);
02742ec6 3597 pf_src_tree_remove_state(s);
70224baa 3598 STATE_DEC_COUNTERS(s);
02742ec6
JS
3599 pool_put(&pf_state_pl, s);
3600 return (PF_DROP);
3601 } else
3602 *sm = s;
70224baa
JL
3603 if (tag > 0) {
3604 pf_tag_ref(tag);
3605 s->tag = tag;
3606 }
315a7da3
JL
3607 if (pd->proto == IPPROTO_TCP &&
3608 (th->th_flags & (TH_SYN|TH_ACK)) == TH_SYN &&
3609 r->keep_state == PF_STATE_SYNPROXY) {
3610 s->src.state = PF_TCPS_PROXY_SRC;
3611 if (nr != NULL) {
3612 if (direction == PF_OUT) {
3613 pf_change_ap(saddr, &th->th_sport,
3614 pd->ip_sum, &th->th_sum, &pd->baddr,
3615 bport, 0, af);
3616 sport = th->th_sport;
3617 } else {
3618 pf_change_ap(daddr, &th->th_dport,
3619 pd->ip_sum, &th->th_sum, &pd->baddr,
3620 bport, 0, af);
3621 sport = th->th_dport;
3622 }
3623 }
4fc5aa1c 3624 s->src.seqhi = htonl(karc4random());
315a7da3
JL
3625 /* Find mss option */
3626 mss = pf_get_mss(m, off, th->th_off, af);
3627 mss = pf_calc_mss(saddr, af, mss);
3628 mss = pf_calc_mss(daddr, af, mss);
3629 s->src.mss = mss;
3630 pf_send_tcp(r, af, daddr, saddr, th->th_dport,
3631 th->th_sport, s->src.seqhi, ntohl(th->th_seq) + 1,
3632 TH_SYN|TH_ACK, 0, s->src.mss, 0, 1, 0, NULL, NULL);
3633 REASON_SET(&reason, PFRES_SYNPROXY);
3634 return (PF_SYNPROXY_DROP);
3635 }
02742ec6
JS
3636 }
3637
315a7da3
JL
3638 /* copy back packet headers if we performed NAT operations */
3639 if (rewrite)
3640 m_copyback(m, off, hdrlen, pd->hdr.any);
3641
02742ec6
JS
3642 return (PF_PASS);
3643}
3644
3645int
3646pf_test_fragment(struct pf_rule **rm, int direction, struct pfi_kif *kif,
3647 struct mbuf *m, void *h, struct pf_pdesc *pd, struct pf_rule **am,
3648 struct pf_ruleset **rsm)
3649{
3650 struct pf_rule *r, *a = NULL;
3651 struct pf_ruleset *ruleset = NULL;
3652 sa_family_t af = pd->af;
3653 u_short reason;
3654 int tag = -1;
70224baa
JL
3655 int asd = 0;
3656 int match = 0;
02742ec6
JS
3657
3658 r = TAILQ_FIRST(pf_main_ruleset.rules[PF_RULESET_FILTER].active.ptr);
3659 while (r != NULL) {
3660 r->evaluations++;
70224baa 3661 if (pfi_kif_match(r->kif, kif) == r->ifnot)
02742ec6
JS
3662 r = r->skip[PF_SKIP_IFP].ptr;
3663 else if (r->direction && r->direction != direction)
3664 r = r->skip[PF_SKIP_DIR].ptr;
3665 else if (r->af && r->af != af)
3666 r = r->skip[PF_SKIP_AF].ptr;
3667 else if (r->proto && r->proto != pd->proto)
3668 r = r->skip[PF_SKIP_PROTO].ptr;
70224baa
JL
3669 else if (PF_MISMATCHAW(&r->src.addr, pd->src, af,
3670 r->src.neg, kif))
02742ec6 3671 r = r->skip[PF_SKIP_SRC_ADDR].ptr;
70224baa
JL
3672 else if (PF_MISMATCHAW(&r->dst.addr, pd->dst, af,
3673 r->dst.neg, NULL))
02742ec6 3674 r = r->skip[PF_SKIP_DST_ADDR].ptr;
70224baa 3675 else if (r->tos && !(r->tos == pd->tos))
02742ec6 3676 r = TAILQ_NEXT(r, entries);
a814431a
MD
3677 else if (r->os_fingerprint != PF_OSFP_ANY)
3678 r = TAILQ_NEXT(r, entries);
3679 else if (pd->proto == IPPROTO_UDP &&
3680 (r->src.port_op || r->dst.port_op))
3681 r = TAILQ_NEXT(r, entries);
3682 else if (pd->proto == IPPROTO_TCP &&
3683 (r->src.port_op || r->dst.port_op || r->flagset))
3684 r = TAILQ_NEXT(r, entries);
3685 else if ((pd->proto == IPPROTO_ICMP ||
3686 pd->proto == IPPROTO_ICMPV6) &&
3687 (r->type || r->code))
02742ec6 3688 r = TAILQ_NEXT(r, entries);
75fda04a
MD
3689 else if (r->prob && r->prob <= karc4random())
3690 r = TAILQ_NEXT(r, entries);
315a7da3 3691 else if (r->match_tag && !pf_match_tag(m, r, &tag))
02742ec6 3692 r = TAILQ_NEXT(r, entries);
02742ec6
JS
3693 else {
3694 if (r->anchor == NULL) {
70224baa 3695 match = 1;
02742ec6
JS
3696 *rm = r;
3697 *am = a;
3698 *rsm = ruleset;
3699 if ((*rm)->quick)
3700 break;
3701 r = TAILQ_NEXT(r, entries);
3702 } else
70224baa
JL
3703 pf_step_into_anchor(&asd, &ruleset,
3704 PF_RULESET_FILTER, &r, &a, &match);
02742ec6 3705 }
70224baa
JL
3706 if (r == NULL && pf_step_out_of_anchor(&asd, &ruleset,
3707 PF_RULESET_FILTER, &r, &a, &match))
3708 break;
02742ec6
JS
3709 }
3710 r = *rm;
3711 a = *am;
3712 ruleset = *rsm;
3713
3714 REASON_SET(&reason, PFRES_MATCH);
3715
3716 if (r->log)
70224baa
JL
3717 PFLOG_PACKET(kif, h, m, af, direction, reason, r, a, ruleset,
3718 pd);
02742ec6
JS
3719
3720 if (r->action != PF_PASS)
3721 return (PF_DROP);
3722
315a7da3 3723 if (pf_tag_packet(m, tag, -1)) {
70224baa
JL
3724 REASON_SET(&reason, PFRES_MEMORY);
3725 return (PF_DROP);
3726 }
02742ec6
JS
3727
3728 return (PF_PASS);
3729}
3730
3731int
3732pf_test_state_tcp(struct pf_state **state, int direction, struct pfi_kif *kif,
3733 struct mbuf *m, int off, void *h, struct pf_pdesc *pd,
3734 u_short *reason)
3735{
315a7da3 3736 struct pf_state_key_cmp key;
02742ec6
JS
3737 struct tcphdr *th = pd->hdr.tcp;
3738 u_int16_t win = ntohs(th->th_win);
70224baa 3739 u_int32_t ack, end, seq, orig_seq;
02742ec6
JS
3740 u_int8_t sws, dws;
3741 int ackskew;
3742 int copyback = 0;
3743 struct pf_state_peer *src, *dst;
3744
3745 key.af = pd->af;
3746 key.proto = IPPROTO_TCP;
3747 if (direction == PF_IN) {
3748 PF_ACPY(&key.ext.addr, pd->src, key.af);
3749 PF_ACPY(&key.gwy.addr, pd->dst, key.af);
3750 key.ext.port = th->th_sport;
3751 key.gwy.port = th->th_dport;
3752 } else {
3753 PF_ACPY(&key.lan.addr, pd->src, key.af);
3754 PF_ACPY(&key.ext.addr, pd->dst, key.af);
3755 key.lan.port = th->th_sport;
3756 key.ext.port = th->th_dport;
3757 }
3758
3759 STATE_LOOKUP();
3760
315a7da3 3761 if (direction == (*state)->state_key->direction) {
02742ec6
JS
3762 src = &(*state)->src;
3763 dst = &(*state)->dst;
3764 } else {
3765 src = &(*state)->dst;
3766 dst = &(*state)->src;
3767 }
3768
3769 if ((*state)->src.state == PF_TCPS_PROXY_SRC) {
315a7da3 3770 if (direction != (*state)->state_key->direction) {
70224baa 3771 REASON_SET(reason, PFRES_SYNPROXY);
02742ec6 3772 return (PF_SYNPROXY_DROP);
70224baa 3773 }
02742ec6 3774 if (th->th_flags & TH_SYN) {
70224baa
JL
3775 if (ntohl(th->th_seq) != (*state)->src.seqlo) {
3776 REASON_SET(reason, PFRES_SYNPROXY);
02742ec6 3777 return (PF_DROP);
70224baa 3778 }
02742ec6
JS
3779 pf_send_tcp((*state)->rule.ptr, pd->af, pd->dst,
3780 pd->src, th->th_dport, th->th_sport,
3781 (*state)->src.seqhi, ntohl(th->th_seq) + 1,
70224baa
JL
3782 TH_SYN|TH_ACK, 0, (*state)->src.mss, 0, 1,
3783 0, NULL, NULL);
3784 REASON_SET(reason, PFRES_SYNPROXY);
02742ec6
JS
3785 return (PF_SYNPROXY_DROP);
3786 } else if (!(th->th_flags & TH_ACK) ||
3787 (ntohl(th->th_ack) != (*state)->src.seqhi + 1) ||
70224baa
JL
3788 (ntohl(th->th_seq) != (*state)->src.seqlo + 1)) {
3789 REASON_SET(reason, PFRES_SYNPROXY);
02742ec6 3790 return (PF_DROP);
70224baa
JL
3791 } else if ((*state)->src_node != NULL &&
3792 pf_src_connlimit(state)) {
3793 REASON_SET(reason, PFRES_SRCLIMIT);
3794 return (PF_DROP);
3795 } else
02742ec6
JS
3796 (*state)->src.state = PF_TCPS_PROXY_DST;
3797 }
3798 if ((*state)->src.state == PF_TCPS_PROXY_DST) {
3799 struct pf_state_host *src, *dst;
3800
3801 if (direction == PF_OUT) {
315a7da3
JL
3802 src = &(*state)->state_key->gwy;
3803 dst = &(*state)->state_key->ext;
02742ec6 3804 } else {
315a7da3
JL
3805 src = &(*state)->state_key->ext;
3806 dst = &(*state)->state_key->lan;
02742ec6 3807 }
315a7da3 3808 if (direction == (*state)->state_key->direction) {
02742ec6
JS
3809 if (((th->th_flags & (TH_SYN|TH_ACK)) != TH_ACK) ||
3810 (ntohl(th->th_ack) != (*state)->src.seqhi + 1) ||
70224baa
JL
3811 (ntohl(th->th_seq) != (*state)->src.seqlo + 1)) {
3812 REASON_SET(reason, PFRES_SYNPROXY);
02742ec6 3813 return (PF_DROP);
70224baa 3814 }
02742ec6
JS
3815 (*state)->src.max_win = MAX(ntohs(th->th_win), 1);
3816 if ((*state)->dst.seqhi == 1)
4fc5aa1c 3817 (*state)->dst.seqhi = htonl(karc4random());
02742ec6
JS
3818 pf_send_tcp((*state)->rule.ptr, pd->af, &src->addr,
3819 &dst->addr, src->port, dst->port,
3820 (*state)->dst.seqhi, 0, TH_SYN, 0,
70224baa
JL
3821 (*state)->src.mss, 0, 0, (*state)->tag, NULL, NULL);
3822 REASON_SET(reason, PFRES_SYNPROXY);
02742ec6
JS
3823 return (PF_SYNPROXY_DROP);
3824 } else if (((th->th_flags & (TH_SYN|TH_ACK)) !=
3825 (TH_SYN|TH_ACK)) ||
70224baa
JL
3826 (ntohl(th->th_ack) != (*state)->dst.seqhi + 1)) {
3827 REASON_SET(reason, PFRES_SYNPROXY);
02742ec6 3828 return (PF_DROP);
70224baa 3829 } else {
02742ec6
JS
3830 (*state)->dst.max_win = MAX(ntohs(th->th_win), 1);
3831 (*state)->dst.seqlo = ntohl(th->th_seq);
3832 pf_send_tcp((*state)->rule.ptr, pd->af, pd->dst,
3833 pd->src, th->th_dport, th->th_sport,
3834 ntohl(th->th_ack), ntohl(th->th_seq) + 1,
70224baa
JL
3835 TH_ACK, (*state)->src.max_win, 0, 0, 0,
3836 (*state)->tag, NULL, NULL);
02742ec6
JS
3837 pf_send_tcp((*state)->rule.ptr, pd->af, &src->addr,
3838 &dst->addr, src->port, dst->port,
3839 (*state)->src.seqhi + 1, (*state)->src.seqlo + 1,
70224baa
JL
3840 TH_ACK, (*state)->dst.max_win, 0, 0, 1,
3841 0, NULL, NULL);
02742ec6
JS
3842 (*state)->src.seqdiff = (*state)->dst.seqhi -
3843 (*state)->src.seqlo;
3844 (*state)->dst.seqdiff = (*state)->src.seqhi -
3845 (*state)->dst.seqlo;
3846 (*state)->src.seqhi = (*state)->src.seqlo +
02742ec6 3847 (*state)->dst.max_win;
9aa13ad5
MD
3848 (*state)->dst.seqhi = (*state)->dst.seqlo +
3849 (*state)->src.max_win;
02742ec6
JS
3850 (*state)->src.wscale = (*state)->dst.wscale = 0;
3851 (*state)->src.state = (*state)->dst.state =
3852 TCPS_ESTABLISHED;
70224baa 3853 REASON_SET(reason, PFRES_SYNPROXY);
02742ec6
JS
3854 return (PF_SYNPROXY_DROP);
3855 }
3856 }
3857
3858 if (src->wscale && dst->wscale && !(th->th_flags & TH_SYN)) {
3859 sws = src->wscale & PF_WSCALE_MASK;
3860 dws = dst->wscale & PF_WSCALE_MASK;
3861 } else
3862 sws = dws = 0;
3863
3864 /*
3865 * Sequence tracking algorithm from Guido van Rooij's paper:
3866 * http://www.madison-gurkha.com/publications/tcp_filtering/
3867 * tcp_filtering.ps
3868 */
3869
70224baa 3870 orig_seq = seq = ntohl(th->th_seq);
02742ec6 3871 if (src->seqlo == 0) {
70224baa
JL
3872 /* First packet from this end. Set its state */
3873
02742ec6
JS
3874 if ((pd->flags & PFDESC_TCP_NORM || dst->scrub) &&
3875 src->scrub == NULL) {
3876 if (pf_normalize_tcp_init(m, off, pd, th, src, dst)) {
3877 REASON_SET(reason, PFRES_MEMORY);
3878 return (PF_DROP);
3879 }
3880 }
3881
3882 /* Deferred generation of sequence number modulator */
3883 if (dst->seqdiff && !src->seqdiff) {
315a7da3
JL
3884
3885 while ((src->seqdiff = pf_new_isn((struct pf_state_key *)&key) - seq) == 0)
02742ec6
JS
3886 ;
3887 ack = ntohl(th->th_ack) - dst->seqdiff;
3888 pf_change_a(&th->th_seq, &th->th_sum, htonl(seq +
3889 src->seqdiff), 0);
3890 pf_change_a(&th->th_ack, &th->th_sum, htonl(ack), 0);
3891 copyback = 1;
3892 } else {
3893 ack = ntohl(th->th_ack);
3894 }
3895
3896 end = seq + pd->p_len;
3897 if (th->th_flags & TH_SYN) {
3898 end++;
a814431a 3899 (*state)->sync_flags |= PFSTATE_GOT_SYN2;
02742ec6
JS
3900 if (dst->wscale & PF_WSCALE_FLAG) {
3901 src->wscale = pf_get_wscale(m, off, th->th_off,
3902 pd->af);
3903 if (src->wscale & PF_WSCALE_FLAG) {
3904 /* Remove scale factor from initial
3905 * window */
3906 sws = src->wscale & PF_WSCALE_MASK;
3907 win = ((u_int32_t)win + (1 << sws) - 1)
3908 >> sws;
3909 dws = dst->wscale & PF_WSCALE_MASK;
3910 } else {
70224baa 3911 /* fixup other window */
02742ec6
JS
3912 dst->max_win <<= dst->wscale &
3913