2 #include <sys/socket.h>
4 #include <linux/netlink.h>
5 #include <linux/rtnetlink.h>
15 static void update_address_rr(flxInterfaceMonitor *m, flxInterfaceAddress *a, int remove) {
19 if (!flx_interface_address_relevant(a) || remove) {
21 flx_server_remove(m->server, a->rr_id);
26 a->rr_id = flx_server_get_next_id(m->server);
27 flx_server_add_address(m->server, a->rr_id, a->interface->hardware->index, AF_UNSPEC, 0, NULL, &a->address);
32 static void update_interface_rr(flxInterfaceMonitor *m, flxInterface *i, int remove) {
33 flxInterfaceAddress *a;
37 for (a = i->addresses; a; a = a->address_next)
38 update_address_rr(m, a, remove);
41 static void update_hw_interface_rr(flxInterfaceMonitor *m, flxHwInterface *hw, int remove) {
47 for (i = hw->interfaces; i; i = i->by_hardware_next)
48 update_interface_rr(m, i, remove);
51 static void free_address(flxInterfaceMonitor *m, flxInterfaceAddress *a) {
54 g_assert(a->interface);
56 FLX_LLIST_REMOVE(flxInterfaceAddress, address, a->interface->addresses, a);
57 flx_server_remove(m->server, a->rr_id);
62 static void free_interface(flxInterfaceMonitor *m, flxInterface *i, gboolean send_goodbye) {
66 g_message("removing interface %s.%i", i->hardware->name, i->protocol);
67 flx_goodbye_interface(m->server, i, send_goodbye);
68 g_message("flushing...");
69 flx_packet_scheduler_flush_responses(i->scheduler);
72 g_assert(!i->announcements);
75 free_address(m, i->addresses);
77 flx_packet_scheduler_free(i->scheduler);
78 flx_cache_free(i->cache);
80 FLX_LLIST_REMOVE(flxInterface, interface, m->interfaces, i);
81 FLX_LLIST_REMOVE(flxInterface, by_hardware, i->hardware->interfaces, i);
86 static void free_hw_interface(flxInterfaceMonitor *m, flxHwInterface *hw, gboolean send_goodbye) {
90 while (hw->interfaces)
91 free_interface(m, hw->interfaces, send_goodbye);
93 FLX_LLIST_REMOVE(flxHwInterface, hardware, m->hw_interfaces, hw);
94 g_hash_table_remove(m->hash_table, &hw->index);
100 static flxInterfaceAddress* get_address(flxInterfaceMonitor *m, flxInterface *i, const flxAddress *raddr) {
101 flxInterfaceAddress *ia;
107 for (ia = i->addresses; ia; ia = ia->address_next)
108 if (flx_address_cmp(&ia->address, raddr) == 0)
114 static int netlink_list_items(flxNetlink *nl, guint16 type, guint *ret_seq) {
116 struct rtgenmsg *gen;
119 memset(&req, 0, sizeof(req));
120 n = (struct nlmsghdr*) req;
121 n->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtgenmsg));
122 n->nlmsg_type = type;
123 n->nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST;
127 memset(gen, 0, sizeof(struct rtgenmsg));
128 gen->rtgen_family = AF_UNSPEC;
130 return flx_netlink_send(nl, n, ret_seq);
133 static void new_interface(flxInterfaceMonitor *m, flxHwInterface *hw, guchar protocol) {
138 g_assert(protocol != AF_UNSPEC);
140 i = g_new(flxInterface, 1);
143 i->protocol = protocol;
144 i->announcing = FALSE;
146 FLX_LLIST_HEAD_INIT(flxInterfaceAddress, i->addresses);
147 FLX_LLIST_HEAD_INIT(flxAnnouncement, i->announcements);
149 i->cache = flx_cache_new(m->server, i);
150 i->scheduler = flx_packet_scheduler_new(m->server, i);
152 FLX_LLIST_PREPEND(flxInterface, by_hardware, hw->interfaces, i);
153 FLX_LLIST_PREPEND(flxInterface, interface, m->interfaces, i);
156 static void check_interface_relevant(flxInterfaceMonitor *m, flxInterface *i) {
162 b = flx_interface_relevant(i);
164 if (b && !i->announcing) {
165 g_message("New relevant interface %s.%i", i->hardware->name, i->protocol);
167 if (i->protocol == AF_INET)
168 flx_mdns_mcast_join_ipv4 (i->hardware->index, m->server->fd_ipv4);
169 if (i->protocol == AF_INET6)
170 flx_mdns_mcast_join_ipv6 (i->hardware->index, m->server->fd_ipv6);
172 i->announcing = TRUE;
173 flx_announce_interface(m->server, i);
174 } else if (!b && i->announcing) {
175 g_message("Interface %s.%i no longer relevant", i->hardware->name, i->protocol);
177 flx_goodbye_interface(m->server, i, FALSE);
179 if (i->protocol == AF_INET)
180 flx_mdns_mcast_leave_ipv4 (i->hardware->index, m->server->fd_ipv4);
181 if (i->protocol == AF_INET6)
182 flx_mdns_mcast_leave_ipv6 (i->hardware->index, m->server->fd_ipv6);
184 i->announcing = FALSE;
188 static void check_hw_interface_relevant(flxInterfaceMonitor *m, flxHwInterface *hw) {
194 for (i = hw->interfaces; i; i = i->by_hardware_next)
195 check_interface_relevant(m, i);
198 static void callback(flxNetlink *nl, struct nlmsghdr *n, gpointer userdata) {
199 flxInterfaceMonitor *m = userdata;
203 g_assert(m->netlink == nl);
205 if (n->nlmsg_type == RTM_NEWLINK) {
206 struct ifinfomsg *ifinfomsg = NLMSG_DATA(n);
208 struct rtattr *a = NULL;
211 if (ifinfomsg->ifi_family != AF_UNSPEC)
214 if (!(hw = g_hash_table_lookup(m->hash_table, &ifinfomsg->ifi_index))) {
215 hw = g_new(flxHwInterface, 1);
220 hw->index = ifinfomsg->ifi_index;
222 FLX_LLIST_HEAD_INIT(flxInterface, hw->interfaces);
223 FLX_LLIST_PREPEND(flxHwInterface, hardware, m->hw_interfaces, hw);
225 g_hash_table_insert(m->hash_table, &hw->index, hw);
227 if (m->server->fd_ipv4 >= 0)
228 new_interface(m, hw, AF_INET);
229 if (m->server->fd_ipv6 >= 0)
230 new_interface(m, hw, AF_INET6);
233 hw->flags = ifinfomsg->ifi_flags;
235 l = NLMSG_PAYLOAD(n, sizeof(struct ifinfomsg));
236 a = IFLA_RTA(ifinfomsg);
238 while (RTA_OK(a, l)) {
239 switch(a->rta_type) {
242 hw->name = g_strndup(RTA_DATA(a), RTA_PAYLOAD(a));
246 g_assert(RTA_PAYLOAD(a) == sizeof(unsigned int));
247 hw->mtu = *((unsigned int*) RTA_DATA(a));
257 update_hw_interface_rr(m, hw, FALSE);
258 check_hw_interface_relevant(m, hw);
260 } else if (n->nlmsg_type == RTM_DELLINK) {
261 struct ifinfomsg *ifinfomsg = NLMSG_DATA(n);
265 if (ifinfomsg->ifi_family != AF_UNSPEC)
268 if (!(hw = flx_interface_monitor_get_hw_interface(m, ifinfomsg->ifi_index)))
271 update_hw_interface_rr(m, hw, TRUE);
272 free_hw_interface(m, hw, FALSE);
274 } else if (n->nlmsg_type == RTM_NEWADDR || n->nlmsg_type == RTM_DELADDR) {
276 struct ifaddrmsg *ifaddrmsg = NLMSG_DATA(n);
278 struct rtattr *a = NULL;
283 if (ifaddrmsg->ifa_family != AF_INET && ifaddrmsg->ifa_family != AF_INET6)
286 if (!(i = (flxInterface*) flx_interface_monitor_get_interface(m, ifaddrmsg->ifa_index, ifaddrmsg->ifa_family)))
289 raddr.family = ifaddrmsg->ifa_family;
291 l = NLMSG_PAYLOAD(n, sizeof(struct ifinfomsg));
292 a = IFA_RTA(ifaddrmsg);
294 while (RTA_OK(a, l)) {
295 switch(a->rta_type) {
297 if ((raddr.family == AF_INET6 && RTA_PAYLOAD(a) != 16) ||
298 (raddr.family == AF_INET && RTA_PAYLOAD(a) != 4))
301 memcpy(raddr.data.data, RTA_DATA(a), RTA_PAYLOAD(a));
317 if (n->nlmsg_type == RTM_NEWADDR) {
318 flxInterfaceAddress *addr;
320 if (!(addr = get_address(m, i, &raddr))) {
321 addr = g_new(flxInterfaceAddress, 1);
323 addr->address = raddr;
327 FLX_LLIST_PREPEND(flxInterfaceAddress, address, i->addresses, addr);
330 addr->flags = ifaddrmsg->ifa_flags;
331 addr->scope = ifaddrmsg->ifa_scope;
333 update_address_rr(m, addr, FALSE);
334 check_interface_relevant(m, i);
336 flxInterfaceAddress *addr;
338 if (!(addr = get_address(m, i, &raddr)))
341 update_address_rr(m, addr, TRUE);
342 free_address(m, addr);
344 check_interface_relevant(m, i);
347 } else if (n->nlmsg_type == NLMSG_DONE) {
349 if (m->list == LIST_IFACE) {
352 if (netlink_list_items(m->netlink, RTM_GETADDR, &m->query_addr_seq) < 0)
353 g_warning("NETLINK: Failed to list addrs: %s", strerror(errno));
358 g_message("Enumeration complete");
361 } else if (n->nlmsg_type == NLMSG_ERROR && (n->nlmsg_seq == m->query_link_seq || n->nlmsg_seq == m->query_addr_seq)) {
362 struct nlmsgerr *e = NLMSG_DATA (n);
365 g_warning("NETLINK: Failed to browse: %s", strerror(-e->error));
369 flxInterfaceMonitor *flx_interface_monitor_new(flxServer *s) {
370 flxInterfaceMonitor *m = NULL;
372 m = g_new0(flxInterfaceMonitor, 1);
374 if (!(m->netlink = flx_netlink_new(s->context, RTMGRP_LINK|RTMGRP_IPV4_IFADDR|RTMGRP_IPV6_IFADDR, callback, m)))
377 m->hash_table = g_hash_table_new(g_int_hash, g_int_equal);
379 FLX_LLIST_HEAD_INIT(flxInterface, m->interfaces);
380 FLX_LLIST_HEAD_INIT(flxHwInterface, m->hw_interfaces);
382 if (netlink_list_items(m->netlink, RTM_GETLINK, &m->query_link_seq) < 0)
385 m->list = LIST_IFACE;
390 flx_interface_monitor_free(m);
394 void flx_interface_monitor_free(flxInterfaceMonitor *m) {
397 while (m->hw_interfaces)
398 free_hw_interface(m, m->hw_interfaces, TRUE);
400 g_assert(!m->interfaces);
404 flx_netlink_free(m->netlink);
407 g_hash_table_destroy(m->hash_table);
413 flxInterface* flx_interface_monitor_get_interface(flxInterfaceMonitor *m, gint index, guchar protocol) {
419 g_assert(protocol != AF_UNSPEC);
421 if (!(hw = flx_interface_monitor_get_hw_interface(m, index)))
424 for (i = hw->interfaces; i; i = i->by_hardware_next)
425 if (i->protocol == protocol)
431 flxHwInterface* flx_interface_monitor_get_hw_interface(flxInterfaceMonitor *m, gint index) {
435 return g_hash_table_lookup(m->hash_table, &index);
439 void flx_interface_send_packet(flxInterface *i, flxDnsPacket *p) {
443 if (flx_interface_relevant(i)) {
444 g_message("sending on '%s.%i'", i->hardware->name, i->protocol);
446 if (i->protocol == AF_INET && i->monitor->server->fd_ipv4 >= 0)
447 flx_send_dns_packet_ipv4(i->monitor->server->fd_ipv4, i->hardware->index, p);
448 else if (i->protocol == AF_INET6 && i->monitor->server->fd_ipv6 >= 0)
449 flx_send_dns_packet_ipv6(i->monitor->server->fd_ipv6, i->hardware->index, p);
453 void flx_interface_post_query(flxInterface *i, flxKey *key, gboolean immediately) {
457 if (flx_interface_relevant(i))
458 flx_packet_scheduler_post_query(i->scheduler, key, immediately);
462 void flx_interface_post_response(flxInterface *i, const flxAddress *a, flxRecord *record, gboolean flush_cache, gboolean immediately) {
466 if (flx_interface_relevant(i))
467 flx_packet_scheduler_post_response(i->scheduler, a, record, flush_cache, immediately);
470 void flx_interface_post_probe(flxInterface *i, flxRecord *record, gboolean immediately) {
474 if (flx_interface_relevant(i))
475 flx_packet_scheduler_post_probe(i->scheduler, record, immediately);
478 void flx_dump_caches(flxInterfaceMonitor *m, FILE *f) {
482 for (i = m->interfaces; i; i = i->interface_next) {
483 if (flx_interface_relevant(i)) {
484 fprintf(f, "\n;;; INTERFACE %s.%i ;;;\n", i->hardware->name, i->protocol);
485 flx_cache_dump(i->cache, f);
491 gboolean flx_interface_relevant(flxInterface *i) {
495 (i->hardware->flags & IFF_UP) &&
496 (i->hardware->flags & IFF_RUNNING) &&
497 !(i->hardware->flags & IFF_LOOPBACK) &&
498 (i->hardware->flags & IFF_MULTICAST) &&
502 gboolean flx_interface_address_relevant(flxInterfaceAddress *a) {
505 return a->scope == RT_SCOPE_UNIVERSE;
509 gboolean flx_interface_match(flxInterface *i, gint index, guchar protocol) {
512 if (index > 0 && index != i->hardware->index)
515 if (protocol != AF_UNSPEC && protocol != i->protocol)
522 void flx_interface_monitor_walk(flxInterfaceMonitor *m, gint interface, guchar protocol, flxInterfaceMonitorWalkCallback callback, gpointer userdata) {
527 if (protocol != AF_UNSPEC) {
530 if ((i = flx_interface_monitor_get_interface(m, interface, protocol)))
531 callback(m, i, userdata);
537 if ((hw = flx_interface_monitor_get_hw_interface(m, interface)))
538 for (i = hw->interfaces; i; i = i->by_hardware_next)
539 if (flx_interface_match(i, interface, protocol))
540 callback(m, i, userdata);
546 for (i = m->interfaces; i; i = i->interface_next)
547 if (flx_interface_match(i, interface, protocol))
548 callback(m, i, userdata);