]> git.saurik.com Git - apple/xnu.git/blame - bsd/net/if_loop.c
xnu-7195.81.3.tar.gz
[apple/xnu.git] / bsd / net / if_loop.c
CommitLineData
1c79356b 1/*
f427ee49 2 * Copyright (c) 2000-2020 Apple Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
316670eb 5 *
2d21ac55
A
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
316670eb 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
316670eb 17 *
2d21ac55
A
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
316670eb 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/*
29 * Copyright (c) 1982, 1986, 1993
30 * The Regents of the University of California. All rights reserved.
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
34 * are met:
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 *
60 * @(#)if_loop.c 8.1 (Berkeley) 6/10/93
9bccf70c 61 * $FreeBSD: src/sys/net/if_loop.c,v 1.47.2.5 2001/07/03 11:01:41 ume Exp $
1c79356b 62 */
2d21ac55
A
63/*
64 * NOTICE: This file was modified by SPARTA, Inc. in 2006 to introduce
65 * support for mandatory and extensible security protections. This notice
66 * is included in support of clause 2.2 (b) of the Apple Public License,
67 * Version 2.0.
68 */
1c79356b
A
69
70/*
71 * Loopback interface driver for protocol testing and timing.
72 */
73#include "loop.h"
74#if NLOOP > 0
75
316670eb
A
76#if NLOOP != 1
77#error "More than one loopback interface is not supported."
78#endif
79
1c79356b
A
80#include <sys/param.h>
81#include <sys/systm.h>
82#include <sys/kernel.h>
83#include <sys/mbuf.h>
84#include <sys/socket.h>
85#include <sys/sockio.h>
6d2010ae 86#include <sys/mcache.h>
316670eb 87#include <sys/sysctl.h>
1c79356b
A
88
89#include <net/if.h>
90#include <net/if_types.h>
1c79356b
A
91#include <net/route.h>
92#include <net/bpf.h>
93#include <sys/malloc.h>
94
316670eb 95#if INET
1c79356b
A
96#include <netinet/in.h>
97#include <netinet/in_var.h>
98#endif
99
2d21ac55 100#if !INET
1c79356b
A
101#include <netinet/in.h>
102#endif
103#include <netinet6/in6_var.h>
104#include <netinet/ip6.h>
1c79356b 105
1c79356b 106#include <net/dlil.h>
91447636 107#include <net/kpi_protocol.h>
1c79356b 108
316670eb
A
109#include <pexpert/pexpert.h>
110
0a7de745
A
111#define LOMTU 16384
112#define LOSNDQ_MAXLEN 256
113
114#define LO_BPF_TAP_OUT(_m) { \
115 if (lo_statics[0].bpf_callback != NULL) { \
116 bpf_tap_out(lo_ifp, DLT_NULL, _m, \
117 &((struct loopback_header *)_m->m_pkthdr.pkt_hdr)-> \
118 protocol, sizeof (u_int32_t)); \
119 } \
316670eb
A
120}
121
0a7de745
A
122#define LO_BPF_TAP_OUT_MULTI(_m) { \
123 if (lo_statics[0].bpf_callback != NULL) { \
124 struct mbuf *_n; \
125 for (_n = _m; _n != NULL; _n = _n->m_nextpkt) \
126 LO_BPF_TAP_OUT(_n); \
127 } \
316670eb 128}
1c79356b
A
129
130struct lo_statics_str {
0a7de745
A
131 int bpf_mode;
132 bpf_packet_func bpf_callback;
1c79356b
A
133};
134
91447636 135static struct lo_statics_str lo_statics[NLOOP];
316670eb 136static int lo_txstart = 0;
1c79356b 137
316670eb 138struct ifnet *lo_ifp = NULL;
1c79356b 139
0a7de745
A
140struct loopback_header {
141 protocol_family_t protocol;
91447636 142};
1c79356b 143
316670eb
A
144/* Local forward declerations */
145void loopattach(void);
146static errno_t lo_demux(struct ifnet *, struct mbuf *, char *,
147 protocol_family_t *);
316670eb
A
148static errno_t
149lo_framer(struct ifnet *, struct mbuf **, const struct sockaddr *,
150 const char *, const char *, u_int32_t *, u_int32_t *);
316670eb
A
151static errno_t lo_add_proto(struct ifnet *, protocol_family_t,
152 const struct ifnet_demux_desc *, u_int32_t);
153static errno_t lo_del_proto(struct ifnet *, protocol_family_t);
154static int lo_output(struct ifnet *, struct mbuf *);
155static errno_t lo_pre_enqueue(struct ifnet *, struct mbuf *);
156static void lo_start(struct ifnet *);
157static errno_t lo_pre_output(struct ifnet *, protocol_family_t, struct mbuf **,
158 const struct sockaddr *, void *, char *, char *);
159static errno_t lo_input(struct ifnet *, protocol_family_t, struct mbuf *);
160static void lo_rtrequest(int, struct rtentry *, struct sockaddr *);
161static errno_t lo_ioctl(struct ifnet *, u_long, void *);
162static errno_t lo_attach_proto(struct ifnet *, protocol_family_t);
2d21ac55 163static void lo_reg_if_mods(void);
316670eb
A
164static errno_t lo_set_bpf_tap(struct ifnet *, bpf_tap_mode, bpf_packet_func);
165static int sysctl_dequeue_max SYSCTL_HANDLER_ARGS;
166static int sysctl_sched_model SYSCTL_HANDLER_ARGS;
167static int sysctl_dequeue_scidx SYSCTL_HANDLER_ARGS;
1c79356b 168
316670eb
A
169SYSCTL_DECL(_net_link);
170
171SYSCTL_NODE(_net_link, OID_AUTO, loopback, CTLFLAG_RW | CTLFLAG_LOCKED, 0,
172 "loopback interface");
173
316670eb
A
174static u_int32_t lo_dequeue_max = LOSNDQ_MAXLEN;
175SYSCTL_PROC(_net_link_loopback, OID_AUTO, max_dequeue,
176 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED, &lo_dequeue_max, LOSNDQ_MAXLEN,
177 sysctl_dequeue_max, "I", "Maximum number of packets dequeued at a time");
178
179static u_int32_t lo_sched_model = IFNET_SCHED_MODEL_NORMAL;
180SYSCTL_PROC(_net_link_loopback, OID_AUTO, sched_model,
181 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED, &lo_sched_model,
182 IFNET_SCHED_MODEL_NORMAL, sysctl_sched_model, "I", "Scheduling model");
183
184static u_int32_t lo_dequeue_sc = MBUF_SC_BE;
185static int lo_dequeue_scidx = MBUF_SCIDX(MBUF_SC_BE);
186SYSCTL_PROC(_net_link_loopback, OID_AUTO, dequeue_sc,
187 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED, &lo_dequeue_scidx,
188 MBUF_SC_BE, sysctl_dequeue_scidx, "I", "Dequeue a specific SC index");
1c79356b 189
91447636 190static errno_t
316670eb
A
191lo_demux(struct ifnet *ifp, struct mbuf *m, char *frame_header,
192 protocol_family_t *protocol_family)
1c79356b 193{
316670eb
A
194#pragma unused(ifp, m)
195 struct loopback_header *header =
196 (struct loopback_header *)(void *)frame_header;
197
91447636 198 *protocol_family = header->protocol;
1c79356b 199
0a7de745 200 return 0;
316670eb 201}
1c79356b 202
316670eb
A
203static errno_t
204lo_framer(struct ifnet *ifp, struct mbuf **m, const struct sockaddr *dest,
205 const char *dest_linkaddr, const char *frame_type,
206 u_int32_t *prepend_len, u_int32_t *postpend_len)
1c79356b 207{
316670eb 208#pragma unused(ifp, dest, dest_linkaddr)
91447636 209 struct loopback_header *header;
1c79356b 210
0a7de745 211 M_PREPEND(*m, sizeof(struct loopback_header), M_WAITOK, 1);
316670eb
A
212 if (*m == NULL) {
213 /* Tell caller not to try to free passed-in mbuf */
0a7de745 214 return EJUSTRETURN;
316670eb
A
215 }
216
0a7de745
A
217 if (prepend_len != NULL) {
218 *prepend_len = sizeof(struct loopback_header);
219 }
220 if (postpend_len != NULL) {
39236c6e 221 *postpend_len = 0;
0a7de745 222 }
316670eb
A
223
224 header = mtod(*m, struct loopback_header *);
0a7de745
A
225 bcopy(frame_type, &header->protocol, sizeof(u_int32_t));
226 return 0;
1c79356b
A
227}
228
91447636 229static errno_t
316670eb
A
230lo_add_proto(struct ifnet *interface, protocol_family_t protocol_family,
231 const struct ifnet_demux_desc *demux_array, u_int32_t demux_count)
1c79356b 232{
316670eb 233#pragma unused(interface, protocol_family, demux_array, demux_count)
0a7de745 234 return 0;
1c79356b
A
235}
236
91447636 237static errno_t
316670eb 238lo_del_proto(struct ifnet *ifp, protocol_family_t protocol)
1c79356b 239{
316670eb 240#pragma unused(ifp, protocol)
0a7de745 241 return 0;
1c79356b
A
242}
243
39037602
A
244static void
245lo_tx_compl(struct ifnet *ifp, struct mbuf *m)
246{
247 errno_t error;
248
249 if ((ifp->if_xflags & IFXF_TIMESTAMP_ENABLED) != 0) {
250 boolean_t requested;
251
252 error = mbuf_get_timestamp_requested(m, &requested);
253 if (requested) {
254 struct timespec now;
255 u_int64_t ts;
256
257 nanouptime(&now);
258 net_timernsec(&now, &ts);
259
260 error = mbuf_set_timestamp(m, ts, TRUE);
0a7de745 261 if (error != 0) {
39037602 262 printf("%s: mbuf_set_timestamp() failed %d\n",
0a7de745
A
263 __func__, error);
264 }
39037602
A
265 }
266 }
267 error = mbuf_set_status(m, KERN_SUCCESS);
0a7de745 268 if (error != 0) {
39037602 269 printf("%s: mbuf_set_status() failed %d\n",
0a7de745
A
270 __func__, error);
271 }
39037602
A
272
273 ifnet_tx_compl(ifp, m);
274}
275
316670eb
A
276/*
277 * Output callback.
278 *
279 * This routine is called only when lo_txstart is disabled.
280 */
1c79356b 281static int
316670eb 282lo_output(struct ifnet *ifp, struct mbuf *m_list)
91447636 283{
316670eb
A
284 struct mbuf *m, *m_tail = NULL;
285 struct ifnet_stat_increment_param s;
286 u_int32_t cnt = 0, len = 0;
287
288 bzero(&s, sizeof(s));
289
2d21ac55 290 for (m = m_list; m; m = m->m_nextpkt) {
39236c6e 291 VERIFY(m->m_flags & M_PKTHDR);
316670eb 292 cnt++;
1c79356b 293
2d21ac55
A
294 /*
295 * Don't overwrite the rcvif field if it is in use.
296 * This is used to match multicast packets, sent looping
297 * back, with the appropriate group record on input.
298 */
0a7de745 299 if (m->m_pkthdr.rcvif == NULL) {
2d21ac55 300 m->m_pkthdr.rcvif = ifp;
0a7de745 301 }
91447636 302
39236c6e
A
303 m->m_pkthdr.pkt_flags |= PKTF_LOOP;
304 m->m_pkthdr.pkt_hdr = mtod(m, char *);
305
306 /* loopback checksums are always OK */
307 m->m_pkthdr.csum_data = 0xffff;
308 m->m_pkthdr.csum_flags =
309 CSUM_DATA_VALID | CSUM_PSEUDO_HDR |
310 CSUM_IP_CHECKED | CSUM_IP_VALID;
311
0a7de745 312 m_adj(m, sizeof(struct loopback_header));
f427ee49 313 len += m->m_pkthdr.len;
316670eb
A
314
315 LO_BPF_TAP_OUT(m);
316 if (m->m_nextpkt == NULL) {
317 m_tail = m;
318 }
39037602 319 lo_tx_compl(ifp, m);
316670eb
A
320 }
321
322 s.packets_in = cnt;
323 s.packets_out = cnt;
324 s.bytes_in = len;
39236c6e 325 s.bytes_out = len;
316670eb 326
0a7de745 327 return ifnet_input_extended(ifp, m_list, m_tail, &s);
316670eb
A
328}
329
330/*
331 * Pre-enqueue callback.
332 *
333 * This routine is called only when lo_txstart is enabled.
334 */
335static errno_t
336lo_pre_enqueue(struct ifnet *ifp, struct mbuf *m0)
337{
338 struct mbuf *m = m0, *n;
339 int error = 0;
340
341 while (m != NULL) {
39236c6e 342 VERIFY(m->m_flags & M_PKTHDR);
91447636 343
316670eb
A
344 n = m->m_nextpkt;
345 m->m_nextpkt = NULL;
346
347 /*
348 * Don't overwrite the rcvif field if it is in use.
349 * This is used to match multicast packets, sent looping
350 * back, with the appropriate group record on input.
351 */
0a7de745 352 if (m->m_pkthdr.rcvif == NULL) {
316670eb 353 m->m_pkthdr.rcvif = ifp;
0a7de745 354 }
1c79356b 355
39236c6e
A
356 m->m_pkthdr.pkt_flags |= PKTF_LOOP;
357 m->m_pkthdr.pkt_hdr = mtod(m, char *);
358
359 /* loopback checksums are always OK */
360 m->m_pkthdr.csum_data = 0xffff;
361 m->m_pkthdr.csum_flags =
362 CSUM_DATA_VALID | CSUM_PSEUDO_HDR |
363 CSUM_IP_CHECKED | CSUM_IP_VALID;
364
0a7de745 365 m_adj(m, sizeof(struct loopback_header));
316670eb
A
366
367 /*
368 * Let the callee free it in case of error,
369 * and perform any necessary accounting.
370 */
371 (void) ifnet_enqueue(ifp, m);
372
373 m = n;
1c79356b 374 }
1c79356b 375
0a7de745 376 return error;
1c79356b
A
377}
378
316670eb
A
379/*
380 * Start output callback.
381 *
382 * This routine is invoked by the start worker thread; because we never call
383 * it directly, there is no need do deploy any serialization mechanism other
384 * than what's already used by the worker thread, i.e. this is already single
385 * threaded.
386 *
387 * This routine is called only when lo_txstart is enabled.
388 */
389static void
390lo_start(struct ifnet *ifp)
391{
392 struct ifnet_stat_increment_param s;
393
0a7de745 394 bzero(&s, sizeof(s));
316670eb
A
395
396 for (;;) {
397 struct mbuf *m = NULL, *m_tail = NULL;
398 u_int32_t cnt, len = 0;
316670eb
A
399
400 if (lo_sched_model == IFNET_SCHED_MODEL_NORMAL) {
401 if (ifnet_dequeue_multi(ifp, lo_dequeue_max, &m,
0a7de745 402 &m_tail, &cnt, &len) != 0) {
316670eb 403 break;
0a7de745 404 }
316670eb
A
405 } else {
406 if (ifnet_dequeue_service_class_multi(ifp,
407 lo_dequeue_sc, lo_dequeue_max, &m,
0a7de745 408 &m_tail, &cnt, &len) != 0) {
316670eb 409 break;
0a7de745 410 }
316670eb
A
411 }
412
413 LO_BPF_TAP_OUT_MULTI(m);
39037602 414 lo_tx_compl(ifp, m);
316670eb
A
415
416 /* stats are required for extended variant */
417 s.packets_in = cnt;
418 s.packets_out = cnt;
419 s.bytes_in = len;
420 s.bytes_out = len;
421
422 (void) ifnet_input_extended(ifp, m, m_tail, &s);
423 }
424}
1c79356b
A
425
426/*
91447636 427 * This is a common pre-output route used by INET and INET6. This could
1c79356b
A
428 * (should?) be split into separate pre-output routines for each protocol.
429 */
2d21ac55 430static errno_t
316670eb
A
431lo_pre_output(struct ifnet *ifp, protocol_family_t protocol_family,
432 struct mbuf **m, const struct sockaddr *dst, void *route, char *frame_type,
433 char *dst_addr)
1c79356b 434{
316670eb
A
435#pragma unused(ifp, dst, dst_addr)
436 struct rtentry *rt = route;
2d21ac55 437
39236c6e 438 VERIFY((*m)->m_flags & M_PKTHDR);
1c79356b 439
39236c6e 440 (*m)->m_flags |= M_LOOP;
1c79356b 441
b0d623f7
A
442 if (rt != NULL) {
443 u_int32_t rt_flags = rt->rt_flags;
444 if (rt_flags & (RTF_REJECT | RTF_BLACKHOLE)) {
445 if (rt_flags & RTF_BLACKHOLE) {
446 m_freem(*m);
0a7de745 447 return EJUSTRETURN;
b0d623f7 448 } else {
0a7de745
A
449 return (rt_flags & RTF_HOST) ?
450 EHOSTUNREACH : ENETUNREACH;
b0d623f7 451 }
91447636 452 }
1c79356b 453 }
b0d623f7 454
0a7de745 455 bcopy(&protocol_family, frame_type, sizeof(protocol_family));
1c79356b 456
0a7de745 457 return 0;
1c79356b
A
458}
459
1c79356b
A
460/*
461 * lo_input - This should work for all attached protocols that use the
462 * ifq/schednetisr input mechanism.
463 */
2d21ac55 464static errno_t
316670eb 465lo_input(struct ifnet *ifp, protocol_family_t protocol_family, struct mbuf *m)
1c79356b 466{
316670eb 467#pragma unused(ifp, protocol_family)
39037602
A
468
469 if ((ifp->if_xflags & IFXF_TIMESTAMP_ENABLED) != 0) {
470 errno_t error;
471 struct timespec now;
472 u_int64_t ts;
473
474 nanouptime(&now);
475 net_timernsec(&now, &ts);
476
477 error = mbuf_set_timestamp(m, ts, TRUE);
0a7de745 478 if (error != 0) {
39037602 479 printf("%s: mbuf_set_timestamp() failed %d\n",
0a7de745
A
480 __func__, error);
481 }
39037602
A
482 }
483
0a7de745 484 if (proto_input(protocol_family, m) != 0) {
1c79356b 485 m_freem(m);
0a7de745
A
486 }
487 return 0;
1c79356b
A
488}
489
1c79356b
A
490/* ARGSUSED */
491static void
316670eb 492lo_rtrequest(int cmd, struct rtentry *rt, struct sockaddr *sa)
1c79356b 493{
316670eb 494#pragma unused(cmd, sa)
b0d623f7
A
495 if (rt != NULL) {
496 RT_LOCK_ASSERT_HELD(rt);
1c79356b
A
497 rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu; /* for ISO */
498 /*
499 * For optimal performance, the send and receive buffers
500 * should be at least twice the MTU plus a little more for
501 * overhead.
502 */
316670eb 503 rt->rt_rmx.rmx_recvpipe = rt->rt_rmx.rmx_sendpipe = 3 * LOMTU;
1c79356b
A
504 }
505}
506
507/*
508 * Process an ioctl request.
509 */
91447636 510static errno_t
316670eb 511lo_ioctl(struct ifnet *ifp, u_long cmd, void *data)
1c79356b 512{
316670eb 513 int error = 0;
1c79356b
A
514
515 switch (cmd) {
0a7de745 516 case SIOCSIFADDR: { /* struct ifaddr pointer */
316670eb
A
517 struct ifaddr *ifa = data;
518
0a7de745 519 ifnet_set_flags(ifp, IFF_UP | IFF_RUNNING, IFF_UP | IFF_RUNNING);
6d2010ae 520 IFA_LOCK_SPIN(ifa);
316670eb 521 ifa->ifa_rtrequest = lo_rtrequest;
6d2010ae 522 IFA_UNLOCK(ifa);
1c79356b
A
523 /*
524 * Everything else is done at a higher level.
525 */
526 break;
316670eb 527 }
1c79356b 528
0a7de745
A
529 case SIOCADDMULTI: /* struct ifreq */
530 case SIOCDELMULTI: { /* struct ifreq */
316670eb
A
531 struct ifreq *ifr = data;
532
533 if (ifr == NULL) {
0a7de745 534 error = EAFNOSUPPORT; /* XXX */
1c79356b
A
535 break;
536 }
537 switch (ifr->ifr_addr.sa_family) {
1c79356b
A
538#if INET
539 case AF_INET:
540 break;
541#endif
1c79356b
A
542 case AF_INET6:
543 break;
1c79356b
A
544
545 default:
546 error = EAFNOSUPPORT;
547 break;
548 }
549 break;
316670eb
A
550 }
551
0a7de745 552 case SIOCSIFMTU: { /* struct ifreq */
316670eb 553 struct ifreq *ifr = data;
1c79356b 554
0a7de745 555 bcopy(&ifr->ifr_mtu, &ifp->if_mtu, sizeof(int));
1c79356b 556 break;
316670eb 557 }
1c79356b 558
0a7de745 559 case SIOCSIFFLAGS: /* struct ifreq */
39037602
A
560 case SIOCSIFTIMESTAMPENABLE:
561 case SIOCSIFTIMESTAMPDISABLE:
1c79356b
A
562 break;
563
564 default:
565 error = EOPNOTSUPP;
4a249263 566 break;
1c79356b 567 }
0a7de745 568 return error;
1c79356b
A
569}
570#endif /* NLOOP > 0 */
571
572
316670eb
A
573static errno_t
574lo_attach_proto(struct ifnet *ifp, protocol_family_t protocol_family)
1c79356b 575{
0a7de745
A
576 struct ifnet_attach_proto_param_v2 proto;
577 errno_t result = 0;
316670eb 578
0a7de745 579 bzero(&proto, sizeof(proto));
2d21ac55
A
580 proto.input = lo_input;
581 proto.pre_output = lo_pre_output;
316670eb 582
2d21ac55 583 result = ifnet_attach_protocol_v2(ifp, protocol_family, &proto);
1c79356b 584
2d21ac55 585 if (result && result != EEXIST) {
316670eb
A
586 printf("lo_attach_proto: ifnet_attach_protocol for %u "
587 "returned=%d\n", protocol_family, result);
9bccf70c 588 }
316670eb 589
0a7de745 590 return result;
9bccf70c
A
591}
592
316670eb
A
593static void
594lo_reg_if_mods(void)
55e303ae 595{
316670eb 596 int error;
55e303ae 597
55e303ae 598 /* Register protocol registration functions */
316670eb 599 if ((error = proto_register_plumber(PF_INET,
0a7de745 600 APPLE_IF_FAM_LOOPBACK, lo_attach_proto, NULL)) != 0) {
316670eb
A
601 printf("proto_register_plumber failed for AF_INET "
602 "error=%d\n", error);
0a7de745 603 }
316670eb
A
604
605 if ((error = proto_register_plumber(PF_INET6,
0a7de745 606 APPLE_IF_FAM_LOOPBACK, lo_attach_proto, NULL)) != 0) {
316670eb
A
607 printf("proto_register_plumber failed for AF_INET6 "
608 "error=%d\n", error);
0a7de745 609 }
55e303ae 610}
1c79356b 611
91447636 612static errno_t
316670eb
A
613lo_set_bpf_tap(struct ifnet *ifp, bpf_tap_mode mode,
614 bpf_packet_func bpf_callback)
1c79356b 615{
316670eb 616 VERIFY(ifp == lo_ifp);
1c79356b 617
316670eb
A
618 lo_statics[0].bpf_mode = mode;
619
620 switch (mode) {
0a7de745
A
621 case BPF_TAP_DISABLE:
622 case BPF_TAP_INPUT:
623 lo_statics[0].bpf_callback = NULL;
624 break;
316670eb 625
0a7de745
A
626 case BPF_TAP_OUTPUT:
627 case BPF_TAP_INPUT_OUTPUT:
628 lo_statics[0].bpf_callback = bpf_callback;
629 break;
1c79356b
A
630 }
631
0a7de745 632 return 0;
1c79356b
A
633}
634
1c79356b
A
635/* ARGSUSED */
636void
2d21ac55 637loopattach(void)
1c79356b 638{
316670eb 639 struct ifnet_init_eparams lo_init;
0a7de745 640 errno_t result = 0;
1c79356b 641
0a7de745 642 PE_parse_boot_argn("lo_txstart", &lo_txstart, sizeof(lo_txstart));
1c79356b 643
2d21ac55 644 lo_reg_if_mods();
316670eb
A
645
646 lo_statics[0].bpf_callback = NULL;
647 lo_statics[0].bpf_mode = BPF_TAP_DISABLE;
648
0a7de745
A
649 bzero(&lo_init, sizeof(lo_init));
650 lo_init.ver = IFNET_INIT_CURRENT_VERSION;
651 lo_init.len = sizeof(lo_init);
652 lo_init.sndq_maxlen = LOSNDQ_MAXLEN;
316670eb 653 if (lo_txstart) {
0a7de745
A
654 lo_init.flags = 0;
655 lo_init.pre_enqueue = lo_pre_enqueue;
656 lo_init.start = lo_start;
316670eb
A
657 lo_init.output_sched_model = lo_sched_model;
658 } else {
0a7de745
A
659 lo_init.flags = IFNET_INIT_LEGACY;
660 lo_init.output = lo_output;
316670eb 661 }
0a7de745
A
662 lo_init.flags |= IFNET_INIT_NX_NOAUTO;
663 lo_init.name = "lo";
664 lo_init.unit = 0;
665 lo_init.family = IFNET_FAMILY_LOOPBACK;
666 lo_init.type = IFT_LOOP;
667 lo_init.demux = lo_demux;
668 lo_init.add_proto = lo_add_proto;
669 lo_init.del_proto = lo_del_proto;
670 lo_init.framer_extended = lo_framer;
671 lo_init.softc = &lo_statics[0];
672 lo_init.ioctl = lo_ioctl;
673 lo_init.set_bpf_tap = lo_set_bpf_tap;
316670eb
A
674
675 result = ifnet_allocate_extended(&lo_init, &lo_ifp);
2d21ac55 676 if (result != 0) {
316670eb
A
677 panic("%s: couldn't allocate loopback ifnet (%d)\n",
678 __func__, result);
679 /* NOTREACHED */
2d21ac55 680 }
316670eb 681
2d21ac55 682 ifnet_set_mtu(lo_ifp, LOMTU);
316670eb
A
683 ifnet_set_flags(lo_ifp, IFF_LOOPBACK | IFF_MULTICAST,
684 IFF_LOOPBACK | IFF_MULTICAST);
685 ifnet_set_offload(lo_ifp,
686 IFNET_CSUM_IP | IFNET_CSUM_TCP | IFNET_CSUM_UDP |
687 IFNET_CSUM_TCPIPV6 | IFNET_CSUM_UDPIPV6 | IFNET_IPV6_FRAGMENT |
39037602
A
688 IFNET_CSUM_FRAGMENT | IFNET_IP_FRAGMENT | IFNET_MULTIPAGES |
689 IFNET_TX_STATUS | IFNET_SW_TIMESTAMP);
0a7de745 690 ifnet_set_hdrlen(lo_ifp, sizeof(struct loopback_header));
2d21ac55
A
691 ifnet_set_eflags(lo_ifp, IFEF_SENDLIST, IFEF_SENDLIST);
692
2d21ac55
A
693 result = ifnet_attach(lo_ifp, NULL);
694 if (result != 0) {
316670eb
A
695 panic("%s: couldn't attach loopback ifnet (%d)\n",
696 __func__, result);
697 /* NOTREACHED */
698 }
5c9f4661
A
699 /*
700 * Disable ECN on loopback as ECN serves no purpose and otherwise
701 * TCP connections are subject to heuristics like SYN retransmits on RST
702 */
f427ee49
A
703 if_clear_eflags(lo_ifp, IFEF_ECN_ENABLE);
704 if_set_eflags(lo_ifp, IFEF_ECN_DISABLE);
5c9f4661 705
0a7de745 706 bpfattach(lo_ifp, DLT_NULL, sizeof(u_int32_t));
316670eb
A
707}
708
709static int
710sysctl_dequeue_max SYSCTL_HANDLER_ARGS
711{
712#pragma unused(arg1, arg2)
713 u_int32_t i;
714 int err;
715
716 i = lo_dequeue_max;
717
718 err = sysctl_handle_int(oidp, &i, 0, req);
0a7de745
A
719 if (err != 0 || req->newptr == USER_ADDR_NULL) {
720 return err;
721 }
316670eb 722
0a7de745 723 if (i < 1) {
316670eb 724 i = 1;
0a7de745 725 } else if (i > LOSNDQ_MAXLEN) {
316670eb 726 i = LOSNDQ_MAXLEN;
0a7de745 727 }
316670eb
A
728
729 lo_dequeue_max = i;
730
0a7de745 731 return err;
316670eb
A
732}
733
734static int
735sysctl_sched_model SYSCTL_HANDLER_ARGS
736{
737#pragma unused(arg1, arg2)
738 u_int32_t i;
739 int err;
740
741 i = lo_sched_model;
742
743 err = sysctl_handle_int(oidp, &i, 0, req);
0a7de745
A
744 if (err != 0 || req->newptr == USER_ADDR_NULL) {
745 return err;
746 }
316670eb
A
747
748 switch (i) {
749 case IFNET_SCHED_MODEL_NORMAL:
750 case IFNET_SCHED_MODEL_DRIVER_MANAGED:
39037602 751 case IFNET_SCHED_MODEL_FQ_CODEL:
316670eb
A
752 break;
753
754 default:
755 err = EINVAL;
756 break;
1c79356b 757 }
316670eb 758
0a7de745 759 if (err == 0 && (err = ifnet_set_output_sched_model(lo_ifp, i)) == 0) {
316670eb 760 lo_sched_model = i;
0a7de745 761 }
316670eb 762
0a7de745 763 return err;
316670eb
A
764}
765
766static int
767sysctl_dequeue_scidx SYSCTL_HANDLER_ARGS
768{
769#pragma unused(arg1, arg2)
770 u_int32_t i;
771 int err;
772
773 i = lo_dequeue_scidx;
774
775 err = sysctl_handle_int(oidp, &i, 0, req);
0a7de745
A
776 if (err != 0 || req->newptr == USER_ADDR_NULL) {
777 return err;
778 }
316670eb 779
0a7de745
A
780 if (!MBUF_VALID_SCIDX(i)) {
781 return EINVAL;
782 }
316670eb 783
0a7de745
A
784 if (lo_sched_model != IFNET_SCHED_MODEL_DRIVER_MANAGED) {
785 return ENODEV;
786 }
316670eb
A
787
788 lo_dequeue_sc = m_service_class_from_idx(i);
789 lo_dequeue_scidx = MBUF_SCIDX(lo_dequeue_sc);
790
0a7de745 791 return err;
1c79356b 792}