| 1 | /* |
| 2 | * Copyright (c) 2000-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 | /* |
| 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 | #include <sys/param.h> |
| 63 | #include <sys/systm.h> |
| 64 | #include <sys/kernel.h> |
| 65 | #include <sys/malloc.h> |
| 66 | #include <sys/mbuf.h> |
| 67 | #include <sys/socket.h> |
| 68 | #include <sys/sockio.h> |
| 69 | #include <sys/sysctl.h> |
| 70 | |
| 71 | #include <pexpert/pexpert.h> |
| 72 | |
| 73 | #define etherbroadcastaddr fugly |
| 74 | #include <net/if.h> |
| 75 | #include <net/route.h> |
| 76 | #include <net/if_llc.h> |
| 77 | #include <net/if_dl.h> |
| 78 | #include <net/if_types.h> |
| 79 | #include <net/if_ether.h> |
| 80 | #include <net/if_gif.h> |
| 81 | #include <netinet/if_ether.h> |
| 82 | #include <netinet/in.h> /* For M_LOOP */ |
| 83 | #include <net/kpi_interface.h> |
| 84 | #include <net/kpi_protocol.h> |
| 85 | #undef etherbroadcastaddr |
| 86 | |
| 87 | /* |
| 88 | #if INET |
| 89 | #include <netinet/in.h> |
| 90 | #include <netinet/in_var.h> |
| 91 | * |
| 92 | #include <netinet/in_systm.h> |
| 93 | #include <netinet/ip.h> |
| 94 | #endif |
| 95 | */ |
| 96 | #include <net/ether_if_module.h> |
| 97 | #include <sys/socketvar.h> |
| 98 | #include <net/if_vlan_var.h> |
| 99 | #if BOND |
| 100 | #include <net/if_bond_internal.h> |
| 101 | #endif /* BOND */ |
| 102 | #if IF_BRIDGE |
| 103 | #include <net/if_bridgevar.h> |
| 104 | #endif /* IF_BRIDGE */ |
| 105 | #if IF_FAKE |
| 106 | #include <net/if_fake_var.h> |
| 107 | #endif /* IF_FAKE */ |
| 108 | #if IF_HEADLESS |
| 109 | extern void if_headless_init(void); |
| 110 | #endif /* IF_HEADLESS */ |
| 111 | |
| 112 | #include <net/dlil.h> |
| 113 | |
| 114 | #include <net/sockaddr_utils.h> |
| 115 | |
| 116 | SYSCTL_DECL(_net_link); |
| 117 | SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW | CTLFLAG_LOCKED, 0, |
| 118 | "Ethernet" ); |
| 119 | |
| 120 | struct en_desc { |
| 121 | u_int16_t type; /* Type of protocol stored in data */ |
| 122 | u_int32_t protocol_family; /* Protocol family */ |
| 123 | u_int32_t data[2]; /* Protocol data */ |
| 124 | }; |
| 125 | |
| 126 | /* descriptors are allocated in blocks of ETHER_DESC_BLK_SIZE */ |
| 127 | #if !XNU_TARGET_OS_OSX |
| 128 | #define ETHER_DESC_BLK_SIZE (2) /* IP, ARP */ |
| 129 | #else /* XNU_TARGET_OS_OSX */ |
| 130 | #define ETHER_DESC_BLK_SIZE (10) |
| 131 | #endif /* XNU_TARGET_OS_OSX */ |
| 132 | |
| 133 | /* |
| 134 | * Header for the demux list, hangs off of IFP at if_family_cookie |
| 135 | */ |
| 136 | struct ether_desc_blk_str { |
| 137 | u_int32_t n_max_used; |
| 138 | u_int32_t n_count; |
| 139 | u_int32_t n_used; |
| 140 | struct en_desc block_ptr[1]; |
| 141 | }; |
| 142 | |
| 143 | /* Size of the above struct before the array of struct en_desc */ |
| 144 | #define \ |
| 145 | ((size_t) offsetof(struct ether_desc_blk_str, block_ptr)) |
| 146 | |
| 147 | __private_extern__ u_char etherbroadcastaddr[ETHER_ADDR_LEN] = |
| 148 | { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; |
| 149 | |
| 150 | /* |
| 151 | * Release all descriptor entries owned by this protocol (there may be several). |
| 152 | * Setting the type to 0 releases the entry. Eventually we should compact-out |
| 153 | * the unused entries. |
| 154 | */ |
| 155 | int |
| 156 | ether_del_proto(ifnet_t ifp, protocol_family_t protocol_family) |
| 157 | { |
| 158 | struct ether_desc_blk_str *desc_blk = |
| 159 | (struct ether_desc_blk_str *)ifp->if_family_cookie; |
| 160 | u_int32_t current = 0; |
| 161 | int found = 0; |
| 162 | |
| 163 | if (desc_blk == NULL) { |
| 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | for (current = desc_blk->n_max_used; current > 0; current--) { |
| 168 | if (desc_blk->block_ptr[current - 1].protocol_family == |
| 169 | protocol_family) { |
| 170 | found = 1; |
| 171 | desc_blk->block_ptr[current - 1].type = 0; |
| 172 | desc_blk->n_used--; |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | if (desc_blk->n_used == 0) { |
| 177 | u_int32_t size = desc_blk->n_count * sizeof(struct en_desc) + |
| 178 | ETHER_DESC_HEADER_SIZE; |
| 179 | kfree_data(ifp->if_family_cookie, size); |
| 180 | ifp->if_family_cookie = 0; |
| 181 | } else { |
| 182 | /* Decrement n_max_used */ |
| 183 | for (; desc_blk->n_max_used > 0 && |
| 184 | desc_blk->block_ptr[desc_blk->n_max_used - 1].type == 0; |
| 185 | desc_blk->n_max_used--) { |
| 186 | ; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | return 0; |
| 191 | } |
| 192 | |
| 193 | static int |
| 194 | ether_add_proto_internal(struct ifnet *ifp, protocol_family_t protocol, |
| 195 | const struct ifnet_demux_desc *demux) |
| 196 | { |
| 197 | struct en_desc *ed; |
| 198 | struct ether_desc_blk_str *desc_blk = |
| 199 | (struct ether_desc_blk_str *)ifp->if_family_cookie; |
| 200 | u_int32_t i; |
| 201 | |
| 202 | switch (demux->type) { |
| 203 | /* These types are supported */ |
| 204 | /* Top three are preferred */ |
| 205 | case DLIL_DESC_ETYPE2: |
| 206 | if (demux->datalen != 2) { |
| 207 | return EINVAL; |
| 208 | } |
| 209 | break; |
| 210 | |
| 211 | case DLIL_DESC_SAP: |
| 212 | if (demux->datalen != 3) { |
| 213 | return EINVAL; |
| 214 | } |
| 215 | break; |
| 216 | |
| 217 | case DLIL_DESC_SNAP: |
| 218 | if (demux->datalen != 5) { |
| 219 | return EINVAL; |
| 220 | } |
| 221 | break; |
| 222 | |
| 223 | default: |
| 224 | return ENOTSUP; |
| 225 | } |
| 226 | |
| 227 | /* Verify a matching descriptor does not exist */ |
| 228 | if (desc_blk != NULL) { |
| 229 | switch (demux->type) { |
| 230 | case DLIL_DESC_ETYPE2: |
| 231 | for (i = 0; i < desc_blk->n_max_used; i++) { |
| 232 | if (desc_blk->block_ptr[i].type == |
| 233 | DLIL_DESC_ETYPE2 && |
| 234 | desc_blk->block_ptr[i].data[0] == |
| 235 | *(u_int16_t*)demux->data) { |
| 236 | return EADDRINUSE; |
| 237 | } |
| 238 | } |
| 239 | break; |
| 240 | case DLIL_DESC_SAP: |
| 241 | case DLIL_DESC_SNAP: |
| 242 | for (i = 0; i < desc_blk->n_max_used; i++) { |
| 243 | if (desc_blk->block_ptr[i].type == |
| 244 | demux->type && |
| 245 | bcmp(s1: desc_blk->block_ptr[i].data, |
| 246 | s2: demux->data, n: demux->datalen) == 0) { |
| 247 | return EADDRINUSE; |
| 248 | } |
| 249 | } |
| 250 | break; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | /* Check for case where all of the descriptor blocks are in use */ |
| 255 | if (desc_blk == NULL || desc_blk->n_used == desc_blk->n_count) { |
| 256 | struct ether_desc_blk_str *tmp; |
| 257 | u_int32_t new_count = ETHER_DESC_BLK_SIZE; |
| 258 | u_int32_t new_size; |
| 259 | u_int32_t old_size = 0; |
| 260 | |
| 261 | i = 0; |
| 262 | |
| 263 | if (desc_blk) { |
| 264 | new_count += desc_blk->n_count; |
| 265 | old_size = desc_blk->n_count * sizeof(struct en_desc) + |
| 266 | ETHER_DESC_HEADER_SIZE; |
| 267 | i = desc_blk->n_used; |
| 268 | } |
| 269 | |
| 270 | new_size = new_count * sizeof(struct en_desc) + |
| 271 | ETHER_DESC_HEADER_SIZE; |
| 272 | |
| 273 | tmp = (struct ether_desc_blk_str *)kalloc_data(new_size, Z_WAITOK); |
| 274 | if (tmp == NULL) { |
| 275 | /* |
| 276 | * Remove any previous descriptors set in the call. |
| 277 | */ |
| 278 | return ENOMEM; |
| 279 | } |
| 280 | |
| 281 | bzero(s: ((char *)tmp) + old_size, n: new_size - old_size); |
| 282 | if (desc_blk) { |
| 283 | bcopy(src: desc_blk, dst: tmp, n: old_size); |
| 284 | kfree_data(desc_blk, old_size); |
| 285 | } |
| 286 | desc_blk = tmp; |
| 287 | ifp->if_family_cookie = (uintptr_t)desc_blk; |
| 288 | desc_blk->n_count = new_count; |
| 289 | } else { |
| 290 | /* Find a free entry */ |
| 291 | for (i = 0; i < desc_blk->n_count; i++) { |
| 292 | if (desc_blk->block_ptr[i].type == 0) { |
| 293 | break; |
| 294 | } |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | /* Bump n_max_used if appropriate */ |
| 299 | if (i + 1 > desc_blk->n_max_used) { |
| 300 | desc_blk->n_max_used = i + 1; |
| 301 | } |
| 302 | |
| 303 | ed = &desc_blk->block_ptr[i]; |
| 304 | ed->protocol_family = protocol; |
| 305 | ed->data[0] = 0; |
| 306 | ed->data[1] = 0; |
| 307 | |
| 308 | switch (demux->type) { |
| 309 | case DLIL_DESC_ETYPE2: |
| 310 | /* 2 byte ethernet raw protocol type is at native_type */ |
| 311 | /* prtocol must be in network byte order */ |
| 312 | ed->type = DLIL_DESC_ETYPE2; |
| 313 | ed->data[0] = *(u_int16_t*)demux->data; |
| 314 | break; |
| 315 | |
| 316 | case DLIL_DESC_SAP: |
| 317 | ed->type = DLIL_DESC_SAP; |
| 318 | bcopy(src: demux->data, dst: &ed->data[0], n: 3); |
| 319 | break; |
| 320 | |
| 321 | case DLIL_DESC_SNAP: { |
| 322 | u_int8_t* pDest = ((u_int8_t*)&ed->data[0]) + 3; |
| 323 | ed->type = DLIL_DESC_SNAP; |
| 324 | bcopy(src: demux->data, dst: pDest, n: 5); |
| 325 | break; |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | desc_blk->n_used++; |
| 330 | |
| 331 | return 0; |
| 332 | } |
| 333 | |
| 334 | int |
| 335 | ether_add_proto(ifnet_t ifp, protocol_family_t protocol, |
| 336 | const struct ifnet_demux_desc *demux_list, u_int32_t demux_count) |
| 337 | { |
| 338 | int error = 0; |
| 339 | u_int32_t i; |
| 340 | |
| 341 | for (i = 0; i < demux_count; i++) { |
| 342 | error = ether_add_proto_internal(ifp, protocol, demux: &demux_list[i]); |
| 343 | if (error) { |
| 344 | ether_del_proto(ifp, protocol_family: protocol); |
| 345 | break; |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | return error; |
| 350 | } |
| 351 | |
| 352 | int |
| 353 | ether_demux(ifnet_t ifp, mbuf_t m, char *, |
| 354 | protocol_family_t *protocol_family) |
| 355 | { |
| 356 | struct ether_header *eh = (struct ether_header *)(void *)frame_header; |
| 357 | u_short ether_type = eh->ether_type; |
| 358 | u_int16_t type; |
| 359 | u_int8_t *data; |
| 360 | u_int32_t i = 0; |
| 361 | struct ether_desc_blk_str *desc_blk = |
| 362 | (struct ether_desc_blk_str *)ifp->if_family_cookie; |
| 363 | u_int32_t maxd = desc_blk ? desc_blk->n_max_used : 0; |
| 364 | struct en_desc *ed = desc_blk ? desc_blk->block_ptr : NULL; |
| 365 | u_int32_t extProto1 = 0; |
| 366 | u_int32_t extProto2 = 0; |
| 367 | |
| 368 | if (eh->ether_dhost[0] & 1) { |
| 369 | /* Check for broadcast */ |
| 370 | if (_ether_cmp(a: etherbroadcastaddr, b: eh->ether_dhost) == 0) { |
| 371 | m->m_flags |= M_BCAST; |
| 372 | } else { |
| 373 | m->m_flags |= M_MCAST; |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | if (m->m_flags & M_HASFCS) { |
| 378 | /* |
| 379 | * If the M_HASFCS is set by the driver we want to make sure |
| 380 | * that we strip off the trailing FCS data before handing it |
| 381 | * up the stack. |
| 382 | */ |
| 383 | m_adj(m, -ETHER_CRC_LEN); |
| 384 | m->m_flags &= ~M_HASFCS; |
| 385 | } |
| 386 | |
| 387 | if ((eh->ether_dhost[0] & 1) == 0) { |
| 388 | /* |
| 389 | * When the driver is put into promiscuous mode we may receive |
| 390 | * unicast frames that are not intended for our interfaces. |
| 391 | * They are marked here as being promiscuous so the caller may |
| 392 | * dispose of them after passing the packets to any interface |
| 393 | * filters. |
| 394 | */ |
| 395 | if (_ether_cmp(a: eh->ether_dhost, IF_LLADDR(ifp))) { |
| 396 | m->m_flags |= M_PROMISC; |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | /* check for VLAN */ |
| 401 | if ((m->m_pkthdr.csum_flags & CSUM_VLAN_TAG_VALID) != 0) { |
| 402 | if (EVL_VLANOFTAG(m->m_pkthdr.vlan_tag) != 0) { |
| 403 | *protocol_family = PF_VLAN; |
| 404 | return 0; |
| 405 | } |
| 406 | /* the packet is just priority-tagged, clear the bit */ |
| 407 | m->m_pkthdr.csum_flags &= ~CSUM_VLAN_TAG_VALID; |
| 408 | } else if (ether_type == htons(ETHERTYPE_VLAN)) { |
| 409 | struct ether_vlan_header * evl; |
| 410 | |
| 411 | evl = (struct ether_vlan_header *)(void *)frame_header; |
| 412 | if (m->m_len < ETHER_VLAN_ENCAP_LEN || |
| 413 | ntohs(evl->evl_proto) == ETHERTYPE_VLAN || |
| 414 | EVL_VLANOFTAG(ntohs(evl->evl_tag)) != 0) { |
| 415 | *protocol_family = PF_VLAN; |
| 416 | return 0; |
| 417 | } |
| 418 | /* the packet is just priority-tagged */ |
| 419 | |
| 420 | /* make the encapsulated ethertype the actual ethertype */ |
| 421 | ether_type = evl->evl_encap_proto = evl->evl_proto; |
| 422 | |
| 423 | /* remove the encapsulation header */ |
| 424 | m->m_len -= ETHER_VLAN_ENCAP_LEN; |
| 425 | m->m_data += ETHER_VLAN_ENCAP_LEN; |
| 426 | m->m_pkthdr.len -= ETHER_VLAN_ENCAP_LEN; |
| 427 | m->m_pkthdr.csum_flags = 0; /* can't trust hardware checksum */ |
| 428 | } else if (ether_type == htons(ETHERTYPE_ARP)) { |
| 429 | m->m_pkthdr.pkt_flags |= PKTF_INET_RESOLVE; /* ARP packet */ |
| 430 | } |
| 431 | data = mtod(m, u_int8_t*); |
| 432 | |
| 433 | /* |
| 434 | * Determine the packet's protocol type and stuff the protocol into |
| 435 | * longs for quick compares. |
| 436 | */ |
| 437 | if (ntohs(ether_type) <= 1500) { |
| 438 | bcopy(src: data, dst: &extProto1, n: sizeof(u_int32_t)); |
| 439 | |
| 440 | /* SAP or SNAP */ |
| 441 | if ((extProto1 & htonl(0xFFFFFF00)) == htonl(0xAAAA0300)) { |
| 442 | /* SNAP */ |
| 443 | type = DLIL_DESC_SNAP; |
| 444 | bcopy(src: data + sizeof(u_int32_t), dst: &extProto2, |
| 445 | n: sizeof(u_int32_t)); |
| 446 | extProto1 &= htonl(0x000000FF); |
| 447 | } else { |
| 448 | type = DLIL_DESC_SAP; |
| 449 | extProto1 &= htonl(0xFFFFFF00); |
| 450 | } |
| 451 | } else { |
| 452 | type = DLIL_DESC_ETYPE2; |
| 453 | } |
| 454 | |
| 455 | /* |
| 456 | * Search through the connected protocols for a match. |
| 457 | */ |
| 458 | switch (type) { |
| 459 | case DLIL_DESC_ETYPE2: |
| 460 | for (i = 0; i < maxd; i++) { |
| 461 | if ((ed[i].type == type) && |
| 462 | (ed[i].data[0] == ether_type)) { |
| 463 | *protocol_family = ed[i].protocol_family; |
| 464 | return 0; |
| 465 | } |
| 466 | } |
| 467 | break; |
| 468 | |
| 469 | case DLIL_DESC_SAP: |
| 470 | for (i = 0; i < maxd; i++) { |
| 471 | if ((ed[i].type == type) && |
| 472 | (ed[i].data[0] == extProto1)) { |
| 473 | *protocol_family = ed[i].protocol_family; |
| 474 | return 0; |
| 475 | } |
| 476 | } |
| 477 | break; |
| 478 | |
| 479 | case DLIL_DESC_SNAP: |
| 480 | for (i = 0; i < maxd; i++) { |
| 481 | if ((ed[i].type == type) && |
| 482 | (ed[i].data[0] == extProto1) && |
| 483 | (ed[i].data[1] == extProto2)) { |
| 484 | *protocol_family = ed[i].protocol_family; |
| 485 | return 0; |
| 486 | } |
| 487 | } |
| 488 | break; |
| 489 | } |
| 490 | |
| 491 | return ENOENT; |
| 492 | } |
| 493 | |
| 494 | /* |
| 495 | * On embedded, ether_frameout is practicaly ether_frameout_extended. |
| 496 | * On non-embedded, ether_frameout has long been exposed as a public KPI, |
| 497 | * and therefore its signature must remain the same (without the pre- and |
| 498 | * postpend length parameters.) |
| 499 | */ |
| 500 | #if KPI_INTERFACE_EMBEDDED |
| 501 | int |
| 502 | ether_frameout(struct ifnet *ifp, struct mbuf **m, |
| 503 | const struct sockaddr *ndest, const char *edst, |
| 504 | const char *ether_type, u_int32_t *prepend_len, u_int32_t *postpend_len) |
| 505 | #else /* !KPI_INTERFACE_EMBEDDED */ |
| 506 | int |
| 507 | ether_frameout(struct ifnet *ifp, struct mbuf **m, |
| 508 | const struct sockaddr *ndest, const char *edst, |
| 509 | const char *ether_type) |
| 510 | #endif /* KPI_INTERFACE_EMBEDDED */ |
| 511 | { |
| 512 | #if KPI_INTERFACE_EMBEDDED |
| 513 | return ether_frameout_extended(ifp, m, ndest, edst, ether_type, |
| 514 | prepend_len, postpend_len); |
| 515 | #else /* !KPI_INTERFACE_EMBEDDED */ |
| 516 | return ether_frameout_extended(interface: ifp, packet: m, dest: ndest, dest_lladdr: edst, frame_type: ether_type, |
| 517 | NULL, NULL); |
| 518 | #endif /* !KPI_INTERFACE_EMBEDDED */ |
| 519 | } |
| 520 | |
| 521 | /* |
| 522 | * Ethernet output routine. |
| 523 | * Encapsulate a packet of type family for the local net. |
| 524 | * Use trailer local net encapsulation if enough data in first |
| 525 | * packet leaves a multiple of 512 bytes of data in remainder. |
| 526 | */ |
| 527 | int |
| 528 | ether_frameout_extended(struct ifnet *ifp, struct mbuf **m, |
| 529 | const struct sockaddr *ndest, const char *edst, |
| 530 | const char *ether_type, u_int32_t *prepend_len, u_int32_t *postpend_len) |
| 531 | { |
| 532 | struct ether_header *eh; |
| 533 | int hlen; /* link layer header length */ |
| 534 | |
| 535 | hlen = ETHER_HDR_LEN; |
| 536 | |
| 537 | /* |
| 538 | * If a simplex interface, and the packet is being sent to our |
| 539 | * Ethernet address or a broadcast address, loopback a copy. |
| 540 | * XXX To make a simplex device behave exactly like a duplex |
| 541 | * device, we should copy in the case of sending to our own |
| 542 | * ethernet address (thus letting the original actually appear |
| 543 | * on the wire). However, we don't do that here for security |
| 544 | * reasons and compatibility with the original behavior. |
| 545 | */ |
| 546 | if ((ifp->if_flags & IFF_SIMPLEX) && |
| 547 | ((*m)->m_flags & M_LOOP) && lo_ifp != NULL) { |
| 548 | if ((*m)->m_flags & M_BCAST) { |
| 549 | struct mbuf *n = m_copy(*m, 0, (int)M_COPYALL); |
| 550 | if (n != NULL) { |
| 551 | dlil_output(lo_ifp, ndest->sa_family, |
| 552 | n, NULL, ndest, 0, NULL); |
| 553 | } |
| 554 | } else if (_ether_cmp(a: edst, IF_LLADDR(ifp)) == 0) { |
| 555 | dlil_output(lo_ifp, ndest->sa_family, *m, |
| 556 | NULL, ndest, 0, NULL); |
| 557 | return EJUSTRETURN; |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | /* |
| 562 | * Add local net header. If no space in first mbuf, |
| 563 | * allocate another. |
| 564 | */ |
| 565 | M_PREPEND(*m, sizeof(struct ether_header), M_DONTWAIT, 0); |
| 566 | if (*m == NULL) { |
| 567 | return EJUSTRETURN; |
| 568 | } |
| 569 | |
| 570 | if (prepend_len != NULL) { |
| 571 | *prepend_len = sizeof(struct ether_header); |
| 572 | } |
| 573 | if (postpend_len != NULL) { |
| 574 | *postpend_len = 0; |
| 575 | } |
| 576 | |
| 577 | eh = mtod(*m, struct ether_header *); |
| 578 | (void) memcpy(dst: &eh->ether_type, src: ether_type, n: sizeof(eh->ether_type)); |
| 579 | (void) memcpy(dst: eh->ether_dhost, src: edst, ETHER_ADDR_LEN); |
| 580 | (void) memcpy(dst: eh->ether_shost, IF_LLADDR(ifp), ETHER_ADDR_LEN); |
| 581 | |
| 582 | return 0; |
| 583 | } |
| 584 | |
| 585 | errno_t |
| 586 | ether_check_multi(ifnet_t ifp, const struct sockaddr *proto_addr) |
| 587 | { |
| 588 | #pragma unused(ifp) |
| 589 | errno_t result = EAFNOSUPPORT; |
| 590 | const u_char *e_addr; |
| 591 | |
| 592 | /* |
| 593 | * AF_SPEC and AF_LINK don't require translation. We do |
| 594 | * want to verify that they specify a valid multicast. |
| 595 | */ |
| 596 | switch (proto_addr->sa_family) { |
| 597 | case AF_UNSPEC: |
| 598 | e_addr = (const u_char*)&proto_addr->sa_data[0]; |
| 599 | if ((e_addr[0] & 0x01) != 0x01) { |
| 600 | result = EADDRNOTAVAIL; |
| 601 | } else { |
| 602 | result = 0; |
| 603 | } |
| 604 | break; |
| 605 | |
| 606 | case AF_LINK: |
| 607 | e_addr = CONST_LLADDR(SDL(proto_addr)); |
| 608 | if ((e_addr[0] & 0x01) != 0x01) { |
| 609 | result = EADDRNOTAVAIL; |
| 610 | } else { |
| 611 | result = 0; |
| 612 | } |
| 613 | break; |
| 614 | } |
| 615 | |
| 616 | return result; |
| 617 | } |
| 618 | |
| 619 | int |
| 620 | ether_ioctl(ifnet_t ifp, u_int32_t command, void *data) |
| 621 | { |
| 622 | #pragma unused(ifp, command, data) |
| 623 | return EOPNOTSUPP; |
| 624 | } |
| 625 | |
| 626 | __private_extern__ int |
| 627 | ether_family_init(void) |
| 628 | { |
| 629 | errno_t error = 0; |
| 630 | |
| 631 | /* Register protocol registration functions */ |
| 632 | if ((error = proto_register_plumber(PF_INET, APPLE_IF_FAM_ETHERNET, |
| 633 | plumb: ether_attach_inet, unplumb: ether_detach_inet)) != 0) { |
| 634 | printf("proto_register_plumber failed for PF_INET error=%d\n" , |
| 635 | error); |
| 636 | goto done; |
| 637 | } |
| 638 | if ((error = proto_register_plumber(PF_INET6, APPLE_IF_FAM_ETHERNET, |
| 639 | plumb: ether_attach_inet6, unplumb: ether_detach_inet6)) != 0) { |
| 640 | printf("proto_register_plumber failed for PF_INET6 error=%d\n" , |
| 641 | error); |
| 642 | goto done; |
| 643 | } |
| 644 | #if VLAN |
| 645 | vlan_family_init(); |
| 646 | #endif /* VLAN */ |
| 647 | #if BOND |
| 648 | bond_family_init(); |
| 649 | #endif /* BOND */ |
| 650 | #if IF_BRIDGE |
| 651 | bridgeattach(0); |
| 652 | #endif /* IF_BRIDGE */ |
| 653 | #if IF_FAKE |
| 654 | if_fake_init(); |
| 655 | #endif /* IF_FAKE */ |
| 656 | #if IF_HEADLESS |
| 657 | if_headless_init(); |
| 658 | #endif /* IF_HEADLESS */ |
| 659 | done: |
| 660 | |
| 661 | return error; |
| 662 | } |
| 663 | |