2 * listen.c Handle socket stuff
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * Copyright 2005,2006 The FreeRADIUS server project
21 * Copyright 2005 Alan DeKok <aland@ox.org>
24 #include <freeradius-devel/ident.h>
27 #include <freeradius-devel/radiusd.h>
28 #include <freeradius-devel/radius_snmp.h>
29 #include <freeradius-devel/modules.h>
30 #include <freeradius-devel/rad_assert.h>
32 #include <sys/resource.h>
38 #ifdef HAVE_SYS_STAT_H
44 static time_t start_time = 0;
47 * FIXME: Delete this crap!
49 extern time_t time_now;
52 * We'll use this below.
54 typedef int (*rad_listen_parse_t)(const char *, int, const CONF_SECTION *, rad_listen_t *);
55 typedef void (*rad_listen_free_t)(rad_listen_t *);
57 typedef struct rad_listen_master_t {
58 rad_listen_parse_t parse;
59 rad_listen_free_t free;
60 rad_listen_recv_t recv;
61 rad_listen_send_t send;
62 rad_listen_print_t print;
63 rad_listen_encode_t encode;
64 rad_listen_decode_t decode;
65 } rad_listen_master_t;
67 typedef struct listen_socket_t {
73 RADCLIENT_LIST *clients;
76 typedef struct listen_detail_t {
82 lrad_ipaddr_t client_ip;
89 * Find a per-socket client.
91 static RADCLIENT *client_listener_find(const rad_listen_t *listener,
92 const lrad_ipaddr_t *ipaddr)
94 const RADCLIENT_LIST *clients;
96 rad_assert(listener != NULL);
97 rad_assert(ipaddr != NULL);
99 rad_assert((listener->type == RAD_LISTEN_AUTH) ||
100 (listener->type == RAD_LISTEN_ACCT));
102 clients = ((listen_socket_t *)listener->data)->clients;
103 if (!clients) clients = mainconfig.clients;
105 rad_assert(clients != NULL);
107 return client_find(clients, ipaddr);
110 static int listen_bind(rad_listen_t *this);
113 * FIXME: have the detail reader use another config "exit when done",
114 * so that it can be used as a one-off tool to update stuff.
118 * Process and reply to a server-status request.
119 * Like rad_authenticate and rad_accounting this should
120 * live in it's own file but it's so small we don't bother.
122 static int rad_status_server(REQUEST *request)
124 int rcode = RLM_MODULE_OK;
127 switch (request->listener->type) {
128 case RAD_LISTEN_AUTH:
129 dval = dict_valbyname(PW_AUTZ_TYPE, "Status-Server");
131 rcode = module_authorize(dval->value, request);
133 rcode = RLM_MODULE_OK;
138 case RLM_MODULE_UPDATED:
139 request->reply->code = PW_AUTHENTICATION_ACK;
142 case RLM_MODULE_FAIL:
143 case RLM_MODULE_HANDLED:
144 request->reply->code = 0; /* don't reply */
148 case RLM_MODULE_REJECT:
149 request->reply->code = PW_AUTHENTICATION_REJECT;
154 case RAD_LISTEN_ACCT:
155 dval = dict_valbyname(PW_ACCT_TYPE, "Status-Server");
157 rcode = module_accounting(dval->value, request);
159 rcode = RLM_MODULE_OK;
164 case RLM_MODULE_UPDATED:
165 request->reply->code = PW_ACCOUNTING_RESPONSE;
169 request->reply->code = 0; /* don't reply */
182 static int socket_print(rad_listen_t *this, char *buffer, size_t bufsize)
185 listen_socket_t *sock = this->data;
187 if ((sock->ipaddr.af == AF_INET) &&
188 (sock->ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
191 ip_ntoh(&sock->ipaddr, buffer, bufsize);
194 len = strlen(buffer);
196 return len + snprintf(buffer + len, bufsize - len, " port %d",
202 * Parse an authentication or accounting socket.
204 static int common_socket_parse(const char *filename, int lineno,
205 const CONF_SECTION *cs, rad_listen_t *this)
209 lrad_ipaddr_t ipaddr;
210 listen_socket_t *sock = this->data;
211 const char *section_name = NULL;
212 CONF_SECTION *client_cs;
217 ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
218 rcode = cf_item_parse(cs, "ipaddr", PW_TYPE_IPADDR,
219 &ipaddr.ipaddr.ip4addr, NULL);
220 if (rcode < 0) return -1;
222 if (rcode == 0) { /* successfully parsed IPv4 */
225 } else { /* maybe IPv6? */
226 rcode = cf_item_parse(cs, "ipv6addr", PW_TYPE_IPV6ADDR,
227 &ipaddr.ipaddr.ip6addr, NULL);
228 if (rcode < 0) return -1;
231 radlog(L_ERR, "%s[%d]: No address specified in listen section",
235 ipaddr.af = AF_INET6;
238 rcode = cf_item_parse(cs, "port", PW_TYPE_INTEGER,
240 if (rcode < 0) return -1;
242 sock->ipaddr = ipaddr;
243 sock->port = listen_port;
246 * And bind it to the port.
248 if (listen_bind(this) < 0) {
250 radlog(L_CONS|L_ERR, "%s[%d]: Error binding to port for %s port %d",
251 filename, cf_section_lineno(cs),
252 ip_ntoh(&sock->ipaddr, buffer, sizeof(buffer)),
258 * If we can bind to interfaces, do so,
261 if (cf_pair_find(cs, "interface")) {
262 #ifndef SO_BINDTODEVICE
263 radlog(L_CONS|L_ERR, "%s[%d]: System does not support binding to interfaces, delete this line from the configuration file.",
264 filename, cf_section_lineno(cs));
268 const CONF_PAIR *cp = cf_pair_find(cs, "interface");
271 rad_assert(cp != NULL);
272 value = cf_pair_value(cp);
273 rad_assert(value != NULL);
275 strcpy(ifreq.ifr_name, value);
277 if (setsockopt(this->fd, SOL_SOCKET, SO_BINDTODEVICE,
278 (char *)&ifreq, sizeof(ifreq)) < 0) {
279 radlog(L_CONS|L_ERR, "%s[%d]: Failed binding to interface %s: %s",
280 filename, cf_section_lineno(cs),
281 value, strerror(errno));
283 } /* else it worked. */
288 * Look for the name of a section that holds a list
291 rcode = cf_item_parse(cs, "clients", PW_TYPE_STRING_PTR,
292 §ion_name, NULL);
293 if (rcode < 0) return -1; /* bad string */
294 if (rcode > 0) return 0; /* non-existent is OK. */
296 client_cs = cf_section_find(section_name);
299 radlog(L_CONS|L_ERR, "%s[%d]: Failed to find client section %s",
300 filename, cf_section_lineno(cs), section_name);
304 sock->clients = clients_parse_section(filename, client_cs);
305 if (!sock->clients) {
313 * Send an authentication response packet
315 static int auth_socket_send(rad_listen_t *listener, REQUEST *request)
317 rad_assert(request->listener == listener);
318 rad_assert(listener->send == auth_socket_send);
320 return rad_send(request->reply, request->packet,
321 request->client->secret);
326 * Send an accounting response packet (or not)
328 static int acct_socket_send(rad_listen_t *listener, REQUEST *request)
330 rad_assert(request->listener == listener);
331 rad_assert(listener->send == acct_socket_send);
334 * Accounting reject's are silently dropped.
336 * We do it here to avoid polluting the rest of the
337 * code with this knowledge
339 if (request->reply->code == 0) return 0;
341 return rad_send(request->reply, request->packet,
342 request->client->secret);
347 * Send a packet to a home server.
349 * FIXME: have different code for proxy auth & acct!
351 static int proxy_socket_send(rad_listen_t *listener, REQUEST *request)
353 listen_socket_t *sock = listener->data;
355 rad_assert(request->proxy_listener == listener);
356 rad_assert(listener->send == proxy_socket_send);
358 request->proxy->src_ipaddr = sock->ipaddr;
359 request->proxy->src_port = sock->port;
361 return rad_send(request->proxy, request->packet,
362 request->home_server->secret);
367 * Check if an incoming request is "ok"
369 * It takes packets, not requests. It sees if the packet looks
370 * OK. If so, it does a number of sanity checks on it.
372 static int auth_socket_recv(rad_listen_t *listener,
373 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
377 RADIUS_PACKET *packet;
378 RAD_REQUEST_FUNP fun = NULL;
381 lrad_ipaddr_t src_ipaddr;
383 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
384 if (rcode < 0) return 0;
386 RAD_SNMP_TYPE_INC(listener, total_requests);
388 if (rcode < 20) { /* AUTH_HDR_LEN */
389 RAD_SNMP_TYPE_INC(listener, total_malformed_requests);
393 if ((client = client_listener_find(listener,
394 &src_ipaddr)) == NULL) {
395 rad_recv_discard(listener->fd);
396 RAD_SNMP_TYPE_INC(listener, total_invalid_requests);
399 * This is debugging rather than logging, so that
400 * DoS attacks don't affect us.
402 DEBUG("Ignoring request from unknown client %s port %d",
403 inet_ntop(src_ipaddr.af, &src_ipaddr.ipaddr,
404 buffer, sizeof(buffer)), src_port);
409 * Some sanity checks, based on the packet code.
412 case PW_AUTHENTICATION_REQUEST:
413 RAD_SNMP_CLIENT_INC(listener, client, requests);
414 fun = rad_authenticate;
417 case PW_STATUS_SERVER:
418 if (!mainconfig.status_server) {
419 rad_recv_discard(listener->fd);
420 RAD_SNMP_TYPE_INC(listener, total_packets_dropped);
421 RAD_SNMP_CLIENT_INC(listener, client, packets_dropped);
422 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
425 fun = rad_status_server;
429 rad_recv_discard(listener->fd);
430 RAD_SNMP_INC(rad_snmp.auth.total_unknown_types);
431 RAD_SNMP_CLIENT_INC(listener, client, unknown_types);
433 DEBUG("Invalid packet code %d sent to authentication port from client %s port %d : IGNORED",
434 code, client->shortname, src_port);
437 } /* switch over packet types */
440 * Now that we've sanity checked everything, receive the
443 packet = rad_recv(listener->fd);
445 RAD_SNMP_TYPE_INC(listener, total_malformed_requests);
446 radlog(L_ERR, "%s", librad_errstr);
450 if (!received_request(listener, packet, prequest, client)) {
451 RAD_SNMP_TYPE_INC(listener, total_packets_dropped);
452 RAD_SNMP_CLIENT_INC(listener, client, packets_dropped);
463 * Receive packets from an accounting socket
465 static int acct_socket_recv(rad_listen_t *listener,
466 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
470 RADIUS_PACKET *packet;
471 RAD_REQUEST_FUNP fun = NULL;
474 lrad_ipaddr_t src_ipaddr;
476 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
477 if (rcode < 0) return 0;
479 RAD_SNMP_TYPE_INC(listener, total_requests);
481 if (rcode < 20) { /* AUTH_HDR_LEN */
482 RAD_SNMP_TYPE_INC(listener, total_malformed_requests);
486 if ((client = client_listener_find(listener,
487 &src_ipaddr)) == NULL) {
488 rad_recv_discard(listener->fd);
489 RAD_SNMP_TYPE_INC(listener, total_invalid_requests);
492 * This is debugging rather than logging, so that
493 * DoS attacks don't affect us.
495 DEBUG("Ignoring request from unknown client %s port %d",
496 inet_ntop(src_ipaddr.af, &src_ipaddr.ipaddr,
497 buffer, sizeof(buffer)), src_port);
502 * Some sanity checks, based on the packet code.
505 case PW_ACCOUNTING_REQUEST:
506 RAD_SNMP_CLIENT_INC(listener, client, requests);
507 fun = rad_accounting;
510 case PW_STATUS_SERVER:
511 if (!mainconfig.status_server) {
512 rad_recv_discard(listener->fd);
513 RAD_SNMP_TYPE_INC(listener, total_packets_dropped);
514 RAD_SNMP_CLIENT_INC(listener, client, unknown_types);
516 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
519 fun = rad_status_server;
523 rad_recv_discard(listener->fd);
524 RAD_SNMP_TYPE_INC(listener, total_unknown_types);
525 RAD_SNMP_CLIENT_INC(listener, client, unknown_types);
527 DEBUG("Invalid packet code %d sent to a accounting port from client %s port %d : IGNORED",
528 code, client->shortname, src_port);
530 } /* switch over packet types */
533 * Now that we've sanity checked everything, receive the
536 packet = rad_recv(listener->fd);
538 RAD_SNMP_TYPE_INC(listener, total_malformed_requests);
539 radlog(L_ERR, "%s", librad_errstr);
544 * There can be no duplicate accounting packets.
546 if (!received_request(listener, packet, prequest, client)) {
547 RAD_SNMP_TYPE_INC(listener, total_packets_dropped);
548 RAD_SNMP_CLIENT_INC(listener, client, packets_dropped);
559 * Recieve packets from a proxy socket.
561 static int proxy_socket_recv(rad_listen_t *listener,
562 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
565 RADIUS_PACKET *packet;
566 RAD_REQUEST_FUNP fun = NULL;
569 packet = rad_recv(listener->fd);
571 radlog(L_ERR, "%s", librad_errstr);
576 * FIXME: Client MIB updates?
578 switch(packet->code) {
579 case PW_AUTHENTICATION_ACK:
580 case PW_ACCESS_CHALLENGE:
581 case PW_AUTHENTICATION_REJECT:
582 fun = rad_authenticate;
585 case PW_ACCOUNTING_RESPONSE:
586 fun = rad_accounting;
591 * FIXME: Update MIB for packet types?
593 radlog(L_ERR, "Invalid packet code %d sent to a proxy port "
594 "from home server %s port %d - ID %d : IGNORED",
596 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
597 packet->src_port, packet->id);
602 request = received_proxy_response(packet);
607 rad_assert(fun != NULL);
615 static int client_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
617 if (!request->reply->code) return 0;
619 rad_encode(request->reply, request->packet,
620 request->client->secret);
621 rad_sign(request->reply, request->packet,
622 request->client->secret);
628 static int client_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
630 if (rad_verify(request->packet, NULL,
631 request->client->secret) < 0) {
635 return rad_decode(request->packet, NULL,
636 request->client->secret);
639 static int proxy_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
641 rad_encode(request->proxy, NULL, request->home_server->secret);
642 rad_sign(request->proxy, NULL, request->home_server->secret);
648 static int proxy_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
650 if (rad_verify(request->proxy_reply, request->proxy,
651 request->home_server->secret) < 0) {
655 return rad_decode(request->proxy_reply, request->proxy,
656 request->home_server->secret);
660 #define STATE_UNOPENED (0)
661 #define STATE_UNLOCKED (1)
662 #define STATE_HEADER (2)
663 #define STATE_READING (3)
664 #define STATE_DONE (4)
665 #define STATE_WAITING (5)
668 * If we're limiting outstanding packets, then mark the response
671 static int detail_send(rad_listen_t *listener, REQUEST *request)
673 listen_detail_t *data = listener->data;
675 rad_assert(request->listener == listener);
676 rad_assert(listener->send == detail_send);
678 if (request->simul_max >= 0) {
679 rad_assert(data->outstanding != NULL);
680 rad_assert(request->simul_max < data->max_outstanding);
682 data->outstanding[request->simul_max] = 0;
690 * Open the detail file..
692 * FIXME: create it, if it's not already there, so that the main
693 * server select() will wake us up if there's anything to read.
695 static int detail_open(rad_listen_t *this)
699 listen_detail_t *data = this->data;
701 rad_assert(data->state == STATE_UNOPENED);
702 snprintf(buffer, sizeof(buffer), "%s.work", data->filename);
705 * FIXME: Have "one-shot" configuration, where it
706 * will read the detail file, and exit once it's
709 * FIXME: Try harder to open the detail file.
710 * Maybe sleep for X usecs if it doesn't exist?
714 * Open detail.work first, so we don't lose
715 * accounting packets. It's probably better to
716 * duplicate them than to lose them.
718 * Note that we're not writing to the file, but
719 * we've got to open it for writing in order to
720 * establish the lock, to prevent rlm_detail from
723 this->fd = open(buffer, O_RDWR);
726 * Try reading the detail file. If it
727 * doesn't exist, we can't do anything.
729 * Doing the stat will tell us if the file
730 * exists, even if we don't have permissions
733 if (stat(data->filename, &st) < 0) {
738 * Open it BEFORE we rename it, just to
741 this->fd = open(data->filename, O_RDWR);
743 radlog(L_ERR, "Failed to open %s: %s",
744 data->filename, strerror(errno));
749 * Rename detail to detail.work
751 if (rename(data->filename, buffer) < 0) {
756 } /* else detail.work existed, and we opened it */
758 rad_assert(data->vps == NULL);
760 rad_assert(data->fp == NULL);
761 data->fp = fdopen(this->fd, "r");
763 radlog(L_ERR, "Failed to re-open %s: %s",
764 data->filename, strerror(errno));
768 data->state = STATE_UNLOCKED;
770 data->client_ip.af = AF_UNSPEC;
777 * This is a bad hack, just so complaints have meaningful text.
779 static const RADCLIENT detail_client = {
794 static int detail_recv(rad_listen_t *listener,
795 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
798 char key[256], value[1024];
799 VALUE_PAIR *vp, **tail;
800 RADIUS_PACKET *packet;
802 listen_detail_t *data = listener->data;
804 if (data->state == STATE_UNOPENED) {
805 rad_assert(listener->fd < 0);
808 * FIXME: 'stat' the detail file. If it doesn't
809 * exist, then return "sleep for 1s", to avoid
812 if (!detail_open(listener)) return 0;
814 rad_assert(listener->fd >= 0);
817 * Try to lock fd. If we can't, return. If we can,
818 * continue. This means that the server doesn't block
819 * while waiting for the lock to open...
821 if (data->state == STATE_UNLOCKED) {
823 * Note that we do NOT block waiting for the
824 * lock. We've re-named the file above, so we've
825 * already guaranteed that any *new* detail
826 * writer will not be opening this file. The
827 * only purpose of the lock is to catch a race
828 * condition where the execution "ping-pongs"
829 * between radiusd & radrelay.
831 if (rad_lockfd_nonblock(listener->fd, 0) < 0) {
835 * Look for the header
837 data->state = STATE_HEADER;
841 * Catch an out of memory condition which will most likely
844 if (data->state == STATE_DONE) goto alloc_packet;
847 * We read the last packet, and returned it for
848 * processing. We later come back here to shut
849 * everything down, and unlink the file.
851 if (feof(data->fp)) {
852 if (data->state == STATE_READING) {
853 data->state = STATE_DONE;
858 * Don't unlink the file until we've received
859 * all of the responses.
861 if (data->max_outstanding > 0) {
864 for (i = 0; i < data->max_outstanding; i++) {
866 * FIXME: close the file?
868 if (data->outstanding[i]) {
869 data->state = STATE_WAITING;
876 rad_assert(data->vps == NULL);
878 snprintf(buffer, sizeof(buffer), "%s.work", data->filename);
880 fclose(data->fp); /* closes listener->fd */
883 data->state = STATE_UNOPENED;
886 * Try to open "detail" again. If we're on a
887 * busy RADIUS server, odds are that it will
890 detail_open(listener);
899 while (fgets(buffer, sizeof(buffer), data->fp)) {
903 if (!strchr(buffer, '\n')) {
904 pairfree(&data->vps);
909 * We've read a header, possibly packet contents,
910 * and are now at the end of the packet.
912 if ((data->state == STATE_READING) &&
913 (buffer[0] == '\n')) {
914 data->state = STATE_DONE;
919 * Look for date/time header, and read VP's if
920 * found. If not, keep reading lines until we
923 if (data->state == STATE_HEADER) {
926 if (sscanf(buffer, "%*s %*s %*d %*d:%*d:%*d %d", &y)) {
927 data->state = STATE_READING;
933 * We have a full "attribute = value" line.
934 * If it doesn't look reasonable, skip it.
936 if (sscanf(buffer, "%255s = %1023s", key, value) != 2) {
941 * Skip non-protocol attributes.
943 if (!strcasecmp(key, "Request-Authenticator")) continue;
946 * Set the original client IP address, based on
947 * what's in the detail file.
949 * Hmm... we don't set the server IP address.
952 if (!strcasecmp(key, "Client-IP-Address")) {
953 data->client_ip.af = AF_INET;
954 ip_hton(value, AF_INET, &data->client_ip);
959 * The original time at which we received the
960 * packet. We need this to properly calculate
963 if (!strcasecmp(key, "Timestamp")) {
964 data->timestamp = atoi(value);
971 * FIXME: do we want to check for non-protocol
972 * attributes like radsqlrelay does?
975 if ((userparse(buffer, &vp) > 0) &&
983 * We got to EOF, If we're in STATE_HEADER, it's OK.
984 * Otherwise it's a problem. In any case, nuke the file
985 * and start over from scratch,
987 if (feof(data->fp)) {
991 if (data->state == STATE_READING) goto alloc_packet;
992 pairfree(&data->vps);
997 * If we're not done, then there's a problem. The checks
1000 rad_assert(data->state == STATE_DONE);
1003 * The packet we read was empty, re-set the state to look
1004 * for a header, and don't return anything.
1007 data->state = STATE_HEADER;
1012 * Allocate the packet. If we fail, it's a serious
1017 * If we keep track of the outstanding requests, do so
1018 * here. Note that to minimize potential work, we do
1019 * so only once the file is opened & locked.
1021 if (data->max_outstanding) {
1024 for (i = 0; i < data->max_outstanding; i++) {
1025 if (!data->outstanding[i]) {
1032 * All of the slots are full. Put the LF back,
1033 * so select will say that data is ready, and set
1034 * the state back to reading. Then, return so
1035 * that we don't overload the server.
1037 if (free_slot < 0) {
1038 ungetc('\n', data->fp);
1039 data->state = STATE_READING;
1044 packet = rad_alloc(1);
1046 return 0; /* maybe memory will magically free up... */
1049 memset(packet, 0, sizeof(*packet));
1050 packet->sockfd = -1;
1051 packet->src_ipaddr.af = AF_INET;
1052 packet->src_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
1053 packet->code = PW_ACCOUNTING_REQUEST;
1054 packet->timestamp = time(NULL);
1057 * Remember where it came from, so that we don't
1058 * proxy it to the place it came from...
1060 if (data->client_ip.af != AF_UNSPEC) {
1061 packet->src_ipaddr = data->client_ip;
1064 vp = pairfind(packet->vps, PW_PACKET_SRC_IP_ADDRESS);
1066 packet->src_ipaddr.af = AF_INET;
1067 packet->src_ipaddr.ipaddr.ip4addr.s_addr = vp->vp_ipaddr;
1069 vp = pairfind(packet->vps, PW_PACKET_SRC_IPV6_ADDRESS);
1071 packet->src_ipaddr.af = AF_INET6;
1072 memcpy(&packet->src_ipaddr.ipaddr.ip6addr,
1073 &vp->vp_ipv6addr, sizeof(vp->vp_ipv6addr));
1077 vp = pairfind(packet->vps, PW_PACKET_DST_IP_ADDRESS);
1079 packet->dst_ipaddr.af = AF_INET;
1080 packet->dst_ipaddr.ipaddr.ip4addr.s_addr = vp->vp_ipaddr;
1082 vp = pairfind(packet->vps, PW_PACKET_DST_IPV6_ADDRESS);
1084 packet->dst_ipaddr.af = AF_INET6;
1085 memcpy(&packet->dst_ipaddr.ipaddr.ip6addr,
1086 &vp->vp_ipv6addr, sizeof(vp->vp_ipv6addr));
1091 * We've got to give SOME value for Id & ports, so that
1092 * the packets can be added to the request queue.
1093 * However, we don't want to keep track of used/unused
1094 * id's and ports, as that's a lot of work. This hack
1095 * ensures that (if we have real random numbers), that
1096 * there will be a collision on every 2^(16+15+15+24 - 1)
1097 * packets, on average. That means we can read 2^37
1098 * packets before having a collision, which means it's
1099 * effectively impossible.
1101 packet->id = lrad_rand() & 0xffff;
1102 packet->src_port = 1024 + (lrad_rand() & 0x7fff);
1103 packet->dst_port = 1024 + (lrad_rand() & 0x7fff);
1105 packet->dst_ipaddr.af = AF_INET;
1106 packet->dst_ipaddr.ipaddr.ip4addr.s_addr = htonl((INADDR_LOOPBACK & ~0xffffff) | (lrad_rand() & 0xffffff));
1108 packet->vps = data->vps;
1114 data->state = STATE_HEADER;
1117 * Look for Acct-Delay-Time, and update
1118 * based on Acct-Delay-Time += (time(NULL) - timestamp)
1120 vp = pairfind(packet->vps, PW_ACCT_DELAY_TIME);
1122 vp = paircreate(PW_ACCT_DELAY_TIME, PW_TYPE_INTEGER);
1123 rad_assert(vp != NULL);
1124 pairadd(&packet->vps, vp);
1126 if (data->timestamp != 0) {
1127 vp->vp_integer += time(NULL) - data->timestamp;
1131 * Keep track of free slots, as a hack, in an otherwise
1134 (*prequest)->simul_max = free_slot;
1135 data->outstanding[free_slot] = 1;
1137 *pfun = rad_accounting;
1140 printf("detail_recv: Read packet from %s\n", data->filename);
1141 for (vp = packet->vps; vp; vp = vp->next) {
1143 vp_print(stdout, vp);
1149 * FIXME: many of these checks may not be necessary when
1150 * reading from the detail file.
1152 if (!received_request(listener, packet, prequest, &detail_client)) {
1162 * Free detail-specific stuff.
1164 static void detail_free(rad_listen_t *this)
1166 listen_detail_t *data = this->data;
1168 free(data->filename);
1169 pairfree(&data->vps);
1170 free(data->outstanding);
1172 if (data->fp != NULL) fclose(data->fp);
1176 static int detail_print(rad_listen_t *this, char *buffer, size_t bufsize)
1178 return snprintf(buffer, bufsize, "%s",
1179 ((listen_detail_t *)(this->data))->filename);
1182 static int detail_encode(UNUSED rad_listen_t *this, UNUSED REQUEST *request)
1185 * We never encode responses "sent to" the detail file.
1190 static int detail_decode(UNUSED rad_listen_t *this, UNUSED REQUEST *request)
1193 * We never decode responses read from the detail file.
1199 static const CONF_PARSER detail_config[] = {
1200 { "filename", PW_TYPE_STRING_PTR,
1201 offsetof(listen_detail_t, filename), NULL, NULL },
1202 { "max_outstanding", PW_TYPE_INTEGER,
1203 offsetof(listen_detail_t, max_outstanding), NULL, "100" },
1205 { NULL, -1, 0, NULL, NULL } /* end the list */
1210 * Parse a detail section.
1212 static int detail_parse(const char *filename, int lineno,
1213 const CONF_SECTION *cs, rad_listen_t *this)
1216 listen_detail_t *data;
1220 rcode = cf_section_parse(cs, data, detail_config);
1222 radlog(L_ERR, "%s[%d]: Failed parsing listen section",
1227 if (!data->filename) {
1228 radlog(L_ERR, "%s[%d]: No detail file specified in listen section",
1235 data->state = STATE_UNOPENED;
1237 if (data->max_outstanding > 32768) data->max_outstanding = 32768;
1239 if (data->max_outstanding > 0) {
1240 data->outstanding = rad_malloc(sizeof(int) * data->max_outstanding);
1250 static int radius_snmp_recv(rad_listen_t *listener,
1251 UNUSED RAD_REQUEST_FUNP *pfun,
1252 UNUSED REQUEST **prequest)
1254 if (!mainconfig.do_snmp) return 0;
1256 if ((rad_snmp.smux_fd >= 0) &&
1257 (rad_snmp.smux_event == SMUX_READ)) {
1262 * If we've got to re-connect, then do so now,
1263 * before calling select again.
1265 if (rad_snmp.smux_event == SMUX_CONNECT) {
1270 * Reset this every time, as the smux connect may have
1271 * opened a new socket.
1273 listener->fd = rad_snmp.smux_fd;
1279 static int radius_snmp_print(rad_listen_t *this, char *buffer, size_t bufsize)
1281 return snprintf(buffer, bufsize, "SMUX with OID .1.3.6.1.4.1.11344.1.1.1");
1286 static const rad_listen_master_t master_listen[RAD_LISTEN_MAX] = {
1287 { NULL, NULL, NULL, NULL, NULL, NULL, NULL}, /* RAD_LISTEN_NONE */
1291 proxy_socket_recv, proxy_socket_send,
1292 socket_print, proxy_socket_encode, proxy_socket_decode },
1294 /* authentication */
1295 { common_socket_parse, NULL,
1296 auth_socket_recv, auth_socket_send,
1297 socket_print, client_socket_encode, client_socket_decode },
1300 { common_socket_parse, NULL,
1301 acct_socket_recv, acct_socket_send,
1302 socket_print, client_socket_encode, client_socket_decode},
1305 { detail_parse, detail_free,
1306 detail_recv, detail_send,
1307 detail_print, detail_encode, detail_decode },
1309 { NULL, NULL, NULL, NULL, NULL, NULL, NULL}, /* RAD_LISTEN_SNMP */
1315 * Binds a listener to a socket.
1317 static int listen_bind(rad_listen_t *this)
1319 rad_listen_t **last;
1320 listen_socket_t *sock = this->data;
1323 * If the port is zero, then it means the appropriate
1324 * thing from /etc/services.
1326 if (sock->port == 0) {
1327 struct servent *svp;
1329 switch (this->type) {
1330 case RAD_LISTEN_AUTH:
1331 svp = getservbyname ("radius", "udp");
1333 sock->port = ntohs(svp->s_port);
1335 sock->port = PW_AUTH_UDP_PORT;
1339 case RAD_LISTEN_ACCT:
1340 svp = getservbyname ("radacct", "udp");
1342 sock->port = ntohs(svp->s_port);
1344 sock->port = PW_ACCT_UDP_PORT;
1349 radlog(L_ERR|L_CONS, "ERROR: Non-fatal internal sanity check failed in bind.");
1355 * Find it in the old list, AFTER updating the port. If
1356 * it's there, use that, rather than creating a new
1357 * socket. This allows HUP's to re-use the old sockets,
1358 * which means that packets waiting in the socket queue
1361 for (last = &mainconfig.listen;
1363 last = &((*last)->next)) {
1364 listen_socket_t *other;
1366 if (this->type != (*last)->type) continue;
1368 if ((this->type == RAD_LISTEN_DETAIL) ||
1369 (this->type == RAD_LISTEN_SNMP)) continue;
1371 other = (listen_socket_t *)((*last)->data);
1373 if ((sock->port == other->port) &&
1374 (sock->ipaddr.af == other->ipaddr.af) &&
1375 (lrad_ipaddr_cmp(&sock->ipaddr, &other->ipaddr) == 0)) {
1376 this->fd = (*last)->fd;
1382 this->fd = lrad_socket(&sock->ipaddr, sock->port);
1384 radlog(L_ERR|L_CONS, "ERROR: Failed to open socket: %s",
1391 if ((flags = fcntl(this->fd, F_GETFL, NULL)) < 0) {
1392 radlog(L_ERR, "Failure in fcntl: %s)\n", strerror(errno));
1396 flags |= O_NONBLOCK;
1397 if( fcntl(this->fd, F_SETFL, flags) < 0) {
1398 radlog(L_ERR, "Failure in fcntl: %s)\n", strerror(errno));
1409 * Allocate & initialize a new listener.
1411 static rad_listen_t *listen_alloc(RAD_LISTEN_TYPE type)
1415 this = rad_malloc(sizeof(*this));
1416 memset(this, 0, sizeof(*this));
1419 this->recv = master_listen[this->type].recv;
1420 this->send = master_listen[this->type].send;
1421 this->print = master_listen[this->type].print;
1422 this->encode = master_listen[this->type].encode;
1423 this->decode = master_listen[this->type].decode;
1426 case RAD_LISTEN_AUTH:
1427 case RAD_LISTEN_ACCT:
1428 case RAD_LISTEN_PROXY:
1429 this->data = rad_malloc(sizeof(listen_socket_t));
1430 memset(this->data, 0, sizeof(listen_socket_t));
1433 case RAD_LISTEN_DETAIL:
1434 this->data = rad_malloc(sizeof(listen_detail_t));
1435 memset(this->data, 0, sizeof(listen_detail_t));
1446 * Externally visible function for creating a new proxy LISTENER.
1448 * For now, don't take ipaddr or port.
1450 * Not thread-safe, but all calls to it are protected by the
1451 * proxy mutex in request_list.c
1453 rad_listen_t *proxy_new_listener()
1455 int last_proxy_port, port;
1456 rad_listen_t *this, *tmp, **last;
1457 listen_socket_t *sock, *old;
1459 this = listen_alloc(RAD_LISTEN_PROXY);
1462 * Find an existing proxy socket to copy.
1464 * FIXME: Make it per-realm, or per-home server!
1466 last_proxy_port = 0;
1468 last = &mainconfig.listen;
1469 for (tmp = mainconfig.listen; tmp != NULL; tmp = tmp->next) {
1470 if (tmp->type == RAD_LISTEN_PROXY) {
1472 if (sock->port > last_proxy_port) {
1473 last_proxy_port = sock->port + 1;
1475 if (!old) old = sock;
1478 last = &(tmp->next);
1481 if (!old) return NULL; /* This is a serious error. */
1484 * FIXME: find a new IP address to listen on?
1487 memcpy(&sock->ipaddr, &old->ipaddr, sizeof(sock->ipaddr));
1490 * Keep going until we find an unused port.
1492 for (port = last_proxy_port; port < 64000; port++) {
1494 if (listen_bind(this) == 0) {
1496 * Add the new listener to the list of
1508 static const LRAD_NAME_NUMBER listen_compare[] = {
1509 { "auth", RAD_LISTEN_AUTH },
1510 { "acct", RAD_LISTEN_ACCT },
1511 { "detail", RAD_LISTEN_DETAIL },
1517 * Generate a list of listeners. Takes an input list of
1518 * listeners, too, so we don't close sockets with waiting packets.
1520 int listen_init(const char *filename, rad_listen_t **head)
1524 rad_listen_t **last;
1526 lrad_ipaddr_t server_ipaddr;
1530 * We shouldn't be called with a pre-existing list.
1532 rad_assert(head && (*head == NULL));
1534 if (start_time != 0) start_time = time(NULL);
1537 server_ipaddr.af = AF_UNSPEC;
1540 * If the port is specified on the command-line,
1541 * it over-rides the configuration file.
1543 if (mainconfig.port >= 0) {
1544 auth_port = mainconfig.port;
1546 rcode = cf_item_parse(mainconfig.config, "port",
1547 PW_TYPE_INTEGER, &auth_port,
1548 Stringify(PW_AUTH_UDP_PORT));
1549 if (rcode < 0) return -1; /* error parsing it */
1552 radlog(L_INFO, "WARNING: The directive 'port' is deprecated, and will be removed in future versions of FreeRADIUS. Please edit the configuration files to use the directive 'listen'.");
1556 * If the IP address was configured on the command-line,
1557 * use that as the "bind_address"
1559 if (mainconfig.myip.af != AF_UNSPEC) {
1560 memcpy(&server_ipaddr, &mainconfig.myip,
1561 sizeof(server_ipaddr));
1566 * Else look for bind_address and/or listen sections.
1568 server_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
1569 rcode = cf_item_parse(mainconfig.config, "bind_address",
1571 &server_ipaddr.ipaddr.ip4addr, NULL);
1572 if (rcode < 0) return -1; /* error parsing it */
1574 if (rcode == 0) { /* successfully parsed IPv4 */
1575 listen_socket_t *sock;
1576 server_ipaddr.af = AF_INET;
1578 radlog(L_INFO, "WARNING: The directive 'bind_adress' is deprecated, and will be removed in future versions of FreeRADIUS. Please edit the configuration files to use the directive 'listen'.");
1581 this = listen_alloc(RAD_LISTEN_AUTH);
1584 sock->ipaddr = server_ipaddr;
1585 sock->port = auth_port;
1587 if (listen_bind(this) < 0) {
1590 radlog(L_CONS|L_ERR, "There appears to be another RADIUS server running on the authentication port %d", sock->port);
1593 auth_port = sock->port; /* may have been updated in listen_bind */
1595 last = &(this->next);
1598 * Open Accounting Socket.
1600 * If we haven't already gotten acct_port from
1601 * /etc/services, then make it auth_port + 1.
1603 this = listen_alloc(RAD_LISTEN_ACCT);
1607 * Create the accounting socket.
1609 * The accounting port is always the
1610 * authentication port + 1
1612 sock->ipaddr = server_ipaddr;
1613 sock->port = auth_port + 1;
1615 if (listen_bind(this) < 0) {
1618 radlog(L_CONS|L_ERR, "There appears to be another RADIUS server running on the accounting port %d", sock->port);
1623 last = &(this->next);
1625 } else if (mainconfig.port > 0) { /* no bind address, but a port */
1626 radlog(L_CONS|L_ERR, "The command-line says \"-p %d\", but there is no associated IP address to use",
1632 * They specified an IP on the command-line, ignore
1633 * all listen sections.
1635 if (mainconfig.myip.af != AF_UNSPEC) goto do_proxy;
1638 * Walk through the "listen" sections, if they exist.
1640 for (cs = cf_subsection_find_next(mainconfig.config, NULL, "listen");
1642 cs = cf_subsection_find_next(mainconfig.config, cs, "listen")) {
1644 char *listen_type, *identity;
1645 int lineno = cf_section_lineno(cs);
1647 listen_type = identity = NULL;
1649 DEBUG2(" listen {");
1651 rcode = cf_item_parse(cs, "type", PW_TYPE_STRING_PTR,
1653 if (rcode < 0) return -1;
1657 radlog(L_ERR, "%s[%d]: No type specified in listen section",
1663 * See if there's an identity.
1665 rcode = cf_item_parse(cs, "identity", PW_TYPE_STRING_PTR,
1673 type = lrad_str2int(listen_compare, listen_type,
1676 if (type == RAD_LISTEN_NONE) {
1678 radlog(L_CONS|L_ERR, "%s[%d]: Invalid type in listen section.",
1684 * Set up cross-type data.
1686 this = listen_alloc(type);
1687 this->identity = identity;
1691 * Call per-type parser.
1693 if (master_listen[type].parse(filename, lineno,
1703 last = &(this->next);
1707 * If we're proxying requests, open the proxy FD.
1708 * Otherwise, don't do anything.
1711 if (mainconfig.proxy_requests == TRUE) {
1713 listen_socket_t *sock = NULL;
1716 * No sockets to receive packets, therefore
1717 * proxying is pointless.
1719 if (!*head) return -1;
1722 * If we previously had proxy sockets, copy them
1723 * to the new config.
1725 if (mainconfig.listen != NULL) {
1726 rad_listen_t *old, *next, **tail;
1728 tail = &mainconfig.listen;
1729 for (old = mainconfig.listen;
1734 if (old->type != RAD_LISTEN_PROXY) {
1735 tail = &((*tail)->next);
1742 last = &(old->next);
1749 * Find the first authentication port,
1752 for (this = *head; this != NULL; this = this->next) {
1753 if (this->type == RAD_LISTEN_AUTH) {
1755 if (server_ipaddr.af == AF_UNSPEC) {
1756 server_ipaddr = sock->ipaddr;
1758 port = sock->port + 2; /* skip acct port */
1763 if (port < 0) port = 1024 + (lrad_rand() & 0x1ff);
1766 * Address is still unspecified, use IPv4.
1768 if (server_ipaddr.af == AF_UNSPEC) {
1769 server_ipaddr.af = AF_INET;
1770 server_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_ANY);
1773 this = listen_alloc(RAD_LISTEN_PROXY);
1777 * Create the first proxy socket.
1779 sock->ipaddr = server_ipaddr;
1782 * Try to find a proxy port (value doesn't matter)
1784 for (sock->port = port;
1787 if (listen_bind(this) == 0) {
1789 last = &(this->next); /* just in case */
1794 if (sock->port >= 64000) {
1797 radlog(L_ERR|L_CONS, "Failed to open socket for proxying");
1804 if (mainconfig.do_snmp) {
1808 * Forget about the old one.
1810 for (this = mainconfig.listen;
1812 this = this->next) {
1813 if (this->type != RAD_LISTEN_SNMP) continue;
1817 this = rad_malloc(sizeof(*this));
1818 memset(this, 0, sizeof(*this));
1820 this->type = RAD_LISTEN_SNMP;
1821 this->fd = rad_snmp.smux_fd;
1823 this->recv = radius_snmp_recv;
1824 this->print = radius_snmp_print;
1827 last = &(this->next);
1832 * Sanity check the configuration.
1835 if (mainconfig.listen != NULL) for (this = *head;
1837 this = this->next) {
1838 if (((this->type == RAD_LISTEN_ACCT) &&
1839 (rcode == RAD_LISTEN_DETAIL)) ||
1840 ((this->type == RAD_LISTEN_DETAIL) &&
1841 (rcode == RAD_LISTEN_ACCT))) {
1842 rad_assert(0 == 1); /* FIXME: configuration error */
1845 if (rcode != 0) continue;
1847 if ((this->type == RAD_LISTEN_ACCT) ||
1848 (this->type == RAD_LISTEN_DETAIL)) {
1858 * Free a linked list of listeners;
1860 void listen_free(rad_listen_t **head)
1864 if (!head || !*head) return;
1868 rad_listen_t *next = this->next;
1870 free(this->identity);
1873 * Other code may have eaten the FD.
1875 if (this->fd >= 0) close(this->fd);
1877 if (master_listen[this->type].free) {
1878 master_listen[this->type].free(this);