/*-
* Copyright (c) 2009-2025 The NetBSD Foundation, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* npfctl(8) data manipulation and helper routines.
*/
#include <sys/cdefs.h>
__RCSID("$NetBSD: npf_data.c,v 1.34 2025/07/01 19:55:15 joe Exp $");
/* If such interface exists or if it is a test interface - done. */
if (if_idx || testif) {
return;
}
/*
* Minimum sanity check. The interface name shall be non-empty
* string shorter than IFNAMSIZ and alphanumeric only.
*/
if (*p == '\0') {
goto err;
}
while (*p) {
const size_t len = (ptrdiff_t)p - (ptrdiff_t)ifname;
if (!isalnum((unsigned char)*p) || len > IFNAMSIZ) {
goto err;
}
p++;
}
/* Throw a warning, so that the user could double check. */
warnx("warning - unknown interface '%s'", ifname);
return;
err:
yyerror("illegitimate interface name '%s'", ifname);
}
static unsigned long
npfctl_find_ifindex(const char *ifname)
{
unsigned long if_idx = if_nametoindex(ifname);
bool testif = npfctl_debug_addif(ifname);
/*
* npfctl_parse_fam_addr: parse a given a string and return the address
* family with the actual address as npf_addr_t.
*
* => Return true on success; false otherwise.
*/
static bool
npfctl_parse_fam_addr(const char *name, sa_family_t *fam, npf_addr_t *addr)
{
static const struct addrinfo hint = {
.ai_family = AF_UNSPEC,
.ai_flags = AI_NUMERICHOST
};
struct addrinfo *ai;
int ret;
ret = getaddrinfo(name, NULL, &hint, &ai);
if (ret) {
yyerror("cannot parse '%s' (%s)", name, gai_strerror(ret));
return false;
}
if (fam) {
*fam = ai->ai_family;
}
if (!npfctl_copy_address(*fam, addr, ai->ai_addr)) {
return false;
}
freeaddrinfo(ai);
return true;
}
/*
* npfctl_parse_mask: parse a given string which represents a mask and
* can either be in quad-dot or CIDR block notation; validates the mask
* given the family.
*
* => Returns true if mask is valid (or is NULL); false otherwise.
*/
static bool
npfctl_parse_mask(const char *s, sa_family_t fam, npf_netmask_t *mask)
{
unsigned max_mask = NPF_MAX_NETMASK;
char *ep = NULL;
npf_addr_t addr;
uint8_t *ap;
assert(fam == AF_INET || fam == AF_INET6);
if (!s) {
/* No mask. */
*mask = NPF_NO_NETMASK;
return true;
}
errno = 0;
*mask = (npf_netmask_t)strtol(s, &ep, 0);
if (*ep == '\0' && s != ep && errno != ERANGE) {
/* Just a number -- CIDR notation. */
goto check;
}
/* Other characters: try to parse a full address. */
if (!npfctl_parse_fam_addr(s, &fam, &addr)) {
return false;
}
/* Convert the address to CIDR block number. */
ap = addr.word8 + (*mask / 8) - 1;
while (ap >= addr.word8) {
for (int j = 8; j > 0; j--) {
if (*ap & 1)
goto check;
*ap >>= 1;
(*mask)--;
if (*mask == 0)
goto check;
}
ap--;
}
*mask = NPF_NO_NETMASK;
return true;
check:
switch (fam) {
case AF_INET:
max_mask = 32;
break;
case AF_INET6:
max_mask = 128;
break;
}
return *mask <= max_mask;
}
/*
* npfctl_parse_fam_addr_mask: return address family, address and mask.
*
* => Mask is optional and can be NULL.
* => Returns true on success or false if unable to parse.
*/
npfvar_t *
npfctl_parse_fam_addr_mask(const char *addr, const char *mask,
unsigned long *nummask)
{
fam_addr_mask_t fam;
char buf[32];
memset(&fam, 0, sizeof(fam));
if (!npfctl_parse_fam_addr(addr, &fam.fam_family, &fam.fam_addr))
return NULL;
/*
* Mask may be NULL. In such case, "no mask" value will be set.
*/
if (nummask) {
/* Let npfctl_parse_mask() validate the number. */
snprintf(buf, sizeof(buf), "%lu", *nummask);
mask = buf;
}
if (!npfctl_parse_mask(mask, fam.fam_family, &fam.fam_mask)) {
return NULL;
}
return npfvar_create_element(NPFVAR_FAM, &fam, sizeof(fam));
}
/*
* this function is called for both gid and uid init in parser
* both uid and gid are both uint32_t
*/
void
npfctl_init_rid(rid_t *rid, uint32_t id1, uint32_t id2, uint8_t op)
{
rid->id[0] = id1;
rid->id[1] = id2;
rid->op = op;
}
/*
* npfctl_parse_port_range: create a port-range variable. Note that the
* passed port numbers should be in host byte order.
*/
npfvar_t *
npfctl_parse_port_range(in_port_t s, in_port_t e)
{
port_range_t pr;
int
npfctl_protono(const char *proto)
{
struct protoent *pe;
pe = getprotobyname(proto);
if (pe == NULL) {
yyerror("unknown protocol '%s'", proto);
return -1;
}
return pe->p_proto;
}
/*
* npfctl_portno: convert port identifier (string) to a number.
*
* => Returns port number in host byte order.
*/
in_port_t
npfctl_portno(const char *port)
{
struct addrinfo *ai, *rai;
in_port_t p = 0;
int e;
e = getaddrinfo(NULL, port, NULL, &rai);
if (e != 0) {
yyerror("invalid port name '%s' (%s)", port, gai_strerror(e));
return 0;
}
for (ai = rai; ai; ai = ai->ai_next) {
switch (ai->ai_family) {
case AF_INET: {
struct sockaddr_in *sin = (void *)ai->ai_addr;
p = sin->sin_port;
goto out;
}
case AF_INET6: {
struct sockaddr_in6 *sin6 = (void *)ai->ai_addr;
p = sin6->sin6_port;
goto out;
}
default:
break;
}
}
out:
freeaddrinfo(rai);
return ntohs(p);
}
switch (proto) {
case IPPROTO_ICMP:
switch (type) {
case ICMP_ECHOREPLY:
case ICMP_SOURCEQUENCH:
case ICMP_ALTHOSTADDR:
case ICMP_ECHO:
case ICMP_ROUTERSOLICIT:
case ICMP_TSTAMP:
case ICMP_TSTAMPREPLY:
case ICMP_IREQ:
case ICMP_IREQREPLY:
case ICMP_MASKREQ:
case ICMP_MASKREPLY:
arr = icmp_code_none;
break;
case ICMP_ROUTERADVERT:
arr = icmp_code_routeradvert;
break;
case ICMP_UNREACH:
arr = icmp_code_unreach;
break;
case ICMP_REDIRECT:
arr = icmp_code_redirect;
break;
case ICMP_TIMXCEED:
arr = icmp_code_timxceed;
break;
case ICMP_PARAMPROB:
arr = icmp_code_paramprob;
break;
case ICMP_PHOTURIS:
arr = icmp_code_photuris;
break;
default:
yyerror("unknown icmp-type %d while parsing code %s",
type, code);
return ~0;
}
break;
case IPPROTO_ICMPV6:
switch (type) {
case ICMP6_DST_UNREACH:
arr = icmp6_code_unreach;
break;
case ICMP6_TIME_EXCEEDED:
arr = icmp6_code_timxceed;
break;
case ICMP6_PARAM_PROB:
arr = icmp6_code_paramprob;
break;
case ICMP6_PACKET_TOO_BIG:
/* code-less info ICMPs */
case ICMP6_ECHO_REQUEST:
case ICMP6_ECHO_REPLY:
case MLD_LISTENER_QUERY:
case MLD_LISTENER_REPORT:
case MLD_LISTENER_DONE:
case ND_ROUTER_SOLICIT:
case ND_ROUTER_ADVERT:
case ND_NEIGHBOR_SOLICIT:
case ND_NEIGHBOR_ADVERT:
case ND_REDIRECT:
arr = icmp6_code_none;
break;
/* XXX TODO: info ICMPs with code values */
default:
yyerror("unknown icmp-type %d while parsing code %s",
type, code);
return ~0;
}
break;
default:
assert(false);
}
for (uint8_t ul = 0; arr[ul]; ul++) {
if (strcmp(arr[ul], code) == 0)
return ul;
}
#else
(void)proto;
#endif
yyerror("unknown code %s for icmp-type %d", code, type);
return ~0;
}
npfvar_t *
npfctl_parse_icmp(int proto __unused, int type, int code)
{
npfvar_t *vp = npfvar_create();
if (!npfvar_add_element(vp, NPFVAR_ICMP, &type, sizeof(type)))
goto out;
if (!npfvar_add_element(vp, NPFVAR_ICMP, &code, sizeof(code)))
goto out;
/*
* npfctl_npt66_calcadj: calculate the adjustment for NPTv6 as per RFC 6296.
*/
uint16_t
npfctl_npt66_calcadj(npf_netmask_t len, const npf_addr_t *pref_in,
const npf_addr_t *pref_out)
{
const uint16_t *addr6_in = (const uint16_t *)pref_in;
const uint16_t *addr6_out = (const uint16_t *)pref_out;
unsigned i, remnant, wordmask, preflen = len >> 4;
uint32_t adj, isum = 0, osum = 0;
/*
* Extract the bits within a 16-bit word (when prefix length is
* not dividable by 16) and include them into the sum.
*/
remnant = len - (preflen << 4);
wordmask = (1U << remnant) - 1;
assert(wordmask == 0 || (len % 16) != 0);
/* Inner prefix - sum and fold. */
for (i = 0; i < preflen; i++) {
isum += addr6_in[i];
}
isum += addr6_in[i] & wordmask;
while (isum >> 16) {
isum = (isum >> 16) + (isum & 0xffff);
}
/* Outer prefix - sum and fold. */
for (i = 0; i < preflen; i++) {
osum += addr6_out[i];
}
osum += addr6_out[i] & wordmask;
while (osum >> 16) {
osum = (osum >> 16) + (osum & 0xffff);
}