1
0
mirror of https://code.semirocket.science/wrapsix synced 2024-11-22 09:44:05 +02:00

Fixed generation of outgoing port in the NAT

Saving MAC address to the NAT too
This commit is contained in:
Michal Zima 2012-04-01 19:08:28 +02:00
parent 19d5a22c18
commit 4b6a373019
2 changed files with 21 additions and 13 deletions

View File

@ -20,6 +20,7 @@
#include <stdlib.h> /* malloc */ #include <stdlib.h> /* malloc */
#include <time.h> /* time */ #include <time.h> /* time */
#include "ethernet.h"
#include "ipv4.h" #include "ipv4.h"
#include "ipv6.h" #include "ipv6.h"
#include "nat.h" #include "nat.h"
@ -68,6 +69,7 @@ void nat_quit(void)
} }
struct s_nat *nat_out(radixtree_t *nat_proto6, radixtree_t *nat_proto4, struct s_nat *nat_out(radixtree_t *nat_proto6, radixtree_t *nat_proto4,
struct s_mac_addr eth_src,
struct s_ipv6_addr ipv6_src, struct s_ipv6_addr ipv6_dst, struct s_ipv6_addr ipv6_src, struct s_ipv6_addr ipv6_dst,
unsigned short port_src, unsigned short port_dst) unsigned short port_src, unsigned short port_dst)
{ {
@ -82,33 +84,37 @@ struct s_nat *nat_out(radixtree_t *nat_proto6, radixtree_t *nat_proto4,
radixsearch6.port_src = port_src; radixsearch6.port_src = port_src;
radixsearch6.port_dst = port_dst; radixsearch6.port_dst = port_dst;
if ((result = (struct s_nat *) radixtree_lookup(nat_proto6, radixtree_ipv6_chunker, &radixsearch6)) == NULL) { if ((result = (struct s_nat *) radixtree_lookup(nat_proto6,
radixtree_ipv6_chunker, &radixsearch6)) == NULL) {
/* if no connection is found, let's create one */ /* if no connection is found, let's create one */
if ((connection = (struct s_nat *) malloc(sizeof(struct s_nat))) == NULL) { if ((connection =
(struct s_nat *) malloc(sizeof(struct s_nat))) == NULL) {
fprintf(stderr, "[Error] Lack of free memory\n"); fprintf(stderr, "[Error] Lack of free memory\n");
return NULL; return NULL;
} }
connection->mac = eth_src;
connection->ipv6 = ipv6_src; connection->ipv6 = ipv6_src;
connection->ipv4 = radixsearch6.ipv4; connection->ipv4 = radixsearch6.ipv4;
connection->ipv6_port_src = port_src; connection->ipv6_port_src = port_src;
connection->ipv4_port_dst = port_dst; connection->ipv4_port_dst = port_dst;
connection->last_packet = time(NULL); connection->last_packet = time(NULL);
/* generate some outgoing port */
do {
/* return port from range 1024 - 65535 */
connection->ipv4_port_src = (rand() % 64511) + 1024;
result = radixtree_lookup(nat_proto6, radixtree_ipv6_chunker, &radixsearch6);
} while (result != NULL);
/* save this connection to the NAT table (to *both* of them) */
radixsearch4.addr = radixsearch6.ipv4; radixsearch4.addr = radixsearch6.ipv4;
radixsearch4.port_src = port_dst; radixsearch4.port_src = port_dst;
radixsearch4.port_dst = connection->ipv4_port_src;
radixsearch4.zeros = 0x0; radixsearch4.zeros = 0x0;
/* generate some outgoing port */
do {
/* returns port from range 1024 - 65535 */
radixsearch4.port_dst = (rand() % 64511) + 1024;
result = radixtree_lookup(nat_proto4, radixtree_ipv4_chunker, &radixsearch4);
} while (result != NULL);
connection->ipv4_port_src = radixsearch4.port_dst;
/* save this connection to the NAT table (to *both* of them) */
radixtree_insert(nat_proto6, radixtree_ipv6_chunker, &radixsearch6, connection); radixtree_insert(nat_proto6, radixtree_ipv6_chunker, &radixsearch6, connection);
radixtree_insert(nat_proto4, radixtree_ipv4_chunker, &radixsearch4, connection); radixtree_insert(nat_proto4, radixtree_ipv4_chunker, &radixsearch4, connection);

View File

@ -33,7 +33,8 @@ struct s_nat {
unsigned short ipv6_port_src; unsigned short ipv6_port_src;
unsigned short ipv4_port_src; unsigned short ipv4_port_src;
unsigned short ipv4_port_dst; unsigned short ipv4_port_dst;
time_t last_packet; /* time of processing last packet of the connection */ time_t last_packet; /* time of processing last
packet of the connection */
}; };
extern radixtree_t *nat6_tcp, *nat6_udp, *nat6_icmp, extern radixtree_t *nat6_tcp, *nat6_udp, *nat6_icmp,
@ -43,6 +44,7 @@ void nat_init(void);
void nat_quit(void); void nat_quit(void);
struct s_nat *nat_out(radixtree_t *nat_proto6, radixtree_t *nat_proto4, struct s_nat *nat_out(radixtree_t *nat_proto6, radixtree_t *nat_proto4,
struct s_mac_addr eth_src,
struct s_ipv6_addr ipv6_src, struct s_ipv6_addr ipv6_dst, struct s_ipv6_addr ipv6_src, struct s_ipv6_addr ipv6_dst,
unsigned short port_src, unsigned short port_dst); unsigned short port_src, unsigned short port_dst);
struct s_nat *nat_in(radixtree_t *nat_proto4, struct s_ipv4_addr ipv4_src, struct s_nat *nat_in(radixtree_t *nat_proto4, struct s_ipv4_addr ipv4_src,