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