4 This file is part of avahi.
6 avahi is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
11 avahi is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
14 Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with avahi; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
27 #include <sys/socket.h>
28 #include <asm/types.h>
29 #include <linux/netlink.h>
30 #include <linux/rtnetlink.h>
35 #include <avahi-common/error.h>
36 #include <avahi-common/malloc.h>
46 static void update_address_rr(AvahiInterfaceMonitor *m, AvahiInterfaceAddress *a, int remove_rrs) {
50 if (avahi_interface_address_relevant(a) &&
52 m->server->config.publish_addresses &&
53 (m->server->state == AVAHI_SERVER_RUNNING ||
54 m->server->state == AVAHI_SERVER_REGISTERING)) {
56 /* Fill the entry group */
58 a->entry_group = avahi_s_entry_group_new(m->server, avahi_host_rr_entry_group_callback, NULL);
60 if (!a->entry_group) /* OOM */
63 if (avahi_s_entry_group_is_empty(a->entry_group)) {
65 if (avahi_server_add_address(m->server, a->entry_group, a->interface->hardware->index, a->interface->protocol, 0, NULL, &a->address) < 0) {
66 avahi_log_warn(__FILE__": avahi_server_add_address() failed: %s", avahi_strerror(m->server->error));
67 avahi_s_entry_group_free(a->entry_group);
68 a->entry_group = NULL;
72 avahi_s_entry_group_commit(a->entry_group);
76 /* Clear the entry group */
78 if (a->entry_group && !avahi_s_entry_group_is_empty(a->entry_group)) {
80 if (avahi_s_entry_group_get_state(a->entry_group) == AVAHI_ENTRY_GROUP_REGISTERING)
81 avahi_server_decrease_host_rr_pending(m->server);
83 avahi_s_entry_group_reset(a->entry_group);
88 static void update_interface_rr(AvahiInterfaceMonitor *m, AvahiInterface *i, int remove_rrs) {
89 AvahiInterfaceAddress *a;
94 for (a = i->addresses; a; a = a->address_next)
95 update_address_rr(m, a, remove_rrs);
98 static void update_hw_interface_rr(AvahiInterfaceMonitor *m, AvahiHwInterface *hw, int remove_rrs) {
104 for (i = hw->interfaces; i; i = i->by_hardware_next)
105 update_interface_rr(m, i, remove_rrs);
108 m->server->config.publish_workstation &&
109 (m->server->state == AVAHI_SERVER_RUNNING ||
110 m->server->state == AVAHI_SERVER_REGISTERING)) {
112 if (!hw->entry_group)
113 hw->entry_group = avahi_s_entry_group_new(m->server, avahi_host_rr_entry_group_callback, NULL);
115 if (!hw->entry_group)
118 if (avahi_s_entry_group_is_empty(hw->entry_group)) {
122 if (!(t = avahi_format_mac_address(hw->mac_address, hw->mac_address_size)))
125 name = avahi_strdup_printf("%s [%s]", m->server->host_name, t);
131 if (avahi_server_add_service(m->server, hw->entry_group, hw->index, AVAHI_PROTO_UNSPEC, name, "_workstation._tcp", NULL, NULL, 9, NULL) < 0) {
132 avahi_log_warn(__FILE__": avahi_server_add_service() failed.");
133 avahi_s_entry_group_free(hw->entry_group);
134 hw->entry_group = NULL;
136 avahi_s_entry_group_commit(hw->entry_group);
143 if (hw->entry_group && !avahi_s_entry_group_is_empty(hw->entry_group)) {
145 if (avahi_s_entry_group_get_state(hw->entry_group) == AVAHI_ENTRY_GROUP_REGISTERING)
146 avahi_server_decrease_host_rr_pending(m->server);
148 avahi_s_entry_group_reset(hw->entry_group);
153 static void free_address(AvahiInterfaceMonitor *m, AvahiInterfaceAddress *a) {
156 assert(a->interface);
158 update_address_rr(m, a, 1);
159 AVAHI_LLIST_REMOVE(AvahiInterfaceAddress, address, a->interface->addresses, a);
162 avahi_s_entry_group_free(a->entry_group);
167 static void free_interface(AvahiInterfaceMonitor *m, AvahiInterface *i, int send_goodbye) {
171 avahi_goodbye_interface(m->server, i, send_goodbye);
172 avahi_response_scheduler_force(i->response_scheduler);
174 assert(!i->announcements);
176 update_interface_rr(m, i, 1);
179 free_address(m, i->addresses);
181 avahi_response_scheduler_free(i->response_scheduler);
182 avahi_query_scheduler_free(i->query_scheduler);
183 avahi_probe_scheduler_free(i->probe_scheduler);
184 avahi_cache_free(i->cache);
186 AVAHI_LLIST_REMOVE(AvahiInterface, interface, m->interfaces, i);
187 AVAHI_LLIST_REMOVE(AvahiInterface, by_hardware, i->hardware->interfaces, i);
192 static void free_hw_interface(AvahiInterfaceMonitor *m, AvahiHwInterface *hw, int send_goodbye) {
196 update_hw_interface_rr(m, hw, 1);
198 while (hw->interfaces)
199 free_interface(m, hw->interfaces, send_goodbye);
202 avahi_s_entry_group_free(hw->entry_group);
204 AVAHI_LLIST_REMOVE(AvahiHwInterface, hardware, m->hw_interfaces, hw);
205 avahi_hashmap_remove(m->hashmap, &hw->index);
207 avahi_free(hw->name);
211 static AvahiInterfaceAddress* get_address(AvahiInterfaceMonitor *m, AvahiInterface *i, const AvahiAddress *raddr) {
212 AvahiInterfaceAddress *ia;
218 for (ia = i->addresses; ia; ia = ia->address_next)
219 if (avahi_address_cmp(&ia->address, raddr) == 0)
225 static int netlink_list_items(AvahiNetlink *nl, uint16_t type, unsigned *ret_seq) {
227 struct rtgenmsg *gen;
230 memset(&req, 0, sizeof(req));
231 n = (struct nlmsghdr*) req;
232 n->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtgenmsg));
233 n->nlmsg_type = type;
234 n->nlmsg_flags = NLM_F_ROOT/*|NLM_F_MATCH*/|NLM_F_REQUEST;
238 memset(gen, 0, sizeof(struct rtgenmsg));
239 gen->rtgen_family = AF_UNSPEC;
241 return avahi_netlink_send(nl, n, ret_seq);
244 static void new_interface(AvahiInterfaceMonitor *m, AvahiHwInterface *hw, AvahiProtocol protocol) {
249 assert(protocol != AVAHI_PROTO_UNSPEC);
251 if (!(i = avahi_new(AvahiInterface, 1)))
256 i->protocol = protocol;
259 AVAHI_LLIST_HEAD_INIT(AvahiInterfaceAddress, i->addresses);
260 AVAHI_LLIST_HEAD_INIT(AvahiAnnouncement, i->announcements);
262 i->cache = avahi_cache_new(m->server, i);
263 i->response_scheduler = avahi_response_scheduler_new(i);
264 i->query_scheduler = avahi_query_scheduler_new(i);
265 i->probe_scheduler = avahi_probe_scheduler_new(i);
267 if (!i->cache || !i->response_scheduler || !i->query_scheduler || !i->probe_scheduler)
270 AVAHI_LLIST_PREPEND(AvahiInterface, by_hardware, hw->interfaces, i);
271 AVAHI_LLIST_PREPEND(AvahiInterface, interface, m->interfaces, i);
278 avahi_cache_free(i->cache);
279 if (i->response_scheduler)
280 avahi_response_scheduler_free(i->response_scheduler);
281 if (i->query_scheduler)
282 avahi_query_scheduler_free(i->query_scheduler);
283 if (i->probe_scheduler)
284 avahi_probe_scheduler_free(i->probe_scheduler);
289 static void check_interface_relevant(AvahiInterfaceMonitor *m, AvahiInterface *i) {
295 b = avahi_interface_relevant(i);
297 if (b && !i->announcing) {
298 avahi_log_debug("New relevant interface %s.%i (#%i)", i->hardware->name, i->protocol, i->hardware->index);
300 if (i->protocol == AVAHI_PROTO_INET)
301 avahi_mdns_mcast_join_ipv4(m->server->fd_ipv4, i->hardware->index);
302 if (i->protocol == AVAHI_PROTO_INET6)
303 avahi_mdns_mcast_join_ipv6(m->server->fd_ipv6, i->hardware->index);
306 avahi_announce_interface(m->server, i);
307 avahi_browser_new_interface(m->server, i);
308 } else if (!b && i->announcing) {
309 avahi_log_debug("Interface %s.%i no longer relevant", i->hardware->name, i->protocol);
311 if (i->protocol == AVAHI_PROTO_INET)
312 avahi_mdns_mcast_leave_ipv4(m->server->fd_ipv4, i->hardware->index);
313 if (i->protocol == AVAHI_PROTO_INET6)
314 avahi_mdns_mcast_leave_ipv6(m->server->fd_ipv6, i->hardware->index);
316 avahi_goodbye_interface(m->server, i, 0);
317 avahi_response_scheduler_clear(i->response_scheduler);
318 avahi_query_scheduler_clear(i->query_scheduler);
319 avahi_probe_scheduler_clear(i->probe_scheduler);
320 avahi_cache_flush(i->cache);
326 static void check_hw_interface_relevant(AvahiInterfaceMonitor *m, AvahiHwInterface *hw) {
332 for (i = hw->interfaces; i; i = i->by_hardware_next)
333 check_interface_relevant(m, i);
336 static void netlink_callback(AvahiNetlink *nl, struct nlmsghdr *n, void* userdata) {
337 AvahiInterfaceMonitor *m = userdata;
341 assert(m->netlink == nl);
343 if (n->nlmsg_type == RTM_NEWLINK) {
344 struct ifinfomsg *ifinfomsg = NLMSG_DATA(n);
345 AvahiHwInterface *hw;
346 struct rtattr *a = NULL;
349 if (ifinfomsg->ifi_family != AF_UNSPEC)
352 if (!(hw = avahi_hashmap_lookup(m->hashmap, &ifinfomsg->ifi_index))) {
354 if (!(hw = avahi_new(AvahiHwInterface, 1)))
361 hw->index = (AvahiIfIndex) ifinfomsg->ifi_index;
362 hw->mac_address_size = 0;
363 hw->entry_group = NULL;
365 AVAHI_LLIST_HEAD_INIT(AvahiInterface, hw->interfaces);
366 AVAHI_LLIST_PREPEND(AvahiHwInterface, hardware, m->hw_interfaces, hw);
368 avahi_hashmap_insert(m->hashmap, &hw->index, hw);
370 if (m->server->fd_ipv4 >= 0)
371 new_interface(m, hw, AVAHI_PROTO_INET);
372 if (m->server->fd_ipv6 >= 0)
373 new_interface(m, hw, AVAHI_PROTO_INET6);
376 hw->flags = ifinfomsg->ifi_flags;
378 l = NLMSG_PAYLOAD(n, sizeof(struct ifinfomsg));
379 a = IFLA_RTA(ifinfomsg);
381 while (RTA_OK(a, l)) {
382 switch(a->rta_type) {
384 avahi_free(hw->name);
385 hw->name = avahi_strndup(RTA_DATA(a), RTA_PAYLOAD(a));
389 assert(RTA_PAYLOAD(a) == sizeof(unsigned int));
390 hw->mtu = *((unsigned int*) RTA_DATA(a));
394 hw->mac_address_size = RTA_PAYLOAD(a);
395 if (hw->mac_address_size > AVAHI_MAX_MAC_ADDRESS)
396 hw->mac_address_size = AVAHI_MAX_MAC_ADDRESS;
398 memcpy(hw->mac_address, RTA_DATA(a), hw->mac_address_size);
409 update_hw_interface_rr(m, hw, 0);
410 check_hw_interface_relevant(m, hw);
412 } else if (n->nlmsg_type == RTM_DELLINK) {
413 struct ifinfomsg *ifinfomsg = NLMSG_DATA(n);
414 AvahiHwInterface *hw;
416 if (ifinfomsg->ifi_family != AF_UNSPEC)
419 if (!(hw = avahi_interface_monitor_get_hw_interface(m, (AvahiIfIndex) ifinfomsg->ifi_index)))
422 update_hw_interface_rr(m, hw, 1);
423 free_hw_interface(m, hw, 0);
425 } else if (n->nlmsg_type == RTM_NEWADDR || n->nlmsg_type == RTM_DELADDR) {
427 struct ifaddrmsg *ifaddrmsg = NLMSG_DATA(n);
429 struct rtattr *a = NULL;
434 if (ifaddrmsg->ifa_family != AVAHI_PROTO_INET && ifaddrmsg->ifa_family != AVAHI_PROTO_INET6)
437 if (!(i = (AvahiInterface*) avahi_interface_monitor_get_interface(m, (AvahiIfIndex) ifaddrmsg->ifa_index, (AvahiProtocol) ifaddrmsg->ifa_family)))
440 raddr.family = (AvahiProtocol) ifaddrmsg->ifa_family;
442 l = NLMSG_PAYLOAD(n, sizeof(struct ifaddrmsg));
443 a = IFA_RTA(ifaddrmsg);
445 while (RTA_OK(a, l)) {
447 switch(a->rta_type) {
449 if ((raddr.family == AVAHI_PROTO_INET6 && RTA_PAYLOAD(a) != 16) ||
450 (raddr.family == AVAHI_PROTO_INET && RTA_PAYLOAD(a) != 4))
453 memcpy(raddr.data.data, RTA_DATA(a), RTA_PAYLOAD(a));
468 if (n->nlmsg_type == RTM_NEWADDR) {
469 AvahiInterfaceAddress *addr;
471 if (!(addr = get_address(m, i, &raddr))) {
472 if (!(addr = avahi_new(AvahiInterfaceAddress, 1)))
476 addr->address = raddr;
478 addr->entry_group = NULL;
480 AVAHI_LLIST_PREPEND(AvahiInterfaceAddress, address, i->addresses, addr);
483 addr->flags = ifaddrmsg->ifa_flags;
484 addr->scope = ifaddrmsg->ifa_scope;
485 addr->prefix_len = ifaddrmsg->ifa_prefixlen;
487 update_address_rr(m, addr, 0);
489 AvahiInterfaceAddress *addr;
491 if (!(addr = get_address(m, i, &raddr)))
494 update_address_rr(m, addr, 1);
495 free_address(m, addr);
498 check_interface_relevant(m, i);
500 } else if (n->nlmsg_type == NLMSG_DONE) {
502 if (m->list == LIST_IFACE) {
505 if (netlink_list_items(m->netlink, RTM_GETADDR, &m->query_addr_seq) < 0)
506 avahi_log_warn("NETLINK: Failed to list addrs: %s", strerror(errno));
511 avahi_log_debug("Network interface enumeration completed");
514 } else if (n->nlmsg_type == NLMSG_ERROR && (n->nlmsg_seq == m->query_link_seq || n->nlmsg_seq == m->query_addr_seq)) {
515 struct nlmsgerr *e = NLMSG_DATA (n);
518 avahi_log_warn("NETLINK: Failed to browse: %s", strerror(-e->error));
522 AvahiInterfaceMonitor *avahi_interface_monitor_new(AvahiServer *s) {
523 AvahiInterfaceMonitor *m = NULL;
525 if (!(m = avahi_new0(AvahiInterfaceMonitor, 1)))
526 return NULL; /* OOM */
529 if (!(m->netlink = avahi_netlink_new(s->poll_api, RTMGRP_LINK|RTMGRP_IPV4_IFADDR|RTMGRP_IPV6_IFADDR, netlink_callback, m)))
532 m->hashmap = avahi_hashmap_new(avahi_int_hash, avahi_int_equal, NULL, NULL);
534 AVAHI_LLIST_HEAD_INIT(AvahiInterface, m->interfaces);
535 AVAHI_LLIST_HEAD_INIT(AvahiHwInterface, m->hw_interfaces);
537 if (netlink_list_items(m->netlink, RTM_GETLINK, &m->query_link_seq) < 0)
540 m->list = LIST_IFACE;
545 avahi_interface_monitor_free(m);
549 void avahi_interface_monitor_sync(AvahiInterfaceMonitor *m) {
552 while (m->list != LIST_DONE) {
553 if (!avahi_netlink_work(m->netlink, 1))
558 void avahi_interface_monitor_free(AvahiInterfaceMonitor *m) {
561 while (m->hw_interfaces)
562 free_hw_interface(m, m->hw_interfaces, 1);
564 assert(!m->interfaces);
568 avahi_netlink_free(m->netlink);
571 avahi_hashmap_free(m->hashmap);
577 AvahiInterface* avahi_interface_monitor_get_interface(AvahiInterfaceMonitor *m, AvahiIfIndex idx, AvahiProtocol protocol) {
578 AvahiHwInterface *hw;
583 assert(protocol != AVAHI_PROTO_UNSPEC);
585 if (!(hw = avahi_interface_monitor_get_hw_interface(m, idx)))
588 for (i = hw->interfaces; i; i = i->by_hardware_next)
589 if (i->protocol == protocol)
595 AvahiHwInterface* avahi_interface_monitor_get_hw_interface(AvahiInterfaceMonitor *m, AvahiIfIndex idx) {
599 return avahi_hashmap_lookup(m->hashmap, &idx);
602 void avahi_interface_send_packet_unicast(AvahiInterface *i, AvahiDnsPacket *p, const AvahiAddress *a, uint16_t port) {
607 if (!avahi_interface_relevant(i))
610 assert(!a || a->family == i->protocol);
613 /* avahi_log_debug("unicast sending on '%s.%i' to %s:%u", i->hardware->name, i->protocol, avahi_address_snprint(t, sizeof(t), a), port); */
615 /* avahi_log_debug("multicast sending on '%s.%i'", i->hardware->name, i->protocol); */
617 if (i->protocol == AVAHI_PROTO_INET && i->monitor->server->fd_ipv4 >= 0)
618 avahi_send_dns_packet_ipv4(i->monitor->server->fd_ipv4, i->hardware->index, p, a ? &a->data.ipv4 : NULL, port);
619 else if (i->protocol == AVAHI_PROTO_INET6 && i->monitor->server->fd_ipv6 >= 0)
620 avahi_send_dns_packet_ipv6(i->monitor->server->fd_ipv6, i->hardware->index, p, a ? &a->data.ipv6 : NULL, port);
623 void avahi_interface_send_packet(AvahiInterface *i, AvahiDnsPacket *p) {
627 avahi_interface_send_packet_unicast(i, p, NULL, 0);
630 int avahi_interface_post_query(AvahiInterface *i, AvahiKey *key, int immediately) {
634 if (avahi_interface_relevant(i))
635 return avahi_query_scheduler_post(i->query_scheduler, key, immediately);
640 int avahi_interface_post_response(AvahiInterface *i, AvahiRecord *record, int flush_cache, const AvahiAddress *querier, int immediately) {
644 if (avahi_interface_relevant(i))
645 return avahi_response_scheduler_post(i->response_scheduler, record, flush_cache, querier, immediately);
650 int avahi_interface_post_probe(AvahiInterface *i, AvahiRecord *record, int immediately) {
654 if (avahi_interface_relevant(i))
655 return avahi_probe_scheduler_post(i->probe_scheduler, record, immediately);
660 int avahi_dump_caches(AvahiInterfaceMonitor *m, AvahiDumpCallback callback, void* userdata) {
664 for (i = m->interfaces; i; i = i->interface_next) {
665 if (avahi_interface_relevant(i)) {
667 snprintf(ln, sizeof(ln), ";;; INTERFACE %s.%i ;;;", i->hardware->name, i->protocol);
668 callback(ln, userdata);
669 if (avahi_cache_dump(i->cache, callback, userdata) < 0)
677 int avahi_interface_relevant(AvahiInterface *i) {
678 AvahiInterfaceAddress *a;
679 int relevant_address;
683 relevant_address = 0;
685 for (a = i->addresses; a; a = a->address_next)
686 if (avahi_interface_address_relevant(a)) {
687 relevant_address = 1;
691 /* avahi_log_debug("%p. iface-relevant: %i %i %i %i %i %i", i, relevant_address, */
692 /* (i->hardware->flags & IFF_UP), */
693 /* (i->hardware->flags & IFF_RUNNING), */
694 /* !(i->hardware->flags & IFF_LOOPBACK), */
695 /* (i->hardware->flags & IFF_MULTICAST), */
696 /* !(i->hardware->flags & IFF_POINTOPOINT)); */
699 (i->hardware->flags & IFF_UP) &&
700 (!i->monitor->server->config.use_iff_running || (i->hardware->flags & IFF_RUNNING)) &&
701 !(i->hardware->flags & IFF_LOOPBACK) &&
702 (i->hardware->flags & IFF_MULTICAST) &&
703 !(i->hardware->flags & IFF_POINTOPOINT) &&
707 int avahi_interface_address_relevant(AvahiInterfaceAddress *a) {
710 return a->scope == RT_SCOPE_UNIVERSE;
714 int avahi_interface_match(AvahiInterface *i, AvahiIfIndex idx, AvahiProtocol protocol) {
717 if (idx > 0 && idx != i->hardware->index)
720 if (protocol != AVAHI_PROTO_UNSPEC && protocol != i->protocol)
726 void avahi_interface_monitor_walk(AvahiInterfaceMonitor *m, AvahiIfIndex interface, AvahiProtocol protocol, AvahiInterfaceMonitorWalkCallback callback, void* userdata) {
731 if (protocol != AVAHI_PROTO_UNSPEC) {
734 if ((i = avahi_interface_monitor_get_interface(m, interface, protocol)))
735 callback(m, i, userdata);
738 AvahiHwInterface *hw;
741 if ((hw = avahi_interface_monitor_get_hw_interface(m, interface)))
742 for (i = hw->interfaces; i; i = i->by_hardware_next)
743 if (avahi_interface_match(i, interface, protocol))
744 callback(m, i, userdata);
750 for (i = m->interfaces; i; i = i->interface_next)
751 if (avahi_interface_match(i, interface, protocol))
752 callback(m, i, userdata);
756 void avahi_update_host_rrs(AvahiInterfaceMonitor *m, int remove_rrs) {
757 AvahiHwInterface *hw;
761 for (hw = m->hw_interfaces; hw; hw = hw->hardware_next)
762 update_hw_interface_rr(m, hw, remove_rrs);
765 int avahi_address_is_local(AvahiInterfaceMonitor *m, const AvahiAddress *a) {
767 AvahiInterfaceAddress *ia;
771 for (i = m->interfaces; i; i = i->interface_next)
772 for (ia = i->addresses; ia; ia = ia->address_next)
773 if (avahi_address_cmp(a, &ia->address) == 0)
779 int avahi_interface_address_on_link(AvahiInterface *i, const AvahiAddress *a) {
780 AvahiInterfaceAddress *ia;
785 if (a->family != i->protocol)
788 for (ia = i->addresses; ia; ia = ia->address_next) {
790 if (a->family == AVAHI_PROTO_INET) {
793 m = ~(((uint32_t) -1) >> ia->prefix_len);
795 if ((ntohl(a->data.ipv4.address) & m) == (ntohl(ia->address.data.ipv4.address) & m))
800 assert(a->family == AVAHI_PROTO_INET6);
804 for (j = 0; j < 16; j++) {
818 if ((a->data.ipv6.address[j] & m) != (ia->address.data.ipv6.address[j] & m))