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