Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 Daniel Drown <dan-android@drown.org> |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | * |
| 16 | * setif.c - network interface configuration |
| 17 | */ |
| 18 | #include <errno.h> |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 19 | #include <net/if.h> |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 20 | #include <netinet/in.h> |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 21 | |
| 22 | #include <linux/rtnetlink.h> |
| 23 | #include <netlink/handlers.h> |
| 24 | #include <netlink/msg.h> |
| 25 | |
Lorenzo Colitti | 8a41a5d | 2014-10-21 12:37:48 +0900 | [diff] [blame] | 26 | #include "logging.h" |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 27 | #include "netlink_msg.h" |
| 28 | |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 29 | #define DEBUG_OPTNAME(a) \ |
| 30 | case (a): { \ |
| 31 | optname = #a; \ |
| 32 | break; \ |
| 33 | } |
Lorenzo Colitti | 8a41a5d | 2014-10-21 12:37:48 +0900 | [diff] [blame] | 34 | |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 35 | /* function: add_address |
| 36 | * adds an IP address to/from an interface, returns 0 on success and <0 on failure |
| 37 | * ifname - name of interface to change |
| 38 | * family - address family (AF_INET, AF_INET6) |
| 39 | * address - pointer to a struct in_addr or in6_addr |
| 40 | * prefixlen - bitlength of network (example: 24 for AF_INET's 255.255.255.0) |
| 41 | * broadcast - broadcast address (only for AF_INET, ignored for AF_INET6) |
| 42 | */ |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 43 | int add_address(const char *ifname, int family, const void *address, int prefixlen, |
| 44 | const void *broadcast) { |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 45 | int retval; |
| 46 | size_t addr_size; |
| 47 | struct ifaddrmsg ifa; |
| 48 | struct nl_msg *msg = NULL; |
| 49 | |
| 50 | addr_size = inet_family_size(family); |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 51 | if (addr_size == 0) { |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 52 | retval = -EAFNOSUPPORT; |
| 53 | goto cleanup; |
| 54 | } |
| 55 | |
| 56 | memset(&ifa, 0, sizeof(ifa)); |
| 57 | if (!(ifa.ifa_index = if_nametoindex(ifname))) { |
| 58 | retval = -ENODEV; |
| 59 | goto cleanup; |
| 60 | } |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 61 | ifa.ifa_family = family; |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 62 | ifa.ifa_prefixlen = prefixlen; |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 63 | ifa.ifa_scope = RT_SCOPE_UNIVERSE; |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 64 | |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 65 | msg = |
| 66 | nlmsg_alloc_ifaddr(RTM_NEWADDR, NLM_F_ACK | NLM_F_REQUEST | NLM_F_CREATE | NLM_F_REPLACE, &ifa); |
| 67 | if (!msg) { |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 68 | retval = -ENOMEM; |
| 69 | goto cleanup; |
| 70 | } |
| 71 | |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 72 | if (nla_put(msg, IFA_LOCAL, addr_size, address) < 0) { |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 73 | retval = -ENOMEM; |
| 74 | goto cleanup; |
| 75 | } |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 76 | if (family == AF_INET6) { |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 77 | // AF_INET6 gets IFA_LOCAL + IFA_ADDRESS |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 78 | if (nla_put(msg, IFA_ADDRESS, addr_size, address) < 0) { |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 79 | retval = -ENOMEM; |
| 80 | goto cleanup; |
| 81 | } |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 82 | } else if (family == AF_INET) { |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 83 | // AF_INET gets IFA_LOCAL + IFA_BROADCAST |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 84 | if (nla_put(msg, IFA_BROADCAST, addr_size, broadcast) < 0) { |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 85 | retval = -ENOMEM; |
| 86 | goto cleanup; |
| 87 | } |
| 88 | } else { |
| 89 | retval = -EAFNOSUPPORT; |
| 90 | goto cleanup; |
| 91 | } |
| 92 | |
| 93 | retval = netlink_sendrecv(msg); |
| 94 | |
| 95 | cleanup: |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 96 | if (msg) nlmsg_free(msg); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 97 | |
| 98 | return retval; |
| 99 | } |
| 100 | |
| 101 | /* function: if_up |
| 102 | * sets interface link state to up and sets mtu, returns 0 on success and <0 on failure |
| 103 | * ifname - interface name to change |
| 104 | * mtu - new mtu |
| 105 | */ |
| 106 | int if_up(const char *ifname, int mtu) { |
| 107 | int retval = -1; |
| 108 | struct ifinfomsg ifi; |
| 109 | struct nl_msg *msg = NULL; |
| 110 | |
| 111 | memset(&ifi, 0, sizeof(ifi)); |
| 112 | if (!(ifi.ifi_index = if_nametoindex(ifname))) { |
| 113 | retval = -ENODEV; |
| 114 | goto cleanup; |
| 115 | } |
| 116 | ifi.ifi_change = IFF_UP; |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 117 | ifi.ifi_flags = IFF_UP; |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 118 | |
| 119 | msg = nlmsg_alloc_ifinfo(RTM_SETLINK, NLM_F_ACK | NLM_F_REQUEST | NLM_F_ROOT, &ifi); |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 120 | if (!msg) { |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 121 | retval = -ENOMEM; |
| 122 | goto cleanup; |
| 123 | } |
| 124 | |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 125 | if (nla_put(msg, IFLA_MTU, 4, &mtu) < 0) { |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 126 | retval = -ENOMEM; |
| 127 | goto cleanup; |
| 128 | } |
| 129 | |
| 130 | retval = netlink_sendrecv(msg); |
| 131 | |
| 132 | cleanup: |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 133 | if (msg) nlmsg_free(msg); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 134 | |
| 135 | return retval; |
| 136 | } |
Lorenzo Colitti | 8a41a5d | 2014-10-21 12:37:48 +0900 | [diff] [blame] | 137 | |
| 138 | static int do_anycast_setsockopt(int sock, int what, struct in6_addr *addr, int ifindex) { |
| 139 | struct ipv6_mreq mreq = { *addr, ifindex }; |
| 140 | char *optname; |
| 141 | int ret; |
| 142 | |
| 143 | switch (what) { |
| 144 | DEBUG_OPTNAME(IPV6_JOIN_ANYCAST) |
| 145 | DEBUG_OPTNAME(IPV6_LEAVE_ANYCAST) |
| 146 | default: |
| 147 | optname = "???"; |
| 148 | break; |
| 149 | } |
| 150 | |
| 151 | ret = setsockopt(sock, SOL_IPV6, what, &mreq, sizeof(mreq)); |
| 152 | if (ret) { |
| 153 | logmsg(ANDROID_LOG_ERROR, "%s: setsockopt(%s): %s", __func__, optname, strerror(errno)); |
| 154 | } |
| 155 | |
| 156 | return ret; |
| 157 | } |
| 158 | |
| 159 | /* function: add_anycast_address |
| 160 | * adds an anycast IPv6 address to an interface, returns 0 on success and <0 on failure |
| 161 | * sock - the socket to add the address to |
| 162 | * addr - the IP address to add |
| 163 | * ifname - name of interface to add the address to |
| 164 | */ |
| 165 | int add_anycast_address(int sock, struct in6_addr *addr, const char *ifname) { |
Dan Albert | 6ea3734 | 2015-02-19 17:46:01 -0800 | [diff] [blame] | 166 | int ifindex; |
Lorenzo Colitti | 8a41a5d | 2014-10-21 12:37:48 +0900 | [diff] [blame] | 167 | |
| 168 | ifindex = if_nametoindex(ifname); |
| 169 | if (!ifindex) { |
| 170 | logmsg(ANDROID_LOG_ERROR, "%s: unknown ifindex for interface %s", __func__, ifname); |
| 171 | return -ENODEV; |
| 172 | } |
| 173 | |
| 174 | return do_anycast_setsockopt(sock, IPV6_JOIN_ANYCAST, addr, ifindex); |
| 175 | } |
| 176 | |
| 177 | /* function: del_anycast_address |
| 178 | * removes an anycast IPv6 address from the system, returns 0 on success and <0 on failure |
| 179 | * sock - the socket to remove from, must have had the address added via add_anycast_address |
| 180 | * addr - the IP address to remove |
| 181 | */ |
| 182 | int del_anycast_address(int sock, struct in6_addr *addr) { |
| 183 | return do_anycast_setsockopt(sock, IPV6_LEAVE_ANYCAST, addr, 0); |
| 184 | } |