#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: bootpgw.c,v 1.16 2017/05/04 16:26:09 sevan Exp $");
#endif
#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/poll.h>
#include <net/if.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#ifndef NO_UNISTD
#include <unistd.h>
#endif
#include <stdlib.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <errno.h>
#include <ctype.h>
#include <netdb.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
__dead static void usage(void);
static void handle_reply(void);
static void handle_request(void);
u_short bootps_port, bootpc_port;
struct sockaddr_in bind_addr;
struct sockaddr_in clnt_addr;
struct sockaddr_in serv_addr;
int debug = 0;
int actualtimeout = 15 * 60000;
u_int maxhops = 4;
u_int minwait = 3;
int s;
char *pktbuf;
int pktlen;
char *progname;
char *servername;
char myhostname[MAXHOSTNAMELEN + 1];
struct in_addr my_ip_addr;
int
main(int argc, char **argv)
{
int timeout;
struct bootp *bp;
struct servent *servp;
struct hostent *hep;
char *stmp;
socklen_t ba_len, ra_len;
int n;
int nfound;
struct pollfd set[1];
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;
gethostname(myhostname, sizeof(myhostname));
myhostname[sizeof(myhostname) - 1] = '\0';
hep = gethostbyname(myhostname);
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 '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)) {
fprintf(stderr,
"%s: invalid debug level\n", progname);
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))
{
fprintf(stderr,
"bootpgw: invalid hop count limit\n");
break;
}
maxhops = (u_int)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)) {
fprintf(stderr,
"%s: invalid timeout specification\n", progname);
break;
}
actualtimeout = n * 60000;
timeout = (n > 0) ? actualtimeout : INFTIM;
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))
{
fprintf(stderr,
"bootpgw: invalid wait time\n");
break;
}
minwait = (u_int)n;
break;
default:
fprintf(stderr, "%s: unknown switch: -%c\n",
progname, argv[0][1]);
usage();
break;
}
}
servername = argv[0];
if (!servername) {
fprintf(stderr, "bootpgw: missing server name\n");
usage();
}
if (inet_aton(servername, &serv_addr.sin_addr) == 0) {
hep = gethostbyname(servername);
if (!hep) {
fprintf(stderr, "bootpgw: can't get addr for %s\n", servername);
exit(1);
}
memcpy(&serv_addr.sin_addr, hep->h_addr,
sizeof(serv_addr.sin_addr));
}
if (standalone) {
if (debug < 3) {
if (fork())
exit(0);
#ifdef NO_SETSID
setpgrp(0,0);
#ifdef TIOCNOTTY
n = open("/dev/tty", O_RDWR);
if (n >= 0) {
ioctl(n, TIOCNOTTY, (char *) 0);
(void) close(n);
}
#endif
#else
if (setsid() < 0)
perror("setsid");
#endif
}
timeout = INFTIM;
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,
"udp/bootps: unknown service -- assuming 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,
"udp/bootpc: unknown service -- assuming port %d",
IPPORT_BOOTPC);
bootpc_port = (u_short) IPPORT_BOOTPC;
}
set[0].fd = s;
set[0].events = POLLIN;
for (;;) {
nfound = poll(set, 1, timeout);
if (nfound < 0) {
if (errno != EINTR) {
report(LOG_ERR, "poll: %s", get_errmsg());
}
continue;
}
if (nfound == 0) {
report(LOG_INFO, "exiting after %d minute%s of inactivity",
actualtimeout / 60000,
actualtimeout == 60000 ? "" : "s");
exit(0);
}
ra_len = sizeof(clnt_addr);
n = recvfrom(s, pktbuf, MAX_MSG_SIZE, 0,
(struct sockaddr *) &clnt_addr, &ra_len);
if (n <= 0) {
continue;
}
if (debug > 3) {
report(LOG_INFO, "recvd pkt from IP addr %s",
inet_ntoa(clnt_addr.sin_addr));
}
if (n < (int)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;
}
}
}
static void
usage(void)
{
fprintf(stderr,
"usage: bootpgw [-d level] [-i] [-s] [-t timeout] server\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(void)
{
struct bootp *bp = (struct bootp *) pktbuf;
#if 0
struct ifreq *ifr;
#endif
u_short secs, hops;
if (debug) {
report(LOG_INFO, "request from %s",
inet_ntoa(clnt_addr.sin_addr));
}
secs = ntohs(bp->bp_secs);
if (secs < minwait)
return;
hops = ntohs(bp->bp_hops);
if (++hops > maxhops) {
report(LOG_NOTICE, "request from %s reached hop limit",
inet_ntoa(clnt_addr.sin_addr));
return;
}
bp->bp_hops = htons(hops);
if (bp->bp_giaddr.s_addr == 0) {
#if 0
struct sockaddr_in *sip;
ifr = getif(s, &clnt_addr.sin_addr);
if (!ifr) {
report(LOG_NOTICE, "no interface for request from %s",
inet_ntoa(clnt_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
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(bootps_port);
if (sendto(s, pktbuf, pktlen, 0,
(struct sockaddr *) &serv_addr,
sizeof(serv_addr)) < 0)
{
report(LOG_ERR, "sendto: %s", get_network_errmsg());
}
}
static void
handle_reply(void)
{
struct bootp *bp = (struct bootp *) pktbuf;
struct ifreq *ifr;
struct sockaddr_in *sip;
u_char canon_haddr[MAXHADDRLEN];
unsigned char *ha;
int len;
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
clnt_addr.sin_family = AF_INET;
clnt_addr.sin_addr = bp->bp_yiaddr;
clnt_addr.sin_port = htons(bootpc_port);
ha = bp->bp_chaddr;
len = bp->bp_hlen;
if (len > MAXHADDRLEN)
len = MAXHADDRLEN;
if (bp->bp_htype == HTYPE_IEEE802) {
haddr_conv802(ha, canon_haddr, len);
ha = canon_haddr;
}
if (debug > 1)
report(LOG_INFO, "setarp %s - %s",
inet_ntoa(bp->bp_yiaddr), haddrtoa(ha, len));
setarp(s, &bp->bp_yiaddr, ha, len);
if (sendto(s, pktbuf, pktlen, 0,
(struct sockaddr *) &clnt_addr,
sizeof(clnt_addr)) < 0)
{
report(LOG_ERR, "sendto: %s", get_network_errmsg());
}
}