| 1 |
/* dnsmasq is Copyright (c) 2000-2026 Simon Kelley |
| 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 |
| 5 |
the Free Software Foundation; version 2 dated June, 1991, or |
| 6 |
(at your option) version 3 dated 29 June, 2007. |
| 7 |
|
| 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. |
| 12 |
|
| 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/>. |
| 15 |
*/ |
| 16 |
|
| 17 |
#include "dnsmasq.h" |
| 18 |
|
| 19 |
#ifdef HAVE_LINUX_NETWORK |
| 20 |
|
| 21 |
#include <linux/types.h> |
| 22 |
#include <linux/netlink.h> |
| 23 |
#include <linux/rtnetlink.h> |
| 24 |
|
| 25 |
/* Blergh. Radv does this, so that's our excuse. */ |
| 26 |
#ifndef SOL_NETLINK |
| 27 |
#define SOL_NETLINK 270 |
| 28 |
#endif |
| 29 |
|
| 30 |
#ifndef NETLINK_NO_ENOBUFS |
| 31 |
#define NETLINK_NO_ENOBUFS 5 |
| 32 |
#endif |
| 33 |
|
| 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 |
|
| 42 |
#ifndef NDA_RTA |
| 43 |
# define NDA_RTA(r) ((struct rtattr*)(((char*)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg)))) |
| 44 |
#endif |
| 45 |
|
| 46 |
/* Used to request refresh of addresses or routes just once, |
| 47 |
* when multiple changes might be announced. */ |
| 48 |
enum async_states { |
| 49 |
STATE_NEWADDR = (1 << 0), |
| 50 |
STATE_NEWROUTE = (1 << 1), |
| 51 |
}; |
| 52 |
|
| 53 |
|
| 54 |
static struct iovec iov; |
| 55 |
static u32 netlink_pid; |
| 56 |
|
| 57 |
static unsigned nl_async(struct nlmsghdr *h, unsigned state); |
| 58 |
static void nl_multicast_state(unsigned state); |
| 59 |
|
| 60 |
char *netlink_init(void) |
| 61 |
{ |
| 62 |
struct sockaddr_nl addr; |
| 63 |
socklen_t slen = sizeof(addr); |
| 64 |
|
| 65 |
addr.nl_family = AF_NETLINK; |
| 66 |
addr.nl_pad = 0; |
| 67 |
addr.nl_pid = 0; /* autobind */ |
| 68 |
addr.nl_groups = RTMGRP_IPV4_ROUTE; |
| 69 |
addr.nl_groups |= RTMGRP_IPV4_IFADDR; |
| 70 |
addr.nl_groups |= RTMGRP_IPV6_ROUTE; |
| 71 |
addr.nl_groups |= RTMGRP_IPV6_IFADDR; |
| 72 |
|
| 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 |
|
| 84 |
if (daemon->netlinkfd == -1 || |
| 85 |
getsockname(daemon->netlinkfd, (struct sockaddr *)&addr, &slen) == -1) |
| 86 |
die(_("cannot create netlink socket: %s"), NULL, EC_MISC); |
| 87 |
|
| 88 |
|
| 89 |
/* save pid assigned by bind() and retrieved by getsockname() */ |
| 90 |
netlink_pid = addr.nl_pid; |
| 91 |
|
| 92 |
iov.iov_len = 100; |
| 93 |
iov.iov_base = safe_malloc(iov.iov_len); |
| 94 |
|
| 95 |
return NULL; |
| 96 |
} |
| 97 |
|
| 98 |
static ssize_t netlink_recv(int flags) |
| 99 |
{ |
| 100 |
struct msghdr msg; |
| 101 |
struct sockaddr_nl nladdr; |
| 102 |
ssize_t rc; |
| 103 |
|
| 104 |
while (1) |
| 105 |
{ |
| 106 |
msg.msg_control = NULL; |
| 107 |
msg.msg_controllen = 0; |
| 108 |
msg.msg_name = &nladdr; |
| 109 |
msg.msg_namelen = sizeof(nladdr); |
| 110 |
msg.msg_iov = &iov; |
| 111 |
msg.msg_iovlen = 1; |
| 112 |
msg.msg_flags = 0; |
| 113 |
|
| 114 |
while ((rc = recvmsg(daemon->netlinkfd, &msg, flags | MSG_PEEK | MSG_TRUNC)) == -1 && |
| 115 |
errno == EINTR); |
| 116 |
|
| 117 |
/* make buffer big enough */ |
| 118 |
if (rc != -1 && (msg.msg_flags & MSG_TRUNC)) |
| 119 |
{ |
| 120 |
/* Very new Linux kernels return the actual size needed, older ones always return truncated size */ |
| 121 |
if ((size_t)rc == iov.iov_len) |
| 122 |
{ |
| 123 |
if (expand_buf(&iov, rc + 100)) |
| 124 |
continue; |
| 125 |
} |
| 126 |
else |
| 127 |
expand_buf(&iov, rc); |
| 128 |
} |
| 129 |
|
| 130 |
/* read it for real */ |
| 131 |
msg.msg_flags = 0; |
| 132 |
while ((rc = recvmsg(daemon->netlinkfd, &msg, flags)) == -1 && errno == EINTR); |
| 133 |
|
| 134 |
/* Make sure this is from the kernel */ |
| 135 |
if (rc == -1 || nladdr.nl_pid == 0) |
| 136 |
break; |
| 137 |
} |
| 138 |
|
| 139 |
/* discard stuff which is truncated at this point (expand_buf() may fail) */ |
| 140 |
if (msg.msg_flags & MSG_TRUNC) |
| 141 |
{ |
| 142 |
rc = -1; |
| 143 |
errno = ENOMEM; |
| 144 |
} |
| 145 |
|
| 146 |
return rc; |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
/* family = AF_UNSPEC finds ARP table entries. |
| 151 |
family = AF_LOCAL finds MAC addresses. |
| 152 |
returns 0 on failure, 1 on success, -1 when restart is required |
| 153 |
*/ |
| 154 |
int iface_enumerate(int family, void *parm, callback_t callback) |
| 155 |
{ |
| 156 |
struct sockaddr_nl addr; |
| 157 |
struct nlmsghdr *h; |
| 158 |
ssize_t len; |
| 159 |
static unsigned int seq = 0; |
| 160 |
int callback_ok = 1; |
| 161 |
unsigned state = 0; |
| 162 |
|
| 163 |
struct { |
| 164 |
struct nlmsghdr nlh; |
| 165 |
struct rtgenmsg g; |
| 166 |
} req; |
| 167 |
|
| 168 |
/* The netlink socket is not available in child processes. */ |
| 169 |
if (daemon->pipe_to_parent != -1) |
| 170 |
{ |
| 171 |
my_syslog(LOG_ERR, _("BUG: called iface_enumerate() in child process")); |
| 172 |
return 0; |
| 173 |
} |
| 174 |
|
| 175 |
memset(&req, 0, sizeof(req)); |
| 176 |
memset(&addr, 0, sizeof(addr)); |
| 177 |
|
| 178 |
addr.nl_family = AF_NETLINK; |
| 179 |
|
| 180 |
if (family == AF_UNSPEC) |
| 181 |
req.nlh.nlmsg_type = RTM_GETNEIGH; |
| 182 |
else if (family == AF_LOCAL) |
| 183 |
req.nlh.nlmsg_type = RTM_GETLINK; |
| 184 |
else |
| 185 |
req.nlh.nlmsg_type = RTM_GETADDR; |
| 186 |
|
| 187 |
req.nlh.nlmsg_len = sizeof(req); |
| 188 |
req.nlh.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST | NLM_F_ACK; |
| 189 |
req.nlh.nlmsg_pid = 0; |
| 190 |
req.nlh.nlmsg_seq = ++seq; |
| 191 |
req.g.rtgen_family = family; |
| 192 |
|
| 193 |
/* Don't block in recvfrom if send fails */ |
| 194 |
while(retry_send(sendto(daemon->netlinkfd, (void *)&req, sizeof(req), 0, |
| 195 |
(struct sockaddr *)&addr, sizeof(addr)))); |
| 196 |
|
| 197 |
if (errno != 0) |
| 198 |
return 0; |
| 199 |
|
| 200 |
while (1) |
| 201 |
{ |
| 202 |
if ((len = netlink_recv(0)) == -1) |
| 203 |
{ |
| 204 |
if (errno == ENOBUFS) |
| 205 |
{ |
| 206 |
nl_multicast_state(state); |
| 207 |
return -1; |
| 208 |
} |
| 209 |
return 0; |
| 210 |
} |
| 211 |
|
| 212 |
for (h = (struct nlmsghdr *)iov.iov_base; NLMSG_OK(h, (size_t)len); h = NLMSG_NEXT(h, len)) |
| 213 |
if (h->nlmsg_pid != netlink_pid || h->nlmsg_type == NLMSG_ERROR) |
| 214 |
{ |
| 215 |
/* May be multicast arriving async */ |
| 216 |
state = nl_async(h, state); |
| 217 |
} |
| 218 |
else if (h->nlmsg_seq != seq) |
| 219 |
{ |
| 220 |
/* May be part of incomplete response to previous request after |
| 221 |
ENOBUFS. Drop it. */ |
| 222 |
continue; |
| 223 |
} |
| 224 |
else if (h->nlmsg_type == NLMSG_DONE) |
| 225 |
return callback_ok; |
| 226 |
else if (h->nlmsg_type == RTM_NEWADDR && family != AF_UNSPEC && family != AF_LOCAL) |
| 227 |
{ |
| 228 |
struct ifaddrmsg *ifa = NLMSG_DATA(h); |
| 229 |
struct rtattr *rta = IFA_RTA(ifa); |
| 230 |
unsigned int len1 = h->nlmsg_len - NLMSG_LENGTH(sizeof(*ifa)); |
| 231 |
|
| 232 |
if (ifa->ifa_family == family) |
| 233 |
{ |
| 234 |
if (ifa->ifa_family == AF_INET) |
| 235 |
{ |
| 236 |
struct in_addr netmask, addr, broadcast; |
| 237 |
char *label = NULL; |
| 238 |
|
| 239 |
netmask.s_addr = htonl(~(in_addr_t)0 << (32 - ifa->ifa_prefixlen)); |
| 240 |
|
| 241 |
addr.s_addr = 0; |
| 242 |
broadcast.s_addr = 0; |
| 243 |
|
| 244 |
while (RTA_OK(rta, len1)) |
| 245 |
{ |
| 246 |
if (rta->rta_type == IFA_LOCAL) |
| 247 |
addr = *((struct in_addr *)(rta+1)); |
| 248 |
else if (rta->rta_type == IFA_BROADCAST) |
| 249 |
broadcast = *((struct in_addr *)(rta+1)); |
| 250 |
else if (rta->rta_type == IFA_LABEL) |
| 251 |
label = RTA_DATA(rta); |
| 252 |
|
| 253 |
rta = RTA_NEXT(rta, len1); |
| 254 |
} |
| 255 |
|
| 256 |
if (addr.s_addr && callback_ok) |
| 257 |
if (!callback.af_inet(addr, ifa->ifa_index, label, netmask, broadcast, parm)) |
| 258 |
callback_ok = 0; |
| 259 |
} |
| 260 |
else if (ifa->ifa_family == AF_INET6) |
| 261 |
{ |
| 262 |
struct in6_addr *addrp = NULL; |
| 263 |
u32 valid = 0, preferred = 0; |
| 264 |
int flags = 0; |
| 265 |
|
| 266 |
while (RTA_OK(rta, len1)) |
| 267 |
{ |
| 268 |
/* |
| 269 |
* Important comment: (from if_addr.h) |
| 270 |
* IFA_ADDRESS is prefix address, rather than local interface address. |
| 271 |
* It makes no difference for normally configured broadcast interfaces, |
| 272 |
* but for point-to-point IFA_ADDRESS is DESTINATION address, |
| 273 |
* local address is supplied in IFA_LOCAL attribute. |
| 274 |
*/ |
| 275 |
if (rta->rta_type == IFA_LOCAL) |
| 276 |
addrp = ((struct in6_addr *)(rta+1)); |
| 277 |
else if (rta->rta_type == IFA_ADDRESS && !addrp) |
| 278 |
addrp = ((struct in6_addr *)(rta+1)); |
| 279 |
else if (rta->rta_type == IFA_CACHEINFO) |
| 280 |
{ |
| 281 |
struct ifa_cacheinfo *ifc = (struct ifa_cacheinfo *)(rta+1); |
| 282 |
preferred = ifc->ifa_prefered; |
| 283 |
valid = ifc->ifa_valid; |
| 284 |
} |
| 285 |
rta = RTA_NEXT(rta, len1); |
| 286 |
} |
| 287 |
|
| 288 |
if (ifa->ifa_flags & IFA_F_TENTATIVE) |
| 289 |
flags |= IFACE_TENTATIVE; |
| 290 |
|
| 291 |
if (ifa->ifa_flags & IFA_F_DEPRECATED) |
| 292 |
flags |= IFACE_DEPRECATED; |
| 293 |
|
| 294 |
if (!(ifa->ifa_flags & IFA_F_TEMPORARY)) |
| 295 |
flags |= IFACE_PERMANENT; |
| 296 |
|
| 297 |
if (addrp && callback_ok) |
| 298 |
if (!callback.af_inet6(addrp, (int)(ifa->ifa_prefixlen), (int)(ifa->ifa_scope), |
| 299 |
(int)(ifa->ifa_index), flags, |
| 300 |
(unsigned int)preferred, (unsigned int)valid, parm)) |
| 301 |
callback_ok = 0; |
| 302 |
} |
| 303 |
} |
| 304 |
} |
| 305 |
else if (h->nlmsg_type == RTM_NEWNEIGH && family == AF_UNSPEC) |
| 306 |
{ |
| 307 |
struct ndmsg *neigh = NLMSG_DATA(h); |
| 308 |
struct rtattr *rta = NDA_RTA(neigh); |
| 309 |
unsigned int len1 = h->nlmsg_len - NLMSG_LENGTH(sizeof(*neigh)); |
| 310 |
size_t maclen = 0; |
| 311 |
char *inaddr = NULL, *mac = NULL; |
| 312 |
|
| 313 |
while (RTA_OK(rta, len1)) |
| 314 |
{ |
| 315 |
if (rta->rta_type == NDA_DST) |
| 316 |
inaddr = (char *)(rta+1); |
| 317 |
else if (rta->rta_type == NDA_LLADDR) |
| 318 |
{ |
| 319 |
maclen = rta->rta_len - sizeof(struct rtattr); |
| 320 |
mac = (char *)(rta+1); |
| 321 |
} |
| 322 |
|
| 323 |
rta = RTA_NEXT(rta, len1); |
| 324 |
} |
| 325 |
|
| 326 |
if (!(neigh->ndm_state & (NUD_NOARP | NUD_INCOMPLETE | NUD_FAILED)) && |
| 327 |
inaddr && mac && callback_ok) |
| 328 |
if (!callback.af_unspec(neigh->ndm_family, inaddr, mac, maclen, parm)) |
| 329 |
callback_ok = 0; |
| 330 |
} |
| 331 |
#ifdef HAVE_DHCP6 |
| 332 |
else if (h->nlmsg_type == RTM_NEWLINK && family == AF_LOCAL) |
| 333 |
{ |
| 334 |
struct ifinfomsg *link = NLMSG_DATA(h); |
| 335 |
struct rtattr *rta = IFLA_RTA(link); |
| 336 |
unsigned int len1 = h->nlmsg_len - NLMSG_LENGTH(sizeof(*link)); |
| 337 |
char *mac = NULL; |
| 338 |
size_t maclen = 0; |
| 339 |
|
| 340 |
while (RTA_OK(rta, len1)) |
| 341 |
{ |
| 342 |
if (rta->rta_type == IFLA_ADDRESS) |
| 343 |
{ |
| 344 |
maclen = rta->rta_len - sizeof(struct rtattr); |
| 345 |
mac = (char *)(rta+1); |
| 346 |
} |
| 347 |
|
| 348 |
rta = RTA_NEXT(rta, len1); |
| 349 |
} |
| 350 |
|
| 351 |
if (mac && callback_ok && !((link->ifi_flags & (IFF_LOOPBACK | IFF_POINTOPOINT))) && |
| 352 |
!callback.af_local((int)link->ifi_index, (unsigned int)link->ifi_type, mac, maclen, parm)) |
| 353 |
callback_ok = 0; |
| 354 |
} |
| 355 |
#endif |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
static void nl_multicast_state(unsigned state) |
| 360 |
{ |
| 361 |
ssize_t len; |
| 362 |
struct nlmsghdr *h; |
| 363 |
|
| 364 |
do { |
| 365 |
/* don't risk blocking reading netlink messages here. */ |
| 366 |
while ((len = netlink_recv(MSG_DONTWAIT)) != -1) |
| 367 |
|
| 368 |
for (h = (struct nlmsghdr *)iov.iov_base; NLMSG_OK(h, (size_t)len); h = NLMSG_NEXT(h, len)) |
| 369 |
state = nl_async(h, state); |
| 370 |
} while (errno == ENOBUFS); |
| 371 |
} |
| 372 |
|
| 373 |
void netlink_multicast(void) |
| 374 |
{ |
| 375 |
unsigned state = 0; |
| 376 |
nl_multicast_state(state); |
| 377 |
} |
| 378 |
|
| 379 |
|
| 380 |
static unsigned nl_async(struct nlmsghdr *h, unsigned state) |
| 381 |
{ |
| 382 |
if (h->nlmsg_type == NLMSG_ERROR) |
| 383 |
{ |
| 384 |
struct nlmsgerr *err = NLMSG_DATA(h); |
| 385 |
if (err->error != 0) |
| 386 |
my_syslog(LOG_ERR, _("netlink returns error: %s"), strerror(-(err->error))); |
| 387 |
} |
| 388 |
else if (h->nlmsg_pid == 0 && h->nlmsg_type == RTM_NEWROUTE && |
| 389 |
(state & STATE_NEWROUTE)==0) |
| 390 |
{ |
| 391 |
/* We arrange to receive netlink multicast messages whenever the network route is added. |
| 392 |
If this happens and we still have a DNS packet in the buffer, we re-send it. |
| 393 |
This helps on DoD links, where frequently the packet which triggers dialling is |
| 394 |
a DNS query, which then gets lost. By re-sending, we can avoid the lookup |
| 395 |
failing. */ |
| 396 |
struct rtmsg *rtm = NLMSG_DATA(h); |
| 397 |
|
| 398 |
if (rtm->rtm_type == RTN_UNICAST && rtm->rtm_scope == RT_SCOPE_LINK && |
| 399 |
(rtm->rtm_table == RT_TABLE_MAIN || |
| 400 |
rtm->rtm_table == RT_TABLE_LOCAL)) |
| 401 |
{ |
| 402 |
queue_event(EVENT_NEWROUTE); |
| 403 |
state |= STATE_NEWROUTE; |
| 404 |
} |
| 405 |
} |
| 406 |
else if ((h->nlmsg_type == RTM_NEWADDR || h->nlmsg_type == RTM_DELADDR) && |
| 407 |
(state & STATE_NEWADDR)==0) |
| 408 |
{ |
| 409 |
queue_event(EVENT_NEWADDR); |
| 410 |
state |= STATE_NEWADDR; |
| 411 |
} |
| 412 |
return state; |
| 413 |
} |
| 414 |
#endif /* HAVE_LINUX_NETWORK */ |