resolved-manager.c revision 4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4
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 Tom Gundersen <teg@jklm.no>
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
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek#include <arpa/inet.h>
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek#include <resolv.h>
b5efdb8af40ea759a1ea584c1bc44ecc81dd00ceLennart Poettering#include <net/if.h>
4ad7f2761da661853dcc29d542efb4727abb1101Nick Owens#include <sys/ioctl.h>
7263f72499e962b3fd54cdb7c79d49ca72121edeZbigniew Jędrzejewski-Szmek#include <sys/poll.h>
e4e73a632524c382139034d4271f53b6089ab4cbLennart Poettering#include <netinet/in.h>
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering
e4e73a632524c382139034d4271f53b6089ab4cbLennart Poettering#include "rtnl-util.h"
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include "event-util.h"
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include "network-util.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "network-internal.h"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#include "conf-parser.h"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#include "socket-util.h"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#include "af-list.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "utf8.h"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#include "resolved.h"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#include "resolved-conf.h"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#include "resolved-dns-domain.h"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#define SEND_TIMEOUT_USEC (200 * USEC_PER_MSEC)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic int manager_process_link(sd_rtnl *rtnl, sd_rtnl_message *mm, void *userdata) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering Manager *m = userdata;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering uint16_t type;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering Link *l;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering int ifindex, r;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering assert(rtnl);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering assert(m);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering assert(mm);
7c1ff6ac3d9e3acae1d601d40728cf7ccc9a7730Tom Gundersen
7c1ff6ac3d9e3acae1d601d40728cf7ccc9a7730Tom Gundersen r = sd_rtnl_message_get_type(mm, &type);
7c1ff6ac3d9e3acae1d601d40728cf7ccc9a7730Tom Gundersen if (r < 0)
7c1ff6ac3d9e3acae1d601d40728cf7ccc9a7730Tom Gundersen goto fail;
7c1ff6ac3d9e3acae1d601d40728cf7ccc9a7730Tom Gundersen
7c1ff6ac3d9e3acae1d601d40728cf7ccc9a7730Tom Gundersen r = sd_rtnl_message_link_get_ifindex(mm, &ifindex);
36d9205d669bcdcb04fa730d1f3549a9fc9a9001Tom Gundersen if (r < 0)
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering goto fail;
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering
36d9205d669bcdcb04fa730d1f3549a9fc9a9001Tom Gundersen l = hashmap_get(m->links, INT_TO_PTR(ifindex));
36d9205d669bcdcb04fa730d1f3549a9fc9a9001Tom Gundersen
36d9205d669bcdcb04fa730d1f3549a9fc9a9001Tom Gundersen switch (type) {
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering case RTM_NEWLINK:{
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering bool is_new = !l;
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering if (!l) {
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering r = link_new(m, &l, ifindex);
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering if (r < 0)
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering goto fail;
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering }
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering r = link_update_rtnl(l, mm);
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering if (r < 0)
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering goto fail;
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering if (is_new)
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering log_debug("Found new link %i/%s", ifindex, l->name);
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering break;
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering }
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering case RTM_DELLINK:
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering if (l) {
36d9205d669bcdcb04fa730d1f3549a9fc9a9001Tom Gundersen log_debug("Removing link %i/%s", l->ifindex, l->name);
36d9205d669bcdcb04fa730d1f3549a9fc9a9001Tom Gundersen link_free(l);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering }
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering break;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering }
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering return 0;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poetteringfail:
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering log_warning("Failed to process RTNL link message: %s", strerror(-r));
dc477e7385e8ab29efb8fadb72ec994077a105c6Lennart Poettering return 0;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering}
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poetteringstatic int manager_process_address(sd_rtnl *rtnl, sd_rtnl_message *mm, void *userdata) {
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering Manager *m = userdata;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering union in_addr_union address;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering uint16_t type;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering int r, ifindex, family;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering LinkAddress *a;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering Link *l;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering assert(rtnl);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering assert(mm);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering assert(m);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering r = sd_rtnl_message_get_type(mm, &type);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering if (r < 0)
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering goto fail;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering r = sd_rtnl_message_addr_get_ifindex(mm, &ifindex);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (r < 0)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering goto fail;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering l = hashmap_get(m->links, INT_TO_PTR(ifindex));
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (!l)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return 0;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering r = sd_rtnl_message_addr_get_family(mm, &family);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (r < 0)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering goto fail;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering switch (family) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering case AF_INET:
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering r = sd_rtnl_message_read_in_addr(mm, IFA_LOCAL, &address.in);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (r < 0) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering r = sd_rtnl_message_read_in_addr(mm, IFA_ADDRESS, &address.in);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (r < 0)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering goto fail;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering }
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering break;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering case AF_INET6:
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering r = sd_rtnl_message_read_in6_addr(mm, IFA_LOCAL, &address.in6);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (r < 0) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering r = sd_rtnl_message_read_in6_addr(mm, IFA_ADDRESS, &address.in6);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (r < 0)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering goto fail;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering }
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering break;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering default:
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return 0;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering }
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering a = link_find_address(l, family, &address);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering switch (type) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering case RTM_NEWADDR:
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (!a) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering r = link_address_new(l, &a, family, &address);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (r < 0)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return r;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering }
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering r = link_address_update_rtnl(a, mm);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (r < 0)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return r;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering break;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering case RTM_DELADDR:
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (a)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering link_address_free(a);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering break;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering }
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return 0;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringfail:
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering log_warning("Failed to process RTNL address message: %s", strerror(-r));
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering return 0;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering}
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic int manager_rtnl_listen(Manager *m) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL, *reply = NULL;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering sd_rtnl_message *i;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering int r;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering assert(m);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering /* First, subscibe to interfaces coming and going */
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering r = sd_rtnl_open(&m->rtnl, 3, RTNLGRP_LINK, RTNLGRP_IPV4_IFADDR, RTNLGRP_IPV6_IFADDR);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering if (r < 0)
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering return r;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering r = sd_rtnl_attach_event(m->rtnl, m->event, 0);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering if (r < 0)
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering return r;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering r = sd_rtnl_add_match(m->rtnl, RTM_NEWLINK, manager_process_link, m);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering if (r < 0)
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering return r;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering r = sd_rtnl_add_match(m->rtnl, RTM_DELLINK, manager_process_link, m);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (r < 0)
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering return r;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering r = sd_rtnl_add_match(m->rtnl, RTM_NEWADDR, manager_process_address, m);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (r < 0)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return r;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering r = sd_rtnl_add_match(m->rtnl, RTM_DELADDR, manager_process_address, m);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (r < 0)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return r;
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering /* Then, enumerate all links */
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering r = sd_rtnl_message_new_link(m->rtnl, &req, RTM_GETLINK, 0);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering if (r < 0)
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering return r;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering r = sd_rtnl_message_request_dump(req, true);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering if (r < 0)
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering return r;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering r = sd_rtnl_call(m->rtnl, req, 0, &reply);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering if (r < 0)
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering return r;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering for (i = reply; i; i = sd_rtnl_message_next(i)) {
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering r = manager_process_link(m->rtnl, i, m);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering if (r < 0)
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering return r;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering }
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering req = sd_rtnl_message_unref(req);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering reply = sd_rtnl_message_unref(reply);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering /* Finally, enumerate all addresses, too */
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering r = sd_rtnl_message_new_addr(m->rtnl, &req, RTM_GETADDR, 0, AF_UNSPEC);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (r < 0)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return r;
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering r = sd_rtnl_message_request_dump(req, true);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering if (r < 0)
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen return r;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen r = sd_rtnl_call(m->rtnl, req, 0, &reply);
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen if (r < 0)
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen return r;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering for (i = reply; i; i = sd_rtnl_message_next(i)) {
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt r = manager_process_address(m->rtnl, i, m);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering if (r < 0)
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering return r;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering }
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering return r;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering}
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poetteringstatic int on_network_event(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering Manager *m = userdata;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering Iterator i;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering Link *l;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering int r;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering assert(m);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering sd_network_monitor_flush(m->network_monitor);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering HASHMAP_FOREACH(l, m->links, i) {
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering r = link_update_monitor(l);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering if (r < 0)
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt log_warning("Failed to update monitor information for %i: %s", l->ifindex, strerror(-r));
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt }
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt r = manager_write_resolv_conf(m);
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt if (r < 0)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering log_warning("Could not update resolv.conf: %s", strerror(-r));
d23a27a964748967e1ad20e86de869a753af555bTom Gundersen
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return 0;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering}
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poetteringstatic int manager_network_monitor_listen(Manager *m) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering int r, fd, events;
d23a27a964748967e1ad20e86de869a753af555bTom Gundersen
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering assert(m);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering r = sd_network_monitor_new(&m->network_monitor, NULL);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (r < 0)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return r;
d23a27a964748967e1ad20e86de869a753af555bTom Gundersen
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering fd = sd_network_monitor_get_fd(m->network_monitor);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (fd < 0)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return fd;
23432a1c249b9c513e438bffe9778a7ce2ee74feZbigniew Jędrzejewski-Szmek
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering events = sd_network_monitor_get_events(m->network_monitor);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (events < 0)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return events;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering r = sd_event_add_io(m->event, &m->network_event_source, fd, events, &on_network_event, m);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (r < 0)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return r;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return 0;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering}
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic int determine_hostname(char **ret) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering _cleanup_free_ char *h = NULL, *n = NULL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering int r;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering assert(ret);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering h = gethostname_malloc();
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (!h)
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering return log_oom();
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering if (!utf8_is_valid(h)) {
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering log_error("System hostname is not UTF-8 clean.");
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering return -EINVAL;
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering }
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering r = dns_name_normalize(h, &n);
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering if (r < 0) {
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering log_error("System hostname '%s' cannot be normalized.", h);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return r;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering *ret = n;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering n = NULL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return 0;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering}
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic int on_hostname_change(sd_event_source *es, int fd, uint32_t revents, void *userdata) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering _cleanup_free_ char *h = NULL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Manager *m = userdata;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering int r;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert(m);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = determine_hostname(&h);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (r < 0)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return 0; /* ignore invalid hostnames */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (streq(h, m->hostname))
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return 0;
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering log_info("System hostname changed to '%s'.", h);
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering free(m->hostname);
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering m->hostname = h;
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering h = NULL;
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek manager_refresh_rrs(m);
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek return 0;
8ac4e9e1e54397f6d1745c2a7a806132418c7da2Lennart Poettering}
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmekstatic int manager_watch_hostname(Manager *m) {
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering _cleanup_free_ char *h = NULL;
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek int r;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering assert(m);
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering m->hostname_fd = open("/proc/sys/kernel/hostname", O_RDONLY|O_CLOEXEC|O_NDELAY|O_NOCTTY);
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek if (m->hostname_fd < 0) {
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering log_warning("Failed to watch hostname: %m");
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering return 0;
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek }
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek r = sd_event_add_io(m->event, &m->hostname_event_source, m->hostname_fd, 0, on_hostname_change, m);
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering if (r < 0) {
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering if (r == -EPERM)
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek /* kernels prior to 3.2 don't support polling this file. Ignore the failure. */
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering m->hostname_fd = safe_close(m->hostname_fd);
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek else {
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek log_error("Failed to add hostname event source: %s", strerror(-r));
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek return r;
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering }
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen }
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen r = determine_hostname(&m->hostname);
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen if (r < 0) {
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering log_info("Defaulting to hostname 'linux'.");
549c1a2564b56f2bb38f1203d59c747ea15817f3Tom Gundersen m->hostname = strdup("linux");
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering if (!m->hostname)
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering return log_oom();
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek } else
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek log_info("Using system hostname '%s'.", m->hostname);
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek return 0;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek}
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmekint manager_new(Manager **ret) {
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek _cleanup_(manager_freep) Manager *m = NULL;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek int r;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen assert(ret);
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen m = new0(Manager, 1);
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen if (!m)
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen return -ENOMEM;
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen m->dns_ipv4_fd = m->dns_ipv6_fd = -1;
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen m->llmnr_ipv4_udp_fd = m->llmnr_ipv6_udp_fd = -1;
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen m->llmnr_ipv4_tcp_fd = m->llmnr_ipv6_tcp_fd = -1;
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen m->hostname_fd = -1;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek m->llmnr_support = SUPPORT_YES;
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek r = manager_parse_dns_server(m, DNS_SERVER_FALLBACK, DNS_SERVERS);
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering if (r < 0)
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek return r;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek r = sd_event_default(&m->event);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering if (r < 0)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return r;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering sd_event_add_signal(m->event, NULL, SIGTERM, NULL, NULL);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering sd_event_add_signal(m->event, NULL, SIGINT, NULL, NULL);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering sd_event_set_watchdog(m->event, true);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering r = manager_watch_hostname(m);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering if (r < 0)
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering return r;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering r = dns_scope_new(m, &m->unicast_scope, NULL, DNS_PROTOCOL_DNS, AF_UNSPEC);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering if (r < 0)
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering return r;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering r = manager_network_monitor_listen(m);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering if (r < 0)
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering return r;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering r = manager_rtnl_listen(m);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering if (r < 0)
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering return r;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering r = manager_connect_bus(m);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering if (r < 0)
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering return r;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering r = manager_llmnr_ipv4_udp_fd(m);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering if (r < 0)
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering return r;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering r = manager_llmnr_ipv6_udp_fd(m);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering if (r < 0)
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering return r;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering r = manager_llmnr_ipv4_tcp_fd(m);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering if (r < 0)
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering return r;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering r = manager_llmnr_ipv6_tcp_fd(m);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering if (r < 0)
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering return r;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering *ret = m;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering m = NULL;
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering return 0;
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering}
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart PoetteringManager *manager_free(Manager *m) {
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering Link *l;
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering if (!m)
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering return NULL;
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering while (m->dns_queries)
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering dns_query_free(m->dns_queries);
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering hashmap_free(m->dns_transactions);
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering while ((l = hashmap_first(m->links)))
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering link_free(l);
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering hashmap_free(m->links);
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering dns_scope_free(m->unicast_scope);
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering while (m->dns_servers)
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering dns_server_free(m->dns_servers);
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering while (m->fallback_dns_servers)
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering dns_server_free(m->fallback_dns_servers);
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering sd_event_source_unref(m->network_event_source);
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering sd_network_monitor_unref(m->network_monitor);
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering sd_event_source_unref(m->dns_ipv4_event_source);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering sd_event_source_unref(m->dns_ipv6_event_source);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering safe_close(m->dns_ipv4_fd);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering safe_close(m->dns_ipv6_fd);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering sd_event_source_unref(m->llmnr_ipv4_udp_event_source);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering sd_event_source_unref(m->llmnr_ipv6_udp_event_source);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering safe_close(m->llmnr_ipv4_udp_fd);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering safe_close(m->llmnr_ipv6_udp_fd);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering sd_event_source_unref(m->llmnr_ipv4_tcp_event_source);
fd0b4602f6332c3f1660eb208c8f5c719709a009Lennart Poettering sd_event_source_unref(m->llmnr_ipv6_tcp_event_source);
fd0b4602f6332c3f1660eb208c8f5c719709a009Lennart Poettering safe_close(m->llmnr_ipv4_tcp_fd);
fd0b4602f6332c3f1660eb208c8f5c719709a009Lennart Poettering safe_close(m->llmnr_ipv6_tcp_fd);
fd0b4602f6332c3f1660eb208c8f5c719709a009Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering sd_event_source_unref(m->bus_retry_event_source);
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering sd_bus_unref(m->bus);
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering sd_event_unref(m->event);
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering dns_resource_key_unref(m->host_ipv4_key);
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering dns_resource_key_unref(m->host_ipv6_key);
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering safe_close(m->hostname_fd);
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering sd_event_source_unref(m->hostname_event_source);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering free(m->hostname);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering free(m);
8ac4e9e1e54397f6d1745c2a7a806132418c7da2Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering return NULL;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering}
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poetteringstatic void write_resolve_conf_server(DnsServer *s, FILE *f, unsigned *count) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering _cleanup_free_ char *t = NULL;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering int r;
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek
0f84a72e3c0f58d71cff2121e6df1611eaf9c9eaDavid Herrmann assert(s);
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering assert(f);
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmek assert(count);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering r = in_addr_to_string(s->family, &s->address, &t);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (r < 0) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering log_warning("Invalid DNS address. Ignoring: %s", strerror(-r));
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering return;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering }
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering if (*count == MAXNS)
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering fputs("# Too many DNS servers configured, the following entries may be ignored\n", f);
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering fprintf(f, "nameserver %s\n", t);
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering (*count) ++;
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering}
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poetteringint manager_write_resolv_conf(Manager *m) {
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering static const char path[] = "/run/systemd/resolve/resolv.conf";
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering _cleanup_free_ char *temp_path = NULL;
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering _cleanup_fclose_ FILE *f = NULL;
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering unsigned count = 0;
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering DnsServer *s;
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek Iterator i;
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek Link *l;
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek int r;
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek assert(m);
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek r = fopen_temporary(path, &f, &temp_path);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek if (r < 0)
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek return r;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek fchmod(fileno(f), 0644);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek fputs("# This file is managed by systemd-resolved(8). Do not edit.\n#\n"
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek "# Third party programs must not access this file directly, but\n"
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek "# only through the symlink at /etc/resolv.conf. To manage\n"
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek "# resolv.conf(5) in a different way, replace the symlink by a\n"
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen "# static file or a different symlink.\n\n", f);
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen LIST_FOREACH(servers, s, m->dns_servers)
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen write_resolve_conf_server(s, f, &count);
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen HASHMAP_FOREACH(l, m->links, i)
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen LIST_FOREACH(servers, s, l->dns_servers)
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering write_resolve_conf_server(s, f, &count);
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering if (count == 0) {
549c1a2564b56f2bb38f1203d59c747ea15817f3Tom Gundersen LIST_FOREACH(servers, s, m->fallback_dns_servers)
549c1a2564b56f2bb38f1203d59c747ea15817f3Tom Gundersen write_resolve_conf_server(s, f, &count);
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering }
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek
f91dc2400dc33e9a0745ecaaef7489af116dca38Lennart Poettering r = fflush_and_check(f);
f91dc2400dc33e9a0745ecaaef7489af116dca38Lennart Poettering if (r < 0)
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek goto fail;
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek if (rename(temp_path, path) < 0) {
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek r = -errno;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek goto fail;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek }
03664a62914782dbd8f069bbcf8a0c8ca1df7010Lukas Nykryn
03664a62914782dbd8f069bbcf8a0c8ca1df7010Lukas Nykryn return 0;
03664a62914782dbd8f069bbcf8a0c8ca1df7010Lukas Nykryn
03664a62914782dbd8f069bbcf8a0c8ca1df7010Lukas Nykrynfail:
03664a62914782dbd8f069bbcf8a0c8ca1df7010Lukas Nykryn unlink(path);
03664a62914782dbd8f069bbcf8a0c8ca1df7010Lukas Nykryn unlink(temp_path);
03664a62914782dbd8f069bbcf8a0c8ca1df7010Lukas Nykryn return r;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek}
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmekint manager_recv(Manager *m, int fd, DnsProtocol protocol, DnsPacket **ret) {
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek union {
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek struct cmsghdr header; /* For alignment */
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen uint8_t buffer[CMSG_SPACE(MAX(sizeof(struct in_pktinfo), sizeof(struct in6_pktinfo)))
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen + CMSG_SPACE(int) /* ttl/hoplimit */
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen + EXTRA_CMSG_SPACE /* kernel appears to require extra buffer space */];
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen } control;
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen union sockaddr_union sa;
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen struct msghdr mh = {};
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen struct cmsghdr *cmsg;
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen struct iovec iov;
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen int ms = 0, r;
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen ssize_t l;
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen assert(m);
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen assert(fd >= 0);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering assert(ret);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering r = ioctl(fd, FIONREAD, &ms);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (r < 0)
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering return -errno;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering if (ms < 0)
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek return -EIO;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek r = dns_packet_new(&p, protocol, ms);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek if (r < 0)
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek return r;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek iov.iov_base = DNS_PACKET_DATA(p);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek iov.iov_len = p->allocated;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek mh.msg_name = &sa.sa;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek mh.msg_namelen = sizeof(sa);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek mh.msg_iov = &iov;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek mh.msg_iovlen = 1;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek mh.msg_control = &control;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek mh.msg_controllen = sizeof(control);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek l = recvmsg(fd, &mh, 0);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek if (l < 0) {
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek if (errno == EAGAIN || errno == EINTR)
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek return 0;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek return -errno;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek }
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek if (l <= 0)
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek return -EIO;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek assert(!(mh.msg_flags & MSG_CTRUNC));
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek assert(!(mh.msg_flags & MSG_TRUNC));
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek p->size = (size_t) l;
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen p->family = sa.sa.sa_family;
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen p->ipproto = IPPROTO_UDP;
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen if (p->family == AF_INET) {
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen p->sender.in = sa.in.sin_addr;
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen p->sender_port = be16toh(sa.in.sin_port);
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen } else if (p->family == AF_INET6) {
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen p->sender.in6 = sa.in6.sin6_addr;
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen p->sender_port = be16toh(sa.in6.sin6_port);
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen p->ifindex = sa.in6.sin6_scope_id;
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen } else
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen return -EAFNOSUPPORT;
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen for (cmsg = CMSG_FIRSTHDR(&mh); cmsg; cmsg = CMSG_NXTHDR(&mh, cmsg)) {
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen if (cmsg->cmsg_level == IPPROTO_IPV6) {
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen assert(p->family == AF_INET6);
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
cb57dd41595adddb08095298bb1ed258c8ea4877Tom Gundersen switch (cmsg->cmsg_type) {
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen case IPV6_PKTINFO: {
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen struct in6_pktinfo *i = (struct in6_pktinfo*) CMSG_DATA(cmsg);
cb57dd41595adddb08095298bb1ed258c8ea4877Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen if (p->ifindex <= 0)
2c1fb4f71206bf970d493294208c5d7597194856Lennart Poettering p->ifindex = i->ipi6_ifindex;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen p->destination.in6 = i->ipi6_addr;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen break;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen }
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen case IPV6_HOPLIMIT:
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen p->ttl = *(int *) CMSG_DATA(cmsg);
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen break;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
2c1fb4f71206bf970d493294208c5d7597194856Lennart Poettering }
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen } else if (cmsg->cmsg_level == IPPROTO_IP) {
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen assert(p->family == AF_INET);
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen switch (cmsg->cmsg_type) {
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen case IP_PKTINFO: {
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen struct in_pktinfo *i = (struct in_pktinfo*) CMSG_DATA(cmsg);
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen if (p->ifindex <= 0)
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen p->ifindex = i->ipi_ifindex;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen p->destination.in = i->ipi_addr;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering break;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering }
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering case IP_TTL:
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering p->ttl = *(int *) CMSG_DATA(cmsg);
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering break;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering }
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering }
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering }
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering /* The Linux kernel sets the interface index to the loopback
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering * device if the packet came from the local host since it
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering * avoids the routing table in such a case. Let's unset the
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering * interface index in such a case. */
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering if (p->ifindex > 0 && manager_ifindex_is_loopback(m, p->ifindex) != 0)
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering p->ifindex = 0;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering /* If we don't know the interface index still, we look for the
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering * first local interface with a matching address. Yuck! */
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering if (p->ifindex <= 0)
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering p->ifindex = manager_find_ifindex(m, p->family, &p->destination);
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering *ret = p;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering p = NULL;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering return 1;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering}
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poetteringstatic int on_dns_packet(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering DnsTransaction *t = NULL;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering Manager *m = userdata;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering int r;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering r = manager_recv(m, fd, DNS_PROTOCOL_DNS, &p);
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering if (r <= 0)
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering return r;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek if (dns_packet_validate_reply(p) > 0) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering t = hashmap_get(m->dns_transactions, UINT_TO_PTR(DNS_PACKET_ID(p)));
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (!t)
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering return 0;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering dns_transaction_process_reply(t, p);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering } else
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering log_debug("Invalid DNS packet.");
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek return 0;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering}
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poetteringint manager_dns_ipv4_fd(Manager *m) {
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering const int one = 1;
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering int r;
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering assert(m);
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering if (m->dns_ipv4_fd >= 0)
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering return m->dns_ipv4_fd;
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering m->dns_ipv4_fd = socket(AF_INET, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (m->dns_ipv4_fd < 0)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return -errno;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
8ac4e9e1e54397f6d1745c2a7a806132418c7da2Lennart Poettering r = setsockopt(m->dns_ipv4_fd, IPPROTO_IP, IP_PKTINFO, &one, sizeof(one));
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (r < 0) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering r = -errno;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering goto fail;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering }
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering r = sd_event_add_io(m->event, &m->dns_ipv4_event_source, m->dns_ipv4_fd, EPOLLIN, on_dns_packet, m);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (r < 0)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering goto fail;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return m->dns_ipv4_fd;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poetteringfail:
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek m->dns_ipv4_fd = safe_close(m->dns_ipv4_fd);
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek return r;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering}
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmek
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmekint manager_dns_ipv6_fd(Manager *m) {
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmek const int one = 1;
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmek int r;
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmek
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmek assert(m);
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmek
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmek if (m->dns_ipv6_fd >= 0)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return m->dns_ipv6_fd;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering m->dns_ipv6_fd = socket(AF_INET6, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (m->dns_ipv6_fd < 0)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return -errno;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering r = setsockopt(m->dns_ipv6_fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &one, sizeof(one));
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (r < 0) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering r = -errno;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering goto fail;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering }
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering r = sd_event_add_io(m->event, &m->dns_ipv6_event_source, m->dns_ipv6_fd, EPOLLIN, on_dns_packet, m);
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek if (r < 0)
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek goto fail;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return m->dns_ipv6_fd;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmekfail:
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering m->dns_ipv6_fd = safe_close(m->dns_ipv6_fd);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return r;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering}
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poetteringstatic int sendmsg_loop(int fd, struct msghdr *mh, int flags) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering int r;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering assert(fd >= 0);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering assert(mh);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering for (;;) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (sendmsg(fd, mh, flags) >= 0)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return 0;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (errno == EINTR)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering continue;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (errno != EAGAIN)
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek return -errno;
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek r = fd_wait_for_event(fd, POLLOUT, SEND_TIMEOUT_USEC);
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek if (r < 0)
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek return r;
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek if (r == 0)
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek return -ETIMEDOUT;
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek }
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek}
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmekstatic int manager_ipv4_send(Manager *m, int fd, int ifindex, const struct in_addr *addr, uint16_t port, DnsPacket *p) {
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek union sockaddr_union sa = {
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek .in.sin_family = AF_INET,
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek };
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek union {
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek struct cmsghdr header; /* For alignment */
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek uint8_t buffer[CMSG_SPACE(sizeof(struct in_pktinfo))];
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek } control;
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek struct msghdr mh = {};
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek struct iovec iov;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek assert(m);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek assert(fd >= 0);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek assert(addr);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek assert(port > 0);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek assert(p);
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen iov.iov_base = DNS_PACKET_DATA(p);
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen iov.iov_len = p->size;
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen sa.in.sin_addr = *addr;
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen sa.in.sin_port = htobe16(port),
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen mh.msg_iov = &iov;
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen mh.msg_iovlen = 1;
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen mh.msg_name = &sa.sa;
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen mh.msg_namelen = sizeof(sa.in);
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen if (ifindex > 0) {
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen struct cmsghdr *cmsg;
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen struct in_pktinfo *pi;
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek
549c1a2564b56f2bb38f1203d59c747ea15817f3Tom Gundersen zero(control);
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering mh.msg_control = &control;
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering mh.msg_controllen = CMSG_LEN(sizeof(struct in_pktinfo));
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering cmsg = CMSG_FIRSTHDR(&mh);
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering cmsg->cmsg_len = mh.msg_controllen;
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering cmsg->cmsg_level = IPPROTO_IP;
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek cmsg->cmsg_type = IP_PKTINFO;
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering pi = (struct in_pktinfo*) CMSG_DATA(cmsg);
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering pi->ipi_ifindex = ifindex;
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering }
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek return sendmsg_loop(fd, &mh, 0);
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek}
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmekstatic int manager_ipv6_send(Manager *m, int fd, int ifindex, const struct in6_addr *addr, uint16_t port, DnsPacket *p) {
1bf968f36393666f2c57953b1748e6219c027deeTom Gundersen union sockaddr_union sa = {
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek .in6.sin6_family = AF_INET6,
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek };
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering union {
f91dc2400dc33e9a0745ecaaef7489af116dca38Lennart Poettering struct cmsghdr header; /* For alignment */
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek uint8_t buffer[CMSG_SPACE(sizeof(struct in6_pktinfo))];
f91dc2400dc33e9a0745ecaaef7489af116dca38Lennart Poettering } control;
f91dc2400dc33e9a0745ecaaef7489af116dca38Lennart Poettering struct msghdr mh = {};
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek struct iovec iov;
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek assert(m);
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek assert(fd >= 0);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering assert(addr);
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek assert(port > 0);
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek assert(p);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek iov.iov_base = DNS_PACKET_DATA(p);
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek iov.iov_len = p->size;
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek sa.in6.sin6_addr = *addr;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek sa.in6.sin6_port = htobe16(port),
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek sa.in6.sin6_scope_id = ifindex;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek
1bf968f36393666f2c57953b1748e6219c027deeTom Gundersen mh.msg_iov = &iov;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek mh.msg_iovlen = 1;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek mh.msg_name = &sa.sa;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek mh.msg_namelen = sizeof(sa.in6);
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen if (ifindex > 0) {
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen struct cmsghdr *cmsg;
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen struct in6_pktinfo *pi;
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen zero(control);
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen mh.msg_control = &control;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek mh.msg_controllen = CMSG_LEN(sizeof(struct in6_pktinfo));
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek cmsg = CMSG_FIRSTHDR(&mh);
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen cmsg->cmsg_len = mh.msg_controllen;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek cmsg->cmsg_level = IPPROTO_IPV6;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek cmsg->cmsg_type = IPV6_PKTINFO;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek pi = (struct in6_pktinfo*) CMSG_DATA(cmsg);
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek pi->ipi6_ifindex = ifindex;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek }
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen return sendmsg_loop(fd, &mh, 0);
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen}
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmekint manager_send(Manager *m, int fd, int ifindex, int family, const union in_addr_union *addr, uint16_t port, DnsPacket *p) {
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek assert(m);
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek assert(fd >= 0);
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek assert(addr);
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek assert(port > 0);
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek assert(p);
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen log_debug("Sending %s packet with id %u on interface %i/%s", DNS_PACKET_QR(p) ? "response" : "query", DNS_PACKET_ID(p), ifindex, af_to_name(family));
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen if (family == AF_INET)
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen return manager_ipv4_send(m, fd, ifindex, &addr->in, port, p);
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen else if (family == AF_INET6)
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen return manager_ipv6_send(m, fd, ifindex, &addr->in6, port, p);
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen return -EAFNOSUPPORT;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen}
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersenbool manager_known_dns_server(Manager *m, int family, const union in_addr_union *in_addr) {
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen DnsServer *s;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen assert(m);
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen assert(in_addr);
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen
f5430a3ef308f3a102899fcaf7fbece757082f2aLennart Poettering LIST_FOREACH(servers, s, m->dns_servers)
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen if (s->family == family && in_addr_equal(family, &s->address, in_addr))
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen return true;
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen LIST_FOREACH(servers, s, m->fallback_dns_servers)
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen if (s->family == family && in_addr_equal(family, &s->address, in_addr))
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen return true;
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen return false;
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen}
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersenstatic DnsServer *manager_set_dns_server(Manager *m, DnsServer *s) {
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen assert(m);
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen if (m->current_dns_server == s)
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen return s;
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen if (s) {
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen _cleanup_free_ char *ip = NULL;
f5430a3ef308f3a102899fcaf7fbece757082f2aLennart Poettering
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen in_addr_to_string(s->family, &s->address, &ip);
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen log_info("Switching to system DNS server %s.", strna(ip));
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen } else
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen log_info("No system DNS server set.");
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen m->current_dns_server = s;
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen return s;
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen}
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-SzmekDnsServer *manager_get_dns_server(Manager *m) {
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek Link *l;
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek assert(m);
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek
f5430a3ef308f3a102899fcaf7fbece757082f2aLennart Poettering if (!m->current_dns_server)
d23a27a964748967e1ad20e86de869a753af555bTom Gundersen manager_set_dns_server(m, m->dns_servers);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (!m->current_dns_server) {
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek bool found = false;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering Iterator i;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering /* No DNS servers configured, let's see if there are
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering * any on any links. If not, we use the fallback
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering * servers */
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering HASHMAP_FOREACH(l, m->links, i)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (l->dns_servers) {
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering found = true;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering break;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering }
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (!found)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering manager_set_dns_server(m, m->fallback_dns_servers);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering }
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return m->current_dns_server;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering}
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poetteringvoid manager_next_dns_server(Manager *m) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering assert(m);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering /* If there's currently no DNS server set, then the next
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering * manager_get_dns_server() will find one */
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (!m->current_dns_server)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return;
816b4547dac1b052239ad6ee3801c78c691e5cb4Daniel Mack
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering /* Change to the next one */
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (m->current_dns_server->servers_next) {
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering manager_set_dns_server(m, m->current_dns_server->servers_next);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering }
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering /* If there was no next one, then start from the beginning of
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering * the list */
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering if (m->current_dns_server->type == DNS_SERVER_FALLBACK)
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering manager_set_dns_server(m, m->fallback_dns_servers);
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering else
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering manager_set_dns_server(m, m->dns_servers);
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering}
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poetteringuint32_t manager_find_mtu(Manager *m) {
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering uint32_t mtu = 0;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering Link *l;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering Iterator i;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering /* If we don't know on which link a DNS packet would be
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering * delivered, let's find the largest MTU that works on all
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering * interfaces we know of */
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering HASHMAP_FOREACH(l, m->links, i) {
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering if (l->mtu <= 0)
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering continue;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering if (mtu <= 0 || l->mtu < mtu)
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering mtu = l->mtu;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering }
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering return mtu;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering}
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
static int on_llmnr_packet(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
_cleanup_(dns_packet_unrefp) DnsPacket *p = NULL;
DnsTransaction *t = NULL;
Manager *m = userdata;
int r;
r = manager_recv(m, fd, DNS_PROTOCOL_LLMNR, &p);
if (r <= 0)
return r;
if (dns_packet_validate_reply(p) > 0) {
log_debug("Got reply packet for id %u", DNS_PACKET_ID(p));
t = hashmap_get(m->dns_transactions, UINT_TO_PTR(DNS_PACKET_ID(p)));
if (!t)
return 0;
dns_transaction_process_reply(t, p);
} else if (dns_packet_validate_query(p) > 0) {
Link *l;
l = hashmap_get(m->links, INT_TO_PTR(p->ifindex));
if (l) {
DnsScope *scope = NULL;
if (p->family == AF_INET)
scope = l->llmnr_ipv4_scope;
else if (p->family == AF_INET6)
scope = l->llmnr_ipv6_scope;
if (scope)
dns_scope_process_query(scope, NULL, p);
}
} else
log_debug("Invalid LLMNR packet.");
return 0;
}
int manager_llmnr_ipv4_udp_fd(Manager *m) {
union sockaddr_union sa = {
.in.sin_family = AF_INET,
.in.sin_port = htobe16(5355),
};
static const int one = 1, pmtu = IP_PMTUDISC_DONT, ttl = 255;
int r;
assert(m);
if (m->llmnr_ipv4_udp_fd >= 0)
return m->llmnr_ipv4_udp_fd;
m->llmnr_ipv4_udp_fd = socket(AF_INET, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
if (m->llmnr_ipv4_udp_fd < 0)
return -errno;
/* RFC 4795, section 2.5 recommends setting the TTL of UDP packets to 255. */
r = setsockopt(m->llmnr_ipv4_udp_fd, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl));
if (r < 0) {
r = -errno;
goto fail;
}
r = setsockopt(m->llmnr_ipv4_udp_fd, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl));
if (r < 0) {
r = -errno;
goto fail;
}
r = setsockopt(m->llmnr_ipv4_udp_fd, IPPROTO_IP, IP_MULTICAST_LOOP, &one, sizeof(one));
if (r < 0) {
r = -errno;
goto fail;
}
r = setsockopt(m->llmnr_ipv4_udp_fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
if (r < 0) {
r = -errno;
goto fail;
}
r = setsockopt(m->llmnr_ipv4_udp_fd, IPPROTO_IP, IP_PKTINFO, &one, sizeof(one));
if (r < 0) {
r = -errno;
goto fail;
}
r = setsockopt(m->llmnr_ipv4_udp_fd, IPPROTO_IP, IP_RECVTTL, &one, sizeof(one));
if (r < 0) {
r = -errno;
goto fail;
}
/* Disable Don't-Fragment bit in the IP header */
r = setsockopt(m->llmnr_ipv4_udp_fd, IPPROTO_IP, IP_MTU_DISCOVER, &pmtu, sizeof(pmtu));
if (r < 0) {
r = -errno;
goto fail;
}
r = bind(m->llmnr_ipv4_udp_fd, &sa.sa, sizeof(sa.in));
if (r < 0) {
r = -errno;
goto fail;
}
r = sd_event_add_io(m->event, &m->llmnr_ipv4_udp_event_source, m->llmnr_ipv4_udp_fd, EPOLLIN, on_llmnr_packet, m);
if (r < 0)
goto fail;
return m->llmnr_ipv4_udp_fd;
fail:
m->llmnr_ipv4_udp_fd = safe_close(m->llmnr_ipv4_udp_fd);
return r;
}
int manager_llmnr_ipv6_udp_fd(Manager *m) {
union sockaddr_union sa = {
.in6.sin6_family = AF_INET6,
.in6.sin6_port = htobe16(5355),
};
static const int one = 1, ttl = 255;
int r;
assert(m);
if (m->llmnr_ipv6_udp_fd >= 0)
return m->llmnr_ipv6_udp_fd;
m->llmnr_ipv6_udp_fd = socket(AF_INET6, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
if (m->llmnr_ipv6_udp_fd < 0)
return -errno;
r = setsockopt(m->llmnr_ipv6_udp_fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof(ttl));
if (r < 0) {
r = -errno;
goto fail;
}
/* RFC 4795, section 2.5 recommends setting the TTL of UDP packets to 255. */
r = setsockopt(m->llmnr_ipv6_udp_fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &ttl, sizeof(ttl));
if (r < 0) {
r = -errno;
goto fail;
}
r = setsockopt(m->llmnr_ipv6_udp_fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &one, sizeof(one));
if (r < 0) {
r = -errno;
goto fail;
}
r = setsockopt(m->llmnr_ipv6_udp_fd, IPPROTO_IPV6, IPV6_V6ONLY, &one, sizeof(one));
if (r < 0) {
r = -errno;
goto fail;
}
r = setsockopt(m->llmnr_ipv6_udp_fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
if (r < 0) {
r = -errno;
goto fail;
}
r = setsockopt(m->llmnr_ipv6_udp_fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &one, sizeof(one));
if (r < 0) {
r = -errno;
goto fail;
}
r = setsockopt(m->llmnr_ipv6_udp_fd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &one, sizeof(one));
if (r < 0) {
r = -errno;
goto fail;
}
r = bind(m->llmnr_ipv6_udp_fd, &sa.sa, sizeof(sa.in6));
if (r < 0) {
r = -errno;
goto fail;
}
r = sd_event_add_io(m->event, &m->llmnr_ipv6_udp_event_source, m->llmnr_ipv6_udp_fd, EPOLLIN, on_llmnr_packet, m);
if (r < 0) {
r = -errno;
goto fail;
}
return m->llmnr_ipv6_udp_fd;
fail:
m->llmnr_ipv6_udp_fd = safe_close(m->llmnr_ipv6_udp_fd);
return r;
}
static int on_llmnr_stream_packet(DnsStream *s) {
assert(s);
if (dns_packet_validate_query(s->read_packet) > 0) {
Link *l;
l = hashmap_get(s->manager->links, INT_TO_PTR(s->read_packet->ifindex));
if (l) {
DnsScope *scope = NULL;
if (s->read_packet->family == AF_INET)
scope = l->llmnr_ipv4_scope;
else if (s->read_packet->family == AF_INET6)
scope = l->llmnr_ipv6_scope;
if (scope) {
dns_scope_process_query(scope, s, s->read_packet);
/* If no reply packet was set, we free the stream */
if (s->write_packet)
return 0;
}
}
}
dns_stream_free(s);
return 0;
}
static int on_llmnr_stream(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
DnsStream *stream;
Manager *m = userdata;
int cfd, r;
cfd = accept4(fd, NULL, NULL, SOCK_NONBLOCK|SOCK_CLOEXEC);
if (cfd < 0) {
if (errno == EAGAIN || errno == EINTR)
return 0;
return -errno;
}
r = dns_stream_new(m, &stream, DNS_PROTOCOL_LLMNR, cfd);
if (r < 0) {
safe_close(cfd);
return r;
}
stream->on_packet = on_llmnr_stream_packet;
return 0;
}
int manager_llmnr_ipv4_tcp_fd(Manager *m) {
union sockaddr_union sa = {
.in.sin_family = AF_INET,
.in.sin_port = htobe16(5355),
};
static const int one = 1, pmtu = IP_PMTUDISC_DONT;
int r;
assert(m);
if (m->llmnr_ipv4_tcp_fd >= 0)
return m->llmnr_ipv4_tcp_fd;
m->llmnr_ipv4_tcp_fd = socket(AF_INET, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
if (m->llmnr_ipv4_tcp_fd < 0)
return -errno;
/* RFC 4795, section 2.5. requires setting the TTL of TCP streams to 1 */
r = setsockopt(m->llmnr_ipv4_tcp_fd, IPPROTO_IP, IP_TTL, &one, sizeof(one));
if (r < 0) {
r = -errno;
goto fail;
}
r = setsockopt(m->llmnr_ipv4_tcp_fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
if (r < 0) {
r = -errno;
goto fail;
}
r = setsockopt(m->llmnr_ipv4_tcp_fd, IPPROTO_IP, IP_PKTINFO, &one, sizeof(one));
if (r < 0) {
r = -errno;
goto fail;
}
r = setsockopt(m->llmnr_ipv4_tcp_fd, IPPROTO_IP, IP_RECVTTL, &one, sizeof(one));
if (r < 0) {
r = -errno;
goto fail;
}
/* Disable Don't-Fragment bit in the IP header */
r = setsockopt(m->llmnr_ipv4_tcp_fd, IPPROTO_IP, IP_MTU_DISCOVER, &pmtu, sizeof(pmtu));
if (r < 0) {
r = -errno;
goto fail;
}
r = bind(m->llmnr_ipv4_tcp_fd, &sa.sa, sizeof(sa.in));
if (r < 0) {
r = -errno;
goto fail;
}
r = listen(m->llmnr_ipv4_tcp_fd, SOMAXCONN);
if (r < 0) {
r = -errno;
goto fail;
}
r = sd_event_add_io(m->event, &m->llmnr_ipv4_tcp_event_source, m->llmnr_ipv4_tcp_fd, EPOLLIN, on_llmnr_stream, m);
if (r < 0)
goto fail;
return m->llmnr_ipv4_tcp_fd;
fail:
m->llmnr_ipv4_tcp_fd = safe_close(m->llmnr_ipv4_tcp_fd);
return r;
}
int manager_llmnr_ipv6_tcp_fd(Manager *m) {
union sockaddr_union sa = {
.in6.sin6_family = AF_INET6,
.in6.sin6_port = htobe16(5355),
};
static const int one = 1;
int r;
assert(m);
if (m->llmnr_ipv6_tcp_fd >= 0)
return m->llmnr_ipv6_tcp_fd;
m->llmnr_ipv6_tcp_fd = socket(AF_INET6, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
if (m->llmnr_ipv6_tcp_fd < 0)
return -errno;
/* RFC 4795, section 2.5. requires setting the TTL of TCP streams to 1 */
r = setsockopt(m->llmnr_ipv6_tcp_fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &one, sizeof(one));
if (r < 0) {
r = -errno;
goto fail;
}
r = setsockopt(m->llmnr_ipv6_tcp_fd, IPPROTO_IPV6, IPV6_V6ONLY, &one, sizeof(one));
if (r < 0) {
r = -errno;
goto fail;
}
r = setsockopt(m->llmnr_ipv6_tcp_fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
if (r < 0) {
r = -errno;
goto fail;
}
r = setsockopt(m->llmnr_ipv6_tcp_fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &one, sizeof(one));
if (r < 0) {
r = -errno;
goto fail;
}
r = setsockopt(m->llmnr_ipv6_tcp_fd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &one, sizeof(one));
if (r < 0) {
r = -errno;
goto fail;
}
r = bind(m->llmnr_ipv6_tcp_fd, &sa.sa, sizeof(sa.in6));
if (r < 0) {
r = -errno;
goto fail;
}
r = listen(m->llmnr_ipv6_tcp_fd, SOMAXCONN);
if (r < 0) {
r = -errno;
goto fail;
}
r = sd_event_add_io(m->event, &m->llmnr_ipv6_tcp_event_source, m->llmnr_ipv6_tcp_fd, EPOLLIN, on_llmnr_stream, m);
if (r < 0) {
r = -errno;
goto fail;
}
return m->llmnr_ipv6_tcp_fd;
fail:
m->llmnr_ipv6_tcp_fd = safe_close(m->llmnr_ipv6_tcp_fd);
return r;
}
int manager_ifindex_is_loopback(Manager *m, int ifindex) {
Link *l;
assert(m);
if (ifindex <= 0)
return -EINVAL;
l = hashmap_get(m->links, INT_TO_PTR(ifindex));
if (l->flags & IFF_LOOPBACK)
return 1;
return 0;
}
int manager_find_ifindex(Manager *m, int family, const union in_addr_union *in_addr) {
LinkAddress *a;
assert(m);
a = manager_find_link_address(m, family, in_addr);
if (a)
return a->link->ifindex;
return 0;
}
void manager_refresh_rrs(Manager *m) {
Iterator i;
Link *l;
assert(m);
m->host_ipv4_key = dns_resource_key_unref(m->host_ipv4_key);
m->host_ipv6_key = dns_resource_key_unref(m->host_ipv6_key);
HASHMAP_FOREACH(l, m->links, i) {
link_add_rrs(l, true);
link_add_rrs(l, false);
}
}
int manager_next_hostname(Manager *m) {
const char *p;
uint64_t u;
char *h;
assert(m);
p = strchr(m->hostname, 0);
assert(p);
while (p > m->hostname) {
if (!strchr("0123456789", p[-1]))
break;
p--;
}
if (*p == 0 || safe_atou64(p, &u) < 0 || u <= 0)
u = 1;
u++;
if (asprintf(&h, "%.*s%" PRIu64, (int) (p - m->hostname), m->hostname, u) < 0)
return -ENOMEM;
log_info("Hostname conflict, changing published hostname from '%s' to '%s'.", m->hostname, h);
free(m->hostname);
m->hostname = h;
manager_refresh_rrs(m);
return 0;
}
LinkAddress* manager_find_link_address(Manager *m, int family, const union in_addr_union *in_addr) {
Iterator i;
Link *l;
assert(m);
HASHMAP_FOREACH(l, m->links, i) {
LinkAddress *a;
a = link_find_address(l, family, in_addr);
if (a)
return a;
}
return NULL;
}
int manager_our_packet(Manager *m, DnsPacket *p) {
assert(m);
assert(p);
return !!manager_find_link_address(m, p->family, &p->sender);
}
static const char* const support_table[_SUPPORT_MAX] = {
[SUPPORT_NO] = "no",
[SUPPORT_YES] = "yes",
[SUPPORT_RESOLVE] = "resolve",
};
DEFINE_STRING_TABLE_LOOKUP(support, Support);