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