]> git.ipfire.org Git - thirdparty/hostap.git/commitdiff
Change radius_msg_free() to free the buffer
authorJouni Malinen <j@w1.fi>
Sat, 19 Dec 2009 14:34:41 +0000 (16:34 +0200)
committerJouni Malinen <j@w1.fi>
Sat, 19 Dec 2009 14:34:41 +0000 (16:34 +0200)
Since all callers were freeing the buffer immediately anyway, move
this operation into radius_msg_free() to reduce code size.

hostapd/accounting.c
hostapd/ieee802_11_auth.c
hostapd/ieee802_1x.c
radius_example/radius_example.c
src/radius/radius.c
src/radius/radius.h
src/radius/radius_client.c
src/radius/radius_server.c
wpa_supplicant/eapol_test.c

index 8adaf88868368611e54dadcb5eab2e3172a1b6ad..9297650d0d935809ed92678d4d36bbaaddb20a40 100644 (file)
@@ -177,7 +177,6 @@ static struct radius_msg * accounting_msg(struct hostapd_data *hapd,
 
  fail:
        radius_msg_free(msg);
-       os_free(msg);
        return NULL;
 }
 
@@ -366,7 +365,6 @@ static void accounting_sta_report(struct hostapd_data *hapd,
 
  fail:
        radius_msg_free(msg);
-       os_free(msg);
 }
 
 
@@ -462,7 +460,6 @@ static void accounting_report_state(struct hostapd_data *hapd, int on)
        {
                printf("Could not add Acct-Terminate-Cause\n");
                radius_msg_free(msg);
-               os_free(msg);
                return;
        }
 
index 981067c68b0a5fe3b635535e737dd4a9b765b6fe..9329aac6c985162f24f779cbd9c787e05cb2113a 100644 (file)
@@ -200,7 +200,6 @@ static int hostapd_radius_acl_query(struct hostapd_data *hapd, const u8 *addr,
 
  fail:
        radius_msg_free(msg);
-       os_free(msg);
        return -1;
 }
 #endif /* CONFIG_NO_RADIUS */
index 813ee5e52b4d044ee8af9c2f52895106af5ea62f..89a11f2de77ad00643ccb4f30a02eb8d144a8853 100644 (file)
@@ -562,7 +562,6 @@ static void ieee802_1x_encapsulate_radius(struct hostapd_data *hapd,
 
  fail:
        radius_msg_free(msg);
-       os_free(msg);
 }
 #endif /* CONFIG_NO_RADIUS */
 
@@ -926,10 +925,7 @@ void ieee802_1x_free_station(struct sta_info *sta)
        sta->eapol_sm = NULL;
 
 #ifndef CONFIG_NO_RADIUS
-       if (sm->last_recv_radius) {
-               radius_msg_free(sm->last_recv_radius);
-               os_free(sm->last_recv_radius);
-       }
+       radius_msg_free(sm->last_recv_radius);
        radius_free_class(&sm->radius_class);
 #endif /* CONFIG_NO_RADIUS */
 
@@ -1241,11 +1237,7 @@ ieee802_1x_receive_auth(struct radius_msg *msg, struct radius_msg *req,
        wpa_printf(MSG_DEBUG, "RADIUS packet matching with station " MACSTR,
                   MAC2STR(sta->addr));
 
-       if (sm->last_recv_radius) {
-               radius_msg_free(sm->last_recv_radius);
-               os_free(sm->last_recv_radius);
-       }
-
+       radius_msg_free(sm->last_recv_radius);
        sm->last_recv_radius = msg;
 
        session_timeout_set =
@@ -1368,11 +1360,8 @@ void ieee802_1x_abort_auth(struct hostapd_data *hapd, struct sta_info *sta)
                       HOSTAPD_LEVEL_DEBUG, "aborting authentication");
 
 #ifndef CONFIG_NO_RADIUS
-       if (sm->last_recv_radius) {
-               radius_msg_free(sm->last_recv_radius);
-               os_free(sm->last_recv_radius);
-               sm->last_recv_radius = NULL;
-       }
+       radius_msg_free(sm->last_recv_radius);
+       sm->last_recv_radius = NULL;
 #endif /* CONFIG_NO_RADIUS */
 
        if (sm->eap_if->eapTimeout) {
index 1b27efc3105445ce4528d015b953e7017f2abe4a..59982abbd2dfa58ca00f9bacfadaa66cce70a37f 100644 (file)
@@ -74,7 +74,6 @@ static void start_example(void *eloop_ctx, void *timeout_ctx)
                                 (u8 *) "user", 4)) {
                printf("Could not add User-Name\n");
                radius_msg_free(msg);
-               os_free(msg);
                return;
        }
 
@@ -84,7 +83,6 @@ static void start_example(void *eloop_ctx, void *timeout_ctx)
                    ctx->conf.auth_server->shared_secret_len)) {
                printf("Could not add User-Password\n");
                radius_msg_free(msg);
-               os_free(msg);
                return;
        }
 
@@ -92,7 +90,6 @@ static void start_example(void *eloop_ctx, void *timeout_ctx)
                                 (u8 *) &ctx->own_ip_addr, 4)) {
                printf("Could not add NAS-IP-Address\n");
                radius_msg_free(msg);
-               os_free(msg);
                return;
        }
 
index 7b69c9b72b1a5bcf5284ca279ff5a5a348ad3cfd..a00cee56912bd076458f58444bc4c2785f6a9d99 100644 (file)
@@ -63,6 +63,15 @@ static int radius_msg_initialize(struct radius_msg *msg, size_t init_len)
 }
 
 
+/**
+ * radius_msg_new - Create a new RADIUS message
+ * @code: Code for RADIUS header
+ * @identifier: Identifier for RADIUS header
+ * Returns: Context for RADIUS message or %NULL on failure
+ *
+ * The caller is responsible for freeing the returned data with
+ * radius_msg_free().
+ */
 struct radius_msg * radius_msg_new(u8 code, u8 identifier)
 {
        struct radius_msg *msg;
@@ -82,16 +91,18 @@ struct radius_msg * radius_msg_new(u8 code, u8 identifier)
 }
 
 
+/**
+ * radius_msg_free - Free a RADIUS message
+ * @msg: RADIUS message from radius_msg_new() or radius_msg_parse()
+ */
 void radius_msg_free(struct radius_msg *msg)
 {
-       os_free(msg->buf);
-       msg->buf = NULL;
-       msg->hdr = NULL;
-       msg->buf_size = msg->buf_used = 0;
+       if (msg == NULL)
+               return;
 
+       os_free(msg->buf);
        os_free(msg->attr_pos);
-       msg->attr_pos = NULL;
-       msg->attr_size = msg->attr_used = 0;
+       os_free(msg);
 }
 
 
@@ -452,7 +463,16 @@ struct radius_attr_hdr *radius_msg_add_attr(struct radius_msg *msg, u8 type,
 }
 
 
-struct radius_msg *radius_msg_parse(const u8 *data, size_t len)
+/**
+ * radius_msg_parse - Parse a RADIUS message
+ * @data: RADIUS message to be parsed
+ * @len: Length of data buffer in octets
+ * Returns: Parsed RADIUS message or %NULL on failure
+ *
+ * This parses a RADIUS message and makes a copy of its data. The caller is
+ * responsible for freeing the returned data with radius_msg_free().
+ */
+struct radius_msg * radius_msg_parse(const u8 *data, size_t len)
 {
        struct radius_msg *msg;
        struct radius_hdr *hdr;
@@ -467,13 +487,13 @@ struct radius_msg *radius_msg_parse(const u8 *data, size_t len)
 
        msg_len = ntohs(hdr->length);
        if (msg_len < sizeof(*hdr) || msg_len > len) {
-               printf("Invalid RADIUS message length\n");
+               wpa_printf(MSG_INFO, "RADIUS: Invalid message length");
                return NULL;
        }
 
        if (msg_len < len) {
-               printf("Ignored %lu extra bytes after RADIUS message\n",
-                      (unsigned long) len - msg_len);
+               wpa_printf(MSG_DEBUG, "RADIUS: Ignored %lu extra bytes after "
+                          "RADIUS message", (unsigned long) len - msg_len);
        }
 
        msg = os_zalloc(sizeof(*msg));
@@ -512,7 +532,6 @@ struct radius_msg *radius_msg_parse(const u8 *data, size_t len)
 
  fail:
        radius_msg_free(msg);
-       os_free(msg);
        return NULL;
 }
 
index 9f77a5af92fe3e321153b0574113528d4030e89b..6050b54082f715dddcece74c12f2567af4fa548d 100644 (file)
@@ -230,7 +230,7 @@ struct radius_msg {
 /* MAC address ASCII format for non-802.1X use */
 #define RADIUS_ADDR_FORMAT "%02x%02x%02x%02x%02x%02x"
 
-struct radius_msg *radius_msg_new(u8 code, u8 identifier);
+struct radius_msg * radius_msg_new(u8 code, u8 identifier);
 void radius_msg_free(struct radius_msg *msg);
 void radius_msg_dump(struct radius_msg *msg);
 int radius_msg_finish(struct radius_msg *msg, const u8 *secret,
@@ -239,9 +239,9 @@ int radius_msg_finish_srv(struct radius_msg *msg, const u8 *secret,
                          size_t secret_len, const u8 *req_authenticator);
 void radius_msg_finish_acct(struct radius_msg *msg, const u8 *secret,
                            size_t secret_len);
-struct radius_attr_hdr *radius_msg_add_attr(struct radius_msg *msg, u8 type,
-                                           const u8 *data, size_t data_len);
-struct radius_msg *radius_msg_parse(const u8 *data, size_t len);
+struct radius_attr_hdr * radius_msg_add_attr(struct radius_msg *msg, u8 type,
+                                            const u8 *data, size_t data_len);
+struct radius_msg * radius_msg_parse(const u8 *data, size_t len);
 int radius_msg_add_eap(struct radius_msg *msg, const u8 *data,
                       size_t data_len);
 u8 *radius_msg_get_eap(struct radius_msg *msg, size_t *len);
index 4cb9c396309845d50a8b84c83267caa4b4a029ea..0ace8d15554bb5b16ed73297e8566a6d32b08c80 100644 (file)
@@ -247,7 +247,6 @@ static int radius_client_init_auth(struct radius_client_data *radius);
 static void radius_client_msg_free(struct radius_msg_list *req)
 {
        radius_msg_free(req->msg);
-       os_free(req->msg);
        os_free(req);
 }
 
@@ -526,7 +525,6 @@ static void radius_client_list_add(struct radius_client_data *radius,
                /* No point in adding entries to retransmit queue since event
                 * loop has already been terminated. */
                radius_msg_free(msg);
-               os_free(msg);
                return;
        }
 
@@ -534,7 +532,6 @@ static void radius_client_list_add(struct radius_client_data *radius,
        if (entry == NULL) {
                printf("Failed to add RADIUS packet into retransmit list\n");
                radius_msg_free(msg);
-               os_free(msg);
                return;
        }
 
@@ -802,7 +799,6 @@ static void radius_client_receive(int sock, void *eloop_ctx, void *sock_ctx)
                switch (res) {
                case RADIUS_RX_PROCESSED:
                        radius_msg_free(msg);
-                       os_free(msg);
                        /* continue */
                case RADIUS_RX_QUEUED:
                        radius_client_msg_free(req);
@@ -830,7 +826,6 @@ static void radius_client_receive(int sock, void *eloop_ctx, void *sock_ctx)
 
  fail:
        radius_msg_free(msg);
-       os_free(msg);
 }
 
 
index 635143f047e0bdb00144ea39a20bf389c99ea0fc..54bfdac4071a7c2ce9c5dbf57a571ab226d59da8 100644 (file)
@@ -359,15 +359,9 @@ static void radius_server_session_free(struct radius_server_data *data,
 {
        eloop_cancel_timeout(radius_server_session_timeout, data, sess);
        eap_server_sm_deinit(sess->eap);
-       if (sess->last_msg) {
-               radius_msg_free(sess->last_msg);
-               os_free(sess->last_msg);
-       }
+       radius_msg_free(sess->last_msg);
        os_free(sess->last_from_addr);
-       if (sess->last_reply) {
-               radius_msg_free(sess->last_reply);
-               os_free(sess->last_reply);
-       }
+       radius_msg_free(sess->last_reply);
        os_free(sess);
        data->num_sess--;
 }
@@ -584,7 +578,6 @@ radius_server_encapsulate_eap(struct radius_server_data *data,
        if (radius_msg_copy_attr(msg, request, RADIUS_ATTR_PROXY_STATE) < 0) {
                RADIUS_DEBUG("Failed to copy Proxy-State attribute(s)");
                radius_msg_free(msg);
-               os_free(msg);
                return NULL;
        }
 
@@ -629,7 +622,6 @@ static int radius_server_reject(struct radius_server_data *data,
        if (radius_msg_copy_attr(msg, request, RADIUS_ATTR_PROXY_STATE) < 0) {
                RADIUS_DEBUG("Failed to copy Proxy-State attribute(s)");
                radius_msg_free(msg);
-               os_free(msg);
                return -1;
        }
 
@@ -652,7 +644,6 @@ static int radius_server_reject(struct radius_server_data *data,
        }
 
        radius_msg_free(msg);
-       os_free(msg);
 
        return ret;
 }
@@ -763,10 +754,7 @@ static int radius_server_request(struct radius_server_data *data,
                RADIUS_DEBUG("No EAP data from the state machine, but eapFail "
                             "set");
        } else if (eap_sm_method_pending(sess->eap)) {
-               if (sess->last_msg) {
-                       radius_msg_free(sess->last_msg);
-                       os_free(sess->last_msg);
-               }
+               radius_msg_free(sess->last_msg);
                sess->last_msg = msg;
                sess->last_from_port = from_port;
                os_free(sess->last_from_addr);
@@ -813,10 +801,7 @@ static int radius_server_request(struct radius_server_data *data,
                if (res < 0) {
                        perror("sendto[RADIUS SRV]");
                }
-               if (sess->last_reply) {
-                       radius_msg_free(sess->last_reply);
-                       os_free(sess->last_reply);
-               }
+               radius_msg_free(sess->last_reply);
                sess->last_reply = reply;
                sess->last_from_port = from_port;
                sess->last_identifier = msg->hdr->identifier;
@@ -944,10 +929,7 @@ static void radius_server_receive_auth(int sock, void *eloop_ctx,
                return; /* msg was stored with the session */
 
 fail:
-       if (msg) {
-               radius_msg_free(msg);
-               os_free(msg);
-       }
+       radius_msg_free(msg);
        os_free(buf);
 }
 
@@ -1515,5 +1497,4 @@ void radius_server_eap_pending_cb(struct radius_server_data *data, void *ctx)
                return; /* msg was stored with the session */
 
        radius_msg_free(msg);
-       os_free(msg);
 }
index 4bac935a0e703f53b58b9b5b74bea600e7fb46d8..a1710e2be6c97985f448991caccad0007776ff35 100644 (file)
@@ -283,7 +283,6 @@ static void ieee802_1x_encapsulate_radius(struct eapol_test_data *e,
 
  fail:
        radius_msg_free(msg);
-       os_free(msg);
 }
 
 
@@ -440,10 +439,8 @@ static void test_eapol_clean(struct eapol_test_data *e,
 
        radius_client_deinit(e->radius);
        os_free(e->last_eap_radius);
-       if (e->last_recv_radius) {
-               radius_msg_free(e->last_recv_radius);
-               os_free(e->last_recv_radius);
-       }
+       radius_msg_free(e->last_recv_radius);
+       e->last_recv_radius = NULL;
        os_free(e->eap_identity);
        e->eap_identity = NULL;
        eapol_sm_deinit(wpa_s->eapol);
@@ -694,11 +691,7 @@ ieee802_1x_receive_auth(struct radius_msg *msg, struct radius_msg *req,
        e->radius_identifier = -1;
        wpa_printf(MSG_DEBUG, "RADIUS packet matching with station");
 
-       if (e->last_recv_radius) {
-               radius_msg_free(e->last_recv_radius);
-               os_free(e->last_recv_radius);
-       }
-
+       radius_msg_free(e->last_recv_radius);
        e->last_recv_radius = msg;
 
        switch (msg->hdr->code) {