if_fddisubr.c revision 93373
1/* 2 * Copyright (c) 1995, 1996 3 * Matt Thomas <matt@3am-software.com>. All rights reserved. 4 * Copyright (c) 1982, 1989, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 * 35 * from: if_ethersubr.c,v 1.5 1994/12/13 22:31:45 wollman Exp 36 * $FreeBSD: head/sys/net/if_fddisubr.c 93373 2002-03-29 08:51:42Z mdodd $ 37 */ 38 39#include "opt_atalk.h" 40#include "opt_inet.h" 41#include "opt_inet6.h" 42#include "opt_ipx.h" 43 44#include <sys/param.h> 45#include <sys/systm.h> 46#include <sys/mbuf.h> 47#include <sys/socket.h> 48#include <sys/malloc.h> 49 50#include <net/if.h> 51#include <net/netisr.h> 52#include <net/route.h> 53#include <net/if_llc.h> 54#include <net/if_dl.h> 55#include <net/if_types.h> 56#include <net/fddi.h> 57 58#if defined(INET) || defined(INET6) 59#include <netinet/in.h> 60#include <netinet/in_var.h> 61#include <netinet/if_ether.h> 62#endif 63#ifdef INET6 64#include <netinet6/nd6.h> 65#endif 66 67#ifdef IPX 68#include <netipx/ipx.h> 69#include <netipx/ipx_if.h> 70#endif 71 72#ifdef NS 73#include <netns/ns.h> 74#include <netns/ns_if.h> 75#endif 76 77#ifdef DECNET 78#include <netdnet/dn.h> 79#endif 80 81#ifdef NETATALK 82#include <netatalk/at.h> 83#include <netatalk/at_var.h> 84#include <netatalk/at_extern.h> 85 86extern u_char at_org_code[ 3 ]; 87extern u_char aarp_org_code[ 3 ]; 88#endif /* NETATALK */ 89 90static int fddi_resolvemulti(struct ifnet *, struct sockaddr **, 91 struct sockaddr *); 92 93#define IFP2AC(IFP) ((struct arpcom *)IFP) 94#define senderr(e) { error = (e); goto bad; } 95 96/* 97 * FDDI output routine. 98 * Encapsulate a packet of type family for the local net. 99 * Use trailer local net encapsulation if enough data in first 100 * packet leaves a multiple of 512 bytes of data in remainder. 101 * Assumes that ifp is actually pointer to arpcom structure. 102 */ 103int 104fddi_output(ifp, m, dst, rt0) 105 struct ifnet *ifp; 106 struct mbuf *m; 107 struct sockaddr *dst; 108 struct rtentry *rt0; 109{ 110 u_int16_t type; 111 int loop_copy = 0, error = 0, hdrcmplt = 0; 112 u_char esrc[FDDI_ADDR_LEN], edst[FDDI_ADDR_LEN]; 113 struct rtentry *rt; 114 struct fddi_header *fh; 115 struct arpcom *ac = IFP2AC(ifp); 116 117 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) 118 senderr(ENETDOWN); 119 getmicrotime(&ifp->if_lastchange); 120 if ((rt = rt0) != NULL) { 121 if ((rt->rt_flags & RTF_UP) == 0) { 122 if ((rt0 = rt = rtalloc1(dst, 1, 0UL)) != NULL) 123 rt->rt_refcnt--; 124 else 125 senderr(EHOSTUNREACH); 126 } 127 if (rt->rt_flags & RTF_GATEWAY) { 128 if (rt->rt_gwroute == 0) 129 goto lookup; 130 if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) { 131 rtfree(rt); rt = rt0; 132 lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1, 0UL); 133 if ((rt = rt->rt_gwroute) == 0) 134 senderr(EHOSTUNREACH); 135 } 136 } 137 if (rt->rt_flags & RTF_REJECT) 138 if (rt->rt_rmx.rmx_expire == 0 || 139 time_second < rt->rt_rmx.rmx_expire) 140 senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH); 141 } 142 switch (dst->sa_family) { 143 144#ifdef INET 145 case AF_INET: { 146 if (!arpresolve(ifp, rt, m, dst, edst, rt0)) 147 return (0); /* if not yet resolved */ 148 type = htons(ETHERTYPE_IP); 149 break; 150 } 151#endif 152#ifdef INET6 153 case AF_INET6: 154 if (!nd6_storelladdr(&ac->ac_if, rt, m, dst, (u_char *)edst)) { 155 /* Something bad happened */ 156 return (0); 157 } 158 type = htons(ETHERTYPE_IPV6); 159 break; 160#endif 161#ifdef IPX 162 case AF_IPX: 163 type = htons(ETHERTYPE_IPX); 164 bcopy((caddr_t)&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host), 165 (caddr_t)edst, FDDI_ADDR_LEN); 166 break; 167#endif 168#ifdef NETATALK 169 case AF_APPLETALK: { 170 struct at_ifaddr *aa; 171 if (!aarpresolve(ac, m, (struct sockaddr_at *)dst, edst)) 172 return (0); 173 /* 174 * ifaddr is the first thing in at_ifaddr 175 */ 176 if ((aa = at_ifawithnet( (struct sockaddr_at *)dst)) == 0) 177 goto bad; 178 179 /* 180 * In the phase 2 case, we need to prepend an mbuf for the llc header. 181 * Since we must preserve the value of m, which is passed to us by 182 * value, we m_copy() the first mbuf, and use it for our llc header. 183 */ 184 if (aa->aa_flags & AFA_PHASE2) { 185 struct llc llc; 186 187 M_PREPEND(m, LLC_SNAPFRAMELEN, M_TRYWAIT); 188 if (m == 0) 189 senderr(ENOBUFS); 190 llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP; 191 llc.llc_control = LLC_UI; 192 bcopy(at_org_code, llc.llc_snap.org_code, sizeof(at_org_code)); 193 llc.llc_snap.ether_type = htons(ETHERTYPE_AT); 194 bcopy(&llc, mtod(m, caddr_t), LLC_SNAPFRAMELEN); 195 type = 0; 196 } else { 197 type = htons(ETHERTYPE_AT); 198 } 199 break; 200 } 201#endif /* NETATALK */ 202#ifdef NS 203 case AF_NS: 204 type = htons(ETHERTYPE_NS); 205 bcopy((caddr_t)&(((struct sockaddr_ns *)dst)->sns_addr.x_host), 206 (caddr_t)edst, FDDI_ADDR_LEN); 207 break; 208#endif 209 210 case pseudo_AF_HDRCMPLT: 211 { 212 struct ether_header *eh; 213 hdrcmplt = 1; 214 eh = (struct ether_header *)dst->sa_data; 215 (void)memcpy((caddr_t)esrc, (caddr_t)eh->ether_shost, FDDI_ADDR_LEN); 216 /* FALLTHROUGH */ 217 } 218 219 case AF_UNSPEC: 220 { 221 struct ether_header *eh; 222 loop_copy = -1; 223 eh = (struct ether_header *)dst->sa_data; 224 (void)memcpy((caddr_t)edst, (caddr_t)eh->ether_dhost, FDDI_ADDR_LEN); 225 if (*edst & 1) 226 m->m_flags |= (M_BCAST|M_MCAST); 227 type = eh->ether_type; 228 break; 229 } 230 231 case AF_IMPLINK: 232 { 233 fh = mtod(m, struct fddi_header *); 234 error = EPROTONOSUPPORT; 235 switch (fh->fddi_fc & (FDDIFC_C|FDDIFC_L|FDDIFC_F)) { 236 case FDDIFC_LLC_ASYNC: { 237 /* legal priorities are 0 through 7 */ 238 if ((fh->fddi_fc & FDDIFC_Z) > 7) 239 goto bad; 240 break; 241 } 242 case FDDIFC_LLC_SYNC: { 243 /* FDDIFC_Z bits reserved, must be zero */ 244 if (fh->fddi_fc & FDDIFC_Z) 245 goto bad; 246 break; 247 } 248 case FDDIFC_SMT: { 249 /* FDDIFC_Z bits must be non zero */ 250 if ((fh->fddi_fc & FDDIFC_Z) == 0) 251 goto bad; 252 break; 253 } 254 default: { 255 /* anything else is too dangerous */ 256 goto bad; 257 } 258 } 259 error = 0; 260 if (fh->fddi_dhost[0] & 1) 261 m->m_flags |= (M_BCAST|M_MCAST); 262 goto queue_it; 263 } 264 default: 265 printf("%s%d: can't handle af%d\n", ifp->if_name, ifp->if_unit, 266 dst->sa_family); 267 senderr(EAFNOSUPPORT); 268 } 269 270 if (type != 0) { 271 struct llc *l; 272 M_PREPEND(m, LLC_SNAPFRAMELEN, M_DONTWAIT); 273 if (m == 0) 274 senderr(ENOBUFS); 275 l = mtod(m, struct llc *); 276 l->llc_control = LLC_UI; 277 l->llc_dsap = l->llc_ssap = LLC_SNAP_LSAP; 278 l->llc_snap.org_code[0] = l->llc_snap.org_code[1] = l->llc_snap.org_code[2] = 0; 279 (void)memcpy((caddr_t) &l->llc_snap.ether_type, (caddr_t) &type, 280 sizeof(u_int16_t)); 281 } 282 283 /* 284 * Add local net header. If no space in first mbuf, 285 * allocate another. 286 */ 287 M_PREPEND(m, FDDI_HDR_LEN, M_DONTWAIT); 288 if (m == 0) 289 senderr(ENOBUFS); 290 fh = mtod(m, struct fddi_header *); 291 fh->fddi_fc = FDDIFC_LLC_ASYNC|FDDIFC_LLC_PRIO4; 292 (void)memcpy((caddr_t)fh->fddi_dhost, (caddr_t)edst, FDDI_ADDR_LEN); 293 queue_it: 294 if (hdrcmplt) 295 (void)memcpy((caddr_t)fh->fddi_shost, (caddr_t)esrc, 296 FDDI_ADDR_LEN); 297 else 298 (void)memcpy((caddr_t)fh->fddi_shost, (caddr_t)ac->ac_enaddr, 299 FDDI_ADDR_LEN); 300 /* 301 * If a simplex interface, and the packet is being sent to our 302 * Ethernet address or a broadcast address, loopback a copy. 303 * XXX To make a simplex device behave exactly like a duplex 304 * device, we should copy in the case of sending to our own 305 * ethernet address (thus letting the original actually appear 306 * on the wire). However, we don't do that here for security 307 * reasons and compatibility with the original behavior. 308 */ 309 if ((ifp->if_flags & IFF_SIMPLEX) && 310 (loop_copy != -1)) { 311 if ((m->m_flags & M_BCAST) || loop_copy) { 312 struct mbuf *n = m_copy(m, 0, (int)M_COPYALL); 313 314 (void) if_simloop(ifp, 315 n, dst->sa_family, FDDI_HDR_LEN); 316 } else if (bcmp(fh->fddi_dhost, 317 fh->fddi_shost, FDDI_ADDR_LEN) == 0) { 318 (void) if_simloop(ifp, 319 m, dst->sa_family, FDDI_HDR_LEN); 320 return (0); /* XXX */ 321 } 322 } 323 324 if (! IF_HANDOFF(&ifp->if_snd, m, ifp)) 325 senderr(ENOBUFS); 326 return (error); 327 328bad: 329 if (m) 330 m_freem(m); 331 return (error); 332} 333 334/* 335 * Process a received FDDI packet; 336 * the packet is in the mbuf chain m without 337 * the fddi header, which is provided separately. 338 */ 339void 340fddi_input(ifp, fh, m) 341 struct ifnet *ifp; 342 struct fddi_header *fh; 343 struct mbuf *m; 344{ 345 struct ifqueue *inq; 346 struct llc *l; 347 348 if ((ifp->if_flags & IFF_UP) == 0) { 349 m_freem(m); 350 return; 351 } 352 getmicrotime(&ifp->if_lastchange); 353 ifp->if_ibytes += m->m_pkthdr.len + sizeof (*fh); 354 if (fh->fddi_dhost[0] & 1) { 355 if (bcmp((caddr_t)fddibroadcastaddr, (caddr_t)fh->fddi_dhost, 356 FDDI_ADDR_LEN) == 0) 357 m->m_flags |= M_BCAST; 358 else 359 m->m_flags |= M_MCAST; 360 ifp->if_imcasts++; 361 } else if ((ifp->if_flags & IFF_PROMISC) 362 && bcmp(IFP2AC(ifp)->ac_enaddr, (caddr_t)fh->fddi_dhost, 363 FDDI_ADDR_LEN) != 0) { 364 m_freem(m); 365 return; 366 } 367 368#ifdef M_LINK0 369 /* 370 * If this has a LLC priority of 0, then mark it so upper 371 * layers have a hint that it really came via a FDDI/Ethernet 372 * bridge. 373 */ 374 if ((fh->fddi_fc & FDDIFC_LLC_PRIO7) == FDDIFC_LLC_PRIO0) 375 m->m_flags |= M_LINK0; 376#endif 377 378 l = mtod(m, struct llc *); 379 switch (l->llc_dsap) { 380#if defined(INET) || defined(INET6) || defined(NS) || defined(DECNET) || defined(IPX) || defined(NETATALK) 381 case LLC_SNAP_LSAP: 382 { 383 u_int16_t type; 384 if (l->llc_control != LLC_UI || l->llc_ssap != LLC_SNAP_LSAP) 385 goto dropanyway; 386#ifdef NETATALK 387 if (Bcmp(&(l->llc_snap.org_code)[0], at_org_code, 388 sizeof(at_org_code)) == 0 && 389 ntohs(l->llc_snap.ether_type) == ETHERTYPE_AT) { 390 inq = &atintrq2; 391 m_adj(m, LLC_SNAPFRAMELEN); 392 schednetisr(NETISR_ATALK); 393 break; 394 } 395 396 if (Bcmp(&(l->llc_snap.org_code)[0], aarp_org_code, 397 sizeof(aarp_org_code)) == 0 && 398 ntohs(l->llc_snap.ether_type) == ETHERTYPE_AARP) { 399 m_adj(m, LLC_SNAPFRAMELEN); 400 aarpinput(IFP2AC(ifp), m); /* XXX */ 401 return; 402 } 403#endif /* NETATALK */ 404 if (l->llc_snap.org_code[0] != 0 || l->llc_snap.org_code[1] != 0|| l->llc_snap.org_code[2] != 0) 405 goto dropanyway; 406 type = ntohs(l->llc_snap.ether_type); 407 m_adj(m, 8); 408 switch (type) { 409#ifdef INET 410 case ETHERTYPE_IP: 411 if (ipflow_fastforward(m)) 412 return; 413 schednetisr(NETISR_IP); 414 inq = &ipintrq; 415 break; 416 417 case ETHERTYPE_ARP: 418 if (ifp->if_flags & IFF_NOARP) 419 goto dropanyway; 420 schednetisr(NETISR_ARP); 421 inq = &arpintrq; 422 break; 423#endif 424#ifdef INET6 425 case ETHERTYPE_IPV6: 426 schednetisr(NETISR_IPV6); 427 inq = &ip6intrq; 428 break; 429#endif 430#ifdef IPX 431 case ETHERTYPE_IPX: 432 schednetisr(NETISR_IPX); 433 inq = &ipxintrq; 434 break; 435#endif 436#ifdef NS 437 case ETHERTYPE_NS: 438 schednetisr(NETISR_NS); 439 inq = &nsintrq; 440 break; 441#endif 442#ifdef DECNET 443 case ETHERTYPE_DECNET: 444 schednetisr(NETISR_DECNET); 445 inq = &decnetintrq; 446 break; 447#endif 448#ifdef NETATALK 449 case ETHERTYPE_AT: 450 schednetisr(NETISR_ATALK); 451 inq = &atintrq1; 452 break; 453 case ETHERTYPE_AARP: 454 /* probably this should be done with a NETISR as well */ 455 aarpinput(IFP2AC(ifp), m); /* XXX */ 456 return; 457#endif /* NETATALK */ 458 default: 459 /* printf("fddi_input: unknown protocol 0x%x\n", type); */ 460 ifp->if_noproto++; 461 goto dropanyway; 462 } 463 break; 464 } 465#endif /* INET || NS */ 466 467 default: 468 /* printf("fddi_input: unknown dsap 0x%x\n", l->llc_dsap); */ 469 ifp->if_noproto++; 470 dropanyway: 471 m_freem(m); 472 return; 473 } 474 475 (void) IF_HANDOFF(inq, m, NULL); 476} 477/* 478 * Perform common duties while attaching to interface list 479 */ 480 481void 482fddi_ifattach(ifp) 483 struct ifnet *ifp; 484{ 485 struct ifaddr *ifa; 486 struct sockaddr_dl *sdl; 487 488 ifp->if_type = IFT_FDDI; 489 ifp->if_addrlen = FDDI_ADDR_LEN; 490 ifp->if_hdrlen = 21; 491 ifp->if_mtu = FDDIMTU; 492 ifp->if_resolvemulti = fddi_resolvemulti; 493 ifp->if_baudrate = 100000000; 494#ifdef IFF_NOTRAILERS 495 ifp->if_flags |= IFF_NOTRAILERS; 496#endif 497 ifp->if_broadcastaddr = fddibroadcastaddr; 498 ifa = ifaddr_byindex(ifp->if_index); 499 sdl = (struct sockaddr_dl *)ifa->ifa_addr; 500 sdl->sdl_type = IFT_FDDI; 501 sdl->sdl_alen = ifp->if_addrlen; 502 bcopy(IFP2AC(ifp)->ac_enaddr, LLADDR(sdl), ifp->if_addrlen); 503} 504 505static int 506fddi_resolvemulti(ifp, llsa, sa) 507 struct ifnet *ifp; 508 struct sockaddr **llsa; 509 struct sockaddr *sa; 510{ 511 struct sockaddr_dl *sdl; 512 struct sockaddr_in *sin; 513#ifdef INET6 514 struct sockaddr_in6 *sin6; 515#endif 516 u_char *e_addr; 517 518 switch(sa->sa_family) { 519 case AF_LINK: 520 /* 521 * No mapping needed. Just check that it's a valid MC address. 522 */ 523 sdl = (struct sockaddr_dl *)sa; 524 e_addr = LLADDR(sdl); 525 if ((e_addr[0] & 1) != 1) 526 return (EADDRNOTAVAIL); 527 *llsa = 0; 528 return (0); 529 530#ifdef INET 531 case AF_INET: 532 sin = (struct sockaddr_in *)sa; 533 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) 534 return (EADDRNOTAVAIL); 535 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR, 536 M_WAITOK); 537 sdl->sdl_len = sizeof *sdl; 538 sdl->sdl_family = AF_LINK; 539 sdl->sdl_index = ifp->if_index; 540 sdl->sdl_type = IFT_FDDI; 541 sdl->sdl_nlen = 0; 542 sdl->sdl_alen = ETHER_ADDR_LEN; /* XXX */ 543 sdl->sdl_slen = 0; 544 e_addr = LLADDR(sdl); 545 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr); 546 *llsa = (struct sockaddr *)sdl; 547 return (0); 548#endif 549#ifdef INET6 550 case AF_INET6: 551 sin6 = (struct sockaddr_in6 *)sa; 552 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 553 /* 554 * An IP6 address of 0 means listen to all 555 * of the Ethernet multicast address used for IP6. 556 * (This is used for multicast routers.) 557 */ 558 ifp->if_flags |= IFF_ALLMULTI; 559 *llsa = 0; 560 return (0); 561 } 562 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) 563 return (EADDRNOTAVAIL); 564 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR, 565 M_WAITOK); 566 sdl->sdl_len = sizeof *sdl; 567 sdl->sdl_family = AF_LINK; 568 sdl->sdl_index = ifp->if_index; 569 sdl->sdl_type = IFT_FDDI; 570 sdl->sdl_nlen = 0; 571 sdl->sdl_alen = ETHER_ADDR_LEN; /* XXX */ 572 sdl->sdl_slen = 0; 573 e_addr = LLADDR(sdl); 574 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr); 575 *llsa = (struct sockaddr *)sdl; 576 return (0); 577#endif 578 579 default: 580 /* 581 * Well, the text isn't quite right, but it's the name 582 * that counts... 583 */ 584 return (EAFNOSUPPORT); 585 } 586} 587