1 /*	if_imp.c	4.26	82/04/24	*/
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 #include "../h/param.h"
12 #include "../h/systm.h"
13 #include "../h/mbuf.h"
14 #include "../h/pte.h"
15 #include "../h/buf.h"
16 #include "../h/protosw.h"
17 #include "../h/socket.h"
18 #include "../h/ubareg.h"
19 #include "../h/ubavar.h"
20 #include "../h/cpu.h"
21 #include "../h/mtpr.h"
22 #include "../h/vmmac.h"
23 #include "../net/in.h"
24 #include "../net/in_systm.h"
25 #include "../net/if.h"
26 #include "../net/if_imp.h"
27 #include "../net/if_imphost.h"
28 #include "../net/ip.h"
29 #include "../net/ip_var.h"
30 #include "../net/route.h"
31 #include <errno.h>
32 
33 /*
34  * IMP software status per interface.
35  * (partially shared with the hardware specific module)
36  *
37  * Each interface is referenced by a network interface structure,
38  * imp_if, which the routing code uses to locate the interface.
39  * This structure contains the output queue for the interface, its
40  * address, ...  IMP specific structures used in connecting the
41  * IMP software modules to the hardware specific interface routines
42  * are stored here.  The common structures are made visible to the
43  * interface driver by passing a pointer to the hardware routine
44  * at "attach" time.
45  *
46  * NOTE: imp_if and imp_cb are assumed adjacent in hardware code.
47  */
48 struct imp_softc {
49 	struct	ifnet imp_if;		/* network visible interface */
50 	struct	impcb imp_cb;		/* hooks to hardware module */
51 	u_char	imp_state;		/* current state of IMP */
52 	char	imp_dropcnt;		/* used during initialization */
53 } imp_softc[NIMP];
54 
55 /*
56  * Messages from IMP regarding why
57  * it's going down.
58  */
59 static char *impmessage[] = {
60 	"in 30 seconds",
61 	"for hardware PM",
62 	"to reload software",
63 	"for emergency reset"
64 };
65 
66 int	impdown(), impinit(), impoutput();
67 
68 /*
69  * IMP attach routine.  Called from hardware device attach routine
70  * at configuration time with a pointer to the UNIBUS device structure.
71  * Sets up local state and returns pointer to base of ifnet+impcb
72  * structures.  This is then used by the device's attach routine
73  * set up its back pointers.
74  */
75 impattach(ui)
76 	struct uba_device *ui;
77 {
78 	struct imp_softc *sc = &imp_softc[ui->ui_unit];
79 	register struct ifnet *ifp = &sc->imp_if;
80 	struct sockaddr_in *sin;
81 
82 COUNT(IMPATTACH);
83 	/* UNIT COULD BE AMBIGUOUS */
84 	ifp->if_unit = ui->ui_unit;
85 	ifp->if_name = "imp";
86 	ifp->if_mtu = IMPMTU - sizeof(struct imp_leader);
87 	ifp->if_net = ui->ui_flags;
88 	/* the host and imp fields will be filled in by the imp */
89 	sin = (struct sockaddr_in *)&ifp->if_addr;
90 	sin->sin_family = AF_INET;
91 	sin->sin_addr = if_makeaddr(ifp->if_net, 0);
92 	ifp->if_init = impinit;
93 	ifp->if_output = impoutput;
94 	/* reset is handled at the hardware level */
95 	if_attach(ifp);
96 	return ((int)&sc->imp_if);
97 }
98 
99 /*
100  * IMP initialization routine: call hardware module to
101  * setup UNIBUS resources, init state and get ready for
102  * NOOPs the IMP should send us, and that we want to drop.
103  */
104 impinit(unit)
105 	int unit;
106 {
107 	int s = splimp();
108 	register struct imp_softc *sc = &imp_softc[unit];
109 
110 COUNT(IMPINIT);
111 	if ((*sc->imp_cb.ic_init)(unit) == 0) {
112 		sc->imp_state = IMPS_DOWN;
113 		sc->imp_if.if_flags &= ~IFF_UP;
114 		splx(s);
115 		return;
116 	}
117 	sc->imp_state = IMPS_INIT;
118 	impnoops(sc);
119 	if_rtinit(&sc->imp_if, RTF_DIRECT|RTF_UP);
120 	splx(s);
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);
205 			impnoops(sc);
206 		}
207 		goto drop;
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 drop;
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 		sin = (struct sockaddr_in *)&sc->imp_if.if_addr;
236 		if (sin->sin_addr.s_host != ip->il_host ||
237 		    sin->sin_addr.s_imp != ip->il_imp) {
238 			sc->imp_if.if_host[0] =
239 				sin->sin_addr.s_host = ip->il_host;
240 			sin->sin_addr.s_imp = ip->il_imp;
241 			impmsg(sc, "reset (host %d/imp %d)", (u_int)ip->il_host,
242 				ntohs(ip->il_imp));
243 		}
244 		sc->imp_state = IMPS_UP;
245 		sc->imp_if.if_flags |= IFF_UP;
246 		/* restart output in case something was q'd */
247 		(*sc->imp_cb.ic_start)(sc->imp_if.if_unit);
248 		goto drop;
249 
250 	/*
251 	 * RFNM or INCOMPLETE message, send next
252 	 * message on the q.  We could pass incomplete's
253 	 * up to the next level, but this currently isn't
254 	 * needed.
255 	 */
256 	case IMPTYPE_RFNM:
257 	case IMPTYPE_INCOMPLETE:
258 		if (hp && hp->h_rfnm && (next = hostdeque(hp)))
259 			(void) impsnd(&sc->imp_if, next);
260 		goto drop;
261 
262 	/*
263 	 * Host or IMP can't be reached.  Flush any packets
264 	 * awaiting transmission and release the host structure.
265 	 */
266 	case IMPTYPE_HOSTDEAD:
267 	case IMPTYPE_HOSTUNREACH:
268 		impnotify(ip->il_mtype, ip, hp);
269 		goto rawlinkin;
270 
271 	/*
272 	 * Error in data.  Clear RFNM status for this host and send
273 	 * noops to the IMP to clear the interface.
274 	 */
275 	case IMPTYPE_BADDATA:
276 		impmsg(sc, "data error");
277 		if (hp)
278 			hp->h_rfnm = 0;
279 		impnoops(sc);
280 		goto drop;
281 
282 	/*
283 	 * Interface reset.
284 	 */
285 	case IMPTYPE_RESET:
286 		impmsg(sc, "interface reset");
287 		impnoops(sc);
288 		goto drop;
289 
290 	default:
291 		sc->imp_if.if_collisions++;		/* XXX */
292 		goto drop;
293 	}
294 
295 	/*
296 	 * Data for a protocol.  Dispatch to the appropriate
297 	 * protocol routine (running at software interrupt).
298 	 * If this isn't a raw interface, advance pointer
299 	 * into mbuf past leader.
300 	 */
301 	switch (ip->il_link) {
302 
303 #ifdef INET
304 	case IMPLINK_IP:
305 		m->m_len -= sizeof(struct imp_leader);
306 		m->m_off += sizeof(struct imp_leader);
307 		schednetisr(NETISR_IP);
308 		inq = &ipintrq;
309 		break;
310 #endif
311 
312 	default:
313 	rawlinkin:
314 		impproto.sp_protocol = ip->il_link;
315 		sin = (struct sockaddr_in *)&sc->imp_if.if_addr;
316 		impdst.sin_addr = sin->sin_addr;;
317 		impsrc.sin_addr.s_net = ip->il_network;
318 		impsrc.sin_addr.s_host = ip->il_host;
319 		impsrc.sin_addr.s_imp = ip->il_imp;
320 		raw_input(m, &impproto, (struct sockaddr *)&impsrc,
321 		  (struct sockaddr *)&impdst);
322 		return;
323 	}
324 	if (IF_QFULL(inq)) {
325 		IF_DROP(inq);
326 		goto drop;
327 	}
328 	IF_ENQUEUE(inq, m);
329 	return;
330 
331 drop:
332 	m_freem(m);
333 }
334 
335 /*
336  * Bring the IMP down after notification.
337  */
338 impdown(sc)
339 	struct imp_softc *sc;
340 {
341 
342 	sc->imp_state = IMPS_DOWN;
343 	impmsg(sc, "marked down");
344 	if_down(&sc->imp_if);
345 }
346 
347 /*VARARGS*/
348 impmsg(sc, fmt, a1, a2)
349 	struct imp_softc *sc;
350 	char *fmt;
351 	u_int a1;
352 {
353 
354 	printf("imp%d: ", sc->imp_if.if_unit);
355 	printf(fmt, a1, a2);
356 	printf("\n");
357 }
358 
359 /*
360  * Process an IMP "error" message, passing this
361  * up to the higher level protocol.
362  */
363 impnotify(what, cp, hp)
364 	int what;
365 	struct control_leader *cp;
366 	struct host *hp;
367 {
368 	struct in_addr in;
369 
370 #ifdef notdef
371 	in.s_net = cp->dl_network;
372 #else
373 	in.s_net = 10;
374 #endif
375 	in.s_host = cp->dl_host;
376 	in.s_imp = cp->dl_imp;
377 	if (cp->dl_link != IMPLINK_IP)
378 		raw_ctlinput(what, (caddr_t)&in);
379 	else
380 		ip_ctlinput(what, (caddr_t)&in);
381 	if (hp)
382 		hostfree(hp);
383 }
384 
385 /*
386  * ARPAnet 1822 output routine.
387  * Called from higher level protocol routines to set up messages for
388  * transmission to the imp.  Sets up the header and calls impsnd to
389  * enqueue the message for this IMP's hardware driver.
390  */
391 impoutput(ifp, m0, dst)
392 	register struct ifnet *ifp;
393 	struct mbuf *m0;
394 	struct sockaddr *dst;
395 {
396 	register struct imp_leader *imp;
397 	register struct mbuf *m = m0;
398 	int x, dhost, dimp, dlink, len, dnet;
399 	int error = 0;
400 
401 COUNT(IMPOUTPUT);
402 	/*
403 	 * Don't even try if the IMP is unavailable.
404 	 */
405 	if (imp_softc[ifp->if_unit].imp_state != IMPS_UP) {
406 		error = ENETDOWN;
407 		goto drop;
408 	}
409 
410 	switch (dst->sa_family) {
411 
412 #ifdef INET
413 	case AF_INET: {
414 		struct ip *ip = mtod(m0, struct ip *);
415 		struct sockaddr_in *sin = (struct sockaddr_in *)dst;
416 
417 		dhost = sin->sin_addr.s_host;
418 		dimp = sin->sin_addr.s_impno;
419 		dlink = IMPLINK_IP;
420 		dnet = 0;
421 		len = ntohs((u_short)ip->ip_len);
422 		break;
423 	}
424 #endif
425 	case AF_IMPLINK:
426 		goto leaderexists;
427 
428 	default:
429 		printf("imp%d: can't handle af%d\n", ifp->if_unit,
430 			dst->sa_family);
431 		error = EAFNOSUPPORT;
432 		goto drop;
433 	}
434 
435 	/*
436 	 * Add IMP leader.  If there's not enough space in the
437 	 * first mbuf, allocate another.  If that should fail, we
438 	 * drop this sucker.
439 	 */
440 	if (m->m_off > MMAXOFF ||
441 	    MMINOFF + sizeof(struct imp_leader) > m->m_off) {
442 		m = m_get(M_DONTWAIT);
443 		if (m == 0) {
444 			error = ENOBUFS;
445 			goto drop;
446 		}
447 		m->m_next = m0;
448 		m->m_off = MMINOFF;
449 		m->m_len = sizeof(struct imp_leader);
450 	} else {
451 		m->m_off -= sizeof(struct imp_leader);
452 		m->m_len += sizeof(struct imp_leader);
453 	}
454 	imp = mtod(m, struct imp_leader *);
455 	imp->il_format = IMP_NFF;
456 	imp->il_mtype = IMPTYPE_DATA;
457 	imp->il_network = dnet;
458 	imp->il_host = dhost;
459 	imp->il_imp = htons((u_short)dimp);
460 	imp->il_length =
461 		htons((u_short)(len + sizeof(struct imp_leader)) << 3);
462 	imp->il_link = dlink;
463 	imp->il_flags = imp->il_htype = imp->il_subtype = 0;
464 
465 leaderexists:
466 	return (impsnd(ifp, m));
467 drop:
468 	m_freem(m0);
469 	return (error);
470 }
471 
472 /*
473  * Put a message on an interface's output queue.
474  * Perform RFNM counting: no more than 8 message may be
475  * in flight to any one host.
476  */
477 impsnd(ifp, m)
478 	struct ifnet *ifp;
479 	struct mbuf *m;
480 {
481 	register struct imp_leader *ip;
482 	register struct host *hp;
483 	struct impcb *icp;
484 	int x;
485 
486 COUNT(IMPSND);
487 	ip = mtod(m, struct imp_leader *);
488 
489 	/*
490 	 * Do RFNM counting for data messages
491 	 * (no more than 8 outstanding to any host)
492 	 */
493 	x = splimp();
494 	if (ip->il_mtype == IMPTYPE_DATA) {
495 		struct in_addr addr;
496 
497 #ifdef notdef
498                 addr.s_net = ip->il_network;
499 #else
500 		addr.s_net = 0;
501 #endif
502                 addr.s_host = ip->il_host;
503                 addr.s_imp = ip->il_imp;
504 		if ((hp = hostlookup(addr)) == 0)
505 			hp = hostenter(addr);
506 
507 		/*
508 		 * If IMP would block, queue until RFNM
509 		 */
510 		if (hp) {
511 			if (hp->h_rfnm < 8) {
512 				hp->h_rfnm++;
513 				goto enque;
514 			}
515 			if (hp->h_qcnt < 8) {	/* high water mark */
516 				HOST_ENQUE(hp, m);
517 				goto start;
518 			}
519 		}
520 		m_freem(m);
521 		splx(x);
522 		return (ENOBUFS);	/* XXX */
523 	}
524 enque:
525 	if (IF_QFULL(&ifp->if_snd)) {
526 		IF_DROP(&ifp->if_snd);
527 		m_freem(m);
528 		splx(x);
529 		return (ENOBUFS);	/* XXX */
530 	}
531 	IF_ENQUEUE(&ifp->if_snd, m);
532 start:
533 	splx(x);
534 	icp = &imp_softc[ifp->if_unit].imp_cb;
535 	if (icp->ic_oactive == 0)
536 		(*icp->ic_start)(ifp->if_unit);
537 	return (0);
538 }
539 
540 /*
541  * Put three 1822 NOOPs at the head of the output queue.
542  * Part of host-IMP initialization procedure.
543  * (Should return success/failure, but noone knows
544  * what to do with this, so why bother?)
545  */
546 impnoops(sc)
547 	register struct imp_softc *sc;
548 {
549 	register i;
550 	register struct mbuf *m;
551 	register struct control_leader *cp;
552 	int x;
553 
554 COUNT(IMPNOOPS);
555 	sc->imp_dropcnt = IMP_DROPCNT;
556 	for (i = 0; i < IMP_DROPCNT + 1; i++ ) {
557 		if ((m = m_getclr(M_DONTWAIT)) == 0)
558 			return;
559 		m->m_off = MMINOFF;
560 		m->m_len = sizeof(struct control_leader);
561 		cp = mtod(m, struct control_leader *);
562 		cp->dl_format = IMP_NFF;
563                 cp->dl_link = i;
564                 cp->dl_mtype = IMPTYPE_NOOP;
565 		x = splimp();
566 		IF_PREPEND(&sc->imp_if.if_snd, m);
567 		splx(x);
568 	}
569 	if (sc->imp_cb.ic_oactive == 0)
570 		(*sc->imp_cb.ic_start)(sc->imp_if.if_unit);
571 }
572 #endif
573