#include <sys/types.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/file.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <sys/utsname.h>
#include <net/if.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#ifndef NO_UNISTD
#include <unistd.h>
#endif
#include <err.h>
#include <stdlib.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include <netdb.h>
#include <paths.h>
#include <syslog.h>
#include <assert.h>
#ifdef NO_SETSID
# include <fcntl.h>
#endif
#include "bootp.h"
#include "getif.h"
#include "hwaddr.h"
#include "report.h"
#include "patchlevel.h"
#define MAX_MSG_SIZE (3*512)
#define TRUE 1
#define FALSE 0
#define get_network_errmsg get_errmsg
static void usage(void) __dead2;
static void handle_reply(void);
static void handle_request(void);
u_short bootps_port, bootpc_port;
struct sockaddr_in bind_addr;
struct sockaddr_in recv_addr;
struct sockaddr_in send_addr;
int debug = 0;
struct timeval actualtimeout =
{
15 * 60L,
0
};
u_char maxhops = 4;
u_int minwait = 3;
int arpmod = TRUE;
int s;
char *pktbuf;
int pktlen;
char *progname;
char *servername;
int32 server_ipa;
struct in_addr my_ip_addr;
struct utsname my_uname;
char *hostname;
int
main(int argc, char **argv)
{
struct timeval *timeout;
struct bootp *bp;
struct servent *servp;
struct hostent *hep;
char *stmp;
int n, ba_len, ra_len;
int nfound, readfds;
int standalone;
progname = strrchr(argv[0], '/');
if (progname) progname++;
else progname = argv[0];
report_init(0);
report(LOG_INFO, "version %s.%d", VERSION, PATCHLEVEL);
assert(sizeof(struct bootp) == BP_MINPKTSZ);
pktbuf = malloc(MAX_MSG_SIZE);
if (!pktbuf) {
report(LOG_ERR, "malloc failed");
exit(1);
}
bp = (struct bootp *) pktbuf;
s = 0;
ba_len = sizeof(bind_addr);
bzero((char *) &bind_addr, ba_len);
errno = 0;
standalone = TRUE;
if (getsockname(s, (struct sockaddr *) &bind_addr, &ba_len) == 0) {
if (bind_addr.sin_family == AF_INET) {
standalone = FALSE;
bootps_port = ntohs(bind_addr.sin_port);
} else {
report(LOG_INFO, "getsockname: not an INET socket");
}
}
stmp = NULL;
timeout = &actualtimeout;
if (uname(&my_uname) < 0)
errx(1, "can't get hostname");
hostname = my_uname.nodename;
hep = gethostbyname(hostname);
if (!hep) {
printf("Can not get my IP address\n");
exit(1);
}
bcopy(hep->h_addr, (char *)&my_ip_addr, sizeof(my_ip_addr));
for (argc--, argv++; argc > 0; argc--, argv++) {
if (argv[0][0] != '-')
break;
switch (argv[0][1]) {
case 'a':
arpmod = FALSE;
break;
case 'd':
if (argv[0][2]) {
stmp = &(argv[0][2]);
} else if (argv[1] && argv[1][0] == '-') {
debug++;
break;
} else {
argc--;
argv++;
stmp = argv[0];
}
if (!stmp || (sscanf(stmp, "%d", &n) != 1) || (n < 0)) {
warnx("invalid debug level");
break;
}
debug = n;
break;
case 'h':
if (argv[0][2]) {
stmp = &(argv[0][2]);
} else {
argc--;
argv++;
stmp = argv[0];
}
if (!stmp || (sscanf(stmp, "%d", &n) != 1) ||
(n < 0) || (n > 16))
{
warnx("invalid hop count limit");
break;
}
maxhops = (u_char)n;
break;
case 'i':
standalone = FALSE;
break;
case 's':
standalone = TRUE;
break;
case 't':
if (argv[0][2]) {
stmp = &(argv[0][2]);
} else {
argc--;
argv++;
stmp = argv[0];
}
if (!stmp || (sscanf(stmp, "%d", &n) != 1) || (n < 0)) {
warnx("invalid timeout specification");
break;
}
actualtimeout.tv_sec = (int32) (60 * n);
timeout = (n > 0) ? &actualtimeout : NULL;
break;
case 'w':
if (argv[0][2]) {
stmp = &(argv[0][2]);
} else {
argc--;
argv++;
stmp = argv[0];
}
if (!stmp || (sscanf(stmp, "%d", &n) != 1) ||
(n < 0) || (n > 60))
{
warnx("invalid wait time");
break;
}
minwait = (u_int)n;
break;
default:
warnx("unknown switch: -%c", argv[0][1]);
usage();
break;
}
}
servername = argv[0];
if (!servername) {
warnx("missing server name");
usage();
}
if (isdigit(servername[0]))
server_ipa = inet_addr(servername);
else {
hep = gethostbyname(servername);
if (!hep)
errx(1, "can't get addr for %s", servername);
bcopy(hep->h_addr, (char *)&server_ipa, sizeof(server_ipa));
}
if (standalone) {
if (debug < 3) {
if (fork())
exit(0);
#ifdef NO_SETSID
setpgrp(0,0);
#ifdef TIOCNOTTY
n = open(_PATH_TTY, O_RDWR);
if (n >= 0) {
ioctl(n, TIOCNOTTY, (char *) 0);
(void) close(n);
}
#endif
#else
if (setsid() < 0)
perror("setsid");
#endif
}
timeout = NULL;
if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
report(LOG_ERR, "socket: %s", get_network_errmsg());
exit(1);
}
servp = getservbyname("bootps", "udp");
if (servp) {
bootps_port = ntohs((u_short) servp->s_port);
} else {
bootps_port = (u_short) IPPORT_BOOTPS;
report(LOG_ERR,
"bootps/udp: unknown service -- using port %d",
bootps_port);
}
bind_addr.sin_family = AF_INET;
bind_addr.sin_port = htons(bootps_port);
bind_addr.sin_addr.s_addr = INADDR_ANY;
if (bind(s, (struct sockaddr *) &bind_addr,
sizeof(bind_addr)) < 0)
{
report(LOG_ERR, "bind: %s", get_network_errmsg());
exit(1);
}
}
servp = getservbyname("bootpc", "udp");
if (servp) {
bootpc_port = ntohs(servp->s_port);
} else {
report(LOG_ERR,
"bootpc/udp: unknown service -- using port %d",
IPPORT_BOOTPC);
bootpc_port = (u_short) IPPORT_BOOTPC;
}
for (;;) {
struct timeval tv;
readfds = 1 << s;
if (timeout)
tv = *timeout;
nfound = select(s + 1, (fd_set *)&readfds, NULL, NULL,
(timeout) ? &tv : NULL);
if (nfound < 0) {
if (errno != EINTR) {
report(LOG_ERR, "select: %s", get_errmsg());
}
continue;
}
if (!(readfds & (1 << s))) {
report(LOG_INFO, "exiting after %ld minutes of inactivity",
(long)(actualtimeout.tv_sec / 60));
exit(0);
}
ra_len = sizeof(recv_addr);
n = recvfrom(s, pktbuf, MAX_MSG_SIZE, 0,
(struct sockaddr *) &recv_addr, &ra_len);
if (n <= 0) {
continue;
}
if (debug > 3) {
report(LOG_INFO, "recvd pkt from IP addr %s",
inet_ntoa(recv_addr.sin_addr));
}
if (n < sizeof(struct bootp)) {
if (debug) {
report(LOG_INFO, "received short packet");
}
continue;
}
pktlen = n;
switch (bp->bp_op) {
case BOOTREQUEST:
handle_request();
break;
case BOOTREPLY:
handle_reply();
break;
}
}
return 0;
}
static void
usage()
{
fprintf(stderr,
"usage: bootpgw [-a] [-i | -s] [-d level] [-h count] [-t timeout]\n"
" [-w time] server\n");
fprintf(stderr, "\t -a\tdon't modify ARP table\n");
fprintf(stderr, "\t -d n\tset debug level\n");
fprintf(stderr, "\t -h n\tset max hop count\n");
fprintf(stderr, "\t -i\tforce inetd mode (run as child of inetd)\n");
fprintf(stderr, "\t -s\tforce standalone mode (run without inetd)\n");
fprintf(stderr, "\t -t n\tset inetd exit timeout to n minutes\n");
fprintf(stderr, "\t -w n\tset min wait time (secs)\n");
exit(1);
}
static void
handle_request()
{
struct bootp *bp = (struct bootp *) pktbuf;
u_short secs;
u_char hops;
if (debug) {
report(LOG_INFO, "request from %s",
inet_ntoa(recv_addr.sin_addr));
}
secs = ntohs(bp->bp_secs);
if (secs < minwait)
return;
hops = bp->bp_hops;
if (++hops > maxhops) {
report(LOG_NOTICE, "request from %s reached hop limit",
inet_ntoa(recv_addr.sin_addr));
return;
}
bp->bp_hops = hops;
if (bp->bp_giaddr.s_addr == 0) {
#if 0
struct sockaddr_in *sip;
struct ifreq *ifr;
ifr = getif(s, &recv_addr.sin_addr);
if (!ifr) {
report(LOG_NOTICE, "no interface for request from %s",
inet_ntoa(recv_addr.sin_addr));
return;
}
sip = (struct sockaddr_in *) &(ifr->ifr_addr);
bp->bp_giaddr = sip->sin_addr;
#else
bp->bp_giaddr = my_ip_addr;
#endif
}
send_addr.sin_family = AF_INET;
send_addr.sin_port = htons(bootps_port);
send_addr.sin_addr.s_addr = server_ipa;
if (sendto(s, pktbuf, pktlen, 0,
(struct sockaddr *) &send_addr,
sizeof(send_addr)) < 0)
{
report(LOG_ERR, "sendto: %s", get_network_errmsg());
}
}
static void
handle_reply()
{
struct bootp *bp = (struct bootp *) pktbuf;
struct ifreq *ifr;
struct sockaddr_in *sip;
unsigned char *ha;
int len, haf;
if (debug) {
report(LOG_INFO, " reply for %s",
inet_ntoa(bp->bp_yiaddr));
}
ifr = getif(s, &(bp->bp_yiaddr));
if (!ifr) {
report(LOG_NOTICE, "no interface for reply to %s",
inet_ntoa(bp->bp_yiaddr));
return;
}
#if 1
sip = (struct sockaddr_in *) &(ifr->ifr_addr);
bp->bp_giaddr = sip->sin_addr;
#endif
send_addr.sin_family = AF_INET;
send_addr.sin_addr = bp->bp_yiaddr;
send_addr.sin_port = htons(bootpc_port);
if (arpmod) {
ha = bp->bp_chaddr;
len = bp->bp_hlen;
struct in_addr dst;
if (len > MAXHADDRLEN)
len = MAXHADDRLEN;
haf = (int) bp->bp_htype;
if (haf == 0)
haf = HTYPE_ETHERNET;
if (debug > 1)
report(LOG_INFO, "setarp %s - %s",
inet_ntoa(dst), haddrtoa(ha, len));
setarp(s, &dst, haf, ha, len);
}
if (sendto(s, pktbuf, pktlen, 0,
(struct sockaddr *) &send_addr,
sizeof(send_addr)) < 0)
{
report(LOG_ERR, "sendto: %s", get_network_errmsg());
}
}