Fix error messages when sending packet fails
[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                 perror("failed to send answer");
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, struct sockaddr *from, char *name, struct dns_question *q)
353 {
354         struct sockaddr *to;
355         char *host;
356
357         /* TODO: Multicast if more than one quarter of TTL has passed */
358         if ((q->class & CLASS_UNICAST) && iface->multicast) {
359                 iface = iface->peer;
360                 to = from;
361         } else {
362                 to = NULL;
363         }
364
365         DBG(1, "Q -> %s %s\n", dns_type_string(q->type), name);
366
367         switch (q->type) {
368         case TYPE_ANY:
369                 if (!strcmp(name, mdns_hostname_local)) {
370                         dns_reply_a(iface, to, announce_ttl);
371                         service_reply(iface, to, NULL, announce_ttl);
372                 }
373                 break;
374
375         case TYPE_PTR:
376                 if (!strcmp(name, sdudp)) {
377                         dns_reply_a(iface, to, announce_ttl);
378                         service_announce_services(iface, to, announce_ttl);
379                 } else {
380                         /* First dot separates instance name from the rest */
381                         char *dot = strchr(name, '.');
382                         /* Length of queried instance */
383                         size_t len = dot ? dot - name : 0;
384
385                         /* Make sure it's query for the instance name we use */
386                         if (len && len == strlen(mdns_hostname) &&
387                             !strncmp(name, mdns_hostname, len))
388                                 service_reply(iface, to, dot + 1, announce_ttl);
389                 }
390                 break;
391
392         case TYPE_AAAA:
393         case TYPE_A:
394                 host = strstr(name, ".local");
395                 if (host)
396                         *host = '\0';
397                 if (!strcmp(mdns_hostname, name))
398                         dns_reply_a(iface, to, announce_ttl);
399                 break;
400         };
401 }
402
403 void
404 dns_handle_packet(struct interface *iface, struct sockaddr *s, uint16_t port, uint8_t *buffer, int len)
405 {
406         struct dns_header *h;
407         uint8_t *b = buffer;
408         int rlen = len;
409
410         h = dns_consume_header(&b, &rlen);
411         if (!h) {
412                 fprintf(stderr, "dropping: bad header\n");
413                 return;
414         }
415
416         if (h->questions && !iface->multicast && port != 5353)
417                 /* silently drop unicast questions that dont originate from port 5353 */
418                 return;
419
420         while (h->questions-- > 0) {
421                 char *name = dns_consume_name(buffer, len, &b, &rlen);
422                 struct dns_question *q;
423
424                 if (!name) {
425                         fprintf(stderr, "dropping: bad name\n");
426                         return;
427                 }
428
429                 q = dns_consume_question(&b, &rlen);
430                 if (!q) {
431                         fprintf(stderr, "dropping: bad question\n");
432                         return;
433                 }
434
435                 if (!(h->flags & FLAG_RESPONSE))
436                         parse_question(iface, s, name, q);
437         }
438
439         if (!(h->flags & FLAG_RESPONSE))
440                 return;
441
442         while (h->answers-- > 0)
443                 if (parse_answer(iface, buffer, len, &b, &rlen, 1))
444                         return;
445
446         while (h->authority-- > 0)
447                 if (parse_answer(iface, buffer, len, &b, &rlen, 1))
448                         return;
449
450         while (h->additional-- > 0)
451                 if (parse_answer(iface, buffer, len, &b, &rlen, 1))
452                         return;
453
454 }