2 * Copyright (C) 2014 John Crispin <blogic@openwrt.org>
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
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.
14 #include <sys/types.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>
31 #include <libubox/uloop.h>
32 #include <libubox/usock.h>
33 #include <libubox/utils.h>
40 #include "interface.h"
42 static char name_buffer[MAX_NAME_LEN + 1];
43 static char dns_buffer[MAX_NAME_LEN];
44 static struct blob_buf ans_buf;
47 dns_type_string(uint16_t type)
54 { TYPE_AAAA, "AAAA" },
62 for (i = 0; i < ARRAY_SIZE(type_str); i++) {
63 if (type == type_str[i].type)
64 return type_str[i].str;
71 dns_send_question(struct interface *iface, const char *question, int type, int multicast)
73 static struct dns_header h;
74 static struct dns_question q;
75 static struct iovec iov[] = {
81 .iov_base = dns_buffer,
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);
94 len = dn_comp(question, (void *) dns_buffer, sizeof(dns_buffer), NULL, NULL);
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");
114 static int dns_answer_cnt;
117 dns_init_answer(void)
120 blob_buf_init(&ans_buf, 0);
124 dns_add_answer(int type, const uint8_t *rdata, uint16_t rdlength, int ttl)
126 struct blob_attr *attr;
127 struct dns_answer *a;
129 attr = blob_new(&ans_buf, 0, sizeof(*a) + rdlength);
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);
141 dns_send_answer(struct interface *iface, struct sockaddr *to, const char *answer)
144 struct blob_attr *attr;
145 struct dns_header h = { 0 };
153 h.answers = cpu_to_be16(dns_answer_cnt);
154 h.flags = cpu_to_be16(0x8400);
156 iov = alloca(sizeof(struct iovec) * ((dns_answer_cnt * 2) + 1));
158 iov[n_iov].iov_base = &h;
159 iov[n_iov].iov_len = sizeof(struct dns_header);
162 answer_len = dn_comp(answer, buffer, sizeof(buffer), NULL, NULL);
166 blob_for_each_attr(attr, ans_buf.head, rem) {
167 struct dns_answer *a = blob_data(attr);
169 iov[n_iov].iov_base = buffer;
170 iov[n_iov].iov_len = answer_len;
173 iov[n_iov].iov_base = blob_data(attr);
174 iov[n_iov].iov_len = blob_len(attr);
177 DBG(1, "A <- %s %s\n", dns_type_string(be16_to_cpu(a->type)), answer);
180 if (interface_send_packet(iface, to, iov, n_iov) < 0)
181 perror("failed to send answer");
185 dns_reply_a(struct interface *iface, struct sockaddr *to, int ttl)
187 struct ifaddrs *ifap, *ifa;
188 struct sockaddr_in *sa;
189 struct sockaddr_in6 *sa6;
194 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
195 if (strcmp(ifa->ifa_name, iface->name))
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);
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);
208 dns_send_answer(iface, to, mdns_hostname_local);
214 scan_name(const uint8_t *buffer, int len)
218 while (len && (*buffer != '\0')) {
221 if (IS_COMPRESSED(l))
229 if (!len || !offset || (*buffer != '\0'))
235 static struct dns_header*
236 dns_consume_header(uint8_t **data, int *len)
238 struct dns_header *h = (struct dns_header *) *data;
239 uint16_t *swap = (uint16_t *) h;
242 if (*len < sizeof(struct dns_header))
245 while (endianess--) {
246 *swap = be16_to_cpu(*swap);
250 *len -= sizeof(struct dns_header);
251 *data += sizeof(struct dns_header);
256 static struct dns_question*
257 dns_consume_question(uint8_t **data, int *len)
259 struct dns_question *q = (struct dns_question *) *data;
260 uint16_t *swap = (uint16_t *) q;
263 if (*len < sizeof(struct dns_question))
266 while (endianess--) {
267 *swap = be16_to_cpu(*swap);
271 *len -= sizeof(struct dns_question);
272 *data += sizeof(struct dns_question);
277 static struct dns_answer*
278 dns_consume_answer(uint8_t **data, int *len)
280 struct dns_answer *a = (struct dns_answer *) *data;
282 if (*len < sizeof(struct dns_answer))
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);
290 *len -= sizeof(struct dns_answer);
291 *data += sizeof(struct dns_answer);
297 dns_consume_name(const uint8_t *base, int blen, uint8_t **data, int *len)
299 int nlen = scan_name(*data, *len);
304 if (dn_expand(base, base + blen, *data, name_buffer, MAX_NAME_LEN) < 0) {
305 perror("dns_consume_name/dn_expand");
316 parse_answer(struct interface *iface, uint8_t *buffer, int len, uint8_t **b, int *rlen, int cache)
318 char *name = dns_consume_name(buffer, len, b, rlen);
319 struct dns_answer *a;
323 fprintf(stderr, "dropping: bad question\n");
327 a = dns_consume_answer(b, rlen);
329 fprintf(stderr, "dropping: bad question\n");
333 if ((a->class & ~CLASS_FLUSH) != CLASS_IN)
337 if (a->rdlength > *rlen) {
338 fprintf(stderr, "dropping: bad question\n");
342 *rlen -= a->rdlength;
346 cache_answer(iface, buffer, len, name, a, rdata, a->class & CLASS_FLUSH);
352 parse_question(struct interface *iface, struct sockaddr *from, char *name, struct dns_question *q)
357 /* TODO: Multicast if more than one quarter of TTL has passed */
358 if ((q->class & CLASS_UNICAST) && iface->multicast) {
365 DBG(1, "Q -> %s %s\n", dns_type_string(q->type), name);
369 if (!strcmp(name, mdns_hostname_local)) {
370 dns_reply_a(iface, to, announce_ttl);
371 service_reply(iface, to, NULL, announce_ttl);
376 if (!strcmp(name, sdudp)) {
377 dns_reply_a(iface, to, announce_ttl);
378 service_announce_services(iface, to, announce_ttl);
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;
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);
394 host = strstr(name, ".local");
397 if (!strcmp(mdns_hostname, name))
398 dns_reply_a(iface, to, announce_ttl);
404 dns_handle_packet(struct interface *iface, struct sockaddr *s, uint16_t port, uint8_t *buffer, int len)
406 struct dns_header *h;
410 h = dns_consume_header(&b, &rlen);
412 fprintf(stderr, "dropping: bad header\n");
416 if (h->questions && !iface->multicast && port != 5353)
417 /* silently drop unicast questions that dont originate from port 5353 */
420 while (h->questions-- > 0) {
421 char *name = dns_consume_name(buffer, len, &b, &rlen);
422 struct dns_question *q;
425 fprintf(stderr, "dropping: bad name\n");
429 q = dns_consume_question(&b, &rlen);
431 fprintf(stderr, "dropping: bad question\n");
435 if (!(h->flags & FLAG_RESPONSE))
436 parse_question(iface, s, name, q);
439 if (!(h->flags & FLAG_RESPONSE))
442 while (h->answers-- > 0)
443 if (parse_answer(iface, buffer, len, &b, &rlen, 1))
446 while (h->authority-- > 0)
447 if (parse_answer(iface, buffer, len, &b, &rlen, 1))
450 while (h->additional-- > 0)
451 if (parse_answer(iface, buffer, len, &b, &rlen, 1))