blob: ef4b5fec3197ec1a855fca3bcf8d86eaa29ca479 [file] [log] [blame]
Tarun Kundu12e3b2e2024-08-15 16:16:53 -07001/* dnsmasq is Copyright (c) 2000-2024 Simon Kelley
Simon Kelley0a852542005-03-23 20:28:59 +00002
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 Kelley824af852008-02-12 20:43:05 +00005 the Free Software Foundation; version 2 dated June, 1991, or
6 (at your option) version 3 dated 29 June, 2007.
7
Simon Kelley0a852542005-03-23 20:28:59 +00008 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 Kelley824af852008-02-12 20:43:05 +000012
Simon Kelley73a08a22009-02-05 20:28:08 +000013 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 Kelley0a852542005-03-23 20:28:59 +000015*/
16
Simon Kelley0a852542005-03-23 20:28:59 +000017#include "dnsmasq.h"
18
Simon Kelley5e9e0ef2006-04-17 14:24:29 +010019#ifdef HAVE_LINUX_NETWORK
Simon Kelley0a852542005-03-23 20:28:59 +000020
Simon Kelley91dccd02005-03-31 17:48:32 +010021#include <linux/types.h>
Simon Kelley0a852542005-03-23 20:28:59 +000022#include <linux/netlink.h>
23#include <linux/rtnetlink.h>
24
Simon Kelley1627d572020-03-10 23:55:18 +000025/* Blergh. Radv does this, so that's our excuse. */
26#ifndef SOL_NETLINK
27#define SOL_NETLINK 270
28#endif
29
Simon Kelley0506a5e2020-03-19 21:56:45 +000030#ifndef NETLINK_NO_ENOBUFS
31#define NETLINK_NO_ENOBUFS 5
32#endif
Simon Kelley1627d572020-03-10 23:55:18 +000033
Simon Kelley832af0b2007-01-21 20:01:28 +000034/* 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 Kelley28866e92011-02-14 20:19:14 +000042#ifndef NDA_RTA
43# define NDA_RTA(r) ((struct rtattr*)(((char*)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
Petr Menšík4c0aecc2021-03-02 18:21:32 +000044#endif
45
46/* Used to request refresh of addresses or routes just once,
47 * when multiple changes might be announced. */
48enum async_states {
49 STATE_NEWADDR = (1 << 0),
50 STATE_NEWROUTE = (1 << 1),
51};
Simon Kelley28866e92011-02-14 20:19:14 +000052
Simon Kelleyc72daea2012-01-05 21:33:27 +000053
Simon Kelley5e9e0ef2006-04-17 14:24:29 +010054static struct iovec iov;
Simon Kelley7622fc02009-06-04 20:32:05 +010055static u32 netlink_pid;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +010056
Petr Menšík4c0aecc2021-03-02 18:21:32 +000057static unsigned nl_async(struct nlmsghdr *h, unsigned state);
58static void nl_multicast_state(unsigned state);
Simon Kelleycdeda282006-03-16 20:16:06 +000059
Simon Kelley913fa152020-04-19 23:16:52 +010060char *netlink_init(void)
Simon Kelley0a852542005-03-23 20:28:59 +000061{
62 struct sockaddr_nl addr;
Simon Kelley7622fc02009-06-04 20:32:05 +010063 socklen_t slen = sizeof(addr);
Simon Kelley0a852542005-03-23 20:28:59 +000064
65 addr.nl_family = AF_NETLINK;
66 addr.nl_pad = 0;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +010067 addr.nl_pid = 0; /* autobind */
Simon Kelley5e9e0ef2006-04-17 14:24:29 +010068 addr.nl_groups = RTMGRP_IPV4_ROUTE;
Tarun Kundu12e3b2e2024-08-15 16:16:53 -070069 addr.nl_groups |= RTMGRP_IPV4_IFADDR;
Simon Kelley54dd3932012-06-20 11:23:38 +010070 addr.nl_groups |= RTMGRP_IPV6_ROUTE;
Tarun Kundu12e3b2e2024-08-15 16:16:53 -070071 addr.nl_groups |= RTMGRP_IPV6_IFADDR;
Simon Kelleyee875042018-10-23 22:10:17 +010072
Simon Kelley309331f2006-04-22 15:05:01 +010073 /* 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 Kelley7622fc02009-06-04 20:32:05 +010084 if (daemon->netlinkfd == -1 ||
Reiter Wolfgang5eb9dde2017-01-08 17:39:06 +000085 getsockname(daemon->netlinkfd, (struct sockaddr *)&addr, &slen) == -1)
Simon Kelley5aabfc72007-08-29 11:24:47 +010086 die(_("cannot create netlink socket: %s"), NULL, EC_MISC);
Simon Kelley0506a5e2020-03-19 21:56:45 +000087
Simon Kelley913fa152020-04-19 23:16:52 +010088
Simon Kelley7622fc02009-06-04 20:32:05 +010089 /* save pid assigned by bind() and retrieved by getsockname() */
90 netlink_pid = addr.nl_pid;
Simon Kelley5aabfc72007-08-29 11:24:47 +010091
Simon Kelley7622fc02009-06-04 20:32:05 +010092 iov.iov_len = 100;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +010093 iov.iov_base = safe_malloc(iov.iov_len);
Simon Kelley913fa152020-04-19 23:16:52 +010094
Simon Kelley913fa152020-04-19 23:16:52 +010095 return NULL;
Simon Kelley0a852542005-03-23 20:28:59 +000096}
97
Petr Menšík8b8a4142021-03-02 21:36:45 +000098static ssize_t netlink_recv(int flags)
Simon Kelleycdeda282006-03-16 20:16:06 +000099{
100 struct msghdr msg;
Simon Kelley7622fc02009-06-04 20:32:05 +0100101 struct sockaddr_nl nladdr;
Simon Kelleycdeda282006-03-16 20:16:06 +0000102 ssize_t rc;
Simon Kelley0a852542005-03-23 20:28:59 +0000103
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100104 while (1)
Simon Kelleycdeda282006-03-16 20:16:06 +0000105 {
Simon Kelley7622fc02009-06-04 20:32:05 +0100106 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;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100112 msg.msg_flags = 0;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100113
Petr Menšík8b8a4142021-03-02 21:36:45 +0000114 while ((rc = recvmsg(daemon->netlinkfd, &msg, flags | MSG_PEEK | MSG_TRUNC)) == -1 &&
115 errno == EINTR);
Simon Kelley7622fc02009-06-04 20:32:05 +0100116
117 /* make buffer big enough */
118 if (rc != -1 && (msg.msg_flags & MSG_TRUNC))
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100119 {
Simon Kelley7622fc02009-06-04 20:32:05 +0100120 /* 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);
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100128 }
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100129
Simon Kelley7622fc02009-06-04 20:32:05 +0100130 /* read it for real */
131 msg.msg_flags = 0;
Petr Menšík8b8a4142021-03-02 21:36:45 +0000132 while ((rc = recvmsg(daemon->netlinkfd, &msg, flags)) == -1 && errno == EINTR);
Simon Kelley7622fc02009-06-04 20:32:05 +0100133
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 }
Simon Kelleycdeda282006-03-16 20:16:06 +0000145
146 return rc;
147}
148
Simon Kelley28866e92011-02-14 20:19:14 +0000149
Simon Kelleyc72daea2012-01-05 21:33:27 +0000150/* family = AF_UNSPEC finds ARP table entries.
Petr Menšík4c0aecc2021-03-02 18:21:32 +0000151 family = AF_LOCAL finds MAC addresses.
152 returns 0 on failure, 1 on success, -1 when restart is required
153*/
Simon Kelley28866e92011-02-14 20:19:14 +0000154int iface_enumerate(int family, void *parm, int (*callback)())
Simon Kelley0a852542005-03-23 20:28:59 +0000155{
156 struct sockaddr_nl addr;
157 struct nlmsghdr *h;
Simon Kelleycdeda282006-03-16 20:16:06 +0000158 ssize_t len;
Simon Kelley0a852542005-03-23 20:28:59 +0000159 static unsigned int seq = 0;
Simon Kelleya0358e52014-06-07 13:38:48 +0100160 int callback_ok = 1;
Petr Menšík4c0aecc2021-03-02 18:21:32 +0000161 unsigned state = 0;
Simon Kelley0a852542005-03-23 20:28:59 +0000162
163 struct {
164 struct nlmsghdr nlh;
165 struct rtgenmsg g;
166 } req;
167
Petr Menšík47b45b22018-08-15 18:17:00 +0200168 memset(&req, 0, sizeof(req));
169 memset(&addr, 0, sizeof(addr));
170
Simon Kelley0a852542005-03-23 20:28:59 +0000171 addr.nl_family = AF_NETLINK;
Simon Kelleyc72daea2012-01-05 21:33:27 +0000172
Simon Kelleyc72daea2012-01-05 21:33:27 +0000173 if (family == AF_UNSPEC)
174 req.nlh.nlmsg_type = RTM_GETNEIGH;
175 else if (family == AF_LOCAL)
176 req.nlh.nlmsg_type = RTM_GETLINK;
177 else
178 req.nlh.nlmsg_type = RTM_GETADDR;
Simon Kelley0a852542005-03-23 20:28:59 +0000179
180 req.nlh.nlmsg_len = sizeof(req);
Simon Kelley7cebd202006-05-06 14:13:33 +0100181 req.nlh.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST | NLM_F_ACK;
Simon Kelley0a852542005-03-23 20:28:59 +0000182 req.nlh.nlmsg_pid = 0;
183 req.nlh.nlmsg_seq = ++seq;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100184 req.g.rtgen_family = family;
Simon Kelley0a852542005-03-23 20:28:59 +0000185
186 /* Don't block in recvfrom if send fails */
Simon Kelleyff841eb2015-03-11 21:36:30 +0000187 while(retry_send(sendto(daemon->netlinkfd, (void *)&req, sizeof(req), 0,
188 (struct sockaddr *)&addr, sizeof(addr))));
189
190 if (errno != 0)
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100191 return 0;
192
193 while (1)
Simon Kelley0a852542005-03-23 20:28:59 +0000194 {
Petr Menšík8b8a4142021-03-02 21:36:45 +0000195 if ((len = netlink_recv(0)) == -1)
Simon Kelley7622fc02009-06-04 20:32:05 +0100196 {
197 if (errno == ENOBUFS)
198 {
Petr Menšík4c0aecc2021-03-02 18:21:32 +0000199 nl_multicast_state(state);
200 return -1;
Simon Kelley7622fc02009-06-04 20:32:05 +0100201 }
202 return 0;
203 }
204
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100205 for (h = (struct nlmsghdr *)iov.iov_base; NLMSG_OK(h, (size_t)len); h = NLMSG_NEXT(h, len))
Ivan Kokshaysky1d076672016-07-11 18:36:05 +0100206 if (h->nlmsg_pid != netlink_pid || h->nlmsg_type == NLMSG_ERROR)
Simon Kelley54dd3932012-06-20 11:23:38 +0100207 {
208 /* May be multicast arriving async */
Petr Menšík4c0aecc2021-03-02 18:21:32 +0000209 state = nl_async(h, state);
Simon Kelley54dd3932012-06-20 11:23:38 +0100210 }
Ivan Kokshaysky1d076672016-07-11 18:36:05 +0100211 else if (h->nlmsg_seq != seq)
212 {
213 /* May be part of incomplete response to previous request after
214 ENOBUFS. Drop it. */
215 continue;
216 }
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100217 else if (h->nlmsg_type == NLMSG_DONE)
Simon Kelleya0358e52014-06-07 13:38:48 +0100218 return callback_ok;
Simon Kelleyc72daea2012-01-05 21:33:27 +0000219 else if (h->nlmsg_type == RTM_NEWADDR && family != AF_UNSPEC && family != AF_LOCAL)
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100220 {
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));
224
Simon Kelley28866e92011-02-14 20:19:14 +0000225 if (ifa->ifa_family == family)
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100226 {
Simon Kelley28866e92011-02-14 20:19:14 +0000227 if (ifa->ifa_family == AF_INET)
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100228 {
Simon Kelley28866e92011-02-14 20:19:14 +0000229 struct in_addr netmask, addr, broadcast;
Simon Kelley3f2873d2013-05-14 11:28:47 +0100230 char *label = NULL;
231
Richard Genoud10cfc0d2014-09-17 21:17:39 +0100232 netmask.s_addr = htonl(~(in_addr_t)0 << (32 - ifa->ifa_prefixlen));
233
Simon Kelley28866e92011-02-14 20:19:14 +0000234 addr.s_addr = 0;
235 broadcast.s_addr = 0;
236
237 while (RTA_OK(rta, len1))
238 {
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));
Simon Kelley3f2873d2013-05-14 11:28:47 +0100243 else if (rta->rta_type == IFA_LABEL)
244 label = RTA_DATA(rta);
Simon Kelley28866e92011-02-14 20:19:14 +0000245
246 rta = RTA_NEXT(rta, len1);
247 }
248
Simon Kelleye44ddca2012-02-18 17:08:50 +0000249 if (addr.s_addr && callback_ok)
Simon Kelley3f2873d2013-05-14 11:28:47 +0100250 if (!((*callback)(addr, ifa->ifa_index, label, netmask, broadcast, parm)))
Simon Kelleye44ddca2012-02-18 17:08:50 +0000251 callback_ok = 0;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100252 }
Simon Kelley28866e92011-02-14 20:19:14 +0000253 else if (ifa->ifa_family == AF_INET6)
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100254 {
Simon Kelley28866e92011-02-14 20:19:14 +0000255 struct in6_addr *addrp = NULL;
Simon Kelley1f776932012-12-16 19:46:08 +0000256 u32 valid = 0, preferred = 0;
Simon Kelleybad7b872012-12-20 22:00:39 +0000257 int flags = 0;
258
Simon Kelley28866e92011-02-14 20:19:14 +0000259 while (RTA_OK(rta, len1))
260 {
Tarun Kundu12e3b2e2024-08-15 16:16:53 -0700261 /*
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.
267 */
268 if (rta->rta_type == IFA_LOCAL)
269 addrp = ((struct in6_addr *)(rta+1));
270 else if (rta->rta_type == IFA_ADDRESS && !addrp)
Simon Kelley28866e92011-02-14 20:19:14 +0000271 addrp = ((struct in6_addr *)(rta+1));
Simon Kelley1f776932012-12-16 19:46:08 +0000272 else if (rta->rta_type == IFA_CACHEINFO)
273 {
274 struct ifa_cacheinfo *ifc = (struct ifa_cacheinfo *)(rta+1);
275 preferred = ifc->ifa_prefered;
276 valid = ifc->ifa_valid;
277 }
Simon Kelley28866e92011-02-14 20:19:14 +0000278 rta = RTA_NEXT(rta, len1);
279 }
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100280
Simon Kelleybad7b872012-12-20 22:00:39 +0000281 if (ifa->ifa_flags & IFA_F_TENTATIVE)
282 flags |= IFACE_TENTATIVE;
283
284 if (ifa->ifa_flags & IFA_F_DEPRECATED)
285 flags |= IFACE_DEPRECATED;
286
Jonas Gorski57ab36e2014-01-22 11:34:16 +0000287 if (!(ifa->ifa_flags & IFA_F_TEMPORARY))
288 flags |= IFACE_PERMANENT;
289
Simon Kelleye44ddca2012-02-18 17:08:50 +0000290 if (addrp && callback_ok)
Simon Kelley52b92f42012-01-22 16:05:15 +0000291 if (!((*callback)(addrp, (int)(ifa->ifa_prefixlen), (int)(ifa->ifa_scope),
Simon Kelleybad7b872012-12-20 22:00:39 +0000292 (int)(ifa->ifa_index), flags,
Simon Kelley1f776932012-12-16 19:46:08 +0000293 (int) preferred, (int)valid, parm)))
Simon Kelleye44ddca2012-02-18 17:08:50 +0000294 callback_ok = 0;
Simon Kelley28866e92011-02-14 20:19:14 +0000295 }
Simon Kelley28866e92011-02-14 20:19:14 +0000296 }
297 }
298 else if (h->nlmsg_type == RTM_NEWNEIGH && family == AF_UNSPEC)
299 {
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));
303 size_t maclen = 0;
304 char *inaddr = NULL, *mac = NULL;
305
306 while (RTA_OK(rta, len1))
307 {
308 if (rta->rta_type == NDA_DST)
309 inaddr = (char *)(rta+1);
310 else if (rta->rta_type == NDA_LLADDR)
311 {
312 maclen = rta->rta_len - sizeof(struct rtattr);
313 mac = (char *)(rta+1);
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100314 }
315
Simon Kelley28866e92011-02-14 20:19:14 +0000316 rta = RTA_NEXT(rta, len1);
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100317 }
Simon Kelley28866e92011-02-14 20:19:14 +0000318
Simon Kelleyfa14bec2015-12-20 17:12:16 +0000319 if (!(neigh->ndm_state & (NUD_NOARP | NUD_INCOMPLETE | NUD_FAILED)) &&
320 inaddr && mac && callback_ok)
Simon Kelley28866e92011-02-14 20:19:14 +0000321 if (!((*callback)(neigh->ndm_family, inaddr, mac, maclen, parm)))
Simon Kelleye44ddca2012-02-18 17:08:50 +0000322 callback_ok = 0;
Simon Kelleyc72daea2012-01-05 21:33:27 +0000323 }
324#ifdef HAVE_DHCP6
325 else if (h->nlmsg_type == RTM_NEWLINK && family == AF_LOCAL)
326 {
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));
330 char *mac = NULL;
331 size_t maclen = 0;
332
333 while (RTA_OK(rta, len1))
334 {
335 if (rta->rta_type == IFLA_ADDRESS)
336 {
337 maclen = rta->rta_len - sizeof(struct rtattr);
338 mac = (char *)(rta+1);
339 }
340
341 rta = RTA_NEXT(rta, len1);
342 }
343
Simon Kelleye44ddca2012-02-18 17:08:50 +0000344 if (mac && callback_ok && !((link->ifi_flags & (IFF_LOOPBACK | IFF_POINTOPOINT))) &&
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000345 !((*callback)((int)link->ifi_index, (unsigned int)link->ifi_type, mac, maclen, parm)))
Simon Kelleye44ddca2012-02-18 17:08:50 +0000346 callback_ok = 0;
Simon Kelleyc72daea2012-01-05 21:33:27 +0000347 }
348#endif
Simon Kelley0a852542005-03-23 20:28:59 +0000349 }
Simon Kelley0a852542005-03-23 20:28:59 +0000350}
351
Petr Menšík4c0aecc2021-03-02 18:21:32 +0000352static void nl_multicast_state(unsigned state)
Simon Kelleycdeda282006-03-16 20:16:06 +0000353{
354 ssize_t len;
355 struct nlmsghdr *h;
Petr Menšík4c0aecc2021-03-02 18:21:32 +0000356
357 do {
358 /* don't risk blocking reading netlink messages here. */
Petr Menšík8b8a4142021-03-02 21:36:45 +0000359 while ((len = netlink_recv(MSG_DONTWAIT)) != -1)
Simon Kelleycdeda282006-03-16 20:16:06 +0000360
Petr Menšík4c0aecc2021-03-02 18:21:32 +0000361 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);
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100364}
365
Petr Menšík4c0aecc2021-03-02 18:21:32 +0000366void netlink_multicast(void)
367{
368 unsigned state = 0;
369 nl_multicast_state(state);
370}
371
372
373static unsigned nl_async(struct nlmsghdr *h, unsigned state)
Simon Kelley54dd3932012-06-20 11:23:38 +0100374{
375 if (h->nlmsg_type == NLMSG_ERROR)
376 {
377 struct nlmsgerr *err = NLMSG_DATA(h);
Simon Kelleydfb23b32012-09-18 21:44:47 +0100378 if (err->error != 0)
379 my_syslog(LOG_ERR, _("netlink returns error: %s"), strerror(-(err->error)));
Simon Kelley54dd3932012-06-20 11:23:38 +0100380 }
Petr Menšík4c0aecc2021-03-02 18:21:32 +0000381 else if (h->nlmsg_pid == 0 && h->nlmsg_type == RTM_NEWROUTE &&
382 (state & STATE_NEWROUTE)==0)
Simon Kelley54dd3932012-06-20 11:23:38 +0100383 {
Simon Kelleye17b4b32012-06-28 21:44:30 +0100384 /* 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
388 failing. */
389 struct rtmsg *rtm = NLMSG_DATA(h);
390
Donald Sharpb2ed6912020-03-02 11:23:36 -0500391 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))
Petr Menšík4c0aecc2021-03-02 18:21:32 +0000394 {
395 queue_event(EVENT_NEWROUTE);
396 state |= STATE_NEWROUTE;
397 }
Simon Kelley54dd3932012-06-20 11:23:38 +0100398 }
Petr Menšík4c0aecc2021-03-02 18:21:32 +0000399 else if ((h->nlmsg_type == RTM_NEWADDR || h->nlmsg_type == RTM_DELADDR) &&
400 (state & STATE_NEWADDR)==0)
401 {
402 queue_event(EVENT_NEWADDR);
403 state |= STATE_NEWADDR;
404 }
405 return state;
Simon Kelley54dd3932012-06-20 11:23:38 +0100406}
Tarun Kundu12e3b2e2024-08-15 16:16:53 -0700407#endif /* HAVE_LINUX_NETWORK */