]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/contrib/pf/net/pf.c
MFC r231852,232127:
[FreeBSD/stable/8.git] / sys / contrib / pf / net / pf.c
1 /*      $OpenBSD: pf.c,v 1.527 2007/02/22 15:23:23 pyr Exp $ */
2 /* add: $OpenBSD: pf.c,v 1.559 2007/09/18 18:45:59 markus Exp $ */
3
4 /*
5  * Copyright (c) 2001 Daniel Hartmeier
6  * Copyright (c) 2002,2003 Henning Brauer
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  *    - Redistributions of source code must retain the above copyright
14  *      notice, this list of conditions and the following disclaimer.
15  *    - Redistributions in binary form must reproduce the above
16  *      copyright notice, this list of conditions and the following
17  *      disclaimer in the documentation and/or other materials provided
18  *      with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  *
33  * Effort sponsored in part by the Defense Advanced Research Projects
34  * Agency (DARPA) and Air Force Research Laboratory, Air Force
35  * Materiel Command, USAF, under agreement number F30602-01-2-0537.
36  *
37  */
38
39 #ifdef __FreeBSD__
40 #include "opt_inet.h"
41 #include "opt_inet6.h"
42
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
45 #endif
46
47 #ifdef __FreeBSD__
48 #include "opt_bpf.h"
49 #include "opt_pf.h"
50
51 #ifdef DEV_BPF
52 #define NBPFILTER       DEV_BPF
53 #else
54 #define NBPFILTER       0
55 #endif
56
57 #ifdef DEV_PFLOG
58 #define NPFLOG          DEV_PFLOG
59 #else
60 #define NPFLOG          0
61 #endif
62
63 #ifdef DEV_PFSYNC
64 #define NPFSYNC         DEV_PFSYNC
65 #else
66 #define NPFSYNC         0
67 #endif
68
69 #else
70 #include "bpfilter.h"
71 #include "pflog.h"
72 #include "pfsync.h"
73 #endif
74
75 #include <sys/param.h>
76 #include <sys/systm.h>
77 #include <sys/mbuf.h>
78 #include <sys/filio.h>
79 #include <sys/socket.h>
80 #include <sys/socketvar.h>
81 #include <sys/kernel.h>
82 #include <sys/time.h>
83 #ifdef __FreeBSD__
84 #include <sys/sysctl.h>
85 #include <sys/endian.h>
86 #else
87 #include <sys/pool.h>
88 #endif
89 #include <sys/proc.h>
90 #ifdef __FreeBSD__
91 #include <sys/kthread.h>
92 #include <sys/lock.h>
93 #include <sys/sx.h>
94 #else
95 #include <sys/rwlock.h>
96 #endif
97
98 #include <net/if.h>
99 #include <net/if_types.h>
100 #include <net/bpf.h>
101 #include <net/route.h>
102 #ifndef __FreeBSD__
103 #include <net/radix_mpath.h>
104 #endif
105
106 #include <netinet/in.h>
107 #include <netinet/in_var.h>
108 #include <netinet/in_systm.h>
109 #include <netinet/ip.h>
110 #include <netinet/ip_var.h>
111 #include <netinet/tcp.h>
112 #include <netinet/tcp_seq.h>
113 #include <netinet/udp.h>
114 #include <netinet/ip_icmp.h>
115 #include <netinet/in_pcb.h>
116 #include <netinet/tcp_timer.h>
117 #include <netinet/tcp_var.h>
118 #include <netinet/udp_var.h>
119 #include <netinet/icmp_var.h>
120 #include <netinet/if_ether.h>
121
122 #ifndef __FreeBSD__
123 #include <dev/rndvar.h>
124 #endif
125 #include <net/pfvar.h>
126 #include <net/if_pflog.h>
127
128 #if NPFSYNC > 0
129 #include <net/if_pfsync.h>
130 #endif /* NPFSYNC > 0 */
131
132 #ifdef INET6
133 #include <netinet/ip6.h>
134 #include <netinet/in_pcb.h>
135 #include <netinet/icmp6.h>
136 #include <netinet6/nd6.h>
137 #ifdef __FreeBSD__
138 #include <netinet6/ip6_var.h>
139 #include <netinet6/in6_pcb.h>
140 #endif
141 #endif /* INET6 */
142
143 #ifdef __FreeBSD__
144 #include <machine/in_cksum.h>
145 #include <sys/limits.h>
146 #include <sys/ucred.h>
147 #include <security/mac/mac_framework.h>
148
149 extern int ip_optcopy(struct ip *, struct ip *);
150 extern int debug_pfugidhack;
151 #endif
152
153 #define DPFPRINTF(n, x) if (pf_status.debug >= (n)) printf x
154
155 /*
156  * Global variables
157  */
158
159 struct pf_altqqueue      pf_altqs[2];
160 struct pf_palist         pf_pabuf;
161 struct pf_altqqueue     *pf_altqs_active;
162 struct pf_altqqueue     *pf_altqs_inactive;
163 struct pf_status         pf_status;
164
165 u_int32_t                ticket_altqs_active;
166 u_int32_t                ticket_altqs_inactive;
167 int                      altqs_inactive_open;
168 u_int32_t                ticket_pabuf;
169
170 struct pf_anchor_stackframe {
171         struct pf_ruleset                       *rs;
172         struct pf_rule                          *r;
173         struct pf_anchor_node                   *parent;
174         struct pf_anchor                        *child;
175 } pf_anchor_stack[64];
176
177 #ifdef __FreeBSD__
178 uma_zone_t               pf_src_tree_pl, pf_rule_pl;
179 uma_zone_t               pf_state_pl, pf_altq_pl, pf_pooladdr_pl;
180 #else
181 struct pool              pf_src_tree_pl, pf_rule_pl;
182 struct pool              pf_state_pl, pf_altq_pl, pf_pooladdr_pl;
183 #endif
184
185 void                     pf_print_host(struct pf_addr *, u_int16_t, u_int8_t);
186
187 void                     pf_init_threshold(struct pf_threshold *, u_int32_t,
188                             u_int32_t);
189 void                     pf_add_threshold(struct pf_threshold *);
190 int                      pf_check_threshold(struct pf_threshold *);
191
192 void                     pf_change_ap(struct pf_addr *, u_int16_t *,
193                             u_int16_t *, u_int16_t *, struct pf_addr *,
194                             u_int16_t, u_int8_t, sa_family_t);
195 int                      pf_modulate_sack(struct mbuf *, int, struct pf_pdesc *,
196                             struct tcphdr *, struct pf_state_peer *);
197 #ifdef INET6
198 void                     pf_change_a6(struct pf_addr *, u_int16_t *,
199                             struct pf_addr *, u_int8_t);
200 #endif /* INET6 */
201 void                     pf_change_icmp(struct pf_addr *, u_int16_t *,
202                             struct pf_addr *, struct pf_addr *, u_int16_t,
203                             u_int16_t *, u_int16_t *, u_int16_t *,
204                             u_int16_t *, u_int8_t, sa_family_t);
205 #ifdef __FreeBSD__
206 void                     pf_send_tcp(struct mbuf *,
207                             const struct pf_rule *, sa_family_t,
208 #else
209 void                     pf_send_tcp(const struct pf_rule *, sa_family_t,
210 #endif
211                             const struct pf_addr *, const struct pf_addr *,
212                             u_int16_t, u_int16_t, u_int32_t, u_int32_t,
213                             u_int8_t, u_int16_t, u_int16_t, u_int8_t, int,
214                             u_int16_t, struct ether_header *, struct ifnet *);
215 void                     pf_send_icmp(struct mbuf *, u_int8_t, u_int8_t,
216                             sa_family_t, struct pf_rule *);
217 struct pf_rule          *pf_match_translation(struct pf_pdesc *, struct mbuf *,
218                             int, int, struct pfi_kif *,
219                             struct pf_addr *, u_int16_t, struct pf_addr *,
220                             u_int16_t, int);
221 struct pf_rule          *pf_get_translation(struct pf_pdesc *, struct mbuf *,
222                             int, int, struct pfi_kif *, struct pf_src_node **,
223                             struct pf_addr *, u_int16_t,
224                             struct pf_addr *, u_int16_t,
225                             struct pf_addr *, u_int16_t *);
226 int                      pf_test_tcp(struct pf_rule **, struct pf_state **,
227                             int, struct pfi_kif *, struct mbuf *, int,
228                             void *, struct pf_pdesc *, struct pf_rule **,
229 #ifdef __FreeBSD__
230                             struct pf_ruleset **, struct ifqueue *,
231                             struct inpcb *);
232 #else
233                             struct pf_ruleset **, struct ifqueue *);
234 #endif
235 int                      pf_test_udp(struct pf_rule **, struct pf_state **,
236                             int, struct pfi_kif *, struct mbuf *, int,
237                             void *, struct pf_pdesc *, struct pf_rule **,
238 #ifdef __FreeBSD__
239                             struct pf_ruleset **, struct ifqueue *,
240                             struct inpcb *);
241 #else
242                             struct pf_ruleset **, struct ifqueue *);
243 #endif
244 int                      pf_test_icmp(struct pf_rule **, struct pf_state **,
245                             int, struct pfi_kif *, struct mbuf *, int,
246                             void *, struct pf_pdesc *, struct pf_rule **,
247                             struct pf_ruleset **, struct ifqueue *);
248 int                      pf_test_other(struct pf_rule **, struct pf_state **,
249                             int, struct pfi_kif *, struct mbuf *, int, void *,
250                             struct pf_pdesc *, struct pf_rule **,
251                             struct pf_ruleset **, struct ifqueue *);
252 int                      pf_test_fragment(struct pf_rule **, int,
253                             struct pfi_kif *, struct mbuf *, void *,
254                             struct pf_pdesc *, struct pf_rule **,
255                             struct pf_ruleset **);
256 int                      pf_tcp_track_full(struct pf_state_peer *,
257                             struct pf_state_peer *, struct pf_state **,
258                             struct pfi_kif *, struct mbuf *, int,
259                             struct pf_pdesc *, u_short *, int *);
260 int                      pf_tcp_track_sloppy(struct pf_state_peer *,
261                             struct pf_state_peer *, struct pf_state **,
262                             struct pf_pdesc *, u_short *);
263 int                      pf_test_state_tcp(struct pf_state **, int,
264                             struct pfi_kif *, struct mbuf *, int,
265                             void *, struct pf_pdesc *, u_short *);
266 int                      pf_test_state_udp(struct pf_state **, int,
267                             struct pfi_kif *, struct mbuf *, int,
268                             void *, struct pf_pdesc *);
269 int                      pf_test_state_icmp(struct pf_state **, int,
270                             struct pfi_kif *, struct mbuf *, int,
271                             void *, struct pf_pdesc *, u_short *);
272 int                      pf_test_state_other(struct pf_state **, int,
273                             struct pfi_kif *, struct pf_pdesc *);
274 int                      pf_match_tag(struct mbuf *, struct pf_rule *,
275                              struct pf_mtag *, int *);
276 int                      pf_step_out_of_anchor(int *, struct pf_ruleset **,
277                              int, struct pf_rule **, struct pf_rule **,
278                              int *);
279 void                     pf_hash(struct pf_addr *, struct pf_addr *,
280                             struct pf_poolhashkey *, sa_family_t);
281 int                      pf_map_addr(u_int8_t, struct pf_rule *,
282                             struct pf_addr *, struct pf_addr *,
283                             struct pf_addr *, struct pf_src_node **);
284 int                      pf_get_sport(sa_family_t, u_int8_t, struct pf_rule *,
285                             struct pf_addr *, struct pf_addr *, u_int16_t,
286                             struct pf_addr *, u_int16_t*, u_int16_t, u_int16_t,
287                             struct pf_src_node **);
288 void                     pf_route(struct mbuf **, struct pf_rule *, int,
289                             struct ifnet *, struct pf_state *,
290                             struct pf_pdesc *);
291 void                     pf_route6(struct mbuf **, struct pf_rule *, int,
292                             struct ifnet *, struct pf_state *,
293                             struct pf_pdesc *);
294 #ifdef __FreeBSD__
295 /* XXX: import */
296 #else
297 int                      pf_socket_lookup(int, struct pf_pdesc *);
298 #endif
299 u_int8_t                 pf_get_wscale(struct mbuf *, int, u_int16_t,
300                             sa_family_t);
301 u_int16_t                pf_get_mss(struct mbuf *, int, u_int16_t,
302                             sa_family_t);
303 u_int16_t                pf_calc_mss(struct pf_addr *, sa_family_t,
304                                 u_int16_t);
305 void                     pf_set_rt_ifp(struct pf_state *,
306                             struct pf_addr *);
307 int                      pf_check_proto_cksum(struct mbuf *, int, int,
308                             u_int8_t, sa_family_t);
309 int                      pf_addr_wrap_neq(struct pf_addr_wrap *,
310                             struct pf_addr_wrap *);
311 struct pf_state         *pf_find_state_recurse(struct pfi_kif *,
312                             struct pf_state_cmp *, u_int8_t);
313 int                      pf_src_connlimit(struct pf_state **);
314 int                      pf_check_congestion(struct ifqueue *);
315
316 #ifdef __FreeBSD__
317 int in4_cksum(struct mbuf *m, u_int8_t nxt, int off, int len);
318
319 extern int pf_end_threads;
320
321 struct pf_pool_limit pf_pool_limits[PF_LIMIT_MAX];
322 #else
323 extern struct pool pfr_ktable_pl;
324 extern struct pool pfr_kentry_pl;
325
326 struct pf_pool_limit pf_pool_limits[PF_LIMIT_MAX] = {
327         { &pf_state_pl, PFSTATE_HIWAT },
328         { &pf_src_tree_pl, PFSNODE_HIWAT },
329         { &pf_frent_pl, PFFRAG_FRENT_HIWAT },
330         { &pfr_ktable_pl, PFR_KTABLE_HIWAT },
331         { &pfr_kentry_pl, PFR_KENTRY_HIWAT }
332 };
333 #endif
334
335 #define STATE_LOOKUP()                                                  \
336         do {                                                            \
337                 if (direction == PF_IN)                                 \
338                         *state = pf_find_state_recurse(                 \
339                             kif, &key, PF_EXT_GWY);                     \
340                 else                                                    \
341                         *state = pf_find_state_recurse(                 \
342                             kif, &key, PF_LAN_EXT);                     \
343                 if (*state == NULL || (*state)->timeout == PFTM_PURGE)  \
344                         return (PF_DROP);                               \
345                 if (direction == PF_OUT &&                              \
346                     (((*state)->rule.ptr->rt == PF_ROUTETO &&           \
347                     (*state)->rule.ptr->direction == PF_OUT) ||         \
348                     ((*state)->rule.ptr->rt == PF_REPLYTO &&            \
349                     (*state)->rule.ptr->direction == PF_IN)) &&         \
350                     (*state)->rt_kif != NULL &&                         \
351                     (*state)->rt_kif != kif)                            \
352                         return (PF_PASS);                               \
353         } while (0)
354
355 #define STATE_TRANSLATE(s) \
356         (s)->lan.addr.addr32[0] != (s)->gwy.addr.addr32[0] || \
357         ((s)->af == AF_INET6 && \
358         ((s)->lan.addr.addr32[1] != (s)->gwy.addr.addr32[1] || \
359         (s)->lan.addr.addr32[2] != (s)->gwy.addr.addr32[2] || \
360         (s)->lan.addr.addr32[3] != (s)->gwy.addr.addr32[3])) || \
361         (s)->lan.port != (s)->gwy.port
362
363 #define BOUND_IFACE(r, k) \
364         ((r)->rule_flag & PFRULE_IFBOUND) ? (k) : pfi_all
365
366 #define STATE_INC_COUNTERS(s)                           \
367         do {                                            \
368                 s->rule.ptr->states++;                  \
369                 if (s->anchor.ptr != NULL)              \
370                         s->anchor.ptr->states++;        \
371                 if (s->nat_rule.ptr != NULL)            \
372                         s->nat_rule.ptr->states++;      \
373         } while (0)
374
375 #define STATE_DEC_COUNTERS(s)                           \
376         do {                                            \
377                 if (s->nat_rule.ptr != NULL)            \
378                         s->nat_rule.ptr->states--;      \
379                 if (s->anchor.ptr != NULL)              \
380                         s->anchor.ptr->states--;        \
381                 s->rule.ptr->states--;                  \
382         } while (0)
383
384 struct pf_src_tree tree_src_tracking;
385
386 struct pf_state_tree_id tree_id;
387 struct pf_state_queue state_list;
388
389 #ifdef __FreeBSD__
390 static int pf_src_compare(struct pf_src_node *, struct pf_src_node *);
391 static int pf_state_compare_lan_ext(struct pf_state *, struct pf_state *);
392 static int pf_state_compare_ext_gwy(struct pf_state *, struct pf_state *);
393 static int pf_state_compare_id(struct pf_state *, struct pf_state *);
394 #endif
395
396 RB_GENERATE(pf_src_tree, pf_src_node, entry, pf_src_compare);
397 RB_GENERATE(pf_state_tree_lan_ext, pf_state,
398     u.s.entry_lan_ext, pf_state_compare_lan_ext);
399 RB_GENERATE(pf_state_tree_ext_gwy, pf_state,
400     u.s.entry_ext_gwy, pf_state_compare_ext_gwy);
401 RB_GENERATE(pf_state_tree_id, pf_state,
402     u.s.entry_id, pf_state_compare_id);
403
404 #ifdef __FreeBSD__
405 static int
406 #else
407 static __inline int
408 #endif
409 pf_src_compare(struct pf_src_node *a, struct pf_src_node *b)
410 {
411         int     diff;
412
413         if (a->rule.ptr > b->rule.ptr)
414                 return (1);
415         if (a->rule.ptr < b->rule.ptr)
416                 return (-1);
417         if ((diff = a->af - b->af) != 0)
418                 return (diff);
419         switch (a->af) {
420 #ifdef INET
421         case AF_INET:
422                 if (a->addr.addr32[0] > b->addr.addr32[0])
423                         return (1);
424                 if (a->addr.addr32[0] < b->addr.addr32[0])
425                         return (-1);
426                 break;
427 #endif /* INET */
428 #ifdef INET6
429         case AF_INET6:
430                 if (a->addr.addr32[3] > b->addr.addr32[3])
431                         return (1);
432                 if (a->addr.addr32[3] < b->addr.addr32[3])
433                         return (-1);
434                 if (a->addr.addr32[2] > b->addr.addr32[2])
435                         return (1);
436                 if (a->addr.addr32[2] < b->addr.addr32[2])
437                         return (-1);
438                 if (a->addr.addr32[1] > b->addr.addr32[1])
439                         return (1);
440                 if (a->addr.addr32[1] < b->addr.addr32[1])
441                         return (-1);
442                 if (a->addr.addr32[0] > b->addr.addr32[0])
443                         return (1);
444                 if (a->addr.addr32[0] < b->addr.addr32[0])
445                         return (-1);
446                 break;
447 #endif /* INET6 */
448         }
449         return (0);
450 }
451
452 #ifdef __FreeBSD__
453 static int
454 #else
455 static __inline int
456 #endif
457 pf_state_compare_lan_ext(struct pf_state *a, struct pf_state *b)
458 {
459         int     diff;
460
461         if ((diff = a->proto - b->proto) != 0)
462                 return (diff);
463         if ((diff = a->af - b->af) != 0)
464                 return (diff);
465         switch (a->af) {
466 #ifdef INET
467         case AF_INET:
468                 if (a->lan.addr.addr32[0] > b->lan.addr.addr32[0])
469                         return (1);
470                 if (a->lan.addr.addr32[0] < b->lan.addr.addr32[0])
471                         return (-1);
472                 if (a->ext.addr.addr32[0] > b->ext.addr.addr32[0])
473                         return (1);
474                 if (a->ext.addr.addr32[0] < b->ext.addr.addr32[0])
475                         return (-1);
476                 break;
477 #endif /* INET */
478 #ifdef INET6
479         case AF_INET6:
480                 if (a->lan.addr.addr32[3] > b->lan.addr.addr32[3])
481                         return (1);
482                 if (a->lan.addr.addr32[3] < b->lan.addr.addr32[3])
483                         return (-1);
484                 if (a->ext.addr.addr32[3] > b->ext.addr.addr32[3])
485                         return (1);
486                 if (a->ext.addr.addr32[3] < b->ext.addr.addr32[3])
487                         return (-1);
488                 if (a->lan.addr.addr32[2] > b->lan.addr.addr32[2])
489                         return (1);
490                 if (a->lan.addr.addr32[2] < b->lan.addr.addr32[2])
491                         return (-1);
492                 if (a->ext.addr.addr32[2] > b->ext.addr.addr32[2])
493                         return (1);
494                 if (a->ext.addr.addr32[2] < b->ext.addr.addr32[2])
495                         return (-1);
496                 if (a->lan.addr.addr32[1] > b->lan.addr.addr32[1])
497                         return (1);
498                 if (a->lan.addr.addr32[1] < b->lan.addr.addr32[1])
499                         return (-1);
500                 if (a->ext.addr.addr32[1] > b->ext.addr.addr32[1])
501                         return (1);
502                 if (a->ext.addr.addr32[1] < b->ext.addr.addr32[1])
503                         return (-1);
504                 if (a->lan.addr.addr32[0] > b->lan.addr.addr32[0])
505                         return (1);
506                 if (a->lan.addr.addr32[0] < b->lan.addr.addr32[0])
507                         return (-1);
508                 if (a->ext.addr.addr32[0] > b->ext.addr.addr32[0])
509                         return (1);
510                 if (a->ext.addr.addr32[0] < b->ext.addr.addr32[0])
511                         return (-1);
512                 break;
513 #endif /* INET6 */
514         }
515
516         if ((diff = a->lan.port - b->lan.port) != 0)
517                 return (diff);
518         if ((diff = a->ext.port - b->ext.port) != 0)
519                 return (diff);
520
521         return (0);
522 }
523
524 #ifdef __FreeBSD__
525 static int
526 #else
527 static __inline int
528 #endif
529 pf_state_compare_ext_gwy(struct pf_state *a, struct pf_state *b)
530 {
531         int     diff;
532
533         if ((diff = a->proto - b->proto) != 0)
534                 return (diff);
535         if ((diff = a->af - b->af) != 0)
536                 return (diff);
537         switch (a->af) {
538 #ifdef INET
539         case AF_INET:
540                 if (a->ext.addr.addr32[0] > b->ext.addr.addr32[0])
541                         return (1);
542                 if (a->ext.addr.addr32[0] < b->ext.addr.addr32[0])
543                         return (-1);
544                 if (a->gwy.addr.addr32[0] > b->gwy.addr.addr32[0])
545                         return (1);
546                 if (a->gwy.addr.addr32[0] < b->gwy.addr.addr32[0])
547                         return (-1);
548                 break;
549 #endif /* INET */
550 #ifdef INET6
551         case AF_INET6:
552                 if (a->ext.addr.addr32[3] > b->ext.addr.addr32[3])
553                         return (1);
554                 if (a->ext.addr.addr32[3] < b->ext.addr.addr32[3])
555                         return (-1);
556                 if (a->gwy.addr.addr32[3] > b->gwy.addr.addr32[3])
557                         return (1);
558                 if (a->gwy.addr.addr32[3] < b->gwy.addr.addr32[3])
559                         return (-1);
560                 if (a->ext.addr.addr32[2] > b->ext.addr.addr32[2])
561                         return (1);
562                 if (a->ext.addr.addr32[2] < b->ext.addr.addr32[2])
563                         return (-1);
564                 if (a->gwy.addr.addr32[2] > b->gwy.addr.addr32[2])
565                         return (1);
566                 if (a->gwy.addr.addr32[2] < b->gwy.addr.addr32[2])
567                         return (-1);
568                 if (a->ext.addr.addr32[1] > b->ext.addr.addr32[1])
569                         return (1);
570                 if (a->ext.addr.addr32[1] < b->ext.addr.addr32[1])
571                         return (-1);
572                 if (a->gwy.addr.addr32[1] > b->gwy.addr.addr32[1])
573                         return (1);
574                 if (a->gwy.addr.addr32[1] < b->gwy.addr.addr32[1])
575                         return (-1);
576                 if (a->ext.addr.addr32[0] > b->ext.addr.addr32[0])
577                         return (1);
578                 if (a->ext.addr.addr32[0] < b->ext.addr.addr32[0])
579                         return (-1);
580                 if (a->gwy.addr.addr32[0] > b->gwy.addr.addr32[0])
581                         return (1);
582                 if (a->gwy.addr.addr32[0] < b->gwy.addr.addr32[0])
583                         return (-1);
584                 break;
585 #endif /* INET6 */
586         }
587
588         if ((diff = a->ext.port - b->ext.port) != 0)
589                 return (diff);
590         if ((diff = a->gwy.port - b->gwy.port) != 0)
591                 return (diff);
592
593         return (0);
594 }
595
596 #ifdef __FreeBSD__
597 static int
598 #else
599 static __inline int
600 #endif
601 pf_state_compare_id(struct pf_state *a, struct pf_state *b)
602 {
603         if (a->id > b->id)
604                 return (1);
605         if (a->id < b->id)
606                 return (-1);
607         if (a->creatorid > b->creatorid)
608                 return (1);
609         if (a->creatorid < b->creatorid)
610                 return (-1);
611
612         return (0);
613 }
614
615 #ifdef INET6
616 void
617 pf_addrcpy(struct pf_addr *dst, struct pf_addr *src, sa_family_t af)
618 {
619         switch (af) {
620 #ifdef INET
621         case AF_INET:
622                 dst->addr32[0] = src->addr32[0];
623                 break;
624 #endif /* INET */
625         case AF_INET6:
626                 dst->addr32[0] = src->addr32[0];
627                 dst->addr32[1] = src->addr32[1];
628                 dst->addr32[2] = src->addr32[2];
629                 dst->addr32[3] = src->addr32[3];
630                 break;
631         }
632 }
633 #endif /* INET6 */
634
635 struct pf_state *
636 pf_find_state_byid(struct pf_state_cmp *key)
637 {
638         pf_status.fcounters[FCNT_STATE_SEARCH]++;
639         return (RB_FIND(pf_state_tree_id, &tree_id, (struct pf_state *)key));
640 }
641
642 struct pf_state *
643 pf_find_state_recurse(struct pfi_kif *kif, struct pf_state_cmp *key, u_int8_t tree)
644 {
645         struct pf_state *s;
646
647         pf_status.fcounters[FCNT_STATE_SEARCH]++;
648
649         switch (tree) {
650         case PF_LAN_EXT:
651                 if ((s = RB_FIND(pf_state_tree_lan_ext, &kif->pfik_lan_ext,
652                     (struct pf_state *)key)) != NULL)
653                         return (s);
654                 if ((s = RB_FIND(pf_state_tree_lan_ext, &pfi_all->pfik_lan_ext,
655                     (struct pf_state *)key)) != NULL)
656                         return (s);
657                 return (NULL);
658         case PF_EXT_GWY:
659                 if ((s = RB_FIND(pf_state_tree_ext_gwy, &kif->pfik_ext_gwy,
660                     (struct pf_state *)key)) != NULL)
661                         return (s);
662                 if ((s = RB_FIND(pf_state_tree_ext_gwy, &pfi_all->pfik_ext_gwy,
663                     (struct pf_state *)key)) != NULL)
664                         return (s);
665                 return (NULL);
666         default:
667                 panic("pf_find_state_recurse");
668         }
669 }
670
671 struct pf_state *
672 pf_find_state_all(struct pf_state_cmp *key, u_int8_t tree, int *more)
673 {
674         struct pf_state *s, *ss = NULL;
675         struct pfi_kif  *kif;
676
677         pf_status.fcounters[FCNT_STATE_SEARCH]++;
678
679         switch (tree) {
680         case PF_LAN_EXT:
681                 TAILQ_FOREACH(kif, &pfi_statehead, pfik_w_states) {
682                         s = RB_FIND(pf_state_tree_lan_ext,
683                             &kif->pfik_lan_ext, (struct pf_state *)key);
684                         if (s == NULL)
685                                 continue;
686                         if (more == NULL)
687                                 return (s);
688                         ss = s;
689                         (*more)++;
690                 }
691                 return (ss);
692         case PF_EXT_GWY:
693                 TAILQ_FOREACH(kif, &pfi_statehead, pfik_w_states) {
694                         s = RB_FIND(pf_state_tree_ext_gwy,
695                             &kif->pfik_ext_gwy, (struct pf_state *)key);
696                         if (s == NULL)
697                                 continue;
698                         if (more == NULL)
699                                 return (s);
700                         ss = s;
701                         (*more)++;
702                 }
703                 return (ss);
704         default:
705                 panic("pf_find_state_all");
706         }
707 }
708
709 void
710 pf_init_threshold(struct pf_threshold *threshold,
711     u_int32_t limit, u_int32_t seconds)
712 {
713         threshold->limit = limit * PF_THRESHOLD_MULT;
714         threshold->seconds = seconds;
715         threshold->count = 0;
716         threshold->last = time_second;
717 }
718
719 void
720 pf_add_threshold(struct pf_threshold *threshold)
721 {
722         u_int32_t t = time_second, diff = t - threshold->last;
723
724         if (diff >= threshold->seconds)
725                 threshold->count = 0;
726         else
727                 threshold->count -= threshold->count * diff /
728                     threshold->seconds;
729         threshold->count += PF_THRESHOLD_MULT;
730         threshold->last = t;
731 }
732
733 int
734 pf_check_threshold(struct pf_threshold *threshold)
735 {
736         return (threshold->count > threshold->limit);
737 }
738
739 int
740 pf_src_connlimit(struct pf_state **state)
741 {
742         struct pf_state *s;
743         int bad = 0;
744
745         (*state)->src_node->conn++;
746         (*state)->src.tcp_est = 1;
747         pf_add_threshold(&(*state)->src_node->conn_rate);
748
749         if ((*state)->rule.ptr->max_src_conn &&
750             (*state)->rule.ptr->max_src_conn <
751             (*state)->src_node->conn) {
752                 pf_status.lcounters[LCNT_SRCCONN]++;
753                 bad++;
754         }
755
756         if ((*state)->rule.ptr->max_src_conn_rate.limit &&
757             pf_check_threshold(&(*state)->src_node->conn_rate)) {
758                 pf_status.lcounters[LCNT_SRCCONNRATE]++;
759                 bad++;
760         }
761
762         if (!bad)
763                 return (0);
764
765         if ((*state)->rule.ptr->overload_tbl) {
766                 struct pfr_addr p;
767                 u_int32_t       killed = 0;
768
769                 pf_status.lcounters[LCNT_OVERLOAD_TABLE]++;
770                 if (pf_status.debug >= PF_DEBUG_MISC) {
771                         printf("pf_src_connlimit: blocking address ");
772                         pf_print_host(&(*state)->src_node->addr, 0,
773                             (*state)->af);
774                 }
775
776                 bzero(&p, sizeof(p));
777                 p.pfra_af = (*state)->af;
778                 switch ((*state)->af) {
779 #ifdef INET
780                 case AF_INET:
781                         p.pfra_net = 32;
782                         p.pfra_ip4addr = (*state)->src_node->addr.v4;
783                         break;
784 #endif /* INET */
785 #ifdef INET6
786                 case AF_INET6:
787                         p.pfra_net = 128;
788                         p.pfra_ip6addr = (*state)->src_node->addr.v6;
789                         break;
790 #endif /* INET6 */
791                 }
792
793                 pfr_insert_kentry((*state)->rule.ptr->overload_tbl,
794                     &p, time_second);
795
796                 /* kill existing states if that's required. */
797                 if ((*state)->rule.ptr->flush) {
798                         pf_status.lcounters[LCNT_OVERLOAD_FLUSH]++;
799
800                         RB_FOREACH(s, pf_state_tree_id, &tree_id) {
801                                 /*
802                                  * Kill states from this source.  (Only those
803                                  * from the same rule if PF_FLUSH_GLOBAL is not
804                                  * set)
805                                  */
806                                 if (s->af == (*state)->af &&
807                                     (((*state)->direction == PF_OUT &&
808                                     PF_AEQ(&(*state)->src_node->addr,
809                                     &s->lan.addr, s->af)) ||
810                                     ((*state)->direction == PF_IN &&
811                                     PF_AEQ(&(*state)->src_node->addr,
812                                     &s->ext.addr, s->af))) &&
813                                     ((*state)->rule.ptr->flush &
814                                     PF_FLUSH_GLOBAL ||
815                                     (*state)->rule.ptr == s->rule.ptr)) {
816                                         s->timeout = PFTM_PURGE;
817                                         s->src.state = s->dst.state =
818                                             TCPS_CLOSED;
819                                         killed++;
820                                 }
821                         }
822                         if (pf_status.debug >= PF_DEBUG_MISC)
823                                 printf(", %u states killed", killed);
824                 }
825                 if (pf_status.debug >= PF_DEBUG_MISC)
826                         printf("\n");
827         }
828
829         /* kill this state */
830         (*state)->timeout = PFTM_PURGE;
831         (*state)->src.state = (*state)->dst.state = TCPS_CLOSED;
832         return (1);
833 }
834
835 int
836 pf_insert_src_node(struct pf_src_node **sn, struct pf_rule *rule,
837     struct pf_addr *src, sa_family_t af)
838 {
839         struct pf_src_node      k;
840
841         if (*sn == NULL) {
842                 k.af = af;
843                 PF_ACPY(&k.addr, src, af);
844                 if (rule->rule_flag & PFRULE_RULESRCTRACK ||
845                     rule->rpool.opts & PF_POOL_STICKYADDR)
846                         k.rule.ptr = rule;
847                 else
848                         k.rule.ptr = NULL;
849                 pf_status.scounters[SCNT_SRC_NODE_SEARCH]++;
850                 *sn = RB_FIND(pf_src_tree, &tree_src_tracking, &k);
851         }
852         if (*sn == NULL) {
853                 if (!rule->max_src_nodes ||
854                     rule->src_nodes < rule->max_src_nodes)
855                         (*sn) = pool_get(&pf_src_tree_pl, PR_NOWAIT);
856                 else
857                         pf_status.lcounters[LCNT_SRCNODES]++;
858                 if ((*sn) == NULL)
859                         return (-1);
860                 bzero(*sn, sizeof(struct pf_src_node));
861
862                 pf_init_threshold(&(*sn)->conn_rate,
863                     rule->max_src_conn_rate.limit,
864                     rule->max_src_conn_rate.seconds);
865
866                 (*sn)->af = af;
867                 if (rule->rule_flag & PFRULE_RULESRCTRACK ||
868                     rule->rpool.opts & PF_POOL_STICKYADDR)
869                         (*sn)->rule.ptr = rule;
870                 else
871                         (*sn)->rule.ptr = NULL;
872                 PF_ACPY(&(*sn)->addr, src, af);
873                 if (RB_INSERT(pf_src_tree,
874                     &tree_src_tracking, *sn) != NULL) {
875                         if (pf_status.debug >= PF_DEBUG_MISC) {
876                                 printf("pf: src_tree insert failed: ");
877                                 pf_print_host(&(*sn)->addr, 0, af);
878                                 printf("\n");
879                         }
880                         pool_put(&pf_src_tree_pl, *sn);
881                         return (-1);
882                 }
883                 (*sn)->creation = time_second;
884                 (*sn)->ruletype = rule->action;
885                 if ((*sn)->rule.ptr != NULL)
886                         (*sn)->rule.ptr->src_nodes++;
887                 pf_status.scounters[SCNT_SRC_NODE_INSERT]++;
888                 pf_status.src_nodes++;
889         } else {
890                 if (rule->max_src_states &&
891                     (*sn)->states >= rule->max_src_states) {
892                         pf_status.lcounters[LCNT_SRCSTATES]++;
893                         return (-1);
894                 }
895         }
896         return (0);
897 }
898
899 int
900 pf_insert_state(struct pfi_kif *kif, struct pf_state *state)
901 {
902         /* Thou MUST NOT insert multiple duplicate keys */
903         state->u.s.kif = kif;
904         if (RB_INSERT(pf_state_tree_lan_ext, &kif->pfik_lan_ext, state)) {
905                 if (pf_status.debug >= PF_DEBUG_MISC) {
906                         printf("pf: state insert failed: tree_lan_ext");
907                         printf(" lan: ");
908                         pf_print_host(&state->lan.addr, state->lan.port,
909                             state->af);
910                         printf(" gwy: ");
911                         pf_print_host(&state->gwy.addr, state->gwy.port,
912                             state->af);
913                         printf(" ext: ");
914                         pf_print_host(&state->ext.addr, state->ext.port,
915                             state->af);
916                         if (state->sync_flags & PFSTATE_FROMSYNC)
917                                 printf(" (from sync)");
918                         printf("\n");
919                 }
920                 return (-1);
921         }
922
923         if (RB_INSERT(pf_state_tree_ext_gwy, &kif->pfik_ext_gwy, state)) {
924                 if (pf_status.debug >= PF_DEBUG_MISC) {
925                         printf("pf: state insert failed: tree_ext_gwy");
926                         printf(" lan: ");
927                         pf_print_host(&state->lan.addr, state->lan.port,
928                             state->af);
929                         printf(" gwy: ");
930                         pf_print_host(&state->gwy.addr, state->gwy.port,
931                             state->af);
932                         printf(" ext: ");
933                         pf_print_host(&state->ext.addr, state->ext.port,
934                             state->af);
935                         if (state->sync_flags & PFSTATE_FROMSYNC)
936                                 printf(" (from sync)");
937                         printf("\n");
938                 }
939                 RB_REMOVE(pf_state_tree_lan_ext, &kif->pfik_lan_ext, state);
940                 return (-1);
941         }
942
943         if (state->id == 0 && state->creatorid == 0) {
944                 state->id = htobe64(pf_status.stateid++);
945                 state->creatorid = pf_status.hostid;
946         }
947         if (RB_INSERT(pf_state_tree_id, &tree_id, state) != NULL) {
948                 if (pf_status.debug >= PF_DEBUG_MISC) {
949 #ifdef __FreeBSD__
950                         printf("pf: state insert failed: "
951                             "id: %016llx creatorid: %08x",
952                             (long long)be64toh(state->id),
953                             ntohl(state->creatorid));
954 #else
955                         printf("pf: state insert failed: "
956                             "id: %016llx creatorid: %08x",
957                             betoh64(state->id), ntohl(state->creatorid));
958 #endif
959                         if (state->sync_flags & PFSTATE_FROMSYNC)
960                                 printf(" (from sync)");
961                         printf("\n");
962                 }
963                 RB_REMOVE(pf_state_tree_lan_ext, &kif->pfik_lan_ext, state);
964                 RB_REMOVE(pf_state_tree_ext_gwy, &kif->pfik_ext_gwy, state);
965                 return (-1);
966         }
967         TAILQ_INSERT_TAIL(&state_list, state, u.s.entry_list);
968         pf_status.fcounters[FCNT_STATE_INSERT]++;
969         pf_status.states++;
970         pfi_kif_ref(kif, PFI_KIF_REF_STATE);
971 #if NPFSYNC
972         pfsync_insert_state(state);
973 #endif
974         return (0);
975 }
976
977 void
978 pf_purge_thread(void *v)
979 {
980         int nloops = 0, s;
981 #ifdef __FreeBSD__
982         int locked;
983 #endif
984
985         for (;;) {
986                 tsleep(pf_purge_thread, PWAIT, "pftm", 1 * hz);
987
988 #ifdef __FreeBSD__
989                 sx_slock(&pf_consistency_lock);
990                 PF_LOCK();
991                 locked = 0;
992
993                 if (pf_end_threads) {
994                         PF_UNLOCK();
995                         sx_sunlock(&pf_consistency_lock);
996                         sx_xlock(&pf_consistency_lock);
997                         PF_LOCK();
998                         pf_purge_expired_states(pf_status.states, 1);
999                         pf_purge_expired_fragments();
1000                         pf_purge_expired_src_nodes(1);
1001                         pf_end_threads++;
1002
1003                         sx_xunlock(&pf_consistency_lock);
1004                         PF_UNLOCK();
1005                         wakeup(pf_purge_thread);
1006                         kproc_exit(0);
1007                 }
1008 #endif
1009                 s = splsoftnet();
1010
1011                 /* process a fraction of the state table every second */
1012 #ifdef __FreeBSD__
1013                 if(!pf_purge_expired_states(1 + (pf_status.states
1014                     / pf_default_rule.timeout[PFTM_INTERVAL]), 0)) {
1015                         PF_UNLOCK();
1016                         sx_sunlock(&pf_consistency_lock);
1017                         sx_xlock(&pf_consistency_lock);
1018                         PF_LOCK();
1019                         locked = 1;
1020
1021                         pf_purge_expired_states(1 + (pf_status.states
1022                             / pf_default_rule.timeout[PFTM_INTERVAL]), 1);
1023                 }
1024 #else
1025                 pf_purge_expired_states(1 + (pf_status.states
1026                     / pf_default_rule.timeout[PFTM_INTERVAL]));
1027 #endif
1028
1029                 /* purge other expired types every PFTM_INTERVAL seconds */
1030                 if (++nloops >= pf_default_rule.timeout[PFTM_INTERVAL]) {
1031                         pf_purge_expired_fragments();
1032                         if (!pf_purge_expired_src_nodes(locked)) {
1033                                 PF_UNLOCK();
1034                                 sx_sunlock(&pf_consistency_lock);
1035                                 sx_xlock(&pf_consistency_lock);
1036                                 PF_LOCK();
1037                                 locked = 1;
1038                                 pf_purge_expired_src_nodes(1);
1039                         }
1040                         nloops = 0;
1041                 }
1042
1043                 splx(s);
1044 #ifdef __FreeBSD__
1045                 PF_UNLOCK();
1046                 if (locked)
1047                         sx_xunlock(&pf_consistency_lock);
1048                 else
1049                         sx_sunlock(&pf_consistency_lock);
1050 #endif
1051         }
1052 }
1053
1054 u_int32_t
1055 pf_state_expires(const struct pf_state *state)
1056 {
1057         u_int32_t       timeout;
1058         u_int32_t       start;
1059         u_int32_t       end;
1060         u_int32_t       states;
1061
1062         /* handle all PFTM_* > PFTM_MAX here */
1063         if (state->timeout == PFTM_PURGE)
1064                 return (time_second);
1065         if (state->timeout == PFTM_UNTIL_PACKET)
1066                 return (0);
1067 #ifdef __FreeBSD__      
1068         KASSERT(state->timeout != PFTM_UNLINKED,
1069             ("pf_state_expires: timeout == PFTM_UNLINKED"));
1070         KASSERT((state->timeout < PFTM_MAX), 
1071             ("pf_state_expires: timeout > PFTM_MAX"));
1072 #else
1073         KASSERT(state->timeout != PFTM_UNLINKED);
1074         KASSERT(state->timeout < PFTM_MAX);
1075 #endif
1076         timeout = state->rule.ptr->timeout[state->timeout];
1077         if (!timeout)
1078                 timeout = pf_default_rule.timeout[state->timeout];
1079         start = state->rule.ptr->timeout[PFTM_ADAPTIVE_START];
1080         if (start) {
1081                 end = state->rule.ptr->timeout[PFTM_ADAPTIVE_END];
1082                 states = state->rule.ptr->states;
1083         } else {
1084                 start = pf_default_rule.timeout[PFTM_ADAPTIVE_START];
1085                 end = pf_default_rule.timeout[PFTM_ADAPTIVE_END];
1086                 states = pf_status.states;
1087         }
1088         if (end && states > start && start < end) {
1089                 if (states < end)
1090                         return (state->expire + timeout * (end - states) /
1091                             (end - start));
1092                 else
1093                         return (time_second);
1094         }
1095         return (state->expire + timeout);
1096 }
1097
1098 #ifdef __FreeBSD__
1099 int
1100 pf_purge_expired_src_nodes(int waslocked)
1101 #else
1102 void
1103 pf_purge_expired_src_nodes(int waslocked)
1104 #endif
1105 {
1106          struct pf_src_node             *cur, *next;
1107          int                             locked = waslocked;
1108
1109          for (cur = RB_MIN(pf_src_tree, &tree_src_tracking); cur; cur = next) {
1110                  next = RB_NEXT(pf_src_tree, &tree_src_tracking, cur);
1111
1112                  if (cur->states <= 0 && cur->expire <= time_second) {
1113                          if (! locked) {
1114 #ifdef __FreeBSD__
1115                                  if (!sx_try_upgrade(&pf_consistency_lock))
1116                                         return (0);
1117 #else
1118                                  rw_enter_write(&pf_consistency_lock);
1119 #endif
1120                                  next = RB_NEXT(pf_src_tree,
1121                                      &tree_src_tracking, cur);
1122                                  locked = 1;
1123                          }
1124                          if (cur->rule.ptr != NULL) {
1125                                  cur->rule.ptr->src_nodes--;
1126                                  if (cur->rule.ptr->states <= 0 &&
1127                                      cur->rule.ptr->max_src_nodes <= 0)
1128                                          pf_rm_rule(NULL, cur->rule.ptr);
1129                          }
1130                          RB_REMOVE(pf_src_tree, &tree_src_tracking, cur);
1131                          pf_status.scounters[SCNT_SRC_NODE_REMOVALS]++;
1132                          pf_status.src_nodes--;
1133                          pool_put(&pf_src_tree_pl, cur);
1134                  }
1135          }
1136
1137          if (locked && !waslocked)
1138 #ifdef __FreeBSD__
1139                 sx_downgrade(&pf_consistency_lock);
1140 #else
1141                 rw_exit_write(&pf_consistency_lock);
1142 #endif
1143
1144 #ifdef __FreeBSD__
1145         return (1);
1146 #endif
1147 }
1148
1149 void
1150 pf_src_tree_remove_state(struct pf_state *s)
1151 {
1152         u_int32_t timeout;
1153
1154         if (s->src_node != NULL) {
1155                 if (s->proto == IPPROTO_TCP) {
1156                         if (s->src.tcp_est)
1157                                 --s->src_node->conn;
1158                 }
1159                 if (--s->src_node->states <= 0) {
1160                         timeout = s->rule.ptr->timeout[PFTM_SRC_NODE];
1161                         if (!timeout)
1162                                 timeout =
1163                                     pf_default_rule.timeout[PFTM_SRC_NODE];
1164                         s->src_node->expire = time_second + timeout;
1165                 }
1166         }
1167         if (s->nat_src_node != s->src_node && s->nat_src_node != NULL) {
1168                 if (--s->nat_src_node->states <= 0) {
1169                         timeout = s->rule.ptr->timeout[PFTM_SRC_NODE];
1170                         if (!timeout)
1171                                 timeout =
1172                                     pf_default_rule.timeout[PFTM_SRC_NODE];
1173                         s->nat_src_node->expire = time_second + timeout;
1174                 }
1175         }
1176         s->src_node = s->nat_src_node = NULL;
1177 }
1178
1179 /* callers should be at splsoftnet */
1180 void
1181 pf_unlink_state(struct pf_state *cur)
1182 {
1183 #ifdef __FreeBSD__
1184         if (cur->local_flags & PFSTATE_EXPIRING)
1185                 return;
1186         cur->local_flags |= PFSTATE_EXPIRING;
1187 #endif
1188         if (cur->src.state == PF_TCPS_PROXY_DST) {
1189 #ifdef __FreeBSD__
1190                 pf_send_tcp(NULL, cur->rule.ptr, cur->af,
1191 #else
1192                 pf_send_tcp(cur->rule.ptr, cur->af,
1193 #endif
1194                     &cur->ext.addr, &cur->lan.addr,
1195                     cur->ext.port, cur->lan.port,
1196                     cur->src.seqhi, cur->src.seqlo + 1,
1197                     TH_RST|TH_ACK, 0, 0, 0, 1, cur->tag, NULL, NULL);
1198         }
1199         RB_REMOVE(pf_state_tree_ext_gwy,
1200             &cur->u.s.kif->pfik_ext_gwy, cur);
1201         RB_REMOVE(pf_state_tree_lan_ext,
1202             &cur->u.s.kif->pfik_lan_ext, cur);
1203         RB_REMOVE(pf_state_tree_id, &tree_id, cur);
1204 #if NPFSYNC
1205         if (cur->creatorid == pf_status.hostid)
1206                 pfsync_delete_state(cur);
1207 #endif
1208         cur->timeout = PFTM_UNLINKED;
1209         pf_src_tree_remove_state(cur);
1210 }
1211
1212 /* callers should be at splsoftnet and hold the
1213  * write_lock on pf_consistency_lock */
1214 void
1215 pf_free_state(struct pf_state *cur)
1216 {
1217 #if NPFSYNC
1218         if (pfsyncif != NULL &&
1219             (pfsyncif->sc_bulk_send_next == cur ||
1220             pfsyncif->sc_bulk_terminator == cur))
1221                 return;
1222 #endif
1223 #ifdef __FreeBSD__
1224         KASSERT(cur->timeout == PFTM_UNLINKED,
1225             ("pf_free_state: cur->timeout != PFTM_UNLINKED"));
1226 #else
1227         KASSERT(cur->timeout == PFTM_UNLINKED);
1228 #endif
1229         if (--cur->rule.ptr->states <= 0 &&
1230             cur->rule.ptr->src_nodes <= 0)
1231                 pf_rm_rule(NULL, cur->rule.ptr);
1232         if (cur->nat_rule.ptr != NULL)
1233                 if (--cur->nat_rule.ptr->states <= 0 &&
1234                         cur->nat_rule.ptr->src_nodes <= 0)
1235                         pf_rm_rule(NULL, cur->nat_rule.ptr);
1236         if (cur->anchor.ptr != NULL)
1237                 if (--cur->anchor.ptr->states <= 0)
1238                         pf_rm_rule(NULL, cur->anchor.ptr);
1239         pf_normalize_tcp_cleanup(cur);
1240         pfi_kif_unref(cur->u.s.kif, PFI_KIF_REF_STATE);
1241         TAILQ_REMOVE(&state_list, cur, u.s.entry_list);
1242         if (cur->tag)
1243                 pf_tag_unref(cur->tag);
1244         pool_put(&pf_state_pl, cur);
1245         pf_status.fcounters[FCNT_STATE_REMOVALS]++;
1246         pf_status.states--;
1247 }
1248
1249 #ifdef __FreeBSD__
1250 int
1251 pf_purge_expired_states(u_int32_t maxcheck, int waslocked)
1252 #else
1253 void
1254 pf_purge_expired_states(u_int32_t maxcheck)
1255 #endif
1256 {
1257         static struct pf_state  *cur = NULL;
1258         struct pf_state         *next;
1259 #ifdef __FreeBSD__
1260         int                      locked = waslocked;
1261 #else
1262         int                      locked = 0;
1263 #endif
1264
1265         while (maxcheck--) {
1266                 /* wrap to start of list when we hit the end */
1267                 if (cur == NULL) {
1268                         cur = TAILQ_FIRST(&state_list);
1269                         if (cur == NULL)
1270                                 break;  /* list empty */
1271                 }
1272
1273                 /* get next state, as cur may get deleted */
1274                 next = TAILQ_NEXT(cur, u.s.entry_list);
1275
1276                 if (cur->timeout == PFTM_UNLINKED) {
1277                         /* free unlinked state */
1278                         if (! locked) {
1279 #ifdef __FreeBSD__
1280                                  if (!sx_try_upgrade(&pf_consistency_lock))
1281                                         return (0);
1282 #else
1283                                 rw_enter_write(&pf_consistency_lock);
1284 #endif
1285                                 locked = 1;
1286                         }
1287                         pf_free_state(cur);
1288                 } else if (pf_state_expires(cur) <= time_second) {
1289                         /* unlink and free expired state */
1290                         pf_unlink_state(cur);
1291                         if (! locked) {
1292 #ifdef __FreeBSD__
1293                                  if (!sx_try_upgrade(&pf_consistency_lock))
1294                                         return (0);
1295 #else
1296                                 rw_enter_write(&pf_consistency_lock);
1297 #endif
1298                                 locked = 1;
1299                         }
1300                         pf_free_state(cur);
1301                 }
1302                 cur = next;
1303         }
1304
1305 #ifdef __FreeBSD__
1306         if (!waslocked && locked)
1307                 sx_downgrade(&pf_consistency_lock);
1308
1309         return (1);
1310 #else
1311         if (locked)
1312                 rw_exit_write(&pf_consistency_lock);
1313 #endif
1314 }
1315
1316 int
1317 pf_tbladdr_setup(struct pf_ruleset *rs, struct pf_addr_wrap *aw)
1318 {
1319         if (aw->type != PF_ADDR_TABLE)
1320                 return (0);
1321         if ((aw->p.tbl = pfr_attach_table(rs, aw->v.tblname)) == NULL)
1322                 return (1);
1323         return (0);
1324 }
1325
1326 void
1327 pf_tbladdr_remove(struct pf_addr_wrap *aw)
1328 {
1329         if (aw->type != PF_ADDR_TABLE || aw->p.tbl == NULL)
1330                 return;
1331         pfr_detach_table(aw->p.tbl);
1332         aw->p.tbl = NULL;
1333 }
1334
1335 void
1336 pf_tbladdr_copyout(struct pf_addr_wrap *aw)
1337 {
1338         struct pfr_ktable *kt = aw->p.tbl;
1339
1340         if (aw->type != PF_ADDR_TABLE || kt == NULL)
1341                 return;
1342         if (!(kt->pfrkt_flags & PFR_TFLAG_ACTIVE) && kt->pfrkt_root != NULL)
1343                 kt = kt->pfrkt_root;
1344         aw->p.tbl = NULL;
1345         aw->p.tblcnt = (kt->pfrkt_flags & PFR_TFLAG_ACTIVE) ?
1346                 kt->pfrkt_cnt : -1;
1347 }
1348
1349 void
1350 pf_print_host(struct pf_addr *addr, u_int16_t p, sa_family_t af)
1351 {
1352         switch (af) {
1353 #ifdef INET
1354         case AF_INET: {
1355                 u_int32_t a = ntohl(addr->addr32[0]);
1356                 printf("%u.%u.%u.%u", (a>>24)&255, (a>>16)&255,
1357                     (a>>8)&255, a&255);
1358                 if (p) {
1359                         p = ntohs(p);
1360                         printf(":%u", p);
1361                 }
1362                 break;
1363         }
1364 #endif /* INET */
1365 #ifdef INET6
1366         case AF_INET6: {
1367                 u_int16_t b;
1368                 u_int8_t i, curstart = 255, curend = 0,
1369                     maxstart = 0, maxend = 0;
1370                 for (i = 0; i < 8; i++) {
1371                         if (!addr->addr16[i]) {
1372                                 if (curstart == 255)
1373                                         curstart = i;
1374                                 else
1375                                         curend = i;
1376                         } else {
1377                                 if (curstart) {
1378                                         if ((curend - curstart) >
1379                                             (maxend - maxstart)) {
1380                                                 maxstart = curstart;
1381                                                 maxend = curend;
1382                                                 curstart = 255;
1383                                         }
1384                                 }
1385                         }
1386                 }
1387                 for (i = 0; i < 8; i++) {
1388                         if (i >= maxstart && i <= maxend) {
1389                                 if (maxend != 7) {
1390                                         if (i == maxstart)
1391                                                 printf(":");
1392                                 } else {
1393                                         if (i == maxend)
1394                                                 printf(":");
1395                                 }
1396                         } else {
1397                                 b = ntohs(addr->addr16[i]);
1398                                 printf("%x", b);
1399                                 if (i < 7)
1400                                         printf(":");
1401                         }
1402                 }
1403                 if (p) {
1404                         p = ntohs(p);
1405                         printf("[%u]", p);
1406                 }
1407                 break;
1408         }
1409 #endif /* INET6 */
1410         }
1411 }
1412
1413 void
1414 pf_print_state(struct pf_state *s)
1415 {
1416         switch (s->proto) {
1417         case IPPROTO_TCP:
1418                 printf("TCP ");
1419                 break;
1420         case IPPROTO_UDP:
1421                 printf("UDP ");
1422                 break;
1423         case IPPROTO_ICMP:
1424                 printf("ICMP ");
1425                 break;
1426         case IPPROTO_ICMPV6:
1427                 printf("ICMPV6 ");
1428                 break;
1429         default:
1430                 printf("%u ", s->proto);
1431                 break;
1432         }
1433         pf_print_host(&s->lan.addr, s->lan.port, s->af);
1434         printf(" ");
1435         pf_print_host(&s->gwy.addr, s->gwy.port, s->af);
1436         printf(" ");
1437         pf_print_host(&s->ext.addr, s->ext.port, s->af);
1438         printf(" [lo=%u high=%u win=%u modulator=%u", s->src.seqlo,
1439             s->src.seqhi, s->src.max_win, s->src.seqdiff);
1440         if (s->src.wscale && s->dst.wscale)
1441                 printf(" wscale=%u", s->src.wscale & PF_WSCALE_MASK);
1442         printf("]");
1443         printf(" [lo=%u high=%u win=%u modulator=%u", s->dst.seqlo,
1444             s->dst.seqhi, s->dst.max_win, s->dst.seqdiff);
1445         if (s->src.wscale && s->dst.wscale)
1446                 printf(" wscale=%u", s->dst.wscale & PF_WSCALE_MASK);
1447         printf("]");
1448         printf(" %u:%u", s->src.state, s->dst.state);
1449 }
1450
1451 void
1452 pf_print_flags(u_int8_t f)
1453 {
1454         if (f)
1455                 printf(" ");
1456         if (f & TH_FIN)
1457                 printf("F");
1458         if (f & TH_SYN)
1459                 printf("S");
1460         if (f & TH_RST)
1461                 printf("R");
1462         if (f & TH_PUSH)
1463                 printf("P");
1464         if (f & TH_ACK)
1465                 printf("A");
1466         if (f & TH_URG)
1467                 printf("U");
1468         if (f & TH_ECE)
1469                 printf("E");
1470         if (f & TH_CWR)
1471                 printf("W");
1472 }
1473
1474 #define PF_SET_SKIP_STEPS(i)                                    \
1475         do {                                                    \
1476                 while (head[i] != cur) {                        \
1477                         head[i]->skip[i].ptr = cur;             \
1478                         head[i] = TAILQ_NEXT(head[i], entries); \
1479                 }                                               \
1480         } while (0)
1481
1482 void
1483 pf_calc_skip_steps(struct pf_rulequeue *rules)
1484 {
1485         struct pf_rule *cur, *prev, *head[PF_SKIP_COUNT];
1486         int i;
1487
1488         cur = TAILQ_FIRST(rules);
1489         prev = cur;
1490         for (i = 0; i < PF_SKIP_COUNT; ++i)
1491                 head[i] = cur;
1492         while (cur != NULL) {
1493
1494                 if (cur->kif != prev->kif || cur->ifnot != prev->ifnot)
1495                         PF_SET_SKIP_STEPS(PF_SKIP_IFP);
1496                 if (cur->direction != prev->direction)
1497                         PF_SET_SKIP_STEPS(PF_SKIP_DIR);
1498                 if (cur->af != prev->af)
1499                         PF_SET_SKIP_STEPS(PF_SKIP_AF);
1500                 if (cur->proto != prev->proto)
1501                         PF_SET_SKIP_STEPS(PF_SKIP_PROTO);
1502                 if (cur->src.neg != prev->src.neg ||
1503                     pf_addr_wrap_neq(&cur->src.addr, &prev->src.addr))
1504                         PF_SET_SKIP_STEPS(PF_SKIP_SRC_ADDR);
1505                 if (cur->src.port[0] != prev->src.port[0] ||
1506                     cur->src.port[1] != prev->src.port[1] ||
1507                     cur->src.port_op != prev->src.port_op)
1508                         PF_SET_SKIP_STEPS(PF_SKIP_SRC_PORT);
1509                 if (cur->dst.neg != prev->dst.neg ||
1510                     pf_addr_wrap_neq(&cur->dst.addr, &prev->dst.addr))
1511                         PF_SET_SKIP_STEPS(PF_SKIP_DST_ADDR);
1512                 if (cur->dst.port[0] != prev->dst.port[0] ||
1513                     cur->dst.port[1] != prev->dst.port[1] ||
1514                     cur->dst.port_op != prev->dst.port_op)
1515                         PF_SET_SKIP_STEPS(PF_SKIP_DST_PORT);
1516
1517                 prev = cur;
1518                 cur = TAILQ_NEXT(cur, entries);
1519         }
1520         for (i = 0; i < PF_SKIP_COUNT; ++i)
1521                 PF_SET_SKIP_STEPS(i);
1522 }
1523
1524 int
1525 pf_addr_wrap_neq(struct pf_addr_wrap *aw1, struct pf_addr_wrap *aw2)
1526 {
1527         if (aw1->type != aw2->type)
1528                 return (1);
1529         switch (aw1->type) {
1530         case PF_ADDR_ADDRMASK:
1531                 if (PF_ANEQ(&aw1->v.a.addr, &aw2->v.a.addr, 0))
1532                         return (1);
1533                 if (PF_ANEQ(&aw1->v.a.mask, &aw2->v.a.mask, 0))
1534                         return (1);
1535                 return (0);
1536         case PF_ADDR_DYNIFTL:
1537                 return (aw1->p.dyn->pfid_kt != aw2->p.dyn->pfid_kt);
1538         case PF_ADDR_NOROUTE:
1539         case PF_ADDR_URPFFAILED:
1540                 return (0);
1541         case PF_ADDR_TABLE:
1542                 return (aw1->p.tbl != aw2->p.tbl);
1543         case PF_ADDR_RTLABEL:
1544                 return (aw1->v.rtlabel != aw2->v.rtlabel);
1545         default:
1546                 printf("invalid address type: %d\n", aw1->type);
1547                 return (1);
1548         }
1549 }
1550
1551 u_int16_t
1552 pf_cksum_fixup(u_int16_t cksum, u_int16_t old, u_int16_t new, u_int8_t udp)
1553 {
1554         u_int32_t       l;
1555
1556         if (udp && !cksum)
1557                 return (0x0000);
1558         l = cksum + old - new;
1559         l = (l >> 16) + (l & 65535);
1560         l = l & 65535;
1561         if (udp && !l)
1562                 return (0xFFFF);
1563         return (l);
1564 }
1565
1566 void
1567 pf_change_ap(struct pf_addr *a, u_int16_t *p, u_int16_t *ic, u_int16_t *pc,
1568     struct pf_addr *an, u_int16_t pn, u_int8_t u, sa_family_t af)
1569 {
1570         struct pf_addr  ao;
1571         u_int16_t       po = *p;
1572
1573         PF_ACPY(&ao, a, af);
1574         PF_ACPY(a, an, af);
1575
1576         *p = pn;
1577
1578         switch (af) {
1579 #ifdef INET
1580         case AF_INET:
1581                 *ic = pf_cksum_fixup(pf_cksum_fixup(*ic,
1582                     ao.addr16[0], an->addr16[0], 0),
1583                     ao.addr16[1], an->addr16[1], 0);
1584                 *p = pn;
1585                 *pc = pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(*pc,
1586                     ao.addr16[0], an->addr16[0], u),
1587                     ao.addr16[1], an->addr16[1], u),
1588                     po, pn, u);
1589                 break;
1590 #endif /* INET */
1591 #ifdef INET6
1592         case AF_INET6:
1593                 *pc = pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
1594                     pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
1595                     pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(*pc,
1596                     ao.addr16[0], an->addr16[0], u),
1597                     ao.addr16[1], an->addr16[1], u),
1598                     ao.addr16[2], an->addr16[2], u),
1599                     ao.addr16[3], an->addr16[3], u),
1600                     ao.addr16[4], an->addr16[4], u),
1601                     ao.addr16[5], an->addr16[5], u),
1602                     ao.addr16[6], an->addr16[6], u),
1603                     ao.addr16[7], an->addr16[7], u),
1604                     po, pn, u);
1605                 break;
1606 #endif /* INET6 */
1607         }
1608 }
1609
1610
1611 /* Changes a u_int32_t.  Uses a void * so there are no align restrictions */
1612 void
1613 pf_change_a(void *a, u_int16_t *c, u_int32_t an, u_int8_t u)
1614 {
1615         u_int32_t       ao;
1616
1617         memcpy(&ao, a, sizeof(ao));
1618         memcpy(a, &an, sizeof(u_int32_t));
1619         *c = pf_cksum_fixup(pf_cksum_fixup(*c, ao / 65536, an / 65536, u),
1620             ao % 65536, an % 65536, u);
1621 }
1622
1623 #ifdef INET6
1624 void
1625 pf_change_a6(struct pf_addr *a, u_int16_t *c, struct pf_addr *an, u_int8_t u)
1626 {
1627         struct pf_addr  ao;
1628
1629         PF_ACPY(&ao, a, AF_INET6);
1630         PF_ACPY(a, an, AF_INET6);
1631
1632         *c = pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
1633             pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
1634             pf_cksum_fixup(pf_cksum_fixup(*c,
1635             ao.addr16[0], an->addr16[0], u),
1636             ao.addr16[1], an->addr16[1], u),
1637             ao.addr16[2], an->addr16[2], u),
1638             ao.addr16[3], an->addr16[3], u),
1639             ao.addr16[4], an->addr16[4], u),
1640             ao.addr16[5], an->addr16[5], u),
1641             ao.addr16[6], an->addr16[6], u),
1642             ao.addr16[7], an->addr16[7], u);
1643 }
1644 #endif /* INET6 */
1645
1646 void
1647 pf_change_icmp(struct pf_addr *ia, u_int16_t *ip, struct pf_addr *oa,
1648     struct pf_addr *na, u_int16_t np, u_int16_t *pc, u_int16_t *h2c,
1649     u_int16_t *ic, u_int16_t *hc, u_int8_t u, sa_family_t af)
1650 {
1651         struct pf_addr  oia, ooa;
1652
1653         PF_ACPY(&oia, ia, af);
1654         PF_ACPY(&ooa, oa, af);
1655
1656         /* Change inner protocol port, fix inner protocol checksum. */
1657         if (ip != NULL) {
1658                 u_int16_t       oip = *ip;
1659                 u_int32_t       opc = 0;        /* make the compiler happy */
1660
1661                 if (pc != NULL)
1662                         opc = *pc;
1663                 *ip = np;
1664                 if (pc != NULL)
1665                         *pc = pf_cksum_fixup(*pc, oip, *ip, u);
1666                 *ic = pf_cksum_fixup(*ic, oip, *ip, 0);
1667                 if (pc != NULL)
1668                         *ic = pf_cksum_fixup(*ic, opc, *pc, 0);
1669         }
1670         /* Change inner ip address, fix inner ip and icmp checksums. */
1671         PF_ACPY(ia, na, af);
1672         switch (af) {
1673 #ifdef INET
1674         case AF_INET: {
1675                 u_int32_t        oh2c = *h2c;
1676
1677                 *h2c = pf_cksum_fixup(pf_cksum_fixup(*h2c,
1678                     oia.addr16[0], ia->addr16[0], 0),
1679                     oia.addr16[1], ia->addr16[1], 0);
1680                 *ic = pf_cksum_fixup(pf_cksum_fixup(*ic,
1681                     oia.addr16[0], ia->addr16[0], 0),
1682                     oia.addr16[1], ia->addr16[1], 0);
1683                 *ic = pf_cksum_fixup(*ic, oh2c, *h2c, 0);
1684                 break;
1685         }
1686 #endif /* INET */
1687 #ifdef INET6
1688         case AF_INET6:
1689                 *ic = pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
1690                     pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
1691                     pf_cksum_fixup(pf_cksum_fixup(*ic,
1692                     oia.addr16[0], ia->addr16[0], u),
1693                     oia.addr16[1], ia->addr16[1], u),
1694                     oia.addr16[2], ia->addr16[2], u),
1695                     oia.addr16[3], ia->addr16[3], u),
1696                     oia.addr16[4], ia->addr16[4], u),
1697                     oia.addr16[5], ia->addr16[5], u),
1698                     oia.addr16[6], ia->addr16[6], u),
1699                     oia.addr16[7], ia->addr16[7], u);
1700                 break;
1701 #endif /* INET6 */
1702         }
1703         /* Change outer ip address, fix outer ip or icmpv6 checksum. */
1704         PF_ACPY(oa, na, af);
1705         switch (af) {
1706 #ifdef INET
1707         case AF_INET:
1708                 *hc = pf_cksum_fixup(pf_cksum_fixup(*hc,
1709                     ooa.addr16[0], oa->addr16[0], 0),
1710                     ooa.addr16[1], oa->addr16[1], 0);
1711                 break;
1712 #endif /* INET */
1713 #ifdef INET6
1714         case AF_INET6:
1715                 *ic = pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
1716                     pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
1717                     pf_cksum_fixup(pf_cksum_fixup(*ic,
1718                     ooa.addr16[0], oa->addr16[0], u),
1719                     ooa.addr16[1], oa->addr16[1], u),
1720                     ooa.addr16[2], oa->addr16[2], u),
1721                     ooa.addr16[3], oa->addr16[3], u),
1722                     ooa.addr16[4], oa->addr16[4], u),
1723                     ooa.addr16[5], oa->addr16[5], u),
1724                     ooa.addr16[6], oa->addr16[6], u),
1725                     ooa.addr16[7], oa->addr16[7], u);
1726                 break;
1727 #endif /* INET6 */
1728         }
1729 }
1730
1731
1732 /*
1733  * Need to modulate the sequence numbers in the TCP SACK option
1734  * (credits to Krzysztof Pfaff for report and patch)
1735  */
1736 int
1737 pf_modulate_sack(struct mbuf *m, int off, struct pf_pdesc *pd,
1738     struct tcphdr *th, struct pf_state_peer *dst)
1739 {
1740         int hlen = (th->th_off << 2) - sizeof(*th), thoptlen = hlen;
1741 #ifdef __FreeBSD__
1742         u_int8_t opts[TCP_MAXOLEN], *opt = opts;
1743 #else
1744         u_int8_t opts[MAX_TCPOPTLEN], *opt = opts;
1745 #endif
1746         int copyback = 0, i, olen;
1747         struct sackblk sack;
1748
1749 #define TCPOLEN_SACKLEN (TCPOLEN_SACK + 2)
1750         if (hlen < TCPOLEN_SACKLEN ||
1751             !pf_pull_hdr(m, off + sizeof(*th), opts, hlen, NULL, NULL, pd->af))
1752                 return 0;
1753
1754         while (hlen >= TCPOLEN_SACKLEN) {
1755                 olen = opt[1];
1756                 switch (*opt) {
1757                 case TCPOPT_EOL:        /* FALLTHROUGH */
1758                 case TCPOPT_NOP:
1759                         opt++;
1760                         hlen--;
1761                         break;
1762                 case TCPOPT_SACK:
1763                         if (olen > hlen)
1764                                 olen = hlen;
1765                         if (olen >= TCPOLEN_SACKLEN) {
1766                                 for (i = 2; i + TCPOLEN_SACK <= olen;
1767                                     i += TCPOLEN_SACK) {
1768                                         memcpy(&sack, &opt[i], sizeof(sack));
1769                                         pf_change_a(&sack.start, &th->th_sum,
1770                                             htonl(ntohl(sack.start) -
1771                                             dst->seqdiff), 0);
1772                                         pf_change_a(&sack.end, &th->th_sum,
1773                                             htonl(ntohl(sack.end) -
1774                                             dst->seqdiff), 0);
1775                                         memcpy(&opt[i], &sack, sizeof(sack));
1776                                 }
1777                                 copyback = 1;
1778                         }
1779                         /* FALLTHROUGH */
1780                 default:
1781                         if (olen < 2)
1782                                 olen = 2;
1783                         hlen -= olen;
1784                         opt += olen;
1785                 }
1786         }
1787
1788         if (copyback)
1789 #ifdef __FreeBSD__
1790                 m_copyback(m, off + sizeof(*th), thoptlen, (caddr_t)opts);
1791 #else
1792                 m_copyback(m, off + sizeof(*th), thoptlen, opts);
1793 #endif
1794         return (copyback);
1795 }
1796
1797 void
1798 #ifdef __FreeBSD__
1799 pf_send_tcp(struct mbuf *replyto, const struct pf_rule *r, sa_family_t af,
1800 #else
1801 pf_send_tcp(const struct pf_rule *r, sa_family_t af,
1802 #endif
1803     const struct pf_addr *saddr, const struct pf_addr *daddr,
1804     u_int16_t sport, u_int16_t dport, u_int32_t seq, u_int32_t ack,
1805     u_int8_t flags, u_int16_t win, u_int16_t mss, u_int8_t ttl, int tag,
1806     u_int16_t rtag, struct ether_header *eh, struct ifnet *ifp)
1807 {
1808         struct mbuf     *m;
1809         int              len, tlen;
1810 #ifdef INET
1811         struct ip       *h;
1812 #endif /* INET */
1813 #ifdef INET6
1814         struct ip6_hdr  *h6;
1815 #endif /* INET6 */
1816         struct tcphdr   *th;
1817         char            *opt;
1818         struct pf_mtag  *pf_mtag;
1819
1820 #ifdef __FreeBSD__
1821         KASSERT(
1822 #ifdef INET
1823             af == AF_INET
1824 #else
1825             0
1826 #endif
1827             ||
1828 #ifdef INET6
1829             af == AF_INET6
1830 #else
1831             0
1832 #endif
1833             , ("Unsupported AF %d", af));
1834         len = 0;
1835         th = NULL;
1836 #ifdef INET
1837         h = NULL;
1838 #endif
1839 #ifdef INET6
1840         h6 = NULL;
1841 #endif
1842 #endif
1843
1844         /* maximum segment size tcp option */
1845         tlen = sizeof(struct tcphdr);
1846         if (mss)
1847                 tlen += 4;
1848
1849         switch (af) {
1850 #ifdef INET
1851         case AF_INET:
1852                 len = sizeof(struct ip) + tlen;
1853                 break;
1854 #endif /* INET */
1855 #ifdef INET6
1856         case AF_INET6:
1857                 len = sizeof(struct ip6_hdr) + tlen;
1858                 break;
1859 #endif /* INET6 */
1860         }
1861
1862         /* create outgoing mbuf */
1863         m = m_gethdr(M_DONTWAIT, MT_HEADER);
1864         if (m == NULL)
1865                 return;
1866 #ifdef __FreeBSD__
1867 #ifdef MAC
1868         if (replyto)
1869                 mac_netinet_firewall_reply(replyto, m);
1870         else
1871                 mac_netinet_firewall_send(m);
1872 #else
1873         (void)replyto;
1874 #endif
1875 #endif
1876         if ((pf_mtag = pf_get_mtag(m)) == NULL) {
1877                 m_freem(m);
1878                 return;
1879         }
1880         if (tag)
1881 #ifdef __FreeBSD__
1882                 m->m_flags |= M_SKIP_FIREWALL;
1883 #else
1884                 pf_mtag->flags |= PF_TAG_GENERATED;
1885 #endif
1886
1887         pf_mtag->tag = rtag;
1888
1889         if (r != NULL && r->rtableid >= 0)
1890 #ifdef __FreeBSD__
1891         {
1892                 M_SETFIB(m, r->rtableid);
1893 #endif
1894                 pf_mtag->rtableid = r->rtableid;
1895 #ifdef __FreeBSD__
1896         }
1897 #endif
1898 #ifdef ALTQ
1899         if (r != NULL && r->qid) {
1900                 pf_mtag->qid = r->qid;
1901                 /* add hints for ecn */
1902                 pf_mtag->af = af;
1903                 pf_mtag->hdr = mtod(m, struct ip *);
1904         }
1905 #endif /* ALTQ */
1906         m->m_data += max_linkhdr;
1907         m->m_pkthdr.len = m->m_len = len;
1908         m->m_pkthdr.rcvif = NULL;
1909         bzero(m->m_data, len);
1910         switch (af) {
1911 #ifdef INET
1912         case AF_INET:
1913                 h = mtod(m, struct ip *);
1914
1915                 /* IP header fields included in the TCP checksum */
1916                 h->ip_p = IPPROTO_TCP;
1917                 h->ip_len = htons(tlen);
1918                 h->ip_src.s_addr = saddr->v4.s_addr;
1919                 h->ip_dst.s_addr = daddr->v4.s_addr;
1920
1921                 th = (struct tcphdr *)((caddr_t)h + sizeof(struct ip));
1922                 break;
1923 #endif /* INET */
1924 #ifdef INET6
1925         case AF_INET6:
1926                 h6 = mtod(m, struct ip6_hdr *);
1927
1928                 /* IP header fields included in the TCP checksum */
1929                 h6->ip6_nxt = IPPROTO_TCP;
1930                 h6->ip6_plen = htons(tlen);
1931                 memcpy(&h6->ip6_src, &saddr->v6, sizeof(struct in6_addr));
1932                 memcpy(&h6->ip6_dst, &daddr->v6, sizeof(struct in6_addr));
1933
1934                 th = (struct tcphdr *)((caddr_t)h6 + sizeof(struct ip6_hdr));
1935                 break;
1936 #endif /* INET6 */
1937         }
1938
1939         /* TCP header */
1940         th->th_sport = sport;
1941         th->th_dport = dport;
1942         th->th_seq = htonl(seq);
1943         th->th_ack = htonl(ack);
1944         th->th_off = tlen >> 2;
1945         th->th_flags = flags;
1946         th->th_win = htons(win);
1947
1948         if (mss) {
1949                 opt = (char *)(th + 1);
1950                 opt[0] = TCPOPT_MAXSEG;
1951                 opt[1] = 4;
1952                 HTONS(mss);
1953                 bcopy((caddr_t)&mss, (caddr_t)(opt + 2), 2);
1954         }
1955
1956         switch (af) {
1957 #ifdef INET
1958         case AF_INET:
1959                 /* TCP checksum */
1960                 th->th_sum = in_cksum(m, len);
1961
1962                 /* Finish the IP header */
1963                 h->ip_v = 4;
1964                 h->ip_hl = sizeof(*h) >> 2;
1965                 h->ip_tos = IPTOS_LOWDELAY;
1966 #ifdef __FreeBSD__
1967                 h->ip_off = V_path_mtu_discovery ? IP_DF : 0;
1968                 h->ip_len = len;
1969 #else
1970                 h->ip_off = htons(ip_mtudisc ? IP_DF : 0);
1971                 h->ip_len = htons(len);
1972 #endif
1973                 h->ip_ttl = ttl ? ttl : V_ip_defttl;
1974                 h->ip_sum = 0;
1975                 if (eh == NULL) {
1976 #ifdef __FreeBSD__
1977                         PF_UNLOCK();
1978                         ip_output(m, (void *)NULL, (void *)NULL, 0,
1979                             (void *)NULL, (void *)NULL);
1980                         PF_LOCK();
1981 #else /* ! __FreeBSD__ */
1982                         ip_output(m, (void *)NULL, (void *)NULL, 0,
1983                             (void *)NULL, (void *)NULL);
1984 #endif
1985                 } else {
1986                         struct route             ro;
1987                         struct rtentry           rt;
1988                         struct ether_header     *e = (void *)ro.ro_dst.sa_data;
1989
1990                         if (ifp == NULL) {
1991                                 m_freem(m);
1992                                 return;
1993                         }
1994                         rt.rt_ifp = ifp;
1995                         ro.ro_rt = &rt;
1996                         ro.ro_dst.sa_len = sizeof(ro.ro_dst);
1997                         ro.ro_dst.sa_family = pseudo_AF_HDRCMPLT;
1998                         bcopy(eh->ether_dhost, e->ether_shost, ETHER_ADDR_LEN);
1999                         bcopy(eh->ether_shost, e->ether_dhost, ETHER_ADDR_LEN);
2000                         e->ether_type = eh->ether_type;
2001 #ifdef __FreeBSD__
2002                         PF_UNLOCK();
2003                         /* XXX_IMPORT: later */
2004                         ip_output(m, (void *)NULL, &ro, 0,
2005                             (void *)NULL, (void *)NULL);
2006                         PF_LOCK();
2007 #else /* ! __FreeBSD__ */
2008                         ip_output(m, (void *)NULL, &ro, IP_ROUTETOETHER,
2009                             (void *)NULL, (void *)NULL);
2010 #endif
2011                 }
2012                 break;
2013 #endif /* INET */
2014 #ifdef INET6
2015         case AF_INET6:
2016                 /* TCP checksum */
2017                 th->th_sum = in6_cksum(m, IPPROTO_TCP,
2018                     sizeof(struct ip6_hdr), tlen);
2019
2020                 h6->ip6_vfc |= IPV6_VERSION;
2021                 h6->ip6_hlim = IPV6_DEFHLIM;
2022
2023 #ifdef __FreeBSD__
2024                 PF_UNLOCK();
2025                 ip6_output(m, NULL, NULL, 0, NULL, NULL, NULL);
2026                 PF_LOCK();
2027 #else
2028                 ip6_output(m, NULL, NULL, 0, NULL, NULL);
2029 #endif
2030                 break;
2031 #endif /* INET6 */
2032         }
2033 }
2034
2035 void
2036 pf_send_icmp(struct mbuf *m, u_int8_t type, u_int8_t code, sa_family_t af,
2037     struct pf_rule *r)
2038 {
2039         struct pf_mtag  *pf_mtag;
2040         struct mbuf     *m0;
2041 #ifdef __FreeBSD__
2042         struct ip *ip;
2043 #endif
2044
2045 #ifdef __FreeBSD__
2046         m0 = m_copypacket(m, M_DONTWAIT);
2047         if (m0 == NULL)
2048                 return;
2049 #else
2050         m0 = m_copy(m, 0, M_COPYALL);
2051 #endif
2052         if ((pf_mtag = pf_get_mtag(m0)) == NULL)
2053                 return;
2054 #ifdef __FreeBSD__
2055         /* XXX: revisit */
2056         m0->m_flags |= M_SKIP_FIREWALL;
2057 #else
2058         pf_mtag->flags |= PF_TAG_GENERATED;
2059 #endif
2060
2061         if (r->rtableid >= 0)
2062 #ifdef __FreeBSD__
2063         {
2064                 M_SETFIB(m0, r->rtableid);
2065 #endif
2066                 pf_mtag->rtableid = r->rtableid;
2067 #ifdef __FreeBSD__
2068         }
2069 #endif
2070
2071 #ifdef ALTQ
2072         if (r->qid) {
2073                 pf_mtag->qid = r->qid;
2074                 /* add hints for ecn */
2075                 pf_mtag->af = af;
2076                 pf_mtag->hdr = mtod(m0, struct ip *);
2077         }
2078 #endif /* ALTQ */
2079
2080         switch (af) {
2081 #ifdef INET
2082         case AF_INET:
2083 #ifdef __FreeBSD__
2084                 /* icmp_error() expects host byte ordering */
2085                 ip = mtod(m0, struct ip *);
2086                 NTOHS(ip->ip_len);
2087                 NTOHS(ip->ip_off);
2088                 PF_UNLOCK();
2089                 icmp_error(m0, type, code, 0, 0);
2090                 PF_LOCK();
2091 #else
2092                 icmp_error(m0, type, code, 0, 0);
2093 #endif
2094                 break;
2095 #endif /* INET */
2096 #ifdef INET6
2097         case AF_INET6:
2098 #ifdef __FreeBSD__
2099                 PF_UNLOCK();
2100 #endif
2101                 icmp6_error(m0, type, code, 0);
2102 #ifdef __FreeBSD__
2103                 PF_LOCK();
2104 #endif
2105                 break;
2106 #endif /* INET6 */
2107         }
2108 }
2109
2110 /*
2111  * Return 1 if the addresses a and b match (with mask m), otherwise return 0.
2112  * If n is 0, they match if they are equal. If n is != 0, they match if they
2113  * are different.
2114  */
2115 int
2116 pf_match_addr(u_int8_t n, struct pf_addr *a, struct pf_addr *m,
2117     struct pf_addr *b, sa_family_t af)
2118 {
2119         int     match = 0;
2120
2121         switch (af) {
2122 #ifdef INET
2123         case AF_INET:
2124                 if ((a->addr32[0] & m->addr32[0]) ==
2125                     (b->addr32[0] & m->addr32[0]))
2126                         match++;
2127                 break;
2128 #endif /* INET */
2129 #ifdef INET6
2130         case AF_INET6:
2131                 if (((a->addr32[0] & m->addr32[0]) ==
2132                      (b->addr32[0] & m->addr32[0])) &&
2133                     ((a->addr32[1] & m->addr32[1]) ==
2134                      (b->addr32[1] & m->addr32[1])) &&
2135                     ((a->addr32[2] & m->addr32[2]) ==
2136                      (b->addr32[2] & m->addr32[2])) &&
2137                     ((a->addr32[3] & m->addr32[3]) ==
2138                      (b->addr32[3] & m->addr32[3])))
2139                         match++;
2140                 break;
2141 #endif /* INET6 */
2142         }
2143         if (match) {
2144                 if (n)
2145                         return (0);
2146                 else
2147                         return (1);
2148         } else {
2149                 if (n)
2150                         return (1);
2151                 else
2152                         return (0);
2153         }
2154 }
2155
2156 int
2157 pf_match(u_int8_t op, u_int32_t a1, u_int32_t a2, u_int32_t p)
2158 {
2159         switch (op) {
2160         case PF_OP_IRG:
2161                 return ((p > a1) && (p < a2));
2162         case PF_OP_XRG:
2163                 return ((p < a1) || (p > a2));
2164         case PF_OP_RRG:
2165                 return ((p >= a1) && (p <= a2));
2166         case PF_OP_EQ:
2167                 return (p == a1);
2168         case PF_OP_NE:
2169                 return (p != a1);
2170         case PF_OP_LT:
2171                 return (p < a1);
2172         case PF_OP_LE:
2173                 return (p <= a1);
2174         case PF_OP_GT:
2175                 return (p > a1);
2176         case PF_OP_GE:
2177                 return (p >= a1);
2178         }
2179         return (0); /* never reached */
2180 }
2181
2182 int
2183 pf_match_port(u_int8_t op, u_int16_t a1, u_int16_t a2, u_int16_t p)
2184 {
2185         NTOHS(a1);
2186         NTOHS(a2);
2187         NTOHS(p);
2188         return (pf_match(op, a1, a2, p));
2189 }
2190
2191 int
2192 pf_match_uid(u_int8_t op, uid_t a1, uid_t a2, uid_t u)
2193 {
2194         if (u == UID_MAX && op != PF_OP_EQ && op != PF_OP_NE)
2195                 return (0);
2196         return (pf_match(op, a1, a2, u));
2197 }
2198
2199 int
2200 pf_match_gid(u_int8_t op, gid_t a1, gid_t a2, gid_t g)
2201 {
2202         if (g == GID_MAX && op != PF_OP_EQ && op != PF_OP_NE)
2203                 return (0);
2204         return (pf_match(op, a1, a2, g));
2205 }
2206
2207 #ifndef __FreeBSD__
2208 struct pf_mtag *
2209 pf_find_mtag(struct mbuf *m)
2210 {
2211         struct m_tag    *mtag;
2212
2213         if ((mtag = m_tag_find(m, PACKET_TAG_PF, NULL)) == NULL)
2214                 return (NULL);
2215
2216         return ((struct pf_mtag *)(mtag + 1));
2217 }
2218
2219 struct pf_mtag *
2220 pf_get_mtag(struct mbuf *m)
2221 {
2222         struct m_tag    *mtag;
2223
2224         if ((mtag = m_tag_find(m, PACKET_TAG_PF, NULL)) == NULL) {
2225                 mtag = m_tag_get(PACKET_TAG_PF, sizeof(struct pf_mtag),
2226                     M_NOWAIT);
2227                 if (mtag == NULL)
2228                         return (NULL);
2229                 bzero(mtag + 1, sizeof(struct pf_mtag));
2230                 m_tag_prepend(m, mtag);
2231         }
2232
2233         return ((struct pf_mtag *)(mtag + 1));
2234 }
2235 #endif
2236
2237 int
2238 pf_match_tag(struct mbuf *m, struct pf_rule *r, struct pf_mtag *pf_mtag,
2239     int *tag)
2240 {
2241         if (*tag == -1)
2242                 *tag = pf_mtag->tag;
2243
2244         return ((!r->match_tag_not && r->match_tag == *tag) ||
2245             (r->match_tag_not && r->match_tag != *tag));
2246 }
2247
2248 int
2249 pf_tag_packet(struct mbuf *m, struct pf_mtag *pf_mtag, int tag, int rtableid)
2250 {
2251         if (tag <= 0 && rtableid < 0)
2252                 return (0);
2253
2254         if (pf_mtag == NULL)
2255                 if ((pf_mtag = pf_get_mtag(m)) == NULL)
2256                         return (1);
2257         if (tag > 0)
2258                 pf_mtag->tag = tag;
2259         if (rtableid >= 0)
2260 #ifdef __FreeBSD__
2261         {
2262                 M_SETFIB(m, rtableid);
2263 #endif
2264                 pf_mtag->rtableid = rtableid;
2265 #ifdef __FreeBSD__
2266         }
2267 #endif
2268
2269         return (0);
2270 }
2271
2272 static void
2273 pf_step_into_anchor(int *depth, struct pf_ruleset **rs, int n,
2274     struct pf_rule **r, struct pf_rule **a,  int *match)
2275 {
2276         struct pf_anchor_stackframe     *f;
2277
2278         (*r)->anchor->match = 0;
2279         if (match)
2280                 *match = 0;
2281         if (*depth >= sizeof(pf_anchor_stack) /
2282             sizeof(pf_anchor_stack[0])) {
2283                 printf("pf_step_into_anchor: stack overflow\n");
2284                 *r = TAILQ_NEXT(*r, entries);
2285                 return;
2286         } else if (*depth == 0 && a != NULL)
2287                 *a = *r;
2288         f = pf_anchor_stack + (*depth)++;
2289         f->rs = *rs;
2290         f->r = *r;
2291         if ((*r)->anchor_wildcard) {
2292                 f->parent = &(*r)->anchor->children;
2293                 if ((f->child = RB_MIN(pf_anchor_node, f->parent)) ==
2294                     NULL) {
2295                         *r = NULL;
2296                         return;
2297                 }
2298                 *rs = &f->child->ruleset;
2299         } else {
2300                 f->parent = NULL;
2301                 f->child = NULL;
2302                 *rs = &(*r)->anchor->ruleset;
2303         }
2304         *r = TAILQ_FIRST((*rs)->rules[n].active.ptr);
2305 }
2306
2307 int
2308 pf_step_out_of_anchor(int *depth, struct pf_ruleset **rs, int n,
2309     struct pf_rule **r, struct pf_rule **a, int *match)
2310 {
2311         struct pf_anchor_stackframe     *f;
2312         int quick = 0;
2313
2314         do {
2315                 if (*depth <= 0)
2316                         break;
2317                 f = pf_anchor_stack + *depth - 1;
2318                 if (f->parent != NULL && f->child != NULL) {
2319                         if (f->child->match ||
2320                             (match != NULL && *match)) {
2321                                 f->r->anchor->match = 1;
2322                                 *match = 0;
2323                         }
2324                         f->child = RB_NEXT(pf_anchor_node, f->parent, f->child);
2325                         if (f->child != NULL) {
2326                                 *rs = &f->child->ruleset;
2327                                 *r = TAILQ_FIRST((*rs)->rules[n].active.ptr);
2328                                 if (*r == NULL)
2329                                         continue;
2330                                 else
2331                                         break;
2332                         }
2333                 }
2334                 (*depth)--;
2335                 if (*depth == 0 && a != NULL)
2336                         *a = NULL;
2337                 *rs = f->rs;
2338                 if (f->r->anchor->match || (match  != NULL && *match))
2339                         quick = f->r->quick;
2340                 *r = TAILQ_NEXT(f->r, entries);
2341         } while (*r == NULL);
2342
2343         return (quick);
2344 }
2345
2346 #ifdef INET6
2347 void
2348 pf_poolmask(struct pf_addr *naddr, struct pf_addr *raddr,
2349     struct pf_addr *rmask, struct pf_addr *saddr, sa_family_t af)
2350 {
2351         switch (af) {
2352 #ifdef INET
2353         case AF_INET:
2354                 naddr->addr32[0] = (raddr->addr32[0] & rmask->addr32[0]) |
2355                 ((rmask->addr32[0] ^ 0xffffffff ) & saddr->addr32[0]);
2356                 break;
2357 #endif /* INET */
2358         case AF_INET6:
2359                 naddr->addr32[0] = (raddr->addr32[0] & rmask->addr32[0]) |
2360                 ((rmask->addr32[0] ^ 0xffffffff ) & saddr->addr32[0]);
2361                 naddr->addr32[1] = (raddr->addr32[1] & rmask->addr32[1]) |
2362                 ((rmask->addr32[1] ^ 0xffffffff ) & saddr->addr32[1]);
2363                 naddr->addr32[2] = (raddr->addr32[2] & rmask->addr32[2]) |
2364                 ((rmask->addr32[2] ^ 0xffffffff ) & saddr->addr32[2]);
2365                 naddr->addr32[3] = (raddr->addr32[3] & rmask->addr32[3]) |
2366                 ((rmask->addr32[3] ^ 0xffffffff ) & saddr->addr32[3]);
2367                 break;
2368         }
2369 }
2370
2371 void
2372 pf_addr_inc(struct pf_addr *addr, sa_family_t af)
2373 {
2374         switch (af) {
2375 #ifdef INET
2376         case AF_INET:
2377                 addr->addr32[0] = htonl(ntohl(addr->addr32[0]) + 1);
2378                 break;
2379 #endif /* INET */
2380         case AF_INET6:
2381                 if (addr->addr32[3] == 0xffffffff) {
2382                         addr->addr32[3] = 0;
2383                         if (addr->addr32[2] == 0xffffffff) {
2384                                 addr->addr32[2] = 0;
2385                                 if (addr->addr32[1] == 0xffffffff) {
2386                                         addr->addr32[1] = 0;
2387                                         addr->addr32[0] =
2388                                             htonl(ntohl(addr->addr32[0]) + 1);
2389                                 } else
2390                                         addr->addr32[1] =
2391                                             htonl(ntohl(addr->addr32[1]) + 1);
2392                         } else
2393                                 addr->addr32[2] =
2394                                     htonl(ntohl(addr->addr32[2]) + 1);
2395                 } else
2396                         addr->addr32[3] =
2397                             htonl(ntohl(addr->addr32[3]) + 1);
2398                 break;
2399         }
2400 }
2401 #endif /* INET6 */
2402
2403 #define mix(a,b,c) \
2404         do {                                    \
2405                 a -= b; a -= c; a ^= (c >> 13); \
2406                 b -= c; b -= a; b ^= (a << 8);  \
2407                 c -= a; c -= b; c ^= (b >> 13); \
2408                 a -= b; a -= c; a ^= (c >> 12); \
2409                 b -= c; b -= a; b ^= (a << 16); \
2410                 c -= a; c -= b; c ^= (b >> 5);  \
2411                 a -= b; a -= c; a ^= (c >> 3);  \
2412                 b -= c; b -= a; b ^= (a << 10); \
2413                 c -= a; c -= b; c ^= (b >> 15); \
2414         } while (0)
2415
2416 /*
2417  * hash function based on bridge_hash in if_bridge.c
2418  */
2419 void
2420 pf_hash(struct pf_addr *inaddr, struct pf_addr *hash,
2421     struct pf_poolhashkey *key, sa_family_t af)
2422 {
2423         u_int32_t       a = 0x9e3779b9, b = 0x9e3779b9, c = key->key32[0];
2424
2425         switch (af) {
2426 #ifdef INET
2427         case AF_INET:
2428                 a += inaddr->addr32[0];
2429                 b += key->key32[1];
2430                 mix(a, b, c);
2431                 hash->addr32[0] = c + key->key32[2];
2432                 break;
2433 #endif /* INET */
2434 #ifdef INET6
2435         case AF_INET6:
2436                 a += inaddr->addr32[0];
2437                 b += inaddr->addr32[2];
2438                 mix(a, b, c);
2439                 hash->addr32[0] = c;
2440                 a += inaddr->addr32[1];
2441                 b += inaddr->addr32[3];
2442                 c += key->key32[1];
2443                 mix(a, b, c);
2444                 hash->addr32[1] = c;
2445                 a += inaddr->addr32[2];
2446                 b += inaddr->addr32[1];
2447                 c += key->key32[2];
2448                 mix(a, b, c);
2449                 hash->addr32[2] = c;
2450                 a += inaddr->addr32[3];
2451                 b += inaddr->addr32[0];
2452                 c += key->key32[3];
2453                 mix(a, b, c);
2454                 hash->addr32[3] = c;
2455                 break;
2456 #endif /* INET6 */
2457         }
2458 }
2459
2460 int
2461 pf_map_addr(sa_family_t af, struct pf_rule *r, struct pf_addr *saddr,
2462     struct pf_addr *naddr, struct pf_addr *init_addr, struct pf_src_node **sn)
2463 {
2464         unsigned char            hash[16];
2465         struct pf_pool          *rpool = &r->rpool;
2466         struct pf_addr          *raddr = &rpool->cur->addr.v.a.addr;
2467         struct pf_addr          *rmask = &rpool->cur->addr.v.a.mask;
2468         struct pf_pooladdr      *acur = rpool->cur;
2469         struct pf_src_node       k;
2470
2471         if (*sn == NULL && r->rpool.opts & PF_POOL_STICKYADDR &&
2472             (r->rpool.opts & PF_POOL_TYPEMASK) != PF_POOL_NONE) {
2473                 k.af = af;
2474                 PF_ACPY(&k.addr, saddr, af);
2475                 if (r->rule_flag & PFRULE_RULESRCTRACK ||
2476                     r->rpool.opts & PF_POOL_STICKYADDR)
2477                         k.rule.ptr = r;
2478                 else
2479                         k.rule.ptr = NULL;
2480                 pf_status.scounters[SCNT_SRC_NODE_SEARCH]++;
2481                 *sn = RB_FIND(pf_src_tree, &tree_src_tracking, &k);
2482                 if (*sn != NULL && !PF_AZERO(&(*sn)->raddr, af)) {
2483                         PF_ACPY(naddr, &(*sn)->raddr, af);
2484                         if (pf_status.debug >= PF_DEBUG_MISC) {
2485                                 printf("pf_map_addr: src tracking maps ");
2486                                 pf_print_host(&k.addr, 0, af);
2487                                 printf(" to ");
2488                                 pf_print_host(naddr, 0, af);
2489                                 printf("\n");
2490                         }
2491                         return (0);
2492                 }
2493         }
2494
2495         if (rpool->cur->addr.type == PF_ADDR_NOROUTE)
2496                 return (1);
2497         if (rpool->cur->addr.type == PF_ADDR_DYNIFTL) {
2498                 switch (af) {
2499 #ifdef INET
2500                 case AF_INET:
2501                         if (rpool->cur->addr.p.dyn->pfid_acnt4 < 1 &&
2502                             (rpool->opts & PF_POOL_TYPEMASK) !=
2503                             PF_POOL_ROUNDROBIN)
2504                                 return (1);
2505                          raddr = &rpool->cur->addr.p.dyn->pfid_addr4;
2506                          rmask = &rpool->cur->addr.p.dyn->pfid_mask4;
2507                         break;
2508 #endif /* INET */
2509 #ifdef INET6
2510                 case AF_INET6:
2511                         if (rpool->cur->addr.p.dyn->pfid_acnt6 < 1 &&
2512                             (rpool->opts & PF_POOL_TYPEMASK) !=
2513                             PF_POOL_ROUNDROBIN)
2514                                 return (1);
2515                         raddr = &rpool->cur->addr.p.dyn->pfid_addr6;
2516                         rmask = &rpool->cur->addr.p.dyn->pfid_mask6;
2517                         break;
2518 #endif /* INET6 */
2519                 }
2520         } else if (rpool->cur->addr.type == PF_ADDR_TABLE) {
2521                 if ((rpool->opts & PF_POOL_TYPEMASK) != PF_POOL_ROUNDROBIN)
2522                         return (1); /* unsupported */
2523         } else {
2524                 raddr = &rpool->cur->addr.v.a.addr;
2525                 rmask = &rpool->cur->addr.v.a.mask;
2526         }
2527
2528         switch (rpool->opts & PF_POOL_TYPEMASK) {
2529         case PF_POOL_NONE:
2530                 PF_ACPY(naddr, raddr, af);
2531                 break;
2532         case PF_POOL_BITMASK:
2533                 PF_POOLMASK(naddr, raddr, rmask, saddr, af);
2534                 break;
2535         case PF_POOL_RANDOM:
2536                 if (init_addr != NULL && PF_AZERO(init_addr, af)) {
2537                         switch (af) {
2538 #ifdef INET
2539                         case AF_INET:
2540                                 rpool->counter.addr32[0] = htonl(arc4random());
2541                                 break;
2542 #endif /* INET */
2543 #ifdef INET6
2544                         case AF_INET6:
2545                                 if (rmask->addr32[3] != 0xffffffff)
2546                                         rpool->counter.addr32[3] =
2547                                             htonl(arc4random());
2548                                 else
2549                                         break;
2550                                 if (rmask->addr32[2] != 0xffffffff)
2551                                         rpool->counter.addr32[2] =
2552                                             htonl(arc4random());
2553                                 else
2554                                         break;
2555                                 if (rmask->addr32[1] != 0xffffffff)
2556                                         rpool->counter.addr32[1] =
2557                                             htonl(arc4random());
2558                                 else
2559                                         break;
2560                                 if (rmask->addr32[0] != 0xffffffff)
2561                                         rpool->counter.addr32[0] =
2562                                             htonl(arc4random());
2563                                 break;
2564 #endif /* INET6 */
2565                         }
2566                         PF_POOLMASK(naddr, raddr, rmask, &rpool->counter, af);
2567                         PF_ACPY(init_addr, naddr, af);
2568
2569                 } else {
2570                         PF_AINC(&rpool->counter, af);
2571                         PF_POOLMASK(naddr, raddr, rmask, &rpool->counter, af);
2572                 }
2573                 break;
2574         case PF_POOL_SRCHASH:
2575                 pf_hash(saddr, (struct pf_addr *)&hash, &rpool->key, af);
2576                 PF_POOLMASK(naddr, raddr, rmask, (struct pf_addr *)&hash, af);
2577                 break;
2578         case PF_POOL_ROUNDROBIN:
2579                 if (rpool->cur->addr.type == PF_ADDR_TABLE) {
2580                         if (!pfr_pool_get(rpool->cur->addr.p.tbl,
2581                             &rpool->tblidx, &rpool->counter,
2582                             &raddr, &rmask, af))
2583                                 goto get_addr;
2584                 } else if (rpool->cur->addr.type == PF_ADDR_DYNIFTL) {
2585                         if (!pfr_pool_get(rpool->cur->addr.p.dyn->pfid_kt,
2586                             &rpool->tblidx, &rpool->counter,
2587                             &raddr, &rmask, af))
2588                                 goto get_addr;
2589                 } else if (pf_match_addr(0, raddr, rmask, &rpool->counter, af))
2590                         goto get_addr;
2591
2592         try_next:
2593                 if ((rpool->cur = TAILQ_NEXT(rpool->cur, entries)) == NULL)
2594                         rpool->cur = TAILQ_FIRST(&rpool->list);
2595                 if (rpool->cur->addr.type == PF_ADDR_TABLE) {
2596                         rpool->tblidx = -1;
2597                         if (pfr_pool_get(rpool->cur->addr.p.tbl,
2598                             &rpool->tblidx, &rpool->counter,
2599                             &raddr, &rmask, af)) {
2600                                 /* table contains no address of type 'af' */
2601                                 if (rpool->cur != acur)
2602                                         goto try_next;
2603                                 return (1);
2604                         }
2605                 } else if (rpool->cur->addr.type == PF_ADDR_DYNIFTL) {
2606                         rpool->tblidx = -1;
2607                         if (pfr_pool_get(rpool->cur->addr.p.dyn->pfid_kt,
2608                             &rpool->tblidx, &rpool->counter,
2609                             &raddr, &rmask, af)) {
2610                                 /* table contains no address of type 'af' */
2611                                 if (rpool->cur != acur)
2612                                         goto try_next;
2613                                 return (1);
2614                         }
2615                 } else {
2616                         raddr = &rpool->cur->addr.v.a.addr;
2617                         rmask = &rpool->cur->addr.v.a.mask;
2618                         PF_ACPY(&rpool->counter, raddr, af);
2619                 }
2620
2621         get_addr:
2622                 PF_ACPY(naddr, &rpool->counter, af);
2623                 if (init_addr != NULL && PF_AZERO(init_addr, af))
2624                         PF_ACPY(init_addr, naddr, af);
2625                 PF_AINC(&rpool->counter, af);
2626                 break;
2627         }
2628         if (*sn != NULL)
2629                 PF_ACPY(&(*sn)->raddr, naddr, af);
2630
2631         if (pf_status.debug >= PF_DEBUG_MISC &&
2632             (rpool->opts & PF_POOL_TYPEMASK) != PF_POOL_NONE) {
2633                 printf("pf_map_addr: selected address ");
2634                 pf_print_host(naddr, 0, af);
2635                 printf("\n");
2636         }
2637
2638         return (0);
2639 }
2640
2641 int
2642 pf_get_sport(sa_family_t af, u_int8_t proto, struct pf_rule *r,
2643     struct pf_addr *saddr, struct pf_addr *daddr, u_int16_t dport,
2644     struct pf_addr *naddr, u_int16_t *nport, u_int16_t low, u_int16_t high,
2645     struct pf_src_node **sn)
2646 {
2647         struct pf_state_cmp     key;
2648         struct pf_addr          init_addr;
2649         u_int16_t               cut;
2650
2651         bzero(&init_addr, sizeof(init_addr));
2652         if (pf_map_addr(af, r, saddr, naddr, &init_addr, sn))
2653                 return (1);
2654
2655         if (proto == IPPROTO_ICMP) {
2656                 low = 1;
2657                 high = 65535;
2658         }
2659
2660         do {
2661                 key.af = af;
2662                 key.proto = proto;
2663                 PF_ACPY(&key.ext.addr, daddr, key.af);
2664                 PF_ACPY(&key.gwy.addr, naddr, key.af);
2665                 key.ext.port = dport;
2666
2667                 /*
2668                  * port search; start random, step;
2669                  * similar 2 portloop in in_pcbbind
2670                  */
2671                 if (!(proto == IPPROTO_TCP || proto == IPPROTO_UDP ||
2672                     proto == IPPROTO_ICMP)) {
2673                         key.gwy.port = dport;
2674                         if (pf_find_state_all(&key, PF_EXT_GWY, NULL) == NULL)
2675                                 return (0);
2676                 } else if (low == 0 && high == 0) {
2677                         key.gwy.port = *nport;
2678                         if (pf_find_state_all(&key, PF_EXT_GWY, NULL) == NULL)
2679                                 return (0);
2680                 } else if (low == high) {
2681                         key.gwy.port = htons(low);
2682                         if (pf_find_state_all(&key, PF_EXT_GWY, NULL) == NULL) {
2683                                 *nport = htons(low);
2684                                 return (0);
2685                         }
2686                 } else {
2687                         u_int16_t tmp;
2688
2689                         if (low > high) {
2690                                 tmp = low;
2691                                 low = high;
2692                                 high = tmp;
2693                         }
2694                         /* low < high */
2695                         cut = htonl(arc4random()) % (1 + high - low) + low;
2696                         /* low <= cut <= high */
2697                         for (tmp = cut; tmp <= high; ++(tmp)) {
2698                                 key.gwy.port = htons(tmp);
2699                                 if (pf_find_state_all(&key, PF_EXT_GWY, NULL) ==
2700                                     NULL) {
2701                                         *nport = htons(tmp);
2702                                         return (0);
2703                                 }
2704                         }
2705                         for (tmp = cut - 1; tmp >= low; --(tmp)) {
2706                                 key.gwy.port = htons(tmp);
2707                                 if (pf_find_state_all(&key, PF_EXT_GWY, NULL) ==
2708                                     NULL) {
2709                                         *nport = htons(tmp);
2710                                         return (0);
2711                                 }
2712                         }
2713                 }
2714
2715                 switch (r->rpool.opts & PF_POOL_TYPEMASK) {
2716                 case PF_POOL_RANDOM:
2717                 case PF_POOL_ROUNDROBIN:
2718                         if (pf_map_addr(af, r, saddr, naddr, &init_addr, sn))
2719                                 return (1);
2720                         break;
2721                 case PF_POOL_NONE:
2722                 case PF_POOL_SRCHASH:
2723                 case PF_POOL_BITMASK:
2724                 default:
2725                         return (1);
2726                 }
2727         } while (! PF_AEQ(&init_addr, naddr, af) );
2728
2729         return (1);                                     /* none available */
2730 }
2731
2732 struct pf_rule *
2733 pf_match_translation(struct pf_pdesc *pd, struct mbuf *m, int off,
2734     int direction, struct pfi_kif *kif, struct pf_addr *saddr, u_int16_t sport,
2735     struct pf_addr *daddr, u_int16_t dport, int rs_num)
2736 {
2737         struct pf_rule          *r, *rm = NULL;
2738         struct pf_ruleset       *ruleset = NULL;
2739         int                      tag = -1;
2740         int                      rtableid = -1;
2741         int                      asd = 0;
2742
2743         r = TAILQ_FIRST(pf_main_ruleset.rules[rs_num].active.ptr);
2744         while (r && rm == NULL) {
2745                 struct pf_rule_addr     *src = NULL, *dst = NULL;
2746                 struct pf_addr_wrap     *xdst = NULL;
2747
2748                 if (r->action == PF_BINAT && direction == PF_IN) {
2749                         src = &r->dst;
2750                         if (r->rpool.cur != NULL)
2751                                 xdst = &r->rpool.cur->addr;
2752                 } else {
2753                         src = &r->src;
2754                         dst = &r->dst;
2755                 }
2756
2757                 r->evaluations++;
2758                 if (pfi_kif_match(r->kif, kif) == r->ifnot)
2759                         r = r->skip[PF_SKIP_IFP].ptr;
2760                 else if (r->direction && r->direction != direction)
2761                         r = r->skip[PF_SKIP_DIR].ptr;
2762                 else if (r->af && r->af != pd->af)
2763                         r = r->skip[PF_SKIP_AF].ptr;
2764                 else if (r->proto && r->proto != pd->proto)
2765                         r = r->skip[PF_SKIP_PROTO].ptr;
2766                 else if (PF_MISMATCHAW(&src->addr, saddr, pd->af,
2767                     src->neg, kif))
2768                         r = r->skip[src == &r->src ? PF_SKIP_SRC_ADDR :
2769                             PF_SKIP_DST_ADDR].ptr;
2770                 else if (src->port_op && !pf_match_port(src->port_op,
2771                     src->port[0], src->port[1], sport))
2772                         r = r->skip[src == &r->src ? PF_SKIP_SRC_PORT :
2773                             PF_SKIP_DST_PORT].ptr;
2774                 else if (dst != NULL &&
2775                     PF_MISMATCHAW(&dst->addr, daddr, pd->af, dst->neg, NULL))
2776                         r = r->skip[PF_SKIP_DST_ADDR].ptr;
2777                 else if (xdst != NULL && PF_MISMATCHAW(xdst, daddr, pd->af,
2778                     0, NULL))
2779                         r = TAILQ_NEXT(r, entries);
2780                 else if (dst != NULL && dst->port_op &&
2781                     !pf_match_port(dst->port_op, dst->port[0],
2782                     dst->port[1], dport))
2783                         r = r->skip[PF_SKIP_DST_PORT].ptr;
2784                 else if (r->match_tag && !pf_match_tag(m, r, pd->pf_mtag, &tag))
2785                         r = TAILQ_NEXT(r, entries);
2786                 else if (r->os_fingerprint != PF_OSFP_ANY && (pd->proto !=
2787                     IPPROTO_TCP || !pf_osfp_match(pf_osfp_fingerprint(pd, m,
2788                     off, pd->hdr.tcp), r->os_fingerprint)))
2789                         r = TAILQ_NEXT(r, entries);
2790                 else {
2791                         if (r->tag)
2792                                 tag = r->tag;
2793                         if (r->rtableid >= 0)
2794                                 rtableid = r->rtableid;
2795                         if (r->anchor == NULL) {
2796                                 rm = r;
2797                         } else
2798                                 pf_step_into_anchor(&asd, &ruleset, rs_num,
2799                                     &r, NULL, NULL);
2800                 }
2801                 if (r == NULL)
2802                         pf_step_out_of_anchor(&asd, &ruleset, rs_num, &r,
2803                             NULL, NULL);
2804         }
2805         if (pf_tag_packet(m, pd->pf_mtag, tag, rtableid))
2806                 return (NULL);
2807         if (rm != NULL && (rm->action == PF_NONAT ||
2808             rm->action == PF_NORDR || rm->action == PF_NOBINAT))
2809                 return (NULL);
2810         return (rm);
2811 }
2812
2813 struct pf_rule *
2814 pf_get_translation(struct pf_pdesc *pd, struct mbuf *m, int off, int direction,
2815     struct pfi_kif *kif, struct pf_src_node **sn,
2816     struct pf_addr *saddr, u_int16_t sport,
2817     struct pf_addr *daddr, u_int16_t dport,
2818     struct pf_addr *naddr, u_int16_t *nport)
2819 {
2820         struct pf_rule  *r = NULL;
2821
2822         if (direction == PF_OUT) {
2823                 r = pf_match_translation(pd, m, off, direction, kif, saddr,
2824                     sport, daddr, dport, PF_RULESET_BINAT);
2825                 if (r == NULL)
2826                         r = pf_match_translation(pd, m, off, direction, kif,
2827                             saddr, sport, daddr, dport, PF_RULESET_NAT);
2828         } else {
2829                 r = pf_match_translation(pd, m, off, direction, kif, saddr,
2830                     sport, daddr, dport, PF_RULESET_RDR);
2831                 if (r == NULL)
2832                         r = pf_match_translation(pd, m, off, direction, kif,
2833                             saddr, sport, daddr, dport, PF_RULESET_BINAT);
2834         }
2835
2836         if (r != NULL) {
2837                 switch (r->action) {
2838                 case PF_NONAT:
2839                 case PF_NOBINAT:
2840                 case PF_NORDR:
2841                         return (NULL);
2842                 case PF_NAT:
2843                         if (pf_get_sport(pd->af, pd->proto, r, saddr,
2844                             daddr, dport, naddr, nport, r->rpool.proxy_port[0],
2845                             r->rpool.proxy_port[1], sn)) {
2846                                 DPFPRINTF(PF_DEBUG_MISC,
2847                                     ("pf: NAT proxy port allocation "
2848                                     "(%u-%u) failed\n",
2849                                     r->rpool.proxy_port[0],
2850                                     r->rpool.proxy_port[1]));
2851                                 return (NULL);
2852                         }
2853                         break;
2854                 case PF_BINAT:
2855                         switch (direction) {
2856                         case PF_OUT:
2857                                 if (r->rpool.cur->addr.type == PF_ADDR_DYNIFTL){
2858                                         switch (pd->af) {
2859 #ifdef INET
2860                                         case AF_INET:
2861                                                 if (r->rpool.cur->addr.p.dyn->
2862                                                     pfid_acnt4 < 1)
2863                                                         return (NULL);
2864                                                 PF_POOLMASK(naddr,
2865                                                     &r->rpool.cur->addr.p.dyn->
2866                                                     pfid_addr4,
2867                                                     &r->rpool.cur->addr.p.dyn->
2868                                                     pfid_mask4,
2869                                                     saddr, AF_INET);
2870                                                 break;
2871 #endif /* INET */
2872 #ifdef INET6
2873                                         case AF_INET6:
2874                                                 if (r->rpool.cur->addr.p.dyn->
2875                                                     pfid_acnt6 < 1)
2876                                                         return (NULL);
2877                                                 PF_POOLMASK(naddr,
2878                                                     &r->rpool.cur->addr.p.dyn->
2879                                                     pfid_addr6,
2880                                                     &r->rpool.cur->addr.p.dyn->
2881                                                     pfid_mask6,
2882                                                     saddr, AF_INET6);
2883                                                 break;
2884 #endif /* INET6 */
2885                                         }
2886                                 } else
2887                                         PF_POOLMASK(naddr,
2888                                             &r->rpool.cur->addr.v.a.addr,
2889                                             &r->rpool.cur->addr.v.a.mask,
2890                                             saddr, pd->af);
2891                                 break;
2892                         case PF_IN:
2893                                 if (r->src.addr.type == PF_ADDR_DYNIFTL) {
2894                                         switch (pd->af) {
2895 #ifdef INET
2896                                         case AF_INET:
2897                                                 if (r->src.addr.p.dyn->
2898                                                     pfid_acnt4 < 1)
2899                                                         return (NULL);
2900                                                 PF_POOLMASK(naddr,
2901                                                     &r->src.addr.p.dyn->
2902                                                     pfid_addr4,
2903                                                     &r->src.addr.p.dyn->
2904                                                     pfid_mask4,
2905                                                     daddr, AF_INET);
2906                                                 break;
2907 #endif /* INET */
2908 #ifdef INET6
2909                                         case AF_INET6:
2910                                                 if (r->src.addr.p.dyn->
2911                                                     pfid_acnt6 < 1)
2912                                                         return (NULL);
2913                                                 PF_POOLMASK(naddr,
2914                                                     &r->src.addr.p.dyn->
2915                                                     pfid_addr6,
2916                                                     &r->src.addr.p.dyn->
2917                                                     pfid_mask6,
2918                                                     daddr, AF_INET6);
2919                                                 break;
2920 #endif /* INET6 */
2921                                         }
2922                                 } else
2923                                         PF_POOLMASK(naddr,
2924                                             &r->src.addr.v.a.addr,
2925                                             &r->src.addr.v.a.mask, daddr,
2926                                             pd->af);
2927                                 break;
2928                         }
2929                         break;
2930                 case PF_RDR: {
2931                         if (pf_map_addr(pd->af, r, saddr, naddr, NULL, sn))
2932                                 return (NULL);
2933                         if ((r->rpool.opts & PF_POOL_TYPEMASK) ==
2934                             PF_POOL_BITMASK)
2935                                 PF_POOLMASK(naddr, naddr,
2936                                     &r->rpool.cur->addr.v.a.mask, daddr,
2937                                     pd->af);
2938
2939                         if (r->rpool.proxy_port[1]) {
2940                                 u_int32_t       tmp_nport;
2941
2942                                 tmp_nport = ((ntohs(dport) -
2943                                     ntohs(r->dst.port[0])) %
2944                                     (r->rpool.proxy_port[1] -
2945                                     r->rpool.proxy_port[0] + 1)) +
2946                                     r->rpool.proxy_port[0];
2947
2948                                 /* wrap around if necessary */
2949                                 if (tmp_nport > 65535)
2950                                         tmp_nport -= 65535;
2951                                 *nport = htons((u_int16_t)tmp_nport);
2952                         } else if (r->rpool.proxy_port[0])
2953                                 *nport = htons(r->rpool.proxy_port[0]);
2954                         break;
2955                 }
2956                 default:
2957                         return (NULL);
2958                 }
2959         }
2960
2961         return (r);
2962 }
2963
2964 int
2965 #ifdef __FreeBSD__
2966 pf_socket_lookup(int direction, struct pf_pdesc *pd, struct inpcb *inp_arg)
2967 #else
2968 pf_socket_lookup(int direction, struct pf_pdesc *pd)
2969 #endif
2970 {
2971         struct pf_addr          *saddr, *daddr;
2972         u_int16_t                sport, dport;
2973 #ifdef __FreeBSD__
2974         struct inpcbinfo        *pi;
2975 #else
2976         struct inpcbtable       *tb;
2977 #endif
2978         struct inpcb            *inp;
2979
2980         if (pd == NULL)
2981                 return (-1);
2982         pd->lookup.uid = UID_MAX;
2983         pd->lookup.gid = GID_MAX;
2984         pd->lookup.pid = NO_PID;                /* XXX: revisit */
2985 #ifdef __FreeBSD__
2986         if (inp_arg != NULL) {
2987                 INP_LOCK_ASSERT(inp_arg);
2988                 pd->lookup.uid = inp_arg->inp_cred->cr_uid;
2989                 pd->lookup.gid = inp_arg->inp_cred->cr_groups[0];
2990                 return (1);
2991         }
2992 #endif
2993         switch (pd->proto) {
2994         case IPPROTO_TCP:
2995                 if (pd->hdr.tcp == NULL)
2996                         return (-1);
2997                 sport = pd->hdr.tcp->th_sport;
2998                 dport = pd->hdr.tcp->th_dport;
2999 #ifdef __FreeBSD__
3000                 pi = &V_tcbinfo;
3001 #else
3002                 tb = &tcbtable;
3003 #endif
3004                 break;
3005         case IPPROTO_UDP:
3006                 if (pd->hdr.udp == NULL)
3007                         return (-1);
3008                 sport = pd->hdr.udp->uh_sport;
3009                 dport = pd->hdr.udp->uh_dport;
3010 #ifdef __FreeBSD__
3011                 pi = &V_udbinfo;
3012 #else
3013                 tb = &udbtable;
3014 #endif
3015                 break;
3016         default:
3017                 return (-1);
3018         }
3019         if (direction == PF_IN) {
3020                 saddr = pd->src;
3021                 daddr = pd->dst;
3022         } else {
3023                 u_int16_t       p;
3024
3025                 p = sport;
3026                 sport = dport;
3027                 dport = p;
3028                 saddr = pd->dst;
3029                 daddr = pd->src;
3030         }
3031         switch (pd->af) {
3032 #ifdef INET
3033         case AF_INET:
3034 #ifdef __FreeBSD__
3035                 INP_INFO_RLOCK(pi);     /* XXX LOR */
3036                 inp = in_pcblookup_hash(pi, saddr->v4, sport, daddr->v4,
3037                         dport, 0, NULL);
3038                 if (inp == NULL) {
3039                         inp = in_pcblookup_hash(pi, saddr->v4, sport,
3040                            daddr->v4, dport, INPLOOKUP_WILDCARD, NULL);
3041                         if(inp == NULL) {
3042                                 INP_INFO_RUNLOCK(pi);
3043                                 return (-1);
3044                         }
3045                 }
3046 #else
3047                 inp = in_pcbhashlookup(tb, saddr->v4, sport, daddr->v4, dport);
3048                 if (inp == NULL) {
3049                         inp = in_pcblookup_listen(tb, daddr->v4, dport, 0);
3050                         if (inp == NULL)
3051                                 return (-1);
3052                 }
3053 #endif
3054                 break;
3055 #endif /* INET */
3056 #ifdef INET6
3057         case AF_INET6:
3058 #ifdef __FreeBSD__
3059                 INP_INFO_RLOCK(pi);
3060                 inp = in6_pcblookup_hash(pi, &saddr->v6, sport,
3061                         &daddr->v6, dport, 0, NULL);
3062                 if (inp == NULL) {
3063                         inp = in6_pcblookup_hash(pi, &saddr->v6, sport,
3064                         &daddr->v6, dport, INPLOOKUP_WILDCARD, NULL);
3065                         if (inp == NULL) {
3066                                 INP_INFO_RUNLOCK(pi);
3067                                 return (-1);
3068                         }
3069                 }
3070 #else
3071                 inp = in6_pcbhashlookup(tb, &saddr->v6, sport, &daddr->v6,
3072                     dport);
3073                 if (inp == NULL) {
3074                         inp = in6_pcblookup_listen(tb, &daddr->v6, dport, 0);
3075                         if (inp == NULL)
3076                                 return (-1);
3077                 }
3078 #endif
3079                 break;
3080 #endif /* INET6 */
3081
3082         default:
3083                 return (-1);
3084         }
3085 #ifdef __FreeBSD__
3086         pd->lookup.uid = inp->inp_cred->cr_uid;
3087         pd->lookup.gid = inp->inp_cred->cr_groups[0];
3088         INP_INFO_RUNLOCK(pi);
3089 #else
3090         pd->lookup.uid = inp->inp_socket->so_euid;
3091         pd->lookup.gid = inp->inp_socket->so_egid;
3092         pd->lookup.pid = inp->inp_socket->so_cpid;
3093 #endif
3094         return (1);
3095 }
3096
3097 u_int8_t
3098 pf_get_wscale(struct mbuf *m, int off, u_int16_t th_off, sa_family_t af)
3099 {
3100         int              hlen;
3101         u_int8_t         hdr[60];
3102         u_int8_t        *opt, optlen;
3103         u_int8_t         wscale = 0;
3104
3105         hlen = th_off << 2;             /* hlen <= sizeof(hdr) */
3106         if (hlen <= sizeof(struct tcphdr))
3107                 return (0);
3108         if (!pf_pull_hdr(m, off, hdr, hlen, NULL, NULL, af))
3109                 return (0);
3110         opt = hdr + sizeof(struct tcphdr);
3111         hlen -= sizeof(struct tcphdr);
3112         while (hlen >= 3) {
3113                 switch (*opt) {
3114                 case TCPOPT_EOL:
3115                 case TCPOPT_NOP:
3116                         ++opt;
3117                         --hlen;
3118                         break;
3119                 case TCPOPT_WINDOW:
3120                         wscale = opt[2];
3121                         if (wscale > TCP_MAX_WINSHIFT)
3122                                 wscale = TCP_MAX_WINSHIFT;
3123                         wscale |= PF_WSCALE_FLAG;
3124                         /* FALLTHROUGH */
3125                 default:
3126                         optlen = opt[1];
3127                         if (optlen < 2)
3128                                 optlen = 2;
3129                         hlen -= optlen;
3130                         opt += optlen;
3131                         break;
3132                 }
3133         }
3134         return (wscale);
3135 }
3136
3137 u_int16_t
3138 pf_get_mss(struct mbuf *m, int off, u_int16_t th_off, sa_family_t af)
3139 {
3140         int              hlen;
3141         u_int8_t         hdr[60];
3142         u_int8_t        *opt, optlen;
3143         u_int16_t        mss = V_tcp_mssdflt;
3144
3145         hlen = th_off << 2;     /* hlen <= sizeof(hdr) */
3146         if (hlen <= sizeof(struct tcphdr))
3147                 return (0);
3148         if (!pf_pull_hdr(m, off, hdr, hlen, NULL, NULL, af))
3149                 return (0);
3150         opt = hdr + sizeof(struct tcphdr);
3151         hlen -= sizeof(struct tcphdr);
3152         while (hlen >= TCPOLEN_MAXSEG) {
3153                 switch (*opt) {
3154                 case TCPOPT_EOL:
3155                 case TCPOPT_NOP:
3156                         ++opt;
3157                         --hlen;
3158                         break;
3159                 case TCPOPT_MAXSEG:
3160                         bcopy((caddr_t)(opt + 2), (caddr_t)&mss, 2);
3161                         NTOHS(mss);
3162                         /* FALLTHROUGH */
3163                 default:
3164                         optlen = opt[1];
3165                         if (optlen < 2)
3166                                 optlen = 2;
3167                         hlen -= optlen;
3168                         opt += optlen;
3169                         break;
3170                 }
3171         }
3172         return (mss);
3173 }
3174
3175 u_int16_t
3176 pf_calc_mss(struct pf_addr *addr, sa_family_t af, u_int16_t offer)
3177 {
3178 #ifdef INET
3179         struct sockaddr_in      *dst;
3180         struct route             ro;
3181 #endif /* INET */
3182 #ifdef INET6
3183         struct sockaddr_in6     *dst6;
3184         struct route_in6         ro6;
3185 #endif /* INET6 */
3186         struct rtentry          *rt = NULL;
3187         int                      hlen = 0;      /* make the compiler happy */
3188         u_int16_t                mss = V_tcp_mssdflt;
3189
3190         switch (af) {
3191 #ifdef INET
3192         case AF_INET:
3193                 hlen = sizeof(struct ip);
3194                 bzero(&ro, sizeof(ro));
3195                 dst = (struct sockaddr_in *)&ro.ro_dst;
3196                 dst->sin_family = AF_INET;
3197                 dst->sin_len = sizeof(*dst);
3198                 dst->sin_addr = addr->v4;
3199 #ifdef __FreeBSD__
3200                 in_rtalloc_ign(&ro, 0, RT_DEFAULT_FIB);
3201 #else /* ! __FreeBSD__ */
3202                 rtalloc_noclone(&ro, NO_CLONING);
3203 #endif
3204                 rt = ro.ro_rt;
3205                 break;
3206 #endif /* INET */
3207 #ifdef INET6
3208         case AF_INET6:
3209                 hlen = sizeof(struct ip6_hdr);
3210                 bzero(&ro6, sizeof(ro6));
3211                 dst6 = (struct sockaddr_in6 *)&ro6.ro_dst;
3212                 dst6->sin6_family = AF_INET6;
3213                 dst6->sin6_len = sizeof(*dst6);
3214                 dst6->sin6_addr = addr->v6;
3215 #ifdef __FreeBSD__
3216                 in6_rtalloc_ign(&ro6, 0, RT_DEFAULT_FIB);
3217 #else /* ! __FreeBSD__ */
3218                 rtalloc_noclone((struct route *)&ro6, NO_CLONING);
3219 #endif
3220                 rt = ro6.ro_rt;
3221                 break;
3222 #endif /* INET6 */
3223         }
3224
3225         if (rt && rt->rt_ifp) {
3226                 mss = rt->rt_ifp->if_mtu - hlen - sizeof(struct tcphdr);
3227                 mss = max(V_tcp_mssdflt, mss);
3228                 RTFREE(rt);
3229         }
3230         mss = min(mss, offer);
3231         mss = max(mss, 64);             /* sanity - at least max opt space */
3232         return (mss);
3233 }
3234
3235 void
3236 pf_set_rt_ifp(struct pf_state *s, struct pf_addr *saddr)
3237 {
3238         struct pf_rule *r = s->rule.ptr;
3239
3240         s->rt_kif = NULL;
3241         if (!r->rt || r->rt == PF_FASTROUTE)
3242                 return;
3243         switch (s->af) {
3244 #ifdef INET
3245         case AF_INET:
3246                 pf_map_addr(AF_INET, r, saddr, &s->rt_addr, NULL,
3247                     &s->nat_src_node);
3248                 s->rt_kif = r->rpool.cur->kif;
3249                 break;
3250 #endif /* INET */
3251 #ifdef INET6
3252         case AF_INET6:
3253                 pf_map_addr(AF_INET6, r, saddr, &s->rt_addr, NULL,
3254                     &s->nat_src_node);
3255                 s->rt_kif = r->rpool.cur->kif;
3256                 break;
3257 #endif /* INET6 */
3258         }
3259 }
3260
3261 int
3262 pf_test_tcp(struct pf_rule **rm, struct pf_state **sm, int direction,
3263     struct pfi_kif *kif, struct mbuf *m, int off, void *h,
3264 #ifdef __FreeBSD__
3265     struct pf_pdesc *pd, struct pf_rule **am, struct pf_ruleset **rsm,
3266     struct ifqueue *ifq, struct inpcb *inp)
3267 #else
3268     struct pf_pdesc *pd, struct pf_rule **am, struct pf_ruleset **rsm,
3269     struct ifqueue *ifq)
3270 #endif
3271 {
3272         struct pf_rule          *nr = NULL;
3273         struct pf_addr          *saddr = pd->src, *daddr = pd->dst;
3274         struct tcphdr           *th = pd->hdr.tcp;
3275         u_int16_t                bport, nport = 0;
3276         sa_family_t              af = pd->af;
3277         struct pf_rule          *r, *a = NULL;
3278         struct pf_ruleset       *ruleset = NULL;
3279         struct pf_src_node      *nsn = NULL;
3280         u_short                  reason;
3281         int                      rewrite = 0;
3282         int                      tag = -1, rtableid = -1;
3283         u_int16_t                mss = V_tcp_mssdflt;
3284         int                      asd = 0;
3285         int                      match = 0;
3286
3287         if (pf_check_congestion(ifq)) {
3288                 REASON_SET(&reason, PFRES_CONGEST);
3289                 return (PF_DROP);
3290         }
3291
3292 #ifdef __FreeBSD__
3293         if (inp != NULL)
3294                 pd->lookup.done = pf_socket_lookup(direction, pd, inp);
3295         else if (debug_pfugidhack) {
3296                 PF_UNLOCK();
3297                 DPFPRINTF(PF_DEBUG_MISC, ("pf: unlocked lookup\n"));
3298                 pd->lookup.done = pf_socket_lookup(direction, pd, inp);
3299                 PF_LOCK();
3300         }
3301 #endif
3302
3303         r = TAILQ_FIRST(pf_main_ruleset.rules[PF_RULESET_FILTER].active.ptr);
3304
3305         if (direction == PF_OUT) {
3306                 bport = nport = th->th_sport;
3307                 /* check outgoing packet for BINAT/NAT */
3308                 if ((nr = pf_get_translation(pd, m, off, PF_OUT, kif, &nsn,
3309                     saddr, th->th_sport, daddr, th->th_dport,
3310                     &pd->naddr, &nport)) != NULL) {
3311                         PF_ACPY(&pd->baddr, saddr, af);
3312                         pf_change_ap(saddr, &th->th_sport, pd->ip_sum,
3313                             &th->th_sum, &pd->naddr, nport, 0, af);
3314                         rewrite++;
3315                         if (nr->natpass)
3316                                 r = NULL;
3317                         pd->nat_rule = nr;
3318                 }
3319         } else {
3320                 bport = nport = th->th_dport;
3321                 /* check incoming packet for BINAT/RDR */
3322                 if ((nr = pf_get_translation(pd, m, off, PF_IN, kif, &nsn,
3323                     saddr, th->th_sport, daddr, th->th_dport,
3324                     &pd->naddr, &nport)) != NULL) {
3325                         PF_ACPY(&pd->baddr, daddr, af);
3326                         pf_change_ap(daddr, &th->th_dport, pd->ip_sum,
3327                             &th->th_sum, &pd->naddr, nport, 0, af);
3328                         rewrite++;
3329                         if (nr->natpass)
3330                                 r = NULL;
3331                         pd->nat_rule = nr;
3332                 }
3333         }
3334
3335         while (r != NULL) {
3336                 r->evaluations++;
3337                 if (pfi_kif_match(r->kif, kif) == r->ifnot)
3338                         r = r->skip[PF_SKIP_IFP].ptr;
3339                 else if (r->direction && r->direction != direction)
3340                         r = r->skip[PF_SKIP_DIR].ptr;
3341                 else if (r->af && r->af != af)
3342                         r = r->skip[PF_SKIP_AF].ptr;
3343                 else if (r->proto && r->proto != IPPROTO_TCP)
3344                         r = r->skip[PF_SKIP_PROTO].ptr;
3345                 else if (PF_MISMATCHAW(&r->src.addr, saddr, af,
3346                     r->src.neg, kif))
3347                         r = r->skip[PF_SKIP_SRC_ADDR].ptr;
3348                 else if (r->src.port_op && !pf_match_port(r->src.port_op,
3349                     r->src.port[0], r->src.port[1], th->th_sport))
3350                         r = r->skip[PF_SKIP_SRC_PORT].ptr;
3351                 else if (PF_MISMATCHAW(&r->dst.addr, daddr, af,
3352                     r->dst.neg, NULL))
3353                         r = r->skip[PF_SKIP_DST_ADDR].ptr;
3354                 else if (r->dst.port_op && !pf_match_port(r->dst.port_op,
3355                     r->dst.port[0], r->dst.port[1], th->th_dport))
3356                         r = r->skip[PF_SKIP_DST_PORT].ptr;
3357                 else if (r->tos && !(r->tos == pd->tos))
3358                         r = TAILQ_NEXT(r, entries);
3359                 else if (r->rule_flag & PFRULE_FRAGMENT)
3360                         r = TAILQ_NEXT(r, entries);
3361                 else if ((r->flagset & th->th_flags) != r->flags)
3362                         r = TAILQ_NEXT(r, entries);
3363                 else if (r->uid.op && (pd->lookup.done || (pd->lookup.done =
3364 #ifdef __FreeBSD__
3365                     pf_socket_lookup(direction, pd, inp), 1)) &&
3366 #else
3367                     pf_socket_lookup(direction, pd), 1)) &&
3368 #endif
3369                     !pf_match_uid(r->uid.op, r->uid.uid[0], r->uid.uid[1],
3370                     pd->lookup.uid))
3371                         r = TAILQ_NEXT(r, entries);
3372                 else if (r->gid.op && (pd->lookup.done || (pd->lookup.done =
3373 #ifdef __FreeBSD__
3374                     pf_socket_lookup(direction, pd, inp), 1)) &&
3375 #else
3376                     pf_socket_lookup(direction, pd), 1)) &&
3377 #endif
3378                     !pf_match_gid(r->gid.op, r->gid.gid[0], r->gid.gid[1],
3379                     pd->lookup.gid))
3380                         r = TAILQ_NEXT(r, entries);
3381                 else if (r->prob && r->prob <= arc4random())
3382                         r = TAILQ_NEXT(r, entries);
3383                 else if (r->match_tag && !pf_match_tag(m, r, pd->pf_mtag, &tag))
3384                         r = TAILQ_NEXT(r, entries);
3385                 else if (r->os_fingerprint != PF_OSFP_ANY && !pf_osfp_match(
3386                     pf_osfp_fingerprint(pd, m, off, th), r->os_fingerprint))
3387                         r = TAILQ_NEXT(r, entries);
3388                 else {
3389                         if (r->tag)
3390                                 tag = r->tag;
3391                         if (r->rtableid >= 0)
3392                                 rtableid = r->rtableid;
3393                         if (r->anchor == NULL) {
3394                                 match = 1;
3395                                 *rm = r;
3396                                 *am = a;
3397                                 *rsm = ruleset;
3398                                 if ((*rm)->quick)
3399                                         break;
3400                                 r = TAILQ_NEXT(r, entries);
3401                         } else
3402                                 pf_step_into_anchor(&asd, &ruleset,
3403                                     PF_RULESET_FILTER, &r, &a, &match);
3404                 }
3405                 if (r == NULL && pf_step_out_of_anchor(&asd, &ruleset,
3406                     PF_RULESET_FILTER, &r, &a, &match))
3407                         break;
3408         }
3409         r = *rm;
3410         a = *am;
3411         ruleset = *rsm;
3412
3413         REASON_SET(&reason, PFRES_MATCH);
3414
3415         if (r->log || (nr != NULL && nr->natpass && nr->log)) {
3416                 if (rewrite)
3417 #ifdef __FreeBSD__
3418                         m_copyback(m, off, sizeof(*th), (caddr_t)th);
3419 #else
3420                         m_copyback(m, off, sizeof(*th), th);
3421 #endif
3422                 PFLOG_PACKET(kif, h, m, af, direction, reason, r->log ? r : nr,
3423                     a, ruleset, pd);
3424         }
3425
3426         if ((r->action == PF_DROP) &&
3427             ((r->rule_flag & PFRULE_RETURNRST) ||
3428             (r->rule_flag & PFRULE_RETURNICMP) ||
3429             (r->rule_flag & PFRULE_RETURN))) {
3430                 /* undo NAT changes, if they have taken place */
3431                 if (nr != NULL) {
3432                         if (direction == PF_OUT) {
3433                                 pf_change_ap(saddr, &th->th_sport, pd->ip_sum,
3434                                     &th->th_sum, &pd->baddr, bport, 0, af);
3435                                 rewrite++;
3436                         } else {
3437                                 pf_change_ap(daddr, &th->th_dport, pd->ip_sum,
3438                                     &th->th_sum, &pd->baddr, bport, 0, af);
3439                                 rewrite++;
3440                         }
3441                 }
3442                 if (((r->rule_flag & PFRULE_RETURNRST) ||
3443                     (r->rule_flag & PFRULE_RETURN)) &&
3444                     !(th->th_flags & TH_RST)) {
3445                         u_int32_t ack = ntohl(th->th_seq) + pd->p_len;
3446
3447                         if (th->th_flags & TH_SYN)
3448                                 ack++;
3449                         if (th->th_flags & TH_FIN)
3450                                 ack++;
3451 #ifdef __FreeBSD__
3452                         pf_send_tcp(m, r, af, pd->dst,
3453 #else
3454                         pf_send_tcp(r, af, pd->dst,
3455 #endif
3456                             pd->src, th->th_dport, th->th_sport,
3457                             ntohl(th->th_ack), ack, TH_RST|TH_ACK, 0, 0,
3458                             r->return_ttl, 1, 0, pd->eh, kif->pfik_ifp);
3459                 } else if ((af == AF_INET) && r->return_icmp)
3460                         pf_send_icmp(m, r->return_icmp >> 8,
3461                             r->return_icmp & 255, af, r);
3462                 else if ((af == AF_INET6) && r->return_icmp6)
3463                         pf_send_icmp(m, r->return_icmp6 >> 8,
3464                             r->return_icmp6 & 255, af, r);
3465         }
3466
3467         if (r->action == PF_DROP)
3468                 return (PF_DROP);
3469
3470         if (pf_tag_packet(m, pd->pf_mtag, tag, rtableid)) {
3471                 REASON_SET(&reason, PFRES_MEMORY);
3472                 return (PF_DROP);
3473         }
3474
3475         if (r->keep_state || nr != NULL ||
3476             (pd->flags & PFDESC_TCP_NORM)) {
3477                 /* create new state */
3478                 u_int16_t        len;
3479                 struct pf_state *s = NULL;
3480                 struct pf_src_node *sn = NULL;
3481
3482                 len = pd->tot_len - off - (th->th_off << 2);
3483
3484                 /* check maximums */
3485                 if (r->max_states && (r->states >= r->max_states)) {
3486                         pf_status.lcounters[LCNT_STATES]++;
3487                         REASON_SET(&reason, PFRES_MAXSTATES);
3488                         goto cleanup;
3489                 }
3490                 /* src node for filter rule */
3491                 if ((r->rule_flag & PFRULE_SRCTRACK ||
3492                     r->rpool.opts & PF_POOL_STICKYADDR) &&
3493                     pf_insert_src_node(&sn, r, saddr, af) != 0) {
3494                         REASON_SET(&reason, PFRES_SRCLIMIT);
3495                         goto cleanup;
3496                 }
3497                 /* src node for translation rule */
3498                 if (nr != NULL && (nr->rpool.opts & PF_POOL_STICKYADDR) &&
3499                     ((direction == PF_OUT &&
3500                     pf_insert_src_node(&nsn, nr, &pd->baddr, af) != 0) ||
3501                     (pf_insert_src_node(&nsn, nr, saddr, af) != 0))) {
3502                         REASON_SET(&reason, PFRES_SRCLIMIT);
3503                         goto cleanup;
3504                 }
3505                 s = pool_get(&pf_state_pl, PR_NOWAIT);
3506                 if (s == NULL) {
3507                         REASON_SET(&reason, PFRES_MEMORY);
3508 cleanup:
3509                         if (sn != NULL && sn->states == 0 && sn->expire == 0) {
3510                                 RB_REMOVE(pf_src_tree, &tree_src_tracking, sn);
3511                                 pf_status.scounters[SCNT_SRC_NODE_REMOVALS]++;
3512                                 pf_status.src_nodes--;
3513                                 pool_put(&pf_src_tree_pl, sn);
3514                         }
3515                         if (nsn != sn && nsn != NULL && nsn->states == 0 &&
3516                             nsn->expire == 0) {
3517                                 RB_REMOVE(pf_src_tree, &tree_src_tracking, nsn);
3518                                 pf_status.scounters[SCNT_SRC_NODE_REMOVALS]++;
3519                                 pf_status.src_nodes--;
3520                                 pool_put(&pf_src_tree_pl, nsn);
3521                         }
3522                         return (PF_DROP);
3523                 }
3524                 bzero(s, sizeof(*s));
3525                 s->rule.ptr = r;
3526                 s->nat_rule.ptr = nr;
3527                 s->anchor.ptr = a;
3528                 STATE_INC_COUNTERS(s);
3529                 if (r->allow_opts)
3530                         s->state_flags |= PFSTATE_ALLOWOPTS;
3531                 if (r->rule_flag & PFRULE_STATESLOPPY)
3532                         s->state_flags |= PFSTATE_SLOPPY;
3533                 s->log = r->log & PF_LOG_ALL;
3534                 if (nr != NULL)
3535                         s->log |= nr->log & PF_LOG_ALL;
3536                 s->proto = IPPROTO_TCP;
3537                 s->direction = direction;
3538                 s->af = af;
3539                 if (direction == PF_OUT) {
3540                         PF_ACPY(&s->gwy.addr, saddr, af);
3541                         s->gwy.port = th->th_sport;             /* sport */
3542                         PF_ACPY(&s->ext.addr, daddr, af);
3543                         s->ext.port = th->th_dport;
3544                         if (nr != NULL) {
3545                                 PF_ACPY(&s->lan.addr, &pd->baddr, af);
3546                                 s->lan.port = bport;
3547                         } else {
3548                                 PF_ACPY(&s->lan.addr, &s->gwy.addr, af);
3549                                 s->lan.port = s->gwy.port;
3550                         }
3551                 } else {
3552                         PF_ACPY(&s->lan.addr, daddr, af);
3553                         s->lan.port = th->th_dport;
3554                         PF_ACPY(&s->ext.addr, saddr, af);
3555                         s->ext.port = th->th_sport;
3556                         if (nr != NULL) {
3557                                 PF_ACPY(&s->gwy.addr, &pd->baddr, af);
3558                                 s->gwy.port = bport;
3559                         } else {
3560                                 PF_ACPY(&s->gwy.addr, &s->lan.addr, af);
3561                                 s->gwy.port = s->lan.port;
3562                         }
3563                 }
3564
3565                 s->src.seqlo = ntohl(th->th_seq);
3566                 s->src.seqhi = s->src.seqlo + len + 1;
3567                 if ((th->th_flags & (TH_SYN|TH_ACK)) == TH_SYN &&
3568                     r->keep_state == PF_STATE_MODULATE) {
3569                         /* Generate sequence number modulator */
3570 #ifdef __FreeBSD__
3571                         while ((s->src.seqdiff =
3572                             pf_new_isn(s) - s->src.seqlo) == 0)
3573                                 ;       
3574 #else
3575                         while ((s->src.seqdiff =
3576                             tcp_rndiss_next() - s->src.seqlo) == 0)
3577                                 ;
3578 #endif
3579                         pf_change_a(&th->th_seq, &th->th_sum,
3580                             htonl(s->src.seqlo + s->src.seqdiff), 0);
3581                         rewrite = 1;
3582                 } else
3583                         s->src.seqdiff = 0;
3584                 if (th->th_flags & TH_SYN) {
3585                         s->src.seqhi++;
3586                         s->src.wscale = pf_get_wscale(m, off, th->th_off, af);
3587                 }
3588                 s->src.max_win = MAX(ntohs(th->th_win), 1);
3589                 if (s->src.wscale & PF_WSCALE_MASK) {
3590                         /* Remove scale factor from initial window */
3591                         int win = s->src.max_win;
3592                         win += 1 << (s->src.wscale & PF_WSCALE_MASK);
3593                         s->src.max_win = (win - 1) >>
3594                             (s->src.wscale & PF_WSCALE_MASK);
3595                 }
3596                 if (th->th_flags & TH_FIN)
3597                         s->src.seqhi++;
3598                 s->dst.seqhi = 1;
3599                 s->dst.max_win = 1;
3600                 s->src.state = TCPS_SYN_SENT;
3601                 s->dst.state = TCPS_CLOSED;
3602                 s->creation = time_second;
3603                 s->expire = time_second;
3604                 s->timeout = PFTM_TCP_FIRST_PACKET;
3605                 pf_set_rt_ifp(s, saddr);
3606                 if (sn != NULL) {
3607                         s->src_node = sn;
3608                         s->src_node->states++;
3609                 }
3610                 if (nsn != NULL) {
3611                         PF_ACPY(&nsn->raddr, &pd->naddr, af);
3612                         s->nat_src_node = nsn;
3613                         s->nat_src_node->states++;
3614                 }
3615                 if ((pd->flags & PFDESC_TCP_NORM) && pf_normalize_tcp_init(m,
3616                     off, pd, th, &s->src, &s->dst)) {
3617                         REASON_SET(&reason, PFRES_MEMORY);
3618                         pf_src_tree_remove_state(s);
3619                         STATE_DEC_COUNTERS(s);
3620                         pool_put(&pf_state_pl, s);
3621                         return (PF_DROP);
3622                 }
3623                 if ((pd->flags & PFDESC_TCP_NORM) && s->src.scrub &&
3624                     pf_normalize_tcp_stateful(m, off, pd, &reason, th, s,
3625                     &s->src, &s->dst, &rewrite)) {
3626                         /* This really shouldn't happen!!! */
3627                         DPFPRINTF(PF_DEBUG_URGENT,
3628                             ("pf_normalize_tcp_stateful failed on first pkt"));
3629                         pf_normalize_tcp_cleanup(s);
3630                         pf_src_tree_remove_state(s);
3631                         STATE_DEC_COUNTERS(s);
3632                         pool_put(&pf_state_pl, s);
3633                         return (PF_DROP);
3634                 }
3635                 if (pf_insert_state(BOUND_IFACE(r, kif), s)) {
3636                         pf_normalize_tcp_cleanup(s);
3637                         REASON_SET(&reason, PFRES_STATEINS);
3638                         pf_src_tree_remove_state(s);
3639                         STATE_DEC_COUNTERS(s);
3640                         pool_put(&pf_state_pl, s);
3641                         return (PF_DROP);
3642                 } else
3643                         *sm = s;
3644                 if (tag > 0) {
3645                         pf_tag_ref(tag);
3646                         s->tag = tag;
3647                 }
3648                 if ((th->th_flags & (TH_SYN|TH_ACK)) == TH_SYN &&
3649                     r->keep_state == PF_STATE_SYNPROXY) {
3650                         s->src.state = PF_TCPS_PROXY_SRC;
3651                         if (nr != NULL) {
3652                                 if (direction == PF_OUT) {
3653                                         pf_change_ap(saddr, &th->th_sport,
3654                                             pd->ip_sum, &th->th_sum, &pd->baddr,
3655                                             bport, 0, af);
3656                                 } else {
3657                                         pf_change_ap(daddr, &th->th_dport,
3658                                             pd->ip_sum, &th->th_sum, &pd->baddr,
3659                                             bport, 0, af);
3660                                 }
3661                         }
3662                         s->src.seqhi = htonl(arc4random());
3663                         /* Find mss option */
3664                         mss = pf_get_mss(m, off, th->th_off, af);
3665                         mss = pf_calc_mss(saddr, af, mss);
3666                         mss = pf_calc_mss(daddr, af, mss);
3667                         s->src.mss = mss;
3668 #ifdef __FreeBSD__
3669                         pf_send_tcp(NULL, r, af, daddr, saddr, th->th_dport,
3670 #else
3671                         pf_send_tcp(r, af, daddr, saddr, th->th_dport,
3672 #endif
3673                             th->th_sport, s->src.seqhi, ntohl(th->th_seq) + 1,
3674                             TH_SYN|TH_ACK, 0, s->src.mss, 0, 1, 0, NULL, NULL);
3675                         REASON_SET(&reason, PFRES_SYNPROXY);
3676                         return (PF_SYNPROXY_DROP);
3677                 }
3678         }
3679
3680         /* copy back packet headers if we performed NAT operations */
3681         if (rewrite)
3682                 m_copyback(m, off, sizeof(*th), (caddr_t)th);
3683
3684         return (PF_PASS);
3685 }
3686
3687 int
3688 pf_test_udp(struct pf_rule **rm, struct pf_state **sm, int direction,
3689     struct pfi_kif *kif, struct mbuf *m, int off, void *h,
3690 #ifdef __FreeBSD__
3691     struct pf_pdesc *pd, struct pf_rule **am, struct pf_ruleset **rsm,
3692     struct ifqueue *ifq, struct inpcb *inp)
3693 #else
3694     struct pf_pdesc *pd, struct pf_rule **am, struct pf_ruleset **rsm,
3695     struct ifqueue *ifq)
3696 #endif
3697 {
3698         struct pf_rule          *nr = NULL;
3699         struct pf_addr          *saddr = pd->src, *daddr = pd->dst;
3700         struct udphdr           *uh = pd->hdr.udp;
3701         u_int16_t                bport, nport = 0;
3702         sa_family_t              af = pd->af;
3703         struct pf_rule          *r, *a = NULL;
3704         struct pf_ruleset       *ruleset = NULL;
3705         struct pf_src_node      *nsn = NULL;
3706         u_short                  reason;
3707         int                      rewrite = 0;
3708         int                      tag = -1, rtableid = -1;
3709         int                      asd = 0;
3710         int                      match = 0;
3711
3712         if (pf_check_congestion(ifq)) {
3713                 REASON_SET(&reason, PFRES_CONGEST);
3714                 return (PF_DROP);
3715         }
3716
3717 #ifdef __FreeBSD__
3718         if (inp != NULL)
3719                 pd->lookup.done = pf_socket_lookup(direction, pd, inp);
3720         else if (debug_pfugidhack) {
3721                 PF_UNLOCK();
3722                 DPFPRINTF(PF_DEBUG_MISC, ("pf: unlocked lookup\n"));
3723                 pd->lookup.done = pf_socket_lookup(direction, pd, inp);
3724                 PF_LOCK();
3725         }
3726 #endif
3727
3728         r = TAILQ_FIRST(pf_main_ruleset.rules[PF_RULESET_FILTER].active.ptr);
3729
3730         if (direction == PF_OUT) {
3731                 bport = nport = uh->uh_sport;
3732                 /* check outgoing packet for BINAT/NAT */
3733                 if ((nr = pf_get_translation(pd, m, off, PF_OUT, kif, &nsn,
3734                     saddr, uh->uh_sport, daddr, uh->uh_dport,
3735                     &pd->naddr, &nport)) != NULL) {
3736                         PF_ACPY(&pd->baddr, saddr, af);
3737                         pf_change_ap(saddr, &uh->uh_sport, pd->ip_sum,
3738                             &uh->uh_sum, &pd->naddr, nport, 1, af);
3739                         rewrite++;
3740                         if (nr->natpass)
3741                                 r = NULL;
3742                         pd->nat_rule = nr;
3743                 }
3744         } else {
3745                 bport = nport = uh->uh_dport;
3746                 /* check incoming packet for BINAT/RDR */
3747                 if ((nr = pf_get_translation(pd, m, off, PF_IN, kif, &nsn,
3748                     saddr, uh->uh_sport, daddr, uh->uh_dport, &pd->naddr,
3749                     &nport)) != NULL) {
3750                         PF_ACPY(&pd->baddr, daddr, af);
3751                         pf_change_ap(daddr, &uh->uh_dport, pd->ip_sum,
3752                             &uh->uh_sum, &pd->naddr, nport, 1, af);
3753                         rewrite++;
3754                         if (nr->natpass)
3755                                 r = NULL;
3756                         pd->nat_rule = nr;
3757                 }
3758         }
3759
3760         while (r != NULL) {
3761                 r->evaluations++;
3762                 if (pfi_kif_match(r->kif, kif) == r->ifnot)
3763                         r = r->skip[PF_SKIP_IFP].ptr;
3764                 else if (r->direction && r->direction != direction)
3765                         r = r->skip[PF_SKIP_DIR].ptr;
3766                 else if (r->af && r->af != af)
3767                         r = r->skip[PF_SKIP_AF].ptr;
3768                 else if (r->proto && r->proto != IPPROTO_UDP)
3769                         r = r->skip[PF_SKIP_PROTO].ptr;
3770                 else if (PF_MISMATCHAW(&r->src.addr, saddr, af,
3771                     r->src.neg, kif))
3772                         r = r->skip[PF_SKIP_SRC_ADDR].ptr;
3773                 else if (r->src.port_op && !pf_match_port(r->src.port_op,
3774                     r->src.port[0], r->src.port[1], uh->uh_sport))
3775                         r = r->skip[PF_SKIP_SRC_PORT].ptr;
3776                 else if (PF_MISMATCHAW(&r->dst.addr, daddr, af,
3777                     r->dst.neg, NULL))
3778                         r = r->skip[PF_SKIP_DST_ADDR].ptr;
3779                 else if (r->dst.port_op && !pf_match_port(r->dst.port_op,
3780                     r->dst.port[0], r->dst.port[1], uh->uh_dport))
3781                         r = r->skip[PF_SKIP_DST_PORT].ptr;
3782                 else if (r->tos && !(r->tos == pd->tos))
3783                         r = TAILQ_NEXT(r, entries);
3784                 else if (r->rule_flag & PFRULE_FRAGMENT)
3785                         r = TAILQ_NEXT(r, entries);
3786                 else if (r->uid.op && (pd->lookup.done || (pd->lookup.done =
3787 #ifdef __FreeBSD__
3788                     pf_socket_lookup(direction, pd, inp), 1)) &&
3789 #else
3790                     pf_socket_lookup(direction, pd), 1)) &&
3791 #endif
3792                     !pf_match_uid(r->uid.op, r->uid.uid[0], r->uid.uid[1],
3793                     pd->lookup.uid))
3794                         r = TAILQ_NEXT(r, entries);
3795                 else if (r->gid.op && (pd->lookup.done || (pd->lookup.done =
3796 #ifdef __FreeBSD__
3797                     pf_socket_lookup(direction, pd, inp), 1)) &&
3798 #else
3799                     pf_socket_lookup(direction, pd), 1)) &&
3800 #endif
3801                     !pf_match_gid(r->gid.op, r->gid.gid[0], r->gid.gid[1],
3802                     pd->lookup.gid))
3803                         r = TAILQ_NEXT(r, entries);
3804                 else if (r->prob && r->prob <= arc4random())
3805                         r = TAILQ_NEXT(r, entries);
3806                 else if (r->match_tag && !pf_match_tag(m, r, pd->pf_mtag, &tag))
3807                         r = TAILQ_NEXT(r, entries);
3808                 else if (r->os_fingerprint != PF_OSFP_ANY)
3809                         r = TAILQ_NEXT(r, entries);
3810                 else {
3811                         if (r->tag)
3812                                 tag = r->tag;
3813                         if (r->rtableid >= 0)
3814                                 rtableid = r->rtableid;
3815                         if (r->anchor == NULL) {
3816                                 match = 1;
3817                                 *rm = r;
3818                                 *am = a;
3819                                 *rsm = ruleset;
3820                                 if ((*rm)->quick)
3821                                         break;
3822                                 r = TAILQ_NEXT(r, entries);
3823                         } else
3824                                 pf_step_into_anchor(&asd, &ruleset,
3825                                     PF_RULESET_FILTER, &r, &a, &match);
3826                 }
3827                 if (r == NULL && pf_step_out_of_anchor(&asd, &ruleset,
3828                     PF_RULESET_FILTER, &r, &a, &match))
3829                         break;
3830         }
3831         r = *rm;
3832         a = *am;
3833         ruleset = *rsm;
3834
3835         REASON_SET(&reason, PFRES_MATCH);
3836
3837         if (r->log || (nr != NULL && nr->natpass && nr->log)) {
3838                 if (rewrite)
3839 #ifdef __FreeBSD__
3840                         m_copyback(m, off, sizeof(*uh), (caddr_t)uh);
3841 #else
3842                         m_copyback(m, off, sizeof(*uh), uh);
3843 #endif
3844                 PFLOG_PACKET(kif, h, m, af, direction, reason, r->log ? r : nr,
3845                     a, ruleset, pd);
3846         }
3847
3848         if ((r->action == PF_DROP) &&
3849             ((r->rule_flag & PFRULE_RETURNICMP) ||
3850             (r->rule_flag & PFRULE_RETURN))) {
3851                 /* undo NAT changes, if they have taken place */
3852                 if (nr != NULL) {
3853                         if (direction == PF_OUT) {
3854                                 pf_change_ap(saddr, &uh->uh_sport, pd->ip_sum,
3855                                     &uh->uh_sum, &pd->baddr, bport, 1, af);
3856                                 rewrite++;
3857                         } else {
3858                                 pf_change_ap(daddr, &uh->uh_dport, pd->ip_sum,
3859                                     &uh->uh_sum, &pd->baddr, bport, 1, af);
3860                                 rewrite++;
3861                         }
3862                 }
3863                 if ((af == AF_INET) && r->return_icmp)
3864                         pf_send_icmp(m, r->return_icmp >> 8,
3865                             r->return_icmp & 255, af, r);
3866                 else if ((af == AF_INET6) && r->return_icmp6)
3867                         pf_send_icmp(m, r->return_icmp6 >> 8,
3868                             r->return_icmp6 & 255, af, r);
3869         }
3870
3871         if (r->action == PF_DROP)
3872                 return (PF_DROP);
3873
3874         if (pf_tag_packet(m, pd->pf_mtag, tag, rtableid)) {
3875                 REASON_SET(&reason, PFRES_MEMORY);
3876                 return (PF_DROP);
3877         }
3878
3879         if (r->keep_state || nr != NULL) {
3880                 /* create new state */
3881                 struct pf_state *s = NULL;
3882                 struct pf_src_node *sn = NULL;
3883
3884                 /* check maximums */
3885                 if (r->max_states && (r->states >= r->max_states)) {
3886                         pf_status.lcounters[LCNT_STATES]++;
3887                         REASON_SET(&reason, PFRES_MAXSTATES);
3888                         goto cleanup;
3889                 }
3890                 /* src node for filter rule */
3891                 if ((r->rule_flag & PFRULE_SRCTRACK ||
3892                     r->rpool.opts & PF_POOL_STICKYADDR) &&
3893                     pf_insert_src_node(&sn, r, saddr, af) != 0) {
3894                         REASON_SET(&reason, PFRES_SRCLIMIT);
3895                         goto cleanup;
3896                 }
3897                 /* src node for translation rule */
3898                 if (nr != NULL && (nr->rpool.opts & PF_POOL_STICKYADDR) &&
3899                     ((direction == PF_OUT &&
3900                     pf_insert_src_node(&nsn, nr, &pd->baddr, af) != 0) ||
3901                     (pf_insert_src_node(&nsn, nr, saddr, af) != 0))) {
3902                         REASON_SET(&reason, PFRES_SRCLIMIT);
3903                         goto cleanup;
3904                 }
3905                 s = pool_get(&pf_state_pl, PR_NOWAIT);
3906                 if (s == NULL) {
3907                         REASON_SET(&reason, PFRES_MEMORY);
3908 cleanup:
3909                         if (sn != NULL && sn->states == 0 && sn->expire == 0) {
3910                                 RB_REMOVE(pf_src_tree, &tree_src_tracking, sn);
3911                                 pf_status.scounters[SCNT_SRC_NODE_REMOVALS]++;
3912                                 pf_status.src_nodes--;
3913                                 pool_put(&pf_src_tree_pl, sn);
3914                         }
3915                         if (nsn != sn && nsn != NULL && nsn->states == 0 &&
3916                             nsn->expire == 0) {
3917                                 RB_REMOVE(pf_src_tree, &tree_src_tracking, nsn);
3918                                 pf_status.scounters[SCNT_SRC_NODE_REMOVALS]++;
3919                                 pf_status.src_nodes--;
3920                                 pool_put(&pf_src_tree_pl, nsn);
3921                         }
3922                         return (PF_DROP);
3923                 }
3924                 bzero(s, sizeof(*s));
3925                 s->rule.ptr = r;
3926                 s->nat_rule.ptr = nr;
3927                 s->anchor.ptr = a;
3928                 STATE_INC_COUNTERS(s);
3929                 if (r->allow_opts)
3930                         s->state_flags |= PFSTATE_ALLOWOPTS;
3931                 if (r->rule_flag & PFRULE_STATESLOPPY)
3932                         s->state_flags |= PFSTATE_SLOPPY;
3933                 s->log = r->log & PF_LOG_ALL;
3934                 if (nr != NULL)
3935                         s->log |= nr->log & PF_LOG_ALL;
3936                 s->proto = IPPROTO_UDP;
3937                 s->direction = direction;
3938                 s->af = af;
3939                 if (direction == PF_OUT) {
3940                         PF_ACPY(&s->gwy.addr, saddr, af);
3941                         s->gwy.port = uh->uh_sport;
3942                         PF_ACPY(&s->ext.addr, daddr, af);
3943                         s->ext.port = uh->uh_dport;
3944                         if (nr != NULL) {
3945                                 PF_ACPY(&s->lan.addr, &pd->baddr, af);
3946                                 s->lan.port = bport;
3947                         } else {
3948                                 PF_ACPY(&s->lan.addr, &s->gwy.addr, af);
3949                                 s->lan.port = s->gwy.port;
3950                         }
3951                 } else {
3952                         PF_ACPY(&s->lan.addr, daddr, af);
3953                         s->lan.port = uh->uh_dport;
3954                         PF_ACPY(&s->ext.addr, saddr, af);
3955                         s->ext.port = uh->uh_sport;
3956                         if (nr != NULL) {
3957                                 PF_ACPY(&s->gwy.addr, &pd->baddr, af);
3958                                 s->gwy.port = bport;
3959                         } else {
3960                                 PF_ACPY(&s->gwy.addr, &s->lan.addr, af);
3961                                 s->gwy.port = s->lan.port;
3962                         }
3963                 }
3964                 s->src.state = PFUDPS_SINGLE;
3965                 s->dst.state = PFUDPS_NO_TRAFFIC;
3966                 s->creation = time_second;
3967                 s->expire = time_second;
3968                 s->timeout = PFTM_UDP_FIRST_PACKET;
3969                 pf_set_rt_ifp(s, saddr);
3970                 if (sn != NULL) {
3971                         s->src_node = sn;
3972                         s->src_node->states++;
3973                 }
3974                 if (nsn != NULL) {
3975                         PF_ACPY(&nsn->raddr, &pd->naddr, af);
3976                         s->nat_src_node = nsn;
3977                         s->nat_src_node->states++;
3978                 }
3979                 if (pf_insert_state(BOUND_IFACE(r, kif), s)) {
3980                         REASON_SET(&reason, PFRES_STATEINS);
3981                         pf_src_tree_remove_state(s);
3982                         STATE_DEC_COUNTERS(s);
3983                         pool_put(&pf_state_pl, s);
3984                         return (PF_DROP);
3985                 } else
3986                         *sm = s;
3987                 if (tag > 0) {
3988                         pf_tag_ref(tag);
3989                         s->tag = tag;
3990                 }
3991         }
3992
3993         /* copy back packet headers if we performed NAT operations */
3994         if (rewrite)
3995                 m_copyback(m, off, sizeof(*uh), (caddr_t)uh);
3996
3997         return (PF_PASS);
3998 }
3999
4000 int
4001 pf_test_icmp(struct pf_rule **rm, struct pf_state **sm, int direction,
4002     struct pfi_kif *kif, struct mbuf *m, int off, void *h,
4003     struct pf_pdesc *pd, struct pf_rule **am, struct pf_ruleset **rsm,
4004     struct ifqueue *ifq)
4005 {
4006         struct pf_rule          *nr = NULL;
4007         struct pf_addr          *saddr = pd->src, *daddr = pd->dst;
4008         struct pf_rule          *r, *a = NULL;
4009         struct pf_ruleset       *ruleset = NULL;
4010         struct pf_src_node      *nsn = NULL;
4011         u_short                  reason;
4012         u_int16_t                icmpid = 0, bport, nport = 0;
4013         sa_family_t              af = pd->af;
4014         u_int8_t                 icmptype = 0;  /* make the compiler happy */
4015         u_int8_t                 icmpcode = 0;  /* make the compiler happy */
4016         int                      state_icmp = 0;
4017         int                      tag = -1, rtableid = -1;
4018 #ifdef INET6
4019         int                      rewrite = 0;
4020 #endif /* INET6 */
4021         int                      asd = 0;
4022         int                      match = 0;
4023
4024         if (pf_check_congestion(ifq)) {
4025                 REASON_SET(&reason, PFRES_CONGEST);
4026                 return (PF_DROP);
4027         }
4028
4029         switch (pd->proto) {
4030 #ifdef INET
4031         case IPPROTO_ICMP:
4032                 icmptype = pd->hdr.icmp->icmp_type;
4033                 icmpcode = pd->hdr.icmp->icmp_code;
4034                 icmpid = pd->hdr.icmp->icmp_id;
4035
4036                 if (icmptype == ICMP_UNREACH ||
4037                     icmptype == ICMP_SOURCEQUENCH ||
4038                     icmptype == ICMP_REDIRECT ||
4039                     icmptype == ICMP_TIMXCEED ||
4040                     icmptype == ICMP_PARAMPROB)
4041                         state_icmp++;
4042                 break;
4043 #endif /* INET */
4044 #ifdef INET6
4045         case IPPROTO_ICMPV6:
4046                 icmptype = pd->hdr.icmp6->icmp6_type;
4047                 icmpcode = pd->hdr.icmp6->icmp6_code;
4048                 icmpid = pd->hdr.icmp6->icmp6_id;
4049
4050                 if (icmptype == ICMP6_DST_UNREACH ||
4051                     icmptype == ICMP6_PACKET_TOO_BIG ||
4052                     icmptype == ICMP6_TIME_EXCEEDED ||
4053                     icmptype == ICMP6_PARAM_PROB)
4054                         state_icmp++;
4055                 break;
4056 #endif /* INET6 */
4057         }
4058
4059         r = TAILQ_FIRST(pf_main_ruleset.rules[PF_RULESET_FILTER].active.ptr);
4060
4061         if (direction == PF_OUT) {
4062                 bport = nport = icmpid;
4063                 /* check outgoing packet for BINAT/NAT */
4064                 if ((nr = pf_get_translation(pd, m, off, PF_OUT, kif, &nsn,
4065                     saddr, icmpid, daddr, icmpid, &pd->naddr, &nport)) !=
4066                     NULL) {
4067                         PF_ACPY(&pd->baddr, saddr, af);
4068                         switch (af) {
4069 #ifdef INET
4070                         case AF_INET:
4071                                 pf_change_a(&saddr->v4.s_addr, pd->ip_sum,
4072                                     pd->naddr.v4.s_addr, 0);
4073                                 pd->hdr.icmp->icmp_cksum = pf_cksum_fixup(
4074                                     pd->hdr.icmp->icmp_cksum, icmpid, nport, 0);
4075                                 pd->hdr.icmp->icmp_id = nport;
4076                                 m_copyback(m, off, ICMP_MINLEN,
4077                                     (caddr_t)pd->hdr.icmp);
4078                                 break;
4079 #endif /* INET */
4080 #ifdef INET6
4081                         case AF_INET6:
4082                                 pf_change_a6(saddr, &pd->hdr.icmp6->icmp6_cksum,
4083                                     &pd->naddr, 0);
4084                                 rewrite++;
4085                                 break;
4086 #endif /* INET6 */
4087                         }
4088                         if (nr->natpass)
4089                                 r = NULL;
4090                         pd->nat_rule = nr;
4091                 }
4092         } else {
4093                 bport = nport = icmpid;
4094                 /* check incoming packet for BINAT/RDR */
4095                 if ((nr = pf_get_translation(pd, m, off, PF_IN, kif, &nsn,
4096                     saddr, icmpid, daddr, icmpid, &pd->naddr, &nport)) !=
4097                     NULL) {
4098                         PF_ACPY(&pd->baddr, daddr, af);
4099                         switch (af) {
4100 #ifdef INET
4101                         case AF_INET:
4102                                 pf_change_a(&daddr->v4.s_addr,
4103                                     pd->ip_sum, pd->naddr.v4.s_addr, 0);
4104                                 break;
4105 #endif /* INET */
4106 #ifdef INET6
4107                         case AF_INET6:
4108                                 pf_change_a6(daddr, &pd->hdr.icmp6->icmp6_cksum,
4109                                     &pd->naddr, 0);
4110                                 rewrite++;
4111                                 break;
4112 #endif /* INET6 */
4113                         }
4114                         if (nr->natpass)
4115                                 r = NULL;
4116                         pd->nat_rule = nr;
4117                 }
4118         }
4119
4120         while (r != NULL) {
4121                 r->evaluations++;
4122                 if (pfi_kif_match(r->kif, kif) == r->ifnot)
4123                         r = r->skip[PF_SKIP_IFP].ptr;
4124                 else if (r->direction && r->direction != direction)
4125                         r = r->skip[PF_SKIP_DIR].ptr;
4126                 else if (r->af && r->af != af)
4127                         r = r->skip[PF_SKIP_AF].ptr;
4128                 else if (r->proto && r->proto != pd->proto)
4129                         r = r->skip[PF_SKIP_PROTO].ptr;
4130                 else if (PF_MISMATCHAW(&r->src.addr, saddr, af,
4131                     r->src.neg, kif))
4132                         r = r->skip[PF_SKIP_SRC_ADDR].ptr;
4133                 else if (PF_MISMATCHAW(&r->dst.addr, daddr, af,
4134                     r->dst.neg, NULL))
4135                         r = r->skip[PF_SKIP_DST_ADDR].ptr;
4136                 else if (r->type && r->type != icmptype + 1)
4137                         r = TAILQ_NEXT(r, entries);
4138                 else if (r->code && r->code != icmpcode + 1)
4139                         r = TAILQ_NEXT(r, entries);
4140                 else if (r->tos && !(r->tos == pd->tos))
4141                         r = TAILQ_NEXT(r, entries);
4142                 else if (r->rule_flag & PFRULE_FRAGMENT)
4143                         r = TAILQ_NEXT(r, entries);
4144                 else if (r->prob && r->prob <= arc4random())
4145                         r = TAILQ_NEXT(r, entries);
4146                 else if (r->match_tag && !pf_match_tag(m, r, pd->pf_mtag, &tag))
4147                         r = TAILQ_NEXT(r, entries);
4148                 else if (r->os_fingerprint != PF_OSFP_ANY)
4149                         r = TAILQ_NEXT(r, entries);
4150                 else {
4151                         if (r->tag)
4152                                 tag = r->tag;
4153                         if (r->rtableid >= 0)
4154                                 rtableid = r->rtableid;
4155                         if (r->anchor == NULL) {
4156                                 match = 1;
4157                                 *rm = r;
4158                                 *am = a;
4159                                 *rsm = ruleset;
4160                                 if ((*rm)->quick)
4161                                         break;
4162                                 r = TAILQ_NEXT(r, entries);
4163                         } else
4164                                 pf_step_into_anchor(&asd, &ruleset,
4165                                     PF_RULESET_FILTER, &r, &a, &match);
4166                 }
4167                 if (r == NULL && pf_step_out_of_anchor(&asd, &ruleset,
4168                     PF_RULESET_FILTER, &r, &a, &match))
4169                         break;
4170         }
4171         r = *rm;
4172         a = *am;
4173         ruleset = *rsm;
4174
4175         REASON_SET(&reason, PFRES_MATCH);
4176
4177         if (r->log || (nr != NULL && nr->natpass && nr->log)) {
4178 #ifdef INET6
4179                 if (rewrite)
4180                         m_copyback(m, off, sizeof(struct icmp6_hdr),
4181                             (caddr_t)pd->hdr.icmp6);
4182 #endif /* INET6 */
4183                 PFLOG_PACKET(kif, h, m, af, direction, reason, r->log ? r : nr,
4184                     a, ruleset, pd);
4185         }
4186
4187         if (r->action != PF_PASS)
4188                 return (PF_DROP);
4189
4190         if (pf_tag_packet(m, pd->pf_mtag, tag, rtableid)) {
4191                 REASON_SET(&reason, PFRES_MEMORY);
4192                 return (PF_DROP);
4193         }
4194
4195         if (!state_icmp && (r->keep_state || nr != NULL)) {
4196                 /* create new state */
4197                 struct pf_state *s = NULL;
4198                 struct pf_src_node *sn = NULL;
4199
4200                 /* check maximums */
4201                 if (r->max_states && (r->states >= r->max_states)) {
4202                         pf_status.lcounters[LCNT_STATES]++;
4203                         REASON_SET(&reason, PFRES_MAXSTATES);
4204                         goto cleanup;
4205                 }
4206                 /* src node for filter rule */
4207                 if ((r->rule_flag & PFRULE_SRCTRACK ||
4208                     r->rpool.opts & PF_POOL_STICKYADDR) &&
4209                     pf_insert_src_node(&sn, r, saddr, af) != 0) {
4210                         REASON_SET(&reason, PFRES_SRCLIMIT);
4211                         goto cleanup;
4212                 }
4213                 /* src node for translation rule */
4214                 if (nr != NULL && (nr->rpool.opts & PF_POOL_STICKYADDR) &&
4215                     ((direction == PF_OUT &&
4216                     pf_insert_src_node(&nsn, nr, &pd->baddr, af) != 0) ||
4217                     (pf_insert_src_node(&nsn, nr, saddr, af) != 0))) {
4218                         REASON_SET(&reason, PFRES_SRCLIMIT);
4219                         goto cleanup;
4220                 }
4221                 s = pool_get(&pf_state_pl, PR_NOWAIT);
4222                 if (s == NULL) {
4223                         REASON_SET(&reason, PFRES_MEMORY);
4224 cleanup:
4225                         if (sn != NULL && sn->states == 0 && sn->expire == 0) {
4226                                 RB_REMOVE(pf_src_tree, &tree_src_tracking, sn);
4227                                 pf_status.scounters[SCNT_SRC_NODE_REMOVALS]++;
4228                                 pf_status.src_nodes--;
4229                                 pool_put(&pf_src_tree_pl, sn);
4230                         }
4231                         if (nsn != sn && nsn != NULL && nsn->states == 0 &&
4232                             nsn->expire == 0) {
4233                                 RB_REMOVE(pf_src_tree, &tree_src_tracking, nsn);
4234                                 pf_status.scounters[SCNT_SRC_NODE_REMOVALS]++;
4235                                 pf_status.src_nodes--;
4236                                 pool_put(&pf_src_tree_pl, nsn);
4237                         }
4238                         return (PF_DROP);
4239                 }
4240                 bzero(s, sizeof(*s));
4241                 s->rule.ptr = r;
4242                 s->nat_rule.ptr = nr;
4243                 s->anchor.ptr = a;
4244                 STATE_INC_COUNTERS(s);
4245                 if (r->allow_opts)
4246                         s->state_flags |= PFSTATE_ALLOWOPTS;
4247                 if (r->rule_flag & PFRULE_STATESLOPPY)
4248                         s->state_flags |= PFSTATE_SLOPPY;
4249                 s->log = r->log & PF_LOG_ALL;
4250                 if (nr != NULL)
4251                         s->log |= nr->log & PF_LOG_ALL;
4252                 s->proto = pd->proto;
4253                 s->direction = direction;
4254                 s->af = af;
4255                 if (direction == PF_OUT) {
4256                         PF_ACPY(&s->gwy.addr, saddr, af);
4257                         s->gwy.port = nport;
4258                         PF_ACPY(&s->ext.addr, daddr, af);
4259                         s->ext.port = 0;
4260                         if (nr != NULL) {
4261                                 PF_ACPY(&s->lan.addr, &pd->baddr, af);
4262                                 s->lan.port = bport;
4263                         } else {
4264                                 PF_ACPY(&s->lan.addr, &s->gwy.addr, af);
4265                                 s->lan.port = s->gwy.port;
4266                         }
4267                 } else {
4268                         PF_ACPY(&s->lan.addr, daddr, af);
4269                         s->lan.port = nport;
4270                         PF_ACPY(&s->ext.addr, saddr, af);
4271                         s->ext.port = 0; 
4272                         if (nr != NULL) {
4273                                 PF_ACPY(&s->gwy.addr, &pd->baddr, af);
4274                                 s->gwy.port = bport;
4275                         } else {
4276                                 PF_ACPY(&s->gwy.addr, &s->lan.addr, af);
4277                                 s->gwy.port = s->lan.port;
4278                         }
4279                 }
4280                 s->creation = time_second;
4281                 s->expire = time_second;
4282                 s->timeout = PFTM_ICMP_FIRST_PACKET;
4283                 pf_set_rt_ifp(s, saddr);
4284                 if (sn != NULL) {
4285                         s->src_node = sn;
4286                         s->src_node->states++;
4287                 }
4288                 if (nsn != NULL) {
4289                         PF_ACPY(&nsn->raddr, &pd->naddr, af);
4290                         s->nat_src_node = nsn;
4291                         s->nat_src_node->states++;
4292                 }
4293                 if (pf_insert_state(BOUND_IFACE(r, kif), s)) {
4294                         REASON_SET(&reason, PFRES_STATEINS);
4295                         pf_src_tree_remove_state(s);
4296                         STATE_DEC_COUNTERS(s);
4297                         pool_put(&pf_state_pl, s);
4298                         return (PF_DROP);
4299                 } else
4300                         *sm = s;
4301                 if (tag > 0) {
4302                         pf_tag_ref(tag);
4303                         s->tag = tag;
4304                 }
4305         }
4306
4307 #ifdef INET6
4308         /* copy back packet headers if we performed IPv6 NAT operations */
4309         if (rewrite)
4310                 m_copyback(m, off, sizeof(struct icmp6_hdr),
4311                     (caddr_t)pd->hdr.icmp6);
4312 #endif /* INET6 */
4313
4314         return (PF_PASS);
4315 }
4316
4317 int
4318 pf_test_other(struct pf_rule **rm, struct pf_state **sm, int direction,
4319     struct pfi_kif *kif, struct mbuf *m, int off, void *h, struct pf_pdesc *pd,
4320     struct pf_rule **am, struct pf_ruleset **rsm, struct ifqueue *ifq)
4321 {
4322         struct pf_rule          *nr = NULL;
4323         struct pf_rule          *r, *a = NULL;
4324         struct pf_ruleset       *ruleset = NULL;
4325         struct pf_src_node      *nsn = NULL;
4326         struct pf_addr          *saddr = pd->src, *daddr = pd->dst;
4327         sa_family_t              af = pd->af;
4328         u_short                  reason;
4329         int                      tag = -1, rtableid = -1;
4330         int                      asd = 0;
4331         int                      match = 0;
4332
4333         if (pf_check_congestion(ifq)) {
4334                 REASON_SET(&reason, PFRES_CONGEST);
4335                 return (PF_DROP);
4336         }
4337
4338         r = TAILQ_FIRST(pf_main_ruleset.rules[PF_RULESET_FILTER].active.ptr);
4339
4340         if (direction == PF_OUT) {
4341                 /* check outgoing packet for BINAT/NAT */
4342                 if ((nr = pf_get_translation(pd, m, off, PF_OUT, kif, &nsn,
4343                     saddr, 0, daddr, 0, &pd->naddr, NULL)) != NULL) {
4344                         PF_ACPY(&pd->baddr, saddr, af);
4345                         switch (af) {
4346 #ifdef INET
4347                         case AF_INET:
4348                                 pf_change_a(&saddr->v4.s_addr, pd->ip_sum,
4349                                     pd->naddr.v4.s_addr, 0);
4350                                 break;
4351 #endif /* INET */
4352 #ifdef INET6
4353                         case AF_INET6:
4354                                 PF_ACPY(saddr, &pd->naddr, af);
4355                                 break;
4356 #endif /* INET6 */
4357                         }
4358                         if (nr->natpass)
4359                                 r = NULL;
4360                         pd->nat_rule = nr;
4361                 }
4362         } else {
4363                 /* check incoming packet for BINAT/RDR */
4364                 if ((nr = pf_get_translation(pd, m, off, PF_IN, kif, &nsn,
4365                     saddr, 0, daddr, 0, &pd->naddr, NULL)) != NULL) {
4366                         PF_ACPY(&pd->baddr, daddr, af);
4367                         switch (af) {
4368 #ifdef INET
4369                         case AF_INET:
4370                                 pf_change_a(&daddr->v4.s_addr,
4371                                     pd->ip_sum, pd->naddr.v4.s_addr, 0);
4372                                 break;
4373 #endif /* INET */
4374 #ifdef INET6
4375                         case AF_INET6:
4376                                 PF_ACPY(daddr, &pd->naddr, af);
4377                                 break;
4378 #endif /* INET6 */
4379                         }
4380                         if (nr->natpass)
4381                                 r = NULL;
4382                         pd->nat_rule = nr;
4383                 }
4384         }
4385
4386         while (r != NULL) {
4387                 r->evaluations++;
4388                 if (pfi_kif_match(r->kif, kif) == r->ifnot)
4389                         r = r->skip[PF_SKIP_IFP].ptr;
4390                 else if (r->direction && r->direction != direction)
4391                         r = r->skip[PF_SKIP_DIR].ptr;
4392                 else if (r->af && r->af != af)
4393                         r = r->skip[PF_SKIP_AF].ptr;
4394                 else if (r->proto && r->proto != pd->proto)
4395                         r = r->skip[PF_SKIP_PROTO].ptr;
4396                 else if (PF_MISMATCHAW(&r->src.addr, pd->src, af,
4397                     r->src.neg, kif))
4398                         r = r->skip[PF_SKIP_SRC_ADDR].ptr;
4399                 else if (PF_MISMATCHAW(&r->dst.addr, pd->dst, af,
4400                     r->dst.neg, NULL))
4401                         r = r->skip[PF_SKIP_DST_ADDR].ptr;
4402                 else if (r->tos && !(r->tos == pd->tos))
4403                         r = TAILQ_NEXT(r, entries);
4404                 else if (r->rule_flag & PFRULE_FRAGMENT)
4405                         r = TAILQ_NEXT(r, entries);
4406                 else if (r->prob && r->prob <= arc4random())
4407                         r = TAILQ_NEXT(r, entries);
4408                 else if (r->match_tag && !pf_match_tag(m, r, pd->pf_mtag, &tag))
4409                         r = TAILQ_NEXT(r, entries);
4410                 else if (r->os_fingerprint != PF_OSFP_ANY)
4411                         r = TAILQ_NEXT(r, entries);
4412                 else {
4413                         if (r->tag)
4414                                 tag = r->tag;
4415                         if (r->rtableid >= 0)
4416                                 rtableid = r->rtableid;
4417                         if (r->anchor == NULL) {
4418                                 match = 1;
4419                                 *rm = r;
4420                                 *am = a;
4421                                 *rsm = ruleset;
4422                                 if ((*rm)->quick)
4423                                         break;
4424                                 r = TAILQ_NEXT(r, entries);
4425                         } else
4426                                 pf_step_into_anchor(&asd, &ruleset,
4427                                     PF_RULESET_FILTER, &r, &a, &match);
4428                 }
4429                 if (r == NULL && pf_step_out_of_anchor(&asd, &ruleset,
4430                     PF_RULESET_FILTER, &r, &a, &match))
4431                         break;
4432         }
4433         r = *rm;
4434         a = *am;
4435         ruleset = *rsm;
4436
4437         REASON_SET(&reason, PFRES_MATCH);
4438
4439         if (r->log || (nr != NULL && nr->natpass && nr->log))
4440                 PFLOG_PACKET(kif, h, m, af, direction, reason, r->log ? r : nr,
4441                     a, ruleset, pd);
4442
4443         if ((r->action == PF_DROP) &&
4444             ((r->rule_flag & PFRULE_RETURNICMP) ||
4445             (r->rule_flag & PFRULE_RETURN))) {
4446                 struct pf_addr *a = NULL;
4447
4448                 if (nr != NULL) {
4449                         if (direction == PF_OUT)
4450                                 a = saddr;
4451                         else
4452                                 a = daddr;
4453                 }
4454                 if (a != NULL) {
4455                         switch (af) {
4456 #ifdef INET
4457                         case AF_INET:
4458                                 pf_change_a(&a->v4.s_addr, pd->ip_sum,
4459                                     pd->baddr.v4.s_addr, 0);
4460                                 break;
4461 #endif /* INET */
4462 #ifdef INET6
4463                         case AF_INET6:
4464                                 PF_ACPY(a, &pd->baddr, af);
4465                                 break;
4466 #endif /* INET6 */
4467                         }
4468                 }
4469                 if ((af == AF_INET) && r->return_icmp)
4470                         pf_send_icmp(m, r->return_icmp >> 8,
4471                             r->return_icmp & 255, af, r);
4472                 else if ((af == AF_INET6) && r->return_icmp6)
4473                         pf_send_icmp(m, r->return_icmp6 >> 8,
4474                             r->return_icmp6 & 255, af, r);
4475         }
4476
4477         if (r->action != PF_PASS)
4478                 return (PF_DROP);
4479
4480         if (pf_tag_packet(m, pd->pf_mtag, tag, rtableid)) {
4481                 REASON_SET(&reason, PFRES_MEMORY);
4482                 return (PF_DROP);
4483         }
4484
4485         if (r->keep_state || nr != NULL) {
4486                 /* create new state */
4487                 struct pf_state *s = NULL;
4488                 struct pf_src_node *sn = NULL;
4489
4490                 /* check maximums */
4491                 if (r->max_states && (r->states >= r->max_states)) {
4492                         pf_status.lcounters[LCNT_STATES]++;
4493                         REASON_SET(&reason, PFRES_MAXSTATES);
4494                         goto cleanup;
4495                 }
4496                 /* src node for filter rule */
4497                 if ((r->rule_flag & PFRULE_SRCTRACK ||
4498                     r->rpool.opts & PF_POOL_STICKYADDR) &&
4499                     pf_insert_src_node(&sn, r, saddr, af) != 0) {
4500                         REASON_SET(&reason, PFRES_SRCLIMIT);
4501                         goto cleanup;
4502                 }
4503                 /* src node for translation rule */
4504                 if (nr != NULL && (nr->rpool.opts & PF_POOL_STICKYADDR) &&
4505                     ((direction == PF_OUT &&
4506                     pf_insert_src_node(&nsn, nr, &pd->baddr, af) != 0) ||
4507                     (pf_insert_src_node(&nsn, nr, saddr, af) != 0))) {
4508                         REASON_SET(&reason, PFRES_SRCLIMIT);
4509                         goto cleanup;
4510                 }
4511                 s = pool_get(&pf_state_pl, PR_NOWAIT);
4512                 if (s == NULL) {
4513                         REASON_SET(&reason, PFRES_MEMORY);
4514 cleanup:
4515                         if (sn != NULL && sn->states == 0 && sn->expire == 0) {
4516                                 RB_REMOVE(pf_src_tree, &tree_src_tracking, sn);
4517                                 pf_status.scounters[SCNT_SRC_NODE_REMOVALS]++;
4518                                 pf_status.src_nodes--;
4519                                 pool_put(&pf_src_tree_pl, sn);
4520                         }
4521                         if (nsn != sn && nsn != NULL && nsn->states == 0 &&
4522                             nsn->expire == 0) {
4523                                 RB_REMOVE(pf_src_tree, &tree_src_tracking, nsn);
4524                                 pf_status.scounters[SCNT_SRC_NODE_REMOVALS]++;
4525                                 pf_status.src_nodes--;
4526                                 pool_put(&pf_src_tree_pl, nsn);
4527                         }
4528                         return (PF_DROP);
4529                 }
4530                 bzero(s, sizeof(*s));
4531                 s->rule.ptr = r;
4532                 s->nat_rule.ptr = nr;
4533                 s->anchor.ptr = a;
4534                 STATE_INC_COUNTERS(s);
4535                 if (r->allow_opts)
4536                         s->state_flags |= PFSTATE_ALLOWOPTS;
4537                 if (r->rule_flag & PFRULE_STATESLOPPY)
4538                         s->state_flags |= PFSTATE_SLOPPY;
4539                 s->log = r->log & PF_LOG_ALL;
4540                 if (nr != NULL)
4541                         s->log |= nr->log & PF_LOG_ALL;
4542                 s->proto = pd->proto;
4543                 s->direction = direction;
4544                 s->af = af;
4545                 if (direction == PF_OUT) {
4546                         PF_ACPY(&s->gwy.addr, saddr, af);
4547                         PF_ACPY(&s->ext.addr, daddr, af);
4548                         if (nr != NULL)
4549                                 PF_ACPY(&s->lan.addr, &pd->baddr, af);
4550                         else
4551                                 PF_ACPY(&s->lan.addr, &s->gwy.addr, af);
4552                 } else {
4553                         PF_ACPY(&s->lan.addr, daddr, af);
4554                         PF_ACPY(&s->ext.addr, saddr, af);
4555                         if (nr != NULL)
4556                                 PF_ACPY(&s->gwy.addr, &pd->baddr, af);
4557                         else
4558                                 PF_ACPY(&s->gwy.addr, &s->lan.addr, af);
4559                 }
4560                 s->src.state = PFOTHERS_SINGLE;
4561                 s->dst.state = PFOTHERS_NO_TRAFFIC;
4562                 s->creation = time_second;
4563                 s->expire = time_second;
4564                 s->timeout = PFTM_OTHER_FIRST_PACKET;
4565                 pf_set_rt_ifp(s, saddr);
4566                 if (sn != NULL) {
4567                         s->src_node = sn;
4568                         s->src_node->states++;
4569                 }
4570                 if (nsn != NULL) {
4571                         PF_ACPY(&nsn->raddr, &pd->naddr, af);
4572                         s->nat_src_node = nsn;
4573                         s->nat_src_node->states++;
4574                 }
4575                 if (pf_insert_state(BOUND_IFACE(r, kif), s)) {
4576                         REASON_SET(&reason, PFRES_STATEINS);
4577                         pf_src_tree_remove_state(s);
4578                         STATE_DEC_COUNTERS(s);
4579                         pool_put(&pf_state_pl, s);
4580                         return (PF_DROP);
4581                 } else
4582                         *sm = s;
4583                 if (tag > 0) {
4584                         pf_tag_ref(tag);
4585                         s->tag = tag;
4586                 }
4587         }
4588
4589         return (PF_PASS);
4590 }
4591
4592 int
4593 pf_test_fragment(struct pf_rule **rm, int direction, struct pfi_kif *kif,
4594     struct mbuf *m, void *h, struct pf_pdesc *pd, struct pf_rule **am,
4595     struct pf_ruleset **rsm)
4596 {
4597         struct pf_rule          *r, *a = NULL;
4598         struct pf_ruleset       *ruleset = NULL;
4599         sa_family_t              af = pd->af;
4600         u_short                  reason;
4601         int                      tag = -1;
4602         int                      asd = 0;
4603         int                      match = 0;
4604
4605         r = TAILQ_FIRST(pf_main_ruleset.rules[PF_RULESET_FILTER].active.ptr);
4606         while (r != NULL) {
4607                 r->evaluations++;
4608                 if (pfi_kif_match(r->kif, kif) == r->ifnot)
4609                         r = r->skip[PF_SKIP_IFP].ptr;
4610                 else if (r->direction && r->direction != direction)
4611                         r = r->skip[PF_SKIP_DIR].ptr;
4612                 else if (r->af && r->af != af)
4613                         r = r->skip[PF_SKIP_AF].ptr;
4614                 else if (r->proto && r->proto != pd->proto)
4615                         r = r->skip[PF_SKIP_PROTO].ptr;
4616                 else if (PF_MISMATCHAW(&r->src.addr, pd->src, af,
4617                     r->src.neg, kif))
4618                         r = r->skip[PF_SKIP_SRC_ADDR].ptr;
4619                 else if (PF_MISMATCHAW(&r->dst.addr, pd->dst, af,
4620                     r->dst.neg, NULL))
4621                         r = r->skip[PF_SKIP_DST_ADDR].ptr;
4622                 else if (r->tos && !(r->tos == pd->tos))
4623                         r = TAILQ_NEXT(r, entries);
4624                 else if (r->os_fingerprint != PF_OSFP_ANY)
4625                         r = TAILQ_NEXT(r, entries);
4626                 else if (pd->proto == IPPROTO_UDP &&
4627                     (r->src.port_op || r->dst.port_op))
4628                         r = TAILQ_NEXT(r, entries);
4629                 else if (pd->proto == IPPROTO_TCP &&
4630                     (r->src.port_op || r->dst.port_op || r->flagset))
4631                         r = TAILQ_NEXT(r, entries);
4632                 else if ((pd->proto == IPPROTO_ICMP ||
4633                     pd->proto == IPPROTO_ICMPV6) &&
4634                     (r->type || r->code))
4635                         r = TAILQ_NEXT(r, entries);
4636                 else if (r->prob && r->prob <= arc4random())
4637                         r = TAILQ_NEXT(r, entries);
4638                 else if (r->match_tag && !pf_match_tag(m, r, pd->pf_mtag, &tag))
4639                         r = TAILQ_NEXT(r, entries);
4640                 else {
4641                         if (r->anchor == NULL) {
4642                                 match = 1;
4643                                 *rm = r;
4644                                 *am = a;
4645                                 *rsm = ruleset;
4646                                 if ((*rm)->quick)
4647                                         break;
4648                                 r = TAILQ_NEXT(r, entries);
4649                         } else
4650                                 pf_step_into_anchor(&asd, &ruleset,
4651                                     PF_RULESET_FILTER, &r, &a, &match);
4652                 }
4653                 if (r == NULL && pf_step_out_of_anchor(&asd, &ruleset,
4654                     PF_RULESET_FILTER, &r, &a, &match))
4655                         break;
4656         }
4657         r = *rm;
4658         a = *am;
4659         ruleset = *rsm;
4660
4661         REASON_SET(&reason, PFRES_MATCH);
4662
4663         if (r->log)
4664                 PFLOG_PACKET(kif, h, m, af, direction, reason, r, a, ruleset,
4665                     pd);
4666
4667         if (r->action != PF_PASS)
4668                 return (PF_DROP);
4669
4670         if (pf_tag_packet(m, pd->pf_mtag, tag, -1)) {
4671                 REASON_SET(&reason, PFRES_MEMORY);
4672                 return (PF_DROP);
4673         }
4674
4675         return (PF_PASS);
4676 }
4677
4678 int
4679 pf_tcp_track_full(struct pf_state_peer *src, struct pf_state_peer *dst,
4680         struct pf_state **state, struct pfi_kif *kif, struct mbuf *m, int off,
4681         struct pf_pdesc *pd, u_short *reason, int *copyback)
4682 {
4683         struct tcphdr           *th = pd->hdr.tcp;
4684         u_int16_t                win = ntohs(th->th_win);
4685         u_int32_t                ack, end, seq, orig_seq;
4686         u_int8_t                 sws, dws;
4687         int                      ackskew;
4688
4689         if (src->wscale && dst->wscale && !(th->th_flags & TH_SYN)) {
4690                 sws = src->wscale & PF_WSCALE_MASK;
4691                 dws = dst->wscale & PF_WSCALE_MASK;
4692         } else
4693                 sws = dws = 0;
4694
4695         /*
4696          * Sequence tracking algorithm from Guido van Rooij's paper:
4697          *   http://www.madison-gurkha.com/publications/tcp_filtering/
4698          *      tcp_filtering.ps
4699          */
4700
4701         orig_seq = seq = ntohl(th->th_seq);
4702         if (src->seqlo == 0) {
4703                 /* First packet from this end. Set its state */
4704
4705                 if ((pd->flags & PFDESC_TCP_NORM || dst->scrub) &&
4706                     src->scrub == NULL) {
4707                         if (pf_normalize_tcp_init(m, off, pd, th, src, dst)) {
4708                                 REASON_SET(reason, PFRES_MEMORY);
4709                                 return (PF_DROP);
4710                         }
4711                 }
4712
4713                 /* Deferred generation of sequence number modulator */
4714                 if (dst->seqdiff && !src->seqdiff) {
4715 #ifdef __FreeBSD__
4716                         while ((src->seqdiff = pf_new_isn(*state) - seq) == 0)
4717                                 ;
4718 #else
4719                         while ((src->seqdiff = tcp_rndiss_next() - seq) == 0)
4720                                 ;
4721 #endif
4722                         ack = ntohl(th->th_ack) - dst->seqdiff;
4723                         pf_change_a(&th->th_seq, &th->th_sum, htonl(seq +
4724                             src->seqdiff), 0);
4725                         pf_change_a(&th->th_ack, &th->th_sum, htonl(ack), 0);
4726                         *copyback = 1;
4727                 } else {
4728                         ack = ntohl(th->th_ack);
4729                 }
4730
4731                 end = seq + pd->p_len;
4732                 if (th->th_flags & TH_SYN) {
4733                         end++;
4734                         if (dst->wscale & PF_WSCALE_FLAG) {
4735                                 src->wscale = pf_get_wscale(m, off, th->th_off,
4736                                     pd->af);
4737                                 if (src->wscale & PF_WSCALE_FLAG) {
4738                                         /* Remove scale factor from initial
4739                                          * window */
4740                                         sws = src->wscale & PF_WSCALE_MASK;
4741                                         win = ((u_int32_t)win + (1 << sws) - 1)
4742                                             >> sws;
4743                                         dws = dst->wscale & PF_WSCALE_MASK;
4744                                 } else {
4745                                         /* fixup other window */
4746                                         dst->max_win <<= dst->wscale &
4747                                             PF_WSCALE_MASK;
4748                                         /* in case of a retrans SYN|ACK */
4749                                         dst->wscale = 0;
4750                                 }
4751                         }
4752                 }
4753                 if (th->th_flags & TH_FIN)
4754                         end++;
4755
4756                 src->seqlo = seq;
4757                 if (src->state < TCPS_SYN_SENT)
4758                         src->state = TCPS_SYN_SENT;
4759
4760                 /*
4761                  * May need to slide the window (seqhi may have been set by
4762                  * the crappy stack check or if we picked up the connection
4763                  * after establishment)
4764                  */
4765                 if (src->seqhi == 1 ||
4766                     SEQ_GEQ(end + MAX(1, dst->max_win << dws), src->seqhi))
4767                         src->seqhi = end + MAX(1, dst->max_win << dws);
4768                 if (win > src->max_win)
4769                         src->max_win = win;
4770
4771         } else {
4772                 ack = ntohl(th->th_ack) - dst->seqdiff;
4773                 if (src->seqdiff) {
4774                         /* Modulate sequence numbers */
4775                         pf_change_a(&th->th_seq, &th->th_sum, htonl(seq +
4776                             src->seqdiff), 0);
4777                         pf_change_a(&th->th_ack, &th->th_sum, htonl(ack), 0);
4778                         *copyback = 1;
4779                 }
4780                 end = seq + pd->p_len;
4781                 if (th->th_flags & TH_SYN)
4782                         end++;
4783                 if (th->th_flags & TH_FIN)
4784                         end++;
4785         }
4786
4787         if ((th->th_flags & TH_ACK) == 0) {
4788                 /* Let it pass through the ack skew check */
4789                 ack = dst->seqlo;
4790         } else if ((ack == 0 &&
4791             (th->th_flags & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST)) ||
4792             /* broken tcp stacks do not set ack */
4793             (dst->state < TCPS_SYN_SENT)) {
4794                 /*
4795                  * Many stacks (ours included) will set the ACK number in an
4796                  * FIN|ACK if the SYN times out -- no sequence to ACK.
4797                  */
4798                 ack = dst->seqlo;
4799         }
4800
4801         if (seq == end) {
4802                 /* Ease sequencing restrictions on no data packets */
4803                 seq = src->seqlo;
4804                 end = seq;
4805         }
4806
4807         ackskew = dst->seqlo - ack;
4808
4809
4810         /*
4811          * Need to demodulate the sequence numbers in any TCP SACK options
4812          * (Selective ACK). We could optionally validate the SACK values
4813          * against the current ACK window, either forwards or backwards, but
4814          * I'm not confident that SACK has been implemented properly
4815          * everywhere. It wouldn't surprise me if several stacks accidently
4816          * SACK too far backwards of previously ACKed data. There really aren't
4817          * any security implications of bad SACKing unless the target stack
4818          * doesn't validate the option length correctly. Someone trying to
4819          * spoof into a TCP connection won't bother blindly sending SACK
4820          * options anyway.
4821          */
4822         if (dst->seqdiff && (th->th_off << 2) > sizeof(struct tcphdr)) {
4823                 if (pf_modulate_sack(m, off, pd, th, dst))
4824                         *copyback = 1;
4825         }
4826
4827
4828 #define MAXACKWINDOW (0xffff + 1500)    /* 1500 is an arbitrary fudge factor */
4829         if (SEQ_GEQ(src->seqhi, end) &&
4830             /* Last octet inside other's window space */
4831             SEQ_GEQ(seq, src->seqlo - (dst->max_win << dws)) &&
4832             /* Retrans: not more than one window back */
4833             (ackskew >= -MAXACKWINDOW) &&
4834             /* Acking not more than one reassembled fragment backwards */
4835             (ackskew <= (MAXACKWINDOW << sws)) &&
4836             /* Acking not more than one window forward */
4837             ((th->th_flags & TH_RST) == 0 || orig_seq == src->seqlo ||
4838             (orig_seq == src->seqlo + 1) || (pd->flags & PFDESC_IP_REAS) == 0)) {
4839             /* Require an exact/+1 sequence match on resets when possible */
4840
4841                 if (dst->scrub || src->scrub) {
4842                         if (pf_normalize_tcp_stateful(m, off, pd, reason, th,
4843                             *state, src, dst, copyback))
4844                                 return (PF_DROP);
4845                 }
4846
4847                 /* update max window */
4848                 if (src->max_win < win)
4849                         src->max_win = win;
4850                 /* synchronize sequencing */
4851                 if (SEQ_GT(end, src->seqlo))
4852                         src->seqlo = end;
4853                 /* slide the window of what the other end can send */
4854                 if (SEQ_GEQ(ack + (win << sws), dst->seqhi))
4855                         dst->seqhi = ack + MAX((win << sws), 1);
4856
4857
4858                 /* update states */
4859                 if (th->th_flags & TH_SYN)
4860                         if (src->state < TCPS_SYN_SENT)
4861                                 src->state = TCPS_SYN_SENT;
4862                 if (th->th_flags & TH_FIN)
4863                         if (src->state < TCPS_CLOSING)
4864                                 src->state = TCPS_CLOSING;
4865                 if (th->th_flags & TH_ACK) {
4866                         if (dst->state == TCPS_SYN_SENT) {
4867                                 dst->state = TCPS_ESTABLISHED;
4868                                 if (src->state == TCPS_ESTABLISHED &&
4869                                     (*state)->src_node != NULL &&
4870                                     pf_src_connlimit(state)) {
4871                                         REASON_SET(reason, PFRES_SRCLIMIT);
4872                                         return (PF_DROP);
4873                                 }
4874                         } else if (dst->state == TCPS_CLOSING)
4875                                 dst->state = TCPS_FIN_WAIT_2;
4876                 }
4877                 if (th->th_flags & TH_RST)
4878                         src->state = dst->state = TCPS_TIME_WAIT;
4879
4880                 /* update expire time */
4881                 (*state)->expire = time_second;
4882                 if (src->state >= TCPS_FIN_WAIT_2 &&
4883                     dst->state >= TCPS_FIN_WAIT_2)
4884                         (*state)->timeout = PFTM_TCP_CLOSED;
4885                 else if (src->state >= TCPS_CLOSING &&
4886                     dst->state >= TCPS_CLOSING)
4887                         (*state)->timeout = PFTM_TCP_FIN_WAIT;
4888                 else if (src->state < TCPS_ESTABLISHED ||
4889                     dst->state < TCPS_ESTABLISHED)
4890                         (*state)->timeout = PFTM_TCP_OPENING;
4891                 else if (src->state >= TCPS_CLOSING ||
4892                     dst->state >= TCPS_CLOSING)
4893                         (*state)->timeout = PFTM_TCP_CLOSING;
4894                 else
4895                         (*state)->timeout = PFTM_TCP_ESTABLISHED;
4896
4897                 /* Fall through to PASS packet */
4898
4899         } else if ((dst->state < TCPS_SYN_SENT ||
4900                 dst->state >= TCPS_FIN_WAIT_2 ||
4901                 src->state >= TCPS_FIN_WAIT_2) &&
4902             SEQ_GEQ(src->seqhi + MAXACKWINDOW, end) &&
4903             /* Within a window forward of the originating packet */
4904             SEQ_GEQ(seq, src->seqlo - MAXACKWINDOW)) {
4905             /* Within a window backward of the originating packet */
4906
4907                 /*
4908                  * This currently handles three situations:
4909                  *  1) Stupid stacks will shotgun SYNs before their peer
4910                  *     replies.
4911                  *  2) When PF catches an already established stream (the
4912                  *     firewall rebooted, the state table was flushed, routes
4913                  *     changed...)
4914                  *  3) Packets get funky immediately after the connection
4915                  *     closes (this should catch Solaris spurious ACK|FINs
4916                  *     that web servers like to spew after a close)
4917                  *
4918                  * This must be a little more careful than the above code
4919                  * since packet floods will also be caught here. We don't
4920                  * update the TTL here to mitigate the damage of a packet
4921                  * flood and so the same code can handle awkward establishment
4922                  * and a loosened connection close.
4923                  * In the establishment case, a correct peer response will
4924                  * validate the connection, go through the normal state code
4925                  * and keep updating the state TTL.
4926                  */
4927
4928                 if (pf_status.debug >= PF_DEBUG_MISC) {
4929                         printf("pf: loose state match: ");
4930                         pf_print_state(*state);
4931                         pf_print_flags(th->th_flags);
4932                         printf(" seq=%u (%u) ack=%u len=%u ackskew=%d "
4933                             "pkts=%llu:%llu\n", seq, orig_seq, ack, pd->p_len,
4934 #ifdef __FreeBSD__
4935                             ackskew, (unsigned long long)(*state)->packets[0],
4936                             (unsigned long long)(*state)->packets[1]);
4937 #else
4938                             ackskew, (*state)->packets[0],
4939                             (*state)->packets[1]);
4940 #endif
4941                 }
4942
4943                 if (dst->scrub || src->scrub) {
4944                         if (pf_normalize_tcp_stateful(m, off, pd, reason, th,
4945                             *state, src, dst, copyback))
4946                                 return (PF_DROP);
4947                 }
4948
4949                 /* update max window */
4950                 if (src->max_win < win)
4951                         src->max_win = win;
4952                 /* synchronize sequencing */
4953                 if (SEQ_GT(end, src->seqlo))
4954                         src->seqlo = end;
4955                 /* slide the window of what the other end can send */
4956                 if (SEQ_GEQ(ack + (win << sws), dst->seqhi))
4957                         dst->seqhi = ack + MAX((win << sws), 1);
4958
4959                 /*
4960                  * Cannot set dst->seqhi here since this could be a shotgunned
4961                  * SYN and not an already established connection.
4962                  */
4963
4964                 if (th->th_flags & TH_FIN)
4965                         if (src->state < TCPS_CLOSING)
4966                                 src->state = TCPS_CLOSING;
4967                 if (th->th_flags & TH_RST)
4968                         src->state = dst->state = TCPS_TIME_WAIT;
4969
4970                 /* Fall through to PASS packet */
4971
4972         } else {
4973                 if ((*state)->dst.state == TCPS_SYN_SENT &&
4974                     (*state)->src.state == TCPS_SYN_SENT) {
4975                         /* Send RST for state mismatches during handshake */
4976                         if (!(th->th_flags & TH_RST))
4977 #ifdef __FreeBSD__
4978                                 pf_send_tcp(m, (*state)->rule.ptr, pd->af,
4979 #else
4980                                 pf_send_tcp((*state)->rule.ptr, pd->af,
4981 #endif
4982                                     pd->dst, pd->src, th->th_dport,
4983                                     th->th_sport, ntohl(th->th_ack), 0,
4984                                     TH_RST, 0, 0,
4985                                     (*state)->rule.ptr->return_ttl, 1, 0,
4986                                     pd->eh, kif->pfik_ifp);
4987                         src->seqlo = 0;
4988                         src->seqhi = 1;
4989                         src->max_win = 1;
4990                 } else if (pf_status.debug >= PF_DEBUG_MISC) {
4991                         printf("pf: BAD state: ");
4992                         pf_print_state(*state);
4993                         pf_print_flags(th->th_flags);
4994                         printf(" seq=%u (%u) ack=%u len=%u ackskew=%d "
4995 #ifdef notyet
4996                             "pkts=%llu:%llu dir=%s,%s\n",
4997 #else
4998                             "pkts=%llu:%llu%s\n",
4999 #endif
5000                             seq, orig_seq, ack, pd->p_len, ackskew,
5001 #ifdef __FreeBSD__
5002                             (unsigned long long)(*state)->packets[0],
5003                             (unsigned long long)(*state)->packets[1],
5004 #else
5005                             (*state)->packets[0], (*state)->packets[1],
5006 #endif
5007 #ifdef notyet
5008                             direction == PF_IN ? "in" : "out",
5009                             direction == (*state)->direction ? "fwd" : "rev");
5010 #else
5011                             "");
5012 #endif
5013                         printf("pf: State failure on: %c %c %c %c | %c %c\n",
5014                             SEQ_GEQ(src->seqhi, end) ? ' ' : '1',
5015                             SEQ_GEQ(seq, src->seqlo - (dst->max_win << dws)) ?
5016                             ' ': '2',
5017                             (ackskew >= -MAXACKWINDOW) ? ' ' : '3',
5018                             (ackskew <= (MAXACKWINDOW << sws)) ? ' ' : '4',
5019                             SEQ_GEQ(src->seqhi + MAXACKWINDOW, end) ?' ' :'5',
5020                             SEQ_GEQ(seq, src->seqlo - MAXACKWINDOW) ?' ' :'6');
5021                 }
5022                 REASON_SET(reason, PFRES_BADSTATE);
5023                 return (PF_DROP);
5024         }
5025
5026         /* Any packets which have gotten here are to be passed */
5027         return (PF_PASS);
5028 }
5029
5030 int
5031 pf_tcp_track_sloppy(struct pf_state_peer *src, struct pf_state_peer *dst,
5032         struct pf_state **state, struct pf_pdesc *pd, u_short *reason)
5033 {
5034         struct tcphdr           *th = pd->hdr.tcp;
5035
5036         if (th->th_flags & TH_SYN)
5037                 if (src->state < TCPS_SYN_SENT)
5038                         src->state = TCPS_SYN_SENT;
5039         if (th->th_flags & TH_FIN)
5040                 if (src->state < TCPS_CLOSING)
5041                         src->state = TCPS_CLOSING;
5042         if (th->th_flags & TH_ACK) {
5043                 if (dst->state == TCPS_SYN_SENT) {
5044                         dst->state = TCPS_ESTABLISHED;
5045                         if (src->state == TCPS_ESTABLISHED &&
5046                             (*state)->src_node != NULL &&
5047                             pf_src_connlimit(state)) {
5048                                 REASON_SET(reason, PFRES_SRCLIMIT);
5049                                 return (PF_DROP);
5050                         }
5051                 } else if (dst->state == TCPS_CLOSING) {
5052                         dst->state = TCPS_FIN_WAIT_2;
5053                 } else if (src->state == TCPS_SYN_SENT &&
5054                     dst->state < TCPS_SYN_SENT) {
5055                         /*
5056                          * Handle a special sloppy case where we only see one
5057                          * half of the connection. If there is a ACK after
5058                          * the initial SYN without ever seeing a packet from
5059                          * the destination, set the connection to established.
5060                          */
5061                         dst->state = src->state = TCPS_ESTABLISHED;
5062                         if ((*state)->src_node != NULL &&
5063                             pf_src_connlimit(state)) {
5064                                 REASON_SET(reason, PFRES_SRCLIMIT);
5065                                 return (PF_DROP);
5066                         }
5067                 } else if (src->state == TCPS_CLOSING &&
5068                     dst->state == TCPS_ESTABLISHED &&
5069                     dst->seqlo == 0) {
5070                         /*
5071                          * Handle the closing of half connections where we
5072                          * don't see the full bidirectional FIN/ACK+ACK
5073                          * handshake.
5074                          */
5075                         dst->state = TCPS_CLOSING;
5076                 }
5077         }
5078         if (th->th_flags & TH_RST)
5079                 src->state = dst->state = TCPS_TIME_WAIT;
5080
5081         /* update expire time */
5082         (*state)->expire = time_second;
5083         if (src->state >= TCPS_FIN_WAIT_2 &&
5084             dst->state >= TCPS_FIN_WAIT_2)
5085                 (*state)->timeout = PFTM_TCP_CLOSED;
5086         else if (src->state >= TCPS_CLOSING &&
5087             dst->state >= TCPS_CLOSING)
5088                 (*state)->timeout = PFTM_TCP_FIN_WAIT;
5089         else if (src->state < TCPS_ESTABLISHED ||
5090             dst->state < TCPS_ESTABLISHED)
5091                 (*state)->timeout = PFTM_TCP_OPENING;
5092         else if (src->state >= TCPS_CLOSING ||
5093             dst->state >= TCPS_CLOSING)
5094                 (*state)->timeout = PFTM_TCP_CLOSING;
5095         else
5096                 (*state)->timeout = PFTM_TCP_ESTABLISHED;
5097
5098         return (PF_PASS);
5099 }
5100
5101
5102 int
5103 pf_test_state_tcp(struct pf_state **state, int direction, struct pfi_kif *kif,
5104     struct mbuf *m, int off, void *h, struct pf_pdesc *pd,
5105     u_short *reason)
5106 {
5107         struct pf_state_cmp      key;
5108         struct tcphdr           *th = pd->hdr.tcp;
5109         int                      copyback = 0;
5110         struct pf_state_peer    *src, *dst;
5111
5112         key.af = pd->af;
5113         key.proto = IPPROTO_TCP;
5114         if (direction == PF_IN) {
5115                 PF_ACPY(&key.ext.addr, pd->src, key.af);
5116                 PF_ACPY(&key.gwy.addr, pd->dst, key.af);
5117                 key.ext.port = th->th_sport;
5118                 key.gwy.port = th->th_dport;
5119         } else {
5120                 PF_ACPY(&key.lan.addr, pd->src, key.af);
5121                 PF_ACPY(&key.ext.addr, pd->dst, key.af);
5122                 key.lan.port = th->th_sport;
5123                 key.ext.port = th->th_dport;
5124         }
5125
5126         STATE_LOOKUP();
5127
5128         if (direction == (*state)->direction) {
5129                 src = &(*state)->src;
5130                 dst = &(*state)->dst;
5131         } else {
5132                 src = &(*state)->dst;
5133                 dst = &(*state)->src;
5134         }
5135
5136         if ((*state)->src.state == PF_TCPS_PROXY_SRC) {
5137                 if (direction != (*state)->direction) {
5138                         REASON_SET(reason, PFRES_SYNPROXY);
5139                         return (PF_SYNPROXY_DROP);
5140                 }
5141                 if (th->th_flags & TH_SYN) {
5142                         if (ntohl(th->th_seq) != (*state)->src.seqlo) {
5143                                 REASON_SET(reason, PFRES_SYNPROXY);
5144                                 return (PF_DROP);
5145                         }
5146 #ifdef __FreeBSD__
5147                         pf_send_tcp(NULL, (*state)->rule.ptr, pd->af, pd->dst,
5148 #else
5149                         pf_send_tcp((*state)->rule.ptr, pd->af, pd->dst,
5150 #endif
5151                             pd->src, th->th_dport, th->th_sport,
5152                             (*state)->src.seqhi, ntohl(th->th_seq) + 1,
5153                             TH_SYN|TH_ACK, 0, (*state)->src.mss, 0, 1,
5154                             0, NULL, NULL);
5155                         REASON_SET(reason, PFRES_SYNPROXY);
5156                         return (PF_SYNPROXY_DROP);
5157                 } else if (!(th->th_flags & TH_ACK) ||
5158                     (ntohl(th->th_ack) != (*state)->src.seqhi + 1) ||
5159                     (ntohl(th->th_seq) != (*state)->src.seqlo + 1)) {
5160                         REASON_SET(reason, PFRES_SYNPROXY);
5161                         return (PF_DROP);
5162                 } else if ((*state)->src_node != NULL &&
5163                     pf_src_connlimit(state)) {
5164                         REASON_SET(reason, PFRES_SRCLIMIT);
5165                         return (PF_DROP);
5166                 } else
5167                         (*state)->src.state = PF_TCPS_PROXY_DST;
5168         }
5169         if ((*state)->src.state == PF_TCPS_PROXY_DST) {
5170                 struct pf_state_host *src, *dst;
5171
5172                 if (direction == PF_OUT) {
5173                         src = &(*state)->gwy;
5174                         dst = &(*state)->ext;
5175                 } else {
5176                         src = &(*state)->ext;
5177                         dst = &(*state)->lan;
5178                 }
5179                 if (direction == (*state)->direction) {
5180                         if (((th->th_flags & (TH_SYN|TH_ACK)) != TH_ACK) ||
5181                             (ntohl(th->th_ack) != (*state)->src.seqhi + 1) ||
5182                             (ntohl(th->th_seq) != (*state)->src.seqlo + 1)) {
5183                                 REASON_SET(reason, PFRES_SYNPROXY);
5184                                 return (PF_DROP);
5185                         }
5186                         (*state)->src.max_win = MAX(ntohs(th->th_win), 1);
5187                         if ((*state)->dst.seqhi == 1)
5188                                 (*state)->dst.seqhi = htonl(arc4random());
5189 #ifdef __FreeBSD__
5190                         pf_send_tcp(NULL, (*state)->rule.ptr, pd->af,
5191                             &src->addr,
5192 #else
5193                         pf_send_tcp((*state)->rule.ptr, pd->af, &src->addr,
5194 #endif
5195                             &dst->addr, src->port, dst->port,
5196                             (*state)->dst.seqhi, 0, TH_SYN, 0,
5197                             (*state)->src.mss, 0, 0, (*state)->tag, NULL, NULL);
5198                         REASON_SET(reason, PFRES_SYNPROXY);
5199                         return (PF_SYNPROXY_DROP);
5200                 } else if (((th->th_flags & (TH_SYN|TH_ACK)) !=
5201                     (TH_SYN|TH_ACK)) ||
5202                     (ntohl(th->th_ack) != (*state)->dst.seqhi + 1)) {
5203                         REASON_SET(reason, PFRES_SYNPROXY);
5204                         return (PF_DROP);
5205                 } else {
5206                         (*state)->dst.max_win = MAX(ntohs(th->th_win), 1);
5207                         (*state)->dst.seqlo = ntohl(th->th_seq);
5208 #ifdef __FreeBSD__
5209                         pf_send_tcp(NULL, (*state)->rule.ptr, pd->af, pd->dst,
5210 #else
5211                         pf_send_tcp((*state)->rule.ptr, pd->af, pd->dst,
5212 #endif
5213                             pd->src, th->th_dport, th->th_sport,
5214                             ntohl(th->th_ack), ntohl(th->th_seq) + 1,
5215                             TH_ACK, (*state)->src.max_win, 0, 0, 0,
5216                             (*state)->tag, NULL, NULL);
5217 #ifdef __FreeBSD__
5218                         pf_send_tcp(NULL, (*state)->rule.ptr, pd->af,
5219                             &src->addr,
5220 #else
5221                         pf_send_tcp((*state)->rule.ptr, pd->af, &src->addr,
5222 #endif
5223                             &dst->addr, src->port, dst->port,
5224                             (*state)->src.seqhi + 1, (*state)->src.seqlo + 1,
5225                             TH_ACK, (*state)->dst.max_win, 0, 0, 1,
5226                             0, NULL, NULL);
5227                         (*state)->src.seqdiff = (*state)->dst.seqhi -
5228                             (*state)->src.seqlo;
5229                         (*state)->dst.seqdiff = (*state)->src.seqhi -
5230                             (*state)->dst.seqlo;
5231                         (*state)->src.seqhi = (*state)->src.seqlo +
5232                             (*state)->dst.max_win;
5233                         (*state)->dst.seqhi = (*state)->dst.seqlo +
5234                             (*state)->src.max_win;
5235                         (*state)->src.wscale = (*state)->dst.wscale = 0;
5236                         (*state)->src.state = (*state)->dst.state =
5237                             TCPS_ESTABLISHED;
5238                         REASON_SET(reason, PFRES_SYNPROXY);
5239                         return (PF_SYNPROXY_DROP);
5240                 }
5241         }
5242
5243         if (((th->th_flags & (TH_SYN|TH_ACK)) == TH_SYN) &&
5244             dst->state >= TCPS_FIN_WAIT_2 &&
5245             src->state >= TCPS_FIN_WAIT_2) {
5246                 if (pf_status.debug >= PF_DEBUG_MISC) {
5247                         printf("pf: state reuse ");
5248                         pf_print_state(*state);
5249                         pf_print_flags(th->th_flags);
5250                         printf("\n");
5251                 }
5252                 /* XXX make sure it's the same direction ?? */
5253                 (*state)->src.state = (*state)->dst.state = TCPS_CLOSED;
5254                 pf_unlink_state(*state);
5255                 *state = NULL;
5256                 return (PF_DROP);
5257         }
5258
5259         if ((*state)->state_flags & PFSTATE_SLOPPY) {
5260                 if (pf_tcp_track_sloppy(src, dst, state, pd, reason) == PF_DROP)
5261                         return (PF_DROP);
5262         } else {
5263                 if (pf_tcp_track_full(src, dst, state, kif, m, off, pd, reason,
5264                     &copyback) == PF_DROP)
5265                         return (PF_DROP);
5266         }
5267
5268         /* translate source/destination address, if necessary */
5269         if (STATE_TRANSLATE(*state)) {
5270                 if (direction == PF_OUT)
5271                         pf_change_ap(pd->src, &th->th_sport, pd->ip_sum,
5272                             &th->th_sum, &(*state)->gwy.addr,
5273                             (*state)->gwy.port, 0, pd->af);
5274                 else
5275                         pf_change_ap(pd->dst, &th->th_dport, pd->ip_sum,
5276                             &th->th_sum, &(*state)->lan.addr,
5277                             (*state)->lan.port, 0, pd->af);
5278                 m_copyback(m, off, sizeof(*th), (caddr_t)th);
5279         } else if (copyback) {
5280                 /* Copyback sequence modulation or stateful scrub changes */
5281                 m_copyback(m, off, sizeof(*th), (caddr_t)th);
5282         }
5283
5284         return (PF_PASS);
5285 }
5286
5287 int
5288 pf_test_state_udp(struct pf_state **state, int direction, struct pfi_kif *kif,
5289     struct mbuf *m, int off, void *h, struct pf_pdesc *pd)
5290 {
5291         struct pf_state_peer    *src, *dst;
5292         struct pf_state_cmp      key;
5293         struct udphdr           *uh = pd->hdr.udp;
5294
5295         key.af = pd->af;
5296         key.proto = IPPROTO_UDP;
5297         if (direction == PF_IN) {
5298                 PF_ACPY(&key.ext.addr, pd->src, key.af);
5299                 PF_ACPY(&key.gwy.addr, pd->dst, key.af);
5300                 key.ext.port = uh->uh_sport;
5301                 key.gwy.port = uh->uh_dport;
5302         } else {
5303                 PF_ACPY(&key.lan.addr, pd->src, key.af);
5304                 PF_ACPY(&key.ext.addr, pd->dst, key.af);
5305                 key.lan.port = uh->uh_sport;
5306                 key.ext.port = uh->uh_dport;
5307         }
5308
5309         STATE_LOOKUP();
5310
5311         if (direction == (*state)->direction) {
5312                 src = &(*state)->src;
5313                 dst = &(*state)->dst;
5314         } else {
5315                 src = &(*state)->dst;
5316                 dst = &(*state)->src;
5317         }
5318
5319         /* update states */
5320         if (src->state < PFUDPS_SINGLE)
5321                 src->state = PFUDPS_SINGLE;
5322         if (dst->state == PFUDPS_SINGLE)
5323                 dst->state = PFUDPS_MULTIPLE;
5324
5325         /* update expire time */
5326         (*state)->expire = time_second;
5327         if (src->state == PFUDPS_MULTIPLE && dst->state == PFUDPS_MULTIPLE)
5328                 (*state)->timeout = PFTM_UDP_MULTIPLE;
5329         else
5330                 (*state)->timeout = PFTM_UDP_SINGLE;
5331
5332         /* translate source/destination address, if necessary */
5333         if (STATE_TRANSLATE(*state)) {
5334                 if (direction == PF_OUT)
5335                         pf_change_ap(pd->src, &uh->uh_sport, pd->ip_sum,
5336                             &uh->uh_sum, &(*state)->gwy.addr,
5337                             (*state)->gwy.port, 1, pd->af);
5338                 else
5339                         pf_change_ap(pd->dst, &uh->uh_dport, pd->ip_sum,
5340                             &uh->uh_sum, &(*state)->lan.addr,
5341                             (*state)->lan.port, 1, pd->af);
5342                 m_copyback(m, off, sizeof(*uh), (caddr_t)uh);
5343         }
5344
5345         return (PF_PASS);
5346 }
5347
5348 int
5349 pf_test_state_icmp(struct pf_state **state, int direction, struct pfi_kif *kif,
5350     struct mbuf *m, int off, void *h, struct pf_pdesc *pd, u_short *reason)
5351 {
5352         struct pf_addr  *saddr = pd->src, *daddr = pd->dst;
5353         u_int16_t        icmpid = 0;            /* make the compiler happy */
5354         u_int16_t       *icmpsum = NULL;        /* make the compiler happy */
5355         u_int8_t         icmptype = 0;          /* make the compiler happy */
5356         int              state_icmp = 0;
5357         struct pf_state_cmp key;
5358
5359         switch (pd->proto) {
5360 #ifdef INET
5361         case IPPROTO_ICMP:
5362                 icmptype = pd->hdr.icmp->icmp_type;
5363                 icmpid = pd->hdr.icmp->icmp_id;
5364                 icmpsum = &pd->hdr.icmp->icmp_cksum;
5365
5366                 if (icmptype == ICMP_UNREACH ||
5367                     icmptype == ICMP_SOURCEQUENCH ||
5368                     icmptype == ICMP_REDIRECT ||
5369                     icmptype == ICMP_TIMXCEED ||
5370                     icmptype == ICMP_PARAMPROB)
5371                         state_icmp++;
5372                 break;
5373 #endif /* INET */
5374 #ifdef INET6
5375         case IPPROTO_ICMPV6:
5376                 icmptype = pd->hdr.icmp6->icmp6_type;
5377                 icmpid = pd->hdr.icmp6->icmp6_id;
5378                 icmpsum = &pd->hdr.icmp6->icmp6_cksum;
5379
5380                 if (icmptype == ICMP6_DST_UNREACH ||
5381                     icmptype == ICMP6_PACKET_TOO_BIG ||
5382                     icmptype == ICMP6_TIME_EXCEEDED ||
5383                     icmptype == ICMP6_PARAM_PROB)
5384                         state_icmp++;
5385                 break;
5386 #endif /* INET6 */
5387         }
5388
5389         if (!state_icmp) {
5390
5391                 /*
5392                  * ICMP query/reply message not related to a TCP/UDP packet.
5393                  * Search for an ICMP state.
5394                  */
5395                 key.af = pd->af;
5396                 key.proto = pd->proto;
5397                 if (direction == PF_IN) {
5398                         PF_ACPY(&key.ext.addr, pd->src, key.af);
5399                         PF_ACPY(&key.gwy.addr, pd->dst, key.af);
5400                         key.ext.port = 0;
5401                         key.gwy.port = icmpid;
5402                 } else {
5403                         PF_ACPY(&key.lan.addr, pd->src, key.af);
5404                         PF_ACPY(&key.ext.addr, pd->dst, key.af);
5405                         key.lan.port = icmpid;
5406                         key.ext.port = 0;
5407                 }
5408
5409                 STATE_LOOKUP();
5410
5411                 (*state)->expire = time_second;
5412                 (*state)->timeout = PFTM_ICMP_ERROR_REPLY;
5413
5414                 /* translate source/destination address, if necessary */
5415                 if (STATE_TRANSLATE(*state)) {
5416                         if (direction == PF_OUT) {
5417                                 switch (pd->af) {
5418 #ifdef INET
5419                                 case AF_INET:
5420                                         pf_change_a(&saddr->v4.s_addr,
5421                                             pd->ip_sum,
5422                                             (*state)->gwy.addr.v4.s_addr, 0);
5423                                         pd->hdr.icmp->icmp_cksum =
5424                                             pf_cksum_fixup(
5425                                             pd->hdr.icmp->icmp_cksum, icmpid,
5426                                             (*state)->gwy.port, 0);
5427                                         pd->hdr.icmp->icmp_id =
5428                                             (*state)->gwy.port;
5429                                         m_copyback(m, off, ICMP_MINLEN,
5430                                             (caddr_t)pd->hdr.icmp);
5431                                         break;
5432 #endif /* INET */
5433 #ifdef INET6
5434                                 case AF_INET6:
5435                                         pf_change_a6(saddr,
5436                                             &pd->hdr.icmp6->icmp6_cksum,
5437                                             &(*state)->gwy.addr, 0);
5438                                         m_copyback(m, off,
5439                                             sizeof(struct icmp6_hdr),
5440                                             (caddr_t)pd->hdr.icmp6);
5441                                         break;
5442 #endif /* INET6 */
5443                                 }
5444                         } else {
5445                                 switch (pd->af) {
5446 #ifdef INET
5447                                 case AF_INET:
5448                                         pf_change_a(&daddr->v4.s_addr,
5449                                             pd->ip_sum,
5450                                             (*state)->lan.addr.v4.s_addr, 0);
5451                                         pd->hdr.icmp->icmp_cksum =
5452                                             pf_cksum_fixup(
5453                                             pd->hdr.icmp->icmp_cksum, icmpid,
5454                                             (*state)->lan.port, 0);
5455                                         pd->hdr.icmp->icmp_id =
5456                                             (*state)->lan.port;
5457                                         m_copyback(m, off, ICMP_MINLEN,
5458                                             (caddr_t)pd->hdr.icmp);
5459                                         break;
5460 #endif /* INET */
5461 #ifdef INET6
5462                                 case AF_INET6:
5463                                         pf_change_a6(daddr,
5464                                             &pd->hdr.icmp6->icmp6_cksum,
5465                                             &(*state)->lan.addr, 0);
5466                                         m_copyback(m, off,
5467                                             sizeof(struct icmp6_hdr),
5468                                             (caddr_t)pd->hdr.icmp6);
5469                                         break;
5470 #endif /* INET6 */
5471                                 }
5472                         }
5473                 }
5474
5475                 return (PF_PASS);
5476
5477         } else {
5478                 /*
5479                  * ICMP error message in response to a TCP/UDP packet.
5480                  * Extract the inner TCP/UDP header and search for that state.
5481                  */
5482
5483                 struct pf_pdesc pd2;
5484 #ifdef INET
5485                 struct ip       h2;
5486 #endif /* INET */
5487 #ifdef INET6
5488                 struct ip6_hdr  h2_6;
5489                 int             terminal = 0;
5490 #endif /* INET6 */
5491                 int             ipoff2 = 0;     /* make the compiler happy */
5492                 int             off2 = 0;       /* make the compiler happy */
5493
5494                 pd2.af = pd->af;
5495                 switch (pd->af) {
5496 #ifdef INET
5497                 case AF_INET:
5498                         /* offset of h2 in mbuf chain */
5499                         ipoff2 = off + ICMP_MINLEN;
5500
5501                         if (!pf_pull_hdr(m, ipoff2, &h2, sizeof(h2),
5502                             NULL, reason, pd2.af)) {
5503                                 DPFPRINTF(PF_DEBUG_MISC,
5504                                     ("pf: ICMP error message too short "
5505                                     "(ip)\n"));
5506                                 return (PF_DROP);
5507                         }
5508                         /*
5509                          * ICMP error messages don't refer to non-first
5510                          * fragments
5511                          */
5512                         if (h2.ip_off & htons(IP_OFFMASK)) {
5513                                 REASON_SET(reason, PFRES_FRAG);
5514                                 return (PF_DROP);
5515                         }
5516
5517                         /* offset of protocol header that follows h2 */
5518                         off2 = ipoff2 + (h2.ip_hl << 2);
5519
5520                         pd2.proto = h2.ip_p;
5521                         pd2.src = (struct pf_addr *)&h2.ip_src;
5522                         pd2.dst = (struct pf_addr *)&h2.ip_dst;
5523                         pd2.ip_sum = &h2.ip_sum;
5524                         break;
5525 #endif /* INET */
5526 #ifdef INET6
5527                 case AF_INET6:
5528                         ipoff2 = off + sizeof(struct icmp6_hdr);
5529
5530                         if (!pf_pull_hdr(m, ipoff2, &h2_6, sizeof(h2_6),
5531                             NULL, reason, pd2.af)) {
5532                                 DPFPRINTF(PF_DEBUG_MISC,
5533                                     ("pf: ICMP error message too short "
5534                                     "(ip6)\n"));
5535                                 return (PF_DROP);
5536                         }
5537                         pd2.proto = h2_6.ip6_nxt;
5538                         pd2.src = (struct pf_addr *)&h2_6.ip6_src;
5539                         pd2.dst = (struct pf_addr *)&h2_6.ip6_dst;
5540                         pd2.ip_sum = NULL;
5541                         off2 = ipoff2 + sizeof(h2_6);
5542                         do {
5543                                 switch (pd2.proto) {
5544                                 case IPPROTO_FRAGMENT:
5545                                         /*
5546                                          * ICMPv6 error messages for
5547                                          * non-first fragments
5548                                          */
5549                                         REASON_SET(reason, PFRES_FRAG);
5550                                         return (PF_DROP);
5551                                 case IPPROTO_AH:
5552                                 case IPPROTO_HOPOPTS:
5553                                 case IPPROTO_ROUTING:
5554                                 case IPPROTO_DSTOPTS: {
5555                                         /* get next header and header length */
5556                                         struct ip6_ext opt6;
5557
5558                                         if (!pf_pull_hdr(m, off2, &opt6,
5559                                             sizeof(opt6), NULL, reason,
5560                                             pd2.af)) {
5561                                                 DPFPRINTF(PF_DEBUG_MISC,
5562                                                     ("pf: ICMPv6 short opt\n"));
5563                                                 return (PF_DROP);
5564                                         }
5565                                         if (pd2.proto == IPPROTO_AH)
5566                                                 off2 += (opt6.ip6e_len + 2) * 4;
5567                                         else
5568                                                 off2 += (opt6.ip6e_len + 1) * 8;
5569                                         pd2.proto = opt6.ip6e_nxt;
5570                                         /* goto the next header */
5571                                         break;
5572                                 }
5573                                 default:
5574                                         terminal++;
5575                                         break;
5576                                 }
5577                         } while (!terminal);
5578                         break;
5579 #endif /* INET6 */
5580 #ifdef __FreeBSD__
5581                 default:
5582                         panic("AF not supported: %d", pd->af);
5583 #endif
5584                 }
5585
5586                 switch (pd2.proto) {
5587                 case IPPROTO_TCP: {
5588                         struct tcphdr            th;
5589                         u_int32_t                seq;
5590                         struct pf_state_peer    *src, *dst;
5591                         u_int8_t                 dws;
5592                         int                      copyback = 0;
5593
5594                         /*
5595                          * Only the first 8 bytes of the TCP header can be
5596                          * expected. Don't access any TCP header fields after
5597                          * th_seq, an ackskew test is not possible.
5598                          */
5599                         if (!pf_pull_hdr(m, off2, &th, 8, NULL, reason,
5600                             pd2.af)) {
5601                                 DPFPRINTF(PF_DEBUG_MISC,
5602                                     ("pf: ICMP error message too short "
5603                                     "(tcp)\n"));
5604                                 return (PF_DROP);
5605                         }
5606
5607                         key.af = pd2.af;
5608                         key.proto = IPPROTO_TCP;
5609                         if (direction == PF_IN) {
5610                                 PF_ACPY(&key.ext.addr, pd2.dst, key.af);
5611                                 PF_ACPY(&key.gwy.addr, pd2.src, key.af);
5612                                 key.ext.port = th.th_dport;
5613                                 key.gwy.port = th.th_sport;
5614                         } else {
5615                                 PF_ACPY(&key.lan.addr, pd2.dst, key.af);
5616                                 PF_ACPY(&key.ext.addr, pd2.src, key.af);
5617                                 key.lan.port = th.th_dport;
5618                                 key.ext.port = th.th_sport;
5619                         }
5620
5621                         STATE_LOOKUP();
5622
5623                         if (direction == (*state)->direction) {
5624                                 src = &(*state)->dst;
5625                                 dst = &(*state)->src;
5626                         } else {
5627                                 src = &(*state)->src;
5628                                 dst = &(*state)->dst;
5629                         }
5630
5631                         if (src->wscale && dst->wscale)
5632                                 dws = dst->wscale & PF_WSCALE_MASK;
5633                         else
5634                                 dws = 0;
5635
5636                         /* Demodulate sequence number */
5637                         seq = ntohl(th.th_seq) - src->seqdiff;
5638                         if (src->seqdiff) {
5639                                 pf_change_a(&th.th_seq, icmpsum,
5640                                     htonl(seq), 0);
5641                                 copyback = 1;
5642                         }
5643
5644                         if (!((*state)->state_flags & PFSTATE_SLOPPY) &&
5645                             (!SEQ_GEQ(src->seqhi, seq) ||
5646                             !SEQ_GEQ(seq, src->seqlo - (dst->max_win << dws)))) {
5647                                 if (pf_status.debug >= PF_DEBUG_MISC) {
5648                                         printf("pf: BAD ICMP %d:%d ",
5649                                             icmptype, pd->hdr.icmp->icmp_code);
5650                                         pf_print_host(pd->src, 0, pd->af);
5651                                         printf(" -> ");
5652                                         pf_print_host(pd->dst, 0, pd->af);
5653                                         printf(" state: ");
5654                                         pf_print_state(*state);
5655                                         printf(" seq=%u\n", seq);
5656                                 }
5657                                 REASON_SET(reason, PFRES_BADSTATE);
5658                                 return (PF_DROP);
5659                         }
5660
5661                         if (STATE_TRANSLATE(*state)) {
5662                                 if (direction == PF_IN) {
5663                                         pf_change_icmp(pd2.src, &th.th_sport,
5664                                             daddr, &(*state)->lan.addr,
5665                                             (*state)->lan.port, NULL,
5666                                             pd2.ip_sum, icmpsum,
5667                                             pd->ip_sum, 0, pd2.af);
5668                                 } else {
5669                                         pf_change_icmp(pd2.dst, &th.th_dport,
5670                                             saddr, &(*state)->gwy.addr,
5671                                             (*state)->gwy.port, NULL,
5672                                             pd2.ip_sum, icmpsum,
5673                                             pd->ip_sum, 0, pd2.af);
5674                                 }
5675                                 copyback = 1;
5676                         }
5677
5678                         if (copyback) {
5679                                 switch (pd2.af) {
5680 #ifdef INET
5681                                 case AF_INET:
5682                                         m_copyback(m, off, ICMP_MINLEN,
5683                                             (caddr_t)pd->hdr.icmp);
5684                                         m_copyback(m, ipoff2, sizeof(h2),
5685                                             (caddr_t)&h2);
5686                                         break;
5687 #endif /* INET */
5688 #ifdef INET6
5689                                 case AF_INET6:
5690                                         m_copyback(m, off,
5691                                             sizeof(struct icmp6_hdr),
5692                                             (caddr_t)pd->hdr.icmp6);
5693                                         m_copyback(m, ipoff2, sizeof(h2_6),
5694                                             (caddr_t)&h2_6);
5695                                         break;
5696 #endif /* INET6 */
5697                                 }
5698                                 m_copyback(m, off2, 8, (caddr_t)&th);
5699                         }
5700
5701                         return (PF_PASS);
5702                         break;
5703                 }
5704                 case IPPROTO_UDP: {
5705                         struct udphdr           uh;
5706
5707                         if (!pf_pull_hdr(m, off2, &uh, sizeof(uh),
5708                             NULL, reason, pd2.af)) {
5709                                 DPFPRINTF(PF_DEBUG_MISC,
5710                                     ("pf: ICMP error message too short "
5711                                     "(udp)\n"));
5712                                 return (PF_DROP);
5713                         }
5714
5715                         key.af = pd2.af;
5716                         key.proto = IPPROTO_UDP;
5717                         if (direction == PF_IN) {
5718                                 PF_ACPY(&key.ext.addr, pd2.dst, key.af);
5719                                 PF_ACPY(&key.gwy.addr, pd2.src, key.af);
5720                                 key.ext.port = uh.uh_dport;
5721                                 key.gwy.port = uh.uh_sport;
5722                         } else {
5723                                 PF_ACPY(&key.lan.addr, pd2.dst, key.af);
5724                                 PF_ACPY(&key.ext.addr, pd2.src, key.af);
5725                                 key.lan.port = uh.uh_dport;
5726                                 key.ext.port = uh.uh_sport;
5727                         }
5728
5729                         STATE_LOOKUP();
5730
5731                         if (STATE_TRANSLATE(*state)) {
5732                                 if (direction == PF_IN) {
5733                                         pf_change_icmp(pd2.src, &uh.uh_sport,
5734                                             daddr, &(*state)->lan.addr,
5735                                             (*state)->lan.port, &uh.uh_sum,
5736                                             pd2.ip_sum, icmpsum,
5737                                             pd->ip_sum, 1, pd2.af);
5738                                 } else {
5739                                         pf_change_icmp(pd2.dst, &uh.uh_dport,
5740                                             saddr, &(*state)->gwy.addr,
5741                                             (*state)->gwy.port, &uh.uh_sum,
5742                                             pd2.ip_sum, icmpsum,
5743                                             pd->ip_sum, 1, pd2.af);
5744                                 }
5745                                 switch (pd2.af) {
5746 #ifdef INET
5747                                 case AF_INET:
5748                                         m_copyback(m, off, ICMP_MINLEN,
5749                                             (caddr_t)pd->hdr.icmp);
5750                                         m_copyback(m, ipoff2, sizeof(h2),
5751                                             (caddr_t)&h2);
5752                                         break;
5753 #endif /* INET */
5754 #ifdef INET6
5755                                 case AF_INET6:
5756                                         m_copyback(m, off,
5757                                             sizeof(struct icmp6_hdr),
5758                                             (caddr_t)pd->hdr.icmp6);
5759                                         m_copyback(m, ipoff2, sizeof(h2_6),
5760                                             (caddr_t)&h2_6);
5761                                         break;
5762 #endif /* INET6 */
5763                                 }
5764                                 m_copyback(m, off2, sizeof(uh),
5765                                     (caddr_t)&uh);
5766                         }
5767
5768                         return (PF_PASS);
5769                         break;
5770                 }
5771 #ifdef INET
5772                 case IPPROTO_ICMP: {
5773                         struct icmp             iih;
5774
5775                         if (!pf_pull_hdr(m, off2, &iih, ICMP_MINLEN,
5776                             NULL, reason, pd2.af)) {
5777                                 DPFPRINTF(PF_DEBUG_MISC,
5778                                     ("pf: ICMP error message too short i"
5779                                     "(icmp)\n"));
5780                                 return (PF_DROP);
5781                         }
5782
5783                         key.af = pd2.af;
5784                         key.proto = IPPROTO_ICMP;
5785                         if (direction == PF_IN) {
5786                                 PF_ACPY(&key.ext.addr, pd2.dst, key.af);
5787                                 PF_ACPY(&key.gwy.addr, pd2.src, key.af);
5788                                 key.ext.port = 0;
5789                                 key.gwy.port = iih.icmp_id;
5790                         } else {
5791                                 PF_ACPY(&key.lan.addr, pd2.dst, key.af);
5792                                 PF_ACPY(&key.ext.addr, pd2.src, key.af);
5793                                 key.lan.port = iih.icmp_id;
5794                                 key.ext.port = 0;
5795                         }
5796
5797                         STATE_LOOKUP();
5798
5799                         if (STATE_TRANSLATE(*state)) {
5800                                 if (direction == PF_IN) {
5801                                         pf_change_icmp(pd2.src, &iih.icmp_id,
5802                                             daddr, &(*state)->lan.addr,
5803                                             (*state)->lan.port, NULL,
5804                                             pd2.ip_sum, icmpsum,
5805                                             pd->ip_sum, 0, AF_INET);
5806                                 } else {
5807                                         pf_change_icmp(pd2.dst, &iih.icmp_id,
5808                                             saddr, &(*state)->gwy.addr,
5809                                             (*state)->gwy.port, NULL,
5810                                             pd2.ip_sum, icmpsum,
5811                                             pd->ip_sum, 0, AF_INET);
5812                                 }
5813                                 m_copyback(m, off, ICMP_MINLEN,
5814                                     (caddr_t)pd->hdr.icmp);
5815                                 m_copyback(m, ipoff2, sizeof(h2),
5816                                     (caddr_t)&h2);
5817                                 m_copyback(m, off2, ICMP_MINLEN,
5818                                     (caddr_t)&iih);
5819                         }
5820
5821                         return (PF_PASS);
5822                         break;
5823                 }
5824 #endif /* INET */
5825 #ifdef INET6
5826                 case IPPROTO_ICMPV6: {
5827                         struct icmp6_hdr        iih;
5828
5829                         if (!pf_pull_hdr(m, off2, &iih,
5830                             sizeof(struct icmp6_hdr), NULL, reason, pd2.af)) {
5831                                 DPFPRINTF(PF_DEBUG_MISC,
5832                                     ("pf: ICMP error message too short "
5833                                     "(icmp6)\n"));
5834                                 return (PF_DROP);
5835                         }
5836
5837                         key.af = pd2.af;
5838                         key.proto = IPPROTO_ICMPV6;
5839                         if (direction == PF_IN) {
5840                                 PF_ACPY(&key.ext.addr, pd2.dst, key.af);
5841                                 PF_ACPY(&key.gwy.addr, pd2.src, key.af);
5842                                 key.ext.port = 0;
5843                                 key.gwy.port = iih.icmp6_id;
5844                         } else {
5845                                 PF_ACPY(&key.lan.addr, pd2.dst, key.af);
5846                                 PF_ACPY(&key.ext.addr, pd2.src, key.af);
5847                                 key.lan.port = iih.icmp6_id;
5848                                 key.ext.port = 0;
5849                         }
5850
5851                         STATE_LOOKUP();
5852
5853                         if (STATE_TRANSLATE(*state)) {
5854                                 if (direction == PF_IN) {
5855                                         pf_change_icmp(pd2.src, &iih.icmp6_id,
5856                                             daddr, &(*state)->lan.addr,
5857                                             (*state)->lan.port, NULL,
5858                                             pd2.ip_sum, icmpsum,
5859                                             pd->ip_sum, 0, AF_INET6);
5860                                 } else {
5861                                         pf_change_icmp(pd2.dst, &iih.icmp6_id,
5862                                             saddr, &(*state)->gwy.addr,
5863                                             (*state)->gwy.port, NULL,
5864                                             pd2.ip_sum, icmpsum,
5865                                             pd->ip_sum, 0, AF_INET6);
5866                                 }
5867                                 m_copyback(m, off, sizeof(struct icmp6_hdr),
5868                                     (caddr_t)pd->hdr.icmp6);
5869                                 m_copyback(m, ipoff2, sizeof(h2_6),
5870                                     (caddr_t)&h2_6);
5871                                 m_copyback(m, off2, sizeof(struct icmp6_hdr),
5872                                     (caddr_t)&iih);
5873                         }
5874
5875                         return (PF_PASS);
5876                         break;
5877                 }
5878 #endif /* INET6 */
5879                 default: {
5880                         key.af = pd2.af;
5881                         key.proto = pd2.proto;
5882                         if (direction == PF_IN) {
5883                                 PF_ACPY(&key.ext.addr, pd2.dst, key.af);
5884                                 PF_ACPY(&key.gwy.addr, pd2.src, key.af);
5885                                 key.ext.port = 0;
5886                                 key.gwy.port = 0;
5887                         } else {
5888                                 PF_ACPY(&key.lan.addr, pd2.dst, key.af);
5889                                 PF_ACPY(&key.ext.addr, pd2.src, key.af);
5890                                 key.lan.port = 0;
5891                                 key.ext.port = 0;
5892                         }
5893
5894                         STATE_LOOKUP();
5895
5896                         if (STATE_TRANSLATE(*state)) {
5897                                 if (direction == PF_IN) {
5898                                         pf_change_icmp(pd2.src, NULL,
5899                                             daddr, &(*state)->lan.addr,
5900                                             0, NULL,
5901                                             pd2.ip_sum, icmpsum,
5902                                             pd->ip_sum, 0, pd2.af);
5903                                 } else {
5904                                         pf_change_icmp(pd2.dst, NULL,
5905                                             saddr, &(*state)->gwy.addr,
5906                                             0, NULL,
5907                                             pd2.ip_sum, icmpsum,
5908                                             pd->ip_sum, 0, pd2.af);
5909                                 }
5910                                 switch (pd2.af) {
5911 #ifdef INET
5912                                 case AF_INET:
5913                                         m_copyback(m, off, ICMP_MINLEN,
5914                                             (caddr_t)pd->hdr.icmp);
5915                                         m_copyback(m, ipoff2, sizeof(h2),
5916                                             (caddr_t)&h2);
5917                                         break;
5918 #endif /* INET */
5919 #ifdef INET6
5920                                 case AF_INET6:
5921                                         m_copyback(m, off,
5922                                             sizeof(struct icmp6_hdr),
5923                                             (caddr_t)pd->hdr.icmp6);
5924                                         m_copyback(m, ipoff2, sizeof(h2_6),
5925                                             (caddr_t)&h2_6);
5926                                         break;
5927 #endif /* INET6 */
5928                                 }
5929                         }
5930
5931                         return (PF_PASS);
5932                         break;
5933                 }
5934                 }
5935         }
5936 }
5937
5938 int
5939 pf_test_state_other(struct pf_state **state, int direction, struct pfi_kif *kif,
5940     struct pf_pdesc *pd)
5941 {
5942         struct pf_state_peer    *src, *dst;
5943         struct pf_state_cmp      key;
5944
5945         key.af = pd->af;
5946         key.proto = pd->proto;
5947         if (direction == PF_IN) {
5948                 PF_ACPY(&key.ext.addr, pd->src, key.af);
5949                 PF_ACPY(&key.gwy.addr, pd->dst, key.af);
5950                 key.ext.port = 0;
5951                 key.gwy.port = 0;
5952         } else {
5953                 PF_ACPY(&key.lan.addr, pd->src, key.af);
5954                 PF_ACPY(&key.ext.addr, pd->dst, key.af);
5955                 key.lan.port = 0;
5956                 key.ext.port = 0;
5957         }
5958
5959         STATE_LOOKUP();
5960
5961         if (direction == (*state)->direction) {
5962                 src = &(*state)->src;
5963                 dst = &(*state)->dst;
5964         } else {
5965                 src = &(*state)->dst;
5966                 dst = &(*state)->src;
5967         }
5968
5969         /* update states */
5970         if (src->state < PFOTHERS_SINGLE)
5971                 src->state = PFOTHERS_SINGLE;
5972         if (dst->state == PFOTHERS_SINGLE)
5973                 dst->state = PFOTHERS_MULTIPLE;
5974
5975         /* update expire time */
5976         (*state)->expire = time_second;
5977         if (src->state == PFOTHERS_MULTIPLE && dst->state == PFOTHERS_MULTIPLE)
5978                 (*state)->timeout = PFTM_OTHER_MULTIPLE;
5979         else
5980                 (*state)->timeout = PFTM_OTHER_SINGLE;
5981
5982         /* translate source/destination address, if necessary */
5983         if (STATE_TRANSLATE(*state)) {
5984                 if (direction == PF_OUT)
5985                         switch (pd->af) {
5986 #ifdef INET
5987                         case AF_INET:
5988                                 pf_change_a(&pd->src->v4.s_addr,
5989                                     pd->ip_sum, (*state)->gwy.addr.v4.s_addr,
5990                                     0);
5991                                 break;
5992 #endif /* INET */
5993 #ifdef INET6
5994                         case AF_INET6:
5995                                 PF_ACPY(pd->src, &(*state)->gwy.addr, pd->af);
5996                                 break;
5997 #endif /* INET6 */
5998                         }
5999                 else
6000                         switch (pd->af) {
6001 #ifdef INET
6002                         case AF_INET:
6003                                 pf_change_a(&pd->dst->v4.s_addr,
6004                                     pd->ip_sum, (*state)->lan.addr.v4.s_addr,
6005                                     0);
6006                                 break;
6007 #endif /* INET */
6008 #ifdef INET6
6009                         case AF_INET6:
6010                                 PF_ACPY(pd->dst, &(*state)->lan.addr, pd->af);
6011                                 break;
6012 #endif /* INET6 */
6013                         }
6014         }
6015
6016         return (PF_PASS);
6017 }
6018
6019 /*
6020  * ipoff and off are measured from the start of the mbuf chain.
6021  * h must be at "ipoff" on the mbuf chain.
6022  */
6023 void *
6024 pf_pull_hdr(struct mbuf *m, int off, void *p, int len,
6025     u_short *actionp, u_short *reasonp, sa_family_t af)
6026 {
6027         switch (af) {
6028 #ifdef INET
6029         case AF_INET: {
6030                 struct ip       *h = mtod(m, struct ip *);
6031                 u_int16_t        fragoff = (ntohs(h->ip_off) & IP_OFFMASK) << 3;
6032
6033                 if (fragoff) {
6034                         if (fragoff >= len)
6035                                 ACTION_SET(actionp, PF_PASS);
6036                         else {
6037                                 ACTION_SET(actionp, PF_DROP);
6038                                 REASON_SET(reasonp, PFRES_FRAG);
6039                         }
6040                         return (NULL);
6041                 }
6042                 if (m->m_pkthdr.len < off + len ||
6043                     ntohs(h->ip_len) < off + len) {
6044                         ACTION_SET(actionp, PF_DROP);
6045                         REASON_SET(reasonp, PFRES_SHORT);
6046                         return (NULL);
6047                 }
6048                 break;
6049         }
6050 #endif /* INET */
6051 #ifdef INET6
6052         case AF_INET6: {
6053                 struct ip6_hdr  *h = mtod(m, struct ip6_hdr *);
6054
6055                 if (m->m_pkthdr.len < off + len ||
6056                     (ntohs(h->ip6_plen) + sizeof(struct ip6_hdr)) <
6057                     (unsigned)(off + len)) {
6058                         ACTION_SET(actionp, PF_DROP);
6059                         REASON_SET(reasonp, PFRES_SHORT);
6060                         return (NULL);
6061                 }
6062                 break;
6063         }
6064 #endif /* INET6 */
6065         }
6066         m_copydata(m, off, len, p);
6067         return (p);
6068 }
6069
6070 int
6071 pf_routable(struct pf_addr *addr, sa_family_t af, struct pfi_kif *kif)
6072 {
6073         struct sockaddr_in      *dst;
6074         int                      ret = 1;
6075         int                      check_mpath;
6076 #ifndef __FreeBSD__
6077         extern int               ipmultipath;
6078 #endif
6079 #ifdef INET6
6080 #ifndef __FreeBSD__
6081         extern int               ip6_multipath;
6082 #endif
6083         struct sockaddr_in6     *dst6;
6084         struct route_in6         ro;
6085 #else
6086         struct route             ro;
6087 #endif
6088         struct radix_node       *rn;
6089         struct rtentry          *rt;
6090         struct ifnet            *ifp;
6091
6092         check_mpath = 0;
6093         bzero(&ro, sizeof(ro));
6094         switch (af) {
6095         case AF_INET:
6096                 dst = satosin(&ro.ro_dst);
6097                 dst->sin_family = AF_INET;
6098                 dst->sin_len = sizeof(*dst);
6099                 dst->sin_addr = addr->v4;
6100 #ifndef __FreeBSD__     /* MULTIPATH_ROUTING */
6101                 if (ipmultipath)
6102                         check_mpath = 1;
6103 #endif
6104                 break;
6105 #ifdef INET6
6106         case AF_INET6:
6107                 dst6 = (struct sockaddr_in6 *)&ro.ro_dst;
6108                 dst6->sin6_family = AF_INET6;
6109                 dst6->sin6_len = sizeof(*dst6);
6110                 dst6->sin6_addr = addr->v6;
6111 #ifndef __FreeBSD__     /* MULTIPATH_ROUTING */
6112                 if (ip6_multipath)
6113                         check_mpath = 1;
6114 #endif
6115                 break;
6116 #endif /* INET6 */
6117         default:
6118                 return (0);
6119         }
6120
6121         /* Skip checks for ipsec interfaces */
6122         if (kif != NULL && kif->pfik_ifp->if_type == IFT_ENC)
6123                 goto out;
6124
6125 #ifdef __FreeBSD__
6126 /* XXX MRT not always INET */ /* stick with table 0 though */
6127         if (af == AF_INET)
6128                 in_rtalloc_ign((struct route *)&ro, 0, RT_DEFAULT_FIB);
6129 #ifdef INET6
6130         else
6131                 in6_rtalloc_ign(&ro, 0, RT_DEFAULT_FIB);
6132 #endif
6133 #else /* ! __FreeBSD__ */
6134         rtalloc_noclone((struct route *)&ro, NO_CLONING);
6135 #endif
6136
6137         if (ro.ro_rt != NULL) {
6138                 /* No interface given, this is a no-route check */
6139                 if (kif == NULL)
6140                         goto out;
6141
6142                 if (kif->pfik_ifp == NULL) {
6143                         ret = 0;
6144                         goto out;
6145                 }
6146
6147                 /* Perform uRPF check if passed input interface */
6148                 ret = 0;
6149                 rn = (struct radix_node *)ro.ro_rt;
6150                 do {
6151                         rt = (struct rtentry *)rn;
6152 #ifndef __FreeBSD__ /* CARPDEV */
6153                         if (rt->rt_ifp->if_type == IFT_CARP)
6154                                 ifp = rt->rt_ifp->if_carpdev;
6155                         else
6156 #endif
6157                                 ifp = rt->rt_ifp;
6158
6159                         if (kif->pfik_ifp == ifp)
6160                                 ret = 1;
6161 #ifdef __FreeBSD__ /* MULTIPATH_ROUTING */
6162                         rn = NULL;
6163 #else
6164                         rn = rn_mpath_next(rn);
6165 #endif
6166                 } while (check_mpath == 1 && rn != NULL && ret == 0);
6167         } else
6168                 ret = 0;
6169 out:
6170         if (ro.ro_rt != NULL)
6171                 RTFREE(ro.ro_rt);
6172         return (ret);
6173 }
6174
6175 int
6176 pf_rtlabel_match(struct pf_addr *addr, sa_family_t af, struct pf_addr_wrap *aw)
6177 {
6178         struct sockaddr_in      *dst;
6179 #ifdef INET6
6180         struct sockaddr_in6     *dst6;
6181         struct route_in6         ro;
6182 #else
6183         struct route             ro;
6184 #endif
6185         int                      ret = 0;
6186
6187         bzero(&ro, sizeof(ro));
6188         switch (af) {
6189         case AF_INET:
6190                 dst = satosin(&ro.ro_dst);
6191                 dst->sin_family = AF_INET;
6192                 dst->sin_len = sizeof(*dst);
6193                 dst->sin_addr = addr->v4;
6194                 break;
6195 #ifdef INET6
6196         case AF_INET6:
6197                 dst6 = (struct sockaddr_in6 *)&ro.ro_dst;
6198                 dst6->sin6_family = AF_INET6;
6199                 dst6->sin6_len = sizeof(*dst6);
6200                 dst6->sin6_addr = addr->v6;
6201                 break;
6202 #endif /* INET6 */
6203         default:
6204                 return (0);
6205         }
6206
6207 #ifdef __FreeBSD__
6208         if (af == AF_INET)
6209                 in_rtalloc_ign((struct route *)&ro, 0, RT_DEFAULT_FIB);
6210 #ifdef INET6
6211         else
6212                 in6_rtalloc_ign(&ro, 0, RT_DEFAULT_FIB);
6213 #endif
6214 #else /* ! __FreeBSD__ */
6215         rtalloc_noclone((struct route *)&ro, NO_CLONING);
6216 #endif
6217
6218         if (ro.ro_rt != NULL) {
6219 #ifdef __FreeBSD__
6220                 /* XXX_IMPORT: later */
6221 #else
6222                 if (ro.ro_rt->rt_labelid == aw->v.rtlabel)
6223                         ret = 1;
6224 #endif
6225                 RTFREE(ro.ro_rt);
6226         }
6227
6228         return (ret);
6229 }
6230
6231 #ifdef INET
6232
6233 void
6234 pf_route(struct mbuf **m, struct pf_rule *r, int dir, struct ifnet *oifp,
6235     struct pf_state *s, struct pf_pdesc *pd)
6236 {
6237         struct mbuf             *m0, *m1;
6238         struct route             iproute;
6239         struct route            *ro = NULL;
6240         struct sockaddr_in      *dst;
6241         struct ip               *ip;
6242         struct ifnet            *ifp = NULL;
6243         struct pf_addr           naddr;
6244         struct pf_src_node      *sn = NULL;
6245         int                      error = 0;
6246 #ifdef __FreeBSD__
6247         int sw_csum;
6248 #endif
6249 #ifdef IPSEC
6250         struct m_tag            *mtag;
6251 #endif /* IPSEC */
6252
6253         if (m == NULL || *m == NULL || r == NULL ||
6254             (dir != PF_IN && dir != PF_OUT) || oifp == NULL)
6255                 panic("pf_route: invalid parameters");
6256
6257         if (pd->pf_mtag->routed++ > 3) {
6258                 m0 = *m;
6259                 *m = NULL;
6260                 goto bad;
6261         }
6262
6263         if (r->rt == PF_DUPTO) {
6264 #ifdef __FreeBSD__
6265                 if ((m0 = m_dup(*m, M_DONTWAIT)) == NULL)
6266 #else
6267                 if ((m0 = m_copym2(*m, 0, M_COPYALL, M_NOWAIT)) == NULL)
6268 #endif
6269                         return;
6270         } else {
6271                 if ((r->rt == PF_REPLYTO) == (r->direction == dir))
6272                         return;
6273                 m0 = *m;
6274         }
6275
6276         if (m0->m_len < sizeof(struct ip)) {
6277                 DPFPRINTF(PF_DEBUG_URGENT,
6278                     ("pf_route: m0->m_len < sizeof(struct ip)\n"));
6279                 goto bad;
6280         }
6281
6282         ip = mtod(m0, struct ip *);
6283
6284         ro = &iproute;
6285         bzero((caddr_t)ro, sizeof(*ro));
6286         dst = satosin(&ro->ro_dst);
6287         dst->sin_family = AF_INET;
6288         dst->sin_len = sizeof(*dst);
6289         dst->sin_addr = ip->ip_dst;
6290
6291         if (r->rt == PF_FASTROUTE) {
6292                 in_rtalloc(ro, 0);
6293                 if (ro->ro_rt == 0) {
6294                         KMOD_IPSTAT_INC(ips_noroute);
6295                         goto bad;
6296                 }
6297
6298                 ifp = ro->ro_rt->rt_ifp;
6299                 ro->ro_rt->rt_use++;
6300
6301                 if (ro->ro_rt->rt_flags & RTF_GATEWAY)
6302                         dst = satosin(ro->ro_rt->rt_gateway);
6303         } else {
6304                 if (TAILQ_EMPTY(&r->rpool.list)) {
6305                         DPFPRINTF(PF_DEBUG_URGENT,
6306                             ("pf_route: TAILQ_EMPTY(&r->rpool.list)\n"));
6307                         goto bad;
6308                 }
6309                 if (s == NULL) {
6310                         pf_map_addr(AF_INET, r, (struct pf_addr *)&ip->ip_src,
6311                             &naddr, NULL, &sn);
6312                         if (!PF_AZERO(&naddr, AF_INET))
6313                                 dst->sin_addr.s_addr = naddr.v4.s_addr;
6314                         ifp = r->rpool.cur->kif ?
6315                             r->rpool.cur->kif->pfik_ifp : NULL;
6316                 } else {
6317                         if (!PF_AZERO(&s->rt_addr, AF_INET))
6318                                 dst->sin_addr.s_addr =
6319                                     s->rt_addr.v4.s_addr;
6320                         ifp = s->rt_kif ? s->rt_kif->pfik_ifp : NULL;
6321                 }
6322         }
6323         if (ifp == NULL)
6324                 goto bad;
6325
6326         if (oifp != ifp) {
6327 #ifdef __FreeBSD__
6328                 PF_UNLOCK();
6329                 if (pf_test(PF_OUT, ifp, &m0, NULL, NULL) != PF_PASS) {
6330                         PF_LOCK();
6331                         goto bad;
6332                 } else if (m0 == NULL) {
6333                         PF_LOCK();
6334                         goto done;
6335                 }
6336                 PF_LOCK();
6337 #else
6338                 if (pf_test(PF_OUT, ifp, &m0, NULL) != PF_PASS)
6339                         goto bad;
6340                 else if (m0 == NULL)
6341                         goto done;
6342 #endif
6343                 if (m0->m_len < sizeof(struct ip)) {
6344                         DPFPRINTF(PF_DEBUG_URGENT,
6345                             ("pf_route: m0->m_len < sizeof(struct ip)\n"));
6346                         goto bad;
6347                 }
6348                 ip = mtod(m0, struct ip *);
6349         }
6350
6351 #ifdef __FreeBSD__
6352         /* Copied from FreeBSD 5.1-CURRENT ip_output. */
6353         m0->m_pkthdr.csum_flags |= CSUM_IP;
6354         sw_csum = m0->m_pkthdr.csum_flags & ~ifp->if_hwassist;
6355         if (sw_csum & CSUM_DELAY_DATA) {
6356                 /*
6357                  * XXX: in_delayed_cksum assumes HBO for ip->ip_len (at least)
6358                  */
6359                 NTOHS(ip->ip_len);
6360                 NTOHS(ip->ip_off);       /* XXX: needed? */
6361                 in_delayed_cksum(m0);
6362                 HTONS(ip->ip_len);
6363                 HTONS(ip->ip_off);
6364                 sw_csum &= ~CSUM_DELAY_DATA;
6365         }
6366         m0->m_pkthdr.csum_flags &= ifp->if_hwassist;
6367
6368         if (ntohs(ip->ip_len) <= ifp->if_mtu ||
6369             (m0->m_pkthdr.csum_flags & ifp->if_hwassist & CSUM_TSO) != 0 ||
6370             (ifp->if_hwassist & CSUM_FRAGMENT &&
6371                 ((ip->ip_off & htons(IP_DF)) == 0))) {
6372                 /*
6373                  * ip->ip_len = htons(ip->ip_len);
6374                  * ip->ip_off = htons(ip->ip_off);
6375                  */
6376                 ip->ip_sum = 0;
6377                 if (sw_csum & CSUM_DELAY_IP) {
6378                         /* From KAME */
6379                         if (ip->ip_v == IPVERSION &&
6380                             (ip->ip_hl << 2) == sizeof(*ip)) {
6381                                 ip->ip_sum = in_cksum_hdr(ip);
6382                         } else {
6383                                 ip->ip_sum = in_cksum(m0, ip->ip_hl << 2);
6384                         }
6385                 }
6386                 PF_UNLOCK();
6387                 error = (*ifp->if_output)(ifp, m0, sintosa(dst), ro);
6388                 PF_LOCK();
6389                 goto done;
6390         }
6391
6392 #else
6393         /* Copied from ip_output. */
6394 #ifdef IPSEC
6395         /*
6396          * If deferred crypto processing is needed, check that the
6397          * interface supports it.
6398          */
6399         if ((mtag = m_tag_find(m0, PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED, NULL))
6400             != NULL && (ifp->if_capabilities & IFCAP_IPSEC) == 0) {
6401                 /* Notify IPsec to do its own crypto. */
6402                 ipsp_skipcrypto_unmark((struct tdb_ident *)(mtag + 1));
6403                 goto bad;
6404         }
6405 #endif /* IPSEC */
6406
6407         /* Catch routing changes wrt. hardware checksumming for TCP or UDP. */
6408         if (m0->m_pkthdr.csum_flags & M_TCPV4_CSUM_OUT) {
6409                 if (!(ifp->if_capabilities & IFCAP_CSUM_TCPv4) ||
6410                     ifp->if_bridge != NULL) {
6411                         in_delayed_cksum(m0);
6412                         m0->m_pkthdr.csum_flags &= ~M_TCPV4_CSUM_OUT; /* Clear */
6413                 }
6414         } else if (m0->m_pkthdr.csum_flags & M_UDPV4_CSUM_OUT) {
6415                 if (!(ifp->if_capabilities & IFCAP_CSUM_UDPv4) ||
6416                     ifp->if_bridge != NULL) {
6417                         in_delayed_cksum(m0);
6418                         m0->m_pkthdr.csum_flags &= ~M_UDPV4_CSUM_OUT; /* Clear */
6419                 }
6420         }
6421
6422         if (ntohs(ip->ip_len) <= ifp->if_mtu) {
6423                 if ((ifp->if_capabilities & IFCAP_CSUM_IPv4) &&
6424                     ifp->if_bridge == NULL) {
6425                         m0->m_pkthdr.csum_flags |= M_IPV4_CSUM_OUT;
6426                         KMOD_IPSTAT_INC(ips_outhwcsum);
6427                 } else {
6428                         ip->ip_sum = 0;
6429                         ip->ip_sum = in_cksum(m0, ip->ip_hl << 2);
6430                 }
6431                 /* Update relevant hardware checksum stats for TCP/UDP */
6432                 if (m0->m_pkthdr.csum_flags & M_TCPV4_CSUM_OUT)
6433                         KMOD_TCPSTAT_INC(tcps_outhwcsum);
6434                 else if (m0->m_pkthdr.csum_flags & M_UDPV4_CSUM_OUT)
6435                         KMOD_UDPSTAT_INC(udps_outhwcsum);
6436                 error = (*ifp->if_output)(ifp, m0, sintosa(dst), NULL);
6437                 goto done;
6438         }
6439 #endif
6440         /*
6441          * Too large for interface; fragment if possible.
6442          * Must be able to put at least 8 bytes per fragment.
6443          */
6444         if (ip->ip_off & htons(IP_DF) || (m0->m_pkthdr.csum_flags & CSUM_TSO)) {
6445                 KMOD_IPSTAT_INC(ips_cantfrag);
6446                 if (r->rt != PF_DUPTO) {
6447 #ifdef __FreeBSD__
6448                         /* icmp_error() expects host byte ordering */
6449                         NTOHS(ip->ip_len);
6450                         NTOHS(ip->ip_off);
6451                         PF_UNLOCK();
6452                         icmp_error(m0, ICMP_UNREACH, ICMP_UNREACH_NEEDFRAG, 0,
6453                             ifp->if_mtu);
6454                         PF_LOCK();
6455 #else
6456                         icmp_error(m0, ICMP_UNREACH, ICMP_UNREACH_NEEDFRAG, 0,
6457                             ifp->if_mtu);
6458 #endif
6459                         goto done;
6460                 } else
6461                         goto bad;
6462         }
6463
6464         m1 = m0;
6465 #ifdef __FreeBSD__
6466         /*
6467          * XXX: is cheaper + less error prone than own function
6468          */
6469         NTOHS(ip->ip_len);
6470         NTOHS(ip->ip_off);
6471         error = ip_fragment(ip, &m0, ifp->if_mtu, ifp->if_hwassist, sw_csum);
6472 #else
6473         error = ip_fragment(m0, ifp, ifp->if_mtu);
6474 #endif
6475         if (error) {
6476 #ifndef __FreeBSD__     /* ip_fragment does not do m_freem() on FreeBSD */
6477                 m0 = NULL;
6478 #endif
6479                 goto bad;
6480         }
6481
6482         for (m0 = m1; m0; m0 = m1) {
6483                 m1 = m0->m_nextpkt;
6484                 m0->m_nextpkt = 0;
6485 #ifdef __FreeBSD__
6486                 if (error == 0) {
6487                         PF_UNLOCK();
6488                         error = (*ifp->if_output)(ifp, m0, sintosa(dst),
6489                             NULL);
6490                         PF_LOCK();
6491                 } else
6492 #else
6493                 if (error == 0)
6494                         error = (*ifp->if_output)(ifp, m0, sintosa(dst),
6495                             NULL);
6496                 else
6497 #endif
6498                         m_freem(m0);
6499         }
6500
6501         if (error == 0)
6502                 KMOD_IPSTAT_INC(ips_fragmented);
6503
6504 done:
6505         if (r->rt != PF_DUPTO)
6506                 *m = NULL;
6507         if (ro == &iproute && ro->ro_rt)
6508                 RTFREE(ro->ro_rt);
6509         return;
6510
6511 bad:
6512         m_freem(m0);
6513         goto done;
6514 }
6515 #endif /* INET */
6516
6517 #ifdef INET6
6518 void
6519 pf_route6(struct mbuf **m, struct pf_rule *r, int dir, struct ifnet *oifp,
6520     struct pf_state *s, struct pf_pdesc *pd)
6521 {
6522         struct mbuf             *m0;
6523         struct route_in6         ip6route;
6524         struct route_in6        *ro;
6525         struct sockaddr_in6     *dst;
6526         struct ip6_hdr          *ip6;
6527         struct ifnet            *ifp = NULL;
6528         struct pf_addr           naddr;
6529         struct pf_src_node      *sn = NULL;
6530         int                      error = 0;
6531
6532         if (m == NULL || *m == NULL || r == NULL ||
6533             (dir != PF_IN && dir != PF_OUT) || oifp == NULL)
6534                 panic("pf_route6: invalid parameters");
6535
6536         if (pd->pf_mtag->routed++ > 3) {
6537                 m0 = *m;
6538                 *m = NULL;
6539                 goto bad;
6540         }
6541
6542         if (r->rt == PF_DUPTO) {
6543 #ifdef __FreeBSD__
6544                 if ((m0 = m_dup(*m, M_DONTWAIT)) == NULL)
6545 #else
6546                 if ((m0 = m_copym2(*m, 0, M_COPYALL, M_NOWAIT)) == NULL)
6547 #endif
6548                         return;
6549         } else {
6550                 if ((r->rt == PF_REPLYTO) == (r->direction == dir))
6551                         return;
6552                 m0 = *m;
6553         }
6554
6555         if (m0->m_len < sizeof(struct ip6_hdr)) {
6556                 DPFPRINTF(PF_DEBUG_URGENT,
6557                     ("pf_route6: m0->m_len < sizeof(struct ip6_hdr)\n"));
6558                 goto bad;
6559         }
6560         ip6 = mtod(m0, struct ip6_hdr *);
6561
6562         ro = &ip6route;
6563         bzero((caddr_t)ro, sizeof(*ro));
6564         dst = (struct sockaddr_in6 *)&ro->ro_dst;
6565         dst->sin6_family = AF_INET6;
6566         dst->sin6_len = sizeof(*dst);
6567         dst->sin6_addr = ip6->ip6_dst;
6568
6569         /* Cheat. XXX why only in the v6 case??? */
6570         if (r->rt == PF_FASTROUTE) {
6571 #ifdef __FreeBSD__
6572                 m0->m_flags |= M_SKIP_FIREWALL;
6573                 PF_UNLOCK();
6574                 ip6_output(m0, NULL, NULL, 0, NULL, NULL, NULL);
6575                 PF_LOCK();
6576 #else
6577                 mtag = m_tag_get(PACKET_TAG_PF_GENERATED, 0, M_NOWAIT);
6578                 if (mtag == NULL)
6579                         goto bad;
6580                 m_tag_prepend(m0, mtag);
6581                 pd->pf_mtag->flags |= PF_TAG_GENERATED;
6582                 ip6_output(m0, NULL, NULL, 0, NULL, NULL);
6583 #endif
6584                 return;
6585         }
6586
6587         if (TAILQ_EMPTY(&r->rpool.list)) {
6588                 DPFPRINTF(PF_DEBUG_URGENT,
6589                     ("pf_route6: TAILQ_EMPTY(&r->rpool.list)\n"));
6590                 goto bad;
6591         }
6592         if (s == NULL) {
6593                 pf_map_addr(AF_INET6, r, (struct pf_addr *)&ip6->ip6_src,
6594                     &naddr, NULL, &sn);
6595                 if (!PF_AZERO(&naddr, AF_INET6))
6596                         PF_ACPY((struct pf_addr *)&dst->sin6_addr,
6597                             &naddr, AF_INET6);
6598                 ifp = r->rpool.cur->kif ? r->rpool.cur->kif->pfik_ifp : NULL;
6599         } else {
6600                 if (!PF_AZERO(&s->rt_addr, AF_INET6))
6601                         PF_ACPY((struct pf_addr *)&dst->sin6_addr,
6602                             &s->rt_addr, AF_INET6);
6603                 ifp = s->rt_kif ? s->rt_kif->pfik_ifp : NULL;
6604         }
6605         if (ifp == NULL)
6606                 goto bad;
6607
6608         if (oifp != ifp) {
6609 #ifdef __FreeBSD__
6610                 PF_UNLOCK();
6611                 if (pf_test6(PF_OUT, ifp, &m0, NULL, NULL) != PF_PASS) {
6612                         PF_LOCK();
6613                         goto bad;
6614                 } else if (m0 == NULL) {
6615                         PF_LOCK();
6616                         goto done;
6617                 }
6618                 PF_LOCK();
6619 #else
6620                 if (pf_test6(PF_OUT, ifp, &m0, NULL) != PF_PASS)
6621                         goto bad;
6622                 else if (m0 == NULL)
6623                         goto done;
6624 #endif
6625                 if (m0->m_len < sizeof(struct ip6_hdr)) {
6626                         DPFPRINTF(PF_DEBUG_URGENT,
6627                             ("pf_route6: m0->m_len < sizeof(struct ip6_hdr)\n"));
6628                         goto bad;
6629                 }
6630                 ip6 = mtod(m0, struct ip6_hdr *);
6631         }
6632
6633         /*
6634          * If the packet is too large for the outgoing interface,
6635          * send back an icmp6 error.
6636          */
6637         if (IN6_IS_SCOPE_EMBED(&dst->sin6_addr))
6638                 dst->sin6_addr.s6_addr16[1] = htons(ifp->if_index);
6639         if ((u_long)m0->m_pkthdr.len <= ifp->if_mtu) {
6640 #ifdef __FreeBSD__
6641                 PF_UNLOCK();
6642 #endif
6643                 error = nd6_output(ifp, ifp, m0, dst, NULL);
6644 #ifdef __FreeBSD__
6645                 PF_LOCK();
6646 #endif
6647         } else {
6648                 in6_ifstat_inc(ifp, ifs6_in_toobig);
6649 #ifdef __FreeBSD__
6650                 if (r->rt != PF_DUPTO) {
6651                         PF_UNLOCK();
6652                         icmp6_error(m0, ICMP6_PACKET_TOO_BIG, 0, ifp->if_mtu);
6653                         PF_LOCK();
6654                  } else
6655 #else
6656                 if (r->rt != PF_DUPTO)
6657                         icmp6_error(m0, ICMP6_PACKET_TOO_BIG, 0, ifp->if_mtu);
6658                 else
6659 #endif
6660                         goto bad;
6661         }
6662
6663 done:
6664         if (r->rt != PF_DUPTO)
6665                 *m = NULL;
6666         return;
6667
6668 bad:
6669         m_freem(m0);
6670         goto done;
6671 }
6672 #endif /* INET6 */
6673
6674
6675 #ifdef __FreeBSD__
6676 /*
6677  * FreeBSD supports cksum offloads for the following drivers.
6678  *  em(4), fxp(4), ixgb(4), lge(4), ndis(4), nge(4), re(4),
6679  *   ti(4), txp(4), xl(4)
6680  *
6681  * CSUM_DATA_VALID | CSUM_PSEUDO_HDR :
6682  *  network driver performed cksum including pseudo header, need to verify
6683  *   csum_data
6684  * CSUM_DATA_VALID :
6685  *  network driver performed cksum, needs to additional pseudo header
6686  *  cksum computation with partial csum_data(i.e. lack of H/W support for
6687  *  pseudo header, for instance hme(4), sk(4) and possibly gem(4))
6688  *
6689  * After validating the cksum of packet, set both flag CSUM_DATA_VALID and
6690  * CSUM_PSEUDO_HDR in order to avoid recomputation of the cksum in upper
6691  * TCP/UDP layer.
6692  * Also, set csum_data to 0xffff to force cksum validation.
6693  */
6694 int
6695 pf_check_proto_cksum(struct mbuf *m, int off, int len, u_int8_t p, sa_family_t af)
6696 {
6697         u_int16_t sum = 0;
6698         int hw_assist = 0;
6699         struct ip *ip;
6700
6701         if (off < sizeof(struct ip) || len < sizeof(struct udphdr))
6702                 return (1);
6703         if (m->m_pkthdr.len < off + len)
6704                 return (1);
6705
6706         switch (p) {
6707         case IPPROTO_TCP:
6708                 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
6709                         if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) {
6710                                 sum = m->m_pkthdr.csum_data;
6711                         } else {
6712                                 ip = mtod(m, struct ip *);      
6713                                 sum = in_pseudo(ip->ip_src.s_addr,
6714                                         ip->ip_dst.s_addr, htonl((u_short)len + 
6715                                         m->m_pkthdr.csum_data + IPPROTO_TCP));
6716                         }
6717                         sum ^= 0xffff;
6718                         ++hw_assist;
6719                 }
6720                 break;
6721         case IPPROTO_UDP:
6722                 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
6723                         if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) {
6724                                 sum = m->m_pkthdr.csum_data;
6725                         } else {
6726                                 ip = mtod(m, struct ip *);      
6727                                 sum = in_pseudo(ip->ip_src.s_addr,
6728                                         ip->ip_dst.s_addr, htonl((u_short)len +
6729                                         m->m_pkthdr.csum_data + IPPROTO_UDP));
6730                         }
6731                         sum ^= 0xffff;
6732                         ++hw_assist;
6733                 }
6734                 break;
6735         case IPPROTO_ICMP:
6736 #ifdef INET6
6737         case IPPROTO_ICMPV6:
6738 #endif /* INET6 */
6739                 break;
6740         default:
6741                 return (1);
6742         }
6743
6744         if (!hw_assist) {
6745                 switch (af) {
6746                 case AF_INET:
6747                         if (p == IPPROTO_ICMP) {
6748                                 if (m->m_len < off)
6749                                         return (1);
6750                                 m->m_data += off;
6751                                 m->m_len -= off;
6752                                 sum = in_cksum(m, len);
6753                                 m->m_data -= off;
6754                                 m->m_len += off;
6755                         } else {
6756                                 if (m->m_len < sizeof(struct ip))
6757                                         return (1);
6758                                 sum = in4_cksum(m, p, off, len);
6759                         }
6760                         break;
6761 #ifdef INET6
6762                 case AF_INET6:
6763                         if (m->m_len < sizeof(struct ip6_hdr))
6764                                 return (1);
6765                         sum = in6_cksum(m, p, off, len);
6766                         break;
6767 #endif /* INET6 */
6768                 default:
6769                         return (1);
6770                 }
6771         }
6772         if (sum) {
6773                 switch (p) {
6774                 case IPPROTO_TCP:
6775                     {
6776                         KMOD_TCPSTAT_INC(tcps_rcvbadsum);
6777                         break;
6778                     }
6779                 case IPPROTO_UDP:
6780                     {
6781                         KMOD_UDPSTAT_INC(udps_badsum);
6782                         break;
6783                     }
6784                 case IPPROTO_ICMP:
6785                     {
6786                         KMOD_ICMPSTAT_INC(icps_checksum);
6787                         break;
6788                     }
6789 #ifdef INET6
6790                 case IPPROTO_ICMPV6:
6791                     {
6792                         KMOD_ICMP6STAT_INC(icp6s_checksum);
6793                         break;
6794                     }
6795 #endif /* INET6 */
6796                 }
6797                 return (1);
6798         } else {
6799                 if (p == IPPROTO_TCP || p == IPPROTO_UDP) {
6800                         m->m_pkthdr.csum_flags |=
6801                             (CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
6802                         m->m_pkthdr.csum_data = 0xffff;
6803                 }
6804         }
6805         return (0);
6806 }
6807 #else /* !__FreeBSD__ */
6808 /*
6809  * check protocol (tcp/udp/icmp/icmp6) checksum and set mbuf flag
6810  *   off is the offset where the protocol header starts
6811  *   len is the total length of protocol header plus payload
6812  * returns 0 when the checksum is valid, otherwise returns 1.
6813  */
6814 int
6815 pf_check_proto_cksum(struct mbuf *m, int off, int len, u_int8_t p,
6816     sa_family_t af)
6817 {
6818         u_int16_t flag_ok, flag_bad;
6819         u_int16_t sum;
6820
6821         switch (p) {
6822         case IPPROTO_TCP:
6823                 flag_ok = M_TCP_CSUM_IN_OK;
6824                 flag_bad = M_TCP_CSUM_IN_BAD;
6825                 break;
6826         case IPPROTO_UDP:
6827                 flag_ok = M_UDP_CSUM_IN_OK;
6828                 flag_bad = M_UDP_CSUM_IN_BAD;
6829                 break;
6830         case IPPROTO_ICMP:
6831 #ifdef INET6
6832         case IPPROTO_ICMPV6:
6833 #endif /* INET6 */
6834                 flag_ok = flag_bad = 0;
6835                 break;
6836         default:
6837                 return (1);
6838         }
6839         if (m->m_pkthdr.csum_flags & flag_ok)
6840                 return (0);
6841         if (m->m_pkthdr.csum_flags & flag_bad)
6842                 return (1);
6843         if (off < sizeof(struct ip) || len < sizeof(struct udphdr))
6844                 return (1);
6845         if (m->m_pkthdr.len < off + len)
6846                 return (1);
6847         switch (af) {
6848 #ifdef INET
6849         case AF_INET:
6850                 if (p == IPPROTO_ICMP) {
6851                         if (m->m_len < off)
6852                                 return (1);
6853                         m->m_data += off;
6854                         m->m_len -= off;
6855                         sum = in_cksum(m, len);
6856                         m->m_data -= off;
6857                         m->m_len += off;
6858                 } else {
6859                         if (m->m_len < sizeof(struct ip))
6860                                 return (1);
6861                         sum = in4_cksum(m, p, off, len);
6862                 }
6863                 break;
6864 #endif /* INET */
6865 #ifdef INET6
6866         case AF_INET6:
6867                 if (m->m_len < sizeof(struct ip6_hdr))
6868                         return (1);
6869                 sum = in6_cksum(m, p, off, len);
6870                 break;
6871 #endif /* INET6 */
6872         default:
6873                 return (1);
6874         }
6875         if (sum) {
6876                 m->m_pkthdr.csum_flags |= flag_bad;
6877                 switch (p) {
6878                 case IPPROTO_TCP:
6879                         KMOD_TCPSTAT_INC(tcps_rcvbadsum);
6880                         break;
6881                 case IPPROTO_UDP:
6882                         KMOD_UDPSTAT_INC(udps_badsum);
6883                         break;
6884                 case IPPROTO_ICMP:
6885                         KMOD_ICMPSTAT_INC(icps_checksum);
6886                         break;
6887 #ifdef INET6
6888                 case IPPROTO_ICMPV6:
6889                         KMOD_ICMP6STAT_INC(icp6s_checksum);
6890                         break;
6891 #endif /* INET6 */
6892                 }
6893                 return (1);
6894         }
6895         m->m_pkthdr.csum_flags |= flag_ok;
6896         return (0);
6897 }
6898 #endif /* __FreeBSD__ */
6899
6900 #ifdef INET
6901 int
6902 #ifdef __FreeBSD__
6903 pf_test(int dir, struct ifnet *ifp, struct mbuf **m0,
6904     struct ether_header *eh, struct inpcb *inp)
6905 #else
6906 pf_test(int dir, struct ifnet *ifp, struct mbuf **m0,
6907     struct ether_header *eh)
6908 #endif
6909 {
6910         struct pfi_kif          *kif;
6911         u_short                  action, reason = 0, log = 0;
6912         struct mbuf             *m = *m0;
6913         struct ip               *h = NULL;      /* make the compiler happy */
6914         struct pf_rule          *a = NULL, *r = &pf_default_rule, *tr, *nr;
6915         struct pf_state         *s = NULL;
6916         struct pf_ruleset       *ruleset = NULL;
6917         struct pf_pdesc          pd;
6918         int                      off, dirndx, pqid = 0;
6919
6920 #ifdef __FreeBSD__
6921         PF_LOCK();
6922 #endif
6923         if (!pf_status.running)
6924 #ifdef __FreeBSD__
6925         {
6926                 PF_UNLOCK();
6927 #endif
6928                 return (PF_PASS);
6929 #ifdef __FreeBSD__
6930         }
6931 #endif
6932
6933         memset(&pd, 0, sizeof(pd));
6934         if ((pd.pf_mtag = pf_get_mtag(m)) == NULL) {
6935 #ifdef __FreeBSD__
6936                 PF_UNLOCK();
6937 #endif
6938                 DPFPRINTF(PF_DEBUG_URGENT,
6939                     ("pf_test: pf_get_mtag returned NULL\n"));
6940                 return (PF_DROP);
6941         }
6942 #ifdef __FreeBSD__
6943         if (m->m_flags & M_SKIP_FIREWALL) {
6944                 PF_UNLOCK();
6945                 return (PF_PASS);
6946         }
6947 #else
6948         if (pd.pf_mtag->flags & PF_TAG_GENERATED)
6949                 return (PF_PASS);
6950 #endif
6951
6952 #ifdef __FreeBSD__
6953         /* XXX_IMPORT: later */
6954 #else
6955         if (ifp->if_type == IFT_CARP && ifp->if_carpdev)
6956                 ifp = ifp->if_carpdev;
6957 #endif
6958
6959         kif = (struct pfi_kif *)ifp->if_pf_kif;
6960         if (kif == NULL) {
6961 #ifdef __FreeBSD__
6962                 PF_UNLOCK();
6963 #endif
6964                 DPFPRINTF(PF_DEBUG_URGENT,
6965                     ("pf_test: kif == NULL, if_xname %s\n", ifp->if_xname));
6966                 return (PF_DROP);
6967         }
6968         if (kif->pfik_flags & PFI_IFLAG_SKIP) {
6969 #ifdef __FreeBSD__
6970                 PF_UNLOCK();
6971 #endif
6972                 return (PF_PASS);
6973         }
6974
6975 #ifdef __FreeBSD__
6976         M_ASSERTPKTHDR(m);
6977 #else
6978 #ifdef DIAGNOSTIC
6979         if ((m->m_flags & M_PKTHDR) == 0)
6980                 panic("non-M_PKTHDR is passed to pf_test");
6981 #endif /* DIAGNOSTIC */
6982 #endif /* __FreeBSD__ */
6983
6984         if (m->m_pkthdr.len < (int)sizeof(*h)) {
6985                 action = PF_DROP;
6986                 REASON_SET(&reason, PFRES_SHORT);
6987                 log = 1;
6988                 goto done;
6989         }
6990
6991         /* We do IP header normalization and packet reassembly here */
6992         if (pf_normalize_ip(m0, dir, kif, &reason, &pd) != PF_PASS) {
6993                 action = PF_DROP;
6994                 goto done;
6995         }
6996         m = *m0;
6997         h = mtod(m, struct ip *);
6998
6999         off = h->ip_hl << 2;
7000         if (off < (int)sizeof(*h)) {
7001                 action = PF_DROP;
7002                 REASON_SET(&reason, PFRES_SHORT);
7003                 log = 1;
7004                 goto done;
7005         }
7006
7007         pd.src = (struct pf_addr *)&h->ip_src;
7008         pd.dst = (struct pf_addr *)&h->ip_dst;
7009         PF_ACPY(&pd.baddr, dir == PF_OUT ? pd.src : pd.dst, AF_INET);
7010         pd.ip_sum = &h->ip_sum;
7011         pd.proto = h->ip_p;
7012         pd.af = AF_INET;
7013         pd.tos = h->ip_tos;
7014         pd.tot_len = ntohs(h->ip_len);
7015         pd.eh = eh;
7016
7017         /* handle fragments that didn't get reassembled by normalization */
7018         if (h->ip_off & htons(IP_MF | IP_OFFMASK)) {
7019                 action = pf_test_fragment(&r, dir, kif, m, h,
7020                     &pd, &a, &ruleset);
7021                 goto done;
7022         }
7023
7024         switch (h->ip_p) {
7025
7026         case IPPROTO_TCP: {
7027                 struct tcphdr   th;
7028
7029                 pd.hdr.tcp = &th;
7030                 if (!pf_pull_hdr(m, off, &th, sizeof(th),
7031                     &action, &reason, AF_INET)) {
7032                         log = action != PF_PASS;
7033                         goto done;
7034                 }
7035                 if (dir == PF_IN && pf_check_proto_cksum(m, off,
7036                     ntohs(h->ip_len) - off, IPPROTO_TCP, AF_INET)) {
7037                         REASON_SET(&reason, PFRES_PROTCKSUM);
7038                         action = PF_DROP;
7039                         goto done;
7040                 }
7041                 pd.p_len = pd.tot_len - off - (th.th_off << 2);
7042                 if ((th.th_flags & TH_ACK) && pd.p_len == 0)
7043                         pqid = 1;
7044                 action = pf_normalize_tcp(dir, kif, m, 0, off, h, &pd);
7045                 if (action == PF_DROP)
7046                         goto done;
7047                 action = pf_test_state_tcp(&s, dir, kif, m, off, h, &pd,
7048                     &reason);
7049                 if (action == PF_PASS) {
7050 #if NPFSYNC
7051                         pfsync_update_state(s);
7052 #endif /* NPFSYNC */
7053                         r = s->rule.ptr;
7054                         a = s->anchor.ptr;
7055                         log = s->log;
7056                 } else if (s == NULL)
7057 #ifdef __FreeBSD__
7058                         action = pf_test_tcp(&r, &s, dir, kif,
7059                             m, off, h, &pd, &a, &ruleset, NULL, inp);
7060 #else
7061                         action = pf_test_tcp(&r, &s, dir, kif,
7062                             m, off, h, &pd, &a, &ruleset, &ipintrq);
7063 #endif
7064                 break;
7065         }
7066
7067         case IPPROTO_UDP: {
7068                 struct udphdr   uh;
7069
7070                 pd.hdr.udp = &uh;
7071                 if (!pf_pull_hdr(m, off, &uh, sizeof(uh),
7072                     &action, &reason, AF_INET)) {
7073                         log = action != PF_PASS;
7074                         goto done;
7075                 }
7076                 if (dir == PF_IN && uh.uh_sum && pf_check_proto_cksum(m,
7077                     off, ntohs(h->ip_len) - off, IPPROTO_UDP, AF_INET)) {
7078                         action = PF_DROP;
7079                         REASON_SET(&reason, PFRES_PROTCKSUM);
7080                         goto done;
7081                 }
7082                 if (uh.uh_dport == 0 ||
7083                     ntohs(uh.uh_ulen) > m->m_pkthdr.len - off ||
7084                     ntohs(uh.uh_ulen) < sizeof(struct udphdr)) {
7085                         action = PF_DROP;
7086                         REASON_SET(&reason, PFRES_SHORT);
7087                         goto done;
7088                 }
7089                 action = pf_test_state_udp(&s, dir, kif, m, off, h, &pd);
7090                 if (action == PF_PASS) {
7091 #if NPFSYNC
7092                         pfsync_update_state(s);
7093 #endif /* NPFSYNC */
7094                         r = s->rule.ptr;
7095                         a = s->anchor.ptr;
7096                         log = s->log;
7097                 } else if (s == NULL)
7098 #ifdef __FreeBSD__
7099                         action = pf_test_udp(&r, &s, dir, kif,
7100                             m, off, h, &pd, &a, &ruleset, NULL, inp);
7101 #else
7102                         action = pf_test_udp(&r, &s, dir, kif,
7103                             m, off, h, &pd, &a, &ruleset, &ipintrq);
7104 #endif
7105                 break;
7106         }
7107
7108         case IPPROTO_ICMP: {
7109                 struct icmp     ih;
7110
7111                 pd.hdr.icmp = &ih;
7112                 if (!pf_pull_hdr(m, off, &ih, ICMP_MINLEN,
7113                     &action, &reason, AF_INET)) {
7114                         log = action != PF_PASS;
7115                         goto done;
7116                 }
7117                 if (dir == PF_IN && pf_check_proto_cksum(m, off,
7118                     ntohs(h->ip_len) - off, IPPROTO_ICMP, AF_INET)) {
7119                         action = PF_DROP;
7120                         REASON_SET(&reason, PFRES_PROTCKSUM);
7121                         goto done;
7122                 }
7123                 action = pf_test_state_icmp(&s, dir, kif, m, off, h, &pd,
7124                     &reason);
7125                 if (action == PF_PASS) {
7126 #if NPFSYNC
7127                         pfsync_update_state(s);
7128 #endif /* NPFSYNC */
7129                         r = s->rule.ptr;
7130                         a = s->anchor.ptr;
7131                         log = s->log;
7132                 } else if (s == NULL)
7133 #ifdef __FreeBSD__
7134                         action = pf_test_icmp(&r, &s, dir, kif,
7135                             m, off, h, &pd, &a, &ruleset, NULL);
7136 #else
7137                         action = pf_test_icmp(&r, &s, dir, kif,
7138                             m, off, h, &pd, &a, &ruleset, &ipintrq);
7139 #endif
7140                 break;
7141         }
7142
7143         default:
7144                 action = pf_test_state_other(&s, dir, kif, &pd);
7145                 if (action == PF_PASS) {
7146 #if NPFSYNC
7147                         pfsync_update_state(s);
7148 #endif /* NPFSYNC */
7149                         r = s->rule.ptr;
7150                         a = s->anchor.ptr;
7151                         log = s->log;
7152                 } else if (s == NULL)
7153 #ifdef __FreeBSD__
7154                         action = pf_test_other(&r, &s, dir, kif, m, off, h,
7155                             &pd, &a, &ruleset, NULL);
7156 #else
7157                         action = pf_test_other(&r, &s, dir, kif, m, off, h,
7158                             &pd, &a, &ruleset, &ipintrq);
7159 #endif
7160                 break;
7161         }
7162
7163 done:
7164         if (action == PF_PASS && h->ip_hl > 5 &&
7165             !((s && s->state_flags & PFSTATE_ALLOWOPTS) || r->allow_opts)) {
7166                 action = PF_DROP;
7167                 REASON_SET(&reason, PFRES_IPOPTIONS);
7168                 log = 1;
7169                 DPFPRINTF(PF_DEBUG_MISC,
7170                     ("pf: dropping packet with ip options\n"));
7171         }
7172
7173         if ((s && s->tag) || r->rtableid)
7174                 pf_tag_packet(m, pd.pf_mtag, s ? s->tag : 0, r->rtableid);
7175
7176 #ifdef ALTQ
7177         if (action == PF_PASS && r->qid) {
7178                 if (pqid || (pd.tos & IPTOS_LOWDELAY))
7179                         pd.pf_mtag->qid = r->pqid;
7180                 else
7181                         pd.pf_mtag->qid = r->qid;
7182                 /* add hints for ecn */
7183                 pd.pf_mtag->af = AF_INET;
7184                 pd.pf_mtag->hdr = h;
7185         }
7186 #endif /* ALTQ */
7187
7188         /*
7189          * connections redirected to loopback should not match sockets
7190          * bound specifically to loopback due to security implications,
7191          * see tcp_input() and in_pcblookup_listen().
7192          */
7193         if (dir == PF_IN && action == PF_PASS && (pd.proto == IPPROTO_TCP ||
7194             pd.proto == IPPROTO_UDP) && s != NULL && s->nat_rule.ptr != NULL &&
7195             (s->nat_rule.ptr->action == PF_RDR ||
7196             s->nat_rule.ptr->action == PF_BINAT) &&
7197             (ntohl(pd.dst->v4.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
7198                 pd.pf_mtag->flags |= PF_TAG_TRANSLATE_LOCALHOST;
7199
7200         if (log) {
7201                 struct pf_rule *lr;
7202
7203                 if (s != NULL && s->nat_rule.ptr != NULL &&
7204                     s->nat_rule.ptr->log & PF_LOG_ALL)
7205                         lr = s->nat_rule.ptr;
7206                 else
7207                         lr = r;
7208                 PFLOG_PACKET(kif, h, m, AF_INET, dir, reason, lr, a, ruleset,
7209                     &pd);
7210         }
7211
7212         kif->pfik_bytes[0][dir == PF_OUT][action != PF_PASS] += pd.tot_len;
7213         kif->pfik_packets[0][dir == PF_OUT][action != PF_PASS]++;
7214
7215         if (action == PF_PASS || r->action == PF_DROP) {
7216                 dirndx = (dir == PF_OUT);
7217                 r->packets[dirndx]++;
7218                 r->bytes[dirndx] += pd.tot_len;
7219                 if (a != NULL) {
7220                         a->packets[dirndx]++;
7221                         a->bytes[dirndx] += pd.tot_len;
7222                 }
7223                 if (s != NULL) {
7224                         if (s->nat_rule.ptr != NULL) {
7225                                 s->nat_rule.ptr->packets[dirndx]++;
7226                                 s->nat_rule.ptr->bytes[dirndx] += pd.tot_len;
7227                         }
7228                         if (s->src_node != NULL) {
7229                                 s->src_node->packets[dirndx]++;
7230                                 s->src_node->bytes[dirndx] += pd.tot_len;
7231                         }
7232                         if (s->nat_src_node != NULL) {
7233                                 s->nat_src_node->packets[dirndx]++;
7234                                 s->nat_src_node->bytes[dirndx] += pd.tot_len;
7235                         }
7236                         dirndx = (dir == s->direction) ? 0 : 1;
7237                         s->packets[dirndx]++;
7238                         s->bytes[dirndx] += pd.tot_len;
7239                 }
7240                 tr = r;
7241                 nr = (s != NULL) ? s->nat_rule.ptr : pd.nat_rule;
7242                 if (nr != NULL) {
7243                         struct pf_addr *x;
7244                         /*
7245                          * XXX: we need to make sure that the addresses
7246                          * passed to pfr_update_stats() are the same than
7247                          * the addresses used during matching (pfr_match)
7248                          */
7249                         if (r == &pf_default_rule) {
7250                                 tr = nr;
7251                                 x = (s == NULL || s->direction == dir) ?
7252                                     &pd.baddr : &pd.naddr;
7253                         } else
7254                                 x = (s == NULL || s->direction == dir) ?
7255                                     &pd.naddr : &pd.baddr;
7256                         if (x == &pd.baddr || s == NULL) {
7257                                 /* we need to change the address */
7258                                 if (dir == PF_OUT)
7259                                         pd.src = x;
7260                                 else
7261                                         pd.dst = x;
7262                         }
7263                 }
7264                 if (tr->src.addr.type == PF_ADDR_TABLE)
7265                         pfr_update_stats(tr->src.addr.p.tbl, (s == NULL ||
7266                             s->direction == dir) ? pd.src : pd.dst, pd.af,
7267                             pd.tot_len, dir == PF_OUT, r->action == PF_PASS,
7268                             tr->src.neg);
7269                 if (tr->dst.addr.type == PF_ADDR_TABLE)
7270                         pfr_update_stats(tr->dst.addr.p.tbl, (s == NULL ||
7271                             s->direction == dir) ? pd.dst : pd.src, pd.af,
7272                             pd.tot_len, dir == PF_OUT, r->action == PF_PASS,
7273                             tr->dst.neg);
7274         }
7275
7276
7277         if (action == PF_SYNPROXY_DROP) {
7278                 m_freem(*m0);
7279                 *m0 = NULL;
7280                 action = PF_PASS;
7281         } else if (r->rt)
7282                 /* pf_route can free the mbuf causing *m0 to become NULL */
7283                 pf_route(m0, r, dir, ifp, s, &pd);
7284
7285 #ifdef __FreeBSD__
7286         PF_UNLOCK();
7287 #endif
7288
7289         return (action);
7290 }
7291 #endif /* INET */
7292
7293 #ifdef INET6
7294 int
7295 #ifdef __FreeBSD__
7296 pf_test6(int dir, struct ifnet *ifp, struct mbuf **m0,
7297     struct ether_header *eh, struct inpcb *inp)
7298 #else
7299 pf_test6(int dir, struct ifnet *ifp, struct mbuf **m0,
7300     struct ether_header *eh)
7301 #endif
7302 {
7303         struct pfi_kif          *kif;
7304         u_short                  action, reason = 0, log = 0;
7305         struct mbuf             *m = *m0, *n = NULL;
7306         struct ip6_hdr          *h;
7307         struct pf_rule          *a = NULL, *r = &pf_default_rule, *tr, *nr;
7308         struct pf_state         *s = NULL;
7309         struct pf_ruleset       *ruleset = NULL;
7310         struct pf_pdesc          pd;
7311         int                      off, terminal = 0, dirndx, rh_cnt = 0;
7312
7313 #ifdef __FreeBSD__
7314         PF_LOCK();
7315 #endif
7316
7317         if (!pf_status.running)
7318 #ifdef __FreeBSD__
7319         {
7320                 PF_UNLOCK();
7321 #endif
7322                 return (PF_PASS);
7323 #ifdef __FreeBSD__
7324         }
7325 #endif
7326
7327         memset(&pd, 0, sizeof(pd));
7328         if ((pd.pf_mtag = pf_get_mtag(m)) == NULL) {
7329 #ifdef __FreeBSD__
7330                 PF_UNLOCK();
7331 #endif
7332                 DPFPRINTF(PF_DEBUG_URGENT,
7333                     ("pf_test6: pf_get_mtag returned NULL\n"));
7334                 return (PF_DROP);
7335         }
7336         if (pd.pf_mtag->flags & PF_TAG_GENERATED)
7337                 return (PF_PASS);
7338
7339 #ifdef __FreeBSD__
7340         /* XXX_IMPORT: later */
7341 #else
7342         if (ifp->if_type == IFT_CARP && ifp->if_carpdev)
7343                 ifp = ifp->if_carpdev;
7344 #endif
7345
7346         kif = (struct pfi_kif *)ifp->if_pf_kif;
7347         if (kif == NULL) {
7348 #ifdef __FreeBSD__
7349                 PF_UNLOCK();
7350 #endif
7351                 DPFPRINTF(PF_DEBUG_URGENT,
7352                     ("pf_test6: kif == NULL, if_xname %s\n", ifp->if_xname));
7353                 return (PF_DROP);
7354         }
7355         if (kif->pfik_flags & PFI_IFLAG_SKIP) {
7356 #ifdef __FreeBSD__
7357                 PF_UNLOCK();
7358 #endif
7359                 return (PF_PASS);
7360         }
7361
7362 #ifdef __FreeBSD__
7363         M_ASSERTPKTHDR(m);
7364 #else
7365 #ifdef DIAGNOSTIC
7366         if ((m->m_flags & M_PKTHDR) == 0)
7367                 panic("non-M_PKTHDR is passed to pf_test6");
7368 #endif /* DIAGNOSTIC */
7369 #endif
7370
7371 #ifdef __FreeBSD__
7372         h = NULL;       /* make the compiler happy */
7373 #endif
7374
7375         if (m->m_pkthdr.len < (int)sizeof(*h)) {
7376                 action = PF_DROP;
7377                 REASON_SET(&reason, PFRES_SHORT);
7378                 log = 1;
7379                 goto done;
7380         }
7381
7382         /* We do IP header normalization and packet reassembly here */
7383         if (pf_normalize_ip6(m0, dir, kif, &reason, &pd) != PF_PASS) {
7384                 action = PF_DROP;
7385                 goto done;
7386         }
7387         m = *m0;
7388         h = mtod(m, struct ip6_hdr *);
7389
7390 #if 1
7391         /*
7392          * we do not support jumbogram yet.  if we keep going, zero ip6_plen
7393          * will do something bad, so drop the packet for now.
7394          */
7395         if (htons(h->ip6_plen) == 0) {
7396                 action = PF_DROP;
7397                 REASON_SET(&reason, PFRES_NORM);        /*XXX*/
7398                 goto done;
7399         }
7400 #endif
7401
7402         pd.src = (struct pf_addr *)&h->ip6_src;
7403         pd.dst = (struct pf_addr *)&h->ip6_dst;
7404         PF_ACPY(&pd.baddr, dir == PF_OUT ? pd.src : pd.dst, AF_INET6);
7405         pd.ip_sum = NULL;
7406         pd.af = AF_INET6;
7407         pd.tos = 0;
7408         pd.tot_len = ntohs(h->ip6_plen) + sizeof(struct ip6_hdr);
7409         pd.eh = eh;
7410
7411         off = ((caddr_t)h - m->m_data) + sizeof(struct ip6_hdr);
7412         pd.proto = h->ip6_nxt;
7413         do {
7414                 switch (pd.proto) {
7415                 case IPPROTO_FRAGMENT:
7416                         action = pf_test_fragment(&r, dir, kif, m, h,
7417                             &pd, &a, &ruleset);
7418                         if (action == PF_DROP)
7419                                 REASON_SET(&reason, PFRES_FRAG);
7420                         goto done;
7421                 case IPPROTO_ROUTING: {
7422                         struct ip6_rthdr rthdr;
7423
7424                         if (rh_cnt++) {
7425                                 DPFPRINTF(PF_DEBUG_MISC,
7426                                     ("pf: IPv6 more than one rthdr\n"));
7427                                 action = PF_DROP;
7428                                 REASON_SET(&reason, PFRES_IPOPTIONS);
7429                                 log = 1;
7430                                 goto done;
7431                         }
7432                         if (!pf_pull_hdr(m, off, &rthdr, sizeof(rthdr), NULL,
7433                             &reason, pd.af)) {
7434                                 DPFPRINTF(PF_DEBUG_MISC,
7435                                     ("pf: IPv6 short rthdr\n"));
7436                                 action = PF_DROP;
7437                                 REASON_SET(&reason, PFRES_SHORT);
7438                                 log = 1;
7439                                 goto done;
7440                         }
7441                         if (rthdr.ip6r_type == IPV6_RTHDR_TYPE_0) {
7442                                 DPFPRINTF(PF_DEBUG_MISC,
7443                                     ("pf: IPv6 rthdr0\n"));
7444                                 action = PF_DROP;
7445                                 REASON_SET(&reason, PFRES_IPOPTIONS);
7446                                 log = 1;
7447                                 goto done;
7448                         }
7449                         /* fallthrough */
7450                 }
7451                 case IPPROTO_AH:
7452                 case IPPROTO_HOPOPTS:
7453                 case IPPROTO_DSTOPTS: {
7454                         /* get next header and header length */
7455                         struct ip6_ext  opt6;
7456
7457                         if (!pf_pull_hdr(m, off, &opt6, sizeof(opt6),
7458                             NULL, &reason, pd.af)) {
7459                                 DPFPRINTF(PF_DEBUG_MISC,
7460                                     ("pf: IPv6 short opt\n"));
7461                                 action = PF_DROP;
7462                                 log = 1;
7463                                 goto done;
7464                         }
7465                         if (pd.proto == IPPROTO_AH)
7466                                 off += (opt6.ip6e_len + 2) * 4;
7467                         else
7468                                 off += (opt6.ip6e_len + 1) * 8;
7469                         pd.proto = opt6.ip6e_nxt;
7470                         /* goto the next header */
7471                         break;
7472                 }
7473                 default:
7474                         terminal++;
7475                         break;
7476                 }
7477         } while (!terminal);
7478
7479         /* if there's no routing header, use unmodified mbuf for checksumming */
7480         if (!n)
7481                 n = m;
7482
7483         switch (pd.proto) {
7484
7485         case IPPROTO_TCP: {
7486                 struct tcphdr   th;
7487
7488                 pd.hdr.tcp = &th;
7489                 if (!pf_pull_hdr(m, off, &th, sizeof(th),
7490                     &action, &reason, AF_INET6)) {
7491                         log = action != PF_PASS;
7492                         goto done;
7493                 }
7494                 if (dir == PF_IN && pf_check_proto_cksum(n, off,
7495                     ntohs(h->ip6_plen) - (off - sizeof(struct ip6_hdr)),
7496                     IPPROTO_TCP, AF_INET6)) {
7497                         action = PF_DROP;
7498                         REASON_SET(&reason, PFRES_PROTCKSUM);
7499                         goto done;
7500                 }
7501                 pd.p_len = pd.tot_len - off - (th.th_off << 2);
7502                 action = pf_normalize_tcp(dir, kif, m, 0, off, h, &pd);
7503                 if (action == PF_DROP)
7504                         goto done;
7505                 action = pf_test_state_tcp(&s, dir, kif, m, off, h, &pd,
7506                     &reason);
7507                 if (action == PF_PASS) {
7508 #if NPFSYNC
7509                         pfsync_update_state(s);
7510 #endif /* NPFSYNC */
7511                         r = s->rule.ptr;
7512                         a = s->anchor.ptr;
7513                         log = s->log;
7514                 } else if (s == NULL)
7515 #ifdef __FreeBSD__
7516                         action = pf_test_tcp(&r, &s, dir, kif,
7517                             m, off, h, &pd, &a, &ruleset, NULL, inp);
7518 #else
7519                         action = pf_test_tcp(&r, &s, dir, kif,
7520                             m, off, h, &pd, &a, &ruleset, &ip6intrq);
7521 #endif
7522                 break;
7523         }
7524
7525         case IPPROTO_UDP: {
7526                 struct udphdr   uh;
7527
7528                 pd.hdr.udp = &uh;
7529                 if (!pf_pull_hdr(m, off, &uh, sizeof(uh),
7530                     &action, &reason, AF_INET6)) {
7531                         log = action != PF_PASS;
7532                         goto done;
7533                 }
7534                 if (dir == PF_IN && uh.uh_sum && pf_check_proto_cksum(n,
7535                     off, ntohs(h->ip6_plen) - (off - sizeof(struct ip6_hdr)),
7536                     IPPROTO_UDP, AF_INET6)) {
7537                         action = PF_DROP;
7538                         REASON_SET(&reason, PFRES_PROTCKSUM);
7539                         goto done;
7540                 }
7541                 if (uh.uh_dport == 0 ||
7542                     ntohs(uh.uh_ulen) > m->m_pkthdr.len - off ||
7543                     ntohs(uh.uh_ulen) < sizeof(struct udphdr)) {
7544                         action = PF_DROP;
7545                         REASON_SET(&reason, PFRES_SHORT);
7546                         goto done;
7547                 }
7548                 action = pf_test_state_udp(&s, dir, kif, m, off, h, &pd);
7549                 if (action == PF_PASS) {
7550 #if NPFSYNC
7551                         pfsync_update_state(s);
7552 #endif /* NPFSYNC */
7553                         r = s->rule.ptr;
7554                         a = s->anchor.ptr;
7555                         log = s->log;
7556                 } else if (s == NULL)
7557 #ifdef __FreeBSD__
7558                         action = pf_test_udp(&r, &s, dir, kif,
7559                             m, off, h, &pd, &a, &ruleset, NULL, inp);
7560 #else
7561                         action = pf_test_udp(&r, &s, dir, kif,
7562                             m, off, h, &pd, &a, &ruleset, &ip6intrq);
7563 #endif
7564                 break;
7565         }
7566
7567         case IPPROTO_ICMPV6: {
7568                 struct icmp6_hdr        ih;
7569
7570                 pd.hdr.icmp6 = &ih;
7571                 if (!pf_pull_hdr(m, off, &ih, sizeof(ih),
7572                     &action, &reason, AF_INET6)) {
7573                         log = action != PF_PASS;
7574                         goto done;
7575                 }
7576                 if (dir == PF_IN && pf_check_proto_cksum(n, off,
7577                     ntohs(h->ip6_plen) - (off - sizeof(struct ip6_hdr)),
7578                     IPPROTO_ICMPV6, AF_INET6)) {
7579                         action = PF_DROP;
7580                         REASON_SET(&reason, PFRES_PROTCKSUM);
7581                         goto done;
7582                 }
7583                 action = pf_test_state_icmp(&s, dir, kif,
7584                     m, off, h, &pd, &reason);
7585                 if (action == PF_PASS) {
7586 #if NPFSYNC
7587                         pfsync_update_state(s);
7588 #endif /* NPFSYNC */
7589                         r = s->rule.ptr;
7590                         a = s->anchor.ptr;
7591                         log = s->log;
7592                 } else if (s == NULL)
7593 #ifdef __FreeBSD__
7594                         action = pf_test_icmp(&r, &s, dir, kif,
7595                             m, off, h, &pd, &a, &ruleset, NULL);
7596 #else
7597                         action = pf_test_icmp(&r, &s, dir, kif,
7598                             m, off, h, &pd, &a, &ruleset, &ip6intrq);
7599 #endif
7600                 break;
7601         }
7602
7603         default:
7604                 action = pf_test_state_other(&s, dir, kif, &pd);
7605                 if (action == PF_PASS) {
7606 #if NPFSYNC
7607                         pfsync_update_state(s);
7608 #endif /* NPFSYNC */
7609                         r = s->rule.ptr;
7610                         a = s->anchor.ptr;
7611                         log = s->log;
7612                 } else if (s == NULL)
7613 #ifdef __FreeBSD__
7614                         action = pf_test_other(&r, &s, dir, kif, m, off, h,
7615                             &pd, &a, &ruleset, NULL);
7616 #else
7617                         action = pf_test_other(&r, &s, dir, kif, m, off, h,
7618                             &pd, &a, &ruleset, &ip6intrq);
7619 #endif
7620                 break;
7621         }
7622
7623 done:
7624         /* handle dangerous IPv6 extension headers. */
7625         if (action == PF_PASS && rh_cnt &&
7626             !((s && s->state_flags & PFSTATE_ALLOWOPTS) || r->allow_opts)) {
7627                 action = PF_DROP;
7628                 REASON_SET(&reason, PFRES_IPOPTIONS);
7629                 log = 1;
7630                 DPFPRINTF(PF_DEBUG_MISC,
7631                     ("pf: dropping packet with dangerous v6 headers\n"));
7632         }
7633
7634         if ((s && s->tag) || r->rtableid)
7635                 pf_tag_packet(m, pd.pf_mtag, s ? s->tag : 0, r->rtableid);
7636
7637 #ifdef ALTQ
7638         if (action == PF_PASS && r->qid) {
7639                 if (pd.tos & IPTOS_LOWDELAY)
7640                         pd.pf_mtag->qid = r->pqid;
7641                 else
7642                         pd.pf_mtag->qid = r->qid;
7643                 /* add hints for ecn */
7644                 pd.pf_mtag->af = AF_INET6;
7645                 pd.pf_mtag->hdr = h;
7646         }
7647 #endif /* ALTQ */
7648
7649         if (dir == PF_IN && action == PF_PASS && (pd.proto == IPPROTO_TCP ||
7650             pd.proto == IPPROTO_UDP) && s != NULL && s->nat_rule.ptr != NULL &&
7651             (s->nat_rule.ptr->action == PF_RDR ||
7652             s->nat_rule.ptr->action == PF_BINAT) &&
7653             IN6_IS_ADDR_LOOPBACK(&pd.dst->v6))
7654                 pd.pf_mtag->flags |= PF_TAG_TRANSLATE_LOCALHOST;
7655
7656         if (log) {
7657                 struct pf_rule *lr;
7658
7659                 if (s != NULL && s->nat_rule.ptr != NULL &&
7660                     s->nat_rule.ptr->log & PF_LOG_ALL)
7661                         lr = s->nat_rule.ptr;
7662                 else
7663                         lr = r;
7664                 PFLOG_PACKET(kif, h, m, AF_INET6, dir, reason, lr, a, ruleset,
7665                     &pd);
7666         }
7667
7668         kif->pfik_bytes[1][dir == PF_OUT][action != PF_PASS] += pd.tot_len;
7669         kif->pfik_packets[1][dir == PF_OUT][action != PF_PASS]++;
7670
7671         if (action == PF_PASS || r->action == PF_DROP) {
7672                 dirndx = (dir == PF_OUT);
7673                 r->packets[dirndx]++;
7674                 r->bytes[dirndx] += pd.tot_len;
7675                 if (a != NULL) {
7676                         a->packets[dirndx]++;
7677                         a->bytes[dirndx] += pd.tot_len;
7678                 }
7679                 if (s != NULL) {
7680                         if (s->nat_rule.ptr != NULL) {
7681                                 s->nat_rule.ptr->packets[dirndx]++;
7682                                 s->nat_rule.ptr->bytes[dirndx] += pd.tot_len;
7683                         }
7684                         if (s->src_node != NULL) {
7685                                 s->src_node->packets[dirndx]++;
7686                                 s->src_node->bytes[dirndx] += pd.tot_len;
7687                         }
7688                         if (s->nat_src_node != NULL) {
7689                                 s->nat_src_node->packets[dirndx]++;
7690                                 s->nat_src_node->bytes[dirndx] += pd.tot_len;
7691                         }
7692                         dirndx = (dir == s->direction) ? 0 : 1;
7693                         s->packets[dirndx]++;
7694                         s->bytes[dirndx] += pd.tot_len;
7695                 }
7696                 tr = r;
7697                 nr = (s != NULL) ? s->nat_rule.ptr : pd.nat_rule;
7698                 if (nr != NULL) {
7699                         struct pf_addr *x;
7700                         /*
7701                          * XXX: we need to make sure that the addresses
7702                          * passed to pfr_update_stats() are the same than
7703                          * the addresses used during matching (pfr_match)
7704                          */
7705                         if (r == &pf_default_rule) {
7706                                 tr = nr;
7707                                 x = (s == NULL || s->direction == dir) ?
7708                                     &pd.baddr : &pd.naddr;
7709                         } else {
7710                                 x = (s == NULL || s->direction == dir) ?
7711                                     &pd.naddr : &pd.baddr;
7712                         }
7713                         if (x == &pd.baddr || s == NULL) {
7714                                 if (dir == PF_OUT)
7715                                         pd.src = x;
7716                                 else
7717                                         pd.dst = x;
7718                         }
7719                 }
7720                 if (tr->src.addr.type == PF_ADDR_TABLE)
7721                         pfr_update_stats(tr->src.addr.p.tbl, (s == NULL ||
7722                             s->direction == dir) ? pd.src : pd.dst, pd.af,
7723                             pd.tot_len, dir == PF_OUT, r->action == PF_PASS,
7724                             tr->src.neg);
7725                 if (tr->dst.addr.type == PF_ADDR_TABLE)
7726                         pfr_update_stats(tr->dst.addr.p.tbl, (s == NULL ||
7727                             s->direction == dir) ? pd.dst : pd.src, pd.af,
7728                             pd.tot_len, dir == PF_OUT, r->action == PF_PASS,
7729                             tr->dst.neg);
7730         }
7731
7732
7733         if (action == PF_SYNPROXY_DROP) {
7734                 m_freem(*m0);
7735                 *m0 = NULL;
7736                 action = PF_PASS;
7737         } else if (r->rt)
7738                 /* pf_route6 can free the mbuf causing *m0 to become NULL */
7739                 pf_route6(m0, r, dir, ifp, s, &pd);
7740
7741 #ifdef __FreeBSD__
7742         PF_UNLOCK();
7743 #endif
7744         return (action);
7745 }
7746 #endif /* INET6 */
7747
7748 int
7749 pf_check_congestion(struct ifqueue *ifq)
7750 {
7751 #ifdef __FreeBSD__
7752         /* XXX_IMPORT: later */
7753         return (0);
7754 #else
7755         if (ifq->ifq_congestion)
7756                 return (1);
7757         else
7758                 return (0);
7759 #endif
7760 }