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.62 2008/05/28 12:11:13 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_netgraph.h" 43 #include "opt_carp.h" 44 #include "opt_ethernet.h" 45 46 #include <sys/param.h> 47 #include <sys/systm.h> 48 #include <sys/globaldata.h> 49 #include <sys/kernel.h> 50 #include <sys/malloc.h> 51 #include <sys/mbuf.h> 52 #include <sys/msgport.h> 53 #include <sys/socket.h> 54 #include <sys/sockio.h> 55 #include <sys/sysctl.h> 56 #include <sys/thread.h> 57 #include <sys/thread2.h> 58 59 #include <net/if.h> 60 #include <net/netisr.h> 61 #include <net/route.h> 62 #include <net/if_llc.h> 63 #include <net/if_dl.h> 64 #include <net/if_types.h> 65 #include <net/ifq_var.h> 66 #include <net/bpf.h> 67 #include <net/ethernet.h> 68 #include <net/vlan/if_vlan_ether.h> 69 70 #if defined(INET) || defined(INET6) 71 #include <netinet/in.h> 72 #include <netinet/in_var.h> 73 #include <netinet/if_ether.h> 74 #include <net/ipfw/ip_fw.h> 75 #include <net/dummynet/ip_dummynet.h> 76 #endif 77 #ifdef INET6 78 #include <netinet6/nd6.h> 79 #endif 80 81 #ifdef CARP 82 #include <netinet/ip_carp.h> 83 #endif 84 85 #ifdef IPX 86 #include <netproto/ipx/ipx.h> 87 #include <netproto/ipx/ipx_if.h> 88 int (*ef_inputp)(struct ifnet*, const struct ether_header *eh, struct mbuf *m); 89 int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp, struct sockaddr *dst, 90 short *tp, int *hlen); 91 #endif 92 93 #ifdef NS 94 #include <netns/ns.h> 95 #include <netns/ns_if.h> 96 ushort ns_nettype; 97 int ether_outputdebug = 0; 98 int ether_inputdebug = 0; 99 #endif 100 101 #ifdef NETATALK 102 #include <netproto/atalk/at.h> 103 #include <netproto/atalk/at_var.h> 104 #include <netproto/atalk/at_extern.h> 105 106 #define llc_snap_org_code llc_un.type_snap.org_code 107 #define llc_snap_ether_type llc_un.type_snap.ether_type 108 109 extern u_char at_org_code[3]; 110 extern u_char aarp_org_code[3]; 111 #endif /* NETATALK */ 112 113 /* netgraph node hooks for ng_ether(4) */ 114 void (*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp); 115 void (*ng_ether_input_orphan_p)(struct ifnet *ifp, 116 struct mbuf *m, const struct ether_header *eh); 117 int (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp); 118 void (*ng_ether_attach_p)(struct ifnet *ifp); 119 void (*ng_ether_detach_p)(struct ifnet *ifp); 120 121 int (*vlan_input_p)(struct mbuf *, struct mbuf_chain *); 122 123 static int ether_output(struct ifnet *, struct mbuf *, struct sockaddr *, 124 struct rtentry *); 125 static void ether_restore_header(struct mbuf **, const struct ether_header *, 126 const struct ether_header *); 127 static void ether_demux_chain(struct ifnet *, struct mbuf *, 128 struct mbuf_chain *); 129 130 /* 131 * if_bridge support 132 */ 133 struct mbuf *(*bridge_input_p)(struct ifnet *, struct mbuf *); 134 int (*bridge_output_p)(struct ifnet *, struct mbuf *, 135 struct sockaddr *, struct rtentry *); 136 void (*bridge_dn_p)(struct mbuf *, struct ifnet *); 137 138 static int ether_resolvemulti(struct ifnet *, struct sockaddr **, 139 struct sockaddr *); 140 141 const uint8_t etherbroadcastaddr[ETHER_ADDR_LEN] = { 142 0xff, 0xff, 0xff, 0xff, 0xff, 0xff 143 }; 144 145 #define gotoerr(e) do { error = (e); goto bad; } while (0) 146 #define IFP2AC(ifp) ((struct arpcom *)(ifp)) 147 148 static boolean_t ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst, 149 struct ip_fw **rule, 150 const struct ether_header *eh); 151 152 static int ether_ipfw; 153 static u_int ether_restore_hdr; 154 static u_int ether_prepend_hdr; 155 156 SYSCTL_DECL(_net_link); 157 SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet"); 158 SYSCTL_INT(_net_link_ether, OID_AUTO, ipfw, CTLFLAG_RW, 159 ðer_ipfw, 0, "Pass ether pkts through firewall"); 160 SYSCTL_UINT(_net_link_ether, OID_AUTO, restore_hdr, CTLFLAG_RW, 161 ðer_restore_hdr, 0, "# of ether header restoration"); 162 SYSCTL_UINT(_net_link_ether, OID_AUTO, prepend_hdr, CTLFLAG_RW, 163 ðer_prepend_hdr, 0, 164 "# of ether header restoration which prepends mbuf"); 165 166 /* 167 * Ethernet output routine. 168 * Encapsulate a packet of type family for the local net. 169 * Use trailer local net encapsulation if enough data in first 170 * packet leaves a multiple of 512 bytes of data in remainder. 171 * Assumes that ifp is actually pointer to arpcom structure. 172 */ 173 static int 174 ether_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst, 175 struct rtentry *rt) 176 { 177 struct ether_header *eh, *deh; 178 u_char *edst; 179 int loop_copy = 0; 180 int hlen = ETHER_HDR_LEN; /* link layer header length */ 181 struct arpcom *ac = IFP2AC(ifp); 182 int error; 183 184 ASSERT_NOT_SERIALIZED(ifp->if_serializer); 185 186 if (ifp->if_flags & IFF_MONITOR) 187 gotoerr(ENETDOWN); 188 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING)) 189 gotoerr(ENETDOWN); 190 191 M_PREPEND(m, sizeof(struct ether_header), MB_DONTWAIT); 192 if (m == NULL) 193 return (ENOBUFS); 194 eh = mtod(m, struct ether_header *); 195 edst = eh->ether_dhost; 196 197 /* 198 * Fill in the destination ethernet address and frame type. 199 */ 200 switch (dst->sa_family) { 201 #ifdef INET 202 case AF_INET: 203 if (!arpresolve(ifp, rt, m, dst, edst)) 204 return (0); /* if not yet resolved */ 205 eh->ether_type = htons(ETHERTYPE_IP); 206 break; 207 #endif 208 #ifdef INET6 209 case AF_INET6: 210 if (!nd6_storelladdr(&ac->ac_if, rt, m, dst, edst)) 211 return (0); /* Something bad happenned. */ 212 eh->ether_type = htons(ETHERTYPE_IPV6); 213 break; 214 #endif 215 #ifdef IPX 216 case AF_IPX: 217 if (ef_outputp != NULL) { 218 error = ef_outputp(ifp, &m, dst, &eh->ether_type, 219 &hlen); 220 if (error) 221 goto bad; 222 } else { 223 eh->ether_type = htons(ETHERTYPE_IPX); 224 bcopy(&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host), 225 edst, ETHER_ADDR_LEN); 226 } 227 break; 228 #endif 229 #ifdef NETATALK 230 case AF_APPLETALK: { 231 struct at_ifaddr *aa; 232 233 if ((aa = at_ifawithnet((struct sockaddr_at *)dst)) == NULL) { 234 error = 0; /* XXX */ 235 goto bad; 236 } 237 /* 238 * In the phase 2 case, need to prepend an mbuf for 239 * the llc header. Since we must preserve the value 240 * of m, which is passed to us by value, we m_copy() 241 * the first mbuf, and use it for our llc header. 242 */ 243 if (aa->aa_flags & AFA_PHASE2) { 244 struct llc llc; 245 246 M_PREPEND(m, sizeof(struct llc), MB_DONTWAIT); 247 eh = mtod(m, struct ether_header *); 248 edst = eh->ether_dhost; 249 llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP; 250 llc.llc_control = LLC_UI; 251 bcopy(at_org_code, llc.llc_snap_org_code, 252 sizeof at_org_code); 253 llc.llc_snap_ether_type = htons(ETHERTYPE_AT); 254 bcopy(&llc, 255 mtod(m, caddr_t) + sizeof(struct ether_header), 256 sizeof(struct llc)); 257 eh->ether_type = htons(m->m_pkthdr.len); 258 hlen = sizeof(struct llc) + ETHER_HDR_LEN; 259 } else { 260 eh->ether_type = htons(ETHERTYPE_AT); 261 } 262 if (!aarpresolve(ac, m, (struct sockaddr_at *)dst, edst)) 263 return (0); 264 break; 265 } 266 #endif 267 #ifdef NS 268 case AF_NS: 269 switch(ns_nettype) { 270 default: 271 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */ 272 eh->ether_type = 0x8137; 273 break; 274 case 0x0: /* Novell 802.3 */ 275 eh->ether_type = htons(m->m_pkthdr.len); 276 break; 277 case 0xe0e0: /* Novell 802.2 and Token-Ring */ 278 M_PREPEND(m, 3, MB_DONTWAIT); 279 eh = mtod(m, struct ether_header *); 280 edst = eh->ether_dhost; 281 eh->ether_type = htons(m->m_pkthdr.len); 282 cp = mtod(m, u_char *) + sizeof(struct ether_header); 283 *cp++ = 0xE0; 284 *cp++ = 0xE0; 285 *cp++ = 0x03; 286 break; 287 } 288 bcopy(&(((struct sockaddr_ns *)dst)->sns_addr.x_host), edst, 289 ETHER_ADDR_LEN); 290 /* 291 * XXX if ns_thishost is the same as the node's ethernet 292 * address then just the default code will catch this anyhow. 293 * So I'm not sure if this next clause should be here at all? 294 * [JRE] 295 */ 296 if (bcmp(edst, &ns_thishost, ETHER_ADDR_LEN) == 0) { 297 m->m_pkthdr.rcvif = ifp; 298 netisr_dispatch(NETISR_NS, m); 299 return (error); 300 } 301 if (bcmp(edst, &ns_broadhost, ETHER_ADDR_LEN) == 0) 302 m->m_flags |= M_BCAST; 303 break; 304 #endif 305 case pseudo_AF_HDRCMPLT: 306 case AF_UNSPEC: 307 loop_copy = -1; /* if this is for us, don't do it */ 308 deh = (struct ether_header *)dst->sa_data; 309 memcpy(edst, deh->ether_dhost, ETHER_ADDR_LEN); 310 eh->ether_type = deh->ether_type; 311 break; 312 313 default: 314 if_printf(ifp, "can't handle af%d\n", dst->sa_family); 315 gotoerr(EAFNOSUPPORT); 316 } 317 318 if (dst->sa_family == pseudo_AF_HDRCMPLT) /* unlikely */ 319 memcpy(eh->ether_shost, 320 ((struct ether_header *)dst->sa_data)->ether_shost, 321 ETHER_ADDR_LEN); 322 else 323 memcpy(eh->ether_shost, ac->ac_enaddr, ETHER_ADDR_LEN); 324 325 /* 326 * Bridges require special output handling. 327 */ 328 if (ifp->if_bridge) { 329 KASSERT(bridge_output_p != NULL, 330 ("%s: if_bridge not loaded!", __func__)); 331 lwkt_serialize_enter(ifp->if_serializer); 332 error = bridge_output_p(ifp, m, NULL, NULL); 333 lwkt_serialize_exit(ifp->if_serializer); 334 return error; 335 } 336 337 /* 338 * If a simplex interface, and the packet is being sent to our 339 * Ethernet address or a broadcast address, loopback a copy. 340 * XXX To make a simplex device behave exactly like a duplex 341 * device, we should copy in the case of sending to our own 342 * ethernet address (thus letting the original actually appear 343 * on the wire). However, we don't do that here for security 344 * reasons and compatibility with the original behavior. 345 */ 346 if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) { 347 int csum_flags = 0; 348 349 if (m->m_pkthdr.csum_flags & CSUM_IP) 350 csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID); 351 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) 352 csum_flags |= (CSUM_DATA_VALID | CSUM_PSEUDO_HDR); 353 if ((m->m_flags & M_BCAST) || (loop_copy > 0)) { 354 struct mbuf *n; 355 356 if ((n = m_copypacket(m, MB_DONTWAIT)) != NULL) { 357 n->m_pkthdr.csum_flags |= csum_flags; 358 if (csum_flags & CSUM_DATA_VALID) 359 n->m_pkthdr.csum_data = 0xffff; 360 if_simloop(ifp, n, dst->sa_family, hlen); 361 } else 362 ifp->if_iqdrops++; 363 } else if (bcmp(eh->ether_dhost, eh->ether_shost, 364 ETHER_ADDR_LEN) == 0) { 365 m->m_pkthdr.csum_flags |= csum_flags; 366 if (csum_flags & CSUM_DATA_VALID) 367 m->m_pkthdr.csum_data = 0xffff; 368 if_simloop(ifp, m, dst->sa_family, hlen); 369 return (0); /* XXX */ 370 } 371 } 372 373 #ifdef CARP 374 if (ifp->if_carp && (error = carp_output(ifp, m, dst, NULL))) 375 goto bad; 376 #endif 377 378 379 /* Handle ng_ether(4) processing, if any */ 380 if (ng_ether_output_p != NULL) { 381 if ((error = (*ng_ether_output_p)(ifp, &m)) != 0) 382 goto bad; 383 if (m == NULL) 384 return (0); 385 } 386 387 /* Continue with link-layer output */ 388 return ether_output_frame(ifp, m); 389 390 bad: 391 m_freem(m); 392 return (error); 393 } 394 395 /* 396 * Ethernet link layer output routine to send a raw frame to the device. 397 * 398 * This assumes that the 14 byte Ethernet header is present and contiguous 399 * in the first mbuf. 400 */ 401 int 402 ether_output_frame(struct ifnet *ifp, struct mbuf *m) 403 { 404 struct ip_fw *rule = NULL; 405 int error = 0; 406 struct altq_pktattr pktattr; 407 struct m_tag *mtag; 408 409 ASSERT_NOT_SERIALIZED(ifp->if_serializer); 410 411 /* Extract info from dummynet tag */ 412 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL); 413 if (mtag != NULL) { 414 rule = ((struct dn_pkt *)m_tag_data(mtag))->dn_priv; 415 416 m_tag_delete(m, mtag); 417 mtag = NULL; 418 } 419 420 if (ifq_is_enabled(&ifp->if_snd)) 421 altq_etherclassify(&ifp->if_snd, m, &pktattr); 422 crit_enter(); 423 if (IPFW_LOADED && ether_ipfw != 0) { 424 struct ether_header save_eh, *eh; 425 426 eh = mtod(m, struct ether_header *); 427 save_eh = *eh; 428 m_adj(m, ETHER_HDR_LEN); 429 if (!ether_ipfw_chk(&m, ifp, &rule, eh)) { 430 crit_exit(); 431 if (m != NULL) { 432 m_freem(m); 433 return ENOBUFS; /* pkt dropped */ 434 } else 435 return 0; /* consumed e.g. in a pipe */ 436 } 437 438 /* packet was ok, restore the ethernet header */ 439 ether_restore_header(&m, eh, &save_eh); 440 if (m == NULL) { 441 crit_exit(); 442 return ENOBUFS; 443 } 444 } 445 crit_exit(); 446 447 /* 448 * Queue message on interface, update output statistics if 449 * successful, and start output if interface not yet active. 450 */ 451 error = ifq_dispatch(ifp, m, &pktattr); 452 return (error); 453 } 454 455 /* 456 * ipfw processing for ethernet packets (in and out). 457 * The second parameter is NULL from ether_demux(), and ifp from 458 * ether_output_frame(). 459 */ 460 static boolean_t 461 ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst, struct ip_fw **rule, 462 const struct ether_header *eh) 463 { 464 struct ether_header save_eh = *eh; /* might be a ptr in m */ 465 struct ip_fw_args args; 466 struct m_tag *mtag; 467 int i; 468 469 if (*rule != NULL && fw_one_pass) 470 return TRUE; /* dummynet packet, already partially processed */ 471 472 /* 473 * I need some amount of data to be contiguous. 474 */ 475 i = min((*m0)->m_pkthdr.len, max_protohdr); 476 if ((*m0)->m_len < i) { 477 *m0 = m_pullup(*m0, i); 478 if (*m0 == NULL) 479 return FALSE; 480 } 481 482 args.m = *m0; /* the packet we are looking at */ 483 args.oif = dst; /* destination, if any */ 484 if ((mtag = m_tag_find(*m0, PACKET_TAG_IPFW_DIVERT, NULL)) != NULL) 485 m_tag_delete(*m0, mtag); 486 args.rule = *rule; /* matching rule to restart */ 487 args.next_hop = NULL; /* we do not support forward yet */ 488 args.eh = &save_eh; /* MAC header for bridged/MAC packets */ 489 i = ip_fw_chk_ptr(&args); 490 *m0 = args.m; 491 *rule = args.rule; 492 493 if ((i & IP_FW_PORT_DENY_FLAG) || *m0 == NULL) /* drop */ 494 return FALSE; 495 496 if (i == 0) /* a PASS rule. */ 497 return TRUE; 498 499 if (i & IP_FW_PORT_DYNT_FLAG) { 500 /* 501 * Pass the pkt to dummynet, which consumes it. 502 */ 503 struct mbuf *m; 504 505 m = *m0; /* pass the original to dummynet */ 506 *m0 = NULL; /* and nothing back to the caller */ 507 508 ether_restore_header(&m, eh, &save_eh); 509 if (m == NULL) 510 return FALSE; 511 512 ip_fw_dn_io_ptr(m, (i & 0xffff), 513 dst ? DN_TO_ETH_OUT: DN_TO_ETH_DEMUX, &args); 514 return FALSE; 515 } 516 /* 517 * XXX at some point add support for divert/forward actions. 518 * If none of the above matches, we have to drop the pkt. 519 */ 520 return FALSE; 521 } 522 523 /* 524 * Process a received Ethernet packet. 525 * 526 * The ethernet header is assumed to be in the mbuf so the caller 527 * MUST MAKE SURE that there are at least sizeof(struct ether_header) 528 * bytes in the first mbuf. 529 * 530 * This allows us to concentrate in one place a bunch of code which 531 * is replicated in all device drivers. Also, many functions called 532 * from ether_input() try to put the eh back into the mbuf, so we 533 * can later propagate the 'contiguous packet' interface to them. 534 * 535 * NOTA BENE: for all drivers "eh" is a pointer into the first mbuf or 536 * cluster, right before m_data. So be very careful when working on m, 537 * as you could destroy *eh !! 538 * 539 * First we perform any link layer operations, then continue to the 540 * upper layers with ether_demux(). 541 */ 542 void 543 ether_input_chain(struct ifnet *ifp, struct mbuf *m, struct mbuf_chain *chain) 544 { 545 struct ether_header *eh; 546 547 ASSERT_SERIALIZED(ifp->if_serializer); 548 M_ASSERTPKTHDR(m); 549 550 /* Discard packet if interface is not up */ 551 if (!(ifp->if_flags & IFF_UP)) { 552 m_freem(m); 553 return; 554 } 555 556 if (m->m_len < sizeof(struct ether_header)) { 557 /* XXX error in the caller. */ 558 m_freem(m); 559 return; 560 } 561 eh = mtod(m, struct ether_header *); 562 563 if (ntohs(eh->ether_type) == ETHERTYPE_VLAN && 564 (m->m_flags & M_VLANTAG) == 0) { 565 /* 566 * Extract vlan tag if hardware does not do it for us 567 */ 568 vlan_ether_decap(&m); 569 if (m == NULL) 570 return; 571 eh = mtod(m, struct ether_header *); 572 } 573 574 m->m_pkthdr.rcvif = ifp; 575 576 if (ETHER_IS_MULTICAST(eh->ether_dhost)) { 577 if (bcmp(ifp->if_broadcastaddr, eh->ether_dhost, 578 ifp->if_addrlen) == 0) 579 m->m_flags |= M_BCAST; 580 else 581 m->m_flags |= M_MCAST; 582 ifp->if_imcasts++; 583 } 584 585 ETHER_BPF_MTAP(ifp, m); 586 587 ifp->if_ibytes += m->m_pkthdr.len; 588 589 if (ifp->if_flags & IFF_MONITOR) { 590 /* 591 * Interface marked for monitoring; discard packet. 592 */ 593 m_freem(m); 594 return; 595 } 596 597 /* 598 * Tap the packet off here for a bridge. bridge_input() 599 * will return NULL if it has consumed the packet, otherwise 600 * it gets processed as normal. Note that bridge_input() 601 * will always return the original packet if we need to 602 * process it locally. 603 */ 604 if (ifp->if_bridge) { 605 KASSERT(bridge_input_p != NULL, 606 ("%s: if_bridge not loaded!", __func__)); 607 608 if(m->m_flags & M_PROTO1) { 609 m->m_flags &= ~M_PROTO1; 610 } else { 611 /* clear M_PROMISC, in case the packets comes from a vlan */ 612 /* m->m_flags &= ~M_PROMISC; */ 613 lwkt_serialize_exit(ifp->if_serializer); 614 m = (*bridge_input_p)(ifp, m); 615 lwkt_serialize_enter(ifp->if_serializer); 616 if (m == NULL) 617 return; 618 619 KASSERT(ifp == m->m_pkthdr.rcvif, 620 ("bridge_input_p changed rcvif\n")); 621 622 /* 'm' may be changed by bridge_input_p() */ 623 eh = mtod(m, struct ether_header *); 624 } 625 } 626 627 /* Handle ng_ether(4) processing, if any */ 628 if (ng_ether_input_p != NULL) { 629 ng_ether_input_p(ifp, &m); 630 if (m == NULL) 631 return; 632 633 /* 'm' may be changed by ng_ether_input_p() */ 634 eh = mtod(m, struct ether_header *); 635 } 636 637 /* Continue with upper layer processing */ 638 ether_demux_chain(ifp, m, chain); 639 } 640 641 void 642 ether_input(struct ifnet *ifp, struct mbuf *m) 643 { 644 ether_input_chain(ifp, m, NULL); 645 } 646 647 /* 648 * Upper layer processing for a received Ethernet packet. 649 */ 650 static void 651 ether_demux_chain(struct ifnet *ifp, struct mbuf *m, struct mbuf_chain *chain) 652 { 653 struct ether_header save_eh, *eh; 654 int isr; 655 u_short ether_type; 656 struct ip_fw *rule = NULL; 657 struct m_tag *mtag; 658 #ifdef NETATALK 659 struct llc *l; 660 #endif 661 662 M_ASSERTPKTHDR(m); 663 KASSERT(m->m_len >= ETHER_HDR_LEN, 664 ("ether header is no contiguous!\n")); 665 666 eh = mtod(m, struct ether_header *); 667 save_eh = *eh; 668 669 /* XXX old crufty stuff, needs to be removed */ 670 m_adj(m, sizeof(struct ether_header)); 671 672 /* Extract info from dummynet tag */ 673 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL); 674 if (mtag != NULL) { 675 rule = ((struct dn_pkt *)m_tag_data(mtag))->dn_priv; 676 KKASSERT(ifp == NULL); 677 ifp = m->m_pkthdr.rcvif; 678 679 m_tag_delete(m, mtag); 680 mtag = NULL; 681 } 682 if (rule) /* packet is passing the second time */ 683 goto post_stats; 684 685 #ifdef CARP 686 /* 687 * XXX: Okay, we need to call carp_forus() and - if it is for 688 * us jump over code that does the normal check 689 * "ac_enaddr == ether_dhost". The check sequence is a bit 690 * different from OpenBSD, so we jump over as few code as 691 * possible, to catch _all_ sanity checks. This needs 692 * evaluation, to see if the carp ether_dhost values break any 693 * of these checks! 694 */ 695 if (ifp->if_carp && carp_forus(ifp->if_carp, eh->ether_dhost)) 696 goto post_stats; 697 #endif 698 699 /* 700 * Discard packet if upper layers shouldn't see it because 701 * it was unicast to a different Ethernet address. If the 702 * driver is working properly, then this situation can only 703 * happen when the interface is in promiscuous mode. 704 */ 705 if (((ifp->if_flags & (IFF_PROMISC | IFF_PPROMISC)) == IFF_PROMISC) && 706 (eh->ether_dhost[0] & 1) == 0 && 707 bcmp(eh->ether_dhost, IFP2AC(ifp)->ac_enaddr, ETHER_ADDR_LEN)) { 708 m_freem(m); 709 return; 710 } 711 712 post_stats: 713 if (IPFW_LOADED && ether_ipfw != 0) { 714 if (!ether_ipfw_chk(&m, NULL, &rule, eh)) { 715 m_freem(m); 716 return; 717 } 718 } 719 720 ether_type = ntohs(save_eh.ether_type); 721 722 if (m->m_flags & M_VLANTAG) { 723 if (ether_type == ETHERTYPE_VLAN) { 724 /* 725 * To prevent possible dangerous recursion, 726 * we don't do vlan-in-vlan 727 */ 728 m->m_pkthdr.rcvif->if_noproto++; 729 m_freem(m); 730 } 731 732 if (vlan_input_p != NULL) { 733 ether_restore_header(&m, eh, &save_eh); 734 if (m != NULL) 735 vlan_input_p(m, chain); 736 } else { 737 m->m_pkthdr.rcvif->if_noproto++; 738 m_freem(m); 739 } 740 return; 741 } 742 KKASSERT(ether_type != ETHERTYPE_VLAN); 743 744 switch (ether_type) { 745 #ifdef INET 746 case ETHERTYPE_IP: 747 if (ipflow_fastforward(m, ifp->if_serializer)) 748 return; 749 isr = NETISR_IP; 750 break; 751 752 case ETHERTYPE_ARP: 753 if (ifp->if_flags & IFF_NOARP) { 754 /* Discard packet if ARP is disabled on interface */ 755 m_freem(m); 756 return; 757 } 758 isr = NETISR_ARP; 759 break; 760 #endif 761 762 #ifdef INET6 763 case ETHERTYPE_IPV6: 764 isr = NETISR_IPV6; 765 break; 766 #endif 767 768 #ifdef IPX 769 case ETHERTYPE_IPX: 770 if (ef_inputp && ef_inputp(ifp, &save_eh, m) == 0) 771 return; 772 isr = NETISR_IPX; 773 break; 774 #endif 775 776 #ifdef NS 777 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */ 778 isr = NETISR_NS; 779 break; 780 781 #endif 782 783 #ifdef NETATALK 784 case ETHERTYPE_AT: 785 isr = NETISR_ATALK1; 786 break; 787 case ETHERTYPE_AARP: 788 isr = NETISR_AARP; 789 break; 790 #endif 791 792 default: 793 #ifdef IPX 794 if (ef_inputp && ef_inputp(ifp, &save_eh, m) == 0) 795 return; 796 #endif 797 #ifdef NS 798 checksum = mtod(m, ushort *); 799 /* Novell 802.3 */ 800 if ((ether_type <= ETHERMTU) && 801 ((*checksum == 0xffff) || (*checksum == 0xE0E0))) { 802 if (*checksum == 0xE0E0) { 803 m->m_pkthdr.len -= 3; 804 m->m_len -= 3; 805 m->m_data += 3; 806 } 807 isr = NETISR_NS; 808 break; 809 } 810 #endif 811 #ifdef NETATALK 812 if (ether_type > ETHERMTU) 813 goto dropanyway; 814 l = mtod(m, struct llc *); 815 if (l->llc_dsap == LLC_SNAP_LSAP && 816 l->llc_ssap == LLC_SNAP_LSAP && 817 l->llc_control == LLC_UI) { 818 if (bcmp(&(l->llc_snap_org_code)[0], at_org_code, 819 sizeof at_org_code) == 0 && 820 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) { 821 m_adj(m, sizeof(struct llc)); 822 isr = NETISR_ATALK2; 823 break; 824 } 825 if (bcmp(&(l->llc_snap_org_code)[0], aarp_org_code, 826 sizeof aarp_org_code) == 0 && 827 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) { 828 m_adj(m, sizeof(struct llc)); 829 isr = NETISR_AARP; 830 break; 831 } 832 } 833 dropanyway: 834 #endif 835 if (ng_ether_input_orphan_p != NULL) 836 (*ng_ether_input_orphan_p)(ifp, m, &save_eh); 837 else 838 m_freem(m); 839 return; 840 } 841 842 #ifdef ETHER_INPUT_CHAIN 843 if (chain != NULL) { 844 struct mbuf_chain *c; 845 lwkt_port_t port; 846 int cpuid; 847 848 port = netisr_mport(isr, &m); 849 if (port == NULL) 850 return; 851 852 m->m_pkthdr.header = port; /* XXX */ 853 cpuid = port->mpu_td->td_gd->gd_cpuid; 854 855 c = &chain[cpuid]; 856 if (c->mc_head == NULL) { 857 c->mc_head = c->mc_tail = m; 858 } else { 859 c->mc_tail->m_nextpkt = m; 860 c->mc_tail = m; 861 } 862 m->m_nextpkt = NULL; 863 } else 864 #endif /* ETHER_INPUT_CHAIN */ 865 netisr_dispatch(isr, m); 866 } 867 868 void 869 ether_demux(struct ifnet *ifp, struct mbuf *m) 870 { 871 ether_demux_chain(ifp, m, NULL); 872 } 873 874 /* 875 * Perform common duties while attaching to interface list 876 */ 877 878 void 879 ether_ifattach(struct ifnet *ifp, uint8_t *lla, lwkt_serialize_t serializer) 880 { 881 ether_ifattach_bpf(ifp, lla, DLT_EN10MB, sizeof(struct ether_header), 882 serializer); 883 } 884 885 void 886 ether_ifattach_bpf(struct ifnet *ifp, uint8_t *lla, u_int dlt, u_int hdrlen, 887 lwkt_serialize_t serializer) 888 { 889 struct sockaddr_dl *sdl; 890 891 ifp->if_type = IFT_ETHER; 892 ifp->if_addrlen = ETHER_ADDR_LEN; 893 ifp->if_hdrlen = ETHER_HDR_LEN; 894 if_attach(ifp, serializer); 895 ifp->if_mtu = ETHERMTU; 896 if (ifp->if_baudrate == 0) 897 ifp->if_baudrate = 10000000; 898 ifp->if_output = ether_output; 899 ifp->if_input = ether_input; 900 ifp->if_resolvemulti = ether_resolvemulti; 901 ifp->if_broadcastaddr = etherbroadcastaddr; 902 sdl = IF_LLSOCKADDR(ifp); 903 sdl->sdl_type = IFT_ETHER; 904 sdl->sdl_alen = ifp->if_addrlen; 905 bcopy(lla, LLADDR(sdl), ifp->if_addrlen); 906 /* 907 * XXX Keep the current drivers happy. 908 * XXX Remove once all drivers have been cleaned up 909 */ 910 if (lla != IFP2AC(ifp)->ac_enaddr) 911 bcopy(lla, IFP2AC(ifp)->ac_enaddr, ifp->if_addrlen); 912 bpfattach(ifp, dlt, hdrlen); 913 if (ng_ether_attach_p != NULL) 914 (*ng_ether_attach_p)(ifp); 915 916 if_printf(ifp, "MAC address: %6D\n", lla, ":"); 917 } 918 919 /* 920 * Perform common duties while detaching an Ethernet interface 921 */ 922 void 923 ether_ifdetach(struct ifnet *ifp) 924 { 925 if_down(ifp); 926 927 if (ng_ether_detach_p != NULL) 928 (*ng_ether_detach_p)(ifp); 929 bpfdetach(ifp); 930 if_detach(ifp); 931 } 932 933 int 934 ether_ioctl(struct ifnet *ifp, int command, caddr_t data) 935 { 936 struct ifaddr *ifa = (struct ifaddr *) data; 937 struct ifreq *ifr = (struct ifreq *) data; 938 int error = 0; 939 940 #define IF_INIT(ifp) \ 941 do { \ 942 if (((ifp)->if_flags & IFF_UP) == 0) { \ 943 (ifp)->if_flags |= IFF_UP; \ 944 (ifp)->if_init((ifp)->if_softc); \ 945 } \ 946 } while (0) 947 948 ASSERT_SERIALIZED(ifp->if_serializer); 949 950 switch (command) { 951 case SIOCSIFADDR: 952 switch (ifa->ifa_addr->sa_family) { 953 #ifdef INET 954 case AF_INET: 955 IF_INIT(ifp); /* before arpwhohas */ 956 arp_ifinit(ifp, ifa); 957 break; 958 #endif 959 #ifdef IPX 960 /* 961 * XXX - This code is probably wrong 962 */ 963 case AF_IPX: 964 { 965 struct ipx_addr *ina = &IA_SIPX(ifa)->sipx_addr; 966 struct arpcom *ac = IFP2AC(ifp); 967 968 if (ipx_nullhost(*ina)) 969 ina->x_host = *(union ipx_host *) ac->ac_enaddr; 970 else 971 bcopy(ina->x_host.c_host, ac->ac_enaddr, 972 sizeof ac->ac_enaddr); 973 974 IF_INIT(ifp); /* Set new address. */ 975 break; 976 } 977 #endif 978 #ifdef NS 979 /* 980 * XXX - This code is probably wrong 981 */ 982 case AF_NS: 983 { 984 struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr); 985 struct arpcom *ac = IFP2AC(ifp); 986 987 if (ns_nullhost(*ina)) 988 ina->x_host = *(union ns_host *)(ac->ac_enaddr); 989 else 990 bcopy(ina->x_host.c_host, ac->ac_enaddr, 991 sizeof ac->ac_enaddr); 992 993 /* 994 * Set new address 995 */ 996 IF_INIT(ifp); 997 break; 998 } 999 #endif 1000 default: 1001 IF_INIT(ifp); 1002 break; 1003 } 1004 break; 1005 1006 case SIOCGIFADDR: 1007 bcopy(IFP2AC(ifp)->ac_enaddr, 1008 ((struct sockaddr *)ifr->ifr_data)->sa_data, 1009 ETHER_ADDR_LEN); 1010 break; 1011 1012 case SIOCSIFMTU: 1013 /* 1014 * Set the interface MTU. 1015 */ 1016 if (ifr->ifr_mtu > ETHERMTU) { 1017 error = EINVAL; 1018 } else { 1019 ifp->if_mtu = ifr->ifr_mtu; 1020 } 1021 break; 1022 default: 1023 error = EINVAL; 1024 break; 1025 } 1026 return (error); 1027 1028 #undef IF_INIT 1029 } 1030 1031 int 1032 ether_resolvemulti( 1033 struct ifnet *ifp, 1034 struct sockaddr **llsa, 1035 struct sockaddr *sa) 1036 { 1037 struct sockaddr_dl *sdl; 1038 struct sockaddr_in *sin; 1039 #ifdef INET6 1040 struct sockaddr_in6 *sin6; 1041 #endif 1042 u_char *e_addr; 1043 1044 switch(sa->sa_family) { 1045 case AF_LINK: 1046 /* 1047 * No mapping needed. Just check that it's a valid MC address. 1048 */ 1049 sdl = (struct sockaddr_dl *)sa; 1050 e_addr = LLADDR(sdl); 1051 if ((e_addr[0] & 1) != 1) 1052 return EADDRNOTAVAIL; 1053 *llsa = 0; 1054 return 0; 1055 1056 #ifdef INET 1057 case AF_INET: 1058 sin = (struct sockaddr_in *)sa; 1059 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) 1060 return EADDRNOTAVAIL; 1061 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR, 1062 M_WAITOK | M_ZERO); 1063 sdl->sdl_len = sizeof *sdl; 1064 sdl->sdl_family = AF_LINK; 1065 sdl->sdl_index = ifp->if_index; 1066 sdl->sdl_type = IFT_ETHER; 1067 sdl->sdl_alen = ETHER_ADDR_LEN; 1068 e_addr = LLADDR(sdl); 1069 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr); 1070 *llsa = (struct sockaddr *)sdl; 1071 return 0; 1072 #endif 1073 #ifdef INET6 1074 case AF_INET6: 1075 sin6 = (struct sockaddr_in6 *)sa; 1076 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 1077 /* 1078 * An IP6 address of 0 means listen to all 1079 * of the Ethernet multicast address used for IP6. 1080 * (This is used for multicast routers.) 1081 */ 1082 ifp->if_flags |= IFF_ALLMULTI; 1083 *llsa = 0; 1084 return 0; 1085 } 1086 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) 1087 return EADDRNOTAVAIL; 1088 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR, 1089 M_WAITOK | M_ZERO); 1090 sdl->sdl_len = sizeof *sdl; 1091 sdl->sdl_family = AF_LINK; 1092 sdl->sdl_index = ifp->if_index; 1093 sdl->sdl_type = IFT_ETHER; 1094 sdl->sdl_alen = ETHER_ADDR_LEN; 1095 e_addr = LLADDR(sdl); 1096 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr); 1097 *llsa = (struct sockaddr *)sdl; 1098 return 0; 1099 #endif 1100 1101 default: 1102 /* 1103 * Well, the text isn't quite right, but it's the name 1104 * that counts... 1105 */ 1106 return EAFNOSUPPORT; 1107 } 1108 } 1109 1110 #if 0 1111 /* 1112 * This is for reference. We have a table-driven version 1113 * of the little-endian crc32 generator, which is faster 1114 * than the double-loop. 1115 */ 1116 uint32_t 1117 ether_crc32_le(const uint8_t *buf, size_t len) 1118 { 1119 uint32_t c, crc, carry; 1120 size_t i, j; 1121 1122 crc = 0xffffffffU; /* initial value */ 1123 1124 for (i = 0; i < len; i++) { 1125 c = buf[i]; 1126 for (j = 0; j < 8; j++) { 1127 carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01); 1128 crc >>= 1; 1129 c >>= 1; 1130 if (carry) 1131 crc = (crc ^ ETHER_CRC_POLY_LE); 1132 } 1133 } 1134 1135 return (crc); 1136 } 1137 #else 1138 uint32_t 1139 ether_crc32_le(const uint8_t *buf, size_t len) 1140 { 1141 static const uint32_t crctab[] = { 1142 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac, 1143 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c, 1144 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c, 1145 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c 1146 }; 1147 uint32_t crc; 1148 size_t i; 1149 1150 crc = 0xffffffffU; /* initial value */ 1151 1152 for (i = 0; i < len; i++) { 1153 crc ^= buf[i]; 1154 crc = (crc >> 4) ^ crctab[crc & 0xf]; 1155 crc = (crc >> 4) ^ crctab[crc & 0xf]; 1156 } 1157 1158 return (crc); 1159 } 1160 #endif 1161 1162 uint32_t 1163 ether_crc32_be(const uint8_t *buf, size_t len) 1164 { 1165 uint32_t c, crc, carry; 1166 size_t i, j; 1167 1168 crc = 0xffffffffU; /* initial value */ 1169 1170 for (i = 0; i < len; i++) { 1171 c = buf[i]; 1172 for (j = 0; j < 8; j++) { 1173 carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01); 1174 crc <<= 1; 1175 c >>= 1; 1176 if (carry) 1177 crc = (crc ^ ETHER_CRC_POLY_BE) | carry; 1178 } 1179 } 1180 1181 return (crc); 1182 } 1183 1184 /* 1185 * find the size of ethernet header, and call classifier 1186 */ 1187 void 1188 altq_etherclassify(struct ifaltq *ifq, struct mbuf *m, 1189 struct altq_pktattr *pktattr) 1190 { 1191 struct ether_header *eh; 1192 uint16_t ether_type; 1193 int hlen, af, hdrsize; 1194 caddr_t hdr; 1195 1196 hlen = sizeof(struct ether_header); 1197 eh = mtod(m, struct ether_header *); 1198 1199 ether_type = ntohs(eh->ether_type); 1200 if (ether_type < ETHERMTU) { 1201 /* ick! LLC/SNAP */ 1202 struct llc *llc = (struct llc *)(eh + 1); 1203 hlen += 8; 1204 1205 if (m->m_len < hlen || 1206 llc->llc_dsap != LLC_SNAP_LSAP || 1207 llc->llc_ssap != LLC_SNAP_LSAP || 1208 llc->llc_control != LLC_UI) 1209 goto bad; /* not snap! */ 1210 1211 ether_type = ntohs(llc->llc_un.type_snap.ether_type); 1212 } 1213 1214 if (ether_type == ETHERTYPE_IP) { 1215 af = AF_INET; 1216 hdrsize = 20; /* sizeof(struct ip) */ 1217 #ifdef INET6 1218 } else if (ether_type == ETHERTYPE_IPV6) { 1219 af = AF_INET6; 1220 hdrsize = 40; /* sizeof(struct ip6_hdr) */ 1221 #endif 1222 } else 1223 goto bad; 1224 1225 while (m->m_len <= hlen) { 1226 hlen -= m->m_len; 1227 m = m->m_next; 1228 } 1229 hdr = m->m_data + hlen; 1230 if (m->m_len < hlen + hdrsize) { 1231 /* 1232 * ip header is not in a single mbuf. this should not 1233 * happen in the current code. 1234 * (todo: use m_pulldown in the future) 1235 */ 1236 goto bad; 1237 } 1238 m->m_data += hlen; 1239 m->m_len -= hlen; 1240 ifq_classify(ifq, m, af, pktattr); 1241 m->m_data -= hlen; 1242 m->m_len += hlen; 1243 1244 return; 1245 1246 bad: 1247 pktattr->pattr_class = NULL; 1248 pktattr->pattr_hdr = NULL; 1249 pktattr->pattr_af = AF_UNSPEC; 1250 } 1251 1252 static void 1253 ether_restore_header(struct mbuf **m0, const struct ether_header *eh, 1254 const struct ether_header *save_eh) 1255 { 1256 struct mbuf *m = *m0; 1257 1258 ether_restore_hdr++; 1259 1260 /* 1261 * Prepend the header, optimize for the common case of 1262 * eh pointing into the mbuf. 1263 */ 1264 if ((const void *)(eh + 1) == (void *)m->m_data) { 1265 m->m_data -= ETHER_HDR_LEN; 1266 m->m_len += ETHER_HDR_LEN; 1267 m->m_pkthdr.len += ETHER_HDR_LEN; 1268 } else { 1269 ether_prepend_hdr++; 1270 1271 M_PREPEND(m, ETHER_HDR_LEN, MB_DONTWAIT); 1272 if (m != NULL) { 1273 bcopy(save_eh, mtod(m, struct ether_header *), 1274 ETHER_HDR_LEN); 1275 } 1276 } 1277 *m0 = m; 1278 } 1279 1280 #ifdef ETHER_INPUT_CHAIN 1281 1282 static void 1283 ether_input_ipifunc(void *arg) 1284 { 1285 struct mbuf *m, *next; 1286 lwkt_port_t port; 1287 1288 m = arg; 1289 do { 1290 next = m->m_nextpkt; 1291 m->m_nextpkt = NULL; 1292 1293 port = m->m_pkthdr.header; 1294 m->m_pkthdr.header = NULL; 1295 1296 lwkt_sendmsg(port, 1297 &m->m_hdr.mh_netmsg.nm_netmsg.nm_lmsg); 1298 1299 m = next; 1300 } while (m != NULL); 1301 } 1302 1303 void 1304 ether_input_dispatch(struct mbuf_chain *chain) 1305 { 1306 #ifdef SMP 1307 int i; 1308 1309 for (i = 0; i < ncpus; ++i) { 1310 if (chain[i].mc_head != NULL) { 1311 lwkt_send_ipiq(globaldata_find(i), 1312 ether_input_ipifunc, chain[i].mc_head); 1313 } 1314 } 1315 #else 1316 ether_input_ipifunc(chain->mc_head); 1317 #endif 1318 } 1319 1320 #endif /* ETHER_INPUT_CHAIN */ 1321