blob: 3ad18a608469e760d95fed0c4c3aba25dab60864 [file] [log] [blame]
Simon Kelleyc8e8f5c2021-01-24 21:59:37 +00001/* dnsmasq is Copyright (c) 2000-2021 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;
Simon Kelley54dd3932012-06-20 11:23:38 +010069 if (option_bool(OPT_CLEVERBIND))
Simon Kelley1f776932012-12-16 19:46:08 +000070 addr.nl_groups |= RTMGRP_IPV4_IFADDR;
Simon Kelley54dd3932012-06-20 11:23:38 +010071 addr.nl_groups |= RTMGRP_IPV6_ROUTE;
Simon Kelley1f776932012-12-16 19:46:08 +000072 if (option_bool(OPT_CLEVERBIND))
73 addr.nl_groups |= RTMGRP_IPV6_IFADDR;
Simon Kelleyee875042018-10-23 22:10:17 +010074
Simon Kelley1f776932012-12-16 19:46:08 +000075#ifdef HAVE_DHCP6
76 if (daemon->doing_ra || daemon->doing_dhcp6)
Simon Kelley54dd3932012-06-20 11:23:38 +010077 addr.nl_groups |= RTMGRP_IPV6_IFADDR;
Simon Kelleycdeda282006-03-16 20:16:06 +000078#endif
Simon Kelley0a852542005-03-23 20:28:59 +000079
Simon Kelley309331f2006-04-22 15:05:01 +010080 /* May not be able to have permission to set multicast groups don't die in that case */
81 if ((daemon->netlinkfd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) != -1)
82 {
83 if (bind(daemon->netlinkfd, (struct sockaddr *)&addr, sizeof(addr)) == -1)
84 {
85 addr.nl_groups = 0;
86 if (errno != EPERM || bind(daemon->netlinkfd, (struct sockaddr *)&addr, sizeof(addr)) == -1)
87 daemon->netlinkfd = -1;
88 }
89 }
90
Simon Kelley7622fc02009-06-04 20:32:05 +010091 if (daemon->netlinkfd == -1 ||
Reiter Wolfgang5eb9dde2017-01-08 17:39:06 +000092 getsockname(daemon->netlinkfd, (struct sockaddr *)&addr, &slen) == -1)
Simon Kelley5aabfc72007-08-29 11:24:47 +010093 die(_("cannot create netlink socket: %s"), NULL, EC_MISC);
Simon Kelley0506a5e2020-03-19 21:56:45 +000094
Simon Kelley913fa152020-04-19 23:16:52 +010095
Simon Kelley7622fc02009-06-04 20:32:05 +010096 /* save pid assigned by bind() and retrieved by getsockname() */
97 netlink_pid = addr.nl_pid;
Simon Kelley5aabfc72007-08-29 11:24:47 +010098
Simon Kelley7622fc02009-06-04 20:32:05 +010099 iov.iov_len = 100;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100100 iov.iov_base = safe_malloc(iov.iov_len);
Simon Kelley913fa152020-04-19 23:16:52 +0100101
Simon Kelley913fa152020-04-19 23:16:52 +0100102 return NULL;
Simon Kelley0a852542005-03-23 20:28:59 +0000103}
104
Simon Kelley5aabfc72007-08-29 11:24:47 +0100105static ssize_t netlink_recv(void)
Simon Kelleycdeda282006-03-16 20:16:06 +0000106{
107 struct msghdr msg;
Simon Kelley7622fc02009-06-04 20:32:05 +0100108 struct sockaddr_nl nladdr;
Simon Kelleycdeda282006-03-16 20:16:06 +0000109 ssize_t rc;
Simon Kelley0a852542005-03-23 20:28:59 +0000110
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100111 while (1)
Simon Kelleycdeda282006-03-16 20:16:06 +0000112 {
Simon Kelley7622fc02009-06-04 20:32:05 +0100113 msg.msg_control = NULL;
114 msg.msg_controllen = 0;
115 msg.msg_name = &nladdr;
116 msg.msg_namelen = sizeof(nladdr);
117 msg.msg_iov = &iov;
118 msg.msg_iovlen = 1;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100119 msg.msg_flags = 0;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100120
Simon Kelley7622fc02009-06-04 20:32:05 +0100121 while ((rc = recvmsg(daemon->netlinkfd, &msg, MSG_PEEK | MSG_TRUNC)) == -1 && errno == EINTR);
122
123 /* make buffer big enough */
124 if (rc != -1 && (msg.msg_flags & MSG_TRUNC))
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100125 {
Simon Kelley7622fc02009-06-04 20:32:05 +0100126 /* Very new Linux kernels return the actual size needed, older ones always return truncated size */
127 if ((size_t)rc == iov.iov_len)
128 {
129 if (expand_buf(&iov, rc + 100))
130 continue;
131 }
132 else
133 expand_buf(&iov, rc);
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100134 }
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100135
Simon Kelley7622fc02009-06-04 20:32:05 +0100136 /* read it for real */
137 msg.msg_flags = 0;
138 while ((rc = recvmsg(daemon->netlinkfd, &msg, 0)) == -1 && errno == EINTR);
139
140 /* Make sure this is from the kernel */
141 if (rc == -1 || nladdr.nl_pid == 0)
142 break;
143 }
144
145 /* discard stuff which is truncated at this point (expand_buf() may fail) */
146 if (msg.msg_flags & MSG_TRUNC)
147 {
148 rc = -1;
149 errno = ENOMEM;
150 }
Simon Kelleycdeda282006-03-16 20:16:06 +0000151
152 return rc;
153}
154
Simon Kelley28866e92011-02-14 20:19:14 +0000155
Simon Kelleyc72daea2012-01-05 21:33:27 +0000156/* family = AF_UNSPEC finds ARP table entries.
Petr Menšík4c0aecc2021-03-02 18:21:32 +0000157 family = AF_LOCAL finds MAC addresses.
158 returns 0 on failure, 1 on success, -1 when restart is required
159*/
Simon Kelley28866e92011-02-14 20:19:14 +0000160int iface_enumerate(int family, void *parm, int (*callback)())
Simon Kelley0a852542005-03-23 20:28:59 +0000161{
162 struct sockaddr_nl addr;
163 struct nlmsghdr *h;
Simon Kelleycdeda282006-03-16 20:16:06 +0000164 ssize_t len;
Simon Kelley0a852542005-03-23 20:28:59 +0000165 static unsigned int seq = 0;
Simon Kelleya0358e52014-06-07 13:38:48 +0100166 int callback_ok = 1;
Petr Menšík4c0aecc2021-03-02 18:21:32 +0000167 unsigned state = 0;
Simon Kelley0a852542005-03-23 20:28:59 +0000168
169 struct {
170 struct nlmsghdr nlh;
171 struct rtgenmsg g;
172 } req;
173
Petr Menšík47b45b22018-08-15 18:17:00 +0200174 memset(&req, 0, sizeof(req));
175 memset(&addr, 0, sizeof(addr));
176
Simon Kelley0a852542005-03-23 20:28:59 +0000177 addr.nl_family = AF_NETLINK;
Simon Kelleyc72daea2012-01-05 21:33:27 +0000178
Simon Kelleyc72daea2012-01-05 21:33:27 +0000179 if (family == AF_UNSPEC)
180 req.nlh.nlmsg_type = RTM_GETNEIGH;
181 else if (family == AF_LOCAL)
182 req.nlh.nlmsg_type = RTM_GETLINK;
183 else
184 req.nlh.nlmsg_type = RTM_GETADDR;
Simon Kelley0a852542005-03-23 20:28:59 +0000185
186 req.nlh.nlmsg_len = sizeof(req);
Simon Kelley7cebd202006-05-06 14:13:33 +0100187 req.nlh.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST | NLM_F_ACK;
Simon Kelley0a852542005-03-23 20:28:59 +0000188 req.nlh.nlmsg_pid = 0;
189 req.nlh.nlmsg_seq = ++seq;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100190 req.g.rtgen_family = family;
Simon Kelley0a852542005-03-23 20:28:59 +0000191
192 /* Don't block in recvfrom if send fails */
Simon Kelleyff841eb2015-03-11 21:36:30 +0000193 while(retry_send(sendto(daemon->netlinkfd, (void *)&req, sizeof(req), 0,
194 (struct sockaddr *)&addr, sizeof(addr))));
195
196 if (errno != 0)
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100197 return 0;
198
199 while (1)
Simon Kelley0a852542005-03-23 20:28:59 +0000200 {
Simon Kelley5aabfc72007-08-29 11:24:47 +0100201 if ((len = netlink_recv()) == -1)
Simon Kelley7622fc02009-06-04 20:32:05 +0100202 {
203 if (errno == ENOBUFS)
204 {
Petr Menšík4c0aecc2021-03-02 18:21:32 +0000205 nl_multicast_state(state);
206 return -1;
Simon Kelley7622fc02009-06-04 20:32:05 +0100207 }
208 return 0;
209 }
210
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100211 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 +0100212 if (h->nlmsg_pid != netlink_pid || h->nlmsg_type == NLMSG_ERROR)
Simon Kelley54dd3932012-06-20 11:23:38 +0100213 {
214 /* May be multicast arriving async */
Petr Menšík4c0aecc2021-03-02 18:21:32 +0000215 state = nl_async(h, state);
Simon Kelley54dd3932012-06-20 11:23:38 +0100216 }
Ivan Kokshaysky1d076672016-07-11 18:36:05 +0100217 else if (h->nlmsg_seq != seq)
218 {
219 /* May be part of incomplete response to previous request after
220 ENOBUFS. Drop it. */
221 continue;
222 }
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100223 else if (h->nlmsg_type == NLMSG_DONE)
Simon Kelleya0358e52014-06-07 13:38:48 +0100224 return callback_ok;
Simon Kelleyc72daea2012-01-05 21:33:27 +0000225 else if (h->nlmsg_type == RTM_NEWADDR && family != AF_UNSPEC && family != AF_LOCAL)
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100226 {
227 struct ifaddrmsg *ifa = NLMSG_DATA(h);
228 struct rtattr *rta = IFA_RTA(ifa);
229 unsigned int len1 = h->nlmsg_len - NLMSG_LENGTH(sizeof(*ifa));
230
Simon Kelley28866e92011-02-14 20:19:14 +0000231 if (ifa->ifa_family == family)
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100232 {
Simon Kelley28866e92011-02-14 20:19:14 +0000233 if (ifa->ifa_family == AF_INET)
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100234 {
Simon Kelley28866e92011-02-14 20:19:14 +0000235 struct in_addr netmask, addr, broadcast;
Simon Kelley3f2873d2013-05-14 11:28:47 +0100236 char *label = NULL;
237
Richard Genoud10cfc0d2014-09-17 21:17:39 +0100238 netmask.s_addr = htonl(~(in_addr_t)0 << (32 - ifa->ifa_prefixlen));
239
Simon Kelley28866e92011-02-14 20:19:14 +0000240 addr.s_addr = 0;
241 broadcast.s_addr = 0;
242
243 while (RTA_OK(rta, len1))
244 {
245 if (rta->rta_type == IFA_LOCAL)
246 addr = *((struct in_addr *)(rta+1));
247 else if (rta->rta_type == IFA_BROADCAST)
248 broadcast = *((struct in_addr *)(rta+1));
Simon Kelley3f2873d2013-05-14 11:28:47 +0100249 else if (rta->rta_type == IFA_LABEL)
250 label = RTA_DATA(rta);
Simon Kelley28866e92011-02-14 20:19:14 +0000251
252 rta = RTA_NEXT(rta, len1);
253 }
254
Simon Kelleye44ddca2012-02-18 17:08:50 +0000255 if (addr.s_addr && callback_ok)
Simon Kelley3f2873d2013-05-14 11:28:47 +0100256 if (!((*callback)(addr, ifa->ifa_index, label, netmask, broadcast, parm)))
Simon Kelleye44ddca2012-02-18 17:08:50 +0000257 callback_ok = 0;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100258 }
Simon Kelley28866e92011-02-14 20:19:14 +0000259 else if (ifa->ifa_family == AF_INET6)
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100260 {
Simon Kelley28866e92011-02-14 20:19:14 +0000261 struct in6_addr *addrp = NULL;
Simon Kelley1f776932012-12-16 19:46:08 +0000262 u32 valid = 0, preferred = 0;
Simon Kelleybad7b872012-12-20 22:00:39 +0000263 int flags = 0;
264
Simon Kelley28866e92011-02-14 20:19:14 +0000265 while (RTA_OK(rta, len1))
266 {
267 if (rta->rta_type == IFA_ADDRESS)
268 addrp = ((struct in6_addr *)(rta+1));
Simon Kelley1f776932012-12-16 19:46:08 +0000269 else if (rta->rta_type == IFA_CACHEINFO)
270 {
271 struct ifa_cacheinfo *ifc = (struct ifa_cacheinfo *)(rta+1);
272 preferred = ifc->ifa_prefered;
273 valid = ifc->ifa_valid;
274 }
Simon Kelley28866e92011-02-14 20:19:14 +0000275 rta = RTA_NEXT(rta, len1);
276 }
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100277
Simon Kelleybad7b872012-12-20 22:00:39 +0000278 if (ifa->ifa_flags & IFA_F_TENTATIVE)
279 flags |= IFACE_TENTATIVE;
280
281 if (ifa->ifa_flags & IFA_F_DEPRECATED)
282 flags |= IFACE_DEPRECATED;
283
Jonas Gorski57ab36e2014-01-22 11:34:16 +0000284 if (!(ifa->ifa_flags & IFA_F_TEMPORARY))
285 flags |= IFACE_PERMANENT;
286
Simon Kelleye44ddca2012-02-18 17:08:50 +0000287 if (addrp && callback_ok)
Simon Kelley52b92f42012-01-22 16:05:15 +0000288 if (!((*callback)(addrp, (int)(ifa->ifa_prefixlen), (int)(ifa->ifa_scope),
Simon Kelleybad7b872012-12-20 22:00:39 +0000289 (int)(ifa->ifa_index), flags,
Simon Kelley1f776932012-12-16 19:46:08 +0000290 (int) preferred, (int)valid, parm)))
Simon Kelleye44ddca2012-02-18 17:08:50 +0000291 callback_ok = 0;
Simon Kelley28866e92011-02-14 20:19:14 +0000292 }
Simon Kelley28866e92011-02-14 20:19:14 +0000293 }
294 }
295 else if (h->nlmsg_type == RTM_NEWNEIGH && family == AF_UNSPEC)
296 {
297 struct ndmsg *neigh = NLMSG_DATA(h);
298 struct rtattr *rta = NDA_RTA(neigh);
299 unsigned int len1 = h->nlmsg_len - NLMSG_LENGTH(sizeof(*neigh));
300 size_t maclen = 0;
301 char *inaddr = NULL, *mac = NULL;
302
303 while (RTA_OK(rta, len1))
304 {
305 if (rta->rta_type == NDA_DST)
306 inaddr = (char *)(rta+1);
307 else if (rta->rta_type == NDA_LLADDR)
308 {
309 maclen = rta->rta_len - sizeof(struct rtattr);
310 mac = (char *)(rta+1);
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100311 }
312
Simon Kelley28866e92011-02-14 20:19:14 +0000313 rta = RTA_NEXT(rta, len1);
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100314 }
Simon Kelley28866e92011-02-14 20:19:14 +0000315
Simon Kelleyfa14bec2015-12-20 17:12:16 +0000316 if (!(neigh->ndm_state & (NUD_NOARP | NUD_INCOMPLETE | NUD_FAILED)) &&
317 inaddr && mac && callback_ok)
Simon Kelley28866e92011-02-14 20:19:14 +0000318 if (!((*callback)(neigh->ndm_family, inaddr, mac, maclen, parm)))
Simon Kelleye44ddca2012-02-18 17:08:50 +0000319 callback_ok = 0;
Simon Kelleyc72daea2012-01-05 21:33:27 +0000320 }
321#ifdef HAVE_DHCP6
322 else if (h->nlmsg_type == RTM_NEWLINK && family == AF_LOCAL)
323 {
324 struct ifinfomsg *link = NLMSG_DATA(h);
325 struct rtattr *rta = IFLA_RTA(link);
326 unsigned int len1 = h->nlmsg_len - NLMSG_LENGTH(sizeof(*link));
327 char *mac = NULL;
328 size_t maclen = 0;
329
330 while (RTA_OK(rta, len1))
331 {
332 if (rta->rta_type == IFLA_ADDRESS)
333 {
334 maclen = rta->rta_len - sizeof(struct rtattr);
335 mac = (char *)(rta+1);
336 }
337
338 rta = RTA_NEXT(rta, len1);
339 }
340
Simon Kelleye44ddca2012-02-18 17:08:50 +0000341 if (mac && callback_ok && !((link->ifi_flags & (IFF_LOOPBACK | IFF_POINTOPOINT))) &&
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000342 !((*callback)((int)link->ifi_index, (unsigned int)link->ifi_type, mac, maclen, parm)))
Simon Kelleye44ddca2012-02-18 17:08:50 +0000343 callback_ok = 0;
Simon Kelleyc72daea2012-01-05 21:33:27 +0000344 }
345#endif
Simon Kelley0a852542005-03-23 20:28:59 +0000346 }
Simon Kelley0a852542005-03-23 20:28:59 +0000347}
348
Petr Menšík4c0aecc2021-03-02 18:21:32 +0000349static void nl_multicast_state(unsigned state)
Simon Kelleycdeda282006-03-16 20:16:06 +0000350{
351 ssize_t len;
352 struct nlmsghdr *h;
Simon Kelleya0358e52014-06-07 13:38:48 +0100353 int flags;
Petr Menšík4c0aecc2021-03-02 18:21:32 +0000354
Simon Kelley7622fc02009-06-04 20:32:05 +0100355 if ((flags = fcntl(daemon->netlinkfd, F_GETFL)) == -1 ||
356 fcntl(daemon->netlinkfd, F_SETFL, flags | O_NONBLOCK) == -1)
357 return;
Petr Menšík4c0aecc2021-03-02 18:21:32 +0000358
359 do {
360 /* don't risk blocking reading netlink messages here. */
361 while ((len = netlink_recv()) != -1)
Simon Kelleycdeda282006-03-16 20:16:06 +0000362
Petr Menšík4c0aecc2021-03-02 18:21:32 +0000363 for (h = (struct nlmsghdr *)iov.iov_base; NLMSG_OK(h, (size_t)len); h = NLMSG_NEXT(h, len))
364 state = nl_async(h, state);
365 } while (errno == ENOBUFS);
366
Simon Kelley54dd3932012-06-20 11:23:38 +0100367 /* restore non-blocking status */
368 fcntl(daemon->netlinkfd, F_SETFL, flags);
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100369}
370
Petr Menšík4c0aecc2021-03-02 18:21:32 +0000371void netlink_multicast(void)
372{
373 unsigned state = 0;
374 nl_multicast_state(state);
375}
376
377
378static unsigned nl_async(struct nlmsghdr *h, unsigned state)
Simon Kelley54dd3932012-06-20 11:23:38 +0100379{
380 if (h->nlmsg_type == NLMSG_ERROR)
381 {
382 struct nlmsgerr *err = NLMSG_DATA(h);
Simon Kelleydfb23b32012-09-18 21:44:47 +0100383 if (err->error != 0)
384 my_syslog(LOG_ERR, _("netlink returns error: %s"), strerror(-(err->error)));
Simon Kelley54dd3932012-06-20 11:23:38 +0100385 }
Petr Menšík4c0aecc2021-03-02 18:21:32 +0000386 else if (h->nlmsg_pid == 0 && h->nlmsg_type == RTM_NEWROUTE &&
387 (state & STATE_NEWROUTE)==0)
Simon Kelley54dd3932012-06-20 11:23:38 +0100388 {
Simon Kelleye17b4b32012-06-28 21:44:30 +0100389 /* We arrange to receive netlink multicast messages whenever the network route is added.
390 If this happens and we still have a DNS packet in the buffer, we re-send it.
391 This helps on DoD links, where frequently the packet which triggers dialling is
392 a DNS query, which then gets lost. By re-sending, we can avoid the lookup
393 failing. */
394 struct rtmsg *rtm = NLMSG_DATA(h);
395
Donald Sharpb2ed6912020-03-02 11:23:36 -0500396 if (rtm->rtm_type == RTN_UNICAST && rtm->rtm_scope == RT_SCOPE_LINK &&
397 (rtm->rtm_table == RT_TABLE_MAIN ||
398 rtm->rtm_table == RT_TABLE_LOCAL))
Petr Menšík4c0aecc2021-03-02 18:21:32 +0000399 {
400 queue_event(EVENT_NEWROUTE);
401 state |= STATE_NEWROUTE;
402 }
Simon Kelley54dd3932012-06-20 11:23:38 +0100403 }
Petr Menšík4c0aecc2021-03-02 18:21:32 +0000404 else if ((h->nlmsg_type == RTM_NEWADDR || h->nlmsg_type == RTM_DELADDR) &&
405 (state & STATE_NEWADDR)==0)
406 {
407 queue_event(EVENT_NEWADDR);
408 state |= STATE_NEWADDR;
409 }
410 return state;
Simon Kelley54dd3932012-06-20 11:23:38 +0100411}
Simon Kelley0a852542005-03-23 20:28:59 +0000412#endif
413
414