55afcc02b0ad3721cc31514d2dfbeacbbbcf13c4
[freeradius.git] / src / main / listen.c
1 /*
2  * listen.c     Handle socket stuff
3  *
4  * Version:     $Id$
5  *
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.
10  *
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.
15  *
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
19  *
20  * Copyright 2005,2006  The FreeRADIUS server project
21  * Copyright 2005  Alan DeKok <aland@ox.org>
22  */
23
24 #include <freeradius-devel/ident.h>
25 RCSID("$Id$")
26
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>
31 #include <freeradius-devel/vqp.h>
32 #include <freeradius-devel/dhcp.h>
33
34 #include <freeradius-devel/vmps.h>
35 #include <freeradius-devel/detail.h>
36
37 #ifdef HAVE_SYS_RESOURCE_H
38 #include <sys/resource.h>
39 #endif
40
41 #ifdef HAVE_NET_IF_H
42 #include <net/if.h>
43 #endif
44
45 #ifdef HAVE_FCNTL_H
46 #include <fcntl.h>
47 #endif
48
49
50 /*
51  *      We'll use this below.
52  */
53 typedef int (*rad_listen_parse_t)(CONF_SECTION *, rad_listen_t *);
54 typedef void (*rad_listen_free_t)(rad_listen_t *);
55
56 typedef struct rad_listen_master_t {
57         rad_listen_parse_t      parse;
58         rad_listen_free_t       free;
59         rad_listen_recv_t       recv;
60         rad_listen_send_t       send;
61         rad_listen_print_t      print;
62         rad_listen_encode_t     encode;
63         rad_listen_decode_t     decode;
64 } rad_listen_master_t;
65
66 typedef struct listen_socket_t {
67         /*
68          *      For normal sockets.
69          */
70         fr_ipaddr_t     ipaddr;
71         int             port;
72         RADCLIENT_LIST  *clients;
73 } listen_socket_t;
74
75 /*
76  *      Find a per-socket client.
77  */
78 RADCLIENT *client_listener_find(const rad_listen_t *listener,
79                                        const fr_ipaddr_t *ipaddr)
80 {
81         const RADCLIENT_LIST *clients;
82
83         rad_assert(listener != NULL);
84         rad_assert(ipaddr != NULL);
85
86         rad_assert((listener->type == RAD_LISTEN_AUTH) ||
87                    (listener->type == RAD_LISTEN_ACCT) ||
88                    (listener->type == RAD_LISTEN_VQP) ||
89                    (listener->type == RAD_LISTEN_DHCP));
90
91         clients = ((listen_socket_t *)listener->data)->clients;
92
93         /*
94          *      This HAS to have been initialized previously.
95          */
96         rad_assert(clients != NULL);
97
98         return client_find(clients, ipaddr);
99 }
100
101 static int listen_bind(rad_listen_t *this);
102
103
104 /*
105  *      Process and reply to a server-status request.
106  *      Like rad_authenticate and rad_accounting this should
107  *      live in it's own file but it's so small we don't bother.
108  */
109 static int rad_status_server(REQUEST *request)
110 {
111         int rcode = RLM_MODULE_OK;
112         DICT_VALUE *dval;
113
114         switch (request->listener->type) {
115         case RAD_LISTEN_AUTH:
116                 dval = dict_valbyname(PW_AUTZ_TYPE, "Status-Server");
117                 if (dval) {
118                         rcode = module_authorize(dval->value, request);
119                 } else {
120                         rcode = RLM_MODULE_OK;
121                 }
122
123                 switch (rcode) {
124                 case RLM_MODULE_OK:
125                 case RLM_MODULE_UPDATED:
126                         request->reply->code = PW_AUTHENTICATION_ACK;
127                         break;
128
129                 case RLM_MODULE_FAIL:
130                 case RLM_MODULE_HANDLED:
131                         request->reply->code = 0; /* don't reply */
132                         break;
133
134                 default:
135                 case RLM_MODULE_REJECT:
136                         request->reply->code = PW_AUTHENTICATION_REJECT;
137                         break;
138                 }
139                 break;
140
141 #ifdef WITH_ACCOUNTING
142         case RAD_LISTEN_ACCT:
143                 dval = dict_valbyname(PW_ACCT_TYPE, "Status-Server");
144                 if (dval) {
145                         rcode = module_accounting(dval->value, request);
146                 } else {
147                         rcode = RLM_MODULE_OK;
148                 }
149
150                 switch (rcode) {
151                 case RLM_MODULE_OK:
152                 case RLM_MODULE_UPDATED:
153                         request->reply->code = PW_ACCOUNTING_RESPONSE;
154                         break;
155
156                 default:
157                         request->reply->code = 0; /* don't reply */
158                         break;
159                 }
160                 break;
161 #endif
162
163         default:
164                 return 0;
165         }
166
167         return 0;
168 }
169
170
171 static int socket_print(rad_listen_t *this, char *buffer, size_t bufsize)
172 {
173         listen_socket_t *sock = this->data;
174         const char *name;
175         char ip_buf[256];
176
177         if ((sock->ipaddr.af == AF_INET) &&
178             (sock->ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
179                 strcpy(ip_buf, "*");
180         } else {
181                 ip_ntoh(&sock->ipaddr, ip_buf, sizeof(ip_buf));
182         }
183
184         switch (this->type) {
185         case RAD_LISTEN_AUTH:
186                 name = "authentication";
187                 break;
188
189 #ifdef WITH_ACCOUNTING
190         case RAD_LISTEN_ACCT:
191                 name = "accounting";
192                 break;
193 #endif
194
195 #ifdef WITH_PROXY
196         case RAD_LISTEN_PROXY:
197                 name = "proxy";
198                 break;
199 #endif
200
201 #ifdef WITH_VMPS
202         case RAD_LISTEN_VQP:
203                 name = "vmps";
204                 break;
205 #endif
206
207 #ifdef WITH_DHCP
208         case RAD_LISTEN_DHCP:
209                 name = "dhcp";
210                 break;
211 #endif
212
213         default:
214                 name = "??";
215                 break;
216         }
217
218         if (!this->server) {
219                 return snprintf(buffer, bufsize, "%s address %s port %d",
220                                 name, ip_buf, sock->port);
221         }
222
223         return snprintf(buffer, bufsize, "%s address %s port %d as server %s",
224                         name, ip_buf, sock->port, this->server);
225 }
226
227
228 /*
229  *      Parse an authentication or accounting socket.
230  */
231 static int common_socket_parse(CONF_SECTION *cs, rad_listen_t *this)
232 {
233         int             rcode;
234         int             listen_port;
235         fr_ipaddr_t     ipaddr;
236         listen_socket_t *sock = this->data;
237         char            *section_name = NULL;
238         CONF_SECTION    *client_cs, *parentcs;
239
240         /*
241          *      Try IPv4 first
242          */
243         ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
244         rcode = cf_item_parse(cs, "ipaddr", PW_TYPE_IPADDR,
245                               &ipaddr.ipaddr.ip4addr, NULL);
246         if (rcode < 0) return -1;
247
248         if (rcode == 0) { /* successfully parsed IPv4 */
249                 ipaddr.af = AF_INET;
250
251         } else {        /* maybe IPv6? */
252                 rcode = cf_item_parse(cs, "ipv6addr", PW_TYPE_IPV6ADDR,
253                                       &ipaddr.ipaddr.ip6addr, NULL);
254                 if (rcode < 0) return -1;
255
256                 if (rcode == 1) {
257                         cf_log_err(cf_sectiontoitem(cs),
258                                    "No address specified in listen section");
259                         return -1;
260                 }
261                 ipaddr.af = AF_INET6;
262         }
263
264         rcode = cf_item_parse(cs, "port", PW_TYPE_INTEGER,
265                               &listen_port, "0");
266         if (rcode < 0) return -1;
267
268         if ((listen_port < 0) || (listen_port > 65500)) {
269                         cf_log_err(cf_sectiontoitem(cs),
270                                    "Invalid value for \"port\"");
271                         return -1;
272         }
273
274         sock->ipaddr = ipaddr;
275         sock->port = listen_port;
276
277         /*
278          *      And bind it to the port.
279          */
280         if (listen_bind(this) < 0) {
281                 char buffer[128];
282                 cf_log_err(cf_sectiontoitem(cs),
283                            "Error binding to port for %s port %d",
284                            ip_ntoh(&sock->ipaddr, buffer, sizeof(buffer)),
285                            sock->port);
286                 return -1;
287         }
288
289         /*
290          *      If we can bind to interfaces, do so,
291          *      else don't.
292          */
293         if (cf_pair_find(cs, "interface")) {
294 #ifndef SO_BINDTODEVICE
295                 cf_log_err(cf_sectiontoitem(cs),
296                            "System does not support binding to interfaces.  Delete this line from the configuration file.");
297                 return -1;
298 #else
299                 const char *value;
300                 CONF_PAIR *cp = cf_pair_find(cs, "interface");
301                 struct ifreq ifreq;
302
303                 rad_assert(cp != NULL);
304                 value = cf_pair_value(cp);
305                 if (!value) {
306                         cf_log_err(cf_sectiontoitem(cs),
307                                    "No interface name given");
308                         return -1;
309                 }
310
311                 strcpy(ifreq.ifr_name, value);
312
313                 if (setsockopt(this->fd, SOL_SOCKET, SO_BINDTODEVICE,
314                                (char *)&ifreq, sizeof(ifreq)) < 0) {
315                         cf_log_err(cf_sectiontoitem(cs),
316                                    "Failed binding to interface %s: %s",
317                                    value, strerror(errno));
318                         return -1;
319                 } /* else it worked. */
320 #endif
321         }
322
323 #ifdef WITH_PROXY
324         /*
325          *      Proxy sockets don't have clients.
326          */
327         if (this->type == RAD_LISTEN_PROXY) return 0;
328 #endif
329         
330         /*
331          *      The more specific configurations are preferred to more
332          *      generic ones.
333          */
334         client_cs = NULL;
335         parentcs = cf_top_section(cs);
336         rcode = cf_item_parse(cs, "clients", PW_TYPE_STRING_PTR,
337                               &section_name, NULL);
338         if (rcode < 0) return -1; /* bad string */
339         if (rcode == 0) {
340                 /*
341                  *      Explicit list given: use it.
342                  */
343                 client_cs = cf_section_sub_find_name2(parentcs,
344                                                       "clients",
345                                                       section_name);
346                 if (!client_cs) {
347                         client_cs = cf_section_find(section_name);
348                 }
349                 if (!client_cs) {
350                         cf_log_err(cf_sectiontoitem(cs),
351                                    "Failed to find clients %s {...}",
352                                    section_name);
353                         free(section_name);
354                         return -1;
355                 }
356                 free(section_name);
357         } /* else there was no "clients = " entry. */
358
359         if (!client_cs) {
360                 CONF_SECTION *server_cs;
361
362                 server_cs = cf_section_sub_find_name2(parentcs,
363                                                       "server",
364                                                       this->server);
365                 /*
366                  *      Found a "server foo" section.  If there are clients
367                  *      in it, use them.
368                  */
369                 if (server_cs &&
370                     (cf_section_sub_find(server_cs, "client") != NULL)) {
371                         client_cs = server_cs;
372                 }
373         }
374
375         /*
376          *      Still nothing.  Look for global clients.
377          */
378         if (!client_cs) client_cs = parentcs;
379
380         sock->clients = clients_parse_section(client_cs);
381         if (!sock->clients) {
382                 cf_log_err(cf_sectiontoitem(cs),
383                            "Failed to find any clients for this listen section");
384                 return -1;
385         }
386
387         return 0;
388 }
389
390 /*
391  *      Send an authentication response packet
392  */
393 static int auth_socket_send(rad_listen_t *listener, REQUEST *request)
394 {
395         rad_assert(request->listener == listener);
396         rad_assert(listener->send == auth_socket_send);
397
398         return rad_send(request->reply, request->packet,
399                         request->client->secret);
400 }
401
402
403 #ifdef WITH_ACCOUNTING
404 /*
405  *      Send an accounting response packet (or not)
406  */
407 static int acct_socket_send(rad_listen_t *listener, REQUEST *request)
408 {
409         rad_assert(request->listener == listener);
410         rad_assert(listener->send == acct_socket_send);
411
412         /*
413          *      Accounting reject's are silently dropped.
414          *
415          *      We do it here to avoid polluting the rest of the
416          *      code with this knowledge
417          */
418         if (request->reply->code == 0) return 0;
419
420         return rad_send(request->reply, request->packet,
421                         request->client->secret);
422 }
423 #endif
424
425 #ifdef WITH_PROXY
426 /*
427  *      Send a packet to a home server.
428  *
429  *      FIXME: have different code for proxy auth & acct!
430  */
431 static int proxy_socket_send(rad_listen_t *listener, REQUEST *request)
432 {
433         listen_socket_t *sock = listener->data;
434
435         rad_assert(request->proxy_listener == listener);
436         rad_assert(listener->send == proxy_socket_send);
437
438         request->proxy->src_ipaddr = sock->ipaddr;
439         request->proxy->src_port = sock->port;
440
441         return rad_send(request->proxy, request->packet,
442                         request->home_server->secret);
443 }
444 #endif
445
446
447 /*
448  *      Check if an incoming request is "ok"
449  *
450  *      It takes packets, not requests.  It sees if the packet looks
451  *      OK.  If so, it does a number of sanity checks on it.
452   */
453 static int auth_socket_recv(rad_listen_t *listener,
454                             RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
455 {
456         ssize_t         rcode;
457         int             code, src_port;
458         RADIUS_PACKET   *packet;
459         RAD_REQUEST_FUNP fun = NULL;
460         char            buffer[128];
461         RADCLIENT       *client;
462         fr_ipaddr_t     src_ipaddr;
463
464         rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
465         if (rcode < 0) return 0;
466
467         RAD_SNMP_TYPE_INC(listener, total_requests);
468
469         if (rcode < 20) {       /* AUTH_HDR_LEN */
470                 RAD_SNMP_TYPE_INC(listener, total_malformed_requests);
471                 return 0;
472         }
473
474         if ((client = client_listener_find(listener,
475                                            &src_ipaddr)) == NULL) {
476                 rad_recv_discard(listener->fd);
477                 RAD_SNMP_TYPE_INC(listener, total_invalid_requests);
478
479                 if (debug_flag > 0) {
480                         char name[1024];
481
482                         listener->print(listener, name, sizeof(name));
483
484                         /*
485                          *      This is debugging rather than logging, so that
486                          *      DoS attacks don't affect us.
487                          */
488                         DEBUG("Ignoring request to %s from unknown client %s port %d",
489                               name,
490                               inet_ntop(src_ipaddr.af, &src_ipaddr.ipaddr,
491                                         buffer, sizeof(buffer)), src_port);
492                 }
493
494                 return 0;
495         }
496
497         /*
498          *      Some sanity checks, based on the packet code.
499          */
500         switch(code) {
501         case PW_AUTHENTICATION_REQUEST:
502                 RAD_SNMP_CLIENT_INC(listener, client, requests);
503                 fun = rad_authenticate;
504                 break;
505
506         case PW_STATUS_SERVER:
507                 if (!mainconfig.status_server) {
508                         rad_recv_discard(listener->fd);
509                         RAD_SNMP_TYPE_INC(listener, total_packets_dropped);
510                         RAD_SNMP_CLIENT_INC(listener, client, packets_dropped);
511                         DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
512                         return 0;
513                 }
514                 fun = rad_status_server;
515                 break;
516
517         default:
518                 rad_recv_discard(listener->fd);
519                 RAD_SNMP_INC(rad_snmp.auth.total_unknown_types);
520                 RAD_SNMP_CLIENT_INC(listener, client, unknown_types);
521
522                 DEBUG("Invalid packet code %d sent to authentication port from client %s port %d : IGNORED",
523                       code, client->shortname, src_port);
524                 return 0;
525                 break;
526         } /* switch over packet types */
527
528         /*
529          *      Now that we've sanity checked everything, receive the
530          *      packet.
531          */
532         packet = rad_recv(listener->fd, client->message_authenticator);
533         if (!packet) {
534                 RAD_SNMP_TYPE_INC(listener, total_malformed_requests);
535                 DEBUG("%s", librad_errstr);
536                 return 0;
537         }
538
539         if (!received_request(listener, packet, prequest, client)) {
540                 RAD_SNMP_TYPE_INC(listener, total_packets_dropped);
541                 RAD_SNMP_CLIENT_INC(listener, client, packets_dropped);
542                 rad_free(&packet);
543                 return 0;
544         }
545
546         *pfun = fun;
547         return 1;
548 }
549
550
551 #ifdef WITH_ACCOUNTING
552 /*
553  *      Receive packets from an accounting socket
554  */
555 static int acct_socket_recv(rad_listen_t *listener,
556                             RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
557 {
558         ssize_t         rcode;
559         int             code, src_port;
560         RADIUS_PACKET   *packet;
561         RAD_REQUEST_FUNP fun = NULL;
562         char            buffer[128];
563         RADCLIENT       *client;
564         fr_ipaddr_t     src_ipaddr;
565
566         rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
567         if (rcode < 0) return 0;
568
569         RAD_SNMP_TYPE_INC(listener, total_requests);
570
571         if (rcode < 20) {       /* AUTH_HDR_LEN */
572                 RAD_SNMP_TYPE_INC(listener, total_malformed_requests);
573                 return 0;
574         }
575
576         if ((client = client_listener_find(listener,
577                                            &src_ipaddr)) == NULL) {
578                 rad_recv_discard(listener->fd);
579                 RAD_SNMP_TYPE_INC(listener, total_invalid_requests);
580
581                 /*
582                  *      This is debugging rather than logging, so that
583                  *      DoS attacks don't affect us.
584                  */
585                 if (debug_flag > 0) {
586                         char name[1024];
587
588                         listener->print(listener, name, sizeof(name));
589
590                         DEBUG("Ignoring request to %s from unknown client %s port %d",
591                               name,
592                               inet_ntop(src_ipaddr.af, &src_ipaddr.ipaddr,
593                                         buffer, sizeof(buffer)), src_port);
594                 }
595
596                 return 0;
597         }
598
599         /*
600          *      Some sanity checks, based on the packet code.
601          */
602         switch(code) {
603         case PW_ACCOUNTING_REQUEST:
604                 RAD_SNMP_CLIENT_INC(listener, client, requests);
605                 fun = rad_accounting;
606                 break;
607
608         case PW_STATUS_SERVER:
609                 if (!mainconfig.status_server) {
610                         rad_recv_discard(listener->fd);
611                         RAD_SNMP_TYPE_INC(listener, total_packets_dropped);
612                         RAD_SNMP_CLIENT_INC(listener, client, unknown_types);
613
614                         DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
615                         return 0;
616                 }
617                 fun = rad_status_server;
618                 break;
619
620         default:
621                 rad_recv_discard(listener->fd);
622                 RAD_SNMP_TYPE_INC(listener, total_unknown_types);
623                 RAD_SNMP_CLIENT_INC(listener, client, unknown_types);
624
625                 DEBUG("Invalid packet code %d sent to a accounting port from client %s port %d : IGNORED",
626                       code, client->shortname, src_port);
627                 return 0;
628         } /* switch over packet types */
629
630         /*
631          *      Now that we've sanity checked everything, receive the
632          *      packet.
633          */
634         packet = rad_recv(listener->fd, 0);
635         if (!packet) {
636                 RAD_SNMP_TYPE_INC(listener, total_malformed_requests);
637                 radlog(L_ERR, "%s", librad_errstr);
638                 return 0;
639         }
640
641         /*
642          *      There can be no duplicate accounting packets.
643          */
644         if (!received_request(listener, packet, prequest, client)) {
645                 RAD_SNMP_TYPE_INC(listener, total_packets_dropped);
646                 RAD_SNMP_CLIENT_INC(listener, client, packets_dropped);
647                 rad_free(&packet);
648                 return 0;
649         }
650
651         *pfun = fun;
652         return 1;
653 }
654 #endif
655
656 #ifdef WITH_PROXY
657 /*
658  *      Recieve packets from a proxy socket.
659  */
660 static int proxy_socket_recv(rad_listen_t *listener,
661                               RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
662 {
663         REQUEST         *request;
664         RADIUS_PACKET   *packet;
665         RAD_REQUEST_FUNP fun = NULL;
666         char            buffer[128];
667
668         packet = rad_recv(listener->fd, 0);
669         if (!packet) {
670                 radlog(L_ERR, "%s", librad_errstr);
671                 return 0;
672         }
673
674         /*
675          *      FIXME: Client MIB updates?
676          */
677         switch(packet->code) {
678         case PW_AUTHENTICATION_ACK:
679         case PW_ACCESS_CHALLENGE:
680         case PW_AUTHENTICATION_REJECT:
681                 fun = rad_authenticate;
682                 break;
683
684 #ifdef WITH_ACCOUNTING
685         case PW_ACCOUNTING_RESPONSE:
686                 fun = rad_accounting;
687                 break;
688 #endif
689
690         default:
691                 /*
692                  *      FIXME: Update MIB for packet types?
693                  */
694                 radlog(L_ERR, "Invalid packet code %d sent to a proxy port "
695                        "from home server %s port %d - ID %d : IGNORED",
696                        packet->code,
697                        ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
698                        packet->src_port, packet->id);
699                 rad_free(&packet);
700                 return 0;
701         }
702
703         request = received_proxy_response(packet);
704         if (!request) {
705                 return 0;
706         }
707
708         rad_assert(fun != NULL);
709         *pfun = fun;
710         *prequest = request;
711
712         return 1;
713 }
714 #endif
715
716
717 static int client_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
718 {
719         if (!request->reply->code) return 0;
720
721         rad_encode(request->reply, request->packet,
722                    request->client->secret);
723         rad_sign(request->reply, request->packet,
724                  request->client->secret);
725
726         return 0;
727 }
728
729
730 static int client_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
731 {
732         if (rad_verify(request->packet, NULL,
733                        request->client->secret) < 0) {
734                 return -1;
735         }
736
737         return rad_decode(request->packet, NULL,
738                           request->client->secret);
739 }
740
741 #ifdef WITH_PROXY
742 static int proxy_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
743 {
744         rad_encode(request->proxy, NULL, request->home_server->secret);
745         rad_sign(request->proxy, NULL, request->home_server->secret);
746
747         return 0;
748 }
749
750
751 static int proxy_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
752 {
753         if (rad_verify(request->proxy_reply, request->proxy,
754                        request->home_server->secret) < 0) {
755                 return -1;
756         }
757
758         return rad_decode(request->proxy_reply, request->proxy,
759                            request->home_server->secret);
760 }
761 #endif
762
763 #ifdef WITH_SNMP
764 static int radius_snmp_recv(rad_listen_t *listener,
765                             UNUSED RAD_REQUEST_FUNP *pfun,
766                             UNUSED REQUEST **prequest)
767 {
768         if ((rad_snmp.smux_fd >= 0) &&
769             (rad_snmp.smux_event == SMUX_READ)) {
770                 smux_read();
771         }
772
773         /*
774          *  If we've got to re-connect, then do so now,
775          *  before calling select again.
776          */
777         if (rad_snmp.smux_event == SMUX_CONNECT) {
778                 smux_connect();
779         }
780
781         /*
782          *      Reset this every time, as the smux connect may have
783          *      opened a new socket.
784          */
785         listener->fd = rad_snmp.smux_fd;
786
787         return 0;
788 }
789
790
791 static int radius_snmp_print(UNUSED rad_listen_t *this, char *buffer, size_t bufsize)
792 {
793         return snprintf(buffer, bufsize, "SMUX with OID .1.3.6.1.4.1.11344.1.1.1");
794 }
795
796 #endif
797
798 #include "dhcpd.c"
799
800 static const rad_listen_master_t master_listen[RAD_LISTEN_MAX] = {
801         { NULL, NULL, NULL, NULL, NULL, NULL, NULL},    /* RAD_LISTEN_NONE */
802
803 #ifdef WITH_PROXY
804         /* proxying */
805         { common_socket_parse, NULL,
806           proxy_socket_recv, proxy_socket_send,
807           socket_print, proxy_socket_encode, proxy_socket_decode },
808 #else
809         { NULL, NULL, NULL, NULL, NULL, NULL, NULL},
810 #endif
811
812         /* authentication */
813         { common_socket_parse, NULL,
814           auth_socket_recv, auth_socket_send,
815           socket_print, client_socket_encode, client_socket_decode },
816
817 #ifdef WITH_ACCOUNTING
818         /* accounting */
819         { common_socket_parse, NULL,
820           acct_socket_recv, acct_socket_send,
821           socket_print, client_socket_encode, client_socket_decode},
822 #else
823         { NULL, NULL, NULL, NULL, NULL, NULL, NULL},
824 #endif
825
826 #ifdef WITH_DETAIL
827         /* detail */
828         { detail_parse, detail_free,
829           detail_recv, detail_send,
830           detail_print, detail_encode, detail_decode },
831 #else
832         { NULL, NULL, NULL, NULL, NULL, NULL, NULL},
833 #endif
834
835 #ifdef WITH_VMPS
836         /* vlan query protocol */
837         { common_socket_parse, NULL,
838           vqp_socket_recv, vqp_socket_send,
839           socket_print, vqp_socket_encode, vqp_socket_decode },
840 #else
841         { NULL, NULL, NULL, NULL, NULL, NULL, NULL},
842 #endif
843
844 #ifdef WITH_DHCP
845         /* dhcp query protocol */
846         { dhcp_socket_parse, NULL,
847           dhcp_socket_recv, dhcp_socket_send,
848           socket_print, dhcp_socket_encode, dhcp_socket_decode },
849 #else
850         { NULL, NULL, NULL, NULL, NULL, NULL, NULL},
851 #endif
852
853         { NULL, NULL, NULL, NULL, NULL, NULL, NULL}     /* RAD_LISTEN_SNMP */
854 };
855
856
857
858 /*
859  *      Binds a listener to a socket.
860  */
861 static int listen_bind(rad_listen_t *this)
862 {
863         listen_socket_t *sock = this->data;
864
865         /*
866          *      If the port is zero, then it means the appropriate
867          *      thing from /etc/services.
868          */
869         if (sock->port == 0) {
870                 struct servent  *svp;
871
872                 switch (this->type) {
873                 case RAD_LISTEN_AUTH:
874                         svp = getservbyname ("radius", "udp");
875                         if (svp != NULL) {
876                                 sock->port = ntohs(svp->s_port);
877                         } else {
878                                 sock->port = PW_AUTH_UDP_PORT;
879                         }
880                         break;
881
882 #ifdef WITH_ACCOUNTING
883                 case RAD_LISTEN_ACCT:
884                         svp = getservbyname ("radacct", "udp");
885                         if (svp != NULL) {
886                                 sock->port = ntohs(svp->s_port);
887                         } else {
888                                 sock->port = PW_ACCT_UDP_PORT;
889                         }
890                         break;
891 #endif
892
893 #ifdef WITH_PROXY
894                 case RAD_LISTEN_PROXY:
895                         sock->port = 0;
896                         break;
897 #endif
898
899 #ifdef WITH_VMPS
900                 case RAD_LISTEN_VQP:
901                         sock->port = 1589;
902                         break;
903 #endif
904
905                 default:
906                         radlog(L_ERR, "ERROR: Non-fatal internal sanity check failed in bind.");
907                         return -1;
908                 }
909         }
910
911         this->fd = fr_socket(&sock->ipaddr, sock->port);
912         if (this->fd < 0) {
913                 radlog(L_ERR, "ERROR: Failed to open socket: %s",
914                        librad_errstr);
915                 return -1;
916         }
917
918         /*
919          *      FreeBSD jail issues.  We bind to 0.0.0.0, but the
920          *      kernel instead binds us to a 1.2.3.4.  If this
921          *      happens, notice, and remember our real IP.
922          */
923         {
924                 struct sockaddr_storage src;
925                 socklen_t               sizeof_src = sizeof(src);
926
927                 memset(&src, 0, sizeof_src);
928                 if (getsockname(this->fd, (struct sockaddr *) &src,
929                                 &sizeof_src) < 0) {
930                         radlog(L_ERR, "Failed getting socket name: %s",
931                                strerror(errno));
932                         return -1;
933                 }
934
935                 if (src.ss_family == AF_INET) {
936                         struct sockaddr_in      s4;
937                         
938                         memcpy(&s4, &src, sizeof(s4));
939                         sock->ipaddr.ipaddr.ip4addr = s4.sin_addr;
940                         sock->port = ntohs(s4.sin_port);
941                         
942 #ifdef HAVE_STRUCT_SOCKADDR_IN6
943                 } else if (src.ss_family == AF_INET6) {
944                         struct sockaddr_in6     s6;
945                         
946                         memcpy(&s6, &src, sizeof(s6));
947                         sock->ipaddr.ipaddr.ip6addr = s6.sin6_addr;
948                         sock->port = ntohs(s6.sin6_port);
949 #endif
950                 } else {
951                         radlog(L_ERR, "Socket has unsupported address family");
952                         return -1;
953                 }
954         }
955
956 #ifdef O_NONBLOCK
957         {
958                 int flags;
959                 
960                 if ((flags = fcntl(this->fd, F_GETFL, NULL)) < 0)  {
961                         radlog(L_ERR, "Failure getting socket flags: %s)\n",
962                                strerror(errno));
963                         return -1;
964                 }
965                 
966                 flags |= O_NONBLOCK;
967                 if( fcntl(this->fd, F_SETFL, flags) < 0) {
968                         radlog(L_ERR, "Failure setting socket flags: %s)\n",
969                                strerror(errno));
970                         return -1;
971                 }
972         }
973 #endif
974
975         return 0;
976 }
977
978
979 /*
980  *      Allocate & initialize a new listener.
981  */
982 static rad_listen_t *listen_alloc(RAD_LISTEN_TYPE type)
983 {
984         rad_listen_t *this;
985
986         this = rad_malloc(sizeof(*this));
987         memset(this, 0, sizeof(*this));
988
989         this->type = type;
990         this->recv = master_listen[this->type].recv;
991         this->send = master_listen[this->type].send;
992         this->print = master_listen[this->type].print;
993         this->encode = master_listen[this->type].encode;
994         this->decode = master_listen[this->type].decode;
995
996         switch (type) {
997         case RAD_LISTEN_AUTH:
998         case RAD_LISTEN_ACCT:
999         case RAD_LISTEN_PROXY:
1000         case RAD_LISTEN_VQP:
1001         case RAD_LISTEN_DHCP:
1002                 this->data = rad_malloc(sizeof(listen_socket_t));
1003                 memset(this->data, 0, sizeof(listen_socket_t));
1004                 break;
1005
1006 #ifdef WITH_DETAIL
1007         case RAD_LISTEN_DETAIL:
1008                 this->data = NULL;
1009                 break;
1010 #endif
1011
1012         default:
1013                 rad_assert("Unsupported option!" == NULL);
1014                 break;
1015         }
1016
1017         return this;
1018 }
1019
1020
1021 #ifdef WITH_PROXY
1022 /*
1023  *      Externally visible function for creating a new proxy LISTENER.
1024  *
1025  *      For now, don't take ipaddr or port.
1026  *
1027  *      Not thread-safe, but all calls to it are protected by the
1028  *      proxy mutex in request_list.c
1029  */
1030 rad_listen_t *proxy_new_listener()
1031 {
1032         int last_proxy_port, port;
1033         rad_listen_t *this, *tmp, **last;
1034         listen_socket_t *sock, *old;
1035
1036         this = listen_alloc(RAD_LISTEN_PROXY);
1037
1038         /*
1039          *      Find an existing proxy socket to copy.
1040          *
1041          *      FIXME: Make it per-realm, or per-home server!
1042          */
1043         last_proxy_port = 0;
1044         old = NULL;
1045         last = &mainconfig.listen;
1046         for (tmp = mainconfig.listen; tmp != NULL; tmp = tmp->next) {
1047                 if (tmp->type == RAD_LISTEN_PROXY) {
1048                         sock = tmp->data;
1049                         if (sock->port > last_proxy_port) {
1050                                 last_proxy_port = sock->port + 1;
1051                         }
1052                         if (!old) old = sock;
1053                 }
1054
1055                 last = &(tmp->next);
1056         }
1057
1058         if (!old) {
1059                 listen_free(&this);
1060                 return NULL;    /* This is a serious error. */
1061         }
1062
1063         /*
1064          *      FIXME: find a new IP address to listen on?
1065          *
1066          *      This could likely be done in the "home server"
1067          *      configuration, to have per-home-server source IP's.
1068          */
1069         sock = this->data;
1070         memcpy(&sock->ipaddr, &old->ipaddr, sizeof(sock->ipaddr));
1071
1072         /*
1073          *      Keep going until we find an unused port.
1074          */
1075         for (port = last_proxy_port; port < 64000; port++) {
1076                 sock->port = port;
1077                 if (listen_bind(this) == 0) {
1078                         /*
1079                          *      Add the new listener to the list of
1080                          *      listeners.
1081                          */
1082                         *last = this;
1083                         return this;
1084                 }
1085         }
1086
1087         listen_free(&this);
1088         return NULL;
1089 }
1090 #endif
1091
1092 static const FR_NAME_NUMBER listen_compare[] = {
1093         { "auth",       RAD_LISTEN_AUTH },
1094 #ifdef WITH_ACCOUNTING
1095         { "acct",       RAD_LISTEN_ACCT },
1096 #endif
1097 #ifdef WITH_DETAIL
1098         { "detail",     RAD_LISTEN_DETAIL },
1099 #endif
1100 #ifdef WITH_PROXY
1101         { "proxy",      RAD_LISTEN_PROXY },
1102 #endif
1103 #ifdef WITH_VMPS
1104         { "vmps",       RAD_LISTEN_VQP },
1105 #endif
1106 #ifdef WITH_DHCP
1107         { "dhcp",       RAD_LISTEN_DHCP },
1108 #else
1109         { "dhcp",       RAD_LISTEN_NONE },
1110 #endif
1111         { NULL, 0 },
1112 };
1113
1114
1115 static rad_listen_t *listen_parse(CONF_SECTION *cs, const char *server)
1116 {
1117         int             type, rcode;
1118         char            *listen_type;
1119         rad_listen_t    *this;
1120
1121         listen_type = NULL;
1122         
1123         cf_log_info(cs, "listen {");
1124
1125         rcode = cf_item_parse(cs, "type", PW_TYPE_STRING_PTR,
1126                               &listen_type, "");
1127         if (rcode < 0) return NULL;
1128         if (rcode == 1) {
1129                 free(listen_type);
1130                 cf_log_err(cf_sectiontoitem(cs),
1131                            "No type specified in listen section");
1132                 return NULL;
1133         }
1134
1135         type = fr_str2int(listen_compare, listen_type,
1136                             RAD_LISTEN_NONE);
1137         if (type == RAD_LISTEN_NONE) {
1138                 cf_log_err(cf_sectiontoitem(cs),
1139                            "Invalid type \"%s\" in listen section.",
1140                            listen_type);
1141                 free(listen_type);
1142                 return NULL;
1143         }
1144         free(listen_type);
1145         
1146         /*
1147          *      Allow listen sections in the default config to
1148          *      refer to a server.
1149          */
1150         if (!server) {
1151                 rcode = cf_item_parse(cs, "virtual_server", PW_TYPE_STRING_PTR,
1152                                       &server, NULL);
1153                 if (rcode == 1) { /* compatiblity with 2.0-pre */
1154                         rcode = cf_item_parse(cs, "server", PW_TYPE_STRING_PTR,
1155                                               &server, NULL);
1156                 }
1157                 if (rcode < 0) return NULL;
1158         }
1159
1160         /*
1161          *      Set up cross-type data.
1162          */
1163         this = listen_alloc(type);
1164         this->server = server;
1165         this->fd = -1;
1166
1167         /*
1168          *      Call per-type parser.
1169          */
1170         if (master_listen[type].parse(cs, this) < 0) {
1171                 listen_free(&this);
1172                 return NULL;
1173         }
1174
1175         cf_log_info(cs, "}");
1176
1177         return this;
1178 }
1179
1180 /*
1181  *      Generate a list of listeners.  Takes an input list of
1182  *      listeners, too, so we don't close sockets with waiting packets.
1183  */
1184 int listen_init(CONF_SECTION *config, rad_listen_t **head)
1185 {
1186         int             override = FALSE;
1187         int             rcode;
1188         CONF_SECTION    *cs;
1189         rad_listen_t    **last;
1190         rad_listen_t    *this;
1191         fr_ipaddr_t     server_ipaddr;
1192         int             auth_port = 0;
1193 #ifdef WITH_PROXY
1194         int             defined_proxy = 0;
1195 #endif
1196
1197         /*
1198          *      We shouldn't be called with a pre-existing list.
1199          */
1200         rad_assert(head && (*head == NULL));
1201
1202         last = head;
1203         server_ipaddr.af = AF_UNSPEC;
1204
1205         /*
1206          *      If the port is specified on the command-line,
1207          *      it over-rides the configuration file.
1208          *
1209          *      FIXME: If argv[0] == "vmpsd", then don't listen on auth/acct!
1210          */
1211         if (mainconfig.port >= 0) auth_port = mainconfig.port;
1212
1213         /*
1214          *      If the IP address was configured on the command-line,
1215          *      use that as the "bind_address"
1216          */
1217         if (mainconfig.myip.af != AF_UNSPEC) {
1218                 memcpy(&server_ipaddr, &mainconfig.myip,
1219                        sizeof(server_ipaddr));
1220                 override = TRUE;
1221                 goto bind_it;
1222         }
1223
1224         /*
1225          *      Else look for bind_address and/or listen sections.
1226          */
1227         server_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
1228         rcode = cf_item_parse(config, "bind_address",
1229                               PW_TYPE_IPADDR,
1230                               &server_ipaddr.ipaddr.ip4addr, NULL);
1231         if (rcode < 0) return -1; /* error parsing it */
1232
1233         if (rcode == 0) { /* successfully parsed IPv4 */
1234                 listen_socket_t *sock;
1235                 server_ipaddr.af = AF_INET;
1236
1237                 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'.");
1238
1239         bind_it:
1240 #ifdef WITH_VMPS
1241                 if (strcmp(progname, "vmpsd") == 0) {
1242                         this = listen_alloc(RAD_LISTEN_VQP);
1243                         if (!auth_port) auth_port = 1589;
1244                 } else
1245 #endif
1246                         this = listen_alloc(RAD_LISTEN_AUTH);
1247
1248                 sock = this->data;
1249
1250                 sock->ipaddr = server_ipaddr;
1251                 sock->port = auth_port;
1252
1253                 sock->clients = clients_parse_section(config);
1254                 if (!sock->clients) {
1255                         cf_log_err(cf_sectiontoitem(config),
1256                                    "Failed to find any clients for this listen section");
1257                         return -1;
1258                 }
1259
1260                 if (listen_bind(this) < 0) {
1261                         listen_free(&this);
1262                         listen_free(head);
1263                         radlog(L_ERR, "There appears to be another RADIUS server running on the authentication port %d", sock->port);
1264                         return -1;
1265                 }
1266                 auth_port = sock->port; /* may have been updated in listen_bind */
1267                 if (override) {
1268                         cs = cf_section_sub_find_name2(config, "server",
1269                                                        mainconfig.name);
1270                         if (cs) this->server = mainconfig.name;
1271                 }
1272
1273                 *last = this;
1274                 last = &(this->next);
1275
1276 #ifdef WITH_VMPS
1277                 /*
1278                  *      No acct for vmpsd
1279                  */
1280                 if (strcmp(progname, "vmpsd") == 0) goto do_proxy;
1281 #endif
1282
1283 #ifdef WITH_ACCOUNTING
1284                 /*
1285                  *      Open Accounting Socket.
1286                  *
1287                  *      If we haven't already gotten acct_port from
1288                  *      /etc/services, then make it auth_port + 1.
1289                  */
1290                 this = listen_alloc(RAD_LISTEN_ACCT);
1291                 sock = this->data;
1292
1293                 /*
1294                  *      Create the accounting socket.
1295                  *
1296                  *      The accounting port is always the
1297                  *      authentication port + 1
1298                  */
1299                 sock->ipaddr = server_ipaddr;
1300                 sock->port = auth_port + 1;
1301
1302                 sock->clients = clients_parse_section(config);
1303                 if (!sock->clients) {
1304                         cf_log_err(cf_sectiontoitem(config),
1305                                    "Failed to find any clients for this listen section");
1306                         return -1;
1307                 }
1308
1309                 if (listen_bind(this) < 0) {
1310                         listen_free(&this);
1311                         listen_free(head);
1312                         radlog(L_ERR, "There appears to be another RADIUS server running on the accounting port %d", sock->port);
1313                         return -1;
1314                 }
1315
1316                 if (override) {
1317                         cs = cf_section_sub_find_name2(config, "server",
1318                                                        mainconfig.name);
1319                         if (cs) this->server = mainconfig.name;
1320                 }
1321
1322                 *last = this;
1323                 last = &(this->next);
1324 #endif
1325         } else if (mainconfig.port > 0) { /* no bind address, but a port */
1326                 radlog(L_ERR, "The command-line says \"-p %d\", but there is no associated IP address to use",
1327                        mainconfig.port);
1328                 return -1;
1329         }
1330
1331         /*
1332          *      They specified an IP on the command-line, ignore
1333          *      all listen sections except the one in '-n'.
1334          */
1335         if (mainconfig.myip.af != AF_UNSPEC) {
1336                 CONF_SECTION *subcs;
1337                 const char *name2 = cf_section_name2(cs);
1338
1339                 cs = cf_section_sub_find_name2(config, "server",
1340                                                mainconfig.name);
1341                 if (!cs) goto do_proxy;
1342
1343                 /*
1344                  *      Should really abstract this code...
1345                  */
1346                 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
1347                      subcs != NULL;
1348                      subcs = cf_subsection_find_next(cs, subcs, "listen")) {
1349                         this = listen_parse(subcs, name2);
1350                         if (!this) {
1351                                 listen_free(head);
1352                                 return -1;
1353                         }
1354
1355 #ifdef WITH_PROXY
1356                         if (this->type == RAD_LISTEN_PROXY) defined_proxy = 1;
1357 #endif
1358                         
1359                         *last = this;
1360                         last = &(this->next);
1361                 } /* loop over "listen" directives in server <foo> */
1362
1363                 goto do_proxy;
1364         }
1365
1366         /*
1367          *      Walk through the "listen" sections, if they exist.
1368          */
1369         for (cs = cf_subsection_find_next(config, NULL, "listen");
1370              cs != NULL;
1371              cs = cf_subsection_find_next(config, cs, "listen")) {
1372                 this = listen_parse(cs, NULL);
1373                 if (!this) {
1374                         listen_free(head);
1375                         return -1;
1376                 }
1377
1378 #ifdef WITH_PROXY
1379                 if (this->type == RAD_LISTEN_PROXY) defined_proxy = 1;
1380 #endif
1381
1382                 *last = this;
1383                 last = &(this->next);
1384         }
1385
1386         /*
1387          *      Check virtual servers for "listen" sections, too.
1388          *
1389          *      FIXME: Move to virtual server init?
1390          */
1391         for (cs = cf_subsection_find_next(config, NULL, "server");
1392              cs != NULL;
1393              cs = cf_subsection_find_next(config, cs, "server")) {
1394                 CONF_SECTION *subcs;
1395                 const char *name2 = cf_section_name2(cs);
1396                 
1397                 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
1398                      subcs != NULL;
1399                      subcs = cf_subsection_find_next(cs, subcs, "listen")) {
1400                         this = listen_parse(subcs, name2);
1401                         if (!this) {
1402                                 listen_free(head);
1403                                 return -1;
1404                         }
1405                         
1406 #ifdef WITH_PROXY
1407                         if (this->type == RAD_LISTEN_PROXY) {
1408                                 radlog(L_ERR, "Error: listen type \"proxy\" Cannot appear in a virtual server section");
1409                                 listen_free(head);
1410                                 return -1;
1411                         }
1412 #endif
1413
1414                         *last = this;
1415                         last = &(this->next);
1416                 } /* loop over "listen" directives in virtual servers */
1417         } /* loop over virtual servers */
1418
1419         /*
1420          *      If we're proxying requests, open the proxy FD.
1421          *      Otherwise, don't do anything.
1422          */
1423  do_proxy:
1424 #ifdef WITH_PROXY
1425         if (mainconfig.proxy_requests == TRUE) {
1426                 int             port = -1;
1427                 listen_socket_t *sock = NULL;
1428
1429                 /*
1430                  *      No sockets to receive packets, therefore
1431                  *      proxying is pointless.
1432                  */
1433                 if (!*head) return -1;
1434
1435                 if (defined_proxy) goto do_snmp;
1436
1437                 /*
1438                  *      Find the first authentication port,
1439                  *      and use it
1440                  */
1441                 for (this = *head; this != NULL; this = this->next) {
1442                         if (this->type == RAD_LISTEN_AUTH) {
1443                                 sock = this->data;
1444                                 if (server_ipaddr.af == AF_UNSPEC) {
1445                                         server_ipaddr = sock->ipaddr;
1446                                 }
1447                                 port = sock->port + 2; /* skip acct port */
1448                                 break;
1449                         }
1450                         if (this->type == RAD_LISTEN_VQP) {
1451                                 sock = this->data;
1452                                 if (server_ipaddr.af == AF_UNSPEC) {
1453                                         server_ipaddr = sock->ipaddr;
1454                                 }
1455                                 port = sock->port + 1;
1456                                 break;
1457                         }
1458                 }
1459
1460                 if (port < 0) port = 1024 + (fr_rand() & 0x1ff);
1461
1462                 /*
1463                  *      Address is still unspecified, use IPv4.
1464                  */
1465                 if (server_ipaddr.af == AF_UNSPEC) {
1466                         server_ipaddr.af = AF_INET;
1467                         server_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_ANY);
1468                 }
1469
1470                 this = listen_alloc(RAD_LISTEN_PROXY);
1471                 sock = this->data;
1472
1473                 /*
1474                  *      Create the first proxy socket.
1475                  */
1476                 sock->ipaddr = server_ipaddr;
1477
1478                 /*
1479                  *      Try to find a proxy port (value doesn't matter)
1480                  */
1481                 for (sock->port = port;
1482                      sock->port < 64000;
1483                      sock->port++) {
1484                         if (listen_bind(this) == 0) {
1485                                 *last = this;
1486                                 last = &(this->next); /* just in case */
1487                                 break;
1488                         }
1489                 }
1490
1491                 if (sock->port >= 64000) {
1492                         listen_free(head);
1493                         listen_free(&this);
1494                         radlog(L_ERR, "Failed to open socket for proxying");
1495                         return -1;
1496                 }
1497         }
1498
1499  do_snmp:                       /* used only in proxy code. */
1500 #endif
1501
1502 #ifdef WITH_SNMP
1503         if (radius_snmp_init(config)) {
1504                 this = rad_malloc(sizeof(*this));
1505                 memset(this, 0, sizeof(*this));
1506
1507                 this->type = RAD_LISTEN_SNMP;
1508                 this->fd = rad_snmp.smux_fd;
1509
1510                 this->recv = radius_snmp_recv;
1511                 this->print = radius_snmp_print;
1512
1513                 *last = this;
1514                 last = &(this->next);
1515         }
1516 #endif
1517
1518         return 0;
1519 }
1520
1521 /*
1522  *      Free a linked list of listeners;
1523  */
1524 void listen_free(rad_listen_t **head)
1525 {
1526         rad_listen_t *this;
1527
1528         if (!head || !*head) return;
1529
1530         this = *head;
1531         while (this) {
1532                 rad_listen_t *next = this->next;
1533
1534                 /*
1535                  *      Other code may have eaten the FD.
1536                  */
1537                 if (this->fd >= 0) close(this->fd);
1538
1539                 if (master_listen[this->type].free) {
1540                         master_listen[this->type].free(this);
1541                 }
1542                 free(this->data);
1543                 free(this);
1544
1545                 this = next;
1546         }
1547
1548         *head = NULL;
1549 }