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.75 2008/07/07 22:02:10 nant 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_mpls.h" 43 #include "opt_netgraph.h" 44 #include "opt_carp.h" 45 #include "opt_ethernet.h" 46 47 #include <sys/param.h> 48 #include <sys/systm.h> 49 #include <sys/globaldata.h> 50 #include <sys/kernel.h> 51 #include <sys/malloc.h> 52 #include <sys/mbuf.h> 53 #include <sys/msgport.h> 54 #include <sys/socket.h> 55 #include <sys/sockio.h> 56 #include <sys/sysctl.h> 57 #include <sys/thread.h> 58 #include <sys/thread2.h> 59 60 #include <net/if.h> 61 #include <net/netisr.h> 62 #include <net/route.h> 63 #include <net/if_llc.h> 64 #include <net/if_dl.h> 65 #include <net/if_types.h> 66 #include <net/ifq_var.h> 67 #include <net/bpf.h> 68 #include <net/ethernet.h> 69 #include <net/vlan/if_vlan_ether.h> 70 #include <net/netmsg2.h> 71 72 #if defined(INET) || defined(INET6) 73 #include <netinet/in.h> 74 #include <netinet/in_var.h> 75 #include <netinet/if_ether.h> 76 #include <net/ipfw/ip_fw.h> 77 #include <net/dummynet/ip_dummynet.h> 78 #endif 79 #ifdef INET6 80 #include <netinet6/nd6.h> 81 #endif 82 83 #ifdef CARP 84 #include <netinet/ip_carp.h> 85 #endif 86 87 #ifdef IPX 88 #include <netproto/ipx/ipx.h> 89 #include <netproto/ipx/ipx_if.h> 90 int (*ef_inputp)(struct ifnet*, const struct ether_header *eh, struct mbuf *m); 91 int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp, struct sockaddr *dst, 92 short *tp, int *hlen); 93 #endif 94 95 #ifdef NS 96 #include <netns/ns.h> 97 #include <netns/ns_if.h> 98 ushort ns_nettype; 99 int ether_outputdebug = 0; 100 int ether_inputdebug = 0; 101 #endif 102 103 #ifdef NETATALK 104 #include <netproto/atalk/at.h> 105 #include <netproto/atalk/at_var.h> 106 #include <netproto/atalk/at_extern.h> 107 108 #define llc_snap_org_code llc_un.type_snap.org_code 109 #define llc_snap_ether_type llc_un.type_snap.ether_type 110 111 extern u_char at_org_code[3]; 112 extern u_char aarp_org_code[3]; 113 #endif /* NETATALK */ 114 115 #ifdef MPLS 116 #include <netproto/mpls/mpls.h> 117 #endif 118 119 /* netgraph node hooks for ng_ether(4) */ 120 void (*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp); 121 void (*ng_ether_input_orphan_p)(struct ifnet *ifp, 122 struct mbuf *m, const struct ether_header *eh); 123 int (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp); 124 void (*ng_ether_attach_p)(struct ifnet *ifp); 125 void (*ng_ether_detach_p)(struct ifnet *ifp); 126 127 int (*vlan_input_p)(struct mbuf *, struct mbuf_chain *); 128 void (*vlan_input2_p)(struct mbuf *); 129 130 static int ether_output(struct ifnet *, struct mbuf *, struct sockaddr *, 131 struct rtentry *); 132 static void ether_restore_header(struct mbuf **, const struct ether_header *, 133 const struct ether_header *); 134 static void ether_demux_chain(struct ifnet *, struct mbuf *, 135 struct mbuf_chain *); 136 137 /* 138 * if_bridge support 139 */ 140 struct mbuf *(*bridge_input_p)(struct ifnet *, struct mbuf *); 141 int (*bridge_output_p)(struct ifnet *, struct mbuf *); 142 void (*bridge_dn_p)(struct mbuf *, struct ifnet *); 143 144 static int ether_resolvemulti(struct ifnet *, struct sockaddr **, 145 struct sockaddr *); 146 147 const uint8_t etherbroadcastaddr[ETHER_ADDR_LEN] = { 148 0xff, 0xff, 0xff, 0xff, 0xff, 0xff 149 }; 150 151 #define gotoerr(e) do { error = (e); goto bad; } while (0) 152 #define IFP2AC(ifp) ((struct arpcom *)(ifp)) 153 154 static boolean_t ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst, 155 struct ip_fw **rule, 156 const struct ether_header *eh); 157 158 static int ether_ipfw; 159 static u_int ether_restore_hdr; 160 static u_int ether_prepend_hdr; 161 162 SYSCTL_DECL(_net_link); 163 SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet"); 164 SYSCTL_INT(_net_link_ether, OID_AUTO, ipfw, CTLFLAG_RW, 165 ðer_ipfw, 0, "Pass ether pkts through firewall"); 166 SYSCTL_UINT(_net_link_ether, OID_AUTO, restore_hdr, CTLFLAG_RW, 167 ðer_restore_hdr, 0, "# of ether header restoration"); 168 SYSCTL_UINT(_net_link_ether, OID_AUTO, prepend_hdr, CTLFLAG_RW, 169 ðer_prepend_hdr, 0, 170 "# of ether header restoration which prepends mbuf"); 171 172 /* 173 * Ethernet output routine. 174 * Encapsulate a packet of type family for the local net. 175 * Use trailer local net encapsulation if enough data in first 176 * packet leaves a multiple of 512 bytes of data in remainder. 177 * Assumes that ifp is actually pointer to arpcom structure. 178 */ 179 static int 180 ether_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst, 181 struct rtentry *rt) 182 { 183 struct ether_header *eh, *deh; 184 u_char *edst; 185 int loop_copy = 0; 186 int hlen = ETHER_HDR_LEN; /* link layer header length */ 187 struct arpcom *ac = IFP2AC(ifp); 188 int error; 189 190 ASSERT_NOT_SERIALIZED(ifp->if_serializer); 191 192 if (ifp->if_flags & IFF_MONITOR) 193 gotoerr(ENETDOWN); 194 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING)) 195 gotoerr(ENETDOWN); 196 197 M_PREPEND(m, sizeof(struct ether_header), MB_DONTWAIT); 198 if (m == NULL) 199 return (ENOBUFS); 200 eh = mtod(m, struct ether_header *); 201 edst = eh->ether_dhost; 202 203 /* 204 * Fill in the destination ethernet address and frame type. 205 */ 206 switch (dst->sa_family) { 207 #ifdef INET 208 case AF_INET: 209 if (!arpresolve(ifp, rt, m, dst, edst)) 210 return (0); /* if not yet resolved */ 211 eh->ether_type = htons(ETHERTYPE_IP); 212 break; 213 #endif 214 #ifdef INET6 215 case AF_INET6: 216 if (!nd6_storelladdr(&ac->ac_if, rt, m, dst, edst)) 217 return (0); /* Something bad happenned. */ 218 eh->ether_type = htons(ETHERTYPE_IPV6); 219 break; 220 #endif 221 #ifdef IPX 222 case AF_IPX: 223 if (ef_outputp != NULL) { 224 error = ef_outputp(ifp, &m, dst, &eh->ether_type, 225 &hlen); 226 if (error) 227 goto bad; 228 } else { 229 eh->ether_type = htons(ETHERTYPE_IPX); 230 bcopy(&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host), 231 edst, ETHER_ADDR_LEN); 232 } 233 break; 234 #endif 235 #ifdef NETATALK 236 case AF_APPLETALK: { 237 struct at_ifaddr *aa; 238 239 if ((aa = at_ifawithnet((struct sockaddr_at *)dst)) == NULL) { 240 error = 0; /* XXX */ 241 goto bad; 242 } 243 /* 244 * In the phase 2 case, need to prepend an mbuf for 245 * the llc header. Since we must preserve the value 246 * of m, which is passed to us by value, we m_copy() 247 * the first mbuf, and use it for our llc header. 248 */ 249 if (aa->aa_flags & AFA_PHASE2) { 250 struct llc llc; 251 252 M_PREPEND(m, sizeof(struct llc), MB_DONTWAIT); 253 eh = mtod(m, struct ether_header *); 254 edst = eh->ether_dhost; 255 llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP; 256 llc.llc_control = LLC_UI; 257 bcopy(at_org_code, llc.llc_snap_org_code, 258 sizeof at_org_code); 259 llc.llc_snap_ether_type = htons(ETHERTYPE_AT); 260 bcopy(&llc, 261 mtod(m, caddr_t) + sizeof(struct ether_header), 262 sizeof(struct llc)); 263 eh->ether_type = htons(m->m_pkthdr.len); 264 hlen = sizeof(struct llc) + ETHER_HDR_LEN; 265 } else { 266 eh->ether_type = htons(ETHERTYPE_AT); 267 } 268 if (!aarpresolve(ac, m, (struct sockaddr_at *)dst, edst)) 269 return (0); 270 break; 271 } 272 #endif 273 #ifdef NS 274 case AF_NS: 275 switch(ns_nettype) { 276 default: 277 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */ 278 eh->ether_type = 0x8137; 279 break; 280 case 0x0: /* Novell 802.3 */ 281 eh->ether_type = htons(m->m_pkthdr.len); 282 break; 283 case 0xe0e0: /* Novell 802.2 and Token-Ring */ 284 M_PREPEND(m, 3, MB_DONTWAIT); 285 eh = mtod(m, struct ether_header *); 286 edst = eh->ether_dhost; 287 eh->ether_type = htons(m->m_pkthdr.len); 288 cp = mtod(m, u_char *) + sizeof(struct ether_header); 289 *cp++ = 0xE0; 290 *cp++ = 0xE0; 291 *cp++ = 0x03; 292 break; 293 } 294 bcopy(&(((struct sockaddr_ns *)dst)->sns_addr.x_host), edst, 295 ETHER_ADDR_LEN); 296 /* 297 * XXX if ns_thishost is the same as the node's ethernet 298 * address then just the default code will catch this anyhow. 299 * So I'm not sure if this next clause should be here at all? 300 * [JRE] 301 */ 302 if (bcmp(edst, &ns_thishost, ETHER_ADDR_LEN) == 0) { 303 m->m_pkthdr.rcvif = ifp; 304 netisr_dispatch(NETISR_NS, m); 305 return (error); 306 } 307 if (bcmp(edst, &ns_broadhost, ETHER_ADDR_LEN) == 0) 308 m->m_flags |= M_BCAST; 309 break; 310 #endif 311 #ifdef MPLS 312 case AF_MPLS: 313 { 314 struct sockaddr *sa_gw; 315 316 if (rt) 317 sa_gw = (struct sockaddr *)rt->rt_gateway; 318 else { 319 /* We realy need a gateway. */ 320 m_freem(m); 321 return (0); 322 } 323 324 switch (sa_gw->sa_family) { 325 case AF_INET: 326 if (!arpresolve(ifp, rt, m, sa_gw, edst)) 327 return (0); 328 break; 329 default: 330 kprintf("ether_output: address family not supported to forward mpls packets: %d.\n", sa_gw->sa_family); 331 m_freem(m); 332 return (0); 333 } 334 eh->ether_type = htons(ETHERTYPE_MPLS); /* XXX how about multicast? */ 335 break; 336 } 337 #endif 338 case pseudo_AF_HDRCMPLT: 339 case AF_UNSPEC: 340 loop_copy = -1; /* if this is for us, don't do it */ 341 deh = (struct ether_header *)dst->sa_data; 342 memcpy(edst, deh->ether_dhost, ETHER_ADDR_LEN); 343 eh->ether_type = deh->ether_type; 344 break; 345 346 default: 347 if_printf(ifp, "can't handle af%d\n", dst->sa_family); 348 gotoerr(EAFNOSUPPORT); 349 } 350 351 if (dst->sa_family == pseudo_AF_HDRCMPLT) /* unlikely */ 352 memcpy(eh->ether_shost, 353 ((struct ether_header *)dst->sa_data)->ether_shost, 354 ETHER_ADDR_LEN); 355 else 356 memcpy(eh->ether_shost, ac->ac_enaddr, ETHER_ADDR_LEN); 357 358 /* 359 * Bridges require special output handling. 360 */ 361 if (ifp->if_bridge) { 362 KASSERT(bridge_output_p != NULL, 363 ("%s: if_bridge not loaded!", __func__)); 364 return bridge_output_p(ifp, m); 365 } 366 367 /* 368 * If a simplex interface, and the packet is being sent to our 369 * Ethernet address or a broadcast address, loopback a copy. 370 * XXX To make a simplex device behave exactly like a duplex 371 * device, we should copy in the case of sending to our own 372 * ethernet address (thus letting the original actually appear 373 * on the wire). However, we don't do that here for security 374 * reasons and compatibility with the original behavior. 375 */ 376 if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) { 377 int csum_flags = 0; 378 379 if (m->m_pkthdr.csum_flags & CSUM_IP) 380 csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID); 381 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) 382 csum_flags |= (CSUM_DATA_VALID | CSUM_PSEUDO_HDR); 383 if ((m->m_flags & M_BCAST) || (loop_copy > 0)) { 384 struct mbuf *n; 385 386 if ((n = m_copypacket(m, MB_DONTWAIT)) != NULL) { 387 n->m_pkthdr.csum_flags |= csum_flags; 388 if (csum_flags & CSUM_DATA_VALID) 389 n->m_pkthdr.csum_data = 0xffff; 390 if_simloop(ifp, n, dst->sa_family, hlen); 391 } else 392 ifp->if_iqdrops++; 393 } else if (bcmp(eh->ether_dhost, eh->ether_shost, 394 ETHER_ADDR_LEN) == 0) { 395 m->m_pkthdr.csum_flags |= csum_flags; 396 if (csum_flags & CSUM_DATA_VALID) 397 m->m_pkthdr.csum_data = 0xffff; 398 if_simloop(ifp, m, dst->sa_family, hlen); 399 return (0); /* XXX */ 400 } 401 } 402 403 #ifdef CARP 404 if (ifp->if_carp && (error = carp_output(ifp, m, dst, NULL))) 405 goto bad; 406 #endif 407 408 409 /* Handle ng_ether(4) processing, if any */ 410 if (ng_ether_output_p != NULL) { 411 if ((error = (*ng_ether_output_p)(ifp, &m)) != 0) 412 goto bad; 413 if (m == NULL) 414 return (0); 415 } 416 417 /* Continue with link-layer output */ 418 return ether_output_frame(ifp, m); 419 420 bad: 421 m_freem(m); 422 return (error); 423 } 424 425 /* 426 * Ethernet link layer output routine to send a raw frame to the device. 427 * 428 * This assumes that the 14 byte Ethernet header is present and contiguous 429 * in the first mbuf. 430 */ 431 int 432 ether_output_frame(struct ifnet *ifp, struct mbuf *m) 433 { 434 struct ip_fw *rule = NULL; 435 int error = 0; 436 struct altq_pktattr pktattr; 437 struct m_tag *mtag; 438 439 ASSERT_NOT_SERIALIZED(ifp->if_serializer); 440 441 /* Extract info from dummynet tag */ 442 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL); 443 if (mtag != NULL) { 444 rule = ((struct dn_pkt *)m_tag_data(mtag))->dn_priv; 445 446 m_tag_delete(m, mtag); 447 mtag = NULL; 448 } 449 450 if (ifq_is_enabled(&ifp->if_snd)) 451 altq_etherclassify(&ifp->if_snd, m, &pktattr); 452 crit_enter(); 453 if (IPFW_LOADED && ether_ipfw != 0) { 454 struct ether_header save_eh, *eh; 455 456 eh = mtod(m, struct ether_header *); 457 save_eh = *eh; 458 m_adj(m, ETHER_HDR_LEN); 459 if (!ether_ipfw_chk(&m, ifp, &rule, eh)) { 460 crit_exit(); 461 if (m != NULL) { 462 m_freem(m); 463 return ENOBUFS; /* pkt dropped */ 464 } else 465 return 0; /* consumed e.g. in a pipe */ 466 } 467 468 /* packet was ok, restore the ethernet header */ 469 ether_restore_header(&m, eh, &save_eh); 470 if (m == NULL) { 471 crit_exit(); 472 return ENOBUFS; 473 } 474 } 475 crit_exit(); 476 477 /* 478 * Queue message on interface, update output statistics if 479 * successful, and start output if interface not yet active. 480 */ 481 error = ifq_dispatch(ifp, m, &pktattr); 482 return (error); 483 } 484 485 /* 486 * ipfw processing for ethernet packets (in and out). 487 * The second parameter is NULL from ether_demux(), and ifp from 488 * ether_output_frame(). 489 */ 490 static boolean_t 491 ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst, struct ip_fw **rule, 492 const struct ether_header *eh) 493 { 494 struct ether_header save_eh = *eh; /* might be a ptr in m */ 495 struct ip_fw_args args; 496 struct m_tag *mtag; 497 int i; 498 499 if (*rule != NULL && fw_one_pass) 500 return TRUE; /* dummynet packet, already partially processed */ 501 502 /* 503 * I need some amount of data to be contiguous. 504 */ 505 i = min((*m0)->m_pkthdr.len, max_protohdr); 506 if ((*m0)->m_len < i) { 507 *m0 = m_pullup(*m0, i); 508 if (*m0 == NULL) 509 return FALSE; 510 } 511 512 args.m = *m0; /* the packet we are looking at */ 513 args.oif = dst; /* destination, if any */ 514 if ((mtag = m_tag_find(*m0, PACKET_TAG_IPFW_DIVERT, NULL)) != NULL) 515 m_tag_delete(*m0, mtag); 516 args.rule = *rule; /* matching rule to restart */ 517 args.next_hop = NULL; /* we do not support forward yet */ 518 args.eh = &save_eh; /* MAC header for bridged/MAC packets */ 519 i = ip_fw_chk_ptr(&args); 520 *m0 = args.m; 521 *rule = args.rule; 522 523 if ((i & IP_FW_PORT_DENY_FLAG) || *m0 == NULL) /* drop */ 524 return FALSE; 525 526 if (i == 0) /* a PASS rule. */ 527 return TRUE; 528 529 if (i & IP_FW_PORT_DYNT_FLAG) { 530 /* 531 * Pass the pkt to dummynet, which consumes it. 532 */ 533 struct mbuf *m; 534 535 m = *m0; /* pass the original to dummynet */ 536 *m0 = NULL; /* and nothing back to the caller */ 537 538 ether_restore_header(&m, eh, &save_eh); 539 if (m == NULL) 540 return FALSE; 541 542 ip_fw_dn_io_ptr(m, (i & 0xffff), 543 dst ? DN_TO_ETH_OUT: DN_TO_ETH_DEMUX, &args); 544 return FALSE; 545 } 546 /* 547 * XXX at some point add support for divert/forward actions. 548 * If none of the above matches, we have to drop the pkt. 549 */ 550 return FALSE; 551 } 552 553 /* 554 * Process a received Ethernet packet. 555 * 556 * The ethernet header is assumed to be in the mbuf so the caller 557 * MUST MAKE SURE that there are at least sizeof(struct ether_header) 558 * bytes in the first mbuf. 559 * 560 * This allows us to concentrate in one place a bunch of code which 561 * is replicated in all device drivers. Also, many functions called 562 * from ether_input() try to put the eh back into the mbuf, so we 563 * can later propagate the 'contiguous packet' interface to them. 564 * 565 * NOTA BENE: for all drivers "eh" is a pointer into the first mbuf or 566 * cluster, right before m_data. So be very careful when working on m, 567 * as you could destroy *eh !! 568 * 569 * First we perform any link layer operations, then continue to the 570 * upper layers with ether_demux(). 571 */ 572 void 573 ether_input_chain(struct ifnet *ifp, struct mbuf *m, struct mbuf_chain *chain) 574 { 575 struct ether_header *eh; 576 577 ASSERT_SERIALIZED(ifp->if_serializer); 578 M_ASSERTPKTHDR(m); 579 580 /* Discard packet if interface is not up */ 581 if (!(ifp->if_flags & IFF_UP)) { 582 m_freem(m); 583 return; 584 } 585 586 if (m->m_len < sizeof(struct ether_header)) { 587 /* XXX error in the caller. */ 588 m_freem(m); 589 return; 590 } 591 eh = mtod(m, struct ether_header *); 592 593 if (ntohs(eh->ether_type) == ETHERTYPE_VLAN && 594 (m->m_flags & M_VLANTAG) == 0) { 595 /* 596 * Extract vlan tag if hardware does not do it for us 597 */ 598 vlan_ether_decap(&m); 599 if (m == NULL) 600 return; 601 eh = mtod(m, struct ether_header *); 602 } 603 604 m->m_pkthdr.rcvif = ifp; 605 606 if (ETHER_IS_MULTICAST(eh->ether_dhost)) { 607 if (bcmp(ifp->if_broadcastaddr, eh->ether_dhost, 608 ifp->if_addrlen) == 0) 609 m->m_flags |= M_BCAST; 610 else 611 m->m_flags |= M_MCAST; 612 ifp->if_imcasts++; 613 } 614 615 ETHER_BPF_MTAP(ifp, m); 616 617 ifp->if_ibytes += m->m_pkthdr.len; 618 619 if (ifp->if_flags & IFF_MONITOR) { 620 /* 621 * Interface marked for monitoring; discard packet. 622 */ 623 m_freem(m); 624 return; 625 } 626 627 /* 628 * Tap the packet off here for a bridge. bridge_input() 629 * will return NULL if it has consumed the packet, otherwise 630 * it gets processed as normal. Note that bridge_input() 631 * will always return the original packet if we need to 632 * process it locally. 633 */ 634 if (ifp->if_bridge) { 635 KASSERT(bridge_input_p != NULL, 636 ("%s: if_bridge not loaded!", __func__)); 637 638 if(m->m_flags & M_PROTO1) { 639 m->m_flags &= ~M_PROTO1; 640 } else { 641 /* clear M_PROMISC, in case the packets comes from a vlan */ 642 /* m->m_flags &= ~M_PROMISC; */ 643 lwkt_serialize_exit(ifp->if_serializer); 644 m = bridge_input_p(ifp, m); 645 lwkt_serialize_enter(ifp->if_serializer); 646 if (m == NULL) 647 return; 648 649 KASSERT(ifp == m->m_pkthdr.rcvif, 650 ("bridge_input_p changed rcvif\n")); 651 652 /* 'm' may be changed by bridge_input_p() */ 653 eh = mtod(m, struct ether_header *); 654 } 655 } 656 657 /* Handle ng_ether(4) processing, if any */ 658 if (ng_ether_input_p != NULL) { 659 ng_ether_input_p(ifp, &m); 660 if (m == NULL) 661 return; 662 663 /* 'm' may be changed by ng_ether_input_p() */ 664 eh = mtod(m, struct ether_header *); 665 } 666 667 /* Continue with upper layer processing */ 668 ether_demux_chain(ifp, m, chain); 669 } 670 671 void 672 ether_input(struct ifnet *ifp, struct mbuf *m) 673 { 674 ether_input_chain(ifp, m, NULL); 675 } 676 677 /* 678 * Upper layer processing for a received Ethernet packet. 679 */ 680 static void 681 ether_demux_chain(struct ifnet *ifp, struct mbuf *m, struct mbuf_chain *chain) 682 { 683 struct ether_header save_eh, *eh; 684 int isr; 685 u_short ether_type; 686 struct ip_fw *rule = NULL; 687 struct m_tag *mtag; 688 #ifdef NETATALK 689 struct llc *l; 690 #endif 691 692 M_ASSERTPKTHDR(m); 693 KASSERT(m->m_len >= ETHER_HDR_LEN, 694 ("ether header is no contiguous!\n")); 695 696 eh = mtod(m, struct ether_header *); 697 save_eh = *eh; 698 699 /* XXX old crufty stuff, needs to be removed */ 700 m_adj(m, sizeof(struct ether_header)); 701 702 /* Extract info from dummynet tag */ 703 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL); 704 if (mtag != NULL) { 705 rule = ((struct dn_pkt *)m_tag_data(mtag))->dn_priv; 706 KKASSERT(ifp == NULL); 707 ifp = m->m_pkthdr.rcvif; 708 709 m_tag_delete(m, mtag); 710 mtag = NULL; 711 } 712 if (rule) /* packet is passing the second time */ 713 goto post_stats; 714 715 #ifdef CARP 716 /* 717 * XXX: Okay, we need to call carp_forus() and - if it is for 718 * us jump over code that does the normal check 719 * "ac_enaddr == ether_dhost". The check sequence is a bit 720 * different from OpenBSD, so we jump over as few code as 721 * possible, to catch _all_ sanity checks. This needs 722 * evaluation, to see if the carp ether_dhost values break any 723 * of these checks! 724 */ 725 if (ifp->if_carp && carp_forus(ifp->if_carp, eh->ether_dhost)) 726 goto post_stats; 727 #endif 728 729 /* 730 * Discard packet if upper layers shouldn't see it because 731 * it was unicast to a different Ethernet address. If the 732 * driver is working properly, then this situation can only 733 * happen when the interface is in promiscuous mode. 734 */ 735 if (((ifp->if_flags & (IFF_PROMISC | IFF_PPROMISC)) == IFF_PROMISC) && 736 (eh->ether_dhost[0] & 1) == 0 && 737 bcmp(eh->ether_dhost, IFP2AC(ifp)->ac_enaddr, ETHER_ADDR_LEN)) { 738 m_freem(m); 739 return; 740 } 741 742 post_stats: 743 if (IPFW_LOADED && ether_ipfw != 0) { 744 if (!ether_ipfw_chk(&m, NULL, &rule, eh)) { 745 m_freem(m); 746 return; 747 } 748 } 749 750 ether_type = ntohs(save_eh.ether_type); 751 752 if (m->m_flags & M_VLANTAG) { 753 if (ether_type == ETHERTYPE_VLAN) { 754 /* 755 * To prevent possible dangerous recursion, 756 * we don't do vlan-in-vlan 757 */ 758 m->m_pkthdr.rcvif->if_noproto++; 759 m_freem(m); 760 return; 761 } 762 763 if (vlan_input_p != NULL) { 764 ether_restore_header(&m, eh, &save_eh); 765 if (m != NULL) 766 vlan_input_p(m, chain); 767 } else { 768 m->m_pkthdr.rcvif->if_noproto++; 769 m_freem(m); 770 } 771 return; 772 } 773 KKASSERT(ether_type != ETHERTYPE_VLAN); 774 775 switch (ether_type) { 776 #ifdef INET 777 case ETHERTYPE_IP: 778 if (ipflow_fastforward(m, ifp->if_serializer)) 779 return; 780 isr = NETISR_IP; 781 break; 782 783 case ETHERTYPE_ARP: 784 if (ifp->if_flags & IFF_NOARP) { 785 /* Discard packet if ARP is disabled on interface */ 786 m_freem(m); 787 return; 788 } 789 isr = NETISR_ARP; 790 break; 791 #endif 792 793 #ifdef INET6 794 case ETHERTYPE_IPV6: 795 isr = NETISR_IPV6; 796 break; 797 #endif 798 799 #ifdef IPX 800 case ETHERTYPE_IPX: 801 if (ef_inputp && ef_inputp(ifp, &save_eh, m) == 0) 802 return; 803 isr = NETISR_IPX; 804 break; 805 #endif 806 807 #ifdef NS 808 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */ 809 isr = NETISR_NS; 810 break; 811 812 #endif 813 814 #ifdef NETATALK 815 case ETHERTYPE_AT: 816 isr = NETISR_ATALK1; 817 break; 818 case ETHERTYPE_AARP: 819 isr = NETISR_AARP; 820 break; 821 #endif 822 823 #ifdef MPLS 824 case ETHERTYPE_MPLS: 825 case ETHERTYPE_MPLS_MCAST: 826 isr = NETISR_MPLS; 827 break; 828 #endif 829 830 default: 831 #ifdef IPX 832 if (ef_inputp && ef_inputp(ifp, &save_eh, m) == 0) 833 return; 834 #endif 835 #ifdef NS 836 checksum = mtod(m, ushort *); 837 /* Novell 802.3 */ 838 if ((ether_type <= ETHERMTU) && 839 ((*checksum == 0xffff) || (*checksum == 0xE0E0))) { 840 if (*checksum == 0xE0E0) { 841 m->m_pkthdr.len -= 3; 842 m->m_len -= 3; 843 m->m_data += 3; 844 } 845 isr = NETISR_NS; 846 break; 847 } 848 #endif 849 #ifdef NETATALK 850 if (ether_type > ETHERMTU) 851 goto dropanyway; 852 l = mtod(m, struct llc *); 853 if (l->llc_dsap == LLC_SNAP_LSAP && 854 l->llc_ssap == LLC_SNAP_LSAP && 855 l->llc_control == LLC_UI) { 856 if (bcmp(&(l->llc_snap_org_code)[0], at_org_code, 857 sizeof at_org_code) == 0 && 858 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) { 859 m_adj(m, sizeof(struct llc)); 860 isr = NETISR_ATALK2; 861 break; 862 } 863 if (bcmp(&(l->llc_snap_org_code)[0], aarp_org_code, 864 sizeof aarp_org_code) == 0 && 865 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) { 866 m_adj(m, sizeof(struct llc)); 867 isr = NETISR_AARP; 868 break; 869 } 870 } 871 dropanyway: 872 #endif 873 if (ng_ether_input_orphan_p != NULL) 874 (*ng_ether_input_orphan_p)(ifp, m, &save_eh); 875 else 876 m_freem(m); 877 return; 878 } 879 880 #ifdef ETHER_INPUT_CHAIN 881 if (chain != NULL) { 882 struct mbuf_chain *c; 883 lwkt_port_t port; 884 int cpuid; 885 886 port = netisr_mport(isr, &m); 887 if (port == NULL) 888 return; 889 890 m->m_pkthdr.header = port; /* XXX */ 891 cpuid = port->mpu_td->td_gd->gd_cpuid; 892 893 c = &chain[cpuid]; 894 if (c->mc_head == NULL) { 895 c->mc_head = c->mc_tail = m; 896 } else { 897 c->mc_tail->m_nextpkt = m; 898 c->mc_tail = m; 899 } 900 m->m_nextpkt = NULL; 901 } else 902 #endif /* ETHER_INPUT_CHAIN */ 903 netisr_dispatch(isr, m); 904 } 905 906 void 907 ether_demux(struct ifnet *ifp, struct mbuf *m) 908 { 909 ether_demux_chain(ifp, m, NULL); 910 } 911 912 /* 913 * Perform common duties while attaching to interface list 914 */ 915 916 void 917 ether_ifattach(struct ifnet *ifp, uint8_t *lla, lwkt_serialize_t serializer) 918 { 919 ether_ifattach_bpf(ifp, lla, DLT_EN10MB, sizeof(struct ether_header), 920 serializer); 921 } 922 923 void 924 ether_ifattach_bpf(struct ifnet *ifp, uint8_t *lla, u_int dlt, u_int hdrlen, 925 lwkt_serialize_t serializer) 926 { 927 struct sockaddr_dl *sdl; 928 929 ifp->if_type = IFT_ETHER; 930 ifp->if_addrlen = ETHER_ADDR_LEN; 931 ifp->if_hdrlen = ETHER_HDR_LEN; 932 if_attach(ifp, serializer); 933 ifp->if_mtu = ETHERMTU; 934 if (ifp->if_baudrate == 0) 935 ifp->if_baudrate = 10000000; 936 ifp->if_output = ether_output; 937 ifp->if_input = ether_input; 938 ifp->if_resolvemulti = ether_resolvemulti; 939 ifp->if_broadcastaddr = etherbroadcastaddr; 940 sdl = IF_LLSOCKADDR(ifp); 941 sdl->sdl_type = IFT_ETHER; 942 sdl->sdl_alen = ifp->if_addrlen; 943 bcopy(lla, LLADDR(sdl), ifp->if_addrlen); 944 /* 945 * XXX Keep the current drivers happy. 946 * XXX Remove once all drivers have been cleaned up 947 */ 948 if (lla != IFP2AC(ifp)->ac_enaddr) 949 bcopy(lla, IFP2AC(ifp)->ac_enaddr, ifp->if_addrlen); 950 bpfattach(ifp, dlt, hdrlen); 951 if (ng_ether_attach_p != NULL) 952 (*ng_ether_attach_p)(ifp); 953 954 if_printf(ifp, "MAC address: %6D\n", lla, ":"); 955 } 956 957 /* 958 * Perform common duties while detaching an Ethernet interface 959 */ 960 void 961 ether_ifdetach(struct ifnet *ifp) 962 { 963 if_down(ifp); 964 965 if (ng_ether_detach_p != NULL) 966 (*ng_ether_detach_p)(ifp); 967 bpfdetach(ifp); 968 if_detach(ifp); 969 } 970 971 int 972 ether_ioctl(struct ifnet *ifp, int command, caddr_t data) 973 { 974 struct ifaddr *ifa = (struct ifaddr *) data; 975 struct ifreq *ifr = (struct ifreq *) data; 976 int error = 0; 977 978 #define IF_INIT(ifp) \ 979 do { \ 980 if (((ifp)->if_flags & IFF_UP) == 0) { \ 981 (ifp)->if_flags |= IFF_UP; \ 982 (ifp)->if_init((ifp)->if_softc); \ 983 } \ 984 } while (0) 985 986 ASSERT_SERIALIZED(ifp->if_serializer); 987 988 switch (command) { 989 case SIOCSIFADDR: 990 switch (ifa->ifa_addr->sa_family) { 991 #ifdef INET 992 case AF_INET: 993 IF_INIT(ifp); /* before arpwhohas */ 994 arp_ifinit(ifp, ifa); 995 break; 996 #endif 997 #ifdef IPX 998 /* 999 * XXX - This code is probably wrong 1000 */ 1001 case AF_IPX: 1002 { 1003 struct ipx_addr *ina = &IA_SIPX(ifa)->sipx_addr; 1004 struct arpcom *ac = IFP2AC(ifp); 1005 1006 if (ipx_nullhost(*ina)) 1007 ina->x_host = *(union ipx_host *) ac->ac_enaddr; 1008 else 1009 bcopy(ina->x_host.c_host, ac->ac_enaddr, 1010 sizeof ac->ac_enaddr); 1011 1012 IF_INIT(ifp); /* Set new address. */ 1013 break; 1014 } 1015 #endif 1016 #ifdef NS 1017 /* 1018 * XXX - This code is probably wrong 1019 */ 1020 case AF_NS: 1021 { 1022 struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr); 1023 struct arpcom *ac = IFP2AC(ifp); 1024 1025 if (ns_nullhost(*ina)) 1026 ina->x_host = *(union ns_host *)(ac->ac_enaddr); 1027 else 1028 bcopy(ina->x_host.c_host, ac->ac_enaddr, 1029 sizeof ac->ac_enaddr); 1030 1031 /* 1032 * Set new address 1033 */ 1034 IF_INIT(ifp); 1035 break; 1036 } 1037 #endif 1038 default: 1039 IF_INIT(ifp); 1040 break; 1041 } 1042 break; 1043 1044 case SIOCGIFADDR: 1045 bcopy(IFP2AC(ifp)->ac_enaddr, 1046 ((struct sockaddr *)ifr->ifr_data)->sa_data, 1047 ETHER_ADDR_LEN); 1048 break; 1049 1050 case SIOCSIFMTU: 1051 /* 1052 * Set the interface MTU. 1053 */ 1054 if (ifr->ifr_mtu > ETHERMTU) { 1055 error = EINVAL; 1056 } else { 1057 ifp->if_mtu = ifr->ifr_mtu; 1058 } 1059 break; 1060 default: 1061 error = EINVAL; 1062 break; 1063 } 1064 return (error); 1065 1066 #undef IF_INIT 1067 } 1068 1069 int 1070 ether_resolvemulti( 1071 struct ifnet *ifp, 1072 struct sockaddr **llsa, 1073 struct sockaddr *sa) 1074 { 1075 struct sockaddr_dl *sdl; 1076 struct sockaddr_in *sin; 1077 #ifdef INET6 1078 struct sockaddr_in6 *sin6; 1079 #endif 1080 u_char *e_addr; 1081 1082 switch(sa->sa_family) { 1083 case AF_LINK: 1084 /* 1085 * No mapping needed. Just check that it's a valid MC address. 1086 */ 1087 sdl = (struct sockaddr_dl *)sa; 1088 e_addr = LLADDR(sdl); 1089 if ((e_addr[0] & 1) != 1) 1090 return EADDRNOTAVAIL; 1091 *llsa = 0; 1092 return 0; 1093 1094 #ifdef INET 1095 case AF_INET: 1096 sin = (struct sockaddr_in *)sa; 1097 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) 1098 return EADDRNOTAVAIL; 1099 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR, 1100 M_WAITOK | M_ZERO); 1101 sdl->sdl_len = sizeof *sdl; 1102 sdl->sdl_family = AF_LINK; 1103 sdl->sdl_index = ifp->if_index; 1104 sdl->sdl_type = IFT_ETHER; 1105 sdl->sdl_alen = ETHER_ADDR_LEN; 1106 e_addr = LLADDR(sdl); 1107 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr); 1108 *llsa = (struct sockaddr *)sdl; 1109 return 0; 1110 #endif 1111 #ifdef INET6 1112 case AF_INET6: 1113 sin6 = (struct sockaddr_in6 *)sa; 1114 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 1115 /* 1116 * An IP6 address of 0 means listen to all 1117 * of the Ethernet multicast address used for IP6. 1118 * (This is used for multicast routers.) 1119 */ 1120 ifp->if_flags |= IFF_ALLMULTI; 1121 *llsa = 0; 1122 return 0; 1123 } 1124 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) 1125 return EADDRNOTAVAIL; 1126 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR, 1127 M_WAITOK | M_ZERO); 1128 sdl->sdl_len = sizeof *sdl; 1129 sdl->sdl_family = AF_LINK; 1130 sdl->sdl_index = ifp->if_index; 1131 sdl->sdl_type = IFT_ETHER; 1132 sdl->sdl_alen = ETHER_ADDR_LEN; 1133 e_addr = LLADDR(sdl); 1134 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr); 1135 *llsa = (struct sockaddr *)sdl; 1136 return 0; 1137 #endif 1138 1139 default: 1140 /* 1141 * Well, the text isn't quite right, but it's the name 1142 * that counts... 1143 */ 1144 return EAFNOSUPPORT; 1145 } 1146 } 1147 1148 #if 0 1149 /* 1150 * This is for reference. We have a table-driven version 1151 * of the little-endian crc32 generator, which is faster 1152 * than the double-loop. 1153 */ 1154 uint32_t 1155 ether_crc32_le(const uint8_t *buf, size_t len) 1156 { 1157 uint32_t c, crc, carry; 1158 size_t i, j; 1159 1160 crc = 0xffffffffU; /* initial value */ 1161 1162 for (i = 0; i < len; i++) { 1163 c = buf[i]; 1164 for (j = 0; j < 8; j++) { 1165 carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01); 1166 crc >>= 1; 1167 c >>= 1; 1168 if (carry) 1169 crc = (crc ^ ETHER_CRC_POLY_LE); 1170 } 1171 } 1172 1173 return (crc); 1174 } 1175 #else 1176 uint32_t 1177 ether_crc32_le(const uint8_t *buf, size_t len) 1178 { 1179 static const uint32_t crctab[] = { 1180 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac, 1181 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c, 1182 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c, 1183 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c 1184 }; 1185 uint32_t crc; 1186 size_t i; 1187 1188 crc = 0xffffffffU; /* initial value */ 1189 1190 for (i = 0; i < len; i++) { 1191 crc ^= buf[i]; 1192 crc = (crc >> 4) ^ crctab[crc & 0xf]; 1193 crc = (crc >> 4) ^ crctab[crc & 0xf]; 1194 } 1195 1196 return (crc); 1197 } 1198 #endif 1199 1200 uint32_t 1201 ether_crc32_be(const uint8_t *buf, size_t len) 1202 { 1203 uint32_t c, crc, carry; 1204 size_t i, j; 1205 1206 crc = 0xffffffffU; /* initial value */ 1207 1208 for (i = 0; i < len; i++) { 1209 c = buf[i]; 1210 for (j = 0; j < 8; j++) { 1211 carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01); 1212 crc <<= 1; 1213 c >>= 1; 1214 if (carry) 1215 crc = (crc ^ ETHER_CRC_POLY_BE) | carry; 1216 } 1217 } 1218 1219 return (crc); 1220 } 1221 1222 /* 1223 * find the size of ethernet header, and call classifier 1224 */ 1225 void 1226 altq_etherclassify(struct ifaltq *ifq, struct mbuf *m, 1227 struct altq_pktattr *pktattr) 1228 { 1229 struct ether_header *eh; 1230 uint16_t ether_type; 1231 int hlen, af, hdrsize; 1232 caddr_t hdr; 1233 1234 hlen = sizeof(struct ether_header); 1235 eh = mtod(m, struct ether_header *); 1236 1237 ether_type = ntohs(eh->ether_type); 1238 if (ether_type < ETHERMTU) { 1239 /* ick! LLC/SNAP */ 1240 struct llc *llc = (struct llc *)(eh + 1); 1241 hlen += 8; 1242 1243 if (m->m_len < hlen || 1244 llc->llc_dsap != LLC_SNAP_LSAP || 1245 llc->llc_ssap != LLC_SNAP_LSAP || 1246 llc->llc_control != LLC_UI) 1247 goto bad; /* not snap! */ 1248 1249 ether_type = ntohs(llc->llc_un.type_snap.ether_type); 1250 } 1251 1252 if (ether_type == ETHERTYPE_IP) { 1253 af = AF_INET; 1254 hdrsize = 20; /* sizeof(struct ip) */ 1255 #ifdef INET6 1256 } else if (ether_type == ETHERTYPE_IPV6) { 1257 af = AF_INET6; 1258 hdrsize = 40; /* sizeof(struct ip6_hdr) */ 1259 #endif 1260 } else 1261 goto bad; 1262 1263 while (m->m_len <= hlen) { 1264 hlen -= m->m_len; 1265 m = m->m_next; 1266 } 1267 hdr = m->m_data + hlen; 1268 if (m->m_len < hlen + hdrsize) { 1269 /* 1270 * ip header is not in a single mbuf. this should not 1271 * happen in the current code. 1272 * (todo: use m_pulldown in the future) 1273 */ 1274 goto bad; 1275 } 1276 m->m_data += hlen; 1277 m->m_len -= hlen; 1278 ifq_classify(ifq, m, af, pktattr); 1279 m->m_data -= hlen; 1280 m->m_len += hlen; 1281 1282 return; 1283 1284 bad: 1285 pktattr->pattr_class = NULL; 1286 pktattr->pattr_hdr = NULL; 1287 pktattr->pattr_af = AF_UNSPEC; 1288 } 1289 1290 static void 1291 ether_restore_header(struct mbuf **m0, const struct ether_header *eh, 1292 const struct ether_header *save_eh) 1293 { 1294 struct mbuf *m = *m0; 1295 1296 ether_restore_hdr++; 1297 1298 /* 1299 * Prepend the header, optimize for the common case of 1300 * eh pointing into the mbuf. 1301 */ 1302 if ((const void *)(eh + 1) == (void *)m->m_data) { 1303 m->m_data -= ETHER_HDR_LEN; 1304 m->m_len += ETHER_HDR_LEN; 1305 m->m_pkthdr.len += ETHER_HDR_LEN; 1306 } else { 1307 ether_prepend_hdr++; 1308 1309 M_PREPEND(m, ETHER_HDR_LEN, MB_DONTWAIT); 1310 if (m != NULL) { 1311 bcopy(save_eh, mtod(m, struct ether_header *), 1312 ETHER_HDR_LEN); 1313 } 1314 } 1315 *m0 = m; 1316 } 1317 1318 #ifdef ETHER_INPUT_CHAIN 1319 1320 static void 1321 ether_input_ipifunc(void *arg) 1322 { 1323 struct mbuf *m, *next; 1324 lwkt_port_t port; 1325 1326 m = arg; 1327 do { 1328 next = m->m_nextpkt; 1329 m->m_nextpkt = NULL; 1330 1331 port = m->m_pkthdr.header; 1332 m->m_pkthdr.header = NULL; 1333 1334 lwkt_sendmsg(port, 1335 &m->m_hdr.mh_netmsg.nm_netmsg.nm_lmsg); 1336 1337 m = next; 1338 } while (m != NULL); 1339 } 1340 1341 void 1342 ether_input_dispatch(struct mbuf_chain *chain) 1343 { 1344 #ifdef SMP 1345 int i; 1346 1347 for (i = 0; i < ncpus; ++i) { 1348 if (chain[i].mc_head != NULL) { 1349 lwkt_send_ipiq(globaldata_find(i), 1350 ether_input_ipifunc, chain[i].mc_head); 1351 } 1352 } 1353 #else 1354 if (chain->mc_head != NULL) 1355 ether_input_ipifunc(chain->mc_head); 1356 #endif 1357 } 1358 1359 void 1360 ether_input_chain_init(struct mbuf_chain *chain) 1361 { 1362 #ifdef SMP 1363 int i; 1364 1365 for (i = 0; i < ncpus; ++i) 1366 chain[i].mc_head = chain[i].mc_tail = NULL; 1367 #else 1368 chain->mc_head = chain->mc_tail = NULL; 1369 #endif 1370 } 1371 1372 #endif /* ETHER_INPUT_CHAIN */ 1373 1374 #ifdef ETHER_INPUT2 1375 1376 static void 1377 ether_demux_oncpu(struct ifnet *ifp, struct mbuf *m) 1378 { 1379 struct ether_header *eh; 1380 int isr, redispatch; 1381 u_short ether_type; 1382 struct ip_fw *rule = NULL; 1383 struct m_tag *mtag; 1384 #ifdef NETATALK 1385 struct llc *l; 1386 #endif 1387 1388 M_ASSERTPKTHDR(m); 1389 KASSERT(m->m_len >= ETHER_HDR_LEN, 1390 ("ether header is no contiguous!\n")); 1391 1392 eh = mtod(m, struct ether_header *); 1393 1394 /* Extract info from dummynet tag */ 1395 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL); 1396 if (mtag != NULL) { 1397 rule = ((struct dn_pkt *)m_tag_data(mtag))->dn_priv; 1398 KKASSERT(ifp == NULL); 1399 ifp = m->m_pkthdr.rcvif; 1400 1401 m_tag_delete(m, mtag); 1402 mtag = NULL; 1403 } 1404 if (rule) /* packet is passing the second time */ 1405 goto post_stats; 1406 1407 #ifdef CARP 1408 /* 1409 * XXX: Okay, we need to call carp_forus() and - if it is for 1410 * us jump over code that does the normal check 1411 * "ac_enaddr == ether_dhost". The check sequence is a bit 1412 * different from OpenBSD, so we jump over as few code as 1413 * possible, to catch _all_ sanity checks. This needs 1414 * evaluation, to see if the carp ether_dhost values break any 1415 * of these checks! 1416 */ 1417 if (ifp->if_carp && carp_forus(ifp->if_carp, eh->ether_dhost)) 1418 goto post_stats; 1419 #endif 1420 1421 /* 1422 * Discard packet if upper layers shouldn't see it because 1423 * it was unicast to a different Ethernet address. If the 1424 * driver is working properly, then this situation can only 1425 * happen when the interface is in promiscuous mode. 1426 */ 1427 if (((ifp->if_flags & (IFF_PROMISC | IFF_PPROMISC)) == IFF_PROMISC) && 1428 (eh->ether_dhost[0] & 1) == 0 && 1429 bcmp(eh->ether_dhost, IFP2AC(ifp)->ac_enaddr, ETHER_ADDR_LEN)) { 1430 m_freem(m); 1431 return; 1432 } 1433 1434 post_stats: 1435 if (IPFW_LOADED && ether_ipfw != 0) { 1436 struct ether_header save_eh = *eh; 1437 1438 /* XXX old crufty stuff, needs to be removed */ 1439 m_adj(m, sizeof(struct ether_header)); 1440 1441 if (!ether_ipfw_chk(&m, NULL, &rule, eh)) { 1442 m_freem(m); 1443 return; 1444 } 1445 1446 ether_restore_header(&m, eh, &save_eh); 1447 if (m == NULL) 1448 return; 1449 eh = mtod(m, struct ether_header *); 1450 } 1451 1452 ether_type = ntohs(eh->ether_type); 1453 KKASSERT(ether_type != ETHERTYPE_VLAN); 1454 1455 if (m->m_flags & M_VLANTAG) { 1456 if (vlan_input2_p != NULL) { 1457 vlan_input2_p(m); 1458 } else { 1459 m->m_pkthdr.rcvif->if_noproto++; 1460 m_freem(m); 1461 } 1462 return; 1463 } 1464 1465 m_adj(m, sizeof(struct ether_header)); 1466 redispatch = 0; 1467 1468 switch (ether_type) { 1469 #ifdef INET 1470 case ETHERTYPE_IP: 1471 #ifdef notyet 1472 if (ipflow_fastforward(m, ifp->if_serializer)) 1473 return; 1474 #endif 1475 isr = NETISR_IP; 1476 break; 1477 1478 case ETHERTYPE_ARP: 1479 if (ifp->if_flags & IFF_NOARP) { 1480 /* Discard packet if ARP is disabled on interface */ 1481 m_freem(m); 1482 return; 1483 } 1484 isr = NETISR_ARP; 1485 break; 1486 #endif 1487 1488 #ifdef INET6 1489 case ETHERTYPE_IPV6: 1490 isr = NETISR_IPV6; 1491 break; 1492 #endif 1493 1494 #ifdef IPX 1495 case ETHERTYPE_IPX: 1496 if (ef_inputp && ef_inputp(ifp, eh, m) == 0) 1497 return; 1498 isr = NETISR_IPX; 1499 break; 1500 #endif 1501 1502 #ifdef NS 1503 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */ 1504 isr = NETISR_NS; 1505 break; 1506 1507 #endif 1508 1509 #ifdef NETATALK 1510 case ETHERTYPE_AT: 1511 isr = NETISR_ATALK1; 1512 break; 1513 case ETHERTYPE_AARP: 1514 isr = NETISR_AARP; 1515 break; 1516 #endif 1517 1518 default: 1519 /* 1520 * The accurate msgport is not determined before 1521 * we reach here, so redo the dispatching 1522 */ 1523 redispatch = 1; 1524 #ifdef IPX 1525 if (ef_inputp && ef_inputp(ifp, eh, m) == 0) 1526 return; 1527 #endif 1528 #ifdef NS 1529 checksum = mtod(m, ushort *); 1530 /* Novell 802.3 */ 1531 if ((ether_type <= ETHERMTU) && 1532 ((*checksum == 0xffff) || (*checksum == 0xE0E0))) { 1533 if (*checksum == 0xE0E0) { 1534 m->m_pkthdr.len -= 3; 1535 m->m_len -= 3; 1536 m->m_data += 3; 1537 } 1538 isr = NETISR_NS; 1539 break; 1540 } 1541 #endif 1542 #ifdef NETATALK 1543 if (ether_type > ETHERMTU) 1544 goto dropanyway; 1545 l = mtod(m, struct llc *); 1546 if (l->llc_dsap == LLC_SNAP_LSAP && 1547 l->llc_ssap == LLC_SNAP_LSAP && 1548 l->llc_control == LLC_UI) { 1549 if (bcmp(&(l->llc_snap_org_code)[0], at_org_code, 1550 sizeof at_org_code) == 0 && 1551 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) { 1552 m_adj(m, sizeof(struct llc)); 1553 isr = NETISR_ATALK2; 1554 break; 1555 } 1556 if (bcmp(&(l->llc_snap_org_code)[0], aarp_org_code, 1557 sizeof aarp_org_code) == 0 && 1558 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) { 1559 m_adj(m, sizeof(struct llc)); 1560 isr = NETISR_AARP; 1561 break; 1562 } 1563 } 1564 dropanyway: 1565 #endif 1566 if (ng_ether_input_orphan_p != NULL) 1567 ng_ether_input_orphan_p(ifp, m, eh); 1568 else 1569 m_freem(m); 1570 return; 1571 } 1572 1573 if (!redispatch) 1574 netisr_run(isr, m); 1575 else 1576 netisr_dispatch(isr, m); 1577 } 1578 1579 void 1580 ether_input_oncpu(struct ifnet *ifp, struct mbuf *m) 1581 { 1582 if ((ifp->if_flags & (IFF_UP | IFF_MONITOR)) != IFF_UP) { 1583 /* 1584 * Receiving interface's flags are changed, when this 1585 * packet is waiting for processing; discard it. 1586 */ 1587 m_freem(m); 1588 return; 1589 } 1590 1591 /* 1592 * Tap the packet off here for a bridge. bridge_input() 1593 * will return NULL if it has consumed the packet, otherwise 1594 * it gets processed as normal. Note that bridge_input() 1595 * will always return the original packet if we need to 1596 * process it locally. 1597 */ 1598 if (ifp->if_bridge) { 1599 KASSERT(bridge_input_p != NULL, 1600 ("%s: if_bridge not loaded!", __func__)); 1601 1602 if(m->m_flags & M_PROTO1) { 1603 m->m_flags &= ~M_PROTO1; 1604 } else { 1605 /* clear M_PROMISC, in case the packets comes from a vlan */ 1606 /* m->m_flags &= ~M_PROMISC; */ 1607 m = bridge_input_p(ifp, m); 1608 if (m == NULL) 1609 return; 1610 1611 KASSERT(ifp == m->m_pkthdr.rcvif, 1612 ("bridge_input_p changed rcvif\n")); 1613 } 1614 } 1615 1616 /* Handle ng_ether(4) processing, if any */ 1617 if (ng_ether_input_p != NULL) { 1618 ng_ether_input_p(ifp, &m); 1619 if (m == NULL) 1620 return; 1621 } 1622 1623 /* Continue with upper layer processing */ 1624 ether_demux_oncpu(ifp, m); 1625 } 1626 1627 static void 1628 ether_input_handler(struct netmsg *nmsg) 1629 { 1630 struct netmsg_packet *nmp = (struct netmsg_packet *)nmsg; 1631 struct ifnet *ifp; 1632 struct mbuf *m; 1633 1634 m = nmp->nm_packet; 1635 M_ASSERTPKTHDR(m); 1636 ifp = m->m_pkthdr.rcvif; 1637 1638 ether_input_oncpu(ifp, m); 1639 } 1640 1641 static __inline void 1642 ether_init_netpacket(int num, struct mbuf *m) 1643 { 1644 struct netmsg_packet *pmsg; 1645 1646 pmsg = &m->m_hdr.mh_netmsg; 1647 netmsg_init(&pmsg->nm_netmsg, &netisr_apanic_rport, 0, 1648 ether_input_handler); 1649 pmsg->nm_packet = m; 1650 pmsg->nm_netmsg.nm_lmsg.u.ms_result = num; 1651 } 1652 1653 static __inline struct lwkt_port * 1654 ether_mport(int num, struct mbuf **m0) 1655 { 1656 struct lwkt_port *port; 1657 struct mbuf *m = *m0; 1658 1659 if (num == NETISR_MAX) { 1660 /* 1661 * All packets whose target msgports can't be 1662 * determined here are dispatched to netisr0, 1663 * where further dispatching may happen. 1664 */ 1665 return cpu_portfn(0); 1666 } 1667 1668 port = netisr_find_port(num, &m); 1669 if (port == NULL) 1670 return NULL; 1671 1672 *m0 = m; 1673 return port; 1674 } 1675 1676 void 1677 ether_input_chain2(struct ifnet *ifp, struct mbuf *m, struct mbuf_chain *chain) 1678 { 1679 struct ether_header *eh, *save_eh, save_eh0; 1680 struct lwkt_port *port; 1681 uint16_t ether_type; 1682 int isr; 1683 1684 ASSERT_SERIALIZED(ifp->if_serializer); 1685 M_ASSERTPKTHDR(m); 1686 1687 /* Discard packet if interface is not up */ 1688 if (!(ifp->if_flags & IFF_UP)) { 1689 m_freem(m); 1690 return; 1691 } 1692 1693 if (m->m_len < sizeof(struct ether_header)) { 1694 /* XXX error in the caller. */ 1695 m_freem(m); 1696 return; 1697 } 1698 eh = mtod(m, struct ether_header *); 1699 1700 m->m_pkthdr.rcvif = ifp; 1701 1702 if (ETHER_IS_MULTICAST(eh->ether_dhost)) { 1703 if (bcmp(ifp->if_broadcastaddr, eh->ether_dhost, 1704 ifp->if_addrlen) == 0) 1705 m->m_flags |= M_BCAST; 1706 else 1707 m->m_flags |= M_MCAST; 1708 ifp->if_imcasts++; 1709 } 1710 1711 ETHER_BPF_MTAP(ifp, m); 1712 1713 ifp->if_ibytes += m->m_pkthdr.len; 1714 1715 if (ifp->if_flags & IFF_MONITOR) { 1716 /* 1717 * Interface marked for monitoring; discard packet. 1718 */ 1719 m_freem(m); 1720 return; 1721 } 1722 1723 if (ntohs(eh->ether_type) == ETHERTYPE_VLAN && 1724 (m->m_flags & M_VLANTAG) == 0) { 1725 /* 1726 * Extract vlan tag if hardware does not do it for us 1727 */ 1728 vlan_ether_decap(&m); 1729 if (m == NULL) 1730 return; 1731 eh = mtod(m, struct ether_header *); 1732 } 1733 ether_type = ntohs(eh->ether_type); 1734 1735 if ((m->m_flags & M_VLANTAG) && ether_type == ETHERTYPE_VLAN) { 1736 /* 1737 * To prevent possible dangerous recursion, 1738 * we don't do vlan-in-vlan 1739 */ 1740 ifp->if_noproto++; 1741 m_freem(m); 1742 return; 1743 } 1744 KKASSERT(ether_type != ETHERTYPE_VLAN); 1745 1746 /* 1747 * Map ether type to netisr id. 1748 */ 1749 switch (ether_type) { 1750 #ifdef INET 1751 case ETHERTYPE_IP: 1752 isr = NETISR_IP; 1753 break; 1754 1755 case ETHERTYPE_ARP: 1756 isr = NETISR_ARP; 1757 break; 1758 #endif 1759 1760 #ifdef INET6 1761 case ETHERTYPE_IPV6: 1762 isr = NETISR_IPV6; 1763 break; 1764 #endif 1765 1766 #ifdef IPX 1767 case ETHERTYPE_IPX: 1768 isr = NETISR_IPX; 1769 break; 1770 #endif 1771 1772 #ifdef NS 1773 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */ 1774 isr = NETISR_NS; 1775 break; 1776 #endif 1777 1778 #ifdef NETATALK 1779 case ETHERTYPE_AT: 1780 isr = NETISR_ATALK1; 1781 break; 1782 case ETHERTYPE_AARP: 1783 isr = NETISR_AARP; 1784 break; 1785 #endif 1786 1787 default: 1788 /* 1789 * NETISR_MAX is an invalid value; it is chosen to let 1790 * ether_mport() know that we are not able to decide 1791 * this packet's msgport here. 1792 */ 1793 isr = NETISR_MAX; 1794 break; 1795 } 1796 1797 /* 1798 * If the packet is in contiguous memory, following 1799 * m_adj() could ensure that the hidden ether header 1800 * will not be destroyed, else we will have to save 1801 * the ether header for the later restoration. 1802 */ 1803 if (m->m_pkthdr.len != m->m_len) { 1804 save_eh0 = *eh; 1805 save_eh = &save_eh0; 1806 } else { 1807 save_eh = NULL; 1808 } 1809 1810 /* 1811 * Temporarily remove ether header; ether_mport() 1812 * expects a packet without ether header. 1813 */ 1814 m_adj(m, sizeof(struct ether_header)); 1815 1816 /* 1817 * Find the packet's target msgport. 1818 */ 1819 port = ether_mport(isr, &m); 1820 if (port == NULL) { 1821 KKASSERT(m == NULL); 1822 return; 1823 } 1824 1825 /* 1826 * Restore ether header. 1827 */ 1828 if (save_eh != NULL) { 1829 ether_restore_header(&m, eh, save_eh); 1830 if (m == NULL) 1831 return; 1832 } else { 1833 m->m_data -= ETHER_HDR_LEN; 1834 m->m_len += ETHER_HDR_LEN; 1835 m->m_pkthdr.len += ETHER_HDR_LEN; 1836 } 1837 1838 /* 1839 * Initialize mbuf's netmsg packet _after_ possible 1840 * ether header restoration, else the initialized 1841 * netmsg packet may be lost during ether header 1842 * restoration. 1843 */ 1844 ether_init_netpacket(isr, m); 1845 1846 #ifdef ETHER_INPUT_CHAIN 1847 if (chain != NULL) { 1848 struct mbuf_chain *c; 1849 int cpuid; 1850 1851 m->m_pkthdr.header = port; /* XXX */ 1852 cpuid = port->mpu_td->td_gd->gd_cpuid; 1853 1854 c = &chain[cpuid]; 1855 if (c->mc_head == NULL) { 1856 c->mc_head = c->mc_tail = m; 1857 } else { 1858 c->mc_tail->m_nextpkt = m; 1859 c->mc_tail = m; 1860 } 1861 m->m_nextpkt = NULL; 1862 } else 1863 #endif /* ETHER_INPUT_CHAIN */ 1864 lwkt_sendmsg(port, &m->m_hdr.mh_netmsg.nm_netmsg.nm_lmsg); 1865 } 1866 1867 #endif /* ETHER_INPUT2 */ 1868