diff --git a/miniupnpd/pf/obsdrdr.c b/miniupnpd/pf/obsdrdr.c index 067a723..0b8cc21 100644 --- a/miniupnpd/pf/obsdrdr.c +++ b/miniupnpd/pf/obsdrdr.c @@ -1,4 +1,4 @@ -/* $Id: obsdrdr.c,v 1.96 2020/05/21 00:18:04 nanard Exp $ */ +/* $Id: obsdrdr.c,v 1.98 2020/05/29 22:29:11 nanard Exp $ */ /* vim: tabstop=4 shiftwidth=4 noexpandtab * MiniUPnP project * http://miniupnp.free.fr/ or https://miniupnp.tuxfamily.org/ @@ -245,6 +245,41 @@ error: return -1; #endif } + +int +clear_nat_rules(void) +{ + struct pfioc_trans io; + struct pfioc_trans_e ioe; + if(dev<0) { + syslog(LOG_ERR, "pf device is not open"); + return -1; + } + memset(&ioe, 0, sizeof(ioe)); + io.size = 1; + io.esize = sizeof(ioe); + io.array = &ioe; +#ifndef PF_NEWSTYLE + ioe.rs_num = PF_RULESET_NAT; +#else + /* ? */ + ioe.type = PF_TRANS_RULESET; +#endif + strlcpy(ioe.anchor, anchor_name, MAXPATHLEN); + if(ioctl(dev, DIOCXBEGIN, &io) < 0) + { + syslog(LOG_ERR, "ioctl(dev, DIOCXBEGIN, ...): %m"); + goto error; + } + if(ioctl(dev, DIOCXCOMMIT, &io) < 0) + { + syslog(LOG_ERR, "ioctl(dev, DIOCXCOMMIT, ...): %m"); + goto error; + } + return 0; +error: + return -1; +} #endif #ifdef ENABLE_PORT_TRIGGERING @@ -407,6 +442,63 @@ int add_nat_rule(const char * ifname, } return r; } + +static int +delete_nat_rule(const char * ifname, unsigned short iport, int proto, in_addr_t iaddr) +{ + int i, n; + struct pfioc_rule pr; + UNUSED(ifname); + if(dev<0) { + syslog(LOG_ERR, "pf device is not open"); + return -1; + } + memset(&pr, 0, sizeof(pr)); + strlcpy(pr.anchor, anchor_name, MAXPATHLEN); + pr.rule.action = PF_NAT; + if(ioctl(dev, DIOCGETRULES, &pr) < 0) + { + syslog(LOG_ERR, "ioctl(dev, DIOCGETRULES, ...): %m"); + goto error; + } + n = pr.nr; + for(i=0; i