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

1447 lines
48 KiB
C
Raw Normal View History

2016-01-18 21:22:54 +00:00
/*
This file is part of NetGuard.
NetGuard is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
NetGuard is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with NetGuard. If not, see <http://www.gnu.org/licenses/>.
Copyright 2015-2016 by Marcel Bokhorst (M66B)
*/
2016-01-09 11:10:11 +00:00
#include <jni.h>
2016-01-13 08:23:21 +00:00
#include <android/log.h>
2016-01-09 20:17:42 +00:00
#include <stdio.h>
2016-01-15 09:08:18 +00:00
#include <stdlib.h>
2016-01-10 07:14:47 +00:00
#include <time.h>
2016-01-09 15:56:23 +00:00
#include <unistd.h>
2016-01-13 08:23:21 +00:00
#include <pthread.h>
2016-01-09 15:56:23 +00:00
#include <arpa/inet.h>
2016-01-09 11:10:11 +00:00
#include <netinet/ip.h>
#include <netinet/ip6.h>
2016-01-09 15:56:23 +00:00
#include <netinet/udp.h>
#include <netinet/tcp.h>
2016-01-09 11:10:11 +00:00
2016-01-17 16:41:54 +00:00
#include "netguard.h"
#define PROFILE 1
2016-01-12 20:15:25 +00:00
// TODO TCP fragmentation
// TODO TCP push
2016-01-17 13:20:07 +00:00
// TODO TCPv6
// TODO UDPv4
// TODO UDPv6
// TODO DHCP
2016-01-15 09:28:31 +00:00
// TODO log allowed traffic
2016-01-15 11:57:32 +00:00
// TODO fix warnings
2016-01-18 19:28:33 +00:00
// TODO non blocking send/write, handle EAGAIN/EWOULDBLOCK
2016-01-15 11:57:32 +00:00
// Window size < 2^31: x <= y: (uint32_t)(y-x) < 0x80000000
2016-01-16 08:07:04 +00:00
// It is assumed that no packets will get lost and that packets arrive in order
2016-01-12 20:15:25 +00:00
// Global variables
2016-01-12 12:15:21 +00:00
2016-01-13 08:23:21 +00:00
static JavaVM *jvm;
pthread_t thread_id;
2016-01-14 06:46:23 +00:00
int signaled = 0;
2016-01-16 08:07:04 +00:00
struct session *session = NULL;
2016-01-17 09:42:21 +00:00
char *pcap_fn = NULL;
2016-01-18 12:07:00 +00:00
int loglevel = ANDROID_LOG_WARN;
int native = 0;
2016-01-11 22:06:35 +00:00
2016-01-13 08:23:21 +00:00
// JNI
2016-01-11 22:06:35 +00:00
2016-01-14 14:02:32 +00:00
JNIEXPORT void JNICALL
Java_eu_faircode_netguard_SinkholeService_jni_1init(JNIEnv *env) {
session = NULL;
pcap_fn = NULL;
2016-01-17 09:42:21 +00:00
}
2016-01-09 11:10:11 +00:00
JNIEXPORT void JNICALL
2016-01-17 05:35:26 +00:00
Java_eu_faircode_netguard_SinkholeService_jni_1start(JNIEnv *env, jobject instance,
2016-01-18 12:07:00 +00:00
jint tun, jint loglevel_, jboolean native_) {
loglevel = loglevel_;
native = native_;
log_android(ANDROID_LOG_INFO, "Starting tun=%d level %d native %d", tun, loglevel, native);
2016-01-18 20:54:45 +00:00
// Set blocking
uint8_t flags = fcntl(tun, F_GETFL, 0);
2016-01-18 20:54:45 +00:00
if (flags < 0 || fcntl(tun, F_SETFL, flags & ~O_NONBLOCK) < 0)
log_android(ANDROID_LOG_ERROR, "fcntl tun ~O_NONBLOCK error %d: %s", errno, strerror(errno));
2016-01-12 16:19:27 +00:00
2016-01-14 06:46:23 +00:00
if (pthread_kill(thread_id, 0) == 0)
log_android(ANDROID_LOG_WARN, "Already running thread %lu", thread_id);
2016-01-14 06:46:23 +00:00
else {
jint rs = (*env)->GetJavaVM(env, &jvm);
if (rs != JNI_OK)
log_android(ANDROID_LOG_ERROR, "GetJavaVM failed");
2016-01-14 06:46:23 +00:00
struct arguments *args = malloc(sizeof(struct arguments));
args->instance = (*env)->NewGlobalRef(env, instance);
args->tun = tun;
int err = pthread_create(&thread_id, NULL, handle_events, args);
2016-01-17 13:20:07 +00:00
if (err == 0)
log_android(ANDROID_LOG_INFO, "Started thread %lu", thread_id);
2016-01-17 13:20:07 +00:00
else
log_android(ANDROID_LOG_ERROR, "pthread_create error %d: %s", err, strerror(err));
2016-01-14 06:46:23 +00:00
}
2016-01-12 12:15:21 +00:00
}
2016-01-09 11:10:11 +00:00
JNIEXPORT void JNICALL
2016-01-17 05:35:26 +00:00
Java_eu_faircode_netguard_SinkholeService_jni_1stop(JNIEnv *env, jobject instance,
jint tun, jboolean clear) {
log_android(ANDROID_LOG_INFO, "Stop tun %d clear %d", tun, (int) clear);
2016-01-14 06:46:23 +00:00
if (pthread_kill(thread_id, 0) == 0) {
log_android(ANDROID_LOG_DEBUG, "Kill thread %lu", thread_id);
int err = pthread_kill(thread_id, SIGUSR1);
if (err != 0)
log_android(ANDROID_LOG_WARN, "pthread_kill error %d: %s", err, strerror(err));
2016-01-14 06:46:23 +00:00
else {
log_android(ANDROID_LOG_DEBUG, "Join thread %lu", thread_id);
2016-01-14 06:46:23 +00:00
pthread_join(thread_id, NULL);
if (err != 0)
log_android(ANDROID_LOG_WARN, "pthread_join error %d: %s", err, strerror(err));
2016-01-14 06:46:23 +00:00
}
2016-01-17 05:35:26 +00:00
if (clear) {
struct session *s = session;
while (s != NULL) {
struct session *p = s;
s = s->next;
free(p);
}
session = NULL;
}
log_android(ANDROID_LOG_INFO, "Stopped thread %lu", thread_id);
} else
log_android(ANDROID_LOG_WARN, "Not running");
2016-01-09 11:10:11 +00:00
}
2016-01-17 09:42:21 +00:00
JNIEXPORT void JNICALL
Java_eu_faircode_netguard_SinkholeService_jni_1done(JNIEnv *env, jobject instance) {
log_android(ANDROID_LOG_INFO, "Done");
struct session *s = session;
while (s != NULL) {
struct session *p = s;
s = s->next;
free(p);
}
session = NULL;
if (pcap_fn != NULL)
free(pcap_fn);
2016-01-17 09:42:21 +00:00
}
JNIEXPORT void JNICALL
Java_eu_faircode_netguard_SinkholeService_jni_1pcap(JNIEnv *env, jclass type, jstring name_) {
// TODO limit pcap file size
// TODO keep pcap file open
if (name_ == NULL) {
pcap_fn = NULL;
log_android(ANDROID_LOG_INFO, "PCAP disabled");
}
else {
const char *name = (*env)->GetStringUTFChars(env, name_, 0);
log_android(ANDROID_LOG_INFO, "PCAP file %s", name);
const char *tmp = malloc(strlen(name) + 1);
strcpy(tmp, name);
pcap_fn = tmp;
2016-01-18 20:37:51 +00:00
write_pcap_hdr();
(*env)->ReleaseStringUTFChars(env, name_, name);
}
}
2016-01-11 22:06:35 +00:00
// Private functions
void handle_signal(int sig, siginfo_t *info, void *context) {
log_android(ANDROID_LOG_DEBUG, "Signal %d", sig);
2016-01-14 06:46:23 +00:00
signaled = 1;
}
2016-01-18 11:19:40 +00:00
void handle_events(void *a) {
2016-01-13 08:23:21 +00:00
fd_set rfds;
fd_set wfds;
fd_set efds;
struct timespec ts;
sigset_t blockset;
sigset_t emptyset;
struct sigaction sa;
2016-01-18 11:19:40 +00:00
struct arguments *args = (struct arguments *) a;
log_android(ANDROID_LOG_INFO, "Start events tun=%d thread %lu", args->tun, thread_id);
2016-01-18 11:19:40 +00:00
// Attach to Java
JNIEnv *env;
jint rs = (*jvm)->AttachCurrentThread(jvm, &env, NULL);
if (rs != JNI_OK) {
log_android(ANDROID_LOG_ERROR, "AttachCurrentThread failed");
2016-01-18 11:19:40 +00:00
return;
}
args->env = env;
// Block SIGUSR1
sigemptyset(&blockset);
sigaddset(&blockset, SIGUSR1);
sigprocmask(SIG_BLOCK, &blockset, NULL);
/// Handle SIGUSR1
sa.sa_sigaction = handle_signal;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
sigaction(SIGUSR1, &sa, NULL);
2016-01-14 06:46:23 +00:00
signaled = 0;
// Loop
while (1) {
log_android(ANDROID_LOG_DEBUG, "Loop thread %lu", thread_id);
2016-01-13 08:23:21 +00:00
// Select
ts.tv_sec = SELECT_TIMEOUT;
ts.tv_nsec = 0;
sigemptyset(&emptyset);
2016-01-18 11:19:40 +00:00
int max = get_selects(args, &rfds, &wfds, &efds);
2016-01-16 18:31:44 +00:00
int ready = pselect(max + 1, &rfds, &wfds, &efds, session == NULL ? NULL : &ts, &emptyset);
2016-01-13 08:23:21 +00:00
if (ready < 0) {
if (errno == EINTR) {
2016-01-14 06:46:23 +00:00
if (signaled) { ;
log_android(ANDROID_LOG_DEBUG, "pselect signaled");
2016-01-14 06:46:23 +00:00
break;
} else {
log_android(ANDROID_LOG_WARN, "pselect interrupted");
2016-01-14 06:46:23 +00:00
continue;
}
} else {
log_android(ANDROID_LOG_ERROR, "pselect error %d: %s",
errno, strerror(errno));
2016-01-14 06:46:23 +00:00
break;
}
2016-01-13 08:23:21 +00:00
}
2016-01-18 11:19:40 +00:00
// Count sessions
int sessions = 0;
struct session *s = session;
while (s != NULL) {
sessions++;
s = s->next;
}
2016-01-13 08:23:21 +00:00
if (ready == 0)
log_android(ANDROID_LOG_DEBUG, "pselect timeout sessions %d", sessions);
2016-01-13 08:23:21 +00:00
else {
log_android(ANDROID_LOG_DEBUG, "pselect sessions %d ready %d", sessions, ready);
#ifdef PROFILE
struct timeval start, end;
float mselapsed;
gettimeofday(&start, NULL);
#endif
2016-01-18 11:19:40 +00:00
// Check upstream
check_tun(args, &rfds, &wfds, &efds);
2016-01-12 08:45:58 +00:00
#ifdef PROFILE
gettimeofday(&end, NULL);
mselapsed = (end.tv_sec - start.tv_sec) * 1000.0 +
(end.tv_usec - start.tv_usec) / 1000.0;
if (mselapsed > 1)
log_android(ANDROID_LOG_INFO, "tun %f", mselapsed);
gettimeofday(&start, NULL);
#endif
2016-01-18 11:19:40 +00:00
// Check downstream
check_sockets(args, &rfds, &wfds, &efds);
#ifdef PROFILE
gettimeofday(&end, NULL);
mselapsed = (end.tv_sec - start.tv_sec) * 1000.0 +
(end.tv_usec - start.tv_usec) / 1000.0;
if (mselapsed > 1)
log_android(ANDROID_LOG_INFO, "sockets %f", mselapsed);
2016-01-18 11:19:40 +00:00
}
#endif
2016-01-18 11:19:40 +00:00
}
2016-01-17 09:42:21 +00:00
2016-01-18 11:19:40 +00:00
(*env)->DeleteGlobalRef(env, args->instance);
rs = (*jvm)->DetachCurrentThread(jvm);
if (rs != JNI_OK)
log_android(ANDROID_LOG_ERROR, "DetachCurrentThread failed");
2016-01-18 11:19:40 +00:00
free(args);
log_android(ANDROID_LOG_INFO, "Stopped events tun=%d thread %lu", args->tun, thread_id);
2016-01-18 20:37:51 +00:00
// TODO report exit to Java
2016-01-18 11:19:40 +00:00
}
2016-01-17 09:42:21 +00:00
2016-01-18 11:19:40 +00:00
int get_selects(const struct arguments *args, fd_set *rfds, fd_set *wfds, fd_set *efds) {
time_t now = time(NULL);
2016-01-17 09:42:21 +00:00
2016-01-18 11:19:40 +00:00
// Select
FD_ZERO(rfds);
FD_ZERO(wfds);
FD_ZERO(efds);
2016-01-17 09:42:21 +00:00
2016-01-18 11:19:40 +00:00
// Always read tun
FD_SET(args->tun, rfds);
FD_SET(args->tun, efds);
int max = args->tun;
2016-01-17 09:42:21 +00:00
2016-01-18 11:19:40 +00:00
struct session *last = NULL;
struct session *cur = session;
while (cur != NULL) {
int timeout = 0;
if (cur->state == TCP_LISTEN || cur->state == TCP_SYN_RECV)
timeout = TCP_INIT_TIMEOUT;
else if (cur->state == TCP_ESTABLISHED)
timeout = TCP_IDLE_TIMEOUT;
else
timeout = TCP_CLOSE_TIMEOUT;
if (cur->state != TCP_TIME_WAIT && cur->time + timeout < now) {
2016-01-18 11:19:40 +00:00
// TODO send keep alives?
char dest[20];
inet_ntop(AF_INET, &(cur->daddr), dest, sizeof(dest));
2016-01-18 14:29:01 +00:00
log_android(ANDROID_LOG_WARN, "Idle %s/%u lport %u",
dest, ntohs(cur->dest), cur->lport);
2016-01-12 08:45:58 +00:00
2016-01-18 14:29:01 +00:00
write_rst(cur, args->tun);
2016-01-18 11:19:40 +00:00
}
if (cur->state == TCP_TIME_WAIT) {
// Log
char dest[20];
inet_ntop(AF_INET, &(cur->daddr), dest, sizeof(dest));
log_android(ANDROID_LOG_INFO, "Close %s/%u lport %u",
dest, ntohs(cur->dest), cur->lport);
2016-01-18 11:19:40 +00:00
// TODO keep for some time?
2016-01-18 11:19:40 +00:00
// TODO non blocking?
if (close(cur->socket))
log_android(ANDROID_LOG_ERROR, "close error %d: %s", errno, strerror(errno));
2016-01-18 11:19:40 +00:00
if (last == NULL)
session = cur->next;
else
last->next = cur->next;
struct session *c = cur;
cur = cur->next;
free(c);
continue;
2016-01-18 14:29:01 +00:00
} else if (cur->state == TCP_LISTEN) {
// Check for connected / errors
FD_SET(cur->socket, efds);
FD_SET(cur->socket, wfds);
if (cur->socket > max)
max = cur->socket;
}
else if (cur->state == TCP_ESTABLISHED ||
cur->state == TCP_SYN_RECV ||
cur->state == TCP_CLOSE_WAIT) {
// Check for data / errors
FD_SET(cur->socket, efds);
FD_SET(cur->socket, rfds);
if (cur->socket > max)
max = cur->socket;
2016-01-11 22:06:35 +00:00
}
2016-01-18 11:19:40 +00:00
last = cur;
cur = cur->next;
2016-01-11 22:06:35 +00:00
}
2016-01-18 14:29:01 +00:00
2016-01-18 11:19:40 +00:00
return max;
}
2016-01-12 16:19:27 +00:00
2016-01-18 11:19:40 +00:00
int check_tun(const struct arguments *args, fd_set *rfds, fd_set *wfds, fd_set *efds) {
2016-01-18 14:29:01 +00:00
// Check tun error
2016-01-18 11:19:40 +00:00
if (FD_ISSET(args->tun, efds)) {
log_android(ANDROID_LOG_ERROR, "tun exception");
2016-01-18 11:19:40 +00:00
return -1; // over and out
}
2016-01-13 08:23:21 +00:00
2016-01-18 11:19:40 +00:00
// Check tun read
if (FD_ISSET(args->tun, rfds)) {
uint8_t buffer[MAX_PKTSIZE];
ssize_t length = read(args->tun, buffer, sizeof(buffer));
2016-01-18 11:19:40 +00:00
if (length < 0) {
log_android(ANDROID_LOG_ERROR, "tun read error %d: %s", errno, strerror(errno));
2016-01-18 14:29:01 +00:00
return (errno == EINTR ? 0 : -1);
2016-01-18 11:19:40 +00:00
}
else if (length > 0) {
// Write pcap record
2016-01-18 20:37:51 +00:00
if (native && pcap_fn != NULL)
write_pcap_rec(buffer, length);
2016-01-18 11:19:40 +00:00
// Handle IP from tun
handle_ip(args, buffer, length);
}
else {
2016-01-18 14:29:01 +00:00
// tun eof
log_android(ANDROID_LOG_ERROR, "tun empty read");
2016-01-18 14:29:01 +00:00
return -1;
2016-01-18 11:19:40 +00:00
}
}
2016-01-18 14:29:01 +00:00
2016-01-18 11:19:40 +00:00
return 0;
2016-01-11 22:06:35 +00:00
}
2016-01-17 16:39:11 +00:00
void check_sockets(const struct arguments *args, fd_set *rfds, fd_set *wfds, fd_set *efds) {
struct session *cur = session;
while (cur != NULL) {
int oldstate = cur->state;
uint32_t oldlocal = cur->local_seq;
uint32_t oldremote = cur->remote_seq;
2016-01-17 16:39:11 +00:00
if (FD_ISSET(cur->socket, efds)) {
2016-01-18 14:29:01 +00:00
// Socket error
int serr = 0;
2016-01-17 16:39:11 +00:00
socklen_t optlen = sizeof(int);
int err = getsockopt(cur->socket, SOL_SOCKET, SO_ERROR, &serr, &optlen);
if (err < 0)
log_android(ANDROID_LOG_ERROR, "getsockopt lport %u error %d: %s",
cur->lport, errno, strerror(errno));
2016-01-18 14:29:01 +00:00
else if (serr)
log_android(ANDROID_LOG_ERROR, "lport %u SO_ERROR %d: %s",
cur->lport, serr, strerror(serr));
2016-01-17 19:40:40 +00:00
2016-01-18 14:29:01 +00:00
write_rst(cur, args->tun);
2016-01-17 16:39:11 +00:00
}
else {
// Assume socket okay
if (cur->state == TCP_LISTEN) {
// Check socket connect
if (FD_ISSET(cur->socket, wfds)) {
// Log
char dest[20];
inet_ntop(AF_INET, &(cur->daddr), dest, sizeof(dest));
log_android(ANDROID_LOG_INFO, "Connected %s/%u lport %u",
dest, ntohs(cur->dest), cur->lport);
2016-01-17 16:39:11 +00:00
2016-01-18 14:29:01 +00:00
if (write_syn_ack(cur, args->tun) >= 0) {
2016-01-17 16:39:11 +00:00
cur->local_seq++; // local SYN
cur->remote_seq++; // remote SYN
cur->state = TCP_SYN_RECV;
}
}
}
else if (cur->state == TCP_SYN_RECV ||
cur->state == TCP_ESTABLISHED ||
cur->state == TCP_CLOSE_WAIT) {
// Check socket read
if (FD_ISSET(cur->socket, rfds)) {
// TODO window size
uint8_t buffer[MAX_DATASIZE4];
ssize_t bytes = recv(cur->socket, buffer, sizeof(buffer), 0);
2016-01-17 16:39:11 +00:00
if (bytes < 0) {
// Socket error
log_android(ANDROID_LOG_ERROR, "recv lport %u error %d: %s",
cur->lport, errno, strerror(errno));
2016-01-17 19:40:40 +00:00
2016-01-18 14:29:01 +00:00
if (errno != EINTR)
write_rst(cur, args->tun);
2016-01-17 16:39:11 +00:00
}
else if (bytes == 0) {
2016-01-18 14:29:01 +00:00
// Socket eof
// TCP: application close
log_android(ANDROID_LOG_DEBUG, "recv empty lport %u state %s",
cur->lport, strstate(cur->state));
2016-01-17 16:39:11 +00:00
2016-01-18 14:29:01 +00:00
if (write_fin(cur, args->tun) >= 0) {
// Shutdown socket for reading
if (shutdown(cur->socket, SHUT_RD))
log_android(ANDROID_LOG_WARN, "shutdown RD error %d: %s",
errno, strerror(errno));
2016-01-18 14:29:01 +00:00
2016-01-17 16:39:11 +00:00
cur->local_seq++; // local FIN
2016-01-18 14:29:01 +00:00
2016-01-17 16:39:11 +00:00
if (cur->state == TCP_SYN_RECV || cur->state == TCP_ESTABLISHED)
cur->state = TCP_FIN_WAIT1;
2016-01-17 19:40:40 +00:00
else if (cur->state == TCP_CLOSE_WAIT)
2016-01-17 16:39:11 +00:00
cur->state = TCP_LAST_ACK;
2016-01-17 19:40:40 +00:00
else
log_android(ANDROID_LOG_ERROR, "Unknown state %s",
strstate(cur->state));
2016-01-18 14:29:01 +00:00
log_android(ANDROID_LOG_DEBUG, "Half close state %s",
strstate(cur->state));
2016-01-17 16:39:11 +00:00
}
} else {
2016-01-18 14:29:01 +00:00
// Socket read data
log_android(ANDROID_LOG_DEBUG,
"recv lport %u bytes %d state %s",
cur->lport, bytes, strstate(cur->state));
2016-01-17 19:40:40 +00:00
2016-01-18 14:29:01 +00:00
// Forward to tun
if (write_data(cur, buffer, bytes, args->tun) >= 0)
cur->local_seq += bytes;
2016-01-17 16:39:11 +00:00
}
}
}
}
if (cur->state != oldstate || cur->local_seq != oldlocal || cur->remote_seq != oldremote) {
char dest[20];
inet_ntop(AF_INET, &(cur->daddr), dest, sizeof(dest));
log_android(ANDROID_LOG_INFO,
"Session %s/%u lport %u new state %s local %u remote %u",
dest, ntohs(cur->dest), cur->lport, strstate(cur->state),
cur->local_seq - cur->local_start,
cur->remote_seq - cur->remote_start);
}
2016-01-17 16:39:11 +00:00
cur = cur->next;
}
}
2016-01-18 11:19:40 +00:00
void handle_ip(const struct arguments *args, const uint8_t *buffer, const uint16_t length) {
2016-01-15 18:51:07 +00:00
uint8_t protocol;
void *saddr;
void *daddr;
char source[40];
char dest[40];
char flags[10];
int flen = 0;
uint8_t *payload;
#ifdef PROFILE
float mselapsed;
struct timeval start, end;
gettimeofday(&start, NULL);
#endif
2016-01-15 18:51:07 +00:00
// Get protocol, addresses & payload
uint8_t version = (*buffer) >> 4;
if (version == 4) {
struct iphdr *ip4hdr = buffer;
protocol = ip4hdr->protocol;
saddr = &ip4hdr->saddr;
daddr = &ip4hdr->daddr;
2016-01-18 20:18:23 +00:00
if (ip4hdr->frag_off & IP_MF) {
log_android(ANDROID_LOG_ERROR, "IP fragment");
2016-01-15 18:51:07 +00:00
flags[flen++] = '+';
2016-01-18 20:18:23 +00:00
}
2016-01-15 18:51:07 +00:00
2016-01-18 20:18:23 +00:00
uint8_t ipoptlen = (ip4hdr->ihl - 5) * 4;
payload = buffer + sizeof(struct iphdr) + ipoptlen;
2016-01-15 18:51:07 +00:00
if (ntohs(ip4hdr->tot_len) != length) {
log_android(ANDROID_LOG_ERROR, "Invalid length %u header length %u",
length, ntohs(ip4hdr->tot_len));
2016-01-15 18:51:07 +00:00
return;
}
uint16_t csum = calc_checksum(ip4hdr, sizeof(struct iphdr));
2016-01-15 18:51:07 +00:00
if (csum != 0) {
log_android(ANDROID_LOG_ERROR, "Invalid IP checksum");
2016-01-15 18:51:07 +00:00
return;
}
}
else if (version == 6) {
struct ip6_hdr *ip6hdr = buffer;
protocol = ip6hdr->ip6_nxt;
saddr = &ip6hdr->ip6_src;
daddr = &ip6hdr->ip6_dst;
payload = buffer + 40;
// TODO check length
// TODO checksum
}
else {
log_android(ANDROID_LOG_WARN, "Unknown version %d", version);
2016-01-15 18:51:07 +00:00
return;
}
inet_ntop(version == 4 ? AF_INET : AF_INET6, saddr, source, sizeof(source));
inet_ntop(version == 4 ? AF_INET : AF_INET6, daddr, dest, sizeof(dest));
// Get ports & flags
int syn = 0;
uint16_t sport = -1;
uint16_t dport = -1;
if (protocol == IPPROTO_TCP) {
struct tcphdr *tcp = payload;
sport = ntohs(tcp->source);
dport = ntohs(tcp->dest);
if (tcp->syn) {
syn = 1;
flags[flen++] = 'S';
}
if (tcp->ack)
flags[flen++] = 'A';
if (tcp->psh)
flags[flen++] = 'P';
if (tcp->fin)
flags[flen++] = 'F';
2016-01-17 09:42:21 +00:00
if (tcp->rst)
2016-01-15 18:51:07 +00:00
flags[flen++] = 'R';
// TODO checksum
} else if (protocol == IPPROTO_UDP) {
struct udphdr *udp = payload;
sport = ntohs(udp->source);
dport = ntohs(udp->dest);
// TODO checksum
}
flags[flen] = 0;
// Get uid
jint uid = -1;
2016-01-18 19:05:58 +00:00
if ((protocol == IPPROTO_TCP && (syn || !native)) || protocol == IPPROTO_UDP) {
2016-01-18 20:37:51 +00:00
log_android(ANDROID_LOG_INFO, "get uid %s/%u syn %d", dest, dport, syn);
2016-01-18 19:05:58 +00:00
int tries = 0;
while (uid < 0 && tries++ < UID_MAXTRY) {
2016-01-18 19:05:58 +00:00
// Most likely in IPv6 table
int8_t saddr128[16];
memset(saddr128, 0, 10);
saddr128[10] = 0xFF;
saddr128[11] = 0xFF;
memcpy(saddr128 + 12, saddr, 4);
uid = get_uid(protocol, 6, saddr128, sport);
// Fallback to IPv4 table
2016-01-18 19:05:58 +00:00
if (uid < 0 && version == 4)
uid = get_uid(protocol, version, saddr, sport);
// Retry delay
if (uid < 0 && tries < UID_MAXTRY) {
2016-01-18 19:05:58 +00:00
log_android(ANDROID_LOG_WARN, "get uid try %d", tries);
usleep(1000 * UID_DELAY);
2016-01-18 19:05:58 +00:00
}
2016-01-15 18:51:07 +00:00
}
2016-01-18 19:05:58 +00:00
if (uid < 0)
log_android(ANDROID_LOG_WARN, "uid not found");
2016-01-15 18:51:07 +00:00
}
log_android(ANDROID_LOG_DEBUG,
"Packet v%d %s/%u -> %s/%u proto %d flags %s uid %d",
version, source, sport, dest, dport, protocol, flags, uid);
#ifdef PROFILE
gettimeofday(&end, NULL);
mselapsed = (end.tv_sec - start.tv_sec) * 1000.0 +
(end.tv_usec - start.tv_usec) / 1000.0;
if (mselapsed > 1)
log_android(ANDROID_LOG_INFO, "handle ip %f", mselapsed);
#endif
2016-01-15 18:51:07 +00:00
2016-01-18 12:07:00 +00:00
if (protocol == IPPROTO_TCP && native)
handle_tcp(args, buffer, length, uid);
2016-01-15 18:51:07 +00:00
if ((protocol == IPPROTO_TCP && (syn || !native)) || protocol == IPPROTO_UDP)
log_java(args, version, source, sport, dest, dport, protocol, flags, uid, 0);
2016-01-15 18:51:07 +00:00
}
2016-01-18 11:19:40 +00:00
void handle_tcp(const struct arguments *args, const uint8_t *buffer, uint16_t length, int uid) {
#ifdef PROFILE
float mselapsed;
struct timeval start, end;
gettimeofday(&start, NULL);
#endif
2016-01-12 08:45:58 +00:00
// Check version
2016-01-14 14:02:32 +00:00
uint8_t version = (*buffer) >> 4;
2016-01-12 08:45:58 +00:00
if (version != 4)
return;
2016-01-11 22:06:35 +00:00
// Get headers
2016-01-12 18:44:56 +00:00
struct iphdr *iphdr = buffer;
2016-01-18 20:18:23 +00:00
uint8_t ipoptlen = (iphdr->ihl - 5) * 4;
struct tcphdr *tcphdr = buffer + sizeof(struct iphdr) + ipoptlen;
uint8_t tcpoptlen = (tcphdr->doff - 5) * 4;
if (tcpoptlen) {
// TODO handle TCP options
log_android(ANDROID_LOG_INFO, "optlen %d", tcpoptlen);
}
2016-01-12 18:44:56 +00:00
// Get data
2016-01-18 20:18:23 +00:00
uint16_t dataoff = sizeof(struct iphdr) + ipoptlen + sizeof(struct tcphdr) + tcpoptlen;
uint16_t datalen = length - dataoff;
2016-01-11 22:06:35 +00:00
2016-01-16 08:07:04 +00:00
// Search session
struct session *last = NULL;
struct session *cur = session;
while (cur != NULL && !(cur->saddr == iphdr->saddr && cur->source == tcphdr->source &&
cur->daddr == iphdr->daddr && cur->dest == tcphdr->dest)) {
2016-01-11 22:06:35 +00:00
last = cur;
cur = cur->next;
}
// Log
char dest[20];
inet_ntop(AF_INET, &(iphdr->daddr), dest, sizeof(dest));
log_android(ANDROID_LOG_DEBUG, "Received %s/%u seq %u ack %u window %u data %d",
dest, ntohs(tcphdr->dest),
ntohl(tcphdr->seq) - (cur == NULL ? 0 : cur->remote_start),
ntohl(tcphdr->ack_seq) - (cur == NULL ? 0 : cur->local_start),
ntohs(tcphdr->window), datalen);
2016-01-11 22:06:35 +00:00
if (cur == NULL) {
if (tcphdr->syn) {
log_android(ANDROID_LOG_INFO, "New session %s/%u uid %d",
dest, ntohs(tcphdr->dest), uid);
2016-01-11 22:06:35 +00:00
2016-01-16 08:07:04 +00:00
// Register session
struct session *syn = malloc(sizeof(struct session));
2016-01-12 08:45:58 +00:00
syn->time = time(NULL);
2016-01-15 09:28:31 +00:00
syn->uid = uid;
2016-01-14 14:02:32 +00:00
syn->remote_seq = ntohl(tcphdr->seq); // ISN remote
2016-01-15 09:08:18 +00:00
syn->local_seq = rand(); // ISN local
2016-01-17 05:48:33 +00:00
syn->remote_start = syn->remote_seq;
syn->local_start = syn->local_seq;
2016-01-11 22:06:35 +00:00
syn->saddr = iphdr->saddr;
syn->source = tcphdr->source;
syn->daddr = iphdr->daddr;
syn->dest = tcphdr->dest;
2016-01-16 11:32:55 +00:00
syn->state = TCP_LISTEN;
2016-01-11 22:06:35 +00:00
syn->next = NULL;
2016-01-15 11:13:12 +00:00
// TODO handle SYN data?
2016-01-18 14:29:01 +00:00
if (datalen)
log_android(ANDROID_LOG_WARN, "SYN session %s/%u data %u",
dest, ntohs(tcphdr->dest), datalen);
2016-01-13 09:26:06 +00:00
// Open socket
2016-01-18 14:29:01 +00:00
syn->socket = open_socket(syn, args);
2016-01-15 07:48:18 +00:00
if (syn->socket < 0) {
2016-01-15 20:40:37 +00:00
syn->state = TCP_TIME_WAIT;
2016-01-18 14:29:01 +00:00
// Remote might retry
2016-01-15 07:48:18 +00:00
free(syn);
}
2016-01-11 22:06:35 +00:00
else {
2016-01-17 16:39:11 +00:00
syn->lport = get_local_port(syn->socket);
2016-01-11 22:06:35 +00:00
2016-01-15 07:48:18 +00:00
if (last == NULL)
2016-01-16 08:07:04 +00:00
session = syn;
2016-01-15 07:48:18 +00:00
else
last->next = syn;
2016-01-11 22:06:35 +00:00
}
}
2016-01-15 09:28:31 +00:00
else {
log_android(ANDROID_LOG_WARN, "Unknown session %s/%u uid %d",
dest, ntohs(tcphdr->dest), uid);
2016-01-18 14:29:01 +00:00
2016-01-17 09:42:21 +00:00
struct session rst;
memset(&rst, 0, sizeof(struct session));
2016-01-18 14:29:01 +00:00
rst.remote_seq = ntohl(tcphdr->seq);
2016-01-17 09:42:21 +00:00
rst.saddr = iphdr->saddr;
rst.source = tcphdr->source;
rst.daddr = iphdr->daddr;
rst.dest = tcphdr->dest;
2016-01-18 14:29:01 +00:00
write_rst(&rst, args->tun);
2016-01-15 09:28:31 +00:00
}
#ifdef PROFILE
gettimeofday(&end, NULL);
mselapsed = (end.tv_sec - start.tv_sec) * 1000.0 +
(end.tv_usec - start.tv_usec) / 1000.0;
if (mselapsed > 1)
log_android(ANDROID_LOG_INFO, "new session %f", mselapsed);
#endif
2016-01-11 22:06:35 +00:00
}
else {
2016-01-18 14:29:01 +00:00
// Session found
2016-01-16 11:32:55 +00:00
int oldstate = cur->state;
uint32_t oldlocal = cur->local_seq;
uint32_t oldremote = cur->remote_seq;
log_android(ANDROID_LOG_DEBUG,
"Session %s/%u lport %u state %s local %u remote %u",
dest, ntohs(cur->dest), cur->lport, strstate(cur->state),
cur->local_seq - cur->local_start,
cur->remote_seq - cur->remote_start);
2016-01-12 08:45:58 +00:00
2016-01-18 14:29:01 +00:00
cur->time = time(NULL);
2016-01-17 13:30:21 +00:00
// Do not change order of conditions
2016-01-17 13:30:21 +00:00
if (tcphdr->rst) {
log_android(ANDROID_LOG_INFO, "RST session %s/%u lport %u received",
dest, ntohs(cur->dest), cur->lport);
2016-01-18 14:29:01 +00:00
cur->state = TCP_TIME_WAIT;
2016-01-12 12:15:21 +00:00
}
2016-01-14 21:48:05 +00:00
2016-01-17 19:40:40 +00:00
else if (tcphdr->syn) {
log_android(ANDROID_LOG_WARN, "Repeated SYN session %s/%u lport %u",
dest, ntohs(cur->dest), cur->lport);
2016-01-18 14:29:01 +00:00
// Note: perfect, ordered packet receive assumed
2016-01-17 13:30:21 +00:00
2016-01-17 19:40:40 +00:00
} else if (tcphdr->fin /* ACK */) {
2016-01-16 11:32:55 +00:00
if (ntohl(tcphdr->ack_seq) == cur->local_seq &&
ntohl(tcphdr->seq) == cur->remote_seq) {
2016-01-18 14:29:01 +00:00
if (datalen)
log_android(ANDROID_LOG_WARN, "FIN session %s/%u lport %u data %u",
dest, ntohs(cur->dest), cur->lport, datalen);
2016-01-16 11:32:55 +00:00
2016-01-18 14:29:01 +00:00
// Forward last data to socket
2016-01-17 09:42:21 +00:00
int ok = 1;
if (tcphdr->ack && datalen) {
log_android(ANDROID_LOG_DEBUG, "send socket data %u", datalen);
2016-01-17 19:40:40 +00:00
if (send_socket(cur->socket, buffer + dataoff, datalen) < 0)
2016-01-17 09:42:21 +00:00
ok = 0;
}
2016-01-16 11:32:55 +00:00
2016-01-18 14:29:01 +00:00
// Shutdown socket for writing
if (shutdown(cur->socket, SHUT_WR)) {
log_android(ANDROID_LOG_ERROR, "shutdown WR error %d: %s",
errno, strerror(errno));
2016-01-18 14:29:01 +00:00
ok = 0;
// Data might be lost
}
2016-01-17 09:42:21 +00:00
if (ok) {
2016-01-18 14:29:01 +00:00
if (write_ack(cur, 1 + datalen, args->tun) >= 0) {
2016-01-18 19:28:33 +00:00
cur->remote_seq += (1 + datalen); // FIN + data from tun
2016-01-17 15:08:40 +00:00
if (cur->state == TCP_ESTABLISHED /* && !tcphdr->ack */)
2016-01-17 09:42:21 +00:00
cur->state = TCP_CLOSE_WAIT;
2016-01-17 15:08:40 +00:00
else if (cur->state == TCP_FIN_WAIT1 && tcphdr->ack)
cur->state = TCP_TIME_WAIT;
else if (cur->state == TCP_FIN_WAIT1 && !tcphdr->ack)
cur->state = TCP_CLOSING;
else if (cur->state == TCP_FIN_WAIT2 /* && !tcphdr->ack */)
2016-01-17 09:42:21 +00:00
cur->state = TCP_TIME_WAIT;
else
log_android(ANDROID_LOG_ERROR,
"Invalid FIN session %s/%u lport %u state %s ACK %d",
dest, ntohs(cur->dest), cur->lport,
strstate(cur->state), tcphdr->ack);
2016-01-16 11:32:55 +00:00
}
2016-01-18 19:28:33 +00:00
} else
2016-01-18 14:29:01 +00:00
write_rst(cur, args->tun);
2016-01-15 20:40:37 +00:00
}
else {
2016-01-18 14:29:01 +00:00
// Special case or hack if you like
2016-01-17 15:08:40 +00:00
// TODO proper wrap around
if (cur->state == TCP_FIN_WAIT1 &&
ntohl(tcphdr->seq) == cur->remote_seq &&
ntohl(tcphdr->ack_seq) < cur->local_seq) {
int confirm = cur->local_seq - ntohl(tcphdr->ack_seq);
log_android(ANDROID_LOG_INFO, "Simultaneous close %s/%u lport %u confirm %d",
dest, ntohs(cur->dest), cur->lport, confirm);
2016-01-18 14:29:01 +00:00
write_ack(cur, confirm, args->tun);
2016-01-17 12:45:34 +00:00
}
2016-01-17 15:08:40 +00:00
else
log_android(ANDROID_LOG_WARN,
"Invalid FIN session %s/%u lport %u state %s seq %u/%u ack %u/%u",
dest, ntohs(cur->dest), cur->lport, strstate(cur->state),
ntohl(tcphdr->seq) - cur->remote_start,
cur->remote_seq - cur->remote_start,
ntohl(tcphdr->ack_seq) - cur->local_start,
cur->local_seq - cur->local_start);
}
2016-01-14 21:48:05 +00:00
}
2016-01-17 13:30:21 +00:00
else if (tcphdr->ack) {
if (((uint32_t) ntohl(tcphdr->seq) + 1) == cur->remote_seq) {
log_android(ANDROID_LOG_INFO, "Keep alive session %s/%u lport %u",
dest, ntohs(cur->dest), cur->lport);
2016-01-17 19:40:40 +00:00
2016-01-17 13:30:21 +00:00
} else if (ntohl(tcphdr->ack_seq) == cur->local_seq &&
ntohl(tcphdr->seq) == cur->remote_seq) {
if (cur->state == TCP_SYN_RECV) {
// TODO process data?
2016-01-17 19:40:40 +00:00
// Remote will retry
2016-01-17 13:30:21 +00:00
cur->state = TCP_ESTABLISHED;
}
else if (cur->state == TCP_ESTABLISHED) {
log_android(ANDROID_LOG_DEBUG, "New ACK session %s/%u lport %u data %u",
dest, ntohs(cur->dest), cur->lport, datalen);
2016-01-17 19:40:40 +00:00
2016-01-18 14:29:01 +00:00
// Forward data to socket
2016-01-17 13:30:21 +00:00
if (datalen) {
log_android(ANDROID_LOG_DEBUG, "send socket data %u", datalen);
if (send_socket(cur->socket, buffer + dataoff, datalen) < 0)
2016-01-18 14:29:01 +00:00
write_rst(cur, args->tun);
else {
2016-01-18 14:29:01 +00:00
if (write_ack(cur, datalen, args->tun) >= 0)
cur->remote_seq += datalen;
2016-01-17 13:30:21 +00:00
}
}
}
else if (cur->state == TCP_LAST_ACK) {
// socket has been shutdown already
2016-01-18 14:29:01 +00:00
cur->state = TCP_TIME_WAIT;
2016-01-17 13:30:21 +00:00
}
else if (cur->state == TCP_FIN_WAIT1)
cur->state = TCP_FIN_WAIT2;
else if (cur->state == TCP_CLOSING)
cur->state = TCP_TIME_WAIT;
else
log_android(ANDROID_LOG_ERROR, "Invalid ACK session %s/%u lport %u state %s",
dest, ntohs(cur->dest), cur->lport, strstate(cur->state));
2016-01-17 13:30:21 +00:00
}
else {
2016-01-17 15:08:40 +00:00
// TODO proper wrap around
if (ntohl(tcphdr->seq) == cur->remote_seq &&
ntohl(tcphdr->ack_seq) < cur->local_seq)
log_android(ANDROID_LOG_INFO,
"Previous ACK session %s/%u lport %u seq %u/%u ack %u/%u",
dest, ntohs(cur->dest), cur->lport,
ntohl(tcphdr->seq) - cur->remote_start,
cur->remote_seq - cur->remote_start,
ntohl(tcphdr->ack_seq) - cur->local_start,
cur->local_seq - cur->local_start);
2016-01-17 15:08:40 +00:00
else
log_android(ANDROID_LOG_WARN,
"Invalid ACK session %s/%u lport %u state %s seq %u/%u ack %u/%u",
dest, ntohs(cur->dest), cur->lport, strstate(cur->state),
ntohl(tcphdr->seq) - cur->remote_start,
cur->remote_seq - cur->remote_start,
ntohl(tcphdr->ack_seq) - cur->local_start,
cur->local_seq - cur->local_start);
2016-01-17 13:30:21 +00:00
}
2016-01-17 09:42:21 +00:00
}
2016-01-16 11:32:55 +00:00
else
log_android(ANDROID_LOG_ERROR, "Unknown packet session %s/%u lport %u",
dest, ntohs(cur->dest), cur->lport);
2016-01-16 11:32:55 +00:00
if (cur->state != oldstate || cur->local_seq != oldlocal || cur->remote_seq != oldremote)
log_android(ANDROID_LOG_INFO,
"Session %s/%u lport %u new state %s local %u remote %u",
dest, ntohs(cur->dest), cur->lport, strstate(cur->state),
cur->local_seq - cur->local_start,
cur->remote_seq - cur->remote_start);
#ifdef PROFILE
gettimeofday(&end, NULL);
mselapsed = (end.tv_sec - start.tv_sec) * 1000.0 +
(end.tv_usec - start.tv_usec) / 1000.0;
if (mselapsed > 1)
log_android(ANDROID_LOG_INFO, "existing session %f", mselapsed);
#endif
2016-01-13 09:26:06 +00:00
}
}
2016-01-18 14:29:01 +00:00
int open_socket(const struct session *cur, const struct arguments *args) {
2016-01-13 09:26:06 +00:00
int sock = -1;
2016-01-18 14:29:01 +00:00
// Build target address
struct sockaddr_in daddr;
memset(&daddr, 0, sizeof(struct sockaddr_in));
daddr.sin_family = AF_INET;
daddr.sin_port = cur->dest;
daddr.sin_addr.s_addr = cur->daddr;
2016-01-13 09:26:06 +00:00
// Get TCP socket
// TODO socket options (SO_REUSEADDR, etc)
2016-01-13 09:26:06 +00:00
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
log_android(ANDROID_LOG_ERROR, "socket error %d: %s", errno, strerror(errno));
2016-01-13 09:26:06 +00:00
return -1;
}
// Protect
2016-01-18 11:19:40 +00:00
JNIEnv *env = args->env;
jobject instance = args->instance;
2016-01-13 09:26:06 +00:00
jclass cls = (*env)->GetObjectClass(env, instance);
jmethodID mid = (*env)->GetMethodID(env, cls, "protect", "(I)Z");
if (mid == 0) {
log_android(ANDROID_LOG_ERROR, "protect not found");
2016-01-13 09:26:06 +00:00
return -1;
}
else {
jboolean isProtected = (*env)->CallBooleanMethod(env, instance, mid, sock);
if (!isProtected)
log_android(ANDROID_LOG_ERROR, "protect failed");
2016-01-13 09:26:06 +00:00
jthrowable ex = (*env)->ExceptionOccurred(env);
if (ex) {
(*env)->ExceptionDescribe(env);
(*env)->ExceptionClear(env);
(*env)->DeleteLocalRef(env, ex);
}
}
2016-01-12 12:15:21 +00:00
2016-01-18 20:54:45 +00:00
// Set non blocking
2016-01-13 10:11:11 +00:00
uint8_t flags = fcntl(sock, F_GETFL, 0);
2016-01-18 20:54:45 +00:00
if (flags < 0 || fcntl(sock, F_SETFL, flags | O_NONBLOCK) < 0) {
log_android(ANDROID_LOG_ERROR, "fcntl socket O_NONBLOCK error %d: %s",
errno, strerror(errno));
2016-01-13 10:11:11 +00:00
return -1;
}
2016-01-13 09:26:06 +00:00
// Initiate connect
2016-01-18 14:29:01 +00:00
int err = connect(sock, &daddr, sizeof(struct sockaddr_in));
2016-01-13 09:26:06 +00:00
if (err < 0 && errno != EINPROGRESS) {
log_android(ANDROID_LOG_ERROR, "connect error %d: %s", errno, strerror(errno));
2016-01-13 09:26:06 +00:00
return -1;
2016-01-11 22:06:35 +00:00
}
2016-01-13 09:26:06 +00:00
2016-01-13 18:25:15 +00:00
// Set blocking
2016-01-14 14:02:32 +00:00
if (fcntl(sock, F_SETFL, flags & ~O_NONBLOCK) < 0) {
2016-01-18 20:54:45 +00:00
log_android(ANDROID_LOG_ERROR, "fcntl socket ~O_NONBLOCK error %d: %s", errno, strerror(errno));
2016-01-13 18:25:15 +00:00
return -1;
}
2016-01-13 09:26:06 +00:00
return sock;
2016-01-11 22:06:35 +00:00
}
2016-01-17 16:39:11 +00:00
int get_local_port(const int sock) {
2016-01-13 09:26:06 +00:00
struct sockaddr_in sin;
int len = sizeof(sin);
if (getsockname(sock, &sin, &len) < 0) {
log_android(ANDROID_LOG_ERROR, "getsockname error %d: %s", errno, strerror(errno));
2016-01-13 09:26:06 +00:00
return -1;
} else
return ntohs(sin.sin_port);
}
ssize_t send_socket(int sock, uint8_t *buffer, uint16_t len) {
// TODO non blocking
ssize_t res = send(sock, buffer, len, 0);
if (res < 0)
log_android(ANDROID_LOG_ERROR, "send error %d: %s", errno, strerror(errno));
return res;
}
2016-01-18 14:29:01 +00:00
int write_syn_ack(struct session *cur, int tun) {
if (write_tcp(cur, NULL, 0, 1, 1, 0, 0, tun) < 0) {
log_android(ANDROID_LOG_ERROR, "write SYN+ACK error %d: %s",
errno, strerror((errno)));
2016-01-18 14:29:01 +00:00
cur->state = TCP_TIME_WAIT;
return -1;
}
return 0;
}
int write_ack(struct session *cur, int bytes, int tun) {
if (write_tcp(cur, NULL, 0, bytes, 0, 0, 0, tun) < 0) {
log_android(ANDROID_LOG_ERROR, "write ACK error %d: %s",
errno, strerror((errno)));
2016-01-18 14:29:01 +00:00
cur->state = TCP_TIME_WAIT;
return -1;
}
return 0;
}
int write_data(struct session *cur, const uint8_t *buffer, uint16_t length, int tun) {
if (write_tcp(cur, buffer, length, 0, 0, 0, 0, tun) < 0) {
log_android(ANDROID_LOG_ERROR, "write data ACK lport %u error %d: %s",
cur->lport, errno, strerror((errno)));
2016-01-18 14:29:01 +00:00
cur->state = TCP_TIME_WAIT;
}
}
int write_fin(struct session *cur, int tun) {
if (write_tcp(cur, NULL, 0, 0, 0, 1, 0, tun) < 0) {
log_android(ANDROID_LOG_ERROR,
"write FIN lport %u error %d: %s",
cur->lport, errno, strerror((errno)));
2016-01-18 14:29:01 +00:00
cur->state = TCP_TIME_WAIT;
return -1;
}
return 0;
}
void write_rst(struct session *cur, int tun) {
2016-01-18 19:05:58 +00:00
log_android(ANDROID_LOG_WARN, "Sending RST");
2016-01-18 14:29:01 +00:00
if (write_tcp(cur, NULL, 0, 0, 0, 0, 1, tun) < 0)
log_android(ANDROID_LOG_ERROR, "write RST error %d: %s",
errno, strerror((errno)));
2016-01-18 14:29:01 +00:00
cur->state = TCP_TIME_WAIT;
}
2016-01-17 19:40:40 +00:00
2016-01-17 16:39:11 +00:00
int write_tcp(const struct session *cur,
uint8_t *data, uint16_t datalen, uint16_t confirm,
int syn, int fin, int rst, int tun) {
#ifdef PROFILE
float mselapsed;
struct timeval start, end;
gettimeofday(&start, NULL);
#endif
2016-01-14 14:02:32 +00:00
// Build packet
2016-01-15 09:28:31 +00:00
uint16_t len = sizeof(struct iphdr) + sizeof(struct tcphdr) + datalen;
2016-01-14 14:02:32 +00:00
u_int8_t *buffer = calloc(len, 1);
struct iphdr *ip = buffer;
struct tcphdr *tcp = buffer + sizeof(struct iphdr);
2016-01-14 18:30:38 +00:00
if (datalen)
2016-01-16 14:12:42 +00:00
memcpy(buffer + sizeof(struct iphdr) + sizeof(struct tcphdr), data, datalen);
2016-01-14 14:02:32 +00:00
// Build IP header
ip->version = 4;
ip->ihl = sizeof(struct iphdr) >> 2;
ip->tot_len = htons(len);
ip->ttl = TCP_TTL;
2016-01-14 14:02:32 +00:00
ip->protocol = IPPROTO_TCP;
ip->saddr = cur->daddr;
ip->daddr = cur->saddr;
// Calculate IP checksum
ip->check = calc_checksum(ip, sizeof(struct iphdr));
2016-01-14 14:02:32 +00:00
// Build TCP header
tcp->source = cur->dest;
tcp->dest = cur->source;
2016-01-17 05:41:32 +00:00
tcp->seq = htonl(cur->local_seq);
2016-01-17 09:42:21 +00:00
tcp->ack_seq = htonl((uint32_t) (cur->remote_seq + confirm));
2016-01-14 14:02:32 +00:00
tcp->doff = sizeof(struct tcphdr) >> 2;
2016-01-15 09:04:44 +00:00
tcp->syn = syn;
2016-01-17 09:42:21 +00:00
tcp->ack = (datalen > 0 || confirm > 0 || syn);
2016-01-14 21:48:05 +00:00
tcp->fin = fin;
2016-01-15 09:04:44 +00:00
tcp->rst = rst;
tcp->window = htons(TCP_WINDOW);
2016-01-14 14:02:32 +00:00
2016-01-17 09:42:21 +00:00
if (!tcp->ack)
tcp->ack_seq = 0;
2016-01-14 14:02:32 +00:00
// Calculate TCP checksum
2016-01-16 14:12:42 +00:00
// TODO optimize memory usage
2016-01-14 18:30:38 +00:00
uint16_t clen = sizeof(struct ippseudo) + sizeof(struct tcphdr) + datalen;
2016-01-14 14:02:32 +00:00
uint8_t csum[clen];
// Build pseudo header
struct ippseudo *pseudo = csum;
pseudo->ippseudo_src.s_addr = ip->saddr;
pseudo->ippseudo_dst.s_addr = ip->daddr;
pseudo->ippseudo_pad = 0;
pseudo->ippseudo_p = ip->protocol;
2016-01-15 09:28:31 +00:00
pseudo->ippseudo_len = htons(sizeof(struct tcphdr) + datalen);
2016-01-14 14:02:32 +00:00
2016-01-14 18:30:38 +00:00
// Copy TCP header + data
2016-01-14 14:02:32 +00:00
memcpy(csum + sizeof(struct ippseudo), tcp, sizeof(struct tcphdr));
2016-01-14 18:30:38 +00:00
if (datalen)
2016-01-16 14:12:42 +00:00
memcpy(csum + sizeof(struct ippseudo) + sizeof(struct tcphdr), data, datalen);
2016-01-14 14:02:32 +00:00
tcp->check = calc_checksum(csum, clen);
2016-01-14 14:02:32 +00:00
char to[20];
inet_ntop(AF_INET, &(ip->daddr), to, sizeof(to));
// Send packet
log_android(ANDROID_LOG_DEBUG,
"Sending%s%s%s%s to tun %s/%u seq %u ack %u data %u confirm %u",
(tcp->syn ? " SYN" : ""),
(tcp->ack ? " ACK" : ""),
(tcp->fin ? " FIN" : ""),
(tcp->rst ? " RST" : ""),
to, ntohs(tcp->dest),
ntohl(tcp->seq) - cur->local_start,
ntohl(tcp->ack_seq) - cur->remote_start,
datalen, confirm);
2016-01-18 20:18:23 +00:00
// TODO non blocking
2016-01-14 14:02:32 +00:00
int res = write(tun, buffer, len);
#ifdef PROFILE
gettimeofday(&end, NULL);
mselapsed = (end.tv_sec - start.tv_sec) * 1000.0 +
(end.tv_usec - start.tv_usec) / 1000.0;
if (mselapsed > 1)
log_android(ANDROID_LOG_INFO, "tun write %f", mselapsed);
#endif
2016-01-17 19:40:40 +00:00
// Write pcap record
2016-01-18 20:37:51 +00:00
if (native && pcap_fn != NULL)
write_pcap_rec(buffer, len);
2016-01-17 09:42:21 +00:00
2016-01-14 14:02:32 +00:00
free(buffer);
return res;
}
2016-01-17 16:39:11 +00:00
jint get_uid(const int protocol, const int version, const void *saddr, const uint16_t sport) {
2016-01-10 07:14:47 +00:00
char line[250];
int fields;
int32_t addr32;
int8_t addr128[16];
uint16_t port;
jint uid = -1;
2016-01-10 07:14:47 +00:00
#ifdef PROFILE
float mselapsed;
struct timeval start, end;
gettimeofday(&start, NULL);
#endif
2016-01-10 07:14:47 +00:00
// Get proc file name
char *fn = NULL;
if (protocol == IPPROTO_TCP)
fn = (version == 4 ? "/proc/net/tcp" : "/proc/net/tcp6");
else if (protocol == IPPROTO_UDP)
fn = (version == 4 ? "/proc/net/udp" : "/proc/net/udp6");
else
2016-01-15 11:13:12 +00:00
return uid;
2016-01-10 07:14:47 +00:00
// Open proc file
FILE *fd = fopen(fn, "r");
if (fd == NULL) {
log_android(ANDROID_LOG_ERROR, "fopen %s error %d: %s", fn, errno, strerror(errno));
2016-01-15 11:13:12 +00:00
return uid;
2016-01-10 07:14:47 +00:00
}
// Scan proc file
2016-01-15 11:13:12 +00:00
jint u;
2016-01-10 07:14:47 +00:00
int i = 0;
while (fgets(line, sizeof(line), fd) != NULL) {
if (i++) {
if (version == 4)
fields = sscanf(
line,
"%*d: %X:%X %*X:%*X %*X %*lX:%*lX %*X:%*X %*X %d %*d %*ld ",
&addr32, &port, &u);
2016-01-10 07:14:47 +00:00
else
fields = sscanf(
line,
"%*d: %8X%8X%8X%8X:%X %*X:%*X %*X %*lX:%*lX %*X:%*X %*X %d %*d %*ld ",
addr128, addr128 + 4, addr128 + 8, addr128 + 12, &port, &u);
2016-01-15 11:13:12 +00:00
if (fields == (version == 4 ? 3 : 6)) {
if (port == sport) {
if (version == 4) {
if (addr32 == *((int32_t *) saddr)) {
uid = u;
break;
}
}
else {
if (memcmp(addr128, saddr, (size_t) 16) == 0) {
uid = u;
break;
}
}
2016-01-10 11:51:02 +00:00
}
2016-01-15 11:13:12 +00:00
} else
log_android(ANDROID_LOG_ERROR, "Invalid field #%d: %s", fields, line);
2016-01-09 20:17:42 +00:00
}
}
2016-01-10 07:14:47 +00:00
2016-01-15 09:28:31 +00:00
if (fclose(fd))
log_android(ANDROID_LOG_ERROR, "fclose %s error %d: %s", fn, errno, strerror(errno));
#ifdef PROFILE
gettimeofday(&end, NULL);
mselapsed = (end.tv_sec - start.tv_sec) * 1000.0 +
(end.tv_usec - start.tv_usec) / 1000.0;
if (mselapsed > 1)
log_android(ANDROID_LOG_INFO, "get uid ip %f", mselapsed);
#endif
2016-01-10 07:14:47 +00:00
2016-01-15 11:13:12 +00:00
return uid;
2016-01-12 12:15:21 +00:00
}
uint16_t calc_checksum(uint8_t *buffer, uint16_t length) {
2016-01-15 11:57:32 +00:00
register uint32_t sum = 0;
register uint16_t *buf = buffer;
register int len = length;
2016-01-12 12:15:21 +00:00
2016-01-15 11:57:32 +00:00
while (len > 1) {
sum += *buf++;
len -= 2;
2016-01-12 12:15:21 +00:00
}
2016-01-15 11:57:32 +00:00
if (len > 0)
sum += *((uint8_t *) buf);
while (sum >> 16)
sum = (sum & 0xFFFF) + (sum >> 16);
return (uint16_t) (~sum);
2016-01-12 12:15:21 +00:00
}
void log_android(int prio, const char *fmt, ...) {
2016-01-17 15:08:40 +00:00
if (prio >= loglevel) {
char line[1024];
2016-01-17 13:20:07 +00:00
va_list argptr;
va_start(argptr, fmt);
2016-01-17 15:08:40 +00:00
vsprintf(line, fmt, argptr);
__android_log_print(prio, TAG, line);
2016-01-17 13:20:07 +00:00
va_end(argptr);
}
}
void log_java(
const struct arguments *args, uint8_t version,
const char *source, uint16_t sport,
const char *dest, uint16_t dport,
uint8_t protocol, const char *flags,
jint uid, jboolean allowed) {
#ifdef PROFILE
float mselapsed;
struct timeval start, end;
gettimeofday(&start, NULL);
#endif
JNIEnv *env = args->env;
jobject instance = args->instance;
jclass cls = (*env)->GetObjectClass(env, instance);
jmethodID mid = (*env)->GetMethodID(
env, cls, "logPacket",
"(ILjava/lang/String;ILjava/lang/String;IILjava/lang/String;IZ)V");
if (mid == 0)
log_android(ANDROID_LOG_ERROR, "logPacket not found");
else {
jstring jsource = (*env)->NewStringUTF(env, source);
jstring jdest = (*env)->NewStringUTF(env, dest);
jstring jflags = (*env)->NewStringUTF(env, flags);
(*env)->CallVoidMethod(env, instance, mid,
version,
jsource, sport,
jdest, dport,
protocol, jflags,
uid, allowed);
(*env)->DeleteLocalRef(env, jsource);
(*env)->DeleteLocalRef(env, jdest);
(*env)->DeleteLocalRef(env, jflags);
jthrowable ex = (*env)->ExceptionOccurred(env);
if (ex) {
(*env)->ExceptionDescribe(env);
(*env)->ExceptionClear(env);
(*env)->DeleteLocalRef(env, ex);
}
}
(*env)->DeleteLocalRef(env, cls);
#ifdef PROFILE
gettimeofday(&end, NULL);
mselapsed = (end.tv_sec - start.tv_sec) * 1000.0 +
(end.tv_usec - start.tv_usec) / 1000.0;
if (mselapsed > 1)
log_android(ANDROID_LOG_INFO, "log java %f", mselapsed);
#endif
}
void write_pcap(const void *ptr, size_t len) {
#ifdef PROFILE
float mselapsed;
struct timeval start, end;
gettimeofday(&start, NULL);
#endif
FILE *fd = fopen(pcap_fn, "ab");
if (fd == NULL)
log_android(ANDROID_LOG_ERROR, "fopen %s error %d: %s",
pcap_fn, errno, strerror(errno));
else {
if (fwrite(ptr, len, 1, fd) < 1)
log_android(ANDROID_LOG_ERROR, "fwrite %s error %d: %s",
pcap_fn, errno, strerror(errno));
else
log_android(ANDROID_LOG_DEBUG, "PCAP write %d", len);
if (fclose(fd))
log_android(ANDROID_LOG_ERROR, "fclose %s error %d: %s",
pcap_fn, errno, strerror(errno));
}
#ifdef PROFILE
gettimeofday(&end, NULL);
mselapsed = (end.tv_sec - start.tv_sec) * 1000.0 +
(end.tv_usec - start.tv_usec) / 1000.0;
if (mselapsed > 1)
log_android(ANDROID_LOG_INFO, "pcap write %f", mselapsed);
#endif
}
2016-01-18 20:37:51 +00:00
void write_pcap_hdr() {
struct pcap_hdr_s pcap_hdr;
pcap_hdr.magic_number = 0xa1b2c3d4;
pcap_hdr.version_major = 2;
pcap_hdr.version_minor = 4;
pcap_hdr.thiszone = 0;
pcap_hdr.sigfigs = 0;
pcap_hdr.snaplen = MAX_PCAP;
pcap_hdr.network = LINKTYPE_RAW;
write_pcap(&pcap_hdr, sizeof(struct pcap_hdr_s));
}
void write_pcap_rec(const uint8_t *buffer, uint16_t length) {
struct timespec ts;
if (clock_gettime(CLOCK_REALTIME, &ts))
log_android(ANDROID_LOG_ERROR, "clock_gettime error %d: %s", errno, strerror(errno));
// TODO use stack
int plen = (length < MAX_PCAP ? length : MAX_PCAP);
struct pcaprec_hdr_s *pcap_rec = malloc(sizeof(struct pcaprec_hdr_s) + plen);
pcap_rec->ts_sec = ts.tv_sec;
pcap_rec->ts_usec = ts.tv_nsec / 1000;
pcap_rec->incl_len = plen;
pcap_rec->orig_len = length;
memcpy(((uint8_t *) pcap_rec) + sizeof(struct pcaprec_hdr_s), buffer, plen);
write_pcap(pcap_rec, sizeof(struct pcaprec_hdr_s) + plen);
free(pcap_rec);
}
2016-01-16 11:32:55 +00:00
const char *strstate(const int state) {
char buf[20];
switch (state) {
case TCP_ESTABLISHED:
return "ESTABLISHED";
2016-01-16 11:32:55 +00:00
case TCP_SYN_SENT:
return "SYN_SENT";
2016-01-16 11:32:55 +00:00
case TCP_SYN_RECV:
return "SYN_RECV";
2016-01-16 11:32:55 +00:00
case TCP_FIN_WAIT1:
return "FIN_WAIT1";
2016-01-16 11:32:55 +00:00
case TCP_FIN_WAIT2:
return "FIN_WAIT2";
2016-01-16 11:32:55 +00:00
case TCP_TIME_WAIT:
return "TIME_WAIT";
2016-01-16 11:32:55 +00:00
case TCP_CLOSE:
return "CLOSE";
2016-01-16 11:32:55 +00:00
case TCP_CLOSE_WAIT:
return "CLOSE_WAIT";
2016-01-16 11:32:55 +00:00
case TCP_LAST_ACK:
return "LAST_ACK";
2016-01-16 11:32:55 +00:00
case TCP_LISTEN:
return "LISTEN";
2016-01-16 11:32:55 +00:00
case TCP_CLOSING:
return "CLOSING";
2016-01-16 11:32:55 +00:00
default:
sprintf(buf, "TCP_%d", state);
2016-01-16 11:32:55 +00:00
return buf;
}
}
2016-01-12 20:15:25 +00:00
char *hex(const u_int8_t *data, const u_int16_t len) {
char hex_str[] = "0123456789ABCDEF";
2016-01-12 12:15:21 +00:00
2016-01-15 11:13:12 +00:00
char *hexout;
hexout = (char *) malloc(len * 3 + 1); // TODO free
2016-01-12 12:15:21 +00:00
for (size_t i = 0; i < len; i++) {
2016-01-13 19:17:21 +00:00
hexout[i * 3 + 0] = hex_str[(data[i] >> 4) & 0x0F];
hexout[i * 3 + 1] = hex_str[(data[i]) & 0x0F];
hexout[i * 3 + 2] = ' ';
2016-01-12 12:15:21 +00:00
}
2016-01-13 19:17:21 +00:00
return hexout;
2016-01-12 16:19:27 +00:00
}