2 * interface.c -- implements fetchmail 'interface' and 'monitor' commands
4 * This module was implemented by George M. Sipe <gsipe@pobox.com>
5 * or <gsipe@acm.org> and is:
7 * Copyright (c) 1996,1997 by George M. Sipe
9 * FreeBSD specific portions written by and Copyright (c) 1999
10 * Andy Doran <ad@psn.ie>.
12 * For license terms, see the file COPYING in this directory.
14 #include <sys/types.h>
15 #include <sys/param.h>
17 #if (defined(linux) && !defined(INET6_ENABLE)) || defined(__FreeBSD__)
22 #if defined(STDC_HEADERS)
25 #if defined(HAVE_UNISTD_H)
28 #include <sys/ioctl.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
33 #if defined(__FreeBSD__)
34 #if defined __FreeBSD_USE_KVM
35 #if __FreeBSD_version >= 300001
36 #include <net/if_var.h>
40 #include <sys/fcntl.h>
42 #include <sys/sysctl.h>
43 #include <net/route.h>
44 #include <net/if_dl.h>
48 #include "fetchmail.h"
54 struct in_addr addr, dstaddr, netmask;
55 int rx_packets, tx_packets;
58 struct interface_pair_s {
59 struct in_addr interface_address;
60 struct in_addr interface_mask;
63 static char *netdevfmt;
66 * Count of packets to see on an interface before monitor considers it up.
67 * Needed because when pppd shuts down the link, the packet counts go up
68 * by two (one rx and one tx?, maybe). A value of 2 seems to do the trick,
69 * but we'll give it some extra.
71 #define MONITOR_SLOP 5
75 void interface_init(void)
76 /* figure out which /proc/dev/net format to use */
78 FILE *fp = popen("uname -r", "r"); /* still wins if /proc is out */
80 /* pre-linux-2.2 format -- transmit packet count in 8th field */
81 netdevfmt = "%d %d %*d %*d %*d %d %*d %d %*d %*d %*d %*d %d";
89 if (fscanf(fp, "%d.%d.%*d", &major, &minor) >= 2
90 && major >= 2 && minor >= 2)
91 /* Linux 2.2 -- transmit packet count in 10th field */
92 netdevfmt = "%d %d %*d %*d %*d %d %*d %*d %*d %d %*d %*d %d";
97 static int _get_ifinfoGT_(int socket_fd, FILE *stats_file, const char *ifname,
99 /* get active network interface information - return non-zero upon success */
101 int namelen = strlen(ifname);
102 struct ifreq request;
103 char *cp, buffer[256];
107 /* initialize result */
108 memset((char *) ifinfo, 0, sizeof(ifinfo_t));
110 /* get the packet I/O counts */
111 while (fgets(buffer, sizeof(buffer) - 1, stats_file)) {
112 for (cp = buffer; *cp && *cp == ' '; ++cp);
113 if (!strncmp(cp, ifname, namelen) &&
114 cp[namelen] == ':') {
116 if (sscanf(cp, netdevfmt,
117 counts, counts+1, counts+2,
118 counts+3,&found)>4) { /* found = dummy */
119 /* newer kernel with byte counts */
120 ifinfo->rx_packets=counts[1];
121 ifinfo->tx_packets=counts[3];
123 /* older kernel, no byte counts */
124 ifinfo->rx_packets=counts[0];
125 ifinfo->tx_packets=counts[2];
130 if (!found) return (FALSE);
132 /* see if the interface is up */
133 strcpy(request.ifr_name, ifname);
134 if (ioctl(socket_fd, SIOCGIFFLAGS, &request) < 0)
136 if (!(request.ifr_flags & IFF_RUNNING))
139 /* get the (local) IP address */
140 strcpy(request.ifr_name, ifname);
141 if (ioctl(socket_fd, SIOCGIFADDR, &request) < 0)
143 ifinfo->addr = ((struct sockaddr_in *) (&request.ifr_addr))->sin_addr;
145 /* get the PPP destination (remote) IP address */
146 ifinfo->dstaddr.s_addr = 0;
147 strcpy(request.ifr_name, ifname);
148 if (ioctl(socket_fd, SIOCGIFDSTADDR, &request) >= 0)
149 ifinfo->dstaddr = ((struct sockaddr_in *)
150 (&request.ifr_dstaddr))->sin_addr;
152 /* get the netmask */
153 strcpy(request.ifr_name, ifname);
154 if (ioctl(socket_fd, SIOCGIFNETMASK, &request) >= 0) {
155 ifinfo->netmask = ((struct sockaddr_in *)
156 (&request.ifr_netmask))->sin_addr;
163 static int get_ifinfo(const char *ifname, ifinfo_t *ifinfo)
165 int socket_fd = socket(AF_INET, SOCK_DGRAM, 0);
166 FILE *stats_file = fopen("/proc/net/dev", "r");
169 if (socket_fd < 0 || !stats_file)
173 char *sp = strchr(ifname, '/');
177 result = _get_ifinfoGT_(socket_fd, stats_file, ifname, ifinfo);
182 SockClose(socket_fd);
184 fclose(stats_file); /* not checking should be safe, mode was "r" */
188 #elif defined __FreeBSD__
190 #if defined __FreeBSD_USE_KVM
193 static struct nlist symbols[] =
198 static u_long ifnet_savedaddr;
199 static gid_t if_rgid;
200 static gid_t if_egid;
203 interface_set_gids(gid_t egid, gid_t rgid)
212 if ((kvmfd = kvm_open(NULL, NULL, NULL, O_RDONLY, NULL)) == NULL)
215 if (kvm_nlist(kvmfd, symbols) < 0)
218 if (kvm_read(kvmfd, (unsigned long) symbols[0].n_value, &ifnet_savedaddr, sizeof(unsigned long)) == -1)
225 get_ifinfo(const char *ifname, ifinfo_t *ifinfo)
230 unsigned long ifnet_addr = ifnet_savedaddr;
231 #if __FreeBSD_version >= 300001
232 struct ifnethead ifnethead;
233 struct ifaddrhead ifaddrhead;
235 struct ifaddr ifaddr;
236 unsigned long ifaddr_addr;
238 unsigned long sa_addr;
244 for (i = 0; ifname[i] && ifname[i] != '/'; i++)
245 iname[i] = ifname[i];
253 report(stderr, 0, GT_("Unable to open kvm interface. Make sure fetchmail is SGID kmem."));
260 #if __FreeBSD_version >= 300001
261 kvm_read(kvmfd, ifnet_savedaddr, (char *) &ifnethead, sizeof ifnethead);
262 ifnet_addr = (u_long) ifnethead.tqh_first;
264 ifnet_addr = ifnet_savedaddr;
269 kvm_read(kvmfd, ifnet_addr, &ifnet, sizeof(ifnet));
270 kvm_read(kvmfd, (unsigned long) ifnet.if_name, tname, sizeof tname);
272 snprintf(tname, sizeof tname,
276 "%s%d", tname, ifnet.if_unit);
278 if (!strcmp(tname, iname))
280 if (!(ifnet.if_flags & IFF_UP))
287 ifinfo->rx_packets = ifnet.if_ipackets;
288 ifinfo->tx_packets = ifnet.if_opackets;
290 #if __FreeBSD_version >= 300001
291 ifaddr_addr = (u_long) ifnet.if_addrhead.tqh_first;
293 ifaddr_addr = (u_long) ifnet.if_addrlist;
298 kvm_read(kvmfd, ifaddr_addr, &ifaddr, sizeof(ifaddr));
299 kvm_read(kvmfd, (u_long)ifaddr.ifa_addr, &sa, sizeof(sa));
301 if (sa.sa_family != AF_INET)
303 #if __FreeBSD_version >= 300001
304 ifaddr_addr = (u_long) ifaddr.ifa_link.tqe_next;
306 ifaddr_addr = (u_long) ifaddr.ifa_next;
311 ifinfo->addr.s_addr = *(u_long *)(sa.sa_data + 2);
312 kvm_read(kvmfd, (u_long)ifaddr.ifa_dstaddr, &sa, sizeof(sa));
313 ifinfo->dstaddr.s_addr = *(u_long *)(sa.sa_data + 2);
314 kvm_read(kvmfd, (u_long)ifaddr.ifa_netmask, &sa, sizeof(sa));
315 ifinfo->netmask.s_addr = *(u_long *)(sa.sa_data + 2);
329 #if __FreeBSD_version >= 300001
330 ifnet_addr = (u_long) ifnet.if_link.tqe_next;
332 ifnet_addr = (unsigned long) ifnet.if_next;
342 #else /* Do not use KVM on FreeBSD */
345 * Expand the compacted form of addresses as returned via the
346 * configuration read via sysctl().
350 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
356 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
357 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
359 memset(rtinfo->rti_info, 0, sizeof(rtinfo->rti_info));
360 for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
361 if ((rtinfo->rti_addrs & (1 << i)) == 0)
363 rtinfo->rti_info[i] = sa = (struct sockaddr *)cp;
372 get_ifinfo(const char *ifname, ifinfo_t *ifinfo)
381 struct if_msghdr *ifm;
382 struct ifa_msghdr *ifam;
383 struct sockaddr_in *sin;
384 struct sockaddr_dl *sdl;
385 struct rt_addrinfo info;
389 memset(ifinfo, 0, sizeof(ifinfo));
391 /* trim interface name */
393 for (i = 0; i < sizeof(iname) && ifname[i] && ifname[i] != '/'; i++)
394 iname[i] = ifname[i];
396 if (i == 0 || i == sizeof(iname))
398 report(stderr, GT_("Unable to parse interface name from %s"), ifname);
405 /* get list of existing interfaces */
410 mib[3] = AF_INET; /* Only IP addresses please. */
411 mib[4] = NET_RT_IFLIST;
412 mib[5] = 0; /* List all interfaces. */
415 /* Get interface data. */
417 if (sysctl(mib, 6, NULL, &needed, NULL, 0) == -1)
420 GT_("get_ifinfo: sysctl (iflist estimate) failed"));
423 if ((buf = malloc(needed)) == NULL)
426 GT_("get_ifinfo: malloc failed"));
429 if (sysctl(mib, 6, buf, &needed, NULL, 0) == -1)
432 GT_("get_ifinfo: sysctl (iflist) failed"));
439 /* first look for the interface information */
444 ifm = (struct if_msghdr *)next;
445 next += ifm->ifm_msglen;
447 if (ifm->ifm_version != RTM_VERSION)
450 GT_("Routing message version %d not understood."),
455 if (ifm->ifm_type == RTM_IFINFO)
457 sdl = (struct sockaddr_dl *)(ifm + 1);
459 if (!(strlen(iname) == sdl->sdl_nlen
460 && strncmp(iname, sdl->sdl_data, sdl->sdl_nlen) == 0))
465 if ( !(ifm->ifm_flags & IFF_UP) )
467 /* the interface is down */
471 ifindex = ifm->ifm_index;
472 ifinfo->rx_packets = ifm->ifm_data.ifi_ipackets;
473 ifinfo->tx_packets = ifm->ifm_data.ifi_opackets;
481 /* we did not find an interface with a matching name */
482 report(stderr, GT_("No interface found with name %s"), iname);
486 /* now look for the interface's IP address */
491 ifam = (struct ifa_msghdr *)next;
492 next += ifam->ifam_msglen;
495 && ifam->ifam_type == RTM_NEWADDR
496 && ifam->ifam_index == ifindex)
498 /* Expand the compacted addresses */
499 info.rti_addrs = ifam->ifam_addrs;
500 rt_xaddrs((char *)(ifam + 1),
501 ifam->ifam_msglen + (char *)ifam,
504 /* Check for IPv4 address information only */
505 if (info.rti_info[RTAX_IFA]->sa_family != AF_INET)
512 sin = (struct sockaddr_in *)info.rti_info[RTAX_IFA];
515 ifinfo->addr = sin->sin_addr;
518 sin = (struct sockaddr_in *)info.rti_info[RTAX_NETMASK];
521 ifinfo->netmask = sin->sin_addr;
524 /* note: RTAX_BRD contains the address at the other
525 * end of a point-to-point link or the broadcast address
526 * of non point-to-point link
528 sin = (struct sockaddr_in *)info.rti_info[RTAX_BRD];
531 ifinfo->dstaddr = sin->sin_addr;
540 report(stderr, GT_("No IP address found for %s"), iname);
548 #endif /* __FREEBSD_USE_SYSCTL_GET_IFFINFO */
550 #endif /* defined __FreeBSD__ */
553 #ifndef HAVE_INET_ATON
555 * Note: This is not a true replacement for inet_aton(), as it won't
556 * do the right thing on "255.255.255.255" (which translates to -1 on
557 * most machines). Fortunately this code will be used only if you're
558 * on an older Linux that lacks a real implementation.
560 #ifdef HAVE_NETINET_IN_SYSTM_H
561 # include <sys/types.h>
562 # include <netinet/in_systm.h>
565 #include <netinet/in.h>
566 #include <netinet/ip.h>
567 #include <arpa/inet.h>
570 static int inet_aton(const char *cp, struct in_addr *inp) {
573 addr = inet_addr(cp);
574 if (addr == ((long) -1)) return 0;
576 memcpy(inp, &addr, sizeof(addr));
579 #endif /* HAVE_INET_ATON */
581 void interface_parse(char *buf, struct hostdata *hp)
582 /* parse 'interface' specification */
586 hp->interface = xstrdup(buf);
588 /* find and isolate just the IP address */
589 if (!(cp1 = strchr(buf, '/')))
591 (void) report(stderr,
592 GT_("missing IP interface address\n"));
597 /* find and isolate just the netmask */
598 if (!(cp2 = strchr(cp1, '/')))
599 cp2 = "255.255.255.255";
603 /* convert IP address and netmask */
604 hp->interface_pair = (struct interface_pair_s *)xmalloc(sizeof(struct interface_pair_s));
605 if (!inet_aton(cp1, &hp->interface_pair->interface_address))
607 (void) report(stderr,
608 GT_("invalid IP interface address\n"));
611 if (!inet_aton(cp2, &hp->interface_pair->interface_mask))
613 (void) report(stderr,
614 GT_("invalid IP interface mask\n"));
617 /* apply the mask now to the IP address (range) required */
618 hp->interface_pair->interface_address.s_addr &=
619 hp->interface_pair->interface_mask.s_addr;
621 /* restore original interface string (for configuration dumper) */
626 void interface_note_activity(struct hostdata *hp)
627 /* save interface I/O counts */
632 /* if not monitoring link, all done */
636 /* get the current I/O stats for the monitored link */
637 if (get_ifinfo(hp->monitor, &ifinfo))
638 /* update this and preceeding host entries using the link
639 (they were already set during this pass but the I/O
640 count has now changed and they need to be re-updated)
642 for (ctl = querylist; ctl; ctl = ctl->next) {
643 if (ctl->server.monitor && !strcmp(hp->monitor, ctl->server.monitor))
644 ctl->server.monitor_io =
645 ifinfo.rx_packets + ifinfo.tx_packets;
646 /* do NOT update host entries following this one */
647 if (&ctl->server == hp)
651 #ifdef ACTIVITY_DEBUG
652 (void) report(stdout,
653 GT_("activity on %s -noted- as %d\n"),
654 hp->monitor, hp->monitor_io);
658 int interface_approve(struct hostdata *hp, flag domonitor)
659 /* return TRUE if OK to poll, FALSE otherwise */
663 /* check interface IP address (range), if specified */
665 /* get interface info */
666 if (!get_ifinfo(hp->interface, &ifinfo)) {
667 (void) report(stdout,
668 GT_("skipping poll of %s, %s down\n"),
669 hp->pollname, hp->interface);
672 /* check the IP addresses (range) */
674 /* check remote IP address */
675 ((ifinfo.dstaddr.s_addr != 0) &&
676 (ifinfo.dstaddr.s_addr &
677 hp->interface_pair->interface_mask.s_addr) ==
678 hp->interface_pair->interface_address.s_addr)
680 /* check local IP address */
681 ((ifinfo.addr.s_addr &
682 hp->interface_pair->interface_mask.s_addr) ==
683 hp->interface_pair->interface_address.s_addr)
686 (void) report(stdout,
687 GT_("skipping poll of %s, %s IP address excluded\n"),
688 hp->pollname, hp->interface);
693 /* if not monitoring link, all done */
694 if (!domonitor || !hp->monitor)
697 #ifdef ACTIVITY_DEBUG
698 (void) report(stdout,
699 GT_("activity on %s checked as %d\n"),
700 hp->monitor, hp->monitor_io);
702 /* if monitoring, check link for activity if it is up */
703 if (get_ifinfo(hp->monitor, &ifinfo))
705 int diff = (ifinfo.rx_packets + ifinfo.tx_packets)
709 * There are three cases here:
711 * (a) If the new packet count is less than the recorded one,
712 * probably pppd was restarted while fetchmail was running.
715 * (b) newpacket count is greater than the old packet count,
716 * but the difference is small and may just reflect the overhead
717 * of a link shutdown. Skip.
719 * (c) newpacket count is greater than the old packet count,
720 * and the difference is large. Connection is live. Don't skip.
722 if (diff >= 0 && diff <= MONITOR_SLOP)
724 (void) report(stdout,
725 GT_("skipping poll of %s, %s inactive\n"),
726 hp->pollname, hp->monitor);
731 #ifdef ACTIVITY_DEBUG
732 report(stdout, GT_("activity on %s was %d, is %d\n"),
733 hp->monitor, hp->monitor_io,
734 ifinfo.rx_packets + ifinfo.tx_packets);
739 #endif /* (defined(linux) && !defined(INET6_ENABLE)) || defined(__FreeBSD__) */