Simon Kelley | c8e8f5c | 2021-01-24 21:59:37 +0000 | [diff] [blame] | 1 | /* dnsmasq is Copyright (c) 2000-2021 Simon Kelley |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +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 | 9e4abcb | 2004-01-22 19:47:41 +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 | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 15 | */ |
| 16 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 17 | #include "dnsmasq.h" |
| 18 | |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 19 | #ifdef HAVE_LINUX_NETWORK |
| 20 | |
| 21 | int indextoname(int fd, int index, char *name) |
| 22 | { |
| 23 | struct ifreq ifr; |
| 24 | |
| 25 | if (index == 0) |
| 26 | return 0; |
| 27 | |
| 28 | ifr.ifr_ifindex = index; |
| 29 | if (ioctl(fd, SIOCGIFNAME, &ifr) == -1) |
| 30 | return 0; |
| 31 | |
Petr Menšík | 47b45b2 | 2018-08-15 18:17:00 +0200 | [diff] [blame] | 32 | safe_strncpy(name, ifr.ifr_name, IF_NAMESIZE); |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 33 | |
| 34 | return 1; |
| 35 | } |
| 36 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 37 | |
| 38 | #elif defined(HAVE_SOLARIS_NETWORK) |
| 39 | |
| 40 | #include <zone.h> |
| 41 | #include <alloca.h> |
| 42 | #ifndef LIFC_UNDER_IPMP |
| 43 | # define LIFC_UNDER_IPMP 0 |
| 44 | #endif |
| 45 | |
| 46 | int indextoname(int fd, int index, char *name) |
| 47 | { |
| 48 | int64_t lifc_flags; |
| 49 | struct lifnum lifn; |
| 50 | int numifs, bufsize, i; |
| 51 | struct lifconf lifc; |
| 52 | struct lifreq *lifrp; |
| 53 | |
| 54 | if (index == 0) |
| 55 | return 0; |
| 56 | |
| 57 | if (getzoneid() == GLOBAL_ZONEID) |
| 58 | { |
| 59 | if (!if_indextoname(index, name)) |
| 60 | return 0; |
| 61 | return 1; |
| 62 | } |
| 63 | |
| 64 | lifc_flags = LIFC_NOXMIT | LIFC_TEMPORARY | LIFC_ALLZONES | LIFC_UNDER_IPMP; |
| 65 | lifn.lifn_family = AF_UNSPEC; |
| 66 | lifn.lifn_flags = lifc_flags; |
| 67 | if (ioctl(fd, SIOCGLIFNUM, &lifn) < 0) |
| 68 | return 0; |
| 69 | |
| 70 | numifs = lifn.lifn_count; |
| 71 | bufsize = numifs * sizeof(struct lifreq); |
| 72 | |
| 73 | lifc.lifc_family = AF_UNSPEC; |
| 74 | lifc.lifc_flags = lifc_flags; |
| 75 | lifc.lifc_len = bufsize; |
| 76 | lifc.lifc_buf = alloca(bufsize); |
| 77 | |
| 78 | if (ioctl(fd, SIOCGLIFCONF, &lifc) < 0) |
| 79 | return 0; |
| 80 | |
| 81 | lifrp = lifc.lifc_req; |
| 82 | for (i = lifc.lifc_len / sizeof(struct lifreq); i; i--, lifrp++) |
| 83 | { |
| 84 | struct lifreq lifr; |
Petr Menšík | 47b45b2 | 2018-08-15 18:17:00 +0200 | [diff] [blame] | 85 | safe_strncpy(lifr.lifr_name, lifrp->lifr_name, IF_NAMESIZE); |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 86 | if (ioctl(fd, SIOCGLIFINDEX, &lifr) < 0) |
| 87 | return 0; |
| 88 | |
| 89 | if (lifr.lifr_index == index) { |
Petr Menšík | 47b45b2 | 2018-08-15 18:17:00 +0200 | [diff] [blame] | 90 | safe_strncpy(name, lifr.lifr_name, IF_NAMESIZE); |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 91 | return 1; |
| 92 | } |
| 93 | } |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 98 | #else |
| 99 | |
| 100 | int indextoname(int fd, int index, char *name) |
| 101 | { |
Simon Kelley | 115ac3e | 2013-05-20 11:28:32 +0100 | [diff] [blame] | 102 | (void)fd; |
| 103 | |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 104 | if (index == 0 || !if_indextoname(index, name)) |
| 105 | return 0; |
| 106 | |
| 107 | return 1; |
| 108 | } |
| 109 | |
| 110 | #endif |
| 111 | |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 112 | int iface_check(int family, union all_addr *addr, char *name, int *auth) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 113 | { |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 114 | struct iname *tmp; |
Simon Kelley | edf0bde | 2013-07-29 17:21:48 +0100 | [diff] [blame] | 115 | int ret = 1, match_addr = 0; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 116 | |
Simon Kelley | 309331f | 2006-04-22 15:05:01 +0100 | [diff] [blame] | 117 | /* Note: have to check all and not bail out early, so that we set the |
Simon Kelley | 89500e3 | 2013-09-20 16:29:20 +0100 | [diff] [blame] | 118 | "used" flags. |
| 119 | |
| 120 | May be called with family == AF_LOCALto check interface by name only. */ |
Simon Kelley | 429798f | 2012-12-10 20:45:53 +0000 | [diff] [blame] | 121 | |
Simon Kelley | 4f7b304 | 2012-11-28 21:27:02 +0000 | [diff] [blame] | 122 | if (auth) |
Simon Kelley | 429798f | 2012-12-10 20:45:53 +0000 | [diff] [blame] | 123 | *auth = 0; |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 124 | |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 125 | if (daemon->if_names || daemon->if_addrs) |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 126 | { |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 127 | ret = 0; |
| 128 | |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 129 | for (tmp = daemon->if_names; tmp; tmp = tmp->next) |
Simon Kelley | 49333cb | 2013-03-15 20:30:51 +0000 | [diff] [blame] | 130 | if (tmp->name && wildcard_match(tmp->name, name)) |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 131 | ret = tmp->used = 1; |
| 132 | |
Simon Kelley | 429798f | 2012-12-10 20:45:53 +0000 | [diff] [blame] | 133 | if (addr) |
| 134 | for (tmp = daemon->if_addrs; tmp; tmp = tmp->next) |
| 135 | if (tmp->addr.sa.sa_family == family) |
| 136 | { |
| 137 | if (family == AF_INET && |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 138 | tmp->addr.in.sin_addr.s_addr == addr->addr4.s_addr) |
Simon Kelley | edf0bde | 2013-07-29 17:21:48 +0100 | [diff] [blame] | 139 | ret = match_addr = tmp->used = 1; |
Simon Kelley | 429798f | 2012-12-10 20:45:53 +0000 | [diff] [blame] | 140 | else if (family == AF_INET6 && |
| 141 | IN6_ARE_ADDR_EQUAL(&tmp->addr.in6.sin6_addr, |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 142 | &addr->addr6)) |
Simon Kelley | edf0bde | 2013-07-29 17:21:48 +0100 | [diff] [blame] | 143 | ret = match_addr = tmp->used = 1; |
Simon Kelley | 429798f | 2012-12-10 20:45:53 +0000 | [diff] [blame] | 144 | } |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 145 | } |
| 146 | |
Simon Kelley | edf0bde | 2013-07-29 17:21:48 +0100 | [diff] [blame] | 147 | if (!match_addr) |
| 148 | for (tmp = daemon->if_except; tmp; tmp = tmp->next) |
| 149 | if (tmp->name && wildcard_match(tmp->name, name)) |
| 150 | ret = 0; |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 151 | |
Simon Kelley | 429798f | 2012-12-10 20:45:53 +0000 | [diff] [blame] | 152 | |
| 153 | for (tmp = daemon->authinterface; tmp; tmp = tmp->next) |
| 154 | if (tmp->name) |
| 155 | { |
Simon Kelley | f25e6c6 | 2013-11-17 12:23:42 +0000 | [diff] [blame] | 156 | if (strcmp(tmp->name, name) == 0 && |
| 157 | (tmp->addr.sa.sa_family == 0 || tmp->addr.sa.sa_family == family)) |
Simon Kelley | 429798f | 2012-12-10 20:45:53 +0000 | [diff] [blame] | 158 | break; |
| 159 | } |
| 160 | else if (addr && tmp->addr.sa.sa_family == AF_INET && family == AF_INET && |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 161 | tmp->addr.in.sin_addr.s_addr == addr->addr4.s_addr) |
Simon Kelley | 429798f | 2012-12-10 20:45:53 +0000 | [diff] [blame] | 162 | break; |
Simon Kelley | 429798f | 2012-12-10 20:45:53 +0000 | [diff] [blame] | 163 | else if (addr && tmp->addr.sa.sa_family == AF_INET6 && family == AF_INET6 && |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 164 | IN6_ARE_ADDR_EQUAL(&tmp->addr.in6.sin6_addr, &addr->addr6)) |
Simon Kelley | 429798f | 2012-12-10 20:45:53 +0000 | [diff] [blame] | 165 | break; |
Simon Kelley | 429798f | 2012-12-10 20:45:53 +0000 | [diff] [blame] | 166 | |
| 167 | if (tmp && auth) |
| 168 | { |
| 169 | *auth = 1; |
| 170 | ret = 1; |
| 171 | } |
| 172 | |
Simon Kelley | 309331f | 2006-04-22 15:05:01 +0100 | [diff] [blame] | 173 | return ret; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 174 | } |
Simon Kelley | e25db1f | 2013-01-29 22:10:26 +0000 | [diff] [blame] | 175 | |
| 176 | |
Josh Soref | 730c674 | 2017-02-06 16:14:04 +0000 | [diff] [blame] | 177 | /* Fix for problem that the kernel sometimes reports the loopback interface as the |
Simon Kelley | e25db1f | 2013-01-29 22:10:26 +0000 | [diff] [blame] | 178 | arrival interface when a packet originates locally, even when sent to address of |
| 179 | an interface other than the loopback. Accept packet if it arrived via a loopback |
| 180 | interface, even when we're not accepting packets that way, as long as the destination |
| 181 | address is one we're believing. Interface list must be up-to-date before calling. */ |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 182 | int loopback_exception(int fd, int family, union all_addr *addr, char *name) |
Simon Kelley | e25db1f | 2013-01-29 22:10:26 +0000 | [diff] [blame] | 183 | { |
| 184 | struct ifreq ifr; |
| 185 | struct irec *iface; |
| 186 | |
Petr Menšík | 47b45b2 | 2018-08-15 18:17:00 +0200 | [diff] [blame] | 187 | safe_strncpy(ifr.ifr_name, name, IF_NAMESIZE); |
Simon Kelley | e25db1f | 2013-01-29 22:10:26 +0000 | [diff] [blame] | 188 | if (ioctl(fd, SIOCGIFFLAGS, &ifr) != -1 && |
| 189 | ifr.ifr_flags & IFF_LOOPBACK) |
| 190 | { |
| 191 | for (iface = daemon->interfaces; iface; iface = iface->next) |
| 192 | if (iface->addr.sa.sa_family == family) |
| 193 | { |
| 194 | if (family == AF_INET) |
| 195 | { |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 196 | if (iface->addr.in.sin_addr.s_addr == addr->addr4.s_addr) |
Simon Kelley | e25db1f | 2013-01-29 22:10:26 +0000 | [diff] [blame] | 197 | return 1; |
| 198 | } |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 199 | else if (IN6_ARE_ADDR_EQUAL(&iface->addr.in6.sin6_addr, &addr->addr6)) |
Simon Kelley | e25db1f | 2013-01-29 22:10:26 +0000 | [diff] [blame] | 200 | return 1; |
Simon Kelley | e25db1f | 2013-01-29 22:10:26 +0000 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | return 0; |
| 204 | } |
| 205 | |
Simon Kelley | 3f2873d | 2013-05-14 11:28:47 +0100 | [diff] [blame] | 206 | /* If we're configured with something like --interface=eth0:0 then we'll listen correctly |
| 207 | on the relevant address, but the name of the arrival interface, derived from the |
| 208 | index won't match the config. Check that we found an interface address for the arrival |
| 209 | interface: daemon->interfaces must be up-to-date. */ |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 210 | int label_exception(int index, int family, union all_addr *addr) |
Simon Kelley | 3f2873d | 2013-05-14 11:28:47 +0100 | [diff] [blame] | 211 | { |
| 212 | struct irec *iface; |
| 213 | |
| 214 | /* labels only supported on IPv4 addresses. */ |
| 215 | if (family != AF_INET) |
| 216 | return 0; |
| 217 | |
| 218 | for (iface = daemon->interfaces; iface; iface = iface->next) |
| 219 | if (iface->index == index && iface->addr.sa.sa_family == AF_INET && |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 220 | iface->addr.in.sin_addr.s_addr == addr->addr4.s_addr) |
Simon Kelley | 3f2873d | 2013-05-14 11:28:47 +0100 | [diff] [blame] | 221 | return 1; |
| 222 | |
| 223 | return 0; |
| 224 | } |
| 225 | |
Simon Kelley | 115ac3e | 2013-05-20 11:28:32 +0100 | [diff] [blame] | 226 | struct iface_param { |
| 227 | struct addrlist *spare; |
| 228 | int fd; |
| 229 | }; |
| 230 | |
| 231 | static int iface_allowed(struct iface_param *param, int if_index, char *label, |
Simon Kelley | 4766936 | 2014-12-17 12:41:56 +0000 | [diff] [blame] | 232 | union mysockaddr *addr, struct in_addr netmask, int prefixlen, int iface_flags) |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 233 | { |
| 234 | struct irec *iface; |
Petr Menšík | a8c1474 | 2021-03-02 21:38:02 +0000 | [diff] [blame] | 235 | int loopback; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 236 | struct ifreq ifr; |
Simon Kelley | 8bc4cec | 2012-07-03 21:04:11 +0100 | [diff] [blame] | 237 | int tftp_ok = !!option_bool(OPT_TFTP); |
Simon Kelley | 9380ba7 | 2012-04-16 14:41:56 +0100 | [diff] [blame] | 238 | int dhcp_ok = 1; |
Simon Kelley | 4f7b304 | 2012-11-28 21:27:02 +0000 | [diff] [blame] | 239 | int auth_dns = 0; |
Petr Menšík | ad59f27 | 2017-03-17 17:22:19 +0000 | [diff] [blame] | 240 | int is_label = 0; |
Simon Kelley | 91543f4 | 2013-09-23 12:41:20 +0100 | [diff] [blame] | 241 | #if defined(HAVE_DHCP) || defined(HAVE_TFTP) |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 242 | struct iname *tmp; |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 243 | #endif |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 244 | |
Vladislav Grishenko | 3b19596 | 2013-11-26 11:08:21 +0000 | [diff] [blame] | 245 | (void)prefixlen; |
| 246 | |
Simon Kelley | 115ac3e | 2013-05-20 11:28:32 +0100 | [diff] [blame] | 247 | if (!indextoname(param->fd, if_index, ifr.ifr_name) || |
| 248 | ioctl(param->fd, SIOCGIFFLAGS, &ifr) == -1) |
| 249 | return 0; |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 250 | |
| 251 | loopback = ifr.ifr_flags & IFF_LOOPBACK; |
Simon Kelley | 9380ba7 | 2012-04-16 14:41:56 +0100 | [diff] [blame] | 252 | |
| 253 | if (loopback) |
Simon Kelley | 3f2873d | 2013-05-14 11:28:47 +0100 | [diff] [blame] | 254 | dhcp_ok = 0; |
| 255 | |
Simon Kelley | 115ac3e | 2013-05-20 11:28:32 +0100 | [diff] [blame] | 256 | if (!label) |
| 257 | label = ifr.ifr_name; |
Petr Menšík | ad59f27 | 2017-03-17 17:22:19 +0000 | [diff] [blame] | 258 | else |
| 259 | is_label = strcmp(label, ifr.ifr_name); |
Simon Kelley | c8a8048 | 2014-03-05 14:29:54 +0000 | [diff] [blame] | 260 | |
| 261 | /* maintain a list of all addresses on all interfaces for --local-service option */ |
| 262 | if (option_bool(OPT_LOCAL_SERVICE)) |
| 263 | { |
| 264 | struct addrlist *al; |
Simon Kelley | 115ac3e | 2013-05-20 11:28:32 +0100 | [diff] [blame] | 265 | |
Simon Kelley | c8a8048 | 2014-03-05 14:29:54 +0000 | [diff] [blame] | 266 | if (param->spare) |
| 267 | { |
| 268 | al = param->spare; |
| 269 | param->spare = al->next; |
| 270 | } |
| 271 | else |
| 272 | al = whine_malloc(sizeof(struct addrlist)); |
| 273 | |
| 274 | if (al) |
| 275 | { |
| 276 | al->next = daemon->interface_addrs; |
| 277 | daemon->interface_addrs = al; |
| 278 | al->prefixlen = prefixlen; |
| 279 | |
| 280 | if (addr->sa.sa_family == AF_INET) |
| 281 | { |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 282 | al->addr.addr4 = addr->in.sin_addr; |
Simon Kelley | c8a8048 | 2014-03-05 14:29:54 +0000 | [diff] [blame] | 283 | al->flags = 0; |
| 284 | } |
Simon Kelley | c8a8048 | 2014-03-05 14:29:54 +0000 | [diff] [blame] | 285 | else |
| 286 | { |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 287 | al->addr.addr6 = addr->in6.sin6_addr; |
Simon Kelley | c8a8048 | 2014-03-05 14:29:54 +0000 | [diff] [blame] | 288 | al->flags = ADDRLIST_IPV6; |
| 289 | } |
Simon Kelley | c8a8048 | 2014-03-05 14:29:54 +0000 | [diff] [blame] | 290 | } |
| 291 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 292 | |
Simon Kelley | 115ac3e | 2013-05-20 11:28:32 +0100 | [diff] [blame] | 293 | if (addr->sa.sa_family != AF_INET6 || !IN6_IS_ADDR_LINKLOCAL(&addr->in6.sin6_addr)) |
Simon Kelley | 115ac3e | 2013-05-20 11:28:32 +0100 | [diff] [blame] | 294 | { |
| 295 | struct interface_name *int_name; |
| 296 | struct addrlist *al; |
Simon Kelley | 376d48c | 2013-11-13 13:04:30 +0000 | [diff] [blame] | 297 | #ifdef HAVE_AUTH |
| 298 | struct auth_zone *zone; |
| 299 | struct auth_name_list *name; |
Simon Kelley | 115ac3e | 2013-05-20 11:28:32 +0100 | [diff] [blame] | 300 | |
Simon Kelley | 376d48c | 2013-11-13 13:04:30 +0000 | [diff] [blame] | 301 | /* Find subnets in auth_zones */ |
| 302 | for (zone = daemon->auth_zones; zone; zone = zone->next) |
| 303 | for (name = zone->interface_names; name; name = name->next) |
| 304 | if (wildcard_match(name->name, label)) |
| 305 | { |
| 306 | if (addr->sa.sa_family == AF_INET && (name->flags & AUTH4)) |
| 307 | { |
| 308 | if (param->spare) |
| 309 | { |
| 310 | al = param->spare; |
| 311 | param->spare = al->next; |
| 312 | } |
| 313 | else |
| 314 | al = whine_malloc(sizeof(struct addrlist)); |
| 315 | |
| 316 | if (al) |
| 317 | { |
| 318 | al->next = zone->subnet; |
| 319 | zone->subnet = al; |
| 320 | al->prefixlen = prefixlen; |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 321 | al->addr.addr4 = addr->in.sin_addr; |
Simon Kelley | 376d48c | 2013-11-13 13:04:30 +0000 | [diff] [blame] | 322 | al->flags = 0; |
| 323 | } |
| 324 | } |
| 325 | |
Simon Kelley | 376d48c | 2013-11-13 13:04:30 +0000 | [diff] [blame] | 326 | if (addr->sa.sa_family == AF_INET6 && (name->flags & AUTH6)) |
| 327 | { |
| 328 | if (param->spare) |
| 329 | { |
| 330 | al = param->spare; |
| 331 | param->spare = al->next; |
| 332 | } |
| 333 | else |
| 334 | al = whine_malloc(sizeof(struct addrlist)); |
| 335 | |
| 336 | if (al) |
| 337 | { |
| 338 | al->next = zone->subnet; |
| 339 | zone->subnet = al; |
Vladislav Grishenko | 3b19596 | 2013-11-26 11:08:21 +0000 | [diff] [blame] | 340 | al->prefixlen = prefixlen; |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 341 | al->addr.addr6 = addr->in6.sin6_addr; |
Simon Kelley | 376d48c | 2013-11-13 13:04:30 +0000 | [diff] [blame] | 342 | al->flags = ADDRLIST_IPV6; |
| 343 | } |
| 344 | } |
Simon Kelley | 376d48c | 2013-11-13 13:04:30 +0000 | [diff] [blame] | 345 | } |
| 346 | #endif |
| 347 | |
| 348 | /* Update addresses from interface_names. These are a set independent |
| 349 | of the set we're listening on. */ |
Simon Kelley | 115ac3e | 2013-05-20 11:28:32 +0100 | [diff] [blame] | 350 | for (int_name = daemon->int_names; int_name; int_name = int_name->next) |
Simon Kelley | b7cf754 | 2021-03-04 16:54:14 +0000 | [diff] [blame] | 351 | if (strncmp(label, int_name->intr, IF_NAMESIZE) == 0) |
Simon Kelley | 115ac3e | 2013-05-20 11:28:32 +0100 | [diff] [blame] | 352 | { |
Simon Kelley | b7cf754 | 2021-03-04 16:54:14 +0000 | [diff] [blame] | 353 | struct addrlist *lp; |
| 354 | |
| 355 | al = NULL; |
| 356 | |
| 357 | if (addr->sa.sa_family == AF_INET && (int_name->flags & (IN4 | INP4))) |
Simon Kelley | 115ac3e | 2013-05-20 11:28:32 +0100 | [diff] [blame] | 358 | { |
Simon Kelley | b7cf754 | 2021-03-04 16:54:14 +0000 | [diff] [blame] | 359 | struct in_addr newaddr = addr->in.sin_addr; |
| 360 | |
| 361 | if (int_name->flags & INP4) |
| 362 | { |
| 363 | if (netmask.s_addr == 0xffff) |
| 364 | continue; |
| 365 | |
| 366 | newaddr.s_addr = (addr->in.sin_addr.s_addr & netmask.s_addr) | |
| 367 | (int_name->proto4.s_addr & ~netmask.s_addr); |
| 368 | } |
| 369 | |
| 370 | /* check for duplicates. */ |
| 371 | for (lp = int_name->addr; lp; lp = lp->next) |
| 372 | if (lp->flags == 0 && lp->addr.addr4.s_addr == newaddr.s_addr) |
| 373 | break; |
| 374 | |
| 375 | if (!lp) |
| 376 | { |
| 377 | if (param->spare) |
| 378 | { |
| 379 | al = param->spare; |
| 380 | param->spare = al->next; |
| 381 | } |
| 382 | else |
| 383 | al = whine_malloc(sizeof(struct addrlist)); |
| 384 | |
| 385 | if (al) |
| 386 | { |
| 387 | al->flags = 0; |
| 388 | al->addr.addr4 = newaddr; |
| 389 | } |
| 390 | } |
Simon Kelley | 115ac3e | 2013-05-20 11:28:32 +0100 | [diff] [blame] | 391 | } |
Simon Kelley | b7cf754 | 2021-03-04 16:54:14 +0000 | [diff] [blame] | 392 | |
| 393 | if (addr->sa.sa_family == AF_INET6 && (int_name->flags & (IN6 | INP6))) |
| 394 | { |
| 395 | struct in6_addr newaddr = addr->in6.sin6_addr; |
| 396 | |
| 397 | if (int_name->flags & INP6) |
| 398 | { |
| 399 | int i; |
| 400 | |
| 401 | /* No sense in doing /128. */ |
| 402 | if (prefixlen == 128) |
| 403 | continue; |
| 404 | |
| 405 | for (i = 0; i < 16; i++) |
| 406 | { |
| 407 | int bits = ((i+1)*8) - prefixlen; |
| 408 | |
| 409 | if (bits >= 8) |
| 410 | newaddr.s6_addr[i] = int_name->proto6.s6_addr[i]; |
| 411 | else if (bits >= 0) |
| 412 | { |
| 413 | unsigned char mask = 0xff << bits; |
| 414 | newaddr.s6_addr[i] = |
| 415 | (addr->in6.sin6_addr.s6_addr[i] & mask) | |
| 416 | (int_name->proto6.s6_addr[i] & ~mask); |
| 417 | } |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | /* check for duplicates. */ |
| 422 | for (lp = int_name->addr; lp; lp = lp->next) |
| 423 | if ((lp->flags & ADDRLIST_IPV6) && |
| 424 | IN6_ARE_ADDR_EQUAL(&lp->addr.addr6, &newaddr)) |
| 425 | break; |
| 426 | |
| 427 | if (!lp) |
| 428 | { |
| 429 | if (param->spare) |
| 430 | { |
| 431 | al = param->spare; |
| 432 | param->spare = al->next; |
| 433 | } |
| 434 | else |
| 435 | al = whine_malloc(sizeof(struct addrlist)); |
| 436 | |
| 437 | if (al) |
| 438 | { |
| 439 | al->flags = ADDRLIST_IPV6; |
| 440 | al->addr.addr6 = newaddr; |
| 441 | |
| 442 | /* Privacy addresses and addresses still undergoing DAD and deprecated addresses |
| 443 | don't appear in forward queries, but will in reverse ones. */ |
| 444 | if (!(iface_flags & IFACE_PERMANENT) || (iface_flags & (IFACE_DEPRECATED | IFACE_TENTATIVE))) |
| 445 | al->flags |= ADDRLIST_REVONLY; |
| 446 | } |
| 447 | } |
| 448 | } |
Simon Kelley | 115ac3e | 2013-05-20 11:28:32 +0100 | [diff] [blame] | 449 | |
| 450 | if (al) |
| 451 | { |
Simon Kelley | 376d48c | 2013-11-13 13:04:30 +0000 | [diff] [blame] | 452 | al->next = int_name->addr; |
| 453 | int_name->addr = al; |
Simon Kelley | 115ac3e | 2013-05-20 11:28:32 +0100 | [diff] [blame] | 454 | } |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | /* check whether the interface IP has been added already |
| 459 | we call this routine multiple times. */ |
| 460 | for (iface = daemon->interfaces; iface; iface = iface->next) |
Petr Mensik | 951a221 | 2019-07-03 17:02:16 +0200 | [diff] [blame] | 461 | if (sockaddr_isequal(&iface->addr, addr) && iface->index == if_index) |
Simon Kelley | 115ac3e | 2013-05-20 11:28:32 +0100 | [diff] [blame] | 462 | { |
Simon Kelley | 4766936 | 2014-12-17 12:41:56 +0000 | [diff] [blame] | 463 | iface->dad = !!(iface_flags & IFACE_TENTATIVE); |
Simon Kelley | 0861921 | 2013-12-02 14:43:48 +0000 | [diff] [blame] | 464 | iface->found = 1; /* for garbage collection */ |
Petr Mensik | 951a221 | 2019-07-03 17:02:16 +0200 | [diff] [blame] | 465 | iface->netmask = netmask; |
Simon Kelley | 115ac3e | 2013-05-20 11:28:32 +0100 | [diff] [blame] | 466 | return 1; |
| 467 | } |
| 468 | |
| 469 | /* If we are restricting the set of interfaces to use, make |
Simon Kelley | 59353a6 | 2004-11-21 19:34:28 +0000 | [diff] [blame] | 470 | sure that loopback interfaces are in that set. */ |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 471 | if (daemon->if_names && loopback) |
Simon Kelley | 59353a6 | 2004-11-21 19:34:28 +0000 | [diff] [blame] | 472 | { |
| 473 | struct iname *lo; |
| 474 | for (lo = daemon->if_names; lo; lo = lo->next) |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 475 | if (lo->name && strcmp(lo->name, ifr.ifr_name) == 0) |
Simon Kelley | 4ce4f37 | 2012-06-14 11:50:45 +0100 | [diff] [blame] | 476 | break; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 477 | |
Simon Kelley | 38365ff | 2013-02-05 14:35:54 +0000 | [diff] [blame] | 478 | if (!lo && (lo = whine_malloc(sizeof(struct iname)))) |
Simon Kelley | 59353a6 | 2004-11-21 19:34:28 +0000 | [diff] [blame] | 479 | { |
Simon Kelley | 38365ff | 2013-02-05 14:35:54 +0000 | [diff] [blame] | 480 | if ((lo->name = whine_malloc(strlen(ifr.ifr_name)+1))) |
| 481 | { |
| 482 | strcpy(lo->name, ifr.ifr_name); |
| 483 | lo->used = 1; |
| 484 | lo->next = daemon->if_names; |
| 485 | daemon->if_names = lo; |
| 486 | } |
| 487 | else |
| 488 | free(lo); |
Simon Kelley | 59353a6 | 2004-11-21 19:34:28 +0000 | [diff] [blame] | 489 | } |
| 490 | } |
| 491 | |
Simon Kelley | 8bc4cec | 2012-07-03 21:04:11 +0100 | [diff] [blame] | 492 | if (addr->sa.sa_family == AF_INET && |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 493 | !iface_check(AF_INET, (union all_addr *)&addr->in.sin_addr, label, &auth_dns)) |
Simon Kelley | 8bc4cec | 2012-07-03 21:04:11 +0100 | [diff] [blame] | 494 | return 1; |
Simon Kelley | 4f7b304 | 2012-11-28 21:27:02 +0000 | [diff] [blame] | 495 | |
Simon Kelley | 8bc4cec | 2012-07-03 21:04:11 +0100 | [diff] [blame] | 496 | if (addr->sa.sa_family == AF_INET6 && |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 497 | !iface_check(AF_INET6, (union all_addr *)&addr->in6.sin6_addr, label, &auth_dns)) |
Simon Kelley | 8bc4cec | 2012-07-03 21:04:11 +0100 | [diff] [blame] | 498 | return 1; |
Simon Kelley | 4f7b304 | 2012-11-28 21:27:02 +0000 | [diff] [blame] | 499 | |
| 500 | #ifdef HAVE_DHCP |
| 501 | /* No DHCP where we're doing auth DNS. */ |
| 502 | if (auth_dns) |
| 503 | { |
| 504 | tftp_ok = 0; |
| 505 | dhcp_ok = 0; |
| 506 | } |
| 507 | else |
| 508 | for (tmp = daemon->dhcp_except; tmp; tmp = tmp->next) |
Simon Kelley | 49333cb | 2013-03-15 20:30:51 +0000 | [diff] [blame] | 509 | if (tmp->name && wildcard_match(tmp->name, ifr.ifr_name)) |
Simon Kelley | 4f7b304 | 2012-11-28 21:27:02 +0000 | [diff] [blame] | 510 | { |
| 511 | tftp_ok = 0; |
| 512 | dhcp_ok = 0; |
| 513 | } |
| 514 | #endif |
| 515 | |
Simon Kelley | 2937f8a | 2013-07-29 19:49:07 +0100 | [diff] [blame] | 516 | |
Simon Kelley | 91543f4 | 2013-09-23 12:41:20 +0100 | [diff] [blame] | 517 | #ifdef HAVE_TFTP |
Simon Kelley | 2937f8a | 2013-07-29 19:49:07 +0100 | [diff] [blame] | 518 | if (daemon->tftp_interfaces) |
| 519 | { |
| 520 | /* dedicated tftp interface list */ |
| 521 | tftp_ok = 0; |
| 522 | for (tmp = daemon->tftp_interfaces; tmp; tmp = tmp->next) |
| 523 | if (tmp->name && wildcard_match(tmp->name, ifr.ifr_name)) |
| 524 | tftp_ok = 1; |
| 525 | } |
Simon Kelley | 91543f4 | 2013-09-23 12:41:20 +0100 | [diff] [blame] | 526 | #endif |
Simon Kelley | 2937f8a | 2013-07-29 19:49:07 +0100 | [diff] [blame] | 527 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 528 | /* add to list */ |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 529 | if ((iface = whine_malloc(sizeof(struct irec)))) |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 530 | { |
Petr Menšík | a8c1474 | 2021-03-02 21:38:02 +0000 | [diff] [blame] | 531 | int mtu = 0; |
| 532 | |
| 533 | if (ioctl(param->fd, SIOCGIFMTU, &ifr) != -1) |
| 534 | mtu = ifr.ifr_mtu; |
| 535 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 536 | iface->addr = *addr; |
| 537 | iface->netmask = netmask; |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 538 | iface->tftp_ok = tftp_ok; |
Simon Kelley | 9380ba7 | 2012-04-16 14:41:56 +0100 | [diff] [blame] | 539 | iface->dhcp_ok = dhcp_ok; |
Simon Kelley | 4f7b304 | 2012-11-28 21:27:02 +0000 | [diff] [blame] | 540 | iface->dns_auth = auth_dns; |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 541 | iface->mtu = mtu; |
Simon Kelley | 4766936 | 2014-12-17 12:41:56 +0000 | [diff] [blame] | 542 | iface->dad = !!(iface_flags & IFACE_TENTATIVE); |
Simon Kelley | 0861921 | 2013-12-02 14:43:48 +0000 | [diff] [blame] | 543 | iface->found = 1; |
Simon Kelley | dc27e14 | 2013-10-16 13:09:53 +0100 | [diff] [blame] | 544 | iface->done = iface->multicast_done = iface->warned = 0; |
Simon Kelley | 5d162f2 | 2012-12-20 14:55:46 +0000 | [diff] [blame] | 545 | iface->index = if_index; |
Petr Menšík | ad59f27 | 2017-03-17 17:22:19 +0000 | [diff] [blame] | 546 | iface->label = is_label; |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 547 | if ((iface->name = whine_malloc(strlen(ifr.ifr_name)+1))) |
Simon Kelley | 6f13e53 | 2012-04-17 14:25:06 +0100 | [diff] [blame] | 548 | { |
| 549 | strcpy(iface->name, ifr.ifr_name); |
Simon Kelley | 115ac3e | 2013-05-20 11:28:32 +0100 | [diff] [blame] | 550 | iface->next = daemon->interfaces; |
| 551 | daemon->interfaces = iface; |
Simon Kelley | 6f13e53 | 2012-04-17 14:25:06 +0100 | [diff] [blame] | 552 | return 1; |
| 553 | } |
| 554 | free(iface); |
Simon Kelley | 5d162f2 | 2012-12-20 14:55:46 +0000 | [diff] [blame] | 555 | |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 556 | } |
| 557 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 558 | errno = ENOMEM; |
| 559 | return 0; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 560 | } |
| 561 | |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 562 | static int iface_allowed_v6(struct in6_addr *local, int prefix, |
Simon Kelley | bad7b87 | 2012-12-20 22:00:39 +0000 | [diff] [blame] | 563 | int scope, int if_index, int flags, |
Simon Kelley | 1f77693 | 2012-12-16 19:46:08 +0000 | [diff] [blame] | 564 | int preferred, int valid, void *vparam) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 565 | { |
Simon Kelley | 59353a6 | 2004-11-21 19:34:28 +0000 | [diff] [blame] | 566 | union mysockaddr addr; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 567 | struct in_addr netmask; /* dummy */ |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 568 | netmask.s_addr = 0; |
Simon Kelley | 52b92f4 | 2012-01-22 16:05:15 +0000 | [diff] [blame] | 569 | |
Simon Kelley | 52b92f4 | 2012-01-22 16:05:15 +0000 | [diff] [blame] | 570 | (void)scope; /* warning */ |
Simon Kelley | 1f77693 | 2012-12-16 19:46:08 +0000 | [diff] [blame] | 571 | (void)preferred; |
| 572 | (void)valid; |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 573 | |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 574 | memset(&addr, 0, sizeof(addr)); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 575 | #ifdef HAVE_SOCKADDR_SA_LEN |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 576 | addr.in6.sin6_len = sizeof(addr.in6); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 577 | #endif |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 578 | addr.in6.sin6_family = AF_INET6; |
| 579 | addr.in6.sin6_addr = *local; |
| 580 | addr.in6.sin6_port = htons(daemon->port); |
Simon Kelley | 62ab3cc | 2013-12-03 11:53:53 +0000 | [diff] [blame] | 581 | /* FreeBSD insists this is zero for non-linklocal addresses */ |
| 582 | if (IN6_IS_ADDR_LINKLOCAL(local)) |
| 583 | addr.in6.sin6_scope_id = if_index; |
| 584 | else |
| 585 | addr.in6.sin6_scope_id = 0; |
Simon Kelley | 1ee9be4 | 2013-12-09 16:50:19 +0000 | [diff] [blame] | 586 | |
Simon Kelley | 4766936 | 2014-12-17 12:41:56 +0000 | [diff] [blame] | 587 | return iface_allowed((struct iface_param *)vparam, if_index, NULL, &addr, netmask, prefix, flags); |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 588 | } |
Simon Kelley | 59353a6 | 2004-11-21 19:34:28 +0000 | [diff] [blame] | 589 | |
Simon Kelley | 3f2873d | 2013-05-14 11:28:47 +0100 | [diff] [blame] | 590 | static int iface_allowed_v4(struct in_addr local, int if_index, char *label, |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 591 | struct in_addr netmask, struct in_addr broadcast, void *vparam) |
| 592 | { |
| 593 | union mysockaddr addr; |
Simon Kelley | 376d48c | 2013-11-13 13:04:30 +0000 | [diff] [blame] | 594 | int prefix, bit; |
Simon Kelley | b8ac466 | 2016-03-10 18:40:53 +0000 | [diff] [blame] | 595 | |
| 596 | (void)broadcast; /* warning */ |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 597 | |
| 598 | memset(&addr, 0, sizeof(addr)); |
Simon Kelley | 1ab84e2 | 2004-01-29 16:48:35 +0000 | [diff] [blame] | 599 | #ifdef HAVE_SOCKADDR_SA_LEN |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 600 | addr.in.sin_len = sizeof(addr.in); |
Simon Kelley | 1ab84e2 | 2004-01-29 16:48:35 +0000 | [diff] [blame] | 601 | #endif |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 602 | addr.in.sin_family = AF_INET; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 603 | addr.in.sin_addr = local; |
| 604 | addr.in.sin_port = htons(daemon->port); |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 605 | |
Simon Kelley | 376d48c | 2013-11-13 13:04:30 +0000 | [diff] [blame] | 606 | /* determine prefix length from netmask */ |
| 607 | for (prefix = 32, bit = 1; (bit & ntohl(netmask.s_addr)) == 0 && prefix != 0; bit = bit << 1, prefix--); |
| 608 | |
| 609 | return iface_allowed((struct iface_param *)vparam, if_index, label, &addr, netmask, prefix, 0); |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 610 | } |
Petr Mensik | 60a3ae1 | 2019-07-09 14:05:59 +0200 | [diff] [blame] | 611 | |
| 612 | /* |
| 613 | * Clean old interfaces no longer found. |
| 614 | */ |
| 615 | static void clean_interfaces() |
| 616 | { |
| 617 | struct irec *iface; |
| 618 | struct irec **up = &daemon->interfaces; |
| 619 | |
| 620 | for (iface = *up; iface; iface = *up) |
| 621 | { |
| 622 | if (!iface->found && !iface->done) |
| 623 | { |
| 624 | *up = iface->next; |
| 625 | free(iface->name); |
| 626 | free(iface); |
| 627 | } |
| 628 | else |
| 629 | { |
| 630 | up = &iface->next; |
| 631 | } |
| 632 | } |
| 633 | } |
| 634 | |
Petr Menšík | 49bdf1e | 2019-07-15 17:13:12 +0200 | [diff] [blame] | 635 | /** Release listener if no other interface needs it. |
| 636 | * |
| 637 | * @return 1 if released, 0 if still required |
| 638 | */ |
| 639 | static int release_listener(struct listener *l) |
| 640 | { |
| 641 | if (l->used > 1) |
| 642 | { |
| 643 | struct irec *iface; |
| 644 | for (iface = daemon->interfaces; iface; iface = iface->next) |
| 645 | if (iface->done && sockaddr_isequal(&l->addr, &iface->addr)) |
| 646 | { |
| 647 | if (iface->found) |
| 648 | { |
| 649 | /* update listener to point to active interface instead */ |
| 650 | if (!l->iface->found) |
| 651 | l->iface = iface; |
| 652 | } |
| 653 | else |
| 654 | { |
| 655 | l->used--; |
| 656 | iface->done = 0; |
| 657 | } |
| 658 | } |
| 659 | |
| 660 | /* Someone is still using this listener, skip its deletion */ |
| 661 | if (l->used > 0) |
| 662 | return 0; |
| 663 | } |
| 664 | |
| 665 | if (l->iface->done) |
| 666 | { |
| 667 | int port; |
| 668 | |
| 669 | port = prettyprint_addr(&l->iface->addr, daemon->addrbuff); |
Simon Kelley | 4c30e96 | 2021-03-12 22:09:14 +0000 | [diff] [blame] | 670 | my_syslog(LOG_DEBUG|MS_DEBUG, _("stopped listening on %s(#%d): %s port %d"), |
Petr Menšík | 49bdf1e | 2019-07-15 17:13:12 +0200 | [diff] [blame] | 671 | l->iface->name, l->iface->index, daemon->addrbuff, port); |
| 672 | /* In case it ever returns */ |
| 673 | l->iface->done = 0; |
| 674 | } |
| 675 | |
| 676 | if (l->fd != -1) |
| 677 | close(l->fd); |
| 678 | if (l->tcpfd != -1) |
| 679 | close(l->tcpfd); |
| 680 | if (l->tftpfd != -1) |
| 681 | close(l->tftpfd); |
| 682 | |
| 683 | free(l); |
| 684 | return 1; |
| 685 | } |
| 686 | |
Simon Kelley | 115ac3e | 2013-05-20 11:28:32 +0100 | [diff] [blame] | 687 | int enumerate_interfaces(int reset) |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 688 | { |
Simon Kelley | 115ac3e | 2013-05-20 11:28:32 +0100 | [diff] [blame] | 689 | static struct addrlist *spare = NULL; |
Simon Kelley | a0358e5 | 2014-06-07 13:38:48 +0100 | [diff] [blame] | 690 | static int done = 0; |
Simon Kelley | 115ac3e | 2013-05-20 11:28:32 +0100 | [diff] [blame] | 691 | struct iface_param param; |
| 692 | int errsave, ret = 1; |
| 693 | struct addrlist *addr, *tmp; |
| 694 | struct interface_name *intname; |
Simon Kelley | 0861921 | 2013-12-02 14:43:48 +0000 | [diff] [blame] | 695 | struct irec *iface; |
Simon Kelley | 376d48c | 2013-11-13 13:04:30 +0000 | [diff] [blame] | 696 | #ifdef HAVE_AUTH |
| 697 | struct auth_zone *zone; |
| 698 | #endif |
Simon Kelley | 74d4fcd | 2021-03-15 21:59:51 +0000 | [diff] [blame] | 699 | struct server *serv; |
| 700 | |
Simon Kelley | 76dd75d | 2013-05-23 10:04:25 +0100 | [diff] [blame] | 701 | /* Do this max once per select cycle - also inhibits netlink socket use |
| 702 | in TCP child processes. */ |
Simon Kelley | 397542b | 2013-09-05 11:27:34 +0100 | [diff] [blame] | 703 | |
Simon Kelley | 115ac3e | 2013-05-20 11:28:32 +0100 | [diff] [blame] | 704 | if (reset) |
| 705 | { |
| 706 | done = 0; |
| 707 | return 1; |
| 708 | } |
| 709 | |
Simon Kelley | a0358e5 | 2014-06-07 13:38:48 +0100 | [diff] [blame] | 710 | if (done) |
Simon Kelley | 115ac3e | 2013-05-20 11:28:32 +0100 | [diff] [blame] | 711 | return 1; |
| 712 | |
| 713 | done = 1; |
| 714 | |
| 715 | if ((param.fd = socket(PF_INET, SOCK_DGRAM, 0)) == -1) |
| 716 | return 0; |
Petr Menšík | 4c0aecc | 2021-03-02 18:21:32 +0000 | [diff] [blame] | 717 | |
Simon Kelley | 74d4fcd | 2021-03-15 21:59:51 +0000 | [diff] [blame] | 718 | /* iface indexes can change when interfaces are created/destroyed. |
| 719 | We use them in the main forwarding control path, when the path |
| 720 | to a server is specified by an interface, so cache them. |
| 721 | Update the cache here. */ |
| 722 | for (serv = daemon->servers; serv; serv = serv->next) |
Simon Kelley | 1de6bbc | 2021-03-19 22:24:08 +0000 | [diff] [blame] | 723 | if (serv->interface[0] != 0) |
Simon Kelley | 74d4fcd | 2021-03-15 21:59:51 +0000 | [diff] [blame] | 724 | { |
Simon Kelley | 1de6bbc | 2021-03-19 22:24:08 +0000 | [diff] [blame] | 725 | #ifdef HAVE_LINUX_NETWORK |
| 726 | struct ifreq ifr; |
| 727 | |
| 728 | safe_strncpy(ifr.ifr_name, serv->interface, IF_NAMESIZE); |
| 729 | if (ioctl(param.fd, SIOCGIFINDEX, &ifr) != -1) |
| 730 | serv->ifindex = ifr.ifr_ifindex; |
| 731 | #else |
| 732 | serv->ifindex = if_nametoindex(serv->interface); |
| 733 | #endif |
Simon Kelley | 74d4fcd | 2021-03-15 21:59:51 +0000 | [diff] [blame] | 734 | } |
| 735 | |
Petr Menšík | 4c0aecc | 2021-03-02 18:21:32 +0000 | [diff] [blame] | 736 | again: |
Simon Kelley | 0861921 | 2013-12-02 14:43:48 +0000 | [diff] [blame] | 737 | /* Mark interfaces for garbage collection */ |
| 738 | for (iface = daemon->interfaces; iface; iface = iface->next) |
| 739 | iface->found = 0; |
| 740 | |
Simon Kelley | 115ac3e | 2013-05-20 11:28:32 +0100 | [diff] [blame] | 741 | /* remove addresses stored against interface_names */ |
| 742 | for (intname = daemon->int_names; intname; intname = intname->next) |
| 743 | { |
Simon Kelley | 376d48c | 2013-11-13 13:04:30 +0000 | [diff] [blame] | 744 | for (addr = intname->addr; addr; addr = tmp) |
Simon Kelley | 115ac3e | 2013-05-20 11:28:32 +0100 | [diff] [blame] | 745 | { |
| 746 | tmp = addr->next; |
| 747 | addr->next = spare; |
| 748 | spare = addr; |
| 749 | } |
| 750 | |
Simon Kelley | 376d48c | 2013-11-13 13:04:30 +0000 | [diff] [blame] | 751 | intname->addr = NULL; |
Simon Kelley | 115ac3e | 2013-05-20 11:28:32 +0100 | [diff] [blame] | 752 | } |
Simon Kelley | 376d48c | 2013-11-13 13:04:30 +0000 | [diff] [blame] | 753 | |
Simon Kelley | c8a8048 | 2014-03-05 14:29:54 +0000 | [diff] [blame] | 754 | /* Remove list of addresses of local interfaces */ |
| 755 | for (addr = daemon->interface_addrs; addr; addr = tmp) |
| 756 | { |
| 757 | tmp = addr->next; |
| 758 | addr->next = spare; |
| 759 | spare = addr; |
| 760 | } |
| 761 | daemon->interface_addrs = NULL; |
| 762 | |
Simon Kelley | 376d48c | 2013-11-13 13:04:30 +0000 | [diff] [blame] | 763 | #ifdef HAVE_AUTH |
| 764 | /* remove addresses stored against auth_zone subnets, but not |
| 765 | ones configured as address literals */ |
| 766 | for (zone = daemon->auth_zones; zone; zone = zone->next) |
| 767 | if (zone->interface_names) |
| 768 | { |
| 769 | struct addrlist **up; |
| 770 | for (up = &zone->subnet, addr = zone->subnet; addr; addr = tmp) |
| 771 | { |
| 772 | tmp = addr->next; |
| 773 | if (addr->flags & ADDRLIST_LITERAL) |
| 774 | up = &addr->next; |
| 775 | else |
| 776 | { |
| 777 | *up = addr->next; |
| 778 | addr->next = spare; |
| 779 | spare = addr; |
| 780 | } |
| 781 | } |
| 782 | } |
| 783 | #endif |
| 784 | |
Simon Kelley | 115ac3e | 2013-05-20 11:28:32 +0100 | [diff] [blame] | 785 | param.spare = spare; |
| 786 | |
Simon Kelley | 115ac3e | 2013-05-20 11:28:32 +0100 | [diff] [blame] | 787 | ret = iface_enumerate(AF_INET6, ¶m, iface_allowed_v6); |
Petr Menšík | 4c0aecc | 2021-03-02 18:21:32 +0000 | [diff] [blame] | 788 | if (ret < 0) |
| 789 | goto again; |
| 790 | else if (ret) |
| 791 | { |
| 792 | ret = iface_enumerate(AF_INET, ¶m, iface_allowed_v4); |
| 793 | if (ret < 0) |
| 794 | goto again; |
| 795 | } |
Simon Kelley | 115ac3e | 2013-05-20 11:28:32 +0100 | [diff] [blame] | 796 | |
| 797 | errsave = errno; |
| 798 | close(param.fd); |
Simon Kelley | 0861921 | 2013-12-02 14:43:48 +0000 | [diff] [blame] | 799 | |
| 800 | if (option_bool(OPT_CLEVERBIND)) |
| 801 | { |
| 802 | /* Garbage-collect listeners listening on addresses that no longer exist. |
| 803 | Does nothing when not binding interfaces or for listeners on localhost, |
| 804 | since the ->iface field is NULL. Note that this needs the protections |
Josh Soref | 730c674 | 2017-02-06 16:14:04 +0000 | [diff] [blame] | 805 | against reentrancy, hence it's here. It also means there's a possibility, |
Simon Kelley | 0861921 | 2013-12-02 14:43:48 +0000 | [diff] [blame] | 806 | in OPT_CLEVERBIND mode, that at listener will just disappear after |
| 807 | a call to enumerate_interfaces, this is checked OK on all calls. */ |
| 808 | struct listener *l, *tmp, **up; |
Petr Mensik | 60a3ae1 | 2019-07-09 14:05:59 +0200 | [diff] [blame] | 809 | int freed = 0; |
Simon Kelley | 0861921 | 2013-12-02 14:43:48 +0000 | [diff] [blame] | 810 | |
| 811 | for (up = &daemon->listeners, l = daemon->listeners; l; l = tmp) |
| 812 | { |
| 813 | tmp = l->next; |
| 814 | |
| 815 | if (!l->iface || l->iface->found) |
| 816 | up = &l->next; |
Petr Menšík | 49bdf1e | 2019-07-15 17:13:12 +0200 | [diff] [blame] | 817 | else if (release_listener(l)) |
Simon Kelley | 0861921 | 2013-12-02 14:43:48 +0000 | [diff] [blame] | 818 | { |
Petr Menšík | 49bdf1e | 2019-07-15 17:13:12 +0200 | [diff] [blame] | 819 | *up = tmp; |
Simon Kelley | 619000a | 2020-04-29 00:09:58 +0100 | [diff] [blame] | 820 | freed = 1; |
Simon Kelley | 0861921 | 2013-12-02 14:43:48 +0000 | [diff] [blame] | 821 | } |
| 822 | } |
Petr Mensik | 60a3ae1 | 2019-07-09 14:05:59 +0200 | [diff] [blame] | 823 | |
| 824 | if (freed) |
| 825 | clean_interfaces(); |
Simon Kelley | 0861921 | 2013-12-02 14:43:48 +0000 | [diff] [blame] | 826 | } |
Petr Mensik | 60a3ae1 | 2019-07-09 14:05:59 +0200 | [diff] [blame] | 827 | |
Simon Kelley | 115ac3e | 2013-05-20 11:28:32 +0100 | [diff] [blame] | 828 | errno = errsave; |
Simon Kelley | 115ac3e | 2013-05-20 11:28:32 +0100 | [diff] [blame] | 829 | spare = param.spare; |
Simon Kelley | 74d4fcd | 2021-03-15 21:59:51 +0000 | [diff] [blame] | 830 | |
Simon Kelley | 115ac3e | 2013-05-20 11:28:32 +0100 | [diff] [blame] | 831 | return ret; |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 832 | } |
| 833 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 834 | /* set NONBLOCK bit on fd: See Stevens 16.6 */ |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 835 | int fix_fd(int fd) |
| 836 | { |
| 837 | int flags; |
| 838 | |
| 839 | if ((flags = fcntl(fd, F_GETFL)) == -1 || |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 840 | fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 841 | return 0; |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 842 | |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 843 | return 1; |
| 844 | } |
| 845 | |
Simon Kelley | 74c95c2 | 2011-10-19 09:33:39 +0100 | [diff] [blame] | 846 | static int make_sock(union mysockaddr *addr, int type, int dienow) |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 847 | { |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 848 | int family = addr->sa.sa_family; |
| 849 | int fd, rc, opt = 1; |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 850 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 851 | if ((fd = socket(family, type, 0)) == -1) |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 852 | { |
Josh Soref | 730c674 | 2017-02-06 16:14:04 +0000 | [diff] [blame] | 853 | int port, errsave; |
Simon Kelley | 39f1b8e | 2012-06-20 20:04:27 +0100 | [diff] [blame] | 854 | char *s; |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 855 | |
| 856 | /* No error if the kernel just doesn't support this IP flavour */ |
| 857 | if (errno == EPROTONOSUPPORT || |
| 858 | errno == EAFNOSUPPORT || |
| 859 | errno == EINVAL) |
| 860 | return -1; |
| 861 | |
| 862 | err: |
Josh Soref | 730c674 | 2017-02-06 16:14:04 +0000 | [diff] [blame] | 863 | errsave = errno; |
Simon Kelley | 39f1b8e | 2012-06-20 20:04:27 +0100 | [diff] [blame] | 864 | port = prettyprint_addr(addr, daemon->addrbuff); |
| 865 | if (!option_bool(OPT_NOWILD) && !option_bool(OPT_CLEVERBIND)) |
| 866 | sprintf(daemon->addrbuff, "port %d", port); |
| 867 | s = _("failed to create listening socket for %s: %s"); |
| 868 | |
Simon Kelley | 5f11b3e | 2012-08-16 14:04:05 +0100 | [diff] [blame] | 869 | if (fd != -1) |
Matthias Andree | 71aaa5a | 2013-12-03 11:20:45 +0000 | [diff] [blame] | 870 | close (fd); |
| 871 | |
Josh Soref | 730c674 | 2017-02-06 16:14:04 +0000 | [diff] [blame] | 872 | errno = errsave; |
Simon Kelley | 1ee9be4 | 2013-12-09 16:50:19 +0000 | [diff] [blame] | 873 | |
Simon Kelley | 5f11b3e | 2012-08-16 14:04:05 +0100 | [diff] [blame] | 874 | if (dienow) |
| 875 | { |
| 876 | /* failure to bind addresses given by --listen-address at this point |
| 877 | is OK if we're doing bind-dynamic */ |
| 878 | if (!option_bool(OPT_CLEVERBIND)) |
| 879 | die(s, daemon->addrbuff, EC_BADNET); |
| 880 | } |
| 881 | else |
| 882 | my_syslog(LOG_WARNING, s, daemon->addrbuff, strerror(errno)); |
| 883 | |
Simon Kelley | 74c95c2 | 2011-10-19 09:33:39 +0100 | [diff] [blame] | 884 | return -1; |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 885 | } |
Simon Kelley | 5f11b3e | 2012-08-16 14:04:05 +0100 | [diff] [blame] | 886 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 887 | if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) == -1 || !fix_fd(fd)) |
| 888 | goto err; |
| 889 | |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 890 | if (family == AF_INET6 && setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &opt, sizeof(opt)) == -1) |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 891 | goto err; |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 892 | |
Simon Kelley | 74c95c2 | 2011-10-19 09:33:39 +0100 | [diff] [blame] | 893 | if ((rc = bind(fd, (struct sockaddr *)addr, sa_len(addr))) == -1) |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 894 | goto err; |
| 895 | |
| 896 | if (type == SOCK_STREAM) |
| 897 | { |
Simon Kelley | 608aa9f | 2019-03-10 22:44:15 +0000 | [diff] [blame] | 898 | #ifdef TCP_FASTOPEN |
| 899 | int qlen = 5; |
Matthias Andree | e39c484 | 2020-03-05 15:58:31 +0000 | [diff] [blame] | 900 | setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, sizeof(qlen)); |
Simon Kelley | 608aa9f | 2019-03-10 22:44:15 +0000 | [diff] [blame] | 901 | #endif |
| 902 | |
Simon Kelley | 09b768e | 2016-12-22 22:16:58 +0000 | [diff] [blame] | 903 | if (listen(fd, TCP_BACKLOG) == -1) |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 904 | goto err; |
| 905 | } |
Simon Kelley | 2329bef | 2013-12-03 13:41:16 +0000 | [diff] [blame] | 906 | else if (family == AF_INET) |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 907 | { |
Simon Kelley | 2329bef | 2013-12-03 13:41:16 +0000 | [diff] [blame] | 908 | if (!option_bool(OPT_NOWILD)) |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 909 | { |
| 910 | #if defined(HAVE_LINUX_NETWORK) |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 911 | if (setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &opt, sizeof(opt)) == -1) |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 912 | goto err; |
| 913 | #elif defined(IP_RECVDSTADDR) && defined(IP_RECVIF) |
| 914 | if (setsockopt(fd, IPPROTO_IP, IP_RECVDSTADDR, &opt, sizeof(opt)) == -1 || |
| 915 | setsockopt(fd, IPPROTO_IP, IP_RECVIF, &opt, sizeof(opt)) == -1) |
| 916 | goto err; |
| 917 | #endif |
| 918 | } |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 919 | } |
Simon Kelley | 2329bef | 2013-12-03 13:41:16 +0000 | [diff] [blame] | 920 | else if (!set_ipv6pktinfo(fd)) |
| 921 | goto err; |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 922 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 923 | return fd; |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 924 | } |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 925 | |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 926 | int set_ipv6pktinfo(int fd) |
| 927 | { |
| 928 | int opt = 1; |
| 929 | |
| 930 | /* The API changed around Linux 2.6.14 but the old ABI is still supported: |
| 931 | handle all combinations of headers and kernel. |
| 932 | OpenWrt note that this fixes the problem addressed by your very broken patch. */ |
| 933 | daemon->v6pktinfo = IPV6_PKTINFO; |
| 934 | |
| 935 | #ifdef IPV6_RECVPKTINFO |
| 936 | if (setsockopt(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &opt, sizeof(opt)) != -1) |
| 937 | return 1; |
| 938 | # ifdef IPV6_2292PKTINFO |
| 939 | else if (errno == ENOPROTOOPT && setsockopt(fd, IPPROTO_IPV6, IPV6_2292PKTINFO, &opt, sizeof(opt)) != -1) |
| 940 | { |
| 941 | daemon->v6pktinfo = IPV6_2292PKTINFO; |
| 942 | return 1; |
| 943 | } |
| 944 | # endif |
| 945 | #else |
| 946 | if (setsockopt(fd, IPPROTO_IPV6, IPV6_PKTINFO, &opt, sizeof(opt)) != -1) |
| 947 | return 1; |
| 948 | #endif |
| 949 | |
| 950 | return 0; |
| 951 | } |
Simon Kelley | 22ce550 | 2013-01-22 13:53:04 +0000 | [diff] [blame] | 952 | |
| 953 | |
| 954 | /* Find the interface on which a TCP connection arrived, if possible, or zero otherwise. */ |
| 955 | int tcp_interface(int fd, int af) |
| 956 | { |
Kevin Darbyshire-Bryant | 70c50ef | 2020-03-06 10:31:15 +0000 | [diff] [blame] | 957 | (void)fd; /* suppress potential unused warning */ |
| 958 | (void)af; /* suppress potential unused warning */ |
Simon Kelley | 22ce550 | 2013-01-22 13:53:04 +0000 | [diff] [blame] | 959 | int if_index = 0; |
| 960 | |
| 961 | #ifdef HAVE_LINUX_NETWORK |
| 962 | int opt = 1; |
| 963 | struct cmsghdr *cmptr; |
| 964 | struct msghdr msg; |
Simon Kelley | 529b030 | 2016-03-16 19:00:45 +0000 | [diff] [blame] | 965 | socklen_t len; |
Simon Kelley | 22ce550 | 2013-01-22 13:53:04 +0000 | [diff] [blame] | 966 | |
Simon Kelley | 529b030 | 2016-03-16 19:00:45 +0000 | [diff] [blame] | 967 | /* use mshdr so that the CMSDG_* macros are available */ |
Simon Kelley | 22ce550 | 2013-01-22 13:53:04 +0000 | [diff] [blame] | 968 | msg.msg_control = daemon->packet; |
Simon Kelley | 529b030 | 2016-03-16 19:00:45 +0000 | [diff] [blame] | 969 | msg.msg_controllen = len = daemon->packet_buff_sz; |
Simon Kelley | 74d4fcd | 2021-03-15 21:59:51 +0000 | [diff] [blame] | 970 | |
Simon Kelley | 22ce550 | 2013-01-22 13:53:04 +0000 | [diff] [blame] | 971 | /* we overwrote the buffer... */ |
Simon Kelley | 74d4fcd | 2021-03-15 21:59:51 +0000 | [diff] [blame] | 972 | daemon->srv_save = NULL; |
| 973 | |
Simon Kelley | 22ce550 | 2013-01-22 13:53:04 +0000 | [diff] [blame] | 974 | if (af == AF_INET) |
| 975 | { |
| 976 | if (setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &opt, sizeof(opt)) != -1 && |
Simon Kelley | 529b030 | 2016-03-16 19:00:45 +0000 | [diff] [blame] | 977 | getsockopt(fd, IPPROTO_IP, IP_PKTOPTIONS, msg.msg_control, &len) != -1) |
| 978 | { |
| 979 | msg.msg_controllen = len; |
| 980 | for (cmptr = CMSG_FIRSTHDR(&msg); cmptr; cmptr = CMSG_NXTHDR(&msg, cmptr)) |
| 981 | if (cmptr->cmsg_level == IPPROTO_IP && cmptr->cmsg_type == IP_PKTINFO) |
| 982 | { |
| 983 | union { |
| 984 | unsigned char *c; |
| 985 | struct in_pktinfo *p; |
| 986 | } p; |
| 987 | |
| 988 | p.c = CMSG_DATA(cmptr); |
| 989 | if_index = p.p->ipi_ifindex; |
| 990 | } |
| 991 | } |
Simon Kelley | 22ce550 | 2013-01-22 13:53:04 +0000 | [diff] [blame] | 992 | } |
Simon Kelley | 22ce550 | 2013-01-22 13:53:04 +0000 | [diff] [blame] | 993 | else |
| 994 | { |
| 995 | /* Only the RFC-2292 API has the ability to find the interface for TCP connections, |
| 996 | it was removed in RFC-3542 !!!! |
| 997 | |
| 998 | Fortunately, Linux kept the 2292 ABI when it moved to 3542. The following code always |
| 999 | uses the old ABI, and should work with pre- and post-3542 kernel headers */ |
| 1000 | |
| 1001 | #ifdef IPV6_2292PKTOPTIONS |
| 1002 | # define PKTOPTIONS IPV6_2292PKTOPTIONS |
| 1003 | #else |
| 1004 | # define PKTOPTIONS IPV6_PKTOPTIONS |
| 1005 | #endif |
| 1006 | |
| 1007 | if (set_ipv6pktinfo(fd) && |
Simon Kelley | 529b030 | 2016-03-16 19:00:45 +0000 | [diff] [blame] | 1008 | getsockopt(fd, IPPROTO_IPV6, PKTOPTIONS, msg.msg_control, &len) != -1) |
Simon Kelley | 22ce550 | 2013-01-22 13:53:04 +0000 | [diff] [blame] | 1009 | { |
Simon Kelley | 529b030 | 2016-03-16 19:00:45 +0000 | [diff] [blame] | 1010 | msg.msg_controllen = len; |
| 1011 | for (cmptr = CMSG_FIRSTHDR(&msg); cmptr; cmptr = CMSG_NXTHDR(&msg, cmptr)) |
Simon Kelley | 22ce550 | 2013-01-22 13:53:04 +0000 | [diff] [blame] | 1012 | if (cmptr->cmsg_level == IPPROTO_IPV6 && cmptr->cmsg_type == daemon->v6pktinfo) |
| 1013 | { |
| 1014 | union { |
| 1015 | unsigned char *c; |
| 1016 | struct in6_pktinfo *p; |
| 1017 | } p; |
| 1018 | p.c = CMSG_DATA(cmptr); |
| 1019 | |
| 1020 | if_index = p.p->ipi6_ifindex; |
| 1021 | } |
| 1022 | } |
| 1023 | } |
Simon Kelley | 22ce550 | 2013-01-22 13:53:04 +0000 | [diff] [blame] | 1024 | #endif /* Linux */ |
| 1025 | |
| 1026 | return if_index; |
| 1027 | } |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1028 | |
Simon Kelley | 74c95c2 | 2011-10-19 09:33:39 +0100 | [diff] [blame] | 1029 | static struct listener *create_listeners(union mysockaddr *addr, int do_tftp, int dienow) |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1030 | { |
| 1031 | struct listener *l = NULL; |
| 1032 | int fd = -1, tcpfd = -1, tftpfd = -1; |
| 1033 | |
Vladislav Grishenko | 408c368 | 2013-09-24 16:18:49 +0100 | [diff] [blame] | 1034 | (void)do_tftp; |
| 1035 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1036 | if (daemon->port != 0) |
| 1037 | { |
Simon Kelley | 74c95c2 | 2011-10-19 09:33:39 +0100 | [diff] [blame] | 1038 | fd = make_sock(addr, SOCK_DGRAM, dienow); |
| 1039 | tcpfd = make_sock(addr, SOCK_STREAM, dienow); |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1040 | } |
| 1041 | |
| 1042 | #ifdef HAVE_TFTP |
| 1043 | if (do_tftp) |
| 1044 | { |
| 1045 | if (addr->sa.sa_family == AF_INET) |
| 1046 | { |
| 1047 | /* port must be restored to DNS port for TCP code */ |
| 1048 | short save = addr->in.sin_port; |
| 1049 | addr->in.sin_port = htons(TFTP_PORT); |
Simon Kelley | 74c95c2 | 2011-10-19 09:33:39 +0100 | [diff] [blame] | 1050 | tftpfd = make_sock(addr, SOCK_DGRAM, dienow); |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1051 | addr->in.sin_port = save; |
| 1052 | } |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1053 | else |
| 1054 | { |
| 1055 | short save = addr->in6.sin6_port; |
| 1056 | addr->in6.sin6_port = htons(TFTP_PORT); |
Simon Kelley | 74c95c2 | 2011-10-19 09:33:39 +0100 | [diff] [blame] | 1057 | tftpfd = make_sock(addr, SOCK_DGRAM, dienow); |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1058 | addr->in6.sin6_port = save; |
| 1059 | } |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1060 | } |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 1061 | #endif |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 1062 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1063 | if (fd != -1 || tcpfd != -1 || tftpfd != -1) |
| 1064 | { |
| 1065 | l = safe_malloc(sizeof(struct listener)); |
| 1066 | l->next = NULL; |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1067 | l->fd = fd; |
| 1068 | l->tcpfd = tcpfd; |
Petr Menšík | 49bdf1e | 2019-07-15 17:13:12 +0200 | [diff] [blame] | 1069 | l->tftpfd = tftpfd; |
| 1070 | l->addr = *addr; |
| 1071 | l->used = 1; |
Simon Kelley | 0861921 | 2013-12-02 14:43:48 +0000 | [diff] [blame] | 1072 | l->iface = NULL; |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1073 | } |
| 1074 | |
| 1075 | return l; |
| 1076 | } |
| 1077 | |
Simon Kelley | 74c95c2 | 2011-10-19 09:33:39 +0100 | [diff] [blame] | 1078 | void create_wildcard_listeners(void) |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 1079 | { |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 1080 | union mysockaddr addr; |
Simon Kelley | 8bc4cec | 2012-07-03 21:04:11 +0100 | [diff] [blame] | 1081 | struct listener *l, *l6; |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 1082 | |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 1083 | memset(&addr, 0, sizeof(addr)); |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1084 | #ifdef HAVE_SOCKADDR_SA_LEN |
| 1085 | addr.in.sin_len = sizeof(addr.in); |
| 1086 | #endif |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 1087 | addr.in.sin_family = AF_INET; |
| 1088 | addr.in.sin_addr.s_addr = INADDR_ANY; |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1089 | addr.in.sin_port = htons(daemon->port); |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 1090 | |
Simon Kelley | 8bc4cec | 2012-07-03 21:04:11 +0100 | [diff] [blame] | 1091 | l = create_listeners(&addr, !!option_bool(OPT_TFTP), 1); |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1092 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1093 | memset(&addr, 0, sizeof(addr)); |
Simon Kelley | ee87504 | 2018-10-23 22:10:17 +0100 | [diff] [blame] | 1094 | #ifdef HAVE_SOCKADDR_SA_LEN |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1095 | addr.in6.sin6_len = sizeof(addr.in6); |
Simon Kelley | ee87504 | 2018-10-23 22:10:17 +0100 | [diff] [blame] | 1096 | #endif |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1097 | addr.in6.sin6_family = AF_INET6; |
| 1098 | addr.in6.sin6_addr = in6addr_any; |
| 1099 | addr.in6.sin6_port = htons(daemon->port); |
Simon Kelley | 8bc4cec | 2012-07-03 21:04:11 +0100 | [diff] [blame] | 1100 | |
| 1101 | l6 = create_listeners(&addr, !!option_bool(OPT_TFTP), 1); |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1102 | if (l) |
Simon Kelley | 8bc4cec | 2012-07-03 21:04:11 +0100 | [diff] [blame] | 1103 | l->next = l6; |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1104 | else |
Simon Kelley | 8bc4cec | 2012-07-03 21:04:11 +0100 | [diff] [blame] | 1105 | l = l6; |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 1106 | |
Simon Kelley | 74c95c2 | 2011-10-19 09:33:39 +0100 | [diff] [blame] | 1107 | daemon->listeners = l; |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 1108 | } |
| 1109 | |
Petr Menšík | 49bdf1e | 2019-07-15 17:13:12 +0200 | [diff] [blame] | 1110 | static struct listener *find_listener(union mysockaddr *addr) |
| 1111 | { |
| 1112 | struct listener *l; |
| 1113 | for (l = daemon->listeners; l; l = l->next) |
| 1114 | if (sockaddr_isequal(&l->addr, addr)) |
| 1115 | return l; |
| 1116 | return NULL; |
| 1117 | } |
| 1118 | |
Simon Kelley | 74c95c2 | 2011-10-19 09:33:39 +0100 | [diff] [blame] | 1119 | void create_bound_listeners(int dienow) |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 1120 | { |
Simon Kelley | 74c95c2 | 2011-10-19 09:33:39 +0100 | [diff] [blame] | 1121 | struct listener *new; |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 1122 | struct irec *iface; |
Simon Kelley | 52d4abf | 2012-03-21 21:39:48 +0000 | [diff] [blame] | 1123 | struct iname *if_tmp; |
Petr Menšík | 49bdf1e | 2019-07-15 17:13:12 +0200 | [diff] [blame] | 1124 | struct listener *existing; |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 1125 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 1126 | for (iface = daemon->interfaces; iface; iface = iface->next) |
Petr Menšík | 49bdf1e | 2019-07-15 17:13:12 +0200 | [diff] [blame] | 1127 | if (!iface->done && !iface->dad && iface->found) |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1128 | { |
Petr Menšík | 49bdf1e | 2019-07-15 17:13:12 +0200 | [diff] [blame] | 1129 | existing = find_listener(&iface->addr); |
| 1130 | if (existing) |
| 1131 | { |
| 1132 | iface->done = 1; |
| 1133 | existing->used++; /* increase usage counter */ |
| 1134 | } |
| 1135 | else if ((new = create_listeners(&iface->addr, iface->tftp_ok, dienow))) |
| 1136 | { |
Petr Menšík | 49bdf1e | 2019-07-15 17:13:12 +0200 | [diff] [blame] | 1137 | new->iface = iface; |
| 1138 | new->next = daemon->listeners; |
| 1139 | daemon->listeners = new; |
| 1140 | iface->done = 1; |
Simon Kelley | 619000a | 2020-04-29 00:09:58 +0100 | [diff] [blame] | 1141 | |
| 1142 | /* Don't log the initial set of listen addresses created |
| 1143 | at startup, since this is happening before the logging |
| 1144 | system is initialised and the sign-on printed. */ |
| 1145 | if (!dienow) |
| 1146 | { |
| 1147 | int port = prettyprint_addr(&iface->addr, daemon->addrbuff); |
Simon Kelley | 4c30e96 | 2021-03-12 22:09:14 +0000 | [diff] [blame] | 1148 | my_syslog(LOG_DEBUG|MS_DEBUG, _("listening on %s(#%d): %s port %d"), |
Simon Kelley | 619000a | 2020-04-29 00:09:58 +0100 | [diff] [blame] | 1149 | iface->name, iface->index, daemon->addrbuff, port); |
| 1150 | } |
Petr Menšík | 49bdf1e | 2019-07-15 17:13:12 +0200 | [diff] [blame] | 1151 | } |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1152 | } |
Simon Kelley | 52d4abf | 2012-03-21 21:39:48 +0000 | [diff] [blame] | 1153 | |
| 1154 | /* Check for --listen-address options that haven't been used because there's |
| 1155 | no interface with a matching address. These may be valid: eg it's possible |
| 1156 | to listen on 127.0.1.1 even if the loopback interface is 127.0.0.1 |
| 1157 | |
Simon Kelley | 5f11b3e | 2012-08-16 14:04:05 +0100 | [diff] [blame] | 1158 | If the address isn't valid the bind() will fail and we'll die() |
| 1159 | (except in bind-dynamic mode, when we'll complain but keep trying.) |
Simon Kelley | 52d4abf | 2012-03-21 21:39:48 +0000 | [diff] [blame] | 1160 | |
| 1161 | The resulting listeners have the ->iface field NULL, and this has to be |
| 1162 | handled by the DNS and TFTP code. It disables --localise-queries processing |
| 1163 | (no netmask) and some MTU login the tftp code. */ |
| 1164 | |
| 1165 | for (if_tmp = daemon->if_addrs; if_tmp; if_tmp = if_tmp->next) |
| 1166 | if (!if_tmp->used && |
Simon Kelley | 8bc4cec | 2012-07-03 21:04:11 +0100 | [diff] [blame] | 1167 | (new = create_listeners(&if_tmp->addr, !!option_bool(OPT_TFTP), dienow))) |
Simon Kelley | 52d4abf | 2012-03-21 21:39:48 +0000 | [diff] [blame] | 1168 | { |
Simon Kelley | 52d4abf | 2012-03-21 21:39:48 +0000 | [diff] [blame] | 1169 | new->next = daemon->listeners; |
| 1170 | daemon->listeners = new; |
Simon Kelley | 619000a | 2020-04-29 00:09:58 +0100 | [diff] [blame] | 1171 | |
| 1172 | if (!dienow) |
| 1173 | { |
| 1174 | int port = prettyprint_addr(&if_tmp->addr, daemon->addrbuff); |
Simon Kelley | 4c30e96 | 2021-03-12 22:09:14 +0000 | [diff] [blame] | 1175 | my_syslog(LOG_DEBUG|MS_DEBUG, _("listening on %s port %d"), daemon->addrbuff, port); |
Simon Kelley | 619000a | 2020-04-29 00:09:58 +0100 | [diff] [blame] | 1176 | } |
Simon Kelley | 52d4abf | 2012-03-21 21:39:48 +0000 | [diff] [blame] | 1177 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1178 | } |
| 1179 | |
Simon Kelley | dc27e14 | 2013-10-16 13:09:53 +0100 | [diff] [blame] | 1180 | /* In --bind-interfaces, the only access control is the addresses we're listening on. |
| 1181 | There's nothing to avoid a query to the address of an internal interface arriving via |
| 1182 | an external interface where we don't want to accept queries, except that in the usual |
| 1183 | case the addresses of internal interfaces are RFC1918. When bind-interfaces in use, |
| 1184 | and we listen on an address that looks like it's probably globally routeable, shout. |
| 1185 | |
| 1186 | The fix is to use --bind-dynamic, which actually checks the arrival interface too. |
| 1187 | Tough if your platform doesn't support this. |
Simon Kelley | 2329bef | 2013-12-03 13:41:16 +0000 | [diff] [blame] | 1188 | |
| 1189 | Note that checking the arrival interface is supported in the standard IPv6 API and |
| 1190 | always done, so we don't warn about any IPv6 addresses here. |
Simon Kelley | dc27e14 | 2013-10-16 13:09:53 +0100 | [diff] [blame] | 1191 | */ |
| 1192 | |
| 1193 | void warn_bound_listeners(void) |
| 1194 | { |
| 1195 | struct irec *iface; |
| 1196 | int advice = 0; |
| 1197 | |
| 1198 | for (iface = daemon->interfaces; iface; iface = iface->next) |
Simon Kelley | f7029f5 | 2013-11-21 15:09:09 +0000 | [diff] [blame] | 1199 | if (!iface->dns_auth) |
Simon Kelley | dc27e14 | 2013-10-16 13:09:53 +0100 | [diff] [blame] | 1200 | { |
Simon Kelley | dc27e14 | 2013-10-16 13:09:53 +0100 | [diff] [blame] | 1201 | if (iface->addr.sa.sa_family == AF_INET) |
| 1202 | { |
| 1203 | if (!private_net(iface->addr.in.sin_addr, 1)) |
| 1204 | { |
| 1205 | inet_ntop(AF_INET, &iface->addr.in.sin_addr, daemon->addrbuff, ADDRSTRLEN); |
Simon Kelley | 2329bef | 2013-12-03 13:41:16 +0000 | [diff] [blame] | 1206 | iface->warned = advice = 1; |
| 1207 | my_syslog(LOG_WARNING, |
| 1208 | _("LOUD WARNING: listening on %s may accept requests via interfaces other than %s"), |
| 1209 | daemon->addrbuff, iface->name); |
Simon Kelley | dc27e14 | 2013-10-16 13:09:53 +0100 | [diff] [blame] | 1210 | } |
| 1211 | } |
Simon Kelley | dc27e14 | 2013-10-16 13:09:53 +0100 | [diff] [blame] | 1212 | } |
| 1213 | |
| 1214 | if (advice) |
Simon Kelley | f7029f5 | 2013-11-21 15:09:09 +0000 | [diff] [blame] | 1215 | my_syslog(LOG_WARNING, _("LOUD WARNING: use --bind-dynamic rather than --bind-interfaces to avoid DNS amplification attacks via these interface(s)")); |
Simon Kelley | dc27e14 | 2013-10-16 13:09:53 +0100 | [diff] [blame] | 1216 | } |
| 1217 | |
Petr Menšík | ad59f27 | 2017-03-17 17:22:19 +0000 | [diff] [blame] | 1218 | void warn_wild_labels(void) |
| 1219 | { |
| 1220 | struct irec *iface; |
| 1221 | |
| 1222 | for (iface = daemon->interfaces; iface; iface = iface->next) |
| 1223 | if (iface->found && iface->name && iface->label) |
| 1224 | my_syslog(LOG_WARNING, _("warning: using interface %s instead"), iface->name); |
| 1225 | } |
| 1226 | |
Simon Kelley | f7029f5 | 2013-11-21 15:09:09 +0000 | [diff] [blame] | 1227 | void warn_int_names(void) |
| 1228 | { |
| 1229 | struct interface_name *intname; |
| 1230 | |
| 1231 | for (intname = daemon->int_names; intname; intname = intname->next) |
| 1232 | if (!intname->addr) |
| 1233 | my_syslog(LOG_WARNING, _("warning: no addresses found for interface %s"), intname->intr); |
| 1234 | } |
| 1235 | |
Simon Kelley | 74c95c2 | 2011-10-19 09:33:39 +0100 | [diff] [blame] | 1236 | int is_dad_listeners(void) |
| 1237 | { |
| 1238 | struct irec *iface; |
| 1239 | |
| 1240 | if (option_bool(OPT_NOWILD)) |
| 1241 | for (iface = daemon->interfaces; iface; iface = iface->next) |
| 1242 | if (iface->dad && !iface->done) |
| 1243 | return 1; |
| 1244 | |
| 1245 | return 0; |
| 1246 | } |
Simon Kelley | 5d162f2 | 2012-12-20 14:55:46 +0000 | [diff] [blame] | 1247 | |
| 1248 | #ifdef HAVE_DHCP6 |
| 1249 | void join_multicast(int dienow) |
| 1250 | { |
| 1251 | struct irec *iface, *tmp; |
| 1252 | |
| 1253 | for (iface = daemon->interfaces; iface; iface = iface->next) |
Simon Kelley | 1962446 | 2012-12-28 11:18:09 +0000 | [diff] [blame] | 1254 | if (iface->addr.sa.sa_family == AF_INET6 && iface->dhcp_ok && !iface->multicast_done) |
Simon Kelley | 5d162f2 | 2012-12-20 14:55:46 +0000 | [diff] [blame] | 1255 | { |
| 1256 | /* There's an irec per address but we only want to join for multicast |
| 1257 | once per interface. Weed out duplicates. */ |
| 1258 | for (tmp = daemon->interfaces; tmp; tmp = tmp->next) |
| 1259 | if (tmp->multicast_done && tmp->index == iface->index) |
| 1260 | break; |
| 1261 | |
| 1262 | iface->multicast_done = 1; |
| 1263 | |
| 1264 | if (!tmp) |
| 1265 | { |
| 1266 | struct ipv6_mreq mreq; |
| 1267 | int err = 0; |
| 1268 | |
| 1269 | mreq.ipv6mr_interface = iface->index; |
| 1270 | |
| 1271 | inet_pton(AF_INET6, ALL_RELAY_AGENTS_AND_SERVERS, &mreq.ipv6mr_multiaddr); |
| 1272 | |
Simon Kelley | ff7eea2 | 2013-09-04 18:01:38 +0100 | [diff] [blame] | 1273 | if ((daemon->doing_dhcp6 || daemon->relay6) && |
Simon Kelley | 5d162f2 | 2012-12-20 14:55:46 +0000 | [diff] [blame] | 1274 | setsockopt(daemon->dhcp6fd, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq, sizeof(mreq)) == -1) |
Simon Kelley | 9cdcfe9 | 2015-08-26 22:38:08 +0100 | [diff] [blame] | 1275 | err = errno; |
Simon Kelley | 5d162f2 | 2012-12-20 14:55:46 +0000 | [diff] [blame] | 1276 | |
| 1277 | inet_pton(AF_INET6, ALL_SERVERS, &mreq.ipv6mr_multiaddr); |
| 1278 | |
| 1279 | if (daemon->doing_dhcp6 && |
| 1280 | setsockopt(daemon->dhcp6fd, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq, sizeof(mreq)) == -1) |
Simon Kelley | 9cdcfe9 | 2015-08-26 22:38:08 +0100 | [diff] [blame] | 1281 | err = errno; |
Simon Kelley | 5d162f2 | 2012-12-20 14:55:46 +0000 | [diff] [blame] | 1282 | |
| 1283 | inet_pton(AF_INET6, ALL_ROUTERS, &mreq.ipv6mr_multiaddr); |
| 1284 | |
| 1285 | if (daemon->doing_ra && |
| 1286 | setsockopt(daemon->icmp6fd, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq, sizeof(mreq)) == -1) |
Simon Kelley | 9cdcfe9 | 2015-08-26 22:38:08 +0100 | [diff] [blame] | 1287 | err = errno; |
Simon Kelley | 5d162f2 | 2012-12-20 14:55:46 +0000 | [diff] [blame] | 1288 | |
| 1289 | if (err) |
| 1290 | { |
| 1291 | char *s = _("interface %s failed to join DHCPv6 multicast group: %s"); |
Simon Kelley | 9cdcfe9 | 2015-08-26 22:38:08 +0100 | [diff] [blame] | 1292 | errno = err; |
| 1293 | |
| 1294 | #ifdef HAVE_LINUX_NETWORK |
| 1295 | if (errno == ENOMEM) |
| 1296 | my_syslog(LOG_ERR, _("try increasing /proc/sys/net/core/optmem_max")); |
| 1297 | #endif |
| 1298 | |
Simon Kelley | 5d162f2 | 2012-12-20 14:55:46 +0000 | [diff] [blame] | 1299 | if (dienow) |
| 1300 | die(s, iface->name, EC_BADNET); |
| 1301 | else |
| 1302 | my_syslog(LOG_ERR, s, iface->name, strerror(errno)); |
| 1303 | } |
| 1304 | } |
| 1305 | } |
| 1306 | } |
| 1307 | #endif |
| 1308 | |
Simon Kelley | 9d6918d | 2017-10-13 17:55:09 +0100 | [diff] [blame] | 1309 | int local_bind(int fd, union mysockaddr *addr, char *intname, unsigned int ifindex, int is_tcp) |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1310 | { |
| 1311 | union mysockaddr addr_copy = *addr; |
Simon Kelley | a2a7e04 | 2020-12-12 23:26:45 +0000 | [diff] [blame] | 1312 | unsigned short port; |
Simon Kelley | 4a8c098 | 2021-03-26 21:19:39 +0000 | [diff] [blame] | 1313 | int tries = 1; |
| 1314 | unsigned short ports_avail = 1; |
| 1315 | |
Simon Kelley | a2a7e04 | 2020-12-12 23:26:45 +0000 | [diff] [blame] | 1316 | if (addr_copy.sa.sa_family == AF_INET) |
| 1317 | port = addr_copy.in.sin_port; |
| 1318 | else |
| 1319 | port = addr_copy.in6.sin6_port; |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1320 | |
| 1321 | /* cannot set source _port_ for TCP connections. */ |
| 1322 | if (is_tcp) |
Simon Kelley | a2a7e04 | 2020-12-12 23:26:45 +0000 | [diff] [blame] | 1323 | port = 0; |
Simon Kelley | 4a8c098 | 2021-03-26 21:19:39 +0000 | [diff] [blame] | 1324 | else if (port == 0 && daemon->max_port != 0) |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1325 | { |
Simon Kelley | 4a8c098 | 2021-03-26 21:19:39 +0000 | [diff] [blame] | 1326 | /* Bind a random port within the range given by min-port and max-port if either |
| 1327 | or both are set. Otherwise use the OS's random ephemeral port allocation by |
| 1328 | leaving port == 0 and tries == 1 */ |
| 1329 | ports_avail = daemon->max_port - daemon->min_port + 1; |
Simon Kelley | a2a7e04 | 2020-12-12 23:26:45 +0000 | [diff] [blame] | 1330 | tries = ports_avail < 30 ? 3 * ports_avail : 100; |
Simon Kelley | 4a8c098 | 2021-03-26 21:19:39 +0000 | [diff] [blame] | 1331 | port = htons(daemon->min_port + (rand16() % ports_avail)); |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1332 | } |
| 1333 | |
Simon Kelley | 4a8c098 | 2021-03-26 21:19:39 +0000 | [diff] [blame] | 1334 | while (1) |
Simon Kelley | a2a7e04 | 2020-12-12 23:26:45 +0000 | [diff] [blame] | 1335 | { |
Simon Kelley | 4a8c098 | 2021-03-26 21:19:39 +0000 | [diff] [blame] | 1336 | /* elide bind() call if it's to port 0, address 0 */ |
Simon Kelley | a2a7e04 | 2020-12-12 23:26:45 +0000 | [diff] [blame] | 1337 | if (addr_copy.sa.sa_family == AF_INET) |
Simon Kelley | a2a7e04 | 2020-12-12 23:26:45 +0000 | [diff] [blame] | 1338 | { |
Simon Kelley | 4a8c098 | 2021-03-26 21:19:39 +0000 | [diff] [blame] | 1339 | if (port == 0 && addr_copy.in.sin_addr.s_addr == 0) |
| 1340 | break; |
| 1341 | addr_copy.in.sin_port = port; |
| 1342 | } |
| 1343 | else |
| 1344 | { |
| 1345 | if (port == 0 && IN6_IS_ADDR_UNSPECIFIED(&addr_copy.in6.sin6_addr)) |
| 1346 | break; |
| 1347 | addr_copy.in6.sin6_port = port; |
Simon Kelley | a2a7e04 | 2020-12-12 23:26:45 +0000 | [diff] [blame] | 1348 | } |
| 1349 | |
Simon Kelley | 4a8c098 | 2021-03-26 21:19:39 +0000 | [diff] [blame] | 1350 | if (bind(fd, (struct sockaddr *)&addr_copy, sa_len(&addr_copy)) != -1) |
| 1351 | break; |
Simon Kelley | a2a7e04 | 2020-12-12 23:26:45 +0000 | [diff] [blame] | 1352 | |
Simon Kelley | 4a8c098 | 2021-03-26 21:19:39 +0000 | [diff] [blame] | 1353 | if (errno != EADDRINUSE && errno != EACCES) |
| 1354 | return 0; |
Simon Kelley | a2a7e04 | 2020-12-12 23:26:45 +0000 | [diff] [blame] | 1355 | |
Simon Kelley | 4a8c098 | 2021-03-26 21:19:39 +0000 | [diff] [blame] | 1356 | if (--tries == 0) |
| 1357 | return 0; |
| 1358 | |
| 1359 | port = htons(daemon->min_port + (rand16() % ports_avail)); |
| 1360 | } |
Simon Kelley | 9d6918d | 2017-10-13 17:55:09 +0100 | [diff] [blame] | 1361 | |
| 1362 | if (!is_tcp && ifindex > 0) |
| 1363 | { |
| 1364 | #if defined(IP_UNICAST_IF) |
| 1365 | if (addr_copy.sa.sa_family == AF_INET) |
| 1366 | { |
| 1367 | uint32_t ifindex_opt = htonl(ifindex); |
| 1368 | return setsockopt(fd, IPPROTO_IP, IP_UNICAST_IF, &ifindex_opt, sizeof(ifindex_opt)) == 0; |
| 1369 | } |
| 1370 | #endif |
Simon Kelley | ee87504 | 2018-10-23 22:10:17 +0100 | [diff] [blame] | 1371 | #if defined (IPV6_UNICAST_IF) |
Simon Kelley | 9d6918d | 2017-10-13 17:55:09 +0100 | [diff] [blame] | 1372 | if (addr_copy.sa.sa_family == AF_INET6) |
| 1373 | { |
| 1374 | uint32_t ifindex_opt = htonl(ifindex); |
| 1375 | return setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_IF, &ifindex_opt, sizeof(ifindex_opt)) == 0; |
| 1376 | } |
| 1377 | #endif |
| 1378 | } |
| 1379 | |
Kevin Darbyshire-Bryant | 70c50ef | 2020-03-06 10:31:15 +0000 | [diff] [blame] | 1380 | (void)intname; /* suppress potential unused warning */ |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1381 | #if defined(SO_BINDTODEVICE) |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 1382 | if (intname[0] != 0 && |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 1383 | setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, intname, IF_NAMESIZE) == -1) |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1384 | return 0; |
| 1385 | #endif |
| 1386 | |
| 1387 | return 1; |
| 1388 | } |
| 1389 | |
Simon Kelley | 74d4fcd | 2021-03-15 21:59:51 +0000 | [diff] [blame] | 1390 | static struct serverfd *allocate_sfd(union mysockaddr *addr, char *intname, unsigned int ifindex) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1391 | { |
| 1392 | struct serverfd *sfd; |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1393 | int errsave; |
Simon Kelley | e83915d | 2018-04-10 21:27:26 +0100 | [diff] [blame] | 1394 | int opt = 1; |
| 1395 | |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1396 | /* when using random ports, servers which would otherwise use |
Simon Kelley | 74d4fcd | 2021-03-15 21:59:51 +0000 | [diff] [blame] | 1397 | the INADDR_ANY/port0 socket have sfd set to NULL, this is |
| 1398 | anything without an explictly set source port. */ |
| 1399 | if (!daemon->osport) |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1400 | { |
| 1401 | errno = 0; |
| 1402 | |
| 1403 | if (addr->sa.sa_family == AF_INET && |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1404 | addr->in.sin_port == htons(0)) |
| 1405 | return NULL; |
| 1406 | |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1407 | if (addr->sa.sa_family == AF_INET6 && |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1408 | addr->in6.sin6_port == htons(0)) |
| 1409 | return NULL; |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1410 | } |
Beniamino Galvani | 2675f20 | 2016-08-28 20:44:05 +0100 | [diff] [blame] | 1411 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1412 | /* may have a suitable one already */ |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1413 | for (sfd = daemon->sfds; sfd; sfd = sfd->next ) |
Simon Kelley | 74d4fcd | 2021-03-15 21:59:51 +0000 | [diff] [blame] | 1414 | if (ifindex == sfd->ifindex && |
| 1415 | sockaddr_isequal(&sfd->source_addr, addr) && |
| 1416 | strcmp(intname, sfd->interface) == 0) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1417 | return sfd; |
| 1418 | |
| 1419 | /* need to make a new one. */ |
| 1420 | errno = ENOMEM; /* in case malloc fails. */ |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1421 | if (!(sfd = whine_malloc(sizeof(struct serverfd)))) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1422 | return NULL; |
| 1423 | |
| 1424 | if ((sfd->fd = socket(addr->sa.sa_family, SOCK_DGRAM, 0)) == -1) |
| 1425 | { |
| 1426 | free(sfd); |
| 1427 | return NULL; |
| 1428 | } |
Simon Kelley | e83915d | 2018-04-10 21:27:26 +0100 | [diff] [blame] | 1429 | |
| 1430 | if ((addr->sa.sa_family == AF_INET6 && setsockopt(sfd->fd, IPPROTO_IPV6, IPV6_V6ONLY, &opt, sizeof(opt)) == -1) || |
| 1431 | !local_bind(sfd->fd, addr, intname, ifindex, 0) || !fix_fd(sfd->fd)) |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1432 | { |
Simon Kelley | e83915d | 2018-04-10 21:27:26 +0100 | [diff] [blame] | 1433 | errsave = errno; /* save error from bind/setsockopt. */ |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1434 | close(sfd->fd); |
| 1435 | free(sfd); |
| 1436 | errno = errsave; |
| 1437 | return NULL; |
| 1438 | } |
Beniamino Galvani | 2675f20 | 2016-08-28 20:44:05 +0100 | [diff] [blame] | 1439 | |
Petr Menšík | 47b45b2 | 2018-08-15 18:17:00 +0200 | [diff] [blame] | 1440 | safe_strncpy(sfd->interface, intname, sizeof(sfd->interface)); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1441 | sfd->source_addr = *addr; |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1442 | sfd->next = daemon->sfds; |
Beniamino Galvani | 2675f20 | 2016-08-28 20:44:05 +0100 | [diff] [blame] | 1443 | sfd->ifindex = ifindex; |
Simon Kelley | 4441cf7 | 2018-04-10 21:39:54 +0100 | [diff] [blame] | 1444 | sfd->preallocated = 0; |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1445 | daemon->sfds = sfd; |
Beniamino Galvani | 2675f20 | 2016-08-28 20:44:05 +0100 | [diff] [blame] | 1446 | |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1447 | return sfd; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1448 | } |
| 1449 | |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1450 | /* create upstream sockets during startup, before root is dropped which may be needed |
| 1451 | this allows query_port to be a low port and interface binding */ |
| 1452 | void pre_allocate_sfds(void) |
| 1453 | { |
| 1454 | struct server *srv; |
Simon Kelley | 4441cf7 | 2018-04-10 21:39:54 +0100 | [diff] [blame] | 1455 | struct serverfd *sfd; |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1456 | |
| 1457 | if (daemon->query_port != 0) |
| 1458 | { |
| 1459 | union mysockaddr addr; |
| 1460 | memset(&addr, 0, sizeof(addr)); |
| 1461 | addr.in.sin_family = AF_INET; |
| 1462 | addr.in.sin_addr.s_addr = INADDR_ANY; |
| 1463 | addr.in.sin_port = htons(daemon->query_port); |
| 1464 | #ifdef HAVE_SOCKADDR_SA_LEN |
| 1465 | addr.in.sin_len = sizeof(struct sockaddr_in); |
| 1466 | #endif |
Simon Kelley | 74d4fcd | 2021-03-15 21:59:51 +0000 | [diff] [blame] | 1467 | if ((sfd = allocate_sfd(&addr, "", 0))) |
Simon Kelley | 4441cf7 | 2018-04-10 21:39:54 +0100 | [diff] [blame] | 1468 | sfd->preallocated = 1; |
Simon Kelley | ee87504 | 2018-10-23 22:10:17 +0100 | [diff] [blame] | 1469 | |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1470 | memset(&addr, 0, sizeof(addr)); |
| 1471 | addr.in6.sin6_family = AF_INET6; |
| 1472 | addr.in6.sin6_addr = in6addr_any; |
| 1473 | addr.in6.sin6_port = htons(daemon->query_port); |
| 1474 | #ifdef HAVE_SOCKADDR_SA_LEN |
| 1475 | addr.in6.sin6_len = sizeof(struct sockaddr_in6); |
| 1476 | #endif |
Simon Kelley | 74d4fcd | 2021-03-15 21:59:51 +0000 | [diff] [blame] | 1477 | if ((sfd = allocate_sfd(&addr, "", 0))) |
Simon Kelley | 4441cf7 | 2018-04-10 21:39:54 +0100 | [diff] [blame] | 1478 | sfd->preallocated = 1; |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1479 | } |
| 1480 | |
| 1481 | for (srv = daemon->servers; srv; srv = srv->next) |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 1482 | if (!(srv->flags & (SERV_LITERAL_ADDRESS | SERV_NO_ADDR | SERV_USE_RESOLV | SERV_NO_REBIND)) && |
Simon Kelley | 74d4fcd | 2021-03-15 21:59:51 +0000 | [diff] [blame] | 1483 | !allocate_sfd(&srv->source_addr, srv->interface, srv->ifindex) && |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1484 | errno != 0 && |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1485 | option_bool(OPT_NOWILD)) |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1486 | { |
Petr Mensik | 51cdd1a | 2019-07-04 20:28:08 +0200 | [diff] [blame] | 1487 | (void)prettyprint_addr(&srv->source_addr, daemon->namebuff); |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 1488 | if (srv->interface[0] != 0) |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1489 | { |
| 1490 | strcat(daemon->namebuff, " "); |
| 1491 | strcat(daemon->namebuff, srv->interface); |
| 1492 | } |
| 1493 | die(_("failed to bind server socket for %s: %s"), |
| 1494 | daemon->namebuff, EC_BADNET); |
| 1495 | } |
| 1496 | } |
| 1497 | |
Simon Kelley | d68c2ca | 2014-02-18 22:30:30 +0000 | [diff] [blame] | 1498 | void mark_servers(int flag) |
| 1499 | { |
| 1500 | struct server *serv; |
| 1501 | |
| 1502 | /* mark everything with argument flag */ |
| 1503 | for (serv = daemon->servers; serv; serv = serv->next) |
Simon Kelley | 40766e5 | 2014-07-29 16:52:00 +0100 | [diff] [blame] | 1504 | { |
| 1505 | if (serv->flags & flag) |
Simon Kelley | b5ea1cc | 2014-07-29 16:34:14 +0100 | [diff] [blame] | 1506 | serv->flags |= SERV_MARK; |
| 1507 | #ifdef HAVE_LOOP |
Simon Kelley | 40766e5 | 2014-07-29 16:52:00 +0100 | [diff] [blame] | 1508 | /* Give looped servers another chance */ |
| 1509 | serv->flags &= ~SERV_LOOP; |
Simon Kelley | b5ea1cc | 2014-07-29 16:34:14 +0100 | [diff] [blame] | 1510 | #endif |
Simon Kelley | 40766e5 | 2014-07-29 16:52:00 +0100 | [diff] [blame] | 1511 | } |
Simon Kelley | d68c2ca | 2014-02-18 22:30:30 +0000 | [diff] [blame] | 1512 | } |
| 1513 | |
| 1514 | void cleanup_servers(void) |
| 1515 | { |
| 1516 | struct server *serv, *tmp, **up; |
| 1517 | |
| 1518 | /* unlink and free anything still marked. */ |
| 1519 | for (serv = daemon->servers, up = &daemon->servers; serv; serv = tmp) |
| 1520 | { |
| 1521 | tmp = serv->next; |
| 1522 | if (serv->flags & SERV_MARK) |
| 1523 | { |
| 1524 | server_gone(serv); |
| 1525 | *up = serv->next; |
| 1526 | if (serv->domain) |
| 1527 | free(serv->domain); |
| 1528 | free(serv); |
| 1529 | } |
| 1530 | else |
| 1531 | up = &serv->next; |
| 1532 | } |
Simon Kelley | b5ea1cc | 2014-07-29 16:34:14 +0100 | [diff] [blame] | 1533 | |
| 1534 | #ifdef HAVE_LOOP |
| 1535 | /* Now we have a new set of servers, test for loops. */ |
| 1536 | loop_send_probes(); |
| 1537 | #endif |
Simon Kelley | d68c2ca | 2014-02-18 22:30:30 +0000 | [diff] [blame] | 1538 | } |
| 1539 | |
| 1540 | void add_update_server(int flags, |
| 1541 | union mysockaddr *addr, |
| 1542 | union mysockaddr *source_addr, |
| 1543 | const char *interface, |
| 1544 | const char *domain) |
| 1545 | { |
Simon Kelley | 7b1eae4 | 2014-02-20 13:43:28 +0000 | [diff] [blame] | 1546 | struct server *serv, *next = NULL; |
| 1547 | char *domain_str = NULL; |
Simon Kelley | d68c2ca | 2014-02-18 22:30:30 +0000 | [diff] [blame] | 1548 | |
| 1549 | /* See if there is a suitable candidate, and unmark */ |
| 1550 | for (serv = daemon->servers; serv; serv = serv->next) |
| 1551 | if (serv->flags & SERV_MARK) |
| 1552 | { |
| 1553 | if (domain) |
| 1554 | { |
| 1555 | if (!(serv->flags & SERV_HAS_DOMAIN) || !hostname_isequal(domain, serv->domain)) |
| 1556 | continue; |
| 1557 | } |
| 1558 | else |
| 1559 | { |
| 1560 | if (serv->flags & SERV_HAS_DOMAIN) |
| 1561 | continue; |
| 1562 | } |
| 1563 | |
Simon Kelley | d68c2ca | 2014-02-18 22:30:30 +0000 | [diff] [blame] | 1564 | break; |
| 1565 | } |
Simon Kelley | 7b1eae4 | 2014-02-20 13:43:28 +0000 | [diff] [blame] | 1566 | |
| 1567 | if (serv) |
| 1568 | { |
| 1569 | domain_str = serv->domain; |
| 1570 | next = serv->next; |
| 1571 | } |
| 1572 | else if ((serv = whine_malloc(sizeof (struct server)))) |
Simon Kelley | d68c2ca | 2014-02-18 22:30:30 +0000 | [diff] [blame] | 1573 | { |
| 1574 | /* Not found, create a new one. */ |
Simon Kelley | 7b1eae4 | 2014-02-20 13:43:28 +0000 | [diff] [blame] | 1575 | if (domain && !(domain_str = whine_malloc(strlen(domain)+1))) |
Simon Kelley | d68c2ca | 2014-02-18 22:30:30 +0000 | [diff] [blame] | 1576 | { |
| 1577 | free(serv); |
| 1578 | serv = NULL; |
| 1579 | } |
| 1580 | else |
| 1581 | { |
| 1582 | struct server *s; |
| 1583 | /* Add to the end of the chain, for order */ |
| 1584 | if (!daemon->servers) |
| 1585 | daemon->servers = serv; |
| 1586 | else |
| 1587 | { |
| 1588 | for (s = daemon->servers; s->next; s = s->next); |
| 1589 | s->next = serv; |
| 1590 | } |
| 1591 | if (domain) |
Simon Kelley | 7b1eae4 | 2014-02-20 13:43:28 +0000 | [diff] [blame] | 1592 | strcpy(domain_str, domain); |
Simon Kelley | d68c2ca | 2014-02-18 22:30:30 +0000 | [diff] [blame] | 1593 | } |
| 1594 | } |
| 1595 | |
| 1596 | if (serv) |
| 1597 | { |
Simon Kelley | 7b1eae4 | 2014-02-20 13:43:28 +0000 | [diff] [blame] | 1598 | memset(serv, 0, sizeof(struct server)); |
Simon Kelley | d68c2ca | 2014-02-18 22:30:30 +0000 | [diff] [blame] | 1599 | serv->flags = flags; |
Simon Kelley | 7b1eae4 | 2014-02-20 13:43:28 +0000 | [diff] [blame] | 1600 | serv->domain = domain_str; |
| 1601 | serv->next = next; |
Simon Kelley | d68c2ca | 2014-02-18 22:30:30 +0000 | [diff] [blame] | 1602 | serv->queries = serv->failed_queries = 0; |
Simon Kelley | b5ea1cc | 2014-07-29 16:34:14 +0100 | [diff] [blame] | 1603 | #ifdef HAVE_LOOP |
| 1604 | serv->uid = rand32(); |
| 1605 | #endif |
| 1606 | |
Simon Kelley | d68c2ca | 2014-02-18 22:30:30 +0000 | [diff] [blame] | 1607 | if (domain) |
| 1608 | serv->flags |= SERV_HAS_DOMAIN; |
| 1609 | |
| 1610 | if (interface) |
Petr Menšík | 47b45b2 | 2018-08-15 18:17:00 +0200 | [diff] [blame] | 1611 | safe_strncpy(serv->interface, interface, sizeof(serv->interface)); |
Simon Kelley | 7b1eae4 | 2014-02-20 13:43:28 +0000 | [diff] [blame] | 1612 | if (addr) |
| 1613 | serv->addr = *addr; |
| 1614 | if (source_addr) |
| 1615 | serv->source_addr = *source_addr; |
Simon Kelley | d68c2ca | 2014-02-18 22:30:30 +0000 | [diff] [blame] | 1616 | } |
| 1617 | } |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1618 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1619 | void check_servers(void) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1620 | { |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1621 | struct irec *iface; |
Simon Kelley | 7bcca00 | 2014-02-19 17:45:17 +0000 | [diff] [blame] | 1622 | struct server *serv; |
Beniamino Galvani | 2675f20 | 2016-08-28 20:44:05 +0100 | [diff] [blame] | 1623 | struct serverfd *sfd, *tmp, **up; |
Simon Kelley | b970260 | 2016-05-03 22:34:06 +0100 | [diff] [blame] | 1624 | int port = 0, count; |
Hannu Nyman | 3e2496f | 2017-02-11 13:44:08 +0000 | [diff] [blame] | 1625 | int locals = 0; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1626 | |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 1627 | /* interface may be new since startup */ |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1628 | if (!option_bool(OPT_NOWILD)) |
Simon Kelley | 115ac3e | 2013-05-20 11:28:32 +0100 | [diff] [blame] | 1629 | enumerate_interfaces(0); |
Simon Kelley | 4441cf7 | 2018-04-10 21:39:54 +0100 | [diff] [blame] | 1630 | |
| 1631 | /* don't garbage collect pre-allocated sfds. */ |
Beniamino Galvani | 2675f20 | 2016-08-28 20:44:05 +0100 | [diff] [blame] | 1632 | for (sfd = daemon->sfds; sfd; sfd = sfd->next) |
Simon Kelley | 4441cf7 | 2018-04-10 21:39:54 +0100 | [diff] [blame] | 1633 | sfd->used = sfd->preallocated; |
Beniamino Galvani | 2675f20 | 2016-08-28 20:44:05 +0100 | [diff] [blame] | 1634 | |
Simon Kelley | b970260 | 2016-05-03 22:34:06 +0100 | [diff] [blame] | 1635 | for (count = 0, serv = daemon->servers; serv; serv = serv->next) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1636 | { |
Simon Kelley | 367341f | 2016-01-12 15:58:23 +0000 | [diff] [blame] | 1637 | if (!(serv->flags & (SERV_LITERAL_ADDRESS | SERV_NO_ADDR | SERV_USE_RESOLV | SERV_NO_REBIND))) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1638 | { |
Simon Kelley | 14ffa07 | 2016-04-25 16:36:44 +0100 | [diff] [blame] | 1639 | /* Init edns_pktsz for newly created server records. */ |
| 1640 | if (serv->edns_pktsz == 0) |
| 1641 | serv->edns_pktsz = daemon->edns_pktsz; |
| 1642 | |
Simon Kelley | 367341f | 2016-01-12 15:58:23 +0000 | [diff] [blame] | 1643 | #ifdef HAVE_DNSSEC |
Simon Kelley | 92be34a | 2016-01-16 18:39:54 +0000 | [diff] [blame] | 1644 | if (option_bool(OPT_DNSSEC_VALID)) |
| 1645 | { |
Simon Kelley | a49c5c2 | 2017-10-10 22:04:59 +0100 | [diff] [blame] | 1646 | if (!(serv->flags & SERV_FOR_NODOTS)) |
| 1647 | serv->flags |= SERV_DO_DNSSEC; |
| 1648 | |
| 1649 | /* Disable DNSSEC validation when using server=/domain/.... servers |
| 1650 | unless there's a configured trust anchor. */ |
Simon Kelley | 92be34a | 2016-01-16 18:39:54 +0000 | [diff] [blame] | 1651 | if (serv->flags & SERV_HAS_DOMAIN) |
| 1652 | { |
| 1653 | struct ds_config *ds; |
| 1654 | char *domain = serv->domain; |
| 1655 | |
| 1656 | /* .example.com is valid */ |
| 1657 | while (*domain == '.') |
| 1658 | domain++; |
| 1659 | |
| 1660 | for (ds = daemon->ds; ds; ds = ds->next) |
| 1661 | if (ds->name[0] != 0 && hostname_isequal(domain, ds->name)) |
| 1662 | break; |
| 1663 | |
| 1664 | if (!ds) |
| 1665 | serv->flags &= ~SERV_DO_DNSSEC; |
| 1666 | } |
Simon Kelley | 367341f | 2016-01-12 15:58:23 +0000 | [diff] [blame] | 1667 | } |
| 1668 | #endif |
| 1669 | |
| 1670 | port = prettyprint_addr(&serv->addr, daemon->namebuff); |
| 1671 | |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 1672 | /* 0.0.0.0 is nothing, the stack treats it like 127.0.0.1 */ |
Simon Kelley | 7bcca00 | 2014-02-19 17:45:17 +0000 | [diff] [blame] | 1673 | if (serv->addr.sa.sa_family == AF_INET && |
| 1674 | serv->addr.in.sin_addr.s_addr == 0) |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 1675 | { |
Simon Kelley | 7bcca00 | 2014-02-19 17:45:17 +0000 | [diff] [blame] | 1676 | serv->flags |= SERV_MARK; |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 1677 | continue; |
| 1678 | } |
| 1679 | |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 1680 | for (iface = daemon->interfaces; iface; iface = iface->next) |
Simon Kelley | 7bcca00 | 2014-02-19 17:45:17 +0000 | [diff] [blame] | 1681 | if (sockaddr_isequal(&serv->addr, &iface->addr)) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1682 | break; |
| 1683 | if (iface) |
| 1684 | { |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 1685 | my_syslog(LOG_WARNING, _("ignoring nameserver %s - local interface"), daemon->namebuff); |
Simon Kelley | 7bcca00 | 2014-02-19 17:45:17 +0000 | [diff] [blame] | 1686 | serv->flags |= SERV_MARK; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1687 | continue; |
| 1688 | } |
| 1689 | |
| 1690 | /* Do we need a socket set? */ |
Simon Kelley | 7bcca00 | 2014-02-19 17:45:17 +0000 | [diff] [blame] | 1691 | if (!serv->sfd && |
Simon Kelley | 74d4fcd | 2021-03-15 21:59:51 +0000 | [diff] [blame] | 1692 | !(serv->sfd = allocate_sfd(&serv->source_addr, serv->interface, serv->ifindex)) && |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1693 | errno != 0) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1694 | { |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 1695 | my_syslog(LOG_WARNING, |
| 1696 | _("ignoring nameserver %s - cannot make/bind socket: %s"), |
| 1697 | daemon->namebuff, strerror(errno)); |
Simon Kelley | 7bcca00 | 2014-02-19 17:45:17 +0000 | [diff] [blame] | 1698 | serv->flags |= SERV_MARK; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1699 | continue; |
| 1700 | } |
Simon Kelley | 16800ea | 2016-08-30 23:07:06 +0100 | [diff] [blame] | 1701 | |
| 1702 | if (serv->sfd) |
| 1703 | serv->sfd->used = 1; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1704 | } |
| 1705 | |
Simon Kelley | 979fe86 | 2015-03-19 22:50:22 +0000 | [diff] [blame] | 1706 | if (!(serv->flags & SERV_NO_REBIND) && !(serv->flags & SERV_LITERAL_ADDRESS)) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1707 | { |
Simon Kelley | b970260 | 2016-05-03 22:34:06 +0100 | [diff] [blame] | 1708 | if (++count > SERVERS_LOGGED) |
| 1709 | continue; |
| 1710 | |
Simon Kelley | 7bcca00 | 2014-02-19 17:45:17 +0000 | [diff] [blame] | 1711 | if (serv->flags & (SERV_HAS_DOMAIN | SERV_FOR_NODOTS | SERV_USE_RESOLV)) |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 1712 | { |
Simon Kelley | 367341f | 2016-01-12 15:58:23 +0000 | [diff] [blame] | 1713 | char *s1, *s2, *s3 = ""; |
| 1714 | #ifdef HAVE_DNSSEC |
| 1715 | if (option_bool(OPT_DNSSEC_VALID) && !(serv->flags & SERV_DO_DNSSEC)) |
| 1716 | s3 = _("(no DNSSEC)"); |
| 1717 | #endif |
Simon Kelley | 7bcca00 | 2014-02-19 17:45:17 +0000 | [diff] [blame] | 1718 | if (!(serv->flags & SERV_HAS_DOMAIN)) |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 1719 | s1 = _("unqualified"), s2 = _("names"); |
Simon Kelley | d9603ef | 2020-01-26 18:13:35 +0000 | [diff] [blame] | 1720 | else if (strlen(serv->domain) == 0) |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 1721 | s1 = _("default"), s2 = ""; |
| 1722 | else |
Simon Kelley | 7bcca00 | 2014-02-19 17:45:17 +0000 | [diff] [blame] | 1723 | s1 = _("domain"), s2 = serv->domain; |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 1724 | |
Simon Kelley | 7bcca00 | 2014-02-19 17:45:17 +0000 | [diff] [blame] | 1725 | if (serv->flags & SERV_NO_ADDR) |
Hannu Nyman | 3e2496f | 2017-02-11 13:44:08 +0000 | [diff] [blame] | 1726 | { |
| 1727 | count--; |
| 1728 | if (++locals <= LOCALS_LOGGED) |
Simon Kelley | 8bd28a8 | 2019-03-01 15:00:12 +0000 | [diff] [blame] | 1729 | my_syslog(LOG_INFO, _("using only locally-known addresses for %s %s"), s1, s2); |
Hannu Nyman | 3e2496f | 2017-02-11 13:44:08 +0000 | [diff] [blame] | 1730 | } |
Simon Kelley | 7bcca00 | 2014-02-19 17:45:17 +0000 | [diff] [blame] | 1731 | else if (serv->flags & SERV_USE_RESOLV) |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 1732 | my_syslog(LOG_INFO, _("using standard nameservers for %s %s"), s1, s2); |
Simon Kelley | 979fe86 | 2015-03-19 22:50:22 +0000 | [diff] [blame] | 1733 | else |
Simon Kelley | 367341f | 2016-01-12 15:58:23 +0000 | [diff] [blame] | 1734 | my_syslog(LOG_INFO, _("using nameserver %s#%d for %s %s %s"), daemon->namebuff, port, s1, s2, s3); |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 1735 | } |
Simon Kelley | b5ea1cc | 2014-07-29 16:34:14 +0100 | [diff] [blame] | 1736 | #ifdef HAVE_LOOP |
| 1737 | else if (serv->flags & SERV_LOOP) |
| 1738 | my_syslog(LOG_INFO, _("NOT using nameserver %s#%d - query loop detected"), daemon->namebuff, port); |
| 1739 | #endif |
Simon Kelley | 7bcca00 | 2014-02-19 17:45:17 +0000 | [diff] [blame] | 1740 | else if (serv->interface[0] != 0) |
| 1741 | my_syslog(LOG_INFO, _("using nameserver %s#%d(via %s)"), daemon->namebuff, port, serv->interface); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1742 | else |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 1743 | my_syslog(LOG_INFO, _("using nameserver %s#%d"), daemon->namebuff, port); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1744 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1745 | } |
Simon Kelley | b970260 | 2016-05-03 22:34:06 +0100 | [diff] [blame] | 1746 | |
Hannu Nyman | 3e2496f | 2017-02-11 13:44:08 +0000 | [diff] [blame] | 1747 | if (locals > LOCALS_LOGGED) |
| 1748 | my_syslog(LOG_INFO, _("using %d more local addresses"), locals - LOCALS_LOGGED); |
Simon Kelley | b970260 | 2016-05-03 22:34:06 +0100 | [diff] [blame] | 1749 | if (count - 1 > SERVERS_LOGGED) |
| 1750 | my_syslog(LOG_INFO, _("using %d more nameservers"), count - SERVERS_LOGGED - 1); |
Simon Kelley | 7bcca00 | 2014-02-19 17:45:17 +0000 | [diff] [blame] | 1751 | |
Beniamino Galvani | 2675f20 | 2016-08-28 20:44:05 +0100 | [diff] [blame] | 1752 | /* Remove unused sfds */ |
| 1753 | for (sfd = daemon->sfds, up = &daemon->sfds; sfd; sfd = tmp) |
| 1754 | { |
| 1755 | tmp = sfd->next; |
| 1756 | if (!sfd->used) |
| 1757 | { |
| 1758 | *up = sfd->next; |
| 1759 | close(sfd->fd); |
| 1760 | free(sfd); |
| 1761 | } |
| 1762 | else |
| 1763 | up = &sfd->next; |
| 1764 | } |
| 1765 | |
Simon Kelley | 7bcca00 | 2014-02-19 17:45:17 +0000 | [diff] [blame] | 1766 | cleanup_servers(); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1767 | } |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 1768 | |
| 1769 | /* Return zero if no servers found, in that case we keep polling. |
| 1770 | This is a protection against an update-time/write race on resolv.conf */ |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1771 | int reload_servers(char *fname) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1772 | { |
| 1773 | FILE *f; |
| 1774 | char *line; |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 1775 | int gotone = 0; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1776 | |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 1777 | /* buff happens to be MAXDNAME long... */ |
| 1778 | if (!(f = fopen(fname, "r"))) |
| 1779 | { |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 1780 | my_syslog(LOG_ERR, _("failed to read %s: %s"), fname, strerror(errno)); |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 1781 | return 0; |
| 1782 | } |
Simon Kelley | d68c2ca | 2014-02-18 22:30:30 +0000 | [diff] [blame] | 1783 | |
| 1784 | mark_servers(SERV_FROM_RESOLV); |
| 1785 | |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 1786 | while ((line = fgets(daemon->namebuff, MAXDNAME, f))) |
| 1787 | { |
| 1788 | union mysockaddr addr, source_addr; |
| 1789 | char *token = strtok(line, " \t\n\r"); |
| 1790 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1791 | if (!token) |
| 1792 | continue; |
| 1793 | if (strcmp(token, "nameserver") != 0 && strcmp(token, "server") != 0) |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 1794 | continue; |
| 1795 | if (!(token = strtok(NULL, " \t\n\r"))) |
| 1796 | continue; |
| 1797 | |
| 1798 | memset(&addr, 0, sizeof(addr)); |
| 1799 | memset(&source_addr, 0, sizeof(source_addr)); |
| 1800 | |
| 1801 | if ((addr.in.sin_addr.s_addr = inet_addr(token)) != (in_addr_t) -1) |
| 1802 | { |
| 1803 | #ifdef HAVE_SOCKADDR_SA_LEN |
| 1804 | source_addr.in.sin_len = addr.in.sin_len = sizeof(source_addr.in); |
| 1805 | #endif |
| 1806 | source_addr.in.sin_family = addr.in.sin_family = AF_INET; |
| 1807 | addr.in.sin_port = htons(NAMESERVER_PORT); |
| 1808 | source_addr.in.sin_addr.s_addr = INADDR_ANY; |
| 1809 | source_addr.in.sin_port = htons(daemon->query_port); |
| 1810 | } |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 1811 | else |
| 1812 | { |
| 1813 | int scope_index = 0; |
| 1814 | char *scope_id = strchr(token, '%'); |
| 1815 | |
| 1816 | if (scope_id) |
| 1817 | { |
| 1818 | *(scope_id++) = 0; |
| 1819 | scope_index = if_nametoindex(scope_id); |
| 1820 | } |
| 1821 | |
| 1822 | if (inet_pton(AF_INET6, token, &addr.in6.sin6_addr) > 0) |
| 1823 | { |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 1824 | #ifdef HAVE_SOCKADDR_SA_LEN |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 1825 | source_addr.in6.sin6_len = addr.in6.sin6_len = sizeof(source_addr.in6); |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 1826 | #endif |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 1827 | source_addr.in6.sin6_family = addr.in6.sin6_family = AF_INET6; |
| 1828 | source_addr.in6.sin6_flowinfo = addr.in6.sin6_flowinfo = 0; |
| 1829 | addr.in6.sin6_port = htons(NAMESERVER_PORT); |
| 1830 | addr.in6.sin6_scope_id = scope_index; |
| 1831 | source_addr.in6.sin6_addr = in6addr_any; |
| 1832 | source_addr.in6.sin6_port = htons(daemon->query_port); |
| 1833 | source_addr.in6.sin6_scope_id = 0; |
| 1834 | } |
| 1835 | else |
| 1836 | continue; |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 1837 | } |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 1838 | |
Simon Kelley | d68c2ca | 2014-02-18 22:30:30 +0000 | [diff] [blame] | 1839 | add_update_server(SERV_FROM_RESOLV, &addr, &source_addr, NULL, NULL); |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 1840 | gotone = 1; |
| 1841 | } |
| 1842 | |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 1843 | fclose(f); |
Simon Kelley | d68c2ca | 2014-02-18 22:30:30 +0000 | [diff] [blame] | 1844 | cleanup_servers(); |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 1845 | |
| 1846 | return gotone; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1847 | } |
| 1848 | |
Simon Kelley | 1ee9be4 | 2013-12-09 16:50:19 +0000 | [diff] [blame] | 1849 | /* Called when addresses are added or deleted from an interface */ |
| 1850 | void newaddress(time_t now) |
| 1851 | { |
| 1852 | (void)now; |
| 1853 | |
Simon Kelley | 89b12ed | 2014-03-06 13:27:57 +0000 | [diff] [blame] | 1854 | if (option_bool(OPT_CLEVERBIND) || option_bool(OPT_LOCAL_SERVICE) || |
| 1855 | daemon->doing_dhcp6 || daemon->relay6 || daemon->doing_ra) |
Simon Kelley | 1ee9be4 | 2013-12-09 16:50:19 +0000 | [diff] [blame] | 1856 | enumerate_interfaces(0); |
| 1857 | |
| 1858 | if (option_bool(OPT_CLEVERBIND)) |
| 1859 | create_bound_listeners(0); |
| 1860 | |
| 1861 | #ifdef HAVE_DHCP6 |
| 1862 | if (daemon->doing_dhcp6 || daemon->relay6 || daemon->doing_ra) |
| 1863 | join_multicast(0); |
| 1864 | |
| 1865 | if (daemon->doing_dhcp6 || daemon->doing_ra) |
| 1866 | dhcp_construct_contexts(now); |
| 1867 | |
| 1868 | if (daemon->doing_dhcp6) |
| 1869 | lease_find_interfaces(now); |
| 1870 | #endif |
| 1871 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1872 | |
| 1873 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1874 | |
| 1875 | |
| 1876 | |