Prepare support for sending unicast DNS answers
[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 <ifaddrs.h>
19 #include <time.h>
20 #include <stdio.h>
21 #include <unistd.h>
22 #include <sys/types.h>
23 #include <sys/socket.h>
24 #include <netinet/in.h>
25 #include <arpa/inet.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 char dns_buffer[MAX_NAME_LEN];
44 static struct blob_buf ans_buf;
45
46 const char*
47 dns_type_string(uint16_t type)
48 {
49         static const struct {
50                 uint16_t type;
51                 char str[5];
52         } type_str[] = {
53                 { TYPE_A, "A" },
54                 { TYPE_AAAA, "AAAA" },
55                 { TYPE_PTR, "PTR" },
56                 { TYPE_TXT, "TXT" },
57                 { TYPE_SRV, "SRV" },
58                 { TYPE_ANY, "ANY" },
59         };
60         int i;
61
62         for (i = 0; i < ARRAY_SIZE(type_str); i++) {
63                 if (type == type_str[i].type)
64                         return type_str[i].str;
65         }
66
67         return "N/A";
68 }
69
70 void
71 dns_send_question(struct interface *iface, const char *question, int type, int multicast)
72 {
73         static struct dns_header h;
74         static struct dns_question q;
75         static struct iovec iov[] = {
76                 {
77                         .iov_base = &h,
78                         .iov_len = sizeof(h),
79                 },
80                 {
81                         .iov_base = dns_buffer,
82                 },
83                 {
84                         .iov_base = &q,
85                         .iov_len = sizeof(q),
86                 }
87         };
88         int len;
89
90         h.questions = cpu_to_be16(1);
91         q.class = cpu_to_be16((multicast ? 0 : CLASS_UNICAST) | 1);
92         q.type = cpu_to_be16(type);
93
94         len = dn_comp(question, (void *) dns_buffer, sizeof(dns_buffer), NULL, NULL);
95         if (len < 1)
96                 return;
97
98         iov[1].iov_len = len;
99
100         DBG(1, "Q <- %s %s\n", dns_type_string(type), question);
101         if (interface_send_packet(iface, NULL, iov, ARRAY_SIZE(iov)) < 0)
102                 perror("failed to send question :");
103 }
104
105
106 struct dns_reply {
107         int type;
108         struct dns_answer a;
109         uint16_t rdlength;
110         uint8_t *rdata;
111         char *buffer;
112 };
113
114 static int dns_answer_cnt;
115
116 void
117 dns_init_answer(void)
118 {
119         dns_answer_cnt = 0;
120         blob_buf_init(&ans_buf, 0);
121 }
122
123 void
124 dns_add_answer(int type, const uint8_t *rdata, uint16_t rdlength, int ttl)
125 {
126         struct blob_attr *attr;
127         struct dns_answer *a;
128
129         attr = blob_new(&ans_buf, 0, sizeof(*a) + rdlength);
130         a = blob_data(attr);
131         a->type = cpu_to_be16(type);
132         a->class = cpu_to_be16(1);
133         a->ttl = cpu_to_be32(ttl);
134         a->rdlength = cpu_to_be16(rdlength);
135         memcpy(a + 1, rdata, rdlength);
136
137         dns_answer_cnt++;
138 }
139
140 void
141 dns_send_answer(struct interface *iface, struct sockaddr *to, const char *answer)
142 {
143         uint8_t buffer[256];
144         struct blob_attr *attr;
145         struct dns_header h = { 0 };
146         struct iovec *iov;
147         int answer_len, rem;
148         int n_iov = 0;
149
150         if (!dns_answer_cnt)
151                 return;
152
153         h.answers = cpu_to_be16(dns_answer_cnt);
154         h.flags = cpu_to_be16(0x8400);
155
156         iov = alloca(sizeof(struct iovec) * ((dns_answer_cnt * 2) + 1));
157
158         iov[n_iov].iov_base = &h;
159         iov[n_iov].iov_len = sizeof(struct dns_header);
160         n_iov++;
161
162         answer_len = dn_comp(answer, buffer, sizeof(buffer), NULL, NULL);
163         if (answer_len < 1)
164                 return;
165
166         blob_for_each_attr(attr, ans_buf.head, rem) {
167                 struct dns_answer *a = blob_data(attr);
168
169                 iov[n_iov].iov_base = buffer;
170                 iov[n_iov].iov_len = answer_len;
171                 n_iov++;
172
173                 iov[n_iov].iov_base = blob_data(attr);
174                 iov[n_iov].iov_len = blob_len(attr);
175                 n_iov++;
176
177                 DBG(1, "A <- %s %s\n", dns_type_string(be16_to_cpu(a->type)), answer);
178         }
179
180         if (interface_send_packet(iface, to, iov, n_iov) < 0)
181                 fprintf(stderr, "failed to send question\n");
182 }
183
184 void
185 dns_reply_a(struct interface *iface, struct sockaddr *to, int ttl)
186 {
187         struct ifaddrs *ifap, *ifa;
188         struct sockaddr_in *sa;
189         struct sockaddr_in6 *sa6;
190
191         getifaddrs(&ifap);
192
193         dns_init_answer();
194         for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
195                 if (strcmp(ifa->ifa_name, iface->name))
196                         continue;
197                 if (ifa->ifa_addr->sa_family == AF_INET) {
198                         sa = (struct sockaddr_in *) ifa->ifa_addr;
199                         dns_add_answer(TYPE_A, (uint8_t *) &sa->sin_addr, 4, ttl);
200                 }
201                 if (ifa->ifa_addr->sa_family == AF_INET6) {
202                         uint8_t ll_prefix[] = {0xfe, 0x80 };
203                         sa6 = (struct sockaddr_in6 *) ifa->ifa_addr;
204                         if (!memcmp(&sa6->sin6_addr, &ll_prefix, 2))
205                                 dns_add_answer(TYPE_AAAA, (uint8_t *) &sa6->sin6_addr, 16, ttl);
206                 }
207         }
208         dns_send_answer(iface, to, mdns_hostname_local);
209
210         freeifaddrs(ifap);
211 }
212
213 static int
214 scan_name(const uint8_t *buffer, int len)
215 {
216         int offset = 0;
217
218         while (len && (*buffer != '\0')) {
219                 int l = *buffer;
220
221                 if (IS_COMPRESSED(l))
222                         return offset + 2;
223
224                 len -= l + 1;
225                 offset += l + 1;
226                 buffer += l + 1;
227         }
228
229         if (!len || !offset || (*buffer != '\0'))
230                 return -1;
231
232         return offset + 1;
233 }
234
235 static struct dns_header*
236 dns_consume_header(uint8_t **data, int *len)
237 {
238         struct dns_header *h = (struct dns_header *) *data;
239         uint16_t *swap = (uint16_t *) h;
240         int endianess = 6;
241
242         if (*len < sizeof(struct dns_header))
243                 return NULL;
244
245         while (endianess--) {
246                 *swap = be16_to_cpu(*swap);
247                 swap++;
248         }
249
250         *len -= sizeof(struct dns_header);
251         *data += sizeof(struct dns_header);
252
253         return h;
254 }
255
256 static struct dns_question*
257 dns_consume_question(uint8_t **data, int *len)
258 {
259         struct dns_question *q = (struct dns_question *) *data;
260         uint16_t *swap = (uint16_t *) q;
261         int endianess = 2;
262
263         if (*len < sizeof(struct dns_question))
264                 return NULL;
265
266         while (endianess--) {
267                 *swap = be16_to_cpu(*swap);
268                 swap++;
269         }
270
271         *len -= sizeof(struct dns_question);
272         *data += sizeof(struct dns_question);
273
274         return q;
275 }
276
277 static struct dns_answer*
278 dns_consume_answer(uint8_t **data, int *len)
279 {
280         struct dns_answer *a = (struct dns_answer *) *data;
281
282         if (*len < sizeof(struct dns_answer))
283                 return NULL;
284
285         a->type = be16_to_cpu(a->type);
286         a->class = be16_to_cpu(a->class);
287         a->ttl = be32_to_cpu(a->ttl);
288         a->rdlength = be16_to_cpu(a->rdlength);
289
290         *len -= sizeof(struct dns_answer);
291         *data += sizeof(struct dns_answer);
292
293         return a;
294 }
295
296 static char *
297 dns_consume_name(const uint8_t *base, int blen, uint8_t **data, int *len)
298 {
299         int nlen = scan_name(*data, *len);
300
301         if (nlen < 1)
302                 return NULL;
303
304         if (dn_expand(base, base + blen, *data, name_buffer, MAX_NAME_LEN) < 0) {
305                 perror("dns_consume_name/dn_expand");
306                 return NULL;
307         }
308
309         *len -= nlen;
310         *data += nlen;
311
312         return name_buffer;
313 }
314
315 static int
316 parse_answer(struct interface *iface, uint8_t *buffer, int len, uint8_t **b, int *rlen, int cache)
317 {
318         char *name = dns_consume_name(buffer, len, b, rlen);
319         struct dns_answer *a;
320         uint8_t *rdata;
321
322         if (!name) {
323                 fprintf(stderr, "dropping: bad question\n");
324                 return -1;
325         }
326
327         a = dns_consume_answer(b, rlen);
328         if (!a) {
329                 fprintf(stderr, "dropping: bad question\n");
330                 return -1;
331         }
332
333         if ((a->class & ~CLASS_FLUSH) != CLASS_IN)
334                 return -1;
335
336         rdata = *b;
337         if (a->rdlength > *rlen) {
338                 fprintf(stderr, "dropping: bad question\n");
339                 return -1;
340         }
341
342         *rlen -= a->rdlength;
343         *b += a->rdlength;
344
345         if (cache)
346                 cache_answer(iface, buffer, len, name, a, rdata, a->class & CLASS_FLUSH);
347
348         return 0;
349 }
350
351 static void
352 parse_question(struct interface *iface, char *name, struct dns_question *q)
353 {
354         char *host;
355
356         if ((q->class & CLASS_UNICAST) && iface->multicast)
357                 iface = iface->peer;
358
359         DBG(1, "Q -> %s %s\n", dns_type_string(q->type), name);
360
361         switch (q->type) {
362         case TYPE_ANY:
363                 if (!strcmp(name, mdns_hostname_local)) {
364                         service_reply(iface, NULL, announce_ttl);
365                         dns_reply_a(iface, NULL, announce_ttl);
366                 }
367                 break;
368
369         case TYPE_PTR:
370                 if (!strcmp(name, sdudp)) {
371                         service_announce_services(iface, announce_ttl);
372                 } else {
373                         /* First dot separates instance name from the rest */
374                         char *dot = strchr(name, '.');
375                         /* Length of queried instance */
376                         size_t len = dot ? dot - name : 0;
377
378                         /* Make sure it's query for the instance name we use */
379                         if (len && len == strlen(mdns_hostname) &&
380                             !strncmp(name, mdns_hostname, len))
381                                 service_reply(iface, dot + 1, announce_ttl);
382                 }
383                 break;
384
385         case TYPE_AAAA:
386         case TYPE_A:
387                 host = strstr(name, ".local");
388                 if (host)
389                         *host = '\0';
390                 if (!strcmp(mdns_hostname, name))
391                         dns_reply_a(iface, NULL, announce_ttl);
392                 break;
393         };
394 }
395
396 void
397 dns_handle_packet(struct interface *iface, struct sockaddr *s, uint16_t port, uint8_t *buffer, int len)
398 {
399         struct dns_header *h;
400         uint8_t *b = buffer;
401         int rlen = len;
402
403         h = dns_consume_header(&b, &rlen);
404         if (!h) {
405                 fprintf(stderr, "dropping: bad header\n");
406                 return;
407         }
408
409         if (h->questions && !iface->multicast && port != 5353)
410                 /* silently drop unicast questions that dont originate from port 5353 */
411                 return;
412
413         while (h->questions-- > 0) {
414                 char *name = dns_consume_name(buffer, len, &b, &rlen);
415                 struct dns_question *q;
416
417                 if (!name) {
418                         fprintf(stderr, "dropping: bad name\n");
419                         return;
420                 }
421
422                 q = dns_consume_question(&b, &rlen);
423                 if (!q) {
424                         fprintf(stderr, "dropping: bad question\n");
425                         return;
426                 }
427
428                 if (!(h->flags & FLAG_RESPONSE))
429                         parse_question(iface, name, q);
430         }
431
432         if (!(h->flags & FLAG_RESPONSE))
433                 return;
434
435         while (h->answers-- > 0)
436                 if (parse_answer(iface, buffer, len, &b, &rlen, 1))
437                         return;
438
439         while (h->authority-- > 0)
440                 if (parse_answer(iface, buffer, len, &b, &rlen, 1))
441                         return;
442
443         while (h->additional-- > 0)
444                 if (parse_answer(iface, buffer, len, &b, &rlen, 1))
445                         return;
446
447 }