Skip to content

Instantly share code, notes, and snippets.

@phhusson
Created April 15, 2020 13:18
Show Gist options
  • Save phhusson/47b80d034ffa3bda02d8639a8078a8d7 to your computer and use it in GitHub Desktop.
Save phhusson/47b80d034ffa3bda02d8639a8078a8d7 to your computer and use it in GitHub Desktop.
/system/bin/ip link add eth_slave type veth peer name slave
ifconfig eth0 down
ip link set eth0 name nfsif
ifconfig nfsif up
#We're changing ip routes/interfaces while nfs is mounted... sounds very safe.
ifconfig nfsif 0.0.0.0 up
brctl addbr br0
#Add nfsif first to get the same MAC, and, hopefully, same IP
brctl addif br0 nfsif
mac="$(cat /sys/class/net/nfsif/address)"
macPrefix="$(echo "$mac" |cut -d : -f 1-5)"
macEnd="$(echo "$mac" |cut -d : -f 6)"
macEndAdd="$(printf '%02x' $((0x$macEnd+3)))"
newMac="${macPrefix}:${macEndAdd}"
ip link set dev eth_slave address $newMac
brctl addif br0 android
ifconfig br0 up
ifconfig android up
exec /netns
netns.c
#define _GNU_SOURCE
#include <sched.h>
#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <fcntl.h>
int createNetns(void* arg) {
while(1) sleep(1);
}
void register_interface(char *const iface, char *const ns) {
if(fork() == 0) {
char *const args[] = {
"/system/bin/ip",
"link",
"set",
iface,
"netns",
ns,
NULL
};
execve("/system/bin/ip", args, environ);
return;
}
int status;
wait(&status);
printf("ip link set returned %d\n", status);
}
int main() {
void* stack = malloc(16*1024);
long pid = clone(createNetns, stack, CLONE_NEWNET | SIGCHLD, NULL);
char *path = NULL;
asprintf(&path, "/proc/%ld/ns/net", pid);
int ns = -1;
while(ns == -1) {
ns = open(path, O_RDONLY);
printf("open ns %s %d\n", path, ns);
sleep(1);
}
register_interface("eth0", path);
int res = setns(ns, 0);
close(ns);
printf("setns returned %d %d\n", res, errno);
char *args[] = {
"/sbin/switch_root",
NULL
};
execve("/sbin/init", args, environ);
return 0;
}
/system/bin/ip link set eth_slave name eth0
ifconfig eth0 up
exec /netns
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment