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: src/sys/net/if_ethersubr.c,v 1.70.2.33 2003/04/28 15:45:53 archie Exp $ 35 * $DragonFly: src/sys/net/if_ethersubr.c,v 1.33 2005/11/28 17:13:45 dillon Exp $ 36 */ 37 38 #include "opt_atalk.h" 39 #include "opt_inet.h" 40 #include "opt_inet6.h" 41 #include "opt_ipx.h" 42 #include "opt_bdg.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/malloc.h> 49 #include <sys/mbuf.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/ifq_var.h> 61 #include <net/bpf.h> 62 #include <net/ethernet.h> 63 #include <net/bridge/bridge.h> 64 65 #if defined(INET) || defined(INET6) 66 #include <netinet/in.h> 67 #include <netinet/in_var.h> 68 #include <netinet/if_ether.h> 69 #include <net/ipfw/ip_fw.h> 70 #include <net/dummynet/ip_dummynet.h> 71 #endif 72 #ifdef INET6 73 #include <netinet6/nd6.h> 74 #endif 75 76 #ifdef IPX 77 #include <netproto/ipx/ipx.h> 78 #include <netproto/ipx/ipx_if.h> 79 int (*ef_inputp)(struct ifnet*, struct ether_header *eh, struct mbuf *m); 80 int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp, struct sockaddr *dst, 81 short *tp, int *hlen); 82 #endif 83 84 #ifdef NS 85 #include <netns/ns.h> 86 #include <netns/ns_if.h> 87 ushort ns_nettype; 88 int ether_outputdebug = 0; 89 int ether_inputdebug = 0; 90 #endif 91 92 #ifdef NETATALK 93 #include <netproto/atalk/at.h> 94 #include <netproto/atalk/at_var.h> 95 #include <netproto/atalk/at_extern.h> 96 97 #define llc_snap_org_code llc_un.type_snap.org_code 98 #define llc_snap_ether_type llc_un.type_snap.ether_type 99 100 extern u_char at_org_code[3]; 101 extern u_char aarp_org_code[3]; 102 #endif /* NETATALK */ 103 104 /* netgraph node hooks for ng_ether(4) */ 105 void (*ng_ether_input_p)(struct ifnet *ifp, 106 struct mbuf **mp, struct ether_header *eh); 107 void (*ng_ether_input_orphan_p)(struct ifnet *ifp, 108 struct mbuf *m, struct ether_header *eh); 109 int (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp); 110 void (*ng_ether_attach_p)(struct ifnet *ifp); 111 void (*ng_ether_detach_p)(struct ifnet *ifp); 112 113 int (*vlan_input_p)(struct ether_header *eh, struct mbuf *m); 114 int (*vlan_input_tag_p)(struct mbuf *m, uint16_t t); 115 116 static int ether_output(struct ifnet *, struct mbuf *, struct sockaddr *, 117 struct rtentry *); 118 119 /* 120 * bridge support 121 */ 122 int do_bridge; 123 bridge_in_t *bridge_in_ptr; 124 bdg_forward_t *bdg_forward_ptr; 125 bdgtakeifaces_t *bdgtakeifaces_ptr; 126 struct bdg_softc *ifp2sc; 127 128 static int ether_resolvemulti(struct ifnet *, struct sockaddr **, 129 struct sockaddr *); 130 131 const uint8_t etherbroadcastaddr[ETHER_ADDR_LEN] = { 132 0xff, 0xff, 0xff, 0xff, 0xff, 0xff 133 }; 134 135 #define gotoerr(e) do { error = (e); goto bad; } while (0) 136 #define IFP2AC(ifp) ((struct arpcom *)(ifp)) 137 138 static boolean_t ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst, 139 struct ip_fw **rule, struct ether_header *eh, 140 boolean_t shared); 141 142 static int ether_ipfw; 143 SYSCTL_DECL(_net_link); 144 SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet"); 145 SYSCTL_INT(_net_link_ether, OID_AUTO, ipfw, CTLFLAG_RW, 146 ðer_ipfw, 0, "Pass ether pkts through firewall"); 147 148 /* 149 * Ethernet output routine. 150 * Encapsulate a packet of type family for the local net. 151 * Use trailer local net encapsulation if enough data in first 152 * packet leaves a multiple of 512 bytes of data in remainder. 153 * Assumes that ifp is actually pointer to arpcom structure. 154 */ 155 static int 156 ether_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst, 157 struct rtentry *rt) 158 { 159 struct ether_header *eh, *deh; 160 u_char *edst; 161 int loop_copy = 0; 162 int hlen = ETHER_HDR_LEN; /* link layer header length */ 163 struct arpcom *ac = IFP2AC(ifp); 164 int error; 165 166 ASSERT_SERIALIZED(ifp->if_serializer); 167 168 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING)) 169 gotoerr(ENETDOWN); 170 171 M_PREPEND(m, sizeof(struct ether_header), MB_DONTWAIT); 172 if (m == NULL) 173 return (ENOBUFS); 174 eh = mtod(m, struct ether_header *); 175 edst = eh->ether_dhost; 176 177 /* 178 * Fill in the destination ethernet address and frame type. 179 */ 180 switch (dst->sa_family) { 181 #ifdef INET 182 case AF_INET: 183 if (!arpresolve(ifp, rt, m, dst, edst)) 184 return (0); /* if not yet resolved */ 185 eh->ether_type = htons(ETHERTYPE_IP); 186 break; 187 #endif 188 #ifdef INET6 189 case AF_INET6: 190 if (!nd6_storelladdr(&ac->ac_if, rt, m, dst, edst)) 191 return (0); /* Something bad happenned. */ 192 eh->ether_type = htons(ETHERTYPE_IPV6); 193 break; 194 #endif 195 #ifdef IPX 196 case AF_IPX: 197 if (ef_outputp != NULL) { 198 error = ef_outputp(ifp, &m, dst, &eh->ether_type, 199 &hlen); 200 if (error) 201 goto bad; 202 } else { 203 eh->ether_type = htons(ETHERTYPE_IPX); 204 bcopy(&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host), 205 edst, ETHER_ADDR_LEN); 206 } 207 break; 208 #endif 209 #ifdef NETATALK 210 case AF_APPLETALK: { 211 struct at_ifaddr *aa; 212 213 if ((aa = at_ifawithnet((struct sockaddr_at *)dst)) == NULL) { 214 error = 0; /* XXX */ 215 goto bad; 216 } 217 /* 218 * In the phase 2 case, need to prepend an mbuf for 219 * the llc header. Since we must preserve the value 220 * of m, which is passed to us by value, we m_copy() 221 * the first mbuf, and use it for our llc header. 222 */ 223 if (aa->aa_flags & AFA_PHASE2) { 224 struct llc llc; 225 226 M_PREPEND(m, sizeof(struct llc), MB_DONTWAIT); 227 eh = mtod(m, struct ether_header *); 228 edst = eh->ether_dhost; 229 llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP; 230 llc.llc_control = LLC_UI; 231 bcopy(at_org_code, llc.llc_snap_org_code, 232 sizeof at_org_code); 233 llc.llc_snap_ether_type = htons(ETHERTYPE_AT); 234 bcopy(&llc, 235 mtod(m, caddr_t) + sizeof(struct ether_header), 236 sizeof(struct llc)); 237 eh->ether_type = htons(m->m_pkthdr.len); 238 hlen = sizeof(struct llc) + ETHER_HDR_LEN; 239 } else { 240 eh->ether_type = htons(ETHERTYPE_AT); 241 } 242 if (!aarpresolve(ac, m, (struct sockaddr_at *)dst, edst)) 243 return (0); 244 break; 245 } 246 #endif 247 #ifdef NS 248 case AF_NS: 249 switch(ns_nettype) { 250 default: 251 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */ 252 eh->ether_type = 0x8137; 253 break; 254 case 0x0: /* Novell 802.3 */ 255 eh->ether_type = htons(m->m_pkthdr.len); 256 break; 257 case 0xe0e0: /* Novell 802.2 and Token-Ring */ 258 M_PREPEND(m, 3, MB_DONTWAIT); 259 eh = mtod(m, struct ether_header *); 260 edst = eh->ether_dhost; 261 eh->ether_type = htons(m->m_pkthdr.len); 262 cp = mtod(m, u_char *) + sizeof(struct ether_header); 263 *cp++ = 0xE0; 264 *cp++ = 0xE0; 265 *cp++ = 0x03; 266 break; 267 } 268 bcopy(&(((struct sockaddr_ns *)dst)->sns_addr.x_host), edst, 269 ETHER_ADDR_LEN); 270 /* 271 * XXX if ns_thishost is the same as the node's ethernet 272 * address then just the default code will catch this anyhow. 273 * So I'm not sure if this next clause should be here at all? 274 * [JRE] 275 */ 276 if (bcmp(edst, &ns_thishost, ETHER_ADDR_LEN) == 0) { 277 m->m_pkthdr.rcvif = ifp; 278 netisr_dispatch(NETISR_NS, m); 279 return (error); 280 } 281 if (bcmp(edst, &ns_broadhost, ETHER_ADDR_LEN) == 0) 282 m->m_flags |= M_BCAST; 283 break; 284 #endif 285 case pseudo_AF_HDRCMPLT: 286 case AF_UNSPEC: 287 loop_copy = -1; /* if this is for us, don't do it */ 288 deh = (struct ether_header *)dst->sa_data; 289 memcpy(edst, deh->ether_dhost, ETHER_ADDR_LEN); 290 eh->ether_type = deh->ether_type; 291 break; 292 293 default: 294 printf("%s: can't handle af%d\n", ifp->if_xname, 295 dst->sa_family); 296 gotoerr(EAFNOSUPPORT); 297 } 298 299 if (dst->sa_family == pseudo_AF_HDRCMPLT) /* unlikely */ 300 memcpy(eh->ether_shost, 301 ((struct ether_header *)dst->sa_data)->ether_shost, 302 ETHER_ADDR_LEN); 303 else 304 memcpy(eh->ether_shost, ac->ac_enaddr, ETHER_ADDR_LEN); 305 306 /* 307 * If a simplex interface, and the packet is being sent to our 308 * Ethernet address or a broadcast address, loopback a copy. 309 * XXX To make a simplex device behave exactly like a duplex 310 * device, we should copy in the case of sending to our own 311 * ethernet address (thus letting the original actually appear 312 * on the wire). However, we don't do that here for security 313 * reasons and compatibility with the original behavior. 314 */ 315 if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) { 316 int csum_flags = 0; 317 318 if (m->m_pkthdr.csum_flags & CSUM_IP) 319 csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID); 320 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) 321 csum_flags |= (CSUM_DATA_VALID | CSUM_PSEUDO_HDR); 322 if ((m->m_flags & M_BCAST) || (loop_copy > 0)) { 323 struct mbuf *n; 324 325 if ((n = m_copypacket(m, MB_DONTWAIT)) != NULL) { 326 n->m_pkthdr.csum_flags |= csum_flags; 327 if (csum_flags & CSUM_DATA_VALID) 328 n->m_pkthdr.csum_data = 0xffff; 329 if_simloop(ifp, n, dst->sa_family, hlen); 330 } else 331 ifp->if_iqdrops++; 332 } else if (bcmp(eh->ether_dhost, eh->ether_shost, 333 ETHER_ADDR_LEN) == 0) { 334 m->m_pkthdr.csum_flags |= csum_flags; 335 if (csum_flags & CSUM_DATA_VALID) 336 m->m_pkthdr.csum_data = 0xffff; 337 if_simloop(ifp, m, dst->sa_family, hlen); 338 return (0); /* XXX */ 339 } 340 } 341 342 /* Handle ng_ether(4) processing, if any */ 343 if (ng_ether_output_p != NULL) { 344 if ((error = (*ng_ether_output_p)(ifp, &m)) != 0) 345 goto bad; 346 if (m == NULL) 347 return (0); 348 } 349 350 /* Continue with link-layer output */ 351 return ether_output_frame(ifp, m); 352 353 bad: 354 m_freem(m); 355 return (error); 356 } 357 358 /* 359 * Ethernet link layer output routine to send a raw frame to the device. 360 * 361 * This assumes that the 14 byte Ethernet header is present and contiguous 362 * in the first mbuf (if BRIDGE'ing). 363 */ 364 int 365 ether_output_frame(struct ifnet *ifp, struct mbuf *m) 366 { 367 struct ip_fw *rule = NULL; 368 int error = 0; 369 struct altq_pktattr pktattr; 370 371 ASSERT_SERIALIZED(ifp->if_serializer); 372 373 /* Extract info from dummynet tag, ignore others */ 374 while (m->m_type == MT_TAG) { 375 if (m->m_flags == PACKET_TAG_DUMMYNET) { 376 rule = ((struct dn_pkt *)m)->rule; 377 break; 378 } 379 m = m->m_next; 380 } 381 if (rule != NULL) /* packet was already bridged */ 382 goto no_bridge; 383 384 if (BDG_ACTIVE(ifp)) { 385 struct ether_header *eh; /* a pointer suffices */ 386 387 m->m_pkthdr.rcvif = NULL; 388 eh = mtod(m, struct ether_header *); 389 m_adj(m, ETHER_HDR_LEN); 390 lwkt_serialize_exit(ifp->if_serializer); 391 m = bdg_forward_ptr(m, eh, ifp); 392 lwkt_serialize_enter(ifp->if_serializer); 393 m_freem(m); 394 return (0); 395 } 396 397 no_bridge: 398 if (ifq_is_enabled(&ifp->if_snd)) 399 altq_etherclassify(&ifp->if_snd, m, &pktattr); 400 crit_enter(); 401 if (IPFW_LOADED && ether_ipfw != 0) { 402 struct ether_header save_eh, *eh; 403 404 eh = mtod(m, struct ether_header *); 405 save_eh = *eh; 406 m_adj(m, ETHER_HDR_LEN); 407 if (!ether_ipfw_chk(&m, ifp, &rule, eh, FALSE)) { 408 crit_exit(); 409 if (m != NULL) { 410 m_freem(m); 411 return ENOBUFS; /* pkt dropped */ 412 } else 413 return 0; /* consumed e.g. in a pipe */ 414 } 415 eh = mtod(m, struct ether_header *); 416 /* packet was ok, restore the ethernet header */ 417 if ((void *)(eh + 1) == (void *)m->m_data) { 418 m->m_data -= ETHER_HDR_LEN ; 419 m->m_len += ETHER_HDR_LEN ; 420 m->m_pkthdr.len += ETHER_HDR_LEN ; 421 } else { 422 M_PREPEND(m, ETHER_HDR_LEN, MB_DONTWAIT); 423 if (m == NULL) /* nope... */ { 424 crit_exit(); 425 return ENOBUFS; 426 } 427 bcopy(&save_eh, mtod(m, struct ether_header *), 428 ETHER_HDR_LEN); 429 } 430 } 431 crit_exit(); 432 433 /* 434 * Queue message on interface, update output statistics if 435 * successful, and start output if interface not yet active. 436 */ 437 error = ifq_handoff(ifp, m, &pktattr); 438 return (error); 439 } 440 441 /* 442 * ipfw processing for ethernet packets (in and out). 443 * The second parameter is NULL from ether_demux(), and ifp from 444 * ether_output_frame(). This section of code could be used from 445 * bridge.c as well as long as we use some extra info 446 * to distinguish that case from ether_output_frame(). 447 */ 448 static boolean_t 449 ether_ipfw_chk( 450 struct mbuf **m0, 451 struct ifnet *dst, 452 struct ip_fw **rule, 453 struct ether_header *eh, 454 boolean_t shared) 455 { 456 struct ether_header save_eh = *eh; /* might be a ptr in m */ 457 struct ip_fw_args args; 458 struct m_tag *mtag; 459 int i; 460 461 if (*rule != NULL && fw_one_pass) 462 return TRUE; /* dummynet packet, already partially processed */ 463 464 /* 465 * I need some amount of data to be contiguous, and in case others 466 * need the packet (shared==TRUE), it also better be in the first mbuf. 467 */ 468 i = min((*m0)->m_pkthdr.len, max_protohdr); 469 if (shared || (*m0)->m_len < i) { 470 *m0 = m_pullup(*m0, i); 471 if (*m0 == NULL) 472 return FALSE; 473 } 474 475 args.m = *m0; /* the packet we are looking at */ 476 args.oif = dst; /* destination, if any */ 477 if ((mtag = m_tag_find(*m0, PACKET_TAG_IPFW_DIVERT, NULL)) != NULL) 478 m_tag_delete(*m0, mtag); 479 args.rule = *rule; /* matching rule to restart */ 480 args.next_hop = NULL; /* we do not support forward yet */ 481 args.eh = &save_eh; /* MAC header for bridged/MAC packets */ 482 i = ip_fw_chk_ptr(&args); 483 *m0 = args.m; 484 *rule = args.rule; 485 486 if ((i & IP_FW_PORT_DENY_FLAG) || *m0 == NULL) /* drop */ 487 return FALSE; 488 489 if (i == 0) /* a PASS rule. */ 490 return TRUE; 491 492 if (DUMMYNET_LOADED && (i & IP_FW_PORT_DYNT_FLAG)) { 493 /* 494 * Pass the pkt to dummynet, which consumes it. 495 * If shared, make a copy and keep the original. 496 */ 497 struct mbuf *m ; 498 499 if (shared) { 500 m = m_copypacket(*m0, MB_DONTWAIT); 501 if (m == NULL) 502 return FALSE; 503 } else { 504 m = *m0 ; /* pass the original to dummynet */ 505 *m0 = NULL ; /* and nothing back to the caller */ 506 } 507 /* 508 * Prepend the header, optimize for the common case of 509 * eh pointing into the mbuf. 510 */ 511 if ((void *)(eh + 1) == (void *)m->m_data) { 512 m->m_data -= ETHER_HDR_LEN ; 513 m->m_len += ETHER_HDR_LEN ; 514 m->m_pkthdr.len += ETHER_HDR_LEN ; 515 } else { 516 M_PREPEND(m, ETHER_HDR_LEN, MB_DONTWAIT); 517 if (m == NULL) 518 return FALSE; 519 bcopy(&save_eh, mtod(m, struct ether_header *), 520 ETHER_HDR_LEN); 521 } 522 ip_dn_io_ptr(m, (i & 0xffff), 523 dst ? DN_TO_ETH_OUT: DN_TO_ETH_DEMUX, &args); 524 return FALSE; 525 } 526 /* 527 * XXX at some point add support for divert/forward actions. 528 * If none of the above matches, we have to drop the pkt. 529 */ 530 return FALSE; 531 } 532 533 /* 534 * XXX merge this function with ether_input. 535 */ 536 static void 537 ether_input_internal(struct ifnet *ifp, struct mbuf *m) 538 { 539 ether_input(ifp, NULL, m); 540 } 541 542 /* 543 * Process a received Ethernet packet. We have two different interfaces: 544 * one (conventional) assumes the packet in the mbuf, with the ethernet 545 * header provided separately in *eh. The second one (new) has everything 546 * in the mbuf, and we can tell it because eh == NULL. 547 * The caller MUST MAKE SURE that there are at least 548 * sizeof(struct ether_header) bytes in the first mbuf. 549 * 550 * This allows us to concentrate in one place a bunch of code which 551 * is replicated in all device drivers. Also, many functions called 552 * from ether_input() try to put the eh back into the mbuf, so we 553 * can later propagate the 'contiguous packet' interface to them, 554 * and handle the old interface just here. 555 * 556 * NOTA BENE: for many drivers "eh" is a pointer into the first mbuf or 557 * cluster, right before m_data. So be very careful when working on m, 558 * as you could destroy *eh !! 559 * 560 * First we perform any link layer operations, then continue 561 * to the upper layers with ether_demux(). 562 */ 563 void 564 ether_input(struct ifnet *ifp, struct ether_header *eh, struct mbuf *m) 565 { 566 struct ether_header save_eh; 567 568 ASSERT_SERIALIZED(ifp->if_serializer); 569 570 if (eh == NULL) { 571 if (m->m_len < sizeof(struct ether_header)) { 572 /* XXX error in the caller. */ 573 m_freem(m); 574 return; 575 } 576 m->m_pkthdr.rcvif = ifp; 577 eh = mtod(m, struct ether_header *); 578 m_adj(m, sizeof(struct ether_header)); 579 /* XXX */ 580 /* m->m_pkthdr.len = m->m_len; */ 581 } 582 583 if (ifp->if_bpf) 584 bpf_ptap(ifp->if_bpf, m, eh, ETHER_HDR_LEN); 585 586 ifp->if_ibytes += m->m_pkthdr.len + (sizeof *eh); 587 588 /* Handle ng_ether(4) processing, if any */ 589 if (ng_ether_input_p != NULL) { 590 lwkt_serialize_exit(ifp->if_serializer); 591 (*ng_ether_input_p)(ifp, &m, eh); 592 lwkt_serialize_enter(ifp->if_serializer); 593 if (m == NULL) 594 return; 595 } 596 597 /* Check for bridging mode */ 598 if (BDG_ACTIVE(ifp)) { 599 struct ifnet *bif; 600 601 /* Check with bridging code */ 602 if ((bif = bridge_in_ptr(ifp, eh)) == BDG_DROP) { 603 m_freem(m); 604 return; 605 } 606 if (bif != BDG_LOCAL) { 607 save_eh = *eh ; /* because it might change */ 608 lwkt_serialize_exit(ifp->if_serializer); 609 m = bdg_forward_ptr(m, eh, bif); /* needs forwarding */ 610 lwkt_serialize_enter(ifp->if_serializer); 611 /* 612 * Do not continue if bdg_forward_ptr() processed our 613 * packet (and cleared the mbuf pointer m) or if 614 * it dropped (m_free'd) the packet itself. 615 */ 616 if (m == NULL) { 617 if (bif == BDG_BCAST || bif == BDG_MCAST) 618 printf("bdg_forward drop MULTICAST PKT\n"); 619 return; 620 } 621 eh = &save_eh ; 622 } 623 if (bif == BDG_LOCAL || bif == BDG_BCAST || bif == BDG_MCAST) 624 goto recvLocal; /* receive locally */ 625 626 /* If not local and not multicast, just drop it */ 627 m_freem(m); 628 return; 629 } 630 631 recvLocal: 632 /* Continue with upper layer processing */ 633 ether_demux(ifp, eh, m); 634 } 635 636 /* 637 * Upper layer processing for a received Ethernet packet. 638 */ 639 void 640 ether_demux(struct ifnet *ifp, struct ether_header *eh, struct mbuf *m) 641 { 642 int isr; 643 u_short ether_type; 644 struct ip_fw *rule = NULL; 645 #ifdef NETATALK 646 struct llc *l; 647 #endif 648 649 /* Extract info from dummynet tag, ignore others */ 650 while (m->m_type == MT_TAG) { 651 if (m->m_flags == PACKET_TAG_DUMMYNET) { 652 rule = ((struct dn_pkt *)m)->rule; 653 ifp = m->m_next->m_pkthdr.rcvif; 654 break; 655 } 656 m = m->m_next; 657 } 658 if (rule) /* packet was already bridged */ 659 goto post_stats; 660 661 /* 662 * Discard packet if upper layers shouldn't see it because 663 * it was unicast to a different Ethernet address. If the 664 * driver is working properly, then this situation can only 665 * happen when the interface is in promiscuous mode. 666 */ 667 if (!BDG_ACTIVE(ifp) && 668 ((ifp->if_flags & (IFF_PROMISC | IFF_PPROMISC)) == IFF_PROMISC) && 669 (eh->ether_dhost[0] & 1) == 0 && 670 bcmp(eh->ether_dhost, IFP2AC(ifp)->ac_enaddr, ETHER_ADDR_LEN)) { 671 m_freem(m); 672 return; 673 } 674 /* Discard packet if interface is not up */ 675 if (!(ifp->if_flags & IFF_UP)) { 676 m_freem(m); 677 return; 678 } 679 if (eh->ether_dhost[0] & 1) { 680 if (bcmp(ifp->if_broadcastaddr, eh->ether_dhost, 681 ifp->if_addrlen) == 0) 682 m->m_flags |= M_BCAST; 683 else 684 m->m_flags |= M_MCAST; 685 ifp->if_imcasts++; 686 } 687 688 post_stats: 689 if (IPFW_LOADED && ether_ipfw != 0) { 690 if (!ether_ipfw_chk(&m, NULL, &rule, eh, FALSE)) { 691 m_freem(m); 692 return; 693 } 694 eh = mtod(m, struct ether_header *); 695 } 696 697 ether_type = ntohs(eh->ether_type); 698 699 switch (ether_type) { 700 #ifdef INET 701 case ETHERTYPE_IP: 702 if (ipflow_fastforward(m, ifp->if_serializer)) 703 return; 704 isr = NETISR_IP; 705 break; 706 707 case ETHERTYPE_ARP: 708 if (ifp->if_flags & IFF_NOARP) { 709 /* Discard packet if ARP is disabled on interface */ 710 m_freem(m); 711 return; 712 } 713 isr = NETISR_ARP; 714 break; 715 #endif 716 717 #ifdef INET6 718 case ETHERTYPE_IPV6: 719 isr = NETISR_IPV6; 720 break; 721 #endif 722 723 #ifdef IPX 724 case ETHERTYPE_IPX: 725 if (ef_inputp && ef_inputp(ifp, eh, m) == 0) 726 return; 727 isr = NETISR_IPX; 728 break; 729 #endif 730 731 #ifdef NS 732 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */ 733 isr = NETISR_NS; 734 break; 735 736 #endif 737 738 #ifdef NETATALK 739 case ETHERTYPE_AT: 740 isr = NETISR_ATALK1; 741 break; 742 case ETHERTYPE_AARP: 743 isr = NETISR_AARP; 744 break; 745 #endif 746 747 case ETHERTYPE_VLAN: 748 if (vlan_input_p != NULL) 749 (*vlan_input_p)(eh, m); 750 else { 751 m->m_pkthdr.rcvif->if_noproto++; 752 m_freem(m); 753 } 754 return; 755 756 default: 757 #ifdef IPX 758 if (ef_inputp && ef_inputp(ifp, eh, m) == 0) 759 return; 760 #endif 761 #ifdef NS 762 checksum = mtod(m, ushort *); 763 /* Novell 802.3 */ 764 if ((ether_type <= ETHERMTU) && 765 ((*checksum == 0xffff) || (*checksum == 0xE0E0))) { 766 if (*checksum == 0xE0E0) { 767 m->m_pkthdr.len -= 3; 768 m->m_len -= 3; 769 m->m_data += 3; 770 } 771 isr = NETISR_NS; 772 break; 773 } 774 #endif 775 #ifdef NETATALK 776 if (ether_type > ETHERMTU) 777 goto dropanyway; 778 l = mtod(m, struct llc *); 779 if (l->llc_dsap == LLC_SNAP_LSAP && 780 l->llc_ssap == LLC_SNAP_LSAP && 781 l->llc_control == LLC_UI) { 782 if (bcmp(&(l->llc_snap_org_code)[0], at_org_code, 783 sizeof at_org_code) == 0 && 784 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) { 785 m_adj(m, sizeof(struct llc)); 786 isr = NETISR_ATALK2; 787 break; 788 } 789 if (bcmp(&(l->llc_snap_org_code)[0], aarp_org_code, 790 sizeof aarp_org_code) == 0 && 791 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) { 792 m_adj(m, sizeof(struct llc)); 793 isr = NETISR_AARP; 794 break; 795 } 796 } 797 dropanyway: 798 #endif 799 if (ng_ether_input_orphan_p != NULL) 800 (*ng_ether_input_orphan_p)(ifp, m, eh); 801 else 802 m_freem(m); 803 return; 804 } 805 netisr_dispatch(isr, m); 806 } 807 808 /* 809 * Perform common duties while attaching to interface list 810 */ 811 812 void 813 ether_ifattach(struct ifnet *ifp, uint8_t *lla, lwkt_serialize_t serializer) 814 { 815 ether_ifattach_bpf(ifp, lla, DLT_EN10MB, sizeof(struct ether_header), 816 serializer); 817 } 818 819 void 820 ether_ifattach_bpf(struct ifnet *ifp, uint8_t *lla, u_int dlt, u_int hdrlen, 821 lwkt_serialize_t serializer) 822 { 823 struct sockaddr_dl *sdl; 824 825 ifp->if_type = IFT_ETHER; 826 ifp->if_addrlen = ETHER_ADDR_LEN; 827 ifp->if_hdrlen = ETHER_HDR_LEN; 828 if_attach(ifp, serializer); 829 ifp->if_mtu = ETHERMTU; 830 if (ifp->if_baudrate == 0) 831 ifp->if_baudrate = 10000000; 832 ifp->if_output = ether_output; 833 ifp->if_input = ether_input_internal; 834 ifp->if_resolvemulti = ether_resolvemulti; 835 ifp->if_broadcastaddr = etherbroadcastaddr; 836 sdl = IF_LLSOCKADDR(ifp); 837 sdl->sdl_type = IFT_ETHER; 838 sdl->sdl_alen = ifp->if_addrlen; 839 bcopy(lla, LLADDR(sdl), ifp->if_addrlen); 840 /* 841 * XXX Keep the current drivers happy. 842 * XXX Remove once all drivers have been cleaned up 843 */ 844 if (lla != IFP2AC(ifp)->ac_enaddr) 845 bcopy(lla, IFP2AC(ifp)->ac_enaddr, ifp->if_addrlen); 846 bpfattach(ifp, dlt, hdrlen); 847 if (ng_ether_attach_p != NULL) 848 (*ng_ether_attach_p)(ifp); 849 if (BDG_LOADED) 850 bdgtakeifaces_ptr(); 851 852 if_printf(ifp, "MAC address: %6D\n", lla, ":"); 853 } 854 855 /* 856 * Perform common duties while detaching an Ethernet interface 857 */ 858 void 859 ether_ifdetach(struct ifnet *ifp) 860 { 861 if_down(ifp); 862 863 if (ng_ether_detach_p != NULL) 864 (*ng_ether_detach_p)(ifp); 865 bpfdetach(ifp); 866 if_detach(ifp); 867 if (BDG_LOADED) 868 bdgtakeifaces_ptr(); 869 } 870 871 int 872 ether_ioctl(struct ifnet *ifp, int command, caddr_t data) 873 { 874 struct ifaddr *ifa = (struct ifaddr *) data; 875 struct ifreq *ifr = (struct ifreq *) data; 876 int error = 0; 877 878 ASSERT_SERIALIZED(ifp->if_serializer); 879 880 switch (command) { 881 case SIOCSIFADDR: 882 ifp->if_flags |= IFF_UP; 883 884 switch (ifa->ifa_addr->sa_family) { 885 #ifdef INET 886 case AF_INET: 887 ifp->if_init(ifp->if_softc); /* before arpwhohas */ 888 arp_ifinit(ifp, ifa); 889 break; 890 #endif 891 #ifdef IPX 892 /* 893 * XXX - This code is probably wrong 894 */ 895 case AF_IPX: 896 { 897 struct ipx_addr *ina = &IA_SIPX(ifa)->sipx_addr; 898 struct arpcom *ac = IFP2AC(ifp); 899 900 if (ipx_nullhost(*ina)) 901 ina->x_host = *(union ipx_host *) ac->ac_enaddr; 902 else 903 bcopy(ina->x_host.c_host, ac->ac_enaddr, 904 sizeof ac->ac_enaddr); 905 906 ifp->if_init(ifp->if_softc); /* Set new address. */ 907 break; 908 } 909 #endif 910 #ifdef NS 911 /* 912 * XXX - This code is probably wrong 913 */ 914 case AF_NS: 915 { 916 struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr); 917 struct arpcom *ac = IFP2AC(ifp); 918 919 if (ns_nullhost(*ina)) 920 ina->x_host = *(union ns_host *)(ac->ac_enaddr); 921 else 922 bcopy(ina->x_host.c_host, ac->ac_enaddr, 923 sizeof ac->ac_enaddr); 924 925 /* 926 * Set new address 927 */ 928 ifp->if_init(ifp->if_softc); 929 break; 930 } 931 #endif 932 default: 933 ifp->if_init(ifp->if_softc); 934 break; 935 } 936 break; 937 938 case SIOCGIFADDR: 939 bcopy(IFP2AC(ifp)->ac_enaddr, 940 ((struct sockaddr *)ifr->ifr_data)->sa_data, 941 ETHER_ADDR_LEN); 942 break; 943 944 case SIOCSIFMTU: 945 /* 946 * Set the interface MTU. 947 */ 948 if (ifr->ifr_mtu > ETHERMTU) { 949 error = EINVAL; 950 } else { 951 ifp->if_mtu = ifr->ifr_mtu; 952 } 953 break; 954 default: 955 error = EINVAL; 956 break; 957 } 958 return (error); 959 } 960 961 int 962 ether_resolvemulti( 963 struct ifnet *ifp, 964 struct sockaddr **llsa, 965 struct sockaddr *sa) 966 { 967 struct sockaddr_dl *sdl; 968 struct sockaddr_in *sin; 969 #ifdef INET6 970 struct sockaddr_in6 *sin6; 971 #endif 972 u_char *e_addr; 973 974 switch(sa->sa_family) { 975 case AF_LINK: 976 /* 977 * No mapping needed. Just check that it's a valid MC address. 978 */ 979 sdl = (struct sockaddr_dl *)sa; 980 e_addr = LLADDR(sdl); 981 if ((e_addr[0] & 1) != 1) 982 return EADDRNOTAVAIL; 983 *llsa = 0; 984 return 0; 985 986 #ifdef INET 987 case AF_INET: 988 sin = (struct sockaddr_in *)sa; 989 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) 990 return EADDRNOTAVAIL; 991 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR, 992 M_WAITOK | M_ZERO); 993 sdl->sdl_len = sizeof *sdl; 994 sdl->sdl_family = AF_LINK; 995 sdl->sdl_index = ifp->if_index; 996 sdl->sdl_type = IFT_ETHER; 997 sdl->sdl_alen = ETHER_ADDR_LEN; 998 e_addr = LLADDR(sdl); 999 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr); 1000 *llsa = (struct sockaddr *)sdl; 1001 return 0; 1002 #endif 1003 #ifdef INET6 1004 case AF_INET6: 1005 sin6 = (struct sockaddr_in6 *)sa; 1006 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 1007 /* 1008 * An IP6 address of 0 means listen to all 1009 * of the Ethernet multicast address used for IP6. 1010 * (This is used for multicast routers.) 1011 */ 1012 ifp->if_flags |= IFF_ALLMULTI; 1013 *llsa = 0; 1014 return 0; 1015 } 1016 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) 1017 return EADDRNOTAVAIL; 1018 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR, 1019 M_WAITOK | M_ZERO); 1020 sdl->sdl_len = sizeof *sdl; 1021 sdl->sdl_family = AF_LINK; 1022 sdl->sdl_index = ifp->if_index; 1023 sdl->sdl_type = IFT_ETHER; 1024 sdl->sdl_alen = ETHER_ADDR_LEN; 1025 e_addr = LLADDR(sdl); 1026 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr); 1027 *llsa = (struct sockaddr *)sdl; 1028 return 0; 1029 #endif 1030 1031 default: 1032 /* 1033 * Well, the text isn't quite right, but it's the name 1034 * that counts... 1035 */ 1036 return EAFNOSUPPORT; 1037 } 1038 } 1039 1040 #if 0 1041 /* 1042 * This is for reference. We have a table-driven version 1043 * of the little-endian crc32 generator, which is faster 1044 * than the double-loop. 1045 */ 1046 uint32_t 1047 ether_crc32_le(const uint8_t *buf, size_t len) 1048 { 1049 uint32_t c, crc, carry; 1050 size_t i, j; 1051 1052 crc = 0xffffffffU; /* initial value */ 1053 1054 for (i = 0; i < len; i++) { 1055 c = buf[i]; 1056 for (j = 0; j < 8; j++) { 1057 carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01); 1058 crc >>= 1; 1059 c >>= 1; 1060 if (carry) 1061 crc = (crc ^ ETHER_CRC_POLY_LE); 1062 } 1063 } 1064 1065 return (crc); 1066 } 1067 #else 1068 uint32_t 1069 ether_crc32_le(const uint8_t *buf, size_t len) 1070 { 1071 static const uint32_t crctab[] = { 1072 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac, 1073 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c, 1074 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c, 1075 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c 1076 }; 1077 uint32_t crc; 1078 size_t i; 1079 1080 crc = 0xffffffffU; /* initial value */ 1081 1082 for (i = 0; i < len; i++) { 1083 crc ^= buf[i]; 1084 crc = (crc >> 4) ^ crctab[crc & 0xf]; 1085 crc = (crc >> 4) ^ crctab[crc & 0xf]; 1086 } 1087 1088 return (crc); 1089 } 1090 #endif 1091 1092 uint32_t 1093 ether_crc32_be(const uint8_t *buf, size_t len) 1094 { 1095 uint32_t c, crc, carry; 1096 size_t i, j; 1097 1098 crc = 0xffffffffU; /* initial value */ 1099 1100 for (i = 0; i < len; i++) { 1101 c = buf[i]; 1102 for (j = 0; j < 8; j++) { 1103 carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01); 1104 crc <<= 1; 1105 c >>= 1; 1106 if (carry) 1107 crc = (crc ^ ETHER_CRC_POLY_BE) | carry; 1108 } 1109 } 1110 1111 return (crc); 1112 } 1113 1114 /* 1115 * find the size of ethernet header, and call classifier 1116 */ 1117 void 1118 altq_etherclassify(struct ifaltq *ifq, struct mbuf *m, 1119 struct altq_pktattr *pktattr) 1120 { 1121 struct ether_header *eh; 1122 uint16_t ether_type; 1123 int hlen, af, hdrsize; 1124 caddr_t hdr; 1125 1126 hlen = sizeof(struct ether_header); 1127 eh = mtod(m, struct ether_header *); 1128 1129 ether_type = ntohs(eh->ether_type); 1130 if (ether_type < ETHERMTU) { 1131 /* ick! LLC/SNAP */ 1132 struct llc *llc = (struct llc *)(eh + 1); 1133 hlen += 8; 1134 1135 if (m->m_len < hlen || 1136 llc->llc_dsap != LLC_SNAP_LSAP || 1137 llc->llc_ssap != LLC_SNAP_LSAP || 1138 llc->llc_control != LLC_UI) 1139 goto bad; /* not snap! */ 1140 1141 ether_type = ntohs(llc->llc_un.type_snap.ether_type); 1142 } 1143 1144 if (ether_type == ETHERTYPE_IP) { 1145 af = AF_INET; 1146 hdrsize = 20; /* sizeof(struct ip) */ 1147 #ifdef INET6 1148 } else if (ether_type == ETHERTYPE_IPV6) { 1149 af = AF_INET6; 1150 hdrsize = 40; /* sizeof(struct ip6_hdr) */ 1151 #endif 1152 } else 1153 goto bad; 1154 1155 while (m->m_len <= hlen) { 1156 hlen -= m->m_len; 1157 m = m->m_next; 1158 } 1159 hdr = m->m_data + hlen; 1160 if (m->m_len < hlen + hdrsize) { 1161 /* 1162 * ip header is not in a single mbuf. this should not 1163 * happen in the current code. 1164 * (todo: use m_pulldown in the future) 1165 */ 1166 goto bad; 1167 } 1168 m->m_data += hlen; 1169 m->m_len -= hlen; 1170 ifq_classify(ifq, m, af, pktattr); 1171 m->m_data -= hlen; 1172 m->m_len += hlen; 1173 1174 return; 1175 1176 bad: 1177 pktattr->pattr_class = NULL; 1178 pktattr->pattr_hdr = NULL; 1179 pktattr->pattr_af = AF_UNSPEC; 1180 } 1181