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
, struct sockaddr
*to
,
72 const char *question
, int type
, int multicast
)
74 static struct dns_header h
;
75 static struct dns_question q
;
76 static struct iovec iov
[] = {
82 .iov_base
= dns_buffer
,
91 h
.questions
= cpu_to_be16(1);
92 q
.class = cpu_to_be16((multicast
? 0 : CLASS_UNICAST
) | 1);
93 q
.type
= cpu_to_be16(type
);
95 len
= dn_comp(question
, (void *) dns_buffer
, sizeof(dns_buffer
), NULL
, NULL
);
101 DBG(1, "Q <- %s %s\n", dns_type_string(type
), question
);
102 if (interface_send_packet(iface
, to
, iov
, ARRAY_SIZE(iov
)) < 0)
103 perror("failed to send question");
115 static int dns_answer_cnt
;
118 dns_init_answer(void)
121 blob_buf_init(&ans_buf
, 0);
125 dns_add_answer(int type
, const uint8_t *rdata
, uint16_t rdlength
, int ttl
)
127 struct blob_attr
*attr
;
128 struct dns_answer
*a
;
130 attr
= blob_new(&ans_buf
, 0, sizeof(*a
) + rdlength
);
132 a
->type
= cpu_to_be16(type
);
133 a
->class = cpu_to_be16(1);
134 a
->ttl
= cpu_to_be32(ttl
);
135 a
->rdlength
= cpu_to_be16(rdlength
);
136 memcpy(a
+ 1, rdata
, rdlength
);
142 dns_send_answer(struct interface
*iface
, struct sockaddr
*to
, const char *answer
)
145 struct blob_attr
*attr
;
146 struct dns_header h
= { 0 };
154 h
.answers
= cpu_to_be16(dns_answer_cnt
);
155 h
.flags
= cpu_to_be16(0x8400);
157 iov
= alloca(sizeof(struct iovec
) * ((dns_answer_cnt
* 2) + 1));
159 iov
[n_iov
].iov_base
= &h
;
160 iov
[n_iov
].iov_len
= sizeof(struct dns_header
);
163 answer_len
= dn_comp(answer
, buffer
, sizeof(buffer
), NULL
, NULL
);
167 blob_for_each_attr(attr
, ans_buf
.head
, rem
) {
168 struct dns_answer
*a
= blob_data(attr
);
170 iov
[n_iov
].iov_base
= buffer
;
171 iov
[n_iov
].iov_len
= answer_len
;
174 iov
[n_iov
].iov_base
= blob_data(attr
);
175 iov
[n_iov
].iov_len
= blob_len(attr
);
178 DBG(1, "A <- %s %s\n", dns_type_string(be16_to_cpu(a
->type
)), answer
);
181 if (interface_send_packet(iface
, to
, iov
, n_iov
) < 0)
182 perror("failed to send answer");
186 dns_reply_a(struct interface
*iface
, struct sockaddr
*to
, int ttl
)
188 struct ifaddrs
*ifap
, *ifa
;
189 struct sockaddr_in
*sa
;
190 struct sockaddr_in6
*sa6
;
195 for (ifa
= ifap
; ifa
; ifa
= ifa
->ifa_next
) {
196 if (strcmp(ifa
->ifa_name
, iface
->name
))
198 if (ifa
->ifa_addr
->sa_family
== AF_INET
) {
199 sa
= (struct sockaddr_in
*) ifa
->ifa_addr
;
200 dns_add_answer(TYPE_A
, (uint8_t *) &sa
->sin_addr
, 4, ttl
);
202 if (ifa
->ifa_addr
->sa_family
== AF_INET6
) {
203 sa6
= (struct sockaddr_in6
*) ifa
->ifa_addr
;
204 dns_add_answer(TYPE_AAAA
, (uint8_t *) &sa6
->sin6_addr
, 16, ttl
);
207 dns_send_answer(iface
, to
, mdns_hostname_local
);
213 scan_name(const uint8_t *buffer
, int len
)
217 while (len
&& (*buffer
!= '\0')) {
220 if (IS_COMPRESSED(l
))
223 if (l
+ 1 > len
) return -1;
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
;
240 if (*len
< sizeof(struct dns_header
))
243 h
->id
= be16_to_cpu(h
->id
);
244 h
->flags
= be16_to_cpu(h
->flags
);
245 h
->questions
= be16_to_cpu(h
->questions
);
246 h
->answers
= be16_to_cpu(h
->answers
);
247 h
->authority
= be16_to_cpu(h
->authority
);
248 h
->additional
= be16_to_cpu(h
->additional
);
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
;
261 if (*len
< sizeof(struct dns_question
))
264 q
->type
= be16_to_cpu(q
->type
);
265 q
->class = be16_to_cpu(q
->class);
267 *len
-= sizeof(struct dns_question
);
268 *data
+= sizeof(struct dns_question
);
273 static struct dns_answer
*
274 dns_consume_answer(uint8_t **data
, int *len
)
276 struct dns_answer
*a
= (struct dns_answer
*) *data
;
278 if (*len
< sizeof(struct dns_answer
))
281 a
->type
= be16_to_cpu(a
->type
);
282 a
->class = be16_to_cpu(a
->class);
283 a
->ttl
= be32_to_cpu(a
->ttl
);
284 a
->rdlength
= be16_to_cpu(a
->rdlength
);
286 *len
-= sizeof(struct dns_answer
);
287 *data
+= sizeof(struct dns_answer
);
293 dns_consume_name(const uint8_t *base
, int blen
, uint8_t **data
, int *len
)
295 int nlen
= scan_name(*data
, *len
);
300 if (dn_expand(base
, base
+ blen
, *data
, name_buffer
, MAX_NAME_LEN
) < 0) {
301 perror("dns_consume_name/dn_expand");
311 static int parse_answer(struct interface
*iface
, struct sockaddr
*from
,
312 uint8_t *buffer
, int len
, uint8_t **b
, int *rlen
,
315 char *name
= dns_consume_name(buffer
, len
, b
, rlen
);
316 struct dns_answer
*a
;
319 if (!name
|| *rlen
< 0) {
320 fprintf(stderr
, "dropping: bad question\n");
324 a
= dns_consume_answer(b
, rlen
);
326 fprintf(stderr
, "dropping: bad question\n");
330 if ((a
->class & ~CLASS_FLUSH
) != CLASS_IN
)
334 if (a
->rdlength
> *rlen
) {
335 fprintf(stderr
, "dropping: bad question\n");
339 *rlen
-= a
->rdlength
;
343 cache_answer(iface
, from
, buffer
, len
, name
, a
, rdata
, a
->class & CLASS_FLUSH
);
349 parse_question(struct interface
*iface
, struct sockaddr
*from
, char *name
, struct dns_question
*q
)
351 struct sockaddr
*to
= NULL
;
354 /* TODO: Multicast if more than one quarter of TTL has passed */
355 if (q
->class & CLASS_UNICAST
) {
357 if (iface
->multicast
)
361 DBG(1, "Q -> %s %s\n", dns_type_string(q
->type
), name
);
365 if (!strcmp(name
, mdns_hostname_local
)) {
366 dns_reply_a(iface
, to
, announce_ttl
);
367 service_reply(iface
, to
, NULL
, NULL
, announce_ttl
);
372 if (!strcmp(name
, C_DNS_SD
)) {
373 dns_reply_a(iface
, to
, announce_ttl
);
374 service_announce_services(iface
, to
, announce_ttl
);
376 if (name
[0] == '_') {
377 service_reply(iface
, to
, NULL
, name
, announce_ttl
);
379 /* First dot separates instance name from the rest */
380 char *dot
= strchr(name
, '.');
384 service_reply(iface
, to
, name
, dot
+ 1, announce_ttl
);
393 host
= strstr(name
, ".local");
396 if (!strcmp(umdns_host_label
, name
))
397 dns_reply_a(iface
, to
, announce_ttl
);
403 dns_handle_packet(struct interface
*iface
, struct sockaddr
*from
, uint16_t port
, uint8_t *buffer
, int len
)
405 struct dns_header
*h
;
409 h
= dns_consume_header(&b
, &rlen
);
411 fprintf(stderr
, "dropping: bad header\n");
415 if (h
->questions
&& !iface
->multicast
&& port
!= MCAST_PORT
)
416 /* silently drop unicast questions that dont originate from port 5353 */
419 while (h
->questions
-- > 0) {
420 char *name
= dns_consume_name(buffer
, len
, &b
, &rlen
);
421 struct dns_question
*q
;
423 if (!name
|| rlen
< 0) {
424 fprintf(stderr
, "dropping: bad name\n");
428 q
= dns_consume_question(&b
, &rlen
);
430 fprintf(stderr
, "dropping: bad question\n");
434 if (!(h
->flags
& FLAG_RESPONSE
))
435 parse_question(iface
, from
, name
, q
);
438 if (!(h
->flags
& FLAG_RESPONSE
))
441 while (h
->answers
-- > 0)
442 if (parse_answer(iface
, from
, buffer
, len
, &b
, &rlen
, 1))
445 while (h
->authority
-- > 0)
446 if (parse_answer(iface
, from
, buffer
, len
, &b
, &rlen
, 1))
449 while (h
->additional
-- > 0)
450 if (parse_answer(iface
, from
, buffer
, len
, &b
, &rlen
, 1))