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