2 This file is part of catta.
4 catta is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
9 catta is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
12 Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with catta; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
24 #include <catta/malloc.h>
30 #include <sys/types.h>
31 #include <sys/socket.h>
32 #include <sys/param.h>
33 #ifdef HAVE_SYS_SYSCTL_H
34 #include <sys/sysctl.h>
36 #include <sys/sockio.h>
39 #include <net/route.h>
41 #include <net/if_dl.h>
42 #include <netinet/in.h>
44 #include <catta/log.h>
46 #include "iface-pfroute.h"
49 static int bitcount (unsigned int n)
60 static void rtm_info(struct rt_msghdr *rtm, CattaInterfaceMonitor *m)
63 struct if_msghdr *ifm = (struct if_msghdr *)rtm;
64 struct sockaddr_dl *sdl = (struct sockaddr_dl *)(ifm + 1);
66 if (sdl->sdl_family != AF_LINK)
69 if (ifm->ifm_addrs == 0 && ifm->ifm_index > 0) {
70 if (!(hw = catta_interface_monitor_get_hw_interface(m, (CattaIfIndex) ifm->ifm_index)))
72 catta_hw_interface_free(hw, 0);
76 if (!(hw = catta_interface_monitor_get_hw_interface(m, ifm->ifm_index)))
77 if (!(hw = catta_hw_interface_new(m, (CattaIfIndex) ifm->ifm_index)))
81 (ifm->ifm_flags & IFF_UP) &&
82 (!m->server->config.use_iff_running || (ifm->ifm_flags & IFF_RUNNING)) &&
83 !(ifm->ifm_flags & IFF_LOOPBACK) &&
84 (ifm->ifm_flags & IFF_MULTICAST) &&
85 (m->server->config.allow_point_to_point || !(ifm->ifm_flags & IFF_POINTOPOINT));
88 hw->name = catta_strndup(sdl->sdl_data, sdl->sdl_nlen);
90 hw->mtu = ifm->ifm_data.ifi_mtu;
92 hw->mac_address_size = sdl->sdl_alen;
93 if (hw->mac_address_size > CATTA_MAC_ADDRESS_MAX)
94 hw->mac_address_size = CATTA_MAC_ADDRESS_MAX;
96 memcpy(hw->mac_address, sdl->sdl_data + sdl->sdl_nlen, hw->mac_address_size);
100 /* catta_log_debug("======\n name: %s\n index:%d\n mtu:%d\n mac:%s\n flags_ok:%d\n======", */
101 /* hw->name, hw->index, */
103 /* catta_format_mac_address(mac, sizeof(mac), hw->mac_address, hw->mac_address_size), */
107 catta_hw_interface_check_relevant(hw);
108 catta_hw_interface_update_rrs(hw, 0);
112 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
113 #ifdef HAVE_SYS_SYSCTL_H
114 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
116 #define ADVANCE(x, n) (x += ROUNDUP(sizeof(struct sockaddr)))
119 static void rtm_addr(struct rt_msghdr *rtm, CattaInterfaceMonitor *m)
121 CattaInterface *iface;
124 struct ifa_msghdr *ifam = (struct ifa_msghdr *) rtm;
125 char *cp = (char *)(ifam + 1);
129 struct sockaddr *sa =NULL;
131 #if defined(__NetBSD__) || defined(__OpenBSD__)
132 if(((struct sockaddr *)cp)->sa_family == AF_UNSPEC)
133 ((struct sockaddr *)cp)->sa_family = AF_INET;
136 for (cp0 = cp, i = 0; i < RTAX_MAX; i++) {
137 if (!(ifam->ifam_addrs & (1<<i)))
139 sa = (struct sockaddr *)cp;
149 if(sa->sa_family != AF_INET && sa->sa_family != AF_INET6)
152 if (!(iface = catta_interface_monitor_get_interface(m, (CattaIfIndex) ifam->ifam_index, catta_af_to_proto(sa->sa_family))))
155 raddr.proto = catta_af_to_proto(sa->sa_family);
157 for(cp = cp0, i = 0; i < RTAX_MAX; i++)
159 if (!(ifam->ifam_addrs & (1<<i)))
161 sa = (struct sockaddr *)cp;
162 #ifdef HAVE_SYS_SYSCTL_H
166 switch(sa->sa_family) {
170 prefixlen = bitcount((unsigned int)((struct sockaddr_in *)sa)->sin_addr.s_addr);
173 memcpy(raddr.data.data, &((struct sockaddr_in *)sa)->sin_addr, sizeof(struct in_addr));
182 prefixlen = bitcount((unsigned int)((struct sockaddr_in6 *)sa)->sin6_addr.s6_addr);
185 memcpy(raddr.data.data, &((struct sockaddr_in6 *)sa)->sin6_addr, sizeof(struct in6_addr));
187 if (IN6_IS_ADDR_LINKLOCAL((struct in6_addr *)raddr.data.data))
189 ((struct in6_addr *)raddr.data.data)->s6_addr[2] = 0;
190 ((struct in6_addr *)raddr.data.data)->s6_addr[3] = 0;
211 if(rtm->rtm_type == RTM_NEWADDR)
213 CattaInterfaceAddress *addriface;
214 if (!(addriface = catta_interface_monitor_get_address(m, iface, &raddr)))
215 if (!(addriface = catta_interface_address_new(m, iface, &raddr, prefixlen)))
217 if (raddr.proto == CATTA_PROTO_INET6)
219 addriface->global_scope = !(IN6_IS_ADDR_LINKLOCAL((struct in6_addr *)raddr.data.data) || IN6_IS_ADDR_MULTICAST((struct in6_addr *)raddr.data.data));
222 addriface->global_scope = 1;
226 CattaInterfaceAddress *addriface;
227 assert(rtm->rtm_type == RTM_DELADDR);
228 if (!(addriface = catta_interface_monitor_get_address(m, iface, &raddr)))
230 catta_interface_address_free(addriface);
233 catta_interface_check_relevant(iface);
234 catta_interface_update_rrs(iface, 0);
237 static void parse_rtmsg(struct rt_msghdr *rtm, CattaInterfaceMonitor *m)
242 if (rtm->rtm_version != RTM_VERSION) {
243 catta_log_warn("routing message version %d not understood",
248 switch (rtm->rtm_type) {
261 static void socket_event(CattaWatch *w, int fd, CATTA_GCC_UNUSED CattaWatchEvent event,void *userdata) {
262 CattaInterfaceMonitor *m = (CattaInterfaceMonitor *)userdata;
263 CattaPfRoute *nl = m->osdep.pfroute;
270 assert(fd == nl->fd);
273 if((bytes = recv(nl->fd, msg, 2048, MSG_DONTWAIT)) < 0) {
274 if (errno == EAGAIN || errno == EINTR)
276 catta_log_error(__FILE__": recv() failed: %s", strerror(errno));
279 parse_rtmsg((struct rt_msghdr *)msg, m);
284 int catta_interface_monitor_init_osdep(CattaInterfaceMonitor *m) {
289 m->osdep.pfroute = NULL;
291 if ((fd = socket(PF_ROUTE, SOCK_RAW, AF_UNSPEC)) < 0) {
292 catta_log_error(__FILE__": socket(PF_ROUTE): %s", strerror(errno));
296 if (!(m->osdep.pfroute = catta_new(CattaPfRoute , 1))) {
297 catta_log_error(__FILE__": catta_new() failed.");
300 m->osdep.pfroute->fd = fd;
302 if (!(m->osdep.pfroute->watch = m->server->poll_api->watch_new(m->server->poll_api,
303 m->osdep.pfroute->fd,
307 catta_log_error(__FILE__": Failed to create watch.");
315 if (m->osdep.pfroute) {
316 if (m->osdep.pfroute->watch)
317 m->server->poll_api->watch_free(m->osdep.pfroute->watch);
322 m->osdep.pfroute = NULL;
328 void catta_interface_monitor_free_osdep(CattaInterfaceMonitor *m) {
331 if (m->osdep.pfroute) {
332 if (m->osdep.pfroute->watch)
333 m->server->poll_api->watch_free(m->osdep.pfroute->watch);
335 if (m->osdep.pfroute->fd >= 0)
336 close(m->osdep.pfroute->fd);
338 catta_free(m->osdep.pfroute);
339 m->osdep.pfroute = NULL;
343 #if defined (SIOCGLIFNUM) && defined(HAVE_STRUCT_LIFCONF) /* Solaris 8 and later; Sol 7? */
345 * I got this function from GNU zsbra
347 static int ip6_masklen (struct in6_addr netmask) {
352 pnt = (unsigned char *) & netmask;
354 while ((*pnt == 0xff) && len < 128) {
369 static void if_add_interface(struct lifreq *lifreq, CattaInterfaceMonitor *m, int fd, int count)
371 CattaHwInterface *hw;
373 struct lifreq lifrcopy;
378 CattaInterfaceAddress *addriface;
379 CattaInterface *iface;
380 struct sockaddr_in mask;
381 struct sockaddr_in6 mask6;
382 char caddr[CATTA_ADDRESS_STR_MAX];
386 if (ioctl(fd, SIOCGLIFFLAGS, &lifrcopy) < 0) {
387 catta_log_error(__FILE__": ioctl(SIOCGLIFFLAGS) %s", strerror(errno));
390 flags = lifrcopy.lifr_flags;
392 if (ioctl(fd, SIOCGLIFMTU, &lifrcopy) < 0) {
393 catta_log_error(__FILE__": ioctl(SIOCGLIFMTU) %s", strerror(errno));
396 mtu = lifrcopy.lifr_metric;
398 if (ioctl(fd, SIOCGLIFADDR, &lifrcopy) < 0) {
399 catta_log_error(__FILE__": ioctl(SIOCGLIFADDR) %s", strerror(errno));
402 addr.proto = catta_af_to_proto(lifreq->lifr_addr.ss_family);
403 if (ioctl(fd, SIOCGLIFNETMASK, &lifrcopy) < 0) {
404 catta_log_error(__FILE__": ioctl(SIOCGLIFNETMASK) %s", strerror(errno));
407 switch (lifreq->lifr_addr.ss_family) {
409 memcpy(addr.data.data, &((struct sockaddr_in *)&lifreq->lifr_addr)->sin_addr, sizeof(struct in_addr));
410 memcpy(&mask, &((struct sockaddr_in *)&lifrcopy.lifr_addr)->sin_addr, sizeof(struct in_addr));
411 prefixlen = bitcount((unsigned int) mask.sin_addr.s_addr);
414 memcpy(addr.data.data, &((struct sockaddr_in6 *)&lifreq->lifr_addr)->sin6_addr, sizeof(struct in6_addr));
415 memcpy(&mask6, &((struct sockaddr_in6 *)&lifrcopy.lifr_addr)->sin6_addr, sizeof(struct in6_addr));
416 prefixlen = lifrcopy.lifr_addrlen;
421 index = if_nametoindex(lifreq->lifr_name);
423 if (!(hw = catta_interface_monitor_get_hw_interface(m, (CattaIfIndex) index))) {
424 if (!(hw = catta_hw_interface_new(m, (CattaIfIndex) index)))
429 (!m->server->config.use_iff_running || (flags & IFF_RUNNING)) &&
430 !(flags & IFF_LOOPBACK) &&
431 (flags & IFF_MULTICAST) &&
432 (m->server->config.allow_point_to_point || !(flags & IFF_POINTOPOINT));
433 hw->name = catta_strdup(lifreq->lifr_name);
435 /* TODO get mac address */
438 if (!(iface = catta_interface_monitor_get_interface(m, (CattaIfIndex)index, addr.proto)))
441 if (!(addriface = catta_interface_monitor_get_address(m, iface, &addr)))
442 if (!(addriface = catta_interface_address_new(m, iface, &addr, prefixlen)))
445 addriface->global_scope = 1;
447 catta_hw_interface_check_relevant(hw);
448 catta_hw_interface_update_rrs(hw, 0);
452 void catta_interface_monitor_sync(CattaInterfaceMonitor *m) {
453 #ifndef HAVE_STRUCT_LIFCONF
456 char *buf, *lim, *next, count = 0;
457 struct rt_msghdr *rtm;
464 mib[2] = 0; /* protocol */
465 mib[3] = 0; /* wildcard address family */
466 mib[4] = NET_RT_IFLIST;
467 mib[5] = 0; /* no flags */
468 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
470 catta_log_error("sysctl failed: %s", strerror(errno));
471 catta_log_error("route-sysctl-estimate");
474 if ((buf = catta_malloc(needed)) == NULL)
476 catta_log_error("malloc failed in catta_interface_monitor_sync");
479 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
480 catta_log_warn("sysctl failed: %s", strerror(errno));
481 if (errno == ENOMEM && count++ < 10) {
482 catta_log_warn("Routing table grew, retrying");
489 for (next = buf; next < lim; next += rtm->rtm_msglen) {
490 rtm = (struct rt_msghdr *)next;
494 m->list_complete = 1;
495 catta_interface_monitor_check_relevant(m);
496 catta_interface_monitor_update_rrs(m, 0);
497 catta_log_info("Network interface enumeration completed.");
498 #elif defined (SIOCGLIFNUM) && defined(HAVE_STRUCT_LIFCONF) /* Solaris 8 and later; Sol 7? */
504 struct lifreq *lifreq;
506 if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
507 catta_log_error(__FILE__": socket(PFROUTE): %s", strerror(errno));
510 lifc.lifc_buf = NULL;
511 lifn.lifn_family = AF_UNSPEC;
513 if (ioctl(sockfd, SIOCGLIFNUM, &lifn) < 0) {
514 catta_log_error(__FILE__": ioctl(SIOCGLIFNUM): %s", strerror(errno));
517 lifc.lifc_len = lifn.lifn_count * sizeof (struct lifreq);
518 if ((lifc.lifc_buf = catta_malloc(lifc.lifc_len)) == NULL) {
519 catta_log_error("malloc failed in catta_interface_monitor_sync");
522 lifc.lifc_family = NULL;
524 if(ioctl(sockfd, SIOCGLIFCONF, &lifc) < 0) {
525 catta_log_error(__FILE__": ioctl(SIOCGLIFCONF): %s", strerror(errno));
528 lifreq = lifc.lifc_req;
530 for (n = 0; n < lifc.lifc_len; n += sizeof(struct lifreq)) {
531 if_add_interface(lifreq, m, sockfd, lifn.lifn_count);
534 m->list_complete = 1;
535 catta_interface_monitor_check_relevant(m);
536 catta_interface_monitor_update_rrs(m, 0);
539 catta_free(lifc.lifc_buf);
541 catta_log_info("Network interface enumeration completed.");