Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 Daniel Drown |
| 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 | * getaddr.c - get a locally configured address |
| 17 | */ |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 18 | #include <net/if.h> |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 19 | #include <netinet/in.h> |
| 20 | #include <string.h> |
| 21 | #include <strings.h> |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 22 | |
Paul Stewart | c2f9edd | 2016-11-11 11:58:52 -0800 | [diff] [blame] | 23 | #include <linux/if_addr.h> |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 24 | #include <linux/rtnetlink.h> |
| 25 | #include <netlink/handlers.h> |
| 26 | #include <netlink/msg.h> |
| 27 | |
| 28 | #include "getaddr.h" |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 29 | #include "logging.h" |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 30 | #include "netlink_msg.h" |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 31 | |
| 32 | // shared state between getinterface_ip and getaddr_cb |
| 33 | struct target { |
| 34 | int family; |
| 35 | unsigned int ifindex; |
| 36 | union anyip ip; |
| 37 | int foundip; |
| 38 | }; |
| 39 | |
| 40 | /* function: getaddr_cb |
| 41 | * callback for getinterface_ip |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 42 | * msg - netlink message |
| 43 | * data - (struct target) info for which address we're looking for |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 44 | */ |
| 45 | static int getaddr_cb(struct nl_msg *msg, void *data) { |
| 46 | struct ifaddrmsg *ifa_p; |
| 47 | struct rtattr *rta_p; |
| 48 | int rta_len; |
| 49 | struct target *targ_p = (struct target *)data; |
| 50 | |
| 51 | ifa_p = (struct ifaddrmsg *)nlmsg_data(nlmsg_hdr(msg)); |
| 52 | rta_p = (struct rtattr *)IFA_RTA(ifa_p); |
| 53 | |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 54 | if (ifa_p->ifa_index != targ_p->ifindex) return NL_OK; |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 55 | |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 56 | if (ifa_p->ifa_scope != RT_SCOPE_UNIVERSE) return NL_OK; |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 57 | |
| 58 | rta_len = RTM_PAYLOAD(nlmsg_hdr(msg)); |
| 59 | for (; RTA_OK(rta_p, rta_len); rta_p = RTA_NEXT(rta_p, rta_len)) { |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 60 | switch (rta_p->rta_type) { |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 61 | case IFA_ADDRESS: |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 62 | if ((targ_p->family == AF_INET6) && !(ifa_p->ifa_flags & IFA_F_SECONDARY)) { |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 63 | memcpy(&targ_p->ip.ip6, RTA_DATA(rta_p), rta_p->rta_len - sizeof(struct rtattr)); |
| 64 | targ_p->foundip = 1; |
| 65 | return NL_OK; |
| 66 | } |
| 67 | break; |
| 68 | case IFA_LOCAL: |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 69 | if (targ_p->family == AF_INET) { |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 70 | memcpy(&targ_p->ip.ip4, RTA_DATA(rta_p), rta_p->rta_len - sizeof(struct rtattr)); |
| 71 | targ_p->foundip = 1; |
| 72 | return NL_OK; |
| 73 | } |
| 74 | break; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | return NL_OK; |
| 79 | } |
| 80 | |
| 81 | /* function: error_handler |
| 82 | * error callback for getinterface_ip |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 83 | * nla - source of the error message |
| 84 | * err - netlink message |
| 85 | * arg - (struct target) info for which address we're looking for |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 86 | */ |
Lorenzo Colitti | 56ec161 | 2014-03-10 16:33:22 +0900 | [diff] [blame] | 87 | static int error_handler(__attribute__((unused)) struct sockaddr_nl *nla, |
| 88 | __attribute__((unused)) struct nlmsgerr *err, |
| 89 | __attribute__((unused)) void *arg) { |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 90 | return NL_OK; |
| 91 | } |
| 92 | |
| 93 | /* function: getinterface_ip |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 94 | * finds the first global non-privacy IP of the given family for the given interface, or returns |
| 95 | * NULL. caller frees pointer |
| 96 | * interface - interface to look for |
| 97 | * family - family |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 98 | */ |
| 99 | union anyip *getinterface_ip(const char *interface, int family) { |
| 100 | struct ifaddrmsg ifa; |
| 101 | struct nl_cb *callbacks = NULL; |
| 102 | struct target targ; |
| 103 | union anyip *retval = NULL; |
| 104 | |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 105 | targ.family = family; |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 106 | targ.foundip = 0; |
| 107 | targ.ifindex = if_nametoindex(interface); |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 108 | if (targ.ifindex == 0) { |
| 109 | return NULL; // interface not found |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | memset(&ifa, 0, sizeof(ifa)); |
| 113 | ifa.ifa_family = targ.family; |
| 114 | |
| 115 | callbacks = nl_cb_alloc(NL_CB_DEFAULT); |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 116 | if (!callbacks) { |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 117 | goto cleanup; |
| 118 | } |
| 119 | nl_cb_set(callbacks, NL_CB_VALID, NL_CB_CUSTOM, getaddr_cb, &targ); |
| 120 | nl_cb_err(callbacks, NL_CB_CUSTOM, error_handler, &targ); |
| 121 | |
| 122 | // sends message and waits for a response |
| 123 | send_ifaddrmsg(RTM_GETADDR, NLM_F_REQUEST | NLM_F_ROOT, &ifa, callbacks); |
| 124 | |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 125 | if (targ.foundip) { |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 126 | retval = malloc(sizeof(union anyip)); |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 127 | if (!retval) { |
| 128 | logmsg(ANDROID_LOG_FATAL, "getinterface_ip/out of memory"); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 129 | goto cleanup; |
| 130 | } |
| 131 | memcpy(retval, &targ.ip, sizeof(union anyip)); |
| 132 | } |
| 133 | |
| 134 | cleanup: |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 135 | if (callbacks) nl_cb_put(callbacks); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 136 | |
| 137 | return retval; |
| 138 | } |