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;
63 * Count of packets to see on an interface before monitor considers it up.
64 * Needed because when pppd shuts down the link, the packet counts go up
65 * by two (one rx and one tx?, maybe). A value of 2 seems to do the trick.
67 #define MONITOR_SLOP 2
69 void interface_init(void)
70 /* figure out which /proc/dev/net format to use */
72 FILE *fp = popen("uname -r", "r"); /* still wins if /proc is out */
74 /* pre-linux-2.2 format -- transmit packet count in 8th field */
75 netdevfmt = "%d %d %*d %*d %*d %d %*d %d %*d %*d %*d %*d %d";
83 if (fscanf(fp, "%d.%d.%*d", &major, &minor) != 2)
86 if (major >= 2 && minor >= 2)
87 /* Linux 2.2 -- transmit packet count in 10th field */
88 netdevfmt = "%d %d %*d %*d %*d %d %*d %*d %*d %d %*d %*d %d";
92 static int _get_ifinfo_(int socket_fd, FILE *stats_file, const char *ifname,
94 /* get active network interface information - return non-zero upon success */
96 int namelen = strlen(ifname);
98 char *cp, buffer[256];
102 /* initialize result */
103 memset((char *) ifinfo, 0, sizeof(ifinfo_t));
105 /* get the packet I/O counts */
106 while (fgets(buffer, sizeof(buffer) - 1, stats_file)) {
107 for (cp = buffer; *cp && *cp == ' '; ++cp);
108 if (!strncmp(cp, ifname, namelen) &&
109 cp[namelen] == ':') {
111 if (sscanf(cp, netdevfmt,
112 counts, counts+1, counts+2,
113 counts+3,&found)>4) { /* found = dummy */
114 /* newer kernel with byte counts */
115 ifinfo->rx_packets=counts[1];
116 ifinfo->tx_packets=counts[3];
118 /* older kernel, no byte counts */
119 ifinfo->rx_packets=counts[0];
120 ifinfo->tx_packets=counts[2];
125 if (!found) return (FALSE);
127 /* see if the interface is up */
128 strcpy(request.ifr_name, ifname);
129 if (ioctl(socket_fd, SIOCGIFFLAGS, &request) < 0)
131 if (!(request.ifr_flags & IFF_RUNNING))
134 /* get the IP address */
135 strcpy(request.ifr_name, ifname);
136 if (ioctl(socket_fd, SIOCGIFADDR, &request) < 0)
138 ifinfo->addr = ((struct sockaddr_in *) (&request.ifr_addr))->sin_addr;
140 /* get the PPP destination IP address */
141 strcpy(request.ifr_name, ifname);
142 if (ioctl(socket_fd, SIOCGIFDSTADDR, &request) >= 0)
143 ifinfo->dstaddr = ((struct sockaddr_in *)
144 (&request.ifr_dstaddr))->sin_addr;
146 /* get the netmask */
147 strcpy(request.ifr_name, ifname);
148 if (ioctl(socket_fd, SIOCGIFNETMASK, &request) >= 0) {
149 ifinfo->netmask = ((struct sockaddr_in *)
150 (&request.ifr_netmask))->sin_addr;
157 static int get_ifinfo(const char *ifname, ifinfo_t *ifinfo)
159 int socket_fd = socket(AF_INET, SOCK_DGRAM, 0);
160 FILE *stats_file = fopen("/proc/net/dev", "r");
163 if (socket_fd < 0 || !stats_file)
167 char *sp = strchr(ifname, '/');
171 result = _get_ifinfo_(socket_fd, stats_file, ifname, ifinfo);
182 #elif defined __FreeBSD__
185 static struct nlist symbols[] =
190 static u_long ifnet_savedaddr;
191 static gid_t if_rgid;
192 static gid_t if_egid;
195 interface_set_gids(gid_t egid, gid_t rgid)
204 if ((kvmfd = kvm_open(NULL, NULL, NULL, O_RDONLY, NULL)) == NULL)
207 if (kvm_nlist(kvmfd, symbols) < 0)
210 if (kvm_read(kvmfd, (unsigned long) symbols[0].n_value, &ifnet_savedaddr, sizeof(unsigned long)) == -1)
217 get_ifinfo(const char *ifname, ifinfo_t *ifinfo)
222 unsigned long ifnet_addr = ifnet_savedaddr;
223 #if __FreeBSD_version >= 300001
224 struct ifnethead ifnethead;
225 struct ifaddrhead ifaddrhead;
227 struct ifaddr ifaddr;
228 unsigned long ifaddr_addr;
230 unsigned long sa_addr;
236 for (i = 0; ifname[i] && ifname[i] != '/'; i++)
237 iname[i] = ifname[i];
245 report(stderr, 0, _("Unable to open kvm interface. Make sure fetchmail is SGID kmem."));
252 #if __FreeBSD_version >= 300001
253 kvm_read(kvmfd, ifnet_savedaddr, (char *) &ifnethead, sizeof ifnethead);
254 ifnet_addr = (u_long) ifnethead.tqh_first;
256 ifnet_addr = ifnet_savedaddr;
261 kvm_read(kvmfd, ifnet_addr, &ifnet, sizeof(ifnet));
262 kvm_read(kvmfd, (unsigned long) ifnet.if_name, tname, sizeof tname);
263 snprintf(tname, sizeof tname - 1, "%s%d", tname, ifnet.if_unit);
265 if (!strcmp(tname, iname))
267 if (!(ifnet.if_flags & IFF_UP))
274 ifinfo->rx_packets = ifnet.if_ipackets;
275 ifinfo->tx_packets = ifnet.if_opackets;
277 #if __FreeBSD_version >= 300001
278 ifaddr_addr = (u_long) ifnet.if_addrhead.tqh_first;
280 ifaddr_addr = (u_long) ifnet.if_addrlist;
285 kvm_read(kvmfd, ifaddr_addr, &ifaddr, sizeof(ifaddr));
286 kvm_read(kvmfd, (u_long)ifaddr.ifa_addr, &sa, sizeof(sa));
288 if (sa.sa_family != AF_INET)
290 #if __FreeBSD_version >= 300001
291 ifaddr_addr = (u_long) ifaddr.ifa_link.tqe_next;
293 ifaddr_addr = (u_long) ifaddr.ifa_next;
298 ifinfo->addr.s_addr = *(u_long *)(sa.sa_data + 2);
299 kvm_read(kvmfd, (u_long)ifaddr.ifa_dstaddr, &sa, sizeof(sa));
300 ifinfo->dstaddr.s_addr = *(u_long *)(sa.sa_data + 2);
301 kvm_read(kvmfd, (u_long)ifaddr.ifa_netmask, &sa, sizeof(sa));
302 ifinfo->netmask.s_addr = *(u_long *)(sa.sa_data + 2);
316 #if __FreeBSD_version >= 300001
317 ifnet_addr = (u_long) ifnet.if_link.tqe_next;
319 ifnet_addr = (unsigned long) ifnet.if_next;
328 #endif /* defined __FreeBSD__ */
331 #ifndef HAVE_INET_ATON
333 * Note: This is not a true replacement for inet_aton(), as it won't
334 * do the right thing on "255.255.255.255" (which translates to -1 on
335 * most machines). Fortunately this code will be used only if you're
336 * on an older Linux that lacks a real implementation.
338 #ifdef HAVE_NETINET_IN_SYSTM_H
339 # include <sys/types.h>
340 # include <netinet/in_systm.h>
343 #include <netinet/in.h>
344 #include <netinet/ip.h>
345 #include <arpa/inet.h>
348 static int inet_aton(const char *cp, struct in_addr *inp) {
351 addr = inet_addr(cp);
352 if (addr == ((long) -1)) return 0;
354 memcpy(inp, &addr, sizeof(addr));
357 #endif /* HAVE_INET_ATON */
359 void interface_parse(char *buf, struct hostdata *hp)
360 /* parse 'interface' specification */
364 hp->interface = xstrdup(buf);
366 /* find and isolate just the IP address */
367 if (!(cp1 = strchr(buf, '/')))
369 (void) report(stderr,
370 _("missing IP interface address\n"));
375 /* find and isolate just the netmask */
376 if (!(cp2 = strchr(cp1, '/')))
377 cp2 = "255.255.255.255";
381 /* convert IP address and netmask */
382 hp->interface_pair = (struct interface_pair_s *)xmalloc(sizeof(struct interface_pair_s));
383 if (!inet_aton(cp1, &hp->interface_pair->interface_address))
385 (void) report(stderr,
386 _("invalid IP interface address\n"));
389 if (!inet_aton(cp2, &hp->interface_pair->interface_mask))
391 (void) report(stderr,
392 _("invalid IP interface mask\n"));
395 /* apply the mask now to the IP address (range) required */
396 hp->interface_pair->interface_address.s_addr &=
397 hp->interface_pair->interface_mask.s_addr;
399 /* restore original interface string (for configuration dumper) */
404 void interface_note_activity(struct hostdata *hp)
405 /* save interface I/O counts */
410 /* if not monitoring link, all done */
414 /* get the current I/O stats for the monitored link */
415 if (get_ifinfo(hp->monitor, &ifinfo))
416 /* update this and preceeding host entries using the link
417 (they were already set during this pass but the I/O
418 count has now changed and they need to be re-updated)
420 for (ctl = querylist; ctl; ctl = ctl->next) {
421 if (ctl->server.monitor && !strcmp(hp->monitor, ctl->server.monitor))
422 ctl->server.monitor_io =
423 ifinfo.rx_packets + ifinfo.tx_packets;
424 /* do NOT update host entries following this one */
425 if (&ctl->server == hp)
429 #ifdef ACTIVITY_DEBUG
430 (void) report(stdout,
431 _("activity on %s -noted- as %d\n"),
432 hp->monitor, hp->monitor_io);
436 int interface_approve(struct hostdata *hp)
437 /* return TRUE if OK to poll, FALSE otherwise */
441 /* check interface IP address (range), if specified */
443 /* get interface info */
444 if (!get_ifinfo(hp->interface, &ifinfo)) {
445 (void) report(stdout,
446 _("skipping poll of %s, %s down\n"),
447 hp->pollname, hp->interface);
450 /* check the IP address (range) */
451 if ((ifinfo.addr.s_addr &
452 hp->interface_pair->interface_mask.s_addr) !=
453 hp->interface_pair->interface_address.s_addr) {
454 (void) report(stdout,
455 _("skipping poll of %s, %s IP address excluded\n"),
456 hp->pollname, hp->interface);
461 /* if not monitoring link, all done */
465 #ifdef ACTIVITY_DEBUG
466 (void) report(stdout,
467 _("activity on %s checked as %d\n"),
468 hp->monitor, hp->monitor_io);
470 /* if monitoring, check link for activity if it is up */
471 if (get_ifinfo(hp->monitor, &ifinfo) &&
472 hp->monitor_io - (ifinfo.rx_packets +
473 ifinfo.tx_packets) >= -MONITOR_SLOP) {
474 (void) report(stdout,
475 _("skipping poll of %s, %s inactive\n"),
476 hp->pollname, hp->monitor);
480 #ifdef ACTIVITY_DEBUG
481 report(stdout _("activity on %s was %d, is %d"),
482 hp->monitor, hp->monitor_io,
483 ifinfo.rx_packets + ifinfo.tx_packets);
488 #endif /* (defined(linux) && !defined(INET6)) || defined(__FreeBSD__) */