The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
proto_radius.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: a8111b5bde7221537a0a9f1425164abe81eb59f4 $
19  * @file proto_radius.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/radius/radius.h>
26 #include <freeradius-devel/io/listen.h>
27 #include <freeradius-devel/unlang/xlat_func.h>
28 #include <freeradius-devel/server/module_rlm.h>
29 #include <stdbool.h>
30 #include "proto_radius.h"
31 
32 extern fr_app_t proto_radius;
33 
34 static int type_parse(TALLOC_CTX *ctx, void *out, UNUSED void *parent, CONF_ITEM *ci, conf_parser_t const *rule);
35 static int transport_parse(TALLOC_CTX *ctx, void *out, void *parent, CONF_ITEM *ci, conf_parser_t const *rule);
36 
37 static conf_parser_t const limit_config[] = {
38  { FR_CONF_OFFSET("cleanup_delay", proto_radius_t, io.cleanup_delay), .dflt = "5.0" } ,
39  { FR_CONF_OFFSET("idle_timeout", proto_radius_t, io.idle_timeout), .dflt = "30.0" } ,
40  { FR_CONF_OFFSET("nak_lifetime", proto_radius_t, io.nak_lifetime), .dflt = "30.0" } ,
41 
42  { FR_CONF_OFFSET("max_connections", proto_radius_t, io.max_connections), .dflt = "1024" } ,
43  { FR_CONF_OFFSET("max_clients", proto_radius_t, io.max_clients), .dflt = "256" } ,
44  { FR_CONF_OFFSET("max_pending_packets", proto_radius_t, io.max_pending_packets), .dflt = "256" } ,
45 
46  /*
47  * For performance tweaking. NOT for normal humans.
48  */
49  { FR_CONF_OFFSET("max_packet_size", proto_radius_t, max_packet_size) } ,
50  { FR_CONF_OFFSET("num_messages", proto_radius_t, num_messages) } ,
51 
53 };
54 
55 static const conf_parser_t priority_config[] = {
56  { FR_CONF_OFFSET("Access-Request", proto_radius_t, priorities[FR_RADIUS_CODE_ACCESS_REQUEST]),
57  .func = cf_table_parse_int, .uctx = &(cf_table_parse_ctx_t){ .table = channel_packet_priority, .len = &channel_packet_priority_len }, .dflt = "high" },
58  { FR_CONF_OFFSET("Accounting-Request", proto_radius_t, priorities[FR_RADIUS_CODE_ACCOUNTING_REQUEST]),
59  .func = cf_table_parse_int, .uctx = &(cf_table_parse_ctx_t){ .table = channel_packet_priority, .len = &channel_packet_priority_len }, .dflt = "low" },
60  { FR_CONF_OFFSET("CoA-Request", proto_radius_t, priorities[FR_RADIUS_CODE_COA_REQUEST]),
61  .func = cf_table_parse_int, .uctx = &(cf_table_parse_ctx_t){ .table = channel_packet_priority, .len = &channel_packet_priority_len }, .dflt = "normal" },
62  { FR_CONF_OFFSET("Disconnect-Request", proto_radius_t, priorities[FR_RADIUS_CODE_DISCONNECT_REQUEST]),
63  .func = cf_table_parse_int, .uctx = &(cf_table_parse_ctx_t){ .table = channel_packet_priority, .len = &channel_packet_priority_len }, .dflt = "low" },
64  { FR_CONF_OFFSET("Status-Server", proto_radius_t, priorities[FR_RADIUS_CODE_STATUS_SERVER]),
65  .func = cf_table_parse_int, .uctx = &(cf_table_parse_ctx_t){ .table = channel_packet_priority, .len = &channel_packet_priority_len }, .dflt = "now" },
66 
68 };
69 
70 /** How to parse a RADIUS listen section
71  *
72  */
74  { FR_CONF_OFFSET_FLAGS("type", CONF_FLAG_NOT_EMPTY, proto_radius_t, allowed_types), .func = type_parse },
75  { FR_CONF_OFFSET_TYPE_FLAGS("transport", FR_TYPE_VOID, 0, proto_radius_t, io.submodule),
76  .func = transport_parse },
77 
78  /*
79  * Check whether or not the *trailing* bits of a
80  * Tunnel-Password are zero, as they should be.
81  */
82  { FR_CONF_OFFSET("tunnel_password_zeros", proto_radius_t, tunnel_password_zeros) } ,
83 
84  { FR_CONF_POINTER("limit", 0, CONF_FLAG_SUBSECTION, NULL), .subcs = (void const *) limit_config },
85  { FR_CONF_POINTER("priority", 0, CONF_FLAG_SUBSECTION, NULL), .subcs = (void const *) priority_config },
86 
87  { FR_CONF_OFFSET("require_message_authenticator", proto_radius_t, require_message_authenticator),
88  .func = cf_table_parse_int,
90  .dflt = "no" },
91 
92  { FR_CONF_OFFSET("limit_proxy_state", proto_radius_t, limit_proxy_state),
93  .func = cf_table_parse_int,
95  .dflt = "auto" },
96 
98 };
99 
100 static fr_dict_t const *dict_radius;
101 
104  { .out = &dict_radius, .proto = "radius" },
105  { NULL }
106 };
107 
113 
116  { .out = &attr_packet_type, .name = "Packet-Type", .type = FR_TYPE_UINT32, .dict = &dict_radius},
117  { .out = &attr_user_name, .name = "User-Name", .type = FR_TYPE_STRING, .dict = &dict_radius},
118  { .out = &attr_state, .name = "State", .type = FR_TYPE_OCTETS, .dict = &dict_radius},
119  { .out = &attr_proxy_state, .name = "Proxy-State", .type = FR_TYPE_OCTETS, .dict = &dict_radius},
120  { .out = &attr_message_authenticator, .name = "Message-Authenticator", .type = FR_TYPE_OCTETS, .dict = &dict_radius},
121  { NULL }
122 };
123 
124 /** Translates the packet-type into a submodule name
125  *
126  * If we found a Packet-Type = Access-Request CONF_PAIR for example, here's we'd load
127  * the proto_radius_auth module.
128  *
129  * @param[in] ctx to allocate data in (instance of proto_radius).
130  * @param[out] out Where to write a module_instance_t containing the module handle and instance.
131  * @param[in] parent Base structure address.
132  * @param[in] ci #CONF_PAIR specifying the name of the type module.
133  * @param[in] rule unused.
134  * @return
135  * - 0 on success.
136  * - -1 on failure.
137  */
138 static int type_parse(UNUSED TALLOC_CTX *ctx, void *out, void *parent, CONF_ITEM *ci, UNUSED conf_parser_t const *rule)
139 {
140  proto_radius_t *inst = talloc_get_type_abort(parent, proto_radius_t);
142  CONF_PAIR *cp;
143  char const *value;
144 
145  cp = cf_item_to_pair(ci);
146  value = cf_pair_value(cp);
147 
149  if (!dv || (dv->value->vb_uint32 >= FR_RADIUS_CODE_MAX)) {
150  cf_log_err(ci, "Unknown RADIUS packet type '%s'", value);
151  return -1;
152  }
153 
154  inst->allowed[dv->value->vb_uint32] = true;
155  *((char const **) out) = value;
156 
157  return 0;
158 }
159 
160 static int transport_parse(TALLOC_CTX *ctx, void *out, void *parent, CONF_ITEM *ci, conf_parser_t const *rule)
161 {
162  proto_radius_t *inst = talloc_get_type_abort(parent, proto_radius_t);
163  module_instance_t *mi;
164 
165  if (unlikely(virtual_server_listen_transport_parse(ctx, out, parent, ci, rule) < 0)) {
166  return -1;
167  }
168 
169  mi = talloc_get_type_abort(*(void **)out, module_instance_t);
170  inst->io.app_io = (fr_app_io_t const *)mi->exported;
171  inst->io.app_io_instance = mi->data;
172  inst->io.app_io_conf = mi->conf;
173 
174  return 0;
175 }
176 
177 /** Decode the packet
178  *
179  */
180 static int mod_decode(void const *instance, request_t *request, uint8_t *const data, size_t data_len)
181 {
183  fr_io_track_t const *track = talloc_get_type_abort_const(request->async->packet_ctx, fr_io_track_t);
184  fr_io_address_t const *address = track->address;
185  fr_client_t *client = UNCONST(fr_client_t *, address->radclient);
186  fr_radius_ctx_t common_ctx;
187  fr_radius_decode_ctx_t decode_ctx;
188  fr_radius_require_ma_t require_message_authenticator = client->require_message_authenticator_is_set ?
190  inst->require_message_authenticator;
191  fr_radius_limit_proxy_state_t limit_proxy_state = client->limit_proxy_state_is_set ?
192  client->limit_proxy_state:
193  inst->limit_proxy_state;
194 
196 
197  /*
198  * Set the request dictionary so that we can do
199  * generic->protocol attribute conversions as
200  * the request runs through the server.
201  */
202  request->dict = dict_radius;
203 
204  common_ctx = (fr_radius_ctx_t) {
205  .secret = client->secret,
206  .secret_length = talloc_array_length(client->secret) - 1,
207  };
208 
209  request->packet->code = data[0];
210 
211  decode_ctx = (fr_radius_decode_ctx_t) {
212  .common = &common_ctx,
213  .tmp_ctx = talloc(request, uint8_t),
214  /* decode figures out request_authenticator */
215  .end = data + data_len,
216  .verify = client->active,
217  };
218 
219  if (request->packet->code == FR_RADIUS_CODE_ACCESS_REQUEST) {
220  /*
221  * bit1 is set if we've seen a packet, and the auto bit in require_message_authenticator is set/
222  * bit2 is set if we always require a message_authenticator.
223  * If either bit is high we require a message authenticator in the packet.
224  */
225  decode_ctx.require_message_authenticator = (
226  (client->received_message_authenticator & require_message_authenticator) |
227  (require_message_authenticator & FR_RADIUS_REQUIRE_MA_YES)
228  ) > 0;
229  decode_ctx.limit_proxy_state = (
230  (client->first_packet_no_proxy_state & limit_proxy_state) |
231  (limit_proxy_state & FR_RADIUS_LIMIT_PROXY_STATE_YES)
232  ) > 0;
233  }
234 
235  /*
236  * The verify() routine over-writes the request packet vector.
237  *
238  * @todo - That needs to be changed.
239  */
240  request->packet->id = data[1];
241  request->reply->id = data[1];
242  memcpy(request->packet->vector, data + 4, sizeof(request->packet->vector));
243 
244  request->packet->data = talloc_memdup(request->packet, data, data_len);
245  request->packet->data_len = data_len;
246 
247  /*
248  * !client->active means a fake packet defining a dynamic client - so there will
249  * be no secret defined yet - so can't verify.
250  */
251  if (fr_radius_decode(request->request_ctx, &request->request_pairs,
252  data, data_len, &decode_ctx) < 0) {
253  talloc_free(decode_ctx.tmp_ctx);
254  RPEDEBUG("Failed reading packet");
255  return -1;
256  }
257  talloc_free(decode_ctx.tmp_ctx);
258 
259  /*
260  * Set the rest of the fields.
261  */
262  request->client = client;
263 
264  request->packet->socket = address->socket;
265  fr_socket_addr_swap(&request->reply->socket, &address->socket);
266 
267  if (request->packet->code == FR_RADIUS_CODE_ACCESS_REQUEST) {
268  /*
269  * If require_message_authenticator is "auto" then
270  * we start requiring messages authenticator after
271  * the first Access-Request packet containing a
272  * verified one. This isn't vulnerable to the same
273  * attack as limit_proxy_state, as the attacker would
274  * need knowledge of the secret.
275  *
276  * Unfortunately there are too many cases where
277  * auto mode could break things (dealing with
278  * multiple clients behind a NAT for example).
279  */
280  if ((require_message_authenticator == FR_RADIUS_REQUIRE_MA_AUTO) &&
282  fr_pair_find_by_da(&request->request_pairs, NULL, attr_message_authenticator)) {
283  client->received_message_authenticator = true;
284 
285  RINFO("Packet from client %pV (%pV) contained a valid Message-Authenticator. Setting \"require_message_authenticator = yes\"",
286  fr_box_ipaddr(client->ipaddr),
288  }
289 
290  /*
291  * It's important we only evaluate this on the
292  * first packet. Otherwise an attacker could send
293  * Access-Requests with no Proxy-State whilst
294  * spoofing a legitimate Proxy-Server, and causing an
295  * outage.
296  *
297  * The likelihood of an attacker sending a packet
298  * to coincide with the reboot of a RADIUS
299  * server is low. That said, 'auto' should likely
300  * not be enabled for internet facing servers.
301  */
302  if ((limit_proxy_state == FR_RADIUS_LIMIT_PROXY_STATE_AUTO) && client->active && !client->seen_first_packet) {
303  client->seen_first_packet = true;
304  client->first_packet_no_proxy_state = fr_pair_find_by_da(&request->request_pairs, NULL, attr_proxy_state) == NULL;
305 
306  RINFO("First packet from %pV (%pV) %s Proxy-State. Setting \"limit_proxy_state = %s\"",
307  fr_box_ipaddr(client->ipaddr),
309  client->first_packet_no_proxy_state ? "did not contain" : "contained",
310  client->first_packet_no_proxy_state ? "yes" : "no");
311 
312  if (!client->received_message_authenticator) {
313  RWARN("Received packet from %pV (%pV) which did not contain Message-Authenticator:",
314  fr_box_ipaddr(client->ipaddr),
316  RWARN("- Enable Message-Authenticator on the client");
317  RWARN("- Require Message-Authenticator in the client definition (client { require_message_authenticator = yes })");
318  }
319 
320  if (!client->first_packet_no_proxy_state) {
321  RWARN("As configured, your client HIGHLY VULNERABLE to the BlastRADIUS response spoofing attack, TAKE ACTION IMMEDIATELY!");
322  } else {
323  RWARN("As configured, your client is vulnerable to the BlastRADIUS response spoofing attack");
324  }
325  }
326  }
327 
328  REQUEST_VERIFY(request);
329 
330  /*
331  * If we're defining a dynamic client, this packet is
332  * fake. We don't have a secret, so we mash all of the
333  * encrypted attributes to sane (i.e. non-hurtful)
334  * values.
335  */
336  if (!client->active) {
337  fr_pair_t *vp;
338 
339  fr_assert(client->dynamic);
340 
342 
343  for (vp = fr_pair_list_head(&request->request_pairs);
344  vp != NULL;
345  vp = fr_pair_list_next(&request->request_pairs, vp)) {
347  switch (vp->vp_type) {
348  default:
349  break;
350 
351  case FR_TYPE_UINT32:
352  vp->vp_uint32 = 0;
353  break;
354 
355  case FR_TYPE_IPV4_ADDR:
356  vp->vp_ipv4addr = INADDR_ANY;
357  break;
358 
359  case FR_TYPE_OCTETS:
360  fr_pair_value_memdup(vp, (uint8_t const *) "", 1, true);
361  break;
362 
363  case FR_TYPE_STRING:
364  fr_pair_value_strdup(vp, "", true);
365  break;
366  }
367  }
368  }
369  }
370 
371  /*
372  * Set the sequence to be at least one. This will
373  * prioritize replies to Access-Challenges over other
374  * packets. The sequence will be updated (if necessary)
375  * by the RADIUS state machine. If the request yields,
376  * it will get re-inserted with an updated sequence
377  * number.
378  */
379  if ((request->packet->code == FR_RADIUS_CODE_ACCESS_REQUEST) &&
380  fr_pair_find_by_da(&request->request_pairs, NULL, attr_state)) {
381  request->async->sequence = 1;
382  }
383 
384  if (fr_packet_pairs_from_packet(request->request_ctx, &request->request_pairs, request->packet) < 0) {
385  RPEDEBUG("Failed decoding 'Net.*' packet");
386  return -1;
387  }
388 
389  return 0;
390 }
391 
392 static ssize_t mod_encode(UNUSED void const *instance, request_t *request, uint8_t *buffer, size_t buffer_len)
393 {
394  fr_io_track_t *track = talloc_get_type_abort(request->async->packet_ctx, fr_io_track_t);
395  fr_io_address_t const *address = track->address;
396  ssize_t data_len;
397  fr_client_t const *client;
398  fr_radius_ctx_t common_ctx = {};
400 
401  /*
402  * Process layer NAK, or "Do not respond".
403  */
404  if ((buffer_len == 1) ||
405  (request->reply->code == FR_RADIUS_CODE_DO_NOT_RESPOND) ||
406  (request->reply->code == 0) || (request->reply->code >= FR_RADIUS_CODE_MAX)) {
407  track->do_not_respond = true;
408  return 1;
409  }
410 
411  client = address->radclient;
412  fr_assert(client);
413 
414  /*
415  * Dynamic client stuff
416  */
417  if (client->dynamic && !client->active) {
418  fr_client_t *new_client;
419 
420  fr_assert(buffer_len >= sizeof(client));
421 
422  /*
423  * We don't accept the new client, so don't do
424  * anything.
425  */
426  if (request->reply->code != FR_RADIUS_CODE_ACCESS_ACCEPT) {
427  *buffer = true;
428  return 1;
429  }
430 
431  /*
432  * Allocate the client. If that fails, send back a NAK.
433  *
434  * @todo - deal with NUMA zones? Or just deal with this
435  * client being in different memory.
436  *
437  * Maybe we should create a CONF_SECTION from the client,
438  * and pass *that* back to mod_write(), which can then
439  * parse it to create the actual client....
440  */
441  new_client = client_afrom_request(NULL, request);
442  if (!new_client) {
443  PERROR("Failed creating new client");
444  *buffer = true;
445  return 1;
446  }
447 
448  memcpy(buffer, &new_client, sizeof(new_client));
449  return sizeof(new_client);
450  }
451 
452  /*
453  * Overwrite the src ip address on the outbound packet
454  * with the one specified by the client. This is useful
455  * to work around broken DSR implementations and other
456  * routing issues.
457  */
458  if (client->src_ipaddr.af != AF_UNSPEC) {
459  request->reply->socket.inet.src_ipaddr = client->src_ipaddr;
460  }
461 
462  common_ctx = (fr_radius_ctx_t) {
463  .secret = client->secret,
464  .secret_length = talloc_array_length(client->secret) - 1,
465  };
467  .common = &common_ctx,
468  .request_authenticator = request->packet->data + 4,
469  .rand_ctx = (fr_fast_rand_t) {
470  .a = fr_rand(),
471  .b = fr_rand(),
472  },
473  .request_code = request->packet->data[0],
474  .code = request->reply->code,
475  .id = request->reply->id,
476  };
477 
478  data_len = fr_radius_encode(&FR_DBUFF_TMP(buffer, buffer_len), &request->reply_pairs, &encode_ctx);
479  if (data_len < 0) {
480  RPEDEBUG("Failed encoding RADIUS reply");
481  return -1;
482  }
483 
484  if (fr_radius_sign(buffer, request->packet->data + 4,
485  (uint8_t const *) client->secret, talloc_array_length(client->secret) - 1) < 0) {
486  RPEDEBUG("Failed signing RADIUS reply");
487  return -1;
488  }
489 
490  fr_packet_net_from_pairs(request->reply, &request->reply_pairs);
491 
492  if (RDEBUG_ENABLED) {
493  RDEBUG("Sending %s ID %i from %pV:%i to %pV:%i length %zu via socket %s",
494  fr_radius_packet_name[request->reply->code],
495  request->reply->id,
496  fr_box_ipaddr(request->reply->socket.inet.src_ipaddr),
497  request->reply->socket.inet.src_port,
498  fr_box_ipaddr(request->reply->socket.inet.dst_ipaddr),
499  request->reply->socket.inet.dst_port,
500  data_len,
501  request->async->listen->name);
502 
503  log_request_pair_list(L_DBG_LVL_1, request, NULL, &request->reply_pairs, NULL);
504  }
505 
506  return data_len;
507 }
508 
509 static int mod_priority_set(void const *instance, uint8_t const *buffer, UNUSED size_t buflen)
510 {
512 
513  fr_assert(buffer[0] > 0);
515 
516  /*
517  * Disallowed packet
518  */
519  if (!inst->priorities[buffer[0]]) return 0;
520 
521  if (!inst->allowed[buffer[0]]) return -1;
522 
523  /*
524  * @todo - if we cared, we could also return -1 for "this
525  * is a bad packet". But that's really only for
526  * mod_inject, as we assume that app_io->read() always
527  * returns good packets.
528  */
529 
530  /*
531  * Return the configured priority.
532  */
533  return inst->priorities[buffer[0]];
534 }
535 
536 /** Open listen sockets/connect to external event source
537  *
538  * @param[in] instance Ctx data for this application.
539  * @param[in] sc to add our file descriptor to.
540  * @param[in] conf Listen section parsed to give us instance.
541  * @return
542  * - 0 on success.
543  * - -1 on failure.
544  */
545 static int mod_open(void *instance, fr_schedule_t *sc, UNUSED CONF_SECTION *conf)
546 {
547  proto_radius_t *inst = talloc_get_type_abort(instance, proto_radius_t);
548 
549  /*
550  * io.app_io should already be set
551  */
552  return fr_master_io_listen(&inst->io, sc,
553  inst->max_packet_size, inst->num_messages);
554 }
555 
556 /** Instantiate the application
557  *
558  * Instantiate I/O and type submodules.
559  *
560  * @return
561  * - 0 on success.
562  * - -1 on failure.
563  */
564 static int mod_instantiate(module_inst_ctx_t const *mctx)
565 {
566  proto_radius_t *inst = talloc_get_type_abort(mctx->mi->data, proto_radius_t);
567 
568  /*
569  * No IO module, it's an empty listener.
570  */
571  if (!inst->io.submodule) return 0;
572 
573  /*
574  * These timers are usually protocol specific.
575  */
576  FR_TIME_DELTA_BOUND_CHECK("idle_timeout", inst->io.idle_timeout, >=, fr_time_delta_from_sec(1));
577  FR_TIME_DELTA_BOUND_CHECK("idle_timeout", inst->io.idle_timeout, <=, fr_time_delta_from_sec(600));
578 
579  FR_TIME_DELTA_BOUND_CHECK("nak_lifetime", inst->io.nak_lifetime, >=, fr_time_delta_from_sec(1));
580  FR_TIME_DELTA_BOUND_CHECK("nak_lifetime", inst->io.nak_lifetime, <=, fr_time_delta_from_sec(600));
581 
582  FR_TIME_DELTA_BOUND_CHECK("cleanup_delay", inst->io.cleanup_delay, <=, fr_time_delta_from_sec(30));
583  FR_TIME_DELTA_BOUND_CHECK("cleanup_delay", inst->io.cleanup_delay, >, fr_time_delta_from_sec(0));
584 
585 #if 0
586  /*
587  * No Access-Request packets, then no cleanup delay.
588  */
589  if (!inst->allowed[FR_RADIUS_CODE_ACCESS_REQUEST]) {
590  inst->io.cleanup_delay = 0;
591  }
592 #endif
593 
594  /*
595  * Ensure that the server CONF_SECTION is always set.
596  */
597  inst->io.server_cs = cf_item_to_section(cf_parent(mctx->mi->conf));
598 
599  /*
600  * These configuration items are not printed by default,
601  * because normal people shouldn't be touching them.
602  */
603  if (!inst->max_packet_size && inst->io.app_io) inst->max_packet_size = inst->io.app_io->default_message_size;
604 
605  if (!inst->num_messages) inst->num_messages = 256;
606 
607  FR_INTEGER_BOUND_CHECK("num_messages", inst->num_messages, >=, 32);
608  FR_INTEGER_BOUND_CHECK("num_messages", inst->num_messages, <=, 65535);
609 
610  FR_INTEGER_BOUND_CHECK("max_packet_size", inst->max_packet_size, >=, 1024);
611  FR_INTEGER_BOUND_CHECK("max_packet_size", inst->max_packet_size, <=, 65535);
612 
613  /*
614  * Tell the master handler about the main protocol instance.
615  */
616  inst->io.app = &proto_radius;
617  inst->io.app_instance = inst;
618 
619  /*
620  * We will need this for dynamic clients and connected sockets.
621  */
622  inst->io.mi = mctx->mi;
623 
624  /*
625  * Instantiate the transport module before calling the
626  * common instantiation function.
627  */
628  if (module_instantiate(inst->io.submodule) < 0) return -1;
629 
630  /*
631  * Instantiate the master io submodule
632  */
634 }
635 
636 /** Get the authentication vector.
637  *
638  * Note that we don't allow people to get the reply vector, because
639  * it doesn't exist until the reply is sent.
640  *
641  */
643  UNUSED xlat_ctx_t const *xctx, request_t *request,
644  UNUSED fr_value_box_list_t *in)
645 {
646  fr_value_box_t *vb;
647 
648  if (request->dict != dict_radius) return XLAT_ACTION_FAIL;
649 
650  MEM(vb = fr_value_box_alloc(ctx, FR_TYPE_OCTETS, NULL));
651  if (fr_value_box_memdup(vb, vb, NULL, request->packet->vector, sizeof(request->packet->vector), true) < 0) {
652  talloc_free(vb);
653  return XLAT_ACTION_FAIL;
654  }
655 
656  fr_dcursor_append(out, vb);
657 
658  return XLAT_ACTION_DONE;
659 }
660 
661 
662 static int mod_load(void)
663 {
664  if (fr_radius_global_init() < 0) {
665  PERROR("Failed initialising protocol library");
666  return -1;
667  }
668 
669 
670  if (!xlat_func_register(NULL, "radius.packet.vector", packet_vector_xlat, FR_TYPE_OCTETS)) return -1;
671 
672  return 0;
673 }
674 
675 static void mod_unload(void)
676 {
677  xlat_func_unregister("radius.packet.vector");
678 
680 }
681 
683  .common = {
684  .magic = MODULE_MAGIC_INIT,
685  .name = "radius",
686  .config = proto_radius_config,
687  .inst_size = sizeof(proto_radius_t),
688  .onload = mod_load,
689  .unload = mod_unload,
691  },
692  .dict = &dict_radius,
693  .open = mod_open,
694  .decode = mod_decode,
695  .encode = mod_encode,
696  .priority = mod_priority_set
697 };
static int const char char buffer[256]
Definition: acutest.h:574
module_t common
Common fields to all loadable modules.
Definition: app_io.h:34
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
#define UNCONST(_type, _ptr)
Remove const qualification from a pointer.
Definition: build.h:165
#define unlikely(_x)
Definition: build.h:379
#define UNUSED
Definition: build.h:313
int cf_table_parse_int(UNUSED TALLOC_CTX *ctx, void *out, UNUSED void *parent, CONF_ITEM *ci, conf_parser_t const *rule)
Generic function for parsing conf pair values as int.
Definition: cf_parse.c:1474
#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
#define FR_CONF_POINTER(_name, _type, _flags, _res_p)
conf_parser_t which parses a single CONF_PAIR producing a single global result
Definition: cf_parse.h:310
#define FR_CONF_OFFSET_FLAGS(_name, _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:256
#define FR_TIME_DELTA_BOUND_CHECK(_name, _var, _op, _bound)
Definition: cf_parse.h:498
@ CONF_FLAG_NOT_EMPTY
CONF_PAIR is required to have a non zero length value.
Definition: cf_parse.h:420
@ CONF_FLAG_SUBSECTION
Instead of putting the information into a configuration structure, the configuration file routines MA...
Definition: cf_parse.h:399
#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:241
Defines a CONF_PAIR to C data type mapping.
Definition: cf_parse.h:564
Common header for all CONF_* types.
Definition: cf_priv.h:49
Configuration AVP similar to a fr_pair_t.
Definition: cf_priv.h:70
A section grouping multiple CONF_PAIR.
Definition: cf_priv.h:101
CONF_PAIR * cf_item_to_pair(CONF_ITEM const *ci)
Cast a CONF_ITEM to a CONF_PAIR.
Definition: cf_util.c:664
char const * cf_pair_value(CONF_PAIR const *pair)
Return the value of a CONF_PAIR.
Definition: cf_util.c:1594
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_log_err(_cf, _fmt,...)
Definition: cf_util.h:289
#define cf_parent(_cf)
Definition: cf_util.h:101
size_t channel_packet_priority_len
Definition: channel.c:170
fr_table_num_sorted_t const channel_packet_priority[]
Definition: channel.c:164
#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 int fr_dcursor_append(fr_dcursor_t *cursor, void *v)
Insert a single item at the end of the list.
Definition: dcursor.h:406
@ FR_RADIUS_CODE_ACCESS_REQUEST
RFC2865 - Access-Request.
Definition: defs.h:33
@ FR_RADIUS_CODE_DISCONNECT_REQUEST
RFC3575/RFC5176 - Disconnect-Request.
Definition: defs.h:46
@ FR_RADIUS_CODE_DO_NOT_RESPOND
Special rcode to indicate we will not respond.
Definition: defs.h:54
@ FR_RADIUS_CODE_MAX
Maximum possible protocol code.
Definition: defs.h:53
@ FR_RADIUS_CODE_STATUS_SERVER
RFC2865/RFC5997 - Status Server (request)
Definition: defs.h:44
@ FR_RADIUS_CODE_COA_REQUEST
RFC3575/RFC5176 - CoA-Request.
Definition: defs.h:49
@ FR_RADIUS_CODE_ACCESS_ACCEPT
RFC2865 - Access-Accept.
Definition: defs.h:34
@ FR_RADIUS_CODE_ACCOUNTING_REQUEST
RFC2866 - Accounting-Request.
Definition: defs.h:36
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
fr_value_box_t const * value
Enum value (what name maps to).
Definition: dict.h:230
fr_dict_enum_value_t * fr_dict_enum_by_name(fr_dict_attr_t const *da, char const *name, ssize_t len)
Definition: dict_util.c:3395
static fr_slen_t in
Definition: dict.h:821
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
Value of an enumerated attribute.
Definition: dict.h:226
Test enumeration values.
Definition: dict_test.h:92
#define MODULE_MAGIC_INIT
Stop people using different module/library/server versions together.
Definition: dl_module.h:63
int af
Address family.
Definition: inet.h:64
fr_socket_t socket
src/dst ip and port.
Definition: base.h:336
fr_client_t const * radclient
old-style client definition
Definition: base.h:338
fr_ipaddr_t ipaddr
IPv4/IPv6 address of the host.
Definition: client.h:83
bool received_message_authenticator
Whether we've seen a message authenticator from this client in any previous packets.
Definition: client.h:111
fr_radius_require_ma_t require_message_authenticator
Require RADIUS message authenticator for incoming packets.
Definition: client.h:94
char const * secret
Secret PSK.
Definition: client.h:90
bool active
for dynamic clients
Definition: client.h:119
fr_ipaddr_t src_ipaddr
IPv4/IPv6 address to send responses from (family must match ipaddr).
Definition: client.h:84
bool require_message_authenticator_is_set
Whether require_message_authenticator is set in the configuration.
Definition: client.h:98
bool seen_first_packet
Whether we've seen a packet from this client.
Definition: client.h:114
bool limit_proxy_state_is_set
Whether limit_proxy_state is set in the configuration.
Definition: client.h:109
bool dynamic
Whether the client was dynamically defined.
Definition: client.h:118
bool first_packet_no_proxy_state
Whether that first packet contained a Proxy-State attribute.
Definition: client.h:115
char const * shortname
Client nickname.
Definition: client.h:88
fr_radius_limit_proxy_state_t limit_proxy_state
Whether to allow Proxy-State in incoming packets that don't contain a message authenticator.
Definition: client.h:105
Describes a host allowed to send packets to the server.
Definition: client.h:80
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 RWARN(fmt,...)
Definition: log.h:297
#define RINFO(fmt,...)
Definition: log.h:296
#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_app_io_t fr_master_app_io
Definition: master.c:3139
int fr_master_io_listen(fr_io_instance_t *inst, fr_schedule_t *sc, size_t default_message_size, size_t num_messages)
Definition: master.c:2931
fr_io_address_t const * address
of this packet.. shared between multiple packets
Definition: master.h:54
bool do_not_respond
don't respond
Definition: master.h:50
@ FR_TYPE_IPV4_ADDR
32 Bit IPv4 Address.
Definition: merged_model.c:86
@ FR_TYPE_STRING
String of printable characters.
Definition: merged_model.c:83
@ FR_TYPE_UINT32
32 Bit unsigned integer.
Definition: merged_model.c:99
@ FR_TYPE_VOID
User data.
Definition: merged_model.c:127
@ FR_TYPE_OCTETS
Raw octets.
Definition: merged_model.c:84
long int ssize_t
Definition: merged_model.c:24
unsigned char uint8_t
Definition: merged_model.c:30
#define MODULE_INST_CTX(_mi)
Wrapper to create a module_inst_ctx_t as a compound literal.
Definition: module_ctx.h:158
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
fr_pair_t * fr_pair_find_by_da(fr_pair_list_t const *list, fr_pair_t const *prev, fr_dict_attr_t const *da)
Find the first pair with a matching da.
Definition: pair.c:693
int fr_pair_value_memdup(fr_pair_t *vp, uint8_t const *src, size_t len, bool tainted)
Copy data into an "octets" data type.
Definition: pair.c:2981
int fr_pair_value_strdup(fr_pair_t *vp, char const *src, bool tainted)
Copy data into an "string" data type.
Definition: pair.c:2634
static fr_internal_encode_ctx_t encode_ctx
static int mod_load(void)
Definition: proto_radius.c:662
static fr_dict_attr_t const * attr_packet_type
Definition: proto_radius.c:108
static ssize_t mod_encode(UNUSED void const *instance, request_t *request, uint8_t *buffer, size_t buffer_len)
Definition: proto_radius.c:392
static xlat_action_t packet_vector_xlat(TALLOC_CTX *ctx, fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, request_t *request, UNUSED fr_value_box_list_t *in)
Get the authentication vector.
Definition: proto_radius.c:642
static fr_dict_attr_t const * attr_state
Definition: proto_radius.c:110
fr_dict_autoload_t proto_radius_dict[]
Definition: proto_radius.c:103
static conf_parser_t const limit_config[]
Definition: proto_radius.c:37
static int type_parse(TALLOC_CTX *ctx, void *out, UNUSED void *parent, CONF_ITEM *ci, conf_parser_t const *rule)
fr_app_t proto_radius
Definition: proto_radius.c:682
static int mod_decode(void const *instance, request_t *request, uint8_t *const data, size_t data_len)
Decode the packet.
Definition: proto_radius.c:180
static fr_dict_t const * dict_radius
Definition: proto_radius.c:100
static void mod_unload(void)
Definition: proto_radius.c:675
static fr_dict_attr_t const * attr_proxy_state
Definition: proto_radius.c:111
static const conf_parser_t priority_config[]
Definition: proto_radius.c:55
static fr_dict_attr_t const * attr_user_name
Definition: proto_radius.c:109
static int transport_parse(TALLOC_CTX *ctx, void *out, void *parent, CONF_ITEM *ci, conf_parser_t const *rule)
Definition: proto_radius.c:160
static int mod_instantiate(module_inst_ctx_t const *mctx)
Instantiate the application.
Definition: proto_radius.c:564
static int mod_open(void *instance, fr_schedule_t *sc, UNUSED CONF_SECTION *conf)
Open listen sockets/connect to external event source.
Definition: proto_radius.c:545
static int mod_priority_set(void const *instance, uint8_t const *buffer, UNUSED size_t buflen)
Definition: proto_radius.c:509
fr_dict_attr_autoload_t proto_radius_dict_attr[]
Definition: proto_radius.c:115
static fr_dict_attr_t const * attr_message_authenticator
Definition: proto_radius.c:112
static conf_parser_t const proto_radius_config[]
How to parse a RADIUS listen section.
Definition: proto_radius.c:73
An instance of a proto_radius listen section.
Definition: proto_radius.h:32
ssize_t fr_radius_decode(TALLOC_CTX *ctx, fr_pair_list_t *out, uint8_t *packet, size_t packet_len, fr_radius_decode_ctx_t *decode_ctx)
Definition: base.c:1087
size_t fr_radius_limit_proxy_state_table_len
Definition: base.c:99
int fr_radius_sign(uint8_t *packet, uint8_t const *vector, uint8_t const *secret, size_t secret_len)
Sign a previously encoded packet.
Definition: base.c:358
size_t fr_radius_require_ma_table_len
Definition: base.c:90
int fr_radius_global_init(void)
Definition: base.c:1217
void fr_radius_global_free(void)
Definition: base.c:1240
fr_table_num_sorted_t const fr_radius_limit_proxy_state_table[]
Definition: base.c:92
fr_table_num_sorted_t const fr_radius_require_ma_table[]
Definition: base.c:83
ssize_t fr_radius_encode(fr_dbuff_t *dbuff, fr_pair_list_t *vps, fr_radius_encode_ctx_t *packet_ctx)
Definition: base.c:952
char const * fr_radius_packet_name[FR_RADIUS_CODE_MAX]
Definition: base.c:112
#define RDEBUG(fmt,...)
Definition: radclient.h:53
#define RDEBUG_ENABLED()
Definition: radclient.h:49
fr_radius_require_ma_t
Control whether Message-Authenticator is required in Access-Requests.
Definition: radius.h:62
@ FR_RADIUS_REQUIRE_MA_YES
Require Message-Authenticator.
Definition: radius.h:68
@ FR_RADIUS_REQUIRE_MA_AUTO
Only require Message-Authenticator if we've previously received a packet from this client with Messag...
Definition: radius.h:64
char const * secret
Definition: radius.h:95
bool limit_proxy_state
Don't allow Proxy-State in requests.
Definition: radius.h:137
#define fr_radius_flag_encrypted(_da)
Definition: radius.h:172
bool require_message_authenticator
Definition: radius.h:136
fr_radius_ctx_t const * common
Definition: radius.h:125
fr_radius_limit_proxy_state_t
Control whether Proxy-State is allowed in Access-Requests.
Definition: radius.h:76
@ FR_RADIUS_LIMIT_PROXY_STATE_AUTO
Do not allow Proxy-State unless:
Definition: radius.h:79
@ FR_RADIUS_LIMIT_PROXY_STATE_YES
Limit Proxy-State.
Definition: radius.h:84
TALLOC_CTX * tmp_ctx
for temporary things cleaned up during decoding
Definition: radius.h:129
static rs_t * conf
Definition: radsniff.c:53
uint32_t fr_rand(void)
Return a 32-bit random number.
Definition: rand.c:106
Smaller fast random number generator.
Definition: rand.h:54
#define REQUEST_VERIFY(_x)
Definition: request.h:276
#define request_set_dynamic_client(_x)
Definition: request.h:163
static int instantiate(module_inst_ctx_t const *mctx)
Definition: rlm_rest.c:1302
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
module_instantiate_t instantiate
Callback to allow the module to register any per-instance resources like sockets and file handles.
Definition: module.h:218
module_t * exported
Public module structure.
Definition: module.h:276
Module instance data.
Definition: module.h:265
static const uchar sc[16]
Definition: smbdes.c:115
fr_client_t * client_afrom_request(TALLOC_CTX *ctx, request_t *request)
Create a new client, consuming all attributes in the control list of the request.
Definition: client.c:930
int module_instantiate(module_instance_t *instance)
Manually complete module setup by calling its instantiate function.
Definition: module.c:1195
fr_assert(0)
MEM(pair_append_request(&vp, attr_eap_aka_sim_identity) >=0)
eap_aka_sim_process_conf_t * inst
fr_pair_t * vp
Stores an attribute, a value and various bits of other data.
Definition: pair.h:68
fr_dict_attr_t const *_CONST da
Dictionary attribute defines the attribute number, vendor and type of the pair.
Definition: pair.h:69
#define talloc_get_type_abort_const
Definition: talloc.h:282
static fr_time_delta_t fr_time_delta_from_sec(int64_t sec)
Definition: time.h:590
xlat_action_t
Definition: xlat.h:35
@ XLAT_ACTION_FAIL
An xlat function failed.
Definition: xlat.h:42
@ XLAT_ACTION_DONE
We're done evaluating this level of nesting.
Definition: xlat.h:41
fr_pair_t * fr_pair_list_head(fr_pair_list_t const *list)
Get the head of a valuepair list.
Definition: pair_inline.c:43
fr_pair_t * fr_pair_list_next(fr_pair_list_t const *list, fr_pair_t const *item))
Get the next item in a valuepair list after a specific entry.
Definition: pair_inline.c:70
static fr_slen_t parent
Definition: pair.h:851
static void fr_socket_addr_swap(fr_socket_t *dst, fr_socket_t const *src)
Swap src/dst information of a fr_socket_t.
Definition: socket.h:121
int fr_value_box_memdup(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, uint8_t const *src, size_t len, bool tainted)
Copy a buffer to a fr_value_box_t.
Definition: value.c:4468
#define fr_value_box_alloc(_ctx, _type, _enumv)
Allocate a value box of a specific type.
Definition: value.h:621
#define fr_box_ipaddr(_val)
Definition: value.h:294
#define fr_box_strvalue_buffer(_val)
Definition: value.h:289
static fr_slen_t data
Definition: value.h:1265
static size_t char ** out
Definition: value.h:997
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.
An xlat calling ctx.
Definition: xlat_ctx.h:49
xlat_t * xlat_func_register(TALLOC_CTX *ctx, char const *name, xlat_func_t func, fr_type_t return_type)
Register an xlat function.
Definition: xlat_func.c:218
void xlat_func_unregister(char const *name)
Unregister an xlat function.
Definition: xlat_func.c:519