if_loop.c revision 1.11 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 * from: @(#)if_loop.c 8.1 (Berkeley) 6/10/93
34 * $Id: if_loop.c,v 1.11 1994/05/13 06:02:44 mycroft Exp $
35 */
36
37 /*
38 * Loopback interface driver for protocol testing and timing.
39 */
40
41 #include "bpfilter.h"
42 #include "loop.h"
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/mbuf.h>
48 #include <sys/socket.h>
49 #include <sys/errno.h>
50 #include <sys/ioctl.h>
51 #include <sys/time.h>
52
53 #include <machine/cpu.h>
54
55 #include <net/if.h>
56 #include <net/if_types.h>
57 #include <net/netisr.h>
58 #include <net/route.h>
59
60 #ifdef INET
61 #include <netinet/in.h>
62 #include <netinet/in_systm.h>
63 #include <netinet/in_var.h>
64 #include <netinet/ip.h>
65 #endif
66
67 #ifdef NS
68 #include <netns/ns.h>
69 #include <netns/ns_if.h>
70 #endif
71
72 #ifdef ISO
73 #include <netiso/iso.h>
74 #include <netiso/iso_var.h>
75 #endif
76
77 #if NBPFILTER > 0
78 #include <net/bpf.h>
79 #endif
80
81 #define LOMTU (32768)
82
83 struct ifnet loif[NLOOP];
84
85 void
86 loopattach(n)
87 int n;
88 {
89 register int i;
90 register struct ifnet *ifp;
91
92 for (i = 0; i < NLOOP; i++) {
93 ifp = &loif[i];
94 ifp->if_unit = i;
95 ifp->if_name = "lo";
96 ifp->if_mtu = LOMTU;
97 ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST;
98 ifp->if_ioctl = loioctl;
99 ifp->if_output = looutput;
100 ifp->if_type = IFT_LOOP;
101 ifp->if_hdrlen = 0;
102 ifp->if_addrlen = 0;
103 if_attach(ifp);
104 #if NBPFILTER > 0
105 bpfattach(&ifp->if_bpf, ifp, DLT_NULL, sizeof(u_int));
106 #endif
107 }
108 }
109
110 int
111 looutput(ifp, m, dst, rt)
112 struct ifnet *ifp;
113 register struct mbuf *m;
114 struct sockaddr *dst;
115 register struct rtentry *rt;
116 {
117 int s, isr;
118 register struct ifqueue *ifq = 0;
119
120 if ((m->m_flags & M_PKTHDR) == 0)
121 panic("looutput no HDR");
122 ifp->if_lastchange = time;
123 #if NBPFILTER > 0
124 if (ifp->if_bpf) {
125 /*
126 * We need to prepend the address family as
127 * a four byte field. Cons up a dummy header
128 * to pacify bpf. This is safe because bpf
129 * will only read from the mbuf (i.e., it won't
130 * try to free it or keep a pointer to it).
131 */
132 struct mbuf m0;
133 u_int af = dst->sa_family;
134
135 m0.m_next = m;
136 m0.m_len = 4;
137 m0.m_data = (char *)⁡
138
139 bpf_mtap(ifp->if_bpf, &m0);
140 }
141 #endif
142 m->m_pkthdr.rcvif = ifp;
143
144 if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
145 m_freem(m);
146 return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
147 rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
148 }
149 ifp->if_opackets++;
150 ifp->if_obytes += m->m_pkthdr.len;
151 switch (dst->sa_family) {
152
153 #ifdef INET
154 case AF_INET:
155 ifq = &ipintrq;
156 isr = NETISR_IP;
157 break;
158 #endif
159 #ifdef NS
160 case AF_NS:
161 ifq = &nsintrq;
162 isr = NETISR_NS;
163 break;
164 #endif
165 #ifdef ISO
166 case AF_ISO:
167 ifq = &clnlintrq;
168 isr = NETISR_ISO;
169 break;
170 #endif
171 default:
172 printf("lo%d: can't handle af%d\n", ifp->if_unit,
173 dst->sa_family);
174 m_freem(m);
175 return (EAFNOSUPPORT);
176 }
177 s = splimp();
178 if (IF_QFULL(ifq)) {
179 IF_DROP(ifq);
180 m_freem(m);
181 splx(s);
182 return (ENOBUFS);
183 }
184 IF_ENQUEUE(ifq, m);
185 schednetisr(isr);
186 ifp->if_ipackets++;
187 ifp->if_ibytes += m->m_pkthdr.len;
188 splx(s);
189 return (0);
190 }
191
192 /* ARGSUSED */
193 void
194 lortrequest(cmd, rt, sa)
195 int cmd;
196 struct rtentry *rt;
197 struct sockaddr *sa;
198 {
199
200 if (rt)
201 rt->rt_rmx.rmx_mtu = LOMTU;
202 }
203
204 /*
205 * Process an ioctl request.
206 */
207 /* ARGSUSED */
208 int
209 loioctl(ifp, cmd, data)
210 register struct ifnet *ifp;
211 int cmd;
212 caddr_t data;
213 {
214 register struct ifaddr *ifa;
215 register struct ifreq *ifr;
216 register int error = 0;
217
218 switch (cmd) {
219
220 case SIOCSIFADDR:
221 ifp->if_flags |= IFF_UP;
222 ifa = (struct ifaddr *)data;
223 if (ifa != 0 && ifa->ifa_addr->sa_family == AF_ISO)
224 ifa->ifa_rtrequest = lortrequest;
225 /*
226 * Everything else is done at a higher level.
227 */
228 break;
229
230 case SIOCADDMULTI:
231 case SIOCDELMULTI:
232 ifr = (struct ifreq *)data;
233 if (ifr == 0) {
234 error = EAFNOSUPPORT; /* XXX */
235 break;
236 }
237 switch (ifr->ifr_addr.sa_family) {
238
239 #ifdef INET
240 case AF_INET:
241 break;
242 #endif
243
244 default:
245 error = EAFNOSUPPORT;
246 break;
247 }
248 break;
249
250 default:
251 error = EINVAL;
252 }
253 return (error);
254 }
255