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

Sources/mdnsd/cache.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 <time.h>
 20 #include <stdio.h>
 21 #include <string.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 <time.h>
 30 
 31 #include <libubox/usock.h>
 32 #include <libubox/uloop.h>
 33 #include <libubox/avl-cmp.h>
 34 #include <libubox/blobmsg_json.h>
 35 #include <libubox/kvlist.h>
 36 #include <libubus.h>
 37 
 38 #include "cache.h"
 39 #include "util.h"
 40 #include "dns.h"
 41 #include "interface.h"
 42 
 43 /*
 44  * mDNS answers are unsolicited and unauthenticated, so any host on the
 45  * local segment can spray responses for an unbounded number of unique
 46  * names. Without these limits the cache grows without bound: every
 47  * record/service is kept until its TTL expires, and an attacker is free
 48  * to pick an arbitrarily large TTL, which defeats the TTL-only GC. A
 49  * sustained flood then exhausts the heap and crashes (or OOMs) the
 50  * daemon.
 51  *
 52  * CACHE_TTL_MAX clamps the stored TTL so the GC keeps reclaiming hostile
 53  * records (RFC 6762 recommends 75 minutes for shared records), and
 54  * cache_entries_max bounds the number of cached records and services so
 55  * memory stays bounded even while a flood is ongoing. The limit defaults
 56  * to CACHE_ENTRIES_MAX and can be overridden with the -c command line
 57  * option.
 58  */
 59 #define CACHE_TTL_MAX           (75 * 60)
 60 #define CACHE_ENTRIES_MAX       2048
 61 
 62 int cache_entries_max = CACHE_ENTRIES_MAX;
 63 
 64 static struct uloop_timeout cache_gc;
 65 struct avl_tree services;
 66 
 67 static int avl_strcasecmp(const void *k1, const void *k2, void *ptr)
 68 {
 69         return strcasecmp(k1, k2);
 70 }
 71 
 72 AVL_TREE(records, avl_strcasecmp, true, NULL);
 73 
 74 static void
 75 cache_record_free(struct cache_record *r)
 76 {
 77         DBG(2, "%s %s\n", dns_type_string(r->type), r->record);
 78         avl_delete(&records, &r->avl);
 79         free(r);
 80 }
 81 
 82 static void
 83 cache_service_free(struct cache_service *s)
 84 {
 85         DBG(2, "%s\n", s->entry);
 86         avl_delete(&services, &s->avl);
 87         free(s);
 88 }
 89 
 90 static int
 91 cache_is_expired(time_t t, uint32_t ttl, int frac)
 92 {
 93         if (monotonic_time() - t >= ttl * frac / 100)
 94                 return 1;
 95 
 96         return 0;
 97 }
 98 
 99 static void
100 cache_refresh_service(struct cache_service *s)
101 {
102         dns_query(s->entry, TYPE_PTR);
103 
104         if (!s->host)
105                 return;
106 
107         dns_query(s->host, TYPE_A);
108         dns_query(s->host, TYPE_AAAA);
109 }
110 
111 static void
112 cache_gc_timer(struct uloop_timeout *timeout)
113 {
114         struct cache_record *r, *p;
115         struct cache_service *s, *t;
116 
117         avl_for_each_element_safe(&records, r, avl, p) {
118                 if (!cache_is_expired(r->time, r->ttl, r->refresh))
119                         continue;
120                 /* Records other than A(AAA) are handled as services */
121                 if (r->type != TYPE_A && r->type != TYPE_AAAA) {
122                         if (cache_is_expired(r->time, r->ttl, 100))
123                                 cache_record_free(r);
124                         continue;
125                 }
126                 if (r->refresh >= 100) {
127                         cache_record_free(r);
128                         continue;
129                 }
130                 r->refresh += 50;
131                 dns_send_question(r->iface, (struct sockaddr *)&r->from, r->record, r->type, 0);
132         }
133 
134         avl_for_each_element_safe(&services, s, avl, t) {
135                 if (!s->host)
136                         continue;
137                 if (!cache_is_expired(s->time, s->ttl, s->refresh))
138                         continue;
139                 if (s->refresh >= 100) {
140                         cache_service_free(s);
141                         continue;
142                 }
143                 s->refresh += 50;
144                 cache_refresh_service(s);
145         }
146 
147         uloop_timeout_set(timeout, 10000);
148 }
149 
150 int
151 cache_init(void)
152 {
153         avl_init(&services, avl_strcasecmp, true, NULL);
154 
155         cache_gc.cb = cache_gc_timer;
156         uloop_timeout_set(&cache_gc, 10000);
157 
158         return 0;
159 }
160 
161 void cache_cleanup(struct interface *iface)
162 {
163         struct cache_record *r, *p;
164         struct cache_service *s, *t;
165 
166         avl_for_each_element_safe(&services, s, avl, t)
167                 if (!iface || iface == s->iface)
168                         cache_service_free(s);
169 
170         avl_for_each_element_safe(&records, r, avl, p)
171                 if (!iface || iface == r->iface)
172                         cache_record_free(r);
173 }
174 
175 void
176 cache_update(void)
177 {
178         struct cache_service *s;
179 
180         dns_query(C_DNS_SD, TYPE_ANY);
181         dns_query(C_DNS_SD, TYPE_PTR);
182         avl_for_each_element(&services, s, avl)
183                 cache_refresh_service(s);
184 }
185 
186 static struct cache_service*
187 cache_service(struct interface *iface, char *entry, size_t hlen, int ttl)
188 {
189         struct cache_service *s, *t;
190         char *entry_buf;
191         char *host_buf;
192         char *type;
193 
194         avl_for_each_element_safe(&services, s, avl, t)
195                 if (!strcmp(s->entry, entry)) {
196                         s->refresh = 50;
197                         s->time = monotonic_time();
198                         s->ttl = ttl;
199                         return s;
200                 }
201 
202         if (services.count >= (unsigned int)cache_entries_max)
203                 return NULL;
204 
205         s = calloc_a(sizeof(*s),
206                 &entry_buf, strlen(entry) + 1,
207                 &host_buf, hlen ? hlen + sizeof(".local") + 1 : 0);
208         if (!s)
209                 return NULL;
210 
211         s->avl.key = s->entry = strcpy(entry_buf, entry);
212         s->time = monotonic_time();
213         s->ttl = ttl;
214         s->iface = iface;
215         s->refresh = 50;
216 
217         if (hlen) {
218                 s->host = strncpy(host_buf, s->entry, hlen);
219                 strcpy(host_buf + hlen, ".local");
220         }
221 
222         type = strstr(entry_buf, "._");
223         if (type)
224                 type++;
225         if (type)
226                 s->avl.key = type;
227         avl_insert(&services, &s->avl);
228 
229         cache_refresh_service(s);
230 
231         return s;
232 }
233 
234 static struct cache_record*
235 cache_record_find(char *record, int type, int port, int rdlength, uint8_t *rdata)
236 {
237         struct cache_record *l = avl_find_element(&records, record, l, avl);
238 
239         while (l && !strcmp(l->record, record)) {
240                 struct cache_record *r = l;
241 
242                 l = !avl_is_last(&records, &l->avl) ? avl_next_element(l, avl) : NULL;
243                 if (r->type != type)
244                         continue;
245 
246                 if (r->type == TYPE_TXT || (r->type == TYPE_SRV))
247                         return r;
248 
249                 if (r->port != port)
250                         continue;
251 
252                 if (r->rdlength != rdlength)
253                         continue;
254 
255                 if (!!r->rdata != !!rdata)
256                         continue;
257 
258                 if (!r->rdata || !rdata || memcmp(r->rdata, rdata, rdlength))
259                         continue;
260 
261                 return r;
262         }
263 
264         return NULL;
265 }
266 
267 int
268 cache_host_is_known(char *record)
269 {
270         struct cache_record *l = avl_find_element(&records, record, l, avl);
271 
272         while (l && !strcmp(l->record, record)) {
273                 struct cache_record *r = l;
274 
275                 l = !avl_is_last(&records, &l->avl) ? avl_next_element(l, avl) : NULL;
276                 if ((r->type != TYPE_A) && (r->type != TYPE_AAAA))
277                         continue;
278                 return 1;
279         }
280 
281         return 0;
282 }
283 
284 void cache_answer(struct interface *iface, struct sockaddr *from, uint8_t *base,
285                   int blen, char *name, struct dns_answer *a, uint8_t *rdata,
286                   int flush)
287 {
288         struct dns_srv_data *dsd = (struct dns_srv_data *) rdata;
289         struct cache_record *r;
290         size_t port = 0, dlen = 0, tlen = 0, nlen, rdlength;
291         char *p = NULL;
292         char *name_buf;
293         void *rdata_ptr, *txt_ptr;
294         int host_len = 0;
295         static char *rdata_buffer = (char *) mdns_buf;
296         time_t now = monotonic_time();
297 
298         nlen = strlen(name);
299 
300         /*
301          * A TTL of 0 is a "goodbye" and is handled below; clamp any other
302          * value so a hostile, very large TTL cannot keep a record alive
303          * indefinitely and defeat the GC.
304          */
305         if (a->ttl > CACHE_TTL_MAX)
306                 a->ttl = CACHE_TTL_MAX;
307 
308         switch (a->type) {
309         case TYPE_PTR:
310                 if (a->rdlength < 2)
311                         return;
312 
313                 if (dn_expand(base, base + blen, rdata, rdata_buffer, MAX_DATA_LEN) < 0) {
314                         perror("process_answer/dn_expand");
315                         return;
316                 }
317 
318                 DBG(1, "A -> %s %s %s ttl:%d\n", dns_type_string(a->type), name, rdata_buffer, a->ttl);
319 
320                 rdlength = strlen(rdata_buffer);
321 
322                 if (strcmp(C_DNS_SD, name) != 0 &&
323                     nlen + 1 < rdlength && !strcmp(rdata_buffer + rdlength - nlen, name))
324                         host_len = rdlength - nlen - 1;
325 
326                 if (name[0] == '_')
327                         cache_service(iface, rdata_buffer, host_len, a->ttl);
328 
329                 dlen = strlen(rdata_buffer) + 1;
330                 rdata = (uint8_t*)rdata_buffer;
331                 break;
332 
333         case TYPE_SRV:
334                 if (a->rdlength < 8)
335                         return;
336 
337                 port = be16_to_cpu(dsd->port);
338                 memcpy(rdata_buffer, dsd, sizeof(*dsd));
339                 if (dn_expand(base, base + blen, (const uint8_t*)&dsd[1],
340                                 &rdata_buffer[sizeof(*dsd)], MAX_DATA_LEN - sizeof(*dsd)) < 0) {
341                         perror("process_answer/dn_expand");
342                         return;
343                 }
344                 dlen = sizeof(*dsd) + strlen(&rdata_buffer[sizeof(*dsd)]) + 1;
345                 rdata = (uint8_t*)rdata_buffer;
346                 break;
347 
348         case TYPE_TXT:
349                 rdlength = a->rdlength;
350                 if (rdlength <= 2 || *rdata >= rdlength)
351                         return;
352 
353                 /*
354                  * The copy fills [0 .. rdlength - 2]; terminate at rdlength - 1
355                  * and rdlength so cache_dump_records() finds the terminating
356                  * empty string inside the tlen sized record.
357                  */
358                 memcpy(rdata_buffer, &rdata[1], rdlength-1);
359                 rdata_buffer[rdlength - 1] = rdata_buffer[rdlength] = '\0';
360                 tlen = rdlength + 1;
361                 p = &rdata_buffer[*rdata];
362 
363                 do {
364                         uint8_t v = *p;
365 
366                         *p = '\0';
367                         if (v && p + v < &rdata_buffer[rdlength])
368                                 p += v + 1;
369                 } while (*p);
370                 break;
371 
372         case TYPE_A:
373                 if (a->rdlength != 4)
374                         return;
375                 dlen = 4;
376                 break;
377 
378         case TYPE_AAAA:
379                 if (a->rdlength != 16)
380                         return;
381                 dlen = 16;
382                 break;
383 
384         default:
385                 return;
386         }
387 
388         r = cache_record_find(name, a->type, port, dlen, rdata);
389         if (r) {
390                 if (!a->ttl) {
391                         DBG(1, "D -> %s %s ttl:%d\n", dns_type_string(r->type), r->record, r->ttl);
392                         r->time = now + 1 - r->ttl;
393                         r->refresh = 100;
394                 } else {
395                         r->ttl = a->ttl;
396                         r->time = now;
397                         r->refresh = 50;
398                         DBG(1, "A -> %s %s ttl:%d\n", dns_type_string(r->type), r->record, r->ttl);
399                 }
400                 return;
401         }
402 
403         if (!a->ttl)
404                 return;
405 
406         if (records.count >= (unsigned int)cache_entries_max) {
407                 DBG(1, "Cache full, dropping %s %s\n", dns_type_string(a->type), name);
408                 return;
409         }
410 
411         r = calloc_a(sizeof(*r),
412                 &name_buf, strlen(name) + 1,
413                 &txt_ptr, tlen,
414                 &rdata_ptr, dlen);
415         if (!r)
416                 return;
417 
418         r->avl.key = r->record = strcpy(name_buf, name);
419         r->type = a->type;
420         r->ttl = a->ttl;
421         r->port = port;
422         r->rdlength = dlen;
423         r->time = now;
424         r->iface = iface;
425         if (interface_ipv6(iface))
426                 memcpy(&r->from, from, sizeof(struct sockaddr_in6));
427         else
428                 memcpy(&r->from, from, sizeof(struct sockaddr_in));
429         r->refresh = 50;
430 
431         if (tlen)
432                 r->txt = memcpy(txt_ptr, rdata_buffer, tlen);
433 
434         if (dlen)
435                 r->rdata = memcpy(rdata_ptr, rdata, dlen);
436 
437         if (avl_insert(&records, &r->avl))
438                 free(r);
439         else
440                 DBG(1, "A -> %s %s ttl:%d\n", dns_type_string(r->type), r->record, r->ttl);
441 }
442 
443 void
444 cache_dump_records(struct blob_buf *buf, const char *name, int array,
445                    const char **hostname)
446 {
447         struct cache_record *r, *last, *next;
448         const char *txt;
449         char buffer[INET6_ADDRSTRLEN];
450         void *c = NULL;
451 
452         last = avl_last_element(&records, last, avl);
453         for (r = avl_find_element(&records, name, r, avl); r; r = next) {
454                 switch (r->type) {
455                 case TYPE_A:
456                         if (!c && array)
457                                 c = blobmsg_open_array(buf, "ipv4");
458                         if ((r->rdlength == 4) && inet_ntop(AF_INET, r->rdata, buffer, INET6_ADDRSTRLEN))
459                                 blobmsg_add_string(buf, "ipv4", buffer);
460                         break;
461                 }
462 
463                 if (r == last)
464                         break;
465 
466                 next = avl_next_element(r, avl);
467                 if (strcmp(r->record, next->record) != 0)
468                         break;
469         }
470 
471         if (c) {
472                 blobmsg_close_array(buf, c);
473                 c = NULL;
474         }
475 
476         for (r = avl_find_element(&records, name, r, avl); r; r = next) {
477                 switch (r->type) {
478                 case TYPE_AAAA:
479                         if (!c && array)
480                                 c = blobmsg_open_array(buf, "ipv6");
481                         if ((r->rdlength == 16) && inet_ntop(AF_INET6, r->rdata, buffer, INET6_ADDRSTRLEN))
482                                 blobmsg_add_string(buf, "ipv6", buffer);
483                         break;
484                 }
485 
486                 if (r == last)
487                         break;
488 
489                 next = avl_next_element(r, avl);
490                 if (strcmp(r->record, next->record) != 0)
491                         break;
492         }
493 
494         if (c) {
495                 blobmsg_close_array(buf, c);
496                 c = NULL;
497         }
498 
499         for (r = avl_find_element(&records, name, r, avl); r; r = next) {
500                 switch (r->type) {
501                 case TYPE_TXT:
502                         if (r->txt && strlen(r->txt)) {
503                                 if (array)
504                                         c = blobmsg_open_array(buf, "txt");
505 
506                                 txt = r->txt;
507                                 do {
508                                         blobmsg_add_string(buf, "txt", txt);
509                                         txt = &txt[strlen(txt) + 1];
510                                 } while (*txt);
511                                 if (array)
512                                         blobmsg_close_array(buf, c);
513                         }
514                         break;
515 
516                 case TYPE_SRV:
517                         if (r->rdata) {
518                                 blobmsg_add_string(buf, "host", (char *)r->rdata + sizeof(struct dns_srv_data));
519                                 if (hostname)
520                                         *hostname = (char *)r->rdata + sizeof(struct dns_srv_data);
521                         }
522 
523 
524                         if (r->record) {
525                                 const struct dns_srv_data *dsd;
526                                 const char *domain = NULL;
527 
528                                 domain = strstr(r->record, "._udp.");
529 
530                                 if (!domain)
531                                         domain = strstr(r->record, "._tcp.");
532 
533                                 if (!domain)
534                                         break;
535 
536                                 domain = domain + strlen("._udp.");
537                                 blobmsg_add_string(buf, "domain", domain);
538 
539                                 if (r->port)
540                                         blobmsg_add_u32(buf, "port", r->port);
541 
542                                 if (r->ttl)
543                                         blobmsg_add_u32(buf, "ttl", r->ttl);
544 
545                                 if (r->time) {
546                                         struct tm *local_time;
547                                         char str_tm[32] = {0};
548                                         time_t last_update = time(NULL) - (monotonic_time() - r->time);
549                                         local_time = localtime(&last_update);
550                                         strftime(str_tm, sizeof(str_tm), "%Y-%m-%dT%H:%M:%SZ", local_time);
551 
552                                         blobmsg_add_string(buf, "last_update", str_tm);
553                                 }
554 
555                                 dsd = (const struct dns_srv_data*)r->rdata;
556 
557                                 if (r->rdlength > sizeof(*dsd)) {
558                                         blobmsg_add_u32(buf, "priority", be16_to_cpu(dsd->priority));
559                                         blobmsg_add_u32(buf, "weight", be16_to_cpu(dsd->weight));
560                                 }
561                         }
562 
563                         break;
564                 }
565 
566                 if (r == last)
567                         break;
568 
569                 next = avl_next_element(r, avl);
570                 if (strcmp(r->record, next->record) != 0)
571                         break;
572         }
573 }
574 
575 void
576 cache_dump_recursive(struct blob_buf *b, const char *name, uint16_t type, struct interface *iface)
577 {
578         time_t now = monotonic_time();
579         for (struct cache_record *r = avl_find_ge_element(&records, name, r, avl);
580                         r && !strcmp(r->record, name);
581                         r = !avl_is_last(&records, &r->avl) ? avl_next_element(r, avl) : NULL) {
582                 int32_t ttl = r->ttl - (now - r->time);
583                 if (ttl <= 0 || (iface && iface->ifindex != r->iface->ifindex) ||
584                                 (type != TYPE_ANY && type != r->type))
585                         continue;
586 
587                 const char *txt;
588                 char buf[INET6_ADDRSTRLEN];
589                 void *k = blobmsg_open_table(b, NULL), *l;
590                 const struct dns_srv_data *dsd = (const struct dns_srv_data*)r->rdata;
591 
592                 blobmsg_add_string(b, "name", r->record);
593                 blobmsg_add_string(b, "type", dns_type_string(r->type));
594                 blobmsg_add_u32(b, "ttl", ttl);
595 
596                 switch (r->type) {
597                 case TYPE_TXT:
598                         if ((txt = r->txt) && strlen(txt)) {
599                                 l = blobmsg_open_array(b, "data");
600                                 do {
601                                         blobmsg_add_string(b, NULL, txt);
602                                         txt = &txt[strlen(txt) + 1];
603                                 } while (*txt);
604                                 blobmsg_close_array(b, l);
605                         }
606                         break;
607 
608                 case TYPE_SRV:
609                         if (r->rdlength > sizeof(*dsd)) {
610                                 blobmsg_add_u32(b, "priority", be16_to_cpu(dsd->priority));
611                                 blobmsg_add_u32(b, "weight", be16_to_cpu(dsd->weight));
612                                 blobmsg_add_u32(b, "port", be16_to_cpu(dsd->port));
613                                 blobmsg_add_string(b, "target", (const char*)&dsd[1]);
614                         }
615                         break;
616 
617                 case TYPE_PTR:
618                         if (r->rdlength > 0)
619                                 blobmsg_add_string(b, "target", (const char*)r->rdata);
620                         break;
621 
622                 case TYPE_A:
623                         if ((r->rdlength == 4) && inet_ntop(AF_INET, r->rdata, buf, sizeof(buf)))
624                                 blobmsg_add_string(b, "target", buf);
625                         break;
626 
627                 case TYPE_AAAA:
628                         if ((r->rdlength == 16) && inet_ntop(AF_INET6, r->rdata, buf, sizeof(buf)))
629                                 blobmsg_add_string(b, "target", buf);
630                         break;
631                 }
632 
633                 blobmsg_close_table(b, k);
634 
635 
636                 if (r->type == TYPE_PTR) {
637                         cache_dump_recursive(b, (const char*)r->rdata, TYPE_SRV, iface);
638                         cache_dump_recursive(b, (const char*)r->rdata, TYPE_TXT, iface);
639                 }
640 
641                 if (r->type == TYPE_SRV) {
642                         cache_dump_recursive(b, (const char*)&dsd[1], TYPE_A, iface);
643                         cache_dump_recursive(b, (const char*)&dsd[1], TYPE_AAAA, iface);
644                 }
645         }
646 }
647 

This page was automatically generated by LXR 0.3.1.  •  OpenWrt