1 /*
2 * Copyright (c) 1988, 1992 The University of Utah and the Center
3 * for Software Science (CSS).
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * the Center for Software Science of the University of Utah Computer
9 * Science Department. CSS requests users of this software to return
10 * to css-dist@cs.utah.edu any improvements that they make and grant
11 * CSS redistribution rights.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by the University of
24 * California, Berkeley and its contributors.
25 * 4. Neither the name of the University nor the names of its contributors
26 * may be used to endorse or promote products derived from this software
27 * without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * SUCH DAMAGE.
40 *
41 * from: @(#)utils.c 8.1 (Berkeley) 6/4/93
42 *
43 * From: Utah Hdr: utils.c 3.1 92/07/06
44 * Author: Jeff Forys, University of Utah CSS
45 */
46
47 #ifndef lint
48 #if 0
49 static const char sccsid[] = "@(#)utils.c 8.1 (Berkeley) 6/4/93";
50 #endif
51 static const char rcsid[] =
52 "$FreeBSD: stable/9/libexec/rbootd/utils.c 229140 2011-12-31 19:36:11Z dim $";
53 #endif /* not lint */
54
55 #include <sys/param.h>
56 #include <sys/time.h>
57 #include <netinet/in.h>
58
59 #include <fcntl.h>
60 #include <signal.h>
61 #include <stdio.h>
62 #include <stdlib.h>
63 #include <string.h>
64 #include <syslog.h>
65 #include <time.h>
66 #include <unistd.h>
67 #include "defs.h"
68
69 /*
70 ** DispPkt -- Display the contents of an RMPCONN packet.
71 **
72 ** Parameters:
73 ** rconn - packet to be displayed.
74 ** direct - direction packet is going (DIR_*).
75 **
76 ** Returns:
77 ** Nothing.
78 **
79 ** Side Effects:
80 ** None.
81 */
82 void
DispPkt(RMPCONN * rconn,int direct)83 DispPkt(RMPCONN *rconn, int direct)
84 {
85 static const char BootFmt[] = "\t\tRetCode:%u SeqNo:%x SessID:%x Vers:%u";
86 static const char ReadFmt[] = "\t\tRetCode:%u Offset:%x SessID:%x\n";
87
88 struct tm *tmp;
89 struct rmp_packet *rmp;
90 int i, omask;
91 u_int32_t t;
92
93 /*
94 * Since we will be working with RmpConns as well as DbgFp, we
95 * must block signals that can affect either.
96 */
97 omask = sigblock(sigmask(SIGHUP)|sigmask(SIGUSR1)|sigmask(SIGUSR2));
98
99 if (DbgFp == NULL) { /* sanity */
100 (void) sigsetmask(omask);
101 return;
102 }
103
104 /* display direction packet is going using '>>>' or '<<<' */
105 fputs((direct==DIR_RCVD)?"<<< ":(direct==DIR_SENT)?">>> ":"", DbgFp);
106
107 /* display packet timestamp */
108 tmp = localtime((time_t *)&rconn->tstamp.tv_sec);
109 fprintf(DbgFp, "%02d:%02d:%02d.%06ld ", tmp->tm_hour, tmp->tm_min,
110 tmp->tm_sec, rconn->tstamp.tv_usec);
111
112 /* display src or dst addr and information about network interface */
113 fprintf(DbgFp, "Addr: %s Intf: %s\n", EnetStr(rconn), IntfName);
114
115 rmp = &rconn->rmp;
116
117 /* display IEEE 802.2 Logical Link Control header */
118 (void) fprintf(DbgFp, "\t802.2 LLC: DSAP:%x SSAP:%x CTRL:%x\n",
119 rmp->hp_llc.dsap, rmp->hp_llc.ssap, ntohs(rmp->hp_llc.cntrl));
120
121 /* display HP extensions to 802.2 Logical Link Control header */
122 (void) fprintf(DbgFp, "\tHP Ext: DXSAP:%x SXSAP:%x\n",
123 ntohs(rmp->hp_llc.dxsap), ntohs(rmp->hp_llc.sxsap));
124
125 /*
126 * Display information about RMP packet using type field to
127 * determine what kind of packet this is.
128 */
129 switch(rmp->r_type) {
130 case RMP_BOOT_REQ: /* boot request */
131 (void) fprintf(DbgFp, "\tBoot Request:");
132 GETWORD(rmp->r_brq.rmp_seqno, t);
133 if (ntohs(rmp->r_brq.rmp_session) == RMP_PROBESID) {
134 if (WORDZE(rmp->r_brq.rmp_seqno))
135 fputs(" (Send Server ID)", DbgFp);
136 else
137 fprintf(DbgFp," (Send Filename #%u)",t);
138 }
139 (void) fputc('\n', DbgFp);
140 (void) fprintf(DbgFp, BootFmt, rmp->r_brq.rmp_retcode,
141 t, ntohs(rmp->r_brq.rmp_session),
142 ntohs(rmp->r_brq.rmp_version));
143 (void) fprintf(DbgFp, "\n\t\tMachine Type: ");
144 for (i = 0; i < RMP_MACHLEN; i++)
145 (void) fputc(rmp->r_brq.rmp_machtype[i], DbgFp);
146 DspFlnm(rmp->r_brq.rmp_flnmsize, &rmp->r_brq.rmp_flnm);
147 break;
148 case RMP_BOOT_REPL: /* boot reply */
149 fprintf(DbgFp, "\tBoot Reply:\n");
150 GETWORD(rmp->r_brpl.rmp_seqno, t);
151 (void) fprintf(DbgFp, BootFmt, rmp->r_brpl.rmp_retcode,
152 t, ntohs(rmp->r_brpl.rmp_session),
153 ntohs(rmp->r_brpl.rmp_version));
154 DspFlnm(rmp->r_brpl.rmp_flnmsize,&rmp->r_brpl.rmp_flnm);
155 break;
156 case RMP_READ_REQ: /* read request */
157 (void) fprintf(DbgFp, "\tRead Request:\n");
158 GETWORD(rmp->r_rrq.rmp_offset, t);
159 (void) fprintf(DbgFp, ReadFmt, rmp->r_rrq.rmp_retcode,
160 t, ntohs(rmp->r_rrq.rmp_session));
161 (void) fprintf(DbgFp, "\t\tNoOfBytes: %u\n",
162 ntohs(rmp->r_rrq.rmp_size));
163 break;
164 case RMP_READ_REPL: /* read reply */
165 (void) fprintf(DbgFp, "\tRead Reply:\n");
166 GETWORD(rmp->r_rrpl.rmp_offset, t);
167 (void) fprintf(DbgFp, ReadFmt, rmp->r_rrpl.rmp_retcode,
168 t, ntohs(rmp->r_rrpl.rmp_session));
169 (void) fprintf(DbgFp, "\t\tNoOfBytesSent: %zu\n",
170 rconn->rmplen - RMPREADSIZE(0));
171 break;
172 case RMP_BOOT_DONE: /* boot complete */
173 (void) fprintf(DbgFp, "\tBoot Complete:\n");
174 (void) fprintf(DbgFp, "\t\tRetCode:%u SessID:%x\n",
175 rmp->r_done.rmp_retcode,
176 ntohs(rmp->r_done.rmp_session));
177 break;
178 default: /* ??? */
179 (void) fprintf(DbgFp, "\tUnknown Type:(%d)\n",
180 rmp->r_type);
181 }
182 (void) fputc('\n', DbgFp);
183 (void) fflush(DbgFp);
184
185 (void) sigsetmask(omask); /* reset old signal mask */
186 }
187
188
189 /*
190 ** GetEtherAddr -- convert an RMP (Ethernet) address into a string.
191 **
192 ** An RMP BOOT packet has been received. Look at the type field
193 ** and process Boot Requests, Read Requests, and Boot Complete
194 ** packets. Any other type will be dropped with a warning msg.
195 **
196 ** Parameters:
197 ** addr - array of RMP_ADDRLEN bytes.
198 **
199 ** Returns:
200 ** Pointer to static string representation of `addr'.
201 **
202 ** Side Effects:
203 ** None.
204 **
205 ** Warnings:
206 ** - The return value points to a static buffer; it must
207 ** be copied if it's to be saved.
208 */
209 char *
GetEtherAddr(u_int8_t * addr)210 GetEtherAddr(u_int8_t *addr)
211 {
212 static char Hex[] = "0123456789abcdef";
213 static char etherstr[RMP_ADDRLEN*3];
214 int i;
215 char *cp;
216
217 /*
218 * For each byte in `addr', convert it to "<hexchar><hexchar>:".
219 * The last byte does not get a trailing `:' appended.
220 */
221 i = 0;
222 cp = etherstr;
223 for(;;) {
224 *cp++ = Hex[*addr >> 4 & 0xf];
225 *cp++ = Hex[*addr++ & 0xf];
226 if (++i == RMP_ADDRLEN)
227 break;
228 *cp++ = ':';
229 }
230 *cp = '\0';
231
232 return(etherstr);
233 }
234
235
236 /*
237 ** DispFlnm -- Print a string of bytes to DbgFp (often, a file name).
238 **
239 ** Parameters:
240 ** size - number of bytes to print.
241 ** flnm - address of first byte.
242 **
243 ** Returns:
244 ** Nothing.
245 **
246 ** Side Effects:
247 ** - Characters are sent to `DbgFp'.
248 */
249 void
DspFlnm(u_int size,char * flnm)250 DspFlnm(u_int size, char *flnm)
251 {
252 int i;
253
254 (void) fprintf(DbgFp, "\n\t\tFile Name (%u): <", size);
255 for (i = 0; i < size; i++)
256 (void) fputc(*flnm++, DbgFp);
257 (void) fputs(">\n", DbgFp);
258 }
259
260
261 /*
262 ** NewClient -- allocate memory for a new CLIENT.
263 **
264 ** Parameters:
265 ** addr - RMP (Ethernet) address of new client.
266 **
267 ** Returns:
268 ** Ptr to new CLIENT or NULL if we ran out of memory.
269 **
270 ** Side Effects:
271 ** - Memory will be malloc'd for the new CLIENT.
272 ** - If malloc() fails, a log message will be generated.
273 */
274 CLIENT *
NewClient(u_int8_t * addr)275 NewClient(u_int8_t *addr)
276 {
277 CLIENT *ctmp;
278
279 if ((ctmp = (CLIENT *) malloc(sizeof(CLIENT))) == NULL) {
280 syslog(LOG_ERR, "NewClient: out of memory (%s)",
281 GetEtherAddr(addr));
282 return(NULL);
283 }
284
285 memset(ctmp, 0, sizeof(CLIENT));
286 memmove(&ctmp->addr[0], addr, RMP_ADDRLEN);
287 return(ctmp);
288 }
289
290 /*
291 ** FreeClient -- free linked list of Clients.
292 **
293 ** Parameters:
294 ** None.
295 **
296 ** Returns:
297 ** Nothing.
298 **
299 ** Side Effects:
300 ** - All malloc'd memory associated with the linked list of
301 ** CLIENTS will be free'd; `Clients' will be set to NULL.
302 **
303 ** Warnings:
304 ** - This routine must be called with SIGHUP blocked.
305 */
306 void
FreeClients(void)307 FreeClients(void)
308 {
309 CLIENT *ctmp;
310
311 while (Clients != NULL) {
312 ctmp = Clients;
313 Clients = Clients->next;
314 FreeClient(ctmp);
315 }
316 }
317
318 /*
319 ** NewStr -- allocate memory for a character array.
320 **
321 ** Parameters:
322 ** str - null terminated character array.
323 **
324 ** Returns:
325 ** Ptr to new character array or NULL if we ran out of memory.
326 **
327 ** Side Effects:
328 ** - Memory will be malloc'd for the new character array.
329 ** - If malloc() fails, a log message will be generated.
330 */
331 char *
NewStr(char * str)332 NewStr(char *str)
333 {
334 char *stmp;
335
336 if ((stmp = (char *)malloc((unsigned) (strlen(str)+1))) == NULL) {
337 syslog(LOG_ERR, "NewStr: out of memory (%s)", str);
338 return(NULL);
339 }
340
341 (void) strcpy(stmp, str);
342 return(stmp);
343 }
344
345 /*
346 ** To save time, NewConn and FreeConn maintain a cache of one RMPCONN
347 ** in `LastFree' (defined below).
348 */
349
350 static RMPCONN *LastFree = NULL;
351
352 /*
353 ** NewConn -- allocate memory for a new RMPCONN connection.
354 **
355 ** Parameters:
356 ** rconn - initialization template for new connection.
357 **
358 ** Returns:
359 ** Ptr to new RMPCONN or NULL if we ran out of memory.
360 **
361 ** Side Effects:
362 ** - Memory may be malloc'd for the new RMPCONN (if not cached).
363 ** - If malloc() fails, a log message will be generated.
364 */
365 RMPCONN *
NewConn(RMPCONN * rconn)366 NewConn(RMPCONN *rconn)
367 {
368 RMPCONN *rtmp;
369
370 if (LastFree == NULL) { /* nothing cached; make a new one */
371 if ((rtmp = (RMPCONN *) malloc(sizeof(RMPCONN))) == NULL) {
372 syslog(LOG_ERR, "NewConn: out of memory (%s)",
373 EnetStr(rconn));
374 return(NULL);
375 }
376 } else { /* use the cached RMPCONN */
377 rtmp = LastFree;
378 LastFree = NULL;
379 }
380
381 /*
382 * Copy template into `rtmp', init file descriptor to `-1' and
383 * set ptr to next elem NULL.
384 */
385 memmove((char *)rtmp, (char *)rconn, sizeof(RMPCONN));
386 rtmp->bootfd = -1;
387 rtmp->next = NULL;
388
389 return(rtmp);
390 }
391
392 /*
393 ** FreeConn -- Free memory associated with an RMPCONN connection.
394 **
395 ** Parameters:
396 ** rtmp - ptr to RMPCONN to be free'd.
397 **
398 ** Returns:
399 ** Nothing.
400 **
401 ** Side Effects:
402 ** - Memory associated with `rtmp' may be free'd (or cached).
403 ** - File desc associated with `rtmp->bootfd' will be closed.
404 */
405 void
FreeConn(RMPCONN * rtmp)406 FreeConn(RMPCONN *rtmp)
407 {
408 /*
409 * If the file descriptor is in use, close the file.
410 */
411 if (rtmp->bootfd >= 0) {
412 (void) close(rtmp->bootfd);
413 rtmp->bootfd = -1;
414 }
415
416 if (LastFree == NULL) /* cache for next time */
417 rtmp = LastFree;
418 else /* already one cached; free this one */
419 free((char *)rtmp);
420 }
421
422 /*
423 ** FreeConns -- free linked list of RMPCONN connections.
424 **
425 ** Parameters:
426 ** None.
427 **
428 ** Returns:
429 ** Nothing.
430 **
431 ** Side Effects:
432 ** - All malloc'd memory associated with the linked list of
433 ** connections will be free'd; `RmpConns' will be set to NULL.
434 ** - If LastFree is != NULL, it too will be free'd & NULL'd.
435 **
436 ** Warnings:
437 ** - This routine must be called with SIGHUP blocked.
438 */
439 void
FreeConns(void)440 FreeConns(void)
441 {
442 RMPCONN *rtmp;
443
444 while (RmpConns != NULL) {
445 rtmp = RmpConns;
446 RmpConns = RmpConns->next;
447 FreeConn(rtmp);
448 }
449
450 if (LastFree != NULL) {
451 free((char *)LastFree);
452 LastFree = NULL;
453 }
454 }
455
456 /*
457 ** AddConn -- Add a connection to the linked list of connections.
458 **
459 ** Parameters:
460 ** rconn - connection to be added.
461 **
462 ** Returns:
463 ** Nothing.
464 **
465 ** Side Effects:
466 ** - RmpConn will point to new connection.
467 **
468 ** Warnings:
469 ** - This routine must be called with SIGHUP blocked.
470 */
471 void
AddConn(RMPCONN * rconn)472 AddConn(RMPCONN *rconn)
473 {
474 if (RmpConns != NULL)
475 rconn->next = RmpConns;
476 RmpConns = rconn;
477 }
478
479 /*
480 ** FindConn -- Find a connection in the linked list of connections.
481 **
482 ** We use the RMP (Ethernet) address as the basis for determining
483 ** if this is the same connection. According to the Remote Maint
484 ** Protocol, we can only have one connection with any machine.
485 **
486 ** Parameters:
487 ** rconn - connection to be found.
488 **
489 ** Returns:
490 ** Matching connection from linked list or NULL if not found.
491 **
492 ** Side Effects:
493 ** None.
494 **
495 ** Warnings:
496 ** - This routine must be called with SIGHUP blocked.
497 */
498 RMPCONN *
FindConn(RMPCONN * rconn)499 FindConn(RMPCONN *rconn)
500 {
501 RMPCONN *rtmp;
502
503 for (rtmp = RmpConns; rtmp != NULL; rtmp = rtmp->next)
504 if (bcmp((char *)&rconn->rmp.hp_hdr.saddr[0],
505 (char *)&rtmp->rmp.hp_hdr.saddr[0], RMP_ADDRLEN) == 0)
506 break;
507
508 return(rtmp);
509 }
510
511 /*
512 ** RemoveConn -- Remove a connection from the linked list of connections.
513 **
514 ** Parameters:
515 ** rconn - connection to be removed.
516 **
517 ** Returns:
518 ** Nothing.
519 **
520 ** Side Effects:
521 ** - If found, an RMPCONN will cease to exist and it will
522 ** be removed from the linked list.
523 **
524 ** Warnings:
525 ** - This routine must be called with SIGHUP blocked.
526 */
527 void
RemoveConn(RMPCONN * rconn)528 RemoveConn(RMPCONN *rconn)
529 {
530 RMPCONN *thisrconn, *lastrconn;
531
532 if (RmpConns == rconn) { /* easy case */
533 RmpConns = RmpConns->next;
534 FreeConn(rconn);
535 } else { /* must traverse linked list */
536 lastrconn = RmpConns; /* set back ptr */
537 thisrconn = lastrconn->next; /* set current ptr */
538 while (thisrconn != NULL) {
539 if (rconn == thisrconn) { /* found it */
540 lastrconn->next = thisrconn->next;
541 FreeConn(thisrconn);
542 break;
543 }
544 lastrconn = thisrconn;
545 thisrconn = thisrconn->next;
546 }
547 }
548 }
549