LCOV - code coverage report
Current view: top level - src/radius - radius_server.c (source / functions) Hit Total Coverage
Test: hostapd hwsim test run 1388338050 Lines: 3 635 0.5 %
Date: 2013-12-29 Functions: 1 24 4.2 %
Branches: 1 316 0.3 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * RADIUS authentication server
       3                 :            :  * Copyright (c) 2005-2009, 2011, Jouni Malinen <j@w1.fi>
       4                 :            :  *
       5                 :            :  * This software may be distributed under the terms of the BSD license.
       6                 :            :  * See README for more details.
       7                 :            :  */
       8                 :            : 
       9                 :            : #include "includes.h"
      10                 :            : #include <net/if.h>
      11                 :            : 
      12                 :            : #include "common.h"
      13                 :            : #include "radius.h"
      14                 :            : #include "eloop.h"
      15                 :            : #include "eap_server/eap.h"
      16                 :            : #include "radius_server.h"
      17                 :            : 
      18                 :            : /**
      19                 :            :  * RADIUS_SESSION_TIMEOUT - Session timeout in seconds
      20                 :            :  */
      21                 :            : #define RADIUS_SESSION_TIMEOUT 60
      22                 :            : 
      23                 :            : /**
      24                 :            :  * RADIUS_MAX_SESSION - Maximum number of active sessions
      25                 :            :  */
      26                 :            : #define RADIUS_MAX_SESSION 100
      27                 :            : 
      28                 :            : /**
      29                 :            :  * RADIUS_MAX_MSG_LEN - Maximum message length for incoming RADIUS messages
      30                 :            :  */
      31                 :            : #define RADIUS_MAX_MSG_LEN 3000
      32                 :            : 
      33                 :            : static struct eapol_callbacks radius_server_eapol_cb;
      34                 :            : 
      35                 :            : struct radius_client;
      36                 :            : struct radius_server_data;
      37                 :            : 
      38                 :            : /**
      39                 :            :  * struct radius_server_counters - RADIUS server statistics counters
      40                 :            :  */
      41                 :            : struct radius_server_counters {
      42                 :            :         u32 access_requests;
      43                 :            :         u32 invalid_requests;
      44                 :            :         u32 dup_access_requests;
      45                 :            :         u32 access_accepts;
      46                 :            :         u32 access_rejects;
      47                 :            :         u32 access_challenges;
      48                 :            :         u32 malformed_access_requests;
      49                 :            :         u32 bad_authenticators;
      50                 :            :         u32 packets_dropped;
      51                 :            :         u32 unknown_types;
      52                 :            : };
      53                 :            : 
      54                 :            : /**
      55                 :            :  * struct radius_session - Internal RADIUS server data for a session
      56                 :            :  */
      57                 :            : struct radius_session {
      58                 :            :         struct radius_session *next;
      59                 :            :         struct radius_client *client;
      60                 :            :         struct radius_server_data *server;
      61                 :            :         unsigned int sess_id;
      62                 :            :         struct eap_sm *eap;
      63                 :            :         struct eap_eapol_interface *eap_if;
      64                 :            : 
      65                 :            :         struct radius_msg *last_msg;
      66                 :            :         char *last_from_addr;
      67                 :            :         int last_from_port;
      68                 :            :         struct sockaddr_storage last_from;
      69                 :            :         socklen_t last_fromlen;
      70                 :            :         u8 last_identifier;
      71                 :            :         struct radius_msg *last_reply;
      72                 :            :         u8 last_authenticator[16];
      73                 :            : };
      74                 :            : 
      75                 :            : /**
      76                 :            :  * struct radius_client - Internal RADIUS server data for a client
      77                 :            :  */
      78                 :            : struct radius_client {
      79                 :            :         struct radius_client *next;
      80                 :            :         struct in_addr addr;
      81                 :            :         struct in_addr mask;
      82                 :            : #ifdef CONFIG_IPV6
      83                 :            :         struct in6_addr addr6;
      84                 :            :         struct in6_addr mask6;
      85                 :            : #endif /* CONFIG_IPV6 */
      86                 :            :         char *shared_secret;
      87                 :            :         int shared_secret_len;
      88                 :            :         struct radius_session *sessions;
      89                 :            :         struct radius_server_counters counters;
      90                 :            : };
      91                 :            : 
      92                 :            : /**
      93                 :            :  * struct radius_server_data - Internal RADIUS server data
      94                 :            :  */
      95                 :            : struct radius_server_data {
      96                 :            :         /**
      97                 :            :          * auth_sock - Socket for RADIUS authentication messages
      98                 :            :          */
      99                 :            :         int auth_sock;
     100                 :            : 
     101                 :            :         /**
     102                 :            :          * clients - List of authorized RADIUS clients
     103                 :            :          */
     104                 :            :         struct radius_client *clients;
     105                 :            : 
     106                 :            :         /**
     107                 :            :          * next_sess_id - Next session identifier
     108                 :            :          */
     109                 :            :         unsigned int next_sess_id;
     110                 :            : 
     111                 :            :         /**
     112                 :            :          * conf_ctx - Context pointer for callbacks
     113                 :            :          *
     114                 :            :          * This is used as the ctx argument in get_eap_user() calls.
     115                 :            :          */
     116                 :            :         void *conf_ctx;
     117                 :            : 
     118                 :            :         /**
     119                 :            :          * num_sess - Number of active sessions
     120                 :            :          */
     121                 :            :         int num_sess;
     122                 :            : 
     123                 :            :         /**
     124                 :            :          * eap_sim_db_priv - EAP-SIM/AKA database context
     125                 :            :          *
     126                 :            :          * This is passed to the EAP-SIM/AKA server implementation as a
     127                 :            :          * callback context.
     128                 :            :          */
     129                 :            :         void *eap_sim_db_priv;
     130                 :            : 
     131                 :            :         /**
     132                 :            :          * ssl_ctx - TLS context
     133                 :            :          *
     134                 :            :          * This is passed to the EAP server implementation as a callback
     135                 :            :          * context for TLS operations.
     136                 :            :          */
     137                 :            :         void *ssl_ctx;
     138                 :            : 
     139                 :            :         /**
     140                 :            :          * pac_opaque_encr_key - PAC-Opaque encryption key for EAP-FAST
     141                 :            :          *
     142                 :            :          * This parameter is used to set a key for EAP-FAST to encrypt the
     143                 :            :          * PAC-Opaque data. It can be set to %NULL if EAP-FAST is not used. If
     144                 :            :          * set, must point to a 16-octet key.
     145                 :            :          */
     146                 :            :         u8 *pac_opaque_encr_key;
     147                 :            : 
     148                 :            :         /**
     149                 :            :          * eap_fast_a_id - EAP-FAST authority identity (A-ID)
     150                 :            :          *
     151                 :            :          * If EAP-FAST is not used, this can be set to %NULL. In theory, this
     152                 :            :          * is a variable length field, but due to some existing implementations
     153                 :            :          * requiring A-ID to be 16 octets in length, it is recommended to use
     154                 :            :          * that length for the field to provide interoperability with deployed
     155                 :            :          * peer implementations.
     156                 :            :          */
     157                 :            :         u8 *eap_fast_a_id;
     158                 :            : 
     159                 :            :         /**
     160                 :            :          * eap_fast_a_id_len - Length of eap_fast_a_id buffer in octets
     161                 :            :          */
     162                 :            :         size_t eap_fast_a_id_len;
     163                 :            : 
     164                 :            :         /**
     165                 :            :          * eap_fast_a_id_info - EAP-FAST authority identifier information
     166                 :            :          *
     167                 :            :          * This A-ID-Info contains a user-friendly name for the A-ID. For
     168                 :            :          * example, this could be the enterprise and server names in
     169                 :            :          * human-readable format. This field is encoded as UTF-8. If EAP-FAST
     170                 :            :          * is not used, this can be set to %NULL.
     171                 :            :          */
     172                 :            :         char *eap_fast_a_id_info;
     173                 :            : 
     174                 :            :         /**
     175                 :            :          * eap_fast_prov - EAP-FAST provisioning modes
     176                 :            :          *
     177                 :            :          * 0 = provisioning disabled, 1 = only anonymous provisioning allowed,
     178                 :            :          * 2 = only authenticated provisioning allowed, 3 = both provisioning
     179                 :            :          * modes allowed.
     180                 :            :          */
     181                 :            :         int eap_fast_prov;
     182                 :            : 
     183                 :            :         /**
     184                 :            :          * pac_key_lifetime - EAP-FAST PAC-Key lifetime in seconds
     185                 :            :          *
     186                 :            :          * This is the hard limit on how long a provisioned PAC-Key can be
     187                 :            :          * used.
     188                 :            :          */
     189                 :            :         int pac_key_lifetime;
     190                 :            : 
     191                 :            :         /**
     192                 :            :          * pac_key_refresh_time - EAP-FAST PAC-Key refresh time in seconds
     193                 :            :          *
     194                 :            :          * This is a soft limit on the PAC-Key. The server will automatically
     195                 :            :          * generate a new PAC-Key when this number of seconds (or fewer) of the
     196                 :            :          * lifetime remains.
     197                 :            :          */
     198                 :            :         int pac_key_refresh_time;
     199                 :            : 
     200                 :            :         /**
     201                 :            :          * eap_sim_aka_result_ind - EAP-SIM/AKA protected success indication
     202                 :            :          *
     203                 :            :          * This controls whether the protected success/failure indication
     204                 :            :          * (AT_RESULT_IND) is used with EAP-SIM and EAP-AKA.
     205                 :            :          */
     206                 :            :         int eap_sim_aka_result_ind;
     207                 :            : 
     208                 :            :         /**
     209                 :            :          * tnc - Trusted Network Connect (TNC)
     210                 :            :          *
     211                 :            :          * This controls whether TNC is enabled and will be required before the
     212                 :            :          * peer is allowed to connect. Note: This is only used with EAP-TTLS
     213                 :            :          * and EAP-FAST. If any other EAP method is enabled, the peer will be
     214                 :            :          * allowed to connect without TNC.
     215                 :            :          */
     216                 :            :         int tnc;
     217                 :            : 
     218                 :            :         /**
     219                 :            :          * pwd_group - The D-H group assigned for EAP-pwd
     220                 :            :          *
     221                 :            :          * If EAP-pwd is not used it can be set to zero.
     222                 :            :          */
     223                 :            :         u16 pwd_group;
     224                 :            : 
     225                 :            :         /**
     226                 :            :          * server_id - Server identity
     227                 :            :          */
     228                 :            :         const char *server_id;
     229                 :            : 
     230                 :            :         /**
     231                 :            :          * wps - Wi-Fi Protected Setup context
     232                 :            :          *
     233                 :            :          * If WPS is used with an external RADIUS server (which is quite
     234                 :            :          * unlikely configuration), this is used to provide a pointer to WPS
     235                 :            :          * context data. Normally, this can be set to %NULL.
     236                 :            :          */
     237                 :            :         struct wps_context *wps;
     238                 :            : 
     239                 :            :         /**
     240                 :            :          * ipv6 - Whether to enable IPv6 support in the RADIUS server
     241                 :            :          */
     242                 :            :         int ipv6;
     243                 :            : 
     244                 :            :         /**
     245                 :            :          * start_time - Timestamp of server start
     246                 :            :          */
     247                 :            :         struct os_reltime start_time;
     248                 :            : 
     249                 :            :         /**
     250                 :            :          * counters - Statistics counters for server operations
     251                 :            :          *
     252                 :            :          * These counters are the sum over all clients.
     253                 :            :          */
     254                 :            :         struct radius_server_counters counters;
     255                 :            : 
     256                 :            :         /**
     257                 :            :          * get_eap_user - Callback for fetching EAP user information
     258                 :            :          * @ctx: Context data from conf_ctx
     259                 :            :          * @identity: User identity
     260                 :            :          * @identity_len: identity buffer length in octets
     261                 :            :          * @phase2: Whether this is for Phase 2 identity
     262                 :            :          * @user: Data structure for filling in the user information
     263                 :            :          * Returns: 0 on success, -1 on failure
     264                 :            :          *
     265                 :            :          * This is used to fetch information from user database. The callback
     266                 :            :          * will fill in information about allowed EAP methods and the user
     267                 :            :          * password. The password field will be an allocated copy of the
     268                 :            :          * password data and RADIUS server will free it after use.
     269                 :            :          */
     270                 :            :         int (*get_eap_user)(void *ctx, const u8 *identity, size_t identity_len,
     271                 :            :                             int phase2, struct eap_user *user);
     272                 :            : 
     273                 :            :         /**
     274                 :            :          * eap_req_id_text - Optional data for EAP-Request/Identity
     275                 :            :          *
     276                 :            :          * This can be used to configure an optional, displayable message that
     277                 :            :          * will be sent in EAP-Request/Identity. This string can contain an
     278                 :            :          * ASCII-0 character (nul) to separate network infromation per RFC
     279                 :            :          * 4284. The actual string length is explicit provided in
     280                 :            :          * eap_req_id_text_len since nul character will not be used as a string
     281                 :            :          * terminator.
     282                 :            :          */
     283                 :            :         char *eap_req_id_text;
     284                 :            : 
     285                 :            :         /**
     286                 :            :          * eap_req_id_text_len - Length of eap_req_id_text buffer in octets
     287                 :            :          */
     288                 :            :         size_t eap_req_id_text_len;
     289                 :            : 
     290                 :            :         /*
     291                 :            :          * msg_ctx - Context data for wpa_msg() calls
     292                 :            :          */
     293                 :            :         void *msg_ctx;
     294                 :            : 
     295                 :            : #ifdef CONFIG_RADIUS_TEST
     296                 :            :         char *dump_msk_file;
     297                 :            : #endif /* CONFIG_RADIUS_TEST */
     298                 :            : };
     299                 :            : 
     300                 :            : 
     301                 :            : extern int wpa_debug_level;
     302                 :            : 
     303                 :            : #define RADIUS_DEBUG(args...) \
     304                 :            : wpa_printf(MSG_DEBUG, "RADIUS SRV: " args)
     305                 :            : #define RADIUS_ERROR(args...) \
     306                 :            : wpa_printf(MSG_ERROR, "RADIUS SRV: " args)
     307                 :            : #define RADIUS_DUMP(args...) \
     308                 :            : wpa_hexdump(MSG_MSGDUMP, "RADIUS SRV: " args)
     309                 :            : #define RADIUS_DUMP_ASCII(args...) \
     310                 :            : wpa_hexdump_ascii(MSG_MSGDUMP, "RADIUS SRV: " args)
     311                 :            : 
     312                 :            : 
     313                 :            : static void radius_server_session_timeout(void *eloop_ctx, void *timeout_ctx);
     314                 :            : static void radius_server_session_remove_timeout(void *eloop_ctx,
     315                 :            :                                                  void *timeout_ctx);
     316                 :            : 
     317                 :            : 
     318                 :            : static struct radius_client *
     319                 :          0 : radius_server_get_client(struct radius_server_data *data, struct in_addr *addr,
     320                 :            :                          int ipv6)
     321                 :            : {
     322                 :          0 :         struct radius_client *client = data->clients;
     323                 :            : 
     324         [ #  # ]:          0 :         while (client) {
     325                 :            : #ifdef CONFIG_IPV6
     326         [ #  # ]:          0 :                 if (ipv6) {
     327                 :            :                         struct in6_addr *addr6;
     328                 :            :                         int i;
     329                 :            : 
     330                 :          0 :                         addr6 = (struct in6_addr *) addr;
     331         [ #  # ]:          0 :                         for (i = 0; i < 16; i++) {
     332         [ #  # ]:          0 :                                 if ((addr6->s6_addr[i] &
     333                 :          0 :                                      client->mask6.s6_addr[i]) !=
     334                 :          0 :                                     (client->addr6.s6_addr[i] &
     335                 :            :                                      client->mask6.s6_addr[i])) {
     336                 :          0 :                                         i = 17;
     337                 :          0 :                                         break;
     338                 :            :                                 }
     339                 :            :                         }
     340         [ #  # ]:          0 :                         if (i == 16) {
     341                 :          0 :                                 break;
     342                 :            :                         }
     343                 :            :                 }
     344                 :            : #endif /* CONFIG_IPV6 */
     345 [ #  # ][ #  # ]:          0 :                 if (!ipv6 && (client->addr.s_addr & client->mask.s_addr) ==
     346                 :          0 :                     (addr->s_addr & client->mask.s_addr)) {
     347                 :          0 :                         break;
     348                 :            :                 }
     349                 :            : 
     350                 :          0 :                 client = client->next;
     351                 :            :         }
     352                 :            : 
     353                 :          0 :         return client;
     354                 :            : }
     355                 :            : 
     356                 :            : 
     357                 :            : static struct radius_session *
     358                 :          0 : radius_server_get_session(struct radius_client *client, unsigned int sess_id)
     359                 :            : {
     360                 :          0 :         struct radius_session *sess = client->sessions;
     361                 :            : 
     362         [ #  # ]:          0 :         while (sess) {
     363         [ #  # ]:          0 :                 if (sess->sess_id == sess_id) {
     364                 :          0 :                         break;
     365                 :            :                 }
     366                 :          0 :                 sess = sess->next;
     367                 :            :         }
     368                 :            : 
     369                 :          0 :         return sess;
     370                 :            : }
     371                 :            : 
     372                 :            : 
     373                 :          0 : static void radius_server_session_free(struct radius_server_data *data,
     374                 :            :                                        struct radius_session *sess)
     375                 :            : {
     376                 :          0 :         eloop_cancel_timeout(radius_server_session_timeout, data, sess);
     377                 :          0 :         eloop_cancel_timeout(radius_server_session_remove_timeout, data, sess);
     378                 :          0 :         eap_server_sm_deinit(sess->eap);
     379                 :          0 :         radius_msg_free(sess->last_msg);
     380                 :          0 :         os_free(sess->last_from_addr);
     381                 :          0 :         radius_msg_free(sess->last_reply);
     382                 :          0 :         os_free(sess);
     383                 :          0 :         data->num_sess--;
     384                 :          0 : }
     385                 :            : 
     386                 :            : 
     387                 :          0 : static void radius_server_session_remove(struct radius_server_data *data,
     388                 :            :                                          struct radius_session *sess)
     389                 :            : {
     390                 :          0 :         struct radius_client *client = sess->client;
     391                 :            :         struct radius_session *session, *prev;
     392                 :            : 
     393                 :          0 :         eloop_cancel_timeout(radius_server_session_remove_timeout, data, sess);
     394                 :            : 
     395                 :          0 :         prev = NULL;
     396                 :          0 :         session = client->sessions;
     397         [ #  # ]:          0 :         while (session) {
     398         [ #  # ]:          0 :                 if (session == sess) {
     399         [ #  # ]:          0 :                         if (prev == NULL) {
     400                 :          0 :                                 client->sessions = sess->next;
     401                 :            :                         } else {
     402                 :          0 :                                 prev->next = sess->next;
     403                 :            :                         }
     404                 :          0 :                         radius_server_session_free(data, sess);
     405                 :          0 :                         break;
     406                 :            :                 }
     407                 :          0 :                 prev = session;
     408                 :          0 :                 session = session->next;
     409                 :            :         }
     410                 :          0 : }
     411                 :            : 
     412                 :            : 
     413                 :          0 : static void radius_server_session_remove_timeout(void *eloop_ctx,
     414                 :            :                                                  void *timeout_ctx)
     415                 :            : {
     416                 :          0 :         struct radius_server_data *data = eloop_ctx;
     417                 :          0 :         struct radius_session *sess = timeout_ctx;
     418                 :          0 :         RADIUS_DEBUG("Removing completed session 0x%x", sess->sess_id);
     419                 :          0 :         radius_server_session_remove(data, sess);
     420                 :          0 : }
     421                 :            : 
     422                 :            : 
     423                 :          0 : static void radius_server_session_timeout(void *eloop_ctx, void *timeout_ctx)
     424                 :            : {
     425                 :          0 :         struct radius_server_data *data = eloop_ctx;
     426                 :          0 :         struct radius_session *sess = timeout_ctx;
     427                 :            : 
     428                 :          0 :         RADIUS_DEBUG("Timing out authentication session 0x%x", sess->sess_id);
     429                 :          0 :         radius_server_session_remove(data, sess);
     430                 :          0 : }
     431                 :            : 
     432                 :            : 
     433                 :            : static struct radius_session *
     434                 :          0 : radius_server_new_session(struct radius_server_data *data,
     435                 :            :                           struct radius_client *client)
     436                 :            : {
     437                 :            :         struct radius_session *sess;
     438                 :            : 
     439         [ #  # ]:          0 :         if (data->num_sess >= RADIUS_MAX_SESSION) {
     440                 :          0 :                 RADIUS_DEBUG("Maximum number of existing session - no room "
     441                 :            :                              "for a new session");
     442                 :          0 :                 return NULL;
     443                 :            :         }
     444                 :            : 
     445                 :          0 :         sess = os_zalloc(sizeof(*sess));
     446         [ #  # ]:          0 :         if (sess == NULL)
     447                 :          0 :                 return NULL;
     448                 :            : 
     449                 :          0 :         sess->server = data;
     450                 :          0 :         sess->client = client;
     451                 :          0 :         sess->sess_id = data->next_sess_id++;
     452                 :          0 :         sess->next = client->sessions;
     453                 :          0 :         client->sessions = sess;
     454                 :          0 :         eloop_register_timeout(RADIUS_SESSION_TIMEOUT, 0,
     455                 :            :                                radius_server_session_timeout, data, sess);
     456                 :          0 :         data->num_sess++;
     457                 :          0 :         return sess;
     458                 :            : }
     459                 :            : 
     460                 :            : 
     461                 :            : static struct radius_session *
     462                 :          0 : radius_server_get_new_session(struct radius_server_data *data,
     463                 :            :                               struct radius_client *client,
     464                 :            :                               struct radius_msg *msg)
     465                 :            : {
     466                 :            :         u8 *user;
     467                 :            :         size_t user_len;
     468                 :            :         int res;
     469                 :            :         struct radius_session *sess;
     470                 :            :         struct eap_config eap_conf;
     471                 :            : 
     472                 :          0 :         RADIUS_DEBUG("Creating a new session");
     473                 :            : 
     474                 :          0 :         user = os_malloc(256);
     475         [ #  # ]:          0 :         if (user == NULL) {
     476                 :          0 :                 return NULL;
     477                 :            :         }
     478                 :          0 :         res = radius_msg_get_attr(msg, RADIUS_ATTR_USER_NAME, user, 256);
     479 [ #  # ][ #  # ]:          0 :         if (res < 0 || res > 256) {
     480                 :          0 :                 RADIUS_DEBUG("Could not get User-Name");
     481                 :          0 :                 os_free(user);
     482                 :          0 :                 return NULL;
     483                 :            :         }
     484                 :          0 :         user_len = res;
     485                 :          0 :         RADIUS_DUMP_ASCII("User-Name", user, user_len);
     486                 :            : 
     487                 :          0 :         res = data->get_eap_user(data->conf_ctx, user, user_len, 0, NULL);
     488                 :          0 :         os_free(user);
     489                 :            : 
     490         [ #  # ]:          0 :         if (res == 0) {
     491                 :          0 :                 RADIUS_DEBUG("Matching user entry found");
     492                 :          0 :                 sess = radius_server_new_session(data, client);
     493         [ #  # ]:          0 :                 if (sess == NULL) {
     494                 :          0 :                         RADIUS_DEBUG("Failed to create a new session");
     495                 :          0 :                         return NULL;
     496                 :            :                 }
     497                 :            :         } else {
     498                 :          0 :                 RADIUS_DEBUG("User-Name not found from user database");
     499                 :          0 :                 return NULL;
     500                 :            :         }
     501                 :            : 
     502                 :          0 :         os_memset(&eap_conf, 0, sizeof(eap_conf));
     503                 :          0 :         eap_conf.ssl_ctx = data->ssl_ctx;
     504                 :          0 :         eap_conf.msg_ctx = data->msg_ctx;
     505                 :          0 :         eap_conf.eap_sim_db_priv = data->eap_sim_db_priv;
     506                 :          0 :         eap_conf.backend_auth = TRUE;
     507                 :          0 :         eap_conf.eap_server = 1;
     508                 :          0 :         eap_conf.pac_opaque_encr_key = data->pac_opaque_encr_key;
     509                 :          0 :         eap_conf.eap_fast_a_id = data->eap_fast_a_id;
     510                 :          0 :         eap_conf.eap_fast_a_id_len = data->eap_fast_a_id_len;
     511                 :          0 :         eap_conf.eap_fast_a_id_info = data->eap_fast_a_id_info;
     512                 :          0 :         eap_conf.eap_fast_prov = data->eap_fast_prov;
     513                 :          0 :         eap_conf.pac_key_lifetime = data->pac_key_lifetime;
     514                 :          0 :         eap_conf.pac_key_refresh_time = data->pac_key_refresh_time;
     515                 :          0 :         eap_conf.eap_sim_aka_result_ind = data->eap_sim_aka_result_ind;
     516                 :          0 :         eap_conf.tnc = data->tnc;
     517                 :          0 :         eap_conf.wps = data->wps;
     518                 :          0 :         eap_conf.pwd_group = data->pwd_group;
     519                 :          0 :         eap_conf.server_id = (const u8 *) data->server_id;
     520                 :          0 :         eap_conf.server_id_len = os_strlen(data->server_id);
     521                 :          0 :         sess->eap = eap_server_sm_init(sess, &radius_server_eapol_cb,
     522                 :            :                                        &eap_conf);
     523         [ #  # ]:          0 :         if (sess->eap == NULL) {
     524                 :          0 :                 RADIUS_DEBUG("Failed to initialize EAP state machine for the "
     525                 :            :                              "new session");
     526                 :          0 :                 radius_server_session_free(data, sess);
     527                 :          0 :                 return NULL;
     528                 :            :         }
     529                 :          0 :         sess->eap_if = eap_get_interface(sess->eap);
     530                 :          0 :         sess->eap_if->eapRestart = TRUE;
     531                 :          0 :         sess->eap_if->portEnabled = TRUE;
     532                 :            : 
     533                 :          0 :         RADIUS_DEBUG("New session 0x%x initialized", sess->sess_id);
     534                 :            : 
     535                 :          0 :         return sess;
     536                 :            : }
     537                 :            : 
     538                 :            : 
     539                 :            : static struct radius_msg *
     540                 :          0 : radius_server_encapsulate_eap(struct radius_server_data *data,
     541                 :            :                               struct radius_client *client,
     542                 :            :                               struct radius_session *sess,
     543                 :            :                               struct radius_msg *request)
     544                 :            : {
     545                 :            :         struct radius_msg *msg;
     546                 :            :         int code;
     547                 :            :         unsigned int sess_id;
     548                 :          0 :         struct radius_hdr *hdr = radius_msg_get_hdr(request);
     549                 :            : 
     550         [ #  # ]:          0 :         if (sess->eap_if->eapFail) {
     551                 :          0 :                 sess->eap_if->eapFail = FALSE;
     552                 :          0 :                 code = RADIUS_CODE_ACCESS_REJECT;
     553         [ #  # ]:          0 :         } else if (sess->eap_if->eapSuccess) {
     554                 :          0 :                 sess->eap_if->eapSuccess = FALSE;
     555                 :          0 :                 code = RADIUS_CODE_ACCESS_ACCEPT;
     556                 :            :         } else {
     557                 :          0 :                 sess->eap_if->eapReq = FALSE;
     558                 :          0 :                 code = RADIUS_CODE_ACCESS_CHALLENGE;
     559                 :            :         }
     560                 :            : 
     561                 :          0 :         msg = radius_msg_new(code, hdr->identifier);
     562         [ #  # ]:          0 :         if (msg == NULL) {
     563                 :          0 :                 RADIUS_DEBUG("Failed to allocate reply message");
     564                 :          0 :                 return NULL;
     565                 :            :         }
     566                 :            : 
     567                 :          0 :         sess_id = htonl(sess->sess_id);
     568   [ #  #  #  # ]:          0 :         if (code == RADIUS_CODE_ACCESS_CHALLENGE &&
     569                 :          0 :             !radius_msg_add_attr(msg, RADIUS_ATTR_STATE,
     570                 :            :                                  (u8 *) &sess_id, sizeof(sess_id))) {
     571                 :          0 :                 RADIUS_DEBUG("Failed to add State attribute");
     572                 :            :         }
     573                 :            : 
     574   [ #  #  #  # ]:          0 :         if (sess->eap_if->eapReqData &&
     575                 :          0 :             !radius_msg_add_eap(msg, wpabuf_head(sess->eap_if->eapReqData),
     576                 :          0 :                                 wpabuf_len(sess->eap_if->eapReqData))) {
     577                 :          0 :                 RADIUS_DEBUG("Failed to add EAP-Message attribute");
     578                 :            :         }
     579                 :            : 
     580 [ #  # ][ #  # ]:          0 :         if (code == RADIUS_CODE_ACCESS_ACCEPT && sess->eap_if->eapKeyData) {
     581                 :            :                 int len;
     582                 :            : #ifdef CONFIG_RADIUS_TEST
     583                 :            :                 if (data->dump_msk_file) {
     584                 :            :                         FILE *f;
     585                 :            :                         char buf[2 * 64 + 1];
     586                 :            :                         f = fopen(data->dump_msk_file, "a");
     587                 :            :                         if (f) {
     588                 :            :                                 len = sess->eap_if->eapKeyDataLen;
     589                 :            :                                 if (len > 64)
     590                 :            :                                         len = 64;
     591                 :            :                                 len = wpa_snprintf_hex(
     592                 :            :                                         buf, sizeof(buf),
     593                 :            :                                         sess->eap_if->eapKeyData, len);
     594                 :            :                                 buf[len] = '\0';
     595                 :            :                                 fprintf(f, "%s\n", buf);
     596                 :            :                                 fclose(f);
     597                 :            :                         }
     598                 :            :                 }
     599                 :            : #endif /* CONFIG_RADIUS_TEST */
     600         [ #  # ]:          0 :                 if (sess->eap_if->eapKeyDataLen > 64) {
     601                 :          0 :                         len = 32;
     602                 :            :                 } else {
     603                 :          0 :                         len = sess->eap_if->eapKeyDataLen / 2;
     604                 :            :                 }
     605         [ #  # ]:          0 :                 if (!radius_msg_add_mppe_keys(msg, hdr->authenticator,
     606                 :          0 :                                               (u8 *) client->shared_secret,
     607                 :          0 :                                               client->shared_secret_len,
     608                 :          0 :                                               sess->eap_if->eapKeyData + len,
     609                 :          0 :                                               len, sess->eap_if->eapKeyData,
     610                 :            :                                               len)) {
     611                 :          0 :                         RADIUS_DEBUG("Failed to add MPPE key attributes");
     612                 :            :                 }
     613                 :            :         }
     614                 :            : 
     615         [ #  # ]:          0 :         if (radius_msg_copy_attr(msg, request, RADIUS_ATTR_PROXY_STATE) < 0) {
     616                 :          0 :                 RADIUS_DEBUG("Failed to copy Proxy-State attribute(s)");
     617                 :          0 :                 radius_msg_free(msg);
     618                 :          0 :                 return NULL;
     619                 :            :         }
     620                 :            : 
     621         [ #  # ]:          0 :         if (radius_msg_finish_srv(msg, (u8 *) client->shared_secret,
     622                 :          0 :                                   client->shared_secret_len,
     623                 :          0 :                                   hdr->authenticator) < 0) {
     624                 :          0 :                 RADIUS_DEBUG("Failed to add Message-Authenticator attribute");
     625                 :            :         }
     626                 :            : 
     627                 :          0 :         return msg;
     628                 :            : }
     629                 :            : 
     630                 :            : 
     631                 :          0 : static int radius_server_reject(struct radius_server_data *data,
     632                 :            :                                 struct radius_client *client,
     633                 :            :                                 struct radius_msg *request,
     634                 :            :                                 struct sockaddr *from, socklen_t fromlen,
     635                 :            :                                 const char *from_addr, int from_port)
     636                 :            : {
     637                 :            :         struct radius_msg *msg;
     638                 :          0 :         int ret = 0;
     639                 :            :         struct eap_hdr eapfail;
     640                 :            :         struct wpabuf *buf;
     641                 :          0 :         struct radius_hdr *hdr = radius_msg_get_hdr(request);
     642                 :            : 
     643                 :          0 :         RADIUS_DEBUG("Reject invalid request from %s:%d",
     644                 :            :                      from_addr, from_port);
     645                 :            : 
     646                 :          0 :         msg = radius_msg_new(RADIUS_CODE_ACCESS_REJECT, hdr->identifier);
     647         [ #  # ]:          0 :         if (msg == NULL) {
     648                 :          0 :                 return -1;
     649                 :            :         }
     650                 :            : 
     651                 :          0 :         os_memset(&eapfail, 0, sizeof(eapfail));
     652                 :          0 :         eapfail.code = EAP_CODE_FAILURE;
     653                 :          0 :         eapfail.identifier = 0;
     654                 :          0 :         eapfail.length = host_to_be16(sizeof(eapfail));
     655                 :            : 
     656         [ #  # ]:          0 :         if (!radius_msg_add_eap(msg, (u8 *) &eapfail, sizeof(eapfail))) {
     657                 :          0 :                 RADIUS_DEBUG("Failed to add EAP-Message attribute");
     658                 :            :         }
     659                 :            : 
     660         [ #  # ]:          0 :         if (radius_msg_copy_attr(msg, request, RADIUS_ATTR_PROXY_STATE) < 0) {
     661                 :          0 :                 RADIUS_DEBUG("Failed to copy Proxy-State attribute(s)");
     662                 :          0 :                 radius_msg_free(msg);
     663                 :          0 :                 return -1;
     664                 :            :         }
     665                 :            : 
     666         [ #  # ]:          0 :         if (radius_msg_finish_srv(msg, (u8 *) client->shared_secret,
     667                 :          0 :                                   client->shared_secret_len,
     668                 :          0 :                                   hdr->authenticator) <
     669                 :            :             0) {
     670                 :          0 :                 RADIUS_DEBUG("Failed to add Message-Authenticator attribute");
     671                 :            :         }
     672                 :            : 
     673         [ #  # ]:          0 :         if (wpa_debug_level <= MSG_MSGDUMP) {
     674                 :          0 :                 radius_msg_dump(msg);
     675                 :            :         }
     676                 :            : 
     677                 :          0 :         data->counters.access_rejects++;
     678                 :          0 :         client->counters.access_rejects++;
     679                 :          0 :         buf = radius_msg_get_buf(msg);
     680         [ #  # ]:          0 :         if (sendto(data->auth_sock, wpabuf_head(buf), wpabuf_len(buf), 0,
     681                 :            :                    (struct sockaddr *) from, sizeof(*from)) < 0) {
     682                 :          0 :                 wpa_printf(MSG_INFO, "sendto[RADIUS SRV]: %s", strerror(errno));
     683                 :          0 :                 ret = -1;
     684                 :            :         }
     685                 :            : 
     686                 :          0 :         radius_msg_free(msg);
     687                 :            : 
     688                 :          0 :         return ret;
     689                 :            : }
     690                 :            : 
     691                 :            : 
     692                 :          0 : static int radius_server_request(struct radius_server_data *data,
     693                 :            :                                  struct radius_msg *msg,
     694                 :            :                                  struct sockaddr *from, socklen_t fromlen,
     695                 :            :                                  struct radius_client *client,
     696                 :            :                                  const char *from_addr, int from_port,
     697                 :            :                                  struct radius_session *force_sess)
     698                 :            : {
     699                 :          0 :         struct wpabuf *eap = NULL;
     700                 :          0 :         int res, state_included = 0;
     701                 :            :         u8 statebuf[4];
     702                 :            :         unsigned int state;
     703                 :            :         struct radius_session *sess;
     704                 :            :         struct radius_msg *reply;
     705                 :          0 :         int is_complete = 0;
     706                 :            : 
     707         [ #  # ]:          0 :         if (force_sess)
     708                 :          0 :                 sess = force_sess;
     709                 :            :         else {
     710                 :          0 :                 res = radius_msg_get_attr(msg, RADIUS_ATTR_STATE, statebuf,
     711                 :            :                                           sizeof(statebuf));
     712                 :          0 :                 state_included = res >= 0;
     713         [ #  # ]:          0 :                 if (res == sizeof(statebuf)) {
     714                 :          0 :                         state = WPA_GET_BE32(statebuf);
     715                 :          0 :                         sess = radius_server_get_session(client, state);
     716                 :            :                 } else {
     717                 :          0 :                         sess = NULL;
     718                 :            :                 }
     719                 :            :         }
     720                 :            : 
     721         [ #  # ]:          0 :         if (sess) {
     722                 :          0 :                 RADIUS_DEBUG("Request for session 0x%x", sess->sess_id);
     723         [ #  # ]:          0 :         } else if (state_included) {
     724                 :          0 :                 RADIUS_DEBUG("State attribute included but no session found");
     725                 :          0 :                 radius_server_reject(data, client, msg, from, fromlen,
     726                 :            :                                      from_addr, from_port);
     727                 :          0 :                 return -1;
     728                 :            :         } else {
     729                 :          0 :                 sess = radius_server_get_new_session(data, client, msg);
     730         [ #  # ]:          0 :                 if (sess == NULL) {
     731                 :          0 :                         RADIUS_DEBUG("Could not create a new session");
     732                 :          0 :                         radius_server_reject(data, client, msg, from, fromlen,
     733                 :            :                                              from_addr, from_port);
     734                 :          0 :                         return -1;
     735                 :            :                 }
     736                 :            :         }
     737                 :            : 
     738   [ #  #  #  # ]:          0 :         if (sess->last_from_port == from_port &&
     739         [ #  # ]:          0 :             sess->last_identifier == radius_msg_get_hdr(msg)->identifier &&
     740                 :          0 :             os_memcmp(sess->last_authenticator,
     741                 :            :                       radius_msg_get_hdr(msg)->authenticator, 16) == 0) {
     742                 :          0 :                 RADIUS_DEBUG("Duplicate message from %s", from_addr);
     743                 :          0 :                 data->counters.dup_access_requests++;
     744                 :          0 :                 client->counters.dup_access_requests++;
     745                 :            : 
     746         [ #  # ]:          0 :                 if (sess->last_reply) {
     747                 :            :                         struct wpabuf *buf;
     748                 :          0 :                         buf = radius_msg_get_buf(sess->last_reply);
     749                 :          0 :                         res = sendto(data->auth_sock, wpabuf_head(buf),
     750                 :            :                                      wpabuf_len(buf), 0,
     751                 :            :                                      (struct sockaddr *) from, fromlen);
     752         [ #  # ]:          0 :                         if (res < 0) {
     753                 :          0 :                                 wpa_printf(MSG_INFO, "sendto[RADIUS SRV]: %s",
     754                 :          0 :                                            strerror(errno));
     755                 :            :                         }
     756                 :          0 :                         return 0;
     757                 :            :                 }
     758                 :            : 
     759                 :          0 :                 RADIUS_DEBUG("No previous reply available for duplicate "
     760                 :            :                              "message");
     761                 :          0 :                 return -1;
     762                 :            :         }
     763                 :            :                       
     764                 :          0 :         eap = radius_msg_get_eap(msg);
     765         [ #  # ]:          0 :         if (eap == NULL) {
     766                 :          0 :                 RADIUS_DEBUG("No EAP-Message in RADIUS packet from %s",
     767                 :            :                              from_addr);
     768                 :          0 :                 data->counters.packets_dropped++;
     769                 :          0 :                 client->counters.packets_dropped++;
     770                 :          0 :                 return -1;
     771                 :            :         }
     772                 :            : 
     773                 :          0 :         RADIUS_DUMP("Received EAP data", wpabuf_head(eap), wpabuf_len(eap));
     774                 :            : 
     775                 :            :         /* FIX: if Code is Request, Success, or Failure, send Access-Reject;
     776                 :            :          * RFC3579 Sect. 2.6.2.
     777                 :            :          * Include EAP-Response/Nak with no preferred method if
     778                 :            :          * code == request.
     779                 :            :          * If code is not 1-4, discard the packet silently.
     780                 :            :          * Or is this already done by the EAP state machine? */
     781                 :            : 
     782                 :          0 :         wpabuf_free(sess->eap_if->eapRespData);
     783                 :          0 :         sess->eap_if->eapRespData = eap;
     784                 :          0 :         sess->eap_if->eapResp = TRUE;
     785                 :          0 :         eap_server_sm_step(sess->eap);
     786                 :            : 
     787 [ #  # ][ #  # ]:          0 :         if ((sess->eap_if->eapReq || sess->eap_if->eapSuccess ||
                 [ #  # ]
     788         [ #  # ]:          0 :              sess->eap_if->eapFail) && sess->eap_if->eapReqData) {
     789                 :          0 :                 RADIUS_DUMP("EAP data from the state machine",
     790                 :            :                             wpabuf_head(sess->eap_if->eapReqData),
     791                 :            :                             wpabuf_len(sess->eap_if->eapReqData));
     792         [ #  # ]:          0 :         } else if (sess->eap_if->eapFail) {
     793                 :          0 :                 RADIUS_DEBUG("No EAP data from the state machine, but eapFail "
     794                 :            :                              "set");
     795         [ #  # ]:          0 :         } else if (eap_sm_method_pending(sess->eap)) {
     796                 :          0 :                 radius_msg_free(sess->last_msg);
     797                 :          0 :                 sess->last_msg = msg;
     798                 :          0 :                 sess->last_from_port = from_port;
     799                 :          0 :                 os_free(sess->last_from_addr);
     800                 :          0 :                 sess->last_from_addr = os_strdup(from_addr);
     801                 :          0 :                 sess->last_fromlen = fromlen;
     802                 :          0 :                 os_memcpy(&sess->last_from, from, fromlen);
     803                 :          0 :                 return -2;
     804                 :            :         } else {
     805                 :          0 :                 RADIUS_DEBUG("No EAP data from the state machine - ignore this"
     806                 :            :                              " Access-Request silently (assuming it was a "
     807                 :            :                              "duplicate)");
     808                 :          0 :                 data->counters.packets_dropped++;
     809                 :          0 :                 client->counters.packets_dropped++;
     810                 :          0 :                 return -1;
     811                 :            :         }
     812                 :            : 
     813 [ #  # ][ #  # ]:          0 :         if (sess->eap_if->eapSuccess || sess->eap_if->eapFail)
     814                 :          0 :                 is_complete = 1;
     815                 :            : 
     816                 :          0 :         reply = radius_server_encapsulate_eap(data, client, sess, msg);
     817                 :            : 
     818         [ #  # ]:          0 :         if (reply) {
     819                 :            :                 struct wpabuf *buf;
     820                 :            :                 struct radius_hdr *hdr;
     821                 :            : 
     822                 :          0 :                 RADIUS_DEBUG("Reply to %s:%d", from_addr, from_port);
     823         [ #  # ]:          0 :                 if (wpa_debug_level <= MSG_MSGDUMP) {
     824                 :          0 :                         radius_msg_dump(reply);
     825                 :            :                 }
     826                 :            : 
     827   [ #  #  #  # ]:          0 :                 switch (radius_msg_get_hdr(reply)->code) {
     828                 :            :                 case RADIUS_CODE_ACCESS_ACCEPT:
     829                 :          0 :                         data->counters.access_accepts++;
     830                 :          0 :                         client->counters.access_accepts++;
     831                 :          0 :                         break;
     832                 :            :                 case RADIUS_CODE_ACCESS_REJECT:
     833                 :          0 :                         data->counters.access_rejects++;
     834                 :          0 :                         client->counters.access_rejects++;
     835                 :          0 :                         break;
     836                 :            :                 case RADIUS_CODE_ACCESS_CHALLENGE:
     837                 :          0 :                         data->counters.access_challenges++;
     838                 :          0 :                         client->counters.access_challenges++;
     839                 :          0 :                         break;
     840                 :            :                 }
     841                 :          0 :                 buf = radius_msg_get_buf(reply);
     842                 :          0 :                 res = sendto(data->auth_sock, wpabuf_head(buf),
     843                 :            :                              wpabuf_len(buf), 0,
     844                 :            :                              (struct sockaddr *) from, fromlen);
     845         [ #  # ]:          0 :                 if (res < 0) {
     846                 :          0 :                         wpa_printf(MSG_INFO, "sendto[RADIUS SRV]: %s",
     847                 :          0 :                                    strerror(errno));
     848                 :            :                 }
     849                 :          0 :                 radius_msg_free(sess->last_reply);
     850                 :          0 :                 sess->last_reply = reply;
     851                 :          0 :                 sess->last_from_port = from_port;
     852                 :          0 :                 hdr = radius_msg_get_hdr(msg);
     853                 :          0 :                 sess->last_identifier = hdr->identifier;
     854                 :          0 :                 os_memcpy(sess->last_authenticator, hdr->authenticator, 16);
     855                 :            :         } else {
     856                 :          0 :                 data->counters.packets_dropped++;
     857                 :          0 :                 client->counters.packets_dropped++;
     858                 :            :         }
     859                 :            : 
     860         [ #  # ]:          0 :         if (is_complete) {
     861                 :          0 :                 RADIUS_DEBUG("Removing completed session 0x%x after timeout",
     862                 :            :                              sess->sess_id);
     863                 :          0 :                 eloop_cancel_timeout(radius_server_session_remove_timeout,
     864                 :            :                                      data, sess);
     865                 :          0 :                 eloop_register_timeout(10, 0,
     866                 :            :                                        radius_server_session_remove_timeout,
     867                 :            :                                        data, sess);
     868                 :            :         }
     869                 :            : 
     870                 :          0 :         return 0;
     871                 :            : }
     872                 :            : 
     873                 :            : 
     874                 :          0 : static void radius_server_receive_auth(int sock, void *eloop_ctx,
     875                 :            :                                        void *sock_ctx)
     876                 :            : {
     877                 :          0 :         struct radius_server_data *data = eloop_ctx;
     878                 :          0 :         u8 *buf = NULL;
     879                 :            :         union {
     880                 :            :                 struct sockaddr_storage ss;
     881                 :            :                 struct sockaddr_in sin;
     882                 :            : #ifdef CONFIG_IPV6
     883                 :            :                 struct sockaddr_in6 sin6;
     884                 :            : #endif /* CONFIG_IPV6 */
     885                 :            :         } from;
     886                 :            :         socklen_t fromlen;
     887                 :            :         int len;
     888                 :          0 :         struct radius_client *client = NULL;
     889                 :          0 :         struct radius_msg *msg = NULL;
     890                 :            :         char abuf[50];
     891                 :          0 :         int from_port = 0;
     892                 :            : 
     893                 :          0 :         buf = os_malloc(RADIUS_MAX_MSG_LEN);
     894         [ #  # ]:          0 :         if (buf == NULL) {
     895                 :          0 :                 goto fail;
     896                 :            :         }
     897                 :            : 
     898                 :          0 :         fromlen = sizeof(from);
     899                 :          0 :         len = recvfrom(sock, buf, RADIUS_MAX_MSG_LEN, 0,
     900                 :            :                        (struct sockaddr *) &from.ss, &fromlen);
     901         [ #  # ]:          0 :         if (len < 0) {
     902                 :          0 :                 wpa_printf(MSG_INFO, "recvfrom[radius_server]: %s",
     903                 :          0 :                            strerror(errno));
     904                 :          0 :                 goto fail;
     905                 :            :         }
     906                 :            : 
     907                 :            : #ifdef CONFIG_IPV6
     908         [ #  # ]:          0 :         if (data->ipv6) {
     909         [ #  # ]:          0 :                 if (inet_ntop(AF_INET6, &from.sin6.sin6_addr, abuf,
     910                 :            :                               sizeof(abuf)) == NULL)
     911                 :          0 :                         abuf[0] = '\0';
     912                 :          0 :                 from_port = ntohs(from.sin6.sin6_port);
     913                 :          0 :                 RADIUS_DEBUG("Received %d bytes from %s:%d",
     914                 :            :                              len, abuf, from_port);
     915                 :            : 
     916                 :          0 :                 client = radius_server_get_client(data,
     917                 :            :                                                   (struct in_addr *)
     918                 :            :                                                   &from.sin6.sin6_addr, 1);
     919                 :            :         }
     920                 :            : #endif /* CONFIG_IPV6 */
     921                 :            : 
     922         [ #  # ]:          0 :         if (!data->ipv6) {
     923                 :          0 :                 os_strlcpy(abuf, inet_ntoa(from.sin.sin_addr), sizeof(abuf));
     924                 :          0 :                 from_port = ntohs(from.sin.sin_port);
     925                 :          0 :                 RADIUS_DEBUG("Received %d bytes from %s:%d",
     926                 :            :                              len, abuf, from_port);
     927                 :            : 
     928                 :          0 :                 client = radius_server_get_client(data, &from.sin.sin_addr, 0);
     929                 :            :         }
     930                 :            : 
     931                 :          0 :         RADIUS_DUMP("Received data", buf, len);
     932                 :            : 
     933         [ #  # ]:          0 :         if (client == NULL) {
     934                 :          0 :                 RADIUS_DEBUG("Unknown client %s - packet ignored", abuf);
     935                 :          0 :                 data->counters.invalid_requests++;
     936                 :          0 :                 goto fail;
     937                 :            :         }
     938                 :            : 
     939                 :          0 :         msg = radius_msg_parse(buf, len);
     940         [ #  # ]:          0 :         if (msg == NULL) {
     941                 :          0 :                 RADIUS_DEBUG("Parsing incoming RADIUS frame failed");
     942                 :          0 :                 data->counters.malformed_access_requests++;
     943                 :          0 :                 client->counters.malformed_access_requests++;
     944                 :          0 :                 goto fail;
     945                 :            :         }
     946                 :            : 
     947                 :          0 :         os_free(buf);
     948                 :          0 :         buf = NULL;
     949                 :            : 
     950         [ #  # ]:          0 :         if (wpa_debug_level <= MSG_MSGDUMP) {
     951                 :          0 :                 radius_msg_dump(msg);
     952                 :            :         }
     953                 :            : 
     954         [ #  # ]:          0 :         if (radius_msg_get_hdr(msg)->code != RADIUS_CODE_ACCESS_REQUEST) {
     955                 :          0 :                 RADIUS_DEBUG("Unexpected RADIUS code %d",
     956                 :            :                              radius_msg_get_hdr(msg)->code);
     957                 :          0 :                 data->counters.unknown_types++;
     958                 :          0 :                 client->counters.unknown_types++;
     959                 :          0 :                 goto fail;
     960                 :            :         }
     961                 :            : 
     962                 :          0 :         data->counters.access_requests++;
     963                 :          0 :         client->counters.access_requests++;
     964                 :            : 
     965         [ #  # ]:          0 :         if (radius_msg_verify_msg_auth(msg, (u8 *) client->shared_secret,
     966                 :          0 :                                        client->shared_secret_len, NULL)) {
     967                 :          0 :                 RADIUS_DEBUG("Invalid Message-Authenticator from %s", abuf);
     968                 :          0 :                 data->counters.bad_authenticators++;
     969                 :          0 :                 client->counters.bad_authenticators++;
     970                 :          0 :                 goto fail;
     971                 :            :         }
     972                 :            : 
     973         [ #  # ]:          0 :         if (radius_server_request(data, msg, (struct sockaddr *) &from,
     974                 :            :                                   fromlen, client, abuf, from_port, NULL) ==
     975                 :            :             -2)
     976                 :          0 :                 return; /* msg was stored with the session */
     977                 :            : 
     978                 :            : fail:
     979                 :          0 :         radius_msg_free(msg);
     980                 :          0 :         os_free(buf);
     981                 :            : }
     982                 :            : 
     983                 :            : 
     984                 :          0 : static int radius_server_disable_pmtu_discovery(int s)
     985                 :            : {
     986                 :          0 :         int r = -1;
     987                 :            : #if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT)
     988                 :            :         /* Turn off Path MTU discovery on IPv4/UDP sockets. */
     989                 :          0 :         int action = IP_PMTUDISC_DONT;
     990                 :          0 :         r = setsockopt(s, IPPROTO_IP, IP_MTU_DISCOVER, &action,
     991                 :            :                        sizeof(action));
     992         [ #  # ]:          0 :         if (r == -1)
     993                 :          0 :                 wpa_printf(MSG_ERROR, "Failed to set IP_MTU_DISCOVER: "
     994                 :          0 :                            "%s", strerror(errno));
     995                 :            : #endif
     996                 :          0 :         return r;
     997                 :            : }
     998                 :            : 
     999                 :            : 
    1000                 :          0 : static int radius_server_open_socket(int port)
    1001                 :            : {
    1002                 :            :         int s;
    1003                 :            :         struct sockaddr_in addr;
    1004                 :            : 
    1005                 :          0 :         s = socket(PF_INET, SOCK_DGRAM, 0);
    1006         [ #  # ]:          0 :         if (s < 0) {
    1007                 :          0 :                 wpa_printf(MSG_INFO, "RADIUS: socket: %s", strerror(errno));
    1008                 :          0 :                 return -1;
    1009                 :            :         }
    1010                 :            : 
    1011                 :          0 :         radius_server_disable_pmtu_discovery(s);
    1012                 :            : 
    1013                 :          0 :         os_memset(&addr, 0, sizeof(addr));
    1014                 :          0 :         addr.sin_family = AF_INET;
    1015                 :          0 :         addr.sin_port = htons(port);
    1016         [ #  # ]:          0 :         if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
    1017                 :          0 :                 wpa_printf(MSG_INFO, "RADIUS: bind: %s", strerror(errno));
    1018                 :          0 :                 close(s);
    1019                 :          0 :                 return -1;
    1020                 :            :         }
    1021                 :            : 
    1022                 :          0 :         return s;
    1023                 :            : }
    1024                 :            : 
    1025                 :            : 
    1026                 :            : #ifdef CONFIG_IPV6
    1027                 :          0 : static int radius_server_open_socket6(int port)
    1028                 :            : {
    1029                 :            :         int s;
    1030                 :            :         struct sockaddr_in6 addr;
    1031                 :            : 
    1032                 :          0 :         s = socket(PF_INET6, SOCK_DGRAM, 0);
    1033         [ #  # ]:          0 :         if (s < 0) {
    1034                 :          0 :                 wpa_printf(MSG_INFO, "RADIUS: socket[IPv6]: %s",
    1035                 :          0 :                            strerror(errno));
    1036                 :          0 :                 return -1;
    1037                 :            :         }
    1038                 :            : 
    1039                 :          0 :         os_memset(&addr, 0, sizeof(addr));
    1040                 :          0 :         addr.sin6_family = AF_INET6;
    1041                 :          0 :         os_memcpy(&addr.sin6_addr, &in6addr_any, sizeof(in6addr_any));
    1042                 :          0 :         addr.sin6_port = htons(port);
    1043         [ #  # ]:          0 :         if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
    1044                 :          0 :                 wpa_printf(MSG_INFO, "RADIUS: bind: %s", strerror(errno));
    1045                 :          0 :                 close(s);
    1046                 :          0 :                 return -1;
    1047                 :            :         }
    1048                 :            : 
    1049                 :          0 :         return s;
    1050                 :            : }
    1051                 :            : #endif /* CONFIG_IPV6 */
    1052                 :            : 
    1053                 :            : 
    1054                 :          0 : static void radius_server_free_sessions(struct radius_server_data *data,
    1055                 :            :                                         struct radius_session *sessions)
    1056                 :            : {
    1057                 :            :         struct radius_session *session, *prev;
    1058                 :            : 
    1059                 :          0 :         session = sessions;
    1060         [ #  # ]:          0 :         while (session) {
    1061                 :          0 :                 prev = session;
    1062                 :          0 :                 session = session->next;
    1063                 :          0 :                 radius_server_session_free(data, prev);
    1064                 :            :         }
    1065                 :          0 : }
    1066                 :            : 
    1067                 :            : 
    1068                 :          0 : static void radius_server_free_clients(struct radius_server_data *data,
    1069                 :            :                                        struct radius_client *clients)
    1070                 :            : {
    1071                 :            :         struct radius_client *client, *prev;
    1072                 :            : 
    1073                 :          0 :         client = clients;
    1074         [ #  # ]:          0 :         while (client) {
    1075                 :          0 :                 prev = client;
    1076                 :          0 :                 client = client->next;
    1077                 :            : 
    1078                 :          0 :                 radius_server_free_sessions(data, prev->sessions);
    1079                 :          0 :                 os_free(prev->shared_secret);
    1080                 :          0 :                 os_free(prev);
    1081                 :            :         }
    1082                 :          0 : }
    1083                 :            : 
    1084                 :            : 
    1085                 :            : static struct radius_client *
    1086                 :          0 : radius_server_read_clients(const char *client_file, int ipv6)
    1087                 :            : {
    1088                 :            :         FILE *f;
    1089                 :          0 :         const int buf_size = 1024;
    1090                 :            :         char *buf, *pos;
    1091                 :            :         struct radius_client *clients, *tail, *entry;
    1092                 :          0 :         int line = 0, mask, failed = 0, i;
    1093                 :            :         struct in_addr addr;
    1094                 :            : #ifdef CONFIG_IPV6
    1095                 :            :         struct in6_addr addr6;
    1096                 :            : #endif /* CONFIG_IPV6 */
    1097                 :            :         unsigned int val;
    1098                 :            : 
    1099                 :          0 :         f = fopen(client_file, "r");
    1100         [ #  # ]:          0 :         if (f == NULL) {
    1101                 :          0 :                 RADIUS_ERROR("Could not open client file '%s'", client_file);
    1102                 :          0 :                 return NULL;
    1103                 :            :         }
    1104                 :            : 
    1105                 :          0 :         buf = os_malloc(buf_size);
    1106         [ #  # ]:          0 :         if (buf == NULL) {
    1107                 :          0 :                 fclose(f);
    1108                 :          0 :                 return NULL;
    1109                 :            :         }
    1110                 :            : 
    1111                 :          0 :         clients = tail = NULL;
    1112         [ #  # ]:          0 :         while (fgets(buf, buf_size, f)) {
    1113                 :            :                 /* Configuration file format:
    1114                 :            :                  * 192.168.1.0/24 secret
    1115                 :            :                  * 192.168.1.2 secret
    1116                 :            :                  * fe80::211:22ff:fe33:4455/64 secretipv6
    1117                 :            :                  */
    1118                 :          0 :                 line++;
    1119                 :          0 :                 buf[buf_size - 1] = '\0';
    1120                 :          0 :                 pos = buf;
    1121 [ #  # ][ #  # ]:          0 :                 while (*pos != '\0' && *pos != '\n')
    1122                 :          0 :                         pos++;
    1123         [ #  # ]:          0 :                 if (*pos == '\n')
    1124                 :          0 :                         *pos = '\0';
    1125 [ #  # ][ #  # ]:          0 :                 if (*buf == '\0' || *buf == '#')
    1126                 :          0 :                         continue;
    1127                 :            : 
    1128                 :          0 :                 pos = buf;
    1129 [ #  # ][ #  # ]:          0 :                 while ((*pos >= '0' && *pos <= '9') || *pos == '.' ||
         [ #  # ][ #  # ]
    1130 [ #  # ][ #  # ]:          0 :                        (*pos >= 'a' && *pos <= 'f') || *pos == ':' ||
                 [ #  # ]
    1131         [ #  # ]:          0 :                        (*pos >= 'A' && *pos <= 'F')) {
    1132                 :          0 :                         pos++;
    1133                 :            :                 }
    1134                 :            : 
    1135         [ #  # ]:          0 :                 if (*pos == '\0') {
    1136                 :          0 :                         failed = 1;
    1137                 :          0 :                         break;
    1138                 :            :                 }
    1139                 :            : 
    1140         [ #  # ]:          0 :                 if (*pos == '/') {
    1141                 :            :                         char *end;
    1142                 :          0 :                         *pos++ = '\0';
    1143                 :          0 :                         mask = strtol(pos, &end, 10);
    1144 [ #  # ][ #  # ]:          0 :                         if ((pos == end) ||
    1145 [ #  # ][ #  # ]:          0 :                             (mask < 0 || mask > (ipv6 ? 128 : 32))) {
    1146                 :          0 :                                 failed = 1;
    1147                 :          0 :                                 break;
    1148                 :            :                         }
    1149                 :          0 :                         pos = end;
    1150                 :            :                 } else {
    1151         [ #  # ]:          0 :                         mask = ipv6 ? 128 : 32;
    1152                 :          0 :                         *pos++ = '\0';
    1153                 :            :                 }
    1154                 :            : 
    1155 [ #  # ][ #  # ]:          0 :                 if (!ipv6 && inet_aton(buf, &addr) == 0) {
    1156                 :          0 :                         failed = 1;
    1157                 :          0 :                         break;
    1158                 :            :                 }
    1159                 :            : #ifdef CONFIG_IPV6
    1160 [ #  # ][ #  # ]:          0 :                 if (ipv6 && inet_pton(AF_INET6, buf, &addr6) <= 0) {
    1161         [ #  # ]:          0 :                         if (inet_pton(AF_INET, buf, &addr) <= 0) {
    1162                 :          0 :                                 failed = 1;
    1163                 :          0 :                                 break;
    1164                 :            :                         }
    1165                 :            :                         /* Convert IPv4 address to IPv6 */
    1166         [ #  # ]:          0 :                         if (mask <= 32)
    1167                 :          0 :                                 mask += (128 - 32);
    1168                 :          0 :                         os_memset(addr6.s6_addr, 0, 10);
    1169                 :          0 :                         addr6.s6_addr[10] = 0xff;
    1170                 :          0 :                         addr6.s6_addr[11] = 0xff;
    1171                 :          0 :                         os_memcpy(addr6.s6_addr + 12, (char *) &addr.s_addr,
    1172                 :            :                                   4);
    1173                 :            :                 }
    1174                 :            : #endif /* CONFIG_IPV6 */
    1175                 :            : 
    1176 [ #  # ][ #  # ]:          0 :                 while (*pos == ' ' || *pos == '\t') {
    1177                 :          0 :                         pos++;
    1178                 :            :                 }
    1179                 :            : 
    1180         [ #  # ]:          0 :                 if (*pos == '\0') {
    1181                 :          0 :                         failed = 1;
    1182                 :          0 :                         break;
    1183                 :            :                 }
    1184                 :            : 
    1185                 :          0 :                 entry = os_zalloc(sizeof(*entry));
    1186         [ #  # ]:          0 :                 if (entry == NULL) {
    1187                 :          0 :                         failed = 1;
    1188                 :          0 :                         break;
    1189                 :            :                 }
    1190                 :          0 :                 entry->shared_secret = os_strdup(pos);
    1191         [ #  # ]:          0 :                 if (entry->shared_secret == NULL) {
    1192                 :          0 :                         failed = 1;
    1193                 :          0 :                         os_free(entry);
    1194                 :          0 :                         break;
    1195                 :            :                 }
    1196                 :          0 :                 entry->shared_secret_len = os_strlen(entry->shared_secret);
    1197                 :          0 :                 entry->addr.s_addr = addr.s_addr;
    1198         [ #  # ]:          0 :                 if (!ipv6) {
    1199                 :          0 :                         val = 0;
    1200         [ #  # ]:          0 :                         for (i = 0; i < mask; i++)
    1201                 :          0 :                                 val |= 1 << (31 - i);
    1202                 :          0 :                         entry->mask.s_addr = htonl(val);
    1203                 :            :                 }
    1204                 :            : #ifdef CONFIG_IPV6
    1205         [ #  # ]:          0 :                 if (ipv6) {
    1206                 :          0 :                         int offset = mask / 8;
    1207                 :            : 
    1208                 :          0 :                         os_memcpy(entry->addr6.s6_addr, addr6.s6_addr, 16);
    1209                 :          0 :                         os_memset(entry->mask6.s6_addr, 0xff, offset);
    1210                 :          0 :                         val = 0;
    1211         [ #  # ]:          0 :                         for (i = 0; i < (mask % 8); i++)
    1212                 :          0 :                                 val |= 1 << (7 - i);
    1213         [ #  # ]:          0 :                         if (offset < 16)
    1214                 :          0 :                                 entry->mask6.s6_addr[offset] = val;
    1215                 :            :                 }
    1216                 :            : #endif /* CONFIG_IPV6 */
    1217                 :            : 
    1218         [ #  # ]:          0 :                 if (tail == NULL) {
    1219                 :          0 :                         clients = tail = entry;
    1220                 :            :                 } else {
    1221                 :          0 :                         tail->next = entry;
    1222                 :          0 :                         tail = entry;
    1223                 :            :                 }
    1224                 :            :         }
    1225                 :            : 
    1226         [ #  # ]:          0 :         if (failed) {
    1227                 :          0 :                 RADIUS_ERROR("Invalid line %d in '%s'", line, client_file);
    1228                 :          0 :                 radius_server_free_clients(NULL, clients);
    1229                 :          0 :                 clients = NULL;
    1230                 :            :         }
    1231                 :            : 
    1232                 :          0 :         os_free(buf);
    1233                 :          0 :         fclose(f);
    1234                 :            : 
    1235                 :          0 :         return clients;
    1236                 :            : }
    1237                 :            : 
    1238                 :            : 
    1239                 :            : /**
    1240                 :            :  * radius_server_init - Initialize RADIUS server
    1241                 :            :  * @conf: Configuration for the RADIUS server
    1242                 :            :  * Returns: Pointer to private RADIUS server context or %NULL on failure
    1243                 :            :  *
    1244                 :            :  * This initializes a RADIUS server instance and returns a context pointer that
    1245                 :            :  * will be used in other calls to the RADIUS server module. The server can be
    1246                 :            :  * deinitialize by calling radius_server_deinit().
    1247                 :            :  */
    1248                 :            : struct radius_server_data *
    1249                 :          0 : radius_server_init(struct radius_server_conf *conf)
    1250                 :            : {
    1251                 :            :         struct radius_server_data *data;
    1252                 :            : 
    1253                 :            : #ifndef CONFIG_IPV6
    1254                 :            :         if (conf->ipv6) {
    1255                 :            :                 wpa_printf(MSG_ERROR, "RADIUS server compiled without IPv6 support");
    1256                 :            :                 return NULL;
    1257                 :            :         }
    1258                 :            : #endif /* CONFIG_IPV6 */
    1259                 :            : 
    1260                 :          0 :         data = os_zalloc(sizeof(*data));
    1261         [ #  # ]:          0 :         if (data == NULL)
    1262                 :          0 :                 return NULL;
    1263                 :            : 
    1264                 :          0 :         os_get_reltime(&data->start_time);
    1265                 :          0 :         data->conf_ctx = conf->conf_ctx;
    1266                 :          0 :         data->eap_sim_db_priv = conf->eap_sim_db_priv;
    1267                 :          0 :         data->ssl_ctx = conf->ssl_ctx;
    1268                 :          0 :         data->msg_ctx = conf->msg_ctx;
    1269                 :          0 :         data->ipv6 = conf->ipv6;
    1270         [ #  # ]:          0 :         if (conf->pac_opaque_encr_key) {
    1271                 :          0 :                 data->pac_opaque_encr_key = os_malloc(16);
    1272                 :          0 :                 os_memcpy(data->pac_opaque_encr_key, conf->pac_opaque_encr_key,
    1273                 :            :                           16);
    1274                 :            :         }
    1275         [ #  # ]:          0 :         if (conf->eap_fast_a_id) {
    1276                 :          0 :                 data->eap_fast_a_id = os_malloc(conf->eap_fast_a_id_len);
    1277         [ #  # ]:          0 :                 if (data->eap_fast_a_id) {
    1278                 :          0 :                         os_memcpy(data->eap_fast_a_id, conf->eap_fast_a_id,
    1279                 :            :                                   conf->eap_fast_a_id_len);
    1280                 :          0 :                         data->eap_fast_a_id_len = conf->eap_fast_a_id_len;
    1281                 :            :                 }
    1282                 :            :         }
    1283         [ #  # ]:          0 :         if (conf->eap_fast_a_id_info)
    1284                 :          0 :                 data->eap_fast_a_id_info = os_strdup(conf->eap_fast_a_id_info);
    1285                 :          0 :         data->eap_fast_prov = conf->eap_fast_prov;
    1286                 :          0 :         data->pac_key_lifetime = conf->pac_key_lifetime;
    1287                 :          0 :         data->pac_key_refresh_time = conf->pac_key_refresh_time;
    1288                 :          0 :         data->get_eap_user = conf->get_eap_user;
    1289                 :          0 :         data->eap_sim_aka_result_ind = conf->eap_sim_aka_result_ind;
    1290                 :          0 :         data->tnc = conf->tnc;
    1291                 :          0 :         data->wps = conf->wps;
    1292                 :          0 :         data->pwd_group = conf->pwd_group;
    1293                 :          0 :         data->server_id = conf->server_id;
    1294         [ #  # ]:          0 :         if (conf->eap_req_id_text) {
    1295                 :          0 :                 data->eap_req_id_text = os_malloc(conf->eap_req_id_text_len);
    1296         [ #  # ]:          0 :                 if (data->eap_req_id_text) {
    1297                 :          0 :                         os_memcpy(data->eap_req_id_text, conf->eap_req_id_text,
    1298                 :            :                                   conf->eap_req_id_text_len);
    1299                 :          0 :                         data->eap_req_id_text_len = conf->eap_req_id_text_len;
    1300                 :            :                 }
    1301                 :            :         }
    1302                 :            : 
    1303                 :            : #ifdef CONFIG_RADIUS_TEST
    1304                 :            :         if (conf->dump_msk_file)
    1305                 :            :                 data->dump_msk_file = os_strdup(conf->dump_msk_file);
    1306                 :            : #endif /* CONFIG_RADIUS_TEST */
    1307                 :            : 
    1308                 :          0 :         data->clients = radius_server_read_clients(conf->client_file,
    1309                 :            :                                                    conf->ipv6);
    1310         [ #  # ]:          0 :         if (data->clients == NULL) {
    1311                 :          0 :                 wpa_printf(MSG_ERROR, "No RADIUS clients configured");
    1312                 :          0 :                 radius_server_deinit(data);
    1313                 :          0 :                 return NULL;
    1314                 :            :         }
    1315                 :            : 
    1316                 :            : #ifdef CONFIG_IPV6
    1317         [ #  # ]:          0 :         if (conf->ipv6)
    1318                 :          0 :                 data->auth_sock = radius_server_open_socket6(conf->auth_port);
    1319                 :            :         else
    1320                 :            : #endif /* CONFIG_IPV6 */
    1321                 :          0 :         data->auth_sock = radius_server_open_socket(conf->auth_port);
    1322         [ #  # ]:          0 :         if (data->auth_sock < 0) {
    1323                 :          0 :                 wpa_printf(MSG_ERROR, "Failed to open UDP socket for RADIUS authentication server");
    1324                 :          0 :                 radius_server_deinit(data);
    1325                 :          0 :                 return NULL;
    1326                 :            :         }
    1327         [ #  # ]:          0 :         if (eloop_register_read_sock(data->auth_sock,
    1328                 :            :                                      radius_server_receive_auth,
    1329                 :            :                                      data, NULL)) {
    1330                 :          0 :                 radius_server_deinit(data);
    1331                 :          0 :                 return NULL;
    1332                 :            :         }
    1333                 :            : 
    1334                 :          0 :         return data;
    1335                 :            : }
    1336                 :            : 
    1337                 :            : 
    1338                 :            : /**
    1339                 :            :  * radius_server_deinit - Deinitialize RADIUS server
    1340                 :            :  * @data: RADIUS server context from radius_server_init()
    1341                 :            :  */
    1342                 :        200 : void radius_server_deinit(struct radius_server_data *data)
    1343                 :            : {
    1344         [ +  - ]:        200 :         if (data == NULL)
    1345                 :        200 :                 return;
    1346                 :            : 
    1347         [ #  # ]:          0 :         if (data->auth_sock >= 0) {
    1348                 :          0 :                 eloop_unregister_read_sock(data->auth_sock);
    1349                 :          0 :                 close(data->auth_sock);
    1350                 :            :         }
    1351                 :            : 
    1352                 :          0 :         radius_server_free_clients(data, data->clients);
    1353                 :            : 
    1354                 :          0 :         os_free(data->pac_opaque_encr_key);
    1355                 :          0 :         os_free(data->eap_fast_a_id);
    1356                 :          0 :         os_free(data->eap_fast_a_id_info);
    1357                 :          0 :         os_free(data->eap_req_id_text);
    1358                 :            : #ifdef CONFIG_RADIUS_TEST
    1359                 :            :         os_free(data->dump_msk_file);
    1360                 :            : #endif /* CONFIG_RADIUS_TEST */
    1361                 :          0 :         os_free(data);
    1362                 :            : }
    1363                 :            : 
    1364                 :            : 
    1365                 :            : /**
    1366                 :            :  * radius_server_get_mib - Get RADIUS server MIB information
    1367                 :            :  * @data: RADIUS server context from radius_server_init()
    1368                 :            :  * @buf: Buffer for returning the MIB data in text format
    1369                 :            :  * @buflen: buf length in octets
    1370                 :            :  * Returns: Number of octets written into buf
    1371                 :            :  */
    1372                 :          0 : int radius_server_get_mib(struct radius_server_data *data, char *buf,
    1373                 :            :                           size_t buflen)
    1374                 :            : {
    1375                 :            :         int ret, uptime;
    1376                 :            :         unsigned int idx;
    1377                 :            :         char *end, *pos;
    1378                 :            :         struct os_reltime now;
    1379                 :            :         struct radius_client *cli;
    1380                 :            : 
    1381                 :            :         /* RFC 2619 - RADIUS Authentication Server MIB */
    1382                 :            : 
    1383 [ #  # ][ #  # ]:          0 :         if (data == NULL || buflen == 0)
    1384                 :          0 :                 return 0;
    1385                 :            : 
    1386                 :          0 :         pos = buf;
    1387                 :          0 :         end = buf + buflen;
    1388                 :            : 
    1389                 :          0 :         os_get_reltime(&now);
    1390                 :          0 :         uptime = (now.sec - data->start_time.sec) * 100 +
    1391                 :          0 :                 ((now.usec - data->start_time.usec) / 10000) % 100;
    1392                 :          0 :         ret = os_snprintf(pos, end - pos,
    1393                 :            :                           "RADIUS-AUTH-SERVER-MIB\n"
    1394                 :            :                           "radiusAuthServIdent=hostapd\n"
    1395                 :            :                           "radiusAuthServUpTime=%d\n"
    1396                 :            :                           "radiusAuthServResetTime=0\n"
    1397                 :            :                           "radiusAuthServConfigReset=4\n",
    1398                 :            :                           uptime);
    1399 [ #  # ][ #  # ]:          0 :         if (ret < 0 || ret >= end - pos) {
    1400                 :          0 :                 *pos = '\0';
    1401                 :          0 :                 return pos - buf;
    1402                 :            :         }
    1403                 :          0 :         pos += ret;
    1404                 :            : 
    1405                 :          0 :         ret = os_snprintf(pos, end - pos,
    1406                 :            :                           "radiusAuthServTotalAccessRequests=%u\n"
    1407                 :            :                           "radiusAuthServTotalInvalidRequests=%u\n"
    1408                 :            :                           "radiusAuthServTotalDupAccessRequests=%u\n"
    1409                 :            :                           "radiusAuthServTotalAccessAccepts=%u\n"
    1410                 :            :                           "radiusAuthServTotalAccessRejects=%u\n"
    1411                 :            :                           "radiusAuthServTotalAccessChallenges=%u\n"
    1412                 :            :                           "radiusAuthServTotalMalformedAccessRequests=%u\n"
    1413                 :            :                           "radiusAuthServTotalBadAuthenticators=%u\n"
    1414                 :            :                           "radiusAuthServTotalPacketsDropped=%u\n"
    1415                 :            :                           "radiusAuthServTotalUnknownTypes=%u\n",
    1416                 :            :                           data->counters.access_requests,
    1417                 :            :                           data->counters.invalid_requests,
    1418                 :            :                           data->counters.dup_access_requests,
    1419                 :            :                           data->counters.access_accepts,
    1420                 :            :                           data->counters.access_rejects,
    1421                 :            :                           data->counters.access_challenges,
    1422                 :            :                           data->counters.malformed_access_requests,
    1423                 :            :                           data->counters.bad_authenticators,
    1424                 :            :                           data->counters.packets_dropped,
    1425                 :            :                           data->counters.unknown_types);
    1426 [ #  # ][ #  # ]:          0 :         if (ret < 0 || ret >= end - pos) {
    1427                 :          0 :                 *pos = '\0';
    1428                 :          0 :                 return pos - buf;
    1429                 :            :         }
    1430                 :          0 :         pos += ret;
    1431                 :            : 
    1432         [ #  # ]:          0 :         for (cli = data->clients, idx = 0; cli; cli = cli->next, idx++) {
    1433                 :            :                 char abuf[50], mbuf[50];
    1434                 :            : #ifdef CONFIG_IPV6
    1435         [ #  # ]:          0 :                 if (data->ipv6) {
    1436         [ #  # ]:          0 :                         if (inet_ntop(AF_INET6, &cli->addr6, abuf,
    1437                 :            :                                       sizeof(abuf)) == NULL)
    1438                 :          0 :                                 abuf[0] = '\0';
    1439         [ #  # ]:          0 :                         if (inet_ntop(AF_INET6, &cli->mask6, abuf,
    1440                 :            :                                       sizeof(mbuf)) == NULL)
    1441                 :          0 :                                 mbuf[0] = '\0';
    1442                 :            :                 }
    1443                 :            : #endif /* CONFIG_IPV6 */
    1444         [ #  # ]:          0 :                 if (!data->ipv6) {
    1445                 :          0 :                         os_strlcpy(abuf, inet_ntoa(cli->addr), sizeof(abuf));
    1446                 :          0 :                         os_strlcpy(mbuf, inet_ntoa(cli->mask), sizeof(mbuf));
    1447                 :            :                 }
    1448                 :            : 
    1449                 :          0 :                 ret = os_snprintf(pos, end - pos,
    1450                 :            :                                   "radiusAuthClientIndex=%u\n"
    1451                 :            :                                   "radiusAuthClientAddress=%s/%s\n"
    1452                 :            :                                   "radiusAuthServAccessRequests=%u\n"
    1453                 :            :                                   "radiusAuthServDupAccessRequests=%u\n"
    1454                 :            :                                   "radiusAuthServAccessAccepts=%u\n"
    1455                 :            :                                   "radiusAuthServAccessRejects=%u\n"
    1456                 :            :                                   "radiusAuthServAccessChallenges=%u\n"
    1457                 :            :                                   "radiusAuthServMalformedAccessRequests=%u\n"
    1458                 :            :                                   "radiusAuthServBadAuthenticators=%u\n"
    1459                 :            :                                   "radiusAuthServPacketsDropped=%u\n"
    1460                 :            :                                   "radiusAuthServUnknownTypes=%u\n",
    1461                 :            :                                   idx,
    1462                 :            :                                   abuf, mbuf,
    1463                 :            :                                   cli->counters.access_requests,
    1464                 :            :                                   cli->counters.dup_access_requests,
    1465                 :            :                                   cli->counters.access_accepts,
    1466                 :            :                                   cli->counters.access_rejects,
    1467                 :            :                                   cli->counters.access_challenges,
    1468                 :            :                                   cli->counters.malformed_access_requests,
    1469                 :            :                                   cli->counters.bad_authenticators,
    1470                 :            :                                   cli->counters.packets_dropped,
    1471                 :            :                                   cli->counters.unknown_types);
    1472 [ #  # ][ #  # ]:          0 :                 if (ret < 0 || ret >= end - pos) {
    1473                 :          0 :                         *pos = '\0';
    1474                 :          0 :                         return pos - buf;
    1475                 :            :                 }
    1476                 :          0 :                 pos += ret;
    1477                 :            :         }
    1478                 :            : 
    1479                 :          0 :         return pos - buf;
    1480                 :            : }
    1481                 :            : 
    1482                 :            : 
    1483                 :          0 : static int radius_server_get_eap_user(void *ctx, const u8 *identity,
    1484                 :            :                                       size_t identity_len, int phase2,
    1485                 :            :                                       struct eap_user *user)
    1486                 :            : {
    1487                 :          0 :         struct radius_session *sess = ctx;
    1488                 :          0 :         struct radius_server_data *data = sess->server;
    1489                 :            : 
    1490                 :          0 :         return data->get_eap_user(data->conf_ctx, identity, identity_len,
    1491                 :            :                                   phase2, user);
    1492                 :            : }
    1493                 :            : 
    1494                 :            : 
    1495                 :          0 : static const char * radius_server_get_eap_req_id_text(void *ctx, size_t *len)
    1496                 :            : {
    1497                 :          0 :         struct radius_session *sess = ctx;
    1498                 :          0 :         struct radius_server_data *data = sess->server;
    1499                 :          0 :         *len = data->eap_req_id_text_len;
    1500                 :          0 :         return data->eap_req_id_text;
    1501                 :            : }
    1502                 :            : 
    1503                 :            : 
    1504                 :            : static struct eapol_callbacks radius_server_eapol_cb =
    1505                 :            : {
    1506                 :            :         .get_eap_user = radius_server_get_eap_user,
    1507                 :            :         .get_eap_req_id_text = radius_server_get_eap_req_id_text,
    1508                 :            : };
    1509                 :            : 
    1510                 :            : 
    1511                 :            : /**
    1512                 :            :  * radius_server_eap_pending_cb - Pending EAP data notification
    1513                 :            :  * @data: RADIUS server context from radius_server_init()
    1514                 :            :  * @ctx: Pending EAP context pointer
    1515                 :            :  *
    1516                 :            :  * This function is used to notify EAP server module that a pending operation
    1517                 :            :  * has been completed and processing of the EAP session can proceed.
    1518                 :            :  */
    1519                 :          0 : void radius_server_eap_pending_cb(struct radius_server_data *data, void *ctx)
    1520                 :            : {
    1521                 :            :         struct radius_client *cli;
    1522                 :          0 :         struct radius_session *s, *sess = NULL;
    1523                 :            :         struct radius_msg *msg;
    1524                 :            : 
    1525         [ #  # ]:          0 :         if (data == NULL)
    1526                 :          0 :                 return;
    1527                 :            : 
    1528         [ #  # ]:          0 :         for (cli = data->clients; cli; cli = cli->next) {
    1529         [ #  # ]:          0 :                 for (s = cli->sessions; s; s = s->next) {
    1530 [ #  # ][ #  # ]:          0 :                         if (s->eap == ctx && s->last_msg) {
    1531                 :          0 :                                 sess = s;
    1532                 :          0 :                                 break;
    1533                 :            :                         }
    1534         [ #  # ]:          0 :                         if (sess)
    1535                 :          0 :                                 break;
    1536                 :            :                 }
    1537         [ #  # ]:          0 :                 if (sess)
    1538                 :          0 :                         break;
    1539                 :            :         }
    1540                 :            : 
    1541         [ #  # ]:          0 :         if (sess == NULL) {
    1542                 :          0 :                 RADIUS_DEBUG("No session matched callback ctx");
    1543                 :          0 :                 return;
    1544                 :            :         }
    1545                 :            : 
    1546                 :          0 :         msg = sess->last_msg;
    1547                 :          0 :         sess->last_msg = NULL;
    1548                 :          0 :         eap_sm_pending_cb(sess->eap);
    1549         [ #  # ]:          0 :         if (radius_server_request(data, msg,
    1550                 :          0 :                                   (struct sockaddr *) &sess->last_from,
    1551                 :            :                                   sess->last_fromlen, cli,
    1552                 :          0 :                                   sess->last_from_addr,
    1553                 :            :                                   sess->last_from_port, sess) == -2)
    1554                 :          0 :                 return; /* msg was stored with the session */
    1555                 :            : 
    1556                 :          0 :         radius_msg_free(msg);
    1557                 :            : }

Generated by: LCOV version 1.9