process.c revision 1.8 1 /* $NetBSD: process.c,v 1.8 2000/07/21 12:37:00 mjl 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.8 2000/07/21 12:37:00 mjl 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 #define MAX_ETH_PAYLOAD 1492
218
219 void
220 mopStartLoad(dst, src, dl_rpr, trans)
221 u_char *dst,*src;
222 struct dllist *dl_rpr;
223 int trans;
224 {
225 int len;
226 int i, slot;
227 u_char pkt[BUFSIZE], *p;
228 int index;
229 u_char mopcode = MOP_K_CODE_MLD;
230 u_short newlen,ptype = MOP_K_PROTO_DL;
231
232 slot = -1;
233
234 /* Look if we have a non terminated load, if so, use it's slot */
235
236 for (i = 0; i < MAXDL; i++) {
237 if (dllist[i].status != DL_STATUS_FREE) {
238 if (mopCmpEAddr(dllist[i].eaddr,dst) == 0) {
239 slot = i;
240 }
241 }
242 }
243
244 /* If no slot yet, then find first free */
245
246 if (slot == -1) {
247 for (i = 0; i < MAXDL; i++) {
248 if (dllist[i].status == DL_STATUS_FREE) {
249 if (slot == -1) {
250 slot = i;
251 memmove((char *)dllist[i].eaddr,
252 (char *)dst, 6);
253 }
254 }
255 }
256 }
257
258 /* If no slot yet, then return. No slot is free */
259
260 if (slot == -1)
261 return;
262
263 /* Ok, save info from RPR */
264
265 dllist[slot] = *dl_rpr;
266 dllist[slot].status = DL_STATUS_READ_IMGHDR;
267
268 /* Get Load and Transfer Address. */
269
270 GetFileInfo(dllist[slot].ldfd,
271 &dllist[slot].loadaddr,
272 &dllist[slot].xferaddr,
273 &dllist[slot].aout,
274 &dllist[slot].a_text, &dllist[slot].a_text_fill,
275 &dllist[slot].a_data, &dllist[slot].a_data_fill,
276 &dllist[slot].a_bss, &dllist[slot].a_bss_fill);
277
278 dllist[slot].nloadaddr = dllist[slot].loadaddr;
279 dllist[slot].lseek = lseek(dllist[slot].ldfd,0L,SEEK_CUR);
280 dllist[slot].a_lseek = 0;
281
282 dllist[slot].count = 0;
283 if (dllist[slot].dl_bsz >= MAX_ETH_PAYLOAD || dllist[slot].dl_bsz == 0)
284 dllist[slot].dl_bsz = MAX_ETH_PAYLOAD;
285 if (dllist[slot].dl_bsz == 1030) /* VS/uVAX 2000 needs this */
286 dllist[slot].dl_bsz = 1000;
287 if (dllist[slot].dl_bsz == 0) /* Needed by "big" VAXen */
288 dllist[slot].dl_bsz = MAX_ETH_PAYLOAD;
289 if (trans == TRANS_8023)
290 dllist[slot].dl_bsz = dllist[slot].dl_bsz - 8;
291
292 index = 0;
293 mopPutHeader(pkt, &index, dst, src, ptype, trans);
294 p = &pkt[index];
295 mopPutChar (pkt,&index,mopcode);
296
297 mopPutChar (pkt,&index,dllist[slot].count);
298 mopPutLong (pkt,&index,dllist[slot].loadaddr);
299
300 len = mopFileRead(&dllist[slot],&pkt[index]);
301
302 dllist[slot].nloadaddr = dllist[slot].loadaddr + len;
303 index = index + len;
304
305 mopPutLength(pkt, trans, index);
306 newlen = mopGetLength(pkt, trans);
307
308 if ((DebugFlag == DEBUG_ONELINE)) {
309 mopPrintOneline(stdout, pkt, trans);
310 }
311
312 if ((DebugFlag >= DEBUG_HEADER)) {
313 mopPrintHeader(stdout, pkt, trans);
314 mopPrintMopHeader(stdout, pkt, trans);
315 }
316
317 if ((DebugFlag >= DEBUG_INFO)) {
318 mopDumpDL(stdout, pkt, trans);
319 }
320
321 if (pfWrite(dllist[slot].ii->fd, pkt, index, trans) != index) {
322 if (DebugFlag) {
323 (void)fprintf(stderr, "error pfWrite()\n");
324 }
325 }
326
327 dllist[slot].status = DL_STATUS_SENT_MLD;
328 }
329
330 void
331 mopNextLoad(dst, src, new_count, trans)
332 u_char *dst,*src,new_count;
333 int trans;
334 {
335 int len;
336 int i, slot;
337 u_char pkt[BUFSIZE], *p;
338 int index, pindex;
339 char line[100];
340 u_short newlen = 0,ptype = MOP_K_PROTO_DL;
341 u_char mopcode;
342
343 slot = -1;
344
345 for (i = 0; i < MAXDL; i++) {
346 if (dllist[i].status != DL_STATUS_FREE) {
347 if (mopCmpEAddr(dst,dllist[i].eaddr) == 0)
348 slot = i;
349 }
350 }
351
352 /* If no slot yet, then return. No slot is free */
353
354 if (slot == -1)
355 return;
356
357 if ((new_count == ((dllist[slot].count+1) % 256))) {
358 dllist[slot].loadaddr = dllist[slot].nloadaddr;
359 dllist[slot].count = new_count;
360 } else {
361 return;
362 }
363
364 if (dllist[slot].status == DL_STATUS_SENT_PLT) {
365 close(dllist[slot].ldfd);
366 dllist[slot].ldfd = 0;
367 dllist[slot].status = DL_STATUS_FREE;
368 sprintf(line,
369 "%x:%x:%x:%x:%x:%x Load completed",
370 dst[0],dst[1],dst[2],dst[3],dst[4],dst[5]);
371 syslog(LOG_INFO, "%s", line);
372 return;
373 }
374
375 dllist[slot].lseek = lseek(dllist[slot].ldfd,0L,SEEK_CUR);
376
377 if (dllist[slot].dl_bsz >= MAX_ETH_PAYLOAD)
378 dllist[slot].dl_bsz = MAX_ETH_PAYLOAD;
379
380 index = 0;
381 mopPutHeader(pkt, &index, dst, src, ptype, trans);
382 p = &pkt[index];
383 mopcode = MOP_K_CODE_MLD;
384 pindex = index;
385 mopPutChar (pkt,&index,mopcode);
386 mopPutChar (pkt,&index,dllist[slot].count);
387 mopPutLong (pkt,&index,dllist[slot].loadaddr);
388
389 len = mopFileRead(&dllist[slot],&pkt[index]);
390
391 if (len > 0 ) {
392
393 dllist[slot].nloadaddr = dllist[slot].loadaddr + len;
394 index = index + len;
395
396 mopPutLength(pkt, trans, index);
397 newlen = mopGetLength(pkt, trans);
398
399 } else {
400 if (len == 0) {
401 index = pindex;
402 mopcode = MOP_K_CODE_PLT;
403 mopPutChar (pkt,&index,mopcode);
404 mopPutChar (pkt,&index,dllist[slot].count);
405 mopPutChar (pkt,&index,MOP_K_PLTP_HSN);
406 mopPutChar (pkt,&index,3);
407 mopPutMulti(pkt,&index,"ipc",3);
408 mopPutChar (pkt,&index,MOP_K_PLTP_HSA);
409 mopPutChar (pkt,&index,6);
410 mopPutMulti(pkt,&index,src,6);
411 mopPutChar (pkt,&index,MOP_K_PLTP_HST);
412 mopPutTime (pkt,&index, 0);
413 mopPutChar (pkt,&index,0);
414 mopPutLong (pkt,&index,dllist[slot].xferaddr);
415
416 mopPutLength(pkt, trans, index);
417 newlen = mopGetLength(pkt, trans);
418
419 dllist[slot].status = DL_STATUS_SENT_PLT;
420 } else {
421 dllist[slot].status = DL_STATUS_FREE;
422 return;
423 }
424 }
425
426 if ((DebugFlag == DEBUG_ONELINE)) {
427 mopPrintOneline(stdout, pkt, trans);
428 }
429
430 if ((DebugFlag >= DEBUG_HEADER)) {
431 mopPrintHeader(stdout, pkt, trans);
432 mopPrintMopHeader(stdout, pkt, trans);
433 }
434
435 if ((DebugFlag >= DEBUG_INFO)) {
436 mopDumpDL(stdout, pkt, trans);
437 }
438
439 if (pfWrite(dllist[slot].ii->fd, pkt, index, trans) != index) {
440 if (DebugFlag) {
441 (void)fprintf(stderr, "error pfWrite()\n");
442 }
443 }
444 }
445
446 void
447 mopProcessDL(fd, ii, pkt, index, dst, src, trans, len)
448 FILE *fd;
449 struct if_info *ii;
450 u_char *pkt;
451 int *index;
452 u_char *dst, *src;
453 int trans;
454 u_short len;
455 {
456 u_char tmpc;
457 u_short moplen;
458 u_char pfile[129], mopcode;
459 char filename[FILENAME_MAX];
460 char line[100];
461 int i,nfd,iindex;
462 struct dllist dl,*dl_rpr;
463 u_char rpr_pgty,load;
464
465 if ((DebugFlag == DEBUG_ONELINE)) {
466 mopPrintOneline(stdout, pkt, trans);
467 }
468
469 if ((DebugFlag >= DEBUG_HEADER)) {
470 mopPrintHeader(stdout, pkt, trans);
471 mopPrintMopHeader(stdout, pkt, trans);
472 }
473
474 if ((DebugFlag >= DEBUG_INFO)) {
475 mopDumpDL(stdout, pkt, trans);
476 }
477
478 moplen = mopGetLength(pkt, trans);
479 mopcode = mopGetChar(pkt,index);
480
481 switch (mopcode) {
482 case MOP_K_CODE_MLT:
483 break;
484 case MOP_K_CODE_DCM:
485 break;
486 case MOP_K_CODE_MLD:
487 break;
488 case MOP_K_CODE_ASV:
489 break;
490 case MOP_K_CODE_RMD:
491 break;
492 case MOP_K_CODE_RPR:
493
494 tmpc = mopGetChar(pkt,index); /* Device Type */
495
496 tmpc = mopGetChar(pkt,index); /* Format Version */
497 if ((tmpc != MOP_K_RPR_FORMAT) &&
498 (tmpc != MOP_K_RPR_FORMAT_V3)) {
499 (void)fprintf(stderr,"mopd: Unknown RPR Format (%d) from ",tmpc);
500 mopPrintHWA(stderr,src);
501 (void)fprintf(stderr,"\n");
502 }
503
504 rpr_pgty = mopGetChar(pkt,index); /* Program Type */
505
506 tmpc = mopGetChar(pkt,index); /* Software ID Len */
507 if (tmpc > sizeof(pfile) - 1)
508 return;
509 for (i = 0; i < tmpc; i++) {
510 pfile[i] = mopGetChar(pkt,index);
511 pfile[i+1] = '\0';
512 }
513
514 if (tmpc == 0) {
515 /* In a normal implementation of a MOP Loader this */
516 /* would cause a question to NML (DECnet) if this */
517 /* node is known and if so what image to load. But */
518 /* we don't have DECnet so we don't have anybody */
519 /* to ask. My solution is to use the ethernet addr */
520 /* as filename. Implementing a database would be */
521 /* overkill. */
522 sprintf(pfile,"%02x%02x%02x%02x%02x%02x%c",
523 src[0],src[1],src[2],src[3],src[4],src[5],0);
524 }
525
526 tmpc = mopGetChar(pkt,index); /* Processor */
527
528 iindex = *index;
529 dl_rpr = &dl;
530 memset(dl_rpr, 0, sizeof(*dl_rpr));
531 dl_rpr->ii = ii;
532 memmove((char *)(dl_rpr->eaddr), (char *)src, 6);
533 mopProcessInfo(pkt,index,moplen,dl_rpr,trans);
534
535 sprintf(filename,"%s/%s.SYS", MOP_FILE_PATH, pfile);
536 if ((mopCmpEAddr(dst,dl_mcst) == 0)) {
537 if ((nfd = open(filename, O_RDONLY, 0)) != -1) {
538 close(nfd);
539 mopSendASV(src, ii->eaddr, ii, trans);
540 sprintf(line,
541 "%x:%x:%x:%x:%x:%x (%d) Do you have %s? (Yes)",
542 src[0],src[1],src[2],
543 src[3],src[4],src[5],trans,pfile);
544 } else {
545 sprintf(line,
546 "%x:%x:%x:%x:%x:%x (%d) Do you have %s? (No)",
547 src[0],src[1],src[2],
548 src[3],src[4],src[5],trans,pfile);
549 }
550 syslog(LOG_INFO, "%s", line);
551 } else {
552 if ((mopCmpEAddr(dst,ii->eaddr) == 0)) {
553 dl_rpr->ldfd = open(filename, O_RDONLY, 0);
554 mopStartLoad(src, ii->eaddr, dl_rpr, trans);
555 sprintf(line,
556 "%x:%x:%x:%x:%x:%x Send me %s",
557 src[0],src[1],src[2],
558 src[3],src[4],src[5],pfile);
559 syslog(LOG_INFO, "%s", line);
560 }
561 }
562
563 break;
564 case MOP_K_CODE_RML:
565
566 load = mopGetChar(pkt,index); /* Load Number */
567
568 tmpc = mopGetChar(pkt,index); /* Error */
569
570 if ((mopCmpEAddr(dst,ii->eaddr) == 0)) {
571 mopNextLoad(src, ii->eaddr, load, trans);
572 }
573
574 break;
575 case MOP_K_CODE_RDS:
576 break;
577 case MOP_K_CODE_MDD:
578 break;
579 case MOP_K_CODE_CCP:
580 break;
581 case MOP_K_CODE_PLT:
582 break;
583 default:
584 break;
585 }
586 }
587
588 void
589 mopProcessRC(fd, ii, pkt, index, dst, src, trans, len)
590 FILE *fd;
591 struct if_info *ii;
592 u_char *pkt;
593 int *index;
594 u_char *dst, *src;
595 int trans;
596 u_short len;
597 {
598 u_char tmpc;
599 u_short tmps, moplen = 0;
600 u_char mopcode;
601 struct dllist dl,*dl_rpr;
602
603 if ((DebugFlag == DEBUG_ONELINE)) {
604 mopPrintOneline(stdout, pkt, trans);
605 }
606
607 if ((DebugFlag >= DEBUG_HEADER)) {
608 mopPrintHeader(stdout, pkt, trans);
609 mopPrintMopHeader(stdout, pkt, trans);
610 }
611
612 if ((DebugFlag >= DEBUG_INFO)) {
613 mopDumpRC(stdout, pkt, trans);
614 }
615
616 moplen = mopGetLength(pkt, trans);
617 mopcode = mopGetChar(pkt,index);
618
619 switch (mopcode) {
620 case MOP_K_CODE_RID:
621 break;
622 case MOP_K_CODE_BOT:
623 break;
624 case MOP_K_CODE_SID:
625
626 tmpc = mopGetChar(pkt,index); /* Reserved */
627
628 if ((DebugFlag >= DEBUG_INFO)) {
629 (void)fprintf(stderr, "Reserved : %02x\n",tmpc);
630 }
631
632 tmps = mopGetShort(pkt,index); /* Receipt # */
633 if ((DebugFlag >= DEBUG_INFO)) {
634 (void)fprintf(stderr, "Receipt Nbr : %04x\n",tmpc);
635 }
636
637 dl_rpr = &dl;
638 memset(dl_rpr, 0, sizeof(*dl_rpr));
639 dl_rpr->ii = ii;
640 memmove((char *)(dl_rpr->eaddr), (char *)src, 6);
641 mopProcessInfo(pkt,index,moplen,dl_rpr,trans);
642
643 break;
644 case MOP_K_CODE_RQC:
645 break;
646 case MOP_K_CODE_CNT:
647 break;
648 case MOP_K_CODE_RVC:
649 break;
650 case MOP_K_CODE_RLC:
651 break;
652 case MOP_K_CODE_CCP:
653 break;
654 case MOP_K_CODE_CRA:
655 break;
656 default:
657 break;
658 }
659 }
660
661