Tarun Kundu | 12e3b2e | 2024-08-15 16:16:53 -0700 | [diff] [blame] | 1 | /* dnsmasq is Copyright (c) 2000-2024 Simon Kelley |
Simon Kelley | 2bb73af | 2013-04-24 17:38:19 +0100 | [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 |
| 5 | the Free Software Foundation; version 2 dated June, 1991, or |
| 6 | (at your option) version 3 dated 29 June, 2007. |
| 7 | |
| 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. |
| 12 | |
| 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/>. |
| 15 | */ |
| 16 | |
| 17 | #include "dnsmasq.h" |
| 18 | |
| 19 | |
| 20 | static struct cond_domain *search_domain(struct in_addr addr, struct cond_domain *c); |
Tarun Kundu | 12e3b2e | 2024-08-15 16:16:53 -0700 | [diff] [blame] | 21 | static int match_domain(struct in_addr addr, struct cond_domain *c); |
Simon Kelley | 2bb73af | 2013-04-24 17:38:19 +0100 | [diff] [blame] | 22 | static struct cond_domain *search_domain6(struct in6_addr *addr, struct cond_domain *c); |
Tarun Kundu | 12e3b2e | 2024-08-15 16:16:53 -0700 | [diff] [blame] | 23 | static int match_domain6(struct in6_addr *addr, struct cond_domain *c); |
Simon Kelley | 2bb73af | 2013-04-24 17:38:19 +0100 | [diff] [blame] | 24 | |
Tarun Kundu | 12e3b2e | 2024-08-15 16:16:53 -0700 | [diff] [blame] | 25 | int is_name_synthetic(int flags, char *name, union all_addr *addrp) |
Simon Kelley | 2bb73af | 2013-04-24 17:38:19 +0100 | [diff] [blame] | 26 | { |
| 27 | char *p; |
| 28 | struct cond_domain *c = NULL; |
Simon Kelley | ee87504 | 2018-10-23 22:10:17 +0100 | [diff] [blame] | 29 | int prot = (flags & F_IPV6) ? AF_INET6 : AF_INET; |
Tarun Kundu | 12e3b2e | 2024-08-15 16:16:53 -0700 | [diff] [blame] | 30 | union all_addr addr; |
| 31 | |
Simon Kelley | 48fd1c4 | 2013-04-25 09:49:38 +0100 | [diff] [blame] | 32 | for (c = daemon->synth_domains; c; c = c->next) |
Simon Kelley | 2bb73af | 2013-04-24 17:38:19 +0100 | [diff] [blame] | 33 | { |
Simon Kelley | 48fd1c4 | 2013-04-25 09:49:38 +0100 | [diff] [blame] | 34 | int found = 0; |
| 35 | char *tail, *pref; |
Simon Kelley | 2bb73af | 2013-04-24 17:38:19 +0100 | [diff] [blame] | 36 | |
Simon Kelley | 48fd1c4 | 2013-04-25 09:49:38 +0100 | [diff] [blame] | 37 | for (tail = name, pref = c->prefix; *tail != 0 && pref && *pref != 0; tail++, pref++) |
| 38 | { |
| 39 | unsigned int c1 = (unsigned char) *pref; |
| 40 | unsigned int c2 = (unsigned char) *tail; |
| 41 | |
| 42 | if (c1 >= 'A' && c1 <= 'Z') |
| 43 | c1 += 'a' - 'A'; |
| 44 | if (c2 >= 'A' && c2 <= 'Z') |
| 45 | c2 += 'a' - 'A'; |
| 46 | |
| 47 | if (c1 != c2) |
| 48 | break; |
| 49 | } |
| 50 | |
| 51 | if (pref && *pref != 0) |
| 52 | continue; /* prefix match fail */ |
Simon Kelley | 6b2b564 | 2018-03-10 18:12:04 +0000 | [diff] [blame] | 53 | |
| 54 | if (c->indexed) |
Simon Kelley | 48fd1c4 | 2013-04-25 09:49:38 +0100 | [diff] [blame] | 55 | { |
Simon Kelley | 6b2b564 | 2018-03-10 18:12:04 +0000 | [diff] [blame] | 56 | for (p = tail; *p; p++) |
| 57 | { |
| 58 | char c = *p; |
| 59 | |
| 60 | if (c < '0' || c > '9') |
| 61 | break; |
| 62 | } |
Simon Kelley | 48fd1c4 | 2013-04-25 09:49:38 +0100 | [diff] [blame] | 63 | |
Simon Kelley | 6b2b564 | 2018-03-10 18:12:04 +0000 | [diff] [blame] | 64 | if (*p != '.') |
Simon Kelley | 48fd1c4 | 2013-04-25 09:49:38 +0100 | [diff] [blame] | 65 | continue; |
| 66 | |
Simon Kelley | 6b2b564 | 2018-03-10 18:12:04 +0000 | [diff] [blame] | 67 | *p = 0; |
| 68 | |
| 69 | if (hostname_isequal(c->domain, p+1)) |
| 70 | { |
| 71 | if (prot == AF_INET) |
| 72 | { |
| 73 | unsigned int index = atoi(tail); |
| 74 | |
| 75 | if (!c->is6 && |
| 76 | index <= ntohl(c->end.s_addr) - ntohl(c->start.s_addr)) |
| 77 | { |
Tarun Kundu | 12e3b2e | 2024-08-15 16:16:53 -0700 | [diff] [blame] | 78 | addr.addr4.s_addr = htonl(ntohl(c->start.s_addr) + index); |
Simon Kelley | 6b2b564 | 2018-03-10 18:12:04 +0000 | [diff] [blame] | 79 | found = 1; |
| 80 | } |
Simon Kelley | ee87504 | 2018-10-23 22:10:17 +0100 | [diff] [blame] | 81 | } |
Simon Kelley | 6b2b564 | 2018-03-10 18:12:04 +0000 | [diff] [blame] | 82 | else |
| 83 | { |
| 84 | u64 index = atoll(tail); |
| 85 | |
| 86 | if (c->is6 && |
| 87 | index <= addr6part(&c->end6) - addr6part(&c->start6)) |
| 88 | { |
| 89 | u64 start = addr6part(&c->start6); |
Tarun Kundu | 12e3b2e | 2024-08-15 16:16:53 -0700 | [diff] [blame] | 90 | addr.addr6 = c->start6; |
| 91 | setaddr6part(&addr.addr6, start + index); |
Simon Kelley | 6b2b564 | 2018-03-10 18:12:04 +0000 | [diff] [blame] | 92 | found = 1; |
| 93 | } |
| 94 | } |
Simon Kelley | 6b2b564 | 2018-03-10 18:12:04 +0000 | [diff] [blame] | 95 | } |
Simon Kelley | 6d95099 | 2016-08-11 23:38:54 +0100 | [diff] [blame] | 96 | } |
| 97 | else |
Simon Kelley | 6d95099 | 2016-08-11 23:38:54 +0100 | [diff] [blame] | 98 | { |
Simon Kelley | 6b2b564 | 2018-03-10 18:12:04 +0000 | [diff] [blame] | 99 | /* NB, must not alter name if we return zero */ |
Simon Kelley | 6d95099 | 2016-08-11 23:38:54 +0100 | [diff] [blame] | 100 | for (p = tail; *p; p++) |
Simon Kelley | 6b2b564 | 2018-03-10 18:12:04 +0000 | [diff] [blame] | 101 | { |
| 102 | char c = *p; |
| 103 | |
| 104 | if ((c >='0' && c <= '9') || c == '-') |
| 105 | continue; |
| 106 | |
Simon Kelley | 6b2b564 | 2018-03-10 18:12:04 +0000 | [diff] [blame] | 107 | if (prot == AF_INET6 && ((c >='A' && c <= 'F') || (c >='a' && c <= 'f'))) |
| 108 | continue; |
Simon Kelley | 6b2b564 | 2018-03-10 18:12:04 +0000 | [diff] [blame] | 109 | |
| 110 | break; |
| 111 | } |
| 112 | |
| 113 | if (*p != '.') |
| 114 | continue; |
| 115 | |
| 116 | *p = 0; |
| 117 | |
Simon Kelley | 6b2b564 | 2018-03-10 18:12:04 +0000 | [diff] [blame] | 118 | if (prot == AF_INET6 && strstr(tail, "--ffff-") == tail) |
| 119 | { |
| 120 | /* special hack for v4-mapped. */ |
| 121 | memcpy(tail, "::ffff:", 7); |
| 122 | for (p = tail + 7; *p; p++) |
| 123 | if (*p == '-') |
| 124 | *p = '.'; |
| 125 | } |
| 126 | else |
Simon Kelley | 6b2b564 | 2018-03-10 18:12:04 +0000 | [diff] [blame] | 127 | { |
| 128 | /* swap . or : for - */ |
| 129 | for (p = tail; *p; p++) |
| 130 | if (*p == '-') |
| 131 | { |
| 132 | if (prot == AF_INET) |
| 133 | *p = '.'; |
Simon Kelley | 6b2b564 | 2018-03-10 18:12:04 +0000 | [diff] [blame] | 134 | else |
| 135 | *p = ':'; |
Simon Kelley | 6b2b564 | 2018-03-10 18:12:04 +0000 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
Tarun Kundu | 12e3b2e | 2024-08-15 16:16:53 -0700 | [diff] [blame] | 139 | if (hostname_isequal(c->domain, p+1) && inet_pton(prot, tail, &addr)) |
| 140 | found = (prot == AF_INET) ? match_domain(addr.addr4, c) : match_domain6(&addr.addr6, c); |
Simon Kelley | 6d95099 | 2016-08-11 23:38:54 +0100 | [diff] [blame] | 141 | } |
Tarun Kundu | 12e3b2e | 2024-08-15 16:16:53 -0700 | [diff] [blame] | 142 | |
Simon Kelley | 48fd1c4 | 2013-04-25 09:49:38 +0100 | [diff] [blame] | 143 | /* restore name */ |
| 144 | for (p = tail; *p; p++) |
| 145 | if (*p == '.' || *p == ':') |
| 146 | *p = '-'; |
| 147 | |
| 148 | *p = '.'; |
Simon Kelley | 6b2b564 | 2018-03-10 18:12:04 +0000 | [diff] [blame] | 149 | |
| 150 | |
Simon Kelley | 48fd1c4 | 2013-04-25 09:49:38 +0100 | [diff] [blame] | 151 | if (found) |
Tarun Kundu | 12e3b2e | 2024-08-15 16:16:53 -0700 | [diff] [blame] | 152 | { |
| 153 | if (addrp) |
| 154 | *addrp = addr; |
| 155 | |
| 156 | return 1; |
| 157 | } |
Simon Kelley | 48fd1c4 | 2013-04-25 09:49:38 +0100 | [diff] [blame] | 158 | } |
Simon Kelley | 2bb73af | 2013-04-24 17:38:19 +0100 | [diff] [blame] | 159 | |
Simon Kelley | 48fd1c4 | 2013-04-25 09:49:38 +0100 | [diff] [blame] | 160 | return 0; |
Simon Kelley | 2bb73af | 2013-04-24 17:38:19 +0100 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 164 | int is_rev_synth(int flag, union all_addr *addr, char *name) |
Simon Kelley | 2bb73af | 2013-04-24 17:38:19 +0100 | [diff] [blame] | 165 | { |
| 166 | struct cond_domain *c; |
| 167 | |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 168 | if (flag & F_IPV4 && (c = search_domain(addr->addr4, daemon->synth_domains))) |
Simon Kelley | 2bb73af | 2013-04-24 17:38:19 +0100 | [diff] [blame] | 169 | { |
| 170 | char *p; |
| 171 | |
Simon Kelley | 48fd1c4 | 2013-04-25 09:49:38 +0100 | [diff] [blame] | 172 | *name = 0; |
Simon Kelley | 6b2b564 | 2018-03-10 18:12:04 +0000 | [diff] [blame] | 173 | if (c->indexed) |
| 174 | { |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 175 | unsigned int index = ntohl(addr->addr4.s_addr) - ntohl(c->start.s_addr); |
Simon Kelley | 6b2b564 | 2018-03-10 18:12:04 +0000 | [diff] [blame] | 176 | snprintf(name, MAXDNAME, "%s%u", c->prefix ? c->prefix : "", index); |
| 177 | } |
| 178 | else |
| 179 | { |
| 180 | if (c->prefix) |
| 181 | strncpy(name, c->prefix, MAXDNAME - ADDRSTRLEN); |
Simon Kelley | 48fd1c4 | 2013-04-25 09:49:38 +0100 | [diff] [blame] | 182 | |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 183 | inet_ntop(AF_INET, &addr->addr4, name + strlen(name), ADDRSTRLEN); |
Simon Kelley | 6b2b564 | 2018-03-10 18:12:04 +0000 | [diff] [blame] | 184 | for (p = name; *p; p++) |
| 185 | if (*p == '.') |
| 186 | *p = '-'; |
| 187 | } |
| 188 | |
Simon Kelley | 2bb73af | 2013-04-24 17:38:19 +0100 | [diff] [blame] | 189 | strncat(name, ".", MAXDNAME); |
| 190 | strncat(name, c->domain, MAXDNAME); |
| 191 | |
| 192 | return 1; |
| 193 | } |
| 194 | |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 195 | if ((flag & F_IPV6) && (c = search_domain6(&addr->addr6, daemon->synth_domains))) |
Simon Kelley | 2bb73af | 2013-04-24 17:38:19 +0100 | [diff] [blame] | 196 | { |
| 197 | char *p; |
| 198 | |
Simon Kelley | 48fd1c4 | 2013-04-25 09:49:38 +0100 | [diff] [blame] | 199 | *name = 0; |
Simon Kelley | 6b2b564 | 2018-03-10 18:12:04 +0000 | [diff] [blame] | 200 | if (c->indexed) |
Simon Kelley | 2bb73af | 2013-04-24 17:38:19 +0100 | [diff] [blame] | 201 | { |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 202 | u64 index = addr6part(&addr->addr6) - addr6part(&c->start6); |
Simon Kelley | 6b2b564 | 2018-03-10 18:12:04 +0000 | [diff] [blame] | 203 | snprintf(name, MAXDNAME, "%s%llu", c->prefix ? c->prefix : "", index); |
Simon Kelley | 2bb73af | 2013-04-24 17:38:19 +0100 | [diff] [blame] | 204 | } |
Simon Kelley | 6b2b564 | 2018-03-10 18:12:04 +0000 | [diff] [blame] | 205 | else |
| 206 | { |
| 207 | if (c->prefix) |
| 208 | strncpy(name, c->prefix, MAXDNAME - ADDRSTRLEN); |
| 209 | |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 210 | inet_ntop(AF_INET6, &addr->addr6, name + strlen(name), ADDRSTRLEN); |
Simon Kelley | 2bb73af | 2013-04-24 17:38:19 +0100 | [diff] [blame] | 211 | |
Simon Kelley | 6b2b564 | 2018-03-10 18:12:04 +0000 | [diff] [blame] | 212 | /* IPv6 presentation address can start with ":", but valid domain names |
| 213 | cannot start with "-" so prepend a zero in that case. */ |
| 214 | if (!c->prefix && *name == ':') |
| 215 | { |
| 216 | *name = '0'; |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 217 | inet_ntop(AF_INET6, &addr->addr6, name+1, ADDRSTRLEN); |
Simon Kelley | 6b2b564 | 2018-03-10 18:12:04 +0000 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | /* V4-mapped have periods.... */ |
| 221 | for (p = name; *p; p++) |
| 222 | if (*p == ':' || *p == '.') |
| 223 | *p = '-'; |
| 224 | |
| 225 | } |
Simon Kelley | 2bb73af | 2013-04-24 17:38:19 +0100 | [diff] [blame] | 226 | |
| 227 | strncat(name, ".", MAXDNAME); |
| 228 | strncat(name, c->domain, MAXDNAME); |
| 229 | |
| 230 | return 1; |
| 231 | } |
Simon Kelley | 2bb73af | 2013-04-24 17:38:19 +0100 | [diff] [blame] | 232 | |
| 233 | return 0; |
| 234 | } |
| 235 | |
| 236 | |
Tarun Kundu | 12e3b2e | 2024-08-15 16:16:53 -0700 | [diff] [blame] | 237 | static int match_domain(struct in_addr addr, struct cond_domain *c) |
| 238 | { |
| 239 | if (c->interface) |
| 240 | { |
| 241 | struct addrlist *al; |
| 242 | for (al = c->al; al; al = al->next) |
| 243 | if (!(al->flags & ADDRLIST_IPV6) && |
| 244 | is_same_net_prefix(addr, al->addr.addr4, al->prefixlen)) |
| 245 | return 1; |
| 246 | } |
| 247 | else if (!c->is6 && |
| 248 | ntohl(addr.s_addr) >= ntohl(c->start.s_addr) && |
| 249 | ntohl(addr.s_addr) <= ntohl(c->end.s_addr)) |
| 250 | return 1; |
| 251 | |
| 252 | return 0; |
| 253 | } |
| 254 | |
Simon Kelley | 2bb73af | 2013-04-24 17:38:19 +0100 | [diff] [blame] | 255 | static struct cond_domain *search_domain(struct in_addr addr, struct cond_domain *c) |
| 256 | { |
| 257 | for (; c; c = c->next) |
Tarun Kundu | 12e3b2e | 2024-08-15 16:16:53 -0700 | [diff] [blame] | 258 | if (match_domain(addr, c)) |
Simon Kelley | 2bb73af | 2013-04-24 17:38:19 +0100 | [diff] [blame] | 259 | return c; |
Tarun Kundu | 12e3b2e | 2024-08-15 16:16:53 -0700 | [diff] [blame] | 260 | |
Simon Kelley | 2bb73af | 2013-04-24 17:38:19 +0100 | [diff] [blame] | 261 | return NULL; |
| 262 | } |
| 263 | |
| 264 | char *get_domain(struct in_addr addr) |
| 265 | { |
| 266 | struct cond_domain *c; |
| 267 | |
| 268 | if ((c = search_domain(addr, daemon->cond_domain))) |
| 269 | return c->domain; |
| 270 | |
| 271 | return daemon->domain_suffix; |
| 272 | } |
| 273 | |
Tarun Kundu | 12e3b2e | 2024-08-15 16:16:53 -0700 | [diff] [blame] | 274 | static int match_domain6(struct in6_addr *addr, struct cond_domain *c) |
| 275 | { |
| 276 | |
| 277 | /* subnet from interface address. */ |
| 278 | if (c->interface) |
| 279 | { |
| 280 | struct addrlist *al; |
| 281 | for (al = c->al; al; al = al->next) |
| 282 | if (al->flags & ADDRLIST_IPV6 && |
| 283 | is_same_net6(addr, &al->addr.addr6, al->prefixlen)) |
| 284 | return 1; |
| 285 | } |
| 286 | else if (c->is6) |
| 287 | { |
| 288 | if (c->prefixlen >= 64) |
| 289 | { |
| 290 | u64 addrpart = addr6part(addr); |
| 291 | if (is_same_net6(addr, &c->start6, 64) && |
| 292 | addrpart >= addr6part(&c->start6) && |
| 293 | addrpart <= addr6part(&c->end6)) |
| 294 | return 1; |
| 295 | } |
| 296 | else if (is_same_net6(addr, &c->start6, c->prefixlen)) |
| 297 | return 1; |
| 298 | } |
| 299 | |
| 300 | return 0; |
| 301 | } |
Simon Kelley | ee87504 | 2018-10-23 22:10:17 +0100 | [diff] [blame] | 302 | |
Simon Kelley | 2bb73af | 2013-04-24 17:38:19 +0100 | [diff] [blame] | 303 | static struct cond_domain *search_domain6(struct in6_addr *addr, struct cond_domain *c) |
| 304 | { |
Simon Kelley | 2bb73af | 2013-04-24 17:38:19 +0100 | [diff] [blame] | 305 | for (; c; c = c->next) |
Tarun Kundu | 12e3b2e | 2024-08-15 16:16:53 -0700 | [diff] [blame] | 306 | if (match_domain6(addr, c)) |
Simon Kelley | 2bb73af | 2013-04-24 17:38:19 +0100 | [diff] [blame] | 307 | return c; |
| 308 | |
| 309 | return NULL; |
| 310 | } |
| 311 | |
| 312 | char *get_domain6(struct in6_addr *addr) |
| 313 | { |
| 314 | struct cond_domain *c; |
| 315 | |
Simon Kelley | 6bd109a | 2013-07-27 15:11:44 +0100 | [diff] [blame] | 316 | if (addr && (c = search_domain6(addr, daemon->cond_domain))) |
Simon Kelley | 2bb73af | 2013-04-24 17:38:19 +0100 | [diff] [blame] | 317 | return c->domain; |
| 318 | |
| 319 | return daemon->domain_suffix; |
| 320 | } |