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