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