1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #include <stdio.h>
28*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
29*7c478bd9Sstevel@tonic-gate #include <unistd.h>
30*7c478bd9Sstevel@tonic-gate #include <string.h>
31*7c478bd9Sstevel@tonic-gate #include <stropts.h>
32*7c478bd9Sstevel@tonic-gate #include <errno.h>
33*7c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
34*7c478bd9Sstevel@tonic-gate #include <sys/socket.h>
35*7c478bd9Sstevel@tonic-gate #include <sys/sockio.h>
36*7c478bd9Sstevel@tonic-gate #include <netinet/in.h>
37*7c478bd9Sstevel@tonic-gate 
38*7c478bd9Sstevel@tonic-gate int
getsourcefilter(int s,uint32_t interface,struct sockaddr * group,socklen_t grouplen,uint32_t * fmode,uint_t * numsrc,struct sockaddr_storage * slist)39*7c478bd9Sstevel@tonic-gate getsourcefilter(int s, uint32_t interface, struct sockaddr *group,
40*7c478bd9Sstevel@tonic-gate     socklen_t grouplen, uint32_t *fmode, uint_t *numsrc,
41*7c478bd9Sstevel@tonic-gate     struct sockaddr_storage *slist)
42*7c478bd9Sstevel@tonic-gate {
43*7c478bd9Sstevel@tonic-gate 	struct group_filter *gf;
44*7c478bd9Sstevel@tonic-gate 	int mallocsize, orig_numsrc, cpsize, rtnerr;
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate 	mallocsize = (*numsrc == 0) ?
47*7c478bd9Sstevel@tonic-gate 	    sizeof (struct group_filter) : GROUP_FILTER_SIZE(*numsrc);
48*7c478bd9Sstevel@tonic-gate 	gf = (struct group_filter *)malloc(mallocsize);
49*7c478bd9Sstevel@tonic-gate 	if (gf == NULL) {
50*7c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
51*7c478bd9Sstevel@tonic-gate 		return (-1);
52*7c478bd9Sstevel@tonic-gate 	}
53*7c478bd9Sstevel@tonic-gate 
54*7c478bd9Sstevel@tonic-gate 	gf->gf_interface = interface;
55*7c478bd9Sstevel@tonic-gate 	gf->gf_numsrc = orig_numsrc = *numsrc;
56*7c478bd9Sstevel@tonic-gate 	switch (group->sa_family) {
57*7c478bd9Sstevel@tonic-gate 	case AF_INET:
58*7c478bd9Sstevel@tonic-gate 		if (grouplen < sizeof (struct sockaddr_in)) {
59*7c478bd9Sstevel@tonic-gate 			rtnerr = ENOPROTOOPT;
60*7c478bd9Sstevel@tonic-gate 			goto done;
61*7c478bd9Sstevel@tonic-gate 		}
62*7c478bd9Sstevel@tonic-gate 		(void) memcpy((void *)&gf->gf_group, (void *)group,
63*7c478bd9Sstevel@tonic-gate 		    sizeof (struct sockaddr_in));
64*7c478bd9Sstevel@tonic-gate 		break;
65*7c478bd9Sstevel@tonic-gate 	case AF_INET6:
66*7c478bd9Sstevel@tonic-gate 		if (grouplen < sizeof (struct sockaddr_in6)) {
67*7c478bd9Sstevel@tonic-gate 			rtnerr = ENOPROTOOPT;
68*7c478bd9Sstevel@tonic-gate 			goto done;
69*7c478bd9Sstevel@tonic-gate 		}
70*7c478bd9Sstevel@tonic-gate 		(void) memcpy((void *)&gf->gf_group, (void *)group,
71*7c478bd9Sstevel@tonic-gate 		    sizeof (struct sockaddr_in6));
72*7c478bd9Sstevel@tonic-gate 		break;
73*7c478bd9Sstevel@tonic-gate 	default:
74*7c478bd9Sstevel@tonic-gate 		rtnerr = EAFNOSUPPORT;
75*7c478bd9Sstevel@tonic-gate 		goto done;
76*7c478bd9Sstevel@tonic-gate 	}
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate 	rtnerr = ioctl(s, SIOCGMSFILTER, (void *)gf);
79*7c478bd9Sstevel@tonic-gate 	if (rtnerr == -1) {
80*7c478bd9Sstevel@tonic-gate 		rtnerr = errno;
81*7c478bd9Sstevel@tonic-gate 		goto done;
82*7c478bd9Sstevel@tonic-gate 	}
83*7c478bd9Sstevel@tonic-gate 
84*7c478bd9Sstevel@tonic-gate 	*fmode = gf->gf_fmode;
85*7c478bd9Sstevel@tonic-gate 	*numsrc = gf->gf_numsrc;
86*7c478bd9Sstevel@tonic-gate 	cpsize = MIN(orig_numsrc, *numsrc) * sizeof (struct sockaddr_storage);
87*7c478bd9Sstevel@tonic-gate 	(void) memcpy((void *)slist, (void *)gf->gf_slist, cpsize);
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate done:
90*7c478bd9Sstevel@tonic-gate 	free(gf);
91*7c478bd9Sstevel@tonic-gate 	errno = rtnerr;
92*7c478bd9Sstevel@tonic-gate 	if (errno != 0)
93*7c478bd9Sstevel@tonic-gate 		return (-1);
94*7c478bd9Sstevel@tonic-gate 
95*7c478bd9Sstevel@tonic-gate 	return (0);
96*7c478bd9Sstevel@tonic-gate }
97*7c478bd9Sstevel@tonic-gate 
98*7c478bd9Sstevel@tonic-gate int
setsourcefilter(int s,uint32_t interface,struct sockaddr * group,socklen_t grouplen,uint32_t fmode,uint_t numsrc,struct sockaddr_storage * slist)99*7c478bd9Sstevel@tonic-gate setsourcefilter(int s, uint32_t interface, struct sockaddr *group,
100*7c478bd9Sstevel@tonic-gate     socklen_t grouplen, uint32_t fmode, uint_t numsrc,
101*7c478bd9Sstevel@tonic-gate     struct sockaddr_storage *slist)
102*7c478bd9Sstevel@tonic-gate {
103*7c478bd9Sstevel@tonic-gate 	struct group_filter *gf;
104*7c478bd9Sstevel@tonic-gate 	int mallocsize, rtnerr;
105*7c478bd9Sstevel@tonic-gate 
106*7c478bd9Sstevel@tonic-gate 	mallocsize = (numsrc == 0) ?
107*7c478bd9Sstevel@tonic-gate 	    sizeof (struct group_filter) : GROUP_FILTER_SIZE(numsrc);
108*7c478bd9Sstevel@tonic-gate 	gf = (struct group_filter *)malloc(mallocsize);
109*7c478bd9Sstevel@tonic-gate 	if (gf == NULL) {
110*7c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
111*7c478bd9Sstevel@tonic-gate 		return (-1);
112*7c478bd9Sstevel@tonic-gate 	}
113*7c478bd9Sstevel@tonic-gate 
114*7c478bd9Sstevel@tonic-gate 	switch (group->sa_family) {
115*7c478bd9Sstevel@tonic-gate 	case AF_INET:
116*7c478bd9Sstevel@tonic-gate 		if (grouplen < sizeof (struct sockaddr_in)) {
117*7c478bd9Sstevel@tonic-gate 			rtnerr = ENOPROTOOPT;
118*7c478bd9Sstevel@tonic-gate 			goto done;
119*7c478bd9Sstevel@tonic-gate 		}
120*7c478bd9Sstevel@tonic-gate 		(void) memcpy((void *)&gf->gf_group, (void *)group,
121*7c478bd9Sstevel@tonic-gate 		    sizeof (struct sockaddr_in));
122*7c478bd9Sstevel@tonic-gate 		break;
123*7c478bd9Sstevel@tonic-gate 	case AF_INET6:
124*7c478bd9Sstevel@tonic-gate 		if (grouplen < sizeof (struct sockaddr_in6)) {
125*7c478bd9Sstevel@tonic-gate 			rtnerr = ENOPROTOOPT;
126*7c478bd9Sstevel@tonic-gate 			goto done;
127*7c478bd9Sstevel@tonic-gate 		}
128*7c478bd9Sstevel@tonic-gate 		(void) memcpy((void *)&gf->gf_group, (void *)group,
129*7c478bd9Sstevel@tonic-gate 		    sizeof (struct sockaddr_in6));
130*7c478bd9Sstevel@tonic-gate 		break;
131*7c478bd9Sstevel@tonic-gate 	default:
132*7c478bd9Sstevel@tonic-gate 		rtnerr = EAFNOSUPPORT;
133*7c478bd9Sstevel@tonic-gate 		goto done;
134*7c478bd9Sstevel@tonic-gate 	}
135*7c478bd9Sstevel@tonic-gate 	gf->gf_interface = interface;
136*7c478bd9Sstevel@tonic-gate 	gf->gf_fmode = fmode;
137*7c478bd9Sstevel@tonic-gate 	gf->gf_numsrc = numsrc;
138*7c478bd9Sstevel@tonic-gate 	(void) memcpy((void *)gf->gf_slist, (void *)slist,
139*7c478bd9Sstevel@tonic-gate 	    (numsrc * sizeof (struct sockaddr_storage)));
140*7c478bd9Sstevel@tonic-gate 
141*7c478bd9Sstevel@tonic-gate 	rtnerr = ioctl(s, SIOCSMSFILTER, (void *)gf);
142*7c478bd9Sstevel@tonic-gate 	if (rtnerr == -1) {
143*7c478bd9Sstevel@tonic-gate 		rtnerr = errno;
144*7c478bd9Sstevel@tonic-gate 	}
145*7c478bd9Sstevel@tonic-gate 
146*7c478bd9Sstevel@tonic-gate done:
147*7c478bd9Sstevel@tonic-gate 	free(gf);
148*7c478bd9Sstevel@tonic-gate 	errno = rtnerr;
149*7c478bd9Sstevel@tonic-gate 	if (errno != 0)
150*7c478bd9Sstevel@tonic-gate 		return (-1);
151*7c478bd9Sstevel@tonic-gate 
152*7c478bd9Sstevel@tonic-gate 	return (0);
153*7c478bd9Sstevel@tonic-gate }
154*7c478bd9Sstevel@tonic-gate 
155*7c478bd9Sstevel@tonic-gate int
getipv4sourcefilter(int s,struct in_addr interface,struct in_addr group,uint32_t * fmode,uint32_t * numsrc,struct in_addr * slist)156*7c478bd9Sstevel@tonic-gate getipv4sourcefilter(int s, struct in_addr interface, struct in_addr group,
157*7c478bd9Sstevel@tonic-gate     uint32_t *fmode, uint32_t *numsrc, struct in_addr *slist)
158*7c478bd9Sstevel@tonic-gate {
159*7c478bd9Sstevel@tonic-gate 	struct ip_msfilter *imsf;
160*7c478bd9Sstevel@tonic-gate 	int mallocsize, orig_numsrc, cpsize, rtnerr;
161*7c478bd9Sstevel@tonic-gate 
162*7c478bd9Sstevel@tonic-gate 	mallocsize = (*numsrc == 0) ?
163*7c478bd9Sstevel@tonic-gate 	    sizeof (struct ip_msfilter) : IP_MSFILTER_SIZE(*numsrc);
164*7c478bd9Sstevel@tonic-gate 	imsf = (struct ip_msfilter *)malloc(mallocsize);
165*7c478bd9Sstevel@tonic-gate 	if (imsf == NULL) {
166*7c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
167*7c478bd9Sstevel@tonic-gate 		return (-1);
168*7c478bd9Sstevel@tonic-gate 	}
169*7c478bd9Sstevel@tonic-gate 
170*7c478bd9Sstevel@tonic-gate 	imsf->imsf_interface = interface;
171*7c478bd9Sstevel@tonic-gate 	imsf->imsf_numsrc = orig_numsrc = *numsrc;
172*7c478bd9Sstevel@tonic-gate 	imsf->imsf_multiaddr = group;
173*7c478bd9Sstevel@tonic-gate 
174*7c478bd9Sstevel@tonic-gate 	rtnerr = ioctl(s, SIOCGIPMSFILTER, (void *)imsf);
175*7c478bd9Sstevel@tonic-gate 	if (rtnerr == -1) {
176*7c478bd9Sstevel@tonic-gate 		rtnerr = errno;
177*7c478bd9Sstevel@tonic-gate 		goto done;
178*7c478bd9Sstevel@tonic-gate 	}
179*7c478bd9Sstevel@tonic-gate 
180*7c478bd9Sstevel@tonic-gate 	*fmode = imsf->imsf_fmode;
181*7c478bd9Sstevel@tonic-gate 	*numsrc = imsf->imsf_numsrc;
182*7c478bd9Sstevel@tonic-gate 	cpsize = MIN(orig_numsrc, *numsrc) * sizeof (struct in_addr);
183*7c478bd9Sstevel@tonic-gate 	(void) memcpy((void *)slist, (void *)imsf->imsf_slist, cpsize);
184*7c478bd9Sstevel@tonic-gate 
185*7c478bd9Sstevel@tonic-gate done:
186*7c478bd9Sstevel@tonic-gate 	free(imsf);
187*7c478bd9Sstevel@tonic-gate 	errno = rtnerr;
188*7c478bd9Sstevel@tonic-gate 	if (errno != 0)
189*7c478bd9Sstevel@tonic-gate 		return (-1);
190*7c478bd9Sstevel@tonic-gate 
191*7c478bd9Sstevel@tonic-gate 	return (0);
192*7c478bd9Sstevel@tonic-gate }
193*7c478bd9Sstevel@tonic-gate 
194*7c478bd9Sstevel@tonic-gate int
setipv4sourcefilter(int s,struct in_addr interface,struct in_addr group,uint32_t fmode,uint32_t numsrc,struct in_addr * slist)195*7c478bd9Sstevel@tonic-gate setipv4sourcefilter(int s, struct in_addr interface, struct in_addr group,
196*7c478bd9Sstevel@tonic-gate     uint32_t fmode, uint32_t numsrc, struct in_addr *slist)
197*7c478bd9Sstevel@tonic-gate {
198*7c478bd9Sstevel@tonic-gate 	struct ip_msfilter *imsf;
199*7c478bd9Sstevel@tonic-gate 	int mallocsize, rtnerr;
200*7c478bd9Sstevel@tonic-gate 
201*7c478bd9Sstevel@tonic-gate 	mallocsize = (numsrc == 0) ?
202*7c478bd9Sstevel@tonic-gate 	    sizeof (struct ip_msfilter) : IP_MSFILTER_SIZE(numsrc);
203*7c478bd9Sstevel@tonic-gate 	imsf = (struct ip_msfilter *)malloc(mallocsize);
204*7c478bd9Sstevel@tonic-gate 	if (imsf == NULL) {
205*7c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
206*7c478bd9Sstevel@tonic-gate 		return (-1);
207*7c478bd9Sstevel@tonic-gate 	}
208*7c478bd9Sstevel@tonic-gate 
209*7c478bd9Sstevel@tonic-gate 	imsf->imsf_multiaddr = group;
210*7c478bd9Sstevel@tonic-gate 	imsf->imsf_interface = interface;
211*7c478bd9Sstevel@tonic-gate 	imsf->imsf_fmode = fmode;
212*7c478bd9Sstevel@tonic-gate 	imsf->imsf_numsrc = numsrc;
213*7c478bd9Sstevel@tonic-gate 	(void) memcpy((void *)imsf->imsf_slist, (void *)slist,
214*7c478bd9Sstevel@tonic-gate 	    (numsrc * sizeof (struct in_addr)));
215*7c478bd9Sstevel@tonic-gate 
216*7c478bd9Sstevel@tonic-gate 	rtnerr = ioctl(s, SIOCSIPMSFILTER, (void *)imsf);
217*7c478bd9Sstevel@tonic-gate 	if (rtnerr == -1) {
218*7c478bd9Sstevel@tonic-gate 		rtnerr = errno;
219*7c478bd9Sstevel@tonic-gate 	}
220*7c478bd9Sstevel@tonic-gate 
221*7c478bd9Sstevel@tonic-gate 	free(imsf);
222*7c478bd9Sstevel@tonic-gate 	errno = rtnerr;
223*7c478bd9Sstevel@tonic-gate 	if (errno != 0)
224*7c478bd9Sstevel@tonic-gate 		return (-1);
225*7c478bd9Sstevel@tonic-gate 
226*7c478bd9Sstevel@tonic-gate 	return (0);
227*7c478bd9Sstevel@tonic-gate }
228