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