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>
18 #include <sys/utsname.h>
21 #if (defined(linux) && !defined(INET6_ENABLE)) || defined(__FreeBSD__)
26 #if defined(STDC_HEADERS)
29 #if defined(HAVE_UNISTD_H)
32 #include <sys/ioctl.h>
33 #include <sys/socket.h>
34 #include <netinet/in.h>
35 #include <arpa/inet.h>
37 #if defined(__FreeBSD__)
38 #if defined __FreeBSD_USE_KVM
39 #if __FreeBSD_version >= 300001
40 #include <net/if_var.h>
44 #include <sys/fcntl.h>
46 #include <sys/sysctl.h>
47 #include <net/route.h>
48 #include <net/if_dl.h>
52 #include "fetchmail.h"
58 struct in_addr addr, dstaddr, netmask;
59 int rx_packets, tx_packets;
62 struct interface_pair_s {
63 struct in_addr interface_address;
64 struct in_addr interface_mask;
67 static char *netdevfmt;
70 * Count of packets to see on an interface before monitor considers it up.
71 * Needed because when pppd shuts down the link, the packet counts go up
72 * by two (one rx and one tx?, maybe). A value of 2 seems to do the trick,
73 * but we'll give it some extra.
75 #define MONITOR_SLOP 5
79 void interface_init(void)
80 /* figure out which /proc/net/dev format to use */
82 struct utsname utsname;
84 /* Linux 2.2 -- transmit packet count in 10th field */
85 netdevfmt = "%d %d %*d %*d %*d %d %*d %*d %*d %d %*d %*d %d";
87 if (uname(&utsname) < 0)
93 if (sscanf(utsname.release, "%d.%d.%*d", &major, &minor) >= 2
94 && !(major >= 2 && minor >= 2))
95 /* pre-linux-2.2 format -- transmit packet count in 8th field */
96 netdevfmt = "%d %d %*d %*d %*d %d %*d %d %*d %*d %*d %*d %d";
100 static int _get_ifinfoGT_(int socket_fd, FILE *stats_file, const char *ifname,
102 /* get active network interface information - return non-zero upon success */
104 int namelen = strlen(ifname);
105 struct ifreq request;
106 char *cp, buffer[256];
110 /* initialize result */
111 memset((char *) ifinfo, 0, sizeof(ifinfo_t));
113 /* get the packet I/O counts */
114 while (fgets(buffer, sizeof(buffer) - 1, stats_file)) {
115 for (cp = buffer; *cp && *cp == ' '; ++cp);
116 if (!strncmp(cp, ifname, namelen) &&
117 cp[namelen] == ':') {
119 if (sscanf(cp, netdevfmt,
120 counts, counts+1, counts+2,
121 counts+3,&found)>4) { /* found = dummy */
122 /* newer kernel with byte counts */
123 ifinfo->rx_packets=counts[1];
124 ifinfo->tx_packets=counts[3];
126 /* older kernel, no byte counts */
127 ifinfo->rx_packets=counts[0];
128 ifinfo->tx_packets=counts[2];
133 if (!found) return (FALSE);
135 /* see if the interface is up */
136 strcpy(request.ifr_name, ifname);
137 if (ioctl(socket_fd, SIOCGIFFLAGS, &request) < 0)
139 if (!(request.ifr_flags & IFF_RUNNING))
142 /* get the (local) IP address */
143 strcpy(request.ifr_name, ifname);
144 if (ioctl(socket_fd, SIOCGIFADDR, &request) < 0)
146 ifinfo->addr = ((struct sockaddr_in *) (&request.ifr_addr))->sin_addr;
148 /* get the PPP destination (remote) IP address */
149 ifinfo->dstaddr.s_addr = 0;
150 strcpy(request.ifr_name, ifname);
151 if (ioctl(socket_fd, SIOCGIFDSTADDR, &request) >= 0)
152 ifinfo->dstaddr = ((struct sockaddr_in *)
153 (&request.ifr_dstaddr))->sin_addr;
155 /* get the netmask */
156 strcpy(request.ifr_name, ifname);
157 if (ioctl(socket_fd, SIOCGIFNETMASK, &request) >= 0) {
158 ifinfo->netmask = ((struct sockaddr_in *)
159 (&request.ifr_netmask))->sin_addr;
166 static int get_ifinfo(const char *ifname, ifinfo_t *ifinfo)
168 int socket_fd = socket(AF_INET, SOCK_DGRAM, 0);
169 FILE *stats_file = fopen("/proc/net/dev", "r");
172 if (socket_fd < 0 || !stats_file)
176 char *sp = strchr(ifname, '/');
180 result = _get_ifinfoGT_(socket_fd, stats_file, ifname, ifinfo);
185 SockClose(socket_fd);
187 fclose(stats_file); /* not checking should be safe, mode was "r" */
191 #elif defined __FreeBSD__
193 #if defined __FreeBSD_USE_KVM
196 static struct nlist symbols[] =
201 static u_long ifnet_savedaddr;
202 static gid_t if_rgid;
203 static gid_t if_egid;
206 interface_set_gids(gid_t egid, gid_t rgid)
215 if ((kvmfd = kvm_open(NULL, NULL, NULL, O_RDONLY, NULL)) == NULL)
218 if (kvm_nlist(kvmfd, symbols) < 0)
221 if (kvm_read(kvmfd, (unsigned long) symbols[0].n_value, &ifnet_savedaddr, sizeof(unsigned long)) == -1)
228 get_ifinfo(const char *ifname, ifinfo_t *ifinfo)
233 unsigned long ifnet_addr = ifnet_savedaddr;
234 #if __FreeBSD_version >= 300001
235 struct ifnethead ifnethead;
236 struct ifaddrhead ifaddrhead;
238 struct ifaddr ifaddr;
239 unsigned long ifaddr_addr;
241 unsigned long sa_addr;
247 for (i = 0; ifname[i] && ifname[i] != '/'; i++)
248 iname[i] = ifname[i];
256 report(stderr, 0, GT_("Unable to open kvm interface. Make sure fetchmail is SGID kmem."));
263 #if __FreeBSD_version >= 300001
264 kvm_read(kvmfd, ifnet_savedaddr, (char *) &ifnethead, sizeof ifnethead);
265 ifnet_addr = (u_long) ifnethead.tqh_first;
267 ifnet_addr = ifnet_savedaddr;
272 kvm_read(kvmfd, ifnet_addr, &ifnet, sizeof(ifnet));
273 kvm_read(kvmfd, (unsigned long) ifnet.if_name, tname, sizeof tname);
274 snprintf(tname, sizeof tname, "%s%d", tname, ifnet.if_unit);
276 if (!strcmp(tname, iname))
278 if (!(ifnet.if_flags & IFF_UP))
285 ifinfo->rx_packets = ifnet.if_ipackets;
286 ifinfo->tx_packets = ifnet.if_opackets;
288 #if __FreeBSD_version >= 300001
289 ifaddr_addr = (u_long) ifnet.if_addrhead.tqh_first;
291 ifaddr_addr = (u_long) ifnet.if_addrlist;
296 kvm_read(kvmfd, ifaddr_addr, &ifaddr, sizeof(ifaddr));
297 kvm_read(kvmfd, (u_long)ifaddr.ifa_addr, &sa, sizeof(sa));
299 if (sa.sa_family != AF_INET)
301 #if __FreeBSD_version >= 300001
302 ifaddr_addr = (u_long) ifaddr.ifa_link.tqe_next;
304 ifaddr_addr = (u_long) ifaddr.ifa_next;
309 ifinfo->addr.s_addr = *(u_long *)(sa.sa_data + 2);
310 kvm_read(kvmfd, (u_long)ifaddr.ifa_dstaddr, &sa, sizeof(sa));
311 ifinfo->dstaddr.s_addr = *(u_long *)(sa.sa_data + 2);
312 kvm_read(kvmfd, (u_long)ifaddr.ifa_netmask, &sa, sizeof(sa));
313 ifinfo->netmask.s_addr = *(u_long *)(sa.sa_data + 2);
327 #if __FreeBSD_version >= 300001
328 ifnet_addr = (u_long) ifnet.if_link.tqe_next;
330 ifnet_addr = (unsigned long) ifnet.if_next;
340 #else /* Do not use KVM on FreeBSD */
343 * Expand the compacted form of addresses as returned via the
344 * configuration read via sysctl().
348 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
354 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
355 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
357 memset(rtinfo->rti_info, 0, sizeof(rtinfo->rti_info));
358 for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
359 if ((rtinfo->rti_addrs & (1 << i)) == 0)
361 rtinfo->rti_info[i] = sa = (struct sockaddr *)cp;
370 get_ifinfo(const char *ifname, ifinfo_t *ifinfo)
379 struct if_msghdr *ifm;
380 struct ifa_msghdr *ifam;
381 struct sockaddr_in *sin;
382 struct sockaddr_dl *sdl;
383 struct rt_addrinfo info;
387 memset(ifinfo, 0, sizeof(ifinfo));
389 /* trim interface name */
391 for (i = 0; i < sizeof(iname) && ifname[i] && ifname[i] != '/'; i++)
392 iname[i] = ifname[i];
394 if (i == 0 || i == sizeof(iname))
396 report(stderr, GT_("Unable to parse interface name from %s"), ifname);
403 /* get list of existing interfaces */
408 mib[3] = AF_INET; /* Only IP addresses please. */
409 mib[4] = NET_RT_IFLIST;
410 mib[5] = 0; /* List all interfaces. */
413 /* Get interface data. */
415 if (sysctl(mib, 6, NULL, &needed, NULL, 0) == -1)
418 GT_("get_ifinfo: sysctl (iflist estimate) failed"));
421 if ((buf = malloc(needed)) == NULL)
424 GT_("get_ifinfo: malloc failed"));
427 if (sysctl(mib, 6, buf, &needed, NULL, 0) == -1)
430 GT_("get_ifinfo: sysctl (iflist) failed"));
437 /* first look for the interface information */
442 ifm = (struct if_msghdr *)next;
443 next += ifm->ifm_msglen;
445 if (ifm->ifm_version != RTM_VERSION)
448 GT_("Routing message version %d not understood."),
453 if (ifm->ifm_type == RTM_IFINFO)
455 sdl = (struct sockaddr_dl *)(ifm + 1);
457 if (!(strlen(iname) == sdl->sdl_nlen
458 && strncmp(iname, sdl->sdl_data, sdl->sdl_nlen) == 0))
463 if ( !(ifm->ifm_flags & IFF_UP) )
465 /* the interface is down */
469 ifindex = ifm->ifm_index;
470 ifinfo->rx_packets = ifm->ifm_data.ifi_ipackets;
471 ifinfo->tx_packets = ifm->ifm_data.ifi_opackets;
479 /* we did not find an interface with a matching name */
480 report(stderr, GT_("No interface found with name %s"), iname);
484 /* now look for the interface's IP address */
489 ifam = (struct ifa_msghdr *)next;
490 next += ifam->ifam_msglen;
493 && ifam->ifam_type == RTM_NEWADDR
494 && ifam->ifam_index == ifindex)
496 /* Expand the compacted addresses */
497 info.rti_addrs = ifam->ifam_addrs;
498 rt_xaddrs((char *)(ifam + 1),
499 ifam->ifam_msglen + (char *)ifam,
502 /* Check for IPv4 address information only */
503 if (info.rti_info[RTAX_IFA]->sa_family != AF_INET)
510 sin = (struct sockaddr_in *)info.rti_info[RTAX_IFA];
513 ifinfo->addr = sin->sin_addr;
516 sin = (struct sockaddr_in *)info.rti_info[RTAX_NETMASK];
519 ifinfo->netmask = sin->sin_addr;
522 /* note: RTAX_BRD contains the address at the other
523 * end of a point-to-point link or the broadcast address
524 * of non point-to-point link
526 sin = (struct sockaddr_in *)info.rti_info[RTAX_BRD];
529 ifinfo->dstaddr = sin->sin_addr;
538 report(stderr, GT_("No IP address found for %s"), iname);
546 #endif /* __FREEBSD_USE_SYSCTL_GET_IFFINFO */
548 #endif /* defined __FreeBSD__ */
551 #ifndef HAVE_INET_ATON
553 * Note: This is not a true replacement for inet_aton(), as it won't
554 * do the right thing on "255.255.255.255" (which translates to -1 on
555 * most machines). Fortunately this code will be used only if you're
556 * on an older Linux that lacks a real implementation.
558 #ifdef HAVE_NETINET_IN_SYSTM_H
559 # include <sys/types.h>
560 # include <netinet/in_systm.h>
563 #include <netinet/in.h>
564 #include <netinet/ip.h>
565 #include <arpa/inet.h>
568 static int inet_aton(const char *cp, struct in_addr *inp) {
571 addr = inet_addr(cp);
572 if (addr == ((long) -1)) return 0;
574 memcpy(inp, &addr, sizeof(addr));
577 #endif /* HAVE_INET_ATON */
579 void interface_parse(char *buf, struct hostdata *hp)
580 /* parse 'interface' specification */
584 hp->interface = xstrdup(buf);
586 /* find and isolate just the IP address */
587 if (!(cp1 = strchr(buf, '/')))
589 (void) report(stderr,
590 GT_("missing IP interface address\n"));
595 /* find and isolate just the netmask */
596 if (!(cp2 = strchr(cp1, '/')))
597 cp2 = "255.255.255.255";
601 /* convert IP address and netmask */
602 hp->interface_pair = (struct interface_pair_s *)xmalloc(sizeof(struct interface_pair_s));
603 if (!inet_aton(cp1, &hp->interface_pair->interface_address))
605 (void) report(stderr,
606 GT_("invalid IP interface address\n"));
609 if (!inet_aton(cp2, &hp->interface_pair->interface_mask))
611 (void) report(stderr,
612 GT_("invalid IP interface mask\n"));
615 /* apply the mask now to the IP address (range) required */
616 hp->interface_pair->interface_address.s_addr &=
617 hp->interface_pair->interface_mask.s_addr;
619 /* restore original interface string (for configuration dumper) */
624 void interface_note_activity(struct hostdata *hp)
625 /* save interface I/O counts */
630 /* if not monitoring link, all done */
634 /* get the current I/O stats for the monitored link */
635 if (get_ifinfo(hp->monitor, &ifinfo))
636 /* update this and preceeding host entries using the link
637 (they were already set during this pass but the I/O
638 count has now changed and they need to be re-updated)
640 for (ctl = querylist; ctl; ctl = ctl->next) {
641 if (ctl->server.monitor && !strcmp(hp->monitor, ctl->server.monitor))
642 ctl->server.monitor_io =
643 ifinfo.rx_packets + ifinfo.tx_packets;
644 /* do NOT update host entries following this one */
645 if (&ctl->server == hp)
649 #ifdef ACTIVITY_DEBUG
650 (void) report(stdout,
651 GT_("activity on %s -noted- as %d\n"),
652 hp->monitor, hp->monitor_io);
656 int interface_approve(struct hostdata *hp, flag domonitor)
657 /* return TRUE if OK to poll, FALSE otherwise */
661 /* check interface IP address (range), if specified */
663 /* get interface info */
664 if (!get_ifinfo(hp->interface, &ifinfo)) {
665 (void) report(stdout,
666 GT_("skipping poll of %s, %s down\n"),
667 hp->pollname, hp->interface);
670 /* check the IP addresses (range) */
672 /* check remote IP address */
673 ((ifinfo.dstaddr.s_addr != 0) &&
674 (ifinfo.dstaddr.s_addr &
675 hp->interface_pair->interface_mask.s_addr) ==
676 hp->interface_pair->interface_address.s_addr)
678 /* check local IP address */
679 ((ifinfo.addr.s_addr &
680 hp->interface_pair->interface_mask.s_addr) ==
681 hp->interface_pair->interface_address.s_addr)
684 (void) report(stdout,
685 GT_("skipping poll of %s, %s IP address excluded\n"),
686 hp->pollname, hp->interface);
691 /* if not monitoring link, all done */
692 if (!domonitor || !hp->monitor)
695 #ifdef ACTIVITY_DEBUG
696 (void) report(stdout,
697 GT_("activity on %s checked as %d\n"),
698 hp->monitor, hp->monitor_io);
700 /* if monitoring, check link for activity if it is up */
701 if (get_ifinfo(hp->monitor, &ifinfo))
703 int diff = (ifinfo.rx_packets + ifinfo.tx_packets)
707 * There are three cases here:
709 * (a) If the new packet count is less than the recorded one,
710 * probably pppd was restarted while fetchmail was running.
713 * (b) newpacket count is greater than the old packet count,
714 * but the difference is small and may just reflect the overhead
715 * of a link shutdown. Skip.
717 * (c) newpacket count is greater than the old packet count,
718 * and the difference is large. Connection is live. Don't skip.
720 if (diff >= 0 && diff <= MONITOR_SLOP)
722 (void) report(stdout,
723 GT_("skipping poll of %s, %s inactive\n"),
724 hp->pollname, hp->monitor);
729 #ifdef ACTIVITY_DEBUG
730 report(stdout, GT_("activity on %s was %d, is %d\n"),
731 hp->monitor, hp->monitor_io,
732 ifinfo.rx_packets + ifinfo.tx_packets);
737 #endif /* (defined(linux) && !defined(INET6_ENABLE)) || defined(__FreeBSD__) */