Skip to content

Commit

Permalink
Merge pull request #185 from liuyuan10/ipv6
Browse files Browse the repository at this point in the history
Add ipv6 utilities
  • Loading branch information
Octavian Purdila authored Aug 3, 2016
2 parents bafaea1 + 90bbe6a commit 11f0696
Show file tree
Hide file tree
Showing 6 changed files with 294 additions and 52 deletions.
6 changes: 3 additions & 3 deletions Documentation/lkl.txt
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,11 @@ are the list of those variable for your environment.
```
$ LKL_HIJACK_NET_MTU=1280 lkl-hijack.sh ip address show
```
* LKL_HIJACK_NET_ARP
* LKL_HIJACK_NET_NEIGHBOR

Add a list of arp permanent entries in the form of "ip|mac;ip|mac;..."
Add a list of permanent neighbor entries in the form of "ip|mac;ip|mac;...". ipv6 are supported
```
$ LKL_HIJACK_NET_ARP="192.168.13.100|12:34:56:78:9a:bc;192.168.13.101|12:34:56:78:9a:be"
$ LKL_HIJACK_NET_NEIGHBOR="192.168.13.100|12:34:56:78:9a:bc;2001:db8:0:f101::3|12:34:56:78:9a:be"
lkl-hijack.sh ip neighbor show
```
* LKL_HIJACK_DEBUG
Expand Down
8 changes: 7 additions & 1 deletion arch/lkl/include/uapi/asm/syscalls.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,20 @@ struct sockaddr {
#include <linux/if.h>
#define __UAPI_DEF_IN_IPPROTO 1
#define __UAPI_DEF_IN_ADDR 1
#define __UAPI_DEF_IN_ADDR 1
#define __UAPI_DEF_IN6_ADDR 1
#define __UAPI_DEF_IP_MREQ 1
#define __UAPI_DEF_IN_PKTINFO 1
#define __UAPI_DEF_SOCKADDR_IN 1
#define __UAPI_DEF_IN_CLASS 1
#include <linux/in.h>
#include <linux/in6.h>
#include <linux/sockios.h>
#include <linux/route.h>
#include <linux/ipv6_route.h>
#include <linux/ipv6.h>
#include <linux/netlink.h>
#include <linux/neighbour.h>
#include <linux/rtnetlink.h>

#include <linux/kdev_t.h>
#include <asm/irq.h>
Expand Down
23 changes: 21 additions & 2 deletions tools/lkl/include/lkl.h
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,24 @@ int lkl_if_set_ipv4(int ifindex, unsigned int addr, unsigned int netmask_len);
*/
int lkl_set_ipv4_gateway(unsigned int addr);

/**
* lkl_if_set_ipv6 - set IPv6 address on interface
*
* @ifindex - the ifindex of the interface
* @addr - 16-byte IPv6 address (i.e., struct in6_addr)
* @netprefix_len - prefix length of the @addr
* @returns - return 0 if no error: otherwise negative value returns
*/
int lkl_if_set_ipv6(int ifindex, void* addr, unsigned int netprefix_len);

/**
* lkl_set_ipv6_gateway - add an IPv6 default route
*
* @addr - 16-byte IPv6 address of the gateway (i.e., struct in6_addr)
* @returns - return 0 if no error: otherwise negative value returns
*/
int lkl_set_ipv6_gateway(void* addr);

/**
* lkl_netdev - host network device handle, defined in lkl_host.h.
*/
Expand Down Expand Up @@ -321,12 +339,13 @@ struct lkl_netdev *lkl_netdev_raw_create(const char *ifname);
void lkl_register_dbg_handler();

/**
* lkl_add_arp_entry - add a permanent arp entry
* lkl_add_neighbor - add a permanent arp entry
* @ifindex - the ifindex of the interface
* @af - adress family of the ip address. Must be LKL_AF_INET or LKL_AF_INET6
* @ip - ip address of the entry in network byte order
* @mac - mac address of the entry
*/
int lkl_add_arp_entry(int ifindex, unsigned int ip, void* mac);
int lkl_add_neighbor(int ifindex, int af, void* addr, void* mac);

#ifdef __cplusplus
}
Expand Down
59 changes: 50 additions & 9 deletions tools/lkl/lib/hijack/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,14 @@ int parse_mac_str(char *mac_str, __lkl__u8 mac[LKL_ETH_ALEN])
return 1;
}

/* Add permanent arp entries in the form of "ip|mac;ip|mac;..." */
static void add_arp(int ifindex, char* entries) {
/* Add permanent neighbor entries in the form of "ip|mac;ip|mac;..." */
static void add_neighbor(int ifindex, char* entries) {
char *saveptr = NULL, *token = NULL;
char *ip = NULL, *mac_str = NULL;
int ret = 0;
__lkl__u8 mac[LKL_ETH_ALEN];
unsigned int ip_addr;
char ip_addr[16];
int af;

for (token = strtok_r(entries, ";", &saveptr); token;
token = strtok_r(NULL, ";", &saveptr)) {
Expand All @@ -80,15 +81,25 @@ static void add_arp(int ifindex, char* entries) {
if (ip == NULL || mac_str == NULL || strtok(NULL, "|") != NULL) {
return;
}
ip_addr = inet_addr(ip);
af = LKL_AF_INET;
ret = inet_pton(AF_INET, ip, ip_addr);
if (ret == 0) {
ret = inet_pton(AF_INET6, ip, ip_addr);
af = LKL_AF_INET6;
}
if (ret != 1) {
fprintf(stderr, "Bad ip address: %s\n", ip);
return;
}

ret = parse_mac_str(mac_str, mac);
if (ret != 1) {
fprintf(stderr, "Failed to parse mac: %s\n", mac_str);
return;
}
ret = lkl_add_arp_entry(ifindex, ip_addr, mac);
ret = lkl_add_neighbor(ifindex, af, ip_addr, mac);
if (ret) {
fprintf(stderr, "Failed to add arp entry: %s\n", lkl_strerror(ret));
fprintf(stderr, "Failed to add neighbor entry: %s\n", lkl_strerror(ret));
return;
}
}
Expand Down Expand Up @@ -215,13 +226,16 @@ hijack_init(void)
char *mtu_str = getenv("LKL_HIJACK_NET_MTU");
__lkl__u8 mac[LKL_ETH_ALEN] = {0};
char *ip = getenv("LKL_HIJACK_NET_IP");
char *ipv6 = getenv("LKL_HIJACK_NET_IPV6");
char *mac_str = getenv("LKL_HIJACK_NET_MAC");
char *netmask_len = getenv("LKL_HIJACK_NET_NETMASK_LEN");
char *netmask6_len = getenv("LKL_HIJACK_NET_NETMASK6_LEN");
char *gateway = getenv("LKL_HIJACK_NET_GATEWAY");
char *gateway6 = getenv("LKL_HIJACK_NET_GATEWAY6");
char *debug = getenv("LKL_HIJACK_DEBUG");
char *mount = getenv("LKL_HIJACK_MOUNT");
struct lkl_netdev *nd = NULL;
char *arp_entries = getenv("LKL_HIJACK_NET_ARP");
char *neigh_entries = getenv("LKL_HIJACK_NET_NEIGHBOR");
/* single_cpu mode:
* 0: Don't pin to single CPU (default).
* 1: Pin only LKL kernel threads to single CPU.
Expand Down Expand Up @@ -393,11 +407,38 @@ hijack_init(void)
}
}

if (nd_ifindex >= 0 && ipv6 && netmask6_len) {
char addr[16];
unsigned int pflen = atoi(netmask6_len);

if (inet_pton(AF_INET6, ipv6, addr) != 1) {
fprintf(stderr, "Invalid ipv6 addr: %s\n", ipv6);
} else {
ret = lkl_if_set_ipv6(nd_ifindex, addr, pflen);
if (ret < 0)
fprintf(stderr, "failed to set IPv6address: %s\n",
lkl_strerror(ret));
}
}

if (nd_ifindex >= 0 && gateway6) {
char gw[16];

if (inet_pton(AF_INET6, gateway6, gw) != 1) {
fprintf(stderr, "Invalid ipv6 gateway: %s\n", gateway6);
} else {
ret = lkl_set_ipv6_gateway(gw);
if (ret< 0)
fprintf(stderr, "failed to set IPv6 gateway: %s\n",
lkl_strerror(ret));
}
}

if (mount)
mount_cmds_exec(mount, mount_fs);

if (nd_ifindex >=0 && arp_entries)
add_arp(nd_ifindex, arp_entries);
if (nd_ifindex >=0 && neigh_entries)
add_neighbor(nd_ifindex, neigh_entries);
}

void __attribute__((destructor))
Expand Down
Loading

0 comments on commit 11f0696

Please sign in to comment.