1 | /* |
2 | * Copyright (c) 2004-2021 Apple Inc. All rights reserved. |
3 | * |
4 | * @APPLE_OSREFERENCE_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. The rights granted to you under the License |
10 | * may not be used to create, or enable the creation or redistribution of, |
11 | * unlawful or unlicensed copies of an Apple operating system, or to |
12 | * circumvent, violate, or enable the circumvention or violation of, any |
13 | * terms of an Apple operating system software license agreement. |
14 | * |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. |
17 | * |
18 | * The Original Code and all software distributed under the License are |
19 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, |
22 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
23 | * Please see the License for the specific language governing rights and |
24 | * limitations under the License. |
25 | * |
26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
27 | */ |
28 | |
29 | /* |
30 | * multicast_util.c: |
31 | * - keep track of multicast addresses added to one interface based on the |
32 | * actual multicast addresses in another |
33 | * - used by VLAN and BOND |
34 | */ |
35 | |
36 | /* |
37 | * Modification History: |
38 | * |
39 | * April 29, 2004 Dieter Siegmund (dieter@apple.com) |
40 | * - created |
41 | */ |
42 | |
43 | #include <net/multicast_list.h> |
44 | #include <sys/param.h> |
45 | #include <sys/systm.h> |
46 | #include <sys/malloc.h> |
47 | #include <net/if_dl.h> |
48 | |
49 | #include <net/sockaddr_utils.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(multicast: mc->mc_ifma); |
73 | if (error != 0) { |
74 | result = error; |
75 | } |
76 | SLIST_REMOVE_HEAD(mc_list, mc_entries); |
77 | ifmaddr_release(ifmaddr: mc->mc_ifma); |
78 | kfree_type(struct multicast_entry, mc); |
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 | u_char 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(s: (char *)&target_sdl, n: 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(mc_list: &new_mc_list); |
120 | error = ifnet_get_multicast_list(interface: source_ifp, addresses: &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(ifmaddr: source_multicast_list[i], |
129 | SA(&source_sdl), |
130 | addr_size: sizeof(source_sdl)) != 0 |
131 | || source_sdl.sdl_family != AF_LINK) { |
132 | continue; |
133 | } |
134 | mc = kalloc_type(struct multicast_entry, Z_WAITOK | Z_NOFAIL); |
135 | bcopy(LLADDR(&source_sdl), LLADDR(&target_sdl), n: alen); |
136 | error = ifnet_add_multicast(interface: target_ifp, SA(&target_sdl), |
137 | multicast: &mc->mc_ifma); |
138 | if (error != 0) { |
139 | kfree_type(struct multicast_entry, mc); |
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(mc_list: &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 | ifnet_free_multicast_list(multicasts: source_multicast_list); |
153 | return error; |
154 | } |
155 | |