Change radius_msg_free() to free the buffer

Since all callers were freeing the buffer immediately anyway, move
this operation into radius_msg_free() to reduce code size.
This commit is contained in:
Jouni Malinen 2009-12-19 16:34:41 +02:00
parent d94f86d85e
commit 9e7245bdb4
9 changed files with 46 additions and 76 deletions

View 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;
}

View 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 */

View 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) {

View 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;
}

View 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;
}

View 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);

View 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);
}

View 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);
}

View 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) {