|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdlib.h> |
|
|
#include <stdio.h> |
|
|
#include <sys/types.h> |
|
|
#include <sys/socket.h> |
|
|
#include <netinet/in.h> |
|
|
#include <netinet/tcp.h> |
|
|
#include <sys/uio.h> |
|
|
#include <unistd.h> |
|
|
#include <errno.h> |
|
|
#include <string.h> |
|
|
#include <arpa/inet.h> |
|
|
|
|
|
#ifndef HAVE_NAT |
|
|
|
|
|
void |
|
|
ipfw_show_nat(int ac, char **av) |
|
|
{ |
|
|
D("unsupported"); |
|
|
} |
|
|
|
|
|
void |
|
|
ipfw_config_nat(int ac, char **av) |
|
|
{ |
|
|
D("unsupported"); |
|
|
} |
|
|
#endif |
|
|
|
|
|
#ifdef NEED_STRTONUM |
|
|
|
|
|
long long int |
|
|
strtonum(const char *nptr, long long minval, long long maxval, |
|
|
const char **errstr) |
|
|
{ |
|
|
long long ret; |
|
|
int errno_c = errno; |
|
|
|
|
|
errno = 0; |
|
|
#ifdef TCC |
|
|
ret = strtol(nptr, (char **)errstr, 0); |
|
|
#else |
|
|
ret = strtoll(nptr, (char **)errstr, 0); |
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (errno != 0 || nptr == *errstr || **errstr != '\0') |
|
|
*errstr = "invalid"; |
|
|
else { |
|
|
*errstr = NULL; |
|
|
errno = errno_c; |
|
|
} |
|
|
return ret; |
|
|
} |
|
|
|
|
|
int |
|
|
ishexnumber(int c) |
|
|
{ |
|
|
return ((c >= '0' && c <= '9') || |
|
|
(c >= 'a' && c <= 'f') || |
|
|
(c >= 'A' && c <= 'F') ); |
|
|
} |
|
|
|
|
|
#endif |
|
|
|
|
|
#ifdef __linux__ |
|
|
|
|
|
|
|
|
int optreset; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
size_t |
|
|
strlcpy(dst, src, siz) |
|
|
char *dst; |
|
|
const char *src; |
|
|
size_t siz; |
|
|
{ |
|
|
char *d = dst; |
|
|
const char *s = src; |
|
|
size_t n = siz; |
|
|
|
|
|
|
|
|
if (n != 0 && --n != 0) { |
|
|
do { |
|
|
if ((*d++ = *s++) == 0) |
|
|
break; |
|
|
} while (--n != 0); |
|
|
} |
|
|
|
|
|
|
|
|
if (n == 0) { |
|
|
if (siz != 0) |
|
|
*d = '\0'; |
|
|
while (*s++) |
|
|
; |
|
|
} |
|
|
|
|
|
return(s - src - 1); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#if defined (EMULATE_SYSCTL) |
|
|
|
|
|
#include <net/if.h> |
|
|
#include <netinet/ip.h> |
|
|
#include <netinet/ip_fw.h> |
|
|
#include <netinet/ip_dummynet.h> |
|
|
int do_cmd(int optname, void *optval, uintptr_t optlen); |
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int |
|
|
sysctlbyname(const char *name, void *oldp, size_t *oldlenp, void *newp, |
|
|
size_t newlen) |
|
|
{ |
|
|
#if defined (EMULATE_SYSCTL) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int l; |
|
|
int ret; |
|
|
struct dn_id* oid; |
|
|
struct sysctlhead* entry; |
|
|
char* pstring; |
|
|
char* pdata; |
|
|
FILE* fp; |
|
|
|
|
|
if((oldlenp != NULL) && ((int)*oldlenp < 0)) |
|
|
fp = (FILE*)oldp; |
|
|
else |
|
|
fp = stdout; |
|
|
if(newp != NULL && newlen != 0) |
|
|
{ |
|
|
|
|
|
l = sizeof(struct dn_id) + sizeof(struct sysctlhead) + strlen(name)+1 + newlen; |
|
|
oid = malloc(l); |
|
|
if (oid == NULL) |
|
|
return -1; |
|
|
oid->len = l; |
|
|
oid->type = DN_SYSCTL_SET; |
|
|
oid->id = DN_API_VERSION; |
|
|
|
|
|
entry = (struct sysctlhead*)(oid+1); |
|
|
pdata = (char*)(entry+1); |
|
|
pstring = pdata + newlen; |
|
|
|
|
|
entry->blocklen = ((sizeof(struct sysctlhead) + strlen(name)+1 + newlen) + 3) & ~3; |
|
|
entry->namelen = strlen(name)+1; |
|
|
entry->flags = 0; |
|
|
entry->datalen = newlen; |
|
|
|
|
|
bcopy(newp, pdata, newlen); |
|
|
bcopy(name, pstring, strlen(name)+1); |
|
|
|
|
|
ret = do_cmd(IP_DUMMYNET3, oid, (uintptr_t)l); |
|
|
if (ret != 0) |
|
|
return -1; |
|
|
} |
|
|
else |
|
|
{ |
|
|
|
|
|
l = sizeof(struct dn_id); |
|
|
oid = malloc(l); |
|
|
if (oid == NULL) |
|
|
return -1; |
|
|
oid->len = l; |
|
|
oid->type = DN_SYSCTL_GET; |
|
|
oid->id = DN_API_VERSION; |
|
|
|
|
|
ret = do_cmd(-IP_DUMMYNET3, oid, (uintptr_t)&l); |
|
|
if (ret != 0) |
|
|
return -1; |
|
|
|
|
|
l=oid->id; |
|
|
free(oid); |
|
|
oid = malloc(l); |
|
|
if (oid == NULL) |
|
|
return -1; |
|
|
oid->len = l; |
|
|
oid->type = DN_SYSCTL_GET; |
|
|
oid->id = DN_API_VERSION; |
|
|
|
|
|
ret = do_cmd(-IP_DUMMYNET3, oid, (uintptr_t)&l); |
|
|
if (ret != 0) |
|
|
return -1; |
|
|
|
|
|
entry = (struct sysctlhead*)(oid+1); |
|
|
while(entry->blocklen != 0) |
|
|
{ |
|
|
pdata = (char*)(entry+1); |
|
|
pstring = pdata+entry->datalen; |
|
|
|
|
|
|
|
|
if(name != NULL && oldp != NULL && *oldlenp > 0) |
|
|
{ |
|
|
|
|
|
if(strcmp(name,pstring) == 0) |
|
|
{ |
|
|
|
|
|
if(*oldlenp < entry->datalen) |
|
|
{ |
|
|
printf("%s error: buffer too small\n",__FUNCTION__); |
|
|
return -1; |
|
|
} |
|
|
*oldlenp = entry->datalen; |
|
|
bcopy(pdata, oldp, *oldlenp); |
|
|
return 0; |
|
|
} |
|
|
} |
|
|
else |
|
|
{ |
|
|
|
|
|
if( name == NULL ) |
|
|
goto print; |
|
|
if ( (strncmp(pstring,name,strlen(name)) == 0) && ( pstring[strlen(name)]=='\0' || pstring[strlen(name)]=='.' ) ) |
|
|
goto print; |
|
|
else |
|
|
goto skip; |
|
|
print: |
|
|
fprintf(fp, "%s: ",pstring); |
|
|
switch( entry->flags >> 2 ) |
|
|
{ |
|
|
case SYSCTLTYPE_LONG: |
|
|
fprintf(fp, "%li ", *(long*)(pdata)); |
|
|
break; |
|
|
case SYSCTLTYPE_UINT: |
|
|
fprintf(fp, "%u ", *(unsigned int*)(pdata)); |
|
|
break; |
|
|
case SYSCTLTYPE_ULONG: |
|
|
fprintf(fp, "%lu ", *(unsigned long*)(pdata)); |
|
|
break; |
|
|
case SYSCTLTYPE_INT: |
|
|
default: |
|
|
fprintf(fp, "%i ", *(int*)(pdata)); |
|
|
} |
|
|
if( (entry->flags & 0x00000003) == CTLFLAG_RD ) |
|
|
fprintf(fp, "\t(read only)\n"); |
|
|
else |
|
|
fprintf(fp, "\n"); |
|
|
skip: ; |
|
|
} |
|
|
entry = (struct sysctlhead*)((unsigned char*)entry + entry->blocklen); |
|
|
} |
|
|
free(oid); |
|
|
return 0; |
|
|
} |
|
|
|
|
|
return -1; |
|
|
|
|
|
#else |
|
|
FILE *fp; |
|
|
char *basename = "/sys/module/ipfw_mod/parameters/"; |
|
|
char filename[256]; |
|
|
char *varp; |
|
|
int ret = 0; |
|
|
int d; |
|
|
|
|
|
if (name == NULL) |
|
|
return -1; |
|
|
|
|
|
|
|
|
varp = strrchr(name, '.'); |
|
|
if (varp == NULL) |
|
|
return -1; |
|
|
|
|
|
snprintf(filename, sizeof(filename), "%s%s", basename, varp+1); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (oldp && oldlenp) { |
|
|
fp = fopen(filename, "r"); |
|
|
if (fp == NULL) { |
|
|
fprintf(stderr, "%s fopen error reading filename %s\n", __FUNCTION__, filename); |
|
|
return -1; |
|
|
} |
|
|
if (*oldlenp == 4) { |
|
|
if (fscanf(fp, "%d", &d) == 1) |
|
|
memcpy(oldp, &d, *oldlenp); |
|
|
else |
|
|
ret = -1; |
|
|
} |
|
|
fclose(fp); |
|
|
} |
|
|
|
|
|
if (newp && newlen) { |
|
|
fp = fopen(filename, "w"); |
|
|
if (fp == NULL) { |
|
|
fprintf(stderr, "%s fopen error writing filename %s\n", __FUNCTION__, filename); |
|
|
return -1; |
|
|
} |
|
|
if (newlen == 4) { |
|
|
if (fprintf(fp, "%d", *(int*)newp) < 1) |
|
|
ret = -1; |
|
|
} |
|
|
|
|
|
fclose(fp); |
|
|
} |
|
|
|
|
|
return ret; |
|
|
#endif |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
enum sock_type {GET_SOCKOPT, SET_SOCKOPT}; |
|
|
|
|
|
struct wire_hdr { |
|
|
uint32_t optlen; |
|
|
uint32_t level; |
|
|
uint32_t optname; |
|
|
uint32_t dir; |
|
|
}; |
|
|
|
|
|
|
|
|
static int |
|
|
writen(int fd, const char *buf, int len) |
|
|
{ |
|
|
int i; |
|
|
|
|
|
for (; len > 0; buf += i, len -= i) { |
|
|
i = write(fd, buf, len); |
|
|
ND("have %d wrote %d", len, i); |
|
|
if (i < 0) { |
|
|
if (errno == EAGAIN) |
|
|
continue; |
|
|
return -1; |
|
|
} |
|
|
} |
|
|
return 0; |
|
|
} |
|
|
|
|
|
|
|
|
static int |
|
|
readn(int fd, char *buf, int len) |
|
|
{ |
|
|
int i, pos; |
|
|
|
|
|
for (pos = 0; pos < len; pos += i) { |
|
|
i = read(fd, buf + pos, len - pos); |
|
|
ND("have %d want %d got %d", pos, len, i); |
|
|
if (i < 0) { |
|
|
if (errno == EAGAIN) |
|
|
continue; |
|
|
return -1; |
|
|
} |
|
|
} |
|
|
ND("full read got %d", pos); |
|
|
return 0; |
|
|
} |
|
|
|
|
|
int |
|
|
__sockopt2(int s, int level, int optname, void *optval, socklen_t *optlen, |
|
|
enum sopt_dir dir) |
|
|
{ |
|
|
struct wire_hdr r; |
|
|
int len = optlen && optval ? *optlen : 0; |
|
|
int new_errno; |
|
|
|
|
|
ND("dir %d optlen %d level %d optname %d", dir, len, level, optname); |
|
|
|
|
|
r.optlen = htonl(len); |
|
|
r.level = htonl(level); |
|
|
r.optname = htonl(optname); |
|
|
r.dir = htonl(dir); |
|
|
|
|
|
if (writen(s, (const char *) &r, sizeof(r))) |
|
|
return -1; |
|
|
|
|
|
|
|
|
if (len < 0) { |
|
|
fprintf(stderr, "%s invalid args found\n", __FUNCTION__); |
|
|
return -1; |
|
|
} else if (len > 0) { |
|
|
if (writen(s, optval, len)) |
|
|
return -1; |
|
|
} |
|
|
|
|
|
|
|
|
if (readn(s, (char *)&r, sizeof(r))) |
|
|
return -1; |
|
|
len = ntohl(r.optlen); |
|
|
ND("got header, datalen %d", len); |
|
|
if (len > 0) { |
|
|
if (readn(s, optval, len)) { |
|
|
return -1; |
|
|
} |
|
|
} |
|
|
if (optlen) |
|
|
*optlen = ntohl(r.optlen); |
|
|
new_errno = ntohl(r.level); |
|
|
if (new_errno) |
|
|
errno = new_errno; |
|
|
return (new_errno ? -1 : 0); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int |
|
|
getsockopt2(int s, int level, int optname, void *optval, |
|
|
socklen_t *optlen) |
|
|
{ |
|
|
return __sockopt2(s, level, optname, optval, optlen, SOPT_GET); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int |
|
|
setsockopt2(int s, int level, int optname, void *optval, |
|
|
socklen_t optlen) |
|
|
{ |
|
|
|
|
|
return __sockopt2(s, level, optname, optval, &optlen, SOPT_SET); |
|
|
} |
|
|
|
|
|
#ifdef socket |
|
|
#undef socket |
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int |
|
|
do_connect(const char *addr, int port) |
|
|
{ |
|
|
int conn_fd; |
|
|
|
|
|
|
|
|
#ifdef NETLINK |
|
|
|
|
|
struct rtnl_handle rth; |
|
|
|
|
|
conn_fd = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE); |
|
|
#else |
|
|
struct sockaddr_in server; |
|
|
const char *s; |
|
|
|
|
|
conn_fd = socket(AF_INET, SOCK_STREAM, 0); |
|
|
if (conn_fd < 0) { |
|
|
perror("socket"); |
|
|
return -1; |
|
|
} |
|
|
#endif |
|
|
#ifndef NETLINK |
|
|
|
|
|
bzero(&server, sizeof(server)); |
|
|
server.sin_family = AF_INET; |
|
|
|
|
|
|
|
|
s = getenv("IPFW_HOST"); |
|
|
if (s) |
|
|
addr = s; |
|
|
inet_aton(addr, &server.sin_addr); |
|
|
s = getenv("IPFW_PORT"); |
|
|
if (s && atoi(s) > 0) |
|
|
port = atoi(s); |
|
|
server.sin_port = htons(port); |
|
|
|
|
|
|
|
|
if (connect(conn_fd, (struct sockaddr*) &server, sizeof(server)) < 0) { |
|
|
perror("connect"); |
|
|
return -1; |
|
|
} |
|
|
#ifdef setsockopt |
|
|
#undef setsockopt |
|
|
#undef getsockopt |
|
|
#endif |
|
|
{ |
|
|
int on = 1, ret; |
|
|
ret = setsockopt(conn_fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)); |
|
|
ND("set TCP_NODELAY %d returns %d", on, ret); |
|
|
} |
|
|
if (0) |
|
|
fprintf(stderr, "connected to %s:%d\n", |
|
|
inet_ntoa(server.sin_addr), ntohs(server.sin_port)); |
|
|
#endif |
|
|
return conn_fd; |
|
|
} |
|
|
|