Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 | /* * Copyright (c) 2000 Apple Computer, Inc. All rights reserved. * * @APPLE_LICENSE_HEADER_START@ * * The contents of this file constitute Original Code as defined in and * are subject to the Apple Public Source License Version 1.1 (the * "License"). You may not use this file except in compliance with the * License. Please obtain a copy of the License at * http://www.apple.com/publicsource and read it before using this file. * * This Original Code and all software distributed under the License are * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the * License for the specific language governing rights and limitations * under the License. * * @APPLE_LICENSE_HEADER_END@ */ /* * Copyright (c) 1982, 1989, 1993 * The Regents of the University of California. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * */ #include <sys/param.h> #include <sys/systm.h> #include <sys/kernel.h> #include <sys/malloc.h> #include <sys/mbuf.h> #include <sys/socket.h> #include <sys/sockio.h> #include <sys/sysctl.h> #include <net/if.h> #include <net/netisr.h> #include <net/route.h> #include <net/if_llc.h> #include <net/if_dl.h> #include <net/if_types.h> #include <netinet/if_ether.h> /* #if INET #include <netinet/in.h> #include <netinet/in_var.h> #include <netinet/in_systm.h> #include <netinet/ip.h> #endif */ #include <sys/socketvar.h> #include <net/dlil.h> #if LLC && CCITT extern struct ifqueue pkintrq; #endif /* General stuff from if_ethersubr.c - may not need some of it */ #include <netat/at_pat.h> #if NETAT extern struct ifqueue atalkintrq; #endif #if BRIDGE #include <net/bridge.h> #endif /* #include "vlan.h" */ #if NVLAN > 0 #include <net/if_vlan_var.h> #endif /* NVLAN > 0 */ static u_long lo_dlt = 0; static ivedonethis = 0; #define IFP2AC(IFP) ((struct arpcom *)IFP) struct en_desc { u_int16_t type; /* Type of protocol stored in data */ struct if_proto *proto; /* Protocol structure */ u_long data[2]; /* Protocol data */ }; #define ETHER_DESC_BLK_SIZE (10) #define MAX_INTERFACES 50 /* * Statics for demux module */ struct ether_desc_blk_str { u_long n_max_used; u_long n_count; struct en_desc *block_ptr; }; static struct ether_desc_blk_str ether_desc_blk[MAX_INTERFACES]; /* * Release all descriptor entries owned by this dl_tag (there may be several). * Setting the type to 0 releases the entry. Eventually we should compact-out * the unused entries. */ static int ether_del_proto(struct if_proto *proto, u_long dl_tag) { struct en_desc* ed = ether_desc_blk[proto->ifp->family_cookie].block_ptr; u_long current = 0; int found = 0; for (current = ether_desc_blk[proto->ifp->family_cookie].n_max_used; current > 0; current--) { if (ed[current - 1].proto == proto) { found = 1; ed[current - 1].type = 0; if (current == ether_desc_blk[proto->ifp->family_cookie].n_max_used) { ether_desc_blk[proto->ifp->family_cookie].n_max_used--; } } } return found; } static int ether_add_proto(struct ddesc_head_str *desc_head, struct if_proto *proto, u_long dl_tag) { char *current_ptr; struct dlil_demux_desc *desc; struct en_desc *ed; struct en_desc *last; u_long *bitmask; u_long *proto_id; u_long i; short total_length; u_long block_count; u_long *tmp; TAILQ_FOREACH(desc, desc_head, next) { switch (desc->type) { /* These types are supported */ /* Top three are preferred */ case DLIL_DESC_ETYPE2: if (desc->variants.native_type_length != 2) return EINVAL; break; case DLIL_DESC_SAP: if (desc->variants.native_type_length != 3) return EINVAL; break; case DLIL_DESC_SNAP: if (desc->variants.native_type_length != 5) return EINVAL; break; case DLIL_DESC_802_2: case DLIL_DESC_802_2_SNAP: break; case DLIL_DESC_RAW: if (desc->variants.bitmask.proto_id_length == 0) break; /* else fall through, bitmask variant not supported */ default: ether_del_proto(proto, dl_tag); return EINVAL; } restart: ed = ether_desc_blk[proto->ifp->family_cookie].block_ptr; /* Find a free entry */ for (i = 0; i < ether_desc_blk[proto->ifp->family_cookie].n_count; i++) { if (ed[i].type == 0) { break; } } if (i >= ether_desc_blk[proto->ifp->family_cookie].n_count) { u_long new_count = ETHER_DESC_BLK_SIZE + ether_desc_blk[proto->ifp->family_cookie].n_count; tmp = _MALLOC((new_count * (sizeof(*ed))), M_IFADDR, M_WAITOK); if (tmp == 0) { /* * Remove any previous descriptors set in the call. */ ether_del_proto(proto, dl_tag); return ENOMEM; } bzero(tmp, new_count * sizeof(*ed)); bcopy(ether_desc_blk[proto->ifp->family_cookie].block_ptr, tmp, ether_desc_blk[proto->ifp->family_cookie].n_count * sizeof(*ed)); FREE(ether_desc_blk[proto->ifp->family_cookie].block_ptr, M_IFADDR); ether_desc_blk[proto->ifp->family_cookie].n_count = new_count; ether_desc_blk[proto->ifp->family_cookie].block_ptr = (struct en_desc*)tmp; } /* Bump n_max_used if appropriate */ if (i + 1 > ether_desc_blk[proto->ifp->family_cookie].n_max_used) { ether_desc_blk[proto->ifp->family_cookie].n_max_used = i + 1; } ed[i].proto = proto; ed[i].data[0] = 0; ed[i].data[1] = 0; switch (desc->type) { case DLIL_DESC_RAW: /* 2 byte ethernet raw protocol type is at native_type */ /* protocol is not in network byte order */ ed[i].type = DLIL_DESC_ETYPE2; ed[i].data[0] = htons(*(u_int16_t*)desc->native_type); break; case DLIL_DESC_ETYPE2: /* 2 byte ethernet raw protocol type is at native_type */ /* prtocol must be in network byte order */ ed[i].type = DLIL_DESC_ETYPE2; ed[i].data[0] = *(u_int16_t*)desc->native_type; break; case DLIL_DESC_802_2: ed[i].type = DLIL_DESC_SAP; ed[i].data[0] = *(u_int32_t*)&desc->variants.desc_802_2; ed[i].data[0] &= htonl(0xFFFFFF00); break; case DLIL_DESC_SAP: ed[i].type = DLIL_DESC_SAP; bcopy(desc->native_type, &ed[i].data[0], 3); break; case DLIL_DESC_802_2_SNAP: ed[i].type = DLIL_DESC_SNAP; desc->variants.desc_802_2_SNAP.protocol_type = htons(desc->variants.desc_802_2_SNAP.protocol_type); bcopy(&desc->variants.desc_802_2_SNAP, &ed[i].data[0], 8); ed[i].data[0] &= htonl(0x000000FF); desc->variants.desc_802_2_SNAP.protocol_type = ntohs(desc->variants.desc_802_2_SNAP.protocol_type); break; case DLIL_DESC_SNAP: { u_int8_t* pDest = ((u_int8_t*)&ed[i].data[0]) + 3; ed[i].type = DLIL_DESC_SNAP; bcopy(&desc->native_type, pDest, 5); } break; } } return 0; } static int ether_shutdown() { return 0; } int ether_demux(ifp, m, frame_header, proto) struct ifnet *ifp; struct mbuf *m; char *frame_header; struct if_proto **proto; { register struct ether_header *eh = (struct ether_header *)frame_header; u_short ether_type = eh->ether_type; u_int16_t type; u_int8_t *data; u_long i = 0; u_long max = ether_desc_blk[ifp->family_cookie].n_max_used; struct en_desc *ed = ether_desc_blk[ifp->family_cookie].block_ptr; u_int32_t extProto1 = 0; u_int32_t extProto2 = 0; if (eh->ether_dhost[0] & 1) { /* Check for broadcast */ if (*(u_int32_t*)eh->ether_dhost == 0xFFFFFFFF && *(u_int16_t*)(eh->ether_dhost + sizeof(u_int32_t)) == 0xFFFF) m->m_flags |= M_BCAST; else m->m_flags |= M_MCAST; } data = mtod(m, u_int8_t*); /* * Determine the packet's protocol type and stuff the protocol into * longs for quick compares. */ if (ntohs(ether_type) < 1500) { extProto1 = *(u_int32_t*)data; // SAP or SNAP if ((extProto1 & htonl(0xFFFFFF00)) == htonl(0xAAAA0300)) { // SNAP type = DLIL_DESC_SNAP; extProto2 = *(u_int32_t*)(data + sizeof(u_int32_t)); extProto1 &= htonl(0x000000FF); } else { type = DLIL_DESC_SAP; extProto1 &= htonl(0xFFFFFF00); } } else { type = DLIL_DESC_ETYPE2; } /* * Search through the connected protocols for a match. */ switch (type) { case DLIL_DESC_ETYPE2: for (i = 0; i < max; i++) { if ((ed[i].type == type) && (ed[i].data[0] == ether_type)) { *proto = ed[i].proto; return 0; } } break; case DLIL_DESC_SAP: for (i = 0; i < max; i++) { if ((ed[i].type == type) && (ed[i].data[0] == extProto1)) { *proto = ed[i].proto; return 0; } } break; case DLIL_DESC_SNAP: for (i = 0; i < max; i++) { if ((ed[i].type == type) && (ed[i].data[0] == extProto1) && (ed[i].data[1] == extProto2)) { *proto = ed[i].proto; return 0; } } break; } return ENOENT; } /* * Ethernet output routine. * Encapsulate a packet of type family for the local net. * Use trailer local net encapsulation if enough data in first * packet leaves a multiple of 512 bytes of data in remainder. * Assumes that ifp is actually pointer to arpcom structure. */ int ether_frameout(ifp, m, ndest, edst, ether_type) register struct ifnet *ifp; struct mbuf **m; struct sockaddr *ndest; char *edst; char *ether_type; { register struct ether_header *eh; int hlen; /* link layer header lenght */ struct arpcom *ac = IFP2AC(ifp); hlen = ETHER_HDR_LEN; /* * If a simplex interface, and the packet is being sent to our * Ethernet address or a broadcast address, loopback a copy. * XXX To make a simplex device behave exactly like a duplex * device, we should copy in the case of sending to our own * ethernet address (thus letting the original actually appear * on the wire). However, we don't do that here for security * reasons and compatibility with the original behavior. */ if ((ifp->if_flags & IFF_SIMPLEX) && ((*m)->m_flags & M_LOOP)) { if (lo_dlt == 0) dlil_find_dltag(APPLE_IF_FAM_LOOPBACK, 0, PF_INET, &lo_dlt); if (lo_dlt) { if ((*m)->m_flags & M_BCAST) { struct mbuf *n = m_copy(*m, 0, (int)M_COPYALL); if (n != NULL) dlil_output(lo_dlt, n, 0, ndest, 0); } else { if (bcmp(edst, ac->ac_enaddr, ETHER_ADDR_LEN) == 0) { dlil_output(lo_dlt, *m, 0, ndest, 0); return EJUSTRETURN; } } } } /* * Add local net header. If no space in first mbuf, * allocate another. */ M_PREPEND(*m, sizeof (struct ether_header), M_DONTWAIT); if (*m == 0) { return (EJUSTRETURN); } eh = mtod(*m, struct ether_header *); (void)memcpy(&eh->ether_type, ether_type, sizeof(eh->ether_type)); (void)memcpy(eh->ether_dhost, edst, 6); (void)memcpy(eh->ether_shost, ac->ac_enaddr, sizeof(eh->ether_shost)); return 0; } static int ether_add_if(struct ifnet *ifp) { u_long i; ifp->if_framer = ether_frameout; ifp->if_demux = ether_demux; ifp->if_event = 0; for (i=0; i < MAX_INTERFACES; i++) if (ether_desc_blk[i].n_count == 0) break; if (i == MAX_INTERFACES) return ENOMEM; ether_desc_blk[i].block_ptr = _MALLOC(ETHER_DESC_BLK_SIZE * sizeof(struct en_desc), M_IFADDR, M_WAITOK); if (ether_desc_blk[i].block_ptr == 0) return ENOMEM; ether_desc_blk[i].n_count = ETHER_DESC_BLK_SIZE; bzero(ether_desc_blk[i].block_ptr, ETHER_DESC_BLK_SIZE * sizeof(struct en_desc)); ifp->family_cookie = i; return 0; } static int ether_del_if(struct ifnet *ifp) { if ((ifp->family_cookie < MAX_INTERFACES) && (ether_desc_blk[ifp->family_cookie].n_count)) { FREE(ether_desc_blk[ifp->family_cookie].block_ptr, M_IFADDR); ether_desc_blk[ifp->family_cookie].block_ptr = NULL; ether_desc_blk[ifp->family_cookie].n_count = 0; ether_desc_blk[ifp->family_cookie].n_max_used = 0; return 0; } else return ENOENT; } int ether_ifmod_ioctl(ifp, command, data) struct ifnet *ifp; u_long command; caddr_t data; { struct rslvmulti_req *rsreq = (struct rslvmulti_req *) data; int error = 0; struct sockaddr_dl *sdl; struct sockaddr_in *sin; u_char *e_addr; switch (command) { case SIOCRSLVMULTI: switch(rsreq->sa->sa_family) { case AF_UNSPEC: /* AppleTalk uses AF_UNSPEC for multicast registration. * No mapping needed. Just check that it's a valid MC address. */ e_addr = &rsreq->sa->sa_data[0]; if ((e_addr[0] & 1) != 1) return EADDRNOTAVAIL; *rsreq->llsa = 0; return EJUSTRETURN; case AF_LINK: /* * No mapping needed. Just check that it's a valid MC address. */ sdl = (struct sockaddr_dl *)rsreq->sa; e_addr = LLADDR(sdl); if ((e_addr[0] & 1) != 1) return EADDRNOTAVAIL; *rsreq->llsa = 0; return EJUSTRETURN; default: return EAFNOSUPPORT; } default: return EOPNOTSUPP; } } int ether_family_init() { int i; struct dlil_ifmod_reg_str ifmod_reg; if (ivedonethis) return 0; ivedonethis = 1; ifmod_reg.add_if = ether_add_if; ifmod_reg.del_if = ether_del_if; ifmod_reg.add_proto = ether_add_proto; ifmod_reg.del_proto = ether_del_proto; ifmod_reg.ifmod_ioctl = ether_ifmod_ioctl; ifmod_reg.shutdown = ether_shutdown; if (dlil_reg_if_modules(APPLE_IF_FAM_ETHERNET, &ifmod_reg)) { printf("WARNING: ether_family_init -- Can't register if family modules\n"); return EIO; } for (i=0; i < MAX_INTERFACES; i++) ether_desc_blk[i].n_count = 0; return 0; } |