2024-01-14 23:50:55 +00:00
|
|
|
/* $Id: pfpinhole.c,v 1.30 2024/01/14 23:56:45 nanard Exp $ */
|
2016-01-18 21:46:30 +00:00
|
|
|
/* vim: tabstop=4 shiftwidth=4 noexpandtab
|
|
|
|
* MiniUPnP project
|
2020-05-11 21:30:19 +00:00
|
|
|
* http://miniupnp.free.fr/ or https://miniupnp.tuxfamily.org/
|
2024-01-14 23:50:55 +00:00
|
|
|
* (c) 2012-2024 Thomas Bernard
|
2012-04-18 20:53:51 +00:00
|
|
|
* This software is subject to the conditions detailed
|
|
|
|
* in the LICENCE file provided within the distribution */
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/tcp.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#ifdef __DragonFly__
|
|
|
|
#include <net/pf/pfvar.h>
|
|
|
|
#else
|
2014-06-15 01:14:43 +00:00
|
|
|
#ifdef __APPLE__
|
2014-05-10 11:09:48 +00:00
|
|
|
#define PRIVATE 1
|
|
|
|
#endif
|
2012-04-18 20:53:51 +00:00
|
|
|
#include <net/pfvar.h>
|
|
|
|
#endif
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <syslog.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2020-05-11 21:30:19 +00:00
|
|
|
#include "config.h"
|
2012-04-18 23:43:50 +00:00
|
|
|
#include "pfpinhole.h"
|
2012-04-18 20:53:51 +00:00
|
|
|
#include "../upnpglobalvars.h"
|
2014-12-10 08:46:57 +00:00
|
|
|
#include "../macros.h"
|
2019-09-02 00:03:41 +00:00
|
|
|
#include "../upnputils.h"
|
2023-12-29 23:30:06 +00:00
|
|
|
#include "rtickets.h"
|
2012-04-18 20:53:51 +00:00
|
|
|
|
2012-04-20 14:49:04 +00:00
|
|
|
/* the pass rules created by add_pinhole() are as follow :
|
|
|
|
*
|
|
|
|
* pass in quick on ep0 inet6 proto udp
|
|
|
|
* from any to dead:beef::42:42 port = 8080
|
|
|
|
* flags S/SA keep state
|
|
|
|
* label "pinhole-2 ts-4321000"
|
|
|
|
*
|
2016-01-18 21:46:30 +00:00
|
|
|
* with the label "pinhole-$uid ts-$timestamp: $description"
|
2012-04-20 14:49:04 +00:00
|
|
|
*/
|
|
|
|
|
2014-05-10 11:09:48 +00:00
|
|
|
#ifdef ENABLE_UPNPPINHOLE
|
|
|
|
|
2012-04-18 20:53:51 +00:00
|
|
|
/* /dev/pf when opened */
|
|
|
|
extern int dev;
|
|
|
|
|
2012-04-22 00:03:35 +00:00
|
|
|
static int next_uid = 1;
|
|
|
|
|
2014-05-15 21:27:51 +00:00
|
|
|
#define PINEHOLE_LABEL_FORMAT "pinhole-%d ts-%u: %s"
|
|
|
|
#define PINEHOLE_LABEL_FORMAT_SKIPDESC "pinhole-%d ts-%u: %*s"
|
2012-04-19 22:07:13 +00:00
|
|
|
|
2012-04-20 14:49:04 +00:00
|
|
|
int add_pinhole(const char * ifname,
|
|
|
|
const char * rem_host, unsigned short rem_port,
|
|
|
|
const char * int_client, unsigned short int_port,
|
2014-05-10 11:09:48 +00:00
|
|
|
int proto, const char * desc, unsigned int timestamp)
|
2012-04-18 20:53:51 +00:00
|
|
|
{
|
2012-04-22 00:03:35 +00:00
|
|
|
int uid;
|
2012-04-18 20:53:51 +00:00
|
|
|
struct pfioc_rule pcr;
|
2012-04-18 23:43:50 +00:00
|
|
|
#ifndef PF_NEWSTYLE
|
|
|
|
struct pfioc_pooladdr pp;
|
|
|
|
#endif
|
2012-04-18 20:53:51 +00:00
|
|
|
|
|
|
|
if(dev<0) {
|
|
|
|
syslog(LOG_ERR, "pf device is not open");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
memset(&pcr, 0, sizeof(pcr));
|
2012-04-18 23:43:50 +00:00
|
|
|
strlcpy(pcr.anchor, anchor_name, MAXPATHLEN);
|
|
|
|
|
|
|
|
#ifndef PF_NEWSTYLE
|
|
|
|
memset(&pp, 0, sizeof(pp));
|
|
|
|
strlcpy(pp.anchor, anchor_name, MAXPATHLEN);
|
|
|
|
if(ioctl(dev, DIOCBEGINADDRS, &pp) < 0) {
|
|
|
|
syslog(LOG_ERR, "ioctl(dev, DIOCBEGINADDRS, ...): %m");
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
pcr.pool_ticket = pp.ticket;
|
|
|
|
#else
|
2012-04-18 20:53:51 +00:00
|
|
|
{
|
2012-04-18 23:43:50 +00:00
|
|
|
#endif
|
2012-04-18 20:53:51 +00:00
|
|
|
pcr.rule.direction = PF_IN;
|
|
|
|
pcr.rule.action = PF_PASS;
|
|
|
|
pcr.rule.af = AF_INET6;
|
2012-04-18 23:43:50 +00:00
|
|
|
#ifdef PF_NEWSTYLE
|
|
|
|
pcr.rule.nat.addr.type = PF_ADDR_NONE;
|
|
|
|
pcr.rule.rdr.addr.type = PF_ADDR_NONE;
|
|
|
|
#endif
|
2012-04-18 20:53:51 +00:00
|
|
|
#ifdef USE_IFNAME_IN_RULES
|
|
|
|
if(ifname)
|
|
|
|
strlcpy(pcr.rule.ifname, ifname, IFNAMSIZ);
|
|
|
|
#endif
|
|
|
|
pcr.rule.proto = proto;
|
|
|
|
|
|
|
|
pcr.rule.quick = 1;/*(GETFLAG(PFNOQUICKRULESMASK))?0:1;*/
|
|
|
|
pcr.rule.log = (GETFLAG(LOGPACKETSMASK))?1:0; /*logpackets;*/
|
|
|
|
/* see the discussion on the forum :
|
|
|
|
* http://miniupnp.tuxfamily.org/forum/viewtopic.php?p=638 */
|
|
|
|
pcr.rule.flags = TH_SYN;
|
|
|
|
pcr.rule.flagset = (TH_SYN|TH_ACK);
|
|
|
|
#ifdef PFRULE_HAS_RTABLEID
|
|
|
|
pcr.rule.rtableid = -1; /* first appeared in OpenBSD 4.0 */
|
|
|
|
#endif
|
|
|
|
#ifdef PFRULE_HAS_ONRDOMAIN
|
|
|
|
pcr.rule.onrdomain = -1; /* first appeared in OpenBSD 5.0 */
|
|
|
|
#endif
|
|
|
|
pcr.rule.keep_state = 1;
|
2012-04-22 00:03:35 +00:00
|
|
|
uid = next_uid;
|
2012-04-19 22:07:13 +00:00
|
|
|
snprintf(pcr.rule.label, PF_RULE_LABEL_SIZE,
|
2014-05-15 21:27:51 +00:00
|
|
|
PINEHOLE_LABEL_FORMAT, uid, timestamp, desc);
|
2012-04-18 20:53:51 +00:00
|
|
|
if(queue)
|
|
|
|
strlcpy(pcr.rule.qname, queue, PF_QNAME_SIZE);
|
|
|
|
if(tag)
|
|
|
|
strlcpy(pcr.rule.tagname, tag, PF_TAG_NAME_SIZE);
|
|
|
|
|
2012-04-18 23:43:50 +00:00
|
|
|
if(rem_port) {
|
|
|
|
pcr.rule.src.port_op = PF_OP_EQ;
|
|
|
|
pcr.rule.src.port[0] = htons(rem_port);
|
|
|
|
}
|
|
|
|
if(rem_host && rem_host[0] != '\0' && rem_host[0] != '*') {
|
|
|
|
pcr.rule.src.addr.type = PF_ADDR_ADDRMASK;
|
|
|
|
if(inet_pton(AF_INET6, rem_host, &pcr.rule.src.addr.v.a.addr.v6) != 1) {
|
|
|
|
syslog(LOG_ERR, "inet_pton(%s) failed", rem_host);
|
|
|
|
}
|
2012-04-18 20:53:51 +00:00
|
|
|
memset(&pcr.rule.src.addr.v.a.mask.addr8, 255, 16);
|
|
|
|
}
|
|
|
|
|
|
|
|
pcr.rule.dst.port_op = PF_OP_EQ;
|
|
|
|
pcr.rule.dst.port[0] = htons(int_port);
|
2012-04-18 23:43:50 +00:00
|
|
|
pcr.rule.dst.addr.type = PF_ADDR_ADDRMASK;
|
2012-04-18 20:53:51 +00:00
|
|
|
if(inet_pton(AF_INET6, int_client, &pcr.rule.dst.addr.v.a.addr.v6) != 1) {
|
|
|
|
syslog(LOG_ERR, "inet_pton(%s) failed", int_client);
|
|
|
|
}
|
|
|
|
memset(&pcr.rule.dst.addr.v.a.mask.addr8, 255, 16);
|
|
|
|
|
|
|
|
if(ifname)
|
|
|
|
strlcpy(pcr.rule.ifname, ifname, IFNAMSIZ);
|
|
|
|
|
|
|
|
pcr.action = PF_CHANGE_GET_TICKET;
|
|
|
|
if(ioctl(dev, DIOCCHANGERULE, &pcr) < 0) {
|
|
|
|
syslog(LOG_ERR, "ioctl(dev, DIOCCHANGERULE, ...) PF_CHANGE_GET_TICKET: %m");
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
pcr.action = PF_CHANGE_ADD_TAIL;
|
|
|
|
if(ioctl(dev, DIOCCHANGERULE, &pcr) < 0) {
|
|
|
|
syslog(LOG_ERR, "ioctl(dev, DIOCCHANGERULE, ...) PF_CHANGE_ADD_TAIL: %m");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-22 00:03:35 +00:00
|
|
|
if(++next_uid >= 65535) {
|
|
|
|
next_uid = 1;
|
|
|
|
}
|
|
|
|
return uid;
|
2012-04-19 22:07:13 +00:00
|
|
|
}
|
|
|
|
|
2016-01-18 21:46:30 +00:00
|
|
|
int find_pinhole(const char * ifname,
|
|
|
|
const char * rem_host, unsigned short rem_port,
|
|
|
|
const char * int_client, unsigned short int_port,
|
|
|
|
int proto,
|
|
|
|
char *desc, int desc_len, unsigned int * timestamp)
|
|
|
|
{
|
|
|
|
int uid;
|
2023-12-08 00:12:11 +00:00
|
|
|
unsigned int ts, tnum;
|
2016-01-18 21:46:30 +00:00
|
|
|
int i, n;
|
|
|
|
struct pfioc_rule pr;
|
|
|
|
struct in6_addr saddr;
|
|
|
|
struct in6_addr daddr;
|
|
|
|
UNUSED(ifname);
|
|
|
|
|
|
|
|
if(dev<0) {
|
|
|
|
syslog(LOG_ERR, "pf device is not open");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if(rem_host && (rem_host[0] != '\0')) {
|
|
|
|
inet_pton(AF_INET6, rem_host, &saddr);
|
|
|
|
} else {
|
|
|
|
memset(&saddr, 0, sizeof(struct in6_addr));
|
|
|
|
}
|
|
|
|
inet_pton(AF_INET6, int_client, &daddr);
|
|
|
|
memset(&pr, 0, sizeof(pr));
|
|
|
|
strlcpy(pr.anchor, anchor_name, MAXPATHLEN);
|
|
|
|
#ifndef PF_NEWSTYLE
|
|
|
|
pr.rule.action = PF_PASS;
|
|
|
|
#endif
|
|
|
|
if(ioctl(dev, DIOCGETRULES, &pr) < 0) {
|
|
|
|
syslog(LOG_ERR, "ioctl(dev, DIOCGETRULES, ...): %m");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
n = pr.nr;
|
2023-12-08 00:12:11 +00:00
|
|
|
#ifdef PF_RELEASETICKETS
|
|
|
|
tnum = pr.ticket;
|
|
|
|
#endif /* PF_RELEASETICKETS */
|
2016-01-18 21:46:30 +00:00
|
|
|
for(i=0; i<n; i++) {
|
|
|
|
pr.nr = i;
|
|
|
|
if(ioctl(dev, DIOCGETRULE, &pr) < 0) {
|
|
|
|
syslog(LOG_ERR, "ioctl(dev, DIOCGETRULE): %m");
|
2023-12-08 00:12:11 +00:00
|
|
|
release_ticket(dev, tnum);
|
2016-01-18 21:46:30 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if((proto == pr.rule.proto) && (rem_port == ntohs(pr.rule.src.port[0]))
|
|
|
|
&& (0 == memcmp(&saddr, &pr.rule.src.addr.v.a.addr.v6, sizeof(struct in6_addr)))
|
|
|
|
&& (int_port == ntohs(pr.rule.dst.port[0])) &&
|
|
|
|
(0 == memcmp(&daddr, &pr.rule.dst.addr.v.a.addr.v6, sizeof(struct in6_addr)))) {
|
|
|
|
if(sscanf(pr.rule.label, PINEHOLE_LABEL_FORMAT_SKIPDESC, &uid, &ts) != 2) {
|
|
|
|
syslog(LOG_DEBUG, "rule with label '%s' is not a IGD pinhole", pr.rule.label);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if(timestamp) *timestamp = ts;
|
|
|
|
if(desc) {
|
|
|
|
char * p = strchr(pr.rule.label, ':');
|
|
|
|
if(p) {
|
|
|
|
p += 2;
|
|
|
|
strlcpy(desc, p, desc_len);
|
|
|
|
}
|
|
|
|
}
|
2023-12-08 00:12:11 +00:00
|
|
|
release_ticket(dev, tnum);
|
2016-01-18 21:46:30 +00:00
|
|
|
return uid;
|
|
|
|
}
|
|
|
|
}
|
2023-12-08 00:12:11 +00:00
|
|
|
release_ticket(dev, tnum);
|
2016-01-18 21:46:30 +00:00
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
2012-04-20 14:49:04 +00:00
|
|
|
int delete_pinhole(unsigned short uid)
|
2012-04-19 22:07:13 +00:00
|
|
|
{
|
|
|
|
int i, n;
|
2023-12-08 00:12:11 +00:00
|
|
|
unsigned int tnum;
|
2012-04-19 22:07:13 +00:00
|
|
|
struct pfioc_rule pr;
|
2012-04-20 14:49:04 +00:00
|
|
|
char label_start[PF_RULE_LABEL_SIZE];
|
|
|
|
char tmp_label[PF_RULE_LABEL_SIZE];
|
2012-04-19 22:07:13 +00:00
|
|
|
|
|
|
|
if(dev<0) {
|
|
|
|
syslog(LOG_ERR, "pf device is not open");
|
|
|
|
return -1;
|
|
|
|
}
|
2012-04-20 14:49:04 +00:00
|
|
|
snprintf(label_start, sizeof(label_start),
|
2012-04-19 22:07:13 +00:00
|
|
|
"pinhole-%hu", uid);
|
|
|
|
memset(&pr, 0, sizeof(pr));
|
|
|
|
strlcpy(pr.anchor, anchor_name, MAXPATHLEN);
|
|
|
|
#ifndef PF_NEWSTYLE
|
|
|
|
pr.rule.action = PF_PASS;
|
|
|
|
#endif
|
|
|
|
if(ioctl(dev, DIOCGETRULES, &pr) < 0) {
|
|
|
|
syslog(LOG_ERR, "ioctl(dev, DIOCGETRULES, ...): %m");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
n = pr.nr;
|
2023-12-08 00:12:11 +00:00
|
|
|
#ifdef PF_RELEASETICKETS
|
|
|
|
tnum = pr.ticket;
|
|
|
|
#endif
|
2012-04-19 22:07:13 +00:00
|
|
|
for(i=0; i<n; i++) {
|
|
|
|
pr.nr = i;
|
|
|
|
if(ioctl(dev, DIOCGETRULE, &pr) < 0) {
|
|
|
|
syslog(LOG_ERR, "ioctl(dev, DIOCGETRULE): %m");
|
|
|
|
return -1;
|
|
|
|
}
|
2012-04-20 14:49:04 +00:00
|
|
|
strlcpy(tmp_label, pr.rule.label, sizeof(tmp_label));
|
|
|
|
strtok(tmp_label, " ");
|
|
|
|
if(0 == strcmp(tmp_label, label_start)) {
|
2012-04-19 22:07:13 +00:00
|
|
|
pr.action = PF_CHANGE_GET_TICKET;
|
|
|
|
if(ioctl(dev, DIOCCHANGERULE, &pr) < 0) {
|
|
|
|
syslog(LOG_ERR, "ioctl(dev, DIOCCHANGERULE, ...) PF_CHANGE_GET_TICKET: %m");
|
2023-12-08 00:12:11 +00:00
|
|
|
release_ticket(dev, tnum);
|
2012-04-19 22:07:13 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
pr.action = PF_CHANGE_REMOVE;
|
|
|
|
pr.nr = i;
|
|
|
|
if(ioctl(dev, DIOCCHANGERULE, &pr) < 0) {
|
|
|
|
syslog(LOG_ERR, "ioctl(dev, DIOCCHANGERULE, ...) PF_CHANGE_REMOVE: %m");
|
2023-12-08 00:12:11 +00:00
|
|
|
release_ticket(dev, tnum);
|
2012-04-19 22:07:13 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2023-12-08 00:12:11 +00:00
|
|
|
release_ticket(dev, tnum);
|
2012-04-19 22:07:13 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2023-12-08 00:12:11 +00:00
|
|
|
release_ticket(dev, tnum);
|
2012-04-19 22:07:13 +00:00
|
|
|
/* not found */
|
2012-04-22 23:47:37 +00:00
|
|
|
return -2;
|
2012-04-18 20:53:51 +00:00
|
|
|
}
|
|
|
|
|
2012-05-01 22:47:42 +00:00
|
|
|
int
|
|
|
|
get_pinhole_info(unsigned short uid,
|
|
|
|
char * rem_host, int rem_hostlen, unsigned short * rem_port,
|
|
|
|
char * int_client, int int_clientlen, unsigned short * int_port,
|
2014-05-10 11:09:48 +00:00
|
|
|
int * proto, char * desc, int desclen,
|
|
|
|
unsigned int * timestamp,
|
2012-05-01 22:47:42 +00:00
|
|
|
u_int64_t * packets, u_int64_t * bytes)
|
2012-04-20 22:08:06 +00:00
|
|
|
{
|
|
|
|
int i, n;
|
2023-12-08 00:12:11 +00:00
|
|
|
unsigned int tnum;
|
2012-04-20 22:08:06 +00:00
|
|
|
struct pfioc_rule pr;
|
|
|
|
char label_start[PF_RULE_LABEL_SIZE];
|
|
|
|
char tmp_label[PF_RULE_LABEL_SIZE];
|
|
|
|
char * p;
|
|
|
|
|
|
|
|
if(dev<0) {
|
|
|
|
syslog(LOG_ERR, "pf device is not open");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
snprintf(label_start, sizeof(label_start),
|
|
|
|
"pinhole-%hu", uid);
|
|
|
|
memset(&pr, 0, sizeof(pr));
|
|
|
|
strlcpy(pr.anchor, anchor_name, MAXPATHLEN);
|
|
|
|
#ifndef PF_NEWSTYLE
|
|
|
|
pr.rule.action = PF_PASS;
|
|
|
|
#endif
|
|
|
|
if(ioctl(dev, DIOCGETRULES, &pr) < 0) {
|
|
|
|
syslog(LOG_ERR, "ioctl(dev, DIOCGETRULES, ...): %m");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
n = pr.nr;
|
2023-12-08 00:12:11 +00:00
|
|
|
#ifdef PF_RELEASETICKETS
|
|
|
|
tnum = pr.ticket;
|
|
|
|
#endif
|
2012-04-20 22:08:06 +00:00
|
|
|
for(i=0; i<n; i++) {
|
|
|
|
pr.nr = i;
|
|
|
|
if(ioctl(dev, DIOCGETRULE, &pr) < 0) {
|
|
|
|
syslog(LOG_ERR, "ioctl(dev, DIOCGETRULE): %m");
|
2023-12-08 00:12:11 +00:00
|
|
|
release_ticket(dev, tnum);
|
2012-04-20 22:08:06 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
strlcpy(tmp_label, pr.rule.label, sizeof(tmp_label));
|
|
|
|
p = tmp_label;
|
|
|
|
strsep(&p, " ");
|
|
|
|
if(0 == strcmp(tmp_label, label_start)) {
|
2012-04-22 23:47:37 +00:00
|
|
|
if(rem_host && (inet_ntop(AF_INET6, &pr.rule.src.addr.v.a.addr.v6, rem_host, rem_hostlen) == NULL)) {
|
2023-12-08 00:12:11 +00:00
|
|
|
release_ticket(dev, tnum);
|
2012-04-22 23:47:37 +00:00
|
|
|
return -1;
|
2012-04-20 22:08:06 +00:00
|
|
|
}
|
2012-04-22 23:47:37 +00:00
|
|
|
if(rem_port)
|
|
|
|
*rem_port = ntohs(pr.rule.src.port[0]);
|
|
|
|
if(int_client && (inet_ntop(AF_INET6, &pr.rule.dst.addr.v.a.addr.v6, int_client, int_clientlen) == NULL)) {
|
2023-12-08 00:12:11 +00:00
|
|
|
release_ticket(dev, tnum);
|
2012-04-22 23:47:37 +00:00
|
|
|
return -1;
|
2012-04-20 22:08:06 +00:00
|
|
|
}
|
2012-04-22 23:47:37 +00:00
|
|
|
if(int_port)
|
|
|
|
*int_port = ntohs(pr.rule.dst.port[0]);
|
|
|
|
if(proto)
|
|
|
|
*proto = pr.rule.proto;
|
|
|
|
if(timestamp)
|
|
|
|
sscanf(p, "ts-%u", timestamp);
|
2014-05-15 21:27:51 +00:00
|
|
|
if(desc) {
|
|
|
|
strsep(&p, " ");
|
|
|
|
if(p) {
|
|
|
|
strlcpy(desc, p, desclen);
|
|
|
|
} else {
|
|
|
|
desc[0] = '\0';
|
|
|
|
}
|
|
|
|
}
|
2012-04-22 23:47:37 +00:00
|
|
|
#ifdef PFRULE_INOUT_COUNTS
|
|
|
|
if(packets)
|
|
|
|
*packets = pr.rule.packets[0] + pr.rule.packets[1];
|
|
|
|
if(bytes)
|
|
|
|
*bytes = pr.rule.bytes[0] + pr.rule.bytes[1];
|
|
|
|
#else
|
|
|
|
if(packets)
|
|
|
|
*packets = pr.rule.packets;
|
|
|
|
if(bytes)
|
|
|
|
*bytes = pr.rule.bytes;
|
|
|
|
#endif
|
2023-12-08 00:12:11 +00:00
|
|
|
release_ticket(dev, tnum);
|
2012-04-20 22:08:06 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2023-12-08 00:12:11 +00:00
|
|
|
release_ticket(dev, tnum);
|
2012-04-20 22:08:06 +00:00
|
|
|
/* not found */
|
2012-04-22 23:47:37 +00:00
|
|
|
return -2;
|
2012-04-20 22:08:06 +00:00
|
|
|
}
|
|
|
|
|
2012-04-23 22:45:45 +00:00
|
|
|
int update_pinhole(unsigned short uid, unsigned int timestamp)
|
|
|
|
{
|
|
|
|
/* TODO :
|
|
|
|
* As it is not possible to change rule label, we should :
|
|
|
|
* 1 - delete
|
|
|
|
* 2 - Add new
|
|
|
|
* the stats of the rule will then be reset :( */
|
2014-12-10 08:46:57 +00:00
|
|
|
UNUSED(uid); UNUSED(timestamp);
|
2012-04-23 22:45:45 +00:00
|
|
|
return -42; /* not implemented */
|
|
|
|
}
|
|
|
|
|
2012-04-22 23:47:37 +00:00
|
|
|
/* return the number of rules removed
|
|
|
|
* or a negative integer in case of error */
|
2012-04-22 00:58:27 +00:00
|
|
|
int clean_pinhole_list(unsigned int * next_timestamp)
|
2012-04-22 00:03:35 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct pfioc_rule pr;
|
|
|
|
time_t current_time;
|
2023-12-08 00:12:11 +00:00
|
|
|
unsigned int ts, tnum;
|
2012-04-22 00:03:35 +00:00
|
|
|
int uid;
|
|
|
|
unsigned int min_ts = UINT_MAX;
|
|
|
|
int min_uid = INT_MAX, max_uid = -1;
|
2012-04-22 23:47:37 +00:00
|
|
|
int n = 0;
|
2012-04-22 00:03:35 +00:00
|
|
|
|
|
|
|
if(dev<0) {
|
|
|
|
syslog(LOG_ERR, "pf device is not open");
|
|
|
|
return -1;
|
|
|
|
}
|
2018-03-13 23:09:42 +00:00
|
|
|
current_time = upnp_time();
|
2012-04-22 00:03:35 +00:00
|
|
|
memset(&pr, 0, sizeof(pr));
|
|
|
|
strlcpy(pr.anchor, anchor_name, MAXPATHLEN);
|
|
|
|
#ifndef PF_NEWSTYLE
|
|
|
|
pr.rule.action = PF_PASS;
|
|
|
|
#endif
|
|
|
|
if(ioctl(dev, DIOCGETRULES, &pr) < 0) {
|
|
|
|
syslog(LOG_ERR, "ioctl(dev, DIOCGETRULES, ...): %m");
|
|
|
|
return -1;
|
|
|
|
}
|
2023-12-08 00:12:11 +00:00
|
|
|
#ifdef PF_RELEASETICKETS
|
|
|
|
tnum = pr.ticket;
|
|
|
|
#endif
|
2012-04-22 00:03:35 +00:00
|
|
|
for(i = pr.nr - 1; i >= 0; i--) {
|
|
|
|
pr.nr = i;
|
|
|
|
if(ioctl(dev, DIOCGETRULE, &pr) < 0) {
|
|
|
|
syslog(LOG_ERR, "ioctl(dev, DIOCGETRULE): %m");
|
2023-12-08 00:12:11 +00:00
|
|
|
release_ticket(dev, tnum);
|
2012-04-22 00:03:35 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2014-05-15 21:27:51 +00:00
|
|
|
if(sscanf(pr.rule.label, PINEHOLE_LABEL_FORMAT_SKIPDESC, &uid, &ts) != 2) {
|
2014-12-10 08:46:57 +00:00
|
|
|
syslog(LOG_DEBUG, "rule with label '%s' is not a IGD pinhole", pr.rule.label);
|
2012-04-22 00:03:35 +00:00
|
|
|
continue;
|
|
|
|
}
|
2012-05-01 09:52:21 +00:00
|
|
|
if(ts <= (unsigned int)current_time) {
|
2012-04-22 00:03:35 +00:00
|
|
|
syslog(LOG_INFO, "removing expired pinhole '%s'", pr.rule.label);
|
|
|
|
pr.action = PF_CHANGE_GET_TICKET;
|
|
|
|
if(ioctl(dev, DIOCCHANGERULE, &pr) < 0) {
|
|
|
|
syslog(LOG_ERR, "ioctl(dev, DIOCCHANGERULE, ...) PF_CHANGE_GET_TICKET: %m");
|
2023-12-08 00:12:11 +00:00
|
|
|
release_ticket(dev, tnum);
|
2012-04-22 00:03:35 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
pr.action = PF_CHANGE_REMOVE;
|
|
|
|
pr.nr = i;
|
|
|
|
if(ioctl(dev, DIOCCHANGERULE, &pr) < 0) {
|
|
|
|
syslog(LOG_ERR, "ioctl(dev, DIOCCHANGERULE, ...) PF_CHANGE_REMOVE: %m");
|
2023-12-08 00:12:11 +00:00
|
|
|
release_ticket(dev, tnum);
|
2012-04-22 00:03:35 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2012-04-22 23:47:37 +00:00
|
|
|
n++;
|
2012-04-22 00:03:35 +00:00
|
|
|
#ifndef PF_NEWSTYLE
|
|
|
|
pr.rule.action = PF_PASS;
|
|
|
|
#endif
|
2023-12-08 00:12:11 +00:00
|
|
|
release_ticket(dev, tnum);
|
2012-04-22 00:03:35 +00:00
|
|
|
if(ioctl(dev, DIOCGETRULES, &pr) < 0) {
|
|
|
|
syslog(LOG_ERR, "ioctl(dev, DIOCGETRULES, ...): %m");
|
|
|
|
return -1;
|
|
|
|
}
|
2023-12-08 00:12:11 +00:00
|
|
|
#ifdef PF_RELEASETICKETS
|
|
|
|
tnum = pr.ticket;
|
|
|
|
#endif
|
2012-04-22 00:03:35 +00:00
|
|
|
} else {
|
|
|
|
if(uid > max_uid)
|
|
|
|
max_uid = uid;
|
|
|
|
else if(uid < min_uid)
|
|
|
|
min_uid = uid;
|
|
|
|
if(ts < min_ts)
|
|
|
|
min_ts = ts;
|
|
|
|
}
|
|
|
|
}
|
2012-05-24 16:18:51 +00:00
|
|
|
if(next_timestamp && (min_ts != UINT_MAX))
|
2012-04-22 00:03:35 +00:00
|
|
|
*next_timestamp = min_ts;
|
|
|
|
if(max_uid > 0) {
|
|
|
|
if(((min_uid - 32000) <= next_uid) && (next_uid <= max_uid)) {
|
|
|
|
next_uid = max_uid + 1;
|
|
|
|
}
|
|
|
|
if(next_uid >= 65535) {
|
|
|
|
next_uid = 1;
|
|
|
|
}
|
|
|
|
}
|
2023-12-08 00:12:11 +00:00
|
|
|
release_ticket(dev, tnum);
|
2012-04-22 23:47:37 +00:00
|
|
|
return n; /* number of rules removed */
|
2012-04-22 00:03:35 +00:00
|
|
|
}
|
|
|
|
|
2014-05-10 11:09:48 +00:00
|
|
|
#endif /* ENABLE_UPNPPINHOLE */
|