xref: /freebsd/lib/libpfctl/libpfctl.h (revision 0972294ef034d92f59857b8312dd2e1e3a7adc9c)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2021 Rubicon Communications, LLC (Netgate)
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  *    - Redistributions of source code must retain the above copyright
12  *      notice, this list of conditions and the following disclaimer.
13  *    - Redistributions in binary form must reproduce the above
14  *      copyright notice, this list of conditions and the following
15  *      disclaimer in the documentation and/or other materials provided
16  *      with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #ifndef _PFCTL_IOCTL_H_
33 #define _PFCTL_IOCTL_H_
34 
35 #include <netpfil/pf/pf.h>
36 
37 struct pfctl_anchor;
38 struct pfctl_eth_anchor;
39 
40 struct pfctl_status_counter {
41 	uint64_t	 id;
42 	uint64_t	 counter;
43 	char		*name;
44 
45 	TAILQ_ENTRY(pfctl_status_counter) entry;
46 };
47 TAILQ_HEAD(pfctl_status_counters, pfctl_status_counter);
48 
49 struct pfctl_status {
50 	bool		running;
51 	uint32_t	since;
52 	uint32_t	debug;
53 	uint32_t	hostid;
54 	uint64_t	states;
55 	uint64_t	src_nodes;
56 	char		ifname[IFNAMSIZ];
57 	uint8_t		pf_chksum[PF_MD5_DIGEST_LENGTH];
58 	bool		syncookies_active;
59 	uint32_t	reass;
60 
61 	struct pfctl_status_counters	 counters;
62 	struct pfctl_status_counters	 lcounters;
63 	struct pfctl_status_counters	 fcounters;
64 	struct pfctl_status_counters	 scounters;
65 	uint64_t	pcounters[2][2][2];
66 	uint64_t	bcounters[2][2];
67 };
68 
69 struct pfctl_eth_rulesets_info {
70 	uint32_t	nr;
71 };
72 
73 struct pfctl_eth_rules_info {
74 	uint32_t	nr;
75 	uint32_t	ticket;
76 };
77 
78 struct pfctl_eth_addr {
79 	uint8_t	addr[ETHER_ADDR_LEN];
80 	uint8_t	mask[ETHER_ADDR_LEN];
81 	bool	neg;
82 	bool	isset;
83 };
84 
85 struct pfctl_eth_rule {
86 	uint32_t		 nr;
87 
88 	char			label[PF_RULE_MAX_LABEL_COUNT][PF_RULE_LABEL_SIZE];
89 	uint32_t		ridentifier;
90 
91 	bool			 quick;
92 
93 	/* Filter */
94 	char			 ifname[IFNAMSIZ];
95 	uint8_t			 ifnot;
96 	uint8_t			 direction;
97 	uint16_t		 proto;
98 	struct pfctl_eth_addr	 src, dst;
99 	struct pf_rule_addr	 ipsrc, ipdst;
100 	char			 match_tagname[PF_TAG_NAME_SIZE];
101 	uint16_t		 match_tag;
102 	bool			 match_tag_not;
103 
104 	/* Stats */
105 	uint64_t		 evaluations;
106 	uint64_t		 packets[2];
107 	uint64_t		 bytes[2];
108 	time_t			 last_active_timestamp;
109 
110 	/* Action */
111 	char			 qname[PF_QNAME_SIZE];
112 	char			 tagname[PF_TAG_NAME_SIZE];
113 	uint16_t		 dnpipe;
114 	uint32_t		 dnflags;
115 	char			 bridge_to[IFNAMSIZ];
116 	uint8_t			 action;
117 
118 	struct pfctl_eth_anchor	*anchor;
119 	uint8_t			 anchor_relative;
120 	uint8_t			 anchor_wildcard;
121 
122 	TAILQ_ENTRY(pfctl_eth_rule)	 entries;
123 };
124 TAILQ_HEAD(pfctl_eth_rules, pfctl_eth_rule);
125 
126 struct pfctl_eth_ruleset_info {
127 	uint32_t	nr;
128 	char		name[PF_ANCHOR_NAME_SIZE];
129 	char		path[MAXPATHLEN];
130 };
131 
132 struct pfctl_eth_ruleset {
133 	struct pfctl_eth_rules	 rules;
134 	struct pfctl_eth_anchor	*anchor;
135 };
136 
137 struct pfctl_eth_anchor {
138 	struct pfctl_eth_anchor		*parent;
139 	char				 name[PF_ANCHOR_NAME_SIZE];
140 	char				 path[MAXPATHLEN];
141 	struct pfctl_eth_ruleset	 ruleset;
142 	int				 refcnt;	/* anchor rules */
143 	int				 match;	/* XXX: used for pfctl black magic */
144 };
145 
146 struct pfctl_pool {
147 	struct pf_palist	 list;
148 	struct pf_pooladdr	*cur;
149 	struct pf_poolhashkey	 key;
150 	struct pf_addr		 counter;
151 	struct pf_mape_portset	 mape;
152 	int			 tblidx;
153 	uint16_t		 proxy_port[2];
154 	uint8_t			 opts;
155 };
156 
157 struct pfctl_rules_info {
158 	uint32_t	nr;
159 	uint32_t	ticket;
160 };
161 
162 struct pfctl_rule {
163 	struct pf_rule_addr	 src;
164 	struct pf_rule_addr	 dst;
165 	union pf_rule_ptr	 skip[PF_SKIP_COUNT];
166 	char			 label[PF_RULE_MAX_LABEL_COUNT][PF_RULE_LABEL_SIZE];
167 	uint32_t		 ridentifier;
168 	char			 ifname[IFNAMSIZ];
169 	char			 qname[PF_QNAME_SIZE];
170 	char			 pqname[PF_QNAME_SIZE];
171 	char			 tagname[PF_TAG_NAME_SIZE];
172 	char			 match_tagname[PF_TAG_NAME_SIZE];
173 
174 	char			 overload_tblname[PF_TABLE_NAME_SIZE];
175 
176 	TAILQ_ENTRY(pfctl_rule)	 entries;
177 	struct pfctl_pool	 nat;
178 	union {
179 		/* Alias old and new names. */
180 		struct pfctl_pool	 rpool;
181 		struct pfctl_pool	 rdr;
182 	};
183 	struct pfctl_pool	 route;
184 
185 	uint64_t		 evaluations;
186 	uint64_t		 packets[2];
187 	uint64_t		 bytes[2];
188 	time_t			 last_active_timestamp;
189 
190 	struct pfi_kif		*kif;
191 	struct pfctl_anchor	*anchor;
192 	struct pfr_ktable	*overload_tbl;
193 
194 	pf_osfp_t		 os_fingerprint;
195 
196 	int			 rtableid;
197 	uint32_t		 timeout[PFTM_MAX];
198 	uint32_t		 max_states;
199 	uint32_t		 max_src_nodes;
200 	uint32_t		 max_src_states;
201 	uint32_t		 max_src_conn;
202 	struct {
203 		uint32_t		limit;
204 		uint32_t		seconds;
205 	}			 max_src_conn_rate;
206 	uint32_t		 qid;
207 	uint32_t		 pqid;
208 	uint16_t		 dnpipe;
209 	uint16_t		 dnrpipe;
210 	uint32_t		 free_flags;
211 	uint32_t		 nr;
212 	uint32_t		 prob;
213 	uid_t			 cuid;
214 	pid_t			 cpid;
215 
216 	uint64_t		 states_cur;
217 	uint64_t		 states_tot;
218 	uint64_t		 src_nodes;
219 
220 	uint16_t		 return_icmp;
221 	uint16_t		 return_icmp6;
222 	uint16_t		 max_mss;
223 	uint16_t		 tag;
224 	uint16_t		 match_tag;
225 	uint16_t		 scrub_flags;
226 
227 	struct pf_rule_uid	 uid;
228 	struct pf_rule_gid	 gid;
229 	char			 rcv_ifname[IFNAMSIZ];
230 
231 	uint32_t		 rule_flag;
232 	uint8_t			 action;
233 	uint8_t			 direction;
234 	uint8_t			 log;
235 	uint8_t			 logif;
236 	uint8_t			 quick;
237 	uint8_t			 ifnot;
238 	uint8_t			 match_tag_not;
239 	uint8_t			 natpass;
240 
241 	uint8_t			 keep_state;
242 	sa_family_t		 af;
243 	uint8_t			 proto;
244 	uint8_t			 type;
245 	uint8_t			 code;
246 	uint8_t			 flags;
247 	uint8_t			 flagset;
248 	uint8_t			 min_ttl;
249 	uint8_t			 allow_opts;
250 	uint8_t			 rt;
251 	uint8_t			 return_ttl;
252 	uint8_t			 tos;
253 	uint8_t			 set_tos;
254 	uint8_t			 anchor_relative;
255 	uint8_t			 anchor_wildcard;
256 
257 	uint8_t			 flush;
258 	uint8_t			 prio;
259 	uint8_t			 set_prio[2];
260 	sa_family_t		 naf;
261 
262 	struct {
263 		struct pf_addr		addr;
264 		uint16_t		port;
265 	}			divert;
266 };
267 
268 TAILQ_HEAD(pfctl_rulequeue, pfctl_rule);
269 
270 struct pfctl_ruleset {
271 	struct {
272 		struct pfctl_rulequeue	 queues[2];
273 		struct {
274 			struct pfctl_rulequeue	*ptr;
275 			struct pfctl_rule	**ptr_array;
276 			uint32_t		 rcount;
277 			uint32_t		 ticket;
278 			int			 open;
279 		}			 active, inactive;
280 	}			 rules[PF_RULESET_MAX];
281 	struct pfctl_anchor	*anchor;
282 	uint32_t		 tticket;
283 	int			 tables;
284 	int			 topen;
285 };
286 
287 RB_HEAD(pfctl_anchor_global, pfctl_anchor);
288 RB_HEAD(pfctl_anchor_node, pfctl_anchor);
289 struct pfctl_anchor {
290 	RB_ENTRY(pfctl_anchor)	 entry_global;
291 	RB_ENTRY(pfctl_anchor)	 entry_node;
292 	struct pfctl_anchor	*parent;
293 	struct pfctl_anchor_node children;
294 	char			 name[PF_ANCHOR_NAME_SIZE];
295 	char			 path[MAXPATHLEN];
296 	struct pfctl_ruleset	 ruleset;
297 	int			 refcnt;	/* anchor rules */
298 	int			 match;	/* XXX: used for pfctl black magic */
299 };
300 RB_PROTOTYPE(pfctl_anchor_global, pfctl_anchor, entry_global,
301     pf_anchor_compare);
302 RB_PROTOTYPE(pfctl_anchor_node, pfctl_anchor, entry_node,
303     pf_anchor_compare);
304 
305 struct pfctl_state_cmp {
306 	uint64_t	id;
307 	uint32_t	creatorid;
308 	uint8_t		direction;
309 };
310 
311 struct pfctl_kill {
312 	struct pfctl_state_cmp	cmp;
313 	sa_family_t		af;
314 	int			proto;
315 	struct pf_rule_addr	src;
316 	struct pf_rule_addr	dst;
317 	struct pf_rule_addr	rt_addr;
318 	char			ifname[IFNAMSIZ];
319 	char			label[PF_RULE_LABEL_SIZE];
320 	bool			kill_match;
321 	bool			nat;
322 };
323 
324 struct pfctl_state_peer {
325 	uint32_t			 seqlo;
326 	uint32_t			 seqhi;
327 	uint32_t			 seqdiff;
328 	uint8_t				 state;
329 	uint8_t				 wscale;
330 };
331 
332 struct pfctl_state_key {
333 	struct pf_addr	 addr[2];
334 	uint16_t	 port[2];
335 	sa_family_t	 af;
336 	uint8_t	 	 proto;
337 };
338 
339 struct pfctl_state {
340 	TAILQ_ENTRY(pfctl_state)	entry;
341 
342 	uint64_t		 id;
343 	uint32_t		 creatorid;
344 	uint8_t		 	 direction;
345 
346 	struct pfctl_state_peer	 src;
347 	struct pfctl_state_peer	 dst;
348 
349 	uint32_t		 rule;
350 	uint32_t		 anchor;
351 	uint32_t		 nat_rule;
352 	struct pf_addr		 rt_addr;
353 	struct pfctl_state_key	 key[2];	/* addresses stack and wire  */
354 	char			 ifname[IFNAMSIZ];
355 	char			 orig_ifname[IFNAMSIZ];
356 	uint64_t		 packets[2];
357 	uint64_t		 bytes[2];
358 	uint32_t		 creation;
359 	uint32_t		 expire;
360 	uint32_t		 pfsync_time;
361 	uint16_t		 state_flags;
362 	uint32_t		 sync_flags;
363 	uint16_t		 qid;
364 	uint16_t		 pqid;
365 	uint16_t		 dnpipe;
366 	uint16_t		 dnrpipe;
367 	uint8_t			 log;
368 	int32_t			 rtableid;
369 	uint8_t			 min_ttl;
370 	uint8_t			 set_tos;
371 	uint16_t		 max_mss;
372 	uint8_t			 set_prio[2];
373 	uint8_t			 rt;
374 	char			 rt_ifname[IFNAMSIZ];
375 };
376 
377 TAILQ_HEAD(pfctl_statelist, pfctl_state);
378 struct pfctl_states {
379 	struct pfctl_statelist	states;
380 };
381 
382 enum pfctl_syncookies_mode {
383 	PFCTL_SYNCOOKIES_NEVER,
384 	PFCTL_SYNCOOKIES_ALWAYS,
385 	PFCTL_SYNCOOKIES_ADAPTIVE
386 };
387 extern const char* PFCTL_SYNCOOKIES_MODE_NAMES[];
388 
389 struct pfctl_syncookies {
390 	enum pfctl_syncookies_mode	mode;
391 	uint8_t				highwater;	/* Percent */
392 	uint8_t				lowwater;	/* Percent */
393 	uint32_t			halfopen_states;
394 };
395 
396 struct pfctl_threshold {
397 	uint32_t		limit;
398 	uint32_t		seconds;
399 	uint32_t		count;
400 	uint32_t		last;
401 };
402 
403 struct pfctl_src_node {
404 	struct pf_addr		addr;
405 	struct pf_addr		raddr;
406 	int			rule;
407 	uint64_t		bytes[2];
408 	uint64_t		packets[2];
409 	uint32_t		states;
410 	uint32_t		conn;
411 	sa_family_t		af;
412 	sa_family_t		naf;
413 	uint8_t			ruletype;
414 	uint64_t		creation;
415 	uint64_t		expire;
416 	struct pfctl_threshold	conn_rate;
417 };
418 
419 #define	PF_DEVICE	"/dev/pf"
420 
421 struct pfctl_handle;
422 struct pfctl_handle	*pfctl_open(const char *pf_device);
423 void	pfctl_close(struct pfctl_handle *);
424 int	pfctl_fd(struct pfctl_handle *);
425 
426 int	pfctl_startstop(struct pfctl_handle *h, int start);
427 struct pfctl_status* pfctl_get_status_h(struct pfctl_handle *h);
428 struct pfctl_status* pfctl_get_status(int dev);
429 int	pfctl_clear_status(struct pfctl_handle *h);
430 uint64_t pfctl_status_counter(struct pfctl_status *status, int id);
431 uint64_t pfctl_status_lcounter(struct pfctl_status *status, int id);
432 uint64_t pfctl_status_fcounter(struct pfctl_status *status, int id);
433 uint64_t pfctl_status_scounter(struct pfctl_status *status, int id);
434 void	pfctl_free_status(struct pfctl_status *status);
435 
436 int	pfctl_get_eth_rulesets_info(int dev,
437 	    struct pfctl_eth_rulesets_info *ri, const char *path);
438 int	pfctl_get_eth_ruleset(int dev, const char *path, int nr,
439 	    struct pfctl_eth_ruleset_info *ri);
440 int	pfctl_get_eth_rules_info(int dev, struct pfctl_eth_rules_info *rules,
441 	    const char *path);
442 int	pfctl_get_eth_rule(int dev, uint32_t nr, uint32_t ticket,
443 	    const char *path, struct pfctl_eth_rule *rule, bool clear,
444 	    char *anchor_call);
445 int	pfctl_add_eth_rule(int dev, const struct pfctl_eth_rule *r,
446 	    const char *anchor, const char *anchor_call, uint32_t ticket);
447 int	pfctl_get_rules_info_h(struct pfctl_handle *h,
448 	    struct pfctl_rules_info *rules, uint32_t ruleset,
449 	    const char *path);
450 int	pfctl_get_rules_info(int dev, struct pfctl_rules_info *rules,
451 	    uint32_t ruleset, const char *path);
452 int	pfctl_get_rule(int dev, uint32_t nr, uint32_t ticket,
453 	    const char *anchor, uint32_t ruleset, struct pfctl_rule *rule,
454 	    char *anchor_call);
455 int	pfctl_get_rule_h(struct pfctl_handle *h, uint32_t nr, uint32_t ticket,
456 	    const char *anchor, uint32_t ruleset, struct pfctl_rule *rule,
457 	    char *anchor_call);
458 int	pfctl_get_clear_rule(int dev, uint32_t nr, uint32_t ticket,
459 	    const char *anchor, uint32_t ruleset, struct pfctl_rule *rule,
460 	    char *anchor_call, bool clear);
461 int	pfctl_get_clear_rule_h(struct pfctl_handle *h, uint32_t nr, uint32_t ticket,
462 	    const char *anchor, uint32_t ruleset, struct pfctl_rule *rule,
463 	    char *anchor_call, bool clear);
464 int	pfctl_add_rule(int dev, const struct pfctl_rule *r,
465 	    const char *anchor, const char *anchor_call, uint32_t ticket,
466 	    uint32_t pool_ticket);
467 int	pfctl_add_rule_h(struct pfctl_handle *h, const struct pfctl_rule *r,
468 	    const char *anchor, const char *anchor_call, uint32_t ticket,
469 	    uint32_t pool_ticket);
470 int	pfctl_set_keepcounters(int dev, bool keep);
471 int	pfctl_get_creatorids(struct pfctl_handle *h, uint32_t *creators, size_t *len);
472 
473 struct pfctl_state_filter {
474 	char			ifname[IFNAMSIZ];
475 	uint16_t		proto;
476 	sa_family_t		af;
477 	struct pf_addr		addr;
478 	struct pf_addr		mask;
479 };
480 typedef int (*pfctl_get_state_fn)(struct pfctl_state *, void *);
481 int pfctl_get_states_iter(pfctl_get_state_fn f, void *arg);
482 int pfctl_get_filtered_states_iter(struct pfctl_state_filter *filter, pfctl_get_state_fn f, void *arg);
483 int	pfctl_get_states(int dev, struct pfctl_states *states);
484 void	pfctl_free_states(struct pfctl_states *states);
485 int	pfctl_clear_states(int dev, const struct pfctl_kill *kill,
486 	    unsigned int *killed);
487 int	pfctl_kill_states(int dev, const struct pfctl_kill *kill,
488 	    unsigned int *killed);
489 int	pfctl_clear_states_h(struct pfctl_handle *h, const struct pfctl_kill *kill,
490 	    unsigned int *killed);
491 int	pfctl_kill_states_h(struct pfctl_handle *h, const struct pfctl_kill *kill,
492 	    unsigned int *killed);
493 int	pfctl_clear_rules(int dev, const char *anchorname);
494 int	pfctl_clear_nat(int dev, const char *anchorname);
495 int	pfctl_clear_eth_rules(int dev, const char *anchorname);
496 int	pfctl_set_syncookies(int dev, const struct pfctl_syncookies *s);
497 int	pfctl_get_syncookies(int dev, struct pfctl_syncookies *s);
498 int	pfctl_table_add_addrs(int dev, struct pfr_table *tbl, struct pfr_addr
499 	    *addr, int size, int *nadd, int flags);
500 int	pfctl_table_del_addrs(int dev, struct pfr_table *tbl, struct pfr_addr
501 	    *addr, int size, int *ndel, int flags);
502 int     pfctl_table_set_addrs(int dev, struct pfr_table *tbl, struct pfr_addr
503 	    *addr, int size, int *size2, int *nadd, int *ndel, int *nchange,
504 	    int flags);
505 int	pfctl_table_get_addrs(int dev, struct pfr_table *tbl, struct pfr_addr
506 	    *addr, int *size, int flags);
507 int	pfctl_set_statusif(struct pfctl_handle *h, const char *ifname);
508 
509 struct pfctl_natlook_key {
510 	sa_family_t af;
511 	uint8_t direction;
512 	uint8_t proto;
513 	struct pf_addr saddr;
514 	struct pf_addr daddr;
515 	uint16_t sport;
516 	uint16_t dport;
517 };
518 struct pfctl_natlook {
519 	struct pf_addr saddr;
520 	struct pf_addr daddr;
521 	uint16_t sport;
522 	uint16_t dport;
523 };
524 int	pfctl_natlook(struct pfctl_handle *h,
525 	    const struct pfctl_natlook_key *k, struct pfctl_natlook *r);
526 int	pfctl_set_debug(struct pfctl_handle *h, uint32_t level);
527 int	pfctl_set_timeout(struct pfctl_handle *h, uint32_t timeout, uint32_t seconds);
528 int	pfctl_get_timeout(struct pfctl_handle *h, uint32_t timeout, uint32_t *seconds);
529 int	pfctl_set_limit(struct pfctl_handle *h, const int index, const uint limit);
530 int	pfctl_get_limit(struct pfctl_handle *h, const int index, uint *limit);
531 int	pfctl_begin_addrs(struct pfctl_handle *h, uint32_t *ticket);
532 int	pfctl_add_addr(struct pfctl_handle *h, const struct pfioc_pooladdr *pa, int which);
533 int	pfctl_get_addrs(struct pfctl_handle *h, uint32_t ticket, uint32_t r_num,
534 	    uint8_t r_action, const char *anchor, uint32_t *nr, int which);
535 int	pfctl_get_addr(struct pfctl_handle *h, uint32_t ticket, uint32_t r_num,
536 	    uint8_t r_action, const char *anchor, uint32_t nr, struct pfioc_pooladdr *pa,
537 	    int which);
538 int	pfctl_get_rulesets(struct pfctl_handle *h, const char *path, uint32_t *nr);
539 int	pfctl_get_ruleset(struct pfctl_handle *h, const char *path, uint32_t nr, struct pfioc_ruleset *rs);
540 typedef int (*pfctl_get_srcnode_fn)(struct pfctl_src_node*, void *);
541 int	pfctl_get_srcnodes(struct pfctl_handle *h, pfctl_get_srcnode_fn fn, void *arg);
542 
543 int	pfctl_clear_tables(struct pfctl_handle *h, struct pfr_table *filter,
544 	    int *ndel, int flags);
545 
546 #endif
547