NetGuard/app/src/main/jni/netguard/netguard.h

551 lines
15 KiB
C
Raw Normal View History

2016-01-18 11:19:40 +00:00
#include <jni.h>
2016-02-09 12:39:49 +00:00
#include <stdio.h>
#include <stdlib.h>
2017-12-20 16:58:32 +00:00
#include <string.h>
2016-02-09 12:39:49 +00:00
#include <ctype.h>
#include <time.h>
#include <unistd.h>
#include <pthread.h>
2017-10-22 07:39:11 +00:00
#include <setjmp.h>
2016-02-09 12:39:49 +00:00
#include <errno.h>
#include <fcntl.h>
2016-03-04 13:55:47 +00:00
#include <dirent.h>
2016-06-25 07:28:34 +00:00
#include <poll.h>
#include <sys/types.h>
2016-02-09 12:39:49 +00:00
#include <sys/ioctl.h>
#include <sys/socket.h>
2016-06-24 13:21:04 +00:00
#include <sys/epoll.h>
2016-02-09 12:39:49 +00:00
#include <dlfcn.h>
2016-02-13 12:41:38 +00:00
#include <sys/stat.h>
#include <sys/resource.h>
2016-02-09 12:39:49 +00:00
#include <netdb.h>
2016-02-09 12:39:49 +00:00
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/in6.h>
2016-02-09 12:39:49 +00:00
#include <netinet/ip.h>
#include <netinet/ip6.h>
#include <netinet/udp.h>
#include <netinet/tcp.h>
#include <netinet/ip_icmp.h>
#include <netinet/icmp6.h>
#include <android/log.h>
#include <sys/system_properties.h>
2016-01-18 11:19:40 +00:00
2016-01-17 16:41:54 +00:00
#define TAG "NetGuard.JNI"
2016-01-21 12:46:57 +00:00
2016-02-09 12:39:49 +00:00
// #define PROFILE_JNI 5
2016-06-25 07:12:54 +00:00
#define EPOLL_TIMEOUT 3600 // seconds
#define EPOLL_EVENTS 20
2016-06-26 07:20:19 +00:00
#define EPOLL_MIN_CHECK 100 // milliseconds
2016-01-21 12:46:57 +00:00
2016-02-03 06:58:25 +00:00
#define ICMP4_MAXMSG (IP_MAXPACKET - 20 - 8) // bytes (socket)
#define ICMP6_MAXMSG (IPV6_MAXPACKET - 40 - 8) // bytes (socket)
#define UDP4_MAXMSG (IP_MAXPACKET - 20 - 8) // bytes (socket)
#define UDP6_MAXMSG (IPV6_MAXPACKET - 40 - 8) // bytes (socket)
2016-01-22 18:03:32 +00:00
2016-02-03 06:42:06 +00:00
#define ICMP_TIMEOUT 15 // seconds
2016-02-02 13:31:44 +00:00
2016-01-24 12:39:04 +00:00
#define UDP_TIMEOUT_53 15 // seconds
#define UDP_TIMEOUT_ANY 300 // seconds
#define UDP_KEEP_TIMEOUT 60 // seconds
2016-01-21 12:46:57 +00:00
2017-11-10 06:49:27 +00:00
#define TCP_INIT_TIMEOUT 20 // seconds ~net.inet.tcp.keepinit
#define TCP_IDLE_TIMEOUT 3600 // seconds ~net.inet.tcp.keepidle
2017-11-10 06:49:27 +00:00
#define TCP_CLOSE_TIMEOUT 20 // seconds
#define TCP_KEEP_TIMEOUT 300 // seconds
// https://en.wikipedia.org/wiki/Maximum_segment_lifetime
2016-01-21 12:46:57 +00:00
2018-03-26 15:19:45 +00:00
#define SESSION_LIMIT 40 // percent
2018-03-25 05:17:09 +00:00
#define SESSION_MAX (1024 * SESSION_LIMIT / 100) // number
2017-06-01 20:10:32 +00:00
2018-03-22 09:07:45 +00:00
#define SEND_BUF_DEFAULT 163840 // bytes
2017-11-05 07:08:20 +00:00
#define UID_MAX_AGE 30000 // milliseconds
2016-07-28 15:46:19 +00:00
#define SOCKS5_NONE 1
#define SOCKS5_HELLO 2
2016-07-28 16:36:27 +00:00
#define SOCKS5_AUTH 3
#define SOCKS5_CONNECT 4
#define SOCKS5_CONNECTED 5
2016-07-28 15:46:19 +00:00
2017-11-09 18:07:08 +00:00
struct context {
pthread_mutex_t lock;
int pipefds[2];
int stopping;
2018-01-06 07:21:26 +00:00
int sdk;
2017-11-09 18:07:08 +00:00
struct ng_session *ng_session;
};
2016-01-17 16:41:54 +00:00
struct arguments {
2016-01-18 11:19:40 +00:00
JNIEnv *env;
2016-01-17 16:41:54 +00:00
jobject instance;
int tun;
jboolean fwd53;
2017-02-10 20:12:04 +00:00
jint rcode;
2017-11-09 18:07:08 +00:00
struct context *ctx;
2016-01-17 16:41:54 +00:00
};
struct allowed {
2016-02-08 15:34:54 +00:00
char raddr[INET6_ADDRSTRLEN + 1];
uint16_t rport; // host notation
};
2016-02-05 09:43:28 +00:00
struct segment {
uint32_t seq;
uint16_t len;
uint16_t sent;
2016-02-05 09:43:28 +00:00
int psh;
uint8_t *data;
struct segment *next;
};
2016-02-02 13:31:44 +00:00
struct icmp_session {
time_t time;
jint uid;
int version;
union {
__be32 ip4; // network notation
struct in6_addr ip6;
} saddr;
union {
__be32 ip4; // network notation
struct in6_addr ip6;
} daddr;
uint16_t id;
uint8_t stop;
};
2016-02-04 09:09:31 +00:00
#define UDP_ACTIVE 0
#define UDP_FINISHING 1
#define UDP_CLOSED 2
#define UDP_BLOCKED 3
2016-01-20 13:11:04 +00:00
struct udp_session {
time_t time;
jint uid;
int version;
2016-03-08 07:57:04 +00:00
uint16_t mss;
2016-02-15 16:48:53 +00:00
uint64_t sent;
uint64_t received;
union {
__be32 ip4; // network notation
struct in6_addr ip6;
} saddr;
2016-01-20 13:11:04 +00:00
__be16 source; // network notation
union {
__be32 ip4; // network notation
struct in6_addr ip6;
} daddr;
2016-01-20 13:11:04 +00:00
__be16 dest; // network notation
2016-02-04 09:09:31 +00:00
uint8_t state;
2016-01-20 13:11:04 +00:00
};
2016-01-20 11:35:51 +00:00
struct tcp_session {
2016-01-19 19:58:51 +00:00
jint uid;
2016-01-22 18:03:32 +00:00
time_t time;
int version;
2016-03-03 12:49:52 +00:00
uint16_t mss;
uint8_t recv_scale;
uint8_t send_scale;
uint32_t recv_window; // host notation, scaled
uint32_t send_window; // host notation, scaled
uint16_t unconfirmed; // packets
2016-01-17 16:41:54 +00:00
uint32_t remote_seq; // confirmed bytes received, host notation
uint32_t local_seq; // confirmed bytes sent, host notation
uint32_t remote_start;
uint32_t local_start;
2016-02-13 13:46:03 +00:00
uint32_t acked; // host notation
2016-06-27 06:38:43 +00:00
long long last_keep_alive;
2016-02-13 13:46:03 +00:00
2016-02-15 16:48:53 +00:00
uint64_t sent;
uint64_t received;
union {
__be32 ip4; // network notation
struct in6_addr ip6;
} saddr;
2016-01-17 16:41:54 +00:00
__be16 source; // network notation
union {
__be32 ip4; // network notation
struct in6_addr ip6;
} daddr;
2016-01-17 16:41:54 +00:00
__be16 dest; // network notation
2016-01-17 16:41:54 +00:00
uint8_t state;
2016-07-28 15:46:19 +00:00
uint8_t socks5;
2016-02-06 07:04:15 +00:00
struct segment *forward;
2016-06-24 13:21:04 +00:00
};
2016-02-05 09:43:28 +00:00
2016-06-24 13:21:04 +00:00
struct ng_session {
uint8_t protocol;
union {
struct icmp_session icmp;
struct udp_session udp;
struct tcp_session tcp;
};
jint socket;
struct epoll_event ev;
struct ng_session *next;
2016-01-17 16:41:54 +00:00
};
2017-11-05 07:08:20 +00:00
struct uid_cache_entry {
uint8_t version;
uint8_t protocol;
uint8_t saddr[16];
uint16_t sport;
uint8_t daddr[16];
uint16_t dport;
jint uid;
long time;
};
// IPv6
struct ip6_hdr_pseudo {
struct in6_addr ip6ph_src;
struct in6_addr ip6ph_dst;
u_int32_t ip6ph_len;
u_int8_t ip6ph_zero[3];
u_int8_t ip6ph_nxt;
} __packed;
// PCAP
2016-01-17 16:41:54 +00:00
// https://wiki.wireshark.org/Development/LibpcapFileFormat
2016-01-23 14:50:18 +00:00
typedef uint16_t guint16_t;
typedef uint32_t guint32_t;
typedef int32_t gint32_t;
2016-01-17 16:41:54 +00:00
typedef struct pcap_hdr_s {
2016-01-17 16:41:54 +00:00
guint32_t magic_number;
guint16_t version_major;
guint16_t version_minor;
gint32_t thiszone;
guint32_t sigfigs;
guint32_t snaplen;
guint32_t network;
} __packed pcap_hdr_s;
2016-01-17 16:41:54 +00:00
typedef struct pcaprec_hdr_s {
2016-01-17 16:41:54 +00:00
guint32_t ts_sec;
guint32_t ts_usec;
guint32_t incl_len;
guint32_t orig_len;
} __packed pcaprec_hdr_s;
2016-01-17 16:41:54 +00:00
#define LINKTYPE_RAW 101
// DNS
2016-01-29 18:10:23 +00:00
#define DNS_QCLASS_IN 1
#define DNS_QTYPE_A 1 // IPv4
#define DNS_QTYPE_AAAA 28 // IPv6
#define DNS_QNAME_MAX 255
#define DNS_TTL (10 * 60) // seconds
struct dns_header {
uint16_t id; // identification number
# if __BYTE_ORDER == __LITTLE_ENDIAN
uint16_t rd :1; // recursion desired
uint16_t tc :1; // truncated message
uint16_t aa :1; // authoritive answer
uint16_t opcode :4; // purpose of message
uint16_t qr :1; // query/response flag
uint16_t rcode :4; // response code
uint16_t cd :1; // checking disabled
uint16_t ad :1; // authenticated data
uint16_t z :1; // its z! reserved
uint16_t ra :1; // recursion available
#elif __BYTE_ORDER == __BIG_ENDIAN
uint16_t qr :1; // query/response flag
uint16_t opcode :4; // purpose of message
uint16_t aa :1; // authoritive answer
uint16_t tc :1; // truncated message
uint16_t rd :1; // recursion desired
uint16_t ra :1; // recursion available
uint16_t z :1; // its z! reserved
uint16_t ad :1; // authenticated data
uint16_t cd :1; // checking disabled
uint16_t rcode :4; // response code
# else
# error "Adjust your <bits/endian.h> defines"
#endif
uint16_t q_count; // number of question entries
uint16_t ans_count; // number of answer entries
uint16_t auth_count; // number of authority entries
uint16_t add_count; // number of resource entries
} __packed;
2016-01-23 08:39:21 +00:00
2016-01-25 14:21:21 +00:00
typedef struct dns_rr {
2016-01-23 14:50:18 +00:00
__be16 qname_ptr;
__be16 qtype;
__be16 qclass;
__be32 ttl;
__be16 rdlength;
} __packed dns_rr;
2016-01-23 14:50:18 +00:00
// DHCP
#define DHCP_OPTION_MAGIC_NUMBER (0x63825363)
typedef struct dhcp_packet {
uint8_t opcode;
uint8_t htype;
uint8_t hlen;
uint8_t hops;
uint32_t xid;
uint16_t secs;
uint16_t flags;
uint32_t ciaddr;
uint32_t yiaddr;
uint32_t siaddr;
uint32_t giaddr;
uint8_t chaddr[16];
uint8_t sname[64];
uint8_t file[128];
uint32_t option_format;
} __packed dhcp_packet;
2016-01-23 14:50:18 +00:00
typedef struct dhcp_option {
uint8_t code;
uint8_t length;
} __packed dhcp_option;
// Prototypes
void handle_signal(int sig, siginfo_t *info, void *context);
2016-01-18 14:29:01 +00:00
2016-01-23 19:46:27 +00:00
void *handle_events(void *a);
2016-01-18 11:19:40 +00:00
2016-01-25 08:27:12 +00:00
void report_exit(const struct arguments *args, const char *fmt, ...);
2016-01-21 11:55:08 +00:00
void report_error(const struct arguments *args, jint error, const char *fmt, ...);
2016-01-28 10:58:39 +00:00
void check_allowed(const struct arguments *args);
2017-11-09 18:07:08 +00:00
void clear(struct context *ctx);
2016-06-24 13:21:04 +00:00
2016-06-24 14:12:05 +00:00
int check_icmp_session(const struct arguments *args,
struct ng_session *s,
int sessions, int maxsessions);
2016-02-09 18:55:28 +00:00
2016-06-24 14:12:05 +00:00
int check_udp_session(const struct arguments *args,
struct ng_session *s,
int sessions, int maxsessions);
2016-02-09 18:55:28 +00:00
2016-06-24 14:12:05 +00:00
int check_tcp_session(const struct arguments *args,
struct ng_session *s,
int sessions, int maxsessions);
2016-01-21 11:04:41 +00:00
2016-06-25 09:03:42 +00:00
int monitor_tcp_session(const struct arguments *args, struct ng_session *s, int epoll_fd);
int get_icmp_timeout(const struct icmp_session *u, int sessions, int maxsessions);
int get_udp_timeout(const struct udp_session *u, int sessions, int maxsessions);
int get_tcp_timeout(const struct tcp_session *t, int sessions, int maxsessions);
2016-03-11 07:08:54 +00:00
uint16_t get_mtu();
uint16_t get_default_mss(int version);
int check_tun(const struct arguments *args,
2016-06-24 13:21:04 +00:00
const struct epoll_event *ev,
const int epoll_fd,
int sessions, int maxsessions);
2016-01-18 11:19:40 +00:00
2016-06-24 13:21:04 +00:00
void check_icmp_socket(const struct arguments *args, const struct epoll_event *ev);
2016-02-02 13:31:44 +00:00
2016-06-24 13:21:04 +00:00
void check_udp_socket(const struct arguments *args, const struct epoll_event *ev);
2016-01-18 14:29:01 +00:00
2016-01-31 15:38:38 +00:00
int32_t get_qname(const uint8_t *data, const size_t datalen, uint16_t off, char *qname);
2016-01-29 18:10:23 +00:00
2019-04-18 18:41:35 +00:00
void parse_dns_response(const struct arguments *args, const struct ng_session *session,
2017-03-04 08:26:46 +00:00
const uint8_t *data, size_t *datalen);
2016-01-29 18:10:23 +00:00
2016-03-03 12:49:52 +00:00
uint32_t get_send_window(const struct tcp_session *cur);
2019-05-06 14:16:25 +00:00
uint32_t get_receive_buffer(const struct ng_session *cur);
2016-03-03 12:49:52 +00:00
2016-06-24 13:21:04 +00:00
uint32_t get_receive_window(const struct ng_session *cur);
2016-02-15 11:50:03 +00:00
2016-06-24 13:21:04 +00:00
void check_tcp_socket(const struct arguments *args,
const struct epoll_event *ev,
const int epoll_fd);
2016-01-18 14:29:01 +00:00
int is_lower_layer(int protocol);
int is_upper_layer(int protocol);
void handle_ip(const struct arguments *args,
const uint8_t *buffer, size_t length,
2016-06-24 13:21:04 +00:00
const int epoll_fd,
int sessions, int maxsessions);
2016-01-20 08:24:34 +00:00
2016-02-02 13:31:44 +00:00
jboolean handle_icmp(const struct arguments *args,
2016-02-04 09:09:31 +00:00
const uint8_t *pkt, size_t length,
const uint8_t *payload,
2016-06-24 13:21:04 +00:00
int uid,
const int epoll_fd);
2016-02-10 18:28:40 +00:00
int has_udp_session(const struct arguments *args, const uint8_t *pkt, const uint8_t *payload);
2016-02-04 09:09:31 +00:00
void block_udp(const struct arguments *args,
const uint8_t *pkt, size_t length,
const uint8_t *payload,
int uid);
2016-01-28 11:46:22 +00:00
jboolean handle_udp(const struct arguments *args,
const uint8_t *pkt, size_t length,
const uint8_t *payload,
2016-06-24 13:21:04 +00:00
int uid, struct allowed *redirect,
const int epoll_fd);
int check_dhcp(const struct arguments *args, const struct udp_session *u,
const uint8_t *data, const size_t datalen);
2016-02-10 18:28:40 +00:00
void clear_tcp_data(struct tcp_session *cur);
jboolean handle_tcp(const struct arguments *args,
const uint8_t *pkt, size_t length,
const uint8_t *payload,
2017-03-04 10:48:46 +00:00
int uid, int allowed, struct allowed *redirect,
2016-06-24 13:21:04 +00:00
const int epoll_fd);
2016-02-09 17:59:20 +00:00
void queue_tcp(const struct arguments *args,
const struct tcphdr *tcphdr,
const char *session, struct tcp_session *cur,
const uint8_t *data, uint16_t datalen);
2016-02-07 19:15:40 +00:00
2016-02-03 17:32:25 +00:00
int open_icmp_socket(const struct arguments *args, const struct icmp_session *cur);
2016-03-08 07:57:04 +00:00
int open_udp_socket(const struct arguments *args,
const struct udp_session *cur, const struct allowed *redirect);
2016-01-25 12:28:52 +00:00
int open_tcp_socket(const struct arguments *args,
const struct tcp_session *cur, const struct allowed *redirect);
2016-01-18 14:29:01 +00:00
2016-01-23 19:46:27 +00:00
int32_t get_local_port(const int sock);
2016-01-17 16:41:54 +00:00
2016-01-24 11:50:40 +00:00
int write_syn_ack(const struct arguments *args, struct tcp_session *cur);
2016-01-17 16:41:54 +00:00
2016-02-10 09:46:00 +00:00
int write_ack(const struct arguments *args, struct tcp_session *cur);
2016-01-17 16:41:54 +00:00
int write_data(const struct arguments *args, struct tcp_session *cur,
2016-01-24 11:50:40 +00:00
const uint8_t *buffer, size_t length);
2016-01-17 16:41:54 +00:00
2016-02-05 11:03:44 +00:00
int write_fin_ack(const struct arguments *args, struct tcp_session *cur);
2016-01-17 16:41:54 +00:00
2016-01-24 11:50:40 +00:00
void write_rst(const struct arguments *args, struct tcp_session *cur);
2016-01-17 16:41:54 +00:00
2017-03-04 10:48:46 +00:00
void write_rst_ack(const struct arguments *args, struct tcp_session *cur);
2016-02-02 13:31:44 +00:00
ssize_t write_icmp(const struct arguments *args, const struct icmp_session *cur,
uint8_t *data, size_t datalen);
2016-01-23 19:46:27 +00:00
ssize_t write_udp(const struct arguments *args, const struct udp_session *cur,
2016-01-24 11:50:40 +00:00
uint8_t *data, size_t datalen);
2016-01-23 19:46:27 +00:00
ssize_t write_tcp(const struct arguments *args, const struct tcp_session *cur,
2016-02-10 09:46:00 +00:00
const uint8_t *data, size_t datalen,
2016-01-24 11:50:40 +00:00
int syn, int ack, int fin, int rst);
2016-01-20 13:11:04 +00:00
2016-01-23 19:46:27 +00:00
uint8_t char2nible(const char c);
void hex2bytes(const char *hex, uint8_t *buffer);
2016-01-17 16:41:54 +00:00
jint get_uid(const int version, const int protocol,
const void *saddr, const uint16_t sport,
2017-03-26 07:02:29 +00:00
const void *daddr, const uint16_t dport);
2016-01-17 16:41:54 +00:00
2017-04-10 08:55:00 +00:00
jint get_uid_sub(const int version, const int protocol,
const void *saddr, const uint16_t sport,
2017-11-05 07:08:20 +00:00
const void *daddr, const uint16_t dport,
const char *source, const char *dest,
long now);
2017-04-10 08:55:00 +00:00
2016-01-23 19:46:27 +00:00
int protect_socket(const struct arguments *args, int socket);
uint16_t calc_checksum(uint16_t start, const uint8_t *buffer, size_t length);
jobject jniGlobalRef(JNIEnv *env, jobject cls);
jclass jniFindClass(JNIEnv *env, const char *name);
jmethodID jniGetMethodID(JNIEnv *env, jclass cls, const char *name, const char *signature);
jfieldID jniGetFieldID(JNIEnv *env, jclass cls, const char *name, const char *type);
jobject jniNewObject(JNIEnv *env, jclass cls, jmethodID constructor, const char *name);
int jniCheckException(JNIEnv *env);
int sdk_int(JNIEnv *env);
void log_android(int prio, const char *fmt, ...);
2016-01-17 16:41:54 +00:00
2016-01-28 10:58:39 +00:00
void log_packet(const struct arguments *args, jobject jpacket);
2016-01-30 08:51:41 +00:00
void dns_resolved(const struct arguments *args,
const char *qname, const char *aname, const char *resource, int ttl);
2016-01-28 10:58:39 +00:00
jboolean is_domain_blocked(const struct arguments *args, const char *name);
2019-06-06 09:58:22 +00:00
jint get_uid_q(const struct arguments *args,
jint version,
jint protocol,
const char *source,
jint sport,
const char *dest,
jint dport);
struct allowed *is_address_allowed(const struct arguments *args, jobject objPacket);
2016-01-28 10:58:39 +00:00
jobject create_packet(const struct arguments *args,
jint version,
jint protocol,
const char *flags,
const char *source,
jint sport,
const char *dest,
jint dport,
const char *data,
jint uid,
jboolean allowed);
2016-02-15 16:48:53 +00:00
void account_usage(const struct arguments *args, jint version, jint protocol,
const char *daddr, jint dport, jint uid, jlong sent, jlong received);
2016-01-18 20:37:51 +00:00
void write_pcap_hdr();
2016-01-23 19:46:27 +00:00
void write_pcap_rec(const uint8_t *buffer, size_t len);
2016-01-18 20:37:51 +00:00
2016-01-22 12:06:50 +00:00
void write_pcap(const void *ptr, size_t len);
2016-02-15 11:50:03 +00:00
int compare_u32(uint32_t seq1, uint32_t seq2);
2016-01-17 16:41:54 +00:00
const char *strstate(const int state);
2016-01-23 19:46:27 +00:00
char *hex(const u_int8_t *data, const size_t len);
2016-06-25 07:28:34 +00:00
int is_readable(int fd);
2016-06-25 09:03:42 +00:00
2016-06-26 07:20:19 +00:00
int is_writable(int fd);
long long get_ms();