]> git.saurik.com Git - apple/xnu.git/blame - bsd/net/if_loop.c
xnu-3789.41.3.tar.gz
[apple/xnu.git] / bsd / net / if_loop.c
CommitLineData
1c79356b 1/*
39236c6e 2 * Copyright (c) 2000-2013 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
1c79356b 100#if INET6
2d21ac55 101#if !INET
1c79356b
A
102#include <netinet/in.h>
103#endif
104#include <netinet6/in6_var.h>
105#include <netinet/ip6.h>
106#endif
107
1c79356b 108#include <net/dlil.h>
91447636 109#include <net/kpi_protocol.h>
1c79356b 110
2d21ac55
A
111#if CONFIG_MACF_NET
112#include <security/mac_framework.h>
113#endif
114
316670eb
A
115#include <pexpert/pexpert.h>
116
117#define LOMTU 16384
118#define LOSNDQ_MAXLEN 256
119
120#define LO_BPF_TAP_OUT(_m) { \
121 if (lo_statics[0].bpf_callback != NULL) { \
122 bpf_tap_out(lo_ifp, DLT_NULL, _m, \
39236c6e
A
123 &((struct loopback_header *)_m->m_pkthdr.pkt_hdr)-> \
124 protocol, sizeof (u_int32_t)); \
316670eb
A
125 } \
126}
127
128#define LO_BPF_TAP_OUT_MULTI(_m) { \
129 if (lo_statics[0].bpf_callback != NULL) { \
130 struct mbuf *_n; \
131 for (_n = _m; _n != NULL; _n = _n->m_nextpkt) \
132 LO_BPF_TAP_OUT(_n); \
133 } \
134}
1c79356b
A
135
136struct lo_statics_str {
316670eb 137 int bpf_mode;
91447636 138 bpf_packet_func bpf_callback;
1c79356b
A
139};
140
91447636 141static struct lo_statics_str lo_statics[NLOOP];
316670eb 142static int lo_txstart = 0;
1c79356b 143
316670eb 144struct ifnet *lo_ifp = NULL;
1c79356b 145
91447636 146struct loopback_header {
2d21ac55 147 protocol_family_t protocol;
91447636 148};
1c79356b 149
316670eb
A
150/* Local forward declerations */
151void loopattach(void);
152static errno_t lo_demux(struct ifnet *, struct mbuf *, char *,
153 protocol_family_t *);
316670eb
A
154static errno_t
155lo_framer(struct ifnet *, struct mbuf **, const struct sockaddr *,
156 const char *, const char *, u_int32_t *, u_int32_t *);
316670eb
A
157static errno_t lo_add_proto(struct ifnet *, protocol_family_t,
158 const struct ifnet_demux_desc *, u_int32_t);
159static errno_t lo_del_proto(struct ifnet *, protocol_family_t);
160static int lo_output(struct ifnet *, struct mbuf *);
161static errno_t lo_pre_enqueue(struct ifnet *, struct mbuf *);
162static void lo_start(struct ifnet *);
163static errno_t lo_pre_output(struct ifnet *, protocol_family_t, struct mbuf **,
164 const struct sockaddr *, void *, char *, char *);
165static errno_t lo_input(struct ifnet *, protocol_family_t, struct mbuf *);
166static void lo_rtrequest(int, struct rtentry *, struct sockaddr *);
167static errno_t lo_ioctl(struct ifnet *, u_long, void *);
168static errno_t lo_attach_proto(struct ifnet *, protocol_family_t);
2d21ac55 169static void lo_reg_if_mods(void);
316670eb
A
170static errno_t lo_set_bpf_tap(struct ifnet *, bpf_tap_mode, bpf_packet_func);
171static int sysctl_dequeue_max SYSCTL_HANDLER_ARGS;
172static int sysctl_sched_model SYSCTL_HANDLER_ARGS;
173static int sysctl_dequeue_scidx SYSCTL_HANDLER_ARGS;
1c79356b 174
316670eb
A
175SYSCTL_DECL(_net_link);
176
177SYSCTL_NODE(_net_link, OID_AUTO, loopback, CTLFLAG_RW | CTLFLAG_LOCKED, 0,
178 "loopback interface");
179
180#define LO_BW_SLEEP 10
181static u_int32_t lo_bw_sleep_usec = LO_BW_SLEEP;
182SYSCTL_UINT(_net_link_loopback, OID_AUTO, bw_sleep_usec,
183 CTLFLAG_RW | CTLFLAG_LOCKED, &lo_bw_sleep_usec, LO_BW_SLEEP, "");
184
185static u_int32_t lo_bw_measure = 0;
186SYSCTL_UINT(_net_link_loopback, OID_AUTO, bw_measure,
187 CTLFLAG_RW | CTLFLAG_LOCKED, &lo_bw_measure, 0, "");
188
189static u_int32_t lo_dequeue_max = LOSNDQ_MAXLEN;
190SYSCTL_PROC(_net_link_loopback, OID_AUTO, max_dequeue,
191 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED, &lo_dequeue_max, LOSNDQ_MAXLEN,
192 sysctl_dequeue_max, "I", "Maximum number of packets dequeued at a time");
193
194static u_int32_t lo_sched_model = IFNET_SCHED_MODEL_NORMAL;
195SYSCTL_PROC(_net_link_loopback, OID_AUTO, sched_model,
196 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED, &lo_sched_model,
197 IFNET_SCHED_MODEL_NORMAL, sysctl_sched_model, "I", "Scheduling model");
198
199static u_int32_t lo_dequeue_sc = MBUF_SC_BE;
200static int lo_dequeue_scidx = MBUF_SCIDX(MBUF_SC_BE);
201SYSCTL_PROC(_net_link_loopback, OID_AUTO, dequeue_sc,
202 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED, &lo_dequeue_scidx,
203 MBUF_SC_BE, sysctl_dequeue_scidx, "I", "Dequeue a specific SC index");
1c79356b 204
91447636 205static errno_t
316670eb
A
206lo_demux(struct ifnet *ifp, struct mbuf *m, char *frame_header,
207 protocol_family_t *protocol_family)
1c79356b 208{
316670eb
A
209#pragma unused(ifp, m)
210 struct loopback_header *header =
211 (struct loopback_header *)(void *)frame_header;
212
91447636 213 *protocol_family = header->protocol;
1c79356b 214
316670eb
A
215 return (0);
216}
1c79356b 217
316670eb
A
218static errno_t
219lo_framer(struct ifnet *ifp, struct mbuf **m, const struct sockaddr *dest,
220 const char *dest_linkaddr, const char *frame_type,
221 u_int32_t *prepend_len, u_int32_t *postpend_len)
1c79356b 222{
316670eb 223#pragma unused(ifp, dest, dest_linkaddr)
91447636 224 struct loopback_header *header;
1c79356b 225
3e170ce0 226 M_PREPEND(*m, sizeof (struct loopback_header), M_WAITOK, 1);
316670eb
A
227 if (*m == NULL) {
228 /* Tell caller not to try to free passed-in mbuf */
229 return (EJUSTRETURN);
230 }
231
39236c6e
A
232 if (prepend_len != NULL)
233 *prepend_len = sizeof (struct loopback_header);
234 if (postpend_len != NULL)
235 *postpend_len = 0;
316670eb
A
236
237 header = mtod(*m, struct loopback_header *);
238 bcopy(frame_type, &header->protocol, sizeof (u_int32_t));
239 return (0);
1c79356b
A
240}
241
91447636 242static errno_t
316670eb
A
243lo_add_proto(struct ifnet *interface, protocol_family_t protocol_family,
244 const struct ifnet_demux_desc *demux_array, u_int32_t demux_count)
1c79356b 245{
316670eb
A
246#pragma unused(interface, protocol_family, demux_array, demux_count)
247 return (0);
1c79356b
A
248}
249
91447636 250static errno_t
316670eb 251lo_del_proto(struct ifnet *ifp, protocol_family_t protocol)
1c79356b 252{
316670eb
A
253#pragma unused(ifp, protocol)
254 return (0);
1c79356b
A
255}
256
39037602
A
257static void
258lo_tx_compl(struct ifnet *ifp, struct mbuf *m)
259{
260 errno_t error;
261
262 if ((ifp->if_xflags & IFXF_TIMESTAMP_ENABLED) != 0) {
263 boolean_t requested;
264
265 error = mbuf_get_timestamp_requested(m, &requested);
266 if (requested) {
267 struct timespec now;
268 u_int64_t ts;
269
270 nanouptime(&now);
271 net_timernsec(&now, &ts);
272
273 error = mbuf_set_timestamp(m, ts, TRUE);
274 if (error != 0)
275 printf("%s: mbuf_set_timestamp() failed %d\n",
276 __func__, error);
277 }
278 }
279 error = mbuf_set_status(m, KERN_SUCCESS);
280 if (error != 0)
281 printf("%s: mbuf_set_status() failed %d\n",
282 __func__, error);
283
284 ifnet_tx_compl(ifp, m);
285}
286
316670eb
A
287/*
288 * Output callback.
289 *
290 * This routine is called only when lo_txstart is disabled.
291 */
1c79356b 292static int
316670eb 293lo_output(struct ifnet *ifp, struct mbuf *m_list)
91447636 294{
316670eb
A
295 struct mbuf *m, *m_tail = NULL;
296 struct ifnet_stat_increment_param s;
297 u_int32_t cnt = 0, len = 0;
298
299 bzero(&s, sizeof(s));
300
2d21ac55 301 for (m = m_list; m; m = m->m_nextpkt) {
39236c6e 302 VERIFY(m->m_flags & M_PKTHDR);
316670eb
A
303 cnt++;
304 len += m->m_pkthdr.len;
1c79356b 305
2d21ac55
A
306 /*
307 * Don't overwrite the rcvif field if it is in use.
308 * This is used to match multicast packets, sent looping
309 * back, with the appropriate group record on input.
310 */
311 if (m->m_pkthdr.rcvif == NULL)
312 m->m_pkthdr.rcvif = ifp;
91447636 313
39236c6e
A
314 m->m_pkthdr.pkt_flags |= PKTF_LOOP;
315 m->m_pkthdr.pkt_hdr = mtod(m, char *);
316
317 /* loopback checksums are always OK */
318 m->m_pkthdr.csum_data = 0xffff;
319 m->m_pkthdr.csum_flags =
320 CSUM_DATA_VALID | CSUM_PSEUDO_HDR |
321 CSUM_IP_CHECKED | CSUM_IP_VALID;
322
316670eb
A
323 m_adj(m, sizeof (struct loopback_header));
324
325 LO_BPF_TAP_OUT(m);
326 if (m->m_nextpkt == NULL) {
327 m_tail = m;
328 }
39037602 329 lo_tx_compl(ifp, m);
316670eb
A
330 }
331
332 s.packets_in = cnt;
333 s.packets_out = cnt;
334 s.bytes_in = len;
39236c6e 335 s.bytes_out = len;
316670eb
A
336
337 return (ifnet_input_extended(ifp, m_list, m_tail, &s));
338}
339
340/*
341 * Pre-enqueue callback.
342 *
343 * This routine is called only when lo_txstart is enabled.
344 */
345static errno_t
346lo_pre_enqueue(struct ifnet *ifp, struct mbuf *m0)
347{
348 struct mbuf *m = m0, *n;
349 int error = 0;
350
351 while (m != NULL) {
39236c6e 352 VERIFY(m->m_flags & M_PKTHDR);
91447636 353
316670eb
A
354 n = m->m_nextpkt;
355 m->m_nextpkt = NULL;
356
357 /*
358 * Don't overwrite the rcvif field if it is in use.
359 * This is used to match multicast packets, sent looping
360 * back, with the appropriate group record on input.
361 */
362 if (m->m_pkthdr.rcvif == NULL)
363 m->m_pkthdr.rcvif = ifp;
1c79356b 364
39236c6e
A
365 m->m_pkthdr.pkt_flags |= PKTF_LOOP;
366 m->m_pkthdr.pkt_hdr = mtod(m, char *);
367
368 /* loopback checksums are always OK */
369 m->m_pkthdr.csum_data = 0xffff;
370 m->m_pkthdr.csum_flags =
371 CSUM_DATA_VALID | CSUM_PSEUDO_HDR |
372 CSUM_IP_CHECKED | CSUM_IP_VALID;
373
316670eb
A
374 m_adj(m, sizeof (struct loopback_header));
375
376 /*
377 * Let the callee free it in case of error,
378 * and perform any necessary accounting.
379 */
380 (void) ifnet_enqueue(ifp, m);
381
382 m = n;
1c79356b 383 }
1c79356b 384
316670eb 385 return (error);
1c79356b
A
386}
387
316670eb
A
388/*
389 * Start output callback.
390 *
391 * This routine is invoked by the start worker thread; because we never call
392 * it directly, there is no need do deploy any serialization mechanism other
393 * than what's already used by the worker thread, i.e. this is already single
394 * threaded.
395 *
396 * This routine is called only when lo_txstart is enabled.
397 */
398static void
399lo_start(struct ifnet *ifp)
400{
401 struct ifnet_stat_increment_param s;
402
403 bzero(&s, sizeof (s));
404
405 for (;;) {
406 struct mbuf *m = NULL, *m_tail = NULL;
407 u_int32_t cnt, len = 0;
408 int sleep_chan = 0;
409 struct timespec ts;
410
411 if (lo_sched_model == IFNET_SCHED_MODEL_NORMAL) {
412 if (ifnet_dequeue_multi(ifp, lo_dequeue_max, &m,
413 &m_tail, &cnt, &len) != 0)
414 break;
415 } else {
416 if (ifnet_dequeue_service_class_multi(ifp,
417 lo_dequeue_sc, lo_dequeue_max, &m,
418 &m_tail, &cnt, &len) != 0)
419 break;
420 }
421
422 LO_BPF_TAP_OUT_MULTI(m);
423
424 if (lo_bw_measure) {
425 if (cnt >= if_bw_measure_size)
426 ifnet_transmit_burst_start(ifp, m);
427 if (lo_bw_sleep_usec > 0) {
428 bzero(&ts, sizeof(ts));
429 ts.tv_nsec = (lo_bw_sleep_usec << 10) * cnt;
430
431 /* Add msleep with timeout */
432 (void) msleep(&sleep_chan, NULL,
433 PSOCK, "lo_start", &ts);
434 }
435 if (cnt >= if_bw_measure_size)
436 ifnet_transmit_burst_end(ifp, m_tail);
437 }
39037602 438 lo_tx_compl(ifp, m);
316670eb
A
439
440 /* stats are required for extended variant */
441 s.packets_in = cnt;
442 s.packets_out = cnt;
443 s.bytes_in = len;
444 s.bytes_out = len;
445
446 (void) ifnet_input_extended(ifp, m, m_tail, &s);
447 }
448}
1c79356b
A
449
450/*
91447636 451 * This is a common pre-output route used by INET and INET6. This could
1c79356b
A
452 * (should?) be split into separate pre-output routines for each protocol.
453 */
2d21ac55 454static errno_t
316670eb
A
455lo_pre_output(struct ifnet *ifp, protocol_family_t protocol_family,
456 struct mbuf **m, const struct sockaddr *dst, void *route, char *frame_type,
457 char *dst_addr)
1c79356b 458{
316670eb
A
459#pragma unused(ifp, dst, dst_addr)
460 struct rtentry *rt = route;
2d21ac55 461
39236c6e 462 VERIFY((*m)->m_flags & M_PKTHDR);
1c79356b 463
39236c6e 464 (*m)->m_flags |= M_LOOP;
1c79356b 465
b0d623f7
A
466 if (rt != NULL) {
467 u_int32_t rt_flags = rt->rt_flags;
468 if (rt_flags & (RTF_REJECT | RTF_BLACKHOLE)) {
469 if (rt_flags & RTF_BLACKHOLE) {
470 m_freem(*m);
316670eb 471 return (EJUSTRETURN);
b0d623f7
A
472 } else {
473 return ((rt_flags & RTF_HOST) ?
474 EHOSTUNREACH : ENETUNREACH);
475 }
91447636 476 }
1c79356b 477 }
b0d623f7 478
316670eb 479 bcopy(&protocol_family, frame_type, sizeof (protocol_family));
1c79356b 480
316670eb 481 return (0);
1c79356b
A
482}
483
1c79356b
A
484/*
485 * lo_input - This should work for all attached protocols that use the
486 * ifq/schednetisr input mechanism.
487 */
2d21ac55 488static errno_t
316670eb 489lo_input(struct ifnet *ifp, protocol_family_t protocol_family, struct mbuf *m)
1c79356b 490{
316670eb 491#pragma unused(ifp, protocol_family)
39037602
A
492
493 if ((ifp->if_xflags & IFXF_TIMESTAMP_ENABLED) != 0) {
494 errno_t error;
495 struct timespec now;
496 u_int64_t ts;
497
498 nanouptime(&now);
499 net_timernsec(&now, &ts);
500
501 error = mbuf_set_timestamp(m, ts, TRUE);
502 if (error != 0)
503 printf("%s: mbuf_set_timestamp() failed %d\n",
504 __func__, error);
505 }
506
91447636 507 if (proto_input(protocol_family, m) != 0)
1c79356b 508 m_freem(m);
1c79356b
A
509 return (0);
510}
511
1c79356b
A
512/* ARGSUSED */
513static void
316670eb 514lo_rtrequest(int cmd, struct rtentry *rt, struct sockaddr *sa)
1c79356b 515{
316670eb 516#pragma unused(cmd, sa)
b0d623f7
A
517 if (rt != NULL) {
518 RT_LOCK_ASSERT_HELD(rt);
1c79356b
A
519 rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu; /* for ISO */
520 /*
521 * For optimal performance, the send and receive buffers
522 * should be at least twice the MTU plus a little more for
523 * overhead.
524 */
316670eb 525 rt->rt_rmx.rmx_recvpipe = rt->rt_rmx.rmx_sendpipe = 3 * LOMTU;
1c79356b
A
526 }
527}
528
529/*
530 * Process an ioctl request.
531 */
91447636 532static errno_t
316670eb 533lo_ioctl(struct ifnet *ifp, u_long cmd, void *data)
1c79356b 534{
316670eb 535 int error = 0;
1c79356b
A
536
537 switch (cmd) {
538
316670eb
A
539 case SIOCSIFADDR: { /* struct ifaddr pointer */
540 struct ifaddr *ifa = data;
541
542 ifnet_set_flags(ifp, IFF_UP|IFF_RUNNING, IFF_UP|IFF_RUNNING);
6d2010ae 543 IFA_LOCK_SPIN(ifa);
316670eb 544 ifa->ifa_rtrequest = lo_rtrequest;
6d2010ae 545 IFA_UNLOCK(ifa);
1c79356b
A
546 /*
547 * Everything else is done at a higher level.
548 */
549 break;
316670eb 550 }
1c79356b 551
316670eb
A
552 case SIOCADDMULTI: /* struct ifreq */
553 case SIOCDELMULTI: { /* struct ifreq */
554 struct ifreq *ifr = data;
555
556 if (ifr == NULL) {
1c79356b
A
557 error = EAFNOSUPPORT; /* XXX */
558 break;
559 }
560 switch (ifr->ifr_addr.sa_family) {
561
562#if INET
563 case AF_INET:
564 break;
565#endif
566#if INET6
567 case AF_INET6:
568 break;
569#endif
570
571 default:
572 error = EAFNOSUPPORT;
573 break;
574 }
575 break;
316670eb
A
576 }
577
578 case SIOCSIFMTU: { /* struct ifreq */
579 struct ifreq *ifr = data;
1c79356b 580
316670eb 581 bcopy(&ifr->ifr_mtu, &ifp->if_mtu, sizeof (int));
1c79356b 582 break;
316670eb 583 }
1c79356b 584
316670eb 585 case SIOCSIFFLAGS: /* struct ifreq */
39037602
A
586 case SIOCSIFTIMESTAMPENABLE:
587 case SIOCSIFTIMESTAMPDISABLE:
1c79356b
A
588 break;
589
590 default:
591 error = EOPNOTSUPP;
4a249263 592 break;
1c79356b
A
593 }
594 return (error);
595}
596#endif /* NLOOP > 0 */
597
598
316670eb
A
599static errno_t
600lo_attach_proto(struct ifnet *ifp, protocol_family_t protocol_family)
1c79356b 601{
2d21ac55
A
602 struct ifnet_attach_proto_param_v2 proto;
603 errno_t result = 0;
316670eb
A
604
605 bzero(&proto, sizeof (proto));
2d21ac55
A
606 proto.input = lo_input;
607 proto.pre_output = lo_pre_output;
316670eb 608
2d21ac55 609 result = ifnet_attach_protocol_v2(ifp, protocol_family, &proto);
1c79356b 610
2d21ac55 611 if (result && result != EEXIST) {
316670eb
A
612 printf("lo_attach_proto: ifnet_attach_protocol for %u "
613 "returned=%d\n", protocol_family, result);
9bccf70c 614 }
316670eb
A
615
616 return (result);
9bccf70c
A
617}
618
316670eb
A
619static void
620lo_reg_if_mods(void)
55e303ae 621{
316670eb 622 int error;
55e303ae 623
55e303ae 624 /* Register protocol registration functions */
316670eb
A
625 if ((error = proto_register_plumber(PF_INET,
626 APPLE_IF_FAM_LOOPBACK, lo_attach_proto, NULL)) != 0)
627 printf("proto_register_plumber failed for AF_INET "
628 "error=%d\n", error);
629
630 if ((error = proto_register_plumber(PF_INET6,
631 APPLE_IF_FAM_LOOPBACK, lo_attach_proto, NULL)) != 0)
632 printf("proto_register_plumber failed for AF_INET6 "
633 "error=%d\n", error);
55e303ae 634}
1c79356b 635
91447636 636static errno_t
316670eb
A
637lo_set_bpf_tap(struct ifnet *ifp, bpf_tap_mode mode,
638 bpf_packet_func bpf_callback)
1c79356b 639{
316670eb 640 VERIFY(ifp == lo_ifp);
1c79356b 641
316670eb
A
642 lo_statics[0].bpf_mode = mode;
643
644 switch (mode) {
645 case BPF_TAP_DISABLE:
646 case BPF_TAP_INPUT:
647 lo_statics[0].bpf_callback = NULL;
648 break;
649
650 case BPF_TAP_OUTPUT:
651 case BPF_TAP_INPUT_OUTPUT:
652 lo_statics[0].bpf_callback = bpf_callback;
653 break;
1c79356b
A
654 }
655
316670eb 656 return (0);
1c79356b
A
657}
658
1c79356b
A
659/* ARGSUSED */
660void
2d21ac55 661loopattach(void)
1c79356b 662{
316670eb 663 struct ifnet_init_eparams lo_init;
2d21ac55 664 errno_t result = 0;
1c79356b 665
316670eb 666 PE_parse_boot_argn("lo_txstart", &lo_txstart, sizeof (lo_txstart));
1c79356b 667
2d21ac55 668 lo_reg_if_mods();
316670eb
A
669
670 lo_statics[0].bpf_callback = NULL;
671 lo_statics[0].bpf_mode = BPF_TAP_DISABLE;
672
673 bzero(&lo_init, sizeof (lo_init));
674 lo_init.ver = IFNET_INIT_CURRENT_VERSION;
675 lo_init.len = sizeof (lo_init);
676 lo_init.sndq_maxlen = LOSNDQ_MAXLEN;
677 if (lo_txstart) {
678 lo_init.flags = 0;
679 lo_init.pre_enqueue = lo_pre_enqueue;
680 lo_init.start = lo_start;
681 lo_init.output_sched_model = lo_sched_model;
682 } else {
683 lo_init.flags = IFNET_INIT_LEGACY;
684 lo_init.output = lo_output;
685 }
686 lo_init.name = "lo";
687 lo_init.unit = 0;
688 lo_init.family = IFNET_FAMILY_LOOPBACK;
689 lo_init.type = IFT_LOOP;
690 lo_init.demux = lo_demux;
691 lo_init.add_proto = lo_add_proto;
692 lo_init.del_proto = lo_del_proto;
39236c6e 693 lo_init.framer_extended = lo_framer;
316670eb
A
694 lo_init.softc = &lo_statics[0];
695 lo_init.ioctl = lo_ioctl;
696 lo_init.set_bpf_tap = lo_set_bpf_tap;
697
698 result = ifnet_allocate_extended(&lo_init, &lo_ifp);
2d21ac55 699 if (result != 0) {
316670eb
A
700 panic("%s: couldn't allocate loopback ifnet (%d)\n",
701 __func__, result);
702 /* NOTREACHED */
2d21ac55 703 }
316670eb 704
2d21ac55 705 ifnet_set_mtu(lo_ifp, LOMTU);
316670eb
A
706 ifnet_set_flags(lo_ifp, IFF_LOOPBACK | IFF_MULTICAST,
707 IFF_LOOPBACK | IFF_MULTICAST);
708 ifnet_set_offload(lo_ifp,
709 IFNET_CSUM_IP | IFNET_CSUM_TCP | IFNET_CSUM_UDP |
710 IFNET_CSUM_TCPIPV6 | IFNET_CSUM_UDPIPV6 | IFNET_IPV6_FRAGMENT |
39037602
A
711 IFNET_CSUM_FRAGMENT | IFNET_IP_FRAGMENT | IFNET_MULTIPAGES |
712 IFNET_TX_STATUS | IFNET_SW_TIMESTAMP);
316670eb 713 ifnet_set_hdrlen(lo_ifp, sizeof (struct loopback_header));
2d21ac55
A
714 ifnet_set_eflags(lo_ifp, IFEF_SENDLIST, IFEF_SENDLIST);
715
716#if CONFIG_MACF_NET
316670eb 717 mac_ifnet_label_init(ifp);
1c79356b 718#endif
2d21ac55
A
719
720 result = ifnet_attach(lo_ifp, NULL);
721 if (result != 0) {
316670eb
A
722 panic("%s: couldn't attach loopback ifnet (%d)\n",
723 __func__, result);
724 /* NOTREACHED */
725 }
726 bpfattach(lo_ifp, DLT_NULL, sizeof (u_int32_t));
727}
728
729static int
730sysctl_dequeue_max SYSCTL_HANDLER_ARGS
731{
732#pragma unused(arg1, arg2)
733 u_int32_t i;
734 int err;
735
736 i = lo_dequeue_max;
737
738 err = sysctl_handle_int(oidp, &i, 0, req);
739 if (err != 0 || req->newptr == USER_ADDR_NULL)
740 return (err);
741
742 if (i < 1)
743 i = 1;
744 else if (i > LOSNDQ_MAXLEN)
745 i = LOSNDQ_MAXLEN;
746
747 lo_dequeue_max = i;
748
749 return (err);
750}
751
752static int
753sysctl_sched_model SYSCTL_HANDLER_ARGS
754{
755#pragma unused(arg1, arg2)
756 u_int32_t i;
757 int err;
758
759 i = lo_sched_model;
760
761 err = sysctl_handle_int(oidp, &i, 0, req);
762 if (err != 0 || req->newptr == USER_ADDR_NULL)
763 return (err);
764
765 switch (i) {
766 case IFNET_SCHED_MODEL_NORMAL:
767 case IFNET_SCHED_MODEL_DRIVER_MANAGED:
39037602 768 case IFNET_SCHED_MODEL_FQ_CODEL:
316670eb
A
769 break;
770
771 default:
772 err = EINVAL;
773 break;
1c79356b 774 }
316670eb
A
775
776 if (err == 0 && (err = ifnet_set_output_sched_model(lo_ifp, i)) == 0)
777 lo_sched_model = i;
778
779 return (err);
780}
781
782static int
783sysctl_dequeue_scidx SYSCTL_HANDLER_ARGS
784{
785#pragma unused(arg1, arg2)
786 u_int32_t i;
787 int err;
788
789 i = lo_dequeue_scidx;
790
791 err = sysctl_handle_int(oidp, &i, 0, req);
792 if (err != 0 || req->newptr == USER_ADDR_NULL)
793 return (err);
794
795 if (!MBUF_VALID_SCIDX(i))
796 return (EINVAL);
797
798 if (lo_sched_model != IFNET_SCHED_MODEL_DRIVER_MANAGED)
799 return (ENODEV);
800
801 lo_dequeue_sc = m_service_class_from_idx(i);
802 lo_dequeue_scidx = MBUF_SCIDX(lo_dequeue_sc);
803
804 return (err);
1c79356b 805}