xref: /original-bsd/sys/net/if_loop.c (revision b3b53e97)
1 /*	if_loop.c	4.9	82/03/30	*/
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 
19 #define	LONET	127
20 #define	LOMTU	(1024+512)
21 
22 struct	ifnet loif;
23 int	looutput();
24 
25 loattach()
26 {
27 	register struct ifnet *ifp = &loif;
28 	register struct sockaddr_in *sin;
29 
30 COUNT(LOATTACH);
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_DIRECT|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 COUNT(LOOUTPUT);
52 	ifp->if_opackets++;
53 	switch (dst->sa_family) {
54 
55 #ifdef INET
56 	case AF_INET:
57 		ifq = &ipintrq;
58 		if (IF_QFULL(ifq)) {
59 			IF_DROP(ifq);
60 			m_freem(m0);
61 			splx(s);
62 			return (0);
63 		}
64 		IF_ENQUEUE(ifq, m0);
65 		schednetisr(NETISR_IP);
66 		break;
67 #endif
68 	default:
69 		splx(s);
70 		printf("lo%d: can't handle af%d\n", ifp->if_unit,
71 			dst->sa_family);
72 		m_freem(m0);
73 		return (0);
74 	}
75 	ifp->if_ipackets++;
76 	splx(s);
77 	return (1);
78 }
79