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