[RFCLAT#10] remove libnl related files

libnl is not required anymore. remove all related files.

Bug: 212345928
Test: atest clatd_test and manual test
1. Connect to ipv6-only wifi.
2. Try IPv4 traffic.
   $ ping 8.8.8.8

Change-Id: Ic4725ef01667576f2a958f38d458374c2e749569
diff --git a/Android.bp b/Android.bp
index 9d2d85e..0eb5298 100644
--- a/Android.bp
+++ b/Android.bp
@@ -50,9 +50,6 @@
         "ipv4.c",
         "ipv6.c",
         "logging.c",
-        "netlink_callbacks.c",
-        "netlink_msg.c",
-        "setif.c",
         "translate.c",
     ],
 }
@@ -67,7 +64,6 @@
     ],
     static_libs: [
         "libip_checksum",
-        "libnl",
     ],
     shared_libs: [
         "libcutils",
@@ -108,7 +104,6 @@
         "libbase",
         "libip_checksum",
         "libnetd_test_tun_interface",
-        "libnl",
     ],
     shared_libs: [
         "libcutils",
diff --git a/clatd.c b/clatd.c
index bc6b652..bb6aedc 100644
--- a/clatd.c
+++ b/clatd.c
@@ -47,7 +47,6 @@
 #include "dump.h"
 #include "getaddr.h"
 #include "logging.h"
-#include "setif.h"
 #include "translate.h"
 
 struct clat_config Global_Clatd_Config;
diff --git a/getaddr.c b/getaddr.c
index f25efc1..ba8052d 100644
--- a/getaddr.c
+++ b/getaddr.c
@@ -22,8 +22,7 @@
 #include <linux/rtnetlink.h>
 #include <net/if.h>
 #include <netinet/in.h>
-#include <netlink/handlers.h>
-#include <netlink/msg.h>
+#include <stdlib.h>
 #include <string.h>
 #include <strings.h>
 #include <unistd.h>
diff --git a/main.c b/main.c
index 069592f..74fb0c2 100644
--- a/main.c
+++ b/main.c
@@ -31,7 +31,6 @@
 #include "common.h"
 #include "config.h"
 #include "logging.h"
-#include "setif.h"
 
 #define DEVICEPREFIX "v4-"
 
diff --git a/netlink_callbacks.c b/netlink_callbacks.c
deleted file mode 100644
index 804976d..0000000
--- a/netlink_callbacks.c
+++ /dev/null
@@ -1,67 +0,0 @@
-/*
- * Copyright 2012 Daniel Drown <dan-android@drown.org>
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- * netlink_callbacks.c - generic callbacks for netlink responses
- */
-#include <net/if.h>
-#include <netinet/in.h>
-
-#include <linux/rtnetlink.h>
-#include <netlink/handlers.h>
-#include <netlink/msg.h>
-
-/* function: ack_handler
- * generic netlink callback for ack messages
- * msg  - netlink message
- * data - pointer to an int, stores the success code
- */
-static int ack_handler(__attribute__((unused)) struct nl_msg *msg, void *data) {
-  int *retval = data;
-  *retval     = 0;
-  return NL_OK;
-}
-
-/* function: error_handler
- * generic netlink callback for error messages
- * nla  - error source
- * err  - netlink error message
- * arg  - pointer to an int, stores the error code
- */
-static int error_handler(__attribute__((unused)) struct sockaddr_nl *nla, struct nlmsgerr *err,
-                         void *arg) {
-  int *retval = arg;
-  if (err->error < 0) {
-    *retval = err->error;
-  } else {
-    *retval = 0;  // NLMSG_ERROR used as reply type on no error
-  }
-  return NL_OK;
-}
-
-/* function: alloc_ack_callbacks
- * allocates a set of netlink callbacks.  returns NULL on failure.  callbacks will modify retval
- * with <0 meaning failure retval - shared state between caller and callback functions
- */
-struct nl_cb *alloc_ack_callbacks(int *retval) {
-  struct nl_cb *callbacks;
-
-  callbacks = nl_cb_alloc(NL_CB_DEFAULT);
-  if (!callbacks) {
-    return NULL;
-  }
-  nl_cb_set(callbacks, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, retval);
-  nl_cb_err(callbacks, NL_CB_CUSTOM, error_handler, retval);
-  return callbacks;
-}
diff --git a/netlink_callbacks.h b/netlink_callbacks.h
deleted file mode 100644
index 298ad3e..0000000
--- a/netlink_callbacks.h
+++ /dev/null
@@ -1,24 +0,0 @@
-/*
- * Copyright 2012 Daniel Drown <dan-android@drown.org>
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- * netlink_callbacks.h - callbacks for netlink responses
- */
-
-#ifndef __NETLINK_CALLBACKS_H__
-#define __NETLINK_CALLBACKS_H__
-
-struct nl_cb *alloc_ack_callbacks(int *retval);
-
-#endif
diff --git a/netlink_msg.c b/netlink_msg.c
deleted file mode 100644
index be76ecd..0000000
--- a/netlink_msg.c
+++ /dev/null
@@ -1,182 +0,0 @@
-/*
- * Copyright 2012 Daniel Drown
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- * netlink_msg.c - send an ifaddrmsg/ifinfomsg/rtmsg via netlink
- */
-
-#include <errno.h>
-#include <linux/netlink.h>
-#include <linux/rtnetlink.h>
-#include <netinet/in.h>
-#include <string.h>
-
-#include <netlink-private/object-api.h>
-#include <netlink-private/types.h>
-#include <netlink/msg.h>
-#include <netlink/netlink.h>
-#include <netlink/socket.h>
-
-#include "netlink_callbacks.h"
-#include "netlink_msg.h"
-
-/* function: family_size
- * returns the size of the address structure for the given family, or 0 on error
- * family - AF_INET or AF_INET6
- */
-size_t inet_family_size(int family) {
-  if (family == AF_INET) {
-    return sizeof(struct in_addr);
-  } else if (family == AF_INET6) {
-    return sizeof(struct in6_addr);
-  } else {
-    return 0;
-  }
-}
-
-/* function: nlmsg_alloc_generic
- * allocates a netlink message with the given struct inside of it. returns NULL on failure
- * type           - netlink message type
- * flags          - netlink message flags
- * payload_struct - pointer to a struct to add to netlink message
- * payload_len    - bytelength of structure
- */
-struct nl_msg *nlmsg_alloc_generic(uint16_t type, uint16_t flags, void *payload_struct,
-                                   size_t payload_len) {
-  struct nl_msg *msg;
-
-  msg = nlmsg_alloc();
-  if (!msg) {
-    return NULL;
-  }
-
-  if ((sizeof(struct nl_msg) + payload_len) > msg->nm_size) {
-    nlmsg_free(msg);
-    return NULL;
-  }
-
-  msg->nm_nlh->nlmsg_len   = NLMSG_LENGTH(payload_len);
-  msg->nm_nlh->nlmsg_flags = flags;
-  msg->nm_nlh->nlmsg_type  = type;
-
-  memcpy(nlmsg_data(msg->nm_nlh), payload_struct, payload_len);
-
-  return msg;
-}
-
-/* function: nlmsg_alloc_ifaddr
- * allocates a netlink message with a struct ifaddrmsg inside of it. returns NULL on failure
- * type  - netlink message type
- * flags - netlink message flags
- * ifa   - ifaddrmsg to copy into the new netlink message
- */
-struct nl_msg *nlmsg_alloc_ifaddr(uint16_t type, uint16_t flags, struct ifaddrmsg *ifa) {
-  return nlmsg_alloc_generic(type, flags, ifa, sizeof(*ifa));
-}
-
-/* function: nlmsg_alloc_ifinfo
- * allocates a netlink message with a struct ifinfomsg inside of it. returns NULL on failure
- * type  - netlink message type
- * flags - netlink message flags
- * ifi   - ifinfomsg to copy into the new netlink message
- */
-struct nl_msg *nlmsg_alloc_ifinfo(uint16_t type, uint16_t flags, struct ifinfomsg *ifi) {
-  return nlmsg_alloc_generic(type, flags, ifi, sizeof(*ifi));
-}
-
-/* function: nlmsg_alloc_rtmsg
- * allocates a netlink message with a struct rtmsg inside of it. returns NULL on failure
- * type  - netlink message type
- * flags - netlink message flags
- * rt    - rtmsg to copy into the new netlink message
- */
-struct nl_msg *nlmsg_alloc_rtmsg(uint16_t type, uint16_t flags, struct rtmsg *rt) {
-  return nlmsg_alloc_generic(type, flags, rt, sizeof(*rt));
-}
-
-/* function: netlink_set_kernel_only
- * sets a socket to receive messages only from the kernel
- * sock - socket to connect
- */
-int netlink_set_kernel_only(struct nl_sock *nl_sk) {
-  struct sockaddr_nl addr = { AF_NETLINK, 0, 0, 0 };
-
-  if (!nl_sk) {
-    return -EFAULT;
-  }
-
-  int sockfd = nl_socket_get_fd(nl_sk);
-  return connect(sockfd, (struct sockaddr *)&addr, sizeof(addr));
-}
-
-/* function: send_netlink_msg
- * sends a netlink message, reads a response, and hands the response(s) to the callbacks
- * msg       - netlink message to send
- * callbacks - callbacks to use on responses
- */
-void send_netlink_msg(struct nl_msg *msg, struct nl_cb *callbacks) {
-  struct nl_sock *nl_sk;
-
-  nl_sk = nl_socket_alloc();
-  if (!nl_sk) goto cleanup;
-
-  if (nl_connect(nl_sk, NETLINK_ROUTE) != 0) goto cleanup;
-
-  if (nl_send_auto_complete(nl_sk, msg) < 0) goto cleanup;
-
-  if (netlink_set_kernel_only(nl_sk) < 0) goto cleanup;
-
-  nl_recvmsgs(nl_sk, callbacks);
-
-cleanup:
-  if (nl_sk) nl_socket_free(nl_sk);
-}
-
-/* function: send_ifaddrmsg
- * sends a netlink/ifaddrmsg message and hands the responses to the callbacks
- * type      - netlink message type
- * flags     - netlink message flags
- * ifa       - ifaddrmsg to send
- * callbacks - callbacks to use with the responses
- */
-void send_ifaddrmsg(uint16_t type, uint16_t flags, struct ifaddrmsg *ifa, struct nl_cb *callbacks) {
-  struct nl_msg *msg = NULL;
-
-  msg = nlmsg_alloc_ifaddr(type, flags, ifa);
-  if (!msg) return;
-
-  send_netlink_msg(msg, callbacks);
-
-  nlmsg_free(msg);
-}
-
-/* function: netlink_sendrecv
- * send a nl_msg and return an int status - only supports OK/ERROR responses
- * msg - msg to send
- */
-int netlink_sendrecv(struct nl_msg *msg) {
-  struct nl_cb *callbacks = NULL;
-  int retval              = -EIO;
-
-  callbacks = alloc_ack_callbacks(&retval);
-  if (!callbacks) {
-    return -ENOMEM;
-  }
-
-  send_netlink_msg(msg, callbacks);
-
-  nl_cb_put(callbacks);
-
-  return retval;
-}
diff --git a/netlink_msg.h b/netlink_msg.h
deleted file mode 100644
index 13e1f28..0000000
--- a/netlink_msg.h
+++ /dev/null
@@ -1,30 +0,0 @@
-/*
- * Copyright 2012 Daniel Drown
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- * netlink_msg.h - send an ifaddrmsg/ifinfomsg via netlink
- */
-#ifndef __NETLINK_IFMSG_H__
-#define __NETLINK_IFMSG_H__
-
-size_t inet_family_size(int family);
-struct nl_msg *nlmsg_alloc_ifaddr(uint16_t type, uint16_t flags, struct ifaddrmsg *ifa);
-struct nl_msg *nlmsg_alloc_ifinfo(uint16_t type, uint16_t flags, struct ifinfomsg *ifi);
-struct nl_msg *nlmsg_alloc_rtmsg(uint16_t type, uint16_t flags, struct rtmsg *rt);
-void send_netlink_msg(struct nl_msg *msg, struct nl_cb *callbacks);
-void send_ifaddrmsg(uint16_t type, uint16_t flags, struct ifaddrmsg *ifa, struct nl_cb *callbacks);
-int netlink_sendrecv(struct nl_msg *msg);
-int netlink_set_kernel_only(struct nl_sock *nl_sk);
-
-#endif
diff --git a/setif.c b/setif.c
deleted file mode 100644
index 184a46b..0000000
--- a/setif.c
+++ /dev/null
@@ -1,184 +0,0 @@
-/*
- * Copyright 2012 Daniel Drown <dan-android@drown.org>
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- * setif.c - network interface configuration
- */
-#include <errno.h>
-#include <net/if.h>
-#include <netinet/in.h>
-
-#include <linux/rtnetlink.h>
-#include <netlink/handlers.h>
-#include <netlink/msg.h>
-
-#include "logging.h"
-#include "netlink_msg.h"
-
-#define DEBUG_OPTNAME(a)                                                                           \
-  case (a): {                                                                                      \
-    optname = #a;                                                                                  \
-    break;                                                                                         \
-  }
-
-/* function: add_address
- * adds an IP address to/from an interface, returns 0 on success and <0 on failure
- * ifname    - name of interface to change
- * family    - address family (AF_INET, AF_INET6)
- * address   - pointer to a struct in_addr or in6_addr
- * prefixlen - bitlength of network (example: 24 for AF_INET's 255.255.255.0)
- * broadcast - broadcast address (only for AF_INET, ignored for AF_INET6)
- */
-int add_address(const char *ifname, int family, const void *address, int prefixlen,
-                const void *broadcast) {
-  int retval;
-  size_t addr_size;
-  struct ifaddrmsg ifa;
-  struct nl_msg *msg = NULL;
-
-  addr_size = inet_family_size(family);
-  if (addr_size == 0) {
-    retval = -EAFNOSUPPORT;
-    goto cleanup;
-  }
-
-  memset(&ifa, 0, sizeof(ifa));
-  if (!(ifa.ifa_index = if_nametoindex(ifname))) {
-    retval = -ENODEV;
-    goto cleanup;
-  }
-  ifa.ifa_family    = family;
-  ifa.ifa_prefixlen = prefixlen;
-  ifa.ifa_scope     = RT_SCOPE_UNIVERSE;
-
-  msg =
-    nlmsg_alloc_ifaddr(RTM_NEWADDR, NLM_F_ACK | NLM_F_REQUEST | NLM_F_CREATE | NLM_F_REPLACE, &ifa);
-  if (!msg) {
-    retval = -ENOMEM;
-    goto cleanup;
-  }
-
-  if (nla_put(msg, IFA_LOCAL, addr_size, address) < 0) {
-    retval = -ENOMEM;
-    goto cleanup;
-  }
-  if (family == AF_INET6) {
-    // AF_INET6 gets IFA_LOCAL + IFA_ADDRESS
-    if (nla_put(msg, IFA_ADDRESS, addr_size, address) < 0) {
-      retval = -ENOMEM;
-      goto cleanup;
-    }
-  } else if (family == AF_INET) {
-    // AF_INET gets IFA_LOCAL + IFA_BROADCAST
-    if (nla_put(msg, IFA_BROADCAST, addr_size, broadcast) < 0) {
-      retval = -ENOMEM;
-      goto cleanup;
-    }
-  } else {
-    retval = -EAFNOSUPPORT;
-    goto cleanup;
-  }
-
-  retval = netlink_sendrecv(msg);
-
-cleanup:
-  if (msg) nlmsg_free(msg);
-
-  return retval;
-}
-
-/* function: if_up
- * sets interface link state to up and sets mtu, returns 0 on success and <0 on failure
- * ifname - interface name to change
- * mtu    - new mtu
- */
-int if_up(const char *ifname, int mtu) {
-  int retval = -1;
-  struct ifinfomsg ifi;
-  struct nl_msg *msg = NULL;
-
-  memset(&ifi, 0, sizeof(ifi));
-  if (!(ifi.ifi_index = if_nametoindex(ifname))) {
-    retval = -ENODEV;
-    goto cleanup;
-  }
-  ifi.ifi_change = IFF_UP;
-  ifi.ifi_flags  = IFF_UP;
-
-  msg = nlmsg_alloc_ifinfo(RTM_SETLINK, NLM_F_ACK | NLM_F_REQUEST | NLM_F_ROOT, &ifi);
-  if (!msg) {
-    retval = -ENOMEM;
-    goto cleanup;
-  }
-
-  if (nla_put(msg, IFLA_MTU, 4, &mtu) < 0) {
-    retval = -ENOMEM;
-    goto cleanup;
-  }
-
-  retval = netlink_sendrecv(msg);
-
-cleanup:
-  if (msg) nlmsg_free(msg);
-
-  return retval;
-}
-
-static int do_anycast_setsockopt(int sock, int what, struct in6_addr *addr, int ifindex) {
-  struct ipv6_mreq mreq = { *addr, ifindex };
-  char *optname;
-  int ret;
-
-  switch (what) {
-    DEBUG_OPTNAME(IPV6_JOIN_ANYCAST)
-    DEBUG_OPTNAME(IPV6_LEAVE_ANYCAST)
-    default:
-      optname = "???";
-      break;
-  }
-
-  ret = setsockopt(sock, SOL_IPV6, what, &mreq, sizeof(mreq));
-  if (ret) {
-    logmsg(ANDROID_LOG_ERROR, "%s: setsockopt(%s): %s", __func__, optname, strerror(errno));
-  }
-
-  return ret;
-}
-
-/* function: add_anycast_address
- * adds an anycast IPv6 address to an interface, returns 0 on success and <0 on failure
- * sock      - the socket to add the address to
- * addr      - the IP address to add
- * ifname    - name of interface to add the address to
- */
-int add_anycast_address(int sock, struct in6_addr *addr, const char *ifname) {
-  int ifindex;
-
-  ifindex = if_nametoindex(ifname);
-  if (!ifindex) {
-    logmsg(ANDROID_LOG_ERROR, "%s: unknown ifindex for interface %s", __func__, ifname);
-    return -ENODEV;
-  }
-
-  return do_anycast_setsockopt(sock, IPV6_JOIN_ANYCAST, addr, ifindex);
-}
-
-/* function: del_anycast_address
- * removes an anycast IPv6 address from the system, returns 0 on success and <0 on failure
- * sock      - the socket to remove from, must have had the address added via add_anycast_address
- * addr      - the IP address to remove
- */
-int del_anycast_address(int sock, struct in6_addr *addr) {
-  return do_anycast_setsockopt(sock, IPV6_LEAVE_ANYCAST, addr, 0);
-}
diff --git a/setif.h b/setif.h
deleted file mode 100644
index 2d0adb1..0000000
--- a/setif.h
+++ /dev/null
@@ -1,28 +0,0 @@
-/*
- * Copyright 2012 Daniel Drown
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- * setif.h - network interface configuration
- */
-#ifndef __SETIF_H__
-#define __SETIF_H__
-
-int add_address(const char *ifname, int family, const void *address, int cidr,
-                const void *broadcast);
-int if_up(const char *ifname, int mtu);
-
-int add_anycast_address(int sock, const struct in6_addr *addr, const char *interface);
-int del_anycast_address(int sock, const struct in6_addr *addr);
-
-#endif