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