if_ethersubr.c revision 1.27 1 1.27 fvdl /* $NetBSD: if_ethersubr.c,v 1.27 1998/03/01 02:25:05 fvdl Exp $ */
2 1.9 cgd
3 1.1 cgd /*
4 1.8 mycroft * Copyright (c) 1982, 1989, 1993
5 1.8 mycroft * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd *
35 1.27 fvdl * @(#)if_ethersubr.c 8.2 (Berkeley) 4/4/96
36 1.1 cgd */
37 1.1 cgd
38 1.4 mycroft #include <sys/param.h>
39 1.4 mycroft #include <sys/systm.h>
40 1.4 mycroft #include <sys/kernel.h>
41 1.4 mycroft #include <sys/malloc.h>
42 1.4 mycroft #include <sys/mbuf.h>
43 1.4 mycroft #include <sys/protosw.h>
44 1.4 mycroft #include <sys/socket.h>
45 1.4 mycroft #include <sys/ioctl.h>
46 1.4 mycroft #include <sys/errno.h>
47 1.4 mycroft #include <sys/syslog.h>
48 1.4 mycroft
49 1.8 mycroft #include <machine/cpu.h>
50 1.8 mycroft
51 1.4 mycroft #include <net/if.h>
52 1.4 mycroft #include <net/netisr.h>
53 1.4 mycroft #include <net/route.h>
54 1.4 mycroft #include <net/if_llc.h>
55 1.4 mycroft #include <net/if_dl.h>
56 1.8 mycroft #include <net/if_types.h>
57 1.1 cgd
58 1.22 is #include <net/if_ether.h>
59 1.22 is
60 1.15 phil #include <netinet/in.h>
61 1.1 cgd #ifdef INET
62 1.4 mycroft #include <netinet/in_var.h>
63 1.1 cgd #endif
64 1.22 is #include <netinet/if_inarp.h>
65 1.1 cgd
66 1.1 cgd #ifdef NS
67 1.4 mycroft #include <netns/ns.h>
68 1.4 mycroft #include <netns/ns_if.h>
69 1.1 cgd #endif
70 1.1 cgd
71 1.1 cgd #ifdef ISO
72 1.4 mycroft #include <netiso/argo_debug.h>
73 1.4 mycroft #include <netiso/iso.h>
74 1.4 mycroft #include <netiso/iso_var.h>
75 1.4 mycroft #include <netiso/iso_snpac.h>
76 1.1 cgd #endif
77 1.4 mycroft
78 1.8 mycroft #ifdef LLC
79 1.8 mycroft #include <netccitt/dll.h>
80 1.8 mycroft #include <netccitt/llc_var.h>
81 1.8 mycroft #endif
82 1.8 mycroft
83 1.8 mycroft #if defined(LLC) && defined(CCITT)
84 1.8 mycroft extern struct ifqueue pkintrq;
85 1.8 mycroft #endif
86 1.1 cgd
87 1.23 christos #ifdef NETATALK
88 1.23 christos #include <netatalk/at.h>
89 1.23 christos #include <netatalk/at_var.h>
90 1.23 christos #include <netatalk/at_extern.h>
91 1.23 christos
92 1.23 christos #define llc_snap_org_code llc_un.type_snap.org_code
93 1.23 christos #define llc_snap_ether_type llc_un.type_snap.ether_type
94 1.23 christos
95 1.23 christos extern u_char at_org_code[3];
96 1.23 christos extern u_char aarp_org_code[3];
97 1.23 christos #endif /* NETATALK */
98 1.23 christos
99 1.1 cgd u_char etherbroadcastaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
100 1.8 mycroft #define senderr(e) { error = (e); goto bad;}
101 1.1 cgd
102 1.22 is #define SIN(x) ((struct sockaddr_in *)x)
103 1.22 is
104 1.1 cgd /*
105 1.1 cgd * Ethernet output routine.
106 1.1 cgd * Encapsulate a packet of type family for the local net.
107 1.22 is * Assumes that ifp is actually pointer to ethercom structure.
108 1.1 cgd */
109 1.8 mycroft int
110 1.8 mycroft ether_output(ifp, m0, dst, rt0)
111 1.1 cgd register struct ifnet *ifp;
112 1.1 cgd struct mbuf *m0;
113 1.1 cgd struct sockaddr *dst;
114 1.8 mycroft struct rtentry *rt0;
115 1.1 cgd {
116 1.10 cgd u_int16_t etype;
117 1.1 cgd int s, error = 0;
118 1.1 cgd u_char edst[6];
119 1.1 cgd register struct mbuf *m = m0;
120 1.8 mycroft register struct rtentry *rt;
121 1.1 cgd struct mbuf *mcopy = (struct mbuf *)0;
122 1.1 cgd register struct ether_header *eh;
123 1.24 christos #ifdef INET
124 1.22 is struct arphdr *ah;
125 1.24 christos #endif /* INET */
126 1.23 christos #ifdef NETATALK
127 1.23 christos struct at_ifaddr *aa;
128 1.23 christos #endif /* NETATALK */
129 1.1 cgd
130 1.8 mycroft if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
131 1.8 mycroft senderr(ENETDOWN);
132 1.8 mycroft ifp->if_lastchange = time;
133 1.18 christos if ((rt = rt0) != NULL) {
134 1.8 mycroft if ((rt->rt_flags & RTF_UP) == 0) {
135 1.27 fvdl if ((rt0 = rt = rtalloc1(dst, 1)) != NULL) {
136 1.8 mycroft rt->rt_refcnt--;
137 1.27 fvdl if (rt->rt_ifp != ifp)
138 1.27 fvdl return (*rt->rt_ifp->if_output)
139 1.27 fvdl (ifp, m0, dst, rt);
140 1.27 fvdl } else
141 1.8 mycroft senderr(EHOSTUNREACH);
142 1.8 mycroft }
143 1.27 fvdl if ((rt->rt_flags & RTF_GATEWAY) && dst->sa_family != AF_NS) {
144 1.8 mycroft if (rt->rt_gwroute == 0)
145 1.8 mycroft goto lookup;
146 1.8 mycroft if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
147 1.8 mycroft rtfree(rt); rt = rt0;
148 1.8 mycroft lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1);
149 1.8 mycroft if ((rt = rt->rt_gwroute) == 0)
150 1.8 mycroft senderr(EHOSTUNREACH);
151 1.27 fvdl /* the "G" test below also prevents rt == rt0 */
152 1.27 fvdl if ((rt->rt_flags & RTF_GATEWAY) ||
153 1.27 fvdl (rt->rt_ifp != ifp)) {
154 1.27 fvdl rt->rt_refcnt--;
155 1.27 fvdl rt0->rt_gwroute = 0;
156 1.27 fvdl senderr(EHOSTUNREACH);
157 1.27 fvdl }
158 1.8 mycroft }
159 1.8 mycroft }
160 1.8 mycroft if (rt->rt_flags & RTF_REJECT)
161 1.8 mycroft if (rt->rt_rmx.rmx_expire == 0 ||
162 1.8 mycroft time.tv_sec < rt->rt_rmx.rmx_expire)
163 1.8 mycroft senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
164 1.1 cgd }
165 1.1 cgd switch (dst->sa_family) {
166 1.1 cgd
167 1.1 cgd #ifdef INET
168 1.1 cgd case AF_INET:
169 1.22 is if (m->m_flags & M_BCAST)
170 1.22 is bcopy((caddr_t)etherbroadcastaddr, (caddr_t)edst,
171 1.22 is sizeof(edst));
172 1.22 is
173 1.22 is else if (m->m_flags & M_MCAST) {
174 1.22 is ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr,
175 1.22 is (caddr_t)edst)
176 1.22 is
177 1.22 is } else if (!arpresolve(ifp, rt, m, dst, edst))
178 1.1 cgd return (0); /* if not yet resolved */
179 1.3 hpeyerl /* If broadcasting on a simplex interface, loopback a copy */
180 1.3 hpeyerl if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX))
181 1.1 cgd mcopy = m_copy(m, 0, (int)M_COPYALL);
182 1.17 mycroft etype = htons(ETHERTYPE_IP);
183 1.8 mycroft break;
184 1.22 is
185 1.22 is case AF_ARP:
186 1.22 is ah = mtod(m, struct arphdr *);
187 1.22 is if (m->m_flags & M_BCAST)
188 1.22 is bcopy((caddr_t)etherbroadcastaddr, (caddr_t)edst,
189 1.22 is sizeof(edst));
190 1.22 is else
191 1.22 is bcopy((caddr_t)ar_tha(ah),
192 1.22 is (caddr_t)edst, sizeof(edst));
193 1.22 is
194 1.22 is ah->ar_hrd = htons(ARPHRD_ETHER);
195 1.22 is
196 1.22 is switch(ntohs(ah->ar_op)) {
197 1.22 is case ARPOP_REVREQUEST:
198 1.22 is case ARPOP_REVREPLY:
199 1.22 is etype = htons(ETHERTYPE_REVARP);
200 1.22 is break;
201 1.22 is
202 1.22 is case ARPOP_REQUEST:
203 1.22 is case ARPOP_REPLY:
204 1.22 is default:
205 1.22 is etype = htons(ETHERTYPE_ARP);
206 1.22 is }
207 1.22 is
208 1.22 is break;
209 1.1 cgd #endif
210 1.23 christos #ifdef NETATALK
211 1.23 christos case AF_APPLETALK:
212 1.23 christos if (!aarpresolve(ifp, m, (struct sockaddr_at *)dst, edst)) {
213 1.23 christos #ifdef NETATALKDEBUG
214 1.23 christos printf("aarpresolv failed\n");
215 1.23 christos #endif /* NETATALKDEBUG */
216 1.23 christos return (0);
217 1.23 christos }
218 1.23 christos /*
219 1.23 christos * ifaddr is the first thing in at_ifaddr
220 1.23 christos */
221 1.23 christos aa = (struct at_ifaddr *) at_ifawithnet(
222 1.25 christos (struct sockaddr_at *)dst, ifp);
223 1.23 christos if (aa == NULL)
224 1.23 christos goto bad;
225 1.23 christos
226 1.23 christos /*
227 1.23 christos * In the phase 2 case, we need to prepend an mbuf for the
228 1.23 christos * llc header. Since we must preserve the value of m,
229 1.23 christos * which is passed to us by value, we m_copy() the first
230 1.23 christos * mbuf, and use it for our llc header.
231 1.23 christos */
232 1.23 christos if (aa->aa_flags & AFA_PHASE2) {
233 1.23 christos struct llc llc;
234 1.23 christos
235 1.23 christos M_PREPEND(m, sizeof(struct llc), M_WAIT);
236 1.23 christos llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
237 1.23 christos llc.llc_control = LLC_UI;
238 1.23 christos bcopy(at_org_code, llc.llc_snap_org_code,
239 1.23 christos sizeof(llc.llc_snap_org_code));
240 1.23 christos llc.llc_snap_ether_type = htons(ETHERTYPE_AT);
241 1.23 christos bcopy(&llc, mtod(m, caddr_t), sizeof(struct llc));
242 1.23 christos etype = htons(m->m_pkthdr.len);
243 1.23 christos } else {
244 1.23 christos etype = htons(ETHERTYPE_AT);
245 1.23 christos }
246 1.23 christos break;
247 1.23 christos #endif /* NETATALK */
248 1.1 cgd #ifdef NS
249 1.1 cgd case AF_NS:
250 1.17 mycroft etype = htons(ETHERTYPE_NS);
251 1.1 cgd bcopy((caddr_t)&(((struct sockaddr_ns *)dst)->sns_addr.x_host),
252 1.1 cgd (caddr_t)edst, sizeof (edst));
253 1.1 cgd if (!bcmp((caddr_t)edst, (caddr_t)&ns_thishost, sizeof(edst)))
254 1.1 cgd return (looutput(ifp, m, dst, rt));
255 1.3 hpeyerl /* If broadcasting on a simplex interface, loopback a copy */
256 1.3 hpeyerl if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX))
257 1.1 cgd mcopy = m_copy(m, 0, (int)M_COPYALL);
258 1.8 mycroft break;
259 1.1 cgd #endif
260 1.1 cgd #ifdef ISO
261 1.1 cgd case AF_ISO: {
262 1.1 cgd int snpalen;
263 1.1 cgd struct llc *l;
264 1.8 mycroft register struct sockaddr_dl *sdl;
265 1.1 cgd
266 1.8 mycroft if (rt && (sdl = (struct sockaddr_dl *)rt->rt_gateway) &&
267 1.8 mycroft sdl->sdl_family == AF_LINK && sdl->sdl_alen > 0) {
268 1.8 mycroft bcopy(LLADDR(sdl), (caddr_t)edst, sizeof(edst));
269 1.18 christos } else {
270 1.18 christos error = iso_snparesolve(ifp, (struct sockaddr_iso *)dst,
271 1.18 christos (char *)edst, &snpalen);
272 1.18 christos if (error)
273 1.18 christos goto bad; /* Not Resolved */
274 1.18 christos }
275 1.3 hpeyerl /* If broadcasting on a simplex interface, loopback a copy */
276 1.8 mycroft if (*edst & 1)
277 1.8 mycroft m->m_flags |= (M_BCAST|M_MCAST);
278 1.3 hpeyerl if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX) &&
279 1.1 cgd (mcopy = m_copy(m, 0, (int)M_COPYALL))) {
280 1.1 cgd M_PREPEND(mcopy, sizeof (*eh), M_DONTWAIT);
281 1.1 cgd if (mcopy) {
282 1.1 cgd eh = mtod(mcopy, struct ether_header *);
283 1.1 cgd bcopy((caddr_t)edst,
284 1.1 cgd (caddr_t)eh->ether_dhost, sizeof (edst));
285 1.22 is bcopy(LLADDR(ifp->if_sadl),
286 1.1 cgd (caddr_t)eh->ether_shost, sizeof (edst));
287 1.1 cgd }
288 1.1 cgd }
289 1.1 cgd M_PREPEND(m, 3, M_DONTWAIT);
290 1.1 cgd if (m == NULL)
291 1.1 cgd return (0);
292 1.17 mycroft etype = htons(m->m_pkthdr.len);
293 1.1 cgd l = mtod(m, struct llc *);
294 1.1 cgd l->llc_dsap = l->llc_ssap = LLC_ISO_LSAP;
295 1.1 cgd l->llc_control = LLC_UI;
296 1.18 christos #ifdef ARGO_DEBUG
297 1.18 christos if (argo_debug[D_ETHER]) {
298 1.1 cgd int i;
299 1.21 christos printf("unoutput: sending pkt to: ");
300 1.1 cgd for (i=0; i<6; i++)
301 1.21 christos printf("%x ", edst[i] & 0xff);
302 1.21 christos printf("\n");
303 1.18 christos }
304 1.18 christos #endif
305 1.8 mycroft } break;
306 1.8 mycroft #endif /* ISO */
307 1.8 mycroft #ifdef LLC
308 1.8 mycroft /* case AF_NSAP: */
309 1.8 mycroft case AF_CCITT: {
310 1.8 mycroft register struct sockaddr_dl *sdl =
311 1.8 mycroft (struct sockaddr_dl *) rt -> rt_gateway;
312 1.8 mycroft
313 1.8 mycroft if (sdl && sdl->sdl_family == AF_LINK
314 1.8 mycroft && sdl->sdl_alen > 0) {
315 1.8 mycroft bcopy(LLADDR(sdl), (char *)edst,
316 1.8 mycroft sizeof(edst));
317 1.8 mycroft } else goto bad; /* Not a link interface ? Funny ... */
318 1.8 mycroft if ((ifp->if_flags & IFF_SIMPLEX) && (*edst & 1) &&
319 1.8 mycroft (mcopy = m_copy(m, 0, (int)M_COPYALL))) {
320 1.8 mycroft M_PREPEND(mcopy, sizeof (*eh), M_DONTWAIT);
321 1.8 mycroft if (mcopy) {
322 1.8 mycroft eh = mtod(mcopy, struct ether_header *);
323 1.8 mycroft bcopy((caddr_t)edst,
324 1.8 mycroft (caddr_t)eh->ether_dhost, sizeof (edst));
325 1.22 is bcopy(LLADDR(ifp->if_sadl),
326 1.8 mycroft (caddr_t)eh->ether_shost, sizeof (edst));
327 1.8 mycroft }
328 1.3 hpeyerl }
329 1.17 mycroft etype = htons(m->m_pkthdr.len);
330 1.8 mycroft #ifdef LLC_DEBUG
331 1.8 mycroft {
332 1.8 mycroft int i;
333 1.8 mycroft register struct llc *l = mtod(m, struct llc *);
334 1.8 mycroft
335 1.21 christos printf("ether_output: sending LLC2 pkt to: ");
336 1.8 mycroft for (i=0; i<6; i++)
337 1.21 christos printf("%x ", edst[i] & 0xff);
338 1.21 christos printf(" len 0x%x dsap 0x%x ssap 0x%x control 0x%x\n",
339 1.17 mycroft m->m_pkthdr.len, l->llc_dsap & 0xff, l->llc_ssap &0xff,
340 1.17 mycroft l->llc_control & 0xff);
341 1.8 mycroft
342 1.8 mycroft }
343 1.8 mycroft #endif /* LLC_DEBUG */
344 1.8 mycroft } break;
345 1.8 mycroft #endif /* LLC */
346 1.1 cgd
347 1.1 cgd case AF_UNSPEC:
348 1.1 cgd eh = (struct ether_header *)dst->sa_data;
349 1.1 cgd bcopy((caddr_t)eh->ether_dhost, (caddr_t)edst, sizeof (edst));
350 1.8 mycroft /* AF_UNSPEC doesn't swap the byte order of the ether_type. */
351 1.17 mycroft etype = eh->ether_type;
352 1.8 mycroft break;
353 1.1 cgd
354 1.1 cgd default:
355 1.21 christos printf("%s: can't handle af%d\n", ifp->if_xname,
356 1.1 cgd dst->sa_family);
357 1.8 mycroft senderr(EAFNOSUPPORT);
358 1.1 cgd }
359 1.1 cgd
360 1.1 cgd if (mcopy)
361 1.1 cgd (void) looutput(ifp, mcopy, dst, rt);
362 1.16 mycroft
363 1.1 cgd /*
364 1.1 cgd * Add local net header. If no space in first mbuf,
365 1.1 cgd * allocate another.
366 1.1 cgd */
367 1.1 cgd M_PREPEND(m, sizeof (struct ether_header), M_DONTWAIT);
368 1.8 mycroft if (m == 0)
369 1.8 mycroft senderr(ENOBUFS);
370 1.1 cgd eh = mtod(m, struct ether_header *);
371 1.8 mycroft bcopy((caddr_t)&etype,(caddr_t)&eh->ether_type,
372 1.1 cgd sizeof(eh->ether_type));
373 1.1 cgd bcopy((caddr_t)edst, (caddr_t)eh->ether_dhost, sizeof (edst));
374 1.22 is bcopy(LLADDR(ifp->if_sadl), (caddr_t)eh->ether_shost,
375 1.1 cgd sizeof(eh->ether_shost));
376 1.1 cgd s = splimp();
377 1.1 cgd /*
378 1.1 cgd * Queue message on interface, and start output if interface
379 1.1 cgd * not yet active.
380 1.1 cgd */
381 1.1 cgd if (IF_QFULL(&ifp->if_snd)) {
382 1.1 cgd IF_DROP(&ifp->if_snd);
383 1.1 cgd splx(s);
384 1.8 mycroft senderr(ENOBUFS);
385 1.1 cgd }
386 1.14 mycroft ifp->if_obytes += m->m_pkthdr.len;
387 1.1 cgd IF_ENQUEUE(&ifp->if_snd, m);
388 1.1 cgd if ((ifp->if_flags & IFF_OACTIVE) == 0)
389 1.1 cgd (*ifp->if_start)(ifp);
390 1.1 cgd splx(s);
391 1.3 hpeyerl if (m->m_flags & M_MCAST)
392 1.1 cgd ifp->if_omcasts++;
393 1.1 cgd return (error);
394 1.1 cgd
395 1.1 cgd bad:
396 1.1 cgd if (m)
397 1.1 cgd m_freem(m);
398 1.1 cgd return (error);
399 1.1 cgd }
400 1.1 cgd
401 1.1 cgd /*
402 1.1 cgd * Process a received Ethernet packet;
403 1.1 cgd * the packet is in the mbuf chain m without
404 1.1 cgd * the ether header, which is provided separately.
405 1.1 cgd */
406 1.8 mycroft void
407 1.1 cgd ether_input(ifp, eh, m)
408 1.1 cgd struct ifnet *ifp;
409 1.1 cgd register struct ether_header *eh;
410 1.1 cgd struct mbuf *m;
411 1.1 cgd {
412 1.1 cgd register struct ifqueue *inq;
413 1.18 christos u_int16_t etype;
414 1.18 christos int s;
415 1.23 christos #if defined (ISO) || defined (LLC) || defined(NETATALK)
416 1.1 cgd register struct llc *l;
417 1.18 christos #endif
418 1.1 cgd
419 1.8 mycroft if ((ifp->if_flags & IFF_UP) == 0) {
420 1.8 mycroft m_freem(m);
421 1.8 mycroft return;
422 1.8 mycroft }
423 1.1 cgd ifp->if_lastchange = time;
424 1.1 cgd ifp->if_ibytes += m->m_pkthdr.len + sizeof (*eh);
425 1.8 mycroft if (eh->ether_dhost[0] & 1) {
426 1.8 mycroft if (bcmp((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost,
427 1.8 mycroft sizeof(etherbroadcastaddr)) == 0)
428 1.8 mycroft m->m_flags |= M_BCAST;
429 1.8 mycroft else
430 1.8 mycroft m->m_flags |= M_MCAST;
431 1.8 mycroft }
432 1.1 cgd if (m->m_flags & (M_BCAST|M_MCAST))
433 1.1 cgd ifp->if_imcasts++;
434 1.1 cgd
435 1.5 deraadt etype = ntohs(eh->ether_type);
436 1.5 deraadt switch (etype) {
437 1.1 cgd #ifdef INET
438 1.1 cgd case ETHERTYPE_IP:
439 1.1 cgd schednetisr(NETISR_IP);
440 1.1 cgd inq = &ipintrq;
441 1.1 cgd break;
442 1.1 cgd
443 1.1 cgd case ETHERTYPE_ARP:
444 1.8 mycroft schednetisr(NETISR_ARP);
445 1.8 mycroft inq = &arpintrq;
446 1.8 mycroft break;
447 1.7 glass
448 1.7 glass case ETHERTYPE_REVARP:
449 1.8 mycroft revarpinput(m); /* XXX queue? */
450 1.1 cgd return;
451 1.1 cgd #endif
452 1.1 cgd #ifdef NS
453 1.1 cgd case ETHERTYPE_NS:
454 1.1 cgd schednetisr(NETISR_NS);
455 1.1 cgd inq = &nsintrq;
456 1.1 cgd break;
457 1.1 cgd
458 1.1 cgd #endif
459 1.23 christos #ifdef NETATALK
460 1.23 christos case ETHERTYPE_AT:
461 1.23 christos schednetisr(NETISR_ATALK);
462 1.23 christos inq = &atintrq1;
463 1.23 christos break;
464 1.23 christos case ETHERTYPE_AARP:
465 1.23 christos /* probably this should be done with a NETISR as well */
466 1.23 christos aarpinput(ifp, m); /* XXX */
467 1.23 christos return;
468 1.23 christos #endif /* NETATALK */
469 1.1 cgd default:
470 1.23 christos #if defined (ISO) || defined (LLC) || defined (NETATALK)
471 1.11 mycroft if (etype > ETHERMTU)
472 1.1 cgd goto dropanyway;
473 1.1 cgd l = mtod(m, struct llc *);
474 1.8 mycroft switch (l->llc_dsap) {
475 1.23 christos #ifdef NETATALK
476 1.23 christos case LLC_SNAP_LSAP:
477 1.23 christos switch (l->llc_control) {
478 1.23 christos case LLC_UI:
479 1.23 christos if (l->llc_ssap != LLC_SNAP_LSAP) {
480 1.23 christos goto dropanyway;
481 1.23 christos }
482 1.23 christos
483 1.23 christos if (Bcmp(&(l->llc_snap_org_code)[0],
484 1.23 christos at_org_code, sizeof(at_org_code)) == 0 &&
485 1.23 christos ntohs(l->llc_snap_ether_type) ==
486 1.23 christos ETHERTYPE_AT) {
487 1.23 christos inq = &atintrq2;
488 1.23 christos m_adj(m, sizeof(struct llc));
489 1.23 christos schednetisr(NETISR_ATALK);
490 1.23 christos break;
491 1.23 christos }
492 1.23 christos
493 1.23 christos if (Bcmp(&(l->llc_snap_org_code)[0],
494 1.23 christos aarp_org_code,
495 1.23 christos sizeof(aarp_org_code)) == 0 &&
496 1.23 christos ntohs(l->llc_snap_ether_type) ==
497 1.23 christos ETHERTYPE_AARP) {
498 1.23 christos m_adj( m, sizeof(struct llc));
499 1.23 christos aarpinput(ifp, m); /* XXX */
500 1.23 christos return;
501 1.23 christos }
502 1.23 christos
503 1.23 christos default:
504 1.23 christos goto dropanyway;
505 1.23 christos }
506 1.23 christos break;
507 1.23 christos #endif /* NETATALK */
508 1.8 mycroft #ifdef ISO
509 1.8 mycroft case LLC_ISO_LSAP:
510 1.8 mycroft switch (l->llc_control) {
511 1.8 mycroft case LLC_UI:
512 1.8 mycroft /* LLC_UI_P forbidden in class 1 service */
513 1.8 mycroft if ((l->llc_dsap == LLC_ISO_LSAP) &&
514 1.8 mycroft (l->llc_ssap == LLC_ISO_LSAP)) {
515 1.8 mycroft /* LSAP for ISO */
516 1.11 mycroft if (m->m_pkthdr.len > etype)
517 1.11 mycroft m_adj(m, etype - m->m_pkthdr.len);
518 1.8 mycroft m->m_data += 3; /* XXX */
519 1.8 mycroft m->m_len -= 3; /* XXX */
520 1.8 mycroft m->m_pkthdr.len -= 3; /* XXX */
521 1.8 mycroft M_PREPEND(m, sizeof *eh, M_DONTWAIT);
522 1.8 mycroft if (m == 0)
523 1.8 mycroft return;
524 1.8 mycroft *mtod(m, struct ether_header *) = *eh;
525 1.18 christos #ifdef ARGO_DEBUG
526 1.18 christos if (argo_debug[D_ETHER])
527 1.21 christos printf("clnp packet");
528 1.18 christos #endif
529 1.8 mycroft schednetisr(NETISR_ISO);
530 1.8 mycroft inq = &clnlintrq;
531 1.8 mycroft break;
532 1.8 mycroft }
533 1.8 mycroft goto dropanyway;
534 1.8 mycroft
535 1.8 mycroft case LLC_XID:
536 1.8 mycroft case LLC_XID_P:
537 1.8 mycroft if(m->m_len < 6)
538 1.8 mycroft goto dropanyway;
539 1.8 mycroft l->llc_window = 0;
540 1.8 mycroft l->llc_fid = 9;
541 1.8 mycroft l->llc_class = 1;
542 1.8 mycroft l->llc_dsap = l->llc_ssap = 0;
543 1.8 mycroft /* Fall through to */
544 1.8 mycroft case LLC_TEST:
545 1.8 mycroft case LLC_TEST_P:
546 1.8 mycroft {
547 1.8 mycroft struct sockaddr sa;
548 1.8 mycroft register struct ether_header *eh2;
549 1.8 mycroft int i;
550 1.8 mycroft u_char c = l->llc_dsap;
551 1.8 mycroft
552 1.8 mycroft l->llc_dsap = l->llc_ssap;
553 1.8 mycroft l->llc_ssap = c;
554 1.8 mycroft if (m->m_flags & (M_BCAST | M_MCAST))
555 1.22 is bcopy(LLADDR(ifp->if_sadl),
556 1.8 mycroft (caddr_t)eh->ether_dhost, 6);
557 1.8 mycroft sa.sa_family = AF_UNSPEC;
558 1.8 mycroft sa.sa_len = sizeof(sa);
559 1.8 mycroft eh2 = (struct ether_header *)sa.sa_data;
560 1.8 mycroft for (i = 0; i < 6; i++) {
561 1.22 is eh2->ether_shost[i] = c =
562 1.22 is eh->ether_dhost[i];
563 1.8 mycroft eh2->ether_dhost[i] =
564 1.22 is eh->ether_dhost[i] =
565 1.22 is eh->ether_shost[i];
566 1.8 mycroft eh->ether_shost[i] = c;
567 1.8 mycroft }
568 1.8 mycroft ifp->if_output(ifp, m, &sa, NULL);
569 1.8 mycroft return;
570 1.8 mycroft }
571 1.8 mycroft default:
572 1.8 mycroft m_freem(m);
573 1.8 mycroft return;
574 1.8 mycroft }
575 1.8 mycroft break;
576 1.8 mycroft #endif /* ISO */
577 1.8 mycroft #ifdef LLC
578 1.8 mycroft case LLC_X25_LSAP:
579 1.8 mycroft {
580 1.11 mycroft if (m->m_pkthdr.len > etype)
581 1.11 mycroft m_adj(m, etype - m->m_pkthdr.len);
582 1.8 mycroft M_PREPEND(m, sizeof(struct sdl_hdr) , M_DONTWAIT);
583 1.1 cgd if (m == 0)
584 1.1 cgd return;
585 1.8 mycroft if ( !sdl_sethdrif(ifp, eh->ether_shost, LLC_X25_LSAP,
586 1.8 mycroft eh->ether_dhost, LLC_X25_LSAP, 6,
587 1.8 mycroft mtod(m, struct sdl_hdr *)))
588 1.8 mycroft panic("ETHER cons addr failure");
589 1.11 mycroft mtod(m, struct sdl_hdr *)->sdlhdr_len = etype;
590 1.8 mycroft #ifdef LLC_DEBUG
591 1.21 christos printf("llc packet\n");
592 1.8 mycroft #endif /* LLC_DEBUG */
593 1.8 mycroft schednetisr(NETISR_CCITT);
594 1.8 mycroft inq = &llcintrq;
595 1.1 cgd break;
596 1.1 cgd }
597 1.8 mycroft #endif /* LLC */
598 1.1 cgd dropanyway:
599 1.1 cgd default:
600 1.8 mycroft m_freem(m);
601 1.8 mycroft return;
602 1.8 mycroft }
603 1.23 christos #else /* ISO || LLC || NETATALK*/
604 1.1 cgd m_freem(m);
605 1.1 cgd return;
606 1.23 christos #endif /* ISO || LLC || NETATALK*/
607 1.1 cgd }
608 1.1 cgd
609 1.1 cgd s = splimp();
610 1.1 cgd if (IF_QFULL(inq)) {
611 1.1 cgd IF_DROP(inq);
612 1.1 cgd m_freem(m);
613 1.1 cgd } else
614 1.1 cgd IF_ENQUEUE(inq, m);
615 1.1 cgd splx(s);
616 1.1 cgd }
617 1.1 cgd
618 1.1 cgd /*
619 1.1 cgd * Convert Ethernet address to printable (loggable) representation.
620 1.1 cgd */
621 1.1 cgd static char digits[] = "0123456789abcdef";
622 1.1 cgd char *
623 1.1 cgd ether_sprintf(ap)
624 1.1 cgd register u_char *ap;
625 1.1 cgd {
626 1.1 cgd register i;
627 1.1 cgd static char etherbuf[18];
628 1.1 cgd register char *cp = etherbuf;
629 1.1 cgd
630 1.1 cgd for (i = 0; i < 6; i++) {
631 1.1 cgd *cp++ = digits[*ap >> 4];
632 1.1 cgd *cp++ = digits[*ap++ & 0xf];
633 1.1 cgd *cp++ = ':';
634 1.1 cgd }
635 1.1 cgd *--cp = 0;
636 1.1 cgd return (etherbuf);
637 1.1 cgd }
638 1.8 mycroft
639 1.8 mycroft /*
640 1.8 mycroft * Perform common duties while attaching to interface list
641 1.8 mycroft */
642 1.8 mycroft void
643 1.22 is ether_ifattach(ifp, lla)
644 1.8 mycroft register struct ifnet *ifp;
645 1.22 is u_int8_t * lla;
646 1.8 mycroft {
647 1.8 mycroft register struct sockaddr_dl *sdl;
648 1.8 mycroft
649 1.8 mycroft ifp->if_type = IFT_ETHER;
650 1.8 mycroft ifp->if_addrlen = 6;
651 1.8 mycroft ifp->if_hdrlen = 14;
652 1.8 mycroft ifp->if_mtu = ETHERMTU;
653 1.12 mycroft ifp->if_output = ether_output;
654 1.22 is if ((sdl = ifp->if_sadl) &&
655 1.22 is sdl->sdl_family == AF_LINK) {
656 1.22 is sdl->sdl_type = IFT_ETHER;
657 1.22 is sdl->sdl_alen = ifp->if_addrlen;
658 1.22 is bcopy((caddr_t)lla, LLADDR(sdl), ifp->if_addrlen);
659 1.22 is }
660 1.22 is LIST_INIT(&((struct ethercom *)ifp)->ec_multiaddrs);
661 1.26 is ifp->if_broadcastaddr = etherbroadcastaddr;
662 1.8 mycroft }
663 1.8 mycroft
664 1.3 hpeyerl u_char ether_ipmulticast_min[6] = { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 };
665 1.3 hpeyerl u_char ether_ipmulticast_max[6] = { 0x01, 0x00, 0x5e, 0x7f, 0xff, 0xff };
666 1.3 hpeyerl /*
667 1.3 hpeyerl * Add an Ethernet multicast address or range of addresses to the list for a
668 1.3 hpeyerl * given interface.
669 1.3 hpeyerl */
670 1.3 hpeyerl int
671 1.22 is ether_addmulti(ifr, ec)
672 1.3 hpeyerl struct ifreq *ifr;
673 1.22 is register struct ethercom *ec;
674 1.3 hpeyerl {
675 1.3 hpeyerl register struct ether_multi *enm;
676 1.24 christos #ifdef INET
677 1.3 hpeyerl struct sockaddr_in *sin;
678 1.24 christos #endif /* INET */
679 1.3 hpeyerl u_char addrlo[6];
680 1.3 hpeyerl u_char addrhi[6];
681 1.3 hpeyerl int s = splimp();
682 1.3 hpeyerl
683 1.3 hpeyerl switch (ifr->ifr_addr.sa_family) {
684 1.3 hpeyerl
685 1.3 hpeyerl case AF_UNSPEC:
686 1.3 hpeyerl bcopy(ifr->ifr_addr.sa_data, addrlo, 6);
687 1.3 hpeyerl bcopy(addrlo, addrhi, 6);
688 1.3 hpeyerl break;
689 1.3 hpeyerl
690 1.3 hpeyerl #ifdef INET
691 1.3 hpeyerl case AF_INET:
692 1.3 hpeyerl sin = (struct sockaddr_in *)&(ifr->ifr_addr);
693 1.3 hpeyerl if (sin->sin_addr.s_addr == INADDR_ANY) {
694 1.3 hpeyerl /*
695 1.3 hpeyerl * An IP address of INADDR_ANY means listen to all
696 1.3 hpeyerl * of the Ethernet multicast addresses used for IP.
697 1.3 hpeyerl * (This is for the sake of IP multicast routers.)
698 1.3 hpeyerl */
699 1.3 hpeyerl bcopy(ether_ipmulticast_min, addrlo, 6);
700 1.3 hpeyerl bcopy(ether_ipmulticast_max, addrhi, 6);
701 1.3 hpeyerl }
702 1.3 hpeyerl else {
703 1.3 hpeyerl ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo);
704 1.3 hpeyerl bcopy(addrlo, addrhi, 6);
705 1.3 hpeyerl }
706 1.3 hpeyerl break;
707 1.3 hpeyerl #endif
708 1.3 hpeyerl
709 1.3 hpeyerl default:
710 1.3 hpeyerl splx(s);
711 1.3 hpeyerl return (EAFNOSUPPORT);
712 1.3 hpeyerl }
713 1.3 hpeyerl
714 1.3 hpeyerl /*
715 1.3 hpeyerl * Verify that we have valid Ethernet multicast addresses.
716 1.3 hpeyerl */
717 1.3 hpeyerl if ((addrlo[0] & 0x01) != 1 || (addrhi[0] & 0x01) != 1) {
718 1.3 hpeyerl splx(s);
719 1.3 hpeyerl return (EINVAL);
720 1.3 hpeyerl }
721 1.3 hpeyerl /*
722 1.3 hpeyerl * See if the address range is already in the list.
723 1.3 hpeyerl */
724 1.22 is ETHER_LOOKUP_MULTI(addrlo, addrhi, ec, enm);
725 1.3 hpeyerl if (enm != NULL) {
726 1.3 hpeyerl /*
727 1.3 hpeyerl * Found it; just increment the reference count.
728 1.3 hpeyerl */
729 1.3 hpeyerl ++enm->enm_refcount;
730 1.3 hpeyerl splx(s);
731 1.3 hpeyerl return (0);
732 1.3 hpeyerl }
733 1.3 hpeyerl /*
734 1.3 hpeyerl * New address or range; malloc a new multicast record
735 1.3 hpeyerl * and link it into the interface's multicast list.
736 1.3 hpeyerl */
737 1.3 hpeyerl enm = (struct ether_multi *)malloc(sizeof(*enm), M_IFMADDR, M_NOWAIT);
738 1.3 hpeyerl if (enm == NULL) {
739 1.3 hpeyerl splx(s);
740 1.3 hpeyerl return (ENOBUFS);
741 1.3 hpeyerl }
742 1.3 hpeyerl bcopy(addrlo, enm->enm_addrlo, 6);
743 1.3 hpeyerl bcopy(addrhi, enm->enm_addrhi, 6);
744 1.22 is enm->enm_ec = ec;
745 1.3 hpeyerl enm->enm_refcount = 1;
746 1.22 is LIST_INSERT_HEAD(&ec->ec_multiaddrs, enm, enm_list);
747 1.22 is ec->ec_multicnt++;
748 1.3 hpeyerl splx(s);
749 1.3 hpeyerl /*
750 1.3 hpeyerl * Return ENETRESET to inform the driver that the list has changed
751 1.3 hpeyerl * and its reception filter should be adjusted accordingly.
752 1.3 hpeyerl */
753 1.3 hpeyerl return (ENETRESET);
754 1.3 hpeyerl }
755 1.3 hpeyerl
756 1.3 hpeyerl /*
757 1.3 hpeyerl * Delete a multicast address record.
758 1.3 hpeyerl */
759 1.3 hpeyerl int
760 1.22 is ether_delmulti(ifr, ec)
761 1.3 hpeyerl struct ifreq *ifr;
762 1.22 is register struct ethercom *ec;
763 1.3 hpeyerl {
764 1.3 hpeyerl register struct ether_multi *enm;
765 1.24 christos #ifdef INET
766 1.3 hpeyerl struct sockaddr_in *sin;
767 1.24 christos #endif /* INET */
768 1.3 hpeyerl u_char addrlo[6];
769 1.3 hpeyerl u_char addrhi[6];
770 1.3 hpeyerl int s = splimp();
771 1.3 hpeyerl
772 1.3 hpeyerl switch (ifr->ifr_addr.sa_family) {
773 1.3 hpeyerl
774 1.3 hpeyerl case AF_UNSPEC:
775 1.3 hpeyerl bcopy(ifr->ifr_addr.sa_data, addrlo, 6);
776 1.3 hpeyerl bcopy(addrlo, addrhi, 6);
777 1.3 hpeyerl break;
778 1.3 hpeyerl
779 1.3 hpeyerl #ifdef INET
780 1.3 hpeyerl case AF_INET:
781 1.3 hpeyerl sin = (struct sockaddr_in *)&(ifr->ifr_addr);
782 1.3 hpeyerl if (sin->sin_addr.s_addr == INADDR_ANY) {
783 1.3 hpeyerl /*
784 1.3 hpeyerl * An IP address of INADDR_ANY means stop listening
785 1.3 hpeyerl * to the range of Ethernet multicast addresses used
786 1.3 hpeyerl * for IP.
787 1.3 hpeyerl */
788 1.3 hpeyerl bcopy(ether_ipmulticast_min, addrlo, 6);
789 1.3 hpeyerl bcopy(ether_ipmulticast_max, addrhi, 6);
790 1.3 hpeyerl }
791 1.3 hpeyerl else {
792 1.3 hpeyerl ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo);
793 1.3 hpeyerl bcopy(addrlo, addrhi, 6);
794 1.3 hpeyerl }
795 1.3 hpeyerl break;
796 1.3 hpeyerl #endif
797 1.3 hpeyerl
798 1.3 hpeyerl default:
799 1.3 hpeyerl splx(s);
800 1.3 hpeyerl return (EAFNOSUPPORT);
801 1.3 hpeyerl }
802 1.3 hpeyerl
803 1.3 hpeyerl /*
804 1.3 hpeyerl * Look up the address in our list.
805 1.3 hpeyerl */
806 1.22 is ETHER_LOOKUP_MULTI(addrlo, addrhi, ec, enm);
807 1.3 hpeyerl if (enm == NULL) {
808 1.3 hpeyerl splx(s);
809 1.3 hpeyerl return (ENXIO);
810 1.3 hpeyerl }
811 1.3 hpeyerl if (--enm->enm_refcount != 0) {
812 1.3 hpeyerl /*
813 1.3 hpeyerl * Still some claims to this record.
814 1.3 hpeyerl */
815 1.3 hpeyerl splx(s);
816 1.3 hpeyerl return (0);
817 1.3 hpeyerl }
818 1.3 hpeyerl /*
819 1.3 hpeyerl * No remaining claims to this record; unlink and free it.
820 1.3 hpeyerl */
821 1.13 mycroft LIST_REMOVE(enm, enm_list);
822 1.3 hpeyerl free(enm, M_IFMADDR);
823 1.22 is ec->ec_multicnt--;
824 1.3 hpeyerl splx(s);
825 1.3 hpeyerl /*
826 1.3 hpeyerl * Return ENETRESET to inform the driver that the list has changed
827 1.3 hpeyerl * and its reception filter should be adjusted accordingly.
828 1.3 hpeyerl */
829 1.3 hpeyerl return (ENETRESET);
830 1.3 hpeyerl }
831