blob: f4c0bf71082088cda80eecb3759731ed6b498104 [file] [log] [blame]
Tarun Kundu12e3b2e2024-08-15 16:16:53 -07001/* dnsmasq is Copyright (c) 2000-2024 Simon Kelley
Simon Kelley2bb73af2013-04-24 17:38:19 +01002
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
20static struct cond_domain *search_domain(struct in_addr addr, struct cond_domain *c);
Tarun Kundu12e3b2e2024-08-15 16:16:53 -070021static int match_domain(struct in_addr addr, struct cond_domain *c);
Simon Kelley2bb73af2013-04-24 17:38:19 +010022static struct cond_domain *search_domain6(struct in6_addr *addr, struct cond_domain *c);
Tarun Kundu12e3b2e2024-08-15 16:16:53 -070023static int match_domain6(struct in6_addr *addr, struct cond_domain *c);
Simon Kelley2bb73af2013-04-24 17:38:19 +010024
Tarun Kundu12e3b2e2024-08-15 16:16:53 -070025int is_name_synthetic(int flags, char *name, union all_addr *addrp)
Simon Kelley2bb73af2013-04-24 17:38:19 +010026{
27 char *p;
28 struct cond_domain *c = NULL;
Simon Kelleyee875042018-10-23 22:10:17 +010029 int prot = (flags & F_IPV6) ? AF_INET6 : AF_INET;
Tarun Kundu12e3b2e2024-08-15 16:16:53 -070030 union all_addr addr;
31
Simon Kelley48fd1c42013-04-25 09:49:38 +010032 for (c = daemon->synth_domains; c; c = c->next)
Simon Kelley2bb73af2013-04-24 17:38:19 +010033 {
Simon Kelley48fd1c42013-04-25 09:49:38 +010034 int found = 0;
35 char *tail, *pref;
Simon Kelley2bb73af2013-04-24 17:38:19 +010036
Simon Kelley48fd1c42013-04-25 09:49:38 +010037 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 Kelley6b2b5642018-03-10 18:12:04 +000053
54 if (c->indexed)
Simon Kelley48fd1c42013-04-25 09:49:38 +010055 {
Simon Kelley6b2b5642018-03-10 18:12:04 +000056 for (p = tail; *p; p++)
57 {
58 char c = *p;
59
60 if (c < '0' || c > '9')
61 break;
62 }
Simon Kelley48fd1c42013-04-25 09:49:38 +010063
Simon Kelley6b2b5642018-03-10 18:12:04 +000064 if (*p != '.')
Simon Kelley48fd1c42013-04-25 09:49:38 +010065 continue;
66
Simon Kelley6b2b5642018-03-10 18:12:04 +000067 *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 Kundu12e3b2e2024-08-15 16:16:53 -070078 addr.addr4.s_addr = htonl(ntohl(c->start.s_addr) + index);
Simon Kelley6b2b5642018-03-10 18:12:04 +000079 found = 1;
80 }
Simon Kelleyee875042018-10-23 22:10:17 +010081 }
Simon Kelley6b2b5642018-03-10 18:12:04 +000082 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 Kundu12e3b2e2024-08-15 16:16:53 -070090 addr.addr6 = c->start6;
91 setaddr6part(&addr.addr6, start + index);
Simon Kelley6b2b5642018-03-10 18:12:04 +000092 found = 1;
93 }
94 }
Simon Kelley6b2b5642018-03-10 18:12:04 +000095 }
Simon Kelley6d950992016-08-11 23:38:54 +010096 }
97 else
Simon Kelley6d950992016-08-11 23:38:54 +010098 {
Simon Kelley6b2b5642018-03-10 18:12:04 +000099 /* NB, must not alter name if we return zero */
Simon Kelley6d950992016-08-11 23:38:54 +0100100 for (p = tail; *p; p++)
Simon Kelley6b2b5642018-03-10 18:12:04 +0000101 {
102 char c = *p;
103
104 if ((c >='0' && c <= '9') || c == '-')
105 continue;
106
Simon Kelley6b2b5642018-03-10 18:12:04 +0000107 if (prot == AF_INET6 && ((c >='A' && c <= 'F') || (c >='a' && c <= 'f')))
108 continue;
Simon Kelley6b2b5642018-03-10 18:12:04 +0000109
110 break;
111 }
112
113 if (*p != '.')
114 continue;
115
116 *p = 0;
117
Simon Kelley6b2b5642018-03-10 18:12:04 +0000118 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 Kelley6b2b5642018-03-10 18:12:04 +0000127 {
128 /* swap . or : for - */
129 for (p = tail; *p; p++)
130 if (*p == '-')
131 {
132 if (prot == AF_INET)
133 *p = '.';
Simon Kelley6b2b5642018-03-10 18:12:04 +0000134 else
135 *p = ':';
Simon Kelley6b2b5642018-03-10 18:12:04 +0000136 }
137 }
138
Tarun Kundu12e3b2e2024-08-15 16:16:53 -0700139 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 Kelley6d950992016-08-11 23:38:54 +0100141 }
Tarun Kundu12e3b2e2024-08-15 16:16:53 -0700142
Simon Kelley48fd1c42013-04-25 09:49:38 +0100143 /* restore name */
144 for (p = tail; *p; p++)
145 if (*p == '.' || *p == ':')
146 *p = '-';
147
148 *p = '.';
Simon Kelley6b2b5642018-03-10 18:12:04 +0000149
150
Simon Kelley48fd1c42013-04-25 09:49:38 +0100151 if (found)
Tarun Kundu12e3b2e2024-08-15 16:16:53 -0700152 {
153 if (addrp)
154 *addrp = addr;
155
156 return 1;
157 }
Simon Kelley48fd1c42013-04-25 09:49:38 +0100158 }
Simon Kelley2bb73af2013-04-24 17:38:19 +0100159
Simon Kelley48fd1c42013-04-25 09:49:38 +0100160 return 0;
Simon Kelley2bb73af2013-04-24 17:38:19 +0100161}
162
163
Simon Kelleycc921df2019-01-02 22:48:59 +0000164int is_rev_synth(int flag, union all_addr *addr, char *name)
Simon Kelley2bb73af2013-04-24 17:38:19 +0100165{
166 struct cond_domain *c;
167
Simon Kelleycc921df2019-01-02 22:48:59 +0000168 if (flag & F_IPV4 && (c = search_domain(addr->addr4, daemon->synth_domains)))
Simon Kelley2bb73af2013-04-24 17:38:19 +0100169 {
170 char *p;
171
Simon Kelley48fd1c42013-04-25 09:49:38 +0100172 *name = 0;
Simon Kelley6b2b5642018-03-10 18:12:04 +0000173 if (c->indexed)
174 {
Simon Kelleycc921df2019-01-02 22:48:59 +0000175 unsigned int index = ntohl(addr->addr4.s_addr) - ntohl(c->start.s_addr);
Simon Kelley6b2b5642018-03-10 18:12:04 +0000176 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 Kelley48fd1c42013-04-25 09:49:38 +0100182
Simon Kelleycc921df2019-01-02 22:48:59 +0000183 inet_ntop(AF_INET, &addr->addr4, name + strlen(name), ADDRSTRLEN);
Simon Kelley6b2b5642018-03-10 18:12:04 +0000184 for (p = name; *p; p++)
185 if (*p == '.')
186 *p = '-';
187 }
188
Simon Kelley2bb73af2013-04-24 17:38:19 +0100189 strncat(name, ".", MAXDNAME);
190 strncat(name, c->domain, MAXDNAME);
191
192 return 1;
193 }
194
Simon Kelleycc921df2019-01-02 22:48:59 +0000195 if ((flag & F_IPV6) && (c = search_domain6(&addr->addr6, daemon->synth_domains)))
Simon Kelley2bb73af2013-04-24 17:38:19 +0100196 {
197 char *p;
198
Simon Kelley48fd1c42013-04-25 09:49:38 +0100199 *name = 0;
Simon Kelley6b2b5642018-03-10 18:12:04 +0000200 if (c->indexed)
Simon Kelley2bb73af2013-04-24 17:38:19 +0100201 {
Simon Kelleycc921df2019-01-02 22:48:59 +0000202 u64 index = addr6part(&addr->addr6) - addr6part(&c->start6);
Simon Kelley6b2b5642018-03-10 18:12:04 +0000203 snprintf(name, MAXDNAME, "%s%llu", c->prefix ? c->prefix : "", index);
Simon Kelley2bb73af2013-04-24 17:38:19 +0100204 }
Simon Kelley6b2b5642018-03-10 18:12:04 +0000205 else
206 {
207 if (c->prefix)
208 strncpy(name, c->prefix, MAXDNAME - ADDRSTRLEN);
209
Simon Kelleycc921df2019-01-02 22:48:59 +0000210 inet_ntop(AF_INET6, &addr->addr6, name + strlen(name), ADDRSTRLEN);
Simon Kelley2bb73af2013-04-24 17:38:19 +0100211
Simon Kelley6b2b5642018-03-10 18:12:04 +0000212 /* 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 Kelleycc921df2019-01-02 22:48:59 +0000217 inet_ntop(AF_INET6, &addr->addr6, name+1, ADDRSTRLEN);
Simon Kelley6b2b5642018-03-10 18:12:04 +0000218 }
219
220 /* V4-mapped have periods.... */
221 for (p = name; *p; p++)
222 if (*p == ':' || *p == '.')
223 *p = '-';
224
225 }
Simon Kelley2bb73af2013-04-24 17:38:19 +0100226
227 strncat(name, ".", MAXDNAME);
228 strncat(name, c->domain, MAXDNAME);
229
230 return 1;
231 }
Simon Kelley2bb73af2013-04-24 17:38:19 +0100232
233 return 0;
234}
235
236
Tarun Kundu12e3b2e2024-08-15 16:16:53 -0700237static 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 Kelley2bb73af2013-04-24 17:38:19 +0100255static struct cond_domain *search_domain(struct in_addr addr, struct cond_domain *c)
256{
257 for (; c; c = c->next)
Tarun Kundu12e3b2e2024-08-15 16:16:53 -0700258 if (match_domain(addr, c))
Simon Kelley2bb73af2013-04-24 17:38:19 +0100259 return c;
Tarun Kundu12e3b2e2024-08-15 16:16:53 -0700260
Simon Kelley2bb73af2013-04-24 17:38:19 +0100261 return NULL;
262}
263
264char *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 Kundu12e3b2e2024-08-15 16:16:53 -0700274static 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 Kelleyee875042018-10-23 22:10:17 +0100302
Simon Kelley2bb73af2013-04-24 17:38:19 +0100303static struct cond_domain *search_domain6(struct in6_addr *addr, struct cond_domain *c)
304{
Simon Kelley2bb73af2013-04-24 17:38:19 +0100305 for (; c; c = c->next)
Tarun Kundu12e3b2e2024-08-15 16:16:53 -0700306 if (match_domain6(addr, c))
Simon Kelley2bb73af2013-04-24 17:38:19 +0100307 return c;
308
309 return NULL;
310}
311
312char *get_domain6(struct in6_addr *addr)
313{
314 struct cond_domain *c;
315
Simon Kelley6bd109a2013-07-27 15:11:44 +0100316 if (addr && (c = search_domain6(addr, daemon->cond_domain)))
Simon Kelley2bb73af2013-04-24 17:38:19 +0100317 return c->domain;
318
319 return daemon->domain_suffix;
320}