xref: /csrg-svn/sys/net/if_loop.c (revision 7239)
1 /*	if_loop.c	4.13	82/06/20	*/
2 
3 /*
4  * Loopback interface driver for protocol testing and timing.
5  */
6 
7 #include "../h/param.h"
8 #include "../h/systm.h"
9 #include "../h/mbuf.h"
10 #include "../h/socket.h"
11 #include "../net/in.h"
12 #include "../net/in_systm.h"
13 #include "../net/if.h"
14 #include "../net/ip.h"
15 #include "../net/ip_var.h"
16 #include "../h/mtpr.h"
17 #include "../net/route.h"
18 #include <errno.h>
19 
20 #define	LONET	127
21 #define	LOMTU	(1024+512)
22 
23 struct	ifnet loif;
24 int	looutput();
25 
26 loattach()
27 {
28 	register struct ifnet *ifp = &loif;
29 	register struct sockaddr_in *sin;
30 
31 	ifp->if_name = "lo";
32 	ifp->if_mtu = LOMTU;
33 	ifp->if_net = LONET;
34 	sin = (struct sockaddr_in *)&ifp->if_addr;
35 	sin->sin_family = AF_INET;
36 	sin->sin_addr = if_makeaddr(ifp->if_net, 0);
37 	ifp->if_flags = IFF_UP;
38 	ifp->if_output = looutput;
39 	if_attach(ifp);
40 	if_rtinit(ifp, RTF_UP);
41 }
42 
43 looutput(ifp, m0, dst)
44 	struct ifnet *ifp;
45 	struct mbuf *m0;
46 	struct sockaddr *dst;
47 {
48 	int s = splimp();
49 	register struct ifqueue *ifq;
50 
51 	ifp->if_opackets++;
52 	switch (dst->sa_family) {
53 
54 #ifdef INET
55 	case AF_INET:
56 		ifq = &ipintrq;
57 		if (IF_QFULL(ifq)) {
58 			IF_DROP(ifq);
59 			m_freem(m0);
60 			splx(s);
61 			return (ENOBUFS);
62 		}
63 		IF_ENQUEUE(ifq, m0);
64 		schednetisr(NETISR_IP);
65 		break;
66 #endif
67 	default:
68 		splx(s);
69 		printf("lo%d: can't handle af%d\n", ifp->if_unit,
70 			dst->sa_family);
71 		m_freem(m0);
72 		return (EAFNOSUPPORT);
73 	}
74 	ifp->if_ipackets++;
75 	splx(s);
76 	return (0);
77 }
78