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