]> git.saurik.com Git - apple/xnu.git/blame - bsd/net/if_ethersubr.c
xnu-344.21.74.tar.gz
[apple/xnu.git] / bsd / net / if_ethersubr.c
CommitLineData
1c79356b
A
1/*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
d7e50217 6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
1c79356b 7 *
d7e50217
A
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
1c79356b
A
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
d7e50217
A
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
1c79356b
A
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25/*
26 * Copyright (c) 1982, 1989, 1993
27 * The Regents of the University of California. All rights reserved.
28 *
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
31 * are met:
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
37 * 3. All advertising materials mentioning features or use of this software
38 * must display the following acknowledgement:
39 * This product includes software developed by the University of
40 * California, Berkeley and its contributors.
41 * 4. Neither the name of the University nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
44 *
45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 * SUCH DAMAGE.
56 *
57 * @(#)if_ethersubr.c 8.1 (Berkeley) 6/10/93
9bccf70c 58 * $FreeBSD: src/sys/net/if_ethersubr.c,v 1.70.2.17 2001/08/01 00:47:49 fenner Exp $
1c79356b
A
59 */
60
1c79356b
A
61#include <sys/param.h>
62#include <sys/systm.h>
63#include <sys/kernel.h>
64#include <sys/malloc.h>
65#include <sys/mbuf.h>
66#include <sys/socket.h>
67#include <sys/sockio.h>
68#include <sys/sysctl.h>
69
70#include <net/if.h>
71#include <net/netisr.h>
72#include <net/route.h>
73#include <net/if_llc.h>
74#include <net/if_dl.h>
75#include <net/if_types.h>
1c79356b 76
9bccf70c 77#if INET || INET6
1c79356b
A
78#include <netinet/in.h>
79#include <netinet/in_var.h>
80#include <netinet/if_ether.h>
81#include <netinet/in_systm.h>
82#include <netinet/ip.h>
83#endif
84
85#if IPX
86#include <netipx/ipx.h>
87#include <netipx/ipx_if.h>
88#endif
89
1c79356b
A
90#include <sys/socketvar.h>
91
92#if LLC && CCITT
93extern struct ifqueue pkintrq;
94#endif
95
96#if BRIDGE
97#include <net/bridge.h>
98#endif
99
100/* #include "vlan.h" */
101#if NVLAN > 0
102#include <net/if_vlan_var.h>
103#endif /* NVLAN > 0 */
104
105static int ether_resolvemulti __P((struct ifnet *, struct sockaddr **,
106 struct sockaddr *));
107extern u_char etherbroadcastaddr[];
108#define senderr(e) do { error = (e); goto bad;} while (0)
109#define IFP2AC(IFP) ((struct arpcom *)IFP)
110
111/*
112 * Perform common duties while attaching to interface list
113 */
114
115
9bccf70c
A
116/*
117 IONetworkingFamily should call dlil_if_attach
118 ether_ifattach becomes obsolete, but remains for
119 temporary compatibility with third parties extensions
120*/
1c79356b
A
121void
122ether_ifattach(ifp)
123 register struct ifnet *ifp;
124{
1c79356b
A
125 boolean_t funnel_state;
126
127 funnel_state = thread_funnel_set(network_flock, TRUE);
1c79356b
A
128
129 ifp->if_name = "en";
130 ifp->if_family = APPLE_IF_FAM_ETHERNET;
131 ifp->if_type = IFT_ETHER;
132 ifp->if_addrlen = 6;
133 ifp->if_hdrlen = 14;
134 ifp->if_mtu = ETHERMTU;
135 ifp->if_resolvemulti = ether_resolvemulti;
136 if (ifp->if_baudrate == 0)
137 ifp->if_baudrate = 10000000;
138
139 dlil_if_attach(ifp);
1c79356b
A
140 (void) thread_funnel_set(network_flock, funnel_state);
141}
142
143SYSCTL_DECL(_net_link);
144SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
145
146int
147ether_resolvemulti(ifp, llsa, sa)
148 struct ifnet *ifp;
149 struct sockaddr **llsa;
150 struct sockaddr *sa;
151{
152 struct sockaddr_dl *sdl;
153 struct sockaddr_in *sin;
154 u_char *e_addr;
155#if INET6
156 struct sockaddr_in6 *sin6;
157#endif
158
159
160 switch(sa->sa_family) {
161 case AF_UNSPEC:
162 /* AppleTalk uses AF_UNSPEC for multicast registration.
163 * No mapping needed. Just check that it's a valid MC address.
164 */
165 e_addr = &sa->sa_data[0];
166 if ((e_addr[0] & 1) != 1)
167 return EADDRNOTAVAIL;
168 *llsa = 0;
169 return 0;
170
171 case AF_LINK:
172 /*
173 * No mapping needed. Just check that it's a valid MC address.
174 */
175 sdl = (struct sockaddr_dl *)sa;
176 e_addr = LLADDR(sdl);
177 if ((e_addr[0] & 1) != 1)
178 return EADDRNOTAVAIL;
179 *llsa = 0;
180 return 0;
181
182#if INET
183 case AF_INET:
184 sin = (struct sockaddr_in *)sa;
185 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
186 return EADDRNOTAVAIL;
187 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
188 M_WAITOK);
189 sdl->sdl_len = sizeof *sdl;
190 sdl->sdl_family = AF_LINK;
191 sdl->sdl_index = ifp->if_index;
192 sdl->sdl_type = IFT_ETHER;
193 sdl->sdl_nlen = 0;
194 sdl->sdl_alen = ETHER_ADDR_LEN;
195 sdl->sdl_slen = 0;
196 e_addr = LLADDR(sdl);
197 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
198 *llsa = (struct sockaddr *)sdl;
199 return 0;
200#endif
201#if INET6
202 case AF_INET6:
203 sin6 = (struct sockaddr_in6 *)sa;
204 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
205 /*
206 * An IP6 address of 0 means listen to all
207 * of the Ethernet multicast address used for IP6.
208 * (This is used for multicast routers.)
209 */
210 ifp->if_flags |= IFF_ALLMULTI;
211 *llsa = 0;
212 return 0;
213 }
214 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
215 M_WAITOK);
216 sdl->sdl_len = sizeof *sdl;
217 sdl->sdl_family = AF_LINK;
218 sdl->sdl_index = ifp->if_index;
219 sdl->sdl_type = IFT_ETHER;
220 sdl->sdl_nlen = 0;
221 sdl->sdl_alen = ETHER_ADDR_LEN;
222 sdl->sdl_slen = 0;
223 e_addr = LLADDR(sdl);
224 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
225 kprintf("ether_resolvemulti Adding %x:%x:%x:%x:%x:%x\n",
226 e_addr[0], e_addr[1], e_addr[2], e_addr[3], e_addr[4], e_addr[5]);
227 *llsa = (struct sockaddr *)sdl;
228 return 0;
229#endif
230
231 default:
232 /*
233 * Well, the text isn't quite right, but it's the name
234 * that counts...
235 */
236 return EAFNOSUPPORT;
237 }
238}
239
240
241
242
243
244u_char ether_ipmulticast_min[6] = { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 };
245u_char ether_ipmulticast_max[6] = { 0x01, 0x00, 0x5e, 0x7f, 0xff, 0xff };
246/*
247 * Add an Ethernet multicast address or range of addresses to the list for a
248 * given interface.
249 */
250int
251ether_addmulti(ifr, ac)
252 struct ifreq *ifr;
253 register struct arpcom *ac;
254{
255 register struct ether_multi *enm;
256 struct sockaddr_in *sin;
257 u_char addrlo[6];
258 u_char addrhi[6];
259 int s = splimp();
260
261 switch (ifr->ifr_addr.sa_family) {
262
263 case AF_UNSPEC:
264 bcopy(ifr->ifr_addr.sa_data, addrlo, 6);
265 bcopy(addrlo, addrhi, 6);
266 break;
267
268#if INET
269 case AF_INET:
270 sin = (struct sockaddr_in *)&(ifr->ifr_addr);
271 if (sin->sin_addr.s_addr == INADDR_ANY) {
272 /*
273 * An IP address of INADDR_ANY means listen to all
274 * of the Ethernet multicast addresses used for IP.
275 * (This is for the sake of IP multicast routers.)
276 */
277 bcopy(ether_ipmulticast_min, addrlo, 6);
278 bcopy(ether_ipmulticast_max, addrhi, 6);
279 }
280 else {
281 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo);
282 bcopy(addrlo, addrhi, 6);
283 }
284 break;
285#endif
286
287 default:
288 splx(s);
289 return (EAFNOSUPPORT);
290 }
291
292 /*
293 * Verify that we have valid Ethernet multicast addresses.
294 */
295 if ((addrlo[0] & 0x01) != 1 || (addrhi[0] & 0x01) != 1) {
296 splx(s);
297 return (EINVAL);
298 }
299 /*
300 * See if the address range is already in the list.
301 */
302 ETHER_LOOKUP_MULTI(addrlo, addrhi, ac, enm);
303 if (enm != NULL) {
304 /*
305 * Found it; just increment the reference count.
306 */
307 ++enm->enm_refcount;
308 splx(s);
309 return (0);
310 }
311 /*
312 * New address or range; malloc a new multicast record
313 * and link it into the interface's multicast list.
314 */
315 enm = (struct ether_multi *)_MALLOC(sizeof(*enm), M_IFMADDR, M_WAITOK);
316 if (enm == NULL) {
317 splx(s);
318 return (ENOBUFS);
319 }
320 bcopy(addrlo, enm->enm_addrlo, 6);
321 bcopy(addrhi, enm->enm_addrhi, 6);
322 enm->enm_ac = ac;
323 enm->enm_refcount = 1;
324 enm->enm_next = ac->ac_multiaddrs;
325 ac->ac_multiaddrs = enm;
326 splx(s);
327 /*
328 * Return ENETRESET to inform the driver that the list has changed
329 * and its reception filter should be adjusted accordingly.
330 */
331 return (ENETRESET);
332}
333
334/*
335 * Delete a multicast address record.
336 */
337int
338ether_delmulti(ifr, ac, ret_mca)
339 struct ifreq *ifr;
340 register struct arpcom *ac;
341 struct ether_addr * ret_mca;
342{
343 register struct ether_multi *enm;
344 register struct ether_multi **p;
345 struct sockaddr_in *sin;
346 u_char addrlo[6];
347 u_char addrhi[6];
348 int s = splimp();
349
350 switch (ifr->ifr_addr.sa_family) {
351
352 case AF_UNSPEC:
353 bcopy(ifr->ifr_addr.sa_data, addrlo, 6);
354 bcopy(addrlo, addrhi, 6);
355 break;
356
357#if INET
358 case AF_INET:
359 sin = (struct sockaddr_in *)&(ifr->ifr_addr);
360 if (sin->sin_addr.s_addr == INADDR_ANY) {
361 /*
362 * An IP address of INADDR_ANY means stop listening
363 * to the range of Ethernet multicast addresses used
364 * for IP.
365 */
366 bcopy(ether_ipmulticast_min, addrlo, 6);
367 bcopy(ether_ipmulticast_max, addrhi, 6);
368 }
369 else {
370 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo);
371 bcopy(addrlo, addrhi, 6);
372 }
373 break;
374#endif
375
376 default:
377 splx(s);
378 return (EAFNOSUPPORT);
379 }
380
381 /*
382 * Look up the address in our list.
383 */
384 ETHER_LOOKUP_MULTI(addrlo, addrhi, ac, enm);
385 if (enm == NULL) {
386 splx(s);
387 return (ENXIO);
388 }
389 if (--enm->enm_refcount != 0) {
390 /*
391 * Still some claims to this record.
392 */
393 splx(s);
394 return (0);
395 }
396
397 /* save the low and high address of the range before deletion */
398 if (ret_mca) {
399 *ret_mca = *((struct ether_addr *)addrlo);
400 *(ret_mca + 1) = *((struct ether_addr *)addrhi);
401 }
402
403 /*
404 * No remaining claims to this record; unlink and free it.
405 */
406 for (p = &enm->enm_ac->ac_multiaddrs;
407 *p != enm;
408 p = &(*p)->enm_next)
409 continue;
410 *p = (*p)->enm_next;
411 FREE(enm, M_IFMADDR);
412 splx(s);
413 /*
414 * Return ENETRESET to inform the driver that the list has changed
415 * and its reception filter should be adjusted accordingly.
416 */
417 return (ENETRESET);
418}
419
420/*
421 * Convert Ethernet address to printable (loggable) representation.
422 */
423static u_char digits[] = "0123456789abcdef";
424char *
425ether_sprintf(p, ap)
426 register u_char *p;
427 register u_char *ap;
428{ register char *cp;
429 register i;
430
431 for (cp = p, i = 0; i < 6; i++) {
432 *cp++ = digits[*ap >> 4];
433 *cp++ = digits[*ap++ & 0xf];
434 *cp++ = ':';
435 }
436 *--cp = 0;
437 return (p);
438}