1 /* dnsmasq is Copyright (c) 2000-2022 Simon Kelley
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.
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.
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/>.
19 #ifdef HAVE_LINUX_NETWORK
21 #include <linux/types.h>
22 #include <linux/netlink.h>
23 #include <linux/rtnetlink.h>
25 /* Blergh. Radv does this, so that's our excuse. */
27 #define SOL_NETLINK 270
30 #ifndef NETLINK_NO_ENOBUFS
31 #define NETLINK_NO_ENOBUFS 5
34 /* linux 2.6.19 buggers up the headers, patch it up here. */
37 ((struct rtattr*)(((char*)(r)) + NLMSG_ALIGN(sizeof(struct ifaddrmsg))))
39 # include <linux/if_addr.h>
43 # define NDA_RTA(r) ((struct rtattr*)(((char*)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
46 /* Used to request refresh of addresses or routes just once,
47 * when multiple changes might be announced. */
49 STATE_NEWADDR = (1 << 0),
50 STATE_NEWROUTE = (1 << 1),
54 static struct iovec iov;
55 static u32 netlink_pid;
57 static unsigned nl_async(struct nlmsghdr *h, unsigned state);
58 static void nl_multicast_state(unsigned state);
60 char *netlink_init(void)
62 struct sockaddr_nl addr;
63 socklen_t slen = sizeof(addr);
65 addr.nl_family = AF_NETLINK;
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;
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)
76 if (bind(daemon->netlinkfd, (struct sockaddr *)&addr, sizeof(addr)) == -1)
79 if (errno != EPERM || bind(daemon->netlinkfd, (struct sockaddr *)&addr, sizeof(addr)) == -1)
80 daemon->netlinkfd = -1;
84 if (daemon->netlinkfd == -1 ||
85 getsockname(daemon->netlinkfd, (struct sockaddr *)&addr, &slen) == -1)
86 die(_("cannot create netlink socket: %s"), NULL, EC_MISC);
89 /* save pid assigned by bind() and retrieved by getsockname() */
90 netlink_pid = addr.nl_pid;
93 iov.iov_base = safe_malloc(iov.iov_len);
98 static ssize_t netlink_recv(int flags)
101 struct sockaddr_nl nladdr;
106 msg.msg_control = NULL;
107 msg.msg_controllen = 0;
108 msg.msg_name = &nladdr;
109 msg.msg_namelen = sizeof(nladdr);
114 while ((rc = recvmsg(daemon->netlinkfd, &msg, flags | MSG_PEEK | MSG_TRUNC)) == -1 &&
117 /* make buffer big enough */
118 if (rc != -1 && (msg.msg_flags & MSG_TRUNC))
120 /* Very new Linux kernels return the actual size needed, older ones always return truncated size */
121 if ((size_t)rc == iov.iov_len)
123 if (expand_buf(&iov, rc + 100))
127 expand_buf(&iov, rc);
130 /* read it for real */
132 while ((rc = recvmsg(daemon->netlinkfd, &msg, flags)) == -1 && errno == EINTR);
134 /* Make sure this is from the kernel */
135 if (rc == -1 || nladdr.nl_pid == 0)
139 /* discard stuff which is truncated at this point (expand_buf() may fail) */
140 if (msg.msg_flags & MSG_TRUNC)
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
154 int iface_enumerate(int family, void *parm, int (*callback)())
156 struct sockaddr_nl addr;
159 static unsigned int seq = 0;
168 memset(&req, 0, sizeof(req));
169 memset(&addr, 0, sizeof(addr));
171 addr.nl_family = AF_NETLINK;
173 if (family == AF_UNSPEC)
174 req.nlh.nlmsg_type = RTM_GETNEIGH;
175 else if (family == AF_LOCAL)
176 req.nlh.nlmsg_type = RTM_GETLINK;
178 req.nlh.nlmsg_type = RTM_GETADDR;
180 req.nlh.nlmsg_len = sizeof(req);
181 req.nlh.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST | NLM_F_ACK;
182 req.nlh.nlmsg_pid = 0;
183 req.nlh.nlmsg_seq = ++seq;
184 req.g.rtgen_family = family;
186 /* Don't block in recvfrom if send fails */
187 while(retry_send(sendto(daemon->netlinkfd, (void *)&req, sizeof(req), 0,
188 (struct sockaddr *)&addr, sizeof(addr))));
195 if ((len = netlink_recv(0)) == -1)
197 if (errno == ENOBUFS)
199 nl_multicast_state(state);
205 for (h = (struct nlmsghdr *)iov.iov_base; NLMSG_OK(h, (size_t)len); h = NLMSG_NEXT(h, len))
206 if (h->nlmsg_pid != netlink_pid || h->nlmsg_type == NLMSG_ERROR)
208 /* May be multicast arriving async */
209 state = nl_async(h, state);
211 else if (h->nlmsg_seq != seq)
213 /* May be part of incomplete response to previous request after
217 else if (h->nlmsg_type == NLMSG_DONE)
219 else if (h->nlmsg_type == RTM_NEWADDR && family != AF_UNSPEC && family != AF_LOCAL)
221 struct ifaddrmsg *ifa = NLMSG_DATA(h);
222 struct rtattr *rta = IFA_RTA(ifa);
223 unsigned int len1 = h->nlmsg_len - NLMSG_LENGTH(sizeof(*ifa));
225 if (ifa->ifa_family == family)
227 if (ifa->ifa_family == AF_INET)
229 struct in_addr netmask, addr, broadcast;
232 netmask.s_addr = htonl(~(in_addr_t)0 << (32 - ifa->ifa_prefixlen));
235 broadcast.s_addr = 0;
237 while (RTA_OK(rta, len1))
239 if (rta->rta_type == IFA_LOCAL)
240 addr = *((struct in_addr *)(rta+1));
241 else if (rta->rta_type == IFA_BROADCAST)
242 broadcast = *((struct in_addr *)(rta+1));
243 else if (rta->rta_type == IFA_LABEL)
244 label = RTA_DATA(rta);
246 rta = RTA_NEXT(rta, len1);
249 if (addr.s_addr && callback_ok)
250 if (!((*callback)(addr, ifa->ifa_index, label, netmask, broadcast, parm)))
253 else if (ifa->ifa_family == AF_INET6)
255 struct in6_addr *addrp = NULL;
256 u32 valid = 0, preferred = 0;
259 while (RTA_OK(rta, len1))
262 * Important comment: (from if_addr.h)
263 * IFA_ADDRESS is prefix address, rather than local interface address.
264 * It makes no difference for normally configured broadcast interfaces,
265 * but for point-to-point IFA_ADDRESS is DESTINATION address,
266 * local address is supplied in IFA_LOCAL attribute.
268 if (rta->rta_type == IFA_LOCAL)
269 addrp = ((struct in6_addr *)(rta+1));
270 else if (rta->rta_type == IFA_ADDRESS && !addrp)
271 addrp = ((struct in6_addr *)(rta+1));
272 else if (rta->rta_type == IFA_CACHEINFO)
274 struct ifa_cacheinfo *ifc = (struct ifa_cacheinfo *)(rta+1);
275 preferred = ifc->ifa_prefered;
276 valid = ifc->ifa_valid;
278 rta = RTA_NEXT(rta, len1);
281 if (ifa->ifa_flags & IFA_F_TENTATIVE)
282 flags |= IFACE_TENTATIVE;
284 if (ifa->ifa_flags & IFA_F_DEPRECATED)
285 flags |= IFACE_DEPRECATED;
287 if (!(ifa->ifa_flags & IFA_F_TEMPORARY))
288 flags |= IFACE_PERMANENT;
290 if (addrp && callback_ok)
291 if (!((*callback)(addrp, (int)(ifa->ifa_prefixlen), (int)(ifa->ifa_scope),
292 (int)(ifa->ifa_index), flags,
293 (int) preferred, (int)valid, parm)))
298 else if (h->nlmsg_type == RTM_NEWNEIGH && family == AF_UNSPEC)
300 struct ndmsg *neigh = NLMSG_DATA(h);
301 struct rtattr *rta = NDA_RTA(neigh);
302 unsigned int len1 = h->nlmsg_len - NLMSG_LENGTH(sizeof(*neigh));
304 char *inaddr = NULL, *mac = NULL;
306 while (RTA_OK(rta, len1))
308 if (rta->rta_type == NDA_DST)
309 inaddr = (char *)(rta+1);
310 else if (rta->rta_type == NDA_LLADDR)
312 maclen = rta->rta_len - sizeof(struct rtattr);
313 mac = (char *)(rta+1);
316 rta = RTA_NEXT(rta, len1);
319 if (!(neigh->ndm_state & (NUD_NOARP | NUD_INCOMPLETE | NUD_FAILED)) &&
320 inaddr && mac && callback_ok)
321 if (!((*callback)(neigh->ndm_family, inaddr, mac, maclen, parm)))
325 else if (h->nlmsg_type == RTM_NEWLINK && family == AF_LOCAL)
327 struct ifinfomsg *link = NLMSG_DATA(h);
328 struct rtattr *rta = IFLA_RTA(link);
329 unsigned int len1 = h->nlmsg_len - NLMSG_LENGTH(sizeof(*link));
333 while (RTA_OK(rta, len1))
335 if (rta->rta_type == IFLA_ADDRESS)
337 maclen = rta->rta_len - sizeof(struct rtattr);
338 mac = (char *)(rta+1);
341 rta = RTA_NEXT(rta, len1);
344 if (mac && callback_ok && !((link->ifi_flags & (IFF_LOOPBACK | IFF_POINTOPOINT))) &&
345 !((*callback)((int)link->ifi_index, (unsigned int)link->ifi_type, mac, maclen, parm)))
352 static void nl_multicast_state(unsigned state)
358 /* don't risk blocking reading netlink messages here. */
359 while ((len = netlink_recv(MSG_DONTWAIT)) != -1)
361 for (h = (struct nlmsghdr *)iov.iov_base; NLMSG_OK(h, (size_t)len); h = NLMSG_NEXT(h, len))
362 state = nl_async(h, state);
363 } while (errno == ENOBUFS);
366 void netlink_multicast(void)
369 nl_multicast_state(state);
373 static unsigned nl_async(struct nlmsghdr *h, unsigned state)
375 if (h->nlmsg_type == NLMSG_ERROR)
377 struct nlmsgerr *err = NLMSG_DATA(h);
379 my_syslog(LOG_ERR, _("netlink returns error: %s"), strerror(-(err->error)));
381 else if (h->nlmsg_pid == 0 && h->nlmsg_type == RTM_NEWROUTE &&
382 (state & STATE_NEWROUTE)==0)
384 /* We arrange to receive netlink multicast messages whenever the network route is added.
385 If this happens and we still have a DNS packet in the buffer, we re-send it.
386 This helps on DoD links, where frequently the packet which triggers dialling is
387 a DNS query, which then gets lost. By re-sending, we can avoid the lookup
389 struct rtmsg *rtm = NLMSG_DATA(h);
391 if (rtm->rtm_type == RTN_UNICAST && rtm->rtm_scope == RT_SCOPE_LINK &&
392 (rtm->rtm_table == RT_TABLE_MAIN ||
393 rtm->rtm_table == RT_TABLE_LOCAL))
395 queue_event(EVENT_NEWROUTE);
396 state |= STATE_NEWROUTE;
399 else if ((h->nlmsg_type == RTM_NEWADDR || h->nlmsg_type == RTM_DELADDR) &&
400 (state & STATE_NEWADDR)==0)
402 queue_event(EVENT_NEWADDR);
403 state |= STATE_NEWADDR;
407 #endif /* HAVE_LINUX_NETWORK */