if_ethersubr.c revision 17415
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.21 1996/06/19 01:50:10 julian Exp $ 35 */ 36 37#include <sys/param.h> 38#include <sys/systm.h> 39#include <sys/kernel.h> 40#include <sys/malloc.h> 41#include <sys/mbuf.h> 42#include <sys/protosw.h> 43#include <sys/socket.h> 44#include <sys/ioctl.h> 45#include <sys/errno.h> 46#include <sys/syslog.h> 47#include <sys/sysctl.h> 48 49#include <net/if.h> 50#include <net/netisr.h> 51#include <net/route.h> 52#include <net/if_llc.h> 53#include <net/if_dl.h> 54#include <net/if_types.h> 55 56#ifdef INET 57#include <netinet/in.h> 58#include <netinet/in_var.h> 59#endif 60#include <netinet/if_ether.h> 61 62#ifdef IPX 63#include <netipx/ipx.h> 64#include <netipx/ipx_if.h> 65#endif 66 67#ifdef NS 68#include <netns/ns.h> 69#include <netns/ns_if.h> 70#endif 71 72#ifdef ISO 73#include <netiso/argo_debug.h> 74#include <netiso/iso.h> 75#include <netiso/iso_var.h> 76#include <netiso/iso_snpac.h> 77#endif 78 79/*#ifdef LLC 80#include <netccitt/dll.h> 81#include <netccitt/llc_var.h> 82#endif*/ 83 84#if defined(LLC) && defined(CCITT) 85extern struct ifqueue pkintrq; 86#endif 87 88#ifdef NETATALK 89#include <netatalk/at.h> 90#include <netatalk/at_var.h> 91#include <netatalk/at_extern.h> 92 93#define llc_snap_org_code llc_un.type_snap.org_code 94#define llc_snap_ether_type llc_un.type_snap.ether_type 95 96extern u_char at_org_code[ 3 ]; 97extern u_char aarp_org_code[ 3 ]; 98#endif NETATALK 99 100u_char etherbroadcastaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 101#define senderr(e) { error = (e); goto bad;} 102 103/* 104 * Ethernet output routine. 105 * Encapsulate a packet of type family for the local net. 106 * Use trailer local net encapsulation if enough data in first 107 * packet leaves a multiple of 512 bytes of data in remainder. 108 * Assumes that ifp is actually pointer to arpcom structure. 109 */ 110int 111ether_output(ifp, m0, dst, rt0) 112 register struct ifnet *ifp; 113 struct mbuf *m0; 114 struct sockaddr *dst; 115 struct rtentry *rt0; 116{ 117 short type; 118 int s, error = 0; 119 u_char edst[6]; 120 register struct mbuf *m = m0; 121 register struct rtentry *rt; 122 struct mbuf *mcopy = (struct mbuf *)0; 123 register struct ether_header *eh; 124 int off, len = m->m_pkthdr.len; 125 struct arpcom *ac = (struct arpcom *)ifp; 126#ifdef NETATALK 127 struct at_ifaddr *aa; 128#endif NETATALK 129 130 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) 131 senderr(ENETDOWN); 132 rt = rt0; 133 if (rt) { 134 if ((rt->rt_flags & RTF_UP) == 0) { 135 rt0 = rt = rtalloc1(dst, 1, 0UL); 136 if (rt0) 137 rt->rt_refcnt--; 138 else 139 senderr(EHOSTUNREACH); 140 } 141 if (rt->rt_flags & RTF_GATEWAY) { 142 if (rt->rt_gwroute == 0) 143 goto lookup; 144 if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) { 145 rtfree(rt); rt = rt0; 146 lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1, 147 0UL); 148 if ((rt = rt->rt_gwroute) == 0) 149 senderr(EHOSTUNREACH); 150 } 151 } 152 if (rt->rt_flags & RTF_REJECT) 153 if (rt->rt_rmx.rmx_expire == 0 || 154 time.tv_sec < rt->rt_rmx.rmx_expire) 155 senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH); 156 } 157 switch (dst->sa_family) { 158 159#ifdef INET 160 case AF_INET: 161 if (!arpresolve(ac, rt, m, dst, edst, rt0)) 162 return (0); /* if not yet resolved */ 163 /* If broadcasting on a simplex interface, loopback a copy */ 164 if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX)) 165 mcopy = m_copy(m, 0, (int)M_COPYALL); 166 off = m->m_pkthdr.len - m->m_len; 167 type = htons(ETHERTYPE_IP); 168 break; 169#endif 170#ifdef IPX 171 case AF_IPX: 172 type = htons(ETHERTYPE_IPX); 173 bcopy((caddr_t)&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host), 174 (caddr_t)edst, sizeof (edst)); 175 if (!bcmp((caddr_t)edst, (caddr_t)&ipx_thishost, sizeof(edst))) 176 return (looutput(ifp, m, dst, rt)); 177 /* If broadcasting on a simplex interface, loopback a copy */ 178 if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX)) 179 mcopy = m_copy(m, 0, (int)M_COPYALL); 180 break; 181#endif 182#ifdef NETATALK 183 case AF_APPLETALK: 184 if (!aarpresolve(ac, m, (struct sockaddr_at *)dst, edst)) { 185#ifdef NETATALKDEBUG 186 extern char *prsockaddr(struct sockaddr *); 187 printf("aarpresolv: failed for %s\n", prsockaddr(dst)); 188#endif NETATALKDEBUG 189 return (0); 190 } 191 /* 192 * ifaddr is the first thing in at_ifaddr 193 */ 194 if ((aa = (struct at_ifaddr *)at_ifawithnet( 195 (struct sockaddr_at *)dst, ifp->if_addrlist)) 196 == 0) 197 goto bad; 198 199 /* 200 * In the phase 2 case, we need to prepend an mbuf for the llc header. 201 * Since we must preserve the value of m, which is passed to us by 202 * value, we m_copy() the first mbuf, and use it for our llc header. 203 */ 204 if ( aa->aa_flags & AFA_PHASE2 ) { 205 struct llc llc; 206 207 M_PREPEND(m, sizeof(struct llc), M_WAIT); 208 len += sizeof(struct llc); 209 llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP; 210 llc.llc_control = LLC_UI; 211 bcopy(at_org_code, llc.llc_snap_org_code, sizeof(at_org_code)); 212 llc.llc_snap_ether_type = htons( ETHERTYPE_AT ); 213 bcopy(&llc, mtod(m, caddr_t), sizeof(struct llc)); 214 type = htons(m->m_pkthdr.len); 215 } else { 216 type = htons(ETHERTYPE_AT); 217 } 218 break; 219#endif NETATALK 220#ifdef NS 221 case AF_NS: 222 type = htons(ETHERTYPE_NS); 223 bcopy((caddr_t)&(((struct sockaddr_ns *)dst)->sns_addr.x_host), 224 (caddr_t)edst, sizeof (edst)); 225 if (!bcmp((caddr_t)edst, (caddr_t)&ns_thishost, sizeof(edst))) 226 return (looutput(ifp, m, dst, rt)); 227 /* If broadcasting on a simplex interface, loopback a copy */ 228 if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX)) 229 mcopy = m_copy(m, 0, (int)M_COPYALL); 230 break; 231#endif 232#ifdef ISO 233 case AF_ISO: { 234 int snpalen; 235 struct llc *l; 236 register struct sockaddr_dl *sdl; 237 238 if (rt && (sdl = (struct sockaddr_dl *)rt->rt_gateway) && 239 sdl->sdl_family == AF_LINK && sdl->sdl_alen > 0) { 240 bcopy(LLADDR(sdl), (caddr_t)edst, sizeof(edst)); 241 } else if (error = 242 iso_snparesolve(ifp, (struct sockaddr_iso *)dst, 243 (char *)edst, &snpalen)) 244 goto bad; /* Not Resolved */ 245 /* If broadcasting on a simplex interface, loopback a copy */ 246 if (*edst & 1) 247 m->m_flags |= (M_BCAST|M_MCAST); 248 if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX) && 249 (mcopy = m_copy(m, 0, (int)M_COPYALL))) { 250 M_PREPEND(mcopy, sizeof (*eh), M_DONTWAIT); 251 if (mcopy) { 252 eh = mtod(mcopy, struct ether_header *); 253 bcopy((caddr_t)edst, 254 (caddr_t)eh->ether_dhost, sizeof (edst)); 255 bcopy((caddr_t)ac->ac_enaddr, 256 (caddr_t)eh->ether_shost, sizeof (edst)); 257 } 258 } 259 M_PREPEND(m, 3, M_DONTWAIT); 260 if (m == NULL) 261 return (0); 262 type = htons(m->m_pkthdr.len); 263 l = mtod(m, struct llc *); 264 l->llc_dsap = l->llc_ssap = LLC_ISO_LSAP; 265 l->llc_control = LLC_UI; 266 len += 3; 267 IFDEBUG(D_ETHER) 268 int i; 269 printf("unoutput: sending pkt to: "); 270 for (i=0; i<6; i++) 271 printf("%x ", edst[i] & 0xff); 272 printf("\n"); 273 ENDDEBUG 274 } break; 275#endif /* ISO */ 276#ifdef LLC 277/* case AF_NSAP: */ 278 case AF_CCITT: { 279 register struct sockaddr_dl *sdl = 280 (struct sockaddr_dl *) rt -> rt_gateway; 281 282 if (sdl && sdl->sdl_family == AF_LINK 283 && sdl->sdl_alen > 0) { 284 bcopy(LLADDR(sdl), (char *)edst, 285 sizeof(edst)); 286 } else goto bad; /* Not a link interface ? Funny ... */ 287 if ((ifp->if_flags & IFF_SIMPLEX) && (*edst & 1) && 288 (mcopy = m_copy(m, 0, (int)M_COPYALL))) { 289 M_PREPEND(mcopy, sizeof (*eh), M_DONTWAIT); 290 if (mcopy) { 291 eh = mtod(mcopy, struct ether_header *); 292 bcopy((caddr_t)edst, 293 (caddr_t)eh->ether_dhost, sizeof (edst)); 294 bcopy((caddr_t)ac->ac_enaddr, 295 (caddr_t)eh->ether_shost, sizeof (edst)); 296 } 297 } 298 type = htons(m->m_pkthdr.len); 299#ifdef LLC_DEBUG 300 { 301 int i; 302 register struct llc *l = mtod(m, struct llc *); 303 304 printf("ether_output: sending LLC2 pkt to: "); 305 for (i=0; i<6; i++) 306 printf("%x ", edst[i] & 0xff); 307 printf(" len 0x%x dsap 0x%x ssap 0x%x control 0x%x\n", 308 type & 0xff, l->llc_dsap & 0xff, l->llc_ssap &0xff, 309 l->llc_control & 0xff); 310 311 } 312#endif /* LLC_DEBUG */ 313 } break; 314#endif /* LLC */ 315 316 case AF_UNSPEC: 317 eh = (struct ether_header *)dst->sa_data; 318 (void)memcpy(edst, eh->ether_dhost, sizeof (edst)); 319 type = eh->ether_type; 320 break; 321 322 default: 323 printf("%s%d: can't handle af%d\n", ifp->if_name, ifp->if_unit, 324 dst->sa_family); 325 senderr(EAFNOSUPPORT); 326 } 327 328 329 if (mcopy) 330 (void) looutput(ifp, mcopy, dst, rt); 331 /* 332 * Add local net header. If no space in first mbuf, 333 * allocate another. 334 */ 335 M_PREPEND(m, sizeof (struct ether_header), M_DONTWAIT); 336 if (m == 0) 337 senderr(ENOBUFS); 338 eh = mtod(m, struct ether_header *); 339 (void)memcpy(&eh->ether_type, &type, 340 sizeof(eh->ether_type)); 341 (void)memcpy(eh->ether_dhost, edst, sizeof (edst)); 342 (void)memcpy(eh->ether_shost, ac->ac_enaddr, 343 sizeof(eh->ether_shost)); 344 s = splimp(); 345 /* 346 * Queue message on interface, and start output if interface 347 * not yet active. 348 */ 349 if (IF_QFULL(&ifp->if_snd)) { 350 IF_DROP(&ifp->if_snd); 351 splx(s); 352 senderr(ENOBUFS); 353 } 354 IF_ENQUEUE(&ifp->if_snd, m); 355 if ((ifp->if_flags & IFF_OACTIVE) == 0) 356 (*ifp->if_start)(ifp); 357 splx(s); 358 ifp->if_obytes += len + sizeof (struct ether_header); 359 if (m->m_flags & M_MCAST) 360 ifp->if_omcasts++; 361 return (error); 362 363bad: 364 if (m) 365 m_freem(m); 366 return (error); 367} 368 369/* 370 * Process a received Ethernet packet; 371 * the packet is in the mbuf chain m without 372 * the ether header, which is provided separately. 373 */ 374void 375ether_input(ifp, eh, m) 376 struct ifnet *ifp; 377 register struct ether_header *eh; 378 struct mbuf *m; 379{ 380 register struct ifqueue *inq; 381 u_short ether_type; 382 int s; 383#if defined (ISO) || defined (LLC) || defined(NETATALK) 384 register struct llc *l; 385#endif 386 387 if ((ifp->if_flags & IFF_UP) == 0) { 388 m_freem(m); 389 return; 390 } 391 ifp->if_ibytes += m->m_pkthdr.len + sizeof (*eh); 392 if (bcmp((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost, 393 sizeof(etherbroadcastaddr)) == 0) 394 m->m_flags |= M_BCAST; 395 else if (eh->ether_dhost[0] & 1) 396 m->m_flags |= M_MCAST; 397 if (m->m_flags & (M_BCAST|M_MCAST)) 398 ifp->if_imcasts++; 399 400 ether_type = ntohs(eh->ether_type); 401 402 switch (ether_type) { 403#ifdef INET 404 case ETHERTYPE_IP: 405 schednetisr(NETISR_IP); 406 inq = &ipintrq; 407 break; 408 409 case ETHERTYPE_ARP: 410 schednetisr(NETISR_ARP); 411 inq = &arpintrq; 412 break; 413#endif 414#ifdef IPX 415 case ETHERTYPE_IPX: 416 schednetisr(NETISR_IPX); 417 inq = &ipxintrq; 418 break; 419#endif 420#ifdef NS 421 case ETHERTYPE_NS: 422 schednetisr(NETISR_NS); 423 inq = &nsintrq; 424 break; 425#endif 426#ifdef NETATALK 427 case ETHERTYPE_AT: 428 schednetisr(NETISR_ATALK); 429 inq = &atintrq1; 430 break; 431 case ETHERTYPE_AARP: 432 /* probably this should be done with a NETISR as well */ 433 aarpinput((struct arpcom *)ifp, m); /* XXX */ 434 return; 435#endif NETATALK 436 default: 437#if defined (ISO) || defined (LLC) || defined(NETATALK) 438 if (ether_type > ETHERMTU) 439 goto dropanyway; 440 l = mtod(m, struct llc *); 441 switch (l->llc_dsap) { 442#ifdef NETATALK 443 case LLC_SNAP_LSAP: 444 switch (l->llc_control) { 445 case LLC_UI: 446 if (l->llc_ssap != LLC_SNAP_LSAP) 447 goto dropanyway; 448 449 if (Bcmp(&(l->llc_snap_org_code)[0], at_org_code, 450 sizeof(at_org_code)) == 0 && 451 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) { 452 inq = &atintrq2; 453 m_adj( m, sizeof( struct llc )); 454 schednetisr(NETISR_ATALK); 455 break; 456 } 457 458 if (Bcmp(&(l->llc_snap_org_code)[0], aarp_org_code, 459 sizeof(aarp_org_code)) == 0 && 460 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) { 461 m_adj( m, sizeof( struct llc )); 462 aarpinput((struct arpcom *)ifp, m); /* XXX */ 463 return; 464 } 465 466 default: 467 goto dropanyway; 468 } 469 break; 470#endif NETATALK 471#ifdef ISO 472 case LLC_ISO_LSAP: 473 switch (l->llc_control) { 474 case LLC_UI: 475 /* LLC_UI_P forbidden in class 1 service */ 476 if ((l->llc_dsap == LLC_ISO_LSAP) && 477 (l->llc_ssap == LLC_ISO_LSAP)) { 478 /* LSAP for ISO */ 479 if (m->m_pkthdr.len > ether_type) 480 m_adj(m, ether_type - m->m_pkthdr.len); 481 m->m_data += 3; /* XXX */ 482 m->m_len -= 3; /* XXX */ 483 m->m_pkthdr.len -= 3; /* XXX */ 484 M_PREPEND(m, sizeof *eh, M_DONTWAIT); 485 if (m == 0) 486 return; 487 *mtod(m, struct ether_header *) = *eh; 488 IFDEBUG(D_ETHER) 489 printf("clnp packet"); 490 ENDDEBUG 491 schednetisr(NETISR_ISO); 492 inq = &clnlintrq; 493 break; 494 } 495 goto dropanyway; 496 497 case LLC_XID: 498 case LLC_XID_P: 499 if(m->m_len < 6) 500 goto dropanyway; 501 l->llc_window = 0; 502 l->llc_fid = 9; 503 l->llc_class = 1; 504 l->llc_dsap = l->llc_ssap = 0; 505 /* Fall through to */ 506 case LLC_TEST: 507 case LLC_TEST_P: 508 { 509 struct sockaddr sa; 510 register struct ether_header *eh2; 511 int i; 512 u_char c = l->llc_dsap; 513 514 l->llc_dsap = l->llc_ssap; 515 l->llc_ssap = c; 516 if (m->m_flags & (M_BCAST | M_MCAST)) 517 bcopy((caddr_t)ac->ac_enaddr, 518 (caddr_t)eh->ether_dhost, 6); 519 sa.sa_family = AF_UNSPEC; 520 sa.sa_len = sizeof(sa); 521 eh2 = (struct ether_header *)sa.sa_data; 522 for (i = 0; i < 6; i++) { 523 eh2->ether_shost[i] = c = eh->ether_dhost[i]; 524 eh2->ether_dhost[i] = 525 eh->ether_dhost[i] = eh->ether_shost[i]; 526 eh->ether_shost[i] = c; 527 } 528 ifp->if_output(ifp, m, &sa, NULL); 529 return; 530 } 531 default: 532 m_freem(m); 533 return; 534 } 535 break; 536#endif /* ISO */ 537#ifdef LLC 538 case LLC_X25_LSAP: 539 { 540 if (m->m_pkthdr.len > ether_type) 541 m_adj(m, ether_type - m->m_pkthdr.len); 542 M_PREPEND(m, sizeof(struct sdl_hdr) , M_DONTWAIT); 543 if (m == 0) 544 return; 545 if ( !sdl_sethdrif(ifp, eh->ether_shost, LLC_X25_LSAP, 546 eh->ether_dhost, LLC_X25_LSAP, 6, 547 mtod(m, struct sdl_hdr *))) 548 panic("ETHER cons addr failure"); 549 mtod(m, struct sdl_hdr *)->sdlhdr_len = ether_type; 550#ifdef LLC_DEBUG 551 printf("llc packet\n"); 552#endif /* LLC_DEBUG */ 553 schednetisr(NETISR_CCITT); 554 inq = &llcintrq; 555 break; 556 } 557#endif /* LLC */ 558 dropanyway: 559 default: 560 m_freem(m); 561 return; 562 } 563#else /* ISO || LLC || NETATALK */ 564 m_freem(m); 565 return; 566#endif /* ISO || LLC || NETATALK */ 567 } 568 569 s = splimp(); 570 if (IF_QFULL(inq)) { 571 IF_DROP(inq); 572 m_freem(m); 573 } else 574 IF_ENQUEUE(inq, m); 575 splx(s); 576} 577 578/* 579 * Perform common duties while attaching to interface list 580 */ 581void 582ether_ifattach(ifp) 583 register struct ifnet *ifp; 584{ 585 register struct ifaddr *ifa; 586 register struct sockaddr_dl *sdl; 587 588 ifp->if_type = IFT_ETHER; 589 ifp->if_addrlen = 6; 590 ifp->if_hdrlen = 14; 591 ifp->if_mtu = ETHERMTU; 592 if (ifp->if_baudrate == 0) 593 ifp->if_baudrate = 10000000; 594 for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next) 595 if ((sdl = (struct sockaddr_dl *)ifa->ifa_addr) && 596 sdl->sdl_family == AF_LINK) { 597 sdl->sdl_type = IFT_ETHER; 598 sdl->sdl_alen = ifp->if_addrlen; 599 bcopy((caddr_t)((struct arpcom *)ifp)->ac_enaddr, 600 LLADDR(sdl), ifp->if_addrlen); 601 break; 602 } 603} 604 605static u_char ether_ipmulticast_min[6] = 606 { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 }; 607static u_char ether_ipmulticast_max[6] = 608 { 0x01, 0x00, 0x5e, 0x7f, 0xff, 0xff }; 609/* 610 * Add an Ethernet multicast address or range of addresses to the list for a 611 * given interface. 612 */ 613int 614ether_addmulti(ifr, ac) 615 struct ifreq *ifr; 616 register struct arpcom *ac; 617{ 618 register struct ether_multi *enm; 619 struct sockaddr_in *sin; 620 u_char addrlo[6]; 621 u_char addrhi[6]; 622 int set_allmulti = 0; 623 int s = splimp(); 624 625 switch (ifr->ifr_addr.sa_family) { 626 627 case AF_UNSPEC: 628 bcopy(ifr->ifr_addr.sa_data, addrlo, 6); 629 bcopy(addrlo, addrhi, 6); 630 break; 631 632#ifdef INET 633 case AF_INET: 634 sin = (struct sockaddr_in *)&(ifr->ifr_addr); 635 if (sin->sin_addr.s_addr == INADDR_ANY) { 636 /* 637 * An IP address of INADDR_ANY means listen to all 638 * of the Ethernet multicast addresses used for IP. 639 * (This is for the sake of IP multicast routers.) 640 */ 641 bcopy(ether_ipmulticast_min, addrlo, 6); 642 bcopy(ether_ipmulticast_max, addrhi, 6); 643 set_allmulti = 1; 644 } 645 else { 646 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo); 647 bcopy(addrlo, addrhi, 6); 648 } 649 break; 650#endif 651 652 default: 653 splx(s); 654 return (EAFNOSUPPORT); 655 } 656 657 /* 658 * Verify that we have valid Ethernet multicast addresses. 659 */ 660 if ((addrlo[0] & 0x01) != 1 || (addrhi[0] & 0x01) != 1) { 661 splx(s); 662 return (EINVAL); 663 } 664 /* 665 * See if the address range is already in the list. 666 */ 667 ETHER_LOOKUP_MULTI(addrlo, addrhi, ac, enm); 668 if (enm != NULL) { 669 /* 670 * Found it; just increment the reference count. 671 */ 672 ++enm->enm_refcount; 673 splx(s); 674 return (0); 675 } 676 /* 677 * New address or range; malloc a new multicast record 678 * and link it into the interface's multicast list. 679 */ 680 enm = (struct ether_multi *)malloc(sizeof(*enm), M_IFMADDR, M_NOWAIT); 681 if (enm == NULL) { 682 splx(s); 683 return (ENOBUFS); 684 } 685 bcopy(addrlo, enm->enm_addrlo, 6); 686 bcopy(addrhi, enm->enm_addrhi, 6); 687 enm->enm_ac = ac; 688 enm->enm_refcount = 1; 689 enm->enm_next = ac->ac_multiaddrs; 690 ac->ac_multiaddrs = enm; 691 ac->ac_multicnt++; 692 splx(s); 693 if (set_allmulti) 694 ac->ac_if.if_flags |= IFF_ALLMULTI; 695 696 /* 697 * Return ENETRESET to inform the driver that the list has changed 698 * and its reception filter should be adjusted accordingly. 699 */ 700 return (ENETRESET); 701} 702 703/* 704 * Delete a multicast address record. 705 */ 706int 707ether_delmulti(ifr, ac) 708 struct ifreq *ifr; 709 register struct arpcom *ac; 710{ 711 register struct ether_multi *enm; 712 register struct ether_multi **p; 713 struct sockaddr_in *sin; 714 u_char addrlo[6]; 715 u_char addrhi[6]; 716 int unset_allmulti = 0; 717 int s = splimp(); 718 719 switch (ifr->ifr_addr.sa_family) { 720 721 case AF_UNSPEC: 722 bcopy(ifr->ifr_addr.sa_data, addrlo, 6); 723 bcopy(addrlo, addrhi, 6); 724 break; 725 726#ifdef INET 727 case AF_INET: 728 sin = (struct sockaddr_in *)&(ifr->ifr_addr); 729 if (sin->sin_addr.s_addr == INADDR_ANY) { 730 /* 731 * An IP address of INADDR_ANY means stop listening 732 * to the range of Ethernet multicast addresses used 733 * for IP. 734 */ 735 bcopy(ether_ipmulticast_min, addrlo, 6); 736 bcopy(ether_ipmulticast_max, addrhi, 6); 737 unset_allmulti = 1; 738 } 739 else { 740 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo); 741 bcopy(addrlo, addrhi, 6); 742 } 743 break; 744#endif 745 746 default: 747 splx(s); 748 return (EAFNOSUPPORT); 749 } 750 751 /* 752 * Look up the address in our list. 753 */ 754 ETHER_LOOKUP_MULTI(addrlo, addrhi, ac, enm); 755 if (enm == NULL) { 756 splx(s); 757 return (ENXIO); 758 } 759 if (--enm->enm_refcount != 0) { 760 /* 761 * Still some claims to this record. 762 */ 763 splx(s); 764 return (0); 765 } 766 /* 767 * No remaining claims to this record; unlink and free it. 768 */ 769 for (p = &enm->enm_ac->ac_multiaddrs; 770 *p != enm; 771 p = &(*p)->enm_next) 772 continue; 773 *p = (*p)->enm_next; 774 free(enm, M_IFMADDR); 775 ac->ac_multicnt--; 776 splx(s); 777 if (unset_allmulti) 778 ac->ac_if.if_flags &= ~IFF_ALLMULTI; 779 780 /* 781 * Return ENETRESET to inform the driver that the list has changed 782 * and its reception filter should be adjusted accordingly. 783 */ 784 return (ENETRESET); 785} 786 787SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet"); 788 789void 790ether_ioctl(struct ifnet *ifp, int command, caddr_t data) 791{ 792 struct ifaddr *ifa = (struct ifaddr *) data; 793 struct ifreq *ifr = (struct ifreq *) data; 794 795 switch (command) { 796 case SIOCSIFADDR: 797 ifp->if_flags |= IFF_UP; 798 799 switch (ifa->ifa_addr->sa_family) { 800#ifdef INET 801 case AF_INET: 802 ifp->if_init(ifp->if_softc); /* before arpwhohas */ 803 arp_ifinit((struct arpcom *)ifp, ifa); 804 break; 805#endif 806#ifdef IPX 807 /* 808 * XXX - This code is probably wrong 809 */ 810 case AF_IPX: 811 { 812 register struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr); 813 814 if (ipx_nullhost(*ina)) 815 ina->x_host = 816 *(union ipx_host *) (sc->arpcom.ac_enaddr); 817 else { 818 bcopy((caddr_t) ina->x_host.c_host, 819 (caddr_t) sc->arpcom.ac_enaddr, 820 sizeof(sc->arpcom.ac_enaddr)); 821 } 822 823 /* 824 * Set new address 825 */ 826 ifp->if_init(ifp->if_softc); 827 break; 828 } 829#endif 830#ifdef NS 831 /* 832 * XXX - This code is probably wrong 833 */ 834 case AF_NS: 835 { 836 register struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr); 837 838 if (ns_nullhost(*ina)) 839 ina->x_host = 840 *(union ns_host *) (sc->arpcom.ac_enaddr); 841 else { 842 bcopy((caddr_t) ina->x_host.c_host, 843 (caddr_t) sc->arpcom.ac_enaddr, 844 sizeof(sc->arpcom.ac_enaddr)); 845 } 846 847 /* 848 * Set new address 849 */ 850 ifp->if_init(ifp->if_softc); 851 break; 852 } 853#endif 854 default: 855 ifp->if_init(ifp->if_softc); 856 break; 857 } 858 break; 859 860 case SIOCGIFADDR: 861 { 862 struct sockaddr *sa; 863 864 sa = (struct sockaddr *) & ifr->ifr_data; 865 bcopy((caddr_t) ifp->if_softc, 866 (caddr_t) sa->sa_data, ETHER_ADDR_LEN); 867 } 868 break; 869 } 870 return; 871} 872