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 | * $Id$ |
---|
35 | */ |
---|
36 | |
---|
37 | /* |
---|
38 | * Loopback interface driver for protocol testing and timing. |
---|
39 | */ |
---|
40 | #include "loop.h" |
---|
41 | #if NLOOP > 0 |
---|
42 | |
---|
43 | #include <sys/param.h> |
---|
44 | #include <sys/systm.h> |
---|
45 | #include <sys/kernel.h> |
---|
46 | #include <sys/mbuf.h> |
---|
47 | #include <sys/socket.h> |
---|
48 | #include <sys/errno.h> |
---|
49 | #include <sys/ioctl.h> |
---|
50 | #include <sys/time.h> |
---|
51 | |
---|
52 | #include <net/if.h> |
---|
53 | #include <net/if_types.h> |
---|
54 | #include <net/netisr.h> |
---|
55 | #include <net/route.h> |
---|
56 | #include <net/bpf.h> |
---|
57 | |
---|
58 | #ifdef INET |
---|
59 | #include <netinet/in.h> |
---|
60 | #include <netinet/in_systm.h> |
---|
61 | #include <netinet/in_var.h> |
---|
62 | #include <netinet/ip.h> |
---|
63 | #endif |
---|
64 | |
---|
65 | #ifdef IPX |
---|
66 | #include <netipx/ipx.h> |
---|
67 | #include <netipx/ipx_if.h> |
---|
68 | #endif |
---|
69 | |
---|
70 | #ifdef NS |
---|
71 | #include <netns/ns.h> |
---|
72 | #include <netns/ns_if.h> |
---|
73 | #endif |
---|
74 | |
---|
75 | #ifdef ISO |
---|
76 | #include <netiso/iso.h> |
---|
77 | #include <netiso/iso_var.h> |
---|
78 | #endif |
---|
79 | |
---|
80 | #ifdef NETATALK |
---|
81 | #include <netinet/if_ether.h> |
---|
82 | #include <netatalk/at.h> |
---|
83 | #include <netatalk/at_var.h> |
---|
84 | #endif /* NETATALK */ |
---|
85 | |
---|
86 | #include "bpfilter.h" |
---|
87 | |
---|
88 | static int loioctl __P((struct ifnet *, int, caddr_t)); |
---|
89 | static void lortrequest __P((int, struct rtentry *, struct sockaddr *)); |
---|
90 | |
---|
91 | void rtems_bsdnet_loopattach __P((void *)); |
---|
92 | PSEUDO_SET(loopattach, if_loop); |
---|
93 | |
---|
94 | #ifdef TINY_LOMTU |
---|
95 | #define LOMTU (1024+512) |
---|
96 | #else |
---|
97 | #define LOMTU 16384 |
---|
98 | #endif |
---|
99 | |
---|
100 | struct ifnet loif[NLOOP]; |
---|
101 | |
---|
102 | /* ARGSUSED */ |
---|
103 | void |
---|
104 | rtems_bsdnet_loopattach(dummy) |
---|
105 | void *dummy; |
---|
106 | { |
---|
107 | register struct ifnet *ifp; |
---|
108 | register int i = 0; |
---|
109 | |
---|
110 | for (ifp = loif; i < NLOOP; ifp++) { |
---|
111 | ifp->if_name = "lo"; |
---|
112 | ifp->if_next = NULL; |
---|
113 | ifp->if_unit = i++; |
---|
114 | ifp->if_mtu = LOMTU; |
---|
115 | ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST; |
---|
116 | ifp->if_ioctl = loioctl; |
---|
117 | ifp->if_output = looutput; |
---|
118 | ifp->if_type = IFT_LOOP; |
---|
119 | ifp->if_hdrlen = 0; |
---|
120 | ifp->if_addrlen = 0; |
---|
121 | ifp->if_snd.ifq_maxlen = ifqmaxlen; |
---|
122 | if_attach(ifp); |
---|
123 | #if NBPFILTER > 0 |
---|
124 | bpfattach(ifp, DLT_NULL, sizeof(u_int)); |
---|
125 | #endif |
---|
126 | } |
---|
127 | } |
---|
128 | |
---|
129 | int |
---|
130 | looutput(ifp, m, dst, rt) |
---|
131 | struct ifnet *ifp; |
---|
132 | register struct mbuf *m; |
---|
133 | struct sockaddr *dst; |
---|
134 | register struct rtentry *rt; |
---|
135 | { |
---|
136 | int s, isr; |
---|
137 | register struct ifqueue *ifq = 0; |
---|
138 | |
---|
139 | if ((m->m_flags & M_PKTHDR) == 0) |
---|
140 | panic("looutput no HDR"); |
---|
141 | #if NBPFILTER > 0 |
---|
142 | /* BPF write needs to be handled specially */ |
---|
143 | if (dst->sa_family == AF_UNSPEC) { |
---|
144 | dst->sa_family = *(mtod(m, int *)); |
---|
145 | m->m_len -= sizeof(int); |
---|
146 | m->m_pkthdr.len -= sizeof(int); |
---|
147 | m->m_data += sizeof(int); |
---|
148 | } |
---|
149 | |
---|
150 | if (ifp->if_bpf) { |
---|
151 | /* |
---|
152 | * We need to prepend the address family as |
---|
153 | * a four byte field. Cons up a dummy header |
---|
154 | * to pacify bpf. This is safe because bpf |
---|
155 | * will only read from the mbuf (i.e., it won't |
---|
156 | * try to free it or keep a pointer a to it). |
---|
157 | */ |
---|
158 | struct mbuf m0; |
---|
159 | u_int af = dst->sa_family; |
---|
160 | |
---|
161 | m0.m_next = m; |
---|
162 | m0.m_len = 4; |
---|
163 | m0.m_data = (char *)⁡ |
---|
164 | |
---|
165 | bpf_mtap(ifp, &m0); |
---|
166 | } |
---|
167 | #endif |
---|
168 | m->m_pkthdr.rcvif = ifp; |
---|
169 | |
---|
170 | if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) { |
---|
171 | m_freem(m); |
---|
172 | return (rt->rt_flags & RTF_BLACKHOLE ? 0 : |
---|
173 | rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH); |
---|
174 | } |
---|
175 | ifp->if_opackets++; |
---|
176 | ifp->if_obytes += m->m_pkthdr.len; |
---|
177 | switch (dst->sa_family) { |
---|
178 | |
---|
179 | #ifdef INET |
---|
180 | case AF_INET: |
---|
181 | ifq = &ipintrq; |
---|
182 | isr = NETISR_IP; |
---|
183 | break; |
---|
184 | #endif |
---|
185 | #ifdef IPX |
---|
186 | case AF_IPX: |
---|
187 | ifq = &ipxintrq; |
---|
188 | isr = NETISR_IPX; |
---|
189 | break; |
---|
190 | #endif |
---|
191 | #ifdef NS |
---|
192 | case AF_NS: |
---|
193 | ifq = &nsintrq; |
---|
194 | isr = NETISR_NS; |
---|
195 | break; |
---|
196 | #endif |
---|
197 | #ifdef ISO |
---|
198 | case AF_ISO: |
---|
199 | ifq = &clnlintrq; |
---|
200 | isr = NETISR_ISO; |
---|
201 | break; |
---|
202 | #endif |
---|
203 | #ifdef NETATALK |
---|
204 | case AF_APPLETALK: |
---|
205 | ifq = &atintrq2; |
---|
206 | isr = NETISR_ATALK; |
---|
207 | break; |
---|
208 | #endif /* NETATALK */ |
---|
209 | default: |
---|
210 | printf("lo%d: can't handle af%d\n", ifp->if_unit, |
---|
211 | dst->sa_family); |
---|
212 | m_freem(m); |
---|
213 | return (EAFNOSUPPORT); |
---|
214 | } |
---|
215 | s = splimp(); |
---|
216 | if (IF_QFULL(ifq)) { |
---|
217 | IF_DROP(ifq); |
---|
218 | m_freem(m); |
---|
219 | splx(s); |
---|
220 | return (ENOBUFS); |
---|
221 | } |
---|
222 | IF_ENQUEUE(ifq, m); |
---|
223 | schednetisr(isr); |
---|
224 | ifp->if_ipackets++; |
---|
225 | ifp->if_ibytes += m->m_pkthdr.len; |
---|
226 | splx(s); |
---|
227 | return (0); |
---|
228 | } |
---|
229 | |
---|
230 | /* ARGSUSED */ |
---|
231 | static void |
---|
232 | lortrequest(cmd, rt, sa) |
---|
233 | int cmd; |
---|
234 | struct rtentry *rt; |
---|
235 | struct sockaddr *sa; |
---|
236 | { |
---|
237 | if (rt) { |
---|
238 | rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu; /* for ISO */ |
---|
239 | /* |
---|
240 | * For optimal performance, the send and receive buffers |
---|
241 | * should be at least twice the MTU plus a little more for |
---|
242 | * overhead. |
---|
243 | */ |
---|
244 | rt->rt_rmx.rmx_recvpipe = |
---|
245 | rt->rt_rmx.rmx_sendpipe = 3 * LOMTU; |
---|
246 | } |
---|
247 | } |
---|
248 | |
---|
249 | /* |
---|
250 | * Process an ioctl request. |
---|
251 | */ |
---|
252 | /* ARGSUSED */ |
---|
253 | static int |
---|
254 | loioctl(ifp, cmd, data) |
---|
255 | register struct ifnet *ifp; |
---|
256 | int cmd; |
---|
257 | caddr_t data; |
---|
258 | { |
---|
259 | register struct ifaddr *ifa; |
---|
260 | register struct ifreq *ifr = (struct ifreq *)data; |
---|
261 | register int error = 0; |
---|
262 | |
---|
263 | switch (cmd) { |
---|
264 | |
---|
265 | case SIOCSIFADDR: |
---|
266 | ifp->if_flags |= IFF_UP | IFF_RUNNING; |
---|
267 | ifa = (struct ifaddr *)data; |
---|
268 | ifa->ifa_rtrequest = lortrequest; |
---|
269 | /* |
---|
270 | * Everything else is done at a higher level. |
---|
271 | */ |
---|
272 | break; |
---|
273 | |
---|
274 | case SIOCADDMULTI: |
---|
275 | case SIOCDELMULTI: |
---|
276 | if (ifr == 0) { |
---|
277 | error = EAFNOSUPPORT; /* XXX */ |
---|
278 | break; |
---|
279 | } |
---|
280 | switch (ifr->ifr_addr.sa_family) { |
---|
281 | |
---|
282 | #ifdef INET |
---|
283 | case AF_INET: |
---|
284 | break; |
---|
285 | #endif |
---|
286 | |
---|
287 | default: |
---|
288 | error = EAFNOSUPPORT; |
---|
289 | break; |
---|
290 | } |
---|
291 | break; |
---|
292 | |
---|
293 | case SIOCSIFMTU: |
---|
294 | ifp->if_mtu = ifr->ifr_mtu; |
---|
295 | break; |
---|
296 | |
---|
297 | default: |
---|
298 | error = EINVAL; |
---|
299 | } |
---|
300 | return (error); |
---|
301 | } |
---|
302 | #endif /* NLOOP > 0 */ |
---|