summaryrefslogtreecommitdiffstats
path: root/libnetutils/ifc_utils.c
diff options
context:
space:
mode:
Diffstat (limited to 'libnetutils/ifc_utils.c')
-rw-r--r--libnetutils/ifc_utils.c355
1 files changed, 344 insertions, 11 deletions
diff --git a/libnetutils/ifc_utils.c b/libnetutils/ifc_utils.c
index c9d6ed2..0a2f760 100644
--- a/libnetutils/ifc_utils.c
+++ b/libnetutils/ifc_utils.c
@@ -26,15 +26,18 @@
#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/if.h>
+#include <netdb.h>
#include <linux/if.h>
#include <linux/if_ether.h>
#include <linux/if_arp.h>
-#include <linux/sockios.h>
+#include <linux/netlink.h>
#include <linux/route.h>
#include <linux/ipv6_route.h>
-#include <netdb.h>
-#include <linux/wireless.h>
+#include <linux/rtnetlink.h>
+#include <linux/sockios.h>
+
+#include "netutils/ifc.h"
#ifdef ANDROID
#define LOG_TAG "NetUtils"
@@ -51,6 +54,10 @@ static int ifc_ctl_sock = -1;
static int ifc_ctl_sock6 = -1;
void printerr(char *fmt, ...);
+#define DBG 0
+#define INET_ADDRLEN 4
+#define INET6_ADDRLEN 16
+
in_addr_t prefixLengthToIpv4Netmask(int prefix_length)
{
in_addr_t mask = 0;
@@ -86,15 +93,41 @@ static const char *ipaddr_to_string(in_addr_t addr)
return inet_ntoa(in_addr);
}
+int string_to_ip(const char *string, struct sockaddr_storage *ss) {
+ struct addrinfo hints, *ai;
+ int ret;
+
+ if (ss == NULL) {
+ return -EFAULT;
+ }
+
+ memset(&hints, 0, sizeof(hints));
+ hints.ai_family = AF_UNSPEC;
+ hints.ai_flags = AI_NUMERICHOST;
+ hints.ai_socktype = SOCK_DGRAM;
+
+ ret = getaddrinfo(string, NULL, &hints, &ai);
+ if (ret == 0) {
+ memcpy(ss, ai->ai_addr, ai->ai_addrlen);
+ freeaddrinfo(ai);
+ }
+
+ return ret;
+}
+
int ifc_init(void)
{
+ int ret;
if (ifc_ctl_sock == -1) {
- ifc_ctl_sock = socket(AF_INET, SOCK_DGRAM, 0);
+ ifc_ctl_sock = socket(AF_INET, SOCK_DGRAM, 0);
if (ifc_ctl_sock < 0) {
printerr("socket() failed: %s\n", strerror(errno));
}
}
- return ifc_ctl_sock < 0 ? -1 : 0;
+
+ ret = ifc_ctl_sock < 0 ? -1 : 0;
+ if (DBG) printerr("ifc_init_returning %d", ret);
+ return ret;
}
int ifc_init6(void)
@@ -110,6 +143,7 @@ int ifc_init6(void)
void ifc_close(void)
{
+ if (DBG) printerr("ifc_close");
if (ifc_ctl_sock != -1) {
(void)close(ifc_ctl_sock);
ifc_ctl_sock = -1;
@@ -141,7 +175,7 @@ int ifc_get_hwaddr(const char *name, void *ptr)
if(r < 0) return -1;
memcpy(ptr, &ifr.ifr_hwaddr.sa_data, ETH_ALEN);
- return 0;
+ return 0;
}
int ifc_get_ifindex(const char *name, int *if_indexp)
@@ -169,12 +203,16 @@ static int ifc_set_flags(const char *name, unsigned set, unsigned clr)
int ifc_up(const char *name)
{
- return ifc_set_flags(name, IFF_UP, 0);
+ int ret = ifc_set_flags(name, IFF_UP, 0);
+ if (DBG) printerr("ifc_up(%s) = %d", name, ret);
+ return ret;
}
int ifc_down(const char *name)
{
- return ifc_set_flags(name, 0, IFF_UP);
+ int ret = ifc_set_flags(name, 0, IFF_UP);
+ if (DBG) printerr("ifc_down(%s) = %d", name, ret);
+ return ret;
}
static void init_sockaddr_in(struct sockaddr *sa, in_addr_t addr)
@@ -188,11 +226,193 @@ static void init_sockaddr_in(struct sockaddr *sa, in_addr_t addr)
int ifc_set_addr(const char *name, in_addr_t addr)
{
struct ifreq ifr;
+ int ret;
ifc_init_ifr(name, &ifr);
init_sockaddr_in(&ifr.ifr_addr, addr);
- return ioctl(ifc_ctl_sock, SIOCSIFADDR, &ifr);
+ ret = ioctl(ifc_ctl_sock, SIOCSIFADDR, &ifr);
+ if (DBG) printerr("ifc_set_addr(%s, xx) = %d", name, ret);
+ return ret;
+}
+
+/*
+ * Adds or deletes an IP address on an interface.
+ *
+ * Action is one of:
+ * - RTM_NEWADDR (to add a new address)
+ * - RTM_DELADDR (to delete an existing address)
+ *
+ * Returns zero on success and negative errno on failure.
+ */
+int ifc_act_on_address(int action, const char *name, const char *address,
+ int prefixlen) {
+ int ifindex, s, len, ret;
+ struct sockaddr_storage ss;
+ void *addr;
+ size_t addrlen;
+ struct {
+ struct nlmsghdr n;
+ struct ifaddrmsg r;
+ // Allow for IPv6 address, headers, and padding.
+ char attrbuf[NLMSG_ALIGN(sizeof(struct nlmsghdr)) +
+ NLMSG_ALIGN(sizeof(struct rtattr)) +
+ NLMSG_ALIGN(INET6_ADDRLEN)];
+ } req;
+ struct rtattr *rta;
+ struct nlmsghdr *nh;
+ struct nlmsgerr *err;
+ char buf[NLMSG_ALIGN(sizeof(struct nlmsghdr)) +
+ NLMSG_ALIGN(sizeof(struct nlmsgerr)) +
+ NLMSG_ALIGN(sizeof(struct nlmsghdr))];
+
+ // Get interface ID.
+ ifindex = if_nametoindex(name);
+ if (ifindex == 0) {
+ return -errno;
+ }
+
+ // Convert string representation to sockaddr_storage.
+ ret = string_to_ip(address, &ss);
+ if (ret) {
+ return ret;
+ }
+
+ // Determine address type and length.
+ if (ss.ss_family == AF_INET) {
+ struct sockaddr_in *sin = (struct sockaddr_in *) &ss;
+ addr = &sin->sin_addr;
+ addrlen = INET_ADDRLEN;
+ } else if (ss.ss_family == AF_INET6) {
+ struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) &ss;
+ addr = &sin6->sin6_addr;
+ addrlen = INET6_ADDRLEN;
+ } else {
+ return -EAFNOSUPPORT;
+ }
+
+ // Fill in netlink structures.
+ memset(&req, 0, sizeof(req));
+
+ // Netlink message header.
+ req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.r));
+ req.n.nlmsg_type = action;
+ req.n.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
+ req.n.nlmsg_pid = getpid();
+
+ // Interface address message header.
+ req.r.ifa_family = ss.ss_family;
+ req.r.ifa_prefixlen = prefixlen;
+ req.r.ifa_index = ifindex;
+
+ // Routing attribute. Contains the actual IP address.
+ rta = (struct rtattr *) (((char *) &req) + NLMSG_ALIGN(req.n.nlmsg_len));
+ rta->rta_type = IFA_LOCAL;
+ rta->rta_len = RTA_LENGTH(addrlen);
+ req.n.nlmsg_len = NLMSG_ALIGN(req.n.nlmsg_len) + RTA_LENGTH(addrlen);
+ memcpy(RTA_DATA(rta), addr, addrlen);
+
+ s = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
+ if (send(s, &req, req.n.nlmsg_len, 0) < 0) {
+ close(s);
+ return -errno;
+ }
+
+ len = recv(s, buf, sizeof(buf), 0);
+ close(s);
+ if (len < 0) {
+ return -errno;
+ }
+
+ // Parse the acknowledgement to find the return code.
+ nh = (struct nlmsghdr *) buf;
+ if (!NLMSG_OK(nh, (unsigned) len) || nh->nlmsg_type != NLMSG_ERROR) {
+ return -EINVAL;
+ }
+ err = NLMSG_DATA(nh);
+
+ // Return code is negative errno.
+ return err->error;
+}
+
+int ifc_add_address(const char *name, const char *address, int prefixlen) {
+ return ifc_act_on_address(RTM_NEWADDR, name, address, prefixlen);
+}
+
+int ifc_del_address(const char *name, const char * address, int prefixlen) {
+ return ifc_act_on_address(RTM_DELADDR, name, address, prefixlen);
+}
+
+/*
+ * Clears IPv6 addresses on the specified interface.
+ */
+int ifc_clear_ipv6_addresses(const char *name) {
+ char rawaddrstr[INET6_ADDRSTRLEN], addrstr[INET6_ADDRSTRLEN];
+ unsigned int prefixlen;
+ int lasterror = 0, i, j, ret;
+ char ifname[64]; // Currently, IFNAMSIZ = 16.
+ FILE *f = fopen("/proc/net/if_inet6", "r");
+ if (!f) {
+ return -errno;
+ }
+
+ // Format:
+ // 20010db8000a0001fc446aa4b5b347ed 03 40 00 01 wlan0
+ while (fscanf(f, "%32s %*02x %02x %*02x %*02x %63s\n",
+ rawaddrstr, &prefixlen, ifname) == 3) {
+ // Is this the interface we're looking for?
+ if (strcmp(name, ifname)) {
+ continue;
+ }
+
+ // Put the colons back into the address.
+ for (i = 0, j = 0; i < 32; i++, j++) {
+ addrstr[j] = rawaddrstr[i];
+ if (i % 4 == 3) {
+ addrstr[++j] = ':';
+ }
+ }
+ addrstr[j - 1] = '\0';
+
+ // Don't delete the link-local address as well, or it will disable IPv6
+ // on the interface.
+ if (strncmp(addrstr, "fe80:", 5) == 0) {
+ continue;
+ }
+
+ ret = ifc_del_address(ifname, addrstr, prefixlen);
+ if (ret) {
+ LOGE("Deleting address %s/%d on %s: %s", addrstr, prefixlen, ifname,
+ strerror(-ret));
+ lasterror = ret;
+ }
+ }
+
+ fclose(f);
+ return lasterror;
+}
+
+/*
+ * Clears IPv4 addresses on the specified interface.
+ */
+void ifc_clear_ipv4_addresses(const char *name) {
+ unsigned count, addr;
+ ifc_init();
+ for (count=0, addr=1;((addr != 0) && (count < 255)); count++) {
+ if (ifc_get_addr(name, &addr) < 0)
+ break;
+ if (addr)
+ ifc_set_addr(name, 0);
+ }
+ ifc_close();
+}
+
+/*
+ * Clears all IP addresses on the specified interface.
+ */
+int ifc_clear_addresses(const char *name) {
+ ifc_clear_ipv4_addresses(name);
+ return ifc_clear_ipv6_addresses(name);
}
int ifc_set_hwaddr(const char *name, const void *ptr)
@@ -206,6 +426,19 @@ int ifc_set_hwaddr(const char *name, const void *ptr)
return ioctl(ifc_ctl_sock, SIOCSIFHWADDR, &ifr);
}
+int ifc_set_mask(const char *name, in_addr_t mask)
+{
+ struct ifreq ifr;
+ int ret;
+
+ ifc_init_ifr(name, &ifr);
+ init_sockaddr_in(&ifr.ifr_addr, mask);
+
+ ret = ioctl(ifc_ctl_sock, SIOCSIFNETMASK, &ifr);
+ if (DBG) printerr("ifc_set_mask(%s, xx) = %d", name, ret);
+ return ret;
+}
+
int ifc_set_prefixLength(const char *name, int prefixLength)
{
struct ifreq ifr;
@@ -313,6 +546,7 @@ int ifc_act_on_ipv4_route(int action, const char *ifname, struct in_addr dst, in
return result;
}
+/* deprecated - v4 only */
int ifc_create_default_route(const char *name, in_addr_t gw)
{
struct in_addr in_dst, in_gw;
@@ -320,7 +554,20 @@ int ifc_create_default_route(const char *name, in_addr_t gw)
in_dst.s_addr = 0;
in_gw.s_addr = gw;
- return ifc_act_on_route(SIOCADDRT, name, in_dst, 0, in_gw);
+ int ret = ifc_act_on_ipv4_route(SIOCADDRT, name, in_dst, 0, in_gw);
+ if (DBG) printerr("ifc_create_default_route(%s, %d) = %d", name, gw, ret);
+ return ret;
+}
+
+/* deprecated v4-only */
+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_act_on_ipv4_route(SIOCADDRT, name, in_dst, 32, in_gw);
}
int ifc_enable(const char *ifname)
@@ -449,6 +696,70 @@ int ifc_remove_host_routes(const char *name)
}
/*
+ * 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.
+ *
+ * DEPRECATED
+ */
+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.
+ * DEPRECATED
+ */
+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.
*/
int ifc_remove_default_route(const char *ifname)
@@ -627,9 +938,31 @@ int ifc_act_on_route(int action, const char *ifname, const char *dst, int prefix
return ret;
}
+/*
+ * DEPRECATED
+ */
+int ifc_add_ipv4_route(const char *ifname, struct in_addr dst, int prefix_length,
+ struct in_addr gw)
+{
+ int i =ifc_act_on_ipv4_route(SIOCADDRT, ifname, dst, prefix_length, gw);
+ if (DBG) printerr("ifc_add_ipv4_route(%s, xx, %d, xx) = %d", ifname, prefix_length, i);
+ return i;
+}
+
+/*
+ * DEPRECATED
+ */
+int ifc_add_ipv6_route(const char *ifname, struct in6_addr dst, int prefix_length,
+ struct in6_addr gw)
+{
+ return ifc_act_on_ipv6_route(SIOCADDRT, ifname, dst, prefix_length, gw);
+}
+
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 i = ifc_act_on_route(SIOCADDRT, ifname, dst, prefix_length, gw);
+ if (DBG) printerr("ifc_add_route(%s, %s, %d, %s) = %d", ifname, dst, prefix_length, gw, i);
+ return i;
}
int ifc_remove_route(const char *ifname, const char*dst, int prefix_length, const char *gw)