Simon Kelley | d1ced3a | 2018-01-01 22:18:03 +0000 | [diff] [blame] | 1 | /* dnsmasq is Copyright (c) 2000-2018 Simon Kelley |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 2 | |
| 3 | This program is free software; you can redistribute it and/or modify |
| 4 | it under the terms of the GNU General Public License as published by |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 5 | the Free Software Foundation; version 2 dated June, 1991, or |
| 6 | (at your option) version 3 dated 29 June, 2007. |
| 7 | |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 8 | This program is distributed in the hope that it will be useful, |
| 9 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | GNU General Public License for more details. |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 12 | |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 13 | You should have received a copy of the GNU General Public License |
| 14 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 15 | */ |
| 16 | |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 17 | #include "dnsmasq.h" |
| 18 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 19 | #ifdef HAVE_LINUX_NETWORK |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 20 | |
Simon Kelley | 91dccd0 | 2005-03-31 17:48:32 +0100 | [diff] [blame] | 21 | #include <linux/types.h> |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 22 | #include <linux/netlink.h> |
| 23 | #include <linux/rtnetlink.h> |
| 24 | |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 25 | /* linux 2.6.19 buggers up the headers, patch it up here. */ |
| 26 | #ifndef IFA_RTA |
| 27 | # define IFA_RTA(r) \ |
| 28 | ((struct rtattr*)(((char*)(r)) + NLMSG_ALIGN(sizeof(struct ifaddrmsg)))) |
| 29 | |
| 30 | # include <linux/if_addr.h> |
| 31 | #endif |
| 32 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 33 | #ifndef NDA_RTA |
| 34 | # define NDA_RTA(r) ((struct rtattr*)(((char*)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg)))) |
| 35 | #endif |
| 36 | |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 37 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 38 | static struct iovec iov; |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 39 | static u32 netlink_pid; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 40 | |
Simon Kelley | a0358e5 | 2014-06-07 13:38:48 +0100 | [diff] [blame] | 41 | static void nl_async(struct nlmsghdr *h); |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 42 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 43 | void netlink_init(void) |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 44 | { |
| 45 | struct sockaddr_nl addr; |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 46 | socklen_t slen = sizeof(addr); |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 47 | |
| 48 | addr.nl_family = AF_NETLINK; |
| 49 | addr.nl_pad = 0; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 50 | addr.nl_pid = 0; /* autobind */ |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 51 | addr.nl_groups = RTMGRP_IPV4_ROUTE; |
Simon Kelley | 54dd393 | 2012-06-20 11:23:38 +0100 | [diff] [blame] | 52 | if (option_bool(OPT_CLEVERBIND)) |
Simon Kelley | 1f77693 | 2012-12-16 19:46:08 +0000 | [diff] [blame] | 53 | addr.nl_groups |= RTMGRP_IPV4_IFADDR; |
Simon Kelley | 54dd393 | 2012-06-20 11:23:38 +0100 | [diff] [blame] | 54 | #ifdef HAVE_IPV6 |
| 55 | addr.nl_groups |= RTMGRP_IPV6_ROUTE; |
Simon Kelley | 1f77693 | 2012-12-16 19:46:08 +0000 | [diff] [blame] | 56 | if (option_bool(OPT_CLEVERBIND)) |
| 57 | addr.nl_groups |= RTMGRP_IPV6_IFADDR; |
| 58 | #endif |
| 59 | #ifdef HAVE_DHCP6 |
| 60 | if (daemon->doing_ra || daemon->doing_dhcp6) |
Simon Kelley | 54dd393 | 2012-06-20 11:23:38 +0100 | [diff] [blame] | 61 | addr.nl_groups |= RTMGRP_IPV6_IFADDR; |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 62 | #endif |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 63 | |
Simon Kelley | 309331f | 2006-04-22 15:05:01 +0100 | [diff] [blame] | 64 | /* May not be able to have permission to set multicast groups don't die in that case */ |
| 65 | if ((daemon->netlinkfd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) != -1) |
| 66 | { |
| 67 | if (bind(daemon->netlinkfd, (struct sockaddr *)&addr, sizeof(addr)) == -1) |
| 68 | { |
| 69 | addr.nl_groups = 0; |
| 70 | if (errno != EPERM || bind(daemon->netlinkfd, (struct sockaddr *)&addr, sizeof(addr)) == -1) |
| 71 | daemon->netlinkfd = -1; |
| 72 | } |
| 73 | } |
| 74 | |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 75 | if (daemon->netlinkfd == -1 || |
Reiter Wolfgang | 5eb9dde | 2017-01-08 17:39:06 +0000 | [diff] [blame] | 76 | getsockname(daemon->netlinkfd, (struct sockaddr *)&addr, &slen) == -1) |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 77 | die(_("cannot create netlink socket: %s"), NULL, EC_MISC); |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 78 | |
| 79 | /* save pid assigned by bind() and retrieved by getsockname() */ |
| 80 | netlink_pid = addr.nl_pid; |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 81 | |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 82 | iov.iov_len = 100; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 83 | iov.iov_base = safe_malloc(iov.iov_len); |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 86 | static ssize_t netlink_recv(void) |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 87 | { |
| 88 | struct msghdr msg; |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 89 | struct sockaddr_nl nladdr; |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 90 | ssize_t rc; |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 91 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 92 | while (1) |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 93 | { |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 94 | msg.msg_control = NULL; |
| 95 | msg.msg_controllen = 0; |
| 96 | msg.msg_name = &nladdr; |
| 97 | msg.msg_namelen = sizeof(nladdr); |
| 98 | msg.msg_iov = &iov; |
| 99 | msg.msg_iovlen = 1; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 100 | msg.msg_flags = 0; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 101 | |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 102 | while ((rc = recvmsg(daemon->netlinkfd, &msg, MSG_PEEK | MSG_TRUNC)) == -1 && errno == EINTR); |
| 103 | |
| 104 | /* make buffer big enough */ |
| 105 | if (rc != -1 && (msg.msg_flags & MSG_TRUNC)) |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 106 | { |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 107 | /* Very new Linux kernels return the actual size needed, older ones always return truncated size */ |
| 108 | if ((size_t)rc == iov.iov_len) |
| 109 | { |
| 110 | if (expand_buf(&iov, rc + 100)) |
| 111 | continue; |
| 112 | } |
| 113 | else |
| 114 | expand_buf(&iov, rc); |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 115 | } |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 116 | |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 117 | /* read it for real */ |
| 118 | msg.msg_flags = 0; |
| 119 | while ((rc = recvmsg(daemon->netlinkfd, &msg, 0)) == -1 && errno == EINTR); |
| 120 | |
| 121 | /* Make sure this is from the kernel */ |
| 122 | if (rc == -1 || nladdr.nl_pid == 0) |
| 123 | break; |
| 124 | } |
| 125 | |
| 126 | /* discard stuff which is truncated at this point (expand_buf() may fail) */ |
| 127 | if (msg.msg_flags & MSG_TRUNC) |
| 128 | { |
| 129 | rc = -1; |
| 130 | errno = ENOMEM; |
| 131 | } |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 132 | |
| 133 | return rc; |
| 134 | } |
| 135 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 136 | |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 137 | /* family = AF_UNSPEC finds ARP table entries. |
| 138 | family = AF_LOCAL finds MAC addresses. */ |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 139 | int iface_enumerate(int family, void *parm, int (*callback)()) |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 140 | { |
| 141 | struct sockaddr_nl addr; |
| 142 | struct nlmsghdr *h; |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 143 | ssize_t len; |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 144 | static unsigned int seq = 0; |
Simon Kelley | a0358e5 | 2014-06-07 13:38:48 +0100 | [diff] [blame] | 145 | int callback_ok = 1; |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 146 | |
| 147 | struct { |
| 148 | struct nlmsghdr nlh; |
| 149 | struct rtgenmsg g; |
| 150 | } req; |
| 151 | |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 152 | addr.nl_family = AF_NETLINK; |
| 153 | addr.nl_pad = 0; |
| 154 | addr.nl_groups = 0; |
| 155 | addr.nl_pid = 0; /* address to kernel */ |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 156 | |
| 157 | again: |
| 158 | if (family == AF_UNSPEC) |
| 159 | req.nlh.nlmsg_type = RTM_GETNEIGH; |
| 160 | else if (family == AF_LOCAL) |
| 161 | req.nlh.nlmsg_type = RTM_GETLINK; |
| 162 | else |
| 163 | req.nlh.nlmsg_type = RTM_GETADDR; |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 164 | |
| 165 | req.nlh.nlmsg_len = sizeof(req); |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 166 | req.nlh.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST | NLM_F_ACK; |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 167 | req.nlh.nlmsg_pid = 0; |
| 168 | req.nlh.nlmsg_seq = ++seq; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 169 | req.g.rtgen_family = family; |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 170 | |
| 171 | /* Don't block in recvfrom if send fails */ |
Simon Kelley | ff841eb | 2015-03-11 21:36:30 +0000 | [diff] [blame] | 172 | while(retry_send(sendto(daemon->netlinkfd, (void *)&req, sizeof(req), 0, |
| 173 | (struct sockaddr *)&addr, sizeof(addr)))); |
| 174 | |
| 175 | if (errno != 0) |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 176 | return 0; |
| 177 | |
| 178 | while (1) |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 179 | { |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 180 | if ((len = netlink_recv()) == -1) |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 181 | { |
| 182 | if (errno == ENOBUFS) |
| 183 | { |
| 184 | sleep(1); |
| 185 | goto again; |
| 186 | } |
| 187 | return 0; |
| 188 | } |
| 189 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 190 | for (h = (struct nlmsghdr *)iov.iov_base; NLMSG_OK(h, (size_t)len); h = NLMSG_NEXT(h, len)) |
Ivan Kokshaysky | 1d07667 | 2016-07-11 18:36:05 +0100 | [diff] [blame] | 191 | if (h->nlmsg_pid != netlink_pid || h->nlmsg_type == NLMSG_ERROR) |
Simon Kelley | 54dd393 | 2012-06-20 11:23:38 +0100 | [diff] [blame] | 192 | { |
| 193 | /* May be multicast arriving async */ |
Simon Kelley | a0358e5 | 2014-06-07 13:38:48 +0100 | [diff] [blame] | 194 | nl_async(h); |
Simon Kelley | 54dd393 | 2012-06-20 11:23:38 +0100 | [diff] [blame] | 195 | } |
Ivan Kokshaysky | 1d07667 | 2016-07-11 18:36:05 +0100 | [diff] [blame] | 196 | else if (h->nlmsg_seq != seq) |
| 197 | { |
| 198 | /* May be part of incomplete response to previous request after |
| 199 | ENOBUFS. Drop it. */ |
| 200 | continue; |
| 201 | } |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 202 | else if (h->nlmsg_type == NLMSG_DONE) |
Simon Kelley | a0358e5 | 2014-06-07 13:38:48 +0100 | [diff] [blame] | 203 | return callback_ok; |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 204 | else if (h->nlmsg_type == RTM_NEWADDR && family != AF_UNSPEC && family != AF_LOCAL) |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 205 | { |
| 206 | struct ifaddrmsg *ifa = NLMSG_DATA(h); |
| 207 | struct rtattr *rta = IFA_RTA(ifa); |
| 208 | unsigned int len1 = h->nlmsg_len - NLMSG_LENGTH(sizeof(*ifa)); |
| 209 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 210 | if (ifa->ifa_family == family) |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 211 | { |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 212 | if (ifa->ifa_family == AF_INET) |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 213 | { |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 214 | struct in_addr netmask, addr, broadcast; |
Simon Kelley | 3f2873d | 2013-05-14 11:28:47 +0100 | [diff] [blame] | 215 | char *label = NULL; |
| 216 | |
Richard Genoud | 10cfc0d | 2014-09-17 21:17:39 +0100 | [diff] [blame] | 217 | netmask.s_addr = htonl(~(in_addr_t)0 << (32 - ifa->ifa_prefixlen)); |
| 218 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 219 | addr.s_addr = 0; |
| 220 | broadcast.s_addr = 0; |
| 221 | |
| 222 | while (RTA_OK(rta, len1)) |
| 223 | { |
| 224 | if (rta->rta_type == IFA_LOCAL) |
| 225 | addr = *((struct in_addr *)(rta+1)); |
| 226 | else if (rta->rta_type == IFA_BROADCAST) |
| 227 | broadcast = *((struct in_addr *)(rta+1)); |
Simon Kelley | 3f2873d | 2013-05-14 11:28:47 +0100 | [diff] [blame] | 228 | else if (rta->rta_type == IFA_LABEL) |
| 229 | label = RTA_DATA(rta); |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 230 | |
| 231 | rta = RTA_NEXT(rta, len1); |
| 232 | } |
| 233 | |
Simon Kelley | e44ddca | 2012-02-18 17:08:50 +0000 | [diff] [blame] | 234 | if (addr.s_addr && callback_ok) |
Simon Kelley | 3f2873d | 2013-05-14 11:28:47 +0100 | [diff] [blame] | 235 | if (!((*callback)(addr, ifa->ifa_index, label, netmask, broadcast, parm))) |
Simon Kelley | e44ddca | 2012-02-18 17:08:50 +0000 | [diff] [blame] | 236 | callback_ok = 0; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 237 | } |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 238 | #ifdef HAVE_IPV6 |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 239 | else if (ifa->ifa_family == AF_INET6) |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 240 | { |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 241 | struct in6_addr *addrp = NULL; |
Simon Kelley | 1f77693 | 2012-12-16 19:46:08 +0000 | [diff] [blame] | 242 | u32 valid = 0, preferred = 0; |
Simon Kelley | bad7b87 | 2012-12-20 22:00:39 +0000 | [diff] [blame] | 243 | int flags = 0; |
| 244 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 245 | while (RTA_OK(rta, len1)) |
| 246 | { |
| 247 | if (rta->rta_type == IFA_ADDRESS) |
| 248 | addrp = ((struct in6_addr *)(rta+1)); |
Simon Kelley | 1f77693 | 2012-12-16 19:46:08 +0000 | [diff] [blame] | 249 | else if (rta->rta_type == IFA_CACHEINFO) |
| 250 | { |
| 251 | struct ifa_cacheinfo *ifc = (struct ifa_cacheinfo *)(rta+1); |
| 252 | preferred = ifc->ifa_prefered; |
| 253 | valid = ifc->ifa_valid; |
| 254 | } |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 255 | rta = RTA_NEXT(rta, len1); |
| 256 | } |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 257 | |
Simon Kelley | bad7b87 | 2012-12-20 22:00:39 +0000 | [diff] [blame] | 258 | if (ifa->ifa_flags & IFA_F_TENTATIVE) |
| 259 | flags |= IFACE_TENTATIVE; |
| 260 | |
| 261 | if (ifa->ifa_flags & IFA_F_DEPRECATED) |
| 262 | flags |= IFACE_DEPRECATED; |
| 263 | |
Jonas Gorski | 57ab36e | 2014-01-22 11:34:16 +0000 | [diff] [blame] | 264 | if (!(ifa->ifa_flags & IFA_F_TEMPORARY)) |
| 265 | flags |= IFACE_PERMANENT; |
| 266 | |
Simon Kelley | e44ddca | 2012-02-18 17:08:50 +0000 | [diff] [blame] | 267 | if (addrp && callback_ok) |
Simon Kelley | 52b92f4 | 2012-01-22 16:05:15 +0000 | [diff] [blame] | 268 | if (!((*callback)(addrp, (int)(ifa->ifa_prefixlen), (int)(ifa->ifa_scope), |
Simon Kelley | bad7b87 | 2012-12-20 22:00:39 +0000 | [diff] [blame] | 269 | (int)(ifa->ifa_index), flags, |
Simon Kelley | 1f77693 | 2012-12-16 19:46:08 +0000 | [diff] [blame] | 270 | (int) preferred, (int)valid, parm))) |
Simon Kelley | e44ddca | 2012-02-18 17:08:50 +0000 | [diff] [blame] | 271 | callback_ok = 0; |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 272 | } |
| 273 | #endif |
| 274 | } |
| 275 | } |
| 276 | else if (h->nlmsg_type == RTM_NEWNEIGH && family == AF_UNSPEC) |
| 277 | { |
| 278 | struct ndmsg *neigh = NLMSG_DATA(h); |
| 279 | struct rtattr *rta = NDA_RTA(neigh); |
| 280 | unsigned int len1 = h->nlmsg_len - NLMSG_LENGTH(sizeof(*neigh)); |
| 281 | size_t maclen = 0; |
| 282 | char *inaddr = NULL, *mac = NULL; |
| 283 | |
| 284 | while (RTA_OK(rta, len1)) |
| 285 | { |
| 286 | if (rta->rta_type == NDA_DST) |
| 287 | inaddr = (char *)(rta+1); |
| 288 | else if (rta->rta_type == NDA_LLADDR) |
| 289 | { |
| 290 | maclen = rta->rta_len - sizeof(struct rtattr); |
| 291 | mac = (char *)(rta+1); |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 292 | } |
| 293 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 294 | rta = RTA_NEXT(rta, len1); |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 295 | } |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 296 | |
Simon Kelley | fa14bec | 2015-12-20 17:12:16 +0000 | [diff] [blame] | 297 | if (!(neigh->ndm_state & (NUD_NOARP | NUD_INCOMPLETE | NUD_FAILED)) && |
| 298 | inaddr && mac && callback_ok) |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 299 | if (!((*callback)(neigh->ndm_family, inaddr, mac, maclen, parm))) |
Simon Kelley | e44ddca | 2012-02-18 17:08:50 +0000 | [diff] [blame] | 300 | callback_ok = 0; |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 301 | } |
| 302 | #ifdef HAVE_DHCP6 |
| 303 | else if (h->nlmsg_type == RTM_NEWLINK && family == AF_LOCAL) |
| 304 | { |
| 305 | struct ifinfomsg *link = NLMSG_DATA(h); |
| 306 | struct rtattr *rta = IFLA_RTA(link); |
| 307 | unsigned int len1 = h->nlmsg_len - NLMSG_LENGTH(sizeof(*link)); |
| 308 | char *mac = NULL; |
| 309 | size_t maclen = 0; |
| 310 | |
| 311 | while (RTA_OK(rta, len1)) |
| 312 | { |
| 313 | if (rta->rta_type == IFLA_ADDRESS) |
| 314 | { |
| 315 | maclen = rta->rta_len - sizeof(struct rtattr); |
| 316 | mac = (char *)(rta+1); |
| 317 | } |
| 318 | |
| 319 | rta = RTA_NEXT(rta, len1); |
| 320 | } |
| 321 | |
Simon Kelley | e44ddca | 2012-02-18 17:08:50 +0000 | [diff] [blame] | 322 | if (mac && callback_ok && !((link->ifi_flags & (IFF_LOOPBACK | IFF_POINTOPOINT))) && |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 323 | !((*callback)((int)link->ifi_index, (unsigned int)link->ifi_type, mac, maclen, parm))) |
Simon Kelley | e44ddca | 2012-02-18 17:08:50 +0000 | [diff] [blame] | 324 | callback_ok = 0; |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 325 | } |
| 326 | #endif |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 327 | } |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 328 | } |
| 329 | |
Simon Kelley | a0358e5 | 2014-06-07 13:38:48 +0100 | [diff] [blame] | 330 | void netlink_multicast(void) |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 331 | { |
| 332 | ssize_t len; |
| 333 | struct nlmsghdr *h; |
Simon Kelley | a0358e5 | 2014-06-07 13:38:48 +0100 | [diff] [blame] | 334 | int flags; |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 335 | |
| 336 | /* don't risk blocking reading netlink messages here. */ |
| 337 | if ((flags = fcntl(daemon->netlinkfd, F_GETFL)) == -1 || |
| 338 | fcntl(daemon->netlinkfd, F_SETFL, flags | O_NONBLOCK) == -1) |
| 339 | return; |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 340 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 341 | if ((len = netlink_recv()) != -1) |
Simon Kelley | 54dd393 | 2012-06-20 11:23:38 +0100 | [diff] [blame] | 342 | for (h = (struct nlmsghdr *)iov.iov_base; NLMSG_OK(h, (size_t)len); h = NLMSG_NEXT(h, len)) |
Simon Kelley | a0358e5 | 2014-06-07 13:38:48 +0100 | [diff] [blame] | 343 | nl_async(h); |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 344 | |
Simon Kelley | 54dd393 | 2012-06-20 11:23:38 +0100 | [diff] [blame] | 345 | /* restore non-blocking status */ |
| 346 | fcntl(daemon->netlinkfd, F_SETFL, flags); |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 347 | } |
| 348 | |
Simon Kelley | a0358e5 | 2014-06-07 13:38:48 +0100 | [diff] [blame] | 349 | static void nl_async(struct nlmsghdr *h) |
Simon Kelley | 54dd393 | 2012-06-20 11:23:38 +0100 | [diff] [blame] | 350 | { |
| 351 | if (h->nlmsg_type == NLMSG_ERROR) |
| 352 | { |
| 353 | struct nlmsgerr *err = NLMSG_DATA(h); |
Simon Kelley | dfb23b3 | 2012-09-18 21:44:47 +0100 | [diff] [blame] | 354 | if (err->error != 0) |
| 355 | my_syslog(LOG_ERR, _("netlink returns error: %s"), strerror(-(err->error))); |
Simon Kelley | 54dd393 | 2012-06-20 11:23:38 +0100 | [diff] [blame] | 356 | } |
| 357 | else if (h->nlmsg_pid == 0 && h->nlmsg_type == RTM_NEWROUTE) |
| 358 | { |
Simon Kelley | e17b4b3 | 2012-06-28 21:44:30 +0100 | [diff] [blame] | 359 | /* We arrange to receive netlink multicast messages whenever the network route is added. |
| 360 | If this happens and we still have a DNS packet in the buffer, we re-send it. |
| 361 | This helps on DoD links, where frequently the packet which triggers dialling is |
| 362 | a DNS query, which then gets lost. By re-sending, we can avoid the lookup |
| 363 | failing. */ |
| 364 | struct rtmsg *rtm = NLMSG_DATA(h); |
| 365 | |
| 366 | if (rtm->rtm_type == RTN_UNICAST && rtm->rtm_scope == RT_SCOPE_LINK) |
Simon Kelley | 47a9516 | 2014-07-08 22:22:02 +0100 | [diff] [blame] | 367 | queue_event(EVENT_NEWROUTE); |
Simon Kelley | 54dd393 | 2012-06-20 11:23:38 +0100 | [diff] [blame] | 368 | } |
Simon Kelley | 1f77693 | 2012-12-16 19:46:08 +0000 | [diff] [blame] | 369 | else if (h->nlmsg_type == RTM_NEWADDR || h->nlmsg_type == RTM_DELADDR) |
Simon Kelley | 47a9516 | 2014-07-08 22:22:02 +0100 | [diff] [blame] | 370 | queue_event(EVENT_NEWADDR); |
Simon Kelley | 54dd393 | 2012-06-20 11:23:38 +0100 | [diff] [blame] | 371 | } |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 372 | #endif |
| 373 | |
| 374 | |