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)) || 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 __FreeBSD_version >= 300001
37 #include <net/if_var.h>
41 #include <sys/fcntl.h>
44 #include "fetchmail.h"
49 struct in_addr addr, dstaddr, netmask;
50 int rx_packets, tx_packets;
53 struct interface_pair_s {
54 struct in_addr interface_address;
55 struct in_addr interface_mask;
58 static char *netdevfmt;
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
70 void interface_init(void)
71 /* figure out which /proc/dev/net format to use */
73 FILE *fp = popen("uname -r", "r"); /* still wins if /proc is out */
75 /* pre-linux-2.2 format -- transmit packet count in 8th field */
76 netdevfmt = "%d %d %*d %*d %*d %d %*d %d %*d %*d %*d %*d %d";
84 if (fscanf(fp, "%d.%d.%*d", &major, &minor) != 2)
87 if (major >= 2 && minor >= 2)
88 /* Linux 2.2 -- transmit packet count in 10th field */
89 netdevfmt = "%d %d %*d %*d %*d %d %*d %*d %*d %d %*d %*d %d";
93 static int _get_ifinfo_(int socket_fd, FILE *stats_file, const char *ifname,
95 /* get active network interface information - return non-zero upon success */
97 int namelen = strlen(ifname);
99 char *cp, buffer[256];
103 /* initialize result */
104 memset((char *) ifinfo, 0, sizeof(ifinfo_t));
106 /* get the packet I/O counts */
107 while (fgets(buffer, sizeof(buffer) - 1, stats_file)) {
108 for (cp = buffer; *cp && *cp == ' '; ++cp);
109 if (!strncmp(cp, ifname, namelen) &&
110 cp[namelen] == ':') {
112 if (sscanf(cp, netdevfmt,
113 counts, counts+1, counts+2,
114 counts+3,&found)>4) { /* found = dummy */
115 /* newer kernel with byte counts */
116 ifinfo->rx_packets=counts[1];
117 ifinfo->tx_packets=counts[3];
119 /* older kernel, no byte counts */
120 ifinfo->rx_packets=counts[0];
121 ifinfo->tx_packets=counts[2];
126 if (!found) return (FALSE);
128 /* see if the interface is up */
129 strcpy(request.ifr_name, ifname);
130 if (ioctl(socket_fd, SIOCGIFFLAGS, &request) < 0)
132 if (!(request.ifr_flags & IFF_RUNNING))
135 /* get the IP address */
136 strcpy(request.ifr_name, ifname);
137 if (ioctl(socket_fd, SIOCGIFADDR, &request) < 0)
139 ifinfo->addr = ((struct sockaddr_in *) (&request.ifr_addr))->sin_addr;
141 /* get the PPP destination IP address */
142 strcpy(request.ifr_name, ifname);
143 if (ioctl(socket_fd, SIOCGIFDSTADDR, &request) >= 0)
144 ifinfo->dstaddr = ((struct sockaddr_in *)
145 (&request.ifr_dstaddr))->sin_addr;
147 /* get the netmask */
148 strcpy(request.ifr_name, ifname);
149 if (ioctl(socket_fd, SIOCGIFNETMASK, &request) >= 0) {
150 ifinfo->netmask = ((struct sockaddr_in *)
151 (&request.ifr_netmask))->sin_addr;
158 static int get_ifinfo(const char *ifname, ifinfo_t *ifinfo)
160 int socket_fd = socket(AF_INET, SOCK_DGRAM, 0);
161 FILE *stats_file = fopen("/proc/net/dev", "r");
164 if (socket_fd < 0 || !stats_file)
168 char *sp = strchr(ifname, '/');
172 result = _get_ifinfo_(socket_fd, stats_file, ifname, ifinfo);
183 #elif defined __FreeBSD__
186 static struct nlist symbols[] =
191 static u_long ifnet_savedaddr;
192 static gid_t if_rgid;
193 static gid_t if_egid;
196 interface_set_gids(gid_t egid, gid_t rgid)
205 if ((kvmfd = kvm_open(NULL, NULL, NULL, O_RDONLY, NULL)) == NULL)
208 if (kvm_nlist(kvmfd, symbols) < 0)
211 if (kvm_read(kvmfd, (unsigned long) symbols[0].n_value, &ifnet_savedaddr, sizeof(unsigned long)) == -1)
218 get_ifinfo(const char *ifname, ifinfo_t *ifinfo)
223 unsigned long ifnet_addr = ifnet_savedaddr;
224 #if __FreeBSD_version >= 300001
225 struct ifnethead ifnethead;
226 struct ifaddrhead ifaddrhead;
228 struct ifaddr ifaddr;
229 unsigned long ifaddr_addr;
231 unsigned long sa_addr;
237 for (i = 0; ifname[i] && ifname[i] != '/'; i++)
238 iname[i] = ifname[i];
246 report(stderr, 0, _("Unable to open kvm interface. Make sure fetchmail is SGID kmem."));
253 #if __FreeBSD_version >= 300001
254 kvm_read(kvmfd, ifnet_savedaddr, (char *) &ifnethead, sizeof ifnethead);
255 ifnet_addr = (u_long) ifnethead.tqh_first;
257 ifnet_addr = ifnet_savedaddr;
262 kvm_read(kvmfd, ifnet_addr, &ifnet, sizeof(ifnet));
263 kvm_read(kvmfd, (unsigned long) ifnet.if_name, tname, sizeof tname);
264 snprintf(tname, sizeof tname - 1, "%s%d", tname, ifnet.if_unit);
266 if (!strcmp(tname, iname))
268 if (!(ifnet.if_flags & IFF_UP))
275 ifinfo->rx_packets = ifnet.if_ipackets;
276 ifinfo->tx_packets = ifnet.if_opackets;
278 #if __FreeBSD_version >= 300001
279 ifaddr_addr = (u_long) ifnet.if_addrhead.tqh_first;
281 ifaddr_addr = (u_long) ifnet.if_addrlist;
286 kvm_read(kvmfd, ifaddr_addr, &ifaddr, sizeof(ifaddr));
287 kvm_read(kvmfd, (u_long)ifaddr.ifa_addr, &sa, sizeof(sa));
289 if (sa.sa_family != AF_INET)
291 #if __FreeBSD_version >= 300001
292 ifaddr_addr = (u_long) ifaddr.ifa_link.tqe_next;
294 ifaddr_addr = (u_long) ifaddr.ifa_next;
299 ifinfo->addr.s_addr = *(u_long *)(sa.sa_data + 2);
300 kvm_read(kvmfd, (u_long)ifaddr.ifa_dstaddr, &sa, sizeof(sa));
301 ifinfo->dstaddr.s_addr = *(u_long *)(sa.sa_data + 2);
302 kvm_read(kvmfd, (u_long)ifaddr.ifa_netmask, &sa, sizeof(sa));
303 ifinfo->netmask.s_addr = *(u_long *)(sa.sa_data + 2);
317 #if __FreeBSD_version >= 300001
318 ifnet_addr = (u_long) ifnet.if_link.tqe_next;
320 ifnet_addr = (unsigned long) ifnet.if_next;
329 #endif /* defined __FreeBSD__ */
332 #ifndef HAVE_INET_ATON
334 * Note: This is not a true replacement for inet_aton(), as it won't
335 * do the right thing on "255.255.255.255" (which translates to -1 on
336 * most machines). Fortunately this code will be used only if you're
337 * on an older Linux that lacks a real implementation.
339 #ifdef HAVE_NETINET_IN_SYSTM_H
340 # include <sys/types.h>
341 # include <netinet/in_systm.h>
344 #include <netinet/in.h>
345 #include <netinet/ip.h>
346 #include <arpa/inet.h>
349 static int inet_aton(const char *cp, struct in_addr *inp) {
352 addr = inet_addr(cp);
353 if (addr == ((long) -1)) return 0;
355 memcpy(inp, &addr, sizeof(addr));
358 #endif /* HAVE_INET_ATON */
360 void interface_parse(char *buf, struct hostdata *hp)
361 /* parse 'interface' specification */
365 hp->interface = xstrdup(buf);
367 /* find and isolate just the IP address */
368 if (!(cp1 = strchr(buf, '/')))
370 (void) report(stderr,
371 _("missing IP interface address\n"));
376 /* find and isolate just the netmask */
377 if (!(cp2 = strchr(cp1, '/')))
378 cp2 = "255.255.255.255";
382 /* convert IP address and netmask */
383 hp->interface_pair = (struct interface_pair_s *)xmalloc(sizeof(struct interface_pair_s));
384 if (!inet_aton(cp1, &hp->interface_pair->interface_address))
386 (void) report(stderr,
387 _("invalid IP interface address\n"));
390 if (!inet_aton(cp2, &hp->interface_pair->interface_mask))
392 (void) report(stderr,
393 _("invalid IP interface mask\n"));
396 /* apply the mask now to the IP address (range) required */
397 hp->interface_pair->interface_address.s_addr &=
398 hp->interface_pair->interface_mask.s_addr;
400 /* restore original interface string (for configuration dumper) */
405 void interface_note_activity(struct hostdata *hp)
406 /* save interface I/O counts */
411 /* if not monitoring link, all done */
415 /* get the current I/O stats for the monitored link */
416 if (get_ifinfo(hp->monitor, &ifinfo))
417 /* update this and preceeding host entries using the link
418 (they were already set during this pass but the I/O
419 count has now changed and they need to be re-updated)
421 for (ctl = querylist; ctl; ctl = ctl->next) {
422 if (ctl->server.monitor && !strcmp(hp->monitor, ctl->server.monitor))
423 ctl->server.monitor_io =
424 ifinfo.rx_packets + ifinfo.tx_packets;
425 /* do NOT update host entries following this one */
426 if (&ctl->server == hp)
430 #ifdef ACTIVITY_DEBUG
431 (void) report(stdout,
432 _("activity on %s -noted- as %d\n"),
433 hp->monitor, hp->monitor_io);
437 int interface_approve(struct hostdata *hp)
438 /* return TRUE if OK to poll, FALSE otherwise */
442 /* check interface IP address (range), if specified */
444 /* get interface info */
445 if (!get_ifinfo(hp->interface, &ifinfo)) {
446 (void) report(stdout,
447 _("skipping poll of %s, %s down\n"),
448 hp->pollname, hp->interface);
451 /* check the IP address (range) */
452 if ((ifinfo.addr.s_addr &
453 hp->interface_pair->interface_mask.s_addr) !=
454 hp->interface_pair->interface_address.s_addr) {
455 (void) report(stdout,
456 _("skipping poll of %s, %s IP address excluded\n"),
457 hp->pollname, hp->interface);
462 /* if not monitoring link, all done */
466 #ifdef ACTIVITY_DEBUG
467 (void) report(stdout,
468 _("activity on %s checked as %d\n"),
469 hp->monitor, hp->monitor_io);
471 /* if monitoring, check link for activity if it is up */
472 if (get_ifinfo(hp->monitor, &ifinfo))
474 int diff = (ifinfo.rx_packets + ifinfo.tx_packets)
478 * There are three cases here:
480 * (a) If the new packet count is less than the recorded one,
481 * probably pppd was restarted while fetchmail was running.
484 * (b) newpacket count is greater than the old packet count,
485 * but the difference is small and may just reflect the overhead
486 * of a link shutdown. Skip.
488 * (c) newpacket count is greater than the old packet count,
489 * and the difference is large. Connection is live. Don't skip.
491 if (diff >= 0 && diff <= MONITOR_SLOP)
493 (void) report(stdout,
494 _("skipping poll of %s, %s inactive\n"),
495 hp->pollname, hp->monitor);
500 #ifdef ACTIVITY_DEBUG
501 report(stdout, _("activity on %s was %d, is %d\n"),
502 hp->monitor, hp->monitor_io,
503 ifinfo.rx_packets + ifinfo.tx_packets);
508 #endif /* (defined(linux) && !defined(INET6)) || defined(__FreeBSD__) */