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