mirror of
https://github.com/status-im/miniupnp.git
synced 2025-01-09 21:55:57 +00:00
f934bf5d17
Also update tests in miniupnpd/pf/
111 lines
2.9 KiB
C
111 lines
2.9 KiB
C
/* $Id: pfpinhole.c,v 1.2 2012/04/18 20:45:33 nanard Exp $ */
|
|
/* MiniUPnP project
|
|
* http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/
|
|
* (c) 2006-2012 Thomas Bernard
|
|
* 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
|
|
#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>
|
|
|
|
#include "../config.h"
|
|
#include "../upnpglobalvars.h"
|
|
|
|
/* /dev/pf when opened */
|
|
extern int dev;
|
|
|
|
int add_pinhole (const char * ifname,
|
|
const char * rem_host, unsigned short rem_port,
|
|
const char * int_client, unsigned short int_port,
|
|
int proto)
|
|
{
|
|
int r;
|
|
struct pfioc_rule pcr;
|
|
|
|
if(dev<0) {
|
|
syslog(LOG_ERR, "pf device is not open");
|
|
return -1;
|
|
}
|
|
r = 0;
|
|
memset(&pcr, 0, sizeof(pcr));
|
|
{
|
|
pcr.rule.direction = PF_IN;
|
|
pcr.rule.action = PF_PASS;
|
|
pcr.rule.af = AF_INET6;
|
|
#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;
|
|
/*strlcpy(pcr.rule.label, desc, PF_RULE_LABEL_SIZE);*/
|
|
if(queue)
|
|
strlcpy(pcr.rule.qname, queue, PF_QNAME_SIZE);
|
|
if(tag)
|
|
strlcpy(pcr.rule.tagname, tag, PF_TAG_NAME_SIZE);
|
|
|
|
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] != '*')
|
|
{
|
|
inet_pton(AF_INET6, rem_host, &pcr.rule.src.addr.v.a.addr.v6);
|
|
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);
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|