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