simplify memory management for dns answers: use a blob_buf to cache entries
[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 static struct blob_buf ans_buf;
44
45 const char*
46 dns_type_string(uint16_t type)
47 {
48         static const struct {
49                 uint16_t type;
50                 char str[5];
51         } type_str[] = {
52                 { TYPE_A, "A" },
53                 { TYPE_AAAA, "AAAA" },
54                 { TYPE_PTR, "PTR" },
55                 { TYPE_TXT, "TXT" },
56                 { TYPE_SRV, "SRV" },
57                 { TYPE_ANY, "ANY" },
58         };
59         int i;
60
61         for (i = 0; i < ARRAY_SIZE(type_str); i++) {
62                 if (type == type_str[i].type)
63                         return type_str[i].str;
64         }
65
66         return "N/A";
67 }
68
69 void
70 dns_send_question(struct interface *iface, const char *question, int type)
71 {
72         static struct dns_header h = {
73                 .questions = cpu_to_be16(1),
74         };
75         static struct dns_question q = {
76                 .class = cpu_to_be16(1),
77         };
78         static struct iovec iov[] = {
79                 {
80                         .iov_base = &h,
81                         .iov_len = sizeof(h),
82                 },
83                 {
84                         .iov_base = name_buffer,
85                 },
86                 {
87                         .iov_base = &q,
88                         .iov_len = sizeof(q),
89                 }
90         };
91         int len;
92
93         q.type = __cpu_to_be16(type);
94
95         len = dn_comp(question, (void *) name_buffer, sizeof(name_buffer), NULL, NULL);
96         if (len < 1)
97                 return;
98
99         iov[1].iov_len = len;
100
101         if (interface_send_packet(iface, iov, ARRAY_SIZE(iov)) < 0)
102                 fprintf(stderr, "failed to send question\n");
103         else
104                 DBG(1, "Q <- %s %s\n", dns_type_string(type), question);
105 }
106
107
108 struct dns_reply {
109         int type;
110         struct dns_answer a;
111         uint16_t rdlength;
112         uint8_t *rdata;
113         char *buffer;
114 };
115
116 static int dns_answer_cnt;
117
118 void
119 dns_init_answer(void)
120 {
121         dns_answer_cnt = 0;
122         blob_buf_init(&ans_buf, 0);
123 }
124
125 void
126 dns_add_answer(int type, const uint8_t *rdata, uint16_t rdlength)
127 {
128         struct blob_attr *attr;
129         struct dns_answer *a;
130
131         attr = blob_new(&ans_buf, 0, sizeof(*a) + rdlength);
132         a = blob_data(attr);
133         a->type = cpu_to_be16(type);
134         a->class = cpu_to_be16(1);
135         a->ttl = cpu_to_be32(announce_ttl);
136         a->rdlength = cpu_to_be16(rdlength);
137         memcpy(a + 1, rdata, rdlength);
138
139         dns_answer_cnt++;
140 }
141
142 void
143 dns_send_answer(struct interface *iface, const char *answer)
144 {
145         uint8_t buffer[256];
146         struct blob_attr *attr;
147         struct dns_header h = { 0 };
148         struct iovec *iov;
149         int answer_len, rem;
150         int n_iov = 0;
151
152         if (!dns_answer_cnt)
153                 return;
154
155         h.answers = __cpu_to_be16(dns_answer_cnt);
156         h.flags = __cpu_to_be16(0x8400);
157
158         iov = alloca(sizeof(struct iovec) * ((dns_answer_cnt * 2) + 1));
159
160         iov[n_iov].iov_base = &h;
161         iov[n_iov].iov_len = sizeof(struct dns_header);
162         n_iov++;
163
164         answer_len = dn_comp(answer, buffer, sizeof(buffer), NULL, NULL);
165         if (answer_len < 1)
166                 return;
167
168         blob_for_each_attr(attr, ans_buf.head, rem) {
169                 struct dns_answer *a = blob_data(attr);
170
171                 iov[n_iov].iov_base = buffer;
172                 iov[n_iov].iov_len = answer_len;
173                 n_iov++;
174
175                 iov[n_iov].iov_base = blob_data(attr);
176                 iov[n_iov].iov_len = blob_len(attr);
177                 n_iov++;
178
179                 DBG(1, "A <- %s %s\n", dns_type_string(be16_to_cpu(a->type)), answer);
180         }
181
182         if (interface_send_packet(iface, iov, n_iov) < 0)
183                 fprintf(stderr, "failed to send question\n");
184 }
185
186 static int
187 scan_name(const uint8_t *buffer, int len)
188 {
189         int offset = 0;
190
191         while (len && (*buffer != '\0')) {
192                 int l = *buffer;
193
194                 if (IS_COMPRESSED(l))
195                         return offset + 2;
196
197                 len -= l + 1;
198                 offset += l + 1;
199                 buffer += l + 1;
200         }
201
202         if (!len || !offset || (*buffer != '\0'))
203                 return -1;
204
205         return offset + 1;
206 }
207
208 static struct dns_header*
209 dns_consume_header(uint8_t **data, int *len)
210 {
211         struct dns_header *h = (struct dns_header *) *data;
212         uint16_t *swap = (uint16_t *) h;
213         int endianess = 6;
214
215         if (*len < sizeof(struct dns_header))
216                 return NULL;
217
218         while (endianess--) {
219                 *swap = __be16_to_cpu(*swap);
220                 swap++;
221         }
222
223         *len -= sizeof(struct dns_header);
224         *data += sizeof(struct dns_header);
225
226         return h;
227 }
228
229 static struct dns_question*
230 dns_consume_question(uint8_t **data, int *len)
231 {
232         struct dns_question *q = (struct dns_question *) *data;
233         uint16_t *swap = (uint16_t *) q;
234         int endianess = 2;
235
236         if (*len < sizeof(struct dns_question))
237                 return NULL;
238
239         while (endianess--) {
240                 *swap = __be16_to_cpu(*swap);
241                 swap++;
242         }
243
244         *len -= sizeof(struct dns_question);
245         *data += sizeof(struct dns_question);
246
247         return q;
248 }
249
250 static struct dns_answer*
251 dns_consume_answer(uint8_t **data, int *len)
252 {
253         struct dns_answer *a = (struct dns_answer *) *data;
254
255         if (*len < sizeof(struct dns_answer))
256                 return NULL;
257
258         a->type = __be16_to_cpu(a->type);
259         a->class = __be16_to_cpu(a->class);
260         a->ttl = __be32_to_cpu(a->ttl);
261         a->rdlength = __be16_to_cpu(a->rdlength);
262
263         *len -= sizeof(struct dns_answer);
264         *data += sizeof(struct dns_answer);
265
266         return a;
267 }
268
269 static char *
270 dns_consume_name(const uint8_t *base, int blen, uint8_t **data, int *len)
271 {
272         int nlen = scan_name(*data, *len);
273
274         if (nlen < 1)
275                 return NULL;
276
277         if (dn_expand(base, base + blen, *data, name_buffer, MAX_NAME_LEN) < 0) {
278                 perror("dns_consume_name/dn_expand");
279                 return NULL;
280         }
281
282         *len -= nlen;
283         *data += nlen;
284
285         return name_buffer;
286 }
287
288 static int
289 parse_answer(struct interface *iface, uint8_t *buffer, int len, uint8_t **b, int *rlen, int cache)
290 {
291         char *name = dns_consume_name(buffer, len, b, rlen);
292         struct dns_answer *a;
293         uint8_t *rdata;
294
295         if (!name) {
296                 fprintf(stderr, "dropping: bad question\n");
297                 return -1;
298         }
299
300         a = dns_consume_answer(b, rlen);
301         if (!a) {
302                 fprintf(stderr, "dropping: bad question\n");
303                 return -1;
304         }
305
306         rdata = *b;
307         if (a->rdlength > *rlen) {
308                 fprintf(stderr, "dropping: bad question\n");
309                 return -1;
310         }
311
312         *rlen -= a->rdlength;
313         *b += a->rdlength;
314
315         if (cache)
316                 cache_answer(iface, buffer, len, name, a, rdata);
317
318         return 0;
319 }
320
321 static void
322 parse_question(struct interface *iface, char *name, struct dns_question *q)
323 {
324         char *host;
325
326         DBG(1, "Q -> %s %s\n", dns_type_string(q->type), name);
327
328         switch (q->type) {
329         case TYPE_ANY:
330                 if (!strcmp(name, mdns_hostname_local))
331                         service_reply(iface, NULL);
332                 break;
333
334         case TYPE_PTR:
335                 service_announce_services(iface, name);
336                 service_reply(iface, name);
337                 break;
338
339         case TYPE_AAAA:
340         case TYPE_A:
341                 host = strstr(name, ".local");
342                 if (host)
343                         *host = '\0';
344                 if (!strcmp(mdns_hostname, name))
345                         service_reply_a(iface, q->type);
346                 break;
347         };
348 }
349
350 void
351 dns_handle_packet(struct interface *iface, uint8_t *buffer, int len)
352 {
353         struct dns_header *h;
354         uint8_t *b = buffer;
355         int rlen = len;
356
357         h = dns_consume_header(&b, &rlen);
358         if (!h) {
359                 fprintf(stderr, "dropping: bad header\n");
360                 return;
361         }
362
363         while (h->questions-- > 0) {
364                 char *name = dns_consume_name(buffer, len, &b, &rlen);
365                 struct dns_question *q;
366
367                 if (!name) {
368                         fprintf(stderr, "dropping: bad name\n");
369                         return;
370                 }
371
372                 q = dns_consume_question(&b, &rlen);
373                 if (!q) {
374                         fprintf(stderr, "dropping: bad question\n");
375                         return;
376                 }
377
378                 if (!(h->flags & FLAG_RESPONSE))
379                         parse_question(iface, name, q);
380         }
381
382         if (!(h->flags & FLAG_RESPONSE))
383                 return;
384
385         while (h->answers-- > 0)
386                 parse_answer(iface, buffer, len, &b, &rlen, 1);
387
388         while (h->authority-- > 0)
389                 parse_answer(iface, buffer, len, &b, &rlen, 0);
390
391         while (h->additional-- > 0)
392                 parse_answer(iface, buffer, len, &b, &rlen, 1);
393 }