1 /* 2 * Copyright (c) 1982, 1989 Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 * 7 * @(#)if_ethersubr.c 7.19 (Berkeley) 05/27/92 8 */ 9 10 #include "param.h" 11 #include "systm.h" 12 #include "kernel.h" 13 #include "malloc.h" 14 #include "mbuf.h" 15 #include "protosw.h" 16 #include "socket.h" 17 #include "ioctl.h" 18 #include "errno.h" 19 #include "syslog.h" 20 #include "machine/cpu.h" 21 22 #include "if.h" 23 #include "netisr.h" 24 #include "route.h" 25 #include "if_llc.h" 26 #include "if_dl.h" 27 #include "if_types.h" 28 29 #ifdef INET 30 #include "../netinet/in.h" 31 #include "../netinet/in_var.h" 32 #endif 33 #include "../netinet/if_ether.h" 34 35 #ifdef NS 36 #include "../netns/ns.h" 37 #include "../netns/ns_if.h" 38 #endif 39 40 #ifdef ISO 41 #include "../netiso/argo_debug.h" 42 #include "../netiso/iso.h" 43 #include "../netiso/iso_var.h" 44 #include "../netiso/iso_snpac.h" 45 #endif 46 47 u_char etherbroadcastaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 48 extern struct ifnet loif; 49 #define senderr(e) { error = (e); goto bad;} 50 51 /* 52 * Ethernet output routine. 53 * Encapsulate a packet of type family for the local net. 54 * Use trailer local net encapsulation if enough data in first 55 * packet leaves a multiple of 512 bytes of data in remainder. 56 * Assumes that ifp is actually pointer to arpcom structure. 57 */ 58 ether_output(ifp, m0, dst, rt0) 59 register struct ifnet *ifp; 60 struct mbuf *m0; 61 struct sockaddr *dst; 62 struct rtentry *rt0; 63 { 64 short type; 65 int s, error = 0; 66 u_char edst[6]; 67 register struct mbuf *m = m0; 68 register struct rtentry *rt; 69 struct mbuf *mcopy = (struct mbuf *)0; 70 register struct ether_header *eh; 71 int usetrailers, off, len = m->m_pkthdr.len; 72 #define ac ((struct arpcom *)ifp) 73 74 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) 75 senderr(ENETDOWN); 76 ifp->if_lastchange = time; 77 if (rt = rt0) { 78 if ((rt->rt_flags & RTF_UP) == 0) { 79 if (rt0 = rt = rtalloc1(dst, 1)) 80 rt->rt_refcnt--; 81 else 82 senderr(EHOSTUNREACH); 83 } 84 if (rt->rt_flags & RTF_GATEWAY) { 85 if (rt->rt_gwroute == 0) 86 goto lookup; 87 if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) { 88 rtfree(rt); rt = rt0; 89 lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1); 90 if ((rt = rt->rt_gwroute) == 0) 91 senderr(EHOSTUNREACH); 92 } 93 } 94 if (rt->rt_flags & RTF_REJECT) 95 if (rt->rt_rmx.rmx_expire == 0 || 96 time.tv_sec < rt->rt_rmx.rmx_expire) 97 senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH); 98 } 99 switch (dst->sa_family) { 100 101 #ifdef INET 102 case AF_INET: 103 if (!arpresolve(ac, rt, m, (struct sockaddr_in *)dst, 104 edst, &usetrailers)) 105 return (0); /* if not yet resolved */ 106 if ((ifp->if_flags & IFF_SIMPLEX) && (*edst & 1)) 107 mcopy = m_copy(m, 0, (int)M_COPYALL); 108 off = m->m_pkthdr.len - m->m_len; 109 if (usetrailers && off > 0 && (off & 0x1ff) == 0 && 110 (m->m_flags & M_EXT) == 0 && 111 m->m_data >= m->m_pktdat + 2 * sizeof (u_short)) { 112 type = ETHERTYPE_TRAIL + (off>>9); 113 m->m_data -= 2 * sizeof (u_short); 114 m->m_len += 2 * sizeof (u_short); 115 len += 2 * sizeof (u_short); 116 *mtod(m, u_short *) = htons((u_short)ETHERTYPE_IP); 117 *(mtod(m, u_short *) + 1) = htons((u_short)m->m_len); 118 goto gottrailertype; 119 } 120 type = ETHERTYPE_IP; 121 goto gottype; 122 #endif 123 #ifdef NS 124 case AF_NS: 125 type = ETHERTYPE_NS; 126 bcopy((caddr_t)&(((struct sockaddr_ns *)dst)->sns_addr.x_host), 127 (caddr_t)edst, sizeof (edst)); 128 if (!bcmp((caddr_t)edst, (caddr_t)&ns_thishost, sizeof(edst))) 129 return (looutput(ifp, m, dst, rt)); 130 if ((ifp->if_flags & IFF_SIMPLEX) && (*edst & 1)) 131 mcopy = m_copy(m, 0, (int)M_COPYALL); 132 goto gottype; 133 #endif 134 #ifdef ISO 135 case AF_ISO: { 136 int snpalen; 137 struct llc *l; 138 register struct sockaddr_dl *sdl; 139 140 if (rt && (sdl = (struct sockaddr_dl *)rt->rt_gateway) && 141 sdl->sdl_family == AF_LINK && sdl->sdl_alen > 0) { 142 bcopy(LLADDR(sdl), (caddr_t)edst, sizeof(edst)); 143 } else if (error = 144 iso_snparesolve(ifp, (struct sockaddr_iso *)dst, 145 (char *)edst, &snpalen)) 146 goto bad; /* Not Resolved */ 147 if ((ifp->if_flags & IFF_SIMPLEX) && (*edst & 1) && 148 (mcopy = m_copy(m, 0, (int)M_COPYALL))) { 149 M_PREPEND(mcopy, sizeof (*eh), M_DONTWAIT); 150 if (mcopy) { 151 eh = mtod(mcopy, struct ether_header *); 152 bcopy((caddr_t)edst, 153 (caddr_t)eh->ether_dhost, sizeof (edst)); 154 bcopy((caddr_t)ac->ac_enaddr, 155 (caddr_t)eh->ether_shost, sizeof (edst)); 156 } 157 } 158 M_PREPEND(m, 3, M_DONTWAIT); 159 if (m == NULL) 160 return (0); 161 type = m->m_pkthdr.len; 162 l = mtod(m, struct llc *); 163 l->llc_dsap = l->llc_ssap = LLC_ISO_LSAP; 164 l->llc_control = LLC_UI; 165 len += 3; 166 IFDEBUG(D_ETHER) 167 int i; 168 printf("unoutput: sending pkt to: "); 169 for (i=0; i<6; i++) 170 printf("%x ", edst[i] & 0xff); 171 printf("\n"); 172 ENDDEBUG 173 } goto gottype; 174 #endif ISO 175 #ifdef RMP 176 case AF_RMP: 177 /* 178 * This is IEEE 802.3 -- the Ethernet `type' field is 179 * really a `length' field. 180 */ 181 type = m->m_len; 182 bcopy((caddr_t)dst->sa_data, (caddr_t)edst, sizeof(edst)); 183 break; 184 #endif 185 186 case AF_UNSPEC: 187 eh = (struct ether_header *)dst->sa_data; 188 bcopy((caddr_t)eh->ether_dhost, (caddr_t)edst, sizeof (edst)); 189 type = eh->ether_type; 190 goto gottype; 191 192 default: 193 printf("%s%d: can't handle af%d\n", ifp->if_name, ifp->if_unit, 194 dst->sa_family); 195 senderr(EAFNOSUPPORT); 196 } 197 198 gottrailertype: 199 /* 200 * Packet to be sent as trailer: move first packet 201 * (control information) to end of chain. 202 */ 203 while (m->m_next) 204 m = m->m_next; 205 m->m_next = m0; 206 m = m0->m_next; 207 m0->m_next = 0; 208 209 gottype: 210 if (mcopy) 211 (void) looutput(ifp, mcopy, dst, rt); 212 /* 213 * Add local net header. If no space in first mbuf, 214 * allocate another. 215 */ 216 M_PREPEND(m, sizeof (struct ether_header), M_DONTWAIT); 217 if (m == 0) 218 senderr(ENOBUFS); 219 eh = mtod(m, struct ether_header *); 220 type = htons((u_short)type); 221 bcopy((caddr_t)&type,(caddr_t)&eh->ether_type, 222 sizeof(eh->ether_type)); 223 bcopy((caddr_t)edst, (caddr_t)eh->ether_dhost, sizeof (edst)); 224 bcopy((caddr_t)ac->ac_enaddr, (caddr_t)eh->ether_shost, 225 sizeof(eh->ether_shost)); 226 s = splimp(); 227 /* 228 * Queue message on interface, and start output if interface 229 * not yet active. 230 */ 231 if (IF_QFULL(&ifp->if_snd)) { 232 IF_DROP(&ifp->if_snd); 233 splx(s); 234 senderr(ENOBUFS); 235 } 236 IF_ENQUEUE(&ifp->if_snd, m); 237 if ((ifp->if_flags & IFF_OACTIVE) == 0) 238 (*ifp->if_start)(ifp); 239 splx(s); 240 ifp->if_obytes += len + sizeof (struct ether_header); 241 if (edst[0] & 1) 242 ifp->if_omcasts++; 243 return (error); 244 245 bad: 246 if (m) 247 m_freem(m); 248 return (error); 249 } 250 251 /* 252 * Process a received Ethernet packet; 253 * the packet is in the mbuf chain m without 254 * the ether header, which is provided separately. 255 */ 256 ether_input(ifp, eh, m) 257 struct ifnet *ifp; 258 register struct ether_header *eh; 259 struct mbuf *m; 260 { 261 register struct ifqueue *inq; 262 register struct llc *l; 263 int s; 264 265 if ((ifp->if_flags & IFF_UP) == 0) { 266 m_freem(m); 267 return; 268 } 269 ifp->if_lastchange = time; 270 ifp->if_ibytes += m->m_pkthdr.len + sizeof (*eh); 271 if (bcmp((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost, 272 sizeof(etherbroadcastaddr)) == 0) 273 m->m_flags |= M_BCAST; 274 else if (eh->ether_dhost[0] & 1) 275 m->m_flags |= M_MCAST; 276 if (m->m_flags & (M_BCAST|M_MCAST)) 277 ifp->if_imcasts++; 278 279 switch (eh->ether_type) { 280 #ifdef INET 281 case ETHERTYPE_IP: 282 schednetisr(NETISR_IP); 283 inq = &ipintrq; 284 break; 285 286 case ETHERTYPE_ARP: 287 schednetisr(NETISR_ARP); 288 inq = &arpintrq; 289 break; 290 #endif 291 #ifdef NS 292 case ETHERTYPE_NS: 293 schednetisr(NETISR_NS); 294 inq = &nsintrq; 295 break; 296 297 #endif 298 default: 299 #ifdef ISO 300 if (eh->ether_type > ETHERMTU) 301 goto dropanyway; 302 l = mtod(m, struct llc *); 303 switch (l->llc_control) { 304 case LLC_UI: 305 /* LLC_UI_P forbidden in class 1 service */ 306 if ((l->llc_dsap == LLC_ISO_LSAP) && 307 (l->llc_ssap == LLC_ISO_LSAP)) { 308 /* LSAP for ISO */ 309 if (m->m_pkthdr.len > eh->ether_type) 310 m_adj(m, eh->ether_type - m->m_pkthdr.len); 311 m->m_data += 3; /* XXX */ 312 m->m_len -= 3; /* XXX */ 313 m->m_pkthdr.len -= 3; /* XXX */ 314 M_PREPEND(m, sizeof *eh, M_DONTWAIT); 315 if (m == 0) 316 return; 317 *mtod(m, struct ether_header *) = *eh; 318 IFDEBUG(D_ETHER) 319 printf("clnp packet"); 320 ENDDEBUG 321 schednetisr(NETISR_ISO); 322 inq = &clnlintrq; 323 break; 324 } 325 goto dropanyway; 326 327 case LLC_XID: 328 case LLC_XID_P: 329 if(m->m_len < 6) 330 goto dropanyway; 331 l->llc_window = 0; 332 l->llc_fid = 9; 333 l->llc_class = 1; 334 l->llc_dsap = l->llc_ssap = 0; 335 /* Fall through to */ 336 case LLC_TEST: 337 case LLC_TEST_P: 338 { 339 struct sockaddr sa; 340 register struct ether_header *eh2; 341 int i; 342 u_char c = l->llc_dsap; 343 l->llc_dsap = l->llc_ssap; 344 l->llc_ssap = c; 345 if (m->m_flags & (M_BCAST | M_MCAST)) 346 bcopy((caddr_t)ac->ac_enaddr, 347 (caddr_t)eh->ether_dhost, 6); 348 sa.sa_family = AF_UNSPEC; 349 sa.sa_len = sizeof(sa); 350 eh2 = (struct ether_header *)sa.sa_data; 351 for (i = 0; i < 6; i++) { 352 eh2->ether_shost[i] = c = eh->ether_dhost[i]; 353 eh2->ether_dhost[i] = 354 eh->ether_dhost[i] = eh->ether_shost[i]; 355 eh->ether_shost[i] = c; 356 } 357 ifp->if_output(ifp, m, &sa, (struct rtentry *)0); 358 return; 359 } 360 dropanyway: 361 default: 362 m_freem(m); 363 return; 364 } 365 #else 366 m_freem(m); 367 return; 368 #endif ISO 369 } 370 371 s = splimp(); 372 if (IF_QFULL(inq)) { 373 IF_DROP(inq); 374 m_freem(m); 375 } else 376 IF_ENQUEUE(inq, m); 377 splx(s); 378 } 379 380 /* 381 * Convert Ethernet address to printable (loggable) representation. 382 */ 383 static char digits[] = "0123456789abcdef"; 384 char * 385 ether_sprintf(ap) 386 register u_char *ap; 387 { 388 register i; 389 static char etherbuf[18]; 390 register char *cp = etherbuf; 391 392 for (i = 0; i < 6; i++) { 393 *cp++ = digits[*ap >> 4]; 394 *cp++ = digits[*ap++ & 0xf]; 395 *cp++ = ':'; 396 } 397 *--cp = 0; 398 return (etherbuf); 399 } 400 401 /* 402 * Perform common duties while attaching to interface list 403 */ 404 ether_ifattach(ifp) 405 register struct ifnet *ifp; 406 { 407 register struct ifaddr *ifa; 408 register struct sockaddr_dl *sdl; 409 410 ifp->if_type = IFT_ETHER; 411 ifp->if_addrlen = 6; 412 ifp->if_hdrlen = 14; 413 ifp->if_mtu = ETHERMTU; 414 for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next) 415 if ((sdl = (struct sockaddr_dl *)ifa->ifa_addr) && 416 sdl->sdl_family == AF_LINK) { 417 sdl->sdl_type = IFT_ETHER; 418 sdl->sdl_alen = ifp->if_addrlen; 419 bcopy((caddr_t)((struct arpcom *)ifp)->ac_enaddr, 420 LLADDR(sdl), ifp->if_addrlen); 421 break; 422 } 423 } 424