• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/mdnsd/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 #define _GNU_SOURCE
 15 #include <sys/types.h>
 16 #include <sys/stat.h>
 17 
 18 #include <fcntl.h>
 19 #include <ifaddrs.h>
 20 #include <time.h>
 21 #include <stdio.h>
 22 #include <unistd.h>
 23 #include <sys/types.h>
 24 #include <sys/socket.h>
 25 #include <netinet/in.h>
 26 #include <arpa/inet.h>
 27 #include <arpa/nameser.h>
 28 #include <resolv.h>
 29 #include <stdlib.h>
 30 #include <string.h>
 31 
 32 #include <libubox/uloop.h>
 33 #include <libubox/usock.h>
 34 #include <libubox/utils.h>
 35 #include <libubox/avl-cmp.h>
 36 
 37 #include "announce.h"
 38 #include "util.h"
 39 #include "dns.h"
 40 #include "cache.h"
 41 #include "service.h"
 42 #include "interface.h"
 43 
 44 #define QUERY_BATCH_SIZE        16
 45 
 46 struct query_entry {
 47         struct avl_node node;
 48         uint16_t type;
 49         char name[];
 50 };
 51 
 52 static AVL_TREE(queries, avl_strcmp, true, NULL);
 53 static char name_buffer[MAX_NAME_LEN + 1];
 54 
 55 static struct {
 56         struct dns_header h;
 57         unsigned char data[9000 - sizeof(struct dns_header)];
 58 } __attribute__((packed)) pkt;
 59 static size_t pkt_len;
 60 static struct dns_question *pkt_q[32];
 61 static unsigned int pkt_n_q;
 62 static unsigned char *dnptrs[255];
 63 
 64 const char*
 65 dns_type_string(uint16_t type)
 66 {
 67         static const struct {
 68                 uint16_t type;
 69                 char str[5];
 70         } type_str[] = {
 71                 { TYPE_A, "A" },
 72                 { TYPE_AAAA, "AAAA" },
 73                 { TYPE_PTR, "PTR" },
 74                 { TYPE_TXT, "TXT" },
 75                 { TYPE_SRV, "SRV" },
 76                 { TYPE_ANY, "ANY" },
 77         };
 78         int i;
 79 
 80         for (i = 0; i < ARRAY_SIZE(type_str); i++) {
 81                 if (type == type_str[i].type)
 82                         return type_str[i].str;
 83         }
 84 
 85         return "N/A";
 86 }
 87 
 88 void dns_packet_init(void)
 89 {
 90         dnptrs[0] = (unsigned char *)&pkt;
 91         dnptrs[1] = NULL;
 92         pkt_len = 0;
 93         pkt_n_q = 0;
 94         memset(&pkt.h, 0, sizeof(pkt.h));
 95 }
 96 
 97 static inline void *dns_packet_tail(size_t len)
 98 {
 99         if (pkt_len + len > sizeof(pkt.data))
100                 return NULL;
101 
102         return &pkt.data[pkt_len];
103 }
104 
105 static int
106 dns_packet_add_name(const char *name)
107 {
108         void *data;
109 
110         data = dns_packet_tail(MAX_NAME_LEN);
111         if (!data)
112                 return -1;
113 
114         return dn_comp(name, data, MAX_NAME_LEN, dnptrs, dnptrs + ARRAY_SIZE(dnptrs) - 1);
115 }
116 
117 static void *dns_packet_record_add(size_t data_len, const char *name)
118 {
119         void *data;
120         int len;
121 
122         len = dns_packet_add_name(name);
123         if (len < 1)
124                 return NULL;
125 
126         data = dns_packet_tail(len + data_len);
127         if (!data)
128                 return NULL;
129 
130         pkt_len += len + data_len;
131 
132         return data + len;
133 }
134 
135 bool dns_packet_question(const char *name, int type)
136 {
137         struct dns_question *q;
138 
139         q = dns_packet_record_add(sizeof(*q), name);
140         if (!q)
141                 return false;
142 
143         pkt.h.questions += cpu_to_be16(1);
144         pkt_q[pkt_n_q++] = q;
145         memset(q, 0, sizeof(*q));
146         q->class = cpu_to_be16(1);
147         q->type = cpu_to_be16(type);
148         DBG(1, "Q <- %s %s\n", dns_type_string(type), name);
149 
150         return true;
151 }
152 
153 void dns_packet_answer(const char *name, int type, const uint8_t *rdata, uint16_t rdlength, int ttl)
154 {
155         struct dns_answer *a;
156 
157         pkt.h.flags |= cpu_to_be16(0x8400);
158 
159         a = dns_packet_record_add(sizeof(*a) + rdlength, name);
160         if (!a)
161                 return;
162 
163         memset(a, 0, sizeof(*a));
164         a->type = cpu_to_be16(type);
165         a->class = cpu_to_be16(1);
166         a->ttl = cpu_to_be32(ttl);
167         a->rdlength = cpu_to_be16(rdlength);
168         memcpy(a + 1, rdata, rdlength);
169         DBG(1, "A <- %s %s\n", dns_type_string(be16_to_cpu(a->type)), name);
170 
171         pkt.h.answers += cpu_to_be16(1);
172 }
173 
174 static void dns_question_set_multicast(struct dns_question *q, bool val)
175 {
176         if (val)
177                 q->class &= ~cpu_to_be16(CLASS_UNICAST);
178         else
179                 q->class |= cpu_to_be16(CLASS_UNICAST);
180 }
181 
182 void dns_packet_send(struct interface *iface, struct sockaddr *to, bool query, int multicast)
183 {
184         struct iovec iov = {
185                 .iov_base = &pkt,
186                 .iov_len = sizeof(pkt.h) + pkt_len,
187         };
188         size_t i;
189 
190         if (query) {
191                 if (multicast < 0)
192                         multicast = iface->need_multicast;
193 
194                 for (i = 0; i < pkt_n_q; i++)
195                         dns_question_set_multicast(pkt_q[i], multicast);
196         }
197 
198         if (interface_send_packet(iface, to, &iov, 1) < 0)
199                 perror("failed to send answer");
200 }
201 
202 static void dns_packet_broadcast(void)
203 {
204         struct interface *iface;
205 
206         vlist_for_each_element(&interfaces, iface, node)
207                 dns_packet_send(iface, NULL, 1, -1);
208 }
209 
210 void
211 dns_send_question(struct interface *iface, struct sockaddr *to,
212                   const char *question, int type, int multicast)
213 {
214         dns_packet_init();
215         dns_packet_question(question, type);
216         dns_packet_send(iface, to, true, multicast);
217 }
218 
219 static void
220 dns_query_pending(struct uloop_timeout *t)
221 {
222         struct query_entry *e, *tmp;
223         int count = 0;
224 
225         dns_packet_init();
226         avl_remove_all_elements(&queries, e, node, tmp) {
227                 dns_packet_question(e->name, e->type);
228                 free(e);
229 
230                 if (++count < QUERY_BATCH_SIZE)
231                         continue;
232 
233                 count = 0;
234                 dns_packet_broadcast();
235                 dns_packet_init();
236         }
237 
238         if (count)
239                 dns_packet_broadcast();
240 }
241 
242 void dns_query(const char *name, uint16_t type)
243 {
244         static struct uloop_timeout timer = {
245                 .cb = dns_query_pending
246         };
247         struct query_entry *e;
248 
249         e = avl_find_element(&queries, name, e, node);
250         while (e) {
251                 if (e->type == type)
252                         return;
253 
254                 if (avl_is_last(&queries, &e->node))
255                         break;
256 
257                 e = avl_next_element(e, node);
258                 if (strcmp(e->name, name) != 0)
259                         break;
260         }
261 
262         e = calloc(1, sizeof(*e) + strlen(name) + 1);
263         e->type = type;
264         e->node.key = e->name;
265         strcpy(e->name, name);
266         avl_insert(&queries, &e->node);
267 
268         if (queries.count > QUERY_BATCH_SIZE)
269                 timer.cb(&timer);
270 
271         if (!timer.pending)
272                 uloop_timeout_set(&timer, 100);
273 }
274 
275 void
276 dns_reply_a(struct interface *iface, struct sockaddr *to, int ttl, const char *hostname)
277 {
278         struct ifaddrs *ifap, *ifa;
279         struct sockaddr_in *sa;
280         struct sockaddr_in6 *sa6;
281 
282         if (!hostname)
283                 hostname = mdns_hostname_local;
284 
285         if (getifaddrs(&ifap) < 0)
286                 return;
287 
288         dns_packet_init();
289         for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
290                 if (strcmp(ifa->ifa_name, iface->name))
291                         continue;
292                 if (!ifa->ifa_addr)
293                         continue;
294                 if (ifa->ifa_addr->sa_family == AF_INET) {
295                         sa = (struct sockaddr_in *) ifa->ifa_addr;
296                         dns_packet_answer(hostname, TYPE_A, (uint8_t *) &sa->sin_addr, 4, ttl);
297                 }
298                 if (ifa->ifa_addr->sa_family == AF_INET6) {
299                         sa6 = (struct sockaddr_in6 *) ifa->ifa_addr;
300                         dns_packet_answer(hostname, TYPE_AAAA, (uint8_t *) &sa6->sin6_addr, 16, ttl);
301                 }
302         }
303         freeifaddrs(ifap);
304 
305         dns_packet_send(iface, to, 0, 0);
306 }
307 
308 void
309 dns_reply_a_additional(struct interface *iface, struct sockaddr *to, int ttl)
310 {
311         struct hostname *h;
312 
313         vlist_for_each_element(&hostnames, h, node)
314                 dns_reply_a(iface, to, ttl, h->hostname);
315 }
316 
317 static int
318 scan_name(const uint8_t *buffer, int len)
319 {
320         int offset = 0;
321 
322         while (len && (*buffer != '\0')) {
323                 int l = *buffer;
324 
325                 if (IS_COMPRESSED(l))
326                         return offset + 2;
327 
328                 if (l + 1 > len) return -1;
329                 len -= l + 1;
330                 offset += l + 1;
331                 buffer += l + 1;
332         }
333 
334         if (!len || !offset || (*buffer != '\0'))
335                 return -1;
336 
337         return offset + 1;
338 }
339 
340 static struct dns_header*
341 dns_consume_header(uint8_t **data, int *len)
342 {
343         struct dns_header *h = (struct dns_header *) *data;
344 
345         if (*len < sizeof(struct dns_header))
346                 return NULL;
347 
348         h->id = be16_to_cpu(h->id);
349         h->flags = be16_to_cpu(h->flags);
350         h->questions = be16_to_cpu(h->questions);
351         h->answers = be16_to_cpu(h->answers);
352         h->authority = be16_to_cpu(h->authority);
353         h->additional = be16_to_cpu(h->additional);
354 
355         *len -= sizeof(struct dns_header);
356         *data += sizeof(struct dns_header);
357 
358         return h;
359 }
360 
361 static struct dns_question*
362 dns_consume_question(uint8_t **data, int *len)
363 {
364         struct dns_question *q = (struct dns_question *) *data;
365 
366         if (*len < sizeof(struct dns_question))
367                 return NULL;
368 
369         q->type = be16_to_cpu(q->type);
370         q->class = be16_to_cpu(q->class);
371 
372         *len -= sizeof(struct dns_question);
373         *data += sizeof(struct dns_question);
374 
375         return q;
376 }
377 
378 static struct dns_answer*
379 dns_consume_answer(uint8_t **data, int *len)
380 {
381         struct dns_answer *a = (struct dns_answer *) *data;
382 
383         if (*len < sizeof(struct dns_answer))
384                 return NULL;
385 
386         a->type = be16_to_cpu(a->type);
387         a->class = be16_to_cpu(a->class);
388         a->ttl = be32_to_cpu(a->ttl);
389         a->rdlength = be16_to_cpu(a->rdlength);
390 
391         *len -= sizeof(struct dns_answer);
392         *data += sizeof(struct dns_answer);
393 
394         return a;
395 }
396 
397 static char *
398 dns_consume_name(const uint8_t *base, int blen, uint8_t **data, int *len)
399 {
400         int nlen = scan_name(*data, *len);
401 
402         if (nlen < 1)
403                 return NULL;
404 
405         if (dn_expand(base, base + blen, *data, name_buffer, MAX_NAME_LEN) < 0) {
406                 perror("dns_consume_name/dn_expand");
407                 return NULL;
408         }
409 
410         *len -= nlen;
411         *data += nlen;
412 
413         return name_buffer;
414 }
415 
416 static int parse_answer(struct interface *iface, struct sockaddr *from,
417                         uint8_t *buffer, int len, uint8_t **b, int *rlen,
418                         int cache)
419 {
420         char *name = dns_consume_name(buffer, len, b, rlen);
421         struct dns_answer *a;
422         uint8_t *rdata;
423 
424         if (!name || *rlen < 0) {
425                 fprintf(stderr, "dropping: bad question\n");
426                 return -1;
427         }
428 
429         a = dns_consume_answer(b, rlen);
430         if (!a) {
431                 fprintf(stderr, "dropping: bad question\n");
432                 return -1;
433         }
434 
435         if ((a->class & ~CLASS_FLUSH) != CLASS_IN)
436                 return -1;
437 
438         rdata = *b;
439         if (a->rdlength > *rlen) {
440                 fprintf(stderr, "dropping: bad question\n");
441                 return -1;
442         }
443 
444         *rlen -= a->rdlength;
445         *b += a->rdlength;
446 
447         if (cache)
448                 cache_answer(iface, from, buffer, len, name, a, rdata, a->class & CLASS_FLUSH);
449 
450         return 0;
451 }
452 
453 static int
454 match_ipv6_addresses(char *reverse_ip, struct in6_addr *intf_ip)
455 {
456         int i = 0, j = 0, idx = 0;
457         char temp_ip[INET6_ADDRSTRLEN] = "";
458         struct in6_addr buf;
459         bool dot = false;
460 
461         for (i = strlen(reverse_ip) - 1; i >= 0; i--) {
462                 if ((reverse_ip[i] == '.') != dot)
463                         return 0;
464                 dot = !dot;
465 
466                 if (reverse_ip[i] == '.')
467                         continue;
468 
469                 if (j == 4) {
470                         temp_ip[idx] = ':';
471                         idx++;
472                         j = 0;
473                 }
474                 temp_ip[idx] = reverse_ip[i];
475                 idx++;
476                 j++;
477         }
478 
479         if (inet_pton(AF_INET6, temp_ip, &buf) <= 0)
480                 return 0;
481 
482         return !memcmp(&buf, intf_ip, sizeof(buf));
483 }
484 
485 static int
486 match_ip_addresses(char *reverse_ip, char *intf_ip)
487 {
488         int ip1[4], ip2[4], res;
489         char c;
490 
491         res = sscanf(reverse_ip, "%d.%d.%d.%d%c", &ip1[3], &ip1[2], &ip1[1], &ip1[0], &c);
492         if (res != 4)
493                 return 0;
494 
495         sscanf(intf_ip, "%d.%d.%d.%d", &ip2[0], &ip2[1], &ip2[2], &ip2[3]);
496 
497         int i;
498         for (i = 0; i < 4; i++) {
499                 if (ip1[i] != ip2[i])
500                         return 0;
501         }
502         return 1;
503 }
504 
505 static void
506 dns_reply_reverse_ip6_mapping(struct interface *iface, struct sockaddr *to, int ttl, char *name, char *reverse_ip)
507 {
508         struct ifaddrs *ifap, *ifa;
509         struct sockaddr_in6 *sa6;
510 
511         char intf_ip[INET6_ADDRSTRLEN] = "";
512         uint8_t buffer[256];
513         int len;
514 
515         if (getifaddrs(&ifap) < 0)
516                 return;
517         dns_packet_init();
518         for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
519                 if (strcmp(ifa->ifa_name, iface->name))
520                         continue;
521                 if (!ifa->ifa_addr)
522                         continue;
523                 if (ifa->ifa_addr->sa_family == AF_INET6) {
524                         sa6 = (struct sockaddr_in6 *) ifa->ifa_addr;
525                         if (inet_ntop(AF_INET6, &sa6->sin6_addr, intf_ip, INET6_ADDRSTRLEN) == NULL)
526                                 continue;
527 
528                         if (match_ipv6_addresses(reverse_ip, &sa6->sin6_addr)) {
529                                 len = dn_comp(mdns_hostname_local, buffer, sizeof(buffer), NULL, NULL);
530 
531                                 if (len < 1)
532                                         continue;
533 
534                                 dns_packet_answer(name, TYPE_PTR, buffer, len, ttl);
535                         }
536                 }
537         }
538         dns_packet_send(iface, to, 0, 0);
539 
540         freeifaddrs(ifap);
541 }
542 
543 static void
544 dns_reply_reverse_ip4_mapping(struct interface *iface, struct sockaddr *to, int ttl, char *name, char *reverse_ip)
545 {
546         struct ifaddrs *ifap, *ifa;
547         struct sockaddr_in *sa;
548 
549         char intf_ip[INET_ADDRSTRLEN] = "";
550         uint8_t buffer[256];
551         int len;
552 
553         if (getifaddrs(&ifap) < 0)
554                 return;
555         dns_packet_init();
556         for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
557                 if (strcmp(ifa->ifa_name, iface->name))
558                         continue;
559                 if (!ifa->ifa_addr)
560                         continue;
561                 if (ifa->ifa_addr->sa_family == AF_INET) {
562                         sa = (struct sockaddr_in *) ifa->ifa_addr;
563                         if (inet_ntop(AF_INET, &sa->sin_addr, intf_ip, INET_ADDRSTRLEN) == NULL)
564                                 continue;
565 
566                         if (match_ip_addresses(reverse_ip, intf_ip)) {
567                                 len = dn_comp(mdns_hostname_local, buffer, sizeof(buffer), NULL, NULL);
568 
569                                 if (len < 1)
570                                         continue;
571 
572                                 dns_packet_answer(name, TYPE_PTR, buffer, len, ttl);
573                         }
574                 }
575         }
576         dns_packet_send(iface, to, 0, 0);
577 
578         freeifaddrs(ifap);
579 }
580 
581 static bool
582 is_reverse_dns_query(const char *name, const char *suffix)
583 {
584         if (!name || !suffix)
585                 return false;
586 
587         size_t name_len = strlen(name);
588         size_t suffix_len = strlen(suffix);
589 
590         if (suffix_len > name_len)
591                 return false;
592 
593         if (strncmp(name + (name_len - suffix_len), suffix, suffix_len) == 0)
594                 return true;
595 
596         return false;
597 }
598 
599 static void
600 parse_question(struct interface *iface, struct sockaddr *from, char *name, struct dns_question *q)
601 {
602         int is_unicast = (q->class & CLASS_UNICAST) != 0;
603         struct sockaddr *to = NULL;
604         struct hostname *h;
605         char *host, *host6;
606 
607         /* TODO: Multicast if more than one quarter of TTL has passed */
608         if (is_unicast) {
609                 to = from;
610                 if (interface_multicast(iface))
611                         iface = interface_get(iface->name, iface->type | SOCKTYPE_BIT_UNICAST);
612                 if (!iface)
613                         return;
614         }
615 
616         DBG(1, "Q -> %s %s\n", dns_type_string(q->type), name);
617 
618         switch (q->type) {
619         case TYPE_ANY:
620                 if (!strcasecmp(name, mdns_hostname_local)) {
621                         dns_reply_a(iface, to, announce_ttl, NULL);
622                         dns_reply_a_additional(iface, to, announce_ttl);
623                         service_reply(iface, to, NULL, NULL, announce_ttl, is_unicast);
624                 }
625                 break;
626 
627         case TYPE_PTR:
628                 if (is_reverse_dns_query(name, ".in-addr.arpa")) {
629                         if (strlen(name) < MIN_PTR_NAME_LEN ||
630                             strlen(name) > MAX_PTR_NAME_LEN)
631                                 break;
632 
633                         host = strstr(name, ".in-addr.arpa");
634                         char name_buf[256];
635                         strcpy(name_buf, name);
636                         *host = '\0';
637                         dns_reply_reverse_ip4_mapping(iface, to, announce_ttl, name_buf, name);
638                         break;
639                 }
640 
641                 if (is_reverse_dns_query(name, ".ip6.arpa")) {
642                         if (strlen(name) != MAX_PTR6_NAME_LEN)
643                                 break;
644 
645                         host6 = strstr(name, ".ip6.arpa");
646                         char name_buf6[256];
647                         strcpy(name_buf6, name);
648                         *host6 = '\0';
649                         dns_reply_reverse_ip6_mapping(iface, to, announce_ttl, name_buf6, name);
650                         break;
651                 }
652 
653                 if (!strcasecmp(name, C_DNS_SD)) {
654                         service_announce_services(iface, to, announce_ttl);
655                 } else {
656                         if (name[0] == '_') {
657                                 service_reply(iface, to, NULL, name, announce_ttl, is_unicast);
658                         } else {
659                                 /* First dot separates instance name from the rest */
660                                 char *dot = strchr(name, '.');
661 
662                                 if (dot) {
663                                         *dot = '\0';
664                                         service_reply(iface, to, name, dot + 1, announce_ttl, is_unicast);
665                                         *dot = '.';
666                                 }
667                         }
668                 }
669                 break;
670 
671         case TYPE_AAAA:
672         case TYPE_A:
673                 host = strcasestr(name, ".local");
674                 if (host)
675                         *host = '\0';
676                 if (!strcasecmp(umdns_host_label, name)) {
677                         dns_reply_a(iface, to, announce_ttl, NULL);
678                 } else {
679                         if (host)
680                                 *host = '.';
681                         vlist_for_each_element(&hostnames, h, node)
682                                 if (!strcasecmp(h->hostname, name))
683                                         dns_reply_a(iface, to, announce_ttl, h->hostname);
684                 }
685                 break;
686         };
687 }
688 
689 void
690 dns_handle_packet(struct interface *iface, struct sockaddr *from, uint16_t port, uint8_t *buffer, int len)
691 {
692         struct dns_header *h;
693         uint8_t *b = buffer;
694         int rlen = len;
695 
696         h = dns_consume_header(&b, &rlen);
697         if (!h) {
698                 fprintf(stderr, "dropping: bad header\n");
699                 return;
700         }
701 
702         if (h->questions && !interface_multicast(iface) && port != MCAST_PORT)
703                 /* silently drop unicast questions that dont originate from port 5353 */
704                 return;
705 
706         while (h->questions-- > 0) {
707                 char *name = dns_consume_name(buffer, len, &b, &rlen);
708                 struct dns_question *q;
709 
710                 if (!name || rlen < 0) {
711                         fprintf(stderr, "dropping: bad name\n");
712                         return;
713                 }
714 
715                 q = dns_consume_question(&b, &rlen);
716                 if (!q) {
717                         fprintf(stderr, "dropping: bad question\n");
718                         return;
719                 }
720 
721                 if (!(h->flags & FLAG_RESPONSE))
722                         parse_question(iface, from, name, q);
723         }
724 
725         if (!(h->flags & FLAG_RESPONSE))
726                 return;
727 
728         while (h->answers-- > 0)
729                 if (parse_answer(iface, from, buffer, len, &b, &rlen, 1))
730                         return;
731 
732         while (h->authority-- > 0)
733                 if (parse_answer(iface, from, buffer, len, &b, &rlen, 1))
734                         return;
735 
736         while (h->additional-- > 0)
737                 if (parse_answer(iface, from, buffer, len, &b, &rlen, 1))
738                         return;
739 
740 }
741 

This page was automatically generated by LXR 0.3.1.  •  OpenWrt