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