The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
bio.c
Go to the documentation of this file.
1 /*
2  * This library is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU Lesser General Public
4  * License as published by the Free Software Foundation; either
5  * version 2.1 of the License, or (at your option) any later version.
6  *
7  * This library 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 GNU
10  * Lesser General Public License for more details.
11  *
12  * You should have received a copy of the GNU Lesser General Public
13  * License along with this library; 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: 4fcfc3e03d03a52b621d35506497b5c54b18ba42 $
19  *
20  * @file protocols/radius/bio.c
21  * @brief Functions to support RADIUS bio handlers
22  *
23  * @copyright 2024 Network RADIUS SAS (legal@networkradius.com)
24  */
25 RCSID("$Id: 4fcfc3e03d03a52b621d35506497b5c54b18ba42 $")
26 
27 #include <freeradius-devel/radius/bio.h>
28 
29 /** Callback for RADIUS packet verification.
30  *
31  */
32 fr_bio_verify_action_t fr_radius_bio_verify(UNUSED fr_bio_t *bio, void *verify_ctx, UNUSED void *packet_ctx, const void *data, size_t *size)
33 {
34  decode_fail_t failure;
35  size_t in_buffer = *size;
36  fr_radius_bio_verify_t *uctx = verify_ctx;
37  uint8_t const *hdr = data;
38 
39  if (in_buffer < 4) {
40  *size = RADIUS_HEADER_LENGTH;
42  }
43 
44  /*
45  * See if we need to discard the packet.
46  */
47  if (!fr_radius_ok(data, size, uctx->max_attributes, uctx->require_message_authenticator, &failure)) {
49 
51  }
52 
53  if (!uctx->allowed[hdr[0]]) return FR_BIO_VERIFY_DISCARD;
54 
55  /*
56  * On input, *size is how much data we have. On output, *size is how much data we want.
57  */
58  return (in_buffer >= *size) ? FR_BIO_VERIFY_OK : FR_BIO_VERIFY_WANT_MORE;
59 }
60 
61 /** And verify a datagram packet.
62  *
63  */
64 fr_bio_verify_action_t fr_radius_bio_verify_datagram(UNUSED fr_bio_t *bio, void *verify_ctx, UNUSED void *packet_ctx, const void *data, size_t *size)
65 {
66  decode_fail_t failure;
67  size_t in_buffer = *size;
68  fr_radius_bio_verify_t *uctx = verify_ctx;
69  uint8_t const *hdr = data;
70 
71  if (in_buffer < RADIUS_HEADER_LENGTH) return FR_BIO_VERIFY_DISCARD;
72 
73  /*
74  * See if we need to discard the packet.
75  *
76  * @todo - move the "allowed" list to this function
77  */
78  if (!fr_radius_ok(data, size, uctx->max_attributes, uctx->require_message_authenticator, &failure)) {
79  return FR_BIO_VERIFY_DISCARD;
80  }
81 
82  if (!uctx->allowed[hdr[0]]) return FR_BIO_VERIFY_DISCARD;
83 
84  /*
85  * On input, *size is how much data we have. On output, *size is how much data we want.
86  */
87  return (in_buffer >= *size) ? FR_BIO_VERIFY_OK : FR_BIO_VERIFY_DISCARD;
88 }
Definition: base.h:112
#define RCSID(id)
Definition: build.h:481
#define UNUSED
Definition: build.h:313
fr_dcursor_eval_t void const * uctx
Definition: dcursor.h:546
fr_bio_verify_action_t
Status returned by the verification callback.
Definition: mem.h:32
@ FR_BIO_VERIFY_ERROR_CLOSE
fatal error, the bio should be closed.
Definition: mem.h:36
@ FR_BIO_VERIFY_DISCARD
the packet should be discarded
Definition: mem.h:34
@ FR_BIO_VERIFY_OK
packet is OK
Definition: mem.h:33
@ FR_BIO_VERIFY_WANT_MORE
not enough data for one packet
Definition: mem.h:35
bool fr_radius_ok(uint8_t const *packet, size_t *packet_len_p, uint32_t max_attributes, bool require_message_authenticator, decode_fail_t *reason)
Definition: merged_model.c:259
unsigned char uint8_t
Definition: merged_model.c:30
#define RADIUS_HEADER_LENGTH
Definition: net.h:80
fr_bio_verify_action_t fr_radius_bio_verify_datagram(UNUSED fr_bio_t *bio, void *verify_ctx, UNUSED void *packet_ctx, const void *data, size_t *size)
And verify a datagram packet.
Definition: bio.c:64
fr_bio_verify_action_t fr_radius_bio_verify(UNUSED fr_bio_t *bio, void *verify_ctx, UNUSED void *packet_ctx, const void *data, size_t *size)
Callback for RADIUS packet verification.
Definition: bio.c:32
@ DECODE_FAIL_UNKNOWN_PACKET_CODE
Definition: test_point.h:29
static fr_slen_t data
Definition: value.h:1265