pf_ioctl.c revision 1.1 1 1.1 itojun /* $OpenBSD: pf_ioctl.c,v 1.112 2004/03/22 04:54:18 mcbride Exp $ */
2 1.1 itojun
3 1.1 itojun /*
4 1.1 itojun * Copyright (c) 2001 Daniel Hartmeier
5 1.1 itojun * Copyright (c) 2002,2003 Henning Brauer
6 1.1 itojun * All rights reserved.
7 1.1 itojun *
8 1.1 itojun * Redistribution and use in source and binary forms, with or without
9 1.1 itojun * modification, are permitted provided that the following conditions
10 1.1 itojun * are met:
11 1.1 itojun *
12 1.1 itojun * - Redistributions of source code must retain the above copyright
13 1.1 itojun * notice, this list of conditions and the following disclaimer.
14 1.1 itojun * - Redistributions in binary form must reproduce the above
15 1.1 itojun * copyright notice, this list of conditions and the following
16 1.1 itojun * disclaimer in the documentation and/or other materials provided
17 1.1 itojun * with the distribution.
18 1.1 itojun *
19 1.1 itojun * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 1.1 itojun * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 1.1 itojun * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22 1.1 itojun * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23 1.1 itojun * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24 1.1 itojun * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25 1.1 itojun * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 1.1 itojun * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 1.1 itojun * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 1.1 itojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29 1.1 itojun * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 1.1 itojun * POSSIBILITY OF SUCH DAMAGE.
31 1.1 itojun *
32 1.1 itojun * Effort sponsored in part by the Defense Advanced Research Projects
33 1.1 itojun * Agency (DARPA) and Air Force Research Laboratory, Air Force
34 1.1 itojun * Materiel Command, USAF, under agreement number F30602-01-2-0537.
35 1.1 itojun *
36 1.1 itojun */
37 1.1 itojun
38 1.1 itojun #include "pfsync.h"
39 1.1 itojun
40 1.1 itojun #include <sys/param.h>
41 1.1 itojun #include <sys/systm.h>
42 1.1 itojun #include <sys/mbuf.h>
43 1.1 itojun #include <sys/filio.h>
44 1.1 itojun #include <sys/fcntl.h>
45 1.1 itojun #include <sys/socket.h>
46 1.1 itojun #include <sys/socketvar.h>
47 1.1 itojun #include <sys/kernel.h>
48 1.1 itojun #include <sys/time.h>
49 1.1 itojun #include <sys/timeout.h>
50 1.1 itojun #include <sys/pool.h>
51 1.1 itojun #include <sys/malloc.h>
52 1.1 itojun
53 1.1 itojun #include <net/if.h>
54 1.1 itojun #include <net/if_types.h>
55 1.1 itojun #include <net/route.h>
56 1.1 itojun
57 1.1 itojun #include <netinet/in.h>
58 1.1 itojun #include <netinet/in_var.h>
59 1.1 itojun #include <netinet/in_systm.h>
60 1.1 itojun #include <netinet/ip.h>
61 1.1 itojun #include <netinet/ip_var.h>
62 1.1 itojun #include <netinet/ip_icmp.h>
63 1.1 itojun
64 1.1 itojun #include <dev/rndvar.h>
65 1.1 itojun #include <net/pfvar.h>
66 1.1 itojun
67 1.1 itojun #if NPFSYNC > 0
68 1.1 itojun #include <net/if_pfsync.h>
69 1.1 itojun #endif /* NPFSYNC > 0 */
70 1.1 itojun
71 1.1 itojun #ifdef INET6
72 1.1 itojun #include <netinet/ip6.h>
73 1.1 itojun #include <netinet/in_pcb.h>
74 1.1 itojun #endif /* INET6 */
75 1.1 itojun
76 1.1 itojun #ifdef ALTQ
77 1.1 itojun #include <altq/altq.h>
78 1.1 itojun #endif
79 1.1 itojun
80 1.1 itojun void pfattach(int);
81 1.1 itojun int pfopen(dev_t, int, int, struct proc *);
82 1.1 itojun int pfclose(dev_t, int, int, struct proc *);
83 1.1 itojun struct pf_pool *pf_get_pool(char *, char *, u_int32_t,
84 1.1 itojun u_int8_t, u_int8_t, u_int8_t, u_int8_t, u_int8_t);
85 1.1 itojun int pf_get_ruleset_number(u_int8_t);
86 1.1 itojun void pf_init_ruleset(struct pf_ruleset *);
87 1.1 itojun void pf_mv_pool(struct pf_palist *, struct pf_palist *);
88 1.1 itojun void pf_empty_pool(struct pf_palist *);
89 1.1 itojun int pfioctl(dev_t, u_long, caddr_t, int, struct proc *);
90 1.1 itojun #ifdef ALTQ
91 1.1 itojun int pf_begin_altq(u_int32_t *);
92 1.1 itojun int pf_rollback_altq(u_int32_t);
93 1.1 itojun int pf_commit_altq(u_int32_t);
94 1.1 itojun #endif /* ALTQ */
95 1.1 itojun int pf_begin_rules(u_int32_t *, int, char *, char *);
96 1.1 itojun int pf_rollback_rules(u_int32_t, int, char *, char *);
97 1.1 itojun int pf_commit_rules(u_int32_t, int, char *, char *);
98 1.1 itojun
99 1.1 itojun extern struct timeout pf_expire_to;
100 1.1 itojun
101 1.1 itojun struct pf_rule pf_default_rule;
102 1.1 itojun
103 1.1 itojun #define TAGID_MAX 50000
104 1.1 itojun TAILQ_HEAD(pf_tags, pf_tagname) pf_tags = TAILQ_HEAD_INITIALIZER(pf_tags),
105 1.1 itojun pf_qids = TAILQ_HEAD_INITIALIZER(pf_qids);
106 1.1 itojun
107 1.1 itojun #if (PF_QNAME_SIZE != PF_TAG_NAME_SIZE)
108 1.1 itojun #error PF_QNAME_SIZE must be equal to PF_TAG_NAME_SIZE
109 1.1 itojun #endif
110 1.1 itojun static u_int16_t tagname2tag(struct pf_tags *, char *);
111 1.1 itojun static void tag2tagname(struct pf_tags *, u_int16_t, char *);
112 1.1 itojun static void tag_unref(struct pf_tags *, u_int16_t);
113 1.1 itojun
114 1.1 itojun #define DPFPRINTF(n, x) if (pf_status.debug >= (n)) printf x
115 1.1 itojun
116 1.1 itojun void
117 1.1 itojun pfattach(int num)
118 1.1 itojun {
119 1.1 itojun u_int32_t *timeout = pf_default_rule.timeout;
120 1.1 itojun
121 1.1 itojun pool_init(&pf_rule_pl, sizeof(struct pf_rule), 0, 0, 0, "pfrulepl",
122 1.1 itojun &pool_allocator_nointr);
123 1.1 itojun pool_init(&pf_src_tree_pl, sizeof(struct pf_src_node), 0, 0, 0,
124 1.1 itojun "pfsrctrpl", NULL);
125 1.1 itojun pool_init(&pf_state_pl, sizeof(struct pf_state), 0, 0, 0, "pfstatepl",
126 1.1 itojun NULL);
127 1.1 itojun pool_init(&pf_altq_pl, sizeof(struct pf_altq), 0, 0, 0, "pfaltqpl",
128 1.1 itojun NULL);
129 1.1 itojun pool_init(&pf_pooladdr_pl, sizeof(struct pf_pooladdr), 0, 0, 0,
130 1.1 itojun "pfpooladdrpl", NULL);
131 1.1 itojun pfr_initialize();
132 1.1 itojun pfi_initialize();
133 1.1 itojun pf_osfp_initialize();
134 1.1 itojun
135 1.1 itojun pool_sethardlimit(pf_pool_limits[PF_LIMIT_STATES].pp,
136 1.1 itojun pf_pool_limits[PF_LIMIT_STATES].limit, NULL, 0);
137 1.1 itojun
138 1.1 itojun RB_INIT(&tree_src_tracking);
139 1.1 itojun TAILQ_INIT(&pf_anchors);
140 1.1 itojun pf_init_ruleset(&pf_main_ruleset);
141 1.1 itojun TAILQ_INIT(&pf_altqs[0]);
142 1.1 itojun TAILQ_INIT(&pf_altqs[1]);
143 1.1 itojun TAILQ_INIT(&pf_pabuf);
144 1.1 itojun pf_altqs_active = &pf_altqs[0];
145 1.1 itojun pf_altqs_inactive = &pf_altqs[1];
146 1.1 itojun TAILQ_INIT(&state_updates);
147 1.1 itojun
148 1.1 itojun /* default rule should never be garbage collected */
149 1.1 itojun pf_default_rule.entries.tqe_prev = &pf_default_rule.entries.tqe_next;
150 1.1 itojun pf_default_rule.action = PF_PASS;
151 1.1 itojun pf_default_rule.nr = -1;
152 1.1 itojun
153 1.1 itojun /* initialize default timeouts */
154 1.1 itojun timeout[PFTM_TCP_FIRST_PACKET] = 120; /* First TCP packet */
155 1.1 itojun timeout[PFTM_TCP_OPENING] = 30; /* No response yet */
156 1.1 itojun timeout[PFTM_TCP_ESTABLISHED] = 24*60*60; /* Established */
157 1.1 itojun timeout[PFTM_TCP_CLOSING] = 15 * 60; /* Half closed */
158 1.1 itojun timeout[PFTM_TCP_FIN_WAIT] = 45; /* Got both FINs */
159 1.1 itojun timeout[PFTM_TCP_CLOSED] = 90; /* Got a RST */
160 1.1 itojun timeout[PFTM_UDP_FIRST_PACKET] = 60; /* First UDP packet */
161 1.1 itojun timeout[PFTM_UDP_SINGLE] = 30; /* Unidirectional */
162 1.1 itojun timeout[PFTM_UDP_MULTIPLE] = 60; /* Bidirectional */
163 1.1 itojun timeout[PFTM_ICMP_FIRST_PACKET] = 20; /* First ICMP packet */
164 1.1 itojun timeout[PFTM_ICMP_ERROR_REPLY] = 10; /* Got error response */
165 1.1 itojun timeout[PFTM_OTHER_FIRST_PACKET] = 60; /* First packet */
166 1.1 itojun timeout[PFTM_OTHER_SINGLE] = 30; /* Unidirectional */
167 1.1 itojun timeout[PFTM_OTHER_MULTIPLE] = 60; /* Bidirectional */
168 1.1 itojun timeout[PFTM_FRAG] = 30; /* Fragment expire */
169 1.1 itojun timeout[PFTM_INTERVAL] = 10; /* Expire interval */
170 1.1 itojun timeout[PFTM_SRC_NODE] = 0; /* Source tracking */
171 1.1 itojun
172 1.1 itojun timeout_set(&pf_expire_to, pf_purge_timeout, &pf_expire_to);
173 1.1 itojun timeout_add(&pf_expire_to, timeout[PFTM_INTERVAL] * hz);
174 1.1 itojun
175 1.1 itojun pf_normalize_init();
176 1.1 itojun bzero(&pf_status, sizeof(pf_status));
177 1.1 itojun pf_status.debug = PF_DEBUG_URGENT;
178 1.1 itojun
179 1.1 itojun /* XXX do our best to avoid a conflict */
180 1.1 itojun pf_status.hostid = arc4random();
181 1.1 itojun }
182 1.1 itojun
183 1.1 itojun int
184 1.1 itojun pfopen(dev_t dev, int flags, int fmt, struct proc *p)
185 1.1 itojun {
186 1.1 itojun if (minor(dev) >= 1)
187 1.1 itojun return (ENXIO);
188 1.1 itojun return (0);
189 1.1 itojun }
190 1.1 itojun
191 1.1 itojun int
192 1.1 itojun pfclose(dev_t dev, int flags, int fmt, struct proc *p)
193 1.1 itojun {
194 1.1 itojun if (minor(dev) >= 1)
195 1.1 itojun return (ENXIO);
196 1.1 itojun return (0);
197 1.1 itojun }
198 1.1 itojun
199 1.1 itojun struct pf_pool *
200 1.1 itojun pf_get_pool(char *anchorname, char *rulesetname, u_int32_t ticket,
201 1.1 itojun u_int8_t rule_action, u_int8_t rule_number, u_int8_t r_last,
202 1.1 itojun u_int8_t active, u_int8_t check_ticket)
203 1.1 itojun {
204 1.1 itojun struct pf_ruleset *ruleset;
205 1.1 itojun struct pf_rule *rule;
206 1.1 itojun int rs_num;
207 1.1 itojun
208 1.1 itojun ruleset = pf_find_ruleset(anchorname, rulesetname);
209 1.1 itojun if (ruleset == NULL)
210 1.1 itojun return (NULL);
211 1.1 itojun rs_num = pf_get_ruleset_number(rule_action);
212 1.1 itojun if (rs_num >= PF_RULESET_MAX)
213 1.1 itojun return (NULL);
214 1.1 itojun if (active) {
215 1.1 itojun if (check_ticket && ticket !=
216 1.1 itojun ruleset->rules[rs_num].active.ticket)
217 1.1 itojun return (NULL);
218 1.1 itojun if (r_last)
219 1.1 itojun rule = TAILQ_LAST(ruleset->rules[rs_num].active.ptr,
220 1.1 itojun pf_rulequeue);
221 1.1 itojun else
222 1.1 itojun rule = TAILQ_FIRST(ruleset->rules[rs_num].active.ptr);
223 1.1 itojun } else {
224 1.1 itojun if (check_ticket && ticket !=
225 1.1 itojun ruleset->rules[rs_num].inactive.ticket)
226 1.1 itojun return (NULL);
227 1.1 itojun if (r_last)
228 1.1 itojun rule = TAILQ_LAST(ruleset->rules[rs_num].inactive.ptr,
229 1.1 itojun pf_rulequeue);
230 1.1 itojun else
231 1.1 itojun rule = TAILQ_FIRST(ruleset->rules[rs_num].inactive.ptr);
232 1.1 itojun }
233 1.1 itojun if (!r_last) {
234 1.1 itojun while ((rule != NULL) && (rule->nr != rule_number))
235 1.1 itojun rule = TAILQ_NEXT(rule, entries);
236 1.1 itojun }
237 1.1 itojun if (rule == NULL)
238 1.1 itojun return (NULL);
239 1.1 itojun
240 1.1 itojun return (&rule->rpool);
241 1.1 itojun }
242 1.1 itojun
243 1.1 itojun int
244 1.1 itojun pf_get_ruleset_number(u_int8_t action)
245 1.1 itojun {
246 1.1 itojun switch (action) {
247 1.1 itojun case PF_SCRUB:
248 1.1 itojun return (PF_RULESET_SCRUB);
249 1.1 itojun break;
250 1.1 itojun case PF_PASS:
251 1.1 itojun case PF_DROP:
252 1.1 itojun return (PF_RULESET_FILTER);
253 1.1 itojun break;
254 1.1 itojun case PF_NAT:
255 1.1 itojun case PF_NONAT:
256 1.1 itojun return (PF_RULESET_NAT);
257 1.1 itojun break;
258 1.1 itojun case PF_BINAT:
259 1.1 itojun case PF_NOBINAT:
260 1.1 itojun return (PF_RULESET_BINAT);
261 1.1 itojun break;
262 1.1 itojun case PF_RDR:
263 1.1 itojun case PF_NORDR:
264 1.1 itojun return (PF_RULESET_RDR);
265 1.1 itojun break;
266 1.1 itojun default:
267 1.1 itojun return (PF_RULESET_MAX);
268 1.1 itojun break;
269 1.1 itojun }
270 1.1 itojun }
271 1.1 itojun
272 1.1 itojun void
273 1.1 itojun pf_init_ruleset(struct pf_ruleset *ruleset)
274 1.1 itojun {
275 1.1 itojun int i;
276 1.1 itojun
277 1.1 itojun memset(ruleset, 0, sizeof(struct pf_ruleset));
278 1.1 itojun for (i = 0; i < PF_RULESET_MAX; i++) {
279 1.1 itojun TAILQ_INIT(&ruleset->rules[i].queues[0]);
280 1.1 itojun TAILQ_INIT(&ruleset->rules[i].queues[1]);
281 1.1 itojun ruleset->rules[i].active.ptr = &ruleset->rules[i].queues[0];
282 1.1 itojun ruleset->rules[i].inactive.ptr = &ruleset->rules[i].queues[1];
283 1.1 itojun }
284 1.1 itojun }
285 1.1 itojun
286 1.1 itojun struct pf_anchor *
287 1.1 itojun pf_find_anchor(const char *anchorname)
288 1.1 itojun {
289 1.1 itojun struct pf_anchor *anchor;
290 1.1 itojun int n = -1;
291 1.1 itojun
292 1.1 itojun anchor = TAILQ_FIRST(&pf_anchors);
293 1.1 itojun while (anchor != NULL && (n = strcmp(anchor->name, anchorname)) < 0)
294 1.1 itojun anchor = TAILQ_NEXT(anchor, entries);
295 1.1 itojun if (n == 0)
296 1.1 itojun return (anchor);
297 1.1 itojun else
298 1.1 itojun return (NULL);
299 1.1 itojun }
300 1.1 itojun
301 1.1 itojun struct pf_ruleset *
302 1.1 itojun pf_find_ruleset(char *anchorname, char *rulesetname)
303 1.1 itojun {
304 1.1 itojun struct pf_anchor *anchor;
305 1.1 itojun struct pf_ruleset *ruleset;
306 1.1 itojun
307 1.1 itojun if (!anchorname[0] && !rulesetname[0])
308 1.1 itojun return (&pf_main_ruleset);
309 1.1 itojun if (!anchorname[0] || !rulesetname[0])
310 1.1 itojun return (NULL);
311 1.1 itojun anchorname[PF_ANCHOR_NAME_SIZE-1] = 0;
312 1.1 itojun rulesetname[PF_RULESET_NAME_SIZE-1] = 0;
313 1.1 itojun anchor = pf_find_anchor(anchorname);
314 1.1 itojun if (anchor == NULL)
315 1.1 itojun return (NULL);
316 1.1 itojun ruleset = TAILQ_FIRST(&anchor->rulesets);
317 1.1 itojun while (ruleset != NULL && strcmp(ruleset->name, rulesetname) < 0)
318 1.1 itojun ruleset = TAILQ_NEXT(ruleset, entries);
319 1.1 itojun if (ruleset != NULL && !strcmp(ruleset->name, rulesetname))
320 1.1 itojun return (ruleset);
321 1.1 itojun else
322 1.1 itojun return (NULL);
323 1.1 itojun }
324 1.1 itojun
325 1.1 itojun struct pf_ruleset *
326 1.1 itojun pf_find_or_create_ruleset(char anchorname[PF_ANCHOR_NAME_SIZE],
327 1.1 itojun char rulesetname[PF_RULESET_NAME_SIZE])
328 1.1 itojun {
329 1.1 itojun struct pf_anchor *anchor, *a;
330 1.1 itojun struct pf_ruleset *ruleset, *r;
331 1.1 itojun
332 1.1 itojun if (!anchorname[0] && !rulesetname[0])
333 1.1 itojun return (&pf_main_ruleset);
334 1.1 itojun if (!anchorname[0] || !rulesetname[0])
335 1.1 itojun return (NULL);
336 1.1 itojun anchorname[PF_ANCHOR_NAME_SIZE-1] = 0;
337 1.1 itojun rulesetname[PF_RULESET_NAME_SIZE-1] = 0;
338 1.1 itojun a = TAILQ_FIRST(&pf_anchors);
339 1.1 itojun while (a != NULL && strcmp(a->name, anchorname) < 0)
340 1.1 itojun a = TAILQ_NEXT(a, entries);
341 1.1 itojun if (a != NULL && !strcmp(a->name, anchorname))
342 1.1 itojun anchor = a;
343 1.1 itojun else {
344 1.1 itojun anchor = (struct pf_anchor *)malloc(sizeof(struct pf_anchor),
345 1.1 itojun M_TEMP, M_NOWAIT);
346 1.1 itojun if (anchor == NULL)
347 1.1 itojun return (NULL);
348 1.1 itojun memset(anchor, 0, sizeof(struct pf_anchor));
349 1.1 itojun bcopy(anchorname, anchor->name, sizeof(anchor->name));
350 1.1 itojun TAILQ_INIT(&anchor->rulesets);
351 1.1 itojun if (a != NULL)
352 1.1 itojun TAILQ_INSERT_BEFORE(a, anchor, entries);
353 1.1 itojun else
354 1.1 itojun TAILQ_INSERT_TAIL(&pf_anchors, anchor, entries);
355 1.1 itojun }
356 1.1 itojun r = TAILQ_FIRST(&anchor->rulesets);
357 1.1 itojun while (r != NULL && strcmp(r->name, rulesetname) < 0)
358 1.1 itojun r = TAILQ_NEXT(r, entries);
359 1.1 itojun if (r != NULL && !strcmp(r->name, rulesetname))
360 1.1 itojun return (r);
361 1.1 itojun ruleset = (struct pf_ruleset *)malloc(sizeof(struct pf_ruleset),
362 1.1 itojun M_TEMP, M_NOWAIT);
363 1.1 itojun if (ruleset != NULL) {
364 1.1 itojun pf_init_ruleset(ruleset);
365 1.1 itojun bcopy(rulesetname, ruleset->name, sizeof(ruleset->name));
366 1.1 itojun ruleset->anchor = anchor;
367 1.1 itojun if (r != NULL)
368 1.1 itojun TAILQ_INSERT_BEFORE(r, ruleset, entries);
369 1.1 itojun else
370 1.1 itojun TAILQ_INSERT_TAIL(&anchor->rulesets, ruleset, entries);
371 1.1 itojun }
372 1.1 itojun return (ruleset);
373 1.1 itojun }
374 1.1 itojun
375 1.1 itojun void
376 1.1 itojun pf_remove_if_empty_ruleset(struct pf_ruleset *ruleset)
377 1.1 itojun {
378 1.1 itojun struct pf_anchor *anchor;
379 1.1 itojun int i;
380 1.1 itojun
381 1.1 itojun if (ruleset == NULL || ruleset->anchor == NULL || ruleset->tables > 0 ||
382 1.1 itojun ruleset->topen)
383 1.1 itojun return;
384 1.1 itojun for (i = 0; i < PF_RULESET_MAX; ++i)
385 1.1 itojun if (!TAILQ_EMPTY(ruleset->rules[i].active.ptr) ||
386 1.1 itojun !TAILQ_EMPTY(ruleset->rules[i].inactive.ptr) ||
387 1.1 itojun ruleset->rules[i].inactive.open)
388 1.1 itojun return;
389 1.1 itojun
390 1.1 itojun anchor = ruleset->anchor;
391 1.1 itojun TAILQ_REMOVE(&anchor->rulesets, ruleset, entries);
392 1.1 itojun free(ruleset, M_TEMP);
393 1.1 itojun
394 1.1 itojun if (TAILQ_EMPTY(&anchor->rulesets)) {
395 1.1 itojun TAILQ_REMOVE(&pf_anchors, anchor, entries);
396 1.1 itojun free(anchor, M_TEMP);
397 1.1 itojun pf_update_anchor_rules();
398 1.1 itojun }
399 1.1 itojun }
400 1.1 itojun
401 1.1 itojun void
402 1.1 itojun pf_mv_pool(struct pf_palist *poola, struct pf_palist *poolb)
403 1.1 itojun {
404 1.1 itojun struct pf_pooladdr *mv_pool_pa;
405 1.1 itojun
406 1.1 itojun while ((mv_pool_pa = TAILQ_FIRST(poola)) != NULL) {
407 1.1 itojun TAILQ_REMOVE(poola, mv_pool_pa, entries);
408 1.1 itojun TAILQ_INSERT_TAIL(poolb, mv_pool_pa, entries);
409 1.1 itojun }
410 1.1 itojun }
411 1.1 itojun
412 1.1 itojun void
413 1.1 itojun pf_empty_pool(struct pf_palist *poola)
414 1.1 itojun {
415 1.1 itojun struct pf_pooladdr *empty_pool_pa;
416 1.1 itojun
417 1.1 itojun while ((empty_pool_pa = TAILQ_FIRST(poola)) != NULL) {
418 1.1 itojun pfi_dynaddr_remove(&empty_pool_pa->addr);
419 1.1 itojun pf_tbladdr_remove(&empty_pool_pa->addr);
420 1.1 itojun pfi_detach_rule(empty_pool_pa->kif);
421 1.1 itojun TAILQ_REMOVE(poola, empty_pool_pa, entries);
422 1.1 itojun pool_put(&pf_pooladdr_pl, empty_pool_pa);
423 1.1 itojun }
424 1.1 itojun }
425 1.1 itojun
426 1.1 itojun void
427 1.1 itojun pf_rm_rule(struct pf_rulequeue *rulequeue, struct pf_rule *rule)
428 1.1 itojun {
429 1.1 itojun if (rulequeue != NULL) {
430 1.1 itojun if (rule->states <= 0) {
431 1.1 itojun /*
432 1.1 itojun * XXX - we need to remove the table *before* detaching
433 1.1 itojun * the rule to make sure the table code does not delete
434 1.1 itojun * the anchor under our feet.
435 1.1 itojun */
436 1.1 itojun pf_tbladdr_remove(&rule->src.addr);
437 1.1 itojun pf_tbladdr_remove(&rule->dst.addr);
438 1.1 itojun }
439 1.1 itojun TAILQ_REMOVE(rulequeue, rule, entries);
440 1.1 itojun rule->entries.tqe_prev = NULL;
441 1.1 itojun rule->nr = -1;
442 1.1 itojun }
443 1.1 itojun
444 1.1 itojun if (rule->states > 0 || rule->src_nodes > 0 ||
445 1.1 itojun rule->entries.tqe_prev != NULL)
446 1.1 itojun return;
447 1.1 itojun pf_tag_unref(rule->tag);
448 1.1 itojun pf_tag_unref(rule->match_tag);
449 1.1 itojun #ifdef ALTQ
450 1.1 itojun if (rule->pqid != rule->qid)
451 1.1 itojun pf_qid_unref(rule->pqid);
452 1.1 itojun pf_qid_unref(rule->qid);
453 1.1 itojun #endif
454 1.1 itojun pfi_dynaddr_remove(&rule->src.addr);
455 1.1 itojun pfi_dynaddr_remove(&rule->dst.addr);
456 1.1 itojun if (rulequeue == NULL) {
457 1.1 itojun pf_tbladdr_remove(&rule->src.addr);
458 1.1 itojun pf_tbladdr_remove(&rule->dst.addr);
459 1.1 itojun }
460 1.1 itojun pfi_detach_rule(rule->kif);
461 1.1 itojun pf_empty_pool(&rule->rpool.list);
462 1.1 itojun pool_put(&pf_rule_pl, rule);
463 1.1 itojun }
464 1.1 itojun
465 1.1 itojun static u_int16_t
466 1.1 itojun tagname2tag(struct pf_tags *head, char *tagname)
467 1.1 itojun {
468 1.1 itojun struct pf_tagname *tag, *p = NULL;
469 1.1 itojun u_int16_t new_tagid = 1;
470 1.1 itojun
471 1.1 itojun TAILQ_FOREACH(tag, head, entries)
472 1.1 itojun if (strcmp(tagname, tag->name) == 0) {
473 1.1 itojun tag->ref++;
474 1.1 itojun return (tag->tag);
475 1.1 itojun }
476 1.1 itojun
477 1.1 itojun /*
478 1.1 itojun * to avoid fragmentation, we do a linear search from the beginning
479 1.1 itojun * and take the first free slot we find. if there is none or the list
480 1.1 itojun * is empty, append a new entry at the end.
481 1.1 itojun */
482 1.1 itojun
483 1.1 itojun /* new entry */
484 1.1 itojun if (!TAILQ_EMPTY(head))
485 1.1 itojun for (p = TAILQ_FIRST(head); p != NULL &&
486 1.1 itojun p->tag == new_tagid; p = TAILQ_NEXT(p, entries))
487 1.1 itojun new_tagid = p->tag + 1;
488 1.1 itojun
489 1.1 itojun if (new_tagid > TAGID_MAX)
490 1.1 itojun return (0);
491 1.1 itojun
492 1.1 itojun /* allocate and fill new struct pf_tagname */
493 1.1 itojun tag = (struct pf_tagname *)malloc(sizeof(struct pf_tagname),
494 1.1 itojun M_TEMP, M_NOWAIT);
495 1.1 itojun if (tag == NULL)
496 1.1 itojun return (0);
497 1.1 itojun bzero(tag, sizeof(struct pf_tagname));
498 1.1 itojun strlcpy(tag->name, tagname, sizeof(tag->name));
499 1.1 itojun tag->tag = new_tagid;
500 1.1 itojun tag->ref++;
501 1.1 itojun
502 1.1 itojun if (p != NULL) /* insert new entry before p */
503 1.1 itojun TAILQ_INSERT_BEFORE(p, tag, entries);
504 1.1 itojun else /* either list empty or no free slot in between */
505 1.1 itojun TAILQ_INSERT_TAIL(head, tag, entries);
506 1.1 itojun
507 1.1 itojun return (tag->tag);
508 1.1 itojun }
509 1.1 itojun
510 1.1 itojun static void
511 1.1 itojun tag2tagname(struct pf_tags *head, u_int16_t tagid, char *p)
512 1.1 itojun {
513 1.1 itojun struct pf_tagname *tag;
514 1.1 itojun
515 1.1 itojun TAILQ_FOREACH(tag, head, entries)
516 1.1 itojun if (tag->tag == tagid) {
517 1.1 itojun strlcpy(p, tag->name, PF_TAG_NAME_SIZE);
518 1.1 itojun return;
519 1.1 itojun }
520 1.1 itojun }
521 1.1 itojun
522 1.1 itojun static void
523 1.1 itojun tag_unref(struct pf_tags *head, u_int16_t tag)
524 1.1 itojun {
525 1.1 itojun struct pf_tagname *p, *next;
526 1.1 itojun
527 1.1 itojun if (tag == 0)
528 1.1 itojun return;
529 1.1 itojun
530 1.1 itojun for (p = TAILQ_FIRST(head); p != NULL; p = next) {
531 1.1 itojun next = TAILQ_NEXT(p, entries);
532 1.1 itojun if (tag == p->tag) {
533 1.1 itojun if (--p->ref == 0) {
534 1.1 itojun TAILQ_REMOVE(head, p, entries);
535 1.1 itojun free(p, M_TEMP);
536 1.1 itojun }
537 1.1 itojun break;
538 1.1 itojun }
539 1.1 itojun }
540 1.1 itojun }
541 1.1 itojun
542 1.1 itojun u_int16_t
543 1.1 itojun pf_tagname2tag(char *tagname)
544 1.1 itojun {
545 1.1 itojun return (tagname2tag(&pf_tags, tagname));
546 1.1 itojun }
547 1.1 itojun
548 1.1 itojun void
549 1.1 itojun pf_tag2tagname(u_int16_t tagid, char *p)
550 1.1 itojun {
551 1.1 itojun return (tag2tagname(&pf_tags, tagid, p));
552 1.1 itojun }
553 1.1 itojun
554 1.1 itojun void
555 1.1 itojun pf_tag_unref(u_int16_t tag)
556 1.1 itojun {
557 1.1 itojun return (tag_unref(&pf_tags, tag));
558 1.1 itojun }
559 1.1 itojun
560 1.1 itojun #ifdef ALTQ
561 1.1 itojun u_int32_t
562 1.1 itojun pf_qname2qid(char *qname)
563 1.1 itojun {
564 1.1 itojun return ((u_int32_t)tagname2tag(&pf_qids, qname));
565 1.1 itojun }
566 1.1 itojun
567 1.1 itojun void
568 1.1 itojun pf_qid2qname(u_int32_t qid, char *p)
569 1.1 itojun {
570 1.1 itojun return (tag2tagname(&pf_qids, (u_int16_t)qid, p));
571 1.1 itojun }
572 1.1 itojun
573 1.1 itojun void
574 1.1 itojun pf_qid_unref(u_int32_t qid)
575 1.1 itojun {
576 1.1 itojun return (tag_unref(&pf_qids, (u_int16_t)qid));
577 1.1 itojun }
578 1.1 itojun
579 1.1 itojun int
580 1.1 itojun pf_begin_altq(u_int32_t *ticket)
581 1.1 itojun {
582 1.1 itojun struct pf_altq *altq;
583 1.1 itojun int error = 0;
584 1.1 itojun
585 1.1 itojun /* Purge the old altq list */
586 1.1 itojun while ((altq = TAILQ_FIRST(pf_altqs_inactive)) != NULL) {
587 1.1 itojun TAILQ_REMOVE(pf_altqs_inactive, altq, entries);
588 1.1 itojun if (altq->qname[0] == 0) {
589 1.1 itojun /* detach and destroy the discipline */
590 1.1 itojun error = altq_remove(altq);
591 1.1 itojun } else
592 1.1 itojun pf_qid_unref(altq->qid);
593 1.1 itojun pool_put(&pf_altq_pl, altq);
594 1.1 itojun }
595 1.1 itojun if (error)
596 1.1 itojun return (error);
597 1.1 itojun *ticket = ++ticket_altqs_inactive;
598 1.1 itojun altqs_inactive_open = 1;
599 1.1 itojun return (0);
600 1.1 itojun }
601 1.1 itojun
602 1.1 itojun int
603 1.1 itojun pf_rollback_altq(u_int32_t ticket)
604 1.1 itojun {
605 1.1 itojun struct pf_altq *altq;
606 1.1 itojun int error = 0;
607 1.1 itojun
608 1.1 itojun if (!altqs_inactive_open || ticket != ticket_altqs_inactive)
609 1.1 itojun return (0);
610 1.1 itojun /* Purge the old altq list */
611 1.1 itojun while ((altq = TAILQ_FIRST(pf_altqs_inactive)) != NULL) {
612 1.1 itojun TAILQ_REMOVE(pf_altqs_inactive, altq, entries);
613 1.1 itojun if (altq->qname[0] == 0) {
614 1.1 itojun /* detach and destroy the discipline */
615 1.1 itojun error = altq_remove(altq);
616 1.1 itojun } else
617 1.1 itojun pf_qid_unref(altq->qid);
618 1.1 itojun pool_put(&pf_altq_pl, altq);
619 1.1 itojun }
620 1.1 itojun altqs_inactive_open = 0;
621 1.1 itojun return (error);
622 1.1 itojun }
623 1.1 itojun
624 1.1 itojun int
625 1.1 itojun pf_commit_altq(u_int32_t ticket)
626 1.1 itojun {
627 1.1 itojun struct pf_altqqueue *old_altqs;
628 1.1 itojun struct pf_altq *altq;
629 1.1 itojun int s, err, error = 0;
630 1.1 itojun
631 1.1 itojun if (!altqs_inactive_open || ticket != ticket_altqs_inactive)
632 1.1 itojun return (EBUSY);
633 1.1 itojun
634 1.1 itojun /* swap altqs, keep the old. */
635 1.1 itojun s = splsoftnet();
636 1.1 itojun old_altqs = pf_altqs_active;
637 1.1 itojun pf_altqs_active = pf_altqs_inactive;
638 1.1 itojun pf_altqs_inactive = old_altqs;
639 1.1 itojun ticket_altqs_active = ticket_altqs_inactive;
640 1.1 itojun
641 1.1 itojun /* Attach new disciplines */
642 1.1 itojun TAILQ_FOREACH(altq, pf_altqs_active, entries) {
643 1.1 itojun if (altq->qname[0] == 0) {
644 1.1 itojun /* attach the discipline */
645 1.1 itojun error = altq_pfattach(altq);
646 1.1 itojun if (error) {
647 1.1 itojun splx(s);
648 1.1 itojun return (error);
649 1.1 itojun }
650 1.1 itojun }
651 1.1 itojun }
652 1.1 itojun
653 1.1 itojun /* Purge the old altq list */
654 1.1 itojun while ((altq = TAILQ_FIRST(pf_altqs_inactive)) != NULL) {
655 1.1 itojun TAILQ_REMOVE(pf_altqs_inactive, altq, entries);
656 1.1 itojun if (altq->qname[0] == 0) {
657 1.1 itojun /* detach and destroy the discipline */
658 1.1 itojun err = altq_pfdetach(altq);
659 1.1 itojun if (err != 0 && error == 0)
660 1.1 itojun error = err;
661 1.1 itojun err = altq_remove(altq);
662 1.1 itojun if (err != 0 && error == 0)
663 1.1 itojun error = err;
664 1.1 itojun } else
665 1.1 itojun pf_qid_unref(altq->qid);
666 1.1 itojun pool_put(&pf_altq_pl, altq);
667 1.1 itojun }
668 1.1 itojun splx(s);
669 1.1 itojun
670 1.1 itojun altqs_inactive_open = 0;
671 1.1 itojun return (error);
672 1.1 itojun }
673 1.1 itojun #endif /* ALTQ */
674 1.1 itojun
675 1.1 itojun int
676 1.1 itojun pf_begin_rules(u_int32_t *ticket, int rs_num, char *anchor, char *ruleset)
677 1.1 itojun {
678 1.1 itojun struct pf_ruleset *rs;
679 1.1 itojun struct pf_rule *rule;
680 1.1 itojun
681 1.1 itojun if (rs_num < 0 || rs_num >= PF_RULESET_MAX)
682 1.1 itojun return (EINVAL);
683 1.1 itojun rs = pf_find_or_create_ruleset(anchor, ruleset);
684 1.1 itojun if (rs == NULL)
685 1.1 itojun return (EINVAL);
686 1.1 itojun while ((rule = TAILQ_FIRST(rs->rules[rs_num].inactive.ptr)) != NULL)
687 1.1 itojun pf_rm_rule(rs->rules[rs_num].inactive.ptr, rule);
688 1.1 itojun *ticket = ++rs->rules[rs_num].inactive.ticket;
689 1.1 itojun rs->rules[rs_num].inactive.open = 1;
690 1.1 itojun return (0);
691 1.1 itojun }
692 1.1 itojun
693 1.1 itojun int
694 1.1 itojun pf_rollback_rules(u_int32_t ticket, int rs_num, char *anchor, char *ruleset)
695 1.1 itojun {
696 1.1 itojun struct pf_ruleset *rs;
697 1.1 itojun struct pf_rule *rule;
698 1.1 itojun
699 1.1 itojun if (rs_num < 0 || rs_num >= PF_RULESET_MAX)
700 1.1 itojun return (EINVAL);
701 1.1 itojun rs = pf_find_ruleset(anchor, ruleset);
702 1.1 itojun if (rs == NULL || !rs->rules[rs_num].inactive.open ||
703 1.1 itojun rs->rules[rs_num].inactive.ticket != ticket)
704 1.1 itojun return (0);
705 1.1 itojun while ((rule = TAILQ_FIRST(rs->rules[rs_num].inactive.ptr)) != NULL)
706 1.1 itojun pf_rm_rule(rs->rules[rs_num].inactive.ptr, rule);
707 1.1 itojun rs->rules[rs_num].inactive.open = 0;
708 1.1 itojun return (0);
709 1.1 itojun }
710 1.1 itojun
711 1.1 itojun int
712 1.1 itojun pf_commit_rules(u_int32_t ticket, int rs_num, char *anchor, char *ruleset)
713 1.1 itojun {
714 1.1 itojun struct pf_ruleset *rs;
715 1.1 itojun struct pf_rule *rule;
716 1.1 itojun struct pf_rulequeue *old_rules;
717 1.1 itojun int s;
718 1.1 itojun
719 1.1 itojun if (rs_num < 0 || rs_num >= PF_RULESET_MAX)
720 1.1 itojun return (EINVAL);
721 1.1 itojun rs = pf_find_ruleset(anchor, ruleset);
722 1.1 itojun if (rs == NULL || !rs->rules[rs_num].inactive.open ||
723 1.1 itojun ticket != rs->rules[rs_num].inactive.ticket)
724 1.1 itojun return (EBUSY);
725 1.1 itojun
726 1.1 itojun /* Swap rules, keep the old. */
727 1.1 itojun s = splsoftnet();
728 1.1 itojun old_rules = rs->rules[rs_num].active.ptr;
729 1.1 itojun rs->rules[rs_num].active.ptr =
730 1.1 itojun rs->rules[rs_num].inactive.ptr;
731 1.1 itojun rs->rules[rs_num].inactive.ptr = old_rules;
732 1.1 itojun rs->rules[rs_num].active.ticket =
733 1.1 itojun rs->rules[rs_num].inactive.ticket;
734 1.1 itojun pf_calc_skip_steps(rs->rules[rs_num].active.ptr);
735 1.1 itojun
736 1.1 itojun /* Purge the old rule list. */
737 1.1 itojun while ((rule = TAILQ_FIRST(old_rules)) != NULL)
738 1.1 itojun pf_rm_rule(old_rules, rule);
739 1.1 itojun rs->rules[rs_num].inactive.open = 0;
740 1.1 itojun pf_remove_if_empty_ruleset(rs);
741 1.1 itojun pf_update_anchor_rules();
742 1.1 itojun splx(s);
743 1.1 itojun return (0);
744 1.1 itojun }
745 1.1 itojun
746 1.1 itojun int
747 1.1 itojun pfioctl(dev_t dev, u_long cmd, caddr_t addr, int flags, struct proc *p)
748 1.1 itojun {
749 1.1 itojun struct pf_pooladdr *pa = NULL;
750 1.1 itojun struct pf_pool *pool = NULL;
751 1.1 itojun int s;
752 1.1 itojun int error = 0;
753 1.1 itojun
754 1.1 itojun /* XXX keep in sync with switch() below */
755 1.1 itojun if (securelevel > 1)
756 1.1 itojun switch (cmd) {
757 1.1 itojun case DIOCGETRULES:
758 1.1 itojun case DIOCGETRULE:
759 1.1 itojun case DIOCGETADDRS:
760 1.1 itojun case DIOCGETADDR:
761 1.1 itojun case DIOCGETSTATE:
762 1.1 itojun case DIOCSETSTATUSIF:
763 1.1 itojun case DIOCGETSTATUS:
764 1.1 itojun case DIOCCLRSTATUS:
765 1.1 itojun case DIOCNATLOOK:
766 1.1 itojun case DIOCSETDEBUG:
767 1.1 itojun case DIOCGETSTATES:
768 1.1 itojun case DIOCGETTIMEOUT:
769 1.1 itojun case DIOCCLRRULECTRS:
770 1.1 itojun case DIOCGETLIMIT:
771 1.1 itojun case DIOCGETALTQS:
772 1.1 itojun case DIOCGETALTQ:
773 1.1 itojun case DIOCGETQSTATS:
774 1.1 itojun case DIOCGETANCHORS:
775 1.1 itojun case DIOCGETANCHOR:
776 1.1 itojun case DIOCGETRULESETS:
777 1.1 itojun case DIOCGETRULESET:
778 1.1 itojun case DIOCRGETTABLES:
779 1.1 itojun case DIOCRGETTSTATS:
780 1.1 itojun case DIOCRCLRTSTATS:
781 1.1 itojun case DIOCRCLRADDRS:
782 1.1 itojun case DIOCRADDADDRS:
783 1.1 itojun case DIOCRDELADDRS:
784 1.1 itojun case DIOCRSETADDRS:
785 1.1 itojun case DIOCRGETADDRS:
786 1.1 itojun case DIOCRGETASTATS:
787 1.1 itojun case DIOCRCLRASTATS:
788 1.1 itojun case DIOCRTSTADDRS:
789 1.1 itojun case DIOCOSFPGET:
790 1.1 itojun case DIOCGETSRCNODES:
791 1.1 itojun case DIOCCLRSRCNODES:
792 1.1 itojun case DIOCIGETIFACES:
793 1.1 itojun case DIOCICLRISTATS:
794 1.1 itojun break;
795 1.1 itojun case DIOCRCLRTABLES:
796 1.1 itojun case DIOCRADDTABLES:
797 1.1 itojun case DIOCRDELTABLES:
798 1.1 itojun case DIOCRSETTFLAGS:
799 1.1 itojun if (((struct pfioc_table *)addr)->pfrio_flags &
800 1.1 itojun PFR_FLAG_DUMMY)
801 1.1 itojun break; /* dummy operation ok */
802 1.1 itojun return (EPERM);
803 1.1 itojun default:
804 1.1 itojun return (EPERM);
805 1.1 itojun }
806 1.1 itojun
807 1.1 itojun if (!(flags & FWRITE))
808 1.1 itojun switch (cmd) {
809 1.1 itojun case DIOCGETRULES:
810 1.1 itojun case DIOCGETRULE:
811 1.1 itojun case DIOCGETADDRS:
812 1.1 itojun case DIOCGETADDR:
813 1.1 itojun case DIOCGETSTATE:
814 1.1 itojun case DIOCGETSTATUS:
815 1.1 itojun case DIOCGETSTATES:
816 1.1 itojun case DIOCGETTIMEOUT:
817 1.1 itojun case DIOCGETLIMIT:
818 1.1 itojun case DIOCGETALTQS:
819 1.1 itojun case DIOCGETALTQ:
820 1.1 itojun case DIOCGETQSTATS:
821 1.1 itojun case DIOCGETANCHORS:
822 1.1 itojun case DIOCGETANCHOR:
823 1.1 itojun case DIOCGETRULESETS:
824 1.1 itojun case DIOCGETRULESET:
825 1.1 itojun case DIOCRGETTABLES:
826 1.1 itojun case DIOCRGETTSTATS:
827 1.1 itojun case DIOCRGETADDRS:
828 1.1 itojun case DIOCRGETASTATS:
829 1.1 itojun case DIOCRTSTADDRS:
830 1.1 itojun case DIOCOSFPGET:
831 1.1 itojun case DIOCGETSRCNODES:
832 1.1 itojun case DIOCIGETIFACES:
833 1.1 itojun break;
834 1.1 itojun case DIOCRCLRTABLES:
835 1.1 itojun case DIOCRADDTABLES:
836 1.1 itojun case DIOCRDELTABLES:
837 1.1 itojun case DIOCRCLRTSTATS:
838 1.1 itojun case DIOCRCLRADDRS:
839 1.1 itojun case DIOCRADDADDRS:
840 1.1 itojun case DIOCRDELADDRS:
841 1.1 itojun case DIOCRSETADDRS:
842 1.1 itojun case DIOCRSETTFLAGS:
843 1.1 itojun if (((struct pfioc_table *)addr)->pfrio_flags &
844 1.1 itojun PFR_FLAG_DUMMY)
845 1.1 itojun break; /* dummy operation ok */
846 1.1 itojun return (EACCES);
847 1.1 itojun default:
848 1.1 itojun return (EACCES);
849 1.1 itojun }
850 1.1 itojun
851 1.1 itojun switch (cmd) {
852 1.1 itojun
853 1.1 itojun case DIOCSTART:
854 1.1 itojun if (pf_status.running)
855 1.1 itojun error = EEXIST;
856 1.1 itojun else {
857 1.1 itojun pf_status.running = 1;
858 1.1 itojun pf_status.since = time.tv_sec;
859 1.1 itojun if (pf_status.stateid == 0) {
860 1.1 itojun pf_status.stateid = time.tv_sec;
861 1.1 itojun pf_status.stateid = pf_status.stateid << 32;
862 1.1 itojun }
863 1.1 itojun DPFPRINTF(PF_DEBUG_MISC, ("pf: started\n"));
864 1.1 itojun }
865 1.1 itojun break;
866 1.1 itojun
867 1.1 itojun case DIOCSTOP:
868 1.1 itojun if (!pf_status.running)
869 1.1 itojun error = ENOENT;
870 1.1 itojun else {
871 1.1 itojun pf_status.running = 0;
872 1.1 itojun pf_status.since = time.tv_sec;
873 1.1 itojun DPFPRINTF(PF_DEBUG_MISC, ("pf: stopped\n"));
874 1.1 itojun }
875 1.1 itojun break;
876 1.1 itojun
877 1.1 itojun case DIOCBEGINRULES: {
878 1.1 itojun struct pfioc_rule *pr = (struct pfioc_rule *)addr;
879 1.1 itojun
880 1.1 itojun error = pf_begin_rules(&pr->ticket, pf_get_ruleset_number(
881 1.1 itojun pr->rule.action), pr->anchor, pr->ruleset);
882 1.1 itojun break;
883 1.1 itojun }
884 1.1 itojun
885 1.1 itojun case DIOCADDRULE: {
886 1.1 itojun struct pfioc_rule *pr = (struct pfioc_rule *)addr;
887 1.1 itojun struct pf_ruleset *ruleset;
888 1.1 itojun struct pf_rule *rule, *tail;
889 1.1 itojun struct pf_pooladdr *pa;
890 1.1 itojun int rs_num;
891 1.1 itojun
892 1.1 itojun ruleset = pf_find_ruleset(pr->anchor, pr->ruleset);
893 1.1 itojun if (ruleset == NULL) {
894 1.1 itojun error = EINVAL;
895 1.1 itojun break;
896 1.1 itojun }
897 1.1 itojun rs_num = pf_get_ruleset_number(pr->rule.action);
898 1.1 itojun if (rs_num >= PF_RULESET_MAX) {
899 1.1 itojun error = EINVAL;
900 1.1 itojun break;
901 1.1 itojun }
902 1.1 itojun if (pr->rule.anchorname[0] && ruleset != &pf_main_ruleset) {
903 1.1 itojun error = EINVAL;
904 1.1 itojun break;
905 1.1 itojun }
906 1.1 itojun if (pr->rule.return_icmp >> 8 > ICMP_MAXTYPE) {
907 1.1 itojun error = EINVAL;
908 1.1 itojun break;
909 1.1 itojun }
910 1.1 itojun if (pr->ticket != ruleset->rules[rs_num].inactive.ticket) {
911 1.1 itojun error = EBUSY;
912 1.1 itojun break;
913 1.1 itojun }
914 1.1 itojun if (pr->pool_ticket != ticket_pabuf) {
915 1.1 itojun error = EBUSY;
916 1.1 itojun break;
917 1.1 itojun }
918 1.1 itojun rule = pool_get(&pf_rule_pl, PR_NOWAIT);
919 1.1 itojun if (rule == NULL) {
920 1.1 itojun error = ENOMEM;
921 1.1 itojun break;
922 1.1 itojun }
923 1.1 itojun bcopy(&pr->rule, rule, sizeof(struct pf_rule));
924 1.1 itojun rule->anchor = NULL;
925 1.1 itojun rule->kif = NULL;
926 1.1 itojun TAILQ_INIT(&rule->rpool.list);
927 1.1 itojun /* initialize refcounting */
928 1.1 itojun rule->states = 0;
929 1.1 itojun rule->src_nodes = 0;
930 1.1 itojun rule->entries.tqe_prev = NULL;
931 1.1 itojun #ifndef INET
932 1.1 itojun if (rule->af == AF_INET) {
933 1.1 itojun pool_put(&pf_rule_pl, rule);
934 1.1 itojun error = EAFNOSUPPORT;
935 1.1 itojun break;
936 1.1 itojun }
937 1.1 itojun #endif /* INET */
938 1.1 itojun #ifndef INET6
939 1.1 itojun if (rule->af == AF_INET6) {
940 1.1 itojun pool_put(&pf_rule_pl, rule);
941 1.1 itojun error = EAFNOSUPPORT;
942 1.1 itojun break;
943 1.1 itojun }
944 1.1 itojun #endif /* INET6 */
945 1.1 itojun tail = TAILQ_LAST(ruleset->rules[rs_num].inactive.ptr,
946 1.1 itojun pf_rulequeue);
947 1.1 itojun if (tail)
948 1.1 itojun rule->nr = tail->nr + 1;
949 1.1 itojun else
950 1.1 itojun rule->nr = 0;
951 1.1 itojun if (rule->ifname[0]) {
952 1.1 itojun rule->kif = pfi_attach_rule(rule->ifname);
953 1.1 itojun if (rule->kif == NULL) {
954 1.1 itojun pool_put(&pf_rule_pl, rule);
955 1.1 itojun error = EINVAL;
956 1.1 itojun break;
957 1.1 itojun }
958 1.1 itojun }
959 1.1 itojun
960 1.1 itojun #ifdef ALTQ
961 1.1 itojun /* set queue IDs */
962 1.1 itojun if (rule->qname[0] != 0) {
963 1.1 itojun if ((rule->qid = pf_qname2qid(rule->qname)) == 0)
964 1.1 itojun error = EBUSY;
965 1.1 itojun else if (rule->pqname[0] != 0) {
966 1.1 itojun if ((rule->pqid =
967 1.1 itojun pf_qname2qid(rule->pqname)) == 0)
968 1.1 itojun error = EBUSY;
969 1.1 itojun } else
970 1.1 itojun rule->pqid = rule->qid;
971 1.1 itojun }
972 1.1 itojun #endif
973 1.1 itojun if (rule->tagname[0])
974 1.1 itojun if ((rule->tag = pf_tagname2tag(rule->tagname)) == 0)
975 1.1 itojun error = EBUSY;
976 1.1 itojun if (rule->match_tagname[0])
977 1.1 itojun if ((rule->match_tag =
978 1.1 itojun pf_tagname2tag(rule->match_tagname)) == 0)
979 1.1 itojun error = EBUSY;
980 1.1 itojun if (rule->rt && !rule->direction)
981 1.1 itojun error = EINVAL;
982 1.1 itojun if (pfi_dynaddr_setup(&rule->src.addr, rule->af))
983 1.1 itojun error = EINVAL;
984 1.1 itojun if (pfi_dynaddr_setup(&rule->dst.addr, rule->af))
985 1.1 itojun error = EINVAL;
986 1.1 itojun if (pf_tbladdr_setup(ruleset, &rule->src.addr))
987 1.1 itojun error = EINVAL;
988 1.1 itojun if (pf_tbladdr_setup(ruleset, &rule->dst.addr))
989 1.1 itojun error = EINVAL;
990 1.1 itojun TAILQ_FOREACH(pa, &pf_pabuf, entries)
991 1.1 itojun if (pf_tbladdr_setup(ruleset, &pa->addr))
992 1.1 itojun error = EINVAL;
993 1.1 itojun
994 1.1 itojun pf_mv_pool(&pf_pabuf, &rule->rpool.list);
995 1.1 itojun if (((((rule->action == PF_NAT) || (rule->action == PF_RDR) ||
996 1.1 itojun (rule->action == PF_BINAT)) && !rule->anchorname[0]) ||
997 1.1 itojun (rule->rt > PF_FASTROUTE)) &&
998 1.1 itojun (TAILQ_FIRST(&rule->rpool.list) == NULL))
999 1.1 itojun error = EINVAL;
1000 1.1 itojun
1001 1.1 itojun if (error) {
1002 1.1 itojun pf_rm_rule(NULL, rule);
1003 1.1 itojun break;
1004 1.1 itojun }
1005 1.1 itojun rule->rpool.cur = TAILQ_FIRST(&rule->rpool.list);
1006 1.1 itojun rule->evaluations = rule->packets = rule->bytes = 0;
1007 1.1 itojun TAILQ_INSERT_TAIL(ruleset->rules[rs_num].inactive.ptr,
1008 1.1 itojun rule, entries);
1009 1.1 itojun break;
1010 1.1 itojun }
1011 1.1 itojun
1012 1.1 itojun case DIOCCOMMITRULES: {
1013 1.1 itojun struct pfioc_rule *pr = (struct pfioc_rule *)addr;
1014 1.1 itojun
1015 1.1 itojun error = pf_commit_rules(pr->ticket, pf_get_ruleset_number(
1016 1.1 itojun pr->rule.action), pr->anchor, pr->ruleset);
1017 1.1 itojun break;
1018 1.1 itojun }
1019 1.1 itojun
1020 1.1 itojun case DIOCGETRULES: {
1021 1.1 itojun struct pfioc_rule *pr = (struct pfioc_rule *)addr;
1022 1.1 itojun struct pf_ruleset *ruleset;
1023 1.1 itojun struct pf_rule *tail;
1024 1.1 itojun int rs_num;
1025 1.1 itojun
1026 1.1 itojun ruleset = pf_find_ruleset(pr->anchor, pr->ruleset);
1027 1.1 itojun if (ruleset == NULL) {
1028 1.1 itojun error = EINVAL;
1029 1.1 itojun break;
1030 1.1 itojun }
1031 1.1 itojun rs_num = pf_get_ruleset_number(pr->rule.action);
1032 1.1 itojun if (rs_num >= PF_RULESET_MAX) {
1033 1.1 itojun error = EINVAL;
1034 1.1 itojun break;
1035 1.1 itojun }
1036 1.1 itojun s = splsoftnet();
1037 1.1 itojun tail = TAILQ_LAST(ruleset->rules[rs_num].active.ptr,
1038 1.1 itojun pf_rulequeue);
1039 1.1 itojun if (tail)
1040 1.1 itojun pr->nr = tail->nr + 1;
1041 1.1 itojun else
1042 1.1 itojun pr->nr = 0;
1043 1.1 itojun pr->ticket = ruleset->rules[rs_num].active.ticket;
1044 1.1 itojun splx(s);
1045 1.1 itojun break;
1046 1.1 itojun }
1047 1.1 itojun
1048 1.1 itojun case DIOCGETRULE: {
1049 1.1 itojun struct pfioc_rule *pr = (struct pfioc_rule *)addr;
1050 1.1 itojun struct pf_ruleset *ruleset;
1051 1.1 itojun struct pf_rule *rule;
1052 1.1 itojun int rs_num, i;
1053 1.1 itojun
1054 1.1 itojun ruleset = pf_find_ruleset(pr->anchor, pr->ruleset);
1055 1.1 itojun if (ruleset == NULL) {
1056 1.1 itojun error = EINVAL;
1057 1.1 itojun break;
1058 1.1 itojun }
1059 1.1 itojun rs_num = pf_get_ruleset_number(pr->rule.action);
1060 1.1 itojun if (rs_num >= PF_RULESET_MAX) {
1061 1.1 itojun error = EINVAL;
1062 1.1 itojun break;
1063 1.1 itojun }
1064 1.1 itojun if (pr->ticket != ruleset->rules[rs_num].active.ticket) {
1065 1.1 itojun error = EBUSY;
1066 1.1 itojun break;
1067 1.1 itojun }
1068 1.1 itojun s = splsoftnet();
1069 1.1 itojun rule = TAILQ_FIRST(ruleset->rules[rs_num].active.ptr);
1070 1.1 itojun while ((rule != NULL) && (rule->nr != pr->nr))
1071 1.1 itojun rule = TAILQ_NEXT(rule, entries);
1072 1.1 itojun if (rule == NULL) {
1073 1.1 itojun error = EBUSY;
1074 1.1 itojun splx(s);
1075 1.1 itojun break;
1076 1.1 itojun }
1077 1.1 itojun bcopy(rule, &pr->rule, sizeof(struct pf_rule));
1078 1.1 itojun pfi_dynaddr_copyout(&pr->rule.src.addr);
1079 1.1 itojun pfi_dynaddr_copyout(&pr->rule.dst.addr);
1080 1.1 itojun pf_tbladdr_copyout(&pr->rule.src.addr);
1081 1.1 itojun pf_tbladdr_copyout(&pr->rule.dst.addr);
1082 1.1 itojun for (i = 0; i < PF_SKIP_COUNT; ++i)
1083 1.1 itojun if (rule->skip[i].ptr == NULL)
1084 1.1 itojun pr->rule.skip[i].nr = -1;
1085 1.1 itojun else
1086 1.1 itojun pr->rule.skip[i].nr =
1087 1.1 itojun rule->skip[i].ptr->nr;
1088 1.1 itojun splx(s);
1089 1.1 itojun break;
1090 1.1 itojun }
1091 1.1 itojun
1092 1.1 itojun case DIOCCHANGERULE: {
1093 1.1 itojun struct pfioc_rule *pcr = (struct pfioc_rule *)addr;
1094 1.1 itojun struct pf_ruleset *ruleset;
1095 1.1 itojun struct pf_rule *oldrule = NULL, *newrule = NULL;
1096 1.1 itojun u_int32_t nr = 0;
1097 1.1 itojun int rs_num;
1098 1.1 itojun
1099 1.1 itojun if (!(pcr->action == PF_CHANGE_REMOVE ||
1100 1.1 itojun pcr->action == PF_CHANGE_GET_TICKET) &&
1101 1.1 itojun pcr->pool_ticket != ticket_pabuf) {
1102 1.1 itojun error = EBUSY;
1103 1.1 itojun break;
1104 1.1 itojun }
1105 1.1 itojun
1106 1.1 itojun if (pcr->action < PF_CHANGE_ADD_HEAD ||
1107 1.1 itojun pcr->action > PF_CHANGE_GET_TICKET) {
1108 1.1 itojun error = EINVAL;
1109 1.1 itojun break;
1110 1.1 itojun }
1111 1.1 itojun ruleset = pf_find_ruleset(pcr->anchor, pcr->ruleset);
1112 1.1 itojun if (ruleset == NULL) {
1113 1.1 itojun error = EINVAL;
1114 1.1 itojun break;
1115 1.1 itojun }
1116 1.1 itojun rs_num = pf_get_ruleset_number(pcr->rule.action);
1117 1.1 itojun if (rs_num >= PF_RULESET_MAX) {
1118 1.1 itojun error = EINVAL;
1119 1.1 itojun break;
1120 1.1 itojun }
1121 1.1 itojun
1122 1.1 itojun if (pcr->action == PF_CHANGE_GET_TICKET) {
1123 1.1 itojun pcr->ticket = ++ruleset->rules[rs_num].active.ticket;
1124 1.1 itojun break;
1125 1.1 itojun } else {
1126 1.1 itojun if (pcr->ticket !=
1127 1.1 itojun ruleset->rules[rs_num].active.ticket) {
1128 1.1 itojun error = EINVAL;
1129 1.1 itojun break;
1130 1.1 itojun }
1131 1.1 itojun if (pcr->rule.return_icmp >> 8 > ICMP_MAXTYPE) {
1132 1.1 itojun error = EINVAL;
1133 1.1 itojun break;
1134 1.1 itojun }
1135 1.1 itojun }
1136 1.1 itojun
1137 1.1 itojun if (pcr->action != PF_CHANGE_REMOVE) {
1138 1.1 itojun newrule = pool_get(&pf_rule_pl, PR_NOWAIT);
1139 1.1 itojun if (newrule == NULL) {
1140 1.1 itojun error = ENOMEM;
1141 1.1 itojun break;
1142 1.1 itojun }
1143 1.1 itojun bcopy(&pcr->rule, newrule, sizeof(struct pf_rule));
1144 1.1 itojun TAILQ_INIT(&newrule->rpool.list);
1145 1.1 itojun /* initialize refcounting */
1146 1.1 itojun newrule->states = 0;
1147 1.1 itojun newrule->entries.tqe_prev = NULL;
1148 1.1 itojun #ifndef INET
1149 1.1 itojun if (newrule->af == AF_INET) {
1150 1.1 itojun pool_put(&pf_rule_pl, newrule);
1151 1.1 itojun error = EAFNOSUPPORT;
1152 1.1 itojun break;
1153 1.1 itojun }
1154 1.1 itojun #endif /* INET */
1155 1.1 itojun #ifndef INET6
1156 1.1 itojun if (newrule->af == AF_INET6) {
1157 1.1 itojun pool_put(&pf_rule_pl, newrule);
1158 1.1 itojun error = EAFNOSUPPORT;
1159 1.1 itojun break;
1160 1.1 itojun }
1161 1.1 itojun #endif /* INET6 */
1162 1.1 itojun if (newrule->ifname[0]) {
1163 1.1 itojun newrule->kif = pfi_attach_rule(newrule->ifname);
1164 1.1 itojun if (newrule->kif == NULL) {
1165 1.1 itojun pool_put(&pf_rule_pl, newrule);
1166 1.1 itojun error = EINVAL;
1167 1.1 itojun break;
1168 1.1 itojun }
1169 1.1 itojun } else
1170 1.1 itojun newrule->kif = NULL;
1171 1.1 itojun
1172 1.1 itojun #ifdef ALTQ
1173 1.1 itojun /* set queue IDs */
1174 1.1 itojun if (newrule->qname[0] != 0) {
1175 1.1 itojun if ((newrule->qid =
1176 1.1 itojun pf_qname2qid(newrule->qname)) == 0)
1177 1.1 itojun error = EBUSY;
1178 1.1 itojun else if (newrule->pqname[0] != 0) {
1179 1.1 itojun if ((newrule->pqid =
1180 1.1 itojun pf_qname2qid(newrule->pqname)) == 0)
1181 1.1 itojun error = EBUSY;
1182 1.1 itojun } else
1183 1.1 itojun newrule->pqid = newrule->qid;
1184 1.1 itojun }
1185 1.1 itojun #endif
1186 1.1 itojun if (newrule->tagname[0])
1187 1.1 itojun if ((newrule->tag =
1188 1.1 itojun pf_tagname2tag(newrule->tagname)) == 0)
1189 1.1 itojun error = EBUSY;
1190 1.1 itojun if (newrule->match_tagname[0])
1191 1.1 itojun if ((newrule->match_tag = pf_tagname2tag(
1192 1.1 itojun newrule->match_tagname)) == 0)
1193 1.1 itojun error = EBUSY;
1194 1.1 itojun
1195 1.1 itojun if (newrule->rt && !newrule->direction)
1196 1.1 itojun error = EINVAL;
1197 1.1 itojun if (pfi_dynaddr_setup(&newrule->src.addr, newrule->af))
1198 1.1 itojun error = EINVAL;
1199 1.1 itojun if (pfi_dynaddr_setup(&newrule->dst.addr, newrule->af))
1200 1.1 itojun error = EINVAL;
1201 1.1 itojun if (pf_tbladdr_setup(ruleset, &newrule->src.addr))
1202 1.1 itojun error = EINVAL;
1203 1.1 itojun if (pf_tbladdr_setup(ruleset, &newrule->dst.addr))
1204 1.1 itojun error = EINVAL;
1205 1.1 itojun
1206 1.1 itojun pf_mv_pool(&pf_pabuf, &newrule->rpool.list);
1207 1.1 itojun if (((((newrule->action == PF_NAT) ||
1208 1.1 itojun (newrule->action == PF_RDR) ||
1209 1.1 itojun (newrule->action == PF_BINAT) ||
1210 1.1 itojun (newrule->rt > PF_FASTROUTE)) &&
1211 1.1 itojun !newrule->anchorname[0])) &&
1212 1.1 itojun (TAILQ_FIRST(&newrule->rpool.list) == NULL))
1213 1.1 itojun error = EINVAL;
1214 1.1 itojun
1215 1.1 itojun if (error) {
1216 1.1 itojun pf_rm_rule(NULL, newrule);
1217 1.1 itojun break;
1218 1.1 itojun }
1219 1.1 itojun newrule->rpool.cur = TAILQ_FIRST(&newrule->rpool.list);
1220 1.1 itojun newrule->evaluations = newrule->packets = 0;
1221 1.1 itojun newrule->bytes = 0;
1222 1.1 itojun }
1223 1.1 itojun pf_empty_pool(&pf_pabuf);
1224 1.1 itojun
1225 1.1 itojun s = splsoftnet();
1226 1.1 itojun
1227 1.1 itojun if (pcr->action == PF_CHANGE_ADD_HEAD)
1228 1.1 itojun oldrule = TAILQ_FIRST(
1229 1.1 itojun ruleset->rules[rs_num].active.ptr);
1230 1.1 itojun else if (pcr->action == PF_CHANGE_ADD_TAIL)
1231 1.1 itojun oldrule = TAILQ_LAST(
1232 1.1 itojun ruleset->rules[rs_num].active.ptr, pf_rulequeue);
1233 1.1 itojun else {
1234 1.1 itojun oldrule = TAILQ_FIRST(
1235 1.1 itojun ruleset->rules[rs_num].active.ptr);
1236 1.1 itojun while ((oldrule != NULL) && (oldrule->nr != pcr->nr))
1237 1.1 itojun oldrule = TAILQ_NEXT(oldrule, entries);
1238 1.1 itojun if (oldrule == NULL) {
1239 1.1 itojun pf_rm_rule(NULL, newrule);
1240 1.1 itojun error = EINVAL;
1241 1.1 itojun splx(s);
1242 1.1 itojun break;
1243 1.1 itojun }
1244 1.1 itojun }
1245 1.1 itojun
1246 1.1 itojun if (pcr->action == PF_CHANGE_REMOVE)
1247 1.1 itojun pf_rm_rule(ruleset->rules[rs_num].active.ptr, oldrule);
1248 1.1 itojun else {
1249 1.1 itojun if (oldrule == NULL)
1250 1.1 itojun TAILQ_INSERT_TAIL(
1251 1.1 itojun ruleset->rules[rs_num].active.ptr,
1252 1.1 itojun newrule, entries);
1253 1.1 itojun else if (pcr->action == PF_CHANGE_ADD_HEAD ||
1254 1.1 itojun pcr->action == PF_CHANGE_ADD_BEFORE)
1255 1.1 itojun TAILQ_INSERT_BEFORE(oldrule, newrule, entries);
1256 1.1 itojun else
1257 1.1 itojun TAILQ_INSERT_AFTER(
1258 1.1 itojun ruleset->rules[rs_num].active.ptr,
1259 1.1 itojun oldrule, newrule, entries);
1260 1.1 itojun }
1261 1.1 itojun
1262 1.1 itojun nr = 0;
1263 1.1 itojun TAILQ_FOREACH(oldrule,
1264 1.1 itojun ruleset->rules[rs_num].active.ptr, entries)
1265 1.1 itojun oldrule->nr = nr++;
1266 1.1 itojun
1267 1.1 itojun pf_calc_skip_steps(ruleset->rules[rs_num].active.ptr);
1268 1.1 itojun pf_remove_if_empty_ruleset(ruleset);
1269 1.1 itojun pf_update_anchor_rules();
1270 1.1 itojun
1271 1.1 itojun ruleset->rules[rs_num].active.ticket++;
1272 1.1 itojun splx(s);
1273 1.1 itojun break;
1274 1.1 itojun }
1275 1.1 itojun
1276 1.1 itojun case DIOCCLRSTATES: {
1277 1.1 itojun struct pf_state *state;
1278 1.1 itojun struct pfioc_state_kill *psk = (struct pfioc_state_kill *)addr;
1279 1.1 itojun int killed = 0;
1280 1.1 itojun
1281 1.1 itojun s = splsoftnet();
1282 1.1 itojun RB_FOREACH(state, pf_state_tree_id, &tree_id) {
1283 1.1 itojun if (!psk->psk_ifname[0] || !strcmp(psk->psk_ifname,
1284 1.1 itojun state->u.s.kif->pfik_name)) {
1285 1.1 itojun state->timeout = PFTM_PURGE;
1286 1.1 itojun #if NPFSYNC
1287 1.1 itojun /* don't send out individual delete messages */
1288 1.1 itojun state->sync_flags = PFSTATE_NOSYNC;
1289 1.1 itojun #endif
1290 1.1 itojun killed++;
1291 1.1 itojun }
1292 1.1 itojun }
1293 1.1 itojun pf_purge_expired_states();
1294 1.1 itojun pf_status.states = 0;
1295 1.1 itojun psk->psk_af = killed;
1296 1.1 itojun #if NPFSYNC
1297 1.1 itojun pfsync_clear_states(pf_status.hostid, psk->psk_ifname);
1298 1.1 itojun #endif
1299 1.1 itojun splx(s);
1300 1.1 itojun break;
1301 1.1 itojun }
1302 1.1 itojun
1303 1.1 itojun case DIOCKILLSTATES: {
1304 1.1 itojun struct pf_state *state;
1305 1.1 itojun struct pfioc_state_kill *psk = (struct pfioc_state_kill *)addr;
1306 1.1 itojun int killed = 0;
1307 1.1 itojun
1308 1.1 itojun s = splsoftnet();
1309 1.1 itojun RB_FOREACH(state, pf_state_tree_id, &tree_id) {
1310 1.1 itojun if ((!psk->psk_af || state->af == psk->psk_af)
1311 1.1 itojun && (!psk->psk_proto || psk->psk_proto ==
1312 1.1 itojun state->proto) &&
1313 1.1 itojun PF_MATCHA(psk->psk_src.not,
1314 1.1 itojun &psk->psk_src.addr.v.a.addr,
1315 1.1 itojun &psk->psk_src.addr.v.a.mask,
1316 1.1 itojun &state->lan.addr, state->af) &&
1317 1.1 itojun PF_MATCHA(psk->psk_dst.not,
1318 1.1 itojun &psk->psk_dst.addr.v.a.addr,
1319 1.1 itojun &psk->psk_dst.addr.v.a.mask,
1320 1.1 itojun &state->ext.addr, state->af) &&
1321 1.1 itojun (psk->psk_src.port_op == 0 ||
1322 1.1 itojun pf_match_port(psk->psk_src.port_op,
1323 1.1 itojun psk->psk_src.port[0], psk->psk_src.port[1],
1324 1.1 itojun state->lan.port)) &&
1325 1.1 itojun (psk->psk_dst.port_op == 0 ||
1326 1.1 itojun pf_match_port(psk->psk_dst.port_op,
1327 1.1 itojun psk->psk_dst.port[0], psk->psk_dst.port[1],
1328 1.1 itojun state->ext.port)) &&
1329 1.1 itojun (!psk->psk_ifname[0] || !strcmp(psk->psk_ifname,
1330 1.1 itojun state->u.s.kif->pfik_name))) {
1331 1.1 itojun state->timeout = PFTM_PURGE;
1332 1.1 itojun killed++;
1333 1.1 itojun }
1334 1.1 itojun }
1335 1.1 itojun pf_purge_expired_states();
1336 1.1 itojun splx(s);
1337 1.1 itojun psk->psk_af = killed;
1338 1.1 itojun break;
1339 1.1 itojun }
1340 1.1 itojun
1341 1.1 itojun case DIOCADDSTATE: {
1342 1.1 itojun struct pfioc_state *ps = (struct pfioc_state *)addr;
1343 1.1 itojun struct pf_state *state;
1344 1.1 itojun struct pfi_kif *kif;
1345 1.1 itojun
1346 1.1 itojun if (ps->state.timeout >= PFTM_MAX &&
1347 1.1 itojun ps->state.timeout != PFTM_UNTIL_PACKET) {
1348 1.1 itojun error = EINVAL;
1349 1.1 itojun break;
1350 1.1 itojun }
1351 1.1 itojun state = pool_get(&pf_state_pl, PR_NOWAIT);
1352 1.1 itojun if (state == NULL) {
1353 1.1 itojun error = ENOMEM;
1354 1.1 itojun break;
1355 1.1 itojun }
1356 1.1 itojun s = splsoftnet();
1357 1.1 itojun kif = pfi_lookup_create(ps->state.u.ifname);
1358 1.1 itojun if (kif == NULL) {
1359 1.1 itojun pool_put(&pf_state_pl, state);
1360 1.1 itojun error = ENOENT;
1361 1.1 itojun splx(s);
1362 1.1 itojun break;
1363 1.1 itojun }
1364 1.1 itojun bcopy(&ps->state, state, sizeof(struct pf_state));
1365 1.1 itojun bzero(&state->u, sizeof(state->u));
1366 1.1 itojun state->rule.ptr = &pf_default_rule;
1367 1.1 itojun state->nat_rule.ptr = NULL;
1368 1.1 itojun state->anchor.ptr = NULL;
1369 1.1 itojun state->rt_kif = NULL;
1370 1.1 itojun state->creation = time.tv_sec;
1371 1.1 itojun state->pfsync_time = 0;
1372 1.1 itojun state->packets[0] = state->packets[1] = 0;
1373 1.1 itojun state->bytes[0] = state->bytes[1] = 0;
1374 1.1 itojun
1375 1.1 itojun if (pf_insert_state(kif, state)) {
1376 1.1 itojun pfi_maybe_destroy(kif);
1377 1.1 itojun pool_put(&pf_state_pl, state);
1378 1.1 itojun error = ENOMEM;
1379 1.1 itojun }
1380 1.1 itojun splx(s);
1381 1.1 itojun break;
1382 1.1 itojun }
1383 1.1 itojun
1384 1.1 itojun case DIOCGETSTATE: {
1385 1.1 itojun struct pfioc_state *ps = (struct pfioc_state *)addr;
1386 1.1 itojun struct pf_state *state;
1387 1.1 itojun u_int32_t nr;
1388 1.1 itojun
1389 1.1 itojun nr = 0;
1390 1.1 itojun s = splsoftnet();
1391 1.1 itojun RB_FOREACH(state, pf_state_tree_id, &tree_id) {
1392 1.1 itojun if (nr >= ps->nr)
1393 1.1 itojun break;
1394 1.1 itojun nr++;
1395 1.1 itojun }
1396 1.1 itojun if (state == NULL) {
1397 1.1 itojun error = EBUSY;
1398 1.1 itojun splx(s);
1399 1.1 itojun break;
1400 1.1 itojun }
1401 1.1 itojun bcopy(state, &ps->state, sizeof(struct pf_state));
1402 1.1 itojun ps->state.rule.nr = state->rule.ptr->nr;
1403 1.1 itojun ps->state.nat_rule.nr = (state->nat_rule.ptr == NULL) ?
1404 1.1 itojun -1 : state->nat_rule.ptr->nr;
1405 1.1 itojun ps->state.anchor.nr = (state->anchor.ptr == NULL) ?
1406 1.1 itojun -1 : state->anchor.ptr->nr;
1407 1.1 itojun splx(s);
1408 1.1 itojun ps->state.expire = pf_state_expires(state);
1409 1.1 itojun if (ps->state.expire > time.tv_sec)
1410 1.1 itojun ps->state.expire -= time.tv_sec;
1411 1.1 itojun else
1412 1.1 itojun ps->state.expire = 0;
1413 1.1 itojun break;
1414 1.1 itojun }
1415 1.1 itojun
1416 1.1 itojun case DIOCGETSTATES: {
1417 1.1 itojun struct pfioc_states *ps = (struct pfioc_states *)addr;
1418 1.1 itojun struct pf_state *state;
1419 1.1 itojun struct pf_state *p, pstore;
1420 1.1 itojun struct pfi_kif *kif;
1421 1.1 itojun u_int32_t nr = 0;
1422 1.1 itojun int space = ps->ps_len;
1423 1.1 itojun
1424 1.1 itojun if (space == 0) {
1425 1.1 itojun s = splsoftnet();
1426 1.1 itojun TAILQ_FOREACH(kif, &pfi_statehead, pfik_w_states)
1427 1.1 itojun nr += kif->pfik_states;
1428 1.1 itojun splx(s);
1429 1.1 itojun ps->ps_len = sizeof(struct pf_state) * nr;
1430 1.1 itojun return (0);
1431 1.1 itojun }
1432 1.1 itojun
1433 1.1 itojun s = splsoftnet();
1434 1.1 itojun p = ps->ps_states;
1435 1.1 itojun TAILQ_FOREACH(kif, &pfi_statehead, pfik_w_states)
1436 1.1 itojun RB_FOREACH(state, pf_state_tree_ext_gwy,
1437 1.1 itojun &kif->pfik_ext_gwy) {
1438 1.1 itojun int secs = time.tv_sec;
1439 1.1 itojun
1440 1.1 itojun if ((nr+1) * sizeof(*p) > (unsigned)ps->ps_len)
1441 1.1 itojun break;
1442 1.1 itojun
1443 1.1 itojun bcopy(state, &pstore, sizeof(pstore));
1444 1.1 itojun strlcpy(pstore.u.ifname, kif->pfik_name,
1445 1.1 itojun sizeof(pstore.u.ifname));
1446 1.1 itojun pstore.rule.nr = state->rule.ptr->nr;
1447 1.1 itojun pstore.nat_rule.nr = (state->nat_rule.ptr ==
1448 1.1 itojun NULL) ? -1 : state->nat_rule.ptr->nr;
1449 1.1 itojun pstore.anchor.nr = (state->anchor.ptr ==
1450 1.1 itojun NULL) ? -1 : state->anchor.ptr->nr;
1451 1.1 itojun pstore.creation = secs - pstore.creation;
1452 1.1 itojun pstore.expire = pf_state_expires(state);
1453 1.1 itojun if (pstore.expire > secs)
1454 1.1 itojun pstore.expire -= secs;
1455 1.1 itojun else
1456 1.1 itojun pstore.expire = 0;
1457 1.1 itojun error = copyout(&pstore, p, sizeof(*p));
1458 1.1 itojun if (error) {
1459 1.1 itojun splx(s);
1460 1.1 itojun goto fail;
1461 1.1 itojun }
1462 1.1 itojun p++;
1463 1.1 itojun nr++;
1464 1.1 itojun }
1465 1.1 itojun ps->ps_len = sizeof(struct pf_state) * nr;
1466 1.1 itojun splx(s);
1467 1.1 itojun break;
1468 1.1 itojun }
1469 1.1 itojun
1470 1.1 itojun case DIOCGETSTATUS: {
1471 1.1 itojun struct pf_status *s = (struct pf_status *)addr;
1472 1.1 itojun bcopy(&pf_status, s, sizeof(struct pf_status));
1473 1.1 itojun pfi_fill_oldstatus(s);
1474 1.1 itojun break;
1475 1.1 itojun }
1476 1.1 itojun
1477 1.1 itojun case DIOCSETSTATUSIF: {
1478 1.1 itojun struct pfioc_if *pi = (struct pfioc_if *)addr;
1479 1.1 itojun
1480 1.1 itojun if (pi->ifname[0] == 0) {
1481 1.1 itojun bzero(pf_status.ifname, IFNAMSIZ);
1482 1.1 itojun break;
1483 1.1 itojun }
1484 1.1 itojun if (ifunit(pi->ifname) == NULL) {
1485 1.1 itojun error = EINVAL;
1486 1.1 itojun break;
1487 1.1 itojun }
1488 1.1 itojun strlcpy(pf_status.ifname, pi->ifname, IFNAMSIZ);
1489 1.1 itojun break;
1490 1.1 itojun }
1491 1.1 itojun
1492 1.1 itojun case DIOCCLRSTATUS: {
1493 1.1 itojun bzero(pf_status.counters, sizeof(pf_status.counters));
1494 1.1 itojun bzero(pf_status.fcounters, sizeof(pf_status.fcounters));
1495 1.1 itojun bzero(pf_status.scounters, sizeof(pf_status.scounters));
1496 1.1 itojun if (*pf_status.ifname)
1497 1.1 itojun pfi_clr_istats(pf_status.ifname, NULL,
1498 1.1 itojun PFI_FLAG_INSTANCE);
1499 1.1 itojun break;
1500 1.1 itojun }
1501 1.1 itojun
1502 1.1 itojun case DIOCNATLOOK: {
1503 1.1 itojun struct pfioc_natlook *pnl = (struct pfioc_natlook *)addr;
1504 1.1 itojun struct pf_state *state;
1505 1.1 itojun struct pf_state key;
1506 1.1 itojun int m = 0, direction = pnl->direction;
1507 1.1 itojun
1508 1.1 itojun key.af = pnl->af;
1509 1.1 itojun key.proto = pnl->proto;
1510 1.1 itojun
1511 1.1 itojun if (!pnl->proto ||
1512 1.1 itojun PF_AZERO(&pnl->saddr, pnl->af) ||
1513 1.1 itojun PF_AZERO(&pnl->daddr, pnl->af) ||
1514 1.1 itojun !pnl->dport || !pnl->sport)
1515 1.1 itojun error = EINVAL;
1516 1.1 itojun else {
1517 1.1 itojun s = splsoftnet();
1518 1.1 itojun
1519 1.1 itojun /*
1520 1.1 itojun * userland gives us source and dest of connection,
1521 1.1 itojun * reverse the lookup so we ask for what happens with
1522 1.1 itojun * the return traffic, enabling us to find it in the
1523 1.1 itojun * state tree.
1524 1.1 itojun */
1525 1.1 itojun if (direction == PF_IN) {
1526 1.1 itojun PF_ACPY(&key.ext.addr, &pnl->daddr, pnl->af);
1527 1.1 itojun key.ext.port = pnl->dport;
1528 1.1 itojun PF_ACPY(&key.gwy.addr, &pnl->saddr, pnl->af);
1529 1.1 itojun key.gwy.port = pnl->sport;
1530 1.1 itojun state = pf_find_state_all(&key, PF_EXT_GWY, &m);
1531 1.1 itojun } else {
1532 1.1 itojun PF_ACPY(&key.lan.addr, &pnl->daddr, pnl->af);
1533 1.1 itojun key.lan.port = pnl->dport;
1534 1.1 itojun PF_ACPY(&key.ext.addr, &pnl->saddr, pnl->af);
1535 1.1 itojun key.ext.port = pnl->sport;
1536 1.1 itojun state = pf_find_state_all(&key, PF_LAN_EXT, &m);
1537 1.1 itojun }
1538 1.1 itojun if (m > 1)
1539 1.1 itojun error = E2BIG; /* more than one state */
1540 1.1 itojun else if (state != NULL) {
1541 1.1 itojun if (direction == PF_IN) {
1542 1.1 itojun PF_ACPY(&pnl->rsaddr, &state->lan.addr,
1543 1.1 itojun state->af);
1544 1.1 itojun pnl->rsport = state->lan.port;
1545 1.1 itojun PF_ACPY(&pnl->rdaddr, &pnl->daddr,
1546 1.1 itojun pnl->af);
1547 1.1 itojun pnl->rdport = pnl->dport;
1548 1.1 itojun } else {
1549 1.1 itojun PF_ACPY(&pnl->rdaddr, &state->gwy.addr,
1550 1.1 itojun state->af);
1551 1.1 itojun pnl->rdport = state->gwy.port;
1552 1.1 itojun PF_ACPY(&pnl->rsaddr, &pnl->saddr,
1553 1.1 itojun pnl->af);
1554 1.1 itojun pnl->rsport = pnl->sport;
1555 1.1 itojun }
1556 1.1 itojun } else
1557 1.1 itojun error = ENOENT;
1558 1.1 itojun splx(s);
1559 1.1 itojun }
1560 1.1 itojun break;
1561 1.1 itojun }
1562 1.1 itojun
1563 1.1 itojun case DIOCSETTIMEOUT: {
1564 1.1 itojun struct pfioc_tm *pt = (struct pfioc_tm *)addr;
1565 1.1 itojun int old;
1566 1.1 itojun
1567 1.1 itojun if (pt->timeout < 0 || pt->timeout >= PFTM_MAX ||
1568 1.1 itojun pt->seconds < 0) {
1569 1.1 itojun error = EINVAL;
1570 1.1 itojun goto fail;
1571 1.1 itojun }
1572 1.1 itojun old = pf_default_rule.timeout[pt->timeout];
1573 1.1 itojun pf_default_rule.timeout[pt->timeout] = pt->seconds;
1574 1.1 itojun pt->seconds = old;
1575 1.1 itojun break;
1576 1.1 itojun }
1577 1.1 itojun
1578 1.1 itojun case DIOCGETTIMEOUT: {
1579 1.1 itojun struct pfioc_tm *pt = (struct pfioc_tm *)addr;
1580 1.1 itojun
1581 1.1 itojun if (pt->timeout < 0 || pt->timeout >= PFTM_MAX) {
1582 1.1 itojun error = EINVAL;
1583 1.1 itojun goto fail;
1584 1.1 itojun }
1585 1.1 itojun pt->seconds = pf_default_rule.timeout[pt->timeout];
1586 1.1 itojun break;
1587 1.1 itojun }
1588 1.1 itojun
1589 1.1 itojun case DIOCGETLIMIT: {
1590 1.1 itojun struct pfioc_limit *pl = (struct pfioc_limit *)addr;
1591 1.1 itojun
1592 1.1 itojun if (pl->index < 0 || pl->index >= PF_LIMIT_MAX) {
1593 1.1 itojun error = EINVAL;
1594 1.1 itojun goto fail;
1595 1.1 itojun }
1596 1.1 itojun pl->limit = pf_pool_limits[pl->index].limit;
1597 1.1 itojun break;
1598 1.1 itojun }
1599 1.1 itojun
1600 1.1 itojun case DIOCSETLIMIT: {
1601 1.1 itojun struct pfioc_limit *pl = (struct pfioc_limit *)addr;
1602 1.1 itojun int old_limit;
1603 1.1 itojun
1604 1.1 itojun if (pl->index < 0 || pl->index >= PF_LIMIT_MAX ||
1605 1.1 itojun pf_pool_limits[pl->index].pp == NULL) {
1606 1.1 itojun error = EINVAL;
1607 1.1 itojun goto fail;
1608 1.1 itojun }
1609 1.1 itojun if (pool_sethardlimit(pf_pool_limits[pl->index].pp,
1610 1.1 itojun pl->limit, NULL, 0) != 0) {
1611 1.1 itojun error = EBUSY;
1612 1.1 itojun goto fail;
1613 1.1 itojun }
1614 1.1 itojun old_limit = pf_pool_limits[pl->index].limit;
1615 1.1 itojun pf_pool_limits[pl->index].limit = pl->limit;
1616 1.1 itojun pl->limit = old_limit;
1617 1.1 itojun break;
1618 1.1 itojun }
1619 1.1 itojun
1620 1.1 itojun case DIOCSETDEBUG: {
1621 1.1 itojun u_int32_t *level = (u_int32_t *)addr;
1622 1.1 itojun
1623 1.1 itojun pf_status.debug = *level;
1624 1.1 itojun break;
1625 1.1 itojun }
1626 1.1 itojun
1627 1.1 itojun case DIOCCLRRULECTRS: {
1628 1.1 itojun struct pf_ruleset *ruleset = &pf_main_ruleset;
1629 1.1 itojun struct pf_rule *rule;
1630 1.1 itojun
1631 1.1 itojun s = splsoftnet();
1632 1.1 itojun TAILQ_FOREACH(rule,
1633 1.1 itojun ruleset->rules[PF_RULESET_FILTER].active.ptr, entries)
1634 1.1 itojun rule->evaluations = rule->packets =
1635 1.1 itojun rule->bytes = 0;
1636 1.1 itojun splx(s);
1637 1.1 itojun break;
1638 1.1 itojun }
1639 1.1 itojun
1640 1.1 itojun #ifdef ALTQ
1641 1.1 itojun case DIOCSTARTALTQ: {
1642 1.1 itojun struct pf_altq *altq;
1643 1.1 itojun struct ifnet *ifp;
1644 1.1 itojun struct tb_profile tb;
1645 1.1 itojun
1646 1.1 itojun /* enable all altq interfaces on active list */
1647 1.1 itojun s = splsoftnet();
1648 1.1 itojun TAILQ_FOREACH(altq, pf_altqs_active, entries) {
1649 1.1 itojun if (altq->qname[0] == 0) {
1650 1.1 itojun if ((ifp = ifunit(altq->ifname)) == NULL) {
1651 1.1 itojun error = EINVAL;
1652 1.1 itojun break;
1653 1.1 itojun }
1654 1.1 itojun if (ifp->if_snd.altq_type != ALTQT_NONE)
1655 1.1 itojun error = altq_enable(&ifp->if_snd);
1656 1.1 itojun if (error != 0)
1657 1.1 itojun break;
1658 1.1 itojun /* set tokenbucket regulator */
1659 1.1 itojun tb.rate = altq->ifbandwidth;
1660 1.1 itojun tb.depth = altq->tbrsize;
1661 1.1 itojun error = tbr_set(&ifp->if_snd, &tb);
1662 1.1 itojun if (error != 0)
1663 1.1 itojun break;
1664 1.1 itojun }
1665 1.1 itojun }
1666 1.1 itojun if (error == 0)
1667 1.1 itojun pfaltq_running = 1;
1668 1.1 itojun splx(s);
1669 1.1 itojun DPFPRINTF(PF_DEBUG_MISC, ("altq: started\n"));
1670 1.1 itojun break;
1671 1.1 itojun }
1672 1.1 itojun
1673 1.1 itojun case DIOCSTOPALTQ: {
1674 1.1 itojun struct pf_altq *altq;
1675 1.1 itojun struct ifnet *ifp;
1676 1.1 itojun struct tb_profile tb;
1677 1.1 itojun int err;
1678 1.1 itojun
1679 1.1 itojun /* disable all altq interfaces on active list */
1680 1.1 itojun s = splsoftnet();
1681 1.1 itojun TAILQ_FOREACH(altq, pf_altqs_active, entries) {
1682 1.1 itojun if (altq->qname[0] == 0) {
1683 1.1 itojun if ((ifp = ifunit(altq->ifname)) == NULL) {
1684 1.1 itojun error = EINVAL;
1685 1.1 itojun break;
1686 1.1 itojun }
1687 1.1 itojun if (ifp->if_snd.altq_type != ALTQT_NONE) {
1688 1.1 itojun err = altq_disable(&ifp->if_snd);
1689 1.1 itojun if (err != 0 && error == 0)
1690 1.1 itojun error = err;
1691 1.1 itojun }
1692 1.1 itojun /* clear tokenbucket regulator */
1693 1.1 itojun tb.rate = 0;
1694 1.1 itojun err = tbr_set(&ifp->if_snd, &tb);
1695 1.1 itojun if (err != 0 && error == 0)
1696 1.1 itojun error = err;
1697 1.1 itojun }
1698 1.1 itojun }
1699 1.1 itojun if (error == 0)
1700 1.1 itojun pfaltq_running = 0;
1701 1.1 itojun splx(s);
1702 1.1 itojun DPFPRINTF(PF_DEBUG_MISC, ("altq: stopped\n"));
1703 1.1 itojun break;
1704 1.1 itojun }
1705 1.1 itojun
1706 1.1 itojun case DIOCBEGINALTQS: {
1707 1.1 itojun u_int32_t *ticket = (u_int32_t *)addr;
1708 1.1 itojun
1709 1.1 itojun error = pf_begin_altq(ticket);
1710 1.1 itojun break;
1711 1.1 itojun }
1712 1.1 itojun
1713 1.1 itojun case DIOCADDALTQ: {
1714 1.1 itojun struct pfioc_altq *pa = (struct pfioc_altq *)addr;
1715 1.1 itojun struct pf_altq *altq, *a;
1716 1.1 itojun
1717 1.1 itojun if (pa->ticket != ticket_altqs_inactive) {
1718 1.1 itojun error = EBUSY;
1719 1.1 itojun break;
1720 1.1 itojun }
1721 1.1 itojun altq = pool_get(&pf_altq_pl, PR_NOWAIT);
1722 1.1 itojun if (altq == NULL) {
1723 1.1 itojun error = ENOMEM;
1724 1.1 itojun break;
1725 1.1 itojun }
1726 1.1 itojun bcopy(&pa->altq, altq, sizeof(struct pf_altq));
1727 1.1 itojun
1728 1.1 itojun /*
1729 1.1 itojun * if this is for a queue, find the discipline and
1730 1.1 itojun * copy the necessary fields
1731 1.1 itojun */
1732 1.1 itojun if (altq->qname[0] != 0) {
1733 1.1 itojun if ((altq->qid = pf_qname2qid(altq->qname)) == 0) {
1734 1.1 itojun error = EBUSY;
1735 1.1 itojun pool_put(&pf_altq_pl, altq);
1736 1.1 itojun break;
1737 1.1 itojun }
1738 1.1 itojun TAILQ_FOREACH(a, pf_altqs_inactive, entries) {
1739 1.1 itojun if (strncmp(a->ifname, altq->ifname,
1740 1.1 itojun IFNAMSIZ) == 0 && a->qname[0] == 0) {
1741 1.1 itojun altq->altq_disc = a->altq_disc;
1742 1.1 itojun break;
1743 1.1 itojun }
1744 1.1 itojun }
1745 1.1 itojun }
1746 1.1 itojun
1747 1.1 itojun error = altq_add(altq);
1748 1.1 itojun if (error) {
1749 1.1 itojun pool_put(&pf_altq_pl, altq);
1750 1.1 itojun break;
1751 1.1 itojun }
1752 1.1 itojun
1753 1.1 itojun TAILQ_INSERT_TAIL(pf_altqs_inactive, altq, entries);
1754 1.1 itojun bcopy(altq, &pa->altq, sizeof(struct pf_altq));
1755 1.1 itojun break;
1756 1.1 itojun }
1757 1.1 itojun
1758 1.1 itojun case DIOCCOMMITALTQS: {
1759 1.1 itojun u_int32_t ticket = *(u_int32_t *)addr;
1760 1.1 itojun
1761 1.1 itojun error = pf_commit_altq(ticket);
1762 1.1 itojun break;
1763 1.1 itojun }
1764 1.1 itojun
1765 1.1 itojun case DIOCGETALTQS: {
1766 1.1 itojun struct pfioc_altq *pa = (struct pfioc_altq *)addr;
1767 1.1 itojun struct pf_altq *altq;
1768 1.1 itojun
1769 1.1 itojun pa->nr = 0;
1770 1.1 itojun s = splsoftnet();
1771 1.1 itojun TAILQ_FOREACH(altq, pf_altqs_active, entries)
1772 1.1 itojun pa->nr++;
1773 1.1 itojun pa->ticket = ticket_altqs_active;
1774 1.1 itojun splx(s);
1775 1.1 itojun break;
1776 1.1 itojun }
1777 1.1 itojun
1778 1.1 itojun case DIOCGETALTQ: {
1779 1.1 itojun struct pfioc_altq *pa = (struct pfioc_altq *)addr;
1780 1.1 itojun struct pf_altq *altq;
1781 1.1 itojun u_int32_t nr;
1782 1.1 itojun
1783 1.1 itojun if (pa->ticket != ticket_altqs_active) {
1784 1.1 itojun error = EBUSY;
1785 1.1 itojun break;
1786 1.1 itojun }
1787 1.1 itojun nr = 0;
1788 1.1 itojun s = splsoftnet();
1789 1.1 itojun altq = TAILQ_FIRST(pf_altqs_active);
1790 1.1 itojun while ((altq != NULL) && (nr < pa->nr)) {
1791 1.1 itojun altq = TAILQ_NEXT(altq, entries);
1792 1.1 itojun nr++;
1793 1.1 itojun }
1794 1.1 itojun if (altq == NULL) {
1795 1.1 itojun error = EBUSY;
1796 1.1 itojun splx(s);
1797 1.1 itojun break;
1798 1.1 itojun }
1799 1.1 itojun bcopy(altq, &pa->altq, sizeof(struct pf_altq));
1800 1.1 itojun splx(s);
1801 1.1 itojun break;
1802 1.1 itojun }
1803 1.1 itojun
1804 1.1 itojun case DIOCCHANGEALTQ:
1805 1.1 itojun /* CHANGEALTQ not supported yet! */
1806 1.1 itojun error = ENODEV;
1807 1.1 itojun break;
1808 1.1 itojun
1809 1.1 itojun case DIOCGETQSTATS: {
1810 1.1 itojun struct pfioc_qstats *pq = (struct pfioc_qstats *)addr;
1811 1.1 itojun struct pf_altq *altq;
1812 1.1 itojun u_int32_t nr;
1813 1.1 itojun int nbytes;
1814 1.1 itojun
1815 1.1 itojun if (pq->ticket != ticket_altqs_active) {
1816 1.1 itojun error = EBUSY;
1817 1.1 itojun break;
1818 1.1 itojun }
1819 1.1 itojun nbytes = pq->nbytes;
1820 1.1 itojun nr = 0;
1821 1.1 itojun s = splsoftnet();
1822 1.1 itojun altq = TAILQ_FIRST(pf_altqs_active);
1823 1.1 itojun while ((altq != NULL) && (nr < pq->nr)) {
1824 1.1 itojun altq = TAILQ_NEXT(altq, entries);
1825 1.1 itojun nr++;
1826 1.1 itojun }
1827 1.1 itojun if (altq == NULL) {
1828 1.1 itojun error = EBUSY;
1829 1.1 itojun splx(s);
1830 1.1 itojun break;
1831 1.1 itojun }
1832 1.1 itojun error = altq_getqstats(altq, pq->buf, &nbytes);
1833 1.1 itojun splx(s);
1834 1.1 itojun if (error == 0) {
1835 1.1 itojun pq->scheduler = altq->scheduler;
1836 1.1 itojun pq->nbytes = nbytes;
1837 1.1 itojun }
1838 1.1 itojun break;
1839 1.1 itojun }
1840 1.1 itojun #endif /* ALTQ */
1841 1.1 itojun
1842 1.1 itojun case DIOCBEGINADDRS: {
1843 1.1 itojun struct pfioc_pooladdr *pp = (struct pfioc_pooladdr *)addr;
1844 1.1 itojun
1845 1.1 itojun pf_empty_pool(&pf_pabuf);
1846 1.1 itojun pp->ticket = ++ticket_pabuf;
1847 1.1 itojun break;
1848 1.1 itojun }
1849 1.1 itojun
1850 1.1 itojun case DIOCADDADDR: {
1851 1.1 itojun struct pfioc_pooladdr *pp = (struct pfioc_pooladdr *)addr;
1852 1.1 itojun
1853 1.1 itojun #ifndef INET
1854 1.1 itojun if (pp->af == AF_INET) {
1855 1.1 itojun error = EAFNOSUPPORT;
1856 1.1 itojun break;
1857 1.1 itojun }
1858 1.1 itojun #endif /* INET */
1859 1.1 itojun #ifndef INET6
1860 1.1 itojun if (pp->af == AF_INET6) {
1861 1.1 itojun error = EAFNOSUPPORT;
1862 1.1 itojun break;
1863 1.1 itojun }
1864 1.1 itojun #endif /* INET6 */
1865 1.1 itojun if (pp->addr.addr.type != PF_ADDR_ADDRMASK &&
1866 1.1 itojun pp->addr.addr.type != PF_ADDR_DYNIFTL &&
1867 1.1 itojun pp->addr.addr.type != PF_ADDR_TABLE) {
1868 1.1 itojun error = EINVAL;
1869 1.1 itojun break;
1870 1.1 itojun }
1871 1.1 itojun pa = pool_get(&pf_pooladdr_pl, PR_NOWAIT);
1872 1.1 itojun if (pa == NULL) {
1873 1.1 itojun error = ENOMEM;
1874 1.1 itojun break;
1875 1.1 itojun }
1876 1.1 itojun bcopy(&pp->addr, pa, sizeof(struct pf_pooladdr));
1877 1.1 itojun if (pa->ifname[0]) {
1878 1.1 itojun pa->kif = pfi_attach_rule(pa->ifname);
1879 1.1 itojun if (pa->kif == NULL) {
1880 1.1 itojun pool_put(&pf_pooladdr_pl, pa);
1881 1.1 itojun error = EINVAL;
1882 1.1 itojun break;
1883 1.1 itojun }
1884 1.1 itojun }
1885 1.1 itojun if (pfi_dynaddr_setup(&pa->addr, pp->af)) {
1886 1.1 itojun pfi_dynaddr_remove(&pa->addr);
1887 1.1 itojun pfi_detach_rule(pa->kif);
1888 1.1 itojun pool_put(&pf_pooladdr_pl, pa);
1889 1.1 itojun error = EINVAL;
1890 1.1 itojun break;
1891 1.1 itojun }
1892 1.1 itojun TAILQ_INSERT_TAIL(&pf_pabuf, pa, entries);
1893 1.1 itojun break;
1894 1.1 itojun }
1895 1.1 itojun
1896 1.1 itojun case DIOCGETADDRS: {
1897 1.1 itojun struct pfioc_pooladdr *pp = (struct pfioc_pooladdr *)addr;
1898 1.1 itojun
1899 1.1 itojun pp->nr = 0;
1900 1.1 itojun s = splsoftnet();
1901 1.1 itojun pool = pf_get_pool(pp->anchor, pp->ruleset, pp->ticket,
1902 1.1 itojun pp->r_action, pp->r_num, 0, 1, 0);
1903 1.1 itojun if (pool == NULL) {
1904 1.1 itojun error = EBUSY;
1905 1.1 itojun splx(s);
1906 1.1 itojun break;
1907 1.1 itojun }
1908 1.1 itojun TAILQ_FOREACH(pa, &pool->list, entries)
1909 1.1 itojun pp->nr++;
1910 1.1 itojun splx(s);
1911 1.1 itojun break;
1912 1.1 itojun }
1913 1.1 itojun
1914 1.1 itojun case DIOCGETADDR: {
1915 1.1 itojun struct pfioc_pooladdr *pp = (struct pfioc_pooladdr *)addr;
1916 1.1 itojun u_int32_t nr = 0;
1917 1.1 itojun
1918 1.1 itojun s = splsoftnet();
1919 1.1 itojun pool = pf_get_pool(pp->anchor, pp->ruleset, pp->ticket,
1920 1.1 itojun pp->r_action, pp->r_num, 0, 1, 1);
1921 1.1 itojun if (pool == NULL) {
1922 1.1 itojun error = EBUSY;
1923 1.1 itojun splx(s);
1924 1.1 itojun break;
1925 1.1 itojun }
1926 1.1 itojun pa = TAILQ_FIRST(&pool->list);
1927 1.1 itojun while ((pa != NULL) && (nr < pp->nr)) {
1928 1.1 itojun pa = TAILQ_NEXT(pa, entries);
1929 1.1 itojun nr++;
1930 1.1 itojun }
1931 1.1 itojun if (pa == NULL) {
1932 1.1 itojun error = EBUSY;
1933 1.1 itojun splx(s);
1934 1.1 itojun break;
1935 1.1 itojun }
1936 1.1 itojun bcopy(pa, &pp->addr, sizeof(struct pf_pooladdr));
1937 1.1 itojun pfi_dynaddr_copyout(&pp->addr.addr);
1938 1.1 itojun pf_tbladdr_copyout(&pp->addr.addr);
1939 1.1 itojun splx(s);
1940 1.1 itojun break;
1941 1.1 itojun }
1942 1.1 itojun
1943 1.1 itojun case DIOCCHANGEADDR: {
1944 1.1 itojun struct pfioc_pooladdr *pca = (struct pfioc_pooladdr *)addr;
1945 1.1 itojun struct pf_pooladdr *oldpa = NULL, *newpa = NULL;
1946 1.1 itojun struct pf_ruleset *ruleset;
1947 1.1 itojun
1948 1.1 itojun if (pca->action < PF_CHANGE_ADD_HEAD ||
1949 1.1 itojun pca->action > PF_CHANGE_REMOVE) {
1950 1.1 itojun error = EINVAL;
1951 1.1 itojun break;
1952 1.1 itojun }
1953 1.1 itojun if (pca->addr.addr.type != PF_ADDR_ADDRMASK &&
1954 1.1 itojun pca->addr.addr.type != PF_ADDR_DYNIFTL &&
1955 1.1 itojun pca->addr.addr.type != PF_ADDR_TABLE) {
1956 1.1 itojun error = EINVAL;
1957 1.1 itojun break;
1958 1.1 itojun }
1959 1.1 itojun
1960 1.1 itojun ruleset = pf_find_ruleset(pca->anchor, pca->ruleset);
1961 1.1 itojun if (ruleset == NULL) {
1962 1.1 itojun error = EBUSY;
1963 1.1 itojun break;
1964 1.1 itojun }
1965 1.1 itojun pool = pf_get_pool(pca->anchor, pca->ruleset, pca->ticket,
1966 1.1 itojun pca->r_action, pca->r_num, pca->r_last, 1, 1);
1967 1.1 itojun if (pool == NULL) {
1968 1.1 itojun error = EBUSY;
1969 1.1 itojun break;
1970 1.1 itojun }
1971 1.1 itojun if (pca->action != PF_CHANGE_REMOVE) {
1972 1.1 itojun newpa = pool_get(&pf_pooladdr_pl, PR_NOWAIT);
1973 1.1 itojun if (newpa == NULL) {
1974 1.1 itojun error = ENOMEM;
1975 1.1 itojun break;
1976 1.1 itojun }
1977 1.1 itojun bcopy(&pca->addr, newpa, sizeof(struct pf_pooladdr));
1978 1.1 itojun #ifndef INET
1979 1.1 itojun if (pca->af == AF_INET) {
1980 1.1 itojun pool_put(&pf_pooladdr_pl, newpa);
1981 1.1 itojun error = EAFNOSUPPORT;
1982 1.1 itojun break;
1983 1.1 itojun }
1984 1.1 itojun #endif /* INET */
1985 1.1 itojun #ifndef INET6
1986 1.1 itojun if (pca->af == AF_INET6) {
1987 1.1 itojun pool_put(&pf_pooladdr_pl, newpa);
1988 1.1 itojun error = EAFNOSUPPORT;
1989 1.1 itojun break;
1990 1.1 itojun }
1991 1.1 itojun #endif /* INET6 */
1992 1.1 itojun if (newpa->ifname[0]) {
1993 1.1 itojun newpa->kif = pfi_attach_rule(newpa->ifname);
1994 1.1 itojun if (newpa->kif == NULL) {
1995 1.1 itojun pool_put(&pf_pooladdr_pl, newpa);
1996 1.1 itojun error = EINVAL;
1997 1.1 itojun break;
1998 1.1 itojun }
1999 1.1 itojun } else
2000 1.1 itojun newpa->kif = NULL;
2001 1.1 itojun if (pfi_dynaddr_setup(&newpa->addr, pca->af) ||
2002 1.1 itojun pf_tbladdr_setup(ruleset, &newpa->addr)) {
2003 1.1 itojun pfi_dynaddr_remove(&newpa->addr);
2004 1.1 itojun pfi_detach_rule(newpa->kif);
2005 1.1 itojun pool_put(&pf_pooladdr_pl, newpa);
2006 1.1 itojun error = EINVAL;
2007 1.1 itojun break;
2008 1.1 itojun }
2009 1.1 itojun }
2010 1.1 itojun
2011 1.1 itojun s = splsoftnet();
2012 1.1 itojun
2013 1.1 itojun if (pca->action == PF_CHANGE_ADD_HEAD)
2014 1.1 itojun oldpa = TAILQ_FIRST(&pool->list);
2015 1.1 itojun else if (pca->action == PF_CHANGE_ADD_TAIL)
2016 1.1 itojun oldpa = TAILQ_LAST(&pool->list, pf_palist);
2017 1.1 itojun else {
2018 1.1 itojun int i = 0;
2019 1.1 itojun
2020 1.1 itojun oldpa = TAILQ_FIRST(&pool->list);
2021 1.1 itojun while ((oldpa != NULL) && (i < pca->nr)) {
2022 1.1 itojun oldpa = TAILQ_NEXT(oldpa, entries);
2023 1.1 itojun i++;
2024 1.1 itojun }
2025 1.1 itojun if (oldpa == NULL) {
2026 1.1 itojun error = EINVAL;
2027 1.1 itojun splx(s);
2028 1.1 itojun break;
2029 1.1 itojun }
2030 1.1 itojun }
2031 1.1 itojun
2032 1.1 itojun if (pca->action == PF_CHANGE_REMOVE) {
2033 1.1 itojun TAILQ_REMOVE(&pool->list, oldpa, entries);
2034 1.1 itojun pfi_dynaddr_remove(&oldpa->addr);
2035 1.1 itojun pf_tbladdr_remove(&oldpa->addr);
2036 1.1 itojun pfi_detach_rule(oldpa->kif);
2037 1.1 itojun pool_put(&pf_pooladdr_pl, oldpa);
2038 1.1 itojun } else {
2039 1.1 itojun if (oldpa == NULL)
2040 1.1 itojun TAILQ_INSERT_TAIL(&pool->list, newpa, entries);
2041 1.1 itojun else if (pca->action == PF_CHANGE_ADD_HEAD ||
2042 1.1 itojun pca->action == PF_CHANGE_ADD_BEFORE)
2043 1.1 itojun TAILQ_INSERT_BEFORE(oldpa, newpa, entries);
2044 1.1 itojun else
2045 1.1 itojun TAILQ_INSERT_AFTER(&pool->list, oldpa,
2046 1.1 itojun newpa, entries);
2047 1.1 itojun }
2048 1.1 itojun
2049 1.1 itojun pool->cur = TAILQ_FIRST(&pool->list);
2050 1.1 itojun PF_ACPY(&pool->counter, &pool->cur->addr.v.a.addr,
2051 1.1 itojun pca->af);
2052 1.1 itojun splx(s);
2053 1.1 itojun break;
2054 1.1 itojun }
2055 1.1 itojun
2056 1.1 itojun case DIOCGETANCHORS: {
2057 1.1 itojun struct pfioc_anchor *pa = (struct pfioc_anchor *)addr;
2058 1.1 itojun struct pf_anchor *anchor;
2059 1.1 itojun
2060 1.1 itojun pa->nr = 0;
2061 1.1 itojun TAILQ_FOREACH(anchor, &pf_anchors, entries)
2062 1.1 itojun pa->nr++;
2063 1.1 itojun break;
2064 1.1 itojun }
2065 1.1 itojun
2066 1.1 itojun case DIOCGETANCHOR: {
2067 1.1 itojun struct pfioc_anchor *pa = (struct pfioc_anchor *)addr;
2068 1.1 itojun struct pf_anchor *anchor;
2069 1.1 itojun u_int32_t nr = 0;
2070 1.1 itojun
2071 1.1 itojun anchor = TAILQ_FIRST(&pf_anchors);
2072 1.1 itojun while (anchor != NULL && nr < pa->nr) {
2073 1.1 itojun anchor = TAILQ_NEXT(anchor, entries);
2074 1.1 itojun nr++;
2075 1.1 itojun }
2076 1.1 itojun if (anchor == NULL)
2077 1.1 itojun error = EBUSY;
2078 1.1 itojun else
2079 1.1 itojun bcopy(anchor->name, pa->name, sizeof(pa->name));
2080 1.1 itojun break;
2081 1.1 itojun }
2082 1.1 itojun
2083 1.1 itojun case DIOCGETRULESETS: {
2084 1.1 itojun struct pfioc_ruleset *pr = (struct pfioc_ruleset *)addr;
2085 1.1 itojun struct pf_anchor *anchor;
2086 1.1 itojun struct pf_ruleset *ruleset;
2087 1.1 itojun
2088 1.1 itojun pr->anchor[PF_ANCHOR_NAME_SIZE-1] = 0;
2089 1.1 itojun if ((anchor = pf_find_anchor(pr->anchor)) == NULL) {
2090 1.1 itojun error = EINVAL;
2091 1.1 itojun break;
2092 1.1 itojun }
2093 1.1 itojun pr->nr = 0;
2094 1.1 itojun TAILQ_FOREACH(ruleset, &anchor->rulesets, entries)
2095 1.1 itojun pr->nr++;
2096 1.1 itojun break;
2097 1.1 itojun }
2098 1.1 itojun
2099 1.1 itojun case DIOCGETRULESET: {
2100 1.1 itojun struct pfioc_ruleset *pr = (struct pfioc_ruleset *)addr;
2101 1.1 itojun struct pf_anchor *anchor;
2102 1.1 itojun struct pf_ruleset *ruleset;
2103 1.1 itojun u_int32_t nr = 0;
2104 1.1 itojun
2105 1.1 itojun if ((anchor = pf_find_anchor(pr->anchor)) == NULL) {
2106 1.1 itojun error = EINVAL;
2107 1.1 itojun break;
2108 1.1 itojun }
2109 1.1 itojun ruleset = TAILQ_FIRST(&anchor->rulesets);
2110 1.1 itojun while (ruleset != NULL && nr < pr->nr) {
2111 1.1 itojun ruleset = TAILQ_NEXT(ruleset, entries);
2112 1.1 itojun nr++;
2113 1.1 itojun }
2114 1.1 itojun if (ruleset == NULL)
2115 1.1 itojun error = EBUSY;
2116 1.1 itojun else
2117 1.1 itojun bcopy(ruleset->name, pr->name, sizeof(pr->name));
2118 1.1 itojun break;
2119 1.1 itojun }
2120 1.1 itojun
2121 1.1 itojun case DIOCRCLRTABLES: {
2122 1.1 itojun struct pfioc_table *io = (struct pfioc_table *)addr;
2123 1.1 itojun
2124 1.1 itojun if (io->pfrio_esize != 0) {
2125 1.1 itojun error = ENODEV;
2126 1.1 itojun break;
2127 1.1 itojun }
2128 1.1 itojun error = pfr_clr_tables(&io->pfrio_table, &io->pfrio_ndel,
2129 1.1 itojun io->pfrio_flags | PFR_FLAG_USERIOCTL);
2130 1.1 itojun break;
2131 1.1 itojun }
2132 1.1 itojun
2133 1.1 itojun case DIOCRADDTABLES: {
2134 1.1 itojun struct pfioc_table *io = (struct pfioc_table *)addr;
2135 1.1 itojun
2136 1.1 itojun if (io->pfrio_esize != sizeof(struct pfr_table)) {
2137 1.1 itojun error = ENODEV;
2138 1.1 itojun break;
2139 1.1 itojun }
2140 1.1 itojun error = pfr_add_tables(io->pfrio_buffer, io->pfrio_size,
2141 1.1 itojun &io->pfrio_nadd, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2142 1.1 itojun break;
2143 1.1 itojun }
2144 1.1 itojun
2145 1.1 itojun case DIOCRDELTABLES: {
2146 1.1 itojun struct pfioc_table *io = (struct pfioc_table *)addr;
2147 1.1 itojun
2148 1.1 itojun if (io->pfrio_esize != sizeof(struct pfr_table)) {
2149 1.1 itojun error = ENODEV;
2150 1.1 itojun break;
2151 1.1 itojun }
2152 1.1 itojun error = pfr_del_tables(io->pfrio_buffer, io->pfrio_size,
2153 1.1 itojun &io->pfrio_ndel, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2154 1.1 itojun break;
2155 1.1 itojun }
2156 1.1 itojun
2157 1.1 itojun case DIOCRGETTABLES: {
2158 1.1 itojun struct pfioc_table *io = (struct pfioc_table *)addr;
2159 1.1 itojun
2160 1.1 itojun if (io->pfrio_esize != sizeof(struct pfr_table)) {
2161 1.1 itojun error = ENODEV;
2162 1.1 itojun break;
2163 1.1 itojun }
2164 1.1 itojun error = pfr_get_tables(&io->pfrio_table, io->pfrio_buffer,
2165 1.1 itojun &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2166 1.1 itojun break;
2167 1.1 itojun }
2168 1.1 itojun
2169 1.1 itojun case DIOCRGETTSTATS: {
2170 1.1 itojun struct pfioc_table *io = (struct pfioc_table *)addr;
2171 1.1 itojun
2172 1.1 itojun if (io->pfrio_esize != sizeof(struct pfr_tstats)) {
2173 1.1 itojun error = ENODEV;
2174 1.1 itojun break;
2175 1.1 itojun }
2176 1.1 itojun error = pfr_get_tstats(&io->pfrio_table, io->pfrio_buffer,
2177 1.1 itojun &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2178 1.1 itojun break;
2179 1.1 itojun }
2180 1.1 itojun
2181 1.1 itojun case DIOCRCLRTSTATS: {
2182 1.1 itojun struct pfioc_table *io = (struct pfioc_table *)addr;
2183 1.1 itojun
2184 1.1 itojun if (io->pfrio_esize != sizeof(struct pfr_table)) {
2185 1.1 itojun error = ENODEV;
2186 1.1 itojun break;
2187 1.1 itojun }
2188 1.1 itojun error = pfr_clr_tstats(io->pfrio_buffer, io->pfrio_size,
2189 1.1 itojun &io->pfrio_nzero, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2190 1.1 itojun break;
2191 1.1 itojun }
2192 1.1 itojun
2193 1.1 itojun case DIOCRSETTFLAGS: {
2194 1.1 itojun struct pfioc_table *io = (struct pfioc_table *)addr;
2195 1.1 itojun
2196 1.1 itojun if (io->pfrio_esize != sizeof(struct pfr_table)) {
2197 1.1 itojun error = ENODEV;
2198 1.1 itojun break;
2199 1.1 itojun }
2200 1.1 itojun error = pfr_set_tflags(io->pfrio_buffer, io->pfrio_size,
2201 1.1 itojun io->pfrio_setflag, io->pfrio_clrflag, &io->pfrio_nchange,
2202 1.1 itojun &io->pfrio_ndel, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2203 1.1 itojun break;
2204 1.1 itojun }
2205 1.1 itojun
2206 1.1 itojun case DIOCRCLRADDRS: {
2207 1.1 itojun struct pfioc_table *io = (struct pfioc_table *)addr;
2208 1.1 itojun
2209 1.1 itojun if (io->pfrio_esize != 0) {
2210 1.1 itojun error = ENODEV;
2211 1.1 itojun break;
2212 1.1 itojun }
2213 1.1 itojun error = pfr_clr_addrs(&io->pfrio_table, &io->pfrio_ndel,
2214 1.1 itojun io->pfrio_flags | PFR_FLAG_USERIOCTL);
2215 1.1 itojun break;
2216 1.1 itojun }
2217 1.1 itojun
2218 1.1 itojun case DIOCRADDADDRS: {
2219 1.1 itojun struct pfioc_table *io = (struct pfioc_table *)addr;
2220 1.1 itojun
2221 1.1 itojun if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2222 1.1 itojun error = ENODEV;
2223 1.1 itojun break;
2224 1.1 itojun }
2225 1.1 itojun error = pfr_add_addrs(&io->pfrio_table, io->pfrio_buffer,
2226 1.1 itojun io->pfrio_size, &io->pfrio_nadd, io->pfrio_flags |
2227 1.1 itojun PFR_FLAG_USERIOCTL);
2228 1.1 itojun break;
2229 1.1 itojun }
2230 1.1 itojun
2231 1.1 itojun case DIOCRDELADDRS: {
2232 1.1 itojun struct pfioc_table *io = (struct pfioc_table *)addr;
2233 1.1 itojun
2234 1.1 itojun if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2235 1.1 itojun error = ENODEV;
2236 1.1 itojun break;
2237 1.1 itojun }
2238 1.1 itojun error = pfr_del_addrs(&io->pfrio_table, io->pfrio_buffer,
2239 1.1 itojun io->pfrio_size, &io->pfrio_ndel, io->pfrio_flags |
2240 1.1 itojun PFR_FLAG_USERIOCTL);
2241 1.1 itojun break;
2242 1.1 itojun }
2243 1.1 itojun
2244 1.1 itojun case DIOCRSETADDRS: {
2245 1.1 itojun struct pfioc_table *io = (struct pfioc_table *)addr;
2246 1.1 itojun
2247 1.1 itojun if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2248 1.1 itojun error = ENODEV;
2249 1.1 itojun break;
2250 1.1 itojun }
2251 1.1 itojun error = pfr_set_addrs(&io->pfrio_table, io->pfrio_buffer,
2252 1.1 itojun io->pfrio_size, &io->pfrio_size2, &io->pfrio_nadd,
2253 1.1 itojun &io->pfrio_ndel, &io->pfrio_nchange, io->pfrio_flags |
2254 1.1 itojun PFR_FLAG_USERIOCTL);
2255 1.1 itojun break;
2256 1.1 itojun }
2257 1.1 itojun
2258 1.1 itojun case DIOCRGETADDRS: {
2259 1.1 itojun struct pfioc_table *io = (struct pfioc_table *)addr;
2260 1.1 itojun
2261 1.1 itojun if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2262 1.1 itojun error = ENODEV;
2263 1.1 itojun break;
2264 1.1 itojun }
2265 1.1 itojun error = pfr_get_addrs(&io->pfrio_table, io->pfrio_buffer,
2266 1.1 itojun &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2267 1.1 itojun break;
2268 1.1 itojun }
2269 1.1 itojun
2270 1.1 itojun case DIOCRGETASTATS: {
2271 1.1 itojun struct pfioc_table *io = (struct pfioc_table *)addr;
2272 1.1 itojun
2273 1.1 itojun if (io->pfrio_esize != sizeof(struct pfr_astats)) {
2274 1.1 itojun error = ENODEV;
2275 1.1 itojun break;
2276 1.1 itojun }
2277 1.1 itojun error = pfr_get_astats(&io->pfrio_table, io->pfrio_buffer,
2278 1.1 itojun &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2279 1.1 itojun break;
2280 1.1 itojun }
2281 1.1 itojun
2282 1.1 itojun case DIOCRCLRASTATS: {
2283 1.1 itojun struct pfioc_table *io = (struct pfioc_table *)addr;
2284 1.1 itojun
2285 1.1 itojun if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2286 1.1 itojun error = ENODEV;
2287 1.1 itojun break;
2288 1.1 itojun }
2289 1.1 itojun error = pfr_clr_astats(&io->pfrio_table, io->pfrio_buffer,
2290 1.1 itojun io->pfrio_size, &io->pfrio_nzero, io->pfrio_flags |
2291 1.1 itojun PFR_FLAG_USERIOCTL);
2292 1.1 itojun break;
2293 1.1 itojun }
2294 1.1 itojun
2295 1.1 itojun case DIOCRTSTADDRS: {
2296 1.1 itojun struct pfioc_table *io = (struct pfioc_table *)addr;
2297 1.1 itojun
2298 1.1 itojun if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2299 1.1 itojun error = ENODEV;
2300 1.1 itojun break;
2301 1.1 itojun }
2302 1.1 itojun error = pfr_tst_addrs(&io->pfrio_table, io->pfrio_buffer,
2303 1.1 itojun io->pfrio_size, &io->pfrio_nmatch, io->pfrio_flags |
2304 1.1 itojun PFR_FLAG_USERIOCTL);
2305 1.1 itojun break;
2306 1.1 itojun }
2307 1.1 itojun
2308 1.1 itojun case DIOCRINABEGIN: {
2309 1.1 itojun struct pfioc_table *io = (struct pfioc_table *)addr;
2310 1.1 itojun
2311 1.1 itojun if (io->pfrio_esize != 0) {
2312 1.1 itojun error = ENODEV;
2313 1.1 itojun break;
2314 1.1 itojun }
2315 1.1 itojun error = pfr_ina_begin(&io->pfrio_table, &io->pfrio_ticket,
2316 1.1 itojun &io->pfrio_ndel, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2317 1.1 itojun break;
2318 1.1 itojun }
2319 1.1 itojun
2320 1.1 itojun case DIOCRINACOMMIT: {
2321 1.1 itojun struct pfioc_table *io = (struct pfioc_table *)addr;
2322 1.1 itojun
2323 1.1 itojun if (io->pfrio_esize != 0) {
2324 1.1 itojun error = ENODEV;
2325 1.1 itojun break;
2326 1.1 itojun }
2327 1.1 itojun error = pfr_ina_commit(&io->pfrio_table, io->pfrio_ticket,
2328 1.1 itojun &io->pfrio_nadd, &io->pfrio_nchange, io->pfrio_flags |
2329 1.1 itojun PFR_FLAG_USERIOCTL);
2330 1.1 itojun break;
2331 1.1 itojun }
2332 1.1 itojun
2333 1.1 itojun case DIOCRINADEFINE: {
2334 1.1 itojun struct pfioc_table *io = (struct pfioc_table *)addr;
2335 1.1 itojun
2336 1.1 itojun if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2337 1.1 itojun error = ENODEV;
2338 1.1 itojun break;
2339 1.1 itojun }
2340 1.1 itojun error = pfr_ina_define(&io->pfrio_table, io->pfrio_buffer,
2341 1.1 itojun io->pfrio_size, &io->pfrio_nadd, &io->pfrio_naddr,
2342 1.1 itojun io->pfrio_ticket, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2343 1.1 itojun break;
2344 1.1 itojun }
2345 1.1 itojun
2346 1.1 itojun case DIOCOSFPADD: {
2347 1.1 itojun struct pf_osfp_ioctl *io = (struct pf_osfp_ioctl *)addr;
2348 1.1 itojun s = splsoftnet();
2349 1.1 itojun error = pf_osfp_add(io);
2350 1.1 itojun splx(s);
2351 1.1 itojun break;
2352 1.1 itojun }
2353 1.1 itojun
2354 1.1 itojun case DIOCOSFPGET: {
2355 1.1 itojun struct pf_osfp_ioctl *io = (struct pf_osfp_ioctl *)addr;
2356 1.1 itojun s = splsoftnet();
2357 1.1 itojun error = pf_osfp_get(io);
2358 1.1 itojun splx(s);
2359 1.1 itojun break;
2360 1.1 itojun }
2361 1.1 itojun
2362 1.1 itojun case DIOCXBEGIN: {
2363 1.1 itojun struct pfioc_trans *io = (struct pfioc_trans *)addr;
2364 1.1 itojun struct pfioc_trans_e ioe;
2365 1.1 itojun struct pfr_table table;
2366 1.1 itojun int i;
2367 1.1 itojun
2368 1.1 itojun if (io->esize != sizeof(ioe)) {
2369 1.1 itojun error = ENODEV;
2370 1.1 itojun goto fail;
2371 1.1 itojun }
2372 1.1 itojun for (i = 0; i < io->size; i++) {
2373 1.1 itojun if (copyin(io->array+i, &ioe, sizeof(ioe))) {
2374 1.1 itojun error = EFAULT;
2375 1.1 itojun goto fail;
2376 1.1 itojun }
2377 1.1 itojun switch (ioe.rs_num) {
2378 1.1 itojun #ifdef ALTQ
2379 1.1 itojun case PF_RULESET_ALTQ:
2380 1.1 itojun if (ioe.anchor[0] || ioe.ruleset[0]) {
2381 1.1 itojun error = EINVAL;
2382 1.1 itojun goto fail;
2383 1.1 itojun }
2384 1.1 itojun if ((error = pf_begin_altq(&ioe.ticket)))
2385 1.1 itojun goto fail;
2386 1.1 itojun break;
2387 1.1 itojun #endif /* ALTQ */
2388 1.1 itojun case PF_RULESET_TABLE:
2389 1.1 itojun bzero(&table, sizeof(table));
2390 1.1 itojun strlcpy(table.pfrt_anchor, ioe.anchor,
2391 1.1 itojun sizeof(table.pfrt_anchor));
2392 1.1 itojun strlcpy(table.pfrt_ruleset, ioe.ruleset,
2393 1.1 itojun sizeof(table.pfrt_ruleset));
2394 1.1 itojun if ((error = pfr_ina_begin(&table,
2395 1.1 itojun &ioe.ticket, NULL, 0)))
2396 1.1 itojun goto fail;
2397 1.1 itojun break;
2398 1.1 itojun default:
2399 1.1 itojun if ((error = pf_begin_rules(&ioe.ticket,
2400 1.1 itojun ioe.rs_num, ioe.anchor, ioe.ruleset)))
2401 1.1 itojun goto fail;
2402 1.1 itojun break;
2403 1.1 itojun }
2404 1.1 itojun if (copyout(&ioe, io->array+i, sizeof(io->array[i]))) {
2405 1.1 itojun error = EFAULT;
2406 1.1 itojun goto fail;
2407 1.1 itojun }
2408 1.1 itojun }
2409 1.1 itojun break;
2410 1.1 itojun }
2411 1.1 itojun
2412 1.1 itojun case DIOCXROLLBACK: {
2413 1.1 itojun struct pfioc_trans *io = (struct pfioc_trans *)addr;
2414 1.1 itojun struct pfioc_trans_e ioe;
2415 1.1 itojun struct pfr_table table;
2416 1.1 itojun int i;
2417 1.1 itojun
2418 1.1 itojun if (io->esize != sizeof(ioe)) {
2419 1.1 itojun error = ENODEV;
2420 1.1 itojun goto fail;
2421 1.1 itojun }
2422 1.1 itojun for (i = 0; i < io->size; i++) {
2423 1.1 itojun if (copyin(io->array+i, &ioe, sizeof(ioe))) {
2424 1.1 itojun error = EFAULT;
2425 1.1 itojun goto fail;
2426 1.1 itojun }
2427 1.1 itojun switch (ioe.rs_num) {
2428 1.1 itojun #ifdef ALTQ
2429 1.1 itojun case PF_RULESET_ALTQ:
2430 1.1 itojun if (ioe.anchor[0] || ioe.ruleset[0]) {
2431 1.1 itojun error = EINVAL;
2432 1.1 itojun goto fail;
2433 1.1 itojun }
2434 1.1 itojun if ((error = pf_rollback_altq(ioe.ticket)))
2435 1.1 itojun goto fail; /* really bad */
2436 1.1 itojun break;
2437 1.1 itojun #endif /* ALTQ */
2438 1.1 itojun case PF_RULESET_TABLE:
2439 1.1 itojun bzero(&table, sizeof(table));
2440 1.1 itojun strlcpy(table.pfrt_anchor, ioe.anchor,
2441 1.1 itojun sizeof(table.pfrt_anchor));
2442 1.1 itojun strlcpy(table.pfrt_ruleset, ioe.ruleset,
2443 1.1 itojun sizeof(table.pfrt_ruleset));
2444 1.1 itojun if ((error = pfr_ina_rollback(&table,
2445 1.1 itojun ioe.ticket, NULL, 0)))
2446 1.1 itojun goto fail; /* really bad */
2447 1.1 itojun break;
2448 1.1 itojun default:
2449 1.1 itojun if ((error = pf_rollback_rules(ioe.ticket,
2450 1.1 itojun ioe.rs_num, ioe.anchor, ioe.ruleset)))
2451 1.1 itojun goto fail; /* really bad */
2452 1.1 itojun break;
2453 1.1 itojun }
2454 1.1 itojun }
2455 1.1 itojun break;
2456 1.1 itojun }
2457 1.1 itojun
2458 1.1 itojun case DIOCXCOMMIT: {
2459 1.1 itojun struct pfioc_trans *io = (struct pfioc_trans *)addr;
2460 1.1 itojun struct pfioc_trans_e ioe;
2461 1.1 itojun struct pfr_table table;
2462 1.1 itojun struct pf_ruleset *rs;
2463 1.1 itojun int i;
2464 1.1 itojun
2465 1.1 itojun if (io->esize != sizeof(ioe)) {
2466 1.1 itojun error = ENODEV;
2467 1.1 itojun goto fail;
2468 1.1 itojun }
2469 1.1 itojun /* first makes sure everything will succeed */
2470 1.1 itojun for (i = 0; i < io->size; i++) {
2471 1.1 itojun if (copyin(io->array+i, &ioe, sizeof(ioe))) {
2472 1.1 itojun error = EFAULT;
2473 1.1 itojun goto fail;
2474 1.1 itojun }
2475 1.1 itojun switch (ioe.rs_num) {
2476 1.1 itojun #ifdef ALTQ
2477 1.1 itojun case PF_RULESET_ALTQ:
2478 1.1 itojun if (ioe.anchor[0] || ioe.ruleset[0]) {
2479 1.1 itojun error = EINVAL;
2480 1.1 itojun goto fail;
2481 1.1 itojun }
2482 1.1 itojun if (!altqs_inactive_open || ioe.ticket !=
2483 1.1 itojun ticket_altqs_inactive) {
2484 1.1 itojun error = EBUSY;
2485 1.1 itojun goto fail;
2486 1.1 itojun }
2487 1.1 itojun break;
2488 1.1 itojun #endif /* ALTQ */
2489 1.1 itojun case PF_RULESET_TABLE:
2490 1.1 itojun rs = pf_find_ruleset(ioe.anchor, ioe.ruleset);
2491 1.1 itojun if (rs == NULL || !rs->topen || ioe.ticket !=
2492 1.1 itojun rs->tticket) {
2493 1.1 itojun error = EBUSY;
2494 1.1 itojun goto fail;
2495 1.1 itojun }
2496 1.1 itojun break;
2497 1.1 itojun default:
2498 1.1 itojun if (ioe.rs_num < 0 || ioe.rs_num >=
2499 1.1 itojun PF_RULESET_MAX) {
2500 1.1 itojun error = EINVAL;
2501 1.1 itojun goto fail;
2502 1.1 itojun }
2503 1.1 itojun rs = pf_find_ruleset(ioe.anchor, ioe.ruleset);
2504 1.1 itojun if (rs == NULL ||
2505 1.1 itojun !rs->rules[ioe.rs_num].inactive.open ||
2506 1.1 itojun rs->rules[ioe.rs_num].inactive.ticket !=
2507 1.1 itojun ioe.ticket) {
2508 1.1 itojun error = EBUSY;
2509 1.1 itojun goto fail;
2510 1.1 itojun }
2511 1.1 itojun break;
2512 1.1 itojun }
2513 1.1 itojun }
2514 1.1 itojun /* now do the commit - no errors should happen here */
2515 1.1 itojun for (i = 0; i < io->size; i++) {
2516 1.1 itojun if (copyin(io->array+i, &ioe, sizeof(ioe))) {
2517 1.1 itojun error = EFAULT;
2518 1.1 itojun goto fail;
2519 1.1 itojun }
2520 1.1 itojun switch (ioe.rs_num) {
2521 1.1 itojun #ifdef ALTQ
2522 1.1 itojun case PF_RULESET_ALTQ:
2523 1.1 itojun if ((error = pf_commit_altq(ioe.ticket)))
2524 1.1 itojun goto fail; /* really bad */
2525 1.1 itojun break;
2526 1.1 itojun #endif /* ALTQ */
2527 1.1 itojun case PF_RULESET_TABLE:
2528 1.1 itojun bzero(&table, sizeof(table));
2529 1.1 itojun strlcpy(table.pfrt_anchor, ioe.anchor,
2530 1.1 itojun sizeof(table.pfrt_anchor));
2531 1.1 itojun strlcpy(table.pfrt_ruleset, ioe.ruleset,
2532 1.1 itojun sizeof(table.pfrt_ruleset));
2533 1.1 itojun if ((error = pfr_ina_commit(&table, ioe.ticket,
2534 1.1 itojun NULL, NULL, 0)))
2535 1.1 itojun goto fail; /* really bad */
2536 1.1 itojun break;
2537 1.1 itojun default:
2538 1.1 itojun if ((error = pf_commit_rules(ioe.ticket,
2539 1.1 itojun ioe.rs_num, ioe.anchor, ioe.ruleset)))
2540 1.1 itojun goto fail; /* really bad */
2541 1.1 itojun break;
2542 1.1 itojun }
2543 1.1 itojun }
2544 1.1 itojun break;
2545 1.1 itojun }
2546 1.1 itojun
2547 1.1 itojun case DIOCGETSRCNODES: {
2548 1.1 itojun struct pfioc_src_nodes *psn = (struct pfioc_src_nodes *)addr;
2549 1.1 itojun struct pf_src_node *n;
2550 1.1 itojun struct pf_src_node *p, pstore;
2551 1.1 itojun u_int32_t nr = 0;
2552 1.1 itojun int space = psn->psn_len;
2553 1.1 itojun
2554 1.1 itojun if (space == 0) {
2555 1.1 itojun s = splsoftnet();
2556 1.1 itojun RB_FOREACH(n, pf_src_tree, &tree_src_tracking)
2557 1.1 itojun nr++;
2558 1.1 itojun splx(s);
2559 1.1 itojun psn->psn_len = sizeof(struct pf_src_node) * nr;
2560 1.1 itojun return (0);
2561 1.1 itojun }
2562 1.1 itojun
2563 1.1 itojun s = splsoftnet();
2564 1.1 itojun p = psn->psn_src_nodes;
2565 1.1 itojun RB_FOREACH(n, pf_src_tree, &tree_src_tracking) {
2566 1.1 itojun int secs = time.tv_sec;
2567 1.1 itojun
2568 1.1 itojun if ((nr + 1) * sizeof(*p) > (unsigned)psn->psn_len)
2569 1.1 itojun break;
2570 1.1 itojun
2571 1.1 itojun bcopy(n, &pstore, sizeof(pstore));
2572 1.1 itojun if (n->rule.ptr != NULL)
2573 1.1 itojun pstore.rule.nr = n->rule.ptr->nr;
2574 1.1 itojun pstore.creation = secs - pstore.creation;
2575 1.1 itojun if (pstore.expire > secs)
2576 1.1 itojun pstore.expire -= secs;
2577 1.1 itojun else
2578 1.1 itojun pstore.expire = 0;
2579 1.1 itojun error = copyout(&pstore, p, sizeof(*p));
2580 1.1 itojun if (error) {
2581 1.1 itojun splx(s);
2582 1.1 itojun goto fail;
2583 1.1 itojun }
2584 1.1 itojun p++;
2585 1.1 itojun nr++;
2586 1.1 itojun }
2587 1.1 itojun psn->psn_len = sizeof(struct pf_src_node) * nr;
2588 1.1 itojun splx(s);
2589 1.1 itojun break;
2590 1.1 itojun }
2591 1.1 itojun
2592 1.1 itojun case DIOCCLRSRCNODES: {
2593 1.1 itojun struct pf_src_node *n;
2594 1.1 itojun struct pf_state *state;
2595 1.1 itojun
2596 1.1 itojun s = splsoftnet();
2597 1.1 itojun RB_FOREACH(state, pf_state_tree_id, &tree_id) {
2598 1.1 itojun state->src_node = NULL;
2599 1.1 itojun state->nat_src_node = NULL;
2600 1.1 itojun }
2601 1.1 itojun RB_FOREACH(n, pf_src_tree, &tree_src_tracking) {
2602 1.1 itojun n->expire = 1;
2603 1.1 itojun n->states = 0;
2604 1.1 itojun }
2605 1.1 itojun pf_purge_expired_src_nodes();
2606 1.1 itojun pf_status.src_nodes = 0;
2607 1.1 itojun splx(s);
2608 1.1 itojun break;
2609 1.1 itojun }
2610 1.1 itojun
2611 1.1 itojun case DIOCSETHOSTID: {
2612 1.1 itojun u_int32_t *hostid = (u_int32_t *)addr;
2613 1.1 itojun
2614 1.1 itojun if (*hostid == 0) {
2615 1.1 itojun error = EINVAL;
2616 1.1 itojun goto fail;
2617 1.1 itojun }
2618 1.1 itojun pf_status.hostid = *hostid;
2619 1.1 itojun break;
2620 1.1 itojun }
2621 1.1 itojun
2622 1.1 itojun case DIOCOSFPFLUSH:
2623 1.1 itojun s = splsoftnet();
2624 1.1 itojun pf_osfp_flush();
2625 1.1 itojun splx(s);
2626 1.1 itojun break;
2627 1.1 itojun
2628 1.1 itojun case DIOCIGETIFACES: {
2629 1.1 itojun struct pfioc_iface *io = (struct pfioc_iface *)addr;
2630 1.1 itojun
2631 1.1 itojun if (io->pfiio_esize != sizeof(struct pfi_if)) {
2632 1.1 itojun error = ENODEV;
2633 1.1 itojun break;
2634 1.1 itojun }
2635 1.1 itojun error = pfi_get_ifaces(io->pfiio_name, io->pfiio_buffer,
2636 1.1 itojun &io->pfiio_size, io->pfiio_flags);
2637 1.1 itojun break;
2638 1.1 itojun }
2639 1.1 itojun
2640 1.1 itojun case DIOCICLRISTATS: {
2641 1.1 itojun struct pfioc_iface *io = (struct pfioc_iface *)addr;
2642 1.1 itojun
2643 1.1 itojun error = pfi_clr_istats(io->pfiio_name, &io->pfiio_nzero,
2644 1.1 itojun io->pfiio_flags);
2645 1.1 itojun break;
2646 1.1 itojun }
2647 1.1 itojun
2648 1.1 itojun default:
2649 1.1 itojun error = ENODEV;
2650 1.1 itojun break;
2651 1.1 itojun }
2652 1.1 itojun fail:
2653 1.1 itojun
2654 1.1 itojun return (error);
2655 1.1 itojun }
2656