1 /* 2 * Copyright (c) 1982, 1989 Regents of the University of California. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms are permitted 6 * provided that the above copyright notice and this paragraph are 7 * duplicated in all such forms and that any documentation, 8 * advertising materials, and other materials related to such 9 * distribution and use acknowledge that the software was developed 10 * by the University of California, Berkeley. The name of the 11 * University may not be used to endorse or promote products derived 12 * from this software without specific prior written permission. 13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 15 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 16 * 17 * @(#)if_ethersubr.c 7.9 (Berkeley) 06/20/90 18 */ 19 20 #include "param.h" 21 #include "systm.h" 22 #include "malloc.h" 23 #include "mbuf.h" 24 #include "protosw.h" 25 #include "socket.h" 26 #include "ioctl.h" 27 #include "errno.h" 28 #include "syslog.h" 29 30 #include "if.h" 31 #include "netisr.h" 32 #include "route.h" 33 #include "if_llc.h" 34 #include "if_dl.h" 35 36 #include "machine/mtpr.h" 37 38 #ifdef INET 39 #include "../netinet/in.h" 40 #include "../netinet/in_var.h" 41 #include "../netinet/if_ether.h" 42 #endif 43 44 #ifdef NS 45 #include "../netns/ns.h" 46 #include "../netns/ns_if.h" 47 #endif 48 49 #ifdef ISO 50 #include "../netiso/argo_debug.h" 51 #include "../netiso/iso.h" 52 #include "../netiso/iso_var.h" 53 #include "../netiso/iso_snpac.h" 54 #endif 55 56 u_char etherbroadcastaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 57 extern struct ifnet loif; 58 59 /* 60 * Ethernet output routine. 61 * Encapsulate a packet of type family for the local net. 62 * Use trailer local net encapsulation if enough data in first 63 * packet leaves a multiple of 512 bytes of data in remainder. 64 * Assumes that ifp is actually pointer to arpcom structure. 65 */ 66 ether_output(ifp, m0, dst, rt) 67 register struct ifnet *ifp; 68 struct mbuf *m0; 69 struct sockaddr *dst; 70 struct rtentry *rt; 71 { 72 short type; 73 int s, error = 0; 74 u_char edst[6]; 75 struct in_addr idst; 76 register struct mbuf *m = m0; 77 struct mbuf *mcopy = (struct mbuf *)0; 78 register struct ether_header *eh; 79 int usetrailers, off, len = m->m_pkthdr.len; 80 extern struct timeval time; 81 #define ac ((struct arpcom *)ifp) 82 83 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) { 84 error = ENETDOWN; 85 goto bad; 86 } 87 ifp->if_lastchange = time; 88 switch (dst->sa_family) { 89 90 #ifdef INET 91 case AF_INET: 92 idst = ((struct sockaddr_in *)dst)->sin_addr; 93 if (!arpresolve(ac, m, &idst, edst, &usetrailers)) 94 return (0); /* if not yet resolved */ 95 if ((ifp->if_flags & IFF_SIMPLEX) && (*edst & 1)) 96 mcopy = m_copy(m, 0, (int)M_COPYALL); 97 off = m->m_pkthdr.len - m->m_len; 98 if (usetrailers && off > 0 && (off & 0x1ff) == 0 && 99 (m->m_flags & M_EXT) == 0 && 100 m->m_data >= m->m_pktdat + 2 * sizeof (u_short)) { 101 type = ETHERTYPE_TRAIL + (off>>9); 102 m->m_data -= 2 * sizeof (u_short); 103 m->m_len += 2 * sizeof (u_short); 104 len += 2 * sizeof (u_short); 105 *mtod(m, u_short *) = htons((u_short)ETHERTYPE_IP); 106 *(mtod(m, u_short *) + 1) = htons((u_short)m->m_len); 107 goto gottrailertype; 108 } 109 type = ETHERTYPE_IP; 110 goto gottype; 111 #endif 112 #ifdef NS 113 case AF_NS: 114 type = ETHERTYPE_NS; 115 bcopy((caddr_t)&(((struct sockaddr_ns *)dst)->sns_addr.x_host), 116 (caddr_t)edst, sizeof (edst)); 117 if (!bcmp((caddr_t)edst, (caddr_t)&ns_thishost, sizeof(edst))) 118 return (looutput(&loif, m, dst)); 119 if ((ifp->if_flags & IFF_SIMPLEX) && (*edst & 1)) 120 mcopy = m_copy(m, 0, (int)M_COPYALL); 121 goto gottype; 122 #endif 123 #ifdef ISO 124 case AF_ISO: { 125 int snpalen; 126 struct llc *l; 127 128 iso_again: 129 iso_etherout(); 130 if (rt && rt->rt_gateway && (rt->rt_flags & RTF_UP)) { 131 if (rt->rt_flags & RTF_GATEWAY) { 132 if (rt->rt_llinfo) { 133 rt = (struct rtentry *)rt->rt_llinfo; 134 goto iso_again; 135 } 136 } else { 137 register struct sockaddr_dl *sdl = 138 (struct sockaddr_dl *)rt->rt_gateway; 139 if (sdl && sdl->sdl_family == AF_LINK 140 && sdl->sdl_alen > 0) { 141 bcopy(LLADDR(sdl), (char *)edst, 142 sizeof(edst)); 143 goto iso_resolved; 144 } 145 } 146 } 147 if ((error = iso_snparesolve(ifp, (struct sockaddr_iso *)dst, 148 (char *)edst, &snpalen)) > 0) 149 goto bad; /* Not Resolved */ 150 iso_resolved: 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 /* 205 * Add local net header. If no space in first mbuf, 206 * allocate another. 207 */ 208 M_PREPEND(m, sizeof (struct ether_header), M_DONTWAIT); 209 if (m == 0) { 210 error = ENOBUFS; 211 goto bad; 212 } 213 eh = mtod(m, struct ether_header *); 214 type = htons((u_short)type); 215 bcopy((caddr_t)&type,(caddr_t)&eh->ether_type, 216 sizeof(eh->ether_type)); 217 bcopy((caddr_t)edst, (caddr_t)eh->ether_dhost, sizeof (edst)); 218 bcopy((caddr_t)ac->ac_enaddr, (caddr_t)eh->ether_shost, 219 sizeof(eh->ether_shost)); 220 /* 221 * Queue message on interface, and start output if interface 222 * not yet active. 223 */ 224 s = splimp(); 225 if (IF_QFULL(&ifp->if_snd)) { 226 IF_DROP(&ifp->if_snd); 227 splx(s); 228 error = ENOBUFS; 229 goto bad; 230 } 231 IF_ENQUEUE(&ifp->if_snd, m); 232 if ((ifp->if_flags & IFF_OACTIVE) == 0) 233 (*ifp->if_start)(ifp); 234 splx(s); 235 if (mcopy) 236 (void) looutput(&loif, mcopy, dst); 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 (mcopy) 244 m_freem(mcopy); 245 if (m) 246 m_freem(m); 247 return (error); 248 } 249 250 /* 251 * Process a received Ethernet packet; 252 * the packet is in the mbuf chain m without 253 * the ether header, which is provided separately. 254 */ 255 ether_input(ifp, eh, m) 256 struct ifnet *ifp; 257 register struct ether_header *eh; 258 struct mbuf *m; 259 { 260 register struct ifqueue *inq; 261 register struct llc *l; 262 int s; 263 264 ifp->if_lastchange = time; 265 ifp->if_ibytes += m->m_pkthdr.len + sizeof (*eh); 266 if (bcmp((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost, 267 sizeof(etherbroadcastaddr)) == 0) 268 m->m_flags |= M_BCAST; 269 else if (eh->ether_dhost[0] & 1) 270 m->m_flags |= M_MCAST; 271 if (m->m_flags & (M_BCAST|M_MCAST)) 272 ifp->if_imcasts++; 273 274 switch (eh->ether_type) { 275 #ifdef INET 276 case ETHERTYPE_IP: 277 schednetisr(NETISR_IP); 278 inq = &ipintrq; 279 break; 280 281 case ETHERTYPE_ARP: 282 arpinput((struct arpcom *)ifp, m); 283 return; 284 #endif 285 #ifdef NS 286 case ETHERTYPE_NS: 287 schednetisr(NETISR_NS); 288 inq = &nsintrq; 289 break; 290 291 #endif 292 default: 293 #ifdef ISO 294 if (eh->ether_type > ETHERMTU) 295 goto dropanyway; 296 l = mtod(m, struct llc *); 297 switch (l->llc_control) { 298 case LLC_UI: 299 /* LLC_UI_P forbidden in class 1 service */ 300 if ((l->llc_dsap == LLC_ISO_LSAP) && 301 (l->llc_ssap == LLC_ISO_LSAP)) { 302 /* LSAP for ISO */ 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 iso_etherout() {} 393