bsd: Prepare event buffer on init process

Currently these three steps runs for each event.
1. get buffer size via system
2. allocate a memory for event
3. free the memory

The wpa_supplicant receives 4 events from boot to be connected.
So this patch prepare the event buffer at the init process.

I have tested wpa_supplicant on NetBSD 6.1.2.
But I could not tested hostapd because I do not have AP enabled device.

Signed-hostap: Masashi Honma <masashi.honma@gmail.com>
This commit is contained in:
Masashi Honma 2014-01-07 15:56:06 +02:00 committed by Jouni Malinen
parent 3043b4f455
commit 38bbd06ecf

View file

@ -62,6 +62,8 @@ struct bsd_driver_data {
int prev_privacy; /* privacy state to restore on deinit */ int prev_privacy; /* privacy state to restore on deinit */
int prev_wpa; /* wpa state to restore on deinit */ int prev_wpa; /* wpa state to restore on deinit */
enum ieee80211_opmode opmode; /* operation mode */ enum ieee80211_opmode opmode; /* operation mode */
char *event_buf;
size_t event_buf_len;
}; };
/* Generic functions for hostapd and wpa_supplicant */ /* Generic functions for hostapd and wpa_supplicant */
@ -642,7 +644,7 @@ bsd_set_opt_ie(void *priv, const u8 *ie, size_t ie_len)
return 0; return 0;
} }
static int static size_t
rtbuf_len(void) rtbuf_len(void)
{ {
size_t len; size_t len;
@ -779,37 +781,26 @@ static void
bsd_wireless_event_receive(int sock, void *ctx, void *sock_ctx) bsd_wireless_event_receive(int sock, void *ctx, void *sock_ctx)
{ {
struct bsd_driver_data *drv = ctx; struct bsd_driver_data *drv = ctx;
char *buf;
struct if_announcemsghdr *ifan; struct if_announcemsghdr *ifan;
struct rt_msghdr *rtm; struct rt_msghdr *rtm;
struct ieee80211_michael_event *mic; struct ieee80211_michael_event *mic;
struct ieee80211_join_event *join; struct ieee80211_join_event *join;
struct ieee80211_leave_event *leave; struct ieee80211_leave_event *leave;
int n, len; int n;
union wpa_event_data data; union wpa_event_data data;
len = rtbuf_len(); n = read(sock, drv->event_buf, drv->event_buf_len);
buf = os_malloc(len);
if (buf == NULL) {
wpa_printf(MSG_ERROR, "%s os_malloc() failed\n", __func__);
return;
}
n = read(sock, buf, len);
if (n < 0) { if (n < 0) {
if (errno != EINTR && errno != EAGAIN) if (errno != EINTR && errno != EAGAIN)
wpa_printf(MSG_ERROR, "%s read() failed: %s\n", wpa_printf(MSG_ERROR, "%s read() failed: %s\n",
__func__, strerror(errno)); __func__, strerror(errno));
os_free(buf);
return; return;
} }
rtm = (struct rt_msghdr *) buf; rtm = (struct rt_msghdr *) drv->event_buf;
if (rtm->rtm_version != RTM_VERSION) { if (rtm->rtm_version != RTM_VERSION) {
wpa_printf(MSG_DEBUG, "Invalid routing message version=%d", wpa_printf(MSG_DEBUG, "Invalid routing message version=%d",
rtm->rtm_version); rtm->rtm_version);
os_free(buf);
return; return;
} }
ifan = (struct if_announcemsghdr *) rtm; ifan = (struct if_announcemsghdr *) rtm;
@ -850,7 +841,6 @@ bsd_wireless_event_receive(int sock, void *ctx, void *sock_ctx)
} }
break; break;
} }
os_free(buf);
} }
static void static void
@ -871,6 +861,14 @@ bsd_init(struct hostapd_data *hapd, struct wpa_init_params *params)
goto bad; goto bad;
} }
drv->event_buf_len = rtbuf_len();
drv->event_buf = os_malloc(drv->event_buf_len);
if (drv->event_buf == NULL) {
wpa_printf(MSG_ERROR, "%s: os_malloc() failed", __func__);
goto bad;
}
drv->hapd = hapd; drv->hapd = hapd;
drv->sock = socket(PF_INET, SOCK_DGRAM, 0); drv->sock = socket(PF_INET, SOCK_DGRAM, 0);
if (drv->sock < 0) { if (drv->sock < 0) {
@ -910,6 +908,7 @@ bad:
l2_packet_deinit(drv->sock_xmit); l2_packet_deinit(drv->sock_xmit);
if (drv->sock >= 0) if (drv->sock >= 0)
close(drv->sock); close(drv->sock);
os_free(drv->event_buf);
if (drv != NULL) if (drv != NULL)
os_free(drv); os_free(drv);
return NULL; return NULL;
@ -930,6 +929,7 @@ bsd_deinit(void *priv)
close(drv->sock); close(drv->sock);
if (drv->sock_xmit != NULL) if (drv->sock_xmit != NULL)
l2_packet_deinit(drv->sock_xmit); l2_packet_deinit(drv->sock_xmit);
os_free(drv->event_buf);
os_free(drv); os_free(drv);
} }
@ -1208,7 +1208,6 @@ static void
wpa_driver_bsd_event_receive(int sock, void *ctx, void *sock_ctx) wpa_driver_bsd_event_receive(int sock, void *ctx, void *sock_ctx)
{ {
struct bsd_driver_data *drv = sock_ctx; struct bsd_driver_data *drv = sock_ctx;
char *buf;
struct if_announcemsghdr *ifan; struct if_announcemsghdr *ifan;
struct if_msghdr *ifm; struct if_msghdr *ifm;
struct rt_msghdr *rtm; struct rt_msghdr *rtm;
@ -1216,30 +1215,20 @@ wpa_driver_bsd_event_receive(int sock, void *ctx, void *sock_ctx)
struct ieee80211_michael_event *mic; struct ieee80211_michael_event *mic;
struct ieee80211_leave_event *leave; struct ieee80211_leave_event *leave;
struct ieee80211_join_event *join; struct ieee80211_join_event *join;
int n, len; int n;
len = rtbuf_len(); n = read(sock, drv->event_buf, drv->event_buf_len);
buf = os_malloc(len);
if (buf == NULL) {
wpa_printf(MSG_ERROR, "%s os_malloc() failed\n", __func__);
return;
}
n = read(sock, buf, len);
if (n < 0) { if (n < 0) {
if (errno != EINTR && errno != EAGAIN) if (errno != EINTR && errno != EAGAIN)
wpa_printf(MSG_ERROR, "%s read() failed: %s\n", wpa_printf(MSG_ERROR, "%s read() failed: %s\n",
__func__, strerror(errno)); __func__, strerror(errno));
os_free(buf);
return; return;
} }
rtm = (struct rt_msghdr *) buf; rtm = (struct rt_msghdr *) drv->event_buf;
if (rtm->rtm_version != RTM_VERSION) { if (rtm->rtm_version != RTM_VERSION) {
wpa_printf(MSG_DEBUG, "Invalid routing message version=%d", wpa_printf(MSG_DEBUG, "Invalid routing message version=%d",
rtm->rtm_version); rtm->rtm_version);
os_free(buf);
return; return;
} }
os_memset(&event, 0, sizeof(event)); os_memset(&event, 0, sizeof(event));
@ -1254,7 +1243,6 @@ wpa_driver_bsd_event_receive(int sock, void *ctx, void *sock_ctx)
case IFAN_DEPARTURE: case IFAN_DEPARTURE:
event.interface_status.ievent = EVENT_INTERFACE_REMOVED; event.interface_status.ievent = EVENT_INTERFACE_REMOVED;
default: default:
os_free(buf);
return; return;
} }
wpa_printf(MSG_DEBUG, "RTM_IFANNOUNCE: Interface '%s' %s", wpa_printf(MSG_DEBUG, "RTM_IFANNOUNCE: Interface '%s' %s",
@ -1327,7 +1315,6 @@ wpa_driver_bsd_event_receive(int sock, void *ctx, void *sock_ctx)
} }
break; break;
} }
os_free(buf);
} }
static void static void
@ -1508,6 +1495,15 @@ wpa_driver_bsd_init(void *ctx, const char *ifname)
drv = os_zalloc(sizeof(*drv)); drv = os_zalloc(sizeof(*drv));
if (drv == NULL) if (drv == NULL)
return NULL; return NULL;
drv->event_buf_len = rtbuf_len();
drv->event_buf = os_malloc(drv->event_buf_len);
if (drv->event_buf == NULL) {
wpa_printf(MSG_ERROR, "%s: os_malloc() failed", __func__);
goto fail1;
}
/* /*
* NB: We require the interface name be mappable to an index. * NB: We require the interface name be mappable to an index.
* This implies we do not support having wpa_supplicant * This implies we do not support having wpa_supplicant
@ -1562,6 +1558,7 @@ wpa_driver_bsd_init(void *ctx, const char *ifname)
fail: fail:
close(drv->sock); close(drv->sock);
fail1: fail1:
os_free(drv->event_buf);
os_free(drv); os_free(drv);
return NULL; return NULL;
#undef GETPARAM #undef GETPARAM
@ -1587,6 +1584,7 @@ wpa_driver_bsd_deinit(void *priv)
l2_packet_deinit(drv->sock_xmit); l2_packet_deinit(drv->sock_xmit);
(void) close(drv->route); /* ioctl socket */ (void) close(drv->route); /* ioctl socket */
(void) close(drv->sock); /* event socket */ (void) close(drv->sock); /* event socket */
os_free(drv->event_buf);
os_free(drv); os_free(drv);
} }