2 * Copyright (c) 2006 Apple Computer, Inc. All Rights Reserved.
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
33 * - keep track of multicast addresses added to one interface based on the
34 * actual multicast addresses in another
35 * - used by VLAN and BOND
39 * Modification History:
41 * April 29, 2004 Dieter Siegmund (dieter@apple.com)
45 #include <net/multicast_list.h>
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/malloc.h>
49 #include <net/if_dl.h>
51 __private_extern__
void
52 multicast_list_init(struct multicast_list
* mc_list
)
59 * Function: multicast_list_remove
61 * Remove the given list of multicast addresses from the interface and from
62 * the multicast list structure.
64 __private_extern__
int
65 multicast_list_remove(struct multicast_list
* mc_list
)
68 struct multicast_entry
* mc
;
71 while ((mc
= SLIST_FIRST(mc_list
)) != NULL
) {
72 error
= ifnet_remove_multicast(mc
->mc_ifma
);
76 SLIST_REMOVE_HEAD(mc_list
, mc_entries
);
77 ifmaddr_release(mc
->mc_ifma
);
84 * Function: multicast_list_program
86 * Program the multicast filter on "target_ifp" using the values from
87 * "source_ifp", and saving the result in "mc_list"
89 * We build a new list of multicast addresses while programming the new list.
90 * If that completes successfully, we remove the old list, and return the
93 * If it fails, we remove what we've added to the new list, and
96 __private_extern__
int
97 multicast_list_program(struct multicast_list
* mc_list
,
98 struct ifnet
* source_ifp
,
99 struct ifnet
* target_ifp
)
104 struct multicast_entry
* mc
= NULL
;
105 struct multicast_list new_mc_list
;
106 struct sockaddr_dl source_sdl
;
107 ifmultiaddr_t
* source_multicast_list
;
108 struct sockaddr_dl target_sdl
;
110 alen
= target_ifp
->if_addrlen
;
111 bzero((char *)&target_sdl
, sizeof(target_sdl
));
112 target_sdl
.sdl_len
= sizeof(target_sdl
);
113 target_sdl
.sdl_family
= AF_LINK
;
114 target_sdl
.sdl_type
= target_ifp
->if_type
;
115 target_sdl
.sdl_alen
= alen
;
116 target_sdl
.sdl_index
= target_ifp
->if_index
;
118 /* build a new list */
119 multicast_list_init(&new_mc_list
);
120 error
= ifnet_get_multicast_list(source_ifp
, &source_multicast_list
);
122 printf("multicast_list_program: "
123 "ifnet_get_multicast_list(%s%d) failed, %d\n",
124 source_ifp
->if_name
, source_ifp
->if_unit
, error
);
127 for (i
= 0; source_multicast_list
[i
] != NULL
; i
++) {
128 if (ifmaddr_address(source_multicast_list
[i
],
129 (struct sockaddr
*)&source_sdl
,
130 sizeof(source_sdl
)) != 0
131 || source_sdl
.sdl_family
!= AF_LINK
) {
134 mc
= _MALLOC(sizeof(struct multicast_entry
), M_DEVBUF
, M_WAITOK
);
135 bcopy(LLADDR(&source_sdl
), LLADDR(&target_sdl
), alen
);
136 error
= ifnet_add_multicast(target_ifp
, (struct sockaddr
*)&target_sdl
,
142 SLIST_INSERT_HEAD(&new_mc_list
, mc
, mc_entries
);
145 /* restore previous state */
146 (void)multicast_list_remove(&new_mc_list
);
148 /* remove the old entries, and return the new list */
149 (void)multicast_list_remove(mc_list
);
150 *mc_list
= new_mc_list
;