xref: /freebsd/sys/netpfil/pf/pf_nl.c (revision dd21556857e8d40f66bf5ad54754d9d52669ebf7)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2023 Alexander V. Chernikov <melifaro@FreeBSD.org>
5  * Copyright (c) 2023 Rubicon Communications, LLC (Netgate)
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  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  */
29 #include <sys/cdefs.h>
30 #include "opt_inet.h"
31 #include "opt_inet6.h"
32 
33 #include <sys/param.h>
34 #include <sys/malloc.h>
35 #include <sys/mbuf.h>
36 #include <sys/priv.h>
37 #include <sys/socket.h>
38 #include <sys/ucred.h>
39 
40 #include <net/pfvar.h>
41 
42 #include <netlink/netlink.h>
43 #include <netlink/netlink_ctl.h>
44 #include <netlink/netlink_generic.h>
45 #include <netlink/netlink_message_writer.h>
46 
47 #include <netpfil/pf/pf_nl.h>
48 
49 #define	DEBUG_MOD_NAME	nl_pf
50 #define	DEBUG_MAX_LEVEL	LOG_DEBUG3
51 #include <netlink/netlink_debug.h>
52 _DECLARE_DEBUG(LOG_DEBUG);
53 
54 struct nl_parsed_state {
55 	uint8_t		version;
56 	uint32_t	id;
57 	uint32_t	creatorid;
58 	char		ifname[IFNAMSIZ];
59 	uint16_t	proto;
60 	sa_family_t	af;
61 	struct pf_addr	addr;
62 	struct pf_addr	mask;
63 };
64 
65 #define	_IN(_field)	offsetof(struct genlmsghdr, _field)
66 #define	_OUT(_field)	offsetof(struct nl_parsed_state, _field)
67 static const struct nlattr_parser nla_p_state[] = {
68 	{ .type = PF_ST_ID, .off = _OUT(id), .cb = nlattr_get_uint32 },
69 	{ .type = PF_ST_CREATORID, .off = _OUT(creatorid), .cb = nlattr_get_uint32 },
70 	{ .type = PF_ST_IFNAME, .arg = (const void *)IFNAMSIZ, .off = _OUT(ifname), .cb = nlattr_get_chara },
71 	{ .type = PF_ST_AF, .off = _OUT(af), .cb = nlattr_get_uint8 },
72 	{ .type = PF_ST_PROTO, .off = _OUT(proto), .cb = nlattr_get_uint16 },
73 	{ .type = PF_ST_FILTER_ADDR, .off = _OUT(addr), .cb = nlattr_get_in6_addr },
74 	{ .type = PF_ST_FILTER_MASK, .off = _OUT(mask), .cb = nlattr_get_in6_addr },
75 };
76 static const struct nlfield_parser nlf_p_generic[] = {
77 	{ .off_in = _IN(version), .off_out = _OUT(version), .cb = nlf_get_u8 },
78 };
79 #undef _IN
80 #undef _OUT
81 NL_DECLARE_PARSER(state_parser, struct genlmsghdr, nlf_p_generic, nla_p_state);
82 
83 static void
84 dump_addr(struct nl_writer *nw, int attr, const struct pf_addr *addr, int af)
85 {
86 	switch (af) {
87 	case AF_INET:
88 		nlattr_add(nw, attr, 4, &addr->v4);
89 		break;
90 	case AF_INET6:
91 		nlattr_add(nw, attr, 16, &addr->v6);
92 		break;
93 	};
94 }
95 
96 static bool
97 dump_state_peer(struct nl_writer *nw, int attr, const struct pf_state_peer *peer)
98 {
99 	int off = nlattr_add_nested(nw, attr);
100 	if (off == 0)
101 		return (false);
102 
103 	nlattr_add_u32(nw, PF_STP_SEQLO, peer->seqlo);
104 	nlattr_add_u32(nw, PF_STP_SEQHI, peer->seqhi);
105 	nlattr_add_u32(nw, PF_STP_SEQDIFF, peer->seqdiff);
106 	nlattr_add_u16(nw, PF_STP_MAX_WIN, peer->max_win);
107 	nlattr_add_u16(nw, PF_STP_MSS, peer->mss);
108 	nlattr_add_u8(nw, PF_STP_STATE, peer->state);
109 	nlattr_add_u8(nw, PF_STP_WSCALE, peer->wscale);
110 
111 	if (peer->scrub != NULL) {
112 		struct pf_state_scrub *sc = peer->scrub;
113 		uint16_t pfss_flags = sc->pfss_flags & PFSS_TIMESTAMP;
114 
115 		nlattr_add_u16(nw, PF_STP_PFSS_FLAGS, pfss_flags);
116 		nlattr_add_u32(nw, PF_STP_PFSS_TS_MOD, sc->pfss_ts_mod);
117 		nlattr_add_u8(nw, PF_STP_PFSS_TTL, sc->pfss_ttl);
118 		nlattr_add_u8(nw, PF_STP_SCRUB_FLAG, PFSYNC_SCRUB_FLAG_VALID);
119 	}
120 	nlattr_set_len(nw, off);
121 
122 	return (true);
123 }
124 
125 static bool
126 dump_state_key(struct nl_writer *nw, int attr, const struct pf_state_key *key)
127 {
128 	int off = nlattr_add_nested(nw, attr);
129 	if (off == 0)
130 		return (false);
131 
132 	dump_addr(nw, PF_STK_ADDR0, &key->addr[0], key->af);
133 	dump_addr(nw, PF_STK_ADDR1, &key->addr[1], key->af);
134 	nlattr_add_u16(nw, PF_STK_PORT0, key->port[0]);
135 	nlattr_add_u16(nw, PF_STK_PORT1, key->port[1]);
136 	nlattr_add_u8(nw, PF_STK_AF, key->af);
137 	nlattr_add_u16(nw, PF_STK_PROTO, key->proto);
138 
139 	nlattr_set_len(nw, off);
140 
141 	return (true);
142 }
143 
144 static int
145 dump_state(struct nlpcb *nlp, const struct nlmsghdr *hdr, struct pf_kstate *s,
146     struct nl_pstate *npt)
147 {
148 	struct nl_writer *nw = npt->nw;
149 	int error = 0;
150 	int af;
151 	struct pf_state_key *key;
152 
153 	PF_STATE_LOCK_ASSERT(s);
154 
155 	if (!nlmsg_reply(nw, hdr, sizeof(struct genlmsghdr)))
156 		goto enomem;
157 
158 	struct genlmsghdr *ghdr_new = nlmsg_reserve_object(nw, struct genlmsghdr);
159 	ghdr_new->cmd = PFNL_CMD_GETSTATES;
160 	ghdr_new->version = 0;
161 	ghdr_new->reserved = 0;
162 
163 	nlattr_add_u64(nw, PF_ST_VERSION, PF_STATE_VERSION);
164 
165 	key = s->key[PF_SK_WIRE];
166 	if (!dump_state_key(nw, PF_ST_KEY_WIRE, key))
167 		goto enomem;
168 	key = s->key[PF_SK_STACK];
169 	if (!dump_state_key(nw, PF_ST_KEY_STACK, key))
170 		goto enomem;
171 
172 	af = s->key[PF_SK_WIRE]->af;
173 	nlattr_add_u8(nw, PF_ST_PROTO, s->key[PF_SK_WIRE]->proto);
174 	nlattr_add_u8(nw, PF_ST_AF, af);
175 
176 	nlattr_add_string(nw, PF_ST_IFNAME, s->kif->pfik_name);
177 	nlattr_add_string(nw, PF_ST_ORIG_IFNAME, s->orig_kif->pfik_name);
178 	dump_addr(nw, PF_ST_RT_ADDR, &s->act.rt_addr, af);
179 	nlattr_add_u32(nw, PF_ST_CREATION, time_uptime - (s->creation / 1000));
180 	uint32_t expire = pf_state_expires(s);
181 	if (expire > time_uptime)
182 		expire = expire - time_uptime;
183 	nlattr_add_u32(nw, PF_ST_EXPIRE, expire);
184 	nlattr_add_u8(nw, PF_ST_DIRECTION, s->direction);
185 	nlattr_add_u8(nw, PF_ST_LOG, s->act.log);
186 	nlattr_add_u8(nw, PF_ST_TIMEOUT, s->timeout);
187 	nlattr_add_u16(nw, PF_ST_STATE_FLAGS, s->state_flags);
188 	uint8_t sync_flags = 0;
189 	if (s->src_node)
190 		sync_flags |= PFSYNC_FLAG_SRCNODE;
191 	if (s->nat_src_node)
192 		sync_flags |= PFSYNC_FLAG_NATSRCNODE;
193 	nlattr_add_u8(nw, PF_ST_SYNC_FLAGS, sync_flags);
194 	nlattr_add_u64(nw, PF_ST_ID, s->id);
195 	nlattr_add_u32(nw, PF_ST_CREATORID, htonl(s->creatorid));
196 
197 	nlattr_add_u32(nw, PF_ST_RULE, s->rule ? s->rule->nr : -1);
198 	nlattr_add_u32(nw, PF_ST_ANCHOR, s->anchor ? s->anchor->nr : -1);
199 	nlattr_add_u32(nw, PF_ST_NAT_RULE, s->nat_rule ? s->nat_rule->nr : -1);
200 
201 	nlattr_add_u64(nw, PF_ST_PACKETS0, s->packets[0]);
202 	nlattr_add_u64(nw, PF_ST_PACKETS1, s->packets[1]);
203 	nlattr_add_u64(nw, PF_ST_BYTES0, s->bytes[0]);
204 	nlattr_add_u64(nw, PF_ST_BYTES1, s->bytes[1]);
205 	nlattr_add_u32(nw, PF_ST_RTABLEID, s->act.rtableid);
206 	nlattr_add_u8(nw, PF_ST_MIN_TTL, s->act.min_ttl);
207 	nlattr_add_u16(nw, PF_ST_MAX_MSS, s->act.max_mss);
208 	nlattr_add_u16(nw, PF_ST_DNPIPE, s->act.dnpipe);
209 	nlattr_add_u16(nw, PF_ST_DNRPIPE, s->act.dnrpipe);
210 	nlattr_add_u8(nw, PF_ST_RT, s->act.rt);
211 	if (s->act.rt_kif != NULL)
212 		nlattr_add_string(nw, PF_ST_RT_IFNAME, s->act.rt_kif->pfik_name);
213 
214 	if (!dump_state_peer(nw, PF_ST_PEER_SRC, &s->src))
215 		goto enomem;
216 	if (!dump_state_peer(nw, PF_ST_PEER_DST, &s->dst))
217 		goto enomem;
218 
219 	if (nlmsg_end(nw))
220 		return (0);
221 
222 enomem:
223 	error = ENOMEM;
224 	nlmsg_abort(nw);
225 	return (error);
226 }
227 
228 static int
229 handle_dumpstates(struct nlpcb *nlp, struct nl_parsed_state *attrs,
230     struct nlmsghdr *hdr, struct nl_pstate *npt)
231 {
232 	int error = 0;
233 
234 	hdr->nlmsg_flags |= NLM_F_MULTI;
235 
236 	for (int i = 0; i <= V_pf_hashmask; i++) {
237 		struct pf_idhash *ih = &V_pf_idhash[i];
238 		struct pf_kstate *s;
239 
240 		if (LIST_EMPTY(&ih->states))
241 			continue;
242 
243 		PF_HASHROW_LOCK(ih);
244 		LIST_FOREACH(s, &ih->states, entry) {
245 			sa_family_t af = s->key[PF_SK_WIRE]->af;
246 
247 			if (s->timeout == PFTM_UNLINKED)
248 				continue;
249 
250 			/* Filter */
251 			if (attrs->creatorid != 0 && s->creatorid != attrs->creatorid)
252 				continue;
253 			if (attrs->ifname[0] != 0 &&
254 			    strncmp(attrs->ifname, s->kif->pfik_name, IFNAMSIZ) != 0)
255 				continue;
256 			if (attrs->proto != 0 && s->key[PF_SK_WIRE]->proto != attrs->proto)
257 				continue;
258 			if (attrs->af != 0 && af != attrs->af)
259 				continue;
260 			if (pf_match_addr(1, &s->key[PF_SK_WIRE]->addr[0],
261 			    &attrs->mask, &attrs->addr, af) &&
262 			    pf_match_addr(1, &s->key[PF_SK_WIRE]->addr[1],
263 			    &attrs->mask, &attrs->addr, af) &&
264 			    pf_match_addr(1, &s->key[PF_SK_STACK]->addr[0],
265 			    &attrs->mask, &attrs->addr, af) &&
266 			    pf_match_addr(1, &s->key[PF_SK_STACK]->addr[1],
267 			    &attrs->mask, &attrs->addr, af))
268 				continue;
269 
270 			error = dump_state(nlp, hdr, s, npt);
271 			if (error != 0)
272 				break;
273 		}
274 		PF_HASHROW_UNLOCK(ih);
275 	}
276 
277 	if (!nlmsg_end_dump(npt->nw, error, hdr)) {
278 		NL_LOG(LOG_DEBUG, "Unable to finalize the dump");
279 		return (ENOMEM);
280 	}
281 
282 	return (error);
283 }
284 
285 static int
286 handle_getstate(struct nlpcb *nlp, struct nl_parsed_state *attrs,
287     struct nlmsghdr *hdr, struct nl_pstate *npt)
288 {
289 	struct pf_kstate *s;
290 	int ret;
291 
292 	s = pf_find_state_byid(attrs->id, attrs->creatorid);
293 	if (s == NULL)
294 		return (ENOENT);
295 	ret = dump_state(nlp, hdr, s, npt);
296 	PF_STATE_UNLOCK(s);
297 
298 	return (ret);
299 }
300 
301 static int
302 dump_creatorid(struct nlpcb *nlp, const struct nlmsghdr *hdr, uint32_t creator,
303     struct nl_pstate *npt)
304 {
305 	struct nl_writer *nw = npt->nw;
306 
307 	if (!nlmsg_reply(nw, hdr, sizeof(struct genlmsghdr)))
308 		goto enomem;
309 
310 	struct genlmsghdr *ghdr_new = nlmsg_reserve_object(nw, struct genlmsghdr);
311 	ghdr_new->cmd = PFNL_CMD_GETCREATORS;
312 	ghdr_new->version = 0;
313 	ghdr_new->reserved = 0;
314 
315 	nlattr_add_u32(nw, PF_ST_CREATORID, htonl(creator));
316 
317 	if (nlmsg_end(nw))
318 		return (0);
319 
320 enomem:
321 	nlmsg_abort(nw);
322 	return (ENOMEM);
323 }
324 
325 static int
326 pf_handle_getstates(struct nlmsghdr *hdr, struct nl_pstate *npt)
327 {
328 	int error;
329 
330 	struct nl_parsed_state attrs = {};
331 	error = nl_parse_nlmsg(hdr, &state_parser, npt, &attrs);
332 	if (error != 0)
333 		return (error);
334 
335 	if (attrs.id != 0)
336 		error = handle_getstate(npt->nlp, &attrs, hdr, npt);
337 	else
338 		error = handle_dumpstates(npt->nlp, &attrs, hdr, npt);
339 
340 	return (error);
341 }
342 
343 static int
344 pf_handle_getcreators(struct nlmsghdr *hdr, struct nl_pstate *npt)
345 {
346 	uint32_t creators[16];
347 	int error = 0;
348 
349 	bzero(creators, sizeof(creators));
350 
351 	for (int i = 0; i < V_pf_hashmask; i++) {
352 		struct pf_idhash *ih = &V_pf_idhash[i];
353 		struct pf_kstate *s;
354 
355 		if (LIST_EMPTY(&ih->states))
356 			continue;
357 
358 		PF_HASHROW_LOCK(ih);
359 		LIST_FOREACH(s, &ih->states, entry) {
360 			int j;
361 			if (s->timeout == PFTM_UNLINKED)
362 				continue;
363 
364 			for (j = 0; j < nitems(creators); j++) {
365 				if (creators[j] == s->creatorid)
366 					break;
367 				if (creators[j] == 0) {
368 					creators[j] = s->creatorid;
369 					break;
370 				}
371 			}
372 			if (j == nitems(creators))
373 				printf("Warning: too many creators!\n");
374 		}
375 		PF_HASHROW_UNLOCK(ih);
376 	}
377 
378 	hdr->nlmsg_flags |= NLM_F_MULTI;
379 	for (int i = 0; i < nitems(creators); i++) {
380 		if (creators[i] == 0)
381 			break;
382 		error = dump_creatorid(npt->nlp, hdr, creators[i], npt);
383 	}
384 
385 	if (!nlmsg_end_dump(npt->nw, error, hdr)) {
386 		NL_LOG(LOG_DEBUG, "Unable to finalize the dump");
387 		return (ENOMEM);
388 	}
389 
390 	return (error);
391 }
392 
393 static int
394 pf_handle_start(struct nlmsghdr *hdr __unused, struct nl_pstate *npt __unused)
395 {
396 	return (pf_start());
397 }
398 
399 static int
400 pf_handle_stop(struct nlmsghdr *hdr __unused, struct nl_pstate *npt __unused)
401 {
402 	return (pf_stop());
403 }
404 
405 #define _OUT(_field)	offsetof(struct pf_addr_wrap, _field)
406 static const struct nlattr_parser nla_p_addr_wrap[] = {
407 	{ .type = PF_AT_ADDR, .off = _OUT(v.a.addr), .cb = nlattr_get_in6_addr },
408 	{ .type = PF_AT_MASK, .off = _OUT(v.a.mask), .cb = nlattr_get_in6_addr },
409 	{ .type = PF_AT_IFNAME, .off = _OUT(v.ifname), .arg = (void *)IFNAMSIZ,.cb = nlattr_get_chara },
410 	{ .type = PF_AT_TABLENAME, .off = _OUT(v.tblname), .arg = (void *)PF_TABLE_NAME_SIZE, .cb = nlattr_get_chara },
411 	{ .type = PF_AT_TYPE, .off = _OUT(type), .cb = nlattr_get_uint8 },
412 	{ .type = PF_AT_IFLAGS, .off = _OUT(iflags), .cb = nlattr_get_uint8 },
413 };
414 NL_DECLARE_ATTR_PARSER(addr_wrap_parser, nla_p_addr_wrap);
415 #undef _OUT
416 
417 static bool
418 nlattr_add_addr_wrap(struct nl_writer *nw, int attrtype, struct pf_addr_wrap *a)
419 {
420 	int off = nlattr_add_nested(nw, attrtype);
421 
422 	nlattr_add_in6_addr(nw, PF_AT_ADDR, &a->v.a.addr.v6);
423 	nlattr_add_in6_addr(nw, PF_AT_MASK, &a->v.a.mask.v6);
424 	nlattr_add_u8(nw, PF_AT_TYPE, a->type);
425 	nlattr_add_u8(nw, PF_AT_IFLAGS, a->iflags);
426 
427 	if (a->type == PF_ADDR_DYNIFTL) {
428 		nlattr_add_string(nw, PF_AT_IFNAME, a->v.ifname);
429 		nlattr_add_u32(nw, PF_AT_DYNCNT, a->p.dyncnt);
430 	} else if (a->type == PF_ADDR_TABLE) {
431 		nlattr_add_string(nw, PF_AT_TABLENAME, a->v.tblname);
432 		nlattr_add_u32(nw, PF_AT_TBLCNT, a->p.tblcnt);
433 	}
434 
435 	nlattr_set_len(nw, off);
436 
437 	return (true);
438 }
439 
440 #define _OUT(_field)	offsetof(struct pf_rule_addr, _field)
441 static const struct nlattr_parser nla_p_ruleaddr[] = {
442 	{ .type = PF_RAT_ADDR, .off = _OUT(addr), .arg = &addr_wrap_parser, .cb = nlattr_get_nested },
443 	{ .type = PF_RAT_SRC_PORT, .off = _OUT(port[0]), .cb = nlattr_get_uint16 },
444 	{ .type = PF_RAT_DST_PORT, .off = _OUT(port[1]), .cb = nlattr_get_uint16 },
445 	{ .type = PF_RAT_NEG, .off = _OUT(neg), .cb = nlattr_get_uint8 },
446 	{ .type = PF_RAT_OP, .off = _OUT(port_op), .cb = nlattr_get_uint8 },
447 };
448 NL_DECLARE_ATTR_PARSER(rule_addr_parser, nla_p_ruleaddr);
449 #undef _OUT
450 
451 static bool
452 nlattr_add_rule_addr(struct nl_writer *nw, int attrtype, struct pf_rule_addr *r)
453 {
454 	struct pf_addr_wrap aw = {0};
455 	int off = nlattr_add_nested(nw, attrtype);
456 
457 	bcopy(&(r->addr), &aw, sizeof(struct pf_addr_wrap));
458 	pf_addr_copyout(&aw);
459 
460 	nlattr_add_addr_wrap(nw, PF_RAT_ADDR, &aw);
461 	nlattr_add_u16(nw, PF_RAT_SRC_PORT, r->port[0]);
462 	nlattr_add_u16(nw, PF_RAT_DST_PORT, r->port[1]);
463 	nlattr_add_u8(nw, PF_RAT_NEG, r->neg);
464 	nlattr_add_u8(nw, PF_RAT_OP, r->port_op);
465 
466 	nlattr_set_len(nw, off);
467 
468 	return (true);
469 }
470 
471 #define _OUT(_field)	offsetof(struct pf_mape_portset, _field)
472 static const struct nlattr_parser nla_p_mape_portset[] = {
473 	{ .type = PF_MET_OFFSET, .off = _OUT(offset), .cb = nlattr_get_uint8 },
474 	{ .type = PF_MET_PSID_LEN, .off = _OUT(psidlen), .cb = nlattr_get_uint8 },
475 	{. type = PF_MET_PSID, .off = _OUT(psid), .cb = nlattr_get_uint16 },
476 };
477 NL_DECLARE_ATTR_PARSER(mape_portset_parser, nla_p_mape_portset);
478 #undef _OUT
479 
480 static bool
481 nlattr_add_mape_portset(struct nl_writer *nw, int attrtype, const struct pf_mape_portset *m)
482 {
483 	int off = nlattr_add_nested(nw, attrtype);
484 
485 	nlattr_add_u8(nw, PF_MET_OFFSET, m->offset);
486 	nlattr_add_u8(nw, PF_MET_PSID_LEN, m->psidlen);
487 	nlattr_add_u16(nw, PF_MET_PSID, m->psid);
488 
489 	nlattr_set_len(nw, off);
490 
491 	return (true);
492 }
493 
494 struct nl_parsed_labels
495 {
496 	char		labels[PF_RULE_MAX_LABEL_COUNT][PF_RULE_LABEL_SIZE];
497 	uint32_t	i;
498 };
499 
500 static int
501 nlattr_get_pf_rule_labels(struct nlattr *nla, struct nl_pstate *npt,
502     const void *arg, void *target)
503 {
504 	struct nl_parsed_labels *l = (struct nl_parsed_labels *)target;
505 	int ret;
506 
507 	if (l->i >= PF_RULE_MAX_LABEL_COUNT)
508 		return (E2BIG);
509 
510 	ret = nlattr_get_chara(nla, npt, (void *)PF_RULE_LABEL_SIZE,
511 	    l->labels[l->i]);
512 	if (ret == 0)
513 		l->i++;
514 
515 	return (ret);
516 }
517 
518 #define _OUT(_field)	offsetof(struct nl_parsed_labels, _field)
519 static const struct nlattr_parser nla_p_labels[] = {
520 	{ .type = PF_LT_LABEL, .off = 0, .cb = nlattr_get_pf_rule_labels },
521 };
522 NL_DECLARE_ATTR_PARSER(rule_labels_parser, nla_p_labels);
523 #undef _OUT
524 
525 static int
526 nlattr_get_nested_pf_rule_labels(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
527 {
528 	struct nl_parsed_labels parsed_labels = { };
529 	int error;
530 
531 	/* Assumes target points to the beginning of the structure */
532 	error = nl_parse_header(NLA_DATA(nla), NLA_DATA_LEN(nla), &rule_labels_parser, npt, &parsed_labels);
533 	if (error != 0)
534 		return (error);
535 
536 	memcpy(target, parsed_labels.labels, sizeof(parsed_labels.labels));
537 
538 	return (0);
539 }
540 
541 static bool
542 nlattr_add_labels(struct nl_writer *nw, int attrtype, const struct pf_krule *r)
543 {
544 	int off = nlattr_add_nested(nw, attrtype);
545 	int i = 0;
546 
547 	while (r->label[i][0] != 0
548 	    && i < PF_RULE_MAX_LABEL_COUNT) {
549 		nlattr_add_string(nw, PF_LT_LABEL, r->label[i]);
550 		i++;
551 	}
552 
553 	nlattr_set_len(nw, off);
554 
555 	return (true);
556 }
557 
558 #define _OUT(_field)	offsetof(struct pf_kpool, _field)
559 static const struct nlattr_parser nla_p_pool[] = {
560 	{ .type = PF_PT_KEY, .off = _OUT(key), .arg = (void *)sizeof(struct pf_poolhashkey), .cb = nlattr_get_bytes },
561 	{ .type = PF_PT_COUNTER, .off = _OUT(counter), .cb = nlattr_get_in6_addr },
562 	{ .type = PF_PT_TBLIDX, .off = _OUT(tblidx), .cb = nlattr_get_uint32 },
563 	{ .type = PF_PT_PROXY_SRC_PORT, .off = _OUT(proxy_port[0]), .cb = nlattr_get_uint16 },
564 	{ .type = PF_PT_PROXY_DST_PORT, .off = _OUT(proxy_port[1]), .cb = nlattr_get_uint16 },
565 	{ .type = PF_PT_OPTS, .off = _OUT(opts), .cb = nlattr_get_uint8 },
566 	{ .type = PF_PT_MAPE, .off = _OUT(mape), .arg = &mape_portset_parser, .cb = nlattr_get_nested },
567 };
568 NL_DECLARE_ATTR_PARSER(pool_parser, nla_p_pool);
569 #undef _OUT
570 
571 static bool
572 nlattr_add_pool(struct nl_writer *nw, int attrtype, const struct pf_kpool *pool)
573 {
574 	int off = nlattr_add_nested(nw, attrtype);
575 
576 	nlattr_add(nw, PF_PT_KEY, sizeof(struct pf_poolhashkey), &pool->key);
577 	nlattr_add_in6_addr(nw, PF_PT_COUNTER, (const struct in6_addr *)&pool->counter);
578 	nlattr_add_u32(nw, PF_PT_TBLIDX, pool->tblidx);
579 	nlattr_add_u16(nw, PF_PT_PROXY_SRC_PORT, pool->proxy_port[0]);
580 	nlattr_add_u16(nw, PF_PT_PROXY_DST_PORT, pool->proxy_port[1]);
581 	nlattr_add_u8(nw, PF_PT_OPTS, pool->opts);
582 	nlattr_add_mape_portset(nw, PF_PT_MAPE, &pool->mape);
583 
584 	nlattr_set_len(nw, off);
585 
586 	return (true);
587 }
588 
589 #define _OUT(_field)	offsetof(struct pf_rule_uid, _field)
590 static const struct nlattr_parser nla_p_rule_uid[] = {
591 	{ .type = PF_RUT_UID_LOW, .off = _OUT(uid[0]), .cb = nlattr_get_uint32 },
592 	{ .type = PF_RUT_UID_HIGH, .off = _OUT(uid[1]), .cb = nlattr_get_uint32 },
593 	{ .type = PF_RUT_OP, .off = _OUT(op), .cb = nlattr_get_uint8 },
594 };
595 NL_DECLARE_ATTR_PARSER(rule_uid_parser, nla_p_rule_uid);
596 #undef _OUT
597 
598 static bool
599 nlattr_add_rule_uid(struct nl_writer *nw, int attrtype, const struct pf_rule_uid *u)
600 {
601 	int off = nlattr_add_nested(nw, attrtype);
602 
603 	nlattr_add_u32(nw, PF_RUT_UID_LOW, u->uid[0]);
604 	nlattr_add_u32(nw, PF_RUT_UID_HIGH, u->uid[1]);
605 	nlattr_add_u8(nw, PF_RUT_OP, u->op);
606 
607 	nlattr_set_len(nw, off);
608 
609 	return (true);
610 }
611 
612 struct nl_parsed_timeouts
613 {
614 	uint32_t	timeouts[PFTM_MAX];
615 	uint32_t	i;
616 };
617 
618 static int
619 nlattr_get_pf_timeout(struct nlattr *nla, struct nl_pstate *npt,
620     const void *arg, void *target)
621 {
622 	struct nl_parsed_timeouts *t = (struct nl_parsed_timeouts *)target;
623 	int ret;
624 
625 	if (t->i >= PFTM_MAX)
626 		return (E2BIG);
627 
628 	ret = nlattr_get_uint32(nla, npt, NULL, &t->timeouts[t->i]);
629 	if (ret == 0)
630 		t->i++;
631 
632 	return (ret);
633 }
634 
635 #define _OUT(_field)	offsetof(struct nl_parsed_timeout, _field)
636 static const struct nlattr_parser nla_p_timeouts[] = {
637 	{ .type = PF_TT_TIMEOUT, .off = 0, .cb = nlattr_get_pf_timeout },
638 };
639 NL_DECLARE_ATTR_PARSER(timeout_parser, nla_p_timeouts);
640 #undef _OUT
641 
642 static int
643 nlattr_get_nested_timeouts(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
644 {
645 	struct nl_parsed_timeouts parsed_timeouts = { };
646 	int error;
647 
648 	/* Assumes target points to the beginning of the structure */
649 	error = nl_parse_header(NLA_DATA(nla), NLA_DATA_LEN(nla), &timeout_parser, npt, &parsed_timeouts);
650 	if (error != 0)
651 		return (error);
652 
653 	memcpy(target, parsed_timeouts.timeouts, sizeof(parsed_timeouts.timeouts));
654 
655 	return (0);
656 }
657 
658 static bool
659 nlattr_add_timeout(struct nl_writer *nw, int attrtype, uint32_t *timeout)
660 {
661 	int off = nlattr_add_nested(nw, attrtype);
662 
663 	for (int i = 0; i < PFTM_MAX; i++)
664 		nlattr_add_u32(nw, PF_RT_TIMEOUT, timeout[i]);
665 
666 	nlattr_set_len(nw, off);
667 
668 	return (true);
669 }
670 
671 #define _OUT(_field)	offsetof(struct pf_krule, _field)
672 static const struct nlattr_parser nla_p_rule[] = {
673 	{ .type = PF_RT_SRC, .off = _OUT(src), .arg = &rule_addr_parser,.cb = nlattr_get_nested },
674 	{ .type = PF_RT_DST, .off = _OUT(dst), .arg = &rule_addr_parser,.cb = nlattr_get_nested },
675 	{ .type = PF_RT_RIDENTIFIER, .off = _OUT(ridentifier), .cb = nlattr_get_uint32 },
676 	{ .type = PF_RT_LABELS, .off = _OUT(label), .arg = &rule_labels_parser,.cb = nlattr_get_nested_pf_rule_labels },
677 	{ .type = PF_RT_IFNAME, .off = _OUT(ifname), .arg = (void *)IFNAMSIZ, .cb = nlattr_get_chara },
678 	{ .type = PF_RT_QNAME, .off = _OUT(qname), .arg = (void *)PF_QNAME_SIZE, .cb = nlattr_get_chara },
679 	{ .type = PF_RT_PQNAME, .off = _OUT(pqname), .arg = (void *)PF_QNAME_SIZE, .cb = nlattr_get_chara },
680 	{ .type = PF_RT_TAGNAME, .off = _OUT(tagname), .arg = (void *)PF_TAG_NAME_SIZE, .cb = nlattr_get_chara },
681 	{ .type = PF_RT_MATCH_TAGNAME, .off = _OUT(match_tagname), .arg = (void *)PF_TAG_NAME_SIZE, .cb = nlattr_get_chara },
682 	{ .type = PF_RT_OVERLOAD_TBLNAME, .off = _OUT(overload_tblname), .arg = (void *)PF_TABLE_NAME_SIZE, .cb = nlattr_get_chara },
683 	{ .type = PF_RT_RPOOL_RDR, .off = _OUT(rdr), .arg = &pool_parser, .cb = nlattr_get_nested },
684 	{ .type = PF_RT_OS_FINGERPRINT, .off = _OUT(os_fingerprint), .cb = nlattr_get_uint32 },
685 	{ .type = PF_RT_RTABLEID, .off = _OUT(rtableid), .cb = nlattr_get_uint32 },
686 	{ .type = PF_RT_TIMEOUT, .off = _OUT(timeout), .arg = &timeout_parser, .cb = nlattr_get_nested_timeouts },
687 	{ .type = PF_RT_MAX_STATES, .off = _OUT(max_states), .cb = nlattr_get_uint32 },
688 	{ .type = PF_RT_MAX_SRC_NODES, .off = _OUT(max_src_nodes), .cb = nlattr_get_uint32 },
689 	{ .type = PF_RT_MAX_SRC_STATES, .off = _OUT(max_src_states), .cb = nlattr_get_uint32 },
690 	{ .type = PF_RT_MAX_SRC_CONN_RATE_LIMIT, .off = _OUT(max_src_conn_rate.limit), .cb = nlattr_get_uint32 },
691 	{ .type = PF_RT_MAX_SRC_CONN_RATE_SECS, .off = _OUT(max_src_conn_rate.seconds), .cb = nlattr_get_uint32 },
692 	{ .type = PF_RT_DNPIPE, .off = _OUT(dnpipe), .cb = nlattr_get_uint16 },
693 	{ .type = PF_RT_DNRPIPE, .off = _OUT(dnrpipe), .cb = nlattr_get_uint16 },
694 	{ .type = PF_RT_DNFLAGS, .off = _OUT(free_flags), .cb = nlattr_get_uint32 },
695 	{ .type = PF_RT_NR, .off = _OUT(nr), .cb = nlattr_get_uint32 },
696 	{ .type = PF_RT_PROB, .off = _OUT(prob), .cb = nlattr_get_uint32 },
697 	{ .type = PF_RT_CUID, .off = _OUT(cuid), .cb = nlattr_get_uint32 },
698 	{. type = PF_RT_CPID, .off = _OUT(cpid), .cb = nlattr_get_uint32 },
699 	{ .type = PF_RT_RETURN_ICMP, .off = _OUT(return_icmp), .cb = nlattr_get_uint16 },
700 	{ .type = PF_RT_RETURN_ICMP6, .off = _OUT(return_icmp6), .cb = nlattr_get_uint16 },
701 	{ .type = PF_RT_MAX_MSS, .off = _OUT(max_mss), .cb = nlattr_get_uint16 },
702 	{ .type = PF_RT_SCRUB_FLAGS, .off = _OUT(scrub_flags), .cb = nlattr_get_uint16 },
703 	{ .type = PF_RT_UID, .off = _OUT(uid), .arg = &rule_uid_parser, .cb = nlattr_get_nested },
704 	{ .type = PF_RT_GID, .off = _OUT(gid), .arg = &rule_uid_parser, .cb = nlattr_get_nested },
705 	{ .type = PF_RT_RULE_FLAG, .off = _OUT(rule_flag), .cb = nlattr_get_uint32 },
706 	{ .type = PF_RT_ACTION, .off = _OUT(action), .cb = nlattr_get_uint8 },
707 	{ .type = PF_RT_DIRECTION, .off = _OUT(direction), .cb = nlattr_get_uint8 },
708 	{ .type = PF_RT_LOG, .off = _OUT(log), .cb = nlattr_get_uint8 },
709 	{ .type = PF_RT_LOGIF, .off = _OUT(logif), .cb = nlattr_get_uint8 },
710 	{ .type = PF_RT_QUICK, .off = _OUT(quick), .cb = nlattr_get_uint8 },
711 	{ .type = PF_RT_IF_NOT, .off = _OUT(ifnot), .cb = nlattr_get_uint8 },
712 	{ .type = PF_RT_MATCH_TAG_NOT, .off = _OUT(match_tag_not), .cb = nlattr_get_uint8 },
713 	{ .type = PF_RT_NATPASS, .off = _OUT(natpass), .cb = nlattr_get_uint8 },
714 	{ .type = PF_RT_KEEP_STATE, .off = _OUT(keep_state), .cb = nlattr_get_uint8 },
715 	{ .type = PF_RT_AF, .off = _OUT(af), .cb = nlattr_get_uint8 },
716 	{ .type = PF_RT_PROTO, .off = _OUT(proto), .cb = nlattr_get_uint8 },
717 	{ .type = PF_RT_TYPE, .off = _OUT(type), .cb = nlattr_get_uint8 },
718 	{ .type = PF_RT_CODE, .off = _OUT(code), .cb = nlattr_get_uint8 },
719 	{ .type = PF_RT_FLAGS, .off = _OUT(flags), .cb = nlattr_get_uint8 },
720 	{ .type = PF_RT_FLAGSET, .off = _OUT(flagset), .cb = nlattr_get_uint8 },
721 	{ .type = PF_RT_MIN_TTL, .off = _OUT(min_ttl), .cb = nlattr_get_uint8 },
722 	{ .type = PF_RT_ALLOW_OPTS, .off = _OUT(allow_opts), .cb = nlattr_get_uint8 },
723 	{ .type = PF_RT_RT, .off = _OUT(rt), .cb = nlattr_get_uint8 },
724 	{ .type = PF_RT_RETURN_TTL, .off = _OUT(return_ttl), .cb = nlattr_get_uint8 },
725 	{ .type = PF_RT_TOS, .off = _OUT(tos), .cb = nlattr_get_uint8 },
726 	{ .type = PF_RT_SET_TOS, .off = _OUT(set_tos), .cb = nlattr_get_uint8 },
727 	{ .type = PF_RT_ANCHOR_RELATIVE, .off = _OUT(anchor_relative), .cb = nlattr_get_uint8 },
728 	{ .type = PF_RT_ANCHOR_WILDCARD, .off = _OUT(anchor_wildcard), .cb = nlattr_get_uint8 },
729 	{ .type = PF_RT_FLUSH, .off = _OUT(flush), .cb = nlattr_get_uint8 },
730 	{ .type = PF_RT_PRIO, .off = _OUT(prio), .cb = nlattr_get_uint8 },
731 	{ .type = PF_RT_SET_PRIO, .off = _OUT(set_prio[0]), .cb = nlattr_get_uint8 },
732 	{ .type = PF_RT_SET_PRIO_REPLY, .off = _OUT(set_prio[1]), .cb = nlattr_get_uint8 },
733 	{ .type = PF_RT_DIVERT_ADDRESS, .off = _OUT(divert.addr), .cb = nlattr_get_in6_addr },
734 	{ .type = PF_RT_DIVERT_PORT, .off = _OUT(divert.port), .cb = nlattr_get_uint16 },
735 	{ .type = PF_RT_RCV_IFNAME, .off = _OUT(rcv_ifname), .arg = (void *)IFNAMSIZ, .cb = nlattr_get_chara },
736 	{ .type = PF_RT_MAX_SRC_CONN, .off = _OUT(max_src_conn), .cb = nlattr_get_uint32 },
737 	{ .type = PF_RT_RPOOL_NAT, .off = _OUT(nat), .arg = &pool_parser, .cb = nlattr_get_nested },
738 	{ .type = PF_RT_NAF, .off = _OUT(naf), .cb = nlattr_get_uint8 },
739 };
740 NL_DECLARE_ATTR_PARSER(rule_parser, nla_p_rule);
741 #undef _OUT
742 struct nl_parsed_addrule {
743 	struct pf_krule	*rule;
744 	uint32_t	 ticket;
745 	uint32_t	 pool_ticket;
746 	char		*anchor;
747 	char		*anchor_call;
748 };
749 #define	_OUT(_field)	offsetof(struct nl_parsed_addrule, _field)
750 static const struct nlattr_parser nla_p_addrule[] = {
751 	{ .type = PF_ART_TICKET, .off = _OUT(ticket), .cb = nlattr_get_uint32 },
752 	{ .type = PF_ART_POOL_TICKET, .off = _OUT(pool_ticket), .cb = nlattr_get_uint32 },
753 	{ .type = PF_ART_ANCHOR, .off = _OUT(anchor), .cb = nlattr_get_string },
754 	{ .type = PF_ART_ANCHOR_CALL, .off = _OUT(anchor_call), .cb = nlattr_get_string },
755 	{ .type = PF_ART_RULE, .off = _OUT(rule), .arg = &rule_parser, .cb = nlattr_get_nested_ptr }
756 };
757 #undef _OUT
758 NL_DECLARE_PARSER(addrule_parser, struct genlmsghdr, nlf_p_empty, nla_p_addrule);
759 
760 static int
761 pf_handle_addrule(struct nlmsghdr *hdr, struct nl_pstate *npt)
762 {
763 	int error;
764 	struct nl_parsed_addrule attrs = {};
765 
766 	attrs.rule = pf_krule_alloc();
767 
768 	error = nl_parse_nlmsg(hdr, &addrule_parser, npt, &attrs);
769 	if (error != 0) {
770 		pf_free_rule(attrs.rule);
771 		return (error);
772 	}
773 
774 	error = pf_ioctl_addrule(attrs.rule, attrs.ticket, attrs.pool_ticket,
775 	    attrs.anchor, attrs.anchor_call, nlp_get_cred(npt->nlp)->cr_uid,
776 	    hdr->nlmsg_pid);
777 
778 	return (error);
779 }
780 
781 #define	_OUT(_field)	offsetof(struct pfioc_rule, _field)
782 static const struct nlattr_parser nla_p_getrules[] = {
783 	{ .type = PF_GR_ANCHOR, .off = _OUT(anchor), .arg = (void *)MAXPATHLEN, .cb = nlattr_get_chara },
784 	{ .type = PF_GR_ACTION, .off = _OUT(rule.action), .cb = nlattr_get_uint8 },
785 };
786 #undef _OUT
787 NL_DECLARE_PARSER(getrules_parser, struct genlmsghdr, nlf_p_empty, nla_p_getrules);
788 
789 static int
790 pf_handle_getrules(struct nlmsghdr *hdr, struct nl_pstate *npt)
791 {
792 	struct pfioc_rule attrs = {};
793 	int error;
794 	struct nl_writer *nw = npt->nw;
795 	struct genlmsghdr *ghdr_new;
796 
797 	error = nl_parse_nlmsg(hdr, &getrules_parser, npt, &attrs);
798 	if (error != 0)
799 		return (error);
800 
801 	if (!nlmsg_reply(nw, hdr, sizeof(struct genlmsghdr)))
802 		return (ENOMEM);
803 
804 	ghdr_new = nlmsg_reserve_object(nw, struct genlmsghdr);
805 	ghdr_new->cmd = PFNL_CMD_GETRULES;
806 	ghdr_new->version = 0;
807 	ghdr_new->reserved = 0;
808 
809 	error = pf_ioctl_getrules(&attrs);
810 	if (error != 0)
811 		goto out;
812 
813 	nlattr_add_u32(nw, PF_GR_NR, attrs.nr);
814 	nlattr_add_u32(nw, PF_GR_TICKET, attrs.ticket);
815 
816 	if (!nlmsg_end(nw)) {
817 		error = ENOMEM;
818 		goto out;
819 	}
820 
821 	return (0);
822 
823 out:
824 	nlmsg_abort(nw);
825 	return (error);
826 }
827 
828 struct nl_parsed_get_rule {
829 	char anchor[MAXPATHLEN];
830 	uint8_t action;
831 	uint32_t nr;
832 	uint32_t ticket;
833 	uint8_t clear;
834 };
835 #define	_OUT(_field)	offsetof(struct nl_parsed_get_rule, _field)
836 static const struct nlattr_parser nla_p_getrule[] = {
837 	{ .type = PF_GR_ANCHOR, .off = _OUT(anchor), .arg = (void *)MAXPATHLEN, .cb = nlattr_get_chara },
838 	{ .type = PF_GR_ACTION, .off = _OUT(action), .cb = nlattr_get_uint8 },
839 	{ .type = PF_GR_NR, .off = _OUT(nr), .cb = nlattr_get_uint32 },
840 	{ .type = PF_GR_TICKET, .off = _OUT(ticket), .cb = nlattr_get_uint32 },
841 	{ .type = PF_GR_CLEAR, .off = _OUT(clear), .cb = nlattr_get_uint8 },
842 };
843 #undef _OUT
844 NL_DECLARE_PARSER(getrule_parser, struct genlmsghdr, nlf_p_empty, nla_p_getrule);
845 
846 static int
847 pf_handle_getrule(struct nlmsghdr *hdr, struct nl_pstate *npt)
848 {
849 	char				 anchor_call[MAXPATHLEN];
850 	struct nl_parsed_get_rule	 attrs = {};
851 	struct nl_writer		*nw = npt->nw;
852 	struct genlmsghdr		*ghdr_new;
853 	struct pf_kruleset		*ruleset;
854 	struct pf_krule			*rule;
855 	int				 rs_num;
856 	int				 error;
857 
858 	error = nl_parse_nlmsg(hdr, &getrule_parser, npt, &attrs);
859 	if (error != 0)
860 		return (error);
861 
862 	if (!nlmsg_reply(nw, hdr, sizeof(struct genlmsghdr)))
863 		return (ENOMEM);
864 
865 	ghdr_new = nlmsg_reserve_object(nw, struct genlmsghdr);
866 	ghdr_new->cmd = PFNL_CMD_GETRULE;
867 	ghdr_new->version = 0;
868 	ghdr_new->reserved = 0;
869 
870 	PF_RULES_WLOCK();
871 	ruleset = pf_find_kruleset(attrs.anchor);
872 	if (ruleset == NULL) {
873 		PF_RULES_WUNLOCK();
874 		error = ENOENT;
875 		goto out;
876 	}
877 
878 	rs_num = pf_get_ruleset_number(attrs.action);
879 	if (rs_num >= PF_RULESET_MAX) {
880 		PF_RULES_WUNLOCK();
881 		error = EINVAL;
882 		goto out;
883 	}
884 
885 	if (attrs.ticket != ruleset->rules[rs_num].active.ticket) {
886 		PF_RULES_WUNLOCK();
887 		error = EBUSY;
888 		goto out;
889 	}
890 
891 	rule = TAILQ_FIRST(ruleset->rules[rs_num].active.ptr);
892 	while ((rule != NULL) && (rule->nr != attrs.nr))
893 		rule = TAILQ_NEXT(rule, entries);
894 	if (rule == NULL) {
895 		PF_RULES_WUNLOCK();
896 		error = EBUSY;
897 		goto out;
898 	}
899 
900 	nlattr_add_rule_addr(nw, PF_RT_SRC, &rule->src);
901 	nlattr_add_rule_addr(nw, PF_RT_DST, &rule->dst);
902 	nlattr_add_u32(nw, PF_RT_RIDENTIFIER, rule->ridentifier);
903 	nlattr_add_labels(nw, PF_RT_LABELS, rule);
904 	nlattr_add_string(nw, PF_RT_IFNAME, rule->ifname);
905 	nlattr_add_string(nw, PF_RT_QNAME, rule->qname);
906 	nlattr_add_string(nw, PF_RT_PQNAME, rule->pqname);
907 	nlattr_add_string(nw, PF_RT_TAGNAME, rule->tagname);
908 	nlattr_add_string(nw, PF_RT_MATCH_TAGNAME, rule->match_tagname);
909 	nlattr_add_string(nw, PF_RT_OVERLOAD_TBLNAME, rule->overload_tblname);
910 	nlattr_add_pool(nw, PF_RT_RPOOL_RDR, &rule->rdr);
911 	nlattr_add_pool(nw, PF_RT_RPOOL_NAT, &rule->nat);
912 	nlattr_add_u32(nw, PF_RT_OS_FINGERPRINT, rule->os_fingerprint);
913 	nlattr_add_u32(nw, PF_RT_RTABLEID, rule->rtableid);
914 	nlattr_add_timeout(nw, PF_RT_TIMEOUT, rule->timeout);
915 	nlattr_add_u32(nw, PF_RT_MAX_STATES, rule->max_states);
916 	nlattr_add_u32(nw, PF_RT_MAX_SRC_NODES, rule->max_src_nodes);
917 	nlattr_add_u32(nw, PF_RT_MAX_SRC_STATES, rule->max_src_states);
918 	nlattr_add_u32(nw, PF_RT_MAX_SRC_CONN, rule->max_src_conn);
919 	nlattr_add_u32(nw, PF_RT_MAX_SRC_CONN_RATE_LIMIT, rule->max_src_conn_rate.limit);
920 	nlattr_add_u32(nw, PF_RT_MAX_SRC_CONN_RATE_SECS, rule->max_src_conn_rate.seconds);
921 
922 	nlattr_add_u16(nw, PF_RT_DNPIPE, rule->dnpipe);
923 	nlattr_add_u16(nw, PF_RT_DNRPIPE, rule->dnrpipe);
924 	nlattr_add_u32(nw, PF_RT_DNFLAGS, rule->free_flags);
925 
926 	nlattr_add_u32(nw, PF_RT_NR, rule->nr);
927 	nlattr_add_u32(nw, PF_RT_PROB, rule->prob);
928 	nlattr_add_u32(nw, PF_RT_CUID, rule->cuid);
929 	nlattr_add_u32(nw, PF_RT_CPID, rule->cpid);
930 
931 	nlattr_add_u16(nw, PF_RT_RETURN_ICMP, rule->return_icmp);
932 	nlattr_add_u16(nw, PF_RT_RETURN_ICMP6, rule->return_icmp6);
933 	nlattr_add_u16(nw, PF_RT_RETURN_ICMP6, rule->return_icmp6);
934 	nlattr_add_u16(nw, PF_RT_MAX_MSS, rule->max_mss);
935 	nlattr_add_u16(nw, PF_RT_SCRUB_FLAGS, rule->scrub_flags);
936 
937 	nlattr_add_rule_uid(nw, PF_RT_UID, &rule->uid);
938 	nlattr_add_rule_uid(nw, PF_RT_GID, (const struct pf_rule_uid *)&rule->gid);
939 
940 	nlattr_add_string(nw, PF_RT_RCV_IFNAME, rule->rcv_ifname);
941 
942 	nlattr_add_u32(nw, PF_RT_RULE_FLAG, rule->rule_flag);
943 	nlattr_add_u8(nw, PF_RT_ACTION, rule->action);
944 	nlattr_add_u8(nw, PF_RT_DIRECTION, rule->direction);
945 	nlattr_add_u8(nw, PF_RT_LOG, rule->log);
946 	nlattr_add_u8(nw, PF_RT_LOGIF, rule->logif);
947 	nlattr_add_u8(nw, PF_RT_QUICK, rule->quick);
948 	nlattr_add_u8(nw, PF_RT_IF_NOT, rule->ifnot);
949 	nlattr_add_u8(nw, PF_RT_MATCH_TAG_NOT, rule->match_tag_not);
950 	nlattr_add_u8(nw, PF_RT_NATPASS, rule->natpass);
951 	nlattr_add_u8(nw, PF_RT_KEEP_STATE, rule->keep_state);
952 
953 	nlattr_add_u8(nw, PF_RT_AF, rule->af);
954 	nlattr_add_u8(nw, PF_RT_NAF, rule->naf);
955 	nlattr_add_u8(nw, PF_RT_PROTO, rule->proto);
956 	nlattr_add_u8(nw, PF_RT_TYPE, rule->type);
957 	nlattr_add_u8(nw, PF_RT_CODE, rule->code);
958 	nlattr_add_u8(nw, PF_RT_FLAGS, rule->flags);
959 	nlattr_add_u8(nw, PF_RT_FLAGSET, rule->flagset);
960 	nlattr_add_u8(nw, PF_RT_MIN_TTL, rule->min_ttl);
961 	nlattr_add_u8(nw, PF_RT_ALLOW_OPTS, rule->allow_opts);
962 	nlattr_add_u8(nw, PF_RT_RT, rule->rt);
963 	nlattr_add_u8(nw, PF_RT_RETURN_TTL, rule->return_ttl);
964 	nlattr_add_u8(nw, PF_RT_TOS, rule->tos);
965 	nlattr_add_u8(nw, PF_RT_SET_TOS, rule->set_tos);
966 	nlattr_add_u8(nw, PF_RT_ANCHOR_RELATIVE, rule->anchor_relative);
967 	nlattr_add_u8(nw, PF_RT_ANCHOR_WILDCARD, rule->anchor_wildcard);
968 	nlattr_add_u8(nw, PF_RT_FLUSH, rule->flush);
969 	nlattr_add_u8(nw, PF_RT_PRIO, rule->prio);
970 	nlattr_add_u8(nw, PF_RT_SET_PRIO, rule->set_prio[0]);
971 	nlattr_add_u8(nw, PF_RT_SET_PRIO_REPLY, rule->set_prio[1]);
972 
973 	nlattr_add_in6_addr(nw, PF_RT_DIVERT_ADDRESS, &rule->divert.addr.v6);
974 	nlattr_add_u16(nw, PF_RT_DIVERT_PORT, rule->divert.port);
975 
976 	nlattr_add_u64(nw, PF_RT_PACKETS_IN, pf_counter_u64_fetch(&rule->packets[0]));
977 	nlattr_add_u64(nw, PF_RT_PACKETS_OUT, pf_counter_u64_fetch(&rule->packets[1]));
978 	nlattr_add_u64(nw, PF_RT_BYTES_IN, pf_counter_u64_fetch(&rule->bytes[0]));
979 	nlattr_add_u64(nw, PF_RT_BYTES_OUT, pf_counter_u64_fetch(&rule->bytes[1]));
980 	nlattr_add_u64(nw, PF_RT_EVALUATIONS, pf_counter_u64_fetch(&rule->evaluations));
981 	nlattr_add_u64(nw, PF_RT_TIMESTAMP, pf_get_timestamp(rule));
982 	nlattr_add_u64(nw, PF_RT_STATES_CUR, counter_u64_fetch(rule->states_cur));
983 	nlattr_add_u64(nw, PF_RT_STATES_TOTAL, counter_u64_fetch(rule->states_tot));
984 	nlattr_add_u64(nw, PF_RT_SRC_NODES, counter_u64_fetch(rule->src_nodes));
985 
986 	error = pf_kanchor_copyout(ruleset, rule, anchor_call, sizeof(anchor_call));
987 	MPASS(error == 0);
988 
989 	nlattr_add_string(nw, PF_RT_ANCHOR_CALL, anchor_call);
990 
991 	if (attrs.clear)
992 		pf_krule_clear_counters(rule);
993 
994 	PF_RULES_WUNLOCK();
995 
996 	if (!nlmsg_end(nw)) {
997 		error = ENOMEM;
998 		goto out;
999 	}
1000 
1001 	return (0);
1002 out:
1003 	nlmsg_abort(nw);
1004 	return (error);
1005 }
1006 
1007 #define	_OUT(_field)	offsetof(struct pf_kstate_kill, _field)
1008 static const struct nlattr_parser nla_p_clear_states[] = {
1009 	{ .type = PF_CS_CMP_ID, .off = _OUT(psk_pfcmp.id), .cb = nlattr_get_uint64 },
1010 	{ .type = PF_CS_CMP_CREATORID, .off = _OUT(psk_pfcmp.creatorid), .cb = nlattr_get_uint32 },
1011 	{ .type = PF_CS_CMP_DIR, .off = _OUT(psk_pfcmp.direction), .cb = nlattr_get_uint8 },
1012 	{ .type = PF_CS_AF, .off = _OUT(psk_af), .cb = nlattr_get_uint8 },
1013 	{ .type = PF_CS_PROTO, .off = _OUT(psk_proto), .cb = nlattr_get_uint8 },
1014 	{ .type = PF_CS_SRC, .off = _OUT(psk_src), .arg = &rule_addr_parser, .cb = nlattr_get_nested },
1015 	{ .type = PF_CS_DST, .off = _OUT(psk_dst), .arg = &rule_addr_parser, .cb = nlattr_get_nested },
1016 	{ .type = PF_CS_RT_ADDR, .off = _OUT(psk_rt_addr), .arg = &rule_addr_parser, .cb = nlattr_get_nested },
1017 	{ .type = PF_CS_IFNAME, .off = _OUT(psk_ifname), .arg = (void *)IFNAMSIZ, .cb = nlattr_get_chara },
1018 	{ .type = PF_CS_LABEL, .off = _OUT(psk_label), .arg = (void *)PF_RULE_LABEL_SIZE, .cb = nlattr_get_chara },
1019 	{ .type = PF_CS_KILL_MATCH, .off = _OUT(psk_kill_match), .cb = nlattr_get_bool },
1020 	{ .type = PF_CS_NAT, .off = _OUT(psk_nat), .cb = nlattr_get_bool },
1021 };
1022 #undef _OUT
1023 NL_DECLARE_PARSER(clear_states_parser, struct genlmsghdr, nlf_p_empty, nla_p_clear_states);
1024 
1025 static int
1026 pf_handle_killclear_states(struct nlmsghdr *hdr, struct nl_pstate *npt, int cmd)
1027 {
1028 	struct pf_kstate_kill		 kill = {};
1029 	struct epoch_tracker		 et;
1030 	struct nl_writer		*nw = npt->nw;
1031 	struct genlmsghdr		*ghdr_new;
1032 	int				 error;
1033 	unsigned int			 killed = 0;
1034 
1035 	error = nl_parse_nlmsg(hdr, &clear_states_parser, npt, &kill);
1036 	if (error != 0)
1037 		return (error);
1038 
1039 	if (!nlmsg_reply(nw, hdr, sizeof(struct genlmsghdr)))
1040 		return (ENOMEM);
1041 
1042 	ghdr_new = nlmsg_reserve_object(nw, struct genlmsghdr);
1043 	ghdr_new->cmd = cmd;
1044 	ghdr_new->version = 0;
1045 	ghdr_new->reserved = 0;
1046 
1047 	NET_EPOCH_ENTER(et);
1048 	if (cmd == PFNL_CMD_KILLSTATES)
1049 		pf_killstates(&kill, &killed);
1050 	else
1051 		killed = pf_clear_states(&kill);
1052 	NET_EPOCH_EXIT(et);
1053 
1054 	nlattr_add_u32(nw, PF_CS_KILLED, killed);
1055 
1056 	if (! nlmsg_end(nw)) {
1057 		error = ENOMEM;
1058 		goto out;
1059 	}
1060 
1061 	return (0);
1062 
1063 out:
1064 	nlmsg_abort(nw);
1065 	return (error);
1066 }
1067 
1068 static int
1069 pf_handle_clear_states(struct nlmsghdr *hdr, struct nl_pstate *npt)
1070 {
1071 	return (pf_handle_killclear_states(hdr, npt, PFNL_CMD_CLRSTATES));
1072 }
1073 
1074 static int
1075 pf_handle_kill_states(struct nlmsghdr *hdr, struct nl_pstate *npt)
1076 {
1077 	return (pf_handle_killclear_states(hdr, npt, PFNL_CMD_KILLSTATES));
1078 }
1079 
1080 struct nl_parsed_set_statusif {
1081 	char ifname[IFNAMSIZ];
1082 };
1083 #define	_OUT(_field)	offsetof(struct nl_parsed_set_statusif, _field)
1084 static const struct nlattr_parser nla_p_set_statusif[] = {
1085 	{ .type = PF_SS_IFNAME, .off = _OUT(ifname), .arg = (const void *)IFNAMSIZ, .cb = nlattr_get_chara },
1086 };
1087 #undef _OUT
1088 NL_DECLARE_PARSER(set_statusif_parser, struct genlmsghdr, nlf_p_empty, nla_p_set_statusif);
1089 
1090 static int
1091 pf_handle_set_statusif(struct nlmsghdr *hdr, struct nl_pstate *npt)
1092 {
1093 	int error;
1094 	struct nl_parsed_set_statusif attrs = {};
1095 
1096 	error = nl_parse_nlmsg(hdr, &set_statusif_parser, npt, &attrs);
1097 	if (error != 0)
1098 		return (error);
1099 
1100 	PF_RULES_WLOCK();
1101 	strlcpy(V_pf_status.ifname, attrs.ifname, IFNAMSIZ);
1102 	PF_RULES_WUNLOCK();
1103 
1104 	return (0);
1105 }
1106 
1107 static bool
1108 nlattr_add_counters(struct nl_writer *nw, int attr, size_t number, char **names,
1109     counter_u64_t *counters)
1110 {
1111 	for (int i = 0; i < number; i++) {
1112 		int off = nlattr_add_nested(nw, attr);
1113 		nlattr_add_u32(nw, PF_C_ID, i);
1114 		nlattr_add_string(nw, PF_C_NAME, names[i]);
1115 		nlattr_add_u64(nw, PF_C_COUNTER, counter_u64_fetch(counters[i]));
1116 		nlattr_set_len(nw, off);
1117 	}
1118 
1119 	return (true);
1120 }
1121 
1122 static bool
1123 nlattr_add_fcounters(struct nl_writer *nw, int attr, size_t number, char **names,
1124     struct pf_counter_u64 *counters)
1125 {
1126 	for (int i = 0; i < number; i++) {
1127 		int off = nlattr_add_nested(nw, attr);
1128 		nlattr_add_u32(nw, PF_C_ID, i);
1129 		nlattr_add_string(nw, PF_C_NAME, names[i]);
1130 		nlattr_add_u64(nw, PF_C_COUNTER, pf_counter_u64_fetch(&counters[i]));
1131 		nlattr_set_len(nw, off);
1132 	}
1133 
1134 	return (true);
1135 }
1136 
1137 static bool
1138 nlattr_add_u64_array(struct nl_writer *nw, int attr, size_t number, uint64_t *array)
1139 {
1140 	int off = nlattr_add_nested(nw, attr);
1141 
1142 	for (size_t i = 0; i < number; i++)
1143 		nlattr_add_u64(nw, 0, array[i]);
1144 
1145 	nlattr_set_len(nw, off);
1146 
1147 	return (true);
1148 }
1149 
1150 static int
1151 pf_handle_get_status(struct nlmsghdr *hdr, struct nl_pstate *npt)
1152 {
1153 	struct pf_status s;
1154 	struct nl_writer *nw = npt->nw;
1155 	struct genlmsghdr *ghdr_new;
1156 	char *pf_reasons[PFRES_MAX+1] = PFRES_NAMES;
1157 	char *pf_lcounter[KLCNT_MAX+1] = KLCNT_NAMES;
1158 	char *pf_fcounter[FCNT_MAX+1] = FCNT_NAMES;
1159 	int error;
1160 
1161 	PF_RULES_RLOCK_TRACKER;
1162 
1163 	if (!nlmsg_reply(nw, hdr, sizeof(struct genlmsghdr)))
1164 		return (ENOMEM);
1165 
1166 	ghdr_new = nlmsg_reserve_object(nw, struct genlmsghdr);
1167 	ghdr_new->cmd = PFNL_CMD_GET_STATUS;
1168 	ghdr_new->version = 0;
1169 	ghdr_new->reserved = 0;
1170 
1171 	PF_RULES_RLOCK();
1172 
1173 	nlattr_add_string(nw, PF_GS_IFNAME, V_pf_status.ifname);
1174 	nlattr_add_bool(nw, PF_GS_RUNNING, V_pf_status.running);
1175 	nlattr_add_u32(nw, PF_GS_SINCE, V_pf_status.since);
1176 	nlattr_add_u32(nw, PF_GS_DEBUG, V_pf_status.debug);
1177 	nlattr_add_u32(nw, PF_GS_HOSTID, ntohl(V_pf_status.hostid));
1178 	nlattr_add_u32(nw, PF_GS_STATES, V_pf_status.states);
1179 	nlattr_add_u32(nw, PF_GS_SRC_NODES, V_pf_status.src_nodes);
1180 	nlattr_add_u32(nw, PF_GS_REASSEMBLE, V_pf_status.reass);
1181 	nlattr_add_u32(nw, PF_GS_SYNCOOKIES_ACTIVE, V_pf_status.syncookies_active);
1182 
1183 	nlattr_add_counters(nw, PF_GS_COUNTERS, PFRES_MAX, pf_reasons,
1184 	    V_pf_status.counters);
1185 	nlattr_add_counters(nw, PF_GS_LCOUNTERS, KLCNT_MAX, pf_lcounter,
1186 	    V_pf_status.lcounters);
1187 	nlattr_add_fcounters(nw, PF_GS_FCOUNTERS, FCNT_MAX, pf_fcounter,
1188 	    V_pf_status.fcounters);
1189 	nlattr_add_counters(nw, PF_GS_SCOUNTERS, SCNT_MAX, pf_fcounter,
1190 	    V_pf_status.scounters);
1191 
1192 	pfi_update_status(V_pf_status.ifname, &s);
1193 	nlattr_add_u64_array(nw, PF_GS_BCOUNTERS, 2 * 2, (uint64_t *)s.bcounters);
1194 	nlattr_add_u64_array(nw, PF_GS_PCOUNTERS, 2 * 2 * 2, (uint64_t *)s.pcounters);
1195 
1196 	nlattr_add(nw, PF_GS_CHKSUM, PF_MD5_DIGEST_LENGTH, V_pf_status.pf_chksum);
1197 
1198 	PF_RULES_RUNLOCK();
1199 
1200 	if (!nlmsg_end(nw)) {
1201 		error = ENOMEM;
1202 		goto out;
1203 	}
1204 
1205 	return (0);
1206 
1207 out:
1208 	nlmsg_abort(nw);
1209 	return (error);
1210 }
1211 
1212 static int
1213 pf_handle_clear_status(struct nlmsghdr *hdr, struct nl_pstate *npt)
1214 {
1215 	pf_ioctl_clear_status();
1216 
1217 	return (0);
1218 }
1219 
1220 struct pf_nl_natlook {
1221 	sa_family_t af;
1222 	uint8_t direction;
1223 	uint8_t proto;
1224 	struct pf_addr src;
1225 	struct pf_addr dst;
1226 	uint16_t sport;
1227 	uint16_t dport;
1228 };
1229 
1230 #define	_OUT(_field)	offsetof(struct pf_nl_natlook, _field)
1231 static const struct nlattr_parser nla_p_natlook[] = {
1232 	{ .type = PF_NL_AF, .off = _OUT(af), .cb = nlattr_get_uint8 },
1233 	{ .type = PF_NL_DIRECTION, .off = _OUT(direction), .cb = nlattr_get_uint8 },
1234 	{ .type = PF_NL_PROTO, .off = _OUT(proto), .cb = nlattr_get_uint8 },
1235 	{ .type = PF_NL_SRC_ADDR, .off = _OUT(src), .cb = nlattr_get_in6_addr },
1236 	{ .type = PF_NL_DST_ADDR, .off = _OUT(dst), .cb = nlattr_get_in6_addr },
1237 	{ .type = PF_NL_SRC_PORT, .off = _OUT(sport), .cb = nlattr_get_uint16 },
1238 	{ .type = PF_NL_DST_PORT, .off = _OUT(dport), .cb = nlattr_get_uint16 },
1239 };
1240 #undef _OUT
1241 NL_DECLARE_PARSER(natlook_parser, struct genlmsghdr, nlf_p_empty, nla_p_natlook);
1242 
1243 static int
1244 pf_handle_natlook(struct nlmsghdr *hdr, struct nl_pstate *npt)
1245 {
1246 	struct pf_nl_natlook	 attrs = {};
1247 	struct pf_state_key_cmp	 key = {};
1248 	struct nl_writer	*nw = npt->nw;
1249 	struct pf_state_key	*sk;
1250 	struct pf_kstate	*state;
1251 	struct genlmsghdr	*ghdr_new;
1252 	int			 error, m = 0;
1253 	int			 sidx, didx;
1254 
1255 	error = nl_parse_nlmsg(hdr, &natlook_parser, npt, &attrs);
1256 	if (error != 0)
1257 		return (error);
1258 
1259 	if (attrs.proto == 0 ||
1260 	    PF_AZERO(&attrs.src, attrs.af) ||
1261 	    PF_AZERO(&attrs.dst, attrs.af) ||
1262 	    ((attrs.proto == IPPROTO_TCP || attrs.proto == IPPROTO_UDP) &&
1263 	     (attrs.sport == 0 || attrs.dport == 0)))
1264 		return (EINVAL);
1265 
1266 	/* NATLOOK src and dst are reversed, so reverse sidx/didx */
1267 	sidx = (attrs.direction == PF_IN) ? 1 : 0;
1268 	didx = (attrs.direction == PF_IN) ? 0 : 1;
1269 
1270 	key.af = attrs.af;
1271 	key.proto = attrs.proto;
1272 	PF_ACPY(&key.addr[sidx], &attrs.src, attrs.af);
1273 	key.port[sidx] = attrs.sport;
1274 	PF_ACPY(&key.addr[didx], &attrs.dst, attrs.af);
1275 	key.port[didx] = attrs.dport;
1276 
1277 	state = pf_find_state_all(&key, attrs.direction, &m);
1278 	if (state == NULL)
1279 		return (ENOENT);
1280 	if (m > 1) {
1281 		PF_STATE_UNLOCK(state);
1282 		return (E2BIG);
1283 	}
1284 
1285 	if (!nlmsg_reply(nw, hdr, sizeof(struct genlmsghdr))) {
1286 		PF_STATE_UNLOCK(state);
1287 		return (ENOMEM);
1288 	}
1289 
1290 	ghdr_new = nlmsg_reserve_object(nw, struct genlmsghdr);
1291 	ghdr_new->cmd = PFNL_CMD_NATLOOK;
1292 	ghdr_new->version = 0;
1293 	ghdr_new->reserved = 0;
1294 
1295 	sk = state->key[sidx];
1296 
1297 	nlattr_add_in6_addr(nw, PF_NL_SRC_ADDR, &sk->addr[sidx].v6);
1298 	nlattr_add_in6_addr(nw, PF_NL_DST_ADDR, &sk->addr[didx].v6);
1299 	nlattr_add_u16(nw, PF_NL_SRC_PORT, sk->port[sidx]);
1300 	nlattr_add_u16(nw, PF_NL_DST_PORT, sk->port[didx]);
1301 
1302 	PF_STATE_UNLOCK(state);
1303 
1304 	if (!nlmsg_end(nw)) {
1305 		nlmsg_abort(nw);
1306 		return (ENOMEM);
1307 	}
1308 
1309 	return (0);
1310 }
1311 
1312 struct pf_nl_set_debug
1313 {
1314 	uint32_t level;
1315 };
1316 #define	_OUT(_field)	offsetof(struct pf_nl_set_debug, _field)
1317 static const struct nlattr_parser nla_p_set_debug[] = {
1318 	{ .type = PF_SD_LEVEL, .off = _OUT(level), .cb = nlattr_get_uint32 },
1319 };
1320 #undef _OUT
1321 NL_DECLARE_PARSER(set_debug_parser, struct genlmsghdr, nlf_p_empty, nla_p_set_debug);
1322 
1323 static int
1324 pf_handle_set_debug(struct nlmsghdr *hdr, struct nl_pstate *npt)
1325 {
1326 	struct pf_nl_set_debug attrs = {};
1327 	int error;
1328 
1329 	error = nl_parse_nlmsg(hdr, &set_debug_parser, npt, &attrs);
1330 	if (error != 0)
1331 		return (error);
1332 
1333 	PF_RULES_WLOCK();
1334 	V_pf_status.debug = attrs.level;
1335 	PF_RULES_WUNLOCK();
1336 
1337 	return (0);
1338 }
1339 
1340 struct pf_nl_set_timeout
1341 {
1342 	uint32_t timeout;
1343 	uint32_t seconds;
1344 };
1345 #define	_OUT(_field)	offsetof(struct pf_nl_set_timeout, _field)
1346 static const struct nlattr_parser nla_p_set_timeout[] = {
1347 	{ .type = PF_TO_TIMEOUT, .off = _OUT(timeout), .cb = nlattr_get_uint32 },
1348 	{ .type = PF_TO_SECONDS, .off = _OUT(seconds), .cb = nlattr_get_uint32 },
1349 };
1350 #undef _OUT
1351 NL_DECLARE_PARSER(set_timeout_parser, struct genlmsghdr, nlf_p_empty, nla_p_set_timeout);
1352 
1353 static int
1354 pf_handle_set_timeout(struct nlmsghdr *hdr, struct nl_pstate *npt)
1355 {
1356 	struct pf_nl_set_timeout attrs = {};
1357 	int error;
1358 
1359 	error = nl_parse_nlmsg(hdr, &set_timeout_parser, npt, &attrs);
1360 	if (error != 0)
1361 		return (error);
1362 
1363 	return (pf_ioctl_set_timeout(attrs.timeout, attrs.seconds, NULL));
1364 }
1365 
1366 static int
1367 pf_handle_get_timeout(struct nlmsghdr *hdr, struct nl_pstate *npt)
1368 {
1369 	struct pf_nl_set_timeout attrs = {};
1370 	struct nl_writer *nw = npt->nw;
1371 	struct genlmsghdr *ghdr_new;
1372 	int error;
1373 
1374 	error = nl_parse_nlmsg(hdr, &set_timeout_parser, npt, &attrs);
1375 	if (error != 0)
1376 		return (error);
1377 
1378 	error = pf_ioctl_get_timeout(attrs.timeout, &attrs.seconds);
1379 	if (error != 0)
1380 		return (error);
1381 
1382 	if (!nlmsg_reply(nw, hdr, sizeof(struct genlmsghdr)))
1383 		return (ENOMEM);
1384 
1385 	ghdr_new = nlmsg_reserve_object(nw, struct genlmsghdr);
1386 	ghdr_new->cmd = PFNL_CMD_GET_TIMEOUT;
1387 	ghdr_new->version = 0;
1388 	ghdr_new->reserved = 0;
1389 
1390 	nlattr_add_u32(nw, PF_TO_SECONDS, attrs.seconds);
1391 
1392 	if (!nlmsg_end(nw)) {
1393 		nlmsg_abort(nw);
1394 		return (ENOMEM);
1395 	}
1396 
1397 	return (0);
1398 }
1399 
1400 struct pf_nl_set_limit
1401 {
1402 	uint32_t index;
1403 	uint32_t limit;
1404 };
1405 #define	_OUT(_field)	offsetof(struct pf_nl_set_limit, _field)
1406 static const struct nlattr_parser nla_p_set_limit[] = {
1407 	{ .type = PF_LI_INDEX, .off = _OUT(index), .cb = nlattr_get_uint32 },
1408 	{ .type = PF_LI_LIMIT, .off = _OUT(limit), .cb = nlattr_get_uint32 },
1409 };
1410 #undef _OUT
1411 NL_DECLARE_PARSER(set_limit_parser, struct genlmsghdr, nlf_p_empty, nla_p_set_limit);
1412 
1413 static int
1414 pf_handle_set_limit(struct nlmsghdr *hdr, struct nl_pstate *npt)
1415 {
1416 	struct pf_nl_set_limit attrs = {};
1417 	int error;
1418 
1419 	error = nl_parse_nlmsg(hdr, &set_limit_parser, npt, &attrs);
1420 	if (error != 0)
1421 		return (error);
1422 
1423 	return (pf_ioctl_set_limit(attrs.index, attrs.limit, NULL));
1424 }
1425 
1426 static int
1427 pf_handle_get_limit(struct nlmsghdr *hdr, struct nl_pstate *npt)
1428 {
1429 	struct pf_nl_set_limit attrs = {};
1430 	struct nl_writer *nw = npt->nw;
1431 	struct genlmsghdr *ghdr_new;
1432 	int error;
1433 
1434 	error = nl_parse_nlmsg(hdr, &set_limit_parser, npt, &attrs);
1435 	if (error != 0)
1436 		return (error);
1437 
1438 	error = pf_ioctl_get_limit(attrs.index, &attrs.limit);
1439 	if (error != 0)
1440 		return (error);
1441 
1442 	if (!nlmsg_reply(nw, hdr, sizeof(struct genlmsghdr)))
1443 		return (ENOMEM);
1444 
1445 	ghdr_new = nlmsg_reserve_object(nw, struct genlmsghdr);
1446 	ghdr_new->cmd = PFNL_CMD_GET_LIMIT;
1447 	ghdr_new->version = 0;
1448 	ghdr_new->reserved = 0;
1449 
1450 	nlattr_add_u32(nw, PF_LI_LIMIT, attrs.limit);
1451 
1452 	if (!nlmsg_end(nw)) {
1453 		nlmsg_abort(nw);
1454 		return (ENOMEM);
1455 	}
1456 
1457 	return (0);
1458 }
1459 
1460 static int
1461 pf_handle_begin_addrs(struct nlmsghdr *hdr, struct nl_pstate *npt)
1462 {
1463 	struct nl_writer *nw = npt->nw;
1464 	struct genlmsghdr *ghdr_new;
1465 	uint32_t ticket;
1466 	int error;
1467 
1468 	error = pf_ioctl_begin_addrs(&ticket);
1469 	if (error != 0)
1470 		return (error);
1471 
1472 	if (!nlmsg_reply(nw, hdr, sizeof(struct genlmsghdr)))
1473 		return (ENOMEM);
1474 
1475 	ghdr_new = nlmsg_reserve_object(nw, struct genlmsghdr);
1476 	ghdr_new->cmd = PFNL_CMD_BEGIN_ADDRS;
1477 	ghdr_new->version = 0;
1478 	ghdr_new->reserved = 0;
1479 
1480 	nlattr_add_u32(nw, PF_BA_TICKET, ticket);
1481 
1482 	if (!nlmsg_end(nw)) {
1483 		nlmsg_abort(nw);
1484 		return (ENOMEM);
1485 	}
1486 
1487 	return (0);
1488 }
1489 
1490 static bool
1491 nlattr_add_pool_addr(struct nl_writer *nw, int attrtype, struct pf_pooladdr *a)
1492 {
1493 	int off;
1494 
1495 	off = nlattr_add_nested(nw, attrtype);
1496 
1497 	nlattr_add_addr_wrap(nw, PF_PA_ADDR, &a->addr);
1498 	nlattr_add_string(nw, PF_PA_IFNAME, a->ifname);
1499 
1500 	nlattr_set_len(nw, off);
1501 
1502 	return (true);
1503 }
1504 
1505 #define _OUT(_field)	offsetof(struct pf_pooladdr, _field)
1506 static const struct nlattr_parser nla_p_pool_addr[] = {
1507 	{ .type = PF_PA_ADDR, .off = _OUT(addr), .arg = &addr_wrap_parser, .cb = nlattr_get_nested },
1508 	{ .type = PF_PA_IFNAME, .off = _OUT(ifname), .arg = (void *)IFNAMSIZ, .cb = nlattr_get_chara },
1509 };
1510 NL_DECLARE_ATTR_PARSER(pool_addr_parser, nla_p_pool_addr);
1511 #undef _OUT
1512 
1513 #define	_OUT(_field)	offsetof(struct pf_nl_pooladdr, _field)
1514 static const struct nlattr_parser nla_p_add_addr[] = {
1515 	{ .type = PF_AA_ACTION, .off = _OUT(action), .cb = nlattr_get_uint32 },
1516 	{ .type = PF_AA_TICKET, .off = _OUT(ticket), .cb = nlattr_get_uint32 },
1517 	{ .type = PF_AA_NR, .off = _OUT(nr), .cb = nlattr_get_uint32 },
1518 	{ .type = PF_AA_R_NUM, .off = _OUT(r_num), .cb = nlattr_get_uint32 },
1519 	{ .type = PF_AA_R_ACTION, .off = _OUT(r_action), .cb = nlattr_get_uint8 },
1520 	{ .type = PF_AA_R_LAST, .off = _OUT(r_last), .cb = nlattr_get_uint8 },
1521 	{ .type = PF_AA_AF, .off = _OUT(af), .cb = nlattr_get_uint8 },
1522 	{ .type = PF_AA_ANCHOR, .off = _OUT(anchor), .arg = (void *)MAXPATHLEN, .cb = nlattr_get_chara },
1523 	{ .type = PF_AA_ADDR, .off = _OUT(addr), .arg = &pool_addr_parser, .cb = nlattr_get_nested },
1524 	{ .type = PF_AA_WHICH, .off = _OUT(which), .cb = nlattr_get_uint32 },
1525 };
1526 #undef _OUT
1527 NL_DECLARE_PARSER(add_addr_parser, struct genlmsghdr, nlf_p_empty, nla_p_add_addr);
1528 
1529 static int
1530 pf_handle_add_addr(struct nlmsghdr *hdr, struct nl_pstate *npt)
1531 {
1532 	struct pf_nl_pooladdr attrs = { 0 };
1533 	int error;
1534 
1535 	error = nl_parse_nlmsg(hdr, &add_addr_parser, npt, &attrs);
1536 	if (error != 0)
1537 		return (error);
1538 
1539 	if (attrs.which == 0)
1540 		attrs.which = PF_RDR;
1541 
1542 	error = pf_ioctl_add_addr(&attrs);
1543 
1544 	return (error);
1545 }
1546 
1547 static int
1548 pf_handle_get_addrs(struct nlmsghdr *hdr, struct nl_pstate *npt)
1549 {
1550 	struct pf_nl_pooladdr attrs = { 0 };
1551 	struct nl_writer *nw = npt->nw;
1552 	struct genlmsghdr *ghdr_new;
1553 	int error;
1554 
1555 	error = nl_parse_nlmsg(hdr, &add_addr_parser, npt, &attrs);
1556 	if (error != 0)
1557 		return (error);
1558 
1559 	if (attrs.which == 0)
1560 		attrs.which = PF_RDR;
1561 
1562 	error = pf_ioctl_get_addrs(&attrs);
1563 	if (error != 0)
1564 		return (error);
1565 
1566 	if (!nlmsg_reply(nw, hdr, sizeof(struct genlmsghdr)))
1567 		return (ENOMEM);
1568 
1569 	ghdr_new = nlmsg_reserve_object(nw, struct genlmsghdr);
1570 	ghdr_new->cmd = PFNL_CMD_GET_ADDRS;
1571 	ghdr_new->version = 0;
1572 	ghdr_new->reserved = 0;
1573 
1574 	nlattr_add_u32(nw, PF_AA_NR, attrs.nr);
1575 
1576 	if (!nlmsg_end(nw)) {
1577 		nlmsg_abort(nw);
1578 		return (ENOMEM);
1579 	}
1580 
1581 	return (error);
1582 }
1583 
1584 static int
1585 pf_handle_get_addr(struct nlmsghdr *hdr, struct nl_pstate *npt)
1586 {
1587 	struct pf_nl_pooladdr attrs = { 0 };
1588 	struct nl_writer *nw = npt->nw;
1589 	struct genlmsghdr *ghdr_new;
1590 	int error;
1591 
1592 	error = nl_parse_nlmsg(hdr, &add_addr_parser, npt, &attrs);
1593 	if (error != 0)
1594 		return (error);
1595 
1596 	if (attrs.which == 0)
1597 		attrs.which = PF_RDR;
1598 
1599 	error = pf_ioctl_get_addr(&attrs);
1600 	if (error != 0)
1601 		return (error);
1602 
1603 	if (!nlmsg_reply(nw, hdr, sizeof(struct genlmsghdr)))
1604 		return (ENOMEM);
1605 
1606 	ghdr_new = nlmsg_reserve_object(nw, struct genlmsghdr);
1607 	ghdr_new->cmd = PFNL_CMD_GET_ADDR;
1608 	ghdr_new->version = 0;
1609 	ghdr_new->reserved = 0;
1610 
1611 	nlattr_add_u32(nw, PF_AA_ACTION, attrs.action);
1612 	nlattr_add_u32(nw, PF_AA_TICKET, attrs.ticket);
1613 	nlattr_add_u32(nw, PF_AA_NR, attrs.nr);
1614 	nlattr_add_u32(nw, PF_AA_R_NUM, attrs.r_num);
1615 	nlattr_add_u8(nw, PF_AA_R_ACTION, attrs.r_action);
1616 	nlattr_add_u8(nw, PF_AA_R_LAST, attrs.r_last);
1617 	nlattr_add_u8(nw, PF_AA_AF, attrs.af);
1618 	nlattr_add_string(nw, PF_AA_ANCHOR, attrs.anchor);
1619 	nlattr_add_pool_addr(nw, PF_AA_ADDR, &attrs.addr);
1620 
1621 	if (!nlmsg_end(nw)) {
1622 		nlmsg_abort(nw);
1623 		return (ENOMEM);
1624 	}
1625 
1626 	return (0);
1627 }
1628 
1629 #define _OUT(_field)	offsetof(struct pfioc_ruleset, _field)
1630 static const struct nlattr_parser nla_p_ruleset[] = {
1631 	{ .type = PF_RS_PATH, .off = _OUT(path), .arg = (void *)MAXPATHLEN, .cb = nlattr_get_chara },
1632 	{ .type = PF_RS_NR, .off = _OUT(nr), .cb = nlattr_get_uint32 },
1633 };
1634 NL_DECLARE_PARSER(ruleset_parser, struct genlmsghdr, nlf_p_empty, nla_p_ruleset);
1635 #undef _OUT
1636 
1637 static int
1638 pf_handle_get_rulesets(struct nlmsghdr *hdr, struct nl_pstate *npt)
1639 {
1640 	struct pfioc_ruleset attrs = { 0 };
1641 	struct nl_writer *nw = npt->nw;
1642 	struct genlmsghdr *ghdr_new;
1643 	int error;
1644 
1645 	error = nl_parse_nlmsg(hdr, &ruleset_parser, npt, &attrs);
1646 	if (error != 0)
1647 		return (error);
1648 
1649 	error = pf_ioctl_get_rulesets(&attrs);
1650 	if (error != 0)
1651 		return (error);
1652 
1653 	if (!nlmsg_reply(nw, hdr, sizeof(struct genlmsghdr)))
1654 		return (ENOMEM);
1655 
1656 	ghdr_new = nlmsg_reserve_object(nw, struct genlmsghdr);
1657 	ghdr_new->cmd = PFNL_CMD_GET_RULESETS;
1658 	ghdr_new->version = 0;
1659 	ghdr_new->reserved = 0;
1660 
1661 	nlattr_add_u32(nw, PF_RS_NR, attrs.nr);
1662 
1663 	if (!nlmsg_end(nw)) {
1664 		nlmsg_abort(nw);
1665 		return (ENOMEM);
1666 	}
1667 
1668 	return (0);
1669 }
1670 
1671 static int
1672 pf_handle_get_ruleset(struct nlmsghdr *hdr, struct nl_pstate *npt)
1673 {
1674 	struct pfioc_ruleset attrs = { 0 };
1675 	struct nl_writer *nw = npt->nw;
1676 	struct genlmsghdr *ghdr_new;
1677 	int error;
1678 
1679 	error = nl_parse_nlmsg(hdr, &ruleset_parser, npt, &attrs);
1680 	if (error)
1681 		return (error);
1682 
1683 	error = pf_ioctl_get_ruleset(&attrs);
1684 	if (error != 0)
1685 		return (error);
1686 
1687 	if (!nlmsg_reply(nw, hdr, sizeof(struct genlmsghdr)))
1688 		return (ENOMEM);
1689 
1690 	ghdr_new = nlmsg_reserve_object(nw, struct genlmsghdr);
1691 	ghdr_new->cmd = PFNL_CMD_GET_RULESET;
1692 	ghdr_new->version = 0;
1693 	ghdr_new->reserved = 0;
1694 
1695 	nlattr_add_string(nw, PF_RS_NAME, attrs.name);
1696 
1697 	if (!nlmsg_end(nw)) {
1698 		nlmsg_abort(nw);
1699 		return (ENOMEM);
1700 	}
1701 
1702 	return (0);
1703 }
1704 
1705 static bool
1706 nlattr_add_pf_threshold(struct nl_writer *nw, int attrtype,
1707     struct pf_threshold *t, int secs)
1708 {
1709 	int	 off = nlattr_add_nested(nw, attrtype);
1710 	int	 diff, conn_rate_count;
1711 
1712 	/* Adjust the connection rate estimate. */
1713 	conn_rate_count = t->count;
1714 	diff = secs - t->last;
1715 	if (diff >= t->seconds)
1716 		conn_rate_count = 0;
1717 	else
1718 		conn_rate_count -= t->count * diff / t->seconds;
1719 
1720 	nlattr_add_u32(nw, PF_TH_LIMIT, t->limit);
1721 	nlattr_add_u32(nw, PF_TH_SECONDS, t->seconds);
1722 	nlattr_add_u32(nw, PF_TH_COUNT, conn_rate_count);
1723 	nlattr_add_u32(nw, PF_TH_LAST, t->last);
1724 
1725 	nlattr_set_len(nw, off);
1726 
1727 	return (true);
1728 }
1729 
1730 static int
1731 pf_handle_get_srcnodes(struct nlmsghdr *hdr, struct nl_pstate *npt)
1732 {
1733 	struct nl_writer	*nw = npt->nw;
1734 	struct genlmsghdr	*ghdr_new;
1735 	struct pf_ksrc_node	*n;
1736 	struct pf_srchash	*sh;
1737 	int			 i;
1738 	int			 secs;
1739 
1740 	hdr->nlmsg_flags |= NLM_F_MULTI;
1741 
1742 	for (i = 0, sh = V_pf_srchash; i <= V_pf_srchashmask;
1743 	    i++, sh++) {
1744 		/* Avoid locking empty rows. */
1745 		if (LIST_EMPTY(&sh->nodes))
1746 			continue;
1747 
1748 		PF_HASHROW_LOCK(sh);
1749 		secs = time_uptime;
1750 
1751 		LIST_FOREACH(n, &sh->nodes, entry) {
1752 			if (!nlmsg_reply(nw, hdr, sizeof(struct genlmsghdr))) {
1753 				nlmsg_abort(nw);
1754 				return (ENOMEM);
1755 			}
1756 
1757 			ghdr_new = nlmsg_reserve_object(nw, struct genlmsghdr);
1758 			ghdr_new->cmd = PFNL_CMD_GET_SRCNODES;
1759 			ghdr_new->version = 0;
1760 			ghdr_new->reserved = 0;
1761 
1762 			nlattr_add_in6_addr(nw, PF_SN_ADDR, &n->addr.v6);
1763 			nlattr_add_in6_addr(nw, PF_SN_RADDR, &n->raddr.v6);
1764 			nlattr_add_u32(nw, PF_SN_RULE_NR, n->rule->nr);
1765 			nlattr_add_u64(nw, PF_SN_BYTES_IN, counter_u64_fetch(n->bytes[0]));
1766 			nlattr_add_u64(nw, PF_SN_BYTES_OUT, counter_u64_fetch(n->bytes[1]));
1767 			nlattr_add_u64(nw, PF_SN_PACKETS_IN, counter_u64_fetch(n->packets[0]));
1768 			nlattr_add_u64(nw, PF_SN_PACKETS_OUT, counter_u64_fetch(n->packets[1]));
1769 			nlattr_add_u32(nw, PF_SN_STATES, n->states);
1770 			nlattr_add_u32(nw, PF_SN_CONNECTIONS, n->conn);
1771 			nlattr_add_u8(nw, PF_SN_AF, n->af);
1772 			nlattr_add_u8(nw, PF_SN_NAF, n->naf);
1773 			nlattr_add_u8(nw, PF_SN_RULE_TYPE, n->ruletype);
1774 
1775 			nlattr_add_u64(nw, PF_SN_CREATION, secs - n->creation);
1776 			if (n->expire > secs)
1777 				nlattr_add_u64(nw, PF_SN_EXPIRE, n->expire - secs);
1778 			else
1779 				nlattr_add_u64(nw, PF_SN_EXPIRE, 0);
1780 
1781 			nlattr_add_pf_threshold(nw, PF_SN_CONNECTION_RATE,
1782 			    &n->conn_rate, secs);
1783 
1784 			if (!nlmsg_end(nw)) {
1785 				PF_HASHROW_UNLOCK(sh);
1786 				nlmsg_abort(nw);
1787 				return (ENOMEM);
1788 			}
1789 		}
1790 		PF_HASHROW_UNLOCK(sh);
1791 	}
1792 
1793 	return (0);
1794 }
1795 
1796 static const struct nlhdr_parser *all_parsers[] = {
1797 	&state_parser,
1798 	&addrule_parser,
1799 	&getrules_parser,
1800 	&clear_states_parser,
1801 	&set_statusif_parser,
1802 	&natlook_parser,
1803 	&set_debug_parser,
1804 	&set_timeout_parser,
1805 	&set_limit_parser,
1806 	&pool_addr_parser,
1807 	&add_addr_parser,
1808 	&ruleset_parser,
1809 };
1810 
1811 static int family_id;
1812 
1813 static const struct genl_cmd pf_cmds[] = {
1814 	{
1815 		.cmd_num = PFNL_CMD_GETSTATES,
1816 		.cmd_name = "GETSTATES",
1817 		.cmd_cb = pf_handle_getstates,
1818 		.cmd_flags = GENL_CMD_CAP_DO | GENL_CMD_CAP_DUMP | GENL_CMD_CAP_HASPOL,
1819 		.cmd_priv = PRIV_NETINET_PF,
1820 	},
1821 	{
1822 		.cmd_num = PFNL_CMD_GETCREATORS,
1823 		.cmd_name = "GETCREATORS",
1824 		.cmd_cb = pf_handle_getcreators,
1825 		.cmd_flags = GENL_CMD_CAP_DO | GENL_CMD_CAP_DUMP | GENL_CMD_CAP_HASPOL,
1826 		.cmd_priv = PRIV_NETINET_PF,
1827 	},
1828 	{
1829 		.cmd_num = PFNL_CMD_START,
1830 		.cmd_name = "START",
1831 		.cmd_cb = pf_handle_start,
1832 		.cmd_flags = GENL_CMD_CAP_DO | GENL_CMD_CAP_HASPOL,
1833 		.cmd_priv = PRIV_NETINET_PF,
1834 	},
1835 	{
1836 		.cmd_num = PFNL_CMD_STOP,
1837 		.cmd_name = "STOP",
1838 		.cmd_cb = pf_handle_stop,
1839 		.cmd_flags = GENL_CMD_CAP_DO | GENL_CMD_CAP_HASPOL,
1840 		.cmd_priv = PRIV_NETINET_PF,
1841 	},
1842 	{
1843 		.cmd_num = PFNL_CMD_ADDRULE,
1844 		.cmd_name = "ADDRULE",
1845 		.cmd_cb = pf_handle_addrule,
1846 		.cmd_flags = GENL_CMD_CAP_DO | GENL_CMD_CAP_DUMP | GENL_CMD_CAP_HASPOL,
1847 		.cmd_priv = PRIV_NETINET_PF,
1848 	},
1849 	{
1850 		.cmd_num = PFNL_CMD_GETRULES,
1851 		.cmd_name = "GETRULES",
1852 		.cmd_cb = pf_handle_getrules,
1853 		.cmd_flags = GENL_CMD_CAP_DUMP | GENL_CMD_CAP_HASPOL,
1854 		.cmd_priv = PRIV_NETINET_PF,
1855 	},
1856 	{
1857 		.cmd_num = PFNL_CMD_GETRULE,
1858 		.cmd_name = "GETRULE",
1859 		.cmd_cb = pf_handle_getrule,
1860 		.cmd_flags = GENL_CMD_CAP_DUMP | GENL_CMD_CAP_HASPOL,
1861 		.cmd_priv = PRIV_NETINET_PF,
1862 	},
1863 	{
1864 		.cmd_num = PFNL_CMD_CLRSTATES,
1865 		.cmd_name = "CLRSTATES",
1866 		.cmd_cb = pf_handle_clear_states,
1867 		.cmd_flags = GENL_CMD_CAP_DO | GENL_CMD_CAP_DUMP | GENL_CMD_CAP_HASPOL,
1868 		.cmd_priv = PRIV_NETINET_PF,
1869 	},
1870 	{
1871 		.cmd_num = PFNL_CMD_KILLSTATES,
1872 		.cmd_name = "KILLSTATES",
1873 		.cmd_cb = pf_handle_kill_states,
1874 		.cmd_flags = GENL_CMD_CAP_DO | GENL_CMD_CAP_DUMP | GENL_CMD_CAP_HASPOL,
1875 		.cmd_priv = PRIV_NETINET_PF,
1876 	},
1877 	{
1878 		.cmd_num = PFNL_CMD_SET_STATUSIF,
1879 		.cmd_name = "SETSTATUSIF",
1880 		.cmd_cb = pf_handle_set_statusif,
1881 		.cmd_flags = GENL_CMD_CAP_DO | GENL_CMD_CAP_HASPOL,
1882 		.cmd_priv = PRIV_NETINET_PF,
1883 	},
1884 	{
1885 		.cmd_num = PFNL_CMD_GET_STATUS,
1886 		.cmd_name = "GETSTATUS",
1887 		.cmd_cb = pf_handle_get_status,
1888 		.cmd_flags = GENL_CMD_CAP_DUMP | GENL_CMD_CAP_HASPOL,
1889 		.cmd_priv = PRIV_NETINET_PF,
1890 	},
1891 	{
1892 		.cmd_num = PFNL_CMD_CLEAR_STATUS,
1893 		.cmd_name = "CLEARSTATUS",
1894 		.cmd_cb = pf_handle_clear_status,
1895 		.cmd_flags = GENL_CMD_CAP_DO | GENL_CMD_CAP_HASPOL,
1896 		.cmd_priv = PRIV_NETINET_PF,
1897 	},
1898 	{
1899 		.cmd_num = PFNL_CMD_NATLOOK,
1900 		.cmd_name = "NATLOOK",
1901 		.cmd_cb = pf_handle_natlook,
1902 		.cmd_flags = GENL_CMD_CAP_DUMP | GENL_CMD_CAP_HASPOL,
1903 		.cmd_priv = PRIV_NETINET_PF,
1904 	},
1905 	{
1906 		.cmd_num = PFNL_CMD_SET_DEBUG,
1907 		.cmd_name = "SET_DEBUG",
1908 		.cmd_cb = pf_handle_set_debug,
1909 		.cmd_flags = GENL_CMD_CAP_DO | GENL_CMD_CAP_HASPOL,
1910 		.cmd_priv = PRIV_NETINET_PF,
1911 	},
1912 	{
1913 		.cmd_num = PFNL_CMD_SET_TIMEOUT,
1914 		.cmd_name = "SET_TIMEOUT",
1915 		.cmd_cb = pf_handle_set_timeout,
1916 		.cmd_flags = GENL_CMD_CAP_DO | GENL_CMD_CAP_HASPOL,
1917 		.cmd_priv = PRIV_NETINET_PF,
1918 	},
1919 	{
1920 		.cmd_num = PFNL_CMD_GET_TIMEOUT,
1921 		.cmd_name = "GET_TIMEOUT",
1922 		.cmd_cb = pf_handle_get_timeout,
1923 		.cmd_flags = GENL_CMD_CAP_DUMP | GENL_CMD_CAP_HASPOL,
1924 		.cmd_priv = PRIV_NETINET_PF,
1925 	},
1926 	{
1927 		.cmd_num = PFNL_CMD_SET_LIMIT,
1928 		.cmd_name = "SET_LIMIT",
1929 		.cmd_cb = pf_handle_set_limit,
1930 		.cmd_flags = GENL_CMD_CAP_DO | GENL_CMD_CAP_HASPOL,
1931 		.cmd_priv = PRIV_NETINET_PF,
1932 	},
1933 	{
1934 		.cmd_num = PFNL_CMD_GET_LIMIT,
1935 		.cmd_name = "GET_LIMIT",
1936 		.cmd_cb = pf_handle_get_limit,
1937 		.cmd_flags = GENL_CMD_CAP_DUMP | GENL_CMD_CAP_HASPOL,
1938 		.cmd_priv = PRIV_NETINET_PF,
1939 	},
1940 	{
1941 		.cmd_num = PFNL_CMD_BEGIN_ADDRS,
1942 		.cmd_name = "BEGIN_ADDRS",
1943 		.cmd_cb = pf_handle_begin_addrs,
1944 		.cmd_flags = GENL_CMD_CAP_DO | GENL_CMD_CAP_DUMP | GENL_CMD_CAP_HASPOL,
1945 		.cmd_priv = PRIV_NETINET_PF,
1946 	},
1947 	{
1948 		.cmd_num = PFNL_CMD_ADD_ADDR,
1949 		.cmd_name = "ADD_ADDR",
1950 		.cmd_cb = pf_handle_add_addr,
1951 		.cmd_flags = GENL_CMD_CAP_DO | GENL_CMD_CAP_HASPOL,
1952 		.cmd_priv = PRIV_NETINET_PF,
1953 	},
1954 	{
1955 		.cmd_num = PFNL_CMD_GET_ADDRS,
1956 		.cmd_name = "GET_ADDRS",
1957 		.cmd_cb = pf_handle_get_addrs,
1958 		.cmd_flags = GENL_CMD_CAP_DUMP | GENL_CMD_CAP_HASPOL,
1959 		.cmd_priv = PRIV_NETINET_PF,
1960 	},
1961 	{
1962 		.cmd_num = PFNL_CMD_GET_ADDR,
1963 		.cmd_name = "GET_ADDRS",
1964 		.cmd_cb = pf_handle_get_addr,
1965 		.cmd_flags = GENL_CMD_CAP_DUMP | GENL_CMD_CAP_HASPOL,
1966 		.cmd_priv = PRIV_NETINET_PF,
1967 	},
1968 	{
1969 		.cmd_num = PFNL_CMD_GET_RULESETS,
1970 		.cmd_name = "GET_RULESETS",
1971 		.cmd_cb = pf_handle_get_rulesets,
1972 		.cmd_flags = GENL_CMD_CAP_DUMP | GENL_CMD_CAP_HASPOL,
1973 		.cmd_priv = PRIV_NETINET_PF,
1974 	},
1975 	{
1976 		.cmd_num = PFNL_CMD_GET_RULESET,
1977 		.cmd_name = "GET_RULESET",
1978 		.cmd_cb = pf_handle_get_ruleset,
1979 		.cmd_flags = GENL_CMD_CAP_DUMP | GENL_CMD_CAP_HASPOL,
1980 		.cmd_priv = PRIV_NETINET_PF,
1981 	},
1982 	{
1983 		.cmd_num = PFNL_CMD_GET_SRCNODES,
1984 		.cmd_name = "GET_SRCNODES",
1985 		.cmd_cb = pf_handle_get_srcnodes,
1986 		.cmd_flags = GENL_CMD_CAP_DUMP | GENL_CMD_CAP_HASPOL,
1987 		.cmd_priv = PRIV_NETINET_PF,
1988 	},
1989 };
1990 
1991 void
1992 pf_nl_register(void)
1993 {
1994 	NL_VERIFY_PARSERS(all_parsers);
1995 
1996 	family_id = genl_register_family(PFNL_FAMILY_NAME, 0, 2, PFNL_CMD_MAX);
1997 	genl_register_cmds(PFNL_FAMILY_NAME, pf_cmds, nitems(pf_cmds));
1998 }
1999 
2000 void
2001 pf_nl_unregister(void)
2002 {
2003 	genl_unregister_family(PFNL_FAMILY_NAME);
2004 }
2005