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