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