Skip to content

Commit

Permalink
changed direct exit(code) to close_maps(0) where appropriate either c…
Browse files Browse the repository at this point in the history
…urrently or for future use
  • Loading branch information
r-caamano committed Apr 22, 2024
1 parent ee3f660 commit 823a6e4
Showing 1 changed file with 39 additions and 39 deletions.
78 changes: 39 additions & 39 deletions src/zfw.c
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ void set_tc(char *action)
if (access("/usr/sbin/tc", F_OK) != 0)
{
printf("tc not installed\n");
exit(0);
close_maps(0);
}
pid_t pid;
char *const parmList[] = {"/usr/sbin/tc", "qdisc", action, "dev", tc_interface, "clsact", NULL};
Expand Down Expand Up @@ -379,12 +379,12 @@ void set_tc_filter(char *action)
if (access("/usr/sbin/tc", F_OK) != 0)
{
printf("tc not installed\n");
exit(0);
close_maps(0);
}
if (!strcmp("add", action) && access(object_file, F_OK) != 0)
{
printf("object file %s not in path\n", object_file);
exit(1);
close_maps(1);
}
pid_t pid;
if (!strcmp(action, "add"))
Expand Down Expand Up @@ -432,7 +432,7 @@ void set_tc_filter(char *action)
if (status)
{
printf("tc %s filter action/%d not set : %s\n", direction_string, x, tc_interface);
exit(1);
close_maps(1);
}
}
}
Expand Down Expand Up @@ -503,7 +503,7 @@ unsigned short port2s(char *port)
if ((tmpint < 0) || (tmpint > 65535) || (!(*(endPtr) == '\0')))
{
printf("Invalid Port: %s\n", port);
exit(1);
close_maps(1);
}
unsigned short usint = (unsigned short)tmpint;
return usint;
Expand All @@ -517,7 +517,7 @@ __u8 proto2u8(char *protocol)
if ((tmpint <= 0) || (tmpint > 255) || (!(*(endPtr) == '\0')))
{
printf("Invalid Protocol: %s\n", protocol);
exit(1);
close_maps(1);
}
__u8 usint = (__u8)tmpint;
return usint;
Expand All @@ -543,7 +543,7 @@ __u16 len2u16(char *len)
if ((tmpint < 0) || (tmpint > 32) || (!(*(endPtr) == '\0')))
{
printf("Invalid Prefix Length: %s\n", len);
exit(1);
close_maps(1);
}
__u16 u16int = (__u16)tmpint;
return u16int;
Expand Down Expand Up @@ -816,7 +816,7 @@ void usage(char *message)
fprintf(stderr, " zfw --vrrp-enable <ifname>\n");
fprintf(stderr, " zfw -V\n");
fprintf(stderr, " zfw --help\n");
exit(1);
close_maps(1);
}

bool set_tun_diag()
Expand Down Expand Up @@ -1207,7 +1207,7 @@ void interface_tc()
if (getifaddrs(&addrs) == -1)
{
printf("can't get addrs");
exit(1);
close_maps(1);
}
struct ifaddrs *address = addrs;
uint32_t idx = 0;
Expand Down Expand Up @@ -1310,7 +1310,7 @@ void interface_diag()
if (getifaddrs(&addrs) == -1)
{
printf("can't get addrs");
exit(1);
close_maps(1);
}
struct ifaddrs *address = addrs;
uint32_t idx = 0;
Expand Down Expand Up @@ -1621,7 +1621,7 @@ bool interface_map()
if (getifaddrs(&addrs) == -1)
{
printf("can't get addrs");
exit(1);
close_maps(1);
}
struct ifaddrs *address = addrs;
uint32_t idx = 0;
Expand Down Expand Up @@ -2201,7 +2201,7 @@ void map_insert()
if (get_key_count() == BPF_MAX_ENTRIES)
{
printf("INSERT FAILURE -- MAX PREFIX TUPLES REACHED\n");
exit(1);
close_maps(1);
}
bool route_insert = false;
if (route)
Expand All @@ -2222,7 +2222,7 @@ void map_insert()
if (fd == -1)
{
printf("BPF_OBJ_GET: %s \n", strerror(errno));
exit(1);
close_maps(1);
}
map.map_fd = fd;
map.key = (uint64_t)&key;
Expand Down Expand Up @@ -2259,7 +2259,7 @@ void map_insert()
{
printf("Unsupported Protocol\n");
close(fd);
exit(1);
close_maps(1);
}
if (lookup)
{
Expand All @@ -2274,7 +2274,7 @@ void map_insert()
{
printf("memcpy failed");
close(fd);
exit(1);
close_maps(1);
}
else
{
Expand All @@ -2289,7 +2289,7 @@ void map_insert()
if (count_fd == -1)
{
printf("BPF_OBJ_GET: %s \n", strerror(errno));
exit(1);
close_maps(1);
}
uint32_t count_key = 0;
uint32_t count_value = 0;
Expand Down Expand Up @@ -2323,15 +2323,15 @@ void map_insert()
{
printf("Insert failed\n");
close(fd);
exit(1);
close_maps(1);
}
}
map.flags = BPF_ANY;
int result = syscall(__NR_bpf, BPF_MAP_UPDATE_ELEM, &map, sizeof(map));
if (result)
{
printf("MAP_UPDATE_ELEM: %s \n", strerror(errno));
exit(1);
close_maps(1);
}
close(fd);
}
Expand All @@ -2346,7 +2346,7 @@ void if_delete_key(uint32_t key)
if (fd == -1)
{
printf("BPF_OBJ_GET: %s\n", strerror(errno));
exit(1);
close_maps(1);
}
// delete element with specified key
map.map_fd = fd;
Expand All @@ -2373,7 +2373,7 @@ void diag_delete_key(uint32_t key)
if (fd == -1)
{
printf("BPF_OBJ_GET: %s\n", strerror(errno));
exit(1);
close_maps(1);
}
// delete element with specified key
map.map_fd = fd;
Expand Down Expand Up @@ -2409,7 +2409,7 @@ void map_delete_key(struct tproxy_key key)
if (fd == -1)
{
printf("BPF_OBJ_GET: %s\n", strerror(errno));
exit(1);
close_maps(1);
}
// delete element with specified key
map.map_fd = fd;
Expand Down Expand Up @@ -2448,7 +2448,7 @@ void map_delete()
if (fd == -1)
{
printf("BPF_OBJ_GET: %s \n", strerror(errno));
exit(1);
close_maps(1);
}
map.map_fd = fd;
map.key = (uint64_t)&key;
Expand All @@ -2458,7 +2458,7 @@ void map_delete()
if (lookup)
{
printf("MAP_DELETE_ELEM: %s\n", strerror(errno));
exit(1);
close_maps(1);
}
else
{
Expand All @@ -2474,7 +2474,7 @@ void map_delete()
else
{
printf("Unsupported Protocol\n");
exit(1);
close_maps(1);
}
remove_index(index, &orule);
if (orule.index_len == 0)
Expand All @@ -2486,7 +2486,7 @@ void map_delete()
if (fd == -1)
{
printf("BPF_OBJ_GET: %s\n", strerror(errno));
exit(1);
close_maps(1);
}
// delete element with specified key
map.map_fd = fd;
Expand All @@ -2496,7 +2496,7 @@ void map_delete()
{
printf("MAP_DELETE_ELEM: %s\n", strerror(errno));
close(fd);
exit(1);
close_maps(1);
}
else
{
Expand All @@ -2511,7 +2511,7 @@ void map_delete()
if (count_fd == -1)
{
printf("BPF_OBJ_GET: %s \n", strerror(errno));
exit(1);
close_maps(1);
}
uint32_t count_key = 0;
uint32_t count_value = 0;
Expand All @@ -2535,7 +2535,7 @@ void map_delete()
{
unbind_prefix(&dcidr, dplen);
}
exit(0);
close_maps(0);
}
}
map.value = (uint64_t)&orule;
Expand All @@ -2547,7 +2547,7 @@ void map_delete()
{
printf("MAP_UPDATE_ELEM: %s \n", strerror(errno));
close(fd);
exit(1);
close_maps(1);
}
}
close(fd);
Expand All @@ -2569,7 +2569,7 @@ void map_flush()
if (fd == -1)
{
printf("BPF_OBJ_GET: %s \n", strerror(errno));
exit(1);
close_maps(1);
}
map.map_fd = fd;
map.key = (uint64_t)key;
Expand Down Expand Up @@ -2598,7 +2598,7 @@ void map_flush()
if (count_fd == -1)
{
printf("BPF_OBJ_GET: %s \n", strerror(errno));
exit(1);
close_maps(1);
}
uint32_t count_key = 0;
uint32_t count_value = 0;
Expand Down Expand Up @@ -2633,7 +2633,7 @@ void map_list()
if (fd == -1)
{
printf("BPF_OBJ_GET: %s \n", strerror(errno));
exit(1);
close_maps(1);
}
map.map_fd = fd;
map.key = (uint64_t)&key;
Expand Down Expand Up @@ -2699,7 +2699,7 @@ void ddos_saddr_map_list()
if (fd == -1)
{
printf("BPF_OBJ_GET: %s \n", strerror(errno));
exit(1);
close_maps(1);
}
map.map_fd = fd;
map.key = (uint64_t)key;
Expand Down Expand Up @@ -2765,7 +2765,7 @@ void ddos_dport_map_list()
if (fd == -1)
{
printf("BPF_OBJ_GET: %s \n", strerror(errno));
exit(1);
close_maps(1);
}
map.map_fd = fd;
map.key = (uint64_t)key;
Expand Down Expand Up @@ -2819,7 +2819,7 @@ int get_key_count()
if (count_fd == -1)
{
printf("BPF_OBJ_GET: %s \n", strerror(errno));
exit(1);
close_maps(1);
}
uint32_t count_key = 0;
uint32_t count_value = 0;
Expand Down Expand Up @@ -2851,7 +2851,7 @@ void map_list_all()
if (fd == -1)
{
printf("BPF_OBJ_GET: %s \n", strerror(errno));
exit(1);
close_maps(1);
}
map.map_fd = fd;
map.key = (uint64_t)key;
Expand Down Expand Up @@ -2953,13 +2953,13 @@ static error_t parse_opt(int key, char *arg, struct argp_state *state)
{
fprintf(stderr, "Interface name or all required as arg to -M, --monitor: %s\n", arg);
fprintf(stderr, "%s --help for more info\n", program_name);
exit(1);
close_maps(1);
}
idx = if_nametoindex(arg);
if (strcmp("all", arg) && idx == 0)
{
printf("Interface not found: %s\n", arg);
exit(1);
close_maps(1);
}
monitor = true;
if (!strcmp("all", arg))
Expand Down Expand Up @@ -3542,7 +3542,7 @@ int main(int argc, char **argv)
ebpf_usage();
}
disable_ebpf();
exit(0);
close_maps(0);
}

if (interface && !(add || delete))
Expand Down

0 comments on commit 823a6e4

Please sign in to comment.