1 /* $NetBSD: if_ethersubr.c,v 1.129 2006/03/16 15:57:59 christos Exp $ */ 2 3 /* 4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the project nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 /* 33 * Copyright (c) 1982, 1989, 1993 34 * The Regents of the University of California. All rights reserved. 35 * 36 * Redistribution and use in source and binary forms, with or without 37 * modification, are permitted provided that the following conditions 38 * are met: 39 * 1. Redistributions of source code must retain the above copyright 40 * notice, this list of conditions and the following disclaimer. 41 * 2. Redistributions in binary form must reproduce the above copyright 42 * notice, this list of conditions and the following disclaimer in the 43 * documentation and/or other materials provided with the distribution. 44 * 3. Neither the name of the University nor the names of its contributors 45 * may be used to endorse or promote products derived from this software 46 * without specific prior written permission. 47 * 48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 58 * SUCH DAMAGE. 59 * 60 * @(#)if_ethersubr.c 8.2 (Berkeley) 4/4/96 61 */ 62 63 #include <sys/cdefs.h> 64 __KERNEL_RCSID(0, "$NetBSD: if_ethersubr.c,v 1.129 2006/03/16 15:57:59 christos Exp $"); 65 66 #include "opt_inet.h" 67 #include "opt_atalk.h" 68 #include "opt_ccitt.h" 69 #include "opt_llc.h" 70 #include "opt_iso.h" 71 #include "opt_ipx.h" 72 #include "opt_mbuftrace.h" 73 #include "opt_ns.h" 74 #include "opt_gateway.h" 75 #include "opt_pfil_hooks.h" 76 #include "vlan.h" 77 #include "pppoe.h" 78 #include "bridge.h" 79 #include "bpfilter.h" 80 #include "arp.h" 81 #include "agr.h" 82 83 #include <sys/param.h> 84 #include <sys/systm.h> 85 #include <sys/kernel.h> 86 #include <sys/callout.h> 87 #include <sys/malloc.h> 88 #include <sys/mbuf.h> 89 #include <sys/protosw.h> 90 #include <sys/socket.h> 91 #include <sys/ioctl.h> 92 #include <sys/errno.h> 93 #include <sys/syslog.h> 94 95 #include <machine/cpu.h> 96 97 #include <net/if.h> 98 #include <net/netisr.h> 99 #include <net/route.h> 100 #include <net/if_llc.h> 101 #include <net/if_dl.h> 102 #include <net/if_types.h> 103 104 #if NARP == 0 105 /* 106 * XXX there should really be a way to issue this warning from within config(8) 107 */ 108 #error You have included NETATALK or a pseudo-device in your configuration that depends on the presence of ethernet interfaces, but have no such interfaces configured. Check if you really need pseudo-device bridge, pppoe, vlan or options NETATALK. 109 #endif 110 111 #if NBPFILTER > 0 112 #include <net/bpf.h> 113 #endif 114 115 #include <net/if_ether.h> 116 #if NVLAN > 0 117 #include <net/if_vlanvar.h> 118 #endif 119 120 #if NPPPOE > 0 121 #include <net/if_pppoe.h> 122 #endif 123 124 #if NAGR > 0 125 #include <net/agr/ieee8023_slowprotocols.h> /* XXX */ 126 #include <net/agr/ieee8023ad.h> 127 #include <net/agr/if_agrvar.h> 128 #endif 129 130 #if NBRIDGE > 0 131 #include <net/if_bridgevar.h> 132 #endif 133 134 #include <netinet/in.h> 135 #ifdef INET 136 #include <netinet/in_var.h> 137 #endif 138 #include <netinet/if_inarp.h> 139 140 #ifdef INET6 141 #ifndef INET 142 #include <netinet/in.h> 143 #endif 144 #include <netinet6/in6_var.h> 145 #include <netinet6/nd6.h> 146 #endif 147 148 #ifdef NS 149 #include <netns/ns.h> 150 #include <netns/ns_if.h> 151 #endif 152 153 #ifdef IPX 154 #include <netipx/ipx.h> 155 #include <netipx/ipx_if.h> 156 #endif 157 158 #ifdef ISO 159 #include <netiso/argo_debug.h> 160 #include <netiso/iso.h> 161 #include <netiso/iso_var.h> 162 #include <netiso/iso_snpac.h> 163 #endif 164 165 #ifdef LLC 166 #include <netccitt/dll.h> 167 #include <netccitt/llc_var.h> 168 #endif 169 170 #if defined(LLC) && defined(CCITT) 171 extern struct ifqueue pkintrq; 172 #endif 173 174 #ifdef NETATALK 175 #include <netatalk/at.h> 176 #include <netatalk/at_var.h> 177 #include <netatalk/at_extern.h> 178 179 #define llc_snap_org_code llc_un.type_snap.org_code 180 #define llc_snap_ether_type llc_un.type_snap.ether_type 181 182 extern u_char at_org_code[3]; 183 extern u_char aarp_org_code[3]; 184 #endif /* NETATALK */ 185 186 static struct timeval bigpktppslim_last; 187 static int bigpktppslim = 2; /* XXX */ 188 static int bigpktpps_count; 189 190 191 const uint8_t etherbroadcastaddr[ETHER_ADDR_LEN] = 192 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 193 const uint8_t ethermulticastaddr_slowprotocols[ETHER_ADDR_LEN] = 194 { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x02 }; 195 #define senderr(e) { error = (e); goto bad;} 196 197 #define SIN(x) ((struct sockaddr_in *)x) 198 199 static int ether_output(struct ifnet *, struct mbuf *, 200 struct sockaddr *, struct rtentry *); 201 static void ether_input(struct ifnet *, struct mbuf *); 202 203 /* 204 * Ethernet output routine. 205 * Encapsulate a packet of type family for the local net. 206 * Assumes that ifp is actually pointer to ethercom structure. 207 */ 208 static int 209 ether_output(struct ifnet *ifp, struct mbuf *m0, struct sockaddr *dst, 210 struct rtentry *rt0) 211 { 212 u_int16_t etype = 0; 213 int error = 0, hdrcmplt = 0; 214 u_char esrc[6], edst[6]; 215 struct mbuf *m = m0; 216 struct rtentry *rt; 217 struct mbuf *mcopy = (struct mbuf *)0; 218 struct ether_header *eh; 219 ALTQ_DECL(struct altq_pktattr pktattr;) 220 #ifdef INET 221 struct arphdr *ah; 222 #endif /* INET */ 223 #ifdef NETATALK 224 struct at_ifaddr *aa; 225 #endif /* NETATALK */ 226 227 #ifdef MBUFTRACE 228 m_claimm(m, ifp->if_mowner); 229 #endif 230 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) 231 senderr(ENETDOWN); 232 if ((rt = rt0) != NULL) { 233 if ((rt->rt_flags & RTF_UP) == 0) { 234 if ((rt0 = rt = rtalloc1(dst, 1)) != NULL) { 235 rt->rt_refcnt--; 236 if (rt->rt_ifp != ifp) 237 return (*rt->rt_ifp->if_output) 238 (ifp, m0, dst, rt); 239 } else 240 senderr(EHOSTUNREACH); 241 } 242 if ((rt->rt_flags & RTF_GATEWAY) && dst->sa_family != AF_NS) { 243 if (rt->rt_gwroute == 0) 244 goto lookup; 245 if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) { 246 rtfree(rt); rt = rt0; 247 lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1); 248 if ((rt = rt->rt_gwroute) == 0) 249 senderr(EHOSTUNREACH); 250 /* the "G" test below also prevents rt == rt0 */ 251 if ((rt->rt_flags & RTF_GATEWAY) || 252 (rt->rt_ifp != ifp)) { 253 rt->rt_refcnt--; 254 rt0->rt_gwroute = 0; 255 senderr(EHOSTUNREACH); 256 } 257 } 258 } 259 if (rt->rt_flags & RTF_REJECT) 260 if (rt->rt_rmx.rmx_expire == 0 || 261 (u_long) time.tv_sec < rt->rt_rmx.rmx_expire) 262 senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH); 263 } 264 265 switch (dst->sa_family) { 266 267 #ifdef INET 268 case AF_INET: 269 if (m->m_flags & M_BCAST) 270 (void)memcpy(edst, etherbroadcastaddr, sizeof(edst)); 271 272 else if (m->m_flags & M_MCAST) { 273 ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, 274 (caddr_t)edst) 275 276 } else if (!arpresolve(ifp, rt, m, dst, edst)) 277 return (0); /* if not yet resolved */ 278 /* If broadcasting on a simplex interface, loopback a copy */ 279 if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX)) 280 mcopy = m_copy(m, 0, (int)M_COPYALL); 281 etype = htons(ETHERTYPE_IP); 282 break; 283 284 case AF_ARP: 285 ah = mtod(m, struct arphdr *); 286 if (m->m_flags & M_BCAST) 287 (void)memcpy(edst, etherbroadcastaddr, sizeof(edst)); 288 else 289 bcopy((caddr_t)ar_tha(ah), 290 (caddr_t)edst, sizeof(edst)); 291 292 ah->ar_hrd = htons(ARPHRD_ETHER); 293 294 switch (ntohs(ah->ar_op)) { 295 case ARPOP_REVREQUEST: 296 case ARPOP_REVREPLY: 297 etype = htons(ETHERTYPE_REVARP); 298 break; 299 300 case ARPOP_REQUEST: 301 case ARPOP_REPLY: 302 default: 303 etype = htons(ETHERTYPE_ARP); 304 } 305 306 break; 307 #endif 308 #ifdef INET6 309 case AF_INET6: 310 if (!nd6_storelladdr(ifp, rt, m, dst, (u_char *)edst)){ 311 /* something bad happened */ 312 return (0); 313 } 314 etype = htons(ETHERTYPE_IPV6); 315 break; 316 #endif 317 #ifdef NETATALK 318 case AF_APPLETALK: 319 if (!aarpresolve(ifp, m, (struct sockaddr_at *)dst, edst)) { 320 #ifdef NETATALKDEBUG 321 printf("aarpresolv failed\n"); 322 #endif /* NETATALKDEBUG */ 323 return (0); 324 } 325 /* 326 * ifaddr is the first thing in at_ifaddr 327 */ 328 aa = (struct at_ifaddr *) at_ifawithnet( 329 (struct sockaddr_at *)dst, ifp); 330 if (aa == NULL) 331 goto bad; 332 333 /* 334 * In the phase 2 case, we need to prepend an mbuf for the 335 * llc header. Since we must preserve the value of m, 336 * which is passed to us by value, we m_copy() the first 337 * mbuf, and use it for our llc header. 338 */ 339 if (aa->aa_flags & AFA_PHASE2) { 340 struct llc llc; 341 342 M_PREPEND(m, sizeof(struct llc), M_DONTWAIT); 343 llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP; 344 llc.llc_control = LLC_UI; 345 bcopy(at_org_code, llc.llc_snap_org_code, 346 sizeof(llc.llc_snap_org_code)); 347 llc.llc_snap_ether_type = htons(ETHERTYPE_ATALK); 348 bcopy(&llc, mtod(m, caddr_t), sizeof(struct llc)); 349 } else { 350 etype = htons(ETHERTYPE_ATALK); 351 } 352 break; 353 #endif /* NETATALK */ 354 #ifdef NS 355 case AF_NS: 356 etype = htons(ETHERTYPE_NS); 357 bcopy((caddr_t)&(((struct sockaddr_ns *)dst)->sns_addr.x_host), 358 (caddr_t)edst, sizeof (edst)); 359 if (!bcmp((caddr_t)edst, (caddr_t)&ns_thishost, sizeof(edst))) 360 return (looutput(ifp, m, dst, rt)); 361 /* If broadcasting on a simplex interface, loopback a copy */ 362 if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX)) 363 mcopy = m_copy(m, 0, (int)M_COPYALL); 364 break; 365 #endif 366 #ifdef IPX 367 case AF_IPX: 368 etype = htons(ETHERTYPE_IPX); 369 bcopy((caddr_t)&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host), 370 (caddr_t)edst, sizeof (edst)); 371 /* If broadcasting on a simplex interface, loopback a copy */ 372 if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX)) 373 mcopy = m_copy(m, 0, (int)M_COPYALL); 374 break; 375 #endif 376 #ifdef ISO 377 case AF_ISO: { 378 int snpalen; 379 struct llc *l; 380 struct sockaddr_dl *sdl; 381 382 if (rt && (sdl = (struct sockaddr_dl *)rt->rt_gateway) && 383 sdl->sdl_family == AF_LINK && sdl->sdl_alen > 0) { 384 bcopy(LLADDR(sdl), (caddr_t)edst, sizeof(edst)); 385 } else { 386 error = iso_snparesolve(ifp, (struct sockaddr_iso *)dst, 387 (char *)edst, &snpalen); 388 if (error) 389 goto bad; /* Not Resolved */ 390 } 391 /* If broadcasting on a simplex interface, loopback a copy */ 392 if (*edst & 1) 393 m->m_flags |= (M_BCAST|M_MCAST); 394 if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX) && 395 (mcopy = m_copy(m, 0, (int)M_COPYALL))) { 396 M_PREPEND(mcopy, sizeof (*eh), M_DONTWAIT); 397 if (mcopy) { 398 eh = mtod(mcopy, struct ether_header *); 399 bcopy((caddr_t)edst, 400 (caddr_t)eh->ether_dhost, sizeof (edst)); 401 bcopy(LLADDR(ifp->if_sadl), 402 (caddr_t)eh->ether_shost, sizeof (edst)); 403 } 404 } 405 M_PREPEND(m, 3, M_DONTWAIT); 406 if (m == NULL) 407 return (0); 408 l = mtod(m, struct llc *); 409 l->llc_dsap = l->llc_ssap = LLC_ISO_LSAP; 410 l->llc_control = LLC_UI; 411 #ifdef ARGO_DEBUG 412 if (argo_debug[D_ETHER]) { 413 int i; 414 printf("unoutput: sending pkt to: "); 415 for (i=0; i<6; i++) 416 printf("%x ", edst[i] & 0xff); 417 printf("\n"); 418 } 419 #endif 420 } break; 421 #endif /* ISO */ 422 #ifdef LLC 423 /* case AF_NSAP: */ 424 case AF_CCITT: { 425 struct sockaddr_dl *sdl = 426 (struct sockaddr_dl *) rt -> rt_gateway; 427 428 if (sdl && sdl->sdl_family == AF_LINK 429 && sdl->sdl_alen > 0) { 430 bcopy(LLADDR(sdl), (char *)edst, 431 sizeof(edst)); 432 } else goto bad; /* Not a link interface ? Funny ... */ 433 if ((ifp->if_flags & IFF_SIMPLEX) && (*edst & 1) && 434 (mcopy = m_copy(m, 0, (int)M_COPYALL))) { 435 M_PREPEND(mcopy, sizeof (*eh), M_DONTWAIT); 436 if (mcopy) { 437 eh = mtod(mcopy, struct ether_header *); 438 bcopy((caddr_t)edst, 439 (caddr_t)eh->ether_dhost, sizeof (edst)); 440 bcopy(LLADDR(ifp->if_sadl), 441 (caddr_t)eh->ether_shost, sizeof (edst)); 442 } 443 } 444 #ifdef LLC_DEBUG 445 { 446 int i; 447 struct llc *l = mtod(m, struct llc *); 448 449 printf("ether_output: sending LLC2 pkt to: "); 450 for (i=0; i<6; i++) 451 printf("%x ", edst[i] & 0xff); 452 printf(" len 0x%x dsap 0x%x ssap 0x%x control 0x%x\n", 453 m->m_pkthdr.len, l->llc_dsap & 0xff, l->llc_ssap &0xff, 454 l->llc_control & 0xff); 455 456 } 457 #endif /* LLC_DEBUG */ 458 } break; 459 #endif /* LLC */ 460 461 case pseudo_AF_HDRCMPLT: 462 hdrcmplt = 1; 463 eh = (struct ether_header *)dst->sa_data; 464 bcopy((caddr_t)eh->ether_shost, (caddr_t)esrc, sizeof (esrc)); 465 /* FALLTHROUGH */ 466 467 case AF_UNSPEC: 468 eh = (struct ether_header *)dst->sa_data; 469 bcopy((caddr_t)eh->ether_dhost, (caddr_t)edst, sizeof (edst)); 470 /* AF_UNSPEC doesn't swap the byte order of the ether_type. */ 471 etype = eh->ether_type; 472 break; 473 474 default: 475 printf("%s: can't handle af%d\n", ifp->if_xname, 476 dst->sa_family); 477 senderr(EAFNOSUPPORT); 478 } 479 480 if (mcopy) 481 (void) looutput(ifp, mcopy, dst, rt); 482 483 /* If no ether type is set, this must be a 802.2 formatted packet. 484 */ 485 if (etype == 0) 486 etype = htons(m->m_pkthdr.len); 487 /* 488 * Add local net header. If no space in first mbuf, 489 * allocate another. 490 */ 491 M_PREPEND(m, sizeof (struct ether_header), M_DONTWAIT); 492 if (m == 0) 493 senderr(ENOBUFS); 494 eh = mtod(m, struct ether_header *); 495 /* Note: etype is already in network byte order. */ 496 #ifdef __NO_STRICT_ALIGNMENT 497 eh->ether_type = etype; 498 #else 499 { 500 uint8_t *dstp = (uint8_t *) &eh->ether_type; 501 #if BYTE_ORDER == BIG_ENDIAN 502 dstp[0] = etype >> 8; 503 dstp[1] = etype; 504 #else 505 dstp[0] = etype; 506 dstp[1] = etype >> 8; 507 #endif /* BYTE_ORDER == BIG_ENDIAN */ 508 } 509 #endif /* __NO_STRICT_ALIGNMENT */ 510 bcopy((caddr_t)edst, (caddr_t)eh->ether_dhost, sizeof (edst)); 511 if (hdrcmplt) 512 bcopy((caddr_t)esrc, (caddr_t)eh->ether_shost, 513 sizeof(eh->ether_shost)); 514 else 515 bcopy(LLADDR(ifp->if_sadl), (caddr_t)eh->ether_shost, 516 sizeof(eh->ether_shost)); 517 518 #ifdef PFIL_HOOKS 519 if ((error = pfil_run_hooks(&ifp->if_pfil, &m, ifp, PFIL_OUT)) != 0) 520 return (error); 521 if (m == NULL) 522 return (0); 523 #endif 524 525 #if NBRIDGE > 0 526 /* 527 * Bridges require special output handling. 528 */ 529 if (ifp->if_bridge) 530 return (bridge_output(ifp, m, NULL, NULL)); 531 #endif 532 533 #ifdef ALTQ 534 /* 535 * If ALTQ is enabled on the parent interface, do 536 * classification; the queueing discipline might not 537 * require classification, but might require the 538 * address family/header pointer in the pktattr. 539 */ 540 if (ALTQ_IS_ENABLED(&ifp->if_snd)) 541 altq_etherclassify(&ifp->if_snd, m, &pktattr); 542 #endif 543 544 return ifq_enqueue(ifp, m ALTQ_COMMA ALTQ_DECL(&pktattr)); 545 546 bad: 547 if (m) 548 m_freem(m); 549 return (error); 550 } 551 552 #ifdef ALTQ 553 /* 554 * This routine is a slight hack to allow a packet to be classified 555 * if the Ethernet headers are present. It will go away when ALTQ's 556 * classification engine understands link headers. 557 */ 558 void 559 altq_etherclassify(struct ifaltq *ifq, struct mbuf *m, 560 struct altq_pktattr *pktattr) 561 { 562 struct ether_header *eh; 563 u_int16_t ether_type; 564 int hlen, af, hdrsize; 565 caddr_t hdr; 566 567 hlen = ETHER_HDR_LEN; 568 eh = mtod(m, struct ether_header *); 569 570 ether_type = htons(eh->ether_type); 571 572 if (ether_type < ETHERMTU) { 573 /* LLC/SNAP */ 574 struct llc *llc = (struct llc *)(eh + 1); 575 hlen += 8; 576 577 if (m->m_len < hlen || 578 llc->llc_dsap != LLC_SNAP_LSAP || 579 llc->llc_ssap != LLC_SNAP_LSAP || 580 llc->llc_control != LLC_UI) { 581 /* Not SNAP. */ 582 goto bad; 583 } 584 585 ether_type = htons(llc->llc_un.type_snap.ether_type); 586 } 587 588 switch (ether_type) { 589 case ETHERTYPE_IP: 590 af = AF_INET; 591 hdrsize = 20; /* sizeof(struct ip) */ 592 break; 593 594 case ETHERTYPE_IPV6: 595 af = AF_INET6; 596 hdrsize = 40; /* sizeof(struct ip6_hdr) */ 597 break; 598 599 default: 600 af = AF_UNSPEC; 601 hdrsize = 0; 602 break; 603 } 604 605 while (m->m_len <= hlen) { 606 hlen -= m->m_len; 607 m = m->m_next; 608 } 609 if (m->m_len < (hlen + hdrsize)) { 610 /* 611 * protocol header not in a single mbuf. 612 * We can't cope with this situation right 613 * now (but it shouldn't ever happen, really, anyhow). 614 */ 615 #ifdef DEBUG 616 printf("altq_etherclassify: headers span multiple mbufs: " 617 "%d < %d\n", m->m_len, (hlen + hdrsize)); 618 #endif 619 goto bad; 620 } 621 622 m->m_data += hlen; 623 m->m_len -= hlen; 624 625 hdr = mtod(m, caddr_t); 626 627 if (ALTQ_NEEDS_CLASSIFY(ifq)) 628 pktattr->pattr_class = 629 (*ifq->altq_classify)(ifq->altq_clfier, m, af); 630 pktattr->pattr_af = af; 631 pktattr->pattr_hdr = hdr; 632 633 m->m_data -= hlen; 634 m->m_len += hlen; 635 636 return; 637 638 bad: 639 pktattr->pattr_class = NULL; 640 pktattr->pattr_hdr = NULL; 641 pktattr->pattr_af = AF_UNSPEC; 642 } 643 #endif /* ALTQ */ 644 645 /* 646 * Process a received Ethernet packet; 647 * the packet is in the mbuf chain m with 648 * the ether header. 649 */ 650 static void 651 ether_input(struct ifnet *ifp, struct mbuf *m) 652 { 653 struct ethercom *ec = (struct ethercom *) ifp; 654 struct ifqueue *inq; 655 u_int16_t etype; 656 struct ether_header *eh; 657 #if defined (ISO) || defined (LLC) || defined(NETATALK) 658 struct llc *l; 659 #endif 660 661 if ((ifp->if_flags & IFF_UP) == 0) { 662 m_freem(m); 663 return; 664 } 665 666 #ifdef MBUFTRACE 667 m_claimm(m, &ec->ec_rx_mowner); 668 #endif 669 eh = mtod(m, struct ether_header *); 670 etype = ntohs(eh->ether_type); 671 672 /* 673 * Determine if the packet is within its size limits. 674 */ 675 if (m->m_pkthdr.len > 676 ETHER_MAX_FRAME(ifp, etype, m->m_flags & M_HASFCS)) { 677 if (ppsratecheck(&bigpktppslim_last, &bigpktpps_count, 678 bigpktppslim)) { 679 printf("%s: discarding oversize frame (len=%d)\n", 680 ifp->if_xname, m->m_pkthdr.len); 681 } 682 m_freem(m); 683 return; 684 } 685 686 if (ETHER_IS_MULTICAST(eh->ether_dhost)) { 687 /* 688 * If this is not a simplex interface, drop the packet 689 * if it came from us. 690 */ 691 if ((ifp->if_flags & IFF_SIMPLEX) == 0 && 692 memcmp(LLADDR(ifp->if_sadl), eh->ether_shost, 693 ETHER_ADDR_LEN) == 0) { 694 m_freem(m); 695 return; 696 } 697 698 if (memcmp(etherbroadcastaddr, 699 eh->ether_dhost, ETHER_ADDR_LEN) == 0) 700 m->m_flags |= M_BCAST; 701 else 702 m->m_flags |= M_MCAST; 703 ifp->if_imcasts++; 704 } 705 706 /* If the CRC is still on the packet, trim it off. */ 707 if (m->m_flags & M_HASFCS) { 708 m_adj(m, -ETHER_CRC_LEN); 709 m->m_flags &= ~M_HASFCS; 710 } 711 712 ifp->if_ibytes += m->m_pkthdr.len; 713 714 #if NBRIDGE > 0 715 /* 716 * Tap the packet off here for a bridge. bridge_input() 717 * will return NULL if it has consumed the packet, otherwise 718 * it gets processed as normal. Note that bridge_input() 719 * will always return the original packet if we need to 720 * process it locally. 721 */ 722 if (ifp->if_bridge) { 723 if(m->m_flags & M_PROTO1) { 724 m->m_flags &= ~M_PROTO1; 725 } else { 726 /* clear M_PROMISC, in case the packets comes from a vlan */ 727 m->m_flags &= ~M_PROMISC; 728 m = bridge_input(ifp, m); 729 if (m == NULL) 730 return; 731 732 /* 733 * Bridge has determined that the packet is for us. 734 * Update our interface pointer -- we may have had 735 * to "bridge" the packet locally. 736 */ 737 ifp = m->m_pkthdr.rcvif; 738 } 739 } else 740 #endif /* NBRIDGE > 0 */ 741 { 742 if ((m->m_flags & (M_BCAST|M_MCAST)) == 0 && 743 (ifp->if_flags & IFF_PROMISC) != 0 && 744 memcmp(LLADDR(ifp->if_sadl), eh->ether_dhost, 745 ETHER_ADDR_LEN) != 0) { 746 m->m_flags |= M_PROMISC; 747 } 748 } 749 750 #ifdef PFIL_HOOKS 751 if ((m->m_flags & M_PROMISC) == 0) { 752 if (pfil_run_hooks(&ifp->if_pfil, &m, ifp, PFIL_IN) != 0) 753 return; 754 if (m == NULL) 755 return; 756 757 eh = mtod(m, struct ether_header *); 758 etype = ntohs(eh->ether_type); 759 } 760 #endif 761 762 /* 763 * If VLANs are configured on the interface, check to 764 * see if the device performed the decapsulation and 765 * provided us with the tag. 766 */ 767 if (ec->ec_nvlans && m_tag_find(m, PACKET_TAG_VLAN, NULL) != NULL) { 768 #if NVLAN > 0 769 /* 770 * vlan_input() will either recursively call ether_input() 771 * or drop the packet. 772 */ 773 vlan_input(ifp, m); 774 #else 775 m_freem(m); 776 #endif 777 return; 778 } 779 780 #if NAGR > 0 781 if (ifp->if_agrprivate && 782 __predict_true(etype != ETHERTYPE_SLOWPROTOCOLS)) { 783 m->m_flags &= ~M_PROMISC; 784 agr_input(ifp, m); 785 return; 786 } 787 #endif /* NAGR > 0 */ 788 789 /* 790 * Handle protocols that expect to have the Ethernet header 791 * (and possibly FCS) intact. 792 */ 793 switch (etype) { 794 #if NVLAN > 0 795 case ETHERTYPE_VLAN: 796 /* 797 * vlan_input() will either recursively call ether_input() 798 * or drop the packet. 799 */ 800 if (((struct ethercom *)ifp)->ec_nvlans != 0) 801 vlan_input(ifp, m); 802 else 803 m_freem(m); 804 return; 805 #endif /* NVLAN > 0 */ 806 #if NPPPOE > 0 807 case ETHERTYPE_PPPOEDISC: 808 case ETHERTYPE_PPPOE: 809 if (m->m_flags & M_PROMISC) { 810 m_freem(m); 811 return; 812 } 813 #ifndef PPPOE_SERVER 814 if (m->m_flags & (M_MCAST | M_BCAST)) { 815 m_freem(m); 816 return; 817 } 818 #endif 819 820 if (etype == ETHERTYPE_PPPOEDISC) 821 inq = &ppoediscinq; 822 else 823 inq = &ppoeinq; 824 if (IF_QFULL(inq)) { 825 IF_DROP(inq); 826 m_freem(m); 827 } else 828 IF_ENQUEUE(inq, m); 829 #ifndef __HAVE_GENERIC_SOFT_INTERRUPTS 830 if (!callout_pending(&pppoe_softintr)) 831 callout_reset(&pppoe_softintr, 1, pppoe_softintr_handler, NULL); 832 #else 833 softintr_schedule(pppoe_softintr); 834 #endif 835 return; 836 #endif /* NPPPOE > 0 */ 837 case ETHERTYPE_SLOWPROTOCOLS: { 838 uint8_t subtype; 839 840 #if defined(DIAGNOSTIC) 841 if (m->m_pkthdr.len < sizeof(*eh) + sizeof(subtype)) { 842 panic("ether_input: too short slow protocol packet"); 843 } 844 #endif 845 m_copydata(m, sizeof(*eh), sizeof(subtype), &subtype); 846 switch (subtype) { 847 #if NAGR > 0 848 case SLOWPROTOCOLS_SUBTYPE_LACP: 849 if (ifp->if_agrprivate) { 850 ieee8023ad_lacp_input(ifp, m); 851 return; 852 } 853 break; 854 855 case SLOWPROTOCOLS_SUBTYPE_MARKER: 856 if (ifp->if_agrprivate) { 857 ieee8023ad_marker_input(ifp, m); 858 return; 859 } 860 break; 861 #endif /* NAGR > 0 */ 862 default: 863 if (subtype == 0 || subtype > 10) { 864 /* illegal value */ 865 m_freem(m); 866 return; 867 } 868 /* unknown subtype */ 869 break; 870 } 871 /* FALLTHROUGH */ 872 } 873 default: 874 if (m->m_flags & M_PROMISC) { 875 m_freem(m); 876 return; 877 } 878 } 879 880 /* Strip off the Ethernet header. */ 881 m_adj(m, sizeof(struct ether_header)); 882 883 /* If the CRC is still on the packet, trim it off. */ 884 if (m->m_flags & M_HASFCS) { 885 m_adj(m, -ETHER_CRC_LEN); 886 m->m_flags &= ~M_HASFCS; 887 } 888 889 switch (etype) { 890 #ifdef INET 891 case ETHERTYPE_IP: 892 #ifdef GATEWAY 893 if (ipflow_fastforward(m)) 894 return; 895 #endif 896 schednetisr(NETISR_IP); 897 inq = &ipintrq; 898 break; 899 900 case ETHERTYPE_ARP: 901 schednetisr(NETISR_ARP); 902 inq = &arpintrq; 903 break; 904 905 case ETHERTYPE_REVARP: 906 revarpinput(m); /* XXX queue? */ 907 return; 908 #endif 909 #ifdef INET6 910 case ETHERTYPE_IPV6: 911 schednetisr(NETISR_IPV6); 912 inq = &ip6intrq; 913 break; 914 #endif 915 #ifdef NS 916 case ETHERTYPE_NS: 917 schednetisr(NETISR_NS); 918 inq = &nsintrq; 919 break; 920 921 #endif 922 #ifdef IPX 923 case ETHERTYPE_IPX: 924 schednetisr(NETISR_IPX); 925 inq = &ipxintrq; 926 break; 927 #endif 928 #ifdef NETATALK 929 case ETHERTYPE_ATALK: 930 schednetisr(NETISR_ATALK); 931 inq = &atintrq1; 932 break; 933 case ETHERTYPE_AARP: 934 /* probably this should be done with a NETISR as well */ 935 aarpinput(ifp, m); /* XXX */ 936 return; 937 #endif /* NETATALK */ 938 default: 939 #if defined (ISO) || defined (LLC) || defined (NETATALK) 940 if (etype > ETHERMTU) 941 goto dropanyway; 942 l = mtod(m, struct llc *); 943 switch (l->llc_dsap) { 944 #ifdef NETATALK 945 case LLC_SNAP_LSAP: 946 switch (l->llc_control) { 947 case LLC_UI: 948 if (l->llc_ssap != LLC_SNAP_LSAP) { 949 goto dropanyway; 950 } 951 952 if (Bcmp(&(l->llc_snap_org_code)[0], 953 at_org_code, sizeof(at_org_code)) == 0 && 954 ntohs(l->llc_snap_ether_type) == 955 ETHERTYPE_ATALK) { 956 inq = &atintrq2; 957 m_adj(m, sizeof(struct llc)); 958 schednetisr(NETISR_ATALK); 959 break; 960 } 961 962 if (Bcmp(&(l->llc_snap_org_code)[0], 963 aarp_org_code, 964 sizeof(aarp_org_code)) == 0 && 965 ntohs(l->llc_snap_ether_type) == 966 ETHERTYPE_AARP) { 967 m_adj( m, sizeof(struct llc)); 968 aarpinput(ifp, m); /* XXX */ 969 return; 970 } 971 972 default: 973 goto dropanyway; 974 } 975 break; 976 #endif /* NETATALK */ 977 #ifdef ISO 978 case LLC_ISO_LSAP: 979 switch (l->llc_control) { 980 case LLC_UI: 981 /* LLC_UI_P forbidden in class 1 service */ 982 if ((l->llc_dsap == LLC_ISO_LSAP) && 983 (l->llc_ssap == LLC_ISO_LSAP)) { 984 /* LSAP for ISO */ 985 if (m->m_pkthdr.len > etype) 986 m_adj(m, etype - m->m_pkthdr.len); 987 m->m_data += 3; /* XXX */ 988 m->m_len -= 3; /* XXX */ 989 m->m_pkthdr.len -= 3; /* XXX */ 990 M_PREPEND(m, sizeof *eh, M_DONTWAIT); 991 if (m == 0) 992 return; 993 *mtod(m, struct ether_header *) = *eh; 994 #ifdef ARGO_DEBUG 995 if (argo_debug[D_ETHER]) 996 printf("clnp packet"); 997 #endif 998 schednetisr(NETISR_ISO); 999 inq = &clnlintrq; 1000 break; 1001 } 1002 goto dropanyway; 1003 1004 case LLC_XID: 1005 case LLC_XID_P: 1006 if(m->m_len < 6) 1007 goto dropanyway; 1008 l->llc_window = 0; 1009 l->llc_fid = 9; 1010 l->llc_class = 1; 1011 l->llc_dsap = l->llc_ssap = 0; 1012 /* Fall through to */ 1013 case LLC_TEST: 1014 case LLC_TEST_P: 1015 { 1016 struct sockaddr sa; 1017 struct ether_header *eh2; 1018 int i; 1019 u_char c = l->llc_dsap; 1020 1021 l->llc_dsap = l->llc_ssap; 1022 l->llc_ssap = c; 1023 if (m->m_flags & (M_BCAST | M_MCAST)) 1024 bcopy(LLADDR(ifp->if_sadl), 1025 (caddr_t)eh->ether_dhost, 6); 1026 sa.sa_family = AF_UNSPEC; 1027 sa.sa_len = sizeof(sa); 1028 eh2 = (struct ether_header *)sa.sa_data; 1029 for (i = 0; i < 6; i++) { 1030 eh2->ether_shost[i] = c = 1031 eh->ether_dhost[i]; 1032 eh2->ether_dhost[i] = 1033 eh->ether_dhost[i] = 1034 eh->ether_shost[i]; 1035 eh->ether_shost[i] = c; 1036 } 1037 ifp->if_output(ifp, m, &sa, NULL); 1038 return; 1039 } 1040 default: 1041 m_freem(m); 1042 return; 1043 } 1044 break; 1045 #endif /* ISO */ 1046 #ifdef LLC 1047 case LLC_X25_LSAP: 1048 { 1049 if (m->m_pkthdr.len > etype) 1050 m_adj(m, etype - m->m_pkthdr.len); 1051 M_PREPEND(m, sizeof(struct sdl_hdr) , M_DONTWAIT); 1052 if (m == 0) 1053 return; 1054 if ( !sdl_sethdrif(ifp, eh->ether_shost, LLC_X25_LSAP, 1055 eh->ether_dhost, LLC_X25_LSAP, 6, 1056 mtod(m, struct sdl_hdr *))) 1057 panic("ETHER cons addr failure"); 1058 mtod(m, struct sdl_hdr *)->sdlhdr_len = etype; 1059 #ifdef LLC_DEBUG 1060 printf("llc packet\n"); 1061 #endif /* LLC_DEBUG */ 1062 schednetisr(NETISR_CCITT); 1063 inq = &llcintrq; 1064 break; 1065 } 1066 #endif /* LLC */ 1067 dropanyway: 1068 default: 1069 m_freem(m); 1070 return; 1071 } 1072 #else /* ISO || LLC || NETATALK*/ 1073 m_freem(m); 1074 return; 1075 #endif /* ISO || LLC || NETATALK*/ 1076 } 1077 1078 if (IF_QFULL(inq)) { 1079 IF_DROP(inq); 1080 m_freem(m); 1081 } else 1082 IF_ENQUEUE(inq, m); 1083 } 1084 1085 /* 1086 * Convert Ethernet address to printable (loggable) representation. 1087 */ 1088 char * 1089 ether_sprintf(const u_char *ap) 1090 { 1091 static char etherbuf[3 * ETHER_ADDR_LEN]; 1092 return ether_snprintf(etherbuf, sizeof(etherbuf), ap); 1093 return etherbuf; 1094 } 1095 1096 char * 1097 ether_snprintf(char *buf, size_t len, const u_char *ap) 1098 { 1099 char *cp = buf; 1100 size_t i; 1101 1102 for (i = 0; i < len / 3; i++) { 1103 *cp++ = hexdigits[*ap >> 4]; 1104 *cp++ = hexdigits[*ap++ & 0xf]; 1105 *cp++ = ':'; 1106 } 1107 *--cp = '\0'; 1108 return buf; 1109 } 1110 1111 /* 1112 * Perform common duties while attaching to interface list 1113 */ 1114 void 1115 ether_ifattach(struct ifnet *ifp, const u_int8_t *lla) 1116 { 1117 struct ethercom *ec = (struct ethercom *)ifp; 1118 1119 ifp->if_type = IFT_ETHER; 1120 ifp->if_addrlen = ETHER_ADDR_LEN; 1121 ifp->if_hdrlen = ETHER_HDR_LEN; 1122 ifp->if_dlt = DLT_EN10MB; 1123 ifp->if_mtu = ETHERMTU; 1124 ifp->if_output = ether_output; 1125 ifp->if_input = ether_input; 1126 if (ifp->if_baudrate == 0) 1127 ifp->if_baudrate = IF_Mbps(10); /* just a default */ 1128 1129 if_alloc_sadl(ifp); 1130 memcpy(LLADDR(ifp->if_sadl), lla, ifp->if_addrlen); 1131 1132 LIST_INIT(&ec->ec_multiaddrs); 1133 ifp->if_broadcastaddr = etherbroadcastaddr; 1134 #if NBPFILTER > 0 1135 bpfattach(ifp, DLT_EN10MB, sizeof(struct ether_header)); 1136 #endif 1137 #ifdef MBUFTRACE 1138 strlcpy(ec->ec_tx_mowner.mo_name, ifp->if_xname, 1139 sizeof(ec->ec_tx_mowner.mo_name)); 1140 strlcpy(ec->ec_tx_mowner.mo_descr, "tx", 1141 sizeof(ec->ec_tx_mowner.mo_descr)); 1142 strlcpy(ec->ec_rx_mowner.mo_name, ifp->if_xname, 1143 sizeof(ec->ec_rx_mowner.mo_name)); 1144 strlcpy(ec->ec_rx_mowner.mo_descr, "rx", 1145 sizeof(ec->ec_rx_mowner.mo_descr)); 1146 MOWNER_ATTACH(&ec->ec_tx_mowner); 1147 MOWNER_ATTACH(&ec->ec_rx_mowner); 1148 ifp->if_mowner = &ec->ec_tx_mowner; 1149 #endif 1150 } 1151 1152 void 1153 ether_ifdetach(struct ifnet *ifp) 1154 { 1155 struct ethercom *ec = (void *) ifp; 1156 struct ether_multi *enm; 1157 int s; 1158 1159 #if NBRIDGE > 0 1160 if (ifp->if_bridge) 1161 bridge_ifdetach(ifp); 1162 #endif 1163 1164 #if NBPFILTER > 0 1165 bpfdetach(ifp); 1166 #endif 1167 1168 #if NVLAN > 0 1169 if (ec->ec_nvlans) 1170 vlan_ifdetach(ifp); 1171 #endif 1172 1173 s = splnet(); 1174 while ((enm = LIST_FIRST(&ec->ec_multiaddrs)) != NULL) { 1175 LIST_REMOVE(enm, enm_list); 1176 free(enm, M_IFMADDR); 1177 ec->ec_multicnt--; 1178 } 1179 splx(s); 1180 1181 #if 0 /* done in if_detach() */ 1182 if_free_sadl(ifp); 1183 #endif 1184 1185 MOWNER_DETACH(&ec->ec_rx_mowner); 1186 MOWNER_DETACH(&ec->ec_tx_mowner); 1187 } 1188 1189 #if 0 1190 /* 1191 * This is for reference. We have a table-driven version 1192 * of the little-endian crc32 generator, which is faster 1193 * than the double-loop. 1194 */ 1195 u_int32_t 1196 ether_crc32_le(const u_int8_t *buf, size_t len) 1197 { 1198 u_int32_t c, crc, carry; 1199 size_t i, j; 1200 1201 crc = 0xffffffffU; /* initial value */ 1202 1203 for (i = 0; i < len; i++) { 1204 c = buf[i]; 1205 for (j = 0; j < 8; j++) { 1206 carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01); 1207 crc >>= 1; 1208 c >>= 1; 1209 if (carry) 1210 crc = (crc ^ ETHER_CRC_POLY_LE); 1211 } 1212 } 1213 1214 return (crc); 1215 } 1216 #else 1217 u_int32_t 1218 ether_crc32_le(const u_int8_t *buf, size_t len) 1219 { 1220 static const u_int32_t crctab[] = { 1221 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac, 1222 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c, 1223 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c, 1224 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c 1225 }; 1226 u_int32_t crc; 1227 size_t i; 1228 1229 crc = 0xffffffffU; /* initial value */ 1230 1231 for (i = 0; i < len; i++) { 1232 crc ^= buf[i]; 1233 crc = (crc >> 4) ^ crctab[crc & 0xf]; 1234 crc = (crc >> 4) ^ crctab[crc & 0xf]; 1235 } 1236 1237 return (crc); 1238 } 1239 #endif 1240 1241 u_int32_t 1242 ether_crc32_be(const u_int8_t *buf, size_t len) 1243 { 1244 u_int32_t c, crc, carry; 1245 size_t i, j; 1246 1247 crc = 0xffffffffU; /* initial value */ 1248 1249 for (i = 0; i < len; i++) { 1250 c = buf[i]; 1251 for (j = 0; j < 8; j++) { 1252 carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01); 1253 crc <<= 1; 1254 c >>= 1; 1255 if (carry) 1256 crc = (crc ^ ETHER_CRC_POLY_BE) | carry; 1257 } 1258 } 1259 1260 return (crc); 1261 } 1262 1263 #ifdef INET 1264 const uint8_t ether_ipmulticast_min[ETHER_ADDR_LEN] = 1265 { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 }; 1266 const uint8_t ether_ipmulticast_max[ETHER_ADDR_LEN] = 1267 { 0x01, 0x00, 0x5e, 0x7f, 0xff, 0xff }; 1268 #endif 1269 #ifdef INET6 1270 const uint8_t ether_ip6multicast_min[ETHER_ADDR_LEN] = 1271 { 0x33, 0x33, 0x00, 0x00, 0x00, 0x00 }; 1272 const uint8_t ether_ip6multicast_max[ETHER_ADDR_LEN] = 1273 { 0x33, 0x33, 0xff, 0xff, 0xff, 0xff }; 1274 #endif 1275 1276 /* 1277 * Convert a sockaddr into an Ethernet address or range of Ethernet 1278 * addresses. 1279 */ 1280 int 1281 ether_multiaddr(struct sockaddr *sa, u_int8_t addrlo[ETHER_ADDR_LEN], 1282 u_int8_t addrhi[ETHER_ADDR_LEN]) 1283 { 1284 #ifdef INET 1285 struct sockaddr_in *sin; 1286 #endif /* INET */ 1287 #ifdef INET6 1288 struct sockaddr_in6 *sin6; 1289 #endif /* INET6 */ 1290 1291 switch (sa->sa_family) { 1292 1293 case AF_UNSPEC: 1294 bcopy(sa->sa_data, addrlo, ETHER_ADDR_LEN); 1295 bcopy(addrlo, addrhi, ETHER_ADDR_LEN); 1296 break; 1297 1298 #ifdef INET 1299 case AF_INET: 1300 sin = satosin(sa); 1301 if (sin->sin_addr.s_addr == INADDR_ANY) { 1302 /* 1303 * An IP address of INADDR_ANY means listen to 1304 * or stop listening to all of the Ethernet 1305 * multicast addresses used for IP. 1306 * (This is for the sake of IP multicast routers.) 1307 */ 1308 bcopy(ether_ipmulticast_min, addrlo, ETHER_ADDR_LEN); 1309 bcopy(ether_ipmulticast_max, addrhi, ETHER_ADDR_LEN); 1310 } 1311 else { 1312 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo); 1313 bcopy(addrlo, addrhi, ETHER_ADDR_LEN); 1314 } 1315 break; 1316 #endif 1317 #ifdef INET6 1318 case AF_INET6: 1319 sin6 = satosin6(sa); 1320 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 1321 /* 1322 * An IP6 address of 0 means listen to or stop 1323 * listening to all of the Ethernet multicast 1324 * address used for IP6. 1325 * (This is used for multicast routers.) 1326 */ 1327 bcopy(ether_ip6multicast_min, addrlo, ETHER_ADDR_LEN); 1328 bcopy(ether_ip6multicast_max, addrhi, ETHER_ADDR_LEN); 1329 } else { 1330 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, addrlo); 1331 bcopy(addrlo, addrhi, ETHER_ADDR_LEN); 1332 } 1333 break; 1334 #endif 1335 1336 default: 1337 return (EAFNOSUPPORT); 1338 } 1339 return (0); 1340 } 1341 1342 /* 1343 * Add an Ethernet multicast address or range of addresses to the list for a 1344 * given interface. 1345 */ 1346 int 1347 ether_addmulti(struct ifreq *ifr, struct ethercom *ec) 1348 { 1349 struct ether_multi *enm; 1350 u_char addrlo[ETHER_ADDR_LEN]; 1351 u_char addrhi[ETHER_ADDR_LEN]; 1352 int s = splnet(), error; 1353 1354 error = ether_multiaddr(&ifr->ifr_addr, addrlo, addrhi); 1355 if (error != 0) { 1356 splx(s); 1357 return (error); 1358 } 1359 1360 /* 1361 * Verify that we have valid Ethernet multicast addresses. 1362 */ 1363 if ((addrlo[0] & 0x01) != 1 || (addrhi[0] & 0x01) != 1) { 1364 splx(s); 1365 return (EINVAL); 1366 } 1367 /* 1368 * See if the address range is already in the list. 1369 */ 1370 ETHER_LOOKUP_MULTI(addrlo, addrhi, ec, enm); 1371 if (enm != NULL) { 1372 /* 1373 * Found it; just increment the reference count. 1374 */ 1375 ++enm->enm_refcount; 1376 splx(s); 1377 return (0); 1378 } 1379 /* 1380 * New address or range; malloc a new multicast record 1381 * and link it into the interface's multicast list. 1382 */ 1383 enm = (struct ether_multi *)malloc(sizeof(*enm), M_IFMADDR, M_NOWAIT); 1384 if (enm == NULL) { 1385 splx(s); 1386 return (ENOBUFS); 1387 } 1388 bcopy(addrlo, enm->enm_addrlo, 6); 1389 bcopy(addrhi, enm->enm_addrhi, 6); 1390 enm->enm_refcount = 1; 1391 LIST_INSERT_HEAD(&ec->ec_multiaddrs, enm, enm_list); 1392 ec->ec_multicnt++; 1393 splx(s); 1394 /* 1395 * Return ENETRESET to inform the driver that the list has changed 1396 * and its reception filter should be adjusted accordingly. 1397 */ 1398 return (ENETRESET); 1399 } 1400 1401 /* 1402 * Delete a multicast address record. 1403 */ 1404 int 1405 ether_delmulti(struct ifreq *ifr, struct ethercom *ec) 1406 { 1407 struct ether_multi *enm; 1408 u_char addrlo[ETHER_ADDR_LEN]; 1409 u_char addrhi[ETHER_ADDR_LEN]; 1410 int s = splnet(), error; 1411 1412 error = ether_multiaddr(&ifr->ifr_addr, addrlo, addrhi); 1413 if (error != 0) { 1414 splx(s); 1415 return (error); 1416 } 1417 1418 /* 1419 * Look ur the address in our list. 1420 */ 1421 ETHER_LOOKUP_MULTI(addrlo, addrhi, ec, enm); 1422 if (enm == NULL) { 1423 splx(s); 1424 return (ENXIO); 1425 } 1426 if (--enm->enm_refcount != 0) { 1427 /* 1428 * Still some claims to this record. 1429 */ 1430 splx(s); 1431 return (0); 1432 } 1433 /* 1434 * No remaining claims to this record; unlink and free it. 1435 */ 1436 LIST_REMOVE(enm, enm_list); 1437 free(enm, M_IFMADDR); 1438 ec->ec_multicnt--; 1439 splx(s); 1440 /* 1441 * Return ENETRESET to inform the driver that the list has changed 1442 * and its reception filter should be adjusted accordingly. 1443 */ 1444 return (ENETRESET); 1445 } 1446 1447 /* 1448 * Common ioctls for Ethernet interfaces. Note, we must be 1449 * called at splnet(). 1450 */ 1451 int 1452 ether_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 1453 { 1454 struct ethercom *ec = (void *) ifp; 1455 struct ifreq *ifr = (struct ifreq *)data; 1456 struct ifaddr *ifa = (struct ifaddr *)data; 1457 int error = 0; 1458 1459 switch (cmd) { 1460 case SIOCSIFADDR: 1461 ifp->if_flags |= IFF_UP; 1462 switch (ifa->ifa_addr->sa_family) { 1463 case AF_LINK: 1464 { 1465 struct sockaddr_dl *sdl = 1466 (struct sockaddr_dl *) ifa->ifa_addr; 1467 1468 if (sdl->sdl_type != IFT_ETHER || 1469 sdl->sdl_alen != ifp->if_addrlen) { 1470 error = EINVAL; 1471 break; 1472 } 1473 1474 memcpy(LLADDR(ifp->if_sadl), LLADDR(sdl), 1475 ifp->if_addrlen); 1476 1477 /* Set new address. */ 1478 error = (*ifp->if_init)(ifp); 1479 break; 1480 } 1481 #ifdef INET 1482 case AF_INET: 1483 if ((ifp->if_flags & IFF_RUNNING) == 0 && 1484 (error = (*ifp->if_init)(ifp)) != 0) 1485 break; 1486 arp_ifinit(ifp, ifa); 1487 break; 1488 #endif /* INET */ 1489 #ifdef NS 1490 case AF_NS: 1491 { 1492 struct ns_addr *ina = &IA_SNS(ifa)->sns_addr; 1493 1494 if (ns_nullhost(*ina)) 1495 ina->x_host = *(union ns_host *) 1496 LLADDR(ifp->if_sadl); 1497 else 1498 memcpy(LLADDR(ifp->if_sadl), 1499 ina->x_host.c_host, ifp->if_addrlen); 1500 /* Set new address. */ 1501 error = (*ifp->if_init)(ifp); 1502 break; 1503 } 1504 #endif /* NS */ 1505 default: 1506 if ((ifp->if_flags & IFF_RUNNING) == 0) 1507 error = (*ifp->if_init)(ifp); 1508 break; 1509 } 1510 break; 1511 1512 case SIOCGIFADDR: 1513 memcpy(((struct sockaddr *)&ifr->ifr_data)->sa_data, 1514 LLADDR(ifp->if_sadl), ETHER_ADDR_LEN); 1515 break; 1516 1517 case SIOCSIFMTU: 1518 { 1519 int maxmtu; 1520 1521 if (ec->ec_capabilities & ETHERCAP_JUMBO_MTU) 1522 maxmtu = ETHERMTU_JUMBO; 1523 else 1524 maxmtu = ETHERMTU; 1525 1526 if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > maxmtu) 1527 error = EINVAL; 1528 else { 1529 ifp->if_mtu = ifr->ifr_mtu; 1530 1531 /* Make sure the device notices the MTU change. */ 1532 if (ifp->if_flags & IFF_UP) 1533 error = (*ifp->if_init)(ifp); 1534 } 1535 break; 1536 } 1537 1538 case SIOCSIFFLAGS: 1539 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) == IFF_RUNNING) { 1540 /* 1541 * If interface is marked down and it is running, 1542 * then stop and disable it. 1543 */ 1544 (*ifp->if_stop)(ifp, 1); 1545 } else if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) == IFF_UP) { 1546 /* 1547 * If interface is marked up and it is stopped, then 1548 * start it. 1549 */ 1550 error = (*ifp->if_init)(ifp); 1551 } else if ((ifp->if_flags & IFF_UP) != 0) { 1552 /* 1553 * Reset the interface to pick up changes in any other 1554 * flags that affect the hardware state. 1555 */ 1556 error = (*ifp->if_init)(ifp); 1557 } 1558 break; 1559 1560 case SIOCADDMULTI: 1561 error = ether_addmulti(ifr, ec); 1562 break; 1563 1564 case SIOCDELMULTI: 1565 error = ether_delmulti(ifr, ec); 1566 break; 1567 1568 default: 1569 error = ENOTTY; 1570 } 1571 1572 return (error); 1573 } 1574