xref: /original-bsd/sys/deprecated/netimp/if_imp.c (revision 9c59a687)
1 /*	if_imp.c	4.21	82/03/30	*/
2 
3 #include "imp.h"
4 #if NIMP > 0
5 /*
6  * ARPAnet IMP interface driver.
7  *
8  * The IMP-host protocol is handled here, leaving
9  * hardware specifics to the lower level interface driver.
10  *
11  * TODO:
12  *	pass more error indications up to protocol modules
13  */
14 #include "../h/param.h"
15 #include "../h/systm.h"
16 #include "../h/mbuf.h"
17 #include "../h/pte.h"
18 #include "../h/buf.h"
19 #include "../h/protosw.h"
20 #include "../h/socket.h"
21 #include "../h/ubareg.h"
22 #include "../h/ubavar.h"
23 #include "../h/cpu.h"
24 #include "../h/mtpr.h"
25 #include "../h/vmmac.h"
26 #include "../net/in.h"
27 #include "../net/in_systm.h"
28 #include "../net/if.h"
29 #include "../net/if_imp.h"
30 #include "../net/if_imphost.h"
31 #include "../net/ip.h"
32 #include "../net/ip_var.h"
33 #include "../net/route.h"
34 
35 /*
36  * IMP software status per interface.
37  * (partially shared with the hardware specific module)
38  *
39  * Each interface is referenced by a network interface structure,
40  * imp_if, which the routing code uses to locate the interface.
41  * This structure contains the output queue for the interface, its
42  * address, ...  IMP specific structures used in connecting the
43  * IMP software modules to the hardware specific interface routines
44  * are stored here.  The common structures are made visible to the
45  * interface driver by passing a pointer to the hardware routine
46  * at "attach" time.
47  *
48  * NOTE: imp_if and imp_cb are assumed adjacent in hardware code.
49  */
50 struct imp_softc {
51 	struct	ifnet imp_if;		/* network visible interface */
52 	struct	impcb imp_cb;		/* hooks to hardware module */
53 	u_char	imp_state;		/* current state of IMP */
54 	char	imp_dropcnt;		/* used during initialization */
55 } imp_softc[NIMP];
56 
57 /*
58  * Messages from IMP regarding why
59  * it's going down.
60  */
61 static char *impmessage[] = {
62 	"in 30 seconds",
63 	"for hardware PM",
64 	"to reload software",
65 	"for emergency reset"
66 };
67 
68 int	impdown(), impinit(), impoutput();
69 
70 /*
71  * IMP attach routine.  Called from hardware device attach routine
72  * at configuration time with a pointer to the UNIBUS device structure.
73  * Sets up local state and returns pointer to base of ifnet+impcb
74  * structures.  This is then used by the device's attach routine
75  * set up its back pointers.
76  */
77 impattach(ui)
78 	struct uba_device *ui;
79 {
80 	struct imp_softc *sc = &imp_softc[ui->ui_unit];
81 	register struct ifnet *ifp = &sc->imp_if;
82 	struct sockaddr_in *sin;
83 
84 COUNT(IMPATTACH);
85 	/* UNIT COULD BE AMBIGUOUS */
86 	ifp->if_unit = ui->ui_unit;
87 	ifp->if_name = "imp";
88 	ifp->if_mtu = IMPMTU - sizeof(struct imp_leader);
89 	ifp->if_net = ui->ui_flags;
90 	/* the host and imp fields will be filled in by the imp */
91 	sin = (struct sockaddr_in *)&ifp->if_addr;
92 	sin->sin_family = AF_INET;
93 	sin->sin_addr = if_makeaddr(ifp->if_net, 0);
94 	ifp->if_init = impinit;
95 	ifp->if_output = impoutput;
96 	/* reset is handled at the hardware level */
97 	if_attach(ifp);
98 	return ((int)&sc->imp_if);
99 }
100 
101 /*
102  * IMP initialization routine: call hardware module to
103  * setup UNIBUS resources, init state and get ready for
104  * NOOPs the IMP should send us, and that we want to drop.
105  */
106 impinit(unit)
107 	int unit;
108 {
109 	register struct imp_softc *sc = &imp_softc[unit];
110 	struct ifnet *ifp;
111 
112 	if ((*sc->imp_cb.ic_init)(unit) == 0) {
113 		sc->imp_state = IMPS_DOWN;
114 		sc->imp_if.if_flags &= ~IFF_UP;
115 		return;
116 	}
117 	sc->imp_state = IMPS_INIT;
118 	sc->imp_dropcnt = IMP_DROPCNT;
119 	impnoops(sc);
120 	if_rtinit(&sc->imp_if, RTF_DIRECT|RTF_UP);
121 }
122 
123 struct sockproto impproto = { PF_IMPLINK };
124 struct sockaddr_in impdst = { AF_IMPLINK };
125 struct sockaddr_in impsrc = { AF_IMPLINK };
126 
127 /*
128  * ARPAnet 1822 input routine.
129  * Called from hardware input interrupt routine to handle 1822
130  * IMP-host messages.  Type 0 messages (non-control) are
131  * passed to higher level protocol processors on the basis
132  * of link number.  Other type messages (control) are handled here.
133  */
134 impinput(unit, m)
135 	int unit;
136 	register struct mbuf *m;
137 {
138 	register struct imp_leader *ip;
139 	register struct imp_softc *sc = &imp_softc[unit];
140 	register struct host *hp;
141 	register struct ifqueue *inq;
142 	struct control_leader *cp;
143 	struct in_addr addr;
144 	struct mbuf *next;
145 	struct sockaddr_in *sin;
146 
147 COUNT(IMPINPUT);
148 	/* verify leader length. */
149 	if (m->m_len < sizeof(struct control_leader) &&
150 	    (m = m_pullup(m, sizeof(struct control_leader))) == 0)
151 		return;
152 	cp = mtod(m, struct control_leader *);
153 	if (cp->dl_mtype == IMPTYPE_DATA)
154 		if (m->m_len < sizeof(struct imp_leader) &&
155 		    (m = m_pullup(m, sizeof(struct imp_leader))) == 0)
156 			return;
157 	ip = mtod(m, struct imp_leader *);
158 
159 	/* check leader type */
160 	if (ip->il_format != IMP_NFF) {
161 		sc->imp_if.if_collisions++;	/* XXX */
162 		goto drop;
163 	}
164 
165 	/*
166 	 * Certain messages require a host structure.
167 	 * Do this in one shot here.
168 	 */
169 	switch (ip->il_mtype) {
170 
171 	case IMPTYPE_RFNM:
172 	case IMPTYPE_INCOMPLETE:
173 	case IMPTYPE_HOSTDEAD:
174 	case IMPTYPE_HOSTUNREACH:
175 	case IMPTYPE_BADDATA:
176 #ifdef notdef
177 		addr.s_net = ip->il_network;
178 #else
179 		addr.s_net = 0;
180 #endif
181 		addr.s_imp = ip->il_imp;
182 		addr.s_host = ip->il_host;
183 		hp = hostlookup(addr);
184 		break;
185 	}
186 
187 	switch (ip->il_mtype) {
188 
189 	case IMPTYPE_DATA:
190 		break;
191 
192 	/*
193 	 * IMP leader error.  Reset the IMP and discard the packet.
194 	 */
195 	case IMPTYPE_BADLEADER:
196 		/*
197 		 * According to 1822 document, this message
198 		 * will be generated in response to the
199 		 * first noop sent to the IMP after
200 		 * the host resets the IMP interface.
201 		 */
202 		if (sc->imp_state != IMPS_INIT) {
203 			impmsg(sc, "leader error");
204 			hostreset(sc->imp_if.if_net);	/* XXX */
205 			impnoops(sc);
206 		}
207 		goto rawlinkin;
208 
209 	/*
210 	 * IMP going down.  Print message, and if not immediate,
211 	 * set off a timer to insure things will be reset at the
212 	 * appropriate time.
213 	 */
214 	case IMPTYPE_DOWN:
215 		if ((ip->il_link & IMP_DMASK) == 0) {
216 			sc->imp_state = IMPS_GOINGDOWN;
217 			timeout(impdown, (caddr_t)sc, 30 * hz);
218 		}
219 		impmsg(sc, "going down %s",
220 			(u_int)impmessage[ip->il_link&IMP_DMASK]);
221 		goto rawlinkin;
222 
223 	/*
224 	 * A NOP usually seen during the initialization sequence.
225 	 * Compare the local address with that in the message.
226 	 * Reset the local address notion if it doesn't match.
227 	 */
228 	case IMPTYPE_NOOP:
229 		if (sc->imp_state == IMPS_DOWN) {
230 			sc->imp_state = IMPS_INIT;
231 			sc->imp_dropcnt = IMP_DROPCNT;
232 		}
233 		if (sc->imp_state != IMPS_INIT || --sc->imp_dropcnt > 0)
234 			goto drop;
235 		sc->imp_state = IMPS_UP;
236 		sc->imp_if.if_flags |= IFF_UP;
237 		sin = (struct sockaddr_in *)&sc->imp_if.if_addr;
238 		sc->imp_if.if_host[0] = sin->sin_addr.s_host = ip->il_host;
239 		sin->sin_addr.s_imp = ip->il_imp;
240 		impmsg(sc, "reset (host %d/imp %d)", (u_int)ip->il_host,
241 			ntohs(ip->il_imp));
242 		/* restart output in case something was q'd */
243 		(*sc->imp_cb.ic_start)(sc->imp_if.if_unit);
244 		goto drop;
245 
246 	/*
247 	 * RFNM or INCOMPLETE message, record in
248 	 * host table and prime output routine.
249 	 *
250 	 * SHOULD NOTIFY PROTOCOL ABOUT INCOMPLETES.
251 	 */
252 	case IMPTYPE_RFNM:
253 	case IMPTYPE_INCOMPLETE:
254 		if (hp && hp->h_rfnm)
255 			if (next = hostdeque(hp))
256 				(void) impsnd(&sc->imp_if, next);
257 		goto drop;
258 
259 	/*
260 	 * Host or IMP can't be reached.  Flush any packets
261 	 * awaiting transmission and release the host structure.
262 	 *
263 	 * TODO: NOTIFY THE PROTOCOL
264 	 */
265 	case IMPTYPE_HOSTDEAD:
266 		impmsg(sc, "host dead");	/* XXX */
267 		goto common;			/* XXX */
268 
269 	/* SHOULD SIGNAL ROUTING DAEMON */
270 	case IMPTYPE_HOSTUNREACH:
271 		impmsg(sc, "host unreachable");	/* XXX */
272 	common:
273 		if (hp)
274 			hostfree(hp);		/* won't work right */
275 		goto rawlinkin;
276 
277 	/*
278 	 * Error in data.  Clear RFNM status for this host and send
279 	 * noops to the IMP to clear the interface.
280 	 */
281 	case IMPTYPE_BADDATA:
282 		impmsg(sc, "data error");
283 		if (hp)
284 			hp->h_rfnm = 0;
285 		impnoops(sc);
286 		goto rawlinkin;
287 
288 	/*
289 	 * Interface reset.
290 	 */
291 	case IMPTYPE_RESET:
292 		impmsg(sc, "interface reset");
293 		impnoops(sc);
294 		goto rawlinkin;
295 
296 	default:
297 		sc->imp_if.if_collisions++;		/* XXX */
298 		goto rawlinkin;
299 	}
300 
301 	/*
302 	 * Data for a protocol.  Dispatch to the appropriate
303 	 * protocol routine (running at software interrupt).
304 	 * If this isn't a raw interface, advance pointer
305 	 * into mbuf past leader.
306 	 */
307 	switch (ip->il_link) {
308 
309 #ifdef INET
310 	case IMPLINK_IP:
311 		m->m_len -= sizeof(struct imp_leader);
312 		m->m_off += sizeof(struct imp_leader);
313 		schednetisr(NETISR_IP);
314 		inq = &ipintrq;
315 		break;
316 #endif
317 
318 	default:
319 	rawlinkin:
320 		impproto.sp_protocol = ip->il_link;
321 		sin = (struct sockaddr_in *)&sc->imp_if.if_addr;
322 		impdst.sin_addr = sin->sin_addr;;
323 		impsrc.sin_addr.s_net = ip->il_network;
324 		impsrc.sin_addr.s_host = ip->il_host;
325 		impsrc.sin_addr.s_imp = ip->il_imp;
326 		raw_input(m, &impproto, (struct sockaddr *)&impdst,
327 		  (struct sockaddr *)&impsrc);
328 		return;
329 	}
330 	if (IF_QFULL(inq)) {
331 		IF_DROP(inq);
332 		goto drop;
333 	}
334 	IF_ENQUEUE(inq, m);
335 	return;
336 
337 drop:
338 	m_freem(m);
339 }
340 
341 /*
342  * Bring the IMP down after notification.
343  */
344 impdown(sc)
345 	struct imp_softc *sc;
346 {
347 
348 	sc->imp_state = IMPS_DOWN;
349 	sc->imp_if.if_flags &= ~IFF_UP;
350 	impmsg(sc, "marked down");
351 	/* notify protocols with messages waiting? */
352 }
353 
354 /*VARARGS*/
355 impmsg(sc, fmt, a1, a2)
356 	struct imp_softc *sc;
357 	char *fmt;
358 	u_int a1;
359 {
360 
361 	printf("imp%d: ", sc->imp_if.if_unit);
362 	printf(fmt, a1, a2);
363 	printf("\n");
364 }
365 
366 /*
367  * ARPAnet 1822 output routine.
368  * Called from higher level protocol routines to set up messages for
369  * transmission to the imp.  Sets up the header and calls impsnd to
370  * enqueue the message for this IMP's hardware driver.
371  */
372 impoutput(ifp, m0, dst)
373 	register struct ifnet *ifp;
374 	struct mbuf *m0;
375 	struct sockaddr *dst;
376 {
377 	register struct imp_leader *imp;
378 	register struct mbuf *m = m0;
379 	int x, dhost, dimp, dlink, len, dnet;
380 
381 COUNT(IMPOUTPUT);
382 	/*
383 	 * Don't even try if the IMP is unavailable.
384 	 */
385 	x = imp_softc[ifp->if_unit].imp_state;
386 	if (x == IMPS_DOWN || x == IMPS_GOINGDOWN)
387 		goto drop;
388 
389 	switch (dst->sa_family) {
390 
391 #ifdef INET
392 	case AF_INET: {
393 		struct ip *ip = mtod(m0, struct ip *);
394 		struct sockaddr_in *sin = (struct sockaddr_in *)dst;
395 
396 		dhost = sin->sin_addr.s_host;
397 		dimp = sin->sin_addr.s_impno;
398 		dlink = IMPLINK_IP;
399 		dnet = 0;
400 		len = ntohs((u_short)ip->ip_len);
401 		break;
402 	}
403 #endif
404 	case AF_IMPLINK:
405 		goto leaderexists;
406 
407 	default:
408 		printf("imp%d: can't handle af%d\n", ifp->if_unit,
409 			dst->sa_family);
410 		goto drop;
411 	}
412 
413 	/*
414 	 * Add IMP leader.  If there's not enough space in the
415 	 * first mbuf, allocate another.  If that should fail, we
416 	 * drop this sucker.
417 	 */
418 	if (m->m_off > MMAXOFF ||
419 	    MMINOFF + sizeof(struct imp_leader) > m->m_off) {
420 		m = m_get(M_DONTWAIT);
421 		if (m == 0)
422 			goto drop;
423 		m->m_next = m0;
424 		m->m_off = MMINOFF;
425 		m->m_len = sizeof(struct imp_leader);
426 	} else {
427 		m->m_off -= sizeof(struct imp_leader);
428 		m->m_len += sizeof(struct imp_leader);
429 	}
430 	imp = mtod(m, struct imp_leader *);
431 	imp->il_format = IMP_NFF;
432 	imp->il_mtype = IMPTYPE_DATA;
433 	imp->il_network = dnet;
434 	imp->il_host = dhost;
435 	imp->il_imp = htons((u_short)dimp);
436 	imp->il_length =
437 		htons((u_short)(len + sizeof(struct imp_leader)) << 3);
438 	imp->il_link = dlink;
439 	imp->il_flags = imp->il_htype = imp->il_subtype = 0;
440 
441 leaderexists:
442 	/*
443 	 * Hand message to impsnd to perform RFNM counting
444 	 * and eventual transmission.
445 	 */
446 	return (impsnd(ifp, m));
447 drop:
448 	m_freem(m0);
449 	return (0);
450 }
451 
452 /*
453  * Put a message on an interface's output queue.
454  * Perform RFNM counting: no more than 8 message may be
455  * in flight to any one host.
456  */
457 impsnd(ifp, m)
458 	struct ifnet *ifp;
459 	struct mbuf *m;
460 {
461 	register struct imp_leader *ip;
462 	register struct host *hp;
463 	struct impcb *icp;
464 	int x;
465 
466 COUNT(IMPSND);
467 	ip = mtod(m, struct imp_leader *);
468 
469 	/*
470 	 * Do RFNM counting for data messages
471 	 * (no more than 8 outstanding to any host)
472 	 */
473 	x = splimp();
474 	if (ip->il_mtype == IMPTYPE_DATA) {
475 		struct in_addr addr;
476 
477 #ifdef notdef
478                 addr.s_net = ip->il_network;
479 #else
480 		addr.s_net = 0;
481 #endif
482                 addr.s_host = ip->il_host;
483                 addr.s_imp = ip->il_imp;
484 		if ((hp = hostlookup(addr)) == 0)
485 			hp = hostenter(addr);
486 
487 		/*
488 		 * If IMP would block, queue until RFNM
489 		 */
490 		if (hp) {
491 			if (hp->h_rfnm < 8) {
492 				hp->h_rfnm++;
493 				goto enque;
494 			}
495 			if (hp->h_qcnt < 8) {	/* high water mark */
496 				HOST_ENQUE(hp, m);
497 				goto start;
498 			}
499 		}
500 		m_freem(m);
501 		splx(x);
502 		return (0);
503 	}
504 enque:
505 	if (IF_QFULL(&ifp->if_snd)) {
506 		IF_DROP(&ifp->if_snd);
507 		m_freem(m);
508 		splx(x);
509 		return (0);
510 	}
511 	IF_ENQUEUE(&ifp->if_snd, m);
512 start:
513 	splx(x);
514 	icp = &imp_softc[ifp->if_unit].imp_cb;
515 	if (icp->ic_oactive == 0)
516 		(*icp->ic_start)(ifp->if_unit);
517 	return (1);
518 }
519 
520 /*
521  * Put three 1822 NOOPs at the head of the output queue.
522  * Part of host-IMP initialization procedure.
523  * (Should return success/failure, but noone knows
524  * what to do with this, so why bother?)
525  */
526 impnoops(sc)
527 	register struct imp_softc *sc;
528 {
529 	register i;
530 	register struct mbuf *m;
531 	register struct control_leader *cp;
532 	int x;
533 
534 COUNT(IMPNOOPS);
535 	sc->imp_state = IMPS_INIT;
536 	sc->imp_dropcnt = IMP_DROPCNT;
537 	for (i = 0; i < IMP_DROPCNT + 1; i++ ) {
538 		if ((m = m_getclr(M_DONTWAIT)) == 0)
539 			return;
540 		m->m_off = MMINOFF;
541 		m->m_len = sizeof(struct control_leader);
542 		cp = mtod(m, struct control_leader *);
543 		cp->dl_format = IMP_NFF;
544                 cp->dl_link = i;
545                 cp->dl_mtype = IMPTYPE_NOOP;
546 		x = splimp();
547 		IF_PREPEND(&sc->imp_if.if_snd, m);
548 		splx(x);
549 	}
550 	if (sc->imp_cb.ic_oactive == 0)
551 		(*sc->imp_cb.ic_start)(sc->imp_if.if_unit);
552 }
553 
554 #ifdef IMPLEADERS
555 printleader(routine, ip)
556 	char *routine;
557 	register struct imp_leader *ip;
558 {
559 	printf("%s: ", routine);
560 	printbyte((char *)ip, 12);
561 	printf("<fmt=%x,net=%x,flags=%x,mtype=", ip->il_format, ip->il_network,
562 		ip->il_flags);
563 	if (ip->il_mtype <= IMPTYPE_READY)
564 		printf("%s,", impleaders[ip->il_mtype]);
565 	else
566 		printf("%x,", ip->il_mtype);
567 	printf("htype=%x,host=%x,imp=%x,link=", ip->il_htype, ip->il_host,
568 		ntohs(ip->il_imp));
569 	if (ip->il_link == IMPLINK_IP)
570 		printf("ip,");
571 	else
572 		printf("%x,", ip->il_link);
573 	printf("subtype=%x,len=%x>\n",ip->il_subtype,ntohs(ip->il_length)>>3);
574 }
575 
576 printbyte(cp, n)
577 	register char *cp;
578 	int n;
579 {
580 	register i, j, c;
581 
582 	for (i=0; i<n; i++) {
583 		c = *cp++;
584 		for (j=0; j<2; j++)
585 			putchar("0123456789abcdef"[(c>>((1-j)*4))&0xf]);
586 		putchar(' ');
587 	}
588 	putchar('\n');
589 }
590 #endif
591 #endif
592