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_entry_group_free(a->entry_group);
22 a->entry_group = NULL;
25 if (!a->entry_group) {
26 /* a->entry_group = flx_entry_group_new(m->server, NULL, NULL); */
27 /* flx_server_add_address(m->server, a->entry_group, a->interface->hardware->index, AF_UNSPEC, 0, NULL, &a->address); */
28 /* flx_entry_group_commit(a->entry_group); */
33 static void update_interface_rr(flxInterfaceMonitor *m, flxInterface *i, int remove) {
34 flxInterfaceAddress *a;
38 for (a = i->addresses; a; a = a->address_next)
39 update_address_rr(m, a, remove);
42 static void update_hw_interface_rr(flxInterfaceMonitor *m, flxHwInterface *hw, int remove) {
48 for (i = hw->interfaces; i; i = i->by_hardware_next)
49 update_interface_rr(m, i, remove);
52 static void free_address(flxInterfaceMonitor *m, flxInterfaceAddress *a) {
55 g_assert(a->interface);
57 FLX_LLIST_REMOVE(flxInterfaceAddress, address, a->interface->addresses, a);
60 flx_entry_group_free(a->entry_group);
65 static void free_interface(flxInterfaceMonitor *m, flxInterface *i, gboolean send_goodbye) {
69 g_message("removing interface %s.%i", i->hardware->name, i->protocol);
70 flx_goodbye_interface(m->server, i, send_goodbye);
71 g_message("flushing...");
72 flx_packet_scheduler_flush_responses(i->scheduler);
75 g_assert(!i->announcements);
78 free_address(m, i->addresses);
80 flx_packet_scheduler_free(i->scheduler);
81 flx_cache_free(i->cache);
83 FLX_LLIST_REMOVE(flxInterface, interface, m->interfaces, i);
84 FLX_LLIST_REMOVE(flxInterface, by_hardware, i->hardware->interfaces, i);
89 static void free_hw_interface(flxInterfaceMonitor *m, flxHwInterface *hw, gboolean send_goodbye) {
93 while (hw->interfaces)
94 free_interface(m, hw->interfaces, send_goodbye);
96 FLX_LLIST_REMOVE(flxHwInterface, hardware, m->hw_interfaces, hw);
97 g_hash_table_remove(m->hash_table, &hw->index);
103 static flxInterfaceAddress* get_address(flxInterfaceMonitor *m, flxInterface *i, const flxAddress *raddr) {
104 flxInterfaceAddress *ia;
110 for (ia = i->addresses; ia; ia = ia->address_next)
111 if (flx_address_cmp(&ia->address, raddr) == 0)
117 static int netlink_list_items(flxNetlink *nl, guint16 type, guint *ret_seq) {
119 struct rtgenmsg *gen;
122 memset(&req, 0, sizeof(req));
123 n = (struct nlmsghdr*) req;
124 n->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtgenmsg));
125 n->nlmsg_type = type;
126 n->nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST;
130 memset(gen, 0, sizeof(struct rtgenmsg));
131 gen->rtgen_family = AF_UNSPEC;
133 return flx_netlink_send(nl, n, ret_seq);
136 static void new_interface(flxInterfaceMonitor *m, flxHwInterface *hw, guchar protocol) {
141 g_assert(protocol != AF_UNSPEC);
143 i = g_new(flxInterface, 1);
146 i->protocol = protocol;
147 i->announcing = FALSE;
149 FLX_LLIST_HEAD_INIT(flxInterfaceAddress, i->addresses);
150 FLX_LLIST_HEAD_INIT(flxAnnouncement, i->announcements);
152 i->cache = flx_cache_new(m->server, i);
153 i->scheduler = flx_packet_scheduler_new(m->server, i);
155 FLX_LLIST_PREPEND(flxInterface, by_hardware, hw->interfaces, i);
156 FLX_LLIST_PREPEND(flxInterface, interface, m->interfaces, i);
159 static void check_interface_relevant(flxInterfaceMonitor *m, flxInterface *i) {
165 b = flx_interface_relevant(i);
167 if (b && !i->announcing) {
168 g_message("New relevant interface %s.%i", i->hardware->name, i->protocol);
170 if (i->protocol == AF_INET)
171 flx_mdns_mcast_join_ipv4 (i->hardware->index, m->server->fd_ipv4);
172 if (i->protocol == AF_INET6)
173 flx_mdns_mcast_join_ipv6 (i->hardware->index, m->server->fd_ipv6);
175 i->announcing = TRUE;
176 flx_announce_interface(m->server, i);
177 } else if (!b && i->announcing) {
178 g_message("Interface %s.%i no longer relevant", i->hardware->name, i->protocol);
180 flx_goodbye_interface(m->server, i, FALSE);
182 if (i->protocol == AF_INET)
183 flx_mdns_mcast_leave_ipv4 (i->hardware->index, m->server->fd_ipv4);
184 if (i->protocol == AF_INET6)
185 flx_mdns_mcast_leave_ipv6 (i->hardware->index, m->server->fd_ipv6);
187 i->announcing = FALSE;
191 static void check_hw_interface_relevant(flxInterfaceMonitor *m, flxHwInterface *hw) {
197 for (i = hw->interfaces; i; i = i->by_hardware_next)
198 check_interface_relevant(m, i);
201 static void callback(flxNetlink *nl, struct nlmsghdr *n, gpointer userdata) {
202 flxInterfaceMonitor *m = userdata;
206 g_assert(m->netlink == nl);
208 if (n->nlmsg_type == RTM_NEWLINK) {
209 struct ifinfomsg *ifinfomsg = NLMSG_DATA(n);
211 struct rtattr *a = NULL;
214 if (ifinfomsg->ifi_family != AF_UNSPEC)
217 if (!(hw = g_hash_table_lookup(m->hash_table, &ifinfomsg->ifi_index))) {
218 hw = g_new(flxHwInterface, 1);
223 hw->index = ifinfomsg->ifi_index;
225 FLX_LLIST_HEAD_INIT(flxInterface, hw->interfaces);
226 FLX_LLIST_PREPEND(flxHwInterface, hardware, m->hw_interfaces, hw);
228 g_hash_table_insert(m->hash_table, &hw->index, hw);
230 if (m->server->fd_ipv4 >= 0)
231 new_interface(m, hw, AF_INET);
232 if (m->server->fd_ipv6 >= 0)
233 new_interface(m, hw, AF_INET6);
236 hw->flags = ifinfomsg->ifi_flags;
238 l = NLMSG_PAYLOAD(n, sizeof(struct ifinfomsg));
239 a = IFLA_RTA(ifinfomsg);
241 while (RTA_OK(a, l)) {
242 switch(a->rta_type) {
245 hw->name = g_strndup(RTA_DATA(a), RTA_PAYLOAD(a));
249 g_assert(RTA_PAYLOAD(a) == sizeof(unsigned int));
250 hw->mtu = *((unsigned int*) RTA_DATA(a));
260 update_hw_interface_rr(m, hw, FALSE);
261 check_hw_interface_relevant(m, hw);
263 } else if (n->nlmsg_type == RTM_DELLINK) {
264 struct ifinfomsg *ifinfomsg = NLMSG_DATA(n);
268 if (ifinfomsg->ifi_family != AF_UNSPEC)
271 if (!(hw = flx_interface_monitor_get_hw_interface(m, ifinfomsg->ifi_index)))
274 update_hw_interface_rr(m, hw, TRUE);
275 free_hw_interface(m, hw, FALSE);
277 } else if (n->nlmsg_type == RTM_NEWADDR || n->nlmsg_type == RTM_DELADDR) {
279 struct ifaddrmsg *ifaddrmsg = NLMSG_DATA(n);
281 struct rtattr *a = NULL;
286 if (ifaddrmsg->ifa_family != AF_INET && ifaddrmsg->ifa_family != AF_INET6)
289 if (!(i = (flxInterface*) flx_interface_monitor_get_interface(m, ifaddrmsg->ifa_index, ifaddrmsg->ifa_family)))
292 raddr.family = ifaddrmsg->ifa_family;
294 l = NLMSG_PAYLOAD(n, sizeof(struct ifinfomsg));
295 a = IFA_RTA(ifaddrmsg);
297 while (RTA_OK(a, l)) {
298 switch(a->rta_type) {
300 if ((raddr.family == AF_INET6 && RTA_PAYLOAD(a) != 16) ||
301 (raddr.family == AF_INET && RTA_PAYLOAD(a) != 4))
304 memcpy(raddr.data.data, RTA_DATA(a), RTA_PAYLOAD(a));
320 if (n->nlmsg_type == RTM_NEWADDR) {
321 flxInterfaceAddress *addr;
323 if (!(addr = get_address(m, i, &raddr))) {
324 addr = g_new(flxInterfaceAddress, 1);
326 addr->address = raddr;
328 addr->entry_group = NULL;
330 FLX_LLIST_PREPEND(flxInterfaceAddress, address, i->addresses, addr);
333 addr->flags = ifaddrmsg->ifa_flags;
334 addr->scope = ifaddrmsg->ifa_scope;
336 update_address_rr(m, addr, FALSE);
337 check_interface_relevant(m, i);
339 flxInterfaceAddress *addr;
341 if (!(addr = get_address(m, i, &raddr)))
344 update_address_rr(m, addr, TRUE);
345 free_address(m, addr);
347 check_interface_relevant(m, i);
350 } else if (n->nlmsg_type == NLMSG_DONE) {
352 if (m->list == LIST_IFACE) {
355 if (netlink_list_items(m->netlink, RTM_GETADDR, &m->query_addr_seq) < 0)
356 g_warning("NETLINK: Failed to list addrs: %s", strerror(errno));
361 g_message("Enumeration complete");
364 } else if (n->nlmsg_type == NLMSG_ERROR && (n->nlmsg_seq == m->query_link_seq || n->nlmsg_seq == m->query_addr_seq)) {
365 struct nlmsgerr *e = NLMSG_DATA (n);
368 g_warning("NETLINK: Failed to browse: %s", strerror(-e->error));
372 flxInterfaceMonitor *flx_interface_monitor_new(flxServer *s) {
373 flxInterfaceMonitor *m = NULL;
375 m = g_new0(flxInterfaceMonitor, 1);
377 if (!(m->netlink = flx_netlink_new(s->context, G_PRIORITY_DEFAULT-10, RTMGRP_LINK|RTMGRP_IPV4_IFADDR|RTMGRP_IPV6_IFADDR, callback, m)))
380 m->hash_table = g_hash_table_new(g_int_hash, g_int_equal);
382 FLX_LLIST_HEAD_INIT(flxInterface, m->interfaces);
383 FLX_LLIST_HEAD_INIT(flxHwInterface, m->hw_interfaces);
385 if (netlink_list_items(m->netlink, RTM_GETLINK, &m->query_link_seq) < 0)
388 m->list = LIST_IFACE;
393 flx_interface_monitor_free(m);
397 void flx_interface_monitor_sync(flxInterfaceMonitor *m) {
400 while (m->list != LIST_DONE) {
401 if (!flx_netlink_work(m->netlink, TRUE))
406 void flx_interface_monitor_free(flxInterfaceMonitor *m) {
409 while (m->hw_interfaces)
410 free_hw_interface(m, m->hw_interfaces, TRUE);
412 g_assert(!m->interfaces);
416 flx_netlink_free(m->netlink);
419 g_hash_table_destroy(m->hash_table);
425 flxInterface* flx_interface_monitor_get_interface(flxInterfaceMonitor *m, gint index, guchar protocol) {
431 g_assert(protocol != AF_UNSPEC);
433 if (!(hw = flx_interface_monitor_get_hw_interface(m, index)))
436 for (i = hw->interfaces; i; i = i->by_hardware_next)
437 if (i->protocol == protocol)
443 flxHwInterface* flx_interface_monitor_get_hw_interface(flxInterfaceMonitor *m, gint index) {
447 return g_hash_table_lookup(m->hash_table, &index);
451 void flx_interface_send_packet(flxInterface *i, flxDnsPacket *p) {
455 if (flx_interface_relevant(i)) {
456 g_message("sending on '%s.%i'", i->hardware->name, i->protocol);
458 if (i->protocol == AF_INET && i->monitor->server->fd_ipv4 >= 0)
459 flx_send_dns_packet_ipv4(i->monitor->server->fd_ipv4, i->hardware->index, p);
460 else if (i->protocol == AF_INET6 && i->monitor->server->fd_ipv6 >= 0)
461 flx_send_dns_packet_ipv6(i->monitor->server->fd_ipv6, i->hardware->index, p);
465 void flx_interface_post_query(flxInterface *i, flxKey *key, gboolean immediately) {
469 if (flx_interface_relevant(i))
470 flx_packet_scheduler_post_query(i->scheduler, key, immediately);
474 void flx_interface_post_response(flxInterface *i, const flxAddress *a, flxRecord *record, gboolean flush_cache, gboolean immediately) {
478 if (flx_interface_relevant(i))
479 flx_packet_scheduler_post_response(i->scheduler, a, record, flush_cache, immediately);
482 void flx_interface_post_probe(flxInterface *i, flxRecord *record, gboolean immediately) {
486 if (flx_interface_relevant(i))
487 flx_packet_scheduler_post_probe(i->scheduler, record, immediately);
490 void flx_dump_caches(flxInterfaceMonitor *m, FILE *f) {
494 for (i = m->interfaces; i; i = i->interface_next) {
495 if (flx_interface_relevant(i)) {
496 fprintf(f, "\n;;; INTERFACE %s.%i ;;;\n", i->hardware->name, i->protocol);
497 flx_cache_dump(i->cache, f);
503 gboolean flx_interface_relevant(flxInterface *i) {
507 (i->hardware->flags & IFF_UP) &&
508 (i->hardware->flags & IFF_RUNNING) &&
509 !(i->hardware->flags & IFF_LOOPBACK) &&
510 (i->hardware->flags & IFF_MULTICAST) &&
514 gboolean flx_interface_address_relevant(flxInterfaceAddress *a) {
517 return a->scope == RT_SCOPE_UNIVERSE;
521 gboolean flx_interface_match(flxInterface *i, gint index, guchar protocol) {
524 if (index > 0 && index != i->hardware->index)
527 if (protocol != AF_UNSPEC && protocol != i->protocol)
534 void flx_interface_monitor_walk(flxInterfaceMonitor *m, gint interface, guchar protocol, flxInterfaceMonitorWalkCallback callback, gpointer userdata) {
539 if (protocol != AF_UNSPEC) {
542 if ((i = flx_interface_monitor_get_interface(m, interface, protocol)))
543 callback(m, i, userdata);
549 if ((hw = flx_interface_monitor_get_hw_interface(m, interface)))
550 for (i = hw->interfaces; i; i = i->by_hardware_next)
551 if (flx_interface_match(i, interface, protocol))
552 callback(m, i, userdata);
558 for (i = m->interfaces; i; i = i->interface_next)
559 if (flx_interface_match(i, interface, protocol))
560 callback(m, i, userdata);