| 1 | /* Get source filter. Linux version. |
| 2 | Copyright (C) 2004-2020 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 | <https://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 | #include "getsourcefilter.h" |
| 35 | |
| 36 | |
| 37 | static const struct |
| 38 | { |
| 39 | int sol; |
| 40 | int af; |
| 41 | socklen_t size; |
| 42 | } sol_map[] = |
| 43 | { |
| 44 | /* Sort the array according to importance of the protocols. Add |
| 45 | more protocols when they become available. */ |
| 46 | { SOL_IP, AF_INET, sizeof (struct sockaddr_in) }, |
| 47 | { SOL_IPV6, AF_INET6, sizeof (struct sockaddr_in6) }, |
| 48 | { SOL_AX25, AF_AX25, sizeof (struct sockaddr_ax25) }, |
| 49 | { SOL_IPX, AF_IPX, sizeof (struct sockaddr_ipx) }, |
| 50 | { SOL_ATALK, AF_APPLETALK, sizeof (struct sockaddr_at) }, |
| 51 | { SOL_ROSE, AF_ROSE, sizeof (struct sockaddr_rose) }, |
| 52 | { SOL_PACKET, AF_PACKET, sizeof (struct sockaddr_ll) } |
| 53 | }; |
| 54 | #define NSOL_MAP (sizeof (sol_map) / sizeof (sol_map[0])) |
| 55 | |
| 56 | |
| 57 | /* Try to determine the socket level value. Ideally both side and |
| 58 | family are set. But sometimes only the size is correct and the |
| 59 | family value might be bogus. Loop over the array entries and look |
| 60 | for a perfect match or the first match based on size. */ |
| 61 | int |
| 62 | __get_sol (int af, socklen_t len) |
| 63 | { |
| 64 | int first_size_sol = -1; |
| 65 | |
| 66 | for (size_t cnt = 0; cnt < NSOL_MAP; ++cnt) |
| 67 | { |
| 68 | /* Just a test so that we make sure the special value used to |
| 69 | signal the "we have so far no socket level value" is OK. */ |
| 70 | assert (sol_map[cnt].sol != -1); |
| 71 | |
| 72 | if (len == sol_map[cnt].size) |
| 73 | { |
| 74 | /* The size matches, which is a requirement. If the family |
| 75 | matches, too, we have a winner. Otherwise we remember the |
| 76 | socket level value for this protocol if it is the first |
| 77 | match. */ |
| 78 | if (af == sol_map[cnt].af) |
| 79 | /* Bingo! */ |
| 80 | return sol_map[cnt].sol; |
| 81 | |
| 82 | if (first_size_sol == -1) |
| 83 | first_size_sol = sol_map[cnt].sol; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | return first_size_sol; |
| 88 | } |
| 89 | |
| 90 | |
| 91 | int |
| 92 | getsourcefilter (int s, uint32_t interface, const struct sockaddr *group, |
| 93 | socklen_t grouplen, uint32_t *fmode, uint32_t *numsrc, |
| 94 | struct sockaddr_storage *slist) |
| 95 | { |
| 96 | /* We have to create an struct ip_msfilter object which we can pass |
| 97 | to the kernel. */ |
| 98 | socklen_t needed = GROUP_FILTER_SIZE (*numsrc); |
| 99 | int use_alloca = __libc_use_alloca (needed); |
| 100 | |
| 101 | struct group_filter *gf; |
| 102 | if (use_alloca) |
| 103 | gf = (struct group_filter *) alloca (needed); |
| 104 | else |
| 105 | { |
| 106 | gf = (struct group_filter *) malloc (needed); |
| 107 | if (gf == NULL) |
| 108 | return -1; |
| 109 | } |
| 110 | |
| 111 | gf->gf_interface = interface; |
| 112 | memcpy (&gf->gf_group, group, grouplen); |
| 113 | gf->gf_numsrc = *numsrc; |
| 114 | |
| 115 | /* We need to provide the appropriate socket level value. */ |
| 116 | int result; |
| 117 | int sol = __get_sol (group->sa_family, grouplen); |
| 118 | if (sol == -1) |
| 119 | { |
| 120 | __set_errno (EINVAL); |
| 121 | result = -1; |
| 122 | } |
| 123 | else |
| 124 | { |
| 125 | result = __getsockopt (s, sol, MCAST_MSFILTER, gf, &needed); |
| 126 | |
| 127 | /* If successful, copy the results to the places the caller wants |
| 128 | them in. */ |
| 129 | if (result == 0) |
| 130 | { |
| 131 | *fmode = gf->gf_fmode; |
| 132 | memcpy (slist, gf->gf_slist, |
| 133 | MIN (*numsrc, gf->gf_numsrc) |
| 134 | * sizeof (struct sockaddr_storage)); |
| 135 | *numsrc = gf->gf_numsrc; |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | if (! use_alloca) |
| 140 | { |
| 141 | int save_errno = errno; |
| 142 | free (gf); |
| 143 | __set_errno (save_errno); |
| 144 | } |
| 145 | |
| 146 | return result; |
| 147 | } |
| 148 | |