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