Changeset View
Changeset View
Standalone View
Standalone View
sys/netinet/libalias/alias_db.c
Show First 20 Lines • Show All 318 Lines • ▼ Show 20 Lines | #define LINK_UNFIREWALLED 0x08 | ||||
int timestamp; /* Time link was last accessed */ | int timestamp; /* Time link was last accessed */ | ||||
#ifndef NO_USE_SOCKETS | #ifndef NO_USE_SOCKETS | ||||
int sockfd; /* socket descriptor */ | int sockfd; /* socket descriptor */ | ||||
#endif | #endif | ||||
/* Linked list of pointers for input and output lookup tables */ | /* Linked list of pointers for input and output lookup tables */ | ||||
union { | union { | ||||
struct { | struct { | ||||
SPLAY_ENTRY(alias_link) out; | |||||
LIST_ENTRY(alias_link) in; | LIST_ENTRY (alias_link) in; | ||||
LIST_ENTRY(alias_link) out; | |||||
} all; | } all; | ||||
struct { | struct { | ||||
LIST_ENTRY(alias_link) list; | LIST_ENTRY (alias_link) list; | ||||
} pptp; | } pptp; | ||||
}; | }; | ||||
struct { | struct { | ||||
TAILQ_ENTRY(alias_link) list; | TAILQ_ENTRY(alias_link) list; | ||||
int time; /* Expire time for link */ | int time; /* Expire time for link */ | ||||
} expire; | } expire; | ||||
/* Auxiliary data */ | /* Auxiliary data */ | ||||
union { | union { | ||||
▲ Show 20 Lines • Show All 44 Lines • ▼ Show 20 Lines | |||||
Miscellaneous: | Miscellaneous: | ||||
SeqDiff() -- difference between two TCP sequences | SeqDiff() -- difference between two TCP sequences | ||||
ShowAliasStats() -- send alias statistics to a monitor file | ShowAliasStats() -- send alias statistics to a monitor file | ||||
*/ | */ | ||||
/* Local prototypes */ | /* Local prototypes */ | ||||
static struct group_in * | static struct group_in * | ||||
StartPointIn(struct libalias *, struct in_addr, u_short, int, int); | StartPointIn(struct libalias *, struct in_addr, u_short, int, int); | ||||
static u_int | |||||
StartPointOut(struct in_addr, struct in_addr, u_short, u_short, int); | |||||
static int SeqDiff(u_long, u_long); | static int SeqDiff(u_long, u_long); | ||||
#ifndef NO_FW_PUNCH | #ifndef NO_FW_PUNCH | ||||
/* Firewall control */ | /* Firewall control */ | ||||
static void InitPunchFW(struct libalias *); | static void InitPunchFW(struct libalias *); | ||||
static void UninitPunchFW(struct libalias *); | static void UninitPunchFW(struct libalias *); | ||||
static void ClearFWHole(struct alias_link *); | static void ClearFWHole(struct alias_link *); | ||||
#endif | #endif | ||||
/* Log file control */ | /* Log file control */ | ||||
static void ShowAliasStats(struct libalias *); | static void ShowAliasStats(struct libalias *); | ||||
static int InitPacketAliasLog(struct libalias *); | static int InitPacketAliasLog(struct libalias *); | ||||
static void UninitPacketAliasLog(struct libalias *); | static void UninitPacketAliasLog(struct libalias *); | ||||
void SctpShowAliasStats(struct libalias *la); | void SctpShowAliasStats(struct libalias *la); | ||||
/* Splay handling */ | |||||
static inline int | |||||
cmp_out(struct alias_link *a, struct alias_link *b) { | |||||
int i = a->src_port - b->src_port; | |||||
if (i != 0) return (i); | |||||
i = a->src_addr.s_addr - b->src_addr.s_addr; | |||||
if (i != 0) return (i); | |||||
i = a->dst_addr.s_addr - b->dst_addr.s_addr; | |||||
if (i != 0) return (i); | |||||
i = a->dst_port - b->dst_port; | |||||
if (i != 0) return (i); | |||||
i = a->link_type - b->link_type; | |||||
return (i); | |||||
} | |||||
SPLAY_PROTOTYPE(splay_out, alias_link, all.out, cmp_out); | |||||
SPLAY_GENERATE(splay_out, alias_link, all.out, cmp_out); | |||||
#define INGUARD \ | #define INGUARD \ | ||||
if (grp->alias_port != alias_port || \ | if (grp->alias_port != alias_port || \ | ||||
grp->link_type != link_type || \ | grp->link_type != link_type || \ | ||||
grp->alias_addr.s_addr != alias_addr.s_addr) \ | grp->alias_addr.s_addr != alias_addr.s_addr) \ | ||||
continue; | continue; | ||||
static struct group_in * | static struct group_in * | ||||
StartPointIn(struct libalias *la, | StartPointIn(struct libalias *la, | ||||
Show All 25 Lines | StartPointIn(struct libalias *la, | ||||
grp->link_type = link_type; | grp->link_type = link_type; | ||||
LIST_INIT(&grp->full); | LIST_INIT(&grp->full); | ||||
LIST_INIT(&grp->partial); | LIST_INIT(&grp->partial); | ||||
LIST_INSERT_HEAD(&la->groupTableIn[n], grp, group_in); | LIST_INSERT_HEAD(&la->groupTableIn[n], grp, group_in); | ||||
return (grp); | return (grp); | ||||
} | } | ||||
#undef INGUARD | #undef INGUARD | ||||
static u_int | |||||
StartPointOut(struct in_addr src_addr, struct in_addr dst_addr, | |||||
u_short src_port, u_short dst_port, int link_type) | |||||
{ | |||||
u_int n; | |||||
n = src_addr.s_addr; | |||||
n += dst_addr.s_addr; | |||||
n += src_port; | |||||
n += dst_port; | |||||
n += link_type; | |||||
return (n % LINK_TABLE_OUT_SIZE); | |||||
} | |||||
static int | static int | ||||
SeqDiff(u_long x, u_long y) | SeqDiff(u_long x, u_long y) | ||||
{ | { | ||||
/* Return the difference between two TCP sequence numbers | /* Return the difference between two TCP sequence numbers | ||||
* This function is encapsulated in case there are any unusual | * This function is encapsulated in case there are any unusual | ||||
* arithmetic conditions that need to be considered. | * arithmetic conditions that need to be considered. | ||||
*/ | */ | ||||
return (ntohl(y) - ntohl(x)); | return (ntohl(y) - ntohl(x)); | ||||
▲ Show 20 Lines • Show All 413 Lines • ▼ Show 20 Lines | if (lnk->server != NULL) { | ||||
head = curr = lnk->server; | head = curr = lnk->server; | ||||
do { | do { | ||||
next = curr->next; | next = curr->next; | ||||
free(curr); | free(curr); | ||||
} while ((curr = next) != head); | } while ((curr = next) != head); | ||||
} else { | } else { | ||||
/* Adjust output table pointers */ | /* Adjust output table pointers */ | ||||
LIST_REMOVE(lnk, all.out); | SPLAY_REMOVE(splay_out, &la->linkSplayOut, lnk); | ||||
} | } | ||||
/* Adjust input table pointers */ | /* Adjust input table pointers */ | ||||
LIST_REMOVE(lnk, all.in); | LIST_REMOVE(lnk, all.in); | ||||
break; | break; | ||||
} | } | ||||
/* remove from housekeeping */ | /* remove from housekeeping */ | ||||
▲ Show 20 Lines • Show All 46 Lines • ▼ Show 20 Lines | #endif | ||||
} | } | ||||
} | } | ||||
struct alias_link * | struct alias_link * | ||||
AddLink(struct libalias *la, struct in_addr src_addr, struct in_addr dst_addr, | AddLink(struct libalias *la, struct in_addr src_addr, struct in_addr dst_addr, | ||||
struct in_addr alias_addr, u_short src_port, u_short dst_port, | struct in_addr alias_addr, u_short src_port, u_short dst_port, | ||||
int alias_port_param, int link_type) | int alias_port_param, int link_type) | ||||
{ | { | ||||
u_int start_point; | |||||
struct alias_link *lnk; | struct alias_link *lnk; | ||||
LIBALIAS_LOCK_ASSERT(la); | LIBALIAS_LOCK_ASSERT(la); | ||||
lnk = malloc(sizeof(struct alias_link)); | lnk = malloc(sizeof(struct alias_link)); | ||||
if (lnk == NULL) { | if (lnk == NULL) { | ||||
#ifdef LIBALIAS_DEBUG | #ifdef LIBALIAS_DEBUG | ||||
fprintf(stderr, "PacketAlias/AddLink(): "); | fprintf(stderr, "PacketAlias/AddLink(): "); | ||||
▲ Show 20 Lines • Show All 112 Lines • ▼ Show 20 Lines | default: { | ||||
grp = StartPointIn(la, alias_addr, lnk->alias_port, link_type, 1); | grp = StartPointIn(la, alias_addr, lnk->alias_port, link_type, 1); | ||||
if (grp == NULL) { | if (grp == NULL) { | ||||
free(lnk); | free(lnk); | ||||
return (NULL); | return (NULL); | ||||
} | } | ||||
/* Set up pointers for output lookup table */ | /* Set up pointers for output lookup table */ | ||||
start_point = StartPointOut(src_addr, dst_addr, | SPLAY_INSERT(splay_out, &la->linkSplayOut, lnk); | ||||
src_port, dst_port, link_type); | |||||
LIST_INSERT_HEAD(&la->linkTableOut[start_point], lnk, all.out); | |||||
/* Set up pointers for input lookup table */ | /* Set up pointers for input lookup table */ | ||||
if (lnk->flags & LINK_PARTIALLY_SPECIFIED) | if (lnk->flags & LINK_PARTIALLY_SPECIFIED) | ||||
LIST_INSERT_HEAD(&grp->partial, lnk, all.in); | LIST_INSERT_HEAD(&grp->partial, lnk, all.in); | ||||
else | else | ||||
LIST_INSERT_HEAD(&grp->full, lnk, all.in); | LIST_INSERT_HEAD(&grp->full, lnk, all.in); | ||||
} | } | ||||
break; | break; | ||||
Show All 36 Lines | if (new_lnk != NULL && | ||||
old_lnk->data.tcp->fwhole > 0) { | old_lnk->data.tcp->fwhole > 0) { | ||||
PunchFWHole(new_lnk); | PunchFWHole(new_lnk); | ||||
} | } | ||||
#endif | #endif | ||||
DeleteLink(&old_lnk, deletePermanent); | DeleteLink(&old_lnk, deletePermanent); | ||||
return (new_lnk); | return (new_lnk); | ||||
} | } | ||||
#define OUTGUARD \ | |||||
if (lnk->src_port != src_port || \ | |||||
lnk->src_addr.s_addr != src_addr.s_addr || \ | |||||
lnk->dst_addr.s_addr != dst_addr.s_addr || \ | |||||
lnk->dst_port != dst_port || \ | |||||
lnk->link_type != link_type) \ | |||||
continue; | |||||
static struct alias_link * | static struct alias_link * | ||||
_SearchLinkOut(struct libalias *la, struct in_addr src_addr, | _SearchLinkOut(struct libalias *la, struct in_addr src_addr, | ||||
struct in_addr dst_addr, | struct in_addr dst_addr, | ||||
u_short src_port, | u_short src_port, | ||||
u_short dst_port, | u_short dst_port, | ||||
int link_type) { | int link_type) { | ||||
u_int i; | |||||
struct alias_link *lnk; | struct alias_link *lnk; | ||||
struct alias_link needle = { | |||||
.src_addr = src_addr, | |||||
.dst_addr = dst_addr, | |||||
.src_port = src_port, | |||||
.dst_port = dst_port, | |||||
.link_type = link_type | |||||
}; | |||||
i = StartPointOut(src_addr, dst_addr, src_port, dst_port, link_type); | lnk = SPLAY_FIND(splay_out, &la->linkSplayOut, &needle); | ||||
LIST_FOREACH(lnk, &la->linkTableOut[i], all.out) { | |||||
OUTGUARD; | |||||
return (UseLink(la, lnk)); | return (UseLink(la, lnk)); | ||||
} | } | ||||
return (NULL); | |||||
} | |||||
#undef OUTGUARD | |||||
static struct alias_link * | static struct alias_link * | ||||
_FindLinkOut(struct libalias *la, struct in_addr src_addr, | _FindLinkOut(struct libalias *la, struct in_addr src_addr, | ||||
struct in_addr dst_addr, | struct in_addr dst_addr, | ||||
u_short src_port, | u_short src_port, | ||||
u_short dst_port, | u_short dst_port, | ||||
int link_type, | int link_type, | ||||
int replace_partial_links) | int replace_partial_links) | ||||
{ | { | ||||
▲ Show 20 Lines • Show All 1,151 Lines • ▼ Show 20 Lines | if (server != NULL) { | ||||
server->addr = addr; | server->addr = addr; | ||||
server->port = port; | server->port = port; | ||||
head = lnk->server; | head = lnk->server; | ||||
if (head == NULL) { | if (head == NULL) { | ||||
server->next = server; | server->next = server; | ||||
/* not usable for outgoing connections */ | /* not usable for outgoing connections */ | ||||
LIST_REMOVE(lnk, all.out); | SPLAY_REMOVE(splay_out, &la->linkSplayOut, lnk); | ||||
} else { | } else { | ||||
struct server *s; | struct server *s; | ||||
for (s = head; s->next != head; s = s->next) | for (s = head; s->next != head; s = s->next) | ||||
; | ; | ||||
s->next = server; | s->next = server; | ||||
server->next = head; | server->next = head; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 152 Lines • ▼ Show 20 Lines | #endif | ||||
LIST_INSERT_HEAD(&instancehead, la, instancelist); | LIST_INSERT_HEAD(&instancehead, la, instancelist); | ||||
#ifdef _KERNEL | #ifdef _KERNEL | ||||
LibAliasTime = time_uptime; | LibAliasTime = time_uptime; | ||||
#else | #else | ||||
LibAliasTime = time(NULL); | LibAliasTime = time(NULL); | ||||
#endif | #endif | ||||
for (i = 0; i < LINK_TABLE_OUT_SIZE; i++) | SPLAY_INIT(&la->linkSplayOut); | ||||
LIST_INIT(&la->linkTableOut[i]); | |||||
for (i = 0; i < LINK_TABLE_IN_SIZE; i++) | for (i = 0; i < LINK_TABLE_IN_SIZE; i++) | ||||
LIST_INIT(&la->groupTableIn[i]); | LIST_INIT(&la->groupTableIn[i]); | ||||
LIST_INIT(&la->pptpList); | LIST_INIT(&la->pptpList); | ||||
TAILQ_INIT(&la->checkExpire); | TAILQ_INIT(&la->checkExpire); | ||||
#ifdef _KERNEL | #ifdef _KERNEL | ||||
AliasSctpInit(la); | AliasSctpInit(la); | ||||
#endif | #endif | ||||
LIBALIAS_LOCK_INIT(la); | LIBALIAS_LOCK_INIT(la); | ||||
▲ Show 20 Lines • Show All 387 Lines • Show Last 20 Lines |