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;
233 unsigned long sa_addr;
239 for (i = 0; ifname[i] && ifname[i] != '/' && i < sizeof(iname) - 1; i++)
240 iname[i] = ifname[i];
248 report(stderr, 0, GT_("Unable to open kvm interface. Make sure fetchmail is SGID kmem."));
255 kvm_read(kvmfd, ifnet_savedaddr, (char *) &ifnethead, sizeof ifnethead);
256 ifnet_addr = (u_long) ifnethead.tqh_first;
260 kvm_read(kvmfd, ifnet_addr, &ifnet, sizeof(ifnet));
261 kvm_read(kvmfd, (unsigned long) ifnet.if_name, tname, sizeof tname);
262 snprintf(tname + strlen(tname), sizeof(tname) - strlen(tname), "%d", ifnet.if_unit);
264 if (!strcmp(tname, iname))
266 if (!(ifnet.if_flags & IFF_UP))
273 ifinfo->rx_packets = ifnet.if_ipackets;
274 ifinfo->tx_packets = ifnet.if_opackets;
276 ifaddr_addr = (u_long) ifnet.if_addrhead.tqh_first;
280 kvm_read(kvmfd, ifaddr_addr, &ifaddr, sizeof(ifaddr));
281 kvm_read(kvmfd, (u_long)ifaddr.ifa_addr, &sa, sizeof(sa));
283 if (sa.sa_family != AF_INET)
285 ifaddr_addr = (u_long) ifaddr.ifa_link.tqe_next;
289 ifinfo->addr.s_addr = *(u_long *)(sa.sa_data + 2);
290 kvm_read(kvmfd, (u_long)ifaddr.ifa_dstaddr, &sa, sizeof(sa));
291 ifinfo->dstaddr.s_addr = *(u_long *)(sa.sa_data + 2);
292 kvm_read(kvmfd, (u_long)ifaddr.ifa_netmask, &sa, sizeof(sa));
293 ifinfo->netmask.s_addr = *(u_long *)(sa.sa_data + 2);
307 ifnet_addr = (u_long) ifnet.if_link.tqe_next;
316 #else /* Do not use KVM on FreeBSD */
319 * Expand the compacted form of addresses as returned via the
320 * configuration read via sysctl().
324 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
330 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
331 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
333 memset(rtinfo->rti_info, 0, sizeof(rtinfo->rti_info));
334 for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
335 if ((rtinfo->rti_addrs & (1 << i)) == 0)
337 rtinfo->rti_info[i] = sa = (struct sockaddr *)cp;
346 get_ifinfo(const char *ifname, ifinfo_t *ifinfo)
355 struct if_msghdr *ifm;
356 struct ifa_msghdr *ifam;
357 struct sockaddr_in *sin;
358 struct sockaddr_dl *sdl;
359 struct rt_addrinfo info;
363 memset(ifinfo, 0, sizeof(ifinfo));
365 /* trim interface name */
367 for (i = 0; i < sizeof(iname) && ifname[i] && ifname[i] != '/'; i++)
368 iname[i] = ifname[i];
370 if (i == 0 || i == sizeof(iname))
372 report(stderr, GT_("Unable to parse interface name from %s"), ifname);
379 /* get list of existing interfaces */
384 mib[3] = AF_INET; /* Only IP addresses please. */
385 mib[4] = NET_RT_IFLIST;
386 mib[5] = 0; /* List all interfaces. */
389 /* Get interface data. */
391 if (sysctl(mib, 6, NULL, &needed, NULL, 0) == -1)
394 GT_("get_ifinfo: sysctl (iflist estimate) failed"));
397 if ((buf = (char *)malloc(needed)) == NULL)
400 GT_("get_ifinfo: malloc failed"));
403 if (sysctl(mib, 6, buf, &needed, NULL, 0) == -1)
406 GT_("get_ifinfo: sysctl (iflist) failed"));
413 /* first look for the interface information */
418 ifm = (struct if_msghdr *)next;
419 next += ifm->ifm_msglen;
421 if (ifm->ifm_version != RTM_VERSION)
424 GT_("Routing message version %d not understood."),
429 if (ifm->ifm_type == RTM_IFINFO)
431 sdl = (struct sockaddr_dl *)(ifm + 1);
433 if (!(strlen(iname) == sdl->sdl_nlen
434 && strncmp(iname, sdl->sdl_data, sdl->sdl_nlen) == 0))
439 if ( !(ifm->ifm_flags & IFF_UP) )
441 /* the interface is down */
445 ifindex = ifm->ifm_index;
446 ifinfo->rx_packets = ifm->ifm_data.ifi_ipackets;
447 ifinfo->tx_packets = ifm->ifm_data.ifi_opackets;
455 /* we did not find an interface with a matching name */
456 report(stderr, GT_("No interface found with name %s"), iname);
460 /* now look for the interface's IP address */
465 ifam = (struct ifa_msghdr *)next;
466 next += ifam->ifam_msglen;
469 && ifam->ifam_type == RTM_NEWADDR
470 && ifam->ifam_index == ifindex)
472 /* Expand the compacted addresses */
473 info.rti_addrs = ifam->ifam_addrs;
474 rt_xaddrs((char *)(ifam + 1),
475 ifam->ifam_msglen + (char *)ifam,
478 /* Check for IPv4 address information only */
479 if (info.rti_info[RTAX_IFA]->sa_family != AF_INET)
486 sin = (struct sockaddr_in *)info.rti_info[RTAX_IFA];
489 ifinfo->addr = sin->sin_addr;
492 sin = (struct sockaddr_in *)info.rti_info[RTAX_NETMASK];
495 ifinfo->netmask = sin->sin_addr;
498 /* note: RTAX_BRD contains the address at the other
499 * end of a point-to-point link or the broadcast address
500 * of non point-to-point link
502 sin = (struct sockaddr_in *)info.rti_info[RTAX_BRD];
505 ifinfo->dstaddr = sin->sin_addr;
514 report(stderr, GT_("No IP address found for %s"), iname);
522 #endif /* __FREEBSD_USE_SYSCTL_GET_IFFINFO */
526 #ifndef HAVE_INET_ATON
528 * Note: This is not a true replacement for inet_aton(), as it won't
529 * do the right thing on "255.255.255.255" (which translates to -1 on
530 * most machines). Fortunately this code will be used only if you're
531 * on an older Linux that lacks a real implementation.
533 #ifdef HAVE_NETINET_IN_SYSTM_H
534 # include <sys/types.h>
535 # include <netinet/in_systm.h>
538 #include <netinet/in.h>
539 #include <netinet/ip.h>
540 #include <arpa/inet.h>
543 static int inet_aton(const char *cp, struct in_addr *inp) {
546 addr = inet_addr(cp);
547 if (addr == ((long) -1)) return 0;
549 memcpy(inp, &addr, sizeof(addr));
552 #endif /* HAVE_INET_ATON */
554 void interface_parse(char *buf, struct hostdata *hp)
555 /* parse 'interface' specification */
558 char mask1[] = "255.255.255.255";
560 hp->interface = xstrdup(buf);
562 /* find and isolate just the IP address */
563 if (!(cp1 = strchr(buf, '/')))
565 (void) report(stderr,
566 GT_("missing IP interface address\n"));
571 /* find and isolate just the netmask */
572 if (!(cp2 = strchr(cp1, '/')))
577 /* convert IP address and netmask */
578 hp->interface_pair = (struct interface_pair_s *)xmalloc(sizeof(struct interface_pair_s));
579 if (!inet_aton(cp1, &hp->interface_pair->interface_address))
581 (void) report(stderr,
582 GT_("invalid IP interface address\n"));
585 if (!inet_aton(cp2, &hp->interface_pair->interface_mask))
587 (void) report(stderr,
588 GT_("invalid IP interface mask\n"));
591 /* apply the mask now to the IP address (range) required */
592 hp->interface_pair->interface_address.s_addr &=
593 hp->interface_pair->interface_mask.s_addr;
595 /* restore original interface string (for configuration dumper) */
600 void interface_note_activity(struct hostdata *hp)
601 /* save interface I/O counts */
606 /* if not monitoring link, all done */
610 /* get the current I/O stats for the monitored link */
611 if (get_ifinfo(hp->monitor, &ifinfo))
612 /* update this and preceeding host entries using the link
613 (they were already set during this pass but the I/O
614 count has now changed and they need to be re-updated)
616 for (ctl = querylist; ctl; ctl = ctl->next) {
617 if (ctl->server.monitor && !strcmp(hp->monitor, ctl->server.monitor))
618 ctl->server.monitor_io =
619 ifinfo.rx_packets + ifinfo.tx_packets;
620 /* do NOT update host entries following this one */
621 if (&ctl->server == hp)
625 #ifdef ACTIVITY_DEBUG
626 (void) report(stdout,
627 GT_("activity on %s -noted- as %d\n"),
628 hp->monitor, hp->monitor_io);
632 int interface_approve(struct hostdata *hp, flag domonitor)
633 /* return TRUE if OK to poll, FALSE otherwise */
637 /* check interface IP address (range), if specified */
639 /* get interface info */
640 if (!get_ifinfo(hp->interface, &ifinfo)) {
641 (void) report(stdout,
642 GT_("skipping poll of %s, %s down\n"),
643 hp->pollname, hp->interface);
646 /* check the IP addresses (range) */
648 /* check remote IP address */
649 ((ifinfo.dstaddr.s_addr != 0) &&
650 (ifinfo.dstaddr.s_addr &
651 hp->interface_pair->interface_mask.s_addr) ==
652 hp->interface_pair->interface_address.s_addr)
654 /* check local IP address */
655 ((ifinfo.addr.s_addr &
656 hp->interface_pair->interface_mask.s_addr) ==
657 hp->interface_pair->interface_address.s_addr)
660 (void) report(stdout,
661 GT_("skipping poll of %s, %s IP address excluded\n"),
662 hp->pollname, hp->interface);
667 /* if not monitoring link, all done */
668 if (!domonitor || !hp->monitor)
671 #ifdef ACTIVITY_DEBUG
672 (void) report(stdout,
673 GT_("activity on %s checked as %d\n"),
674 hp->monitor, hp->monitor_io);
676 /* if monitoring, check link for activity if it is up */
677 if (get_ifinfo(hp->monitor, &ifinfo))
679 int diff = (ifinfo.rx_packets + ifinfo.tx_packets)
683 * There are three cases here:
685 * (a) If the new packet count is less than the recorded one,
686 * probably pppd was restarted while fetchmail was running.
689 * (b) newpacket count is greater than the old packet count,
690 * but the difference is small and may just reflect the overhead
691 * of a link shutdown. Skip.
693 * (c) newpacket count is greater than the old packet count,
694 * and the difference is large. Connection is live. Don't skip.
696 if (diff >= 0 && diff <= MONITOR_SLOP)
698 (void) report(stdout,
699 GT_("skipping poll of %s, %s inactive\n"),
700 hp->pollname, hp->monitor);
705 #ifdef ACTIVITY_DEBUG
706 report(stdout, GT_("activity on %s was %d, is %d\n"),
707 hp->monitor, hp->monitor_io,
708 ifinfo.rx_packets + ifinfo.tx_packets);
713 #endif /* CAN_MONITOR */
715 #ifndef have_interface_init
716 void interface_init(void) {}