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

389 lines
11 KiB
C
Raw Normal View History

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
#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)
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
2016-02-04 09:09:31 +00:00
#define UDP_KEEP_TIMEOUT 60 // seconds
2016-01-21 12:46:57 +00:00
2016-01-22 18:03:32 +00:00
#define TCP_RECV_WINDOW 2048 // bytes
#define TCP_SEND_WINDOW 2048 // bytes (maximum)
#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
#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
2016-02-02 14:11:09 +00:00
#define RTLD_NOLOAD 4
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-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;
jint socket;
struct icmp_session *next;
};
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;
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
jint socket;
2016-01-20 13:11:04 +00:00
struct udp_session *next;
};
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-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;
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;
jint socket;
2016-01-20 11:35:51 +00:00
struct tcp_session *next;
2016-01-17 16:41:54 +00:00
};
// 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;
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;
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 63
#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;
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;
2016-01-23 14:50:18 +00:00
typedef struct dhcp_option {
uint8_t code;
uint8_t length;
} __packed;
// Prototypes
2016-01-20 08:24:34 +00:00
void clear_sessions();
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
2016-01-28 10:58:39 +00:00
void check_allowed(const struct arguments *args);
2016-02-02 17:15:20 +00:00
int check_sessions(const struct arguments *args);
2016-01-21 11:04:41 +00:00
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-02-02 13:31:44 +00:00
void check_icmp_sockets(const struct arguments *args, fd_set *rfds, fd_set *wfds, fd_set *efds);
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-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
void parse_dns_response(const struct arguments *args, const uint8_t *data, const size_t datalen);
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
int is_lower_layer(int protocol);
int is_upper_layer(int protocol);
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-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-02-02 13:31:44 +00:00
int uid);
2016-02-04 09:09:31 +00:00
int has_udp_session(const uint8_t *pkt, const uint8_t *payload);
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,
int uid);
2016-01-29 06:59:35 +00:00
int get_dns_query(const struct arguments *args, const struct udp_session *u,
const uint8_t *data, const size_t datalen,
2016-01-29 18:10:23 +00:00
uint16_t *qtype, uint16_t *qclass, char *qname);
2016-01-18 14:29:01 +00:00
2016-01-26 17:53:17 +00:00
int check_domain(const struct arguments *args, const struct udp_session *u,
const uint8_t *data, const size_t datalen,
uint16_t qclass, uint16_t qtype, const char *name);
2016-01-23 14:50:18 +00:00
int check_dhcp(const struct arguments *args, const struct udp_session *u,
const uint8_t *data, const size_t datalen);
jboolean handle_tcp(const struct arguments *args,
const uint8_t *pkt, size_t length,
const uint8_t *payload,
2016-01-28 10:58:39 +00:00
int uid);
2016-02-03 17:32:25 +00:00
int open_icmp_socket(const struct arguments *args, const struct icmp_session *cur);
2016-01-25 12:28:52 +00:00
int open_udp_socket(const struct arguments *args, const struct udp_session *cur);
int open_tcp_socket(const struct arguments *args, const struct tcp_session *cur);
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-01-24 11:50:40 +00:00
int write_ack(const struct arguments *args, struct tcp_session *cur, size_t bytes);
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-01-24 11:50:40 +00:00
int write_fin_ack(const struct arguments *args, struct tcp_session *cur, size_t bytes);
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
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,
const uint8_t *data, size_t datalen, size_t confirm,
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
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);
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);
int __system_property_get(JNIEnv *env, const char *name, char *value);
2016-01-25 12:58:44 +00:00
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);
jboolean 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-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-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);