The FreeRADIUS server $Id: a4e7f8c94e8038b7627bcf52356b7ed15955c1d7 $
Loading...
Searching...
No Matches
proto_arp.c
Go to the documentation of this file.
1/*
2 * This program 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
5 * (at 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: efe37d760f5886daa9bee0f4c2e10fde4c57dbdc $
19 * @file proto_arp.c
20 * @brief ARP protocol handler.
21 *
22 * @copyright 2017 Arran Cudbard-Bell (a.cudbardb@freeradius.org)
23 * @copyright 2016 Alan DeKok (aland@freeradius.org)
24 */
25#include <freeradius-devel/server/module_rlm.h>
26#include <freeradius-devel/server/packet.h>
27#include <freeradius-devel/util/debug.h>
28#include "proto_arp.h"
29
30extern fr_app_t proto_arp;
31
32/** How to parse an ARP listen section
33 *
34 */
36 { FR_CONF_OFFSET_TYPE_FLAGS("transport", FR_TYPE_VOID, 0, proto_arp_t, io_submodule),
38
39 { FR_CONF_OFFSET("num_messages", proto_arp_t, num_messages) } ,
40
41 { FR_CONF_OFFSET("active", proto_arp_t, active), .dflt = "false" } ,
42
44};
45
46static fr_dict_t const *dict_arp;
47
50 { .out = &dict_arp, .proto = "arp" },
52};
53
54#if 0
56
57extern fr_dict_attr_autoload_t proto_arp_dict_attr[];
58fr_dict_attr_autoload_t proto_arp_dict_attr[] = {
59 { .out = &attr_packet_type, .name = "Packet-Type", .type = FR_TYPE_UINT32, .dict = &dict_arp},
61};
62#endif
63
64/** Decode the packet
65 *
66 */
67static int mod_decode(UNUSED void const *instance, request_t *request, uint8_t *const data, size_t data_len)
68{
69// proto_arp_t const *inst = talloc_get_type_abort_const(instance, proto_arp_t);
70 fr_arp_packet_t const *arp;
71
72 if (fr_arp_decode(request->request_ctx, &request->request_pairs, data, data_len) < 0) {
73 RPEDEBUG("Failed decoding packet");
74 return -1;
75 }
76
77 arp = (fr_arp_packet_t const *) data;
78 request->packet->code = fr_nbo_to_uint16(arp->op);
79 fr_assert(request->packet->code < FR_ARP_CODE_MAX);
80
81 request->packet->data = talloc_memdup(request->packet, data, data_len);
82 request->packet->data_len = data_len;
83
84 if (fr_packet_pairs_from_packet(request->request_ctx, &request->request_pairs, request->packet) < 0) {
85 RPEDEBUG("Failed decoding 'Net.*' packet");
86 return -1;
87 }
88
90
91 if (RDEBUG_ENABLED) {
92 RDEBUG("Received ARP %s via socket %s",
93 fr_arp_packet_codes[request->packet->code],
94 request->async->inst->name);
95
96 log_request_pair_list(L_DBG_LVL_1, request, NULL, &request->request_pairs, NULL);
97 }
98
99 return 0;
100}
101
102static uint8_t const zeros[6] = { 0 };
103
104static ssize_t mod_encode(void const *instance, request_t *request, uint8_t *buffer, size_t buffer_len)
105{
106 ssize_t slen;
108 fr_arp_packet_t *arp;
109
110 /*
111 * Process layer NAK, never respond, or "Do not respond".
112 */
113 if ((buffer_len == 1) || !inst->active ||
114 (request->reply->code == FR_ARP_DO_NOT_RESPOND) ||
115 (request->reply->code == 0) || (request->reply->code >= FR_ARP_CODE_MAX)) {
116 *buffer = false;
117 return 1;
118 }
119
120 slen = fr_arp_encode(&FR_DBUFF_TMP(buffer, buffer_len), request->packet->data, &request->reply_pairs);
121 if (slen <= 0) {
122 RPEDEBUG("Failed encoding reply");
123 return -1;
124 }
126
127 arp = (fr_arp_packet_t *) buffer;
128 fr_assert(request->packet->data_len == FR_ARP_PACKET_SIZE);
129
130 /*
131 * The reply code is the opcode, unless the policy set one.
132 */
133 if ((arp->op[0] == 0) && (arp->op[1] == 0)) {
134 arp->op[0] = (request->reply->code >> 8) & 0xff;
135 arp->op[1] = request->reply->code & 0xff;
136 }
137
138 if (memcmp(arp->sha, zeros, sizeof(arp->sha)) == 0) {
139 RDEBUG("WARNING: Sender-Hardware-Address of zeros will likely cause problems");
140 }
141
142 fr_packet_net_from_pairs(request->reply, &request->reply_pairs);
143
144 if (RDEBUG_ENABLED) {
145 RDEBUG("Sending %u via socket %s",
146 request->reply->code,
147 request->async->inst->name);
148
149 log_request_pair_list(L_DBG_LVL_1, request, NULL, &request->reply_pairs, NULL);
150 }
151
152 return slen;
153}
154
155/** Open listen sockets/connect to external event source
156 *
157 * @param[in] instance Ctx data for this application.
158 * @param[in] sc to add our file descriptor to.
159 * @param[in] conf Listen section parsed to give us instance.
160 * @return
161 * - 0 on success.
162 * - -1 on failure.
163 */
164static int mod_open(void *instance, fr_schedule_t *sc, CONF_SECTION *conf)
165{
167 fr_listen_t *listen;
168 proto_arp_t *inst = talloc_get_type_abort(instance, proto_arp_t);
169
170 /*
171 * Build the listener. This describes the complete path data
172 * takes from the socket to the decoder and back again.
173 */
174 MEM(listen = fr_listen_alloc(inst, conf, inst->server_cs, &proto_arp, instance,
175 (fr_app_io_t const *)inst->io_submodule->exported, inst->io_submodule->data));
176
177 /*
178 * Set configurable parameters for message ring buffer.
179 */
181 listen->num_messages = inst->num_messages;
182
183 /*
184 * Then the socket of it we will open.
185 */
186 MEM(li = fr_listen_instance_alloc(inst, listen));
187 talloc_set_destructor(li, fr_io_listen_free); /* frees li->thread_instance */
188
189 if (listen->app_io->common.thread_inst_size) {
190 li->thread_instance = talloc_zero_array(NULL, uint8_t, listen->app_io->common.thread_inst_size);
191 talloc_set_name(li->thread_instance, "proto_%s_thread_t", listen->app_io->common.name);
192 }
193
194 /*
195 * Open the raw socket.
196 */
197 if (li->listen->app_io->open(li) < 0) {
198 talloc_free(li);
199 return -1;
200 }
201 fr_assert(li->fd >= 0);
202
203 li->name = li->listen->app_io->get_name(li);
204
205 /*
206 * Watch the directory for changes.
207 */
208 if (!fr_schedule_listen_add(sc, li)) {
209 talloc_free(li);
210 return -1;
211 }
212
213 inst->listen = li; /* Probably won't need it, but doesn't hurt */
214 inst->sc = sc;
215
216 return 0;
217}
218
219/** Instantiate the application
220 *
221 * Instantiate I/O and type submodules.
222 *
223 * @return
224 * - 0 on success.
225 * - -1 on failure.
226 */
227static int mod_instantiate(module_inst_ctx_t const *mctx)
228{
229 proto_arp_t *inst = talloc_get_type_abort(mctx->mi->data, proto_arp_t);
230 CONF_SECTION *conf = mctx->mi->conf;
231
232 /*
233 * Ensure that the server CONF_SECTION is always set.
234 */
235 inst->server_cs = cf_item_to_section(cf_parent(conf));
236 inst->cs = conf;
237
238 if (!inst->num_messages) inst->num_messages = 256;
239
240 FR_INTEGER_BOUND_CHECK("num_messages", inst->num_messages, >=, 32);
241 FR_INTEGER_BOUND_CHECK("num_messages", inst->num_messages, <=, 65535);
242
243 return 0;
244}
245
246static int mod_load(void)
247{
248 if (fr_arp_global_init() < 0) {
249 PERROR("Failed initialising protocol library");
250 return -1;
251 }
252 return 0;
253}
254
255static void mod_unload(void)
256{
258}
259
261 .common = {
262 .magic = MODULE_MAGIC_INIT,
263 .name = "arp",
265 .inst_size = sizeof(proto_arp_t),
266 .onload = mod_load,
267 .unload = mod_unload,
268 .instantiate = mod_instantiate
269 },
270 .dict = &dict_arp,
271 .open = mod_open,
272 .decode = mod_decode,
273 .encode = mod_encode,
274};
static int const char char buffer[256]
Definition acutest.h:641
fr_io_open_t open
Open a new socket for listening, or accept/connect a new connection.
Definition app_io.h:43
module_t common
Common fields to all loadable modules.
Definition app_io.h:34
fr_io_name_t get_name
get the socket name
Definition app_io.h:70
Public structure describing an I/O path for a protocol.
Definition app_io.h:33
module_t common
Common fields provided by all modules.
Definition application.h:72
Describes a new application (protocol)
Definition application.h:71
uint8_t op[2]
1 - Request, 2 - Reply.
Definition arp.h:60
void fr_arp_global_free(void)
Definition base.c:373
int fr_arp_global_init(void)
Definition base.c:350
ssize_t fr_arp_decode(TALLOC_CTX *ctx, fr_pair_list_t *out, uint8_t const *packet, size_t packet_len)
Decode a raw ARP packet into VPs.
Definition base.c:310
#define FR_ARP_PACKET_SIZE
Definition arp.h:37
@ FR_ARP_CODE_MAX
Definition arp.h:73
@ FR_ARP_DO_NOT_RESPOND
Definition arp.h:74
char const * fr_arp_packet_codes[FR_ARP_CODE_MAX]
Definition base.c:75
ssize_t fr_arp_encode(fr_dbuff_t *dbuff, uint8_t const *original, fr_pair_list_t *vps)
Encode VPS into a raw ARP packet.
Definition base.c:235
uint8_t sha[ETHER_ADDR_LEN]
sender hardware address.
Definition arp.h:61
static uint8_t request[4]
#define UNUSED
Definition build.h:415
#define CONF_PARSER_TERMINATOR
Definition cf_parse.h:722
cf_parse_t func
Override default parsing behaviour for the specified type with a custom parsing function.
Definition cf_parse.h:666
#define FR_INTEGER_BOUND_CHECK(_name, _var, _op, _bound)
Definition cf_parse.h:572
#define FR_CONF_OFFSET(_name, _struct, _field)
conf_parser_t which parses a single CONF_PAIR, writing the result to a field in a struct
Definition cf_parse.h:280
#define FR_CONF_OFFSET_TYPE_FLAGS(_name, _type, _flags, _struct, _field)
conf_parser_t which parses a single CONF_PAIR, writing the result to a field in a struct
Definition cf_parse.h:238
Defines a CONF_PAIR to C data type mapping.
Definition cf_parse.h:649
A section grouping multiple CONF_PAIR.
Definition cf_priv.h:106
CONF_SECTION * cf_item_to_section(CONF_ITEM const *ci)
Cast a CONF_ITEM to a CONF_SECTION.
Definition cf_util.c:671
#define cf_parent(_cf)
Definition cf_util.h:115
#define FR_DBUFF_TMP(_start, _len_or_end)
Creates a compound literal to pass into functions which accept a dbuff.
Definition dbuff.h:556
#define MEM(x)
Definition debug.h:38
static fr_dict_attr_t const * attr_packet_type
Definition dhcpclient.c:88
fr_dict_attr_t const ** out
Where to write a pointer to the resolved fr_dict_attr_t.
Definition dict.h:316
fr_dict_t const ** out
Where to write a pointer to the loaded/resolved fr_dict_t.
Definition dict.h:329
#define DICT_AUTOLOAD_TERMINATOR
Definition dict.h:335
Specifies an attribute which must be present for the module to function.
Definition dict.h:315
Specifies a dictionary which must be loaded/loadable for the module to function.
Definition dict.h:328
#define MODULE_MAGIC_INIT
Stop people using different module/library/server versions together.
Definition dl_module.h:63
talloc_free(hp)
void log_request_pair_list(fr_log_lvl_t lvl, request_t *request, fr_pair_t const *parent, fr_pair_list_t const *vps, char const *prefix)
Print a fr_pair_list_t.
Definition log.c:831
#define PERROR(_fmt,...)
Definition log.h:233
#define RPEDEBUG(fmt,...)
Definition log.h:393
int fr_packet_pairs_from_packet(TALLOC_CTX *ctx, fr_pair_list_t *list, fr_packet_t const *packet)
Allocate a "Net." struct with src/dst host and port.
Definition packet.c:91
void fr_packet_net_from_pairs(fr_packet_t *packet, fr_pair_list_t const *list)
Convert pairs to information in a packet.
Definition packet.c:162
@ L_DBG_LVL_1
Highest priority debug messages (-x).
Definition log.h:67
int fr_io_listen_free(fr_listen_instance_t *li)
Free a socket's thread instance along with it.
Definition listen.c:126
fr_listen_t * fr_listen_alloc(TALLOC_CTX *ctx, CONF_SECTION *listen_cs, CONF_SECTION *server_cs, fr_app_t const *app, void const *app_instance, fr_app_io_t const *app_io, void const *app_io_instance)
Allocate a listener, and give it its id.
Definition listen.c:78
fr_listen_instance_t * fr_listen_instance_alloc(TALLOC_CTX *ctx, fr_listen_t *listen)
Allocate a socket of a listener.
Definition listen.c:111
size_t num_messages
for the message ring buffer
Definition listen.h:54
char const * name
printable name for this socket - set by open
Definition listen.h:68
fr_listen_t * listen
the listener this is a socket of.
Definition listen.h:65
int fd
file descriptor for this socket - set by open
Definition listen.h:67
fr_app_io_t const * app_io
I/O path functions.
Definition listen.h:33
void * thread_instance
thread / socket context
Definition listen.h:69
size_t default_message_size
copied from app_io, but may be changed
Definition listen.h:53
@ FR_TYPE_UINT32
32 Bit unsigned integer.
@ FR_TYPE_VOID
User data.
long int ssize_t
unsigned char uint8_t
module_instance_t * mi
Instance of the module being instantiated.
Definition module_ctx.h:51
Temporary structure to hold arguments for instantiation calls.
Definition module_ctx.h:50
static uint16_t fr_nbo_to_uint16(uint8_t const data[static sizeof(uint16_t)])
Read an unsigned 16bit integer from wire format (big endian)
Definition nbo.h:146
static int mod_load(void)
Definition proto_arp.c:246
static int mod_open(void *instance, fr_schedule_t *sc, CONF_SECTION *conf)
Open listen sockets/connect to external event source.
Definition proto_arp.c:164
fr_app_t proto_arp
Definition proto_arp.c:260
static ssize_t mod_encode(void const *instance, request_t *request, uint8_t *buffer, size_t buffer_len)
Definition proto_arp.c:104
static void mod_unload(void)
Definition proto_arp.c:255
static conf_parser_t const proto_arp_config[]
How to parse an ARP listen section.
Definition proto_arp.c:35
static fr_dict_t const * dict_arp
Definition proto_arp.c:46
static uint8_t const zeros[6]
Definition proto_arp.c:102
static int mod_decode(UNUSED void const *instance, request_t *request, uint8_t *const data, size_t data_len)
Decode the packet.
Definition proto_arp.c:67
static int mod_instantiate(module_inst_ctx_t const *mctx)
Instantiate the application.
Definition proto_arp.c:227
fr_dict_autoload_t proto_arp_dict[]
Definition proto_arp.c:49
#define fr_assert(_expr)
Definition rad_assert.h:37
#define RDEBUG(fmt,...)
#define RDEBUG_ENABLED()
static rs_t * conf
Definition radsniff.c:52
#define REQUEST_VERIFY(_x)
Definition request.h:310
fr_network_t * fr_schedule_listen_add(fr_schedule_t *sc, fr_listen_instance_t *li)
Add a fr_listen_instance_t to a scheduler.
Definition schedule.c:786
The scheduler.
Definition schedule.c:77
CONF_SECTION * conf
Module's instance configuration.
Definition module.h:355
void * data
Module's instance data.
Definition module.h:297
size_t thread_inst_size
Size of the module's thread-specific instance data.
Definition module.h:249
conf_parser_t const * config
How to convert a CONF_SECTION to a module instance.
Definition module.h:207
static const uchar sc[16]
Definition smbdes.c:115
eap_aka_sim_process_conf_t * inst
#define talloc_get_type_abort_const
Definition talloc.h:117
static fr_slen_t data
Definition value.h:1366
int virtual_server_listen_transport_parse(TALLOC_CTX *ctx, void *out, void *parent, CONF_ITEM *ci, conf_parser_t const *rule)
Generic conf_parser_t func for loading drivers.