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.52 1998/08/04 23:17:05 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) { error = (e); goto bad;} 118 119 /* 120 * Ethernet output routine. 121 * Encapsulate a packet of type family for the local net. 122 * Use trailer local net encapsulation if enough data in first 123 * packet leaves a multiple of 512 bytes of data in remainder. 124 * Assumes that ifp is actually pointer to arpcom structure. 125 */ 126 int 127 ether_output(ifp, m0, dst, rt0) 128 register struct ifnet *ifp; 129 struct mbuf *m0; 130 struct sockaddr *dst; 131 struct rtentry *rt0; 132 { 133 short type; 134 int s, error = 0; 135 u_char edst[6]; 136 register struct mbuf *m = m0; 137 register struct rtentry *rt; 138 register struct ether_header *eh; 139 int off, len = m->m_pkthdr.len, loop_copy = 0; 140 int hlen; /* link layer header lenght */ 141 struct arpcom *ac = (struct arpcom *)ifp; 142 143 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) 144 senderr(ENETDOWN); 145 rt = rt0; 146 if (rt) { 147 if ((rt->rt_flags & RTF_UP) == 0) { 148 rt0 = rt = rtalloc1(dst, 1, 0UL); 149 if (rt0) 150 rt->rt_refcnt--; 151 else 152 senderr(EHOSTUNREACH); 153 } 154 if (rt->rt_flags & RTF_GATEWAY) { 155 if (rt->rt_gwroute == 0) 156 goto lookup; 157 if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) { 158 rtfree(rt); rt = rt0; 159 lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1, 160 0UL); 161 if ((rt = rt->rt_gwroute) == 0) 162 senderr(EHOSTUNREACH); 163 } 164 } 165 if (rt->rt_flags & RTF_REJECT) 166 if (rt->rt_rmx.rmx_expire == 0 || 167 time_second < rt->rt_rmx.rmx_expire) 168 senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH); 169 } 170 hlen = ETHER_HDR_LEN; 171 switch (dst->sa_family) { 172 #ifdef INET 173 case AF_INET: 174 if (!arpresolve(ac, rt, m, dst, edst, rt0)) 175 return (0); /* if not yet resolved */ 176 off = m->m_pkthdr.len - m->m_len; 177 type = htons(ETHERTYPE_IP); 178 break; 179 #endif 180 #ifdef IPX 181 case AF_IPX: 182 type = htons(ETHERTYPE_IPX); 183 bcopy((caddr_t)&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host), 184 (caddr_t)edst, sizeof (edst)); 185 break; 186 #endif 187 #ifdef NETATALK 188 case AF_APPLETALK: 189 { 190 struct at_ifaddr *aa; 191 192 if ((aa = at_ifawithnet((struct sockaddr_at *)dst)) == NULL) { 193 goto bad; 194 } 195 if (!aarpresolve(ac, m, (struct sockaddr_at *)dst, edst)) 196 return (0); 197 /* 198 * In the phase 2 case, need to prepend an mbuf for the llc header. 199 * Since we must preserve the value of m, which is passed to us by 200 * value, we m_copy() the first mbuf, and use it for our llc header. 201 */ 202 if ( aa->aa_flags & AFA_PHASE2 ) { 203 struct llc llc; 204 205 M_PREPEND(m, sizeof(struct llc), M_WAIT); 206 len += sizeof(struct llc); 207 llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP; 208 llc.llc_control = LLC_UI; 209 bcopy(at_org_code, llc.llc_snap_org_code, sizeof(at_org_code)); 210 llc.llc_snap_ether_type = htons( ETHERTYPE_AT ); 211 bcopy(&llc, mtod(m, caddr_t), sizeof(struct llc)); 212 type = htons(m->m_pkthdr.len); 213 hlen = sizeof(struct llc) + ETHER_HDR_LEN; 214 } else { 215 type = htons(ETHERTYPE_AT); 216 } 217 break; 218 } 219 #endif NETATALK 220 #ifdef NS 221 case AF_NS: 222 switch(ns_nettype){ 223 default: 224 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */ 225 type = 0x8137; 226 break; 227 case 0x0: /* Novell 802.3 */ 228 type = htons( m->m_pkthdr.len); 229 break; 230 case 0xe0e0: /* Novell 802.2 and Token-Ring */ 231 M_PREPEND(m, 3, M_WAIT); 232 type = htons( m->m_pkthdr.len); 233 cp = mtod(m, u_char *); 234 *cp++ = 0xE0; 235 *cp++ = 0xE0; 236 *cp++ = 0x03; 237 break; 238 } 239 bcopy((caddr_t)&(((struct sockaddr_ns *)dst)->sns_addr.x_host), 240 (caddr_t)edst, sizeof (edst)); 241 if (!bcmp((caddr_t)edst, (caddr_t)&ns_thishost, sizeof(edst))){ 242 m->m_pkthdr.rcvif = ifp; 243 schednetisr(NETISR_NS); 244 inq = &nsintrq; 245 s = splimp(); 246 if (IF_QFULL(inq)) { 247 IF_DROP(inq); 248 m_freem(m); 249 } else 250 IF_ENQUEUE(inq, m); 251 splx(s); 252 return (error); 253 } 254 if (!bcmp((caddr_t)edst, (caddr_t)&ns_broadhost, sizeof(edst))){ 255 m2 = m_copy(m, 0, (int)M_COPYALL); 256 m2->m_pkthdr.rcvif = ifp; 257 schednetisr(NETISR_NS); 258 inq = &nsintrq; 259 s = splimp(); 260 if (IF_QFULL(inq)) { 261 IF_DROP(inq); 262 m_freem(m2); 263 } else 264 IF_ENQUEUE(inq, m2); 265 splx(s); 266 } 267 break; 268 #endif /* NS */ 269 #ifdef ISO 270 case AF_ISO: { 271 int snpalen; 272 struct llc *l; 273 register struct sockaddr_dl *sdl; 274 275 if (rt && (sdl = (struct sockaddr_dl *)rt->rt_gateway) && 276 sdl->sdl_family == AF_LINK && sdl->sdl_alen > 0) { 277 bcopy(LLADDR(sdl), (caddr_t)edst, sizeof(edst)); 278 } else if (error = 279 iso_snparesolve(ifp, (struct sockaddr_iso *)dst, 280 (char *)edst, &snpalen)) 281 goto bad; /* Not Resolved */ 282 /* If broadcasting on a simplex interface, loopback a copy */ 283 if (*edst & 1) 284 m->m_flags |= (M_BCAST|M_MCAST); 285 M_PREPEND(m, 3, M_DONTWAIT); 286 if (m == NULL) 287 return (0); 288 type = htons(m->m_pkthdr.len); 289 l = mtod(m, struct llc *); 290 l->llc_dsap = l->llc_ssap = LLC_ISO_LSAP; 291 l->llc_control = LLC_UI; 292 len += 3; 293 IFDEBUG(D_ETHER) 294 int i; 295 printf("unoutput: sending pkt to: "); 296 for (i=0; i<6; i++) 297 printf("%x ", edst[i] & 0xff); 298 printf("\n"); 299 ENDDEBUG 300 } break; 301 #endif /* ISO */ 302 #ifdef LLC 303 /* case AF_NSAP: */ 304 case AF_CCITT: { 305 register struct sockaddr_dl *sdl = 306 (struct sockaddr_dl *) rt -> rt_gateway; 307 308 if (sdl && sdl->sdl_family == AF_LINK 309 && sdl->sdl_alen > 0) { 310 bcopy(LLADDR(sdl), (char *)edst, sizeof(edst)); 311 } else goto bad; /* Not a link interface ? Funny ... */ 312 if (*edst & 1) 313 loop_copy = 1; 314 type = htons(m->m_pkthdr.len); 315 #ifdef LLC_DEBUG 316 { 317 int i; 318 register struct llc *l = mtod(m, struct llc *); 319 320 printf("ether_output: sending LLC2 pkt to: "); 321 for (i=0; i<6; i++) 322 printf("%x ", edst[i] & 0xff); 323 printf(" len 0x%x dsap 0x%x ssap 0x%x control 0x%x\n", 324 type & 0xff, l->llc_dsap & 0xff, l->llc_ssap &0xff, 325 l->llc_control & 0xff); 326 327 } 328 #endif /* LLC_DEBUG */ 329 } break; 330 #endif /* LLC */ 331 332 case AF_UNSPEC: 333 loop_copy = -1; /* if this is for us, don't do it */ 334 eh = (struct ether_header *)dst->sa_data; 335 (void)memcpy(edst, eh->ether_dhost, sizeof (edst)); 336 type = eh->ether_type; 337 break; 338 339 default: 340 printf("%s%d: can't handle af%d\n", ifp->if_name, ifp->if_unit, 341 dst->sa_family); 342 senderr(EAFNOSUPPORT); 343 } 344 345 /* 346 * Add local net header. If no space in first mbuf, 347 * allocate another. 348 */ 349 M_PREPEND(m, sizeof (struct ether_header), M_DONTWAIT); 350 if (m == 0) 351 senderr(ENOBUFS); 352 eh = mtod(m, struct ether_header *); 353 (void)memcpy(&eh->ether_type, &type, 354 sizeof(eh->ether_type)); 355 (void)memcpy(eh->ether_dhost, edst, sizeof (edst)); 356 (void)memcpy(eh->ether_shost, ac->ac_enaddr, 357 sizeof(eh->ether_shost)); 358 359 /* 360 * If a simplex interface, and the packet is being sent to our 361 * Ethernet address or a broadcast address, loopback a copy. 362 * XXX To make a simplex device behave exactly like a duplex 363 * device, we should copy in the case of sending to our own 364 * ethernet address (thus letting the original actually appear 365 * on the wire). However, we don't do that here for security 366 * reasons and compatibility with the original behavior. 367 */ 368 if ((ifp->if_flags & IFF_SIMPLEX) && 369 (loop_copy != -1)) { 370 if ((m->m_flags & M_BCAST) || (loop_copy > 0)) { 371 struct mbuf *n = m_copy(m, 0, (int)M_COPYALL); 372 373 (void) if_simloop(ifp, n, dst, hlen); 374 } else if (bcmp(eh->ether_dhost, 375 eh->ether_shost, ETHER_ADDR_LEN) == 0) { 376 (void) if_simloop(ifp, m, dst, hlen); 377 return(0); /* XXX */ 378 } 379 } 380 #ifdef BRIDGE 381 if (do_bridge) { 382 struct ifnet *old_ifp = ifp ; 383 struct mbuf *m0 = m ; 384 if (m->m_pkthdr.rcvif ) 385 m->m_pkthdr.rcvif = NULL ; 386 ifp = bridge_dst_lookup(m); 387 bdg_forward(&m0, ifp); 388 if (m0) 389 m_freem(m0); 390 return 0 ; 391 } 392 #endif 393 s = splimp(); 394 /* 395 * Queue message on interface, and start output if interface 396 * not yet active. 397 */ 398 if (IF_QFULL(&ifp->if_snd)) { 399 IF_DROP(&ifp->if_snd); 400 splx(s); 401 senderr(ENOBUFS); 402 } 403 IF_ENQUEUE(&ifp->if_snd, m); 404 if ((ifp->if_flags & IFF_OACTIVE) == 0) 405 (*ifp->if_start)(ifp); 406 splx(s); 407 ifp->if_obytes += len + sizeof (struct ether_header); 408 if (m->m_flags & M_MCAST) 409 ifp->if_omcasts++; 410 return (error); 411 412 bad: 413 if (m) 414 m_freem(m); 415 return (error); 416 } 417 418 /* 419 * Process a received Ethernet packet; 420 * the packet is in the mbuf chain m without 421 * the ether header, which is provided separately. 422 */ 423 void 424 ether_input(ifp, eh, m) 425 struct ifnet *ifp; 426 register struct ether_header *eh; 427 struct mbuf *m; 428 { 429 register struct ifqueue *inq; 430 u_short ether_type; 431 int s; 432 #if defined (ISO) || defined (LLC) || defined(NETATALK) 433 register struct llc *l; 434 #endif 435 436 if ((ifp->if_flags & IFF_UP) == 0) { 437 m_freem(m); 438 return; 439 } 440 ifp->if_ibytes += m->m_pkthdr.len + sizeof (*eh); 441 if (eh->ether_dhost[0] & 1) { 442 if (bcmp((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost, 443 sizeof(etherbroadcastaddr)) == 0) 444 m->m_flags |= M_BCAST; 445 else 446 m->m_flags |= M_MCAST; 447 } 448 if (m->m_flags & (M_BCAST|M_MCAST)) 449 ifp->if_imcasts++; 450 451 ether_type = ntohs(eh->ether_type); 452 453 #if NVLAN > 0 454 if (ether_type == vlan_proto) { 455 if (vlan_input(eh, m) < 0) 456 ifp->if_data.ifi_noproto++; 457 return; 458 } 459 #endif /* NVLAN > 0 */ 460 461 switch (ether_type) { 462 #ifdef INET 463 case ETHERTYPE_IP: 464 if (ipflow_fastforward(m)) 465 return; 466 schednetisr(NETISR_IP); 467 inq = &ipintrq; 468 break; 469 470 case ETHERTYPE_ARP: 471 schednetisr(NETISR_ARP); 472 inq = &arpintrq; 473 break; 474 #endif 475 #ifdef IPX 476 case ETHERTYPE_IPX: 477 schednetisr(NETISR_IPX); 478 inq = &ipxintrq; 479 break; 480 #endif 481 #ifdef NS 482 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */ 483 schednetisr(NETISR_NS); 484 inq = &nsintrq; 485 break; 486 487 #endif /* NS */ 488 #ifdef NETATALK 489 case ETHERTYPE_AT: 490 schednetisr(NETISR_ATALK); 491 inq = &atintrq1; 492 break; 493 case ETHERTYPE_AARP: 494 /* probably this should be done with a NETISR as well */ 495 aarpinput((struct arpcom *)ifp, m); /* XXX */ 496 return; 497 #endif NETATALK 498 default: 499 #ifdef NS 500 checksum = mtod(m, ushort *); 501 /* Novell 802.3 */ 502 if ((ether_type <= ETHERMTU) && 503 ((*checksum == 0xffff) || (*checksum == 0xE0E0))){ 504 if(*checksum == 0xE0E0) { 505 m->m_pkthdr.len -= 3; 506 m->m_len -= 3; 507 m->m_data += 3; 508 } 509 schednetisr(NETISR_NS); 510 inq = &nsintrq; 511 break; 512 } 513 #endif /* NS */ 514 #if defined (ISO) || defined (LLC) || defined(NETATALK) 515 if (ether_type > ETHERMTU) 516 goto dropanyway; 517 l = mtod(m, struct llc *); 518 switch (l->llc_dsap) { 519 #ifdef NETATALK 520 case LLC_SNAP_LSAP: 521 switch (l->llc_control) { 522 case LLC_UI: 523 if (l->llc_ssap != LLC_SNAP_LSAP) 524 goto dropanyway; 525 526 if (Bcmp(&(l->llc_snap_org_code)[0], at_org_code, 527 sizeof(at_org_code)) == 0 && 528 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) { 529 inq = &atintrq2; 530 m_adj( m, sizeof( struct llc )); 531 schednetisr(NETISR_ATALK); 532 break; 533 } 534 535 if (Bcmp(&(l->llc_snap_org_code)[0], aarp_org_code, 536 sizeof(aarp_org_code)) == 0 && 537 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) { 538 m_adj( m, sizeof( struct llc )); 539 aarpinput((struct arpcom *)ifp, m); /* XXX */ 540 return; 541 } 542 543 default: 544 goto dropanyway; 545 } 546 break; 547 #endif NETATALK 548 #ifdef ISO 549 case LLC_ISO_LSAP: 550 switch (l->llc_control) { 551 case LLC_UI: 552 /* LLC_UI_P forbidden in class 1 service */ 553 if ((l->llc_dsap == LLC_ISO_LSAP) && 554 (l->llc_ssap == LLC_ISO_LSAP)) { 555 /* LSAP for ISO */ 556 if (m->m_pkthdr.len > ether_type) 557 m_adj(m, ether_type - m->m_pkthdr.len); 558 m->m_data += 3; /* XXX */ 559 m->m_len -= 3; /* XXX */ 560 m->m_pkthdr.len -= 3; /* XXX */ 561 M_PREPEND(m, sizeof *eh, M_DONTWAIT); 562 if (m == 0) 563 return; 564 *mtod(m, struct ether_header *) = *eh; 565 IFDEBUG(D_ETHER) 566 printf("clnp packet"); 567 ENDDEBUG 568 schednetisr(NETISR_ISO); 569 inq = &clnlintrq; 570 break; 571 } 572 goto dropanyway; 573 574 case LLC_XID: 575 case LLC_XID_P: 576 if(m->m_len < 6) 577 goto dropanyway; 578 l->llc_window = 0; 579 l->llc_fid = 9; 580 l->llc_class = 1; 581 l->llc_dsap = l->llc_ssap = 0; 582 /* Fall through to */ 583 case LLC_TEST: 584 case LLC_TEST_P: 585 { 586 struct sockaddr sa; 587 register struct ether_header *eh2; 588 int i; 589 u_char c = l->llc_dsap; 590 591 l->llc_dsap = l->llc_ssap; 592 l->llc_ssap = c; 593 if (m->m_flags & (M_BCAST | M_MCAST)) 594 bcopy((caddr_t)ac->ac_enaddr, 595 (caddr_t)eh->ether_dhost, 6); 596 sa.sa_family = AF_UNSPEC; 597 sa.sa_len = sizeof(sa); 598 eh2 = (struct ether_header *)sa.sa_data; 599 for (i = 0; i < 6; i++) { 600 eh2->ether_shost[i] = c = eh->ether_dhost[i]; 601 eh2->ether_dhost[i] = 602 eh->ether_dhost[i] = eh->ether_shost[i]; 603 eh->ether_shost[i] = c; 604 } 605 ifp->if_output(ifp, m, &sa, NULL); 606 return; 607 } 608 default: 609 m_freem(m); 610 return; 611 } 612 break; 613 #endif /* ISO */ 614 #ifdef LLC 615 case LLC_X25_LSAP: 616 { 617 if (m->m_pkthdr.len > ether_type) 618 m_adj(m, ether_type - m->m_pkthdr.len); 619 M_PREPEND(m, sizeof(struct sdl_hdr) , M_DONTWAIT); 620 if (m == 0) 621 return; 622 if ( !sdl_sethdrif(ifp, eh->ether_shost, LLC_X25_LSAP, 623 eh->ether_dhost, LLC_X25_LSAP, 6, 624 mtod(m, struct sdl_hdr *))) 625 panic("ETHER cons addr failure"); 626 mtod(m, struct sdl_hdr *)->sdlhdr_len = ether_type; 627 #ifdef LLC_DEBUG 628 printf("llc packet\n"); 629 #endif /* LLC_DEBUG */ 630 schednetisr(NETISR_CCITT); 631 inq = &llcintrq; 632 break; 633 } 634 #endif /* LLC */ 635 dropanyway: 636 default: 637 m_freem(m); 638 return; 639 } 640 #else /* ISO || LLC || NETATALK */ 641 m_freem(m); 642 return; 643 #endif /* ISO || LLC || NETATALK */ 644 } 645 646 s = splimp(); 647 if (IF_QFULL(inq)) { 648 IF_DROP(inq); 649 m_freem(m); 650 } else 651 IF_ENQUEUE(inq, m); 652 splx(s); 653 } 654 655 /* 656 * Perform common duties while attaching to interface list 657 */ 658 void 659 ether_ifattach(ifp) 660 register struct ifnet *ifp; 661 { 662 register struct ifaddr *ifa; 663 register struct sockaddr_dl *sdl; 664 665 ifp->if_type = IFT_ETHER; 666 ifp->if_addrlen = 6; 667 ifp->if_hdrlen = 14; 668 ifp->if_mtu = ETHERMTU; 669 ifp->if_resolvemulti = ether_resolvemulti; 670 if (ifp->if_baudrate == 0) 671 ifp->if_baudrate = 10000000; 672 ifa = ifnet_addrs[ifp->if_index - 1]; 673 if (ifa == 0) { 674 printf("ether_ifattach: no lladdr!\n"); 675 return; 676 } 677 sdl = (struct sockaddr_dl *)ifa->ifa_addr; 678 sdl->sdl_type = IFT_ETHER; 679 sdl->sdl_alen = ifp->if_addrlen; 680 bcopy(((struct arpcom *)ifp)->ac_enaddr, LLADDR(sdl), ifp->if_addrlen); 681 } 682 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((struct arpcom *)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 = (struct arpcom *) (ifp->if_softc); 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 = (struct arpcom *) (ifp->if_softc); 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(((struct arpcom *)ifp->if_softc)->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