if_ethersubr.c revision 1.21 1 1.21 christos /* $NetBSD: if_ethersubr.c,v 1.21 1996/10/13 02:11:00 christos 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.8 mycroft * @(#)if_ethersubr.c 8.1 (Berkeley) 6/10/93
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.15 phil #include <netinet/in.h>
59 1.1 cgd #ifdef INET
60 1.4 mycroft #include <netinet/in_var.h>
61 1.1 cgd #endif
62 1.4 mycroft #include <netinet/if_ether.h>
63 1.1 cgd
64 1.1 cgd #ifdef NS
65 1.4 mycroft #include <netns/ns.h>
66 1.4 mycroft #include <netns/ns_if.h>
67 1.1 cgd #endif
68 1.1 cgd
69 1.1 cgd #ifdef ISO
70 1.4 mycroft #include <netiso/argo_debug.h>
71 1.4 mycroft #include <netiso/iso.h>
72 1.4 mycroft #include <netiso/iso_var.h>
73 1.4 mycroft #include <netiso/iso_snpac.h>
74 1.1 cgd #endif
75 1.4 mycroft
76 1.8 mycroft #ifdef LLC
77 1.8 mycroft #include <netccitt/dll.h>
78 1.8 mycroft #include <netccitt/llc_var.h>
79 1.8 mycroft #endif
80 1.8 mycroft
81 1.8 mycroft #if defined(LLC) && defined(CCITT)
82 1.8 mycroft extern struct ifqueue pkintrq;
83 1.8 mycroft #endif
84 1.1 cgd
85 1.1 cgd u_char etherbroadcastaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
86 1.8 mycroft #define senderr(e) { error = (e); goto bad;}
87 1.1 cgd
88 1.1 cgd /*
89 1.1 cgd * Ethernet output routine.
90 1.1 cgd * Encapsulate a packet of type family for the local net.
91 1.1 cgd * Assumes that ifp is actually pointer to arpcom structure.
92 1.1 cgd */
93 1.8 mycroft int
94 1.8 mycroft ether_output(ifp, m0, dst, rt0)
95 1.1 cgd register struct ifnet *ifp;
96 1.1 cgd struct mbuf *m0;
97 1.1 cgd struct sockaddr *dst;
98 1.8 mycroft struct rtentry *rt0;
99 1.1 cgd {
100 1.10 cgd u_int16_t etype;
101 1.1 cgd int s, error = 0;
102 1.1 cgd u_char edst[6];
103 1.1 cgd register struct mbuf *m = m0;
104 1.8 mycroft register struct rtentry *rt;
105 1.1 cgd struct mbuf *mcopy = (struct mbuf *)0;
106 1.1 cgd register struct ether_header *eh;
107 1.8 mycroft struct arpcom *ac = (struct arpcom *)ifp;
108 1.1 cgd
109 1.8 mycroft if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
110 1.8 mycroft senderr(ENETDOWN);
111 1.8 mycroft ifp->if_lastchange = time;
112 1.18 christos if ((rt = rt0) != NULL) {
113 1.8 mycroft if ((rt->rt_flags & RTF_UP) == 0) {
114 1.18 christos if ((rt0 = rt = rtalloc1(dst, 1)) != NULL)
115 1.8 mycroft rt->rt_refcnt--;
116 1.8 mycroft else
117 1.8 mycroft senderr(EHOSTUNREACH);
118 1.8 mycroft }
119 1.8 mycroft if (rt->rt_flags & RTF_GATEWAY) {
120 1.8 mycroft if (rt->rt_gwroute == 0)
121 1.8 mycroft goto lookup;
122 1.8 mycroft if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
123 1.8 mycroft rtfree(rt); rt = rt0;
124 1.8 mycroft lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1);
125 1.8 mycroft if ((rt = rt->rt_gwroute) == 0)
126 1.8 mycroft senderr(EHOSTUNREACH);
127 1.8 mycroft }
128 1.8 mycroft }
129 1.8 mycroft if (rt->rt_flags & RTF_REJECT)
130 1.8 mycroft if (rt->rt_rmx.rmx_expire == 0 ||
131 1.8 mycroft time.tv_sec < rt->rt_rmx.rmx_expire)
132 1.8 mycroft senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
133 1.1 cgd }
134 1.1 cgd switch (dst->sa_family) {
135 1.1 cgd
136 1.1 cgd #ifdef INET
137 1.1 cgd case AF_INET:
138 1.8 mycroft if (!arpresolve(ac, rt, m, dst, edst))
139 1.1 cgd return (0); /* if not yet resolved */
140 1.3 hpeyerl /* If broadcasting on a simplex interface, loopback a copy */
141 1.3 hpeyerl if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX))
142 1.1 cgd mcopy = m_copy(m, 0, (int)M_COPYALL);
143 1.17 mycroft etype = htons(ETHERTYPE_IP);
144 1.8 mycroft break;
145 1.1 cgd #endif
146 1.1 cgd #ifdef NS
147 1.1 cgd case AF_NS:
148 1.17 mycroft etype = htons(ETHERTYPE_NS);
149 1.1 cgd bcopy((caddr_t)&(((struct sockaddr_ns *)dst)->sns_addr.x_host),
150 1.1 cgd (caddr_t)edst, sizeof (edst));
151 1.1 cgd if (!bcmp((caddr_t)edst, (caddr_t)&ns_thishost, sizeof(edst)))
152 1.1 cgd return (looutput(ifp, m, dst, rt));
153 1.3 hpeyerl /* If broadcasting on a simplex interface, loopback a copy */
154 1.3 hpeyerl if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX))
155 1.1 cgd mcopy = m_copy(m, 0, (int)M_COPYALL);
156 1.8 mycroft break;
157 1.1 cgd #endif
158 1.1 cgd #ifdef ISO
159 1.1 cgd case AF_ISO: {
160 1.1 cgd int snpalen;
161 1.1 cgd struct llc *l;
162 1.8 mycroft register struct sockaddr_dl *sdl;
163 1.1 cgd
164 1.8 mycroft if (rt && (sdl = (struct sockaddr_dl *)rt->rt_gateway) &&
165 1.8 mycroft sdl->sdl_family == AF_LINK && sdl->sdl_alen > 0) {
166 1.8 mycroft bcopy(LLADDR(sdl), (caddr_t)edst, sizeof(edst));
167 1.18 christos } else {
168 1.18 christos error = iso_snparesolve(ifp, (struct sockaddr_iso *)dst,
169 1.18 christos (char *)edst, &snpalen);
170 1.18 christos if (error)
171 1.18 christos goto bad; /* Not Resolved */
172 1.18 christos }
173 1.3 hpeyerl /* If broadcasting on a simplex interface, loopback a copy */
174 1.8 mycroft if (*edst & 1)
175 1.8 mycroft m->m_flags |= (M_BCAST|M_MCAST);
176 1.3 hpeyerl if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX) &&
177 1.1 cgd (mcopy = m_copy(m, 0, (int)M_COPYALL))) {
178 1.1 cgd M_PREPEND(mcopy, sizeof (*eh), M_DONTWAIT);
179 1.1 cgd if (mcopy) {
180 1.1 cgd eh = mtod(mcopy, struct ether_header *);
181 1.1 cgd bcopy((caddr_t)edst,
182 1.1 cgd (caddr_t)eh->ether_dhost, sizeof (edst));
183 1.1 cgd bcopy((caddr_t)ac->ac_enaddr,
184 1.1 cgd (caddr_t)eh->ether_shost, sizeof (edst));
185 1.1 cgd }
186 1.1 cgd }
187 1.1 cgd M_PREPEND(m, 3, M_DONTWAIT);
188 1.1 cgd if (m == NULL)
189 1.1 cgd return (0);
190 1.17 mycroft etype = htons(m->m_pkthdr.len);
191 1.1 cgd l = mtod(m, struct llc *);
192 1.1 cgd l->llc_dsap = l->llc_ssap = LLC_ISO_LSAP;
193 1.1 cgd l->llc_control = LLC_UI;
194 1.18 christos #ifdef ARGO_DEBUG
195 1.18 christos if (argo_debug[D_ETHER]) {
196 1.1 cgd int i;
197 1.21 christos printf("unoutput: sending pkt to: ");
198 1.1 cgd for (i=0; i<6; i++)
199 1.21 christos printf("%x ", edst[i] & 0xff);
200 1.21 christos printf("\n");
201 1.18 christos }
202 1.18 christos #endif
203 1.8 mycroft } break;
204 1.8 mycroft #endif /* ISO */
205 1.8 mycroft #ifdef LLC
206 1.8 mycroft /* case AF_NSAP: */
207 1.8 mycroft case AF_CCITT: {
208 1.8 mycroft register struct sockaddr_dl *sdl =
209 1.8 mycroft (struct sockaddr_dl *) rt -> rt_gateway;
210 1.8 mycroft
211 1.8 mycroft if (sdl && sdl->sdl_family == AF_LINK
212 1.8 mycroft && sdl->sdl_alen > 0) {
213 1.8 mycroft bcopy(LLADDR(sdl), (char *)edst,
214 1.8 mycroft sizeof(edst));
215 1.8 mycroft } else goto bad; /* Not a link interface ? Funny ... */
216 1.8 mycroft if ((ifp->if_flags & IFF_SIMPLEX) && (*edst & 1) &&
217 1.8 mycroft (mcopy = m_copy(m, 0, (int)M_COPYALL))) {
218 1.8 mycroft M_PREPEND(mcopy, sizeof (*eh), M_DONTWAIT);
219 1.8 mycroft if (mcopy) {
220 1.8 mycroft eh = mtod(mcopy, struct ether_header *);
221 1.8 mycroft bcopy((caddr_t)edst,
222 1.8 mycroft (caddr_t)eh->ether_dhost, sizeof (edst));
223 1.8 mycroft bcopy((caddr_t)ac->ac_enaddr,
224 1.8 mycroft (caddr_t)eh->ether_shost, sizeof (edst));
225 1.8 mycroft }
226 1.3 hpeyerl }
227 1.17 mycroft etype = htons(m->m_pkthdr.len);
228 1.8 mycroft #ifdef LLC_DEBUG
229 1.8 mycroft {
230 1.8 mycroft int i;
231 1.8 mycroft register struct llc *l = mtod(m, struct llc *);
232 1.8 mycroft
233 1.21 christos printf("ether_output: sending LLC2 pkt to: ");
234 1.8 mycroft for (i=0; i<6; i++)
235 1.21 christos printf("%x ", edst[i] & 0xff);
236 1.21 christos printf(" len 0x%x dsap 0x%x ssap 0x%x control 0x%x\n",
237 1.17 mycroft m->m_pkthdr.len, l->llc_dsap & 0xff, l->llc_ssap &0xff,
238 1.17 mycroft l->llc_control & 0xff);
239 1.8 mycroft
240 1.8 mycroft }
241 1.8 mycroft #endif /* LLC_DEBUG */
242 1.8 mycroft } break;
243 1.8 mycroft #endif /* LLC */
244 1.1 cgd
245 1.1 cgd case AF_UNSPEC:
246 1.1 cgd eh = (struct ether_header *)dst->sa_data;
247 1.1 cgd bcopy((caddr_t)eh->ether_dhost, (caddr_t)edst, sizeof (edst));
248 1.8 mycroft /* AF_UNSPEC doesn't swap the byte order of the ether_type. */
249 1.17 mycroft etype = eh->ether_type;
250 1.8 mycroft break;
251 1.1 cgd
252 1.1 cgd default:
253 1.21 christos printf("%s: can't handle af%d\n", ifp->if_xname,
254 1.1 cgd dst->sa_family);
255 1.8 mycroft senderr(EAFNOSUPPORT);
256 1.1 cgd }
257 1.1 cgd
258 1.1 cgd if (mcopy)
259 1.1 cgd (void) looutput(ifp, mcopy, dst, rt);
260 1.16 mycroft
261 1.1 cgd /*
262 1.1 cgd * Add local net header. If no space in first mbuf,
263 1.1 cgd * allocate another.
264 1.1 cgd */
265 1.1 cgd M_PREPEND(m, sizeof (struct ether_header), M_DONTWAIT);
266 1.8 mycroft if (m == 0)
267 1.8 mycroft senderr(ENOBUFS);
268 1.1 cgd eh = mtod(m, struct ether_header *);
269 1.8 mycroft bcopy((caddr_t)&etype,(caddr_t)&eh->ether_type,
270 1.1 cgd sizeof(eh->ether_type));
271 1.1 cgd bcopy((caddr_t)edst, (caddr_t)eh->ether_dhost, sizeof (edst));
272 1.1 cgd bcopy((caddr_t)ac->ac_enaddr, (caddr_t)eh->ether_shost,
273 1.1 cgd sizeof(eh->ether_shost));
274 1.1 cgd s = splimp();
275 1.1 cgd /*
276 1.1 cgd * Queue message on interface, and start output if interface
277 1.1 cgd * not yet active.
278 1.1 cgd */
279 1.1 cgd if (IF_QFULL(&ifp->if_snd)) {
280 1.1 cgd IF_DROP(&ifp->if_snd);
281 1.1 cgd splx(s);
282 1.8 mycroft senderr(ENOBUFS);
283 1.1 cgd }
284 1.14 mycroft ifp->if_obytes += m->m_pkthdr.len;
285 1.1 cgd IF_ENQUEUE(&ifp->if_snd, m);
286 1.1 cgd if ((ifp->if_flags & IFF_OACTIVE) == 0)
287 1.1 cgd (*ifp->if_start)(ifp);
288 1.1 cgd splx(s);
289 1.3 hpeyerl if (m->m_flags & M_MCAST)
290 1.1 cgd ifp->if_omcasts++;
291 1.1 cgd return (error);
292 1.1 cgd
293 1.1 cgd bad:
294 1.1 cgd if (m)
295 1.1 cgd m_freem(m);
296 1.1 cgd return (error);
297 1.1 cgd }
298 1.1 cgd
299 1.1 cgd /*
300 1.1 cgd * Process a received Ethernet packet;
301 1.1 cgd * the packet is in the mbuf chain m without
302 1.1 cgd * the ether header, which is provided separately.
303 1.1 cgd */
304 1.8 mycroft void
305 1.1 cgd ether_input(ifp, eh, m)
306 1.1 cgd struct ifnet *ifp;
307 1.1 cgd register struct ether_header *eh;
308 1.1 cgd struct mbuf *m;
309 1.1 cgd {
310 1.1 cgd register struct ifqueue *inq;
311 1.18 christos u_int16_t etype;
312 1.18 christos int s;
313 1.18 christos #if defined (ISO) || defined (LLC)
314 1.1 cgd register struct llc *l;
315 1.8 mycroft struct arpcom *ac = (struct arpcom *)ifp;
316 1.18 christos #endif
317 1.1 cgd
318 1.8 mycroft if ((ifp->if_flags & IFF_UP) == 0) {
319 1.8 mycroft m_freem(m);
320 1.8 mycroft return;
321 1.8 mycroft }
322 1.1 cgd ifp->if_lastchange = time;
323 1.1 cgd ifp->if_ibytes += m->m_pkthdr.len + sizeof (*eh);
324 1.8 mycroft if (eh->ether_dhost[0] & 1) {
325 1.8 mycroft if (bcmp((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost,
326 1.8 mycroft sizeof(etherbroadcastaddr)) == 0)
327 1.8 mycroft m->m_flags |= M_BCAST;
328 1.8 mycroft else
329 1.8 mycroft m->m_flags |= M_MCAST;
330 1.8 mycroft }
331 1.1 cgd if (m->m_flags & (M_BCAST|M_MCAST))
332 1.1 cgd ifp->if_imcasts++;
333 1.1 cgd
334 1.5 deraadt etype = ntohs(eh->ether_type);
335 1.5 deraadt switch (etype) {
336 1.1 cgd #ifdef INET
337 1.1 cgd case ETHERTYPE_IP:
338 1.1 cgd schednetisr(NETISR_IP);
339 1.1 cgd inq = &ipintrq;
340 1.1 cgd break;
341 1.1 cgd
342 1.1 cgd case ETHERTYPE_ARP:
343 1.8 mycroft schednetisr(NETISR_ARP);
344 1.8 mycroft inq = &arpintrq;
345 1.8 mycroft break;
346 1.7 glass
347 1.7 glass case ETHERTYPE_REVARP:
348 1.8 mycroft revarpinput(m); /* XXX queue? */
349 1.1 cgd return;
350 1.1 cgd #endif
351 1.1 cgd #ifdef NS
352 1.1 cgd case ETHERTYPE_NS:
353 1.1 cgd schednetisr(NETISR_NS);
354 1.1 cgd inq = &nsintrq;
355 1.1 cgd break;
356 1.1 cgd
357 1.1 cgd #endif
358 1.1 cgd default:
359 1.8 mycroft #if defined (ISO) || defined (LLC)
360 1.11 mycroft if (etype > ETHERMTU)
361 1.1 cgd goto dropanyway;
362 1.1 cgd l = mtod(m, struct llc *);
363 1.8 mycroft switch (l->llc_dsap) {
364 1.8 mycroft #ifdef ISO
365 1.8 mycroft case LLC_ISO_LSAP:
366 1.8 mycroft switch (l->llc_control) {
367 1.8 mycroft case LLC_UI:
368 1.8 mycroft /* LLC_UI_P forbidden in class 1 service */
369 1.8 mycroft if ((l->llc_dsap == LLC_ISO_LSAP) &&
370 1.8 mycroft (l->llc_ssap == LLC_ISO_LSAP)) {
371 1.8 mycroft /* LSAP for ISO */
372 1.11 mycroft if (m->m_pkthdr.len > etype)
373 1.11 mycroft m_adj(m, etype - m->m_pkthdr.len);
374 1.8 mycroft m->m_data += 3; /* XXX */
375 1.8 mycroft m->m_len -= 3; /* XXX */
376 1.8 mycroft m->m_pkthdr.len -= 3; /* XXX */
377 1.8 mycroft M_PREPEND(m, sizeof *eh, M_DONTWAIT);
378 1.8 mycroft if (m == 0)
379 1.8 mycroft return;
380 1.8 mycroft *mtod(m, struct ether_header *) = *eh;
381 1.18 christos #ifdef ARGO_DEBUG
382 1.18 christos if (argo_debug[D_ETHER])
383 1.21 christos printf("clnp packet");
384 1.18 christos #endif
385 1.8 mycroft schednetisr(NETISR_ISO);
386 1.8 mycroft inq = &clnlintrq;
387 1.8 mycroft break;
388 1.8 mycroft }
389 1.8 mycroft goto dropanyway;
390 1.8 mycroft
391 1.8 mycroft case LLC_XID:
392 1.8 mycroft case LLC_XID_P:
393 1.8 mycroft if(m->m_len < 6)
394 1.8 mycroft goto dropanyway;
395 1.8 mycroft l->llc_window = 0;
396 1.8 mycroft l->llc_fid = 9;
397 1.8 mycroft l->llc_class = 1;
398 1.8 mycroft l->llc_dsap = l->llc_ssap = 0;
399 1.8 mycroft /* Fall through to */
400 1.8 mycroft case LLC_TEST:
401 1.8 mycroft case LLC_TEST_P:
402 1.8 mycroft {
403 1.8 mycroft struct sockaddr sa;
404 1.8 mycroft register struct ether_header *eh2;
405 1.8 mycroft int i;
406 1.8 mycroft u_char c = l->llc_dsap;
407 1.8 mycroft
408 1.8 mycroft l->llc_dsap = l->llc_ssap;
409 1.8 mycroft l->llc_ssap = c;
410 1.8 mycroft if (m->m_flags & (M_BCAST | M_MCAST))
411 1.8 mycroft bcopy((caddr_t)ac->ac_enaddr,
412 1.8 mycroft (caddr_t)eh->ether_dhost, 6);
413 1.8 mycroft sa.sa_family = AF_UNSPEC;
414 1.8 mycroft sa.sa_len = sizeof(sa);
415 1.8 mycroft eh2 = (struct ether_header *)sa.sa_data;
416 1.8 mycroft for (i = 0; i < 6; i++) {
417 1.8 mycroft eh2->ether_shost[i] = c = eh->ether_dhost[i];
418 1.8 mycroft eh2->ether_dhost[i] =
419 1.8 mycroft eh->ether_dhost[i] = eh->ether_shost[i];
420 1.8 mycroft eh->ether_shost[i] = c;
421 1.8 mycroft }
422 1.8 mycroft ifp->if_output(ifp, m, &sa, NULL);
423 1.8 mycroft return;
424 1.8 mycroft }
425 1.8 mycroft default:
426 1.8 mycroft m_freem(m);
427 1.8 mycroft return;
428 1.8 mycroft }
429 1.8 mycroft break;
430 1.8 mycroft #endif /* ISO */
431 1.8 mycroft #ifdef LLC
432 1.8 mycroft case LLC_X25_LSAP:
433 1.8 mycroft {
434 1.11 mycroft if (m->m_pkthdr.len > etype)
435 1.11 mycroft m_adj(m, etype - m->m_pkthdr.len);
436 1.8 mycroft M_PREPEND(m, sizeof(struct sdl_hdr) , M_DONTWAIT);
437 1.1 cgd if (m == 0)
438 1.1 cgd return;
439 1.8 mycroft if ( !sdl_sethdrif(ifp, eh->ether_shost, LLC_X25_LSAP,
440 1.8 mycroft eh->ether_dhost, LLC_X25_LSAP, 6,
441 1.8 mycroft mtod(m, struct sdl_hdr *)))
442 1.8 mycroft panic("ETHER cons addr failure");
443 1.11 mycroft mtod(m, struct sdl_hdr *)->sdlhdr_len = etype;
444 1.8 mycroft #ifdef LLC_DEBUG
445 1.21 christos printf("llc packet\n");
446 1.8 mycroft #endif /* LLC_DEBUG */
447 1.8 mycroft schednetisr(NETISR_CCITT);
448 1.8 mycroft inq = &llcintrq;
449 1.1 cgd break;
450 1.1 cgd }
451 1.8 mycroft #endif /* LLC */
452 1.1 cgd dropanyway:
453 1.1 cgd default:
454 1.8 mycroft m_freem(m);
455 1.8 mycroft return;
456 1.8 mycroft }
457 1.8 mycroft #else /* ISO || LLC */
458 1.1 cgd m_freem(m);
459 1.1 cgd return;
460 1.8 mycroft #endif /* ISO || LLC */
461 1.1 cgd }
462 1.1 cgd
463 1.1 cgd s = splimp();
464 1.1 cgd if (IF_QFULL(inq)) {
465 1.1 cgd IF_DROP(inq);
466 1.1 cgd m_freem(m);
467 1.1 cgd } else
468 1.1 cgd IF_ENQUEUE(inq, m);
469 1.1 cgd splx(s);
470 1.1 cgd }
471 1.1 cgd
472 1.1 cgd /*
473 1.1 cgd * Convert Ethernet address to printable (loggable) representation.
474 1.1 cgd */
475 1.1 cgd static char digits[] = "0123456789abcdef";
476 1.1 cgd char *
477 1.1 cgd ether_sprintf(ap)
478 1.1 cgd register u_char *ap;
479 1.1 cgd {
480 1.1 cgd register i;
481 1.1 cgd static char etherbuf[18];
482 1.1 cgd register char *cp = etherbuf;
483 1.1 cgd
484 1.1 cgd for (i = 0; i < 6; i++) {
485 1.1 cgd *cp++ = digits[*ap >> 4];
486 1.1 cgd *cp++ = digits[*ap++ & 0xf];
487 1.1 cgd *cp++ = ':';
488 1.1 cgd }
489 1.1 cgd *--cp = 0;
490 1.1 cgd return (etherbuf);
491 1.1 cgd }
492 1.8 mycroft
493 1.8 mycroft /*
494 1.8 mycroft * Perform common duties while attaching to interface list
495 1.8 mycroft */
496 1.8 mycroft void
497 1.8 mycroft ether_ifattach(ifp)
498 1.8 mycroft register struct ifnet *ifp;
499 1.8 mycroft {
500 1.8 mycroft register struct ifaddr *ifa;
501 1.8 mycroft register struct sockaddr_dl *sdl;
502 1.8 mycroft
503 1.8 mycroft ifp->if_type = IFT_ETHER;
504 1.8 mycroft ifp->if_addrlen = 6;
505 1.8 mycroft ifp->if_hdrlen = 14;
506 1.8 mycroft ifp->if_mtu = ETHERMTU;
507 1.12 mycroft ifp->if_output = ether_output;
508 1.13 mycroft for (ifa = ifp->if_addrlist.tqh_first; ifa != 0;
509 1.13 mycroft ifa = ifa->ifa_list.tqe_next)
510 1.8 mycroft if ((sdl = (struct sockaddr_dl *)ifa->ifa_addr) &&
511 1.8 mycroft sdl->sdl_family == AF_LINK) {
512 1.8 mycroft sdl->sdl_type = IFT_ETHER;
513 1.8 mycroft sdl->sdl_alen = ifp->if_addrlen;
514 1.8 mycroft bcopy((caddr_t)((struct arpcom *)ifp)->ac_enaddr,
515 1.8 mycroft LLADDR(sdl), ifp->if_addrlen);
516 1.8 mycroft break;
517 1.8 mycroft }
518 1.13 mycroft LIST_INIT(&((struct arpcom *)ifp)->ac_multiaddrs);
519 1.8 mycroft }
520 1.8 mycroft
521 1.3 hpeyerl u_char ether_ipmulticast_min[6] = { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 };
522 1.3 hpeyerl u_char ether_ipmulticast_max[6] = { 0x01, 0x00, 0x5e, 0x7f, 0xff, 0xff };
523 1.3 hpeyerl /*
524 1.3 hpeyerl * Add an Ethernet multicast address or range of addresses to the list for a
525 1.3 hpeyerl * given interface.
526 1.3 hpeyerl */
527 1.3 hpeyerl int
528 1.3 hpeyerl ether_addmulti(ifr, ac)
529 1.3 hpeyerl struct ifreq *ifr;
530 1.3 hpeyerl register struct arpcom *ac;
531 1.3 hpeyerl {
532 1.3 hpeyerl register struct ether_multi *enm;
533 1.3 hpeyerl struct sockaddr_in *sin;
534 1.3 hpeyerl u_char addrlo[6];
535 1.3 hpeyerl u_char addrhi[6];
536 1.3 hpeyerl int s = splimp();
537 1.3 hpeyerl
538 1.3 hpeyerl switch (ifr->ifr_addr.sa_family) {
539 1.3 hpeyerl
540 1.3 hpeyerl case AF_UNSPEC:
541 1.3 hpeyerl bcopy(ifr->ifr_addr.sa_data, addrlo, 6);
542 1.3 hpeyerl bcopy(addrlo, addrhi, 6);
543 1.3 hpeyerl break;
544 1.3 hpeyerl
545 1.3 hpeyerl #ifdef INET
546 1.3 hpeyerl case AF_INET:
547 1.3 hpeyerl sin = (struct sockaddr_in *)&(ifr->ifr_addr);
548 1.3 hpeyerl if (sin->sin_addr.s_addr == INADDR_ANY) {
549 1.3 hpeyerl /*
550 1.3 hpeyerl * An IP address of INADDR_ANY means listen to all
551 1.3 hpeyerl * of the Ethernet multicast addresses used for IP.
552 1.3 hpeyerl * (This is for the sake of IP multicast routers.)
553 1.3 hpeyerl */
554 1.3 hpeyerl bcopy(ether_ipmulticast_min, addrlo, 6);
555 1.3 hpeyerl bcopy(ether_ipmulticast_max, addrhi, 6);
556 1.3 hpeyerl }
557 1.3 hpeyerl else {
558 1.3 hpeyerl ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo);
559 1.3 hpeyerl bcopy(addrlo, addrhi, 6);
560 1.3 hpeyerl }
561 1.3 hpeyerl break;
562 1.3 hpeyerl #endif
563 1.3 hpeyerl
564 1.3 hpeyerl default:
565 1.3 hpeyerl splx(s);
566 1.3 hpeyerl return (EAFNOSUPPORT);
567 1.3 hpeyerl }
568 1.3 hpeyerl
569 1.3 hpeyerl /*
570 1.3 hpeyerl * Verify that we have valid Ethernet multicast addresses.
571 1.3 hpeyerl */
572 1.3 hpeyerl if ((addrlo[0] & 0x01) != 1 || (addrhi[0] & 0x01) != 1) {
573 1.3 hpeyerl splx(s);
574 1.3 hpeyerl return (EINVAL);
575 1.3 hpeyerl }
576 1.3 hpeyerl /*
577 1.3 hpeyerl * See if the address range is already in the list.
578 1.3 hpeyerl */
579 1.3 hpeyerl ETHER_LOOKUP_MULTI(addrlo, addrhi, ac, enm);
580 1.3 hpeyerl if (enm != NULL) {
581 1.3 hpeyerl /*
582 1.3 hpeyerl * Found it; just increment the reference count.
583 1.3 hpeyerl */
584 1.3 hpeyerl ++enm->enm_refcount;
585 1.3 hpeyerl splx(s);
586 1.3 hpeyerl return (0);
587 1.3 hpeyerl }
588 1.3 hpeyerl /*
589 1.3 hpeyerl * New address or range; malloc a new multicast record
590 1.3 hpeyerl * and link it into the interface's multicast list.
591 1.3 hpeyerl */
592 1.3 hpeyerl enm = (struct ether_multi *)malloc(sizeof(*enm), M_IFMADDR, M_NOWAIT);
593 1.3 hpeyerl if (enm == NULL) {
594 1.3 hpeyerl splx(s);
595 1.3 hpeyerl return (ENOBUFS);
596 1.3 hpeyerl }
597 1.3 hpeyerl bcopy(addrlo, enm->enm_addrlo, 6);
598 1.3 hpeyerl bcopy(addrhi, enm->enm_addrhi, 6);
599 1.3 hpeyerl enm->enm_ac = ac;
600 1.3 hpeyerl enm->enm_refcount = 1;
601 1.13 mycroft LIST_INSERT_HEAD(&ac->ac_multiaddrs, enm, enm_list);
602 1.3 hpeyerl ac->ac_multicnt++;
603 1.3 hpeyerl splx(s);
604 1.3 hpeyerl /*
605 1.3 hpeyerl * Return ENETRESET to inform the driver that the list has changed
606 1.3 hpeyerl * and its reception filter should be adjusted accordingly.
607 1.3 hpeyerl */
608 1.3 hpeyerl return (ENETRESET);
609 1.3 hpeyerl }
610 1.3 hpeyerl
611 1.3 hpeyerl /*
612 1.3 hpeyerl * Delete a multicast address record.
613 1.3 hpeyerl */
614 1.3 hpeyerl int
615 1.3 hpeyerl ether_delmulti(ifr, ac)
616 1.3 hpeyerl struct ifreq *ifr;
617 1.3 hpeyerl register struct arpcom *ac;
618 1.3 hpeyerl {
619 1.3 hpeyerl register struct ether_multi *enm;
620 1.3 hpeyerl struct sockaddr_in *sin;
621 1.3 hpeyerl u_char addrlo[6];
622 1.3 hpeyerl u_char addrhi[6];
623 1.3 hpeyerl int s = splimp();
624 1.3 hpeyerl
625 1.3 hpeyerl switch (ifr->ifr_addr.sa_family) {
626 1.3 hpeyerl
627 1.3 hpeyerl case AF_UNSPEC:
628 1.3 hpeyerl bcopy(ifr->ifr_addr.sa_data, addrlo, 6);
629 1.3 hpeyerl bcopy(addrlo, addrhi, 6);
630 1.3 hpeyerl break;
631 1.3 hpeyerl
632 1.3 hpeyerl #ifdef INET
633 1.3 hpeyerl case AF_INET:
634 1.3 hpeyerl sin = (struct sockaddr_in *)&(ifr->ifr_addr);
635 1.3 hpeyerl if (sin->sin_addr.s_addr == INADDR_ANY) {
636 1.3 hpeyerl /*
637 1.3 hpeyerl * An IP address of INADDR_ANY means stop listening
638 1.3 hpeyerl * to the range of Ethernet multicast addresses used
639 1.3 hpeyerl * for IP.
640 1.3 hpeyerl */
641 1.3 hpeyerl bcopy(ether_ipmulticast_min, addrlo, 6);
642 1.3 hpeyerl bcopy(ether_ipmulticast_max, addrhi, 6);
643 1.3 hpeyerl }
644 1.3 hpeyerl else {
645 1.3 hpeyerl ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo);
646 1.3 hpeyerl bcopy(addrlo, addrhi, 6);
647 1.3 hpeyerl }
648 1.3 hpeyerl break;
649 1.3 hpeyerl #endif
650 1.3 hpeyerl
651 1.3 hpeyerl default:
652 1.3 hpeyerl splx(s);
653 1.3 hpeyerl return (EAFNOSUPPORT);
654 1.3 hpeyerl }
655 1.3 hpeyerl
656 1.3 hpeyerl /*
657 1.3 hpeyerl * Look up the address in our list.
658 1.3 hpeyerl */
659 1.3 hpeyerl ETHER_LOOKUP_MULTI(addrlo, addrhi, ac, enm);
660 1.3 hpeyerl if (enm == NULL) {
661 1.3 hpeyerl splx(s);
662 1.3 hpeyerl return (ENXIO);
663 1.3 hpeyerl }
664 1.3 hpeyerl if (--enm->enm_refcount != 0) {
665 1.3 hpeyerl /*
666 1.3 hpeyerl * Still some claims to this record.
667 1.3 hpeyerl */
668 1.3 hpeyerl splx(s);
669 1.3 hpeyerl return (0);
670 1.3 hpeyerl }
671 1.3 hpeyerl /*
672 1.3 hpeyerl * No remaining claims to this record; unlink and free it.
673 1.3 hpeyerl */
674 1.13 mycroft LIST_REMOVE(enm, enm_list);
675 1.3 hpeyerl free(enm, M_IFMADDR);
676 1.3 hpeyerl ac->ac_multicnt--;
677 1.3 hpeyerl splx(s);
678 1.3 hpeyerl /*
679 1.3 hpeyerl * Return ENETRESET to inform the driver that the list has changed
680 1.3 hpeyerl * and its reception filter should be adjusted accordingly.
681 1.3 hpeyerl */
682 1.3 hpeyerl return (ENETRESET);
683 1.3 hpeyerl }
684