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$ 35 */ 36 37 #include "opt_atalk.h" 38 #include "opt_inet.h" 39 #include "opt_inet6.h" 40 #include "opt_ipx.h" 41 #include "opt_bdg.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/bpf.h> 60 61 #if defined(INET) || defined(INET6) 62 #include <netinet/in.h> 63 #include <netinet/in_var.h> 64 #include <netinet/if_ether.h> 65 #endif 66 #ifdef INET6 67 #include <netinet6/nd6.h> 68 #include <netinet6/in6_ifattach.h> 69 #endif 70 71 #ifdef IPX 72 #include <netipx/ipx.h> 73 #include <netipx/ipx_if.h> 74 int (*ef_inputp)(struct ifnet*, struct ether_header *eh, struct mbuf *m); 75 int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp, 76 struct sockaddr *dst, short *tp); 77 #endif 78 79 #ifdef NS 80 #include <netns/ns.h> 81 #include <netns/ns_if.h> 82 ushort ns_nettype; 83 int ether_outputdebug = 0; 84 int ether_inputdebug = 0; 85 #endif 86 87 #ifdef NETATALK 88 #include <netatalk/at.h> 89 #include <netatalk/at_var.h> 90 #include <netatalk/at_extern.h> 91 92 #define llc_snap_org_code llc_un.type_snap.org_code 93 #define llc_snap_ether_type llc_un.type_snap.ether_type 94 95 extern u_char at_org_code[3]; 96 extern u_char aarp_org_code[3]; 97 #endif /* NETATALK */ 98 99 #ifdef BRIDGE 100 #include <net/bridge.h> 101 #endif 102 103 #include "vlan.h" 104 #if NVLAN > 0 105 #include <net/if_vlan_var.h> 106 #endif /* NVLAN > 0 */ 107 108 static int ether_resolvemulti __P((struct ifnet *, struct sockaddr **, 109 struct sockaddr *)); 110 u_char etherbroadcastaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 111 #define senderr(e) do { error = (e); goto bad;} while (0) 112 #define IFP2AC(IFP) ((struct arpcom *)IFP) 113 114 #ifdef NETGRAPH 115 #include <netgraph/ng_ether.h> 116 #include <netgraph/ng_message.h> 117 #include <netgraph/netgraph.h> 118 119 static void ngether_init(void* ignored); 120 static void ngether_send(struct arpcom *ac, 121 struct ether_header *eh, struct mbuf *m); 122 static ng_constructor_t ngether_constructor; 123 static ng_rcvmsg_t ngether_rcvmsg; 124 static ng_shutdown_t ngether_rmnode; 125 static ng_newhook_t ngether_newhook; 126 static ng_connect_t ngether_connect; 127 static ng_rcvdata_t ngether_rcvdata; 128 static ng_disconnect_t ngether_disconnect; 129 130 static struct ng_type typestruct = { 131 NG_VERSION, 132 NG_ETHER_NODE_TYPE, 133 NULL, 134 ngether_constructor, 135 ngether_rcvmsg, 136 ngether_rmnode, 137 ngether_newhook, 138 NULL, 139 ngether_connect, 140 ngether_rcvdata, 141 ngether_rcvdata, 142 ngether_disconnect, 143 NULL 144 }; 145 146 #define AC2NG(AC) ((node_p)((AC)->ac_ng)) 147 #define NGEF_DIVERT NGF_TYPE1 /* all packets sent to netgraph */ 148 #endif /* NETGRAPH */ 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 int 158 ether_output(ifp, m, dst, rt0) 159 register struct ifnet *ifp; 160 struct mbuf *m; 161 struct sockaddr *dst; 162 struct rtentry *rt0; 163 { 164 short type; 165 int s, error = 0, hdrcmplt = 0; 166 u_char esrc[6], edst[6]; 167 register struct rtentry *rt; 168 register struct ether_header *eh; 169 int off, len = m->m_pkthdr.len, loop_copy = 0; 170 int hlen; /* link layer header lenght */ 171 struct arpcom *ac = IFP2AC(ifp); 172 173 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) 174 senderr(ENETDOWN); 175 rt = rt0; 176 if (rt) { 177 if ((rt->rt_flags & RTF_UP) == 0) { 178 rt0 = rt = rtalloc1(dst, 1, 0UL); 179 if (rt0) 180 rt->rt_refcnt--; 181 else 182 senderr(EHOSTUNREACH); 183 } 184 if (rt->rt_flags & RTF_GATEWAY) { 185 if (rt->rt_gwroute == 0) 186 goto lookup; 187 if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) { 188 rtfree(rt); rt = rt0; 189 lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1, 190 0UL); 191 if ((rt = rt->rt_gwroute) == 0) 192 senderr(EHOSTUNREACH); 193 } 194 } 195 if (rt->rt_flags & RTF_REJECT) 196 if (rt->rt_rmx.rmx_expire == 0 || 197 time_second < rt->rt_rmx.rmx_expire) 198 senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH); 199 } 200 hlen = ETHER_HDR_LEN; 201 switch (dst->sa_family) { 202 #ifdef INET 203 case AF_INET: 204 if (!arpresolve(ac, rt, m, dst, edst, rt0)) 205 return (0); /* if not yet resolved */ 206 off = m->m_pkthdr.len - m->m_len; 207 type = htons(ETHERTYPE_IP); 208 break; 209 #endif 210 #ifdef INET6 211 case AF_INET6: 212 if (!nd6_storelladdr(&ac->ac_if, rt, m, dst, (u_char *)edst)) { 213 /* this must be impossible, so we bark */ 214 printf("nd6_storelladdr failed\n"); 215 return(0); 216 } 217 off = m->m_pkthdr.len - m->m_len; 218 type = htons(ETHERTYPE_IPV6); 219 break; 220 #endif 221 #ifdef IPX 222 case AF_IPX: 223 if (ef_outputp) { 224 error = ef_outputp(ifp, &m, dst, &type); 225 if (error) 226 goto bad; 227 } else 228 type = htons(ETHERTYPE_IPX); 229 bcopy((caddr_t)&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host), 230 (caddr_t)edst, sizeof (edst)); 231 break; 232 #endif 233 #ifdef NETATALK 234 case AF_APPLETALK: 235 { 236 struct at_ifaddr *aa; 237 238 if ((aa = at_ifawithnet((struct sockaddr_at *)dst)) == NULL) { 239 goto bad; 240 } 241 if (!aarpresolve(ac, m, (struct sockaddr_at *)dst, edst)) 242 return (0); 243 /* 244 * In the phase 2 case, need to prepend an mbuf for the llc header. 245 * Since we must preserve the value of m, which is passed to us by 246 * value, we m_copy() the first mbuf, and use it for our llc header. 247 */ 248 if ( aa->aa_flags & AFA_PHASE2 ) { 249 struct llc llc; 250 251 M_PREPEND(m, sizeof(struct llc), M_WAIT); 252 len += sizeof(struct llc); 253 llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP; 254 llc.llc_control = LLC_UI; 255 bcopy(at_org_code, llc.llc_snap_org_code, sizeof(at_org_code)); 256 llc.llc_snap_ether_type = htons( ETHERTYPE_AT ); 257 bcopy(&llc, mtod(m, caddr_t), sizeof(struct llc)); 258 type = htons(m->m_pkthdr.len); 259 hlen = sizeof(struct llc) + ETHER_HDR_LEN; 260 } else { 261 type = htons(ETHERTYPE_AT); 262 } 263 break; 264 } 265 #endif NETATALK 266 #ifdef NS 267 case AF_NS: 268 switch(ns_nettype){ 269 default: 270 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */ 271 type = 0x8137; 272 break; 273 case 0x0: /* Novell 802.3 */ 274 type = htons( m->m_pkthdr.len); 275 break; 276 case 0xe0e0: /* Novell 802.2 and Token-Ring */ 277 M_PREPEND(m, 3, M_WAIT); 278 type = htons( m->m_pkthdr.len); 279 cp = mtod(m, u_char *); 280 *cp++ = 0xE0; 281 *cp++ = 0xE0; 282 *cp++ = 0x03; 283 break; 284 } 285 bcopy((caddr_t)&(((struct sockaddr_ns *)dst)->sns_addr.x_host), 286 (caddr_t)edst, sizeof (edst)); 287 /* 288 * XXX if ns_thishost is the same as the node's ethernet 289 * address then just the default code will catch this anyhow. 290 * So I'm not sure if this next clause should be here at all? 291 * [JRE] 292 */ 293 if (!bcmp((caddr_t)edst, (caddr_t)&ns_thishost, sizeof(edst))){ 294 m->m_pkthdr.rcvif = ifp; 295 schednetisr(NETISR_NS); 296 inq = &nsintrq; 297 s = splimp(); 298 if (IF_QFULL(inq)) { 299 IF_DROP(inq); 300 m_freem(m); 301 } else 302 IF_ENQUEUE(inq, m); 303 splx(s); 304 return (error); 305 } 306 if (!bcmp((caddr_t)edst, (caddr_t)&ns_broadhost, sizeof(edst))){ 307 m->m_flags |= M_BCAST; 308 } 309 break; 310 #endif /* NS */ 311 312 case pseudo_AF_HDRCMPLT: 313 hdrcmplt = 1; 314 eh = (struct ether_header *)dst->sa_data; 315 (void)memcpy(esrc, eh->ether_shost, sizeof (esrc)); 316 /* FALLTHROUGH */ 317 318 case AF_UNSPEC: 319 loop_copy = -1; /* if this is for us, don't do it */ 320 eh = (struct ether_header *)dst->sa_data; 321 (void)memcpy(edst, eh->ether_dhost, sizeof (edst)); 322 type = eh->ether_type; 323 break; 324 325 default: 326 printf("%s%d: can't handle af%d\n", ifp->if_name, ifp->if_unit, 327 dst->sa_family); 328 senderr(EAFNOSUPPORT); 329 } 330 331 /* 332 * Add local net header. If no space in first mbuf, 333 * allocate another. 334 */ 335 M_PREPEND(m, sizeof (struct ether_header), M_DONTWAIT); 336 if (m == 0) 337 senderr(ENOBUFS); 338 eh = mtod(m, struct ether_header *); 339 (void)memcpy(&eh->ether_type, &type, 340 sizeof(eh->ether_type)); 341 (void)memcpy(eh->ether_dhost, edst, sizeof (edst)); 342 if (hdrcmplt) 343 (void)memcpy(eh->ether_shost, esrc, 344 sizeof(eh->ether_shost)); 345 else 346 (void)memcpy(eh->ether_shost, ac->ac_enaddr, 347 sizeof(eh->ether_shost)); 348 349 /* 350 * If a simplex interface, and the packet is being sent to our 351 * Ethernet address or a broadcast address, loopback a copy. 352 * XXX To make a simplex device behave exactly like a duplex 353 * device, we should copy in the case of sending to our own 354 * ethernet address (thus letting the original actually appear 355 * on the wire). However, we don't do that here for security 356 * reasons and compatibility with the original behavior. 357 */ 358 if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) { 359 if ((m->m_flags & M_BCAST) || (loop_copy > 0)) { 360 struct mbuf *n = m_copy(m, 0, (int)M_COPYALL); 361 362 (void) if_simloop(ifp, n, dst->sa_family, hlen); 363 } else if (bcmp(eh->ether_dhost, 364 eh->ether_shost, ETHER_ADDR_LEN) == 0) { 365 (void) if_simloop(ifp, m, dst->sa_family, hlen); 366 return (0); /* XXX */ 367 } 368 } 369 370 #ifdef BRIDGE 371 if (do_bridge) { 372 struct ether_header hdr; 373 374 m->m_pkthdr.rcvif = NULL; 375 bcopy(mtod(m, struct ether_header *), &hdr, ETHER_HDR_LEN); 376 m_adj(m, ETHER_HDR_LEN); 377 ifp = bridge_dst_lookup(&hdr); 378 bdg_forward(&m, &hdr, ifp); 379 if (m != NULL) 380 m_freem(m); 381 return (0); 382 } 383 #endif 384 385 s = splimp(); 386 /* 387 * Queue message on interface, and start output if interface 388 * not yet active. 389 */ 390 if (IF_QFULL(&ifp->if_snd)) { 391 IF_DROP(&ifp->if_snd); 392 splx(s); 393 senderr(ENOBUFS); 394 } 395 IF_ENQUEUE(&ifp->if_snd, m); 396 if ((ifp->if_flags & IFF_OACTIVE) == 0) 397 (*ifp->if_start)(ifp); 398 splx(s); 399 ifp->if_obytes += len + sizeof (struct ether_header); 400 if (m->m_flags & M_MCAST) 401 ifp->if_omcasts++; 402 return (error); 403 404 bad: 405 if (m) 406 m_freem(m); 407 return (error); 408 } 409 410 /* 411 * Process a received Ethernet packet; 412 * the packet is in the mbuf chain m without 413 * the ether header, which is provided separately. 414 */ 415 void 416 ether_input(ifp, eh, m) 417 struct ifnet *ifp; 418 register struct ether_header *eh; 419 struct mbuf *m; 420 { 421 register struct ifqueue *inq; 422 u_short ether_type; 423 int s; 424 #if defined(NETATALK) 425 register struct llc *l; 426 #endif 427 428 /* Check for a BPF tap */ 429 if (ifp->if_bpf != NULL) { 430 struct m_hdr mh; 431 432 /* This kludge is OK; BPF treats the "mbuf" as read-only */ 433 mh.mh_next = m; 434 mh.mh_data = (char *)eh; 435 mh.mh_len = ETHER_HDR_LEN; 436 bpf_mtap(ifp, (struct mbuf *)&mh); 437 } 438 439 #ifdef BRIDGE 440 /* Check for bridging mode */ 441 if (do_bridge) { 442 struct ifnet *bif; 443 444 /* Check with bridging code */ 445 if ((bif = bridge_in(ifp, eh)) == BDG_DROP) { 446 m_freem(m); 447 return; 448 } 449 if (bif != BDG_LOCAL) 450 bdg_forward(&m, eh, bif); /* needs forwarding */ 451 if (bif == BDG_LOCAL 452 || bif == BDG_BCAST 453 || bif == BDG_MCAST) 454 goto recvLocal; /* receive locally */ 455 456 /* If not local and not multicast, just drop it */ 457 if (m != NULL) 458 m_freem(m); 459 return; 460 } 461 #endif 462 463 /* Discard packet if upper layers shouldn't see it. This should 464 only happen when the interface is in promiscuous mode. */ 465 if ((ifp->if_flags & IFF_PROMISC) != 0 466 && (eh->ether_dhost[0] & 1) == 0 467 && bcmp(eh->ether_dhost, 468 IFP2AC(ifp)->ac_enaddr, ETHER_ADDR_LEN) != 0) { 469 m_freem(m); 470 return; 471 } 472 473 #ifdef BRIDGE 474 recvLocal: 475 #endif 476 /* Discard packet if interface is not up */ 477 if ((ifp->if_flags & IFF_UP) == 0) { 478 m_freem(m); 479 return; 480 } 481 ifp->if_ibytes += m->m_pkthdr.len + sizeof (*eh); 482 if (eh->ether_dhost[0] & 1) { 483 if (bcmp((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost, 484 sizeof(etherbroadcastaddr)) == 0) 485 m->m_flags |= M_BCAST; 486 else 487 m->m_flags |= M_MCAST; 488 } 489 if (m->m_flags & (M_BCAST|M_MCAST)) 490 ifp->if_imcasts++; 491 492 ether_type = ntohs(eh->ether_type); 493 494 #ifdef NETGRAPH 495 { 496 struct arpcom *ac = IFP2AC(ifp); 497 if (AC2NG(ac) && (AC2NG(ac)->flags & NGEF_DIVERT)) { 498 ngether_send(ac, eh, m); 499 return; 500 } 501 } 502 #endif /* NETGRAPH */ 503 504 #if NVLAN > 0 505 if (ether_type == vlan_proto) { 506 if (vlan_input(eh, m) < 0) 507 ifp->if_data.ifi_noproto++; 508 return; 509 } 510 #endif /* NVLAN > 0 */ 511 512 switch (ether_type) { 513 #ifdef INET 514 case ETHERTYPE_IP: 515 if (ipflow_fastforward(m)) 516 return; 517 schednetisr(NETISR_IP); 518 inq = &ipintrq; 519 break; 520 521 case ETHERTYPE_ARP: 522 schednetisr(NETISR_ARP); 523 inq = &arpintrq; 524 break; 525 #endif 526 #ifdef IPX 527 case ETHERTYPE_IPX: 528 if (ef_inputp && ef_inputp(ifp, eh, m) == 0) 529 return; 530 schednetisr(NETISR_IPX); 531 inq = &ipxintrq; 532 break; 533 #endif 534 #ifdef INET6 535 case ETHERTYPE_IPV6: 536 schednetisr(NETISR_IPV6); 537 inq = &ip6intrq; 538 break; 539 #endif 540 #ifdef NS 541 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */ 542 schednetisr(NETISR_NS); 543 inq = &nsintrq; 544 break; 545 546 #endif /* NS */ 547 #ifdef NETATALK 548 case ETHERTYPE_AT: 549 schednetisr(NETISR_ATALK); 550 inq = &atintrq1; 551 break; 552 case ETHERTYPE_AARP: 553 /* probably this should be done with a NETISR as well */ 554 aarpinput(IFP2AC(ifp), m); /* XXX */ 555 return; 556 #endif NETATALK 557 default: 558 #ifdef IPX 559 if (ef_inputp && ef_inputp(ifp, eh, m) == 0) 560 return; 561 #endif /* IPX */ 562 #ifdef NS 563 checksum = mtod(m, ushort *); 564 /* Novell 802.3 */ 565 if ((ether_type <= ETHERMTU) && 566 ((*checksum == 0xffff) || (*checksum == 0xE0E0))){ 567 if(*checksum == 0xE0E0) { 568 m->m_pkthdr.len -= 3; 569 m->m_len -= 3; 570 m->m_data += 3; 571 } 572 schednetisr(NETISR_NS); 573 inq = &nsintrq; 574 break; 575 } 576 #endif /* NS */ 577 #if defined(NETATALK) 578 if (ether_type > ETHERMTU) 579 goto dropanyway; 580 l = mtod(m, struct llc *); 581 switch (l->llc_dsap) { 582 case LLC_SNAP_LSAP: 583 switch (l->llc_control) { 584 case LLC_UI: 585 if (l->llc_ssap != LLC_SNAP_LSAP) 586 goto dropanyway; 587 588 if (Bcmp(&(l->llc_snap_org_code)[0], at_org_code, 589 sizeof(at_org_code)) == 0 && 590 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) { 591 inq = &atintrq2; 592 m_adj( m, sizeof( struct llc )); 593 schednetisr(NETISR_ATALK); 594 break; 595 } 596 597 if (Bcmp(&(l->llc_snap_org_code)[0], aarp_org_code, 598 sizeof(aarp_org_code)) == 0 && 599 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) { 600 m_adj( m, sizeof( struct llc )); 601 aarpinput(IFP2AC(ifp), m); /* XXX */ 602 return; 603 } 604 605 default: 606 goto dropanyway; 607 } 608 break; 609 dropanyway: 610 default: 611 #ifdef NETGRAPH 612 ngether_send(IFP2AC(ifp), eh, m); 613 #else /* NETGRAPH */ 614 m_freem(m); 615 #endif /* NETGRAPH */ 616 return; 617 } 618 #else /* NETATALK */ 619 #ifdef NETGRAPH 620 ngether_send(IFP2AC(ifp), eh, m); 621 #else /* NETGRAPH */ 622 m_freem(m); 623 #endif /* NETGRAPH */ 624 return; 625 #endif /* NETATALK */ 626 } 627 628 s = splimp(); 629 if (IF_QFULL(inq)) { 630 IF_DROP(inq); 631 m_freem(m); 632 } else 633 IF_ENQUEUE(inq, m); 634 splx(s); 635 } 636 637 /* 638 * Perform common duties while attaching to interface list 639 */ 640 void 641 ether_ifattach(ifp) 642 register struct ifnet *ifp; 643 { 644 register struct ifaddr *ifa; 645 register struct sockaddr_dl *sdl; 646 647 ifp->if_type = IFT_ETHER; 648 ifp->if_addrlen = 6; 649 ifp->if_hdrlen = 14; 650 ifp->if_mtu = ETHERMTU; 651 ifp->if_resolvemulti = ether_resolvemulti; 652 if (ifp->if_baudrate == 0) 653 ifp->if_baudrate = 10000000; 654 ifa = ifnet_addrs[ifp->if_index - 1]; 655 if (ifa == 0) { 656 printf("ether_ifattach: no lladdr!\n"); 657 return; 658 } 659 sdl = (struct sockaddr_dl *)ifa->ifa_addr; 660 sdl->sdl_type = IFT_ETHER; 661 sdl->sdl_alen = ifp->if_addrlen; 662 bcopy((IFP2AC(ifp))->ac_enaddr, LLADDR(sdl), ifp->if_addrlen); 663 #ifdef NETGRAPH 664 ngether_init(ifp); 665 #endif /* NETGRAPH */ 666 #ifdef INET6 667 in6_ifattach_getifid(ifp); 668 #endif 669 } 670 671 SYSCTL_DECL(_net_link); 672 SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet"); 673 674 int 675 ether_ioctl(ifp, command, data) 676 struct ifnet *ifp; 677 int command; 678 caddr_t data; 679 { 680 struct ifaddr *ifa = (struct ifaddr *) data; 681 struct ifreq *ifr = (struct ifreq *) data; 682 int error = 0; 683 684 switch (command) { 685 case SIOCSIFADDR: 686 ifp->if_flags |= IFF_UP; 687 688 switch (ifa->ifa_addr->sa_family) { 689 #ifdef INET 690 case AF_INET: 691 ifp->if_init(ifp->if_softc); /* before arpwhohas */ 692 arp_ifinit(IFP2AC(ifp), ifa); 693 break; 694 #endif 695 #ifdef IPX 696 /* 697 * XXX - This code is probably wrong 698 */ 699 case AF_IPX: 700 { 701 register struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr); 702 struct arpcom *ac = IFP2AC(ifp); 703 704 if (ipx_nullhost(*ina)) 705 ina->x_host = 706 *(union ipx_host *) 707 ac->ac_enaddr; 708 else { 709 bcopy((caddr_t) ina->x_host.c_host, 710 (caddr_t) ac->ac_enaddr, 711 sizeof(ac->ac_enaddr)); 712 } 713 714 /* 715 * Set new address 716 */ 717 ifp->if_init(ifp->if_softc); 718 break; 719 } 720 #endif 721 #ifdef NS 722 /* 723 * XXX - This code is probably wrong 724 */ 725 case AF_NS: 726 { 727 register struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr); 728 struct arpcom *ac = IFP2AC(ifp); 729 730 if (ns_nullhost(*ina)) 731 ina->x_host = 732 *(union ns_host *) (ac->ac_enaddr); 733 else { 734 bcopy((caddr_t) ina->x_host.c_host, 735 (caddr_t) ac->ac_enaddr, 736 sizeof(ac->ac_enaddr)); 737 } 738 739 /* 740 * Set new address 741 */ 742 ifp->if_init(ifp->if_softc); 743 break; 744 } 745 #endif 746 default: 747 ifp->if_init(ifp->if_softc); 748 break; 749 } 750 break; 751 752 case SIOCGIFADDR: 753 { 754 struct sockaddr *sa; 755 756 sa = (struct sockaddr *) & ifr->ifr_data; 757 bcopy(IFP2AC(ifp)->ac_enaddr, 758 (caddr_t) sa->sa_data, ETHER_ADDR_LEN); 759 } 760 break; 761 762 case SIOCSIFMTU: 763 /* 764 * Set the interface MTU. 765 */ 766 if (ifr->ifr_mtu > ETHERMTU) { 767 error = EINVAL; 768 } else { 769 ifp->if_mtu = ifr->ifr_mtu; 770 } 771 break; 772 } 773 return (error); 774 } 775 776 int 777 ether_resolvemulti(ifp, llsa, sa) 778 struct ifnet *ifp; 779 struct sockaddr **llsa; 780 struct sockaddr *sa; 781 { 782 struct sockaddr_dl *sdl; 783 struct sockaddr_in *sin; 784 #ifdef INET6 785 struct sockaddr_in6 *sin6; 786 #endif 787 u_char *e_addr; 788 789 switch(sa->sa_family) { 790 case AF_LINK: 791 /* 792 * No mapping needed. Just check that it's a valid MC address. 793 */ 794 sdl = (struct sockaddr_dl *)sa; 795 e_addr = LLADDR(sdl); 796 if ((e_addr[0] & 1) != 1) 797 return EADDRNOTAVAIL; 798 *llsa = 0; 799 return 0; 800 801 #ifdef INET 802 case AF_INET: 803 sin = (struct sockaddr_in *)sa; 804 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) 805 return EADDRNOTAVAIL; 806 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR, 807 M_WAITOK); 808 sdl->sdl_len = sizeof *sdl; 809 sdl->sdl_family = AF_LINK; 810 sdl->sdl_index = ifp->if_index; 811 sdl->sdl_type = IFT_ETHER; 812 sdl->sdl_nlen = 0; 813 sdl->sdl_alen = ETHER_ADDR_LEN; 814 sdl->sdl_slen = 0; 815 e_addr = LLADDR(sdl); 816 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr); 817 *llsa = (struct sockaddr *)sdl; 818 return 0; 819 #endif 820 #ifdef INET6 821 case AF_INET6: 822 sin6 = (struct sockaddr_in6 *)sa; 823 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) 824 return EADDRNOTAVAIL; 825 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR, 826 M_WAITOK); 827 sdl->sdl_len = sizeof *sdl; 828 sdl->sdl_family = AF_LINK; 829 sdl->sdl_index = ifp->if_index; 830 sdl->sdl_type = IFT_ETHER; 831 sdl->sdl_nlen = 0; 832 sdl->sdl_alen = ETHER_ADDR_LEN; 833 sdl->sdl_slen = 0; 834 e_addr = LLADDR(sdl); 835 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr); 836 *llsa = (struct sockaddr *)sdl; 837 return 0; 838 #endif 839 840 default: 841 /* 842 * Well, the text isn't quite right, but it's the name 843 * that counts... 844 */ 845 return EAFNOSUPPORT; 846 } 847 } 848 849 #ifdef NETGRAPH 850 851 /*********************************************************************** 852 * This section contains the methods for the Netgraph interface 853 ***********************************************************************/ 854 /* It's Ascii-art time! 855 * The ifnet is the first part of the arpcom which must be 856 * the first part of the device's softc.. yuk. 857 * 858 * +--------------------------+-----+---------+ 859 * | struct ifnet (*ifp) | | | 860 * | | | | 861 * +--------------------------+ | | 862 * +--|[ac_ng] struct arpcom (*ac) | | 863 * | +--------------------------------+ | 864 * | | struct softc (*ifp->if_softc) (device) | 865 * | +------------------------------------------+ 866 * | ^ 867 * AC2NG() | 868 * | v 869 * | +----------------------+ 870 * | | [private] [flags] | 871 * +------>| struct ng_node | 872 * | [hooks] | ** we only allow one hook 873 * +----------------------+ 874 * ^ 875 * | 876 * v 877 * +-------------+ 878 * | [node] | 879 * | hook | 880 * | [private]|-- *unused* 881 * +-------------+ 882 */ 883 884 /* 885 * called during interface attaching 886 */ 887 static void 888 ngether_init(void *ifpvoid) 889 { 890 struct ifnet *ifp = ifpvoid; 891 struct arpcom *ac = IFP2AC(ifp); 892 static int ngether_done_init; 893 char namebuf[32]; 894 node_p node; 895 896 /* 897 * we have found a node, make sure our 'type' is availabe. 898 */ 899 if (ngether_done_init == 0) { 900 if (ng_newtype(&typestruct)) { 901 printf("ngether install failed\n"); 902 return; 903 } 904 ngether_done_init = 1; 905 } 906 if (ng_make_node_common(&typestruct, &node) != 0) 907 return; 908 ac->ac_ng = node; 909 node->private = ifp; 910 sprintf(namebuf, "%s%d", ifp->if_name, ifp->if_unit); 911 ng_name_node(AC2NG(ac), namebuf); 912 } 913 914 /* 915 * It is not possible or allowable to create a node of this type. 916 * If the hardware exists, it will already have created it. 917 */ 918 static int 919 ngether_constructor(node_p *nodep) 920 { 921 return (EINVAL); 922 } 923 924 /* 925 * Give our ok for a hook to be added... 926 * 927 * Allow one hook at a time (rawdata). 928 * It can eiteh rdivert everything or only unclaimed packets. 929 */ 930 static int 931 ngether_newhook(node_p node, hook_p hook, const char *name) 932 { 933 934 /* check if there is already a hook */ 935 if (LIST_FIRST(&(node->hooks))) 936 return(EISCONN); 937 /* 938 * Check for which mode hook we want. 939 */ 940 if (strcmp(name, NG_ETHER_HOOK_ORPHAN) != 0) { 941 if (strcmp(name, NG_ETHER_HOOK_DIVERT) != 0) { 942 return (EINVAL); 943 } 944 node->flags |= NGEF_DIVERT; 945 } else { 946 node->flags &= ~NGEF_DIVERT; 947 } 948 return (0); 949 } 950 951 /* 952 * incoming messages. 953 * Just respond to the generic TEXT_STATUS message 954 */ 955 static int 956 ngether_rcvmsg(node_p node, struct ng_mesg *msg, const char *retaddr, 957 struct ng_mesg **resp, hook_p lasthook) 958 { 959 struct ifnet *ifp; 960 int error = 0; 961 962 ifp = node->private; 963 switch (msg->header.typecookie) { 964 case NGM_ETHER_COOKIE: 965 error = EINVAL; 966 break; 967 case NGM_GENERIC_COOKIE: 968 switch(msg->header.cmd) { 969 case NGM_TEXT_STATUS: { 970 char *arg; 971 int pos = 0; 972 int resplen = sizeof(struct ng_mesg) + 512; 973 MALLOC(*resp, struct ng_mesg *, resplen, 974 M_NETGRAPH, M_NOWAIT); 975 if (*resp == NULL) { 976 error = ENOMEM; 977 break; 978 } 979 bzero(*resp, resplen); 980 arg = (*resp)->data; 981 982 /* 983 * Put in the throughput information. 984 */ 985 pos = sprintf(arg, "%ld bytes in, %ld bytes out\n", 986 ifp->if_ibytes, ifp->if_obytes); 987 pos += sprintf(arg + pos, 988 "%ld output errors\n", 989 ifp->if_oerrors); 990 pos += sprintf(arg + pos, 991 "ierrors = %ld\n", 992 ifp->if_ierrors); 993 994 (*resp)->header.version = NG_VERSION; 995 (*resp)->header.arglen = strlen(arg) + 1; 996 (*resp)->header.token = msg->header.token; 997 (*resp)->header.typecookie = NGM_ETHER_COOKIE; 998 (*resp)->header.cmd = msg->header.cmd; 999 strncpy((*resp)->header.cmdstr, "status", 1000 NG_CMDSTRLEN); 1001 } 1002 break; 1003 default: 1004 error = EINVAL; 1005 break; 1006 } 1007 break; 1008 default: 1009 error = EINVAL; 1010 break; 1011 } 1012 free(msg, M_NETGRAPH); 1013 return (error); 1014 } 1015 1016 /* 1017 * Receive a completed ethernet packet. 1018 * Queue it for output. 1019 */ 1020 static int 1021 ngether_rcvdata(hook_p hook, struct mbuf *m, meta_p meta, 1022 struct mbuf **ret_m, meta_p *ret_meta) 1023 { 1024 struct ifnet *ifp; 1025 int error = 0; 1026 int s; 1027 struct ether_header *eh; 1028 1029 ifp = hook->node->private; 1030 1031 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) 1032 senderr(ENETDOWN); 1033 /* drop in the MAC address */ 1034 eh = mtod(m, struct ether_header *); 1035 bcopy(IFP2AC(ifp)->ac_enaddr, eh->ether_shost, 6); 1036 /* 1037 * If a simplex interface, and the packet is being sent to our 1038 * Ethernet address or a broadcast address, loopback a copy. 1039 * XXX To make a simplex device behave exactly like a duplex 1040 * device, we should copy in the case of sending to our own 1041 * ethernet address (thus letting the original actually appear 1042 * on the wire). However, we don't do that here for security 1043 * reasons and compatibility with the original behavior. 1044 */ 1045 if (ifp->if_flags & IFF_SIMPLEX) { 1046 if (m->m_flags & M_BCAST) { 1047 struct mbuf *n = m_copy(m, 0, (int)M_COPYALL); 1048 1049 ng_queue_data(hook, n, meta); 1050 } else if (bcmp(eh->ether_dhost, 1051 eh->ether_shost, ETHER_ADDR_LEN) == 0) { 1052 ng_queue_data(hook, m, meta); 1053 return (0); /* XXX */ 1054 } 1055 } 1056 s = splimp(); 1057 /* 1058 * Queue message on interface, and start output if interface 1059 * not yet active. 1060 * XXX if we lookead at the priority in the meta data we could 1061 * queue high priority items at the head. 1062 */ 1063 if (IF_QFULL(&ifp->if_snd)) { 1064 IF_DROP(&ifp->if_snd); 1065 splx(s); 1066 senderr(ENOBUFS); 1067 } 1068 IF_ENQUEUE(&ifp->if_snd, m); 1069 if ((ifp->if_flags & IFF_OACTIVE) == 0) 1070 (*ifp->if_start)(ifp); 1071 splx(s); 1072 ifp->if_obytes += m->m_pkthdr.len; 1073 if (m->m_flags & M_MCAST) 1074 ifp->if_omcasts++; 1075 return (error); 1076 1077 bad: 1078 NG_FREE_DATA(m, meta); 1079 return (error); 1080 } 1081 1082 /* 1083 * pass an mbuf out to the connected hook 1084 * More complicated than just an m_prepend, as it tries to save later nodes 1085 * from needing to do lots of m_pullups. 1086 */ 1087 static void 1088 ngether_send(struct arpcom *ac, struct ether_header *eh, struct mbuf *m) 1089 { 1090 int room; 1091 node_p node = AC2NG(ac); 1092 struct ether_header *eh2; 1093 1094 if (node && LIST_FIRST(&(node->hooks))) { 1095 /* 1096 * Possibly the header is already on the front, 1097 */ 1098 eh2 = mtod(m, struct ether_header *) - 1; 1099 if ( eh == eh2) { 1100 /* 1101 * This is the case so just move the markers back to 1102 * re-include it. We lucked out. 1103 * This allows us to avoid a yucky m_pullup 1104 * in later nodes if it works. 1105 */ 1106 m->m_len += sizeof(*eh); 1107 m->m_data -= sizeof(*eh); 1108 m->m_pkthdr.len += sizeof(*eh); 1109 } else { 1110 /* 1111 * Alternatively there may be room even though 1112 * it is stored somewhere else. If so, copy it in. 1113 * This only safe because we KNOW that this packet has 1114 * just been generated by an ethernet card, so there 1115 * are no aliases to the buffer. (unlike in outgoing 1116 * packets). 1117 * Nearly all ethernet cards will end up producing mbufs 1118 * that fall into these cases. So we are not optimising 1119 * contorted cases. 1120 */ 1121 1122 if (m->m_flags & M_EXT) { 1123 room = (mtod(m, caddr_t) - m->m_ext.ext_buf); 1124 if (room > m->m_ext.ext_size) /* garbage */ 1125 room = 0; /* fail immediatly */ 1126 } else { 1127 room = (mtod(m, caddr_t) - m->m_pktdat); 1128 } 1129 if (room > sizeof (*eh)) { 1130 /* we have room, just copy it and adjust */ 1131 m->m_len += sizeof(*eh); 1132 m->m_data -= sizeof(*eh); 1133 m->m_pkthdr.len += sizeof(*eh); 1134 } else { 1135 /* 1136 * Doing anything more is likely to get more 1137 * expensive than it's worth.. 1138 * it's probable that everything else is in one 1139 * big lump. The next node will do an m_pullup() 1140 * for exactly the amount of data it needs and 1141 * hopefully everything after that will not 1142 * need one. So let's just use M_PREPEND. 1143 */ 1144 M_PREPEND(m, sizeof (*eh), M_DONTWAIT); 1145 if (m == NULL) 1146 return; 1147 } 1148 bcopy ((caddr_t)eh, mtod(m, struct ether_header *), 1149 sizeof(*eh)); 1150 } 1151 ng_queue_data(LIST_FIRST(&(node->hooks)), m, NULL); 1152 } else { 1153 m_freem(m); 1154 } 1155 } 1156 1157 /* 1158 * do local shutdown processing.. 1159 * This node will refuse to go away, unless the hardware says to.. 1160 * don't unref the node, or remove our name. just clear our links up. 1161 */ 1162 static int 1163 ngether_rmnode(node_p node) 1164 { 1165 ng_cutlinks(node); 1166 node->flags &= ~NG_INVALID; /* bounce back to life */ 1167 return (0); 1168 } 1169 1170 /* already linked */ 1171 static int 1172 ngether_connect(hook_p hook) 1173 { 1174 /* be really amiable and just say "YUP that's OK by me! " */ 1175 return (0); 1176 } 1177 1178 /* 1179 * notify on hook disconnection (destruction) 1180 * 1181 * For this type, removal of the last lins no effect. The interface can run 1182 * independently. 1183 * Since we have no per-hook information, this is rather simple. 1184 */ 1185 static int 1186 ngether_disconnect(hook_p hook) 1187 { 1188 hook->node->flags &= ~NGEF_DIVERT; 1189 return (0); 1190 } 1191 #endif /* NETGRAPH */ 1192 1193 /********************************** END *************************************/ 1194