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