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