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