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