nl80211: Simplify if_indices tracking

Maintain a single array (of struct with two int variables) instead of
two independent arrays (of int) for tracking know ifindexes and reasons
for having added them. The previous implementation tried to maintain two
independent arrays even though they were always required to be of
exactly same length and order. That had resulted in a bug earlier and
the code was not exactly easy to understand either, so replace this with
a single array.

Signed-off-by: Jouni Malinen <j@w1.fi>
This commit is contained in:
Jouni Malinen 2019-04-28 21:05:54 +03:00
parent e6ca2f1139
commit 099224c18e
2 changed files with 26 additions and 48 deletions

View File

@ -2010,9 +2010,8 @@ static void * wpa_driver_nl80211_drv_init(void *ctx, const char *ifname,
*/ */
drv->set_rekey_offload = 1; drv->set_rekey_offload = 1;
drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int); drv->num_if_indices = ARRAY_SIZE(drv->default_if_indices);
drv->if_indices = drv->default_if_indices; drv->if_indices = drv->default_if_indices;
drv->if_indices_reason = drv->default_if_indices_reason;
drv->first_bss = os_zalloc(sizeof(*drv->first_bss)); drv->first_bss = os_zalloc(sizeof(*drv->first_bss));
if (!drv->first_bss) { if (!drv->first_bss) {
@ -2789,9 +2788,6 @@ static void wpa_driver_nl80211_deinit(struct i802_bss *bss)
if (drv->if_indices != drv->default_if_indices) if (drv->if_indices != drv->default_if_indices)
os_free(drv->if_indices); os_free(drv->if_indices);
if (drv->if_indices_reason != drv->default_if_indices_reason)
os_free(drv->if_indices_reason);
if (drv->disabled_11b_rates) if (drv->disabled_11b_rates)
nl80211_disable_11b_rates(drv, drv->ifindex, 0); nl80211_disable_11b_rates(drv, drv->ifindex, 0);
@ -6620,11 +6616,11 @@ static void dump_ifidx(struct wpa_driver_nl80211_data *drv)
end = pos + sizeof(buf); end = pos + sizeof(buf);
for (i = 0; i < drv->num_if_indices; i++) { for (i = 0; i < drv->num_if_indices; i++) {
if (!drv->if_indices[i]) if (!drv->if_indices[i].ifindex)
continue; continue;
res = os_snprintf(pos, end - pos, " %d(%d)", res = os_snprintf(pos, end - pos, " %d(%d)",
drv->if_indices[i], drv->if_indices[i].ifindex,
drv->if_indices_reason[i]); drv->if_indices[i].reason);
if (os_snprintf_error(end - pos, res)) if (os_snprintf_error(end - pos, res))
break; break;
pos += res; pos += res;
@ -6640,8 +6636,7 @@ static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
int ifidx_reason) int ifidx_reason)
{ {
int i; int i;
int *old, *old_reason; struct drv_nl80211_if_info *old;
int alloc_failed = 0;
wpa_printf(MSG_DEBUG, wpa_printf(MSG_DEBUG,
"nl80211: Add own interface ifindex %d (ifidx_reason %d)", "nl80211: Add own interface ifindex %d (ifidx_reason %d)",
@ -6652,9 +6647,9 @@ static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
return; return;
} }
for (i = 0; i < drv->num_if_indices; i++) { for (i = 0; i < drv->num_if_indices; i++) {
if (drv->if_indices[i] == 0) { if (drv->if_indices[i].ifindex == 0) {
drv->if_indices[i] = ifidx; drv->if_indices[i].ifindex = ifidx;
drv->if_indices_reason[i] = ifidx_reason; drv->if_indices[i].reason = ifidx_reason;
dump_ifidx(drv); dump_ifidx(drv);
return; return;
} }
@ -6665,31 +6660,13 @@ static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
else else
old = NULL; old = NULL;
if (drv->if_indices_reason != drv->default_if_indices_reason)
old_reason = drv->if_indices_reason;
else
old_reason = NULL;
drv->if_indices = os_realloc_array(old, drv->num_if_indices + 1, drv->if_indices = os_realloc_array(old, drv->num_if_indices + 1,
sizeof(int)); sizeof(*old));
drv->if_indices_reason = os_realloc_array(old_reason,
drv->num_if_indices + 1,
sizeof(int));
if (!drv->if_indices) { if (!drv->if_indices) {
if (!old) if (!old)
drv->if_indices = drv->default_if_indices; drv->if_indices = drv->default_if_indices;
else else
drv->if_indices = old; drv->if_indices = old;
alloc_failed = 1;
}
if (!drv->if_indices_reason) {
if (!old_reason)
drv->if_indices_reason = drv->default_if_indices_reason;
else
drv->if_indices_reason = old_reason;
alloc_failed = 1;
}
if (alloc_failed) {
wpa_printf(MSG_ERROR, "Failed to reallocate memory for " wpa_printf(MSG_ERROR, "Failed to reallocate memory for "
"interfaces"); "interfaces");
wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx); wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx);
@ -6698,12 +6675,8 @@ static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
if (!old) if (!old)
os_memcpy(drv->if_indices, drv->default_if_indices, os_memcpy(drv->if_indices, drv->default_if_indices,
sizeof(drv->default_if_indices)); sizeof(drv->default_if_indices));
if (!old_reason) drv->if_indices[drv->num_if_indices].ifindex = ifidx;
os_memcpy(drv->if_indices_reason, drv->if_indices[drv->num_if_indices].reason = ifidx_reason;
drv->default_if_indices_reason,
sizeof(drv->default_if_indices_reason));
drv->if_indices[drv->num_if_indices] = ifidx;
drv->if_indices_reason[drv->num_if_indices] = ifidx_reason;
drv->num_if_indices++; drv->num_if_indices++;
dump_ifidx(drv); dump_ifidx(drv);
} }
@ -6715,10 +6688,12 @@ static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
int i; int i;
for (i = 0; i < drv->num_if_indices; i++) { for (i = 0; i < drv->num_if_indices; i++) {
if ((drv->if_indices[i] == ifidx || ifidx == IFIDX_ANY) && if ((drv->if_indices[i].ifindex == ifidx ||
(drv->if_indices_reason[i] == ifidx_reason || ifidx == IFIDX_ANY) &&
(drv->if_indices[i].reason == ifidx_reason ||
ifidx_reason == IFIDX_ANY)) { ifidx_reason == IFIDX_ANY)) {
drv->if_indices[i] = 0; drv->if_indices[i].ifindex = 0;
drv->if_indices[i].reason = 0;
break; break;
} }
} }
@ -6732,8 +6707,8 @@ static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
int i; int i;
for (i = 0; i < drv->num_if_indices; i++) for (i = 0; i < drv->num_if_indices; i++)
if (drv->if_indices[i] == ifidx && if (drv->if_indices[i].ifindex == ifidx &&
(drv->if_indices_reason[i] == ifidx_reason || (drv->if_indices[i].reason == ifidx_reason ||
ifidx_reason == IFIDX_ANY)) ifidx_reason == IFIDX_ANY))
return 1; return 1;

View File

@ -83,6 +83,12 @@ struct i802_bss {
u8 rand_addr[ETH_ALEN]; u8 rand_addr[ETH_ALEN];
}; };
struct drv_nl80211_if_info {
int ifindex;
/* the AP/AP_VLAN iface that is in this bridge */
int reason;
};
struct wpa_driver_nl80211_data { struct wpa_driver_nl80211_data {
struct nl80211_global *global; struct nl80211_global *global;
struct dl_list list; struct dl_list list;
@ -187,11 +193,8 @@ struct wpa_driver_nl80211_data {
struct nl_handle *rtnl_sk; /* nl_sock for NETLINK_ROUTE */ struct nl_handle *rtnl_sk; /* nl_sock for NETLINK_ROUTE */
int default_if_indices[16]; struct drv_nl80211_if_info default_if_indices[16];
/* the AP/AP_VLAN iface that is in this bridge */ struct drv_nl80211_if_info *if_indices;
int default_if_indices_reason[16];
int *if_indices;
int *if_indices_reason;
int num_if_indices; int num_if_indices;
/* From failed authentication command */ /* From failed authentication command */