The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
network.c
Go to the documentation of this file.
1/*
2 * This program is is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or (at
5 * your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15 */
16
17/**
18 * $Id: b55ca171ba8e4d7f72b7b6e727557daf3e8d7e91 $
19 * @file lib/bio/network.c
20 * @brief BIO patricia trie filtering handlers
21 *
22 * @copyright 2024 Network RADIUS SAS (legal@networkradius.com)
23 */
24
25#include <freeradius-devel/util/value.h>
26#include <freeradius-devel/util/trie.h>
27
28#include <freeradius-devel/bio/bio_priv.h>
29#include <freeradius-devel/bio/fd_priv.h>
30
31#include <freeradius-devel/bio/network.h>
32
33/** The network filtering bio
34 */
35typedef struct {
37
38 fr_bio_read_t discard; //!< callback to run when discarding a packet due to filtering
39
40 size_t offset; //!< where #fr_bio_fd_packet_ctx_t is stored
41
42 fr_trie_t const *trie; //!< patricia trie for filtering
44
45/** Read a UDP packet, and only return packets from allowed sources.
46 *
47 */
48static ssize_t fr_bio_network_read(fr_bio_t *bio, void *packet_ctx, void *buffer, size_t size)
49{
50 ssize_t rcode;
51 bool *value;
52 fr_bio_network_t *my = talloc_get_type_abort(bio, fr_bio_network_t);
54 fr_bio_t *next;
55
56 if (!packet_ctx) {
57 fr_strerror_const("Cannot perform network filtering");
58 return fr_bio_error(GENERIC);
59 }
60
61 next = fr_bio_next(&my->bio);
62 fr_assert(next != NULL);
63
64 rcode = next->read(next, packet_ctx, buffer, size);
65 if (rcode <= 0) return rcode;
66
67 addr = fr_bio_fd_packet_ctx(my, packet_ctx);
68
69 /*
70 * Look up this particular source. If it's not found, then we suppress this packet.
71 */
73 &addr->socket.inet.src_ipaddr.addr, addr->socket.inet.src_ipaddr.prefix);
75 if (my->discard) return my->discard(bio, packet_ctx, buffer, rcode);
76 return 0;
77 }
78
79 return rcode;
80}
81
82
83/** Allocate a bio for filtering IP addresses
84 *
85 * This is used for unconnected UDP bios, where we filter packets based on source IP address.
86 *
87 * It is also used for accept bios, where we filter new connections based on source IP address. The caller
88 * should chain this bio to the next FD bio, and then fr_bio_read() from the top-level bio. The result will
89 * be filtered or "clean" FDs.
90 *
91 * A patricia trie (but not the bio) could also be used in an haproxy "activate" callback, where the callback
92 * gets the haproxy socket info, and then checks if the source is allowed. However, that patricia trie is a
93 * property of the main "accept" bio, and should be managed by the activate() callback for the haproxy bio.
94 */
95fr_bio_t *fr_bio_network_alloc(TALLOC_CTX *ctx, fr_ipaddr_t const *allow, fr_ipaddr_t const *deny,
96 fr_bio_read_t discard, fr_bio_t *next)
97{
99 fr_bio_fd_info_t const *info;
100
101 /*
102 * We are only usable for FD bios. We need to get "offset" into the packet_ctx, and we don't
103 * want to have an API which allows for two different "offset" values to be passed to two
104 * different bios.
105 */
106 if (strcmp(talloc_get_name(next), "fr_bio_fd_t") != 0) {
107 fr_strerror_const("Invalid 'next' BIO - must be an FD one");
108 return NULL;
109 }
110
111 info = fr_bio_fd_info(next);
112 fr_assert(info != NULL);
113
114 /*
115 * We can only filter connections for IP address families.
116 *
117 * Unix domain sockets have to use a different method for filtering input connections.
118 */
119 if (!((info->socket.af == AF_INET) || (info->socket.af == AF_INET6))) return NULL;
120
121 /*
122 * We can only be used for accept() sockets, or unconnected UDP sockets.
123 */
124 switch (info->type) {
126 break;
127
130 fr_strerror_const("Cannot use network BIO with connected FD BIO");
131 return NULL;
132
133 case FR_BIO_FD_LISTEN:
134 break;
135 }
136
137 my = talloc_zero(ctx, fr_bio_network_t);
138 if (!my) return NULL;
139
140 my->offset = ((fr_bio_fd_t *) next)->offset;
141 my->discard = discard;
142
143 my->bio.write = fr_bio_next_write;
144 my->bio.read = fr_bio_network_read;
145
146 my->trie = fr_bio_network_trie_alloc(my, info->socket.af, allow, deny);
147 if (!my->trie) {
149 return NULL;
150 }
151
152 fr_bio_chain(&my->bio, next);
153 talloc_set_destructor((fr_bio_t *) my, fr_bio_destructor); /* always use a common destructor */
154
155 return (fr_bio_t *) my;
156}
157
158/** Create a patricia trie for doing network filtering.
159 *
160 */
161fr_trie_t *fr_bio_network_trie_alloc(TALLOC_CTX *ctx, int af, fr_ipaddr_t const *allow, fr_ipaddr_t const *deny)
162{
163 size_t i, num;
164 fr_trie_t *trie;
165
166 trie = fr_trie_alloc(ctx, NULL, NULL);
167 if (!trie) return NULL;
168
169 num = talloc_array_length(allow);
170 fr_assert(num > 0);
171
172 for (i = 0; i < num; i++) {
173 bool *value;
174
175 /*
176 * Can't add v4 networks to a v6 socket, or vice versa.
177 */
178 if (allow[i].af != af) {
179 fr_strerror_printf("Address family in entry %zu - 'allow = %pV' "
180 "does not match 'ipaddr'", i + 1, fr_box_ipaddr(allow[i]));
181 fail:
182 talloc_free(trie);
183 return NULL;
184 }
185
186 /*
187 * Catch duplicants, and also catch the case where we insert a /16, and then later try to
188 * insert a /24 inside of that.
189 *
190 * If instead we insert a /24, and then later a /16, we won't catch that.
191 */
192 value = fr_trie_lookup_by_key(trie, &allow[i].addr, allow[i].prefix);
193 if (value) {
194 fr_strerror_printf("Cannot add duplicate / overlapping entry 'allow = %pV'",
195 fr_box_ipaddr(allow[i]));
196 goto fail;
197 }
198
199 /*
200 * Insert the network into the trie. Lookups will return a bool ptr of allow / deny.
201 */
202 if (fr_trie_insert_by_key(trie, &allow[i].addr, allow[i].prefix, FR_BIO_NETWORK_ALLOW) < 0) {
203 fr_strerror_printf("Failed adding 'allow = %pV' to filtering rules", fr_box_ipaddr(allow[i]));
204 goto fail;
205 }
206 }
207
208 /*
209 * And now check denied networks.
210 */
211 num = talloc_array_length(deny);
212 if (!num) return trie;
213
214 /*
215 * Since the default is to deny, you can only add a "deny" inside of a previous "allow".
216 */
217 for (i = 0; i < num; i++) {
218 bool *value;
219
220 /*
221 * Can't add v4 networks to a v6 socket, or vice versa.
222 */
223 if (deny[i].af != af) {
224 fr_strerror_printf("Address family in entry %zu - 'deny = %pV' "
225 "does not match 'ipaddr'", i + 1, fr_box_ipaddr(deny[i]));
226 goto fail;
227 }
228
229 /*
230 * Exact duplicates are forbidden.
231 */
232 value = fr_trie_match_by_key(trie, &deny[i].addr, deny[i].prefix);
233 if (value) {
234 fr_strerror_printf("Cannot add duplicate entry 'deny = %pV'", fr_box_ipaddr(deny[i]));
235 goto fail;
236 }
237
238 /*
239 * A "deny" can only be within a previous "allow".
240 */
241 value = fr_trie_lookup_by_key(trie, &deny[i].addr, deny[i].prefix);
242 if (!value) {
243 fr_strerror_printf("The network in entry %zu - 'deny = %pV' is not "
244 "contained within a previous 'allow'", i + 1, fr_box_ipaddr(deny[i]));
245 goto fail;
246 }
247
248 /*
249 * A "deny" cannot be within a previous "deny".
250 */
251 if (value == FR_BIO_NETWORK_DENY) {
252 fr_strerror_printf("The network in entry %zu - 'deny = %pV' is overlaps "
253 "with another 'deny' rule", i + 1, fr_box_ipaddr(deny[i]));
254 goto fail;
255 }
256
257 /*
258 * Insert the rule into the trie.
259 */
260 if (fr_trie_insert_by_key(trie, &deny[i].addr, deny[i].prefix, FR_BIO_NETWORK_DENY) < 0) {
261 fr_strerror_printf("Failed adding 'deny = %pV' to filtering rules", fr_box_ipaddr(deny[i]));
262 goto fail;
263 }
264 }
265
266 return trie;
267}
static int const char char buffer[256]
Definition acutest.h:578
fr_bio_read_t _CONST read
read from the underlying bio
Definition base.h:116
ssize_t(* fr_bio_read_t)(fr_bio_t *bio, void *packet_ctx, void *buffer, size_t size)
Do a raw read from a socket, or other data source.
Definition base.h:81
static fr_bio_t * fr_bio_next(fr_bio_t *bio)
Definition base.h:131
#define fr_bio_error(_x)
Definition base.h:200
size_t offset
where fr_bio_fd_packet_ctx_t is stored
Definition network.c:40
fr_trie_t * fr_bio_network_trie_alloc(TALLOC_CTX *ctx, int af, fr_ipaddr_t const *allow, fr_ipaddr_t const *deny)
Create a patricia trie for doing network filtering.
Definition network.c:161
fr_bio_t * fr_bio_network_alloc(TALLOC_CTX *ctx, fr_ipaddr_t const *allow, fr_ipaddr_t const *deny, fr_bio_read_t discard, fr_bio_t *next)
Allocate a bio for filtering IP addresses.
Definition network.c:95
fr_bio_read_t discard
callback to run when discarding a packet due to filtering
Definition network.c:38
static ssize_t fr_bio_network_read(fr_bio_t *bio, void *packet_ctx, void *buffer, size_t size)
Read a UDP packet, and only return packets from allowed sources.
Definition network.c:48
fr_trie_t const * trie
patricia trie for filtering
Definition network.c:42
The network filtering bio.
Definition network.c:35
#define FR_BIO_NETWORK_DENY
Definition network.h:44
#define FR_BIO_NETWORK_ALLOW
Definition network.h:43
static void fr_bio_chain(fr_bio_t *first, fr_bio_t *second)
Chain one bio after another.
Definition bio_priv.h:84
Test enumeration values.
Definition dict_test.h:92
fr_bio_fd_info_t const * fr_bio_fd_info(fr_bio_t *bio)
Returns a pointer to the bio-specific information.
Definition fd.c:1295
fr_socket_t socket
as connected socket
Definition fd.h:132
@ FR_BIO_FD_CONNECTED
connected client sockets (UDP or TCP)
Definition fd.h:68
@ FR_BIO_FD_INVALID
not set
Definition fd.h:64
@ FR_BIO_FD_UNCONNECTED
unconnected UDP / datagram only
Definition fd.h:65
@ FR_BIO_FD_LISTEN
returns new fd in buffer on fr_bio_read() or fr_bio_fd_accept()
Definition fd.h:69
fr_bio_fd_type_t type
type of the socket
Definition fd.h:134
fr_socket_t socket
socket information, including FD.
Definition fd.h:52
Run-time status of the socket.
Definition fd.h:131
Per-packet context.
Definition fd.h:51
void fr_bio_shutdown & my
Definition fd_errno.h:70
#define fr_bio_fd_packet_ctx(_my, _packet_ctx)
Definition fd_priv.h:62
Our FD bio structure.
Definition fd_priv.h:35
talloc_free(hp)
IPv4/6 prefix.
int fr_bio_destructor(fr_bio_t *bio)
Free this bio.
Definition base.c:35
ssize_t fr_bio_next_write(fr_bio_t *bio, void *packet_ctx, void const *buffer, size_t size)
Internal bio function which just writes to the "next" bio.
Definition base.c:64
long int ssize_t
#define fr_assert(_expr)
Definition rad_assert.h:38
fr_trie_t * fr_trie_alloc(TALLOC_CTX *ctx, fr_trie_key_t get_key, fr_free_t free_data)
Allocate a trie.
Definition trie.c:741
void * fr_trie_lookup_by_key(fr_trie_t const *ft, void const *key, size_t keylen)
Lookup a key in a trie and return user ctx, if any.
Definition trie.c:1265
void * fr_trie_match_by_key(fr_trie_t const *ft, void const *key, size_t keylen)
Match a key and length in a trie and return user ctx, if any.
Definition trie.c:1289
int fr_trie_insert_by_key(fr_trie_t *ft, void const *key, size_t keylen, void const *data)
Insert a key and user ctx into a trie.
Definition trie.c:1878
int af
AF_INET, AF_INET6, or AF_UNIX.
Definition socket.h:78
#define fr_strerror_printf(_fmt,...)
Log to thread local error buffer.
Definition strerror.h:64
#define fr_strerror_const(_msg)
Definition strerror.h:223
#define fr_box_ipaddr(_val)
Definition value.h:317