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 static void 529 ether_input(struct ifnet *ifp, struct mbuf *m, 530 const struct pktinfo *pi, int cpuid) 531 { 532 ether_input_pkt(ifp, m, pi, cpuid); 533 } 534 535 /* 536 * Perform common duties while attaching to interface list 537 */ 538 void 539 ether_ifattach(struct ifnet *ifp, const uint8_t *lla, 540 lwkt_serialize_t serializer) 541 { 542 ether_ifattach_bpf(ifp, lla, DLT_EN10MB, sizeof(struct ether_header), 543 serializer); 544 } 545 546 void 547 ether_ifattach_bpf(struct ifnet *ifp, const uint8_t *lla, 548 u_int dlt, u_int hdrlen, lwkt_serialize_t serializer) 549 { 550 struct sockaddr_dl *sdl; 551 char ethstr[ETHER_ADDRSTRLEN + 1]; 552 struct ifaltq *ifq; 553 int i; 554 555 ifp->if_type = IFT_ETHER; 556 ifp->if_addrlen = ETHER_ADDR_LEN; 557 ifp->if_hdrlen = ETHER_HDR_LEN; 558 if_attach(ifp, serializer); 559 ifq = &ifp->if_snd; 560 for (i = 0; i < ifq->altq_subq_cnt; ++i) { 561 struct ifaltq_subque *ifsq = ifq_get_subq(ifq, i); 562 563 ifsq->ifsq_maxbcnt = ifsq->ifsq_maxlen * 564 (ETHER_MAX_LEN - ETHER_CRC_LEN); 565 } 566 ifp->if_mtu = ETHERMTU; 567 if (ifp->if_tsolen <= 0) { 568 if ((ether_tsolen_default / ETHERMTU) < 2) { 569 kprintf("ether TSO maxlen %d -> %d\n", 570 ether_tsolen_default, ETHER_TSOLEN_DEFAULT); 571 ether_tsolen_default = ETHER_TSOLEN_DEFAULT; 572 } 573 ifp->if_tsolen = ether_tsolen_default; 574 } 575 if (ifp->if_baudrate == 0) 576 ifp->if_baudrate = 10000000; 577 ifp->if_output = ether_output; 578 ifp->if_input = ether_input; 579 ifp->if_resolvemulti = ether_resolvemulti; 580 ifp->if_broadcastaddr = etherbroadcastaddr; 581 sdl = IF_LLSOCKADDR(ifp); 582 sdl->sdl_type = IFT_ETHER; 583 sdl->sdl_alen = ifp->if_addrlen; 584 bcopy(lla, LLADDR(sdl), ifp->if_addrlen); 585 /* 586 * XXX Keep the current drivers happy. 587 * XXX Remove once all drivers have been cleaned up 588 */ 589 if (lla != IFP2AC(ifp)->ac_enaddr) 590 bcopy(lla, IFP2AC(ifp)->ac_enaddr, ifp->if_addrlen); 591 bpfattach(ifp, dlt, hdrlen); 592 if (ng_ether_attach_p != NULL) 593 (*ng_ether_attach_p)(ifp); 594 595 if_printf(ifp, "MAC address: %s\n", kether_ntoa(lla, ethstr)); 596 } 597 598 /* 599 * Perform common duties while detaching an Ethernet interface 600 */ 601 void 602 ether_ifdetach(struct ifnet *ifp) 603 { 604 if_down(ifp); 605 606 if (ng_ether_detach_p != NULL) 607 (*ng_ether_detach_p)(ifp); 608 bpfdetach(ifp); 609 if_detach(ifp); 610 } 611 612 int 613 ether_ioctl(struct ifnet *ifp, u_long command, caddr_t data) 614 { 615 struct ifaddr *ifa = (struct ifaddr *) data; 616 struct ifreq *ifr = (struct ifreq *) data; 617 int error = 0; 618 619 #define IF_INIT(ifp) \ 620 do { \ 621 if (((ifp)->if_flags & IFF_UP) == 0) { \ 622 (ifp)->if_flags |= IFF_UP; \ 623 (ifp)->if_init((ifp)->if_softc); \ 624 } \ 625 } while (0) 626 627 ASSERT_IFNET_SERIALIZED_ALL(ifp); 628 629 switch (command) { 630 case SIOCSIFADDR: 631 switch (ifa->ifa_addr->sa_family) { 632 #ifdef INET 633 case AF_INET: 634 IF_INIT(ifp); /* before arpwhohas */ 635 arp_ifinit(ifp, ifa); 636 break; 637 #endif 638 default: 639 IF_INIT(ifp); 640 break; 641 } 642 break; 643 644 case SIOCGIFADDR: 645 bcopy(IFP2AC(ifp)->ac_enaddr, 646 ((struct sockaddr *)ifr->ifr_data)->sa_data, 647 ETHER_ADDR_LEN); 648 break; 649 650 case SIOCSIFMTU: 651 /* 652 * Set the interface MTU. 653 */ 654 if (ifr->ifr_mtu > ETHERMTU) { 655 error = EINVAL; 656 } else { 657 ifp->if_mtu = ifr->ifr_mtu; 658 } 659 break; 660 default: 661 error = EINVAL; 662 break; 663 } 664 return (error); 665 666 #undef IF_INIT 667 } 668 669 int 670 ether_resolvemulti( 671 struct ifnet *ifp, 672 struct sockaddr **llsa, 673 struct sockaddr *sa) 674 { 675 struct sockaddr_dl *sdl; 676 #ifdef INET 677 struct sockaddr_in *sin; 678 #endif 679 #ifdef INET6 680 struct sockaddr_in6 *sin6; 681 #endif 682 u_char *e_addr; 683 684 switch(sa->sa_family) { 685 case AF_LINK: 686 /* 687 * No mapping needed. Just check that it's a valid MC address. 688 */ 689 sdl = (struct sockaddr_dl *)sa; 690 e_addr = LLADDR(sdl); 691 if ((e_addr[0] & 1) != 1) 692 return EADDRNOTAVAIL; 693 *llsa = NULL; 694 return 0; 695 696 #ifdef INET 697 case AF_INET: 698 sin = (struct sockaddr_in *)sa; 699 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) 700 return EADDRNOTAVAIL; 701 sdl = kmalloc(sizeof *sdl, M_IFMADDR, M_WAITOK | M_ZERO); 702 sdl->sdl_len = sizeof *sdl; 703 sdl->sdl_family = AF_LINK; 704 sdl->sdl_index = ifp->if_index; 705 sdl->sdl_type = IFT_ETHER; 706 sdl->sdl_alen = ETHER_ADDR_LEN; 707 e_addr = LLADDR(sdl); 708 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr); 709 *llsa = (struct sockaddr *)sdl; 710 return 0; 711 #endif 712 #ifdef INET6 713 case AF_INET6: 714 sin6 = (struct sockaddr_in6 *)sa; 715 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 716 /* 717 * An IP6 address of 0 means listen to all 718 * of the Ethernet multicast address used for IP6. 719 * (This is used for multicast routers.) 720 */ 721 ifp->if_flags |= IFF_ALLMULTI; 722 *llsa = NULL; 723 return 0; 724 } 725 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) 726 return EADDRNOTAVAIL; 727 sdl = kmalloc(sizeof *sdl, M_IFMADDR, M_WAITOK | M_ZERO); 728 sdl->sdl_len = sizeof *sdl; 729 sdl->sdl_family = AF_LINK; 730 sdl->sdl_index = ifp->if_index; 731 sdl->sdl_type = IFT_ETHER; 732 sdl->sdl_alen = ETHER_ADDR_LEN; 733 e_addr = LLADDR(sdl); 734 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr); 735 *llsa = (struct sockaddr *)sdl; 736 return 0; 737 #endif 738 739 default: 740 /* 741 * Well, the text isn't quite right, but it's the name 742 * that counts... 743 */ 744 return EAFNOSUPPORT; 745 } 746 } 747 748 #if 0 749 /* 750 * This is for reference. We have a table-driven version 751 * of the little-endian crc32 generator, which is faster 752 * than the double-loop. 753 */ 754 uint32_t 755 ether_crc32_le(const uint8_t *buf, size_t len) 756 { 757 uint32_t c, crc, carry; 758 size_t i, j; 759 760 crc = 0xffffffffU; /* initial value */ 761 762 for (i = 0; i < len; i++) { 763 c = buf[i]; 764 for (j = 0; j < 8; j++) { 765 carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01); 766 crc >>= 1; 767 c >>= 1; 768 if (carry) 769 crc = (crc ^ ETHER_CRC_POLY_LE); 770 } 771 } 772 773 return (crc); 774 } 775 #else 776 uint32_t 777 ether_crc32_le(const uint8_t *buf, size_t len) 778 { 779 static const uint32_t crctab[] = { 780 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac, 781 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c, 782 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c, 783 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c 784 }; 785 uint32_t crc; 786 size_t i; 787 788 crc = 0xffffffffU; /* initial value */ 789 790 for (i = 0; i < len; i++) { 791 crc ^= buf[i]; 792 crc = (crc >> 4) ^ crctab[crc & 0xf]; 793 crc = (crc >> 4) ^ crctab[crc & 0xf]; 794 } 795 796 return (crc); 797 } 798 #endif 799 800 uint32_t 801 ether_crc32_be(const uint8_t *buf, size_t len) 802 { 803 uint32_t c, crc, carry; 804 size_t i, j; 805 806 crc = 0xffffffffU; /* initial value */ 807 808 for (i = 0; i < len; i++) { 809 c = buf[i]; 810 for (j = 0; j < 8; j++) { 811 carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01); 812 crc <<= 1; 813 c >>= 1; 814 if (carry) 815 crc = (crc ^ ETHER_CRC_POLY_BE) | carry; 816 } 817 } 818 819 return (crc); 820 } 821 822 /* 823 * find the size of ethernet header, and call classifier 824 */ 825 void 826 altq_etherclassify(struct ifaltq *ifq, struct mbuf *m, 827 struct altq_pktattr *pktattr) 828 { 829 struct ether_header *eh; 830 uint16_t ether_type; 831 int hlen, af, hdrsize; 832 833 hlen = sizeof(struct ether_header); 834 eh = mtod(m, struct ether_header *); 835 836 ether_type = ntohs(eh->ether_type); 837 if (ether_type < ETHERMTU) { 838 /* ick! LLC/SNAP */ 839 struct llc *llc = (struct llc *)(eh + 1); 840 hlen += 8; 841 842 if (m->m_len < hlen || 843 llc->llc_dsap != LLC_SNAP_LSAP || 844 llc->llc_ssap != LLC_SNAP_LSAP || 845 llc->llc_control != LLC_UI) 846 goto bad; /* not snap! */ 847 848 ether_type = ntohs(llc->llc_un.type_snap.ether_type); 849 } 850 851 if (ether_type == ETHERTYPE_IP) { 852 af = AF_INET; 853 hdrsize = 20; /* sizeof(struct ip) */ 854 #ifdef INET6 855 } else if (ether_type == ETHERTYPE_IPV6) { 856 af = AF_INET6; 857 hdrsize = 40; /* sizeof(struct ip6_hdr) */ 858 #endif 859 } else 860 goto bad; 861 862 while (m->m_len <= hlen) { 863 hlen -= m->m_len; 864 m = m->m_next; 865 } 866 if (m->m_len < hlen + hdrsize) { 867 /* 868 * ip header is not in a single mbuf. this should not 869 * happen in the current code. 870 * (todo: use m_pulldown in the future) 871 */ 872 goto bad; 873 } 874 m->m_data += hlen; 875 m->m_len -= hlen; 876 ifq_classify(ifq, m, af, pktattr); 877 m->m_data -= hlen; 878 m->m_len += hlen; 879 880 return; 881 882 bad: 883 pktattr->pattr_class = NULL; 884 pktattr->pattr_hdr = NULL; 885 pktattr->pattr_af = AF_UNSPEC; 886 } 887 888 static void 889 ether_restore_header(struct mbuf **m0, const struct ether_header *eh, 890 const struct ether_header *save_eh) 891 { 892 struct mbuf *m = *m0; 893 894 ether_restore_hdr++; 895 896 /* 897 * Prepend the header, optimize for the common case of 898 * eh pointing into the mbuf. 899 */ 900 if ((const void *)(eh + 1) == (void *)m->m_data) { 901 m->m_data -= ETHER_HDR_LEN; 902 m->m_len += ETHER_HDR_LEN; 903 m->m_pkthdr.len += ETHER_HDR_LEN; 904 } else { 905 ether_prepend_hdr++; 906 907 M_PREPEND(m, ETHER_HDR_LEN, MB_DONTWAIT); 908 if (m != NULL) { 909 bcopy(save_eh, mtod(m, struct ether_header *), 910 ETHER_HDR_LEN); 911 } 912 } 913 *m0 = m; 914 } 915 916 /* 917 * Upper layer processing for a received Ethernet packet. 918 */ 919 void 920 ether_demux_oncpu(struct ifnet *ifp, struct mbuf *m) 921 { 922 struct ether_header *eh; 923 int isr, discard = 0; 924 u_short ether_type; 925 struct ip_fw *rule = NULL; 926 927 M_ASSERTPKTHDR(m); 928 KASSERT(m->m_len >= ETHER_HDR_LEN, 929 ("ether header is not contiguous!")); 930 931 eh = mtod(m, struct ether_header *); 932 933 if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED) { 934 struct m_tag *mtag; 935 936 /* Extract info from dummynet tag */ 937 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL); 938 KKASSERT(mtag != NULL); 939 rule = ((struct dn_pkt *)m_tag_data(mtag))->dn_priv; 940 KKASSERT(rule != NULL); 941 942 m_tag_delete(m, mtag); 943 m->m_pkthdr.fw_flags &= ~DUMMYNET_MBUF_TAGGED; 944 945 /* packet is passing the second time */ 946 goto post_stats; 947 } 948 949 /* 950 * We got a packet which was unicast to a different Ethernet 951 * address. If the driver is working properly, then this 952 * situation can only happen when the interface is in 953 * promiscuous mode. We defer the packet discarding until the 954 * vlan processing is done, so that vlan/bridge or vlan/netgraph 955 * could work. 956 */ 957 if (((ifp->if_flags & (IFF_PROMISC | IFF_PPROMISC)) == IFF_PROMISC) && 958 !ETHER_IS_MULTICAST(eh->ether_dhost) && 959 bcmp(eh->ether_dhost, IFP2AC(ifp)->ac_enaddr, ETHER_ADDR_LEN)) { 960 if (ether_debug & 1) { 961 kprintf("%02x:%02x:%02x:%02x:%02x:%02x " 962 "%02x:%02x:%02x:%02x:%02x:%02x " 963 "%04x vs %02x:%02x:%02x:%02x:%02x:%02x\n", 964 eh->ether_dhost[0], 965 eh->ether_dhost[1], 966 eh->ether_dhost[2], 967 eh->ether_dhost[3], 968 eh->ether_dhost[4], 969 eh->ether_dhost[5], 970 eh->ether_shost[0], 971 eh->ether_shost[1], 972 eh->ether_shost[2], 973 eh->ether_shost[3], 974 eh->ether_shost[4], 975 eh->ether_shost[5], 976 eh->ether_type, 977 ((u_char *)IFP2AC(ifp)->ac_enaddr)[0], 978 ((u_char *)IFP2AC(ifp)->ac_enaddr)[1], 979 ((u_char *)IFP2AC(ifp)->ac_enaddr)[2], 980 ((u_char *)IFP2AC(ifp)->ac_enaddr)[3], 981 ((u_char *)IFP2AC(ifp)->ac_enaddr)[4], 982 ((u_char *)IFP2AC(ifp)->ac_enaddr)[5] 983 ); 984 } 985 if ((ether_debug & 2) == 0) 986 discard = 1; 987 } 988 989 post_stats: 990 if (IPFW_LOADED && ether_ipfw != 0 && !discard) { 991 struct ether_header save_eh = *eh; 992 993 /* XXX old crufty stuff, needs to be removed */ 994 m_adj(m, sizeof(struct ether_header)); 995 996 if (!ether_ipfw_chk(&m, NULL, &rule, eh)) { 997 m_freem(m); 998 return; 999 } 1000 1001 ether_restore_header(&m, eh, &save_eh); 1002 if (m == NULL) 1003 return; 1004 eh = mtod(m, struct ether_header *); 1005 } 1006 1007 ether_type = ntohs(eh->ether_type); 1008 KKASSERT(ether_type != ETHERTYPE_VLAN); 1009 1010 if (m->m_flags & M_VLANTAG) { 1011 void (*vlan_input_func)(struct mbuf *); 1012 1013 vlan_input_func = vlan_input_p; 1014 if (vlan_input_func != NULL) { 1015 vlan_input_func(m); 1016 } else { 1017 IFNET_STAT_INC(m->m_pkthdr.rcvif, noproto, 1); 1018 m_freem(m); 1019 } 1020 return; 1021 } 1022 1023 /* 1024 * If we have been asked to discard this packet 1025 * (e.g. not for us), drop it before entering 1026 * the upper layer. 1027 */ 1028 if (discard) { 1029 m_freem(m); 1030 return; 1031 } 1032 1033 /* 1034 * Clear protocol specific flags, 1035 * before entering the upper layer. 1036 */ 1037 m->m_flags &= ~M_ETHER_FLAGS; 1038 1039 /* Strip ethernet header. */ 1040 m_adj(m, sizeof(struct ether_header)); 1041 1042 switch (ether_type) { 1043 #ifdef INET 1044 case ETHERTYPE_IP: 1045 if ((m->m_flags & M_LENCHECKED) == 0) { 1046 if (!ip_lengthcheck(&m, 0)) 1047 return; 1048 } 1049 if (ipflow_fastforward(m)) 1050 return; 1051 isr = NETISR_IP; 1052 break; 1053 1054 case ETHERTYPE_ARP: 1055 if (ifp->if_flags & IFF_NOARP) { 1056 /* Discard packet if ARP is disabled on interface */ 1057 m_freem(m); 1058 return; 1059 } 1060 isr = NETISR_ARP; 1061 break; 1062 #endif 1063 1064 #ifdef INET6 1065 case ETHERTYPE_IPV6: 1066 isr = NETISR_IPV6; 1067 break; 1068 #endif 1069 1070 #ifdef MPLS 1071 case ETHERTYPE_MPLS: 1072 case ETHERTYPE_MPLS_MCAST: 1073 /* Should have been set by ether_input_pkt(). */ 1074 KKASSERT(m->m_flags & M_MPLSLABELED); 1075 isr = NETISR_MPLS; 1076 break; 1077 #endif 1078 1079 default: 1080 /* 1081 * The accurate msgport is not determined before 1082 * we reach here, so recharacterize packet. 1083 */ 1084 m->m_flags &= ~M_HASH; 1085 if (ng_ether_input_orphan_p != NULL) { 1086 /* 1087 * Put back the ethernet header so netgraph has a 1088 * consistent view of inbound packets. 1089 */ 1090 M_PREPEND(m, ETHER_HDR_LEN, MB_DONTWAIT); 1091 if (m == NULL) { 1092 /* 1093 * M_PREPEND frees the mbuf in case of failure. 1094 */ 1095 return; 1096 } 1097 /* 1098 * Hold BGL and recheck ng_ether_input_orphan_p 1099 */ 1100 get_mplock(); 1101 if (ng_ether_input_orphan_p != NULL) { 1102 ng_ether_input_orphan_p(ifp, m); 1103 rel_mplock(); 1104 return; 1105 } 1106 rel_mplock(); 1107 } 1108 m_freem(m); 1109 return; 1110 } 1111 1112 if (m->m_flags & M_HASH) { 1113 if (&curthread->td_msgport == 1114 netisr_hashport(m->m_pkthdr.hash)) { 1115 netisr_handle(isr, m); 1116 return; 1117 } else { 1118 /* 1119 * XXX Something is wrong, 1120 * we probably should panic here! 1121 */ 1122 m->m_flags &= ~M_HASH; 1123 atomic_add_long(ðer_input_wronghash, 1); 1124 } 1125 } 1126 #ifdef RSS_DEBUG 1127 atomic_add_long(ðer_input_requeue, 1); 1128 #endif 1129 netisr_queue(isr, m); 1130 } 1131 1132 /* 1133 * First we perform any link layer operations, then continue to the 1134 * upper layers with ether_demux_oncpu(). 1135 */ 1136 static void 1137 ether_input_oncpu(struct ifnet *ifp, struct mbuf *m) 1138 { 1139 #ifdef CARP 1140 void *carp; 1141 #endif 1142 1143 if ((ifp->if_flags & (IFF_UP | IFF_MONITOR)) != IFF_UP) { 1144 /* 1145 * Receiving interface's flags are changed, when this 1146 * packet is waiting for processing; discard it. 1147 */ 1148 m_freem(m); 1149 return; 1150 } 1151 1152 /* 1153 * Tap the packet off here for a bridge. bridge_input() 1154 * will return NULL if it has consumed the packet, otherwise 1155 * it gets processed as normal. Note that bridge_input() 1156 * will always return the original packet if we need to 1157 * process it locally. 1158 */ 1159 if (ifp->if_bridge) { 1160 KASSERT(bridge_input_p != NULL, 1161 ("%s: if_bridge not loaded!", __func__)); 1162 1163 if(m->m_flags & M_ETHER_BRIDGED) { 1164 m->m_flags &= ~M_ETHER_BRIDGED; 1165 } else { 1166 m = bridge_input_p(ifp, m); 1167 if (m == NULL) 1168 return; 1169 1170 KASSERT(ifp == m->m_pkthdr.rcvif, 1171 ("bridge_input_p changed rcvif")); 1172 } 1173 } 1174 1175 #ifdef CARP 1176 carp = ifp->if_carp; 1177 if (carp) { 1178 m = carp_input(carp, m); 1179 if (m == NULL) 1180 return; 1181 KASSERT(ifp == m->m_pkthdr.rcvif, 1182 ("carp_input changed rcvif")); 1183 } 1184 #endif 1185 1186 /* Handle ng_ether(4) processing, if any */ 1187 if (ng_ether_input_p != NULL) { 1188 /* 1189 * Hold BGL and recheck ng_ether_input_p 1190 */ 1191 get_mplock(); 1192 if (ng_ether_input_p != NULL) 1193 ng_ether_input_p(ifp, &m); 1194 rel_mplock(); 1195 1196 if (m == NULL) 1197 return; 1198 } 1199 1200 /* Continue with upper layer processing */ 1201 ether_demux_oncpu(ifp, m); 1202 } 1203 1204 /* 1205 * Perform certain functions of ether_input_pkt(): 1206 * - Test IFF_UP 1207 * - Update statistics 1208 * - Run bpf(4) tap if requested 1209 * Then pass the packet to ether_input_oncpu(). 1210 * 1211 * This function should be used by pseudo interface (e.g. vlan(4)), 1212 * when it tries to claim that the packet is received by it. 1213 * 1214 * REINPUT_KEEPRCVIF 1215 * REINPUT_RUNBPF 1216 */ 1217 void 1218 ether_reinput_oncpu(struct ifnet *ifp, struct mbuf *m, int reinput_flags) 1219 { 1220 /* Discard packet if interface is not up */ 1221 if (!(ifp->if_flags & IFF_UP)) { 1222 m_freem(m); 1223 return; 1224 } 1225 1226 /* 1227 * Change receiving interface. The bridge will often pass a flag to 1228 * ask that this not be done so ARPs get applied to the correct 1229 * side. 1230 */ 1231 if ((reinput_flags & REINPUT_KEEPRCVIF) == 0 || 1232 m->m_pkthdr.rcvif == NULL) { 1233 m->m_pkthdr.rcvif = ifp; 1234 } 1235 1236 /* Update statistics */ 1237 IFNET_STAT_INC(ifp, ipackets, 1); 1238 IFNET_STAT_INC(ifp, ibytes, m->m_pkthdr.len); 1239 if (m->m_flags & (M_MCAST | M_BCAST)) 1240 IFNET_STAT_INC(ifp, imcasts, 1); 1241 1242 if (reinput_flags & REINPUT_RUNBPF) 1243 BPF_MTAP(ifp, m); 1244 1245 ether_input_oncpu(ifp, m); 1246 } 1247 1248 static __inline boolean_t 1249 ether_vlancheck(struct mbuf **m0) 1250 { 1251 struct mbuf *m = *m0; 1252 struct ether_header *eh; 1253 uint16_t ether_type; 1254 1255 eh = mtod(m, struct ether_header *); 1256 ether_type = ntohs(eh->ether_type); 1257 1258 if (ether_type == ETHERTYPE_VLAN && (m->m_flags & M_VLANTAG) == 0) { 1259 /* 1260 * Extract vlan tag if hardware does not do it for us 1261 */ 1262 vlan_ether_decap(&m); 1263 if (m == NULL) 1264 goto failed; 1265 1266 eh = mtod(m, struct ether_header *); 1267 ether_type = ntohs(eh->ether_type); 1268 } 1269 1270 if (ether_type == ETHERTYPE_VLAN && (m->m_flags & M_VLANTAG)) { 1271 /* 1272 * To prevent possible dangerous recursion, 1273 * we don't do vlan-in-vlan 1274 */ 1275 IFNET_STAT_INC(m->m_pkthdr.rcvif, noproto, 1); 1276 goto failed; 1277 } 1278 KKASSERT(ether_type != ETHERTYPE_VLAN); 1279 1280 m->m_flags |= M_ETHER_VLANCHECKED; 1281 *m0 = m; 1282 return TRUE; 1283 failed: 1284 if (m != NULL) 1285 m_freem(m); 1286 *m0 = NULL; 1287 return FALSE; 1288 } 1289 1290 static void 1291 ether_input_handler(netmsg_t nmsg) 1292 { 1293 struct netmsg_packet *nmp = &nmsg->packet; /* actual size */ 1294 struct ether_header *eh; 1295 struct ifnet *ifp; 1296 struct mbuf *m; 1297 1298 m = nmp->nm_packet; 1299 M_ASSERTPKTHDR(m); 1300 1301 if ((m->m_flags & M_ETHER_VLANCHECKED) == 0) { 1302 if (!ether_vlancheck(&m)) { 1303 KKASSERT(m == NULL); 1304 return; 1305 } 1306 } 1307 if ((m->m_flags & (M_HASH | M_CKHASH)) == (M_HASH | M_CKHASH) || 1308 __predict_false(ether_input_ckhash)) { 1309 int isr; 1310 1311 /* 1312 * Need to verify the hash supplied by the hardware 1313 * which could be wrong. 1314 */ 1315 m->m_flags &= ~(M_HASH | M_CKHASH); 1316 isr = ether_characterize(&m); 1317 if (m == NULL) 1318 return; 1319 KKASSERT(m->m_flags & M_HASH); 1320 1321 if (netisr_hashcpu(m->m_pkthdr.hash) != mycpuid) { 1322 /* 1323 * Wrong hardware supplied hash; redispatch 1324 */ 1325 ether_dispatch(isr, m, -1); 1326 if (__predict_false(ether_input_ckhash)) 1327 atomic_add_long(ðer_input_wronghwhash, 1); 1328 return; 1329 } 1330 } 1331 ifp = m->m_pkthdr.rcvif; 1332 1333 eh = mtod(m, struct ether_header *); 1334 if (ETHER_IS_MULTICAST(eh->ether_dhost)) { 1335 if (bcmp(ifp->if_broadcastaddr, eh->ether_dhost, 1336 ifp->if_addrlen) == 0) 1337 m->m_flags |= M_BCAST; 1338 else 1339 m->m_flags |= M_MCAST; 1340 IFNET_STAT_INC(ifp, imcasts, 1); 1341 } 1342 1343 ether_input_oncpu(ifp, m); 1344 } 1345 1346 /* 1347 * Send the packet to the target netisr msgport 1348 * 1349 * At this point the packet must be characterized (M_HASH set), 1350 * so we know which netisr to send it to. 1351 */ 1352 static void 1353 ether_dispatch(int isr, struct mbuf *m, int cpuid) 1354 { 1355 struct netmsg_packet *pmsg; 1356 int target_cpuid; 1357 1358 KKASSERT(m->m_flags & M_HASH); 1359 target_cpuid = netisr_hashcpu(m->m_pkthdr.hash); 1360 1361 pmsg = &m->m_hdr.mh_netmsg; 1362 netmsg_init(&pmsg->base, NULL, &netisr_apanic_rport, 1363 0, ether_input_handler); 1364 pmsg->nm_packet = m; 1365 pmsg->base.lmsg.u.ms_result = isr; 1366 1367 logether(disp_beg, NULL); 1368 if (target_cpuid == cpuid) { 1369 lwkt_sendmsg_oncpu(netisr_cpuport(target_cpuid), 1370 &pmsg->base.lmsg); 1371 } else { 1372 lwkt_sendmsg(netisr_cpuport(target_cpuid), 1373 &pmsg->base.lmsg); 1374 } 1375 logether(disp_end, NULL); 1376 } 1377 1378 /* 1379 * Process a received Ethernet packet. 1380 * 1381 * The ethernet header is assumed to be in the mbuf so the caller 1382 * MUST MAKE SURE that there are at least sizeof(struct ether_header) 1383 * bytes in the first mbuf. 1384 * 1385 * If the caller knows that the current thread is stick to the current 1386 * cpu, e.g. the interrupt thread or the netisr thread, the current cpuid 1387 * (mycpuid) should be passed through 'cpuid' argument. Else -1 should 1388 * be passed as 'cpuid' argument. 1389 */ 1390 void 1391 ether_input_pkt(struct ifnet *ifp, struct mbuf *m, const struct pktinfo *pi, 1392 int cpuid) 1393 { 1394 int isr; 1395 1396 M_ASSERTPKTHDR(m); 1397 1398 /* Discard packet if interface is not up */ 1399 if (!(ifp->if_flags & IFF_UP)) { 1400 m_freem(m); 1401 return; 1402 } 1403 1404 if (m->m_len < sizeof(struct ether_header)) { 1405 /* XXX error in the caller. */ 1406 m_freem(m); 1407 return; 1408 } 1409 1410 m->m_pkthdr.rcvif = ifp; 1411 1412 logether(pkt_beg, ifp); 1413 1414 ETHER_BPF_MTAP(ifp, m); 1415 1416 IFNET_STAT_INC(ifp, ibytes, m->m_pkthdr.len); 1417 1418 if (ifp->if_flags & IFF_MONITOR) { 1419 struct ether_header *eh; 1420 1421 eh = mtod(m, struct ether_header *); 1422 if (ETHER_IS_MULTICAST(eh->ether_dhost)) 1423 IFNET_STAT_INC(ifp, imcasts, 1); 1424 1425 /* 1426 * Interface marked for monitoring; discard packet. 1427 */ 1428 m_freem(m); 1429 1430 logether(pkt_end, ifp); 1431 return; 1432 } 1433 1434 /* 1435 * If the packet has been characterized (pi->pi_netisr / M_HASH) 1436 * we can dispatch it immediately with trivial checks. 1437 */ 1438 if (pi != NULL && (m->m_flags & M_HASH)) { 1439 #ifdef RSS_DEBUG 1440 atomic_add_long(ðer_pktinfo_try, 1); 1441 #endif 1442 netisr_hashcheck(pi->pi_netisr, m, pi); 1443 if (m->m_flags & M_HASH) { 1444 ether_dispatch(pi->pi_netisr, m, cpuid); 1445 #ifdef RSS_DEBUG 1446 atomic_add_long(ðer_pktinfo_hit, 1); 1447 #endif 1448 logether(pkt_end, ifp); 1449 return; 1450 } 1451 } 1452 #ifdef RSS_DEBUG 1453 else if (ifp->if_capenable & IFCAP_RSS) { 1454 if (pi == NULL) 1455 atomic_add_long(ðer_rss_nopi, 1); 1456 else 1457 atomic_add_long(ðer_rss_nohash, 1); 1458 } 1459 #endif 1460 1461 /* 1462 * Packet hash will be recalculated by software, so clear 1463 * the M_HASH and M_CKHASH flag set by the driver; the hash 1464 * value calculated by the hardware may not be exactly what 1465 * we want. 1466 */ 1467 m->m_flags &= ~(M_HASH | M_CKHASH); 1468 1469 if (!ether_vlancheck(&m)) { 1470 KKASSERT(m == NULL); 1471 logether(pkt_end, ifp); 1472 return; 1473 } 1474 1475 isr = ether_characterize(&m); 1476 if (m == NULL) { 1477 logether(pkt_end, ifp); 1478 return; 1479 } 1480 1481 /* 1482 * Finally dispatch it 1483 */ 1484 ether_dispatch(isr, m, cpuid); 1485 1486 logether(pkt_end, ifp); 1487 } 1488 1489 static int 1490 ether_characterize(struct mbuf **m0) 1491 { 1492 struct mbuf *m = *m0; 1493 struct ether_header *eh; 1494 uint16_t ether_type; 1495 int isr; 1496 1497 eh = mtod(m, struct ether_header *); 1498 ether_type = ntohs(eh->ether_type); 1499 1500 /* 1501 * Map ether type to netisr id. 1502 */ 1503 switch (ether_type) { 1504 #ifdef INET 1505 case ETHERTYPE_IP: 1506 isr = NETISR_IP; 1507 break; 1508 1509 case ETHERTYPE_ARP: 1510 isr = NETISR_ARP; 1511 break; 1512 #endif 1513 1514 #ifdef INET6 1515 case ETHERTYPE_IPV6: 1516 isr = NETISR_IPV6; 1517 break; 1518 #endif 1519 1520 #ifdef MPLS 1521 case ETHERTYPE_MPLS: 1522 case ETHERTYPE_MPLS_MCAST: 1523 m->m_flags |= M_MPLSLABELED; 1524 isr = NETISR_MPLS; 1525 break; 1526 #endif 1527 1528 default: 1529 /* 1530 * NETISR_MAX is an invalid value; it is chosen to let 1531 * netisr_characterize() know that we have no clear 1532 * idea where this packet should go. 1533 */ 1534 isr = NETISR_MAX; 1535 break; 1536 } 1537 1538 /* 1539 * Ask the isr to characterize the packet since we couldn't. 1540 * This is an attempt to optimally get us onto the correct protocol 1541 * thread. 1542 */ 1543 netisr_characterize(isr, &m, sizeof(struct ether_header)); 1544 1545 *m0 = m; 1546 return isr; 1547 } 1548 1549 static void 1550 ether_demux_handler(netmsg_t nmsg) 1551 { 1552 struct netmsg_packet *nmp = &nmsg->packet; /* actual size */ 1553 struct ifnet *ifp; 1554 struct mbuf *m; 1555 1556 m = nmp->nm_packet; 1557 M_ASSERTPKTHDR(m); 1558 ifp = m->m_pkthdr.rcvif; 1559 1560 ether_demux_oncpu(ifp, m); 1561 } 1562 1563 void 1564 ether_demux(struct mbuf *m) 1565 { 1566 struct netmsg_packet *pmsg; 1567 int isr; 1568 1569 isr = ether_characterize(&m); 1570 if (m == NULL) 1571 return; 1572 1573 KKASSERT(m->m_flags & M_HASH); 1574 pmsg = &m->m_hdr.mh_netmsg; 1575 netmsg_init(&pmsg->base, NULL, &netisr_apanic_rport, 1576 0, ether_demux_handler); 1577 pmsg->nm_packet = m; 1578 pmsg->base.lmsg.u.ms_result = isr; 1579 1580 lwkt_sendmsg(netisr_hashport(m->m_pkthdr.hash), &pmsg->base.lmsg); 1581 } 1582 1583 u_char * 1584 kether_aton(const char *macstr, u_char *addr) 1585 { 1586 unsigned int o0, o1, o2, o3, o4, o5; 1587 int n; 1588 1589 if (macstr == NULL || addr == NULL) 1590 return NULL; 1591 1592 n = ksscanf(macstr, "%x:%x:%x:%x:%x:%x", &o0, &o1, &o2, 1593 &o3, &o4, &o5); 1594 if (n != 6) 1595 return NULL; 1596 1597 addr[0] = o0; 1598 addr[1] = o1; 1599 addr[2] = o2; 1600 addr[3] = o3; 1601 addr[4] = o4; 1602 addr[5] = o5; 1603 1604 return addr; 1605 } 1606 1607 char * 1608 kether_ntoa(const u_char *addr, char *buf) 1609 { 1610 int len = ETHER_ADDRSTRLEN + 1; 1611 int n; 1612 1613 n = ksnprintf(buf, len, "%02x:%02x:%02x:%02x:%02x:%02x", addr[0], 1614 addr[1], addr[2], addr[3], addr[4], addr[5]); 1615 1616 if (n < 17) 1617 return NULL; 1618 1619 return buf; 1620 } 1621 1622 MODULE_VERSION(ether, 1); 1623