xref: /xnu-11215/bsd/net/ether_inet_pr_module.c (revision 8d741a5d)
1 /*
2  * Copyright (c) 2000-2022 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, 1989, 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  */
61 /*
62  * NOTICE: This file was modified by SPARTA, Inc. in 2006 to introduce
63  * support for mandatory and extensible security protections.  This notice
64  * is included in support of clause 2.2 (b) of the Apple Public License,
65  * Version 2.0.
66  */
67 
68 
69 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/kernel.h>
72 #include <sys/malloc.h>
73 #include <sys/mbuf.h>
74 #include <sys/socket.h>
75 #include <sys/sockio.h>
76 #include <sys/sysctl.h>
77 
78 #include <net/dlil.h>
79 #include <net/if.h>
80 #include <net/route.h>
81 #include <net/if_llc.h>
82 #include <net/if_dl.h>
83 #include <net/if_types.h>
84 #include <net/kpi_interface.h>
85 #include <net/kpi_protocol.h>
86 #include <netinet/in.h>
87 #include <netinet/in_var.h>
88 #include <netinet/if_ether.h>
89 #include <netinet/in_systm.h>
90 #include <netinet/ip.h>
91 #include <netinet/in_arp.h>
92 
93 #include <sys/socketvar.h>
94 
95 #include <net/dlil.h>
96 
97 /* #include "vlan.h" */
98 #if NVLAN > 0
99 #include <net/if_vlan_var.h>
100 #endif /* NVLAN > 0 */
101 #include <net/ether_if_module.h>
102 #if CONFIG_MACF
103 #include <security/mac_framework.h>
104 #endif
105 
106 #include <net/sockaddr_utils.h>
107 
108 /* Local function declarations */
109 extern void *kdp_get_interface(void);
110 extern void kdp_set_ip_and_mac_addresses(struct in_addr *ipaddr,
111     struct ether_addr *macaddr);
112 
113 #define _ip_copy(dst, src)      \
114 	bcopy(src, dst, sizeof (struct in_addr))
115 
116 static void
ether_inet_arp_input(struct ifnet * ifp,struct mbuf * m)117 ether_inet_arp_input(struct ifnet *ifp, struct mbuf *m)
118 {
119 	struct ether_arp *ea;
120 	struct sockaddr_dl      sender_hw;
121 	struct sockaddr_in      sender_ip;
122 	struct sockaddr_in      target_ip;
123 
124 	if (mbuf_len(m) < sizeof(*ea) && mbuf_pullup(&m, sizeof(*ea)) != 0) {
125 		return;
126 	}
127 
128 	ea = mtod(m, struct ether_arp *);
129 
130 	/* Verify this is an ethernet/ip arp and address lengths are correct */
131 	if (ntohs(ea->arp_hrd) != ARPHRD_ETHER ||
132 	    ntohs(ea->arp_pro) != ETHERTYPE_IP ||
133 	    ea->arp_pln != sizeof(struct in_addr) ||
134 	    ea->arp_hln != ETHER_ADDR_LEN) {
135 		mbuf_freem(m);
136 		return;
137 	}
138 
139 	/* Verify the sender is not broadcast */
140 	if (bcmp(ea->arp_sha, etherbroadcastaddr, ETHER_ADDR_LEN) == 0) {
141 		mbuf_freem(m);
142 		return;
143 	}
144 
145 	SOCKADDR_ZERO(&sender_ip, sizeof(sender_ip));
146 	sender_ip.sin_len = sizeof(sender_ip);
147 	sender_ip.sin_family = AF_INET;
148 	_ip_copy(&sender_ip.sin_addr, ea->arp_spa);
149 	target_ip = sender_ip;
150 	_ip_copy(&target_ip.sin_addr, ea->arp_tpa);
151 
152 	SOCKADDR_ZERO(&sender_hw, sizeof(sender_hw));
153 	sender_hw.sdl_len = sizeof(sender_hw);
154 	sender_hw.sdl_family = AF_LINK;
155 	sender_hw.sdl_type = IFT_ETHER;
156 	sender_hw.sdl_alen = ETHER_ADDR_LEN;
157 	bcopy(ea->arp_sha, LLADDR(&sender_hw), ETHER_ADDR_LEN);
158 
159 	/* update L2 reachability record, if present */
160 	arp_llreach_set_reachable(ifp, LLADDR(&sender_hw), ETHER_ADDR_LEN);
161 
162 	arp_ip_handle_input(ifp, ntohs(ea->arp_op), &sender_hw, &sender_ip,
163 	    &target_ip);
164 	mbuf_freem(m);
165 }
166 
167 /*
168  * Process a received Ethernet packet;
169  * the packet is in the mbuf chain m without
170  * the ether header, which is provided separately.
171  */
172 static errno_t
ether_inet_input(ifnet_t ifp,protocol_family_t protocol_family,mbuf_t m_list)173 ether_inet_input(ifnet_t ifp, protocol_family_t protocol_family,
174     mbuf_t m_list)
175 {
176 #pragma unused(ifp, protocol_family)
177 	mbuf_t  m;
178 	mbuf_t  *tailptr = &m_list;
179 	mbuf_t  nextpkt;
180 	bool is_cache_valid = false;
181 	u_char cached_shost[ETHER_ADDR_LEN] = {};
182 
183 	/* Strip ARP and non-IP packets out of the list */
184 	for (m = m_list; m; m = nextpkt) {
185 		struct ether_header *eh __single = mbuf_pkthdr_header(m);
186 		struct ifnet *mifp;
187 
188 		/*
189 		 * Trust the ifp in the mbuf, rather than ifproto's
190 		 * since the packet could have been injected via
191 		 * a dlil_input_packet_list() using an ifp that is
192 		 * different than the one where the packet really
193 		 * came from.
194 		 */
195 		mifp = mbuf_pkthdr_rcvif(m);
196 
197 		nextpkt = m->m_nextpkt;
198 
199 		if (__probable(eh->ether_type == htons(ETHERTYPE_IP))) {
200 			/*
201 			 * Update L2 reachability record, if present
202 			 * (and if not a broadcast sender).
203 			 * Note that M_BCAST will be already set by ether_demux()
204 			 */
205 			if (__improbable((m->m_flags & M_BCAST) == 0 && (is_cache_valid == false ||
206 			    memcmp(eh->ether_shost, cached_shost, ETHER_ADDR_LEN) != 0))) {
207 				memcpy(eh->ether_shost, cached_shost, ETHER_ADDR_LEN);
208 				is_cache_valid = true;
209 
210 				arp_llreach_set_reachable(mifp, eh->ether_shost,
211 				    ETHER_ADDR_LEN);
212 			}
213 			/* put this packet in the list */
214 			*tailptr = m;
215 			tailptr = &m->m_nextpkt;
216 		} else {
217 			/* Pass ARP packets to arp input */
218 			m->m_nextpkt = NULL;
219 			if (eh->ether_type == htons(ETHERTYPE_ARP)) {
220 				ether_inet_arp_input(mifp, m);
221 			} else {
222 				mbuf_freem(m);
223 			}
224 		}
225 	}
226 
227 	*tailptr = NULL;
228 
229 	/* Pass IP list to ip input */
230 	if (m_list != NULL && proto_input(PF_INET, m_list) != 0) {
231 		mbuf_freem_list(m_list);
232 	}
233 
234 	return EJUSTRETURN;
235 }
236 
237 static errno_t
ether_inet_pre_output(ifnet_t ifp,protocol_family_t protocol_family,mbuf_t * m0,const struct sockaddr * dst_netaddr,void * route,char * type,char * edst)238 ether_inet_pre_output(ifnet_t ifp, protocol_family_t protocol_family,
239     mbuf_t *m0, const struct sockaddr *dst_netaddr,
240     void *route, char *type, char *edst)
241 {
242 #pragma unused(protocol_family)
243 	struct mbuf *m = *m0;
244 	const struct ether_header *eh;
245 	errno_t result = 0;
246 	uint8_t *frame_type = dlil_frame_type(type);
247 	uint8_t *dst_host_lladdr = dlil_link_addr(edst);
248 
249 	if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING)) {
250 		return ENETDOWN;
251 	}
252 
253 	/*
254 	 * Tell ether_frameout it's ok to loop packet unless negated below.
255 	 */
256 	m->m_flags |= M_LOOP;
257 
258 	switch (dst_netaddr->sa_family) {
259 	case AF_INET: {
260 		struct sockaddr_dl ll_dest = {};
261 
262 		result = arp_lookup_ip(ifp,
263 		    SIN(dst_netaddr),
264 		    &ll_dest, sizeof(ll_dest), (route_t)route, *m0);
265 		if (result == 0) {
266 			u_int16_t ethertype_ip = htons(ETHERTYPE_IP);
267 
268 			bcopy(LLADDR(&ll_dest), dst_host_lladdr, ETHER_ADDR_LEN);
269 			bcopy(&ethertype_ip, frame_type, sizeof(ethertype_ip));
270 		}
271 		break;
272 	}
273 
274 	case pseudo_AF_HDRCMPLT:
275 	case AF_UNSPEC:
276 		m->m_flags &= ~M_LOOP;
277 		eh = (const struct ether_header *)__DECONST(void *, dst_netaddr->sa_data);
278 		bcopy(eh->ether_dhost, dst_host_lladdr, ETHER_ADDR_LEN);
279 		bcopy(&eh->ether_type, frame_type, sizeof(eh->ether_type));
280 		break;
281 
282 	default:
283 		printf("%s: can't handle af%d\n", if_name(ifp),
284 		    dst_netaddr->sa_family);
285 
286 		result = EAFNOSUPPORT;
287 		break;
288 	}
289 
290 	return result;
291 }
292 
293 static errno_t
ether_inet_resolve_multi(ifnet_t ifp,const struct sockaddr * proto_addr,struct sockaddr_dl * out_ll,size_t ll_len)294 ether_inet_resolve_multi(ifnet_t ifp, const struct sockaddr *proto_addr,
295     struct sockaddr_dl *out_ll, size_t ll_len)
296 {
297 	static const size_t minsize =
298 	    offsetof(struct sockaddr_dl, sdl_data[0]) + ETHER_ADDR_LEN;
299 	const struct sockaddr_in *sin = SIN(proto_addr);
300 
301 	if (proto_addr->sa_family != AF_INET) {
302 		return EAFNOSUPPORT;
303 	}
304 
305 	if (proto_addr->sa_len < sizeof(struct sockaddr_in)) {
306 		return EINVAL;
307 	}
308 
309 	if (ll_len < minsize) {
310 		return EMSGSIZE;
311 	}
312 
313 	SOCKADDR_ZERO(out_ll, minsize);
314 	out_ll->sdl_len = minsize;
315 	out_ll->sdl_family = AF_LINK;
316 	out_ll->sdl_index = ifp->if_index;
317 	out_ll->sdl_type = IFT_ETHER;
318 	out_ll->sdl_nlen = 0;
319 	out_ll->sdl_alen = ETHER_ADDR_LEN;
320 	out_ll->sdl_slen = 0;
321 	ETHER_MAP_IP_MULTICAST(&sin->sin_addr, LLADDR(out_ll));
322 
323 	return 0;
324 }
325 
326 static errno_t
ether_inet_prmod_ioctl(ifnet_t ifp,protocol_family_t protocol_family,u_long command,void * data)327 ether_inet_prmod_ioctl(ifnet_t ifp, protocol_family_t protocol_family,
328     u_long command, void *data)
329 {
330 #pragma unused(protocol_family)
331 	int error = 0;
332 
333 	switch (command) {
334 	case SIOCSIFADDR:               /* struct ifaddr pointer */
335 	case SIOCAIFADDR: {             /* struct ifaddr pointer */
336 		/*
337 		 * Note: caller of ifnet_ioctl() passes in pointer to
338 		 * struct ifaddr as parameter to SIOC{A,S}IFADDR, for
339 		 * legacy reasons.
340 		 */
341 		struct ifaddr *ifa __single = data;
342 
343 		if (!(ifnet_flags(ifp) & IFF_RUNNING)) {
344 			ifnet_set_flags(ifp, IFF_UP, IFF_UP);
345 			ifnet_ioctl(ifp, 0, SIOCSIFFLAGS, NULL);
346 		}
347 
348 		if (ifaddr_address_family(ifa) != AF_INET) {
349 			break;
350 		}
351 
352 		inet_arp_init_ifaddr(ifp, ifa);
353 
354 		if (command != SIOCSIFADDR) {
355 			break;
356 		}
357 
358 		/*
359 		 * Register new IP and MAC addresses with the kernel
360 		 * debugger if the interface is the same as was registered
361 		 * by IOKernelDebugger. If no interface was registered,
362 		 * fall back and just match against en0 interface.
363 		 * Do this only for the first address of the interface
364 		 * and not for aliases.
365 		 */
366 		if ((kdp_get_interface() != 0 &&
367 		    kdp_get_interface() == ifp->if_softc) ||
368 		    (kdp_get_interface() == 0 && ifp->if_unit == 0)) {
369 			kdp_set_ip_and_mac_addresses(&(IA_SIN(ifa)->sin_addr),
370 			    (struct ether_addr *)IF_LLADDR(ifp));
371 		}
372 		break;
373 	}
374 
375 	case SIOCGIFADDR: {             /* struct ifreq */
376 		struct ifreq *ifr __single = data;
377 		ifnet_guarded_lladdr_copy_bytes(ifp, ifr->ifr_addr.sa_data,
378 		    ETHER_ADDR_LEN);
379 		break;
380 	}
381 
382 	default:
383 		error = EOPNOTSUPP;
384 		break;
385 	}
386 
387 	return error;
388 }
389 
390 static void
ether_inet_event(ifnet_t ifp,protocol_family_t protocol,const struct kev_msg * event)391 ether_inet_event(ifnet_t ifp, protocol_family_t protocol,
392     const struct kev_msg *event)
393 {
394 #pragma unused(protocol)
395 	uint16_t address_count = 0;
396 	ifaddr_ref_t * __counted_by(address_count) addresses = NULL;
397 
398 	if (event->vendor_code != KEV_VENDOR_APPLE ||
399 	    event->kev_class != KEV_NETWORK_CLASS ||
400 	    event->kev_subclass != KEV_DL_SUBCLASS ||
401 	    event->event_code != KEV_DL_LINK_ADDRESS_CHANGED) {
402 		return;
403 	}
404 
405 	if (ifnet_get_address_list_family_with_count(ifp, &addresses, &address_count, AF_INET) == 0) {
406 		int i;
407 
408 		for (i = 0; addresses[i] != NULL; i++) {
409 			inet_arp_init_ifaddr(ifp, addresses[i]);
410 		}
411 
412 		ifnet_address_list_free_counted_by(addresses, address_count);
413 	}
414 }
415 
416 static errno_t
ether_inet_arp(ifnet_t ifp,u_short arpop,const struct sockaddr_dl * sender_hw,const struct sockaddr * sender_proto,const struct sockaddr_dl * target_hw,const struct sockaddr * target_proto)417 ether_inet_arp(ifnet_t ifp, u_short arpop, const struct sockaddr_dl *sender_hw,
418     const struct sockaddr *sender_proto, const struct sockaddr_dl *target_hw,
419     const struct sockaddr *target_proto)
420 {
421 	mbuf_ref_t  m;
422 	errno_t result;
423 	struct ether_header *eh;
424 	struct ether_arp *ea;
425 	const struct sockaddr_in *sender_ip =
426 	    SIN(sender_proto);
427 	const struct sockaddr_inarp *target_ip =
428 	    __SA_UTILS_CONV_TO_SOCKADDR_INARP(target_proto);
429 	char *datap;
430 
431 	if (target_ip == NULL) {
432 		return EINVAL;
433 	}
434 
435 	if ((sender_ip && sender_ip->sin_family != AF_INET) ||
436 	    target_ip->sin_family != AF_INET) {
437 		return EAFNOSUPPORT;
438 	}
439 
440 	result = mbuf_gethdr(MBUF_DONTWAIT, MBUF_TYPE_DATA, &m);
441 	if (result != 0) {
442 		return result;
443 	}
444 
445 	mbuf_setlen(m, sizeof(*ea));
446 	mbuf_pkthdr_setlen(m, sizeof(*ea));
447 
448 	/* Move the data pointer in the mbuf to the end, aligned to 4 bytes */
449 	datap = mtod(m, char*);
450 	datap += mbuf_trailingspace(m);
451 	datap -= (((uintptr_t)datap) & 0x3);
452 	mbuf_setdata(m, datap, sizeof(*ea));
453 	ea = mtod(m, struct ether_arp *);
454 
455 	/*
456 	 * Prepend the ethernet header, we will send the raw frame;
457 	 * callee frees the original mbuf when allocation fails.
458 	 */
459 	result = mbuf_prepend(&m, sizeof(*eh), MBUF_DONTWAIT);
460 	if (result != 0) {
461 		return result;
462 	}
463 
464 	eh = mtod(m, struct ether_header *);
465 	eh->ether_type = htons(ETHERTYPE_ARP);
466 
467 	/* Fill out the arp header */
468 	ea->arp_pro = htons(ETHERTYPE_IP);
469 	ea->arp_hln = sizeof(ea->arp_sha);
470 	ea->arp_pln = sizeof(ea->arp_spa);
471 	ea->arp_hrd = htons(ARPHRD_ETHER);
472 	ea->arp_op = htons(arpop);
473 
474 	/* Sender Hardware */
475 	if (sender_hw != NULL) {
476 		bcopy(CONST_LLADDR(sender_hw), ea->arp_sha,
477 		    sizeof(ea->arp_sha));
478 	} else {
479 		ifnet_lladdr_copy_bytes(ifp, ea->arp_sha, ETHER_ADDR_LEN);
480 	}
481 	ifnet_lladdr_copy_bytes(ifp, eh->ether_shost, sizeof(eh->ether_shost));
482 
483 	/* Sender IP */
484 	if (sender_ip != NULL) {
485 		bcopy(&sender_ip->sin_addr, ea->arp_spa, sizeof(ea->arp_spa));
486 	} else {
487 		struct ifaddr *ifa;
488 
489 		/* Look for an IP address to use as our source */
490 		ifnet_lock_shared(ifp);
491 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
492 			IFA_LOCK(ifa);
493 			if (ifa->ifa_addr != NULL &&
494 			    ifa->ifa_addr->sa_family == AF_INET) {
495 				bcopy(&(SIN(ifa->ifa_addr))->sin_addr, ea->arp_spa,
496 				    sizeof(ea->arp_spa));
497 				IFA_UNLOCK(ifa);
498 				break;
499 			}
500 			IFA_UNLOCK(ifa);
501 		}
502 		ifnet_lock_done(ifp);
503 
504 		if (ifa == NULL) {
505 			mbuf_freem(m);
506 			return ENXIO;
507 		}
508 	}
509 
510 	/* Target Hardware */
511 	if (target_hw == NULL) {
512 		bzero(ea->arp_tha, sizeof(ea->arp_tha));
513 		bcopy(etherbroadcastaddr, eh->ether_dhost,
514 		    sizeof(eh->ether_dhost));
515 		m->m_flags |= M_BCAST;
516 	} else {
517 		bcopy(CONST_LLADDR(target_hw), ea->arp_tha,
518 		    sizeof(ea->arp_tha));
519 		bcopy(CONST_LLADDR(target_hw), eh->ether_dhost,
520 		    sizeof(eh->ether_dhost));
521 
522 		if (bcmp(eh->ether_dhost, etherbroadcastaddr,
523 		    ETHER_ADDR_LEN) == 0) {
524 			m->m_flags |= M_BCAST;
525 		}
526 	}
527 
528 	/* Target IP */
529 	bcopy(&target_ip->sin_addr, ea->arp_tpa, sizeof(ea->arp_tpa));
530 
531 	/*
532 	 * PKTF_{INET,INET6}_RESOLVE_RTR are mutually exclusive, so make
533 	 * sure only one of them is set (just in case.)
534 	 */
535 	m->m_pkthdr.pkt_flags &= ~(PKTF_INET6_RESOLVE | PKTF_RESOLVE_RTR);
536 	m->m_pkthdr.pkt_flags |= PKTF_INET_RESOLVE;
537 	/*
538 	 * If this is an ARP request for a (default) router, mark
539 	 * the packet accordingly so that the driver can find out,
540 	 * in case it needs to perform driver-specific action(s).
541 	 */
542 	if (arpop == ARPOP_REQUEST && (target_ip->sin_other & SIN_ROUTER)) {
543 		m->m_pkthdr.pkt_flags |= PKTF_RESOLVE_RTR;
544 	}
545 
546 	if (ifp->if_eflags & IFEF_TXSTART) {
547 		/*
548 		 * Use control service class if the interface
549 		 * supports transmit-start model
550 		 */
551 		(void) m_set_service_class(m, MBUF_SC_CTL);
552 	}
553 
554 	ifnet_output_raw(ifp, IS_INTF_CLAT46(ifp) ? 0 : AF_INET, m);
555 
556 	return 0;
557 }
558 
559 errno_t
ether_attach_inet(struct ifnet * ifp,protocol_family_t proto_family)560 ether_attach_inet(struct ifnet *ifp, protocol_family_t proto_family)
561 {
562 #pragma unused(proto_family)
563 	struct ifnet_attach_proto_param_v2 proto = {};
564 	u_short en_native = htons(ETHERTYPE_IP);
565 	u_short arp_native = htons(ETHERTYPE_ARP);
566 	struct ifnet_demux_desc demux[2] = {
567 		{ .type = DLIL_DESC_ETYPE2, .data = &en_native,
568 		  .datalen = sizeof(en_native) },
569 		{ .type = DLIL_DESC_ETYPE2, .data = &arp_native,
570 		  .datalen = sizeof(arp_native) }
571 	};
572 	errno_t error;
573 
574 	proto.demux_list = demux;
575 	proto.demux_count = sizeof(demux) / sizeof(demux[0]);
576 	proto.input = ether_inet_input;
577 	proto.pre_output = ether_inet_pre_output;
578 	proto.ioctl = ether_inet_prmod_ioctl;
579 	proto.event = ether_inet_event;
580 	proto.resolve = ether_inet_resolve_multi;
581 	proto.send_arp = ether_inet_arp;
582 
583 	error = ifnet_attach_protocol_v2(ifp, proto_family, &proto);
584 	if (error && error != EEXIST) {
585 		printf("WARNING: %s can't attach ip to %s\n", __func__,
586 		    if_name(ifp));
587 	}
588 	return error;
589 }
590 
591 void
ether_detach_inet(struct ifnet * ifp,protocol_family_t proto_family)592 ether_detach_inet(struct ifnet *ifp, protocol_family_t proto_family)
593 {
594 	(void) ifnet_detach_protocol(ifp, proto_family);
595 }
596