]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/contrib/pf/net/pf_ioctl.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / sys / contrib / pf / net / pf_ioctl.c
1 /*      $OpenBSD: pf_ioctl.c,v 1.175 2007/02/26 22:47:43 deraadt Exp $ */
2
3 /*
4  * Copyright (c) 2001 Daniel Hartmeier
5  * Copyright (c) 2002,2003 Henning Brauer
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  *    - Redistributions of source code must retain the above copyright
13  *      notice, this list of conditions and the following disclaimer.
14  *    - Redistributions in binary form must reproduce the above
15  *      copyright notice, this list of conditions and the following
16  *      disclaimer in the documentation and/or other materials provided
17  *      with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  * Effort sponsored in part by the Defense Advanced Research Projects
33  * Agency (DARPA) and Air Force Research Laboratory, Air Force
34  * Materiel Command, USAF, under agreement number F30602-01-2-0537.
35  *
36  */
37
38 #ifdef __FreeBSD__
39 #include "opt_inet.h"
40 #include "opt_inet6.h"
41
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
44 #endif
45
46 #ifdef __FreeBSD__
47 #include "opt_bpf.h"
48 #include "opt_pf.h"
49
50 #ifdef DEV_BPF
51 #define NBPFILTER       DEV_BPF
52 #else
53 #define NBPFILTER       0
54 #endif
55
56 #ifdef DEV_PFLOG
57 #define NPFLOG          DEV_PFLOG
58 #else
59 #define NPFLOG          0
60 #endif
61
62 #ifdef DEV_PFSYNC
63 #define NPFSYNC         DEV_PFSYNC
64 #else
65 #define NPFSYNC         0
66 #endif
67
68 #else
69 #include "bpfilter.h"
70 #include "pflog.h"
71 #include "pfsync.h"
72 #endif
73
74 #include <sys/param.h>
75 #include <sys/systm.h>
76 #include <sys/mbuf.h>
77 #include <sys/filio.h>
78 #include <sys/fcntl.h>
79 #include <sys/socket.h>
80 #include <sys/socketvar.h>
81 #include <sys/kernel.h>
82 #include <sys/time.h>
83 #include <sys/malloc.h>
84 #ifdef __FreeBSD__
85 #include <sys/module.h>
86 #include <sys/conf.h>
87 #include <sys/proc.h>
88 #include <sys/sysctl.h>
89 #else
90 #include <sys/timeout.h>
91 #include <sys/pool.h>
92 #endif
93 #include <sys/proc.h>
94 #include <sys/malloc.h>
95 #include <sys/kthread.h>
96 #ifndef __FreeBSD__
97 #include <sys/rwlock.h>
98 #include <uvm/uvm_extern.h>
99 #endif
100
101 #include <net/if.h>
102 #include <net/if_types.h>
103 #include <net/route.h>
104
105 #include <netinet/in.h>
106 #include <netinet/in_var.h>
107 #include <netinet/in_systm.h>
108 #include <netinet/ip.h>
109 #include <netinet/ip_var.h>
110 #include <netinet/ip_icmp.h>
111
112 #ifdef __FreeBSD__
113 #include <sys/md5.h>
114 #else
115 #include <dev/rndvar.h>
116 #include <crypto/md5.h>
117 #endif
118 #include <net/pfvar.h>
119
120 #if NPFSYNC > 0
121 #include <net/if_pfsync.h>
122 #endif /* NPFSYNC > 0 */
123
124 #include <net/if_pflog.h>
125
126 #ifdef INET6
127 #include <netinet/ip6.h>
128 #include <netinet/in_pcb.h>
129 #endif /* INET6 */
130
131 #ifdef ALTQ
132 #include <altq/altq.h>
133 #endif
134
135 #ifdef __FreeBSD__
136 #include <sys/limits.h>
137 #include <sys/lock.h>
138 #include <sys/mutex.h>
139 #include <net/pfil.h>
140 #endif /* __FreeBSD__ */
141
142 #ifdef __FreeBSD__
143 void                     init_zone_var(void);
144 void                     cleanup_pf_zone(void);
145 int                      pfattach(void);
146 #else
147 void                     pfattach(int);
148 void                     pf_thread_create(void *);
149 int                      pfopen(dev_t, int, int, struct proc *);
150 int                      pfclose(dev_t, int, int, struct proc *);
151 #endif
152 struct pf_pool          *pf_get_pool(char *, u_int32_t, u_int8_t, u_int32_t,
153                             u_int8_t, u_int8_t, u_int8_t);
154
155 void                     pf_mv_pool(struct pf_palist *, struct pf_palist *);
156 void                     pf_empty_pool(struct pf_palist *);
157 #ifdef __FreeBSD__
158 int                      pfioctl(struct cdev *, u_long, caddr_t, int, struct thread *);
159 #else
160 int                      pfioctl(struct cdev *, u_long, caddr_t, int, struct proc *);
161 #endif
162 #ifdef ALTQ
163 int                      pf_begin_altq(u_int32_t *);
164 int                      pf_rollback_altq(u_int32_t);
165 int                      pf_commit_altq(u_int32_t);
166 int                      pf_enable_altq(struct pf_altq *);
167 int                      pf_disable_altq(struct pf_altq *);
168 #endif /* ALTQ */
169 int                      pf_begin_rules(u_int32_t *, int, const char *);
170 int                      pf_rollback_rules(u_int32_t, int, char *);
171 int                      pf_setup_pfsync_matching(struct pf_ruleset *);
172 void                     pf_hash_rule(MD5_CTX *, struct pf_rule *);
173 void                     pf_hash_rule_addr(MD5_CTX *, struct pf_rule_addr *);
174 int                      pf_commit_rules(u_int32_t, int, char *);
175
176 struct pf_rule           pf_default_rule;
177 #ifdef __FreeBSD__
178 struct sx                pf_consistency_lock;
179 SX_SYSINIT(pf_consistency_lock, &pf_consistency_lock, "pf_statetbl_lock");
180 #else
181 struct rwlock            pf_consistency_lock = RWLOCK_INITIALIZER;
182 #endif
183 #ifdef ALTQ
184 static int               pf_altq_running;
185 #endif
186
187 #define TAGID_MAX        50000
188 TAILQ_HEAD(pf_tags, pf_tagname) pf_tags = TAILQ_HEAD_INITIALIZER(pf_tags),
189                                 pf_qids = TAILQ_HEAD_INITIALIZER(pf_qids);
190
191 #if (PF_QNAME_SIZE != PF_TAG_NAME_SIZE)
192 #error PF_QNAME_SIZE must be equal to PF_TAG_NAME_SIZE
193 #endif
194 u_int16_t                tagname2tag(struct pf_tags *, char *);
195 void                     tag2tagname(struct pf_tags *, u_int16_t, char *);
196 void                     tag_unref(struct pf_tags *, u_int16_t);
197 int                      pf_rtlabel_add(struct pf_addr_wrap *);
198 void                     pf_rtlabel_remove(struct pf_addr_wrap *);
199 void                     pf_rtlabel_copyout(struct pf_addr_wrap *);
200
201 #define DPFPRINTF(n, x) if (pf_status.debug >= (n)) printf x
202
203
204 #ifdef __FreeBSD__
205 static struct cdev      *pf_dev;
206
207 /*
208  * XXX - These are new and need to be checked when moveing to a new version
209  */
210 static void              pf_clear_states(void);
211 static int               pf_clear_tables(void);
212 static void              pf_clear_srcnodes(void);
213 /*
214  * XXX - These are new and need to be checked when moveing to a new version
215  */
216  
217 /*
218  * Wrapper functions for pfil(9) hooks
219  */
220 static int pf_check_in(void *arg, struct mbuf **m, struct ifnet *ifp,
221                 int dir, struct inpcb *inp);
222 static int pf_check_out(void *arg, struct mbuf **m, struct ifnet *ifp,
223                 int dir, struct inpcb *inp);
224 #ifdef INET6
225 static int pf_check6_in(void *arg, struct mbuf **m, struct ifnet *ifp,
226                 int dir, struct inpcb *inp);
227 static int pf_check6_out(void *arg, struct mbuf **m, struct ifnet *ifp,
228                 int dir, struct inpcb *inp);
229 #endif
230
231 static int               hook_pf(void);
232 static int               dehook_pf(void);
233 static int               shutdown_pf(void);
234 static int               pf_load(void);
235 static int               pf_unload(void);
236
237 static struct cdevsw pf_cdevsw = {
238         .d_ioctl =      pfioctl,
239         .d_name =       PF_NAME,
240         .d_version =    D_VERSION,
241 };
242
243 static volatile int pf_pfil_hooked = 0;
244 int pf_end_threads = 0;
245 struct mtx pf_task_mtx;
246 pflog_packet_t *pflog_packet_ptr = NULL;
247
248 int debug_pfugidhack = 0;
249 SYSCTL_INT(_debug, OID_AUTO, pfugidhack, CTLFLAG_RW, &debug_pfugidhack, 0,
250     "Enable/disable pf user/group rules mpsafe hack");
251
252 void
253 init_pf_mutex(void)
254 {
255         mtx_init(&pf_task_mtx, "pf task mtx", NULL, MTX_DEF);
256 }
257
258 void
259 destroy_pf_mutex(void)
260 {
261         mtx_destroy(&pf_task_mtx);
262 }
263
264 void
265 init_zone_var(void)
266 {
267         pf_src_tree_pl = pf_rule_pl = NULL;
268         pf_state_pl = pf_altq_pl = pf_pooladdr_pl = NULL;
269         pf_frent_pl = pf_frag_pl = pf_cache_pl = pf_cent_pl = NULL;
270         pf_state_scrub_pl = NULL;
271         pfr_ktable_pl = pfr_kentry_pl = NULL;
272 }
273
274 void
275 cleanup_pf_zone(void)
276 {
277         UMA_DESTROY(pf_src_tree_pl);
278         UMA_DESTROY(pf_rule_pl);
279         UMA_DESTROY(pf_state_pl);
280         UMA_DESTROY(pf_altq_pl);
281         UMA_DESTROY(pf_pooladdr_pl);
282         UMA_DESTROY(pf_frent_pl);
283         UMA_DESTROY(pf_frag_pl);
284         UMA_DESTROY(pf_cache_pl);
285         UMA_DESTROY(pf_cent_pl);
286         UMA_DESTROY(pfr_ktable_pl);
287         UMA_DESTROY(pfr_kentry_pl2);
288         UMA_DESTROY(pfr_kentry_pl);
289         UMA_DESTROY(pf_state_scrub_pl);
290         UMA_DESTROY(pfi_addr_pl);
291 }
292
293 int
294 pfattach(void)
295 {
296         u_int32_t *my_timeout = pf_default_rule.timeout;
297         int error = 1;
298
299         do {
300                 UMA_CREATE(pf_src_tree_pl,struct pf_src_node, "pfsrctrpl");
301                 UMA_CREATE(pf_rule_pl,    struct pf_rule, "pfrulepl");
302                 UMA_CREATE(pf_state_pl,   struct pf_state, "pfstatepl");
303                 UMA_CREATE(pf_altq_pl,    struct pf_altq, "pfaltqpl");
304                 UMA_CREATE(pf_pooladdr_pl, struct pf_pooladdr, "pfpooladdrpl");
305                 UMA_CREATE(pfr_ktable_pl,  struct pfr_ktable, "pfrktable");
306                 UMA_CREATE(pfr_kentry_pl,  struct pfr_kentry, "pfrkentry");
307                 UMA_CREATE(pfr_kentry_pl2,  struct pfr_kentry, "pfrkentry2");
308                 UMA_CREATE(pf_frent_pl,   struct pf_frent, "pffrent");
309                 UMA_CREATE(pf_frag_pl,    struct pf_fragment, "pffrag");
310                 UMA_CREATE(pf_cache_pl,   struct pf_fragment, "pffrcache");
311                 UMA_CREATE(pf_cent_pl,    struct pf_frcache, "pffrcent");
312                 UMA_CREATE(pf_state_scrub_pl, struct pf_state_scrub, 
313                     "pfstatescrub");
314                 UMA_CREATE(pfi_addr_pl, struct pfi_dynaddr, "pfiaddrpl");
315                 error = 0;
316         } while(0);
317         if (error) {
318                 cleanup_pf_zone();
319                 return (error);
320         }
321         pfr_initialize();
322         pfi_initialize();
323         if ( (error = pf_osfp_initialize()) ) {
324                 cleanup_pf_zone();
325                 pf_osfp_cleanup();
326                 return (error);
327         }
328
329         pf_pool_limits[PF_LIMIT_STATES].pp = pf_state_pl;
330         pf_pool_limits[PF_LIMIT_STATES].limit = PFSTATE_HIWAT;
331         pf_pool_limits[PF_LIMIT_SRC_NODES].pp = pf_src_tree_pl;
332         pf_pool_limits[PF_LIMIT_SRC_NODES].limit = PFSNODE_HIWAT;
333         pf_pool_limits[PF_LIMIT_FRAGS].pp = pf_frent_pl;
334         pf_pool_limits[PF_LIMIT_FRAGS].limit = PFFRAG_FRENT_HIWAT;
335         pf_pool_limits[PF_LIMIT_TABLES].pp = pfr_ktable_pl;
336         pf_pool_limits[PF_LIMIT_TABLES].limit = PFR_KTABLE_HIWAT;
337         pf_pool_limits[PF_LIMIT_TABLE_ENTRIES].pp = pfr_kentry_pl;
338         pf_pool_limits[PF_LIMIT_TABLE_ENTRIES].limit = PFR_KENTRY_HIWAT;
339         uma_zone_set_max(pf_pool_limits[PF_LIMIT_STATES].pp,
340                 pf_pool_limits[PF_LIMIT_STATES].limit);
341
342         RB_INIT(&tree_src_tracking);
343         RB_INIT(&pf_anchors);
344         pf_init_ruleset(&pf_main_ruleset);
345         TAILQ_INIT(&pf_altqs[0]);
346         TAILQ_INIT(&pf_altqs[1]);
347         TAILQ_INIT(&pf_pabuf);
348         pf_altqs_active = &pf_altqs[0];
349         pf_altqs_inactive = &pf_altqs[1];
350         TAILQ_INIT(&state_list);
351
352         /* default rule should never be garbage collected */
353         pf_default_rule.entries.tqe_prev = &pf_default_rule.entries.tqe_next;
354         pf_default_rule.action = PF_PASS;
355         pf_default_rule.nr = -1;
356         pf_default_rule.rtableid = -1;
357
358         /* initialize default timeouts */
359         my_timeout[PFTM_TCP_FIRST_PACKET] = PFTM_TCP_FIRST_PACKET_VAL;
360         my_timeout[PFTM_TCP_OPENING] = PFTM_TCP_OPENING_VAL;
361         my_timeout[PFTM_TCP_ESTABLISHED] = PFTM_TCP_ESTABLISHED_VAL;
362         my_timeout[PFTM_TCP_CLOSING] = PFTM_TCP_CLOSING_VAL;
363         my_timeout[PFTM_TCP_FIN_WAIT] = PFTM_TCP_FIN_WAIT_VAL;
364         my_timeout[PFTM_TCP_CLOSED] = PFTM_TCP_CLOSED_VAL;
365         my_timeout[PFTM_UDP_FIRST_PACKET] = PFTM_UDP_FIRST_PACKET_VAL;
366         my_timeout[PFTM_UDP_SINGLE] = PFTM_UDP_SINGLE_VAL;
367         my_timeout[PFTM_UDP_MULTIPLE] = PFTM_UDP_MULTIPLE_VAL;
368         my_timeout[PFTM_ICMP_FIRST_PACKET] = PFTM_ICMP_FIRST_PACKET_VAL;
369         my_timeout[PFTM_ICMP_ERROR_REPLY] = PFTM_ICMP_ERROR_REPLY_VAL;
370         my_timeout[PFTM_OTHER_FIRST_PACKET] = PFTM_OTHER_FIRST_PACKET_VAL;
371         my_timeout[PFTM_OTHER_SINGLE] = PFTM_OTHER_SINGLE_VAL;
372         my_timeout[PFTM_OTHER_MULTIPLE] = PFTM_OTHER_MULTIPLE_VAL;
373         my_timeout[PFTM_FRAG] = PFTM_FRAG_VAL;
374         my_timeout[PFTM_INTERVAL] = PFTM_INTERVAL_VAL;
375         my_timeout[PFTM_SRC_NODE] = PFTM_SRC_NODE_VAL;
376         my_timeout[PFTM_TS_DIFF] = PFTM_TS_DIFF_VAL;
377         my_timeout[PFTM_ADAPTIVE_START] = PFSTATE_ADAPT_START;
378         my_timeout[PFTM_ADAPTIVE_END] = PFSTATE_ADAPT_END;
379
380         pf_normalize_init();
381         bzero(&pf_status, sizeof(pf_status));
382         pf_status.debug = PF_DEBUG_URGENT;
383
384         pf_pfil_hooked = 0;
385
386         /* XXX do our best to avoid a conflict */
387         pf_status.hostid = arc4random();
388
389         if (kthread_create(pf_purge_thread, NULL, NULL, 0, 0, "pfpurge"))
390                 return (ENXIO);
391
392         return (error);
393 }
394 #else /* !__FreeBSD__ */
395 void
396 pfattach(int num)
397 {
398         u_int32_t *timeout = pf_default_rule.timeout;
399
400         pool_init(&pf_rule_pl, sizeof(struct pf_rule), 0, 0, 0, "pfrulepl",
401             &pool_allocator_nointr);
402         pool_init(&pf_src_tree_pl, sizeof(struct pf_src_node), 0, 0, 0,
403             "pfsrctrpl", NULL);
404         pool_init(&pf_state_pl, sizeof(struct pf_state), 0, 0, 0, "pfstatepl",
405             NULL);
406         pool_init(&pf_altq_pl, sizeof(struct pf_altq), 0, 0, 0, "pfaltqpl",
407             &pool_allocator_nointr);
408         pool_init(&pf_pooladdr_pl, sizeof(struct pf_pooladdr), 0, 0, 0,
409             "pfpooladdrpl", &pool_allocator_nointr);
410         pfr_initialize();
411         pfi_initialize();
412         pf_osfp_initialize();
413
414         pool_sethardlimit(pf_pool_limits[PF_LIMIT_STATES].pp,
415             pf_pool_limits[PF_LIMIT_STATES].limit, NULL, 0);
416
417         if (ctob(physmem) <= 100*1024*1024)
418                 pf_pool_limits[PF_LIMIT_TABLE_ENTRIES].limit =
419                     PFR_KENTRY_HIWAT_SMALL;
420
421         RB_INIT(&tree_src_tracking);
422         RB_INIT(&pf_anchors);
423         pf_init_ruleset(&pf_main_ruleset);
424         TAILQ_INIT(&pf_altqs[0]);
425         TAILQ_INIT(&pf_altqs[1]);
426         TAILQ_INIT(&pf_pabuf);
427         pf_altqs_active = &pf_altqs[0];
428         pf_altqs_inactive = &pf_altqs[1];
429         TAILQ_INIT(&state_list);
430
431         /* default rule should never be garbage collected */
432         pf_default_rule.entries.tqe_prev = &pf_default_rule.entries.tqe_next;
433         pf_default_rule.action = PF_PASS;
434         pf_default_rule.nr = -1;
435         pf_default_rule.rtableid = -1;
436
437         /* initialize default timeouts */
438         timeout[PFTM_TCP_FIRST_PACKET] = PFTM_TCP_FIRST_PACKET_VAL;
439         timeout[PFTM_TCP_OPENING] = PFTM_TCP_OPENING_VAL;
440         timeout[PFTM_TCP_ESTABLISHED] = PFTM_TCP_ESTABLISHED_VAL;
441         timeout[PFTM_TCP_CLOSING] = PFTM_TCP_CLOSING_VAL;
442         timeout[PFTM_TCP_FIN_WAIT] = PFTM_TCP_FIN_WAIT_VAL;
443         timeout[PFTM_TCP_CLOSED] = PFTM_TCP_CLOSED_VAL;
444         timeout[PFTM_UDP_FIRST_PACKET] = PFTM_UDP_FIRST_PACKET_VAL;
445         timeout[PFTM_UDP_SINGLE] = PFTM_UDP_SINGLE_VAL;
446         timeout[PFTM_UDP_MULTIPLE] = PFTM_UDP_MULTIPLE_VAL;
447         timeout[PFTM_ICMP_FIRST_PACKET] = PFTM_ICMP_FIRST_PACKET_VAL;
448         timeout[PFTM_ICMP_ERROR_REPLY] = PFTM_ICMP_ERROR_REPLY_VAL;
449         timeout[PFTM_OTHER_FIRST_PACKET] = PFTM_OTHER_FIRST_PACKET_VAL;
450         timeout[PFTM_OTHER_SINGLE] = PFTM_OTHER_SINGLE_VAL;
451         timeout[PFTM_OTHER_MULTIPLE] = PFTM_OTHER_MULTIPLE_VAL;
452         timeout[PFTM_FRAG] = PFTM_FRAG_VAL;
453         timeout[PFTM_INTERVAL] = PFTM_INTERVAL_VAL;
454         timeout[PFTM_SRC_NODE] = PFTM_SRC_NODE_VAL;
455         timeout[PFTM_TS_DIFF] = PFTM_TS_DIFF_VAL;
456         timeout[PFTM_ADAPTIVE_START] = PFSTATE_ADAPT_START;
457         timeout[PFTM_ADAPTIVE_END] = PFSTATE_ADAPT_END;
458
459         pf_normalize_init();
460         bzero(&pf_status, sizeof(pf_status));
461         pf_status.debug = PF_DEBUG_URGENT;
462
463         /* XXX do our best to avoid a conflict */
464         pf_status.hostid = arc4random();
465
466         /* require process context to purge states, so perform in a thread */
467         kthread_create_deferred(pf_thread_create, NULL);
468 }
469
470 void
471 pf_thread_create(void *v)
472 {
473         if (kthread_create(pf_purge_thread, NULL, NULL, "pfpurge"))
474                 panic("pfpurge thread");
475 }
476
477 int
478 pfopen(struct cdev *dev, int flags, int fmt, struct proc *p)
479 {
480         if (minor(dev) >= 1)
481                 return (ENXIO);
482         return (0);
483 }
484
485 int
486 pfclose(struct cdev *dev, int flags, int fmt, struct proc *p)
487 {
488         if (minor(dev) >= 1)
489                 return (ENXIO);
490         return (0);
491 }
492 #endif /* __FreeBSD__ */
493
494 struct pf_pool *
495 pf_get_pool(char *anchor, u_int32_t ticket, u_int8_t rule_action,
496     u_int32_t rule_number, u_int8_t r_last, u_int8_t active,
497     u_int8_t check_ticket)
498 {
499         struct pf_ruleset       *ruleset;
500         struct pf_rule          *rule;
501         int                      rs_num;
502
503         ruleset = pf_find_ruleset(anchor);
504         if (ruleset == NULL)
505                 return (NULL);
506         rs_num = pf_get_ruleset_number(rule_action);
507         if (rs_num >= PF_RULESET_MAX)
508                 return (NULL);
509         if (active) {
510                 if (check_ticket && ticket !=
511                     ruleset->rules[rs_num].active.ticket)
512                         return (NULL);
513                 if (r_last)
514                         rule = TAILQ_LAST(ruleset->rules[rs_num].active.ptr,
515                             pf_rulequeue);
516                 else
517                         rule = TAILQ_FIRST(ruleset->rules[rs_num].active.ptr);
518         } else {
519                 if (check_ticket && ticket !=
520                     ruleset->rules[rs_num].inactive.ticket)
521                         return (NULL);
522                 if (r_last)
523                         rule = TAILQ_LAST(ruleset->rules[rs_num].inactive.ptr,
524                             pf_rulequeue);
525                 else
526                         rule = TAILQ_FIRST(ruleset->rules[rs_num].inactive.ptr);
527         }
528         if (!r_last) {
529                 while ((rule != NULL) && (rule->nr != rule_number))
530                         rule = TAILQ_NEXT(rule, entries);
531         }
532         if (rule == NULL)
533                 return (NULL);
534
535         return (&rule->rpool);
536 }
537
538 void
539 pf_mv_pool(struct pf_palist *poola, struct pf_palist *poolb)
540 {
541         struct pf_pooladdr      *mv_pool_pa;
542
543         while ((mv_pool_pa = TAILQ_FIRST(poola)) != NULL) {
544                 TAILQ_REMOVE(poola, mv_pool_pa, entries);
545                 TAILQ_INSERT_TAIL(poolb, mv_pool_pa, entries);
546         }
547 }
548
549 void
550 pf_empty_pool(struct pf_palist *poola)
551 {
552         struct pf_pooladdr      *empty_pool_pa;
553
554         while ((empty_pool_pa = TAILQ_FIRST(poola)) != NULL) {
555                 pfi_dynaddr_remove(&empty_pool_pa->addr);
556                 pf_tbladdr_remove(&empty_pool_pa->addr);
557                 pfi_kif_unref(empty_pool_pa->kif, PFI_KIF_REF_RULE);
558                 TAILQ_REMOVE(poola, empty_pool_pa, entries);
559                 pool_put(&pf_pooladdr_pl, empty_pool_pa);
560         }
561 }
562
563 void
564 pf_rm_rule(struct pf_rulequeue *rulequeue, struct pf_rule *rule)
565 {
566         if (rulequeue != NULL) {
567                 if (rule->states <= 0) {
568                         /*
569                          * XXX - we need to remove the table *before* detaching
570                          * the rule to make sure the table code does not delete
571                          * the anchor under our feet.
572                          */
573                         pf_tbladdr_remove(&rule->src.addr);
574                         pf_tbladdr_remove(&rule->dst.addr);
575                         if (rule->overload_tbl)
576                                 pfr_detach_table(rule->overload_tbl);
577                 }
578                 TAILQ_REMOVE(rulequeue, rule, entries);
579                 rule->entries.tqe_prev = NULL;
580                 rule->nr = -1;
581         }
582
583         if (rule->states > 0 || rule->src_nodes > 0 ||
584             rule->entries.tqe_prev != NULL)
585                 return;
586         pf_tag_unref(rule->tag);
587         pf_tag_unref(rule->match_tag);
588 #ifdef ALTQ
589         if (rule->pqid != rule->qid)
590                 pf_qid_unref(rule->pqid);
591         pf_qid_unref(rule->qid);
592 #endif
593         pf_rtlabel_remove(&rule->src.addr);
594         pf_rtlabel_remove(&rule->dst.addr);
595         pfi_dynaddr_remove(&rule->src.addr);
596         pfi_dynaddr_remove(&rule->dst.addr);
597         if (rulequeue == NULL) {
598                 pf_tbladdr_remove(&rule->src.addr);
599                 pf_tbladdr_remove(&rule->dst.addr);
600                 if (rule->overload_tbl)
601                         pfr_detach_table(rule->overload_tbl);
602         }
603         pfi_kif_unref(rule->kif, PFI_KIF_REF_RULE);
604         pf_anchor_remove(rule);
605         pf_empty_pool(&rule->rpool.list);
606         pool_put(&pf_rule_pl, rule);
607 }
608
609 u_int16_t
610 tagname2tag(struct pf_tags *head, char *tagname)
611 {
612         struct pf_tagname       *tag, *p = NULL;
613         u_int16_t                new_tagid = 1;
614
615         TAILQ_FOREACH(tag, head, entries)
616                 if (strcmp(tagname, tag->name) == 0) {
617                         tag->ref++;
618                         return (tag->tag);
619                 }
620
621         /*
622          * to avoid fragmentation, we do a linear search from the beginning
623          * and take the first free slot we find. if there is none or the list
624          * is empty, append a new entry at the end.
625          */
626
627         /* new entry */
628         if (!TAILQ_EMPTY(head))
629                 for (p = TAILQ_FIRST(head); p != NULL &&
630                     p->tag == new_tagid; p = TAILQ_NEXT(p, entries))
631                         new_tagid = p->tag + 1;
632
633         if (new_tagid > TAGID_MAX)
634                 return (0);
635
636         /* allocate and fill new struct pf_tagname */
637         tag = (struct pf_tagname *)malloc(sizeof(struct pf_tagname),
638             M_TEMP, M_NOWAIT);
639         if (tag == NULL)
640                 return (0);
641         bzero(tag, sizeof(struct pf_tagname));
642         strlcpy(tag->name, tagname, sizeof(tag->name));
643         tag->tag = new_tagid;
644         tag->ref++;
645
646         if (p != NULL)  /* insert new entry before p */
647                 TAILQ_INSERT_BEFORE(p, tag, entries);
648         else    /* either list empty or no free slot in between */
649                 TAILQ_INSERT_TAIL(head, tag, entries);
650
651         return (tag->tag);
652 }
653
654 void
655 tag2tagname(struct pf_tags *head, u_int16_t tagid, char *p)
656 {
657         struct pf_tagname       *tag;
658
659         TAILQ_FOREACH(tag, head, entries)
660                 if (tag->tag == tagid) {
661                         strlcpy(p, tag->name, PF_TAG_NAME_SIZE);
662                         return;
663                 }
664 }
665
666 void
667 tag_unref(struct pf_tags *head, u_int16_t tag)
668 {
669         struct pf_tagname       *p, *next;
670
671         if (tag == 0)
672                 return;
673
674         for (p = TAILQ_FIRST(head); p != NULL; p = next) {
675                 next = TAILQ_NEXT(p, entries);
676                 if (tag == p->tag) {
677                         if (--p->ref == 0) {
678                                 TAILQ_REMOVE(head, p, entries);
679                                 free(p, M_TEMP);
680                         }
681                         break;
682                 }
683         }
684 }
685
686 u_int16_t
687 pf_tagname2tag(char *tagname)
688 {
689         return (tagname2tag(&pf_tags, tagname));
690 }
691
692 void
693 pf_tag2tagname(u_int16_t tagid, char *p)
694 {
695         tag2tagname(&pf_tags, tagid, p);
696 }
697
698 void
699 pf_tag_ref(u_int16_t tag)
700 {
701         struct pf_tagname *t;
702
703         TAILQ_FOREACH(t, &pf_tags, entries)
704                 if (t->tag == tag)
705                         break;
706         if (t != NULL)
707                 t->ref++;
708 }
709
710 void
711 pf_tag_unref(u_int16_t tag)
712 {
713         tag_unref(&pf_tags, tag);
714 }
715
716 int
717 pf_rtlabel_add(struct pf_addr_wrap *a)
718 {
719 #ifdef __FreeBSD__
720         /* XXX_IMPORT: later */
721         return (0);
722 #else
723         if (a->type == PF_ADDR_RTLABEL &&
724             (a->v.rtlabel = rtlabel_name2id(a->v.rtlabelname)) == 0)
725                 return (-1);
726         return (0);
727 #endif
728 }
729
730 void
731 pf_rtlabel_remove(struct pf_addr_wrap *a)
732 {
733 #ifdef __FreeBSD__
734         /* XXX_IMPORT: later */
735 #else
736         if (a->type == PF_ADDR_RTLABEL)
737                 rtlabel_unref(a->v.rtlabel);
738 #endif
739 }
740
741 void
742 pf_rtlabel_copyout(struct pf_addr_wrap *a)
743 {
744 #ifdef __FreeBSD__
745         /* XXX_IMPORT: later */
746         if (a->type == PF_ADDR_RTLABEL && a->v.rtlabel)
747                 strlcpy(a->v.rtlabelname, "?", sizeof(a->v.rtlabelname));
748 #else
749         const char      *name;
750
751         if (a->type == PF_ADDR_RTLABEL && a->v.rtlabel) {
752                 if ((name = rtlabel_id2name(a->v.rtlabel)) == NULL)
753                         strlcpy(a->v.rtlabelname, "?",
754                             sizeof(a->v.rtlabelname));
755                 else
756                         strlcpy(a->v.rtlabelname, name,
757                             sizeof(a->v.rtlabelname));
758         }
759 #endif
760 }
761
762 #ifdef ALTQ
763 u_int32_t
764 pf_qname2qid(char *qname)
765 {
766         return ((u_int32_t)tagname2tag(&pf_qids, qname));
767 }
768
769 void
770 pf_qid2qname(u_int32_t qid, char *p)
771 {
772         tag2tagname(&pf_qids, (u_int16_t)qid, p);
773 }
774
775 void
776 pf_qid_unref(u_int32_t qid)
777 {
778         tag_unref(&pf_qids, (u_int16_t)qid);
779 }
780
781 int
782 pf_begin_altq(u_int32_t *ticket)
783 {
784         struct pf_altq  *altq;
785         int              error = 0;
786
787         /* Purge the old altq list */
788         while ((altq = TAILQ_FIRST(pf_altqs_inactive)) != NULL) {
789                 TAILQ_REMOVE(pf_altqs_inactive, altq, entries);
790 #ifdef __FreeBSD__
791                 if (altq->qname[0] == 0 &&
792                     (altq->local_flags & PFALTQ_FLAG_IF_REMOVED) == 0) {
793 #else
794                 if (altq->qname[0] == 0) {
795 #endif
796                         /* detach and destroy the discipline */
797                         error = altq_remove(altq);
798                 } else
799                         pf_qid_unref(altq->qid);
800                 pool_put(&pf_altq_pl, altq);
801         }
802         if (error)
803                 return (error);
804         *ticket = ++ticket_altqs_inactive;
805         altqs_inactive_open = 1;
806         return (0);
807 }
808
809 int
810 pf_rollback_altq(u_int32_t ticket)
811 {
812         struct pf_altq  *altq;
813         int              error = 0;
814
815         if (!altqs_inactive_open || ticket != ticket_altqs_inactive)
816                 return (0);
817         /* Purge the old altq list */
818         while ((altq = TAILQ_FIRST(pf_altqs_inactive)) != NULL) {
819                 TAILQ_REMOVE(pf_altqs_inactive, altq, entries);
820 #ifdef __FreeBSD__
821                 if (altq->qname[0] == 0 &&
822                     (altq->local_flags & PFALTQ_FLAG_IF_REMOVED) == 0) {
823 #else
824                 if (altq->qname[0] == 0) {
825 #endif
826                         /* detach and destroy the discipline */
827                         error = altq_remove(altq);
828                 } else
829                         pf_qid_unref(altq->qid);
830                 pool_put(&pf_altq_pl, altq);
831         }
832         altqs_inactive_open = 0;
833         return (error);
834 }
835
836 int
837 pf_commit_altq(u_int32_t ticket)
838 {
839         struct pf_altqqueue     *old_altqs;
840         struct pf_altq          *altq;
841         int                      s, err, error = 0;
842
843         if (!altqs_inactive_open || ticket != ticket_altqs_inactive)
844                 return (EBUSY);
845
846         /* swap altqs, keep the old. */
847         s = splsoftnet();
848         old_altqs = pf_altqs_active;
849         pf_altqs_active = pf_altqs_inactive;
850         pf_altqs_inactive = old_altqs;
851         ticket_altqs_active = ticket_altqs_inactive;
852
853         /* Attach new disciplines */
854         TAILQ_FOREACH(altq, pf_altqs_active, entries) {
855 #ifdef __FreeBSD__
856                 if (altq->qname[0] == 0 &&
857                     (altq->local_flags & PFALTQ_FLAG_IF_REMOVED) == 0) {
858 #else
859                 if (altq->qname[0] == 0) {
860 #endif
861                         /* attach the discipline */
862                         error = altq_pfattach(altq);
863                         if (error == 0 && pf_altq_running)
864                                 error = pf_enable_altq(altq);
865                         if (error != 0) {
866                                 splx(s);
867                                 return (error);
868                         }
869                 }
870         }
871
872         /* Purge the old altq list */
873         while ((altq = TAILQ_FIRST(pf_altqs_inactive)) != NULL) {
874                 TAILQ_REMOVE(pf_altqs_inactive, altq, entries);
875 #ifdef __FreeBSD__
876                 if (altq->qname[0] == 0 &&
877                     (altq->local_flags & PFALTQ_FLAG_IF_REMOVED) == 0) {
878 #else
879                 if (altq->qname[0] == 0) {
880 #endif
881                         /* detach and destroy the discipline */
882                         if (pf_altq_running)
883                                 error = pf_disable_altq(altq);
884                         err = altq_pfdetach(altq);
885                         if (err != 0 && error == 0)
886                                 error = err;
887                         err = altq_remove(altq);
888                         if (err != 0 && error == 0)
889                                 error = err;
890                 } else
891                         pf_qid_unref(altq->qid);
892                 pool_put(&pf_altq_pl, altq);
893         }
894         splx(s);
895
896         altqs_inactive_open = 0;
897         return (error);
898 }
899
900 int
901 pf_enable_altq(struct pf_altq *altq)
902 {
903         struct ifnet            *ifp;
904         struct tb_profile        tb;
905         int                      s, error = 0;
906
907         if ((ifp = ifunit(altq->ifname)) == NULL)
908                 return (EINVAL);
909
910         if (ifp->if_snd.altq_type != ALTQT_NONE)
911                 error = altq_enable(&ifp->if_snd);
912
913         /* set tokenbucket regulator */
914         if (error == 0 && ifp != NULL && ALTQ_IS_ENABLED(&ifp->if_snd)) {
915                 tb.rate = altq->ifbandwidth;
916                 tb.depth = altq->tbrsize;
917                 s = splnet();
918 #ifdef __FreeBSD__
919                 PF_UNLOCK();
920 #endif
921                 error = tbr_set(&ifp->if_snd, &tb);
922 #ifdef __FreeBSD__
923                 PF_LOCK();
924 #endif
925                 splx(s);
926         }
927
928         return (error);
929 }
930
931 int
932 pf_disable_altq(struct pf_altq *altq)
933 {
934         struct ifnet            *ifp;
935         struct tb_profile        tb;
936         int                      s, error;
937
938         if ((ifp = ifunit(altq->ifname)) == NULL)
939                 return (EINVAL);
940
941         /*
942          * when the discipline is no longer referenced, it was overridden
943          * by a new one.  if so, just return.
944          */
945         if (altq->altq_disc != ifp->if_snd.altq_disc)
946                 return (0);
947
948         error = altq_disable(&ifp->if_snd);
949
950         if (error == 0) {
951                 /* clear tokenbucket regulator */
952                 tb.rate = 0;
953                 s = splnet();
954 #ifdef __FreeBSD__
955                 PF_UNLOCK();
956 #endif
957                 error = tbr_set(&ifp->if_snd, &tb);
958 #ifdef __FreeBSD__
959                 PF_LOCK();
960 #endif
961                 splx(s);
962         }
963
964         return (error);
965 }
966
967 #ifdef __FreeBSD__
968 void
969 pf_altq_ifnet_event(struct ifnet *ifp, int remove)
970 {
971         struct ifnet            *ifp1;
972         struct pf_altq          *a1, *a2, *a3;
973         u_int32_t                ticket;
974         int                      error = 0;
975
976         /* Interrupt userland queue modifications */
977         if (altqs_inactive_open)
978                 pf_rollback_altq(ticket_altqs_inactive);
979
980         /* Start new altq ruleset */
981         if (pf_begin_altq(&ticket))
982                 return;
983
984         /* Copy the current active set */
985         TAILQ_FOREACH(a1, pf_altqs_active, entries) {
986                 a2 = pool_get(&pf_altq_pl, PR_NOWAIT);
987                 if (a2 == NULL) {
988                         error = ENOMEM;
989                         break;
990                 }
991                 bcopy(a1, a2, sizeof(struct pf_altq));
992
993                 if (a2->qname[0] != 0) {
994                         if ((a2->qid = pf_qname2qid(a2->qname)) == 0) {
995                                 error = EBUSY;
996                                 pool_put(&pf_altq_pl, a2);
997                                 break;
998                         }
999                         a2->altq_disc = NULL;
1000                         TAILQ_FOREACH(a3, pf_altqs_inactive, entries) {
1001                                 if (strncmp(a3->ifname, a2->ifname,
1002                                     IFNAMSIZ) == 0 && a3->qname[0] == 0) {
1003                                         a2->altq_disc = a3->altq_disc;
1004                                         break;
1005                                 }
1006                         }
1007                 }
1008                 /* Deactivate the interface in question */
1009                 a2->local_flags &= ~PFALTQ_FLAG_IF_REMOVED;
1010                 if ((ifp1 = ifunit(a2->ifname)) == NULL ||
1011                     (remove && ifp1 == ifp)) {
1012                         a2->local_flags |= PFALTQ_FLAG_IF_REMOVED;
1013                 } else {
1014                         PF_UNLOCK();
1015                         error = altq_add(a2);
1016                         PF_LOCK();
1017
1018                         if (ticket != ticket_altqs_inactive)
1019                                 error = EBUSY;
1020
1021                         if (error) {
1022                                 pool_put(&pf_altq_pl, a2);
1023                                 break;
1024                         }
1025                 }
1026
1027                 TAILQ_INSERT_TAIL(pf_altqs_inactive, a2, entries);
1028         }
1029
1030         if (error != 0)
1031                 pf_rollback_altq(ticket);
1032         else
1033                 pf_commit_altq(ticket);
1034 }
1035 #endif
1036 #endif /* ALTQ */
1037
1038 int
1039 pf_begin_rules(u_int32_t *ticket, int rs_num, const char *anchor)
1040 {
1041         struct pf_ruleset       *rs;
1042         struct pf_rule          *rule;
1043
1044         if (rs_num < 0 || rs_num >= PF_RULESET_MAX)
1045                 return (EINVAL);
1046         rs = pf_find_or_create_ruleset(anchor);
1047         if (rs == NULL)
1048                 return (EINVAL);
1049         while ((rule = TAILQ_FIRST(rs->rules[rs_num].inactive.ptr)) != NULL) {
1050                 pf_rm_rule(rs->rules[rs_num].inactive.ptr, rule);
1051                 rs->rules[rs_num].inactive.rcount--;
1052         }
1053         *ticket = ++rs->rules[rs_num].inactive.ticket;
1054         rs->rules[rs_num].inactive.open = 1;
1055         return (0);
1056 }
1057
1058 int
1059 pf_rollback_rules(u_int32_t ticket, int rs_num, char *anchor)
1060 {
1061         struct pf_ruleset       *rs;
1062         struct pf_rule          *rule;
1063
1064         if (rs_num < 0 || rs_num >= PF_RULESET_MAX)
1065                 return (EINVAL);
1066         rs = pf_find_ruleset(anchor);
1067         if (rs == NULL || !rs->rules[rs_num].inactive.open ||
1068             rs->rules[rs_num].inactive.ticket != ticket)
1069                 return (0);
1070         while ((rule = TAILQ_FIRST(rs->rules[rs_num].inactive.ptr)) != NULL) {
1071                 pf_rm_rule(rs->rules[rs_num].inactive.ptr, rule);
1072                 rs->rules[rs_num].inactive.rcount--;
1073         }
1074         rs->rules[rs_num].inactive.open = 0;
1075         return (0);
1076 }
1077
1078 #define PF_MD5_UPD(st, elm)                                             \
1079                 MD5Update(ctx, (u_int8_t *) &(st)->elm, sizeof((st)->elm))
1080
1081 #define PF_MD5_UPD_STR(st, elm)                                         \
1082                 MD5Update(ctx, (u_int8_t *) (st)->elm, strlen((st)->elm))
1083
1084 #define PF_MD5_UPD_HTONL(st, elm, stor) do {                            \
1085                 (stor) = htonl((st)->elm);                              \
1086                 MD5Update(ctx, (u_int8_t *) &(stor), sizeof(u_int32_t));\
1087 } while (0)
1088
1089 #define PF_MD5_UPD_HTONS(st, elm, stor) do {                            \
1090                 (stor) = htons((st)->elm);                              \
1091                 MD5Update(ctx, (u_int8_t *) &(stor), sizeof(u_int16_t));\
1092 } while (0)
1093
1094 void
1095 pf_hash_rule_addr(MD5_CTX *ctx, struct pf_rule_addr *pfr)
1096 {
1097         PF_MD5_UPD(pfr, addr.type);
1098         switch (pfr->addr.type) {
1099                 case PF_ADDR_DYNIFTL:
1100                         PF_MD5_UPD(pfr, addr.v.ifname);
1101                         PF_MD5_UPD(pfr, addr.iflags);
1102                         break;
1103                 case PF_ADDR_TABLE:
1104                         PF_MD5_UPD(pfr, addr.v.tblname);
1105                         break;
1106                 case PF_ADDR_ADDRMASK:
1107                         /* XXX ignore af? */
1108                         PF_MD5_UPD(pfr, addr.v.a.addr.addr32);
1109                         PF_MD5_UPD(pfr, addr.v.a.mask.addr32);
1110                         break;
1111                 case PF_ADDR_RTLABEL:
1112                         PF_MD5_UPD(pfr, addr.v.rtlabelname);
1113                         break;
1114         }
1115
1116         PF_MD5_UPD(pfr, port[0]);
1117         PF_MD5_UPD(pfr, port[1]);
1118         PF_MD5_UPD(pfr, neg);
1119         PF_MD5_UPD(pfr, port_op);
1120 }
1121
1122 void
1123 pf_hash_rule(MD5_CTX *ctx, struct pf_rule *rule)
1124 {
1125         u_int16_t x;
1126         u_int32_t y;
1127
1128         pf_hash_rule_addr(ctx, &rule->src);
1129         pf_hash_rule_addr(ctx, &rule->dst);
1130         PF_MD5_UPD_STR(rule, label);
1131         PF_MD5_UPD_STR(rule, ifname);
1132         PF_MD5_UPD_STR(rule, match_tagname);
1133         PF_MD5_UPD_HTONS(rule, match_tag, x); /* dup? */
1134         PF_MD5_UPD_HTONL(rule, os_fingerprint, y);
1135         PF_MD5_UPD_HTONL(rule, prob, y);
1136         PF_MD5_UPD_HTONL(rule, uid.uid[0], y);
1137         PF_MD5_UPD_HTONL(rule, uid.uid[1], y);
1138         PF_MD5_UPD(rule, uid.op);
1139         PF_MD5_UPD_HTONL(rule, gid.gid[0], y);
1140         PF_MD5_UPD_HTONL(rule, gid.gid[1], y);
1141         PF_MD5_UPD(rule, gid.op);
1142         PF_MD5_UPD_HTONL(rule, rule_flag, y);
1143         PF_MD5_UPD(rule, action);
1144         PF_MD5_UPD(rule, direction);
1145         PF_MD5_UPD(rule, af);
1146         PF_MD5_UPD(rule, quick);
1147         PF_MD5_UPD(rule, ifnot);
1148         PF_MD5_UPD(rule, match_tag_not);
1149         PF_MD5_UPD(rule, natpass);
1150         PF_MD5_UPD(rule, keep_state);
1151         PF_MD5_UPD(rule, proto);
1152         PF_MD5_UPD(rule, type);
1153         PF_MD5_UPD(rule, code);
1154         PF_MD5_UPD(rule, flags);
1155         PF_MD5_UPD(rule, flagset);
1156         PF_MD5_UPD(rule, allow_opts);
1157         PF_MD5_UPD(rule, rt);
1158         PF_MD5_UPD(rule, tos);
1159 }
1160
1161 int
1162 pf_commit_rules(u_int32_t ticket, int rs_num, char *anchor)
1163 {
1164         struct pf_ruleset       *rs;
1165         struct pf_rule          *rule, **old_array;
1166         struct pf_rulequeue     *old_rules;
1167         int                      s, error;
1168         u_int32_t                old_rcount;
1169
1170         if (rs_num < 0 || rs_num >= PF_RULESET_MAX)
1171                 return (EINVAL);
1172         rs = pf_find_ruleset(anchor);
1173         if (rs == NULL || !rs->rules[rs_num].inactive.open ||
1174             ticket != rs->rules[rs_num].inactive.ticket)
1175                 return (EBUSY);
1176
1177         /* Calculate checksum for the main ruleset */
1178         if (rs == &pf_main_ruleset) {
1179                 error = pf_setup_pfsync_matching(rs);
1180                 if (error != 0)
1181                         return (error);
1182         }
1183
1184         /* Swap rules, keep the old. */
1185         s = splsoftnet();
1186         old_rules = rs->rules[rs_num].active.ptr;
1187         old_rcount = rs->rules[rs_num].active.rcount;
1188         old_array = rs->rules[rs_num].active.ptr_array;
1189
1190         rs->rules[rs_num].active.ptr =
1191             rs->rules[rs_num].inactive.ptr;
1192         rs->rules[rs_num].active.ptr_array =
1193             rs->rules[rs_num].inactive.ptr_array;
1194         rs->rules[rs_num].active.rcount =
1195             rs->rules[rs_num].inactive.rcount;
1196         rs->rules[rs_num].inactive.ptr = old_rules;
1197         rs->rules[rs_num].inactive.ptr_array = old_array;
1198         rs->rules[rs_num].inactive.rcount = old_rcount;
1199
1200         rs->rules[rs_num].active.ticket =
1201             rs->rules[rs_num].inactive.ticket;
1202         pf_calc_skip_steps(rs->rules[rs_num].active.ptr);
1203
1204
1205         /* Purge the old rule list. */
1206         while ((rule = TAILQ_FIRST(old_rules)) != NULL)
1207                 pf_rm_rule(old_rules, rule);
1208         if (rs->rules[rs_num].inactive.ptr_array)
1209                 free(rs->rules[rs_num].inactive.ptr_array, M_TEMP);
1210         rs->rules[rs_num].inactive.ptr_array = NULL;
1211         rs->rules[rs_num].inactive.rcount = 0;
1212         rs->rules[rs_num].inactive.open = 0;
1213         pf_remove_if_empty_ruleset(rs);
1214         splx(s);
1215         return (0);
1216 }
1217
1218 int
1219 pf_setup_pfsync_matching(struct pf_ruleset *rs)
1220 {
1221         MD5_CTX                  ctx;
1222         struct pf_rule          *rule;
1223         int                      rs_cnt;
1224         u_int8_t                 digest[PF_MD5_DIGEST_LENGTH];
1225
1226         MD5Init(&ctx);
1227         for (rs_cnt = 0; rs_cnt < PF_RULESET_MAX; rs_cnt++) {
1228                 /* XXX PF_RULESET_SCRUB as well? */
1229                 if (rs_cnt == PF_RULESET_SCRUB)
1230                         continue;
1231
1232                 if (rs->rules[rs_cnt].inactive.ptr_array)
1233                         free(rs->rules[rs_cnt].inactive.ptr_array, M_TEMP);
1234                 rs->rules[rs_cnt].inactive.ptr_array = NULL;
1235
1236                 if (rs->rules[rs_cnt].inactive.rcount) {
1237                         rs->rules[rs_cnt].inactive.ptr_array =
1238                             malloc(sizeof(caddr_t) *
1239                             rs->rules[rs_cnt].inactive.rcount,
1240                             M_TEMP, M_NOWAIT);
1241
1242                         if (!rs->rules[rs_cnt].inactive.ptr_array)
1243                                 return (ENOMEM);
1244                 }
1245
1246                 TAILQ_FOREACH(rule, rs->rules[rs_cnt].inactive.ptr,
1247                     entries) {
1248                         pf_hash_rule(&ctx, rule);
1249                         (rs->rules[rs_cnt].inactive.ptr_array)[rule->nr] = rule;
1250                 }
1251         }
1252
1253         MD5Final(digest, &ctx);
1254         memcpy(pf_status.pf_chksum, digest, sizeof(pf_status.pf_chksum));
1255         return (0);
1256 }
1257
1258 int
1259 #ifdef __FreeBSD__
1260 pfioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, struct thread *td)
1261 #else
1262 pfioctl(dev_t dev, u_long cmd, caddr_t addr, int flags, struct proc *p)
1263 #endif
1264 {
1265         struct pf_pooladdr      *pa = NULL;
1266         struct pf_pool          *pool = NULL;
1267 #ifndef __FreeBSD__
1268         int                      s;
1269 #endif
1270         int                      error = 0;
1271
1272         /* XXX keep in sync with switch() below */
1273 #ifdef __FreeBSD__
1274         if (securelevel_gt(td->td_ucred, 2))
1275 #else
1276         if (securelevel > 1)
1277 #endif
1278                 switch (cmd) {
1279                 case DIOCGETRULES:
1280                 case DIOCGETRULE:
1281                 case DIOCGETADDRS:
1282                 case DIOCGETADDR:
1283                 case DIOCGETSTATE:
1284                 case DIOCSETSTATUSIF:
1285                 case DIOCGETSTATUS:
1286                 case DIOCCLRSTATUS:
1287                 case DIOCNATLOOK:
1288                 case DIOCSETDEBUG:
1289                 case DIOCGETSTATES:
1290                 case DIOCGETTIMEOUT:
1291                 case DIOCCLRRULECTRS:
1292                 case DIOCGETLIMIT:
1293                 case DIOCGETALTQS:
1294                 case DIOCGETALTQ:
1295                 case DIOCGETQSTATS:
1296                 case DIOCGETRULESETS:
1297                 case DIOCGETRULESET:
1298                 case DIOCRGETTABLES:
1299                 case DIOCRGETTSTATS:
1300                 case DIOCRCLRTSTATS:
1301                 case DIOCRCLRADDRS:
1302                 case DIOCRADDADDRS:
1303                 case DIOCRDELADDRS:
1304                 case DIOCRSETADDRS:
1305                 case DIOCRGETADDRS:
1306                 case DIOCRGETASTATS:
1307                 case DIOCRCLRASTATS:
1308                 case DIOCRTSTADDRS:
1309                 case DIOCOSFPGET:
1310                 case DIOCGETSRCNODES:
1311                 case DIOCCLRSRCNODES:
1312                 case DIOCIGETIFACES:
1313 #ifdef __FreeBSD__
1314                 case DIOCGIFSPEED:
1315 #endif
1316                 case DIOCSETIFFLAG:
1317                 case DIOCCLRIFFLAG:
1318                         break;
1319                 case DIOCRCLRTABLES:
1320                 case DIOCRADDTABLES:
1321                 case DIOCRDELTABLES:
1322                 case DIOCRSETTFLAGS:
1323                         if (((struct pfioc_table *)addr)->pfrio_flags &
1324                             PFR_FLAG_DUMMY)
1325                                 break; /* dummy operation ok */
1326                         return (EPERM);
1327                 default:
1328                         return (EPERM);
1329                 }
1330
1331         if (!(flags & FWRITE))
1332                 switch (cmd) {
1333                 case DIOCGETRULES:
1334                 case DIOCGETADDRS:
1335                 case DIOCGETADDR:
1336                 case DIOCGETSTATE:
1337                 case DIOCGETSTATUS:
1338                 case DIOCGETSTATES:
1339                 case DIOCGETTIMEOUT:
1340                 case DIOCGETLIMIT:
1341                 case DIOCGETALTQS:
1342                 case DIOCGETALTQ:
1343                 case DIOCGETQSTATS:
1344                 case DIOCGETRULESETS:
1345                 case DIOCGETRULESET:
1346                 case DIOCNATLOOK:
1347                 case DIOCRGETTABLES:
1348                 case DIOCRGETTSTATS:
1349                 case DIOCRGETADDRS:
1350                 case DIOCRGETASTATS:
1351                 case DIOCRTSTADDRS:
1352                 case DIOCOSFPGET:
1353                 case DIOCGETSRCNODES:
1354                 case DIOCIGETIFACES:
1355 #ifdef __FreeBSD__
1356                 case DIOCGIFSPEED:
1357 #endif
1358                         break;
1359                 case DIOCRCLRTABLES:
1360                 case DIOCRADDTABLES:
1361                 case DIOCRDELTABLES:
1362                 case DIOCRCLRTSTATS:
1363                 case DIOCRCLRADDRS:
1364                 case DIOCRADDADDRS:
1365                 case DIOCRDELADDRS:
1366                 case DIOCRSETADDRS:
1367                 case DIOCRSETTFLAGS:
1368                         if (((struct pfioc_table *)addr)->pfrio_flags &
1369                             PFR_FLAG_DUMMY) {
1370                                 flags |= FWRITE; /* need write lock for dummy */
1371                                 break; /* dummy operation ok */
1372                         }
1373                         return (EACCES);
1374                 case DIOCGETRULE:
1375                         if (((struct pfioc_rule *)addr)->action == PF_GET_CLR_CNTR)
1376                                 return (EACCES);
1377                         break;
1378                 default:
1379                         return (EACCES);
1380                 }
1381
1382         if (flags & FWRITE)
1383 #ifdef __FreeBSD__
1384                 sx_xlock(&pf_consistency_lock);
1385         else
1386                 sx_slock(&pf_consistency_lock);
1387 #else
1388                 rw_enter_write(&pf_consistency_lock);
1389         else
1390                 rw_enter_read(&pf_consistency_lock);
1391 #endif
1392
1393 #ifdef __FreeBSD__
1394         PF_LOCK();
1395 #else
1396         s = splsoftnet();
1397 #endif
1398         switch (cmd) {
1399
1400         case DIOCSTART:
1401                 if (pf_status.running)
1402                         error = EEXIST;
1403                 else {
1404 #ifdef __FreeBSD__
1405                         PF_UNLOCK();
1406                         error = hook_pf();
1407                         PF_LOCK();
1408                         if (error) {
1409                                 DPFPRINTF(PF_DEBUG_MISC,
1410                                     ("pf: pfil registeration fail\n"));
1411                                 break;
1412                         }
1413 #endif
1414                         pf_status.running = 1;
1415                         pf_status.since = time_second;
1416                         if (pf_status.stateid == 0) {
1417                                 pf_status.stateid = time_second;
1418                                 pf_status.stateid = pf_status.stateid << 32;
1419                         }
1420                         DPFPRINTF(PF_DEBUG_MISC, ("pf: started\n"));
1421                 }
1422                 break;
1423
1424         case DIOCSTOP:
1425                 if (!pf_status.running)
1426                         error = ENOENT;
1427                 else {
1428                         pf_status.running = 0;
1429 #ifdef __FreeBSD__
1430                         PF_UNLOCK();
1431                         error = dehook_pf();
1432                         PF_LOCK();
1433                         if (error) {
1434                                 pf_status.running = 1;
1435                                 DPFPRINTF(PF_DEBUG_MISC,
1436                                         ("pf: pfil unregisteration failed\n"));
1437                         }
1438 #endif
1439                         pf_status.since = time_second;
1440                         DPFPRINTF(PF_DEBUG_MISC, ("pf: stopped\n"));
1441                 }
1442                 break;
1443
1444         case DIOCADDRULE: {
1445                 struct pfioc_rule       *pr = (struct pfioc_rule *)addr;
1446                 struct pf_ruleset       *ruleset;
1447                 struct pf_rule          *rule, *tail;
1448                 struct pf_pooladdr      *pa;
1449                 int                      rs_num;
1450
1451                 pr->anchor[sizeof(pr->anchor) - 1] = 0;
1452                 ruleset = pf_find_ruleset(pr->anchor);
1453                 if (ruleset == NULL) {
1454                         error = EINVAL;
1455                         break;
1456                 }
1457                 rs_num = pf_get_ruleset_number(pr->rule.action);
1458                 if (rs_num >= PF_RULESET_MAX) {
1459                         error = EINVAL;
1460                         break;
1461                 }
1462                 if (pr->rule.return_icmp >> 8 > ICMP_MAXTYPE) {
1463                         error = EINVAL;
1464                         break;
1465                 }
1466                 if (pr->ticket != ruleset->rules[rs_num].inactive.ticket) {
1467 #ifdef __FreeBSD__
1468                         DPFPRINTF(PF_DEBUG_MISC,
1469                             ("ticket: %d != [%d]%d\n", pr->ticket, rs_num,
1470                             ruleset->rules[rs_num].inactive.ticket));
1471 #endif
1472                         error = EBUSY;
1473                         break;
1474                 }
1475                 if (pr->pool_ticket != ticket_pabuf) {
1476 #ifdef __FreeBSD__
1477                         DPFPRINTF(PF_DEBUG_MISC,
1478                             ("pool_ticket: %d != %d\n", pr->pool_ticket,
1479                             ticket_pabuf));
1480 #endif
1481                         error = EBUSY;
1482                         break;
1483                 }
1484                 rule = pool_get(&pf_rule_pl, PR_NOWAIT);
1485                 if (rule == NULL) {
1486                         error = ENOMEM;
1487                         break;
1488                 }
1489                 bcopy(&pr->rule, rule, sizeof(struct pf_rule));
1490 #ifdef __FreeBSD__
1491                 rule->cuid = td->td_ucred->cr_ruid;
1492                 rule->cpid = td->td_proc ? td->td_proc->p_pid : 0;
1493 #else
1494                 rule->cuid = p->p_cred->p_ruid;
1495                 rule->cpid = p->p_pid;
1496 #endif
1497                 rule->anchor = NULL;
1498                 rule->kif = NULL;
1499                 TAILQ_INIT(&rule->rpool.list);
1500                 /* initialize refcounting */
1501                 rule->states = 0;
1502                 rule->src_nodes = 0;
1503                 rule->entries.tqe_prev = NULL;
1504 #ifndef INET
1505                 if (rule->af == AF_INET) {
1506                         pool_put(&pf_rule_pl, rule);
1507                         error = EAFNOSUPPORT;
1508                         break;
1509                 }
1510 #endif /* INET */
1511 #ifndef INET6
1512                 if (rule->af == AF_INET6) {
1513                         pool_put(&pf_rule_pl, rule);
1514                         error = EAFNOSUPPORT;
1515                         break;
1516                 }
1517 #endif /* INET6 */
1518                 tail = TAILQ_LAST(ruleset->rules[rs_num].inactive.ptr,
1519                     pf_rulequeue);
1520                 if (tail)
1521                         rule->nr = tail->nr + 1;
1522                 else
1523                         rule->nr = 0;
1524                 if (rule->ifname[0]) {
1525                         rule->kif = pfi_kif_get(rule->ifname);
1526                         if (rule->kif == NULL) {
1527                                 pool_put(&pf_rule_pl, rule);
1528                                 error = EINVAL;
1529                                 break;
1530                         }
1531                         pfi_kif_ref(rule->kif, PFI_KIF_REF_RULE);
1532                 }
1533
1534 #ifdef __FreeBSD__ /* ROUTING */
1535                 if (rule->rtableid > 0 && rule->rtableid > rt_numfibs)
1536 #else
1537                 if (rule->rtableid > 0 && !rtable_exists(rule->rtableid))
1538 #endif
1539                         error = EBUSY;
1540
1541 #ifdef ALTQ
1542                 /* set queue IDs */
1543                 if (rule->qname[0] != 0) {
1544                         if ((rule->qid = pf_qname2qid(rule->qname)) == 0)
1545                                 error = EBUSY;
1546                         else if (rule->pqname[0] != 0) {
1547                                 if ((rule->pqid =
1548                                     pf_qname2qid(rule->pqname)) == 0)
1549                                         error = EBUSY;
1550                         } else
1551                                 rule->pqid = rule->qid;
1552                 }
1553 #endif
1554                 if (rule->tagname[0])
1555                         if ((rule->tag = pf_tagname2tag(rule->tagname)) == 0)
1556                                 error = EBUSY;
1557                 if (rule->match_tagname[0])
1558                         if ((rule->match_tag =
1559                             pf_tagname2tag(rule->match_tagname)) == 0)
1560                                 error = EBUSY;
1561                 if (rule->rt && !rule->direction)
1562                         error = EINVAL;
1563 #if NPFLOG > 0
1564 #ifdef __FreeBSD__
1565                 if (!rule->log)
1566                         rule->logif = 0;
1567 #endif
1568                 if (rule->logif >= PFLOGIFS_MAX)
1569                         error = EINVAL;
1570 #endif
1571                 if (pf_rtlabel_add(&rule->src.addr) ||
1572                     pf_rtlabel_add(&rule->dst.addr))
1573                         error = EBUSY;
1574                 if (pfi_dynaddr_setup(&rule->src.addr, rule->af))
1575                         error = EINVAL;
1576                 if (pfi_dynaddr_setup(&rule->dst.addr, rule->af))
1577                         error = EINVAL;
1578                 if (pf_tbladdr_setup(ruleset, &rule->src.addr))
1579                         error = EINVAL;
1580                 if (pf_tbladdr_setup(ruleset, &rule->dst.addr))
1581                         error = EINVAL;
1582                 if (pf_anchor_setup(rule, ruleset, pr->anchor_call))
1583                         error = EINVAL;
1584                 TAILQ_FOREACH(pa, &pf_pabuf, entries)
1585                         if (pf_tbladdr_setup(ruleset, &pa->addr))
1586                                 error = EINVAL;
1587
1588                 if (rule->overload_tblname[0]) {
1589                         if ((rule->overload_tbl = pfr_attach_table(ruleset,
1590                             rule->overload_tblname)) == NULL)
1591                                 error = EINVAL;
1592                         else
1593                                 rule->overload_tbl->pfrkt_flags |=
1594                                     PFR_TFLAG_ACTIVE;
1595                 }
1596
1597                 pf_mv_pool(&pf_pabuf, &rule->rpool.list);
1598                 if (((((rule->action == PF_NAT) || (rule->action == PF_RDR) ||
1599                     (rule->action == PF_BINAT)) && rule->anchor == NULL) ||
1600                     (rule->rt > PF_FASTROUTE)) &&
1601                     (TAILQ_FIRST(&rule->rpool.list) == NULL))
1602                         error = EINVAL;
1603
1604                 if (error) {
1605                         pf_rm_rule(NULL, rule);
1606                         break;
1607                 }
1608
1609 #ifdef __FreeBSD__
1610                 if (!debug_pfugidhack && (rule->uid.op || rule->gid.op ||
1611                     rule->log & PF_LOG_SOCKET_LOOKUP)) {
1612                         DPFPRINTF(PF_DEBUG_MISC,
1613                             ("pf: debug.pfugidhack enabled\n"));
1614                         debug_pfugidhack = 1;
1615                 }
1616 #endif
1617
1618                 rule->rpool.cur = TAILQ_FIRST(&rule->rpool.list);
1619                 rule->evaluations = rule->packets[0] = rule->packets[1] =
1620                     rule->bytes[0] = rule->bytes[1] = 0;
1621                 TAILQ_INSERT_TAIL(ruleset->rules[rs_num].inactive.ptr,
1622                     rule, entries);
1623                 ruleset->rules[rs_num].inactive.rcount++;
1624                 break;
1625         }
1626
1627         case DIOCGETRULES: {
1628                 struct pfioc_rule       *pr = (struct pfioc_rule *)addr;
1629                 struct pf_ruleset       *ruleset;
1630                 struct pf_rule          *tail;
1631                 int                      rs_num;
1632
1633                 pr->anchor[sizeof(pr->anchor) - 1] = 0;
1634                 ruleset = pf_find_ruleset(pr->anchor);
1635                 if (ruleset == NULL) {
1636                         error = EINVAL;
1637                         break;
1638                 }
1639                 rs_num = pf_get_ruleset_number(pr->rule.action);
1640                 if (rs_num >= PF_RULESET_MAX) {
1641                         error = EINVAL;
1642                         break;
1643                 }
1644                 tail = TAILQ_LAST(ruleset->rules[rs_num].active.ptr,
1645                     pf_rulequeue);
1646                 if (tail)
1647                         pr->nr = tail->nr + 1;
1648                 else
1649                         pr->nr = 0;
1650                 pr->ticket = ruleset->rules[rs_num].active.ticket;
1651                 break;
1652         }
1653
1654         case DIOCGETRULE: {
1655                 struct pfioc_rule       *pr = (struct pfioc_rule *)addr;
1656                 struct pf_ruleset       *ruleset;
1657                 struct pf_rule          *rule;
1658                 int                      rs_num, i;
1659
1660                 pr->anchor[sizeof(pr->anchor) - 1] = 0;
1661                 ruleset = pf_find_ruleset(pr->anchor);
1662                 if (ruleset == NULL) {
1663                         error = EINVAL;
1664                         break;
1665                 }
1666                 rs_num = pf_get_ruleset_number(pr->rule.action);
1667                 if (rs_num >= PF_RULESET_MAX) {
1668                         error = EINVAL;
1669                         break;
1670                 }
1671                 if (pr->ticket != ruleset->rules[rs_num].active.ticket) {
1672                         error = EBUSY;
1673                         break;
1674                 }
1675                 rule = TAILQ_FIRST(ruleset->rules[rs_num].active.ptr);
1676                 while ((rule != NULL) && (rule->nr != pr->nr))
1677                         rule = TAILQ_NEXT(rule, entries);
1678                 if (rule == NULL) {
1679                         error = EBUSY;
1680                         break;
1681                 }
1682                 bcopy(rule, &pr->rule, sizeof(struct pf_rule));
1683                 if (pf_anchor_copyout(ruleset, rule, pr)) {
1684                         error = EBUSY;
1685                         break;
1686                 }
1687                 pfi_dynaddr_copyout(&pr->rule.src.addr);
1688                 pfi_dynaddr_copyout(&pr->rule.dst.addr);
1689                 pf_tbladdr_copyout(&pr->rule.src.addr);
1690                 pf_tbladdr_copyout(&pr->rule.dst.addr);
1691                 pf_rtlabel_copyout(&pr->rule.src.addr);
1692                 pf_rtlabel_copyout(&pr->rule.dst.addr);
1693                 for (i = 0; i < PF_SKIP_COUNT; ++i)
1694                         if (rule->skip[i].ptr == NULL)
1695                                 pr->rule.skip[i].nr = -1;
1696                         else
1697                                 pr->rule.skip[i].nr =
1698                                     rule->skip[i].ptr->nr;
1699
1700                 if (pr->action == PF_GET_CLR_CNTR) {
1701                         rule->evaluations = 0;
1702                         rule->packets[0] = rule->packets[1] = 0;
1703                         rule->bytes[0] = rule->bytes[1] = 0;
1704                 }
1705                 break;
1706         }
1707
1708         case DIOCCHANGERULE: {
1709                 struct pfioc_rule       *pcr = (struct pfioc_rule *)addr;
1710                 struct pf_ruleset       *ruleset;
1711                 struct pf_rule          *oldrule = NULL, *newrule = NULL;
1712                 u_int32_t                nr = 0;
1713                 int                      rs_num;
1714
1715                 if (!(pcr->action == PF_CHANGE_REMOVE ||
1716                     pcr->action == PF_CHANGE_GET_TICKET) &&
1717                     pcr->pool_ticket != ticket_pabuf) {
1718                         error = EBUSY;
1719                         break;
1720                 }
1721
1722                 if (pcr->action < PF_CHANGE_ADD_HEAD ||
1723                     pcr->action > PF_CHANGE_GET_TICKET) {
1724                         error = EINVAL;
1725                         break;
1726                 }
1727                 ruleset = pf_find_ruleset(pcr->anchor);
1728                 if (ruleset == NULL) {
1729                         error = EINVAL;
1730                         break;
1731                 }
1732                 rs_num = pf_get_ruleset_number(pcr->rule.action);
1733                 if (rs_num >= PF_RULESET_MAX) {
1734                         error = EINVAL;
1735                         break;
1736                 }
1737
1738                 if (pcr->action == PF_CHANGE_GET_TICKET) {
1739                         pcr->ticket = ++ruleset->rules[rs_num].active.ticket;
1740                         break;
1741                 } else {
1742                         if (pcr->ticket !=
1743                             ruleset->rules[rs_num].active.ticket) {
1744                                 error = EINVAL;
1745                                 break;
1746                         }
1747                         if (pcr->rule.return_icmp >> 8 > ICMP_MAXTYPE) {
1748                                 error = EINVAL;
1749                                 break;
1750                         }
1751                 }
1752
1753                 if (pcr->action != PF_CHANGE_REMOVE) {
1754                         newrule = pool_get(&pf_rule_pl, PR_NOWAIT);
1755                         if (newrule == NULL) {
1756                                 error = ENOMEM;
1757                                 break;
1758                         }
1759                         bcopy(&pcr->rule, newrule, sizeof(struct pf_rule));
1760 #ifdef __FreeBSD__
1761                         newrule->cuid = td->td_ucred->cr_ruid;
1762                         newrule->cpid = td->td_proc ? td->td_proc->p_pid : 0;
1763 #else
1764                         newrule->cuid = p->p_cred->p_ruid;
1765                         newrule->cpid = p->p_pid;
1766 #endif
1767                         TAILQ_INIT(&newrule->rpool.list);
1768                         /* initialize refcounting */
1769                         newrule->states = 0;
1770                         newrule->entries.tqe_prev = NULL;
1771 #ifndef INET
1772                         if (newrule->af == AF_INET) {
1773                                 pool_put(&pf_rule_pl, newrule);
1774                                 error = EAFNOSUPPORT;
1775                                 break;
1776                         }
1777 #endif /* INET */
1778 #ifndef INET6
1779                         if (newrule->af == AF_INET6) {
1780                                 pool_put(&pf_rule_pl, newrule);
1781                                 error = EAFNOSUPPORT;
1782                                 break;
1783                         }
1784 #endif /* INET6 */
1785                         if (newrule->ifname[0]) {
1786                                 newrule->kif = pfi_kif_get(newrule->ifname);
1787                                 if (newrule->kif == NULL) {
1788                                         pool_put(&pf_rule_pl, newrule);
1789                                         error = EINVAL;
1790                                         break;
1791                                 }
1792                                 pfi_kif_ref(newrule->kif, PFI_KIF_REF_RULE);
1793                         } else
1794                                 newrule->kif = NULL;
1795
1796                         if (newrule->rtableid > 0 &&
1797 #ifdef __FreeBSD__ /* ROUTING */
1798                             newrule->rtableid > rt_numfibs)
1799 #else
1800                             !rtable_exists(newrule->rtableid))
1801 #endif
1802                                 error = EBUSY;
1803
1804 #ifdef ALTQ
1805                         /* set queue IDs */
1806                         if (newrule->qname[0] != 0) {
1807                                 if ((newrule->qid =
1808                                     pf_qname2qid(newrule->qname)) == 0)
1809                                         error = EBUSY;
1810                                 else if (newrule->pqname[0] != 0) {
1811                                         if ((newrule->pqid =
1812                                             pf_qname2qid(newrule->pqname)) == 0)
1813                                                 error = EBUSY;
1814                                 } else
1815                                         newrule->pqid = newrule->qid;
1816                         }
1817 #endif /* ALTQ */
1818                         if (newrule->tagname[0])
1819                                 if ((newrule->tag =
1820                                     pf_tagname2tag(newrule->tagname)) == 0)
1821                                         error = EBUSY;
1822                         if (newrule->match_tagname[0])
1823                                 if ((newrule->match_tag = pf_tagname2tag(
1824                                     newrule->match_tagname)) == 0)
1825                                         error = EBUSY;
1826                         if (newrule->rt && !newrule->direction)
1827                                 error = EINVAL;
1828 #ifdef __FreeBSD__
1829 #if NPFLOG > 0
1830                         if (!newrule->log)
1831                                 newrule->logif = 0;
1832                         if (newrule->logif >= PFLOGIFS_MAX)
1833                                 error = EINVAL;
1834 #endif
1835 #endif
1836                         if (pf_rtlabel_add(&newrule->src.addr) ||
1837                             pf_rtlabel_add(&newrule->dst.addr))
1838                                 error = EBUSY;
1839                         if (pfi_dynaddr_setup(&newrule->src.addr, newrule->af))
1840                                 error = EINVAL;
1841                         if (pfi_dynaddr_setup(&newrule->dst.addr, newrule->af))
1842                                 error = EINVAL;
1843                         if (pf_tbladdr_setup(ruleset, &newrule->src.addr))
1844                                 error = EINVAL;
1845                         if (pf_tbladdr_setup(ruleset, &newrule->dst.addr))
1846                                 error = EINVAL;
1847                         if (pf_anchor_setup(newrule, ruleset, pcr->anchor_call))
1848                                 error = EINVAL;
1849                         TAILQ_FOREACH(pa, &pf_pabuf, entries)
1850                                 if (pf_tbladdr_setup(ruleset, &pa->addr))
1851                                         error = EINVAL;
1852
1853                         if (newrule->overload_tblname[0]) {
1854                                 if ((newrule->overload_tbl = pfr_attach_table(
1855                                     ruleset, newrule->overload_tblname)) ==
1856                                     NULL)
1857                                         error = EINVAL;
1858                                 else
1859                                         newrule->overload_tbl->pfrkt_flags |=
1860                                             PFR_TFLAG_ACTIVE;
1861                         }
1862
1863                         pf_mv_pool(&pf_pabuf, &newrule->rpool.list);
1864                         if (((((newrule->action == PF_NAT) ||
1865                             (newrule->action == PF_RDR) ||
1866                             (newrule->action == PF_BINAT) ||
1867                             (newrule->rt > PF_FASTROUTE)) &&
1868                             !newrule->anchor)) &&
1869                             (TAILQ_FIRST(&newrule->rpool.list) == NULL))
1870                                 error = EINVAL;
1871
1872                         if (error) {
1873                                 pf_rm_rule(NULL, newrule);
1874                                 break;
1875                         }
1876
1877 #ifdef __FreeBSD__
1878                         if (!debug_pfugidhack && (newrule->uid.op ||
1879                             newrule->gid.op ||
1880                             newrule->log & PF_LOG_SOCKET_LOOKUP)) {
1881                                 DPFPRINTF(PF_DEBUG_MISC,
1882                                     ("pf: debug.pfugidhack enabled\n"));
1883                                 debug_pfugidhack = 1;
1884                         }
1885 #endif
1886
1887                         newrule->rpool.cur = TAILQ_FIRST(&newrule->rpool.list);
1888                         newrule->evaluations = 0;
1889                         newrule->packets[0] = newrule->packets[1] = 0;
1890                         newrule->bytes[0] = newrule->bytes[1] = 0;
1891                 }
1892                 pf_empty_pool(&pf_pabuf);
1893
1894                 if (pcr->action == PF_CHANGE_ADD_HEAD)
1895                         oldrule = TAILQ_FIRST(
1896                             ruleset->rules[rs_num].active.ptr);
1897                 else if (pcr->action == PF_CHANGE_ADD_TAIL)
1898                         oldrule = TAILQ_LAST(
1899                             ruleset->rules[rs_num].active.ptr, pf_rulequeue);
1900                 else {
1901                         oldrule = TAILQ_FIRST(
1902                             ruleset->rules[rs_num].active.ptr);
1903                         while ((oldrule != NULL) && (oldrule->nr != pcr->nr))
1904                                 oldrule = TAILQ_NEXT(oldrule, entries);
1905                         if (oldrule == NULL) {
1906                                 if (newrule != NULL)
1907                                         pf_rm_rule(NULL, newrule);
1908                                 error = EINVAL;
1909                                 break;
1910                         }
1911                 }
1912
1913                 if (pcr->action == PF_CHANGE_REMOVE) {
1914                         pf_rm_rule(ruleset->rules[rs_num].active.ptr, oldrule);
1915                         ruleset->rules[rs_num].active.rcount--;
1916                 } else {
1917                         if (oldrule == NULL)
1918                                 TAILQ_INSERT_TAIL(
1919                                     ruleset->rules[rs_num].active.ptr,
1920                                     newrule, entries);
1921                         else if (pcr->action == PF_CHANGE_ADD_HEAD ||
1922                             pcr->action == PF_CHANGE_ADD_BEFORE)
1923                                 TAILQ_INSERT_BEFORE(oldrule, newrule, entries);
1924                         else
1925                                 TAILQ_INSERT_AFTER(
1926                                     ruleset->rules[rs_num].active.ptr,
1927                                     oldrule, newrule, entries);
1928                         ruleset->rules[rs_num].active.rcount++;
1929                 }
1930
1931                 nr = 0;
1932                 TAILQ_FOREACH(oldrule,
1933                     ruleset->rules[rs_num].active.ptr, entries)
1934                         oldrule->nr = nr++;
1935
1936                 ruleset->rules[rs_num].active.ticket++;
1937
1938                 pf_calc_skip_steps(ruleset->rules[rs_num].active.ptr);
1939                 pf_remove_if_empty_ruleset(ruleset);
1940
1941                 break;
1942         }
1943
1944         case DIOCCLRSTATES: {
1945                 struct pf_state         *state, *nexts;
1946                 struct pfioc_state_kill *psk = (struct pfioc_state_kill *)addr;
1947                 int                      killed = 0;
1948
1949                 for (state = RB_MIN(pf_state_tree_id, &tree_id); state;
1950                     state = nexts) {
1951                         nexts = RB_NEXT(pf_state_tree_id, &tree_id, state);
1952
1953                         if (!psk->psk_ifname[0] || !strcmp(psk->psk_ifname,
1954                             state->u.s.kif->pfik_name)) {
1955 #if NPFSYNC
1956                                 /* don't send out individual delete messages */
1957                                 state->sync_flags = PFSTATE_NOSYNC;
1958 #endif
1959                                 pf_unlink_state(state);
1960                                 killed++;
1961                         }
1962                 }
1963                 psk->psk_af = killed;
1964 #if NPFSYNC
1965                 pfsync_clear_states(pf_status.hostid, psk->psk_ifname);
1966 #endif
1967                 break;
1968         }
1969
1970         case DIOCKILLSTATES: {
1971                 struct pf_state         *state, *nexts;
1972                 struct pf_state_host    *src, *dst;
1973                 struct pfioc_state_kill *psk = (struct pfioc_state_kill *)addr;
1974                 int                      killed = 0;
1975
1976                 for (state = RB_MIN(pf_state_tree_id, &tree_id); state;
1977                     state = nexts) {
1978                         nexts = RB_NEXT(pf_state_tree_id, &tree_id, state);
1979
1980                         if (state->direction == PF_OUT) {
1981                                 src = &state->lan;
1982                                 dst = &state->ext;
1983                         } else {
1984                                 src = &state->ext;
1985                                 dst = &state->lan;
1986                         }
1987                         if ((!psk->psk_af || state->af == psk->psk_af)
1988                             && (!psk->psk_proto || psk->psk_proto ==
1989                             state->proto) &&
1990                             PF_MATCHA(psk->psk_src.neg,
1991                             &psk->psk_src.addr.v.a.addr,
1992                             &psk->psk_src.addr.v.a.mask,
1993                             &src->addr, state->af) &&
1994                             PF_MATCHA(psk->psk_dst.neg,
1995                             &psk->psk_dst.addr.v.a.addr,
1996                             &psk->psk_dst.addr.v.a.mask,
1997                             &dst->addr, state->af) &&
1998                             (psk->psk_src.port_op == 0 ||
1999                             pf_match_port(psk->psk_src.port_op,
2000                             psk->psk_src.port[0], psk->psk_src.port[1],
2001                             src->port)) &&
2002                             (psk->psk_dst.port_op == 0 ||
2003                             pf_match_port(psk->psk_dst.port_op,
2004                             psk->psk_dst.port[0], psk->psk_dst.port[1],
2005                             dst->port)) &&
2006                             (!psk->psk_ifname[0] || !strcmp(psk->psk_ifname,
2007                             state->u.s.kif->pfik_name))) {
2008 #if NPFSYNC > 0
2009                                 /* send immediate delete of state */
2010                                 pfsync_delete_state(state);
2011                                 state->sync_flags |= PFSTATE_NOSYNC;
2012 #endif
2013                                 pf_unlink_state(state);
2014                                 killed++;
2015                         }
2016                 }
2017                 psk->psk_af = killed;
2018                 break;
2019         }
2020
2021         case DIOCADDSTATE: {
2022                 struct pfioc_state      *ps = (struct pfioc_state *)addr;
2023                 struct pf_state         *state;
2024                 struct pfi_kif          *kif;
2025
2026                 if (ps->state.timeout >= PFTM_MAX &&
2027                     ps->state.timeout != PFTM_UNTIL_PACKET) {
2028                         error = EINVAL;
2029                         break;
2030                 }
2031                 state = pool_get(&pf_state_pl, PR_NOWAIT);
2032                 if (state == NULL) {
2033                         error = ENOMEM;
2034                         break;
2035                 }
2036                 kif = pfi_kif_get(ps->state.u.ifname);
2037                 if (kif == NULL) {
2038                         pool_put(&pf_state_pl, state);
2039                         error = ENOENT;
2040                         break;
2041                 }
2042                 bcopy(&ps->state, state, sizeof(struct pf_state));
2043                 bzero(&state->u, sizeof(state->u));
2044                 state->rule.ptr = &pf_default_rule;
2045                 state->nat_rule.ptr = NULL;
2046                 state->anchor.ptr = NULL;
2047                 state->rt_kif = NULL;
2048                 state->creation = time_second;
2049                 state->pfsync_time = 0;
2050                 state->packets[0] = state->packets[1] = 0;
2051                 state->bytes[0] = state->bytes[1] = 0;
2052
2053                 if (pf_insert_state(kif, state)) {
2054                         pfi_kif_unref(kif, PFI_KIF_REF_NONE);
2055                         pool_put(&pf_state_pl, state);
2056                         error = ENOMEM;
2057                 }
2058                 break;
2059         }
2060
2061         case DIOCGETSTATE: {
2062                 struct pfioc_state      *ps = (struct pfioc_state *)addr;
2063                 struct pf_state         *state;
2064                 u_int32_t                nr;
2065                 int                      secs;
2066
2067                 nr = 0;
2068                 RB_FOREACH(state, pf_state_tree_id, &tree_id) {
2069                         if (nr >= ps->nr)
2070                                 break;
2071                         nr++;
2072                 }
2073                 if (state == NULL) {
2074                         error = EBUSY;
2075                         break;
2076                 }
2077                 secs = time_second;
2078                 bcopy(state, &ps->state, sizeof(ps->state));
2079                 strlcpy(ps->state.u.ifname, state->u.s.kif->pfik_name,
2080                     sizeof(ps->state.u.ifname));
2081                 ps->state.rule.nr = state->rule.ptr->nr;
2082                 ps->state.nat_rule.nr = (state->nat_rule.ptr == NULL) ?
2083                     -1 : state->nat_rule.ptr->nr;
2084                 ps->state.anchor.nr = (state->anchor.ptr == NULL) ?
2085                     -1 : state->anchor.ptr->nr;
2086                 ps->state.creation = secs - ps->state.creation;
2087                 ps->state.expire = pf_state_expires(state);
2088                 if (ps->state.expire > secs)
2089                         ps->state.expire -= secs;
2090                 else
2091                         ps->state.expire = 0;
2092                 break;
2093         }
2094
2095         case DIOCGETSTATES: {
2096                 struct pfioc_states     *ps = (struct pfioc_states *)addr;
2097                 struct pf_state         *state;
2098                 struct pf_state         *p, *pstore;
2099                 u_int32_t                nr = 0;
2100                 int                      space = ps->ps_len;
2101
2102                 if (space == 0) {
2103                         nr = pf_status.states;
2104                         ps->ps_len = sizeof(struct pf_state) * nr;
2105                         break;
2106                 }
2107
2108 #ifdef __FreeBSD__
2109                 PF_UNLOCK();
2110 #endif
2111                 pstore = malloc(sizeof(*pstore), M_TEMP, M_WAITOK);
2112 #ifdef __FreeBSD__
2113                 PF_LOCK();
2114 #endif
2115
2116                 p = ps->ps_states;
2117
2118                 state = TAILQ_FIRST(&state_list);
2119                 while (state) {
2120                         if (state->timeout != PFTM_UNLINKED) {
2121                                 int     secs = time_second;
2122
2123                                 if ((nr+1) * sizeof(*p) > (unsigned)ps->ps_len)
2124                                         break;
2125
2126                                 bcopy(state, pstore, sizeof(*pstore));
2127                                 strlcpy(pstore->u.ifname,
2128                                     state->u.s.kif->pfik_name,
2129                                     sizeof(pstore->u.ifname));
2130                                 pstore->rule.nr = state->rule.ptr->nr;
2131                                 pstore->nat_rule.nr = (state->nat_rule.ptr ==
2132                                     NULL) ? -1 : state->nat_rule.ptr->nr;
2133                                 pstore->anchor.nr = (state->anchor.ptr ==
2134                                     NULL) ? -1 : state->anchor.ptr->nr;
2135                                 pstore->creation = secs - pstore->creation;
2136                                 pstore->expire = pf_state_expires(state);
2137                                 if (pstore->expire > secs)
2138                                         pstore->expire -= secs;
2139                                 else
2140                                         pstore->expire = 0;
2141 #ifdef __FreeBSD__
2142                                 PF_COPYOUT(pstore, p, sizeof(*p), error);
2143 #else
2144                                 error = copyout(pstore, p, sizeof(*p));
2145 #endif
2146                                 if (error) {
2147                                         free(pstore, M_TEMP);
2148                                         goto fail;
2149                                 }
2150                                 p++;
2151                                 nr++;
2152                         }
2153                         state = TAILQ_NEXT(state, u.s.entry_list);
2154                 }
2155
2156                 ps->ps_len = sizeof(struct pf_state) * nr;
2157
2158                 free(pstore, M_TEMP);
2159                 break;
2160         }
2161
2162         case DIOCGETSTATUS: {
2163                 struct pf_status *s = (struct pf_status *)addr;
2164                 bcopy(&pf_status, s, sizeof(struct pf_status));
2165                 pfi_fill_oldstatus(s);
2166                 break;
2167         }
2168
2169         case DIOCSETSTATUSIF: {
2170                 struct pfioc_if *pi = (struct pfioc_if *)addr;
2171
2172                 if (pi->ifname[0] == 0) {
2173                         bzero(pf_status.ifname, IFNAMSIZ);
2174                         break;
2175                 }
2176                 if (ifunit(pi->ifname) == NULL) {
2177                         error = EINVAL;
2178                         break;
2179                 }
2180                 strlcpy(pf_status.ifname, pi->ifname, IFNAMSIZ);
2181                 break;
2182         }
2183
2184         case DIOCCLRSTATUS: {
2185                 bzero(pf_status.counters, sizeof(pf_status.counters));
2186                 bzero(pf_status.fcounters, sizeof(pf_status.fcounters));
2187                 bzero(pf_status.scounters, sizeof(pf_status.scounters));
2188                 pf_status.since = time_second;
2189                 if (*pf_status.ifname)
2190                         pfi_clr_istats(pf_status.ifname);
2191                 break;
2192         }
2193
2194         case DIOCNATLOOK: {
2195                 struct pfioc_natlook    *pnl = (struct pfioc_natlook *)addr;
2196                 struct pf_state         *state;
2197                 struct pf_state_cmp      key;
2198                 int                      m = 0, direction = pnl->direction;
2199
2200                 key.af = pnl->af;
2201                 key.proto = pnl->proto;
2202
2203                 if (!pnl->proto ||
2204                     PF_AZERO(&pnl->saddr, pnl->af) ||
2205                     PF_AZERO(&pnl->daddr, pnl->af) ||
2206                     ((pnl->proto == IPPROTO_TCP ||
2207                     pnl->proto == IPPROTO_UDP) &&
2208                     (!pnl->dport || !pnl->sport)))
2209                         error = EINVAL;
2210                 else {
2211                         /*
2212                          * userland gives us source and dest of connection,
2213                          * reverse the lookup so we ask for what happens with
2214                          * the return traffic, enabling us to find it in the
2215                          * state tree.
2216                          */
2217                         if (direction == PF_IN) {
2218                                 PF_ACPY(&key.ext.addr, &pnl->daddr, pnl->af);
2219                                 key.ext.port = pnl->dport;
2220                                 PF_ACPY(&key.gwy.addr, &pnl->saddr, pnl->af);
2221                                 key.gwy.port = pnl->sport;
2222                                 state = pf_find_state_all(&key, PF_EXT_GWY, &m);
2223                         } else {
2224                                 PF_ACPY(&key.lan.addr, &pnl->daddr, pnl->af);
2225                                 key.lan.port = pnl->dport;
2226                                 PF_ACPY(&key.ext.addr, &pnl->saddr, pnl->af);
2227                                 key.ext.port = pnl->sport;
2228                                 state = pf_find_state_all(&key, PF_LAN_EXT, &m);
2229                         }
2230                         if (m > 1)
2231                                 error = E2BIG;  /* more than one state */
2232                         else if (state != NULL) {
2233                                 if (direction == PF_IN) {
2234                                         PF_ACPY(&pnl->rsaddr, &state->lan.addr,
2235                                             state->af);
2236                                         pnl->rsport = state->lan.port;
2237                                         PF_ACPY(&pnl->rdaddr, &pnl->daddr,
2238                                             pnl->af);
2239                                         pnl->rdport = pnl->dport;
2240                                 } else {
2241                                         PF_ACPY(&pnl->rdaddr, &state->gwy.addr,
2242                                             state->af);
2243                                         pnl->rdport = state->gwy.port;
2244                                         PF_ACPY(&pnl->rsaddr, &pnl->saddr,
2245                                             pnl->af);
2246                                         pnl->rsport = pnl->sport;
2247                                 }
2248                         } else
2249                                 error = ENOENT;
2250                 }
2251                 break;
2252         }
2253
2254         case DIOCSETTIMEOUT: {
2255                 struct pfioc_tm *pt = (struct pfioc_tm *)addr;
2256                 int              old;
2257
2258                 if (pt->timeout < 0 || pt->timeout >= PFTM_MAX ||
2259                     pt->seconds < 0) {
2260                         error = EINVAL;
2261                         goto fail;
2262                 }
2263                 old = pf_default_rule.timeout[pt->timeout];
2264                 if (pt->timeout == PFTM_INTERVAL && pt->seconds == 0)
2265                         pt->seconds = 1;
2266                 pf_default_rule.timeout[pt->timeout] = pt->seconds;
2267                 if (pt->timeout == PFTM_INTERVAL && pt->seconds < old)
2268                         wakeup(pf_purge_thread);
2269                 pt->seconds = old;
2270                 break;
2271         }
2272
2273         case DIOCGETTIMEOUT: {
2274                 struct pfioc_tm *pt = (struct pfioc_tm *)addr;
2275
2276                 if (pt->timeout < 0 || pt->timeout >= PFTM_MAX) {
2277                         error = EINVAL;
2278                         goto fail;
2279                 }
2280                 pt->seconds = pf_default_rule.timeout[pt->timeout];
2281                 break;
2282         }
2283
2284         case DIOCGETLIMIT: {
2285                 struct pfioc_limit      *pl = (struct pfioc_limit *)addr;
2286
2287                 if (pl->index < 0 || pl->index >= PF_LIMIT_MAX) {
2288                         error = EINVAL;
2289                         goto fail;
2290                 }
2291                 pl->limit = pf_pool_limits[pl->index].limit;
2292                 break;
2293         }
2294
2295         case DIOCSETLIMIT: {
2296                 struct pfioc_limit      *pl = (struct pfioc_limit *)addr;
2297                 int                      old_limit;
2298
2299                 if (pl->index < 0 || pl->index >= PF_LIMIT_MAX ||
2300                     pf_pool_limits[pl->index].pp == NULL) {
2301                         error = EINVAL;
2302                         goto fail;
2303                 }
2304 #ifdef __FreeBSD__
2305                 uma_zone_set_max(pf_pool_limits[pl->index].pp, pl->limit);
2306 #else
2307                 if (pool_sethardlimit(pf_pool_limits[pl->index].pp,
2308                     pl->limit, NULL, 0) != 0) {
2309                         error = EBUSY;
2310                         goto fail;
2311                 }
2312 #endif
2313                 old_limit = pf_pool_limits[pl->index].limit;
2314                 pf_pool_limits[pl->index].limit = pl->limit;
2315                 pl->limit = old_limit;
2316                 break;
2317         }
2318
2319         case DIOCSETDEBUG: {
2320                 u_int32_t       *level = (u_int32_t *)addr;
2321
2322                 pf_status.debug = *level;
2323                 break;
2324         }
2325
2326         case DIOCCLRRULECTRS: {
2327                 /* obsoleted by DIOCGETRULE with action=PF_GET_CLR_CNTR */
2328                 struct pf_ruleset       *ruleset = &pf_main_ruleset;
2329                 struct pf_rule          *rule;
2330
2331                 TAILQ_FOREACH(rule,
2332                     ruleset->rules[PF_RULESET_FILTER].active.ptr, entries) {
2333                         rule->evaluations = 0;
2334                         rule->packets[0] = rule->packets[1] = 0;
2335                         rule->bytes[0] = rule->bytes[1] = 0;
2336                 }
2337                 break;
2338         }
2339
2340 #ifdef __FreeBSD__
2341         case DIOCGIFSPEED: {
2342                 struct pf_ifspeed       *psp = (struct pf_ifspeed *)addr;
2343                 struct pf_ifspeed       ps;
2344                 struct ifnet            *ifp;
2345
2346                 if (psp->ifname[0] != 0) {
2347                         /* Can we completely trust user-land? */
2348                         strlcpy(ps.ifname, psp->ifname, IFNAMSIZ);
2349                         ifp = ifunit(ps.ifname);
2350                         if (ifp != NULL)
2351                                 psp->baudrate = ifp->if_baudrate;
2352                         else
2353                                 error = EINVAL;
2354                 } else
2355                         error = EINVAL;
2356                 break;
2357         }
2358 #endif /* __FreeBSD__ */
2359
2360 #ifdef ALTQ
2361         case DIOCSTARTALTQ: {
2362                 struct pf_altq          *altq;
2363
2364                 /* enable all altq interfaces on active list */
2365                 TAILQ_FOREACH(altq, pf_altqs_active, entries) {
2366 #ifdef __FreeBSD__
2367                         if (altq->qname[0] == 0 && (altq->local_flags &
2368                             PFALTQ_FLAG_IF_REMOVED) == 0) {
2369 #else
2370                         if (altq->qname[0] == 0) {
2371 #endif
2372                                 error = pf_enable_altq(altq);
2373                                 if (error != 0)
2374                                         break;
2375                         }
2376                 }
2377                 if (error == 0)
2378                         pf_altq_running = 1;
2379                 DPFPRINTF(PF_DEBUG_MISC, ("altq: started\n"));
2380                 break;
2381         }
2382
2383         case DIOCSTOPALTQ: {
2384                 struct pf_altq          *altq;
2385
2386                 /* disable all altq interfaces on active list */
2387                 TAILQ_FOREACH(altq, pf_altqs_active, entries) {
2388 #ifdef __FreeBSD__
2389                         if (altq->qname[0] == 0 && (altq->local_flags &
2390                             PFALTQ_FLAG_IF_REMOVED) == 0) {
2391 #else
2392                         if (altq->qname[0] == 0) {
2393 #endif
2394                                 error = pf_disable_altq(altq);
2395                                 if (error != 0)
2396                                         break;
2397                         }
2398                 }
2399                 if (error == 0)
2400                         pf_altq_running = 0;
2401                 DPFPRINTF(PF_DEBUG_MISC, ("altq: stopped\n"));
2402                 break;
2403         }
2404
2405         case DIOCADDALTQ: {
2406                 struct pfioc_altq       *pa = (struct pfioc_altq *)addr;
2407                 struct pf_altq          *altq, *a;
2408
2409                 if (pa->ticket != ticket_altqs_inactive) {
2410                         error = EBUSY;
2411                         break;
2412                 }
2413                 altq = pool_get(&pf_altq_pl, PR_NOWAIT);
2414                 if (altq == NULL) {
2415                         error = ENOMEM;
2416                         break;
2417                 }
2418                 bcopy(&pa->altq, altq, sizeof(struct pf_altq));
2419 #ifdef __FreeBSD__
2420                 altq->local_flags = 0;
2421 #endif
2422
2423                 /*
2424                  * if this is for a queue, find the discipline and
2425                  * copy the necessary fields
2426                  */
2427                 if (altq->qname[0] != 0) {
2428                         if ((altq->qid = pf_qname2qid(altq->qname)) == 0) {
2429                                 error = EBUSY;
2430                                 pool_put(&pf_altq_pl, altq);
2431                                 break;
2432                         }
2433                         altq->altq_disc = NULL;
2434                         TAILQ_FOREACH(a, pf_altqs_inactive, entries) {
2435                                 if (strncmp(a->ifname, altq->ifname,
2436                                     IFNAMSIZ) == 0 && a->qname[0] == 0) {
2437                                         altq->altq_disc = a->altq_disc;
2438                                         break;
2439                                 }
2440                         }
2441                 }
2442
2443 #ifdef __FreeBSD__
2444                 struct ifnet *ifp;
2445
2446                 if ((ifp = ifunit(altq->ifname)) == NULL) {
2447                         altq->local_flags |= PFALTQ_FLAG_IF_REMOVED;
2448                 } else {
2449                         PF_UNLOCK();
2450 #endif          
2451                 error = altq_add(altq);
2452 #ifdef __FreeBSD__
2453                         PF_LOCK();
2454                 }
2455 #endif
2456                 if (error) {
2457                         pool_put(&pf_altq_pl, altq);
2458                         break;
2459                 }
2460
2461                 TAILQ_INSERT_TAIL(pf_altqs_inactive, altq, entries);
2462                 bcopy(altq, &pa->altq, sizeof(struct pf_altq));
2463                 break;
2464         }
2465
2466         case DIOCGETALTQS: {
2467                 struct pfioc_altq       *pa = (struct pfioc_altq *)addr;
2468                 struct pf_altq          *altq;
2469
2470                 pa->nr = 0;
2471                 TAILQ_FOREACH(altq, pf_altqs_active, entries)
2472                         pa->nr++;
2473                 pa->ticket = ticket_altqs_active;
2474                 break;
2475         }
2476
2477         case DIOCGETALTQ: {
2478                 struct pfioc_altq       *pa = (struct pfioc_altq *)addr;
2479                 struct pf_altq          *altq;
2480                 u_int32_t                nr;
2481
2482                 if (pa->ticket != ticket_altqs_active) {
2483                         error = EBUSY;
2484                         break;
2485                 }
2486                 nr = 0;
2487                 altq = TAILQ_FIRST(pf_altqs_active);
2488                 while ((altq != NULL) && (nr < pa->nr)) {
2489                         altq = TAILQ_NEXT(altq, entries);
2490                         nr++;
2491                 }
2492                 if (altq == NULL) {
2493                         error = EBUSY;
2494                         break;
2495                 }
2496                 bcopy(altq, &pa->altq, sizeof(struct pf_altq));
2497                 break;
2498         }
2499
2500         case DIOCCHANGEALTQ:
2501                 /* CHANGEALTQ not supported yet! */
2502                 error = ENODEV;
2503                 break;
2504
2505         case DIOCGETQSTATS: {
2506                 struct pfioc_qstats     *pq = (struct pfioc_qstats *)addr;
2507                 struct pf_altq          *altq;
2508                 u_int32_t                nr;
2509                 int                      nbytes;
2510
2511                 if (pq->ticket != ticket_altqs_active) {
2512                         error = EBUSY;
2513                         break;
2514                 }
2515                 nbytes = pq->nbytes;
2516                 nr = 0;
2517                 altq = TAILQ_FIRST(pf_altqs_active);
2518                 while ((altq != NULL) && (nr < pq->nr)) {
2519                         altq = TAILQ_NEXT(altq, entries);
2520                         nr++;
2521                 }
2522                 if (altq == NULL) {
2523                         error = EBUSY;
2524                         break;
2525                 }
2526 #ifdef __FreeBSD__
2527                 if ((altq->local_flags & PFALTQ_FLAG_IF_REMOVED) != 0) {
2528                         error = ENXIO;
2529                         break;
2530                 }
2531                 PF_UNLOCK();
2532 #endif
2533                 error = altq_getqstats(altq, pq->buf, &nbytes);
2534 #ifdef __FreeBSD__
2535                 PF_LOCK();
2536 #endif
2537                 if (error == 0) {
2538                         pq->scheduler = altq->scheduler;
2539                         pq->nbytes = nbytes;
2540                 }
2541                 break;
2542         }
2543 #endif /* ALTQ */
2544
2545         case DIOCBEGINADDRS: {
2546                 struct pfioc_pooladdr   *pp = (struct pfioc_pooladdr *)addr;
2547
2548                 pf_empty_pool(&pf_pabuf);
2549                 pp->ticket = ++ticket_pabuf;
2550                 break;
2551         }
2552
2553         case DIOCADDADDR: {
2554                 struct pfioc_pooladdr   *pp = (struct pfioc_pooladdr *)addr;
2555
2556                 if (pp->ticket != ticket_pabuf) {
2557                         error = EBUSY;
2558                         break;
2559                 }
2560 #ifndef INET
2561                 if (pp->af == AF_INET) {
2562                         error = EAFNOSUPPORT;
2563                         break;
2564                 }
2565 #endif /* INET */
2566 #ifndef INET6
2567                 if (pp->af == AF_INET6) {
2568                         error = EAFNOSUPPORT;
2569                         break;
2570                 }
2571 #endif /* INET6 */
2572                 if (pp->addr.addr.type != PF_ADDR_ADDRMASK &&
2573                     pp->addr.addr.type != PF_ADDR_DYNIFTL &&
2574                     pp->addr.addr.type != PF_ADDR_TABLE) {
2575                         error = EINVAL;
2576                         break;
2577                 }
2578                 pa = pool_get(&pf_pooladdr_pl, PR_NOWAIT);
2579                 if (pa == NULL) {
2580                         error = ENOMEM;
2581                         break;
2582                 }
2583                 bcopy(&pp->addr, pa, sizeof(struct pf_pooladdr));
2584                 if (pa->ifname[0]) {
2585                         pa->kif = pfi_kif_get(pa->ifname);
2586                         if (pa->kif == NULL) {
2587                                 pool_put(&pf_pooladdr_pl, pa);
2588                                 error = EINVAL;
2589                                 break;
2590                         }
2591                         pfi_kif_ref(pa->kif, PFI_KIF_REF_RULE);
2592                 }
2593                 if (pfi_dynaddr_setup(&pa->addr, pp->af)) {
2594                         pfi_dynaddr_remove(&pa->addr);
2595                         pfi_kif_unref(pa->kif, PFI_KIF_REF_RULE);
2596                         pool_put(&pf_pooladdr_pl, pa);
2597                         error = EINVAL;
2598                         break;
2599                 }
2600                 TAILQ_INSERT_TAIL(&pf_pabuf, pa, entries);
2601                 break;
2602         }
2603
2604         case DIOCGETADDRS: {
2605                 struct pfioc_pooladdr   *pp = (struct pfioc_pooladdr *)addr;
2606
2607                 pp->nr = 0;
2608                 pool = pf_get_pool(pp->anchor, pp->ticket, pp->r_action,
2609                     pp->r_num, 0, 1, 0);
2610                 if (pool == NULL) {
2611                         error = EBUSY;
2612                         break;
2613                 }
2614                 TAILQ_FOREACH(pa, &pool->list, entries)
2615                         pp->nr++;
2616                 break;
2617         }
2618
2619         case DIOCGETADDR: {
2620                 struct pfioc_pooladdr   *pp = (struct pfioc_pooladdr *)addr;
2621                 u_int32_t                nr = 0;
2622
2623                 pool = pf_get_pool(pp->anchor, pp->ticket, pp->r_action,
2624                     pp->r_num, 0, 1, 1);
2625                 if (pool == NULL) {
2626                         error = EBUSY;
2627                         break;
2628                 }
2629                 pa = TAILQ_FIRST(&pool->list);
2630                 while ((pa != NULL) && (nr < pp->nr)) {
2631                         pa = TAILQ_NEXT(pa, entries);
2632                         nr++;
2633                 }
2634                 if (pa == NULL) {
2635                         error = EBUSY;
2636                         break;
2637                 }
2638                 bcopy(pa, &pp->addr, sizeof(struct pf_pooladdr));
2639                 pfi_dynaddr_copyout(&pp->addr.addr);
2640                 pf_tbladdr_copyout(&pp->addr.addr);
2641                 pf_rtlabel_copyout(&pp->addr.addr);
2642                 break;
2643         }
2644
2645         case DIOCCHANGEADDR: {
2646                 struct pfioc_pooladdr   *pca = (struct pfioc_pooladdr *)addr;
2647                 struct pf_pooladdr      *oldpa = NULL, *newpa = NULL;
2648                 struct pf_ruleset       *ruleset;
2649
2650                 if (pca->action < PF_CHANGE_ADD_HEAD ||
2651                     pca->action > PF_CHANGE_REMOVE) {
2652                         error = EINVAL;
2653                         break;
2654                 }
2655                 if (pca->addr.addr.type != PF_ADDR_ADDRMASK &&
2656                     pca->addr.addr.type != PF_ADDR_DYNIFTL &&
2657                     pca->addr.addr.type != PF_ADDR_TABLE) {
2658                         error = EINVAL;
2659                         break;
2660                 }
2661
2662                 ruleset = pf_find_ruleset(pca->anchor);
2663                 if (ruleset == NULL) {
2664                         error = EBUSY;
2665                         break;
2666                 }
2667                 pool = pf_get_pool(pca->anchor, pca->ticket, pca->r_action,
2668                     pca->r_num, pca->r_last, 1, 1);
2669                 if (pool == NULL) {
2670                         error = EBUSY;
2671                         break;
2672                 }
2673                 if (pca->action != PF_CHANGE_REMOVE) {
2674                         newpa = pool_get(&pf_pooladdr_pl, PR_NOWAIT);
2675                         if (newpa == NULL) {
2676                                 error = ENOMEM;
2677                                 break;
2678                         }
2679                         bcopy(&pca->addr, newpa, sizeof(struct pf_pooladdr));
2680 #ifndef INET
2681                         if (pca->af == AF_INET) {
2682                                 pool_put(&pf_pooladdr_pl, newpa);
2683                                 error = EAFNOSUPPORT;
2684                                 break;
2685                         }
2686 #endif /* INET */
2687 #ifndef INET6
2688                         if (pca->af == AF_INET6) {
2689                                 pool_put(&pf_pooladdr_pl, newpa);
2690                                 error = EAFNOSUPPORT;
2691                                 break;
2692                         }
2693 #endif /* INET6 */
2694                         if (newpa->ifname[0]) {
2695                                 newpa->kif = pfi_kif_get(newpa->ifname);
2696                                 if (newpa->kif == NULL) {
2697                                         pool_put(&pf_pooladdr_pl, newpa);
2698                                         error = EINVAL;
2699                                         break;
2700                                 }
2701                                 pfi_kif_ref(newpa->kif, PFI_KIF_REF_RULE);
2702                         } else
2703                                 newpa->kif = NULL;
2704                         if (pfi_dynaddr_setup(&newpa->addr, pca->af) ||
2705                             pf_tbladdr_setup(ruleset, &newpa->addr)) {
2706                                 pfi_dynaddr_remove(&newpa->addr);
2707                                 pfi_kif_unref(newpa->kif, PFI_KIF_REF_RULE);
2708                                 pool_put(&pf_pooladdr_pl, newpa);
2709                                 error = EINVAL;
2710                                 break;
2711                         }
2712                 }
2713
2714                 if (pca->action == PF_CHANGE_ADD_HEAD)
2715                         oldpa = TAILQ_FIRST(&pool->list);
2716                 else if (pca->action == PF_CHANGE_ADD_TAIL)
2717                         oldpa = TAILQ_LAST(&pool->list, pf_palist);
2718                 else {
2719                         int     i = 0;
2720
2721                         oldpa = TAILQ_FIRST(&pool->list);
2722                         while ((oldpa != NULL) && (i < pca->nr)) {
2723                                 oldpa = TAILQ_NEXT(oldpa, entries);
2724                                 i++;
2725                         }
2726                         if (oldpa == NULL) {
2727                                 error = EINVAL;
2728                                 break;
2729                         }
2730                 }
2731
2732                 if (pca->action == PF_CHANGE_REMOVE) {
2733                         TAILQ_REMOVE(&pool->list, oldpa, entries);
2734                         pfi_dynaddr_remove(&oldpa->addr);
2735                         pf_tbladdr_remove(&oldpa->addr);
2736                         pfi_kif_unref(oldpa->kif, PFI_KIF_REF_RULE);
2737                         pool_put(&pf_pooladdr_pl, oldpa);
2738                 } else {
2739                         if (oldpa == NULL)
2740                                 TAILQ_INSERT_TAIL(&pool->list, newpa, entries);
2741                         else if (pca->action == PF_CHANGE_ADD_HEAD ||
2742                             pca->action == PF_CHANGE_ADD_BEFORE)
2743                                 TAILQ_INSERT_BEFORE(oldpa, newpa, entries);
2744                         else
2745                                 TAILQ_INSERT_AFTER(&pool->list, oldpa,
2746                                     newpa, entries);
2747                 }
2748
2749                 pool->cur = TAILQ_FIRST(&pool->list);
2750                 PF_ACPY(&pool->counter, &pool->cur->addr.v.a.addr,
2751                     pca->af);
2752                 break;
2753         }
2754
2755         case DIOCGETRULESETS: {
2756                 struct pfioc_ruleset    *pr = (struct pfioc_ruleset *)addr;
2757                 struct pf_ruleset       *ruleset;
2758                 struct pf_anchor        *anchor;
2759
2760                 pr->path[sizeof(pr->path) - 1] = 0;
2761                 if ((ruleset = pf_find_ruleset(pr->path)) == NULL) {
2762                         error = EINVAL;
2763                         break;
2764                 }
2765                 pr->nr = 0;
2766                 if (ruleset->anchor == NULL) {
2767                         /* XXX kludge for pf_main_ruleset */
2768                         RB_FOREACH(anchor, pf_anchor_global, &pf_anchors)
2769                                 if (anchor->parent == NULL)
2770                                         pr->nr++;
2771                 } else {
2772                         RB_FOREACH(anchor, pf_anchor_node,
2773                             &ruleset->anchor->children)
2774                                 pr->nr++;
2775                 }
2776                 break;
2777         }
2778
2779         case DIOCGETRULESET: {
2780                 struct pfioc_ruleset    *pr = (struct pfioc_ruleset *)addr;
2781                 struct pf_ruleset       *ruleset;
2782                 struct pf_anchor        *anchor;
2783                 u_int32_t                nr = 0;
2784
2785                 pr->path[sizeof(pr->path) - 1] = 0;
2786                 if ((ruleset = pf_find_ruleset(pr->path)) == NULL) {
2787                         error = EINVAL;
2788                         break;
2789                 }
2790                 pr->name[0] = 0;
2791                 if (ruleset->anchor == NULL) {
2792                         /* XXX kludge for pf_main_ruleset */
2793                         RB_FOREACH(anchor, pf_anchor_global, &pf_anchors)
2794                                 if (anchor->parent == NULL && nr++ == pr->nr) {
2795                                         strlcpy(pr->name, anchor->name,
2796                                             sizeof(pr->name));
2797                                         break;
2798                                 }
2799                 } else {
2800                         RB_FOREACH(anchor, pf_anchor_node,
2801                             &ruleset->anchor->children)
2802                                 if (nr++ == pr->nr) {
2803                                         strlcpy(pr->name, anchor->name,
2804                                             sizeof(pr->name));
2805                                         break;
2806                                 }
2807                 }
2808                 if (!pr->name[0])
2809                         error = EBUSY;
2810                 break;
2811         }
2812
2813         case DIOCRCLRTABLES: {
2814                 struct pfioc_table *io = (struct pfioc_table *)addr;
2815
2816                 if (io->pfrio_esize != 0) {
2817                         error = ENODEV;
2818                         break;
2819                 }
2820                 error = pfr_clr_tables(&io->pfrio_table, &io->pfrio_ndel,
2821                     io->pfrio_flags | PFR_FLAG_USERIOCTL);
2822                 break;
2823         }
2824
2825         case DIOCRADDTABLES: {
2826                 struct pfioc_table *io = (struct pfioc_table *)addr;
2827
2828                 if (io->pfrio_esize != sizeof(struct pfr_table)) {
2829                         error = ENODEV;
2830                         break;
2831                 }
2832                 error = pfr_add_tables(io->pfrio_buffer, io->pfrio_size,
2833                     &io->pfrio_nadd, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2834                 break;
2835         }
2836
2837         case DIOCRDELTABLES: {
2838                 struct pfioc_table *io = (struct pfioc_table *)addr;
2839
2840                 if (io->pfrio_esize != sizeof(struct pfr_table)) {
2841                         error = ENODEV;
2842                         break;
2843                 }
2844                 error = pfr_del_tables(io->pfrio_buffer, io->pfrio_size,
2845                     &io->pfrio_ndel, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2846                 break;
2847         }
2848
2849         case DIOCRGETTABLES: {
2850                 struct pfioc_table *io = (struct pfioc_table *)addr;
2851
2852                 if (io->pfrio_esize != sizeof(struct pfr_table)) {
2853                         error = ENODEV;
2854                         break;
2855                 }
2856                 error = pfr_get_tables(&io->pfrio_table, io->pfrio_buffer,
2857                     &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2858                 break;
2859         }
2860
2861         case DIOCRGETTSTATS: {
2862                 struct pfioc_table *io = (struct pfioc_table *)addr;
2863
2864                 if (io->pfrio_esize != sizeof(struct pfr_tstats)) {
2865                         error = ENODEV;
2866                         break;
2867                 }
2868                 error = pfr_get_tstats(&io->pfrio_table, io->pfrio_buffer,
2869                     &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2870                 break;
2871         }
2872
2873         case DIOCRCLRTSTATS: {
2874                 struct pfioc_table *io = (struct pfioc_table *)addr;
2875
2876                 if (io->pfrio_esize != sizeof(struct pfr_table)) {
2877                         error = ENODEV;
2878                         break;
2879                 }
2880                 error = pfr_clr_tstats(io->pfrio_buffer, io->pfrio_size,
2881                     &io->pfrio_nzero, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2882                 break;
2883         }
2884
2885         case DIOCRSETTFLAGS: {
2886                 struct pfioc_table *io = (struct pfioc_table *)addr;
2887
2888                 if (io->pfrio_esize != sizeof(struct pfr_table)) {
2889                         error = ENODEV;
2890                         break;
2891                 }
2892                 error = pfr_set_tflags(io->pfrio_buffer, io->pfrio_size,
2893                     io->pfrio_setflag, io->pfrio_clrflag, &io->pfrio_nchange,
2894                     &io->pfrio_ndel, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2895                 break;
2896         }
2897
2898         case DIOCRCLRADDRS: {
2899                 struct pfioc_table *io = (struct pfioc_table *)addr;
2900
2901                 if (io->pfrio_esize != 0) {
2902                         error = ENODEV;
2903                         break;
2904                 }
2905                 error = pfr_clr_addrs(&io->pfrio_table, &io->pfrio_ndel,
2906                     io->pfrio_flags | PFR_FLAG_USERIOCTL);
2907                 break;
2908         }
2909
2910         case DIOCRADDADDRS: {
2911                 struct pfioc_table *io = (struct pfioc_table *)addr;
2912
2913                 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2914                         error = ENODEV;
2915                         break;
2916                 }
2917                 error = pfr_add_addrs(&io->pfrio_table, io->pfrio_buffer,
2918                     io->pfrio_size, &io->pfrio_nadd, io->pfrio_flags |
2919                     PFR_FLAG_USERIOCTL);
2920                 break;
2921         }
2922
2923         case DIOCRDELADDRS: {
2924                 struct pfioc_table *io = (struct pfioc_table *)addr;
2925
2926                 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2927                         error = ENODEV;
2928                         break;
2929                 }
2930                 error = pfr_del_addrs(&io->pfrio_table, io->pfrio_buffer,
2931                     io->pfrio_size, &io->pfrio_ndel, io->pfrio_flags |
2932                     PFR_FLAG_USERIOCTL);
2933                 break;
2934         }
2935
2936         case DIOCRSETADDRS: {
2937                 struct pfioc_table *io = (struct pfioc_table *)addr;
2938
2939                 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2940                         error = ENODEV;
2941                         break;
2942                 }
2943                 error = pfr_set_addrs(&io->pfrio_table, io->pfrio_buffer,
2944                     io->pfrio_size, &io->pfrio_size2, &io->pfrio_nadd,
2945                     &io->pfrio_ndel, &io->pfrio_nchange, io->pfrio_flags |
2946                     PFR_FLAG_USERIOCTL, 0);
2947                 break;
2948         }
2949
2950         case DIOCRGETADDRS: {
2951                 struct pfioc_table *io = (struct pfioc_table *)addr;
2952
2953                 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2954                         error = ENODEV;
2955                         break;
2956                 }
2957                 error = pfr_get_addrs(&io->pfrio_table, io->pfrio_buffer,
2958                     &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2959                 break;
2960         }
2961
2962         case DIOCRGETASTATS: {
2963                 struct pfioc_table *io = (struct pfioc_table *)addr;
2964
2965                 if (io->pfrio_esize != sizeof(struct pfr_astats)) {
2966                         error = ENODEV;
2967                         break;
2968                 }
2969                 error = pfr_get_astats(&io->pfrio_table, io->pfrio_buffer,
2970                     &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2971                 break;
2972         }
2973
2974         case DIOCRCLRASTATS: {
2975                 struct pfioc_table *io = (struct pfioc_table *)addr;
2976
2977                 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2978                         error = ENODEV;
2979                         break;
2980                 }
2981                 error = pfr_clr_astats(&io->pfrio_table, io->pfrio_buffer,
2982                     io->pfrio_size, &io->pfrio_nzero, io->pfrio_flags |
2983                     PFR_FLAG_USERIOCTL);
2984                 break;
2985         }
2986
2987         case DIOCRTSTADDRS: {
2988                 struct pfioc_table *io = (struct pfioc_table *)addr;
2989
2990                 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2991                         error = ENODEV;
2992                         break;
2993                 }
2994                 error = pfr_tst_addrs(&io->pfrio_table, io->pfrio_buffer,
2995                     io->pfrio_size, &io->pfrio_nmatch, io->pfrio_flags |
2996                     PFR_FLAG_USERIOCTL);
2997                 break;
2998         }
2999
3000         case DIOCRINADEFINE: {
3001                 struct pfioc_table *io = (struct pfioc_table *)addr;
3002
3003                 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
3004                         error = ENODEV;
3005                         break;
3006                 }
3007                 error = pfr_ina_define(&io->pfrio_table, io->pfrio_buffer,
3008                     io->pfrio_size, &io->pfrio_nadd, &io->pfrio_naddr,
3009                     io->pfrio_ticket, io->pfrio_flags | PFR_FLAG_USERIOCTL);
3010                 break;
3011         }
3012
3013         case DIOCOSFPADD: {
3014                 struct pf_osfp_ioctl *io = (struct pf_osfp_ioctl *)addr;
3015                 error = pf_osfp_add(io);
3016                 break;
3017         }
3018
3019         case DIOCOSFPGET: {
3020                 struct pf_osfp_ioctl *io = (struct pf_osfp_ioctl *)addr;
3021                 error = pf_osfp_get(io);
3022                 break;
3023         }
3024
3025         case DIOCXBEGIN: {
3026                 struct pfioc_trans      *io = (struct pfioc_trans *)addr;
3027                 struct pfioc_trans_e    *ioe;
3028                 struct pfr_table        *table;
3029                 int                      i;
3030
3031                 if (io->esize != sizeof(*ioe)) {
3032                         error = ENODEV;
3033                         goto fail;
3034                 }
3035 #ifdef __FreeBSD__
3036                 PF_UNLOCK();
3037 #endif
3038                 ioe = (struct pfioc_trans_e *)malloc(sizeof(*ioe),
3039                     M_TEMP, M_WAITOK);
3040                 table = (struct pfr_table *)malloc(sizeof(*table),
3041                     M_TEMP, M_WAITOK);
3042 #ifdef __FreeBSD__
3043                 PF_LOCK();
3044 #endif
3045                 for (i = 0; i < io->size; i++) {
3046 #ifdef __FreeBSD__
3047                         PF_COPYIN(io->array+i, ioe, sizeof(*ioe), error);
3048                         if (error) {
3049 #else
3050                         if (copyin(io->array+i, ioe, sizeof(*ioe))) {
3051 #endif
3052                                 free(table, M_TEMP);
3053                                 free(ioe, M_TEMP);
3054                                 error = EFAULT;
3055                                 goto fail;
3056                         }
3057                         switch (ioe->rs_num) {
3058 #ifdef ALTQ
3059                         case PF_RULESET_ALTQ:
3060                                 if (ioe->anchor[0]) {
3061                                         free(table, M_TEMP);
3062                                         free(ioe, M_TEMP);
3063                                         error = EINVAL;
3064                                         goto fail;
3065                                 }
3066                                 if ((error = pf_begin_altq(&ioe->ticket))) {
3067                                         free(table, M_TEMP);
3068                                         free(ioe, M_TEMP);
3069                                         goto fail;
3070                                 }
3071                                 break;
3072 #endif /* ALTQ */
3073                         case PF_RULESET_TABLE:
3074                                 bzero(table, sizeof(*table));
3075                                 strlcpy(table->pfrt_anchor, ioe->anchor,
3076                                     sizeof(table->pfrt_anchor));
3077                                 if ((error = pfr_ina_begin(table,
3078                                     &ioe->ticket, NULL, 0))) {
3079                                         free(table, M_TEMP);
3080                                         free(ioe, M_TEMP);
3081                                         goto fail;
3082                                 }
3083                                 break;
3084                         default:
3085                                 if ((error = pf_begin_rules(&ioe->ticket,
3086                                     ioe->rs_num, ioe->anchor))) {
3087                                         free(table, M_TEMP);
3088                                         free(ioe, M_TEMP);
3089                                         goto fail;
3090                                 }
3091                                 break;
3092                         }
3093 #ifdef __FreeBSD__
3094                         PF_COPYOUT(ioe, io->array+i, sizeof(io->array[i]),
3095                             error);
3096                         if (error) {
3097 #else
3098                         if (copyout(ioe, io->array+i, sizeof(io->array[i]))) {
3099 #endif
3100                                 free(table, M_TEMP);
3101                                 free(ioe, M_TEMP);
3102                                 error = EFAULT;
3103                                 goto fail;
3104                         }
3105                 }
3106                 free(table, M_TEMP);
3107                 free(ioe, M_TEMP);
3108                 break;
3109         }
3110
3111         case DIOCXROLLBACK: {
3112                 struct pfioc_trans      *io = (struct pfioc_trans *)addr;
3113                 struct pfioc_trans_e    *ioe;
3114                 struct pfr_table        *table;
3115                 int                      i;
3116
3117                 if (io->esize != sizeof(*ioe)) {
3118                         error = ENODEV;
3119                         goto fail;
3120                 }
3121 #ifdef __FreeBSD__
3122                 PF_UNLOCK();
3123 #endif
3124                 ioe = (struct pfioc_trans_e *)malloc(sizeof(*ioe),
3125                     M_TEMP, M_WAITOK);
3126                 table = (struct pfr_table *)malloc(sizeof(*table),
3127                     M_TEMP, M_WAITOK);
3128 #ifdef __FreeBSD__
3129                 PF_LOCK();
3130 #endif
3131                 for (i = 0; i < io->size; i++) {
3132 #ifdef __FreeBSD__
3133                         PF_COPYIN(io->array+i, ioe, sizeof(*ioe), error);
3134                         if (error) {
3135 #else
3136                         if (copyin(io->array+i, ioe, sizeof(*ioe))) {
3137 #endif
3138                                 free(table, M_TEMP);
3139                                 free(ioe, M_TEMP);
3140                                 error = EFAULT;
3141                                 goto fail;
3142                         }
3143                         switch (ioe->rs_num) {
3144 #ifdef ALTQ
3145                         case PF_RULESET_ALTQ:
3146                                 if (ioe->anchor[0]) {
3147                                         free(table, M_TEMP);
3148                                         free(ioe, M_TEMP);
3149                                         error = EINVAL;
3150                                         goto fail;
3151                                 }
3152                                 if ((error = pf_rollback_altq(ioe->ticket))) {
3153                                         free(table, M_TEMP);
3154                                         free(ioe, M_TEMP);
3155                                         goto fail; /* really bad */
3156                                 }
3157                                 break;
3158 #endif /* ALTQ */
3159                         case PF_RULESET_TABLE:
3160                                 bzero(table, sizeof(*table));
3161                                 strlcpy(table->pfrt_anchor, ioe->anchor,
3162                                     sizeof(table->pfrt_anchor));
3163                                 if ((error = pfr_ina_rollback(table,
3164                                     ioe->ticket, NULL, 0))) {
3165                                         free(table, M_TEMP);
3166                                         free(ioe, M_TEMP);
3167                                         goto fail; /* really bad */
3168                                 }
3169                                 break;
3170                         default:
3171                                 if ((error = pf_rollback_rules(ioe->ticket,
3172                                     ioe->rs_num, ioe->anchor))) {
3173                                         free(table, M_TEMP);
3174                                         free(ioe, M_TEMP);
3175                                         goto fail; /* really bad */
3176                                 }
3177                                 break;
3178                         }
3179                 }
3180                 free(table, M_TEMP);
3181                 free(ioe, M_TEMP);
3182                 break;
3183         }
3184
3185         case DIOCXCOMMIT: {
3186                 struct pfioc_trans      *io = (struct pfioc_trans *)addr;
3187                 struct pfioc_trans_e    *ioe;
3188                 struct pfr_table        *table;
3189                 struct pf_ruleset       *rs;
3190                 int                      i;
3191
3192                 if (io->esize != sizeof(*ioe)) {
3193                         error = ENODEV;
3194                         goto fail;
3195                 }
3196 #ifdef __FreeBSD__
3197                 PF_UNLOCK();
3198 #endif
3199                 ioe = (struct pfioc_trans_e *)malloc(sizeof(*ioe),
3200                     M_TEMP, M_WAITOK);
3201                 table = (struct pfr_table *)malloc(sizeof(*table),
3202                     M_TEMP, M_WAITOK);
3203 #ifdef __FreeBSD__
3204                 PF_LOCK();
3205 #endif
3206                 /* first makes sure everything will succeed */
3207                 for (i = 0; i < io->size; i++) {
3208 #ifdef __FreeBSD__
3209                         PF_COPYIN(io->array+i, ioe, sizeof(*ioe), error);
3210                         if (error) {
3211 #else
3212                         if (copyin(io->array+i, ioe, sizeof(*ioe))) {
3213 #endif
3214                                 free(table, M_TEMP);
3215                                 free(ioe, M_TEMP);
3216                                 error = EFAULT;
3217                                 goto fail;
3218                         }
3219                         switch (ioe->rs_num) {
3220 #ifdef ALTQ
3221                         case PF_RULESET_ALTQ:
3222                                 if (ioe->anchor[0]) {
3223                                         free(table, M_TEMP);
3224                                         free(ioe, M_TEMP);
3225                                         error = EINVAL;
3226                                         goto fail;
3227                                 }
3228                                 if (!altqs_inactive_open || ioe->ticket !=
3229                                     ticket_altqs_inactive) {
3230                                         free(table, M_TEMP);
3231                                         free(ioe, M_TEMP);
3232                                         error = EBUSY;
3233                                         goto fail;
3234                                 }
3235                                 break;
3236 #endif /* ALTQ */
3237                         case PF_RULESET_TABLE:
3238                                 rs = pf_find_ruleset(ioe->anchor);
3239                                 if (rs == NULL || !rs->topen || ioe->ticket !=
3240                                      rs->tticket) {
3241                                         free(table, M_TEMP);
3242                                         free(ioe, M_TEMP);
3243                                         error = EBUSY;
3244                                         goto fail;
3245                                 }
3246                                 break;
3247                         default:
3248                                 if (ioe->rs_num < 0 || ioe->rs_num >=
3249                                     PF_RULESET_MAX) {
3250                                         free(table, M_TEMP);
3251                                         free(ioe, M_TEMP);
3252                                         error = EINVAL;
3253                                         goto fail;
3254                                 }
3255                                 rs = pf_find_ruleset(ioe->anchor);
3256                                 if (rs == NULL ||
3257                                     !rs->rules[ioe->rs_num].inactive.open ||
3258                                     rs->rules[ioe->rs_num].inactive.ticket !=
3259                                     ioe->ticket) {
3260                                         free(table, M_TEMP);
3261                                         free(ioe, M_TEMP);
3262                                         error = EBUSY;
3263                                         goto fail;
3264                                 }
3265                                 break;
3266                         }
3267                 }
3268                 /* now do the commit - no errors should happen here */
3269                 for (i = 0; i < io->size; i++) {
3270 #ifdef __FreeBSD__
3271                         PF_COPYIN(io->array+i, ioe, sizeof(*ioe), error);
3272                         if (error) {
3273 #else
3274                         if (copyin(io->array+i, ioe, sizeof(*ioe))) {
3275 #endif
3276                                 free(table, M_TEMP);
3277                                 free(ioe, M_TEMP);
3278                                 error = EFAULT;
3279                                 goto fail;
3280                         }
3281                         switch (ioe->rs_num) {
3282 #ifdef ALTQ
3283                         case PF_RULESET_ALTQ:
3284                                 if ((error = pf_commit_altq(ioe->ticket))) {
3285                                         free(table, M_TEMP);
3286                                         free(ioe, M_TEMP);
3287                                         goto fail; /* really bad */
3288                                 }
3289                                 break;
3290 #endif /* ALTQ */
3291                         case PF_RULESET_TABLE:
3292                                 bzero(table, sizeof(*table));
3293                                 strlcpy(table->pfrt_anchor, ioe->anchor,
3294                                     sizeof(table->pfrt_anchor));
3295                                 if ((error = pfr_ina_commit(table, ioe->ticket,
3296                                     NULL, NULL, 0))) {
3297                                         free(table, M_TEMP);
3298                                         free(ioe, M_TEMP);
3299                                         goto fail; /* really bad */
3300                                 }
3301                                 break;
3302                         default:
3303                                 if ((error = pf_commit_rules(ioe->ticket,
3304                                     ioe->rs_num, ioe->anchor))) {
3305                                         free(table, M_TEMP);
3306                                         free(ioe, M_TEMP);
3307                                         goto fail; /* really bad */
3308                                 }
3309                                 break;
3310                         }
3311                 }
3312                 free(table, M_TEMP);
3313                 free(ioe, M_TEMP);
3314                 break;
3315         }
3316
3317         case DIOCGETSRCNODES: {
3318                 struct pfioc_src_nodes  *psn = (struct pfioc_src_nodes *)addr;
3319                 struct pf_src_node      *n, *p, *pstore;
3320                 u_int32_t                nr = 0;
3321                 int                      space = psn->psn_len;
3322
3323                 if (space == 0) {
3324                         RB_FOREACH(n, pf_src_tree, &tree_src_tracking)
3325                                 nr++;
3326                         psn->psn_len = sizeof(struct pf_src_node) * nr;
3327                         break;
3328                 }
3329
3330 #ifdef __FreeBSD__
3331                 PF_UNLOCK();
3332 #endif
3333                 pstore = malloc(sizeof(*pstore), M_TEMP, M_WAITOK);
3334 #ifdef __FreeBSD__
3335                 PF_LOCK();
3336 #endif
3337
3338                 p = psn->psn_src_nodes;
3339                 RB_FOREACH(n, pf_src_tree, &tree_src_tracking) {
3340                         int     secs = time_second, diff;
3341
3342                         if ((nr + 1) * sizeof(*p) > (unsigned)psn->psn_len)
3343                                 break;
3344
3345                         bcopy(n, pstore, sizeof(*pstore));
3346                         if (n->rule.ptr != NULL)
3347                                 pstore->rule.nr = n->rule.ptr->nr;
3348                         pstore->creation = secs - pstore->creation;
3349                         if (pstore->expire > secs)
3350                                 pstore->expire -= secs;
3351                         else
3352                                 pstore->expire = 0;
3353
3354                         /* adjust the connection rate estimate */
3355                         diff = secs - n->conn_rate.last;
3356                         if (diff >= n->conn_rate.seconds)
3357                                 pstore->conn_rate.count = 0;
3358                         else
3359                                 pstore->conn_rate.count -=
3360                                     n->conn_rate.count * diff /
3361                                     n->conn_rate.seconds;
3362
3363 #ifdef __FreeBSD__
3364                         PF_COPYOUT(pstore, p, sizeof(*p), error);
3365 #else
3366                         error = copyout(pstore, p, sizeof(*p));
3367 #endif
3368                         if (error) {
3369                                 free(pstore, M_TEMP);
3370                                 goto fail;
3371                         }
3372                         p++;
3373                         nr++;
3374                 }
3375                 psn->psn_len = sizeof(struct pf_src_node) * nr;
3376
3377                 free(pstore, M_TEMP);
3378                 break;
3379         }
3380
3381         case DIOCCLRSRCNODES: {
3382                 struct pf_src_node      *n;
3383                 struct pf_state         *state;
3384
3385                 RB_FOREACH(state, pf_state_tree_id, &tree_id) {
3386                         state->src_node = NULL;
3387                         state->nat_src_node = NULL;
3388                 }
3389                 RB_FOREACH(n, pf_src_tree, &tree_src_tracking) {
3390                         n->expire = 1;
3391                         n->states = 0;
3392                 }
3393                 pf_purge_expired_src_nodes(1);
3394                 pf_status.src_nodes = 0;
3395                 break;
3396         }
3397
3398         case DIOCKILLSRCNODES: {
3399                 struct pf_src_node      *sn;
3400                 struct pf_state         *s;
3401                 struct pfioc_src_node_kill *psnk = \
3402                         (struct pfioc_src_node_kill *) addr;
3403                 int                     killed = 0;
3404
3405                 RB_FOREACH(sn, pf_src_tree, &tree_src_tracking) {
3406                         if (PF_MATCHA(psnk->psnk_src.neg, \
3407                                       &psnk->psnk_src.addr.v.a.addr, \
3408                                       &psnk->psnk_src.addr.v.a.mask, \
3409                                       &sn->addr, sn->af) &&
3410                             PF_MATCHA(psnk->psnk_dst.neg, \
3411                                       &psnk->psnk_dst.addr.v.a.addr, \
3412                                       &psnk->psnk_dst.addr.v.a.mask, \
3413                                       &sn->raddr, sn->af)) {
3414                                 /* Handle state to src_node linkage */
3415                                 if (sn->states != 0) {
3416                                         RB_FOREACH(s, pf_state_tree_id, 
3417                                             &tree_id) {
3418                                                 if (s->src_node == sn)
3419                                                         s->src_node = NULL;
3420                                                 if (s->nat_src_node == sn)
3421                                                         s->nat_src_node = NULL;
3422                                         }
3423                                         sn->states = 0;
3424                                 }
3425                                 sn->expire = 1;
3426                                 killed++;
3427                         }
3428                 }
3429
3430                 if (killed > 0)
3431                         pf_purge_expired_src_nodes(1);
3432
3433                 psnk->psnk_af = killed;
3434                 break;
3435         }
3436
3437         case DIOCSETHOSTID: {
3438                 u_int32_t       *hostid = (u_int32_t *)addr;
3439
3440                 if (*hostid == 0)
3441                         pf_status.hostid = arc4random();
3442                 else
3443                         pf_status.hostid = *hostid;
3444                 break;
3445         }
3446
3447         case DIOCOSFPFLUSH:
3448                 pf_osfp_flush();
3449                 break;
3450
3451         case DIOCIGETIFACES: {
3452                 struct pfioc_iface *io = (struct pfioc_iface *)addr;
3453
3454                 if (io->pfiio_esize != sizeof(struct pfi_kif)) {
3455                         error = ENODEV;
3456                         break;
3457                 }
3458                 error = pfi_get_ifaces(io->pfiio_name, io->pfiio_buffer,
3459                     &io->pfiio_size);
3460                 break;
3461         }
3462
3463         case DIOCSETIFFLAG: {
3464                 struct pfioc_iface *io = (struct pfioc_iface *)addr;
3465
3466                 error = pfi_set_flags(io->pfiio_name, io->pfiio_flags);
3467                 break;
3468         }
3469
3470         case DIOCCLRIFFLAG: {
3471                 struct pfioc_iface *io = (struct pfioc_iface *)addr;
3472
3473                 error = pfi_clear_flags(io->pfiio_name, io->pfiio_flags);
3474                 break;
3475         }
3476
3477         default:
3478                 error = ENODEV;
3479                 break;
3480         }
3481 fail:
3482 #ifdef __FreeBSD__
3483         PF_UNLOCK();
3484
3485         if (flags & FWRITE)
3486                 sx_xunlock(&pf_consistency_lock);
3487         else
3488                 sx_sunlock(&pf_consistency_lock);
3489 #else
3490         splx(s);
3491         /* XXX: Lock order? */
3492         if (flags & FWRITE)
3493                 rw_exit_write(&pf_consistency_lock);
3494         else
3495                 rw_exit_read(&pf_consistency_lock);
3496 #endif
3497         return (error);
3498 }
3499
3500 #ifdef __FreeBSD__
3501 /*
3502  * XXX - Check for version missmatch!!!
3503  */
3504 static void
3505 pf_clear_states(void)
3506 {
3507         struct pf_state         *state;
3508
3509         RB_FOREACH(state, pf_state_tree_id, &tree_id) {
3510                 state->timeout = PFTM_PURGE;
3511 #if NPFSYNC
3512                 /* don't send out individual delete messages */
3513                 state->sync_flags = PFSTATE_NOSYNC;
3514 #endif
3515                 pf_unlink_state(state);
3516         }
3517
3518 #if 0 /* NPFSYNC */
3519 /*
3520  * XXX This is called on module unload, we do not want to sync that over? */
3521  */
3522         pfsync_clear_states(pf_status.hostid, psk->psk_ifname);
3523 #endif
3524 }
3525
3526 static int
3527 pf_clear_tables(void)
3528 {
3529         struct pfioc_table io;
3530         int error;
3531
3532         bzero(&io, sizeof(io));
3533
3534         error = pfr_clr_tables(&io.pfrio_table, &io.pfrio_ndel,
3535             io.pfrio_flags);
3536
3537         return (error);
3538 }
3539
3540 static void
3541 pf_clear_srcnodes(void)
3542 {
3543         struct pf_src_node      *n;
3544         struct pf_state         *state;
3545
3546         RB_FOREACH(state, pf_state_tree_id, &tree_id) {
3547                 state->src_node = NULL;
3548                 state->nat_src_node = NULL;
3549         }
3550         RB_FOREACH(n, pf_src_tree, &tree_src_tracking) {
3551                 n->expire = 1;
3552                 n->states = 0;
3553         }
3554 }
3555 /*
3556  * XXX - Check for version missmatch!!!
3557  */
3558
3559 /*
3560  * Duplicate pfctl -Fa operation to get rid of as much as we can.
3561  */
3562 static int
3563 shutdown_pf(void)
3564 {
3565         int error = 0;
3566         u_int32_t t[5];
3567         char nn = '\0';
3568
3569         pf_status.running = 0;
3570         do {
3571                 if ((error = pf_begin_rules(&t[0], PF_RULESET_SCRUB, &nn))
3572                     != 0) {
3573                         DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: SCRUB\n"));
3574                         break;
3575                 }
3576                 if ((error = pf_begin_rules(&t[1], PF_RULESET_FILTER, &nn))
3577                     != 0) {
3578                         DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: FILTER\n"));
3579                         break;          /* XXX: rollback? */
3580                 }
3581                 if ((error = pf_begin_rules(&t[2], PF_RULESET_NAT, &nn))
3582                     != 0) {
3583                         DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: NAT\n"));
3584                         break;          /* XXX: rollback? */
3585                 }
3586                 if ((error = pf_begin_rules(&t[3], PF_RULESET_BINAT, &nn))
3587                     != 0) {
3588                         DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: BINAT\n"));
3589                         break;          /* XXX: rollback? */
3590                 }
3591                 if ((error = pf_begin_rules(&t[4], PF_RULESET_RDR, &nn))
3592                     != 0) {
3593                         DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: RDR\n"));
3594                         break;          /* XXX: rollback? */
3595                 }
3596
3597                 /* XXX: these should always succeed here */
3598                 pf_commit_rules(t[0], PF_RULESET_SCRUB, &nn);
3599                 pf_commit_rules(t[1], PF_RULESET_FILTER, &nn);
3600                 pf_commit_rules(t[2], PF_RULESET_NAT, &nn);
3601                 pf_commit_rules(t[3], PF_RULESET_BINAT, &nn);
3602                 pf_commit_rules(t[4], PF_RULESET_RDR, &nn);
3603
3604                 if ((error = pf_clear_tables()) != 0)
3605                         break;
3606
3607 #ifdef ALTQ
3608                 if ((error = pf_begin_altq(&t[0])) != 0) {
3609                         DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: ALTQ\n"));
3610                         break;
3611                 }
3612                 pf_commit_altq(t[0]);
3613 #endif
3614
3615                 pf_clear_states();
3616
3617                 pf_clear_srcnodes();
3618
3619                 /* status does not use malloced mem so no need to cleanup */
3620                 /* fingerprints and interfaces have thier own cleanup code */
3621         } while(0);
3622
3623         return (error);
3624 }
3625
3626 static int
3627 pf_check_in(void *arg, struct mbuf **m, struct ifnet *ifp, int dir,
3628     struct inpcb *inp)
3629 {
3630         /*
3631          * XXX Wed Jul 9 22:03:16 2003 UTC
3632          * OpenBSD has changed its byte ordering convention on ip_len/ip_off
3633          * in network stack. OpenBSD's network stack have converted
3634          * ip_len/ip_off to host byte order frist as FreeBSD.
3635          * Now this is not true anymore , so we should convert back to network
3636          * byte order. 
3637          */
3638         struct ip *h = NULL;
3639         int chk;
3640
3641         if ((*m)->m_pkthdr.len >= (int)sizeof(struct ip)) {
3642                 /* if m_pkthdr.len is less than ip header, pf will handle. */
3643                 h = mtod(*m, struct ip *);
3644                 HTONS(h->ip_len);
3645                 HTONS(h->ip_off);
3646         }
3647         chk = pf_test(PF_IN, ifp, m, NULL, inp);
3648         if (chk && *m) {
3649                 m_freem(*m);
3650                 *m = NULL;
3651         }
3652         if (*m != NULL) {
3653                 /* pf_test can change ip header location */
3654                 h = mtod(*m, struct ip *);
3655                 NTOHS(h->ip_len);
3656                 NTOHS(h->ip_off);
3657         }
3658         return chk;
3659 }
3660
3661 static int
3662 pf_check_out(void *arg, struct mbuf **m, struct ifnet *ifp, int dir,
3663     struct inpcb *inp)
3664 {
3665         /*
3666          * XXX Wed Jul 9 22:03:16 2003 UTC
3667          * OpenBSD has changed its byte ordering convention on ip_len/ip_off
3668          * in network stack. OpenBSD's network stack have converted
3669          * ip_len/ip_off to host byte order frist as FreeBSD.
3670          * Now this is not true anymore , so we should convert back to network
3671          * byte order. 
3672          */
3673         struct ip *h = NULL;
3674         int chk;
3675
3676         /* We need a proper CSUM befor we start (s. OpenBSD ip_output) */
3677         if ((*m)->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
3678                 in_delayed_cksum(*m);
3679                 (*m)->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
3680         }
3681         if ((*m)->m_pkthdr.len >= (int)sizeof(*h)) {
3682                 /* if m_pkthdr.len is less than ip header, pf will handle. */
3683                 h = mtod(*m, struct ip *);
3684                 HTONS(h->ip_len);
3685                 HTONS(h->ip_off);
3686         }
3687         chk = pf_test(PF_OUT, ifp, m, NULL, inp);
3688         if (chk && *m) {
3689                 m_freem(*m);
3690                 *m = NULL;
3691         }
3692         if (*m != NULL) {
3693                 /* pf_test can change ip header location */
3694                 h = mtod(*m, struct ip *);
3695                 NTOHS(h->ip_len);
3696                 NTOHS(h->ip_off);
3697         }
3698         return chk;
3699 }
3700
3701 #ifdef INET6
3702 static int
3703 pf_check6_in(void *arg, struct mbuf **m, struct ifnet *ifp, int dir,
3704     struct inpcb *inp)
3705 {
3706         /*
3707          * IPv6 is not affected by ip_len/ip_off byte order changes.
3708          */
3709         int chk;
3710
3711         /*
3712          * In case of loopback traffic IPv6 uses the real interface in
3713          * order to support scoped addresses. In order to support stateful
3714          * filtering we have change this to lo0 as it is the case in IPv4.
3715          */
3716         chk = pf_test6(PF_IN, (*m)->m_flags & M_LOOP ? &loif[0] : ifp, m,
3717             NULL, inp);
3718         if (chk && *m) {
3719                 m_freem(*m);
3720                 *m = NULL;
3721         }
3722         return chk;
3723 }
3724
3725 static int
3726 pf_check6_out(void *arg, struct mbuf **m, struct ifnet *ifp, int dir,
3727     struct inpcb *inp)
3728 {
3729         /*
3730          * IPv6 does not affected ip_len/ip_off byte order changes.
3731          */
3732         int chk;
3733
3734         /* We need a proper CSUM befor we start (s. OpenBSD ip_output) */
3735         if ((*m)->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
3736                 in_delayed_cksum(*m);
3737                 (*m)->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
3738         }
3739         chk = pf_test6(PF_OUT, ifp, m, NULL, inp);
3740         if (chk && *m) {
3741                 m_freem(*m);
3742                 *m = NULL;
3743         }
3744         return chk;
3745 }
3746 #endif /* INET6 */
3747
3748 static int
3749 hook_pf(void)
3750 {
3751         struct pfil_head *pfh_inet;
3752 #ifdef INET6
3753         struct pfil_head *pfh_inet6;
3754 #endif
3755         
3756         PF_ASSERT(MA_NOTOWNED);
3757
3758         if (pf_pfil_hooked)
3759                 return (0); 
3760         
3761         pfh_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET);
3762         if (pfh_inet == NULL)
3763                 return (ESRCH); /* XXX */
3764         pfil_add_hook(pf_check_in, NULL, PFIL_IN | PFIL_WAITOK, pfh_inet);
3765         pfil_add_hook(pf_check_out, NULL, PFIL_OUT | PFIL_WAITOK, pfh_inet);
3766 #ifdef INET6
3767         pfh_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6);
3768         if (pfh_inet6 == NULL) {
3769                 pfil_remove_hook(pf_check_in, NULL, PFIL_IN | PFIL_WAITOK,
3770                     pfh_inet);
3771                 pfil_remove_hook(pf_check_out, NULL, PFIL_OUT | PFIL_WAITOK,
3772                     pfh_inet);
3773                 return (ESRCH); /* XXX */
3774         }
3775         pfil_add_hook(pf_check6_in, NULL, PFIL_IN | PFIL_WAITOK, pfh_inet6);
3776         pfil_add_hook(pf_check6_out, NULL, PFIL_OUT | PFIL_WAITOK, pfh_inet6);
3777 #endif
3778
3779         pf_pfil_hooked = 1;
3780         return (0);
3781 }
3782
3783 static int
3784 dehook_pf(void)
3785 {
3786         struct pfil_head *pfh_inet;
3787 #ifdef INET6
3788         struct pfil_head *pfh_inet6;
3789 #endif
3790
3791         PF_ASSERT(MA_NOTOWNED);
3792
3793         if (pf_pfil_hooked == 0)
3794                 return (0);
3795
3796         pfh_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET);
3797         if (pfh_inet == NULL)
3798                 return (ESRCH); /* XXX */
3799         pfil_remove_hook(pf_check_in, NULL, PFIL_IN | PFIL_WAITOK,
3800             pfh_inet);
3801         pfil_remove_hook(pf_check_out, NULL, PFIL_OUT | PFIL_WAITOK,
3802             pfh_inet);
3803 #ifdef INET6
3804         pfh_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6);
3805         if (pfh_inet6 == NULL)
3806                 return (ESRCH); /* XXX */
3807         pfil_remove_hook(pf_check6_in, NULL, PFIL_IN | PFIL_WAITOK,
3808             pfh_inet6);
3809         pfil_remove_hook(pf_check6_out, NULL, PFIL_OUT | PFIL_WAITOK,
3810             pfh_inet6);
3811 #endif
3812
3813         pf_pfil_hooked = 0;
3814         return (0);
3815 }
3816
3817 static int
3818 pf_load(void)
3819 {
3820         init_zone_var();
3821         init_pf_mutex();
3822         pf_dev = make_dev(&pf_cdevsw, 0, 0, 0, 0600, PF_NAME);
3823         if (pfattach() < 0) {
3824                 destroy_dev(pf_dev);
3825                 destroy_pf_mutex();
3826                 return (ENOMEM);
3827         }
3828         return (0);
3829 }
3830
3831 static int
3832 pf_unload(void)
3833 {
3834         int error = 0;
3835
3836         PF_LOCK();
3837         pf_status.running = 0;
3838         PF_UNLOCK();
3839         error = dehook_pf();
3840         if (error) {
3841                 /*
3842                  * Should not happen!
3843                  * XXX Due to error code ESRCH, kldunload will show
3844                  * a message like 'No such process'.
3845                  */
3846                 printf("%s : pfil unregisteration fail\n", __FUNCTION__);
3847                 return error;
3848         }
3849         PF_LOCK();
3850         shutdown_pf();
3851         pf_end_threads = 1;
3852         while (pf_end_threads < 2) {
3853                 wakeup_one(pf_purge_thread);
3854                 msleep(pf_purge_thread, &pf_task_mtx, 0, "pftmo", hz);
3855         }
3856         pfi_cleanup();
3857         pf_osfp_flush();
3858         pf_osfp_cleanup();
3859         cleanup_pf_zone();
3860         PF_UNLOCK();
3861         destroy_dev(pf_dev);
3862         destroy_pf_mutex();
3863         return error;
3864 }
3865
3866 static int
3867 pf_modevent(module_t mod, int type, void *data)
3868 {
3869         int error = 0;
3870
3871         switch(type) {
3872         case MOD_LOAD:
3873                 error = pf_load();
3874                 break;
3875
3876         case MOD_UNLOAD:
3877                 error = pf_unload();
3878                 break;
3879         default:
3880                 error = EINVAL;
3881                 break;
3882         }
3883         return error;
3884 }
3885
3886 static moduledata_t pf_mod = {
3887         "pf",
3888         pf_modevent,
3889         0
3890 };
3891
3892 DECLARE_MODULE(pf, pf_mod, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_FIRST);
3893 MODULE_VERSION(pf, PF_MODVER);
3894 #endif  /* __FreeBSD__ */