resolved-dns-server.c revision 9df3ba6c6cb65eecec06f39dfe85a3596cedac4e
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/***
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering This file is part of systemd.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Copyright 2014 Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering systemd is free software; you can redistribute it and/or modify it
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering under the terms of the GNU Lesser General Public License as published by
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering (at your option) any later version.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering systemd is distributed in the hope that it will be useful, but
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Lesser General Public License for more details.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering You should have received a copy of the GNU Lesser General Public License
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering***/
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
b5efdb8af40ea759a1ea584c1bc44ecc81dd00ceLennart Poettering#include "siphash24.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering#include "resolved-dns-server.h"
b5efdb8af40ea759a1ea584c1bc44ecc81dd00ceLennart Poettering
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen/* After how much time to repeat classic DNS requests */
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering#define DNS_TIMEOUT_MIN_USEC (500 * USEC_PER_MSEC)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#define DNS_TIMEOUT_MAX_USEC (5 * USEC_PER_SEC)
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersenint dns_server_new(
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen Manager *m,
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen DnsServer **ret,
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen DnsServerType type,
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen Link *l,
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen int family,
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen const union in_addr_union *in_addr) {
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen DnsServer *s, *tail;
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert(m);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert((type == DNS_SERVER_LINK) == !!l);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert(in_addr);
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering s = new0(DnsServer, 1);
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering if (!s)
3c0cf502796be355431d4a64d738e75f543aa51dLennart Poettering return -ENOMEM;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering s->n_ref = 1;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering s->type = type;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering s->family = family;
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering s->address = *in_addr;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering s->resend_timeout = DNS_TIMEOUT_MIN_USEC;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering if (type == DNS_SERVER_LINK) {
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering LIST_FIND_TAIL(servers, l->dns_servers, tail);
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering LIST_INSERT_AFTER(servers, l->dns_servers, tail, s);
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering s->link = l;
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering } else if (type == DNS_SERVER_SYSTEM) {
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering LIST_FIND_TAIL(servers, m->dns_servers, tail);
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering LIST_INSERT_AFTER(servers, m->dns_servers, tail, s);
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering } else if (type == DNS_SERVER_FALLBACK) {
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering LIST_FIND_TAIL(servers, m->fallback_dns_servers, tail);
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering LIST_INSERT_AFTER(servers, m->fallback_dns_servers, tail, s);
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering } else
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_not_reached("Unknown server type");
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering s->manager = m;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen /* A new DNS server that isn't fallback is added and the one
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering * we used so far was a fallback one? Then let's try to pick
f4461e5641d53f27d6e76e0607bdaa9c0c58c1f6Lennart Poettering * the new one */
f4461e5641d53f27d6e76e0607bdaa9c0c58c1f6Lennart Poettering if (type != DNS_SERVER_FALLBACK &&
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen m->current_dns_server &&
d74fb368b18f0fbd9a4fe6f15691bbea7f3c4a01Tom Gundersen m->current_dns_server->type == DNS_SERVER_FALLBACK)
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering manager_set_dns_server(m, NULL);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (ret)
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen *ret = s;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering return 0;
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering}
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart PoetteringDnsServer* dns_server_ref(DnsServer *s) {
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering if (!s)
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering return NULL;
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering assert(s->n_ref > 0);
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering s->n_ref ++;
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering return s;
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering}
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poetteringstatic DnsServer* dns_server_free(DnsServer *s) {
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering if (!s)
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering return NULL;
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering if (s->link && s->link->current_dns_server == s)
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering link_set_dns_server(s->link, NULL);
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (s->manager && s->manager->current_dns_server == s)
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering manager_set_dns_server(s->manager, NULL);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering free(s);
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering return NULL;
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering}
3e684349c2cead2e6fd2f816c34eb17daba23a49Lennart Poettering
3e684349c2cead2e6fd2f816c34eb17daba23a49Lennart PoetteringDnsServer* dns_server_unref(DnsServer *s) {
3e684349c2cead2e6fd2f816c34eb17daba23a49Lennart Poettering if (!s)
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering return NULL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert(s->n_ref > 0);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (s->n_ref == 1)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering dns_server_free(s);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering else
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen s->n_ref --;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return NULL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering}
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersenvoid dns_server_packet_received(DnsServer *s, usec_t rtt) {
cab5b05903096e1c9cf5575ccc73f89d15c8db69Lennart Poettering assert(s);
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen if (rtt > s->max_rtt) {
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen s->max_rtt = rtt;
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering s->resend_timeout = MIN(MAX(DNS_TIMEOUT_MIN_USEC, s->max_rtt * 2),
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen DNS_TIMEOUT_MAX_USEC);
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen }
3e684349c2cead2e6fd2f816c34eb17daba23a49Lennart Poettering}
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poetteringvoid dns_server_packet_lost(DnsServer *s, usec_t usec) {
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen assert(s);
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering if (s->resend_timeout <= usec)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering s->resend_timeout = MIN(s->resend_timeout * 2, DNS_TIMEOUT_MAX_USEC);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering}
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic unsigned long dns_server_hash_func(const void *p, const uint8_t hash_key[HASH_KEY_SIZE]) {
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering const DnsServer *s = p;
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering uint64_t u;
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering siphash24((uint8_t*) &u, &s->address, FAMILY_ADDRESS_SIZE(s->family), hash_key);
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen u = u * hash_key[0] + u + s->family;
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering return u;
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering}
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poetteringstatic int dns_server_compare_func(const void *a, const void *b) {
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering const DnsServer *x = a, *y = b;
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering if (x->family < y->family)
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering return -1;
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering if (x->family > y->family)
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering return 1;
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering return memcmp(&x->address, &y->address, FAMILY_ADDRESS_SIZE(x->family));
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering}
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poetteringconst struct hash_ops dns_server_hash_ops = {
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering .hash = dns_server_hash_func,
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering .compare = dns_server_compare_func
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering};
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering