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