2 * Copyright (c) 2007 Bruce M. Simpson.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of Bruce M. Simpson nor the names of other
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY BRUCE M. SIMPSON AND AFFILIATES
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BRUCE M. SIMPSON OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include "namespace.h"
35 #include <sys/types.h>
36 #include <sys/param.h>
37 #include <sys/ioctl.h>
38 #include <sys/socket.h>
40 #include <net/if_dl.h>
42 #include <netinet/in.h>
43 #include <netinet/in_systm.h>
44 #include <netinet/ip.h>
45 #include <netinet/ip_var.h>
53 #include "un-namespace.h"
56 * Advanced (Full-state) multicast group membership APIs [RFC3678]
57 * Currently this module assumes IPv4 support (INET) in the base system.
64 struct sockaddr_storage ss;
66 struct sockaddr_dl sdl;
68 struct sockaddr_in sin;
71 struct sockaddr_in6 sin6;
74 typedef union sockunion sockunion_t;
77 #define MIN(a, b) ((a) < (b) ? (a) : (b))
81 * Internal: Map an IPv4 unicast address to an interface index.
82 * This is quite inefficient so it is recommended applications use
83 * the newer, more portable, protocol independent API.
86 __inaddr_to_index(in_addr_t ifaddr)
89 struct ifaddrs *ifaddrs;
94 if (getifaddrs(&ifaddrs) < 0)
101 * Pass #1: Find the ifaddr entry corresponding to the
102 * supplied IPv4 address. We should really use the ifindex
103 * consistently for matches, however it is not available to
106 for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) {
107 psu = (sockunion_t *)ifa->ifa_addr;
108 if (psu && psu->ss.ss_family == AF_INET &&
109 psu->sin.sin_addr.s_addr == ifaddr) {
110 ifname = ifa->ifa_name;
118 * Pass #2: Find the index of the interface matching the name
119 * we obtained from looking up the IPv4 ifaddr in pass #1.
120 * There must be a better way of doing this.
122 for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) {
123 psu = (sockunion_t *)ifa->ifa_addr;
124 if (psu && psu->ss.ss_family == AF_LINK &&
125 strcmp(ifa->ifa_name, ifname) == 0) {
126 ifindex = psu->sdl.sdl_index;
130 assert(ifindex != 0);
133 freeifaddrs(ifaddrs);
138 * Set IPv4 source filter list in use on socket.
140 * Stubbed to setsourcefilter(). Performs conversion of structures which
141 * may be inefficient; applications are encouraged to use the
142 * protocol-independent API.
145 setipv4sourcefilter(int s, struct in_addr interface, struct in_addr group,
146 uint32_t fmode, uint32_t numsrc, struct in_addr *slist)
149 sockunion_t tmpgroup;
150 struct in_addr *pina;
151 sockunion_t *psu, *tmpslist;
160 if (!IN_MULTICAST(ntohl(group.s_addr)) ||
161 (fmode != MCAST_INCLUDE && fmode != MCAST_EXCLUDE)) {
166 ifindex = __inaddr_to_index(interface.s_addr);
168 errno = EADDRNOTAVAIL;
172 memset(&tmpgroup, 0, sizeof(sockunion_t));
173 tmpgroup.sin.sin_family = AF_INET;
174 tmpgroup.sin.sin_len = sizeof(struct sockaddr_in);
175 tmpgroup.sin.sin_addr = group;
177 if (numsrc != 0 || slist != NULL) {
178 tmpslist = calloc(numsrc, sizeof(sockunion_t));
179 if (tmpslist == NULL) {
186 for (i = 0; i < numsrc; i++, pina++, psu++) {
187 psu->sin.sin_family = AF_INET;
188 psu->sin.sin_len = sizeof(struct sockaddr_in);
189 psu->sin.sin_addr = *pina;
193 err = setsourcefilter(s, ifindex, (struct sockaddr *)&tmpgroup,
194 sizeof(struct sockaddr_in), fmode, numsrc,
195 (struct sockaddr_storage *)tmpslist);
197 if (tmpslist != NULL)
202 return (EAFNOSUPPORT);
207 * Get IPv4 source filter list in use on socket.
209 * Stubbed to getsourcefilter(). Performs conversion of structures which
210 * may be inefficient; applications are encouraged to use the
211 * protocol-independent API.
212 * An slist of NULL may be used for guessing the required buffer size.
215 getipv4sourcefilter(int s, struct in_addr interface, struct in_addr group,
216 uint32_t *fmode, uint32_t *numsrc, struct in_addr *slist)
218 sockunion_t *psu, *tmpslist;
219 sockunion_t tmpgroup;
220 struct in_addr *pina;
223 uint32_t ifindex, onumsrc;
226 assert(fmode != NULL);
227 assert(numsrc != NULL);
233 if (!IN_MULTICAST(ntohl(group.s_addr)) ||
234 (onumsrc != 0 && slist == NULL)) {
239 ifindex = __inaddr_to_index(interface.s_addr);
241 errno = EADDRNOTAVAIL;
245 memset(&tmpgroup, 0, sizeof(sockunion_t));
246 tmpgroup.sin.sin_family = AF_INET;
247 tmpgroup.sin.sin_len = sizeof(struct sockaddr_in);
248 tmpgroup.sin.sin_addr = group;
250 if (onumsrc != 0 || slist != NULL) {
251 tmpslist = calloc(onumsrc, sizeof(sockunion_t));
252 if (tmpslist == NULL) {
258 err = getsourcefilter(s, ifindex, (struct sockaddr *)&tmpgroup,
259 sizeof(struct sockaddr_in), fmode, numsrc,
260 (struct sockaddr_storage *)tmpslist);
262 if (tmpslist != NULL && *numsrc != 0) {
265 for (i = 0; i < MIN(onumsrc, *numsrc); i++, psu++) {
266 if (psu->ss.ss_family != AF_INET)
268 *pina++ = psu->sin.sin_addr;
277 * Set protocol-independent source filter list in use on socket.
280 setsourcefilter(int s, uint32_t interface, struct sockaddr *group,
281 socklen_t grouplen, uint32_t fmode, uint32_t numsrc,
282 struct sockaddr_storage *slist)
284 struct __msfilterreq msfr;
288 if (fmode != MCAST_INCLUDE && fmode != MCAST_EXCLUDE) {
293 psu = (sockunion_t *)group;
294 switch (psu->ss.ss_family) {
297 if ((grouplen != sizeof(struct sockaddr_in) ||
298 !IN_MULTICAST(ntohl(psu->sin.sin_addr.s_addr)))) {
303 optname = IP_MSFILTER;
308 if (grouplen != sizeof(struct sockaddr_in6) ||
309 !IN6_IS_ADDR_MULTICAST(&psu->sin6.sin6_addr)) {
313 level = IPPROTO_IPV6;
314 optname = IPV6_MSFILTER;
318 errno = EAFNOSUPPORT;
322 memset(&msfr, 0, sizeof(msfr));
323 msfr.msfr_ifindex = interface;
324 msfr.msfr_fmode = fmode;
325 msfr.msfr_nsrcs = numsrc;
326 memcpy(&msfr.msfr_group, &psu->ss, psu->ss.ss_len);
327 msfr.msfr_srcs = slist; /* pointer */
329 return (_setsockopt(s, level, optname, &msfr, sizeof(msfr)));
333 * Get protocol-independent source filter list in use on socket.
334 * An slist of NULL may be used for guessing the required buffer size.
337 getsourcefilter(int s, uint32_t interface, struct sockaddr *group,
338 socklen_t grouplen, uint32_t *fmode, uint32_t *numsrc,
339 struct sockaddr_storage *slist)
341 struct __msfilterreq msfr;
343 int err, level, optlen, optname;
345 if (interface == 0 || group == NULL || numsrc == NULL ||
354 psu = (sockunion_t *)group;
355 switch (psu->ss.ss_family) {
358 if ((grouplen != sizeof(struct sockaddr_in) ||
359 !IN_MULTICAST(ntohl(psu->sin.sin_addr.s_addr)))) {
364 optname = IP_MSFILTER;
369 if (grouplen != sizeof(struct sockaddr_in6) ||
370 !IN6_IS_ADDR_MULTICAST(&psu->sin6.sin6_addr)) {
374 level = IPPROTO_IPV6;
375 optname = IPV6_MSFILTER;
379 errno = EAFNOSUPPORT;
384 optlen = sizeof(struct __msfilterreq);
385 memset(&msfr, 0, optlen);
386 msfr.msfr_ifindex = interface;
388 msfr.msfr_nsrcs = *numsrc;
389 memcpy(&msfr.msfr_group, &psu->ss, psu->ss.ss_len);
392 * msfr_srcs is a pointer to a vector of sockaddr_storage. It
393 * may be NULL. The kernel will always return the total number
394 * of filter entries for the group in msfr.msfr_nsrcs.
396 msfr.msfr_srcs = slist;
397 err = _getsockopt(s, level, optname, &msfr, &optlen);
399 *numsrc = msfr.msfr_nsrcs;
400 *fmode = msfr.msfr_fmode;