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; |
Simon Kelley | e44ddca | 2012-02-18 17:08:50 +0000 | [diff] [blame] | 139 | int callback_ok = 1; |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 140 | |
| 141 | struct { |
| 142 | struct nlmsghdr nlh; |
| 143 | struct rtgenmsg g; |
| 144 | } req; |
| 145 | |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 146 | addr.nl_family = AF_NETLINK; |
| 147 | addr.nl_pad = 0; |
| 148 | addr.nl_groups = 0; |
| 149 | addr.nl_pid = 0; /* address to kernel */ |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 150 | |
| 151 | again: |
| 152 | if (family == AF_UNSPEC) |
| 153 | req.nlh.nlmsg_type = RTM_GETNEIGH; |
| 154 | else if (family == AF_LOCAL) |
| 155 | req.nlh.nlmsg_type = RTM_GETLINK; |
| 156 | else |
| 157 | req.nlh.nlmsg_type = RTM_GETADDR; |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 158 | |
| 159 | req.nlh.nlmsg_len = sizeof(req); |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 160 | 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] | 161 | req.nlh.nlmsg_pid = 0; |
| 162 | req.nlh.nlmsg_seq = ++seq; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 163 | req.g.rtgen_family = family; |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 164 | |
| 165 | /* Don't block in recvfrom if send fails */ |
| 166 | while((len = sendto(daemon->netlinkfd, (void *)&req, sizeof(req), 0, |
| 167 | (struct sockaddr *)&addr, sizeof(addr))) == -1 && retry_send()); |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 168 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 169 | if (len == -1) |
| 170 | return 0; |
| 171 | |
| 172 | while (1) |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 173 | { |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 174 | if ((len = netlink_recv()) == -1) |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 175 | { |
| 176 | if (errno == ENOBUFS) |
| 177 | { |
| 178 | sleep(1); |
| 179 | goto again; |
| 180 | } |
| 181 | return 0; |
| 182 | } |
| 183 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 184 | 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] | 185 | if (h->nlmsg_seq != seq || h->nlmsg_pid != netlink_pid) |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 186 | nl_routechange(h); /* May be multicast arriving async */ |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 187 | else if (h->nlmsg_type == NLMSG_ERROR) |
| 188 | nl_err(h); |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 189 | else if (h->nlmsg_type == NLMSG_DONE) |
Simon Kelley | e44ddca | 2012-02-18 17:08:50 +0000 | [diff] [blame] | 190 | return callback_ok; |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 191 | 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] | 192 | { |
| 193 | struct ifaddrmsg *ifa = NLMSG_DATA(h); |
| 194 | struct rtattr *rta = IFA_RTA(ifa); |
| 195 | unsigned int len1 = h->nlmsg_len - NLMSG_LENGTH(sizeof(*ifa)); |
| 196 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 197 | if (ifa->ifa_family == family) |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 198 | { |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 199 | if (ifa->ifa_family == AF_INET) |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 200 | { |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 201 | struct in_addr netmask, addr, broadcast; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 202 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 203 | netmask.s_addr = htonl(0xffffffff << (32 - ifa->ifa_prefixlen)); |
| 204 | addr.s_addr = 0; |
| 205 | broadcast.s_addr = 0; |
| 206 | |
| 207 | while (RTA_OK(rta, len1)) |
| 208 | { |
| 209 | if (rta->rta_type == IFA_LOCAL) |
| 210 | addr = *((struct in_addr *)(rta+1)); |
| 211 | else if (rta->rta_type == IFA_BROADCAST) |
| 212 | broadcast = *((struct in_addr *)(rta+1)); |
| 213 | |
| 214 | rta = RTA_NEXT(rta, len1); |
| 215 | } |
| 216 | |
Simon Kelley | e44ddca | 2012-02-18 17:08:50 +0000 | [diff] [blame] | 217 | if (addr.s_addr && callback_ok) |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 218 | if (!((*callback)(addr, ifa->ifa_index, netmask, broadcast, parm))) |
Simon Kelley | e44ddca | 2012-02-18 17:08:50 +0000 | [diff] [blame] | 219 | callback_ok = 0; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 220 | } |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 221 | #ifdef HAVE_IPV6 |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 222 | else if (ifa->ifa_family == AF_INET6) |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 223 | { |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 224 | struct in6_addr *addrp = NULL; |
| 225 | while (RTA_OK(rta, len1)) |
| 226 | { |
| 227 | if (rta->rta_type == IFA_ADDRESS) |
| 228 | addrp = ((struct in6_addr *)(rta+1)); |
| 229 | |
| 230 | rta = RTA_NEXT(rta, len1); |
| 231 | } |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 232 | |
Simon Kelley | e44ddca | 2012-02-18 17:08:50 +0000 | [diff] [blame] | 233 | if (addrp && callback_ok) |
Simon Kelley | 52b92f4 | 2012-01-22 16:05:15 +0000 | [diff] [blame] | 234 | if (!((*callback)(addrp, (int)(ifa->ifa_prefixlen), (int)(ifa->ifa_scope), |
| 235 | (int)(ifa->ifa_index), (int)(ifa->ifa_flags & IFA_F_TENTATIVE), parm))) |
Simon Kelley | e44ddca | 2012-02-18 17:08:50 +0000 | [diff] [blame] | 236 | callback_ok = 0; |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 237 | } |
| 238 | #endif |
| 239 | } |
| 240 | } |
| 241 | else if (h->nlmsg_type == RTM_NEWNEIGH && family == AF_UNSPEC) |
| 242 | { |
| 243 | struct ndmsg *neigh = NLMSG_DATA(h); |
| 244 | struct rtattr *rta = NDA_RTA(neigh); |
| 245 | unsigned int len1 = h->nlmsg_len - NLMSG_LENGTH(sizeof(*neigh)); |
| 246 | size_t maclen = 0; |
| 247 | char *inaddr = NULL, *mac = NULL; |
| 248 | |
| 249 | while (RTA_OK(rta, len1)) |
| 250 | { |
| 251 | if (rta->rta_type == NDA_DST) |
| 252 | inaddr = (char *)(rta+1); |
| 253 | else if (rta->rta_type == NDA_LLADDR) |
| 254 | { |
| 255 | maclen = rta->rta_len - sizeof(struct rtattr); |
| 256 | mac = (char *)(rta+1); |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 257 | } |
| 258 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 259 | rta = RTA_NEXT(rta, len1); |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 260 | } |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 261 | |
Simon Kelley | e44ddca | 2012-02-18 17:08:50 +0000 | [diff] [blame] | 262 | if (inaddr && mac && callback_ok) |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 263 | if (!((*callback)(neigh->ndm_family, inaddr, mac, maclen, parm))) |
Simon Kelley | e44ddca | 2012-02-18 17:08:50 +0000 | [diff] [blame] | 264 | callback_ok = 0; |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 265 | } |
| 266 | #ifdef HAVE_DHCP6 |
| 267 | else if (h->nlmsg_type == RTM_NEWLINK && family == AF_LOCAL) |
| 268 | { |
| 269 | struct ifinfomsg *link = NLMSG_DATA(h); |
| 270 | struct rtattr *rta = IFLA_RTA(link); |
| 271 | unsigned int len1 = h->nlmsg_len - NLMSG_LENGTH(sizeof(*link)); |
| 272 | char *mac = NULL; |
| 273 | size_t maclen = 0; |
| 274 | |
| 275 | while (RTA_OK(rta, len1)) |
| 276 | { |
| 277 | if (rta->rta_type == IFLA_ADDRESS) |
| 278 | { |
| 279 | maclen = rta->rta_len - sizeof(struct rtattr); |
| 280 | mac = (char *)(rta+1); |
| 281 | } |
| 282 | |
| 283 | rta = RTA_NEXT(rta, len1); |
| 284 | } |
| 285 | |
Simon Kelley | e44ddca | 2012-02-18 17:08:50 +0000 | [diff] [blame] | 286 | if (mac && callback_ok && !((link->ifi_flags & (IFF_LOOPBACK | IFF_POINTOPOINT))) && |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 287 | !((*callback)((int)link->ifi_index, (unsigned int)link->ifi_type, mac, maclen, parm))) |
Simon Kelley | e44ddca | 2012-02-18 17:08:50 +0000 | [diff] [blame] | 288 | callback_ok = 0; |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 289 | } |
| 290 | #endif |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 291 | } |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 292 | } |
| 293 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 294 | void netlink_multicast(void) |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 295 | { |
| 296 | ssize_t len; |
| 297 | struct nlmsghdr *h; |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 298 | int flags; |
| 299 | |
| 300 | /* don't risk blocking reading netlink messages here. */ |
| 301 | if ((flags = fcntl(daemon->netlinkfd, F_GETFL)) == -1 || |
| 302 | fcntl(daemon->netlinkfd, F_SETFL, flags | O_NONBLOCK) == -1) |
| 303 | return; |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 304 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 305 | if ((len = netlink_recv()) != -1) |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 306 | { |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 307 | for (h = (struct nlmsghdr *)iov.iov_base; NLMSG_OK(h, (size_t)len); h = NLMSG_NEXT(h, len)) |
| 308 | if (h->nlmsg_type == NLMSG_ERROR) |
| 309 | nl_err(h); |
| 310 | else |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 311 | nl_routechange(h); |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 312 | } |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 313 | |
| 314 | /* restore non-blocking status */ |
| 315 | fcntl(daemon->netlinkfd, F_SETFL, flags); |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 316 | } |
| 317 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 318 | static void nl_err(struct nlmsghdr *h) |
| 319 | { |
| 320 | struct nlmsgerr *err = NLMSG_DATA(h); |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 321 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 322 | if (err->error != 0) |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 323 | my_syslog(LOG_ERR, _("netlink returns error: %s"), strerror(-(err->error))); |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | /* We arrange to receive netlink multicast messages whenever the network route is added. |
| 327 | If this happens and we still have a DNS packet in the buffer, we re-send it. |
| 328 | This helps on DoD links, where frequently the packet which triggers dialling is |
| 329 | 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] | 330 | 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] | 331 | static void nl_routechange(struct nlmsghdr *h) |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 332 | { |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 333 | if (h->nlmsg_pid == 0 && h->nlmsg_type == RTM_NEWROUTE) |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 334 | { |
| 335 | struct rtmsg *rtm = NLMSG_DATA(h); |
Simon Kelley | 3927da4 | 2008-07-20 15:10:39 +0100 | [diff] [blame] | 336 | int fd; |
| 337 | |
| 338 | if (rtm->rtm_type != RTN_UNICAST || rtm->rtm_scope != RT_SCOPE_LINK) |
| 339 | return; |
| 340 | |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 341 | /* Force re-reading resolv file right now, for luck. */ |
Simon Kelley | c52e189 | 2010-06-07 22:01:39 +0100 | [diff] [blame] | 342 | daemon->last_resolv = 0; |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 343 | |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 344 | #ifdef HAVE_DHCP6 |
| 345 | /* force RAs to sync new network and pick up new interfaces. */ |
Simon Kelley | 843c96b | 2012-02-27 17:42:38 +0000 | [diff] [blame] | 346 | if (daemon->ra_contexts) |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 347 | { |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 348 | schedule_subnet_map(); |
| 349 | ra_start_unsolicted(dnsmasq_time(), NULL); |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 350 | /* cause lease_update_file to run after we return, in case we were called from |
| 351 | iface_enumerate and can't re-enter it now */ |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 352 | send_alarm(0, 0); |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 353 | } |
| 354 | #endif |
| 355 | |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 356 | if (daemon->srv_save) |
| 357 | { |
| 358 | if (daemon->srv_save->sfd) |
| 359 | fd = daemon->srv_save->sfd->fd; |
| 360 | else if (daemon->rfd_save && daemon->rfd_save->refcount != 0) |
| 361 | fd = daemon->rfd_save->fd; |
| 362 | else |
| 363 | return; |
| 364 | |
| 365 | while(sendto(fd, daemon->packet, daemon->packet_len, 0, |
| 366 | &daemon->srv_save->addr.sa, sa_len(&daemon->srv_save->addr)) == -1 && retry_send()); |
| 367 | } |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 368 | } |
| 369 | } |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 370 | |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 371 | #endif |
| 372 | |
| 373 | |