1/* Get source filter. Linux version.
2 Copyright (C) 2004-2017 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@redhat.com>, 2004.
5
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
19
20#include <alloca.h>
21#include <assert.h>
22#include <errno.h>
23#include <stdlib.h>
24#include <string.h>
25#include <stdint.h>
26#include <netatalk/at.h>
27#include <netax25/ax25.h>
28#include <netinet/in.h>
29#include <netipx/ipx.h>
30#include <netpacket/packet.h>
31#include <netrose/rose.h>
32#include <sys/param.h>
33#include <sys/socket.h>
34
35
36static const struct
37{
38 int sol;
39 int af;
40 socklen_t size;
41} sol_map[] =
42 {
43 /* Sort the array according to importance of the protocols. Add
44 more protocols when they become available. */
45 { SOL_IP, AF_INET, sizeof (struct sockaddr_in) },
46 { SOL_IPV6, AF_INET6, sizeof (struct sockaddr_in6) },
47 { SOL_AX25, AF_AX25, sizeof (struct sockaddr_ax25) },
48 { SOL_IPX, AF_IPX, sizeof (struct sockaddr_ipx) },
49 { SOL_ATALK, AF_APPLETALK, sizeof (struct sockaddr_at) },
50 { SOL_ROSE, AF_ROSE, sizeof (struct sockaddr_rose) },
51 { SOL_PACKET, AF_PACKET, sizeof (struct sockaddr_ll) }
52 };
53#define NSOL_MAP (sizeof (sol_map) / sizeof (sol_map[0]))
54
55
56/* Try to determine the socket level value. Ideally both side and
57 family are set. But sometimes only the size is correct and the
58 family value might be bogus. Loop over the array entries and look
59 for a perfect match or the first match based on size. */
60int
61__get_sol (int af, socklen_t len)
62{
63 int first_size_sol = -1;
64
65 for (size_t cnt = 0; cnt < NSOL_MAP; ++cnt)
66 {
67 /* Just a test so that we make sure the special value used to
68 signal the "we have so far no socket level value" is OK. */
69 assert (sol_map[cnt].sol != -1);
70
71 if (len == sol_map[cnt].size)
72 {
73 /* The size matches, which is a requirement. If the family
74 matches, too, we have a winner. Otherwise we remember the
75 socket level value for this protocol if it is the first
76 match. */
77 if (af == sol_map[cnt].af)
78 /* Bingo! */
79 return sol_map[cnt].sol;
80
81 if (first_size_sol == -1)
82 first_size_sol = sol_map[cnt].sol;
83 }
84 }
85
86 return first_size_sol;
87}
88
89
90int
91getsourcefilter (int s, uint32_t interface, const struct sockaddr *group,
92 socklen_t grouplen, uint32_t *fmode, uint32_t *numsrc,
93 struct sockaddr_storage *slist)
94{
95 /* We have to create an struct ip_msfilter object which we can pass
96 to the kernel. */
97 socklen_t needed = GROUP_FILTER_SIZE (*numsrc);
98 int use_alloca = __libc_use_alloca (needed);
99
100 struct group_filter *gf;
101 if (use_alloca)
102 gf = (struct group_filter *) alloca (needed);
103 else
104 {
105 gf = (struct group_filter *) malloc (needed);
106 if (gf == NULL)
107 return -1;
108 }
109
110 gf->gf_interface = interface;
111 memcpy (&gf->gf_group, group, grouplen);
112 gf->gf_numsrc = *numsrc;
113
114 /* We need to provide the appropriate socket level value. */
115 int result;
116 int sol = __get_sol (group->sa_family, grouplen);
117 if (sol == -1)
118 {
119 __set_errno (EINVAL);
120 result = -1;
121 }
122 else
123 {
124 result = __getsockopt (s, sol, MCAST_MSFILTER, gf, &needed);
125
126 /* If successful, copy the results to the places the caller wants
127 them in. */
128 if (result == 0)
129 {
130 *fmode = gf->gf_fmode;
131 memcpy (slist, gf->gf_slist,
132 MIN (*numsrc, gf->gf_numsrc)
133 * sizeof (struct sockaddr_storage));
134 *numsrc = gf->gf_numsrc;
135 }
136 }
137
138 if (! use_alloca)
139 {
140 int save_errno = errno;
141 free (gf);
142 __set_errno (save_errno);
143 }
144
145 return result;
146}
147