2021-03-04 11:30:38 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "checksum.h"
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <netinet/ip.h>
|
|
|
|
#include <netinet/ip6.h>
|
|
|
|
#include <netinet/tcp.h>
|
2022-01-01 17:22:04 +00:00
|
|
|
#include <netinet/udp.h>
|
2021-03-04 11:30:38 +00:00
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
2024-03-08 08:09:14 +00:00
|
|
|
#ifndef IPPROTO_DIVERT
|
|
|
|
#define IPPROTO_DIVERT 258
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef AF_DIVERT
|
|
|
|
#define AF_DIVERT 44 /* divert(4) */
|
|
|
|
#endif
|
|
|
|
#ifndef PF_DIVERT
|
|
|
|
#define PF_DIVERT AF_DIVERT
|
|
|
|
#endif
|
|
|
|
|
2021-03-04 11:30:38 +00:00
|
|
|
// returns netorder value
|
|
|
|
uint32_t net32_add(uint32_t netorder_value, uint32_t cpuorder_increment);
|
2022-02-02 12:18:35 +00:00
|
|
|
uint32_t net16_add(uint16_t netorder_value, uint16_t cpuorder_increment);
|
2021-03-04 11:30:38 +00:00
|
|
|
|
2022-02-01 17:35:52 +00:00
|
|
|
#define FOOL_NONE 0x00
|
|
|
|
#define FOOL_MD5SIG 0x01
|
|
|
|
#define FOOL_BADSUM 0x02
|
|
|
|
#define FOOL_TS 0x04
|
|
|
|
#define FOOL_BADSEQ 0x08
|
|
|
|
#define FOOL_HOPBYHOP 0x10
|
|
|
|
#define FOOL_HOPBYHOP2 0x20
|
2022-02-05 10:36:03 +00:00
|
|
|
#define FOOL_DESTOPT 0x40
|
2022-02-05 12:41:46 +00:00
|
|
|
#define FOOL_IPFRAG1 0x80
|
2024-03-02 14:53:37 +00:00
|
|
|
#define FOOL_DATANOACK 0x100
|
2021-03-04 11:30:38 +00:00
|
|
|
|
2021-03-19 12:39:32 +00:00
|
|
|
#define SCALE_NONE ((uint8_t)-1)
|
|
|
|
|
2021-03-04 11:30:38 +00:00
|
|
|
// seq and wsize have network byte order
|
|
|
|
bool prepare_tcp_segment4(
|
|
|
|
const struct sockaddr_in *src, const struct sockaddr_in *dst,
|
|
|
|
uint8_t tcp_flags,
|
2021-03-21 08:51:38 +00:00
|
|
|
uint32_t nseq, uint32_t nack_seq,
|
|
|
|
uint16_t nwsize,
|
2021-03-19 12:39:32 +00:00
|
|
|
uint8_t scale_factor,
|
2021-03-04 11:30:38 +00:00
|
|
|
uint32_t *timestamps,
|
|
|
|
uint8_t ttl,
|
2024-03-02 14:53:37 +00:00
|
|
|
uint32_t fooling,
|
2021-12-26 14:11:21 +00:00
|
|
|
uint32_t badseq_increment,
|
|
|
|
uint32_t badseq_ack_increment,
|
2021-03-04 11:30:38 +00:00
|
|
|
const void *data, uint16_t len,
|
|
|
|
uint8_t *buf, size_t *buflen);
|
|
|
|
bool prepare_tcp_segment6(
|
|
|
|
const struct sockaddr_in6 *src, const struct sockaddr_in6 *dst,
|
|
|
|
uint8_t tcp_flags,
|
2021-03-21 08:51:38 +00:00
|
|
|
uint32_t nseq, uint32_t nack_seq,
|
|
|
|
uint16_t nwsize,
|
2021-03-19 12:39:32 +00:00
|
|
|
uint8_t scale_factor,
|
2021-03-04 11:30:38 +00:00
|
|
|
uint32_t *timestamps,
|
|
|
|
uint8_t ttl,
|
2024-03-02 14:53:37 +00:00
|
|
|
uint32_t fooling,
|
2021-12-26 14:11:21 +00:00
|
|
|
uint32_t badseq_increment,
|
|
|
|
uint32_t badseq_ack_increment,
|
2021-03-04 11:30:38 +00:00
|
|
|
const void *data, uint16_t len,
|
|
|
|
uint8_t *buf, size_t *buflen);
|
|
|
|
bool prepare_tcp_segment(
|
|
|
|
const struct sockaddr *src, const struct sockaddr *dst,
|
|
|
|
uint8_t tcp_flags,
|
2021-03-21 08:51:38 +00:00
|
|
|
uint32_t nseq, uint32_t nack_seq,
|
|
|
|
uint16_t nwsize,
|
2021-03-19 12:39:32 +00:00
|
|
|
uint8_t scale_factor,
|
2021-03-04 11:30:38 +00:00
|
|
|
uint32_t *timestamps,
|
|
|
|
uint8_t ttl,
|
2024-03-02 14:53:37 +00:00
|
|
|
uint32_t fooling,
|
2021-12-26 14:11:21 +00:00
|
|
|
uint32_t badseq_increment,
|
|
|
|
uint32_t badseq_ack_increment,
|
2021-03-04 11:30:38 +00:00
|
|
|
const void *data, uint16_t len,
|
|
|
|
uint8_t *buf, size_t *buflen);
|
|
|
|
|
2022-01-01 17:22:04 +00:00
|
|
|
|
|
|
|
bool prepare_udp_segment4(
|
|
|
|
const struct sockaddr_in *src, const struct sockaddr_in *dst,
|
|
|
|
uint8_t ttl,
|
2024-03-02 14:53:37 +00:00
|
|
|
uint32_t fooling,
|
2023-09-07 10:41:25 +00:00
|
|
|
const uint8_t *padding, size_t padding_size,
|
2022-07-27 09:00:36 +00:00
|
|
|
int padlen,
|
2022-01-01 17:22:04 +00:00
|
|
|
const void *data, uint16_t len,
|
|
|
|
uint8_t *buf, size_t *buflen);
|
|
|
|
bool prepare_udp_segment6(
|
|
|
|
const struct sockaddr_in6 *src, const struct sockaddr_in6 *dst,
|
|
|
|
uint8_t ttl,
|
2024-03-02 14:53:37 +00:00
|
|
|
uint32_t fooling,
|
2023-09-07 10:41:25 +00:00
|
|
|
const uint8_t *padding, size_t padding_size,
|
2022-07-27 09:00:36 +00:00
|
|
|
int padlen,
|
2022-01-01 17:22:04 +00:00
|
|
|
const void *data, uint16_t len,
|
|
|
|
uint8_t *buf, size_t *buflen);
|
|
|
|
bool prepare_udp_segment(
|
|
|
|
const struct sockaddr *src, const struct sockaddr *dst,
|
|
|
|
uint8_t ttl,
|
2024-03-02 14:53:37 +00:00
|
|
|
uint32_t fooling,
|
2023-09-07 10:41:25 +00:00
|
|
|
const uint8_t *padding, size_t padding_size,
|
2022-07-27 09:00:36 +00:00
|
|
|
int padlen,
|
2022-01-01 17:22:04 +00:00
|
|
|
const void *data, uint16_t len,
|
|
|
|
uint8_t *buf, size_t *buflen);
|
|
|
|
|
2022-02-05 10:36:03 +00:00
|
|
|
bool ip6_insert_simple_hdr(uint8_t type, uint8_t *data_pkt, size_t len_pkt, uint8_t *buf, size_t *buflen);
|
2022-01-01 17:22:04 +00:00
|
|
|
|
2022-01-03 09:38:18 +00:00
|
|
|
// ipv4: ident==-1 - copy ip_id from original ipv4 packet
|
|
|
|
bool ip_frag4(
|
|
|
|
const uint8_t *pkt, size_t pkt_size,
|
|
|
|
size_t frag_pos, uint32_t ident,
|
|
|
|
uint8_t *pkt1, size_t *pkt1_size,
|
|
|
|
uint8_t *pkt2, size_t *pkt2_size);
|
|
|
|
bool ip_frag6(
|
|
|
|
const uint8_t *pkt, size_t pkt_size,
|
|
|
|
size_t frag_pos, uint32_t ident,
|
|
|
|
uint8_t *pkt1, size_t *pkt1_size,
|
|
|
|
uint8_t *pkt2, size_t *pkt2_size);
|
|
|
|
bool ip_frag(
|
|
|
|
const uint8_t *pkt, size_t pkt_size,
|
|
|
|
size_t frag_pos, uint32_t ident,
|
|
|
|
uint8_t *pkt1, size_t *pkt1_size,
|
|
|
|
uint8_t *pkt2, size_t *pkt2_size);
|
|
|
|
|
|
|
|
|
2022-01-01 17:22:04 +00:00
|
|
|
void extract_ports(const struct tcphdr *tcphdr, const struct udphdr *udphdr, uint8_t *proto, uint16_t *sport, uint16_t *dport);
|
|
|
|
void extract_endpoints(const struct ip *ip,const struct ip6_hdr *ip6hdr,const struct tcphdr *tcphdr,const struct udphdr *udphdr, struct sockaddr_storage *src, struct sockaddr_storage *dst);
|
2021-03-04 11:30:38 +00:00
|
|
|
uint8_t *tcp_find_option(struct tcphdr *tcp, uint8_t kind);
|
|
|
|
uint32_t *tcp_find_timestamps(struct tcphdr *tcp);
|
2021-03-19 12:39:32 +00:00
|
|
|
uint8_t tcp_find_scale_factor(const struct tcphdr *tcp);
|
2021-03-04 11:30:38 +00:00
|
|
|
|
|
|
|
// auto creates internal socket and uses it for subsequent calls
|
2022-05-15 12:54:35 +00:00
|
|
|
bool rawsend(const struct sockaddr* dst,uint32_t fwmark,const char *ifout,const void *data,size_t len);
|
2021-03-04 11:30:38 +00:00
|
|
|
// should pre-do it if dropping privileges. otherwise its not necessary
|
2022-05-15 12:54:35 +00:00
|
|
|
bool rawsend_preinit(bool bind_fix4, bool bind_fix6);
|
2021-03-04 11:30:38 +00:00
|
|
|
// cleans up socket autocreated by rawsend
|
2023-10-13 17:10:46 +00:00
|
|
|
void rawsend_cleanup(void);
|
2021-03-04 11:30:38 +00:00
|
|
|
|
2024-03-08 11:01:24 +00:00
|
|
|
int socket_divert(sa_family_t family);
|
2024-03-08 08:09:14 +00:00
|
|
|
|
2022-01-01 17:22:04 +00:00
|
|
|
const char *proto_name(uint8_t proto);
|
|
|
|
uint16_t family_from_proto(uint8_t l3proto);
|
2021-03-04 11:30:38 +00:00
|
|
|
void print_ip(const struct ip *ip);
|
|
|
|
void print_ip6hdr(const struct ip6_hdr *ip6hdr, uint8_t proto);
|
|
|
|
void print_tcphdr(const struct tcphdr *tcphdr);
|
2022-01-01 17:22:04 +00:00
|
|
|
void print_udphdr(const struct udphdr *udphdr);
|
2021-03-04 11:30:38 +00:00
|
|
|
|
2021-11-12 09:36:25 +00:00
|
|
|
bool proto_check_ipv4(const uint8_t *data, size_t len);
|
2021-03-04 11:30:38 +00:00
|
|
|
void proto_skip_ipv4(uint8_t **data, size_t *len);
|
2021-11-12 09:36:25 +00:00
|
|
|
bool proto_check_ipv6(const uint8_t *data, size_t len);
|
2022-01-05 12:34:57 +00:00
|
|
|
void proto_skip_ipv6(uint8_t **data, size_t *len, uint8_t *proto_type, uint8_t **last_header_type);
|
2022-01-01 17:22:04 +00:00
|
|
|
bool proto_check_tcp(const uint8_t *data, size_t len);
|
|
|
|
void proto_skip_tcp(uint8_t **data, size_t *len);
|
|
|
|
bool proto_check_udp(const uint8_t *data, size_t len);
|
|
|
|
void proto_skip_udp(uint8_t **data, size_t *len);
|
2021-03-12 11:33:48 +00:00
|
|
|
|
2021-03-04 11:30:38 +00:00
|
|
|
bool tcp_synack_segment(const struct tcphdr *tcphdr);
|
2021-03-12 11:33:48 +00:00
|
|
|
bool tcp_syn_segment(const struct tcphdr *tcphdr);
|
|
|
|
bool tcp_ack_segment(const struct tcphdr *tcphdr);
|
2021-03-19 12:39:32 +00:00
|
|
|
// scale_factor=SCALE_NONE - do not change
|
2021-03-12 11:33:48 +00:00
|
|
|
void tcp_rewrite_wscale(struct tcphdr *tcp, uint8_t scale_factor);
|
|
|
|
void tcp_rewrite_winsize(struct tcphdr *tcp, uint16_t winsize, uint8_t scale_factor);
|
2024-03-02 14:53:37 +00:00
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
uint8_t delta, min, max;
|
|
|
|
} autottl;
|
|
|
|
#define AUTOTTL_DEFAULT_DELTA 1
|
|
|
|
#define AUTOTTL_DEFAULT_MIN 3
|
|
|
|
#define AUTOTTL_DEFAULT_MAX 20
|
|
|
|
#define AUTOTTL_ENABLED(a) (!!(a).delta)
|
|
|
|
#define AUTOTTL_SET_DEFAULT(a) {(a).delta=AUTOTTL_DEFAULT_DELTA; (a).min=AUTOTTL_DEFAULT_MIN; (a).max=AUTOTTL_DEFAULT_MAX;}
|
|
|
|
|
|
|
|
uint8_t autottl_guess(uint8_t ttl, const autottl *attl);
|