2020-11-04 21:31:47 +00:00
|
|
|
/* $Id: portinuse.c,v 1.12 2020/11/04 21:29:50 nanard Exp $ */
|
2017-07-05 21:06:16 +00:00
|
|
|
/* vim: tabstop=4 shiftwidth=4 noexpandtab
|
|
|
|
* MiniUPnP project
|
2020-10-30 21:14:45 +00:00
|
|
|
* (c) 2007-2020 Thomas Bernard
|
2019-10-13 19:33:32 +00:00
|
|
|
* http://miniupnp.free.fr/ or https://miniupnp.tuxfamily.org/
|
2012-07-10 21:25:29 +00:00
|
|
|
* This software is subject to the conditions detailed
|
|
|
|
* in the LICENCE file provided within the distribution */
|
2014-04-13 11:15:58 +00:00
|
|
|
|
|
|
|
#if defined(__DragonFly__) || defined(__FreeBSD__)
|
2014-03-20 12:40:53 +00:00
|
|
|
#include <err.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#endif
|
2014-04-13 11:15:58 +00:00
|
|
|
|
2012-07-10 21:25:29 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <syslog.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
2014-04-13 11:15:58 +00:00
|
|
|
|
2014-03-14 11:08:13 +00:00
|
|
|
#if defined(__OpenBSD__)
|
|
|
|
#include <sys/queue.h>
|
2017-06-11 10:52:55 +00:00
|
|
|
#include <sys/select.h>
|
2014-03-14 11:08:13 +00:00
|
|
|
#include <kvm.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <nlist.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <net/route.h>
|
|
|
|
#include <netinet/in_systm.h>
|
|
|
|
#include <netinet/ip.h>
|
|
|
|
#include <netinet/in_pcb.h>
|
|
|
|
#endif
|
2012-07-10 21:25:29 +00:00
|
|
|
|
2014-04-13 11:15:58 +00:00
|
|
|
#if defined(__DragonFly__) || defined(__FreeBSD__)
|
2014-03-20 12:40:53 +00:00
|
|
|
#include <sys/socketvar.h>
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
/* sys/socketvar.h must be included above the following headers */
|
|
|
|
#include <netinet/in_pcb.h>
|
|
|
|
#include <netinet/tcp_var.h>
|
|
|
|
#endif
|
|
|
|
|
2012-07-10 21:25:29 +00:00
|
|
|
#include "macros.h"
|
|
|
|
#include "config.h"
|
|
|
|
#include "upnpglobalvars.h"
|
|
|
|
#include "getifaddr.h"
|
|
|
|
#include "portinuse.h"
|
|
|
|
|
2014-03-13 13:48:52 +00:00
|
|
|
#if defined(USE_NETFILTER)
|
2013-12-13 11:29:01 +00:00
|
|
|
#include "netfilter/iptcrdr.h"
|
2014-03-13 13:48:52 +00:00
|
|
|
#endif
|
2013-12-13 11:29:01 +00:00
|
|
|
|
2014-03-13 13:48:52 +00:00
|
|
|
#ifdef CHECK_PORTINUSE
|
|
|
|
|
|
|
|
#if defined(USE_NETFILTER)
|
2012-07-10 21:25:29 +00:00
|
|
|
/* Hardcoded for now. Ideally would come from .conf file */
|
2017-07-05 18:40:51 +00:00
|
|
|
# ifdef TOMATO
|
2017-07-05 21:06:07 +00:00
|
|
|
const char *chains_to_check[] = { "WANPREROUTING" , 0 };
|
2017-07-05 18:40:51 +00:00
|
|
|
# else
|
2017-07-05 21:06:07 +00:00
|
|
|
const char *chains_to_check[] = { "PREROUTING" , 0 };
|
2017-07-05 18:40:51 +00:00
|
|
|
# endif
|
2014-03-13 13:48:52 +00:00
|
|
|
#endif
|
2012-07-10 21:25:29 +00:00
|
|
|
|
2014-03-13 13:49:05 +00:00
|
|
|
int
|
|
|
|
port_in_use(const char *if_name,
|
|
|
|
unsigned eport, int proto,
|
|
|
|
const char *iaddr, unsigned iport)
|
2012-07-10 21:25:29 +00:00
|
|
|
{
|
|
|
|
int found = 0;
|
|
|
|
char ip_addr_str[INET_ADDRSTRLEN];
|
2013-12-13 11:29:01 +00:00
|
|
|
struct in_addr ip_addr;
|
2014-03-13 13:58:36 +00:00
|
|
|
#ifdef __linux__
|
|
|
|
/* linux code */
|
|
|
|
char line[256];
|
|
|
|
FILE *f;
|
2014-03-13 13:49:05 +00:00
|
|
|
const char * tcpfile = "/proc/net/tcp";
|
|
|
|
const char * udpfile = "/proc/net/udp";
|
2014-03-14 11:08:13 +00:00
|
|
|
#endif
|
2012-07-10 21:25:29 +00:00
|
|
|
|
2014-04-09 13:38:54 +00:00
|
|
|
if(getifaddr(if_name, ip_addr_str, INET_ADDRSTRLEN, &ip_addr, NULL) < 0) {
|
2013-12-13 11:29:01 +00:00
|
|
|
ip_addr.s_addr = 0;
|
2014-04-09 13:38:54 +00:00
|
|
|
ip_addr_str[0] = '\0';
|
|
|
|
}
|
2012-07-10 21:25:29 +00:00
|
|
|
|
2016-01-04 03:56:39 +00:00
|
|
|
syslog(LOG_DEBUG, "Check protocol %s for port %u on ext_if %s %s, %08X",
|
2014-03-20 12:49:10 +00:00
|
|
|
(proto==IPPROTO_TCP)?"tcp":"udp", eport, if_name,
|
|
|
|
ip_addr_str, (unsigned)ip_addr.s_addr);
|
2012-07-10 21:25:29 +00:00
|
|
|
|
2014-03-28 11:45:39 +00:00
|
|
|
/* Phase 1 : check for local sockets (would be listed by netstat) */
|
|
|
|
#if defined(__linux__)
|
2014-03-14 11:08:13 +00:00
|
|
|
f = fopen((proto==IPPROTO_TCP)?tcpfile:udpfile, "r");
|
|
|
|
if (!f) {
|
|
|
|
syslog(LOG_ERR, "cannot open %s", (proto==IPPROTO_TCP)?tcpfile:udpfile);
|
2014-03-20 12:49:10 +00:00
|
|
|
return -1;
|
2014-03-14 11:08:13 +00:00
|
|
|
}
|
|
|
|
|
2012-07-10 21:25:29 +00:00
|
|
|
while (fgets(line, 255, f)) {
|
|
|
|
char eaddr[68];
|
|
|
|
unsigned tmp_port;
|
|
|
|
if (sscanf(line, "%*d: %64[0-9A-Fa-f]:%x %*x:%*x %*x %*x:%*x "
|
|
|
|
"%*x:%*x %*x %*d %*d %*llu",
|
|
|
|
eaddr, &tmp_port) == 2
|
|
|
|
) {
|
|
|
|
/* TODO add IPV6 support if enabled
|
|
|
|
* Presently assumes IPV4 */
|
2014-03-28 11:45:39 +00:00
|
|
|
#ifdef DEBUG
|
2016-01-04 03:56:39 +00:00
|
|
|
syslog(LOG_DEBUG, "port_in_use check port %u and address %s", tmp_port, eaddr);
|
2014-03-28 11:45:39 +00:00
|
|
|
#endif
|
2012-07-10 21:25:29 +00:00
|
|
|
if (tmp_port == eport) {
|
|
|
|
char tmp_addr[4];
|
|
|
|
struct in_addr *tmp_ip_addr = (struct in_addr *)tmp_addr;
|
|
|
|
if (sscanf(eaddr,"%2hhx%2hhx%2hhx%2hhx",
|
|
|
|
&tmp_addr[3],&tmp_addr[2],&tmp_addr[1],&tmp_addr[0]) == 4)
|
|
|
|
{
|
|
|
|
if (tmp_ip_addr->s_addr == 0 || tmp_ip_addr->s_addr == ip_addr.s_addr)
|
|
|
|
{
|
|
|
|
found++;
|
|
|
|
break; /* don't care how many, just that we found at least one */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose(f);
|
|
|
|
|
2014-03-28 11:45:39 +00:00
|
|
|
#elif defined(__OpenBSD__)
|
2014-03-14 11:08:13 +00:00
|
|
|
static struct nlist list[] = {
|
|
|
|
#if 0
|
|
|
|
{"_tcpstat", 0, 0, 0, 0},
|
|
|
|
{"_udpstat", 0, 0, 0, 0},
|
|
|
|
{"_tcbinfo", 0, 0, 0, 0},
|
|
|
|
{"_udbinfo", 0, 0, 0, 0},
|
|
|
|
#endif
|
|
|
|
{"_tcbtable", 0, 0, 0, 0},
|
|
|
|
{"_udbtable", 0, 0, 0, 0},
|
|
|
|
{NULL,0, 0, 0, 0}
|
|
|
|
};
|
|
|
|
char errstr[_POSIX2_LINE_MAX];
|
|
|
|
kvm_t *kd;
|
|
|
|
ssize_t n;
|
|
|
|
struct inpcbtable table;
|
|
|
|
struct inpcb *next;
|
|
|
|
struct inpcb inpcb;
|
|
|
|
kd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errstr);
|
|
|
|
if(!kd) {
|
2014-03-28 11:45:39 +00:00
|
|
|
syslog(LOG_ERR, "%s: kvm_openfiles(): %s",
|
|
|
|
"portinuse()", errstr);
|
2014-03-14 11:08:13 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if(kvm_nlist(kd, list) < 0) {
|
2014-03-28 11:45:39 +00:00
|
|
|
syslog(LOG_ERR, "%s: kvm_nlist(): %s",
|
|
|
|
"portinuse()", kvm_geterr(kd));
|
2014-03-14 11:08:13 +00:00
|
|
|
kvm_close(kd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
n = kvm_read(kd, list[(proto==IPPROTO_TCP)?0:1].n_value, &table, sizeof(table));
|
|
|
|
if(n < 0) {
|
2014-03-28 11:45:39 +00:00
|
|
|
syslog(LOG_ERR, "%s: kvm_read(): %s",
|
|
|
|
"portinuse()", kvm_geterr(kd));
|
2014-03-14 11:08:13 +00:00
|
|
|
kvm_close(kd);
|
|
|
|
return -1;
|
|
|
|
}
|
2020-10-30 21:14:45 +00:00
|
|
|
/* inpt_queue was CIRCLEQ_HEAD, it is TAILQ_HEAD since OpenBSD 5.5 */
|
|
|
|
#ifdef INPT_QUEUE_IS_CIRCLEQ
|
|
|
|
next = CIRCLEQ_FIRST(&table.inpt_queue);
|
|
|
|
#else
|
|
|
|
next = TAILQ_FIRST(&table.inpt_queue);
|
|
|
|
#endif
|
2014-03-14 11:08:13 +00:00
|
|
|
while(next != NULL) {
|
|
|
|
if(((u_long)next & 3) != 0) break;
|
|
|
|
n = kvm_read(kd, (u_long)next, &inpcb, sizeof(inpcb));
|
|
|
|
if(n < 0) {
|
|
|
|
syslog(LOG_ERR, "kvm_read(): %s", kvm_geterr(kd));
|
|
|
|
break;
|
|
|
|
}
|
2020-10-30 21:14:45 +00:00
|
|
|
#ifdef INPT_QUEUE_IS_CIRCLEQ
|
|
|
|
next = CIRCLEQ_NEXT(&inpcb, inp_queue);
|
|
|
|
#else
|
|
|
|
next = TAILQ_NEXT(&inpcb, inp_queue);
|
|
|
|
#endif
|
2014-03-28 11:45:39 +00:00
|
|
|
/* skip IPv6 sockets */
|
2014-03-14 11:08:13 +00:00
|
|
|
if((inpcb.inp_flags & INP_IPV6) != 0)
|
|
|
|
continue;
|
2014-03-28 11:45:39 +00:00
|
|
|
#ifdef DEBUG
|
2014-03-14 11:08:13 +00:00
|
|
|
syslog(LOG_DEBUG, "%08lx:%hu %08lx:%hu",
|
|
|
|
(u_long)inpcb.inp_laddr.s_addr, ntohs(inpcb.inp_lport),
|
|
|
|
(u_long)inpcb.inp_faddr.s_addr, ntohs(inpcb.inp_fport));
|
2014-03-28 11:45:39 +00:00
|
|
|
#endif
|
2014-03-14 11:08:13 +00:00
|
|
|
if(eport == (unsigned)ntohs(inpcb.inp_lport)) {
|
|
|
|
if(inpcb.inp_laddr.s_addr == INADDR_ANY || inpcb.inp_laddr.s_addr == ip_addr.s_addr) {
|
|
|
|
found++;
|
|
|
|
break; /* don't care how many, just that we found at least one */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
kvm_close(kd);
|
|
|
|
|
2014-03-28 11:45:39 +00:00
|
|
|
#elif defined(__DragonFly__)
|
2014-03-20 12:40:53 +00:00
|
|
|
const char *varname;
|
|
|
|
struct xinpcb *xip;
|
|
|
|
struct xtcpcb *xtp;
|
|
|
|
struct inpcb *inp;
|
|
|
|
void *buf = NULL;
|
|
|
|
void *so_begin, *so_end;
|
|
|
|
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
switch (proto) {
|
|
|
|
case IPPROTO_TCP:
|
|
|
|
varname = "net.inet.tcp.pcblist";
|
|
|
|
break;
|
|
|
|
case IPPROTO_UDP:
|
|
|
|
varname = "net.inet.udp.pcblist";
|
|
|
|
break;
|
|
|
|
default:
|
2014-03-20 13:07:52 +00:00
|
|
|
syslog(LOG_ERR, "port_in_use() unknown proto=%d", proto);
|
|
|
|
return -1;
|
2014-03-20 12:40:53 +00:00
|
|
|
}
|
|
|
|
|
2014-03-20 13:07:52 +00:00
|
|
|
if (sysctlbyname(varname, NULL, &len, NULL, 0) < 0) {
|
|
|
|
syslog(LOG_ERR, "sysctlbyname(%s, NULL, ...): %m", varname);
|
|
|
|
return -1;
|
2014-03-20 12:40:53 +00:00
|
|
|
}
|
2014-03-20 13:07:52 +00:00
|
|
|
buf = malloc(len);
|
|
|
|
if (buf == NULL) {
|
|
|
|
syslog(LOG_ERR, "malloc(%u) failed", (unsigned)len);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (sysctlbyname(varname, buf, &len, NULL, 0) < 0) {
|
|
|
|
syslog(LOG_ERR, "sysctlbyname(%s, buf, ...): %m", varname);
|
|
|
|
free(buf);
|
|
|
|
return -1;
|
2014-03-20 12:40:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
so_begin = buf;
|
|
|
|
so_end = (uint8_t *)buf + len;
|
|
|
|
for (so_begin = buf, so_end = (uint8_t *)so_begin + len;
|
|
|
|
(uint8_t *)so_begin + sizeof(size_t) < (uint8_t *)so_end &&
|
|
|
|
(uint8_t *)so_begin + *(size_t *)so_begin <= (uint8_t *)so_end;
|
|
|
|
so_begin = (uint8_t *)so_begin + *(size_t *)so_begin) {
|
|
|
|
switch (proto) {
|
|
|
|
case IPPROTO_TCP:
|
|
|
|
xtp = (struct xtcpcb *)so_begin;
|
|
|
|
if (xtp->xt_len != sizeof *xtp) {
|
2014-03-20 13:07:52 +00:00
|
|
|
syslog(LOG_WARNING, "struct xtcpcb size mismatch; %ld vs %ld",
|
|
|
|
(long)xtp->xt_len, sizeof *xtp);
|
|
|
|
free(buf);
|
|
|
|
return -1;
|
2014-03-20 12:40:53 +00:00
|
|
|
}
|
|
|
|
inp = &xtp->xt_inp;
|
|
|
|
break;
|
|
|
|
case IPPROTO_UDP:
|
|
|
|
xip = (struct xinpcb *)so_begin;
|
|
|
|
if (xip->xi_len != sizeof *xip) {
|
2014-03-20 13:07:52 +00:00
|
|
|
syslog(LOG_WARNING, "struct xinpcb size mismatch : %ld vs %ld",
|
|
|
|
(long)xip->xi_len, sizeof *xip);
|
|
|
|
free(buf);
|
|
|
|
return -1;
|
2014-03-20 12:40:53 +00:00
|
|
|
}
|
|
|
|
inp = &xip->xi_inp;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
/* no support for IPv6 */
|
2016-07-23 05:47:52 +00:00
|
|
|
if (INP_ISIPV6(inp) != 0)
|
2014-03-20 12:40:53 +00:00
|
|
|
continue;
|
2020-11-04 21:31:47 +00:00
|
|
|
syslog(LOG_DEBUG, "%08lx:%hu %08lx:%hu <=> %u %08lx:%u",
|
2014-03-20 12:40:53 +00:00
|
|
|
(u_long)inp->inp_laddr.s_addr, ntohs(inp->inp_lport),
|
|
|
|
(u_long)inp->inp_faddr.s_addr, ntohs(inp->inp_fport),
|
|
|
|
eport, (u_long)ip_addr.s_addr, iport
|
|
|
|
);
|
|
|
|
if (eport == (unsigned)ntohs(inp->inp_lport)) {
|
|
|
|
if (inp->inp_laddr.s_addr == INADDR_ANY || inp->inp_laddr.s_addr == ip_addr.s_addr) {
|
|
|
|
found++;
|
|
|
|
break; /* don't care how many, just that we found at least one */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-04-14 07:17:10 +00:00
|
|
|
if (buf) {
|
2014-03-20 12:40:53 +00:00
|
|
|
free(buf);
|
2014-03-20 13:07:52 +00:00
|
|
|
buf = NULL;
|
2014-03-20 12:40:53 +00:00
|
|
|
}
|
2014-04-13 11:15:58 +00:00
|
|
|
#elif defined(__FreeBSD__)
|
|
|
|
const char *varname;
|
|
|
|
struct xinpgen *xig, *exig;
|
|
|
|
struct xinpcb *xip;
|
|
|
|
struct xtcpcb *xtp;
|
2019-03-07 16:09:08 +00:00
|
|
|
struct in_conninfo *inc;
|
2014-04-13 11:15:58 +00:00
|
|
|
void *buf = NULL;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
switch (proto) {
|
|
|
|
case IPPROTO_TCP:
|
|
|
|
varname = "net.inet.tcp.pcblist";
|
|
|
|
break;
|
2014-04-13 11:28:47 +00:00
|
|
|
case IPPROTO_UDP:
|
|
|
|
varname = "net.inet.udp.pcblist";
|
2014-04-13 11:15:58 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
syslog(LOG_ERR, "port_in_use() unknown proto=%d", proto);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sysctlbyname(varname, NULL, &len, NULL, 0) < 0) {
|
|
|
|
syslog(LOG_ERR, "sysctlbyname(%s, NULL, ...): %m", varname);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
buf = malloc(len);
|
|
|
|
if (buf == NULL) {
|
|
|
|
syslog(LOG_ERR, "malloc(%u) failed", (unsigned)len);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (sysctlbyname(varname, buf, &len, NULL, 0) < 0) {
|
|
|
|
syslog(LOG_ERR, "sysctlbyname(%s, buf, ...): %m", varname);
|
|
|
|
free(buf);
|
|
|
|
return -1;
|
2014-04-13 11:28:47 +00:00
|
|
|
}
|
|
|
|
|
2014-04-13 11:15:58 +00:00
|
|
|
xig = (struct xinpgen *)buf;
|
|
|
|
exig = (struct xinpgen *)(void *)((char *)buf + len - sizeof *exig);
|
|
|
|
if (xig->xig_len != sizeof *xig) {
|
|
|
|
syslog(LOG_WARNING, "struct xinpgen size mismatch; %ld vs %ld",
|
|
|
|
(long)xig->xig_len, sizeof *xig);
|
|
|
|
free(buf);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (exig->xig_len != sizeof *exig) {
|
|
|
|
syslog(LOG_WARNING, "struct xinpgen size mismatch; %ld vs %ld",
|
|
|
|
(long)exig->xig_len, sizeof *exig);
|
|
|
|
free(buf);
|
|
|
|
return -1;
|
|
|
|
}
|
2014-04-13 11:28:47 +00:00
|
|
|
|
|
|
|
while (1) {
|
2014-04-13 11:15:58 +00:00
|
|
|
xig = (struct xinpgen *)(void *)((char *)xig + xig->xig_len);
|
|
|
|
if (xig >= exig)
|
|
|
|
break;
|
|
|
|
switch (proto) {
|
|
|
|
case IPPROTO_TCP:
|
|
|
|
xtp = (struct xtcpcb *)xig;
|
|
|
|
if (xtp->xt_len != sizeof *xtp) {
|
|
|
|
syslog(LOG_WARNING, "struct xtcpcb size mismatch; %ld vs %ld",
|
|
|
|
(long)xtp->xt_len, sizeof *xtp);
|
|
|
|
free(buf);
|
|
|
|
return -1;
|
|
|
|
}
|
2019-03-07 16:09:08 +00:00
|
|
|
xip = &xtp->xt_inp;
|
|
|
|
inc = &xip->inp_inc;
|
2014-04-13 11:28:47 +00:00
|
|
|
break;
|
2014-04-13 11:15:58 +00:00
|
|
|
case IPPROTO_UDP:
|
|
|
|
xip = (struct xinpcb *)xig;
|
|
|
|
if (xip->xi_len != sizeof *xip) {
|
|
|
|
syslog(LOG_WARNING, "struct xinpcb size mismatch : %ld vs %ld",
|
|
|
|
(long)xip->xi_len, sizeof *xip);
|
|
|
|
free(buf);
|
|
|
|
return -1;
|
|
|
|
}
|
2019-03-07 16:09:08 +00:00
|
|
|
inc = &xip->inp_inc;
|
2014-04-13 11:15:58 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
/* no support for IPv6 */
|
2019-03-07 16:09:08 +00:00
|
|
|
if ((xip->inp_vflag & INP_IPV6) != 0)
|
2014-04-13 11:15:58 +00:00
|
|
|
continue;
|
2020-11-01 23:26:13 +00:00
|
|
|
syslog(LOG_DEBUG, "%08lx:%hu %08lx:%hu <=> %u %08lx:%u",
|
2019-03-07 16:09:08 +00:00
|
|
|
(u_long)inc->inc_laddr.s_addr, ntohs(inc->inc_lport),
|
|
|
|
(u_long)inc->inc_faddr.s_addr, ntohs(inc->inc_fport),
|
2014-04-13 11:15:58 +00:00
|
|
|
eport, (u_long)ip_addr.s_addr, iport
|
|
|
|
);
|
2019-03-07 16:09:08 +00:00
|
|
|
if (eport == (unsigned)ntohs(inc->inc_lport)) {
|
|
|
|
if (inc->inc_laddr.s_addr == INADDR_ANY || inc->inc_laddr.s_addr == ip_addr.s_addr) {
|
2014-04-13 11:15:58 +00:00
|
|
|
found++;
|
|
|
|
break; /* don't care how many, just that we found at least one */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-04-13 11:28:47 +00:00
|
|
|
if (buf) {
|
2014-04-13 11:15:58 +00:00
|
|
|
free(buf);
|
|
|
|
buf = NULL;
|
|
|
|
}
|
2014-04-14 07:17:10 +00:00
|
|
|
/* #elif __NetBSD__ */
|
2014-03-28 11:45:39 +00:00
|
|
|
#else
|
2014-04-13 11:15:58 +00:00
|
|
|
/* TODO : NetBSD / Darwin (OS X) / Solaris code */
|
2014-03-28 11:45:39 +00:00
|
|
|
#error "No port_in_use() implementation available for this OS"
|
2014-03-20 12:40:53 +00:00
|
|
|
#endif
|
2014-03-14 11:08:13 +00:00
|
|
|
|
2014-03-28 11:45:39 +00:00
|
|
|
/* Phase 2 : check existing mappings
|
|
|
|
* TODO : implement for pf/ipfw/etc. */
|
2014-03-13 13:48:52 +00:00
|
|
|
#if defined(USE_NETFILTER)
|
2012-07-10 21:25:29 +00:00
|
|
|
if (!found) {
|
|
|
|
char iaddr_old[16];
|
|
|
|
unsigned short iport_old;
|
2014-03-14 11:08:13 +00:00
|
|
|
int i;
|
|
|
|
for (i = 0; chains_to_check[i]; i++) {
|
2012-07-10 21:25:29 +00:00
|
|
|
if (get_nat_redirect_rule(chains_to_check[i], if_name, eport, proto,
|
2014-03-13 13:48:52 +00:00
|
|
|
iaddr_old, sizeof(iaddr_old), &iport_old,
|
2012-07-10 21:25:29 +00:00
|
|
|
0, 0, 0, 0, 0, 0, 0) == 0)
|
|
|
|
{
|
|
|
|
syslog(LOG_DEBUG, "port_in_use check port %d on nat chain %s redirected to %s port %d", eport,
|
|
|
|
chains_to_check[i], iaddr_old, iport_old);
|
|
|
|
if (!(strcmp(iaddr, iaddr_old)==0 && iport==iport_old)) {
|
|
|
|
/* only "in use" if redirected to somewhere else */
|
|
|
|
found++;
|
|
|
|
break; /* don't care how many, just that we found at least one */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-03-14 11:08:13 +00:00
|
|
|
#else /* USE_NETFILTER */
|
|
|
|
UNUSED(iport); UNUSED(iaddr);
|
2014-03-13 13:48:52 +00:00
|
|
|
#endif /* USE_NETFILTER */
|
2012-07-10 21:25:29 +00:00
|
|
|
return found;
|
|
|
|
}
|
2014-03-13 13:48:52 +00:00
|
|
|
#endif /* CHECK_PORTINUSE */
|