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