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