Home | History | Annotate | Line # | Download | only in bootpgw
bootpgw.c revision 1.5
      1  1.1       gwr /*
      2  1.1       gwr  * bootpgw.c - BOOTP GateWay
      3  1.1       gwr  * This program forwards BOOTP Request packets to a BOOTP server.
      4  1.1       gwr  */
      5  1.1       gwr 
      6  1.1       gwr /************************************************************************
      7  1.1       gwr           Copyright 1988, 1991 by Carnegie Mellon University
      8  1.1       gwr 
      9  1.1       gwr                           All Rights Reserved
     10  1.1       gwr 
     11  1.1       gwr Permission to use, copy, modify, and distribute this software and its
     12  1.1       gwr documentation for any purpose and without fee is hereby granted, provided
     13  1.1       gwr that the above copyright notice appear in all copies and that both that
     14  1.1       gwr copyright notice and this permission notice appear in supporting
     15  1.1       gwr documentation, and that the name of Carnegie Mellon University not be used
     16  1.1       gwr in advertising or publicity pertaining to distribution of the software
     17  1.1       gwr without specific, written prior permission.
     18  1.1       gwr 
     19  1.1       gwr CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
     20  1.1       gwr SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
     21  1.1       gwr IN NO EVENT SHALL CMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
     22  1.1       gwr DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
     23  1.1       gwr PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
     24  1.1       gwr ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
     25  1.1       gwr SOFTWARE.
     26  1.1       gwr ************************************************************************/
     27  1.1       gwr 
     28  1.1       gwr #ifndef lint
     29  1.5  christos static char rcsid[] = "$Id: bootpgw.c,v 1.5 1996/05/06 13:49:16 christos Exp $";
     30  1.1       gwr #endif
     31  1.1       gwr 
     32  1.1       gwr /*
     33  1.1       gwr  * BOOTPGW is typically used to forward BOOTP client requests from
     34  1.1       gwr  * one subnet to a BOOTP server on a different subnet.
     35  1.1       gwr  */
     36  1.1       gwr 
     37  1.1       gwr #include <sys/types.h>
     38  1.1       gwr #include <sys/param.h>
     39  1.1       gwr #include <sys/socket.h>
     40  1.1       gwr #include <sys/ioctl.h>
     41  1.1       gwr #include <sys/file.h>
     42  1.1       gwr #include <sys/time.h>
     43  1.1       gwr #include <sys/stat.h>
     44  1.1       gwr 
     45  1.1       gwr #include <net/if.h>
     46  1.1       gwr #include <netinet/in.h>
     47  1.1       gwr #include <arpa/inet.h>			/* inet_ntoa */
     48  1.1       gwr 
     49  1.1       gwr #ifndef	NO_UNISTD
     50  1.1       gwr #include <unistd.h>
     51  1.1       gwr #endif
     52  1.1       gwr #include <stdlib.h>
     53  1.1       gwr #include <signal.h>
     54  1.1       gwr #include <stdio.h>
     55  1.1       gwr #include <string.h>
     56  1.1       gwr #include <errno.h>
     57  1.1       gwr #include <ctype.h>
     58  1.1       gwr #include <netdb.h>
     59  1.1       gwr #include <syslog.h>
     60  1.1       gwr #include <assert.h>
     61  1.1       gwr 
     62  1.1       gwr #ifdef	NO_SETSID
     63  1.1       gwr # include <fcntl.h>		/* for O_RDONLY, etc */
     64  1.1       gwr #endif
     65  1.1       gwr 
     66  1.1       gwr #ifndef	USE_BFUNCS
     67  1.1       gwr # include <memory.h>
     68  1.1       gwr /* Yes, memcpy is OK here (no overlapped copies). */
     69  1.1       gwr # define bcopy(a,b,c)    memcpy(b,a,c)
     70  1.1       gwr # define bzero(p,l)      memset(p,0,l)
     71  1.1       gwr # define bcmp(a,b,c)     memcmp(a,b,c)
     72  1.1       gwr #endif
     73  1.1       gwr 
     74  1.1       gwr #include "bootp.h"
     75  1.1       gwr #include "getif.h"
     76  1.1       gwr #include "hwaddr.h"
     77  1.1       gwr #include "report.h"
     78  1.1       gwr #include "patchlevel.h"
     79  1.1       gwr 
     80  1.1       gwr /* Local definitions: */
     81  1.2       gwr #define MAX_MSG_SIZE			(3*512)	/* Maximum packet size */
     82  1.1       gwr #define TRUE 1
     83  1.1       gwr #define FALSE 0
     84  1.1       gwr #define get_network_errmsg get_errmsg
     85  1.1       gwr 
     86  1.1       gwr 
     88  1.1       gwr 
     89  1.1       gwr /*
     90  1.1       gwr  * Externals, forward declarations, and global variables
     91  1.1       gwr  */
     92  1.1       gwr 
     93  1.1       gwr #ifdef	__STDC__
     94  1.1       gwr #define P(args) args
     95  1.1       gwr #else
     96  1.1       gwr #define P(args) ()
     97  1.1       gwr #endif
     98  1.1       gwr 
     99  1.1       gwr static void usage P((void));
    100  1.1       gwr static void handle_reply P((void));
    101  1.1       gwr static void handle_request P((void));
    102  1.1       gwr 
    103  1.1       gwr #undef	P
    104  1.1       gwr 
    105  1.1       gwr /*
    106  1.1       gwr  * IP port numbers for client and server obtained from /etc/services
    107  1.1       gwr  */
    108  1.1       gwr 
    109  1.1       gwr u_short bootps_port, bootpc_port;
    110  1.1       gwr 
    111  1.1       gwr 
    112  1.1       gwr /*
    113  1.1       gwr  * Internet socket and interface config structures
    114  1.1       gwr  */
    115  1.1       gwr 
    116  1.4        ws struct sockaddr_in bind_addr;	/* Listening */
    117  1.4        ws struct sockaddr_in clnt_addr;	/* client address */
    118  1.1       gwr struct sockaddr_in serv_addr;	/* server address */
    119  1.1       gwr 
    120  1.1       gwr 
    121  1.1       gwr /*
    122  1.1       gwr  * option defaults
    123  1.1       gwr  */
    124  1.1       gwr int debug = 0;					/* Debugging flag (level) */
    125  1.1       gwr struct timeval actualtimeout =
    126  1.1       gwr {								/* fifteen minutes */
    127  1.1       gwr 	15 * 60L,					/* tv_sec */
    128  1.1       gwr 	0							/* tv_usec */
    129  1.1       gwr };
    130  1.1       gwr u_int maxhops = 4;				/* Number of hops allowed for requests. */
    131  1.1       gwr u_int minwait = 3;				/* Number of seconds client must wait before
    132  1.1       gwr 						   its bootrequest packets are forwarded. */
    133  1.1       gwr 
    134  1.1       gwr /*
    135  1.1       gwr  * General
    136  1.1       gwr  */
    137  1.1       gwr 
    138  1.1       gwr int s;							/* Socket file descriptor */
    139  1.1       gwr char *pktbuf;					/* Receive packet buffer */
    140  1.1       gwr int pktlen;
    141  1.1       gwr char *progname;
    142  1.1       gwr char *servername;
    143  1.1       gwr 
    144  1.1       gwr char myhostname[64];
    145  1.1       gwr struct in_addr my_ip_addr;
    146  1.1       gwr 
    147  1.1       gwr 
    149  1.1       gwr 
    150  1.1       gwr 
    151  1.1       gwr /*
    152  1.1       gwr  * Initialization such as command-line processing is done and then the
    153  1.1       gwr  * main server loop is started.
    154  1.5  christos  */
    155  1.1       gwr 
    156  1.1       gwr int
    157  1.1       gwr main(argc, argv)
    158  1.1       gwr 	int argc;
    159  1.1       gwr 	char **argv;
    160  1.1       gwr {
    161  1.1       gwr 	struct timeval *timeout;
    162  1.1       gwr 	struct bootp *bp;
    163  1.1       gwr 	struct servent *servp;
    164  1.1       gwr 	struct hostent *hep;
    165  1.1       gwr 	char *stmp;
    166  1.1       gwr 	int n, ba_len, ra_len;
    167  1.1       gwr 	int nfound, readfds;
    168  1.1       gwr 	int standalone;
    169  1.1       gwr 
    170  1.1       gwr 	progname = strrchr(argv[0], '/');
    171  1.1       gwr 	if (progname) progname++;
    172  1.1       gwr 	else progname = argv[0];
    173  1.1       gwr 
    174  1.1       gwr 	/*
    175  1.1       gwr 	 * Initialize logging.
    176  1.1       gwr 	 */
    177  1.1       gwr 	report_init(0);				/* uses progname */
    178  1.1       gwr 
    179  1.1       gwr 	/*
    180  1.1       gwr 	 * Log startup
    181  1.1       gwr 	 */
    182  1.1       gwr 	report(LOG_INFO, "version %s.%d", VERSION, PATCHLEVEL);
    183  1.1       gwr 
    184  1.1       gwr 	/* Debugging for compilers with struct padding. */
    185  1.1       gwr 	assert(sizeof(struct bootp) == BP_MINPKTSZ);
    186  1.2       gwr 
    187  1.1       gwr 	/* Get space for receiving packets and composing replies. */
    188  1.1       gwr 	pktbuf = malloc(MAX_MSG_SIZE);
    189  1.1       gwr 	if (!pktbuf) {
    190  1.1       gwr 		report(LOG_ERR, "malloc failed");
    191  1.1       gwr 		exit(1);
    192  1.1       gwr 	}
    193  1.1       gwr 	bp = (struct bootp *) pktbuf;
    194  1.1       gwr 
    195  1.1       gwr 	/*
    196  1.1       gwr 	 * Check to see if a socket was passed to us from inetd.
    197  1.1       gwr 	 *
    198  1.1       gwr 	 * Use getsockname() to determine if descriptor 0 is indeed a socket
    199  1.1       gwr 	 * (and thus we are probably a child of inetd) or if it is instead
    200  1.1       gwr 	 * something else and we are running standalone.
    201  1.1       gwr 	 */
    202  1.1       gwr 	s = 0;
    203  1.1       gwr 	ba_len = sizeof(bind_addr);
    204  1.1       gwr 	bzero((char *) &bind_addr, ba_len);
    205  1.1       gwr 	errno = 0;
    206  1.1       gwr 	standalone = TRUE;
    207  1.1       gwr 	if (getsockname(s, (struct sockaddr *) &bind_addr, &ba_len) == 0) {
    208  1.1       gwr 		/*
    209  1.1       gwr 		 * Descriptor 0 is a socket.  Assume we are a child of inetd.
    210  1.1       gwr 		 */
    211  1.1       gwr 		if (bind_addr.sin_family == AF_INET) {
    212  1.1       gwr 			standalone = FALSE;
    213  1.1       gwr 			bootps_port = ntohs(bind_addr.sin_port);
    214  1.1       gwr 		} else {
    215  1.1       gwr 			/* Some other type of socket? */
    216  1.1       gwr 			report(LOG_INFO, "getsockname: not an INET socket");
    217  1.1       gwr 		}
    218  1.1       gwr 	}
    219  1.1       gwr 	/*
    220  1.1       gwr 	 * Set defaults that might be changed by option switches.
    221  1.1       gwr 	 */
    222  1.1       gwr 	stmp = NULL;
    223  1.1       gwr 	timeout = &actualtimeout;
    224  1.1       gwr 	gethostname(myhostname, sizeof(myhostname));
    225  1.1       gwr 	hep = gethostbyname(myhostname);
    226  1.1       gwr 	if (!hep) {
    227  1.1       gwr 		printf("Can not get my IP address\n");
    228  1.1       gwr 		exit(1);
    229  1.1       gwr 	}
    230  1.1       gwr 	bcopy(hep->h_addr, (char *)&my_ip_addr, sizeof(my_ip_addr));
    231  1.1       gwr 
    232  1.1       gwr 	/*
    233  1.1       gwr 	 * Read switches.
    234  1.1       gwr 	 */
    235  1.1       gwr 	for (argc--, argv++; argc > 0; argc--, argv++) {
    236  1.1       gwr 		if (argv[0][0] != '-')
    237  1.1       gwr 			break;
    238  1.1       gwr 		switch (argv[0][1]) {
    239  1.1       gwr 
    240  1.1       gwr 		case 'd':				/* debug level */
    241  1.1       gwr 			if (argv[0][2]) {
    242  1.1       gwr 				stmp = &(argv[0][2]);
    243  1.1       gwr 			} else if (argv[1] && argv[1][0] == '-') {
    244  1.1       gwr 				/*
    245  1.1       gwr 				 * Backwards-compatible behavior:
    246  1.1       gwr 				 * no parameter, so just increment the debug flag.
    247  1.1       gwr 				 */
    248  1.1       gwr 				debug++;
    249  1.1       gwr 				break;
    250  1.1       gwr 			} else {
    251  1.1       gwr 				argc--;
    252  1.1       gwr 				argv++;
    253  1.1       gwr 				stmp = argv[0];
    254  1.1       gwr 			}
    255  1.1       gwr 			if (!stmp || (sscanf(stmp, "%d", &n) != 1) || (n < 0)) {
    256  1.1       gwr 				fprintf(stderr,
    257  1.1       gwr 						"%s: invalid debug level\n", progname);
    258  1.1       gwr 				break;
    259  1.1       gwr 			}
    260  1.1       gwr 			debug = n;
    261  1.1       gwr 			break;
    262  1.1       gwr 
    263  1.1       gwr 		case 'h':				/* hop count limit */
    264  1.1       gwr 			if (argv[0][2]) {
    265  1.1       gwr 				stmp = &(argv[0][2]);
    266  1.1       gwr 			} else {
    267  1.1       gwr 				argc--;
    268  1.1       gwr 				argv++;
    269  1.1       gwr 				stmp = argv[0];
    270  1.1       gwr 			}
    271  1.1       gwr 			if (!stmp || (sscanf(stmp, "%d", &n) != 1) ||
    272  1.1       gwr 				(n < 0) || (n > 16))
    273  1.1       gwr 			{
    274  1.1       gwr 				fprintf(stderr,
    275  1.1       gwr 						"bootpgw: invalid hop count limit\n");
    276  1.1       gwr 				break;
    277  1.1       gwr 			}
    278  1.1       gwr 			maxhops = (u_int)n;
    279  1.1       gwr 			break;
    280  1.1       gwr 
    281  1.1       gwr 		case 'i':				/* inetd mode */
    282  1.1       gwr 			standalone = FALSE;
    283  1.1       gwr 			break;
    284  1.1       gwr 
    285  1.1       gwr 		case 's':				/* standalone mode */
    286  1.1       gwr 			standalone = TRUE;
    287  1.1       gwr 			break;
    288  1.1       gwr 
    289  1.1       gwr 		case 't':				/* timeout */
    290  1.1       gwr 			if (argv[0][2]) {
    291  1.1       gwr 				stmp = &(argv[0][2]);
    292  1.1       gwr 			} else {
    293  1.1       gwr 				argc--;
    294  1.1       gwr 				argv++;
    295  1.1       gwr 				stmp = argv[0];
    296  1.1       gwr 			}
    297  1.1       gwr 			if (!stmp || (sscanf(stmp, "%d", &n) != 1) || (n < 0)) {
    298  1.1       gwr 				fprintf(stderr,
    299  1.1       gwr 						"%s: invalid timeout specification\n", progname);
    300  1.1       gwr 				break;
    301  1.1       gwr 			}
    302  1.1       gwr 			actualtimeout.tv_sec = (int32) (60 * n);
    303  1.1       gwr 			/*
    304  1.1       gwr 			 * If the actual timeout is zero, pass a NULL pointer
    305  1.1       gwr 			 * to select so it blocks indefinitely, otherwise,
    306  1.1       gwr 			 * point to the actual timeout value.
    307  1.1       gwr 			 */
    308  1.1       gwr 			timeout = (n > 0) ? &actualtimeout : NULL;
    309  1.1       gwr 			break;
    310  1.1       gwr 
    311  1.1       gwr 		case 'w':				/* wait time */
    312  1.1       gwr 			if (argv[0][2]) {
    313  1.1       gwr 				stmp = &(argv[0][2]);
    314  1.1       gwr 			} else {
    315  1.1       gwr 				argc--;
    316  1.1       gwr 				argv++;
    317  1.1       gwr 				stmp = argv[0];
    318  1.1       gwr 			}
    319  1.1       gwr 			if (!stmp || (sscanf(stmp, "%d", &n) != 1) ||
    320  1.1       gwr 				(n < 0) || (n > 60))
    321  1.1       gwr 			{
    322  1.1       gwr 				fprintf(stderr,
    323  1.1       gwr 						"bootpgw: invalid wait time\n");
    324  1.1       gwr 				break;
    325  1.1       gwr 			}
    326  1.1       gwr 			minwait = (u_int)n;
    327  1.1       gwr 			break;
    328  1.1       gwr 
    329  1.1       gwr 		default:
    330  1.1       gwr 			fprintf(stderr, "%s: unknown switch: -%c\n",
    331  1.1       gwr 					progname, argv[0][1]);
    332  1.1       gwr 			usage();
    333  1.1       gwr 			break;
    334  1.1       gwr 
    335  1.1       gwr 		} /* switch */
    336  1.1       gwr 	} /* for args */
    337  1.1       gwr 
    338  1.1       gwr 	/* Make sure server name argument is suplied. */
    339  1.1       gwr 	servername = argv[0];
    340  1.1       gwr 	if (!servername) {
    341  1.1       gwr 		fprintf(stderr, "bootpgw: missing server name\n");
    342  1.1       gwr 		usage();
    343  1.1       gwr 	}
    344  1.1       gwr 	/*
    345  1.4        ws 	 * Get address of real bootp server.
    346  1.1       gwr 	 */
    347  1.1       gwr 	if (inet_aton(servername, &serv_addr.sin_addr) == 0) {
    348  1.1       gwr 		hep = gethostbyname(servername);
    349  1.1       gwr 		if (!hep) {
    350  1.1       gwr 			fprintf(stderr, "bootpgw: can't get addr for %s\n", servername);
    351  1.4        ws 			exit(1);
    352  1.4        ws 		}
    353  1.1       gwr 		memcpy(&serv_addr.sin_addr, hep->h_addr,
    354  1.1       gwr 		    sizeof(serv_addr.sin_addr));
    355  1.1       gwr 	}
    356  1.1       gwr 
    357  1.1       gwr 	if (standalone) {
    358  1.1       gwr 		/*
    359  1.1       gwr 		 * Go into background and disassociate from controlling terminal.
    360  1.1       gwr 		 * XXX - This is not the POSIX way (Should use setsid). -gwr
    361  1.1       gwr 		 */
    362  1.1       gwr 		if (debug < 3) {
    363  1.1       gwr 			if (fork())
    364  1.1       gwr 				exit(0);
    365  1.1       gwr #ifdef	NO_SETSID
    366  1.1       gwr 			setpgrp(0,0);
    367  1.1       gwr #ifdef TIOCNOTTY
    368  1.1       gwr 			n = open("/dev/tty", O_RDWR);
    369  1.1       gwr 			if (n >= 0) {
    370  1.1       gwr 				ioctl(n, TIOCNOTTY, (char *) 0);
    371  1.1       gwr 				(void) close(n);
    372  1.1       gwr 			}
    373  1.1       gwr #endif	/* TIOCNOTTY */
    374  1.1       gwr #else	/* SETSID */
    375  1.1       gwr 			if (setsid() < 0)
    376  1.1       gwr 				perror("setsid");
    377  1.1       gwr #endif	/* SETSID */
    378  1.1       gwr 		} /* if debug < 3 */
    379  1.1       gwr 		/*
    380  1.1       gwr 		 * Nuke any timeout value
    381  1.1       gwr 		 */
    382  1.1       gwr 		timeout = NULL;
    383  1.1       gwr 
    384  1.1       gwr 		/*
    385  1.1       gwr 		 * Here, bootpd would do:
    386  1.1       gwr 		 *	chdir
    387  1.1       gwr 		 *	tzone_init
    388  1.1       gwr 		 *	rdtab_init
    389  1.1       gwr 		 *	readtab
    390  1.1       gwr 		 */
    391  1.1       gwr 
    392  1.1       gwr 		/*
    393  1.1       gwr 		 * Create a socket.
    394  1.1       gwr 		 */
    395  1.1       gwr 		if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
    396  1.1       gwr 			report(LOG_ERR, "socket: %s", get_network_errmsg());
    397  1.1       gwr 			exit(1);
    398  1.1       gwr 		}
    399  1.1       gwr 		/*
    400  1.1       gwr 		 * Get server's listening port number
    401  1.1       gwr 		 */
    402  1.1       gwr 		servp = getservbyname("bootps", "udp");
    403  1.1       gwr 		if (servp) {
    404  1.1       gwr 			bootps_port = ntohs((u_short) servp->s_port);
    405  1.1       gwr 		} else {
    406  1.1       gwr 			bootps_port = (u_short) IPPORT_BOOTPS;
    407  1.1       gwr 			report(LOG_ERR,
    408  1.1       gwr 				   "udp/bootps: unknown service -- assuming port %d",
    409  1.1       gwr 				   bootps_port);
    410  1.1       gwr 		}
    411  1.1       gwr 
    412  1.1       gwr 		/*
    413  1.1       gwr 		 * Bind socket to BOOTPS port.
    414  1.1       gwr 		 */
    415  1.1       gwr 		bind_addr.sin_family = AF_INET;
    416  1.1       gwr 		bind_addr.sin_port = htons(bootps_port);
    417  1.1       gwr 		bind_addr.sin_addr.s_addr = INADDR_ANY;
    418  1.1       gwr 		if (bind(s, (struct sockaddr *) &bind_addr,
    419  1.1       gwr 				 sizeof(bind_addr)) < 0)
    420  1.1       gwr 		{
    421  1.1       gwr 			report(LOG_ERR, "bind: %s", get_network_errmsg());
    422  1.1       gwr 			exit(1);
    423  1.1       gwr 		}
    424  1.1       gwr 	} /* if standalone */
    425  1.1       gwr 	/*
    426  1.1       gwr 	 * Get destination port number so we can reply to client
    427  1.1       gwr 	 */
    428  1.1       gwr 	servp = getservbyname("bootpc", "udp");
    429  1.1       gwr 	if (servp) {
    430  1.1       gwr 		bootpc_port = ntohs(servp->s_port);
    431  1.1       gwr 	} else {
    432  1.1       gwr 		report(LOG_ERR,
    433  1.1       gwr 			   "udp/bootpc: unknown service -- assuming port %d",
    434  1.1       gwr 			   IPPORT_BOOTPC);
    435  1.1       gwr 		bootpc_port = (u_short) IPPORT_BOOTPC;
    436  1.1       gwr 	}
    437  1.1       gwr 
    438  1.1       gwr 	/* no signal catchers */
    439  1.1       gwr 
    440  1.1       gwr 	/*
    441  1.1       gwr 	 * Process incoming requests.
    442  1.1       gwr 	 */
    443  1.1       gwr 	for (;;) {
    444  1.1       gwr 		readfds = 1 << s;
    445  1.1       gwr 		nfound = select(s + 1, (fd_set *)&readfds, NULL, NULL, timeout);
    446  1.1       gwr 		if (nfound < 0) {
    447  1.1       gwr 			if (errno != EINTR) {
    448  1.1       gwr 				report(LOG_ERR, "select: %s", get_errmsg());
    449  1.1       gwr 			}
    450  1.1       gwr 			continue;
    451  1.1       gwr 		}
    452  1.1       gwr 		if (!(readfds & (1 << s))) {
    453  1.1       gwr 			report(LOG_INFO, "exiting after %ld minutes of inactivity",
    454  1.1       gwr 				   actualtimeout.tv_sec / 60);
    455  1.4        ws 			exit(0);
    456  1.2       gwr 		}
    457  1.4        ws 		ra_len = sizeof(clnt_addr);
    458  1.1       gwr 		n = recvfrom(s, pktbuf, MAX_MSG_SIZE, 0,
    459  1.1       gwr 					 (struct sockaddr *) &clnt_addr, &ra_len);
    460  1.1       gwr 		if (n <= 0) {
    461  1.1       gwr 			continue;
    462  1.1       gwr 		}
    463  1.4        ws 		if (debug > 3) {
    464  1.1       gwr 			report(LOG_INFO, "recvd pkt from IP addr %s",
    465  1.1       gwr 				   inet_ntoa(clnt_addr.sin_addr));
    466  1.1       gwr 		}
    467  1.1       gwr 		if (n < sizeof(struct bootp)) {
    468  1.1       gwr 			if (debug) {
    469  1.1       gwr 				report(LOG_INFO, "received short packet");
    470  1.1       gwr 			}
    471  1.1       gwr 			continue;
    472  1.1       gwr 		}
    473  1.1       gwr 		pktlen = n;
    474  1.1       gwr 
    475  1.1       gwr 		switch (bp->bp_op) {
    476  1.1       gwr 		case BOOTREQUEST:
    477  1.1       gwr 			handle_request();
    478  1.1       gwr 			break;
    479  1.1       gwr 		case BOOTREPLY:
    480  1.1       gwr 			handle_reply();
    481  1.1       gwr 			break;
    482  1.1       gwr 		}
    483  1.1       gwr 	}
    484  1.1       gwr }
    485  1.1       gwr 
    486  1.1       gwr 
    488  1.1       gwr 
    489  1.1       gwr 
    490  1.1       gwr /*
    491  1.1       gwr  * Print "usage" message and exit
    492  1.1       gwr  */
    493  1.1       gwr 
    494  1.1       gwr static void
    495  1.1       gwr usage()
    496  1.1       gwr {
    497  1.1       gwr 	fprintf(stderr,
    498  1.1       gwr 			"usage:  bootpgw [-d level] [-i] [-s] [-t timeout] server\n");
    499  1.1       gwr 	fprintf(stderr, "\t -d n\tset debug level\n");
    500  1.1       gwr 	fprintf(stderr, "\t -h n\tset max hop count\n");
    501  1.1       gwr 	fprintf(stderr, "\t -i\tforce inetd mode (run as child of inetd)\n");
    502  1.1       gwr 	fprintf(stderr, "\t -s\tforce standalone mode (run without inetd)\n");
    503  1.1       gwr 	fprintf(stderr, "\t -t n\tset inetd exit timeout to n minutes\n");
    504  1.1       gwr 	fprintf(stderr, "\t -w n\tset min wait time (secs)\n");
    505  1.1       gwr 	exit(1);
    506  1.1       gwr }
    507  1.1       gwr 
    508  1.1       gwr 
    510  1.1       gwr 
    511  1.1       gwr /*
    512  1.1       gwr  * Process BOOTREQUEST packet.
    513  1.1       gwr  *
    514  1.1       gwr  * Note, this just forwards the request to a real server.
    515  1.1       gwr  */
    516  1.1       gwr static void
    517  1.1       gwr handle_request()
    518  1.1       gwr {
    519  1.4        ws 	struct bootp *bp = (struct bootp *) pktbuf;
    520  1.1       gwr 	struct ifreq *ifr;
    521  1.1       gwr 	u_short secs, hops;
    522  1.1       gwr 
    523  1.4        ws 	/* XXX - SLIP init: Set bp_ciaddr = clnt_addr here? */
    524  1.1       gwr 
    525  1.1       gwr 	if (debug) {
    526  1.1       gwr 		report(LOG_INFO, "request from %s",
    527  1.1       gwr 			   inet_ntoa(clnt_addr.sin_addr));
    528  1.1       gwr 	}
    529  1.1       gwr 	/* Has the client been waiting long enough? */
    530  1.1       gwr 	secs = ntohs(bp->bp_secs);
    531  1.1       gwr 	if (secs < minwait)
    532  1.1       gwr 		return;
    533  1.4        ws 
    534  1.4        ws 	/* Has this packet hopped too many times? */
    535  1.1       gwr 	hops = ntohs(bp->bp_hops);
    536  1.1       gwr 	if (++hops > maxhops) {
    537  1.1       gwr 		report(LOG_NOTICE, "request from %s reached hop limit",
    538  1.1       gwr 			   inet_ntoa(clnt_addr.sin_addr));
    539  1.1       gwr 		return;
    540  1.1       gwr 	}
    541  1.1       gwr 	bp->bp_hops = htons(hops);
    542  1.1       gwr 
    543  1.1       gwr 	/*
    544  1.1       gwr 	 * Here one might discard a request from the same subnet as the
    545  1.1       gwr 	 * real server, but we can assume that the real server will send
    546  1.1       gwr 	 * a reply to the client before it waits for minwait seconds.
    547  1.1       gwr 	 */
    548  1.1       gwr 
    549  1.1       gwr 	/* If gateway address is not set, put in local interface addr. */
    550  1.1       gwr 	if (bp->bp_giaddr.s_addr == 0) {
    551  1.1       gwr #if 0	/* BUG */
    552  1.1       gwr 		struct sockaddr_in *sip;
    553  1.1       gwr 		/*
    554  1.1       gwr 		 * XXX - This picks the wrong interface when the receive addr
    555  1.4        ws 		 * is the broadcast address.  There is no  portable way to
    556  1.1       gwr 		 * find out which interface a broadcast was received on. -gwr
    557  1.1       gwr 		 * (Thanks to <walker (at) zk3.dec.com> for finding this bug!)
    558  1.4        ws 		 */
    559  1.1       gwr 		ifr = getif(s, &clnt_addr.sin_addr);
    560  1.1       gwr 		if (!ifr) {
    561  1.1       gwr 			report(LOG_NOTICE, "no interface for request from %s",
    562  1.1       gwr 				   inet_ntoa(clnt_addr.sin_addr));
    563  1.1       gwr 			return;
    564  1.1       gwr 		}
    565  1.1       gwr 		sip = (struct sockaddr_in *) &(ifr->ifr_addr);
    566  1.1       gwr 		bp->bp_giaddr = sip->sin_addr;
    567  1.1       gwr #else	/* BUG */
    568  1.1       gwr 		/*
    569  1.1       gwr 		 * XXX - Just set "giaddr" to our "official" IP address.
    570  1.1       gwr 		 * RFC 1532 says giaddr MUST be set to the address of the
    571  1.1       gwr 		 * interface on which the request was received.  Setting
    572  1.1       gwr 		 * it to our "default" IP address is not strictly correct,
    573  1.1       gwr 		 * but is good enough to allow the real BOOTP server to
    574  1.1       gwr 		 * get the reply back here.  Then, before we forward the
    575  1.1       gwr 		 * reply to the client, the giaddr field is corrected.
    576  1.1       gwr 		 * (In case the client uses giaddr, which it should not.)
    577  1.1       gwr 		 * See handle_reply()
    578  1.1       gwr 		 */
    579  1.1       gwr 		bp->bp_giaddr = my_ip_addr;
    580  1.1       gwr #endif	/* BUG */
    581  1.1       gwr 
    582  1.1       gwr 		/*
    583  1.1       gwr 		 * XXX - DHCP says to insert a subnet mask option into the
    584  1.4        ws 		 * options area of the request (if vendor magic == std).
    585  1.4        ws 		 */
    586  1.1       gwr 	}
    587  1.1       gwr 	/* Set up socket address for send. */
    588  1.1       gwr 	serv_addr.sin_family = AF_INET;
    589  1.4        ws 	serv_addr.sin_port = htons(bootps_port);
    590  1.4        ws 
    591  1.1       gwr 	/* Send reply with same size packet as request used. */
    592  1.1       gwr 	if (sendto(s, pktbuf, pktlen, 0,
    593  1.1       gwr 			   (struct sockaddr *) &serv_addr,
    594  1.1       gwr 			   sizeof(serv_addr)) < 0)
    595  1.1       gwr 	{
    596  1.1       gwr 		report(LOG_ERR, "sendto: %s", get_network_errmsg());
    597  1.1       gwr 	}
    598  1.1       gwr }
    599  1.1       gwr 
    600  1.1       gwr 
    602  1.1       gwr 
    603  1.1       gwr /*
    604  1.1       gwr  * Process BOOTREPLY packet.
    605  1.1       gwr  */
    606  1.1       gwr static void
    607  1.1       gwr handle_reply()
    608  1.1       gwr {
    609  1.1       gwr 	struct bootp *bp = (struct bootp *) pktbuf;
    610  1.1       gwr 	struct ifreq *ifr;
    611  1.1       gwr 	struct sockaddr_in *sip;
    612  1.1       gwr 	u_char canon_haddr[MAXHADDRLEN];
    613  1.1       gwr 	unsigned char *ha;
    614  1.1       gwr 	int len;
    615  1.1       gwr 
    616  1.1       gwr 	if (debug) {
    617  1.1       gwr 		report(LOG_INFO, "   reply for %s",
    618  1.1       gwr 			   inet_ntoa(bp->bp_yiaddr));
    619  1.1       gwr 	}
    620  1.1       gwr 	/* Make sure client is directly accessible. */
    621  1.1       gwr 	ifr = getif(s, &(bp->bp_yiaddr));
    622  1.1       gwr 	if (!ifr) {
    623  1.1       gwr 		report(LOG_NOTICE, "no interface for reply to %s",
    624  1.1       gwr 			   inet_ntoa(bp->bp_yiaddr));
    625  1.1       gwr 		return;
    626  1.1       gwr 	}
    627  1.1       gwr #if 1	/* Experimental (see BUG above) */
    628  1.1       gwr /* #ifdef CATER_TO_OLD_CLIENTS ? */
    629  1.1       gwr 	/*
    630  1.1       gwr 	 * The giaddr field has been set to our "default" IP address
    631  1.1       gwr 	 * which might not be on the same interface as the client.
    632  1.1       gwr 	 * In case the client looks at giaddr, (which it should not)
    633  1.1       gwr 	 * giaddr is now set to the address of the correct interface.
    634  1.1       gwr 	 */
    635  1.4        ws 	sip = (struct sockaddr_in *) &(ifr->ifr_addr);
    636  1.4        ws 	bp->bp_giaddr = sip->sin_addr;
    637  1.4        ws #endif
    638  1.1       gwr 
    639  1.1       gwr 	/* Set up socket address for send to client. */
    640  1.1       gwr 	clnt_addr.sin_family = AF_INET;
    641  1.1       gwr 	clnt_addr.sin_addr = bp->bp_yiaddr;
    642  1.1       gwr 	clnt_addr.sin_port = htons(bootpc_port);
    643  1.1       gwr 
    644  1.1       gwr 	/* Create an ARP cache entry for the client. */
    645  1.1       gwr 	ha = bp->bp_chaddr;
    646  1.1       gwr 	len = bp->bp_hlen;
    647  1.1       gwr 	if (len > MAXHADDRLEN)
    648  1.1       gwr 		len = MAXHADDRLEN;
    649  1.1       gwr 	if (bp->bp_htype == HTYPE_IEEE802) {
    650  1.1       gwr 		haddr_conv802(ha, canon_haddr, len);
    651  1.1       gwr 		ha = canon_haddr;
    652  1.1       gwr 	}
    653  1.1       gwr 	if (debug > 1)
    654  1.1       gwr 		report(LOG_INFO, "setarp %s - %s",
    655  1.4        ws 			   inet_ntoa(bp->bp_yiaddr), haddrtoa(ha, len));
    656  1.4        ws 	setarp(s, &bp->bp_yiaddr, ha, len);
    657  1.1       gwr 
    658  1.1       gwr 	/* Send reply with same size packet as request used. */
    659  1.1       gwr 	if (sendto(s, pktbuf, pktlen, 0,
    660  1.1       gwr 			   (struct sockaddr *) &clnt_addr,
    661  1.1       gwr 			   sizeof(clnt_addr)) < 0)
    662  1.1       gwr 	{
    663  1.1       gwr 		report(LOG_ERR, "sendto: %s", get_network_errmsg());
    664  1.1       gwr 	}
    665  1.1       gwr }
    666  1.1       gwr 
    667  1.1       gwr /*
    668  1.1       gwr  * Local Variables:
    669  1.1       gwr  * tab-width: 4
    670  1.1       gwr  * c-indent-level: 4
    671  1.1       gwr  * c-argdecl-indent: 4
    672  1.1       gwr  * c-continued-statement-offset: 4
    673                 * c-continued-brace-offset: -4
    674                 * c-label-offset: -4
    675                 * c-brace-offset: 0
    676                 * End:
    677                 */
    678