xref: /freebsd/sys/netpfil/pf/pf_nl.c (revision 9c125336727b48c576bf3598d7d2c52daa5909d7)
12cef6288SAlexander V. Chernikov /*-
22cef6288SAlexander V. Chernikov  * SPDX-License-Identifier: BSD-2-Clause
32cef6288SAlexander V. Chernikov  *
42cef6288SAlexander V. Chernikov  * Copyright (c) 2023 Alexander V. Chernikov <melifaro@FreeBSD.org>
52cef6288SAlexander V. Chernikov  * Copyright (c) 2023 Rubicon Communications, LLC (Netgate)
62cef6288SAlexander V. Chernikov  *
72cef6288SAlexander V. Chernikov  * Redistribution and use in source and binary forms, with or without
82cef6288SAlexander V. Chernikov  * modification, are permitted provided that the following conditions
92cef6288SAlexander V. Chernikov  * are met:
102cef6288SAlexander V. Chernikov  * 1. Redistributions of source code must retain the above copyright
112cef6288SAlexander V. Chernikov  *    notice, this list of conditions and the following disclaimer.
122cef6288SAlexander V. Chernikov  * 2. Redistributions in binary form must reproduce the above copyright
132cef6288SAlexander V. Chernikov  *    notice, this list of conditions and the following disclaimer in the
142cef6288SAlexander V. Chernikov  *    documentation and/or other materials provided with the distribution.
152cef6288SAlexander V. Chernikov  *
162cef6288SAlexander V. Chernikov  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
172cef6288SAlexander V. Chernikov  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
182cef6288SAlexander V. Chernikov  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
192cef6288SAlexander V. Chernikov  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
202cef6288SAlexander V. Chernikov  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
212cef6288SAlexander V. Chernikov  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
222cef6288SAlexander V. Chernikov  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
232cef6288SAlexander V. Chernikov  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
242cef6288SAlexander V. Chernikov  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
252cef6288SAlexander V. Chernikov  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
262cef6288SAlexander V. Chernikov  * SUCH DAMAGE.
272cef6288SAlexander V. Chernikov  *
282cef6288SAlexander V. Chernikov  */
2971d3c704SKristof Provost #include <sys/cdefs.h>
3071d3c704SKristof Provost #include "opt_inet.h"
3171d3c704SKristof Provost #include "opt_inet6.h"
322cef6288SAlexander V. Chernikov 
332cef6288SAlexander V. Chernikov #include <sys/param.h>
342cef6288SAlexander V. Chernikov #include <sys/malloc.h>
352cef6288SAlexander V. Chernikov #include <sys/mbuf.h>
36e774c1efSKristof Provost #include <sys/priv.h>
372cef6288SAlexander V. Chernikov #include <sys/socket.h>
387b2ab18fSKonstantin Belousov #include <sys/ucred.h>
392cef6288SAlexander V. Chernikov 
402cef6288SAlexander V. Chernikov #include <net/pfvar.h>
412cef6288SAlexander V. Chernikov 
422cef6288SAlexander V. Chernikov #include <netlink/netlink.h>
432cef6288SAlexander V. Chernikov #include <netlink/netlink_ctl.h>
442cef6288SAlexander V. Chernikov #include <netlink/netlink_generic.h>
452cef6288SAlexander V. Chernikov #include <netlink/netlink_message_writer.h>
462cef6288SAlexander V. Chernikov 
472cef6288SAlexander V. Chernikov #include <netpfil/pf/pf_nl.h>
482cef6288SAlexander V. Chernikov 
492cef6288SAlexander V. Chernikov #define	DEBUG_MOD_NAME	nl_pf
502cef6288SAlexander V. Chernikov #define	DEBUG_MAX_LEVEL	LOG_DEBUG3
512cef6288SAlexander V. Chernikov #include <netlink/netlink_debug.h>
522cef6288SAlexander V. Chernikov _DECLARE_DEBUG(LOG_DEBUG);
532cef6288SAlexander V. Chernikov 
542cef6288SAlexander V. Chernikov struct nl_parsed_state {
552cef6288SAlexander V. Chernikov 	uint8_t		version;
562cef6288SAlexander V. Chernikov 	uint32_t	id;
572cef6288SAlexander V. Chernikov 	uint32_t	creatorid;
58044eef6aSKristof Provost 	char		ifname[IFNAMSIZ];
59044eef6aSKristof Provost 	uint16_t	proto;
60044eef6aSKristof Provost 	sa_family_t	af;
61044eef6aSKristof Provost 	struct pf_addr	addr;
62044eef6aSKristof Provost 	struct pf_addr	mask;
632cef6288SAlexander V. Chernikov };
642cef6288SAlexander V. Chernikov 
652cef6288SAlexander V. Chernikov #define	_IN(_field)	offsetof(struct genlmsghdr, _field)
662cef6288SAlexander V. Chernikov #define	_OUT(_field)	offsetof(struct nl_parsed_state, _field)
672cef6288SAlexander V. Chernikov static const struct nlattr_parser nla_p_state[] = {
682cef6288SAlexander V. Chernikov 	{ .type = PF_ST_ID, .off = _OUT(id), .cb = nlattr_get_uint32 },
692cef6288SAlexander V. Chernikov 	{ .type = PF_ST_CREATORID, .off = _OUT(creatorid), .cb = nlattr_get_uint32 },
70044eef6aSKristof Provost 	{ .type = PF_ST_IFNAME, .arg = (const void *)IFNAMSIZ, .off = _OUT(ifname), .cb = nlattr_get_chara },
71e8ff5e56SKristof Provost 	{ .type = PF_ST_AF, .off = _OUT(af), .cb = nlattr_get_uint8 },
72044eef6aSKristof Provost 	{ .type = PF_ST_PROTO, .off = _OUT(proto), .cb = nlattr_get_uint16 },
73044eef6aSKristof Provost 	{ .type = PF_ST_FILTER_ADDR, .off = _OUT(addr), .cb = nlattr_get_in6_addr },
74044eef6aSKristof Provost 	{ .type = PF_ST_FILTER_MASK, .off = _OUT(mask), .cb = nlattr_get_in6_addr },
752cef6288SAlexander V. Chernikov };
762cef6288SAlexander V. Chernikov static const struct nlfield_parser nlf_p_generic[] = {
772cef6288SAlexander V. Chernikov 	{ .off_in = _IN(version), .off_out = _OUT(version), .cb = nlf_get_u8 },
782cef6288SAlexander V. Chernikov };
792cef6288SAlexander V. Chernikov #undef _IN
802cef6288SAlexander V. Chernikov #undef _OUT
812cef6288SAlexander V. Chernikov NL_DECLARE_PARSER(state_parser, struct genlmsghdr, nlf_p_generic, nla_p_state);
822cef6288SAlexander V. Chernikov 
832cef6288SAlexander V. Chernikov static void
842cef6288SAlexander V. Chernikov dump_addr(struct nl_writer *nw, int attr, const struct pf_addr *addr, int af)
852cef6288SAlexander V. Chernikov {
862cef6288SAlexander V. Chernikov 	switch (af) {
872cef6288SAlexander V. Chernikov 	case AF_INET:
882cef6288SAlexander V. Chernikov 		nlattr_add(nw, attr, 4, &addr->v4);
892cef6288SAlexander V. Chernikov 		break;
902cef6288SAlexander V. Chernikov 	case AF_INET6:
912cef6288SAlexander V. Chernikov 		nlattr_add(nw, attr, 16, &addr->v6);
922cef6288SAlexander V. Chernikov 		break;
932cef6288SAlexander V. Chernikov 	};
942cef6288SAlexander V. Chernikov }
952cef6288SAlexander V. Chernikov 
962cef6288SAlexander V. Chernikov static bool
972cef6288SAlexander V. Chernikov dump_state_peer(struct nl_writer *nw, int attr, const struct pf_state_peer *peer)
982cef6288SAlexander V. Chernikov {
992cef6288SAlexander V. Chernikov 	int off = nlattr_add_nested(nw, attr);
1002cef6288SAlexander V. Chernikov 	if (off == 0)
1012cef6288SAlexander V. Chernikov 		return (false);
1022cef6288SAlexander V. Chernikov 
1032cef6288SAlexander V. Chernikov 	nlattr_add_u32(nw, PF_STP_SEQLO, peer->seqlo);
1042cef6288SAlexander V. Chernikov 	nlattr_add_u32(nw, PF_STP_SEQHI, peer->seqhi);
1052cef6288SAlexander V. Chernikov 	nlattr_add_u32(nw, PF_STP_SEQDIFF, peer->seqdiff);
1062cef6288SAlexander V. Chernikov 	nlattr_add_u16(nw, PF_STP_MAX_WIN, peer->max_win);
1072cef6288SAlexander V. Chernikov 	nlattr_add_u16(nw, PF_STP_MSS, peer->mss);
1082cef6288SAlexander V. Chernikov 	nlattr_add_u8(nw, PF_STP_STATE, peer->state);
1092cef6288SAlexander V. Chernikov 	nlattr_add_u8(nw, PF_STP_WSCALE, peer->wscale);
1102cef6288SAlexander V. Chernikov 
1112cef6288SAlexander V. Chernikov 	if (peer->scrub != NULL) {
1122cef6288SAlexander V. Chernikov 		struct pf_state_scrub *sc = peer->scrub;
1132cef6288SAlexander V. Chernikov 		uint16_t pfss_flags = sc->pfss_flags & PFSS_TIMESTAMP;
1142cef6288SAlexander V. Chernikov 
1152cef6288SAlexander V. Chernikov 		nlattr_add_u16(nw, PF_STP_PFSS_FLAGS, pfss_flags);
1162cef6288SAlexander V. Chernikov 		nlattr_add_u32(nw, PF_STP_PFSS_TS_MOD, sc->pfss_ts_mod);
1172cef6288SAlexander V. Chernikov 		nlattr_add_u8(nw, PF_STP_PFSS_TTL, sc->pfss_ttl);
1182cef6288SAlexander V. Chernikov 		nlattr_add_u8(nw, PF_STP_SCRUB_FLAG, PFSYNC_SCRUB_FLAG_VALID);
1192cef6288SAlexander V. Chernikov 	}
1202cef6288SAlexander V. Chernikov 	nlattr_set_len(nw, off);
1212cef6288SAlexander V. Chernikov 
1222cef6288SAlexander V. Chernikov 	return (true);
1232cef6288SAlexander V. Chernikov }
1242cef6288SAlexander V. Chernikov 
1252cef6288SAlexander V. Chernikov static bool
1262cef6288SAlexander V. Chernikov dump_state_key(struct nl_writer *nw, int attr, const struct pf_state_key *key)
1272cef6288SAlexander V. Chernikov {
1282cef6288SAlexander V. Chernikov 	int off = nlattr_add_nested(nw, attr);
1292cef6288SAlexander V. Chernikov 	if (off == 0)
1302cef6288SAlexander V. Chernikov 		return (false);
1312cef6288SAlexander V. Chernikov 
1322cef6288SAlexander V. Chernikov 	dump_addr(nw, PF_STK_ADDR0, &key->addr[0], key->af);
1332cef6288SAlexander V. Chernikov 	dump_addr(nw, PF_STK_ADDR1, &key->addr[1], key->af);
1342cef6288SAlexander V. Chernikov 	nlattr_add_u16(nw, PF_STK_PORT0, key->port[0]);
1352cef6288SAlexander V. Chernikov 	nlattr_add_u16(nw, PF_STK_PORT1, key->port[1]);
1362cef6288SAlexander V. Chernikov 
1372cef6288SAlexander V. Chernikov 	nlattr_set_len(nw, off);
1382cef6288SAlexander V. Chernikov 
1392cef6288SAlexander V. Chernikov 	return (true);
1402cef6288SAlexander V. Chernikov }
1412cef6288SAlexander V. Chernikov 
1422cef6288SAlexander V. Chernikov static int
1432cef6288SAlexander V. Chernikov dump_state(struct nlpcb *nlp, const struct nlmsghdr *hdr, struct pf_kstate *s,
1442cef6288SAlexander V. Chernikov     struct nl_pstate *npt)
1452cef6288SAlexander V. Chernikov {
1462cef6288SAlexander V. Chernikov 	struct nl_writer *nw = npt->nw;
1472cef6288SAlexander V. Chernikov 	int error = 0;
1482cef6288SAlexander V. Chernikov 	int af;
1492cef6288SAlexander V. Chernikov 	struct pf_state_key *key;
1502cef6288SAlexander V. Chernikov 
151b1e0f0ffSKristof Provost 	PF_STATE_LOCK_ASSERT(s);
152b1e0f0ffSKristof Provost 
1532cef6288SAlexander V. Chernikov 	if (!nlmsg_reply(nw, hdr, sizeof(struct genlmsghdr)))
1542cef6288SAlexander V. Chernikov 		goto enomem;
1552cef6288SAlexander V. Chernikov 
1562cef6288SAlexander V. Chernikov 	struct genlmsghdr *ghdr_new = nlmsg_reserve_object(nw, struct genlmsghdr);
1572cef6288SAlexander V. Chernikov 	ghdr_new->cmd = PFNL_CMD_GETSTATES;
1582cef6288SAlexander V. Chernikov 	ghdr_new->version = 0;
1592cef6288SAlexander V. Chernikov 	ghdr_new->reserved = 0;
1602cef6288SAlexander V. Chernikov 
1612cef6288SAlexander V. Chernikov 	nlattr_add_u64(nw, PF_ST_VERSION, PF_STATE_VERSION);
1622cef6288SAlexander V. Chernikov 
1632cef6288SAlexander V. Chernikov 	key = s->key[PF_SK_WIRE];
1642cef6288SAlexander V. Chernikov 	if (!dump_state_key(nw, PF_ST_KEY_WIRE, key))
1652cef6288SAlexander V. Chernikov 		goto enomem;
1662cef6288SAlexander V. Chernikov 	key = s->key[PF_SK_STACK];
1672cef6288SAlexander V. Chernikov 	if (!dump_state_key(nw, PF_ST_KEY_STACK, key))
1682cef6288SAlexander V. Chernikov 		goto enomem;
1692cef6288SAlexander V. Chernikov 
1702cef6288SAlexander V. Chernikov 	af = s->key[PF_SK_WIRE]->af;
1712cef6288SAlexander V. Chernikov 	nlattr_add_u8(nw, PF_ST_PROTO, s->key[PF_SK_WIRE]->proto);
1722cef6288SAlexander V. Chernikov 	nlattr_add_u8(nw, PF_ST_AF, af);
1732cef6288SAlexander V. Chernikov 
1742cef6288SAlexander V. Chernikov 	nlattr_add_string(nw, PF_ST_IFNAME, s->kif->pfik_name);
1752cef6288SAlexander V. Chernikov 	nlattr_add_string(nw, PF_ST_ORIG_IFNAME, s->orig_kif->pfik_name);
1762cef6288SAlexander V. Chernikov 	dump_addr(nw, PF_ST_RT_ADDR, &s->rt_addr, af);
17704932601SKristof Provost 	nlattr_add_u32(nw, PF_ST_CREATION, time_uptime - (s->creation / 1000));
1782cef6288SAlexander V. Chernikov 	uint32_t expire = pf_state_expires(s);
1792cef6288SAlexander V. Chernikov 	if (expire > time_uptime)
1802cef6288SAlexander V. Chernikov 		expire = expire - time_uptime;
1812cef6288SAlexander V. Chernikov 	nlattr_add_u32(nw, PF_ST_EXPIRE, expire);
1822cef6288SAlexander V. Chernikov 	nlattr_add_u8(nw, PF_ST_DIRECTION, s->direction);
1832cef6288SAlexander V. Chernikov 	nlattr_add_u8(nw, PF_ST_LOG, s->act.log);
1842cef6288SAlexander V. Chernikov 	nlattr_add_u8(nw, PF_ST_TIMEOUT, s->timeout);
1852cef6288SAlexander V. Chernikov 	nlattr_add_u16(nw, PF_ST_STATE_FLAGS, s->state_flags);
1862cef6288SAlexander V. Chernikov 	uint8_t sync_flags = 0;
1872cef6288SAlexander V. Chernikov 	if (s->src_node)
1882cef6288SAlexander V. Chernikov 		sync_flags |= PFSYNC_FLAG_SRCNODE;
1892cef6288SAlexander V. Chernikov 	if (s->nat_src_node)
1902cef6288SAlexander V. Chernikov 		sync_flags |= PFSYNC_FLAG_NATSRCNODE;
1912cef6288SAlexander V. Chernikov 	nlattr_add_u8(nw, PF_ST_SYNC_FLAGS, sync_flags);
1922cef6288SAlexander V. Chernikov 	nlattr_add_u64(nw, PF_ST_ID, s->id);
1932cef6288SAlexander V. Chernikov 	nlattr_add_u32(nw, PF_ST_CREATORID, htonl(s->creatorid));
1942cef6288SAlexander V. Chernikov 
195e5c64b26SKajetan Staszkiewicz 	nlattr_add_u32(nw, PF_ST_RULE, s->rule ? s->rule->nr : -1);
196e5c64b26SKajetan Staszkiewicz 	nlattr_add_u32(nw, PF_ST_ANCHOR, s->anchor ? s->anchor->nr : -1);
197e5c64b26SKajetan Staszkiewicz 	nlattr_add_u32(nw, PF_ST_NAT_RULE, s->nat_rule ? s->nat_rule->nr : -1);
1982cef6288SAlexander V. Chernikov 
1992cef6288SAlexander V. Chernikov 	nlattr_add_u64(nw, PF_ST_PACKETS0, s->packets[0]);
2002cef6288SAlexander V. Chernikov 	nlattr_add_u64(nw, PF_ST_PACKETS1, s->packets[1]);
2012cef6288SAlexander V. Chernikov 	nlattr_add_u64(nw, PF_ST_BYTES0, s->bytes[0]);
2022cef6288SAlexander V. Chernikov 	nlattr_add_u64(nw, PF_ST_BYTES1, s->bytes[1]);
203881bf881SKristof Provost 	nlattr_add_u32(nw, PF_ST_RTABLEID, s->act.rtableid);
204881bf881SKristof Provost 	nlattr_add_u8(nw, PF_ST_MIN_TTL, s->act.min_ttl);
205881bf881SKristof Provost 	nlattr_add_u16(nw, PF_ST_MAX_MSS, s->act.max_mss);
206881bf881SKristof Provost 	nlattr_add_u16(nw, PF_ST_DNPIPE, s->act.dnpipe);
207881bf881SKristof Provost 	nlattr_add_u16(nw, PF_ST_DNRPIPE, s->act.dnrpipe);
208881bf881SKristof Provost 	nlattr_add_u8(nw, PF_ST_RT, s->rt);
209881bf881SKristof Provost 	if (s->rt_kif != NULL)
210881bf881SKristof Provost 		nlattr_add_string(nw, PF_ST_RT_IFNAME, s->rt_kif->pfik_name);
2112cef6288SAlexander V. Chernikov 
2122cef6288SAlexander V. Chernikov 	if (!dump_state_peer(nw, PF_ST_PEER_SRC, &s->src))
2132cef6288SAlexander V. Chernikov 		goto enomem;
2142cef6288SAlexander V. Chernikov 	if (!dump_state_peer(nw, PF_ST_PEER_DST, &s->dst))
2152cef6288SAlexander V. Chernikov 		goto enomem;
2162cef6288SAlexander V. Chernikov 
2172cef6288SAlexander V. Chernikov 	if (nlmsg_end(nw))
2182cef6288SAlexander V. Chernikov 		return (0);
2192cef6288SAlexander V. Chernikov 
2202cef6288SAlexander V. Chernikov enomem:
2212cef6288SAlexander V. Chernikov 	error = ENOMEM;
2222cef6288SAlexander V. Chernikov 	nlmsg_abort(nw);
2232cef6288SAlexander V. Chernikov 	return (error);
2242cef6288SAlexander V. Chernikov }
2252cef6288SAlexander V. Chernikov 
2262cef6288SAlexander V. Chernikov static int
2272cef6288SAlexander V. Chernikov handle_dumpstates(struct nlpcb *nlp, struct nl_parsed_state *attrs,
2282cef6288SAlexander V. Chernikov     struct nlmsghdr *hdr, struct nl_pstate *npt)
2292cef6288SAlexander V. Chernikov {
2302cef6288SAlexander V. Chernikov 	int error = 0;
2312cef6288SAlexander V. Chernikov 
2322cef6288SAlexander V. Chernikov 	hdr->nlmsg_flags |= NLM_F_MULTI;
2332cef6288SAlexander V. Chernikov 
234271f1469SKristof Provost 	for (int i = 0; i <= V_pf_hashmask; i++) {
2352cef6288SAlexander V. Chernikov 		struct pf_idhash *ih = &V_pf_idhash[i];
2362cef6288SAlexander V. Chernikov 		struct pf_kstate *s;
2372cef6288SAlexander V. Chernikov 
2382cef6288SAlexander V. Chernikov 		if (LIST_EMPTY(&ih->states))
2392cef6288SAlexander V. Chernikov 			continue;
2402cef6288SAlexander V. Chernikov 
2412cef6288SAlexander V. Chernikov 		PF_HASHROW_LOCK(ih);
2422cef6288SAlexander V. Chernikov 		LIST_FOREACH(s, &ih->states, entry) {
243044eef6aSKristof Provost 			sa_family_t af = s->key[PF_SK_WIRE]->af;
244044eef6aSKristof Provost 
245044eef6aSKristof Provost 			if (s->timeout == PFTM_UNLINKED)
246044eef6aSKristof Provost 				continue;
247044eef6aSKristof Provost 
248044eef6aSKristof Provost 			/* Filter */
249044eef6aSKristof Provost 			if (attrs->creatorid != 0 && s->creatorid != attrs->creatorid)
250044eef6aSKristof Provost 				continue;
251044eef6aSKristof Provost 			if (attrs->ifname[0] != 0 &&
252044eef6aSKristof Provost 			    strncmp(attrs->ifname, s->kif->pfik_name, IFNAMSIZ) != 0)
253044eef6aSKristof Provost 				continue;
254044eef6aSKristof Provost 			if (attrs->proto != 0 && s->key[PF_SK_WIRE]->proto != attrs->proto)
255044eef6aSKristof Provost 				continue;
256044eef6aSKristof Provost 			if (attrs->af != 0 && af != attrs->af)
257044eef6aSKristof Provost 				continue;
258044eef6aSKristof Provost 			if (pf_match_addr(1, &s->key[PF_SK_WIRE]->addr[0],
259044eef6aSKristof Provost 			    &attrs->mask, &attrs->addr, af) &&
260044eef6aSKristof Provost 			    pf_match_addr(1, &s->key[PF_SK_WIRE]->addr[1],
261044eef6aSKristof Provost 			    &attrs->mask, &attrs->addr, af) &&
262044eef6aSKristof Provost 			    pf_match_addr(1, &s->key[PF_SK_STACK]->addr[0],
263044eef6aSKristof Provost 			    &attrs->mask, &attrs->addr, af) &&
264044eef6aSKristof Provost 			    pf_match_addr(1, &s->key[PF_SK_STACK]->addr[1],
265044eef6aSKristof Provost 			    &attrs->mask, &attrs->addr, af))
266044eef6aSKristof Provost 				continue;
267044eef6aSKristof Provost 
2682cef6288SAlexander V. Chernikov 			error = dump_state(nlp, hdr, s, npt);
2692cef6288SAlexander V. Chernikov 			if (error != 0)
2702cef6288SAlexander V. Chernikov 				break;
2712cef6288SAlexander V. Chernikov 		}
2722cef6288SAlexander V. Chernikov 		PF_HASHROW_UNLOCK(ih);
2732cef6288SAlexander V. Chernikov 	}
2742cef6288SAlexander V. Chernikov 
2752cef6288SAlexander V. Chernikov 	if (!nlmsg_end_dump(npt->nw, error, hdr)) {
2762cef6288SAlexander V. Chernikov 		NL_LOG(LOG_DEBUG, "Unable to finalize the dump");
2772cef6288SAlexander V. Chernikov 		return (ENOMEM);
2782cef6288SAlexander V. Chernikov 	}
2792cef6288SAlexander V. Chernikov 
2802cef6288SAlexander V. Chernikov 	return (error);
2812cef6288SAlexander V. Chernikov }
2822cef6288SAlexander V. Chernikov 
2832cef6288SAlexander V. Chernikov static int
2842cef6288SAlexander V. Chernikov handle_getstate(struct nlpcb *nlp, struct nl_parsed_state *attrs,
2852cef6288SAlexander V. Chernikov     struct nlmsghdr *hdr, struct nl_pstate *npt)
2862cef6288SAlexander V. Chernikov {
287b1e0f0ffSKristof Provost 	struct pf_kstate *s;
288b1e0f0ffSKristof Provost 	int ret;
289b1e0f0ffSKristof Provost 
290b1e0f0ffSKristof Provost 	s = pf_find_state_byid(attrs->id, attrs->creatorid);
2912cef6288SAlexander V. Chernikov 	if (s == NULL)
2922cef6288SAlexander V. Chernikov 		return (ENOENT);
293b1e0f0ffSKristof Provost 	ret = dump_state(nlp, hdr, s, npt);
294b1e0f0ffSKristof Provost 	PF_STATE_UNLOCK(s);
295b1e0f0ffSKristof Provost 
296b1e0f0ffSKristof Provost 	return (ret);
2972cef6288SAlexander V. Chernikov }
2982cef6288SAlexander V. Chernikov 
2992cef6288SAlexander V. Chernikov static int
300a7191e5dSKristof Provost dump_creatorid(struct nlpcb *nlp, const struct nlmsghdr *hdr, uint32_t creator,
301a7191e5dSKristof Provost     struct nl_pstate *npt)
302a7191e5dSKristof Provost {
303a7191e5dSKristof Provost 	struct nl_writer *nw = npt->nw;
304a7191e5dSKristof Provost 
305a7191e5dSKristof Provost 	if (!nlmsg_reply(nw, hdr, sizeof(struct genlmsghdr)))
306a7191e5dSKristof Provost 		goto enomem;
307a7191e5dSKristof Provost 
308a7191e5dSKristof Provost 	struct genlmsghdr *ghdr_new = nlmsg_reserve_object(nw, struct genlmsghdr);
309a7191e5dSKristof Provost 	ghdr_new->cmd = PFNL_CMD_GETCREATORS;
310a7191e5dSKristof Provost 	ghdr_new->version = 0;
311a7191e5dSKristof Provost 	ghdr_new->reserved = 0;
312a7191e5dSKristof Provost 
313a7191e5dSKristof Provost 	nlattr_add_u32(nw, PF_ST_CREATORID, htonl(creator));
314a7191e5dSKristof Provost 
315a7191e5dSKristof Provost 	if (nlmsg_end(nw))
316a7191e5dSKristof Provost 		return (0);
317a7191e5dSKristof Provost 
318a7191e5dSKristof Provost enomem:
319a7191e5dSKristof Provost 	nlmsg_abort(nw);
320a7191e5dSKristof Provost 	return (ENOMEM);
321a7191e5dSKristof Provost }
322a7191e5dSKristof Provost 
323a7191e5dSKristof Provost static int
3242cef6288SAlexander V. Chernikov pf_handle_getstates(struct nlmsghdr *hdr, struct nl_pstate *npt)
3252cef6288SAlexander V. Chernikov {
3262cef6288SAlexander V. Chernikov 	int error;
3272cef6288SAlexander V. Chernikov 
3282cef6288SAlexander V. Chernikov 	struct nl_parsed_state attrs = {};
3292cef6288SAlexander V. Chernikov 	error = nl_parse_nlmsg(hdr, &state_parser, npt, &attrs);
3302cef6288SAlexander V. Chernikov 	if (error != 0)
3312cef6288SAlexander V. Chernikov 		return (error);
3322cef6288SAlexander V. Chernikov 
3332cef6288SAlexander V. Chernikov 	if (attrs.id != 0)
3342cef6288SAlexander V. Chernikov 		error = handle_getstate(npt->nlp, &attrs, hdr, npt);
3352cef6288SAlexander V. Chernikov 	else
3362cef6288SAlexander V. Chernikov 		error = handle_dumpstates(npt->nlp, &attrs, hdr, npt);
3372cef6288SAlexander V. Chernikov 
3382cef6288SAlexander V. Chernikov 	return (error);
3392cef6288SAlexander V. Chernikov }
3402cef6288SAlexander V. Chernikov 
341a7191e5dSKristof Provost static int
342a7191e5dSKristof Provost pf_handle_getcreators(struct nlmsghdr *hdr, struct nl_pstate *npt)
343a7191e5dSKristof Provost {
344a7191e5dSKristof Provost 	uint32_t creators[16];
345a7191e5dSKristof Provost 	int error = 0;
346a7191e5dSKristof Provost 
347a7191e5dSKristof Provost 	bzero(creators, sizeof(creators));
348a7191e5dSKristof Provost 
349271f1469SKristof Provost 	for (int i = 0; i < V_pf_hashmask; i++) {
350a7191e5dSKristof Provost 		struct pf_idhash *ih = &V_pf_idhash[i];
351a7191e5dSKristof Provost 		struct pf_kstate *s;
352a7191e5dSKristof Provost 
353a7191e5dSKristof Provost 		if (LIST_EMPTY(&ih->states))
354a7191e5dSKristof Provost 			continue;
355a7191e5dSKristof Provost 
356a7191e5dSKristof Provost 		PF_HASHROW_LOCK(ih);
357a7191e5dSKristof Provost 		LIST_FOREACH(s, &ih->states, entry) {
358a7191e5dSKristof Provost 			int j;
359a7191e5dSKristof Provost 			if (s->timeout == PFTM_UNLINKED)
360a7191e5dSKristof Provost 				continue;
361a7191e5dSKristof Provost 
362a7191e5dSKristof Provost 			for (j = 0; j < nitems(creators); j++) {
363a7191e5dSKristof Provost 				if (creators[j] == s->creatorid)
364a7191e5dSKristof Provost 					break;
365a7191e5dSKristof Provost 				if (creators[j] == 0) {
366a7191e5dSKristof Provost 					creators[j] = s->creatorid;
367a7191e5dSKristof Provost 					break;
368a7191e5dSKristof Provost 				}
369a7191e5dSKristof Provost 			}
370a7191e5dSKristof Provost 			if (j == nitems(creators))
371a7191e5dSKristof Provost 				printf("Warning: too many creators!\n");
372a7191e5dSKristof Provost 		}
373a7191e5dSKristof Provost 		PF_HASHROW_UNLOCK(ih);
374a7191e5dSKristof Provost 	}
375a7191e5dSKristof Provost 
376a7191e5dSKristof Provost 	hdr->nlmsg_flags |= NLM_F_MULTI;
377a7191e5dSKristof Provost 	for (int i = 0; i < nitems(creators); i++) {
378a7191e5dSKristof Provost 		if (creators[i] == 0)
379a7191e5dSKristof Provost 			break;
380a7191e5dSKristof Provost 		error = dump_creatorid(npt->nlp, hdr, creators[i], npt);
381a7191e5dSKristof Provost 	}
382a7191e5dSKristof Provost 
383a7191e5dSKristof Provost 	if (!nlmsg_end_dump(npt->nw, error, hdr)) {
384a7191e5dSKristof Provost 		NL_LOG(LOG_DEBUG, "Unable to finalize the dump");
385a7191e5dSKristof Provost 		return (ENOMEM);
386a7191e5dSKristof Provost 	}
387a7191e5dSKristof Provost 
388a7191e5dSKristof Provost 	return (error);
389a7191e5dSKristof Provost }
390a7191e5dSKristof Provost 
39181647eb6SKristof Provost static int
39281647eb6SKristof Provost pf_handle_start(struct nlmsghdr *hdr __unused, struct nl_pstate *npt __unused)
39381647eb6SKristof Provost {
39481647eb6SKristof Provost 	return (pf_start());
39581647eb6SKristof Provost }
39681647eb6SKristof Provost 
39781647eb6SKristof Provost static int
39881647eb6SKristof Provost pf_handle_stop(struct nlmsghdr *hdr __unused, struct nl_pstate *npt __unused)
39981647eb6SKristof Provost {
40081647eb6SKristof Provost 	return (pf_stop());
40181647eb6SKristof Provost }
40281647eb6SKristof Provost 
403ffbf2595SKristof Provost #define _OUT(_field)	offsetof(struct pf_addr_wrap, _field)
404ffbf2595SKristof Provost static const struct nlattr_parser nla_p_addr_wrap[] = {
405ffbf2595SKristof Provost 	{ .type = PF_AT_ADDR, .off = _OUT(v.a.addr), .cb = nlattr_get_in6_addr },
406ffbf2595SKristof Provost 	{ .type = PF_AT_MASK, .off = _OUT(v.a.mask), .cb = nlattr_get_in6_addr },
407ffbf2595SKristof Provost 	{ .type = PF_AT_IFNAME, .off = _OUT(v.ifname), .arg = (void *)IFNAMSIZ,.cb = nlattr_get_chara },
408ffbf2595SKristof Provost 	{ .type = PF_AT_TABLENAME, .off = _OUT(v.tblname), .arg = (void *)PF_TABLE_NAME_SIZE, .cb = nlattr_get_chara },
409ffbf2595SKristof Provost 	{ .type = PF_AT_TYPE, .off = _OUT(type), .cb = nlattr_get_uint8 },
410ffbf2595SKristof Provost 	{ .type = PF_AT_IFLAGS, .off = _OUT(iflags), .cb = nlattr_get_uint8 },
411ffbf2595SKristof Provost };
412ffbf2595SKristof Provost NL_DECLARE_ATTR_PARSER(addr_wrap_parser, nla_p_addr_wrap);
413ffbf2595SKristof Provost #undef _OUT
414ffbf2595SKristof Provost 
415777a4702SKristof Provost static bool
416777a4702SKristof Provost nlattr_add_addr_wrap(struct nl_writer *nw, int attrtype, struct pf_addr_wrap *a)
417777a4702SKristof Provost {
418777a4702SKristof Provost 	int off = nlattr_add_nested(nw, attrtype);
419777a4702SKristof Provost 
420777a4702SKristof Provost 	nlattr_add_in6_addr(nw, PF_AT_ADDR, &a->v.a.addr.v6);
421777a4702SKristof Provost 	nlattr_add_in6_addr(nw, PF_AT_MASK, &a->v.a.mask.v6);
422777a4702SKristof Provost 	nlattr_add_u8(nw, PF_AT_TYPE, a->type);
423777a4702SKristof Provost 	nlattr_add_u8(nw, PF_AT_IFLAGS, a->iflags);
424777a4702SKristof Provost 
425777a4702SKristof Provost 	if (a->type == PF_ADDR_DYNIFTL) {
426777a4702SKristof Provost 		nlattr_add_string(nw, PF_AT_IFNAME, a->v.ifname);
4271fc0dac5SKajetan Staszkiewicz 		nlattr_add_u32(nw, PF_AT_DYNCNT, a->p.dyncnt);
428777a4702SKristof Provost 	} else if (a->type == PF_ADDR_TABLE) {
429777a4702SKristof Provost 		nlattr_add_string(nw, PF_AT_TABLENAME, a->v.tblname);
4301fc0dac5SKajetan Staszkiewicz 		nlattr_add_u32(nw, PF_AT_TBLCNT, a->p.tblcnt);
431777a4702SKristof Provost 	}
432777a4702SKristof Provost 
433777a4702SKristof Provost 	nlattr_set_len(nw, off);
434777a4702SKristof Provost 
435777a4702SKristof Provost 	return (true);
436777a4702SKristof Provost }
437777a4702SKristof Provost 
438ffbf2595SKristof Provost #define _OUT(_field)	offsetof(struct pf_rule_addr, _field)
439ffbf2595SKristof Provost static const struct nlattr_parser nla_p_ruleaddr[] = {
440ffbf2595SKristof Provost 	{ .type = PF_RAT_ADDR, .off = _OUT(addr), .arg = &addr_wrap_parser, .cb = nlattr_get_nested },
441ffbf2595SKristof Provost 	{ .type = PF_RAT_SRC_PORT, .off = _OUT(port[0]), .cb = nlattr_get_uint16 },
442ffbf2595SKristof Provost 	{ .type = PF_RAT_DST_PORT, .off = _OUT(port[1]), .cb = nlattr_get_uint16 },
443ffbf2595SKristof Provost 	{ .type = PF_RAT_NEG, .off = _OUT(neg), .cb = nlattr_get_uint8 },
444ffbf2595SKristof Provost 	{ .type = PF_RAT_OP, .off = _OUT(port_op), .cb = nlattr_get_uint8 },
445ffbf2595SKristof Provost };
446ffbf2595SKristof Provost NL_DECLARE_ATTR_PARSER(rule_addr_parser, nla_p_ruleaddr);
447ffbf2595SKristof Provost #undef _OUT
448ffbf2595SKristof Provost 
449777a4702SKristof Provost static bool
450777a4702SKristof Provost nlattr_add_rule_addr(struct nl_writer *nw, int attrtype, struct pf_rule_addr *r)
451777a4702SKristof Provost {
4521fc0dac5SKajetan Staszkiewicz 	struct pf_addr_wrap aw = {0};
453777a4702SKristof Provost 	int off = nlattr_add_nested(nw, attrtype);
454777a4702SKristof Provost 
4551fc0dac5SKajetan Staszkiewicz 	bcopy(&(r->addr), &aw, sizeof(struct pf_addr_wrap));
4561fc0dac5SKajetan Staszkiewicz 	pf_addr_copyout(&aw);
4571fc0dac5SKajetan Staszkiewicz 
4581fc0dac5SKajetan Staszkiewicz 	nlattr_add_addr_wrap(nw, PF_RAT_ADDR, &aw);
459777a4702SKristof Provost 	nlattr_add_u16(nw, PF_RAT_SRC_PORT, r->port[0]);
460777a4702SKristof Provost 	nlattr_add_u16(nw, PF_RAT_DST_PORT, r->port[1]);
461777a4702SKristof Provost 	nlattr_add_u8(nw, PF_RAT_NEG, r->neg);
462777a4702SKristof Provost 	nlattr_add_u8(nw, PF_RAT_OP, r->port_op);
463777a4702SKristof Provost 
464777a4702SKristof Provost 	nlattr_set_len(nw, off);
465777a4702SKristof Provost 
466777a4702SKristof Provost 	return (true);
467777a4702SKristof Provost }
468777a4702SKristof Provost 
469ffbf2595SKristof Provost #define _OUT(_field)	offsetof(struct pf_mape_portset, _field)
470ffbf2595SKristof Provost static const struct nlattr_parser nla_p_mape_portset[] = {
471ffbf2595SKristof Provost 	{ .type = PF_MET_OFFSET, .off = _OUT(offset), .cb = nlattr_get_uint8 },
472ffbf2595SKristof Provost 	{ .type = PF_MET_PSID_LEN, .off = _OUT(psidlen), .cb = nlattr_get_uint8 },
473ffbf2595SKristof Provost 	{. type = PF_MET_PSID, .off = _OUT(psid), .cb = nlattr_get_uint16 },
474ffbf2595SKristof Provost };
475ffbf2595SKristof Provost NL_DECLARE_ATTR_PARSER(mape_portset_parser, nla_p_mape_portset);
476ffbf2595SKristof Provost #undef _OUT
477ffbf2595SKristof Provost 
478777a4702SKristof Provost static bool
479777a4702SKristof Provost nlattr_add_mape_portset(struct nl_writer *nw, int attrtype, const struct pf_mape_portset *m)
480777a4702SKristof Provost {
481777a4702SKristof Provost 	int off = nlattr_add_nested(nw, attrtype);
482777a4702SKristof Provost 
483777a4702SKristof Provost 	nlattr_add_u8(nw, PF_MET_OFFSET, m->offset);
484777a4702SKristof Provost 	nlattr_add_u8(nw, PF_MET_PSID_LEN, m->psidlen);
485777a4702SKristof Provost 	nlattr_add_u16(nw, PF_MET_PSID, m->psid);
486777a4702SKristof Provost 
487777a4702SKristof Provost 	nlattr_set_len(nw, off);
488777a4702SKristof Provost 
489777a4702SKristof Provost 	return (true);
490777a4702SKristof Provost }
491777a4702SKristof Provost 
492ffbf2595SKristof Provost struct nl_parsed_labels
493ffbf2595SKristof Provost {
494ffbf2595SKristof Provost 	char		labels[PF_RULE_MAX_LABEL_COUNT][PF_RULE_LABEL_SIZE];
495ffbf2595SKristof Provost 	uint32_t	i;
496ffbf2595SKristof Provost };
497ffbf2595SKristof Provost 
498ffbf2595SKristof Provost static int
499ffbf2595SKristof Provost nlattr_get_pf_rule_labels(struct nlattr *nla, struct nl_pstate *npt,
500ffbf2595SKristof Provost     const void *arg, void *target)
501ffbf2595SKristof Provost {
502ffbf2595SKristof Provost 	struct nl_parsed_labels *l = (struct nl_parsed_labels *)target;
503ffbf2595SKristof Provost 	int ret;
504ffbf2595SKristof Provost 
505ffbf2595SKristof Provost 	if (l->i >= PF_RULE_MAX_LABEL_COUNT)
506ffbf2595SKristof Provost 		return (E2BIG);
507ffbf2595SKristof Provost 
508ffbf2595SKristof Provost 	ret = nlattr_get_chara(nla, npt, (void *)PF_RULE_LABEL_SIZE,
509ffbf2595SKristof Provost 	    l->labels[l->i]);
510ffbf2595SKristof Provost 	if (ret == 0)
511ffbf2595SKristof Provost 		l->i++;
512ffbf2595SKristof Provost 
513ffbf2595SKristof Provost 	return (ret);
514ffbf2595SKristof Provost }
515ffbf2595SKristof Provost 
516ffbf2595SKristof Provost #define _OUT(_field)	offsetof(struct nl_parsed_labels, _field)
517ffbf2595SKristof Provost static const struct nlattr_parser nla_p_labels[] = {
518ffbf2595SKristof Provost 	{ .type = PF_LT_LABEL, .off = 0, .cb = nlattr_get_pf_rule_labels },
519ffbf2595SKristof Provost };
520ffbf2595SKristof Provost NL_DECLARE_ATTR_PARSER(rule_labels_parser, nla_p_labels);
521ffbf2595SKristof Provost #undef _OUT
522ffbf2595SKristof Provost 
523ffbf2595SKristof Provost static int
524ffbf2595SKristof Provost nlattr_get_nested_pf_rule_labels(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
525ffbf2595SKristof Provost {
526ffbf2595SKristof Provost 	struct nl_parsed_labels parsed_labels = { };
527ffbf2595SKristof Provost 	int error;
528ffbf2595SKristof Provost 
529ffbf2595SKristof Provost 	/* Assumes target points to the beginning of the structure */
530ffbf2595SKristof Provost 	error = nl_parse_header(NLA_DATA(nla), NLA_DATA_LEN(nla), &rule_labels_parser, npt, &parsed_labels);
531ffbf2595SKristof Provost 	if (error != 0)
532ffbf2595SKristof Provost 		return (error);
533ffbf2595SKristof Provost 
53484ff9e91SKristof Provost 	memcpy(target, parsed_labels.labels, sizeof(parsed_labels.labels));
535ffbf2595SKristof Provost 
536ffbf2595SKristof Provost 	return (0);
537ffbf2595SKristof Provost }
538ffbf2595SKristof Provost 
539777a4702SKristof Provost static bool
540777a4702SKristof Provost nlattr_add_labels(struct nl_writer *nw, int attrtype, const struct pf_krule *r)
541777a4702SKristof Provost {
542777a4702SKristof Provost 	int off = nlattr_add_nested(nw, attrtype);
543777a4702SKristof Provost 	int i = 0;
544777a4702SKristof Provost 
545777a4702SKristof Provost 	while (r->label[i][0] != 0
546777a4702SKristof Provost 	    && i < PF_RULE_MAX_LABEL_COUNT) {
547777a4702SKristof Provost 		nlattr_add_string(nw, PF_LT_LABEL, r->label[i]);
548777a4702SKristof Provost 		i++;
549777a4702SKristof Provost 	}
550777a4702SKristof Provost 
551777a4702SKristof Provost 	nlattr_set_len(nw, off);
552777a4702SKristof Provost 
553777a4702SKristof Provost 	return (true);
554777a4702SKristof Provost }
555777a4702SKristof Provost 
556ffbf2595SKristof Provost #define _OUT(_field)	offsetof(struct pf_kpool, _field)
557ffbf2595SKristof Provost static const struct nlattr_parser nla_p_pool[] = {
558ffbf2595SKristof Provost 	{ .type = PF_PT_KEY, .off = _OUT(key), .arg = (void *)sizeof(struct pf_poolhashkey), .cb = nlattr_get_bytes },
559ffbf2595SKristof Provost 	{ .type = PF_PT_COUNTER, .off = _OUT(counter), .cb = nlattr_get_in6_addr },
560ffbf2595SKristof Provost 	{ .type = PF_PT_TBLIDX, .off = _OUT(tblidx), .cb = nlattr_get_uint32 },
561ffbf2595SKristof Provost 	{ .type = PF_PT_PROXY_SRC_PORT, .off = _OUT(proxy_port[0]), .cb = nlattr_get_uint16 },
562ffbf2595SKristof Provost 	{ .type = PF_PT_PROXY_DST_PORT, .off = _OUT(proxy_port[1]), .cb = nlattr_get_uint16 },
563ffbf2595SKristof Provost 	{ .type = PF_PT_OPTS, .off = _OUT(opts), .cb = nlattr_get_uint8 },
564ffbf2595SKristof Provost 	{ .type = PF_PT_MAPE, .off = _OUT(mape), .arg = &mape_portset_parser, .cb = nlattr_get_nested },
565ffbf2595SKristof Provost };
566ffbf2595SKristof Provost NL_DECLARE_ATTR_PARSER(pool_parser, nla_p_pool);
567ffbf2595SKristof Provost #undef _OUT
568ffbf2595SKristof Provost 
569777a4702SKristof Provost static bool
570777a4702SKristof Provost nlattr_add_pool(struct nl_writer *nw, int attrtype, const struct pf_kpool *pool)
571777a4702SKristof Provost {
572777a4702SKristof Provost 	int off = nlattr_add_nested(nw, attrtype);
573777a4702SKristof Provost 
574777a4702SKristof Provost 	nlattr_add(nw, PF_PT_KEY, sizeof(struct pf_poolhashkey), &pool->key);
575777a4702SKristof Provost 	nlattr_add_in6_addr(nw, PF_PT_COUNTER, (const struct in6_addr *)&pool->counter);
576777a4702SKristof Provost 	nlattr_add_u32(nw, PF_PT_TBLIDX, pool->tblidx);
577777a4702SKristof Provost 	nlattr_add_u16(nw, PF_PT_PROXY_SRC_PORT, pool->proxy_port[0]);
578777a4702SKristof Provost 	nlattr_add_u16(nw, PF_PT_PROXY_DST_PORT, pool->proxy_port[1]);
579777a4702SKristof Provost 	nlattr_add_u8(nw, PF_PT_OPTS, pool->opts);
580777a4702SKristof Provost 	nlattr_add_mape_portset(nw, PF_PT_MAPE, &pool->mape);
581777a4702SKristof Provost 
582777a4702SKristof Provost 	nlattr_set_len(nw, off);
583777a4702SKristof Provost 
584777a4702SKristof Provost 	return (true);
585777a4702SKristof Provost }
586777a4702SKristof Provost 
587ffbf2595SKristof Provost #define _OUT(_field)	offsetof(struct pf_rule_uid, _field)
588ffbf2595SKristof Provost static const struct nlattr_parser nla_p_rule_uid[] = {
589ffbf2595SKristof Provost 	{ .type = PF_RUT_UID_LOW, .off = _OUT(uid[0]), .cb = nlattr_get_uint32 },
590ffbf2595SKristof Provost 	{ .type = PF_RUT_UID_HIGH, .off = _OUT(uid[1]), .cb = nlattr_get_uint32 },
591ffbf2595SKristof Provost 	{ .type = PF_RUT_OP, .off = _OUT(op), .cb = nlattr_get_uint8 },
592ffbf2595SKristof Provost };
593ffbf2595SKristof Provost NL_DECLARE_ATTR_PARSER(rule_uid_parser, nla_p_rule_uid);
594ffbf2595SKristof Provost #undef _OUT
595ffbf2595SKristof Provost 
596777a4702SKristof Provost static bool
597777a4702SKristof Provost nlattr_add_rule_uid(struct nl_writer *nw, int attrtype, const struct pf_rule_uid *u)
598777a4702SKristof Provost {
599777a4702SKristof Provost 	int off = nlattr_add_nested(nw, attrtype);
600777a4702SKristof Provost 
601777a4702SKristof Provost 	nlattr_add_u32(nw, PF_RUT_UID_LOW, u->uid[0]);
602777a4702SKristof Provost 	nlattr_add_u32(nw, PF_RUT_UID_HIGH, u->uid[1]);
603777a4702SKristof Provost 	nlattr_add_u8(nw, PF_RUT_OP, u->op);
604777a4702SKristof Provost 
605777a4702SKristof Provost 	nlattr_set_len(nw, off);
606777a4702SKristof Provost 
607777a4702SKristof Provost 	return (true);
608777a4702SKristof Provost }
609777a4702SKristof Provost 
610ffbf2595SKristof Provost struct nl_parsed_timeouts
611ffbf2595SKristof Provost {
612ffbf2595SKristof Provost 	uint32_t	timeouts[PFTM_MAX];
613ffbf2595SKristof Provost 	uint32_t	i;
614ffbf2595SKristof Provost };
615ffbf2595SKristof Provost 
616ffbf2595SKristof Provost static int
617ffbf2595SKristof Provost nlattr_get_pf_timeout(struct nlattr *nla, struct nl_pstate *npt,
618ffbf2595SKristof Provost     const void *arg, void *target)
619ffbf2595SKristof Provost {
620ffbf2595SKristof Provost 	struct nl_parsed_timeouts *t = (struct nl_parsed_timeouts *)target;
621ffbf2595SKristof Provost 	int ret;
622ffbf2595SKristof Provost 
623ffbf2595SKristof Provost 	if (t->i >= PFTM_MAX)
624ffbf2595SKristof Provost 		return (E2BIG);
625ffbf2595SKristof Provost 
626ffbf2595SKristof Provost 	ret = nlattr_get_uint32(nla, npt, NULL, &t->timeouts[t->i]);
627ffbf2595SKristof Provost 	if (ret == 0)
628ffbf2595SKristof Provost 		t->i++;
629ffbf2595SKristof Provost 
630ffbf2595SKristof Provost 	return (ret);
631ffbf2595SKristof Provost }
632ffbf2595SKristof Provost 
633ffbf2595SKristof Provost #define _OUT(_field)	offsetof(struct nl_parsed_timeout, _field)
634ffbf2595SKristof Provost static const struct nlattr_parser nla_p_timeouts[] = {
635ffbf2595SKristof Provost 	{ .type = PF_TT_TIMEOUT, .off = 0, .cb = nlattr_get_pf_timeout },
636ffbf2595SKristof Provost };
637ffbf2595SKristof Provost NL_DECLARE_ATTR_PARSER(timeout_parser, nla_p_timeouts);
638ffbf2595SKristof Provost #undef _OUT
639ffbf2595SKristof Provost 
640ffbf2595SKristof Provost static int
641ffbf2595SKristof Provost nlattr_get_nested_timeouts(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
642ffbf2595SKristof Provost {
643ffbf2595SKristof Provost 	struct nl_parsed_timeouts parsed_timeouts = { };
644ffbf2595SKristof Provost 	int error;
645ffbf2595SKristof Provost 
646ffbf2595SKristof Provost 	/* Assumes target points to the beginning of the structure */
647ffbf2595SKristof Provost 	error = nl_parse_header(NLA_DATA(nla), NLA_DATA_LEN(nla), &timeout_parser, npt, &parsed_timeouts);
648ffbf2595SKristof Provost 	if (error != 0)
649ffbf2595SKristof Provost 		return (error);
650ffbf2595SKristof Provost 
651ffbf2595SKristof Provost 	memcpy(target, parsed_timeouts.timeouts, sizeof(parsed_timeouts.timeouts));
652ffbf2595SKristof Provost 
653ffbf2595SKristof Provost 	return (0);
654ffbf2595SKristof Provost }
655ffbf2595SKristof Provost 
656777a4702SKristof Provost static bool
657777a4702SKristof Provost nlattr_add_timeout(struct nl_writer *nw, int attrtype, uint32_t *timeout)
658777a4702SKristof Provost {
659777a4702SKristof Provost 	int off = nlattr_add_nested(nw, attrtype);
660777a4702SKristof Provost 
661777a4702SKristof Provost 	for (int i = 0; i < PFTM_MAX; i++)
662777a4702SKristof Provost 		nlattr_add_u32(nw, PF_RT_TIMEOUT, timeout[i]);
663777a4702SKristof Provost 
664777a4702SKristof Provost 	nlattr_set_len(nw, off);
665777a4702SKristof Provost 
666777a4702SKristof Provost 	return (true);
667777a4702SKristof Provost }
668777a4702SKristof Provost 
669ffbf2595SKristof Provost #define _OUT(_field)	offsetof(struct pf_krule, _field)
670ffbf2595SKristof Provost static const struct nlattr_parser nla_p_rule[] = {
671ffbf2595SKristof Provost 	{ .type = PF_RT_SRC, .off = _OUT(src), .arg = &rule_addr_parser,.cb = nlattr_get_nested },
672ffbf2595SKristof Provost 	{ .type = PF_RT_DST, .off = _OUT(dst), .arg = &rule_addr_parser,.cb = nlattr_get_nested },
673ffbf2595SKristof Provost 	{ .type = PF_RT_RIDENTIFIER, .off = _OUT(ridentifier), .cb = nlattr_get_uint32 },
674ffbf2595SKristof Provost 	{ .type = PF_RT_LABELS, .off = _OUT(label), .arg = &rule_labels_parser,.cb = nlattr_get_nested_pf_rule_labels },
675ffbf2595SKristof Provost 	{ .type = PF_RT_IFNAME, .off = _OUT(ifname), .arg = (void *)IFNAMSIZ, .cb = nlattr_get_chara },
676ffbf2595SKristof Provost 	{ .type = PF_RT_QNAME, .off = _OUT(qname), .arg = (void *)PF_QNAME_SIZE, .cb = nlattr_get_chara },
677ffbf2595SKristof Provost 	{ .type = PF_RT_PQNAME, .off = _OUT(pqname), .arg = (void *)PF_QNAME_SIZE, .cb = nlattr_get_chara },
678ffbf2595SKristof Provost 	{ .type = PF_RT_TAGNAME, .off = _OUT(tagname), .arg = (void *)PF_TAG_NAME_SIZE, .cb = nlattr_get_chara },
679ffbf2595SKristof Provost 	{ .type = PF_RT_MATCH_TAGNAME, .off = _OUT(match_tagname), .arg = (void *)PF_TAG_NAME_SIZE, .cb = nlattr_get_chara },
680ffbf2595SKristof Provost 	{ .type = PF_RT_OVERLOAD_TBLNAME, .off = _OUT(overload_tblname), .arg = (void *)PF_TABLE_NAME_SIZE, .cb = nlattr_get_chara },
681ffbf2595SKristof Provost 	{ .type = PF_RT_RPOOL, .off = _OUT(rpool), .arg = &pool_parser, .cb = nlattr_get_nested },
682ffbf2595SKristof Provost 	{ .type = PF_RT_OS_FINGERPRINT, .off = _OUT(os_fingerprint), .cb = nlattr_get_uint32 },
683ffbf2595SKristof Provost 	{ .type = PF_RT_RTABLEID, .off = _OUT(rtableid), .cb = nlattr_get_uint32 },
684ffbf2595SKristof Provost 	{ .type = PF_RT_TIMEOUT, .off = _OUT(timeout), .arg = &timeout_parser, .cb = nlattr_get_nested_timeouts },
685ffbf2595SKristof Provost 	{ .type = PF_RT_MAX_STATES, .off = _OUT(max_states), .cb = nlattr_get_uint32 },
686ffbf2595SKristof Provost 	{ .type = PF_RT_MAX_SRC_NODES, .off = _OUT(max_src_nodes), .cb = nlattr_get_uint32 },
687ffbf2595SKristof Provost 	{ .type = PF_RT_MAX_SRC_STATES, .off = _OUT(max_src_states), .cb = nlattr_get_uint32 },
688ffbf2595SKristof Provost 	{ .type = PF_RT_MAX_SRC_CONN_RATE_LIMIT, .off = _OUT(max_src_conn_rate.limit), .cb = nlattr_get_uint32 },
689ffbf2595SKristof Provost 	{ .type = PF_RT_MAX_SRC_CONN_RATE_SECS, .off = _OUT(max_src_conn_rate.seconds), .cb = nlattr_get_uint32 },
690ffbf2595SKristof Provost 	{ .type = PF_RT_DNPIPE, .off = _OUT(dnpipe), .cb = nlattr_get_uint16 },
691ffbf2595SKristof Provost 	{ .type = PF_RT_DNRPIPE, .off = _OUT(dnrpipe), .cb = nlattr_get_uint16 },
692ffbf2595SKristof Provost 	{ .type = PF_RT_DNFLAGS, .off = _OUT(free_flags), .cb = nlattr_get_uint32 },
693ffbf2595SKristof Provost 	{ .type = PF_RT_NR, .off = _OUT(nr), .cb = nlattr_get_uint32 },
694ffbf2595SKristof Provost 	{ .type = PF_RT_PROB, .off = _OUT(prob), .cb = nlattr_get_uint32 },
695ffbf2595SKristof Provost 	{ .type = PF_RT_CUID, .off = _OUT(cuid), .cb = nlattr_get_uint32 },
696ffbf2595SKristof Provost 	{. type = PF_RT_CPID, .off = _OUT(cpid), .cb = nlattr_get_uint32 },
697ffbf2595SKristof Provost 	{ .type = PF_RT_RETURN_ICMP, .off = _OUT(return_icmp), .cb = nlattr_get_uint16 },
698ffbf2595SKristof Provost 	{ .type = PF_RT_RETURN_ICMP6, .off = _OUT(return_icmp6), .cb = nlattr_get_uint16 },
699ffbf2595SKristof Provost 	{ .type = PF_RT_MAX_MSS, .off = _OUT(max_mss), .cb = nlattr_get_uint16 },
700ffbf2595SKristof Provost 	{ .type = PF_RT_SCRUB_FLAGS, .off = _OUT(scrub_flags), .cb = nlattr_get_uint16 },
701ffbf2595SKristof Provost 	{ .type = PF_RT_UID, .off = _OUT(uid), .arg = &rule_uid_parser, .cb = nlattr_get_nested },
702ffbf2595SKristof Provost 	{ .type = PF_RT_GID, .off = _OUT(gid), .arg = &rule_uid_parser, .cb = nlattr_get_nested },
703ffbf2595SKristof Provost 	{ .type = PF_RT_RULE_FLAG, .off = _OUT(rule_flag), .cb = nlattr_get_uint32 },
704ffbf2595SKristof Provost 	{ .type = PF_RT_ACTION, .off = _OUT(action), .cb = nlattr_get_uint8 },
705ffbf2595SKristof Provost 	{ .type = PF_RT_DIRECTION, .off = _OUT(direction), .cb = nlattr_get_uint8 },
706ffbf2595SKristof Provost 	{ .type = PF_RT_LOG, .off = _OUT(log), .cb = nlattr_get_uint8 },
707ffbf2595SKristof Provost 	{ .type = PF_RT_LOGIF, .off = _OUT(logif), .cb = nlattr_get_uint8 },
708ffbf2595SKristof Provost 	{ .type = PF_RT_QUICK, .off = _OUT(quick), .cb = nlattr_get_uint8 },
709ffbf2595SKristof Provost 	{ .type = PF_RT_IF_NOT, .off = _OUT(ifnot), .cb = nlattr_get_uint8 },
710ffbf2595SKristof Provost 	{ .type = PF_RT_MATCH_TAG_NOT, .off = _OUT(match_tag_not), .cb = nlattr_get_uint8 },
711ffbf2595SKristof Provost 	{ .type = PF_RT_NATPASS, .off = _OUT(natpass), .cb = nlattr_get_uint8 },
712ffbf2595SKristof Provost 	{ .type = PF_RT_KEEP_STATE, .off = _OUT(keep_state), .cb = nlattr_get_uint8 },
713ffbf2595SKristof Provost 	{ .type = PF_RT_AF, .off = _OUT(af), .cb = nlattr_get_uint8 },
714ffbf2595SKristof Provost 	{ .type = PF_RT_PROTO, .off = _OUT(proto), .cb = nlattr_get_uint8 },
715ffbf2595SKristof Provost 	{ .type = PF_RT_TYPE, .off = _OUT(type), .cb = nlattr_get_uint8 },
716ffbf2595SKristof Provost 	{ .type = PF_RT_CODE, .off = _OUT(code), .cb = nlattr_get_uint8 },
717ffbf2595SKristof Provost 	{ .type = PF_RT_FLAGS, .off = _OUT(flags), .cb = nlattr_get_uint8 },
718ffbf2595SKristof Provost 	{ .type = PF_RT_FLAGSET, .off = _OUT(flagset), .cb = nlattr_get_uint8 },
719ffbf2595SKristof Provost 	{ .type = PF_RT_MIN_TTL, .off = _OUT(min_ttl), .cb = nlattr_get_uint8 },
720ffbf2595SKristof Provost 	{ .type = PF_RT_ALLOW_OPTS, .off = _OUT(allow_opts), .cb = nlattr_get_uint8 },
721ffbf2595SKristof Provost 	{ .type = PF_RT_RT, .off = _OUT(rt), .cb = nlattr_get_uint8 },
722ffbf2595SKristof Provost 	{ .type = PF_RT_RETURN_TTL, .off = _OUT(return_ttl), .cb = nlattr_get_uint8 },
723ffbf2595SKristof Provost 	{ .type = PF_RT_TOS, .off = _OUT(tos), .cb = nlattr_get_uint8 },
724ffbf2595SKristof Provost 	{ .type = PF_RT_SET_TOS, .off = _OUT(set_tos), .cb = nlattr_get_uint8 },
725ffbf2595SKristof Provost 	{ .type = PF_RT_ANCHOR_RELATIVE, .off = _OUT(anchor_relative), .cb = nlattr_get_uint8 },
726ffbf2595SKristof Provost 	{ .type = PF_RT_ANCHOR_WILDCARD, .off = _OUT(anchor_wildcard), .cb = nlattr_get_uint8 },
727ffbf2595SKristof Provost 	{ .type = PF_RT_FLUSH, .off = _OUT(flush), .cb = nlattr_get_uint8 },
728ffbf2595SKristof Provost 	{ .type = PF_RT_PRIO, .off = _OUT(prio), .cb = nlattr_get_uint8 },
729ffbf2595SKristof Provost 	{ .type = PF_RT_SET_PRIO, .off = _OUT(set_prio[0]), .cb = nlattr_get_uint8 },
730ffbf2595SKristof Provost 	{ .type = PF_RT_SET_PRIO_REPLY, .off = _OUT(set_prio[1]), .cb = nlattr_get_uint8 },
731ffbf2595SKristof Provost 	{ .type = PF_RT_DIVERT_ADDRESS, .off = _OUT(divert.addr), .cb = nlattr_get_in6_addr },
732ffbf2595SKristof Provost 	{ .type = PF_RT_DIVERT_PORT, .off = _OUT(divert.port), .cb = nlattr_get_uint16 },
7332339ead6SKristof Provost 	{ .type = PF_RT_RCV_IFNAME, .off = _OUT(rcv_ifname), .arg = (void *)IFNAMSIZ, .cb = nlattr_get_chara },
7347fe42038SKajetan Staszkiewicz 	{ .type = PF_RT_MAX_SRC_CONN, .off = _OUT(max_src_conn), .cb = nlattr_get_uint32 },
735ffbf2595SKristof Provost };
736ffbf2595SKristof Provost NL_DECLARE_ATTR_PARSER(rule_parser, nla_p_rule);
737ffbf2595SKristof Provost #undef _OUT
738ffbf2595SKristof Provost struct nl_parsed_addrule {
739ffbf2595SKristof Provost 	struct pf_krule	*rule;
740ffbf2595SKristof Provost 	uint32_t	 ticket;
741ffbf2595SKristof Provost 	uint32_t	 pool_ticket;
742ffbf2595SKristof Provost 	char		*anchor;
743ffbf2595SKristof Provost 	char		*anchor_call;
744ffbf2595SKristof Provost };
745ffbf2595SKristof Provost #define	_IN(_field)	offsetof(struct genlmsghdr, _field)
746ffbf2595SKristof Provost #define	_OUT(_field)	offsetof(struct nl_parsed_addrule, _field)
747ffbf2595SKristof Provost static const struct nlattr_parser nla_p_addrule[] = {
748ffbf2595SKristof Provost 	{ .type = PF_ART_TICKET, .off = _OUT(ticket), .cb = nlattr_get_uint32 },
749ffbf2595SKristof Provost 	{ .type = PF_ART_POOL_TICKET, .off = _OUT(pool_ticket), .cb = nlattr_get_uint32 },
750ffbf2595SKristof Provost 	{ .type = PF_ART_ANCHOR, .off = _OUT(anchor), .cb = nlattr_get_string },
751ffbf2595SKristof Provost 	{ .type = PF_ART_ANCHOR_CALL, .off = _OUT(anchor_call), .cb = nlattr_get_string },
752ffbf2595SKristof Provost 	{ .type = PF_ART_RULE, .off = _OUT(rule), .arg = &rule_parser, .cb = nlattr_get_nested_ptr }
753ffbf2595SKristof Provost };
754ffbf2595SKristof Provost static const struct nlfield_parser nlf_p_addrule[] = {
755ffbf2595SKristof Provost };
756ffbf2595SKristof Provost #undef _IN
757ffbf2595SKristof Provost #undef _OUT
758ffbf2595SKristof Provost NL_DECLARE_PARSER(addrule_parser, struct genlmsghdr, nlf_p_addrule, nla_p_addrule);
759ffbf2595SKristof Provost 
760ffbf2595SKristof Provost static int
761ffbf2595SKristof Provost pf_handle_addrule(struct nlmsghdr *hdr, struct nl_pstate *npt)
762ffbf2595SKristof Provost {
763ffbf2595SKristof Provost 	int error;
764ffbf2595SKristof Provost 	struct nl_parsed_addrule attrs = {};
765ffbf2595SKristof Provost 
766ffbf2595SKristof Provost 	attrs.rule = pf_krule_alloc();
767ffbf2595SKristof Provost 
768ffbf2595SKristof Provost 	error = nl_parse_nlmsg(hdr, &addrule_parser, npt, &attrs);
769e249f5daSKristof Provost 	if (error != 0) {
770e249f5daSKristof Provost 		pf_free_rule(attrs.rule);
771ffbf2595SKristof Provost 		return (error);
772e249f5daSKristof Provost 	}
773ffbf2595SKristof Provost 
774ffbf2595SKristof Provost 	error = pf_ioctl_addrule(attrs.rule, attrs.ticket, attrs.pool_ticket,
775ffbf2595SKristof Provost 	    attrs.anchor, attrs.anchor_call, nlp_get_cred(npt->nlp)->cr_uid,
776ffbf2595SKristof Provost 	    hdr->nlmsg_pid);
777ffbf2595SKristof Provost 
778ffbf2595SKristof Provost 	return (error);
779ffbf2595SKristof Provost }
780ffbf2595SKristof Provost 
78144f323ecSKristof Provost #define	_IN(_field)	offsetof(struct genlmsghdr, _field)
78244f323ecSKristof Provost #define	_OUT(_field)	offsetof(struct pfioc_rule, _field)
78344f323ecSKristof Provost static const struct nlattr_parser nla_p_getrules[] = {
78444f323ecSKristof Provost 	{ .type = PF_GR_ANCHOR, .off = _OUT(anchor), .arg = (void *)MAXPATHLEN, .cb = nlattr_get_chara },
78544f323ecSKristof Provost 	{ .type = PF_GR_ACTION, .off = _OUT(rule.action), .cb = nlattr_get_uint8 },
78644f323ecSKristof Provost };
78744f323ecSKristof Provost static const struct nlfield_parser nlf_p_getrules[] = {
78844f323ecSKristof Provost };
789706d465dSKristof Provost #undef _IN
790777a4702SKristof Provost #undef _OUT
79144f323ecSKristof Provost NL_DECLARE_PARSER(getrules_parser, struct genlmsghdr, nlf_p_getrules, nla_p_getrules);
79244f323ecSKristof Provost 
79344f323ecSKristof Provost static int
79444f323ecSKristof Provost pf_handle_getrules(struct nlmsghdr *hdr, struct nl_pstate *npt)
79544f323ecSKristof Provost {
79644f323ecSKristof Provost 	struct pfioc_rule attrs = {};
79744f323ecSKristof Provost 	int error;
79844f323ecSKristof Provost 	struct nl_writer *nw = npt->nw;
79944f323ecSKristof Provost 	struct genlmsghdr *ghdr_new;
80044f323ecSKristof Provost 
80144f323ecSKristof Provost 	error = nl_parse_nlmsg(hdr, &getrules_parser, npt, &attrs);
80244f323ecSKristof Provost 	if (error != 0)
80344f323ecSKristof Provost 		return (error);
80444f323ecSKristof Provost 
80544f323ecSKristof Provost 	if (!nlmsg_reply(nw, hdr, sizeof(struct genlmsghdr)))
80644f323ecSKristof Provost 		return (ENOMEM);
80744f323ecSKristof Provost 
80844f323ecSKristof Provost 	ghdr_new = nlmsg_reserve_object(nw, struct genlmsghdr);
80944f323ecSKristof Provost 	ghdr_new->cmd = PFNL_CMD_GETRULES;
81044f323ecSKristof Provost 	ghdr_new->version = 0;
81144f323ecSKristof Provost 	ghdr_new->reserved = 0;
81244f323ecSKristof Provost 
81344f323ecSKristof Provost 	error = pf_ioctl_getrules(&attrs);
81444f323ecSKristof Provost 	if (error != 0)
81544f323ecSKristof Provost 		goto out;
81644f323ecSKristof Provost 
81744f323ecSKristof Provost 	nlattr_add_u32(nw, PF_GR_NR, attrs.nr);
81844f323ecSKristof Provost 	nlattr_add_u32(nw, PF_GR_TICKET, attrs.ticket);
81944f323ecSKristof Provost 
82044f323ecSKristof Provost 	if (!nlmsg_end(nw)) {
82144f323ecSKristof Provost 		error = ENOMEM;
82244f323ecSKristof Provost 		goto out;
82344f323ecSKristof Provost 	}
82444f323ecSKristof Provost 
82544f323ecSKristof Provost 	return (0);
82644f323ecSKristof Provost 
82744f323ecSKristof Provost out:
82844f323ecSKristof Provost 	nlmsg_abort(nw);
82944f323ecSKristof Provost 	return (error);
83044f323ecSKristof Provost }
83144f323ecSKristof Provost 
832777a4702SKristof Provost struct nl_parsed_get_rule {
833777a4702SKristof Provost 	char anchor[MAXPATHLEN];
834777a4702SKristof Provost 	uint8_t action;
835777a4702SKristof Provost 	uint32_t nr;
836777a4702SKristof Provost 	uint32_t ticket;
837777a4702SKristof Provost 	uint8_t clear;
838777a4702SKristof Provost };
839777a4702SKristof Provost #define	_IN(_field)	offsetof(struct genlmsghdr, _field)
840777a4702SKristof Provost #define	_OUT(_field)	offsetof(struct nl_parsed_get_rule, _field)
841777a4702SKristof Provost static const struct nlattr_parser nla_p_getrule[] = {
842777a4702SKristof Provost 	{ .type = PF_GR_ANCHOR, .off = _OUT(anchor), .arg = (void *)MAXPATHLEN, .cb = nlattr_get_chara },
843777a4702SKristof Provost 	{ .type = PF_GR_ACTION, .off = _OUT(action), .cb = nlattr_get_uint8 },
844777a4702SKristof Provost 	{ .type = PF_GR_NR, .off = _OUT(nr), .cb = nlattr_get_uint32 },
845777a4702SKristof Provost 	{ .type = PF_GR_TICKET, .off = _OUT(ticket), .cb = nlattr_get_uint32 },
846777a4702SKristof Provost 	{ .type = PF_GR_CLEAR, .off = _OUT(clear), .cb = nlattr_get_uint8 },
847777a4702SKristof Provost };
848777a4702SKristof Provost static const struct nlfield_parser nlf_p_getrule[] = {
849777a4702SKristof Provost };
850706d465dSKristof Provost #undef _IN
851706d465dSKristof Provost #undef _OUT
852777a4702SKristof Provost NL_DECLARE_PARSER(getrule_parser, struct genlmsghdr, nlf_p_getrule, nla_p_getrule);
853777a4702SKristof Provost 
854777a4702SKristof Provost static int
855777a4702SKristof Provost pf_handle_getrule(struct nlmsghdr *hdr, struct nl_pstate *npt)
856777a4702SKristof Provost {
857777a4702SKristof Provost 	char				 anchor_call[MAXPATHLEN];
858777a4702SKristof Provost 	struct nl_parsed_get_rule	 attrs = {};
859777a4702SKristof Provost 	struct nl_writer		*nw = npt->nw;
860777a4702SKristof Provost 	struct genlmsghdr		*ghdr_new;
861777a4702SKristof Provost 	struct pf_kruleset		*ruleset;
862777a4702SKristof Provost 	struct pf_krule			*rule;
863777a4702SKristof Provost 	int				 rs_num;
864777a4702SKristof Provost 	int				 error;
865777a4702SKristof Provost 
866777a4702SKristof Provost 	error = nl_parse_nlmsg(hdr, &getrule_parser, npt, &attrs);
867777a4702SKristof Provost 	if (error != 0)
868777a4702SKristof Provost 		return (error);
869777a4702SKristof Provost 
870777a4702SKristof Provost 	if (!nlmsg_reply(nw, hdr, sizeof(struct genlmsghdr)))
871777a4702SKristof Provost 		return (ENOMEM);
872777a4702SKristof Provost 
873777a4702SKristof Provost 	ghdr_new = nlmsg_reserve_object(nw, struct genlmsghdr);
874777a4702SKristof Provost 	ghdr_new->cmd = PFNL_CMD_GETRULE;
875777a4702SKristof Provost 	ghdr_new->version = 0;
876777a4702SKristof Provost 	ghdr_new->reserved = 0;
877777a4702SKristof Provost 
878777a4702SKristof Provost 	PF_RULES_WLOCK();
879777a4702SKristof Provost 	ruleset = pf_find_kruleset(attrs.anchor);
880777a4702SKristof Provost 	if (ruleset == NULL) {
881777a4702SKristof Provost 		PF_RULES_WUNLOCK();
882777a4702SKristof Provost 		error = ENOENT;
883777a4702SKristof Provost 		goto out;
884777a4702SKristof Provost 	}
885777a4702SKristof Provost 
886777a4702SKristof Provost 	rs_num = pf_get_ruleset_number(attrs.action);
887777a4702SKristof Provost 	if (rs_num >= PF_RULESET_MAX) {
888777a4702SKristof Provost 		PF_RULES_WUNLOCK();
889777a4702SKristof Provost 		error = EINVAL;
890777a4702SKristof Provost 		goto out;
891777a4702SKristof Provost 	}
892777a4702SKristof Provost 
893777a4702SKristof Provost 	if (attrs.ticket != ruleset->rules[rs_num].active.ticket) {
894777a4702SKristof Provost 		PF_RULES_WUNLOCK();
895777a4702SKristof Provost 		error = EBUSY;
896777a4702SKristof Provost 		goto out;
897777a4702SKristof Provost 	}
898777a4702SKristof Provost 
899777a4702SKristof Provost 	rule = TAILQ_FIRST(ruleset->rules[rs_num].active.ptr);
900777a4702SKristof Provost 	while ((rule != NULL) && (rule->nr != attrs.nr))
901777a4702SKristof Provost 		rule = TAILQ_NEXT(rule, entries);
902777a4702SKristof Provost 	if (rule == NULL) {
903777a4702SKristof Provost 		PF_RULES_WUNLOCK();
904777a4702SKristof Provost 		error = EBUSY;
905777a4702SKristof Provost 		goto out;
906777a4702SKristof Provost 	}
907777a4702SKristof Provost 
908777a4702SKristof Provost 	nlattr_add_rule_addr(nw, PF_RT_SRC, &rule->src);
909777a4702SKristof Provost 	nlattr_add_rule_addr(nw, PF_RT_DST, &rule->dst);
910777a4702SKristof Provost 	nlattr_add_u32(nw, PF_RT_RIDENTIFIER, rule->ridentifier);
911777a4702SKristof Provost 	nlattr_add_labels(nw, PF_RT_LABELS, rule);
912777a4702SKristof Provost 	nlattr_add_string(nw, PF_RT_IFNAME, rule->ifname);
913777a4702SKristof Provost 	nlattr_add_string(nw, PF_RT_QNAME, rule->qname);
914777a4702SKristof Provost 	nlattr_add_string(nw, PF_RT_PQNAME, rule->pqname);
915777a4702SKristof Provost 	nlattr_add_string(nw, PF_RT_TAGNAME, rule->tagname);
916777a4702SKristof Provost 	nlattr_add_string(nw, PF_RT_MATCH_TAGNAME, rule->match_tagname);
917777a4702SKristof Provost 	nlattr_add_string(nw, PF_RT_OVERLOAD_TBLNAME, rule->overload_tblname);
918777a4702SKristof Provost 	nlattr_add_pool(nw, PF_RT_RPOOL, &rule->rpool);
919777a4702SKristof Provost 	nlattr_add_u32(nw, PF_RT_OS_FINGERPRINT, rule->os_fingerprint);
920777a4702SKristof Provost 	nlattr_add_u32(nw, PF_RT_RTABLEID, rule->rtableid);
921777a4702SKristof Provost 	nlattr_add_timeout(nw, PF_RT_TIMEOUT, rule->timeout);
922777a4702SKristof Provost 	nlattr_add_u32(nw, PF_RT_MAX_STATES, rule->max_states);
923777a4702SKristof Provost 	nlattr_add_u32(nw, PF_RT_MAX_SRC_NODES, rule->max_src_nodes);
924777a4702SKristof Provost 	nlattr_add_u32(nw, PF_RT_MAX_SRC_STATES, rule->max_src_states);
9257fe42038SKajetan Staszkiewicz 	nlattr_add_u32(nw, PF_RT_MAX_SRC_CONN, rule->max_src_conn);
926777a4702SKristof Provost 	nlattr_add_u32(nw, PF_RT_MAX_SRC_CONN_RATE_LIMIT, rule->max_src_conn_rate.limit);
927777a4702SKristof Provost 	nlattr_add_u32(nw, PF_RT_MAX_SRC_CONN_RATE_SECS, rule->max_src_conn_rate.seconds);
928777a4702SKristof Provost 
929777a4702SKristof Provost 	nlattr_add_u16(nw, PF_RT_DNPIPE, rule->dnpipe);
930777a4702SKristof Provost 	nlattr_add_u16(nw, PF_RT_DNRPIPE, rule->dnrpipe);
931777a4702SKristof Provost 	nlattr_add_u32(nw, PF_RT_DNFLAGS, rule->free_flags);
932777a4702SKristof Provost 
933777a4702SKristof Provost 	nlattr_add_u32(nw, PF_RT_NR, rule->nr);
934777a4702SKristof Provost 	nlattr_add_u32(nw, PF_RT_PROB, rule->prob);
935777a4702SKristof Provost 	nlattr_add_u32(nw, PF_RT_CUID, rule->cuid);
936777a4702SKristof Provost 	nlattr_add_u32(nw, PF_RT_CPID, rule->cpid);
937777a4702SKristof Provost 
938777a4702SKristof Provost 	nlattr_add_u16(nw, PF_RT_RETURN_ICMP, rule->return_icmp);
939777a4702SKristof Provost 	nlattr_add_u16(nw, PF_RT_RETURN_ICMP6, rule->return_icmp6);
940777a4702SKristof Provost 	nlattr_add_u16(nw, PF_RT_RETURN_ICMP6, rule->return_icmp6);
941777a4702SKristof Provost 	nlattr_add_u16(nw, PF_RT_MAX_MSS, rule->max_mss);
942777a4702SKristof Provost 	nlattr_add_u16(nw, PF_RT_SCRUB_FLAGS, rule->scrub_flags);
943777a4702SKristof Provost 
944777a4702SKristof Provost 	nlattr_add_rule_uid(nw, PF_RT_UID, &rule->uid);
945777a4702SKristof Provost 	nlattr_add_rule_uid(nw, PF_RT_GID, (const struct pf_rule_uid *)&rule->gid);
946777a4702SKristof Provost 
9472339ead6SKristof Provost 	nlattr_add_string(nw, PF_RT_RCV_IFNAME, rule->rcv_ifname);
9482339ead6SKristof Provost 
949777a4702SKristof Provost 	nlattr_add_u32(nw, PF_RT_RULE_FLAG, rule->rule_flag);
950777a4702SKristof Provost 	nlattr_add_u8(nw, PF_RT_ACTION, rule->action);
951777a4702SKristof Provost 	nlattr_add_u8(nw, PF_RT_DIRECTION, rule->direction);
952777a4702SKristof Provost 	nlattr_add_u8(nw, PF_RT_LOG, rule->log);
953777a4702SKristof Provost 	nlattr_add_u8(nw, PF_RT_LOGIF, rule->logif);
954777a4702SKristof Provost 	nlattr_add_u8(nw, PF_RT_QUICK, rule->quick);
955777a4702SKristof Provost 	nlattr_add_u8(nw, PF_RT_IF_NOT, rule->ifnot);
956777a4702SKristof Provost 	nlattr_add_u8(nw, PF_RT_MATCH_TAG_NOT, rule->match_tag_not);
957777a4702SKristof Provost 	nlattr_add_u8(nw, PF_RT_NATPASS, rule->natpass);
958777a4702SKristof Provost 	nlattr_add_u8(nw, PF_RT_KEEP_STATE, rule->keep_state);
959777a4702SKristof Provost 
960777a4702SKristof Provost 	nlattr_add_u8(nw, PF_RT_AF, rule->af);
961777a4702SKristof Provost 	nlattr_add_u8(nw, PF_RT_PROTO, rule->proto);
962777a4702SKristof Provost 	nlattr_add_u8(nw, PF_RT_TYPE, rule->type);
963777a4702SKristof Provost 	nlattr_add_u8(nw, PF_RT_CODE, rule->code);
964777a4702SKristof Provost 	nlattr_add_u8(nw, PF_RT_FLAGS, rule->flags);
965777a4702SKristof Provost 	nlattr_add_u8(nw, PF_RT_FLAGSET, rule->flagset);
966777a4702SKristof Provost 	nlattr_add_u8(nw, PF_RT_MIN_TTL, rule->min_ttl);
967777a4702SKristof Provost 	nlattr_add_u8(nw, PF_RT_ALLOW_OPTS, rule->allow_opts);
968777a4702SKristof Provost 	nlattr_add_u8(nw, PF_RT_RT, rule->rt);
969777a4702SKristof Provost 	nlattr_add_u8(nw, PF_RT_RETURN_TTL, rule->return_ttl);
970777a4702SKristof Provost 	nlattr_add_u8(nw, PF_RT_TOS, rule->tos);
971777a4702SKristof Provost 	nlattr_add_u8(nw, PF_RT_SET_TOS, rule->set_tos);
972777a4702SKristof Provost 	nlattr_add_u8(nw, PF_RT_ANCHOR_RELATIVE, rule->anchor_relative);
973777a4702SKristof Provost 	nlattr_add_u8(nw, PF_RT_ANCHOR_WILDCARD, rule->anchor_wildcard);
974777a4702SKristof Provost 	nlattr_add_u8(nw, PF_RT_FLUSH, rule->flush);
975777a4702SKristof Provost 	nlattr_add_u8(nw, PF_RT_PRIO, rule->prio);
976777a4702SKristof Provost 	nlattr_add_u8(nw, PF_RT_SET_PRIO, rule->set_prio[0]);
977777a4702SKristof Provost 	nlattr_add_u8(nw, PF_RT_SET_PRIO_REPLY, rule->set_prio[1]);
978777a4702SKristof Provost 
979777a4702SKristof Provost 	nlattr_add_in6_addr(nw, PF_RT_DIVERT_ADDRESS, &rule->divert.addr.v6);
980777a4702SKristof Provost 	nlattr_add_u16(nw, PF_RT_DIVERT_PORT, rule->divert.port);
981777a4702SKristof Provost 
982777a4702SKristof Provost 	nlattr_add_u64(nw, PF_RT_PACKETS_IN, pf_counter_u64_fetch(&rule->packets[0]));
983777a4702SKristof Provost 	nlattr_add_u64(nw, PF_RT_PACKETS_OUT, pf_counter_u64_fetch(&rule->packets[1]));
984777a4702SKristof Provost 	nlattr_add_u64(nw, PF_RT_BYTES_IN, pf_counter_u64_fetch(&rule->bytes[0]));
985777a4702SKristof Provost 	nlattr_add_u64(nw, PF_RT_BYTES_OUT, pf_counter_u64_fetch(&rule->bytes[1]));
986777a4702SKristof Provost 	nlattr_add_u64(nw, PF_RT_EVALUATIONS, pf_counter_u64_fetch(&rule->evaluations));
987777a4702SKristof Provost 	nlattr_add_u64(nw, PF_RT_TIMESTAMP, pf_get_timestamp(rule));
988777a4702SKristof Provost 	nlattr_add_u64(nw, PF_RT_STATES_CUR, counter_u64_fetch(rule->states_cur));
989777a4702SKristof Provost 	nlattr_add_u64(nw, PF_RT_STATES_TOTAL, counter_u64_fetch(rule->states_tot));
990777a4702SKristof Provost 	nlattr_add_u64(nw, PF_RT_SRC_NODES, counter_u64_fetch(rule->src_nodes));
991777a4702SKristof Provost 
9926ee3e376SKristof Provost 	error = pf_kanchor_copyout(ruleset, rule, anchor_call, sizeof(anchor_call));
993777a4702SKristof Provost 	MPASS(error == 0);
994777a4702SKristof Provost 
995777a4702SKristof Provost 	nlattr_add_string(nw, PF_RT_ANCHOR_CALL, anchor_call);
996777a4702SKristof Provost 
997777a4702SKristof Provost 	if (attrs.clear)
998777a4702SKristof Provost 		pf_krule_clear_counters(rule);
999777a4702SKristof Provost 
1000777a4702SKristof Provost 	PF_RULES_WUNLOCK();
1001777a4702SKristof Provost 
1002777a4702SKristof Provost 	if (!nlmsg_end(nw)) {
1003777a4702SKristof Provost 		error = ENOMEM;
1004777a4702SKristof Provost 		goto out;
1005777a4702SKristof Provost 	}
1006777a4702SKristof Provost 
1007777a4702SKristof Provost 	return (0);
1008777a4702SKristof Provost out:
1009777a4702SKristof Provost 	nlmsg_abort(nw);
1010777a4702SKristof Provost 	return (error);
1011777a4702SKristof Provost }
1012777a4702SKristof Provost 
1013706d465dSKristof Provost #define	_IN(_field)	offsetof(struct genlmsghdr, _field)
1014706d465dSKristof Provost #define	_OUT(_field)	offsetof(struct pf_kstate_kill, _field)
1015706d465dSKristof Provost static const struct nlattr_parser nla_p_clear_states[] = {
1016706d465dSKristof Provost 	{ .type = PF_CS_CMP_ID, .off = _OUT(psk_pfcmp.id), .cb = nlattr_get_uint64 },
1017706d465dSKristof Provost 	{ .type = PF_CS_CMP_CREATORID, .off = _OUT(psk_pfcmp.creatorid), .cb = nlattr_get_uint32 },
1018706d465dSKristof Provost 	{ .type = PF_CS_CMP_DIR, .off = _OUT(psk_pfcmp.direction), .cb = nlattr_get_uint8 },
1019706d465dSKristof Provost 	{ .type = PF_CS_AF, .off = _OUT(psk_af), .cb = nlattr_get_uint8 },
1020706d465dSKristof Provost 	{ .type = PF_CS_PROTO, .off = _OUT(psk_proto), .cb = nlattr_get_uint8 },
1021706d465dSKristof Provost 	{ .type = PF_CS_SRC, .off = _OUT(psk_src), .arg = &rule_addr_parser, .cb = nlattr_get_nested },
1022706d465dSKristof Provost 	{ .type = PF_CS_DST, .off = _OUT(psk_dst), .arg = &rule_addr_parser, .cb = nlattr_get_nested },
1023706d465dSKristof Provost 	{ .type = PF_CS_RT_ADDR, .off = _OUT(psk_rt_addr), .arg = &rule_addr_parser, .cb = nlattr_get_nested },
1024706d465dSKristof Provost 	{ .type = PF_CS_IFNAME, .off = _OUT(psk_ifname), .arg = (void *)IFNAMSIZ, .cb = nlattr_get_chara },
1025706d465dSKristof Provost 	{ .type = PF_CS_LABEL, .off = _OUT(psk_label), .arg = (void *)PF_RULE_LABEL_SIZE, .cb = nlattr_get_chara },
1026706d465dSKristof Provost 	{ .type = PF_CS_KILL_MATCH, .off = _OUT(psk_kill_match), .cb = nlattr_get_bool },
1027706d465dSKristof Provost 	{ .type = PF_CS_NAT, .off = _OUT(psk_nat), .cb = nlattr_get_bool },
1028706d465dSKristof Provost };
1029706d465dSKristof Provost static const struct nlfield_parser nlf_p_clear_states[] = {};
1030706d465dSKristof Provost #undef _IN
1031706d465dSKristof Provost #undef _OUT
1032706d465dSKristof Provost NL_DECLARE_PARSER(clear_states_parser, struct genlmsghdr, nlf_p_clear_states, nla_p_clear_states);
1033706d465dSKristof Provost 
1034706d465dSKristof Provost static int
1035706d465dSKristof Provost pf_handle_killclear_states(struct nlmsghdr *hdr, struct nl_pstate *npt, int cmd)
1036706d465dSKristof Provost {
1037706d465dSKristof Provost 	struct pf_kstate_kill		 kill = {};
1038706d465dSKristof Provost 	struct epoch_tracker		 et;
1039706d465dSKristof Provost 	struct nl_writer		*nw = npt->nw;
1040706d465dSKristof Provost 	struct genlmsghdr		*ghdr_new;
1041706d465dSKristof Provost 	int				 error;
1042706d465dSKristof Provost 	unsigned int			 killed = 0;
1043706d465dSKristof Provost 
1044706d465dSKristof Provost 	error = nl_parse_nlmsg(hdr, &clear_states_parser, npt, &kill);
1045706d465dSKristof Provost 	if (error != 0)
1046706d465dSKristof Provost 		return (error);
1047706d465dSKristof Provost 
1048706d465dSKristof Provost 	if (!nlmsg_reply(nw, hdr, sizeof(struct genlmsghdr)))
1049706d465dSKristof Provost 		return (ENOMEM);
1050706d465dSKristof Provost 
1051706d465dSKristof Provost 	ghdr_new = nlmsg_reserve_object(nw, struct genlmsghdr);
1052706d465dSKristof Provost 	ghdr_new->cmd = cmd;
1053706d465dSKristof Provost 	ghdr_new->version = 0;
1054706d465dSKristof Provost 	ghdr_new->reserved = 0;
1055706d465dSKristof Provost 
1056706d465dSKristof Provost 	NET_EPOCH_ENTER(et);
1057706d465dSKristof Provost 	if (cmd == PFNL_CMD_KILLSTATES)
1058706d465dSKristof Provost 		pf_killstates(&kill, &killed);
1059706d465dSKristof Provost 	else
1060706d465dSKristof Provost 		killed = pf_clear_states(&kill);
1061706d465dSKristof Provost 	NET_EPOCH_EXIT(et);
1062706d465dSKristof Provost 
1063706d465dSKristof Provost 	nlattr_add_u32(nw, PF_CS_KILLED, killed);
1064706d465dSKristof Provost 
1065706d465dSKristof Provost 	if (! nlmsg_end(nw)) {
1066706d465dSKristof Provost 		error = ENOMEM;
1067706d465dSKristof Provost 		goto out;
1068706d465dSKristof Provost 	}
1069706d465dSKristof Provost 
1070706d465dSKristof Provost 	return (0);
1071706d465dSKristof Provost 
1072706d465dSKristof Provost out:
1073706d465dSKristof Provost 	nlmsg_abort(nw);
1074706d465dSKristof Provost 	return (error);
1075706d465dSKristof Provost }
1076706d465dSKristof Provost 
1077706d465dSKristof Provost static int
1078706d465dSKristof Provost pf_handle_clear_states(struct nlmsghdr *hdr, struct nl_pstate *npt)
1079706d465dSKristof Provost {
1080706d465dSKristof Provost 	return (pf_handle_killclear_states(hdr, npt, PFNL_CMD_CLRSTATES));
1081706d465dSKristof Provost }
1082706d465dSKristof Provost 
1083706d465dSKristof Provost static int
1084706d465dSKristof Provost pf_handle_kill_states(struct nlmsghdr *hdr, struct nl_pstate *npt)
1085706d465dSKristof Provost {
1086706d465dSKristof Provost 	return (pf_handle_killclear_states(hdr, npt, PFNL_CMD_KILLSTATES));
1087706d465dSKristof Provost }
1088706d465dSKristof Provost 
1089470a2b33SKristof Provost struct nl_parsed_set_statusif {
1090470a2b33SKristof Provost 	char ifname[IFNAMSIZ];
1091470a2b33SKristof Provost };
1092470a2b33SKristof Provost #define	_IN(_field)	offsetof(struct genlmsghdr, _field)
1093470a2b33SKristof Provost #define	_OUT(_field)	offsetof(struct nl_parsed_set_statusif, _field)
1094470a2b33SKristof Provost static const struct nlattr_parser nla_p_set_statusif[] = {
1095470a2b33SKristof Provost 	{ .type = PF_SS_IFNAME, .off = _OUT(ifname), .arg = (const void *)IFNAMSIZ, .cb = nlattr_get_chara },
1096470a2b33SKristof Provost };
1097470a2b33SKristof Provost static const struct nlfield_parser nlf_p_set_statusif[] = {};
1098470a2b33SKristof Provost #undef _IN
1099470a2b33SKristof Provost #undef _OUT
1100470a2b33SKristof Provost NL_DECLARE_PARSER(set_statusif_parser, struct genlmsghdr, nlf_p_set_statusif, nla_p_set_statusif);
1101470a2b33SKristof Provost 
1102470a2b33SKristof Provost static int
1103470a2b33SKristof Provost pf_handle_set_statusif(struct nlmsghdr *hdr, struct nl_pstate *npt)
1104470a2b33SKristof Provost {
1105470a2b33SKristof Provost 	int error;
1106470a2b33SKristof Provost 	struct nl_parsed_set_statusif attrs = {};
1107470a2b33SKristof Provost 
1108470a2b33SKristof Provost 	error = nl_parse_nlmsg(hdr, &set_statusif_parser, npt, &attrs);
1109470a2b33SKristof Provost 	if (error != 0)
1110470a2b33SKristof Provost 		return (error);
1111470a2b33SKristof Provost 
1112470a2b33SKristof Provost 	PF_RULES_WLOCK();
1113470a2b33SKristof Provost 	strlcpy(V_pf_status.ifname, attrs.ifname, IFNAMSIZ);
1114470a2b33SKristof Provost 	PF_RULES_WUNLOCK();
1115470a2b33SKristof Provost 
1116470a2b33SKristof Provost 	return (0);
1117470a2b33SKristof Provost }
1118470a2b33SKristof Provost 
11195824df8dSKristof Provost static bool
11205824df8dSKristof Provost nlattr_add_counters(struct nl_writer *nw, int attr, size_t number, char **names,
11215824df8dSKristof Provost     counter_u64_t *counters)
11225824df8dSKristof Provost {
11235824df8dSKristof Provost 	for (int i = 0; i < number; i++) {
11245824df8dSKristof Provost 		int off = nlattr_add_nested(nw, attr);
11255824df8dSKristof Provost 		nlattr_add_u32(nw, PF_C_ID, i);
11265824df8dSKristof Provost 		nlattr_add_string(nw, PF_C_NAME, names[i]);
11275824df8dSKristof Provost 		nlattr_add_u64(nw, PF_C_COUNTER, counter_u64_fetch(counters[i]));
11285824df8dSKristof Provost 		nlattr_set_len(nw, off);
11295824df8dSKristof Provost 	}
11305824df8dSKristof Provost 
11315824df8dSKristof Provost 	return (true);
11325824df8dSKristof Provost }
11335824df8dSKristof Provost 
11345824df8dSKristof Provost static bool
11355824df8dSKristof Provost nlattr_add_fcounters(struct nl_writer *nw, int attr, size_t number, char **names,
11365824df8dSKristof Provost     struct pf_counter_u64 *counters)
11375824df8dSKristof Provost {
11385824df8dSKristof Provost 	for (int i = 0; i < number; i++) {
11395824df8dSKristof Provost 		int off = nlattr_add_nested(nw, attr);
11405824df8dSKristof Provost 		nlattr_add_u32(nw, PF_C_ID, i);
11415824df8dSKristof Provost 		nlattr_add_string(nw, PF_C_NAME, names[i]);
11425824df8dSKristof Provost 		nlattr_add_u64(nw, PF_C_COUNTER, pf_counter_u64_fetch(&counters[i]));
11435824df8dSKristof Provost 		nlattr_set_len(nw, off);
11445824df8dSKristof Provost 	}
11455824df8dSKristof Provost 
11465824df8dSKristof Provost 	return (true);
11475824df8dSKristof Provost }
11485824df8dSKristof Provost 
11495824df8dSKristof Provost static bool
11505824df8dSKristof Provost nlattr_add_u64_array(struct nl_writer *nw, int attr, size_t number, uint64_t *array)
11515824df8dSKristof Provost {
11525824df8dSKristof Provost 	int off = nlattr_add_nested(nw, attr);
11535824df8dSKristof Provost 
11545824df8dSKristof Provost 	for (size_t i = 0; i < number; i++)
11555824df8dSKristof Provost 		nlattr_add_u64(nw, 0, array[i]);
11565824df8dSKristof Provost 
11575824df8dSKristof Provost 	nlattr_set_len(nw, off);
11585824df8dSKristof Provost 
11595824df8dSKristof Provost 	return (true);
11605824df8dSKristof Provost }
11615824df8dSKristof Provost 
11625824df8dSKristof Provost static int
11635824df8dSKristof Provost pf_handle_get_status(struct nlmsghdr *hdr, struct nl_pstate *npt)
11645824df8dSKristof Provost {
11655824df8dSKristof Provost 	struct pf_status s;
11665824df8dSKristof Provost 	struct nl_writer *nw = npt->nw;
11675824df8dSKristof Provost 	struct genlmsghdr *ghdr_new;
11685824df8dSKristof Provost 	char *pf_reasons[PFRES_MAX+1] = PFRES_NAMES;
11695824df8dSKristof Provost 	char *pf_lcounter[KLCNT_MAX+1] = KLCNT_NAMES;
11705824df8dSKristof Provost 	char *pf_fcounter[FCNT_MAX+1] = FCNT_NAMES;
11715824df8dSKristof Provost 	int error;
11725824df8dSKristof Provost 
11735824df8dSKristof Provost 	PF_RULES_RLOCK_TRACKER;
11745824df8dSKristof Provost 
11755824df8dSKristof Provost 	if (!nlmsg_reply(nw, hdr, sizeof(struct genlmsghdr)))
11765824df8dSKristof Provost 		return (ENOMEM);
11775824df8dSKristof Provost 
11785824df8dSKristof Provost 	ghdr_new = nlmsg_reserve_object(nw, struct genlmsghdr);
11795824df8dSKristof Provost 	ghdr_new->cmd = PFNL_CMD_GET_STATUS;
11805824df8dSKristof Provost 	ghdr_new->version = 0;
11815824df8dSKristof Provost 	ghdr_new->reserved = 0;
11825824df8dSKristof Provost 
11835824df8dSKristof Provost 	PF_RULES_RLOCK();
11845824df8dSKristof Provost 
11855824df8dSKristof Provost 	nlattr_add_string(nw, PF_GS_IFNAME, V_pf_status.ifname);
11865824df8dSKristof Provost 	nlattr_add_bool(nw, PF_GS_RUNNING, V_pf_status.running);
11875824df8dSKristof Provost 	nlattr_add_u32(nw, PF_GS_SINCE, V_pf_status.since);
11885824df8dSKristof Provost 	nlattr_add_u32(nw, PF_GS_DEBUG, V_pf_status.debug);
11895824df8dSKristof Provost 	nlattr_add_u32(nw, PF_GS_HOSTID, ntohl(V_pf_status.hostid));
11905824df8dSKristof Provost 	nlattr_add_u32(nw, PF_GS_STATES, V_pf_status.states);
11915824df8dSKristof Provost 	nlattr_add_u32(nw, PF_GS_SRC_NODES, V_pf_status.src_nodes);
11925824df8dSKristof Provost 	nlattr_add_u32(nw, PF_GS_REASSEMBLE, V_pf_status.reass);
11935824df8dSKristof Provost 	nlattr_add_u32(nw, PF_GS_SYNCOOKIES_ACTIVE, V_pf_status.syncookies_active);
11945824df8dSKristof Provost 
11955824df8dSKristof Provost 	nlattr_add_counters(nw, PF_GS_COUNTERS, PFRES_MAX, pf_reasons,
11965824df8dSKristof Provost 	    V_pf_status.counters);
11975824df8dSKristof Provost 	nlattr_add_counters(nw, PF_GS_LCOUNTERS, KLCNT_MAX, pf_lcounter,
11985824df8dSKristof Provost 	    V_pf_status.lcounters);
11995824df8dSKristof Provost 	nlattr_add_fcounters(nw, PF_GS_FCOUNTERS, FCNT_MAX, pf_fcounter,
12005824df8dSKristof Provost 	    V_pf_status.fcounters);
12015824df8dSKristof Provost 	nlattr_add_counters(nw, PF_GS_SCOUNTERS, SCNT_MAX, pf_fcounter,
12025824df8dSKristof Provost 	    V_pf_status.scounters);
12035824df8dSKristof Provost 
12045824df8dSKristof Provost 	pfi_update_status(V_pf_status.ifname, &s);
12055824df8dSKristof Provost 	nlattr_add_u64_array(nw, PF_GS_BCOUNTERS, 2 * 2, (uint64_t *)s.bcounters);
12065824df8dSKristof Provost 	nlattr_add_u64_array(nw, PF_GS_PCOUNTERS, 2 * 2 * 2, (uint64_t *)s.pcounters);
12075824df8dSKristof Provost 
12085824df8dSKristof Provost 	nlattr_add(nw, PF_GS_CHKSUM, PF_MD5_DIGEST_LENGTH, V_pf_status.pf_chksum);
12095824df8dSKristof Provost 
12105824df8dSKristof Provost 	PF_RULES_RUNLOCK();
12115824df8dSKristof Provost 
12125824df8dSKristof Provost 	if (!nlmsg_end(nw)) {
12135824df8dSKristof Provost 		error = ENOMEM;
12145824df8dSKristof Provost 		goto out;
12155824df8dSKristof Provost 	}
12165824df8dSKristof Provost 
12175824df8dSKristof Provost 	return (0);
12185824df8dSKristof Provost 
12195824df8dSKristof Provost out:
12205824df8dSKristof Provost 	nlmsg_abort(nw);
12215824df8dSKristof Provost 	return (error);
12225824df8dSKristof Provost }
12235824df8dSKristof Provost 
12249dbbe68bSKristof Provost static int
12259dbbe68bSKristof Provost pf_handle_clear_status(struct nlmsghdr *hdr, struct nl_pstate *npt)
12269dbbe68bSKristof Provost {
12279dbbe68bSKristof Provost 	pf_ioctl_clear_status();
12289dbbe68bSKristof Provost 
12299dbbe68bSKristof Provost 	return (0);
12309dbbe68bSKristof Provost }
12319dbbe68bSKristof Provost 
123271d3c704SKristof Provost struct pf_nl_natlook {
123371d3c704SKristof Provost 	sa_family_t af;
123471d3c704SKristof Provost 	uint8_t direction;
123571d3c704SKristof Provost 	uint8_t proto;
123671d3c704SKristof Provost 	struct pf_addr src;
123771d3c704SKristof Provost 	struct pf_addr dst;
123871d3c704SKristof Provost 	uint16_t sport;
123971d3c704SKristof Provost 	uint16_t dport;
124071d3c704SKristof Provost };
124171d3c704SKristof Provost 
124271d3c704SKristof Provost #define	_IN(_field)	offsetof(struct genlmsghdr, _field)
124371d3c704SKristof Provost #define	_OUT(_field)	offsetof(struct pf_nl_natlook, _field)
124471d3c704SKristof Provost static const struct nlattr_parser nla_p_natlook[] = {
124571d3c704SKristof Provost 	{ .type = PF_NL_AF, .off = _OUT(af), .cb = nlattr_get_uint8 },
124671d3c704SKristof Provost 	{ .type = PF_NL_DIRECTION, .off = _OUT(direction), .cb = nlattr_get_uint8 },
124771d3c704SKristof Provost 	{ .type = PF_NL_PROTO, .off = _OUT(proto), .cb = nlattr_get_uint8 },
124871d3c704SKristof Provost 	{ .type = PF_NL_SRC_ADDR, .off = _OUT(src), .cb = nlattr_get_in6_addr },
124971d3c704SKristof Provost 	{ .type = PF_NL_DST_ADDR, .off = _OUT(dst), .cb = nlattr_get_in6_addr },
125071d3c704SKristof Provost 	{ .type = PF_NL_SRC_PORT, .off = _OUT(sport), .cb = nlattr_get_uint16 },
125171d3c704SKristof Provost 	{ .type = PF_NL_DST_PORT, .off = _OUT(dport), .cb = nlattr_get_uint16 },
125271d3c704SKristof Provost };
125371d3c704SKristof Provost static const struct nlfield_parser nlf_p_natlook[] = {};
125471d3c704SKristof Provost #undef _IN
125571d3c704SKristof Provost #undef _OUT
125671d3c704SKristof Provost NL_DECLARE_PARSER(natlook_parser, struct genlmsghdr, nlf_p_natlook, nla_p_natlook);
125771d3c704SKristof Provost 
125871d3c704SKristof Provost static int
125971d3c704SKristof Provost pf_handle_natlook(struct nlmsghdr *hdr, struct nl_pstate *npt)
126071d3c704SKristof Provost {
126171d3c704SKristof Provost 	struct pf_nl_natlook	 attrs = {};
126271d3c704SKristof Provost 	struct pf_state_key_cmp	 key = {};
126371d3c704SKristof Provost 	struct nl_writer	*nw = npt->nw;
126471d3c704SKristof Provost 	struct pf_state_key	*sk;
126571d3c704SKristof Provost 	struct pf_kstate	*state;
126671d3c704SKristof Provost 	struct genlmsghdr	*ghdr_new;
126771d3c704SKristof Provost 	int			 error, m;
126871d3c704SKristof Provost 	int			 sidx, didx;
126971d3c704SKristof Provost 
127071d3c704SKristof Provost 	error = nl_parse_nlmsg(hdr, &natlook_parser, npt, &attrs);
127171d3c704SKristof Provost 	if (error != 0)
127271d3c704SKristof Provost 		return (error);
127371d3c704SKristof Provost 
127471d3c704SKristof Provost 	if (attrs.proto == 0 ||
127571d3c704SKristof Provost 	    PF_AZERO(&attrs.src, attrs.af) ||
127671d3c704SKristof Provost 	    PF_AZERO(&attrs.dst, attrs.af) ||
127771d3c704SKristof Provost 	    ((attrs.proto == IPPROTO_TCP || attrs.proto == IPPROTO_UDP) &&
127871d3c704SKristof Provost 	     (attrs.sport == 0 || attrs.dport == 0)))
127971d3c704SKristof Provost 		return (EINVAL);
128071d3c704SKristof Provost 
128171d3c704SKristof Provost 	/* NATLOOK src and dst are reversed, so reverse sidx/didx */
128271d3c704SKristof Provost 	sidx = (attrs.direction == PF_IN) ? 1 : 0;
128371d3c704SKristof Provost 	didx = (attrs.direction == PF_IN) ? 0 : 1;
128471d3c704SKristof Provost 
128571d3c704SKristof Provost 	key.af = attrs.af;
128671d3c704SKristof Provost 	key.proto = attrs.proto;
128771d3c704SKristof Provost 	PF_ACPY(&key.addr[sidx], &attrs.src, attrs.af);
128871d3c704SKristof Provost 	key.port[sidx] = attrs.sport;
128971d3c704SKristof Provost 	PF_ACPY(&key.addr[didx], &attrs.dst, attrs.af);
129071d3c704SKristof Provost 	key.port[didx] = attrs.dport;
129171d3c704SKristof Provost 
129271d3c704SKristof Provost 	state = pf_find_state_all(&key, attrs.direction, &m);
129371d3c704SKristof Provost 	if (state == NULL)
129471d3c704SKristof Provost 		return (ENOENT);
129571d3c704SKristof Provost 	if (m > 1) {
129671d3c704SKristof Provost 		PF_STATE_UNLOCK(state);
129771d3c704SKristof Provost 		return (E2BIG);
129871d3c704SKristof Provost 	}
129971d3c704SKristof Provost 
130071d3c704SKristof Provost 	if (!nlmsg_reply(nw, hdr, sizeof(struct genlmsghdr))) {
130171d3c704SKristof Provost 		PF_STATE_UNLOCK(state);
130271d3c704SKristof Provost 		return (ENOMEM);
130371d3c704SKristof Provost 	}
130471d3c704SKristof Provost 
130571d3c704SKristof Provost 	ghdr_new = nlmsg_reserve_object(nw, struct genlmsghdr);
130671d3c704SKristof Provost 	ghdr_new->cmd = PFNL_CMD_NATLOOK;
130771d3c704SKristof Provost 	ghdr_new->version = 0;
130871d3c704SKristof Provost 	ghdr_new->reserved = 0;
130971d3c704SKristof Provost 
131071d3c704SKristof Provost 	sk = state->key[sidx];
131171d3c704SKristof Provost 
131271d3c704SKristof Provost 	nlattr_add_in6_addr(nw, PF_NL_SRC_ADDR, &sk->addr[sidx].v6);
131371d3c704SKristof Provost 	nlattr_add_in6_addr(nw, PF_NL_DST_ADDR, &sk->addr[didx].v6);
131471d3c704SKristof Provost 	nlattr_add_u16(nw, PF_NL_SRC_PORT, sk->port[sidx]);
131571d3c704SKristof Provost 	nlattr_add_u16(nw, PF_NL_DST_PORT, sk->port[didx]);
131671d3c704SKristof Provost 
131771d3c704SKristof Provost 	PF_STATE_UNLOCK(state);
131871d3c704SKristof Provost 
131971d3c704SKristof Provost 	if (!nlmsg_end(nw)) {
132071d3c704SKristof Provost 		nlmsg_abort(nw);
132171d3c704SKristof Provost 		return (ENOMEM);
132271d3c704SKristof Provost 	}
132371d3c704SKristof Provost 
132471d3c704SKristof Provost 	return (0);
132571d3c704SKristof Provost }
132671d3c704SKristof Provost 
1327c36c90a2SKristof Provost struct pf_nl_set_debug
1328c36c90a2SKristof Provost {
1329c36c90a2SKristof Provost 	uint32_t level;
1330c36c90a2SKristof Provost };
1331c36c90a2SKristof Provost #define	_OUT(_field)	offsetof(struct pf_nl_set_debug, _field)
1332c36c90a2SKristof Provost static const struct nlattr_parser nla_p_set_debug[] = {
1333c36c90a2SKristof Provost 	{ .type = PF_SD_LEVEL, .off = _OUT(level), .cb = nlattr_get_uint32 },
1334c36c90a2SKristof Provost };
1335c36c90a2SKristof Provost static const struct nlfield_parser nlf_p_set_debug[] = {};
1336c36c90a2SKristof Provost #undef _OUT
1337c36c90a2SKristof Provost NL_DECLARE_PARSER(set_debug_parser, struct genlmsghdr, nlf_p_set_debug, nla_p_set_debug);
1338c36c90a2SKristof Provost 
1339c36c90a2SKristof Provost static int
1340c36c90a2SKristof Provost pf_handle_set_debug(struct nlmsghdr *hdr, struct nl_pstate *npt)
1341c36c90a2SKristof Provost {
1342c36c90a2SKristof Provost 	struct pf_nl_set_debug attrs = {};
1343c36c90a2SKristof Provost 	int error;
1344c36c90a2SKristof Provost 
1345c36c90a2SKristof Provost 	error = nl_parse_nlmsg(hdr, &set_debug_parser, npt, &attrs);
1346c36c90a2SKristof Provost 	if (error != 0)
1347c36c90a2SKristof Provost 		return (error);
1348c36c90a2SKristof Provost 
1349c36c90a2SKristof Provost 	PF_RULES_WLOCK();
1350c36c90a2SKristof Provost 	V_pf_status.debug = attrs.level;
1351c36c90a2SKristof Provost 	PF_RULES_WUNLOCK();
1352c36c90a2SKristof Provost 
1353c36c90a2SKristof Provost 	return (0);
1354c36c90a2SKristof Provost }
1355c36c90a2SKristof Provost 
135630bad751SKristof Provost struct pf_nl_set_timeout
135730bad751SKristof Provost {
135830bad751SKristof Provost 	uint32_t timeout;
135930bad751SKristof Provost 	uint32_t seconds;
136030bad751SKristof Provost };
136130bad751SKristof Provost #define	_OUT(_field)	offsetof(struct pf_nl_set_timeout, _field)
136230bad751SKristof Provost static const struct nlattr_parser nla_p_set_timeout[] = {
136330bad751SKristof Provost 	{ .type = PF_TO_TIMEOUT, .off = _OUT(timeout), .cb = nlattr_get_uint32 },
136430bad751SKristof Provost 	{ .type = PF_TO_SECONDS, .off = _OUT(seconds), .cb = nlattr_get_uint32 },
136530bad751SKristof Provost };
136630bad751SKristof Provost static const struct nlfield_parser nlf_p_set_timeout[] = {};
136730bad751SKristof Provost #undef _OUT
136830bad751SKristof Provost NL_DECLARE_PARSER(set_timeout_parser, struct genlmsghdr, nlf_p_set_timeout, nla_p_set_timeout);
136930bad751SKristof Provost 
137030bad751SKristof Provost static int
137130bad751SKristof Provost pf_handle_set_timeout(struct nlmsghdr *hdr, struct nl_pstate *npt)
137230bad751SKristof Provost {
137330bad751SKristof Provost 	struct pf_nl_set_timeout attrs = {};
137430bad751SKristof Provost 	int error;
137530bad751SKristof Provost 
137630bad751SKristof Provost 	error = nl_parse_nlmsg(hdr, &set_timeout_parser, npt, &attrs);
137730bad751SKristof Provost 	if (error != 0)
137830bad751SKristof Provost 		return (error);
137930bad751SKristof Provost 
138030bad751SKristof Provost 	return (pf_ioctl_set_timeout(attrs.timeout, attrs.seconds, NULL));
138130bad751SKristof Provost }
138230bad751SKristof Provost 
138330bad751SKristof Provost static int
138430bad751SKristof Provost pf_handle_get_timeout(struct nlmsghdr *hdr, struct nl_pstate *npt)
138530bad751SKristof Provost {
138630bad751SKristof Provost 	struct pf_nl_set_timeout attrs = {};
138730bad751SKristof Provost 	struct nl_writer *nw = npt->nw;
138830bad751SKristof Provost 	struct genlmsghdr *ghdr_new;
138930bad751SKristof Provost 	int error;
139030bad751SKristof Provost 
139130bad751SKristof Provost 	error = nl_parse_nlmsg(hdr, &set_timeout_parser, npt, &attrs);
139230bad751SKristof Provost 	if (error != 0)
139330bad751SKristof Provost 		return (error);
139430bad751SKristof Provost 
139530bad751SKristof Provost 	error = pf_ioctl_get_timeout(attrs.timeout, &attrs.seconds);
139630bad751SKristof Provost 	if (error != 0)
139730bad751SKristof Provost 		return (error);
139830bad751SKristof Provost 
139930bad751SKristof Provost 	if (!nlmsg_reply(nw, hdr, sizeof(struct genlmsghdr)))
140030bad751SKristof Provost 		return (ENOMEM);
140130bad751SKristof Provost 
140230bad751SKristof Provost 	ghdr_new = nlmsg_reserve_object(nw, struct genlmsghdr);
140330bad751SKristof Provost 	ghdr_new->cmd = PFNL_CMD_GET_TIMEOUT;
140430bad751SKristof Provost 	ghdr_new->version = 0;
140530bad751SKristof Provost 	ghdr_new->reserved = 0;
140630bad751SKristof Provost 
140730bad751SKristof Provost 	nlattr_add_u32(nw, PF_TO_SECONDS, attrs.seconds);
140830bad751SKristof Provost 
140930bad751SKristof Provost 	if (!nlmsg_end(nw)) {
141030bad751SKristof Provost 		nlmsg_abort(nw);
141130bad751SKristof Provost 		return (ENOMEM);
141230bad751SKristof Provost 	}
141330bad751SKristof Provost 
141430bad751SKristof Provost 	return (0);
141530bad751SKristof Provost }
141630bad751SKristof Provost 
1417d9ab8999SKristof Provost struct pf_nl_set_limit
1418d9ab8999SKristof Provost {
1419d9ab8999SKristof Provost 	uint32_t index;
1420d9ab8999SKristof Provost 	uint32_t limit;
1421d9ab8999SKristof Provost };
1422d9ab8999SKristof Provost #define	_OUT(_field)	offsetof(struct pf_nl_set_limit, _field)
1423d9ab8999SKristof Provost static const struct nlattr_parser nla_p_set_limit[] = {
1424d9ab8999SKristof Provost 	{ .type = PF_LI_INDEX, .off = _OUT(index), .cb = nlattr_get_uint32 },
1425d9ab8999SKristof Provost 	{ .type = PF_LI_LIMIT, .off = _OUT(limit), .cb = nlattr_get_uint32 },
1426d9ab8999SKristof Provost };
1427d9ab8999SKristof Provost static const struct nlfield_parser nlf_p_set_limit[] = {};
1428d9ab8999SKristof Provost #undef _OUT
1429d9ab8999SKristof Provost NL_DECLARE_PARSER(set_limit_parser, struct genlmsghdr, nlf_p_set_limit, nla_p_set_limit);
1430d9ab8999SKristof Provost 
1431d9ab8999SKristof Provost static int
1432d9ab8999SKristof Provost pf_handle_set_limit(struct nlmsghdr *hdr, struct nl_pstate *npt)
1433d9ab8999SKristof Provost {
1434d9ab8999SKristof Provost 	struct pf_nl_set_limit attrs = {};
1435d9ab8999SKristof Provost 	int error;
1436d9ab8999SKristof Provost 
1437d9ab8999SKristof Provost 	error = nl_parse_nlmsg(hdr, &set_limit_parser, npt, &attrs);
1438d9ab8999SKristof Provost 	if (error != 0)
1439d9ab8999SKristof Provost 		return (error);
1440d9ab8999SKristof Provost 
1441d9ab8999SKristof Provost 	return (pf_ioctl_set_limit(attrs.index, attrs.limit, NULL));
1442d9ab8999SKristof Provost }
1443d9ab8999SKristof Provost 
1444d9ab8999SKristof Provost static int
1445d9ab8999SKristof Provost pf_handle_get_limit(struct nlmsghdr *hdr, struct nl_pstate *npt)
1446d9ab8999SKristof Provost {
1447d9ab8999SKristof Provost 	struct pf_nl_set_limit attrs = {};
1448d9ab8999SKristof Provost 	struct nl_writer *nw = npt->nw;
1449d9ab8999SKristof Provost 	struct genlmsghdr *ghdr_new;
1450d9ab8999SKristof Provost 	int error;
1451d9ab8999SKristof Provost 
1452d9ab8999SKristof Provost 	error = nl_parse_nlmsg(hdr, &set_limit_parser, npt, &attrs);
1453d9ab8999SKristof Provost 	if (error != 0)
1454d9ab8999SKristof Provost 		return (error);
1455d9ab8999SKristof Provost 
1456d9ab8999SKristof Provost 	error = pf_ioctl_get_limit(attrs.index, &attrs.limit);
1457d9ab8999SKristof Provost 	if (error != 0)
1458d9ab8999SKristof Provost 		return (error);
1459d9ab8999SKristof Provost 
1460d9ab8999SKristof Provost 	if (!nlmsg_reply(nw, hdr, sizeof(struct genlmsghdr)))
1461d9ab8999SKristof Provost 		return (ENOMEM);
1462d9ab8999SKristof Provost 
1463d9ab8999SKristof Provost 	ghdr_new = nlmsg_reserve_object(nw, struct genlmsghdr);
1464d9ab8999SKristof Provost 	ghdr_new->cmd = PFNL_CMD_GET_LIMIT;
1465d9ab8999SKristof Provost 	ghdr_new->version = 0;
1466d9ab8999SKristof Provost 	ghdr_new->reserved = 0;
1467d9ab8999SKristof Provost 
1468d9ab8999SKristof Provost 	nlattr_add_u32(nw, PF_LI_LIMIT, attrs.limit);
1469d9ab8999SKristof Provost 
1470d9ab8999SKristof Provost 	if (!nlmsg_end(nw)) {
1471d9ab8999SKristof Provost 		nlmsg_abort(nw);
1472d9ab8999SKristof Provost 		return (ENOMEM);
1473d9ab8999SKristof Provost 	}
1474d9ab8999SKristof Provost 
1475d9ab8999SKristof Provost 	return (0);
1476d9ab8999SKristof Provost }
1477d9ab8999SKristof Provost 
1478ba2a9207SKristof Provost static int
1479ba2a9207SKristof Provost pf_handle_begin_addrs(struct nlmsghdr *hdr, struct nl_pstate *npt)
1480ba2a9207SKristof Provost {
1481ba2a9207SKristof Provost 	struct nl_writer *nw = npt->nw;
1482ba2a9207SKristof Provost 	struct genlmsghdr *ghdr_new;
1483ba2a9207SKristof Provost 	uint32_t ticket;
1484ba2a9207SKristof Provost 	int error;
1485ba2a9207SKristof Provost 
1486ba2a9207SKristof Provost 	error = pf_ioctl_begin_addrs(&ticket);
1487ba2a9207SKristof Provost 	if (error != 0)
1488ba2a9207SKristof Provost 		return (error);
1489ba2a9207SKristof Provost 
1490ba2a9207SKristof Provost 	if (!nlmsg_reply(nw, hdr, sizeof(struct genlmsghdr)))
1491ba2a9207SKristof Provost 		return (ENOMEM);
1492ba2a9207SKristof Provost 
1493ba2a9207SKristof Provost 	ghdr_new = nlmsg_reserve_object(nw, struct genlmsghdr);
1494ba2a9207SKristof Provost 	ghdr_new->cmd = PFNL_CMD_BEGIN_ADDRS;
1495ba2a9207SKristof Provost 	ghdr_new->version = 0;
1496ba2a9207SKristof Provost 	ghdr_new->reserved = 0;
1497ba2a9207SKristof Provost 
1498ba2a9207SKristof Provost 	nlattr_add_u32(nw, PF_BA_TICKET, ticket);
1499ba2a9207SKristof Provost 
1500ba2a9207SKristof Provost 	if (!nlmsg_end(nw)) {
1501ba2a9207SKristof Provost 		nlmsg_abort(nw);
1502ba2a9207SKristof Provost 		return (ENOMEM);
1503ba2a9207SKristof Provost 	}
1504ba2a9207SKristof Provost 
1505ba2a9207SKristof Provost 	return (0);
1506ba2a9207SKristof Provost }
1507ba2a9207SKristof Provost 
1508644b7b5aSKristof Provost static bool
1509644b7b5aSKristof Provost nlattr_add_pool_addr(struct nl_writer *nw, int attrtype, struct pf_pooladdr *a)
1510644b7b5aSKristof Provost {
1511644b7b5aSKristof Provost 	int off;
1512644b7b5aSKristof Provost 
1513644b7b5aSKristof Provost 	off = nlattr_add_nested(nw, attrtype);
1514644b7b5aSKristof Provost 
1515644b7b5aSKristof Provost 	nlattr_add_addr_wrap(nw, PF_PA_ADDR, &a->addr);
1516644b7b5aSKristof Provost 	nlattr_add_string(nw, PF_PA_IFNAME, a->ifname);
1517644b7b5aSKristof Provost 
1518644b7b5aSKristof Provost 	nlattr_set_len(nw, off);
1519644b7b5aSKristof Provost 
1520644b7b5aSKristof Provost 	return (true);
1521644b7b5aSKristof Provost }
1522644b7b5aSKristof Provost 
1523d909f06bSKristof Provost #define _OUT(_field)	offsetof(struct pf_pooladdr, _field)
1524d909f06bSKristof Provost static const struct nlattr_parser nla_p_pool_addr[] = {
1525d909f06bSKristof Provost 	{ .type = PF_PA_ADDR, .off = _OUT(addr), .arg = &addr_wrap_parser, .cb = nlattr_get_nested },
1526d909f06bSKristof Provost 	{ .type = PF_PA_IFNAME, .off = _OUT(ifname), .arg = (void *)IFNAMSIZ, .cb = nlattr_get_chara },
1527d909f06bSKristof Provost };
1528d909f06bSKristof Provost NL_DECLARE_ATTR_PARSER(pool_addr_parser, nla_p_pool_addr);
1529d909f06bSKristof Provost #undef _OUT
1530d909f06bSKristof Provost 
1531d909f06bSKristof Provost #define	_OUT(_field)	offsetof(struct pfioc_pooladdr, _field)
1532d909f06bSKristof Provost static const struct nlattr_parser nla_p_add_addr[] = {
1533d909f06bSKristof Provost 	{ .type = PF_AA_ACTION, .off = _OUT(action), .cb = nlattr_get_uint32 },
1534d909f06bSKristof Provost 	{ .type = PF_AA_TICKET, .off = _OUT(ticket), .cb = nlattr_get_uint32 },
1535d909f06bSKristof Provost 	{ .type = PF_AA_NR, .off = _OUT(nr), .cb = nlattr_get_uint32 },
1536d909f06bSKristof Provost 	{ .type = PF_AA_R_NUM, .off = _OUT(r_num), .cb = nlattr_get_uint32 },
1537d909f06bSKristof Provost 	{ .type = PF_AA_R_ACTION, .off = _OUT(r_action), .cb = nlattr_get_uint8 },
1538d909f06bSKristof Provost 	{ .type = PF_AA_R_LAST, .off = _OUT(r_last), .cb = nlattr_get_uint8 },
1539d909f06bSKristof Provost 	{ .type = PF_AA_AF, .off = _OUT(af), .cb = nlattr_get_uint8 },
1540d909f06bSKristof Provost 	{ .type = PF_AA_ANCHOR, .off = _OUT(anchor), .arg = (void *)MAXPATHLEN, .cb = nlattr_get_chara },
1541d909f06bSKristof Provost 	{ .type = PF_AA_ADDR, .off = _OUT(addr), .arg = &pool_addr_parser, .cb = nlattr_get_nested },
1542d909f06bSKristof Provost };
1543d909f06bSKristof Provost static const struct nlfield_parser nlf_p_add_addr[] = {};
1544d909f06bSKristof Provost #undef _OUT
1545d909f06bSKristof Provost NL_DECLARE_PARSER(add_addr_parser, struct genlmsghdr, nlf_p_add_addr, nla_p_add_addr);
1546d909f06bSKristof Provost 
1547d909f06bSKristof Provost static int
1548d909f06bSKristof Provost pf_handle_add_addr(struct nlmsghdr *hdr, struct nl_pstate *npt)
1549d909f06bSKristof Provost {
1550d909f06bSKristof Provost 	struct pfioc_pooladdr attrs = { 0 };
1551d909f06bSKristof Provost 	int error;
1552d909f06bSKristof Provost 
1553d909f06bSKristof Provost 	error = nl_parse_nlmsg(hdr, &add_addr_parser, npt, &attrs);
1554d909f06bSKristof Provost 	if (error != 0)
1555d909f06bSKristof Provost 		return (error);
1556d909f06bSKristof Provost 
1557d909f06bSKristof Provost 	error = pf_ioctl_add_addr(&attrs);
1558d909f06bSKristof Provost 
1559d909f06bSKristof Provost 	return (error);
1560d909f06bSKristof Provost }
1561d909f06bSKristof Provost 
1562644b7b5aSKristof Provost static int
1563644b7b5aSKristof Provost pf_handle_get_addrs(struct nlmsghdr *hdr, struct nl_pstate *npt)
1564644b7b5aSKristof Provost {
1565644b7b5aSKristof Provost 	struct pfioc_pooladdr attrs = { 0 };
1566644b7b5aSKristof Provost 	struct nl_writer *nw = npt->nw;
1567644b7b5aSKristof Provost 	struct genlmsghdr *ghdr_new;
1568644b7b5aSKristof Provost 	int error;
1569644b7b5aSKristof Provost 
1570644b7b5aSKristof Provost 	error = nl_parse_nlmsg(hdr, &add_addr_parser, npt, &attrs);
1571644b7b5aSKristof Provost 	if (error != 0)
1572644b7b5aSKristof Provost 		return (error);
1573644b7b5aSKristof Provost 
1574644b7b5aSKristof Provost 	error = pf_ioctl_get_addrs(&attrs);
1575644b7b5aSKristof Provost 	if (error != 0)
1576644b7b5aSKristof Provost 		return (error);
1577644b7b5aSKristof Provost 
1578644b7b5aSKristof Provost 	if (!nlmsg_reply(nw, hdr, sizeof(struct genlmsghdr)))
1579644b7b5aSKristof Provost 		return (ENOMEM);
1580644b7b5aSKristof Provost 
1581644b7b5aSKristof Provost 	ghdr_new = nlmsg_reserve_object(nw, struct genlmsghdr);
1582644b7b5aSKristof Provost 	ghdr_new->cmd = PFNL_CMD_GET_ADDRS;
1583644b7b5aSKristof Provost 	ghdr_new->version = 0;
1584644b7b5aSKristof Provost 	ghdr_new->reserved = 0;
1585644b7b5aSKristof Provost 
1586644b7b5aSKristof Provost 	nlattr_add_u32(nw, PF_AA_NR, attrs.nr);
1587644b7b5aSKristof Provost 
1588644b7b5aSKristof Provost 	if (!nlmsg_end(nw)) {
1589644b7b5aSKristof Provost 		nlmsg_abort(nw);
1590644b7b5aSKristof Provost 		return (ENOMEM);
1591644b7b5aSKristof Provost 	}
1592644b7b5aSKristof Provost 
1593644b7b5aSKristof Provost 	return (error);
1594644b7b5aSKristof Provost }
1595644b7b5aSKristof Provost 
15969ae91f59SKristof Provost static int
15979ae91f59SKristof Provost pf_handle_get_addr(struct nlmsghdr *hdr, struct nl_pstate *npt)
15989ae91f59SKristof Provost {
15999ae91f59SKristof Provost 	struct pfioc_pooladdr attrs = { 0 };
16009ae91f59SKristof Provost 	struct nl_writer *nw = npt->nw;
16019ae91f59SKristof Provost 	struct genlmsghdr *ghdr_new;
16029ae91f59SKristof Provost 	int error;
16039ae91f59SKristof Provost 
16049ae91f59SKristof Provost 	error = nl_parse_nlmsg(hdr, &add_addr_parser, npt, &attrs);
16059ae91f59SKristof Provost 	if (error != 0)
16069ae91f59SKristof Provost 		return (error);
16079ae91f59SKristof Provost 
16089ae91f59SKristof Provost 	error = pf_ioctl_get_addr(&attrs);
16099ae91f59SKristof Provost 	if (error != 0)
16109ae91f59SKristof Provost 		return (error);
16119ae91f59SKristof Provost 
16129ae91f59SKristof Provost 	if (!nlmsg_reply(nw, hdr, sizeof(struct genlmsghdr)))
16139ae91f59SKristof Provost 		return (ENOMEM);
16149ae91f59SKristof Provost 
16159ae91f59SKristof Provost 	ghdr_new = nlmsg_reserve_object(nw, struct genlmsghdr);
16169ae91f59SKristof Provost 	ghdr_new->cmd = PFNL_CMD_GET_ADDRS;
16179ae91f59SKristof Provost 	ghdr_new->version = 0;
16189ae91f59SKristof Provost 	ghdr_new->reserved = 0;
16199ae91f59SKristof Provost 
16209ae91f59SKristof Provost 	nlattr_add_u32(nw, PF_AA_ACTION, attrs.action);
16219ae91f59SKristof Provost 	nlattr_add_u32(nw, PF_AA_TICKET, attrs.ticket);
16229ae91f59SKristof Provost 	nlattr_add_u32(nw, PF_AA_NR, attrs.nr);
16239ae91f59SKristof Provost 	nlattr_add_u32(nw, PF_AA_R_NUM, attrs.r_num);
16249ae91f59SKristof Provost 	nlattr_add_u8(nw, PF_AA_R_ACTION, attrs.r_action);
16259ae91f59SKristof Provost 	nlattr_add_u8(nw, PF_AA_R_LAST, attrs.r_last);
16269ae91f59SKristof Provost 	nlattr_add_u8(nw, PF_AA_AF, attrs.af);
16279ae91f59SKristof Provost 	nlattr_add_string(nw, PF_AA_ANCHOR, attrs.anchor);
16289ae91f59SKristof Provost 	nlattr_add_pool_addr(nw, PF_AA_ADDR, &attrs.addr);
16299ae91f59SKristof Provost 
16309ae91f59SKristof Provost 	if (!nlmsg_end(nw)) {
16319ae91f59SKristof Provost 		nlmsg_abort(nw);
16329ae91f59SKristof Provost 		return (ENOMEM);
16339ae91f59SKristof Provost 	}
16349ae91f59SKristof Provost 
16359ae91f59SKristof Provost 	return (0);
16369ae91f59SKristof Provost }
16379ae91f59SKristof Provost 
163825e0f8f9SKristof Provost #define _OUT(_field)	offsetof(struct pfioc_ruleset, _field)
163925e0f8f9SKristof Provost static const struct nlattr_parser nla_p_ruleset[] = {
164025e0f8f9SKristof Provost 	{ .type = PF_RS_PATH, .off = _OUT(path), .arg = (void *)MAXPATHLEN, .cb = nlattr_get_chara },
164148f5bf8bSKristof Provost 	{ .type = PF_RS_NR, .off = _OUT(nr), .cb = nlattr_get_uint32 },
164225e0f8f9SKristof Provost };
164325e0f8f9SKristof Provost static const struct nlfield_parser nlf_p_ruleset[] = {
164425e0f8f9SKristof Provost };
164525e0f8f9SKristof Provost NL_DECLARE_PARSER(ruleset_parser, struct genlmsghdr, nlf_p_ruleset, nla_p_ruleset);
164625e0f8f9SKristof Provost #undef _OUT
164725e0f8f9SKristof Provost 
164825e0f8f9SKristof Provost static int
164925e0f8f9SKristof Provost pf_handle_get_rulesets(struct nlmsghdr *hdr, struct nl_pstate *npt)
165025e0f8f9SKristof Provost {
165125e0f8f9SKristof Provost 	struct pfioc_ruleset attrs = { 0 };
165225e0f8f9SKristof Provost 	struct nl_writer *nw = npt->nw;
165325e0f8f9SKristof Provost 	struct genlmsghdr *ghdr_new;
165425e0f8f9SKristof Provost 	int error;
165525e0f8f9SKristof Provost 
165625e0f8f9SKristof Provost 	error = nl_parse_nlmsg(hdr, &ruleset_parser, npt, &attrs);
165725e0f8f9SKristof Provost 	if (error != 0)
165825e0f8f9SKristof Provost 		return (error);
165925e0f8f9SKristof Provost 
166025e0f8f9SKristof Provost 	error = pf_ioctl_get_rulesets(&attrs);
166125e0f8f9SKristof Provost 	if (error != 0)
166225e0f8f9SKristof Provost 		return (error);
166325e0f8f9SKristof Provost 
166425e0f8f9SKristof Provost 	if (!nlmsg_reply(nw, hdr, sizeof(struct genlmsghdr)))
166525e0f8f9SKristof Provost 		return (ENOMEM);
166625e0f8f9SKristof Provost 
166725e0f8f9SKristof Provost 	ghdr_new = nlmsg_reserve_object(nw, struct genlmsghdr);
166825e0f8f9SKristof Provost 	ghdr_new->cmd = PFNL_CMD_GET_RULESETS;
166925e0f8f9SKristof Provost 	ghdr_new->version = 0;
167025e0f8f9SKristof Provost 	ghdr_new->reserved = 0;
167125e0f8f9SKristof Provost 
167225e0f8f9SKristof Provost 	nlattr_add_u32(nw, PF_RS_NR, attrs.nr);
167325e0f8f9SKristof Provost 
167425e0f8f9SKristof Provost 	if (!nlmsg_end(nw)) {
167525e0f8f9SKristof Provost 		nlmsg_abort(nw);
167625e0f8f9SKristof Provost 		return (ENOMEM);
167725e0f8f9SKristof Provost 	}
167825e0f8f9SKristof Provost 
167925e0f8f9SKristof Provost 	return (0);
168025e0f8f9SKristof Provost }
168125e0f8f9SKristof Provost 
168248f5bf8bSKristof Provost static int
168348f5bf8bSKristof Provost pf_handle_get_ruleset(struct nlmsghdr *hdr, struct nl_pstate *npt)
168448f5bf8bSKristof Provost {
168548f5bf8bSKristof Provost 	struct pfioc_ruleset attrs = { 0 };
168648f5bf8bSKristof Provost 	struct nl_writer *nw = npt->nw;
168748f5bf8bSKristof Provost 	struct genlmsghdr *ghdr_new;
168848f5bf8bSKristof Provost 	int error;
168948f5bf8bSKristof Provost 
169048f5bf8bSKristof Provost 	error = nl_parse_nlmsg(hdr, &ruleset_parser, npt, &attrs);
169148f5bf8bSKristof Provost 	if (error)
169248f5bf8bSKristof Provost 		return (error);
169348f5bf8bSKristof Provost 
169448f5bf8bSKristof Provost 	error = pf_ioctl_get_ruleset(&attrs);
169548f5bf8bSKristof Provost 	if (error != 0)
169648f5bf8bSKristof Provost 		return (error);
169748f5bf8bSKristof Provost 
169848f5bf8bSKristof Provost 	if (!nlmsg_reply(nw, hdr, sizeof(struct genlmsghdr)))
169948f5bf8bSKristof Provost 		return (ENOMEM);
170048f5bf8bSKristof Provost 
170148f5bf8bSKristof Provost 	ghdr_new = nlmsg_reserve_object(nw, struct genlmsghdr);
170248f5bf8bSKristof Provost 	ghdr_new->cmd = PFNL_CMD_GET_RULESET;
170348f5bf8bSKristof Provost 	ghdr_new->version = 0;
170448f5bf8bSKristof Provost 	ghdr_new->reserved = 0;
170548f5bf8bSKristof Provost 
170648f5bf8bSKristof Provost 	nlattr_add_string(nw, PF_RS_NAME, attrs.name);
170748f5bf8bSKristof Provost 
170848f5bf8bSKristof Provost 	if (!nlmsg_end(nw)) {
170948f5bf8bSKristof Provost 		nlmsg_abort(nw);
171048f5bf8bSKristof Provost 		return (ENOMEM);
171148f5bf8bSKristof Provost 	}
171248f5bf8bSKristof Provost 
171348f5bf8bSKristof Provost 	return (0);
171448f5bf8bSKristof Provost }
171548f5bf8bSKristof Provost 
1716*9c125336SKristof Provost static bool
1717*9c125336SKristof Provost nlattr_add_pf_threshold(struct nl_writer *nw, int attrtype, struct pf_threshold *t)
1718*9c125336SKristof Provost {
1719*9c125336SKristof Provost 	int off = nlattr_add_nested(nw, attrtype);
1720*9c125336SKristof Provost 
1721*9c125336SKristof Provost 	nlattr_add_u32(nw, PF_TH_LIMIT, t->limit);
1722*9c125336SKristof Provost 	nlattr_add_u32(nw, PF_TH_SECONDS, t->seconds);
1723*9c125336SKristof Provost 	nlattr_add_u32(nw, PF_TH_COUNT, t->count);
1724*9c125336SKristof Provost 	nlattr_add_u32(nw, PF_TH_LAST, t->last);
1725*9c125336SKristof Provost 
1726*9c125336SKristof Provost 	nlattr_set_len(nw, off);
1727*9c125336SKristof Provost 
1728*9c125336SKristof Provost 	return (true);
1729*9c125336SKristof Provost }
1730*9c125336SKristof Provost 
1731*9c125336SKristof Provost static int
1732*9c125336SKristof Provost pf_handle_get_srcnodes(struct nlmsghdr *hdr, struct nl_pstate *npt)
1733*9c125336SKristof Provost {
1734*9c125336SKristof Provost 	struct nl_writer	*nw = npt->nw;
1735*9c125336SKristof Provost 	struct genlmsghdr	*ghdr_new;
1736*9c125336SKristof Provost 	struct pf_ksrc_node	*n;
1737*9c125336SKristof Provost 	struct pf_srchash	*sh;
1738*9c125336SKristof Provost 	int			 i;
1739*9c125336SKristof Provost 
1740*9c125336SKristof Provost 	hdr->nlmsg_flags |= NLM_F_MULTI;
1741*9c125336SKristof Provost 
1742*9c125336SKristof Provost 	for (i = 0, sh = V_pf_srchash; i <= V_pf_srchashmask;
1743*9c125336SKristof Provost 	    i++, sh++) {
1744*9c125336SKristof Provost 		/* Avoid locking empty rows. */
1745*9c125336SKristof Provost 		if (LIST_EMPTY(&sh->nodes))
1746*9c125336SKristof Provost 			continue;
1747*9c125336SKristof Provost 
1748*9c125336SKristof Provost 		PF_HASHROW_LOCK(sh);
1749*9c125336SKristof Provost 		LIST_FOREACH(n, &sh->nodes, entry) {
1750*9c125336SKristof Provost 			if (!nlmsg_reply(nw, hdr, sizeof(struct genlmsghdr))) {
1751*9c125336SKristof Provost 				nlmsg_abort(nw);
1752*9c125336SKristof Provost 				return (ENOMEM);
1753*9c125336SKristof Provost 			}
1754*9c125336SKristof Provost 
1755*9c125336SKristof Provost 			ghdr_new = nlmsg_reserve_object(nw, struct genlmsghdr);
1756*9c125336SKristof Provost 			ghdr_new->cmd = PFNL_CMD_GET_SRCNODES;
1757*9c125336SKristof Provost 			ghdr_new->version = 0;
1758*9c125336SKristof Provost 			ghdr_new->reserved = 0;
1759*9c125336SKristof Provost 
1760*9c125336SKristof Provost 			nlattr_add_in6_addr(nw, PF_SN_ADDR, &n->addr.v6);
1761*9c125336SKristof Provost 			nlattr_add_in6_addr(nw, PF_SN_RADDR, &n->raddr.v6);
1762*9c125336SKristof Provost 			nlattr_add_u32(nw, PF_SN_RULE_NR, n->rule->nr);
1763*9c125336SKristof Provost 			nlattr_add_u64(nw, PF_SN_BYTES_IN, counter_u64_fetch(n->bytes[0]));
1764*9c125336SKristof Provost 			nlattr_add_u64(nw, PF_SN_BYTES_OUT, counter_u64_fetch(n->bytes[1]));
1765*9c125336SKristof Provost 			nlattr_add_u64(nw, PF_SN_PACKETS_IN, counter_u64_fetch(n->packets[0]));
1766*9c125336SKristof Provost 			nlattr_add_u64(nw, PF_SN_PACKETS_OUT, counter_u64_fetch(n->packets[1]));
1767*9c125336SKristof Provost 			nlattr_add_u32(nw, PF_SN_STATES, n->states);
1768*9c125336SKristof Provost 			nlattr_add_u32(nw, PF_SN_CONNECTIONS, n->conn);
1769*9c125336SKristof Provost 			nlattr_add_u8(nw, PF_SN_AF, n->af);
1770*9c125336SKristof Provost 			nlattr_add_u8(nw, PF_SN_RULE_TYPE, n->ruletype);
1771*9c125336SKristof Provost 			nlattr_add_u64(nw, PF_SN_CREATION, n->creation);
1772*9c125336SKristof Provost 			nlattr_add_u64(nw, PF_SN_EXPIRE, n->expire);
1773*9c125336SKristof Provost 			nlattr_add_pf_threshold(nw, PF_SN_CONNECTION_RATE, &n->conn_rate);
1774*9c125336SKristof Provost 
1775*9c125336SKristof Provost 			if (!nlmsg_end(nw)) {
1776*9c125336SKristof Provost 				PF_HASHROW_UNLOCK(sh);
1777*9c125336SKristof Provost 				nlmsg_abort(nw);
1778*9c125336SKristof Provost 				return (ENOMEM);
1779*9c125336SKristof Provost 			}
1780*9c125336SKristof Provost 		}
1781*9c125336SKristof Provost 		PF_HASHROW_UNLOCK(sh);
1782*9c125336SKristof Provost 	}
1783*9c125336SKristof Provost 
1784*9c125336SKristof Provost 	return (0);
1785*9c125336SKristof Provost }
1786*9c125336SKristof Provost 
178744f323ecSKristof Provost static const struct nlhdr_parser *all_parsers[] = {
178844f323ecSKristof Provost 	&state_parser,
178944f323ecSKristof Provost 	&addrule_parser,
1790706d465dSKristof Provost 	&getrules_parser,
1791706d465dSKristof Provost 	&clear_states_parser,
1792470a2b33SKristof Provost 	&set_statusif_parser,
179371d3c704SKristof Provost 	&natlook_parser,
1794c36c90a2SKristof Provost 	&set_debug_parser,
179530bad751SKristof Provost 	&set_timeout_parser,
1796d9ab8999SKristof Provost 	&set_limit_parser,
1797d909f06bSKristof Provost 	&pool_addr_parser,
1798d909f06bSKristof Provost 	&add_addr_parser,
179925e0f8f9SKristof Provost 	&ruleset_parser,
180044f323ecSKristof Provost };
18012cef6288SAlexander V. Chernikov 
18022cef6288SAlexander V. Chernikov static int family_id;
18032cef6288SAlexander V. Chernikov 
18042cef6288SAlexander V. Chernikov static const struct genl_cmd pf_cmds[] = {
18052cef6288SAlexander V. Chernikov 	{
18062cef6288SAlexander V. Chernikov 		.cmd_num = PFNL_CMD_GETSTATES,
18072cef6288SAlexander V. Chernikov 		.cmd_name = "GETSTATES",
18082cef6288SAlexander V. Chernikov 		.cmd_cb = pf_handle_getstates,
18092cef6288SAlexander V. Chernikov 		.cmd_flags = GENL_CMD_CAP_DO | GENL_CMD_CAP_DUMP | GENL_CMD_CAP_HASPOL,
1810e774c1efSKristof Provost 		.cmd_priv = PRIV_NETINET_PF,
18112cef6288SAlexander V. Chernikov 	},
1812a7191e5dSKristof Provost 	{
1813a7191e5dSKristof Provost 		.cmd_num = PFNL_CMD_GETCREATORS,
1814a7191e5dSKristof Provost 		.cmd_name = "GETCREATORS",
1815a7191e5dSKristof Provost 		.cmd_cb = pf_handle_getcreators,
1816a7191e5dSKristof Provost 		.cmd_flags = GENL_CMD_CAP_DO | GENL_CMD_CAP_DUMP | GENL_CMD_CAP_HASPOL,
1817e774c1efSKristof Provost 		.cmd_priv = PRIV_NETINET_PF,
1818a7191e5dSKristof Provost 	},
181981647eb6SKristof Provost 	{
182081647eb6SKristof Provost 		.cmd_num = PFNL_CMD_START,
182181647eb6SKristof Provost 		.cmd_name = "START",
182281647eb6SKristof Provost 		.cmd_cb = pf_handle_start,
182381647eb6SKristof Provost 		.cmd_flags = GENL_CMD_CAP_DO | GENL_CMD_CAP_HASPOL,
1824e774c1efSKristof Provost 		.cmd_priv = PRIV_NETINET_PF,
182581647eb6SKristof Provost 	},
182681647eb6SKristof Provost 	{
182781647eb6SKristof Provost 		.cmd_num = PFNL_CMD_STOP,
182881647eb6SKristof Provost 		.cmd_name = "STOP",
182981647eb6SKristof Provost 		.cmd_cb = pf_handle_stop,
183081647eb6SKristof Provost 		.cmd_flags = GENL_CMD_CAP_DO | GENL_CMD_CAP_HASPOL,
1831e774c1efSKristof Provost 		.cmd_priv = PRIV_NETINET_PF,
183281647eb6SKristof Provost 	},
1833ffbf2595SKristof Provost 	{
1834ffbf2595SKristof Provost 		.cmd_num = PFNL_CMD_ADDRULE,
1835ffbf2595SKristof Provost 		.cmd_name = "ADDRULE",
1836ffbf2595SKristof Provost 		.cmd_cb = pf_handle_addrule,
1837ffbf2595SKristof Provost 		.cmd_flags = GENL_CMD_CAP_DO | GENL_CMD_CAP_DUMP | GENL_CMD_CAP_HASPOL,
1838e774c1efSKristof Provost 		.cmd_priv = PRIV_NETINET_PF,
1839ffbf2595SKristof Provost 	},
184044f323ecSKristof Provost 	{
184144f323ecSKristof Provost 		.cmd_num = PFNL_CMD_GETRULES,
184244f323ecSKristof Provost 		.cmd_name = "GETRULES",
184344f323ecSKristof Provost 		.cmd_cb = pf_handle_getrules,
184444f323ecSKristof Provost 		.cmd_flags = GENL_CMD_CAP_DUMP | GENL_CMD_CAP_HASPOL,
1845e774c1efSKristof Provost 		.cmd_priv = PRIV_NETINET_PF,
184644f323ecSKristof Provost 	},
1847777a4702SKristof Provost 	{
1848777a4702SKristof Provost 		.cmd_num = PFNL_CMD_GETRULE,
1849777a4702SKristof Provost 		.cmd_name = "GETRULE",
1850777a4702SKristof Provost 		.cmd_cb = pf_handle_getrule,
1851777a4702SKristof Provost 		.cmd_flags = GENL_CMD_CAP_DUMP | GENL_CMD_CAP_HASPOL,
1852777a4702SKristof Provost 		.cmd_priv = PRIV_NETINET_PF,
1853777a4702SKristof Provost 	},
1854706d465dSKristof Provost 	{
1855706d465dSKristof Provost 		.cmd_num = PFNL_CMD_CLRSTATES,
1856706d465dSKristof Provost 		.cmd_name = "CLRSTATES",
1857706d465dSKristof Provost 		.cmd_cb = pf_handle_clear_states,
1858706d465dSKristof Provost 		.cmd_flags = GENL_CMD_CAP_DO | GENL_CMD_CAP_DUMP | GENL_CMD_CAP_HASPOL,
1859706d465dSKristof Provost 		.cmd_priv = PRIV_NETINET_PF,
1860706d465dSKristof Provost 	},
1861706d465dSKristof Provost 	{
1862706d465dSKristof Provost 		.cmd_num = PFNL_CMD_KILLSTATES,
1863706d465dSKristof Provost 		.cmd_name = "KILLSTATES",
1864706d465dSKristof Provost 		.cmd_cb = pf_handle_kill_states,
1865706d465dSKristof Provost 		.cmd_flags = GENL_CMD_CAP_DO | GENL_CMD_CAP_DUMP | GENL_CMD_CAP_HASPOL,
1866706d465dSKristof Provost 		.cmd_priv = PRIV_NETINET_PF,
1867706d465dSKristof Provost 	},
1868470a2b33SKristof Provost 	{
1869470a2b33SKristof Provost 		.cmd_num = PFNL_CMD_SET_STATUSIF,
1870470a2b33SKristof Provost 		.cmd_name = "SETSTATUSIF",
1871470a2b33SKristof Provost 		.cmd_cb = pf_handle_set_statusif,
1872470a2b33SKristof Provost 		.cmd_flags = GENL_CMD_CAP_DO | GENL_CMD_CAP_HASPOL,
1873470a2b33SKristof Provost 		.cmd_priv = PRIV_NETINET_PF,
18745824df8dSKristof Provost 	},
18755824df8dSKristof Provost 	{
18765824df8dSKristof Provost 		.cmd_num = PFNL_CMD_GET_STATUS,
18775824df8dSKristof Provost 		.cmd_name = "GETSTATUS",
18785824df8dSKristof Provost 		.cmd_cb = pf_handle_get_status,
18795824df8dSKristof Provost 		.cmd_flags = GENL_CMD_CAP_DUMP | GENL_CMD_CAP_HASPOL,
18805824df8dSKristof Provost 		.cmd_priv = PRIV_NETINET_PF,
18815824df8dSKristof Provost 	},
18829dbbe68bSKristof Provost 	{
18839dbbe68bSKristof Provost 		.cmd_num = PFNL_CMD_CLEAR_STATUS,
18849dbbe68bSKristof Provost 		.cmd_name = "CLEARSTATUS",
18859dbbe68bSKristof Provost 		.cmd_cb = pf_handle_clear_status,
18869dbbe68bSKristof Provost 		.cmd_flags = GENL_CMD_CAP_DO | GENL_CMD_CAP_HASPOL,
18879dbbe68bSKristof Provost 		.cmd_priv = PRIV_NETINET_PF,
18889dbbe68bSKristof Provost 	},
188971d3c704SKristof Provost 	{
189071d3c704SKristof Provost 		.cmd_num = PFNL_CMD_NATLOOK,
189171d3c704SKristof Provost 		.cmd_name = "NATLOOK",
189271d3c704SKristof Provost 		.cmd_cb = pf_handle_natlook,
189371d3c704SKristof Provost 		.cmd_flags = GENL_CMD_CAP_DUMP | GENL_CMD_CAP_HASPOL,
189471d3c704SKristof Provost 		.cmd_priv = PRIV_NETINET_PF,
189571d3c704SKristof Provost 	},
1896c36c90a2SKristof Provost 	{
1897c36c90a2SKristof Provost 		.cmd_num = PFNL_CMD_SET_DEBUG,
1898c36c90a2SKristof Provost 		.cmd_name = "SET_DEBUG",
1899c36c90a2SKristof Provost 		.cmd_cb = pf_handle_set_debug,
1900c36c90a2SKristof Provost 		.cmd_flags = GENL_CMD_CAP_DO | GENL_CMD_CAP_HASPOL,
1901c36c90a2SKristof Provost 		.cmd_priv = PRIV_NETINET_PF,
1902c36c90a2SKristof Provost 	},
190330bad751SKristof Provost 	{
190430bad751SKristof Provost 		.cmd_num = PFNL_CMD_SET_TIMEOUT,
190530bad751SKristof Provost 		.cmd_name = "SET_TIMEOUT",
190630bad751SKristof Provost 		.cmd_cb = pf_handle_set_timeout,
190730bad751SKristof Provost 		.cmd_flags = GENL_CMD_CAP_DO | GENL_CMD_CAP_HASPOL,
190830bad751SKristof Provost 		.cmd_priv = PRIV_NETINET_PF,
190930bad751SKristof Provost 	},
191030bad751SKristof Provost 	{
191130bad751SKristof Provost 		.cmd_num = PFNL_CMD_GET_TIMEOUT,
191230bad751SKristof Provost 		.cmd_name = "GET_TIMEOUT",
191330bad751SKristof Provost 		.cmd_cb = pf_handle_get_timeout,
191430bad751SKristof Provost 		.cmd_flags = GENL_CMD_CAP_DUMP | GENL_CMD_CAP_HASPOL,
191530bad751SKristof Provost 		.cmd_priv = PRIV_NETINET_PF,
191630bad751SKristof Provost 	},
1917d9ab8999SKristof Provost 	{
1918d9ab8999SKristof Provost 		.cmd_num = PFNL_CMD_SET_LIMIT,
1919d9ab8999SKristof Provost 		.cmd_name = "SET_LIMIT",
1920d9ab8999SKristof Provost 		.cmd_cb = pf_handle_set_limit,
1921d9ab8999SKristof Provost 		.cmd_flags = GENL_CMD_CAP_DO | GENL_CMD_CAP_HASPOL,
1922d9ab8999SKristof Provost 		.cmd_priv = PRIV_NETINET_PF,
1923d9ab8999SKristof Provost 	},
1924d9ab8999SKristof Provost 	{
1925d9ab8999SKristof Provost 		.cmd_num = PFNL_CMD_GET_LIMIT,
1926d9ab8999SKristof Provost 		.cmd_name = "GET_LIMIT",
1927d9ab8999SKristof Provost 		.cmd_cb = pf_handle_get_limit,
1928d9ab8999SKristof Provost 		.cmd_flags = GENL_CMD_CAP_DUMP | GENL_CMD_CAP_HASPOL,
1929d9ab8999SKristof Provost 		.cmd_priv = PRIV_NETINET_PF,
1930d9ab8999SKristof Provost 	},
1931ba2a9207SKristof Provost 	{
1932ba2a9207SKristof Provost 		.cmd_num = PFNL_CMD_BEGIN_ADDRS,
1933ba2a9207SKristof Provost 		.cmd_name = "BEGIN_ADDRS",
1934ba2a9207SKristof Provost 		.cmd_cb = pf_handle_begin_addrs,
1935ba2a9207SKristof Provost 		.cmd_flags = GENL_CMD_CAP_DO | GENL_CMD_CAP_DUMP | GENL_CMD_CAP_HASPOL,
1936ba2a9207SKristof Provost 		.cmd_priv = PRIV_NETINET_PF,
1937ba2a9207SKristof Provost 	},
1938d909f06bSKristof Provost 	{
1939d909f06bSKristof Provost 		.cmd_num = PFNL_CMD_ADD_ADDR,
1940d909f06bSKristof Provost 		.cmd_name = "ADD_ADDR",
1941d909f06bSKristof Provost 		.cmd_cb = pf_handle_add_addr,
1942d909f06bSKristof Provost 		.cmd_flags = GENL_CMD_CAP_DO | GENL_CMD_CAP_HASPOL,
1943d909f06bSKristof Provost 		.cmd_priv = PRIV_NETINET_PF,
1944d909f06bSKristof Provost 	},
1945644b7b5aSKristof Provost 	{
1946644b7b5aSKristof Provost 		.cmd_num = PFNL_CMD_GET_ADDRS,
1947644b7b5aSKristof Provost 		.cmd_name = "GET_ADDRS",
1948644b7b5aSKristof Provost 		.cmd_cb = pf_handle_get_addrs,
1949644b7b5aSKristof Provost 		.cmd_flags = GENL_CMD_CAP_DUMP | GENL_CMD_CAP_HASPOL,
1950644b7b5aSKristof Provost 		.cmd_priv = PRIV_NETINET_PF,
1951644b7b5aSKristof Provost 	},
19529ae91f59SKristof Provost 	{
19539ae91f59SKristof Provost 		.cmd_num = PFNL_CMD_GET_ADDR,
19549ae91f59SKristof Provost 		.cmd_name = "GET_ADDRS",
19559ae91f59SKristof Provost 		.cmd_cb = pf_handle_get_addr,
19569ae91f59SKristof Provost 		.cmd_flags = GENL_CMD_CAP_DUMP | GENL_CMD_CAP_HASPOL,
19579ae91f59SKristof Provost 		.cmd_priv = PRIV_NETINET_PF,
19589ae91f59SKristof Provost 	},
195925e0f8f9SKristof Provost 	{
196025e0f8f9SKristof Provost 		.cmd_num = PFNL_CMD_GET_RULESETS,
196125e0f8f9SKristof Provost 		.cmd_name = "GET_RULESETS",
196225e0f8f9SKristof Provost 		.cmd_cb = pf_handle_get_rulesets,
196325e0f8f9SKristof Provost 		.cmd_flags = GENL_CMD_CAP_DUMP | GENL_CMD_CAP_HASPOL,
196425e0f8f9SKristof Provost 		.cmd_priv = PRIV_NETINET_PF,
196525e0f8f9SKristof Provost 	},
196648f5bf8bSKristof Provost 	{
196748f5bf8bSKristof Provost 		.cmd_num = PFNL_CMD_GET_RULESET,
196848f5bf8bSKristof Provost 		.cmd_name = "GET_RULESET",
196948f5bf8bSKristof Provost 		.cmd_cb = pf_handle_get_ruleset,
197048f5bf8bSKristof Provost 		.cmd_flags = GENL_CMD_CAP_DUMP | GENL_CMD_CAP_HASPOL,
197148f5bf8bSKristof Provost 		.cmd_priv = PRIV_NETINET_PF,
197248f5bf8bSKristof Provost 	},
1973*9c125336SKristof Provost 	{
1974*9c125336SKristof Provost 		.cmd_num = PFNL_CMD_GET_SRCNODES,
1975*9c125336SKristof Provost 		.cmd_name = "GET_SRCNODES",
1976*9c125336SKristof Provost 		.cmd_cb = pf_handle_get_srcnodes,
1977*9c125336SKristof Provost 		.cmd_flags = GENL_CMD_CAP_DUMP | GENL_CMD_CAP_HASPOL,
1978*9c125336SKristof Provost 		.cmd_priv = PRIV_NETINET_PF,
1979*9c125336SKristof Provost 	},
19802cef6288SAlexander V. Chernikov };
19812cef6288SAlexander V. Chernikov 
19822cef6288SAlexander V. Chernikov void
19832cef6288SAlexander V. Chernikov pf_nl_register(void)
19842cef6288SAlexander V. Chernikov {
19852cef6288SAlexander V. Chernikov 	NL_VERIFY_PARSERS(all_parsers);
1986ffbf2595SKristof Provost 
19872cef6288SAlexander V. Chernikov 	family_id = genl_register_family(PFNL_FAMILY_NAME, 0, 2, PFNL_CMD_MAX);
19882cef6288SAlexander V. Chernikov 	genl_register_cmds(PFNL_FAMILY_NAME, pf_cmds, NL_ARRAY_LEN(pf_cmds));
19892cef6288SAlexander V. Chernikov }
19902cef6288SAlexander V. Chernikov 
19912cef6288SAlexander V. Chernikov void
19922cef6288SAlexander V. Chernikov pf_nl_unregister(void)
19932cef6288SAlexander V. Chernikov {
19942cef6288SAlexander V. Chernikov 	genl_unregister_family(PFNL_FAMILY_NAME);
19952cef6288SAlexander V. Chernikov }
1996