if_ethersubr.c revision 1.7 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.7 glass * $Id: if_ethersubr.c,v 1.7 1994/04/18 06:18:10 glass 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 if (m->m_flags & M_MCAST)
271 1.1 cgd ifp->if_omcasts++;
272 1.1 cgd return (error);
273 1.1 cgd
274 1.1 cgd bad:
275 1.1 cgd if (m)
276 1.1 cgd m_freem(m);
277 1.1 cgd return (error);
278 1.1 cgd }
279 1.1 cgd
280 1.1 cgd /*
281 1.1 cgd * Process a received Ethernet packet;
282 1.1 cgd * the packet is in the mbuf chain m without
283 1.1 cgd * the ether header, which is provided separately.
284 1.1 cgd */
285 1.1 cgd ether_input(ifp, eh, m)
286 1.1 cgd struct ifnet *ifp;
287 1.1 cgd register struct ether_header *eh;
288 1.1 cgd struct mbuf *m;
289 1.1 cgd {
290 1.1 cgd register struct ifqueue *inq;
291 1.1 cgd register struct llc *l;
292 1.5 deraadt u_short etype;
293 1.1 cgd int s;
294 1.1 cgd
295 1.1 cgd ifp->if_lastchange = time;
296 1.1 cgd ifp->if_ibytes += m->m_pkthdr.len + sizeof (*eh);
297 1.3 hpeyerl if (eh->ether_dhost[0] & 1) {
298 1.3 hpeyerl if (bcmp((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost,
299 1.3 hpeyerl sizeof(etherbroadcastaddr)) == 0)
300 1.3 hpeyerl m->m_flags |= M_BCAST;
301 1.3 hpeyerl else
302 1.3 hpeyerl m->m_flags |= M_MCAST;
303 1.3 hpeyerl }
304 1.1 cgd if (m->m_flags & (M_BCAST|M_MCAST))
305 1.1 cgd ifp->if_imcasts++;
306 1.1 cgd
307 1.5 deraadt etype = ntohs(eh->ether_type);
308 1.5 deraadt switch (etype) {
309 1.1 cgd #ifdef INET
310 1.1 cgd case ETHERTYPE_IP:
311 1.1 cgd schednetisr(NETISR_IP);
312 1.1 cgd inq = &ipintrq;
313 1.1 cgd break;
314 1.1 cgd
315 1.1 cgd case ETHERTYPE_ARP:
316 1.1 cgd arpinput((struct arpcom *)ifp, m);
317 1.7 glass return;
318 1.7 glass
319 1.7 glass case ETHERTYPE_REVARP:
320 1.7 glass revarpinput((struct arpcom *) ifp, m);
321 1.1 cgd return;
322 1.1 cgd #endif
323 1.1 cgd #ifdef NS
324 1.1 cgd case ETHERTYPE_NS:
325 1.1 cgd schednetisr(NETISR_NS);
326 1.1 cgd inq = &nsintrq;
327 1.1 cgd break;
328 1.1 cgd
329 1.1 cgd #endif
330 1.1 cgd default:
331 1.1 cgd #ifdef ISO
332 1.5 deraadt if (etype > ETHERMTU)
333 1.1 cgd goto dropanyway;
334 1.1 cgd l = mtod(m, struct llc *);
335 1.1 cgd switch (l->llc_control) {
336 1.1 cgd case LLC_UI:
337 1.1 cgd /* LLC_UI_P forbidden in class 1 service */
338 1.1 cgd if ((l->llc_dsap == LLC_ISO_LSAP) &&
339 1.1 cgd (l->llc_ssap == LLC_ISO_LSAP)) {
340 1.1 cgd /* LSAP for ISO */
341 1.5 deraadt if (m->m_pkthdr.len > etype)
342 1.5 deraadt m_adj(m, etype - m->m_pkthdr.len);
343 1.1 cgd m->m_data += 3; /* XXX */
344 1.1 cgd m->m_len -= 3; /* XXX */
345 1.1 cgd m->m_pkthdr.len -= 3; /* XXX */
346 1.1 cgd M_PREPEND(m, sizeof *eh, M_DONTWAIT);
347 1.1 cgd if (m == 0)
348 1.1 cgd return;
349 1.1 cgd *mtod(m, struct ether_header *) = *eh;
350 1.1 cgd IFDEBUG(D_ETHER)
351 1.1 cgd printf("clnp packet");
352 1.1 cgd ENDDEBUG
353 1.1 cgd schednetisr(NETISR_ISO);
354 1.1 cgd inq = &clnlintrq;
355 1.1 cgd break;
356 1.1 cgd }
357 1.1 cgd goto dropanyway;
358 1.1 cgd
359 1.1 cgd case LLC_XID:
360 1.1 cgd case LLC_XID_P:
361 1.1 cgd if(m->m_len < 6)
362 1.1 cgd goto dropanyway;
363 1.1 cgd l->llc_window = 0;
364 1.1 cgd l->llc_fid = 9;
365 1.1 cgd l->llc_class = 1;
366 1.1 cgd l->llc_dsap = l->llc_ssap = 0;
367 1.1 cgd /* Fall through to */
368 1.1 cgd case LLC_TEST:
369 1.1 cgd case LLC_TEST_P:
370 1.1 cgd {
371 1.1 cgd struct sockaddr sa;
372 1.1 cgd register struct ether_header *eh2;
373 1.1 cgd int i;
374 1.1 cgd u_char c = l->llc_dsap;
375 1.1 cgd l->llc_dsap = l->llc_ssap;
376 1.1 cgd l->llc_ssap = c;
377 1.1 cgd if (m->m_flags & (M_BCAST | M_MCAST))
378 1.1 cgd bcopy((caddr_t)ac->ac_enaddr,
379 1.1 cgd (caddr_t)eh->ether_dhost, 6);
380 1.1 cgd sa.sa_family = AF_UNSPEC;
381 1.1 cgd sa.sa_len = sizeof(sa);
382 1.1 cgd eh2 = (struct ether_header *)sa.sa_data;
383 1.1 cgd for (i = 0; i < 6; i++) {
384 1.1 cgd eh2->ether_shost[i] = c = eh->ether_dhost[i];
385 1.1 cgd eh2->ether_dhost[i] =
386 1.1 cgd eh->ether_dhost[i] = eh->ether_shost[i];
387 1.1 cgd eh->ether_shost[i] = c;
388 1.1 cgd }
389 1.1 cgd ifp->if_output(ifp, m, &sa);
390 1.1 cgd return;
391 1.1 cgd }
392 1.1 cgd dropanyway:
393 1.1 cgd default:
394 1.1 cgd m_freem(m);
395 1.1 cgd return;
396 1.1 cgd }
397 1.1 cgd #else
398 1.1 cgd m_freem(m);
399 1.1 cgd return;
400 1.1 cgd #endif ISO
401 1.1 cgd }
402 1.1 cgd
403 1.1 cgd s = splimp();
404 1.1 cgd if (IF_QFULL(inq)) {
405 1.1 cgd IF_DROP(inq);
406 1.1 cgd m_freem(m);
407 1.1 cgd } else
408 1.1 cgd IF_ENQUEUE(inq, m);
409 1.1 cgd splx(s);
410 1.1 cgd }
411 1.1 cgd
412 1.1 cgd /*
413 1.1 cgd * Convert Ethernet address to printable (loggable) representation.
414 1.1 cgd */
415 1.1 cgd static char digits[] = "0123456789abcdef";
416 1.1 cgd char *
417 1.1 cgd ether_sprintf(ap)
418 1.1 cgd register u_char *ap;
419 1.1 cgd {
420 1.1 cgd register i;
421 1.1 cgd static char etherbuf[18];
422 1.1 cgd register char *cp = etherbuf;
423 1.1 cgd
424 1.1 cgd for (i = 0; i < 6; i++) {
425 1.1 cgd *cp++ = digits[*ap >> 4];
426 1.1 cgd *cp++ = digits[*ap++ & 0xf];
427 1.1 cgd *cp++ = ':';
428 1.1 cgd }
429 1.1 cgd *--cp = 0;
430 1.1 cgd return (etherbuf);
431 1.1 cgd }
432 1.3 hpeyerl
433 1.3 hpeyerl u_char ether_ipmulticast_min[6] = { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 };
434 1.3 hpeyerl u_char ether_ipmulticast_max[6] = { 0x01, 0x00, 0x5e, 0x7f, 0xff, 0xff };
435 1.3 hpeyerl
436 1.3 hpeyerl /* XXX */
437 1.3 hpeyerl #undef ac
438 1.3 hpeyerl /*
439 1.3 hpeyerl * Add an Ethernet multicast address or range of addresses to the list for a
440 1.3 hpeyerl * given interface.
441 1.3 hpeyerl */
442 1.3 hpeyerl int
443 1.3 hpeyerl ether_addmulti(ifr, ac)
444 1.3 hpeyerl struct ifreq *ifr;
445 1.3 hpeyerl register struct arpcom *ac;
446 1.3 hpeyerl {
447 1.3 hpeyerl register struct ether_multi *enm;
448 1.3 hpeyerl struct sockaddr_in *sin;
449 1.3 hpeyerl u_char addrlo[6];
450 1.3 hpeyerl u_char addrhi[6];
451 1.3 hpeyerl int s = splimp();
452 1.3 hpeyerl
453 1.3 hpeyerl switch (ifr->ifr_addr.sa_family) {
454 1.3 hpeyerl
455 1.3 hpeyerl case AF_UNSPEC:
456 1.3 hpeyerl bcopy(ifr->ifr_addr.sa_data, addrlo, 6);
457 1.3 hpeyerl bcopy(addrlo, addrhi, 6);
458 1.3 hpeyerl break;
459 1.3 hpeyerl
460 1.3 hpeyerl #ifdef INET
461 1.3 hpeyerl case AF_INET:
462 1.3 hpeyerl sin = (struct sockaddr_in *)&(ifr->ifr_addr);
463 1.3 hpeyerl if (sin->sin_addr.s_addr == INADDR_ANY) {
464 1.3 hpeyerl /*
465 1.3 hpeyerl * An IP address of INADDR_ANY means listen to all
466 1.3 hpeyerl * of the Ethernet multicast addresses used for IP.
467 1.3 hpeyerl * (This is for the sake of IP multicast routers.)
468 1.3 hpeyerl */
469 1.3 hpeyerl bcopy(ether_ipmulticast_min, addrlo, 6);
470 1.3 hpeyerl bcopy(ether_ipmulticast_max, addrhi, 6);
471 1.3 hpeyerl }
472 1.3 hpeyerl else {
473 1.3 hpeyerl ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo);
474 1.3 hpeyerl bcopy(addrlo, addrhi, 6);
475 1.3 hpeyerl }
476 1.3 hpeyerl break;
477 1.3 hpeyerl #endif
478 1.3 hpeyerl
479 1.3 hpeyerl default:
480 1.3 hpeyerl splx(s);
481 1.3 hpeyerl return (EAFNOSUPPORT);
482 1.3 hpeyerl }
483 1.3 hpeyerl
484 1.3 hpeyerl /*
485 1.3 hpeyerl * Verify that we have valid Ethernet multicast addresses.
486 1.3 hpeyerl */
487 1.3 hpeyerl if ((addrlo[0] & 0x01) != 1 || (addrhi[0] & 0x01) != 1) {
488 1.3 hpeyerl splx(s);
489 1.3 hpeyerl return (EINVAL);
490 1.3 hpeyerl }
491 1.3 hpeyerl /*
492 1.3 hpeyerl * See if the address range is already in the list.
493 1.3 hpeyerl */
494 1.3 hpeyerl ETHER_LOOKUP_MULTI(addrlo, addrhi, ac, enm);
495 1.3 hpeyerl if (enm != NULL) {
496 1.3 hpeyerl /*
497 1.3 hpeyerl * Found it; just increment the reference count.
498 1.3 hpeyerl */
499 1.3 hpeyerl ++enm->enm_refcount;
500 1.3 hpeyerl splx(s);
501 1.3 hpeyerl return (0);
502 1.3 hpeyerl }
503 1.3 hpeyerl /*
504 1.3 hpeyerl * New address or range; malloc a new multicast record
505 1.3 hpeyerl * and link it into the interface's multicast list.
506 1.3 hpeyerl */
507 1.3 hpeyerl enm = (struct ether_multi *)malloc(sizeof(*enm), M_IFMADDR, M_NOWAIT);
508 1.3 hpeyerl if (enm == NULL) {
509 1.3 hpeyerl splx(s);
510 1.3 hpeyerl return (ENOBUFS);
511 1.3 hpeyerl }
512 1.3 hpeyerl bcopy(addrlo, enm->enm_addrlo, 6);
513 1.3 hpeyerl bcopy(addrhi, enm->enm_addrhi, 6);
514 1.3 hpeyerl enm->enm_ac = ac;
515 1.3 hpeyerl enm->enm_refcount = 1;
516 1.3 hpeyerl enm->enm_next = ac->ac_multiaddrs;
517 1.3 hpeyerl ac->ac_multiaddrs = enm;
518 1.3 hpeyerl ac->ac_multicnt++;
519 1.3 hpeyerl splx(s);
520 1.3 hpeyerl /*
521 1.3 hpeyerl * Return ENETRESET to inform the driver that the list has changed
522 1.3 hpeyerl * and its reception filter should be adjusted accordingly.
523 1.3 hpeyerl */
524 1.3 hpeyerl return (ENETRESET);
525 1.3 hpeyerl }
526 1.3 hpeyerl
527 1.3 hpeyerl /*
528 1.3 hpeyerl * Delete a multicast address record.
529 1.3 hpeyerl */
530 1.3 hpeyerl int
531 1.3 hpeyerl ether_delmulti(ifr, ac)
532 1.3 hpeyerl struct ifreq *ifr;
533 1.3 hpeyerl register struct arpcom *ac;
534 1.3 hpeyerl {
535 1.3 hpeyerl register struct ether_multi *enm;
536 1.3 hpeyerl register struct ether_multi **p;
537 1.3 hpeyerl struct sockaddr_in *sin;
538 1.3 hpeyerl u_char addrlo[6];
539 1.3 hpeyerl u_char addrhi[6];
540 1.3 hpeyerl int s = splimp();
541 1.3 hpeyerl
542 1.3 hpeyerl switch (ifr->ifr_addr.sa_family) {
543 1.3 hpeyerl
544 1.3 hpeyerl case AF_UNSPEC:
545 1.3 hpeyerl bcopy(ifr->ifr_addr.sa_data, addrlo, 6);
546 1.3 hpeyerl bcopy(addrlo, addrhi, 6);
547 1.3 hpeyerl break;
548 1.3 hpeyerl
549 1.3 hpeyerl #ifdef INET
550 1.3 hpeyerl case AF_INET:
551 1.3 hpeyerl sin = (struct sockaddr_in *)&(ifr->ifr_addr);
552 1.3 hpeyerl if (sin->sin_addr.s_addr == INADDR_ANY) {
553 1.3 hpeyerl /*
554 1.3 hpeyerl * An IP address of INADDR_ANY means stop listening
555 1.3 hpeyerl * to the range of Ethernet multicast addresses used
556 1.3 hpeyerl * for IP.
557 1.3 hpeyerl */
558 1.3 hpeyerl bcopy(ether_ipmulticast_min, addrlo, 6);
559 1.3 hpeyerl bcopy(ether_ipmulticast_max, addrhi, 6);
560 1.3 hpeyerl }
561 1.3 hpeyerl else {
562 1.3 hpeyerl ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo);
563 1.3 hpeyerl bcopy(addrlo, addrhi, 6);
564 1.3 hpeyerl }
565 1.3 hpeyerl break;
566 1.3 hpeyerl #endif
567 1.3 hpeyerl
568 1.3 hpeyerl default:
569 1.3 hpeyerl splx(s);
570 1.3 hpeyerl return (EAFNOSUPPORT);
571 1.3 hpeyerl }
572 1.3 hpeyerl
573 1.3 hpeyerl /*
574 1.3 hpeyerl * Look up the address in our list.
575 1.3 hpeyerl */
576 1.3 hpeyerl ETHER_LOOKUP_MULTI(addrlo, addrhi, ac, enm);
577 1.3 hpeyerl if (enm == NULL) {
578 1.3 hpeyerl splx(s);
579 1.3 hpeyerl return (ENXIO);
580 1.3 hpeyerl }
581 1.3 hpeyerl if (--enm->enm_refcount != 0) {
582 1.3 hpeyerl /*
583 1.3 hpeyerl * Still some claims to this record.
584 1.3 hpeyerl */
585 1.3 hpeyerl splx(s);
586 1.3 hpeyerl return (0);
587 1.3 hpeyerl }
588 1.3 hpeyerl /*
589 1.3 hpeyerl * No remaining claims to this record; unlink and free it.
590 1.3 hpeyerl */
591 1.3 hpeyerl for (p = &enm->enm_ac->ac_multiaddrs;
592 1.3 hpeyerl *p != enm;
593 1.3 hpeyerl p = &(*p)->enm_next)
594 1.3 hpeyerl continue;
595 1.3 hpeyerl *p = (*p)->enm_next;
596 1.3 hpeyerl free(enm, M_IFMADDR);
597 1.3 hpeyerl ac->ac_multicnt--;
598 1.3 hpeyerl splx(s);
599 1.3 hpeyerl /*
600 1.3 hpeyerl * Return ENETRESET to inform the driver that the list has changed
601 1.3 hpeyerl * and its reception filter should be adjusted accordingly.
602 1.3 hpeyerl */
603 1.3 hpeyerl return (ENETRESET);
604 1.3 hpeyerl }
605