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