firewall + random init pkt size
This commit is contained in:
1 parent
42eef31a64
commit
dbcd167a85
12 files changed
+286
-4
No files matched your search
+89
-1
@@ -21,6 +21,7 @@
|
||||
|
||||
/* Forward declaration for new functions */
|
||||
static int assign_string(char *dest, size_t dest_size, const char *src);
|
||||
static int parse_firewall_rule(const char *rule_str, struct global_config *global);
|
||||
|
||||
typedef enum {
|
||||
SECTION_UNKNOWN,
|
||||
@@ -28,7 +29,8 @@ typedef enum {
|
||||
SECTION_SERVER,
|
||||
SECTION_CLIENT,
|
||||
SECTION_ROUTING,
|
||||
SECTION_DEBUG
|
||||
SECTION_DEBUG,
|
||||
SECTION_FIREWALL
|
||||
} section_type_t;
|
||||
|
||||
static char* trim(char *str) {
|
||||
@@ -203,6 +205,59 @@ static int add_route_entry(struct CFG_ROUTE_ENTRY **list, const char *subnet_str
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int parse_firewall_rule(const char *rule_str, struct global_config *global) {
|
||||
if (!rule_str || !global) return -1;
|
||||
|
||||
char rule_copy[64];
|
||||
strncpy(rule_copy, rule_str, sizeof(rule_copy) - 1);
|
||||
rule_copy[sizeof(rule_copy) - 1] = '\0';
|
||||
trim(rule_copy);
|
||||
|
||||
if (strcasecmp(rule_copy, "all") == 0) {
|
||||
global->firewall_bypass_all = 1;
|
||||
DEBUG_INFO(DEBUG_CATEGORY_CONFIG, "Firewall: bypass all enabled");
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Parse IP:port or IP
|
||||
char *port_str = strchr(rule_copy, ':');
|
||||
uint16_t port = 0;
|
||||
if (port_str) {
|
||||
*port_str = '\0';
|
||||
port = (uint16_t)atoi(port_str + 1);
|
||||
}
|
||||
|
||||
// Parse IP to uint32_t (host order)
|
||||
struct in_addr addr;
|
||||
if (inet_pton(AF_INET, rule_copy, &addr) != 1) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "parse_firewall_rule: invalid IP: %s", rule_str);
|
||||
return -1;
|
||||
}
|
||||
|
||||
uint32_t ip = ntohl(addr.s_addr); // convert to host order for sorting
|
||||
|
||||
// Reallocate array if needed
|
||||
if ((global->firewall_rule_count % INITIAL_ARRAY_CAPACITY) == 0) {
|
||||
int new_cap = global->firewall_rule_count + INITIAL_ARRAY_CAPACITY;
|
||||
struct CFG_FIREWALL_RULE *new_rules = u_realloc(global->firewall_rules,
|
||||
new_cap * sizeof(struct CFG_FIREWALL_RULE));
|
||||
if (!new_rules) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_MEMORY, "Failed to realloc firewall rules");
|
||||
return -1;
|
||||
}
|
||||
global->firewall_rules = new_rules;
|
||||
}
|
||||
|
||||
struct CFG_FIREWALL_RULE *rule = &global->firewall_rules[global->firewall_rule_count];
|
||||
rule->ip = ip;
|
||||
rule->port = port;
|
||||
rule->bypass = 0;
|
||||
global->firewall_rule_count++;
|
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_CONFIG, "Added firewall rule: %s:%d", rule_str, port);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static struct CFG_SERVER* find_server_by_name(struct CFG_SERVER *servers, const char *name) {
|
||||
struct CFG_SERVER *srv = servers;
|
||||
while (srv) {
|
||||
@@ -392,6 +447,7 @@ static section_type_t parse_section_header(const char *line, char *name, size_t
|
||||
if (strcasecmp(section, "global") == 0) return SECTION_GLOBAL;
|
||||
if (strcasecmp(section, "routing") == 0) return SECTION_ROUTING;
|
||||
if (strcasecmp(section, "debug") == 0) return SECTION_DEBUG;
|
||||
if (strcasecmp(section, "firewall") == 0) return SECTION_FIREWALL;
|
||||
|
||||
char *colon = strchr(section, ':');
|
||||
if (!colon) return SECTION_UNKNOWN;
|
||||
@@ -418,6 +474,9 @@ static struct utun_config* parse_config_internal(FILE *fp, const char *filename)
|
||||
|
||||
// Set default values
|
||||
cfg->global.keepalive_timeout = 2000; // Default 2 seconds
|
||||
cfg->global.firewall_rules = NULL;
|
||||
cfg->global.firewall_rule_count = 0;
|
||||
cfg->global.firewall_bypass_all = 0;
|
||||
|
||||
section_type_t cur_section = SECTION_UNKNOWN;
|
||||
struct CFG_SERVER *cur_server = NULL;
|
||||
@@ -508,6 +567,13 @@ static struct utun_config* parse_config_internal(FILE *fp, const char *filename)
|
||||
cfg->global.debug_levels.count++;
|
||||
}
|
||||
break;
|
||||
case SECTION_FIREWALL:
|
||||
if (strcmp(key, "allow") == 0) {
|
||||
if (parse_firewall_rule(value, &cfg->global) < 0) {
|
||||
DEBUG_WARN(DEBUG_CATEGORY_CONFIG, "%s:%d: Invalid firewall rule: %s", filename, line_num, value);
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
DEBUG_WARN(DEBUG_CATEGORY_CONFIG, "%s:%d: Key outside section: %s", filename, line_num, key);
|
||||
break;
|
||||
@@ -580,6 +646,9 @@ void free_config(struct utun_config *config) {
|
||||
free_route_entries(config->route_subnets);
|
||||
free_route_entries(config->my_subnets);
|
||||
|
||||
// Free firewall rules
|
||||
u_free(config->global.firewall_rules);
|
||||
|
||||
u_free(config);
|
||||
}
|
||||
|
||||
@@ -644,6 +713,25 @@ void print_config(const struct utun_config *cfg) {
|
||||
DEBUG_INFO(DEBUG_CATEGORY_CONFIG, " %s/%d", ip_to_string(&my->ip, ip_buffer, sizeof(ip_buffer)), my->netmask);
|
||||
my = my->next;
|
||||
}
|
||||
|
||||
DEBUG_INFO(DEBUG_CATEGORY_CONFIG, "Firewall:");
|
||||
if (cfg->global.firewall_bypass_all) {
|
||||
DEBUG_INFO(DEBUG_CATEGORY_CONFIG, " bypass_all=1");
|
||||
} else if (cfg->global.firewall_rule_count > 0) {
|
||||
for (int i = 0; i < cfg->global.firewall_rule_count; i++) {
|
||||
struct CFG_FIREWALL_RULE *r = &cfg->global.firewall_rules[i];
|
||||
char ip_str[16];
|
||||
struct in_addr ia = { .s_addr = htonl(r->ip) };
|
||||
inet_ntop(AF_INET, &ia, ip_str, sizeof(ip_str));
|
||||
if (r->port == 0) {
|
||||
DEBUG_INFO(DEBUG_CATEGORY_CONFIG, " allow=%s", ip_str);
|
||||
} else {
|
||||
DEBUG_INFO(DEBUG_CATEGORY_CONFIG, " allow=%s:%d", ip_str, r->port);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
DEBUG_INFO(DEBUG_CATEGORY_CONFIG, " no rules");
|
||||
}
|
||||
}
|
||||
|
||||
int update_config_keys(const char *filename, const char *priv_key, const char *pub_key) {
|
||||
|
||||
Reference in new issue
Block a user