1 /* 2 * Copyright (c) 1982, 1986, 1988 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.3 (Berkeley) 04/25/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; 77 #define ac ((struct arpcom *)ifp) 78 79 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) { 80 error = ENETDOWN; 81 goto bad; 82 } 83 if (ifp->if_flags & IFF_SIMPLEX && dst->sa_family != AF_UNSPEC && 84 !bcmp((caddr_t)edst, (caddr_t)etherbroadcastaddr, sizeof (edst))) 85 mcopy = m_copy(m, 0, (int)M_COPYALL); 86 switch (dst->sa_family) { 87 88 #ifdef INET 89 case AF_INET: 90 idst = ((struct sockaddr_in *)dst)->sin_addr; 91 if (!arpresolve(ac, m, &idst, edst, &usetrailers)) 92 return (0); /* if not yet resolved */ 93 if ((ifp->if_flags & IFF_SIMPLEX) && (*edst & 1)) 94 mcopy = m_copy(m, 0, (int)M_COPYALL); 95 off = m->m_pkthdr.len - m->m_len; 96 if (usetrailers && off > 0 && (off & 0x1ff) == 0 && 97 (m->m_flags & M_EXT) == 0 && 98 m->m_data >= m->m_pktdat + 2 * sizeof (u_short)) { 99 type = ETHERTYPE_TRAIL + (off>>9); 100 m->m_data -= 2 * sizeof (u_short); 101 m->m_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 if (!bcmp((caddr_t)edst, (caddr_t)&ns_thishost, sizeof(edst))) 113 return(looutput(&loif, m, dst)); 114 bcopy((caddr_t)&(((struct sockaddr_ns *)dst)->sns_addr.x_host), 115 (caddr_t)edst, sizeof (edst)); 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 IFDEBUG(D_ETHER) 142 int i; 143 printf("unoutput: sending pkt to: "); 144 for (i=0; i<6; i++) 145 printf("%x ", edst[i] & 0xff); 146 printf("\n"); 147 ENDDEBUG 148 } goto gottype; 149 #endif ISO 150 case AF_UNSPEC: 151 eh = (struct ether_header *)dst->sa_data; 152 bcopy((caddr_t)eh->ether_dhost, (caddr_t)edst, sizeof (edst)); 153 type = eh->ether_type; 154 goto gottype; 155 156 default: 157 printf("%s%d: can't handle af%d\n", ifp->if_name, ifp->if_unit, 158 dst->sa_family); 159 error = EAFNOSUPPORT; 160 goto bad; 161 } 162 163 gottrailertype: 164 /* 165 * Packet to be sent as trailer: move first packet 166 * (control information) to end of chain. 167 */ 168 while (m->m_next) 169 m = m->m_next; 170 m->m_next = m0; 171 m = m0->m_next; 172 m0->m_next = 0; 173 174 gottype: 175 /* 176 * Add local net header. If no space in first mbuf, 177 * allocate another. 178 */ 179 M_PREPEND(m, sizeof (struct ether_header), M_DONTWAIT); 180 if (m == 0) { 181 error = ENOBUFS; 182 goto bad; 183 } 184 eh = mtod(m, struct ether_header *); 185 type = htons((u_short)type); 186 bcopy((caddr_t)&type,(caddr_t)&eh->ether_type, 187 sizeof(eh->ether_type)); 188 bcopy((caddr_t)edst, (caddr_t)eh->ether_dhost, sizeof (edst)); 189 bcopy((caddr_t)ac->ac_enaddr, (caddr_t)eh->ether_shost, 190 sizeof(eh->ether_shost)); 191 /* 192 * Queue message on interface, and start output if interface 193 * not yet active. 194 */ 195 s = splimp(); 196 if (IF_QFULL(&ifp->if_snd)) { 197 IF_DROP(&ifp->if_snd); 198 splx(s); 199 error = ENOBUFS; 200 goto bad; 201 } 202 IF_ENQUEUE(&ifp->if_snd, m); 203 if ((ifp->if_flags & IFF_OACTIVE) == 0) 204 error = (*ifp->if_start)(ifp); 205 splx(s); 206 if (mcopy) 207 (void) looutput(&loif, mcopy, dst); 208 return (error); 209 210 bad: 211 if (mcopy) 212 m_freem(mcopy); 213 if (m) 214 m_freem(m); 215 return (error); 216 } 217 218 /* 219 * Pull packet off interface. Off is nonzero if packet 220 * has trailing header; we still have to drop 221 * the type and length which are at the front of any trailer data. 222 */ 223 ether_input(ifp, eh, m) 224 struct ifnet *ifp; 225 register struct ether_header *eh; 226 struct mbuf *m; 227 { 228 register struct ifqueue *inq; 229 register struct llc *l; 230 int s; 231 232 if (bcmp((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost, 233 sizeof(etherbroadcastaddr)) == 0) 234 m->m_flags |= M_BCAST; 235 else if (eh->ether_dhost[0] & 1) 236 m->m_flags |= M_MCAST; 237 238 switch (eh->ether_type) { 239 #ifdef INET 240 case ETHERTYPE_IP: 241 schednetisr(NETISR_IP); 242 inq = &ipintrq; 243 break; 244 245 case ETHERTYPE_ARP: 246 arpinput((struct arpcom *)ifp, m); 247 return; 248 #endif 249 #ifdef NS 250 case ETHERTYPE_NS: 251 schednetisr(NETISR_NS); 252 inq = &nsintrq; 253 break; 254 255 #endif 256 default: 257 if (eh->ether_type > ETHERMTU) 258 goto dropanyway; 259 l = mtod(m, struct llc *); 260 switch (l->llc_control) { 261 case LLC_UI: 262 /* LLC_UI_P forbidden in class 1 service */ 263 if ((l->llc_dsap == LLC_ISO_LSAP) && 264 (l->llc_ssap == LLC_ISO_LSAP)) { 265 #ifdef ISO 266 /* LSAP for ISO */ 267 M_PREPEND(m, sizeof *eh, M_DONTWAIT); 268 if (m == 0) 269 return; 270 *mtod(m, struct ether_header *) = *eh; 271 IFDEBUG(D_ETHER) 272 printf("clnp packet"); 273 ENDDEBUG 274 schednetisr(NETISR_ISO); 275 inq = &clnlintrq; 276 if (IF_QFULL(inq)){ 277 IFDEBUG(D_ETHER) 278 printf(" qfull\n"); 279 ENDDEBUG 280 IF_DROP(inq); 281 m_freem(m); 282 } else { 283 IF_ENQUEUE(inq, m); 284 IFDEBUG(D_ETHER) 285 printf(" queued\n"); 286 ENDDEBUG 287 } 288 return; 289 #endif ISO 290 } 291 break; 292 case LLC_XID: 293 case LLC_XID_P: 294 if(m->m_len < 6) 295 goto dropanyway; 296 l->llc_window = 0; 297 l->llc_fid = 9; 298 l->llc_class = 1; 299 l->llc_dsap = l->llc_ssap = 0; 300 /* Fall through to */ 301 case LLC_TEST: 302 case LLC_TEST_P: 303 { 304 struct sockaddr sa; 305 register struct ether_header *eh2; 306 int i; 307 u_char c = l->llc_dsap; 308 l->llc_dsap = l->llc_ssap; 309 l->llc_ssap = c; 310 if (m->m_flags & (M_BCAST | M_MCAST)) 311 bcopy((caddr_t)ac->ac_enaddr, 312 (caddr_t)eh->ether_dhost, 6); 313 sa.sa_family = AF_UNSPEC; 314 sa.sa_len = sizeof(sa); 315 eh2 = (struct ether_header *)sa.sa_data; 316 for (i = 0; i < 6; i++) { 317 eh2->ether_shost[i] = c = eh->ether_dhost[i]; 318 eh2->ether_dhost[i] = 319 eh->ether_dhost[i] = eh->ether_shost[i]; 320 eh->ether_shost[i] = c; 321 } 322 ifp->if_output(ifp, m, &sa); 323 return; 324 } 325 dropanyway: 326 default: 327 m_freem(m); 328 return; 329 } 330 } 331 332 s = splimp(); 333 if (IF_QFULL(inq)) { 334 IF_DROP(inq); 335 m_freem(m); 336 } else 337 IF_ENQUEUE(inq, m); 338 splx(s); 339 } 340 341 /* 342 * Convert Ethernet address to printable (loggable) representation. 343 */ 344 static char digits[] = "0123456789abcdef"; 345 char * 346 ether_sprintf(ap) 347 register u_char *ap; 348 { 349 register i; 350 static char etherbuf[18]; 351 register char *cp = etherbuf; 352 353 for (i = 0; i < 6; i++) { 354 *cp++ = digits[*ap >> 4]; 355 *cp++ = digits[*ap++ & 0xf]; 356 *cp++ = ':'; 357 } 358 *--cp = 0; 359 return (etherbuf); 360 } 361