xref: /xnu-11417.101.15/bsd/net/ether_inet_pr_module.c (revision e3723e1f17661b24996789d8afc084c0c3303b26)
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,IFNET_FRAME_TYPE_RW_T frame_type,IFNET_LLADDR_RW_T dst_host_lladdr)238 ether_inet_pre_output(ifnet_t ifp, protocol_family_t protocol_family,
239     mbuf_t *m0, const struct sockaddr *dst_netaddr, void *route,
240     IFNET_FRAME_TYPE_RW_T frame_type, IFNET_LLADDR_RW_T dst_host_lladdr)
241 {
242 #pragma unused(protocol_family)
243 	struct mbuf *m = *m0;
244 	const struct ether_header *eh;
245 	errno_t result = 0;
246 
247 	if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING)) {
248 		return ENETDOWN;
249 	}
250 
251 	/*
252 	 * Tell ether_frameout it's ok to loop packet unless negated below.
253 	 */
254 	m->m_flags |= M_LOOP;
255 
256 	switch (dst_netaddr->sa_family) {
257 	case AF_INET: {
258 		struct sockaddr_dl ll_dest = {};
259 
260 		result = arp_lookup_ip(ifp,
261 		    SIN(dst_netaddr),
262 		    &ll_dest, sizeof(ll_dest), (route_t)route, *m0);
263 		if (result == 0) {
264 			u_int16_t ethertype_ip = htons(ETHERTYPE_IP);
265 
266 			bcopy(LLADDR(&ll_dest), dst_host_lladdr, ETHER_ADDR_LEN);
267 			bcopy(&ethertype_ip, frame_type, sizeof(ethertype_ip));
268 		}
269 		break;
270 	}
271 
272 	case pseudo_AF_HDRCMPLT:
273 	case AF_UNSPEC:
274 		m->m_flags &= ~M_LOOP;
275 		eh = (const struct ether_header *)__DECONST(void *, dst_netaddr->sa_data);
276 		bcopy(eh->ether_dhost, dst_host_lladdr, ETHER_ADDR_LEN);
277 		bcopy(&eh->ether_type, frame_type, sizeof(eh->ether_type));
278 		break;
279 
280 	default:
281 		printf("%s: can't handle af%d\n", if_name(ifp),
282 		    dst_netaddr->sa_family);
283 
284 		result = EAFNOSUPPORT;
285 		break;
286 	}
287 
288 	return result;
289 }
290 
291 static errno_t
ether_inet_resolve_multi(ifnet_t ifp,const struct sockaddr * proto_addr,struct sockaddr_dl * out_ll,size_t ll_len)292 ether_inet_resolve_multi(ifnet_t ifp, const struct sockaddr *proto_addr,
293     struct sockaddr_dl *out_ll, size_t ll_len)
294 {
295 	static const size_t minsize =
296 	    offsetof(struct sockaddr_dl, sdl_data[0]) + ETHER_ADDR_LEN;
297 	const struct sockaddr_in *sin = SIN(proto_addr);
298 
299 	if (proto_addr->sa_family != AF_INET) {
300 		return EAFNOSUPPORT;
301 	}
302 
303 	if (proto_addr->sa_len < sizeof(struct sockaddr_in)) {
304 		return EINVAL;
305 	}
306 
307 	if (ll_len < minsize) {
308 		return EMSGSIZE;
309 	}
310 
311 	SOCKADDR_ZERO(out_ll, minsize);
312 	out_ll->sdl_len = minsize;
313 	out_ll->sdl_family = AF_LINK;
314 	out_ll->sdl_index = ifp->if_index;
315 	out_ll->sdl_type = IFT_ETHER;
316 	out_ll->sdl_nlen = 0;
317 	out_ll->sdl_alen = ETHER_ADDR_LEN;
318 	out_ll->sdl_slen = 0;
319 	ETHER_MAP_IP_MULTICAST(&sin->sin_addr, LLADDR(out_ll));
320 
321 	return 0;
322 }
323 
324 static errno_t
ether_inet_prmod_ioctl(ifnet_t ifp,protocol_family_t protocol_family,u_long command,void * data)325 ether_inet_prmod_ioctl(ifnet_t ifp, protocol_family_t protocol_family,
326     u_long command, void *data)
327 {
328 #pragma unused(protocol_family)
329 	int error = 0;
330 
331 	switch (command) {
332 	case SIOCSIFADDR:               /* struct ifaddr pointer */
333 	case SIOCAIFADDR: {             /* struct ifaddr pointer */
334 		/*
335 		 * Note: caller of ifnet_ioctl() passes in pointer to
336 		 * struct ifaddr as parameter to SIOC{A,S}IFADDR, for
337 		 * legacy reasons.
338 		 */
339 		struct ifaddr *ifa __single = data;
340 
341 		if (!(ifnet_flags(ifp) & IFF_RUNNING)) {
342 			ifnet_set_flags(ifp, IFF_UP, IFF_UP);
343 			ifnet_ioctl(ifp, 0, SIOCSIFFLAGS, NULL);
344 		}
345 
346 		if (ifaddr_address_family(ifa) != AF_INET) {
347 			break;
348 		}
349 
350 		inet_arp_init_ifaddr(ifp, ifa);
351 
352 		if (command != SIOCSIFADDR) {
353 			break;
354 		}
355 
356 		/*
357 		 * Register new IP and MAC addresses with the kernel
358 		 * debugger if the interface is the same as was registered
359 		 * by IOKernelDebugger. If no interface was registered,
360 		 * fall back and just match against en0 interface.
361 		 * Do this only for the first address of the interface
362 		 * and not for aliases.
363 		 */
364 		if ((kdp_get_interface() != 0 &&
365 		    kdp_get_interface() == ifp->if_softc) ||
366 		    (kdp_get_interface() == 0 && ifp->if_unit == 0)) {
367 			kdp_set_ip_and_mac_addresses(&(IA_SIN(ifa)->sin_addr),
368 			    (struct ether_addr *)IF_LLADDR(ifp));
369 		}
370 		break;
371 	}
372 
373 	case SIOCGIFADDR: {             /* struct ifreq */
374 		struct ifreq *ifr __single = data;
375 		ifnet_guarded_lladdr_copy_bytes(ifp, ifr->ifr_addr.sa_data,
376 		    ETHER_ADDR_LEN);
377 		break;
378 	}
379 
380 	default:
381 		error = EOPNOTSUPP;
382 		break;
383 	}
384 
385 	return error;
386 }
387 
388 static void
ether_inet_event(ifnet_t ifp,protocol_family_t protocol,const struct kev_msg * event)389 ether_inet_event(ifnet_t ifp, protocol_family_t protocol,
390     const struct kev_msg *event)
391 {
392 #pragma unused(protocol)
393 	uint16_t address_count = 0;
394 	ifaddr_ref_t * __counted_by(address_count) addresses = NULL;
395 
396 	if (event->vendor_code != KEV_VENDOR_APPLE ||
397 	    event->kev_class != KEV_NETWORK_CLASS ||
398 	    event->kev_subclass != KEV_DL_SUBCLASS ||
399 	    event->event_code != KEV_DL_LINK_ADDRESS_CHANGED) {
400 		return;
401 	}
402 
403 	if (ifnet_get_address_list_family_with_count(ifp, &addresses, &address_count, AF_INET) == 0) {
404 		int i;
405 
406 		for (i = 0; addresses[i] != NULL; i++) {
407 			inet_arp_init_ifaddr(ifp, addresses[i]);
408 		}
409 
410 		ifnet_address_list_free_counted_by(addresses, address_count);
411 	}
412 }
413 
414 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)415 ether_inet_arp(ifnet_t ifp, u_short arpop, const struct sockaddr_dl *sender_hw,
416     const struct sockaddr *sender_proto, const struct sockaddr_dl *target_hw,
417     const struct sockaddr *target_proto)
418 {
419 	mbuf_ref_t  m;
420 	errno_t result;
421 	struct ether_header *eh;
422 	struct ether_arp *ea;
423 	const struct sockaddr_in *sender_ip =
424 	    SIN(sender_proto);
425 	const struct sockaddr_inarp *target_ip =
426 	    __SA_UTILS_CONV_TO_SOCKADDR_INARP(target_proto);
427 	char *datap;
428 
429 	if (target_ip == NULL) {
430 		return EINVAL;
431 	}
432 
433 	if ((sender_ip && sender_ip->sin_family != AF_INET) ||
434 	    target_ip->sin_family != AF_INET) {
435 		return EAFNOSUPPORT;
436 	}
437 
438 	result = mbuf_gethdr(MBUF_DONTWAIT, MBUF_TYPE_DATA, &m);
439 	if (result != 0) {
440 		return result;
441 	}
442 
443 	mbuf_setlen(m, sizeof(*ea));
444 	mbuf_pkthdr_setlen(m, sizeof(*ea));
445 
446 	/* Move the data pointer in the mbuf to the end, aligned to 4 bytes */
447 	datap = mtod(m, char*);
448 	datap += mbuf_trailingspace(m);
449 	datap -= (((uintptr_t)datap) & 0x3);
450 	mbuf_setdata(m, datap, sizeof(*ea));
451 	ea = mtod(m, struct ether_arp *);
452 
453 	/*
454 	 * Prepend the ethernet header, we will send the raw frame;
455 	 * callee frees the original mbuf when allocation fails.
456 	 */
457 	result = mbuf_prepend(&m, sizeof(*eh), MBUF_DONTWAIT);
458 	if (result != 0) {
459 		return result;
460 	}
461 
462 	eh = mtod(m, struct ether_header *);
463 	eh->ether_type = htons(ETHERTYPE_ARP);
464 
465 	/* Fill out the arp header */
466 	ea->arp_pro = htons(ETHERTYPE_IP);
467 	ea->arp_hln = sizeof(ea->arp_sha);
468 	ea->arp_pln = sizeof(ea->arp_spa);
469 	ea->arp_hrd = htons(ARPHRD_ETHER);
470 	ea->arp_op = htons(arpop);
471 
472 	/* Sender Hardware */
473 	if (sender_hw != NULL) {
474 		bcopy(CONST_LLADDR(sender_hw), ea->arp_sha,
475 		    sizeof(ea->arp_sha));
476 	} else {
477 		ifnet_lladdr_copy_bytes(ifp, ea->arp_sha, ETHER_ADDR_LEN);
478 	}
479 	ifnet_lladdr_copy_bytes(ifp, eh->ether_shost, sizeof(eh->ether_shost));
480 
481 	/* Sender IP */
482 	if (sender_ip != NULL) {
483 		bcopy(&sender_ip->sin_addr, ea->arp_spa, sizeof(ea->arp_spa));
484 	} else {
485 		struct ifaddr *ifa;
486 
487 		/* Look for an IP address to use as our source */
488 		ifnet_lock_shared(ifp);
489 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
490 			IFA_LOCK(ifa);
491 			if (ifa->ifa_addr != NULL &&
492 			    ifa->ifa_addr->sa_family == AF_INET) {
493 				bcopy(&(SIN(ifa->ifa_addr))->sin_addr, ea->arp_spa,
494 				    sizeof(ea->arp_spa));
495 				IFA_UNLOCK(ifa);
496 				break;
497 			}
498 			IFA_UNLOCK(ifa);
499 		}
500 		ifnet_lock_done(ifp);
501 
502 		if (ifa == NULL) {
503 			mbuf_freem(m);
504 			return ENXIO;
505 		}
506 	}
507 
508 	/* Target Hardware */
509 	if (target_hw == NULL) {
510 		bzero(ea->arp_tha, sizeof(ea->arp_tha));
511 		bcopy(etherbroadcastaddr, eh->ether_dhost,
512 		    sizeof(eh->ether_dhost));
513 		m->m_flags |= M_BCAST;
514 	} else {
515 		bcopy(CONST_LLADDR(target_hw), ea->arp_tha,
516 		    sizeof(ea->arp_tha));
517 		bcopy(CONST_LLADDR(target_hw), eh->ether_dhost,
518 		    sizeof(eh->ether_dhost));
519 
520 		if (bcmp(eh->ether_dhost, etherbroadcastaddr,
521 		    ETHER_ADDR_LEN) == 0) {
522 			m->m_flags |= M_BCAST;
523 		}
524 	}
525 
526 	/* Target IP */
527 	bcopy(&target_ip->sin_addr, ea->arp_tpa, sizeof(ea->arp_tpa));
528 
529 	/*
530 	 * PKTF_{INET,INET6}_RESOLVE_RTR are mutually exclusive, so make
531 	 * sure only one of them is set (just in case.)
532 	 */
533 	m->m_pkthdr.pkt_flags &= ~(PKTF_INET6_RESOLVE | PKTF_RESOLVE_RTR);
534 	m->m_pkthdr.pkt_flags |= PKTF_INET_RESOLVE;
535 	/*
536 	 * If this is an ARP request for a (default) router, mark
537 	 * the packet accordingly so that the driver can find out,
538 	 * in case it needs to perform driver-specific action(s).
539 	 */
540 	if (arpop == ARPOP_REQUEST && (target_ip->sin_other & SIN_ROUTER)) {
541 		m->m_pkthdr.pkt_flags |= PKTF_RESOLVE_RTR;
542 	}
543 
544 	if (ifp->if_eflags & IFEF_TXSTART) {
545 		/*
546 		 * Use control service class if the interface
547 		 * supports transmit-start model
548 		 */
549 		(void) m_set_service_class(m, MBUF_SC_CTL);
550 	}
551 
552 	ifnet_output_raw(ifp, IS_INTF_CLAT46(ifp) ? 0 : AF_INET, m);
553 
554 	return 0;
555 }
556 
557 errno_t
ether_attach_inet(struct ifnet * ifp,protocol_family_t proto_family)558 ether_attach_inet(struct ifnet *ifp, protocol_family_t proto_family)
559 {
560 #pragma unused(proto_family)
561 	struct ifnet_attach_proto_param_v2 proto = {};
562 	u_short en_native = htons(ETHERTYPE_IP);
563 	u_short arp_native = htons(ETHERTYPE_ARP);
564 	struct ifnet_demux_desc demux[2] = {
565 		{ .type = DLIL_DESC_ETYPE2, .data = &en_native,
566 		  .datalen = sizeof(en_native) },
567 		{ .type = DLIL_DESC_ETYPE2, .data = &arp_native,
568 		  .datalen = sizeof(arp_native) }
569 	};
570 	errno_t error;
571 
572 	proto.demux_list = demux;
573 	proto.demux_count = sizeof(demux) / sizeof(demux[0]);
574 	proto.input = ether_inet_input;
575 	proto.pre_output = ether_inet_pre_output;
576 	proto.ioctl = ether_inet_prmod_ioctl;
577 	proto.event = ether_inet_event;
578 	proto.resolve = ether_inet_resolve_multi;
579 	proto.send_arp = ether_inet_arp;
580 
581 	error = ifnet_attach_protocol_v2(ifp, proto_family, &proto);
582 	if (error && error != EEXIST) {
583 		printf("WARNING: %s can't attach ip to %s\n", __func__,
584 		    if_name(ifp));
585 	}
586 	return error;
587 }
588 
589 void
ether_detach_inet(struct ifnet * ifp,protocol_family_t proto_family)590 ether_detach_inet(struct ifnet *ifp, protocol_family_t proto_family)
591 {
592 	(void) ifnet_detach_protocol(ifp, proto_family);
593 }
594