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