xref: /csrg-svn/sys/net/if_loop.c (revision 8397)
1*8397Swnj /*	if_loop.c	4.14	82/10/09	*/
25122Swnj 
35122Swnj /*
45122Swnj  * Loopback interface driver for protocol testing and timing.
55122Swnj  */
65122Swnj 
75122Swnj #include "../h/param.h"
85122Swnj #include "../h/systm.h"
95122Swnj #include "../h/mbuf.h"
105122Swnj #include "../h/socket.h"
11*8397Swnj #include "../netinet/in.h"
12*8397Swnj #include "../netinet/in_systm.h"
135122Swnj #include "../net/if.h"
14*8397Swnj #include "../net/netisr.h"
15*8397Swnj #include "../netinet/ip.h"
16*8397Swnj #include "../netinet/ip_var.h"
175122Swnj #include "../h/mtpr.h"
186366Ssam #include "../net/route.h"
196556Ssam #include <errno.h>
205122Swnj 
215296Sroot #define	LONET	127
225207Swnj #define	LOMTU	(1024+512)
235122Swnj 
245122Swnj struct	ifnet loif;
255122Swnj int	looutput();
265122Swnj 
275122Swnj loattach()
285122Swnj {
295122Swnj 	register struct ifnet *ifp = &loif;
306337Ssam 	register struct sockaddr_in *sin;
315122Swnj 
325172Swnj 	ifp->if_name = "lo";
335122Swnj 	ifp->if_mtu = LOMTU;
345122Swnj 	ifp->if_net = LONET;
356337Ssam 	sin = (struct sockaddr_in *)&ifp->if_addr;
366337Ssam 	sin->sin_family = AF_INET;
376337Ssam 	sin->sin_addr = if_makeaddr(ifp->if_net, 0);
386337Ssam 	ifp->if_flags = IFF_UP;
395122Swnj 	ifp->if_output = looutput;
405161Swnj 	if_attach(ifp);
417154Swnj 	if_rtinit(ifp, RTF_UP);
425122Swnj }
435122Swnj 
446337Ssam looutput(ifp, m0, dst)
455122Swnj 	struct ifnet *ifp;
465122Swnj 	struct mbuf *m0;
476337Ssam 	struct sockaddr *dst;
485122Swnj {
495122Swnj 	int s = splimp();
506209Swnj 	register struct ifqueue *ifq;
515122Swnj 
525172Swnj 	ifp->if_opackets++;
536337Ssam 	switch (dst->sa_family) {
545122Swnj 
555122Swnj #ifdef INET
566337Ssam 	case AF_INET:
576209Swnj 		ifq = &ipintrq;
586209Swnj 		if (IF_QFULL(ifq)) {
596209Swnj 			IF_DROP(ifq);
606337Ssam 			m_freem(m0);
616209Swnj 			splx(s);
626544Sfeldman 			return (ENOBUFS);
636209Swnj 		}
646209Swnj 		IF_ENQUEUE(ifq, m0);
656262Swnj 		schednetisr(NETISR_IP);
665122Swnj 		break;
675122Swnj #endif
685122Swnj 	default:
695122Swnj 		splx(s);
706337Ssam 		printf("lo%d: can't handle af%d\n", ifp->if_unit,
716337Ssam 			dst->sa_family);
726337Ssam 		m_freem(m0);
736544Sfeldman 		return (EAFNOSUPPORT);
745122Swnj 	}
755172Swnj 	ifp->if_ipackets++;
765122Swnj 	splx(s);
776544Sfeldman 	return (0);
785122Swnj }
79