2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1982, 1989, 1993
24 * The Regents of the University of California. All rights reserved.
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions
29 * 1. Redistributions of source code must retain the above copyright
30 * notice, this list of conditions and the following disclaimer.
31 * 2. Redistributions in binary form must reproduce the above copyright
32 * notice, this list of conditions and the following disclaimer in the
33 * documentation and/or other materials provided with the distribution.
34 * 3. All advertising materials mentioning features or use of this software
35 * must display the following acknowledgement:
36 * This product includes software developed by the University of
37 * California, Berkeley and its contributors.
38 * 4. Neither the name of the University nor the names of its contributors
39 * may be used to endorse or promote products derived from this software
40 * without specific prior written permission.
42 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
43 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
46 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 * @(#)if_ethersubr.c 8.1 (Berkeley) 6/10/93
55 * $FreeBSD: src/sys/net/if_ethersubr.c,v 1.70.2.17 2001/08/01 00:47:49 fenner Exp $
58 #include <sys/param.h>
59 #include <sys/systm.h>
60 #include <sys/kernel.h>
61 #include <sys/malloc.h>
63 #include <sys/socket.h>
64 #include <sys/sockio.h>
65 #include <sys/sysctl.h>
68 #include <net/netisr.h>
69 #include <net/route.h>
70 #include <net/if_llc.h>
71 #include <net/if_dl.h>
72 #include <net/if_types.h>
75 #include <netinet/in.h>
76 #include <netinet/in_var.h>
77 #include <netinet/if_ether.h>
78 #include <netinet/in_systm.h>
79 #include <netinet/ip.h>
83 #include <netipx/ipx.h>
84 #include <netipx/ipx_if.h>
87 #include <sys/socketvar.h>
90 extern struct ifqueue pkintrq
;
94 #include <net/bridge.h>
97 /* #include "vlan.h" */
99 #include <net/if_vlan_var.h>
100 #endif /* NVLAN > 0 */
102 static int ether_resolvemulti
__P((struct ifnet
*, struct sockaddr
**,
104 extern u_char etherbroadcastaddr
[];
105 #define senderr(e) do { error = (e); goto bad;} while (0)
106 #define IFP2AC(IFP) ((struct arpcom *)IFP)
109 * Perform common duties while attaching to interface list
114 IONetworkingFamily should call dlil_if_attach
115 ether_ifattach becomes obsolete, but remains for
116 temporary compatibility with third parties extensions
120 register struct ifnet
*ifp
;
122 boolean_t funnel_state
;
124 funnel_state
= thread_funnel_set(network_flock
, TRUE
);
127 ifp
->if_family
= APPLE_IF_FAM_ETHERNET
;
128 ifp
->if_type
= IFT_ETHER
;
131 ifp
->if_mtu
= ETHERMTU
;
132 ifp
->if_resolvemulti
= ether_resolvemulti
;
133 if (ifp
->if_baudrate
== 0)
134 ifp
->if_baudrate
= 10000000;
137 (void) thread_funnel_set(network_flock
, funnel_state
);
140 SYSCTL_DECL(_net_link
);
141 SYSCTL_NODE(_net_link
, IFT_ETHER
, ether
, CTLFLAG_RW
, 0, "Ethernet");
144 ether_resolvemulti(ifp
, llsa
, sa
)
146 struct sockaddr
**llsa
;
149 struct sockaddr_dl
*sdl
;
150 struct sockaddr_in
*sin
;
153 struct sockaddr_in6
*sin6
;
157 switch(sa
->sa_family
) {
159 /* AppleTalk uses AF_UNSPEC for multicast registration.
160 * No mapping needed. Just check that it's a valid MC address.
162 e_addr
= &sa
->sa_data
[0];
163 if ((e_addr
[0] & 1) != 1)
164 return EADDRNOTAVAIL
;
170 * No mapping needed. Just check that it's a valid MC address.
172 sdl
= (struct sockaddr_dl
*)sa
;
173 e_addr
= LLADDR(sdl
);
174 if ((e_addr
[0] & 1) != 1)
175 return EADDRNOTAVAIL
;
181 sin
= (struct sockaddr_in
*)sa
;
182 if (!IN_MULTICAST(ntohl(sin
->sin_addr
.s_addr
)))
183 return EADDRNOTAVAIL
;
184 MALLOC(sdl
, struct sockaddr_dl
*, sizeof *sdl
, M_IFMADDR
,
186 sdl
->sdl_len
= sizeof *sdl
;
187 sdl
->sdl_family
= AF_LINK
;
188 sdl
->sdl_index
= ifp
->if_index
;
189 sdl
->sdl_type
= IFT_ETHER
;
191 sdl
->sdl_alen
= ETHER_ADDR_LEN
;
193 e_addr
= LLADDR(sdl
);
194 ETHER_MAP_IP_MULTICAST(&sin
->sin_addr
, e_addr
);
195 *llsa
= (struct sockaddr
*)sdl
;
200 sin6
= (struct sockaddr_in6
*)sa
;
201 if (IN6_IS_ADDR_UNSPECIFIED(&sin6
->sin6_addr
)) {
203 * An IP6 address of 0 means listen to all
204 * of the Ethernet multicast address used for IP6.
205 * (This is used for multicast routers.)
207 ifp
->if_flags
|= IFF_ALLMULTI
;
211 MALLOC(sdl
, struct sockaddr_dl
*, sizeof *sdl
, M_IFMADDR
,
213 sdl
->sdl_len
= sizeof *sdl
;
214 sdl
->sdl_family
= AF_LINK
;
215 sdl
->sdl_index
= ifp
->if_index
;
216 sdl
->sdl_type
= IFT_ETHER
;
218 sdl
->sdl_alen
= ETHER_ADDR_LEN
;
220 e_addr
= LLADDR(sdl
);
221 ETHER_MAP_IPV6_MULTICAST(&sin6
->sin6_addr
, e_addr
);
222 kprintf("ether_resolvemulti Adding %x:%x:%x:%x:%x:%x\n",
223 e_addr
[0], e_addr
[1], e_addr
[2], e_addr
[3], e_addr
[4], e_addr
[5]);
224 *llsa
= (struct sockaddr
*)sdl
;
230 * Well, the text isn't quite right, but it's the name
241 u_char ether_ipmulticast_min
[6] = { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 };
242 u_char ether_ipmulticast_max
[6] = { 0x01, 0x00, 0x5e, 0x7f, 0xff, 0xff };
244 * Add an Ethernet multicast address or range of addresses to the list for a
248 ether_addmulti(ifr
, ac
)
250 register struct arpcom
*ac
;
252 register struct ether_multi
*enm
;
253 struct sockaddr_in
*sin
;
258 switch (ifr
->ifr_addr
.sa_family
) {
261 bcopy(ifr
->ifr_addr
.sa_data
, addrlo
, 6);
262 bcopy(addrlo
, addrhi
, 6);
267 sin
= (struct sockaddr_in
*)&(ifr
->ifr_addr
);
268 if (sin
->sin_addr
.s_addr
== INADDR_ANY
) {
270 * An IP address of INADDR_ANY means listen to all
271 * of the Ethernet multicast addresses used for IP.
272 * (This is for the sake of IP multicast routers.)
274 bcopy(ether_ipmulticast_min
, addrlo
, 6);
275 bcopy(ether_ipmulticast_max
, addrhi
, 6);
278 ETHER_MAP_IP_MULTICAST(&sin
->sin_addr
, addrlo
);
279 bcopy(addrlo
, addrhi
, 6);
286 return (EAFNOSUPPORT
);
290 * Verify that we have valid Ethernet multicast addresses.
292 if ((addrlo
[0] & 0x01) != 1 || (addrhi
[0] & 0x01) != 1) {
297 * See if the address range is already in the list.
299 ETHER_LOOKUP_MULTI(addrlo
, addrhi
, ac
, enm
);
302 * Found it; just increment the reference count.
309 * New address or range; malloc a new multicast record
310 * and link it into the interface's multicast list.
312 enm
= (struct ether_multi
*)_MALLOC(sizeof(*enm
), M_IFMADDR
, M_WAITOK
);
317 bcopy(addrlo
, enm
->enm_addrlo
, 6);
318 bcopy(addrhi
, enm
->enm_addrhi
, 6);
320 enm
->enm_refcount
= 1;
321 enm
->enm_next
= ac
->ac_multiaddrs
;
322 ac
->ac_multiaddrs
= enm
;
325 * Return ENETRESET to inform the driver that the list has changed
326 * and its reception filter should be adjusted accordingly.
332 * Delete a multicast address record.
335 ether_delmulti(ifr
, ac
, ret_mca
)
337 register struct arpcom
*ac
;
338 struct ether_addr
* ret_mca
;
340 register struct ether_multi
*enm
;
341 register struct ether_multi
**p
;
342 struct sockaddr_in
*sin
;
347 switch (ifr
->ifr_addr
.sa_family
) {
350 bcopy(ifr
->ifr_addr
.sa_data
, addrlo
, 6);
351 bcopy(addrlo
, addrhi
, 6);
356 sin
= (struct sockaddr_in
*)&(ifr
->ifr_addr
);
357 if (sin
->sin_addr
.s_addr
== INADDR_ANY
) {
359 * An IP address of INADDR_ANY means stop listening
360 * to the range of Ethernet multicast addresses used
363 bcopy(ether_ipmulticast_min
, addrlo
, 6);
364 bcopy(ether_ipmulticast_max
, addrhi
, 6);
367 ETHER_MAP_IP_MULTICAST(&sin
->sin_addr
, addrlo
);
368 bcopy(addrlo
, addrhi
, 6);
375 return (EAFNOSUPPORT
);
379 * Look up the address in our list.
381 ETHER_LOOKUP_MULTI(addrlo
, addrhi
, ac
, enm
);
386 if (--enm
->enm_refcount
!= 0) {
388 * Still some claims to this record.
394 /* save the low and high address of the range before deletion */
396 *ret_mca
= *((struct ether_addr
*)addrlo
);
397 *(ret_mca
+ 1) = *((struct ether_addr
*)addrhi
);
401 * No remaining claims to this record; unlink and free it.
403 for (p
= &enm
->enm_ac
->ac_multiaddrs
;
408 FREE(enm
, M_IFMADDR
);
411 * Return ENETRESET to inform the driver that the list has changed
412 * and its reception filter should be adjusted accordingly.
418 * Convert Ethernet address to printable (loggable) representation.
420 static u_char digits
[] = "0123456789abcdef";
428 for (cp
= p
, i
= 0; i
< 6; i
++) {
429 *cp
++ = digits
[*ap
>> 4];
430 *cp
++ = digits
[*ap
++ & 0xf];