Simon Kelley | 5954608 | 2012-01-06 20:02:04 +0000 | [diff] [blame^] | 1 | /* dnsmasq is Copyright (c) 2000-2012 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 | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 25 | /* linux 2.6.19 buggers up the headers, patch it up here. */ |
| 26 | #ifndef IFA_RTA |
| 27 | # define IFA_RTA(r) \ |
| 28 | ((struct rtattr*)(((char*)(r)) + NLMSG_ALIGN(sizeof(struct ifaddrmsg)))) |
| 29 | |
| 30 | # include <linux/if_addr.h> |
| 31 | #endif |
| 32 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 33 | #ifndef NDA_RTA |
| 34 | # define NDA_RTA(r) ((struct rtattr*)(((char*)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg)))) |
| 35 | #endif |
| 36 | |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 37 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 38 | static struct iovec iov; |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 39 | static u32 netlink_pid; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 40 | |
| 41 | static void nl_err(struct nlmsghdr *h); |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 42 | static void nl_routechange(struct nlmsghdr *h); |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 43 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 44 | void netlink_init(void) |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 45 | { |
| 46 | struct sockaddr_nl addr; |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 47 | socklen_t slen = sizeof(addr); |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 48 | |
| 49 | addr.nl_family = AF_NETLINK; |
| 50 | addr.nl_pad = 0; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 51 | addr.nl_pid = 0; /* autobind */ |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 52 | #ifdef HAVE_IPV6 |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 53 | addr.nl_groups = RTMGRP_IPV4_ROUTE | RTMGRP_IPV6_ROUTE; |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 54 | #else |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 55 | addr.nl_groups = RTMGRP_IPV4_ROUTE; |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 56 | #endif |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 57 | |
Simon Kelley | 309331f | 2006-04-22 15:05:01 +0100 | [diff] [blame] | 58 | /* May not be able to have permission to set multicast groups don't die in that case */ |
| 59 | if ((daemon->netlinkfd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) != -1) |
| 60 | { |
| 61 | if (bind(daemon->netlinkfd, (struct sockaddr *)&addr, sizeof(addr)) == -1) |
| 62 | { |
| 63 | addr.nl_groups = 0; |
| 64 | if (errno != EPERM || bind(daemon->netlinkfd, (struct sockaddr *)&addr, sizeof(addr)) == -1) |
| 65 | daemon->netlinkfd = -1; |
| 66 | } |
| 67 | } |
| 68 | |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 69 | if (daemon->netlinkfd == -1 || |
| 70 | getsockname(daemon->netlinkfd, (struct sockaddr *)&addr, &slen) == 1) |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 71 | die(_("cannot create netlink socket: %s"), NULL, EC_MISC); |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 72 | |
| 73 | /* save pid assigned by bind() and retrieved by getsockname() */ |
| 74 | netlink_pid = addr.nl_pid; |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 75 | |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 76 | iov.iov_len = 100; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 77 | iov.iov_base = safe_malloc(iov.iov_len); |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 80 | static ssize_t netlink_recv(void) |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 81 | { |
| 82 | struct msghdr msg; |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 83 | struct sockaddr_nl nladdr; |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 84 | ssize_t rc; |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 85 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 86 | while (1) |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 87 | { |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 88 | msg.msg_control = NULL; |
| 89 | msg.msg_controllen = 0; |
| 90 | msg.msg_name = &nladdr; |
| 91 | msg.msg_namelen = sizeof(nladdr); |
| 92 | msg.msg_iov = &iov; |
| 93 | msg.msg_iovlen = 1; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 94 | msg.msg_flags = 0; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 95 | |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 96 | while ((rc = recvmsg(daemon->netlinkfd, &msg, MSG_PEEK | MSG_TRUNC)) == -1 && errno == EINTR); |
| 97 | |
| 98 | /* make buffer big enough */ |
| 99 | if (rc != -1 && (msg.msg_flags & MSG_TRUNC)) |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 100 | { |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 101 | /* Very new Linux kernels return the actual size needed, older ones always return truncated size */ |
| 102 | if ((size_t)rc == iov.iov_len) |
| 103 | { |
| 104 | if (expand_buf(&iov, rc + 100)) |
| 105 | continue; |
| 106 | } |
| 107 | else |
| 108 | expand_buf(&iov, rc); |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 109 | } |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 110 | |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 111 | /* read it for real */ |
| 112 | msg.msg_flags = 0; |
| 113 | while ((rc = recvmsg(daemon->netlinkfd, &msg, 0)) == -1 && errno == EINTR); |
| 114 | |
| 115 | /* Make sure this is from the kernel */ |
| 116 | if (rc == -1 || nladdr.nl_pid == 0) |
| 117 | break; |
| 118 | } |
| 119 | |
| 120 | /* discard stuff which is truncated at this point (expand_buf() may fail) */ |
| 121 | if (msg.msg_flags & MSG_TRUNC) |
| 122 | { |
| 123 | rc = -1; |
| 124 | errno = ENOMEM; |
| 125 | } |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 126 | |
| 127 | return rc; |
| 128 | } |
| 129 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 130 | |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 131 | /* family = AF_UNSPEC finds ARP table entries. |
| 132 | family = AF_LOCAL finds MAC addresses. */ |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 133 | int iface_enumerate(int family, void *parm, int (*callback)()) |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 134 | { |
| 135 | struct sockaddr_nl addr; |
| 136 | struct nlmsghdr *h; |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 137 | ssize_t len; |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 138 | static unsigned int seq = 0; |
| 139 | |
| 140 | struct { |
| 141 | struct nlmsghdr nlh; |
| 142 | struct rtgenmsg g; |
| 143 | } req; |
| 144 | |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 145 | addr.nl_family = AF_NETLINK; |
| 146 | addr.nl_pad = 0; |
| 147 | addr.nl_groups = 0; |
| 148 | addr.nl_pid = 0; /* address to kernel */ |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 149 | |
| 150 | again: |
| 151 | if (family == AF_UNSPEC) |
| 152 | req.nlh.nlmsg_type = RTM_GETNEIGH; |
| 153 | else if (family == AF_LOCAL) |
| 154 | req.nlh.nlmsg_type = RTM_GETLINK; |
| 155 | else |
| 156 | req.nlh.nlmsg_type = RTM_GETADDR; |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 157 | |
| 158 | req.nlh.nlmsg_len = sizeof(req); |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 159 | 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] | 160 | req.nlh.nlmsg_pid = 0; |
| 161 | req.nlh.nlmsg_seq = ++seq; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 162 | req.g.rtgen_family = family; |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 163 | |
| 164 | /* Don't block in recvfrom if send fails */ |
| 165 | while((len = sendto(daemon->netlinkfd, (void *)&req, sizeof(req), 0, |
| 166 | (struct sockaddr *)&addr, sizeof(addr))) == -1 && retry_send()); |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 167 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 168 | if (len == -1) |
| 169 | return 0; |
| 170 | |
| 171 | while (1) |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 172 | { |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 173 | if ((len = netlink_recv()) == -1) |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 174 | { |
| 175 | if (errno == ENOBUFS) |
| 176 | { |
| 177 | sleep(1); |
| 178 | goto again; |
| 179 | } |
| 180 | return 0; |
| 181 | } |
| 182 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 183 | for (h = (struct nlmsghdr *)iov.iov_base; NLMSG_OK(h, (size_t)len); h = NLMSG_NEXT(h, len)) |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 184 | if (h->nlmsg_seq != seq || h->nlmsg_pid != netlink_pid) |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 185 | nl_routechange(h); /* May be multicast arriving async */ |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 186 | else if (h->nlmsg_type == NLMSG_ERROR) |
| 187 | nl_err(h); |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 188 | else if (h->nlmsg_type == NLMSG_DONE) |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 189 | return 1; |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 190 | 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] | 191 | { |
| 192 | struct ifaddrmsg *ifa = NLMSG_DATA(h); |
| 193 | struct rtattr *rta = IFA_RTA(ifa); |
| 194 | unsigned int len1 = h->nlmsg_len - NLMSG_LENGTH(sizeof(*ifa)); |
| 195 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 196 | if (ifa->ifa_family == family) |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 197 | { |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 198 | if (ifa->ifa_family == AF_INET) |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 199 | { |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 200 | struct in_addr netmask, addr, broadcast; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 201 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 202 | netmask.s_addr = htonl(0xffffffff << (32 - ifa->ifa_prefixlen)); |
| 203 | addr.s_addr = 0; |
| 204 | broadcast.s_addr = 0; |
| 205 | |
| 206 | while (RTA_OK(rta, len1)) |
| 207 | { |
| 208 | if (rta->rta_type == IFA_LOCAL) |
| 209 | addr = *((struct in_addr *)(rta+1)); |
| 210 | else if (rta->rta_type == IFA_BROADCAST) |
| 211 | broadcast = *((struct in_addr *)(rta+1)); |
| 212 | |
| 213 | rta = RTA_NEXT(rta, len1); |
| 214 | } |
| 215 | |
| 216 | if (addr.s_addr) |
| 217 | if (!((*callback)(addr, ifa->ifa_index, netmask, broadcast, parm))) |
| 218 | return 0; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 219 | } |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 220 | #ifdef HAVE_IPV6 |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 221 | else if (ifa->ifa_family == AF_INET6) |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 222 | { |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 223 | struct in6_addr *addrp = NULL; |
| 224 | while (RTA_OK(rta, len1)) |
| 225 | { |
| 226 | if (rta->rta_type == IFA_ADDRESS) |
| 227 | addrp = ((struct in6_addr *)(rta+1)); |
| 228 | |
| 229 | rta = RTA_NEXT(rta, len1); |
| 230 | } |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 231 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 232 | if (addrp) |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 233 | if (!((*callback)(addrp, ifa->ifa_prefixlen, ifa->ifa_index, |
Simon Kelley | 74c95c2 | 2011-10-19 09:33:39 +0100 | [diff] [blame] | 234 | ifa->ifa_index, ifa->ifa_flags & IFA_F_TENTATIVE, parm))) |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 235 | return 0; |
| 236 | } |
| 237 | #endif |
| 238 | } |
| 239 | } |
| 240 | else if (h->nlmsg_type == RTM_NEWNEIGH && family == AF_UNSPEC) |
| 241 | { |
| 242 | struct ndmsg *neigh = NLMSG_DATA(h); |
| 243 | struct rtattr *rta = NDA_RTA(neigh); |
| 244 | unsigned int len1 = h->nlmsg_len - NLMSG_LENGTH(sizeof(*neigh)); |
| 245 | size_t maclen = 0; |
| 246 | char *inaddr = NULL, *mac = NULL; |
| 247 | |
| 248 | while (RTA_OK(rta, len1)) |
| 249 | { |
| 250 | if (rta->rta_type == NDA_DST) |
| 251 | inaddr = (char *)(rta+1); |
| 252 | else if (rta->rta_type == NDA_LLADDR) |
| 253 | { |
| 254 | maclen = rta->rta_len - sizeof(struct rtattr); |
| 255 | mac = (char *)(rta+1); |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 256 | } |
| 257 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 258 | rta = RTA_NEXT(rta, len1); |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 259 | } |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 260 | |
| 261 | if (inaddr && mac) |
| 262 | if (!((*callback)(neigh->ndm_family, inaddr, mac, maclen, parm))) |
| 263 | return 0; |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 264 | } |
| 265 | #ifdef HAVE_DHCP6 |
| 266 | else if (h->nlmsg_type == RTM_NEWLINK && family == AF_LOCAL) |
| 267 | { |
| 268 | struct ifinfomsg *link = NLMSG_DATA(h); |
| 269 | struct rtattr *rta = IFLA_RTA(link); |
| 270 | unsigned int len1 = h->nlmsg_len - NLMSG_LENGTH(sizeof(*link)); |
| 271 | char *mac = NULL; |
| 272 | size_t maclen = 0; |
| 273 | |
| 274 | while (RTA_OK(rta, len1)) |
| 275 | { |
| 276 | if (rta->rta_type == IFLA_ADDRESS) |
| 277 | { |
| 278 | maclen = rta->rta_len - sizeof(struct rtattr); |
| 279 | mac = (char *)(rta+1); |
| 280 | } |
| 281 | |
| 282 | rta = RTA_NEXT(rta, len1); |
| 283 | } |
| 284 | |
| 285 | if (mac && !((*callback)(link->ifi_type, link->ifi_flags, mac, maclen, parm))) |
| 286 | return 0; |
| 287 | } |
| 288 | #endif |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 289 | } |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 290 | } |
| 291 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 292 | void netlink_multicast(void) |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 293 | { |
| 294 | ssize_t len; |
| 295 | struct nlmsghdr *h; |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 296 | int flags; |
| 297 | |
| 298 | /* don't risk blocking reading netlink messages here. */ |
| 299 | if ((flags = fcntl(daemon->netlinkfd, F_GETFL)) == -1 || |
| 300 | fcntl(daemon->netlinkfd, F_SETFL, flags | O_NONBLOCK) == -1) |
| 301 | return; |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 302 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 303 | if ((len = netlink_recv()) != -1) |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 304 | { |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 305 | for (h = (struct nlmsghdr *)iov.iov_base; NLMSG_OK(h, (size_t)len); h = NLMSG_NEXT(h, len)) |
| 306 | if (h->nlmsg_type == NLMSG_ERROR) |
| 307 | nl_err(h); |
| 308 | else |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 309 | nl_routechange(h); |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 310 | } |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 311 | |
| 312 | /* restore non-blocking status */ |
| 313 | fcntl(daemon->netlinkfd, F_SETFL, flags); |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 314 | } |
| 315 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 316 | static void nl_err(struct nlmsghdr *h) |
| 317 | { |
| 318 | struct nlmsgerr *err = NLMSG_DATA(h); |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 319 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 320 | if (err->error != 0) |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 321 | my_syslog(LOG_ERR, _("netlink returns error: %s"), strerror(-(err->error))); |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | /* We arrange to receive netlink multicast messages whenever the network route is added. |
| 325 | If this happens and we still have a DNS packet in the buffer, we re-send it. |
| 326 | This helps on DoD links, where frequently the packet which triggers dialling is |
| 327 | a DNS query, which then gets lost. By re-sending, we can avoid the lookup |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 328 | failing. Note that we only accept these messages from the kernel (pid == 0) */ |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 329 | static void nl_routechange(struct nlmsghdr *h) |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 330 | { |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 331 | if (h->nlmsg_pid == 0 && h->nlmsg_type == RTM_NEWROUTE) |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 332 | { |
| 333 | struct rtmsg *rtm = NLMSG_DATA(h); |
Simon Kelley | 3927da4 | 2008-07-20 15:10:39 +0100 | [diff] [blame] | 334 | int fd; |
| 335 | |
| 336 | if (rtm->rtm_type != RTN_UNICAST || rtm->rtm_scope != RT_SCOPE_LINK) |
| 337 | return; |
| 338 | |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 339 | /* Force re-reading resolv file right now, for luck. */ |
Simon Kelley | c52e189 | 2010-06-07 22:01:39 +0100 | [diff] [blame] | 340 | daemon->last_resolv = 0; |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 341 | |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 342 | if (daemon->srv_save) |
| 343 | { |
| 344 | if (daemon->srv_save->sfd) |
| 345 | fd = daemon->srv_save->sfd->fd; |
| 346 | else if (daemon->rfd_save && daemon->rfd_save->refcount != 0) |
| 347 | fd = daemon->rfd_save->fd; |
| 348 | else |
| 349 | return; |
| 350 | |
| 351 | while(sendto(fd, daemon->packet, daemon->packet_len, 0, |
| 352 | &daemon->srv_save->addr.sa, sa_len(&daemon->srv_save->addr)) == -1 && retry_send()); |
| 353 | } |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 354 | } |
| 355 | } |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 356 | |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 357 | #endif |
| 358 | |
| 359 | |