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
58 #include "opt_atalk.h"
64 #include <sys/param.h>
65 #include <sys/systm.h>
66 #include <sys/kernel.h>
67 #include <sys/malloc.h>
69 #include <sys/socket.h>
70 #include <sys/sockio.h>
71 #include <sys/sysctl.h>
74 #include <net/netisr.h>
75 #include <net/route.h>
76 #include <net/if_llc.h>
77 #include <net/if_dl.h>
78 #include <net/if_types.h>
82 #include <netinet/in.h>
83 #include <netinet/in_var.h>
84 #include <netinet/if_ether.h>
85 #include <netinet/in_systm.h>
86 #include <netinet/ip.h>
90 #include <netipx/ipx.h>
91 #include <netipx/ipx_if.h>
96 #include <netns/ns_if.h>
98 int ether_outputdebug
= 0;
99 int ether_inputdebug
= 0;
103 #include <netiso/argo_debug.h>
104 #include <netiso/iso.h>
105 #include <netiso/iso_var.h>
106 #include <netiso/iso_snpac.h>
110 #include <netccitt/dll.h>
111 #include <netccitt/llc_var.h>
114 #include <sys/socketvar.h>
117 extern struct ifqueue pkintrq
;
121 #include <net/bridge.h>
124 /* #include "vlan.h" */
126 #include <net/if_vlan_var.h>
127 #endif /* NVLAN > 0 */
129 static int ether_resolvemulti
__P((struct ifnet
*, struct sockaddr
**,
131 extern u_char etherbroadcastaddr
[];
132 #define senderr(e) do { error = (e); goto bad;} while (0)
133 #define IFP2AC(IFP) ((struct arpcom *)IFP)
136 * Perform common duties while attaching to interface list
142 register struct ifnet
*ifp
;
144 register struct ifaddr
*ifa
;
145 register struct sockaddr_dl
*sdl
;
146 boolean_t funnel_state
;
148 funnel_state
= thread_funnel_set(network_flock
, TRUE
);
149 printf("ether_ifattach called for %s\n", ifp
->if_name
);
153 ifp
->if_family
= APPLE_IF_FAM_ETHERNET
;
154 ifp
->if_type
= IFT_ETHER
;
157 ifp
->if_mtu
= ETHERMTU
;
158 ifp
->if_resolvemulti
= ether_resolvemulti
;
159 if (ifp
->if_baudrate
== 0)
160 ifp
->if_baudrate
= 10000000;
163 ifa
= ifnet_addrs
[ifp
->if_index
- 1];
165 printf("ether_ifattach: no lladdr!\n");
166 (void) thread_funnel_set(network_flock
, funnel_state
);
169 sdl
= (struct sockaddr_dl
*)ifa
->ifa_addr
;
170 sdl
->sdl_type
= IFT_ETHER
;
171 sdl
->sdl_alen
= ifp
->if_addrlen
;
172 bcopy((IFP2AC(ifp
))->ac_enaddr
, LLADDR(sdl
), ifp
->if_addrlen
);
174 in6_ifattach_getifid(ifp
);
176 (void) thread_funnel_set(network_flock
, funnel_state
);
179 SYSCTL_DECL(_net_link
);
180 SYSCTL_NODE(_net_link
, IFT_ETHER
, ether
, CTLFLAG_RW
, 0, "Ethernet");
183 ether_resolvemulti(ifp
, llsa
, sa
)
185 struct sockaddr
**llsa
;
188 struct sockaddr_dl
*sdl
;
189 struct sockaddr_in
*sin
;
192 struct sockaddr_in6
*sin6
;
196 switch(sa
->sa_family
) {
198 /* AppleTalk uses AF_UNSPEC for multicast registration.
199 * No mapping needed. Just check that it's a valid MC address.
201 e_addr
= &sa
->sa_data
[0];
202 if ((e_addr
[0] & 1) != 1)
203 return EADDRNOTAVAIL
;
209 * No mapping needed. Just check that it's a valid MC address.
211 sdl
= (struct sockaddr_dl
*)sa
;
212 e_addr
= LLADDR(sdl
);
213 if ((e_addr
[0] & 1) != 1)
214 return EADDRNOTAVAIL
;
220 sin
= (struct sockaddr_in
*)sa
;
221 if (!IN_MULTICAST(ntohl(sin
->sin_addr
.s_addr
)))
222 return EADDRNOTAVAIL
;
223 MALLOC(sdl
, struct sockaddr_dl
*, sizeof *sdl
, M_IFMADDR
,
225 sdl
->sdl_len
= sizeof *sdl
;
226 sdl
->sdl_family
= AF_LINK
;
227 sdl
->sdl_index
= ifp
->if_index
;
228 sdl
->sdl_type
= IFT_ETHER
;
230 sdl
->sdl_alen
= ETHER_ADDR_LEN
;
232 e_addr
= LLADDR(sdl
);
233 ETHER_MAP_IP_MULTICAST(&sin
->sin_addr
, e_addr
);
234 *llsa
= (struct sockaddr
*)sdl
;
239 sin6
= (struct sockaddr_in6
*)sa
;
240 if (IN6_IS_ADDR_UNSPECIFIED(&sin6
->sin6_addr
)) {
242 * An IP6 address of 0 means listen to all
243 * of the Ethernet multicast address used for IP6.
244 * (This is used for multicast routers.)
246 ifp
->if_flags
|= IFF_ALLMULTI
;
250 MALLOC(sdl
, struct sockaddr_dl
*, sizeof *sdl
, M_IFMADDR
,
252 sdl
->sdl_len
= sizeof *sdl
;
253 sdl
->sdl_family
= AF_LINK
;
254 sdl
->sdl_index
= ifp
->if_index
;
255 sdl
->sdl_type
= IFT_ETHER
;
257 sdl
->sdl_alen
= ETHER_ADDR_LEN
;
259 e_addr
= LLADDR(sdl
);
260 ETHER_MAP_IPV6_MULTICAST(&sin6
->sin6_addr
, e_addr
);
261 kprintf("ether_resolvemulti Adding %x:%x:%x:%x:%x:%x\n",
262 e_addr
[0], e_addr
[1], e_addr
[2], e_addr
[3], e_addr
[4], e_addr
[5]);
263 *llsa
= (struct sockaddr
*)sdl
;
269 * Well, the text isn't quite right, but it's the name
280 u_char ether_ipmulticast_min
[6] = { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 };
281 u_char ether_ipmulticast_max
[6] = { 0x01, 0x00, 0x5e, 0x7f, 0xff, 0xff };
283 * Add an Ethernet multicast address or range of addresses to the list for a
287 ether_addmulti(ifr
, ac
)
289 register struct arpcom
*ac
;
291 register struct ether_multi
*enm
;
292 struct sockaddr_in
*sin
;
297 switch (ifr
->ifr_addr
.sa_family
) {
300 bcopy(ifr
->ifr_addr
.sa_data
, addrlo
, 6);
301 bcopy(addrlo
, addrhi
, 6);
306 sin
= (struct sockaddr_in
*)&(ifr
->ifr_addr
);
307 if (sin
->sin_addr
.s_addr
== INADDR_ANY
) {
309 * An IP address of INADDR_ANY means listen to all
310 * of the Ethernet multicast addresses used for IP.
311 * (This is for the sake of IP multicast routers.)
313 bcopy(ether_ipmulticast_min
, addrlo
, 6);
314 bcopy(ether_ipmulticast_max
, addrhi
, 6);
317 ETHER_MAP_IP_MULTICAST(&sin
->sin_addr
, addrlo
);
318 bcopy(addrlo
, addrhi
, 6);
325 return (EAFNOSUPPORT
);
329 * Verify that we have valid Ethernet multicast addresses.
331 if ((addrlo
[0] & 0x01) != 1 || (addrhi
[0] & 0x01) != 1) {
336 * See if the address range is already in the list.
338 ETHER_LOOKUP_MULTI(addrlo
, addrhi
, ac
, enm
);
341 * Found it; just increment the reference count.
348 * New address or range; malloc a new multicast record
349 * and link it into the interface's multicast list.
351 enm
= (struct ether_multi
*)_MALLOC(sizeof(*enm
), M_IFMADDR
, M_WAITOK
);
356 bcopy(addrlo
, enm
->enm_addrlo
, 6);
357 bcopy(addrhi
, enm
->enm_addrhi
, 6);
359 enm
->enm_refcount
= 1;
360 enm
->enm_next
= ac
->ac_multiaddrs
;
361 ac
->ac_multiaddrs
= enm
;
364 * Return ENETRESET to inform the driver that the list has changed
365 * and its reception filter should be adjusted accordingly.
371 * Delete a multicast address record.
374 ether_delmulti(ifr
, ac
, ret_mca
)
376 register struct arpcom
*ac
;
377 struct ether_addr
* ret_mca
;
379 register struct ether_multi
*enm
;
380 register struct ether_multi
**p
;
381 struct sockaddr_in
*sin
;
386 switch (ifr
->ifr_addr
.sa_family
) {
389 bcopy(ifr
->ifr_addr
.sa_data
, addrlo
, 6);
390 bcopy(addrlo
, addrhi
, 6);
395 sin
= (struct sockaddr_in
*)&(ifr
->ifr_addr
);
396 if (sin
->sin_addr
.s_addr
== INADDR_ANY
) {
398 * An IP address of INADDR_ANY means stop listening
399 * to the range of Ethernet multicast addresses used
402 bcopy(ether_ipmulticast_min
, addrlo
, 6);
403 bcopy(ether_ipmulticast_max
, addrhi
, 6);
406 ETHER_MAP_IP_MULTICAST(&sin
->sin_addr
, addrlo
);
407 bcopy(addrlo
, addrhi
, 6);
414 return (EAFNOSUPPORT
);
418 * Look up the address in our list.
420 ETHER_LOOKUP_MULTI(addrlo
, addrhi
, ac
, enm
);
425 if (--enm
->enm_refcount
!= 0) {
427 * Still some claims to this record.
433 /* save the low and high address of the range before deletion */
435 *ret_mca
= *((struct ether_addr
*)addrlo
);
436 *(ret_mca
+ 1) = *((struct ether_addr
*)addrhi
);
440 * No remaining claims to this record; unlink and free it.
442 for (p
= &enm
->enm_ac
->ac_multiaddrs
;
447 FREE(enm
, M_IFMADDR
);
450 * Return ENETRESET to inform the driver that the list has changed
451 * and its reception filter should be adjusted accordingly.
457 * Convert Ethernet address to printable (loggable) representation.
459 static u_char digits
[] = "0123456789abcdef";
467 for (cp
= p
, i
= 0; i
< 6; i
++) {
468 *cp
++ = digits
[*ap
>> 4];
469 *cp
++ = digits
[*ap
++ & 0xf];