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 * $FreeBSD$ 35 */ 36 37 #include "opt_atalk.h" 38 #include "opt_inet.h" 39 #include "opt_inet6.h" 40 #include "opt_ipx.h" 41 #include "opt_bdg.h" 42 #include "opt_netgraph.h" 43 44 #include <sys/param.h> 45 #include <sys/systm.h> 46 #include <sys/kernel.h> 47 #include <sys/malloc.h> 48 #include <sys/mbuf.h> 49 #include <sys/socket.h> 50 #include <sys/sockio.h> 51 #include <sys/sysctl.h> 52 53 #include <net/if.h> 54 #include <net/netisr.h> 55 #include <net/route.h> 56 #include <net/if_llc.h> 57 #include <net/if_dl.h> 58 #include <net/if_types.h> 59 #include <net/bpf.h> 60 #include <net/ethernet.h> 61 62 #if defined(INET) || defined(INET6) 63 #include <netinet/in.h> 64 #include <netinet/in_var.h> 65 #include <netinet/if_ether.h> 66 #endif 67 #ifdef INET6 68 #include <netinet6/nd6.h> 69 #endif 70 71 #ifdef IPX 72 #include <netipx/ipx.h> 73 #include <netipx/ipx_if.h> 74 int (*ef_inputp)(struct ifnet*, struct ether_header *eh, struct mbuf *m); 75 int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp, 76 struct sockaddr *dst, short *tp); 77 #endif 78 79 #ifdef NS 80 #include <netns/ns.h> 81 #include <netns/ns_if.h> 82 ushort ns_nettype; 83 int ether_outputdebug = 0; 84 int ether_inputdebug = 0; 85 #endif 86 87 #ifdef NETATALK 88 #include <netatalk/at.h> 89 #include <netatalk/at_var.h> 90 #include <netatalk/at_extern.h> 91 92 #define llc_snap_org_code llc_un.type_snap.org_code 93 #define llc_snap_ether_type llc_un.type_snap.ether_type 94 95 extern u_char at_org_code[3]; 96 extern u_char aarp_org_code[3]; 97 #endif /* NETATALK */ 98 99 #ifdef BRIDGE 100 #include <net/bridge.h> 101 #endif 102 103 #include "vlan.h" 104 #if NVLAN > 0 105 #include <net/if_vlan_var.h> 106 #endif /* NVLAN > 0 */ 107 108 /* netgraph node hooks for ng_ether(4) */ 109 void (*ng_ether_input_p)(struct ifnet *ifp, 110 struct mbuf **mp, struct ether_header *eh); 111 void (*ng_ether_input_orphan_p)(struct ifnet *ifp, 112 struct mbuf *m, struct ether_header *eh); 113 int (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp); 114 void (*ng_ether_attach_p)(struct ifnet *ifp); 115 void (*ng_ether_detach_p)(struct ifnet *ifp); 116 117 static int ether_resolvemulti __P((struct ifnet *, struct sockaddr **, 118 struct sockaddr *)); 119 u_char etherbroadcastaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 120 #define senderr(e) do { error = (e); goto bad;} while (0) 121 #define IFP2AC(IFP) ((struct arpcom *)IFP) 122 123 /* 124 * Ethernet output routine. 125 * Encapsulate a packet of type family for the local net. 126 * Use trailer local net encapsulation if enough data in first 127 * packet leaves a multiple of 512 bytes of data in remainder. 128 * Assumes that ifp is actually pointer to arpcom structure. 129 */ 130 int 131 ether_output(ifp, m, dst, rt0) 132 register struct ifnet *ifp; 133 struct mbuf *m; 134 struct sockaddr *dst; 135 struct rtentry *rt0; 136 { 137 short type; 138 int error = 0, hdrcmplt = 0; 139 u_char esrc[6], edst[6]; 140 register struct rtentry *rt; 141 register struct ether_header *eh; 142 int off, loop_copy = 0; 143 int hlen; /* link layer header lenght */ 144 struct arpcom *ac = IFP2AC(ifp); 145 146 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) 147 senderr(ENETDOWN); 148 rt = rt0; 149 if (rt) { 150 if ((rt->rt_flags & RTF_UP) == 0) { 151 rt0 = rt = rtalloc1(dst, 1, 0UL); 152 if (rt0) 153 rt->rt_refcnt--; 154 else 155 senderr(EHOSTUNREACH); 156 } 157 if (rt->rt_flags & RTF_GATEWAY) { 158 if (rt->rt_gwroute == 0) 159 goto lookup; 160 if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) { 161 rtfree(rt); rt = rt0; 162 lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1, 163 0UL); 164 if ((rt = rt->rt_gwroute) == 0) 165 senderr(EHOSTUNREACH); 166 } 167 } 168 if (rt->rt_flags & RTF_REJECT) 169 if (rt->rt_rmx.rmx_expire == 0 || 170 time_second < rt->rt_rmx.rmx_expire) 171 senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH); 172 } 173 hlen = ETHER_HDR_LEN; 174 switch (dst->sa_family) { 175 #ifdef INET 176 case AF_INET: 177 if (!arpresolve(ac, rt, m, dst, edst, rt0)) 178 return (0); /* if not yet resolved */ 179 off = m->m_pkthdr.len - m->m_len; 180 type = htons(ETHERTYPE_IP); 181 break; 182 #endif 183 #ifdef INET6 184 case AF_INET6: 185 if (!nd6_storelladdr(&ac->ac_if, rt, m, dst, (u_char *)edst)) { 186 /* this must be impossible, so we bark */ 187 printf("nd6_storelladdr failed\n"); 188 return(0); 189 } 190 off = m->m_pkthdr.len - m->m_len; 191 type = htons(ETHERTYPE_IPV6); 192 break; 193 #endif 194 #ifdef IPX 195 case AF_IPX: 196 if (ef_outputp) { 197 error = ef_outputp(ifp, &m, dst, &type); 198 if (error) 199 goto bad; 200 } else 201 type = htons(ETHERTYPE_IPX); 202 bcopy((caddr_t)&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host), 203 (caddr_t)edst, sizeof (edst)); 204 break; 205 #endif 206 #ifdef NETATALK 207 case AF_APPLETALK: 208 { 209 struct at_ifaddr *aa; 210 211 if ((aa = at_ifawithnet((struct sockaddr_at *)dst)) == NULL) { 212 goto bad; 213 } 214 if (!aarpresolve(ac, m, (struct sockaddr_at *)dst, edst)) 215 return (0); 216 /* 217 * In the phase 2 case, need to prepend an mbuf for the llc header. 218 * Since we must preserve the value of m, which is passed to us by 219 * value, we m_copy() the first mbuf, and use it for our llc header. 220 */ 221 if ( aa->aa_flags & AFA_PHASE2 ) { 222 struct llc llc; 223 224 M_PREPEND(m, sizeof(struct llc), M_WAIT); 225 llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP; 226 llc.llc_control = LLC_UI; 227 bcopy(at_org_code, llc.llc_snap_org_code, sizeof(at_org_code)); 228 llc.llc_snap_ether_type = htons( ETHERTYPE_AT ); 229 bcopy(&llc, mtod(m, caddr_t), sizeof(struct llc)); 230 type = htons(m->m_pkthdr.len); 231 hlen = sizeof(struct llc) + ETHER_HDR_LEN; 232 } else { 233 type = htons(ETHERTYPE_AT); 234 } 235 break; 236 } 237 #endif NETATALK 238 #ifdef NS 239 case AF_NS: 240 switch(ns_nettype){ 241 default: 242 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */ 243 type = 0x8137; 244 break; 245 case 0x0: /* Novell 802.3 */ 246 type = htons( m->m_pkthdr.len); 247 break; 248 case 0xe0e0: /* Novell 802.2 and Token-Ring */ 249 M_PREPEND(m, 3, M_WAIT); 250 type = htons( m->m_pkthdr.len); 251 cp = mtod(m, u_char *); 252 *cp++ = 0xE0; 253 *cp++ = 0xE0; 254 *cp++ = 0x03; 255 break; 256 } 257 bcopy((caddr_t)&(((struct sockaddr_ns *)dst)->sns_addr.x_host), 258 (caddr_t)edst, sizeof (edst)); 259 /* 260 * XXX if ns_thishost is the same as the node's ethernet 261 * address then just the default code will catch this anyhow. 262 * So I'm not sure if this next clause should be here at all? 263 * [JRE] 264 */ 265 if (!bcmp((caddr_t)edst, (caddr_t)&ns_thishost, sizeof(edst))){ 266 m->m_pkthdr.rcvif = ifp; 267 schednetisr(NETISR_NS); 268 inq = &nsintrq; 269 s = splimp(); 270 if (IF_QFULL(inq)) { 271 IF_DROP(inq); 272 m_freem(m); 273 } else 274 IF_ENQUEUE(inq, m); 275 splx(s); 276 return (error); 277 } 278 if (!bcmp((caddr_t)edst, (caddr_t)&ns_broadhost, sizeof(edst))){ 279 m->m_flags |= M_BCAST; 280 } 281 break; 282 #endif /* NS */ 283 284 case pseudo_AF_HDRCMPLT: 285 hdrcmplt = 1; 286 eh = (struct ether_header *)dst->sa_data; 287 (void)memcpy(esrc, eh->ether_shost, sizeof (esrc)); 288 /* FALLTHROUGH */ 289 290 case AF_UNSPEC: 291 loop_copy = -1; /* if this is for us, don't do it */ 292 eh = (struct ether_header *)dst->sa_data; 293 (void)memcpy(edst, eh->ether_dhost, sizeof (edst)); 294 type = eh->ether_type; 295 break; 296 297 default: 298 printf("%s%d: can't handle af%d\n", ifp->if_name, ifp->if_unit, 299 dst->sa_family); 300 senderr(EAFNOSUPPORT); 301 } 302 303 /* 304 * Add local net header. If no space in first mbuf, 305 * allocate another. 306 */ 307 M_PREPEND(m, sizeof (struct ether_header), M_DONTWAIT); 308 if (m == 0) 309 senderr(ENOBUFS); 310 eh = mtod(m, struct ether_header *); 311 (void)memcpy(&eh->ether_type, &type, 312 sizeof(eh->ether_type)); 313 (void)memcpy(eh->ether_dhost, edst, sizeof (edst)); 314 if (hdrcmplt) 315 (void)memcpy(eh->ether_shost, esrc, 316 sizeof(eh->ether_shost)); 317 else 318 (void)memcpy(eh->ether_shost, ac->ac_enaddr, 319 sizeof(eh->ether_shost)); 320 321 /* 322 * If a simplex interface, and the packet is being sent to our 323 * Ethernet address or a broadcast address, loopback a copy. 324 * XXX To make a simplex device behave exactly like a duplex 325 * device, we should copy in the case of sending to our own 326 * ethernet address (thus letting the original actually appear 327 * on the wire). However, we don't do that here for security 328 * reasons and compatibility with the original behavior. 329 */ 330 if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) { 331 if ((m->m_flags & M_BCAST) || (loop_copy > 0)) { 332 struct mbuf *n = m_copy(m, 0, (int)M_COPYALL); 333 334 (void) if_simloop(ifp, n, dst->sa_family, hlen); 335 } else if (bcmp(eh->ether_dhost, 336 eh->ether_shost, ETHER_ADDR_LEN) == 0) { 337 (void) if_simloop(ifp, m, dst->sa_family, hlen); 338 return (0); /* XXX */ 339 } 340 } 341 342 /* Handle ng_ether(4) processing, if any */ 343 if (ng_ether_output_p != NULL) { 344 if ((error = (*ng_ether_output_p)(ifp, &m)) != 0) { 345 bad: if (m != NULL) 346 m_freem(m); 347 return (error); 348 } 349 if (m == NULL) 350 return (0); 351 } 352 353 /* Continue with link-layer output */ 354 return ether_output_frame(ifp, m); 355 } 356 357 /* 358 * Ethernet link layer output routine to send a raw frame to the device. 359 * 360 * This assumes that the 14 byte Ethernet header is present and contiguous 361 * in the first mbuf (if BRIDGE'ing). 362 */ 363 int 364 ether_output_frame(ifp, m) 365 struct ifnet *ifp; 366 struct mbuf *m; 367 { 368 int s, error = 0; 369 370 #ifdef BRIDGE 371 if (do_bridge) { 372 struct ether_header hdr; 373 374 m->m_pkthdr.rcvif = NULL; 375 bcopy(mtod(m, struct ether_header *), &hdr, ETHER_HDR_LEN); 376 m_adj(m, ETHER_HDR_LEN); 377 ifp = bridge_dst_lookup(&hdr); 378 bdg_forward(&m, &hdr, ifp); 379 if (m != NULL) 380 m_freem(m); 381 return (0); 382 } 383 #endif 384 385 s = splimp(); 386 /* 387 * Queue message on interface, and start output if interface 388 * not yet active. 389 */ 390 if (IF_QFULL(&ifp->if_snd)) { 391 IF_DROP(&ifp->if_snd); 392 splx(s); 393 m_freem(m); 394 return (ENOBUFS); 395 } 396 ifp->if_obytes += m->m_pkthdr.len; 397 if (m->m_flags & M_MCAST) 398 ifp->if_omcasts++; 399 IF_ENQUEUE(&ifp->if_snd, m); 400 if ((ifp->if_flags & IFF_OACTIVE) == 0) 401 (*ifp->if_start)(ifp); 402 splx(s); 403 return (error); 404 } 405 406 /* 407 * Process a received Ethernet packet; 408 * the packet is in the mbuf chain m without 409 * the ether header, which is provided separately. 410 * 411 * First we perform any link layer operations, then continue 412 * to the upper layers with ether_demux(). 413 */ 414 void 415 ether_input(ifp, eh, m) 416 struct ifnet *ifp; 417 struct ether_header *eh; 418 struct mbuf *m; 419 { 420 421 /* Check for a BPF tap */ 422 if (ifp->if_bpf != NULL) { 423 struct m_hdr mh; 424 425 /* This kludge is OK; BPF treats the "mbuf" as read-only */ 426 mh.mh_next = m; 427 mh.mh_data = (char *)eh; 428 mh.mh_len = ETHER_HDR_LEN; 429 bpf_mtap(ifp, (struct mbuf *)&mh); 430 } 431 432 /* Handle ng_ether(4) processing, if any */ 433 if (ng_ether_input_p != NULL) { 434 (*ng_ether_input_p)(ifp, &m, eh); 435 if (m == NULL) 436 return; 437 } 438 439 #ifdef BRIDGE 440 /* Check for bridging mode */ 441 if (do_bridge) { 442 struct ifnet *bif; 443 444 /* Check with bridging code */ 445 if ((bif = bridge_in(ifp, eh)) == BDG_DROP) { 446 m_freem(m); 447 return; 448 } 449 if (bif != BDG_LOCAL) 450 bdg_forward(&m, eh, bif); /* needs forwarding */ 451 if (bif == BDG_LOCAL 452 || bif == BDG_BCAST 453 || bif == BDG_MCAST) 454 goto recvLocal; /* receive locally */ 455 456 /* If not local and not multicast, just drop it */ 457 if (m != NULL) 458 m_freem(m); 459 return; 460 } 461 #endif 462 463 /* Discard packet if upper layers shouldn't see it. This should 464 only happen when the interface is in promiscuous mode. */ 465 if ((ifp->if_flags & IFF_PROMISC) != 0 466 && (eh->ether_dhost[0] & 1) == 0 467 && bcmp(eh->ether_dhost, 468 IFP2AC(ifp)->ac_enaddr, ETHER_ADDR_LEN) != 0) { 469 m_freem(m); 470 return; 471 } 472 473 #ifdef BRIDGE 474 recvLocal: 475 #endif 476 /* Continue with upper layer processing */ 477 ether_demux(ifp, eh, m); 478 } 479 480 /* 481 * Upper layer processing for a received Ethernet packet. 482 */ 483 void 484 ether_demux(ifp, eh, m) 485 struct ifnet *ifp; 486 struct ether_header *eh; 487 struct mbuf *m; 488 { 489 struct ifqueue *inq; 490 u_short ether_type; 491 int s; 492 #if defined(NETATALK) 493 register struct llc *l; 494 #endif 495 496 /* Discard packet if interface is not up */ 497 if ((ifp->if_flags & IFF_UP) == 0) { 498 m_freem(m); 499 return; 500 } 501 ifp->if_ibytes += m->m_pkthdr.len + sizeof (*eh); 502 if (eh->ether_dhost[0] & 1) { 503 if (bcmp((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost, 504 sizeof(etherbroadcastaddr)) == 0) 505 m->m_flags |= M_BCAST; 506 else 507 m->m_flags |= M_MCAST; 508 } 509 if (m->m_flags & (M_BCAST|M_MCAST)) 510 ifp->if_imcasts++; 511 512 ether_type = ntohs(eh->ether_type); 513 514 #if NVLAN > 0 515 if (ether_type == vlan_proto) { 516 if (vlan_input(eh, m) < 0) 517 ifp->if_data.ifi_noproto++; 518 return; 519 } 520 #endif /* NVLAN > 0 */ 521 522 switch (ether_type) { 523 #ifdef INET 524 case ETHERTYPE_IP: 525 if (ipflow_fastforward(m)) 526 return; 527 schednetisr(NETISR_IP); 528 inq = &ipintrq; 529 break; 530 531 case ETHERTYPE_ARP: 532 schednetisr(NETISR_ARP); 533 inq = &arpintrq; 534 break; 535 #endif 536 #ifdef IPX 537 case ETHERTYPE_IPX: 538 if (ef_inputp && ef_inputp(ifp, eh, m) == 0) 539 return; 540 schednetisr(NETISR_IPX); 541 inq = &ipxintrq; 542 break; 543 #endif 544 #ifdef INET6 545 case ETHERTYPE_IPV6: 546 schednetisr(NETISR_IPV6); 547 inq = &ip6intrq; 548 break; 549 #endif 550 #ifdef NS 551 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */ 552 schednetisr(NETISR_NS); 553 inq = &nsintrq; 554 break; 555 556 #endif /* NS */ 557 #ifdef NETATALK 558 case ETHERTYPE_AT: 559 schednetisr(NETISR_ATALK); 560 inq = &atintrq1; 561 break; 562 case ETHERTYPE_AARP: 563 /* probably this should be done with a NETISR as well */ 564 aarpinput(IFP2AC(ifp), m); /* XXX */ 565 return; 566 #endif NETATALK 567 default: 568 #ifdef IPX 569 if (ef_inputp && ef_inputp(ifp, eh, m) == 0) 570 return; 571 #endif /* IPX */ 572 #ifdef NS 573 checksum = mtod(m, ushort *); 574 /* Novell 802.3 */ 575 if ((ether_type <= ETHERMTU) && 576 ((*checksum == 0xffff) || (*checksum == 0xE0E0))){ 577 if(*checksum == 0xE0E0) { 578 m->m_pkthdr.len -= 3; 579 m->m_len -= 3; 580 m->m_data += 3; 581 } 582 schednetisr(NETISR_NS); 583 inq = &nsintrq; 584 break; 585 } 586 #endif /* NS */ 587 #if defined(NETATALK) 588 if (ether_type > ETHERMTU) 589 goto dropanyway; 590 l = mtod(m, struct llc *); 591 switch (l->llc_dsap) { 592 case LLC_SNAP_LSAP: 593 switch (l->llc_control) { 594 case LLC_UI: 595 if (l->llc_ssap != LLC_SNAP_LSAP) 596 goto dropanyway; 597 598 if (Bcmp(&(l->llc_snap_org_code)[0], at_org_code, 599 sizeof(at_org_code)) == 0 && 600 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) { 601 inq = &atintrq2; 602 m_adj( m, sizeof( struct llc )); 603 schednetisr(NETISR_ATALK); 604 break; 605 } 606 607 if (Bcmp(&(l->llc_snap_org_code)[0], aarp_org_code, 608 sizeof(aarp_org_code)) == 0 && 609 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) { 610 m_adj( m, sizeof( struct llc )); 611 aarpinput(IFP2AC(ifp), m); /* XXX */ 612 return; 613 } 614 615 default: 616 goto dropanyway; 617 } 618 break; 619 dropanyway: 620 default: 621 if (ng_ether_input_orphan_p != NULL) 622 (*ng_ether_input_orphan_p)(ifp, m, eh); 623 else 624 m_freem(m); 625 return; 626 } 627 #else /* NETATALK */ 628 if (ng_ether_input_orphan_p != NULL) 629 (*ng_ether_input_orphan_p)(ifp, m, eh); 630 else 631 m_freem(m); 632 return; 633 #endif /* NETATALK */ 634 } 635 636 s = splimp(); 637 if (IF_QFULL(inq)) { 638 IF_DROP(inq); 639 m_freem(m); 640 } else 641 IF_ENQUEUE(inq, m); 642 splx(s); 643 } 644 645 /* 646 * Perform common duties while attaching to interface list 647 */ 648 void 649 ether_ifattach(ifp, bpf) 650 register struct ifnet *ifp; 651 int bpf; 652 { 653 register struct ifaddr *ifa; 654 register struct sockaddr_dl *sdl; 655 656 if_attach(ifp); 657 ifp->if_type = IFT_ETHER; 658 ifp->if_addrlen = 6; 659 ifp->if_hdrlen = 14; 660 ifp->if_mtu = ETHERMTU; 661 ifp->if_resolvemulti = ether_resolvemulti; 662 if (ifp->if_baudrate == 0) 663 ifp->if_baudrate = 10000000; 664 ifa = ifnet_addrs[ifp->if_index - 1]; 665 KASSERT(ifa != NULL, ("%s: no lladdr!\n", __FUNCTION__)); 666 sdl = (struct sockaddr_dl *)ifa->ifa_addr; 667 sdl->sdl_type = IFT_ETHER; 668 sdl->sdl_alen = ifp->if_addrlen; 669 bcopy((IFP2AC(ifp))->ac_enaddr, LLADDR(sdl), ifp->if_addrlen); 670 if (bpf) 671 bpfattach(ifp, DLT_EN10MB, sizeof(struct ether_header)); 672 if (ng_ether_attach_p != NULL) 673 (*ng_ether_attach_p)(ifp); 674 } 675 676 /* 677 * Perform common duties while detaching an Ethernet interface 678 */ 679 void 680 ether_ifdetach(ifp, bpf) 681 struct ifnet *ifp; 682 int bpf; 683 { 684 if (ng_ether_detach_p != NULL) 685 (*ng_ether_detach_p)(ifp); 686 if (bpf) 687 bpfdetach(ifp); 688 if_detach(ifp); 689 } 690 691 SYSCTL_DECL(_net_link); 692 SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet"); 693 694 int 695 ether_ioctl(ifp, command, data) 696 struct ifnet *ifp; 697 int command; 698 caddr_t data; 699 { 700 struct ifaddr *ifa = (struct ifaddr *) data; 701 struct ifreq *ifr = (struct ifreq *) data; 702 int error = 0; 703 704 switch (command) { 705 case SIOCSIFADDR: 706 ifp->if_flags |= IFF_UP; 707 708 switch (ifa->ifa_addr->sa_family) { 709 #ifdef INET 710 case AF_INET: 711 ifp->if_init(ifp->if_softc); /* before arpwhohas */ 712 arp_ifinit(IFP2AC(ifp), ifa); 713 break; 714 #endif 715 #ifdef IPX 716 /* 717 * XXX - This code is probably wrong 718 */ 719 case AF_IPX: 720 { 721 register struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr); 722 struct arpcom *ac = IFP2AC(ifp); 723 724 if (ipx_nullhost(*ina)) 725 ina->x_host = 726 *(union ipx_host *) 727 ac->ac_enaddr; 728 else { 729 bcopy((caddr_t) ina->x_host.c_host, 730 (caddr_t) ac->ac_enaddr, 731 sizeof(ac->ac_enaddr)); 732 } 733 734 /* 735 * Set new address 736 */ 737 ifp->if_init(ifp->if_softc); 738 break; 739 } 740 #endif 741 #ifdef NS 742 /* 743 * XXX - This code is probably wrong 744 */ 745 case AF_NS: 746 { 747 register struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr); 748 struct arpcom *ac = IFP2AC(ifp); 749 750 if (ns_nullhost(*ina)) 751 ina->x_host = 752 *(union ns_host *) (ac->ac_enaddr); 753 else { 754 bcopy((caddr_t) ina->x_host.c_host, 755 (caddr_t) ac->ac_enaddr, 756 sizeof(ac->ac_enaddr)); 757 } 758 759 /* 760 * Set new address 761 */ 762 ifp->if_init(ifp->if_softc); 763 break; 764 } 765 #endif 766 default: 767 ifp->if_init(ifp->if_softc); 768 break; 769 } 770 break; 771 772 case SIOCGIFADDR: 773 { 774 struct sockaddr *sa; 775 776 sa = (struct sockaddr *) & ifr->ifr_data; 777 bcopy(IFP2AC(ifp)->ac_enaddr, 778 (caddr_t) sa->sa_data, ETHER_ADDR_LEN); 779 } 780 break; 781 782 case SIOCSIFMTU: 783 /* 784 * Set the interface MTU. 785 */ 786 if (ifr->ifr_mtu > ETHERMTU) { 787 error = EINVAL; 788 } else { 789 ifp->if_mtu = ifr->ifr_mtu; 790 } 791 break; 792 } 793 return (error); 794 } 795 796 int 797 ether_resolvemulti(ifp, llsa, sa) 798 struct ifnet *ifp; 799 struct sockaddr **llsa; 800 struct sockaddr *sa; 801 { 802 struct sockaddr_dl *sdl; 803 struct sockaddr_in *sin; 804 #ifdef INET6 805 struct sockaddr_in6 *sin6; 806 #endif 807 u_char *e_addr; 808 809 switch(sa->sa_family) { 810 case AF_LINK: 811 /* 812 * No mapping needed. Just check that it's a valid MC address. 813 */ 814 sdl = (struct sockaddr_dl *)sa; 815 e_addr = LLADDR(sdl); 816 if ((e_addr[0] & 1) != 1) 817 return EADDRNOTAVAIL; 818 *llsa = 0; 819 return 0; 820 821 #ifdef INET 822 case AF_INET: 823 sin = (struct sockaddr_in *)sa; 824 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) 825 return EADDRNOTAVAIL; 826 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR, 827 M_WAITOK); 828 sdl->sdl_len = sizeof *sdl; 829 sdl->sdl_family = AF_LINK; 830 sdl->sdl_index = ifp->if_index; 831 sdl->sdl_type = IFT_ETHER; 832 sdl->sdl_nlen = 0; 833 sdl->sdl_alen = ETHER_ADDR_LEN; 834 sdl->sdl_slen = 0; 835 e_addr = LLADDR(sdl); 836 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr); 837 *llsa = (struct sockaddr *)sdl; 838 return 0; 839 #endif 840 #ifdef INET6 841 case AF_INET6: 842 sin6 = (struct sockaddr_in6 *)sa; 843 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 844 /* 845 * An IP6 address of 0 means listen to all 846 * of the Ethernet multicast address used for IP6. 847 * (This is used for multicast routers.) 848 */ 849 ifp->if_flags |= IFF_ALLMULTI; 850 *llsa = 0; 851 return 0; 852 } 853 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) 854 return EADDRNOTAVAIL; 855 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR, 856 M_WAITOK); 857 sdl->sdl_len = sizeof *sdl; 858 sdl->sdl_family = AF_LINK; 859 sdl->sdl_index = ifp->if_index; 860 sdl->sdl_type = IFT_ETHER; 861 sdl->sdl_nlen = 0; 862 sdl->sdl_alen = ETHER_ADDR_LEN; 863 sdl->sdl_slen = 0; 864 e_addr = LLADDR(sdl); 865 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr); 866 *llsa = (struct sockaddr *)sdl; 867 return 0; 868 #endif 869 870 default: 871 /* 872 * Well, the text isn't quite right, but it's the name 873 * that counts... 874 */ 875 return EAFNOSUPPORT; 876 } 877 } 878 879