1 /*
2 * Copyright (c) 2019-2021 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 #if SKYWALK
29
30 #include <sys/param.h>
31 #include <sys/kernel.h>
32 #include <sys/malloc.h>
33 #include <sys/mbuf.h>
34 #include <sys/queue.h>
35 #include <sys/socket.h>
36 #include <sys/sockio.h>
37 #include <sys/sysctl.h>
38 #include <sys/systm.h>
39 #include <sys/kern_event.h>
40 #include <sys/mcache.h>
41 #include <sys/syslog.h>
42
43 #include <net/bpf.h>
44 #include <net/ethernet.h>
45 #include <net/if.h>
46 #include <net/if_vlan_var.h>
47 #include <net/if_arp.h>
48 #include <net/if_dl.h>
49 #include <net/if_ether.h>
50 #include <net/if_types.h>
51 #include <libkern/OSAtomic.h>
52
53 #include <net/dlil.h>
54
55 #include <net/kpi_interface.h>
56 #include <net/kpi_protocol.h>
57
58 #include <kern/locks.h>
59 #include <kern/zalloc.h>
60
61 #ifdef INET
62 #include <netinet/in.h>
63 #include <netinet/if_ether.h>
64 #endif
65
66 #include <net/if_media.h>
67 #include <net/ether_if_module.h>
68 #include <skywalk/os_skywalk_private.h>
69 #include <skywalk/nexus/netif/nx_netif.h>
70 #include <skywalk/channel/channel_var.h>
71
72 static boolean_t
is_power_of_two(unsigned int val)73 is_power_of_two(unsigned int val)
74 {
75 return (val & (val - 1)) == 0;
76 }
77
78 #define HEADLESS_ZERO_IFNAME "zero"
79 #define HEADLESS_NULL_IFNAME "null"
80
81 SYSCTL_DECL(_net_link);
82 SYSCTL_NODE(_net_link, OID_AUTO, headless, CTLFLAG_RW | CTLFLAG_LOCKED, 0,
83 "headless interface");
84
85 static int if_headless_nxattach = 0;
86 SYSCTL_INT(_net_link_headless, OID_AUTO, nxattach,
87 CTLFLAG_RW | CTLFLAG_LOCKED, &if_headless_nxattach, 0,
88 "headless interface auto-attach nexus");
89
90 static int if_headless_debug = 0;
91 SYSCTL_INT(_net_link_headless, OID_AUTO, debug,
92 CTLFLAG_RW | CTLFLAG_LOCKED, &if_headless_debug, 0,
93 "headless interface debug logs");
94
95 static int if_headless_multibuflet = 0;
96 SYSCTL_INT(_net_link_headless, OID_AUTO, multibuflet,
97 CTLFLAG_RW | CTLFLAG_LOCKED, &if_headless_multibuflet, 0,
98 "headless interface using multi-buflet packets");
99
100 static int if_headless_packet_length = 1500;
101 SYSCTL_INT(_net_link_headless, OID_AUTO, packet_length,
102 CTLFLAG_RW | CTLFLAG_LOCKED, &if_headless_packet_length, 0,
103 "headless interface packet length");
104
105 static int if_headless_create_payload = 0;
106 SYSCTL_INT(_net_link_headless, OID_AUTO, create_payload,
107 CTLFLAG_RW | CTLFLAG_LOCKED, &if_headless_create_payload, 0,
108 "headless interface create payload data or not");
109
110 /*
111 * SIOCSDRVSPEC
112 */
113 enum {
114 IF_HEADLESS_S_CMD_NONE = 0,
115 IF_HEADLESS_S_CMD_SET_MEDIA = 1,
116 };
117
118 #define IF_HEADLESS_MEDIA_LIST_MAX 27
119
120 struct if_headless_media {
121 int32_t iffm_current;
122 uint32_t iffm_count;
123 uint32_t iffm_reserved[3];
124 int32_t iffm_list[IF_HEADLESS_MEDIA_LIST_MAX];
125 };
126
127 struct if_headless_request {
128 uint64_t iffr_reserved[4];
129 union {
130 char iffru_buf[128]; /* stable size */
131 struct if_headless_media iffru_media;
132 } iffr_u;
133 #define iffr_media iffr_u.iffru_media
134 };
135
136 /* sysctl net.link.headless.tx_headroom */
137 #define headless_TX_HEADROOM_MAX 32
138 static uint16_t if_headless_tx_headroom = 0;
139
140 extern void if_headless_init(void);
141
142 static int
143 headless_tx_headroom_sysctl SYSCTL_HANDLER_ARGS
144 {
145 #pragma unused(oidp, arg1, arg2)
146 uint16_t new_value;
147 int changed;
148 int error;
149
150 error = sysctl_io_number(req, if_headless_tx_headroom,
151 sizeof(if_headless_tx_headroom), &new_value, &changed);
152 if (error == 0 && changed != 0) {
153 if (new_value > headless_TX_HEADROOM_MAX ||
154 (new_value % 8) != 0) {
155 return EINVAL;
156 }
157 if_headless_tx_headroom = new_value;
158 }
159 return 0;
160 }
161
162 SYSCTL_PROC(_net_link_headless, OID_AUTO, tx_headroom,
163 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
164 0, 0, headless_tx_headroom_sysctl, "IU", "headless ethernet Tx headroom");
165
166 /* sysctl net.link.headless.max_mtu */
167 #define headless_MAX_MTU_DEFAULT 2048
168 #define headless_MAX_MTU_MAX ((16 * 1024) - ETHER_HDR_LEN)
169
170 static unsigned int if_headless_max_mtu = headless_MAX_MTU_DEFAULT;
171
172 /* sysctl net.link.headless.buflet_size */
173 #define headless_BUFLET_SIZE_MIN 512
174 #define headless_BUFLET_SIZE_MAX 2048
175
176 static unsigned int if_headless_buflet_size = headless_BUFLET_SIZE_MIN;
177
178 static int
179 headless_max_mtu_sysctl SYSCTL_HANDLER_ARGS
180 {
181 #pragma unused(oidp, arg1, arg2)
182 unsigned int new_value;
183 int changed;
184 int error;
185
186 error = sysctl_io_number(req, if_headless_max_mtu,
187 sizeof(if_headless_max_mtu), &new_value, &changed);
188 if (error == 0 && changed != 0) {
189 if (new_value > headless_MAX_MTU_MAX ||
190 new_value < ETHERMTU ||
191 new_value <= if_headless_buflet_size) {
192 return EINVAL;
193 }
194 if_headless_max_mtu = new_value;
195 }
196 return 0;
197 }
198
199 SYSCTL_PROC(_net_link_headless, OID_AUTO, max_mtu,
200 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
201 0, 0, headless_max_mtu_sysctl, "IU", "headless interface maximum MTU");
202
203 static int
204 headless_buflet_size_sysctl SYSCTL_HANDLER_ARGS
205 {
206 #pragma unused(oidp, arg1, arg2)
207 unsigned int new_value;
208 int changed;
209 int error;
210
211 error = sysctl_io_number(req, if_headless_buflet_size,
212 sizeof(if_headless_buflet_size), &new_value, &changed);
213 if (error == 0 && changed != 0) {
214 /* must be a power of 2 between min and max */
215 if (new_value > headless_BUFLET_SIZE_MAX ||
216 new_value < headless_BUFLET_SIZE_MIN ||
217 !is_power_of_two(new_value) ||
218 new_value >= if_headless_max_mtu) {
219 return EINVAL;
220 }
221 if_headless_buflet_size = new_value;
222 }
223 return 0;
224 }
225
226 SYSCTL_PROC(_net_link_headless, OID_AUTO, buflet_size,
227 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
228 0, 0, headless_buflet_size_sysctl, "IU", "headless interface buflet size");
229
230 /**
231 ** virtual ethernet structures, types
232 **/
233
234 #define IFF_NUM_TX_RINGS_WMM_MODE 4
235 #define IFF_NUM_RX_RINGS_WMM_MODE 1
236 #define IFF_MAX_TX_RINGS IFF_NUM_TX_RINGS_WMM_MODE
237 #define IFF_MAX_RX_RINGS IFF_NUM_RX_RINGS_WMM_MODE
238
239 typedef uint16_t iff_flags_t;
240 #define IFF_FLAGS_HWCSUM 0x0001
241 #define IFF_FLAGS_BSD_MODE 0x0002
242 #define IFF_FLAGS_DETACHING 0x0004
243 #define IFF_FLAGS_WMM_MODE 0x0008
244 #define IFF_FLAGS_MULTIBUFLETS 0x0010
245 #define IFF_FLAGS_COPYPKT_MODE 0x0020
246
247 typedef struct {
248 kern_pbufpool_t fpp_pp;
249 uint32_t fpp_retain_count;
250 } headless_packet_pool, *headless_packet_pool_t;
251
252 typedef struct {
253 uuid_t fnx_provider;
254 uuid_t fnx_instance;
255 } headless_nx, *headless_nx_t;
256
257 struct if_headless {
258 struct if_clone * iff_cloner;
259 char iff_name[IFNAMSIZ]; /* our unique id */
260 ifnet_t iff_ifp;
261 iff_flags_t iff_flags;
262 uint32_t iff_retain_count;
263 ifnet_t iff_peer; /* the other end */
264 int iff_media_current;
265 int iff_media_active;
266 uint32_t iff_media_count;
267 int iff_media_list[IF_HEADLESS_MEDIA_LIST_MAX];
268 struct mbuf * iff_pending_tx_packet;
269 boolean_t iff_start_busy;
270 unsigned int iff_max_mtu;
271 headless_nx iff_nx;
272 kern_channel_ring_t iff_rx_ring[IFF_MAX_RX_RINGS];
273 kern_channel_ring_t iff_tx_ring[IFF_MAX_TX_RINGS];
274 thread_call_t iff_doorbell_tcall;
275 boolean_t iff_tcall_active;
276 boolean_t iff_waiting_for_tcall;
277 boolean_t iff_channel_connected;
278 headless_packet_pool_t iff_fpp;
279 uint16_t iff_tx_headroom;
280 };
281
282 typedef struct if_headless * if_headless_ref;
283
284 static if_headless_ref
285 ifnet_get_if_headless(ifnet_t ifp);
286
287 #define HEADLESS_DPRINTF(fmt, ...) \
288 { if (if_headless_debug != 0) printf("%s " fmt, __func__, ## __VA_ARGS__); }
289
290 static inline void
headless_set_detaching(if_headless_ref headlessif)291 headless_set_detaching(if_headless_ref headlessif)
292 {
293 headlessif->iff_flags |= IFF_FLAGS_DETACHING;
294 }
295
296 static inline boolean_t
headless_is_detaching(if_headless_ref headlessif)297 headless_is_detaching(if_headless_ref headlessif)
298 {
299 return (headlessif->iff_flags & IFF_FLAGS_DETACHING) != 0;
300 }
301
302 static inline boolean_t
headless_using_multibuflets(if_headless_ref headlessif)303 headless_using_multibuflets(if_headless_ref headlessif)
304 {
305 return (headlessif->iff_flags & IFF_FLAGS_MULTIBUFLETS) != 0;
306 }
307
308 #define HEADLESS_MAXUNIT IF_MAXUNIT
309 #define HEADLESS_ZONE_MAX_ELEM MIN(IFNETS_MAX, HEADLESS_MAXUNIT)
310
311 static int headless_clone_create(struct if_clone *, u_int32_t, void *);
312 static int headless_clone_destroy(ifnet_t);
313 static int headless_ioctl(ifnet_t ifp, u_long cmd, void * addr);
314 static void headless_if_free(ifnet_t ifp);
315 static void headless_ifnet_set_attrs(if_headless_ref headlessif, ifnet_t ifp);
316 static void headless_free(if_headless_ref headlessif);
317
318 static struct if_clone
319 headless_zero_cloner = IF_CLONE_INITIALIZER(HEADLESS_ZERO_IFNAME,
320 headless_clone_create,
321 headless_clone_destroy,
322 0,
323 HEADLESS_MAXUNIT,
324 HEADLESS_ZONE_MAX_ELEM,
325 sizeof(struct if_headless));
326
327 static struct if_clone
328 headless_null_cloner = IF_CLONE_INITIALIZER(HEADLESS_NULL_IFNAME,
329 headless_clone_create,
330 headless_clone_destroy,
331 0,
332 HEADLESS_MAXUNIT,
333 HEADLESS_ZONE_MAX_ELEM,
334 sizeof(struct if_headless));
335
336 static void interface_link_event(ifnet_t ifp, u_int32_t event_code);
337
338 /* some media words to pretend to be ethernet */
339 static int default_media_words[] = {
340 IFM_MAKEWORD(IFM_ETHER, 0, 0, 0),
341 IFM_MAKEWORD(IFM_ETHER, IFM_10G_T, IFM_FDX, 0),
342 IFM_MAKEWORD(IFM_ETHER, IFM_2500_T, IFM_FDX, 0),
343 IFM_MAKEWORD(IFM_ETHER, IFM_5000_T, IFM_FDX, 0),
344 };
345 #define default_media_words_count (sizeof(default_media_words) \
346 / sizeof (default_media_words[0]))
347
348 /**
349 ** veth locks
350 **/
351
352 static LCK_GRP_DECLARE(headless_lck_grp, "headless");
353 static LCK_MTX_DECLARE(headless_lck_mtx, &headless_lck_grp);
354
355 static inline void
headless_lock(void)356 headless_lock(void)
357 {
358 lck_mtx_lock(&headless_lck_mtx);
359 }
360
361 static inline void
headless_unlock(void)362 headless_unlock(void)
363 {
364 lck_mtx_unlock(&headless_lck_mtx);
365 }
366
367 static inline unsigned int
headless_max_mtu(ifnet_t ifp)368 headless_max_mtu(ifnet_t ifp)
369 {
370 if_headless_ref headlessif;
371 unsigned int max_mtu = ETHERMTU;
372
373 headless_lock();
374 headlessif = ifnet_get_if_headless(ifp);
375 if (headlessif != NULL) {
376 max_mtu = headlessif->iff_max_mtu;
377 }
378 headless_unlock();
379 return max_mtu;
380 }
381
382 static void
headless_packet_pool_free(headless_packet_pool_t fpp)383 headless_packet_pool_free(headless_packet_pool_t fpp)
384 {
385 kern_pbufpool_destroy(fpp->fpp_pp);
386 kfree_type(headless_packet_pool, fpp);
387 }
388
389 static void
headless_free(if_headless_ref headlessif)390 headless_free(if_headless_ref headlessif)
391 {
392 assert(headlessif->iff_retain_count == 0);
393 if (headlessif->iff_fpp != NULL) {
394 headless_packet_pool_free(headlessif->iff_fpp);
395 }
396
397 HEADLESS_DPRINTF("%s\n", headlessif->iff_name);
398 if_clone_softc_deallocate(headlessif->iff_cloner, headlessif);
399 }
400
401 static void
headless_release(if_headless_ref headlessif)402 headless_release(if_headless_ref headlessif)
403 {
404 u_int32_t old_retain_count;
405
406 old_retain_count = OSDecrementAtomic(&headlessif->iff_retain_count);
407 switch (old_retain_count) {
408 case 0:
409 assert(old_retain_count != 0);
410 break;
411 case 1:
412 headless_free(headlessif);
413 break;
414 default:
415 break;
416 }
417 return;
418 }
419
420 static void
headless_retain(if_headless_ref headlessif)421 headless_retain(if_headless_ref headlessif)
422 {
423 OSIncrementAtomic(&headlessif->iff_retain_count);
424 }
425
426 static void
headless_seg_ctor_fn(const kern_pbufpool_t pp,const kern_segment_t buf_seg,const IOSKMemoryDescriptor buf_desc)427 headless_seg_ctor_fn(const kern_pbufpool_t pp, const kern_segment_t buf_seg,
428 const IOSKMemoryDescriptor buf_desc)
429 {
430 #pragma unused(pp, buf_seg, buf_desc)
431 }
432
433 static void
headless_seg_dtor_fn(const kern_pbufpool_t pp,const kern_segment_t buf_seg,const IOSKMemoryDescriptor buf_desc)434 headless_seg_dtor_fn(const kern_pbufpool_t pp, const kern_segment_t buf_seg,
435 const IOSKMemoryDescriptor buf_desc)
436 {
437 #pragma unused(pp, buf_seg, buf_desc)
438 }
439
440 static headless_packet_pool_t
headless_packet_pool_alloc(boolean_t multi_buflet,unsigned int max_mtu)441 headless_packet_pool_alloc(boolean_t multi_buflet, unsigned int max_mtu)
442 {
443 headless_packet_pool_t fpp = NULL;
444 errno_t error;
445 struct kern_pbufpool * pp;
446 struct kern_pbufpool_init pp_init;
447
448 bzero(&pp_init, sizeof(pp_init));
449 pp_init.kbi_version = KERN_PBUFPOOL_CURRENT_VERSION;
450 pp_init.kbi_flags |= KBIF_USER_ACCESS;
451 pp_init.kbi_flags |= KBIF_VIRTUAL_DEVICE;
452 (void)snprintf((char *)pp_init.kbi_name, sizeof(pp_init.kbi_name),
453 "%s", "headless ethernet");
454 pp_init.kbi_packets = 4096; /* XXX make this configurable */
455 if (multi_buflet) {
456 pp_init.kbi_bufsize = if_headless_buflet_size;
457 pp_init.kbi_max_frags = howmany(max_mtu, if_headless_buflet_size);
458 pp_init.kbi_buflets = pp_init.kbi_packets *
459 pp_init.kbi_max_frags;
460 pp_init.kbi_flags |= KBIF_BUFFER_ON_DEMAND;
461 } else {
462 pp_init.kbi_bufsize = max_mtu;
463 pp_init.kbi_max_frags = 1;
464 pp_init.kbi_buflets = pp_init.kbi_packets;
465 }
466 pp_init.kbi_buf_seg_size = skmem_usr_buf_seg_size;
467 if (skywalk_netif_direct_enabled()) {
468 pp_init.kbi_flags |= KBIF_USER_ACCESS;
469 }
470 pp_init.kbi_buf_seg_ctor = headless_seg_ctor_fn;
471 pp_init.kbi_buf_seg_dtor = headless_seg_dtor_fn;
472 pp_init.kbi_ctx = NULL;
473 pp_init.kbi_ctx_retain = NULL;
474 pp_init.kbi_ctx_release = NULL;
475
476 error = kern_pbufpool_create(&pp_init, &pp, NULL);
477 if (error != 0) {
478 printf("%s: kern_pbufpool_create failed %d\n", __func__, error);
479 } else {
480 fpp = kalloc_type(headless_packet_pool, Z_WAITOK | Z_ZERO);
481 fpp->fpp_pp = pp;
482 fpp->fpp_retain_count = 1;
483 }
484 return fpp;
485 }
486
487 /**
488 ** nexus netif domain provider
489 **/
490 static errno_t
headless_nxdp_init(kern_nexus_domain_provider_t domprov)491 headless_nxdp_init(kern_nexus_domain_provider_t domprov)
492 {
493 #pragma unused(domprov)
494 return 0;
495 }
496
497 static void
headless_nxdp_fini(kern_nexus_domain_provider_t domprov)498 headless_nxdp_fini(kern_nexus_domain_provider_t domprov)
499 {
500 #pragma unused(domprov)
501 }
502
503 static uuid_t headless_nx_dom_prov;
504
505 static errno_t
headless_register_nexus_domain_provider(void)506 headless_register_nexus_domain_provider(void)
507 {
508 const struct kern_nexus_domain_provider_init dp_init = {
509 .nxdpi_version = KERN_NEXUS_DOMAIN_PROVIDER_CURRENT_VERSION,
510 .nxdpi_flags = 0,
511 .nxdpi_init = headless_nxdp_init,
512 .nxdpi_fini = headless_nxdp_fini
513 };
514 errno_t err = 0;
515
516 /* headless_nxdp_init() is called before this function returns */
517 err = kern_nexus_register_domain_provider(NEXUS_TYPE_NET_IF,
518 (const uint8_t *)
519 "com.apple.headless",
520 &dp_init, sizeof(dp_init),
521 &headless_nx_dom_prov);
522 if (err != 0) {
523 printf("%s: failed to register domain provider\n", __func__);
524 return err;
525 }
526 return 0;
527 }
528
529 /**
530 ** netif nexus routines
531 **/
532 static if_headless_ref
headless_nexus_context(kern_nexus_t nexus)533 headless_nexus_context(kern_nexus_t nexus)
534 {
535 if_headless_ref headlessif;
536
537 headlessif = (if_headless_ref)kern_nexus_get_context(nexus);
538 assert(headlessif != NULL);
539 return headlessif;
540 }
541
542 static errno_t
headless_nx_ring_init(kern_nexus_provider_t nxprov,kern_nexus_t nexus,kern_channel_t channel,kern_channel_ring_t ring,boolean_t is_tx_ring,void ** ring_ctx)543 headless_nx_ring_init(kern_nexus_provider_t nxprov, kern_nexus_t nexus,
544 kern_channel_t channel, kern_channel_ring_t ring, boolean_t is_tx_ring,
545 void **ring_ctx)
546 {
547 if_headless_ref headlessif;
548 #pragma unused(nxprov, channel, ring_ctx)
549 headless_lock();
550 headlessif = headless_nexus_context(nexus);
551 if (headless_is_detaching(headlessif)) {
552 headless_unlock();
553 return 0;
554 }
555 if (is_tx_ring) {
556 assert(headlessif->iff_tx_ring[0] == NULL);
557 headlessif->iff_tx_ring[0] = ring;
558 } else {
559 assert(headlessif->iff_rx_ring[0] == NULL);
560 headlessif->iff_rx_ring[0] = ring;
561 }
562 headless_unlock();
563 HEADLESS_DPRINTF("%s: %s ring init\n",
564 headlessif->iff_name, is_tx_ring ? "TX" : "RX");
565 return 0;
566 }
567
568 static void
headless_nx_ring_fini(kern_nexus_provider_t nxprov,kern_nexus_t nexus,kern_channel_ring_t ring)569 headless_nx_ring_fini(kern_nexus_provider_t nxprov, kern_nexus_t nexus,
570 kern_channel_ring_t ring)
571 {
572 #pragma unused(nxprov, ring)
573 if_headless_ref headlessif;
574 thread_call_t tcall = NULL;
575
576 headless_lock();
577 headlessif = headless_nexus_context(nexus);
578 if (headlessif->iff_rx_ring[0] == ring) {
579 headlessif->iff_rx_ring[0] = NULL;
580 HEADLESS_DPRINTF("%s: RX ring fini\n", headlessif->iff_name);
581 } else if (headlessif->iff_tx_ring[0] == ring) {
582 tcall = headlessif->iff_doorbell_tcall;
583 headlessif->iff_doorbell_tcall = NULL;
584 headlessif->iff_tx_ring[0] = NULL;
585 }
586 headless_unlock();
587 if (tcall != NULL) {
588 boolean_t success;
589
590 success = thread_call_cancel_wait(tcall);
591 HEADLESS_DPRINTF("%s: thread_call_cancel %s\n",
592 headlessif->iff_name,
593 success ? "SUCCESS" : "FAILURE");
594 if (!success) {
595 headless_lock();
596 if (headlessif->iff_tcall_active) {
597 headlessif->iff_waiting_for_tcall = TRUE;
598 HEADLESS_DPRINTF("%s: *waiting for threadcall\n",
599 headlessif->iff_name);
600 do {
601 msleep(headlessif, &headless_lck_mtx,
602 PZERO, "headless threadcall", 0);
603 } while (headlessif->iff_tcall_active);
604 HEADLESS_DPRINTF("%s: ^threadcall done\n",
605 headlessif->iff_name);
606 headlessif->iff_waiting_for_tcall = FALSE;
607 }
608 headless_unlock();
609 }
610 success = thread_call_free(tcall);
611 HEADLESS_DPRINTF("%s: thread_call_free %s\n",
612 headlessif->iff_name,
613 success ? "SUCCESS" : "FAILURE");
614 headless_release(headlessif);
615 assert(success == TRUE);
616 }
617 }
618
619 static errno_t
headless_nx_pre_connect(kern_nexus_provider_t nxprov,proc_t proc,kern_nexus_t nexus,nexus_port_t port,kern_channel_t channel,void ** channel_context)620 headless_nx_pre_connect(kern_nexus_provider_t nxprov,
621 proc_t proc, kern_nexus_t nexus, nexus_port_t port, kern_channel_t channel,
622 void **channel_context)
623 {
624 #pragma unused(nxprov, proc, nexus, port, channel, channel_context)
625 return 0;
626 }
627
628 static errno_t
headless_nx_connected(kern_nexus_provider_t nxprov,kern_nexus_t nexus,kern_channel_t channel)629 headless_nx_connected(kern_nexus_provider_t nxprov,
630 kern_nexus_t nexus, kern_channel_t channel)
631 {
632 #pragma unused(nxprov, channel)
633 if_headless_ref headlessif;
634
635 headlessif = headless_nexus_context(nexus);
636 headless_lock();
637 if (headless_is_detaching(headlessif)) {
638 headless_unlock();
639 return EBUSY;
640 }
641 headless_retain(headlessif);
642 headlessif->iff_channel_connected = TRUE;
643 headless_unlock();
644 HEADLESS_DPRINTF("%s: connected channel %p\n",
645 headlessif->iff_name, channel);
646 return 0;
647 }
648
649 static void
headless_nx_pre_disconnect(kern_nexus_provider_t nxprov,kern_nexus_t nexus,kern_channel_t channel)650 headless_nx_pre_disconnect(kern_nexus_provider_t nxprov,
651 kern_nexus_t nexus, kern_channel_t channel)
652 {
653 #pragma unused(nxprov, channel)
654 if_headless_ref headlessif;
655
656 headlessif = headless_nexus_context(nexus);
657 HEADLESS_DPRINTF("%s: pre-disconnect channel %p\n",
658 headlessif->iff_name, channel);
659 /* Quiesce the interface and flush any pending outbound packets. */
660 if_down(headlessif->iff_ifp);
661 headless_lock();
662 headlessif->iff_channel_connected = FALSE;
663 headless_unlock();
664 }
665
666 static void
headless_nx_disconnected(kern_nexus_provider_t nxprov,kern_nexus_t nexus,kern_channel_t channel)667 headless_nx_disconnected(kern_nexus_provider_t nxprov,
668 kern_nexus_t nexus, kern_channel_t channel)
669 {
670 #pragma unused(nxprov, channel)
671 if_headless_ref headlessif;
672
673 headlessif = headless_nexus_context(nexus);
674 HEADLESS_DPRINTF("%s: disconnected channel %p\n",
675 headlessif->iff_name, channel);
676 headless_release(headlessif);
677 }
678
679 static errno_t
headless_nx_slot_init(kern_nexus_provider_t nxprov,kern_nexus_t nexus,kern_channel_ring_t ring,kern_channel_slot_t slot,uint32_t slot_index,struct kern_slot_prop ** slot_prop_addr,void ** slot_context)680 headless_nx_slot_init(kern_nexus_provider_t nxprov,
681 kern_nexus_t nexus, kern_channel_ring_t ring, kern_channel_slot_t slot,
682 uint32_t slot_index, struct kern_slot_prop **slot_prop_addr,
683 void **slot_context)
684 {
685 #pragma unused(nxprov, nexus, ring, slot, slot_index, slot_prop_addr, slot_context)
686 return 0;
687 }
688
689 static void
headless_nx_slot_fini(kern_nexus_provider_t nxprov,kern_nexus_t nexus,kern_channel_ring_t ring,kern_channel_slot_t slot,uint32_t slot_index)690 headless_nx_slot_fini(kern_nexus_provider_t nxprov,
691 kern_nexus_t nexus, kern_channel_ring_t ring, kern_channel_slot_t slot,
692 uint32_t slot_index)
693 {
694 #pragma unused(nxprov, nexus, ring, slot, slot_index)
695 }
696
697 static errno_t
headless_nx_sync_tx(kern_nexus_provider_t nxprov,kern_nexus_t nexus,kern_channel_ring_t tx_ring,uint32_t flags)698 headless_nx_sync_tx(kern_nexus_provider_t nxprov,
699 kern_nexus_t nexus, kern_channel_ring_t tx_ring, uint32_t flags)
700 {
701 #pragma unused(nxprov)
702 if_headless_ref headlessif;
703 ifnet_t ifp;
704 kern_channel_slot_t last_tx_slot = NULL;
705 struct kern_channel_ring_stat_increment stats = {
706 .kcrsi_slots_transferred = 0, .kcrsi_bytes_transferred = 0
707 };
708 kern_channel_slot_t tx_slot;
709 struct netif_stats *nifs = &NX_NETIF_PRIVATE(nexus)->nif_stats;
710
711 STATS_INC(nifs, NETIF_STATS_TX_SYNC);
712 headlessif = headless_nexus_context(nexus);
713 HEADLESS_DPRINTF("%s ring %d flags 0x%x\n", headlessif->iff_name,
714 tx_ring->ckr_ring_id, flags);
715
716 headless_lock();
717 if (headless_is_detaching(headlessif) ||
718 !headlessif->iff_channel_connected) {
719 headless_unlock();
720 return 0;
721 }
722 headless_unlock();
723 ifp = headlessif->iff_ifp;
724 tx_slot = kern_channel_get_next_slot(tx_ring, NULL, NULL);
725 while (tx_slot != NULL) {
726 kern_packet_t ph;
727
728 /* detach the packet from the TX ring */
729 ph = kern_channel_slot_get_packet(tx_ring, tx_slot);
730 assert(ph != 0);
731 kern_channel_slot_detach_packet(tx_ring, tx_slot, ph);
732
733 kern_pbufpool_free(headlessif->iff_fpp->fpp_pp, ph);
734 last_tx_slot = tx_slot;
735 tx_slot = kern_channel_get_next_slot(tx_ring, tx_slot, NULL);
736 STATS_INC(nifs, NETIF_STATS_TX_PACKETS);
737 }
738
739 if (last_tx_slot != NULL) {
740 kern_channel_advance_slot(tx_ring, last_tx_slot);
741 kern_channel_increment_ring_net_stats(tx_ring, ifp, &stats);
742 }
743 return 0;
744 }
745
746 static errno_t
headless_nx_sync_rx_null(kern_nexus_provider_t nxprov,kern_nexus_t nexus,kern_channel_ring_t rx_ring,uint32_t flags)747 headless_nx_sync_rx_null(kern_nexus_provider_t nxprov,
748 kern_nexus_t nexus, kern_channel_ring_t rx_ring, uint32_t flags)
749 {
750 #pragma unused(nxprov, rx_ring, flags)
751 if_headless_ref headlessif;
752 struct netif_stats *nifs = &NX_NETIF_PRIVATE(nexus)->nif_stats;
753
754 headlessif = headless_nexus_context(nexus);
755 HEADLESS_DPRINTF("%s:\n", headlessif->iff_name);
756 STATS_INC(nifs, NETIF_STATS_RX_SYNC);
757 return 0;
758 }
759
760 static errno_t
headless_nx_sync_rx(kern_nexus_provider_t nxprov,kern_nexus_t nexus,kern_channel_ring_t rx_ring,uint32_t flags)761 headless_nx_sync_rx(kern_nexus_provider_t nxprov,
762 kern_nexus_t nexus, kern_channel_ring_t rx_ring, uint32_t flags)
763 {
764 #pragma unused(nxprov)
765 if_headless_ref headlessif;
766 ifnet_t ifp;
767 kern_channel_slot_t last_rx_slot = NULL;
768 struct kern_channel_ring_stat_increment stats = {
769 .kcrsi_slots_transferred = 0, .kcrsi_bytes_transferred = 0
770 };
771 kern_channel_slot_t rx_slot;
772 struct netif_stats *nifs = &NX_NETIF_PRIVATE(nexus)->nif_stats;
773
774 kern_channel_reclaim(rx_ring);
775 STATS_INC(nifs, NETIF_STATS_RX_SYNC);
776 headlessif = headless_nexus_context(nexus);
777 HEADLESS_DPRINTF("%s ring %d flags 0x%x\n", headlessif->iff_name,
778 rx_ring->ckr_ring_id, flags);
779
780 headless_lock();
781 if (headless_is_detaching(headlessif) ||
782 !headlessif->iff_channel_connected) {
783 headless_unlock();
784 return 0;
785 }
786 headless_unlock();
787 ifp = headlessif->iff_ifp;
788 rx_slot = kern_channel_get_next_slot(rx_ring, NULL, NULL);
789 kern_pbufpool_t pp = headlessif->iff_fpp->fpp_pp;
790 while (rx_slot != NULL) {
791 kern_packet_t ph;
792 kern_buflet_t buf = NULL;
793 int err;
794 err = kern_pbufpool_alloc(pp, 1, &ph);
795 buf = kern_packet_get_next_buflet(ph, buf);
796 kern_buflet_set_data_offset(buf, 0);
797 if (if_headless_create_payload) {
798 // This is a plain TCP SYN packet
799 void *addr = kern_buflet_get_data_address(buf);
800 uint64_t *u64 = addr;
801 *(u64 + 0) = 0xc100d51dc3355b68ULL;
802 *(u64 + 1) = 0x004500084019c564ULL;
803 *(u64 + 2) = 0x0634004000004000ULL;
804 *(u64 + 3) = 0x716111e3068d11c0ULL;
805 *(u64 + 4) = 0xc0118d06e3116171ULL;
806 *(u64 + 5) = 0x8a3700000000b002ULL;
807 *(u64 + 6) = 0x02b000000000378aULL;
808 *(u64 + 7) = 0x010106030301b405ULL;
809 *(u64 + 8) = 0x000022cc5c940a08ULL;
810 *(u64 + 9) = 0x0000040200000000ULL;
811 }
812 kern_buflet_set_data_length(buf, (uint16_t)if_headless_packet_length);
813 err = kern_packet_set_headroom(ph, 0);
814 ASSERT(err == 0);
815 err = kern_packet_set_link_header_length(ph, 14);
816 ASSERT(err == 0);
817 kern_packet_finalize(ph);
818
819 kern_channel_slot_attach_packet(rx_ring, rx_slot, ph);
820
821 STATS_INC(nifs, NETIF_STATS_RX_PACKETS);
822 last_rx_slot = rx_slot;
823 rx_slot = kern_channel_get_next_slot(rx_ring, rx_slot, NULL);
824 }
825
826 if (last_rx_slot != NULL) {
827 kern_channel_advance_slot(rx_ring, last_rx_slot);
828 kern_channel_increment_ring_net_stats(rx_ring, ifp, &stats);
829 }
830 return 0;
831 }
832
833 static void
headless_async_doorbell(thread_call_param_t arg0,thread_call_param_t arg1)834 headless_async_doorbell(thread_call_param_t arg0, thread_call_param_t arg1)
835 {
836 #pragma unused(arg1)
837 errno_t error;
838 if_headless_ref headlessif = (if_headless_ref)arg0;
839 kern_channel_ring_t ring;
840 boolean_t more;
841
842 headless_lock();
843 ring = headlessif->iff_tx_ring[0];
844 if (headless_is_detaching(headlessif) ||
845 !headlessif->iff_channel_connected ||
846 ring == NULL) {
847 goto done;
848 }
849 headlessif->iff_tcall_active = TRUE;
850 headless_unlock();
851 error = kern_channel_tx_refill(ring, UINT32_MAX,
852 UINT32_MAX, FALSE, &more);
853 if (error != 0) {
854 HEADLESS_DPRINTF("%s: TX refill failed %d\n",
855 headlessif->iff_name, error);
856 } else {
857 HEADLESS_DPRINTF("%s: TX refilled\n", headlessif->iff_name);
858 }
859
860 headless_lock();
861 done:
862 headlessif->iff_tcall_active = FALSE;
863 if (headlessif->iff_waiting_for_tcall) {
864 HEADLESS_DPRINTF("%s: threadcall waking up waiter\n",
865 headlessif->iff_name);
866 wakeup((caddr_t)headlessif);
867 }
868 headless_unlock();
869 }
870
871 static void
headless_schedule_async_doorbell(if_headless_ref headlessif)872 headless_schedule_async_doorbell(if_headless_ref headlessif)
873 {
874 thread_call_t tcall;
875
876 headless_lock();
877 if (headless_is_detaching(headlessif) ||
878 !headlessif->iff_channel_connected) {
879 headless_unlock();
880 return;
881 }
882 tcall = headlessif->iff_doorbell_tcall;
883 if (tcall != NULL) {
884 thread_call_enter(tcall);
885 } else {
886 tcall = thread_call_allocate_with_options(headless_async_doorbell,
887 (thread_call_param_t)headlessif,
888 THREAD_CALL_PRIORITY_KERNEL,
889 THREAD_CALL_OPTIONS_ONCE);
890 if (tcall == NULL) {
891 printf("%s: %s tcall alloc failed\n",
892 __func__, headlessif->iff_name);
893 } else {
894 headlessif->iff_doorbell_tcall = tcall;
895 headless_retain(headlessif);
896 thread_call_enter(tcall);
897 }
898 }
899 headless_unlock();
900 }
901
902 static errno_t
headless_nx_tx_doorbell(kern_nexus_provider_t nxprov,kern_nexus_t nexus,kern_channel_ring_t ring,uint32_t flags)903 headless_nx_tx_doorbell(kern_nexus_provider_t nxprov,
904 kern_nexus_t nexus, kern_channel_ring_t ring, uint32_t flags)
905 {
906 #pragma unused(nxprov, ring, flags)
907 errno_t error;
908 if_headless_ref headlessif;
909
910 headlessif = headless_nexus_context(nexus);
911 HEADLESS_DPRINTF("%s\n", headlessif->iff_name);
912
913 if ((flags & KERN_NEXUS_TXDOORBELLF_ASYNC_REFILL) == 0) {
914 boolean_t more;
915 /* synchronous tx refill */
916 error = kern_channel_tx_refill(ring, UINT32_MAX,
917 UINT32_MAX, TRUE, &more);
918 if (error != 0) {
919 HEADLESS_DPRINTF("%s: TX refill (sync) %d\n",
920 headlessif->iff_name, error);
921 } else {
922 HEADLESS_DPRINTF("%s: TX refilled (sync)\n",
923 headlessif->iff_name);
924 }
925 } else {
926 HEADLESS_DPRINTF("%s: schedule async refill\n",
927 headlessif->iff_name);
928 headless_schedule_async_doorbell(headlessif);
929 }
930 return 0;
931 }
932
933 static errno_t
headless_netif_prepare(kern_nexus_t nexus,ifnet_t ifp)934 headless_netif_prepare(kern_nexus_t nexus, ifnet_t ifp)
935 {
936 if_headless_ref headlessif;
937
938 headlessif = (if_headless_ref)kern_nexus_get_context(nexus);
939 headless_ifnet_set_attrs(headlessif, ifp);
940 return 0;
941 }
942
943 static errno_t
create_netif_provider_and_instance(if_headless_ref headlessif,struct ifnet_init_eparams * init_params,ifnet_t * ifp,uuid_t * provider,uuid_t * instance)944 create_netif_provider_and_instance(if_headless_ref headlessif,
945 struct ifnet_init_eparams * init_params, ifnet_t *ifp,
946 uuid_t * provider, uuid_t * instance)
947 {
948 errno_t err;
949 nexus_controller_t controller = kern_nexus_shared_controller();
950 struct kern_nexus_net_init net_init;
951 nexus_name_t provider_name;
952 nexus_attr_t nexus_attr = NULL;
953 struct kern_nexus_provider_init prov_init = {
954 .nxpi_version = KERN_NEXUS_DOMAIN_PROVIDER_CURRENT_VERSION,
955 .nxpi_flags = NXPIF_VIRTUAL_DEVICE,
956 .nxpi_pre_connect = headless_nx_pre_connect,
957 .nxpi_connected = headless_nx_connected,
958 .nxpi_pre_disconnect = headless_nx_pre_disconnect,
959 .nxpi_disconnected = headless_nx_disconnected,
960 .nxpi_ring_init = headless_nx_ring_init,
961 .nxpi_ring_fini = headless_nx_ring_fini,
962 .nxpi_slot_init = headless_nx_slot_init,
963 .nxpi_slot_fini = headless_nx_slot_fini,
964 .nxpi_sync_tx = headless_nx_sync_tx,
965 .nxpi_sync_rx = headless_nx_sync_rx,
966 .nxpi_tx_doorbell = headless_nx_tx_doorbell,
967 };
968
969 if (headlessif->iff_cloner == &headless_zero_cloner) {
970 prov_init.nxpi_sync_rx = headless_nx_sync_rx;
971 prov_init.nxpi_sync_tx = headless_nx_sync_tx;
972 } else if (headlessif->iff_cloner == &headless_null_cloner) {
973 prov_init.nxpi_sync_rx = headless_nx_sync_rx_null;
974 prov_init.nxpi_sync_tx = headless_nx_sync_tx;
975 }
976
977 _CASSERT(IFF_MAX_RX_RINGS == 1);
978
979 snprintf((char *)provider_name, sizeof(provider_name),
980 "com.apple.netif.%s", headlessif->iff_name);
981 err = kern_nexus_controller_register_provider(controller,
982 headless_nx_dom_prov,
983 provider_name,
984 &prov_init,
985 sizeof(prov_init),
986 nexus_attr,
987 provider);
988 if (err != 0) {
989 printf("%s register provider failed, error %d\n",
990 __func__, err);
991 goto failed;
992 }
993 bzero(&net_init, sizeof(net_init));
994 net_init.nxneti_version = KERN_NEXUS_NET_CURRENT_VERSION;
995 net_init.nxneti_flags = 0;
996 net_init.nxneti_eparams = init_params;
997 net_init.nxneti_lladdr = NULL;
998 net_init.nxneti_prepare = headless_netif_prepare;
999 net_init.nxneti_rx_pbufpool = headlessif->iff_fpp->fpp_pp;
1000 net_init.nxneti_tx_pbufpool = headlessif->iff_fpp->fpp_pp;
1001 err = kern_nexus_controller_alloc_net_provider_instance(controller,
1002 *provider,
1003 headlessif,
1004 NULL,
1005 instance,
1006 &net_init,
1007 ifp);
1008 if (err != 0) {
1009 printf("%s alloc_net_provider_instance failed, %d\n",
1010 __func__, err);
1011 kern_nexus_controller_deregister_provider(controller,
1012 *provider);
1013 uuid_clear(*provider);
1014 goto failed;
1015 }
1016
1017 failed:
1018 if (nexus_attr != NULL) {
1019 kern_nexus_attr_destroy(nexus_attr);
1020 }
1021 return err;
1022 }
1023
1024
1025 static errno_t
headless_attach_netif_nexus(if_headless_ref headlessif,struct ifnet_init_eparams * init_params,ifnet_t * ifp)1026 headless_attach_netif_nexus(if_headless_ref headlessif,
1027 struct ifnet_init_eparams * init_params, ifnet_t *ifp)
1028 {
1029 headless_packet_pool_t fpp;
1030 headless_nx_t nx = &headlessif->iff_nx;
1031 boolean_t multi_buflet;
1032
1033 multi_buflet = headless_using_multibuflets(headlessif);
1034 fpp = headless_packet_pool_alloc(multi_buflet, headlessif->iff_max_mtu);
1035 if (fpp == NULL) {
1036 return ENOMEM;
1037 }
1038 headlessif->iff_fpp = fpp;
1039 return create_netif_provider_and_instance(headlessif, init_params, ifp,
1040 &nx->fnx_provider,
1041 &nx->fnx_instance);
1042 }
1043
1044 static void
detach_provider_and_instance(uuid_t provider,uuid_t instance)1045 detach_provider_and_instance(uuid_t provider, uuid_t instance)
1046 {
1047 nexus_controller_t controller = kern_nexus_shared_controller();
1048 errno_t err;
1049
1050 if (!uuid_is_null(instance)) {
1051 err = kern_nexus_controller_free_provider_instance(controller,
1052 instance);
1053 if (err != 0) {
1054 printf("%s free_provider_instance failed %d\n",
1055 __func__, err);
1056 }
1057 uuid_clear(instance);
1058 }
1059 if (!uuid_is_null(provider)) {
1060 err = kern_nexus_controller_deregister_provider(controller,
1061 provider);
1062 if (err != 0) {
1063 printf("%s deregister_provider %d\n", __func__, err);
1064 }
1065 uuid_clear(provider);
1066 }
1067 return;
1068 }
1069
1070 static void
headless_detach_netif_nexus(headless_nx_t nx)1071 headless_detach_netif_nexus(headless_nx_t nx)
1072 {
1073 detach_provider_and_instance(nx->fnx_provider, nx->fnx_instance);
1074 }
1075
1076 /**
1077 ** headless interface routines
1078 **/
1079 static void
headless_ifnet_set_attrs(if_headless_ref headlessif,ifnet_t ifp)1080 headless_ifnet_set_attrs(if_headless_ref headlessif, ifnet_t ifp)
1081 {
1082 (void)ifnet_set_capabilities_enabled(ifp, 0, -1);
1083 ifnet_set_addrlen(ifp, ETHER_ADDR_LEN);
1084 ifnet_set_baudrate(ifp, 0);
1085 ifnet_set_mtu(ifp, ETHERMTU);
1086 ifnet_set_flags(ifp,
1087 IFF_BROADCAST | IFF_MULTICAST | IFF_SIMPLEX,
1088 0xffff);
1089 ifnet_set_hdrlen(ifp, sizeof(struct ether_header));
1090 if ((headlessif->iff_flags & IFF_FLAGS_HWCSUM) != 0) {
1091 ifnet_set_offload(ifp,
1092 IFNET_CSUM_IP | IFNET_CSUM_TCP | IFNET_CSUM_UDP |
1093 IFNET_CSUM_TCPIPV6 | IFNET_CSUM_UDPIPV6);
1094 } else {
1095 ifnet_set_offload(ifp, 0);
1096 }
1097 }
1098
1099 static void
interface_link_event(ifnet_t ifp,u_int32_t event_code)1100 interface_link_event(ifnet_t ifp, u_int32_t event_code)
1101 {
1102 struct event {
1103 u_int32_t ifnet_family;
1104 u_int32_t unit;
1105 char if_name[IFNAMSIZ];
1106 };
1107 _Alignas(struct kern_event_msg) char message[sizeof(struct kern_event_msg) + sizeof(struct event)] = { 0 };
1108 struct kern_event_msg *header = (struct kern_event_msg*)message;
1109 struct event *data = (struct event *)(header + 1);
1110
1111 header->total_size = sizeof(message);
1112 header->vendor_code = KEV_VENDOR_APPLE;
1113 header->kev_class = KEV_NETWORK_CLASS;
1114 header->kev_subclass = KEV_DL_SUBCLASS;
1115 header->event_code = event_code;
1116 data->ifnet_family = ifnet_family(ifp);
1117 data->unit = (u_int32_t)ifnet_unit(ifp);
1118 strlcpy(data->if_name, ifnet_name(ifp), IFNAMSIZ);
1119 ifnet_event(ifp, header);
1120 }
1121
1122 static if_headless_ref
ifnet_get_if_headless(ifnet_t ifp)1123 ifnet_get_if_headless(ifnet_t ifp)
1124 {
1125 return (if_headless_ref)ifnet_softc(ifp);
1126 }
1127
1128 static int
headless_clone_create(struct if_clone * ifc,u_int32_t unit,void * params)1129 headless_clone_create(struct if_clone *ifc, u_int32_t unit, void *params)
1130 {
1131 #pragma unused(params)
1132 int error;
1133 if_headless_ref headlessif;
1134 struct ifnet_init_eparams headless_init;
1135 ifnet_t ifp;
1136 uint8_t mac_address[ETHER_ADDR_LEN];
1137
1138 headlessif = if_clone_softc_allocate(ifc);
1139 if (headlessif == NULL) {
1140 return ENOBUFS;
1141 }
1142 headlessif->iff_retain_count = 1;
1143 if (strcmp(ifc->ifc_name, HEADLESS_ZERO_IFNAME) == 0) {
1144 headlessif->iff_cloner = &headless_zero_cloner;
1145 ASSERT(strlen(HEADLESS_ZERO_IFNAME) == 4);
1146 bcopy(HEADLESS_ZERO_IFNAME, mac_address, 4);
1147 } else {
1148 headlessif->iff_cloner = &headless_null_cloner;
1149 ASSERT(strlen(HEADLESS_NULL_IFNAME) == 4);
1150 bcopy(HEADLESS_NULL_IFNAME, mac_address, 4);
1151 }
1152 mac_address[ETHER_ADDR_LEN - 2] = (unit & 0xff00) >> 8;
1153 mac_address[ETHER_ADDR_LEN - 1] = unit & 0xff;
1154 headlessif->iff_max_mtu = if_headless_max_mtu;
1155
1156 /* use the interface name as the unique id for ifp recycle */
1157 if ((unsigned int)
1158 snprintf(headlessif->iff_name, sizeof(headlessif->iff_name), "%s%d",
1159 ifc->ifc_name, unit) >= sizeof(headlessif->iff_name)) {
1160 headless_release(headlessif);
1161 return EINVAL;
1162 }
1163 bzero(&headless_init, sizeof(headless_init));
1164 headless_init.ver = IFNET_INIT_CURRENT_VERSION;
1165 headless_init.len = sizeof(headless_init);
1166 headless_init.flags |= IFNET_INIT_SKYWALK_NATIVE;
1167 if (if_headless_multibuflet != 0) {
1168 headlessif->iff_flags |= IFF_FLAGS_MULTIBUFLETS;
1169 }
1170
1171 headlessif->iff_tx_headroom = if_headless_tx_headroom;
1172 headless_init.tx_headroom = headlessif->iff_tx_headroom;
1173 if (if_headless_nxattach == 0) {
1174 headless_init.flags |= IFNET_INIT_NX_NOAUTO;
1175 }
1176 headless_init.uniqueid = headlessif->iff_name;
1177 headless_init.uniqueid_len = (uint32_t)strlen(headlessif->iff_name);
1178 headless_init.name = ifc->ifc_name;
1179 headless_init.unit = unit;
1180 headless_init.family = IFNET_FAMILY_ETHERNET;
1181 headless_init.type = IFT_ETHER;
1182 headless_init.demux = ether_demux;
1183 headless_init.add_proto = ether_add_proto;
1184 headless_init.del_proto = ether_del_proto;
1185 headless_init.check_multi = ether_check_multi;
1186 headless_init.framer_extended = ether_frameout_extended;
1187 headless_init.softc = headlessif;
1188 headless_init.ioctl = headless_ioctl;
1189 headless_init.set_bpf_tap = NULL;
1190 headless_init.detach = headless_if_free;
1191 headless_init.broadcast_addr = etherbroadcastaddr;
1192 headless_init.broadcast_len = ETHER_ADDR_LEN;
1193 error = headless_attach_netif_nexus(headlessif, &headless_init, &ifp);
1194 if (error != 0) {
1195 headless_release(headlessif);
1196 return error;
1197 }
1198 /* take an additional reference to ensure that it doesn't go away */
1199 headless_retain(headlessif);
1200 headlessif->iff_ifp = ifp;
1201 headlessif->iff_media_count = default_media_words_count;
1202 bcopy(default_media_words, headlessif->iff_media_list,
1203 sizeof(default_media_words));
1204 ifnet_set_lladdr(ifp, mac_address, sizeof(mac_address));
1205
1206 /* attach as ethernet */
1207 bpfattach(ifp, DLT_EN10MB, sizeof(struct ether_header));
1208
1209 interface_link_event(ifp, KEV_DL_LINK_ON);
1210
1211 return 0;
1212 }
1213
1214 static int
headless_clone_destroy(ifnet_t ifp)1215 headless_clone_destroy(ifnet_t ifp)
1216 {
1217 if_headless_ref headlessif;
1218 headless_nx nx;
1219 boolean_t nx_attached = FALSE;
1220
1221 interface_link_event(ifp, KEV_DL_LINK_OFF);
1222 headless_lock();
1223 headlessif = ifnet_get_if_headless(ifp);
1224 if (headlessif == NULL || headless_is_detaching(headlessif)) {
1225 headless_unlock();
1226 return 0;
1227 }
1228 headless_set_detaching(headlessif);
1229 nx_attached = TRUE;
1230 nx = headlessif->iff_nx;
1231 bzero(&headlessif->iff_nx, sizeof(headlessif->iff_nx));
1232 headless_unlock();
1233
1234 if (nx_attached) {
1235 headless_detach_netif_nexus(&nx);
1236 headless_release(headlessif);
1237 }
1238 ifnet_detach(ifp);
1239 return 0;
1240 }
1241
1242 static int
headless_set_media(ifnet_t ifp,struct if_headless_request * iffr)1243 headless_set_media(ifnet_t ifp, struct if_headless_request * iffr)
1244 {
1245 if_headless_ref headlessif;
1246 int error;
1247
1248 if (iffr->iffr_media.iffm_count > IF_HEADLESS_MEDIA_LIST_MAX) {
1249 /* list is too long */
1250 return EINVAL;
1251 }
1252 headless_lock();
1253 headlessif = ifnet_get_if_headless(ifp);
1254 if (headlessif == NULL) {
1255 error = EINVAL;
1256 goto done;
1257 }
1258 headlessif->iff_media_count = iffr->iffr_media.iffm_count;
1259 bcopy(iffr->iffr_media.iffm_list, headlessif->iff_media_list,
1260 iffr->iffr_media.iffm_count * sizeof(headlessif->iff_media_list[0]));
1261 #if 0
1262 /* XXX: "auto-negotiate" active with peer? */
1263 /* generate link status event? */
1264 headlessif->iff_media_current = iffr->iffr_media.iffm_current;
1265 #endif
1266 error = 0;
1267 done:
1268 headless_unlock();
1269 return error;
1270 }
1271
1272 static int
if_headless_request_copyin(user_addr_t user_addr,struct if_headless_request * iffr,size_t len)1273 if_headless_request_copyin(user_addr_t user_addr,
1274 struct if_headless_request *iffr, size_t len)
1275 {
1276 int error;
1277
1278 if (user_addr == USER_ADDR_NULL || len < sizeof(*iffr)) {
1279 error = EINVAL;
1280 goto done;
1281 }
1282 error = copyin(user_addr, iffr, sizeof(*iffr));
1283 if (error != 0) {
1284 goto done;
1285 }
1286 if (iffr->iffr_reserved[0] != 0 || iffr->iffr_reserved[1] != 0 ||
1287 iffr->iffr_reserved[2] != 0 || iffr->iffr_reserved[3] != 0) {
1288 error = EINVAL;
1289 goto done;
1290 }
1291 done:
1292 return error;
1293 }
1294
1295 static int
headless_set_drvspec(ifnet_t ifp,uint64_t cmd,size_t len,user_addr_t user_addr)1296 headless_set_drvspec(ifnet_t ifp, uint64_t cmd, size_t len,
1297 user_addr_t user_addr)
1298 {
1299 int error;
1300 struct if_headless_request iffr;
1301
1302 switch (cmd) {
1303 case IF_HEADLESS_S_CMD_SET_MEDIA:
1304 error = if_headless_request_copyin(user_addr, &iffr, len);
1305 if (error != 0) {
1306 break;
1307 }
1308 error = headless_set_media(ifp, &iffr);
1309 break;
1310 default:
1311 error = EOPNOTSUPP;
1312 break;
1313 }
1314 return error;
1315 }
1316
1317 static int
headless_get_drvspec(ifnet_t ifp,uint64_t cmd,size_t len,user_addr_t user_addr)1318 headless_get_drvspec(ifnet_t ifp, uint64_t cmd, size_t len,
1319 user_addr_t user_addr)
1320 {
1321 #pragma unused(ifp, len, user_addr)
1322 int error = EOPNOTSUPP;
1323
1324 switch (cmd) {
1325 default:
1326 break;
1327 }
1328 return error;
1329 }
1330
1331 union ifdrvu {
1332 struct ifdrv32 *ifdrvu_32;
1333 struct ifdrv64 *ifdrvu_64;
1334 void *ifdrvu_p;
1335 };
1336
1337 static int
headless_ioctl(ifnet_t ifp,u_long cmd,void * data)1338 headless_ioctl(ifnet_t ifp, u_long cmd, void * data)
1339 {
1340 unsigned int count;
1341 struct ifdevmtu * devmtu_p;
1342 union ifdrvu drv;
1343 uint64_t drv_cmd;
1344 uint64_t drv_len;
1345 boolean_t drv_set_command = FALSE;
1346 int error = 0;
1347 struct ifmediareq * ifmr;
1348 struct ifreq * ifr;
1349 if_headless_ref headlessif;
1350 int status;
1351 user_addr_t user_addr;
1352
1353 ifr = (struct ifreq *)data;
1354 switch (cmd) {
1355 case SIOCSIFADDR:
1356 ifnet_set_flags(ifp, IFF_UP, IFF_UP);
1357 break;
1358
1359 case SIOCGIFMEDIA32:
1360 case SIOCGIFMEDIA64:
1361 headless_lock();
1362 headlessif = ifnet_get_if_headless(ifp);
1363 if (headlessif == NULL) {
1364 headless_unlock();
1365 return EOPNOTSUPP;
1366 }
1367 status = (headlessif->iff_peer != NULL)
1368 ? (IFM_AVALID | IFM_ACTIVE) : IFM_AVALID;
1369 ifmr = (struct ifmediareq *)data;
1370 user_addr = (cmd == SIOCGIFMEDIA64) ?
1371 CAST_USER_ADDR_T(((struct ifmediareq64 *)ifmr)->ifmu_ulist) :
1372 CAST_USER_ADDR_T(((struct ifmediareq32 *)ifmr)->ifmu_ulist);
1373 count = ifmr->ifm_count;
1374 ifmr->ifm_active = IFM_ETHER;
1375 ifmr->ifm_current = IFM_ETHER;
1376 ifmr->ifm_mask = 0;
1377 ifmr->ifm_status = status;
1378 if (user_addr == USER_ADDR_NULL) {
1379 ifmr->ifm_count = headlessif->iff_media_count;
1380 } else if (count > 0) {
1381 if (count > headlessif->iff_media_count) {
1382 count = headlessif->iff_media_count;
1383 }
1384 ifmr->ifm_count = count;
1385 error = copyout(&headlessif->iff_media_list, user_addr,
1386 count * sizeof(int));
1387 }
1388 headless_unlock();
1389 break;
1390
1391 case SIOCGIFDEVMTU:
1392 devmtu_p = &ifr->ifr_devmtu;
1393 devmtu_p->ifdm_current = ifnet_mtu(ifp);
1394 devmtu_p->ifdm_max = headless_max_mtu(ifp);
1395 devmtu_p->ifdm_min = IF_MINMTU;
1396 break;
1397
1398 case SIOCSIFMTU:
1399 if ((unsigned int)ifr->ifr_mtu > headless_max_mtu(ifp) ||
1400 ifr->ifr_mtu < IF_MINMTU) {
1401 error = EINVAL;
1402 } else {
1403 error = ifnet_set_mtu(ifp, ifr->ifr_mtu);
1404 }
1405 break;
1406
1407 case SIOCSDRVSPEC32:
1408 case SIOCSDRVSPEC64:
1409 error = proc_suser(current_proc());
1410 if (error != 0) {
1411 break;
1412 }
1413 drv_set_command = TRUE;
1414 OS_FALLTHROUGH;
1415 case SIOCGDRVSPEC32:
1416 case SIOCGDRVSPEC64:
1417 drv.ifdrvu_p = data;
1418 if (cmd == SIOCGDRVSPEC32 || cmd == SIOCSDRVSPEC32) {
1419 drv_cmd = drv.ifdrvu_32->ifd_cmd;
1420 drv_len = drv.ifdrvu_32->ifd_len;
1421 user_addr = CAST_USER_ADDR_T(drv.ifdrvu_32->ifd_data);
1422 } else {
1423 drv_cmd = drv.ifdrvu_64->ifd_cmd;
1424 drv_len = drv.ifdrvu_64->ifd_len;
1425 user_addr = CAST_USER_ADDR_T(drv.ifdrvu_64->ifd_data);
1426 }
1427 if (drv_set_command) {
1428 error = headless_set_drvspec(ifp, drv_cmd,
1429 (size_t)drv_len, user_addr);
1430 } else {
1431 error = headless_get_drvspec(ifp, drv_cmd,
1432 (size_t)drv_len, user_addr);
1433 }
1434 break;
1435
1436 case SIOCSIFLLADDR:
1437 error = ifnet_set_lladdr(ifp, ifr->ifr_addr.sa_data,
1438 ifr->ifr_addr.sa_len);
1439 break;
1440
1441 case SIOCSIFFLAGS:
1442 if ((ifp->if_flags & IFF_UP) != 0) {
1443 /* marked up, set running if not already set */
1444 if ((ifp->if_flags & IFF_RUNNING) == 0) {
1445 /* set running */
1446 error = ifnet_set_flags(ifp, IFF_RUNNING,
1447 IFF_RUNNING);
1448 }
1449 } else if ((ifp->if_flags & IFF_RUNNING) != 0) {
1450 /* marked down, clear running */
1451 error = ifnet_set_flags(ifp, 0, IFF_RUNNING);
1452 }
1453 break;
1454
1455 case SIOCADDMULTI:
1456 case SIOCDELMULTI:
1457 error = 0;
1458 break;
1459 default:
1460 error = EOPNOTSUPP;
1461 break;
1462 }
1463 return error;
1464 }
1465
1466 static void
headless_if_free(ifnet_t ifp)1467 headless_if_free(ifnet_t ifp)
1468 {
1469 if_headless_ref headlessif;
1470
1471 if (ifp == NULL) {
1472 return;
1473 }
1474 headless_lock();
1475 headlessif = ifnet_get_if_headless(ifp);
1476 if (headlessif == NULL) {
1477 headless_unlock();
1478 return;
1479 }
1480 ifp->if_softc = NULL;
1481 assert(headlessif->iff_doorbell_tcall == NULL);
1482 headless_unlock();
1483 headless_release(headlessif);
1484 ifnet_release(ifp);
1485 return;
1486 }
1487
1488 void
if_headless_init(void)1489 if_headless_init(void)
1490 {
1491 int error;
1492
1493 (void)headless_register_nexus_domain_provider();
1494 error = if_clone_attach(&headless_zero_cloner);
1495 if (error != 0) {
1496 return;
1497 }
1498 error = if_clone_attach(&headless_null_cloner);
1499 if (error != 0) {
1500 if_clone_detach(&headless_zero_cloner);
1501 return;
1502 }
1503 return;
1504 }
1505 #else /* !SKYWALK */
1506 extern void if_headless_init(void);
1507
1508 void
if_headless_init(void)1509 if_headless_init(void)
1510 {
1511 /* nothing here */
1512 }
1513 #endif /* SKYWALK */
1514