]> git.saurik.com Git - apple/xnu.git/blob - bsd/net/multicast_list.c
a7d3f9b5f14a0913e423e7107f25021cc5f88ad3
[apple/xnu.git] / bsd / net / multicast_list.c
1 /*
2 * Copyright (c) 2006 Apple Computer, Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
5 *
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
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
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.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
29 */
30
31 /*
32 * multicast_util.c:
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
36 */
37
38 /*
39 * Modification History:
40 *
41 * April 29, 2004 Dieter Siegmund (dieter@apple.com)
42 * - created
43 */
44
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>
50
51 __private_extern__ void
52 multicast_list_init(struct multicast_list * mc_list)
53 {
54 SLIST_INIT(mc_list);
55 return;
56 }
57
58 /*
59 * Function: multicast_list_remove
60 * Purpose:
61 * Remove the given list of multicast addresses from the interface and from
62 * the multicast list structure.
63 */
64 __private_extern__ int
65 multicast_list_remove(struct multicast_list * mc_list)
66 {
67 int error;
68 struct multicast_entry * mc;
69 int result = 0;
70
71 while ((mc = SLIST_FIRST(mc_list)) != NULL) {
72 error = ifnet_remove_multicast(mc->mc_ifma);
73 if (error != 0) {
74 result = error;
75 }
76 SLIST_REMOVE_HEAD(mc_list, mc_entries);
77 ifmaddr_release(mc->mc_ifma);
78 FREE(mc, M_DEVBUF);
79 }
80 return (result);
81 }
82
83 /*
84 * Function: multicast_list_program
85 * Purpose:
86 * Program the multicast filter on "target_ifp" using the values from
87 * "source_ifp", and saving the result in "mc_list"
88 *
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
91 * new list.
92 *
93 * If it fails, we remove what we've added to the new list, and
94 * return an error.
95 */
96 __private_extern__ int
97 multicast_list_program(struct multicast_list * mc_list,
98 struct ifnet * source_ifp,
99 struct ifnet * target_ifp)
100 {
101 int alen;
102 int error = 0;
103 int i;
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;
109
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;
117
118 /* build a new list */
119 multicast_list_init(&new_mc_list);
120 error = ifnet_get_multicast_list(source_ifp, &source_multicast_list);
121 if (error != 0) {
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);
125 return (error);
126 }
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) {
132 continue;
133 }
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,
137 &mc->mc_ifma);
138 if (error != 0) {
139 FREE(mc, M_DEVBUF);
140 break;
141 }
142 SLIST_INSERT_HEAD(&new_mc_list, mc, mc_entries);
143 }
144 if (error != 0) {
145 /* restore previous state */
146 (void)multicast_list_remove(&new_mc_list);
147 } else {
148 /* remove the old entries, and return the new list */
149 (void)multicast_list_remove(mc_list);
150 *mc_list = new_mc_list;
151 }
152 return (error);
153 }