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 * This is free software; you can redistribute it and/or modify it under
13 * the terms of the GNU General Public License as published by the Free
14 * Software Foundation; version 2, or (at your option) any later version.
16 #include <sys/types.h>
17 #include <sys/param.h>
19 #if (defined(linux) && !defined(INET6_ENABLE)) || defined(__FreeBSD__)
24 #if defined(STDC_HEADERS)
27 #if defined(HAVE_UNISTD_H)
30 #include <sys/ioctl.h>
31 #include <sys/socket.h>
32 #include <netinet/in.h>
33 #include <arpa/inet.h>
35 #if defined(__FreeBSD__)
36 #if defined __FreeBSD_USE_KVM
37 #if __FreeBSD_version >= 300001
38 #include <net/if_var.h>
42 #include <sys/fcntl.h>
44 #include <sys/sysctl.h>
45 #include <net/route.h>
46 #include <net/if_dl.h>
50 #include "fetchmail.h"
56 struct in_addr addr, dstaddr, netmask;
57 int rx_packets, tx_packets;
60 struct interface_pair_s {
61 struct in_addr interface_address;
62 struct in_addr interface_mask;
65 static char *netdevfmt;
68 * Count of packets to see on an interface before monitor considers it up.
69 * Needed because when pppd shuts down the link, the packet counts go up
70 * by two (one rx and one tx?, maybe). A value of 2 seems to do the trick,
71 * but we'll give it some extra.
73 #define MONITOR_SLOP 5
77 void interface_init(void)
78 /* figure out which /proc/dev/net format to use */
80 FILE *fp = popen("uname -r", "r"); /* still wins if /proc is out */
82 /* pre-linux-2.2 format -- transmit packet count in 8th field */
83 netdevfmt = "%d %d %*d %*d %*d %d %*d %d %*d %*d %*d %*d %d";
91 if (fscanf(fp, "%d.%d.%*d", &major, &minor) >= 2
92 && major >= 2 && minor >= 2)
93 /* Linux 2.2 -- transmit packet count in 10th field */
94 netdevfmt = "%d %d %*d %*d %*d %d %*d %*d %*d %d %*d %*d %d";
99 static int _get_ifinfo_(int socket_fd, FILE *stats_file, const char *ifname,
101 /* get active network interface information - return non-zero upon success */
103 int namelen = strlen(ifname);
104 struct ifreq request;
105 char *cp, buffer[256];
109 /* initialize result */
110 memset((char *) ifinfo, 0, sizeof(ifinfo_t));
112 /* get the packet I/O counts */
113 while (fgets(buffer, sizeof(buffer) - 1, stats_file)) {
114 for (cp = buffer; *cp && *cp == ' '; ++cp);
115 if (!strncmp(cp, ifname, namelen) &&
116 cp[namelen] == ':') {
118 if (sscanf(cp, netdevfmt,
119 counts, counts+1, counts+2,
120 counts+3,&found)>4) { /* found = dummy */
121 /* newer kernel with byte counts */
122 ifinfo->rx_packets=counts[1];
123 ifinfo->tx_packets=counts[3];
125 /* older kernel, no byte counts */
126 ifinfo->rx_packets=counts[0];
127 ifinfo->tx_packets=counts[2];
132 if (!found) return (FALSE);
134 /* see if the interface is up */
135 strcpy(request.ifr_name, ifname);
136 if (ioctl(socket_fd, SIOCGIFFLAGS, &request) < 0)
138 if (!(request.ifr_flags & IFF_RUNNING))
141 /* get the (local) IP address */
142 strcpy(request.ifr_name, ifname);
143 if (ioctl(socket_fd, SIOCGIFADDR, &request) < 0)
145 ifinfo->addr = ((struct sockaddr_in *) (&request.ifr_addr))->sin_addr;
147 /* get the PPP destination (remote) IP address */
148 ifinfo->dstaddr.s_addr = 0;
149 strcpy(request.ifr_name, ifname);
150 if (ioctl(socket_fd, SIOCGIFDSTADDR, &request) >= 0)
151 ifinfo->dstaddr = ((struct sockaddr_in *)
152 (&request.ifr_dstaddr))->sin_addr;
154 /* get the netmask */
155 strcpy(request.ifr_name, ifname);
156 if (ioctl(socket_fd, SIOCGIFNETMASK, &request) >= 0) {
157 ifinfo->netmask = ((struct sockaddr_in *)
158 (&request.ifr_netmask))->sin_addr;
165 static int get_ifinfo(const char *ifname, ifinfo_t *ifinfo)
167 int socket_fd = socket(AF_INET, SOCK_DGRAM, 0);
168 FILE *stats_file = fopen("/proc/net/dev", "r");
171 if (socket_fd < 0 || !stats_file)
175 char *sp = strchr(ifname, '/');
179 result = _get_ifinfo_(socket_fd, stats_file, ifname, ifinfo);
184 SockClose(close(socket_fd));
186 fclose(stats_file); /* not checking should be safe, mode was "r" */
190 #elif defined __FreeBSD__
192 #if defined __FreeBSD_USE_KVM
195 static struct nlist symbols[] =
200 static u_long ifnet_savedaddr;
201 static gid_t if_rgid;
202 static gid_t if_egid;
205 interface_set_gids(gid_t egid, gid_t rgid)
214 if ((kvmfd = kvm_open(NULL, NULL, NULL, O_RDONLY, NULL)) == NULL)
217 if (kvm_nlist(kvmfd, symbols) < 0)
220 if (kvm_read(kvmfd, (unsigned long) symbols[0].n_value, &ifnet_savedaddr, sizeof(unsigned long)) == -1)
227 get_ifinfo(const char *ifname, ifinfo_t *ifinfo)
232 unsigned long ifnet_addr = ifnet_savedaddr;
233 #if __FreeBSD_version >= 300001
234 struct ifnethead ifnethead;
235 struct ifaddrhead ifaddrhead;
237 struct ifaddr ifaddr;
238 unsigned long ifaddr_addr;
240 unsigned long sa_addr;
246 for (i = 0; ifname[i] && ifname[i] != '/'; i++)
247 iname[i] = ifname[i];
255 report(stderr, 0, _("Unable to open kvm interface. Make sure fetchmail is SGID kmem."));
262 #if __FreeBSD_version >= 300001
263 kvm_read(kvmfd, ifnet_savedaddr, (char *) &ifnethead, sizeof ifnethead);
264 ifnet_addr = (u_long) ifnethead.tqh_first;
266 ifnet_addr = ifnet_savedaddr;
271 kvm_read(kvmfd, ifnet_addr, &ifnet, sizeof(ifnet));
272 kvm_read(kvmfd, (unsigned long) ifnet.if_name, tname, sizeof tname);
273 snprintf(tname, sizeof tname - 1, "%s%d", tname, ifnet.if_unit);
275 if (!strcmp(tname, iname))
277 if (!(ifnet.if_flags & IFF_UP))
284 ifinfo->rx_packets = ifnet.if_ipackets;
285 ifinfo->tx_packets = ifnet.if_opackets;
287 #if __FreeBSD_version >= 300001
288 ifaddr_addr = (u_long) ifnet.if_addrhead.tqh_first;
290 ifaddr_addr = (u_long) ifnet.if_addrlist;
295 kvm_read(kvmfd, ifaddr_addr, &ifaddr, sizeof(ifaddr));
296 kvm_read(kvmfd, (u_long)ifaddr.ifa_addr, &sa, sizeof(sa));
298 if (sa.sa_family != AF_INET)
300 #if __FreeBSD_version >= 300001
301 ifaddr_addr = (u_long) ifaddr.ifa_link.tqe_next;
303 ifaddr_addr = (u_long) ifaddr.ifa_next;
308 ifinfo->addr.s_addr = *(u_long *)(sa.sa_data + 2);
309 kvm_read(kvmfd, (u_long)ifaddr.ifa_dstaddr, &sa, sizeof(sa));
310 ifinfo->dstaddr.s_addr = *(u_long *)(sa.sa_data + 2);
311 kvm_read(kvmfd, (u_long)ifaddr.ifa_netmask, &sa, sizeof(sa));
312 ifinfo->netmask.s_addr = *(u_long *)(sa.sa_data + 2);
326 #if __FreeBSD_version >= 300001
327 ifnet_addr = (u_long) ifnet.if_link.tqe_next;
329 ifnet_addr = (unsigned long) ifnet.if_next;
339 #else /* Do not use KVM on FreeBSD */
342 * Expand the compacted form of addresses as returned via the
343 * configuration read via sysctl().
347 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
353 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
354 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
356 memset(rtinfo->rti_info, 0, sizeof(rtinfo->rti_info));
357 for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
358 if ((rtinfo->rti_addrs & (1 << i)) == 0)
360 rtinfo->rti_info[i] = sa = (struct sockaddr *)cp;
369 get_ifinfo(const char *ifname, ifinfo_t *ifinfo)
378 struct if_msghdr *ifm;
379 struct ifa_msghdr *ifam;
380 struct sockaddr_in *sin;
381 struct sockaddr_dl *sdl;
382 struct rt_addrinfo info;
386 memset(ifinfo, 0, sizeof(ifinfo));
388 /* trim interface name */
390 for (i = 0; i < sizeof(iname) && ifname[i] && ifname[i] != '/'; i++)
391 iname[i] = ifname[i];
393 if (i == 0 || i == sizeof(iname))
395 report(stderr, _("Unable to parse interface name from %s"), ifname);
402 /* get list of existing interfaces */
407 mib[3] = AF_INET; /* Only IP addresses please. */
408 mib[4] = NET_RT_IFLIST;
409 mib[5] = 0; /* List all interfaces. */
412 /* Get interface data. */
414 if (sysctl(mib, 6, NULL, &needed, NULL, 0) == -1)
417 _("get_ifinfo: sysctl (iflist estimate) failed"));
420 if ((buf = malloc(needed)) == NULL)
423 _("get_ifinfo: malloc failed"));
426 if (sysctl(mib, 6, buf, &needed, NULL, 0) == -1)
429 _("get_ifinfo: sysctl (iflist) failed"));
436 /* first look for the interface information */
441 ifm = (struct if_msghdr *)next;
442 next += ifm->ifm_msglen;
444 if (ifm->ifm_version != RTM_VERSION)
447 _("Routing message version %d not understood."),
452 if (ifm->ifm_type == RTM_IFINFO)
454 sdl = (struct sockaddr_dl *)(ifm + 1);
456 if (!(strlen(iname) == sdl->sdl_nlen
457 && strncmp(iname, sdl->sdl_data, sdl->sdl_nlen) == 0))
462 if ( !(ifm->ifm_flags & IFF_UP) )
464 /* the interface is down */
468 ifindex = ifm->ifm_index;
469 ifinfo->rx_packets = ifm->ifm_data.ifi_ipackets;
470 ifinfo->tx_packets = ifm->ifm_data.ifi_opackets;
478 /* we did not find an interface with a matching name */
479 report(stderr, _("No interface found with name %s"), iname);
483 /* now look for the interface's IP address */
488 ifam = (struct ifa_msghdr *)next;
489 next += ifam->ifam_msglen;
492 && ifam->ifam_type == RTM_NEWADDR
493 && ifam->ifam_index == ifindex)
495 /* Expand the compacted addresses */
496 info.rti_addrs = ifam->ifam_addrs;
497 rt_xaddrs((char *)(ifam + 1),
498 ifam->ifam_msglen + (char *)ifam,
501 /* Check for IPv4 address information only */
502 if (info.rti_info[RTAX_IFA]->sa_family != AF_INET)
509 sin = (struct sockaddr_in *)info.rti_info[RTAX_IFA];
512 ifinfo->addr = sin->sin_addr;
515 sin = (struct sockaddr_in *)info.rti_info[RTAX_NETMASK];
518 ifinfo->netmask = sin->sin_addr;
521 /* note: RTAX_BRD contains the address at the other
522 * end of a point-to-point link or the broadcast address
523 * of non point-to-point link
525 sin = (struct sockaddr_in *)info.rti_info[RTAX_BRD];
528 ifinfo->dstaddr = sin->sin_addr;
537 report(stderr, _("No IP address found for %s"), iname);
545 #endif /* __FREEBSD_USE_SYSCTL_GET_IFFINFO */
547 #endif /* defined __FreeBSD__ */
550 #ifndef HAVE_INET_ATON
552 * Note: This is not a true replacement for inet_aton(), as it won't
553 * do the right thing on "255.255.255.255" (which translates to -1 on
554 * most machines). Fortunately this code will be used only if you're
555 * on an older Linux that lacks a real implementation.
557 #ifdef HAVE_NETINET_IN_SYSTM_H
558 # include <sys/types.h>
559 # include <netinet/in_systm.h>
562 #include <netinet/in.h>
563 #include <netinet/ip.h>
564 #include <arpa/inet.h>
567 static int inet_aton(const char *cp, struct in_addr *inp) {
570 addr = inet_addr(cp);
571 if (addr == ((long) -1)) return 0;
573 memcpy(inp, &addr, sizeof(addr));
576 #endif /* HAVE_INET_ATON */
578 void interface_parse(char *buf, struct hostdata *hp)
579 /* parse 'interface' specification */
583 hp->interface = xstrdup(buf);
585 /* find and isolate just the IP address */
586 if (!(cp1 = strchr(buf, '/')))
588 (void) report(stderr,
589 _("missing IP interface address\n"));
594 /* find and isolate just the netmask */
595 if (!(cp2 = strchr(cp1, '/')))
596 cp2 = "255.255.255.255";
600 /* convert IP address and netmask */
601 hp->interface_pair = (struct interface_pair_s *)xmalloc(sizeof(struct interface_pair_s));
602 if (!inet_aton(cp1, &hp->interface_pair->interface_address))
604 (void) report(stderr,
605 _("invalid IP interface address\n"));
608 if (!inet_aton(cp2, &hp->interface_pair->interface_mask))
610 (void) report(stderr,
611 _("invalid IP interface mask\n"));
614 /* apply the mask now to the IP address (range) required */
615 hp->interface_pair->interface_address.s_addr &=
616 hp->interface_pair->interface_mask.s_addr;
618 /* restore original interface string (for configuration dumper) */
623 void interface_note_activity(struct hostdata *hp)
624 /* save interface I/O counts */
629 /* if not monitoring link, all done */
633 /* get the current I/O stats for the monitored link */
634 if (get_ifinfo(hp->monitor, &ifinfo))
635 /* update this and preceeding host entries using the link
636 (they were already set during this pass but the I/O
637 count has now changed and they need to be re-updated)
639 for (ctl = querylist; ctl; ctl = ctl->next) {
640 if (ctl->server.monitor && !strcmp(hp->monitor, ctl->server.monitor))
641 ctl->server.monitor_io =
642 ifinfo.rx_packets + ifinfo.tx_packets;
643 /* do NOT update host entries following this one */
644 if (&ctl->server == hp)
648 #ifdef ACTIVITY_DEBUG
649 (void) report(stdout,
650 _("activity on %s -noted- as %d\n"),
651 hp->monitor, hp->monitor_io);
655 int interface_approve(struct hostdata *hp, flag domonitor)
656 /* return TRUE if OK to poll, FALSE otherwise */
660 /* check interface IP address (range), if specified */
662 /* get interface info */
663 if (!get_ifinfo(hp->interface, &ifinfo)) {
664 (void) report(stdout,
665 _("skipping poll of %s, %s down\n"),
666 hp->pollname, hp->interface);
669 /* check the IP addresses (range) */
671 /* check remote IP address */
672 ((ifinfo.dstaddr.s_addr != 0) &&
673 (ifinfo.dstaddr.s_addr &
674 hp->interface_pair->interface_mask.s_addr) ==
675 hp->interface_pair->interface_address.s_addr)
677 /* check local IP address */
678 ((ifinfo.addr.s_addr &
679 hp->interface_pair->interface_mask.s_addr) ==
680 hp->interface_pair->interface_address.s_addr)
683 (void) report(stdout,
684 _("skipping poll of %s, %s IP address excluded\n"),
685 hp->pollname, hp->interface);
690 /* if not monitoring link, all done */
691 if (!domonitor || !hp->monitor)
694 #ifdef ACTIVITY_DEBUG
695 (void) report(stdout,
696 _("activity on %s checked as %d\n"),
697 hp->monitor, hp->monitor_io);
699 /* if monitoring, check link for activity if it is up */
700 if (get_ifinfo(hp->monitor, &ifinfo))
702 int diff = (ifinfo.rx_packets + ifinfo.tx_packets)
706 * There are three cases here:
708 * (a) If the new packet count is less than the recorded one,
709 * probably pppd was restarted while fetchmail was running.
712 * (b) newpacket count is greater than the old packet count,
713 * but the difference is small and may just reflect the overhead
714 * of a link shutdown. Skip.
716 * (c) newpacket count is greater than the old packet count,
717 * and the difference is large. Connection is live. Don't skip.
719 if (diff >= 0 && diff <= MONITOR_SLOP)
721 (void) report(stdout,
722 _("skipping poll of %s, %s inactive\n"),
723 hp->pollname, hp->monitor);
728 #ifdef ACTIVITY_DEBUG
729 report(stdout, _("activity on %s was %d, is %d\n"),
730 hp->monitor, hp->monitor_io,
731 ifinfo.rx_packets + ifinfo.tx_packets);
736 #endif /* (defined(linux) && !defined(INET6_ENABLE)) || defined(__FreeBSD__) */