2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
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
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
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
23 * @APPLE_LICENSE_HEADER_END@
26 * Copyright (c) 1982, 1989, 1993
27 * The Regents of the University of California. All rights reserved.
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
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.
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
57 * @(#)if_ethersubr.c 8.1 (Berkeley) 6/10/93
58 * $FreeBSD: src/sys/net/if_ethersubr.c,v 1.70.2.17 2001/08/01 00:47:49 fenner Exp $
61 #include <sys/param.h>
62 #include <sys/systm.h>
63 #include <sys/kernel.h>
64 #include <sys/malloc.h>
66 #include <sys/socket.h>
67 #include <sys/sockio.h>
68 #include <sys/sysctl.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>
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>
86 #include <netipx/ipx.h>
87 #include <netipx/ipx_if.h>
90 #include <sys/socketvar.h>
93 extern struct ifqueue pkintrq
;
97 #include <net/bridge.h>
100 /* #include "vlan.h" */
102 #include <net/if_vlan_var.h>
103 #endif /* NVLAN > 0 */
105 static int ether_resolvemulti
__P((struct ifnet
*, struct sockaddr
**,
107 extern u_char etherbroadcastaddr
[];
108 #define senderr(e) do { error = (e); goto bad;} while (0)
109 #define IFP2AC(IFP) ((struct arpcom *)IFP)
112 * Perform common duties while attaching to interface list
117 IONetworkingFamily should call dlil_if_attach
118 ether_ifattach becomes obsolete, but remains for
119 temporary compatibility with third parties extensions
123 register struct ifnet
*ifp
;
125 boolean_t funnel_state
;
127 funnel_state
= thread_funnel_set(network_flock
, TRUE
);
130 ifp
->if_family
= APPLE_IF_FAM_ETHERNET
;
131 ifp
->if_type
= IFT_ETHER
;
134 ifp
->if_mtu
= ETHERMTU
;
135 ifp
->if_resolvemulti
= ether_resolvemulti
;
136 if (ifp
->if_baudrate
== 0)
137 ifp
->if_baudrate
= 10000000;
140 (void) thread_funnel_set(network_flock
, funnel_state
);
143 SYSCTL_DECL(_net_link
);
144 SYSCTL_NODE(_net_link
, IFT_ETHER
, ether
, CTLFLAG_RW
, 0, "Ethernet");
147 ether_resolvemulti(ifp
, llsa
, sa
)
149 struct sockaddr
**llsa
;
152 struct sockaddr_dl
*sdl
;
153 struct sockaddr_in
*sin
;
156 struct sockaddr_in6
*sin6
;
160 switch(sa
->sa_family
) {
162 /* AppleTalk uses AF_UNSPEC for multicast registration.
163 * No mapping needed. Just check that it's a valid MC address.
165 e_addr
= &sa
->sa_data
[0];
166 if ((e_addr
[0] & 1) != 1)
167 return EADDRNOTAVAIL
;
173 * No mapping needed. Just check that it's a valid MC address.
175 sdl
= (struct sockaddr_dl
*)sa
;
176 e_addr
= LLADDR(sdl
);
177 if ((e_addr
[0] & 1) != 1)
178 return EADDRNOTAVAIL
;
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
,
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
;
194 sdl
->sdl_alen
= ETHER_ADDR_LEN
;
196 e_addr
= LLADDR(sdl
);
197 ETHER_MAP_IP_MULTICAST(&sin
->sin_addr
, e_addr
);
198 *llsa
= (struct sockaddr
*)sdl
;
203 sin6
= (struct sockaddr_in6
*)sa
;
204 if (IN6_IS_ADDR_UNSPECIFIED(&sin6
->sin6_addr
)) {
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.)
210 ifp
->if_flags
|= IFF_ALLMULTI
;
214 MALLOC(sdl
, struct sockaddr_dl
*, sizeof *sdl
, M_IFMADDR
,
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
;
221 sdl
->sdl_alen
= ETHER_ADDR_LEN
;
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
;
233 * Well, the text isn't quite right, but it's the name
244 u_char ether_ipmulticast_min
[6] = { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 };
245 u_char ether_ipmulticast_max
[6] = { 0x01, 0x00, 0x5e, 0x7f, 0xff, 0xff };
247 * Add an Ethernet multicast address or range of addresses to the list for a
251 ether_addmulti(ifr
, ac
)
253 register struct arpcom
*ac
;
255 register struct ether_multi
*enm
;
256 struct sockaddr_in
*sin
;
261 switch (ifr
->ifr_addr
.sa_family
) {
264 bcopy(ifr
->ifr_addr
.sa_data
, addrlo
, 6);
265 bcopy(addrlo
, addrhi
, 6);
270 sin
= (struct sockaddr_in
*)&(ifr
->ifr_addr
);
271 if (sin
->sin_addr
.s_addr
== INADDR_ANY
) {
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.)
277 bcopy(ether_ipmulticast_min
, addrlo
, 6);
278 bcopy(ether_ipmulticast_max
, addrhi
, 6);
281 ETHER_MAP_IP_MULTICAST(&sin
->sin_addr
, addrlo
);
282 bcopy(addrlo
, addrhi
, 6);
289 return (EAFNOSUPPORT
);
293 * Verify that we have valid Ethernet multicast addresses.
295 if ((addrlo
[0] & 0x01) != 1 || (addrhi
[0] & 0x01) != 1) {
300 * See if the address range is already in the list.
302 ETHER_LOOKUP_MULTI(addrlo
, addrhi
, ac
, enm
);
305 * Found it; just increment the reference count.
312 * New address or range; malloc a new multicast record
313 * and link it into the interface's multicast list.
315 enm
= (struct ether_multi
*)_MALLOC(sizeof(*enm
), M_IFMADDR
, M_WAITOK
);
320 bcopy(addrlo
, enm
->enm_addrlo
, 6);
321 bcopy(addrhi
, enm
->enm_addrhi
, 6);
323 enm
->enm_refcount
= 1;
324 enm
->enm_next
= ac
->ac_multiaddrs
;
325 ac
->ac_multiaddrs
= enm
;
328 * Return ENETRESET to inform the driver that the list has changed
329 * and its reception filter should be adjusted accordingly.
335 * Delete a multicast address record.
338 ether_delmulti(ifr
, ac
, ret_mca
)
340 register struct arpcom
*ac
;
341 struct ether_addr
* ret_mca
;
343 register struct ether_multi
*enm
;
344 register struct ether_multi
**p
;
345 struct sockaddr_in
*sin
;
350 switch (ifr
->ifr_addr
.sa_family
) {
353 bcopy(ifr
->ifr_addr
.sa_data
, addrlo
, 6);
354 bcopy(addrlo
, addrhi
, 6);
359 sin
= (struct sockaddr_in
*)&(ifr
->ifr_addr
);
360 if (sin
->sin_addr
.s_addr
== INADDR_ANY
) {
362 * An IP address of INADDR_ANY means stop listening
363 * to the range of Ethernet multicast addresses used
366 bcopy(ether_ipmulticast_min
, addrlo
, 6);
367 bcopy(ether_ipmulticast_max
, addrhi
, 6);
370 ETHER_MAP_IP_MULTICAST(&sin
->sin_addr
, addrlo
);
371 bcopy(addrlo
, addrhi
, 6);
378 return (EAFNOSUPPORT
);
382 * Look up the address in our list.
384 ETHER_LOOKUP_MULTI(addrlo
, addrhi
, ac
, enm
);
389 if (--enm
->enm_refcount
!= 0) {
391 * Still some claims to this record.
397 /* save the low and high address of the range before deletion */
399 *ret_mca
= *((struct ether_addr
*)addrlo
);
400 *(ret_mca
+ 1) = *((struct ether_addr
*)addrhi
);
404 * No remaining claims to this record; unlink and free it.
406 for (p
= &enm
->enm_ac
->ac_multiaddrs
;
411 FREE(enm
, M_IFMADDR
);
414 * Return ENETRESET to inform the driver that the list has changed
415 * and its reception filter should be adjusted accordingly.
421 * Convert Ethernet address to printable (loggable) representation.
423 static u_char digits
[] = "0123456789abcdef";
431 for (cp
= p
, i
= 0; i
< 6; i
++) {
432 *cp
++ = digits
[*ap
>> 4];
433 *cp
++ = digits
[*ap
++ & 0xf];