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