xref: /freebsd/sys/net/if_loop.c (revision ba582a82b0e36455da2d545ca1a71155ec32d033)
1 /*
2  * Copyright (c) 1982, 1986, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *	@(#)if_loop.c	8.1 (Berkeley) 6/10/93
34  */
35 
36 /*
37  * Loopback interface driver for protocol testing and timing.
38  */
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/mbuf.h>
44 #include <sys/socket.h>
45 #include <sys/errno.h>
46 #include <sys/ioctl.h>
47 #include <sys/time.h>
48 #include <machine/cpu.h>
49 
50 #include <net/if.h>
51 #include <net/if_types.h>
52 #include <net/netisr.h>
53 #include <net/route.h>
54 #include <net/bpf.h>
55 
56 #ifdef	INET
57 #include <netinet/in.h>
58 #include <netinet/in_systm.h>
59 #include <netinet/in_var.h>
60 #include <netinet/ip.h>
61 #endif
62 
63 #ifdef NS
64 #include <netns/ns.h>
65 #include <netns/ns_if.h>
66 #endif
67 
68 #ifdef ISO
69 #include <netiso/iso.h>
70 #include <netiso/iso_var.h>
71 #endif
72 
73 #include "bpfilter.h"
74 
75 #ifdef TINY_LOMTU
76 #define	LOMTU	(1024+512)
77 #else
78 #define LOMTU	65532
79 #endif
80 
81 struct	ifnet loif;
82 
83 /* ARGSUSED */
84 void
85 loopattach(void)
86 {
87 	register struct ifnet *ifp = &loif;
88 
89 	ifp->if_name = "lo";
90 	ifp->if_mtu = LOMTU;
91 	ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST;
92 	ifp->if_ioctl = loioctl;
93 	ifp->if_output = looutput;
94 	ifp->if_type = IFT_LOOP;
95 	ifp->if_hdrlen = 0;
96 	ifp->if_addrlen = 0;
97 	if_attach(ifp);
98 #if NBPFILTER > 0
99 	bpfattach(&ifp->if_bpf, ifp, DLT_NULL, sizeof(u_int));
100 #endif
101 }
102 
103 TEXT_SET(pseudo_set, loopattach);
104 
105 int
106 looutput(ifp, m, dst, rt)
107 	struct ifnet *ifp;
108 	register struct mbuf *m;
109 	struct sockaddr *dst;
110 	register struct rtentry *rt;
111 {
112 	int s, isr;
113 	register struct ifqueue *ifq = 0;
114 
115 	if ((m->m_flags & M_PKTHDR) == 0)
116 		panic("looutput no HDR");
117 	ifp->if_lastchange = time;
118 #if NBPFILTER > 0
119 	if (loif.if_bpf) {
120 		/*
121 		 * We need to prepend the address family as
122 		 * a four byte field.  Cons up a dummy header
123 		 * to pacify bpf.  This is safe because bpf
124 		 * will only read from the mbuf (i.e., it won't
125 		 * try to free it or keep a pointer a to it).
126 		 */
127 		struct mbuf m0;
128 		u_int af = dst->sa_family;
129 
130 		m0.m_next = m;
131 		m0.m_len = 4;
132 		m0.m_data = (char *)&af;
133 
134 		bpf_mtap(loif.if_bpf, &m0);
135 	}
136 #endif
137 	m->m_pkthdr.rcvif = ifp;
138 
139 	if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
140 		m_freem(m);
141 		return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
142 		        rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
143 	}
144 	ifp->if_opackets++;
145 	ifp->if_obytes += m->m_pkthdr.len;
146 	switch (dst->sa_family) {
147 
148 #ifdef INET
149 	case AF_INET:
150 		ifq = &ipintrq;
151 		isr = NETISR_IP;
152 		break;
153 #endif
154 #ifdef NS
155 	case AF_NS:
156 		ifq = &nsintrq;
157 		isr = NETISR_NS;
158 		break;
159 #endif
160 #ifdef ISO
161 	case AF_ISO:
162 		ifq = &clnlintrq;
163 		isr = NETISR_ISO;
164 		break;
165 #endif
166 	default:
167 		printf("lo%d: can't handle af%d\n", ifp->if_unit,
168 			dst->sa_family);
169 		m_freem(m);
170 		return (EAFNOSUPPORT);
171 	}
172 	s = splimp();
173 	if (IF_QFULL(ifq)) {
174 		IF_DROP(ifq);
175 		m_freem(m);
176 		splx(s);
177 		return (ENOBUFS);
178 	}
179 	IF_ENQUEUE(ifq, m);
180 	schednetisr(isr);
181 	ifp->if_ipackets++;
182 	ifp->if_ibytes += m->m_pkthdr.len;
183 	splx(s);
184 	return (0);
185 }
186 
187 /* ARGSUSED */
188 void
189 lortrequest(cmd, rt, sa)
190 	int cmd;
191 	struct rtentry *rt;
192 	struct sockaddr *sa;
193 {
194 
195 	if (rt)
196 		rt->rt_rmx.rmx_mtu = LOMTU;
197 }
198 
199 /*
200  * Process an ioctl request.
201  */
202 /* ARGSUSED */
203 int
204 loioctl(ifp, cmd, data)
205 	register struct ifnet *ifp;
206 	int cmd;
207 	caddr_t data;
208 {
209 	register struct ifaddr *ifa;
210 	register struct ifreq *ifr;
211 	register int error = 0;
212 
213 	switch (cmd) {
214 
215 	case SIOCSIFADDR:
216 		ifp->if_flags |= IFF_UP;
217 		ifa = (struct ifaddr *)data;
218 		if (ifa != 0 && ifa->ifa_addr->sa_family == AF_ISO)
219 			ifa->ifa_rtrequest = lortrequest;
220 		/*
221 		 * Everything else is done at a higher level.
222 		 */
223 		break;
224 
225 	case SIOCADDMULTI:
226 	case SIOCDELMULTI:
227 		ifr = (struct ifreq *)data;
228 		if (ifr == 0) {
229 			error = EAFNOSUPPORT;		/* XXX */
230 			break;
231 		}
232 		switch (ifr->ifr_addr.sa_family) {
233 
234 #ifdef INET
235 		case AF_INET:
236 			break;
237 #endif
238 
239 		default:
240 			error = EAFNOSUPPORT;
241 			break;
242 		}
243 		break;
244 
245 	default:
246 		error = EINVAL;
247 	}
248 	return (error);
249 }
250