Home | History | Annotate | Line # | Download | only in common
dovend.c revision 1.3.10.1
      1  1.3.10.1     tv /*	$NetBSD: dovend.c,v 1.3.10.1 2000/10/17 19:50:20 tv Exp $	*/
      2       1.3  lukem 
      3       1.3  lukem #include <sys/cdefs.h>
      4       1.3  lukem #ifndef lint
      5  1.3.10.1     tv __RCSID("$NetBSD: dovend.c,v 1.3.10.1 2000/10/17 19:50:20 tv Exp $");
      6       1.3  lukem #endif
      7       1.2  perry 
      8       1.1    gwr /*
      9       1.1    gwr  * dovend.c : Inserts all but the first few vendor options.
     10       1.1    gwr  */
     11       1.1    gwr 
     12       1.1    gwr #include <sys/types.h>
     13       1.1    gwr 
     14       1.1    gwr #include <netinet/in.h>
     15       1.1    gwr #include <arpa/inet.h>			/* inet_ntoa */
     16       1.1    gwr 
     17       1.1    gwr #include <stdlib.h>
     18       1.1    gwr #include <stdio.h>
     19       1.1    gwr #include <string.h>
     20       1.1    gwr #include <errno.h>
     21       1.1    gwr #include <syslog.h>
     22       1.1    gwr 
     23       1.1    gwr #ifndef USE_BFUNCS
     24       1.1    gwr # include <memory.h>
     25       1.1    gwr /* Yes, memcpy is OK here (no overlapped copies). */
     26       1.1    gwr # define bcopy(a,b,c)    memcpy(b,a,c)
     27       1.1    gwr # define bzero(p,l)      memset(p,0,l)
     28       1.1    gwr # define bcmp(a,b,c)     memcmp(a,b,c)
     29       1.1    gwr # define index           strchr
     30       1.1    gwr #endif
     31       1.1    gwr 
     32       1.1    gwr #include "bootp.h"
     33       1.1    gwr #include "bootpd.h"
     34       1.1    gwr #include "report.h"
     35       1.1    gwr #include "dovend.h"
     36       1.1    gwr 
     37       1.1    gwr #ifdef	__STDC__
     38       1.1    gwr #define P(args) args
     39       1.1    gwr #else
     40       1.1    gwr #define P(args) ()
     41       1.1    gwr #endif
     42       1.1    gwr 
     43       1.1    gwr PRIVATE int insert_generic P((struct shared_bindata *, byte **, int *));
     44       1.1    gwr 
     45       1.1    gwr /*
     46       1.1    gwr  * Insert the 2nd part of the options into an option buffer.
     47       1.1    gwr  * Return amount of space used.
     48       1.1    gwr  *
     49       1.1    gwr  * This inserts everything EXCEPT:
     50       1.1    gwr  *   magic cookie, subnet mask, gateway, bootsize, extension file
     51       1.1    gwr  * Those are handled separately (in bootpd.c) to allow this function
     52       1.1    gwr  * to be shared between bootpd and bootpef.
     53       1.1    gwr  *
     54       1.1    gwr  * When an "extension file" is in use, the options inserted by
     55       1.1    gwr  * this function go into the exten_file, not the bootp response.
     56       1.1    gwr  */
     57       1.1    gwr 
     58       1.1    gwr int
     59       1.1    gwr dovend_rfc1497(hp, buf, len)
     60       1.1    gwr 	struct host *hp;
     61       1.1    gwr 	byte *buf;
     62       1.1    gwr 	int len;
     63       1.1    gwr {
     64       1.1    gwr 	int bytesleft = len;
     65       1.1    gwr 	byte *vp = buf;
     66       1.3  lukem #if 0
     67       1.1    gwr 	char *tmpstr;
     68       1.3  lukem #endif
     69       1.1    gwr 
     70  1.3.10.1     tv 	static const char noroom[] = "%s: No room for \"%s\" option";
     71       1.1    gwr #define	NEED(LEN, MSG) do                       \
     72       1.1    gwr 		if (bytesleft < (LEN)) {         	    \
     73       1.1    gwr 			report(LOG_NOTICE, noroom,          \
     74       1.1    gwr 				   hp->hostname->string, MSG);  \
     75       1.1    gwr 			return (vp - buf);                  \
     76       1.1    gwr 		} while (0)
     77       1.1    gwr 
     78       1.1    gwr 	/*
     79       1.1    gwr 	 * Note that the following have already been inserted:
     80       1.1    gwr 	 *   magic_cookie, subnet_mask, gateway, bootsize
     81       1.1    gwr 	 *
     82       1.1    gwr 	 * The remaining options are inserted in order of importance.
     83       1.1    gwr 	 * (Of course the importance of each is a matter of opinion.)
     84       1.1    gwr 	 * The option insertion order should probably be configurable.
     85       1.1    gwr 	 *
     86       1.1    gwr 	 * This is the order used in the NetBSD version.  Can anyone
     87       1.1    gwr 	 * explain why the time_offset and swap_server are first?
     88       1.1    gwr 	 * Also, why is the hostname so far down the list?  -gwr
     89       1.1    gwr 	 */
     90       1.1    gwr 
     91       1.1    gwr 	if (hp->flags.time_offset) {
     92       1.1    gwr 		NEED(6, "to");
     93       1.1    gwr 		*vp++ = TAG_TIME_OFFSET;/* -1 byte  */
     94       1.1    gwr 		*vp++ = 4;				/* -1 byte  */
     95       1.1    gwr 		insert_u_long(htonl(hp->time_offset), &vp);	/* -4 bytes */
     96       1.1    gwr 		bytesleft -= 6;
     97       1.1    gwr 	}
     98       1.1    gwr 	/*
     99       1.1    gwr 	 * swap server, root path, dump path
    100       1.1    gwr 	 */
    101       1.1    gwr 	if (hp->flags.swap_server) {
    102       1.1    gwr 		NEED(6, "sw");
    103       1.1    gwr 		/* There is just one SWAP_SERVER, so it is not an iplist. */
    104       1.1    gwr 		*vp++ = TAG_SWAP_SERVER;/* -1 byte  */
    105       1.1    gwr 		*vp++ = 4;				/* -1 byte  */
    106       1.1    gwr 		insert_u_long(hp->swap_server.s_addr, &vp);	/* -4 bytes */
    107       1.1    gwr 		bytesleft -= 6;			/* Fix real count */
    108       1.1    gwr 	}
    109       1.1    gwr 	if (hp->flags.root_path) {
    110       1.1    gwr 		/*
    111       1.1    gwr 		 * Check for room for root_path.  Add 2 to account for
    112       1.1    gwr 		 * TAG_ROOT_PATH and length.
    113       1.1    gwr 		 */
    114       1.1    gwr 		len = strlen(hp->root_path->string);
    115       1.1    gwr 		NEED((len + 2), "rp");
    116       1.1    gwr 		*vp++ = TAG_ROOT_PATH;
    117       1.1    gwr 		*vp++ = (byte) (len & 0xFF);
    118       1.1    gwr 		bcopy(hp->root_path->string, vp, len);
    119       1.1    gwr 		vp += len;
    120       1.1    gwr 		bytesleft -= len + 2;
    121       1.1    gwr 	}
    122       1.1    gwr 	if (hp->flags.dump_file) {
    123       1.1    gwr 		/*
    124       1.1    gwr 		 * Check for room for dump_file.  Add 2 to account for
    125       1.1    gwr 		 * TAG_DUMP_FILE and length.
    126       1.1    gwr 		 */
    127       1.1    gwr 		len = strlen(hp->dump_file->string);
    128       1.1    gwr 		NEED((len + 2), "df");
    129       1.1    gwr 		*vp++ = TAG_DUMP_FILE;
    130       1.1    gwr 		*vp++ = (byte) (len & 0xFF);
    131       1.1    gwr 		bcopy(hp->dump_file->string, vp, len);
    132       1.1    gwr 		vp += len;
    133       1.1    gwr 		bytesleft -= len + 2;
    134       1.1    gwr 	}
    135       1.1    gwr 	/*
    136       1.1    gwr 	 * DNS server and domain
    137       1.1    gwr 	 */
    138       1.1    gwr 	if (hp->flags.domain_server) {
    139       1.1    gwr 		if (insert_ip(TAG_DOMAIN_SERVER,
    140       1.1    gwr 					  hp->domain_server,
    141       1.1    gwr 					  &vp, &bytesleft))
    142       1.1    gwr 			NEED(8, "ds");
    143       1.1    gwr 	}
    144       1.1    gwr 	if (hp->flags.domain_name) {
    145       1.1    gwr 		/*
    146       1.1    gwr 		 * Check for room for domain_name.  Add 2 to account for
    147       1.1    gwr 		 * TAG_DOMAIN_NAME and length.
    148       1.1    gwr 		 */
    149       1.1    gwr 		len = strlen(hp->domain_name->string);
    150       1.1    gwr 		NEED((len + 2), "dn");
    151       1.1    gwr 		*vp++ = TAG_DOMAIN_NAME;
    152       1.1    gwr 		*vp++ = (byte) (len & 0xFF);
    153       1.1    gwr 		bcopy(hp->domain_name->string, vp, len);
    154       1.1    gwr 		vp += len;
    155       1.1    gwr 		bytesleft -= len + 2;
    156       1.1    gwr 	}
    157       1.1    gwr 	/*
    158       1.1    gwr 	 * NIS (YP) server and domain
    159       1.1    gwr 	 */
    160       1.1    gwr 	if (hp->flags.nis_server) {
    161       1.1    gwr 		if (insert_ip(TAG_NIS_SERVER,
    162       1.1    gwr 					  hp->nis_server,
    163       1.1    gwr 					  &vp, &bytesleft))
    164       1.1    gwr 			NEED(8, "ds");
    165       1.1    gwr 	}
    166       1.1    gwr 	if (hp->flags.nis_domain) {
    167       1.1    gwr 		/*
    168       1.1    gwr 		 * Check for room for nis_domain.  Add 2 to account for
    169       1.1    gwr 		 * TAG_NIS_DOMAIN and length.
    170       1.1    gwr 		 */
    171       1.1    gwr 		len = strlen(hp->nis_domain->string);
    172       1.1    gwr 		NEED((len + 2), "dn");
    173       1.1    gwr 		*vp++ = TAG_NIS_DOMAIN;
    174       1.1    gwr 		*vp++ = (byte) (len & 0xFF);
    175       1.1    gwr 		bcopy(hp->nis_domain->string, vp, len);
    176       1.1    gwr 		vp += len;
    177       1.1    gwr 		bytesleft -= len + 2;
    178       1.1    gwr 	}
    179       1.1    gwr 	/* IEN 116 name server */
    180       1.1    gwr 	if (hp->flags.name_server) {
    181       1.1    gwr 		if (insert_ip(TAG_NAME_SERVER,
    182       1.1    gwr 					  hp->name_server,
    183       1.1    gwr 					  &vp, &bytesleft))
    184       1.1    gwr 			NEED(8, "ns");
    185       1.1    gwr 	}
    186       1.1    gwr 	if (hp->flags.rlp_server) {
    187       1.1    gwr 		if (insert_ip(TAG_RLP_SERVER,
    188       1.1    gwr 					  hp->rlp_server,
    189       1.1    gwr 					  &vp, &bytesleft))
    190       1.1    gwr 			NEED(8, "rl");
    191       1.1    gwr 	}
    192       1.1    gwr 	/* Time server (RFC 868) */
    193       1.1    gwr 	if (hp->flags.time_server) {
    194       1.1    gwr 		if (insert_ip(TAG_TIME_SERVER,
    195       1.1    gwr 					  hp->time_server,
    196       1.1    gwr 					  &vp, &bytesleft))
    197       1.1    gwr 			NEED(8, "ts");
    198       1.1    gwr 	}
    199       1.1    gwr 	/* NTP (time) Server (RFC 1129) */
    200       1.1    gwr 	if (hp->flags.ntp_server) {
    201       1.1    gwr 		if (insert_ip(TAG_NTP_SERVER,
    202       1.1    gwr 					  hp->ntp_server,
    203       1.1    gwr 					  &vp, &bytesleft))
    204       1.1    gwr 			NEED(8, "ts");
    205       1.1    gwr 	}
    206       1.1    gwr 	/*
    207       1.1    gwr 	 * I wonder:  If the hostname were "promoted" into the BOOTP
    208       1.1    gwr 	 * response part, might these "extension" files possibly be
    209       1.1    gwr 	 * shared between several clients?
    210       1.1    gwr 	 *
    211       1.1    gwr 	 * Also, why not just use longer BOOTP packets with all the
    212       1.1    gwr 	 * additional length used as option data.  This bootpd version
    213       1.1    gwr 	 * already supports that feature by replying with the same
    214       1.1    gwr 	 * packet length as the client request packet. -gwr
    215       1.1    gwr 	 */
    216       1.1    gwr 	if (hp->flags.name_switch && hp->flags.send_name) {
    217       1.1    gwr 		/*
    218       1.1    gwr 		 * Check for room for hostname.  Add 2 to account for
    219       1.1    gwr 		 * TAG_HOST_NAME and length.
    220       1.1    gwr 		 */
    221       1.1    gwr 		len = strlen(hp->hostname->string);
    222       1.1    gwr #if 0
    223       1.1    gwr 		/*
    224       1.1    gwr 		 * XXX - Too much magic.  The user can always set the hostname
    225       1.1    gwr 		 * to the short version in the bootptab file. -gwr
    226       1.1    gwr 		 */
    227       1.1    gwr 		if ((len + 2) > bytesleft) {
    228       1.1    gwr 			/*
    229       1.1    gwr 			 * Not enough room for full (domain-qualified) hostname, try
    230       1.1    gwr 			 * stripping it down to just the first field (host).
    231       1.1    gwr 			 */
    232       1.1    gwr 			tmpstr = hp->hostname->string;
    233       1.1    gwr 			len = 0;
    234       1.1    gwr 			while (*tmpstr && (*tmpstr != '.')) {
    235       1.1    gwr 				tmpstr++;
    236       1.1    gwr 				len++;
    237       1.1    gwr 			}
    238       1.1    gwr 		}
    239       1.1    gwr #endif
    240       1.1    gwr 		NEED((len + 2), "hn");
    241       1.1    gwr 		*vp++ = TAG_HOST_NAME;
    242       1.1    gwr 		*vp++ = (byte) (len & 0xFF);
    243       1.1    gwr 		bcopy(hp->hostname->string, vp, len);
    244       1.1    gwr 		vp += len;
    245       1.1    gwr 		bytesleft -= len + 2;
    246       1.1    gwr 	}
    247       1.1    gwr 	/*
    248       1.1    gwr 	 * The rest of these are less important, so they go last.
    249       1.1    gwr 	 */
    250       1.1    gwr 	if (hp->flags.lpr_server) {
    251       1.1    gwr 		if (insert_ip(TAG_LPR_SERVER,
    252       1.1    gwr 					  hp->lpr_server,
    253       1.1    gwr 					  &vp, &bytesleft))
    254       1.1    gwr 			NEED(8, "lp");
    255       1.1    gwr 	}
    256       1.1    gwr 	if (hp->flags.cookie_server) {
    257       1.1    gwr 		if (insert_ip(TAG_COOKIE_SERVER,
    258       1.1    gwr 					  hp->cookie_server,
    259       1.1    gwr 					  &vp, &bytesleft))
    260       1.1    gwr 			NEED(8, "cs");
    261       1.1    gwr 	}
    262       1.1    gwr 	if (hp->flags.log_server) {
    263       1.1    gwr 		if (insert_ip(TAG_LOG_SERVER,
    264       1.1    gwr 					  hp->log_server,
    265       1.1    gwr 					  &vp, &bytesleft))
    266       1.1    gwr 			NEED(8, "lg");
    267       1.1    gwr 	}
    268       1.1    gwr 	/*
    269       1.1    gwr 	 * XXX - Add new tags here (to insert options)
    270       1.1    gwr 	 */
    271       1.1    gwr 	if (hp->flags.generic) {
    272       1.1    gwr 		if (insert_generic(hp->generic, &vp, &bytesleft))
    273       1.1    gwr 			NEED(64, "(generic)");
    274       1.1    gwr 	}
    275       1.1    gwr 	/*
    276       1.1    gwr 	 * The end marker is inserted by the caller.
    277       1.1    gwr 	 */
    278       1.1    gwr 	return (vp - buf);
    279       1.1    gwr #undef	NEED
    280       1.1    gwr }								/* dovend_rfc1497 */
    281       1.1    gwr 
    282       1.1    gwr 
    284       1.1    gwr 
    285       1.1    gwr /*
    286       1.1    gwr  * Insert a tag value, a length value, and a list of IP addresses into the
    287       1.1    gwr  * memory buffer indirectly pointed to by "dest".  "tag" is the RFC1048 tag
    288       1.1    gwr  * number to use, "iplist" is a pointer to a list of IP addresses
    289       1.1    gwr  * (struct in_addr_list), and "bytesleft" points to an integer which
    290       1.1    gwr  * indicates the size of the "dest" buffer.
    291       1.1    gwr  *
    292       1.1    gwr  * Return zero if everything fits.
    293       1.1    gwr  *
    294       1.1    gwr  * This is used to fill the vendor-specific area of a bootp packet in
    295       1.1    gwr  * conformance to RFC1048.
    296       1.1    gwr  */
    297       1.1    gwr 
    298       1.1    gwr int
    299       1.1    gwr insert_ip(tag, iplist, dest, bytesleft)
    300       1.1    gwr 	byte tag;
    301       1.1    gwr 	struct in_addr_list *iplist;
    302       1.1    gwr 	byte **dest;
    303       1.1    gwr 	int *bytesleft;
    304       1.1    gwr {
    305       1.1    gwr 	struct in_addr *addrptr;
    306       1.1    gwr 	unsigned addrcount = 1;
    307       1.1    gwr 	byte *d;
    308       1.1    gwr 
    309       1.1    gwr 	if (iplist == NULL)
    310       1.1    gwr 		return (0);
    311       1.1    gwr 
    312       1.1    gwr 	if (*bytesleft >= 6) {
    313       1.1    gwr 		d = *dest;				/* Save pointer for later */
    314       1.1    gwr 		**dest = tag;
    315       1.1    gwr 		(*dest) += 2;
    316       1.1    gwr 		(*bytesleft) -= 2;		/* Account for tag and length */
    317       1.1    gwr 		addrptr = iplist->addr;
    318       1.1    gwr 		addrcount = iplist->addrcount;
    319       1.1    gwr 		while ((*bytesleft >= 4) && (addrcount > 0)) {
    320       1.1    gwr 			insert_u_long(addrptr->s_addr, dest);
    321       1.1    gwr 			addrptr++;
    322       1.1    gwr 			addrcount--;
    323       1.1    gwr 			(*bytesleft) -= 4;	/* Four bytes per address */
    324       1.1    gwr 		}
    325       1.1    gwr 		d[1] = (byte) ((*dest - d - 2) & 0xFF);
    326       1.1    gwr 	}
    327       1.1    gwr 	return (addrcount);
    328       1.1    gwr }
    329       1.1    gwr 
    330       1.1    gwr 
    332       1.1    gwr 
    333       1.1    gwr /*
    334       1.1    gwr  * Insert generic data into a bootp packet.  The data is assumed to already
    335       1.1    gwr  * be in RFC1048 format.  It is inserted using a first-fit algorithm which
    336       1.1    gwr  * attempts to insert as many tags as possible.  Tags and data which are
    337       1.1    gwr  * too large to fit are skipped; any remaining tags are tried until they
    338       1.1    gwr  * have all been exhausted.
    339       1.1    gwr  * Return zero if everything fits.
    340       1.1    gwr  */
    341       1.1    gwr 
    342       1.1    gwr static int
    343       1.1    gwr insert_generic(gendata, buff, bytesleft)
    344       1.1    gwr 	struct shared_bindata *gendata;
    345       1.1    gwr 	byte **buff;
    346       1.1    gwr 	int *bytesleft;
    347       1.1    gwr {
    348       1.1    gwr 	byte *srcptr;
    349       1.1    gwr 	int length, numbytes;
    350       1.1    gwr 	int skipped = 0;
    351       1.1    gwr 
    352       1.1    gwr 	if (gendata == NULL)
    353       1.1    gwr 		return (0);
    354       1.1    gwr 
    355       1.1    gwr 	srcptr = gendata->data;
    356       1.1    gwr 	length = gendata->length;
    357       1.1    gwr 	while ((length > 0) && (*bytesleft > 0)) {
    358       1.1    gwr 		switch (*srcptr) {
    359       1.1    gwr 		case TAG_END:
    360       1.1    gwr 			length = 0;			/* Force an exit on next iteration */
    361       1.1    gwr 			break;
    362       1.1    gwr 		case TAG_PAD:
    363       1.1    gwr 			*(*buff)++ = *srcptr++;
    364       1.1    gwr 			(*bytesleft)--;
    365       1.1    gwr 			length--;
    366       1.1    gwr 			break;
    367       1.1    gwr 		default:
    368       1.1    gwr 			numbytes = srcptr[1] + 2;
    369       1.1    gwr 			if (*bytesleft < numbytes)
    370       1.1    gwr 				skipped += numbytes;
    371       1.1    gwr 			else {
    372       1.1    gwr 				bcopy(srcptr, *buff, numbytes);
    373       1.1    gwr 				(*buff) += numbytes;
    374       1.1    gwr 				(*bytesleft) -= numbytes;
    375       1.1    gwr 			}
    376       1.1    gwr 			srcptr += numbytes;
    377       1.1    gwr 			length -= numbytes;
    378       1.1    gwr 			break;
    379       1.1    gwr 		}
    380       1.1    gwr 	} /* while */
    381       1.1    gwr 	return (skipped);
    382       1.1    gwr }
    383       1.1    gwr 
    384       1.1    gwr /*
    385       1.1    gwr  * Insert the unsigned long "value" into memory starting at the byte
    386       1.1    gwr  * pointed to by the byte pointer (*dest).  (*dest) is updated to
    387       1.1    gwr  * point to the next available byte.
    388       1.1    gwr  *
    389       1.1    gwr  * Since it is desirable to internally store network addresses in network
    390       1.1    gwr  * byte order (in struct in_addr's), this routine expects longs to be
    391       1.1    gwr  * passed in network byte order.
    392       1.1    gwr  *
    393       1.1    gwr  * However, due to the nature of the main algorithm, the long must be in
    394       1.1    gwr  * host byte order, thus necessitating the use of ntohl() first.
    395       1.1    gwr  */
    396       1.1    gwr 
    397       1.1    gwr void
    398       1.1    gwr insert_u_long(value, dest)
    399       1.1    gwr 	u_int32 value;
    400       1.1    gwr 	byte **dest;
    401       1.1    gwr {
    402       1.1    gwr 	byte *temp;
    403       1.1    gwr 	int n;
    404       1.1    gwr 
    405       1.1    gwr 	value = ntohl(value);		/* Must use host byte order here */
    406       1.1    gwr 	temp = (*dest += 4);
    407       1.1    gwr 	for (n = 4; n > 0; n--) {
    408       1.1    gwr 		*--temp = (byte) (value & 0xFF);
    409       1.1    gwr 		value >>= 8;
    410       1.1    gwr 	}
    411       1.1    gwr 	/* Final result is network byte order */
    412       1.1    gwr }
    413       1.1    gwr 
    414       1.1    gwr /*
    415       1.1    gwr  * Local Variables:
    416       1.1    gwr  * tab-width: 4
    417       1.1    gwr  * c-indent-level: 4
    418       1.1    gwr  * c-argdecl-indent: 4
    419       1.1    gwr  * c-continued-statement-offset: 4
    420       1.1    gwr  * c-continued-brace-offset: -4
    421       1.1    gwr  * c-label-offset: -4
    422       1.1    gwr  * c-brace-offset: 0
    423                   * End:
    424                   */
    425