if_ethersubr.c revision 1.3 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.3 hpeyerl * $Id: if_ethersubr.c,v 1.3 1993/12/06 04:50:19 hpeyerl Exp $
35 1.1 cgd */
36 1.1 cgd
37 1.1 cgd #include "param.h"
38 1.1 cgd #include "systm.h"
39 1.1 cgd #include "kernel.h"
40 1.1 cgd #include "malloc.h"
41 1.1 cgd #include "mbuf.h"
42 1.1 cgd #include "protosw.h"
43 1.1 cgd #include "socket.h"
44 1.1 cgd #include "ioctl.h"
45 1.1 cgd #include "errno.h"
46 1.1 cgd #include "syslog.h"
47 1.1 cgd
48 1.1 cgd #include "if.h"
49 1.1 cgd #include "netisr.h"
50 1.1 cgd #include "route.h"
51 1.1 cgd #include "if_llc.h"
52 1.1 cgd #include "if_dl.h"
53 1.1 cgd
54 1.1 cgd #include "machine/mtpr.h"
55 1.1 cgd
56 1.1 cgd #ifdef INET
57 1.1 cgd #include "../netinet/in.h"
58 1.1 cgd #include "../netinet/in_var.h"
59 1.1 cgd #endif
60 1.1 cgd #include "../netinet/if_ether.h"
61 1.1 cgd
62 1.1 cgd #ifdef NS
63 1.1 cgd #include "../netns/ns.h"
64 1.1 cgd #include "../netns/ns_if.h"
65 1.1 cgd #endif
66 1.1 cgd
67 1.1 cgd #ifdef ISO
68 1.1 cgd #include "../netiso/argo_debug.h"
69 1.1 cgd #include "../netiso/iso.h"
70 1.1 cgd #include "../netiso/iso_var.h"
71 1.1 cgd #include "../netiso/iso_snpac.h"
72 1.1 cgd #endif
73 1.1 cgd
74 1.1 cgd u_char etherbroadcastaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
75 1.1 cgd extern struct ifnet loif;
76 1.1 cgd
77 1.1 cgd /*
78 1.1 cgd * Ethernet output routine.
79 1.1 cgd * Encapsulate a packet of type family for the local net.
80 1.1 cgd * Use trailer local net encapsulation if enough data in first
81 1.1 cgd * packet leaves a multiple of 512 bytes of data in remainder.
82 1.1 cgd * Assumes that ifp is actually pointer to arpcom structure.
83 1.1 cgd */
84 1.1 cgd ether_output(ifp, m0, dst, rt)
85 1.1 cgd register struct ifnet *ifp;
86 1.1 cgd struct mbuf *m0;
87 1.1 cgd struct sockaddr *dst;
88 1.1 cgd struct rtentry *rt;
89 1.1 cgd {
90 1.1 cgd short type;
91 1.1 cgd int s, error = 0;
92 1.1 cgd u_char edst[6];
93 1.1 cgd struct in_addr idst;
94 1.1 cgd register struct mbuf *m = m0;
95 1.1 cgd struct mbuf *mcopy = (struct mbuf *)0;
96 1.1 cgd register struct ether_header *eh;
97 1.1 cgd int usetrailers, off, len = m->m_pkthdr.len;
98 1.1 cgd #define ac ((struct arpcom *)ifp)
99 1.1 cgd
100 1.1 cgd if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) {
101 1.1 cgd error = ENETDOWN;
102 1.1 cgd goto bad;
103 1.1 cgd }
104 1.1 cgd ifp->if_lastchange = time;
105 1.1 cgd switch (dst->sa_family) {
106 1.1 cgd
107 1.1 cgd #ifdef INET
108 1.1 cgd case AF_INET:
109 1.1 cgd idst = ((struct sockaddr_in *)dst)->sin_addr;
110 1.1 cgd if (!arpresolve(ac, m, &idst, edst, &usetrailers))
111 1.1 cgd return (0); /* if not yet resolved */
112 1.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.1 cgd type = eh->ether_type;
214 1.1 cgd goto gottype;
215 1.1 cgd
216 1.1 cgd default:
217 1.1 cgd printf("%s%d: can't handle af%d\n", ifp->if_name, ifp->if_unit,
218 1.1 cgd dst->sa_family);
219 1.1 cgd error = EAFNOSUPPORT;
220 1.1 cgd goto bad;
221 1.1 cgd }
222 1.1 cgd
223 1.1 cgd gottrailertype:
224 1.1 cgd /*
225 1.1 cgd * Packet to be sent as trailer: move first packet
226 1.1 cgd * (control information) to end of chain.
227 1.1 cgd */
228 1.1 cgd while (m->m_next)
229 1.1 cgd m = m->m_next;
230 1.1 cgd m->m_next = m0;
231 1.1 cgd m = m0->m_next;
232 1.1 cgd m0->m_next = 0;
233 1.1 cgd
234 1.1 cgd gottype:
235 1.1 cgd if (mcopy)
236 1.1 cgd (void) looutput(ifp, mcopy, dst, rt);
237 1.1 cgd /*
238 1.1 cgd * Add local net header. If no space in first mbuf,
239 1.1 cgd * allocate another.
240 1.1 cgd */
241 1.1 cgd M_PREPEND(m, sizeof (struct ether_header), M_DONTWAIT);
242 1.1 cgd if (m == 0) {
243 1.1 cgd error = ENOBUFS;
244 1.1 cgd goto bad;
245 1.1 cgd }
246 1.1 cgd eh = mtod(m, struct ether_header *);
247 1.1 cgd type = htons((u_short)type);
248 1.1 cgd bcopy((caddr_t)&type,(caddr_t)&eh->ether_type,
249 1.1 cgd sizeof(eh->ether_type));
250 1.1 cgd bcopy((caddr_t)edst, (caddr_t)eh->ether_dhost, sizeof (edst));
251 1.1 cgd bcopy((caddr_t)ac->ac_enaddr, (caddr_t)eh->ether_shost,
252 1.1 cgd sizeof(eh->ether_shost));
253 1.1 cgd s = splimp();
254 1.1 cgd /*
255 1.1 cgd * Queue message on interface, and start output if interface
256 1.1 cgd * not yet active.
257 1.1 cgd */
258 1.1 cgd if (IF_QFULL(&ifp->if_snd)) {
259 1.1 cgd IF_DROP(&ifp->if_snd);
260 1.1 cgd splx(s);
261 1.1 cgd error = ENOBUFS;
262 1.1 cgd goto bad;
263 1.1 cgd }
264 1.1 cgd IF_ENQUEUE(&ifp->if_snd, m);
265 1.1 cgd if ((ifp->if_flags & IFF_OACTIVE) == 0)
266 1.1 cgd (*ifp->if_start)(ifp);
267 1.1 cgd splx(s);
268 1.1 cgd ifp->if_obytes += len + sizeof (struct ether_header);
269 1.3 hpeyerl #ifdef MULTICAST
270 1.3 hpeyerl if (m->m_flags & M_MCAST)
271 1.3 hpeyerl #else
272 1.1 cgd if (edst[0] & 1)
273 1.3 hpeyerl #endif
274 1.1 cgd ifp->if_omcasts++;
275 1.1 cgd return (error);
276 1.1 cgd
277 1.1 cgd bad:
278 1.1 cgd if (m)
279 1.1 cgd m_freem(m);
280 1.1 cgd return (error);
281 1.1 cgd }
282 1.1 cgd
283 1.1 cgd /*
284 1.1 cgd * Process a received Ethernet packet;
285 1.1 cgd * the packet is in the mbuf chain m without
286 1.1 cgd * the ether header, which is provided separately.
287 1.1 cgd */
288 1.1 cgd ether_input(ifp, eh, m)
289 1.1 cgd struct ifnet *ifp;
290 1.1 cgd register struct ether_header *eh;
291 1.1 cgd struct mbuf *m;
292 1.1 cgd {
293 1.1 cgd register struct ifqueue *inq;
294 1.1 cgd register struct llc *l;
295 1.1 cgd int s;
296 1.1 cgd
297 1.1 cgd ifp->if_lastchange = time;
298 1.1 cgd ifp->if_ibytes += m->m_pkthdr.len + sizeof (*eh);
299 1.3 hpeyerl #ifdef MULTICAST
300 1.3 hpeyerl if (eh->ether_dhost[0] & 1) {
301 1.3 hpeyerl if (bcmp((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost,
302 1.3 hpeyerl sizeof(etherbroadcastaddr)) == 0)
303 1.3 hpeyerl m->m_flags |= M_BCAST;
304 1.3 hpeyerl else
305 1.3 hpeyerl m->m_flags |= M_MCAST;
306 1.3 hpeyerl }
307 1.3 hpeyerl #else
308 1.1 cgd if (bcmp((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost,
309 1.1 cgd sizeof(etherbroadcastaddr)) == 0)
310 1.1 cgd m->m_flags |= M_BCAST;
311 1.1 cgd else if (eh->ether_dhost[0] & 1)
312 1.1 cgd m->m_flags |= M_MCAST;
313 1.3 hpeyerl #endif
314 1.1 cgd if (m->m_flags & (M_BCAST|M_MCAST))
315 1.1 cgd ifp->if_imcasts++;
316 1.1 cgd
317 1.1 cgd switch (eh->ether_type) {
318 1.1 cgd #ifdef INET
319 1.1 cgd case ETHERTYPE_IP:
320 1.1 cgd schednetisr(NETISR_IP);
321 1.1 cgd inq = &ipintrq;
322 1.1 cgd break;
323 1.1 cgd
324 1.1 cgd case ETHERTYPE_ARP:
325 1.1 cgd arpinput((struct arpcom *)ifp, m);
326 1.1 cgd return;
327 1.1 cgd #endif
328 1.1 cgd #ifdef NS
329 1.1 cgd case ETHERTYPE_NS:
330 1.1 cgd schednetisr(NETISR_NS);
331 1.1 cgd inq = &nsintrq;
332 1.1 cgd break;
333 1.1 cgd
334 1.1 cgd #endif
335 1.1 cgd default:
336 1.1 cgd #ifdef ISO
337 1.1 cgd if (eh->ether_type > ETHERMTU)
338 1.1 cgd goto dropanyway;
339 1.1 cgd l = mtod(m, struct llc *);
340 1.1 cgd switch (l->llc_control) {
341 1.1 cgd case LLC_UI:
342 1.1 cgd /* LLC_UI_P forbidden in class 1 service */
343 1.1 cgd if ((l->llc_dsap == LLC_ISO_LSAP) &&
344 1.1 cgd (l->llc_ssap == LLC_ISO_LSAP)) {
345 1.1 cgd /* LSAP for ISO */
346 1.1 cgd if (m->m_pkthdr.len > eh->ether_type)
347 1.1 cgd m_adj(m, eh->ether_type - m->m_pkthdr.len);
348 1.1 cgd m->m_data += 3; /* XXX */
349 1.1 cgd m->m_len -= 3; /* XXX */
350 1.1 cgd m->m_pkthdr.len -= 3; /* XXX */
351 1.1 cgd M_PREPEND(m, sizeof *eh, M_DONTWAIT);
352 1.1 cgd if (m == 0)
353 1.1 cgd return;
354 1.1 cgd *mtod(m, struct ether_header *) = *eh;
355 1.1 cgd IFDEBUG(D_ETHER)
356 1.1 cgd printf("clnp packet");
357 1.1 cgd ENDDEBUG
358 1.1 cgd schednetisr(NETISR_ISO);
359 1.1 cgd inq = &clnlintrq;
360 1.1 cgd break;
361 1.1 cgd }
362 1.1 cgd goto dropanyway;
363 1.1 cgd
364 1.1 cgd case LLC_XID:
365 1.1 cgd case LLC_XID_P:
366 1.1 cgd if(m->m_len < 6)
367 1.1 cgd goto dropanyway;
368 1.1 cgd l->llc_window = 0;
369 1.1 cgd l->llc_fid = 9;
370 1.1 cgd l->llc_class = 1;
371 1.1 cgd l->llc_dsap = l->llc_ssap = 0;
372 1.1 cgd /* Fall through to */
373 1.1 cgd case LLC_TEST:
374 1.1 cgd case LLC_TEST_P:
375 1.1 cgd {
376 1.1 cgd struct sockaddr sa;
377 1.1 cgd register struct ether_header *eh2;
378 1.1 cgd int i;
379 1.1 cgd u_char c = l->llc_dsap;
380 1.1 cgd l->llc_dsap = l->llc_ssap;
381 1.1 cgd l->llc_ssap = c;
382 1.1 cgd if (m->m_flags & (M_BCAST | M_MCAST))
383 1.1 cgd bcopy((caddr_t)ac->ac_enaddr,
384 1.1 cgd (caddr_t)eh->ether_dhost, 6);
385 1.1 cgd sa.sa_family = AF_UNSPEC;
386 1.1 cgd sa.sa_len = sizeof(sa);
387 1.1 cgd eh2 = (struct ether_header *)sa.sa_data;
388 1.1 cgd for (i = 0; i < 6; i++) {
389 1.1 cgd eh2->ether_shost[i] = c = eh->ether_dhost[i];
390 1.1 cgd eh2->ether_dhost[i] =
391 1.1 cgd eh->ether_dhost[i] = eh->ether_shost[i];
392 1.1 cgd eh->ether_shost[i] = c;
393 1.1 cgd }
394 1.1 cgd ifp->if_output(ifp, m, &sa);
395 1.1 cgd return;
396 1.1 cgd }
397 1.1 cgd dropanyway:
398 1.1 cgd default:
399 1.1 cgd m_freem(m);
400 1.1 cgd return;
401 1.1 cgd }
402 1.1 cgd #else
403 1.1 cgd m_freem(m);
404 1.1 cgd return;
405 1.1 cgd #endif ISO
406 1.1 cgd }
407 1.1 cgd
408 1.1 cgd s = splimp();
409 1.1 cgd if (IF_QFULL(inq)) {
410 1.1 cgd IF_DROP(inq);
411 1.1 cgd m_freem(m);
412 1.1 cgd } else
413 1.1 cgd IF_ENQUEUE(inq, m);
414 1.1 cgd splx(s);
415 1.1 cgd }
416 1.1 cgd
417 1.1 cgd /*
418 1.1 cgd * Convert Ethernet address to printable (loggable) representation.
419 1.1 cgd */
420 1.1 cgd static char digits[] = "0123456789abcdef";
421 1.1 cgd char *
422 1.1 cgd ether_sprintf(ap)
423 1.1 cgd register u_char *ap;
424 1.1 cgd {
425 1.1 cgd register i;
426 1.1 cgd static char etherbuf[18];
427 1.1 cgd register char *cp = etherbuf;
428 1.1 cgd
429 1.1 cgd for (i = 0; i < 6; i++) {
430 1.1 cgd *cp++ = digits[*ap >> 4];
431 1.1 cgd *cp++ = digits[*ap++ & 0xf];
432 1.1 cgd *cp++ = ':';
433 1.1 cgd }
434 1.1 cgd *--cp = 0;
435 1.1 cgd return (etherbuf);
436 1.1 cgd }
437 1.3 hpeyerl
438 1.3 hpeyerl #ifdef MULTICAST
439 1.3 hpeyerl u_char ether_ipmulticast_min[6] = { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 };
440 1.3 hpeyerl u_char ether_ipmulticast_max[6] = { 0x01, 0x00, 0x5e, 0x7f, 0xff, 0xff };
441 1.3 hpeyerl
442 1.3 hpeyerl /* XXX */
443 1.3 hpeyerl #undef ac
444 1.3 hpeyerl /*
445 1.3 hpeyerl * Add an Ethernet multicast address or range of addresses to the list for a
446 1.3 hpeyerl * given interface.
447 1.3 hpeyerl */
448 1.3 hpeyerl int
449 1.3 hpeyerl ether_addmulti(ifr, ac)
450 1.3 hpeyerl struct ifreq *ifr;
451 1.3 hpeyerl register struct arpcom *ac;
452 1.3 hpeyerl {
453 1.3 hpeyerl register struct ether_multi *enm;
454 1.3 hpeyerl struct sockaddr_in *sin;
455 1.3 hpeyerl u_char addrlo[6];
456 1.3 hpeyerl u_char addrhi[6];
457 1.3 hpeyerl int s = splimp();
458 1.3 hpeyerl
459 1.3 hpeyerl switch (ifr->ifr_addr.sa_family) {
460 1.3 hpeyerl
461 1.3 hpeyerl case AF_UNSPEC:
462 1.3 hpeyerl bcopy(ifr->ifr_addr.sa_data, addrlo, 6);
463 1.3 hpeyerl bcopy(addrlo, addrhi, 6);
464 1.3 hpeyerl break;
465 1.3 hpeyerl
466 1.3 hpeyerl #ifdef INET
467 1.3 hpeyerl case AF_INET:
468 1.3 hpeyerl sin = (struct sockaddr_in *)&(ifr->ifr_addr);
469 1.3 hpeyerl if (sin->sin_addr.s_addr == INADDR_ANY) {
470 1.3 hpeyerl /*
471 1.3 hpeyerl * An IP address of INADDR_ANY means listen to all
472 1.3 hpeyerl * of the Ethernet multicast addresses used for IP.
473 1.3 hpeyerl * (This is for the sake of IP multicast routers.)
474 1.3 hpeyerl */
475 1.3 hpeyerl bcopy(ether_ipmulticast_min, addrlo, 6);
476 1.3 hpeyerl bcopy(ether_ipmulticast_max, addrhi, 6);
477 1.3 hpeyerl }
478 1.3 hpeyerl else {
479 1.3 hpeyerl ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo);
480 1.3 hpeyerl bcopy(addrlo, addrhi, 6);
481 1.3 hpeyerl }
482 1.3 hpeyerl break;
483 1.3 hpeyerl #endif
484 1.3 hpeyerl
485 1.3 hpeyerl default:
486 1.3 hpeyerl splx(s);
487 1.3 hpeyerl return (EAFNOSUPPORT);
488 1.3 hpeyerl }
489 1.3 hpeyerl
490 1.3 hpeyerl /*
491 1.3 hpeyerl * Verify that we have valid Ethernet multicast addresses.
492 1.3 hpeyerl */
493 1.3 hpeyerl if ((addrlo[0] & 0x01) != 1 || (addrhi[0] & 0x01) != 1) {
494 1.3 hpeyerl splx(s);
495 1.3 hpeyerl return (EINVAL);
496 1.3 hpeyerl }
497 1.3 hpeyerl /*
498 1.3 hpeyerl * See if the address range is already in the list.
499 1.3 hpeyerl */
500 1.3 hpeyerl ETHER_LOOKUP_MULTI(addrlo, addrhi, ac, enm);
501 1.3 hpeyerl if (enm != NULL) {
502 1.3 hpeyerl /*
503 1.3 hpeyerl * Found it; just increment the reference count.
504 1.3 hpeyerl */
505 1.3 hpeyerl ++enm->enm_refcount;
506 1.3 hpeyerl splx(s);
507 1.3 hpeyerl return (0);
508 1.3 hpeyerl }
509 1.3 hpeyerl /*
510 1.3 hpeyerl * New address or range; malloc a new multicast record
511 1.3 hpeyerl * and link it into the interface's multicast list.
512 1.3 hpeyerl */
513 1.3 hpeyerl enm = (struct ether_multi *)malloc(sizeof(*enm), M_IFMADDR, M_NOWAIT);
514 1.3 hpeyerl if (enm == NULL) {
515 1.3 hpeyerl splx(s);
516 1.3 hpeyerl return (ENOBUFS);
517 1.3 hpeyerl }
518 1.3 hpeyerl bcopy(addrlo, enm->enm_addrlo, 6);
519 1.3 hpeyerl bcopy(addrhi, enm->enm_addrhi, 6);
520 1.3 hpeyerl enm->enm_ac = ac;
521 1.3 hpeyerl enm->enm_refcount = 1;
522 1.3 hpeyerl enm->enm_next = ac->ac_multiaddrs;
523 1.3 hpeyerl ac->ac_multiaddrs = enm;
524 1.3 hpeyerl ac->ac_multicnt++;
525 1.3 hpeyerl splx(s);
526 1.3 hpeyerl /*
527 1.3 hpeyerl * Return ENETRESET to inform the driver that the list has changed
528 1.3 hpeyerl * and its reception filter should be adjusted accordingly.
529 1.3 hpeyerl */
530 1.3 hpeyerl return (ENETRESET);
531 1.3 hpeyerl }
532 1.3 hpeyerl
533 1.3 hpeyerl /*
534 1.3 hpeyerl * Delete a multicast address record.
535 1.3 hpeyerl */
536 1.3 hpeyerl int
537 1.3 hpeyerl ether_delmulti(ifr, ac)
538 1.3 hpeyerl struct ifreq *ifr;
539 1.3 hpeyerl register struct arpcom *ac;
540 1.3 hpeyerl {
541 1.3 hpeyerl register struct ether_multi *enm;
542 1.3 hpeyerl register struct ether_multi **p;
543 1.3 hpeyerl struct sockaddr_in *sin;
544 1.3 hpeyerl u_char addrlo[6];
545 1.3 hpeyerl u_char addrhi[6];
546 1.3 hpeyerl int s = splimp();
547 1.3 hpeyerl
548 1.3 hpeyerl switch (ifr->ifr_addr.sa_family) {
549 1.3 hpeyerl
550 1.3 hpeyerl case AF_UNSPEC:
551 1.3 hpeyerl bcopy(ifr->ifr_addr.sa_data, addrlo, 6);
552 1.3 hpeyerl bcopy(addrlo, addrhi, 6);
553 1.3 hpeyerl break;
554 1.3 hpeyerl
555 1.3 hpeyerl #ifdef INET
556 1.3 hpeyerl case AF_INET:
557 1.3 hpeyerl sin = (struct sockaddr_in *)&(ifr->ifr_addr);
558 1.3 hpeyerl if (sin->sin_addr.s_addr == INADDR_ANY) {
559 1.3 hpeyerl /*
560 1.3 hpeyerl * An IP address of INADDR_ANY means stop listening
561 1.3 hpeyerl * to the range of Ethernet multicast addresses used
562 1.3 hpeyerl * for IP.
563 1.3 hpeyerl */
564 1.3 hpeyerl bcopy(ether_ipmulticast_min, addrlo, 6);
565 1.3 hpeyerl bcopy(ether_ipmulticast_max, addrhi, 6);
566 1.3 hpeyerl }
567 1.3 hpeyerl else {
568 1.3 hpeyerl ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo);
569 1.3 hpeyerl bcopy(addrlo, addrhi, 6);
570 1.3 hpeyerl }
571 1.3 hpeyerl break;
572 1.3 hpeyerl #endif
573 1.3 hpeyerl
574 1.3 hpeyerl default:
575 1.3 hpeyerl splx(s);
576 1.3 hpeyerl return (EAFNOSUPPORT);
577 1.3 hpeyerl }
578 1.3 hpeyerl
579 1.3 hpeyerl /*
580 1.3 hpeyerl * Look up the address in our list.
581 1.3 hpeyerl */
582 1.3 hpeyerl ETHER_LOOKUP_MULTI(addrlo, addrhi, ac, enm);
583 1.3 hpeyerl if (enm == NULL) {
584 1.3 hpeyerl splx(s);
585 1.3 hpeyerl return (ENXIO);
586 1.3 hpeyerl }
587 1.3 hpeyerl if (--enm->enm_refcount != 0) {
588 1.3 hpeyerl /*
589 1.3 hpeyerl * Still some claims to this record.
590 1.3 hpeyerl */
591 1.3 hpeyerl splx(s);
592 1.3 hpeyerl return (0);
593 1.3 hpeyerl }
594 1.3 hpeyerl /*
595 1.3 hpeyerl * No remaining claims to this record; unlink and free it.
596 1.3 hpeyerl */
597 1.3 hpeyerl for (p = &enm->enm_ac->ac_multiaddrs;
598 1.3 hpeyerl *p != enm;
599 1.3 hpeyerl p = &(*p)->enm_next)
600 1.3 hpeyerl continue;
601 1.3 hpeyerl *p = (*p)->enm_next;
602 1.3 hpeyerl free(enm, M_IFMADDR);
603 1.3 hpeyerl ac->ac_multicnt--;
604 1.3 hpeyerl splx(s);
605 1.3 hpeyerl /*
606 1.3 hpeyerl * Return ENETRESET to inform the driver that the list has changed
607 1.3 hpeyerl * and its reception filter should be adjusted accordingly.
608 1.3 hpeyerl */
609 1.3 hpeyerl return (ENETRESET);
610 1.3 hpeyerl }
611 1.3 hpeyerl #endif
612