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>
81 #include <netinet/in.h>
82 #include <netinet/in_var.h>
83 #include <netinet/if_ether.h>
84 #include <netinet/in_systm.h>
85 #include <netinet/ip.h>
89 #include <netipx/ipx.h>
90 #include <netipx/ipx_if.h>
95 #include <netns/ns_if.h>
97 int ether_outputdebug
= 0;
98 int ether_inputdebug
= 0;
102 #include <netiso/argo_debug.h>
103 #include <netiso/iso.h>
104 #include <netiso/iso_var.h>
105 #include <netiso/iso_snpac.h>
109 #include <netccitt/dll.h>
110 #include <netccitt/llc_var.h>
113 #include <sys/socketvar.h>
116 extern struct ifqueue pkintrq
;
120 #include <net/bridge.h>
123 /* #include "vlan.h" */
125 #include <net/if_vlan_var.h>
126 #endif /* NVLAN > 0 */
128 static int ether_resolvemulti
__P((struct ifnet
*, struct sockaddr
**,
130 extern u_char etherbroadcastaddr
[];
131 #define senderr(e) do { error = (e); goto bad;} while (0)
132 #define IFP2AC(IFP) ((struct arpcom *)IFP)
135 * Perform common duties while attaching to interface list
141 register struct ifnet
*ifp
;
143 register struct ifaddr
*ifa
;
144 register struct sockaddr_dl
*sdl
;
145 boolean_t funnel_state
;
147 funnel_state
= thread_funnel_set(network_flock
, TRUE
);
148 printf("ether_ifattach called for %s\n", ifp
->if_name
);
152 ifp
->if_family
= APPLE_IF_FAM_ETHERNET
;
153 ifp
->if_type
= IFT_ETHER
;
156 ifp
->if_mtu
= ETHERMTU
;
157 ifp
->if_resolvemulti
= ether_resolvemulti
;
158 if (ifp
->if_baudrate
== 0)
159 ifp
->if_baudrate
= 10000000;
162 ifa
= ifnet_addrs
[ifp
->if_index
- 1];
164 printf("ether_ifattach: no lladdr!\n");
165 (void) thread_funnel_set(network_flock
, funnel_state
);
168 sdl
= (struct sockaddr_dl
*)ifa
->ifa_addr
;
169 sdl
->sdl_type
= IFT_ETHER
;
170 sdl
->sdl_alen
= ifp
->if_addrlen
;
171 bcopy((IFP2AC(ifp
))->ac_enaddr
, LLADDR(sdl
), ifp
->if_addrlen
);
173 in6_ifattach_getifid(ifp
);
175 (void) thread_funnel_set(network_flock
, funnel_state
);
178 SYSCTL_DECL(_net_link
);
179 SYSCTL_NODE(_net_link
, IFT_ETHER
, ether
, CTLFLAG_RW
, 0, "Ethernet");
182 ether_resolvemulti(ifp
, llsa
, sa
)
184 struct sockaddr
**llsa
;
187 struct sockaddr_dl
*sdl
;
188 struct sockaddr_in
*sin
;
191 struct sockaddr_in6
*sin6
;
195 switch(sa
->sa_family
) {
197 /* AppleTalk uses AF_UNSPEC for multicast registration.
198 * No mapping needed. Just check that it's a valid MC address.
200 e_addr
= &sa
->sa_data
[0];
201 if ((e_addr
[0] & 1) != 1)
202 return EADDRNOTAVAIL
;
208 * No mapping needed. Just check that it's a valid MC address.
210 sdl
= (struct sockaddr_dl
*)sa
;
211 e_addr
= LLADDR(sdl
);
212 if ((e_addr
[0] & 1) != 1)
213 return EADDRNOTAVAIL
;
219 sin
= (struct sockaddr_in
*)sa
;
220 if (!IN_MULTICAST(ntohl(sin
->sin_addr
.s_addr
)))
221 return EADDRNOTAVAIL
;
222 MALLOC(sdl
, struct sockaddr_dl
*, sizeof *sdl
, M_IFMADDR
,
224 sdl
->sdl_len
= sizeof *sdl
;
225 sdl
->sdl_family
= AF_LINK
;
226 sdl
->sdl_index
= ifp
->if_index
;
227 sdl
->sdl_type
= IFT_ETHER
;
229 sdl
->sdl_alen
= ETHER_ADDR_LEN
;
231 e_addr
= LLADDR(sdl
);
232 ETHER_MAP_IP_MULTICAST(&sin
->sin_addr
, e_addr
);
233 *llsa
= (struct sockaddr
*)sdl
;
238 sin6
= (struct sockaddr_in6
*)sa
;
239 if (IN6_IS_ADDR_UNSPECIFIED(&sin6
->sin6_addr
)) {
241 * An IP6 address of 0 means listen to all
242 * of the Ethernet multicast address used for IP6.
243 * (This is used for multicast routers.)
245 ifp
->if_flags
|= IFF_ALLMULTI
;
249 MALLOC(sdl
, struct sockaddr_dl
*, sizeof *sdl
, M_IFMADDR
,
251 sdl
->sdl_len
= sizeof *sdl
;
252 sdl
->sdl_family
= AF_LINK
;
253 sdl
->sdl_index
= ifp
->if_index
;
254 sdl
->sdl_type
= IFT_ETHER
;
256 sdl
->sdl_alen
= ETHER_ADDR_LEN
;
258 e_addr
= LLADDR(sdl
);
259 ETHER_MAP_IPV6_MULTICAST(&sin6
->sin6_addr
, e_addr
);
260 kprintf("ether_resolvemulti Adding %x:%x:%x:%x:%x:%x\n",
261 e_addr
[0], e_addr
[1], e_addr
[2], e_addr
[3], e_addr
[4], e_addr
[5]);
262 *llsa
= (struct sockaddr
*)sdl
;
268 * Well, the text isn't quite right, but it's the name
279 u_char ether_ipmulticast_min
[6] = { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 };
280 u_char ether_ipmulticast_max
[6] = { 0x01, 0x00, 0x5e, 0x7f, 0xff, 0xff };
282 * Add an Ethernet multicast address or range of addresses to the list for a
286 ether_addmulti(ifr
, ac
)
288 register struct arpcom
*ac
;
290 register struct ether_multi
*enm
;
291 struct sockaddr_in
*sin
;
296 switch (ifr
->ifr_addr
.sa_family
) {
299 bcopy(ifr
->ifr_addr
.sa_data
, addrlo
, 6);
300 bcopy(addrlo
, addrhi
, 6);
305 sin
= (struct sockaddr_in
*)&(ifr
->ifr_addr
);
306 if (sin
->sin_addr
.s_addr
== INADDR_ANY
) {
308 * An IP address of INADDR_ANY means listen to all
309 * of the Ethernet multicast addresses used for IP.
310 * (This is for the sake of IP multicast routers.)
312 bcopy(ether_ipmulticast_min
, addrlo
, 6);
313 bcopy(ether_ipmulticast_max
, addrhi
, 6);
316 ETHER_MAP_IP_MULTICAST(&sin
->sin_addr
, addrlo
);
317 bcopy(addrlo
, addrhi
, 6);
324 return (EAFNOSUPPORT
);
328 * Verify that we have valid Ethernet multicast addresses.
330 if ((addrlo
[0] & 0x01) != 1 || (addrhi
[0] & 0x01) != 1) {
335 * See if the address range is already in the list.
337 ETHER_LOOKUP_MULTI(addrlo
, addrhi
, ac
, enm
);
340 * Found it; just increment the reference count.
347 * New address or range; malloc a new multicast record
348 * and link it into the interface's multicast list.
350 enm
= (struct ether_multi
*)_MALLOC(sizeof(*enm
), M_IFMADDR
, M_WAITOK
);
355 bcopy(addrlo
, enm
->enm_addrlo
, 6);
356 bcopy(addrhi
, enm
->enm_addrhi
, 6);
358 enm
->enm_refcount
= 1;
359 enm
->enm_next
= ac
->ac_multiaddrs
;
360 ac
->ac_multiaddrs
= enm
;
363 * Return ENETRESET to inform the driver that the list has changed
364 * and its reception filter should be adjusted accordingly.
370 * Delete a multicast address record.
373 ether_delmulti(ifr
, ac
, ret_mca
)
375 register struct arpcom
*ac
;
376 struct ether_addr
* ret_mca
;
378 register struct ether_multi
*enm
;
379 register struct ether_multi
**p
;
380 struct sockaddr_in
*sin
;
385 switch (ifr
->ifr_addr
.sa_family
) {
388 bcopy(ifr
->ifr_addr
.sa_data
, addrlo
, 6);
389 bcopy(addrlo
, addrhi
, 6);
394 sin
= (struct sockaddr_in
*)&(ifr
->ifr_addr
);
395 if (sin
->sin_addr
.s_addr
== INADDR_ANY
) {
397 * An IP address of INADDR_ANY means stop listening
398 * to the range of Ethernet multicast addresses used
401 bcopy(ether_ipmulticast_min
, addrlo
, 6);
402 bcopy(ether_ipmulticast_max
, addrhi
, 6);
405 ETHER_MAP_IP_MULTICAST(&sin
->sin_addr
, addrlo
);
406 bcopy(addrlo
, addrhi
, 6);
413 return (EAFNOSUPPORT
);
417 * Look up the address in our list.
419 ETHER_LOOKUP_MULTI(addrlo
, addrhi
, ac
, enm
);
424 if (--enm
->enm_refcount
!= 0) {
426 * Still some claims to this record.
432 /* save the low and high address of the range before deletion */
434 *ret_mca
= *((struct ether_addr
*)addrlo
);
435 *(ret_mca
+ 1) = *((struct ether_addr
*)addrhi
);
439 * No remaining claims to this record; unlink and free it.
441 for (p
= &enm
->enm_ac
->ac_multiaddrs
;
446 FREE(enm
, M_IFMADDR
);
449 * Return ENETRESET to inform the driver that the list has changed
450 * and its reception filter should be adjusted accordingly.
456 * Convert Ethernet address to printable (loggable) representation.
458 static u_char digits
[] = "0123456789abcdef";
466 for (cp
= p
, i
= 0; i
< 6; i
++) {
467 *cp
++ = digits
[*ap
>> 4];
468 *cp
++ = digits
[*ap
++ & 0xf];