1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2001 Daniel Hartmeier
5 * Copyright (c) 2002,2003 Henning Brauer
6 * Copyright (c) 2012 Gleb Smirnoff <glebius@FreeBSD.org>
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * - Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * - Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer in the documentation and/or other materials provided
18 * with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 *
33 * Effort sponsored in part by the Defense Advanced Research Projects
34 * Agency (DARPA) and Air Force Research Laboratory, Air Force
35 * Materiel Command, USAF, under agreement number F30602-01-2-0537.
36 *
37 * $OpenBSD: pf_ioctl.c,v 1.213 2009/02/15 21:46:12 mbalmer Exp $
38 */
39
40 #include <sys/cdefs.h>
41 #include "opt_inet.h"
42 #include "opt_inet6.h"
43 #include "opt_bpf.h"
44 #include "opt_pf.h"
45
46 #include <sys/param.h>
47 #include <sys/_bitset.h>
48 #include <sys/bitset.h>
49 #include <sys/bus.h>
50 #include <sys/conf.h>
51 #include <sys/endian.h>
52 #include <sys/fcntl.h>
53 #include <sys/filio.h>
54 #include <sys/hash.h>
55 #include <sys/interrupt.h>
56 #include <sys/jail.h>
57 #include <sys/kernel.h>
58 #include <sys/kthread.h>
59 #include <sys/lock.h>
60 #include <sys/mbuf.h>
61 #include <sys/module.h>
62 #include <sys/nv.h>
63 #include <sys/proc.h>
64 #include <sys/sdt.h>
65 #include <sys/smp.h>
66 #include <sys/socket.h>
67 #include <sys/sysctl.h>
68 #include <sys/md5.h>
69 #include <sys/ucred.h>
70
71 #include <net/if.h>
72 #include <net/if_var.h>
73 #include <net/if_private.h>
74 #include <net/vnet.h>
75 #include <net/route.h>
76 #include <net/pfil.h>
77 #include <net/pfvar.h>
78 #include <net/if_pfsync.h>
79 #include <net/if_pflog.h>
80
81 #include <netinet/in.h>
82 #include <netinet/ip.h>
83 #include <netinet/ip_var.h>
84 #include <netinet6/ip6_var.h>
85 #include <netinet/ip_icmp.h>
86 #include <netpfil/pf/pf_nl.h>
87 #include <netpfil/pf/pf_nv.h>
88
89 #ifdef INET6
90 #include <netinet/ip6.h>
91 #endif /* INET6 */
92
93 #ifdef ALTQ
94 #include <net/altq/altq.h>
95 #endif
96
97 SDT_PROBE_DEFINE3(pf, ioctl, ioctl, error, "int", "int", "int");
98 SDT_PROBE_DEFINE3(pf, ioctl, function, error, "char *", "int", "int");
99 SDT_PROBE_DEFINE2(pf, ioctl, addrule, error, "int", "int");
100 SDT_PROBE_DEFINE2(pf, ioctl, nvchk, error, "int", "int");
101
102 static struct pf_kpool *pf_get_kpool(const char *, u_int32_t, u_int8_t,
103 u_int32_t, u_int8_t, u_int8_t, u_int8_t);
104
105 static void pf_mv_kpool(struct pf_kpalist *, struct pf_kpalist *);
106 static void pf_empty_kpool(struct pf_kpalist *);
107 static int pfioctl(struct cdev *, u_long, caddr_t, int,
108 struct thread *);
109 static int pf_begin_eth(uint32_t *, const char *);
110 static void pf_rollback_eth_cb(struct epoch_context *);
111 static int pf_rollback_eth(uint32_t, const char *);
112 static int pf_commit_eth(uint32_t, const char *);
113 static void pf_free_eth_rule(struct pf_keth_rule *);
114 #ifdef ALTQ
115 static int pf_begin_altq(u_int32_t *);
116 static int pf_rollback_altq(u_int32_t);
117 static int pf_commit_altq(u_int32_t);
118 static int pf_enable_altq(struct pf_altq *);
119 static int pf_disable_altq(struct pf_altq *);
120 static uint16_t pf_qname2qid(const char *);
121 static void pf_qid_unref(uint16_t);
122 #endif /* ALTQ */
123 static int pf_begin_rules(u_int32_t *, int, const char *);
124 static int pf_rollback_rules(u_int32_t, int, char *);
125 static int pf_setup_pfsync_matching(struct pf_kruleset *);
126 static void pf_hash_rule_rolling(MD5_CTX *, struct pf_krule *);
127 static void pf_hash_rule(struct pf_krule *);
128 static void pf_hash_rule_addr(MD5_CTX *, struct pf_rule_addr *);
129 static int pf_commit_rules(u_int32_t, int, char *);
130 static int pf_addr_setup(struct pf_kruleset *,
131 struct pf_addr_wrap *, sa_family_t);
132 static void pf_src_node_copy(const struct pf_ksrc_node *,
133 struct pf_src_node *);
134 #ifdef ALTQ
135 static int pf_export_kaltq(struct pf_altq *,
136 struct pfioc_altq_v1 *, size_t);
137 static int pf_import_kaltq(struct pfioc_altq_v1 *,
138 struct pf_altq *, size_t);
139 #endif /* ALTQ */
140
141 VNET_DEFINE(struct pf_krule, pf_default_rule);
142
143 static __inline int pf_krule_compare(struct pf_krule *,
144 struct pf_krule *);
145
146 RB_GENERATE(pf_krule_global, pf_krule, entry_global, pf_krule_compare);
147
148 #ifdef ALTQ
149 VNET_DEFINE_STATIC(int, pf_altq_running);
150 #define V_pf_altq_running VNET(pf_altq_running)
151 #endif
152
153 #define TAGID_MAX 50000
154 struct pf_tagname {
155 TAILQ_ENTRY(pf_tagname) namehash_entries;
156 TAILQ_ENTRY(pf_tagname) taghash_entries;
157 char name[PF_TAG_NAME_SIZE];
158 uint16_t tag;
159 int ref;
160 };
161
162 struct pf_tagset {
163 TAILQ_HEAD(, pf_tagname) *namehash;
164 TAILQ_HEAD(, pf_tagname) *taghash;
165 unsigned int mask;
166 uint32_t seed;
167 BITSET_DEFINE(, TAGID_MAX) avail;
168 };
169
170 VNET_DEFINE(struct pf_tagset, pf_tags);
171 #define V_pf_tags VNET(pf_tags)
172 static unsigned int pf_rule_tag_hashsize;
173 #define PF_RULE_TAG_HASH_SIZE_DEFAULT 128
174 SYSCTL_UINT(_net_pf, OID_AUTO, rule_tag_hashsize, CTLFLAG_RDTUN,
175 &pf_rule_tag_hashsize, PF_RULE_TAG_HASH_SIZE_DEFAULT,
176 "Size of pf(4) rule tag hashtable");
177
178 #ifdef ALTQ
179 VNET_DEFINE(struct pf_tagset, pf_qids);
180 #define V_pf_qids VNET(pf_qids)
181 static unsigned int pf_queue_tag_hashsize;
182 #define PF_QUEUE_TAG_HASH_SIZE_DEFAULT 128
183 SYSCTL_UINT(_net_pf, OID_AUTO, queue_tag_hashsize, CTLFLAG_RDTUN,
184 &pf_queue_tag_hashsize, PF_QUEUE_TAG_HASH_SIZE_DEFAULT,
185 "Size of pf(4) queue tag hashtable");
186 #endif
187 VNET_DEFINE(uma_zone_t, pf_tag_z);
188 #define V_pf_tag_z VNET(pf_tag_z)
189 static MALLOC_DEFINE(M_PFALTQ, "pf_altq", "pf(4) altq configuration db");
190 static MALLOC_DEFINE(M_PFRULE, "pf_rule", "pf(4) rules");
191
192 #if (PF_QNAME_SIZE != PF_TAG_NAME_SIZE)
193 #error PF_QNAME_SIZE must be equal to PF_TAG_NAME_SIZE
194 #endif
195
196 VNET_DEFINE_STATIC(bool, pf_filter_local) = false;
197 #define V_pf_filter_local VNET(pf_filter_local)
198 SYSCTL_BOOL(_net_pf, OID_AUTO, filter_local, CTLFLAG_VNET | CTLFLAG_RW,
199 &VNET_NAME(pf_filter_local), false,
200 "Enable filtering for packets delivered to local network stack");
201
202 #ifdef PF_DEFAULT_TO_DROP
203 VNET_DEFINE_STATIC(bool, default_to_drop) = true;
204 #else
205 VNET_DEFINE_STATIC(bool, default_to_drop);
206 #endif
207 #define V_default_to_drop VNET(default_to_drop)
208 SYSCTL_BOOL(_net_pf, OID_AUTO, default_to_drop, CTLFLAG_RDTUN | CTLFLAG_VNET,
209 &VNET_NAME(default_to_drop), false,
210 "Make the default rule drop all packets.");
211
212 static void pf_init_tagset(struct pf_tagset *, unsigned int *,
213 unsigned int);
214 static void pf_cleanup_tagset(struct pf_tagset *);
215 static uint16_t tagname2hashindex(const struct pf_tagset *, const char *);
216 static uint16_t tag2hashindex(const struct pf_tagset *, uint16_t);
217 static u_int16_t tagname2tag(struct pf_tagset *, const char *);
218 static u_int16_t pf_tagname2tag(const char *);
219 static void tag_unref(struct pf_tagset *, u_int16_t);
220
221 #define DPFPRINTF(n, x) if (V_pf_status.debug >= (n)) printf x
222
223 struct cdev *pf_dev;
224
225 /*
226 * XXX - These are new and need to be checked when moveing to a new version
227 */
228 static void pf_clear_all_states(void);
229 static int pf_killstates_row(struct pf_kstate_kill *,
230 struct pf_idhash *);
231 static int pf_killstates_nv(struct pfioc_nv *);
232 static int pf_clearstates_nv(struct pfioc_nv *);
233 static int pf_getstate(struct pfioc_nv *);
234 static int pf_getstatus(struct pfioc_nv *);
235 static int pf_clear_tables(void);
236 static void pf_kill_srcnodes(struct pfioc_src_node_kill *);
237 static int pf_keepcounters(struct pfioc_nv *);
238 static void pf_tbladdr_copyout(struct pf_addr_wrap *);
239
240 /*
241 * Wrapper functions for pfil(9) hooks
242 */
243 static pfil_return_t pf_eth_check_in(struct mbuf **m, struct ifnet *ifp,
244 int flags, void *ruleset __unused, struct inpcb *inp);
245 static pfil_return_t pf_eth_check_out(struct mbuf **m, struct ifnet *ifp,
246 int flags, void *ruleset __unused, struct inpcb *inp);
247 #ifdef INET
248 static pfil_return_t pf_check_in(struct mbuf **m, struct ifnet *ifp,
249 int flags, void *ruleset __unused, struct inpcb *inp);
250 static pfil_return_t pf_check_out(struct mbuf **m, struct ifnet *ifp,
251 int flags, void *ruleset __unused, struct inpcb *inp);
252 #endif
253 #ifdef INET6
254 static pfil_return_t pf_check6_in(struct mbuf **m, struct ifnet *ifp,
255 int flags, void *ruleset __unused, struct inpcb *inp);
256 static pfil_return_t pf_check6_out(struct mbuf **m, struct ifnet *ifp,
257 int flags, void *ruleset __unused, struct inpcb *inp);
258 #endif
259
260 static void hook_pf_eth(void);
261 static void hook_pf(void);
262 static void dehook_pf_eth(void);
263 static void dehook_pf(void);
264 static int shutdown_pf(void);
265 static int pf_load(void);
266 static void pf_unload(void);
267
268 static struct cdevsw pf_cdevsw = {
269 .d_ioctl = pfioctl,
270 .d_name = PF_NAME,
271 .d_version = D_VERSION,
272 };
273
274 VNET_DEFINE_STATIC(bool, pf_pfil_hooked);
275 #define V_pf_pfil_hooked VNET(pf_pfil_hooked)
276 VNET_DEFINE_STATIC(bool, pf_pfil_eth_hooked);
277 #define V_pf_pfil_eth_hooked VNET(pf_pfil_eth_hooked)
278
279 /*
280 * We need a flag that is neither hooked nor running to know when
281 * the VNET is "valid". We primarily need this to control (global)
282 * external event, e.g., eventhandlers.
283 */
284 VNET_DEFINE(int, pf_vnet_active);
285 #define V_pf_vnet_active VNET(pf_vnet_active)
286
287 int pf_end_threads;
288 struct proc *pf_purge_proc;
289
290 VNET_DEFINE(struct rmlock, pf_rules_lock);
291 VNET_DEFINE_STATIC(struct sx, pf_ioctl_lock);
292 #define V_pf_ioctl_lock VNET(pf_ioctl_lock)
293 struct sx pf_end_lock;
294
295 /* pfsync */
296 VNET_DEFINE(pfsync_state_import_t *, pfsync_state_import_ptr);
297 VNET_DEFINE(pfsync_insert_state_t *, pfsync_insert_state_ptr);
298 VNET_DEFINE(pfsync_update_state_t *, pfsync_update_state_ptr);
299 VNET_DEFINE(pfsync_delete_state_t *, pfsync_delete_state_ptr);
300 VNET_DEFINE(pfsync_clear_states_t *, pfsync_clear_states_ptr);
301 VNET_DEFINE(pfsync_defer_t *, pfsync_defer_ptr);
302 VNET_DEFINE(pflow_export_state_t *, pflow_export_state_ptr);
303 pfsync_detach_ifnet_t *pfsync_detach_ifnet_ptr;
304
305 /* pflog */
306 pflog_packet_t *pflog_packet_ptr = NULL;
307
308 /*
309 * Copy a user-provided string, returning an error if truncation would occur.
310 * Avoid scanning past "sz" bytes in the source string since there's no
311 * guarantee that it's nul-terminated.
312 */
313 static int
pf_user_strcpy(char * dst,const char * src,size_t sz)314 pf_user_strcpy(char *dst, const char *src, size_t sz)
315 {
316 if (strnlen(src, sz) == sz)
317 return (EINVAL);
318 (void)strlcpy(dst, src, sz);
319 return (0);
320 }
321
322 static void
pfattach_vnet(void)323 pfattach_vnet(void)
324 {
325 u_int32_t *my_timeout = V_pf_default_rule.timeout;
326
327 bzero(&V_pf_status, sizeof(V_pf_status));
328
329 pf_initialize();
330 pfr_initialize();
331 pfi_initialize_vnet();
332 pf_normalize_init();
333 pf_syncookies_init();
334
335 V_pf_limits[PF_LIMIT_STATES].limit = PFSTATE_HIWAT;
336 V_pf_limits[PF_LIMIT_SRC_NODES].limit = PFSNODE_HIWAT;
337
338 RB_INIT(&V_pf_anchors);
339 pf_init_kruleset(&pf_main_ruleset);
340
341 pf_init_keth(V_pf_keth);
342
343 /* default rule should never be garbage collected */
344 V_pf_default_rule.entries.tqe_prev = &V_pf_default_rule.entries.tqe_next;
345 V_pf_default_rule.action = V_default_to_drop ? PF_DROP : PF_PASS;
346 V_pf_default_rule.nr = -1;
347 V_pf_default_rule.rtableid = -1;
348
349 pf_counter_u64_init(&V_pf_default_rule.evaluations, M_WAITOK);
350 for (int i = 0; i < 2; i++) {
351 pf_counter_u64_init(&V_pf_default_rule.packets[i], M_WAITOK);
352 pf_counter_u64_init(&V_pf_default_rule.bytes[i], M_WAITOK);
353 }
354 V_pf_default_rule.states_cur = counter_u64_alloc(M_WAITOK);
355 V_pf_default_rule.states_tot = counter_u64_alloc(M_WAITOK);
356 V_pf_default_rule.src_nodes = counter_u64_alloc(M_WAITOK);
357
358 V_pf_default_rule.timestamp = uma_zalloc_pcpu(pf_timestamp_pcpu_zone,
359 M_WAITOK | M_ZERO);
360
361 #ifdef PF_WANT_32_TO_64_COUNTER
362 V_pf_kifmarker = malloc(sizeof(*V_pf_kifmarker), PFI_MTYPE, M_WAITOK | M_ZERO);
363 V_pf_rulemarker = malloc(sizeof(*V_pf_rulemarker), M_PFRULE, M_WAITOK | M_ZERO);
364 PF_RULES_WLOCK();
365 LIST_INSERT_HEAD(&V_pf_allkiflist, V_pf_kifmarker, pfik_allkiflist);
366 LIST_INSERT_HEAD(&V_pf_allrulelist, &V_pf_default_rule, allrulelist);
367 V_pf_allrulecount++;
368 LIST_INSERT_HEAD(&V_pf_allrulelist, V_pf_rulemarker, allrulelist);
369 PF_RULES_WUNLOCK();
370 #endif
371
372 /* initialize default timeouts */
373 my_timeout[PFTM_TCP_FIRST_PACKET] = PFTM_TCP_FIRST_PACKET_VAL;
374 my_timeout[PFTM_TCP_OPENING] = PFTM_TCP_OPENING_VAL;
375 my_timeout[PFTM_TCP_ESTABLISHED] = PFTM_TCP_ESTABLISHED_VAL;
376 my_timeout[PFTM_TCP_CLOSING] = PFTM_TCP_CLOSING_VAL;
377 my_timeout[PFTM_TCP_FIN_WAIT] = PFTM_TCP_FIN_WAIT_VAL;
378 my_timeout[PFTM_TCP_CLOSED] = PFTM_TCP_CLOSED_VAL;
379 my_timeout[PFTM_SCTP_FIRST_PACKET] = PFTM_TCP_FIRST_PACKET_VAL;
380 my_timeout[PFTM_SCTP_OPENING] = PFTM_TCP_OPENING_VAL;
381 my_timeout[PFTM_SCTP_ESTABLISHED] = PFTM_TCP_ESTABLISHED_VAL;
382 my_timeout[PFTM_SCTP_CLOSING] = PFTM_TCP_CLOSING_VAL;
383 my_timeout[PFTM_SCTP_CLOSED] = PFTM_TCP_CLOSED_VAL;
384 my_timeout[PFTM_UDP_FIRST_PACKET] = PFTM_UDP_FIRST_PACKET_VAL;
385 my_timeout[PFTM_UDP_SINGLE] = PFTM_UDP_SINGLE_VAL;
386 my_timeout[PFTM_UDP_MULTIPLE] = PFTM_UDP_MULTIPLE_VAL;
387 my_timeout[PFTM_ICMP_FIRST_PACKET] = PFTM_ICMP_FIRST_PACKET_VAL;
388 my_timeout[PFTM_ICMP_ERROR_REPLY] = PFTM_ICMP_ERROR_REPLY_VAL;
389 my_timeout[PFTM_OTHER_FIRST_PACKET] = PFTM_OTHER_FIRST_PACKET_VAL;
390 my_timeout[PFTM_OTHER_SINGLE] = PFTM_OTHER_SINGLE_VAL;
391 my_timeout[PFTM_OTHER_MULTIPLE] = PFTM_OTHER_MULTIPLE_VAL;
392 my_timeout[PFTM_FRAG] = PFTM_FRAG_VAL;
393 my_timeout[PFTM_INTERVAL] = PFTM_INTERVAL_VAL;
394 my_timeout[PFTM_SRC_NODE] = PFTM_SRC_NODE_VAL;
395 my_timeout[PFTM_TS_DIFF] = PFTM_TS_DIFF_VAL;
396 my_timeout[PFTM_ADAPTIVE_START] = PFSTATE_ADAPT_START;
397 my_timeout[PFTM_ADAPTIVE_END] = PFSTATE_ADAPT_END;
398
399 V_pf_status.debug = PF_DEBUG_URGENT;
400 /*
401 * XXX This is different than in OpenBSD where reassembly is enabled by
402 * defult. In FreeBSD we expect people to still use scrub rules and
403 * switch to the new syntax later. Only when they switch they must
404 * explicitly enable reassemle. We could change the default once the
405 * scrub rule functionality is hopefully removed some day in future.
406 */
407 V_pf_status.reass = 0;
408
409 V_pf_pfil_hooked = false;
410 V_pf_pfil_eth_hooked = false;
411
412 /* XXX do our best to avoid a conflict */
413 V_pf_status.hostid = arc4random();
414
415 for (int i = 0; i < PFRES_MAX; i++)
416 V_pf_status.counters[i] = counter_u64_alloc(M_WAITOK);
417 for (int i = 0; i < KLCNT_MAX; i++)
418 V_pf_status.lcounters[i] = counter_u64_alloc(M_WAITOK);
419 for (int i = 0; i < FCNT_MAX; i++)
420 pf_counter_u64_init(&V_pf_status.fcounters[i], M_WAITOK);
421 for (int i = 0; i < SCNT_MAX; i++)
422 V_pf_status.scounters[i] = counter_u64_alloc(M_WAITOK);
423
424 if (swi_add(&V_pf_swi_ie, "pf send", pf_intr, curvnet, SWI_NET,
425 INTR_MPSAFE, &V_pf_swi_cookie) != 0)
426 /* XXXGL: leaked all above. */
427 return;
428 }
429
430 static struct pf_kpool *
pf_get_kpool(const char * anchor,u_int32_t ticket,u_int8_t rule_action,u_int32_t rule_number,u_int8_t r_last,u_int8_t active,u_int8_t check_ticket)431 pf_get_kpool(const char *anchor, u_int32_t ticket, u_int8_t rule_action,
432 u_int32_t rule_number, u_int8_t r_last, u_int8_t active,
433 u_int8_t check_ticket)
434 {
435 struct pf_kruleset *ruleset;
436 struct pf_krule *rule;
437 int rs_num;
438
439 ruleset = pf_find_kruleset(anchor);
440 if (ruleset == NULL)
441 return (NULL);
442 rs_num = pf_get_ruleset_number(rule_action);
443 if (rs_num >= PF_RULESET_MAX)
444 return (NULL);
445 if (active) {
446 if (check_ticket && ticket !=
447 ruleset->rules[rs_num].active.ticket)
448 return (NULL);
449 if (r_last)
450 rule = TAILQ_LAST(ruleset->rules[rs_num].active.ptr,
451 pf_krulequeue);
452 else
453 rule = TAILQ_FIRST(ruleset->rules[rs_num].active.ptr);
454 } else {
455 if (check_ticket && ticket !=
456 ruleset->rules[rs_num].inactive.ticket)
457 return (NULL);
458 if (r_last)
459 rule = TAILQ_LAST(ruleset->rules[rs_num].inactive.ptr,
460 pf_krulequeue);
461 else
462 rule = TAILQ_FIRST(ruleset->rules[rs_num].inactive.ptr);
463 }
464 if (!r_last) {
465 while ((rule != NULL) && (rule->nr != rule_number))
466 rule = TAILQ_NEXT(rule, entries);
467 }
468 if (rule == NULL)
469 return (NULL);
470
471 return (&rule->rpool);
472 }
473
474 static void
pf_mv_kpool(struct pf_kpalist * poola,struct pf_kpalist * poolb)475 pf_mv_kpool(struct pf_kpalist *poola, struct pf_kpalist *poolb)
476 {
477 struct pf_kpooladdr *mv_pool_pa;
478
479 while ((mv_pool_pa = TAILQ_FIRST(poola)) != NULL) {
480 TAILQ_REMOVE(poola, mv_pool_pa, entries);
481 TAILQ_INSERT_TAIL(poolb, mv_pool_pa, entries);
482 }
483 }
484
485 static void
pf_empty_kpool(struct pf_kpalist * poola)486 pf_empty_kpool(struct pf_kpalist *poola)
487 {
488 struct pf_kpooladdr *pa;
489
490 while ((pa = TAILQ_FIRST(poola)) != NULL) {
491 switch (pa->addr.type) {
492 case PF_ADDR_DYNIFTL:
493 pfi_dynaddr_remove(pa->addr.p.dyn);
494 break;
495 case PF_ADDR_TABLE:
496 /* XXX: this could be unfinished pooladdr on pabuf */
497 if (pa->addr.p.tbl != NULL)
498 pfr_detach_table(pa->addr.p.tbl);
499 break;
500 }
501 if (pa->kif)
502 pfi_kkif_unref(pa->kif);
503 TAILQ_REMOVE(poola, pa, entries);
504 free(pa, M_PFRULE);
505 }
506 }
507
508 static void
pf_unlink_rule_locked(struct pf_krulequeue * rulequeue,struct pf_krule * rule)509 pf_unlink_rule_locked(struct pf_krulequeue *rulequeue, struct pf_krule *rule)
510 {
511
512 PF_RULES_WASSERT();
513 PF_UNLNKDRULES_ASSERT();
514
515 TAILQ_REMOVE(rulequeue, rule, entries);
516
517 rule->rule_ref |= PFRULE_REFS;
518 TAILQ_INSERT_TAIL(&V_pf_unlinked_rules, rule, entries);
519 }
520
521 static void
pf_unlink_rule(struct pf_krulequeue * rulequeue,struct pf_krule * rule)522 pf_unlink_rule(struct pf_krulequeue *rulequeue, struct pf_krule *rule)
523 {
524
525 PF_RULES_WASSERT();
526
527 PF_UNLNKDRULES_LOCK();
528 pf_unlink_rule_locked(rulequeue, rule);
529 PF_UNLNKDRULES_UNLOCK();
530 }
531
532 static void
pf_free_eth_rule(struct pf_keth_rule * rule)533 pf_free_eth_rule(struct pf_keth_rule *rule)
534 {
535 PF_RULES_WASSERT();
536
537 if (rule == NULL)
538 return;
539
540 if (rule->tag)
541 tag_unref(&V_pf_tags, rule->tag);
542 if (rule->match_tag)
543 tag_unref(&V_pf_tags, rule->match_tag);
544 #ifdef ALTQ
545 pf_qid_unref(rule->qid);
546 #endif
547
548 if (rule->bridge_to)
549 pfi_kkif_unref(rule->bridge_to);
550 if (rule->kif)
551 pfi_kkif_unref(rule->kif);
552
553 if (rule->ipsrc.addr.type == PF_ADDR_TABLE)
554 pfr_detach_table(rule->ipsrc.addr.p.tbl);
555 if (rule->ipdst.addr.type == PF_ADDR_TABLE)
556 pfr_detach_table(rule->ipdst.addr.p.tbl);
557
558 counter_u64_free(rule->evaluations);
559 for (int i = 0; i < 2; i++) {
560 counter_u64_free(rule->packets[i]);
561 counter_u64_free(rule->bytes[i]);
562 }
563 uma_zfree_pcpu(pf_timestamp_pcpu_zone, rule->timestamp);
564 pf_keth_anchor_remove(rule);
565
566 free(rule, M_PFRULE);
567 }
568
569 void
pf_free_rule(struct pf_krule * rule)570 pf_free_rule(struct pf_krule *rule)
571 {
572
573 PF_RULES_WASSERT();
574 PF_CONFIG_ASSERT();
575
576 if (rule->tag)
577 tag_unref(&V_pf_tags, rule->tag);
578 if (rule->match_tag)
579 tag_unref(&V_pf_tags, rule->match_tag);
580 #ifdef ALTQ
581 if (rule->pqid != rule->qid)
582 pf_qid_unref(rule->pqid);
583 pf_qid_unref(rule->qid);
584 #endif
585 switch (rule->src.addr.type) {
586 case PF_ADDR_DYNIFTL:
587 pfi_dynaddr_remove(rule->src.addr.p.dyn);
588 break;
589 case PF_ADDR_TABLE:
590 pfr_detach_table(rule->src.addr.p.tbl);
591 break;
592 }
593 switch (rule->dst.addr.type) {
594 case PF_ADDR_DYNIFTL:
595 pfi_dynaddr_remove(rule->dst.addr.p.dyn);
596 break;
597 case PF_ADDR_TABLE:
598 pfr_detach_table(rule->dst.addr.p.tbl);
599 break;
600 }
601 if (rule->overload_tbl)
602 pfr_detach_table(rule->overload_tbl);
603 if (rule->kif)
604 pfi_kkif_unref(rule->kif);
605 if (rule->rcv_kif)
606 pfi_kkif_unref(rule->rcv_kif);
607 pf_kanchor_remove(rule);
608 pf_empty_kpool(&rule->rpool.list);
609
610 pf_krule_free(rule);
611 }
612
613 static void
pf_init_tagset(struct pf_tagset * ts,unsigned int * tunable_size,unsigned int default_size)614 pf_init_tagset(struct pf_tagset *ts, unsigned int *tunable_size,
615 unsigned int default_size)
616 {
617 unsigned int i;
618 unsigned int hashsize;
619
620 if (*tunable_size == 0 || !powerof2(*tunable_size))
621 *tunable_size = default_size;
622
623 hashsize = *tunable_size;
624 ts->namehash = mallocarray(hashsize, sizeof(*ts->namehash), M_PFHASH,
625 M_WAITOK);
626 ts->taghash = mallocarray(hashsize, sizeof(*ts->taghash), M_PFHASH,
627 M_WAITOK);
628 ts->mask = hashsize - 1;
629 ts->seed = arc4random();
630 for (i = 0; i < hashsize; i++) {
631 TAILQ_INIT(&ts->namehash[i]);
632 TAILQ_INIT(&ts->taghash[i]);
633 }
634 BIT_FILL(TAGID_MAX, &ts->avail);
635 }
636
637 static void
pf_cleanup_tagset(struct pf_tagset * ts)638 pf_cleanup_tagset(struct pf_tagset *ts)
639 {
640 unsigned int i;
641 unsigned int hashsize;
642 struct pf_tagname *t, *tmp;
643
644 /*
645 * Only need to clean up one of the hashes as each tag is hashed
646 * into each table.
647 */
648 hashsize = ts->mask + 1;
649 for (i = 0; i < hashsize; i++)
650 TAILQ_FOREACH_SAFE(t, &ts->namehash[i], namehash_entries, tmp)
651 uma_zfree(V_pf_tag_z, t);
652
653 free(ts->namehash, M_PFHASH);
654 free(ts->taghash, M_PFHASH);
655 }
656
657 static uint16_t
tagname2hashindex(const struct pf_tagset * ts,const char * tagname)658 tagname2hashindex(const struct pf_tagset *ts, const char *tagname)
659 {
660 size_t len;
661
662 len = strnlen(tagname, PF_TAG_NAME_SIZE - 1);
663 return (murmur3_32_hash(tagname, len, ts->seed) & ts->mask);
664 }
665
666 static uint16_t
tag2hashindex(const struct pf_tagset * ts,uint16_t tag)667 tag2hashindex(const struct pf_tagset *ts, uint16_t tag)
668 {
669
670 return (tag & ts->mask);
671 }
672
673 static u_int16_t
tagname2tag(struct pf_tagset * ts,const char * tagname)674 tagname2tag(struct pf_tagset *ts, const char *tagname)
675 {
676 struct pf_tagname *tag;
677 u_int32_t index;
678 u_int16_t new_tagid;
679
680 PF_RULES_WASSERT();
681
682 index = tagname2hashindex(ts, tagname);
683 TAILQ_FOREACH(tag, &ts->namehash[index], namehash_entries)
684 if (strcmp(tagname, tag->name) == 0) {
685 tag->ref++;
686 return (tag->tag);
687 }
688
689 /*
690 * new entry
691 *
692 * to avoid fragmentation, we do a linear search from the beginning
693 * and take the first free slot we find.
694 */
695 new_tagid = BIT_FFS(TAGID_MAX, &ts->avail);
696 /*
697 * Tags are 1-based, with valid tags in the range [1..TAGID_MAX].
698 * BIT_FFS() returns a 1-based bit number, with 0 indicating no bits
699 * set. It may also return a bit number greater than TAGID_MAX due
700 * to rounding of the number of bits in the vector up to a multiple
701 * of the vector word size at declaration/allocation time.
702 */
703 if ((new_tagid == 0) || (new_tagid > TAGID_MAX))
704 return (0);
705
706 /* Mark the tag as in use. Bits are 0-based for BIT_CLR() */
707 BIT_CLR(TAGID_MAX, new_tagid - 1, &ts->avail);
708
709 /* allocate and fill new struct pf_tagname */
710 tag = uma_zalloc(V_pf_tag_z, M_NOWAIT);
711 if (tag == NULL)
712 return (0);
713 strlcpy(tag->name, tagname, sizeof(tag->name));
714 tag->tag = new_tagid;
715 tag->ref = 1;
716
717 /* Insert into namehash */
718 TAILQ_INSERT_TAIL(&ts->namehash[index], tag, namehash_entries);
719
720 /* Insert into taghash */
721 index = tag2hashindex(ts, new_tagid);
722 TAILQ_INSERT_TAIL(&ts->taghash[index], tag, taghash_entries);
723
724 return (tag->tag);
725 }
726
727 static void
tag_unref(struct pf_tagset * ts,u_int16_t tag)728 tag_unref(struct pf_tagset *ts, u_int16_t tag)
729 {
730 struct pf_tagname *t;
731 uint16_t index;
732
733 PF_RULES_WASSERT();
734
735 index = tag2hashindex(ts, tag);
736 TAILQ_FOREACH(t, &ts->taghash[index], taghash_entries)
737 if (tag == t->tag) {
738 if (--t->ref == 0) {
739 TAILQ_REMOVE(&ts->taghash[index], t,
740 taghash_entries);
741 index = tagname2hashindex(ts, t->name);
742 TAILQ_REMOVE(&ts->namehash[index], t,
743 namehash_entries);
744 /* Bits are 0-based for BIT_SET() */
745 BIT_SET(TAGID_MAX, tag - 1, &ts->avail);
746 uma_zfree(V_pf_tag_z, t);
747 }
748 break;
749 }
750 }
751
752 static uint16_t
pf_tagname2tag(const char * tagname)753 pf_tagname2tag(const char *tagname)
754 {
755 return (tagname2tag(&V_pf_tags, tagname));
756 }
757
758 static int
pf_begin_eth(uint32_t * ticket,const char * anchor)759 pf_begin_eth(uint32_t *ticket, const char *anchor)
760 {
761 struct pf_keth_rule *rule, *tmp;
762 struct pf_keth_ruleset *rs;
763
764 PF_RULES_WASSERT();
765
766 rs = pf_find_or_create_keth_ruleset(anchor);
767 if (rs == NULL)
768 return (EINVAL);
769
770 /* Purge old inactive rules. */
771 TAILQ_FOREACH_SAFE(rule, rs->inactive.rules, entries,
772 tmp) {
773 TAILQ_REMOVE(rs->inactive.rules, rule,
774 entries);
775 pf_free_eth_rule(rule);
776 }
777
778 *ticket = ++rs->inactive.ticket;
779 rs->inactive.open = 1;
780
781 return (0);
782 }
783
784 static void
pf_rollback_eth_cb(struct epoch_context * ctx)785 pf_rollback_eth_cb(struct epoch_context *ctx)
786 {
787 struct pf_keth_ruleset *rs;
788
789 rs = __containerof(ctx, struct pf_keth_ruleset, epoch_ctx);
790
791 CURVNET_SET(rs->vnet);
792
793 PF_RULES_WLOCK();
794 pf_rollback_eth(rs->inactive.ticket,
795 rs->anchor ? rs->anchor->path : "");
796 PF_RULES_WUNLOCK();
797
798 CURVNET_RESTORE();
799 }
800
801 static int
pf_rollback_eth(uint32_t ticket,const char * anchor)802 pf_rollback_eth(uint32_t ticket, const char *anchor)
803 {
804 struct pf_keth_rule *rule, *tmp;
805 struct pf_keth_ruleset *rs;
806
807 PF_RULES_WASSERT();
808
809 rs = pf_find_keth_ruleset(anchor);
810 if (rs == NULL)
811 return (EINVAL);
812
813 if (!rs->inactive.open ||
814 ticket != rs->inactive.ticket)
815 return (0);
816
817 /* Purge old inactive rules. */
818 TAILQ_FOREACH_SAFE(rule, rs->inactive.rules, entries,
819 tmp) {
820 TAILQ_REMOVE(rs->inactive.rules, rule, entries);
821 pf_free_eth_rule(rule);
822 }
823
824 rs->inactive.open = 0;
825
826 pf_remove_if_empty_keth_ruleset(rs);
827
828 return (0);
829 }
830
831 #define PF_SET_SKIP_STEPS(i) \
832 do { \
833 while (head[i] != cur) { \
834 head[i]->skip[i].ptr = cur; \
835 head[i] = TAILQ_NEXT(head[i], entries); \
836 } \
837 } while (0)
838
839 static void
pf_eth_calc_skip_steps(struct pf_keth_ruleq * rules)840 pf_eth_calc_skip_steps(struct pf_keth_ruleq *rules)
841 {
842 struct pf_keth_rule *cur, *prev, *head[PFE_SKIP_COUNT];
843 int i;
844
845 cur = TAILQ_FIRST(rules);
846 prev = cur;
847 for (i = 0; i < PFE_SKIP_COUNT; ++i)
848 head[i] = cur;
849 while (cur != NULL) {
850 if (cur->kif != prev->kif || cur->ifnot != prev->ifnot)
851 PF_SET_SKIP_STEPS(PFE_SKIP_IFP);
852 if (cur->direction != prev->direction)
853 PF_SET_SKIP_STEPS(PFE_SKIP_DIR);
854 if (cur->proto != prev->proto)
855 PF_SET_SKIP_STEPS(PFE_SKIP_PROTO);
856 if (memcmp(&cur->src, &prev->src, sizeof(cur->src)) != 0)
857 PF_SET_SKIP_STEPS(PFE_SKIP_SRC_ADDR);
858 if (memcmp(&cur->dst, &prev->dst, sizeof(cur->dst)) != 0)
859 PF_SET_SKIP_STEPS(PFE_SKIP_DST_ADDR);
860 if (cur->ipsrc.neg != prev->ipsrc.neg ||
861 pf_addr_wrap_neq(&cur->ipsrc.addr, &prev->ipsrc.addr))
862 PF_SET_SKIP_STEPS(PFE_SKIP_SRC_IP_ADDR);
863 if (cur->ipdst.neg != prev->ipdst.neg ||
864 pf_addr_wrap_neq(&cur->ipdst.addr, &prev->ipdst.addr))
865 PF_SET_SKIP_STEPS(PFE_SKIP_DST_IP_ADDR);
866
867 prev = cur;
868 cur = TAILQ_NEXT(cur, entries);
869 }
870 for (i = 0; i < PFE_SKIP_COUNT; ++i)
871 PF_SET_SKIP_STEPS(i);
872 }
873
874 static int
pf_commit_eth(uint32_t ticket,const char * anchor)875 pf_commit_eth(uint32_t ticket, const char *anchor)
876 {
877 struct pf_keth_ruleq *rules;
878 struct pf_keth_ruleset *rs;
879
880 rs = pf_find_keth_ruleset(anchor);
881 if (rs == NULL) {
882 return (EINVAL);
883 }
884
885 if (!rs->inactive.open ||
886 ticket != rs->inactive.ticket)
887 return (EBUSY);
888
889 PF_RULES_WASSERT();
890
891 pf_eth_calc_skip_steps(rs->inactive.rules);
892
893 rules = rs->active.rules;
894 ck_pr_store_ptr(&rs->active.rules, rs->inactive.rules);
895 rs->inactive.rules = rules;
896 rs->inactive.ticket = rs->active.ticket;
897
898 /* Clean up inactive rules (i.e. previously active rules), only when
899 * we're sure they're no longer used. */
900 NET_EPOCH_CALL(pf_rollback_eth_cb, &rs->epoch_ctx);
901
902 return (0);
903 }
904
905 #ifdef ALTQ
906 static uint16_t
pf_qname2qid(const char * qname)907 pf_qname2qid(const char *qname)
908 {
909 return (tagname2tag(&V_pf_qids, qname));
910 }
911
912 static void
pf_qid_unref(uint16_t qid)913 pf_qid_unref(uint16_t qid)
914 {
915 tag_unref(&V_pf_qids, qid);
916 }
917
918 static int
pf_begin_altq(u_int32_t * ticket)919 pf_begin_altq(u_int32_t *ticket)
920 {
921 struct pf_altq *altq, *tmp;
922 int error = 0;
923
924 PF_RULES_WASSERT();
925
926 /* Purge the old altq lists */
927 TAILQ_FOREACH_SAFE(altq, V_pf_altq_ifs_inactive, entries, tmp) {
928 if ((altq->local_flags & PFALTQ_FLAG_IF_REMOVED) == 0) {
929 /* detach and destroy the discipline */
930 error = altq_remove(altq);
931 }
932 free(altq, M_PFALTQ);
933 }
934 TAILQ_INIT(V_pf_altq_ifs_inactive);
935 TAILQ_FOREACH_SAFE(altq, V_pf_altqs_inactive, entries, tmp) {
936 pf_qid_unref(altq->qid);
937 free(altq, M_PFALTQ);
938 }
939 TAILQ_INIT(V_pf_altqs_inactive);
940 if (error)
941 return (error);
942 *ticket = ++V_ticket_altqs_inactive;
943 V_altqs_inactive_open = 1;
944 return (0);
945 }
946
947 static int
pf_rollback_altq(u_int32_t ticket)948 pf_rollback_altq(u_int32_t ticket)
949 {
950 struct pf_altq *altq, *tmp;
951 int error = 0;
952
953 PF_RULES_WASSERT();
954
955 if (!V_altqs_inactive_open || ticket != V_ticket_altqs_inactive)
956 return (0);
957 /* Purge the old altq lists */
958 TAILQ_FOREACH_SAFE(altq, V_pf_altq_ifs_inactive, entries, tmp) {
959 if ((altq->local_flags & PFALTQ_FLAG_IF_REMOVED) == 0) {
960 /* detach and destroy the discipline */
961 error = altq_remove(altq);
962 }
963 free(altq, M_PFALTQ);
964 }
965 TAILQ_INIT(V_pf_altq_ifs_inactive);
966 TAILQ_FOREACH_SAFE(altq, V_pf_altqs_inactive, entries, tmp) {
967 pf_qid_unref(altq->qid);
968 free(altq, M_PFALTQ);
969 }
970 TAILQ_INIT(V_pf_altqs_inactive);
971 V_altqs_inactive_open = 0;
972 return (error);
973 }
974
975 static int
pf_commit_altq(u_int32_t ticket)976 pf_commit_altq(u_int32_t ticket)
977 {
978 struct pf_altqqueue *old_altqs, *old_altq_ifs;
979 struct pf_altq *altq, *tmp;
980 int err, error = 0;
981
982 PF_RULES_WASSERT();
983
984 if (!V_altqs_inactive_open || ticket != V_ticket_altqs_inactive)
985 return (EBUSY);
986
987 /* swap altqs, keep the old. */
988 old_altqs = V_pf_altqs_active;
989 old_altq_ifs = V_pf_altq_ifs_active;
990 V_pf_altqs_active = V_pf_altqs_inactive;
991 V_pf_altq_ifs_active = V_pf_altq_ifs_inactive;
992 V_pf_altqs_inactive = old_altqs;
993 V_pf_altq_ifs_inactive = old_altq_ifs;
994 V_ticket_altqs_active = V_ticket_altqs_inactive;
995
996 /* Attach new disciplines */
997 TAILQ_FOREACH(altq, V_pf_altq_ifs_active, entries) {
998 if ((altq->local_flags & PFALTQ_FLAG_IF_REMOVED) == 0) {
999 /* attach the discipline */
1000 error = altq_pfattach(altq);
1001 if (error == 0 && V_pf_altq_running)
1002 error = pf_enable_altq(altq);
1003 if (error != 0)
1004 return (error);
1005 }
1006 }
1007
1008 /* Purge the old altq lists */
1009 TAILQ_FOREACH_SAFE(altq, V_pf_altq_ifs_inactive, entries, tmp) {
1010 if ((altq->local_flags & PFALTQ_FLAG_IF_REMOVED) == 0) {
1011 /* detach and destroy the discipline */
1012 if (V_pf_altq_running)
1013 error = pf_disable_altq(altq);
1014 err = altq_pfdetach(altq);
1015 if (err != 0 && error == 0)
1016 error = err;
1017 err = altq_remove(altq);
1018 if (err != 0 && error == 0)
1019 error = err;
1020 }
1021 free(altq, M_PFALTQ);
1022 }
1023 TAILQ_INIT(V_pf_altq_ifs_inactive);
1024 TAILQ_FOREACH_SAFE(altq, V_pf_altqs_inactive, entries, tmp) {
1025 pf_qid_unref(altq->qid);
1026 free(altq, M_PFALTQ);
1027 }
1028 TAILQ_INIT(V_pf_altqs_inactive);
1029
1030 V_altqs_inactive_open = 0;
1031 return (error);
1032 }
1033
1034 static int
pf_enable_altq(struct pf_altq * altq)1035 pf_enable_altq(struct pf_altq *altq)
1036 {
1037 struct ifnet *ifp;
1038 struct tb_profile tb;
1039 int error = 0;
1040
1041 if ((ifp = ifunit(altq->ifname)) == NULL)
1042 return (EINVAL);
1043
1044 if (ifp->if_snd.altq_type != ALTQT_NONE)
1045 error = altq_enable(&ifp->if_snd);
1046
1047 /* set tokenbucket regulator */
1048 if (error == 0 && ifp != NULL && ALTQ_IS_ENABLED(&ifp->if_snd)) {
1049 tb.rate = altq->ifbandwidth;
1050 tb.depth = altq->tbrsize;
1051 error = tbr_set(&ifp->if_snd, &tb);
1052 }
1053
1054 return (error);
1055 }
1056
1057 static int
pf_disable_altq(struct pf_altq * altq)1058 pf_disable_altq(struct pf_altq *altq)
1059 {
1060 struct ifnet *ifp;
1061 struct tb_profile tb;
1062 int error;
1063
1064 if ((ifp = ifunit(altq->ifname)) == NULL)
1065 return (EINVAL);
1066
1067 /*
1068 * when the discipline is no longer referenced, it was overridden
1069 * by a new one. if so, just return.
1070 */
1071 if (altq->altq_disc != ifp->if_snd.altq_disc)
1072 return (0);
1073
1074 error = altq_disable(&ifp->if_snd);
1075
1076 if (error == 0) {
1077 /* clear tokenbucket regulator */
1078 tb.rate = 0;
1079 error = tbr_set(&ifp->if_snd, &tb);
1080 }
1081
1082 return (error);
1083 }
1084
1085 static int
pf_altq_ifnet_event_add(struct ifnet * ifp,int remove,u_int32_t ticket,struct pf_altq * altq)1086 pf_altq_ifnet_event_add(struct ifnet *ifp, int remove, u_int32_t ticket,
1087 struct pf_altq *altq)
1088 {
1089 struct ifnet *ifp1;
1090 int error = 0;
1091
1092 /* Deactivate the interface in question */
1093 altq->local_flags &= ~PFALTQ_FLAG_IF_REMOVED;
1094 if ((ifp1 = ifunit(altq->ifname)) == NULL ||
1095 (remove && ifp1 == ifp)) {
1096 altq->local_flags |= PFALTQ_FLAG_IF_REMOVED;
1097 } else {
1098 error = altq_add(ifp1, altq);
1099
1100 if (ticket != V_ticket_altqs_inactive)
1101 error = EBUSY;
1102
1103 if (error)
1104 free(altq, M_PFALTQ);
1105 }
1106
1107 return (error);
1108 }
1109
1110 void
pf_altq_ifnet_event(struct ifnet * ifp,int remove)1111 pf_altq_ifnet_event(struct ifnet *ifp, int remove)
1112 {
1113 struct pf_altq *a1, *a2, *a3;
1114 u_int32_t ticket;
1115 int error = 0;
1116
1117 /*
1118 * No need to re-evaluate the configuration for events on interfaces
1119 * that do not support ALTQ, as it's not possible for such
1120 * interfaces to be part of the configuration.
1121 */
1122 if (!ALTQ_IS_READY(&ifp->if_snd))
1123 return;
1124
1125 /* Interrupt userland queue modifications */
1126 if (V_altqs_inactive_open)
1127 pf_rollback_altq(V_ticket_altqs_inactive);
1128
1129 /* Start new altq ruleset */
1130 if (pf_begin_altq(&ticket))
1131 return;
1132
1133 /* Copy the current active set */
1134 TAILQ_FOREACH(a1, V_pf_altq_ifs_active, entries) {
1135 a2 = malloc(sizeof(*a2), M_PFALTQ, M_NOWAIT);
1136 if (a2 == NULL) {
1137 error = ENOMEM;
1138 break;
1139 }
1140 bcopy(a1, a2, sizeof(struct pf_altq));
1141
1142 error = pf_altq_ifnet_event_add(ifp, remove, ticket, a2);
1143 if (error)
1144 break;
1145
1146 TAILQ_INSERT_TAIL(V_pf_altq_ifs_inactive, a2, entries);
1147 }
1148 if (error)
1149 goto out;
1150 TAILQ_FOREACH(a1, V_pf_altqs_active, entries) {
1151 a2 = malloc(sizeof(*a2), M_PFALTQ, M_NOWAIT);
1152 if (a2 == NULL) {
1153 error = ENOMEM;
1154 break;
1155 }
1156 bcopy(a1, a2, sizeof(struct pf_altq));
1157
1158 if ((a2->qid = pf_qname2qid(a2->qname)) == 0) {
1159 error = EBUSY;
1160 free(a2, M_PFALTQ);
1161 break;
1162 }
1163 a2->altq_disc = NULL;
1164 TAILQ_FOREACH(a3, V_pf_altq_ifs_inactive, entries) {
1165 if (strncmp(a3->ifname, a2->ifname,
1166 IFNAMSIZ) == 0) {
1167 a2->altq_disc = a3->altq_disc;
1168 break;
1169 }
1170 }
1171 error = pf_altq_ifnet_event_add(ifp, remove, ticket, a2);
1172 if (error)
1173 break;
1174
1175 TAILQ_INSERT_TAIL(V_pf_altqs_inactive, a2, entries);
1176 }
1177
1178 out:
1179 if (error != 0)
1180 pf_rollback_altq(ticket);
1181 else
1182 pf_commit_altq(ticket);
1183 }
1184 #endif /* ALTQ */
1185
1186 static struct pf_krule_global *
pf_rule_tree_alloc(int flags)1187 pf_rule_tree_alloc(int flags)
1188 {
1189 struct pf_krule_global *tree;
1190
1191 tree = malloc(sizeof(struct pf_krule_global), M_TEMP, flags);
1192 if (tree == NULL)
1193 return (NULL);
1194 RB_INIT(tree);
1195 return (tree);
1196 }
1197
1198 static void
pf_rule_tree_free(struct pf_krule_global * tree)1199 pf_rule_tree_free(struct pf_krule_global *tree)
1200 {
1201
1202 free(tree, M_TEMP);
1203 }
1204
1205 static int
pf_begin_rules(u_int32_t * ticket,int rs_num,const char * anchor)1206 pf_begin_rules(u_int32_t *ticket, int rs_num, const char *anchor)
1207 {
1208 struct pf_krule_global *tree;
1209 struct pf_kruleset *rs;
1210 struct pf_krule *rule;
1211
1212 PF_RULES_WASSERT();
1213
1214 if (rs_num < 0 || rs_num >= PF_RULESET_MAX)
1215 return (EINVAL);
1216 tree = pf_rule_tree_alloc(M_NOWAIT);
1217 if (tree == NULL)
1218 return (ENOMEM);
1219 rs = pf_find_or_create_kruleset(anchor);
1220 if (rs == NULL) {
1221 free(tree, M_TEMP);
1222 return (EINVAL);
1223 }
1224 pf_rule_tree_free(rs->rules[rs_num].inactive.tree);
1225 rs->rules[rs_num].inactive.tree = tree;
1226
1227 while ((rule = TAILQ_FIRST(rs->rules[rs_num].inactive.ptr)) != NULL) {
1228 pf_unlink_rule(rs->rules[rs_num].inactive.ptr, rule);
1229 rs->rules[rs_num].inactive.rcount--;
1230 }
1231 *ticket = ++rs->rules[rs_num].inactive.ticket;
1232 rs->rules[rs_num].inactive.open = 1;
1233 return (0);
1234 }
1235
1236 static int
pf_rollback_rules(u_int32_t ticket,int rs_num,char * anchor)1237 pf_rollback_rules(u_int32_t ticket, int rs_num, char *anchor)
1238 {
1239 struct pf_kruleset *rs;
1240 struct pf_krule *rule;
1241
1242 PF_RULES_WASSERT();
1243
1244 if (rs_num < 0 || rs_num >= PF_RULESET_MAX)
1245 return (EINVAL);
1246 rs = pf_find_kruleset(anchor);
1247 if (rs == NULL || !rs->rules[rs_num].inactive.open ||
1248 rs->rules[rs_num].inactive.ticket != ticket)
1249 return (0);
1250 while ((rule = TAILQ_FIRST(rs->rules[rs_num].inactive.ptr)) != NULL) {
1251 pf_unlink_rule(rs->rules[rs_num].inactive.ptr, rule);
1252 rs->rules[rs_num].inactive.rcount--;
1253 }
1254 rs->rules[rs_num].inactive.open = 0;
1255 return (0);
1256 }
1257
1258 #define PF_MD5_UPD(st, elm) \
1259 MD5Update(ctx, (u_int8_t *) &(st)->elm, sizeof((st)->elm))
1260
1261 #define PF_MD5_UPD_STR(st, elm) \
1262 MD5Update(ctx, (u_int8_t *) (st)->elm, strlen((st)->elm))
1263
1264 #define PF_MD5_UPD_HTONL(st, elm, stor) do { \
1265 (stor) = htonl((st)->elm); \
1266 MD5Update(ctx, (u_int8_t *) &(stor), sizeof(u_int32_t));\
1267 } while (0)
1268
1269 #define PF_MD5_UPD_HTONS(st, elm, stor) do { \
1270 (stor) = htons((st)->elm); \
1271 MD5Update(ctx, (u_int8_t *) &(stor), sizeof(u_int16_t));\
1272 } while (0)
1273
1274 static void
pf_hash_rule_addr(MD5_CTX * ctx,struct pf_rule_addr * pfr)1275 pf_hash_rule_addr(MD5_CTX *ctx, struct pf_rule_addr *pfr)
1276 {
1277 PF_MD5_UPD(pfr, addr.type);
1278 switch (pfr->addr.type) {
1279 case PF_ADDR_DYNIFTL:
1280 PF_MD5_UPD(pfr, addr.v.ifname);
1281 PF_MD5_UPD(pfr, addr.iflags);
1282 break;
1283 case PF_ADDR_TABLE:
1284 PF_MD5_UPD(pfr, addr.v.tblname);
1285 break;
1286 case PF_ADDR_ADDRMASK:
1287 /* XXX ignore af? */
1288 PF_MD5_UPD(pfr, addr.v.a.addr.addr32);
1289 PF_MD5_UPD(pfr, addr.v.a.mask.addr32);
1290 break;
1291 }
1292
1293 PF_MD5_UPD(pfr, port[0]);
1294 PF_MD5_UPD(pfr, port[1]);
1295 PF_MD5_UPD(pfr, neg);
1296 PF_MD5_UPD(pfr, port_op);
1297 }
1298
1299 static void
pf_hash_rule_rolling(MD5_CTX * ctx,struct pf_krule * rule)1300 pf_hash_rule_rolling(MD5_CTX *ctx, struct pf_krule *rule)
1301 {
1302 u_int16_t x;
1303 u_int32_t y;
1304
1305 pf_hash_rule_addr(ctx, &rule->src);
1306 pf_hash_rule_addr(ctx, &rule->dst);
1307 for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++)
1308 PF_MD5_UPD_STR(rule, label[i]);
1309 PF_MD5_UPD_STR(rule, ifname);
1310 PF_MD5_UPD_STR(rule, rcv_ifname);
1311 PF_MD5_UPD_STR(rule, match_tagname);
1312 PF_MD5_UPD_HTONS(rule, match_tag, x); /* dup? */
1313 PF_MD5_UPD_HTONL(rule, os_fingerprint, y);
1314 PF_MD5_UPD_HTONL(rule, prob, y);
1315 PF_MD5_UPD_HTONL(rule, uid.uid[0], y);
1316 PF_MD5_UPD_HTONL(rule, uid.uid[1], y);
1317 PF_MD5_UPD(rule, uid.op);
1318 PF_MD5_UPD_HTONL(rule, gid.gid[0], y);
1319 PF_MD5_UPD_HTONL(rule, gid.gid[1], y);
1320 PF_MD5_UPD(rule, gid.op);
1321 PF_MD5_UPD_HTONL(rule, rule_flag, y);
1322 PF_MD5_UPD(rule, action);
1323 PF_MD5_UPD(rule, direction);
1324 PF_MD5_UPD(rule, af);
1325 PF_MD5_UPD(rule, quick);
1326 PF_MD5_UPD(rule, ifnot);
1327 PF_MD5_UPD(rule, match_tag_not);
1328 PF_MD5_UPD(rule, natpass);
1329 PF_MD5_UPD(rule, keep_state);
1330 PF_MD5_UPD(rule, proto);
1331 PF_MD5_UPD(rule, type);
1332 PF_MD5_UPD(rule, code);
1333 PF_MD5_UPD(rule, flags);
1334 PF_MD5_UPD(rule, flagset);
1335 PF_MD5_UPD(rule, allow_opts);
1336 PF_MD5_UPD(rule, rt);
1337 PF_MD5_UPD(rule, tos);
1338 PF_MD5_UPD(rule, scrub_flags);
1339 PF_MD5_UPD(rule, min_ttl);
1340 PF_MD5_UPD(rule, set_tos);
1341 if (rule->anchor != NULL)
1342 PF_MD5_UPD_STR(rule, anchor->path);
1343 }
1344
1345 static void
pf_hash_rule(struct pf_krule * rule)1346 pf_hash_rule(struct pf_krule *rule)
1347 {
1348 MD5_CTX ctx;
1349
1350 MD5Init(&ctx);
1351 pf_hash_rule_rolling(&ctx, rule);
1352 MD5Final(rule->md5sum, &ctx);
1353 }
1354
1355 static int
pf_krule_compare(struct pf_krule * a,struct pf_krule * b)1356 pf_krule_compare(struct pf_krule *a, struct pf_krule *b)
1357 {
1358
1359 return (memcmp(a->md5sum, b->md5sum, PF_MD5_DIGEST_LENGTH));
1360 }
1361
1362 static int
pf_commit_rules(u_int32_t ticket,int rs_num,char * anchor)1363 pf_commit_rules(u_int32_t ticket, int rs_num, char *anchor)
1364 {
1365 struct pf_kruleset *rs;
1366 struct pf_krule *rule, **old_array, *old_rule;
1367 struct pf_krulequeue *old_rules;
1368 struct pf_krule_global *old_tree;
1369 int error;
1370 u_int32_t old_rcount;
1371
1372 PF_RULES_WASSERT();
1373
1374 if (rs_num < 0 || rs_num >= PF_RULESET_MAX)
1375 return (EINVAL);
1376 rs = pf_find_kruleset(anchor);
1377 if (rs == NULL || !rs->rules[rs_num].inactive.open ||
1378 ticket != rs->rules[rs_num].inactive.ticket)
1379 return (EBUSY);
1380
1381 /* Calculate checksum for the main ruleset */
1382 if (rs == &pf_main_ruleset) {
1383 error = pf_setup_pfsync_matching(rs);
1384 if (error != 0)
1385 return (error);
1386 }
1387
1388 /* Swap rules, keep the old. */
1389 old_rules = rs->rules[rs_num].active.ptr;
1390 old_rcount = rs->rules[rs_num].active.rcount;
1391 old_array = rs->rules[rs_num].active.ptr_array;
1392 old_tree = rs->rules[rs_num].active.tree;
1393
1394 rs->rules[rs_num].active.ptr =
1395 rs->rules[rs_num].inactive.ptr;
1396 rs->rules[rs_num].active.ptr_array =
1397 rs->rules[rs_num].inactive.ptr_array;
1398 rs->rules[rs_num].active.tree =
1399 rs->rules[rs_num].inactive.tree;
1400 rs->rules[rs_num].active.rcount =
1401 rs->rules[rs_num].inactive.rcount;
1402
1403 /* Attempt to preserve counter information. */
1404 if (V_pf_status.keep_counters && old_tree != NULL) {
1405 TAILQ_FOREACH(rule, rs->rules[rs_num].active.ptr,
1406 entries) {
1407 old_rule = RB_FIND(pf_krule_global, old_tree, rule);
1408 if (old_rule == NULL) {
1409 continue;
1410 }
1411 pf_counter_u64_critical_enter();
1412 pf_counter_u64_rollup_protected(&rule->evaluations,
1413 pf_counter_u64_fetch(&old_rule->evaluations));
1414 pf_counter_u64_rollup_protected(&rule->packets[0],
1415 pf_counter_u64_fetch(&old_rule->packets[0]));
1416 pf_counter_u64_rollup_protected(&rule->packets[1],
1417 pf_counter_u64_fetch(&old_rule->packets[1]));
1418 pf_counter_u64_rollup_protected(&rule->bytes[0],
1419 pf_counter_u64_fetch(&old_rule->bytes[0]));
1420 pf_counter_u64_rollup_protected(&rule->bytes[1],
1421 pf_counter_u64_fetch(&old_rule->bytes[1]));
1422 pf_counter_u64_critical_exit();
1423 }
1424 }
1425
1426 rs->rules[rs_num].inactive.ptr = old_rules;
1427 rs->rules[rs_num].inactive.ptr_array = old_array;
1428 rs->rules[rs_num].inactive.tree = NULL; /* important for pf_ioctl_addrule */
1429 rs->rules[rs_num].inactive.rcount = old_rcount;
1430
1431 rs->rules[rs_num].active.ticket =
1432 rs->rules[rs_num].inactive.ticket;
1433 pf_calc_skip_steps(rs->rules[rs_num].active.ptr);
1434
1435 /* Purge the old rule list. */
1436 PF_UNLNKDRULES_LOCK();
1437 while ((rule = TAILQ_FIRST(old_rules)) != NULL)
1438 pf_unlink_rule_locked(old_rules, rule);
1439 PF_UNLNKDRULES_UNLOCK();
1440 if (rs->rules[rs_num].inactive.ptr_array)
1441 free(rs->rules[rs_num].inactive.ptr_array, M_TEMP);
1442 rs->rules[rs_num].inactive.ptr_array = NULL;
1443 rs->rules[rs_num].inactive.rcount = 0;
1444 rs->rules[rs_num].inactive.open = 0;
1445 pf_remove_if_empty_kruleset(rs);
1446 free(old_tree, M_TEMP);
1447
1448 return (0);
1449 }
1450
1451 static int
pf_setup_pfsync_matching(struct pf_kruleset * rs)1452 pf_setup_pfsync_matching(struct pf_kruleset *rs)
1453 {
1454 MD5_CTX ctx;
1455 struct pf_krule *rule;
1456 int rs_cnt;
1457 u_int8_t digest[PF_MD5_DIGEST_LENGTH];
1458
1459 MD5Init(&ctx);
1460 for (rs_cnt = 0; rs_cnt < PF_RULESET_MAX; rs_cnt++) {
1461 /* XXX PF_RULESET_SCRUB as well? */
1462 if (rs_cnt == PF_RULESET_SCRUB)
1463 continue;
1464
1465 if (rs->rules[rs_cnt].inactive.ptr_array)
1466 free(rs->rules[rs_cnt].inactive.ptr_array, M_TEMP);
1467 rs->rules[rs_cnt].inactive.ptr_array = NULL;
1468
1469 if (rs->rules[rs_cnt].inactive.rcount) {
1470 rs->rules[rs_cnt].inactive.ptr_array =
1471 mallocarray(rs->rules[rs_cnt].inactive.rcount,
1472 sizeof(struct pf_rule **),
1473 M_TEMP, M_NOWAIT);
1474
1475 if (!rs->rules[rs_cnt].inactive.ptr_array)
1476 return (ENOMEM);
1477 }
1478
1479 TAILQ_FOREACH(rule, rs->rules[rs_cnt].inactive.ptr,
1480 entries) {
1481 pf_hash_rule_rolling(&ctx, rule);
1482 (rs->rules[rs_cnt].inactive.ptr_array)[rule->nr] = rule;
1483 }
1484 }
1485
1486 MD5Final(digest, &ctx);
1487 memcpy(V_pf_status.pf_chksum, digest, sizeof(V_pf_status.pf_chksum));
1488 return (0);
1489 }
1490
1491 static int
pf_eth_addr_setup(struct pf_keth_ruleset * ruleset,struct pf_addr_wrap * addr)1492 pf_eth_addr_setup(struct pf_keth_ruleset *ruleset, struct pf_addr_wrap *addr)
1493 {
1494 int error = 0;
1495
1496 switch (addr->type) {
1497 case PF_ADDR_TABLE:
1498 addr->p.tbl = pfr_eth_attach_table(ruleset, addr->v.tblname);
1499 if (addr->p.tbl == NULL)
1500 error = ENOMEM;
1501 break;
1502 default:
1503 error = EINVAL;
1504 }
1505
1506 return (error);
1507 }
1508
1509 static int
pf_addr_setup(struct pf_kruleset * ruleset,struct pf_addr_wrap * addr,sa_family_t af)1510 pf_addr_setup(struct pf_kruleset *ruleset, struct pf_addr_wrap *addr,
1511 sa_family_t af)
1512 {
1513 int error = 0;
1514
1515 switch (addr->type) {
1516 case PF_ADDR_TABLE:
1517 addr->p.tbl = pfr_attach_table(ruleset, addr->v.tblname);
1518 if (addr->p.tbl == NULL)
1519 error = ENOMEM;
1520 break;
1521 case PF_ADDR_DYNIFTL:
1522 error = pfi_dynaddr_setup(addr, af);
1523 break;
1524 }
1525
1526 return (error);
1527 }
1528
1529 void
pf_addr_copyout(struct pf_addr_wrap * addr)1530 pf_addr_copyout(struct pf_addr_wrap *addr)
1531 {
1532
1533 switch (addr->type) {
1534 case PF_ADDR_DYNIFTL:
1535 pfi_dynaddr_copyout(addr);
1536 break;
1537 case PF_ADDR_TABLE:
1538 pf_tbladdr_copyout(addr);
1539 break;
1540 }
1541 }
1542
1543 static void
pf_src_node_copy(const struct pf_ksrc_node * in,struct pf_src_node * out)1544 pf_src_node_copy(const struct pf_ksrc_node *in, struct pf_src_node *out)
1545 {
1546 int secs = time_uptime, diff;
1547
1548 bzero(out, sizeof(struct pf_src_node));
1549
1550 bcopy(&in->addr, &out->addr, sizeof(struct pf_addr));
1551 bcopy(&in->raddr, &out->raddr, sizeof(struct pf_addr));
1552
1553 if (in->rule != NULL)
1554 out->rule.nr = in->rule->nr;
1555
1556 for (int i = 0; i < 2; i++) {
1557 out->bytes[i] = counter_u64_fetch(in->bytes[i]);
1558 out->packets[i] = counter_u64_fetch(in->packets[i]);
1559 }
1560
1561 out->states = in->states;
1562 out->conn = in->conn;
1563 out->af = in->af;
1564 out->ruletype = in->ruletype;
1565
1566 out->creation = secs - in->creation;
1567 if (out->expire > secs)
1568 out->expire -= secs;
1569 else
1570 out->expire = 0;
1571
1572 /* Adjust the connection rate estimate. */
1573 diff = secs - in->conn_rate.last;
1574 if (diff >= in->conn_rate.seconds)
1575 out->conn_rate.count = 0;
1576 else
1577 out->conn_rate.count -=
1578 in->conn_rate.count * diff /
1579 in->conn_rate.seconds;
1580 }
1581
1582 #ifdef ALTQ
1583 /*
1584 * Handle export of struct pf_kaltq to user binaries that may be using any
1585 * version of struct pf_altq.
1586 */
1587 static int
pf_export_kaltq(struct pf_altq * q,struct pfioc_altq_v1 * pa,size_t ioc_size)1588 pf_export_kaltq(struct pf_altq *q, struct pfioc_altq_v1 *pa, size_t ioc_size)
1589 {
1590 u_int32_t version;
1591
1592 if (ioc_size == sizeof(struct pfioc_altq_v0))
1593 version = 0;
1594 else
1595 version = pa->version;
1596
1597 if (version > PFIOC_ALTQ_VERSION)
1598 return (EINVAL);
1599
1600 #define ASSIGN(x) exported_q->x = q->x
1601 #define COPY(x) \
1602 bcopy(&q->x, &exported_q->x, min(sizeof(q->x), sizeof(exported_q->x)))
1603 #define SATU16(x) (u_int32_t)uqmin((x), USHRT_MAX)
1604 #define SATU32(x) (u_int32_t)uqmin((x), UINT_MAX)
1605
1606 switch (version) {
1607 case 0: {
1608 struct pf_altq_v0 *exported_q =
1609 &((struct pfioc_altq_v0 *)pa)->altq;
1610
1611 COPY(ifname);
1612
1613 ASSIGN(scheduler);
1614 ASSIGN(tbrsize);
1615 exported_q->tbrsize = SATU16(q->tbrsize);
1616 exported_q->ifbandwidth = SATU32(q->ifbandwidth);
1617
1618 COPY(qname);
1619 COPY(parent);
1620 ASSIGN(parent_qid);
1621 exported_q->bandwidth = SATU32(q->bandwidth);
1622 ASSIGN(priority);
1623 ASSIGN(local_flags);
1624
1625 ASSIGN(qlimit);
1626 ASSIGN(flags);
1627
1628 if (q->scheduler == ALTQT_HFSC) {
1629 #define ASSIGN_OPT(x) exported_q->pq_u.hfsc_opts.x = q->pq_u.hfsc_opts.x
1630 #define ASSIGN_OPT_SATU32(x) exported_q->pq_u.hfsc_opts.x = \
1631 SATU32(q->pq_u.hfsc_opts.x)
1632
1633 ASSIGN_OPT_SATU32(rtsc_m1);
1634 ASSIGN_OPT(rtsc_d);
1635 ASSIGN_OPT_SATU32(rtsc_m2);
1636
1637 ASSIGN_OPT_SATU32(lssc_m1);
1638 ASSIGN_OPT(lssc_d);
1639 ASSIGN_OPT_SATU32(lssc_m2);
1640
1641 ASSIGN_OPT_SATU32(ulsc_m1);
1642 ASSIGN_OPT(ulsc_d);
1643 ASSIGN_OPT_SATU32(ulsc_m2);
1644
1645 ASSIGN_OPT(flags);
1646
1647 #undef ASSIGN_OPT
1648 #undef ASSIGN_OPT_SATU32
1649 } else
1650 COPY(pq_u);
1651
1652 ASSIGN(qid);
1653 break;
1654 }
1655 case 1: {
1656 struct pf_altq_v1 *exported_q =
1657 &((struct pfioc_altq_v1 *)pa)->altq;
1658
1659 COPY(ifname);
1660
1661 ASSIGN(scheduler);
1662 ASSIGN(tbrsize);
1663 ASSIGN(ifbandwidth);
1664
1665 COPY(qname);
1666 COPY(parent);
1667 ASSIGN(parent_qid);
1668 ASSIGN(bandwidth);
1669 ASSIGN(priority);
1670 ASSIGN(local_flags);
1671
1672 ASSIGN(qlimit);
1673 ASSIGN(flags);
1674 COPY(pq_u);
1675
1676 ASSIGN(qid);
1677 break;
1678 }
1679 default:
1680 panic("%s: unhandled struct pfioc_altq version", __func__);
1681 break;
1682 }
1683
1684 #undef ASSIGN
1685 #undef COPY
1686 #undef SATU16
1687 #undef SATU32
1688
1689 return (0);
1690 }
1691
1692 /*
1693 * Handle import to struct pf_kaltq of struct pf_altq from user binaries
1694 * that may be using any version of it.
1695 */
1696 static int
pf_import_kaltq(struct pfioc_altq_v1 * pa,struct pf_altq * q,size_t ioc_size)1697 pf_import_kaltq(struct pfioc_altq_v1 *pa, struct pf_altq *q, size_t ioc_size)
1698 {
1699 u_int32_t version;
1700
1701 if (ioc_size == sizeof(struct pfioc_altq_v0))
1702 version = 0;
1703 else
1704 version = pa->version;
1705
1706 if (version > PFIOC_ALTQ_VERSION)
1707 return (EINVAL);
1708
1709 #define ASSIGN(x) q->x = imported_q->x
1710 #define COPY(x) \
1711 bcopy(&imported_q->x, &q->x, min(sizeof(imported_q->x), sizeof(q->x)))
1712
1713 switch (version) {
1714 case 0: {
1715 struct pf_altq_v0 *imported_q =
1716 &((struct pfioc_altq_v0 *)pa)->altq;
1717
1718 COPY(ifname);
1719
1720 ASSIGN(scheduler);
1721 ASSIGN(tbrsize); /* 16-bit -> 32-bit */
1722 ASSIGN(ifbandwidth); /* 32-bit -> 64-bit */
1723
1724 COPY(qname);
1725 COPY(parent);
1726 ASSIGN(parent_qid);
1727 ASSIGN(bandwidth); /* 32-bit -> 64-bit */
1728 ASSIGN(priority);
1729 ASSIGN(local_flags);
1730
1731 ASSIGN(qlimit);
1732 ASSIGN(flags);
1733
1734 if (imported_q->scheduler == ALTQT_HFSC) {
1735 #define ASSIGN_OPT(x) q->pq_u.hfsc_opts.x = imported_q->pq_u.hfsc_opts.x
1736
1737 /*
1738 * The m1 and m2 parameters are being copied from
1739 * 32-bit to 64-bit.
1740 */
1741 ASSIGN_OPT(rtsc_m1);
1742 ASSIGN_OPT(rtsc_d);
1743 ASSIGN_OPT(rtsc_m2);
1744
1745 ASSIGN_OPT(lssc_m1);
1746 ASSIGN_OPT(lssc_d);
1747 ASSIGN_OPT(lssc_m2);
1748
1749 ASSIGN_OPT(ulsc_m1);
1750 ASSIGN_OPT(ulsc_d);
1751 ASSIGN_OPT(ulsc_m2);
1752
1753 ASSIGN_OPT(flags);
1754
1755 #undef ASSIGN_OPT
1756 } else
1757 COPY(pq_u);
1758
1759 ASSIGN(qid);
1760 break;
1761 }
1762 case 1: {
1763 struct pf_altq_v1 *imported_q =
1764 &((struct pfioc_altq_v1 *)pa)->altq;
1765
1766 COPY(ifname);
1767
1768 ASSIGN(scheduler);
1769 ASSIGN(tbrsize);
1770 ASSIGN(ifbandwidth);
1771
1772 COPY(qname);
1773 COPY(parent);
1774 ASSIGN(parent_qid);
1775 ASSIGN(bandwidth);
1776 ASSIGN(priority);
1777 ASSIGN(local_flags);
1778
1779 ASSIGN(qlimit);
1780 ASSIGN(flags);
1781 COPY(pq_u);
1782
1783 ASSIGN(qid);
1784 break;
1785 }
1786 default:
1787 panic("%s: unhandled struct pfioc_altq version", __func__);
1788 break;
1789 }
1790
1791 #undef ASSIGN
1792 #undef COPY
1793
1794 return (0);
1795 }
1796
1797 static struct pf_altq *
pf_altq_get_nth_active(u_int32_t n)1798 pf_altq_get_nth_active(u_int32_t n)
1799 {
1800 struct pf_altq *altq;
1801 u_int32_t nr;
1802
1803 nr = 0;
1804 TAILQ_FOREACH(altq, V_pf_altq_ifs_active, entries) {
1805 if (nr == n)
1806 return (altq);
1807 nr++;
1808 }
1809
1810 TAILQ_FOREACH(altq, V_pf_altqs_active, entries) {
1811 if (nr == n)
1812 return (altq);
1813 nr++;
1814 }
1815
1816 return (NULL);
1817 }
1818 #endif /* ALTQ */
1819
1820 struct pf_krule *
pf_krule_alloc(void)1821 pf_krule_alloc(void)
1822 {
1823 struct pf_krule *rule;
1824
1825 rule = malloc(sizeof(struct pf_krule), M_PFRULE, M_WAITOK | M_ZERO);
1826 mtx_init(&rule->rpool.mtx, "pf_krule_pool", NULL, MTX_DEF);
1827 rule->timestamp = uma_zalloc_pcpu(pf_timestamp_pcpu_zone,
1828 M_WAITOK | M_ZERO);
1829 return (rule);
1830 }
1831
1832 void
pf_krule_free(struct pf_krule * rule)1833 pf_krule_free(struct pf_krule *rule)
1834 {
1835 #ifdef PF_WANT_32_TO_64_COUNTER
1836 bool wowned;
1837 #endif
1838
1839 if (rule == NULL)
1840 return;
1841
1842 #ifdef PF_WANT_32_TO_64_COUNTER
1843 if (rule->allrulelinked) {
1844 wowned = PF_RULES_WOWNED();
1845 if (!wowned)
1846 PF_RULES_WLOCK();
1847 LIST_REMOVE(rule, allrulelist);
1848 V_pf_allrulecount--;
1849 if (!wowned)
1850 PF_RULES_WUNLOCK();
1851 }
1852 #endif
1853
1854 pf_counter_u64_deinit(&rule->evaluations);
1855 for (int i = 0; i < 2; i++) {
1856 pf_counter_u64_deinit(&rule->packets[i]);
1857 pf_counter_u64_deinit(&rule->bytes[i]);
1858 }
1859 counter_u64_free(rule->states_cur);
1860 counter_u64_free(rule->states_tot);
1861 counter_u64_free(rule->src_nodes);
1862 uma_zfree_pcpu(pf_timestamp_pcpu_zone, rule->timestamp);
1863
1864 mtx_destroy(&rule->rpool.mtx);
1865 free(rule, M_PFRULE);
1866 }
1867
1868 void
pf_krule_clear_counters(struct pf_krule * rule)1869 pf_krule_clear_counters(struct pf_krule *rule)
1870 {
1871 pf_counter_u64_zero(&rule->evaluations);
1872 for (int i = 0; i < 2; i++) {
1873 pf_counter_u64_zero(&rule->packets[i]);
1874 pf_counter_u64_zero(&rule->bytes[i]);
1875 }
1876 counter_u64_zero(rule->states_tot);
1877 }
1878
1879 static void
pf_kpooladdr_to_pooladdr(const struct pf_kpooladdr * kpool,struct pf_pooladdr * pool)1880 pf_kpooladdr_to_pooladdr(const struct pf_kpooladdr *kpool,
1881 struct pf_pooladdr *pool)
1882 {
1883
1884 bzero(pool, sizeof(*pool));
1885 bcopy(&kpool->addr, &pool->addr, sizeof(pool->addr));
1886 strlcpy(pool->ifname, kpool->ifname, sizeof(pool->ifname));
1887 }
1888
1889 static int
pf_pooladdr_to_kpooladdr(const struct pf_pooladdr * pool,struct pf_kpooladdr * kpool)1890 pf_pooladdr_to_kpooladdr(const struct pf_pooladdr *pool,
1891 struct pf_kpooladdr *kpool)
1892 {
1893 int ret;
1894
1895 bzero(kpool, sizeof(*kpool));
1896 bcopy(&pool->addr, &kpool->addr, sizeof(kpool->addr));
1897 ret = pf_user_strcpy(kpool->ifname, pool->ifname,
1898 sizeof(kpool->ifname));
1899 return (ret);
1900 }
1901
1902 static void
pf_pool_to_kpool(const struct pf_pool * pool,struct pf_kpool * kpool)1903 pf_pool_to_kpool(const struct pf_pool *pool, struct pf_kpool *kpool)
1904 {
1905 _Static_assert(sizeof(pool->key) == sizeof(kpool->key), "");
1906 _Static_assert(sizeof(pool->counter) == sizeof(kpool->counter), "");
1907
1908 bcopy(&pool->key, &kpool->key, sizeof(kpool->key));
1909 bcopy(&pool->counter, &kpool->counter, sizeof(kpool->counter));
1910
1911 kpool->tblidx = pool->tblidx;
1912 kpool->proxy_port[0] = pool->proxy_port[0];
1913 kpool->proxy_port[1] = pool->proxy_port[1];
1914 kpool->opts = pool->opts;
1915 }
1916
1917 static int
pf_rule_to_krule(const struct pf_rule * rule,struct pf_krule * krule)1918 pf_rule_to_krule(const struct pf_rule *rule, struct pf_krule *krule)
1919 {
1920 int ret;
1921
1922 #ifndef INET
1923 if (rule->af == AF_INET) {
1924 return (EAFNOSUPPORT);
1925 }
1926 #endif /* INET */
1927 #ifndef INET6
1928 if (rule->af == AF_INET6) {
1929 return (EAFNOSUPPORT);
1930 }
1931 #endif /* INET6 */
1932
1933 ret = pf_check_rule_addr(&rule->src);
1934 if (ret != 0)
1935 return (ret);
1936 ret = pf_check_rule_addr(&rule->dst);
1937 if (ret != 0)
1938 return (ret);
1939
1940 bcopy(&rule->src, &krule->src, sizeof(rule->src));
1941 bcopy(&rule->dst, &krule->dst, sizeof(rule->dst));
1942
1943 ret = pf_user_strcpy(krule->label[0], rule->label, sizeof(rule->label));
1944 if (ret != 0)
1945 return (ret);
1946 ret = pf_user_strcpy(krule->ifname, rule->ifname, sizeof(rule->ifname));
1947 if (ret != 0)
1948 return (ret);
1949 ret = pf_user_strcpy(krule->qname, rule->qname, sizeof(rule->qname));
1950 if (ret != 0)
1951 return (ret);
1952 ret = pf_user_strcpy(krule->pqname, rule->pqname, sizeof(rule->pqname));
1953 if (ret != 0)
1954 return (ret);
1955 ret = pf_user_strcpy(krule->tagname, rule->tagname,
1956 sizeof(rule->tagname));
1957 if (ret != 0)
1958 return (ret);
1959 ret = pf_user_strcpy(krule->match_tagname, rule->match_tagname,
1960 sizeof(rule->match_tagname));
1961 if (ret != 0)
1962 return (ret);
1963 ret = pf_user_strcpy(krule->overload_tblname, rule->overload_tblname,
1964 sizeof(rule->overload_tblname));
1965 if (ret != 0)
1966 return (ret);
1967
1968 pf_pool_to_kpool(&rule->rpool, &krule->rpool);
1969
1970 /* Don't allow userspace to set evaluations, packets or bytes. */
1971 /* kif, anchor, overload_tbl are not copied over. */
1972
1973 krule->os_fingerprint = rule->os_fingerprint;
1974
1975 krule->rtableid = rule->rtableid;
1976 /* pf_rule->timeout is smaller than pf_krule->timeout */
1977 bcopy(rule->timeout, krule->timeout, sizeof(rule->timeout));
1978 krule->max_states = rule->max_states;
1979 krule->max_src_nodes = rule->max_src_nodes;
1980 krule->max_src_states = rule->max_src_states;
1981 krule->max_src_conn = rule->max_src_conn;
1982 krule->max_src_conn_rate.limit = rule->max_src_conn_rate.limit;
1983 krule->max_src_conn_rate.seconds = rule->max_src_conn_rate.seconds;
1984 krule->qid = rule->qid;
1985 krule->pqid = rule->pqid;
1986 krule->nr = rule->nr;
1987 krule->prob = rule->prob;
1988 krule->cuid = rule->cuid;
1989 krule->cpid = rule->cpid;
1990
1991 krule->return_icmp = rule->return_icmp;
1992 krule->return_icmp6 = rule->return_icmp6;
1993 krule->max_mss = rule->max_mss;
1994 krule->tag = rule->tag;
1995 krule->match_tag = rule->match_tag;
1996 krule->scrub_flags = rule->scrub_flags;
1997
1998 bcopy(&rule->uid, &krule->uid, sizeof(krule->uid));
1999 bcopy(&rule->gid, &krule->gid, sizeof(krule->gid));
2000
2001 krule->rule_flag = rule->rule_flag;
2002 krule->action = rule->action;
2003 krule->direction = rule->direction;
2004 krule->log = rule->log;
2005 krule->logif = rule->logif;
2006 krule->quick = rule->quick;
2007 krule->ifnot = rule->ifnot;
2008 krule->match_tag_not = rule->match_tag_not;
2009 krule->natpass = rule->natpass;
2010
2011 krule->keep_state = rule->keep_state;
2012 krule->af = rule->af;
2013 krule->proto = rule->proto;
2014 krule->type = rule->type;
2015 krule->code = rule->code;
2016 krule->flags = rule->flags;
2017 krule->flagset = rule->flagset;
2018 krule->min_ttl = rule->min_ttl;
2019 krule->allow_opts = rule->allow_opts;
2020 krule->rt = rule->rt;
2021 krule->return_ttl = rule->return_ttl;
2022 krule->tos = rule->tos;
2023 krule->set_tos = rule->set_tos;
2024
2025 krule->flush = rule->flush;
2026 krule->prio = rule->prio;
2027 krule->set_prio[0] = rule->set_prio[0];
2028 krule->set_prio[1] = rule->set_prio[1];
2029
2030 bcopy(&rule->divert, &krule->divert, sizeof(krule->divert));
2031
2032 return (0);
2033 }
2034
2035 int
pf_ioctl_getrules(struct pfioc_rule * pr)2036 pf_ioctl_getrules(struct pfioc_rule *pr)
2037 {
2038 struct pf_kruleset *ruleset;
2039 struct pf_krule *tail;
2040 int rs_num;
2041
2042 PF_RULES_WLOCK();
2043 ruleset = pf_find_kruleset(pr->anchor);
2044 if (ruleset == NULL) {
2045 PF_RULES_WUNLOCK();
2046 return (EINVAL);
2047 }
2048 rs_num = pf_get_ruleset_number(pr->rule.action);
2049 if (rs_num >= PF_RULESET_MAX) {
2050 PF_RULES_WUNLOCK();
2051 return (EINVAL);
2052 }
2053 tail = TAILQ_LAST(ruleset->rules[rs_num].active.ptr,
2054 pf_krulequeue);
2055 if (tail)
2056 pr->nr = tail->nr + 1;
2057 else
2058 pr->nr = 0;
2059 pr->ticket = ruleset->rules[rs_num].active.ticket;
2060 PF_RULES_WUNLOCK();
2061
2062 return (0);
2063 }
2064
2065 int
pf_ioctl_addrule(struct pf_krule * rule,uint32_t ticket,uint32_t pool_ticket,const char * anchor,const char * anchor_call,uid_t uid,pid_t pid)2066 pf_ioctl_addrule(struct pf_krule *rule, uint32_t ticket,
2067 uint32_t pool_ticket, const char *anchor, const char *anchor_call,
2068 uid_t uid, pid_t pid)
2069 {
2070 struct pf_kruleset *ruleset;
2071 struct pf_krule *tail;
2072 struct pf_kpooladdr *pa;
2073 struct pfi_kkif *kif = NULL, *rcv_kif = NULL;
2074 int rs_num;
2075 int error = 0;
2076
2077 if ((rule->return_icmp >> 8) > ICMP_MAXTYPE) {
2078 error = EINVAL;
2079 goto errout_unlocked;
2080 }
2081
2082 #define ERROUT(x) ERROUT_FUNCTION(errout, x)
2083
2084 if (rule->ifname[0])
2085 kif = pf_kkif_create(M_WAITOK);
2086 if (rule->rcv_ifname[0])
2087 rcv_kif = pf_kkif_create(M_WAITOK);
2088 pf_counter_u64_init(&rule->evaluations, M_WAITOK);
2089 for (int i = 0; i < 2; i++) {
2090 pf_counter_u64_init(&rule->packets[i], M_WAITOK);
2091 pf_counter_u64_init(&rule->bytes[i], M_WAITOK);
2092 }
2093 rule->states_cur = counter_u64_alloc(M_WAITOK);
2094 rule->states_tot = counter_u64_alloc(M_WAITOK);
2095 rule->src_nodes = counter_u64_alloc(M_WAITOK);
2096 rule->cuid = uid;
2097 rule->cpid = pid;
2098 TAILQ_INIT(&rule->rpool.list);
2099
2100 PF_CONFIG_LOCK();
2101 PF_RULES_WLOCK();
2102 #ifdef PF_WANT_32_TO_64_COUNTER
2103 LIST_INSERT_HEAD(&V_pf_allrulelist, rule, allrulelist);
2104 MPASS(!rule->allrulelinked);
2105 rule->allrulelinked = true;
2106 V_pf_allrulecount++;
2107 #endif
2108 ruleset = pf_find_kruleset(anchor);
2109 if (ruleset == NULL)
2110 ERROUT(EINVAL);
2111 rs_num = pf_get_ruleset_number(rule->action);
2112 if (rs_num >= PF_RULESET_MAX)
2113 ERROUT(EINVAL);
2114 if (ticket != ruleset->rules[rs_num].inactive.ticket) {
2115 DPFPRINTF(PF_DEBUG_MISC,
2116 ("ticket: %d != [%d]%d\n", ticket, rs_num,
2117 ruleset->rules[rs_num].inactive.ticket));
2118 ERROUT(EBUSY);
2119 }
2120 if (pool_ticket != V_ticket_pabuf) {
2121 DPFPRINTF(PF_DEBUG_MISC,
2122 ("pool_ticket: %d != %d\n", pool_ticket,
2123 V_ticket_pabuf));
2124 ERROUT(EBUSY);
2125 }
2126 /*
2127 * XXXMJG hack: there is no mechanism to ensure they started the
2128 * transaction. Ticket checked above may happen to match by accident,
2129 * even if nobody called DIOCXBEGIN, let alone this process.
2130 * Partially work around it by checking if the RB tree got allocated,
2131 * see pf_begin_rules.
2132 */
2133 if (ruleset->rules[rs_num].inactive.tree == NULL) {
2134 ERROUT(EINVAL);
2135 }
2136
2137 tail = TAILQ_LAST(ruleset->rules[rs_num].inactive.ptr,
2138 pf_krulequeue);
2139 if (tail)
2140 rule->nr = tail->nr + 1;
2141 else
2142 rule->nr = 0;
2143 if (rule->ifname[0]) {
2144 rule->kif = pfi_kkif_attach(kif, rule->ifname);
2145 kif = NULL;
2146 pfi_kkif_ref(rule->kif);
2147 } else
2148 rule->kif = NULL;
2149
2150 if (rule->rcv_ifname[0]) {
2151 rule->rcv_kif = pfi_kkif_attach(rcv_kif, rule->rcv_ifname);
2152 rcv_kif = NULL;
2153 pfi_kkif_ref(rule->rcv_kif);
2154 } else
2155 rule->rcv_kif = NULL;
2156
2157 if (rule->rtableid > 0 && rule->rtableid >= rt_numfibs)
2158 error = EBUSY;
2159
2160 #ifdef ALTQ
2161 /* set queue IDs */
2162 if (rule->qname[0] != 0) {
2163 if ((rule->qid = pf_qname2qid(rule->qname)) == 0)
2164 error = EBUSY;
2165 else if (rule->pqname[0] != 0) {
2166 if ((rule->pqid =
2167 pf_qname2qid(rule->pqname)) == 0)
2168 error = EBUSY;
2169 } else
2170 rule->pqid = rule->qid;
2171 }
2172 #endif
2173 if (rule->tagname[0])
2174 if ((rule->tag = pf_tagname2tag(rule->tagname)) == 0)
2175 error = EBUSY;
2176 if (rule->match_tagname[0])
2177 if ((rule->match_tag =
2178 pf_tagname2tag(rule->match_tagname)) == 0)
2179 error = EBUSY;
2180 if (rule->rt && !rule->direction)
2181 error = EINVAL;
2182 if (!rule->log)
2183 rule->logif = 0;
2184 if (rule->logif >= PFLOGIFS_MAX)
2185 error = EINVAL;
2186 if (pf_addr_setup(ruleset, &rule->src.addr, rule->af))
2187 error = ENOMEM;
2188 if (pf_addr_setup(ruleset, &rule->dst.addr, rule->af))
2189 error = ENOMEM;
2190 if (pf_kanchor_setup(rule, ruleset, anchor_call))
2191 error = EINVAL;
2192 if (rule->scrub_flags & PFSTATE_SETPRIO &&
2193 (rule->set_prio[0] > PF_PRIO_MAX ||
2194 rule->set_prio[1] > PF_PRIO_MAX))
2195 error = EINVAL;
2196 TAILQ_FOREACH(pa, &V_pf_pabuf, entries)
2197 if (pa->addr.type == PF_ADDR_TABLE) {
2198 pa->addr.p.tbl = pfr_attach_table(ruleset,
2199 pa->addr.v.tblname);
2200 if (pa->addr.p.tbl == NULL)
2201 error = ENOMEM;
2202 }
2203
2204 rule->overload_tbl = NULL;
2205 if (rule->overload_tblname[0]) {
2206 if ((rule->overload_tbl = pfr_attach_table(ruleset,
2207 rule->overload_tblname)) == NULL)
2208 error = EINVAL;
2209 else
2210 rule->overload_tbl->pfrkt_flags |=
2211 PFR_TFLAG_ACTIVE;
2212 }
2213
2214 pf_mv_kpool(&V_pf_pabuf, &rule->rpool.list);
2215 if (((((rule->action == PF_NAT) || (rule->action == PF_RDR) ||
2216 (rule->action == PF_BINAT)) && rule->anchor == NULL) ||
2217 (rule->rt > PF_NOPFROUTE)) &&
2218 (TAILQ_FIRST(&rule->rpool.list) == NULL))
2219 error = EINVAL;
2220
2221 if (rule->action == PF_PASS && rule->rpool.opts & PF_POOL_STICKYADDR &&
2222 !rule->keep_state) {
2223 error = EINVAL;
2224 }
2225
2226 if (error) {
2227 pf_free_rule(rule);
2228 rule = NULL;
2229 ERROUT(error);
2230 }
2231
2232 rule->rpool.cur = TAILQ_FIRST(&rule->rpool.list);
2233 TAILQ_INSERT_TAIL(ruleset->rules[rs_num].inactive.ptr,
2234 rule, entries);
2235 ruleset->rules[rs_num].inactive.rcount++;
2236
2237 PF_RULES_WUNLOCK();
2238 pf_hash_rule(rule);
2239 if (RB_INSERT(pf_krule_global, ruleset->rules[rs_num].inactive.tree, rule) != NULL) {
2240 PF_RULES_WLOCK();
2241 TAILQ_REMOVE(ruleset->rules[rs_num].inactive.ptr, rule, entries);
2242 ruleset->rules[rs_num].inactive.rcount--;
2243 pf_free_rule(rule);
2244 rule = NULL;
2245 ERROUT(EEXIST);
2246 }
2247 PF_CONFIG_UNLOCK();
2248
2249 return (0);
2250
2251 #undef ERROUT
2252 errout:
2253 PF_RULES_WUNLOCK();
2254 PF_CONFIG_UNLOCK();
2255 errout_unlocked:
2256 pf_kkif_free(rcv_kif);
2257 pf_kkif_free(kif);
2258 pf_krule_free(rule);
2259 return (error);
2260 }
2261
2262 static bool
pf_label_match(const struct pf_krule * rule,const char * label)2263 pf_label_match(const struct pf_krule *rule, const char *label)
2264 {
2265 int i = 0;
2266
2267 while (*rule->label[i]) {
2268 if (strcmp(rule->label[i], label) == 0)
2269 return (true);
2270 i++;
2271 }
2272
2273 return (false);
2274 }
2275
2276 static unsigned int
pf_kill_matching_state(struct pf_state_key_cmp * key,int dir)2277 pf_kill_matching_state(struct pf_state_key_cmp *key, int dir)
2278 {
2279 struct pf_kstate *s;
2280 int more = 0;
2281
2282 s = pf_find_state_all(key, dir, &more);
2283 if (s == NULL)
2284 return (0);
2285
2286 if (more) {
2287 PF_STATE_UNLOCK(s);
2288 return (0);
2289 }
2290
2291 pf_unlink_state(s);
2292 return (1);
2293 }
2294
2295 static int
pf_killstates_row(struct pf_kstate_kill * psk,struct pf_idhash * ih)2296 pf_killstates_row(struct pf_kstate_kill *psk, struct pf_idhash *ih)
2297 {
2298 struct pf_kstate *s;
2299 struct pf_state_key *sk;
2300 struct pf_addr *srcaddr, *dstaddr;
2301 struct pf_state_key_cmp match_key;
2302 int idx, killed = 0;
2303 unsigned int dir;
2304 u_int16_t srcport, dstport;
2305 struct pfi_kkif *kif;
2306
2307 relock_DIOCKILLSTATES:
2308 PF_HASHROW_LOCK(ih);
2309 LIST_FOREACH(s, &ih->states, entry) {
2310 /* For floating states look at the original kif. */
2311 kif = s->kif == V_pfi_all ? s->orig_kif : s->kif;
2312
2313 sk = s->key[psk->psk_nat ? PF_SK_STACK : PF_SK_WIRE];
2314 if (s->direction == PF_OUT) {
2315 srcaddr = &sk->addr[1];
2316 dstaddr = &sk->addr[0];
2317 srcport = sk->port[1];
2318 dstport = sk->port[0];
2319 } else {
2320 srcaddr = &sk->addr[0];
2321 dstaddr = &sk->addr[1];
2322 srcport = sk->port[0];
2323 dstport = sk->port[1];
2324 }
2325
2326 if (psk->psk_af && sk->af != psk->psk_af)
2327 continue;
2328
2329 if (psk->psk_proto && psk->psk_proto != sk->proto)
2330 continue;
2331
2332 if (! PF_MATCHA(psk->psk_src.neg, &psk->psk_src.addr.v.a.addr,
2333 &psk->psk_src.addr.v.a.mask, srcaddr, sk->af))
2334 continue;
2335
2336 if (! PF_MATCHA(psk->psk_dst.neg, &psk->psk_dst.addr.v.a.addr,
2337 &psk->psk_dst.addr.v.a.mask, dstaddr, sk->af))
2338 continue;
2339
2340 if (! PF_MATCHA(psk->psk_rt_addr.neg,
2341 &psk->psk_rt_addr.addr.v.a.addr,
2342 &psk->psk_rt_addr.addr.v.a.mask,
2343 &s->rt_addr, sk->af))
2344 continue;
2345
2346 if (psk->psk_src.port_op != 0 &&
2347 ! pf_match_port(psk->psk_src.port_op,
2348 psk->psk_src.port[0], psk->psk_src.port[1], srcport))
2349 continue;
2350
2351 if (psk->psk_dst.port_op != 0 &&
2352 ! pf_match_port(psk->psk_dst.port_op,
2353 psk->psk_dst.port[0], psk->psk_dst.port[1], dstport))
2354 continue;
2355
2356 if (psk->psk_label[0] &&
2357 ! pf_label_match(s->rule, psk->psk_label))
2358 continue;
2359
2360 if (psk->psk_ifname[0] && strcmp(psk->psk_ifname,
2361 kif->pfik_name))
2362 continue;
2363
2364 if (psk->psk_kill_match) {
2365 /* Create the key to find matching states, with lock
2366 * held. */
2367
2368 bzero(&match_key, sizeof(match_key));
2369
2370 if (s->direction == PF_OUT) {
2371 dir = PF_IN;
2372 idx = psk->psk_nat ? PF_SK_WIRE : PF_SK_STACK;
2373 } else {
2374 dir = PF_OUT;
2375 idx = psk->psk_nat ? PF_SK_STACK : PF_SK_WIRE;
2376 }
2377
2378 match_key.af = s->key[idx]->af;
2379 match_key.proto = s->key[idx]->proto;
2380 PF_ACPY(&match_key.addr[0],
2381 &s->key[idx]->addr[1], match_key.af);
2382 match_key.port[0] = s->key[idx]->port[1];
2383 PF_ACPY(&match_key.addr[1],
2384 &s->key[idx]->addr[0], match_key.af);
2385 match_key.port[1] = s->key[idx]->port[0];
2386 }
2387
2388 pf_unlink_state(s);
2389 killed++;
2390
2391 if (psk->psk_kill_match)
2392 killed += pf_kill_matching_state(&match_key, dir);
2393
2394 goto relock_DIOCKILLSTATES;
2395 }
2396 PF_HASHROW_UNLOCK(ih);
2397
2398 return (killed);
2399 }
2400
2401 int
pf_start(void)2402 pf_start(void)
2403 {
2404 int error = 0;
2405
2406 sx_xlock(&V_pf_ioctl_lock);
2407 if (V_pf_status.running)
2408 error = EEXIST;
2409 else {
2410 hook_pf();
2411 if (! TAILQ_EMPTY(V_pf_keth->active.rules))
2412 hook_pf_eth();
2413 V_pf_status.running = 1;
2414 V_pf_status.since = time_second;
2415 new_unrhdr64(&V_pf_stateid, time_second);
2416
2417 DPFPRINTF(PF_DEBUG_MISC, ("pf: started\n"));
2418 }
2419 sx_xunlock(&V_pf_ioctl_lock);
2420
2421 return (error);
2422 }
2423
2424 int
pf_stop(void)2425 pf_stop(void)
2426 {
2427 int error = 0;
2428
2429 sx_xlock(&V_pf_ioctl_lock);
2430 if (!V_pf_status.running)
2431 error = ENOENT;
2432 else {
2433 V_pf_status.running = 0;
2434 dehook_pf();
2435 dehook_pf_eth();
2436 V_pf_status.since = time_second;
2437 DPFPRINTF(PF_DEBUG_MISC, ("pf: stopped\n"));
2438 }
2439 sx_xunlock(&V_pf_ioctl_lock);
2440
2441 return (error);
2442 }
2443
2444 void
pf_ioctl_clear_status(void)2445 pf_ioctl_clear_status(void)
2446 {
2447 PF_RULES_WLOCK();
2448 for (int i = 0; i < PFRES_MAX; i++)
2449 counter_u64_zero(V_pf_status.counters[i]);
2450 for (int i = 0; i < FCNT_MAX; i++)
2451 pf_counter_u64_zero(&V_pf_status.fcounters[i]);
2452 for (int i = 0; i < SCNT_MAX; i++)
2453 counter_u64_zero(V_pf_status.scounters[i]);
2454 for (int i = 0; i < KLCNT_MAX; i++)
2455 counter_u64_zero(V_pf_status.lcounters[i]);
2456 V_pf_status.since = time_second;
2457 if (*V_pf_status.ifname)
2458 pfi_update_status(V_pf_status.ifname, NULL);
2459 PF_RULES_WUNLOCK();
2460 }
2461
2462 int
pf_ioctl_set_timeout(int timeout,int seconds,int * prev_seconds)2463 pf_ioctl_set_timeout(int timeout, int seconds, int *prev_seconds)
2464 {
2465 uint32_t old;
2466
2467 if (timeout < 0 || timeout >= PFTM_MAX ||
2468 seconds < 0)
2469 return (EINVAL);
2470
2471 PF_RULES_WLOCK();
2472 old = V_pf_default_rule.timeout[timeout];
2473 if (timeout == PFTM_INTERVAL && seconds == 0)
2474 seconds = 1;
2475 V_pf_default_rule.timeout[timeout] = seconds;
2476 if (timeout == PFTM_INTERVAL && seconds < old)
2477 wakeup(pf_purge_thread);
2478
2479 if (prev_seconds != NULL)
2480 *prev_seconds = old;
2481
2482 PF_RULES_WUNLOCK();
2483
2484 return (0);
2485 }
2486
2487 int
pf_ioctl_get_timeout(int timeout,int * seconds)2488 pf_ioctl_get_timeout(int timeout, int *seconds)
2489 {
2490 PF_RULES_RLOCK_TRACKER;
2491
2492 if (timeout < 0 || timeout >= PFTM_MAX)
2493 return (EINVAL);
2494
2495 PF_RULES_RLOCK();
2496 *seconds = V_pf_default_rule.timeout[timeout];
2497 PF_RULES_RUNLOCK();
2498
2499 return (0);
2500 }
2501
2502 int
pf_ioctl_set_limit(int index,unsigned int limit,unsigned int * old_limit)2503 pf_ioctl_set_limit(int index, unsigned int limit, unsigned int *old_limit)
2504 {
2505
2506 PF_RULES_WLOCK();
2507 if (index < 0 || index >= PF_LIMIT_MAX ||
2508 V_pf_limits[index].zone == NULL) {
2509 PF_RULES_WUNLOCK();
2510 return (EINVAL);
2511 }
2512 uma_zone_set_max(V_pf_limits[index].zone, limit);
2513 if (old_limit != NULL)
2514 *old_limit = V_pf_limits[index].limit;
2515 V_pf_limits[index].limit = limit;
2516 PF_RULES_WUNLOCK();
2517
2518 return (0);
2519 }
2520
2521 int
pf_ioctl_get_limit(int index,unsigned int * limit)2522 pf_ioctl_get_limit(int index, unsigned int *limit)
2523 {
2524 PF_RULES_RLOCK_TRACKER;
2525
2526 if (index < 0 || index >= PF_LIMIT_MAX)
2527 return (EINVAL);
2528
2529 PF_RULES_RLOCK();
2530 *limit = V_pf_limits[index].limit;
2531 PF_RULES_RUNLOCK();
2532
2533 return (0);
2534 }
2535
2536 int
pf_ioctl_begin_addrs(uint32_t * ticket)2537 pf_ioctl_begin_addrs(uint32_t *ticket)
2538 {
2539 PF_RULES_WLOCK();
2540 pf_empty_kpool(&V_pf_pabuf);
2541 *ticket = ++V_ticket_pabuf;
2542 PF_RULES_WUNLOCK();
2543
2544 return (0);
2545 }
2546
2547 int
pf_ioctl_add_addr(struct pfioc_pooladdr * pp)2548 pf_ioctl_add_addr(struct pfioc_pooladdr *pp)
2549 {
2550 struct pf_kpooladdr *pa = NULL;
2551 struct pfi_kkif *kif = NULL;
2552 int error;
2553
2554 #ifndef INET
2555 if (pp->af == AF_INET)
2556 return (EAFNOSUPPORT);
2557 #endif /* INET */
2558 #ifndef INET6
2559 if (pp->af == AF_INET6)
2560 return (EAFNOSUPPORT);
2561 #endif /* INET6 */
2562
2563 if (pp->addr.addr.type != PF_ADDR_ADDRMASK &&
2564 pp->addr.addr.type != PF_ADDR_DYNIFTL &&
2565 pp->addr.addr.type != PF_ADDR_TABLE)
2566 return (EINVAL);
2567
2568 if (pp->addr.addr.p.dyn != NULL)
2569 return (EINVAL);
2570
2571 pa = malloc(sizeof(*pa), M_PFRULE, M_WAITOK);
2572 error = pf_pooladdr_to_kpooladdr(&pp->addr, pa);
2573 if (error != 0)
2574 goto out;
2575 if (pa->ifname[0])
2576 kif = pf_kkif_create(M_WAITOK);
2577 PF_RULES_WLOCK();
2578 if (pp->ticket != V_ticket_pabuf) {
2579 PF_RULES_WUNLOCK();
2580 if (pa->ifname[0])
2581 pf_kkif_free(kif);
2582 error = EBUSY;
2583 goto out;
2584 }
2585 if (pa->ifname[0]) {
2586 pa->kif = pfi_kkif_attach(kif, pa->ifname);
2587 kif = NULL;
2588 pfi_kkif_ref(pa->kif);
2589 } else
2590 pa->kif = NULL;
2591 if (pa->addr.type == PF_ADDR_DYNIFTL && ((error =
2592 pfi_dynaddr_setup(&pa->addr, pp->af)) != 0)) {
2593 if (pa->ifname[0])
2594 pfi_kkif_unref(pa->kif);
2595 PF_RULES_WUNLOCK();
2596 goto out;
2597 }
2598 TAILQ_INSERT_TAIL(&V_pf_pabuf, pa, entries);
2599 PF_RULES_WUNLOCK();
2600
2601 return (0);
2602
2603 out:
2604 free(pa, M_PFRULE);
2605 return (error);
2606 }
2607
2608 int
pf_ioctl_get_addrs(struct pfioc_pooladdr * pp)2609 pf_ioctl_get_addrs(struct pfioc_pooladdr *pp)
2610 {
2611 struct pf_kpool *pool;
2612 struct pf_kpooladdr *pa;
2613
2614 PF_RULES_RLOCK_TRACKER;
2615
2616 pp->anchor[sizeof(pp->anchor) - 1] = 0;
2617 pp->nr = 0;
2618
2619 PF_RULES_RLOCK();
2620 pool = pf_get_kpool(pp->anchor, pp->ticket, pp->r_action,
2621 pp->r_num, 0, 1, 0);
2622 if (pool == NULL) {
2623 PF_RULES_RUNLOCK();
2624 return (EBUSY);
2625 }
2626 TAILQ_FOREACH(pa, &pool->list, entries)
2627 pp->nr++;
2628 PF_RULES_RUNLOCK();
2629
2630 return (0);
2631 }
2632
2633 int
pf_ioctl_get_addr(struct pfioc_pooladdr * pp)2634 pf_ioctl_get_addr(struct pfioc_pooladdr *pp)
2635 {
2636 struct pf_kpool *pool;
2637 struct pf_kpooladdr *pa;
2638 u_int32_t nr = 0;
2639
2640 PF_RULES_RLOCK_TRACKER;
2641
2642 pp->anchor[sizeof(pp->anchor) - 1] = 0;
2643
2644 PF_RULES_RLOCK();
2645 pool = pf_get_kpool(pp->anchor, pp->ticket, pp->r_action,
2646 pp->r_num, 0, 1, 1);
2647 if (pool == NULL) {
2648 PF_RULES_RUNLOCK();
2649 return (EBUSY);
2650 }
2651 pa = TAILQ_FIRST(&pool->list);
2652 while ((pa != NULL) && (nr < pp->nr)) {
2653 pa = TAILQ_NEXT(pa, entries);
2654 nr++;
2655 }
2656 if (pa == NULL) {
2657 PF_RULES_RUNLOCK();
2658 return (EBUSY);
2659 }
2660 pf_kpooladdr_to_pooladdr(pa, &pp->addr);
2661 pf_addr_copyout(&pp->addr.addr);
2662 PF_RULES_RUNLOCK();
2663
2664 return (0);
2665 }
2666
2667 int
pf_ioctl_get_rulesets(struct pfioc_ruleset * pr)2668 pf_ioctl_get_rulesets(struct pfioc_ruleset *pr)
2669 {
2670 struct pf_kruleset *ruleset;
2671 struct pf_kanchor *anchor;
2672
2673 PF_RULES_RLOCK_TRACKER;
2674
2675 pr->path[sizeof(pr->path) - 1] = 0;
2676
2677 PF_RULES_RLOCK();
2678 if ((ruleset = pf_find_kruleset(pr->path)) == NULL) {
2679 PF_RULES_RUNLOCK();
2680 return (ENOENT);
2681 }
2682 pr->nr = 0;
2683 if (ruleset->anchor == NULL) {
2684 /* XXX kludge for pf_main_ruleset */
2685 RB_FOREACH(anchor, pf_kanchor_global, &V_pf_anchors)
2686 if (anchor->parent == NULL)
2687 pr->nr++;
2688 } else {
2689 RB_FOREACH(anchor, pf_kanchor_node,
2690 &ruleset->anchor->children)
2691 pr->nr++;
2692 }
2693 PF_RULES_RUNLOCK();
2694
2695 return (0);
2696 }
2697
2698 int
pf_ioctl_get_ruleset(struct pfioc_ruleset * pr)2699 pf_ioctl_get_ruleset(struct pfioc_ruleset *pr)
2700 {
2701 struct pf_kruleset *ruleset;
2702 struct pf_kanchor *anchor;
2703 u_int32_t nr = 0;
2704 int error = 0;
2705
2706 PF_RULES_RLOCK_TRACKER;
2707
2708 PF_RULES_RLOCK();
2709 if ((ruleset = pf_find_kruleset(pr->path)) == NULL) {
2710 PF_RULES_RUNLOCK();
2711 return (ENOENT);
2712 }
2713
2714 pr->name[0] = 0;
2715 if (ruleset->anchor == NULL) {
2716 /* XXX kludge for pf_main_ruleset */
2717 RB_FOREACH(anchor, pf_kanchor_global, &V_pf_anchors)
2718 if (anchor->parent == NULL && nr++ == pr->nr) {
2719 strlcpy(pr->name, anchor->name,
2720 sizeof(pr->name));
2721 break;
2722 }
2723 } else {
2724 RB_FOREACH(anchor, pf_kanchor_node,
2725 &ruleset->anchor->children)
2726 if (nr++ == pr->nr) {
2727 strlcpy(pr->name, anchor->name,
2728 sizeof(pr->name));
2729 break;
2730 }
2731 }
2732 if (!pr->name[0])
2733 error = EBUSY;
2734 PF_RULES_RUNLOCK();
2735
2736 return (error);
2737 }
2738
2739 static int
pfioctl(struct cdev * dev,u_long cmd,caddr_t addr,int flags,struct thread * td)2740 pfioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, struct thread *td)
2741 {
2742 int error = 0;
2743 PF_RULES_RLOCK_TRACKER;
2744
2745 #define ERROUT_IOCTL(target, x) \
2746 do { \
2747 error = (x); \
2748 SDT_PROBE3(pf, ioctl, ioctl, error, cmd, error, __LINE__); \
2749 goto target; \
2750 } while (0)
2751
2752
2753 /* XXX keep in sync with switch() below */
2754 if (securelevel_gt(td->td_ucred, 2))
2755 switch (cmd) {
2756 case DIOCGETRULES:
2757 case DIOCGETRULENV:
2758 case DIOCGETADDRS:
2759 case DIOCGETADDR:
2760 case DIOCGETSTATE:
2761 case DIOCGETSTATENV:
2762 case DIOCSETSTATUSIF:
2763 case DIOCGETSTATUSNV:
2764 case DIOCCLRSTATUS:
2765 case DIOCNATLOOK:
2766 case DIOCSETDEBUG:
2767 #ifdef COMPAT_FREEBSD14
2768 case DIOCGETSTATES:
2769 case DIOCGETSTATESV2:
2770 #endif
2771 case DIOCGETTIMEOUT:
2772 case DIOCCLRRULECTRS:
2773 case DIOCGETLIMIT:
2774 case DIOCGETALTQSV0:
2775 case DIOCGETALTQSV1:
2776 case DIOCGETALTQV0:
2777 case DIOCGETALTQV1:
2778 case DIOCGETQSTATSV0:
2779 case DIOCGETQSTATSV1:
2780 case DIOCGETRULESETS:
2781 case DIOCGETRULESET:
2782 case DIOCRGETTABLES:
2783 case DIOCRGETTSTATS:
2784 case DIOCRCLRTSTATS:
2785 case DIOCRCLRADDRS:
2786 case DIOCRADDADDRS:
2787 case DIOCRDELADDRS:
2788 case DIOCRSETADDRS:
2789 case DIOCRGETADDRS:
2790 case DIOCRGETASTATS:
2791 case DIOCRCLRASTATS:
2792 case DIOCRTSTADDRS:
2793 case DIOCOSFPGET:
2794 case DIOCGETSRCNODES:
2795 case DIOCCLRSRCNODES:
2796 case DIOCGETSYNCOOKIES:
2797 case DIOCIGETIFACES:
2798 case DIOCGIFSPEEDV0:
2799 case DIOCGIFSPEEDV1:
2800 case DIOCSETIFFLAG:
2801 case DIOCCLRIFFLAG:
2802 case DIOCGETETHRULES:
2803 case DIOCGETETHRULE:
2804 case DIOCGETETHRULESETS:
2805 case DIOCGETETHRULESET:
2806 break;
2807 case DIOCRCLRTABLES:
2808 case DIOCRADDTABLES:
2809 case DIOCRDELTABLES:
2810 case DIOCRSETTFLAGS:
2811 if (((struct pfioc_table *)addr)->pfrio_flags &
2812 PFR_FLAG_DUMMY)
2813 break; /* dummy operation ok */
2814 return (EPERM);
2815 default:
2816 return (EPERM);
2817 }
2818
2819 if (!(flags & FWRITE))
2820 switch (cmd) {
2821 case DIOCGETRULES:
2822 case DIOCGETADDRS:
2823 case DIOCGETADDR:
2824 case DIOCGETSTATE:
2825 case DIOCGETSTATENV:
2826 case DIOCGETSTATUSNV:
2827 #ifdef COMPAT_FREEBSD14
2828 case DIOCGETSTATES:
2829 case DIOCGETSTATESV2:
2830 #endif
2831 case DIOCGETTIMEOUT:
2832 case DIOCGETLIMIT:
2833 case DIOCGETALTQSV0:
2834 case DIOCGETALTQSV1:
2835 case DIOCGETALTQV0:
2836 case DIOCGETALTQV1:
2837 case DIOCGETQSTATSV0:
2838 case DIOCGETQSTATSV1:
2839 case DIOCGETRULESETS:
2840 case DIOCGETRULESET:
2841 case DIOCNATLOOK:
2842 case DIOCRGETTABLES:
2843 case DIOCRGETTSTATS:
2844 case DIOCRGETADDRS:
2845 case DIOCRGETASTATS:
2846 case DIOCRTSTADDRS:
2847 case DIOCOSFPGET:
2848 case DIOCGETSRCNODES:
2849 case DIOCGETSYNCOOKIES:
2850 case DIOCIGETIFACES:
2851 case DIOCGIFSPEEDV1:
2852 case DIOCGIFSPEEDV0:
2853 case DIOCGETRULENV:
2854 case DIOCGETETHRULES:
2855 case DIOCGETETHRULE:
2856 case DIOCGETETHRULESETS:
2857 case DIOCGETETHRULESET:
2858 break;
2859 case DIOCRCLRTABLES:
2860 case DIOCRADDTABLES:
2861 case DIOCRDELTABLES:
2862 case DIOCRCLRTSTATS:
2863 case DIOCRCLRADDRS:
2864 case DIOCRADDADDRS:
2865 case DIOCRDELADDRS:
2866 case DIOCRSETADDRS:
2867 case DIOCRSETTFLAGS:
2868 if (((struct pfioc_table *)addr)->pfrio_flags &
2869 PFR_FLAG_DUMMY) {
2870 flags |= FWRITE; /* need write lock for dummy */
2871 break; /* dummy operation ok */
2872 }
2873 return (EACCES);
2874 default:
2875 return (EACCES);
2876 }
2877
2878 CURVNET_SET(TD_TO_VNET(td));
2879
2880 switch (cmd) {
2881 #ifdef COMPAT_FREEBSD14
2882 case DIOCSTART:
2883 error = pf_start();
2884 break;
2885
2886 case DIOCSTOP:
2887 error = pf_stop();
2888 break;
2889 #endif
2890
2891 case DIOCGETETHRULES: {
2892 struct pfioc_nv *nv = (struct pfioc_nv *)addr;
2893 nvlist_t *nvl;
2894 void *packed;
2895 struct pf_keth_rule *tail;
2896 struct pf_keth_ruleset *rs;
2897 u_int32_t ticket, nr;
2898 const char *anchor = "";
2899
2900 nvl = NULL;
2901 packed = NULL;
2902
2903 #define ERROUT(x) ERROUT_IOCTL(DIOCGETETHRULES_error, x)
2904
2905 if (nv->len > pf_ioctl_maxcount)
2906 ERROUT(ENOMEM);
2907
2908 /* Copy the request in */
2909 packed = malloc(nv->len, M_NVLIST, M_WAITOK);
2910 error = copyin(nv->data, packed, nv->len);
2911 if (error)
2912 ERROUT(error);
2913
2914 nvl = nvlist_unpack(packed, nv->len, 0);
2915 if (nvl == NULL)
2916 ERROUT(EBADMSG);
2917
2918 if (! nvlist_exists_string(nvl, "anchor"))
2919 ERROUT(EBADMSG);
2920
2921 anchor = nvlist_get_string(nvl, "anchor");
2922
2923 rs = pf_find_keth_ruleset(anchor);
2924
2925 nvlist_destroy(nvl);
2926 nvl = NULL;
2927 free(packed, M_NVLIST);
2928 packed = NULL;
2929
2930 if (rs == NULL)
2931 ERROUT(ENOENT);
2932
2933 /* Reply */
2934 nvl = nvlist_create(0);
2935 if (nvl == NULL)
2936 ERROUT(ENOMEM);
2937
2938 PF_RULES_RLOCK();
2939
2940 ticket = rs->active.ticket;
2941 tail = TAILQ_LAST(rs->active.rules, pf_keth_ruleq);
2942 if (tail)
2943 nr = tail->nr + 1;
2944 else
2945 nr = 0;
2946
2947 PF_RULES_RUNLOCK();
2948
2949 nvlist_add_number(nvl, "ticket", ticket);
2950 nvlist_add_number(nvl, "nr", nr);
2951
2952 packed = nvlist_pack(nvl, &nv->len);
2953 if (packed == NULL)
2954 ERROUT(ENOMEM);
2955
2956 if (nv->size == 0)
2957 ERROUT(0);
2958 else if (nv->size < nv->len)
2959 ERROUT(ENOSPC);
2960
2961 error = copyout(packed, nv->data, nv->len);
2962
2963 #undef ERROUT
2964 DIOCGETETHRULES_error:
2965 free(packed, M_NVLIST);
2966 nvlist_destroy(nvl);
2967 break;
2968 }
2969
2970 case DIOCGETETHRULE: {
2971 struct epoch_tracker et;
2972 struct pfioc_nv *nv = (struct pfioc_nv *)addr;
2973 nvlist_t *nvl = NULL;
2974 void *nvlpacked = NULL;
2975 struct pf_keth_rule *rule = NULL;
2976 struct pf_keth_ruleset *rs;
2977 u_int32_t ticket, nr;
2978 bool clear = false;
2979 const char *anchor;
2980
2981 #define ERROUT(x) ERROUT_IOCTL(DIOCGETETHRULE_error, x)
2982
2983 if (nv->len > pf_ioctl_maxcount)
2984 ERROUT(ENOMEM);
2985
2986 nvlpacked = malloc(nv->len, M_NVLIST, M_WAITOK);
2987 error = copyin(nv->data, nvlpacked, nv->len);
2988 if (error)
2989 ERROUT(error);
2990
2991 nvl = nvlist_unpack(nvlpacked, nv->len, 0);
2992 if (nvl == NULL)
2993 ERROUT(EBADMSG);
2994 if (! nvlist_exists_number(nvl, "ticket"))
2995 ERROUT(EBADMSG);
2996 ticket = nvlist_get_number(nvl, "ticket");
2997 if (! nvlist_exists_string(nvl, "anchor"))
2998 ERROUT(EBADMSG);
2999 anchor = nvlist_get_string(nvl, "anchor");
3000
3001 if (nvlist_exists_bool(nvl, "clear"))
3002 clear = nvlist_get_bool(nvl, "clear");
3003
3004 if (clear && !(flags & FWRITE))
3005 ERROUT(EACCES);
3006
3007 if (! nvlist_exists_number(nvl, "nr"))
3008 ERROUT(EBADMSG);
3009 nr = nvlist_get_number(nvl, "nr");
3010
3011 PF_RULES_RLOCK();
3012 rs = pf_find_keth_ruleset(anchor);
3013 if (rs == NULL) {
3014 PF_RULES_RUNLOCK();
3015 ERROUT(ENOENT);
3016 }
3017 if (ticket != rs->active.ticket) {
3018 PF_RULES_RUNLOCK();
3019 ERROUT(EBUSY);
3020 }
3021
3022 nvlist_destroy(nvl);
3023 nvl = NULL;
3024 free(nvlpacked, M_NVLIST);
3025 nvlpacked = NULL;
3026
3027 rule = TAILQ_FIRST(rs->active.rules);
3028 while ((rule != NULL) && (rule->nr != nr))
3029 rule = TAILQ_NEXT(rule, entries);
3030 if (rule == NULL) {
3031 PF_RULES_RUNLOCK();
3032 ERROUT(ENOENT);
3033 }
3034 /* Make sure rule can't go away. */
3035 NET_EPOCH_ENTER(et);
3036 PF_RULES_RUNLOCK();
3037 nvl = pf_keth_rule_to_nveth_rule(rule);
3038 if (pf_keth_anchor_nvcopyout(rs, rule, nvl)) {
3039 NET_EPOCH_EXIT(et);
3040 ERROUT(EBUSY);
3041 }
3042 NET_EPOCH_EXIT(et);
3043 if (nvl == NULL)
3044 ERROUT(ENOMEM);
3045
3046 nvlpacked = nvlist_pack(nvl, &nv->len);
3047 if (nvlpacked == NULL)
3048 ERROUT(ENOMEM);
3049
3050 if (nv->size == 0)
3051 ERROUT(0);
3052 else if (nv->size < nv->len)
3053 ERROUT(ENOSPC);
3054
3055 error = copyout(nvlpacked, nv->data, nv->len);
3056 if (error == 0 && clear) {
3057 counter_u64_zero(rule->evaluations);
3058 for (int i = 0; i < 2; i++) {
3059 counter_u64_zero(rule->packets[i]);
3060 counter_u64_zero(rule->bytes[i]);
3061 }
3062 }
3063
3064 #undef ERROUT
3065 DIOCGETETHRULE_error:
3066 free(nvlpacked, M_NVLIST);
3067 nvlist_destroy(nvl);
3068 break;
3069 }
3070
3071 case DIOCADDETHRULE: {
3072 struct pfioc_nv *nv = (struct pfioc_nv *)addr;
3073 nvlist_t *nvl = NULL;
3074 void *nvlpacked = NULL;
3075 struct pf_keth_rule *rule = NULL, *tail = NULL;
3076 struct pf_keth_ruleset *ruleset = NULL;
3077 struct pfi_kkif *kif = NULL, *bridge_to_kif = NULL;
3078 const char *anchor = "", *anchor_call = "";
3079
3080 #define ERROUT(x) ERROUT_IOCTL(DIOCADDETHRULE_error, x)
3081
3082 if (nv->len > pf_ioctl_maxcount)
3083 ERROUT(ENOMEM);
3084
3085 nvlpacked = malloc(nv->len, M_NVLIST, M_WAITOK);
3086 error = copyin(nv->data, nvlpacked, nv->len);
3087 if (error)
3088 ERROUT(error);
3089
3090 nvl = nvlist_unpack(nvlpacked, nv->len, 0);
3091 if (nvl == NULL)
3092 ERROUT(EBADMSG);
3093
3094 if (! nvlist_exists_number(nvl, "ticket"))
3095 ERROUT(EBADMSG);
3096
3097 if (nvlist_exists_string(nvl, "anchor"))
3098 anchor = nvlist_get_string(nvl, "anchor");
3099 if (nvlist_exists_string(nvl, "anchor_call"))
3100 anchor_call = nvlist_get_string(nvl, "anchor_call");
3101
3102 ruleset = pf_find_keth_ruleset(anchor);
3103 if (ruleset == NULL)
3104 ERROUT(EINVAL);
3105
3106 if (nvlist_get_number(nvl, "ticket") !=
3107 ruleset->inactive.ticket) {
3108 DPFPRINTF(PF_DEBUG_MISC,
3109 ("ticket: %d != %d\n",
3110 (u_int32_t)nvlist_get_number(nvl, "ticket"),
3111 ruleset->inactive.ticket));
3112 ERROUT(EBUSY);
3113 }
3114
3115 rule = malloc(sizeof(*rule), M_PFRULE, M_WAITOK);
3116 rule->timestamp = NULL;
3117
3118 error = pf_nveth_rule_to_keth_rule(nvl, rule);
3119 if (error != 0)
3120 ERROUT(error);
3121
3122 if (rule->ifname[0])
3123 kif = pf_kkif_create(M_WAITOK);
3124 if (rule->bridge_to_name[0])
3125 bridge_to_kif = pf_kkif_create(M_WAITOK);
3126 rule->evaluations = counter_u64_alloc(M_WAITOK);
3127 for (int i = 0; i < 2; i++) {
3128 rule->packets[i] = counter_u64_alloc(M_WAITOK);
3129 rule->bytes[i] = counter_u64_alloc(M_WAITOK);
3130 }
3131 rule->timestamp = uma_zalloc_pcpu(pf_timestamp_pcpu_zone,
3132 M_WAITOK | M_ZERO);
3133
3134 PF_RULES_WLOCK();
3135
3136 if (rule->ifname[0]) {
3137 rule->kif = pfi_kkif_attach(kif, rule->ifname);
3138 pfi_kkif_ref(rule->kif);
3139 } else
3140 rule->kif = NULL;
3141 if (rule->bridge_to_name[0]) {
3142 rule->bridge_to = pfi_kkif_attach(bridge_to_kif,
3143 rule->bridge_to_name);
3144 pfi_kkif_ref(rule->bridge_to);
3145 } else
3146 rule->bridge_to = NULL;
3147
3148 #ifdef ALTQ
3149 /* set queue IDs */
3150 if (rule->qname[0] != 0) {
3151 if ((rule->qid = pf_qname2qid(rule->qname)) == 0)
3152 error = EBUSY;
3153 else
3154 rule->qid = rule->qid;
3155 }
3156 #endif
3157 if (rule->tagname[0])
3158 if ((rule->tag = pf_tagname2tag(rule->tagname)) == 0)
3159 error = EBUSY;
3160 if (rule->match_tagname[0])
3161 if ((rule->match_tag = pf_tagname2tag(
3162 rule->match_tagname)) == 0)
3163 error = EBUSY;
3164
3165 if (error == 0 && rule->ipdst.addr.type == PF_ADDR_TABLE)
3166 error = pf_eth_addr_setup(ruleset, &rule->ipdst.addr);
3167 if (error == 0 && rule->ipsrc.addr.type == PF_ADDR_TABLE)
3168 error = pf_eth_addr_setup(ruleset, &rule->ipsrc.addr);
3169
3170 if (error) {
3171 pf_free_eth_rule(rule);
3172 PF_RULES_WUNLOCK();
3173 ERROUT(error);
3174 }
3175
3176 if (pf_keth_anchor_setup(rule, ruleset, anchor_call)) {
3177 pf_free_eth_rule(rule);
3178 PF_RULES_WUNLOCK();
3179 ERROUT(EINVAL);
3180 }
3181
3182 tail = TAILQ_LAST(ruleset->inactive.rules, pf_keth_ruleq);
3183 if (tail)
3184 rule->nr = tail->nr + 1;
3185 else
3186 rule->nr = 0;
3187
3188 TAILQ_INSERT_TAIL(ruleset->inactive.rules, rule, entries);
3189
3190 PF_RULES_WUNLOCK();
3191
3192 #undef ERROUT
3193 DIOCADDETHRULE_error:
3194 nvlist_destroy(nvl);
3195 free(nvlpacked, M_NVLIST);
3196 break;
3197 }
3198
3199 case DIOCGETETHRULESETS: {
3200 struct epoch_tracker et;
3201 struct pfioc_nv *nv = (struct pfioc_nv *)addr;
3202 nvlist_t *nvl = NULL;
3203 void *nvlpacked = NULL;
3204 struct pf_keth_ruleset *ruleset;
3205 struct pf_keth_anchor *anchor;
3206 int nr = 0;
3207
3208 #define ERROUT(x) ERROUT_IOCTL(DIOCGETETHRULESETS_error, x)
3209
3210 if (nv->len > pf_ioctl_maxcount)
3211 ERROUT(ENOMEM);
3212
3213 nvlpacked = malloc(nv->len, M_NVLIST, M_WAITOK);
3214 error = copyin(nv->data, nvlpacked, nv->len);
3215 if (error)
3216 ERROUT(error);
3217
3218 nvl = nvlist_unpack(nvlpacked, nv->len, 0);
3219 if (nvl == NULL)
3220 ERROUT(EBADMSG);
3221 if (! nvlist_exists_string(nvl, "path"))
3222 ERROUT(EBADMSG);
3223
3224 NET_EPOCH_ENTER(et);
3225
3226 if ((ruleset = pf_find_keth_ruleset(
3227 nvlist_get_string(nvl, "path"))) == NULL) {
3228 NET_EPOCH_EXIT(et);
3229 ERROUT(ENOENT);
3230 }
3231
3232 if (ruleset->anchor == NULL) {
3233 RB_FOREACH(anchor, pf_keth_anchor_global, &V_pf_keth_anchors)
3234 if (anchor->parent == NULL)
3235 nr++;
3236 } else {
3237 RB_FOREACH(anchor, pf_keth_anchor_node,
3238 &ruleset->anchor->children)
3239 nr++;
3240 }
3241
3242 NET_EPOCH_EXIT(et);
3243
3244 nvlist_destroy(nvl);
3245 nvl = NULL;
3246 free(nvlpacked, M_NVLIST);
3247 nvlpacked = NULL;
3248
3249 nvl = nvlist_create(0);
3250 if (nvl == NULL)
3251 ERROUT(ENOMEM);
3252
3253 nvlist_add_number(nvl, "nr", nr);
3254
3255 nvlpacked = nvlist_pack(nvl, &nv->len);
3256 if (nvlpacked == NULL)
3257 ERROUT(ENOMEM);
3258
3259 if (nv->size == 0)
3260 ERROUT(0);
3261 else if (nv->size < nv->len)
3262 ERROUT(ENOSPC);
3263
3264 error = copyout(nvlpacked, nv->data, nv->len);
3265
3266 #undef ERROUT
3267 DIOCGETETHRULESETS_error:
3268 free(nvlpacked, M_NVLIST);
3269 nvlist_destroy(nvl);
3270 break;
3271 }
3272
3273 case DIOCGETETHRULESET: {
3274 struct epoch_tracker et;
3275 struct pfioc_nv *nv = (struct pfioc_nv *)addr;
3276 nvlist_t *nvl = NULL;
3277 void *nvlpacked = NULL;
3278 struct pf_keth_ruleset *ruleset;
3279 struct pf_keth_anchor *anchor;
3280 int nr = 0, req_nr = 0;
3281 bool found = false;
3282
3283 #define ERROUT(x) ERROUT_IOCTL(DIOCGETETHRULESET_error, x)
3284
3285 if (nv->len > pf_ioctl_maxcount)
3286 ERROUT(ENOMEM);
3287
3288 nvlpacked = malloc(nv->len, M_NVLIST, M_WAITOK);
3289 error = copyin(nv->data, nvlpacked, nv->len);
3290 if (error)
3291 ERROUT(error);
3292
3293 nvl = nvlist_unpack(nvlpacked, nv->len, 0);
3294 if (nvl == NULL)
3295 ERROUT(EBADMSG);
3296 if (! nvlist_exists_string(nvl, "path"))
3297 ERROUT(EBADMSG);
3298 if (! nvlist_exists_number(nvl, "nr"))
3299 ERROUT(EBADMSG);
3300
3301 req_nr = nvlist_get_number(nvl, "nr");
3302
3303 NET_EPOCH_ENTER(et);
3304
3305 if ((ruleset = pf_find_keth_ruleset(
3306 nvlist_get_string(nvl, "path"))) == NULL) {
3307 NET_EPOCH_EXIT(et);
3308 ERROUT(ENOENT);
3309 }
3310
3311 nvlist_destroy(nvl);
3312 nvl = NULL;
3313 free(nvlpacked, M_NVLIST);
3314 nvlpacked = NULL;
3315
3316 nvl = nvlist_create(0);
3317 if (nvl == NULL) {
3318 NET_EPOCH_EXIT(et);
3319 ERROUT(ENOMEM);
3320 }
3321
3322 if (ruleset->anchor == NULL) {
3323 RB_FOREACH(anchor, pf_keth_anchor_global,
3324 &V_pf_keth_anchors) {
3325 if (anchor->parent == NULL && nr++ == req_nr) {
3326 found = true;
3327 break;
3328 }
3329 }
3330 } else {
3331 RB_FOREACH(anchor, pf_keth_anchor_node,
3332 &ruleset->anchor->children) {
3333 if (nr++ == req_nr) {
3334 found = true;
3335 break;
3336 }
3337 }
3338 }
3339
3340 NET_EPOCH_EXIT(et);
3341 if (found) {
3342 nvlist_add_number(nvl, "nr", nr);
3343 nvlist_add_string(nvl, "name", anchor->name);
3344 if (ruleset->anchor)
3345 nvlist_add_string(nvl, "path",
3346 ruleset->anchor->path);
3347 else
3348 nvlist_add_string(nvl, "path", "");
3349 } else {
3350 ERROUT(EBUSY);
3351 }
3352
3353 nvlpacked = nvlist_pack(nvl, &nv->len);
3354 if (nvlpacked == NULL)
3355 ERROUT(ENOMEM);
3356
3357 if (nv->size == 0)
3358 ERROUT(0);
3359 else if (nv->size < nv->len)
3360 ERROUT(ENOSPC);
3361
3362 error = copyout(nvlpacked, nv->data, nv->len);
3363
3364 #undef ERROUT
3365 DIOCGETETHRULESET_error:
3366 free(nvlpacked, M_NVLIST);
3367 nvlist_destroy(nvl);
3368 break;
3369 }
3370
3371 case DIOCADDRULENV: {
3372 struct pfioc_nv *nv = (struct pfioc_nv *)addr;
3373 nvlist_t *nvl = NULL;
3374 void *nvlpacked = NULL;
3375 struct pf_krule *rule = NULL;
3376 const char *anchor = "", *anchor_call = "";
3377 uint32_t ticket = 0, pool_ticket = 0;
3378
3379 #define ERROUT(x) ERROUT_IOCTL(DIOCADDRULENV_error, x)
3380
3381 if (nv->len > pf_ioctl_maxcount)
3382 ERROUT(ENOMEM);
3383
3384 nvlpacked = malloc(nv->len, M_NVLIST, M_WAITOK);
3385 error = copyin(nv->data, nvlpacked, nv->len);
3386 if (error)
3387 ERROUT(error);
3388
3389 nvl = nvlist_unpack(nvlpacked, nv->len, 0);
3390 if (nvl == NULL)
3391 ERROUT(EBADMSG);
3392
3393 if (! nvlist_exists_number(nvl, "ticket"))
3394 ERROUT(EINVAL);
3395 ticket = nvlist_get_number(nvl, "ticket");
3396
3397 if (! nvlist_exists_number(nvl, "pool_ticket"))
3398 ERROUT(EINVAL);
3399 pool_ticket = nvlist_get_number(nvl, "pool_ticket");
3400
3401 if (! nvlist_exists_nvlist(nvl, "rule"))
3402 ERROUT(EINVAL);
3403
3404 rule = pf_krule_alloc();
3405 error = pf_nvrule_to_krule(nvlist_get_nvlist(nvl, "rule"),
3406 rule);
3407 if (error)
3408 ERROUT(error);
3409
3410 if (nvlist_exists_string(nvl, "anchor"))
3411 anchor = nvlist_get_string(nvl, "anchor");
3412 if (nvlist_exists_string(nvl, "anchor_call"))
3413 anchor_call = nvlist_get_string(nvl, "anchor_call");
3414
3415 if ((error = nvlist_error(nvl)))
3416 ERROUT(error);
3417
3418 /* Frees rule on error */
3419 error = pf_ioctl_addrule(rule, ticket, pool_ticket, anchor,
3420 anchor_call, td->td_ucred->cr_ruid,
3421 td->td_proc ? td->td_proc->p_pid : 0);
3422
3423 nvlist_destroy(nvl);
3424 free(nvlpacked, M_NVLIST);
3425 break;
3426 #undef ERROUT
3427 DIOCADDRULENV_error:
3428 pf_krule_free(rule);
3429 nvlist_destroy(nvl);
3430 free(nvlpacked, M_NVLIST);
3431
3432 break;
3433 }
3434 case DIOCADDRULE: {
3435 struct pfioc_rule *pr = (struct pfioc_rule *)addr;
3436 struct pf_krule *rule;
3437
3438 rule = pf_krule_alloc();
3439 error = pf_rule_to_krule(&pr->rule, rule);
3440 if (error != 0) {
3441 pf_krule_free(rule);
3442 break;
3443 }
3444
3445 pr->anchor[sizeof(pr->anchor) - 1] = 0;
3446
3447 /* Frees rule on error */
3448 error = pf_ioctl_addrule(rule, pr->ticket, pr->pool_ticket,
3449 pr->anchor, pr->anchor_call, td->td_ucred->cr_ruid,
3450 td->td_proc ? td->td_proc->p_pid : 0);
3451 break;
3452 }
3453
3454 case DIOCGETRULES: {
3455 struct pfioc_rule *pr = (struct pfioc_rule *)addr;
3456
3457 pr->anchor[sizeof(pr->anchor) - 1] = 0;
3458
3459 error = pf_ioctl_getrules(pr);
3460
3461 break;
3462 }
3463
3464 case DIOCGETRULENV: {
3465 struct pfioc_nv *nv = (struct pfioc_nv *)addr;
3466 nvlist_t *nvrule = NULL;
3467 nvlist_t *nvl = NULL;
3468 struct pf_kruleset *ruleset;
3469 struct pf_krule *rule;
3470 void *nvlpacked = NULL;
3471 int rs_num, nr;
3472 bool clear_counter = false;
3473
3474 #define ERROUT(x) ERROUT_IOCTL(DIOCGETRULENV_error, x)
3475
3476 if (nv->len > pf_ioctl_maxcount)
3477 ERROUT(ENOMEM);
3478
3479 /* Copy the request in */
3480 nvlpacked = malloc(nv->len, M_NVLIST, M_WAITOK);
3481 error = copyin(nv->data, nvlpacked, nv->len);
3482 if (error)
3483 ERROUT(error);
3484
3485 nvl = nvlist_unpack(nvlpacked, nv->len, 0);
3486 if (nvl == NULL)
3487 ERROUT(EBADMSG);
3488
3489 if (! nvlist_exists_string(nvl, "anchor"))
3490 ERROUT(EBADMSG);
3491 if (! nvlist_exists_number(nvl, "ruleset"))
3492 ERROUT(EBADMSG);
3493 if (! nvlist_exists_number(nvl, "ticket"))
3494 ERROUT(EBADMSG);
3495 if (! nvlist_exists_number(nvl, "nr"))
3496 ERROUT(EBADMSG);
3497
3498 if (nvlist_exists_bool(nvl, "clear_counter"))
3499 clear_counter = nvlist_get_bool(nvl, "clear_counter");
3500
3501 if (clear_counter && !(flags & FWRITE))
3502 ERROUT(EACCES);
3503
3504 nr = nvlist_get_number(nvl, "nr");
3505
3506 PF_RULES_WLOCK();
3507 ruleset = pf_find_kruleset(nvlist_get_string(nvl, "anchor"));
3508 if (ruleset == NULL) {
3509 PF_RULES_WUNLOCK();
3510 ERROUT(ENOENT);
3511 }
3512
3513 rs_num = pf_get_ruleset_number(nvlist_get_number(nvl, "ruleset"));
3514 if (rs_num >= PF_RULESET_MAX) {
3515 PF_RULES_WUNLOCK();
3516 ERROUT(EINVAL);
3517 }
3518
3519 if (nvlist_get_number(nvl, "ticket") !=
3520 ruleset->rules[rs_num].active.ticket) {
3521 PF_RULES_WUNLOCK();
3522 ERROUT(EBUSY);
3523 }
3524
3525 if ((error = nvlist_error(nvl))) {
3526 PF_RULES_WUNLOCK();
3527 ERROUT(error);
3528 }
3529
3530 rule = TAILQ_FIRST(ruleset->rules[rs_num].active.ptr);
3531 while ((rule != NULL) && (rule->nr != nr))
3532 rule = TAILQ_NEXT(rule, entries);
3533 if (rule == NULL) {
3534 PF_RULES_WUNLOCK();
3535 ERROUT(EBUSY);
3536 }
3537
3538 nvrule = pf_krule_to_nvrule(rule);
3539
3540 nvlist_destroy(nvl);
3541 nvl = nvlist_create(0);
3542 if (nvl == NULL) {
3543 PF_RULES_WUNLOCK();
3544 ERROUT(ENOMEM);
3545 }
3546 nvlist_add_number(nvl, "nr", nr);
3547 nvlist_add_nvlist(nvl, "rule", nvrule);
3548 nvlist_destroy(nvrule);
3549 nvrule = NULL;
3550 if (pf_kanchor_nvcopyout(ruleset, rule, nvl)) {
3551 PF_RULES_WUNLOCK();
3552 ERROUT(EBUSY);
3553 }
3554
3555 free(nvlpacked, M_NVLIST);
3556 nvlpacked = nvlist_pack(nvl, &nv->len);
3557 if (nvlpacked == NULL) {
3558 PF_RULES_WUNLOCK();
3559 ERROUT(ENOMEM);
3560 }
3561
3562 if (nv->size == 0) {
3563 PF_RULES_WUNLOCK();
3564 ERROUT(0);
3565 }
3566 else if (nv->size < nv->len) {
3567 PF_RULES_WUNLOCK();
3568 ERROUT(ENOSPC);
3569 }
3570
3571 if (clear_counter)
3572 pf_krule_clear_counters(rule);
3573
3574 PF_RULES_WUNLOCK();
3575
3576 error = copyout(nvlpacked, nv->data, nv->len);
3577
3578 #undef ERROUT
3579 DIOCGETRULENV_error:
3580 free(nvlpacked, M_NVLIST);
3581 nvlist_destroy(nvrule);
3582 nvlist_destroy(nvl);
3583
3584 break;
3585 }
3586
3587 case DIOCCHANGERULE: {
3588 struct pfioc_rule *pcr = (struct pfioc_rule *)addr;
3589 struct pf_kruleset *ruleset;
3590 struct pf_krule *oldrule = NULL, *newrule = NULL;
3591 struct pfi_kkif *kif = NULL;
3592 struct pf_kpooladdr *pa;
3593 u_int32_t nr = 0;
3594 int rs_num;
3595
3596 pcr->anchor[sizeof(pcr->anchor) - 1] = 0;
3597
3598 if (pcr->action < PF_CHANGE_ADD_HEAD ||
3599 pcr->action > PF_CHANGE_GET_TICKET) {
3600 error = EINVAL;
3601 break;
3602 }
3603 if (pcr->rule.return_icmp >> 8 > ICMP_MAXTYPE) {
3604 error = EINVAL;
3605 break;
3606 }
3607
3608 if (pcr->action != PF_CHANGE_REMOVE) {
3609 newrule = pf_krule_alloc();
3610 error = pf_rule_to_krule(&pcr->rule, newrule);
3611 if (error != 0) {
3612 pf_krule_free(newrule);
3613 break;
3614 }
3615
3616 if (newrule->ifname[0])
3617 kif = pf_kkif_create(M_WAITOK);
3618 pf_counter_u64_init(&newrule->evaluations, M_WAITOK);
3619 for (int i = 0; i < 2; i++) {
3620 pf_counter_u64_init(&newrule->packets[i], M_WAITOK);
3621 pf_counter_u64_init(&newrule->bytes[i], M_WAITOK);
3622 }
3623 newrule->states_cur = counter_u64_alloc(M_WAITOK);
3624 newrule->states_tot = counter_u64_alloc(M_WAITOK);
3625 newrule->src_nodes = counter_u64_alloc(M_WAITOK);
3626 newrule->cuid = td->td_ucred->cr_ruid;
3627 newrule->cpid = td->td_proc ? td->td_proc->p_pid : 0;
3628 TAILQ_INIT(&newrule->rpool.list);
3629 }
3630 #define ERROUT(x) ERROUT_IOCTL(DIOCCHANGERULE_error, x)
3631
3632 PF_CONFIG_LOCK();
3633 PF_RULES_WLOCK();
3634 #ifdef PF_WANT_32_TO_64_COUNTER
3635 if (newrule != NULL) {
3636 LIST_INSERT_HEAD(&V_pf_allrulelist, newrule, allrulelist);
3637 newrule->allrulelinked = true;
3638 V_pf_allrulecount++;
3639 }
3640 #endif
3641
3642 if (!(pcr->action == PF_CHANGE_REMOVE ||
3643 pcr->action == PF_CHANGE_GET_TICKET) &&
3644 pcr->pool_ticket != V_ticket_pabuf)
3645 ERROUT(EBUSY);
3646
3647 ruleset = pf_find_kruleset(pcr->anchor);
3648 if (ruleset == NULL)
3649 ERROUT(EINVAL);
3650
3651 rs_num = pf_get_ruleset_number(pcr->rule.action);
3652 if (rs_num >= PF_RULESET_MAX)
3653 ERROUT(EINVAL);
3654
3655 /*
3656 * XXXMJG: there is no guarantee that the ruleset was
3657 * created by the usual route of calling DIOCXBEGIN.
3658 * As a result it is possible the rule tree will not
3659 * be allocated yet. Hack around it by doing it here.
3660 * Note it is fine to let the tree persist in case of
3661 * error as it will be freed down the road on future
3662 * updates (if need be).
3663 */
3664 if (ruleset->rules[rs_num].active.tree == NULL) {
3665 ruleset->rules[rs_num].active.tree = pf_rule_tree_alloc(M_NOWAIT);
3666 if (ruleset->rules[rs_num].active.tree == NULL) {
3667 ERROUT(ENOMEM);
3668 }
3669 }
3670
3671 if (pcr->action == PF_CHANGE_GET_TICKET) {
3672 pcr->ticket = ++ruleset->rules[rs_num].active.ticket;
3673 ERROUT(0);
3674 } else if (pcr->ticket !=
3675 ruleset->rules[rs_num].active.ticket)
3676 ERROUT(EINVAL);
3677
3678 if (pcr->action != PF_CHANGE_REMOVE) {
3679 if (newrule->ifname[0]) {
3680 newrule->kif = pfi_kkif_attach(kif,
3681 newrule->ifname);
3682 kif = NULL;
3683 pfi_kkif_ref(newrule->kif);
3684 } else
3685 newrule->kif = NULL;
3686
3687 if (newrule->rtableid > 0 &&
3688 newrule->rtableid >= rt_numfibs)
3689 error = EBUSY;
3690
3691 #ifdef ALTQ
3692 /* set queue IDs */
3693 if (newrule->qname[0] != 0) {
3694 if ((newrule->qid =
3695 pf_qname2qid(newrule->qname)) == 0)
3696 error = EBUSY;
3697 else if (newrule->pqname[0] != 0) {
3698 if ((newrule->pqid =
3699 pf_qname2qid(newrule->pqname)) == 0)
3700 error = EBUSY;
3701 } else
3702 newrule->pqid = newrule->qid;
3703 }
3704 #endif /* ALTQ */
3705 if (newrule->tagname[0])
3706 if ((newrule->tag =
3707 pf_tagname2tag(newrule->tagname)) == 0)
3708 error = EBUSY;
3709 if (newrule->match_tagname[0])
3710 if ((newrule->match_tag = pf_tagname2tag(
3711 newrule->match_tagname)) == 0)
3712 error = EBUSY;
3713 if (newrule->rt && !newrule->direction)
3714 error = EINVAL;
3715 if (!newrule->log)
3716 newrule->logif = 0;
3717 if (newrule->logif >= PFLOGIFS_MAX)
3718 error = EINVAL;
3719 if (pf_addr_setup(ruleset, &newrule->src.addr, newrule->af))
3720 error = ENOMEM;
3721 if (pf_addr_setup(ruleset, &newrule->dst.addr, newrule->af))
3722 error = ENOMEM;
3723 if (pf_kanchor_setup(newrule, ruleset, pcr->anchor_call))
3724 error = EINVAL;
3725 TAILQ_FOREACH(pa, &V_pf_pabuf, entries)
3726 if (pa->addr.type == PF_ADDR_TABLE) {
3727 pa->addr.p.tbl =
3728 pfr_attach_table(ruleset,
3729 pa->addr.v.tblname);
3730 if (pa->addr.p.tbl == NULL)
3731 error = ENOMEM;
3732 }
3733
3734 newrule->overload_tbl = NULL;
3735 if (newrule->overload_tblname[0]) {
3736 if ((newrule->overload_tbl = pfr_attach_table(
3737 ruleset, newrule->overload_tblname)) ==
3738 NULL)
3739 error = EINVAL;
3740 else
3741 newrule->overload_tbl->pfrkt_flags |=
3742 PFR_TFLAG_ACTIVE;
3743 }
3744
3745 pf_mv_kpool(&V_pf_pabuf, &newrule->rpool.list);
3746 if (((((newrule->action == PF_NAT) ||
3747 (newrule->action == PF_RDR) ||
3748 (newrule->action == PF_BINAT) ||
3749 (newrule->rt > PF_NOPFROUTE)) &&
3750 !newrule->anchor)) &&
3751 (TAILQ_FIRST(&newrule->rpool.list) == NULL))
3752 error = EINVAL;
3753
3754 if (error) {
3755 pf_free_rule(newrule);
3756 PF_RULES_WUNLOCK();
3757 PF_CONFIG_UNLOCK();
3758 break;
3759 }
3760
3761 newrule->rpool.cur = TAILQ_FIRST(&newrule->rpool.list);
3762 }
3763 pf_empty_kpool(&V_pf_pabuf);
3764
3765 if (pcr->action == PF_CHANGE_ADD_HEAD)
3766 oldrule = TAILQ_FIRST(
3767 ruleset->rules[rs_num].active.ptr);
3768 else if (pcr->action == PF_CHANGE_ADD_TAIL)
3769 oldrule = TAILQ_LAST(
3770 ruleset->rules[rs_num].active.ptr, pf_krulequeue);
3771 else {
3772 oldrule = TAILQ_FIRST(
3773 ruleset->rules[rs_num].active.ptr);
3774 while ((oldrule != NULL) && (oldrule->nr != pcr->nr))
3775 oldrule = TAILQ_NEXT(oldrule, entries);
3776 if (oldrule == NULL) {
3777 if (newrule != NULL)
3778 pf_free_rule(newrule);
3779 PF_RULES_WUNLOCK();
3780 PF_CONFIG_UNLOCK();
3781 error = EINVAL;
3782 break;
3783 }
3784 }
3785
3786 if (pcr->action == PF_CHANGE_REMOVE) {
3787 pf_unlink_rule(ruleset->rules[rs_num].active.ptr,
3788 oldrule);
3789 RB_REMOVE(pf_krule_global,
3790 ruleset->rules[rs_num].active.tree, oldrule);
3791 ruleset->rules[rs_num].active.rcount--;
3792 } else {
3793 pf_hash_rule(newrule);
3794 if (RB_INSERT(pf_krule_global,
3795 ruleset->rules[rs_num].active.tree, newrule) != NULL) {
3796 pf_free_rule(newrule);
3797 PF_RULES_WUNLOCK();
3798 PF_CONFIG_UNLOCK();
3799 error = EEXIST;
3800 break;
3801 }
3802
3803 if (oldrule == NULL)
3804 TAILQ_INSERT_TAIL(
3805 ruleset->rules[rs_num].active.ptr,
3806 newrule, entries);
3807 else if (pcr->action == PF_CHANGE_ADD_HEAD ||
3808 pcr->action == PF_CHANGE_ADD_BEFORE)
3809 TAILQ_INSERT_BEFORE(oldrule, newrule, entries);
3810 else
3811 TAILQ_INSERT_AFTER(
3812 ruleset->rules[rs_num].active.ptr,
3813 oldrule, newrule, entries);
3814 ruleset->rules[rs_num].active.rcount++;
3815 }
3816
3817 nr = 0;
3818 TAILQ_FOREACH(oldrule,
3819 ruleset->rules[rs_num].active.ptr, entries)
3820 oldrule->nr = nr++;
3821
3822 ruleset->rules[rs_num].active.ticket++;
3823
3824 pf_calc_skip_steps(ruleset->rules[rs_num].active.ptr);
3825 pf_remove_if_empty_kruleset(ruleset);
3826
3827 PF_RULES_WUNLOCK();
3828 PF_CONFIG_UNLOCK();
3829 break;
3830
3831 #undef ERROUT
3832 DIOCCHANGERULE_error:
3833 PF_RULES_WUNLOCK();
3834 PF_CONFIG_UNLOCK();
3835 pf_krule_free(newrule);
3836 pf_kkif_free(kif);
3837 break;
3838 }
3839
3840 case DIOCCLRSTATESNV: {
3841 error = pf_clearstates_nv((struct pfioc_nv *)addr);
3842 break;
3843 }
3844
3845 case DIOCKILLSTATESNV: {
3846 error = pf_killstates_nv((struct pfioc_nv *)addr);
3847 break;
3848 }
3849
3850 case DIOCADDSTATE: {
3851 struct pfioc_state *ps = (struct pfioc_state *)addr;
3852 struct pfsync_state_1301 *sp = &ps->state;
3853
3854 if (sp->timeout >= PFTM_MAX) {
3855 error = EINVAL;
3856 break;
3857 }
3858 if (V_pfsync_state_import_ptr != NULL) {
3859 PF_RULES_RLOCK();
3860 error = V_pfsync_state_import_ptr(
3861 (union pfsync_state_union *)sp, PFSYNC_SI_IOCTL,
3862 PFSYNC_MSG_VERSION_1301);
3863 PF_RULES_RUNLOCK();
3864 } else
3865 error = EOPNOTSUPP;
3866 break;
3867 }
3868
3869 case DIOCGETSTATE: {
3870 struct pfioc_state *ps = (struct pfioc_state *)addr;
3871 struct pf_kstate *s;
3872
3873 s = pf_find_state_byid(ps->state.id, ps->state.creatorid);
3874 if (s == NULL) {
3875 error = ENOENT;
3876 break;
3877 }
3878
3879 pfsync_state_export((union pfsync_state_union*)&ps->state,
3880 s, PFSYNC_MSG_VERSION_1301);
3881 PF_STATE_UNLOCK(s);
3882 break;
3883 }
3884
3885 case DIOCGETSTATENV: {
3886 error = pf_getstate((struct pfioc_nv *)addr);
3887 break;
3888 }
3889
3890 #ifdef COMPAT_FREEBSD14
3891 case DIOCGETSTATES: {
3892 struct pfioc_states *ps = (struct pfioc_states *)addr;
3893 struct pf_kstate *s;
3894 struct pfsync_state_1301 *pstore, *p;
3895 int i, nr;
3896 size_t slice_count = 16, count;
3897 void *out;
3898
3899 if (ps->ps_len <= 0) {
3900 nr = uma_zone_get_cur(V_pf_state_z);
3901 ps->ps_len = sizeof(struct pfsync_state_1301) * nr;
3902 break;
3903 }
3904
3905 out = ps->ps_states;
3906 pstore = mallocarray(slice_count,
3907 sizeof(struct pfsync_state_1301), M_TEMP, M_WAITOK | M_ZERO);
3908 nr = 0;
3909
3910 for (i = 0; i <= V_pf_hashmask; i++) {
3911 struct pf_idhash *ih = &V_pf_idhash[i];
3912
3913 DIOCGETSTATES_retry:
3914 p = pstore;
3915
3916 if (LIST_EMPTY(&ih->states))
3917 continue;
3918
3919 PF_HASHROW_LOCK(ih);
3920 count = 0;
3921 LIST_FOREACH(s, &ih->states, entry) {
3922 if (s->timeout == PFTM_UNLINKED)
3923 continue;
3924 count++;
3925 }
3926
3927 if (count > slice_count) {
3928 PF_HASHROW_UNLOCK(ih);
3929 free(pstore, M_TEMP);
3930 slice_count = count * 2;
3931 pstore = mallocarray(slice_count,
3932 sizeof(struct pfsync_state_1301), M_TEMP,
3933 M_WAITOK | M_ZERO);
3934 goto DIOCGETSTATES_retry;
3935 }
3936
3937 if ((nr+count) * sizeof(*p) > ps->ps_len) {
3938 PF_HASHROW_UNLOCK(ih);
3939 goto DIOCGETSTATES_full;
3940 }
3941
3942 LIST_FOREACH(s, &ih->states, entry) {
3943 if (s->timeout == PFTM_UNLINKED)
3944 continue;
3945
3946 pfsync_state_export((union pfsync_state_union*)p,
3947 s, PFSYNC_MSG_VERSION_1301);
3948 p++;
3949 nr++;
3950 }
3951 PF_HASHROW_UNLOCK(ih);
3952 error = copyout(pstore, out,
3953 sizeof(struct pfsync_state_1301) * count);
3954 if (error)
3955 break;
3956 out = ps->ps_states + nr;
3957 }
3958 DIOCGETSTATES_full:
3959 ps->ps_len = sizeof(struct pfsync_state_1301) * nr;
3960 free(pstore, M_TEMP);
3961
3962 break;
3963 }
3964
3965 case DIOCGETSTATESV2: {
3966 struct pfioc_states_v2 *ps = (struct pfioc_states_v2 *)addr;
3967 struct pf_kstate *s;
3968 struct pf_state_export *pstore, *p;
3969 int i, nr;
3970 size_t slice_count = 16, count;
3971 void *out;
3972
3973 if (ps->ps_req_version > PF_STATE_VERSION) {
3974 error = ENOTSUP;
3975 break;
3976 }
3977
3978 if (ps->ps_len <= 0) {
3979 nr = uma_zone_get_cur(V_pf_state_z);
3980 ps->ps_len = sizeof(struct pf_state_export) * nr;
3981 break;
3982 }
3983
3984 out = ps->ps_states;
3985 pstore = mallocarray(slice_count,
3986 sizeof(struct pf_state_export), M_TEMP, M_WAITOK | M_ZERO);
3987 nr = 0;
3988
3989 for (i = 0; i <= V_pf_hashmask; i++) {
3990 struct pf_idhash *ih = &V_pf_idhash[i];
3991
3992 DIOCGETSTATESV2_retry:
3993 p = pstore;
3994
3995 if (LIST_EMPTY(&ih->states))
3996 continue;
3997
3998 PF_HASHROW_LOCK(ih);
3999 count = 0;
4000 LIST_FOREACH(s, &ih->states, entry) {
4001 if (s->timeout == PFTM_UNLINKED)
4002 continue;
4003 count++;
4004 }
4005
4006 if (count > slice_count) {
4007 PF_HASHROW_UNLOCK(ih);
4008 free(pstore, M_TEMP);
4009 slice_count = count * 2;
4010 pstore = mallocarray(slice_count,
4011 sizeof(struct pf_state_export), M_TEMP,
4012 M_WAITOK | M_ZERO);
4013 goto DIOCGETSTATESV2_retry;
4014 }
4015
4016 if ((nr+count) * sizeof(*p) > ps->ps_len) {
4017 PF_HASHROW_UNLOCK(ih);
4018 goto DIOCGETSTATESV2_full;
4019 }
4020
4021 LIST_FOREACH(s, &ih->states, entry) {
4022 if (s->timeout == PFTM_UNLINKED)
4023 continue;
4024
4025 pf_state_export(p, s);
4026 p++;
4027 nr++;
4028 }
4029 PF_HASHROW_UNLOCK(ih);
4030 error = copyout(pstore, out,
4031 sizeof(struct pf_state_export) * count);
4032 if (error)
4033 break;
4034 out = ps->ps_states + nr;
4035 }
4036 DIOCGETSTATESV2_full:
4037 ps->ps_len = nr * sizeof(struct pf_state_export);
4038 free(pstore, M_TEMP);
4039
4040 break;
4041 }
4042 #endif
4043 case DIOCGETSTATUSNV: {
4044 error = pf_getstatus((struct pfioc_nv *)addr);
4045 break;
4046 }
4047
4048 case DIOCSETSTATUSIF: {
4049 struct pfioc_if *pi = (struct pfioc_if *)addr;
4050
4051 if (pi->ifname[0] == 0) {
4052 bzero(V_pf_status.ifname, IFNAMSIZ);
4053 break;
4054 }
4055 PF_RULES_WLOCK();
4056 error = pf_user_strcpy(V_pf_status.ifname, pi->ifname, IFNAMSIZ);
4057 PF_RULES_WUNLOCK();
4058 break;
4059 }
4060
4061 case DIOCCLRSTATUS: {
4062 pf_ioctl_clear_status();
4063 break;
4064 }
4065
4066 case DIOCNATLOOK: {
4067 struct pfioc_natlook *pnl = (struct pfioc_natlook *)addr;
4068 struct pf_state_key *sk;
4069 struct pf_kstate *state;
4070 struct pf_state_key_cmp key;
4071 int m = 0, direction = pnl->direction;
4072 int sidx, didx;
4073
4074 /* NATLOOK src and dst are reversed, so reverse sidx/didx */
4075 sidx = (direction == PF_IN) ? 1 : 0;
4076 didx = (direction == PF_IN) ? 0 : 1;
4077
4078 if (!pnl->proto ||
4079 PF_AZERO(&pnl->saddr, pnl->af) ||
4080 PF_AZERO(&pnl->daddr, pnl->af) ||
4081 ((pnl->proto == IPPROTO_TCP ||
4082 pnl->proto == IPPROTO_UDP) &&
4083 (!pnl->dport || !pnl->sport)))
4084 error = EINVAL;
4085 else {
4086 bzero(&key, sizeof(key));
4087 key.af = pnl->af;
4088 key.proto = pnl->proto;
4089 PF_ACPY(&key.addr[sidx], &pnl->saddr, pnl->af);
4090 key.port[sidx] = pnl->sport;
4091 PF_ACPY(&key.addr[didx], &pnl->daddr, pnl->af);
4092 key.port[didx] = pnl->dport;
4093
4094 state = pf_find_state_all(&key, direction, &m);
4095 if (state == NULL) {
4096 error = ENOENT;
4097 } else {
4098 if (m > 1) {
4099 PF_STATE_UNLOCK(state);
4100 error = E2BIG; /* more than one state */
4101 } else {
4102 sk = state->key[sidx];
4103 PF_ACPY(&pnl->rsaddr, &sk->addr[sidx], sk->af);
4104 pnl->rsport = sk->port[sidx];
4105 PF_ACPY(&pnl->rdaddr, &sk->addr[didx], sk->af);
4106 pnl->rdport = sk->port[didx];
4107 PF_STATE_UNLOCK(state);
4108 }
4109 }
4110 }
4111 break;
4112 }
4113
4114 case DIOCSETTIMEOUT: {
4115 struct pfioc_tm *pt = (struct pfioc_tm *)addr;
4116
4117 error = pf_ioctl_set_timeout(pt->timeout, pt->seconds,
4118 &pt->seconds);
4119 break;
4120 }
4121
4122 case DIOCGETTIMEOUT: {
4123 struct pfioc_tm *pt = (struct pfioc_tm *)addr;
4124
4125 error = pf_ioctl_get_timeout(pt->timeout, &pt->seconds);
4126 break;
4127 }
4128
4129 case DIOCGETLIMIT: {
4130 struct pfioc_limit *pl = (struct pfioc_limit *)addr;
4131
4132 error = pf_ioctl_get_limit(pl->index, &pl->limit);
4133 break;
4134 }
4135
4136 case DIOCSETLIMIT: {
4137 struct pfioc_limit *pl = (struct pfioc_limit *)addr;
4138 unsigned int old_limit;
4139
4140 error = pf_ioctl_set_limit(pl->index, pl->limit, &old_limit);
4141 pl->limit = old_limit;
4142 break;
4143 }
4144
4145 case DIOCSETDEBUG: {
4146 u_int32_t *level = (u_int32_t *)addr;
4147
4148 PF_RULES_WLOCK();
4149 V_pf_status.debug = *level;
4150 PF_RULES_WUNLOCK();
4151 break;
4152 }
4153
4154 case DIOCCLRRULECTRS: {
4155 /* obsoleted by DIOCGETRULE with action=PF_GET_CLR_CNTR */
4156 struct pf_kruleset *ruleset = &pf_main_ruleset;
4157 struct pf_krule *rule;
4158
4159 PF_RULES_WLOCK();
4160 TAILQ_FOREACH(rule,
4161 ruleset->rules[PF_RULESET_FILTER].active.ptr, entries) {
4162 pf_counter_u64_zero(&rule->evaluations);
4163 for (int i = 0; i < 2; i++) {
4164 pf_counter_u64_zero(&rule->packets[i]);
4165 pf_counter_u64_zero(&rule->bytes[i]);
4166 }
4167 }
4168 PF_RULES_WUNLOCK();
4169 break;
4170 }
4171
4172 case DIOCGIFSPEEDV0:
4173 case DIOCGIFSPEEDV1: {
4174 struct pf_ifspeed_v1 *psp = (struct pf_ifspeed_v1 *)addr;
4175 struct pf_ifspeed_v1 ps;
4176 struct ifnet *ifp;
4177
4178 if (psp->ifname[0] == '\0') {
4179 error = EINVAL;
4180 break;
4181 }
4182
4183 error = pf_user_strcpy(ps.ifname, psp->ifname, IFNAMSIZ);
4184 if (error != 0)
4185 break;
4186 ifp = ifunit(ps.ifname);
4187 if (ifp != NULL) {
4188 psp->baudrate32 =
4189 (u_int32_t)uqmin(ifp->if_baudrate, UINT_MAX);
4190 if (cmd == DIOCGIFSPEEDV1)
4191 psp->baudrate = ifp->if_baudrate;
4192 } else {
4193 error = EINVAL;
4194 }
4195 break;
4196 }
4197
4198 #ifdef ALTQ
4199 case DIOCSTARTALTQ: {
4200 struct pf_altq *altq;
4201
4202 PF_RULES_WLOCK();
4203 /* enable all altq interfaces on active list */
4204 TAILQ_FOREACH(altq, V_pf_altq_ifs_active, entries) {
4205 if ((altq->local_flags & PFALTQ_FLAG_IF_REMOVED) == 0) {
4206 error = pf_enable_altq(altq);
4207 if (error != 0)
4208 break;
4209 }
4210 }
4211 if (error == 0)
4212 V_pf_altq_running = 1;
4213 PF_RULES_WUNLOCK();
4214 DPFPRINTF(PF_DEBUG_MISC, ("altq: started\n"));
4215 break;
4216 }
4217
4218 case DIOCSTOPALTQ: {
4219 struct pf_altq *altq;
4220
4221 PF_RULES_WLOCK();
4222 /* disable all altq interfaces on active list */
4223 TAILQ_FOREACH(altq, V_pf_altq_ifs_active, entries) {
4224 if ((altq->local_flags & PFALTQ_FLAG_IF_REMOVED) == 0) {
4225 error = pf_disable_altq(altq);
4226 if (error != 0)
4227 break;
4228 }
4229 }
4230 if (error == 0)
4231 V_pf_altq_running = 0;
4232 PF_RULES_WUNLOCK();
4233 DPFPRINTF(PF_DEBUG_MISC, ("altq: stopped\n"));
4234 break;
4235 }
4236
4237 case DIOCADDALTQV0:
4238 case DIOCADDALTQV1: {
4239 struct pfioc_altq_v1 *pa = (struct pfioc_altq_v1 *)addr;
4240 struct pf_altq *altq, *a;
4241 struct ifnet *ifp;
4242
4243 altq = malloc(sizeof(*altq), M_PFALTQ, M_WAITOK | M_ZERO);
4244 error = pf_import_kaltq(pa, altq, IOCPARM_LEN(cmd));
4245 if (error)
4246 break;
4247 altq->local_flags = 0;
4248
4249 PF_RULES_WLOCK();
4250 if (pa->ticket != V_ticket_altqs_inactive) {
4251 PF_RULES_WUNLOCK();
4252 free(altq, M_PFALTQ);
4253 error = EBUSY;
4254 break;
4255 }
4256
4257 /*
4258 * if this is for a queue, find the discipline and
4259 * copy the necessary fields
4260 */
4261 if (altq->qname[0] != 0) {
4262 if ((altq->qid = pf_qname2qid(altq->qname)) == 0) {
4263 PF_RULES_WUNLOCK();
4264 error = EBUSY;
4265 free(altq, M_PFALTQ);
4266 break;
4267 }
4268 altq->altq_disc = NULL;
4269 TAILQ_FOREACH(a, V_pf_altq_ifs_inactive, entries) {
4270 if (strncmp(a->ifname, altq->ifname,
4271 IFNAMSIZ) == 0) {
4272 altq->altq_disc = a->altq_disc;
4273 break;
4274 }
4275 }
4276 }
4277
4278 if ((ifp = ifunit(altq->ifname)) == NULL)
4279 altq->local_flags |= PFALTQ_FLAG_IF_REMOVED;
4280 else
4281 error = altq_add(ifp, altq);
4282
4283 if (error) {
4284 PF_RULES_WUNLOCK();
4285 free(altq, M_PFALTQ);
4286 break;
4287 }
4288
4289 if (altq->qname[0] != 0)
4290 TAILQ_INSERT_TAIL(V_pf_altqs_inactive, altq, entries);
4291 else
4292 TAILQ_INSERT_TAIL(V_pf_altq_ifs_inactive, altq, entries);
4293 /* version error check done on import above */
4294 pf_export_kaltq(altq, pa, IOCPARM_LEN(cmd));
4295 PF_RULES_WUNLOCK();
4296 break;
4297 }
4298
4299 case DIOCGETALTQSV0:
4300 case DIOCGETALTQSV1: {
4301 struct pfioc_altq_v1 *pa = (struct pfioc_altq_v1 *)addr;
4302 struct pf_altq *altq;
4303
4304 PF_RULES_RLOCK();
4305 pa->nr = 0;
4306 TAILQ_FOREACH(altq, V_pf_altq_ifs_active, entries)
4307 pa->nr++;
4308 TAILQ_FOREACH(altq, V_pf_altqs_active, entries)
4309 pa->nr++;
4310 pa->ticket = V_ticket_altqs_active;
4311 PF_RULES_RUNLOCK();
4312 break;
4313 }
4314
4315 case DIOCGETALTQV0:
4316 case DIOCGETALTQV1: {
4317 struct pfioc_altq_v1 *pa = (struct pfioc_altq_v1 *)addr;
4318 struct pf_altq *altq;
4319
4320 PF_RULES_RLOCK();
4321 if (pa->ticket != V_ticket_altqs_active) {
4322 PF_RULES_RUNLOCK();
4323 error = EBUSY;
4324 break;
4325 }
4326 altq = pf_altq_get_nth_active(pa->nr);
4327 if (altq == NULL) {
4328 PF_RULES_RUNLOCK();
4329 error = EBUSY;
4330 break;
4331 }
4332 pf_export_kaltq(altq, pa, IOCPARM_LEN(cmd));
4333 PF_RULES_RUNLOCK();
4334 break;
4335 }
4336
4337 case DIOCCHANGEALTQV0:
4338 case DIOCCHANGEALTQV1:
4339 /* CHANGEALTQ not supported yet! */
4340 error = ENODEV;
4341 break;
4342
4343 case DIOCGETQSTATSV0:
4344 case DIOCGETQSTATSV1: {
4345 struct pfioc_qstats_v1 *pq = (struct pfioc_qstats_v1 *)addr;
4346 struct pf_altq *altq;
4347 int nbytes;
4348 u_int32_t version;
4349
4350 PF_RULES_RLOCK();
4351 if (pq->ticket != V_ticket_altqs_active) {
4352 PF_RULES_RUNLOCK();
4353 error = EBUSY;
4354 break;
4355 }
4356 nbytes = pq->nbytes;
4357 altq = pf_altq_get_nth_active(pq->nr);
4358 if (altq == NULL) {
4359 PF_RULES_RUNLOCK();
4360 error = EBUSY;
4361 break;
4362 }
4363
4364 if ((altq->local_flags & PFALTQ_FLAG_IF_REMOVED) != 0) {
4365 PF_RULES_RUNLOCK();
4366 error = ENXIO;
4367 break;
4368 }
4369 PF_RULES_RUNLOCK();
4370 if (cmd == DIOCGETQSTATSV0)
4371 version = 0; /* DIOCGETQSTATSV0 means stats struct v0 */
4372 else
4373 version = pq->version;
4374 error = altq_getqstats(altq, pq->buf, &nbytes, version);
4375 if (error == 0) {
4376 pq->scheduler = altq->scheduler;
4377 pq->nbytes = nbytes;
4378 }
4379 break;
4380 }
4381 #endif /* ALTQ */
4382
4383 case DIOCBEGINADDRS: {
4384 struct pfioc_pooladdr *pp = (struct pfioc_pooladdr *)addr;
4385
4386 error = pf_ioctl_begin_addrs(&pp->ticket);
4387 break;
4388 }
4389
4390 case DIOCADDADDR: {
4391 struct pfioc_pooladdr *pp = (struct pfioc_pooladdr *)addr;
4392
4393 error = pf_ioctl_add_addr(pp);
4394 break;
4395 }
4396
4397 case DIOCGETADDRS: {
4398 struct pfioc_pooladdr *pp = (struct pfioc_pooladdr *)addr;
4399
4400 error = pf_ioctl_get_addrs(pp);
4401 break;
4402 }
4403
4404 case DIOCGETADDR: {
4405 struct pfioc_pooladdr *pp = (struct pfioc_pooladdr *)addr;
4406
4407 error = pf_ioctl_get_addr(pp);
4408 break;
4409 }
4410
4411 case DIOCCHANGEADDR: {
4412 struct pfioc_pooladdr *pca = (struct pfioc_pooladdr *)addr;
4413 struct pf_kpool *pool;
4414 struct pf_kpooladdr *oldpa = NULL, *newpa = NULL;
4415 struct pf_kruleset *ruleset;
4416 struct pfi_kkif *kif = NULL;
4417
4418 pca->anchor[sizeof(pca->anchor) - 1] = 0;
4419
4420 if (pca->action < PF_CHANGE_ADD_HEAD ||
4421 pca->action > PF_CHANGE_REMOVE) {
4422 error = EINVAL;
4423 break;
4424 }
4425 if (pca->addr.addr.type != PF_ADDR_ADDRMASK &&
4426 pca->addr.addr.type != PF_ADDR_DYNIFTL &&
4427 pca->addr.addr.type != PF_ADDR_TABLE) {
4428 error = EINVAL;
4429 break;
4430 }
4431 if (pca->addr.addr.p.dyn != NULL) {
4432 error = EINVAL;
4433 break;
4434 }
4435
4436 if (pca->action != PF_CHANGE_REMOVE) {
4437 #ifndef INET
4438 if (pca->af == AF_INET) {
4439 error = EAFNOSUPPORT;
4440 break;
4441 }
4442 #endif /* INET */
4443 #ifndef INET6
4444 if (pca->af == AF_INET6) {
4445 error = EAFNOSUPPORT;
4446 break;
4447 }
4448 #endif /* INET6 */
4449 newpa = malloc(sizeof(*newpa), M_PFRULE, M_WAITOK);
4450 bcopy(&pca->addr, newpa, sizeof(struct pf_pooladdr));
4451 if (newpa->ifname[0])
4452 kif = pf_kkif_create(M_WAITOK);
4453 newpa->kif = NULL;
4454 }
4455 #define ERROUT(x) ERROUT_IOCTL(DIOCCHANGEADDR_error, x)
4456 PF_RULES_WLOCK();
4457 ruleset = pf_find_kruleset(pca->anchor);
4458 if (ruleset == NULL)
4459 ERROUT(EBUSY);
4460
4461 pool = pf_get_kpool(pca->anchor, pca->ticket, pca->r_action,
4462 pca->r_num, pca->r_last, 1, 1);
4463 if (pool == NULL)
4464 ERROUT(EBUSY);
4465
4466 if (pca->action != PF_CHANGE_REMOVE) {
4467 if (newpa->ifname[0]) {
4468 newpa->kif = pfi_kkif_attach(kif, newpa->ifname);
4469 pfi_kkif_ref(newpa->kif);
4470 kif = NULL;
4471 }
4472
4473 switch (newpa->addr.type) {
4474 case PF_ADDR_DYNIFTL:
4475 error = pfi_dynaddr_setup(&newpa->addr,
4476 pca->af);
4477 break;
4478 case PF_ADDR_TABLE:
4479 newpa->addr.p.tbl = pfr_attach_table(ruleset,
4480 newpa->addr.v.tblname);
4481 if (newpa->addr.p.tbl == NULL)
4482 error = ENOMEM;
4483 break;
4484 }
4485 if (error)
4486 goto DIOCCHANGEADDR_error;
4487 }
4488
4489 switch (pca->action) {
4490 case PF_CHANGE_ADD_HEAD:
4491 oldpa = TAILQ_FIRST(&pool->list);
4492 break;
4493 case PF_CHANGE_ADD_TAIL:
4494 oldpa = TAILQ_LAST(&pool->list, pf_kpalist);
4495 break;
4496 default:
4497 oldpa = TAILQ_FIRST(&pool->list);
4498 for (int i = 0; oldpa && i < pca->nr; i++)
4499 oldpa = TAILQ_NEXT(oldpa, entries);
4500
4501 if (oldpa == NULL)
4502 ERROUT(EINVAL);
4503 }
4504
4505 if (pca->action == PF_CHANGE_REMOVE) {
4506 TAILQ_REMOVE(&pool->list, oldpa, entries);
4507 switch (oldpa->addr.type) {
4508 case PF_ADDR_DYNIFTL:
4509 pfi_dynaddr_remove(oldpa->addr.p.dyn);
4510 break;
4511 case PF_ADDR_TABLE:
4512 pfr_detach_table(oldpa->addr.p.tbl);
4513 break;
4514 }
4515 if (oldpa->kif)
4516 pfi_kkif_unref(oldpa->kif);
4517 free(oldpa, M_PFRULE);
4518 } else {
4519 if (oldpa == NULL)
4520 TAILQ_INSERT_TAIL(&pool->list, newpa, entries);
4521 else if (pca->action == PF_CHANGE_ADD_HEAD ||
4522 pca->action == PF_CHANGE_ADD_BEFORE)
4523 TAILQ_INSERT_BEFORE(oldpa, newpa, entries);
4524 else
4525 TAILQ_INSERT_AFTER(&pool->list, oldpa,
4526 newpa, entries);
4527 }
4528
4529 pool->cur = TAILQ_FIRST(&pool->list);
4530 PF_ACPY(&pool->counter, &pool->cur->addr.v.a.addr, pca->af);
4531 PF_RULES_WUNLOCK();
4532 break;
4533
4534 #undef ERROUT
4535 DIOCCHANGEADDR_error:
4536 if (newpa != NULL) {
4537 if (newpa->kif)
4538 pfi_kkif_unref(newpa->kif);
4539 free(newpa, M_PFRULE);
4540 }
4541 PF_RULES_WUNLOCK();
4542 pf_kkif_free(kif);
4543 break;
4544 }
4545
4546 case DIOCGETRULESETS: {
4547 struct pfioc_ruleset *pr = (struct pfioc_ruleset *)addr;
4548
4549 pr->path[sizeof(pr->path) - 1] = 0;
4550
4551 error = pf_ioctl_get_rulesets(pr);
4552 break;
4553 }
4554
4555 case DIOCGETRULESET: {
4556 struct pfioc_ruleset *pr = (struct pfioc_ruleset *)addr;
4557
4558 pr->path[sizeof(pr->path) - 1] = 0;
4559
4560 error = pf_ioctl_get_ruleset(pr);
4561 break;
4562 }
4563
4564 case DIOCRCLRTABLES: {
4565 struct pfioc_table *io = (struct pfioc_table *)addr;
4566
4567 if (io->pfrio_esize != 0) {
4568 error = ENODEV;
4569 break;
4570 }
4571 PF_RULES_WLOCK();
4572 error = pfr_clr_tables(&io->pfrio_table, &io->pfrio_ndel,
4573 io->pfrio_flags | PFR_FLAG_USERIOCTL);
4574 PF_RULES_WUNLOCK();
4575 break;
4576 }
4577
4578 case DIOCRADDTABLES: {
4579 struct pfioc_table *io = (struct pfioc_table *)addr;
4580 struct pfr_table *pfrts;
4581 size_t totlen;
4582
4583 if (io->pfrio_esize != sizeof(struct pfr_table)) {
4584 error = ENODEV;
4585 break;
4586 }
4587
4588 if (io->pfrio_size < 0 || io->pfrio_size > pf_ioctl_maxcount ||
4589 WOULD_OVERFLOW(io->pfrio_size, sizeof(struct pfr_table))) {
4590 error = ENOMEM;
4591 break;
4592 }
4593
4594 totlen = io->pfrio_size * sizeof(struct pfr_table);
4595 pfrts = mallocarray(io->pfrio_size, sizeof(struct pfr_table),
4596 M_TEMP, M_WAITOK);
4597 error = copyin(io->pfrio_buffer, pfrts, totlen);
4598 if (error) {
4599 free(pfrts, M_TEMP);
4600 break;
4601 }
4602 PF_RULES_WLOCK();
4603 error = pfr_add_tables(pfrts, io->pfrio_size,
4604 &io->pfrio_nadd, io->pfrio_flags | PFR_FLAG_USERIOCTL);
4605 PF_RULES_WUNLOCK();
4606 free(pfrts, M_TEMP);
4607 break;
4608 }
4609
4610 case DIOCRDELTABLES: {
4611 struct pfioc_table *io = (struct pfioc_table *)addr;
4612 struct pfr_table *pfrts;
4613 size_t totlen;
4614
4615 if (io->pfrio_esize != sizeof(struct pfr_table)) {
4616 error = ENODEV;
4617 break;
4618 }
4619
4620 if (io->pfrio_size < 0 || io->pfrio_size > pf_ioctl_maxcount ||
4621 WOULD_OVERFLOW(io->pfrio_size, sizeof(struct pfr_table))) {
4622 error = ENOMEM;
4623 break;
4624 }
4625
4626 totlen = io->pfrio_size * sizeof(struct pfr_table);
4627 pfrts = mallocarray(io->pfrio_size, sizeof(struct pfr_table),
4628 M_TEMP, M_WAITOK);
4629 error = copyin(io->pfrio_buffer, pfrts, totlen);
4630 if (error) {
4631 free(pfrts, M_TEMP);
4632 break;
4633 }
4634 PF_RULES_WLOCK();
4635 error = pfr_del_tables(pfrts, io->pfrio_size,
4636 &io->pfrio_ndel, io->pfrio_flags | PFR_FLAG_USERIOCTL);
4637 PF_RULES_WUNLOCK();
4638 free(pfrts, M_TEMP);
4639 break;
4640 }
4641
4642 case DIOCRGETTABLES: {
4643 struct pfioc_table *io = (struct pfioc_table *)addr;
4644 struct pfr_table *pfrts;
4645 size_t totlen;
4646 int n;
4647
4648 if (io->pfrio_esize != sizeof(struct pfr_table)) {
4649 error = ENODEV;
4650 break;
4651 }
4652 PF_RULES_RLOCK();
4653 n = pfr_table_count(&io->pfrio_table, io->pfrio_flags);
4654 if (n < 0) {
4655 PF_RULES_RUNLOCK();
4656 error = EINVAL;
4657 break;
4658 }
4659 io->pfrio_size = min(io->pfrio_size, n);
4660
4661 totlen = io->pfrio_size * sizeof(struct pfr_table);
4662
4663 pfrts = mallocarray(io->pfrio_size, sizeof(struct pfr_table),
4664 M_TEMP, M_NOWAIT | M_ZERO);
4665 if (pfrts == NULL) {
4666 error = ENOMEM;
4667 PF_RULES_RUNLOCK();
4668 break;
4669 }
4670 error = pfr_get_tables(&io->pfrio_table, pfrts,
4671 &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL);
4672 PF_RULES_RUNLOCK();
4673 if (error == 0)
4674 error = copyout(pfrts, io->pfrio_buffer, totlen);
4675 free(pfrts, M_TEMP);
4676 break;
4677 }
4678
4679 case DIOCRGETTSTATS: {
4680 struct pfioc_table *io = (struct pfioc_table *)addr;
4681 struct pfr_tstats *pfrtstats;
4682 size_t totlen;
4683 int n;
4684
4685 if (io->pfrio_esize != sizeof(struct pfr_tstats)) {
4686 error = ENODEV;
4687 break;
4688 }
4689 PF_TABLE_STATS_LOCK();
4690 PF_RULES_RLOCK();
4691 n = pfr_table_count(&io->pfrio_table, io->pfrio_flags);
4692 if (n < 0) {
4693 PF_RULES_RUNLOCK();
4694 PF_TABLE_STATS_UNLOCK();
4695 error = EINVAL;
4696 break;
4697 }
4698 io->pfrio_size = min(io->pfrio_size, n);
4699
4700 totlen = io->pfrio_size * sizeof(struct pfr_tstats);
4701 pfrtstats = mallocarray(io->pfrio_size,
4702 sizeof(struct pfr_tstats), M_TEMP, M_NOWAIT | M_ZERO);
4703 if (pfrtstats == NULL) {
4704 error = ENOMEM;
4705 PF_RULES_RUNLOCK();
4706 PF_TABLE_STATS_UNLOCK();
4707 break;
4708 }
4709 error = pfr_get_tstats(&io->pfrio_table, pfrtstats,
4710 &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL);
4711 PF_RULES_RUNLOCK();
4712 PF_TABLE_STATS_UNLOCK();
4713 if (error == 0)
4714 error = copyout(pfrtstats, io->pfrio_buffer, totlen);
4715 free(pfrtstats, M_TEMP);
4716 break;
4717 }
4718
4719 case DIOCRCLRTSTATS: {
4720 struct pfioc_table *io = (struct pfioc_table *)addr;
4721 struct pfr_table *pfrts;
4722 size_t totlen;
4723
4724 if (io->pfrio_esize != sizeof(struct pfr_table)) {
4725 error = ENODEV;
4726 break;
4727 }
4728
4729 if (io->pfrio_size < 0 || io->pfrio_size > pf_ioctl_maxcount ||
4730 WOULD_OVERFLOW(io->pfrio_size, sizeof(struct pfr_table))) {
4731 /* We used to count tables and use the minimum required
4732 * size, so we didn't fail on overly large requests.
4733 * Keep doing so. */
4734 io->pfrio_size = pf_ioctl_maxcount;
4735 break;
4736 }
4737
4738 totlen = io->pfrio_size * sizeof(struct pfr_table);
4739 pfrts = mallocarray(io->pfrio_size, sizeof(struct pfr_table),
4740 M_TEMP, M_WAITOK);
4741 error = copyin(io->pfrio_buffer, pfrts, totlen);
4742 if (error) {
4743 free(pfrts, M_TEMP);
4744 break;
4745 }
4746
4747 PF_TABLE_STATS_LOCK();
4748 PF_RULES_RLOCK();
4749 error = pfr_clr_tstats(pfrts, io->pfrio_size,
4750 &io->pfrio_nzero, io->pfrio_flags | PFR_FLAG_USERIOCTL);
4751 PF_RULES_RUNLOCK();
4752 PF_TABLE_STATS_UNLOCK();
4753 free(pfrts, M_TEMP);
4754 break;
4755 }
4756
4757 case DIOCRSETTFLAGS: {
4758 struct pfioc_table *io = (struct pfioc_table *)addr;
4759 struct pfr_table *pfrts;
4760 size_t totlen;
4761 int n;
4762
4763 if (io->pfrio_esize != sizeof(struct pfr_table)) {
4764 error = ENODEV;
4765 break;
4766 }
4767
4768 PF_RULES_RLOCK();
4769 n = pfr_table_count(&io->pfrio_table, io->pfrio_flags);
4770 if (n < 0) {
4771 PF_RULES_RUNLOCK();
4772 error = EINVAL;
4773 break;
4774 }
4775
4776 io->pfrio_size = min(io->pfrio_size, n);
4777 PF_RULES_RUNLOCK();
4778
4779 totlen = io->pfrio_size * sizeof(struct pfr_table);
4780 pfrts = mallocarray(io->pfrio_size, sizeof(struct pfr_table),
4781 M_TEMP, M_WAITOK);
4782 error = copyin(io->pfrio_buffer, pfrts, totlen);
4783 if (error) {
4784 free(pfrts, M_TEMP);
4785 break;
4786 }
4787 PF_RULES_WLOCK();
4788 error = pfr_set_tflags(pfrts, io->pfrio_size,
4789 io->pfrio_setflag, io->pfrio_clrflag, &io->pfrio_nchange,
4790 &io->pfrio_ndel, io->pfrio_flags | PFR_FLAG_USERIOCTL);
4791 PF_RULES_WUNLOCK();
4792 free(pfrts, M_TEMP);
4793 break;
4794 }
4795
4796 case DIOCRCLRADDRS: {
4797 struct pfioc_table *io = (struct pfioc_table *)addr;
4798
4799 if (io->pfrio_esize != 0) {
4800 error = ENODEV;
4801 break;
4802 }
4803 PF_RULES_WLOCK();
4804 error = pfr_clr_addrs(&io->pfrio_table, &io->pfrio_ndel,
4805 io->pfrio_flags | PFR_FLAG_USERIOCTL);
4806 PF_RULES_WUNLOCK();
4807 break;
4808 }
4809
4810 case DIOCRADDADDRS: {
4811 struct pfioc_table *io = (struct pfioc_table *)addr;
4812 struct pfr_addr *pfras;
4813 size_t totlen;
4814
4815 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
4816 error = ENODEV;
4817 break;
4818 }
4819 if (io->pfrio_size < 0 ||
4820 io->pfrio_size > pf_ioctl_maxcount ||
4821 WOULD_OVERFLOW(io->pfrio_size, sizeof(struct pfr_addr))) {
4822 error = EINVAL;
4823 break;
4824 }
4825 totlen = io->pfrio_size * sizeof(struct pfr_addr);
4826 pfras = mallocarray(io->pfrio_size, sizeof(struct pfr_addr),
4827 M_TEMP, M_WAITOK);
4828 error = copyin(io->pfrio_buffer, pfras, totlen);
4829 if (error) {
4830 free(pfras, M_TEMP);
4831 break;
4832 }
4833 PF_RULES_WLOCK();
4834 error = pfr_add_addrs(&io->pfrio_table, pfras,
4835 io->pfrio_size, &io->pfrio_nadd, io->pfrio_flags |
4836 PFR_FLAG_USERIOCTL);
4837 PF_RULES_WUNLOCK();
4838 if (error == 0 && io->pfrio_flags & PFR_FLAG_FEEDBACK)
4839 error = copyout(pfras, io->pfrio_buffer, totlen);
4840 free(pfras, M_TEMP);
4841 break;
4842 }
4843
4844 case DIOCRDELADDRS: {
4845 struct pfioc_table *io = (struct pfioc_table *)addr;
4846 struct pfr_addr *pfras;
4847 size_t totlen;
4848
4849 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
4850 error = ENODEV;
4851 break;
4852 }
4853 if (io->pfrio_size < 0 ||
4854 io->pfrio_size > pf_ioctl_maxcount ||
4855 WOULD_OVERFLOW(io->pfrio_size, sizeof(struct pfr_addr))) {
4856 error = EINVAL;
4857 break;
4858 }
4859 totlen = io->pfrio_size * sizeof(struct pfr_addr);
4860 pfras = mallocarray(io->pfrio_size, sizeof(struct pfr_addr),
4861 M_TEMP, M_WAITOK);
4862 error = copyin(io->pfrio_buffer, pfras, totlen);
4863 if (error) {
4864 free(pfras, M_TEMP);
4865 break;
4866 }
4867 PF_RULES_WLOCK();
4868 error = pfr_del_addrs(&io->pfrio_table, pfras,
4869 io->pfrio_size, &io->pfrio_ndel, io->pfrio_flags |
4870 PFR_FLAG_USERIOCTL);
4871 PF_RULES_WUNLOCK();
4872 if (error == 0 && io->pfrio_flags & PFR_FLAG_FEEDBACK)
4873 error = copyout(pfras, io->pfrio_buffer, totlen);
4874 free(pfras, M_TEMP);
4875 break;
4876 }
4877
4878 case DIOCRSETADDRS: {
4879 struct pfioc_table *io = (struct pfioc_table *)addr;
4880 struct pfr_addr *pfras;
4881 size_t totlen, count;
4882
4883 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
4884 error = ENODEV;
4885 break;
4886 }
4887 if (io->pfrio_size < 0 || io->pfrio_size2 < 0) {
4888 error = EINVAL;
4889 break;
4890 }
4891 count = max(io->pfrio_size, io->pfrio_size2);
4892 if (count > pf_ioctl_maxcount ||
4893 WOULD_OVERFLOW(count, sizeof(struct pfr_addr))) {
4894 error = EINVAL;
4895 break;
4896 }
4897 totlen = count * sizeof(struct pfr_addr);
4898 pfras = mallocarray(count, sizeof(struct pfr_addr), M_TEMP,
4899 M_WAITOK);
4900 error = copyin(io->pfrio_buffer, pfras, totlen);
4901 if (error) {
4902 free(pfras, M_TEMP);
4903 break;
4904 }
4905 PF_RULES_WLOCK();
4906 error = pfr_set_addrs(&io->pfrio_table, pfras,
4907 io->pfrio_size, &io->pfrio_size2, &io->pfrio_nadd,
4908 &io->pfrio_ndel, &io->pfrio_nchange, io->pfrio_flags |
4909 PFR_FLAG_USERIOCTL, 0);
4910 PF_RULES_WUNLOCK();
4911 if (error == 0 && io->pfrio_flags & PFR_FLAG_FEEDBACK)
4912 error = copyout(pfras, io->pfrio_buffer, totlen);
4913 free(pfras, M_TEMP);
4914 break;
4915 }
4916
4917 case DIOCRGETADDRS: {
4918 struct pfioc_table *io = (struct pfioc_table *)addr;
4919 struct pfr_addr *pfras;
4920 size_t totlen;
4921
4922 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
4923 error = ENODEV;
4924 break;
4925 }
4926 if (io->pfrio_size < 0 ||
4927 io->pfrio_size > pf_ioctl_maxcount ||
4928 WOULD_OVERFLOW(io->pfrio_size, sizeof(struct pfr_addr))) {
4929 error = EINVAL;
4930 break;
4931 }
4932 totlen = io->pfrio_size * sizeof(struct pfr_addr);
4933 pfras = mallocarray(io->pfrio_size, sizeof(struct pfr_addr),
4934 M_TEMP, M_WAITOK | M_ZERO);
4935 PF_RULES_RLOCK();
4936 error = pfr_get_addrs(&io->pfrio_table, pfras,
4937 &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL);
4938 PF_RULES_RUNLOCK();
4939 if (error == 0)
4940 error = copyout(pfras, io->pfrio_buffer, totlen);
4941 free(pfras, M_TEMP);
4942 break;
4943 }
4944
4945 case DIOCRGETASTATS: {
4946 struct pfioc_table *io = (struct pfioc_table *)addr;
4947 struct pfr_astats *pfrastats;
4948 size_t totlen;
4949
4950 if (io->pfrio_esize != sizeof(struct pfr_astats)) {
4951 error = ENODEV;
4952 break;
4953 }
4954 if (io->pfrio_size < 0 ||
4955 io->pfrio_size > pf_ioctl_maxcount ||
4956 WOULD_OVERFLOW(io->pfrio_size, sizeof(struct pfr_astats))) {
4957 error = EINVAL;
4958 break;
4959 }
4960 totlen = io->pfrio_size * sizeof(struct pfr_astats);
4961 pfrastats = mallocarray(io->pfrio_size,
4962 sizeof(struct pfr_astats), M_TEMP, M_WAITOK | M_ZERO);
4963 PF_RULES_RLOCK();
4964 error = pfr_get_astats(&io->pfrio_table, pfrastats,
4965 &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL);
4966 PF_RULES_RUNLOCK();
4967 if (error == 0)
4968 error = copyout(pfrastats, io->pfrio_buffer, totlen);
4969 free(pfrastats, M_TEMP);
4970 break;
4971 }
4972
4973 case DIOCRCLRASTATS: {
4974 struct pfioc_table *io = (struct pfioc_table *)addr;
4975 struct pfr_addr *pfras;
4976 size_t totlen;
4977
4978 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
4979 error = ENODEV;
4980 break;
4981 }
4982 if (io->pfrio_size < 0 ||
4983 io->pfrio_size > pf_ioctl_maxcount ||
4984 WOULD_OVERFLOW(io->pfrio_size, sizeof(struct pfr_addr))) {
4985 error = EINVAL;
4986 break;
4987 }
4988 totlen = io->pfrio_size * sizeof(struct pfr_addr);
4989 pfras = mallocarray(io->pfrio_size, sizeof(struct pfr_addr),
4990 M_TEMP, M_WAITOK);
4991 error = copyin(io->pfrio_buffer, pfras, totlen);
4992 if (error) {
4993 free(pfras, M_TEMP);
4994 break;
4995 }
4996 PF_RULES_WLOCK();
4997 error = pfr_clr_astats(&io->pfrio_table, pfras,
4998 io->pfrio_size, &io->pfrio_nzero, io->pfrio_flags |
4999 PFR_FLAG_USERIOCTL);
5000 PF_RULES_WUNLOCK();
5001 if (error == 0 && io->pfrio_flags & PFR_FLAG_FEEDBACK)
5002 error = copyout(pfras, io->pfrio_buffer, totlen);
5003 free(pfras, M_TEMP);
5004 break;
5005 }
5006
5007 case DIOCRTSTADDRS: {
5008 struct pfioc_table *io = (struct pfioc_table *)addr;
5009 struct pfr_addr *pfras;
5010 size_t totlen;
5011
5012 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
5013 error = ENODEV;
5014 break;
5015 }
5016 if (io->pfrio_size < 0 ||
5017 io->pfrio_size > pf_ioctl_maxcount ||
5018 WOULD_OVERFLOW(io->pfrio_size, sizeof(struct pfr_addr))) {
5019 error = EINVAL;
5020 break;
5021 }
5022 totlen = io->pfrio_size * sizeof(struct pfr_addr);
5023 pfras = mallocarray(io->pfrio_size, sizeof(struct pfr_addr),
5024 M_TEMP, M_WAITOK);
5025 error = copyin(io->pfrio_buffer, pfras, totlen);
5026 if (error) {
5027 free(pfras, M_TEMP);
5028 break;
5029 }
5030 PF_RULES_RLOCK();
5031 error = pfr_tst_addrs(&io->pfrio_table, pfras,
5032 io->pfrio_size, &io->pfrio_nmatch, io->pfrio_flags |
5033 PFR_FLAG_USERIOCTL);
5034 PF_RULES_RUNLOCK();
5035 if (error == 0)
5036 error = copyout(pfras, io->pfrio_buffer, totlen);
5037 free(pfras, M_TEMP);
5038 break;
5039 }
5040
5041 case DIOCRINADEFINE: {
5042 struct pfioc_table *io = (struct pfioc_table *)addr;
5043 struct pfr_addr *pfras;
5044 size_t totlen;
5045
5046 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
5047 error = ENODEV;
5048 break;
5049 }
5050 if (io->pfrio_size < 0 ||
5051 io->pfrio_size > pf_ioctl_maxcount ||
5052 WOULD_OVERFLOW(io->pfrio_size, sizeof(struct pfr_addr))) {
5053 error = EINVAL;
5054 break;
5055 }
5056 totlen = io->pfrio_size * sizeof(struct pfr_addr);
5057 pfras = mallocarray(io->pfrio_size, sizeof(struct pfr_addr),
5058 M_TEMP, M_WAITOK);
5059 error = copyin(io->pfrio_buffer, pfras, totlen);
5060 if (error) {
5061 free(pfras, M_TEMP);
5062 break;
5063 }
5064 PF_RULES_WLOCK();
5065 error = pfr_ina_define(&io->pfrio_table, pfras,
5066 io->pfrio_size, &io->pfrio_nadd, &io->pfrio_naddr,
5067 io->pfrio_ticket, io->pfrio_flags | PFR_FLAG_USERIOCTL);
5068 PF_RULES_WUNLOCK();
5069 free(pfras, M_TEMP);
5070 break;
5071 }
5072
5073 case DIOCOSFPADD: {
5074 struct pf_osfp_ioctl *io = (struct pf_osfp_ioctl *)addr;
5075 PF_RULES_WLOCK();
5076 error = pf_osfp_add(io);
5077 PF_RULES_WUNLOCK();
5078 break;
5079 }
5080
5081 case DIOCOSFPGET: {
5082 struct pf_osfp_ioctl *io = (struct pf_osfp_ioctl *)addr;
5083 PF_RULES_RLOCK();
5084 error = pf_osfp_get(io);
5085 PF_RULES_RUNLOCK();
5086 break;
5087 }
5088
5089 case DIOCXBEGIN: {
5090 struct pfioc_trans *io = (struct pfioc_trans *)addr;
5091 struct pfioc_trans_e *ioes, *ioe;
5092 size_t totlen;
5093 int i;
5094
5095 if (io->esize != sizeof(*ioe)) {
5096 error = ENODEV;
5097 break;
5098 }
5099 if (io->size < 0 ||
5100 io->size > pf_ioctl_maxcount ||
5101 WOULD_OVERFLOW(io->size, sizeof(struct pfioc_trans_e))) {
5102 error = EINVAL;
5103 break;
5104 }
5105 totlen = sizeof(struct pfioc_trans_e) * io->size;
5106 ioes = mallocarray(io->size, sizeof(struct pfioc_trans_e),
5107 M_TEMP, M_WAITOK);
5108 error = copyin(io->array, ioes, totlen);
5109 if (error) {
5110 free(ioes, M_TEMP);
5111 break;
5112 }
5113 /* Ensure there's no more ethernet rules to clean up. */
5114 NET_EPOCH_DRAIN_CALLBACKS();
5115 PF_RULES_WLOCK();
5116 for (i = 0, ioe = ioes; i < io->size; i++, ioe++) {
5117 ioe->anchor[sizeof(ioe->anchor) - 1] = '\0';
5118 switch (ioe->rs_num) {
5119 case PF_RULESET_ETH:
5120 if ((error = pf_begin_eth(&ioe->ticket, ioe->anchor))) {
5121 PF_RULES_WUNLOCK();
5122 free(ioes, M_TEMP);
5123 goto fail;
5124 }
5125 break;
5126 #ifdef ALTQ
5127 case PF_RULESET_ALTQ:
5128 if (ioe->anchor[0]) {
5129 PF_RULES_WUNLOCK();
5130 free(ioes, M_TEMP);
5131 error = EINVAL;
5132 goto fail;
5133 }
5134 if ((error = pf_begin_altq(&ioe->ticket))) {
5135 PF_RULES_WUNLOCK();
5136 free(ioes, M_TEMP);
5137 goto fail;
5138 }
5139 break;
5140 #endif /* ALTQ */
5141 case PF_RULESET_TABLE:
5142 {
5143 struct pfr_table table;
5144
5145 bzero(&table, sizeof(table));
5146 strlcpy(table.pfrt_anchor, ioe->anchor,
5147 sizeof(table.pfrt_anchor));
5148 if ((error = pfr_ina_begin(&table,
5149 &ioe->ticket, NULL, 0))) {
5150 PF_RULES_WUNLOCK();
5151 free(ioes, M_TEMP);
5152 goto fail;
5153 }
5154 break;
5155 }
5156 default:
5157 if ((error = pf_begin_rules(&ioe->ticket,
5158 ioe->rs_num, ioe->anchor))) {
5159 PF_RULES_WUNLOCK();
5160 free(ioes, M_TEMP);
5161 goto fail;
5162 }
5163 break;
5164 }
5165 }
5166 PF_RULES_WUNLOCK();
5167 error = copyout(ioes, io->array, totlen);
5168 free(ioes, M_TEMP);
5169 break;
5170 }
5171
5172 case DIOCXROLLBACK: {
5173 struct pfioc_trans *io = (struct pfioc_trans *)addr;
5174 struct pfioc_trans_e *ioe, *ioes;
5175 size_t totlen;
5176 int i;
5177
5178 if (io->esize != sizeof(*ioe)) {
5179 error = ENODEV;
5180 break;
5181 }
5182 if (io->size < 0 ||
5183 io->size > pf_ioctl_maxcount ||
5184 WOULD_OVERFLOW(io->size, sizeof(struct pfioc_trans_e))) {
5185 error = EINVAL;
5186 break;
5187 }
5188 totlen = sizeof(struct pfioc_trans_e) * io->size;
5189 ioes = mallocarray(io->size, sizeof(struct pfioc_trans_e),
5190 M_TEMP, M_WAITOK);
5191 error = copyin(io->array, ioes, totlen);
5192 if (error) {
5193 free(ioes, M_TEMP);
5194 break;
5195 }
5196 PF_RULES_WLOCK();
5197 for (i = 0, ioe = ioes; i < io->size; i++, ioe++) {
5198 ioe->anchor[sizeof(ioe->anchor) - 1] = '\0';
5199 switch (ioe->rs_num) {
5200 case PF_RULESET_ETH:
5201 if ((error = pf_rollback_eth(ioe->ticket,
5202 ioe->anchor))) {
5203 PF_RULES_WUNLOCK();
5204 free(ioes, M_TEMP);
5205 goto fail; /* really bad */
5206 }
5207 break;
5208 #ifdef ALTQ
5209 case PF_RULESET_ALTQ:
5210 if (ioe->anchor[0]) {
5211 PF_RULES_WUNLOCK();
5212 free(ioes, M_TEMP);
5213 error = EINVAL;
5214 goto fail;
5215 }
5216 if ((error = pf_rollback_altq(ioe->ticket))) {
5217 PF_RULES_WUNLOCK();
5218 free(ioes, M_TEMP);
5219 goto fail; /* really bad */
5220 }
5221 break;
5222 #endif /* ALTQ */
5223 case PF_RULESET_TABLE:
5224 {
5225 struct pfr_table table;
5226
5227 bzero(&table, sizeof(table));
5228 strlcpy(table.pfrt_anchor, ioe->anchor,
5229 sizeof(table.pfrt_anchor));
5230 if ((error = pfr_ina_rollback(&table,
5231 ioe->ticket, NULL, 0))) {
5232 PF_RULES_WUNLOCK();
5233 free(ioes, M_TEMP);
5234 goto fail; /* really bad */
5235 }
5236 break;
5237 }
5238 default:
5239 if ((error = pf_rollback_rules(ioe->ticket,
5240 ioe->rs_num, ioe->anchor))) {
5241 PF_RULES_WUNLOCK();
5242 free(ioes, M_TEMP);
5243 goto fail; /* really bad */
5244 }
5245 break;
5246 }
5247 }
5248 PF_RULES_WUNLOCK();
5249 free(ioes, M_TEMP);
5250 break;
5251 }
5252
5253 case DIOCXCOMMIT: {
5254 struct pfioc_trans *io = (struct pfioc_trans *)addr;
5255 struct pfioc_trans_e *ioe, *ioes;
5256 struct pf_kruleset *rs;
5257 struct pf_keth_ruleset *ers;
5258 size_t totlen;
5259 int i;
5260
5261 if (io->esize != sizeof(*ioe)) {
5262 error = ENODEV;
5263 break;
5264 }
5265
5266 if (io->size < 0 ||
5267 io->size > pf_ioctl_maxcount ||
5268 WOULD_OVERFLOW(io->size, sizeof(struct pfioc_trans_e))) {
5269 error = EINVAL;
5270 break;
5271 }
5272
5273 totlen = sizeof(struct pfioc_trans_e) * io->size;
5274 ioes = mallocarray(io->size, sizeof(struct pfioc_trans_e),
5275 M_TEMP, M_WAITOK);
5276 error = copyin(io->array, ioes, totlen);
5277 if (error) {
5278 free(ioes, M_TEMP);
5279 break;
5280 }
5281 PF_RULES_WLOCK();
5282 /* First makes sure everything will succeed. */
5283 for (i = 0, ioe = ioes; i < io->size; i++, ioe++) {
5284 ioe->anchor[sizeof(ioe->anchor) - 1] = 0;
5285 switch (ioe->rs_num) {
5286 case PF_RULESET_ETH:
5287 ers = pf_find_keth_ruleset(ioe->anchor);
5288 if (ers == NULL || ioe->ticket == 0 ||
5289 ioe->ticket != ers->inactive.ticket) {
5290 PF_RULES_WUNLOCK();
5291 free(ioes, M_TEMP);
5292 error = EINVAL;
5293 goto fail;
5294 }
5295 break;
5296 #ifdef ALTQ
5297 case PF_RULESET_ALTQ:
5298 if (ioe->anchor[0]) {
5299 PF_RULES_WUNLOCK();
5300 free(ioes, M_TEMP);
5301 error = EINVAL;
5302 goto fail;
5303 }
5304 if (!V_altqs_inactive_open || ioe->ticket !=
5305 V_ticket_altqs_inactive) {
5306 PF_RULES_WUNLOCK();
5307 free(ioes, M_TEMP);
5308 error = EBUSY;
5309 goto fail;
5310 }
5311 break;
5312 #endif /* ALTQ */
5313 case PF_RULESET_TABLE:
5314 rs = pf_find_kruleset(ioe->anchor);
5315 if (rs == NULL || !rs->topen || ioe->ticket !=
5316 rs->tticket) {
5317 PF_RULES_WUNLOCK();
5318 free(ioes, M_TEMP);
5319 error = EBUSY;
5320 goto fail;
5321 }
5322 break;
5323 default:
5324 if (ioe->rs_num < 0 || ioe->rs_num >=
5325 PF_RULESET_MAX) {
5326 PF_RULES_WUNLOCK();
5327 free(ioes, M_TEMP);
5328 error = EINVAL;
5329 goto fail;
5330 }
5331 rs = pf_find_kruleset(ioe->anchor);
5332 if (rs == NULL ||
5333 !rs->rules[ioe->rs_num].inactive.open ||
5334 rs->rules[ioe->rs_num].inactive.ticket !=
5335 ioe->ticket) {
5336 PF_RULES_WUNLOCK();
5337 free(ioes, M_TEMP);
5338 error = EBUSY;
5339 goto fail;
5340 }
5341 break;
5342 }
5343 }
5344 /* Now do the commit - no errors should happen here. */
5345 for (i = 0, ioe = ioes; i < io->size; i++, ioe++) {
5346 switch (ioe->rs_num) {
5347 case PF_RULESET_ETH:
5348 if ((error = pf_commit_eth(ioe->ticket, ioe->anchor))) {
5349 PF_RULES_WUNLOCK();
5350 free(ioes, M_TEMP);
5351 goto fail; /* really bad */
5352 }
5353 break;
5354 #ifdef ALTQ
5355 case PF_RULESET_ALTQ:
5356 if ((error = pf_commit_altq(ioe->ticket))) {
5357 PF_RULES_WUNLOCK();
5358 free(ioes, M_TEMP);
5359 goto fail; /* really bad */
5360 }
5361 break;
5362 #endif /* ALTQ */
5363 case PF_RULESET_TABLE:
5364 {
5365 struct pfr_table table;
5366
5367 bzero(&table, sizeof(table));
5368 (void)strlcpy(table.pfrt_anchor, ioe->anchor,
5369 sizeof(table.pfrt_anchor));
5370 if ((error = pfr_ina_commit(&table,
5371 ioe->ticket, NULL, NULL, 0))) {
5372 PF_RULES_WUNLOCK();
5373 free(ioes, M_TEMP);
5374 goto fail; /* really bad */
5375 }
5376 break;
5377 }
5378 default:
5379 if ((error = pf_commit_rules(ioe->ticket,
5380 ioe->rs_num, ioe->anchor))) {
5381 PF_RULES_WUNLOCK();
5382 free(ioes, M_TEMP);
5383 goto fail; /* really bad */
5384 }
5385 break;
5386 }
5387 }
5388 PF_RULES_WUNLOCK();
5389
5390 /* Only hook into EtherNet taffic if we've got rules for it. */
5391 if (! TAILQ_EMPTY(V_pf_keth->active.rules))
5392 hook_pf_eth();
5393 else
5394 dehook_pf_eth();
5395
5396 free(ioes, M_TEMP);
5397 break;
5398 }
5399
5400 case DIOCGETSRCNODES: {
5401 struct pfioc_src_nodes *psn = (struct pfioc_src_nodes *)addr;
5402 struct pf_srchash *sh;
5403 struct pf_ksrc_node *n;
5404 struct pf_src_node *p, *pstore;
5405 uint32_t i, nr = 0;
5406
5407 for (i = 0, sh = V_pf_srchash; i <= V_pf_srchashmask;
5408 i++, sh++) {
5409 PF_HASHROW_LOCK(sh);
5410 LIST_FOREACH(n, &sh->nodes, entry)
5411 nr++;
5412 PF_HASHROW_UNLOCK(sh);
5413 }
5414
5415 psn->psn_len = min(psn->psn_len,
5416 sizeof(struct pf_src_node) * nr);
5417
5418 if (psn->psn_len == 0) {
5419 psn->psn_len = sizeof(struct pf_src_node) * nr;
5420 break;
5421 }
5422
5423 nr = 0;
5424
5425 p = pstore = malloc(psn->psn_len, M_TEMP, M_WAITOK | M_ZERO);
5426 for (i = 0, sh = V_pf_srchash; i <= V_pf_srchashmask;
5427 i++, sh++) {
5428 PF_HASHROW_LOCK(sh);
5429 LIST_FOREACH(n, &sh->nodes, entry) {
5430
5431 if ((nr + 1) * sizeof(*p) > (unsigned)psn->psn_len)
5432 break;
5433
5434 pf_src_node_copy(n, p);
5435
5436 p++;
5437 nr++;
5438 }
5439 PF_HASHROW_UNLOCK(sh);
5440 }
5441 error = copyout(pstore, psn->psn_src_nodes,
5442 sizeof(struct pf_src_node) * nr);
5443 if (error) {
5444 free(pstore, M_TEMP);
5445 break;
5446 }
5447 psn->psn_len = sizeof(struct pf_src_node) * nr;
5448 free(pstore, M_TEMP);
5449 break;
5450 }
5451
5452 case DIOCCLRSRCNODES: {
5453 pf_kill_srcnodes(NULL);
5454 break;
5455 }
5456
5457 case DIOCKILLSRCNODES:
5458 pf_kill_srcnodes((struct pfioc_src_node_kill *)addr);
5459 break;
5460
5461 #ifdef COMPAT_FREEBSD13
5462 case DIOCKEEPCOUNTERS_FREEBSD13:
5463 #endif
5464 case DIOCKEEPCOUNTERS:
5465 error = pf_keepcounters((struct pfioc_nv *)addr);
5466 break;
5467
5468 case DIOCGETSYNCOOKIES:
5469 error = pf_get_syncookies((struct pfioc_nv *)addr);
5470 break;
5471
5472 case DIOCSETSYNCOOKIES:
5473 error = pf_set_syncookies((struct pfioc_nv *)addr);
5474 break;
5475
5476 case DIOCSETHOSTID: {
5477 u_int32_t *hostid = (u_int32_t *)addr;
5478
5479 PF_RULES_WLOCK();
5480 if (*hostid == 0)
5481 V_pf_status.hostid = arc4random();
5482 else
5483 V_pf_status.hostid = *hostid;
5484 PF_RULES_WUNLOCK();
5485 break;
5486 }
5487
5488 case DIOCOSFPFLUSH:
5489 PF_RULES_WLOCK();
5490 pf_osfp_flush();
5491 PF_RULES_WUNLOCK();
5492 break;
5493
5494 case DIOCIGETIFACES: {
5495 struct pfioc_iface *io = (struct pfioc_iface *)addr;
5496 struct pfi_kif *ifstore;
5497 size_t bufsiz;
5498
5499 if (io->pfiio_esize != sizeof(struct pfi_kif)) {
5500 error = ENODEV;
5501 break;
5502 }
5503
5504 if (io->pfiio_size < 0 ||
5505 io->pfiio_size > pf_ioctl_maxcount ||
5506 WOULD_OVERFLOW(io->pfiio_size, sizeof(struct pfi_kif))) {
5507 error = EINVAL;
5508 break;
5509 }
5510
5511 io->pfiio_name[sizeof(io->pfiio_name) - 1] = '\0';
5512
5513 bufsiz = io->pfiio_size * sizeof(struct pfi_kif);
5514 ifstore = mallocarray(io->pfiio_size, sizeof(struct pfi_kif),
5515 M_TEMP, M_WAITOK | M_ZERO);
5516
5517 PF_RULES_RLOCK();
5518 pfi_get_ifaces(io->pfiio_name, ifstore, &io->pfiio_size);
5519 PF_RULES_RUNLOCK();
5520 error = copyout(ifstore, io->pfiio_buffer, bufsiz);
5521 free(ifstore, M_TEMP);
5522 break;
5523 }
5524
5525 case DIOCSETIFFLAG: {
5526 struct pfioc_iface *io = (struct pfioc_iface *)addr;
5527
5528 io->pfiio_name[sizeof(io->pfiio_name) - 1] = '\0';
5529
5530 PF_RULES_WLOCK();
5531 error = pfi_set_flags(io->pfiio_name, io->pfiio_flags);
5532 PF_RULES_WUNLOCK();
5533 break;
5534 }
5535
5536 case DIOCCLRIFFLAG: {
5537 struct pfioc_iface *io = (struct pfioc_iface *)addr;
5538
5539 io->pfiio_name[sizeof(io->pfiio_name) - 1] = '\0';
5540
5541 PF_RULES_WLOCK();
5542 error = pfi_clear_flags(io->pfiio_name, io->pfiio_flags);
5543 PF_RULES_WUNLOCK();
5544 break;
5545 }
5546
5547 case DIOCSETREASS: {
5548 u_int32_t *reass = (u_int32_t *)addr;
5549
5550 V_pf_status.reass = *reass & (PF_REASS_ENABLED|PF_REASS_NODF);
5551 /* Removal of DF flag without reassembly enabled is not a
5552 * valid combination. Disable reassembly in such case. */
5553 if (!(V_pf_status.reass & PF_REASS_ENABLED))
5554 V_pf_status.reass = 0;
5555 break;
5556 }
5557
5558 default:
5559 error = ENODEV;
5560 break;
5561 }
5562 fail:
5563 CURVNET_RESTORE();
5564
5565 #undef ERROUT_IOCTL
5566
5567 return (error);
5568 }
5569
5570 void
pfsync_state_export(union pfsync_state_union * sp,struct pf_kstate * st,int msg_version)5571 pfsync_state_export(union pfsync_state_union *sp, struct pf_kstate *st, int msg_version)
5572 {
5573 bzero(sp, sizeof(union pfsync_state_union));
5574
5575 /* copy from state key */
5576 sp->pfs_1301.key[PF_SK_WIRE].addr[0] = st->key[PF_SK_WIRE]->addr[0];
5577 sp->pfs_1301.key[PF_SK_WIRE].addr[1] = st->key[PF_SK_WIRE]->addr[1];
5578 sp->pfs_1301.key[PF_SK_WIRE].port[0] = st->key[PF_SK_WIRE]->port[0];
5579 sp->pfs_1301.key[PF_SK_WIRE].port[1] = st->key[PF_SK_WIRE]->port[1];
5580 sp->pfs_1301.key[PF_SK_STACK].addr[0] = st->key[PF_SK_STACK]->addr[0];
5581 sp->pfs_1301.key[PF_SK_STACK].addr[1] = st->key[PF_SK_STACK]->addr[1];
5582 sp->pfs_1301.key[PF_SK_STACK].port[0] = st->key[PF_SK_STACK]->port[0];
5583 sp->pfs_1301.key[PF_SK_STACK].port[1] = st->key[PF_SK_STACK]->port[1];
5584 sp->pfs_1301.proto = st->key[PF_SK_WIRE]->proto;
5585 sp->pfs_1301.af = st->key[PF_SK_WIRE]->af;
5586
5587 /* copy from state */
5588 strlcpy(sp->pfs_1301.ifname, st->kif->pfik_name, sizeof(sp->pfs_1301.ifname));
5589 bcopy(&st->rt_addr, &sp->pfs_1301.rt_addr, sizeof(sp->pfs_1301.rt_addr));
5590 sp->pfs_1301.creation = htonl(time_uptime - (st->creation / 1000));
5591 sp->pfs_1301.expire = pf_state_expires(st);
5592 if (sp->pfs_1301.expire <= time_uptime)
5593 sp->pfs_1301.expire = htonl(0);
5594 else
5595 sp->pfs_1301.expire = htonl(sp->pfs_1301.expire - time_uptime);
5596
5597 sp->pfs_1301.direction = st->direction;
5598 sp->pfs_1301.log = st->act.log;
5599 sp->pfs_1301.timeout = st->timeout;
5600
5601 switch (msg_version) {
5602 case PFSYNC_MSG_VERSION_1301:
5603 sp->pfs_1301.state_flags = st->state_flags;
5604 break;
5605 case PFSYNC_MSG_VERSION_1400:
5606 sp->pfs_1400.state_flags = htons(st->state_flags);
5607 sp->pfs_1400.qid = htons(st->act.qid);
5608 sp->pfs_1400.pqid = htons(st->act.pqid);
5609 sp->pfs_1400.dnpipe = htons(st->act.dnpipe);
5610 sp->pfs_1400.dnrpipe = htons(st->act.dnrpipe);
5611 sp->pfs_1400.rtableid = htonl(st->act.rtableid);
5612 sp->pfs_1400.min_ttl = st->act.min_ttl;
5613 sp->pfs_1400.set_tos = st->act.set_tos;
5614 sp->pfs_1400.max_mss = htons(st->act.max_mss);
5615 sp->pfs_1400.set_prio[0] = st->act.set_prio[0];
5616 sp->pfs_1400.set_prio[1] = st->act.set_prio[1];
5617 sp->pfs_1400.rt = st->rt;
5618 if (st->rt_kif)
5619 strlcpy(sp->pfs_1400.rt_ifname,
5620 st->rt_kif->pfik_name,
5621 sizeof(sp->pfs_1400.rt_ifname));
5622 break;
5623 default:
5624 panic("%s: Unsupported pfsync_msg_version %d",
5625 __func__, msg_version);
5626 }
5627
5628 if (st->src_node)
5629 sp->pfs_1301.sync_flags |= PFSYNC_FLAG_SRCNODE;
5630 if (st->nat_src_node)
5631 sp->pfs_1301.sync_flags |= PFSYNC_FLAG_NATSRCNODE;
5632
5633 sp->pfs_1301.id = st->id;
5634 sp->pfs_1301.creatorid = st->creatorid;
5635 pf_state_peer_hton(&st->src, &sp->pfs_1301.src);
5636 pf_state_peer_hton(&st->dst, &sp->pfs_1301.dst);
5637
5638 if (st->rule == NULL)
5639 sp->pfs_1301.rule = htonl(-1);
5640 else
5641 sp->pfs_1301.rule = htonl(st->rule->nr);
5642 if (st->anchor == NULL)
5643 sp->pfs_1301.anchor = htonl(-1);
5644 else
5645 sp->pfs_1301.anchor = htonl(st->anchor->nr);
5646 if (st->nat_rule == NULL)
5647 sp->pfs_1301.nat_rule = htonl(-1);
5648 else
5649 sp->pfs_1301.nat_rule = htonl(st->nat_rule->nr);
5650
5651 pf_state_counter_hton(st->packets[0], sp->pfs_1301.packets[0]);
5652 pf_state_counter_hton(st->packets[1], sp->pfs_1301.packets[1]);
5653 pf_state_counter_hton(st->bytes[0], sp->pfs_1301.bytes[0]);
5654 pf_state_counter_hton(st->bytes[1], sp->pfs_1301.bytes[1]);
5655 }
5656
5657 void
pf_state_export(struct pf_state_export * sp,struct pf_kstate * st)5658 pf_state_export(struct pf_state_export *sp, struct pf_kstate *st)
5659 {
5660 bzero(sp, sizeof(*sp));
5661
5662 sp->version = PF_STATE_VERSION;
5663
5664 /* copy from state key */
5665 sp->key[PF_SK_WIRE].addr[0] = st->key[PF_SK_WIRE]->addr[0];
5666 sp->key[PF_SK_WIRE].addr[1] = st->key[PF_SK_WIRE]->addr[1];
5667 sp->key[PF_SK_WIRE].port[0] = st->key[PF_SK_WIRE]->port[0];
5668 sp->key[PF_SK_WIRE].port[1] = st->key[PF_SK_WIRE]->port[1];
5669 sp->key[PF_SK_STACK].addr[0] = st->key[PF_SK_STACK]->addr[0];
5670 sp->key[PF_SK_STACK].addr[1] = st->key[PF_SK_STACK]->addr[1];
5671 sp->key[PF_SK_STACK].port[0] = st->key[PF_SK_STACK]->port[0];
5672 sp->key[PF_SK_STACK].port[1] = st->key[PF_SK_STACK]->port[1];
5673 sp->proto = st->key[PF_SK_WIRE]->proto;
5674 sp->af = st->key[PF_SK_WIRE]->af;
5675
5676 /* copy from state */
5677 strlcpy(sp->ifname, st->kif->pfik_name, sizeof(sp->ifname));
5678 strlcpy(sp->orig_ifname, st->orig_kif->pfik_name,
5679 sizeof(sp->orig_ifname));
5680 bcopy(&st->rt_addr, &sp->rt_addr, sizeof(sp->rt_addr));
5681 sp->creation = htonl(time_uptime - (st->creation / 1000));
5682 sp->expire = pf_state_expires(st);
5683 if (sp->expire <= time_uptime)
5684 sp->expire = htonl(0);
5685 else
5686 sp->expire = htonl(sp->expire - time_uptime);
5687
5688 sp->direction = st->direction;
5689 sp->log = st->act.log;
5690 sp->timeout = st->timeout;
5691 /* 8 bits for the old libpfctl, 16 bits for the new libpfctl */
5692 sp->state_flags_compat = st->state_flags;
5693 sp->state_flags = htons(st->state_flags);
5694 if (st->src_node)
5695 sp->sync_flags |= PFSYNC_FLAG_SRCNODE;
5696 if (st->nat_src_node)
5697 sp->sync_flags |= PFSYNC_FLAG_NATSRCNODE;
5698
5699 sp->id = st->id;
5700 sp->creatorid = st->creatorid;
5701 pf_state_peer_hton(&st->src, &sp->src);
5702 pf_state_peer_hton(&st->dst, &sp->dst);
5703
5704 if (st->rule == NULL)
5705 sp->rule = htonl(-1);
5706 else
5707 sp->rule = htonl(st->rule->nr);
5708 if (st->anchor == NULL)
5709 sp->anchor = htonl(-1);
5710 else
5711 sp->anchor = htonl(st->anchor->nr);
5712 if (st->nat_rule == NULL)
5713 sp->nat_rule = htonl(-1);
5714 else
5715 sp->nat_rule = htonl(st->nat_rule->nr);
5716
5717 sp->packets[0] = st->packets[0];
5718 sp->packets[1] = st->packets[1];
5719 sp->bytes[0] = st->bytes[0];
5720 sp->bytes[1] = st->bytes[1];
5721
5722 sp->qid = htons(st->act.qid);
5723 sp->pqid = htons(st->act.pqid);
5724 sp->dnpipe = htons(st->act.dnpipe);
5725 sp->dnrpipe = htons(st->act.dnrpipe);
5726 sp->rtableid = htonl(st->act.rtableid);
5727 sp->min_ttl = st->act.min_ttl;
5728 sp->set_tos = st->act.set_tos;
5729 sp->max_mss = htons(st->act.max_mss);
5730 sp->rt = st->rt;
5731 if (st->rt_kif)
5732 strlcpy(sp->rt_ifname, st->rt_kif->pfik_name,
5733 sizeof(sp->rt_ifname));
5734 sp->set_prio[0] = st->act.set_prio[0];
5735 sp->set_prio[1] = st->act.set_prio[1];
5736
5737 }
5738
5739 static void
pf_tbladdr_copyout(struct pf_addr_wrap * aw)5740 pf_tbladdr_copyout(struct pf_addr_wrap *aw)
5741 {
5742 struct pfr_ktable *kt;
5743
5744 KASSERT(aw->type == PF_ADDR_TABLE, ("%s: type %u", __func__, aw->type));
5745
5746 kt = aw->p.tbl;
5747 if (!(kt->pfrkt_flags & PFR_TFLAG_ACTIVE) && kt->pfrkt_root != NULL)
5748 kt = kt->pfrkt_root;
5749 aw->p.tbl = NULL;
5750 aw->p.tblcnt = (kt->pfrkt_flags & PFR_TFLAG_ACTIVE) ?
5751 kt->pfrkt_cnt : -1;
5752 }
5753
5754 static int
pf_add_status_counters(nvlist_t * nvl,const char * name,counter_u64_t * counters,size_t number,char ** names)5755 pf_add_status_counters(nvlist_t *nvl, const char *name, counter_u64_t *counters,
5756 size_t number, char **names)
5757 {
5758 nvlist_t *nvc;
5759
5760 nvc = nvlist_create(0);
5761 if (nvc == NULL)
5762 return (ENOMEM);
5763
5764 for (int i = 0; i < number; i++) {
5765 nvlist_append_number_array(nvc, "counters",
5766 counter_u64_fetch(counters[i]));
5767 nvlist_append_string_array(nvc, "names",
5768 names[i]);
5769 nvlist_append_number_array(nvc, "ids",
5770 i);
5771 }
5772 nvlist_add_nvlist(nvl, name, nvc);
5773 nvlist_destroy(nvc);
5774
5775 return (0);
5776 }
5777
5778 static int
pf_getstatus(struct pfioc_nv * nv)5779 pf_getstatus(struct pfioc_nv *nv)
5780 {
5781 nvlist_t *nvl = NULL, *nvc = NULL;
5782 void *nvlpacked = NULL;
5783 int error;
5784 struct pf_status s;
5785 char *pf_reasons[PFRES_MAX+1] = PFRES_NAMES;
5786 char *pf_lcounter[KLCNT_MAX+1] = KLCNT_NAMES;
5787 char *pf_fcounter[FCNT_MAX+1] = FCNT_NAMES;
5788 PF_RULES_RLOCK_TRACKER;
5789
5790 #define ERROUT(x) ERROUT_FUNCTION(errout, x)
5791
5792 PF_RULES_RLOCK();
5793
5794 nvl = nvlist_create(0);
5795 if (nvl == NULL)
5796 ERROUT(ENOMEM);
5797
5798 nvlist_add_bool(nvl, "running", V_pf_status.running);
5799 nvlist_add_number(nvl, "since", V_pf_status.since);
5800 nvlist_add_number(nvl, "debug", V_pf_status.debug);
5801 nvlist_add_number(nvl, "hostid", V_pf_status.hostid);
5802 nvlist_add_number(nvl, "states", V_pf_status.states);
5803 nvlist_add_number(nvl, "src_nodes", V_pf_status.src_nodes);
5804 nvlist_add_number(nvl, "reass", V_pf_status.reass);
5805 nvlist_add_bool(nvl, "syncookies_active",
5806 V_pf_status.syncookies_active);
5807 nvlist_add_number(nvl, "halfopen_states", V_pf_status.states_halfopen);
5808
5809 /* counters */
5810 error = pf_add_status_counters(nvl, "counters", V_pf_status.counters,
5811 PFRES_MAX, pf_reasons);
5812 if (error != 0)
5813 ERROUT(error);
5814
5815 /* lcounters */
5816 error = pf_add_status_counters(nvl, "lcounters", V_pf_status.lcounters,
5817 KLCNT_MAX, pf_lcounter);
5818 if (error != 0)
5819 ERROUT(error);
5820
5821 /* fcounters */
5822 nvc = nvlist_create(0);
5823 if (nvc == NULL)
5824 ERROUT(ENOMEM);
5825
5826 for (int i = 0; i < FCNT_MAX; i++) {
5827 nvlist_append_number_array(nvc, "counters",
5828 pf_counter_u64_fetch(&V_pf_status.fcounters[i]));
5829 nvlist_append_string_array(nvc, "names",
5830 pf_fcounter[i]);
5831 nvlist_append_number_array(nvc, "ids",
5832 i);
5833 }
5834 nvlist_add_nvlist(nvl, "fcounters", nvc);
5835 nvlist_destroy(nvc);
5836 nvc = NULL;
5837
5838 /* scounters */
5839 error = pf_add_status_counters(nvl, "scounters", V_pf_status.scounters,
5840 SCNT_MAX, pf_fcounter);
5841 if (error != 0)
5842 ERROUT(error);
5843
5844 nvlist_add_string(nvl, "ifname", V_pf_status.ifname);
5845 nvlist_add_binary(nvl, "chksum", V_pf_status.pf_chksum,
5846 PF_MD5_DIGEST_LENGTH);
5847
5848 pfi_update_status(V_pf_status.ifname, &s);
5849
5850 /* pcounters / bcounters */
5851 for (int i = 0; i < 2; i++) {
5852 for (int j = 0; j < 2; j++) {
5853 for (int k = 0; k < 2; k++) {
5854 nvlist_append_number_array(nvl, "pcounters",
5855 s.pcounters[i][j][k]);
5856 }
5857 nvlist_append_number_array(nvl, "bcounters",
5858 s.bcounters[i][j]);
5859 }
5860 }
5861
5862 nvlpacked = nvlist_pack(nvl, &nv->len);
5863 if (nvlpacked == NULL)
5864 ERROUT(ENOMEM);
5865
5866 if (nv->size == 0)
5867 ERROUT(0);
5868 else if (nv->size < nv->len)
5869 ERROUT(ENOSPC);
5870
5871 PF_RULES_RUNLOCK();
5872 error = copyout(nvlpacked, nv->data, nv->len);
5873 goto done;
5874
5875 #undef ERROUT
5876 errout:
5877 PF_RULES_RUNLOCK();
5878 done:
5879 free(nvlpacked, M_NVLIST);
5880 nvlist_destroy(nvc);
5881 nvlist_destroy(nvl);
5882
5883 return (error);
5884 }
5885
5886 /*
5887 * XXX - Check for version mismatch!!!
5888 */
5889 static void
pf_clear_all_states(void)5890 pf_clear_all_states(void)
5891 {
5892 struct epoch_tracker et;
5893 struct pf_kstate *s;
5894 u_int i;
5895
5896 NET_EPOCH_ENTER(et);
5897 for (i = 0; i <= V_pf_hashmask; i++) {
5898 struct pf_idhash *ih = &V_pf_idhash[i];
5899 relock:
5900 PF_HASHROW_LOCK(ih);
5901 LIST_FOREACH(s, &ih->states, entry) {
5902 s->timeout = PFTM_PURGE;
5903 /* Don't send out individual delete messages. */
5904 s->state_flags |= PFSTATE_NOSYNC;
5905 pf_unlink_state(s);
5906 goto relock;
5907 }
5908 PF_HASHROW_UNLOCK(ih);
5909 }
5910 NET_EPOCH_EXIT(et);
5911 }
5912
5913 static int
pf_clear_tables(void)5914 pf_clear_tables(void)
5915 {
5916 struct pfioc_table io;
5917 int error;
5918
5919 bzero(&io, sizeof(io));
5920 io.pfrio_flags |= PFR_FLAG_ALLRSETS;
5921
5922 error = pfr_clr_tables(&io.pfrio_table, &io.pfrio_ndel,
5923 io.pfrio_flags);
5924
5925 return (error);
5926 }
5927
5928 static void
pf_kill_srcnodes(struct pfioc_src_node_kill * psnk)5929 pf_kill_srcnodes(struct pfioc_src_node_kill *psnk)
5930 {
5931 struct pf_ksrc_node_list kill;
5932 u_int killed;
5933
5934 LIST_INIT(&kill);
5935 for (int i = 0; i <= V_pf_srchashmask; i++) {
5936 struct pf_srchash *sh = &V_pf_srchash[i];
5937 struct pf_ksrc_node *sn, *tmp;
5938
5939 PF_HASHROW_LOCK(sh);
5940 LIST_FOREACH_SAFE(sn, &sh->nodes, entry, tmp)
5941 if (psnk == NULL ||
5942 (PF_MATCHA(psnk->psnk_src.neg,
5943 &psnk->psnk_src.addr.v.a.addr,
5944 &psnk->psnk_src.addr.v.a.mask,
5945 &sn->addr, sn->af) &&
5946 PF_MATCHA(psnk->psnk_dst.neg,
5947 &psnk->psnk_dst.addr.v.a.addr,
5948 &psnk->psnk_dst.addr.v.a.mask,
5949 &sn->raddr, sn->af))) {
5950 pf_unlink_src_node(sn);
5951 LIST_INSERT_HEAD(&kill, sn, entry);
5952 sn->expire = 1;
5953 }
5954 PF_HASHROW_UNLOCK(sh);
5955 }
5956
5957 for (int i = 0; i <= V_pf_hashmask; i++) {
5958 struct pf_idhash *ih = &V_pf_idhash[i];
5959 struct pf_kstate *s;
5960
5961 PF_HASHROW_LOCK(ih);
5962 LIST_FOREACH(s, &ih->states, entry) {
5963 if (s->src_node && s->src_node->expire == 1)
5964 s->src_node = NULL;
5965 if (s->nat_src_node && s->nat_src_node->expire == 1)
5966 s->nat_src_node = NULL;
5967 }
5968 PF_HASHROW_UNLOCK(ih);
5969 }
5970
5971 killed = pf_free_src_nodes(&kill);
5972
5973 if (psnk != NULL)
5974 psnk->psnk_killed = killed;
5975 }
5976
5977 static int
pf_keepcounters(struct pfioc_nv * nv)5978 pf_keepcounters(struct pfioc_nv *nv)
5979 {
5980 nvlist_t *nvl = NULL;
5981 void *nvlpacked = NULL;
5982 int error = 0;
5983
5984 #define ERROUT(x) ERROUT_FUNCTION(on_error, x)
5985
5986 if (nv->len > pf_ioctl_maxcount)
5987 ERROUT(ENOMEM);
5988
5989 nvlpacked = malloc(nv->len, M_NVLIST, M_WAITOK);
5990 error = copyin(nv->data, nvlpacked, nv->len);
5991 if (error)
5992 ERROUT(error);
5993
5994 nvl = nvlist_unpack(nvlpacked, nv->len, 0);
5995 if (nvl == NULL)
5996 ERROUT(EBADMSG);
5997
5998 if (! nvlist_exists_bool(nvl, "keep_counters"))
5999 ERROUT(EBADMSG);
6000
6001 V_pf_status.keep_counters = nvlist_get_bool(nvl, "keep_counters");
6002
6003 on_error:
6004 nvlist_destroy(nvl);
6005 free(nvlpacked, M_NVLIST);
6006 return (error);
6007 }
6008
6009 unsigned int
pf_clear_states(const struct pf_kstate_kill * kill)6010 pf_clear_states(const struct pf_kstate_kill *kill)
6011 {
6012 struct pf_state_key_cmp match_key;
6013 struct pf_kstate *s;
6014 struct pfi_kkif *kif;
6015 int idx;
6016 unsigned int killed = 0, dir;
6017
6018 NET_EPOCH_ASSERT();
6019
6020 for (unsigned int i = 0; i <= V_pf_hashmask; i++) {
6021 struct pf_idhash *ih = &V_pf_idhash[i];
6022
6023 relock_DIOCCLRSTATES:
6024 PF_HASHROW_LOCK(ih);
6025 LIST_FOREACH(s, &ih->states, entry) {
6026 /* For floating states look at the original kif. */
6027 kif = s->kif == V_pfi_all ? s->orig_kif : s->kif;
6028
6029 if (kill->psk_ifname[0] &&
6030 strcmp(kill->psk_ifname,
6031 kif->pfik_name))
6032 continue;
6033
6034 if (kill->psk_kill_match) {
6035 bzero(&match_key, sizeof(match_key));
6036
6037 if (s->direction == PF_OUT) {
6038 dir = PF_IN;
6039 idx = PF_SK_STACK;
6040 } else {
6041 dir = PF_OUT;
6042 idx = PF_SK_WIRE;
6043 }
6044
6045 match_key.af = s->key[idx]->af;
6046 match_key.proto = s->key[idx]->proto;
6047 PF_ACPY(&match_key.addr[0],
6048 &s->key[idx]->addr[1], match_key.af);
6049 match_key.port[0] = s->key[idx]->port[1];
6050 PF_ACPY(&match_key.addr[1],
6051 &s->key[idx]->addr[0], match_key.af);
6052 match_key.port[1] = s->key[idx]->port[0];
6053 }
6054
6055 /*
6056 * Don't send out individual
6057 * delete messages.
6058 */
6059 s->state_flags |= PFSTATE_NOSYNC;
6060 pf_unlink_state(s);
6061 killed++;
6062
6063 if (kill->psk_kill_match)
6064 killed += pf_kill_matching_state(&match_key,
6065 dir);
6066
6067 goto relock_DIOCCLRSTATES;
6068 }
6069 PF_HASHROW_UNLOCK(ih);
6070 }
6071
6072 if (V_pfsync_clear_states_ptr != NULL)
6073 V_pfsync_clear_states_ptr(V_pf_status.hostid, kill->psk_ifname);
6074
6075 return (killed);
6076 }
6077
6078 void
pf_killstates(struct pf_kstate_kill * kill,unsigned int * killed)6079 pf_killstates(struct pf_kstate_kill *kill, unsigned int *killed)
6080 {
6081 struct pf_kstate *s;
6082
6083 NET_EPOCH_ASSERT();
6084 if (kill->psk_pfcmp.id) {
6085 if (kill->psk_pfcmp.creatorid == 0)
6086 kill->psk_pfcmp.creatorid = V_pf_status.hostid;
6087 if ((s = pf_find_state_byid(kill->psk_pfcmp.id,
6088 kill->psk_pfcmp.creatorid))) {
6089 pf_unlink_state(s);
6090 *killed = 1;
6091 }
6092 return;
6093 }
6094
6095 for (unsigned int i = 0; i <= V_pf_hashmask; i++)
6096 *killed += pf_killstates_row(kill, &V_pf_idhash[i]);
6097 }
6098
6099 static int
pf_killstates_nv(struct pfioc_nv * nv)6100 pf_killstates_nv(struct pfioc_nv *nv)
6101 {
6102 struct pf_kstate_kill kill;
6103 struct epoch_tracker et;
6104 nvlist_t *nvl = NULL;
6105 void *nvlpacked = NULL;
6106 int error = 0;
6107 unsigned int killed = 0;
6108
6109 #define ERROUT(x) ERROUT_FUNCTION(on_error, x)
6110
6111 if (nv->len > pf_ioctl_maxcount)
6112 ERROUT(ENOMEM);
6113
6114 nvlpacked = malloc(nv->len, M_NVLIST, M_WAITOK);
6115 error = copyin(nv->data, nvlpacked, nv->len);
6116 if (error)
6117 ERROUT(error);
6118
6119 nvl = nvlist_unpack(nvlpacked, nv->len, 0);
6120 if (nvl == NULL)
6121 ERROUT(EBADMSG);
6122
6123 error = pf_nvstate_kill_to_kstate_kill(nvl, &kill);
6124 if (error)
6125 ERROUT(error);
6126
6127 NET_EPOCH_ENTER(et);
6128 pf_killstates(&kill, &killed);
6129 NET_EPOCH_EXIT(et);
6130
6131 free(nvlpacked, M_NVLIST);
6132 nvlpacked = NULL;
6133 nvlist_destroy(nvl);
6134 nvl = nvlist_create(0);
6135 if (nvl == NULL)
6136 ERROUT(ENOMEM);
6137
6138 nvlist_add_number(nvl, "killed", killed);
6139
6140 nvlpacked = nvlist_pack(nvl, &nv->len);
6141 if (nvlpacked == NULL)
6142 ERROUT(ENOMEM);
6143
6144 if (nv->size == 0)
6145 ERROUT(0);
6146 else if (nv->size < nv->len)
6147 ERROUT(ENOSPC);
6148
6149 error = copyout(nvlpacked, nv->data, nv->len);
6150
6151 on_error:
6152 nvlist_destroy(nvl);
6153 free(nvlpacked, M_NVLIST);
6154 return (error);
6155 }
6156
6157 static int
pf_clearstates_nv(struct pfioc_nv * nv)6158 pf_clearstates_nv(struct pfioc_nv *nv)
6159 {
6160 struct pf_kstate_kill kill;
6161 struct epoch_tracker et;
6162 nvlist_t *nvl = NULL;
6163 void *nvlpacked = NULL;
6164 int error = 0;
6165 unsigned int killed;
6166
6167 #define ERROUT(x) ERROUT_FUNCTION(on_error, x)
6168
6169 if (nv->len > pf_ioctl_maxcount)
6170 ERROUT(ENOMEM);
6171
6172 nvlpacked = malloc(nv->len, M_NVLIST, M_WAITOK);
6173 error = copyin(nv->data, nvlpacked, nv->len);
6174 if (error)
6175 ERROUT(error);
6176
6177 nvl = nvlist_unpack(nvlpacked, nv->len, 0);
6178 if (nvl == NULL)
6179 ERROUT(EBADMSG);
6180
6181 error = pf_nvstate_kill_to_kstate_kill(nvl, &kill);
6182 if (error)
6183 ERROUT(error);
6184
6185 NET_EPOCH_ENTER(et);
6186 killed = pf_clear_states(&kill);
6187 NET_EPOCH_EXIT(et);
6188
6189 free(nvlpacked, M_NVLIST);
6190 nvlpacked = NULL;
6191 nvlist_destroy(nvl);
6192 nvl = nvlist_create(0);
6193 if (nvl == NULL)
6194 ERROUT(ENOMEM);
6195
6196 nvlist_add_number(nvl, "killed", killed);
6197
6198 nvlpacked = nvlist_pack(nvl, &nv->len);
6199 if (nvlpacked == NULL)
6200 ERROUT(ENOMEM);
6201
6202 if (nv->size == 0)
6203 ERROUT(0);
6204 else if (nv->size < nv->len)
6205 ERROUT(ENOSPC);
6206
6207 error = copyout(nvlpacked, nv->data, nv->len);
6208
6209 #undef ERROUT
6210 on_error:
6211 nvlist_destroy(nvl);
6212 free(nvlpacked, M_NVLIST);
6213 return (error);
6214 }
6215
6216 static int
pf_getstate(struct pfioc_nv * nv)6217 pf_getstate(struct pfioc_nv *nv)
6218 {
6219 nvlist_t *nvl = NULL, *nvls;
6220 void *nvlpacked = NULL;
6221 struct pf_kstate *s = NULL;
6222 int error = 0;
6223 uint64_t id, creatorid;
6224
6225 #define ERROUT(x) ERROUT_FUNCTION(errout, x)
6226
6227 if (nv->len > pf_ioctl_maxcount)
6228 ERROUT(ENOMEM);
6229
6230 nvlpacked = malloc(nv->len, M_NVLIST, M_WAITOK);
6231 error = copyin(nv->data, nvlpacked, nv->len);
6232 if (error)
6233 ERROUT(error);
6234
6235 nvl = nvlist_unpack(nvlpacked, nv->len, 0);
6236 if (nvl == NULL)
6237 ERROUT(EBADMSG);
6238
6239 PFNV_CHK(pf_nvuint64(nvl, "id", &id));
6240 PFNV_CHK(pf_nvuint64(nvl, "creatorid", &creatorid));
6241
6242 s = pf_find_state_byid(id, creatorid);
6243 if (s == NULL)
6244 ERROUT(ENOENT);
6245
6246 free(nvlpacked, M_NVLIST);
6247 nvlpacked = NULL;
6248 nvlist_destroy(nvl);
6249 nvl = nvlist_create(0);
6250 if (nvl == NULL)
6251 ERROUT(ENOMEM);
6252
6253 nvls = pf_state_to_nvstate(s);
6254 if (nvls == NULL)
6255 ERROUT(ENOMEM);
6256
6257 nvlist_add_nvlist(nvl, "state", nvls);
6258 nvlist_destroy(nvls);
6259
6260 nvlpacked = nvlist_pack(nvl, &nv->len);
6261 if (nvlpacked == NULL)
6262 ERROUT(ENOMEM);
6263
6264 if (nv->size == 0)
6265 ERROUT(0);
6266 else if (nv->size < nv->len)
6267 ERROUT(ENOSPC);
6268
6269 error = copyout(nvlpacked, nv->data, nv->len);
6270
6271 #undef ERROUT
6272 errout:
6273 if (s != NULL)
6274 PF_STATE_UNLOCK(s);
6275 free(nvlpacked, M_NVLIST);
6276 nvlist_destroy(nvl);
6277 return (error);
6278 }
6279
6280 /*
6281 * XXX - Check for version mismatch!!!
6282 */
6283
6284 /*
6285 * Duplicate pfctl -Fa operation to get rid of as much as we can.
6286 */
6287 static int
shutdown_pf(void)6288 shutdown_pf(void)
6289 {
6290 int error = 0;
6291 u_int32_t t[5];
6292 char nn = '\0';
6293 struct pf_kanchor *anchor;
6294 struct pf_keth_anchor *eth_anchor;
6295 int rs_num;
6296
6297 do {
6298 /* Unlink rules of all user defined anchors */
6299 RB_FOREACH(anchor, pf_kanchor_global, &V_pf_anchors) {
6300 /* Wildcard based anchors may not have a respective
6301 * explicit anchor rule or they may be left empty
6302 * without rules. It leads to anchor.refcnt=0, and the
6303 * rest of the logic does not expect it. */
6304 if (anchor->refcnt == 0)
6305 anchor->refcnt = 1;
6306 for (rs_num = 0; rs_num < PF_RULESET_MAX; ++rs_num) {
6307 if ((error = pf_begin_rules(&t[rs_num], rs_num,
6308 anchor->path)) != 0) {
6309 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: "
6310 "anchor.path=%s rs_num=%d\n",
6311 anchor->path, rs_num));
6312 goto error; /* XXX: rollback? */
6313 }
6314 }
6315 for (rs_num = 0; rs_num < PF_RULESET_MAX; ++rs_num) {
6316 error = pf_commit_rules(t[rs_num], rs_num,
6317 anchor->path);
6318 MPASS(error == 0);
6319 }
6320 }
6321
6322 /* Unlink rules of all user defined ether anchors */
6323 RB_FOREACH(eth_anchor, pf_keth_anchor_global,
6324 &V_pf_keth_anchors) {
6325 /* Wildcard based anchors may not have a respective
6326 * explicit anchor rule or they may be left empty
6327 * without rules. It leads to anchor.refcnt=0, and the
6328 * rest of the logic does not expect it. */
6329 if (eth_anchor->refcnt == 0)
6330 eth_anchor->refcnt = 1;
6331 if ((error = pf_begin_eth(&t[0], eth_anchor->path))
6332 != 0) {
6333 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: eth "
6334 "anchor.path=%s\n", eth_anchor->path));
6335 goto error;
6336 }
6337 error = pf_commit_eth(t[0], eth_anchor->path);
6338 MPASS(error == 0);
6339 }
6340
6341 if ((error = pf_begin_rules(&t[0], PF_RULESET_SCRUB, &nn))
6342 != 0) {
6343 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: SCRUB\n"));
6344 break;
6345 }
6346 if ((error = pf_begin_rules(&t[1], PF_RULESET_FILTER, &nn))
6347 != 0) {
6348 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: FILTER\n"));
6349 break; /* XXX: rollback? */
6350 }
6351 if ((error = pf_begin_rules(&t[2], PF_RULESET_NAT, &nn))
6352 != 0) {
6353 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: NAT\n"));
6354 break; /* XXX: rollback? */
6355 }
6356 if ((error = pf_begin_rules(&t[3], PF_RULESET_BINAT, &nn))
6357 != 0) {
6358 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: BINAT\n"));
6359 break; /* XXX: rollback? */
6360 }
6361 if ((error = pf_begin_rules(&t[4], PF_RULESET_RDR, &nn))
6362 != 0) {
6363 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: RDR\n"));
6364 break; /* XXX: rollback? */
6365 }
6366
6367 error = pf_commit_rules(t[0], PF_RULESET_SCRUB, &nn);
6368 MPASS(error == 0);
6369 error = pf_commit_rules(t[1], PF_RULESET_FILTER, &nn);
6370 MPASS(error == 0);
6371 error = pf_commit_rules(t[2], PF_RULESET_NAT, &nn);
6372 MPASS(error == 0);
6373 error = pf_commit_rules(t[3], PF_RULESET_BINAT, &nn);
6374 MPASS(error == 0);
6375 error = pf_commit_rules(t[4], PF_RULESET_RDR, &nn);
6376 MPASS(error == 0);
6377
6378 if ((error = pf_clear_tables()) != 0)
6379 break;
6380
6381 if ((error = pf_begin_eth(&t[0], &nn)) != 0) {
6382 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: eth\n"));
6383 break;
6384 }
6385 error = pf_commit_eth(t[0], &nn);
6386 MPASS(error == 0);
6387
6388 #ifdef ALTQ
6389 if ((error = pf_begin_altq(&t[0])) != 0) {
6390 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: ALTQ\n"));
6391 break;
6392 }
6393 pf_commit_altq(t[0]);
6394 #endif
6395
6396 pf_clear_all_states();
6397
6398 pf_kill_srcnodes(NULL);
6399
6400 /* status does not use malloced mem so no need to cleanup */
6401 /* fingerprints and interfaces have their own cleanup code */
6402 } while(0);
6403
6404 error:
6405 return (error);
6406 }
6407
6408 static pfil_return_t
pf_check_return(int chk,struct mbuf ** m)6409 pf_check_return(int chk, struct mbuf **m)
6410 {
6411
6412 switch (chk) {
6413 case PF_PASS:
6414 if (*m == NULL)
6415 return (PFIL_CONSUMED);
6416 else
6417 return (PFIL_PASS);
6418 break;
6419 default:
6420 if (*m != NULL) {
6421 m_freem(*m);
6422 *m = NULL;
6423 }
6424 return (PFIL_DROPPED);
6425 }
6426 }
6427
6428 static pfil_return_t
pf_eth_check_in(struct mbuf ** m,struct ifnet * ifp,int flags,void * ruleset __unused,struct inpcb * inp)6429 pf_eth_check_in(struct mbuf **m, struct ifnet *ifp, int flags,
6430 void *ruleset __unused, struct inpcb *inp)
6431 {
6432 int chk;
6433
6434 CURVNET_ASSERT_SET();
6435
6436 chk = pf_test_eth(PF_IN, flags, ifp, m, inp);
6437
6438 return (pf_check_return(chk, m));
6439 }
6440
6441 static pfil_return_t
pf_eth_check_out(struct mbuf ** m,struct ifnet * ifp,int flags,void * ruleset __unused,struct inpcb * inp)6442 pf_eth_check_out(struct mbuf **m, struct ifnet *ifp, int flags,
6443 void *ruleset __unused, struct inpcb *inp)
6444 {
6445 int chk;
6446
6447 CURVNET_ASSERT_SET();
6448
6449 chk = pf_test_eth(PF_OUT, flags, ifp, m, inp);
6450
6451 return (pf_check_return(chk, m));
6452 }
6453
6454 #ifdef INET
6455 static pfil_return_t
pf_check_in(struct mbuf ** m,struct ifnet * ifp,int flags,void * ruleset __unused,struct inpcb * inp)6456 pf_check_in(struct mbuf **m, struct ifnet *ifp, int flags,
6457 void *ruleset __unused, struct inpcb *inp)
6458 {
6459 int chk;
6460
6461 CURVNET_ASSERT_SET();
6462
6463 chk = pf_test(AF_INET, PF_IN, flags, ifp, m, inp, NULL);
6464
6465 return (pf_check_return(chk, m));
6466 }
6467
6468 static pfil_return_t
pf_check_out(struct mbuf ** m,struct ifnet * ifp,int flags,void * ruleset __unused,struct inpcb * inp)6469 pf_check_out(struct mbuf **m, struct ifnet *ifp, int flags,
6470 void *ruleset __unused, struct inpcb *inp)
6471 {
6472 int chk;
6473
6474 CURVNET_ASSERT_SET();
6475
6476 chk = pf_test(AF_INET, PF_OUT, flags, ifp, m, inp, NULL);
6477
6478 return (pf_check_return(chk, m));
6479 }
6480 #endif
6481
6482 #ifdef INET6
6483 static pfil_return_t
pf_check6_in(struct mbuf ** m,struct ifnet * ifp,int flags,void * ruleset __unused,struct inpcb * inp)6484 pf_check6_in(struct mbuf **m, struct ifnet *ifp, int flags,
6485 void *ruleset __unused, struct inpcb *inp)
6486 {
6487 int chk;
6488
6489 CURVNET_ASSERT_SET();
6490
6491 /*
6492 * In case of loopback traffic IPv6 uses the real interface in
6493 * order to support scoped addresses. In order to support stateful
6494 * filtering we have change this to lo0 as it is the case in IPv4.
6495 */
6496 chk = pf_test(AF_INET6, PF_IN, flags, (*m)->m_flags & M_LOOP ? V_loif : ifp,
6497 m, inp, NULL);
6498
6499 return (pf_check_return(chk, m));
6500 }
6501
6502 static pfil_return_t
pf_check6_out(struct mbuf ** m,struct ifnet * ifp,int flags,void * ruleset __unused,struct inpcb * inp)6503 pf_check6_out(struct mbuf **m, struct ifnet *ifp, int flags,
6504 void *ruleset __unused, struct inpcb *inp)
6505 {
6506 int chk;
6507
6508 CURVNET_ASSERT_SET();
6509
6510 chk = pf_test(AF_INET6, PF_OUT, flags, ifp, m, inp, NULL);
6511
6512 return (pf_check_return(chk, m));
6513 }
6514 #endif /* INET6 */
6515
6516 VNET_DEFINE_STATIC(pfil_hook_t, pf_eth_in_hook);
6517 VNET_DEFINE_STATIC(pfil_hook_t, pf_eth_out_hook);
6518 #define V_pf_eth_in_hook VNET(pf_eth_in_hook)
6519 #define V_pf_eth_out_hook VNET(pf_eth_out_hook)
6520
6521 #ifdef INET
6522 VNET_DEFINE_STATIC(pfil_hook_t, pf_ip4_in_hook);
6523 VNET_DEFINE_STATIC(pfil_hook_t, pf_ip4_out_hook);
6524 #define V_pf_ip4_in_hook VNET(pf_ip4_in_hook)
6525 #define V_pf_ip4_out_hook VNET(pf_ip4_out_hook)
6526 #endif
6527 #ifdef INET6
6528 VNET_DEFINE_STATIC(pfil_hook_t, pf_ip6_in_hook);
6529 VNET_DEFINE_STATIC(pfil_hook_t, pf_ip6_out_hook);
6530 #define V_pf_ip6_in_hook VNET(pf_ip6_in_hook)
6531 #define V_pf_ip6_out_hook VNET(pf_ip6_out_hook)
6532 #endif
6533
6534 static void
hook_pf_eth(void)6535 hook_pf_eth(void)
6536 {
6537 struct pfil_hook_args pha = {
6538 .pa_version = PFIL_VERSION,
6539 .pa_modname = "pf",
6540 .pa_type = PFIL_TYPE_ETHERNET,
6541 };
6542 struct pfil_link_args pla = {
6543 .pa_version = PFIL_VERSION,
6544 };
6545 int ret __diagused;
6546
6547 if (atomic_load_bool(&V_pf_pfil_eth_hooked))
6548 return;
6549
6550 pha.pa_mbuf_chk = pf_eth_check_in;
6551 pha.pa_flags = PFIL_IN;
6552 pha.pa_rulname = "eth-in";
6553 V_pf_eth_in_hook = pfil_add_hook(&pha);
6554 pla.pa_flags = PFIL_IN | PFIL_HEADPTR | PFIL_HOOKPTR;
6555 pla.pa_head = V_link_pfil_head;
6556 pla.pa_hook = V_pf_eth_in_hook;
6557 ret = pfil_link(&pla);
6558 MPASS(ret == 0);
6559 pha.pa_mbuf_chk = pf_eth_check_out;
6560 pha.pa_flags = PFIL_OUT;
6561 pha.pa_rulname = "eth-out";
6562 V_pf_eth_out_hook = pfil_add_hook(&pha);
6563 pla.pa_flags = PFIL_OUT | PFIL_HEADPTR | PFIL_HOOKPTR;
6564 pla.pa_head = V_link_pfil_head;
6565 pla.pa_hook = V_pf_eth_out_hook;
6566 ret = pfil_link(&pla);
6567 MPASS(ret == 0);
6568
6569 atomic_store_bool(&V_pf_pfil_eth_hooked, true);
6570 }
6571
6572 static void
hook_pf(void)6573 hook_pf(void)
6574 {
6575 struct pfil_hook_args pha = {
6576 .pa_version = PFIL_VERSION,
6577 .pa_modname = "pf",
6578 };
6579 struct pfil_link_args pla = {
6580 .pa_version = PFIL_VERSION,
6581 };
6582 int ret __diagused;
6583
6584 if (atomic_load_bool(&V_pf_pfil_hooked))
6585 return;
6586
6587 #ifdef INET
6588 pha.pa_type = PFIL_TYPE_IP4;
6589 pha.pa_mbuf_chk = pf_check_in;
6590 pha.pa_flags = PFIL_IN;
6591 pha.pa_rulname = "default-in";
6592 V_pf_ip4_in_hook = pfil_add_hook(&pha);
6593 pla.pa_flags = PFIL_IN | PFIL_HEADPTR | PFIL_HOOKPTR;
6594 pla.pa_head = V_inet_pfil_head;
6595 pla.pa_hook = V_pf_ip4_in_hook;
6596 ret = pfil_link(&pla);
6597 MPASS(ret == 0);
6598 pha.pa_mbuf_chk = pf_check_out;
6599 pha.pa_flags = PFIL_OUT;
6600 pha.pa_rulname = "default-out";
6601 V_pf_ip4_out_hook = pfil_add_hook(&pha);
6602 pla.pa_flags = PFIL_OUT | PFIL_HEADPTR | PFIL_HOOKPTR;
6603 pla.pa_head = V_inet_pfil_head;
6604 pla.pa_hook = V_pf_ip4_out_hook;
6605 ret = pfil_link(&pla);
6606 MPASS(ret == 0);
6607 if (V_pf_filter_local) {
6608 pla.pa_flags = PFIL_OUT | PFIL_HEADPTR | PFIL_HOOKPTR;
6609 pla.pa_head = V_inet_local_pfil_head;
6610 pla.pa_hook = V_pf_ip4_out_hook;
6611 ret = pfil_link(&pla);
6612 MPASS(ret == 0);
6613 }
6614 #endif
6615 #ifdef INET6
6616 pha.pa_type = PFIL_TYPE_IP6;
6617 pha.pa_mbuf_chk = pf_check6_in;
6618 pha.pa_flags = PFIL_IN;
6619 pha.pa_rulname = "default-in6";
6620 V_pf_ip6_in_hook = pfil_add_hook(&pha);
6621 pla.pa_flags = PFIL_IN | PFIL_HEADPTR | PFIL_HOOKPTR;
6622 pla.pa_head = V_inet6_pfil_head;
6623 pla.pa_hook = V_pf_ip6_in_hook;
6624 ret = pfil_link(&pla);
6625 MPASS(ret == 0);
6626 pha.pa_mbuf_chk = pf_check6_out;
6627 pha.pa_rulname = "default-out6";
6628 pha.pa_flags = PFIL_OUT;
6629 V_pf_ip6_out_hook = pfil_add_hook(&pha);
6630 pla.pa_flags = PFIL_OUT | PFIL_HEADPTR | PFIL_HOOKPTR;
6631 pla.pa_head = V_inet6_pfil_head;
6632 pla.pa_hook = V_pf_ip6_out_hook;
6633 ret = pfil_link(&pla);
6634 MPASS(ret == 0);
6635 if (V_pf_filter_local) {
6636 pla.pa_flags = PFIL_OUT | PFIL_HEADPTR | PFIL_HOOKPTR;
6637 pla.pa_head = V_inet6_local_pfil_head;
6638 pla.pa_hook = V_pf_ip6_out_hook;
6639 ret = pfil_link(&pla);
6640 MPASS(ret == 0);
6641 }
6642 #endif
6643
6644 atomic_store_bool(&V_pf_pfil_hooked, true);
6645 }
6646
6647 static void
dehook_pf_eth(void)6648 dehook_pf_eth(void)
6649 {
6650
6651 if (!atomic_load_bool(&V_pf_pfil_eth_hooked))
6652 return;
6653
6654 pfil_remove_hook(V_pf_eth_in_hook);
6655 pfil_remove_hook(V_pf_eth_out_hook);
6656
6657 atomic_store_bool(&V_pf_pfil_eth_hooked, false);
6658 }
6659
6660 static void
dehook_pf(void)6661 dehook_pf(void)
6662 {
6663
6664 if (!atomic_load_bool(&V_pf_pfil_hooked))
6665 return;
6666
6667 #ifdef INET
6668 pfil_remove_hook(V_pf_ip4_in_hook);
6669 pfil_remove_hook(V_pf_ip4_out_hook);
6670 #endif
6671 #ifdef INET6
6672 pfil_remove_hook(V_pf_ip6_in_hook);
6673 pfil_remove_hook(V_pf_ip6_out_hook);
6674 #endif
6675
6676 atomic_store_bool(&V_pf_pfil_hooked, false);
6677 }
6678
6679 static void
pf_load_vnet(void)6680 pf_load_vnet(void)
6681 {
6682 V_pf_tag_z = uma_zcreate("pf tags", sizeof(struct pf_tagname),
6683 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
6684
6685 rm_init_flags(&V_pf_rules_lock, "pf rulesets", RM_RECURSE);
6686 sx_init(&V_pf_ioctl_lock, "pf ioctl");
6687
6688 pf_init_tagset(&V_pf_tags, &pf_rule_tag_hashsize,
6689 PF_RULE_TAG_HASH_SIZE_DEFAULT);
6690 #ifdef ALTQ
6691 pf_init_tagset(&V_pf_qids, &pf_queue_tag_hashsize,
6692 PF_QUEUE_TAG_HASH_SIZE_DEFAULT);
6693 #endif
6694
6695 V_pf_keth = &V_pf_main_keth_anchor.ruleset;
6696
6697 pfattach_vnet();
6698 V_pf_vnet_active = 1;
6699 }
6700
6701 static int
pf_load(void)6702 pf_load(void)
6703 {
6704 int error;
6705
6706 sx_init(&pf_end_lock, "pf end thread");
6707
6708 pf_mtag_initialize();
6709
6710 pf_dev = make_dev(&pf_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600, PF_NAME);
6711 if (pf_dev == NULL)
6712 return (ENOMEM);
6713
6714 pf_end_threads = 0;
6715 error = kproc_create(pf_purge_thread, NULL, &pf_purge_proc, 0, 0, "pf purge");
6716 if (error != 0)
6717 return (error);
6718
6719 pfi_initialize();
6720
6721 return (0);
6722 }
6723
6724 static void
pf_unload_vnet(void)6725 pf_unload_vnet(void)
6726 {
6727 int ret __diagused;
6728
6729 V_pf_vnet_active = 0;
6730 V_pf_status.running = 0;
6731 dehook_pf();
6732 dehook_pf_eth();
6733
6734 PF_RULES_WLOCK();
6735 pf_syncookies_cleanup();
6736 shutdown_pf();
6737 PF_RULES_WUNLOCK();
6738
6739 /* Make sure we've cleaned up ethernet rules before we continue. */
6740 NET_EPOCH_DRAIN_CALLBACKS();
6741
6742 ret = swi_remove(V_pf_swi_cookie);
6743 MPASS(ret == 0);
6744 ret = intr_event_destroy(V_pf_swi_ie);
6745 MPASS(ret == 0);
6746
6747 pf_unload_vnet_purge();
6748
6749 pf_normalize_cleanup();
6750 PF_RULES_WLOCK();
6751 pfi_cleanup_vnet();
6752 PF_RULES_WUNLOCK();
6753 pfr_cleanup();
6754 pf_osfp_flush();
6755 pf_cleanup();
6756 if (IS_DEFAULT_VNET(curvnet))
6757 pf_mtag_cleanup();
6758
6759 pf_cleanup_tagset(&V_pf_tags);
6760 #ifdef ALTQ
6761 pf_cleanup_tagset(&V_pf_qids);
6762 #endif
6763 uma_zdestroy(V_pf_tag_z);
6764
6765 #ifdef PF_WANT_32_TO_64_COUNTER
6766 PF_RULES_WLOCK();
6767 LIST_REMOVE(V_pf_kifmarker, pfik_allkiflist);
6768
6769 MPASS(LIST_EMPTY(&V_pf_allkiflist));
6770 MPASS(V_pf_allkifcount == 0);
6771
6772 LIST_REMOVE(&V_pf_default_rule, allrulelist);
6773 V_pf_allrulecount--;
6774 LIST_REMOVE(V_pf_rulemarker, allrulelist);
6775
6776 MPASS(LIST_EMPTY(&V_pf_allrulelist));
6777 MPASS(V_pf_allrulecount == 0);
6778
6779 PF_RULES_WUNLOCK();
6780
6781 free(V_pf_kifmarker, PFI_MTYPE);
6782 free(V_pf_rulemarker, M_PFRULE);
6783 #endif
6784
6785 /* Free counters last as we updated them during shutdown. */
6786 pf_counter_u64_deinit(&V_pf_default_rule.evaluations);
6787 for (int i = 0; i < 2; i++) {
6788 pf_counter_u64_deinit(&V_pf_default_rule.packets[i]);
6789 pf_counter_u64_deinit(&V_pf_default_rule.bytes[i]);
6790 }
6791 counter_u64_free(V_pf_default_rule.states_cur);
6792 counter_u64_free(V_pf_default_rule.states_tot);
6793 counter_u64_free(V_pf_default_rule.src_nodes);
6794 uma_zfree_pcpu(pf_timestamp_pcpu_zone, V_pf_default_rule.timestamp);
6795
6796 for (int i = 0; i < PFRES_MAX; i++)
6797 counter_u64_free(V_pf_status.counters[i]);
6798 for (int i = 0; i < KLCNT_MAX; i++)
6799 counter_u64_free(V_pf_status.lcounters[i]);
6800 for (int i = 0; i < FCNT_MAX; i++)
6801 pf_counter_u64_deinit(&V_pf_status.fcounters[i]);
6802 for (int i = 0; i < SCNT_MAX; i++)
6803 counter_u64_free(V_pf_status.scounters[i]);
6804
6805 rm_destroy(&V_pf_rules_lock);
6806 sx_destroy(&V_pf_ioctl_lock);
6807 }
6808
6809 static void
pf_unload(void)6810 pf_unload(void)
6811 {
6812
6813 sx_xlock(&pf_end_lock);
6814 pf_end_threads = 1;
6815 while (pf_end_threads < 2) {
6816 wakeup_one(pf_purge_thread);
6817 sx_sleep(pf_purge_proc, &pf_end_lock, 0, "pftmo", 0);
6818 }
6819 sx_xunlock(&pf_end_lock);
6820
6821 pf_nl_unregister();
6822
6823 if (pf_dev != NULL)
6824 destroy_dev(pf_dev);
6825
6826 pfi_cleanup();
6827
6828 sx_destroy(&pf_end_lock);
6829 }
6830
6831 static void
vnet_pf_init(void * unused __unused)6832 vnet_pf_init(void *unused __unused)
6833 {
6834
6835 pf_load_vnet();
6836 }
6837 VNET_SYSINIT(vnet_pf_init, SI_SUB_PROTO_FIREWALL, SI_ORDER_THIRD,
6838 vnet_pf_init, NULL);
6839
6840 static void
vnet_pf_uninit(const void * unused __unused)6841 vnet_pf_uninit(const void *unused __unused)
6842 {
6843
6844 pf_unload_vnet();
6845 }
6846 SYSUNINIT(pf_unload, SI_SUB_PROTO_FIREWALL, SI_ORDER_SECOND, pf_unload, NULL);
6847 VNET_SYSUNINIT(vnet_pf_uninit, SI_SUB_PROTO_FIREWALL, SI_ORDER_THIRD,
6848 vnet_pf_uninit, NULL);
6849
6850 static int
pf_modevent(module_t mod,int type,void * data)6851 pf_modevent(module_t mod, int type, void *data)
6852 {
6853 int error = 0;
6854
6855 switch(type) {
6856 case MOD_LOAD:
6857 error = pf_load();
6858 pf_nl_register();
6859 break;
6860 case MOD_UNLOAD:
6861 /* Handled in SYSUNINIT(pf_unload) to ensure it's done after
6862 * the vnet_pf_uninit()s */
6863 break;
6864 default:
6865 error = EINVAL;
6866 break;
6867 }
6868
6869 return (error);
6870 }
6871
6872 static moduledata_t pf_mod = {
6873 "pf",
6874 pf_modevent,
6875 0
6876 };
6877
6878 DECLARE_MODULE(pf, pf_mod, SI_SUB_PROTO_FIREWALL, SI_ORDER_SECOND);
6879 MODULE_DEPEND(pf, netlink, 1, 1, 1);
6880 MODULE_VERSION(pf, PF_MODVER);
6881