2020-05-10 18:00:37 +00:00
|
|
|
/* $Id: iptpinhole.c,v 1.21 2020/05/10 17:49:33 nanard Exp $ */
|
2012-04-26 14:08:39 +00:00
|
|
|
/* MiniUPnP project
|
2019-08-24 08:55:33 +00:00
|
|
|
* http://miniupnp.free.fr/ or https://miniupnp.tuxfamily.org/
|
2020-05-10 18:00:37 +00:00
|
|
|
* (c) 2012-2020 Thomas Bernard
|
2012-04-26 14:08:39 +00:00
|
|
|
* This software is subject to the conditions detailed
|
|
|
|
* in the LICENCE file provided within the distribution */
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <syslog.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <arpa/inet.h>
|
2012-04-27 06:51:25 +00:00
|
|
|
#include <sys/queue.h>
|
2012-04-26 14:08:39 +00:00
|
|
|
|
2020-05-03 23:31:31 +00:00
|
|
|
#include "config.h"
|
2016-01-18 19:16:06 +00:00
|
|
|
#include "../macros.h"
|
2012-04-26 14:08:39 +00:00
|
|
|
#include "iptpinhole.h"
|
|
|
|
#include "../upnpglobalvars.h"
|
2018-04-22 19:24:30 +00:00
|
|
|
#include "../upnputils.h"
|
2012-04-26 14:08:39 +00:00
|
|
|
|
2014-05-10 11:09:48 +00:00
|
|
|
#ifdef ENABLE_UPNPPINHOLE
|
2012-04-26 14:08:39 +00:00
|
|
|
|
|
|
|
#include <xtables.h>
|
|
|
|
#include <libiptc/libip6tc.h>
|
|
|
|
#include "tiny_nf_nat.h"
|
|
|
|
|
|
|
|
#define IP6TC_HANDLE struct ip6tc_handle *
|
|
|
|
|
|
|
|
static int next_uid = 1;
|
|
|
|
|
2020-09-28 20:44:24 +00:00
|
|
|
static const char * miniupnpd_v6_filter_chain = "MINIUPNPD";
|
|
|
|
|
2012-05-01 22:47:42 +00:00
|
|
|
static LIST_HEAD(pinhole_list_t, pinhole_t) pinhole_list;
|
2012-04-27 06:51:25 +00:00
|
|
|
|
2012-05-08 20:51:23 +00:00
|
|
|
static struct pinhole_t *
|
|
|
|
get_pinhole(unsigned short uid);
|
|
|
|
|
2012-04-27 06:51:25 +00:00
|
|
|
struct pinhole_t {
|
|
|
|
struct in6_addr saddr;
|
|
|
|
struct in6_addr daddr;
|
|
|
|
LIST_ENTRY(pinhole_t) entries;
|
|
|
|
unsigned int timestamp;
|
|
|
|
unsigned short sport;
|
|
|
|
unsigned short dport;
|
|
|
|
unsigned short uid;
|
|
|
|
unsigned char proto;
|
2014-05-10 11:09:48 +00:00
|
|
|
char desc[];
|
2012-04-27 06:51:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
void init_iptpinhole(void)
|
|
|
|
{
|
|
|
|
LIST_INIT(&pinhole_list);
|
|
|
|
}
|
|
|
|
|
|
|
|
void shutdown_iptpinhole(void)
|
|
|
|
{
|
2016-01-19 09:53:14 +00:00
|
|
|
struct pinhole_t * p;
|
|
|
|
while(pinhole_list.lh_first != NULL) {
|
|
|
|
p = pinhole_list.lh_first;
|
|
|
|
LIST_REMOVE(p, entries);
|
|
|
|
free(p);
|
|
|
|
}
|
2012-04-27 06:51:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* return uid */
|
|
|
|
static int
|
|
|
|
add_to_pinhole_list(struct in6_addr * saddr, unsigned short sport,
|
|
|
|
struct in6_addr * daddr, unsigned short dport,
|
2014-05-10 11:09:48 +00:00
|
|
|
int proto, const char *desc, unsigned int timestamp)
|
2012-04-27 06:51:25 +00:00
|
|
|
{
|
|
|
|
struct pinhole_t * p;
|
|
|
|
|
2014-05-10 11:09:48 +00:00
|
|
|
p = calloc(1, sizeof(struct pinhole_t) + strlen(desc) + 1);
|
2012-04-27 06:51:25 +00:00
|
|
|
if(!p) {
|
|
|
|
syslog(LOG_ERR, "add_to_pinhole_list calloc() error");
|
|
|
|
return -1;
|
|
|
|
}
|
2014-05-10 11:09:48 +00:00
|
|
|
strcpy(p->desc, desc);
|
2012-04-27 06:51:25 +00:00
|
|
|
memcpy(&p->saddr, saddr, sizeof(struct in6_addr));
|
|
|
|
p->sport = sport;
|
|
|
|
memcpy(&p->daddr, daddr, sizeof(struct in6_addr));
|
|
|
|
p->dport = dport;
|
|
|
|
p->timestamp = timestamp;
|
|
|
|
p->proto = (unsigned char)proto;
|
|
|
|
LIST_INSERT_HEAD(&pinhole_list, p, entries);
|
2012-05-08 20:51:23 +00:00
|
|
|
while(get_pinhole(next_uid) != NULL) {
|
|
|
|
next_uid++;
|
|
|
|
if(next_uid > 65535)
|
|
|
|
next_uid = 1;
|
|
|
|
}
|
2012-04-27 06:51:25 +00:00
|
|
|
p->uid = next_uid;
|
|
|
|
next_uid++;
|
|
|
|
if(next_uid > 65535)
|
|
|
|
next_uid = 1;
|
|
|
|
return p->uid;
|
|
|
|
}
|
|
|
|
|
2012-05-01 22:47:42 +00:00
|
|
|
static struct pinhole_t *
|
|
|
|
get_pinhole(unsigned short uid)
|
|
|
|
{
|
|
|
|
struct pinhole_t * p;
|
|
|
|
|
|
|
|
for(p = pinhole_list.lh_first; p != NULL; p = p->entries.le_next) {
|
|
|
|
if(p->uid == uid)
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
return NULL; /* not found */
|
|
|
|
}
|
|
|
|
|
2012-04-27 06:51:25 +00:00
|
|
|
/* new_match()
|
|
|
|
* Allocate and set a new ip6t_entry_match structure
|
|
|
|
* The caller must free() it after usage */
|
2012-04-26 14:08:39 +00:00
|
|
|
static struct ip6t_entry_match *
|
|
|
|
new_match(int proto, unsigned short sport, unsigned short dport)
|
|
|
|
{
|
|
|
|
struct ip6t_entry_match *match;
|
2012-04-27 06:51:25 +00:00
|
|
|
struct ip6t_tcp *info; /* TODO : use ip6t_udp if needed */
|
2012-04-26 14:08:39 +00:00
|
|
|
size_t size;
|
|
|
|
const char * name;
|
|
|
|
size = XT_ALIGN(sizeof(struct ip6t_entry_match))
|
|
|
|
+ XT_ALIGN(sizeof(struct ip6t_tcp));
|
|
|
|
match = calloc(1, size);
|
|
|
|
match->u.user.match_size = size;
|
|
|
|
switch(proto) {
|
|
|
|
case IPPROTO_TCP:
|
|
|
|
name = "tcp";
|
|
|
|
break;
|
|
|
|
case IPPROTO_UDP:
|
|
|
|
name = "udp";
|
|
|
|
break;
|
|
|
|
case IPPROTO_UDPLITE:
|
|
|
|
name = "udplite";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
name = NULL;
|
|
|
|
}
|
|
|
|
if(name)
|
|
|
|
strncpy(match->u.user.name, name, sizeof(match->u.user.name));
|
|
|
|
else
|
|
|
|
syslog(LOG_WARNING, "no name for protocol %d", proto);
|
|
|
|
info = (struct ip6t_tcp *)match->data;
|
|
|
|
if(sport) {
|
|
|
|
info->spts[0] = sport; /* specified source port */
|
|
|
|
info->spts[1] = sport;
|
|
|
|
} else {
|
|
|
|
info->spts[0] = 0; /* all source ports */
|
|
|
|
info->spts[1] = 0xFFFF;
|
|
|
|
}
|
|
|
|
info->dpts[0] = dport; /* specified destination port */
|
|
|
|
info->dpts[1] = dport;
|
|
|
|
return match;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct ip6t_entry_target *
|
|
|
|
get_accept_target(void)
|
|
|
|
{
|
|
|
|
struct ip6t_entry_target * target = NULL;
|
|
|
|
size_t size;
|
|
|
|
size = XT_ALIGN(sizeof(struct ip6t_entry_target))
|
|
|
|
+ XT_ALIGN(sizeof(int));
|
|
|
|
target = calloc(1, size);
|
|
|
|
target->u.user.target_size = size;
|
|
|
|
strncpy(target->u.user.name, "ACCEPT", sizeof(target->u.user.name));
|
|
|
|
return target;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ip6tc_init_verify_append(const char * table,
|
|
|
|
const char * chain,
|
|
|
|
struct ip6t_entry * e)
|
|
|
|
{
|
|
|
|
IP6TC_HANDLE h;
|
|
|
|
|
|
|
|
h = ip6tc_init(table);
|
|
|
|
if(!h) {
|
|
|
|
syslog(LOG_ERR, "ip6tc_init error : %s", ip6tc_strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if(!ip6tc_is_chain(chain, h)) {
|
|
|
|
syslog(LOG_ERR, "chain %s not found", chain);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
if(!ip6tc_append_entry(chain, e, h)) {
|
|
|
|
syslog(LOG_ERR, "ip6tc_append_entry() error : %s", ip6tc_strerror(errno));
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
if(!ip6tc_commit(h)) {
|
|
|
|
syslog(LOG_ERR, "ip6tc_commit() error : %s", ip6tc_strerror(errno));
|
|
|
|
goto error;
|
|
|
|
}
|
2019-08-24 08:55:33 +00:00
|
|
|
ip6tc_free(h);
|
2012-04-26 14:08:39 +00:00
|
|
|
return 0; /* ok */
|
|
|
|
error:
|
|
|
|
ip6tc_free(h);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
ip6tables -I %s %d -p %s -i %s -s %s --sport %hu -d %s --dport %hu -j ACCEPT
|
|
|
|
ip6tables -I %s %d -p %s -i %s --sport %hu -d %s --dport %hu -j ACCEPT
|
|
|
|
|
|
|
|
miniupnpd_forward_chain, line_number, proto, ext_if_name, raddr, rport, iaddr, iport
|
|
|
|
|
|
|
|
ip6tables -t raw -I PREROUTING %d -p %s -i %s -s %s --sport %hu -d %s --dport %hu -j TRACE
|
|
|
|
ip6tables -t raw -I PREROUTING %d -p %s -i %s --sport %hu -d %s --dport %hu -j TRACE
|
|
|
|
*/
|
|
|
|
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-26 14:08:39 +00:00
|
|
|
{
|
|
|
|
int uid;
|
|
|
|
struct ip6t_entry * e;
|
2015-02-08 09:23:22 +00:00
|
|
|
struct ip6t_entry * tmp;
|
2012-04-26 14:08:39 +00:00
|
|
|
struct ip6t_entry_match *match = NULL;
|
|
|
|
struct ip6t_entry_target *target = NULL;
|
|
|
|
|
|
|
|
e = calloc(1, sizeof(struct ip6t_entry));
|
2015-02-08 09:23:22 +00:00
|
|
|
if(!e) {
|
|
|
|
syslog(LOG_ERR, "%s: calloc(%d) failed",
|
|
|
|
"add_pinhole", (int)sizeof(struct ip6t_entry));
|
|
|
|
return -1;
|
|
|
|
}
|
2012-04-26 14:08:39 +00:00
|
|
|
e->ipv6.proto = proto;
|
2014-05-29 14:56:48 +00:00
|
|
|
if (proto)
|
|
|
|
e->ipv6.flags |= IP6T_F_PROTO;
|
2012-04-26 14:08:39 +00:00
|
|
|
|
2017-03-13 10:47:21 +00:00
|
|
|
/* TODO: check if enforcing USE_IFNAME_IN_RULES is needed */
|
2012-04-26 14:08:39 +00:00
|
|
|
if(ifname)
|
|
|
|
strncpy(e->ipv6.iniface, ifname, IFNAMSIZ);
|
2015-02-10 15:04:10 +00:00
|
|
|
if(rem_host && (rem_host[0] != '\0')) {
|
2022-03-19 17:50:58 +00:00
|
|
|
if(inet_pton(AF_INET6, rem_host, &e->ipv6.src) < 1) {
|
|
|
|
syslog(LOG_WARNING, "failed to parse INET6 address \"%s\"", rem_host);
|
|
|
|
} else {
|
|
|
|
memset(&e->ipv6.smsk, 0xff, sizeof(e->ipv6.smsk));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (inet_pton(AF_INET6, int_client, &e->ipv6.dst) < 1) {
|
|
|
|
syslog(LOG_WARNING, "failed to parse INET6 address \"%s\"", int_client);
|
|
|
|
} else {
|
|
|
|
memset(&e->ipv6.dmsk, 0xff, sizeof(e->ipv6.dmsk));
|
2012-04-26 14:08:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*e->nfcache = NFC_IP_DST_PT;*/
|
|
|
|
/*e->nfcache |= NFC_UNKNOWN;*/
|
|
|
|
|
|
|
|
match = new_match(proto, rem_port, int_port);
|
|
|
|
target = get_accept_target();
|
2015-02-08 09:23:22 +00:00
|
|
|
tmp = realloc(e, sizeof(struct ip6t_entry)
|
2012-04-26 14:08:39 +00:00
|
|
|
+ match->u.match_size
|
|
|
|
+ target->u.target_size);
|
2015-02-08 09:23:22 +00:00
|
|
|
if(!tmp) {
|
|
|
|
syslog(LOG_ERR, "%s: realloc(%d) failed",
|
|
|
|
"add_pinhole", (int)(sizeof(struct ip6t_entry) + match->u.match_size + target->u.target_size));
|
|
|
|
free(e);
|
|
|
|
free(match);
|
|
|
|
free(target);
|
|
|
|
return -1;
|
|
|
|
}
|
2015-02-08 09:46:13 +00:00
|
|
|
e = tmp;
|
2012-04-26 14:08:39 +00:00
|
|
|
memcpy(e->elems, match, match->u.match_size);
|
|
|
|
memcpy(e->elems + match->u.match_size, target, target->u.target_size);
|
|
|
|
e->target_offset = sizeof(struct ip6t_entry)
|
|
|
|
+ match->u.match_size;
|
|
|
|
e->next_offset = sizeof(struct ip6t_entry)
|
|
|
|
+ match->u.match_size
|
|
|
|
+ target->u.target_size;
|
|
|
|
free(match);
|
|
|
|
free(target);
|
|
|
|
|
|
|
|
if(ip6tc_init_verify_append("filter", miniupnpd_v6_filter_chain, e) < 0) {
|
|
|
|
free(e);
|
|
|
|
return -1;
|
|
|
|
}
|
2012-04-27 06:51:25 +00:00
|
|
|
uid = add_to_pinhole_list(&e->ipv6.src, rem_port,
|
|
|
|
&e->ipv6.dst, int_port,
|
2014-05-10 11:09:48 +00:00
|
|
|
proto, desc, timestamp);
|
2012-04-26 14:08:39 +00:00
|
|
|
free(e);
|
|
|
|
return uid;
|
|
|
|
}
|
|
|
|
|
2016-01-18 19:16:06 +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)
|
|
|
|
{
|
|
|
|
struct pinhole_t * p;
|
|
|
|
struct in6_addr saddr;
|
|
|
|
struct in6_addr daddr;
|
|
|
|
UNUSED(ifname);
|
|
|
|
|
|
|
|
if(rem_host && (rem_host[0] != '\0')) {
|
2022-03-19 17:50:58 +00:00
|
|
|
if (inet_pton(AF_INET6, rem_host, &saddr) < 1) {
|
|
|
|
syslog(LOG_WARNING, "Failed to parse INET6 address \"%s\"", rem_host);
|
|
|
|
memset(&saddr, 0, sizeof(struct in6_addr));
|
|
|
|
}
|
2016-01-18 19:16:06 +00:00
|
|
|
} else {
|
|
|
|
memset(&saddr, 0, sizeof(struct in6_addr));
|
|
|
|
}
|
2022-03-19 17:50:58 +00:00
|
|
|
if (inet_pton(AF_INET6, int_client, &daddr) < 1) {
|
|
|
|
syslog(LOG_WARNING, "Failed to parse INET6 address \"%s\"", int_client);
|
|
|
|
memset(&daddr, 0, sizeof(struct in6_addr));
|
|
|
|
}
|
2016-01-18 19:16:06 +00:00
|
|
|
for(p = pinhole_list.lh_first; p != NULL; p = p->entries.le_next) {
|
|
|
|
if((proto == p->proto) && (rem_port == p->sport) &&
|
|
|
|
(0 == memcmp(&saddr, &p->saddr, sizeof(struct in6_addr))) &&
|
|
|
|
(int_port == p->dport) &&
|
|
|
|
(0 == memcmp(&daddr, &p->daddr, sizeof(struct in6_addr)))) {
|
|
|
|
if(desc) strncpy(desc, p->desc, desc_len);
|
|
|
|
if(timestamp) *timestamp = p->timestamp;
|
|
|
|
return (int)p->uid;
|
|
|
|
}
|
|
|
|
}
|
2016-01-19 09:52:36 +00:00
|
|
|
return -2; /* not found */
|
2016-01-18 19:16:06 +00:00
|
|
|
}
|
|
|
|
|
2012-05-01 22:47:42 +00:00
|
|
|
int
|
|
|
|
delete_pinhole(unsigned short uid)
|
|
|
|
{
|
|
|
|
struct pinhole_t * p;
|
|
|
|
IP6TC_HANDLE h;
|
|
|
|
const struct ip6t_entry * e;
|
|
|
|
const struct ip6t_entry_match *match = NULL;
|
|
|
|
/*const struct ip6t_entry_target *target = NULL;*/
|
|
|
|
unsigned int index;
|
|
|
|
|
|
|
|
p = get_pinhole(uid);
|
|
|
|
if(!p)
|
|
|
|
return -2; /* not found */
|
|
|
|
|
|
|
|
h = ip6tc_init("filter");
|
|
|
|
if(!h) {
|
|
|
|
syslog(LOG_ERR, "ip6tc_init error : %s", ip6tc_strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if(!ip6tc_is_chain(miniupnpd_v6_filter_chain, h)) {
|
|
|
|
syslog(LOG_ERR, "chain %s not found", miniupnpd_v6_filter_chain);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
index = 0;
|
|
|
|
for(e = ip6tc_first_rule(miniupnpd_v6_filter_chain, h);
|
|
|
|
e;
|
|
|
|
e = ip6tc_next_rule(e, h)) {
|
|
|
|
if((e->ipv6.proto == p->proto) &&
|
|
|
|
(0 == memcmp(&e->ipv6.src, &p->saddr, sizeof(e->ipv6.src))) &&
|
|
|
|
(0 == memcmp(&e->ipv6.dst, &p->daddr, sizeof(e->ipv6.dst)))) {
|
|
|
|
const struct ip6t_tcp * info;
|
|
|
|
match = (const struct ip6t_entry_match *)&e->elems;
|
|
|
|
info = (const struct ip6t_tcp *)&match->data;
|
|
|
|
if((info->spts[0] == p->sport) && (info->dpts[0] == p->dport)) {
|
|
|
|
if(!ip6tc_delete_num_entry(miniupnpd_v6_filter_chain, index, h)) {
|
2016-01-04 03:56:39 +00:00
|
|
|
syslog(LOG_ERR, "ip6tc_delete_num_entry(%s,%u,...): %s",
|
2012-05-01 22:47:42 +00:00
|
|
|
miniupnpd_v6_filter_chain, index, ip6tc_strerror(errno));
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
if(!ip6tc_commit(h)) {
|
|
|
|
syslog(LOG_ERR, "ip6tc_commit(): %s",
|
|
|
|
ip6tc_strerror(errno));
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
ip6tc_free(h);
|
|
|
|
LIST_REMOVE(p, entries);
|
|
|
|
return 0; /* ok */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
ip6tc_free(h);
|
|
|
|
syslog(LOG_WARNING, "delete_pinhole() rule with PID=%hu not found", uid);
|
2016-01-13 10:26:20 +00:00
|
|
|
LIST_REMOVE(p, entries);
|
2012-05-01 22:47:42 +00:00
|
|
|
return -2; /* not found */
|
|
|
|
error:
|
|
|
|
ip6tc_free(h);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
update_pinhole(unsigned short uid, unsigned int timestamp)
|
|
|
|
{
|
|
|
|
struct pinhole_t * p;
|
|
|
|
|
|
|
|
p = get_pinhole(uid);
|
|
|
|
if(p) {
|
|
|
|
p->timestamp = timestamp;
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return -2; /* Not found */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
get_pinhole_info(unsigned short uid,
|
2014-05-10 11:09:48 +00:00
|
|
|
char * rem_host, int rem_hostlen,
|
|
|
|
unsigned short * rem_port,
|
|
|
|
char * int_client, int int_clientlen,
|
|
|
|
unsigned short * int_port,
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
struct pinhole_t * p;
|
|
|
|
|
|
|
|
p = get_pinhole(uid);
|
|
|
|
if(!p)
|
|
|
|
return -2; /* Not found */
|
2015-02-10 15:04:10 +00:00
|
|
|
if(rem_host && (rem_host[0] != '\0')) {
|
2012-05-01 22:47:42 +00:00
|
|
|
if(inet_ntop(AF_INET6, &p->saddr, rem_host, rem_hostlen) == NULL)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if(rem_port)
|
|
|
|
*rem_port = p->sport;
|
|
|
|
if(int_client) {
|
|
|
|
if(inet_ntop(AF_INET6, &p->daddr, int_client, int_clientlen) == NULL)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if(int_port)
|
|
|
|
*int_port = p->dport;
|
|
|
|
if(proto)
|
|
|
|
*proto = p->proto;
|
|
|
|
if(timestamp)
|
|
|
|
*timestamp = p->timestamp;
|
2014-05-10 11:09:48 +00:00
|
|
|
if (desc)
|
|
|
|
strncpy(desc, p->desc, desclen);
|
2012-05-07 22:21:03 +00:00
|
|
|
if(packets || bytes) {
|
|
|
|
/* theses informations need to be read from netfilter */
|
|
|
|
IP6TC_HANDLE h;
|
|
|
|
const struct ip6t_entry * e;
|
|
|
|
const struct ip6t_entry_match * match;
|
|
|
|
h = ip6tc_init("filter");
|
|
|
|
if(!h) {
|
|
|
|
syslog(LOG_ERR, "ip6tc_init error : %s", ip6tc_strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
for(e = ip6tc_first_rule(miniupnpd_v6_filter_chain, h);
|
|
|
|
e;
|
|
|
|
e = ip6tc_next_rule(e, h)) {
|
|
|
|
if((e->ipv6.proto == p->proto) &&
|
|
|
|
(0 == memcmp(&e->ipv6.src, &p->saddr, sizeof(e->ipv6.src))) &&
|
|
|
|
(0 == memcmp(&e->ipv6.dst, &p->daddr, sizeof(e->ipv6.dst)))) {
|
|
|
|
const struct ip6t_tcp * info;
|
|
|
|
match = (const struct ip6t_entry_match *)&e->elems;
|
|
|
|
info = (const struct ip6t_tcp *)&match->data;
|
|
|
|
if((info->spts[0] == p->sport) && (info->dpts[0] == p->dport)) {
|
|
|
|
if(packets)
|
|
|
|
*packets = e->counters.pcnt;
|
|
|
|
if(bytes)
|
|
|
|
*bytes = e->counters.bcnt;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ip6tc_free(h);
|
|
|
|
}
|
2012-05-01 22:47:42 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-05-10 13:27:03 +00:00
|
|
|
int get_pinhole_uid_by_index(int index)
|
|
|
|
{
|
|
|
|
struct pinhole_t * p;
|
|
|
|
|
|
|
|
for(p = pinhole_list.lh_first; p != NULL; p = p->entries.le_next)
|
|
|
|
if (!index--)
|
|
|
|
return p->uid;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-05-08 20:51:23 +00:00
|
|
|
int
|
|
|
|
clean_pinhole_list(unsigned int * next_timestamp)
|
|
|
|
{
|
|
|
|
unsigned int min_ts = UINT_MAX;
|
|
|
|
struct pinhole_t * p;
|
|
|
|
time_t current_time;
|
|
|
|
int n = 0;
|
|
|
|
|
2018-03-13 23:09:42 +00:00
|
|
|
current_time = upnp_time();
|
2012-05-08 20:51:23 +00:00
|
|
|
p = pinhole_list.lh_first;
|
|
|
|
while(p != NULL) {
|
|
|
|
if(p->timestamp <= (unsigned int)current_time) {
|
|
|
|
unsigned short uid = p->uid;
|
|
|
|
syslog(LOG_INFO, "removing expired pinhole with uid=%hu", uid);
|
|
|
|
p = p->entries.le_next;
|
|
|
|
if(delete_pinhole(uid) == 0)
|
|
|
|
n++;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
if(p->timestamp < min_ts)
|
|
|
|
min_ts = p->timestamp;
|
|
|
|
p = p->entries.le_next;
|
|
|
|
}
|
|
|
|
}
|
2012-09-18 08:31:10 +00:00
|
|
|
if(next_timestamp && (min_ts != UINT_MAX))
|
2012-05-08 20:51:23 +00:00
|
|
|
*next_timestamp = min_ts;
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2014-05-10 11:09:48 +00:00
|
|
|
#endif /* ENABLE_UPNPPINHOLE */
|