1 /* 2 * Copyright (c) 1982, 1989, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * @(#)if_ethersubr.c 8.1 (Berkeley) 6/10/93 34 * $FreeBSD$ 35 */ 36 37 #include "opt_atalk.h" 38 #include "opt_inet.h" 39 #include "opt_inet6.h" 40 #include "opt_ipx.h" 41 #include "opt_bdg.h" 42 #include "opt_netgraph.h" 43 44 #include <sys/param.h> 45 #include <sys/systm.h> 46 #include <sys/kernel.h> 47 #include <sys/malloc.h> 48 #include <sys/mbuf.h> 49 #include <sys/random.h> 50 #include <sys/socket.h> 51 #include <sys/sockio.h> 52 #include <sys/sysctl.h> 53 54 #include <net/if.h> 55 #include <net/netisr.h> 56 #include <net/route.h> 57 #include <net/if_llc.h> 58 #include <net/if_dl.h> 59 #include <net/if_types.h> 60 #include <net/bpf.h> 61 #include <net/ethernet.h> 62 #include <net/bridge.h> 63 64 #if defined(INET) || defined(INET6) 65 #include <netinet/in.h> 66 #include <netinet/in_var.h> 67 #include <netinet/if_ether.h> 68 #include <netinet/ip_fw.h> 69 #include <netinet/ip_dummynet.h> 70 #endif 71 #ifdef INET6 72 #include <netinet6/nd6.h> 73 #endif 74 75 #ifdef IPX 76 #include <netipx/ipx.h> 77 #include <netipx/ipx_if.h> 78 int (*ef_inputp)(struct ifnet*, struct ether_header *eh, struct mbuf *m); 79 int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp, 80 struct sockaddr *dst, short *tp, int *hlen); 81 #endif 82 83 #ifdef NS 84 #include <netns/ns.h> 85 #include <netns/ns_if.h> 86 ushort ns_nettype; 87 int ether_outputdebug = 0; 88 int ether_inputdebug = 0; 89 #endif 90 91 #ifdef NETATALK 92 #include <netatalk/at.h> 93 #include <netatalk/at_var.h> 94 #include <netatalk/at_extern.h> 95 96 #define llc_snap_org_code llc_un.type_snap.org_code 97 #define llc_snap_ether_type llc_un.type_snap.ether_type 98 99 extern u_char at_org_code[3]; 100 extern u_char aarp_org_code[3]; 101 #endif /* NETATALK */ 102 103 /* netgraph node hooks for ng_ether(4) */ 104 void (*ng_ether_input_p)(struct ifnet *ifp, 105 struct mbuf **mp, struct ether_header *eh); 106 void (*ng_ether_input_orphan_p)(struct ifnet *ifp, 107 struct mbuf *m, struct ether_header *eh); 108 int (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp); 109 void (*ng_ether_attach_p)(struct ifnet *ifp); 110 void (*ng_ether_detach_p)(struct ifnet *ifp); 111 112 int (*vlan_input_p)(struct ether_header *eh, struct mbuf *m); 113 int (*vlan_input_tag_p)(struct ether_header *eh, struct mbuf *m, 114 u_int16_t t); 115 116 /* bridge support */ 117 int do_bridge; 118 bridge_in_t *bridge_in_ptr; 119 bdg_forward_t *bdg_forward_ptr; 120 bdgtakeifaces_t *bdgtakeifaces_ptr; 121 struct bdg_softc *ifp2sc; 122 123 static int ether_resolvemulti(struct ifnet *, struct sockaddr **, 124 struct sockaddr *); 125 u_char etherbroadcastaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 126 #define senderr(e) do { error = (e); goto bad;} while (0) 127 #define IFP2AC(IFP) ((struct arpcom *)IFP) 128 129 int 130 ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst, 131 struct ip_fw **rule, struct ether_header *eh, int shared); 132 static int ether_ipfw; 133 134 /* 135 * Ethernet output routine. 136 * Encapsulate a packet of type family for the local net. 137 * Use trailer local net encapsulation if enough data in first 138 * packet leaves a multiple of 512 bytes of data in remainder. 139 * Assumes that ifp is actually pointer to arpcom structure. 140 */ 141 int 142 ether_output(ifp, m, dst, rt0) 143 register struct ifnet *ifp; 144 struct mbuf *m; 145 struct sockaddr *dst; 146 struct rtentry *rt0; 147 { 148 short type; 149 int error = 0, hdrcmplt = 0; 150 u_char esrc[6], edst[6]; 151 register struct rtentry *rt; 152 register struct ether_header *eh; 153 int off, loop_copy = 0; 154 int hlen; /* link layer header lenght */ 155 struct arpcom *ac = IFP2AC(ifp); 156 157 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) 158 senderr(ENETDOWN); 159 rt = rt0; 160 if (rt) { 161 if ((rt->rt_flags & RTF_UP) == 0) { 162 rt0 = rt = rtalloc1(dst, 1, 0UL); 163 if (rt0) 164 rt->rt_refcnt--; 165 else 166 senderr(EHOSTUNREACH); 167 } 168 if (rt->rt_flags & RTF_GATEWAY) { 169 if (rt->rt_gwroute == 0) 170 goto lookup; 171 if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) { 172 rtfree(rt); rt = rt0; 173 lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1, 174 0UL); 175 if ((rt = rt->rt_gwroute) == 0) 176 senderr(EHOSTUNREACH); 177 } 178 } 179 if (rt->rt_flags & RTF_REJECT) 180 if (rt->rt_rmx.rmx_expire == 0 || 181 time_second < rt->rt_rmx.rmx_expire) 182 senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH); 183 } 184 hlen = ETHER_HDR_LEN; 185 switch (dst->sa_family) { 186 #ifdef INET 187 case AF_INET: 188 if (!arpresolve(ifp, rt, m, dst, edst, rt0)) 189 return (0); /* if not yet resolved */ 190 off = m->m_pkthdr.len - m->m_len; 191 type = htons(ETHERTYPE_IP); 192 break; 193 #endif 194 #ifdef INET6 195 case AF_INET6: 196 if (!nd6_storelladdr(&ac->ac_if, rt, m, dst, (u_char *)edst)) { 197 /* Something bad happened */ 198 return(0); 199 } 200 off = m->m_pkthdr.len - m->m_len; 201 type = htons(ETHERTYPE_IPV6); 202 break; 203 #endif 204 #ifdef IPX 205 case AF_IPX: 206 if (ef_outputp) { 207 error = ef_outputp(ifp, &m, dst, &type, &hlen); 208 if (error) 209 goto bad; 210 } else 211 type = htons(ETHERTYPE_IPX); 212 bcopy((caddr_t)&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host), 213 (caddr_t)edst, sizeof (edst)); 214 break; 215 #endif 216 #ifdef NETATALK 217 case AF_APPLETALK: 218 { 219 struct at_ifaddr *aa; 220 221 if ((aa = at_ifawithnet((struct sockaddr_at *)dst)) == NULL) { 222 goto bad; 223 } 224 if (!aarpresolve(ac, m, (struct sockaddr_at *)dst, edst)) 225 return (0); 226 /* 227 * In the phase 2 case, need to prepend an mbuf for the llc header. 228 * Since we must preserve the value of m, which is passed to us by 229 * value, we m_copy() the first mbuf, and use it for our llc header. 230 */ 231 if ( aa->aa_flags & AFA_PHASE2 ) { 232 struct llc llc; 233 234 M_PREPEND(m, sizeof(struct llc), M_TRYWAIT); 235 llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP; 236 llc.llc_control = LLC_UI; 237 bcopy(at_org_code, llc.llc_snap_org_code, sizeof(at_org_code)); 238 llc.llc_snap_ether_type = htons( ETHERTYPE_AT ); 239 bcopy(&llc, mtod(m, caddr_t), sizeof(struct llc)); 240 type = htons(m->m_pkthdr.len); 241 hlen = sizeof(struct llc) + ETHER_HDR_LEN; 242 } else { 243 type = htons(ETHERTYPE_AT); 244 } 245 break; 246 } 247 #endif /* NETATALK */ 248 #ifdef NS 249 case AF_NS: 250 switch(ns_nettype){ 251 default: 252 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */ 253 type = 0x8137; 254 break; 255 case 0x0: /* Novell 802.3 */ 256 type = htons( m->m_pkthdr.len); 257 break; 258 case 0xe0e0: /* Novell 802.2 and Token-Ring */ 259 M_PREPEND(m, 3, M_TRYWAIT); 260 type = htons( m->m_pkthdr.len); 261 cp = mtod(m, u_char *); 262 *cp++ = 0xE0; 263 *cp++ = 0xE0; 264 *cp++ = 0x03; 265 break; 266 } 267 bcopy((caddr_t)&(((struct sockaddr_ns *)dst)->sns_addr.x_host), 268 (caddr_t)edst, sizeof (edst)); 269 /* 270 * XXX if ns_thishost is the same as the node's ethernet 271 * address then just the default code will catch this anyhow. 272 * So I'm not sure if this next clause should be here at all? 273 * [JRE] 274 */ 275 if (!bcmp((caddr_t)edst, (caddr_t)&ns_thishost, sizeof(edst))){ 276 m->m_pkthdr.rcvif = ifp; 277 inq = &nsintrq; 278 if (IF_HANDOFF(inq, m, NULL)) 279 schednetisr(NETISR_NS); 280 return (error); 281 } 282 if (!bcmp((caddr_t)edst, (caddr_t)&ns_broadhost, sizeof(edst))){ 283 m->m_flags |= M_BCAST; 284 } 285 break; 286 #endif /* NS */ 287 288 case pseudo_AF_HDRCMPLT: 289 hdrcmplt = 1; 290 eh = (struct ether_header *)dst->sa_data; 291 (void)memcpy(esrc, eh->ether_shost, sizeof (esrc)); 292 /* FALLTHROUGH */ 293 294 case AF_UNSPEC: 295 loop_copy = -1; /* if this is for us, don't do it */ 296 eh = (struct ether_header *)dst->sa_data; 297 (void)memcpy(edst, eh->ether_dhost, sizeof (edst)); 298 type = eh->ether_type; 299 break; 300 301 default: 302 printf("%s%d: can't handle af%d\n", ifp->if_name, ifp->if_unit, 303 dst->sa_family); 304 senderr(EAFNOSUPPORT); 305 } 306 307 /* 308 * Add local net header. If no space in first mbuf, 309 * allocate another. 310 */ 311 M_PREPEND(m, sizeof (struct ether_header), M_DONTWAIT); 312 if (m == 0) 313 senderr(ENOBUFS); 314 eh = mtod(m, struct ether_header *); 315 (void)memcpy(&eh->ether_type, &type, 316 sizeof(eh->ether_type)); 317 (void)memcpy(eh->ether_dhost, edst, sizeof (edst)); 318 if (hdrcmplt) 319 (void)memcpy(eh->ether_shost, esrc, 320 sizeof(eh->ether_shost)); 321 else 322 (void)memcpy(eh->ether_shost, ac->ac_enaddr, 323 sizeof(eh->ether_shost)); 324 325 /* 326 * If a simplex interface, and the packet is being sent to our 327 * Ethernet address or a broadcast address, loopback a copy. 328 * XXX To make a simplex device behave exactly like a duplex 329 * device, we should copy in the case of sending to our own 330 * ethernet address (thus letting the original actually appear 331 * on the wire). However, we don't do that here for security 332 * reasons and compatibility with the original behavior. 333 */ 334 if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) { 335 int csum_flags = 0; 336 337 if (m->m_pkthdr.csum_flags & CSUM_IP) 338 csum_flags |= (CSUM_IP_CHECKED|CSUM_IP_VALID); 339 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) 340 csum_flags |= (CSUM_DATA_VALID|CSUM_PSEUDO_HDR); 341 if ((m->m_flags & M_BCAST) || (loop_copy > 0)) { 342 struct mbuf *n = m_copy(m, 0, (int)M_COPYALL); 343 344 n->m_pkthdr.csum_flags |= csum_flags; 345 if (csum_flags & CSUM_DATA_VALID) 346 n->m_pkthdr.csum_data = 0xffff; 347 348 (void) if_simloop(ifp, n, dst->sa_family, hlen); 349 } else if (bcmp(eh->ether_dhost, 350 eh->ether_shost, ETHER_ADDR_LEN) == 0) { 351 m->m_pkthdr.csum_flags |= csum_flags; 352 if (csum_flags & CSUM_DATA_VALID) 353 m->m_pkthdr.csum_data = 0xffff; 354 (void) if_simloop(ifp, m, dst->sa_family, hlen); 355 return (0); /* XXX */ 356 } 357 } 358 359 /* Handle ng_ether(4) processing, if any */ 360 if (ng_ether_output_p != NULL) { 361 if ((error = (*ng_ether_output_p)(ifp, &m)) != 0) { 362 bad: if (m != NULL) 363 m_freem(m); 364 return (error); 365 } 366 if (m == NULL) 367 return (0); 368 } 369 370 /* Continue with link-layer output */ 371 return ether_output_frame(ifp, m); 372 } 373 374 /* 375 * Ethernet link layer output routine to send a raw frame to the device. 376 * 377 * This assumes that the 14 byte Ethernet header is present and contiguous 378 * in the first mbuf (if BRIDGE'ing). 379 */ 380 int 381 ether_output_frame(ifp, m) 382 struct ifnet *ifp; 383 struct mbuf *m; 384 { 385 int error = 0; 386 387 #if 1 /* XXX ipfw */ 388 struct ip_fw *rule = NULL; 389 if (m->m_type == MT_DUMMYNET) { /* extract info from dummynet header */ 390 rule = (struct ip_fw *)(m->m_data) ; 391 m = m->m_next ; 392 goto no_bridge; 393 } 394 #endif 395 if (BDG_ACTIVE(ifp) ) { 396 struct ether_header *eh; /* a ptr suffices */ 397 398 m->m_pkthdr.rcvif = NULL; 399 eh = mtod(m, struct ether_header *); 400 m_adj(m, ETHER_HDR_LEN); 401 m = bdg_forward_ptr(m, eh, ifp); 402 if (m != NULL) 403 m_freem(m); 404 return (0); 405 } 406 407 #if 1 /* XXX ipfw */ 408 no_bridge: 409 if (IPFW_LOADED && ether_ipfw != 0) { 410 struct ether_header save_eh, *eh; 411 412 eh = mtod(m, struct ether_header *); 413 save_eh = *eh; 414 m_adj(m, ETHER_HDR_LEN); 415 if (ether_ipfw_chk(&m, ifp, &rule, eh, 0) == 0) { 416 if (m) { 417 m_freem(m); 418 return ENOBUFS; /* pkt dropped */ 419 } else 420 return 0; /* consumed e.g. in a pipe */ 421 } 422 /* packet was ok, restore the ethernet header */ 423 if ( (void *)(eh + 1) == (void *)m->m_data) { 424 m->m_data -= ETHER_HDR_LEN ; 425 m->m_len += ETHER_HDR_LEN ; 426 m->m_pkthdr.len += ETHER_HDR_LEN ; 427 } else { 428 M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT); 429 if (m == NULL) /* nope... */ 430 return ENOBUFS; 431 bcopy(&save_eh, mtod(m, struct ether_header *), 432 ETHER_HDR_LEN); 433 } 434 } 435 #endif /* XXX ipfw */ 436 /* 437 * Queue message on interface, update output statistics if 438 * successful, and start output if interface not yet active. 439 */ 440 if (! IF_HANDOFF(&ifp->if_snd, m, ifp)) 441 return (ENOBUFS); 442 return (error); 443 } 444 445 /* 446 * ipfw processing for ethernet packets (in and out). 447 * The second parameter is NULL from ether_demux, and ifp from 448 * ether_output_frame. This section of code could be used from 449 * bridge.c as well as long as we put some extra field (e.g. shared) 450 * to distinguish that case from ether_output_frame(); 451 */ 452 int 453 ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst, 454 struct ip_fw **rule, struct ether_header *eh, int shared) 455 { 456 struct ether_header save_eh = *eh; /* could be a ptr in m */ 457 int i; 458 459 if (*rule != NULL) /* dummynet packet, already partially processed */ 460 return 1; /* HACK! I should obey the fw_one_pass */ 461 /* 462 * i need some amt of data to be contiguous, and in case others need 463 * the packet (shared==1) also better be in the first mbuf. 464 */ 465 i = min( (*m0)->m_pkthdr.len, max_protohdr) ; 466 if ( shared || (*m0)->m_len < i) { 467 *m0 = m_pullup(*m0, i); 468 if (*m0 == NULL) { 469 printf("-- bdg: pullup failed.\n") ; 470 return 0; 471 } 472 } 473 474 i = ip_fw_chk_ptr(m0, dst, NULL /* cookie */, rule, 475 (struct sockaddr_in **)&save_eh); 476 if ( (i & IP_FW_PORT_DENY_FLAG) || *m0 == NULL) /* drop */ 477 return 0; 478 479 if (i == 0) /* a PASS rule. */ 480 return 1; 481 482 if (DUMMYNET_LOADED && (i & IP_FW_PORT_DYNT_FLAG)) { 483 /* 484 * Pass the pkt to dummynet, which consumes it. 485 * If shared, make a copy and keep the original. 486 * Need to prepend the ethernet header, optimize the common 487 * case of eh pointing already into the original mbuf. 488 */ 489 struct mbuf *m ; 490 491 if (shared) { 492 m = m_copypacket(*m0, M_DONTWAIT); 493 if (m == NULL) { 494 printf("bdg_fwd: copy(1) failed\n"); 495 return 0; 496 } 497 } else { 498 m = *m0 ; /* pass the original to dummynet */ 499 *m0 = NULL ; /* and nothing back to the caller */ 500 } 501 if ( (void *)(eh + 1) == (void *)m->m_data) { 502 m->m_data -= ETHER_HDR_LEN ; 503 m->m_len += ETHER_HDR_LEN ; 504 m->m_pkthdr.len += ETHER_HDR_LEN ; 505 } else { 506 M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT); 507 if (m == NULL) /* nope... */ 508 return 0; 509 bcopy(&save_eh, mtod(m, struct ether_header *), 510 ETHER_HDR_LEN); 511 } 512 ip_dn_io_ptr((i & 0xffff), dst ? DN_TO_ETH_OUT: DN_TO_ETH_DEMUX, 513 m, dst, NULL /*route*/, 0 /*dst*/, *rule, 0 /*flags*/); 514 return 0; 515 } 516 /* 517 * XXX add divert/forward actions... 518 */ 519 /* if none of the above matches, we have to drop the pkt */ 520 printf("ether_ipfw: No rules match, so dropping packet!\n"); 521 return 0; 522 } 523 524 /* 525 * Process a received Ethernet packet; 526 * the packet is in the mbuf chain m without 527 * the ether header, which is provided separately. 528 * 529 * NOTA BENE: for many drivers "eh" is a pointer into the first mbuf or 530 * cluster, right before m_data. So be very careful when working on m, 531 * as you could destroy *eh !! 532 * A (probably) more convenient and efficient interface to ether_input 533 * is to have the whole packet (with the ethernet header) into the mbuf: 534 * modules which do not need the ethernet header can easily drop it, while 535 * others (most noticeably bridge and ng_ether) do not need to do additional 536 * work to put the ethernet header back into the mbuf. 537 * 538 * First we perform any link layer operations, then continue 539 * to the upper layers with ether_demux(). 540 */ 541 void 542 ether_input(ifp, eh, m) 543 struct ifnet *ifp; 544 struct ether_header *eh; 545 struct mbuf *m; 546 { 547 struct ether_header save_eh; 548 549 /* Check for a BPF tap */ 550 if (ifp->if_bpf != NULL) { 551 struct m_hdr mh; 552 553 /* This kludge is OK; BPF treats the "mbuf" as read-only */ 554 mh.mh_next = m; 555 mh.mh_data = (char *)eh; 556 mh.mh_len = ETHER_HDR_LEN; 557 bpf_mtap(ifp, (struct mbuf *)&mh); 558 } 559 560 ifp->if_ibytes += m->m_pkthdr.len + sizeof (*eh); 561 562 /* Handle ng_ether(4) processing, if any */ 563 if (ng_ether_input_p != NULL) { 564 (*ng_ether_input_p)(ifp, &m, eh); 565 if (m == NULL) 566 return; 567 } 568 569 /* Check for bridging mode */ 570 if (BDG_ACTIVE(ifp) ) { 571 struct ifnet *bif; 572 573 /* Check with bridging code */ 574 if ((bif = bridge_in_ptr(ifp, eh)) == BDG_DROP) { 575 m_freem(m); 576 return; 577 } 578 if (bif != BDG_LOCAL) { 579 struct mbuf *oldm = m ; 580 581 save_eh = *eh ; /* because it might change */ 582 m = bdg_forward_ptr(m, eh, bif); /* needs forwarding */ 583 /* 584 * Do not continue if bdg_forward_ptr() processed our 585 * packet (and cleared the mbuf pointer m) or if 586 * it dropped (m_free'd) the packet itself. 587 */ 588 if (m == NULL) { 589 if (bif == BDG_BCAST || bif == BDG_MCAST) 590 printf("bdg_forward drop MULTICAST PKT\n"); 591 return; 592 } 593 if (m != oldm) /* m changed! */ 594 eh = &save_eh ; 595 } 596 if (bif == BDG_LOCAL 597 || bif == BDG_BCAST 598 || bif == BDG_MCAST) 599 goto recvLocal; /* receive locally */ 600 601 /* If not local and not multicast, just drop it */ 602 if (m != NULL) 603 m_freem(m); 604 return; 605 } 606 607 recvLocal: 608 /* Continue with upper layer processing */ 609 ether_demux(ifp, eh, m); 610 /* First chunk of an mbuf contains good junk */ 611 if (harvest.ethernet) 612 random_harvest(m, 16, 3, 0, RANDOM_NET); 613 } 614 615 /* 616 * Upper layer processing for a received Ethernet packet. 617 */ 618 void 619 ether_demux(ifp, eh, m) 620 struct ifnet *ifp; 621 struct ether_header *eh; 622 struct mbuf *m; 623 { 624 struct ifqueue *inq; 625 u_short ether_type; 626 #if defined(NETATALK) 627 register struct llc *l; 628 #endif 629 630 #if 1 /* XXX ipfw */ 631 struct ip_fw *rule = NULL; 632 if (m->m_type == MT_DUMMYNET) { /* extract info from dummynet header */ 633 rule = (struct ip_fw *)(m->m_data) ; 634 m = m->m_next ; 635 ifp = m->m_pkthdr.rcvif; 636 goto post_stats; 637 } 638 #endif 639 640 if (! (BDG_ACTIVE(ifp) ) ) 641 /* Discard packet if upper layers shouldn't see it because it was 642 unicast to a different Ethernet address. If the driver is working 643 properly, then this situation can only happen when the interface 644 is in promiscuous mode. */ 645 if ((ifp->if_flags & IFF_PROMISC) != 0 646 && (eh->ether_dhost[0] & 1) == 0 647 && bcmp(eh->ether_dhost, 648 IFP2AC(ifp)->ac_enaddr, ETHER_ADDR_LEN) != 0) { 649 m_freem(m); 650 return; 651 } 652 653 /* Discard packet if interface is not up */ 654 if ((ifp->if_flags & IFF_UP) == 0) { 655 m_freem(m); 656 return; 657 } 658 if (eh->ether_dhost[0] & 1) { 659 if (bcmp((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost, 660 sizeof(etherbroadcastaddr)) == 0) 661 m->m_flags |= M_BCAST; 662 else 663 m->m_flags |= M_MCAST; 664 } 665 if (m->m_flags & (M_BCAST|M_MCAST)) 666 ifp->if_imcasts++; 667 668 #if 1 /* XXX ipfw */ 669 post_stats: 670 if ( IPFW_LOADED && ether_ipfw != 0) { 671 if (ether_ipfw_chk(&m, NULL, &rule, eh, 0 ) == 0) { 672 if (m) 673 m_freem(m); 674 return; 675 } 676 } 677 #endif /* XXX ipfw */ 678 679 ether_type = ntohs(eh->ether_type); 680 681 switch (ether_type) { 682 #ifdef INET 683 case ETHERTYPE_IP: 684 if (ipflow_fastforward(m)) 685 return; 686 schednetisr(NETISR_IP); 687 inq = &ipintrq; 688 break; 689 690 case ETHERTYPE_ARP: 691 if (ifp->if_flags & IFF_NOARP) { 692 /* Discard packet if ARP is disabled on interface */ 693 m_freem(m); 694 return; 695 } 696 schednetisr(NETISR_ARP); 697 inq = &arpintrq; 698 break; 699 #endif 700 #ifdef IPX 701 case ETHERTYPE_IPX: 702 if (ef_inputp && ef_inputp(ifp, eh, m) == 0) 703 return; 704 schednetisr(NETISR_IPX); 705 inq = &ipxintrq; 706 break; 707 #endif 708 #ifdef INET6 709 case ETHERTYPE_IPV6: 710 schednetisr(NETISR_IPV6); 711 inq = &ip6intrq; 712 break; 713 #endif 714 #ifdef NS 715 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */ 716 schednetisr(NETISR_NS); 717 inq = &nsintrq; 718 break; 719 720 #endif /* NS */ 721 #ifdef NETATALK 722 case ETHERTYPE_AT: 723 schednetisr(NETISR_ATALK); 724 inq = &atintrq1; 725 break; 726 case ETHERTYPE_AARP: 727 /* probably this should be done with a NETISR as well */ 728 aarpinput(IFP2AC(ifp), m); /* XXX */ 729 return; 730 #endif /* NETATALK */ 731 case ETHERTYPE_VLAN: 732 /* XXX lock ? */ 733 if (vlan_input_p != NULL) 734 (*vlan_input_p)(eh, m); 735 else { 736 m->m_pkthdr.rcvif->if_noproto++; 737 m_freem(m); 738 } 739 /* XXX unlock ? */ 740 return; 741 default: 742 #ifdef IPX 743 if (ef_inputp && ef_inputp(ifp, eh, m) == 0) 744 return; 745 #endif /* IPX */ 746 #ifdef NS 747 checksum = mtod(m, ushort *); 748 /* Novell 802.3 */ 749 if ((ether_type <= ETHERMTU) && 750 ((*checksum == 0xffff) || (*checksum == 0xE0E0))){ 751 if(*checksum == 0xE0E0) { 752 m->m_pkthdr.len -= 3; 753 m->m_len -= 3; 754 m->m_data += 3; 755 } 756 schednetisr(NETISR_NS); 757 inq = &nsintrq; 758 break; 759 } 760 #endif /* NS */ 761 #if defined(NETATALK) 762 if (ether_type > ETHERMTU) 763 goto dropanyway; 764 l = mtod(m, struct llc *); 765 switch (l->llc_dsap) { 766 case LLC_SNAP_LSAP: 767 switch (l->llc_control) { 768 case LLC_UI: 769 if (l->llc_ssap != LLC_SNAP_LSAP) 770 goto dropanyway; 771 772 if (Bcmp(&(l->llc_snap_org_code)[0], at_org_code, 773 sizeof(at_org_code)) == 0 && 774 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) { 775 inq = &atintrq2; 776 m_adj( m, sizeof( struct llc )); 777 schednetisr(NETISR_ATALK); 778 break; 779 } 780 781 if (Bcmp(&(l->llc_snap_org_code)[0], aarp_org_code, 782 sizeof(aarp_org_code)) == 0 && 783 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) { 784 m_adj( m, sizeof( struct llc )); 785 aarpinput(IFP2AC(ifp), m); /* XXX */ 786 return; 787 } 788 789 default: 790 goto dropanyway; 791 } 792 break; 793 dropanyway: 794 default: 795 if (ng_ether_input_orphan_p != NULL) 796 (*ng_ether_input_orphan_p)(ifp, m, eh); 797 else 798 m_freem(m); 799 return; 800 } 801 #else /* NETATALK */ 802 if (ng_ether_input_orphan_p != NULL) 803 (*ng_ether_input_orphan_p)(ifp, m, eh); 804 else 805 m_freem(m); 806 return; 807 #endif /* NETATALK */ 808 } 809 810 (void) IF_HANDOFF(inq, m, NULL); 811 } 812 813 /* 814 * Perform common duties while attaching to interface list 815 */ 816 void 817 ether_ifattach(ifp, bpf) 818 register struct ifnet *ifp; 819 int bpf; 820 { 821 register struct ifaddr *ifa; 822 register struct sockaddr_dl *sdl; 823 824 ifp->if_type = IFT_ETHER; 825 ifp->if_addrlen = 6; 826 ifp->if_hdrlen = 14; 827 if_attach(ifp); 828 ifp->if_mtu = ETHERMTU; 829 ifp->if_resolvemulti = ether_resolvemulti; 830 if (ifp->if_baudrate == 0) 831 ifp->if_baudrate = 10000000; 832 ifp->if_broadcastaddr = etherbroadcastaddr; 833 ifa = ifaddr_byindex(ifp->if_index); 834 KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__)); 835 sdl = (struct sockaddr_dl *)ifa->ifa_addr; 836 sdl->sdl_type = IFT_ETHER; 837 sdl->sdl_alen = ifp->if_addrlen; 838 bcopy((IFP2AC(ifp))->ac_enaddr, LLADDR(sdl), ifp->if_addrlen); 839 if (bpf) 840 bpfattach(ifp, DLT_EN10MB, sizeof(struct ether_header)); 841 if (ng_ether_attach_p != NULL) 842 (*ng_ether_attach_p)(ifp); 843 if (BDG_LOADED) 844 bdgtakeifaces_ptr(); 845 } 846 847 /* 848 * Perform common duties while detaching an Ethernet interface 849 */ 850 void 851 ether_ifdetach(ifp, bpf) 852 struct ifnet *ifp; 853 int bpf; 854 { 855 if (ng_ether_detach_p != NULL) 856 (*ng_ether_detach_p)(ifp); 857 if (bpf) 858 bpfdetach(ifp); 859 if_detach(ifp); 860 if (BDG_LOADED) 861 bdgtakeifaces_ptr(); 862 } 863 864 SYSCTL_DECL(_net_link); 865 SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet"); 866 SYSCTL_INT(_net_link_ether, OID_AUTO, ipfw, CTLFLAG_RW, 867 ðer_ipfw,0,"Pass ether pkts through firewall"); 868 869 int 870 ether_ioctl(ifp, command, data) 871 struct ifnet *ifp; 872 int command; 873 caddr_t data; 874 { 875 struct ifaddr *ifa = (struct ifaddr *) data; 876 struct ifreq *ifr = (struct ifreq *) data; 877 int error = 0; 878 879 switch (command) { 880 case SIOCSIFADDR: 881 ifp->if_flags |= IFF_UP; 882 883 switch (ifa->ifa_addr->sa_family) { 884 #ifdef INET 885 case AF_INET: 886 ifp->if_init(ifp->if_softc); /* before arpwhohas */ 887 arp_ifinit(ifp, ifa); 888 break; 889 #endif 890 #ifdef IPX 891 /* 892 * XXX - This code is probably wrong 893 */ 894 case AF_IPX: 895 { 896 register struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr); 897 struct arpcom *ac = IFP2AC(ifp); 898 899 if (ipx_nullhost(*ina)) 900 ina->x_host = 901 *(union ipx_host *) 902 ac->ac_enaddr; 903 else { 904 bcopy((caddr_t) ina->x_host.c_host, 905 (caddr_t) ac->ac_enaddr, 906 sizeof(ac->ac_enaddr)); 907 } 908 909 /* 910 * Set new address 911 */ 912 ifp->if_init(ifp->if_softc); 913 break; 914 } 915 #endif 916 #ifdef NS 917 /* 918 * XXX - This code is probably wrong 919 */ 920 case AF_NS: 921 { 922 register struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr); 923 struct arpcom *ac = IFP2AC(ifp); 924 925 if (ns_nullhost(*ina)) 926 ina->x_host = 927 *(union ns_host *) (ac->ac_enaddr); 928 else { 929 bcopy((caddr_t) ina->x_host.c_host, 930 (caddr_t) ac->ac_enaddr, 931 sizeof(ac->ac_enaddr)); 932 } 933 934 /* 935 * Set new address 936 */ 937 ifp->if_init(ifp->if_softc); 938 break; 939 } 940 #endif 941 default: 942 ifp->if_init(ifp->if_softc); 943 break; 944 } 945 break; 946 947 case SIOCGIFADDR: 948 { 949 struct sockaddr *sa; 950 951 sa = (struct sockaddr *) & ifr->ifr_data; 952 bcopy(IFP2AC(ifp)->ac_enaddr, 953 (caddr_t) sa->sa_data, ETHER_ADDR_LEN); 954 } 955 break; 956 957 case SIOCSIFMTU: 958 /* 959 * Set the interface MTU. 960 */ 961 if (ifr->ifr_mtu > ETHERMTU) { 962 error = EINVAL; 963 } else { 964 ifp->if_mtu = ifr->ifr_mtu; 965 } 966 break; 967 } 968 return (error); 969 } 970 971 int 972 ether_resolvemulti(ifp, llsa, sa) 973 struct ifnet *ifp; 974 struct sockaddr **llsa; 975 struct sockaddr *sa; 976 { 977 struct sockaddr_dl *sdl; 978 struct sockaddr_in *sin; 979 #ifdef INET6 980 struct sockaddr_in6 *sin6; 981 #endif 982 u_char *e_addr; 983 984 switch(sa->sa_family) { 985 case AF_LINK: 986 /* 987 * No mapping needed. Just check that it's a valid MC address. 988 */ 989 sdl = (struct sockaddr_dl *)sa; 990 e_addr = LLADDR(sdl); 991 if ((e_addr[0] & 1) != 1) 992 return EADDRNOTAVAIL; 993 *llsa = 0; 994 return 0; 995 996 #ifdef INET 997 case AF_INET: 998 sin = (struct sockaddr_in *)sa; 999 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) 1000 return EADDRNOTAVAIL; 1001 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR, 1002 M_WAITOK|M_ZERO); 1003 sdl->sdl_len = sizeof *sdl; 1004 sdl->sdl_family = AF_LINK; 1005 sdl->sdl_index = ifp->if_index; 1006 sdl->sdl_type = IFT_ETHER; 1007 sdl->sdl_alen = ETHER_ADDR_LEN; 1008 e_addr = LLADDR(sdl); 1009 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr); 1010 *llsa = (struct sockaddr *)sdl; 1011 return 0; 1012 #endif 1013 #ifdef INET6 1014 case AF_INET6: 1015 sin6 = (struct sockaddr_in6 *)sa; 1016 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 1017 /* 1018 * An IP6 address of 0 means listen to all 1019 * of the Ethernet multicast address used for IP6. 1020 * (This is used for multicast routers.) 1021 */ 1022 ifp->if_flags |= IFF_ALLMULTI; 1023 *llsa = 0; 1024 return 0; 1025 } 1026 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) 1027 return EADDRNOTAVAIL; 1028 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR, 1029 M_WAITOK|M_ZERO); 1030 sdl->sdl_len = sizeof *sdl; 1031 sdl->sdl_family = AF_LINK; 1032 sdl->sdl_index = ifp->if_index; 1033 sdl->sdl_type = IFT_ETHER; 1034 sdl->sdl_alen = ETHER_ADDR_LEN; 1035 e_addr = LLADDR(sdl); 1036 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr); 1037 *llsa = (struct sockaddr *)sdl; 1038 return 0; 1039 #endif 1040 1041 default: 1042 /* 1043 * Well, the text isn't quite right, but it's the name 1044 * that counts... 1045 */ 1046 return EAFNOSUPPORT; 1047 } 1048 } 1049 1050