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.8 (Berkeley) 06/09/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 if (rt && rt->rt_gateway && (rt->rt_flags & RTF_UP)) { 130 if (rt->rt_flags & RTF_GATEWAY) { 131 register struct llinfo_llc *lc = 132 (struct llinfo_llc *)rt->rt_llinfo; 133 if (lc && lc->lc_rtgate) { 134 rt = lc->lc_rtgate; 135 goto iso_again; 136 } 137 } else { 138 register struct sockaddr_dl *sdl = 139 (struct sockaddr_dl *)rt->rt_gateway; 140 if (sdl && sdl->sdl_family == AF_LINK 141 && sdl->sdl_alen > 0) { 142 bcopy(LLADDR(sdl), (char *)edst, 143 sizeof(edst)); 144 goto iso_resolved; 145 } 146 } 147 } 148 if ((error = iso_snparesolve(ifp, (struct sockaddr_iso *)dst, 149 (char *)edst, &snpalen)) > 0) 150 goto bad; /* Not Resolved */ 151 iso_resolved: 152 M_PREPEND(m, 3, M_DONTWAIT); 153 if (m == NULL) 154 return (0); 155 type = m->m_pkthdr.len; 156 l = mtod(m, struct llc *); 157 l->llc_dsap = l->llc_ssap = LLC_ISO_LSAP; 158 l->llc_control = LLC_UI; 159 len += 3; 160 IFDEBUG(D_ETHER) 161 int i; 162 printf("unoutput: sending pkt to: "); 163 for (i=0; i<6; i++) 164 printf("%x ", edst[i] & 0xff); 165 printf("\n"); 166 ENDDEBUG 167 } goto gottype; 168 #endif ISO 169 #ifdef RMP 170 case AF_RMP: 171 /* 172 * This is IEEE 802.3 -- the Ethernet `type' field is 173 * really a `length' field. 174 */ 175 type = m->m_len; 176 bcopy((caddr_t)dst->sa_data, (caddr_t)edst, sizeof(edst)); 177 break; 178 #endif 179 180 case AF_UNSPEC: 181 eh = (struct ether_header *)dst->sa_data; 182 bcopy((caddr_t)eh->ether_dhost, (caddr_t)edst, sizeof (edst)); 183 type = eh->ether_type; 184 goto gottype; 185 186 default: 187 printf("%s%d: can't handle af%d\n", ifp->if_name, ifp->if_unit, 188 dst->sa_family); 189 error = EAFNOSUPPORT; 190 goto bad; 191 } 192 193 gottrailertype: 194 /* 195 * Packet to be sent as trailer: move first packet 196 * (control information) to end of chain. 197 */ 198 while (m->m_next) 199 m = m->m_next; 200 m->m_next = m0; 201 m = m0->m_next; 202 m0->m_next = 0; 203 204 gottype: 205 /* 206 * Add local net header. If no space in first mbuf, 207 * allocate another. 208 */ 209 M_PREPEND(m, sizeof (struct ether_header), M_DONTWAIT); 210 if (m == 0) { 211 error = ENOBUFS; 212 goto bad; 213 } 214 eh = mtod(m, struct ether_header *); 215 type = htons((u_short)type); 216 bcopy((caddr_t)&type,(caddr_t)&eh->ether_type, 217 sizeof(eh->ether_type)); 218 bcopy((caddr_t)edst, (caddr_t)eh->ether_dhost, sizeof (edst)); 219 bcopy((caddr_t)ac->ac_enaddr, (caddr_t)eh->ether_shost, 220 sizeof(eh->ether_shost)); 221 /* 222 * Queue message on interface, and start output if interface 223 * not yet active. 224 */ 225 s = splimp(); 226 if (IF_QFULL(&ifp->if_snd)) { 227 IF_DROP(&ifp->if_snd); 228 splx(s); 229 error = ENOBUFS; 230 goto bad; 231 } 232 IF_ENQUEUE(&ifp->if_snd, m); 233 if ((ifp->if_flags & IFF_OACTIVE) == 0) 234 (*ifp->if_start)(ifp); 235 splx(s); 236 if (mcopy) 237 (void) looutput(&loif, mcopy, dst); 238 ifp->if_obytes += len + sizeof (struct ether_header); 239 if (edst[0] & 1) 240 ifp->if_omcasts++; 241 return (error); 242 243 bad: 244 if (mcopy) 245 m_freem(mcopy); 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 ifp->if_lastchange = time; 266 ifp->if_ibytes += m->m_pkthdr.len + sizeof (*eh); 267 if (bcmp((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost, 268 sizeof(etherbroadcastaddr)) == 0) 269 m->m_flags |= M_BCAST; 270 else if (eh->ether_dhost[0] & 1) 271 m->m_flags |= M_MCAST; 272 if (m->m_flags & (M_BCAST|M_MCAST)) 273 ifp->if_imcasts++; 274 275 switch (eh->ether_type) { 276 #ifdef INET 277 case ETHERTYPE_IP: 278 schednetisr(NETISR_IP); 279 inq = &ipintrq; 280 break; 281 282 case ETHERTYPE_ARP: 283 arpinput((struct arpcom *)ifp, m); 284 return; 285 #endif 286 #ifdef NS 287 case ETHERTYPE_NS: 288 schednetisr(NETISR_NS); 289 inq = &nsintrq; 290 break; 291 292 #endif 293 default: 294 #ifdef ISO 295 if (eh->ether_type > ETHERMTU) 296 goto dropanyway; 297 l = mtod(m, struct llc *); 298 switch (l->llc_control) { 299 case LLC_UI: 300 /* LLC_UI_P forbidden in class 1 service */ 301 if ((l->llc_dsap == LLC_ISO_LSAP) && 302 (l->llc_ssap == LLC_ISO_LSAP)) { 303 /* LSAP for ISO */ 304 m->m_data += 3; /* XXX */ 305 m->m_len -= 3; /* XXX */ 306 m->m_pkthdr.len -= 3; /* XXX */ 307 M_PREPEND(m, sizeof *eh, M_DONTWAIT); 308 if (m == 0) 309 return; 310 *mtod(m, struct ether_header *) = *eh; 311 IFDEBUG(D_ETHER) 312 printf("clnp packet"); 313 ENDDEBUG 314 schednetisr(NETISR_ISO); 315 inq = &clnlintrq; 316 break; 317 } 318 goto dropanyway; 319 320 case LLC_XID: 321 case LLC_XID_P: 322 if(m->m_len < 6) 323 goto dropanyway; 324 l->llc_window = 0; 325 l->llc_fid = 9; 326 l->llc_class = 1; 327 l->llc_dsap = l->llc_ssap = 0; 328 /* Fall through to */ 329 case LLC_TEST: 330 case LLC_TEST_P: 331 { 332 struct sockaddr sa; 333 register struct ether_header *eh2; 334 int i; 335 u_char c = l->llc_dsap; 336 l->llc_dsap = l->llc_ssap; 337 l->llc_ssap = c; 338 if (m->m_flags & (M_BCAST | M_MCAST)) 339 bcopy((caddr_t)ac->ac_enaddr, 340 (caddr_t)eh->ether_dhost, 6); 341 sa.sa_family = AF_UNSPEC; 342 sa.sa_len = sizeof(sa); 343 eh2 = (struct ether_header *)sa.sa_data; 344 for (i = 0; i < 6; i++) { 345 eh2->ether_shost[i] = c = eh->ether_dhost[i]; 346 eh2->ether_dhost[i] = 347 eh->ether_dhost[i] = eh->ether_shost[i]; 348 eh->ether_shost[i] = c; 349 } 350 ifp->if_output(ifp, m, &sa); 351 return; 352 } 353 dropanyway: 354 default: 355 m_freem(m); 356 return; 357 } 358 #else 359 m_freem(m); 360 return; 361 #endif ISO 362 } 363 364 s = splimp(); 365 if (IF_QFULL(inq)) { 366 IF_DROP(inq); 367 m_freem(m); 368 } else 369 IF_ENQUEUE(inq, m); 370 splx(s); 371 } 372 373 /* 374 * Convert Ethernet address to printable (loggable) representation. 375 */ 376 static char digits[] = "0123456789abcdef"; 377 char * 378 ether_sprintf(ap) 379 register u_char *ap; 380 { 381 register i; 382 static char etherbuf[18]; 383 register char *cp = etherbuf; 384 385 for (i = 0; i < 6; i++) { 386 *cp++ = digits[*ap >> 4]; 387 *cp++ = digits[*ap++ & 0xf]; 388 *cp++ = ':'; 389 } 390 *--cp = 0; 391 return (etherbuf); 392 } 393