mka: Reorganize loops in number of KaY functions

Use for loop to remove unnecessary goto use and similar cleanup to
simplify the loops in ieee802_1x_mka_i_in_peerlist(),
ieee802_1x_mka_decode_live_peer_body(), and
ieee802_1x_kay_decode_mkpdu().

Signed-off-by: Sabrina Dubroca <sd@queasysnail.net>
This commit is contained in:
Sabrina Dubroca 2016-08-12 15:07:33 +02:00 committed by Jouni Malinen
parent de7f5337f4
commit b3df7836e8

View file

@ -948,27 +948,24 @@ static Boolean
ieee802_1x_mka_i_in_peerlist(struct ieee802_1x_mka_participant *participant, ieee802_1x_mka_i_in_peerlist(struct ieee802_1x_mka_participant *participant,
const u8 *mka_msg, size_t msg_len) const u8 *mka_msg, size_t msg_len)
{ {
Boolean included = FALSE;
struct ieee802_1x_mka_hdr *hdr; struct ieee802_1x_mka_hdr *hdr;
size_t body_len; size_t body_len;
size_t left_len; size_t left_len;
u8 body_type; u8 body_type;
u32 peer_mn;
be32 _peer_mn;
const u8 *peer_mi;
const u8 *pos; const u8 *pos;
size_t i; size_t i;
pos = mka_msg; for (pos = mka_msg, left_len = msg_len;
left_len = msg_len; left_len > MKA_HDR_LEN + DEFAULT_ICV_LEN;
while (left_len > (MKA_HDR_LEN + DEFAULT_ICV_LEN)) { left_len -= body_len + MKA_HDR_LEN,
pos += body_len + MKA_HDR_LEN) {
hdr = (struct ieee802_1x_mka_hdr *) pos; hdr = (struct ieee802_1x_mka_hdr *) pos;
body_len = get_mka_param_body_len(hdr); body_len = get_mka_param_body_len(hdr);
body_type = get_mka_param_body_type(hdr); body_type = get_mka_param_body_type(hdr);
if (body_type != MKA_LIVE_PEER_LIST && if (body_type != MKA_LIVE_PEER_LIST &&
body_type != MKA_POTENTIAL_PEER_LIST) body_type != MKA_POTENTIAL_PEER_LIST)
goto SKIP_PEER; continue;
ieee802_1x_mka_dump_peer_body( ieee802_1x_mka_dump_peer_body(
(struct ieee802_1x_mka_peer_body *)pos); (struct ieee802_1x_mka_peer_body *)pos);
@ -978,34 +975,27 @@ ieee802_1x_mka_i_in_peerlist(struct ieee802_1x_mka_participant *participant,
"KaY: MKA Peer Packet Body Length (%zu bytes) is less than the Parameter Set Header Length (%zu bytes) + the Parameter Set Body Length (%zu bytes) + %d bytes of ICV", "KaY: MKA Peer Packet Body Length (%zu bytes) is less than the Parameter Set Header Length (%zu bytes) + the Parameter Set Body Length (%zu bytes) + %d bytes of ICV",
left_len, MKA_HDR_LEN, left_len, MKA_HDR_LEN,
body_len, DEFAULT_ICV_LEN); body_len, DEFAULT_ICV_LEN);
goto SKIP_PEER; continue;
} }
if ((body_len % 16) != 0) { if ((body_len % 16) != 0) {
wpa_printf(MSG_ERROR, wpa_printf(MSG_ERROR,
"KaY: MKA Peer Packet Body Length (%zu bytes) should be a multiple of 16 octets", "KaY: MKA Peer Packet Body Length (%zu bytes) should be a multiple of 16 octets",
body_len); body_len);
goto SKIP_PEER; continue;
} }
for (i = 0; i < body_len; i += MI_LEN + sizeof(peer_mn)) { for (i = 0; i < body_len;
peer_mi = MKA_HDR_LEN + pos + i; i += sizeof(struct ieee802_1x_mka_peer_id)) {
os_memcpy(&_peer_mn, peer_mi + MI_LEN, const struct ieee802_1x_mka_peer_id *peer_mi;
sizeof(_peer_mn));
peer_mn = be_to_host32(_peer_mn);
if (os_memcmp(peer_mi, participant->mi, MI_LEN) == 0 &&
peer_mn == participant->mn) {
included = TRUE;
break;
}
}
if (included) peer_mi = (const struct ieee802_1x_mka_peer_id *)
(pos + MKA_HDR_LEN + i);
if (os_memcmp(peer_mi->mi, participant->mi,
MI_LEN) == 0 &&
be_to_host32(peer_mi->mn) == participant->mn)
return TRUE; return TRUE;
}
SKIP_PEER:
left_len -= body_len + MKA_HDR_LEN;
pos += body_len + MKA_HDR_LEN;
} }
return FALSE; return FALSE;
@ -1022,9 +1012,6 @@ static int ieee802_1x_mka_decode_live_peer_body(
const struct ieee802_1x_mka_hdr *hdr; const struct ieee802_1x_mka_hdr *hdr;
struct ieee802_1x_kay_peer *peer; struct ieee802_1x_kay_peer *peer;
size_t body_len; size_t body_len;
u32 peer_mn;
be32 _peer_mn;
const u8 *peer_mi;
size_t i; size_t i;
Boolean is_included; Boolean is_included;
@ -1040,10 +1027,13 @@ static int ieee802_1x_mka_decode_live_peer_body(
return -1; return -1;
} }
for (i = 0; i < body_len; i += MI_LEN + sizeof(peer_mn)) { for (i = 0; i < body_len; i += sizeof(struct ieee802_1x_mka_peer_id)) {
peer_mi = MKA_HDR_LEN + peer_msg + i; const struct ieee802_1x_mka_peer_id *peer_mi;
os_memcpy(&_peer_mn, peer_mi + MI_LEN, sizeof(_peer_mn)); u32 peer_mn;
peer_mn = be_to_host32(_peer_mn);
peer_mi = (const struct ieee802_1x_mka_peer_id *)
(peer_msg + MKA_HDR_LEN + i);
peer_mn = be_to_host32(peer_mi->mn);
/* it is myself */ /* it is myself */
if (os_memcmp(peer_mi, participant->mi, MI_LEN) == 0) { if (os_memcmp(peer_mi, participant->mi, MI_LEN) == 0) {
@ -1053,20 +1043,19 @@ static int ieee802_1x_mka_decode_live_peer_body(
wpa_printf(MSG_DEBUG, "KaY: Could not update mi"); wpa_printf(MSG_DEBUG, "KaY: Could not update mi");
continue; continue;
} }
if (!is_included) if (!is_included)
continue; continue;
peer = ieee802_1x_kay_get_peer(participant, peer_mi); peer = ieee802_1x_kay_get_peer(participant, peer_mi->mi);
if (NULL != peer) { if (peer) {
peer->mn = peer_mn; peer->mn = peer_mn;
peer->expire = time(NULL) + MKA_LIFE_TIME / 1000; peer->expire = time(NULL) + MKA_LIFE_TIME / 1000;
} else { } else if (!ieee802_1x_kay_create_potential_peer(
if (!ieee802_1x_kay_create_potential_peer( participant, peer_mi->mi, peer_mn)) {
participant, peer_mi, peer_mn)) {
return -1; return -1;
} }
} }
}
return 0; return 0;
} }
@ -3063,7 +3052,9 @@ static int ieee802_1x_kay_decode_mkpdu(struct ieee802_1x_kay *kay,
handled[i] = FALSE; handled[i] = FALSE;
handled[0] = TRUE; handled[0] = TRUE;
while (left_len > MKA_HDR_LEN + DEFAULT_ICV_LEN) { for (; left_len > MKA_HDR_LEN + DEFAULT_ICV_LEN;
pos += body_len + MKA_HDR_LEN,
left_len -= body_len + MKA_HDR_LEN) {
hdr = (struct ieee802_1x_mka_hdr *) pos; hdr = (struct ieee802_1x_mka_hdr *) pos;
body_len = get_mka_param_body_len(hdr); body_len = get_mka_param_body_len(hdr);
body_type = get_mka_param_body_type(hdr); body_type = get_mka_param_body_type(hdr);
@ -3076,11 +3067,11 @@ static int ieee802_1x_kay_decode_mkpdu(struct ieee802_1x_kay *kay,
"KaY: MKA Peer Packet Body Length (%zu bytes) is less than the Parameter Set Header Length (%zu bytes) + the Parameter Set Body Length (%zu bytes) + %d bytes of ICV", "KaY: MKA Peer Packet Body Length (%zu bytes) is less than the Parameter Set Header Length (%zu bytes) + the Parameter Set Body Length (%zu bytes) + %d bytes of ICV",
left_len, MKA_HDR_LEN, left_len, MKA_HDR_LEN,
body_len, DEFAULT_ICV_LEN); body_len, DEFAULT_ICV_LEN);
goto next_para_set; continue;
} }
if (handled[body_type]) if (handled[body_type])
goto next_para_set; continue;
handled[body_type] = TRUE; handled[body_type] = TRUE;
if (body_type < ARRAY_SIZE(mka_body_handler) && if (body_type < ARRAY_SIZE(mka_body_handler) &&
@ -3092,10 +3083,6 @@ static int ieee802_1x_kay_decode_mkpdu(struct ieee802_1x_kay *kay,
"The type %d is not supported in this MKA version %d", "The type %d is not supported in this MKA version %d",
body_type, MKA_VERSION_ID); body_type, MKA_VERSION_ID);
} }
next_para_set:
pos += body_len + MKA_HDR_LEN;
left_len -= body_len + MKA_HDR_LEN;
} }
kay->active = TRUE; kay->active = TRUE;