Merge "Add v4/v6 route add/removal support" into honeycomb-LTE
This commit is contained in:
commit
aded02cbd0
1 changed files with 29 additions and 83 deletions
|
|
@ -269,7 +269,7 @@ int ifc_get_info(const char *name, in_addr_t *addr, int *prefixLength, unsigned
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int ifc_add_ipv4_route(const char *ifname, struct in_addr dst, int prefix_length,
|
int ifc_act_on_ipv4_route(int action, const char *ifname, struct in_addr dst, int prefix_length,
|
||||||
struct in_addr gw)
|
struct in_addr gw)
|
||||||
{
|
{
|
||||||
struct rtentry rt;
|
struct rtentry rt;
|
||||||
|
|
@ -301,7 +301,7 @@ int ifc_add_ipv4_route(const char *ifname, struct in_addr dst, int prefix_length
|
||||||
return -errno;
|
return -errno;
|
||||||
}
|
}
|
||||||
|
|
||||||
result = ioctl(ifc_ctl_sock, SIOCADDRT, &rt);
|
result = ioctl(ifc_ctl_sock, action, &rt);
|
||||||
if (result < 0) {
|
if (result < 0) {
|
||||||
if (errno == EEXIST) {
|
if (errno == EEXIST) {
|
||||||
result = 0;
|
result = 0;
|
||||||
|
|
@ -320,17 +320,7 @@ int ifc_create_default_route(const char *name, in_addr_t gw)
|
||||||
in_dst.s_addr = 0;
|
in_dst.s_addr = 0;
|
||||||
in_gw.s_addr = gw;
|
in_gw.s_addr = gw;
|
||||||
|
|
||||||
return ifc_add_ipv4_route(name, in_dst, 0, in_gw);
|
return ifc_act_on_route(SIOCADDRT, name, in_dst, 0, in_gw);
|
||||||
}
|
|
||||||
|
|
||||||
int ifc_add_host_route(const char *name, in_addr_t dst)
|
|
||||||
{
|
|
||||||
struct in_addr in_dst, in_gw;
|
|
||||||
|
|
||||||
in_dst.s_addr = dst;
|
|
||||||
in_gw.s_addr = 0;
|
|
||||||
|
|
||||||
return ifc_add_ipv4_route(name, in_dst, 32, in_gw);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int ifc_enable(const char *ifname)
|
int ifc_enable(const char *ifname)
|
||||||
|
|
@ -448,67 +438,6 @@ int ifc_remove_host_routes(const char *name)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* Return the address of the default gateway
|
|
||||||
*
|
|
||||||
* TODO: factor out common code from this and remove_host_routes()
|
|
||||||
* so that we only scan /proc/net/route in one place.
|
|
||||||
*/
|
|
||||||
int ifc_get_default_route(const char *ifname)
|
|
||||||
{
|
|
||||||
char name[64];
|
|
||||||
in_addr_t dest, gway, mask;
|
|
||||||
int flags, refcnt, use, metric, mtu, win, irtt;
|
|
||||||
int result;
|
|
||||||
FILE *fp;
|
|
||||||
|
|
||||||
fp = fopen("/proc/net/route", "r");
|
|
||||||
if (fp == NULL)
|
|
||||||
return 0;
|
|
||||||
/* Skip the header line */
|
|
||||||
if (fscanf(fp, "%*[^\n]\n") < 0) {
|
|
||||||
fclose(fp);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
ifc_init();
|
|
||||||
result = 0;
|
|
||||||
for (;;) {
|
|
||||||
int nread = fscanf(fp, "%63s%X%X%X%d%d%d%X%d%d%d\n",
|
|
||||||
name, &dest, &gway, &flags, &refcnt, &use, &metric, &mask,
|
|
||||||
&mtu, &win, &irtt);
|
|
||||||
if (nread != 11) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if ((flags & (RTF_UP|RTF_GATEWAY)) == (RTF_UP|RTF_GATEWAY)
|
|
||||||
&& dest == 0
|
|
||||||
&& strcmp(ifname, name) == 0) {
|
|
||||||
result = gway;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fclose(fp);
|
|
||||||
ifc_close();
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Sets the specified gateway as the default route for the named interface.
|
|
||||||
*/
|
|
||||||
int ifc_set_default_route(const char *ifname, in_addr_t gateway)
|
|
||||||
{
|
|
||||||
struct in_addr addr;
|
|
||||||
int result;
|
|
||||||
|
|
||||||
ifc_init();
|
|
||||||
addr.s_addr = gateway;
|
|
||||||
if ((result = ifc_create_default_route(ifname, gateway)) < 0) {
|
|
||||||
LOGD("failed to add %s as default route for %s: %s",
|
|
||||||
inet_ntoa(addr), ifname, strerror(errno));
|
|
||||||
}
|
|
||||||
ifc_close();
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Removes the default route for the named interface.
|
* Removes the default route for the named interface.
|
||||||
*/
|
*/
|
||||||
|
|
@ -572,7 +501,7 @@ ifc_configure(const char *ifname,
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int ifc_add_ipv6_route(const char *ifname, struct in6_addr dst, int prefix_length,
|
int ifc_act_on_ipv6_route(int action, const char *ifname, struct in6_addr dst, int prefix_length,
|
||||||
struct in6_addr gw)
|
struct in6_addr gw)
|
||||||
{
|
{
|
||||||
struct in6_rtmsg rtmsg;
|
struct in6_rtmsg rtmsg;
|
||||||
|
|
@ -607,7 +536,7 @@ int ifc_add_ipv6_route(const char *ifname, struct in6_addr dst, int prefix_lengt
|
||||||
return -errno;
|
return -errno;
|
||||||
}
|
}
|
||||||
|
|
||||||
result = ioctl(ifc_ctl_sock6, SIOCADDRT, &rtmsg);
|
result = ioctl(ifc_ctl_sock6, action, &rtmsg);
|
||||||
if (result < 0) {
|
if (result < 0) {
|
||||||
if (errno == EEXIST) {
|
if (errno == EEXIST) {
|
||||||
result = 0;
|
result = 0;
|
||||||
|
|
@ -619,8 +548,8 @@ int ifc_add_ipv6_route(const char *ifname, struct in6_addr dst, int prefix_lengt
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
int ifc_add_route(const char *ifname, const char *dst, int prefix_length,
|
int ifc_act_on_route(int action, const char *ifname, const char *dst, int prefix_length,
|
||||||
const char *gw)
|
const char *gw)
|
||||||
{
|
{
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
struct sockaddr_in ipv4_dst, ipv4_gw;
|
struct sockaddr_in ipv4_dst, ipv4_gw;
|
||||||
|
|
@ -638,7 +567,7 @@ int ifc_add_route(const char *ifname, const char *dst, int prefix_length,
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (gw == NULL) {
|
if (gw == NULL || (strlen(gw) == 0)) {
|
||||||
if (addr_ai->ai_family == AF_INET6) {
|
if (addr_ai->ai_family == AF_INET6) {
|
||||||
gw = "::";
|
gw = "::";
|
||||||
} else if (addr_ai->ai_family == AF_INET) {
|
} else if (addr_ai->ai_family == AF_INET) {
|
||||||
|
|
@ -646,6 +575,13 @@ int ifc_add_route(const char *ifname, const char *dst, int prefix_length,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (((addr_ai->ai_family == AF_INET6) && (prefix_length < 0 || prefix_length > 128)) ||
|
||||||
|
((addr_ai->ai_family == AF_INET) && (prefix_length < 0 || prefix_length > 32))) {
|
||||||
|
printerr("ifc_add_route: invalid prefix length");
|
||||||
|
freeaddrinfo(addr_ai);
|
||||||
|
return -EINVAL;
|
||||||
|
}
|
||||||
|
|
||||||
ret = getaddrinfo(gw, NULL, &hints, &gw_ai);
|
ret = getaddrinfo(gw, NULL, &hints, &gw_ai);
|
||||||
if (ret != 0) {
|
if (ret != 0) {
|
||||||
printerr("getaddrinfo failed: invalid gateway %s\n", gw);
|
printerr("getaddrinfo failed: invalid gateway %s\n", gw);
|
||||||
|
|
@ -663,13 +599,13 @@ int ifc_add_route(const char *ifname, const char *dst, int prefix_length,
|
||||||
if (addr_ai->ai_family == AF_INET6) {
|
if (addr_ai->ai_family == AF_INET6) {
|
||||||
memcpy(&ipv6_dst, addr_ai->ai_addr, sizeof(struct sockaddr_in6));
|
memcpy(&ipv6_dst, addr_ai->ai_addr, sizeof(struct sockaddr_in6));
|
||||||
memcpy(&ipv6_gw, gw_ai->ai_addr, sizeof(struct sockaddr_in6));
|
memcpy(&ipv6_gw, gw_ai->ai_addr, sizeof(struct sockaddr_in6));
|
||||||
ret = ifc_add_ipv6_route(ifname, ipv6_dst.sin6_addr, prefix_length,
|
ret = ifc_act_on_ipv6_route(action, ifname, ipv6_dst.sin6_addr,
|
||||||
ipv6_gw.sin6_addr);
|
prefix_length, ipv6_gw.sin6_addr);
|
||||||
} else if (addr_ai->ai_family == AF_INET) {
|
} else if (addr_ai->ai_family == AF_INET) {
|
||||||
memcpy(&ipv4_dst, addr_ai->ai_addr, sizeof(struct sockaddr_in));
|
memcpy(&ipv4_dst, addr_ai->ai_addr, sizeof(struct sockaddr_in));
|
||||||
memcpy(&ipv4_gw, gw_ai->ai_addr, sizeof(struct sockaddr_in));
|
memcpy(&ipv4_gw, gw_ai->ai_addr, sizeof(struct sockaddr_in));
|
||||||
ret = ifc_add_ipv4_route(ifname, ipv4_dst.sin_addr, prefix_length,
|
ret = ifc_act_on_ipv4_route(action, ifname, ipv4_dst.sin_addr,
|
||||||
ipv4_gw.sin_addr);
|
prefix_length, ipv4_gw.sin_addr);
|
||||||
} else {
|
} else {
|
||||||
printerr("ifc_add_route: getaddrinfo returned un supported address family %d\n",
|
printerr("ifc_add_route: getaddrinfo returned un supported address family %d\n",
|
||||||
addr_ai->ai_family);
|
addr_ai->ai_family);
|
||||||
|
|
@ -680,3 +616,13 @@ int ifc_add_route(const char *ifname, const char *dst, int prefix_length,
|
||||||
freeaddrinfo(gw_ai);
|
freeaddrinfo(gw_ai);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int ifc_add_route(const char *ifname, const char *dst, int prefix_length, const char *gw)
|
||||||
|
{
|
||||||
|
return ifc_act_on_route(SIOCADDRT, ifname, dst, prefix_length, gw);
|
||||||
|
}
|
||||||
|
|
||||||
|
int ifc_remove_route(const char *ifname, const char*dst, int prefix_length, const char *gw)
|
||||||
|
{
|
||||||
|
return ifc_act_on_route(SIOCDELRT, ifname, dst, prefix_length, gw);
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue