move rdata_buffer to cache_answer()
[project/mdnsd.git] / dns.c
1 /*
2 * Copyright (C) 2014 John Crispin <blogic@openwrt.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License version 2.1
6 * as published by the Free Software Foundation
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
14 #include <sys/types.h>
15 #include <sys/stat.h>
16
17 #include <fcntl.h>
18 #include <time.h>
19 #include <stdio.h>
20 #include <unistd.h>
21 #include <sys/types.h>
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include <arpa/inet.h>
25 #include <asm/byteorder.h>
26 #include <arpa/nameser.h>
27 #include <resolv.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include <libubox/uloop.h>
32 #include <libubox/usock.h>
33 #include <libubox/utils.h>
34
35 #include "announce.h"
36 #include "util.h"
37 #include "dns.h"
38 #include "cache.h"
39 #include "service.h"
40 #include "interface.h"
41
42 static char name_buffer[MAX_NAME_LEN + 1];
43
44 const char*
45 dns_type_string(uint16_t type)
46 {
47 switch (type) {
48 case TYPE_A:
49 return "A";
50
51 case TYPE_AAAA:
52 return "AAAA";
53
54 case TYPE_PTR:
55 return "PTR";
56
57 case TYPE_TXT:
58 return "TXT";
59
60 case TYPE_SRV:
61 return "SRV";
62
63 case TYPE_ANY:
64 return "ANY";
65 }
66
67 return "N/A";
68 }
69
70 void
71 dns_send_question(struct interface *iface, const char *question, int type)
72 {
73 static struct dns_header h = {
74 .questions = cpu_to_be16(1),
75 };
76 static struct dns_question q = {
77 .class = cpu_to_be16(1),
78 };
79 static struct iovec iov[] = {
80 {
81 .iov_base = &h,
82 .iov_len = sizeof(h),
83 },
84 {
85 .iov_base = name_buffer,
86 },
87 {
88 .iov_base = &q,
89 .iov_len = sizeof(q),
90 }
91 };
92 int len;
93
94 q.type = __cpu_to_be16(type);
95
96 len = dn_comp(question, (void *) name_buffer, sizeof(name_buffer), NULL, NULL);
97 if (len < 1)
98 return;
99
100 iov[1].iov_len = len;
101
102 if (interface_send_packet(iface, iov, ARRAY_SIZE(iov)) < 0)
103 fprintf(stderr, "failed to send question\n");
104 else
105 DBG(1, "Q <- %s %s\n", dns_type_string(type), question);
106 }
107
108
109 struct dns_reply {
110 int type;
111 struct dns_answer a;
112 uint16_t rdlength;
113 uint8_t *rdata;
114 char *buffer;
115 };
116
117 #define MAX_ANSWER 8
118 static struct dns_reply dns_reply[1 + (MAX_ANSWER * 3)];
119 static int dns_answer_cnt;
120
121 void
122 dns_init_answer(void)
123 {
124 dns_answer_cnt = 0;
125 }
126
127 void
128 dns_add_answer(int type, const uint8_t *rdata, uint16_t rdlength)
129 {
130 struct dns_reply *a = &dns_reply[dns_answer_cnt];
131 if (dns_answer_cnt == MAX_ANSWER)
132 return;
133 a->rdata = memdup(rdata, rdlength);
134 a->type = type;
135 a->rdlength = rdlength;
136 dns_answer_cnt++;
137 }
138
139 void
140 dns_send_answer(struct interface *iface, const char *answer)
141 {
142 uint8_t buffer[256];
143 struct dns_header h = { 0 };
144 struct iovec *iov;
145 int len, i;
146
147 if (!dns_answer_cnt)
148 return;
149
150 h.answers = __cpu_to_be16(dns_answer_cnt);
151 h.flags = __cpu_to_be16(0x8400);
152
153 iov = alloca(sizeof(struct iovec) * ((dns_answer_cnt * 3) + 1));
154 iov[0].iov_base = &h;
155 iov[0].iov_len = sizeof(struct dns_header);
156
157 for (i = 0; i < dns_answer_cnt; i++) {
158 struct dns_answer *a = &dns_reply[i].a;
159 int id = (i * 3) + 1;
160
161 memset(a, 0, sizeof(*a));
162 a->type = __cpu_to_be16(dns_reply[i].type);
163 a->class = __cpu_to_be16(1);
164 a->ttl = __cpu_to_be32(announce_ttl);
165 a->rdlength = __cpu_to_be16(dns_reply[i].rdlength);
166
167 len = dn_comp(answer, buffer, sizeof(buffer), NULL, NULL);
168 if (len < 1)
169 return;
170
171 dns_reply[i].buffer = iov[id].iov_base = memdup(buffer, len);
172 iov[id].iov_len = len;
173
174 iov[id + 1].iov_base = a;
175 iov[id + 1].iov_len = sizeof(struct dns_answer);
176
177 iov[id + 2].iov_base = dns_reply[i].rdata;
178 iov[id + 2].iov_len = dns_reply[i].rdlength;
179
180 DBG(1, "A <- %s %s\n", dns_type_string(dns_reply[i].type), answer);
181 }
182
183 if (interface_send_packet(iface, iov, (dns_answer_cnt * 3) + 1) < 0)
184 fprintf(stderr, "failed to send question\n");
185
186 for (i = 0; i < dns_answer_cnt; i++) {
187 free(dns_reply[i].buffer);
188 free(dns_reply[i].rdata);
189 }
190 dns_answer_cnt = 0;
191 }
192
193 static int
194 scan_name(const uint8_t *buffer, int len)
195 {
196 int offset = 0;
197
198 while (len && (*buffer != '\0')) {
199 int l = *buffer;
200
201 if (IS_COMPRESSED(l))
202 return offset + 2;
203
204 len -= l + 1;
205 offset += l + 1;
206 buffer += l + 1;
207 }
208
209 if (!len || !offset || (*buffer != '\0'))
210 return -1;
211
212 return offset + 1;
213 }
214
215 static struct dns_header*
216 dns_consume_header(uint8_t **data, int *len)
217 {
218 struct dns_header *h = (struct dns_header *) *data;
219 uint16_t *swap = (uint16_t *) h;
220 int endianess = 6;
221
222 if (*len < sizeof(struct dns_header))
223 return NULL;
224
225 while (endianess--) {
226 *swap = __be16_to_cpu(*swap);
227 swap++;
228 }
229
230 *len -= sizeof(struct dns_header);
231 *data += sizeof(struct dns_header);
232
233 return h;
234 }
235
236 static struct dns_question*
237 dns_consume_question(uint8_t **data, int *len)
238 {
239 struct dns_question *q = (struct dns_question *) *data;
240 uint16_t *swap = (uint16_t *) q;
241 int endianess = 2;
242
243 if (*len < sizeof(struct dns_question))
244 return NULL;
245
246 while (endianess--) {
247 *swap = __be16_to_cpu(*swap);
248 swap++;
249 }
250
251 *len -= sizeof(struct dns_question);
252 *data += sizeof(struct dns_question);
253
254 return q;
255 }
256
257 static struct dns_answer*
258 dns_consume_answer(uint8_t **data, int *len)
259 {
260 struct dns_answer *a = (struct dns_answer *) *data;
261
262 if (*len < sizeof(struct dns_answer))
263 return NULL;
264
265 a->type = __be16_to_cpu(a->type);
266 a->class = __be16_to_cpu(a->class);
267 a->ttl = __be32_to_cpu(a->ttl);
268 a->rdlength = __be16_to_cpu(a->rdlength);
269
270 *len -= sizeof(struct dns_answer);
271 *data += sizeof(struct dns_answer);
272
273 return a;
274 }
275
276 static char *
277 dns_consume_name(const uint8_t *base, int blen, uint8_t **data, int *len)
278 {
279 int nlen = scan_name(*data, *len);
280
281 if (nlen < 1)
282 return NULL;
283
284 if (dn_expand(base, base + blen, *data, name_buffer, MAX_NAME_LEN) < 0) {
285 perror("dns_consume_name/dn_expand");
286 return NULL;
287 }
288
289 *len -= nlen;
290 *data += nlen;
291
292 return name_buffer;
293 }
294
295 static int
296 parse_answer(struct interface *iface, uint8_t *buffer, int len, uint8_t **b, int *rlen, int cache)
297 {
298 char *name = dns_consume_name(buffer, len, b, rlen);
299 struct dns_answer *a;
300 uint8_t *rdata;
301
302 if (!name) {
303 fprintf(stderr, "dropping: bad question\n");
304 return -1;
305 }
306
307 a = dns_consume_answer(b, rlen);
308 if (!a) {
309 fprintf(stderr, "dropping: bad question\n");
310 return -1;
311 }
312
313 rdata = *b;
314 if (a->rdlength > *rlen) {
315 fprintf(stderr, "dropping: bad question\n");
316 return -1;
317 }
318
319 *rlen -= a->rdlength;
320 *b += a->rdlength;
321
322 if (cache)
323 cache_answer(iface, buffer, len, name, a, rdata);
324
325 return 0;
326 }
327
328 static void
329 parse_question(struct interface *iface, char *name, struct dns_question *q)
330 {
331 char *host;
332
333 DBG(1, "Q -> %s %s\n", dns_type_string(q->type), name);
334
335 switch (q->type) {
336 case TYPE_ANY:
337 host = service_name("local");
338 if (!strcmp(name, host))
339 service_reply(iface, NULL);
340 break;
341
342 case TYPE_PTR:
343 service_announce_services(iface, name);
344 service_reply(iface, name);
345 break;
346
347 case TYPE_AAAA:
348 case TYPE_A:
349 host = strstr(name, ".local");
350 if (host)
351 *host = '\0';
352 if (!strcmp(hostname, name))
353 service_reply_a(iface, q->type);
354 break;
355 };
356 }
357
358 void
359 dns_handle_packet(struct interface *iface, uint8_t *buffer, int len)
360 {
361 struct dns_header *h;
362 uint8_t *b = buffer;
363 int rlen = len;
364
365 h = dns_consume_header(&b, &rlen);
366 if (!h) {
367 fprintf(stderr, "dropping: bad header\n");
368 return;
369 }
370
371 while (h->questions-- > 0) {
372 char *name = dns_consume_name(buffer, len, &b, &rlen);
373 struct dns_question *q;
374
375 if (!name) {
376 fprintf(stderr, "dropping: bad name\n");
377 return;
378 }
379
380 q = dns_consume_question(&b, &rlen);
381 if (!q) {
382 fprintf(stderr, "dropping: bad question\n");
383 return;
384 }
385
386 if (!(h->flags & FLAG_RESPONSE))
387 parse_question(iface, name, q);
388 }
389
390 if (!(h->flags & FLAG_RESPONSE))
391 return;
392
393 while (h->answers-- > 0)
394 parse_answer(iface, buffer, len, &b, &rlen, 1);
395
396 while (h->authority-- > 0)
397 parse_answer(iface, buffer, len, &b, &rlen, 0);
398
399 while (h->additional-- > 0)
400 parse_answer(iface, buffer, len, &b, &rlen, 1);
401 }