interface: include libubox/utils.h
[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 <arpa/nameser.h>
26 #include <resolv.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include <libubox/uloop.h>
31 #include <libubox/usock.h>
32 #include <libubox/utils.h>
33
34 #include "announce.h"
35 #include "util.h"
36 #include "dns.h"
37 #include "cache.h"
38 #include "service.h"
39 #include "interface.h"
40
41 static char name_buffer[MAX_NAME_LEN + 1];
42 static char dns_buffer[MAX_NAME_LEN];
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, int unicast)
71 {
72         static struct dns_header h;
73         static struct dns_question q;
74         static struct iovec iov[] = {
75                 {
76                         .iov_base = &h,
77                         .iov_len = sizeof(h),
78                 },
79                 {
80                         .iov_base = dns_buffer,
81                 },
82                 {
83                         .iov_base = &q,
84                         .iov_len = sizeof(q),
85                 }
86         };
87         int len;
88
89         h.questions = cpu_to_be16(1);
90         q.class = cpu_to_be16(((unicast) ? (CLASS_UNICAST) : (0))  | 1);
91         q.type = cpu_to_be16(type);
92
93         len = dn_comp(question, (void *) dns_buffer, sizeof(dns_buffer), NULL, NULL);
94         if (len < 1)
95                 return;
96
97         iov[1].iov_len = len;
98
99         DBG(1, "Q <- %s %s\n", dns_type_string(type), question);
100         if (interface_send_packet(iface, iov, ARRAY_SIZE(iov)) < 0)
101                 perror("failed to send question :");
102 }
103
104
105 struct dns_reply {
106         int type;
107         struct dns_answer a;
108         uint16_t rdlength;
109         uint8_t *rdata;
110         char *buffer;
111 };
112
113 static int dns_answer_cnt;
114
115 void
116 dns_init_answer(void)
117 {
118         dns_answer_cnt = 0;
119         blob_buf_init(&ans_buf, 0);
120 }
121
122 void
123 dns_add_answer(int type, const uint8_t *rdata, uint16_t rdlength, int ttl)
124 {
125         struct blob_attr *attr;
126         struct dns_answer *a;
127
128         attr = blob_new(&ans_buf, 0, sizeof(*a) + rdlength);
129         a = blob_data(attr);
130         a->type = cpu_to_be16(type);
131         a->class = cpu_to_be16(1);
132         a->ttl = cpu_to_be32(ttl);
133         a->rdlength = cpu_to_be16(rdlength);
134         memcpy(a + 1, rdata, rdlength);
135
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 blob_attr *attr;
144         struct dns_header h = { 0 };
145         struct iovec *iov;
146         int answer_len, rem;
147         int n_iov = 0;
148
149         if (!dns_answer_cnt)
150                 return;
151
152         h.answers = cpu_to_be16(dns_answer_cnt);
153         h.flags = cpu_to_be16(0x8400);
154
155         iov = alloca(sizeof(struct iovec) * ((dns_answer_cnt * 2) + 1));
156
157         iov[n_iov].iov_base = &h;
158         iov[n_iov].iov_len = sizeof(struct dns_header);
159         n_iov++;
160
161         answer_len = dn_comp(answer, buffer, sizeof(buffer), NULL, NULL);
162         if (answer_len < 1)
163                 return;
164
165         blob_for_each_attr(attr, ans_buf.head, rem) {
166                 struct dns_answer *a = blob_data(attr);
167
168                 iov[n_iov].iov_base = buffer;
169                 iov[n_iov].iov_len = answer_len;
170                 n_iov++;
171
172                 iov[n_iov].iov_base = blob_data(attr);
173                 iov[n_iov].iov_len = blob_len(attr);
174                 n_iov++;
175
176                 DBG(1, "A <- %s %s\n", dns_type_string(be16_to_cpu(a->type)), answer);
177         }
178
179         if (interface_send_packet(iface, iov, n_iov) < 0)
180                 fprintf(stderr, "failed to send question\n");
181 }
182
183 static int
184 scan_name(const uint8_t *buffer, int len)
185 {
186         int offset = 0;
187
188         while (len && (*buffer != '\0')) {
189                 int l = *buffer;
190
191                 if (IS_COMPRESSED(l))
192                         return offset + 2;
193
194                 len -= l + 1;
195                 offset += l + 1;
196                 buffer += l + 1;
197         }
198
199         if (!len || !offset || (*buffer != '\0'))
200                 return -1;
201
202         return offset + 1;
203 }
204
205 static struct dns_header*
206 dns_consume_header(uint8_t **data, int *len)
207 {
208         struct dns_header *h = (struct dns_header *) *data;
209         uint16_t *swap = (uint16_t *) h;
210         int endianess = 6;
211
212         if (*len < sizeof(struct dns_header))
213                 return NULL;
214
215         while (endianess--) {
216                 *swap = be16_to_cpu(*swap);
217                 swap++;
218         }
219
220         *len -= sizeof(struct dns_header);
221         *data += sizeof(struct dns_header);
222
223         return h;
224 }
225
226 static struct dns_question*
227 dns_consume_question(uint8_t **data, int *len)
228 {
229         struct dns_question *q = (struct dns_question *) *data;
230         uint16_t *swap = (uint16_t *) q;
231         int endianess = 2;
232
233         if (*len < sizeof(struct dns_question))
234                 return NULL;
235
236         while (endianess--) {
237                 *swap = be16_to_cpu(*swap);
238                 swap++;
239         }
240
241         *len -= sizeof(struct dns_question);
242         *data += sizeof(struct dns_question);
243
244         return q;
245 }
246
247 static struct dns_answer*
248 dns_consume_answer(uint8_t **data, int *len)
249 {
250         struct dns_answer *a = (struct dns_answer *) *data;
251
252         if (*len < sizeof(struct dns_answer))
253                 return NULL;
254
255         a->type = be16_to_cpu(a->type);
256         a->class = be16_to_cpu(a->class);
257         a->ttl = be32_to_cpu(a->ttl);
258         a->rdlength = be16_to_cpu(a->rdlength);
259
260         *len -= sizeof(struct dns_answer);
261         *data += sizeof(struct dns_answer);
262
263         return a;
264 }
265
266 static char *
267 dns_consume_name(const uint8_t *base, int blen, uint8_t **data, int *len)
268 {
269         int nlen = scan_name(*data, *len);
270
271         if (nlen < 1)
272                 return NULL;
273
274         if (dn_expand(base, base + blen, *data, name_buffer, MAX_NAME_LEN) < 0) {
275                 perror("dns_consume_name/dn_expand");
276                 return NULL;
277         }
278
279         *len -= nlen;
280         *data += nlen;
281
282         return name_buffer;
283 }
284
285 static int
286 parse_answer(struct interface *iface, uint8_t *buffer, int len, uint8_t **b, int *rlen, int cache)
287 {
288         char *name = dns_consume_name(buffer, len, b, rlen);
289         struct dns_answer *a;
290         uint8_t *rdata;
291
292         if (!name) {
293                 fprintf(stderr, "dropping: bad question\n");
294                 return -1;
295         }
296
297         a = dns_consume_answer(b, rlen);
298         if (!a) {
299                 fprintf(stderr, "dropping: bad question\n");
300                 return -1;
301         }
302
303         if ((a->class & ~CLASS_FLUSH) != CLASS_IN)
304                 return -1;
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, a->class & CLASS_FLUSH);
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         if ((q->class & CLASS_UNICAST) && iface->multicast)
327                 iface = iface->peer;
328
329         DBG(1, "Q -> %s %s\n", dns_type_string(q->type), name);
330
331         switch (q->type) {
332         case TYPE_ANY:
333                 if (!strcmp(name, mdns_hostname_local))
334                         service_reply(iface, NULL, announce_ttl);
335                 break;
336
337         case TYPE_PTR:
338                 service_announce_services(iface, name, announce_ttl);
339                 service_reply(iface, name, announce_ttl);
340                 break;
341
342         case TYPE_AAAA:
343         case TYPE_A:
344                 host = strstr(name, ".local");
345                 if (host)
346                         *host = '\0';
347                 if (!strcmp(mdns_hostname, name))
348                         service_reply_a(iface, announce_ttl);
349                 break;
350         };
351 }
352
353 void
354 dns_handle_packet(struct interface *iface, struct sockaddr *s, uint16_t port, uint8_t *buffer, int len)
355 {
356         struct dns_header *h;
357         uint8_t *b = buffer;
358         int rlen = len;
359
360         h = dns_consume_header(&b, &rlen);
361         if (!h) {
362                 fprintf(stderr, "dropping: bad header\n");
363                 return;
364         }
365
366         if (h->questions && !iface->multicast && port != 5353)
367                 // silently drop unicast questions that dont originate from port 5353  
368                 return;
369
370         while (h->questions-- > 0) {
371                 char *name = dns_consume_name(buffer, len, &b, &rlen);
372                 struct dns_question *q;
373
374                 if (!name) {
375                         fprintf(stderr, "dropping: bad name\n");
376                         return;
377                 }
378
379                 q = dns_consume_question(&b, &rlen);
380                 if (!q) {
381                         fprintf(stderr, "dropping: bad question\n");
382                         return;
383                 }
384
385                 if (!(h->flags & FLAG_RESPONSE))
386                         parse_question(iface, name, q);
387         }
388
389         if (!(h->flags & FLAG_RESPONSE))
390                 return;
391
392         while (h->answers-- > 0)
393                 if (parse_answer(iface, buffer, len, &b, &rlen, 1))
394                         return;
395
396         while (h->authority-- > 0)
397                 if (parse_answer(iface, buffer, len, &b, &rlen, 1))
398                         return;
399
400         while (h->additional-- > 0)
401                 if (parse_answer(iface, buffer, len, &b, &rlen, 1))
402                         return;
403
404 }