The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
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: 714962d09bfaa9026ded2101fef0c2f7dc817d1c $
19  * @file proto_arp.c
20  * @brief RADIUS master 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/virtual_servers.h>
27 #include <freeradius-devel/server/packet.h>
28 #include <freeradius-devel/util/debug.h>
29 #include "proto_arp.h"
30 
31 extern fr_app_t proto_arp;
32 
33 /** How to parse an ARP listen section
34  *
35  */
36 static conf_parser_t const proto_arp_config[] = {
37  { FR_CONF_OFFSET("num_messages", proto_arp_t, num_messages) } ,
38 
39  { FR_CONF_OFFSET("active", proto_arp_t, active), .dflt = "false" } ,
40 
42 };
43 
44 static fr_dict_t const *dict_arp;
45 
48  { .out = &dict_arp, .proto = "arp" },
49  { NULL }
50 };
51 
52 #if 0
53 static fr_dict_attr_t const *attr_packet_type;
54 
55 extern fr_dict_attr_autoload_t proto_arp_dict_attr[];
56 fr_dict_attr_autoload_t proto_arp_dict_attr[] = {
57  { .out = &attr_packet_type, .name = "Packet-Type", .type = FR_TYPE_UINT32, .dict = &dict_arp},
58  { NULL }
59 };
60 #endif
61 
62 /** Decode the packet
63  *
64  */
65 static int mod_decode(UNUSED void const *instance, request_t *request, uint8_t *const data, size_t data_len)
66 {
67 // proto_arp_t const *inst = talloc_get_type_abort_const(instance, proto_arp_t);
68  fr_arp_packet_t const *arp;
69 
70  /*
71  * Set the request dictionary so that we can do
72  * generic->protocol attribute conversions as
73  * the request runs through the server.
74  */
75  request->dict = dict_arp;
76 
77  if (fr_arp_decode(request->request_ctx, &request->request_pairs, data, data_len) < 0) {
78  RPEDEBUG("Failed decoding packet");
79  return -1;
80  }
81 
82  arp = (fr_arp_packet_t const *) data;
83  request->packet->code = fr_nbo_to_uint16(arp->op);
84  fr_assert(request->packet->code < FR_ARP_CODE_MAX);
85 
86  request->packet->data = talloc_memdup(request->packet, data, data_len);
87  request->packet->data_len = data_len;
88 
89  if (fr_packet_pairs_from_packet(request->request_ctx, &request->request_pairs, request->packet) < 0) {
90  RPEDEBUG("Failed decoding 'Net.*' packet");
91  return -1;
92  }
93 
94  REQUEST_VERIFY(request);
95 
96  if (RDEBUG_ENABLED) {
97  RDEBUG("Received ARP %s via socket %s",
98  fr_arp_packet_codes[request->packet->code],
99  request->async->listen->name);
100 
101  log_request_pair_list(L_DBG_LVL_1, request, NULL, &request->request_pairs, NULL);
102  }
103 
104  return 0;
105 }
106 
107 static uint8_t const zeros[6] = { 0 };
108 
109 static ssize_t mod_encode(void const *instance, request_t *request, uint8_t *buffer, size_t buffer_len)
110 {
111  ssize_t slen;
113  fr_arp_packet_t *arp;
114 
115  /*
116  * Process layer NAK, never respond, or "Do not respond".
117  */
118  if ((buffer_len == 1) || !inst->active ||
119  (request->reply->code == FR_ARP_DO_NOT_RESPOND) ||
120  (request->reply->code == 0) || (request->reply->code >= FR_ARP_CODE_MAX)) {
121  *buffer = false;
122  return 1;
123  }
124 
125  slen = fr_arp_encode(&FR_DBUFF_TMP(buffer, buffer_len), request->packet->data, &request->reply_pairs);
126  if (slen <= 0) {
127  RPEDEBUG("Failed encoding reply");
128  return -1;
129  }
130  fr_assert(slen == FR_ARP_PACKET_SIZE);
131 
132  arp = (fr_arp_packet_t *) buffer;
133  fr_assert(request->packet->data_len == FR_ARP_PACKET_SIZE);
134 
135  if (memcmp(arp->sha, zeros, sizeof(arp->sha)) == 0) {
136  RDEBUG("WARNING: Sender-Hardware-Address of zeros will likely cause problems");
137  }
138 
139  fr_packet_net_from_pairs(request->reply, &request->reply_pairs);
140 
141  if (RDEBUG_ENABLED) {
142  RDEBUG("Sending %d via socket %s",
143  request->reply->code,
144  request->async->listen->name);
145 
146  log_request_pair_list(L_DBG_LVL_1, request, NULL, &request->reply_pairs, NULL);
147  }
148 
149  return slen;
150 }
151 
152 /** Open listen sockets/connect to external event source
153  *
154  * @param[in] instance Ctx data for this application.
155  * @param[in] sc to add our file descriptor to.
156  * @param[in] conf Listen section parsed to give us instance.
157  * @return
158  * - 0 on success.
159  * - -1 on failure.
160  */
161 static int mod_open(void *instance, fr_schedule_t *sc, UNUSED CONF_SECTION *conf)
162 {
163  fr_listen_t *li;
164  proto_arp_t *inst = talloc_get_type_abort(instance, proto_arp_t);
165 
166  /*
167  * Build the #fr_listen_t. This describes the complete
168  * path, data takes from the socket to the decoder and
169  * back again.
170  */
171  li = talloc_zero(inst, fr_listen_t);
172  talloc_set_destructor(li, fr_io_listen_free);
173 
174  li->app = &proto_arp;
175  li->app_instance = instance;
176  li->server_cs = inst->server_cs;
177 
178  /*
179  * Set configurable parameters for message ring buffer.
180  */
182  li->num_messages = inst->num_messages;
183 
184  li->app_io = (fr_app_io_t const *)inst->io_submodule->exported;
185  li->app_io_instance = inst->io_submodule->data;
187  li->thread_instance = talloc_zero_array(NULL, uint8_t, li->app_io->common.thread_inst_size);
188  talloc_set_name(li->thread_instance, "proto_%s_thread_t", li->app_io->common.name);
189  }
190 
191  /*
192  * Open the raw socket.
193  */
194  if (li->app_io->open(li) < 0) {
195  talloc_free(li);
196  return -1;
197  }
198  fr_assert(li->fd >= 0);
199 
200  li->name = li->app_io->get_name(li);
201 
202  /*
203  * Watch the directory for changes.
204  */
205  if (!fr_schedule_listen_add(sc, li)) {
206  talloc_free(li);
207  return -1;
208  }
209 
210  inst->listen = li; /* Probably won't need it, but doesn't hurt */
211  inst->sc = sc;
212 
213  return 0;
214 }
215 
216 /** Instantiate the application
217  *
218  * Instantiate I/O and type submodules.
219  *
220  * @return
221  * - 0 on success.
222  * - -1 on failure.
223  */
224 static int mod_instantiate(module_inst_ctx_t const *mctx)
225 {
226  proto_arp_t *inst = talloc_get_type_abort(mctx->mi->data, proto_arp_t);
227  CONF_SECTION *conf = mctx->mi->conf;
228 
229  /*
230  * Ensure that the server CONF_SECTION is always set.
231  */
232  inst->server_cs = cf_item_to_section(cf_parent(conf));
233  inst->cs = conf;
234 
235  if (!inst->num_messages) inst->num_messages = 256;
236 
237  FR_INTEGER_BOUND_CHECK("num_messages", inst->num_messages, >=, 32);
238  FR_INTEGER_BOUND_CHECK("num_messages", inst->num_messages, <=, 65535);
239 
240  return 0;
241 }
242 
243 static int mod_load(void)
244 {
245  if (fr_arp_global_init() < 0) {
246  PERROR("Failed initialising protocol library");
247  return -1;
248  }
249  return 0;
250 }
251 
252 static void mod_unload(void)
253 {
255 }
256 
258  .common = {
259  .magic = MODULE_MAGIC_INIT,
260  .name = "arp",
261  .config = proto_arp_config,
262  .inst_size = sizeof(proto_arp_t),
263  .onload = mod_load,
264  .unload = mod_unload,
266  },
267  .dict = &dict_arp,
268  .open = mod_open,
269  .decode = mod_decode,
270  .encode = mod_encode,
271 };
static int const char char buffer[256]
Definition: acutest.h:574
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:56
void fr_arp_global_free(void)
Definition: base.c:284
int fr_arp_global_init(void)
Definition: base.c:261
@ FR_ARP_CODE_MAX
Definition: arp.h:69
@ FR_ARP_DO_NOT_RESPOND
Definition: arp.h:70
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:221
#define FR_ARP_PACKET_SIZE
Definition: arp.h:37
char const * fr_arp_packet_codes[FR_ARP_CODE_MAX]
Definition: base.c:63
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:146
uint8_t sha[ETHER_ADDR_LEN]
sender hardware address.
Definition: arp.h:57
#define UNUSED
Definition: build.h:313
#define CONF_PARSER_TERMINATOR
Definition: cf_parse.h:627
#define FR_INTEGER_BOUND_CHECK(_name, _var, _op, _bound)
Definition: cf_parse.h:487
#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:268
Defines a CONF_PAIR to C data type mapping.
Definition: cf_parse.h:564
A section grouping multiple CONF_PAIR.
Definition: cf_priv.h:101
CONF_SECTION * cf_item_to_section(CONF_ITEM const *ci)
Cast a CONF_ITEM to a CONF_SECTION.
Definition: cf_util.c:684
#define cf_parent(_cf)
Definition: cf_util.h:101
#define FR_DBUFF_TMP(_start, _len_or_end)
Creates a compound literal to pass into functions which accept a dbuff.
Definition: dbuff.h:514
static fr_dict_attr_t const * attr_packet_type
Definition: dhcpclient.c:89
fr_dict_attr_t const ** out
Where to write a pointer to the resolved fr_dict_attr_t.
Definition: dict.h:267
fr_dict_t const ** out
Where to write a pointer to the loaded/resolved fr_dict_t.
Definition: dict.h:280
Specifies an attribute which must be present for the module to function.
Definition: dict.h:266
Specifies a dictionary which must be loaded/loadable for the module to function.
Definition: dict.h:279
#define MODULE_MAGIC_INIT
Stop people using different module/library/server versions together.
Definition: dl_module.h:63
if(rcode > 0)
Definition: fd_read.h:9
size_t num_messages
for the message ring buffer
Definition: listen.h:52
char const * name
printable name for this socket - set by open
Definition: listen.h:29
void const * app_instance
Definition: listen.h:38
size_t default_message_size
copied from app_io, but may be changed
Definition: listen.h:51
fr_app_t const * app
Definition: listen.h:37
void const * app_io_instance
I/O path configuration context.
Definition: listen.h:32
int fr_io_listen_free(fr_listen_t *li)
Definition: master.c:2923
CONF_SECTION * server_cs
CONF_SECTION of the server.
Definition: listen.h:40
void * thread_instance
thread / socket context
Definition: listen.h:33
int fd
file descriptor for this socket - set by open
Definition: listen.h:28
fr_app_io_t const * app_io
I/O path functions.
Definition: listen.h:31
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:830
#define PERROR(_fmt,...)
Definition: log.h:228
#define RPEDEBUG(fmt,...)
Definition: log.h:376
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:89
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:139
talloc_free(reap)
@ L_DBG_LVL_1
Highest priority debug messages (-x).
Definition: log.h:70
@ FR_TYPE_UINT32
32 Bit unsigned integer.
Definition: merged_model.c:99
long int ssize_t
Definition: merged_model.c:24
unsigned char uint8_t
Definition: merged_model.c:30
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:144
static int mod_load(void)
Definition: proto_arp.c:243
fr_app_t proto_arp
Definition: proto_arp.c:257
static ssize_t mod_encode(void const *instance, request_t *request, uint8_t *buffer, size_t buffer_len)
Definition: proto_arp.c:109
static void mod_unload(void)
Definition: proto_arp.c:252
static conf_parser_t const proto_arp_config[]
How to parse an ARP listen section.
Definition: proto_arp.c:36
static fr_dict_t const * dict_arp
Definition: proto_arp.c:44
static uint8_t const zeros[6]
Definition: proto_arp.c:107
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:65
static int mod_instantiate(module_inst_ctx_t const *mctx)
Instantiate the application.
Definition: proto_arp.c:224
static int mod_open(void *instance, fr_schedule_t *sc, UNUSED CONF_SECTION *conf)
Open listen sockets/connect to external event source.
Definition: proto_arp.c:161
fr_dict_autoload_t proto_arp_dict[]
Definition: proto_arp.c:47
#define RDEBUG(fmt,...)
Definition: radclient.h:53
#define RDEBUG_ENABLED()
Definition: radclient.h:49
static rs_t * conf
Definition: radsniff.c:53
#define REQUEST_VERIFY(_x)
Definition: request.h:276
static int instantiate(module_inst_ctx_t const *mctx)
Definition: rlm_rest.c:1302
fr_network_t * fr_schedule_listen_add(fr_schedule_t *sc, fr_listen_t *li)
Add a fr_listen_t to a scheduler.
Definition: schedule.c:881
The scheduler.
Definition: schedule.c:125
CONF_SECTION * conf
Module's instance configuration.
Definition: module.h:329
void * data
Module's instance data.
Definition: module.h:271
size_t thread_inst_size
Size of the module's thread-specific instance data.
Definition: module.h:235
static const uchar sc[16]
Definition: smbdes.c:115
fr_assert(0)
eap_aka_sim_process_conf_t * inst
#define talloc_get_type_abort_const
Definition: talloc.h:282
static fr_slen_t data
Definition: value.h:1265