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