if_ethersubr.c revision 1.5 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.5 deraadt * $Id: if_ethersubr.c,v 1.5 1994/01/23 23:41:14 deraadt Exp $
35 1.1 cgd */
36 1.1 cgd
37 1.4 mycroft #include <sys/param.h>
38 1.4 mycroft #include <sys/systm.h>
39 1.4 mycroft #include <sys/kernel.h>
40 1.4 mycroft #include <sys/malloc.h>
41 1.4 mycroft #include <sys/mbuf.h>
42 1.4 mycroft #include <sys/protosw.h>
43 1.4 mycroft #include <sys/socket.h>
44 1.4 mycroft #include <sys/ioctl.h>
45 1.4 mycroft #include <sys/errno.h>
46 1.4 mycroft #include <sys/syslog.h>
47 1.4 mycroft
48 1.4 mycroft #include <net/if.h>
49 1.4 mycroft #include <net/netisr.h>
50 1.4 mycroft #include <net/route.h>
51 1.4 mycroft #include <net/if_llc.h>
52 1.4 mycroft #include <net/if_dl.h>
53 1.1 cgd
54 1.1 cgd #ifdef INET
55 1.4 mycroft #include <netinet/in.h>
56 1.4 mycroft #include <netinet/in_var.h>
57 1.1 cgd #endif
58 1.4 mycroft #include <netinet/if_ether.h>
59 1.1 cgd
60 1.1 cgd #ifdef NS
61 1.4 mycroft #include <netns/ns.h>
62 1.4 mycroft #include <netns/ns_if.h>
63 1.1 cgd #endif
64 1.1 cgd
65 1.1 cgd #ifdef ISO
66 1.4 mycroft #include <netiso/argo_debug.h>
67 1.4 mycroft #include <netiso/iso.h>
68 1.4 mycroft #include <netiso/iso_var.h>
69 1.4 mycroft #include <netiso/iso_snpac.h>
70 1.1 cgd #endif
71 1.4 mycroft
72 1.4 mycroft #include <machine/cpu.h>
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.5 deraadt u_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.3 hpeyerl /* If broadcasting on a simplex interface, loopback a copy */
113 1.3 hpeyerl if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX))
114 1.1 cgd mcopy = m_copy(m, 0, (int)M_COPYALL);
115 1.1 cgd off = m->m_pkthdr.len - m->m_len;
116 1.1 cgd if (usetrailers && off > 0 && (off & 0x1ff) == 0 &&
117 1.1 cgd (m->m_flags & M_EXT) == 0 &&
118 1.1 cgd m->m_data >= m->m_pktdat + 2 * sizeof (u_short)) {
119 1.1 cgd type = ETHERTYPE_TRAIL + (off>>9);
120 1.1 cgd m->m_data -= 2 * sizeof (u_short);
121 1.1 cgd m->m_len += 2 * sizeof (u_short);
122 1.1 cgd len += 2 * sizeof (u_short);
123 1.1 cgd *mtod(m, u_short *) = htons((u_short)ETHERTYPE_IP);
124 1.1 cgd *(mtod(m, u_short *) + 1) = htons((u_short)m->m_len);
125 1.1 cgd goto gottrailertype;
126 1.1 cgd }
127 1.1 cgd type = ETHERTYPE_IP;
128 1.1 cgd goto gottype;
129 1.1 cgd #endif
130 1.1 cgd #ifdef NS
131 1.1 cgd case AF_NS:
132 1.1 cgd type = ETHERTYPE_NS;
133 1.1 cgd bcopy((caddr_t)&(((struct sockaddr_ns *)dst)->sns_addr.x_host),
134 1.1 cgd (caddr_t)edst, sizeof (edst));
135 1.1 cgd if (!bcmp((caddr_t)edst, (caddr_t)&ns_thishost, sizeof(edst)))
136 1.1 cgd return (looutput(ifp, m, dst, rt));
137 1.3 hpeyerl /* If broadcasting on a simplex interface, loopback a copy */
138 1.3 hpeyerl if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX))
139 1.1 cgd mcopy = m_copy(m, 0, (int)M_COPYALL);
140 1.1 cgd goto gottype;
141 1.1 cgd #endif
142 1.1 cgd #ifdef ISO
143 1.1 cgd case AF_ISO: {
144 1.1 cgd int snpalen;
145 1.1 cgd struct llc *l;
146 1.1 cgd
147 1.1 cgd iso_again:
148 1.1 cgd if (rt && rt->rt_gateway && (rt->rt_flags & RTF_UP)) {
149 1.1 cgd if (rt->rt_flags & RTF_GATEWAY) {
150 1.1 cgd if (rt->rt_llinfo) {
151 1.1 cgd rt = (struct rtentry *)rt->rt_llinfo;
152 1.1 cgd goto iso_again;
153 1.1 cgd }
154 1.1 cgd } else {
155 1.1 cgd register struct sockaddr_dl *sdl =
156 1.1 cgd (struct sockaddr_dl *)rt->rt_gateway;
157 1.1 cgd if (sdl && sdl->sdl_family == AF_LINK
158 1.1 cgd && sdl->sdl_alen > 0) {
159 1.1 cgd bcopy(LLADDR(sdl), (char *)edst,
160 1.1 cgd sizeof(edst));
161 1.1 cgd goto iso_resolved;
162 1.1 cgd }
163 1.1 cgd }
164 1.1 cgd }
165 1.1 cgd if ((error = iso_snparesolve(ifp, (struct sockaddr_iso *)dst,
166 1.1 cgd (char *)edst, &snpalen)) > 0)
167 1.1 cgd goto bad; /* Not Resolved */
168 1.1 cgd iso_resolved:
169 1.3 hpeyerl /* If broadcasting on a simplex interface, loopback a copy */
170 1.3 hpeyerl if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX) &&
171 1.1 cgd (mcopy = m_copy(m, 0, (int)M_COPYALL))) {
172 1.1 cgd M_PREPEND(mcopy, sizeof (*eh), M_DONTWAIT);
173 1.1 cgd if (mcopy) {
174 1.1 cgd eh = mtod(mcopy, struct ether_header *);
175 1.1 cgd bcopy((caddr_t)edst,
176 1.1 cgd (caddr_t)eh->ether_dhost, sizeof (edst));
177 1.1 cgd bcopy((caddr_t)ac->ac_enaddr,
178 1.1 cgd (caddr_t)eh->ether_shost, sizeof (edst));
179 1.1 cgd }
180 1.1 cgd }
181 1.1 cgd M_PREPEND(m, 3, M_DONTWAIT);
182 1.1 cgd if (m == NULL)
183 1.1 cgd return (0);
184 1.1 cgd type = m->m_pkthdr.len;
185 1.1 cgd l = mtod(m, struct llc *);
186 1.1 cgd l->llc_dsap = l->llc_ssap = LLC_ISO_LSAP;
187 1.1 cgd l->llc_control = LLC_UI;
188 1.1 cgd len += 3;
189 1.1 cgd IFDEBUG(D_ETHER)
190 1.1 cgd int i;
191 1.1 cgd printf("unoutput: sending pkt to: ");
192 1.1 cgd for (i=0; i<6; i++)
193 1.1 cgd printf("%x ", edst[i] & 0xff);
194 1.1 cgd printf("\n");
195 1.1 cgd ENDDEBUG
196 1.3 hpeyerl }
197 1.3 hpeyerl goto gottype;
198 1.1 cgd #endif ISO
199 1.1 cgd #ifdef RMP
200 1.1 cgd case AF_RMP:
201 1.1 cgd /*
202 1.1 cgd * This is IEEE 802.3 -- the Ethernet `type' field is
203 1.1 cgd * really a `length' field.
204 1.1 cgd */
205 1.1 cgd type = m->m_len;
206 1.1 cgd bcopy((caddr_t)dst->sa_data, (caddr_t)edst, sizeof(edst));
207 1.1 cgd break;
208 1.1 cgd #endif
209 1.1 cgd
210 1.1 cgd case AF_UNSPEC:
211 1.1 cgd eh = (struct ether_header *)dst->sa_data;
212 1.1 cgd bcopy((caddr_t)eh->ether_dhost, (caddr_t)edst, sizeof (edst));
213 1.5 deraadt /* AF_UNSPEC doesn't swap the byte order of the ether_type */
214 1.5 deraadt type = ntohs(eh->ether_type);
215 1.1 cgd goto gottype;
216 1.1 cgd
217 1.1 cgd default:
218 1.1 cgd printf("%s%d: can't handle af%d\n", ifp->if_name, ifp->if_unit,
219 1.1 cgd dst->sa_family);
220 1.1 cgd error = EAFNOSUPPORT;
221 1.1 cgd goto bad;
222 1.1 cgd }
223 1.1 cgd
224 1.1 cgd gottrailertype:
225 1.1 cgd /*
226 1.1 cgd * Packet to be sent as trailer: move first packet
227 1.1 cgd * (control information) to end of chain.
228 1.1 cgd */
229 1.1 cgd while (m->m_next)
230 1.1 cgd m = m->m_next;
231 1.1 cgd m->m_next = m0;
232 1.1 cgd m = m0->m_next;
233 1.1 cgd m0->m_next = 0;
234 1.1 cgd
235 1.1 cgd gottype:
236 1.1 cgd if (mcopy)
237 1.1 cgd (void) looutput(ifp, mcopy, dst, rt);
238 1.1 cgd /*
239 1.1 cgd * Add local net header. If no space in first mbuf,
240 1.1 cgd * allocate another.
241 1.1 cgd */
242 1.1 cgd M_PREPEND(m, sizeof (struct ether_header), M_DONTWAIT);
243 1.1 cgd if (m == 0) {
244 1.1 cgd error = ENOBUFS;
245 1.1 cgd goto bad;
246 1.1 cgd }
247 1.1 cgd eh = mtod(m, struct ether_header *);
248 1.1 cgd type = htons((u_short)type);
249 1.1 cgd bcopy((caddr_t)&type,(caddr_t)&eh->ether_type,
250 1.1 cgd sizeof(eh->ether_type));
251 1.1 cgd bcopy((caddr_t)edst, (caddr_t)eh->ether_dhost, sizeof (edst));
252 1.1 cgd bcopy((caddr_t)ac->ac_enaddr, (caddr_t)eh->ether_shost,
253 1.1 cgd sizeof(eh->ether_shost));
254 1.1 cgd s = splimp();
255 1.1 cgd /*
256 1.1 cgd * Queue message on interface, and start output if interface
257 1.1 cgd * not yet active.
258 1.1 cgd */
259 1.1 cgd if (IF_QFULL(&ifp->if_snd)) {
260 1.1 cgd IF_DROP(&ifp->if_snd);
261 1.1 cgd splx(s);
262 1.1 cgd error = ENOBUFS;
263 1.1 cgd goto bad;
264 1.1 cgd }
265 1.1 cgd IF_ENQUEUE(&ifp->if_snd, m);
266 1.1 cgd if ((ifp->if_flags & IFF_OACTIVE) == 0)
267 1.1 cgd (*ifp->if_start)(ifp);
268 1.1 cgd splx(s);
269 1.1 cgd ifp->if_obytes += len + sizeof (struct ether_header);
270 1.3 hpeyerl #ifdef MULTICAST
271 1.3 hpeyerl if (m->m_flags & M_MCAST)
272 1.3 hpeyerl #else
273 1.1 cgd if (edst[0] & 1)
274 1.3 hpeyerl #endif
275 1.1 cgd ifp->if_omcasts++;
276 1.1 cgd return (error);
277 1.1 cgd
278 1.1 cgd bad:
279 1.1 cgd if (m)
280 1.1 cgd m_freem(m);
281 1.1 cgd return (error);
282 1.1 cgd }
283 1.1 cgd
284 1.1 cgd /*
285 1.1 cgd * Process a received Ethernet packet;
286 1.1 cgd * the packet is in the mbuf chain m without
287 1.1 cgd * the ether header, which is provided separately.
288 1.1 cgd */
289 1.1 cgd ether_input(ifp, eh, m)
290 1.1 cgd struct ifnet *ifp;
291 1.1 cgd register struct ether_header *eh;
292 1.1 cgd struct mbuf *m;
293 1.1 cgd {
294 1.1 cgd register struct ifqueue *inq;
295 1.1 cgd register struct llc *l;
296 1.5 deraadt u_short etype;
297 1.1 cgd int s;
298 1.1 cgd
299 1.1 cgd ifp->if_lastchange = time;
300 1.1 cgd ifp->if_ibytes += m->m_pkthdr.len + sizeof (*eh);
301 1.3 hpeyerl #ifdef MULTICAST
302 1.3 hpeyerl if (eh->ether_dhost[0] & 1) {
303 1.3 hpeyerl if (bcmp((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost,
304 1.3 hpeyerl sizeof(etherbroadcastaddr)) == 0)
305 1.3 hpeyerl m->m_flags |= M_BCAST;
306 1.3 hpeyerl else
307 1.3 hpeyerl m->m_flags |= M_MCAST;
308 1.3 hpeyerl }
309 1.3 hpeyerl #else
310 1.1 cgd if (bcmp((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost,
311 1.1 cgd sizeof(etherbroadcastaddr)) == 0)
312 1.1 cgd m->m_flags |= M_BCAST;
313 1.1 cgd else if (eh->ether_dhost[0] & 1)
314 1.1 cgd m->m_flags |= M_MCAST;
315 1.3 hpeyerl #endif
316 1.1 cgd if (m->m_flags & (M_BCAST|M_MCAST))
317 1.1 cgd ifp->if_imcasts++;
318 1.1 cgd
319 1.5 deraadt etype = ntohs(eh->ether_type);
320 1.5 deraadt switch (etype) {
321 1.1 cgd #ifdef INET
322 1.1 cgd case ETHERTYPE_IP:
323 1.1 cgd schednetisr(NETISR_IP);
324 1.1 cgd inq = &ipintrq;
325 1.1 cgd break;
326 1.1 cgd
327 1.1 cgd case ETHERTYPE_ARP:
328 1.1 cgd arpinput((struct arpcom *)ifp, m);
329 1.1 cgd return;
330 1.1 cgd #endif
331 1.1 cgd #ifdef NS
332 1.1 cgd case ETHERTYPE_NS:
333 1.1 cgd schednetisr(NETISR_NS);
334 1.1 cgd inq = &nsintrq;
335 1.1 cgd break;
336 1.1 cgd
337 1.1 cgd #endif
338 1.1 cgd default:
339 1.1 cgd #ifdef ISO
340 1.5 deraadt if (etype > ETHERMTU)
341 1.1 cgd goto dropanyway;
342 1.1 cgd l = mtod(m, struct llc *);
343 1.1 cgd switch (l->llc_control) {
344 1.1 cgd case LLC_UI:
345 1.1 cgd /* LLC_UI_P forbidden in class 1 service */
346 1.1 cgd if ((l->llc_dsap == LLC_ISO_LSAP) &&
347 1.1 cgd (l->llc_ssap == LLC_ISO_LSAP)) {
348 1.1 cgd /* LSAP for ISO */
349 1.5 deraadt if (m->m_pkthdr.len > etype)
350 1.5 deraadt m_adj(m, etype - m->m_pkthdr.len);
351 1.1 cgd m->m_data += 3; /* XXX */
352 1.1 cgd m->m_len -= 3; /* XXX */
353 1.1 cgd m->m_pkthdr.len -= 3; /* XXX */
354 1.1 cgd M_PREPEND(m, sizeof *eh, M_DONTWAIT);
355 1.1 cgd if (m == 0)
356 1.1 cgd return;
357 1.1 cgd *mtod(m, struct ether_header *) = *eh;
358 1.1 cgd IFDEBUG(D_ETHER)
359 1.1 cgd printf("clnp packet");
360 1.1 cgd ENDDEBUG
361 1.1 cgd schednetisr(NETISR_ISO);
362 1.1 cgd inq = &clnlintrq;
363 1.1 cgd break;
364 1.1 cgd }
365 1.1 cgd goto dropanyway;
366 1.1 cgd
367 1.1 cgd case LLC_XID:
368 1.1 cgd case LLC_XID_P:
369 1.1 cgd if(m->m_len < 6)
370 1.1 cgd goto dropanyway;
371 1.1 cgd l->llc_window = 0;
372 1.1 cgd l->llc_fid = 9;
373 1.1 cgd l->llc_class = 1;
374 1.1 cgd l->llc_dsap = l->llc_ssap = 0;
375 1.1 cgd /* Fall through to */
376 1.1 cgd case LLC_TEST:
377 1.1 cgd case LLC_TEST_P:
378 1.1 cgd {
379 1.1 cgd struct sockaddr sa;
380 1.1 cgd register struct ether_header *eh2;
381 1.1 cgd int i;
382 1.1 cgd u_char c = l->llc_dsap;
383 1.1 cgd l->llc_dsap = l->llc_ssap;
384 1.1 cgd l->llc_ssap = c;
385 1.1 cgd if (m->m_flags & (M_BCAST | M_MCAST))
386 1.1 cgd bcopy((caddr_t)ac->ac_enaddr,
387 1.1 cgd (caddr_t)eh->ether_dhost, 6);
388 1.1 cgd sa.sa_family = AF_UNSPEC;
389 1.1 cgd sa.sa_len = sizeof(sa);
390 1.1 cgd eh2 = (struct ether_header *)sa.sa_data;
391 1.1 cgd for (i = 0; i < 6; i++) {
392 1.1 cgd eh2->ether_shost[i] = c = eh->ether_dhost[i];
393 1.1 cgd eh2->ether_dhost[i] =
394 1.1 cgd eh->ether_dhost[i] = eh->ether_shost[i];
395 1.1 cgd eh->ether_shost[i] = c;
396 1.1 cgd }
397 1.1 cgd ifp->if_output(ifp, m, &sa);
398 1.1 cgd return;
399 1.1 cgd }
400 1.1 cgd dropanyway:
401 1.1 cgd default:
402 1.1 cgd m_freem(m);
403 1.1 cgd return;
404 1.1 cgd }
405 1.1 cgd #else
406 1.1 cgd m_freem(m);
407 1.1 cgd return;
408 1.1 cgd #endif ISO
409 1.1 cgd }
410 1.1 cgd
411 1.1 cgd s = splimp();
412 1.1 cgd if (IF_QFULL(inq)) {
413 1.1 cgd IF_DROP(inq);
414 1.1 cgd m_freem(m);
415 1.1 cgd } else
416 1.1 cgd IF_ENQUEUE(inq, m);
417 1.1 cgd splx(s);
418 1.1 cgd }
419 1.1 cgd
420 1.1 cgd /*
421 1.1 cgd * Convert Ethernet address to printable (loggable) representation.
422 1.1 cgd */
423 1.1 cgd static char digits[] = "0123456789abcdef";
424 1.1 cgd char *
425 1.1 cgd ether_sprintf(ap)
426 1.1 cgd register u_char *ap;
427 1.1 cgd {
428 1.1 cgd register i;
429 1.1 cgd static char etherbuf[18];
430 1.1 cgd register char *cp = etherbuf;
431 1.1 cgd
432 1.1 cgd for (i = 0; i < 6; i++) {
433 1.1 cgd *cp++ = digits[*ap >> 4];
434 1.1 cgd *cp++ = digits[*ap++ & 0xf];
435 1.1 cgd *cp++ = ':';
436 1.1 cgd }
437 1.1 cgd *--cp = 0;
438 1.1 cgd return (etherbuf);
439 1.1 cgd }
440 1.3 hpeyerl
441 1.3 hpeyerl #ifdef MULTICAST
442 1.3 hpeyerl u_char ether_ipmulticast_min[6] = { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 };
443 1.3 hpeyerl u_char ether_ipmulticast_max[6] = { 0x01, 0x00, 0x5e, 0x7f, 0xff, 0xff };
444 1.3 hpeyerl
445 1.3 hpeyerl /* XXX */
446 1.3 hpeyerl #undef ac
447 1.3 hpeyerl /*
448 1.3 hpeyerl * Add an Ethernet multicast address or range of addresses to the list for a
449 1.3 hpeyerl * given interface.
450 1.3 hpeyerl */
451 1.3 hpeyerl int
452 1.3 hpeyerl ether_addmulti(ifr, ac)
453 1.3 hpeyerl struct ifreq *ifr;
454 1.3 hpeyerl register struct arpcom *ac;
455 1.3 hpeyerl {
456 1.3 hpeyerl register struct ether_multi *enm;
457 1.3 hpeyerl struct sockaddr_in *sin;
458 1.3 hpeyerl u_char addrlo[6];
459 1.3 hpeyerl u_char addrhi[6];
460 1.3 hpeyerl int s = splimp();
461 1.3 hpeyerl
462 1.3 hpeyerl switch (ifr->ifr_addr.sa_family) {
463 1.3 hpeyerl
464 1.3 hpeyerl case AF_UNSPEC:
465 1.3 hpeyerl bcopy(ifr->ifr_addr.sa_data, addrlo, 6);
466 1.3 hpeyerl bcopy(addrlo, addrhi, 6);
467 1.3 hpeyerl break;
468 1.3 hpeyerl
469 1.3 hpeyerl #ifdef INET
470 1.3 hpeyerl case AF_INET:
471 1.3 hpeyerl sin = (struct sockaddr_in *)&(ifr->ifr_addr);
472 1.3 hpeyerl if (sin->sin_addr.s_addr == INADDR_ANY) {
473 1.3 hpeyerl /*
474 1.3 hpeyerl * An IP address of INADDR_ANY means listen to all
475 1.3 hpeyerl * of the Ethernet multicast addresses used for IP.
476 1.3 hpeyerl * (This is for the sake of IP multicast routers.)
477 1.3 hpeyerl */
478 1.3 hpeyerl bcopy(ether_ipmulticast_min, addrlo, 6);
479 1.3 hpeyerl bcopy(ether_ipmulticast_max, addrhi, 6);
480 1.3 hpeyerl }
481 1.3 hpeyerl else {
482 1.3 hpeyerl ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo);
483 1.3 hpeyerl bcopy(addrlo, addrhi, 6);
484 1.3 hpeyerl }
485 1.3 hpeyerl break;
486 1.3 hpeyerl #endif
487 1.3 hpeyerl
488 1.3 hpeyerl default:
489 1.3 hpeyerl splx(s);
490 1.3 hpeyerl return (EAFNOSUPPORT);
491 1.3 hpeyerl }
492 1.3 hpeyerl
493 1.3 hpeyerl /*
494 1.3 hpeyerl * Verify that we have valid Ethernet multicast addresses.
495 1.3 hpeyerl */
496 1.3 hpeyerl if ((addrlo[0] & 0x01) != 1 || (addrhi[0] & 0x01) != 1) {
497 1.3 hpeyerl splx(s);
498 1.3 hpeyerl return (EINVAL);
499 1.3 hpeyerl }
500 1.3 hpeyerl /*
501 1.3 hpeyerl * See if the address range is already in the list.
502 1.3 hpeyerl */
503 1.3 hpeyerl ETHER_LOOKUP_MULTI(addrlo, addrhi, ac, enm);
504 1.3 hpeyerl if (enm != NULL) {
505 1.3 hpeyerl /*
506 1.3 hpeyerl * Found it; just increment the reference count.
507 1.3 hpeyerl */
508 1.3 hpeyerl ++enm->enm_refcount;
509 1.3 hpeyerl splx(s);
510 1.3 hpeyerl return (0);
511 1.3 hpeyerl }
512 1.3 hpeyerl /*
513 1.3 hpeyerl * New address or range; malloc a new multicast record
514 1.3 hpeyerl * and link it into the interface's multicast list.
515 1.3 hpeyerl */
516 1.3 hpeyerl enm = (struct ether_multi *)malloc(sizeof(*enm), M_IFMADDR, M_NOWAIT);
517 1.3 hpeyerl if (enm == NULL) {
518 1.3 hpeyerl splx(s);
519 1.3 hpeyerl return (ENOBUFS);
520 1.3 hpeyerl }
521 1.3 hpeyerl bcopy(addrlo, enm->enm_addrlo, 6);
522 1.3 hpeyerl bcopy(addrhi, enm->enm_addrhi, 6);
523 1.3 hpeyerl enm->enm_ac = ac;
524 1.3 hpeyerl enm->enm_refcount = 1;
525 1.3 hpeyerl enm->enm_next = ac->ac_multiaddrs;
526 1.3 hpeyerl ac->ac_multiaddrs = enm;
527 1.3 hpeyerl ac->ac_multicnt++;
528 1.3 hpeyerl splx(s);
529 1.3 hpeyerl /*
530 1.3 hpeyerl * Return ENETRESET to inform the driver that the list has changed
531 1.3 hpeyerl * and its reception filter should be adjusted accordingly.
532 1.3 hpeyerl */
533 1.3 hpeyerl return (ENETRESET);
534 1.3 hpeyerl }
535 1.3 hpeyerl
536 1.3 hpeyerl /*
537 1.3 hpeyerl * Delete a multicast address record.
538 1.3 hpeyerl */
539 1.3 hpeyerl int
540 1.3 hpeyerl ether_delmulti(ifr, ac)
541 1.3 hpeyerl struct ifreq *ifr;
542 1.3 hpeyerl register struct arpcom *ac;
543 1.3 hpeyerl {
544 1.3 hpeyerl register struct ether_multi *enm;
545 1.3 hpeyerl register struct ether_multi **p;
546 1.3 hpeyerl struct sockaddr_in *sin;
547 1.3 hpeyerl u_char addrlo[6];
548 1.3 hpeyerl u_char addrhi[6];
549 1.3 hpeyerl int s = splimp();
550 1.3 hpeyerl
551 1.3 hpeyerl switch (ifr->ifr_addr.sa_family) {
552 1.3 hpeyerl
553 1.3 hpeyerl case AF_UNSPEC:
554 1.3 hpeyerl bcopy(ifr->ifr_addr.sa_data, addrlo, 6);
555 1.3 hpeyerl bcopy(addrlo, addrhi, 6);
556 1.3 hpeyerl break;
557 1.3 hpeyerl
558 1.3 hpeyerl #ifdef INET
559 1.3 hpeyerl case AF_INET:
560 1.3 hpeyerl sin = (struct sockaddr_in *)&(ifr->ifr_addr);
561 1.3 hpeyerl if (sin->sin_addr.s_addr == INADDR_ANY) {
562 1.3 hpeyerl /*
563 1.3 hpeyerl * An IP address of INADDR_ANY means stop listening
564 1.3 hpeyerl * to the range of Ethernet multicast addresses used
565 1.3 hpeyerl * for IP.
566 1.3 hpeyerl */
567 1.3 hpeyerl bcopy(ether_ipmulticast_min, addrlo, 6);
568 1.3 hpeyerl bcopy(ether_ipmulticast_max, addrhi, 6);
569 1.3 hpeyerl }
570 1.3 hpeyerl else {
571 1.3 hpeyerl ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo);
572 1.3 hpeyerl bcopy(addrlo, addrhi, 6);
573 1.3 hpeyerl }
574 1.3 hpeyerl break;
575 1.3 hpeyerl #endif
576 1.3 hpeyerl
577 1.3 hpeyerl default:
578 1.3 hpeyerl splx(s);
579 1.3 hpeyerl return (EAFNOSUPPORT);
580 1.3 hpeyerl }
581 1.3 hpeyerl
582 1.3 hpeyerl /*
583 1.3 hpeyerl * Look up the address in our list.
584 1.3 hpeyerl */
585 1.3 hpeyerl ETHER_LOOKUP_MULTI(addrlo, addrhi, ac, enm);
586 1.3 hpeyerl if (enm == NULL) {
587 1.3 hpeyerl splx(s);
588 1.3 hpeyerl return (ENXIO);
589 1.3 hpeyerl }
590 1.3 hpeyerl if (--enm->enm_refcount != 0) {
591 1.3 hpeyerl /*
592 1.3 hpeyerl * Still some claims to this record.
593 1.3 hpeyerl */
594 1.3 hpeyerl splx(s);
595 1.3 hpeyerl return (0);
596 1.3 hpeyerl }
597 1.3 hpeyerl /*
598 1.3 hpeyerl * No remaining claims to this record; unlink and free it.
599 1.3 hpeyerl */
600 1.3 hpeyerl for (p = &enm->enm_ac->ac_multiaddrs;
601 1.3 hpeyerl *p != enm;
602 1.3 hpeyerl p = &(*p)->enm_next)
603 1.3 hpeyerl continue;
604 1.3 hpeyerl *p = (*p)->enm_next;
605 1.3 hpeyerl free(enm, M_IFMADDR);
606 1.3 hpeyerl ac->ac_multicnt--;
607 1.3 hpeyerl splx(s);
608 1.3 hpeyerl /*
609 1.3 hpeyerl * Return ENETRESET to inform the driver that the list has changed
610 1.3 hpeyerl * and its reception filter should be adjusted accordingly.
611 1.3 hpeyerl */
612 1.3 hpeyerl return (ENETRESET);
613 1.3 hpeyerl }
614 1.3 hpeyerl #endif
615