1 /* 2 * Copyright (c) 1982, 1989, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * @(#)if_ethersubr.c 8.1 (Berkeley) 6/10/93 34 * $Id: if_ethersubr.c,v 1.55 1999/01/31 08:17:16 julian Exp $ 35 */ 36 37 #include "opt_atalk.h" 38 #include "opt_inet.h" 39 #include "opt_ipx.h" 40 #include "opt_bdg.h" 41 42 #include <sys/param.h> 43 #include <sys/systm.h> 44 #include <sys/kernel.h> 45 #include <sys/malloc.h> 46 #include <sys/mbuf.h> 47 #include <sys/socket.h> 48 #include <sys/sockio.h> 49 #include <sys/sysctl.h> 50 51 #include <net/if.h> 52 #include <net/netisr.h> 53 #include <net/route.h> 54 #include <net/if_llc.h> 55 #include <net/if_dl.h> 56 #include <net/if_types.h> 57 58 #ifdef INET 59 #include <netinet/in.h> 60 #include <netinet/in_var.h> 61 #include <netinet/if_ether.h> 62 #endif 63 64 #ifdef IPX 65 #include <netipx/ipx.h> 66 #include <netipx/ipx_if.h> 67 #endif 68 69 #ifdef NS 70 #include <netns/ns.h> 71 #include <netns/ns_if.h> 72 ushort ns_nettype; 73 int ether_outputdebug = 0; 74 int ether_inputdebug = 0; 75 #endif 76 77 #ifdef ISO 78 #include <netiso/argo_debug.h> 79 #include <netiso/iso.h> 80 #include <netiso/iso_var.h> 81 #include <netiso/iso_snpac.h> 82 #endif 83 84 /*#ifdef LLC 85 #include <netccitt/dll.h> 86 #include <netccitt/llc_var.h> 87 #endif*/ 88 89 #if defined(LLC) && defined(CCITT) 90 extern struct ifqueue pkintrq; 91 #endif 92 93 #ifdef NETATALK 94 #include <netatalk/at.h> 95 #include <netatalk/at_var.h> 96 #include <netatalk/at_extern.h> 97 98 #define llc_snap_org_code llc_un.type_snap.org_code 99 #define llc_snap_ether_type llc_un.type_snap.ether_type 100 101 extern u_char at_org_code[3]; 102 extern u_char aarp_org_code[3]; 103 #endif /* NETATALK */ 104 105 #ifdef BRIDGE 106 #include <net/bridge.h> 107 #endif 108 109 #include "vlan.h" 110 #if NVLAN > 0 111 #include <net/if_vlan_var.h> 112 #endif /* NVLAN > 0 */ 113 114 static int ether_resolvemulti __P((struct ifnet *, struct sockaddr **, 115 struct sockaddr *)); 116 u_char etherbroadcastaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 117 #define senderr(e) do { error = (e); goto bad;} while (0) 118 #define IFP2AC(IFP) ((struct arpcom *)IFP) 119 120 /* 121 * Ethernet output routine. 122 * Encapsulate a packet of type family for the local net. 123 * Use trailer local net encapsulation if enough data in first 124 * packet leaves a multiple of 512 bytes of data in remainder. 125 * Assumes that ifp is actually pointer to arpcom structure. 126 */ 127 int 128 ether_output(ifp, m0, dst, rt0) 129 register struct ifnet *ifp; 130 struct mbuf *m0; 131 struct sockaddr *dst; 132 struct rtentry *rt0; 133 { 134 short type; 135 int s, error = 0; 136 u_char edst[6]; 137 register struct mbuf *m = m0; 138 register struct rtentry *rt; 139 register struct ether_header *eh; 140 int off, len = m->m_pkthdr.len, loop_copy = 0; 141 int hlen; /* link layer header lenght */ 142 struct arpcom *ac = IFP2AC(ifp); 143 144 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) 145 senderr(ENETDOWN); 146 rt = rt0; 147 if (rt) { 148 if ((rt->rt_flags & RTF_UP) == 0) { 149 rt0 = rt = rtalloc1(dst, 1, 0UL); 150 if (rt0) 151 rt->rt_refcnt--; 152 else 153 senderr(EHOSTUNREACH); 154 } 155 if (rt->rt_flags & RTF_GATEWAY) { 156 if (rt->rt_gwroute == 0) 157 goto lookup; 158 if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) { 159 rtfree(rt); rt = rt0; 160 lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1, 161 0UL); 162 if ((rt = rt->rt_gwroute) == 0) 163 senderr(EHOSTUNREACH); 164 } 165 } 166 if (rt->rt_flags & RTF_REJECT) 167 if (rt->rt_rmx.rmx_expire == 0 || 168 time_second < rt->rt_rmx.rmx_expire) 169 senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH); 170 } 171 hlen = ETHER_HDR_LEN; 172 switch (dst->sa_family) { 173 #ifdef INET 174 case AF_INET: 175 if (!arpresolve(ac, rt, m, dst, edst, rt0)) 176 return (0); /* if not yet resolved */ 177 off = m->m_pkthdr.len - m->m_len; 178 type = htons(ETHERTYPE_IP); 179 break; 180 #endif 181 #ifdef IPX 182 case AF_IPX: 183 type = htons(ETHERTYPE_IPX); 184 bcopy((caddr_t)&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host), 185 (caddr_t)edst, sizeof (edst)); 186 break; 187 #endif 188 #ifdef NETATALK 189 case AF_APPLETALK: 190 { 191 struct at_ifaddr *aa; 192 193 if ((aa = at_ifawithnet((struct sockaddr_at *)dst)) == NULL) { 194 goto bad; 195 } 196 if (!aarpresolve(ac, m, (struct sockaddr_at *)dst, edst)) 197 return (0); 198 /* 199 * In the phase 2 case, need to prepend an mbuf for the llc header. 200 * Since we must preserve the value of m, which is passed to us by 201 * value, we m_copy() the first mbuf, and use it for our llc header. 202 */ 203 if ( aa->aa_flags & AFA_PHASE2 ) { 204 struct llc llc; 205 206 M_PREPEND(m, sizeof(struct llc), M_WAIT); 207 len += sizeof(struct llc); 208 llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP; 209 llc.llc_control = LLC_UI; 210 bcopy(at_org_code, llc.llc_snap_org_code, sizeof(at_org_code)); 211 llc.llc_snap_ether_type = htons( ETHERTYPE_AT ); 212 bcopy(&llc, mtod(m, caddr_t), sizeof(struct llc)); 213 type = htons(m->m_pkthdr.len); 214 hlen = sizeof(struct llc) + ETHER_HDR_LEN; 215 } else { 216 type = htons(ETHERTYPE_AT); 217 } 218 break; 219 } 220 #endif NETATALK 221 #ifdef NS 222 case AF_NS: 223 switch(ns_nettype){ 224 default: 225 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */ 226 type = 0x8137; 227 break; 228 case 0x0: /* Novell 802.3 */ 229 type = htons( m->m_pkthdr.len); 230 break; 231 case 0xe0e0: /* Novell 802.2 and Token-Ring */ 232 M_PREPEND(m, 3, M_WAIT); 233 type = htons( m->m_pkthdr.len); 234 cp = mtod(m, u_char *); 235 *cp++ = 0xE0; 236 *cp++ = 0xE0; 237 *cp++ = 0x03; 238 break; 239 } 240 bcopy((caddr_t)&(((struct sockaddr_ns *)dst)->sns_addr.x_host), 241 (caddr_t)edst, sizeof (edst)); 242 /* 243 * XXX if ns_thishost is the same as the node's ethernet 244 * address then just the default code will catch this anyhow. 245 * So I'm not sure if this next clause should be here at all? 246 * [JRE] 247 */ 248 if (!bcmp((caddr_t)edst, (caddr_t)&ns_thishost, sizeof(edst))){ 249 m->m_pkthdr.rcvif = ifp; 250 schednetisr(NETISR_NS); 251 inq = &nsintrq; 252 s = splimp(); 253 if (IF_QFULL(inq)) { 254 IF_DROP(inq); 255 m_freem(m); 256 } else 257 IF_ENQUEUE(inq, m); 258 splx(s); 259 return (error); 260 } 261 if (!bcmp((caddr_t)edst, (caddr_t)&ns_broadhost, sizeof(edst))){ 262 m->m_flags |= M_BCAST; 263 } 264 break; 265 #endif /* NS */ 266 #ifdef ISO 267 case AF_ISO: { 268 int snpalen; 269 struct llc *l; 270 register struct sockaddr_dl *sdl; 271 272 if (rt && (sdl = (struct sockaddr_dl *)rt->rt_gateway) && 273 sdl->sdl_family == AF_LINK && sdl->sdl_alen > 0) { 274 bcopy(LLADDR(sdl), (caddr_t)edst, sizeof(edst)); 275 } else if (error = 276 iso_snparesolve(ifp, (struct sockaddr_iso *)dst, 277 (char *)edst, &snpalen)) 278 goto bad; /* Not Resolved */ 279 /* If broadcasting on a simplex interface, loopback a copy */ 280 if (*edst & 1) 281 m->m_flags |= (M_BCAST|M_MCAST); 282 M_PREPEND(m, 3, M_DONTWAIT); 283 if (m == NULL) 284 return (0); 285 type = htons(m->m_pkthdr.len); 286 l = mtod(m, struct llc *); 287 l->llc_dsap = l->llc_ssap = LLC_ISO_LSAP; 288 l->llc_control = LLC_UI; 289 len += 3; 290 IFDEBUG(D_ETHER) 291 int i; 292 printf("unoutput: sending pkt to: "); 293 for (i=0; i<6; i++) 294 printf("%x ", edst[i] & 0xff); 295 printf("\n"); 296 ENDDEBUG 297 } break; 298 #endif /* ISO */ 299 #ifdef LLC 300 /* case AF_NSAP: */ 301 case AF_CCITT: { 302 register struct sockaddr_dl *sdl = 303 (struct sockaddr_dl *) rt -> rt_gateway; 304 305 if (sdl && sdl->sdl_family == AF_LINK 306 && sdl->sdl_alen > 0) { 307 bcopy(LLADDR(sdl), (char *)edst, sizeof(edst)); 308 } else goto bad; /* Not a link interface ? Funny ... */ 309 if (*edst & 1) 310 loop_copy = 1; 311 type = htons(m->m_pkthdr.len); 312 #ifdef LLC_DEBUG 313 { 314 int i; 315 register struct llc *l = mtod(m, struct llc *); 316 317 printf("ether_output: sending LLC2 pkt to: "); 318 for (i=0; i<6; i++) 319 printf("%x ", edst[i] & 0xff); 320 printf(" len 0x%x dsap 0x%x ssap 0x%x control 0x%x\n", 321 type & 0xff, l->llc_dsap & 0xff, l->llc_ssap &0xff, 322 l->llc_control & 0xff); 323 324 } 325 #endif /* LLC_DEBUG */ 326 } break; 327 #endif /* LLC */ 328 329 case AF_UNSPEC: 330 loop_copy = -1; /* if this is for us, don't do it */ 331 eh = (struct ether_header *)dst->sa_data; 332 (void)memcpy(edst, eh->ether_dhost, sizeof (edst)); 333 type = eh->ether_type; 334 break; 335 336 default: 337 printf("%s%d: can't handle af%d\n", ifp->if_name, ifp->if_unit, 338 dst->sa_family); 339 senderr(EAFNOSUPPORT); 340 } 341 342 /* 343 * Add local net header. If no space in first mbuf, 344 * allocate another. 345 */ 346 M_PREPEND(m, sizeof (struct ether_header), M_DONTWAIT); 347 if (m == 0) 348 senderr(ENOBUFS); 349 eh = mtod(m, struct ether_header *); 350 (void)memcpy(&eh->ether_type, &type, 351 sizeof(eh->ether_type)); 352 (void)memcpy(eh->ether_dhost, edst, sizeof (edst)); 353 (void)memcpy(eh->ether_shost, ac->ac_enaddr, 354 sizeof(eh->ether_shost)); 355 356 /* 357 * If a simplex interface, and the packet is being sent to our 358 * Ethernet address or a broadcast address, loopback a copy. 359 * XXX To make a simplex device behave exactly like a duplex 360 * device, we should copy in the case of sending to our own 361 * ethernet address (thus letting the original actually appear 362 * on the wire). However, we don't do that here for security 363 * reasons and compatibility with the original behavior. 364 */ 365 if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) { 366 if ((m->m_flags & M_BCAST) || (loop_copy > 0)) { 367 struct mbuf *n = m_copy(m, 0, (int)M_COPYALL); 368 369 (void) if_simloop(ifp, n, dst, hlen); 370 } else if (bcmp(eh->ether_dhost, 371 eh->ether_shost, ETHER_ADDR_LEN) == 0) { 372 (void) if_simloop(ifp, m, dst, hlen); 373 return (0); /* XXX */ 374 } 375 } 376 #ifdef BRIDGE 377 if (do_bridge) { 378 struct mbuf *m0 = m ; 379 380 if (m->m_pkthdr.rcvif) 381 m->m_pkthdr.rcvif = NULL ; 382 ifp = bridge_dst_lookup(m); 383 bdg_forward(&m0, ifp); 384 if (m0) 385 m_freem(m0); 386 return (0); 387 } 388 #endif 389 s = splimp(); 390 /* 391 * Queue message on interface, and start output if interface 392 * not yet active. 393 */ 394 if (IF_QFULL(&ifp->if_snd)) { 395 IF_DROP(&ifp->if_snd); 396 splx(s); 397 senderr(ENOBUFS); 398 } 399 IF_ENQUEUE(&ifp->if_snd, m); 400 if ((ifp->if_flags & IFF_OACTIVE) == 0) 401 (*ifp->if_start)(ifp); 402 splx(s); 403 ifp->if_obytes += len + sizeof (struct ether_header); 404 if (m->m_flags & M_MCAST) 405 ifp->if_omcasts++; 406 return (error); 407 408 bad: 409 if (m) 410 m_freem(m); 411 return (error); 412 } 413 414 /* 415 * Process a received Ethernet packet; 416 * the packet is in the mbuf chain m without 417 * the ether header, which is provided separately. 418 */ 419 void 420 ether_input(ifp, eh, m) 421 struct ifnet *ifp; 422 register struct ether_header *eh; 423 struct mbuf *m; 424 { 425 register struct ifqueue *inq; 426 u_short ether_type; 427 int s; 428 #if defined (ISO) || defined (LLC) || defined(NETATALK) 429 register struct llc *l; 430 #endif 431 432 if ((ifp->if_flags & IFF_UP) == 0) { 433 m_freem(m); 434 return; 435 } 436 ifp->if_ibytes += m->m_pkthdr.len + sizeof (*eh); 437 if (eh->ether_dhost[0] & 1) { 438 if (bcmp((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost, 439 sizeof(etherbroadcastaddr)) == 0) 440 m->m_flags |= M_BCAST; 441 else 442 m->m_flags |= M_MCAST; 443 } 444 if (m->m_flags & (M_BCAST|M_MCAST)) 445 ifp->if_imcasts++; 446 447 ether_type = ntohs(eh->ether_type); 448 449 #if NVLAN > 0 450 if (ether_type == vlan_proto) { 451 if (vlan_input(eh, m) < 0) 452 ifp->if_data.ifi_noproto++; 453 return; 454 } 455 #endif /* NVLAN > 0 */ 456 457 switch (ether_type) { 458 #ifdef INET 459 case ETHERTYPE_IP: 460 if (ipflow_fastforward(m)) 461 return; 462 schednetisr(NETISR_IP); 463 inq = &ipintrq; 464 break; 465 466 case ETHERTYPE_ARP: 467 schednetisr(NETISR_ARP); 468 inq = &arpintrq; 469 break; 470 #endif 471 #ifdef IPX 472 case ETHERTYPE_IPX: 473 schednetisr(NETISR_IPX); 474 inq = &ipxintrq; 475 break; 476 #endif 477 #ifdef NS 478 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */ 479 schednetisr(NETISR_NS); 480 inq = &nsintrq; 481 break; 482 483 #endif /* NS */ 484 #ifdef NETATALK 485 case ETHERTYPE_AT: 486 schednetisr(NETISR_ATALK); 487 inq = &atintrq1; 488 break; 489 case ETHERTYPE_AARP: 490 /* probably this should be done with a NETISR as well */ 491 aarpinput(IFP2AC(ifp), m); /* XXX */ 492 return; 493 #endif NETATALK 494 default: 495 #ifdef NS 496 checksum = mtod(m, ushort *); 497 /* Novell 802.3 */ 498 if ((ether_type <= ETHERMTU) && 499 ((*checksum == 0xffff) || (*checksum == 0xE0E0))){ 500 if(*checksum == 0xE0E0) { 501 m->m_pkthdr.len -= 3; 502 m->m_len -= 3; 503 m->m_data += 3; 504 } 505 schednetisr(NETISR_NS); 506 inq = &nsintrq; 507 break; 508 } 509 #endif /* NS */ 510 #if defined (ISO) || defined (LLC) || defined(NETATALK) 511 if (ether_type > ETHERMTU) 512 goto dropanyway; 513 l = mtod(m, struct llc *); 514 switch (l->llc_dsap) { 515 #ifdef NETATALK 516 case LLC_SNAP_LSAP: 517 switch (l->llc_control) { 518 case LLC_UI: 519 if (l->llc_ssap != LLC_SNAP_LSAP) 520 goto dropanyway; 521 522 if (Bcmp(&(l->llc_snap_org_code)[0], at_org_code, 523 sizeof(at_org_code)) == 0 && 524 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) { 525 inq = &atintrq2; 526 m_adj( m, sizeof( struct llc )); 527 schednetisr(NETISR_ATALK); 528 break; 529 } 530 531 if (Bcmp(&(l->llc_snap_org_code)[0], aarp_org_code, 532 sizeof(aarp_org_code)) == 0 && 533 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) { 534 m_adj( m, sizeof( struct llc )); 535 aarpinput(IFP2AC(ifp), m); /* XXX */ 536 return; 537 } 538 539 default: 540 goto dropanyway; 541 } 542 break; 543 #endif NETATALK 544 #ifdef ISO 545 case LLC_ISO_LSAP: 546 switch (l->llc_control) { 547 case LLC_UI: 548 /* LLC_UI_P forbidden in class 1 service */ 549 if ((l->llc_dsap == LLC_ISO_LSAP) && 550 (l->llc_ssap == LLC_ISO_LSAP)) { 551 /* LSAP for ISO */ 552 if (m->m_pkthdr.len > ether_type) 553 m_adj(m, ether_type - m->m_pkthdr.len); 554 m->m_data += 3; /* XXX */ 555 m->m_len -= 3; /* XXX */ 556 m->m_pkthdr.len -= 3; /* XXX */ 557 M_PREPEND(m, sizeof *eh, M_DONTWAIT); 558 if (m == 0) 559 return; 560 *mtod(m, struct ether_header *) = *eh; 561 IFDEBUG(D_ETHER) 562 printf("clnp packet"); 563 ENDDEBUG 564 schednetisr(NETISR_ISO); 565 inq = &clnlintrq; 566 break; 567 } 568 goto dropanyway; 569 570 case LLC_XID: 571 case LLC_XID_P: 572 if(m->m_len < 6) 573 goto dropanyway; 574 l->llc_window = 0; 575 l->llc_fid = 9; 576 l->llc_class = 1; 577 l->llc_dsap = l->llc_ssap = 0; 578 /* Fall through to */ 579 case LLC_TEST: 580 case LLC_TEST_P: 581 { 582 struct sockaddr sa; 583 register struct ether_header *eh2; 584 int i; 585 u_char c = l->llc_dsap; 586 587 l->llc_dsap = l->llc_ssap; 588 l->llc_ssap = c; 589 if (m->m_flags & (M_BCAST | M_MCAST)) 590 bcopy((caddr_t)ac->ac_enaddr, 591 (caddr_t)eh->ether_dhost, 6); 592 sa.sa_family = AF_UNSPEC; 593 sa.sa_len = sizeof(sa); 594 eh2 = (struct ether_header *)sa.sa_data; 595 for (i = 0; i < 6; i++) { 596 eh2->ether_shost[i] = c = eh->ether_dhost[i]; 597 eh2->ether_dhost[i] = 598 eh->ether_dhost[i] = eh->ether_shost[i]; 599 eh->ether_shost[i] = c; 600 } 601 ifp->if_output(ifp, m, &sa, NULL); 602 return; 603 } 604 default: 605 m_freem(m); 606 return; 607 } 608 break; 609 #endif /* ISO */ 610 #ifdef LLC 611 case LLC_X25_LSAP: 612 { 613 if (m->m_pkthdr.len > ether_type) 614 m_adj(m, ether_type - m->m_pkthdr.len); 615 M_PREPEND(m, sizeof(struct sdl_hdr) , M_DONTWAIT); 616 if (m == 0) 617 return; 618 if ( !sdl_sethdrif(ifp, eh->ether_shost, LLC_X25_LSAP, 619 eh->ether_dhost, LLC_X25_LSAP, 6, 620 mtod(m, struct sdl_hdr *))) 621 panic("ETHER cons addr failure"); 622 mtod(m, struct sdl_hdr *)->sdlhdr_len = ether_type; 623 #ifdef LLC_DEBUG 624 printf("llc packet\n"); 625 #endif /* LLC_DEBUG */ 626 schednetisr(NETISR_CCITT); 627 inq = &llcintrq; 628 break; 629 } 630 #endif /* LLC */ 631 dropanyway: 632 default: 633 m_freem(m); 634 return; 635 } 636 #else /* ISO || LLC || NETATALK */ 637 m_freem(m); 638 return; 639 #endif /* ISO || LLC || NETATALK */ 640 } 641 642 s = splimp(); 643 if (IF_QFULL(inq)) { 644 IF_DROP(inq); 645 m_freem(m); 646 } else 647 IF_ENQUEUE(inq, m); 648 splx(s); 649 } 650 651 /* 652 * Perform common duties while attaching to interface list 653 */ 654 void 655 ether_ifattach(ifp) 656 register struct ifnet *ifp; 657 { 658 register struct ifaddr *ifa; 659 register struct sockaddr_dl *sdl; 660 661 ifp->if_type = IFT_ETHER; 662 ifp->if_addrlen = 6; 663 ifp->if_hdrlen = 14; 664 ifp->if_mtu = ETHERMTU; 665 ifp->if_resolvemulti = ether_resolvemulti; 666 if (ifp->if_baudrate == 0) 667 ifp->if_baudrate = 10000000; 668 ifa = ifnet_addrs[ifp->if_index - 1]; 669 if (ifa == 0) { 670 printf("ether_ifattach: no lladdr!\n"); 671 return; 672 } 673 sdl = (struct sockaddr_dl *)ifa->ifa_addr; 674 sdl->sdl_type = IFT_ETHER; 675 sdl->sdl_alen = ifp->if_addrlen; 676 bcopy((IFP2AC(ifp))->ac_enaddr, LLADDR(sdl), ifp->if_addrlen); 677 #ifdef NETGRAPH 678 ngether_init(ifp); 679 #endif /* NETGRAPH */ 680 } 681 682 SYSCTL_DECL(_net_link); 683 SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet"); 684 685 int 686 ether_ioctl(ifp, command, data) 687 struct ifnet *ifp; 688 int command; 689 caddr_t data; 690 { 691 struct ifaddr *ifa = (struct ifaddr *) data; 692 struct ifreq *ifr = (struct ifreq *) data; 693 int error = 0; 694 695 switch (command) { 696 case SIOCSIFADDR: 697 ifp->if_flags |= IFF_UP; 698 699 switch (ifa->ifa_addr->sa_family) { 700 #ifdef INET 701 case AF_INET: 702 ifp->if_init(ifp->if_softc); /* before arpwhohas */ 703 arp_ifinit(IFP2AC(ifp), ifa); 704 break; 705 #endif 706 #ifdef IPX 707 /* 708 * XXX - This code is probably wrong 709 */ 710 case AF_IPX: 711 { 712 register struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr); 713 struct arpcom *ac = IFP2AC(ifp); 714 715 if (ipx_nullhost(*ina)) 716 ina->x_host = 717 *(union ipx_host *) 718 ac->ac_enaddr; 719 else { 720 bcopy((caddr_t) ina->x_host.c_host, 721 (caddr_t) ac->ac_enaddr, 722 sizeof(ac->ac_enaddr)); 723 } 724 725 /* 726 * Set new address 727 */ 728 ifp->if_init(ifp->if_softc); 729 break; 730 } 731 #endif 732 #ifdef NS 733 /* 734 * XXX - This code is probably wrong 735 */ 736 case AF_NS: 737 { 738 register struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr); 739 struct arpcom *ac = IFP2AC(ifp); 740 741 if (ns_nullhost(*ina)) 742 ina->x_host = 743 *(union ns_host *) (ac->ac_enaddr); 744 else { 745 bcopy((caddr_t) ina->x_host.c_host, 746 (caddr_t) ac->ac_enaddr, 747 sizeof(ac->ac_enaddr)); 748 } 749 750 /* 751 * Set new address 752 */ 753 ifp->if_init(ifp->if_softc); 754 break; 755 } 756 #endif 757 default: 758 ifp->if_init(ifp->if_softc); 759 break; 760 } 761 break; 762 763 case SIOCGIFADDR: 764 { 765 struct sockaddr *sa; 766 767 sa = (struct sockaddr *) & ifr->ifr_data; 768 bcopy(IFP2AC(ifp)->ac_enaddr, 769 (caddr_t) sa->sa_data, ETHER_ADDR_LEN); 770 } 771 break; 772 773 case SIOCSIFMTU: 774 /* 775 * Set the interface MTU. 776 */ 777 if (ifr->ifr_mtu > ETHERMTU) { 778 error = EINVAL; 779 } else { 780 ifp->if_mtu = ifr->ifr_mtu; 781 } 782 break; 783 } 784 return (error); 785 } 786 787 int 788 ether_resolvemulti(ifp, llsa, sa) 789 struct ifnet *ifp; 790 struct sockaddr **llsa; 791 struct sockaddr *sa; 792 { 793 struct sockaddr_dl *sdl; 794 struct sockaddr_in *sin; 795 u_char *e_addr; 796 797 switch(sa->sa_family) { 798 case AF_LINK: 799 /* 800 * No mapping needed. Just check that it's a valid MC address. 801 */ 802 sdl = (struct sockaddr_dl *)sa; 803 e_addr = LLADDR(sdl); 804 if ((e_addr[0] & 1) != 1) 805 return EADDRNOTAVAIL; 806 *llsa = 0; 807 return 0; 808 809 #ifdef INET 810 case AF_INET: 811 sin = (struct sockaddr_in *)sa; 812 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) 813 return EADDRNOTAVAIL; 814 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR, 815 M_WAITOK); 816 sdl->sdl_len = sizeof *sdl; 817 sdl->sdl_family = AF_LINK; 818 sdl->sdl_index = ifp->if_index; 819 sdl->sdl_type = IFT_ETHER; 820 sdl->sdl_nlen = 0; 821 sdl->sdl_alen = ETHER_ADDR_LEN; 822 sdl->sdl_slen = 0; 823 e_addr = LLADDR(sdl); 824 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr); 825 *llsa = (struct sockaddr *)sdl; 826 return 0; 827 #endif 828 829 default: 830 /* 831 * Well, the text isn't quite right, but it's the name 832 * that counts... 833 */ 834 return EAFNOSUPPORT; 835 } 836 } 837