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