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.
15 #include "fetchmail.h"
18 #include <sys/types.h>
19 #include <sys/param.h>
22 #include <sys/utsname.h>
29 #include <sys/ioctl.h>
30 #include <sys/socket.h>
31 #include <netinet/in.h>
32 #include <arpa/inet.h>
34 #if defined(__FreeBSD__)
35 #if defined __FreeBSD_USE_KVM
36 #include <net/if_var.h>
39 #include <sys/fcntl.h>
40 #else /* !defined __FreeBSD_USE_KVM */
41 #include <sys/sysctl.h>
42 #include <net/route.h>
43 #include <net/if_dl.h>
44 #endif /* defined __FreeBSD_USE_KVM */
45 #endif /* defined __FreeBSD__ */
51 struct in_addr addr, dstaddr, netmask;
52 int rx_packets, tx_packets;
55 struct interface_pair_s {
56 struct in_addr interface_address;
57 struct in_addr interface_mask;
61 * Count of packets to see on an interface before monitor considers it up.
62 * Needed because when pppd shuts down the link, the packet counts go up
63 * by two (one rx and one tx?, maybe). A value of 2 seems to do the trick,
64 * but we'll give it some extra.
66 #define MONITOR_SLOP 5
69 #define have_interface_init
71 static const char *netdevfmt;
73 void interface_init(void)
74 /* figure out which /proc/net/dev format to use */
76 struct utsname utsname;
78 /* Linux 2.2 -- transmit packet count in 10th field */
79 netdevfmt = "%d %d %*d %*d %*d %d %*d %*d %*d %d %*d %*d %d";
81 if (uname(&utsname) < 0)
87 if (sscanf(utsname.release, "%d.%d.%*d", &major, &minor) >= 2
88 && (major < 2 || (major == 2 && minor < 2)))
89 /* pre-linux-2.2 format -- transmit packet count in 8th field */
90 netdevfmt = "%d %d %*d %*d %*d %d %*d %d %*d %*d %*d %*d %d";
94 static int _get_ifinfoGT_(int socket_fd, FILE *stats_file, const char *ifname,
96 /* get active network interface information - return non-zero upon success */
98 int namelen = strlen(ifname);
100 char *cp, buffer[256];
104 /* initialize result */
105 memset((char *) ifinfo, 0, sizeof(ifinfo_t));
107 /* get the packet I/O counts */
108 while (fgets(buffer, sizeof(buffer) - 1, stats_file)) {
109 for (cp = buffer; *cp && *cp == ' '; ++cp);
110 if (!strncmp(cp, ifname, namelen) &&
111 cp[namelen] == ':') {
113 if (sscanf(cp, netdevfmt,
114 counts, counts+1, counts+2,
115 counts+3,&found)>4) { /* found = dummy */
116 /* newer kernel with byte counts */
117 ifinfo->rx_packets=counts[1];
118 ifinfo->tx_packets=counts[3];
120 /* older kernel, no byte counts */
121 ifinfo->rx_packets=counts[0];
122 ifinfo->tx_packets=counts[2];
127 if (!found) return (FALSE);
129 /* see if the interface is up */
130 strcpy(request.ifr_name, ifname);
131 if (ioctl(socket_fd, SIOCGIFFLAGS, &request) < 0)
133 if (!(request.ifr_flags & IFF_RUNNING))
136 /* get the (local) IP address */
137 strcpy(request.ifr_name, ifname);
138 if (ioctl(socket_fd, SIOCGIFADDR, &request) < 0)
140 ifinfo->addr = ((struct sockaddr_in *) (&request.ifr_addr))->sin_addr;
142 /* get the PPP destination (remote) IP address */
143 ifinfo->dstaddr.s_addr = 0;
144 strcpy(request.ifr_name, ifname);
145 if (ioctl(socket_fd, SIOCGIFDSTADDR, &request) >= 0)
146 ifinfo->dstaddr = ((struct sockaddr_in *)
147 (&request.ifr_dstaddr))->sin_addr;
149 /* get the netmask */
150 strcpy(request.ifr_name, ifname);
151 if (ioctl(socket_fd, SIOCGIFNETMASK, &request) >= 0) {
152 ifinfo->netmask = ((struct sockaddr_in *)
153 (&request.ifr_netmask))->sin_addr;
160 static int get_ifinfo(const char *ifname, ifinfo_t *ifinfo)
162 int socket_fd = socket(AF_INET, SOCK_DGRAM, 0);
163 FILE *stats_file = fopen("/proc/net/dev", "r");
166 if (socket_fd < 0 || !stats_file)
170 char *tmp = xstrdup(ifname);
171 char *sp = strchr(tmp, '/');
172 /* hide slash and trailing info from ifname */
175 result = _get_ifinfoGT_(socket_fd, stats_file, tmp, ifinfo);
179 SockClose(socket_fd);
181 fclose(stats_file); /* not checking should be safe, mode was "r" */
185 #elif defined __FreeBSD__
187 #if defined __FreeBSD_USE_KVM
190 static struct nlist symbols[] =
195 static u_long ifnet_savedaddr;
196 static gid_t if_rgid;
197 static gid_t if_egid;
200 interface_set_gids(gid_t egid, gid_t rgid)
209 if ((kvmfd = kvm_open(NULL, NULL, NULL, O_RDONLY, NULL)) == NULL)
212 if (kvm_nlist(kvmfd, symbols) < 0)
215 if (kvm_read(kvmfd, (unsigned long) symbols[0].n_value, &ifnet_savedaddr, sizeof(unsigned long)) == -1)
222 get_ifinfo(const char *ifname, ifinfo_t *ifinfo)
227 unsigned long ifnet_addr = ifnet_savedaddr;
228 struct ifnethead ifnethead;
229 struct ifaddrhead ifaddrhead;
230 struct ifaddr ifaddr;
231 unsigned long ifaddr_addr;
238 for (i = 0; ifname[i] && ifname[i] != '/' && i < sizeof(iname) - 1; i++)
239 iname[i] = ifname[i];
247 report(stderr, 0, GT_("Unable to open kvm interface. Make sure fetchmail is SGID kmem."));
254 kvm_read(kvmfd, ifnet_savedaddr, (char *) &ifnethead, sizeof ifnethead);
255 ifnet_addr = (u_long) ifnethead.tqh_first;
259 kvm_read(kvmfd, ifnet_addr, &ifnet, sizeof(ifnet));
260 kvm_read(kvmfd, (unsigned long) ifnet.if_name, tname, sizeof tname);
261 snprintf(tname + strlen(tname), sizeof(tname) - strlen(tname), "%d", ifnet.if_unit);
263 if (!strcmp(tname, iname))
265 if (!(ifnet.if_flags & IFF_UP))
272 ifinfo->rx_packets = ifnet.if_ipackets;
273 ifinfo->tx_packets = ifnet.if_opackets;
275 ifaddr_addr = (u_long) ifnet.if_addrhead.tqh_first;
279 kvm_read(kvmfd, ifaddr_addr, &ifaddr, sizeof(ifaddr));
280 kvm_read(kvmfd, (u_long)ifaddr.ifa_addr, &sa, sizeof(sa));
282 if (sa.sa_family != AF_INET)
284 ifaddr_addr = (u_long) ifaddr.ifa_link.tqe_next;
288 ifinfo->addr.s_addr = *(u_long *)(sa.sa_data + 2);
289 kvm_read(kvmfd, (u_long)ifaddr.ifa_dstaddr, &sa, sizeof(sa));
290 ifinfo->dstaddr.s_addr = *(u_long *)(sa.sa_data + 2);
291 kvm_read(kvmfd, (u_long)ifaddr.ifa_netmask, &sa, sizeof(sa));
292 ifinfo->netmask.s_addr = *(u_long *)(sa.sa_data + 2);
306 ifnet_addr = (u_long) ifnet.if_link.tqe_next;
315 #else /* Do not use KVM on FreeBSD */
318 * Expand the compacted form of addresses as returned via the
319 * configuration read via sysctl().
323 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
329 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
330 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
332 memset(rtinfo->rti_info, 0, sizeof(rtinfo->rti_info));
333 for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
334 if ((rtinfo->rti_addrs & (1 << i)) == 0)
336 rtinfo->rti_info[i] = sa = (struct sockaddr *)cp;
345 get_ifinfo(const char *ifname, ifinfo_t *ifinfo)
354 struct if_msghdr *ifm;
355 struct ifa_msghdr *ifam;
356 struct sockaddr_in *sin;
357 struct sockaddr_dl *sdl;
358 struct rt_addrinfo info;
362 memset(ifinfo, 0, sizeof(*ifinfo));
364 /* trim interface name */
366 for (i = 0; i < sizeof(iname) && ifname[i] && ifname[i] != '/'; i++)
367 iname[i] = ifname[i];
369 if (i == 0 || i == sizeof(iname))
371 report(stderr, GT_("Unable to parse interface name from %s"), ifname);
378 /* get list of existing interfaces */
383 mib[3] = AF_INET; /* Only IP addresses please. */
384 mib[4] = NET_RT_IFLIST;
385 mib[5] = 0; /* List all interfaces. */
388 /* Get interface data. */
390 if (sysctl(mib, 6, NULL, &needed, NULL, 0) == -1)
393 GT_("get_ifinfo: sysctl (iflist estimate) failed"));
396 if ((buf = (char *)malloc(needed)) == NULL)
399 GT_("get_ifinfo: malloc failed"));
402 if (sysctl(mib, 6, buf, &needed, NULL, 0) == -1)
405 GT_("get_ifinfo: sysctl (iflist) failed"));
412 /* first look for the interface information */
417 ifm = (struct if_msghdr *)next;
418 next += ifm->ifm_msglen;
420 if (ifm->ifm_version != RTM_VERSION)
423 GT_("Routing message version %d not understood."),
428 if (ifm->ifm_type == RTM_IFINFO)
430 sdl = (struct sockaddr_dl *)(ifm + 1);
432 if (!(strlen(iname) == sdl->sdl_nlen
433 && strncmp(iname, sdl->sdl_data, sdl->sdl_nlen) == 0))
438 if ( !(ifm->ifm_flags & IFF_UP) )
440 /* the interface is down */
444 ifindex = ifm->ifm_index;
445 ifinfo->rx_packets = ifm->ifm_data.ifi_ipackets;
446 ifinfo->tx_packets = ifm->ifm_data.ifi_opackets;
454 /* we did not find an interface with a matching name */
455 report(stderr, GT_("No interface found with name %s"), iname);
459 /* now look for the interface's IP address */
464 ifam = (struct ifa_msghdr *)next;
465 next += ifam->ifam_msglen;
468 && ifam->ifam_type == RTM_NEWADDR
469 && ifam->ifam_index == ifindex)
471 /* Expand the compacted addresses */
472 info.rti_addrs = ifam->ifam_addrs;
473 rt_xaddrs((char *)(ifam + 1),
474 ifam->ifam_msglen + (char *)ifam,
477 /* Check for IPv4 address information only */
478 if (info.rti_info[RTAX_IFA]->sa_family != AF_INET)
485 sin = (struct sockaddr_in *)info.rti_info[RTAX_IFA];
488 ifinfo->addr = sin->sin_addr;
491 sin = (struct sockaddr_in *)info.rti_info[RTAX_NETMASK];
494 ifinfo->netmask = sin->sin_addr;
497 /* note: RTAX_BRD contains the address at the other
498 * end of a point-to-point link or the broadcast address
499 * of non point-to-point link
501 sin = (struct sockaddr_in *)info.rti_info[RTAX_BRD];
504 ifinfo->dstaddr = sin->sin_addr;
513 report(stderr, GT_("No IP address found for %s"), iname);
521 #endif /* __FREEBSD_USE_SYSCTL_GET_IFFINFO */
525 #ifndef HAVE_INET_ATON
527 * Note: This is not a true replacement for inet_aton(), as it won't
528 * do the right thing on "255.255.255.255" (which translates to -1 on
529 * most machines). Fortunately this code will be used only if you're
530 * on an older Linux that lacks a real implementation.
532 #ifdef HAVE_NETINET_IN_SYSTM_H
533 # include <sys/types.h>
534 # include <netinet/in_systm.h>
537 #include <netinet/in.h>
538 #include <netinet/ip.h>
539 #include <arpa/inet.h>
542 static int inet_aton(const char *cp, struct in_addr *inp) {
545 addr = inet_addr(cp);
546 if (addr == ((long) -1)) return 0;
548 memcpy(inp, &addr, sizeof(addr));
551 #endif /* HAVE_INET_ATON */
553 void interface_parse(char *buf, struct hostdata *hp)
554 /* parse 'interface' specification */
557 char mask1[] = "255.255.255.255";
559 hp->interface = xstrdup(buf);
561 /* find and isolate just the IP address */
562 if (!(cp1 = strchr(buf, '/')))
564 (void) report(stderr,
565 GT_("missing IP interface address\n"));
570 /* find and isolate just the netmask */
571 if (!(cp2 = strchr(cp1, '/')))
576 /* convert IP address and netmask */
577 hp->interface_pair = (struct interface_pair_s *)xmalloc(sizeof(struct interface_pair_s));
578 if (!inet_aton(cp1, &hp->interface_pair->interface_address))
580 (void) report(stderr,
581 GT_("invalid IP interface address\n"));
584 if (!inet_aton(cp2, &hp->interface_pair->interface_mask))
586 (void) report(stderr,
587 GT_("invalid IP interface mask\n"));
590 /* apply the mask now to the IP address (range) required */
591 hp->interface_pair->interface_address.s_addr &=
592 hp->interface_pair->interface_mask.s_addr;
594 /* restore original interface string (for configuration dumper) */
599 void interface_note_activity(struct hostdata *hp)
600 /* save interface I/O counts */
605 /* if not monitoring link, all done */
609 /* get the current I/O stats for the monitored link */
610 if (get_ifinfo(hp->monitor, &ifinfo))
611 /* update this and preceeding host entries using the link
612 (they were already set during this pass but the I/O
613 count has now changed and they need to be re-updated)
615 for (ctl = querylist; ctl; ctl = ctl->next) {
616 if (ctl->server.monitor && !strcmp(hp->monitor, ctl->server.monitor))
617 ctl->server.monitor_io =
618 ifinfo.rx_packets + ifinfo.tx_packets;
619 /* do NOT update host entries following this one */
620 if (&ctl->server == hp)
624 #ifdef ACTIVITY_DEBUG
625 (void) report(stdout,
626 GT_("activity on %s -noted- as %d\n"),
627 hp->monitor, hp->monitor_io);
631 int interface_approve(struct hostdata *hp, flag domonitor)
632 /* return TRUE if OK to poll, FALSE otherwise */
636 /* check interface IP address (range), if specified */
638 /* get interface info */
639 if (!get_ifinfo(hp->interface, &ifinfo)) {
640 (void) report(stdout,
641 GT_("skipping poll of %s, %s down\n"),
642 hp->pollname, hp->interface);
645 /* check the IP addresses (range) */
647 /* check remote IP address */
648 ((ifinfo.dstaddr.s_addr != 0) &&
649 (ifinfo.dstaddr.s_addr &
650 hp->interface_pair->interface_mask.s_addr) ==
651 hp->interface_pair->interface_address.s_addr)
653 /* check local IP address */
654 ((ifinfo.addr.s_addr &
655 hp->interface_pair->interface_mask.s_addr) ==
656 hp->interface_pair->interface_address.s_addr)
659 (void) report(stdout,
660 GT_("skipping poll of %s, %s IP address excluded\n"),
661 hp->pollname, hp->interface);
666 /* if not monitoring link, all done */
667 if (!domonitor || !hp->monitor)
670 #ifdef ACTIVITY_DEBUG
671 (void) report(stdout,
672 GT_("activity on %s checked as %d\n"),
673 hp->monitor, hp->monitor_io);
675 /* if monitoring, check link for activity if it is up */
676 if (get_ifinfo(hp->monitor, &ifinfo))
678 int diff = (ifinfo.rx_packets + ifinfo.tx_packets)
682 * There are three cases here:
684 * (a) If the new packet count is less than the recorded one,
685 * probably pppd was restarted while fetchmail was running.
688 * (b) newpacket count is greater than the old packet count,
689 * but the difference is small and may just reflect the overhead
690 * of a link shutdown. Skip.
692 * (c) newpacket count is greater than the old packet count,
693 * and the difference is large. Connection is live. Don't skip.
695 if (diff >= 0 && diff <= MONITOR_SLOP)
697 (void) report(stdout,
698 GT_("skipping poll of %s, %s inactive\n"),
699 hp->pollname, hp->monitor);
704 #ifdef ACTIVITY_DEBUG
705 report(stdout, GT_("activity on %s was %d, is %d\n"),
706 hp->monitor, hp->monitor_io,
707 ifinfo.rx_packets + ifinfo.tx_packets);
712 #endif /* CAN_MONITOR */
714 #ifndef have_interface_init
715 void interface_init(void) {}