Merge pull request #1 from Waujito/zabbius-kern_mod

Zabbius kern mod
This commit is contained in:
Sergey Zabodalov 2024-08-07 20:31:58 +03:00 committed by GitHub
commit 0a66da7014
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 137 additions and 93 deletions

View File

@ -1,3 +1,17 @@
struct config_t {
unsigned int queue_start_num;
int rawsocket;
int threads;
int use_gso;
int fragmentation_strategy;
unsigned char fake_sni_ttl;
int fake_sni_strategy;
int verbose;
unsigned int seg2_delay;
};
extern struct config_t config;
#define MAX_THREADS 16
#ifndef THREADS_NUM

View File

@ -190,9 +190,8 @@ int ip4_frag(const __u8 *pkt, __u32 buflen, __u32 payload_offset,
f2_hdr->tot_len = htons(f2_dlen);
#if defined(DEBUG)
if (config.verbose)
printf("Packet split in portion %u %u\n", f1_plen, f2_plen);
#endif
ip4_set_checksum(f1_hdr);
ip4_set_checksum(f2_hdr);
@ -226,7 +225,11 @@ int tcp4_frag(const __u8 *pkt, __u32 buflen, __u32 payload_offset,
}
if (!(ntohs(hdr->frag_off) & IP_DF)) {
if (
ntohs(hdr->frag_off) & IP_MF ||
ntohs(hdr->frag_off) & IP_OFFMASK) {
printf("tcp4_frag: frag value: %d\n",
ntohs(hdr->frag_off));
lgerror("tcp4_frag: ip fragmentation is set", -EINVAL);
return -EINVAL;
}
@ -268,9 +271,8 @@ int tcp4_frag(const __u8 *pkt, __u32 buflen, __u32 payload_offset,
s2_tcph->seq = htonl(ntohl(s2_tcph->seq) + payload_offset);
#if defined(DEBUG)
if (config.verbose)
printf("Packet split in portion %u %u\n", s1_plen, s2_plen);
#endif
tcp4_set_checksum(s1_tcph, s1_hdr);
tcp4_set_checksum(s2_tcph, s2_hdr);

View File

@ -29,18 +29,9 @@
#include "config.h"
#include "mangle.h"
static struct {
uint32_t queue_start_num;
int rawsocket;
pthread_mutex_t rawsocket_lock;
int threads;
bool use_gso;
int fragmentation_strategy;
uint8_t fake_sni_ttl;
int fake_sni_strategy;
bool verbose;
uint32_t seg2_delay;
} config = {
pthread_mutex_t rawsocket_lock;
struct config_t config = {
.rawsocket = -2,
.threads = THREADS_NUM,
.fragmentation_strategy = FRAGMENTATION_STRATEGY,
@ -68,114 +59,148 @@ static struct {
const char* get_value(const char *option, const char *prefix)
{
errno = 0;
size_t prefix_len = strlen(prefix);
size_t option_len = strlen(option);
if (option_len < prefix_len || memcmp(prefix, option, prefix_len))
return 0;
if (option_len <= prefix_len || strncmp(prefix, option, prefix_len)) {
return NULL;
}
return option + prefix_len;
}
bool parse_bool_option(const char *value) {
int parse_bool_option(const char *value) {
errno = 0;
if (strcmp(value, "1") == 0) {
return true;
return 1;
}
else if (strcmp(value, "0") == 0) {
return false;
return 0;
}
errno = EINVAL;
return false;
}
uint32_t parse_uint_option(const char* value) {
char* end;
uint32_t result = strtoul(value, &end, 10);
if (*end != '\0') {
errno = EINVAL;
return -1;
}
long parse_numeric_option(const char* value) {
errno = 0;
char* end;
long result = strtol(value, &end, 10);
if (*end != '\0') {
errno = EINVAL;
return 0;
}
return result;
}
bool parse_option(const char* option) {
int parse_option(const char* option) {
const char* value;
int ret;
if ((value = get_value(option, "--gso=")) != 0) {
config.use_gso = parse_bool_option(value);
return errno == 0;
if (!strcmp(option, "--no-gso")) {
config.use_gso = 0;
goto out;
}
if ((value = get_value(option, "--verbose=")) != 0) {
config.verbose = parse_bool_option(value);
return errno == 0;
if (!strcmp(option, "--silent")) {
config.verbose = 0;
goto out;
}
if ((value = get_value(option, "--frag=")) != 0) {
if (!value) {
goto err;
}
if (strcmp(value, "tcp") == 0) {
config.fragmentation_strategy = FRAG_STRAT_TCP;
}
else if (strcmp(value, "ip") == 0) {
} else if (strcmp(value, "ip") == 0) {
config.fragmentation_strategy = FRAG_STRAT_IP;
}
else if (strcmp(value, "none") == 0) {
} else if (strcmp(value, "none") == 0) {
config.fragmentation_strategy = FRAG_STRAT_NONE;
} else {
goto err;
}
else {
return false;
}
return true;
goto out;
}
if ((value = get_value(option, "--fake-sni=")) != 0) {
if (strcmp(value, "ack") == 0) {
config.fake_sni_strategy = FKSN_STRAT_ACK_SEQ;
}
else if (strcmp(value, "ttl") == 0) {
} else if (strcmp(value, "ttl") == 0) {
config.fake_sni_strategy = FKSN_STRAT_TTL;
}
else if (strcmp(value, "none") == 0) {
config.fake_sni_strategy = FKSN_STRAT_NONE;
} else {
goto err;
}
else {
return false;
}
return true;
goto out;
}
if ((value = get_value(option, "--seg2delay=")) != 0) {
config.seg2_delay = parse_uint_option(value);
return errno == 0;
long num = parse_numeric_option(value);
if (errno != 0 ||
num < 0)
goto err;
config.seg2_delay = num;
goto out;
}
if ((value = get_value(option, "--threads=")) != 0) {
config.threads = parse_uint_option(value);
return errno == 0 && config.threads <= MAX_THREADS;
long num = parse_numeric_option(value);
if (errno != 0 ||
num < 0 ||
num > MAX_THREADS) {
errno = EINVAL;
goto err;
}
config.threads = num;
goto out;
}
if ((value = get_value(option, "--fake-sni-ttl=")) != 0) {
config.fake_sni_ttl = parse_uint_option(value);
return errno == 0;
long num = parse_numeric_option(value);
if (errno != 0 ||
num < 0 ||
num > 255) {
goto err;
}
return false;
config.fake_sni_ttl = num;
goto out;
}
err:
errno = EINVAL;
return -1;
out:
errno = 0;
return 0;
}
static int parse_args(int argc, const char *argv[]) {
int err;
char *end;
if (argc != 2) {
if (argc < 2) {
errno = EINVAL;
goto errormsg_help;
}
config.queue_start_num = parse_uint_option(argv[1]);
config.queue_start_num = parse_numeric_option(argv[1]);
if (errno != 0) goto errormsg_help;
for (int i = 2; i < argc; i++) {
if (!parse_option(argv[i])) {
if (parse_option(argv[i])) {
printf("Invalid option %s\n", argv[i]);
goto errormsg_help;
}
@ -187,13 +212,13 @@ errormsg_help:
err = errno;
printf("Usage: %s <queue_num> [OPTIONS]\n", argv[0]);
printf("Options:\n");
printf("\t--gso={0,1}\n");
printf("\t--fake-sni={ack,ttl,none}\n");
printf("\t--fake-sni-ttl=<ttl>\n");
printf("\t--frag={tcp,ip,none}\n");
printf("\t--seg2delay=<delay>\n");
printf("\t--threads=<threads number>\n");
printf("\t--verbose={0,1}\n");
printf("\t--silent\n");
printf("\t--no-gso\n");
errno = err;
if (errno == 0) errno = EINVAL;
@ -254,7 +279,7 @@ static int open_raw_socket(void) {
return -1;
}
int mst = pthread_mutex_init(&config.rawsocket_lock, NULL);
int mst = pthread_mutex_init(&rawsocket_lock, NULL);
if (mst) {
fprintf(stderr, "Mutex err: %d\n", mst);
close(config.rawsocket);
@ -276,11 +301,11 @@ static int close_raw_socket(void) {
if (close(config.rawsocket)) {
perror("Unable to close raw socket");
pthread_mutex_destroy(&config.rawsocket_lock);
pthread_mutex_destroy(&rawsocket_lock);
return -1;
}
pthread_mutex_destroy(&config.rawsocket_lock);
pthread_mutex_destroy(&rawsocket_lock);
config.rawsocket = -2;
return 0;
@ -318,9 +343,7 @@ static int send_raw_socket(const uint8_t *pkt, uint32_t pktlen) {
break;
default:
errno = EINVAL;
printf("send_raw_socket: Packet is too big but fragmentation is disabled! "
"Pass -DRAWSOCK_TCP_FSTRAT or -DRAWSOCK_IP_FSTRAT as CFLAGS "
"To enable it only for raw socket\n");
printf("send_raw_socket: Packet is too big but fragmentation is disabled!\n");
return -EINVAL;
}
@ -359,13 +382,13 @@ static int send_raw_socket(const uint8_t *pkt, uint32_t pktlen) {
}
};
pthread_mutex_lock(&config.rawsocket_lock);
pthread_mutex_lock(&rawsocket_lock);
int sent = sendto(config.rawsocket,
pkt, pktlen, 0,
(struct sockaddr *)&daddr, sizeof(daddr));
pthread_mutex_unlock(&config.rawsocket_lock);
pthread_mutex_unlock(&rawsocket_lock);
/* The function will return -errno on error as well as errno value set itself */
if (sent < 0) sent = -errno;
@ -479,9 +502,6 @@ static int process_packet(const struct packet_data packet, struct queue_data qda
fprintf(stderr, "WARNING! Google video packet is too big and may cause issues!\n");
}
uint8_t fake_sni[MNL_SOCKET_BUFFER_SIZE];
uint32_t fsn_len = MNL_SOCKET_BUFFER_SIZE;
uint8_t frag1[MNL_SOCKET_BUFFER_SIZE];
uint8_t frag2[MNL_SOCKET_BUFFER_SIZE];
uint32_t f1len = MNL_SOCKET_BUFFER_SIZE;
@ -494,6 +514,9 @@ static int process_packet(const struct packet_data packet, struct queue_data qda
(struct tcphdr *)tcph, (struct iphdr *)iph);
if (config.fake_sni_strategy != FKSN_STRAT_NONE) {
uint8_t fake_sni[MNL_SOCKET_BUFFER_SIZE];
uint32_t fsn_len = MNL_SOCKET_BUFFER_SIZE;
ret = gen_fake_sni(iph, tcph, fake_sni, &fsn_len);
if (ret < 0) {
errno = -ret;
@ -522,8 +545,9 @@ static int process_packet(const struct packet_data packet, struct queue_data qda
errno = -ret;
perror("tcp4_frag");
goto send_verd;
goto fallback;
}
break;
case FRAG_STRAT_IP:
ipd_offset = ((char *)data - (char *)tcph) + vrd.sni_offset;
@ -535,8 +559,9 @@ static int process_packet(const struct packet_data packet, struct queue_data qda
errno = -ret;
perror("ip4_frag");
goto send_verd;
goto fallback;
}
break;
default:
ret = send_raw_socket(raw_payload, raw_payload_len);
@ -544,7 +569,7 @@ static int process_packet(const struct packet_data packet, struct queue_data qda
errno = -ret;
perror("raw pack send");
}
goto send_verd;
goto fallback;
}
ret = send_raw_socket(frag2, f2len);
@ -552,7 +577,7 @@ static int process_packet(const struct packet_data packet, struct queue_data qda
errno = -ret;
perror("raw frags send: frag2");
goto send_verd;
goto fallback;
}
if (config.seg2_delay) {
@ -564,14 +589,14 @@ static int process_packet(const struct packet_data packet, struct queue_data qda
pthread_t thr;
pthread_create(&thr, NULL, delay_packet_send, dpdt);
pthread_detach(thr);
}
else {
} else {
ret = send_raw_socket(frag1, f1len);
if (ret < 0) {
errno = -ret;
perror("raw frags send: frag1");
goto send_verd;
goto fallback;
}
}
}
@ -728,6 +753,7 @@ struct queue_conf {
struct queue_res {
int status;
};
static struct queue_res defqres = {0};
static struct queue_res threads_reses[MAX_THREADS];
@ -760,8 +786,9 @@ int main(int argc, const char *argv[]) {
break;
}
if (config.seg2_delay)
if (config.seg2_delay) {
printf("Some outgoing googlevideo request segments will be delayed for %d ms as of seg2_delay define\n", config.seg2_delay);
}
switch (config.fake_sni_strategy) {
case FKSN_STRAT_TTL:
@ -775,15 +802,16 @@ int main(int argc, const char *argv[]) {
break;
}
if (config.use_gso)
if (config.use_gso) {
printf("GSO is enabled\n");
}
if (open_raw_socket() < 0) {
perror("Unable to open raw socket");
exit(EXIT_FAILURE);
}
struct queue_res *qres;
struct queue_res *qres = &defqres;
if (config.threads == 1) {
struct queue_conf tconf = {