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