]> git.saurik.com Git - apple/xnu.git/blob - bsd/net/multicast_list.c
a58cb912d7442269a5ef8c893ba847cd9472d023
[apple/xnu.git] / bsd / net / multicast_list.c
1 /*
2 * Copyright (c) 2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 /*
25 * multicast_util.c:
26 * - keep track of multicast addresses added to one interface based on the
27 * actual multicast addresses in another
28 * - used by VLAN and BOND
29 */
30
31 /*
32 * Modification History:
33 *
34 * April 29, 2004 Dieter Siegmund (dieter@apple.com)
35 * - created
36 */
37
38 #include <net/multicast_list.h>
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/malloc.h>
42 #include <net/if_dl.h>
43
44 __private_extern__ void
45 multicast_list_init(struct multicast_list * mc_list)
46 {
47 SLIST_INIT(mc_list);
48 return;
49 }
50
51 /*
52 * Function: multicast_list_remove
53 * Purpose:
54 * Remove the given list of multicast addresses from the interface and from
55 * the multicast list structure.
56 */
57 __private_extern__ int
58 multicast_list_remove(struct multicast_list * mc_list)
59 {
60 int error;
61 struct multicast_entry * mc;
62 int result = 0;
63
64 while ((mc = SLIST_FIRST(mc_list)) != NULL) {
65 error = ifnet_remove_multicast(mc->mc_ifma);
66 if (error != 0) {
67 result = error;
68 }
69 SLIST_REMOVE_HEAD(mc_list, mc_entries);
70 ifmaddr_release(mc->mc_ifma);
71 FREE(mc, M_DEVBUF);
72 }
73 return (result);
74 }
75
76 /*
77 * Function: multicast_list_program
78 * Purpose:
79 * Program the multicast filter on "target_ifp" using the values from
80 * "source_ifp", and saving the result in "mc_list"
81 *
82 * We build a new list of multicast addresses while programming the new list.
83 * If that completes successfully, we remove the old list, and return the
84 * new list.
85 *
86 * If it fails, we remove what we've added to the new list, and
87 * return an error.
88 */
89 __private_extern__ int
90 multicast_list_program(struct multicast_list * mc_list,
91 struct ifnet * source_ifp,
92 struct ifnet * target_ifp)
93 {
94 int alen;
95 int error = 0;
96 int i;
97 struct multicast_entry * mc = NULL;
98 struct multicast_list new_mc_list;
99 struct sockaddr_dl source_sdl;
100 ifmultiaddr_t * source_multicast_list;
101 struct sockaddr_dl target_sdl;
102
103 alen = target_ifp->if_addrlen;
104 bzero((char *)&target_sdl, sizeof(target_sdl));
105 target_sdl.sdl_len = sizeof(target_sdl);
106 target_sdl.sdl_family = AF_LINK;
107 target_sdl.sdl_type = target_ifp->if_type;
108 target_sdl.sdl_alen = alen;
109 target_sdl.sdl_index = target_ifp->if_index;
110
111 /* build a new list */
112 multicast_list_init(&new_mc_list);
113 error = ifnet_get_multicast_list(source_ifp, &source_multicast_list);
114 if (error != 0) {
115 printf("multicast_list_program: "
116 "ifnet_get_multicast_list(%s%d) failed, %d\n",
117 source_ifp->if_name, source_ifp->if_unit, error);
118 return (error);
119 }
120 for (i = 0; source_multicast_list[i] != NULL; i++) {
121 if (ifmaddr_address(source_multicast_list[i],
122 (struct sockaddr *)&source_sdl,
123 sizeof(source_sdl)) != 0
124 || source_sdl.sdl_family != AF_LINK) {
125 continue;
126 }
127 mc = _MALLOC(sizeof(struct multicast_entry), M_DEVBUF, M_WAITOK);
128 bcopy(LLADDR(&source_sdl), LLADDR(&target_sdl), alen);
129 error = ifnet_add_multicast(target_ifp, (struct sockaddr *)&target_sdl,
130 &mc->mc_ifma);
131 if (error != 0) {
132 FREE(mc, M_DEVBUF);
133 break;
134 }
135 SLIST_INSERT_HEAD(&new_mc_list, mc, mc_entries);
136 }
137 if (error != 0) {
138 /* restore previous state */
139 (void)multicast_list_remove(&new_mc_list);
140 } else {
141 /* remove the old entries, and return the new list */
142 (void)multicast_list_remove(mc_list);
143 *mc_list = new_mc_list;
144 }
145 return (error);
146 }