if_loop.c revision 1.4 1 /*
2 * Copyright (c) 1982, 1986 Regents of the University of California.
3 * 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 7.13 (Berkeley) 4/26/91
34 */
35
36 /*
37 * Loopback interface driver for protocol testing and timing.
38 */
39
40 #include "param.h"
41 #include "systm.h"
42 #include "mbuf.h"
43 #include "socket.h"
44 #include "errno.h"
45 #include "ioctl.h"
46 #include "protosw.h"
47
48 #include "loop.h"
49
50 #include "../net/if.h"
51 #include "../net/if_types.h"
52 #include "../net/netisr.h"
53 #include "../net/route.h"
54
55 #include "machine/mtpr.h"
56
57 #ifdef INET
58 #include "../netinet/in.h"
59 #include "../netinet/in_systm.h"
60 #include "../netinet/in_var.h"
61 #include "../netinet/ip.h"
62 #endif
63
64 #ifdef NS
65 #include "../netns/ns.h"
66 #include "../netns/ns_if.h"
67 #endif
68
69 #ifdef ISO
70 #include "../netiso/iso.h"
71 #include "../netiso/iso_var.h"
72 #endif
73
74 #include "bpfilter.h"
75 #if NBPFILTER > 0
76 #include <sys/time.h>
77 #include <net/bpf.h>
78 static caddr_t lo_bpf;
79 #endif
80
81 #define LOMTU (1024+512)
82
83 struct ifnet loif[NLOOP];
84 int looutput(), loioctl();
85
86 loattach()
87 {
88 register int i;
89 register struct ifnet *ifp;
90
91 for (i = 0; i < NLOOP; i++)
92 {
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;
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(&lo_bpf, ifp, DLT_NULL, sizeof(u_int));
106 #endif
107 }
108 }
109
110 looutput(ifp, m, dst, rt)
111 struct ifnet *ifp;
112 register struct mbuf *m;
113 struct sockaddr *dst;
114 register struct rtentry *rt;
115 {
116 int s, isr;
117 register struct ifqueue *ifq = 0;
118
119 if ((m->m_flags & M_PKTHDR) == 0)
120 panic("looutput no HDR");
121 #if NBPFILTER > 0
122 if (lo_bpf) {
123 /*
124 * We need to prepend the address family as
125 * a four byte field. Cons up a dummy header
126 * to pacify bpf. This is safe because bpf
127 * will only read from the mbuf (i.e., it won't
128 * try to free it or keep a pointer to it).
129 */
130 struct mbuf m0;
131 u_int af = dst->sa_family;
132
133 m0.m_next = m;
134 m0.m_len = 4;
135 m0.m_data = (char *)⁡
136
137 bpf_mtap(lo_bpf, &m0);
138 }
139 #endif
140 m->m_pkthdr.rcvif = ifp;
141
142 if (rt && rt->rt_flags & RTF_REJECT) {
143 m_freem(m);
144 return (rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
145 }
146 ifp->if_opackets++;
147 ifp->if_obytes += m->m_pkthdr.len;
148 switch (dst->sa_family) {
149
150 #ifdef INET
151 case AF_INET:
152 ifq = &ipintrq;
153 isr = NETISR_IP;
154 break;
155 #endif
156 #ifdef NS
157 case AF_NS:
158 ifq = &nsintrq;
159 isr = NETISR_NS;
160 break;
161 #endif
162 #ifdef ISO
163 case AF_ISO:
164 ifq = &clnlintrq;
165 isr = NETISR_ISO;
166 break;
167 #endif
168 default:
169 printf("lo%d: can't handle af%d\n", ifp->if_unit,
170 dst->sa_family);
171 m_freem(m);
172 return (EAFNOSUPPORT);
173 }
174 s = splimp();
175 if (IF_QFULL(ifq)) {
176 IF_DROP(ifq);
177 m_freem(m);
178 splx(s);
179 return (ENOBUFS);
180 }
181 IF_ENQUEUE(ifq, m);
182 schednetisr(isr);
183 ifp->if_ipackets++;
184 ifp->if_ibytes += m->m_pkthdr.len;
185 splx(s);
186 return (0);
187 }
188
189 /* ARGSUSED */
190 lortrequest(cmd, rt, sa)
191 struct rtentry *rt;
192 struct sockaddr *sa;
193 {
194 if (rt)
195 rt->rt_rmx.rmx_mtu = LOMTU;
196 }
197
198 /*
199 * Process an ioctl request.
200 */
201 /* ARGSUSED */
202 loioctl(ifp, cmd, data)
203 register struct ifnet *ifp;
204 int cmd;
205 caddr_t data;
206 {
207 register struct ifaddr *ifa;
208 int error = 0;
209
210 switch (cmd) {
211
212 case SIOCSIFADDR:
213 ifp->if_flags |= IFF_UP;
214 ifa = (struct ifaddr *)data;
215 if (ifa != 0 && ifa->ifa_addr->sa_family == AF_ISO)
216 ifa->ifa_rtrequest = lortrequest;
217 /*
218 * Everything else is done at a higher level.
219 */
220 break;
221
222 default:
223 error = EINVAL;
224 }
225 return (error);
226 }
227