blob: 184a46b24a853d4951857f0107a91308434e9057 [file] [log] [blame]
Daniel Drowna45056e2012-03-23 10:42:54 -05001/*
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 Drowna45056e2012-03-23 10:42:54 -050019#include <net/if.h>
junyulaic4e591a2018-11-26 22:36:10 +090020#include <netinet/in.h>
Daniel Drowna45056e2012-03-23 10:42:54 -050021
22#include <linux/rtnetlink.h>
23#include <netlink/handlers.h>
24#include <netlink/msg.h>
25
Lorenzo Colitti8a41a5d2014-10-21 12:37:48 +090026#include "logging.h"
Daniel Drowna45056e2012-03-23 10:42:54 -050027#include "netlink_msg.h"
28
junyulaic4e591a2018-11-26 22:36:10 +090029#define DEBUG_OPTNAME(a) \
30 case (a): { \
31 optname = #a; \
32 break; \
33 }
Lorenzo Colitti8a41a5d2014-10-21 12:37:48 +090034
Daniel Drowna45056e2012-03-23 10:42:54 -050035/* 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 */
junyulaic4e591a2018-11-26 22:36:10 +090043int add_address(const char *ifname, int family, const void *address, int prefixlen,
44 const void *broadcast) {
Daniel Drowna45056e2012-03-23 10:42:54 -050045 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);
junyulaic4e591a2018-11-26 22:36:10 +090051 if (addr_size == 0) {
Daniel Drowna45056e2012-03-23 10:42:54 -050052 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 }
junyulaic4e591a2018-11-26 22:36:10 +090061 ifa.ifa_family = family;
Daniel Drowna45056e2012-03-23 10:42:54 -050062 ifa.ifa_prefixlen = prefixlen;
junyulaic4e591a2018-11-26 22:36:10 +090063 ifa.ifa_scope = RT_SCOPE_UNIVERSE;
Daniel Drowna45056e2012-03-23 10:42:54 -050064
junyulaic4e591a2018-11-26 22:36:10 +090065 msg =
66 nlmsg_alloc_ifaddr(RTM_NEWADDR, NLM_F_ACK | NLM_F_REQUEST | NLM_F_CREATE | NLM_F_REPLACE, &ifa);
67 if (!msg) {
Daniel Drowna45056e2012-03-23 10:42:54 -050068 retval = -ENOMEM;
69 goto cleanup;
70 }
71
junyulaic4e591a2018-11-26 22:36:10 +090072 if (nla_put(msg, IFA_LOCAL, addr_size, address) < 0) {
Daniel Drowna45056e2012-03-23 10:42:54 -050073 retval = -ENOMEM;
74 goto cleanup;
75 }
junyulaic4e591a2018-11-26 22:36:10 +090076 if (family == AF_INET6) {
Daniel Drowna45056e2012-03-23 10:42:54 -050077 // AF_INET6 gets IFA_LOCAL + IFA_ADDRESS
junyulaic4e591a2018-11-26 22:36:10 +090078 if (nla_put(msg, IFA_ADDRESS, addr_size, address) < 0) {
Daniel Drowna45056e2012-03-23 10:42:54 -050079 retval = -ENOMEM;
80 goto cleanup;
81 }
junyulaic4e591a2018-11-26 22:36:10 +090082 } else if (family == AF_INET) {
Daniel Drowna45056e2012-03-23 10:42:54 -050083 // AF_INET gets IFA_LOCAL + IFA_BROADCAST
junyulaic4e591a2018-11-26 22:36:10 +090084 if (nla_put(msg, IFA_BROADCAST, addr_size, broadcast) < 0) {
Daniel Drowna45056e2012-03-23 10:42:54 -050085 retval = -ENOMEM;
86 goto cleanup;
87 }
88 } else {
89 retval = -EAFNOSUPPORT;
90 goto cleanup;
91 }
92
93 retval = netlink_sendrecv(msg);
94
95cleanup:
junyulaic4e591a2018-11-26 22:36:10 +090096 if (msg) nlmsg_free(msg);
Daniel Drowna45056e2012-03-23 10:42:54 -050097
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 */
106int 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;
junyulaic4e591a2018-11-26 22:36:10 +0900117 ifi.ifi_flags = IFF_UP;
Daniel Drowna45056e2012-03-23 10:42:54 -0500118
119 msg = nlmsg_alloc_ifinfo(RTM_SETLINK, NLM_F_ACK | NLM_F_REQUEST | NLM_F_ROOT, &ifi);
junyulaic4e591a2018-11-26 22:36:10 +0900120 if (!msg) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500121 retval = -ENOMEM;
122 goto cleanup;
123 }
124
junyulaic4e591a2018-11-26 22:36:10 +0900125 if (nla_put(msg, IFLA_MTU, 4, &mtu) < 0) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500126 retval = -ENOMEM;
127 goto cleanup;
128 }
129
130 retval = netlink_sendrecv(msg);
131
132cleanup:
junyulaic4e591a2018-11-26 22:36:10 +0900133 if (msg) nlmsg_free(msg);
Daniel Drowna45056e2012-03-23 10:42:54 -0500134
135 return retval;
136}
Lorenzo Colitti8a41a5d2014-10-21 12:37:48 +0900137
138static 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 */
165int add_anycast_address(int sock, struct in6_addr *addr, const char *ifname) {
Dan Albert6ea37342015-02-19 17:46:01 -0800166 int ifindex;
Lorenzo Colitti8a41a5d2014-10-21 12:37:48 +0900167
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 */
182int del_anycast_address(int sock, struct in6_addr *addr) {
183 return do_anycast_setsockopt(sock, IPV6_LEAVE_ANYCAST, addr, 0);
184}