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