|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef _GLUE_H |
|
|
#define _GLUE_H |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef __FBSDID |
|
|
#define __FBSDID(x) struct __hack |
|
|
#endif |
|
|
|
|
|
#include <stdint.h> |
|
|
#include <sys/types.h> |
|
|
|
|
|
#define true 1 |
|
|
#ifdef _KERNEL |
|
|
#undef _KERNEL |
|
|
#include <sys/ioctl.h> |
|
|
#include <sys/time.h> |
|
|
#include <errno.h> |
|
|
#define _KERNEL |
|
|
#else |
|
|
#include <sys/ioctl.h> |
|
|
#endif |
|
|
|
|
|
#include <time.h> |
|
|
#ifndef USERSPACE |
|
|
#include <netinet/ether.h> |
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static __inline time_t |
|
|
_long_to_time(long tlong) |
|
|
{ |
|
|
#if 0 |
|
|
if (sizeof(long) == sizeof(__int32_t)) |
|
|
return((time_t)(__int32_t)(tlong)); |
|
|
#else |
|
|
if (sizeof(long) == sizeof(int32_t)) |
|
|
return((time_t)(int32_t)(tlong)); |
|
|
#endif |
|
|
return((time_t)tlong); |
|
|
} |
|
|
|
|
|
#define min(a, b) ((a) < (b) ? (a) : (b) ) |
|
|
|
|
|
|
|
|
|
|
|
#include <sys/time.h> |
|
|
#include <stdio.h> |
|
|
extern char *strrchr(const char *, int); |
|
|
static inline const char *xyz(const char *s) { |
|
|
static char buf[128]; |
|
|
struct timeval t; |
|
|
const char *ret = strrchr(s, '/'); |
|
|
if (ret) s = ret + 1; |
|
|
gettimeofday(&t, NULL); |
|
|
buf[sizeof(buf) - 1] = '\0'; |
|
|
snprintf(buf, sizeof(buf), "[%4d.%06d] %s", |
|
|
(int)(t.tv_sec % 1000), (int)(t.tv_usec), s); |
|
|
return buf; |
|
|
} |
|
|
|
|
|
#define ND(fmt, ...) do {} while (0) |
|
|
#define D1(fmt, ...) do {} while (0) |
|
|
#define D(fmt, ...) fprintf(stderr, "%s:%-10s [%d] " fmt "\n", \ |
|
|
xyz(__FILE__), __FUNCTION__, __LINE__, ## __VA_ARGS__) |
|
|
|
|
|
|
|
|
#define RD(lps, format, ...) \ |
|
|
do { \ |
|
|
static int t0, __cnt; \ |
|
|
struct timeval __xxts; \ |
|
|
gettimeofday(&__xxts, NULL); \ |
|
|
if (t0 != __xxts.tv_sec) { \ |
|
|
t0 = __xxts.tv_sec; \ |
|
|
__cnt = 0; \ |
|
|
} \ |
|
|
if (__cnt++ < lps) { \ |
|
|
D(format, ##__VA_ARGS__); \ |
|
|
} \ |
|
|
} while (0) |
|
|
|
|
|
#define DX(lev, fmt, ...) do { \ |
|
|
if (dn_cfg.debug > lev) D(fmt, ## __VA_ARGS__); } while (0) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef NEED_STRTONUM |
|
|
|
|
|
|
|
|
#define HN_DECIMAL 0x01 |
|
|
#define HN_NOSPACE 0x02 |
|
|
#define HN_B 0x04 |
|
|
#define HN_DIVISOR_1000 0x08 |
|
|
#define HN_IEC_PREFIXES 0x10 |
|
|
|
|
|
#define HN_GETSCALE 0x10 |
|
|
#define HN_AUTOSCALE 0x20 |
|
|
|
|
|
|
|
|
int humanize_number(char *_buf, size_t _len, int64_t _number, |
|
|
const char *_suffix, int _scale, int _flags); |
|
|
int expand_number(const char *buf, uint64_t *num); |
|
|
|
|
|
|
|
|
long long |
|
|
strtonum(const char *nptr, long long minval, long long maxval, |
|
|
const char **errstr); |
|
|
#ifndef __APPLE__ |
|
|
int ishexnumber(int c); |
|
|
#endif |
|
|
#endif |
|
|
|
|
|
#ifdef NEED_SYSCTLBYNAME |
|
|
int sysctlbyname(const char *name, void *oldp, size_t *oldlenp, |
|
|
void *newp, size_t newlen); |
|
|
#define setprogname(x) |
|
|
|
|
|
extern int optreset; |
|
|
|
|
|
long long int strtonum(const char *nptr, long long minval, |
|
|
long long maxval, const char **errstr); |
|
|
|
|
|
|
|
|
struct ether_addr; |
|
|
struct ether_addr * ether_aton(const char *a); |
|
|
|
|
|
#define ICMP6_MAXTYPE 201 |
|
|
#define __u6_addr in6_u |
|
|
#define in6_u __in6_u |
|
|
|
|
|
|
|
|
#define __u6_addr32 u6_addr32 |
|
|
|
|
|
#define NET_RT_IFLIST 3 |
|
|
|
|
|
#define RTM_VERSION 5 |
|
|
|
|
|
#endif |
|
|
|
|
|
#ifdef NEED_SIN_LEN |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define sin_len sin_zero[0] |
|
|
#define sin6_len sin6_flowinfo |
|
|
#endif |
|
|
|
|
|
#ifdef NEED_ROUNDUP2 |
|
|
|
|
|
#define roundup2(x, y) (((x)+((y)-1))&(~((y)-1))) |
|
|
#endif |
|
|
|
|
|
|
|
|
size_t strlcpy(char * dst, const char * src, size_t siz); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define ICMP6_DST_UNREACH_NOROUTE 0 |
|
|
#define ICMP6_DST_UNREACH_ADMIN 1 |
|
|
#define ICMP6_DST_UNREACH_ADDR 3 |
|
|
#define ICMP6_DST_UNREACH_NOPORT 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define CTLFLAG_RD 1 |
|
|
#define CTLFLAG_RDTUN 1 |
|
|
#define CTLFLAG_RW 2 |
|
|
#define CTLFLAG_SECURE3 0 |
|
|
#define CTLFLAG_VNET 0 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct thread { |
|
|
void *sopt_td; |
|
|
void *td_ucred; |
|
|
}; |
|
|
|
|
|
enum sopt_dir { SOPT_GET, SOPT_SET }; |
|
|
|
|
|
struct sockopt { |
|
|
enum sopt_dir sopt_dir; |
|
|
int sopt_level; |
|
|
int sopt_name; |
|
|
void *sopt_val; |
|
|
size_t sopt_valsize; |
|
|
struct thread *sopt_td; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define _IPFW_SOCKOPT_BASE 100 |
|
|
#define IP_FW_TABLE_ADD (_IPFW_SOCKOPT_BASE + 0) |
|
|
#define IP_FW_TABLE_DEL (_IPFW_SOCKOPT_BASE + 1) |
|
|
#define IP_FW_TABLE_FLUSH (_IPFW_SOCKOPT_BASE + 2) |
|
|
#define IP_FW_TABLE_GETSIZE (_IPFW_SOCKOPT_BASE + 3) |
|
|
#define IP_FW_TABLE_LIST (_IPFW_SOCKOPT_BASE + 4) |
|
|
#define IP_FW_DYN_GET (_IPFW_SOCKOPT_BASE + 5) |
|
|
|
|
|
#define IP_FW3 (_IPFW_SOCKOPT_BASE + 8) |
|
|
#define IP_DUMMYNET3 (_IPFW_SOCKOPT_BASE + 9) |
|
|
|
|
|
#define IP_FW_ADD (_IPFW_SOCKOPT_BASE + 10) |
|
|
#define IP_FW_DEL (_IPFW_SOCKOPT_BASE + 11) |
|
|
#define IP_FW_FLUSH (_IPFW_SOCKOPT_BASE + 12) |
|
|
#define IP_FW_ZERO (_IPFW_SOCKOPT_BASE + 13) |
|
|
#define IP_FW_GET (_IPFW_SOCKOPT_BASE + 14) |
|
|
#define IP_FW_RESETLOG (_IPFW_SOCKOPT_BASE + 15) |
|
|
|
|
|
#define IP_FW_NAT_CFG (_IPFW_SOCKOPT_BASE + 16) |
|
|
#define IP_FW_NAT_DEL (_IPFW_SOCKOPT_BASE + 17) |
|
|
#define IP_FW_NAT_GET_CONFIG (_IPFW_SOCKOPT_BASE + 18) |
|
|
#define IP_FW_NAT_GET_LOG (_IPFW_SOCKOPT_BASE + 19) |
|
|
|
|
|
#define IP_DUMMYNET_CONFIGURE (_IPFW_SOCKOPT_BASE + 20) |
|
|
#define IP_DUMMYNET_DEL (_IPFW_SOCKOPT_BASE + 21) |
|
|
#define IP_DUMMYNET_FLUSH (_IPFW_SOCKOPT_BASE + 22) |
|
|
|
|
|
#define IP_DUMMYNET_GET (_IPFW_SOCKOPT_BASE + 24) |
|
|
#define _IPFW_SOCKOPT_END (_IPFW_SOCKOPT_BASE + 25) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef USERSPACE |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define LOCALADDR "127.0.0.1" |
|
|
#define IPFW_PORT 5555 |
|
|
|
|
|
#ifndef KERNEL_SIDE |
|
|
#ifdef _KERNEL |
|
|
#error _KERNEL defined in user space |
|
|
#endif |
|
|
int do_connect(const char *addr, int port); |
|
|
#include <sys/socket.h> |
|
|
|
|
|
#define socket(a, b, c) do_connect(LOCALADDR, IPFW_PORT) |
|
|
#define setsockopt setsockopt2 |
|
|
#define getsockopt getsockopt2 |
|
|
int getsockopt2(int s, int lev, int optname, void *optval, socklen_t *optlen); |
|
|
int setsockopt2(int s, int lev, int optname, void *optval, socklen_t optlen); |
|
|
#endif |
|
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef EMULATE_SYSCTL |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
enum { |
|
|
SYSCTLTYPE_INT = 0, |
|
|
SYSCTLTYPE_UINT = 1, |
|
|
SYSCTLTYPE_SHORT = 2, |
|
|
SYSCTLTYPE_USHORT = 3, |
|
|
SYSCTLTYPE_LONG = 4, |
|
|
SYSCTLTYPE_ULONG = 5, |
|
|
SYSCTLTYPE_STRING = 6, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SYSCTLTYPE_PROC = 0x100, |
|
|
CTLTYPE_INT = (0x100 | (0<<2)), |
|
|
CTLTYPE_UINT = (0x100 | (1<<2)), |
|
|
CTLTYPE_LONG = (0x100 | (4<<2)), |
|
|
CTLTYPE_ULONG = (0x100 | (5<<2)) |
|
|
}; |
|
|
|
|
|
struct sysctlhead { |
|
|
uint32_t blocklen; |
|
|
uint32_t namelen; |
|
|
uint32_t flags; |
|
|
uint32_t datalen; |
|
|
}; |
|
|
|
|
|
|
|
|
#endif |
|
|
|
|
|
#ifndef __FreeBSD__ |
|
|
int sysctlbyname(const char *name, void *oldp, size_t *oldlenp, void *newp, |
|
|
size_t newlen); |
|
|
|
|
|
#define test_bit(ix, pData) ((*pData) & (1<<(ix))) |
|
|
#define __set_bit(ix, pData) (*pData) |= (1<<(ix)) |
|
|
#define __clear_bit(ix, pData) (*pData) &= ~(1<<(ix)) |
|
|
|
|
|
static inline int fls(int _n) |
|
|
{ |
|
|
unsigned int n = _n; |
|
|
int i = 0; |
|
|
for (i = 0; n > 0; n >>= 1, i++) |
|
|
; |
|
|
return i; |
|
|
} |
|
|
|
|
|
static inline unsigned long __fls(unsigned long word) |
|
|
{ |
|
|
return fls(word) - 1; |
|
|
} |
|
|
|
|
|
|
|
|
#endif |
|
|
|
|
|
#ifdef KERNEL_SIDE |
|
|
|
|
|
typedef uint64_t *counter_u64_t; |
|
|
static inline void counter_u64_add(counter_u64_t c, int64_t v) |
|
|
{ |
|
|
*c += v; |
|
|
} |
|
|
static inline void counter_u64_zero(counter_u64_t c) |
|
|
{ |
|
|
*c = 0; |
|
|
} |
|
|
static inline uint64_t counter_u64_fetch(counter_u64_t c) |
|
|
{ |
|
|
return *c; |
|
|
} |
|
|
|
|
|
struct rm_priotracker { |
|
|
}; |
|
|
|
|
|
#define vslock(_a, _b) (0) |
|
|
#define vsunlock(_a, _b) |
|
|
|
|
|
typedef uint64_t u_register_t; |
|
|
|
|
|
typedef uintptr_t eventhandler_tag; |
|
|
#define EVENTHANDLER_REGISTER(_a, _b, ...) (uintptr_t)_b; |
|
|
#define EVENTHANDLER_DEREGISTER(_a, _b, ...) (void)_b; |
|
|
|
|
|
|
|
|
#define if_name(_ifp) (_ifp->if_xname) |
|
|
#define ifunit_ref(_n) NULL |
|
|
#define if_rele(_n) |
|
|
|
|
|
#define rtalloc1_fib(_a, ...) NULL |
|
|
#define rt_key(_a) NULL |
|
|
#define rt_mask(_a) NULL |
|
|
#define RTFREE_LOCKED(_a) ((void)NULL) |
|
|
struct rtentry { |
|
|
}; |
|
|
#define rt_tables_get_rnh(_a, _b) NULL |
|
|
|
|
|
#endif |
|
|
|
|
|
#ifdef _KERNEL |
|
|
|
|
|
|
|
|
#ifdef linux |
|
|
#define div64(a,b) (((int64_t)a)/((int64_t)b)) |
|
|
#define LINUX_VERSION_CODE 30003 |
|
|
#define KERNEL_VERSION(a,b,c) (a*10000+b*100 + c) |
|
|
#define __printflike(a,b) |
|
|
#endif |
|
|
|
|
|
#endif |
|
|
|
|
|
#ifndef __FreeBSD__ |
|
|
#ifndef IFNAMSIZ |
|
|
#define IFNAMSIZ 16 |
|
|
#endif |
|
|
#include "missing.h" |
|
|
|
|
|
struct if_data { |
|
|
|
|
|
u_long ifi_mtu; |
|
|
}; |
|
|
|
|
|
#endif |
|
|
|
|
|
#ifdef __APPLE__ |
|
|
#include <sys/socketvar.h> |
|
|
|
|
|
|
|
|
struct if_data64 { |
|
|
|
|
|
u_long ifi_mtu; |
|
|
}; |
|
|
|
|
|
struct net_event_data { |
|
|
}; |
|
|
|
|
|
struct in_addr; |
|
|
#endif |
|
|
|
|
|
#define __PAST_END(v, idx) v[idx] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <strings.h> |
|
|
|
|
|
static inline void |
|
|
_pkt_copy(const void *_src, void *_dst, int l) |
|
|
{ |
|
|
const uint64_t *src = _src; |
|
|
uint64_t *dst = _dst; |
|
|
#define likely(x) __builtin_expect(!!(x), 1) |
|
|
#define unlikely(x) __builtin_expect(!!(x), 0) |
|
|
if (unlikely(l >= 1024)) { |
|
|
bcopy(src, dst, l); |
|
|
return; |
|
|
} |
|
|
for (; l > 0; l-=64) { |
|
|
*dst++ = *src++; |
|
|
*dst++ = *src++; |
|
|
*dst++ = *src++; |
|
|
*dst++ = *src++; |
|
|
*dst++ = *src++; |
|
|
*dst++ = *src++; |
|
|
*dst++ = *src++; |
|
|
*dst++ = *src++; |
|
|
} |
|
|
} |
|
|
|
|
|
#endif |
|
|
|