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