xref: /csrg-svn/sys/net/if_loop.c (revision 6544)
1*6544Sfeldman /*	if_loop.c	4.10	82/04/13	*/
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"
115122Swnj #include "../net/in.h"
125122Swnj #include "../net/in_systm.h"
135122Swnj #include "../net/if.h"
145122Swnj #include "../net/ip.h"
155122Swnj #include "../net/ip_var.h"
165122Swnj #include "../h/mtpr.h"
176366Ssam #include "../net/route.h"
185122Swnj 
195296Sroot #define	LONET	127
205207Swnj #define	LOMTU	(1024+512)
215122Swnj 
225122Swnj struct	ifnet loif;
235122Swnj int	looutput();
245122Swnj 
255122Swnj loattach()
265122Swnj {
275122Swnj 	register struct ifnet *ifp = &loif;
286337Ssam 	register struct sockaddr_in *sin;
295122Swnj 
306366Ssam COUNT(LOATTACH);
315172Swnj 	ifp->if_name = "lo";
325122Swnj 	ifp->if_mtu = LOMTU;
335122Swnj 	ifp->if_net = LONET;
346337Ssam 	sin = (struct sockaddr_in *)&ifp->if_addr;
356337Ssam 	sin->sin_family = AF_INET;
366337Ssam 	sin->sin_addr = if_makeaddr(ifp->if_net, 0);
376337Ssam 	ifp->if_flags = IFF_UP;
385122Swnj 	ifp->if_output = looutput;
395161Swnj 	if_attach(ifp);
406366Ssam 	if_rtinit(ifp, RTF_DIRECT|RTF_UP);
415122Swnj }
425122Swnj 
436337Ssam looutput(ifp, m0, dst)
445122Swnj 	struct ifnet *ifp;
455122Swnj 	struct mbuf *m0;
466337Ssam 	struct sockaddr *dst;
475122Swnj {
485122Swnj 	int s = splimp();
496209Swnj 	register struct ifqueue *ifq;
505122Swnj 
516366Ssam COUNT(LOOUTPUT);
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);
62*6544Sfeldman 			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);
73*6544Sfeldman 		return (EAFNOSUPPORT);
745122Swnj 	}
755172Swnj 	ifp->if_ipackets++;
765122Swnj 	splx(s);
77*6544Sfeldman 	return (0);
785122Swnj }
79