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