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