process.c revision 1.7 1 /* $NetBSD: process.c,v 1.7 2000/06/27 18:57:41 ragge Exp $ */
2
3 /*
4 * Copyright (c) 1993-95 Mats O Jansson. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Mats O Jansson.
17 * 4. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __RCSID("$NetBSD: process.c,v 1.7 2000/06/27 18:57:41 ragge Exp $");
35 #endif
36
37 #include "os.h"
38 #include "cmp.h"
39 #include "common.h"
40 #include "dl.h"
41 #include "file.h"
42 #include "get.h"
43 #include "mopdef.h"
44 #include "nmadef.h"
45 #include "pf.h"
46 #include "print.h"
47 #include "put.h"
48 #include "rc.h"
49
50 extern u_char buf[];
51 extern int DebugFlag;
52
53 struct dllist dllist[MAXDL]; /* dump/load list */
54 extern char dl_mcst[]; /* Dump/Load Multicast */
55 extern char rc_mcst[]; /* Remote Console Multicast */
56
57 void mopNextLoad __P((u_char *, u_char *, u_char, int));
58 void mopProcessDL __P((FILE *, struct if_info *, u_char *, int *,
59 u_char *, u_char *, int, u_short));
60 void mopProcessRC __P((FILE *, struct if_info *, u_char *, int *,
61 u_char *, u_char *, int, u_short));
62 void mopProcessInfo __P((u_char *, int *, u_short, struct dllist *, int));
63 void mopSendASV __P((u_char *, u_char *, struct if_info *, int));
64 void mopStartLoad __P((u_char *, u_char *, struct dllist *, int));
65
66 void
67 mopProcessInfo(pkt, index, moplen, dl_rpr, trans)
68 u_char *pkt;
69 int *index;
70 u_short moplen;
71 struct dllist *dl_rpr;
72 int trans;
73 {
74 u_short itype,tmps;
75 u_char ilen ,tmpc,device;
76 u_char uc1,uc2,uc3,*ucp;
77
78 device = 0;
79
80 switch(trans) {
81 case TRANS_ETHER:
82 moplen = moplen + 16;
83 break;
84 case TRANS_8023:
85 moplen = moplen + 14;
86 break;
87 }
88
89 itype = mopGetShort(pkt,index);
90
91 while (*index < (int)(moplen)) {
92 ilen = mopGetChar(pkt,index);
93 switch (itype) {
94 case 0:
95 tmpc = mopGetChar(pkt,index);
96 *index = *index + tmpc;
97 break;
98 case MOP_K_INFO_VER:
99 uc1 = mopGetChar(pkt,index);
100 uc2 = mopGetChar(pkt,index);
101 uc3 = mopGetChar(pkt,index);
102 break;
103 case MOP_K_INFO_MFCT:
104 tmps = mopGetShort(pkt,index);
105 break;
106 case MOP_K_INFO_CNU:
107 ucp = pkt + *index; *index = *index + 6;
108 break;
109 case MOP_K_INFO_RTM:
110 tmps = mopGetShort(pkt,index);
111 break;
112 case MOP_K_INFO_CSZ:
113 tmps = mopGetShort(pkt,index);
114 break;
115 case MOP_K_INFO_RSZ:
116 tmps = mopGetShort(pkt,index);
117 break;
118 case MOP_K_INFO_HWA:
119 ucp = pkt + *index; *index = *index + 6;
120 break;
121 case MOP_K_INFO_TIME:
122 ucp = pkt + *index; *index = *index + 10;
123 break;
124 case MOP_K_INFO_SOFD:
125 device = mopGetChar(pkt,index);
126 break;
127 case MOP_K_INFO_SFID:
128 tmpc = mopGetChar(pkt,index);
129 ucp = pkt + *index; *index = *index + tmpc;
130 break;
131 case MOP_K_INFO_PRTY:
132 tmpc = mopGetChar(pkt,index);
133 break;
134 case MOP_K_INFO_DLTY:
135 tmpc = mopGetChar(pkt,index);
136 break;
137 case MOP_K_INFO_DLBSZ:
138 tmps = mopGetShort(pkt,index);
139 dl_rpr->dl_bsz = tmps;
140 break;
141 default:
142 if (((device = NMA_C_SOFD_LCS) || /* DECserver 100 */
143 (device = NMA_C_SOFD_DS2) || /* DECserver 200 */
144 (device = NMA_C_SOFD_DP2) || /* DECserver 250 */
145 (device = NMA_C_SOFD_DS3)) && /* DECserver 300 */
146 ((itype > 101) && (itype < 107)))
147 {
148 switch (itype) {
149 case 102:
150 ucp = pkt + *index;
151 *index = *index + ilen;
152 break;
153 case 103:
154 ucp = pkt + *index;
155 *index = *index + ilen;
156 break;
157 case 104:
158 tmps = mopGetShort(pkt,index);
159 break;
160 case 105:
161 ucp = pkt + *index;
162 *index = *index + ilen;
163 break;
164 case 106:
165 ucp = pkt + *index;
166 *index = *index + ilen;
167 break;
168 };
169 } else {
170 ucp = pkt + *index; *index = *index + ilen;
171 };
172 }
173 itype = mopGetShort(pkt,index);
174 }
175 }
176
177 void
178 mopSendASV(dst, src, ii, trans)
179 u_char *dst,*src;
180 struct if_info *ii;
181 int trans;
182 {
183 u_char pkt[200], *p;
184 int index;
185 u_char mopcode = MOP_K_CODE_ASV;
186 u_short newlen = 0,ptype = MOP_K_PROTO_DL;
187
188 index = 0;
189 mopPutHeader(pkt, &index, dst, src, ptype, trans);
190
191 p = &pkt[index];
192 mopPutChar(pkt,&index,mopcode);
193
194 mopPutLength(pkt, trans, index);
195 newlen = mopGetLength(pkt, trans);
196
197 if ((DebugFlag == DEBUG_ONELINE)) {
198 mopPrintOneline(stdout, pkt, trans);
199 }
200
201 if ((DebugFlag >= DEBUG_HEADER)) {
202 mopPrintHeader(stdout, pkt, trans);
203 mopPrintMopHeader(stdout, pkt, trans);
204 }
205
206 if ((DebugFlag >= DEBUG_INFO)) {
207 mopDumpDL(stdout, pkt, trans);
208 }
209
210 if (pfWrite(ii->fd, pkt, index, trans) != index) {
211 if (DebugFlag) {
212 (void)fprintf(stderr, "error pfWrite()\n");
213 }
214 }
215 }
216
217 void
218 mopStartLoad(dst, src, dl_rpr, trans)
219 u_char *dst,*src;
220 struct dllist *dl_rpr;
221 int trans;
222 {
223 int len;
224 int i, slot;
225 u_char pkt[BUFSIZE], *p;
226 int index;
227 u_char mopcode = MOP_K_CODE_MLD;
228 u_short newlen,ptype = MOP_K_PROTO_DL;
229
230 slot = -1;
231
232 /* Look if we have a non terminated load, if so, use it's slot */
233
234 for (i = 0; i < MAXDL; i++) {
235 if (dllist[i].status != DL_STATUS_FREE) {
236 if (mopCmpEAddr(dllist[i].eaddr,dst) == 0) {
237 slot = i;
238 }
239 }
240 }
241
242 /* If no slot yet, then find first free */
243
244 if (slot == -1) {
245 for (i = 0; i < MAXDL; i++) {
246 if (dllist[i].status == DL_STATUS_FREE) {
247 if (slot == -1) {
248 slot = i;
249 memmove((char *)dllist[i].eaddr,
250 (char *)dst, 6);
251 }
252 }
253 }
254 }
255
256 /* If no slot yet, then return. No slot is free */
257
258 if (slot == -1)
259 return;
260
261 /* Ok, save info from RPR */
262
263 dllist[slot] = *dl_rpr;
264 dllist[slot].status = DL_STATUS_READ_IMGHDR;
265
266 /* Get Load and Transfer Address. */
267
268 GetFileInfo(dllist[slot].ldfd,
269 &dllist[slot].loadaddr,
270 &dllist[slot].xferaddr,
271 &dllist[slot].aout,
272 &dllist[slot].a_text, &dllist[slot].a_text_fill,
273 &dllist[slot].a_data, &dllist[slot].a_data_fill,
274 &dllist[slot].a_bss, &dllist[slot].a_bss_fill);
275
276 dllist[slot].nloadaddr = dllist[slot].loadaddr;
277 dllist[slot].lseek = lseek(dllist[slot].ldfd,0L,SEEK_CUR);
278 dllist[slot].a_lseek = 0;
279
280 dllist[slot].count = 0;
281 if (dllist[slot].dl_bsz >= 1492)
282 dllist[slot].dl_bsz = 1492;
283 if (dllist[slot].dl_bsz == 1030) /* VS/uVAX 2000 needs this */
284 dllist[slot].dl_bsz = 1000;
285 if (dllist[slot].dl_bsz == 0) /* Needed by "big" VAXen */
286 dllist[slot].dl_bsz = 1492;
287 if (trans == TRANS_8023)
288 dllist[slot].dl_bsz = dllist[slot].dl_bsz - 8;
289
290 index = 0;
291 mopPutHeader(pkt, &index, dst, src, ptype, trans);
292 p = &pkt[index];
293 mopPutChar (pkt,&index,mopcode);
294
295 mopPutChar (pkt,&index,dllist[slot].count);
296 mopPutLong (pkt,&index,dllist[slot].loadaddr);
297
298 len = mopFileRead(&dllist[slot],&pkt[index]);
299
300 dllist[slot].nloadaddr = dllist[slot].loadaddr + len;
301 index = index + len;
302
303 mopPutLength(pkt, trans, index);
304 newlen = mopGetLength(pkt, trans);
305
306 if ((DebugFlag == DEBUG_ONELINE)) {
307 mopPrintOneline(stdout, pkt, trans);
308 }
309
310 if ((DebugFlag >= DEBUG_HEADER)) {
311 mopPrintHeader(stdout, pkt, trans);
312 mopPrintMopHeader(stdout, pkt, trans);
313 }
314
315 if ((DebugFlag >= DEBUG_INFO)) {
316 mopDumpDL(stdout, pkt, trans);
317 }
318
319 if (pfWrite(dllist[slot].ii->fd, pkt, index, trans) != index) {
320 if (DebugFlag) {
321 (void)fprintf(stderr, "error pfWrite()\n");
322 }
323 }
324
325 dllist[slot].status = DL_STATUS_SENT_MLD;
326 }
327
328 void
329 mopNextLoad(dst, src, new_count, trans)
330 u_char *dst,*src,new_count;
331 int trans;
332 {
333 int len;
334 int i, slot;
335 u_char pkt[BUFSIZE], *p;
336 int index, pindex;
337 char line[100];
338 u_short newlen = 0,ptype = MOP_K_PROTO_DL;
339 u_char mopcode;
340
341 slot = -1;
342
343 for (i = 0; i < MAXDL; i++) {
344 if (dllist[i].status != DL_STATUS_FREE) {
345 if (mopCmpEAddr(dst,dllist[i].eaddr) == 0)
346 slot = i;
347 }
348 }
349
350 /* If no slot yet, then return. No slot is free */
351
352 if (slot == -1)
353 return;
354
355 if ((new_count == ((dllist[slot].count+1) % 256))) {
356 dllist[slot].loadaddr = dllist[slot].nloadaddr;
357 dllist[slot].count = new_count;
358 } else {
359 return;
360 }
361
362 if (dllist[slot].status == DL_STATUS_SENT_PLT) {
363 close(dllist[slot].ldfd);
364 dllist[slot].ldfd = 0;
365 dllist[slot].status = DL_STATUS_FREE;
366 sprintf(line,
367 "%x:%x:%x:%x:%x:%x Load completed",
368 dst[0],dst[1],dst[2],dst[3],dst[4],dst[5]);
369 syslog(LOG_INFO, line);
370 return;
371 }
372
373 dllist[slot].lseek = lseek(dllist[slot].ldfd,0L,SEEK_CUR);
374
375 if (dllist[slot].dl_bsz >= 1492)
376 dllist[slot].dl_bsz = 1492;
377
378 index = 0;
379 mopPutHeader(pkt, &index, dst, src, ptype, trans);
380 p = &pkt[index];
381 mopcode = MOP_K_CODE_MLD;
382 pindex = index;
383 mopPutChar (pkt,&index,mopcode);
384 mopPutChar (pkt,&index,dllist[slot].count);
385 mopPutLong (pkt,&index,dllist[slot].loadaddr);
386
387 len = mopFileRead(&dllist[slot],&pkt[index]);
388
389 if (len > 0 ) {
390
391 dllist[slot].nloadaddr = dllist[slot].loadaddr + len;
392 index = index + len;
393
394 mopPutLength(pkt, trans, index);
395 newlen = mopGetLength(pkt, trans);
396
397 } else {
398 if (len == 0) {
399 index = pindex;
400 mopcode = MOP_K_CODE_PLT;
401 mopPutChar (pkt,&index,mopcode);
402 mopPutChar (pkt,&index,dllist[slot].count);
403 mopPutChar (pkt,&index,MOP_K_PLTP_HSN);
404 mopPutChar (pkt,&index,3);
405 mopPutMulti(pkt,&index,"ipc",3);
406 mopPutChar (pkt,&index,MOP_K_PLTP_HSA);
407 mopPutChar (pkt,&index,6);
408 mopPutMulti(pkt,&index,src,6);
409 mopPutChar (pkt,&index,MOP_K_PLTP_HST);
410 mopPutTime (pkt,&index, 0);
411 mopPutChar (pkt,&index,0);
412 mopPutLong (pkt,&index,dllist[slot].xferaddr);
413
414 mopPutLength(pkt, trans, index);
415 newlen = mopGetLength(pkt, trans);
416
417 dllist[slot].status = DL_STATUS_SENT_PLT;
418 } else {
419 dllist[slot].status = DL_STATUS_FREE;
420 return;
421 }
422 }
423
424 if ((DebugFlag == DEBUG_ONELINE)) {
425 mopPrintOneline(stdout, pkt, trans);
426 }
427
428 if ((DebugFlag >= DEBUG_HEADER)) {
429 mopPrintHeader(stdout, pkt, trans);
430 mopPrintMopHeader(stdout, pkt, trans);
431 }
432
433 if ((DebugFlag >= DEBUG_INFO)) {
434 mopDumpDL(stdout, pkt, trans);
435 }
436
437 if (pfWrite(dllist[slot].ii->fd, pkt, index, trans) != index) {
438 if (DebugFlag) {
439 (void)fprintf(stderr, "error pfWrite()\n");
440 }
441 }
442 }
443
444 void
445 mopProcessDL(fd, ii, pkt, index, dst, src, trans, len)
446 FILE *fd;
447 struct if_info *ii;
448 u_char *pkt;
449 int *index;
450 u_char *dst, *src;
451 int trans;
452 u_short len;
453 {
454 u_char tmpc;
455 u_short moplen;
456 u_char pfile[17], mopcode;
457 char filename[FILENAME_MAX];
458 char line[100];
459 int i,nfd,iindex;
460 struct dllist dl,*dl_rpr;
461 u_char rpr_pgty,load;
462
463 if ((DebugFlag == DEBUG_ONELINE)) {
464 mopPrintOneline(stdout, pkt, trans);
465 }
466
467 if ((DebugFlag >= DEBUG_HEADER)) {
468 mopPrintHeader(stdout, pkt, trans);
469 mopPrintMopHeader(stdout, pkt, trans);
470 }
471
472 if ((DebugFlag >= DEBUG_INFO)) {
473 mopDumpDL(stdout, pkt, trans);
474 }
475
476 moplen = mopGetLength(pkt, trans);
477 mopcode = mopGetChar(pkt,index);
478
479 switch (mopcode) {
480 case MOP_K_CODE_MLT:
481 break;
482 case MOP_K_CODE_DCM:
483 break;
484 case MOP_K_CODE_MLD:
485 break;
486 case MOP_K_CODE_ASV:
487 break;
488 case MOP_K_CODE_RMD:
489 break;
490 case MOP_K_CODE_RPR:
491
492 tmpc = mopGetChar(pkt,index); /* Device Type */
493
494 tmpc = mopGetChar(pkt,index); /* Format Version */
495 if ((tmpc != MOP_K_RPR_FORMAT) &&
496 (tmpc != MOP_K_RPR_FORMAT_V3)) {
497 (void)fprintf(stderr,"mopd: Unknown RPR Format (%d) from ",tmpc);
498 mopPrintHWA(stderr,src);
499 (void)fprintf(stderr,"\n");
500 }
501
502 rpr_pgty = mopGetChar(pkt,index); /* Program Type */
503
504 tmpc = mopGetChar(pkt,index); /* Software ID Len */
505 for (i = 0; i < tmpc; i++) {
506 pfile[i] = mopGetChar(pkt,index);
507 pfile[i+1] = '\0';
508 }
509
510 if (tmpc == 0) {
511 /* In a normal implementation of a MOP Loader this */
512 /* would cause a question to NML (DECnet) if this */
513 /* node is known and if so what image to load. But */
514 /* we don't have DECnet so we don't have anybody */
515 /* to ask. My solution is to use the ethernet addr */
516 /* as filename. Implementing a database would be */
517 /* overkill. */
518 sprintf(pfile,"%02x%02x%02x%02x%02x%02x%c",
519 src[0],src[1],src[2],src[3],src[4],src[5],0);
520 }
521
522 tmpc = mopGetChar(pkt,index); /* Processor */
523
524 iindex = *index;
525 dl_rpr = &dl;
526 memset(dl_rpr, 0, sizeof(*dl_rpr));
527 dl_rpr->ii = ii;
528 memmove((char *)(dl_rpr->eaddr), (char *)src, 6);
529 mopProcessInfo(pkt,index,moplen,dl_rpr,trans);
530
531 sprintf(filename,"%s/%s.SYS", MOP_FILE_PATH, pfile);
532 if ((mopCmpEAddr(dst,dl_mcst) == 0)) {
533 if ((nfd = open(filename, O_RDONLY, 0)) != -1) {
534 close(nfd);
535 mopSendASV(src, ii->eaddr, ii, trans);
536 sprintf(line,
537 "%x:%x:%x:%x:%x:%x (%d) Do you have %s? (Yes)",
538 src[0],src[1],src[2],
539 src[3],src[4],src[5],trans,pfile);
540 } else {
541 sprintf(line,
542 "%x:%x:%x:%x:%x:%x (%d) Do you have %s? (No)",
543 src[0],src[1],src[2],
544 src[3],src[4],src[5],trans,pfile);
545 }
546 syslog(LOG_INFO, line);
547 } else {
548 if ((mopCmpEAddr(dst,ii->eaddr) == 0)) {
549 dl_rpr->ldfd = open(filename, O_RDONLY, 0);
550 mopStartLoad(src, ii->eaddr, dl_rpr, trans);
551 sprintf(line,
552 "%x:%x:%x:%x:%x:%x Send me %s",
553 src[0],src[1],src[2],
554 src[3],src[4],src[5],pfile);
555 syslog(LOG_INFO, line);
556 }
557 }
558
559 break;
560 case MOP_K_CODE_RML:
561
562 load = mopGetChar(pkt,index); /* Load Number */
563
564 tmpc = mopGetChar(pkt,index); /* Error */
565
566 if ((mopCmpEAddr(dst,ii->eaddr) == 0)) {
567 mopNextLoad(src, ii->eaddr, load, trans);
568 }
569
570 break;
571 case MOP_K_CODE_RDS:
572 break;
573 case MOP_K_CODE_MDD:
574 break;
575 case MOP_K_CODE_CCP:
576 break;
577 case MOP_K_CODE_PLT:
578 break;
579 default:
580 break;
581 }
582 }
583
584 void
585 mopProcessRC(fd, ii, pkt, index, dst, src, trans, len)
586 FILE *fd;
587 struct if_info *ii;
588 u_char *pkt;
589 int *index;
590 u_char *dst, *src;
591 int trans;
592 u_short len;
593 {
594 u_char tmpc;
595 u_short tmps, moplen = 0;
596 u_char mopcode;
597 struct dllist dl,*dl_rpr;
598
599 if ((DebugFlag == DEBUG_ONELINE)) {
600 mopPrintOneline(stdout, pkt, trans);
601 }
602
603 if ((DebugFlag >= DEBUG_HEADER)) {
604 mopPrintHeader(stdout, pkt, trans);
605 mopPrintMopHeader(stdout, pkt, trans);
606 }
607
608 if ((DebugFlag >= DEBUG_INFO)) {
609 mopDumpRC(stdout, pkt, trans);
610 }
611
612 moplen = mopGetLength(pkt, trans);
613 mopcode = mopGetChar(pkt,index);
614
615 switch (mopcode) {
616 case MOP_K_CODE_RID:
617 break;
618 case MOP_K_CODE_BOT:
619 break;
620 case MOP_K_CODE_SID:
621
622 tmpc = mopGetChar(pkt,index); /* Reserved */
623
624 if ((DebugFlag >= DEBUG_INFO)) {
625 (void)fprintf(stderr, "Reserved : %02x\n",tmpc);
626 }
627
628 tmps = mopGetShort(pkt,index); /* Receipt # */
629 if ((DebugFlag >= DEBUG_INFO)) {
630 (void)fprintf(stderr, "Receipt Nbr : %04x\n",tmpc);
631 }
632
633 dl_rpr = &dl;
634 memset(dl_rpr, 0, sizeof(*dl_rpr));
635 dl_rpr->ii = ii;
636 memmove((char *)(dl_rpr->eaddr), (char *)src, 6);
637 mopProcessInfo(pkt,index,moplen,dl_rpr,trans);
638
639 break;
640 case MOP_K_CODE_RQC:
641 break;
642 case MOP_K_CODE_CNT:
643 break;
644 case MOP_K_CODE_RVC:
645 break;
646 case MOP_K_CODE_RLC:
647 break;
648 case MOP_K_CODE_CCP:
649 break;
650 case MOP_K_CODE_CRA:
651 break;
652 default:
653 break;
654 }
655 }
656
657