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> 19 #include <net/if.h> 20 #include <netinet/in.h> 21 22 #include <linux/rtnetlink.h> 23 #include <netlink/handlers.h> 24 #include <netlink/msg.h> 25 26 #include "logging.h" 27 #include "netlink_msg.h" 28 29 #define DEBUG_OPTNAME(a) \ 30 case (a): { \ 31 optname = #a; \ 32 break; \ 33 } 34 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 */ add_address(const char * ifname,int family,const void * address,int prefixlen,const void * broadcast)43 int add_address(const char *ifname, int family, const void *address, int prefixlen, 44 const void *broadcast) { 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); 51 if (addr_size == 0) { 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 } 61 ifa.ifa_family = family; 62 ifa.ifa_prefixlen = prefixlen; 63 ifa.ifa_scope = RT_SCOPE_UNIVERSE; 64 65 msg = 66 nlmsg_alloc_ifaddr(RTM_NEWADDR, NLM_F_ACK | NLM_F_REQUEST | NLM_F_CREATE | NLM_F_REPLACE, &ifa); 67 if (!msg) { 68 retval = -ENOMEM; 69 goto cleanup; 70 } 71 72 if (nla_put(msg, IFA_LOCAL, addr_size, address) < 0) { 73 retval = -ENOMEM; 74 goto cleanup; 75 } 76 if (family == AF_INET6) { 77 // AF_INET6 gets IFA_LOCAL + IFA_ADDRESS 78 if (nla_put(msg, IFA_ADDRESS, addr_size, address) < 0) { 79 retval = -ENOMEM; 80 goto cleanup; 81 } 82 } else if (family == AF_INET) { 83 // AF_INET gets IFA_LOCAL + IFA_BROADCAST 84 if (nla_put(msg, IFA_BROADCAST, addr_size, broadcast) < 0) { 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: 96 if (msg) nlmsg_free(msg); 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 */ if_up(const char * ifname,int mtu)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; 117 ifi.ifi_flags = IFF_UP; 118 119 msg = nlmsg_alloc_ifinfo(RTM_SETLINK, NLM_F_ACK | NLM_F_REQUEST | NLM_F_ROOT, &ifi); 120 if (!msg) { 121 retval = -ENOMEM; 122 goto cleanup; 123 } 124 125 if (nla_put(msg, IFLA_MTU, 4, &mtu) < 0) { 126 retval = -ENOMEM; 127 goto cleanup; 128 } 129 130 retval = netlink_sendrecv(msg); 131 132 cleanup: 133 if (msg) nlmsg_free(msg); 134 135 return retval; 136 } 137 do_anycast_setsockopt(int sock,int what,struct in6_addr * addr,int ifindex)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 */ add_anycast_address(int sock,struct in6_addr * addr,const char * ifname)165 int add_anycast_address(int sock, struct in6_addr *addr, const char *ifname) { 166 int ifindex; 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 */ del_anycast_address(int sock,struct in6_addr * addr)182 int del_anycast_address(int sock, struct in6_addr *addr) { 183 return do_anycast_setsockopt(sock, IPV6_LEAVE_ANYCAST, addr, 0); 184 } 185