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