-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rc add zfw calls 1.3.x #1054
Merged
Merged
Rc add zfw calls 1.3.x #1054
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
21c62ee
rebase zfw integration. run commands on worker threads.
scareything bf66b24
fix todos. only present diverter options on linux
scareything ca521e4
try to appease msvc preprocessor
scareything 8f9d3c0
parse diverter options
scareything 1ab5a81
correct va_arg usage
scareything 43e4f36
add queue_command function
scareything 1d793e3
use correct zfw arguments
scareything 2ab3c59
use correct ip link arguments
scareything 20497af
use correct tc object when initializing diverter
scareything 35d73a6
report nonzero zfw exit codes
scareything 8d697e3
use zfw_path when adding user rules
scareything 7e2e7e5
remove extraneous comma
scareything 77aa07c
promote log messages from diverter initialization
scareything 696efda
call set_diverter with dns subnet vs dns ip
scareything e23d233
use vsnprintf
scareything d363b83
move diverter fns into their own module
scareything 26782eb
remove unused include
scareything cd2e6ff
removed year from copyright
scareything File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
Copyright NetFoundry Inc. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
https://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
// support interactions with zfw command line utility | ||
|
||
#include <stdint.h> | ||
#include "model/dtos.h" | ||
|
||
extern char *diverter_if; | ||
extern bool diverter; | ||
extern bool firewall; | ||
|
||
void diverter_init(uint32_t dns_prefix, uint32_t dns_prefix_len, const char *tun_name); | ||
void diverter_quit(); | ||
void init_diverter_interface(const char *interface, const char *direction); | ||
void diverter_add_svc(const tunnel_service *svc); | ||
void diverter_remove_svc(const tunnel_service *svc); | ||
void set_diverter(uint32_t dns_prefix, unsigned char dns_prefix_len, const char *tun_name); | ||
void diverter_cleanup(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,264 @@ | ||
/* | ||
Copyright NetFoundry Inc. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
https://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
#include <stdbool.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <limits.h> | ||
#include <net/if.h> | ||
|
||
#include <uv.h> | ||
|
||
#include "ziti/ziti_log.h" | ||
#include "linux/diverter.h" | ||
#include "../netif_driver/linux/utils.h" | ||
|
||
char check_alt[IF_NAMESIZE]; | ||
char *diverter_path = "/opt/openziti/bin"; | ||
char zfw_path[PATH_MAX]; | ||
char *tc_ingress_object = "zfw_tc_ingress.o"; | ||
char *tc_egress_object = "zfw_tc_outbound_track.o"; | ||
char *xdp_ingress_object = "zfw_xdp_tun_ingress.o"; | ||
|
||
char *diverter_if = NULL; | ||
bool diverter = false; | ||
bool firewall = false; | ||
|
||
void diverter_init(uint32_t dns_prefix, uint32_t dns_prefix_len, const char *tun_name) { | ||
if(!diverter && !firewall){ | ||
diverter_if = getenv("ZITI_DIVERTER"); | ||
if(diverter_if && strlen(diverter_if)){ | ||
diverter = true; | ||
} | ||
char *zifi_firewall = getenv("ZITI_FIREWALL"); | ||
if(zifi_firewall && strlen(zifi_firewall)){ | ||
diverter = true; | ||
firewall = true; | ||
diverter_if = getenv("ZITI_FIREWALL"); | ||
} | ||
} | ||
char *diverter_env_path = getenv("ZFW_OBJECT_PATH"); | ||
if(diverter_env_path && strlen(diverter_env_path)){ | ||
diverter_path = diverter_env_path; | ||
snprintf(zfw_path, sizeof(zfw_path), "%s/%s", diverter_env_path, "zfw"); | ||
}else{ | ||
snprintf(zfw_path, sizeof(zfw_path), "%s/%s", diverter_path, "zfw"); | ||
} | ||
if(diverter && tun_name){ | ||
if(!firewall){ | ||
diverter_quit(); | ||
} | ||
if (is_executable(zfw_path)){ | ||
int count = 0; | ||
char *interface = strtok(diverter_if,","); | ||
while(interface != NULL){ | ||
uint32_t idx = if_nametoindex(interface); | ||
if (!idx) | ||
{ | ||
ZITI_LOG(WARN,"Diverter interface not found: %s", interface); | ||
interface = strtok(NULL,","); | ||
continue; | ||
} | ||
if(if_indextoname(idx, check_alt)){ | ||
interface = check_alt; | ||
} | ||
init_diverter_interface(interface, "ingress"); | ||
init_diverter_interface(interface, "egress"); | ||
interface = strtok(NULL,","); | ||
count++; | ||
} | ||
if(count){ | ||
set_diverter(dns_prefix, dns_prefix_len, tun_name); | ||
}else{ | ||
ZITI_LOG(ERROR,"No valid diverter interfaces found"); | ||
exit(1); | ||
} | ||
}else{ | ||
ZITI_LOG(ERROR, "Diverter binary not found"); | ||
exit(1); | ||
} | ||
} | ||
} | ||
|
||
static void diverter_update(const char *ip, uint8_t prefix_len, uint16_t lowport, uint16_t highport, const char *protocol, const char *service_id, const char *action) { | ||
int rndm; | ||
uv_random(NULL, NULL, &rndm, sizeof(rndm), 0, NULL); | ||
unsigned short random_port = 1024 + rndm % (65535 - 1023); | ||
// called while uv loop is running, so queue the command to prevent i/o stalls | ||
queue_command("%s %s -c %s -m %d -l %d -h %d -t %d -p %s -s %s", zfw_path, action, ip, prefix_len, lowport, highport, random_port, protocol, service_id); | ||
} | ||
|
||
void diverter_quit() { | ||
run_command("%s -Q", zfw_path); | ||
} | ||
|
||
void init_diverter_interface(const char *interface, const char *direction) { | ||
// run_command is ok here because the uv loop is not yet running when this is called | ||
const char *obj = (strcmp(direction, "ingress") == 0 ? tc_ingress_object : tc_egress_object); | ||
int ec = run_command("%s -X %s -O %s/%s -z %s", zfw_path, interface, diverter_path, obj, direction); | ||
if (ec != 0) { | ||
ZITI_LOG(WARN, "zfw -X failed"); | ||
return; | ||
} | ||
|
||
// set tun mode | ||
ec = run_command("%s -T %s", zfw_path, interface); | ||
if (ec != 0) { | ||
ZITI_LOG(WARN, "zfw -T failed"); | ||
return; | ||
} | ||
|
||
// enable ipv6 | ||
ec = run_command("%s -6 %s", zfw_path, interface); | ||
if (ec != 0) { | ||
ZITI_LOG(WARN, "zfw -6 failed"); | ||
return; | ||
} | ||
|
||
// pass non-tuple | ||
if (!firewall) { | ||
run_command("%s -q %s", zfw_path, interface); | ||
} | ||
} | ||
|
||
static void bind_diverter_route(const char *ip, uint8_t prefix_len) { | ||
// called while uv loop is running, so queue the command to prevent i/o stalls | ||
queue_command("%s -B %s -m %d", zfw_path, ip, prefix_len); | ||
} | ||
|
||
static void unbind_diverter_route(const char *ip, uint8_t prefix_len) { | ||
// called while uv loop is running, so queue the command to prevent i/o stalls | ||
queue_command("%s -J %s -m %d", zfw_path, ip, prefix_len); | ||
} | ||
|
||
void diverter_add_svc(const tunnel_service *svc) { | ||
if(svc && diverter){ | ||
if(svc->Permissions.Dial){ | ||
for(int x = 0; svc->Addresses && (svc->Addresses[x] != NULL); x++){ | ||
if(!svc->Addresses[x]->IsHost){ | ||
for(int i = 0; (svc->Ports != NULL) && (svc->Ports[i] != NULL); i++){ | ||
for(int j = 0; (svc->Protocols != NULL) && (svc->Protocols[j] != NULL); j++){ | ||
if((svc->AllowedSourceAddresses && svc->AllowedSourceAddresses[0] != NULL) || firewall){ | ||
diverter_update(svc->Addresses[x]->IP, svc->Addresses[x]->Prefix, svc->Ports[i]->Low, svc->Ports[i]->High, svc->Protocols[j], svc->Id, "-I"); | ||
} | ||
} | ||
} | ||
|
||
} | ||
} | ||
} | ||
else if(svc->Permissions.Bind){ | ||
for(int x = 0; svc->AllowedSourceAddresses && (svc->AllowedSourceAddresses[x] != NULL); x++){ | ||
if(!svc->AllowedSourceAddresses[x]->IsHost){ | ||
bind_diverter_route(svc->AllowedSourceAddresses[x]->IP, svc->AllowedSourceAddresses[x]->Prefix); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
void diverter_remove_svc(const tunnel_service *svc) { | ||
if(svc && diverter){ | ||
if(svc->Permissions.Bind){ | ||
for(int x = 0; svc->AllowedSourceAddresses && (svc->AllowedSourceAddresses[x] != NULL); x++){ | ||
if(!svc->AllowedSourceAddresses[x]->IsHost){ | ||
unbind_diverter_route(svc->AllowedSourceAddresses[x]->IP, svc->AllowedSourceAddresses[x]->Prefix); | ||
} | ||
} | ||
} | ||
if(svc->Permissions.Dial){ | ||
for(int x = 0; svc->Addresses && (svc->Addresses[x] != NULL); x++){ | ||
if(!svc->Addresses[x]->IsHost){ | ||
for(int i = 0; (svc->Ports != NULL) && (svc->Ports[i] != NULL); i++){ | ||
for(int j = 0; (svc->Protocols != NULL) && (svc->Protocols[j] != NULL); j++){ | ||
if((svc->AllowedSourceAddresses && svc->AllowedSourceAddresses[0] != NULL) || firewall){ | ||
diverter_update(svc->Addresses[x]->IP, svc->Addresses[x]->Prefix, svc->Ports[i]->Low, svc->Ports[i]->High, svc->Protocols[j], svc->Id, "-D"); | ||
} | ||
} | ||
} | ||
|
||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
static void diverter_binding_flush() { | ||
// called by exit handler, so run_command is appropriate | ||
run_command("%s -F -j", zfw_path); | ||
} | ||
|
||
static void diverter_ingress_flush() { | ||
// called by exit handler, so run_command is appropriate | ||
run_command("%s -F -z ingress", zfw_path); | ||
} | ||
|
||
static void setup_xdp(const char *tun_name) { | ||
// run_command is ok here because the uv loop is not yet running when this is called | ||
run_command("/usr/sbin/ip link set %s xdpgeneric obj %s/%s sec xdp_redirect", tun_name, diverter_path, xdp_ingress_object); | ||
} | ||
|
||
static void add_user_rules() { | ||
// called by exit handler, so run_command is appropriate | ||
run_command("%s -A", zfw_path); | ||
} | ||
|
||
static void disable_firewall() { | ||
// run_command is ok here because the uv loop is not yet running when this is called | ||
run_command("%s -I -c 0.0.0.0 -m 0 -l 1 -h 65535 -t 0 -p tcp", zfw_path); | ||
run_command("%s -I -c 0.0.0.0 -m 0 -l 1 -h 65535 -t 0 -p udp", zfw_path); | ||
run_command("%s -I -c :: -m 0 -l 1 -h 65535 -t 0 -p tcp", zfw_path); | ||
run_command("%s -I -c :: -m 0 -l 1 -h 65535 -t 0 -p udp", zfw_path); | ||
} | ||
|
||
static void pass_dns_range(uint32_t dns_prefix, uint8_t dns_prefix_len) { | ||
// run_command is ok here because the uv loop is not yet running when this is called | ||
char prefix[INET_ADDRSTRLEN]; | ||
inet_ntop(AF_INET, &dns_prefix, prefix, INET_ADDRSTRLEN); | ||
run_command("%s -I -c %s -m %d -l 1 -h 65535 -t 65535 -p tcp", zfw_path, prefix, dns_prefix_len); | ||
run_command("%s -I -c %s -m %d -l 1 -h 65535 -t 65535 -p udp", zfw_path, prefix, dns_prefix_len); | ||
} | ||
|
||
void set_diverter(uint32_t dns_prefix, uint8_t dns_prefix_len, const char *tun_name) | ||
{ | ||
if (!firewall) { | ||
ZITI_LOG(INFO,"Starting ziti-edge-tunnel in diverter mode"); | ||
} else { | ||
ZITI_LOG(INFO,"Starting ziti-edge-tunnel in diverter firewall mode"); | ||
} | ||
if (!firewall) { | ||
disable_firewall(); | ||
} else { | ||
if (is_executable(zfw_path)) { | ||
ZITI_LOG(INFO,"loading user defined FW rules"); | ||
add_user_rules(); | ||
} else { | ||
ZITI_LOG(DEBUG, "Diverter user defined FW rules not found"); | ||
} | ||
pass_dns_range(dns_prefix, dns_prefix_len); | ||
} | ||
setup_xdp(tun_name); | ||
} | ||
|
||
void diverter_cleanup(void) { | ||
if (diverter && !firewall) { | ||
diverter_binding_flush(); | ||
diverter_quit(); | ||
} else if (firewall) { | ||
diverter_binding_flush(); | ||
diverter_ingress_flush(); | ||
add_user_rules(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should this be
else
? Both permissions could be set