[PATCH 3/8] staging: ks7010: Refactor hostif_data_indicationyes

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Function hostif_data_indication has 14 local variables an is over 200
lines long. Contains code nested deeply. Function could be refactored.

Start to refactor function. Split out three separate functions. Don't
modify code except to make it compile, add/remove correct local
variables and add code to call new functions.

Signed-off-by: Tobin C. Harding <me@xxxxxxxx>
---
 drivers/staging/ks7010/ks_hostif.c | 267 +++++++++++++++++++++----------------
 1 file changed, 149 insertions(+), 118 deletions(-)

diff --git a/drivers/staging/ks7010/ks_hostif.c b/drivers/staging/ks7010/ks_hostif.c
index a976060..f4beb86 100644
--- a/drivers/staging/ks7010/ks_hostif.c
+++ b/drivers/staging/ks7010/ks_hostif.c
@@ -307,42 +307,9 @@ int get_ap_information(struct ks_wlan_private *priv, struct ap_info_t *ap_info,
 	return 0;
 }
 
-static
-void hostif_data_indication(struct ks_wlan_private *priv)
+static int source_address_is_valid(struct ks_wlan_private *priv,
+				   struct ether_hdr *eth_hdr)
 {
-	unsigned int rx_ind_size;	/* indicate data size */
-	struct sk_buff *skb;
-	unsigned short auth_type;
-	unsigned char temp[256];
-
-	unsigned char RecvMIC[8];
-	char buf[128];
-	struct ether_hdr *eth_hdr;
-	unsigned short eth_proto;
-	unsigned long now;
-	struct mic_failure_t *mic_failure;
-	struct ieee802_1x_hdr *aa1x_hdr;
-	struct wpa_eapol_key *eap_key;
-	struct michel_mic_t michel_mic;
-	union iwreq_data wrqu;
-
-	DPRINTK(3, "\n");
-
-	/* min length check */
-	if (priv->rx_size <= ETH_HLEN) {
-		DPRINTK(3, "rx_size = %d\n", priv->rx_size);
-		priv->nstats.rx_errors++;
-		return;
-	}
-
-	auth_type = get_WORD(priv);	/* AuthType */
-	get_WORD(priv);	/* Reserve Area */
-
-	eth_hdr = (struct ether_hdr *)(priv->rxp);
-	eth_proto = ntohs(eth_hdr->h_proto);
-	DPRINTK(3, "ether protocol = %04X\n", eth_proto);
-
-	/* source address check */
 	if (!memcmp(&priv->eth_addr[0], eth_hdr->h_source, ETH_ALEN)) {
 		DPRINTK(1, "invalid : source is own mac address !!\n");
 		DPRINTK(1,
@@ -351,92 +318,19 @@ void hostif_data_indication(struct ks_wlan_private *priv)
 			eth_hdr->h_source[2], eth_hdr->h_source[3],
 			eth_hdr->h_source[4], eth_hdr->h_source[5]);
 		priv->nstats.rx_errors++;
-		return;
-	}
-
-	/*  for WPA */
-	if (auth_type != TYPE_DATA && priv->wpa.rsn_enabled) {
-		if (memcmp(&eth_hdr->h_source[0], &priv->eth_addr[0], ETH_ALEN)) {	/* source address check */
-			if (eth_hdr->h_dest_snap != eth_hdr->h_source_snap) {
-				DPRINTK(1, "invalid data format\n");
-				priv->nstats.rx_errors++;
-				return;
-			}
-			if (((auth_type == TYPE_PMK1
-			      && priv->wpa.pairwise_suite ==
-			      IW_AUTH_CIPHER_TKIP) || (auth_type == TYPE_GMK1
-						       && priv->wpa.
-						       group_suite ==
-						       IW_AUTH_CIPHER_TKIP)
-			     || (auth_type == TYPE_GMK2
-				 && priv->wpa.group_suite ==
-				 IW_AUTH_CIPHER_TKIP))
-			    && priv->wpa.key[auth_type - 1].key_len) {
-				DPRINTK(4, "TKIP: protocol=%04X: size=%u\n",
-					eth_proto, priv->rx_size);
-				/* MIC save */
-				memcpy(&RecvMIC[0],
-				       (priv->rxp) + ((priv->rx_size) - 8), 8);
-				priv->rx_size = priv->rx_size - 8;
-				if (auth_type > 0 && auth_type < 4) {	/* auth_type check */
-					MichaelMICFunction(&michel_mic, (uint8_t *) priv->wpa.key[auth_type - 1].rx_mic_key, (uint8_t *) priv->rxp, (int)priv->rx_size, (uint8_t) 0,	/* priority */
-							   (uint8_t *)
-							   michel_mic.Result);
-				}
-				if (memcmp(michel_mic.Result, RecvMIC, 8)) {
-					now = jiffies;
-					mic_failure = &priv->wpa.mic_failure;
-					/* MIC FAILURE */
-					if (mic_failure->last_failure_time &&
-					    (now -
-					     mic_failure->last_failure_time) /
-					    HZ >= 60) {
-						mic_failure->failure = 0;
-					}
-					DPRINTK(4, "MIC FAILURE\n");
-					if (mic_failure->failure == 0) {
-						mic_failure->failure = 1;
-						mic_failure->counter = 0;
-					} else if (mic_failure->failure == 1) {
-						mic_failure->failure = 2;
-						mic_failure->counter =
-						    (uint16_t) ((now -
-								 mic_failure->
-								 last_failure_time)
-								/ HZ);
-						if (!mic_failure->counter)	/* mic_failure counter value range 1-60 */
-							mic_failure->counter =
-							    1;
-					}
-					priv->wpa.mic_failure.
-					    last_failure_time = now;
-					/*  needed parameters: count, keyid, key type, TSC */
-					sprintf(buf,
-						"MLME-MICHAELMICFAILURE.indication(keyid=%d %scast addr="
-						"%pM)",
-						auth_type - 1,
-						eth_hdr->
-						h_dest[0] & 0x01 ? "broad" :
-						"uni", eth_hdr->h_source);
-					memset(&wrqu, 0, sizeof(wrqu));
-					wrqu.data.length = strlen(buf);
-					DPRINTK(4,
-						"IWEVENT:MICHAELMICFAILURE\n");
-					wireless_send_event(priv->net_dev,
-							    IWEVCUSTOM, &wrqu,
-							    buf);
-					return;
-				}
-			}
-		}
+		return false;
 	}
+	return true;
+}
 
-	if ((priv->connect_status & FORCE_DISCONNECT) ||
-	    priv->wpa.mic_failure.failure == 2) {
-		return;
-	}
+static void check_13th_byte_of_rx_data(struct ks_wlan_private *priv)
+{
+	unsigned int rx_ind_size;	/* indicate data size */
+	struct sk_buff *skb;
+	struct ieee802_1x_hdr *aa1x_hdr;
+	struct wpa_eapol_key *eap_key;
+	unsigned char temp[256];
 
-	/* check 13th byte at rx data */
 	switch (*(priv->rxp + 12)) {
 	case 0xAA:	/* SNAP */
 		rx_ind_size = priv->rx_size - 6;
@@ -506,6 +400,143 @@ void hostif_data_indication(struct ks_wlan_private *priv)
 	}
 }
 
+#define ERROR -1		/* FIXME */
+static int function_needs_naming(struct ks_wlan_private *priv,
+				struct ether_hdr *eth_hdr)
+{
+	unsigned short auth_type;
+	unsigned short eth_proto;
+	unsigned char RecvMIC[8];
+	struct michel_mic_t michel_mic;
+	unsigned long now;
+	char buf[128];
+	struct mic_failure_t *mic_failure;
+	union iwreq_data wrqu;
+
+	if (memcmp(&eth_hdr->h_source[0], &priv->eth_addr[0], ETH_ALEN)) {	/* source address check */
+		if (eth_hdr->h_dest_snap != eth_hdr->h_source_snap) {
+			DPRINTK(1, "invalid data format\n");
+			priv->nstats.rx_errors++;
+			return -ERROR;
+		}
+
+		auth_type = get_WORD(priv);	/* AuthType */
+		eth_proto = ntohs(eth_hdr->h_proto);
+
+		if (((auth_type == TYPE_PMK1
+					&& priv->wpa.pairwise_suite ==
+					IW_AUTH_CIPHER_TKIP) || (auth_type == TYPE_GMK1
+								&& priv->wpa.
+								group_suite ==
+								IW_AUTH_CIPHER_TKIP)
+				|| (auth_type == TYPE_GMK2
+					&& priv->wpa.group_suite ==
+					IW_AUTH_CIPHER_TKIP))
+			&& priv->wpa.key[auth_type - 1].key_len) {
+			DPRINTK(4, "TKIP: protocol=%04X: size=%u\n",
+				eth_proto, priv->rx_size);
+			/* MIC save */
+			memcpy(&RecvMIC[0],
+				(priv->rxp) + ((priv->rx_size) - 8), 8);
+			priv->rx_size = priv->rx_size - 8;
+			if (auth_type > 0 && auth_type < 4) {	/* auth_type check */
+				MichaelMICFunction(&michel_mic, (uint8_t *) priv->wpa.key[auth_type - 1].rx_mic_key, (uint8_t *) priv->rxp, (int)priv->rx_size, (uint8_t) 0,	/* priority */
+						(uint8_t *)
+						michel_mic.Result);
+			}
+			if (memcmp(michel_mic.Result, RecvMIC, 8)) {
+				now = jiffies;
+				mic_failure = &priv->wpa.mic_failure;
+				/* MIC FAILURE */
+				if (mic_failure->last_failure_time &&
+					(now -
+						mic_failure->last_failure_time) /
+					HZ >= 60) {
+					mic_failure->failure = 0;
+				}
+				DPRINTK(4, "MIC FAILURE\n");
+				if (mic_failure->failure == 0) {
+					mic_failure->failure = 1;
+					mic_failure->counter = 0;
+				} else if (mic_failure->failure == 1) {
+					mic_failure->failure = 2;
+					mic_failure->counter =
+						(uint16_t) ((now -
+								mic_failure->
+								last_failure_time)
+							/ HZ);
+					if (!mic_failure->counter)	/* mic_failure counter value range 1-60 */
+						mic_failure->counter =
+							1;
+				}
+				priv->wpa.mic_failure.
+					last_failure_time = now;
+				/*  needed parameters: count, keyid, key type, TSC */
+				sprintf(buf,
+					"MLME-MICHAELMICFAILURE.indication(keyid=%d %scast addr="
+					"%pM)",
+					auth_type - 1,
+					eth_hdr->
+					h_dest[0] & 0x01 ? "broad" :
+					"uni", eth_hdr->h_source);
+				memset(&wrqu, 0, sizeof(wrqu));
+				wrqu.data.length = strlen(buf);
+				DPRINTK(4,
+					"IWEVENT:MICHAELMICFAILURE\n");
+				wireless_send_event(priv->net_dev,
+						IWEVCUSTOM, &wrqu,
+						buf);
+				return -ERROR;
+			}
+		}
+	}
+	return 0;
+}
+
+static
+void hostif_data_indication(struct ks_wlan_private *priv)
+{
+	struct ether_hdr *eth_hdr;
+	unsigned short auth_type;
+	unsigned short eth_proto;
+	int err;
+
+	DPRINTK(3, "\n");
+
+	/* min length check */
+	if (priv->rx_size <= ETH_HLEN) {
+		DPRINTK(3, "rx_size = %d\n", priv->rx_size);
+		priv->nstats.rx_errors++;
+		return;
+	}
+
+	auth_type = get_WORD(priv);	/* AuthType */
+	get_WORD(priv);	/* Reserve Area */
+
+	eth_hdr = (struct ether_hdr *)(priv->rxp);
+	eth_proto = ntohs(eth_hdr->h_proto);
+	DPRINTK(3, "ether protocol = %04X\n", eth_proto);
+
+	if (!source_address_is_valid(priv, eth_hdr))
+		return;
+
+	/*  for WPA */
+	if (auth_type != TYPE_DATA && priv->wpa.rsn_enabled) {
+		err = function_needs_naming(priv, eth_hdr);
+		if (err)
+			return;
+	}
+
+	if ((priv->connect_status & FORCE_DISCONNECT) ||
+	    priv->wpa.mic_failure.failure == 2) {
+		return;
+	}
+
+	/* check 13th byte at rx data */
+	check_13th_byte_of_rx_data(priv);
+}
+
+
 static
 void hostif_mib_get_confirm(struct ks_wlan_private *priv)
 {
-- 
2.7.4

_______________________________________________
devel mailing list
devel@xxxxxxxxxxxxxxxxxxxxxx
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel



[Index of Archives]     [Linux Driver Backports]     [DMA Engine]     [Linux GPIO]     [Linux SPI]     [Video for Linux]     [Linux USB Devel]     [Linux Coverity]     [Linux Audio Users]     [Linux Kernel]     [Linux SCSI]     [Yosemite Backpacking]
  Powered by Linux