xref: /csrg-svn/sys/deprecated/netimp/if_imp.c (revision 24777)
1 /*
2  * Copyright (c) 1982 Regents of the University of California.
3  * All rights reserved.  The Berkeley software License Agreement
4  * specifies the terms and conditions for redistribution.
5  *
6  *	@(#)if_imp.c	6.7 (Berkeley) 09/16/85
7  */
8 
9 #include "imp.h"
10 #if NIMP > 0
11 /*
12  * ARPANET IMP interface driver.
13  *
14  * The IMP-host protocol is handled here, leaving
15  * hardware specifics to the lower level interface driver.
16  */
17 #include "../machine/pte.h"
18 
19 #include "param.h"
20 #include "systm.h"
21 #include "mbuf.h"
22 #include "buf.h"
23 #include "protosw.h"
24 #include "socket.h"
25 #include "vmmac.h"
26 #include "time.h"
27 #include "kernel.h"
28 #include "errno.h"
29 #include "ioctl.h"
30 
31 #include "../vax/cpu.h"
32 #include "../vax/mtpr.h"
33 #include "../vaxuba/ubareg.h"
34 #include "../vaxuba/ubavar.h"
35 
36 #include "../net/if.h"
37 #include "../net/route.h"
38 
39 #include "../net/netisr.h"
40 #include "../netinet/in.h"
41 #include "../netinet/in_systm.h"
42 #include "../netinet/in_var.h"
43 #include "../netinet/ip.h"
44 #include "../netinet/ip_var.h"
45 /* define IMPLEADERS here to get leader printing code */
46 #include "if_imp.h"
47 #include "if_imphost.h"
48 
49 /*
50  * IMP software status per interface.
51  * (partially shared with the hardware specific module)
52  *
53  * Each interface is referenced by a network interface structure,
54  * imp_if, which the routing code uses to locate the interface.
55  * This structure contains the output queue for the interface, its
56  * address, ...  IMP specific structures used in connecting the
57  * IMP software modules to the hardware specific interface routines
58  * are stored here.  The common structures are made visible to the
59  * interface driver by passing a pointer to the hardware routine
60  * at "attach" time.
61  *
62  * NOTE: imp_if and imp_cb are assumed adjacent in hardware code.
63  */
64 struct imp_softc {
65 	struct	ifnet imp_if;		/* network visible interface */
66 	struct	impcb imp_cb;		/* hooks to hardware module */
67 	u_char	imp_state;		/* current state of IMP */
68 	char	imp_dropcnt;		/* used during initialization */
69 } imp_softc[NIMP];
70 
71 struct ifqueue impintrq;
72 
73 /*
74  * Messages from IMP regarding why
75  * it's going down.
76  */
77 static char *impmessage[] = {
78 	"in 30 seconds",
79 	"for hardware PM",
80 	"to reload software",
81 	"for emergency reset"
82 };
83 
84 #define HOSTDEADTIMER	10		/* How long to wait when down */
85 
86 int	impdown(), impinit(), impioctl(), impoutput();
87 
88 /*
89  * IMP attach routine.  Called from hardware device attach routine
90  * at configuration time with a pointer to the UNIBUS device structure.
91  * Sets up local state and returns pointer to base of ifnet+impcb
92  * structures.  This is then used by the device's attach routine
93  * set up its back pointers.
94  */
95 impattach(ui, reset)
96 	struct uba_device *ui;
97 	int (*reset)();
98 {
99 	struct imp_softc *sc;
100 	register struct ifnet *ifp;
101 
102 	if (ui->ui_unit >= NIMP) {
103 		printf("imp%d: not configured\n", ui->ui_unit);
104 		return (0);
105 	}
106 	sc = &imp_softc[ui->ui_unit];
107 	ifp = &sc->imp_if;
108 	/* UNIT COULD BE AMBIGUOUS */
109 	ifp->if_unit = ui->ui_unit;
110 	ifp->if_name = "imp";
111 	ifp->if_mtu = IMPMTU - sizeof(struct imp_leader);
112 	ifp->if_reset = reset;
113 	ifp->if_init = impinit;
114 	ifp->if_ioctl = impioctl;
115 	ifp->if_output = impoutput;
116 	/* reset is handled at the hardware level */
117 	if_attach(ifp);
118 	return ((int)ifp);
119 }
120 
121 /*
122  * IMP initialization routine: call hardware module to
123  * setup UNIBUS resources, init state and get ready for
124  * NOOPs the IMP should send us, and that we want to drop.
125  */
126 impinit(unit)
127 	int unit;
128 {
129 	int s = splimp();
130 	register struct imp_softc *sc = &imp_softc[unit];
131 
132 	if (sc->imp_if.if_addrlist == 0)
133 		return;
134 	if ((*sc->imp_cb.ic_init)(unit) == 0) {
135 		sc->imp_state = IMPS_DOWN;
136 		sc->imp_if.if_flags &= ~IFF_UP;
137 		splx(s);
138 		return;
139 	}
140 	sc->imp_state = IMPS_INIT;
141 	impnoops(sc);
142 	splx(s);
143 }
144 
145 #ifdef IMPLEADERS
146 int	impprintfs = 0;
147 #endif
148 
149 /*
150  * ARPAnet 1822 input routine.
151  * Called from hardware input interrupt routine to handle 1822
152  * IMP-host messages.  Type 0 messages (non-control) are
153  * passed to higher level protocol processors on the basis
154  * of link number.  Other type messages (control) are handled here.
155  */
156 impinput(unit, m)
157 	int unit;
158 	register struct mbuf *m;
159 {
160 	register struct imp_leader *ip;
161 	register struct imp_softc *sc = &imp_softc[unit];
162 	struct ifnet *ifp;
163 	register struct host *hp;
164 	register struct ifqueue *inq;
165 	struct control_leader *cp;
166 	struct in_addr addr;
167 	struct mbuf *next;
168 	struct sockaddr_in *sin;
169 
170 	/*
171 	 * Pull the interface pointer out of the mbuf
172 	 * and save for later; adjust mbuf to look at rest of data.
173 	 */
174 	ifp = *(mtod(m, struct ifnet **));
175 	IF_ADJ(m);
176 	/* verify leader length. */
177 	if (m->m_len < sizeof(struct control_leader) &&
178 	    (m = m_pullup(m, sizeof(struct control_leader))) == 0)
179 		return;
180 	cp = mtod(m, struct control_leader *);
181 	if (cp->dl_mtype == IMPTYPE_DATA)
182 		if (m->m_len < sizeof(struct imp_leader) &&
183 		    (m = m_pullup(m, sizeof(struct imp_leader))) == 0)
184 			return;
185 	ip = mtod(m, struct imp_leader *);
186 #ifdef IMPLEADERS
187 	if (impprintfs)
188 		printleader("impinput", ip);
189 #endif
190 
191 	/* check leader type */
192 	if (ip->il_format != IMP_NFF) {
193 		sc->imp_if.if_collisions++;	/* XXX */
194 		goto drop;
195 	}
196 
197 	if (ip->il_mtype != IMPTYPE_DATA) {
198 		/* If not data packet, build IP addr from leader (BRL) */
199 		imp_leader_to_addr(&addr, ip, &sc->imp_if);
200 	}
201 	switch (ip->il_mtype) {
202 
203 	case IMPTYPE_DATA:
204 		break;
205 
206 	/*
207 	 * IMP leader error.  Reset the IMP and discard the packet.
208 	 */
209 	case IMPTYPE_BADLEADER:
210 		/*
211 		 * According to 1822 document, this message
212 		 * will be generated in response to the
213 		 * first noop sent to the IMP after
214 		 * the host resets the IMP interface.
215 		 */
216 		if (sc->imp_state != IMPS_INIT) {
217 			impmsg(sc, "leader error");
218 			hostreset(((struct in_ifaddr *)&sc->imp_if.if_addrlist)->ia_net);
219 			impnoops(sc);
220 		}
221 		goto drop;
222 
223 	/*
224 	 * IMP going down.  Print message, and if not immediate,
225 	 * set off a timer to insure things will be reset at the
226 	 * appropriate time.
227 	 */
228 	case IMPTYPE_DOWN:
229 		if (sc->imp_state < IMPS_INIT)
230 			goto drop;
231 		if ((ip->il_link & IMP_DMASK) == 0) {
232 			sc->imp_state = IMPS_GOINGDOWN;
233 			timeout(impdown, (caddr_t)sc, 30 * hz);
234 		}
235 		impmsg(sc, "going down %s",
236 			(u_int)impmessage[ip->il_link&IMP_DMASK]);
237 		goto drop;
238 
239 	/*
240 	 * A NOP usually seen during the initialization sequence.
241 	 * Compare the local address with that in the message.
242 	 * Reset the local address notion if it doesn't match.
243 	 */
244 	case IMPTYPE_NOOP:
245 		if (sc->imp_state == IMPS_DOWN) {
246 			sc->imp_state = IMPS_INIT;
247 			sc->imp_dropcnt = IMP_DROPCNT;
248 		}
249 		if (sc->imp_state == IMPS_INIT && --sc->imp_dropcnt > 0)
250 			goto drop;
251 		sin = (struct sockaddr_in *)&sc->imp_if.if_addrlist->ifa_addr;
252 		if (ip->il_imp != 0) {	/* BRL */
253 			struct in_addr leader_addr;
254 			imp_leader_to_addr(&leader_addr, ip, &sc->imp_if);
255 			if (sin->sin_addr.s_addr != leader_addr.s_addr) {
256 				impmsg(sc, "address reset to x%x (%d/%d)",
257 					htonl(leader_addr.s_addr),
258 					(u_int)ip->il_host,
259 					htons(ip->il_imp));
260 				sin->sin_addr.s_addr = leader_addr.s_addr;
261 			}
262 		}
263 		sc->imp_state = IMPS_UP;
264 		sc->imp_if.if_flags |= IFF_UP;
265 		goto drop;
266 
267 	/*
268 	 * RFNM or INCOMPLETE message, send next
269 	 * message on the q.  We could pass incomplete's
270 	 * up to the next level, but this currently isn't
271 	 * needed.
272 	 */
273 	case IMPTYPE_RFNM:
274 	case IMPTYPE_INCOMPLETE:
275 		if (hp = hostlookup(addr)) {
276 			hp->h_timer = HOSTTIMER;
277 			if (hp->h_rfnm == 0)
278 				hp->h_flags &= ~HF_INUSE;
279 			else if (next = hostdeque(hp))
280 				(void) impsnd(&sc->imp_if, next);
281 		}
282 		goto drop;
283 
284 	/*
285 	 * Host or IMP can't be reached.  Flush any packets
286 	 * awaiting transmission and release the host structure.
287 	 * Enqueue for notifying protocols at software interrupt time.
288 	 */
289 	case IMPTYPE_HOSTDEAD:
290 	case IMPTYPE_HOSTUNREACH:
291 		if (hp = hostlookup(addr)) {
292 			hp->h_flags |= (1 << (int)ip->il_mtype);
293 			hostfree(hp);
294 			hp->h_timer = HOSTDEADTIMER;
295 		}
296 		goto rawlinkin;
297 
298 	/*
299 	 * Error in data.  Clear RFNM status for this host and send
300 	 * noops to the IMP to clear the interface.
301 	 */
302 	case IMPTYPE_BADDATA:
303 		impmsg(sc, "data error");
304 		if (hp = hostlookup(addr))
305 			hp->h_rfnm = 0;
306 		impnoops(sc);
307 		goto drop;
308 
309 	/*
310 	 * Interface reset.
311 	 */
312 	case IMPTYPE_RESET:
313 		impmsg(sc, "interface reset");
314 		/* clear RFNM counts */
315 		hostreset(((struct in_ifaddr *)&sc->imp_if.if_addrlist)->ia_net);
316 		impnoops(sc);
317 		goto drop;
318 
319 	default:
320 		sc->imp_if.if_collisions++;		/* XXX */
321 		goto drop;
322 	}
323 
324 	/*
325 	 * Data for a protocol.  Dispatch to the appropriate
326 	 * protocol routine (running at software interrupt).
327 	 * If this isn't a raw interface, advance pointer
328 	 * into mbuf past leader.
329 	 */
330 	switch (ip->il_link) {
331 
332 	case IMPLINK_IP:
333 		m->m_len -= sizeof(struct imp_leader);
334 		m->m_off += sizeof(struct imp_leader);
335 		schednetisr(NETISR_IP);
336 		inq = &ipintrq;
337 		break;
338 
339 	default:
340 	rawlinkin:
341 		schednetisr(NETISR_IMP);
342 		inq = &impintrq;
343 		break;
344 	}
345 	/*
346 	 * Re-insert interface pointer in the mbuf chain
347 	 * for the next protocol up.
348 	 */
349 	m->m_off -= sizeof(struct ifnet *);
350 	m->m_len += sizeof(struct ifnet *);
351 	*(mtod(m, struct ifnet **)) = ifp;
352 	if (IF_QFULL(inq)) {
353 		IF_DROP(inq);
354 		goto drop;
355 	}
356 	IF_ENQUEUE(inq, m);
357 	return;
358 
359 drop:
360 	m_freem(m);
361 }
362 
363 /*
364  * Bring the IMP down after notification.
365  */
366 impdown(sc)
367 	struct imp_softc *sc;
368 {
369 	int s = splimp();
370 
371 	sc->imp_state = IMPS_DOWN;
372 	impmsg(sc, "marked down");
373 	hostreset(((struct in_ifaddr *)&sc->imp_if.if_addrlist)->ia_net);
374 	if_down(&sc->imp_if);
375 	splx(s);
376 }
377 
378 /*VARARGS*/
379 impmsg(sc, fmt, a1, a2, a3)
380 	struct imp_softc *sc;
381 	char *fmt;
382 	u_int a1;
383 {
384 
385 	printf("imp%d: ", sc->imp_if.if_unit);
386 	printf(fmt, a1, a2, a3);
387 	printf("\n");
388 }
389 
390 struct sockproto impproto = { PF_IMPLINK };
391 struct sockaddr_in impdst = { AF_IMPLINK };
392 struct sockaddr_in impsrc = { AF_IMPLINK };
393 
394 /*
395  * Pick up the IMP "error" messages enqueued earlier,
396  * passing these up to the higher level protocol
397  * and the raw interface.
398  */
399 impintr()
400 {
401 	register struct mbuf *m;
402 	register struct control_leader *cp;
403 	struct ifnet *ifp;
404 	int s;
405 
406 	for (;;) {
407 		s = splimp();
408 		IF_DEQUEUEIF(&impintrq, m, ifp);
409 		splx(s);
410 		if (m == 0)
411 			return;
412 
413 		cp = mtod(m, struct control_leader *);
414 		imp_leader_to_addr(&impsrc.sin_addr, (struct imp_leader *)cp,
415 		    ifp);
416 		impproto.sp_protocol = cp->dl_link;
417 		impdst.sin_addr = IA_SIN(ifp->if_addrlist->ifa_addr)->sin_addr;
418 
419 		switch (cp->dl_link) {
420 
421 		case IMPLINK_IP:
422 			pfctlinput(cp->dl_mtype, (caddr_t)&impsrc);
423 			break;
424 		default:
425 			raw_ctlinput(cp->dl_mtype, (caddr_t)&impsrc);
426 			break;
427 		}
428 
429 		raw_input(m, &impproto, (struct sockaddr *)&impsrc,
430 		  (struct sockaddr *)&impdst);
431 	}
432 }
433 
434 /*
435  * ARPAnet 1822 output routine.
436  * Called from higher level protocol routines to set up messages for
437  * transmission to the imp.  Sets up the header and calls impsnd to
438  * enqueue the message for this IMP's hardware driver.
439  */
440 impoutput(ifp, m0, dst)
441 	register struct ifnet *ifp;
442 	struct mbuf *m0;
443 	struct sockaddr *dst;
444 {
445 	register struct imp_leader *imp;
446 	register struct mbuf *m = m0;
447 	int dlink, len;
448 	int error = 0;
449 
450 	/*
451 	 * Don't even try if the IMP is unavailable.
452 	 */
453 	if (imp_softc[ifp->if_unit].imp_state != IMPS_UP) {
454 		error = ENETDOWN;
455 		goto drop;
456 	}
457 
458 	switch (dst->sa_family) {
459 
460 	case AF_INET: {
461 		struct ip *ip = mtod(m, struct ip *);
462 
463 		dlink = IMPLINK_IP;
464 		len = ntohs((u_short)ip->ip_len);
465 		break;
466 	}
467 
468 	case AF_IMPLINK:
469 		len = 0;
470 		do
471 			len += m->m_len;
472 		while (m = m->m_next);
473 		m = m0;
474 		goto leaderexists;
475 
476 	default:
477 		printf("imp%d: can't handle af%d\n", ifp->if_unit,
478 			dst->sa_family);
479 		error = EAFNOSUPPORT;
480 		goto drop;
481 	}
482 
483 	/*
484 	 * Add IMP leader.  If there's not enough space in the
485 	 * first mbuf, allocate another.  If that should fail, we
486 	 * drop this sucker.
487 	 */
488 	if (m->m_off > MMAXOFF ||
489 	    MMINOFF + sizeof(struct imp_leader) > m->m_off) {
490 		m = m_get(M_DONTWAIT, MT_HEADER);
491 		if (m == 0) {
492 			error = ENOBUFS;
493 			goto drop;
494 		}
495 		m->m_next = m0;
496 		m->m_len = sizeof(struct imp_leader);
497 	} else {
498 		m->m_off -= sizeof(struct imp_leader);
499 		m->m_len += sizeof(struct imp_leader);
500 	}
501 	imp = mtod(m, struct imp_leader *);
502 	imp->il_format = IMP_NFF;
503 	imp->il_mtype = IMPTYPE_DATA;
504 	imp_addr_to_leader(imp,
505 		((struct sockaddr_in *)dst)->sin_addr.s_addr); /* BRL */
506 	imp->il_length = htons((u_short)len << 3);		/* BRL */
507 	imp->il_link = dlink;
508 	imp->il_flags = imp->il_htype = imp->il_subtype = 0;
509 
510 leaderexists:
511 	return (impsnd(ifp, m));
512 drop:
513 	m_freem(m0);
514 	return (error);
515 }
516 
517 /*
518  * Put a message on an interface's output queue.
519  * Perform RFNM counting: no more than 8 message may be
520  * in flight to any one host.
521  */
522 impsnd(ifp, m)
523 	struct ifnet *ifp;
524 	struct mbuf *m;
525 {
526 	register struct imp_leader *ip;
527 	register struct host *hp;
528 	struct impcb *icp;
529 	int s, error;
530 
531 	ip = mtod(m, struct imp_leader *);
532 
533 	/*
534 	 * Do RFNM counting for data messages
535 	 * (no more than 8 outstanding to any host)
536 	 */
537 	s = splimp();
538 	if (ip->il_mtype == IMPTYPE_DATA) {
539 		struct in_addr addr;
540 
541 		imp_leader_to_addr(&addr, ip, ifp);	/* BRL */
542 		if ((hp = hostlookup(addr)) == 0)
543 			hp = hostenter(addr);
544 		if (hp && (hp->h_flags & (HF_DEAD|HF_UNREACH))) {
545 			error = hp->h_flags&HF_DEAD ? EHOSTDOWN : EHOSTUNREACH;
546 			hp->h_flags &= ~HF_INUSE;
547 			goto bad;
548 		}
549 
550 		/*
551 		 * If IMP would block, queue until RFNM
552 		 */
553 		if (hp) {
554 #ifndef NORFNM
555 			if (hp->h_rfnm < 8)
556 #endif
557 			{
558 				hp->h_timer = HOSTTIMER;
559 				hp->h_rfnm++;
560 				goto enque;
561 			}
562 			if (hp->h_qcnt < 8) {	/* high water mark */
563 				HOST_ENQUE(hp, m);
564 				goto start;
565 			}
566 		}
567 		error = ENOBUFS;
568 		goto bad;
569 	}
570 enque:
571 	if (IF_QFULL(&ifp->if_snd)) {
572 		IF_DROP(&ifp->if_snd);
573 		error = ENOBUFS;
574 		if (ip->il_mtype == IMPTYPE_DATA)
575 			hp->h_rfnm--;
576 bad:
577 		m_freem(m);
578 		splx(s);
579 		return (error);
580 	}
581 	IF_ENQUEUE(&ifp->if_snd, m);
582 start:
583 	icp = &imp_softc[ifp->if_unit].imp_cb;
584 	if (icp->ic_oactive == 0)
585 		(*icp->ic_start)(ifp->if_unit);
586 	splx(s);
587 	return (0);
588 }
589 
590 /*
591  * Put three 1822 NOOPs at the head of the output queue.
592  * Part of host-IMP initialization procedure.
593  * (Should return success/failure, but noone knows
594  * what to do with this, so why bother?)
595  * This routine is always called at splimp, so we don't
596  * protect the call to IF_PREPEND.
597  */
598 impnoops(sc)
599 	register struct imp_softc *sc;
600 {
601 	register i;
602 	register struct mbuf *m;
603 	register struct control_leader *cp;
604 
605 	sc->imp_dropcnt = IMP_DROPCNT;
606 	for (i = 0; i < IMP_DROPCNT + 1; i++) {
607 		if ((m = m_getclr(M_DONTWAIT, MT_HEADER)) == 0)
608 			return;
609 		m->m_len = sizeof(struct control_leader);
610 		cp = mtod(m, struct control_leader *);
611 		cp->dl_format = IMP_NFF;
612                 cp->dl_link = i;
613                 cp->dl_mtype = IMPTYPE_NOOP;
614 		IF_PREPEND(&sc->imp_if.if_snd, m);
615 	}
616 	if (sc->imp_cb.ic_oactive == 0)
617 		(*sc->imp_cb.ic_start)(sc->imp_if.if_unit);
618 }
619 
620 /*
621  * Process an ioctl request.
622  */
623 impioctl(ifp, cmd, data)
624 	register struct ifnet *ifp;
625 	int cmd;
626 	caddr_t data;
627 {
628 	struct ifaddr *ifa = (struct ifaddr *) data;
629 	int s = splimp(), error = 0;
630 
631 	switch (cmd) {
632 
633 	case SIOCSIFADDR:
634 		if (ifa->ifa_addr.sa_family != AF_INET) {
635 			error = EINVAL;
636 			break;
637 		}
638 		if ((ifp->if_flags & IFF_RUNNING) == 0)
639 			impinit(ifp->if_unit);
640 		break;
641 
642 	default:
643 		error = EINVAL;
644 	}
645 	splx(s);
646 	return (error);
647 }
648 
649 #ifdef IMPLEADERS
650 printleader(routine, ip)
651 	char *routine;
652 	register struct imp_leader *ip;
653 {
654 	printf("%s: ", routine);
655 	printbyte((char *)ip, 12);
656 	printf("<fmt=%x,net=%x,flags=%x,mtype=", ip->il_format, ip->il_network,
657 		ip->il_flags);
658 	if (ip->il_mtype <= IMPTYPE_READY)
659 		printf("%s,", impleaders[ip->il_mtype]);
660 	else
661 		printf("%x,", ip->il_mtype);
662 	printf("htype=%x,host=%x,imp=%x,link=", ip->il_htype, ip->il_host,
663 		ntohs(ip->il_imp));
664 	if (ip->il_link == IMPLINK_IP)
665 		printf("ip,");
666 	else
667 		printf("%x,", ip->il_link);
668 	printf("subtype=%x,len=%x>\n",ip->il_subtype,ntohs(ip->il_length)>>3);
669 }
670 
671 printbyte(cp, n)
672 	register char *cp;
673 	int n;
674 {
675 	register i, j, c;
676 
677 	for (i=0; i<n; i++) {
678 		c = *cp++;
679 		for (j=0; j<2; j++)
680 			putchar("0123456789abcdef"[(c>>((1-j)*4))&0xf], 0);
681 		putchar(' ', 0);
682 	}
683 	putchar('\n', 0);
684 }
685 #endif
686 
687 /*
688  * Routine to convert from IMP Leader to InterNet Address.
689  *
690  * This procedure is necessary because IMPs may be assigned Class A, B, or C
691  * network numbers, but only have 8 bits in the leader to reflect the
692  * IMP "network number".  The strategy is to take the network number from
693  * the ifnet structure, and blend in the host-on-imp and imp-on-net numbers
694  * from the leader.
695  *
696  * There is no support for "Logical Hosts".
697  *
698  * Class A:	Net.Host.0.Imp
699  * Class B:	Net.net.Host.Imp
700  * Class C:	Net.net.net.(Host4|Imp4)
701  */
702 imp_leader_to_addr(ap, ip, ifp)
703 	struct in_addr *ap;
704 	register struct imp_leader *ip;
705 	struct ifnet *ifp;
706 {
707 	register long final;
708 	register struct sockaddr_in *sin;
709 	int imp = htons(ip->il_imp);
710 
711 	sin = (struct sockaddr_in *)(&ifp->if_addrlist->ifa_addr);
712 	final = htonl(sin->sin_addr.s_addr);
713 
714 	if (IN_CLASSA(final)) {
715 		final &= IN_CLASSA_NET;
716 		final |= (imp & 0xFF) | ((ip->il_host & 0xFF)<<16);
717 	} else if (IN_CLASSB(final)) {
718 		final &= IN_CLASSB_NET;
719 		final |= (imp & 0xFF) | ((ip->il_host & 0xFF)<<8);
720 	} else {
721 		final &= IN_CLASSC_NET;
722 		final |= (imp & 0x0F) | ((ip->il_host & 0x0F)<<4);
723 	}
724 	ap->s_addr = htonl(final);
725 }
726 
727 /*
728  * Function to take InterNet address and fill in IMP leader fields.
729  */
730 imp_addr_to_leader(imp, a)
731 	register struct imp_leader *imp;
732 	long a;
733 {
734 	register long addr = htonl(a);		/* host order */
735 
736 	imp->il_network = 0;	/* !! */
737 
738 	if (IN_CLASSA(addr)) {
739 		imp->il_host = ((addr>>16) & 0xFF);
740 		imp->il_imp = addr & 0xFF;
741 	} else if (IN_CLASSB(addr)) {
742 		imp->il_host = ((addr>>8) & 0xFF);
743 		imp->il_imp = addr & 0xFF;
744 	} else {
745 		imp->il_host = ((addr>>4) & 0xF);
746 		imp->il_imp = addr & 0xF;
747 	}
748 	imp->il_imp = htons(imp->il_imp);	/* network order! */
749 }
750 #endif
751