1/* Get source filter. Linux version.
2 Copyright (C) 2004-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19#include <assert.h>
20#include <errno.h>
21#include <stdlib.h>
22#include <scratch_buffer.h>
23#include <string.h>
24#include <stdint.h>
25#include <netatalk/at.h>
26#include <netax25/ax25.h>
27#include <netinet/in.h>
28#include <netipx/ipx.h>
29#include <netpacket/packet.h>
30#include <netrose/rose.h>
31#include <sys/param.h>
32#include <sys/socket.h>
33#include "getsourcefilter.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
99 struct scratch_buffer buf;
100 scratch_buffer_init (buffer: &buf);
101 if (!scratch_buffer_set_array_size (buffer: &buf, nelem: 1, size: needed))
102 return -1;
103 struct group_filter *gf = buf.data;
104
105 gf->gf_interface = interface;
106 memcpy (&gf->gf_group, group, grouplen);
107 gf->gf_numsrc = *numsrc;
108
109 /* We need to provide the appropriate socket level value. */
110 int result;
111 int sol = __get_sol (af: group->sa_family, len: grouplen);
112 if (sol == -1)
113 {
114 __set_errno (EINVAL);
115 result = -1;
116 }
117 else
118 {
119 result = __getsockopt (fd: s, level: sol, MCAST_MSFILTER, optval: gf, optlen: &needed);
120
121 /* If successful, copy the results to the places the caller wants
122 them in. */
123 if (result == 0)
124 {
125 *fmode = gf->gf_fmode;
126 memcpy (slist, gf->gf_slist,
127 MIN (*numsrc, gf->gf_numsrc)
128 * sizeof (struct sockaddr_storage));
129 *numsrc = gf->gf_numsrc;
130 }
131 }
132
133 scratch_buffer_free (buffer: &buf);
134
135 return result;
136}
137

source code of glibc/sysdeps/unix/sysv/linux/getsourcefilter.c