2016-01-18 11:19:40 +00:00
|
|
|
#include <jni.h>
|
|
|
|
|
2016-01-17 16:41:54 +00:00
|
|
|
#define TAG "NetGuard.JNI"
|
2016-01-21 12:46:57 +00:00
|
|
|
|
2016-01-18 19:57:49 +00:00
|
|
|
#define SELECT_TIMEOUT 10 // seconds
|
2016-01-21 12:46:57 +00:00
|
|
|
|
2016-01-22 18:03:32 +00:00
|
|
|
#define TUN_MAXMSG 32768 // bytes (device)
|
|
|
|
#define UDP4_MAXMSG 65507 // bytes (socket)
|
|
|
|
|
2016-01-20 13:11:04 +00:00
|
|
|
#define UDP_TTL 64
|
2016-01-22 18:03:32 +00:00
|
|
|
#define UDP_TIMEOUT 300 // seconds
|
2016-01-21 12:46:57 +00:00
|
|
|
|
2016-01-22 18:03:32 +00:00
|
|
|
#define TCP_TTL 64
|
|
|
|
#define TCP_RECV_WINDOW 2048 // bytes
|
|
|
|
#define TCP_SEND_WINDOW 2048 // bytes (maximum)
|
2016-01-18 19:57:49 +00:00
|
|
|
#define TCP_INIT_TIMEOUT 30 // seconds ~net.inet.tcp.keepinit
|
|
|
|
#define TCP_IDLE_TIMEOUT 300 // seconds ~net.inet.tcp.keepidle
|
2016-01-21 11:04:41 +00:00
|
|
|
#define TCP_CLOSE_TIMEOUT 30 // seconds
|
|
|
|
#define TCP_KEEP_TIMEOUT 300 // seconds
|
2016-01-21 12:46:57 +00:00
|
|
|
|
2016-01-20 09:27:18 +00:00
|
|
|
#define UID_DELAY 1 // milliseconds
|
|
|
|
#define UID_DELAYTRY 10 // milliseconds
|
2016-01-18 19:57:49 +00:00
|
|
|
#define UID_MAXTRY 3
|
2016-01-21 12:46:57 +00:00
|
|
|
|
2016-01-22 12:06:50 +00:00
|
|
|
#define MAX_PCAP_FILE (1024 * 1024) // bytes
|
2016-01-23 14:50:18 +00:00
|
|
|
#define MAX_PCAP_RECORD 128 // bytes
|
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;
|
2016-01-23 11:48:17 +00:00
|
|
|
jint ucount;
|
2016-01-22 15:13:33 +00:00
|
|
|
jint *uids;
|
2016-01-23 11:48:17 +00:00
|
|
|
int hcount;
|
|
|
|
char **hosts;
|
2016-01-19 19:58:51 +00:00
|
|
|
jboolean log;
|
|
|
|
jboolean filter;
|
2016-01-17 16:41:54 +00:00
|
|
|
};
|
|
|
|
|
2016-01-20 13:11:04 +00:00
|
|
|
struct udp_session {
|
|
|
|
// TODO UDPv6
|
|
|
|
time_t time;
|
|
|
|
jint uid;
|
2016-01-20 15:52:38 +00:00
|
|
|
int version;
|
2016-01-23 08:39:21 +00:00
|
|
|
__be32 saddr; // network notation
|
2016-01-20 13:11:04 +00:00
|
|
|
__be16 source; // network notation
|
2016-01-23 08:39:21 +00:00
|
|
|
__be32 daddr; // network notation
|
2016-01-20 13:11:04 +00:00
|
|
|
__be16 dest; // network notation
|
2016-01-21 12:43:41 +00:00
|
|
|
uint8_t error;
|
2016-01-20 13:11:04 +00:00
|
|
|
jint socket;
|
|
|
|
struct udp_session *next;
|
|
|
|
};
|
|
|
|
|
2016-01-20 11:35:51 +00:00
|
|
|
struct tcp_session {
|
2016-01-18 19:57:49 +00:00
|
|
|
// TODO TCPv6
|
2016-01-19 19:58:51 +00:00
|
|
|
jint uid;
|
2016-01-22 18:03:32 +00:00
|
|
|
time_t time;
|
2016-01-20 15:52:38 +00:00
|
|
|
int version;
|
2016-01-22 18:03:32 +00:00
|
|
|
uint16_t send_window; // host notation
|
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-01-23 08:39:21 +00:00
|
|
|
__be32 saddr; // network notation
|
2016-01-17 16:41:54 +00:00
|
|
|
__be16 source; // network notation
|
2016-01-23 08:39:21 +00:00
|
|
|
__be32 daddr; // network notation
|
2016-01-17 16:41:54 +00:00
|
|
|
__be16 dest; // network notation
|
|
|
|
uint8_t state;
|
|
|
|
jint socket;
|
2016-01-20 11:35:51 +00:00
|
|
|
struct tcp_session *next;
|
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
|
|
|
|
2016-01-23 14:50:18 +00:00
|
|
|
typedef struct __attribute__((__packed__)) 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;
|
|
|
|
} pcap_hdr_t;
|
|
|
|
|
2016-01-23 14:50:18 +00:00
|
|
|
typedef struct __attribute__((__packed__)) 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;
|
|
|
|
} pcaprec_hdr_t;
|
|
|
|
|
|
|
|
#define LINKTYPE_RAW 101
|
|
|
|
|
2016-01-23 14:50:18 +00:00
|
|
|
typedef struct __attribute__((__packed__)) dns_header {
|
2016-01-23 08:39:21 +00:00
|
|
|
__be16 msgid;
|
|
|
|
__be16 flags;
|
|
|
|
__be16 qdcount;
|
|
|
|
__be16 ancount;
|
|
|
|
__be16 nscount;
|
|
|
|
__be16 arcount;
|
|
|
|
} dns_header_t;
|
|
|
|
|
2016-01-23 14:50:18 +00:00
|
|
|
typedef struct __attribute__((__packed__)) dns_response {
|
|
|
|
__be16 qname_ptr;
|
|
|
|
__be16 qtype;
|
|
|
|
__be16 qclass;
|
|
|
|
__be32 ttl;
|
|
|
|
__be16 rdlength;
|
|
|
|
__be32 rdata;
|
|
|
|
} dns_response_t;
|
|
|
|
|
2016-01-23 08:39:21 +00:00
|
|
|
#define DNS_QR 1
|
|
|
|
#define DNS_OPCODE 30
|
|
|
|
#define DNS_TC 8
|
|
|
|
|
|
|
|
#define DNS_QTYPE_A 1 // IPv4
|
|
|
|
#define DNS_QTYPE_AAAA 28 // IPv6
|
|
|
|
#define DNS_QCLASS_IN 1
|
|
|
|
|
2016-01-23 14:50:18 +00:00
|
|
|
#define DNS_TTL 3600 // seconds
|
|
|
|
|
2016-01-20 08:24:34 +00:00
|
|
|
void clear_sessions();
|
|
|
|
|
2016-01-18 18:37:52 +00:00
|
|
|
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-21 11:55:08 +00:00
|
|
|
void report_exit(struct arguments *args);
|
|
|
|
|
2016-01-21 11:04:41 +00:00
|
|
|
void check_sessions(const struct arguments *args);
|
|
|
|
|
2016-01-20 13:11:04 +00:00
|
|
|
int get_selects(const struct arguments *args, fd_set *rfds, fd_set *wfds, fd_set *efds);
|
2016-01-20 08:24:34 +00:00
|
|
|
|
2016-01-20 13:11:04 +00:00
|
|
|
int check_tun(const struct arguments *args, fd_set *rfds, fd_set *wfds, fd_set *efds);
|
2016-01-18 11:19:40 +00:00
|
|
|
|
2016-01-20 13:11:04 +00:00
|
|
|
void check_udp_sockets(const struct arguments *args, fd_set *rfds, fd_set *wfds, fd_set *efds);
|
2016-01-18 14:29:01 +00:00
|
|
|
|
2016-01-20 13:11:04 +00:00
|
|
|
void check_tcp_sockets(const struct arguments *args, fd_set *rfds, fd_set *wfds, fd_set *efds);
|
2016-01-18 14:29:01 +00:00
|
|
|
|
2016-01-23 19:46:27 +00:00
|
|
|
void handle_ip(const struct arguments *args, const uint8_t *buffer, size_t length);
|
2016-01-20 08:24:34 +00:00
|
|
|
|
2016-01-23 19:46:27 +00:00
|
|
|
jboolean handle_udp(const struct arguments *args, const uint8_t *buffer, size_t length, int uid);
|
2016-01-18 14:29:01 +00:00
|
|
|
|
2016-01-23 19:46:27 +00:00
|
|
|
jboolean handle_tcp(const struct arguments *args, const uint8_t *buffer, size_t length, int uid);
|
2016-01-18 14:29:01 +00:00
|
|
|
|
2016-01-23 14:50:18 +00:00
|
|
|
int check_dns(const struct arguments *args, const struct udp_session *u,
|
2016-01-23 19:46:27 +00:00
|
|
|
const uint8_t *buffer, const size_t length);
|
2016-01-23 14:50:18 +00:00
|
|
|
|
2016-01-21 12:21:13 +00:00
|
|
|
int open_socket(const struct tcp_session *cur, const struct arguments *args);
|
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-20 15:52:38 +00:00
|
|
|
int write_syn_ack(const struct arguments *args, struct tcp_session *cur, int tun);
|
2016-01-17 16:41:54 +00:00
|
|
|
|
2016-01-23 19:46:27 +00:00
|
|
|
int write_ack(const struct arguments *args, struct tcp_session *cur, size_t bytes, int tun);
|
2016-01-17 16:41:54 +00:00
|
|
|
|
2016-01-20 15:52:38 +00:00
|
|
|
int write_data(const struct arguments *args, struct tcp_session *cur,
|
2016-01-23 19:46:27 +00:00
|
|
|
const uint8_t *buffer, size_t length, int tun);
|
2016-01-17 16:41:54 +00:00
|
|
|
|
2016-01-23 19:46:27 +00:00
|
|
|
int write_fin_ack(const struct arguments *args, struct tcp_session *cur, size_t bytes, int tun);
|
2016-01-17 16:41:54 +00:00
|
|
|
|
2016-01-20 15:52:38 +00:00
|
|
|
void write_rst(const struct arguments *args, struct tcp_session *cur, int tun);
|
2016-01-17 16:41:54 +00:00
|
|
|
|
2016-01-23 19:46:27 +00:00
|
|
|
ssize_t write_udp(const struct arguments *args, const struct udp_session *cur,
|
|
|
|
uint8_t *data, size_t datalen, int tun);
|
|
|
|
|
|
|
|
ssize_t write_tcp(const struct arguments *args, const struct tcp_session *cur,
|
|
|
|
const uint8_t *data, size_t datalen, size_t confirm,
|
|
|
|
int syn, int ack, int fin, int rst, int tun);
|
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
|
|
|
|
2016-01-19 08:49:47 +00:00
|
|
|
jint get_uid(const int protocol, const int version,
|
|
|
|
const void *saddr, const uint16_t sport, int dump);
|
2016-01-17 16:41:54 +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);
|
2016-01-18 18:37:52 +00:00
|
|
|
|
2016-01-22 09:37:57 +00:00
|
|
|
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);
|
|
|
|
|
2016-01-18 18:37:52 +00:00
|
|
|
void log_android(int prio, const char *fmt, ...);
|
2016-01-17 16:41:54 +00:00
|
|
|
|
2016-01-20 15:52:38 +00:00
|
|
|
void log_packet(const struct arguments *args,
|
|
|
|
jint version,
|
|
|
|
jint protocol,
|
|
|
|
const char *flags,
|
2016-01-22 09:37:57 +00:00
|
|
|
const char *source,
|
|
|
|
jint sport,
|
|
|
|
const char *dest,
|
|
|
|
jint dport,
|
2016-01-20 15:52:38 +00:00
|
|
|
jint uid,
|
|
|
|
jboolean allowed);
|
2016-01-18 18:37:52 +00:00
|
|
|
|
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-01-23 11:48:17 +00:00
|
|
|
void read_hosts(const char *name, struct arguments *args);
|
|
|
|
|
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);
|