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