if_ethersubr.c revision 1.2.4.1 1 1.1 cgd /*
2 1.1 cgd * Copyright (c) 1982, 1989 Regents of the University of California.
3 1.1 cgd * All rights reserved.
4 1.1 cgd *
5 1.1 cgd * Redistribution and use in source and binary forms, with or without
6 1.1 cgd * modification, are permitted provided that the following conditions
7 1.1 cgd * are met:
8 1.1 cgd * 1. Redistributions of source code must retain the above copyright
9 1.1 cgd * notice, this list of conditions and the following disclaimer.
10 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer in the
12 1.1 cgd * documentation and/or other materials provided with the distribution.
13 1.1 cgd * 3. All advertising materials mentioning features or use of this software
14 1.1 cgd * must display the following acknowledgement:
15 1.1 cgd * This product includes software developed by the University of
16 1.1 cgd * California, Berkeley and its contributors.
17 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
18 1.1 cgd * may be used to endorse or promote products derived from this software
19 1.1 cgd * without specific prior written permission.
20 1.1 cgd *
21 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 cgd * SUCH DAMAGE.
32 1.1 cgd *
33 1.2 cgd * from: @(#)if_ethersubr.c 7.13 (Berkeley) 4/20/91
34 1.2.4.1 mycroft * $Id: if_ethersubr.c,v 1.2.4.1 1993/10/16 10:49:24 mycroft Exp $
35 1.1 cgd */
36 1.1 cgd
37 1.1 cgd #include "param.h"
38 1.1 cgd #include "systm.h"
39 1.1 cgd #include "kernel.h"
40 1.1 cgd #include "malloc.h"
41 1.1 cgd #include "mbuf.h"
42 1.1 cgd #include "protosw.h"
43 1.1 cgd #include "socket.h"
44 1.1 cgd #include "ioctl.h"
45 1.1 cgd #include "errno.h"
46 1.1 cgd #include "syslog.h"
47 1.1 cgd
48 1.1 cgd #include "if.h"
49 1.1 cgd #include "netisr.h"
50 1.1 cgd #include "route.h"
51 1.1 cgd #include "if_llc.h"
52 1.1 cgd #include "if_dl.h"
53 1.1 cgd
54 1.1 cgd #ifdef INET
55 1.1 cgd #include "../netinet/in.h"
56 1.1 cgd #include "../netinet/in_var.h"
57 1.1 cgd #endif
58 1.1 cgd #include "../netinet/if_ether.h"
59 1.1 cgd
60 1.1 cgd #ifdef NS
61 1.1 cgd #include "../netns/ns.h"
62 1.1 cgd #include "../netns/ns_if.h"
63 1.1 cgd #endif
64 1.1 cgd
65 1.1 cgd #ifdef ISO
66 1.1 cgd #include "../netiso/argo_debug.h"
67 1.1 cgd #include "../netiso/iso.h"
68 1.1 cgd #include "../netiso/iso_var.h"
69 1.1 cgd #include "../netiso/iso_snpac.h"
70 1.1 cgd #endif
71 1.1 cgd
72 1.1 cgd u_char etherbroadcastaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
73 1.1 cgd extern struct ifnet loif;
74 1.1 cgd
75 1.1 cgd /*
76 1.1 cgd * Ethernet output routine.
77 1.1 cgd * Encapsulate a packet of type family for the local net.
78 1.1 cgd * Use trailer local net encapsulation if enough data in first
79 1.1 cgd * packet leaves a multiple of 512 bytes of data in remainder.
80 1.1 cgd * Assumes that ifp is actually pointer to arpcom structure.
81 1.1 cgd */
82 1.1 cgd ether_output(ifp, m0, dst, rt)
83 1.1 cgd register struct ifnet *ifp;
84 1.1 cgd struct mbuf *m0;
85 1.1 cgd struct sockaddr *dst;
86 1.1 cgd struct rtentry *rt;
87 1.1 cgd {
88 1.1 cgd short type;
89 1.1 cgd int s, error = 0;
90 1.1 cgd u_char edst[6];
91 1.1 cgd struct in_addr idst;
92 1.1 cgd register struct mbuf *m = m0;
93 1.1 cgd struct mbuf *mcopy = (struct mbuf *)0;
94 1.1 cgd register struct ether_header *eh;
95 1.1 cgd int usetrailers, off, len = m->m_pkthdr.len;
96 1.1 cgd #define ac ((struct arpcom *)ifp)
97 1.1 cgd
98 1.1 cgd if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) {
99 1.1 cgd error = ENETDOWN;
100 1.1 cgd goto bad;
101 1.1 cgd }
102 1.1 cgd ifp->if_lastchange = time;
103 1.1 cgd switch (dst->sa_family) {
104 1.1 cgd
105 1.1 cgd #ifdef INET
106 1.1 cgd case AF_INET:
107 1.1 cgd idst = ((struct sockaddr_in *)dst)->sin_addr;
108 1.1 cgd if (!arpresolve(ac, m, &idst, edst, &usetrailers))
109 1.1 cgd return (0); /* if not yet resolved */
110 1.1 cgd if ((ifp->if_flags & IFF_SIMPLEX) && (*edst & 1))
111 1.1 cgd mcopy = m_copy(m, 0, (int)M_COPYALL);
112 1.1 cgd off = m->m_pkthdr.len - m->m_len;
113 1.1 cgd if (usetrailers && off > 0 && (off & 0x1ff) == 0 &&
114 1.1 cgd (m->m_flags & M_EXT) == 0 &&
115 1.1 cgd m->m_data >= m->m_pktdat + 2 * sizeof (u_short)) {
116 1.1 cgd type = ETHERTYPE_TRAIL + (off>>9);
117 1.1 cgd m->m_data -= 2 * sizeof (u_short);
118 1.1 cgd m->m_len += 2 * sizeof (u_short);
119 1.1 cgd len += 2 * sizeof (u_short);
120 1.1 cgd *mtod(m, u_short *) = htons((u_short)ETHERTYPE_IP);
121 1.1 cgd *(mtod(m, u_short *) + 1) = htons((u_short)m->m_len);
122 1.1 cgd goto gottrailertype;
123 1.1 cgd }
124 1.1 cgd type = ETHERTYPE_IP;
125 1.1 cgd goto gottype;
126 1.1 cgd #endif
127 1.1 cgd #ifdef NS
128 1.1 cgd case AF_NS:
129 1.1 cgd type = ETHERTYPE_NS;
130 1.1 cgd bcopy((caddr_t)&(((struct sockaddr_ns *)dst)->sns_addr.x_host),
131 1.1 cgd (caddr_t)edst, sizeof (edst));
132 1.1 cgd if (!bcmp((caddr_t)edst, (caddr_t)&ns_thishost, sizeof(edst)))
133 1.1 cgd return (looutput(ifp, m, dst, rt));
134 1.1 cgd if ((ifp->if_flags & IFF_SIMPLEX) && (*edst & 1))
135 1.1 cgd mcopy = m_copy(m, 0, (int)M_COPYALL);
136 1.1 cgd goto gottype;
137 1.1 cgd #endif
138 1.1 cgd #ifdef ISO
139 1.1 cgd case AF_ISO: {
140 1.1 cgd int snpalen;
141 1.1 cgd struct llc *l;
142 1.1 cgd
143 1.1 cgd iso_again:
144 1.1 cgd if (rt && rt->rt_gateway && (rt->rt_flags & RTF_UP)) {
145 1.1 cgd if (rt->rt_flags & RTF_GATEWAY) {
146 1.1 cgd if (rt->rt_llinfo) {
147 1.1 cgd rt = (struct rtentry *)rt->rt_llinfo;
148 1.1 cgd goto iso_again;
149 1.1 cgd }
150 1.1 cgd } else {
151 1.1 cgd register struct sockaddr_dl *sdl =
152 1.1 cgd (struct sockaddr_dl *)rt->rt_gateway;
153 1.1 cgd if (sdl && sdl->sdl_family == AF_LINK
154 1.1 cgd && sdl->sdl_alen > 0) {
155 1.1 cgd bcopy(LLADDR(sdl), (char *)edst,
156 1.1 cgd sizeof(edst));
157 1.1 cgd goto iso_resolved;
158 1.1 cgd }
159 1.1 cgd }
160 1.1 cgd }
161 1.1 cgd if ((error = iso_snparesolve(ifp, (struct sockaddr_iso *)dst,
162 1.1 cgd (char *)edst, &snpalen)) > 0)
163 1.1 cgd goto bad; /* Not Resolved */
164 1.1 cgd iso_resolved:
165 1.1 cgd if ((ifp->if_flags & IFF_SIMPLEX) && (*edst & 1) &&
166 1.1 cgd (mcopy = m_copy(m, 0, (int)M_COPYALL))) {
167 1.1 cgd M_PREPEND(mcopy, sizeof (*eh), M_DONTWAIT);
168 1.1 cgd if (mcopy) {
169 1.1 cgd eh = mtod(mcopy, struct ether_header *);
170 1.1 cgd bcopy((caddr_t)edst,
171 1.1 cgd (caddr_t)eh->ether_dhost, sizeof (edst));
172 1.1 cgd bcopy((caddr_t)ac->ac_enaddr,
173 1.1 cgd (caddr_t)eh->ether_shost, sizeof (edst));
174 1.1 cgd }
175 1.1 cgd }
176 1.1 cgd M_PREPEND(m, 3, M_DONTWAIT);
177 1.1 cgd if (m == NULL)
178 1.1 cgd return (0);
179 1.1 cgd type = m->m_pkthdr.len;
180 1.1 cgd l = mtod(m, struct llc *);
181 1.1 cgd l->llc_dsap = l->llc_ssap = LLC_ISO_LSAP;
182 1.1 cgd l->llc_control = LLC_UI;
183 1.1 cgd len += 3;
184 1.1 cgd IFDEBUG(D_ETHER)
185 1.1 cgd int i;
186 1.1 cgd printf("unoutput: sending pkt to: ");
187 1.1 cgd for (i=0; i<6; i++)
188 1.1 cgd printf("%x ", edst[i] & 0xff);
189 1.1 cgd printf("\n");
190 1.1 cgd ENDDEBUG
191 1.1 cgd } goto gottype;
192 1.1 cgd #endif ISO
193 1.1 cgd #ifdef RMP
194 1.1 cgd case AF_RMP:
195 1.1 cgd /*
196 1.1 cgd * This is IEEE 802.3 -- the Ethernet `type' field is
197 1.1 cgd * really a `length' field.
198 1.1 cgd */
199 1.1 cgd type = m->m_len;
200 1.1 cgd bcopy((caddr_t)dst->sa_data, (caddr_t)edst, sizeof(edst));
201 1.1 cgd break;
202 1.1 cgd #endif
203 1.1 cgd
204 1.1 cgd case AF_UNSPEC:
205 1.1 cgd eh = (struct ether_header *)dst->sa_data;
206 1.1 cgd bcopy((caddr_t)eh->ether_dhost, (caddr_t)edst, sizeof (edst));
207 1.1 cgd type = eh->ether_type;
208 1.1 cgd goto gottype;
209 1.1 cgd
210 1.1 cgd default:
211 1.1 cgd printf("%s%d: can't handle af%d\n", ifp->if_name, ifp->if_unit,
212 1.1 cgd dst->sa_family);
213 1.1 cgd error = EAFNOSUPPORT;
214 1.1 cgd goto bad;
215 1.1 cgd }
216 1.1 cgd
217 1.1 cgd gottrailertype:
218 1.1 cgd /*
219 1.1 cgd * Packet to be sent as trailer: move first packet
220 1.1 cgd * (control information) to end of chain.
221 1.1 cgd */
222 1.1 cgd while (m->m_next)
223 1.1 cgd m = m->m_next;
224 1.1 cgd m->m_next = m0;
225 1.1 cgd m = m0->m_next;
226 1.1 cgd m0->m_next = 0;
227 1.1 cgd
228 1.1 cgd gottype:
229 1.1 cgd if (mcopy)
230 1.1 cgd (void) looutput(ifp, mcopy, dst, rt);
231 1.1 cgd /*
232 1.1 cgd * Add local net header. If no space in first mbuf,
233 1.1 cgd * allocate another.
234 1.1 cgd */
235 1.1 cgd M_PREPEND(m, sizeof (struct ether_header), M_DONTWAIT);
236 1.1 cgd if (m == 0) {
237 1.1 cgd error = ENOBUFS;
238 1.1 cgd goto bad;
239 1.1 cgd }
240 1.1 cgd eh = mtod(m, struct ether_header *);
241 1.1 cgd type = htons((u_short)type);
242 1.1 cgd bcopy((caddr_t)&type,(caddr_t)&eh->ether_type,
243 1.1 cgd sizeof(eh->ether_type));
244 1.1 cgd bcopy((caddr_t)edst, (caddr_t)eh->ether_dhost, sizeof (edst));
245 1.1 cgd bcopy((caddr_t)ac->ac_enaddr, (caddr_t)eh->ether_shost,
246 1.1 cgd sizeof(eh->ether_shost));
247 1.1 cgd s = splimp();
248 1.1 cgd /*
249 1.1 cgd * Queue message on interface, and start output if interface
250 1.1 cgd * not yet active.
251 1.1 cgd */
252 1.1 cgd if (IF_QFULL(&ifp->if_snd)) {
253 1.1 cgd IF_DROP(&ifp->if_snd);
254 1.1 cgd splx(s);
255 1.1 cgd error = ENOBUFS;
256 1.1 cgd goto bad;
257 1.1 cgd }
258 1.1 cgd IF_ENQUEUE(&ifp->if_snd, m);
259 1.1 cgd if ((ifp->if_flags & IFF_OACTIVE) == 0)
260 1.1 cgd (*ifp->if_start)(ifp);
261 1.1 cgd splx(s);
262 1.1 cgd ifp->if_obytes += len + sizeof (struct ether_header);
263 1.1 cgd if (edst[0] & 1)
264 1.1 cgd ifp->if_omcasts++;
265 1.1 cgd return (error);
266 1.1 cgd
267 1.1 cgd bad:
268 1.1 cgd if (m)
269 1.1 cgd m_freem(m);
270 1.1 cgd return (error);
271 1.1 cgd }
272 1.1 cgd
273 1.1 cgd /*
274 1.1 cgd * Process a received Ethernet packet;
275 1.1 cgd * the packet is in the mbuf chain m without
276 1.1 cgd * the ether header, which is provided separately.
277 1.1 cgd */
278 1.1 cgd ether_input(ifp, eh, m)
279 1.1 cgd struct ifnet *ifp;
280 1.1 cgd register struct ether_header *eh;
281 1.1 cgd struct mbuf *m;
282 1.1 cgd {
283 1.1 cgd register struct ifqueue *inq;
284 1.1 cgd register struct llc *l;
285 1.1 cgd int s;
286 1.1 cgd
287 1.1 cgd ifp->if_lastchange = time;
288 1.1 cgd ifp->if_ibytes += m->m_pkthdr.len + sizeof (*eh);
289 1.1 cgd if (bcmp((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost,
290 1.1 cgd sizeof(etherbroadcastaddr)) == 0)
291 1.1 cgd m->m_flags |= M_BCAST;
292 1.1 cgd else if (eh->ether_dhost[0] & 1)
293 1.1 cgd m->m_flags |= M_MCAST;
294 1.1 cgd if (m->m_flags & (M_BCAST|M_MCAST))
295 1.1 cgd ifp->if_imcasts++;
296 1.1 cgd
297 1.1 cgd switch (eh->ether_type) {
298 1.1 cgd #ifdef INET
299 1.1 cgd case ETHERTYPE_IP:
300 1.1 cgd schednetisr(NETISR_IP);
301 1.1 cgd inq = &ipintrq;
302 1.1 cgd break;
303 1.1 cgd
304 1.1 cgd case ETHERTYPE_ARP:
305 1.1 cgd arpinput((struct arpcom *)ifp, m);
306 1.1 cgd return;
307 1.1 cgd #endif
308 1.1 cgd #ifdef NS
309 1.1 cgd case ETHERTYPE_NS:
310 1.1 cgd schednetisr(NETISR_NS);
311 1.1 cgd inq = &nsintrq;
312 1.1 cgd break;
313 1.1 cgd
314 1.1 cgd #endif
315 1.1 cgd default:
316 1.1 cgd #ifdef ISO
317 1.1 cgd if (eh->ether_type > ETHERMTU)
318 1.1 cgd goto dropanyway;
319 1.1 cgd l = mtod(m, struct llc *);
320 1.1 cgd switch (l->llc_control) {
321 1.1 cgd case LLC_UI:
322 1.1 cgd /* LLC_UI_P forbidden in class 1 service */
323 1.1 cgd if ((l->llc_dsap == LLC_ISO_LSAP) &&
324 1.1 cgd (l->llc_ssap == LLC_ISO_LSAP)) {
325 1.1 cgd /* LSAP for ISO */
326 1.1 cgd if (m->m_pkthdr.len > eh->ether_type)
327 1.1 cgd m_adj(m, eh->ether_type - m->m_pkthdr.len);
328 1.1 cgd m->m_data += 3; /* XXX */
329 1.1 cgd m->m_len -= 3; /* XXX */
330 1.1 cgd m->m_pkthdr.len -= 3; /* XXX */
331 1.1 cgd M_PREPEND(m, sizeof *eh, M_DONTWAIT);
332 1.1 cgd if (m == 0)
333 1.1 cgd return;
334 1.1 cgd *mtod(m, struct ether_header *) = *eh;
335 1.1 cgd IFDEBUG(D_ETHER)
336 1.1 cgd printf("clnp packet");
337 1.1 cgd ENDDEBUG
338 1.1 cgd schednetisr(NETISR_ISO);
339 1.1 cgd inq = &clnlintrq;
340 1.1 cgd break;
341 1.1 cgd }
342 1.1 cgd goto dropanyway;
343 1.1 cgd
344 1.1 cgd case LLC_XID:
345 1.1 cgd case LLC_XID_P:
346 1.1 cgd if(m->m_len < 6)
347 1.1 cgd goto dropanyway;
348 1.1 cgd l->llc_window = 0;
349 1.1 cgd l->llc_fid = 9;
350 1.1 cgd l->llc_class = 1;
351 1.1 cgd l->llc_dsap = l->llc_ssap = 0;
352 1.1 cgd /* Fall through to */
353 1.1 cgd case LLC_TEST:
354 1.1 cgd case LLC_TEST_P:
355 1.1 cgd {
356 1.1 cgd struct sockaddr sa;
357 1.1 cgd register struct ether_header *eh2;
358 1.1 cgd int i;
359 1.1 cgd u_char c = l->llc_dsap;
360 1.1 cgd l->llc_dsap = l->llc_ssap;
361 1.1 cgd l->llc_ssap = c;
362 1.1 cgd if (m->m_flags & (M_BCAST | M_MCAST))
363 1.1 cgd bcopy((caddr_t)ac->ac_enaddr,
364 1.1 cgd (caddr_t)eh->ether_dhost, 6);
365 1.1 cgd sa.sa_family = AF_UNSPEC;
366 1.1 cgd sa.sa_len = sizeof(sa);
367 1.1 cgd eh2 = (struct ether_header *)sa.sa_data;
368 1.1 cgd for (i = 0; i < 6; i++) {
369 1.1 cgd eh2->ether_shost[i] = c = eh->ether_dhost[i];
370 1.1 cgd eh2->ether_dhost[i] =
371 1.1 cgd eh->ether_dhost[i] = eh->ether_shost[i];
372 1.1 cgd eh->ether_shost[i] = c;
373 1.1 cgd }
374 1.1 cgd ifp->if_output(ifp, m, &sa);
375 1.1 cgd return;
376 1.1 cgd }
377 1.1 cgd dropanyway:
378 1.1 cgd default:
379 1.1 cgd m_freem(m);
380 1.1 cgd return;
381 1.1 cgd }
382 1.1 cgd #else
383 1.1 cgd m_freem(m);
384 1.1 cgd return;
385 1.1 cgd #endif ISO
386 1.1 cgd }
387 1.1 cgd
388 1.1 cgd s = splimp();
389 1.1 cgd if (IF_QFULL(inq)) {
390 1.1 cgd IF_DROP(inq);
391 1.1 cgd m_freem(m);
392 1.1 cgd } else
393 1.1 cgd IF_ENQUEUE(inq, m);
394 1.1 cgd splx(s);
395 1.1 cgd }
396 1.1 cgd
397 1.1 cgd /*
398 1.1 cgd * Convert Ethernet address to printable (loggable) representation.
399 1.1 cgd */
400 1.1 cgd static char digits[] = "0123456789abcdef";
401 1.1 cgd char *
402 1.1 cgd ether_sprintf(ap)
403 1.1 cgd register u_char *ap;
404 1.1 cgd {
405 1.1 cgd register i;
406 1.1 cgd static char etherbuf[18];
407 1.1 cgd register char *cp = etherbuf;
408 1.1 cgd
409 1.1 cgd for (i = 0; i < 6; i++) {
410 1.1 cgd *cp++ = digits[*ap >> 4];
411 1.1 cgd *cp++ = digits[*ap++ & 0xf];
412 1.1 cgd *cp++ = ':';
413 1.1 cgd }
414 1.1 cgd *--cp = 0;
415 1.1 cgd return (etherbuf);
416 1.1 cgd }
417