tzsetup: Sync with FreeBSD.
[dragonfly.git] / sys / net / if_loop.c
CommitLineData
984263bc
MD
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
1fd46b85 34 * $FreeBSD: src/sys/net/if_loop.c,v 1.47.2.9 2004/02/08 08:40:24 silby Exp $
984263bc
MD
35 */
36
37/*
38 * Loopback interface driver for protocol testing and timing.
39 */
1f2de5d4 40#include "use_loop.h"
984263bc 41
984263bc
MD
42#include "opt_inet.h"
43#include "opt_inet6.h"
44#include "opt_ipx.h"
45
46#include <sys/param.h>
47#include <sys/systm.h>
48#include <sys/kernel.h>
82c6f048 49#include <sys/lock.h>
984263bc
MD
50#include <sys/mbuf.h>
51#include <sys/socket.h>
52#include <sys/sockio.h>
53
684a93c4
MD
54#include <sys/mplock2.h>
55
984263bc
MD
56#include <net/if.h>
57#include <net/if_types.h>
4d723e5a 58#include <net/ifq_var.h>
984263bc
MD
59#include <net/netisr.h>
60#include <net/route.h>
61#include <net/bpf.h>
62#include <net/bpfdesc.h>
63
64#ifdef INET
65#include <netinet/in.h>
66#include <netinet/in_var.h>
67#endif
68
69#ifdef IPX
d2438d69
MD
70#include <netproto/ipx/ipx.h>
71#include <netproto/ipx/ipx_if.h>
984263bc
MD
72#endif
73
74#ifdef INET6
75#ifndef INET
76#include <netinet/in.h>
77#endif
78#include <netinet6/in6_var.h>
79#include <netinet/ip6.h>
80#endif
81
7d982e78
SZ
82static void loopattach(void *);
83static int looutput(struct ifnet *, struct mbuf *, struct sockaddr *,
84 struct rtentry *);
85static int loioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
86static void lortrequest(int, struct rtentry *, struct rt_addrinfo *);
4d723e5a 87#ifdef ALTQ
f0a26983 88static void lo_altqstart(struct ifnet *, struct ifaltq_subque *);
4d723e5a 89#endif
984263bc
MD
90PSEUDO_SET(loopattach, if_loop);
91
984263bc
MD
92#ifdef TINY_LOMTU
93#define LOMTU (1024+512)
94#elif defined(LARGE_LOMTU)
95#define LOMTU 131072
96#else
97#define LOMTU 16384
98#endif
99
72662a67
SZ
100#define LO_CSUM_FEATURES (CSUM_IP | CSUM_UDP | CSUM_TCP)
101
984263bc
MD
102struct ifnet loif[NLOOP];
103
104/* ARGSUSED */
105static void
f23061d4 106loopattach(void *dummy)
984263bc 107{
82ed7fc2 108 struct ifnet *ifp;
f23061d4
JH
109 int i;
110
111 for (i = 0, ifp = loif; i < NLOOP; i++, ifp++) {
112 if_initname(ifp, "lo", i);
113 ifp->if_mtu = LOMTU;
114 ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST;
72662a67
SZ
115 ifp->if_capabilities = IFCAP_HWCSUM;
116 ifp->if_hwassist = LO_CSUM_FEATURES;
117 ifp->if_capenable = ifp->if_capabilities;
f23061d4
JH
118 ifp->if_ioctl = loioctl;
119 ifp->if_output = looutput;
120 ifp->if_type = IFT_LOOP;
4d723e5a
JS
121 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
122 ifq_set_ready(&ifp->if_snd);
123#ifdef ALTQ
124 ifp->if_start = lo_altqstart;
125#endif
78195a76 126 if_attach(ifp, NULL);
f23061d4 127 bpfattach(ifp, DLT_NULL, sizeof(u_int));
984263bc
MD
128 }
129}
130
7d982e78 131static int
2d2b761b
SZ
132looutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
133 struct rtentry *rt)
984263bc 134{
2d2b761b 135 M_ASSERTPKTHDR(m);
984263bc
MD
136
137 if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
138 m_freem(m);
139 return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
140 rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
141 }
984263bc
MD
142
143 ifp->if_opackets++;
144 ifp->if_obytes += m->m_pkthdr.len;
145#if 1 /* XXX */
146 switch (dst->sa_family) {
147 case AF_INET:
148 case AF_INET6:
149 case AF_IPX:
150 case AF_NS:
984263bc
MD
151 break;
152 default:
4b1cf444 153 kprintf("looutput: af=%d unexpected\n", dst->sa_family);
984263bc
MD
154 m_freem(m);
155 return (EAFNOSUPPORT);
156 }
157#endif
72662a67
SZ
158
159 if (ifp->if_capenable & IFCAP_RXCSUM) {
2d2b761b
SZ
160 int csum_flags = 0;
161
72662a67
SZ
162 if (m->m_pkthdr.csum_flags & CSUM_IP)
163 csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID);
164 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
165 csum_flags |= (CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
166
167 m->m_pkthdr.csum_flags |= csum_flags;
168 if (csum_flags & CSUM_DATA_VALID)
169 m->m_pkthdr.csum_data = 0xffff;
170 }
f23061d4 171 return (if_simloop(ifp, m, dst->sa_family, 0));
984263bc
MD
172}
173
174/*
175 * if_simloop()
176 *
177 * This function is to support software emulation of hardware loopback,
178 * i.e., for interfaces with the IFF_SIMPLEX attribute. Since they can't
179 * hear their own broadcasts, we create a copy of the packet that we
180 * would normally receive via a hardware loopback.
181 *
182 * This function expects the packet to include the media header of length hlen.
183 */
984263bc 184int
f23061d4 185if_simloop(struct ifnet *ifp, struct mbuf *m, int af, int hlen)
984263bc 186{
8bde602d 187 int isr;
984263bc
MD
188
189 KASSERT((m->m_flags & M_PKTHDR) != 0, ("if_simloop: no HDR"));
190 m->m_pkthdr.rcvif = ifp;
191
192 /* BPF write needs to be handled specially */
193 if (af == AF_UNSPEC) {
194 KASSERT(m->m_len >= sizeof(int), ("if_simloop: m_len"));
195 af = *(mtod(m, int *));
196 m->m_len -= sizeof(int);
197 m->m_pkthdr.len -= sizeof(int);
198 m->m_data += sizeof(int);
199 }
200
984263bc 201 if (ifp->if_bpf) {
fda7d388 202 bpf_gettoken();
82c6f048
SZ
203
204 /* Re-check */
205 if (ifp->if_bpf == NULL)
206 goto rel;
207
984263bc 208 if (ifp->if_bpf->bif_dlt == DLT_NULL) {
1f8e62c9
JS
209 uint32_t bpf_af = (uint32_t)af;
210 bpf_ptap(ifp->if_bpf, m, &bpf_af, 4);
82c6f048 211 } else {
1f8e62c9 212 bpf_mtap(ifp->if_bpf, m);
984263bc 213 }
82c6f048 214rel:
fda7d388 215 bpf_reltoken();
984263bc
MD
216 }
217
218 /* Strip away media header */
469ac2dc 219 if (hlen > 0)
984263bc 220 m_adj(m, hlen);
4d723e5a
JS
221
222#ifdef ALTQ
223 /*
224 * altq for loop is just for debugging.
225 * only used when called for loop interface (not for
226 * a simplex interface).
227 */
228 if (ifq_is_enabled(&ifp->if_snd) && ifp->if_start == lo_altqstart) {
229 struct altq_pktattr pktattr;
230 int32_t *afp;
4d723e5a
JS
231
232 /*
233 * if the queueing discipline needs packet classification,
234 * do it before prepending link headers.
235 */
236 ifq_classify(&ifp->if_snd, m, af, &pktattr);
237
238 M_PREPEND(m, sizeof(int32_t), MB_DONTWAIT);
4090d6ff 239 if (m == NULL)
4d723e5a
JS
240 return(ENOBUFS);
241 afp = mtod(m, int32_t *);
242 *afp = (int32_t)af;
243
3c4cd924 244 return ifq_dispatch(ifp, m, &pktattr);
4d723e5a
JS
245 }
246#endif /* ALTQ */
984263bc
MD
247
248 /* Deliver to upper layer protocol */
249 switch (af) {
250#ifdef INET
251 case AF_INET:
984263bc
MD
252 isr = NETISR_IP;
253 break;
254#endif
255#ifdef INET6
256 case AF_INET6:
257 m->m_flags |= M_LOOP;
984263bc
MD
258 isr = NETISR_IPV6;
259 break;
260#endif
261#ifdef IPX
262 case AF_IPX:
984263bc
MD
263 isr = NETISR_IPX;
264 break;
f7ea898e 265#endif
984263bc 266 default:
4b1cf444 267 kprintf("if_simloop: can't handle af=%d\n", af);
984263bc
MD
268 m_freem(m);
269 return (EAFNOSUPPORT);
270 }
8bde602d 271
984263bc
MD
272 ifp->if_ipackets++;
273 ifp->if_ibytes += m->m_pkthdr.len;
8bde602d 274 netisr_queue(isr, m);
984263bc
MD
275 return (0);
276}
277
4d723e5a
JS
278#ifdef ALTQ
279static void
f0a26983 280lo_altqstart(struct ifnet *ifp, struct ifaltq_subque *ifsq)
4d723e5a
JS
281{
282 struct mbuf *m;
283 int32_t af, *afp;
4986965b 284 int isr;
4d723e5a
JS
285
286 while (1) {
4986965b 287 crit_enter();
f0a26983 288 m = ifsq_dequeue(ifsq, NULL);
4986965b 289 crit_exit();
4d723e5a
JS
290 if (m == NULL)
291 return;
292
293 afp = mtod(m, int32_t *);
294 af = *afp;
295 m_adj(m, sizeof(int32_t));
296
297 switch (af) {
298#ifdef INET
299 case AF_INET:
300 isr = NETISR_IP;
301 break;
302#endif
303#ifdef INET6
304 case AF_INET6:
305 m->m_flags |= M_LOOP;
306 isr = NETISR_IPV6;
307 break;
308#endif
309#ifdef IPX
310 case AF_IPX:
311 isr = NETISR_IPX;
312 break;
313#endif
4d723e5a
JS
314#ifdef ISO
315 case AF_ISO:
316 isr = NETISR_ISO;
317 break;
ffd619d1 318#endif
4d723e5a 319 default:
4b1cf444 320 kprintf("lo_altqstart: can't handle af%d\n", af);
4d723e5a
JS
321 m_freem(m);
322 return;
323 }
324
325 ifp->if_ipackets++;
326 ifp->if_ibytes += m->m_pkthdr.len;
327 netisr_queue(isr, m);
328 }
329}
330#endif /* ALTQ */
331
984263bc
MD
332/* ARGSUSED */
333static void
f23061d4 334lortrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
984263bc
MD
335{
336 if (rt) {
337 rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu; /* for ISO */
338 /*
339 * For optimal performance, the send and receive buffers
340 * should be at least twice the MTU plus a little more for
341 * overhead.
342 */
f23061d4 343 rt->rt_rmx.rmx_recvpipe = rt->rt_rmx.rmx_sendpipe = 3 * LOMTU;
984263bc
MD
344 }
345}
346
347/*
348 * Process an ioctl request.
349 */
350/* ARGSUSED */
7d982e78 351static int
f23061d4 352loioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
984263bc 353{
82ed7fc2
RG
354 struct ifaddr *ifa;
355 struct ifreq *ifr = (struct ifreq *)data;
72662a67 356 int error = 0, mask;
984263bc
MD
357
358 switch (cmd) {
984263bc
MD
359 case SIOCSIFADDR:
360 ifp->if_flags |= IFF_UP | IFF_RUNNING;
361 ifa = (struct ifaddr *)data;
362 ifa->ifa_rtrequest = lortrequest;
363 /*
364 * Everything else is done at a higher level.
365 */
366 break;
367
368 case SIOCADDMULTI:
369 case SIOCDELMULTI:
4090d6ff 370 if (ifr == NULL) {
984263bc
MD
371 error = EAFNOSUPPORT; /* XXX */
372 break;
373 }
374 switch (ifr->ifr_addr.sa_family) {
375
376#ifdef INET
377 case AF_INET:
378 break;
379#endif
380#ifdef INET6
381 case AF_INET6:
382 break;
383#endif
384
385 default:
386 error = EAFNOSUPPORT;
387 break;
388 }
389 break;
390
391 case SIOCSIFMTU:
392 ifp->if_mtu = ifr->ifr_mtu;
393 break;
394
395 case SIOCSIFFLAGS:
396 break;
397
72662a67
SZ
398 case SIOCSIFCAP:
399 mask = (ifr->ifr_reqcap ^ ifp->if_capenable) & IFCAP_HWCSUM;
400 if (mask) {
401 ifp->if_capenable ^= mask;
402 if (IFCAP_TXCSUM & ifp->if_capenable)
403 ifp->if_hwassist = LO_CSUM_FEATURES;
404 else
405 ifp->if_hwassist = 0;
406 }
407 break;
408
984263bc
MD
409 default:
410 error = EINVAL;
411 }
412 return (error);
413}