1 /* $NetBSD: if_ethersubr.c,v 1.56 2000/05/12 16:22:36 thorpej 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. All advertising materials mentioning features or use of this software 45 * must display the following acknowledgement: 46 * This product includes software developed by the University of 47 * California, Berkeley and its contributors. 48 * 4. Neither the name of the University nor the names of its contributors 49 * may be used to endorse or promote products derived from this software 50 * without specific prior written permission. 51 * 52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 62 * SUCH DAMAGE. 63 * 64 * @(#)if_ethersubr.c 8.2 (Berkeley) 4/4/96 65 */ 66 67 #include "opt_inet.h" 68 #include "opt_atalk.h" 69 #include "opt_ccitt.h" 70 #include "opt_llc.h" 71 #include "opt_iso.h" 72 #include "opt_ns.h" 73 #include "opt_gateway.h" 74 75 #include <sys/param.h> 76 #include <sys/systm.h> 77 #include <sys/kernel.h> 78 #include <sys/malloc.h> 79 #include <sys/mbuf.h> 80 #include <sys/protosw.h> 81 #include <sys/socket.h> 82 #include <sys/ioctl.h> 83 #include <sys/errno.h> 84 #include <sys/syslog.h> 85 86 #include <machine/cpu.h> 87 88 #include <net/if.h> 89 #include <net/netisr.h> 90 #include <net/route.h> 91 #include <net/if_llc.h> 92 #include <net/if_dl.h> 93 #include <net/if_types.h> 94 95 #include <net/if_ether.h> 96 97 #include <netinet/in.h> 98 #ifdef INET 99 #include <netinet/in_var.h> 100 #endif 101 #include <netinet/if_inarp.h> 102 103 #ifdef INET6 104 #ifndef INET 105 #include <netinet/in.h> 106 #endif 107 #include <netinet6/in6_var.h> 108 #include <netinet6/nd6.h> 109 #endif 110 111 #ifdef NS 112 #include <netns/ns.h> 113 #include <netns/ns_if.h> 114 #endif 115 116 #ifdef IPX 117 #include <netipx/ipx.h> 118 #include <netipx/ipx_if.h> 119 #endif 120 121 #ifdef ISO 122 #include <netiso/argo_debug.h> 123 #include <netiso/iso.h> 124 #include <netiso/iso_var.h> 125 #include <netiso/iso_snpac.h> 126 #endif 127 128 #ifdef LLC 129 #include <netccitt/dll.h> 130 #include <netccitt/llc_var.h> 131 #endif 132 133 #if defined(LLC) && defined(CCITT) 134 extern struct ifqueue pkintrq; 135 #endif 136 137 #ifdef NETATALK 138 #include <netatalk/at.h> 139 #include <netatalk/at_var.h> 140 #include <netatalk/at_extern.h> 141 142 #define llc_snap_org_code llc_un.type_snap.org_code 143 #define llc_snap_ether_type llc_un.type_snap.ether_type 144 145 extern u_char at_org_code[3]; 146 extern u_char aarp_org_code[3]; 147 #endif /* NETATALK */ 148 149 u_char etherbroadcastaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 150 #define senderr(e) { error = (e); goto bad;} 151 152 #define SIN(x) ((struct sockaddr_in *)x) 153 154 static int ether_output __P((struct ifnet *, struct mbuf *, 155 struct sockaddr *, struct rtentry *)); 156 static void ether_input __P((struct ifnet *, struct mbuf *)); 157 158 /* 159 * Ethernet output routine. 160 * Encapsulate a packet of type family for the local net. 161 * Assumes that ifp is actually pointer to ethercom structure. 162 */ 163 static int 164 ether_output(ifp, m0, dst, rt0) 165 struct ifnet *ifp; 166 struct mbuf *m0; 167 struct sockaddr *dst; 168 struct rtentry *rt0; 169 { 170 u_int16_t etype = 0; 171 int s, error = 0, hdrcmplt = 0; 172 u_char esrc[6], edst[6]; 173 struct mbuf *m = m0; 174 struct rtentry *rt; 175 struct mbuf *mcopy = (struct mbuf *)0; 176 struct ether_header *eh; 177 #ifdef INET 178 struct arphdr *ah; 179 #endif /* INET */ 180 #ifdef NETATALK 181 struct at_ifaddr *aa; 182 #endif /* NETATALK */ 183 184 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) 185 senderr(ENETDOWN); 186 ifp->if_lastchange = time; 187 if ((rt = rt0) != NULL) { 188 if ((rt->rt_flags & RTF_UP) == 0) { 189 if ((rt0 = rt = rtalloc1(dst, 1)) != NULL) { 190 rt->rt_refcnt--; 191 if (rt->rt_ifp != ifp) 192 return (*rt->rt_ifp->if_output) 193 (ifp, m0, dst, rt); 194 } else 195 senderr(EHOSTUNREACH); 196 } 197 if ((rt->rt_flags & RTF_GATEWAY) && dst->sa_family != AF_NS) { 198 if (rt->rt_gwroute == 0) 199 goto lookup; 200 if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) { 201 rtfree(rt); rt = rt0; 202 lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1); 203 if ((rt = rt->rt_gwroute) == 0) 204 senderr(EHOSTUNREACH); 205 /* the "G" test below also prevents rt == rt0 */ 206 if ((rt->rt_flags & RTF_GATEWAY) || 207 (rt->rt_ifp != ifp)) { 208 rt->rt_refcnt--; 209 rt0->rt_gwroute = 0; 210 senderr(EHOSTUNREACH); 211 } 212 } 213 } 214 if (rt->rt_flags & RTF_REJECT) 215 if (rt->rt_rmx.rmx_expire == 0 || 216 time.tv_sec < rt->rt_rmx.rmx_expire) 217 senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH); 218 } 219 switch (dst->sa_family) { 220 221 #ifdef INET 222 case AF_INET: 223 if (m->m_flags & M_BCAST) 224 bcopy((caddr_t)etherbroadcastaddr, (caddr_t)edst, 225 sizeof(edst)); 226 227 else if (m->m_flags & M_MCAST) { 228 ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, 229 (caddr_t)edst) 230 231 } else if (!arpresolve(ifp, rt, m, dst, edst)) 232 return (0); /* if not yet resolved */ 233 /* If broadcasting on a simplex interface, loopback a copy */ 234 if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX)) 235 mcopy = m_copy(m, 0, (int)M_COPYALL); 236 etype = htons(ETHERTYPE_IP); 237 break; 238 239 case AF_ARP: 240 ah = mtod(m, struct arphdr *); 241 if (m->m_flags & M_BCAST) 242 bcopy((caddr_t)etherbroadcastaddr, (caddr_t)edst, 243 sizeof(edst)); 244 else 245 bcopy((caddr_t)ar_tha(ah), 246 (caddr_t)edst, sizeof(edst)); 247 248 ah->ar_hrd = htons(ARPHRD_ETHER); 249 250 switch(ntohs(ah->ar_op)) { 251 case ARPOP_REVREQUEST: 252 case ARPOP_REVREPLY: 253 etype = htons(ETHERTYPE_REVARP); 254 break; 255 256 case ARPOP_REQUEST: 257 case ARPOP_REPLY: 258 default: 259 etype = htons(ETHERTYPE_ARP); 260 } 261 262 break; 263 #endif 264 #ifdef INET6 265 case AF_INET6: 266 #ifdef OLDIP6OUTPUT 267 if (!nd6_resolve(ifp, rt, m, dst, (u_char *)edst)) 268 return(0); /* if not yet resolves */ 269 #else 270 if (!nd6_storelladdr(ifp, rt, m, dst, (u_char *)edst)){ 271 /* this must be impossible, so we bark */ 272 printf("nd6_storelladdr failed\n"); 273 return(0); 274 } 275 #endif /* OLDIP6OUTPUT */ 276 etype = htons(ETHERTYPE_IPV6); 277 break; 278 #endif 279 #ifdef NETATALK 280 case AF_APPLETALK: 281 if (!aarpresolve(ifp, m, (struct sockaddr_at *)dst, edst)) { 282 #ifdef NETATALKDEBUG 283 printf("aarpresolv failed\n"); 284 #endif /* NETATALKDEBUG */ 285 return (0); 286 } 287 /* 288 * ifaddr is the first thing in at_ifaddr 289 */ 290 aa = (struct at_ifaddr *) at_ifawithnet( 291 (struct sockaddr_at *)dst, ifp); 292 if (aa == NULL) 293 goto bad; 294 295 /* 296 * In the phase 2 case, we need to prepend an mbuf for the 297 * llc header. Since we must preserve the value of m, 298 * which is passed to us by value, we m_copy() the first 299 * mbuf, and use it for our llc header. 300 */ 301 if (aa->aa_flags & AFA_PHASE2) { 302 struct llc llc; 303 304 M_PREPEND(m, sizeof(struct llc), M_DONTWAIT); 305 llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP; 306 llc.llc_control = LLC_UI; 307 bcopy(at_org_code, llc.llc_snap_org_code, 308 sizeof(llc.llc_snap_org_code)); 309 llc.llc_snap_ether_type = htons(ETHERTYPE_ATALK); 310 bcopy(&llc, mtod(m, caddr_t), sizeof(struct llc)); 311 } else { 312 etype = htons(ETHERTYPE_ATALK); 313 } 314 break; 315 #endif /* NETATALK */ 316 #ifdef NS 317 case AF_NS: 318 etype = htons(ETHERTYPE_NS); 319 bcopy((caddr_t)&(((struct sockaddr_ns *)dst)->sns_addr.x_host), 320 (caddr_t)edst, sizeof (edst)); 321 if (!bcmp((caddr_t)edst, (caddr_t)&ns_thishost, sizeof(edst))) 322 return (looutput(ifp, m, dst, rt)); 323 /* If broadcasting on a simplex interface, loopback a copy */ 324 if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX)) 325 mcopy = m_copy(m, 0, (int)M_COPYALL); 326 break; 327 #endif 328 #ifdef IPX 329 case AF_IPX: 330 etype = htons(ETHERTYPE_IPX); 331 bcopy((caddr_t)&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host), 332 (caddr_t)edst, sizeof (edst)); 333 /* If broadcasting on a simplex interface, loopback a copy */ 334 if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX)) 335 mcopy = m_copy(m, 0, (int)M_COPYALL); 336 break; 337 #endif 338 #ifdef ISO 339 case AF_ISO: { 340 int snpalen; 341 struct llc *l; 342 struct sockaddr_dl *sdl; 343 344 if (rt && (sdl = (struct sockaddr_dl *)rt->rt_gateway) && 345 sdl->sdl_family == AF_LINK && sdl->sdl_alen > 0) { 346 bcopy(LLADDR(sdl), (caddr_t)edst, sizeof(edst)); 347 } else { 348 error = iso_snparesolve(ifp, (struct sockaddr_iso *)dst, 349 (char *)edst, &snpalen); 350 if (error) 351 goto bad; /* Not Resolved */ 352 } 353 /* If broadcasting on a simplex interface, loopback a copy */ 354 if (*edst & 1) 355 m->m_flags |= (M_BCAST|M_MCAST); 356 if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX) && 357 (mcopy = m_copy(m, 0, (int)M_COPYALL))) { 358 M_PREPEND(mcopy, sizeof (*eh), M_DONTWAIT); 359 if (mcopy) { 360 eh = mtod(mcopy, struct ether_header *); 361 bcopy((caddr_t)edst, 362 (caddr_t)eh->ether_dhost, sizeof (edst)); 363 bcopy(LLADDR(ifp->if_sadl), 364 (caddr_t)eh->ether_shost, sizeof (edst)); 365 } 366 } 367 M_PREPEND(m, 3, M_DONTWAIT); 368 if (m == NULL) 369 return (0); 370 l = mtod(m, struct llc *); 371 l->llc_dsap = l->llc_ssap = LLC_ISO_LSAP; 372 l->llc_control = LLC_UI; 373 #ifdef ARGO_DEBUG 374 if (argo_debug[D_ETHER]) { 375 int i; 376 printf("unoutput: sending pkt to: "); 377 for (i=0; i<6; i++) 378 printf("%x ", edst[i] & 0xff); 379 printf("\n"); 380 } 381 #endif 382 } break; 383 #endif /* ISO */ 384 #ifdef LLC 385 /* case AF_NSAP: */ 386 case AF_CCITT: { 387 struct sockaddr_dl *sdl = 388 (struct sockaddr_dl *) rt -> rt_gateway; 389 390 if (sdl && sdl->sdl_family == AF_LINK 391 && sdl->sdl_alen > 0) { 392 bcopy(LLADDR(sdl), (char *)edst, 393 sizeof(edst)); 394 } else goto bad; /* Not a link interface ? Funny ... */ 395 if ((ifp->if_flags & IFF_SIMPLEX) && (*edst & 1) && 396 (mcopy = m_copy(m, 0, (int)M_COPYALL))) { 397 M_PREPEND(mcopy, sizeof (*eh), M_DONTWAIT); 398 if (mcopy) { 399 eh = mtod(mcopy, struct ether_header *); 400 bcopy((caddr_t)edst, 401 (caddr_t)eh->ether_dhost, sizeof (edst)); 402 bcopy(LLADDR(ifp->if_sadl), 403 (caddr_t)eh->ether_shost, sizeof (edst)); 404 } 405 } 406 #ifdef LLC_DEBUG 407 { 408 int i; 409 struct llc *l = mtod(m, struct llc *); 410 411 printf("ether_output: sending LLC2 pkt to: "); 412 for (i=0; i<6; i++) 413 printf("%x ", edst[i] & 0xff); 414 printf(" len 0x%x dsap 0x%x ssap 0x%x control 0x%x\n", 415 m->m_pkthdr.len, l->llc_dsap & 0xff, l->llc_ssap &0xff, 416 l->llc_control & 0xff); 417 418 } 419 #endif /* LLC_DEBUG */ 420 } break; 421 #endif /* LLC */ 422 423 case pseudo_AF_HDRCMPLT: 424 hdrcmplt = 1; 425 eh = (struct ether_header *)dst->sa_data; 426 bcopy((caddr_t)eh->ether_shost, (caddr_t)esrc, sizeof (esrc)); 427 /* FALLTHROUGH */ 428 429 case AF_UNSPEC: 430 eh = (struct ether_header *)dst->sa_data; 431 bcopy((caddr_t)eh->ether_dhost, (caddr_t)edst, sizeof (edst)); 432 /* AF_UNSPEC doesn't swap the byte order of the ether_type. */ 433 etype = eh->ether_type; 434 break; 435 436 default: 437 printf("%s: can't handle af%d\n", ifp->if_xname, 438 dst->sa_family); 439 senderr(EAFNOSUPPORT); 440 } 441 442 if (mcopy) 443 (void) looutput(ifp, mcopy, dst, rt); 444 445 /* If no ether type is set, this must be a 802.2 formatted packet. 446 */ 447 if (etype == 0) 448 etype = htons(m->m_pkthdr.len); 449 /* 450 * Add local net header. If no space in first mbuf, 451 * allocate another. 452 */ 453 M_PREPEND(m, sizeof (struct ether_header), M_DONTWAIT); 454 if (m == 0) 455 senderr(ENOBUFS); 456 eh = mtod(m, struct ether_header *); 457 bcopy((caddr_t)&etype,(caddr_t)&eh->ether_type, 458 sizeof(eh->ether_type)); 459 bcopy((caddr_t)edst, (caddr_t)eh->ether_dhost, sizeof (edst)); 460 if (hdrcmplt) 461 bcopy((caddr_t)esrc, (caddr_t)eh->ether_shost, 462 sizeof(eh->ether_shost)); 463 else 464 bcopy(LLADDR(ifp->if_sadl), (caddr_t)eh->ether_shost, 465 sizeof(eh->ether_shost)); 466 s = splimp(); 467 /* 468 * Queue message on interface, and start output if interface 469 * not yet active. 470 */ 471 if (IF_QFULL(&ifp->if_snd)) { 472 IF_DROP(&ifp->if_snd); 473 splx(s); 474 senderr(ENOBUFS); 475 } 476 ifp->if_obytes += m->m_pkthdr.len; 477 IF_ENQUEUE(&ifp->if_snd, m); 478 if ((ifp->if_flags & IFF_OACTIVE) == 0) 479 (*ifp->if_start)(ifp); 480 splx(s); 481 if (m->m_flags & M_MCAST) 482 ifp->if_omcasts++; 483 return (error); 484 485 bad: 486 if (m) 487 m_freem(m); 488 return (error); 489 } 490 491 /* 492 * Process a received Ethernet packet; 493 * the packet is in the mbuf chain m with 494 * the ether header. 495 */ 496 static void 497 ether_input(ifp, m) 498 struct ifnet *ifp; 499 struct mbuf *m; 500 { 501 struct ifqueue *inq; 502 u_int16_t etype; 503 int s; 504 struct ether_header *eh; 505 #if defined (ISO) || defined (LLC) || defined(NETATALK) 506 struct llc *l; 507 #endif 508 509 if ((ifp->if_flags & IFF_UP) == 0) { 510 m_freem(m); 511 return; 512 } 513 514 eh = mtod(m, struct ether_header *); 515 516 ifp->if_lastchange = time; 517 ifp->if_ibytes += m->m_pkthdr.len; 518 if (eh->ether_dhost[0] & 1) { 519 if (bcmp((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost, 520 sizeof(etherbroadcastaddr)) == 0) 521 m->m_flags |= M_BCAST; 522 else 523 m->m_flags |= M_MCAST; 524 } 525 if (m->m_flags & (M_BCAST|M_MCAST)) 526 ifp->if_imcasts++; 527 528 etype = ntohs(eh->ether_type); 529 530 /* Strip off the Ethernet header. */ 531 m_adj(m, sizeof(struct ether_header)); 532 533 /* If the CRC is still on the packet, trim it off. */ 534 if (m->m_flags & M_HASFCS) 535 m_adj(m, -ETHER_CRC_LEN); 536 537 switch (etype) { 538 #ifdef INET 539 case ETHERTYPE_IP: 540 #ifdef GATEWAY 541 if (ipflow_fastforward(m)) 542 return; 543 #endif 544 schednetisr(NETISR_IP); 545 inq = &ipintrq; 546 break; 547 548 case ETHERTYPE_ARP: 549 schednetisr(NETISR_ARP); 550 inq = &arpintrq; 551 break; 552 553 case ETHERTYPE_REVARP: 554 revarpinput(m); /* XXX queue? */ 555 return; 556 #endif 557 #ifdef INET6 558 case ETHERTYPE_IPV6: 559 schednetisr(NETISR_IPV6); 560 inq = &ip6intrq; 561 break; 562 #endif 563 #ifdef NS 564 case ETHERTYPE_NS: 565 schednetisr(NETISR_NS); 566 inq = &nsintrq; 567 break; 568 569 #endif 570 #ifdef IPX 571 case ETHERTYPE_IPX: 572 schednetisr(NETISR_IPX); 573 inq = &ipxintrq; 574 break; 575 #endif 576 #ifdef NETATALK 577 case ETHERTYPE_ATALK: 578 schednetisr(NETISR_ATALK); 579 inq = &atintrq1; 580 break; 581 case ETHERTYPE_AARP: 582 /* probably this should be done with a NETISR as well */ 583 aarpinput(ifp, m); /* XXX */ 584 return; 585 #endif /* NETATALK */ 586 default: 587 #if defined (ISO) || defined (LLC) || defined (NETATALK) 588 if (etype > ETHERMTU) 589 goto dropanyway; 590 l = mtod(m, struct llc *); 591 switch (l->llc_dsap) { 592 #ifdef NETATALK 593 case LLC_SNAP_LSAP: 594 switch (l->llc_control) { 595 case LLC_UI: 596 if (l->llc_ssap != LLC_SNAP_LSAP) { 597 goto dropanyway; 598 } 599 600 if (Bcmp(&(l->llc_snap_org_code)[0], 601 at_org_code, sizeof(at_org_code)) == 0 && 602 ntohs(l->llc_snap_ether_type) == 603 ETHERTYPE_ATALK) { 604 inq = &atintrq2; 605 m_adj(m, sizeof(struct llc)); 606 schednetisr(NETISR_ATALK); 607 break; 608 } 609 610 if (Bcmp(&(l->llc_snap_org_code)[0], 611 aarp_org_code, 612 sizeof(aarp_org_code)) == 0 && 613 ntohs(l->llc_snap_ether_type) == 614 ETHERTYPE_AARP) { 615 m_adj( m, sizeof(struct llc)); 616 aarpinput(ifp, m); /* XXX */ 617 return; 618 } 619 620 default: 621 goto dropanyway; 622 } 623 break; 624 #endif /* NETATALK */ 625 #ifdef ISO 626 case LLC_ISO_LSAP: 627 switch (l->llc_control) { 628 case LLC_UI: 629 /* LLC_UI_P forbidden in class 1 service */ 630 if ((l->llc_dsap == LLC_ISO_LSAP) && 631 (l->llc_ssap == LLC_ISO_LSAP)) { 632 /* LSAP for ISO */ 633 if (m->m_pkthdr.len > etype) 634 m_adj(m, etype - m->m_pkthdr.len); 635 m->m_data += 3; /* XXX */ 636 m->m_len -= 3; /* XXX */ 637 m->m_pkthdr.len -= 3; /* XXX */ 638 M_PREPEND(m, sizeof *eh, M_DONTWAIT); 639 if (m == 0) 640 return; 641 *mtod(m, struct ether_header *) = *eh; 642 #ifdef ARGO_DEBUG 643 if (argo_debug[D_ETHER]) 644 printf("clnp packet"); 645 #endif 646 schednetisr(NETISR_ISO); 647 inq = &clnlintrq; 648 break; 649 } 650 goto dropanyway; 651 652 case LLC_XID: 653 case LLC_XID_P: 654 if(m->m_len < 6) 655 goto dropanyway; 656 l->llc_window = 0; 657 l->llc_fid = 9; 658 l->llc_class = 1; 659 l->llc_dsap = l->llc_ssap = 0; 660 /* Fall through to */ 661 case LLC_TEST: 662 case LLC_TEST_P: 663 { 664 struct sockaddr sa; 665 struct ether_header *eh2; 666 int i; 667 u_char c = l->llc_dsap; 668 669 l->llc_dsap = l->llc_ssap; 670 l->llc_ssap = c; 671 if (m->m_flags & (M_BCAST | M_MCAST)) 672 bcopy(LLADDR(ifp->if_sadl), 673 (caddr_t)eh->ether_dhost, 6); 674 sa.sa_family = AF_UNSPEC; 675 sa.sa_len = sizeof(sa); 676 eh2 = (struct ether_header *)sa.sa_data; 677 for (i = 0; i < 6; i++) { 678 eh2->ether_shost[i] = c = 679 eh->ether_dhost[i]; 680 eh2->ether_dhost[i] = 681 eh->ether_dhost[i] = 682 eh->ether_shost[i]; 683 eh->ether_shost[i] = c; 684 } 685 ifp->if_output(ifp, m, &sa, NULL); 686 return; 687 } 688 default: 689 m_freem(m); 690 return; 691 } 692 break; 693 #endif /* ISO */ 694 #ifdef LLC 695 case LLC_X25_LSAP: 696 { 697 if (m->m_pkthdr.len > etype) 698 m_adj(m, etype - m->m_pkthdr.len); 699 M_PREPEND(m, sizeof(struct sdl_hdr) , M_DONTWAIT); 700 if (m == 0) 701 return; 702 if ( !sdl_sethdrif(ifp, eh->ether_shost, LLC_X25_LSAP, 703 eh->ether_dhost, LLC_X25_LSAP, 6, 704 mtod(m, struct sdl_hdr *))) 705 panic("ETHER cons addr failure"); 706 mtod(m, struct sdl_hdr *)->sdlhdr_len = etype; 707 #ifdef LLC_DEBUG 708 printf("llc packet\n"); 709 #endif /* LLC_DEBUG */ 710 schednetisr(NETISR_CCITT); 711 inq = &llcintrq; 712 break; 713 } 714 #endif /* LLC */ 715 dropanyway: 716 default: 717 m_freem(m); 718 return; 719 } 720 #else /* ISO || LLC || NETATALK*/ 721 m_freem(m); 722 return; 723 #endif /* ISO || LLC || NETATALK*/ 724 } 725 726 s = splimp(); 727 if (IF_QFULL(inq)) { 728 IF_DROP(inq); 729 m_freem(m); 730 } else 731 IF_ENQUEUE(inq, m); 732 splx(s); 733 } 734 735 /* 736 * Convert Ethernet address to printable (loggable) representation. 737 */ 738 static char digits[] = "0123456789abcdef"; 739 char * 740 ether_sprintf(ap) 741 const u_char *ap; 742 { 743 static char etherbuf[18]; 744 char *cp = etherbuf; 745 int i; 746 747 for (i = 0; i < 6; i++) { 748 *cp++ = digits[*ap >> 4]; 749 *cp++ = digits[*ap++ & 0xf]; 750 *cp++ = ':'; 751 } 752 *--cp = 0; 753 return (etherbuf); 754 } 755 756 /* 757 * Perform common duties while attaching to interface list 758 */ 759 void 760 ether_ifattach(ifp, lla) 761 struct ifnet *ifp; 762 const u_int8_t *lla; 763 { 764 struct sockaddr_dl *sdl; 765 766 ifp->if_type = IFT_ETHER; 767 ifp->if_addrlen = 6; 768 ifp->if_hdrlen = 14; 769 ifp->if_mtu = ETHERMTU; 770 ifp->if_output = ether_output; 771 ifp->if_input = ether_input; 772 if (ifp->if_baudrate == 0) 773 ifp->if_baudrate = IF_Mbps(10); /* just a default */ 774 if ((sdl = ifp->if_sadl) && 775 sdl->sdl_family == AF_LINK) { 776 sdl->sdl_type = IFT_ETHER; 777 sdl->sdl_alen = ifp->if_addrlen; 778 bcopy(lla, LLADDR(sdl), ifp->if_addrlen); 779 } 780 LIST_INIT(&((struct ethercom *)ifp)->ec_multiaddrs); 781 ifp->if_broadcastaddr = etherbroadcastaddr; 782 } 783 784 void 785 ether_ifdetach(ifp) 786 struct ifnet *ifp; 787 { 788 789 /* Nothing. */ 790 } 791 792 #if 0 793 /* 794 * This is for reference. We have a table-driven version 795 * of the little-endian crc32 generator, which is faster 796 * than the double-loop. 797 */ 798 u_int32_t 799 ether_crc32_le(buf, len) 800 const u_int8_t *buf; 801 size_t len; 802 { 803 u_int32_t c, crc, carry; 804 size_t i, j; 805 806 crc = 0xffffffffU; /* initial value */ 807 808 for (i = 0; i < len; i++) { 809 c = buf[i]; 810 for (j = 0; j < 8; j++) { 811 carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01); 812 crc >>= 1; 813 c >>= 1; 814 if (carry) 815 crc = (crc ^ ETHER_CRC_POLY_LE); 816 } 817 } 818 819 return (crc); 820 } 821 #else 822 u_int32_t 823 ether_crc32_le(buf, len) 824 const u_int8_t *buf; 825 size_t len; 826 { 827 static const u_int32_t crctab[] = { 828 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac, 829 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c, 830 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c, 831 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c 832 }; 833 u_int32_t crc; 834 int i; 835 836 crc = 0xffffffffU; /* initial value */ 837 838 for (i = 0; i < len; i++) { 839 crc ^= buf[i]; 840 crc = (crc >> 4) ^ crctab[crc & 0xf]; 841 crc = (crc >> 4) ^ crctab[crc & 0xf]; 842 } 843 844 return (crc); 845 } 846 #endif 847 848 u_int32_t 849 ether_crc32_be(buf, len) 850 const u_int8_t *buf; 851 size_t len; 852 { 853 u_int32_t c, crc, carry; 854 size_t i, j; 855 856 crc = 0xffffffffU; /* initial value */ 857 858 for (i = 0; i < len; i++) { 859 c = buf[i]; 860 for (j = 0; j < 8; j++) { 861 carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01); 862 crc <<= 1; 863 c >>= 1; 864 if (carry) 865 crc = (crc ^ ETHER_CRC_POLY_BE) | carry; 866 } 867 } 868 869 return (crc); 870 } 871 872 #ifdef INET 873 u_char ether_ipmulticast_min[6] = { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 }; 874 u_char ether_ipmulticast_max[6] = { 0x01, 0x00, 0x5e, 0x7f, 0xff, 0xff }; 875 #endif 876 #ifdef INET6 877 u_char ether_ip6multicast_min[6] = { 0x33, 0x33, 0x00, 0x00, 0x00, 0x00 }; 878 u_char ether_ip6multicast_max[6] = { 0x33, 0x33, 0xff, 0xff, 0xff, 0xff }; 879 #endif 880 /* 881 * Add an Ethernet multicast address or range of addresses to the list for a 882 * given interface. 883 */ 884 int 885 ether_addmulti(ifr, ec) 886 struct ifreq *ifr; 887 struct ethercom *ec; 888 { 889 struct ether_multi *enm; 890 #ifdef INET 891 struct sockaddr_in *sin; 892 #endif /* INET */ 893 #ifdef INET6 894 struct sockaddr_in6 *sin6; 895 #endif /* INET6 */ 896 u_char addrlo[6]; 897 u_char addrhi[6]; 898 int s = splimp(); 899 900 switch (ifr->ifr_addr.sa_family) { 901 902 case AF_UNSPEC: 903 bcopy(ifr->ifr_addr.sa_data, addrlo, 6); 904 bcopy(addrlo, addrhi, 6); 905 break; 906 907 #ifdef INET 908 case AF_INET: 909 sin = (struct sockaddr_in *)&(ifr->ifr_addr); 910 if (sin->sin_addr.s_addr == INADDR_ANY) { 911 /* 912 * An IP address of INADDR_ANY means listen to all 913 * of the Ethernet multicast addresses used for IP. 914 * (This is for the sake of IP multicast routers.) 915 */ 916 bcopy(ether_ipmulticast_min, addrlo, 6); 917 bcopy(ether_ipmulticast_max, addrhi, 6); 918 } 919 else { 920 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo); 921 bcopy(addrlo, addrhi, 6); 922 } 923 break; 924 #endif 925 #ifdef INET6 926 case AF_INET6: 927 sin6 = (struct sockaddr_in6 *) 928 &(((struct in6_ifreq *)ifr)->ifr_addr); 929 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 930 /* 931 * An IP6 address of 0 means listen to all 932 * of the Ethernet multicast address used for IP6. 933 * (This is used for multicast routers.) 934 */ 935 bcopy(ether_ip6multicast_min, addrlo, ETHER_ADDR_LEN); 936 bcopy(ether_ip6multicast_max, addrhi, ETHER_ADDR_LEN); 937 #if 0 938 set_allmulti = 1; 939 #endif 940 } else { 941 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, addrlo); 942 bcopy(addrlo, addrhi, ETHER_ADDR_LEN); 943 } 944 break; 945 #endif 946 947 default: 948 splx(s); 949 return (EAFNOSUPPORT); 950 } 951 952 /* 953 * Verify that we have valid Ethernet multicast addresses. 954 */ 955 if ((addrlo[0] & 0x01) != 1 || (addrhi[0] & 0x01) != 1) { 956 splx(s); 957 return (EINVAL); 958 } 959 /* 960 * See if the address range is already in the list. 961 */ 962 ETHER_LOOKUP_MULTI(addrlo, addrhi, ec, enm); 963 if (enm != NULL) { 964 /* 965 * Found it; just increment the reference count. 966 */ 967 ++enm->enm_refcount; 968 splx(s); 969 return (0); 970 } 971 /* 972 * New address or range; malloc a new multicast record 973 * and link it into the interface's multicast list. 974 */ 975 enm = (struct ether_multi *)malloc(sizeof(*enm), M_IFMADDR, M_NOWAIT); 976 if (enm == NULL) { 977 splx(s); 978 return (ENOBUFS); 979 } 980 bcopy(addrlo, enm->enm_addrlo, 6); 981 bcopy(addrhi, enm->enm_addrhi, 6); 982 enm->enm_ec = ec; 983 enm->enm_refcount = 1; 984 LIST_INSERT_HEAD(&ec->ec_multiaddrs, enm, enm_list); 985 ec->ec_multicnt++; 986 splx(s); 987 /* 988 * Return ENETRESET to inform the driver that the list has changed 989 * and its reception filter should be adjusted accordingly. 990 */ 991 return (ENETRESET); 992 } 993 994 /* 995 * Delete a multicast address record. 996 */ 997 int 998 ether_delmulti(ifr, ec) 999 struct ifreq *ifr; 1000 struct ethercom *ec; 1001 { 1002 struct ether_multi *enm; 1003 #ifdef INET 1004 struct sockaddr_in *sin; 1005 #endif /* INET */ 1006 #ifdef INET6 1007 struct sockaddr_in6 *sin6; 1008 #endif /* INET6 */ 1009 u_char addrlo[6]; 1010 u_char addrhi[6]; 1011 int s = splimp(); 1012 1013 switch (ifr->ifr_addr.sa_family) { 1014 1015 case AF_UNSPEC: 1016 bcopy(ifr->ifr_addr.sa_data, addrlo, 6); 1017 bcopy(addrlo, addrhi, 6); 1018 break; 1019 1020 #ifdef INET 1021 case AF_INET: 1022 sin = (struct sockaddr_in *)&(ifr->ifr_addr); 1023 if (sin->sin_addr.s_addr == INADDR_ANY) { 1024 /* 1025 * An IP address of INADDR_ANY means stop listening 1026 * to the range of Ethernet multicast addresses used 1027 * for IP. 1028 */ 1029 bcopy(ether_ipmulticast_min, addrlo, 6); 1030 bcopy(ether_ipmulticast_max, addrhi, 6); 1031 } 1032 else { 1033 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo); 1034 bcopy(addrlo, addrhi, 6); 1035 } 1036 break; 1037 #endif 1038 #ifdef INET6 1039 case AF_INET6: 1040 sin6 = (struct sockaddr_in6 *)&(ifr->ifr_addr); 1041 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 1042 /* 1043 * An IP6 address of all 0 means stop listening 1044 * to the range of Ethernet multicast addresses used 1045 * for IP6 1046 */ 1047 bcopy(ether_ip6multicast_min, addrlo, ETHER_ADDR_LEN); 1048 bcopy(ether_ip6multicast_max, addrhi, ETHER_ADDR_LEN); 1049 } else { 1050 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, addrlo); 1051 bcopy(addrlo, addrhi, ETHER_ADDR_LEN); 1052 } 1053 break; 1054 #endif 1055 1056 default: 1057 splx(s); 1058 return (EAFNOSUPPORT); 1059 } 1060 1061 /* 1062 * Look up the address in our list. 1063 */ 1064 ETHER_LOOKUP_MULTI(addrlo, addrhi, ec, enm); 1065 if (enm == NULL) { 1066 splx(s); 1067 return (ENXIO); 1068 } 1069 if (--enm->enm_refcount != 0) { 1070 /* 1071 * Still some claims to this record. 1072 */ 1073 splx(s); 1074 return (0); 1075 } 1076 /* 1077 * No remaining claims to this record; unlink and free it. 1078 */ 1079 LIST_REMOVE(enm, enm_list); 1080 free(enm, M_IFMADDR); 1081 ec->ec_multicnt--; 1082 splx(s); 1083 /* 1084 * Return ENETRESET to inform the driver that the list has changed 1085 * and its reception filter should be adjusted accordingly. 1086 */ 1087 return (ENETRESET); 1088 } 1089