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_SNMP_TYPE_INC(listener, total_invalid_requests);
398 * This is debugging rather than logging, so that
399 * DoS attacks don't affect us.
401 DEBUG("Ignoring request from unknown client %s port %d",
402 inet_ntop(src_ipaddr.af, &src_ipaddr.ipaddr,
403 buffer, sizeof(buffer)), src_port);
408 * Some sanity checks, based on the packet code.
411 case PW_AUTHENTICATION_REQUEST:
412 RAD_SNMP_CLIENT_INC(listener, client, requests);
413 fun = rad_authenticate;
416 case PW_STATUS_SERVER:
417 if (!mainconfig.status_server) {
418 RAD_SNMP_TYPE_INC(listener, total_packets_dropped);
419 RAD_SNMP_CLIENT_INC(listener, client, packets_dropped);
420 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
423 fun = rad_status_server;
427 RAD_SNMP_INC(rad_snmp.auth.total_unknown_types);
428 RAD_SNMP_CLIENT_INC(listener, client, unknown_types);
430 DEBUG("Invalid packet code %d sent to authentication port from client %s port %d : IGNORED",
431 code, client->shortname, src_port);
434 } /* switch over packet types */
437 * Now that we've sanity checked everything, receive the
440 packet = rad_recv(listener->fd);
442 RAD_SNMP_TYPE_INC(listener, total_malformed_requests);
443 radlog(L_ERR, "%s", librad_errstr);
447 if (!received_request(listener, packet, prequest, client)) {
448 RAD_SNMP_TYPE_INC(listener, total_packets_dropped);
449 RAD_SNMP_CLIENT_INC(listener, client, packets_dropped);
460 * Receive packets from an accounting socket
462 static int acct_socket_recv(rad_listen_t *listener,
463 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
467 RADIUS_PACKET *packet;
468 RAD_REQUEST_FUNP fun = NULL;
471 lrad_ipaddr_t src_ipaddr;
473 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
474 if (rcode < 0) return 0;
476 RAD_SNMP_TYPE_INC(listener, total_requests);
478 if (rcode < 20) { /* AUTH_HDR_LEN */
479 RAD_SNMP_TYPE_INC(listener, total_malformed_requests);
483 if ((client = client_listener_find(listener,
484 &src_ipaddr)) == NULL) {
485 RAD_SNMP_TYPE_INC(listener, total_invalid_requests);
488 * This is debugging rather than logging, so that
489 * DoS attacks don't affect us.
491 DEBUG("Ignoring request from unknown client %s port %d",
492 inet_ntop(src_ipaddr.af, &src_ipaddr.ipaddr,
493 buffer, sizeof(buffer)), src_port);
498 * Some sanity checks, based on the packet code.
501 case PW_ACCOUNTING_REQUEST:
502 RAD_SNMP_CLIENT_INC(listener, client, requests);
503 fun = rad_accounting;
506 case PW_STATUS_SERVER:
507 if (!mainconfig.status_server) {
508 RAD_SNMP_TYPE_INC(listener, total_packets_dropped);
509 RAD_SNMP_CLIENT_INC(listener, client, unknown_types);
511 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
514 fun = rad_status_server;
519 * FIXME: Update MIB for packet types?
521 RAD_SNMP_TYPE_INC(listener, total_unknown_types);
522 RAD_SNMP_CLIENT_INC(listener, client, unknown_types);
524 DEBUG("Invalid packet code %d sent to a accounting port from client %s port %d : IGNORED",
525 code, client->shortname, src_port);
527 } /* switch over packet types */
530 * Now that we've sanity checked everything, receive the
533 packet = rad_recv(listener->fd);
535 RAD_SNMP_TYPE_INC(listener, total_malformed_requests);
536 radlog(L_ERR, "%s", librad_errstr);
541 * There can be no duplicate accounting packets.
543 if (!received_request(listener, packet, prequest, client)) {
544 RAD_SNMP_TYPE_INC(listener, total_packets_dropped);
545 RAD_SNMP_CLIENT_INC(listener, client, packets_dropped);
556 * Recieve packets from a proxy socket.
558 static int proxy_socket_recv(rad_listen_t *listener,
559 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
562 RADIUS_PACKET *packet;
563 RAD_REQUEST_FUNP fun = NULL;
566 packet = rad_recv(listener->fd);
568 radlog(L_ERR, "%s", librad_errstr);
573 * FIXME: Client MIB updates?
575 switch(packet->code) {
576 case PW_AUTHENTICATION_ACK:
577 case PW_ACCESS_CHALLENGE:
578 case PW_AUTHENTICATION_REJECT:
579 fun = rad_authenticate;
582 case PW_ACCOUNTING_RESPONSE:
583 fun = rad_accounting;
588 * FIXME: Update MIB for packet types?
590 radlog(L_ERR, "Invalid packet code %d sent to a proxy port "
591 "from home server %s port %d - ID %d : IGNORED",
593 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
594 packet->src_port, packet->id);
599 request = received_proxy_response(packet);
604 rad_assert(fun != NULL);
612 static int client_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
614 if (!request->reply->code) return 0;
616 rad_encode(request->reply, request->packet,
617 request->client->secret);
618 rad_sign(request->reply, request->packet,
619 request->client->secret);
625 static int client_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
627 if (rad_verify(request->packet, NULL,
628 request->client->secret) < 0) {
632 return rad_decode(request->packet, NULL,
633 request->client->secret);
636 static int proxy_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
638 rad_encode(request->proxy, NULL, request->home_server->secret);
639 rad_sign(request->proxy, NULL, request->home_server->secret);
645 static int proxy_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
647 if (rad_verify(request->proxy_reply, request->proxy,
648 request->home_server->secret) < 0) {
652 return rad_decode(request->proxy_reply, request->proxy,
653 request->home_server->secret);
657 #define STATE_UNOPENED (0)
658 #define STATE_UNLOCKED (1)
659 #define STATE_HEADER (2)
660 #define STATE_READING (3)
661 #define STATE_DONE (4)
662 #define STATE_WAITING (5)
665 * If we're limiting outstanding packets, then mark the response
668 static int detail_send(rad_listen_t *listener, REQUEST *request)
670 listen_detail_t *data = listener->data;
672 rad_assert(request->listener == listener);
673 rad_assert(listener->send == detail_send);
675 if (request->simul_max >= 0) {
676 rad_assert(data->outstanding != NULL);
677 rad_assert(request->simul_max < data->max_outstanding);
679 data->outstanding[request->simul_max] = 0;
687 * Open the detail file..
689 * FIXME: create it, if it's not already there, so that the main
690 * server select() will wake us up if there's anything to read.
692 static int detail_open(rad_listen_t *this)
696 listen_detail_t *data = this->data;
698 rad_assert(data->state == STATE_UNOPENED);
699 snprintf(buffer, sizeof(buffer), "%s.work", data->detail);
702 * FIXME: Have "one-shot" configuration, where it
703 * will read the detail file, and exit once it's
706 * FIXME: Try harder to open the detail file.
707 * Maybe sleep for X usecs if it doesn't exist?
711 * Open detail.work first, so we don't lose
712 * accounting packets. It's probably better to
713 * duplicate them than to lose them.
715 * Note that we're not writing to the file, but
716 * we've got to open it for writing in order to
717 * establish the lock, to prevent rlm_detail from
720 this->fd = open(buffer, O_RDWR);
723 * Try reading the detail file. If it
724 * doesn't exist, we can't do anything.
726 * Doing the stat will tell us if the file
727 * exists, even if we don't have permissions
730 if (stat(data->detail, &st) < 0) {
735 * Open it BEFORE we rename it, just to
738 this->fd = open(data->detail, O_RDWR);
740 radlog(L_ERR, "Failed to open %s: %s",
741 data->detail, strerror(errno));
746 * Rename detail to detail.work
748 if (rename(data->detail, buffer) < 0) {
753 } /* else detail.work existed, and we opened it */
755 rad_assert(data->vps == NULL);
757 rad_assert(data->fp == NULL);
758 data->fp = fdopen(this->fd, "r");
760 radlog(L_ERR, "Failed to re-open %s: %s",
761 data->detail, strerror(errno));
765 data->state = STATE_UNLOCKED;
767 data->client_ip.af = AF_UNSPEC;
774 * This is a bad hack, just so complaints have meaningful text.
776 static const RADCLIENT detail_client = {
791 static int detail_recv(rad_listen_t *listener,
792 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
795 char key[256], value[1024];
796 VALUE_PAIR *vp, **tail;
797 RADIUS_PACKET *packet;
799 listen_detail_t *data = listener->data;
801 if (data->state == STATE_UNOPENED) {
802 rad_assert(listener->fd < 0);
803 if (!detail_open(listener)) return 0;
805 rad_assert(listener->fd >= 0);
808 * Try to lock fd. If we can't, return. If we can,
809 * continue. This means that the server doesn't block
810 * while waiting for the lock to open...
812 if (data->state == STATE_UNLOCKED) {
814 * Note that we do NOT block waiting for the
815 * lock. We've re-named the file above, so we've
816 * already guaranteed that any *new* detail
817 * writer will not be opening this file. The
818 * only purpose of the lock is to catch a race
819 * condition where the execution "ping-pongs"
820 * between radiusd & radrelay.
822 if (rad_lockfd_nonblock(listener->fd, 0) < 0) {
826 * Look for the header
828 data->state = STATE_HEADER;
832 * Catch an out of memory condition which will most likely
835 if (data->state == STATE_DONE) goto alloc_packet;
838 * If we're in another state, then it means that we read
839 * a partial packet, which is bad.
841 rad_assert(data->state == STATE_HEADER);
842 rad_assert(data->vps == NULL);
845 * We read the last packet, and returned it for
846 * processing. We later come back here to shut
847 * everything down, and unlink the file.
849 if (feof(data->fp)) {
850 rad_assert(data->state == STATE_HEADER);
853 * Don't unlink the file until we've received
854 * all of the responses.
856 if (data->max_outstanding > 0) {
859 for (i = 0; i < data->max_outstanding; i++) {
861 * FIXME: close the file?
863 if (data->outstanding[i]) {
864 data->state = STATE_WAITING;
871 rad_assert(data->vps == NULL);
873 snprintf(buffer, sizeof(buffer), "%s.work", data->detail);
875 fclose(data->fp); /* closes listener->fd */
878 data->state = STATE_UNOPENED;
881 * Try to open "detail" again. If we're on a
882 * busy RADIUS server, odds are that it will
885 detail_open(listener);
894 while (fgets(buffer, sizeof(buffer), data->fp)) {
898 if (!strchr(buffer, '\n')) {
899 pairfree(&data->vps);
904 * We've read a header, possibly packet contents,
905 * and are now at the end of the packet.
907 if ((data->state == STATE_READING) &&
908 (buffer[0] == '\n')) {
909 data->state = STATE_DONE;
914 * Look for date/time header, and read VP's if
915 * found. If not, keep reading lines until we
918 if (data->state == STATE_HEADER) {
921 if (sscanf(buffer, "%*s %*s %*d %*d:%*d:%*d %d", &y)) {
922 data->state = STATE_READING;
928 * We have a full "attribute = value" line.
929 * If it doesn't look reasonable, skip it.
931 if (sscanf(buffer, "%255s = %1023s", key, value) != 2) {
936 * Skip non-protocol attributes.
938 if (!strcasecmp(key, "Request-Authenticator")) continue;
941 * Set the original client IP address, based on
942 * what's in the detail file.
944 * Hmm... we don't set the server IP address.
947 if (!strcasecmp(key, "Client-IP-Address")) {
948 data->client_ip.af = AF_INET;
949 ip_hton(value, AF_INET, &data->client_ip);
954 * The original time at which we received the
955 * packet. We need this to properly calculate
958 if (!strcasecmp(key, "Timestamp")) {
959 data->timestamp = atoi(value);
966 * FIXME: do we want to check for non-protocol
967 * attributes like radsqlrelay does?
970 if ((userparse(buffer, &vp) > 0) &&
978 * We got to EOF, If we're in STATE_HEADER, it's OK.
979 * Otherwise it's a problem. In any case, nuke the file
980 * and start over from scratch,
982 if (feof(data->fp)) {
987 * FIXME: Do load management.
991 * If we're not done, then there's a problem. The checks
994 rad_assert(data->state == STATE_DONE);
997 * The packet we read was empty, re-set the state to look
998 * for a header, and don't return anything.
1001 data->state = STATE_HEADER;
1006 * Allocate the packet. If we fail, it's a serious
1011 * If we keep track of the outstanding requests, do so
1012 * here. Note that to minimize potential work, we do
1013 * so only once the file is opened & locked.
1015 if (data->max_outstanding) {
1018 for (i = 0; i < data->max_outstanding; i++) {
1019 if (!data->outstanding[i]) {
1026 * All of the slots are full. Put the LF back,
1027 * so select will say that data is ready, and set
1028 * the state back to reading. Then, return so
1029 * that we don't overload the server.
1031 if (free_slot < 0) {
1032 ungetc('\n', data->fp);
1033 data->state = STATE_READING;
1038 packet = rad_alloc(1);
1040 return 0; /* maybe memory will magically free up... */
1043 memset(packet, 0, sizeof(*packet));
1044 packet->sockfd = -1;
1045 packet->src_ipaddr.af = AF_INET;
1046 packet->src_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
1047 packet->code = PW_ACCOUNTING_REQUEST;
1048 packet->timestamp = time(NULL);
1051 * Look for Acct-Delay-Time, and update
1052 * based on Acct-Delay-Time += (time(NULL) - timestamp)
1054 vp = pairfind(packet->vps, PW_ACCT_DELAY_TIME);
1056 vp = paircreate(PW_ACCT_DELAY_TIME, PW_TYPE_INTEGER);
1057 rad_assert(vp != NULL);
1059 if (data->timestamp != 0) {
1060 vp->vp_integer += time(NULL) - data->timestamp;
1064 * Remember where it came from, so that we don't
1065 * proxy it to the place it came from...
1067 if (data->client_ip.af != AF_UNSPEC) {
1068 packet->src_ipaddr = data->client_ip;
1071 vp = pairfind(packet->vps, PW_PACKET_SRC_IP_ADDRESS);
1073 packet->src_ipaddr.af = AF_INET;
1074 packet->src_ipaddr.ipaddr.ip4addr.s_addr = vp->vp_ipaddr;
1076 vp = pairfind(packet->vps, PW_PACKET_SRC_IPV6_ADDRESS);
1078 packet->src_ipaddr.af = AF_INET6;
1079 memcpy(&packet->src_ipaddr.ipaddr.ip6addr,
1080 &vp->vp_ipv6addr, sizeof(vp->vp_ipv6addr));
1084 vp = pairfind(packet->vps, PW_PACKET_DST_IP_ADDRESS);
1086 packet->dst_ipaddr.af = AF_INET;
1087 packet->dst_ipaddr.ipaddr.ip4addr.s_addr = vp->vp_ipaddr;
1089 vp = pairfind(packet->vps, PW_PACKET_DST_IPV6_ADDRESS);
1091 packet->dst_ipaddr.af = AF_INET6;
1092 memcpy(&packet->dst_ipaddr.ipaddr.ip6addr,
1093 &vp->vp_ipv6addr, sizeof(vp->vp_ipv6addr));
1098 * We've got to give SOME value for Id & ports, so that
1099 * the packets can be added to the request queue.
1100 * However, we don't want to keep track of used/unused
1101 * id's and ports, as that's a lot of work. This hack
1102 * ensures that (if we have real random numbers), that
1103 * there will be a collision on every 2^(16+15+15+24 - 1)
1104 * packets, on average. That means we can read 2^37
1105 * packets before having a collision, which means it's
1106 * effectively impossible.
1108 packet->id = lrad_rand() & 0xffff;
1109 packet->src_port = 1024 + (lrad_rand() & 0x7fff);
1110 packet->dst_port = 1024 + (lrad_rand() & 0x7fff);
1112 packet->dst_ipaddr.af = AF_INET;
1113 packet->dst_ipaddr.ipaddr.ip4addr.s_addr = htonl((INADDR_LOOPBACK & ~0xffffff) | (lrad_rand() & 0xffffff));
1115 packet->vps = data->vps;
1121 data->state = STATE_HEADER;
1124 * Keep track of free slots, as a hack, in an otherwise
1127 (*prequest)->simul_max = free_slot;
1128 data->outstanding[free_slot] = 1;
1130 *pfun = rad_accounting;
1133 printf("detail_recv: Read packet from %s\n", data->detail);
1134 for (vp = packet->vps; vp; vp = vp->next) {
1136 vp_print(stdout, vp);
1142 * FIXME: many of these checks may not be necessary when
1143 * reading from the detail file.
1145 if (!received_request(listener, packet, prequest, &detail_client)) {
1155 * Free detail-specific stuff.
1157 static void detail_free(rad_listen_t *this)
1159 listen_detail_t *data = this->data;
1162 pairfree(&data->vps);
1163 free(data->outstanding);
1165 if (data->fp != NULL) fclose(data->fp);
1169 static int detail_print(rad_listen_t *this, char *buffer, size_t bufsize)
1171 return snprintf(buffer, bufsize, "%s",
1172 ((listen_detail_t *)(this->data))->detail);
1175 static int detail_encode(UNUSED rad_listen_t *this, UNUSED REQUEST *request)
1178 * We never encode responses "sent to" the detail file.
1183 static int detail_decode(UNUSED rad_listen_t *this, UNUSED REQUEST *request)
1186 * We never decode responses read from the detail file.
1192 static const CONF_PARSER detail_config[] = {
1193 { "detail", PW_TYPE_STRING_PTR,
1194 offsetof(listen_detail_t, detail), NULL, NULL },
1195 { "max_outstanding", PW_TYPE_INTEGER,
1196 offsetof(listen_detail_t, max_outstanding), NULL, "100" },
1198 { NULL, -1, 0, NULL, NULL } /* end the list */
1203 * Parse a detail section.
1205 static int detail_parse(const char *filename, int lineno,
1206 const CONF_SECTION *cs, rad_listen_t *this)
1209 listen_detail_t *data;
1213 rcode = cf_section_parse(cs, data, detail_config);
1215 radlog(L_ERR, "%s[%d]: Failed parsing listen section",
1220 if (!data->detail) {
1221 radlog(L_ERR, "%s[%d]: No detail file specified in listen section",
1228 data->state = STATE_UNOPENED;
1230 if (data->max_outstanding > 32768) data->max_outstanding = 32768;
1232 if (data->max_outstanding > 0) {
1233 data->outstanding = rad_malloc(sizeof(int) * data->max_outstanding);
1243 static int radius_snmp_recv(rad_listen_t *listener,
1244 UNUSED RAD_REQUEST_FUNP *pfun,
1245 UNUSED REQUEST **prequest)
1247 if (!mainconfig.do_snmp) return 0;
1249 if ((rad_snmp.smux_fd >= 0) &&
1250 (rad_snmp.smux_event == SMUX_READ)) {
1255 * If we've got to re-connect, then do so now,
1256 * before calling select again.
1258 if (rad_snmp.smux_event == SMUX_CONNECT) {
1263 * Reset this every time, as the smux connect may have
1264 * opened a new socket.
1266 listener->fd = rad_snmp.smux_fd;
1272 static int radius_snmp_print(rad_listen_t *this, char *buffer, size_t bufsize)
1274 return snprintf(buffer, bufsize, "SMUX with OID .1.3.6.1.4.1.3317.1.3.1");
1279 static const rad_listen_master_t master_listen[RAD_LISTEN_MAX] = {
1280 { NULL, NULL, NULL, NULL, NULL, NULL, NULL}, /* RAD_LISTEN_NONE */
1284 proxy_socket_recv, proxy_socket_send,
1285 socket_print, proxy_socket_encode, proxy_socket_decode },
1287 /* authentication */
1288 { common_socket_parse, NULL,
1289 auth_socket_recv, auth_socket_send,
1290 socket_print, client_socket_encode, client_socket_decode },
1293 { common_socket_parse, NULL,
1294 acct_socket_recv, acct_socket_send,
1295 socket_print, client_socket_encode, client_socket_decode},
1298 { detail_parse, detail_free,
1299 detail_recv, detail_send,
1300 detail_print, detail_encode, detail_decode },
1302 { NULL, NULL, NULL, NULL, NULL, NULL, NULL}, /* RAD_LISTEN_SNMP */
1308 * Binds a listener to a socket.
1310 static int listen_bind(rad_listen_t *this)
1312 rad_listen_t **last;
1313 listen_socket_t *sock = this->data;
1316 * If the port is zero, then it means the appropriate
1317 * thing from /etc/services.
1319 if (sock->port == 0) {
1320 struct servent *svp;
1322 switch (this->type) {
1323 case RAD_LISTEN_AUTH:
1324 svp = getservbyname ("radius", "udp");
1326 sock->port = ntohs(svp->s_port);
1328 sock->port = PW_AUTH_UDP_PORT;
1332 case RAD_LISTEN_ACCT:
1333 svp = getservbyname ("radacct", "udp");
1335 sock->port = ntohs(svp->s_port);
1337 sock->port = PW_ACCT_UDP_PORT;
1342 radlog(L_ERR|L_CONS, "ERROR: Non-fatal internal sanity check failed in bind.");
1348 * Find it in the old list, AFTER updating the port. If
1349 * it's there, use that, rather than creating a new
1350 * socket. This allows HUP's to re-use the old sockets,
1351 * which means that packets waiting in the socket queue
1354 for (last = &mainconfig.listen;
1356 last = &((*last)->next)) {
1357 if ((this->type == (*last)->type) &&
1358 (sock->port == ((listen_socket_t *)((*last)->data))->port) &&
1359 (sock->ipaddr.af == ((listen_socket_t *)((*last)->data))->ipaddr.af)) {
1362 if (sock->ipaddr.af == AF_INET) {
1363 equal = (sock->ipaddr.ipaddr.ip4addr.s_addr == ((listen_socket_t *)((*last)->data))->ipaddr.ipaddr.ip4addr.s_addr);
1364 } else if (sock->ipaddr.af == AF_INET6) {
1365 equal = IN6_ARE_ADDR_EQUAL(&(sock->ipaddr.ipaddr.ip6addr), &(((listen_socket_t *)((*last)->data))->ipaddr.ipaddr.ip6addr));
1371 this->fd = (*last)->fd;
1378 this->fd = lrad_socket(&sock->ipaddr, sock->port);
1380 radlog(L_ERR|L_CONS, "ERROR: Failed to open socket: %s",
1387 if ((flags = fcntl(this->fd, F_GETFL, NULL)) < 0) {
1388 radlog(L_ERR, "Failure in fcntl: %s)\n", strerror(errno));
1392 flags |= O_NONBLOCK;
1393 if( fcntl(this->fd, F_SETFL, flags) < 0) {
1394 radlog(L_ERR, "Failure in fcntl: %s)\n", strerror(errno));
1405 * Allocate & initialize a new listener.
1407 static rad_listen_t *listen_alloc(RAD_LISTEN_TYPE type)
1411 this = rad_malloc(sizeof(*this));
1412 memset(this, 0, sizeof(*this));
1415 this->recv = master_listen[this->type].recv;
1416 this->send = master_listen[this->type].send;
1417 this->print = master_listen[this->type].print;
1418 this->encode = master_listen[this->type].encode;
1419 this->decode = master_listen[this->type].decode;
1422 case RAD_LISTEN_AUTH:
1423 case RAD_LISTEN_ACCT:
1424 case RAD_LISTEN_PROXY:
1425 this->data = rad_malloc(sizeof(listen_socket_t));
1426 memset(this->data, 0, sizeof(listen_socket_t));
1429 case RAD_LISTEN_DETAIL:
1430 this->data = rad_malloc(sizeof(listen_detail_t));
1431 memset(this->data, 0, sizeof(listen_detail_t));
1442 * Externally visible function for creating a new proxy LISTENER.
1444 * For now, don't take ipaddr or port.
1446 * Not thread-safe, but all calls to it are protected by the
1447 * proxy mutex in request_list.c
1449 rad_listen_t *proxy_new_listener()
1451 int last_proxy_port, port;
1452 rad_listen_t *this, *tmp, **last;
1453 listen_socket_t *sock, *old;
1455 this = listen_alloc(RAD_LISTEN_PROXY);
1458 * Find an existing proxy socket to copy.
1460 * FIXME: Make it per-realm, or per-home server!
1462 last_proxy_port = 0;
1464 last = &mainconfig.listen;
1465 for (tmp = mainconfig.listen; tmp != NULL; tmp = tmp->next) {
1466 if (tmp->type == RAD_LISTEN_PROXY) {
1468 if (sock->port > last_proxy_port) {
1469 last_proxy_port = sock->port + 1;
1471 if (!old) old = sock;
1474 last = &(tmp->next);
1477 if (!old) return NULL; /* This is a serious error. */
1480 * FIXME: find a new IP address to listen on?
1483 memcpy(&sock->ipaddr, &old->ipaddr, sizeof(sock->ipaddr));
1486 * Keep going until we find an unused port.
1488 for (port = last_proxy_port; port < 64000; port++) {
1490 if (listen_bind(this) == 0) {
1492 * Add the new listener to the list of
1504 static const LRAD_NAME_NUMBER listen_compare[] = {
1505 { "auth", RAD_LISTEN_AUTH },
1506 { "acct", RAD_LISTEN_ACCT },
1507 { "detail", RAD_LISTEN_DETAIL },
1513 * Generate a list of listeners. Takes an input list of
1514 * listeners, too, so we don't close sockets with waiting packets.
1516 int listen_init(const char *filename, rad_listen_t **head)
1520 rad_listen_t **last;
1522 lrad_ipaddr_t server_ipaddr;
1526 * We shouldn't be called with a pre-existing list.
1528 rad_assert(head && (*head == NULL));
1530 if (start_time != 0) start_time = time(NULL);
1533 server_ipaddr.af = AF_UNSPEC;
1536 * If the port is specified on the command-line,
1537 * it over-rides the configuration file.
1539 if (mainconfig.port >= 0) {
1540 auth_port = mainconfig.port;
1542 rcode = cf_item_parse(mainconfig.config, "port",
1543 PW_TYPE_INTEGER, &auth_port,
1544 Stringify(PW_AUTH_UDP_PORT));
1545 if (rcode < 0) return -1; /* error parsing it */
1548 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'.");
1552 * If the IP address was configured on the command-line,
1553 * use that as the "bind_address"
1555 if (mainconfig.myip.af != AF_UNSPEC) {
1556 memcpy(&server_ipaddr, &mainconfig.myip,
1557 sizeof(server_ipaddr));
1562 * Else look for bind_address and/or listen sections.
1564 server_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
1565 rcode = cf_item_parse(mainconfig.config, "bind_address",
1567 &server_ipaddr.ipaddr.ip4addr, NULL);
1568 if (rcode < 0) return -1; /* error parsing it */
1570 if (rcode == 0) { /* successfully parsed IPv4 */
1571 listen_socket_t *sock;
1572 server_ipaddr.af = AF_INET;
1574 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'.");
1577 this = listen_alloc(RAD_LISTEN_AUTH);
1580 sock->ipaddr = server_ipaddr;
1581 sock->port = auth_port;
1583 if (listen_bind(this) < 0) {
1586 radlog(L_CONS|L_ERR, "There appears to be another RADIUS server running on the authentication port %d", sock->port);
1589 auth_port = sock->port; /* may have been updated in listen_bind */
1591 last = &(this->next);
1594 * Open Accounting Socket.
1596 * If we haven't already gotten acct_port from
1597 * /etc/services, then make it auth_port + 1.
1599 this = listen_alloc(RAD_LISTEN_ACCT);
1603 * Create the accounting socket.
1605 * The accounting port is always the
1606 * authentication port + 1
1608 sock->ipaddr = server_ipaddr;
1609 sock->port = auth_port + 1;
1611 if (listen_bind(this) < 0) {
1614 radlog(L_CONS|L_ERR, "There appears to be another RADIUS server running on the accounting port %d", sock->port);
1619 last = &(this->next);
1621 } else if (mainconfig.port > 0) { /* no bind address, but a port */
1622 radlog(L_CONS|L_ERR, "The command-line says \"-p %d\", but there is no associated IP address to use",
1628 * They specified an IP on the command-line, ignore
1629 * all listen sections.
1631 if (mainconfig.myip.af != AF_UNSPEC) goto do_proxy;
1634 * Walk through the "listen" sections, if they exist.
1636 for (cs = cf_subsection_find_next(mainconfig.config, NULL, "listen");
1638 cs = cf_subsection_find_next(mainconfig.config, cs, "listen")) {
1640 char *listen_type, *identity;
1641 int lineno = cf_section_lineno(cs);
1643 listen_type = identity = NULL;
1645 rcode = cf_item_parse(cs, "type", PW_TYPE_STRING_PTR,
1647 if (rcode < 0) return -1;
1651 radlog(L_ERR, "%s[%d]: No type specified in listen section",
1657 * See if there's an identity.
1659 rcode = cf_item_parse(cs, "identity", PW_TYPE_STRING_PTR,
1667 type = lrad_str2int(listen_compare, listen_type,
1670 if (type == RAD_LISTEN_NONE) {
1672 radlog(L_CONS|L_ERR, "%s[%d]: Invalid type in listen section.",
1678 * Set up cross-type data.
1680 this = listen_alloc(type);
1681 this->identity = identity;
1685 * Call per-type parser.
1687 if (master_listen[type].parse(filename, lineno,
1695 last = &(this->next);
1699 * If we're proxying requests, open the proxy FD.
1700 * Otherwise, don't do anything.
1703 if (mainconfig.proxy_requests == TRUE) {
1705 listen_socket_t *sock = NULL;
1708 * No sockets to receive packets, therefore
1709 * proxying is pointless.
1711 if (!*head) return -1;
1714 * If we previously had proxy sockets, copy them
1715 * to the new config.
1717 if (mainconfig.listen != NULL) {
1718 rad_listen_t *old, *next, **tail;
1720 tail = &mainconfig.listen;
1721 for (old = mainconfig.listen;
1726 if (old->type != RAD_LISTEN_PROXY) {
1727 tail = &((*tail)->next);
1734 last = &(old->next);
1741 * Find the first authentication port,
1744 for (this = *head; this != NULL; this = this->next) {
1745 if (this->type == RAD_LISTEN_AUTH) {
1747 if (server_ipaddr.af == AF_UNSPEC) {
1748 server_ipaddr = sock->ipaddr;
1750 port = sock->port + 2; /* skip acct port */
1755 if (port < 0) port = 1024 + (lrad_rand() & 0x1ff);
1758 * Address is still unspecified, use IPv4.
1760 if (server_ipaddr.af == AF_UNSPEC) {
1761 server_ipaddr.af = AF_INET;
1762 server_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_ANY);
1765 this = listen_alloc(RAD_LISTEN_PROXY);
1769 * Create the first proxy socket.
1771 sock->ipaddr = server_ipaddr;
1774 * Try to find a proxy port (value doesn't matter)
1776 for (sock->port = port;
1779 if (listen_bind(this) == 0) {
1781 last = &(this->next); /* just in case */
1786 if (sock->port >= 64000) {
1789 radlog(L_ERR|L_CONS, "Failed to open socket for proxying");
1796 if (mainconfig.do_snmp) {
1800 * Forget about the old one.
1802 for (this = mainconfig.listen;
1804 this = this->next) {
1805 if (this->type != RAD_LISTEN_SNMP) continue;
1809 this = rad_malloc(sizeof(*this));
1810 memset(this, 0, sizeof(*this));
1812 this->type = RAD_LISTEN_SNMP;
1813 this->fd = rad_snmp.smux_fd;
1815 this->recv = radius_snmp_recv;
1816 this->print = radius_snmp_print;
1819 last = &(this->next);
1824 * Sanity check the configuration.
1827 if (mainconfig.listen != NULL) for (this = *head;
1829 this = this->next) {
1830 if (((this->type == RAD_LISTEN_ACCT) &&
1831 (rcode == RAD_LISTEN_DETAIL)) ||
1832 ((this->type == RAD_LISTEN_DETAIL) &&
1833 (rcode == RAD_LISTEN_ACCT))) {
1834 rad_assert(0 == 1); /* FIXME: configuration error */
1837 if (rcode != 0) continue;
1839 if ((this->type == RAD_LISTEN_ACCT) ||
1840 (this->type == RAD_LISTEN_DETAIL)) {
1850 * Free a linked list of listeners;
1852 void listen_free(rad_listen_t **head)
1856 if (!head || !*head) return;
1860 rad_listen_t *next = this->next;
1862 free(this->identity);
1865 * Other code may have eaten the FD.
1867 if (this->fd >= 0) close(this->fd);
1869 if (master_listen[this->type].free) {
1870 master_listen[this->type].free(this);